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
4afee9fa5adf8d7c3c01a79e52d4e880641b53af
a047a4718edfa935d17231e9e6ecec8c7b701e05
/src/analysis/convex/basic.lean
67ba247b56d8566e5979a7cff8d4be5f4469382b
[ "Apache-2.0" ]
permissive
utensil-contrib/mathlib
bae0c9fafe5e2bdb516efc89d6f8c1502ecc9767
b91909e77e219098a2f8cc031f89d595fe274bd2
refs/heads/master
1,668,048,976,965
1,592,442,701,000
1,592,442,701,000
273,197,855
0
0
null
1,592,472,812,000
1,592,472,811,000
null
UTF-8
Lean
false
false
39,217
lean
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudriashov -/ import data.set.intervals.unordered_interval import data.set.intervals.image_preimage import data.complex.module import algebra.pointwise /-! # Convex sets and functions on real vector spaces In a real vector space, we define the following objects and properties. * `segment x y` is the closed segment joining `x` and `y`. * A set `s` is `convex` if for any two points `x y ∈ s` it includes `segment x y`; * A function `f` is `convex_on` a set `s` if `s` is itself a convex set, and for any two points `x y ∈ s` the segment joining `(x, f x)` to `(y, f y)` is (non-strictly) above the graph of `f`; equivalently, `convex_on f s` means that the epigraph `{p : E × ℝ | p.1 ∈ s ∧ f p.1 ≤ p.2}` is a convex set; * Center mass of a finite set of points with prescribed weights. * Convex hull of a set `s` is the minimal convex set that includes `s`. * Standard simplex `std_simplex ι [fintype ι]` is the intersection of the positive quadrant with the hyperplane `s.sum = 1` in the space `ι → ℝ`. We also provide various equivalent versions of the definitions above, prove that some specific sets are convex, and prove Jensen's inequality. ## Notations We use the following local notations: * `I = Icc (0:ℝ) 1`; * `[x, y] = segment x y`. They are defined using `local notation`, so they are not available outside of this file. -/ universes u' u v w x variables {E : Type u} {F : Type v} {ι : Type w} {ι' : Type x} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {s : set E} open set open_locale classical big_operators local notation `I` := (Icc 0 1 : set ℝ) local attribute [instance] set.pointwise_add set.smul_set section sets /-! ### Segment -/ /-- Segments in a vector space -/ def segment (x y : E) : set E := {z : E | ∃ (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), a • x + b • y = z} local notation `[`x `, ` y `]` := segment x y lemma segment_symm (x y : E) : [x, y] = [y, x] := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ lemma left_mem_segment (x y : E) : x ∈ [x, y] := ⟨1, 0, zero_le_one, le_refl 0, add_zero 1, by rw [zero_smul, one_smul, add_zero]⟩ lemma right_mem_segment (x y : E) : y ∈ [x, y] := segment_symm y x ▸ left_mem_segment y x lemma segment_same (x : E) : [x, x] = {x} := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, by simpa only [(add_smul _ _ _).symm, mem_singleton_iff, hab, one_smul, eq_comm] using hz, λ h, mem_singleton_iff.1 h ▸ left_mem_segment z z⟩ lemma segment_eq_image (x y : E) : segment x y = (λ (θ : ℝ), (1 - θ) • x + θ • y) '' I := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, ⟨b, ⟨hb, hab ▸ le_add_of_nonneg_left ha⟩, hab ▸ hz ▸ by simp only [add_sub_cancel]⟩, λ ⟨θ, ⟨hθ₀, hθ₁⟩, hz⟩, ⟨1-θ, θ, sub_nonneg.2 hθ₁, hθ₀, sub_add_cancel _ _, hz⟩⟩ lemma segment_eq_image' (x y : E) : segment x y = (λ (θ : ℝ), x + θ • (y - x)) '' I := by { convert segment_eq_image x y, ext θ, simp only [smul_sub, sub_smul, one_smul], abel } lemma segment_eq_image₂ (x y : E) : segment x y = (λ p:ℝ×ℝ, p.1 • x + p.2 • y) '' {p | 0 ≤ p.1 ∧ 0 ≤ p.2 ∧ p.1 + p.2 = 1} := by simp only [segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc] lemma segment_eq_Icc {a b : ℝ} (h : a ≤ b) : [a, b] = Icc a b := begin rw [segment_eq_image'], show (((+) a) ∘ (λ t, t * (b - a))) '' Icc 0 1 = Icc a b, rw [image_comp, image_mul_right_Icc (@zero_le_one ℝ _) (sub_nonneg.2 h), image_const_add_Icc], simp end lemma segment_eq_Icc' (a b : ℝ) : [a, b] = Icc (min a b) (max a b) := by cases le_total a b; [skip, rw segment_symm]; simp [segment_eq_Icc, *] lemma segment_eq_interval (a b : ℝ) : segment a b = interval a b := segment_eq_Icc' _ _ lemma mem_segment_translate (a : E) {x b c} : a + x ∈ [a + b, a + c] ↔ x ∈ [b, c] := begin rw [segment_eq_image', segment_eq_image'], refine exists_congr (λ θ, and_congr iff.rfl _), simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj] end lemma segment_translate_preimage (a b c : E) : (λ x, a + x) ⁻¹' [a + b, a + c] = [b, c] := set.ext $ λ x, mem_segment_translate a lemma segment_translate_image (a b c: E) : (λx, a + x) '' [b, c] = [a + b, a + c] := segment_translate_preimage a b c ▸ image_preimage_eq $ add_left_surjective a /-! ### Convexity of sets -/ /-- Convexity of sets -/ def convex (s : set E) := ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ s lemma convex_iff_forall_pos : convex s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := begin refine ⟨λ h x y hx hy a b ha hb hab, h hx hy (le_of_lt ha) (le_of_lt hb) hab, _⟩, intros h x y hx hy a b ha hb hab, cases eq_or_lt_of_le ha with ha ha, { subst a, rw [zero_add] at hab, simp [hab, hy] }, cases eq_or_lt_of_le hb with hb hb, { subst b, rw [add_zero] at hab, simp [hab, hx] }, exact h hx hy ha hb hab end lemma convex_iff_segment_subset : convex s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → [x, y] ⊆ s := by simp only [convex, segment_eq_image₂, subset_def, ball_image_iff, prod.forall, mem_set_of_eq, and_imp] lemma convex.segment_subset (h : convex s) {x y:E} (hx : x ∈ s) (hy : y ∈ s) : [x, y] ⊆ s := convex_iff_segment_subset.1 h hx hy /-- Alternative definition of set convexity, in terms of pointwise set operations. -/ lemma convex_iff_pointwise_add_subset: convex s ↔ ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • s + b • s ⊆ s := iff.intro begin rintros hA a b ha hb hab w ⟨au, ⟨u, hu, rfl⟩, bv, ⟨v, hv, rfl⟩, rfl⟩, exact hA hu hv ha hb hab end (λ h x y hx hy a b ha hb hab, (h ha hb hab) (set.add_mem_pointwise_add ⟨_, hx, rfl⟩ ⟨_, hy, rfl⟩)) /-- Alternative definition of set convexity, using division -/ lemma convex_iff_div: convex s ↔ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a/(a+b)) • x + (b/(a+b)) • y ∈ s := ⟨begin assume h x y hx hy a b ha hb hab, apply h hx hy, have ha', from mul_le_mul_of_nonneg_left ha (le_of_lt (inv_pos.2 hab)), rwa [mul_zero, ←div_eq_inv_mul] at ha', have hb', from mul_le_mul_of_nonneg_left hb (le_of_lt (inv_pos.2 hab)), rwa [mul_zero, ←div_eq_inv_mul] at hb', rw [←add_div], exact div_self (ne_of_lt hab).symm end, begin assume h x y hx hy a b ha hb hab, have h', from h hx hy ha hb, rw [hab, div_one, div_one] at h', exact h' zero_lt_one end⟩ /-! ### Examples of convex sets -/ lemma convex_empty : convex (∅ : set E) := by finish lemma convex_singleton (c : E) : convex ({c} : set E) := begin intros x y hx hy a b ha hb hab, rw [set.eq_of_mem_singleton hx, set.eq_of_mem_singleton hy, ←add_smul, hab, one_smul], exact mem_singleton c end lemma convex_univ : convex (set.univ : set E) := λ _ _ _ _ _ _ _ _ _, trivial lemma convex.inter {t : set E} (hs: convex s) (ht: convex t) : convex (s ∩ t) := λ x y (hx : x ∈ s ∩ t) (hy : y ∈ s ∩ t) a b (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), ⟨hs hx.left hy.left ha hb hab, ht hx.right hy.right ha hb hab⟩ lemma convex_sInter {S : set (set E)} (h : ∀ s ∈ S, convex s) : convex (⋂₀ S) := assume x y hx hy a b ha hb hab s hs, h s hs (hx s hs) (hy s hs) ha hb hab lemma convex_Inter {ι : Sort*} {s: ι → set E} (h: ∀ i : ι, convex (s i)) : convex (⋂ i, s i) := (sInter_range s) ▸ convex_sInter $ forall_range_iff.2 h lemma convex.prod {s : set E} {t : set F} (hs : convex s) (ht : convex t) : convex (s.prod t) := begin intros x y hx hy a b ha hb hab, apply mem_prod.2, exact ⟨hs (mem_prod.1 hx).1 (mem_prod.1 hy).1 ha hb hab, ht (mem_prod.1 hx).2 (mem_prod.1 hy).2 ha hb hab⟩ end lemma convex.is_linear_image (hs : convex s) {f : E → F} (hf : is_linear_map ℝ f) : convex (f '' s) := begin rintros _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩ a b ha hb hab, exact ⟨a • x + b • y, hs hx hy ha hb hab, by simp only [hf.map_add,hf.map_smul]⟩ end lemma convex.linear_image (hs : convex s) (f : E →ₗ[ℝ] F) : convex (image f s) := hs.is_linear_image f.is_linear lemma convex.is_linear_preimage {s : set F} (hs : convex s) {f : E → F} (hf : is_linear_map ℝ f) : convex (preimage f s) := begin intros x y hx hy a b ha hb hab, convert hs hx hy ha hb hab, simp only [mem_preimage, hf.map_add, hf.map_smul] end lemma convex.linear_preimage {s : set F} (hs : convex s) (f : E →ₗ[ℝ] F) : convex (preimage f s) := hs.is_linear_preimage f.is_linear lemma convex.neg (hs : convex s) : convex ((λ z, -z) '' s) := hs.is_linear_image is_linear_map.is_linear_map_neg lemma convex.neg_preimage (hs : convex s) : convex ((λ z, -z) ⁻¹' s) := hs.is_linear_preimage is_linear_map.is_linear_map_neg lemma convex.smul (c : ℝ) (hs : convex s) : convex (c • s) := begin rw smul_set_eq_image, exact hs.is_linear_image (is_linear_map.is_linear_map_smul c) end lemma convex.smul_preimage (c : ℝ) (hs : convex s) : convex ((λ z, c • z) ⁻¹' s) := hs.is_linear_preimage (is_linear_map.is_linear_map_smul c) lemma convex.add {t : set E} (hs : convex s) (ht : convex t) : convex (s + t) := by { rw pointwise_add_eq_image, exact (hs.prod ht).is_linear_image is_linear_map.is_linear_map_add } lemma convex.sub {t : set E} (hs : convex s) (ht : convex t) : convex ((λx : E × E, x.1 - x.2) '' (s.prod t)) := (hs.prod ht).is_linear_image is_linear_map.is_linear_map_sub lemma convex.translate (hs : convex s) (z : E) : convex ((λx, z + x) '' s) := begin convert (convex_singleton z).add hs, ext x, simp [set.mem_image, mem_pointwise_add, eq_comm] end lemma convex.affinity (hs : convex s) (z : E) (c : ℝ) : convex ((λx, z + c • x) '' s) := begin convert (hs.smul c).translate z using 1, erw [smul_set_eq_image, ←image_comp] end lemma convex_real_iff {s : set ℝ} : convex s ↔ ∀ {x y}, x ∈ s → y ∈ s → Icc x y ⊆ s := begin simp only [convex_iff_segment_subset, segment_eq_Icc'], split; intros h x y hx hy, { cases le_or_lt x y with hxy hxy, { simpa [hxy] using h hx hy }, { simp [hxy] } }, { apply h; cases le_total x y; simp [*] } end lemma convex_Iio (r : ℝ) : convex (Iio r) := convex_real_iff.2 $ λ x y hx hy z hz, lt_of_le_of_lt hz.2 hy lemma convex_Ioi (r : ℝ) : convex (Ioi r) := convex_real_iff.2 $ λ x y hx hy z hz, lt_of_lt_of_le hx hz.1 lemma convex_Iic (r : ℝ) : convex (Iic r) := convex_real_iff.2 $ λ x y hx hy z hz, le_trans hz.2 hy lemma convex_Ici (r : ℝ) : convex (Ici r) := convex_real_iff.2 $ λ x y hx hy z hz, le_trans hx hz.1 lemma convex_Ioo (r : ℝ) (s : ℝ) : convex (Ioo r s) := (convex_Ioi _).inter (convex_Iio _) lemma convex_Ico (r : ℝ) (s : ℝ) : convex (Ico r s) := (convex_Ici _).inter (convex_Iio _) lemma convex_Ioc (r : ℝ) (s : ℝ) : convex (Ioc r s) := (convex_Ioi _).inter (convex_Iic _) lemma convex_Icc (r : ℝ) (s : ℝ) : convex (Icc r s) := (convex_Ici _).inter (convex_Iic _) lemma convex_segment (a b : E) : convex [a, b] := begin have : (λ (t : ℝ), a + t • (b - a)) = (λz : E, a + z) ∘ (λt:ℝ, t • (b - a)) := rfl, rw [segment_eq_image', this, image_comp], refine ((convex_Icc _ _).is_linear_image _).translate _, exact is_linear_map.is_linear_map_smul' _ end lemma convex_halfspace_lt {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w < r} := (convex_Iio r).is_linear_preimage h lemma convex_halfspace_le {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w ≤ r} := (convex_Iic r).is_linear_preimage h lemma convex_halfspace_gt {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | r < f w} := (convex_Ioi r).is_linear_preimage h lemma convex_halfspace_ge {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | r ≤ f w} := (convex_Ici r).is_linear_preimage h lemma convex_hyperplane {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w = r} := begin show convex (f ⁻¹' {p | p = r}), rw set_of_eq_eq_singleton, exact (convex_singleton r).is_linear_preimage h end lemma convex_halfspace_re_lt (r : ℝ) : convex {c : ℂ | c.re < r} := convex_halfspace_lt (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_le (r : ℝ) : convex {c : ℂ | c.re ≤ r} := convex_halfspace_le (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_gt (r : ℝ) : convex {c : ℂ | r < c.re } := convex_halfspace_gt (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_lge (r : ℝ) : convex {c : ℂ | r ≤ c.re} := convex_halfspace_ge (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_im_lt (r : ℝ) : convex {c : ℂ | c.im < r} := convex_halfspace_lt (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_le (r : ℝ) : convex {c : ℂ | c.im ≤ r} := convex_halfspace_le (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_gt (r : ℝ) : convex {c : ℂ | r < c.im } := convex_halfspace_gt (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_lge (r : ℝ) : convex {c : ℂ | r ≤ c.im} := convex_halfspace_ge (is_linear_map.mk complex.add_im complex.smul_im) _ section submodule open submodule lemma submodule.convex (K : submodule ℝ E) : convex (↑K : set E) := by { repeat {intro}, refine add_mem _ (smul_mem _ _ _) (smul_mem _ _ _); assumption } lemma subspace.convex (K : subspace ℝ E) : convex (↑K : set E) := K.convex end submodule end sets section functions local notation `[`x `, ` y `]` := segment x y /-! ### Convex functions -/ /-- Convexity of functions -/ def convex_on (s : set E) (f : E → ℝ) : Prop := convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ a * f x + b * f y lemma convex_on_id {s : set ℝ} (hs : convex s) : convex_on s id := ⟨hs, by { intros, refl }⟩ lemma convex_on_const (c : ℝ) (hs : convex s) : convex_on s (λ x:E, c) := ⟨hs, by { intros, simp only [← add_mul, *, one_mul] }⟩ variables {t : set E} {f g : E → ℝ} lemma convex_on_iff_div: convex_on s f ↔ convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → f ((a/(a+b)) • x + (b/(a+b)) • y) ≤ (a/(a+b)) * f x + (b/(a+b)) * f y := and_congr iff.rfl ⟨begin intros h x y hx hy a b ha hb hab, apply h hx hy (div_nonneg ha hab) (div_nonneg hb hab), rw [←add_div], exact div_self (ne_of_gt hab) end, begin intros h x y hx hy a b ha hb hab, simpa [hab, zero_lt_one] using h hx hy ha hb, end⟩ /-- For a function on a convex set in a linear ordered space, in order to prove that it is convex it suffices to verify the inequality `f (a • x + b • y) ≤ a * f x + b * f y` only for `x < y` and positive `a`, `b`. The main use case is `E = ℝ` however one can apply it, e.g., to `ℝ^n` with lexicographic order. -/ lemma linear_order.convex_on_of_lt [linear_order E] (hs : convex s) (hf : ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → x < y → ∀ ⦃a b : ℝ⦄, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) ≤ a * f x + b * f y) : convex_on s f := begin use hs, intros x y hx hy a b ha hb hab, wlog hxy : x<=y using [x y a b, y x b a], { exact le_total _ _ }, { cases eq_or_lt_of_le hxy with hxy hxy, by { subst y, rw [← add_smul, ← add_mul, hab, one_smul, one_mul] }, cases eq_or_lt_of_le ha with ha ha, by { subst a, rw [zero_add] at hab, subst b, simp }, cases eq_or_lt_of_le hb with hb hb, by { subst b, rw [add_zero] at hab, subst a, simp }, exact hf hx hy hxy ha hb hab } end /-- For a function `f` defined on a convex subset `D` of `ℝ`, if for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is less than or equal to the slope of the secant line of `f` on `[x, z]`, then `f` is convex on `D`. This way of proving convexity of a function is used in the proof of convexity of a function with a monotone derivative. -/ lemma convex_on_real_of_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} (hf : ∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) ≤ (f z - f y) / (z - y)) : convex_on s f := linear_order.convex_on_of_lt hs begin assume x z hx hz hxz a b ha hb hab, let y := a * x + b * z, have hxy : x < y, { rw [← one_mul x, ← hab, add_mul], exact add_lt_add_left ((mul_lt_mul_left hb).2 hxz) _ }, have hyz : y < z, { rw [← one_mul z, ← hab, add_mul], exact add_lt_add_right ((mul_lt_mul_left ha).2 hxz) _ }, have : (f y - f x) * (z - y) ≤ (f z - f y) * (y - x), from (div_le_div_iff (sub_pos.2 hxy) (sub_pos.2 hyz)).1 (hf hx hz hxy hyz), have A : z - y + (y - x) = z - x, by abel, have B : 0 < z - x, from sub_pos.2 (lt_trans hxy hyz), rw [sub_mul, sub_mul, sub_le_iff_le_add', ← add_sub_assoc, le_sub_iff_add_le, ← mul_add, A, ← le_div_iff B, add_div, mul_div_assoc, mul_div_assoc, mul_comm (f x), mul_comm (f z)] at this, rw [eq_comm, ← sub_eq_iff_eq_add] at hab; subst a, convert this; symmetry; simp only [div_eq_iff (ne_of_gt B), y]; ring end lemma convex_on.subset (h_convex_on : convex_on t f) (h_subset : s ⊆ t) (h_convex : convex s) : convex_on s f := begin apply and.intro h_convex, intros x y hx hy, exact h_convex_on.2 (h_subset hx) (h_subset hy), end lemma convex_on.add (hf : convex_on s f) (hg : convex_on s g) : convex_on s (λx, f x + g x) := begin apply and.intro hf.1, intros x y hx hy a b ha hb hab, calc f (a • x + b • y) + g (a • x + b • y) ≤ (a * f x + b * f y) + (a * g x + b * g y) : add_le_add (hf.2 hx hy ha hb hab) (hg.2 hx hy ha hb hab) ... = a * f x + a * g x + b * f y + b * g y : by linarith ... = a * (f x + g x) + b * (f y + g y) : by simp [mul_add, add_assoc] end lemma convex_on.smul {c : ℝ} (hc : 0 ≤ c) (hf : convex_on s f) : convex_on s (λx, c * f x) := begin apply and.intro hf.1, intros x y hx hy a b ha hb hab, calc c * f (a • x + b • y) ≤ c * (a * f x + b * f y) : mul_le_mul_of_nonneg_left (hf.2 hx hy ha hb hab) hc ... = a * (c * f x) + b * (c * f y) : by rw mul_add; ac_refl end lemma convex_on.le_on_segment' {x y : E} {a b : ℝ} (hf : convex_on s f) (hx : x ∈ s) (hy : y ∈ s) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : f (a • x + b • y) ≤ max (f x) (f y) := calc f (a • x + b • y) ≤ a * f x + b * f y : hf.2 hx hy ha hb hab ... ≤ a * max (f x) (f y) + b * max (f x) (f y) : add_le_add (mul_le_mul_of_nonneg_left (le_max_left _ _) ha) (mul_le_mul_of_nonneg_left (le_max_right _ _) hb) ... ≤ max (f x) (f y) : by rw [←add_mul, hab, one_mul] lemma convex_on.le_on_segment (hf : convex_on s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ [x, y]) : f z ≤ max (f x) (f y) := let ⟨a, b, ha, hb, hab, hz⟩ := hz in hz ▸ hf.le_on_segment' hx hy ha hb hab lemma convex_on.convex_le (hf : convex_on s f) (r : ℝ) : convex {x ∈ s | f x ≤ r} := convex_iff_segment_subset.2 $ λ x y hx hy z hz, ⟨hf.1.segment_subset hx.1 hy.1 hz, le_trans (hf.le_on_segment hx.1 hy.1 hz) $ max_le hx.2 hy.2⟩ lemma convex_on.convex_lt (hf : convex_on s f) (r : ℝ) : convex {x ∈ s | f x < r} := convex_iff_segment_subset.2 $ λ x y hx hy z hz, ⟨hf.1.segment_subset hx.1 hy.1 hz, lt_of_le_of_lt (hf.le_on_segment hx.1 hy.1 hz) $ max_lt hx.2 hy.2⟩ lemma convex_on.convex_epigraph (hf : convex_on s f) : convex {p : E × ℝ | p.1 ∈ s ∧ f p.1 ≤ p.2} := begin rintros ⟨x, r⟩ ⟨y, t⟩ ⟨hx, hr⟩ ⟨hy, ht⟩ a b ha hb hab, refine ⟨hf.1 hx hy ha hb hab, _⟩, calc f (a • x + b • y) ≤ a * f x + b * f y : hf.2 hx hy ha hb hab ... ≤ a * r + b * t : add_le_add (mul_le_mul_of_nonneg_left hr ha) (mul_le_mul_of_nonneg_left ht hb) end lemma convex_on_iff_convex_epigraph : convex_on s f ↔ convex {p : E × ℝ | p.1 ∈ s ∧ f p.1 ≤ p.2} := begin refine ⟨convex_on.convex_epigraph, λ h, ⟨_, _⟩⟩, { assume x y hx hy a b ha hb hab, exact (@h (x, f x) (y, f y) ⟨hx, le_refl _⟩ ⟨hy, le_refl _⟩ a b ha hb hab).1 }, { assume x y hx hy a b ha hb hab, exact (@h (x, f x) (y, f y) ⟨hx, le_refl _⟩ ⟨hy, le_refl _⟩ a b ha hb hab).2 } end end functions section center_mass /-- Center mass of a finite collection of points with prescribed weights. Note that we require neither `0 ≤ w i` nor `∑ w = 1`. -/ noncomputable def finset.center_mass (t : finset ι) (w : ι → ℝ) (z : ι → E) : E := (∑ i in t, w i)⁻¹ • (∑ i in t, w i • z i) variables (i j : ι) (c : ℝ) (t : finset ι) (w : ι → ℝ) (z : ι → E) open finset lemma finset.center_mass_empty : (∅ : finset ι).center_mass w z = 0 := by simp only [center_mass, sum_empty, smul_zero] lemma finset.center_mass_pair (hne : i ≠ j) : ({i, j} : finset ι).center_mass w z = (w i / (w i + w j)) • z i + (w j / (w i + w j)) • z j := by simp only [center_mass, sum_pair hne, smul_add, (mul_smul _ _ _).symm, div_eq_inv_mul] variable {w} lemma finset.center_mass_insert (ha : i ∉ t) (hw : ∑ j in t, w j ≠ 0) : (insert i t).center_mass w z = (w i / (w i + ∑ j in t, w j)) • z i + ((∑ j in t, w j) / (w i + ∑ j in t, w j)) • t.center_mass w z := begin simp only [center_mass, sum_insert ha, smul_add, (mul_smul _ _ _).symm], congr' 2, { apply mul_comm }, { rw [div_mul_eq_mul_div, mul_inv_cancel hw, one_div_eq_inv] } end lemma finset.center_mass_singleton (hw : w i ≠ 0) : ({i} : finset ι).center_mass w z = z i := by rw [center_mass, sum_singleton, sum_singleton, ← mul_smul, inv_mul_cancel hw, one_smul] lemma finset.center_mass_eq_of_sum_1 (hw : ∑ i in t, w i = 1) : t.center_mass w z = ∑ i in t, w i • z i := by simp only [finset.center_mass, hw, inv_one, one_smul] lemma finset.center_mass_smul : t.center_mass w (λ i, c • z i) = c • t.center_mass w z := by simp only [finset.center_mass, finset.smul_sum, (mul_smul _ _ _).symm, mul_comm c, mul_assoc] /-- A convex combination of two centers of mass is a center of mass as well. This version deals with two different index types. -/ lemma finset.center_mass_segment' (s : finset ι) (t : finset ι') (ws : ι → ℝ) (zs : ι → E) (wt : ι' → ℝ) (zt : ι' → E) (hws : ∑ i in s, ws i = 1) (hwt : ∑ i in t, wt i = 1) (a b : ℝ) (hab : a + b = 1): a • s.center_mass ws zs + b • t.center_mass wt zt = (s.image sum.inl ∪ t.image sum.inr).center_mass (sum.elim (λ i, a * ws i) (λ j, b * wt j)) (sum.elim zs zt) := begin rw [s.center_mass_eq_of_sum_1 _ hws, t.center_mass_eq_of_sum_1 _ hwt, smul_sum, smul_sum, ← finset.sum_sum_elim, finset.center_mass_eq_of_sum_1], { congr, ext ⟨⟩; simp only [sum.elim_inl, sum.elim_inr, mul_smul] }, { rw [sum_sum_elim, ← mul_sum, ← mul_sum, hws, hwt, mul_one, mul_one, hab] } end /-- A convex combination of two centers of mass is a center of mass as well. This version works if two centers of mass share the set of original points. -/ lemma finset.center_mass_segment (s : finset ι) (w₁ w₂ : ι → ℝ) (z : ι → E) (hw₁ : ∑ i in s, w₁ i = 1) (hw₂ : ∑ i in s, w₂ i = 1) (a b : ℝ) (hab : a + b = 1): a • s.center_mass w₁ z + b • s.center_mass w₂ z = s.center_mass (λ i, a * w₁ i + b * w₂ i) z := have hw : ∑ i in s, (a * w₁ i + b * w₂ i) = 1, by simp only [mul_sum.symm, sum_add_distrib, mul_one, *], by simp only [finset.center_mass_eq_of_sum_1, smul_sum, sum_add_distrib, add_smul, mul_smul, *] lemma finset.center_mass_ite_eq (hi : i ∈ t) : t.center_mass (λ j, if (i = j) then 1 else 0) z = z i := begin rw [finset.center_mass_eq_of_sum_1], transitivity ∑ j in t, if (i = j) then z i else 0, { congr, ext i, split_ifs, exacts [h ▸ one_smul _ _, zero_smul _ _] }, { rw [sum_ite_eq, if_pos hi] }, { rw [sum_ite_eq, if_pos hi] } end variables {t w} lemma finset.center_mass_subset {t' : finset ι} (ht : t ⊆ t') (h : ∀ i ∈ t', i ∉ t → w i = 0) : t.center_mass w z = t'.center_mass w z := begin rw [center_mass, sum_subset ht h, smul_sum, center_mass, smul_sum], apply sum_subset ht, assume i hit' hit, rw [h i hit' hit, zero_smul, smul_zero] end lemma finset.center_mass_filter_ne_zero : (t.filter (λ i, w i ≠ 0)).center_mass w z = t.center_mass w z := finset.center_mass_subset z (filter_subset _) $ λ i hit hit', by simpa only [hit, mem_filter, true_and, ne.def, not_not] using hit' variable {z} /-- Center mass of a finite subset of a convex set belongs to the set provided that all weights are non-negative, and the total weight is positive. -/ lemma convex.center_mass_mem (hs : convex s) : (∀ i ∈ t, 0 ≤ w i) → (0 < ∑ i in t, w i) → (∀ i ∈ t, z i ∈ s) → t.center_mass w z ∈ s := begin refine finset.induction (by simp [lt_irrefl]) (λ i t hi ht h₀ hpos hmem, _) t, have zi : z i ∈ s, from hmem _ (mem_insert_self _ _), have hs₀ : ∀ j ∈ t, 0 ≤ w j, from λ j hj, h₀ j $ mem_insert_of_mem hj, rw [sum_insert hi] at hpos, by_cases hsum_t : ∑ j in t, w j = 0, { have ws : ∀ j ∈ t, w j = 0, from (sum_eq_zero_iff_of_nonneg hs₀).1 hsum_t, have wz : ∑ j in t, w j • z j = 0, from sum_eq_zero (λ i hi, by simp [ws i hi]), simp only [center_mass, sum_insert hi, wz, hsum_t, add_zero], simp only [hsum_t, add_zero] at hpos, rw [← mul_smul, inv_mul_cancel (ne_of_gt hpos), one_smul], exact zi }, { rw [finset.center_mass_insert _ _ _ hi hsum_t], refine convex_iff_div.1 hs zi (ht hs₀ _ _) _ (sum_nonneg hs₀) hpos, { exact lt_of_le_of_ne (sum_nonneg hs₀) (ne.symm hsum_t) }, { intros j hj, exact hmem j (mem_insert_of_mem hj) }, { exact h₀ _ (mem_insert_self _ _) } } end lemma convex.sum_mem (hs : convex s) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : ∑ i in t, w i = 1) (hz : ∀ i ∈ t, z i ∈ s) : ∑ i in t, w i • z i ∈ s := by simpa only [h₁, center_mass, inv_one, one_smul] using hs.center_mass_mem h₀ (h₁.symm ▸ zero_lt_one) hz lemma convex_iff_sum_mem : convex s ↔ (∀ (t : finset E) (w : E → ℝ), (∀ i ∈ t, 0 ≤ w i) → ∑ i in t, w i = 1 → (∀ x ∈ t, x ∈ s) → ∑ x in t, w x • x ∈ s ) := begin refine ⟨λ hs t w hw₀ hw₁ hts, hs.sum_mem hw₀ hw₁ hts, _⟩, intros h x y hx hy a b ha hb hab, by_cases h_cases: x = y, { rw [h_cases, ←add_smul, hab, one_smul], exact hy }, { convert h {x, y} (λ z, if z = y then b else a) _ _ _, { simp only [sum_pair h_cases, if_neg h_cases, if_pos rfl] }, { simp_intros i hi, cases hi; subst i; simp [ha, hb, if_neg h_cases] }, { simp only [sum_pair h_cases, if_neg h_cases, if_pos rfl, hab] }, { simp_intros i hi, cases hi; subst i; simp [hx, hy, if_neg h_cases] } } end /-- Jensen's inequality, `finset.center_mass` version. -/ lemma convex_on.map_center_mass_le {f : E → ℝ} (hf : convex_on s f) (h₀ : ∀ i ∈ t, 0 ≤ w i) (hpos : 0 < ∑ i in t, w i) (hmem : ∀ i ∈ t, z i ∈ s) : f (t.center_mass w z) ≤ t.center_mass w (f ∘ z) := begin have hmem' : ∀ i ∈ t, (z i, (f ∘ z) i) ∈ {p : E × ℝ | p.1 ∈ s ∧ f p.1 ≤ p.2}, from λ i hi, ⟨hmem i hi, le_refl _⟩, convert (hf.convex_epigraph.center_mass_mem h₀ hpos hmem').2; simp only [center_mass, function.comp, prod.smul_fst, prod.fst_sum, prod.smul_snd, prod.snd_sum] end /-- Jensen's inequality, `finset.sum` version. -/ lemma convex_on.map_sum_le {f : E → ℝ} (hf : convex_on s f) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : ∑ i in t, w i = 1) (hmem : ∀ i ∈ t, z i ∈ s) : f (∑ i in t, w i • z i) ≤ ∑ i in t, w i * (f (z i)) := by simpa only [center_mass, h₁, inv_one, one_smul] using hf.map_center_mass_le h₀ (h₁.symm ▸ zero_lt_one) hmem /-- If a function `f` is convex on `s` takes value `y` at the center mass of some points `z i ∈ s`, then for some `i` we have `y ≤ f (z i)`. -/ lemma convex_on.exists_ge_of_center_mass {f : E → ℝ} (h : convex_on s f) (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hws : 0 < ∑ i in t, w i) (hz : ∀ i ∈ t, z i ∈ s) : ∃ i ∈ t, f (t.center_mass w z) ≤ f (z i) := begin set y := t.center_mass w z, have : f y ≤ t.center_mass w (f ∘ z) := h.map_center_mass_le hw₀ hws hz, rw ← sum_filter_ne_zero at hws, rw [← finset.center_mass_filter_ne_zero (f ∘ z), center_mass, smul_eq_mul, ← div_eq_inv_mul, le_div_iff hws, mul_sum] at this, replace : ∃ i ∈ t.filter (λ i, w i ≠ 0), f y * w i ≤ w i • (f ∘ z) i := exists_le_of_sum_le (nonempty_of_sum_ne_zero (ne_of_gt hws)) this, rcases this with ⟨i, hi, H⟩, rw [mem_filter] at hi, use [i, hi.1], simp only [smul_eq_mul, mul_comm (w i)] at H, refine (mul_le_mul_right _).1 H, exact lt_of_le_of_ne (hw₀ i hi.1) hi.2.symm end end center_mass section convex_hull variable {t : set E} /-- Convex hull of a set `s` is the minimal convex set that includes `s` -/ def convex_hull (s : set E) : set E := ⋂ (t : set E) (hst : s ⊆ t) (ht : convex t), t variable (s) lemma subset_convex_hull : s ⊆ convex_hull s := set.subset_Inter $ λ t, set.subset_Inter $ λ hst, set.subset_Inter $ λ ht, hst lemma convex_convex_hull : convex (convex_hull s) := convex_Inter $ λ t, convex_Inter $ λ ht, convex_Inter id variable {s} lemma convex_hull_min (hst : s ⊆ t) (ht : convex t) : convex_hull s ⊆ t := set.Inter_subset_of_subset t $ set.Inter_subset_of_subset hst $ set.Inter_subset _ ht lemma convex_hull_mono (hst : s ⊆ t) : convex_hull s ⊆ convex_hull t := convex_hull_min (set.subset.trans hst $ subset_convex_hull t) (convex_convex_hull t) lemma convex.convex_hull_eq {s : set E} (hs : convex s) : convex_hull s = s := set.subset.antisymm (convex_hull_min (set.subset.refl _) hs) (subset_convex_hull s) @[simp] lemma convex_hull_singleton {x : E} : convex_hull ({x} : set E) = {x} := (convex_singleton x).convex_hull_eq lemma is_linear_map.image_convex_hull {f : E → F} (hf : is_linear_map ℝ f) : f '' (convex_hull s) = convex_hull (f '' s) := begin refine set.subset.antisymm _ _, { rw [set.image_subset_iff], exact convex_hull_min (set.image_subset_iff.1 $ subset_convex_hull $ f '' s) ((convex_convex_hull (f '' s)).is_linear_preimage hf) }, { exact convex_hull_min (set.image_subset _ $ subset_convex_hull s) ((convex_convex_hull s).is_linear_image hf) } end lemma linear_map.image_convex_hull (f : E →ₗ[ℝ] F) : f '' (convex_hull s) = convex_hull (f '' s) := f.is_linear.image_convex_hull lemma finset.center_mass_mem_convex_hull (t : finset ι) {w : ι → ℝ} (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hws : 0 < ∑ i in t, w i) {z : ι → E} (hz : ∀ i ∈ t, z i ∈ s) : t.center_mass w z ∈ convex_hull s := (convex_convex_hull s).center_mass_mem hw₀ hws (λ i hi, subset_convex_hull s $ hz i hi) -- TODO : Do we need other versions of the next lemma? /-- Convex hull of `s` is equal to the set of all centers of masses of `finset`s `t`, `z '' t ⊆ s`. This version allows finsets in any type in any universe. -/ lemma convex_hull_eq (s : set E) : convex_hull s = {x : E | ∃ (ι : Type u') (t : finset ι) (w : ι → ℝ) (z : ι → E) (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hw₁ : ∑ i in t, w i = 1) (hz : ∀ i ∈ t, z i ∈ s) , t.center_mass w z = x} := begin refine subset.antisymm (convex_hull_min _ _) _, { intros x hx, use [punit, {punit.star}, λ _, 1, λ _, x, λ _ _, zero_le_one, finset.sum_singleton, λ _ _, hx], simp only [finset.center_mass, finset.sum_singleton, inv_one, one_smul] }, { rintros x y ⟨ι, sx, wx, zx, hwx₀, hwx₁, hzx, rfl⟩ ⟨ι', sy, wy, zy, hwy₀, hwy₁, hzy, rfl⟩ a b ha hb hab, rw [finset.center_mass_segment' _ _ _ _ _ _ hwx₁ hwy₁ _ _ hab], refine ⟨_, _, _, _, _, _, _, rfl⟩, { rintros i hi, rw [finset.mem_union, finset.mem_image, finset.mem_image] at hi, rcases hi with ⟨j, hj, rfl⟩|⟨j, hj, rfl⟩; simp only [sum.elim_inl, sum.elim_inr]; apply_rules [mul_nonneg, hwx₀, hwy₀] }, { simp [finset.sum_sum_elim, finset.mul_sum.symm, *] }, { intros i hi, rw [finset.mem_union, finset.mem_image, finset.mem_image] at hi, rcases hi with ⟨j, hj, rfl⟩|⟨j, hj, rfl⟩; simp only [sum.elim_inl, sum.elim_inr]; apply_rules [hzx, hzy] } }, { rintros _ ⟨ι, t, w, z, hw₀, hw₁, hz, rfl⟩, exact t.center_mass_mem_convex_hull hw₀ (hw₁.symm ▸ zero_lt_one) hz } end /-- Maximum principle for convex functions. If a function `f` is convex on the convex hull of `s`, then `f` can't have a maximum on `convex_hull s` outside of `s`. -/ lemma convex_on.exists_ge_of_mem_convex_hull {f : E → ℝ} (hf : convex_on (convex_hull s) f) {x} (hx : x ∈ convex_hull s) : ∃ y ∈ s, f x ≤ f y := begin rw convex_hull_eq at hx, rcases hx with ⟨α, t, w, z, hw₀, hw₁, hz, rfl⟩, rcases hf.exists_ge_of_center_mass hw₀ (hw₁.symm ▸ zero_lt_one) (λ i hi, subset_convex_hull s (hz i hi)) with ⟨i, hit, Hi⟩, exact ⟨z i, hz i hit, Hi⟩ end lemma finset.convex_hull_eq (s : finset E) : convex_hull ↑s = {x : E | ∃ (w : E → ℝ) (hw₀ : ∀ y ∈ s, 0 ≤ w y) (hw₁ : ∑ y in s, w y = 1), s.center_mass w id = x} := begin refine subset.antisymm (convex_hull_min _ _) _, { intros x hx, rw [finset.mem_coe] at hx, refine ⟨_, _, _, finset.center_mass_ite_eq _ _ _ hx⟩, { intros, split_ifs, exacts [zero_le_one, le_refl 0] }, { rw [finset.sum_ite_eq, if_pos hx] } }, { rintros x y ⟨wx, hwx₀, hwx₁, rfl⟩ ⟨wy, hwy₀, hwy₁, rfl⟩ a b ha hb hab, rw [finset.center_mass_segment _ _ _ _ hwx₁ hwy₁ _ _ hab], refine ⟨_, _, _, rfl⟩, { rintros i hi, apply_rules [add_nonneg, mul_nonneg, hwx₀, hwy₀], }, { simp only [finset.sum_add_distrib, finset.mul_sum.symm, mul_one, *] } }, { rintros _ ⟨w, hw₀, hw₁, rfl⟩, exact s.center_mass_mem_convex_hull (λ x hx, hw₀ _ hx) (hw₁.symm ▸ zero_lt_one) (λ x hx, hx) } end lemma set.finite.convex_hull_eq {s : set E} (hs : finite s) : convex_hull s = {x : E | ∃ (w : E → ℝ) (hw₀ : ∀ y ∈ s, 0 ≤ w y) (hw₁ : ∑ y in hs.to_finset, w y = 1), hs.to_finset.center_mass w id = x} := by simpa only [set.finite.coe_to_finset, set.finite.mem_to_finset, exists_prop] using hs.to_finset.convex_hull_eq lemma convex_hull_eq_union_convex_hull_finite_subsets (s : set E) : convex_hull s = ⋃ (t : finset E) (w : ↑t ⊆ s), convex_hull ↑t := begin refine subset.antisymm _ _, { rw [convex_hull_eq.{u}], rintros x ⟨ι, t, w, z, hw₀, hw₁, hz, rfl⟩, simp only [mem_Union], refine ⟨t.image z, _, _⟩, { rw [finset.coe_image, image_subset_iff], exact hz }, { apply t.center_mass_mem_convex_hull hw₀, { simp only [hw₁, zero_lt_one] }, { exact λ i hi, finset.mem_coe.2 (finset.mem_image_of_mem _ hi) } } }, { exact Union_subset (λ i, Union_subset convex_hull_mono), }, end lemma is_linear_map.convex_hull_image {f : E → F} (hf : is_linear_map ℝ f) (s : set E) : convex_hull (f '' s) = f '' convex_hull s := set.subset.antisymm (convex_hull_min (image_subset _ (subset_convex_hull s)) $ (convex_convex_hull s).is_linear_image hf) (image_subset_iff.2 $ convex_hull_min (image_subset_iff.1 $ subset_convex_hull _) ((convex_convex_hull _).is_linear_preimage hf)) lemma linear_map.convex_hull_image (f : E →ₗ[ℝ] F) (s : set E) : convex_hull (f '' s) = f '' convex_hull s := f.is_linear.convex_hull_image s end convex_hull /-! ### Simplex -/ section simplex variables (ι) [fintype ι] {f : ι → ℝ} /-- Standard simplex in the space of functions `ι → ℝ` is the set of vectors with non-negative coordinates with total sum `1`. -/ def std_simplex (ι : Type*) [fintype ι] : set (ι → ℝ) := { f | (∀ x, 0 ≤ f x) ∧ ∑ x, f x = 1 } lemma std_simplex_eq_inter : std_simplex ι = (⋂ x, {f | 0 ≤ f x}) ∩ {f | ∑ x, f x = 1} := by { ext f, simp only [std_simplex, set.mem_inter_eq, set.mem_Inter, set.mem_set_of_eq] } lemma convex_std_simplex : convex (std_simplex ι) := begin refine λ f g hf hg a b ha hb hab, ⟨λ x, _, _⟩, { apply_rules [add_nonneg, mul_nonneg, hf.1, hg.1] }, { erw [finset.sum_add_distrib, ← finset.smul_sum, ← finset.smul_sum, hf.2, hg.2, smul_eq_mul, smul_eq_mul, mul_one, mul_one], exact hab } end variable {ι} lemma ite_eq_mem_std_simplex (i : ι) : (λ j, ite (i = j) (1:ℝ) 0) ∈ std_simplex ι := ⟨λ j, by simp only []; split_ifs; norm_num, by rw [finset.sum_ite_eq, if_pos (finset.mem_univ _)] ⟩ /-- `std_simplex ι` is the convex hull of the canonical basis in `ι → ℝ`. -/ lemma convex_hull_basis_eq_std_simplex : convex_hull (range $ λ(i j:ι), if i = j then (1:ℝ) else 0) = std_simplex ι := begin refine subset.antisymm (convex_hull_min _ (convex_std_simplex ι)) _, { rintros _ ⟨i, rfl⟩, exact ite_eq_mem_std_simplex i }, { rintros w ⟨hw₀, hw₁⟩, rw [pi_eq_sum_univ w, ← finset.univ.center_mass_eq_of_sum_1 _ hw₁], exact finset.univ.center_mass_mem_convex_hull (λ i hi, hw₀ i) (hw₁.symm ▸ zero_lt_one) (λ i hi, mem_range_self i) } end variable {ι} /-- Convex hull of a finite set is the image of the standard simplex in `s → ℝ` under the linear map sending each function `w` to `∑ x in s, w x • x`. Since we have no sums over finite sets, we use sum over `@finset.univ _ hs.fintype`. The map is defined in terms of operations on `(s → ℝ) →ₗ[ℝ] ℝ` so that later we will not need to prove that this map is linear. -/ lemma set.finite.convex_hull_eq_image {s : set E} (hs : finite s) : convex_hull s = by haveI := hs.fintype; exact (⇑(∑ x : s, (@linear_map.proj ℝ s _ (λ i, ℝ) _ _ x).smul_right x.1)) '' (std_simplex s) := begin rw [← convex_hull_basis_eq_std_simplex, ← linear_map.convex_hull_image, ← range_comp, (∘)], apply congr_arg, convert (subtype.range_val s).symm, ext x, simp [linear_map.sum_apply, ite_smul, finset.filter_eq] end /-- All values of a function `f ∈ std_simplex ι` belong to `[0, 1]`. -/ lemma mem_Icc_of_mem_std_simplex (hf : f ∈ std_simplex ι) (x) : f x ∈ I := ⟨hf.1 x, hf.2 ▸ finset.single_le_sum (λ y hy, hf.1 y) (finset.mem_univ x)⟩ end simplex
7e7b00360959d8e43ab493d53f3893c3486029f6
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/order/zorn_atoms.lean
f009f8d38cf931c41f84294e66346a75346f1c3c
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
1,746
lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import order.zorn import order.atoms /-! # Zorn lemma for (co)atoms In this file we use Zorn's lemma to prove that a partial order is atomic if every nonempty chain `c`, `⊥ ∉ c`, has a lower bound not equal to `⊥`. We also prove the order dual version of this statement. -/ open set /-- **Zorn's lemma**: A partial order is coatomic if every nonempty chain `c`, `⊤ ∉ c`, has an upper bound not equal to `⊤`. -/ lemma is_coatomic.of_is_chain_bounded {α : Type*} [partial_order α] [order_top α] (h : ∀ c : set α, is_chain (≤) c → c.nonempty → ⊤ ∉ c → ∃ x ≠ ⊤, x ∈ upper_bounds c) : is_coatomic α := begin refine ⟨λ x, le_top.eq_or_lt.imp_right $ λ hx, _⟩, rcases zorn_nonempty_partial_order₀ (Ico x ⊤) (λ c hxc hc y hy, _) x (left_mem_Ico.2 hx) with ⟨y, ⟨hxy, hy⟩, -, hy'⟩, { refine ⟨y, ⟨hy.ne, λ z hyz, le_top.eq_or_lt.resolve_right $ λ hz, _⟩, hxy⟩, exact hyz.ne' (hy' z ⟨hxy.trans hyz.le, hz⟩ hyz.le) }, { rcases h c hc ⟨y, hy⟩ (λ h, (hxc h).2.ne rfl) with ⟨z, hz, hcz⟩, exact ⟨z, ⟨le_trans (hxc hy).1 (hcz hy), hz.lt_top⟩, hcz⟩ } end /-- **Zorn's lemma**: A partial order is atomic if every nonempty chain `c`, `⊥ ∉ c`, has an lower bound not equal to `⊥`. -/ lemma is_atomic.of_is_chain_bounded {α : Type*} [partial_order α] [order_bot α] (h : ∀ c : set α, is_chain (≤) c → c.nonempty → ⊥ ∉ c → ∃ x ≠ ⊥, x ∈ lower_bounds c) : is_atomic α := is_coatomic_dual_iff_is_atomic.mp $ is_coatomic.of_is_chain_bounded $ λ c hc, h c hc.symm
5fb283122784644b291786b32c03b701c17db7c9
c777c32c8e484e195053731103c5e52af26a25d1
/src/algebra/category/Module/adjunctions.lean
e03b0271ee9ce99831e68e144912bf68608e29c8
[ "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
11,067
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johan Commelin -/ import algebra.category.Module.monoidal import category_theory.monoidal.functorial import category_theory.monoidal.types import linear_algebra.direct_sum.finsupp import category_theory.linear.linear_functor /-! The functor of forming finitely supported functions on a type with values in a `[ring R]` is the left adjoint of the forgetful functor from `R`-modules to types. -/ noncomputable theory open category_theory namespace Module universe u open_locale classical variables (R : Type u) section variables [ring R] /-- The free functor `Type u ⥤ Module R` sending a type `X` to the free `R`-module with generators `x : X`, implemented as the type `X →₀ R`. -/ @[simps] def free : Type u ⥤ Module R := { obj := λ X, Module.of R (X →₀ R), map := λ X Y f, finsupp.lmap_domain _ _ f, map_id' := by { intros, exact finsupp.lmap_domain_id _ _ }, map_comp' := by { intros, exact finsupp.lmap_domain_comp _ _ _ _, } } /-- The free-forgetful adjunction for R-modules. -/ def adj : free R ⊣ forget (Module.{u} R) := adjunction.mk_of_hom_equiv { hom_equiv := λ X M, (finsupp.lift M R X).to_equiv.symm, hom_equiv_naturality_left_symm' := λ _ _ M f g, finsupp.lhom_ext' (λ x, linear_map.ext_ring (finsupp.sum_map_domain_index_add_monoid_hom (λ y, ((smul_add_hom R M).flip) (g y))).symm) } instance : is_right_adjoint (forget (Module.{u} R)) := ⟨_, adj R⟩ end namespace free variables [comm_ring R] local attribute [ext] tensor_product.ext /-- (Implementation detail) The unitor for `free R`. -/ def ε : 𝟙_ (Module.{u} R) ⟶ (free R).obj (𝟙_ (Type u)) := finsupp.lsingle punit.star @[simp] lemma ε_apply (r : R) : ε R r = finsupp.single punit.star r := rfl /-- (Implementation detail) The tensorator for `free R`. -/ def μ (α β : Type u) : (free R).obj α ⊗ (free R).obj β ≅ (free R).obj (α ⊗ β) := (finsupp_tensor_finsupp' R α β).to_Module_iso lemma μ_natural {X Y X' Y' : Type u} (f : X ⟶ Y) (g : X' ⟶ Y') : ((free R).map f ⊗ (free R).map g) ≫ (μ R Y Y').hom = (μ R X X').hom ≫ (free R).map (f ⊗ g) := begin intros, ext x x' ⟨y, y'⟩, dsimp [μ], simp_rw [finsupp.map_domain_single, finsupp_tensor_finsupp'_single_tmul_single, mul_one, finsupp.map_domain_single, category_theory.tensor_apply], end lemma left_unitality (X : Type u) : (λ_ ((free R).obj X)).hom = (ε R ⊗ 𝟙 ((free R).obj X)) ≫ (μ R (𝟙_ (Type u)) X).hom ≫ map (free R).obj (λ_ X).hom := begin intros, ext, dsimp [ε, μ], simp_rw [finsupp_tensor_finsupp'_single_tmul_single, Module.monoidal_category.left_unitor_hom_apply, finsupp.smul_single', mul_one, finsupp.map_domain_single, category_theory.left_unitor_hom_apply], end lemma right_unitality (X : Type u) : (ρ_ ((free R).obj X)).hom = (𝟙 ((free R).obj X) ⊗ ε R) ≫ (μ R X (𝟙_ (Type u))).hom ≫ map (free R).obj (ρ_ X).hom := begin intros, ext, dsimp [ε, μ], simp_rw [finsupp_tensor_finsupp'_single_tmul_single, Module.monoidal_category.right_unitor_hom_apply, finsupp.smul_single', mul_one, finsupp.map_domain_single, category_theory.right_unitor_hom_apply], end lemma associativity (X Y Z : Type u) : ((μ R X Y).hom ⊗ 𝟙 ((free R).obj Z)) ≫ (μ R (X ⊗ Y) Z).hom ≫ map (free R).obj (α_ X Y Z).hom = (α_ ((free R).obj X) ((free R).obj Y) ((free R).obj Z)).hom ≫ (𝟙 ((free R).obj X) ⊗ (μ R Y Z).hom) ≫ (μ R X (Y ⊗ Z)).hom := begin intros, ext, dsimp [μ], simp_rw [finsupp_tensor_finsupp'_single_tmul_single, finsupp.map_domain_single, mul_one, category_theory.associator_hom_apply], end /-- The free R-module functor is lax monoidal. -/ -- In fact, it's strong monoidal, but we don't yet have a typeclass for that. @[simps] instance : lax_monoidal.{u} (free R).obj := { -- Send `R` to `punit →₀ R` ε := ε R, -- Send `(α →₀ R) ⊗ (β →₀ R)` to `α × β →₀ R` μ := λ X Y, (μ R X Y).hom, μ_natural' := λ X Y X' Y' f g, μ_natural R f g, left_unitality' := left_unitality R, right_unitality' := right_unitality R, associativity' := associativity R, } instance : is_iso (lax_monoidal.ε (free R).obj) := ⟨⟨finsupp.lapply punit.star, ⟨by { ext, simp, }, by { ext ⟨⟩ ⟨⟩, simp, }⟩⟩⟩ end free variables [comm_ring R] /-- The free functor `Type u ⥤ Module R`, as a monoidal functor. -/ def monoidal_free : monoidal_functor (Type u) (Module.{u} R) := { ε_is_iso := by { dsimp, apply_instance, }, μ_is_iso := λ X Y, by { dsimp, apply_instance, }, ..lax_monoidal_functor.of (free R).obj } example (X Y : Type u) : (free R).obj (X × Y) ≅ (free R).obj X ⊗ (free R).obj Y := ((monoidal_free R).μ_iso X Y).symm end Module namespace category_theory universes v u /-- `Free R C` is a type synonym for `C`, which, given `[comm_ring R]` and `[category C]`, we will equip with a category structure where the morphisms are formal `R`-linear combinations of the morphisms in `C`. -/ @[nolint unused_arguments has_nonempty_instance] def Free (R : Type*) (C : Type u) := C /-- Consider an object of `C` as an object of the `R`-linear completion. It may be preferable to use `(Free.embedding R C).obj X` instead; this functor can also be used to lift morphisms. -/ def Free.of (R : Type*) {C : Type u} (X : C) : Free R C := X variables (R : Type*) [comm_ring R] (C : Type u) [category.{v} C] open finsupp -- Conceptually, it would be nice to construct this via "transport of enrichment", -- using the fact that `Module.free R : Type ⥤ Module R` and `Module.forget` are both lax monoidal. -- This still seems difficult, so we just do it by hand. instance category_Free : category (Free R C) := { hom := λ (X Y : C), (X ⟶ Y) →₀ R, id := λ (X : C), finsupp.single (𝟙 X) 1, comp := λ (X Y Z : C) f g, f.sum (λ f' s, g.sum (λ g' t, finsupp.single (f' ≫ g') (s * t))), assoc' := λ W X Y Z f g h, begin dsimp, -- This imitates the proof of associativity for `monoid_algebra`. simp only [sum_sum_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, category.assoc, mul_assoc, zero_mul, mul_zero, sum_zero, sum_add], end }. namespace Free section local attribute [reducible] category_theory.category_Free instance : preadditive (Free R C) := { hom_group := λ X Y, finsupp.add_comm_group, add_comp' := λ X Y Z f f' g, begin dsimp, rw [finsupp.sum_add_index']; { simp [add_mul], } end, comp_add' := λ X Y Z f g g', begin dsimp, rw ← finsupp.sum_add, congr, ext r h, rw [finsupp.sum_add_index']; { simp [mul_add], }, end, } instance : linear R (Free R C) := { hom_module := λ X Y, finsupp.module (X ⟶ Y) R, smul_comp' := λ X Y Z r f g, begin dsimp, rw [finsupp.sum_smul_index]; simp [finsupp.smul_sum, mul_assoc], end, comp_smul' := λ X Y Z f r g, begin dsimp, simp_rw [finsupp.smul_sum], congr, ext h s, rw [finsupp.sum_smul_index]; simp [finsupp.smul_sum, mul_left_comm], end, } lemma single_comp_single {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) (r s : R) : (single f r ≫ single g s : (Free.of R X) ⟶ (Free.of R Z)) = single (f ≫ g) (r * s) := by { dsimp, simp, } end local attribute [simp] single_comp_single /-- A category embeds into its `R`-linear completion. -/ @[simps] def embedding : C ⥤ Free R C := { obj := λ X, X, map := λ X Y f, finsupp.single f 1, map_id' := λ X, rfl, map_comp' := λ X Y Z f g, by simp, } variables (R) {C} {D : Type u} [category.{v} D] [preadditive D] [linear R D] open preadditive linear /-- A functor to an `R`-linear category lifts to a functor from its `R`-linear completion. -/ @[simps] def lift (F : C ⥤ D) : Free R C ⥤ D := { obj := λ X, F.obj X, map := λ X Y f, f.sum (λ f' r, r • (F.map f')), map_id' := by { dsimp [category_theory.category_Free], simp }, map_comp' := λ X Y Z f g, begin apply finsupp.induction_linear f, { simp only [limits.zero_comp, sum_zero_index] }, { intros f₁ f₂ w₁ w₂, rw add_comp, rw [finsupp.sum_add_index', finsupp.sum_add_index'], { simp only [w₁, w₂, add_comp] }, { intros, rw zero_smul }, { intros, simp only [add_smul], }, { intros, rw zero_smul }, { intros, simp only [add_smul], }, }, { intros f' r, apply finsupp.induction_linear g, { simp only [limits.comp_zero, sum_zero_index] }, { intros f₁ f₂ w₁ w₂, rw comp_add, rw [finsupp.sum_add_index', finsupp.sum_add_index'], { simp only [w₁, w₂, comp_add], }, { intros, rw zero_smul }, { intros, simp only [add_smul], }, { intros, rw zero_smul }, { intros, simp only [add_smul], }, }, { intros g' s, erw single_comp_single, simp [mul_comm r s, mul_smul] } } end, } @[simp] lemma lift_map_single (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) (r : R) : (lift R F).map (single f r) = r • F.map f := by simp instance lift_additive (F : C ⥤ D) : (lift R F).additive := { map_add' := λ X Y f g, begin dsimp, rw finsupp.sum_add_index'; simp [add_smul] end, } instance lift_linear (F : C ⥤ D) : (lift R F).linear R := { map_smul' := λ X Y f r, begin dsimp, rw finsupp.sum_smul_index; simp [finsupp.smul_sum, mul_smul], end, } /-- The embedding into the `R`-linear completion, followed by the lift, is isomorphic to the original functor. -/ def embedding_lift_iso (F : C ⥤ D) : embedding R C ⋙ lift R F ≅ F := nat_iso.of_components (λ X, iso.refl _) (by tidy) /-- Two `R`-linear functors out of the `R`-linear completion are isomorphic iff their compositions with the embedding functor are isomorphic. -/ @[ext] def ext {F G : Free R C ⥤ D} [F.additive] [F.linear R] [G.additive] [G.linear R] (α : embedding R C ⋙ F ≅ embedding R C ⋙ G) : F ≅ G := nat_iso.of_components (λ X, α.app X) begin intros X Y f, apply finsupp.induction_linear f, { simp, }, { intros f₁ f₂ w₁ w₂, simp only [F.map_add, G.map_add, add_comp, comp_add, w₁, w₂], }, { intros f' r, rw [iso.app_hom, iso.app_hom, ←smul_single_one, F.map_smul, G.map_smul, smul_comp, comp_smul], change r • (embedding R C ⋙ F).map f' ≫ _ = r • _ ≫ (embedding R C ⋙ G).map f', rw α.hom.naturality f', apply_instance, -- Why are these not picked up automatically when we rewrite? apply_instance, } end /-- `Free.lift` is unique amongst `R`-linear functors `Free R C ⥤ D` which compose with `embedding ℤ C` to give the original functor. -/ def lift_unique (F : C ⥤ D) (L : Free R C ⥤ D) [L.additive] [L.linear R] (α : embedding R C ⋙ L ≅ F) : L ≅ lift R F := ext R (α.trans (embedding_lift_iso R F).symm) end Free end category_theory
d3b8378f0e4f8ea4f23c2f28047ab106931411e2
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/data/equiv/module.lean
c14248ef3fdeceddb1ee1499f6e2c2806a844523
[ "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
14,972
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro, Anne Baanen, Frédéric Dupuis, Heather Macbeth -/ import algebra.module.linear_map /-! # (Semi)linear equivalences In this file we define * `linear_equiv σ M M₂`, `M ≃ₛₗ[σ] M₂`: an invertible semilinear map. Here, `σ` is a `ring_hom` from `R` to `R₂` and an `e : M ≃ₛₗ[σ] M₂` satisfies `e (c • x) = (σ c) • (e x)`. The plain linear version, with `σ` being `ring_hom.id R`, is denoted by `M ≃ₗ[R] M₂`. ## Implementation notes To ensure that composition works smoothly for semilinear equivalences, we use the typeclasses `ring_hom_comp_triple`, `ring_hom_inv_pair` and `ring_hom_surjective` from `algebra/ring/comp_typeclasses`. The group structure on automorphisms, `linear_equiv.automorphism_group`, is provided elsewhere. ## TODO * Parts of this file have not yet been generalized to semilinear maps ## Tags linear equiv, linear equivalences, linear isomorphism, linear isomorphic -/ open function open_locale big_operators universes u u' v w x y z variables {R : Type*} {R₁ : Type*} {R₂ : Type*} {R₃ : Type*} variables {k : Type*} {S : Type*} {M : Type*} {M₁ : Type*} {M₂ : Type*} {M₃ : Type*} variables {N₁ : Type*} {N₂ : Type*} {N₃ : Type*} {N₄ : Type*} {ι : Type*} section set_option old_structure_cmd true /-- A linear equivalence is an invertible linear map. -/ @[nolint has_inhabited_instance] structure linear_equiv {R : Type*} {S : Type*} [semiring R] [semiring S] (σ : R →+* S) {σ' : S →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] (M : Type*) (M₂ : Type*) [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module S M₂] extends linear_map σ M M₂, M ≃+ M₂ end attribute [nolint doc_blame] linear_equiv.to_linear_map attribute [nolint doc_blame] linear_equiv.to_add_equiv notation M ` ≃ₛₗ[`:50 σ `] ` M₂ := linear_equiv σ M M₂ notation M ` ≃ₗ[`:50 R `] ` M₂ := linear_equiv (ring_hom.id R) M M₂ namespace linear_equiv section add_comm_monoid variables {M₄ : Type*} variables [semiring R] [semiring S] section variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] variables [module R M] [module S M₂] {σ : R →+* S} {σ' : S →+* R} variables [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] include R include σ' instance : has_coe (M ≃ₛₗ[σ] M₂) (M →ₛₗ[σ] M₂) := ⟨to_linear_map⟩ -- see Note [function coercion] instance : has_coe_to_fun (M ≃ₛₗ[σ] M₂) := ⟨_, λ f, f.to_fun⟩ @[simp] lemma coe_mk {to_fun inv_fun map_add map_smul left_inv right_inv } : ⇑(⟨to_fun, map_add, map_smul, inv_fun, left_inv, right_inv⟩ : M ≃ₛₗ[σ] M₂) = to_fun := rfl -- This exists for compatibility, previously `≃ₗ[R]` extended `≃` instead of `≃+`. @[nolint doc_blame] def to_equiv : (M ≃ₛₗ[σ] M₂) → M ≃ M₂ := λ f, f.to_add_equiv.to_equiv lemma to_equiv_injective : function.injective (to_equiv : (M ≃ₛₗ[σ] M₂) → M ≃ M₂) := λ ⟨_, _, _, _, _, _⟩ ⟨_, _, _, _, _, _⟩ h, linear_equiv.mk.inj_eq.mpr (equiv.mk.inj h) @[simp] lemma to_equiv_inj {e₁ e₂ : M ≃ₛₗ[σ] M₂} : e₁.to_equiv = e₂.to_equiv ↔ e₁ = e₂ := to_equiv_injective.eq_iff lemma to_linear_map_injective : function.injective (coe : (M ≃ₛₗ[σ] M₂) → (M →ₛₗ[σ] M₂)) := λ e₁ e₂ H, to_equiv_injective $ equiv.ext $ linear_map.congr_fun H @[simp, norm_cast] lemma to_linear_map_inj {e₁ e₂ : M ≃ₛₗ[σ] M₂} : (e₁ : M →ₛₗ[σ] M₂) = e₂ ↔ e₁ = e₂ := to_linear_map_injective.eq_iff end section variables [semiring R₁] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] variables [add_comm_monoid M₃] [add_comm_monoid M₄] variables [add_comm_monoid N₁] [add_comm_monoid N₂] variables {module_M : module R M} {module_S_M₂ : module S M₂} {σ : R →+* S} {σ' : S →+* R} variables {re₁ : ring_hom_inv_pair σ σ'} {re₂ : ring_hom_inv_pair σ' σ} variables (e e' : M ≃ₛₗ[σ] M₂) lemma to_linear_map_eq_coe : e.to_linear_map = (e : M →ₛₗ[σ] M₂) := rfl @[simp, norm_cast] theorem coe_coe : ⇑(e : M →ₛₗ[σ] M₂) = e := rfl @[simp] lemma coe_to_equiv : ⇑e.to_equiv = e := rfl @[simp] lemma coe_to_linear_map : ⇑e.to_linear_map = e := rfl @[simp] lemma to_fun_eq_coe : e.to_fun = e := rfl section variables {e e'} @[ext] lemma ext (h : ∀ x, e x = e' x) : e = e' := to_equiv_injective (equiv.ext h) protected lemma congr_arg : Π {x x' : M}, x = x' → e x = e x' | _ _ rfl := rfl protected lemma congr_fun (h : e = e') (x : M) : e x = e' x := h ▸ rfl lemma ext_iff : e = e' ↔ ∀ x, e x = e' x := ⟨λ h x, h ▸ rfl, ext⟩ end section variables (M R) /-- The identity map is a linear equivalence. -/ @[refl] def refl [module R M] : M ≃ₗ[R] M := { .. linear_map.id, .. equiv.refl M } end @[simp] lemma refl_apply [module R M] (x : M) : refl R M x = x := rfl include module_M module_S_M₂ re₁ re₂ /-- Linear equivalences are symmetric. -/ @[symm] def symm (e : M ≃ₛₗ[σ] M₂) : M₂ ≃ₛₗ[σ'] M := { to_fun := e.to_linear_map.inverse e.inv_fun e.left_inv e.right_inv, inv_fun := e.to_equiv.symm.inv_fun, map_smul' := λ r x, by simp, .. e.to_linear_map.inverse e.inv_fun e.left_inv e.right_inv, .. e.to_equiv.symm } omit module_M module_S_M₂ re₁ re₂ /-- See Note [custom simps projection] -/ def simps.symm_apply {R : Type*} {S : Type*} [semiring R] [semiring S] {σ : R →+* S} {σ' : S →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] {M : Type*} {M₂ : Type*} [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module S M₂] (e : M ≃ₛₗ[σ] M₂) : M₂ → M := e.symm initialize_simps_projections linear_equiv (to_fun → apply, inv_fun → symm_apply) include σ' @[simp] lemma inv_fun_eq_symm : e.inv_fun = e.symm := rfl omit σ' variables {module_M₁ : module R₁ M₁} {module_M₂ : module R₂ M₂} {module_M₃ : module R₃ M₃} variables {module_N₁ : module R₁ N₁} {module_N₂ : module R₁ N₂} variables {σ₁₂ : R₁ →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R₁ →+* R₃} variables {σ₂₁ : R₂ →+* R₁} {σ₃₂ : R₃ →+* R₂} {σ₃₁ : R₃ →+* R₁} variables [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] variables [ring_hom_comp_triple σ₃₂ σ₂₁ σ₃₁] variables {re₁₂ : ring_hom_inv_pair σ₁₂ σ₂₁} {re₂₃ : ring_hom_inv_pair σ₂₃ σ₃₂} variables [ring_hom_inv_pair σ₁₃ σ₃₁] {re₂₁ : ring_hom_inv_pair σ₂₁ σ₁₂} variables {re₃₂ : ring_hom_inv_pair σ₃₂ σ₂₃} [ring_hom_inv_pair σ₃₁ σ₁₃] variables (e₁₂ : M₁ ≃ₛₗ[σ₁₂] M₂) (e₂₃ : M₂ ≃ₛₗ[σ₂₃] M₃) include σ₃₁ /-- Linear equivalences are transitive. -/ -- Note: The linter thinks the `ring_hom_comp_triple` argument is doubled -- it is not. @[trans, nolint unused_arguments] def trans : M₁ ≃ₛₗ[σ₁₃] M₃ := { .. e₂₃.to_linear_map.comp e₁₂.to_linear_map, .. e₁₂.to_equiv.trans e₂₃.to_equiv } omit σ₃₁ infixl ` ≪≫ₗ `:80 := @linear_equiv.trans _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) ring_hom_comp_triple.ids ring_hom_comp_triple.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids variables {e₁₂} {e₂₃} @[simp] lemma coe_to_add_equiv : ⇑(e.to_add_equiv) = e := rfl /-- The two paths coercion can take to an `add_monoid_hom` are equivalent -/ lemma to_add_monoid_hom_commutes : e.to_linear_map.to_add_monoid_hom = e.to_add_equiv.to_add_monoid_hom := rfl include σ₃₁ @[simp] theorem trans_apply (c : M₁) : (e₁₂.trans e₂₃ : M₁ ≃ₛₗ[σ₁₃] M₃) c = e₂₃ (e₁₂ c) := rfl omit σ₃₁ include σ' @[simp] theorem apply_symm_apply (c : M₂) : e (e.symm c) = c := e.right_inv c @[simp] theorem symm_apply_apply (b : M) : e.symm (e b) = b := e.left_inv b omit σ' include σ₃₁ σ₂₁ σ₃₂ @[simp] lemma symm_trans_apply (c : M₃) : (e₁₂.trans e₂₃ : M₁ ≃ₛₗ[σ₁₃] M₃).symm c = e₁₂.symm (e₂₃.symm c) := rfl omit σ₃₁ σ₂₁ σ₃₂ @[simp] lemma trans_refl : e.trans (refl S M₂) = e := to_equiv_injective e.to_equiv.trans_refl @[simp] lemma refl_trans : (refl R M).trans e = e := to_equiv_injective e.to_equiv.refl_trans include σ' lemma symm_apply_eq {x y} : e.symm x = y ↔ x = e y := e.to_equiv.symm_apply_eq lemma eq_symm_apply {x y} : y = e.symm x ↔ e y = x := e.to_equiv.eq_symm_apply omit σ' @[simp] lemma refl_symm [module R M] : (refl R M).symm = linear_equiv.refl R M := rfl @[simp] lemma trans_symm [module R M] [module R M₂] (f : M ≃ₗ[R] M₂) : f.trans f.symm = linear_equiv.refl R M := by { ext x, simp } @[simp] lemma symm_trans [module R M] [module R M₂] (f : M ≃ₗ[R] M₂) : f.symm.trans f = linear_equiv.refl R M₂ := by { ext x, simp } @[simp, norm_cast] lemma refl_to_linear_map [module R M] : (linear_equiv.refl R M : M →ₗ[R] M) = linear_map.id := rfl @[simp, norm_cast] lemma comp_coe [module R M] [module R M₂] [module R M₃] (f : M ≃ₗ[R] M₂) (f' : M₂ ≃ₗ[R] M₃) : (f' : M₂ →ₗ[R] M₃).comp (f : M →ₗ[R] M₂) = (f.trans f' : M ≃ₗ[R] M₃) := rfl @[simp] lemma mk_coe (h₁ h₂ f h₃ h₄) : (linear_equiv.mk e h₁ h₂ f h₃ h₄ : M ≃ₛₗ[σ] M₂) = e := ext $ λ _, rfl @[simp] theorem map_add (a b : M) : e (a + b) = e a + e b := e.map_add' a b @[simp] theorem map_zero : e 0 = 0 := e.to_linear_map.map_zero @[simp] theorem map_smulₛₗ (c : R) (x : M) : e (c • x) = (σ c) • e x := e.map_smul' c x include module_N₁ module_N₂ theorem map_smul (e : N₁ ≃ₗ[R₁] N₂) (c : R₁) (x : N₁) : e (c • x) = c • e x := map_smulₛₗ _ _ _ omit module_N₁ module_N₂ @[simp] lemma map_sum {s : finset ι} (u : ι → M) : e (∑ i in s, u i) = ∑ i in s, e (u i) := e.to_linear_map.map_sum @[simp] theorem map_eq_zero_iff {x : M} : e x = 0 ↔ x = 0 := e.to_add_equiv.map_eq_zero_iff theorem map_ne_zero_iff {x : M} : e x ≠ 0 ↔ x ≠ 0 := e.to_add_equiv.map_ne_zero_iff include module_M module_S_M₂ re₁ re₂ @[simp] theorem symm_symm (e : M ≃ₛₗ[σ] M₂): e.symm.symm = e := by { cases e, refl } omit module_M module_S_M₂ re₁ re₂ lemma symm_bijective [module R M] [module S M₂] [ring_hom_inv_pair σ' σ] [ring_hom_inv_pair σ σ'] : function.bijective (symm : (M ≃ₛₗ[σ] M₂) → (M₂ ≃ₛₗ[σ'] M)) := equiv.bijective ⟨(symm : (M ≃ₛₗ[σ] M₂) → (M₂ ≃ₛₗ[σ'] M)), (symm : (M₂ ≃ₛₗ[σ'] M) → (M ≃ₛₗ[σ] M₂)), symm_symm, symm_symm⟩ @[simp] lemma mk_coe' (f h₁ h₂ h₃ h₄) : (linear_equiv.mk f h₁ h₂ ⇑e h₃ h₄ : M₂ ≃ₛₗ[σ'] M) = e.symm := symm_bijective.injective $ ext $ λ x, rfl include σ' @[simp] theorem symm_mk (f h₁ h₂ h₃ h₄) : (⟨e, h₁, h₂, f, h₃, h₄⟩ : M ≃ₛₗ[σ] M₂).symm = { to_fun := f, inv_fun := e, ..(⟨e, h₁, h₂, f, h₃, h₄⟩ : M ≃ₛₗ[σ] M₂).symm } := rfl omit σ' @[simp] lemma coe_symm_mk [module R M] [module R M₂] {to_fun inv_fun map_add map_smul left_inv right_inv} : ⇑((⟨to_fun, map_add, map_smul, inv_fun, left_inv, right_inv⟩ : M ≃ₗ[R] M₂).symm) = inv_fun := rfl protected lemma bijective : function.bijective e := e.to_equiv.bijective protected lemma injective : function.injective e := e.to_equiv.injective protected lemma surjective : function.surjective e := e.to_equiv.surjective include σ' protected lemma image_eq_preimage (s : set M) : e '' s = e.symm ⁻¹' s := e.to_equiv.image_eq_preimage s omit σ' end variables [semiring R₁] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] /-- An involutive linear map is a linear equivalence. -/ def of_involutive {σ σ' : R →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] {module_M : module R M} (f : M →ₛₗ[σ] M) (hf : involutive f) : M ≃ₛₗ[σ] M := { .. f, .. hf.to_equiv f } @[simp] lemma coe_of_involutive {σ σ' : R →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] {module_M : module R M} (f : M →ₛₗ[σ] M) (hf : involutive f) : ⇑(of_involutive f hf) = f := rfl section restrict_scalars variables (R) [module R M] [module R M₂] [module S M] [module S M₂] [linear_map.compatible_smul M M₂ R S] /-- If `M` and `M₂` are both `R`-semimodules and `S`-semimodules and `R`-semimodule structures are defined by an action of `R` on `S` (formally, we have two scalar towers), then any `S`-linear equivalence from `M` to `M₂` is also an `R`-linear equivalence. See also `linear_map.restrict_scalars`. -/ @[simps] def restrict_scalars (f : M ≃ₗ[S] M₂) : M ≃ₗ[R] M₂ := { to_fun := f, inv_fun := f.symm, left_inv := f.left_inv, right_inv := f.right_inv, .. f.to_linear_map.restrict_scalars R } lemma restrict_scalars_injective : function.injective (restrict_scalars R : (M ≃ₗ[S] M₂) → (M ≃ₗ[R] M₂)) := λ f g h, ext (linear_equiv.congr_fun h : _) @[simp] lemma restrict_scalars_inj (f g : M ≃ₗ[S] M₂) : f.restrict_scalars R = g.restrict_scalars R ↔ f = g := (restrict_scalars_injective R).eq_iff end restrict_scalars end add_comm_monoid end linear_equiv namespace module /-- `g : R ≃+* S` is `R`-linear when the module structure on `S` is `module.comp_hom S g` . -/ @[simps] def comp_hom.to_linear_equiv {R S : Type*} [semiring R] [semiring S] (g : R ≃+* S) : (by haveI := comp_hom S (↑g : R →+* S); exact (R ≃ₗ[R] S)) := by exact { to_fun := (g : R → S), inv_fun := (g.symm : S → R), map_smul' := g.map_mul, ..g } end module namespace distrib_mul_action variables (R M) [semiring R] [add_comm_monoid M] [module R M] section variables [monoid S] [distrib_mul_action S M] [smul_comm_class S R M] /-- Each element of the monoid defines a linear map. This is a stronger version of `distrib_mul_action.to_add_monoid_hom`. -/ @[simps] def to_linear_map (s : S) : M →ₗ[R] M := { to_fun := has_scalar.smul s, map_add' := smul_add s, map_smul' := λ a b, smul_comm _ _ _ } end section variables [group S] [distrib_mul_action S M] [smul_comm_class S R M] /-- Each element of the group defines a linear equivalence. This is a stronger version of `distrib_mul_action.to_add_equiv`. -/ @[simps] def to_linear_equiv (s : S) : M ≃ₗ[R] M := { ..to_add_equiv M s, ..to_linear_map R M s } end end distrib_mul_action
9b8b20200e55712434a7dfd74290656fd1e35804
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Meta/Tactic/FVarSubst.lean
92bc23ccf00b8147f233fdad9206482750126210
[ "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,246
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 Std.Data.AssocList import Lean.Expr import Lean.LocalContext import Lean.Util.ReplaceExpr namespace Lean namespace Meta /- Some tactics substitute hypotheses with expressions. We track these substitutions using `FVarSubst`. It is just a mapping from the original FVarId (internal) name to an expression. The free variables occurring in the expression must be defined in the new goal. -/ structure FVarSubst := (map : Std.AssocList FVarId Expr := {}) namespace FVarSubst def empty : FVarSubst := {} def isEmpty (s : FVarSubst) : Bool := s.map.isEmpty def contains (s : FVarSubst) (fvarId : FVarId) : Bool := s.map.contains fvarId /- Add entry `fvarId |-> v` to `s` if `s` does not contain an entry for `fvarId`. -/ def insert (s : FVarSubst) (fvarId : FVarId) (v : Expr) : FVarSubst := if s.contains fvarId then s else let map := s.map.mapVal fun e => e.replaceFVarId fvarId v; { map := map.insert fvarId v } def erase (s : FVarSubst) (fvarId : FVarId) : FVarSubst := { map := s.map.erase fvarId } def find? (s : FVarSubst) (fvarId : FVarId) : Option Expr := s.map.find? fvarId def get (s : FVarSubst) (fvarId : FVarId) : Expr := match s.map.find? fvarId with | none => mkFVar fvarId -- it has not been replaced | some v => v /-- Given `e`, for each `(x => v)` in `s` replace `x` with `v` in `e` -/ def apply (s : FVarSubst) (e : Expr) : Expr := if s.map.isEmpty then e else if !e.hasFVar then e else e.replace $ fun e => match e with | Expr.fvar fvarId _ => match s.map.find? fvarId with | none => e | some v => v | _ => none def domain (s : FVarSubst) : List FVarId := s.map.foldl (fun r k v => k :: r) [] def any (p : FVarId → Expr → Bool) (s : FVarSubst) : Bool := s.map.any p end FVarSubst end Meta def LocalDecl.applyFVarSubst (s : Meta.FVarSubst) : LocalDecl → LocalDecl | LocalDecl.cdecl i id n t bi => LocalDecl.cdecl i id n (s.apply t) bi | LocalDecl.ldecl i id n t v nd => LocalDecl.ldecl i id n (s.apply t) (s.apply v) nd abbrev Expr.applyFVarSubst (s : Meta.FVarSubst) (e : Expr) : Expr := s.apply e end Lean
14caa8dfc60541f5980181f81fa7e0a86660ce63
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/order/galois_connection.lean
a2e3d64cd68a6a5c00fe43b9b56a9e1ddc32e2a4
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
25,267
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl -/ import order.complete_lattice import order.rel_iso /-! # Galois connections, insertions and coinsertions Galois connections are order theoretic adjoints, i.e. a pair of functions `u` and `l`, such that `∀a b, l a ≤ b ↔ a ≤ u b`. ## Main definitions * `galois_connection`: A Galois connection is a pair of functions `l` and `u` satisfying `l a ≤ b ↔ a ≤ u b`. They are special cases of adjoint functors in category theory, but do not depend on the category theory library in mathlib. * `galois_insertion`: A Galois insertion is a Galois connection where `l ∘ u = id` * `galois_coinsertion`: A Galois coinsertion is a Galois connection where `u ∘ l = id` ## Implementation details Galois insertions can be used to lift order structures from one type to another. For example if `α` is a complete lattice, and `l : α → β`, and `u : β → α` form a Galois insertion, then `β` is also a complete lattice. `l` is the lower adjoint and `u` is the upper adjoint. An example of this is the Galois insertion is in group thery. If `G` is a topological space, then there is a Galois insertion between the set of subsets of `G`, `set G`, and the set of subgroups of `G`, `subgroup G`. The left adjoint is `subgroup.closure`, taking the `subgroup` generated by a `set`, The right adjoint is the coercion from `subgroup G` to `set G`, taking the underlying set of an subgroup. Naively lifting a lattice structure along this Galois insertion would mean that the definition of `inf` on subgroups would be `subgroup.closure (↑S ∩ ↑T)`. This is an undesirable definition because the intersection of subgroups is already a subgroup, so there is no need to take the closure. For this reason a `choice` function is added as a field to the `galois_insertion` structure. It has type `Π S : set G, ↑(closure S) ≤ S → subgroup G`. When `↑(closure S) ≤ S`, then `S` is already a subgroup, so this function can be defined using `subgroup.mk` and not `closure`. This means the infimum of subgroups will be defined to be the intersection of sets, paired with a proof that intersection of subgroups is a subgroup, rather than the closure of the intersection. -/ open function set universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {a a₁ a₂ : α} {b b₁ b₂ : β} /-- A Galois connection is a pair of functions `l` and `u` satisfying `l a ≤ b ↔ a ≤ u b`. They are special cases of adjoint functors in category theory, but do not depend on the category theory library in mathlib. -/ def galois_connection [preorder α] [preorder β] (l : α → β) (u : β → α) := ∀a b, l a ≤ b ↔ a ≤ u b /-- Makes a Galois connection from an order-preserving bijection. -/ theorem order_iso.to_galois_connection [preorder α] [preorder β] (oi : α ≃o β) : galois_connection oi oi.symm := λ b g, oi.rel_symm_apply.symm namespace galois_connection section variables [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) lemma monotone_intro (hu : monotone u) (hl : monotone l) (hul : ∀ a, a ≤ u (l a)) (hlu : ∀ a, l (u a) ≤ a) : galois_connection l u := assume a b, ⟨assume h, le_trans (hul _) (hu h), assume h, le_trans (hl h) (hlu _)⟩ include gc lemma l_le {a : α} {b : β} : a ≤ u b → l a ≤ b := (gc _ _).mpr lemma le_u {a : α} {b : β} : l a ≤ b → a ≤ u b := (gc _ _).mp lemma le_u_l (a) : a ≤ u (l a) := gc.le_u $ le_refl _ lemma l_u_le (a) : l (u a) ≤ a := gc.l_le $ le_refl _ lemma monotone_u : monotone u := assume a b H, gc.le_u (le_trans (gc.l_u_le a) H) lemma monotone_l : monotone l := assume a b H, gc.l_le (le_trans H (gc.le_u_l b)) lemma upper_bounds_l_image_subset {s : set α} : upper_bounds (l '' s) ⊆ u ⁻¹' upper_bounds s := assume b hb c, assume : c ∈ s, gc.le_u (hb (mem_image_of_mem _ ‹c ∈ s›)) lemma lower_bounds_u_image_subset {s : set β} : lower_bounds (u '' s) ⊆ l ⁻¹' lower_bounds s := assume a ha c, assume : c ∈ s, gc.l_le (ha (mem_image_of_mem _ ‹c ∈ s›)) lemma is_lub_l_image {s : set α} {a : α} (h : is_lub s a) : is_lub (l '' s) (l a) := ⟨gc.monotone_l.mem_upper_bounds_image $ and.elim_left ‹is_lub s a›, assume b hb, gc.l_le $ and.elim_right ‹is_lub s a› $ gc.upper_bounds_l_image_subset hb⟩ lemma is_glb_u_image {s : set β} {b : β} (h : is_glb s b) : is_glb (u '' s) (u b) := ⟨gc.monotone_u.mem_lower_bounds_image $ and.elim_left ‹is_glb s b›, assume a ha, gc.le_u $ and.elim_right ‹is_glb s b› $ gc.lower_bounds_u_image_subset ha⟩ lemma is_glb_l {a : α} : is_glb { b | a ≤ u b } (l a) := ⟨assume b, gc.l_le, assume b h, h $ gc.le_u_l _⟩ lemma is_lub_u {b : β} : is_lub { a | l a ≤ b } (u b) := ⟨assume b, gc.le_u, assume b h, h $ gc.l_u_le _⟩ end section partial_order variables [partial_order α] [partial_order β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma u_l_u_eq_u : u ∘ l ∘ u = u := funext (assume x, le_antisymm (gc.monotone_u (gc.l_u_le _)) (gc.le_u_l _)) lemma l_u_l_eq_l : l ∘ u ∘ l = l := funext (assume x, le_antisymm (gc.l_u_le _) (gc.monotone_l (gc.le_u_l _))) lemma l_unique {l' : α → β} {u' : β → α} (gc' : galois_connection l' u') (hu : ∀ b, u b = u' b) {a : α} : l a = l' a := le_antisymm (gc.l_le $ (hu (l' a)).symm ▸ gc'.le_u_l _) (gc'.l_le $ hu (l a) ▸ gc.le_u_l _) lemma u_unique {l' : α → β} {u' : β → α} (gc' : galois_connection l' u') (hl : ∀ a, l a = l' a) {b : β} : u b = u' b := le_antisymm (gc'.le_u $ hl (u b) ▸ gc.l_u_le _) (gc.le_u $ (hl (u' b)).symm ▸ gc'.l_u_le _) end partial_order section order_top variables [order_top α] [order_top β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma u_top : u ⊤ = ⊤ := (gc.is_glb_u_image is_glb_empty).unique $ by simp only [is_glb_empty, image_empty] end order_top section order_bot variables [order_bot α] [order_bot β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma l_bot : l ⊥ = ⊥ := (gc.is_lub_l_image is_lub_empty).unique $ by simp only [is_lub_empty, image_empty] end order_bot section semilattice_sup variables [semilattice_sup α] [semilattice_sup β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma l_sup : l (a₁ ⊔ a₂) = l a₁ ⊔ l a₂ := (gc.is_lub_l_image is_lub_pair).unique $ by simp only [image_pair, is_lub_pair] end semilattice_sup section semilattice_inf variables [semilattice_inf α] [semilattice_inf β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma u_inf : u (b₁ ⊓ b₂) = u b₁ ⊓ u b₂ := (gc.is_glb_u_image is_glb_pair).unique $ by simp only [image_pair, is_glb_pair] end semilattice_inf section complete_lattice variables [complete_lattice α] [complete_lattice β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma l_supr {f : ι → α} : l (supr f) = (⨆i, l (f i)) := eq.symm $ is_lub.supr_eq $ show is_lub (range (l ∘ f)) (l (supr f)), by rw [range_comp, ← Sup_range]; exact gc.is_lub_l_image (is_lub_Sup _) lemma u_infi {f : ι → β} : u (infi f) = (⨅i, u (f i)) := eq.symm $ is_glb.infi_eq $ show is_glb (range (u ∘ f)) (u (infi f)), by rw [range_comp, ← Inf_range]; exact gc.is_glb_u_image (is_glb_Inf _) lemma l_Sup {s : set α} : l (Sup s) = (⨆a∈s, l a) := by simp only [Sup_eq_supr, gc.l_supr] lemma u_Inf {s : set β} : u (Inf s) = (⨅a∈s, u a) := by simp only [Inf_eq_infi, gc.u_infi] end complete_lattice /- Constructing Galois connections -/ section constructions protected lemma id [pα : preorder α] : @galois_connection α α pα pα id id := assume a b, iff.intro (λx, x) (λx, x) protected lemma compose [preorder α] [preorder β] [preorder γ] (l1 : α → β) (u1 : β → α) (l2 : β → γ) (u2 : γ → β) (gc1 : galois_connection l1 u1) (gc2 : galois_connection l2 u2) : galois_connection (l2 ∘ l1) (u1 ∘ u2) := by intros a b; rw [gc2, gc1] protected lemma dual [pα : preorder α] [pβ : preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) : @galois_connection (order_dual β) (order_dual α) _ _ u l := assume a b, (gc _ _).symm protected lemma dfun {ι : Type u} {α : ι → Type v} {β : ι → Type w} [∀i, preorder (α i)] [∀i, preorder (β i)] (l : Πi, α i → β i) (u : Πi, β i → α i) (gc : ∀i, galois_connection (l i) (u i)) : @galois_connection (Π i, α i) (Π i, β i) _ _ (λa i, l i (a i)) (λb i, u i (b i)) := assume a b, forall_congr $ assume i, gc i (a i) (b i) end constructions end galois_connection namespace nat lemma galois_connection_mul_div {k : ℕ} (h : 0 < k) : galois_connection (λn, n * k) (λn, n / k) := assume x y, (le_div_iff_mul_le x y h).symm end nat /-- A Galois insertion is a Galois connection where `l ∘ u = id`. It also contains a constructive choice function, to give better definitional equalities when lifting order structures. Dual to `galois_coinsertion` -/ @[nolint has_inhabited_instance] structure galois_insertion {α β : Type*} [preorder α] [preorder β] (l : α → β) (u : β → α) := (choice : Πx:α, u (l x) ≤ x → β) (gc : galois_connection l u) (le_l_u : ∀x, x ≤ l (u x)) (choice_eq : ∀a h, choice a h = l a) /-- A constructor for a Galois insertion with the trivial `choice` function. -/ def galois_insertion.monotone_intro {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} (hu : monotone u) (hl : monotone l) (hul : ∀ a, a ≤ u (l a)) (hlu : ∀ b, l (u b) = b) : galois_insertion l u := { choice := λ x _, l x, gc := galois_connection.monotone_intro hu hl hul (λ b, le_of_eq (hlu b)), le_l_u := λ b, le_of_eq $ (hlu b).symm, choice_eq := λ _ _, rfl } /-- Makes a Galois insertion from an order-preserving bijection. -/ protected def rel_iso.to_galois_insertion [preorder α] [preorder β] (oi : α ≃o β) : @galois_insertion α β _ _ (oi) (oi.symm) := { choice := λ b h, oi b, gc := oi.to_galois_connection, le_l_u := λ g, le_of_eq (oi.right_inv g).symm, choice_eq := λ b h, rfl } /-- Make a `galois_insertion l u` from a `galois_connection l u` such that `∀ b, b ≤ l (u b)` -/ def galois_connection.to_galois_insertion {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) (h : ∀ b, b ≤ l (u b)) : galois_insertion l u := { choice := λ x _, l x, gc := gc, le_l_u := h, choice_eq := λ _ _, rfl } /-- Lift the bottom along a Galois connection -/ def galois_connection.lift_order_bot {α β : Type*} [order_bot α] [partial_order β] {l : α → β} {u : β → α} (gc : galois_connection l u) : order_bot β := { bot := l ⊥, bot_le := assume b, gc.l_le $ bot_le, .. ‹partial_order β› } namespace galois_insertion variables {l : α → β} {u : β → α} lemma l_u_eq [preorder α] [partial_order β] (gi : galois_insertion l u) (b : β) : l (u b) = b := le_antisymm (gi.gc.l_u_le _) (gi.le_l_u _) lemma l_surjective [preorder α] [partial_order β] (gi : galois_insertion l u) : surjective l := assume b, ⟨u b, gi.l_u_eq b⟩ lemma u_injective [preorder α] [partial_order β] (gi : galois_insertion l u) : injective u := assume a b h, calc a = l (u a) : (gi.l_u_eq a).symm ... = l (u b) : congr_arg l h ... = b : gi.l_u_eq b lemma l_sup_u [semilattice_sup α] [semilattice_sup β] (gi : galois_insertion l u) (a b : β) : l (u a ⊔ u b) = a ⊔ b := calc l (u a ⊔ u b) = l (u a) ⊔ l (u b) : gi.gc.l_sup ... = a ⊔ b : by simp only [gi.l_u_eq] lemma l_supr_u [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → β) : l (⨆ i, u (f i)) = ⨆ i, (f i) := calc l (⨆ (i : ι), u (f i)) = ⨆ (i : ι), l (u (f i)) : gi.gc.l_supr ... = ⨆ (i : ι), f i : congr_arg _ $ funext $ λ i, gi.l_u_eq (f i) lemma l_supr_of_ul_eq_self [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → α) (hf : ∀ i, u (l (f i)) = f i) : l (⨆ i, (f i)) = ⨆ i, l (f i) := calc l (⨆ (i : ι), (f i)) = l ⨆ (i : ι), (u (l (f i))) : by simp [hf] ... = ⨆ (i : ι), l (f i) : gi.l_supr_u _ lemma l_inf_u [semilattice_inf α] [semilattice_inf β] (gi : galois_insertion l u) (a b : β) : l (u a ⊓ u b) = a ⊓ b := calc l (u a ⊓ u b) = l (u (a ⊓ b)) : congr_arg l gi.gc.u_inf.symm ... = a ⊓ b : by simp only [gi.l_u_eq] lemma l_infi_u [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → β) : l (⨅ i, u (f i)) = ⨅ i, (f i) := calc l (⨅ (i : ι), u (f i)) = l (u (⨅ (i : ι), (f i))) : congr_arg l gi.gc.u_infi.symm ... = ⨅ (i : ι), f i : gi.l_u_eq _ lemma l_infi_of_ul_eq_self [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → α) (hf : ∀ i, u (l (f i)) = f i) : l (⨅ i, (f i)) = ⨅ i, l (f i) := calc l (⨅ i, (f i)) = l ⨅ (i : ι), (u (l (f i))) : by simp [hf] ... = ⨅ i, l (f i) : gi.l_infi_u _ lemma u_le_u_iff [preorder α] [preorder β] (gi : galois_insertion l u) {a b} : u a ≤ u b ↔ a ≤ b := ⟨λ h, le_trans (gi.le_l_u _) (gi.gc.l_le h), λ h, gi.gc.monotone_u h⟩ lemma strict_mono_u [preorder α] [partial_order β] (gi : galois_insertion l u) : strict_mono u := strict_mono_of_le_iff_le $ λ _ _, gi.u_le_u_iff.symm section lift variables [partial_order β] /-- Lift the suprema along a Galois insertion -/ def lift_semilattice_sup [semilattice_sup α] (gi : galois_insertion l u) : semilattice_sup β := { sup := λa b, l (u a ⊔ u b), le_sup_left := assume a b, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_sup_left, le_sup_right := assume a b, le_trans (gi.le_l_u b) $ gi.gc.monotone_l $ le_sup_right, sup_le := assume a b c hac hbc, gi.gc.l_le $ sup_le (gi.gc.monotone_u hac) (gi.gc.monotone_u hbc), .. ‹partial_order β› } /-- Lift the infima along a Galois insertion -/ def lift_semilattice_inf [semilattice_inf α] (gi : galois_insertion l u) : semilattice_inf β := { inf := λa b, gi.choice (u a ⊓ u b) $ (le_inf (gi.gc.monotone_u $ gi.gc.l_le $ inf_le_left) (gi.gc.monotone_u $ gi.gc.l_le $ inf_le_right)), inf_le_left := by simp only [gi.choice_eq]; exact assume a b, gi.gc.l_le inf_le_left, inf_le_right := by simp only [gi.choice_eq]; exact assume a b, gi.gc.l_le inf_le_right, le_inf := by simp only [gi.choice_eq]; exact assume a b c hac hbc, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_inf (gi.gc.monotone_u hac) (gi.gc.monotone_u hbc), .. ‹partial_order β› } /-- Lift the suprema and infima along a Galois insertion -/ def lift_lattice [lattice α] (gi : galois_insertion l u) : lattice β := { .. gi.lift_semilattice_sup, .. gi.lift_semilattice_inf } /-- Lift the top along a Galois insertion -/ def lift_order_top [order_top α] (gi : galois_insertion l u) : order_top β := { top := gi.choice ⊤ $ le_top, le_top := by simp only [gi.choice_eq]; exact assume b, le_trans (gi.le_l_u b) (gi.gc.monotone_l le_top), .. ‹partial_order β› } /-- Lift the top, bottom, suprema, and infima along a Galois insertion -/ def lift_bounded_lattice [bounded_lattice α] (gi : galois_insertion l u) : bounded_lattice β := { .. gi.lift_lattice, .. gi.lift_order_top, .. gi.gc.lift_order_bot } /-- Lift all suprema and infima along a Galois insertion -/ def lift_complete_lattice [complete_lattice α] (gi : galois_insertion l u) : complete_lattice β := { Sup := λs, l (⨆ b∈s, u b), Sup_le := assume s a hs, gi.gc.l_le $ supr_le $ assume b, supr_le $ assume hb, gi.gc.monotone_u $ hs _ hb, le_Sup := assume s a ha, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_supr_of_le a $ le_supr_of_le ha $ le_refl _, Inf := λs, gi.choice (⨅ b∈s, u b) $ le_infi $ assume b, le_infi $ assume hb, gi.gc.monotone_u $ gi.gc.l_le $ infi_le_of_le b $ infi_le_of_le hb $ le_refl _, Inf_le := by simp only [gi.choice_eq]; exact assume s a ha, gi.gc.l_le $ infi_le_of_le a $ infi_le_of_le ha $ le_refl _, le_Inf := by simp only [gi.choice_eq]; exact assume s a hs, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_infi $ assume b, show u a ≤ ⨅ (H : b ∈ s), u b, from le_infi $ assume hb, gi.gc.monotone_u $ hs _ hb, .. gi.lift_bounded_lattice } end lift end galois_insertion /-- A Galois coinsertion is a Galois connection where `u ∘ l = id`. It also contains a constructive choice function, to give better definitional equalities when lifting order structures. Dual to `galois_insertion` -/ @[nolint has_inhabited_instance] structure galois_coinsertion {α β : Type*} [preorder α] [preorder β] (l : α → β) (u : β → α) := (choice : Πx:β, x ≤ l (u x) → α) (gc : galois_connection l u) (u_l_le : ∀x, u (l x) ≤ x) (choice_eq : ∀a h, choice a h = u a) /-- Make a `galois_insertion u l` in the `order_dual`, from a `galois_coinsertion l u` -/ def galois_coinsertion.dual {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} : galois_coinsertion l u → @galois_insertion (order_dual β) (order_dual α) _ _ u l := λ x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Make a `galois_coinsertion u l` in the `order_dual`, from a `galois_insertion l u` -/ def galois_insertion.dual {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} : galois_insertion l u → @galois_coinsertion (order_dual β) (order_dual α) _ _ u l := λ x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Make a `galois_coinsertion l u` from a `galois_insertion l u` in the `order_dual` -/ def galois_coinsertion.of_dual {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} : @galois_insertion (order_dual β) (order_dual α) _ _ u l → galois_coinsertion l u := λ x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Make a `galois_insertion l u` from a `galois_coinsertion l u` in the `order_dual` -/ def galois_insertion.of_dual {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} : @galois_coinsertion (order_dual β) (order_dual α) _ _ u l → galois_insertion l u := λ x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Makes a Galois coinsertion from an order-preserving bijection. -/ protected def rel_iso.to_galois_coinsertion [preorder α] [preorder β] (oi : α ≃o β) : @galois_coinsertion α β _ _ (oi) (oi.symm) := { choice := λ b h, oi.symm b, gc := oi.to_galois_connection, u_l_le := λ g, le_of_eq (oi.left_inv g), choice_eq := λ b h, rfl } /-- A constructor for a Galois coinsertion with the trivial `choice` function. -/ def galois_coinsertion.monotone_intro {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} (hu : monotone u) (hl : monotone l) (hlu : ∀ b, l (u b) ≤ b) (hul : ∀ a, u (l a) = a) : galois_coinsertion l u := galois_coinsertion.of_dual (galois_insertion.monotone_intro hl.order_dual hu.order_dual hlu hul) /-- Make a `galois_coinsertion l u` from a `galois_connection l u` such that `∀ b, b ≤ l (u b)` -/ def galois_connection.to_galois_coinsertion {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) (h : ∀ a, u (l a) ≤ a) : galois_coinsertion l u := { choice := λ x _, u x, gc := gc, u_l_le := h, choice_eq := λ _ _, rfl } /-- Lift the top along a Galois connection -/ def galois_connection.lift_order_top {α β : Type*} [partial_order α] [order_top β] {l : α → β} {u : β → α} (gc : galois_connection l u) : order_top α := { top := u ⊤, le_top := assume b, gc.le_u $ le_top, .. ‹partial_order α› } namespace galois_coinsertion variables {l : α → β} {u : β → α} lemma u_l_eq [partial_order α] [preorder β] (gi : galois_coinsertion l u) (a : α) : u (l a) = a := gi.dual.l_u_eq a lemma u_surjective [partial_order α] [preorder β] (gi : galois_coinsertion l u) : surjective u := gi.dual.l_surjective lemma l_injective [partial_order α] [preorder β] (gi : galois_coinsertion l u) : injective l := gi.dual.u_injective lemma u_inf_l [semilattice_inf α] [semilattice_inf β] (gi : galois_coinsertion l u) (a b : α) : u (l a ⊓ l b) = a ⊓ b := gi.dual.l_sup_u a b lemma u_infi_l [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → α) : u (⨅ i, l (f i)) = ⨅ i, (f i) := gi.dual.l_supr_u _ lemma u_infi_of_lu_eq_self [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → β) (hf : ∀ i, l (u (f i)) = f i) : u (⨅ i, (f i)) = ⨅ i, u (f i) := gi.dual.l_supr_of_ul_eq_self _ hf lemma u_sup_l [semilattice_sup α] [semilattice_sup β] (gi : galois_coinsertion l u) (a b : α) : u (l a ⊔ l b) = a ⊔ b := gi.dual.l_inf_u _ _ lemma u_supr_l [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → α) : u (⨆ i, l (f i)) = ⨆ i, (f i) := gi.dual.l_infi_u _ lemma u_supr_of_lu_eq_self [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → β) (hf : ∀ i, l (u (f i)) = f i) : u (⨆ i, (f i)) = ⨆ i, u (f i) := gi.dual.l_infi_of_ul_eq_self _ hf lemma l_le_l_iff [preorder α] [preorder β] (gi : galois_coinsertion l u) {a b} : l a ≤ l b ↔ a ≤ b := gi.dual.u_le_u_iff lemma strict_mono_l [partial_order α] [preorder β] (gi : galois_coinsertion l u) : strict_mono l := λ a b h, gi.dual.strict_mono_u h section lift variables [partial_order α] /-- Lift the infima along a Galois coinsertion -/ def lift_semilattice_inf [semilattice_inf β] (gi : galois_coinsertion l u) : semilattice_inf α := { inf := λa b, u (l a ⊓ l b), inf_le_left := assume a b, le_trans (gi.gc.monotone_u $ inf_le_left) (gi.u_l_le a), inf_le_right := assume a b, le_trans (gi.gc.monotone_u $ inf_le_right) (gi.u_l_le b), le_inf := assume a b c hac hbc, gi.gc.le_u $ le_inf (gi.gc.monotone_l hac) (gi.gc.monotone_l hbc), .. ‹partial_order α› } /-- Lift the suprema along a Galois coinsertion -/ def lift_semilattice_sup [semilattice_sup β] (gi : galois_coinsertion l u) : semilattice_sup α := { sup := λa b, gi.choice (l a ⊔ l b) $ (sup_le (gi.gc.monotone_l $ gi.gc.le_u $ le_sup_left) (gi.gc.monotone_l $ gi.gc.le_u $ le_sup_right)), le_sup_left := by simp only [gi.choice_eq]; exact assume a b, gi.gc.le_u le_sup_left, le_sup_right := by simp only [gi.choice_eq]; exact assume a b, gi.gc.le_u le_sup_right, sup_le := by simp only [gi.choice_eq]; exact assume a b c hac hbc, le_trans (gi.gc.monotone_u $ sup_le (gi.gc.monotone_l hac) (gi.gc.monotone_l hbc)) (gi.u_l_le c), .. ‹partial_order α› } /-- Lift the suprema and infima along a Galois coinsertion -/ def lift_lattice [lattice β] (gi : galois_coinsertion l u) : lattice α := { .. gi.lift_semilattice_sup, .. gi.lift_semilattice_inf } /-- Lift the bot along a Galois coinsertion -/ def lift_order_bot [order_bot β] (gi : galois_coinsertion l u) : order_bot α := { bot := gi.choice ⊥ $ bot_le, bot_le := by simp only [gi.choice_eq]; exact assume b, le_trans (gi.gc.monotone_u bot_le) (gi.u_l_le b), .. ‹partial_order α› } /-- Lift the top, bottom, suprema, and infima along a Galois coinsertion -/ def lift_bounded_lattice [bounded_lattice β] (gi : galois_coinsertion l u) : bounded_lattice α := { .. gi.lift_lattice, .. gi.lift_order_bot, .. gi.gc.lift_order_top } /-- Lift all suprema and infima along a Galois coinsertion -/ def lift_complete_lattice [complete_lattice β] (gi : galois_coinsertion l u) : complete_lattice α := { Inf := λs, u (⨅ a∈s, l a), le_Inf := assume s a hs, gi.gc.le_u $ le_infi $ assume b, le_infi $ assume hb, gi.gc.monotone_l $ hs _ hb, Inf_le := assume s a ha, le_trans (gi.gc.monotone_u $ infi_le_of_le a $ infi_le_of_le ha $ le_refl _) (gi.u_l_le a), Sup := λs, gi.choice (⨆ a∈s, l a) $ supr_le $ assume b, supr_le $ assume hb, gi.gc.monotone_l $ gi.gc.le_u $ le_supr_of_le b $ le_supr_of_le hb $ le_refl _, le_Sup := by simp only [gi.choice_eq]; exact assume s a ha, gi.gc.le_u $ le_supr_of_le a $ le_supr_of_le ha $ le_refl _, Sup_le := by simp only [gi.choice_eq]; exact assume s a hs, le_trans (gi.gc.monotone_u $ supr_le $ assume b, show (⨆ (hb : b ∈ s), l b) ≤ l a, from supr_le $ assume hb, gi.gc.monotone_l $ hs b hb) (gi.u_l_le a), .. gi.lift_bounded_lattice } end lift end galois_coinsertion /-- If `α` is a partial order with bottom element (e.g., `ℕ`, `ℝ≥0`), then `λ o : with_bot α, o.get_or_else ⊥` and coercion form a Galois insertion. -/ def with_bot.gi_get_or_else_bot [order_bot α] : galois_insertion (λ o : with_bot α, o.get_or_else ⊥) coe := { gc := λ a b, with_bot.get_or_else_bot_le_iff, le_l_u := λ a, le_rfl, choice := λ o ho, _, choice_eq := λ _ _, rfl }
6260f9202ed1218acf248fdd7f462cb288e73c98
618003631150032a5676f229d13a079ac875ff77
/src/tactic/subtype_instance.lean
16421e76a6a76b3f11515750c138b779a9d74d38
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
2,454
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon Provides a `subtype_instance` tactic which builds instances for algebraic substructures (sub-groups, sub-rings...). -/ import data.string.basic open tactic expr name list namespace tactic open tactic.interactive (get_current_field refine_struct) open lean lean.parser open interactive /-- makes the substructure axiom name from field name, by postfacing with `_mem`-/ def mk_mem_name (sub : name) : name → name | (mk_string n _) := mk_string (n ++ "_mem") sub | n := n meta def derive_field_subtype : tactic unit := do field ← get_current_field, b ← target >>= is_prop, if b then do `[simp [subtype.ext], dsimp [set.set_coe_eq_subtype]], intros, applyc field; assumption else do s ← find_local ``(set _), `(set %%α) ← infer_type s, e ← mk_const field, expl_arity ← get_expl_arity $ e α, xs ← (iota expl_arity).mmap $ λ _, intro1, args ← xs.mmap $ λ x, mk_app `subtype.val [x], hyps ← xs.mmap $ λ x, mk_app `subtype.property [x], val ← mk_app field args, subname ← local_context >>= list.mfirst (λ h, do (expr.const n _, args) ← get_app_fn_args <$> infer_type h, is_def_eq s args.ilast reducible, return n), mem_field ← resolve_constant $ mk_mem_name subname field, val_mem ← mk_app mem_field hyps, `(coe_sort %%s) <- target >>= instantiate_mvars, tactic.refine ``(@subtype.mk _ %%s %%val %%val_mem) namespace interactive /-- builds instances for algebraic substructures Example: ```lean variables {α : Type*} [monoid α] {s : set α} class is_submonoid (s : set α) : Prop := (one_mem : (1:α) ∈ s) (mul_mem {a b} : a ∈ s → b ∈ s → a * b ∈ s) instance subtype.monoid {s : set α} [is_submonoid s] : monoid s := by subtype_instance ``` -/ meta def subtype_instance := do t ← target, let cl := t.get_app_fn.const_name, src ← find_ancestors cl t.app_arg, let inst := pexpr.mk_structure_instance { struct := cl, field_values := [], field_names := [], sources := src.map to_pexpr }, refine_struct inst ; derive_field_subtype add_tactic_doc { name := "subtype_instance", category := doc_category.tactic, decl_names := [``subtype_instance], tags := ["type class", "structures"] } end interactive end tactic
b6ca82b298e5199d8c56625b36fe0b195f5cc0aa
59b654f4ee2fef898a3487dc03554a569051b63a
/src/utils.lean
2276ad260a060acf2c49252f876b581b83fee37c
[]
no_license
gunpinyo/twisted_cube_formalisation
180c9157478b66ec2b11ca47c8ff998a3e978a88
f78206ac495e84bd43a9b820fa10b6c94722e0ec
refs/heads/master
1,624,501,222,992
1,607,081,624,000
1,607,081,624,000
166,885,106
0
0
null
null
null
null
UTF-8
Lean
false
false
64
lean
import data.sum import data.bool notation α + β := sum α β
a7e81c28a7f5530a95fa721a0a4c7a56848845a1
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebra/group_power/order.lean
38428a814aa03871b861247a47c9d21c878e48fb
[ "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
14,736
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis -/ import algebra.order.ring import algebra.group_power.ring /-! # Lemmas about the interaction of power operations with order Note that some lemmas are in `algebra/group_power/lemmas.lean` as they import files which depend on this file. -/ variables {A G M R : Type*} section preorder variables [monoid M] [preorder M] [covariant_class M M (*) (≤)] @[to_additive nsmul_le_nsmul_of_le_right, mono] lemma pow_le_pow_of_le_left' [covariant_class M M (function.swap (*)) (≤)] {a b : M} (hab : a ≤ b) : ∀ i : ℕ, a ^ i ≤ b ^ i | 0 := by simp | (k+1) := by { rw [pow_succ, pow_succ], exact mul_le_mul' hab (pow_le_pow_of_le_left' k) } attribute [mono] nsmul_le_nsmul_of_le_right @[to_additive nsmul_nonneg] theorem one_le_pow_of_one_le' {a : M} (H : 1 ≤ a) : ∀ n : ℕ, 1 ≤ a ^ n | 0 := by simp | (k + 1) := by { rw pow_succ, exact one_le_mul H (one_le_pow_of_one_le' k) } @[to_additive nsmul_nonpos] lemma pow_le_one' {a : M} (H : a ≤ 1) (n : ℕ) : a ^ n ≤ 1 := @one_le_pow_of_one_le' Mᵒᵈ _ _ _ _ H n @[to_additive nsmul_le_nsmul] theorem pow_le_pow' {a : M} {n m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := let ⟨k, hk⟩ := nat.le.dest h in calc a ^ n ≤ a ^ n * a ^ k : le_mul_of_one_le_right' (one_le_pow_of_one_le' ha _) ... = a ^ m : by rw [← hk, pow_add] @[to_additive nsmul_le_nsmul_of_nonpos] theorem pow_le_pow_of_le_one' {a : M} {n m : ℕ} (ha : a ≤ 1) (h : n ≤ m) : a ^ m ≤ a ^ n := @pow_le_pow' Mᵒᵈ _ _ _ _ _ _ ha h @[to_additive nsmul_pos] theorem one_lt_pow' {a : M} (ha : 1 < a) {k : ℕ} (hk : k ≠ 0) : 1 < a ^ k := begin rcases nat.exists_eq_succ_of_ne_zero hk with ⟨l, rfl⟩, clear hk, induction l with l IH, { simpa using ha }, { rw pow_succ, exact one_lt_mul'' ha IH } end @[to_additive nsmul_neg] lemma pow_lt_one' {a : M} (ha : a < 1) {k : ℕ} (hk : k ≠ 0) : a ^ k < 1 := @one_lt_pow' Mᵒᵈ _ _ _ _ ha k hk @[to_additive nsmul_lt_nsmul] theorem pow_lt_pow' [covariant_class M M (*) (<)] {a : M} {n m : ℕ} (ha : 1 < a) (h : n < m) : a ^ n < a ^ m := begin rcases nat.le.dest h with ⟨k, rfl⟩, clear h, rw [pow_add, pow_succ', mul_assoc, ← pow_succ], exact lt_mul_of_one_lt_right' _ (one_lt_pow' ha k.succ_ne_zero) end @[to_additive nsmul_strict_mono_right] lemma pow_strict_mono_left [covariant_class M M (*) (<)] {a : M} (ha : 1 < a) : strict_mono ((^) a : ℕ → M) := λ m n, pow_lt_pow' ha end preorder section linear_order variables [monoid M] [linear_order M] [covariant_class M M (*) (≤)] @[to_additive nsmul_nonneg_iff] lemma one_le_pow_iff {x : M} {n : ℕ} (hn : n ≠ 0) : 1 ≤ x ^ n ↔ 1 ≤ x := ⟨le_imp_le_of_lt_imp_lt $ λ h, pow_lt_one' h hn, λ h, one_le_pow_of_one_le' h n⟩ @[to_additive] lemma pow_le_one_iff {x : M} {n : ℕ} (hn : n ≠ 0) : x ^ n ≤ 1 ↔ x ≤ 1 := @one_le_pow_iff Mᵒᵈ _ _ _ _ _ hn @[to_additive nsmul_pos_iff] lemma one_lt_pow_iff {x : M} {n : ℕ} (hn : n ≠ 0) : 1 < x ^ n ↔ 1 < x := lt_iff_lt_of_le_iff_le (pow_le_one_iff hn) @[to_additive] lemma pow_lt_one_iff {x : M} {n : ℕ} (hn : n ≠ 0) : x ^ n < 1 ↔ x < 1 := lt_iff_lt_of_le_iff_le (one_le_pow_iff hn) @[to_additive] lemma pow_eq_one_iff {x : M} {n : ℕ} (hn : n ≠ 0) : x ^ n = 1 ↔ x = 1 := by simp only [le_antisymm_iff, pow_le_one_iff hn, one_le_pow_iff hn] variables [covariant_class M M (*) (<)] {a : M} {m n : ℕ} @[to_additive nsmul_le_nsmul_iff] lemma pow_le_pow_iff' (ha : 1 < a) : a ^ m ≤ a ^ n ↔ m ≤ n := (pow_strict_mono_left ha).le_iff_le @[to_additive nsmul_lt_nsmul_iff] lemma pow_lt_pow_iff' (ha : 1 < a) : a ^ m < a ^ n ↔ m < n := (pow_strict_mono_left ha).lt_iff_lt end linear_order section div_inv_monoid variables [div_inv_monoid G] [preorder G] [covariant_class G G (*) (≤)] @[to_additive zsmul_nonneg] theorem one_le_zpow {x : G} (H : 1 ≤ x) {n : ℤ} (hn : 0 ≤ n) : 1 ≤ x ^ n := begin lift n to ℕ using hn, rw zpow_coe_nat, apply one_le_pow_of_one_le' H, end end div_inv_monoid namespace canonically_ordered_comm_semiring variables [canonically_ordered_comm_semiring R] theorem pow_pos {a : R} (H : 0 < a) (n : ℕ) : 0 < a ^ n := pos_iff_ne_zero.2 $ pow_ne_zero _ H.ne' end canonically_ordered_comm_semiring section ordered_semiring variables [ordered_semiring R] {a x y : R} {n m : ℕ} lemma zero_pow_le_one : ∀ n : ℕ, (0 : R) ^ n ≤ 1 | 0 := (pow_zero _).le | (n + 1) := by { rw [zero_pow n.succ_pos], exact zero_le_one } theorem pow_add_pow_le (hx : 0 ≤ x) (hy : 0 ≤ y) (hn : n ≠ 0) : x ^ n + y ^ n ≤ (x + y) ^ n := begin rcases nat.exists_eq_succ_of_ne_zero hn with ⟨k, rfl⟩, induction k with k ih, { simp only [pow_one] }, let n := k.succ, have h1 := add_nonneg (mul_nonneg hx (pow_nonneg hy n)) (mul_nonneg hy (pow_nonneg hx n)), have h2 := add_nonneg hx hy, calc x^n.succ + y^n.succ ≤ x*x^n + y*y^n + (x*y^n + y*x^n) : by { rw [pow_succ _ n, pow_succ _ n], exact le_add_of_nonneg_right h1 } ... = (x+y) * (x^n + y^n) : by rw [add_mul, mul_add, mul_add, add_comm (y*x^n), ← add_assoc, ← add_assoc, add_assoc (x*x^n) (x*y^n), add_comm (x*y^n) (y*y^n), ← add_assoc] ... ≤ (x+y)^n.succ : by { rw [pow_succ _ n], exact mul_le_mul_of_nonneg_left (ih (nat.succ_ne_zero k)) h2 } end theorem pow_lt_pow_of_lt_left (Hxy : x < y) (Hxpos : 0 ≤ x) (Hnpos : 0 < n) : x ^ n < y ^ n := begin cases lt_or_eq_of_le Hxpos, { rw ← tsub_add_cancel_of_le (nat.succ_le_of_lt Hnpos), induction (n - 1), { simpa only [pow_one] }, rw [pow_add, pow_add, nat.succ_eq_add_one, pow_one, pow_one], apply mul_lt_mul ih (le_of_lt Hxy) h (le_of_lt (pow_pos (lt_trans h Hxy) _)) }, { rw [←h, zero_pow Hnpos], apply pow_pos (by rwa ←h at Hxy : 0 < y),} end lemma pow_lt_one (h₀ : 0 ≤ a) (h₁ : a < 1) {n : ℕ} (hn : n ≠ 0) : a ^ n < 1 := (one_pow n).subst (pow_lt_pow_of_lt_left h₁ h₀ (nat.pos_of_ne_zero hn)) theorem strict_mono_on_pow (hn : 0 < n) : strict_mono_on (λ x : R, x ^ n) (set.Ici 0) := λ x hx y hy h, pow_lt_pow_of_lt_left h hx hn theorem one_le_pow_of_one_le (H : 1 ≤ a) : ∀ (n : ℕ), 1 ≤ a ^ n | 0 := by rw [pow_zero] | (n+1) := by { rw pow_succ, simpa only [mul_one] using mul_le_mul H (one_le_pow_of_one_le n) zero_le_one (le_trans zero_le_one H) } lemma pow_mono (h : 1 ≤ a) : monotone (λ n : ℕ, a ^ n) := monotone_nat_of_le_succ $ λ n, by { rw pow_succ, exact le_mul_of_one_le_left (pow_nonneg (zero_le_one.trans h) _) h } theorem pow_le_pow (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := pow_mono ha h theorem le_self_pow (ha : 1 ≤ a) (h : 1 ≤ m) : a ≤ a ^ m := eq.trans_le (pow_one a).symm (pow_le_pow ha h) lemma strict_mono_pow (h : 1 < a) : strict_mono (λ n : ℕ, a ^ n) := have 0 < a := zero_le_one.trans_lt h, strict_mono_nat_of_lt_succ $ λ n, by simpa only [one_mul, pow_succ] using mul_lt_mul h (le_refl (a ^ n)) (pow_pos this _) this.le lemma pow_lt_pow (h : 1 < a) (h2 : n < m) : a ^ n < a ^ m := strict_mono_pow h h2 lemma pow_lt_pow_iff (h : 1 < a) : a ^ n < a ^ m ↔ n < m := (strict_mono_pow h).lt_iff_lt lemma pow_le_pow_iff (h : 1 < a) : a ^ n ≤ a ^ m ↔ n ≤ m := (strict_mono_pow h).le_iff_le lemma strict_anti_pow (h₀ : 0 < a) (h₁ : a < 1) : strict_anti (λ n : ℕ, a ^ n) := strict_anti_nat_of_succ_lt $ λ n, by simpa only [pow_succ, one_mul] using mul_lt_mul h₁ le_rfl (pow_pos h₀ n) zero_le_one lemma pow_lt_pow_iff_of_lt_one (h₀ : 0 < a) (h₁ : a < 1) : a ^ m < a ^ n ↔ n < m := (strict_anti_pow h₀ h₁).lt_iff_lt lemma pow_lt_pow_of_lt_one (h : 0 < a) (ha : a < 1) {i j : ℕ} (hij : i < j) : a ^ j < a ^ i := (pow_lt_pow_iff_of_lt_one h ha).2 hij @[mono] lemma pow_le_pow_of_le_left {a b : R} (ha : 0 ≤ a) (hab : a ≤ b) : ∀ i : ℕ, a^i ≤ b^i | 0 := by simp | (k+1) := by { rw [pow_succ, pow_succ], exact mul_le_mul hab (pow_le_pow_of_le_left _) (pow_nonneg ha _) (le_trans ha hab) } lemma one_lt_pow (ha : 1 < a) {n : ℕ} (hn : n ≠ 0) : 1 < a ^ n := pow_zero a ▸ pow_lt_pow ha (pos_iff_ne_zero.2 hn) lemma pow_le_one : ∀ (n : ℕ) (h₀ : 0 ≤ a) (h₁ : a ≤ 1), a ^ n ≤ 1 | 0 h₀ h₁ := (pow_zero a).le | (n + 1) h₀ h₁ := (pow_succ' a n).le.trans (mul_le_one (pow_le_one n h₀ h₁) h₀ h₁) lemma sq_pos_of_pos (ha : 0 < a) : 0 < a ^ 2 := by { rw sq, exact mul_pos ha ha } end ordered_semiring section ordered_ring variables [ordered_ring R] {a : R} lemma sq_pos_of_neg (ha : a < 0) : 0 < a ^ 2 := by { rw sq, exact mul_pos_of_neg_of_neg ha ha } lemma pow_bit0_pos_of_neg (ha : a < 0) (n : ℕ) : 0 < a ^ bit0 n := begin rw pow_bit0', exact pow_pos (mul_pos_of_neg_of_neg ha ha) _, end lemma pow_bit1_neg (ha : a < 0) (n : ℕ) : a ^ bit1 n < 0 := begin rw [bit1, pow_succ], exact mul_neg_of_neg_of_pos ha (pow_bit0_pos_of_neg ha n), end end ordered_ring section linear_ordered_semiring variables [linear_ordered_semiring R] {a b : R} lemma pow_le_one_iff_of_nonneg {a : R} (ha : 0 ≤ a) {n : ℕ} (hn : n ≠ 0) : a ^ n ≤ 1 ↔ a ≤ 1 := begin refine ⟨_, pow_le_one n ha⟩, rw [←not_lt, ←not_lt], exact mt (λ h, one_lt_pow h hn), end lemma one_le_pow_iff_of_nonneg {a : R} (ha : 0 ≤ a) {n : ℕ} (hn : n ≠ 0) : 1 ≤ a ^ n ↔ 1 ≤ a := begin refine ⟨_, λ h, one_le_pow_of_one_le h n⟩, rw [←not_lt, ←not_lt], exact mt (λ h, pow_lt_one ha h hn), end lemma one_lt_pow_iff_of_nonneg {a : R} (ha : 0 ≤ a) {n : ℕ} (hn : n ≠ 0) : 1 < a ^ n ↔ 1 < a := lt_iff_lt_of_le_iff_le (pow_le_one_iff_of_nonneg ha hn) lemma pow_lt_one_iff_of_nonneg {a : R} (ha : 0 ≤ a) {n : ℕ} (hn : n ≠ 0) : a ^ n < 1 ↔ a < 1 := lt_iff_lt_of_le_iff_le (one_le_pow_iff_of_nonneg ha hn) lemma sq_le_one_iff {a : R} (ha : 0 ≤ a) : a^2 ≤ 1 ↔ a ≤ 1 := pow_le_one_iff_of_nonneg ha (nat.succ_ne_zero _) lemma sq_lt_one_iff {a : R} (ha : 0 ≤ a) : a^2 < 1 ↔ a < 1 := pow_lt_one_iff_of_nonneg ha (nat.succ_ne_zero _) lemma one_le_sq_iff {a : R} (ha : 0 ≤ a) : 1 ≤ a^2 ↔ 1 ≤ a := one_le_pow_iff_of_nonneg ha (nat.succ_ne_zero _) lemma one_lt_sq_iff {a : R} (ha : 0 ≤ a) : 1 < a^2 ↔ 1 < a := one_lt_pow_iff_of_nonneg ha (nat.succ_ne_zero _) @[simp] theorem pow_left_inj {x y : R} {n : ℕ} (Hxpos : 0 ≤ x) (Hypos : 0 ≤ y) (Hnpos : 0 < n) : x ^ n = y ^ n ↔ x = y := (@strict_mono_on_pow R _ _ Hnpos).inj_on.eq_iff Hxpos Hypos lemma lt_of_pow_lt_pow {a b : R} (n : ℕ) (hb : 0 ≤ b) (h : a ^ n < b ^ n) : a < b := lt_of_not_ge $ λ hn, not_lt_of_ge (pow_le_pow_of_le_left hb hn _) h lemma le_of_pow_le_pow {a b : R} (n : ℕ) (hb : 0 ≤ b) (hn : 0 < n) (h : a ^ n ≤ b ^ n) : a ≤ b := le_of_not_lt $ λ h1, not_le_of_lt (pow_lt_pow_of_lt_left h1 hb hn) h @[simp] lemma sq_eq_sq {a b : R} (ha : 0 ≤ a) (hb : 0 ≤ b) : a ^ 2 = b ^ 2 ↔ a = b := pow_left_inj ha hb dec_trivial lemma lt_of_mul_self_lt_mul_self (hb : 0 ≤ b) : a * a < b * b → a < b := by { simp_rw ←sq, exact lt_of_pow_lt_pow _ hb } end linear_ordered_semiring section linear_ordered_ring variable [linear_ordered_ring R] lemma pow_abs (a : R) (n : ℕ) : |a| ^ n = |a ^ n| := ((abs_hom.to_monoid_hom : R →* R).map_pow a n).symm lemma abs_neg_one_pow (n : ℕ) : |(-1 : R) ^ n| = 1 := by rw [←pow_abs, abs_neg, abs_one, one_pow] theorem pow_bit0_nonneg (a : R) (n : ℕ) : 0 ≤ a ^ bit0 n := by { rw pow_bit0, exact mul_self_nonneg _ } theorem sq_nonneg (a : R) : 0 ≤ a ^ 2 := pow_bit0_nonneg a 1 alias sq_nonneg ← pow_two_nonneg theorem pow_bit0_pos {a : R} (h : a ≠ 0) (n : ℕ) : 0 < a ^ bit0 n := (pow_bit0_nonneg a n).lt_of_ne (pow_ne_zero _ h).symm theorem sq_pos_of_ne_zero (a : R) (h : a ≠ 0) : 0 < a ^ 2 := pow_bit0_pos h 1 alias sq_pos_of_ne_zero ← pow_two_pos_of_ne_zero theorem pow_bit0_pos_iff (a : R) {n : ℕ} (hn : n ≠ 0) : 0 < a ^ bit0 n ↔ a ≠ 0 := begin refine ⟨λ h, _, λ h, pow_bit0_pos h n⟩, rintro rfl, rw zero_pow (nat.zero_lt_bit0 hn) at h, exact lt_irrefl _ h, end theorem sq_pos_iff (a : R) : 0 < a ^ 2 ↔ a ≠ 0 := pow_bit0_pos_iff a one_ne_zero variables {x y : R} theorem sq_abs (x : R) : |x| ^ 2 = x ^ 2 := by simpa only [sq] using abs_mul_abs_self x theorem abs_sq (x : R) : |x ^ 2| = x ^ 2 := by simpa only [sq] using abs_mul_self x theorem sq_lt_sq : x ^ 2 < y ^ 2 ↔ |x| < |y| := by simpa only [sq_abs] using (@strict_mono_on_pow R _ _ two_pos).lt_iff_lt (abs_nonneg x) (abs_nonneg y) theorem sq_lt_sq' (h1 : -y < x) (h2 : x < y) : x ^ 2 < y ^ 2 := sq_lt_sq.2 (lt_of_lt_of_le (abs_lt.2 ⟨h1, h2⟩) (le_abs_self _)) theorem sq_le_sq : x ^ 2 ≤ y ^ 2 ↔ |x| ≤ |y| := by simpa only [sq_abs] using (@strict_mono_on_pow R _ _ two_pos).le_iff_le (abs_nonneg x) (abs_nonneg y) theorem sq_le_sq' (h1 : -y ≤ x) (h2 : x ≤ y) : x ^ 2 ≤ y ^ 2 := sq_le_sq.2 (le_trans (abs_le.mpr ⟨h1, h2⟩) (le_abs_self _)) theorem abs_lt_of_sq_lt_sq (h : x^2 < y^2) (hy : 0 ≤ y) : |x| < y := by rwa [← abs_of_nonneg hy, ← sq_lt_sq] theorem abs_lt_of_sq_lt_sq' (h : x^2 < y^2) (hy : 0 ≤ y) : -y < x ∧ x < y := abs_lt.mp $ abs_lt_of_sq_lt_sq h hy theorem abs_le_of_sq_le_sq (h : x^2 ≤ y^2) (hy : 0 ≤ y) : |x| ≤ y := by rwa [← abs_of_nonneg hy, ← sq_le_sq] theorem abs_le_of_sq_le_sq' (h : x^2 ≤ y^2) (hy : 0 ≤ y) : -y ≤ x ∧ x ≤ y := abs_le.mp $ abs_le_of_sq_le_sq h hy lemma sq_eq_sq_iff_abs_eq_abs (x y : R) : x^2 = y^2 ↔ |x| = |y| := by simp only [le_antisymm_iff, sq_le_sq] @[simp] lemma sq_le_one_iff_abs_le_one (x : R) : x^2 ≤ 1 ↔ |x| ≤ 1 := by simpa only [one_pow, abs_one] using @sq_le_sq _ _ x 1 @[simp] lemma sq_lt_one_iff_abs_lt_one (x : R) : x^2 < 1 ↔ |x| < 1 := by simpa only [one_pow, abs_one] using @sq_lt_sq _ _ x 1 @[simp] lemma one_le_sq_iff_one_le_abs (x : R) : 1 ≤ x^2 ↔ 1 ≤ |x| := by simpa only [one_pow, abs_one] using @sq_le_sq _ _ 1 x @[simp] lemma one_lt_sq_iff_one_lt_abs (x : R) : 1 < x^2 ↔ 1 < |x| := by simpa only [one_pow, abs_one] using @sq_lt_sq _ _ 1 x lemma pow_four_le_pow_two_of_pow_two_le {x y : R} (h : x^2 ≤ y) : x^4 ≤ y^2 := (pow_mul x 2 2).symm ▸ pow_le_pow_of_le_left (sq_nonneg x) h 2 end linear_ordered_ring section linear_ordered_comm_ring variables [linear_ordered_comm_ring R] /-- Arithmetic mean-geometric mean (AM-GM) inequality for linearly ordered commutative rings. -/ lemma two_mul_le_add_sq (a b : R) : 2 * a * b ≤ a ^ 2 + b ^ 2 := sub_nonneg.mp ((sub_add_eq_add_sub _ _ _).subst ((sub_sq a b).subst (sq_nonneg _))) alias two_mul_le_add_sq ← two_mul_le_add_pow_two end linear_ordered_comm_ring
de59dff214787116a96dfd69e50fa9d28ec64f4a
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/tactic/elide.lean
0a1cecb9a44b837bf15b489d70ebf0a5734ba402
[ "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
2,174
lean
import logic.basic tactic.core namespace tactic namespace elide meta def replace : ℕ → expr → tactic expr | 0 e := do t ← infer_type e, expr.sort u ← infer_type t, return $ (expr.const ``hidden [u]).app t e | (i+1) (expr.app f x) := do f' ← replace (i+1) f, x' ← replace i x, return (f' x') | (i+1) (expr.lam n b d e) := do d' ← replace i d, var ← mk_local' n b d, e' ← replace i (expr.instantiate_var e var), return (expr.lam n b d' (expr.abstract_local e' var.local_uniq_name)) | (i+1) (expr.pi n b d e) := do d' ← replace i d, var ← mk_local' n b d, e' ← replace i (expr.instantiate_var e var), return (expr.pi n b d' (expr.abstract_local e' var.local_uniq_name)) | (i+1) el@(expr.elet n t d e) := do t' ← replace i t, d' ← replace i d, var ← mk_local_def n d, e' ← replace i (expr.instantiate_var e var) | replace el 0, return (expr.elet n t' d' (expr.abstract_local e' var.local_uniq_name)) | (i+1) e := return e meta def unelide (e : expr) : expr := expr.replace e $ λ e n, match e with | (expr.app (expr.app (expr.const n _) _) e') := if n = ``hidden then some e' else none | (expr.app (expr.lam _ _ _ (expr.var 0)) e') := some e' | _ := none end end elide namespace interactive open interactive.types interactive /-- The `elide n (at ...)` tactic hides all subterms of the target goal or hypotheses beyond depth `n` by replacing them with `hidden`, which is a variant on the identity function. (Tactics should still mostly be able to see through the abbreviation, but if you want to unhide the term you can use `unelide`.) -/ meta def elide (n : ℕ) (loc : parse location) : tactic unit := loc.apply (λ h, infer_type h >>= tactic.elide.replace n >>= tactic.change_core h ∘ some) (target >>= tactic.elide.replace n >>= tactic.change) /-- The `unelide (at ...)` tactic removes all `hidden` subterms in the target types (usually added by `elide`). -/ meta def unelide (loc : parse location) : tactic unit := loc.apply (λ h, infer_type h >>= tactic.change_core h ∘ some ∘ elide.unelide) (target >>= tactic.change ∘ elide.unelide) end interactive end tactic
aed1ff6910f7fb6e11e011f1709f0fb9802e0946
159fed64bfae88f3b6a6166836d6278f953bcbf9
/Structure/Generic/Instances/Basic.lean
cd12bcfcab39162dc0c109d675ff797086dec755
[ "MIT" ]
permissive
SReichelt/lean4-experiments
3e56830c8b2fbe3814eda071c48e3c8810d254a8
ff55357a01a34a91bf670d712637480089085ee4
refs/heads/main
1,683,977,454,907
1,622,991,121,000
1,622,991,121,000
340,765,677
2
0
null
null
null
null
UTF-8
Lean
false
false
17,331
lean
import Structure.Generic.Axioms import mathlib4_experiments.CoreExt import mathlib4_experiments.Data.Equiv.Basic open GeneralizedRelation set_option autoBoundImplicitLocal false --set_option pp.universes true universes u v w instance unitHasInstances : HasInstances Unit := ⟨λ _ => True⟩ def unit : Universe.{0} := ⟨Unit⟩ namespace unit instance hasExternalFunctors (U : Universe.{u}) : HasExternalFunctors U unit := ⟨λ _ => PUnit.{u}⟩ @[reducible] def unitFunctor {U : Universe.{u}} (α : U) (β : unit) : α ⟶' β := ⟨Function.const ⌈α⌉ trivial, ⟨⟩⟩ @[simp] theorem unitFunctorIsUnique {U : Universe.{u}} {α : U} {β : unit} (F : α ⟶' β) : F = unitFunctor α β := match F with | ⟨_, _⟩ => by simp def funEquiv (α β : unit) : True ≃ (α ⟶' β) := { toFun := λ _ => unitFunctor α β, invFun := λ _ => trivial, leftInv := λ _ => by simp, rightInv := λ _ => by simp } instance hasInternalFunctors : HasInternalFunctors unit := { Fun := λ _ _ => ⟨⟩, funEquiv := funEquiv } instance hasIdFun : HasIdFun unit := ⟨λ _ => ⟨⟩⟩ instance hasConstFun (U : Universe.{u}) : HasConstFun U unit := ⟨λ _ _ _ => ⟨⟩⟩ instance hasCompFun (U : Universe.{u}) (V : Universe.{v}) [HasExternalFunctors U V] : HasCompFun U V unit := ⟨λ _ _ => ⟨⟩⟩ instance hasLinearFunOp : HasLinearFunOp unit := { appIsFun := λ _ _ => ⟨⟩, appFunIsFun := λ _ _ => ⟨⟩, compFunIsFun := λ _ _ => ⟨⟩, compFunFunIsFun := λ _ _ _ => ⟨⟩ } instance hasAffineFunOp : HasAffineFunOp unit := { constFunIsFun := λ _ _ => ⟨⟩ } instance hasFullFunOp : HasFullFunOp unit := { dupIsFun := λ _ => ⟨⟩, dupFunIsFun := λ _ _ => ⟨⟩ } instance hasFunOp : HasFunOp unit := ⟨⟩ instance hasExternalEquivalences : HasExternalEquivalences unit unit := ⟨λ _ _ => True⟩ @[reducible] def unitEquivalence (α : unit) (β : unit) : α ⟷' β := ⟨unitFunctor α β, unitFunctor β α, trivial⟩ @[simp] theorem unitEquivalenceIsUnique {α : unit} {β : unit} (E : α ⟷' β) : E = unitEquivalence α β := match E with | ⟨_, _, _⟩ => by simp; exact HEq.rfl def equivEquiv (α β : unit) : True ≃ (α ⟷' β) := { toFun := λ _ => unitEquivalence α β, invFun := λ _ => trivial, leftInv := λ _ => by simp, rightInv := λ _ => by simp } instance hasInternalEquivalences : HasInternalEquivalences unit := { Equiv := λ _ _ => ⟨⟩, equivEquiv := equivEquiv, equivElimToFunIsFun := λ _ _ => ⟨⟩, equivElimInvFunIsFun := λ _ _ => ⟨⟩ } instance hasIdEquiv : HasIdEquiv unit := ⟨λ _ => trivial⟩ instance hasCompEquiv : HasCompEquiv unit unit unit := ⟨λ _ _ => trivial⟩ instance hasInvEquiv : HasInvEquiv unit unit := ⟨λ _ => trivial⟩ instance hasEquivOp : HasEquivOp unit := { compEquivIsFun := λ _ _ => ⟨⟩, compEquivFunIsFun := λ _ _ _ => ⟨⟩, invEquivIsFun := λ _ _ => ⟨⟩, invEquivIsEquiv := λ _ _ => trivial } @[reducible] def unitProduct (α : unit) (β : unit) : α ⊓' β := ⟨⟨⟩, ⟨⟩⟩ @[simp] theorem unitProductIsUnique {α : unit} {β : unit} (P : α ⊓' β) : P = unitProduct α β := match P with | ⟨_, _⟩ => rfl def prodEquiv (α β : unit) : True ≃ (α ⊓' β) := { toFun := λ _ => unitProduct α β, invFun := λ _ => ⟨⟩, leftInv := λ _ => by simp, rightInv := λ _ => by simp } instance hasInternalProducts : HasInternalProducts unit := { Prod := λ _ _ => ⟨⟩, prodEquiv := prodEquiv, prodIntroIsFun := λ _ _ => ⟨⟩, prodElimFstIsFun := λ _ _ => ⟨⟩, prodElimSndIsFun := λ _ _ => ⟨⟩ } instance hasUnitType : HasUnitType unit := { Unit := ⟨⟩, unit := trivial, unitIntroIsFun := λ _ => ⟨⟩ } def Rel (α : Sort u) : GeneralizedRelation α unit := λ _ _ => ⟨⟩ instance Rel.isEquivalence (α : Sort u) : IsEquivalence (Rel α) := { refl := λ _ => trivial, trans := trivial, symm := trivial } class HasUnitEquivalences (U : Universe.{u}) where (Equiv (α : U) : GeneralizedRelation ⌈α⌉ unit) [equivIsEquivalence (α : U) : IsEquivalence (Equiv α)] instance hasUnitInstanceEquivalences (U : Universe.{u}) [HasUnitEquivalences U] : HasInstanceEquivalences U := ⟨unit, λ α => unit.Rel ⌈α⌉⟩ instance hasUnitEquivalence : HasUnitEquivalences unit := ⟨λ _ => unit.Rel True⟩ instance hasEquivCongr : HasEquivCongr unit := { equivCongrArg := λ _ => unitFunctor (U := unit) ⟨⟩ ⟨⟩, equivCongrFun := λ _ => unitFunctor (U := unit) ⟨⟩ ⟨⟩ } instance hasNaturalEquivalences : HasNaturalEquivalences unit := { equivHasInstEquivs := hasUnitInstanceEquivalences unit, isNat := λ _ _ _ _ => trivial } section Morphisms variable {α : Sort u} {V : Universe.{v}} [HasInternalFunctors V] [HasUnitEquivalences V] (R : GeneralizedRelation α V) variable [HasLinearFunOp V] [HasTrans R] instance isCompositionRelation : IsCompositionRelation R := { assoc := trivial } variable [HasRefl R] instance isMorphismRelation [IsPreorder R] : IsMorphismRelation R := { leftId := trivial, rightId := trivial } variable [HasSubLinearFunOp V] [HasNonLinearFunOp V] [HasInternalEquivalences V] [HasSymm R] instance isIsomorphismRelation [IsEquivalence R] : IsIsomorphismRelation R := { leftInv := trivial, rightInv := trivial } end Morphisms section Functors variable {α : Sort u} {V : Universe.{v}} {W : Universe.{w}} [HasInternalFunctors V] [HasInternalEquivalences V] [HasInternalFunctors W] [HasInternalEquivalences W] [HasUnitEquivalences W] [HasExternalFunctors V W] (R : GeneralizedRelation α V) (S : GeneralizedRelation α W) [IsEquivalence R] [IsEquivalence S] (F : BaseFunctor R S) instance isReflFunctor : IsReflFunctor R S F := ⟨λ _ => trivial⟩ instance isSymmFunctor : IsSymmFunctor R S F := ⟨λ _ => trivial⟩ instance isTransFunctor : IsTransFunctor R S F := ⟨λ _ _ => trivial⟩ instance isPreorderFunctor : IsPreorderFunctor R S F := ⟨⟩ instance isEquivalenceFunctor : IsEquivalenceFunctor R S F := ⟨⟩ end Functors end unit def sort : Universe.{u} := ⟨Sort u⟩ @[reducible] def prop := sort.{0} @[reducible] def type := sort.{1} namespace sort instance hasExternalFunctors (U : Universe.{u}) : HasExternalFunctors U sort.{v} := ⟨λ _ => PUnit.{max u v}⟩ @[reducible] def toBundledFunctor {U : Universe.{u}} {α : U} {β : sort.{v}} (f : α → β) : α ⟶' β := ⟨f, ⟨⟩⟩ theorem toFromBundledFunctor {U : Universe.{u}} {α : U} {β : sort.{v}} (F : α ⟶' β) : toBundledFunctor F.f = F := match F with | ⟨_, _⟩ => by simp def funEquiv (α β : sort.{u}) : (α → β) ≃ (α ⟶' β) := { toFun := λ f => toBundledFunctor f, invFun := λ F => F.f, leftInv := λ f => rfl, rightInv := λ F => toFromBundledFunctor F } instance hasInternalFunctors : HasInternalFunctors sort.{u} := { Fun := λ α β => α → β, funEquiv := funEquiv } instance hasIdFun : HasIdFun sort.{u} := ⟨λ _ => ⟨⟩⟩ instance hasConstFun (U : Universe.{u}) : HasConstFun U sort.{v} := ⟨λ _ _ _ => ⟨⟩⟩ instance hasCompFun (U : Universe.{u}) (V : Universe.{v}) [HasExternalFunctors U V] : HasCompFun U V sort.{w} := ⟨λ _ _ => ⟨⟩⟩ instance hasLinearFunOp : HasLinearFunOp sort.{u} := { appIsFun := λ _ _ => ⟨⟩, appFunIsFun := λ _ _ => ⟨⟩, compFunIsFun := λ _ _ => ⟨⟩, compFunFunIsFun := λ _ _ _ => ⟨⟩ } instance hasAffineFunOp : HasAffineFunOp sort.{u} := { constFunIsFun := λ _ _ => ⟨⟩ } instance hasFullFunOp : HasFullFunOp sort.{u} := { dupIsFun := λ _ => ⟨⟩, dupFunIsFun := λ _ _ => ⟨⟩ } instance hasFunOp : HasFunOp sort.{u} := ⟨⟩ end sort namespace prop instance hasExternalEquivalences : HasExternalEquivalences prop prop := ⟨λ _ _ => PUnit.{0}⟩ @[reducible] def toBundledEquivalence {p q : prop} (h : p ↔ q) : p ⟷' q := ⟨sort.toBundledFunctor h.mp, sort.toBundledFunctor h.mpr, ⟨⟩⟩ @[reducible] def fromBundledEquivalence {p q : prop} (E : p ⟷' q) : p ↔ q := ⟨E.toFun.f, E.invFun.f⟩ theorem fromToBundledEquivalence {p q : prop} (h : p ↔ q) : fromBundledEquivalence (toBundledEquivalence h) = h := rfl theorem toFromBundledEquivalence {p q : prop} (E : p ⟷' q) : toBundledEquivalence (fromBundledEquivalence E) = E := match E with | ⟨toFun, invFun, _⟩ => by simp; exact ⟨sort.toFromBundledFunctor toFun, sort.toFromBundledFunctor invFun, HEq.rfl⟩ def equivEquiv (p q : prop) : (p ↔ q) ≃ (p ⟷' q) := { toFun := toBundledEquivalence, invFun := fromBundledEquivalence, leftInv := fromToBundledEquivalence, rightInv := toFromBundledEquivalence } instance hasInternalEquivalences : HasInternalEquivalences prop := { Equiv := Iff, equivEquiv := equivEquiv, equivElimToFunIsFun := λ _ _ => ⟨⟩, equivElimInvFunIsFun := λ _ _ => ⟨⟩ } instance hasIdEquiv : HasIdEquiv prop := ⟨λ _ => ⟨⟩⟩ instance hasCompEquiv : HasCompEquiv prop prop prop := ⟨λ _ _ => ⟨⟩⟩ instance hasInvEquiv : HasInvEquiv prop prop := ⟨λ _ => ⟨⟩⟩ instance hasEquivOp : HasEquivOp prop := { compEquivIsFun := λ _ _ => ⟨⟩, compEquivFunIsFun := λ _ _ _ => ⟨⟩, invEquivIsFun := λ _ _ => ⟨⟩, invEquivIsEquiv := λ _ _ => ⟨⟩ } def prodEquiv (p q : prop) : (p ∧ q) ≃ (p ⊓' q) := { toFun := λ h => ⟨h.left, h.right⟩, invFun := λ P => ⟨P.fst, P.snd⟩, leftInv := λ _ => rfl, rightInv := λ ⟨_, _⟩ => rfl } instance hasInternalProducts : HasInternalProducts prop := { Prod := And, prodEquiv := prodEquiv, prodIntroIsFun := λ _ _ => ⟨⟩, prodElimFstIsFun := λ _ _ => ⟨⟩, prodElimSndIsFun := λ _ _ => ⟨⟩ } instance hasEmptyType : HasEmptyType prop := { Empty := False, emptyIsEmpty := id, emptyElimIsFun := λ _ => ⟨⟩ } instance hasClassicalLogic : HasClassicalLogic prop := { byContradiction := @Classical.byContradiction } instance hasUnitType : HasUnitType prop := { Unit := True, unit := trivial, unitIntroIsFun := λ _ => ⟨⟩ } -- Every equivalence relation can trivially be converted to an instance of `IsEquivalence`. instance relEquiv {α : Sort u} {R : GeneralizedRelation α prop} (e : Equivalence R) : IsEquivalence R := { refl := e.refl, trans := e.trans, symm := ⟨e.symm, e.symm⟩ } namespace relEquiv instance eq (α : Sort u) : IsEquivalence (V := prop) (@Eq α) := relEquiv Eq.isEquivalence instance setoid (α : Sort u) [s : Setoid α] : IsEquivalence (V := prop) s.r := relEquiv s.iseqv end relEquiv instance hasUnitEquivalences : unit.HasUnitEquivalences prop := ⟨unit.Rel⟩ instance hasEquivCongr : HasEquivCongr prop := { equivCongrArg := λ _ => unit.unitFunctor (U := unit) ⟨⟩ ⟨⟩, equivCongrFun := λ _ => unit.unitFunctor (U := unit) ⟨⟩ ⟨⟩ } instance hasNaturalEquivalences : HasNaturalEquivalences prop := { equivHasInstEquivs := unit.hasUnitInstanceEquivalences unit, isNat := λ _ _ _ _ => trivial } section NaturalTransformations variable {α : Sort u} {β : Sort v} {V : Universe.{v}} [HasInternalFunctors V] [HasExternalFunctors V prop] (R : GeneralizedRelation α V) (S : GeneralizedRelation β prop) [HasTrans S] {mF mG : α → β} (F : MappedBaseFunctor R S mF) (G : MappedBaseFunctor R S mG) instance isNatural (n : ∀ a, S (mF a) (mG a)) : IsNatural R S F G n := ⟨λ _ => trivial⟩ def natEquiv : (∀ a, S (mF a) (mG a)) ≃ NaturalQuantification R S F G := { toFun := λ n => ⟨n⟩, invFun := λ N => N.n, leftInv := λ _ => rfl, rightInv := λ { n := _, isNatural := ⟨_⟩ } => rfl } instance hasIntNat : HasInternalNaturalQuantification R S F G := { Nat := ∀ a, S (mF a) (mG a), natEquiv := natEquiv R S F G } end NaturalTransformations instance hasNat {U₁ U₂ V : Universe} [HasExternalFunctors U₁ U₂] [HasExternalFunctors V prop] : HasNaturalQuantification U₁ U₂ V prop := { hasNat := λ {α β} R S {h mF mG} F G => hasIntNat R S F G } instance hasInstanceIsomorphisms : HasInstanceIsomorphisms prop := { equivIsIso := λ p => unit.isIsomorphismRelation (unit.Rel p) } end prop namespace type class IsEquiv {α β : type} (toFun : α ⟶' β) (invFun : β ⟶' α) where (leftInv : ∀ a, invFun (toFun a) = a) (rightInv : ∀ b, toFun (invFun b) = b) instance hasExternalEquivalences : HasExternalEquivalences type type := ⟨IsEquiv⟩ @[reducible] def isEquivalence {α β : type} (e : Equiv α β) : IsEquiv (sort.toBundledFunctor e.toFun) (sort.toBundledFunctor e.invFun) := ⟨e.leftInv, e.rightInv⟩ theorem isEquivalenceIsUnique {α β : type} {e : Equiv α β} (h : IsEquiv (sort.toBundledFunctor e.toFun) (sort.toBundledFunctor e.invFun)) : h = isEquivalence e := match h with | ⟨_, _⟩ => sorry -- by proof irrelevance @[reducible] def invIsEquivalence {α β : type} {toFun : α ⟶' β} {invFun : β ⟶' α} (h : IsEquiv toFun invFun) : IsEquiv invFun toFun := ⟨h.rightInv, h.leftInv⟩ @[reducible] def toBundledEquivalence {α β : type} (e : Equiv α β) : α ⟷' β := ⟨sort.toBundledFunctor e.toFun, sort.toBundledFunctor e.invFun, isEquivalence e⟩ @[reducible] def fromBundledEquivalence {α β : type} (E : α ⟷' β) : Equiv α β := ⟨E.toFun.f, E.invFun.f, E.isEquiv.leftInv, E.isEquiv.rightInv⟩ theorem fromToBundledEquivalence {α β : type} (e : Equiv α β) : fromBundledEquivalence (toBundledEquivalence e) = e := match e with | ⟨_, _, _, _⟩ => rfl theorem toFromBundledEquivalence {α β : type} (E : α ⟷' β) : toBundledEquivalence (fromBundledEquivalence E) = E := match E with | ⟨toFun, invFun, _⟩ => by simp; exact ⟨sort.toFromBundledFunctor toFun, sort.toFromBundledFunctor invFun, sorry⟩ -- by `isEquivalenceIsUnique` def equivEquiv (α β : type) : Equiv α β ≃ (α ⟷' β) := { toFun := toBundledEquivalence, invFun := fromBundledEquivalence, leftInv := fromToBundledEquivalence, rightInv := toFromBundledEquivalence } instance hasInternalEquivalences : HasInternalEquivalences type := { Equiv := Equiv, equivEquiv := equivEquiv, equivElimToFunIsFun := λ _ _ => ⟨⟩, equivElimInvFunIsFun := λ _ _ => ⟨⟩ } instance hasIdEquiv : HasIdEquiv type := ⟨λ α => isEquivalence (Equiv.refl α)⟩ instance hasCompEquiv : HasCompEquiv type type type := ⟨λ E F => isEquivalence (Equiv.trans (fromBundledEquivalence E) (fromBundledEquivalence F))⟩ instance hasInvEquiv : HasInvEquiv type type := ⟨λ E => invIsEquivalence E.isEquiv⟩ instance hasEquivOp : HasEquivOp type := { compEquivIsFun := λ _ _ => ⟨⟩, compEquivFunIsFun := λ _ _ _ => ⟨⟩, invEquivIsFun := λ _ _ => ⟨⟩, invEquivIsEquiv := λ α β => ⟨@Equiv.symm_symm α β, @Equiv.symm_symm β α⟩ } def prodEquiv (α β : type) : Prod α β ≃ (α ⊓' β) := { toFun := λ p => ⟨p.fst, p.snd⟩, invFun := λ P => ⟨P.fst, P.snd⟩, leftInv := λ ⟨_, _⟩ => rfl, rightInv := λ ⟨_, _⟩ => rfl } instance hasInternalProducts : HasInternalProducts type := { Prod := Prod, prodEquiv := prodEquiv, prodIntroIsFun := λ _ _ => ⟨⟩, prodElimFstIsFun := λ _ _ => ⟨⟩, prodElimSndIsFun := λ _ _ => ⟨⟩ } instance hasEmptyType : HasEmptyType type := { Empty := Empty, emptyIsEmpty := λ a => (by induction a), emptyElimIsFun := λ _ => ⟨⟩ } instance hasUnitType : HasUnitType type := { Unit := Unit, unit := ⟨⟩, unitIntroIsFun := λ _ => ⟨⟩ } instance hasInstanceEquivalences : HasInstanceEquivalences type := ⟨prop, @Eq⟩ instance hasEquivCongr : HasEquivCongr type := { equivCongrArg := λ F => sort.toBundledFunctor (congrArg F.f), equivCongrFun := λ a => sort.toBundledFunctor (λ h => congrFun h a) } instance hasNaturalEquivalences : HasNaturalEquivalences type := { equivHasInstEquivs := unit.hasUnitInstanceEquivalences prop, isNat := λ _ _ _ _ => trivial } instance hasInstanceIsomorphisms : HasInstanceIsomorphisms type := { equivIsIso := λ α => unit.isIsomorphismRelation (V := prop) (@Eq α) } end type
9c376a830987f712a05dc1240179081b936367db
efa51dd2edbbbbd6c34bd0ce436415eb405832e7
/20161026_ICTAC_Tutorial/ex5.lean
c580ff02581b8c0ac679c9239a46159c9756f979
[ "Apache-2.0" ]
permissive
leanprover/presentations
dd031a05bcb12c8855676c77e52ed84246bd889a
3ce2d132d299409f1de269fa8e95afa1333d644e
refs/heads/master
1,688,703,388,796
1,686,838,383,000
1,687,465,742,000
29,750,158
12
9
Apache-2.0
1,540,211,670,000
1,422,042,683,000
Lean
UTF-8
Lean
false
false
129
lean
constants A B : Type check prod A B -- Type check prod nat nat -- Type check list A -- Type check list nat -- Type
b8a087dcc9b7a8343fa177fffe10dd49ce51c106
4fa118f6209450d4e8d058790e2967337811b2b5
/src/Huber_ring/localization.lean
dceb88171d8b56bf3b5e437d69b6ac7cf2666016
[ "Apache-2.0" ]
permissive
leanprover-community/lean-perfectoid-spaces
16ab697a220ed3669bf76311daa8c466382207f7
95a6520ce578b30a80b4c36e36ab2d559a842690
refs/heads/master
1,639,557,829,139
1,638,797,866,000
1,638,797,866,000
135,769,296
96
10
Apache-2.0
1,638,797,866,000
1,527,892,754,000
Lean
UTF-8
Lean
false
false
20,667
lean
import ring_theory.localization import tactic.tidy import tactic.ring import Huber_ring.basic import for_mathlib.topological_rings import for_mathlib.algebra import for_mathlib.submodule import for_mathlib.nonarchimedean.basic /-! # Localization of Huber rings This file contains technical machinery that is needed for the definition of the structure presheaf on Spa (the adic spectrum). We start with a Huber ring A, a subset T ⊆ A, and an element s of A. Our goal is to define a topology on (away s), which is the localization of A at s. This topology will depend on T, and should not depend on the ring of definition. In the literature, this ring is commonly denoted with A⟮T/s⟯ to indicate the dependence on T. For the same reason, we start by defining a wrapper type that includes T in its assumptions. To realize this goal, we need to use several technical lemmas from the theory of topological rings. This file ends with the universal property of A⟮T/s⟯. We point out that this universal property is recorded in [Wedhorn, Prop & Def 5.51]. However, the running assumption of section 5.6 of [Wedhorn] is the conclusion of [Wedhorn, Lem 6.20], which explains our “detour” through section 6 of [Wedhorn]. (We only need the case n=1 of [Wedhorn, Lem 6.20].) # Notation We make heavy use of the following notation (also used in [Wedhorn]): - if S and T are two subset of a monoid A, then S * T denotes the set {s*t | s ∈ S, t ∈ T}. - if x is an element of A, then x * T = {x*t | t ∈ T} - if A is an A₀-algebra, x is an element of A, and M ⊆ A an A₀-submodule, then x • M is the submodule {x • m | m ∈ M}. - this generalizes to sets, for S ⊆ A, the notation S • M means the submodule generated by S * M. - in particular, if N is another such submodule, then the submodule M * N is the submodule generated by the product of sets M * N. -/ universes u v local attribute [instance, priority 0] classical.prop_decidable local attribute [instance] set.pointwise_mul_comm_semiring local attribute [instance] set.smul_set_action local attribute [instance] set.pointwise_mul_image_is_semiring_hom namespace Huber_ring open localization algebra topological_ring submodule set topological_add_group variables {A : Type u} [comm_ring A] [topological_space A] [topological_ring A] variables (T : set A) (s : A) /--The localization of a topological ring at an element `s`, endowed with a topology that depends on a set `T`-/ @[nolint] def away (T : set A) (s : A) := away s local notation `A⟮T/s⟯` := away T s namespace away /-- The ring structure on A⟮T/s⟯. -/ instance : comm_ring A⟮T/s⟯ := by delta away; apply_instance /-- The module structure on A⟮T/s⟯. -/ instance : module A A⟮T/s⟯ := by delta away; apply_instance /-- The algebra structure on A⟮T/s⟯. -/ instance : algebra A A⟮T/s⟯ := by delta away; apply_instance /-- The coercion from A to A⟮T/s⟯. -/ instance : has_coe A A⟮T/s⟯ := ⟨λ a, (of_id A A⟮T/s⟯ : A → A⟮T/s⟯) a⟩ set_option class.instance_max_depth 50 /--An auxiliary subring, used to define the topology on `away T s`-/ def D.aux : set A⟮T/s⟯ := let s_inv : A⟮T/s⟯ := ((to_units ⟨s, ⟨1, by simp⟩⟩)⁻¹ : units A⟮T/s⟯) in ring.closure (s_inv • of_id A A⟮T/s⟯ '' T) local notation `D` := D.aux T s /-- The set D is a subring. -/ instance : is_subring D := by delta D.aux; apply_instance local notation `Dspan` U := span D (of_id A A⟮T/s⟯ '' (U : set A)) /- To put a topology on `away T s` we want to use the construction `topology_of_submodules_comm` which needs a directed family of submodules of `A⟮T/s⟯ = away T s` viewed as `D`-algebra. This directed family has two satisfy two extra conditions. Proving these two conditions takes up the beef of this file. Initially we only assume that `A` is a nonarchimedean ring, but towards the end we need to strengthen this assumption to Huber ring. -/ set_option class.instance_max_depth 50 /--The submodules spanned by the open subgroups of `A` form a directed family-/ lemma directed (U₁ U₂ : open_add_subgroup A) : ∃ (U : open_add_subgroup A), (Dspan U) ≤ (Dspan U₁) ⊓ (Dspan U₂) := begin use U₁ ⊓ U₂, apply lattice.le_inf _ _; rw span_le; refine subset.trans (image_subset _ _) subset_span, { apply inter_subset_left }, { apply inter_subset_right }, end /--For every open subgroup `U` of `A` and every `a : A`, there exists an open subgroup `V` of `A`, such that `a • (span D V)` is contained in the `D`-span of `U`.-/ lemma left_mul_subset (h : nonarchimedean A) (U : open_add_subgroup A) (a : A) : ∃ V : open_add_subgroup A, (a : A⟮T/s⟯) • (Dspan V) ≤ (Dspan U) := begin cases h _ _ with V hV, use V, work_on_goal 0 { erw [smul_singleton, ← span_image, span_le, ← image_comp, ← algebra.map_lmul_left, image_comp], refine subset.trans (image_subset (of_id A A⟮T/s⟯ : A → A⟮T/s⟯) _) subset_span, rw image_subset_iff, exact hV }, apply mem_nhds_sets (continuous_mul_left _ _ U.is_open), rw [mem_preimage, mul_zero], exact U.zero_mem end /--For every open subgroup `U` of `A`, there exists an open subgroup `V` of `A`, such that the multiplication map sends the `D`-span of `V` into the `D`-span of `U`.-/ lemma mul_le (h : nonarchimedean A) (U : open_add_subgroup A) : ∃ (V : open_add_subgroup A), (Dspan V) * (Dspan V) ≤ (Dspan U) := begin rcases nonarchimedean.mul_subset h U with ⟨V, hV⟩, use V, rw span_mul_span, apply span_mono, rw ← is_semiring_hom.map_mul (image (of_id A A⟮T/s⟯ : A → A⟮T/s⟯)), exact image_subset _ hV, end /--A technical auxiliary lemma: for every finite set L contained in the ideal generated by T, there exists a finite set K such that L is contained in the subgroup generated by the set T * K. (Recall that T * K is the set of products of elements in T and in K.)-/ @[nolint] lemma K.aux (L : finset A) (h : (↑L : set A) ⊆ ideal.span T) : ∃ (K : finset A), (↑L : set A) ⊆ (↑(span ℤ (T * ↑K)) : set A) := begin delta ideal.span at h, rw [← set.image_id T] at h, erw finsupp.span_eq_map_total at h, choose s hs using finset.subset_image_iff.mp h, use s.bind (λ f, f.frange), rcases hs with ⟨hs, rfl⟩, intros l hl, rcases finset.mem_image.mp hl with ⟨f, hf, rfl⟩, refine is_add_submonoid.finset_sum_mem ↑(span _ _) _ _ _, intros t ht, refine subset_span ⟨t, _, _, _, mul_comm _ _⟩, { replace hf := hs hf, erw finsupp.mem_supported A f at hf, exact hf ht }, { erw [linear_map.id_apply, finset.mem_bind], use [f, hf], erw finsupp.mem_support_iff at ht, erw finsupp.mem_frange, exact ⟨ht, ⟨t, rfl⟩⟩ } end end away end Huber_ring namespace Huber_ring open localization algebra topological_ring submodule set topological_add_group variables {A : Type u} [Huber_ring A] variables (T : set A) (s : A) namespace away local notation `A⟮T/s⟯` := away T s local notation `D` := D.aux T s local notation `Dspan` U := span D (of_id A A⟮T/s⟯ '' (U : set A)) set_option class.instance_max_depth 80 /-- If T ⊆ A generates an open ideal, and U is an open subgroup of A, then T • U generates an open subgroup. (This lemma is the main part of case n = 1 of [Wedhorn, Lem 6.20].)-/ lemma mul_T_open (hT : is_open ((ideal.span T) : set A)) (U : open_add_subgroup A) : is_open (↑(T • span ℤ (U : set A)) : set A) := begin -- Choose an ideal of definition I ⊆ span T rcases exists_pod_subset _ (mem_nhds_sets hT $ ideal.zero_mem $ ideal.span T) with ⟨A₀, _, _, _, ⟨_, emb, I, fg, top⟩, hI⟩, resetI, dsimp only at hI, -- Choose a generating set L ⊆ I cases fg with L hL, rw ← hL at hI, -- Observe L ⊆ span T have Lsub : (↑(L.image (to_fun A)) : set A) ⊆ ↑(ideal.span T) := by { rw finset.coe_image, exact set.subset.trans (image_subset _ subset_span) hI }, -- Choose a finite set K such that L ⊆ span (T * K) cases K.aux _ _ Lsub with K hK, -- Choose V such that K * V ⊆ U let nonarch := Huber_ring.nonarchimedean, let V := K.inf (λ k : A, classical.some (nonarch.left_mul_subset U k)), cases is_ideal_adic_iff.mp top with H₁ H₂, have hV : ↑K * (V : set A) ⊆ U, { rintros _ ⟨k, hk, v, hv, rfl⟩, apply classical.some_spec (nonarch.left_mul_subset U k), refine ⟨v, _, rfl⟩, apply (finset.inf_le hk : V ≤ _), exact hv }, replace hV : span ℤ _ ≤ span ℤ _ := span_mono hV, erw [← span_mul_span, ← submodule.smul_def] at hV, haveI : is_ring_hom (to_fun A : A₀ → A) := algebra.is_ring_hom, -- Choose m such that I^m ⊆ V cases H₂ _ (mem_nhds_sets (emb.continuous _ V.is_open) _) with m hm, work_on_goal 1 { show to_fun A (0 : A₀) ∈ V, convert V.zero_mem, exact is_ring_hom.map_zero _ }, rw ← image_subset_iff at hm, change to_fun A '' ↑(I ^ m) ⊆ ↑V at hm, erw [← span_int_eq (V : set A), ← span_int_eq (↑(I^m) : set A₀)] at hm, change (submodule.map (alg_hom_int $ to_fun A).to_linear_map _) ≤ _ at hm, work_on_goal 1 {apply_instance}, -- It suffices to provide an open subgroup apply @open_add_subgroup.is_open_of_open_add_subgroup A _ _ _ _ (submodule.submodule_is_add_subgroup _), refine ⟨⟨to_fun A '' ↑(I^(m+1)), _, _⟩, _⟩, work_on_goal 2 {assumption}, all_goals { try {apply_instance} }, { exact emb.is_open_map _ (H₁ _) }, -- What remains is the following calculation: I^(m+1) ⊆ T • span U. -- Unfortunately it seems to be hard to express in calc mode -- First observe: I^(m+1) = L • I^m as A₀-ideal, but also as ℤ-submodule erw [subtype.coe_mk, pow_succ, ← hL, ← submodule.smul_def, hL, smul_eq_smul_span_int], change (submodule.map (alg_hom_int $ to_fun A).to_linear_map _) ≤ _, work_on_goal 1 {apply_instance}, -- Now we map the above equality through the canonical map A₀ → A erw [submodule.map_mul, ← span_image, ← submodule.smul_def], erw [finset.coe_image] at hK, -- Next observe: L • I^m ≤ (T * K) • V refine le_trans (smul_le_smul hK hm) _, -- Also observe: T • (K • V) ≤ T • U refine (le_trans (le_of_eq _) (smul_le_smul (le_refl T) hV)), change span _ _ * _ = _, erw [span_span, ← mul_smul], refl end -- The above lemma is what we really need, but the version below is here for comparison with -- Wedhorn. /-- If T ⊆ A generates an open ideal, and U is an open subgroup of A, then T • U is a neighborhood of zero. (This lemma is case n = 1 of [Wedhorn, Lem 6.20].)-/ lemma mul_T_nhds (hT : is_open ((ideal.span T) : set A)) (U : open_add_subgroup A) : ↑(T • span ℤ (U : set A)) ∈ nhds (0 : A) := mem_nhds_sets (mul_T_open _ hT _) (submodule.zero_mem (T • span ℤ (U : set A))) set_option class.instance_max_depth 80 /- Our next goal is the lemma mul_left, which says that for every element a of A⟮T/s⟯ and every open subgroup U of A, there exists an open subgroup V of A, such that a • Dspan V ≤ Dspan U. We prove this statement using two helper lemmas. The first proves the case where a = s⁻¹. The second considers arbitrary powers of s⁻¹. -/ /--Helper lemma. A special case of mul_left, where the element a is s⁻¹.-/ lemma mul_left.aux₁ (hT : is_open (↑(ideal.span T) : set A)) (U : open_add_subgroup A) : ∃ (V : open_add_subgroup A), (↑((to_units ⟨s, ⟨1, pow_one s⟩⟩)⁻¹ : units A⟮T/s⟯) : A⟮T/s⟯) • (Dspan ↑V) ≤ Dspan ↑U := begin refine ⟨⟨_, mul_T_open _ hT U, by apply_instance⟩, _⟩, erw [subtype.coe_mk (↑(T • span ℤ ↑U) : set A), @submodule.smul_def ℤ, span_mul_span], change _ • span _ ↑(submodule.map (alg_hom_int $ (of_id A A⟮T/s⟯ : A → A⟮T/s⟯)).to_linear_map _) ≤ _, erw [← span_image, span_span_int, submodule.smul_def, span_mul_span, span_le], rintros _ ⟨s_inv, hs_inv, tu, htu, rfl⟩, erw mem_image at htu, rcases htu with ⟨_, ⟨t, ht, u, hu, rfl⟩, rfl⟩, rw submodule.mem_coe, convert (span _ _).smul_mem _ _ using 1, work_on_goal 3 { exact subset_span ⟨u, hu, rfl⟩ }, work_on_goal 1 { constructor }, work_on_goal 0 { change s_inv * (algebra_map _ _) = _ • (algebra_map _ _), rw [algebra.map_mul, ← mul_assoc], congr }, { apply ring.mem_closure, refine ⟨t, ⟨t, ht, rfl⟩, _⟩, rw set.mem_singleton_iff at hs_inv, rw hs_inv, refl } end /--Helper lemma. A special case of mul_left, where the element a is the inverse of a power of s.-/ lemma mul_left.aux₂ (hT : is_open (↑(ideal.span T) : set A)) (s' : powers s) (U : open_add_subgroup A) : ∃ (V : open_add_subgroup A), (↑((to_units s')⁻¹ : units A⟮T/s⟯) : A⟮T/s⟯) • (Dspan (V : set A)) ≤ Dspan (U : set A) := begin rcases s' with ⟨_, ⟨n, rfl⟩⟩, induction n with k hk, { use U, simp only [pow_zero], change (1 : A⟮T/s⟯) • _ ≤ _, rw one_smul, exact le_refl _ }, cases hk with W hW, cases mul_left.aux₁ T s hT W with V hV, use V, refine le_trans _ hW, refine le_trans (le_of_eq _) (smul_le_smul (le_refl _) hV), change _ = (_ : A⟮T/s⟯) • _, rw ← mul_smul, congr' 1, change ⟦((1 : A), _)⟧ = ⟦(1 * 1, _)⟧, simpa [pow_succ'], end /-- For every element a of A⟮T/s⟯ and every open subgroup U of A, there exists an open subgroup V of A, such that a • Dspan V ≤ Dspan U. -/ lemma mul_left (hT : is_open (↑(ideal.span T) : set A)) (a : A⟮T/s⟯) (U : open_add_subgroup A) : ∃ (V : open_add_subgroup A), a • (Dspan (V : set A)) ≤ Dspan (U : set A) := begin apply localization.induction_on a, intros a' s', clear a, cases mul_left.aux₂ _ _ hT s' U with W hW, cases left_mul_subset T s Huber_ring.nonarchimedean W a' with V hV, use V, erw [localization.mk_eq, mul_comm, mul_smul], exact le_trans (smul_le_smul (le_refl _) hV) hW end /- Now that we have the lemma mul_left in place, we can define the topology on A⟮T/s⟯. We construct the topology using a basis of open subgroups. -/ /-- The basis of open subgroups of the topology on A⟮T/s⟯.-/ def top_loc_basis (hT : is_open (↑(ideal.span T) : set A)) : subgroups_basis A⟮T/s⟯ := subgroups_basis.of_indexed_submodules_of_comm (λ U : open_add_subgroup A, (span D (coe '' U.1))) (directed T s) (mul_left T s hT) (mul_le T s Huber_ring.nonarchimedean) /-- The topology on A⟮T/s⟯.-/ def top_space (hT : is_open (↑(ideal.span T) : set A)) : topological_space A⟮T/s⟯ := @subgroups_basis.topology A⟮T/s⟯ _ (top_loc_basis T s hT) /-- The natural map A → A⟮T/s⟯ is continuous.-/ lemma of_continuous (hT : is_open (↑(ideal.span T) : set A)) : @continuous _ _ _ (away.top_space T s hT) (of : A → A⟮T/s⟯) := begin letI := away.top_loc_basis T s hT, letI := away.top_space T s hT, haveI : topological_add_group A⟮T/s⟯ := subgroups_basis.is_topological_add_group, suffices : continuous_at (coe : A → A⟮T/s⟯) 0, from topological_add_group.continuous_of_continuous_at_zero _ this, unfold continuous_at, rw subgroups_basis.tendsto_into, rintros _ ⟨U, rfl⟩, suffices : coe ⁻¹' (Dspan U.val).carrier ∈ nhds (0 : A), { simpa only [show ((0:A) : A⟮T/s⟯) = 0, from rfl, sub_zero] using this }, apply filter.mem_sets_of_superset (open_add_subgroup.mem_nhds_zero U), rw ← image_subset_iff, exact subset_span end section variables {B : Type*} [comm_ring B] (f : A → B) [is_ring_hom f] variables (fs : units B) (hs : f s = fs) /-- The universal property of the localization of a Huber ring. (Let A be a Huber ring, s an element of A and T ⊆ A a subset that generates an open ideal. Let B be a ring, and f : A → B a ring homomorphism, such that f(s) is invertible. The natural map A⟮T/s⟯ → B is simply defined using the universal property of ordinary localizations. Under additional assumptions, this map is continuous. See lift_continuous.) -/ noncomputable def lift : A⟮T/s⟯ → B := localization.away.lift f (hs.symm ▸ is_unit_unit fs) /-- The natural map from the localization of a Huber ring to another topological ring (satisfying certain assumptions) is a ring homomorphism. -/ instance : is_ring_hom (lift T s f fs hs : A⟮T/s⟯ → B) := localization.away.lift.is_ring_hom f _ variable {f} @[simp] lemma lift_of (a : A) : lift T s f fs hs (of a) = f a := localization.away.lift_of _ _ _ @[simp] lemma lift_coe (a : A) : lift T s f fs hs a = f a := localization.away.lift_of _ _ _ @[simp] lemma lift_comp_of : lift T s f fs hs ∘ of = f := localization.lift'_comp_of _ _ _ end section variables {B : Type*} [comm_ring B] [topological_space B] [topological_ring B] variables (hB : nonarchimedean B) {f : A → B} [is_ring_hom f] (hf : continuous f) variables (fs : units B) (hs : f s = fs) variables (hT : is_open (↑(ideal.span T) : set A)) variables (hTB : is_power_bounded_subset ((↑fs⁻¹ : B) • f '' T)) include hB hf hT hTB /-- Let A be a Huber ring, s an element of A and T ⊆ A a subset that generates an open ideal. Let B be a nonarchimedean ring, and f : A → B a continuous ring homomorphism, such that f(s) is invertible. Suppose that f(s)⁻¹ * f(T) is a power bounded subset of B. Then the natural map A⟮T/s⟯ → B is continuous. -/ lemma lift_continuous : @continuous _ _ (away.top_space T s hT) _ (lift T s f fs hs) := begin letI := away.top_loc_basis T s hT, letI := away.top_space T s hT, haveI : topological_add_group A⟮T/s⟯ := subgroups_basis.is_topological_add_group, apply continuous_of_continuous_at_zero _ _, all_goals {try {apply_instance}}, intros U hU, rw is_ring_hom.map_zero (lift T s f fs hs) at hU, rw filter.mem_map_sets_iff, let hF := power_bounded.ring.closure' hB _ hTB, erw is_bounded_add_subgroup_iff hB at hF, rcases hF U hU with ⟨V, hVF⟩, let hV := V.mem_nhds_zero, rw ← is_ring_hom.map_zero f at hV, replace hV := hf.tendsto 0 hV, rw filter.mem_map_sets_iff at hV, rcases hV with ⟨W, hW, hWV⟩, cases Huber_ring.nonarchimedean W hW with Y hY, refine ⟨↑(Dspan Y), _, _⟩, { apply mem_nhds_sets, { exact subgroups_basis.is_op _ rfl (mem_range_self _) }, { exact (Dspan ↑Y).zero_mem } }, { refine set.subset.trans _ hVF, rintros _ ⟨x, hx, rfl⟩, apply span_induction hx, { rintros _ ⟨a, ha, rfl⟩, erw [lift_of, ← mul_one (f a)], refine mul_mem_mul (subset_span $ hWV $ ⟨a, hY ha, rfl⟩) (subset_span $ is_submonoid.one_mem _) }, { rw is_ring_hom.map_zero (lift T s f fs hs), exact is_add_submonoid.zero_mem _ }, { intros a b ha hb, rw is_ring_hom.map_add (lift T s f fs hs), exact is_add_submonoid.add_mem ha hb }, { rw [submodule.smul_def, span_mul_span], intros d a ha, rw [smul_def'', is_ring_hom.map_mul (lift T s f fs hs), mul_comm], rcases (finsupp.mem_span_iff_total ℤ).mp (by rw set.image_id; exact ha) with ⟨l, hl₁, hl₂⟩, rw finsupp.mem_supported at hl₁, rw [← hl₂, finsupp.total_apply] at ha ⊢, rw finsupp.sum_mul, refine is_add_submonoid.finset_sum_mem ↑(span _ _) _ _ _, intros b hb', apply subset_span, --show (↑(_ : ℤ) * _) * _ ∈ _, simp only [smul_def''], rcases hl₁ hb' with ⟨v, hv, b, hb, rfl⟩, refine ⟨↑(l (v * b)) * v, _, b * lift T s f fs hs ↑d, _, _⟩, { rw ← gsmul_eq_mul, exact is_add_subgroup.gsmul_mem hv }, { refine is_submonoid.mul_mem hb _, cases d with d hd, rw subtype.coe_mk, apply ring.in_closure.rec_on hd, { rw is_ring_hom.map_one (lift T s f fs hs), exact is_submonoid.one_mem _ }, { rw [is_ring_hom.map_neg (lift T s f fs hs), is_ring_hom.map_one (lift T s f fs hs)], exact is_add_subgroup.neg_mem (is_submonoid.one_mem _) }, { rintros _ ⟨_, ⟨t, ht, rfl⟩, rfl⟩ b hb, rw is_ring_hom.map_mul (lift T s f fs hs), refine is_submonoid.mul_mem _ hb, apply ring.mem_closure, erw [smul_eq_mul, is_ring_hom.map_mul (lift T s f fs hs), lift_of], refine ⟨_, ⟨t, ht, rfl⟩, _⟩, congr' 1, erw [← units.coe_map' (lift T s f fs hs), ← units.ext_iff, (units.map' _).map_inv, inv_inj', units.ext_iff, ← hs], { exact lift_of T s fs hs s } }, { intros a b ha hb, rw is_ring_hom.map_add (lift T s f fs hs), exact is_add_submonoid.add_mem ha hb } }, { simpa [mul_assoc] } } } end end end away end Huber_ring
fd3dfc4ec4fb54a58ff2c5d828b7c073497dd61c
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/stage0/src/Lean/Elab/Tactic/ElabTerm.lean
9b8dbad2c62312ab6032637f8f7885d957d6f32e
[ "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
12,056
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.CollectMVars import Lean.Meta.Tactic.Apply import Lean.Meta.Tactic.Constructor import Lean.Meta.Tactic.Assert import Lean.Elab.Tactic.Basic import Lean.Elab.SyntheticMVars namespace Lean.Elab.Tactic open Meta /- `elabTerm` for Tactics and basic tactics that use it. -/ def elabTerm (stx : Syntax) (expectedType? : Option Expr) (mayPostpone := false) : TacticM Expr := do /- We have disabled `Term.withoutErrToSorry` to improve error recovery. When we were using it, any tactic using `elabTerm` would be interrupted at elaboration errors. Tactics that do not want to proceed should check whether the result contains sythetic sorrys or disable `errToSorry` before invoking `elabTerm` -/ withRef stx do -- <| Term.withoutErrToSorry do let e ← Term.elabTerm stx expectedType? Term.synthesizeSyntheticMVars mayPostpone instantiateMVars e def elabTermEnsuringType (stx : Syntax) (expectedType? : Option Expr) (mayPostpone := false) : TacticM Expr := do let e ← elabTerm stx expectedType? mayPostpone -- We do use `Term.ensureExpectedType` because we don't want coercions being inserted here. match expectedType? with | none => return e | some expectedType => let eType ← inferType e -- We allow synthetic opaque metavars to be assigned in the following step since the `isDefEq` is not really -- part of the elaboration, but part of the tactic. See issue #492 unless (← withAssignableSyntheticOpaque do isDefEq eType expectedType) do Term.throwTypeMismatchError none expectedType eType e return e /- Try to close main goal using `x target`, where `target` is the type of the main goal. -/ def closeMainGoalUsing (x : Expr → TacticM Expr) (checkUnassigned := true) : TacticM Unit := withMainContext do closeMainGoal (checkUnassigned := checkUnassigned) (← x (← getMainTarget)) def logUnassignedAndAbort (mvarIds : Array MVarId) : TacticM Unit := do if (← Term.logUnassignedUsingErrorInfos mvarIds) then throwAbortTactic def filterOldMVars (mvarIds : Array MVarId) (mvarCounterSaved : Nat) : MetaM (Array MVarId) := do let mctx ← getMCtx return mvarIds.filter fun mvarId => (mctx.getDecl mvarId |>.index) >= mvarCounterSaved @[builtinTactic «exact»] def evalExact : Tactic := fun stx => match stx with | `(tactic| exact $e) => closeMainGoalUsing (checkUnassigned := false) fun type => do let mvarCounterSaved := (← getMCtx).mvarCounter let r ← elabTermEnsuringType e type logUnassignedAndAbort (← filterOldMVars (← getMVars r) mvarCounterSaved) return r | _ => throwUnsupportedSyntax def elabTermWithHoles (stx : Syntax) (expectedType? : Option Expr) (tagSuffix : Name) (allowNaturalHoles := false) : TacticM (Expr × List MVarId) := do let mvarCounterSaved := (← getMCtx).mvarCounter let val ← elabTermEnsuringType stx expectedType? let newMVarIds ← getMVarsNoDelayed val /- ignore let-rec auxiliary variables, they are synthesized automatically later -/ let newMVarIds ← newMVarIds.filterM fun mvarId => return !(← Term.isLetRecAuxMVar mvarId) let newMVarIds ← if allowNaturalHoles then pure newMVarIds.toList else let naturalMVarIds ← newMVarIds.filterM fun mvarId => return (← getMVarDecl mvarId).kind.isNatural let syntheticMVarIds ← newMVarIds.filterM fun mvarId => return !(← getMVarDecl mvarId).kind.isNatural let naturalMVarIds ← filterOldMVars naturalMVarIds mvarCounterSaved logUnassignedAndAbort naturalMVarIds pure syntheticMVarIds.toList tagUntaggedGoals (← getMainTag) tagSuffix newMVarIds pure (val, newMVarIds) /- If `allowNaturalHoles == true`, then we allow the resultant expression to contain unassigned "natural" metavariables. Recall that "natutal" metavariables are created for explicit holes `_` and implicit arguments. They are meant to be filled by typing constraints. "Synthetic" metavariables are meant to be filled by tactics and are usually created using the synthetic hole notation `?<hole-name>`. -/ def refineCore (stx : Syntax) (tagSuffix : Name) (allowNaturalHoles : Bool) : TacticM Unit := do withMainContext do let (val, mvarIds') ← elabTermWithHoles stx (← getMainTarget) tagSuffix allowNaturalHoles assignExprMVar (← getMainGoal) val replaceMainGoal mvarIds' @[builtinTactic «refine»] def evalRefine : Tactic := fun stx => match stx with | `(tactic| refine $e) => refineCore e `refine (allowNaturalHoles := false) | _ => throwUnsupportedSyntax @[builtinTactic «refine'»] def evalRefine' : Tactic := fun stx => match stx with | `(tactic| refine' $e) => refineCore e `refine' (allowNaturalHoles := true) | _ => throwUnsupportedSyntax @[builtinTactic «specialize»] def evalSpecialize : Tactic := fun stx => withMainContext do match stx with | `(tactic| specialize $e:term) => let (e, mvarIds') ← elabTermWithHoles e none `specialize (allowNaturalHoles := true) let h := e.getAppFn if h.isFVar then let localDecl ← getLocalDecl h.fvarId! let mvarId ← assert (← getMainGoal) localDecl.userName (← inferType e).headBeta e let (_, mvarId) ← intro1P mvarId let mvarId ← tryClear mvarId h.fvarId! replaceMainGoal (mvarId :: mvarIds') else throwError "'specialize' requires a term of the form `h x_1 .. x_n` where `h` appears in the local context" | _ => throwUnsupportedSyntax /-- Given a tactic ``` apply f ``` we want the `apply` tactic to create all metavariables. The following definition will return `@f` for `f`. That is, it will **not** create metavariables for implicit arguments. A similar method is also used in Lean 3. This method is useful when applying lemmas such as: ``` theorem infLeRight {s t : Set α} : s ⊓ t ≤ t ``` where `s ≤ t` here is defined as ``` ∀ {x : α}, x ∈ s → x ∈ t ``` -/ def elabTermForApply (stx : Syntax) : TacticM Expr := do if stx.isIdent then match (← Term.resolveId? stx (withInfo := true)) with | some e => return e | _ => pure () elabTerm stx none (mayPostpone := true) def evalApplyLikeTactic (tac : MVarId → Expr → MetaM (List MVarId)) (e : Syntax) : TacticM Unit := do withMainContext do let val ← elabTermForApply e let mvarIds' ← tac (← getMainGoal) val Term.synthesizeSyntheticMVarsNoPostponing replaceMainGoal mvarIds' @[builtinTactic Lean.Parser.Tactic.apply] def evalApply : Tactic := fun stx => match stx with | `(tactic| apply $e) => evalApplyLikeTactic Meta.apply e | _ => throwUnsupportedSyntax @[builtinTactic Lean.Parser.Tactic.constructor] def evalConstructor : Tactic := fun stx => withMainContext do let mvarIds' ← Meta.constructor (← getMainGoal) Term.synthesizeSyntheticMVarsNoPostponing replaceMainGoal mvarIds' @[builtinTactic Lean.Parser.Tactic.existsIntro] def evalExistsIntro : Tactic := fun stx => match stx with | `(tactic| exists $e) => evalApplyLikeTactic (fun mvarId e => return [(← Meta.existsIntro mvarId e)]) e | _ => throwUnsupportedSyntax @[builtinTactic Lean.Parser.Tactic.withReducible] def evalWithReducible : Tactic := fun stx => withReducible <| evalTactic stx[1] @[builtinTactic Lean.Parser.Tactic.withReducibleAndInstances] def evalWithReducibleAndInstances : Tactic := fun stx => withReducibleAndInstances <| evalTactic stx[1] /-- Elaborate `stx`. If it a free variable, return it. Otherwise, assert it, and return the free variable. Note that, the main goal is updated when `Meta.assert` is used in the second case. -/ def elabAsFVar (stx : Syntax) (userName? : Option Name := none) : TacticM FVarId := withMainContext do let e ← elabTerm stx none match e with | Expr.fvar fvarId _ => pure fvarId | _ => let type ← inferType e let intro (userName : Name) (preserveBinderNames : Bool) : TacticM FVarId := do let mvarId ← getMainGoal let (fvarId, mvarId) ← liftMetaM do let mvarId ← Meta.assert mvarId userName type e Meta.intro1Core mvarId preserveBinderNames replaceMainGoal [mvarId] return fvarId match userName? with | none => intro `h false | some userName => intro userName true @[builtinTactic Lean.Parser.Tactic.rename] def evalRename : Tactic := fun stx => match stx with | `(tactic| rename $typeStx:term => $h:ident) => do withMainContext do /- Remark: we must not use `withoutModifyingState` because we may miss errors message. For example, suppose the following `elabTerm` logs an error during elaboration. In this scenario, the term `type` contains a synthetic `sorry`, and the error message `"failed to find ..."` is not logged by the outer loop. By using `withoutModifyingStateWithInfoAndMessages`, we ensure that the messages and the info trees are preserved while the rest of the state is backtracked. -/ let fvarId ← withoutModifyingStateWithInfoAndMessages <| withNewMCtxDepth do let type ← elabTerm typeStx none (mayPostpone := true) let fvarId? ← (← getLCtx).findDeclRevM? fun localDecl => do if (← isDefEq type localDecl.type) then return localDecl.fvarId else return none match fvarId? with | none => throwError "failed to find a hypothesis with type{indentExpr type}" | some fvarId => return fvarId let lctxNew := (← getLCtx).setUserName fvarId h.getId let mvarNew ← mkFreshExprMVarAt lctxNew (← getLocalInstances) (← getMainTarget) MetavarKind.syntheticOpaque (← getMainTag) assignExprMVar (← getMainGoal) mvarNew replaceMainGoal [mvarNew.mvarId!] | _ => throwUnsupportedSyntax /-- Make sure `expectedType` does not contain free and metavariables. It applies zeta-reduction to eliminate let-free-vars. -/ private def preprocessPropToDecide (expectedType : Expr) : TermElabM Expr := do let mut expectedType ← instantiateMVars expectedType if expectedType.hasFVar then expectedType ← zetaReduce expectedType if expectedType.hasFVar || expectedType.hasMVar then throwError "expected type must not contain free or meta variables{indentExpr expectedType}" return expectedType @[builtinTactic Lean.Parser.Tactic.decide] def evalDecide : Tactic := fun stx => closeMainGoalUsing fun expectedType => do let expectedType ← preprocessPropToDecide expectedType let d ← mkDecide expectedType let d ← instantiateMVars d let r ← withDefault <| whnf d unless r.isConstOf ``true do throwError "failed to reduce to 'true'{indentExpr r}" let s := d.appArg! -- get instance from `d` let rflPrf ← mkEqRefl (toExpr true) return mkApp3 (Lean.mkConst ``of_decide_eq_true) expectedType s rflPrf private def mkNativeAuxDecl (baseName : Name) (type val : Expr) : TermElabM Name := do let auxName ← Term.mkAuxName baseName let decl := Declaration.defnDecl { name := auxName, levelParams := [], type := type, value := val, hints := ReducibilityHints.abbrev, safety := DefinitionSafety.safe } addDecl decl compileDecl decl pure auxName @[builtinTactic Lean.Parser.Tactic.nativeDecide] def evalNativeDecide : Tactic := fun stx => closeMainGoalUsing fun expectedType => do let expectedType ← preprocessPropToDecide expectedType let d ← mkDecide expectedType let auxDeclName ← mkNativeAuxDecl `_nativeDecide (Lean.mkConst `Bool) d let rflPrf ← mkEqRefl (toExpr true) let s := d.appArg! -- get instance from `d` return mkApp3 (Lean.mkConst ``of_decide_eq_true) expectedType s <| mkApp3 (Lean.mkConst ``Lean.ofReduceBool) (Lean.mkConst auxDeclName) (toExpr true) rflPrf end Lean.Elab.Tactic
3cccc7f2f6a4cf6f9f96ac2c0be3a8098dea6a52
b7f22e51856f4989b970961f794f1c435f9b8f78
/hott/algebra/category/constructions/discrete.hlean
f2ba204ce5169667f213ab6cbe698c22dd615c01
[ "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
2,828
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 Discrete category -/ import ..groupoid types.bool ..nat_trans open eq is_trunc iso bool functor nat_trans namespace category definition precategory_of_1_type [constructor] (A : Type) [H : is_trunc 1 A] : precategory A := @precategory.mk _ _ (@is_trunc_eq _ _ H) (λ (a b c : A) (p : b = c) (q : a = b), q ⬝ p) (λ (a : A), refl a) (λ (a b c d : A) (p : c = d) (q : b = c) (r : a = b), con.assoc r q p) (λ (a b : A) (p : a = b), con_idp p) (λ (a b : A) (p : a = b), idp_con p) definition groupoid_of_1_type [constructor] (A : Type) [H : is_trunc 1 A] : groupoid A := groupoid.mk !precategory_of_1_type (λ (a b : A) (p : a = b), is_iso.mk _ !con.right_inv !con.left_inv) definition Precategory_of_1_type [constructor] (A : Type) [H : is_trunc 1 A] : Precategory := precategory.Mk (precategory_of_1_type A) definition Groupoid_of_1_type [constructor] (A : Type) [H : is_trunc 1 A] : Groupoid := groupoid.Mk _ (groupoid_of_1_type A) definition discrete_precategory [constructor] (A : Type) [H : is_set A] : precategory A := precategory_of_1_type A definition discrete_groupoid [constructor] (A : Type) [H : is_set A] : groupoid A := groupoid_of_1_type A definition Discrete_precategory [constructor] (A : Type) [H : is_set A] : Precategory := precategory.Mk (discrete_precategory A) definition Discrete_groupoid [constructor] (A : Type) [H : is_set A] : Groupoid := groupoid.Mk _ (discrete_groupoid A) definition c2 [constructor] : Precategory := Discrete_precategory bool definition c2_functor [constructor] (C : Precategory) (x y : C) : c2 ⇒ C := functor.mk (bool.rec x y) (bool.rec (bool.rec (λf, id) (by contradiction)) (bool.rec (by contradiction) (λf, id))) abstract (bool.rec idp idp) end abstract begin intro b₁ b₂ b₃ g f, induction b₁: induction b₂: induction b₃: esimp at *: try contradiction: exact !id_id⁻¹ end end definition c2_functor_eta {C : Precategory} (F : c2 ⇒ C) : c2_functor C (to_fun_ob F ff) (to_fun_ob F tt) = F := begin fapply functor_eq: esimp, { intro b, induction b: reflexivity}, { intro b₁ b₂ p, induction p, induction b₁: esimp; rewrite [id_leftright]; exact !respect_id⁻¹} end definition c2_nat_trans [constructor] {C : Precategory} {x y u v : C} (f : x ⟶ u) (g : y ⟶ v) : c2_functor C x y ⟹ c2_functor C u v := begin fapply nat_trans.mk: esimp, { intro b, induction b, exact f, exact g}, { intro b₁ b₂ p, induction p, induction b₁: esimp: apply id_comp_eq_comp_id}, end end category
d0e59605ade3851abd0a8d756a9cf95b433a4760
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/real/cau_seq_completion.lean
6585ac8eb9315cec30c2d48905042a1b3b59b2a1
[ "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
9,985
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Robert Y. Lewis Generalizes the Cauchy completion of (ℚ, abs) to the completion of a commutative ring with absolute value. -/ import data.real.cau_seq namespace cau_seq.completion open cau_seq section parameters {α : Type*} [linear_ordered_field α] parameters {β : Type*} [comm_ring β] {abv : β → α} [is_absolute_value abv] def Cauchy := @quotient (cau_seq _ abv) cau_seq.equiv def mk : cau_seq _ abv → Cauchy := quotient.mk @[simp] theorem mk_eq_mk (f) : @eq Cauchy ⟦f⟧ (mk f) := rfl theorem mk_eq {f g} : mk f = mk g ↔ f ≈ g := quotient.eq def of_rat (x : β) : Cauchy := mk (const abv x) instance : has_zero Cauchy := ⟨of_rat 0⟩ instance : has_one Cauchy := ⟨of_rat 1⟩ instance : inhabited Cauchy := ⟨0⟩ theorem of_rat_zero : of_rat 0 = 0 := rfl theorem of_rat_one : of_rat 1 = 1 := rfl @[simp] theorem mk_eq_zero {f} : mk f = 0 ↔ lim_zero f := by have : mk f = 0 ↔ lim_zero (f - 0) := quotient.eq; rwa sub_zero at this instance : has_add Cauchy := ⟨λ x y, quotient.lift_on₂ x y (λ f g, mk (f + g)) $ λ f₁ g₁ f₂ g₂ hf hg, quotient.sound $ by simpa [(≈), setoid.r, sub_eq_add_neg, add_comm, add_left_comm, add_assoc] using add_lim_zero hf hg⟩ @[simp] theorem mk_add (f g : cau_seq β abv) : mk f + mk g = mk (f + g) := rfl instance : has_neg Cauchy := ⟨λ x, quotient.lift_on x (λ f, mk (-f)) $ λ f₁ f₂ hf, quotient.sound $ by simpa [(≈), setoid.r] using neg_lim_zero hf⟩ @[simp] theorem mk_neg (f : cau_seq β abv) : -mk f = mk (-f) := rfl instance : has_mul Cauchy := ⟨λ x y, quotient.lift_on₂ x y (λ f g, mk (f * g)) $ λ f₁ g₁ f₂ g₂ hf hg, quotient.sound $ by simpa [(≈), setoid.r, mul_add, mul_comm, add_assoc, sub_eq_add_neg] using add_lim_zero (mul_lim_zero_right g₁ hf) (mul_lim_zero_right f₂ hg)⟩ @[simp] theorem mk_mul (f g : cau_seq β abv) : mk f * mk g = mk (f * g) := rfl instance : has_sub Cauchy := ⟨λ x y, quotient.lift_on₂ x y (λ f g, mk (f - g)) $ λ f₁ g₁ f₂ g₂ hf hg, quotient.sound $ show ((f₁ - g₁) - (f₂ - g₂)).lim_zero, by simpa [sub_eq_add_neg, add_assoc, add_comm, add_left_comm] using sub_lim_zero hf hg⟩ @[simp] theorem mk_sub (f g : cau_seq β abv) : mk f - mk g = mk (f - g) := rfl theorem of_rat_add (x y : β) : of_rat (x + y) = of_rat x + of_rat y := congr_arg mk (const_add _ _) theorem of_rat_neg (x : β) : of_rat (-x) = -of_rat x := congr_arg mk (const_neg _) theorem of_rat_mul (x y : β) : of_rat (x * y) = of_rat x * of_rat y := congr_arg mk (const_mul _ _) private lemma zero_def : 0 = mk 0 := rfl private lemma one_def : 1 = mk 1 := rfl instance : comm_ring Cauchy := by refine { neg := has_neg.neg, sub := has_sub.sub, sub_eq_add_neg := _, add := (+), zero := (0 : Cauchy), mul := (*), one := 1, nsmul := nsmul_rec, npow := npow_rec, gsmul := gsmul_rec, .. }; try { intros; refl }; { repeat {refine λ a, quotient.induction_on a (λ _, _)}, simp [zero_def, one_def, mul_left_comm, mul_comm, mul_add, add_comm, add_left_comm, sub_eq_add_neg] } theorem of_rat_sub (x y : β) : of_rat (x - y) = of_rat x - of_rat y := congr_arg mk (const_sub _ _) end open_locale classical section parameters {α : Type*} [linear_ordered_field α] parameters {β : Type*} [field β] {abv : β → α} [is_absolute_value abv] local notation `Cauchy` := @Cauchy _ _ _ _ abv _ noncomputable instance : has_inv Cauchy := ⟨λ x, quotient.lift_on x (λ f, mk $ if h : lim_zero f then 0 else inv f h) $ λ f g fg, begin have := lim_zero_congr fg, by_cases hf : lim_zero f, { simp [hf, this.1 hf, setoid.refl] }, { have hg := mt this.2 hf, simp [hf, hg], have If : mk (inv f hf) * mk f = 1 := mk_eq.2 (inv_mul_cancel hf), have Ig : mk (inv g hg) * mk g = 1 := mk_eq.2 (inv_mul_cancel hg), rw [mk_eq.2 fg, ← Ig] at If, rw mul_comm at Ig, rw [← mul_one (mk (inv f hf)), ← Ig, ← mul_assoc, If, mul_assoc, Ig, mul_one] } end⟩ @[simp] theorem inv_zero : (0 : Cauchy)⁻¹ = 0 := congr_arg mk $ by rw dif_pos; [refl, exact zero_lim_zero] @[simp] theorem inv_mk {f} (hf) : (@mk α _ β _ abv _ f)⁻¹ = mk (inv f hf) := congr_arg mk $ by rw dif_neg lemma cau_seq_zero_ne_one : ¬ (0 : cau_seq _ abv) ≈ 1 := λ h, have lim_zero (1 - 0), from setoid.symm h, have lim_zero 1, by simpa, one_ne_zero $ const_lim_zero.1 this lemma zero_ne_one : (0 : Cauchy) ≠ 1 := λ h, cau_seq_zero_ne_one $ mk_eq.1 h protected theorem inv_mul_cancel {x : Cauchy} : x ≠ 0 → x⁻¹ * x = 1 := quotient.induction_on x $ λ f hf, begin simp at hf, simp [hf], exact quotient.sound (cau_seq.inv_mul_cancel hf) end noncomputable def field : field Cauchy := { inv := has_inv.inv, mul_inv_cancel := λ x x0, by rw [mul_comm, cau_seq.completion.inv_mul_cancel x0], exists_pair_ne := ⟨0, 1, zero_ne_one⟩, inv_zero := inv_zero, .. Cauchy.comm_ring } local attribute [instance] field theorem of_rat_inv (x : β) : of_rat (x⁻¹) = ((of_rat x)⁻¹ : Cauchy) := congr_arg mk $ by split_ifs with h; [simp [const_lim_zero.1 h], refl] theorem of_rat_div (x y : β) : of_rat (x / y) = (of_rat x / of_rat y : Cauchy) := by simp only [div_eq_inv_mul, of_rat_inv, of_rat_mul] end end cau_seq.completion variables {α : Type*} [linear_ordered_field α] namespace cau_seq section variables (β : Type*) [ring β] (abv : β → α) [is_absolute_value abv] class is_complete := (is_complete : ∀ s : cau_seq β abv, ∃ b : β, s ≈ const abv b) end section variables {β : Type*} [ring β] {abv : β → α} [is_absolute_value abv] variable [is_complete β abv] lemma complete : ∀ s : cau_seq β abv, ∃ b : β, s ≈ const abv b := is_complete.is_complete noncomputable def lim (s : cau_seq β abv) := classical.some (complete s) lemma equiv_lim (s : cau_seq β abv) : s ≈ const abv (lim s) := classical.some_spec (complete s) lemma eq_lim_of_const_equiv {f : cau_seq β abv} {x : β} (h : cau_seq.const abv x ≈ f) : x = lim f := const_equiv.mp $ setoid.trans h $ equiv_lim f lemma lim_eq_of_equiv_const {f : cau_seq β abv} {x : β} (h : f ≈ cau_seq.const abv x) : lim f = x := (eq_lim_of_const_equiv $ setoid.symm h).symm lemma lim_eq_lim_of_equiv {f g : cau_seq β abv} (h : f ≈ g) : lim f = lim g := lim_eq_of_equiv_const $ setoid.trans h $ equiv_lim g @[simp] lemma lim_const (x : β) : lim (const abv x) = x := lim_eq_of_equiv_const $ setoid.refl _ lemma lim_add (f g : cau_seq β abv) : lim f + lim g = lim (f + g) := eq_lim_of_const_equiv $ show lim_zero (const abv (lim f + lim g) - (f + g)), by rw [const_add, add_sub_comm]; exact add_lim_zero (setoid.symm (equiv_lim f)) (setoid.symm (equiv_lim g)) lemma lim_mul_lim (f g : cau_seq β abv) : lim f * lim g = lim (f * g) := eq_lim_of_const_equiv $ show lim_zero (const abv (lim f * lim g) - f * g), from have h : const abv (lim f * lim g) - f * g = (const abv (lim f) - f) * g + const abv (lim f) * (const abv (lim g) - g) := by simp [const_mul (lim f), mul_add, add_mul, sub_eq_add_neg, add_comm, add_left_comm], by rw h; exact add_lim_zero (mul_lim_zero_left _ (setoid.symm (equiv_lim _))) (mul_lim_zero_right _ (setoid.symm (equiv_lim _))) lemma lim_mul (f : cau_seq β abv) (x : β) : lim f * x = lim (f * const abv x) := by rw [← lim_mul_lim, lim_const] lemma lim_neg (f : cau_seq β abv) : lim (-f) = -lim f := lim_eq_of_equiv_const (show lim_zero (-f - const abv (-lim f)), by rw [const_neg, sub_neg_eq_add, add_comm, ← sub_eq_add_neg]; exact setoid.symm (equiv_lim f)) lemma lim_eq_zero_iff (f : cau_seq β abv) : lim f = 0 ↔ lim_zero f := ⟨assume h, by have hf := equiv_lim f; rw h at hf; exact (lim_zero_congr hf).mpr (const_lim_zero.mpr rfl), assume h, have h₁ : f = (f - const abv 0) := ext (λ n, by simp [sub_apply, const_apply]), by rw h₁ at h; exact lim_eq_of_equiv_const h ⟩ end section variables {β : Type*} [field β] {abv : β → α} [is_absolute_value abv] [is_complete β abv] lemma lim_inv {f : cau_seq β abv} (hf : ¬ lim_zero f) : lim (inv f hf) = (lim f)⁻¹ := have hl : lim f ≠ 0 := by rwa ← lim_eq_zero_iff at hf, lim_eq_of_equiv_const $ show lim_zero (inv f hf - const abv (lim f)⁻¹), from have h₁ : ∀ (g f : cau_seq β abv) (hf : ¬ lim_zero f), lim_zero (g - f * inv f hf * g) := λ g f hf, by rw [← one_mul g, ← mul_assoc, ← sub_mul, mul_one, mul_comm, mul_comm f]; exact mul_lim_zero_right _ (setoid.symm (cau_seq.inv_mul_cancel _)), have h₂ : lim_zero ((inv f hf - const abv (lim f)⁻¹) - (const abv (lim f) - f) * (inv f hf * const abv (lim f)⁻¹)) := by rw [sub_mul, ← sub_add, sub_sub, sub_add_eq_sub_sub, sub_right_comm, sub_add]; exact show lim_zero (inv f hf - const abv (lim f) * (inv f hf * const abv (lim f)⁻¹) - (const abv (lim f)⁻¹ - f * (inv f hf * const abv (lim f)⁻¹))), from sub_lim_zero (by rw [← mul_assoc, mul_right_comm, const_inv hl]; exact h₁ _ _ _) (by rw [← mul_assoc]; exact h₁ _ _ _), (lim_zero_congr h₂).mpr $ mul_lim_zero_left _ (setoid.symm (equiv_lim f)) end section variables [is_complete α abs] lemma lim_le {f : cau_seq α abs} {x : α} (h : f ≤ cau_seq.const abs x) : lim f ≤ x := cau_seq.const_le.1 $ cau_seq.le_of_eq_of_le (setoid.symm (equiv_lim f)) h lemma le_lim {f : cau_seq α abs} {x : α} (h : cau_seq.const abs x ≤ f) : x ≤ lim f := cau_seq.const_le.1 $ cau_seq.le_of_le_of_eq h (equiv_lim f) lemma lt_lim {f : cau_seq α abs} {x : α} (h : cau_seq.const abs x < f) : x < lim f := cau_seq.const_lt.1 $ cau_seq.lt_of_lt_of_eq h (equiv_lim f) lemma lim_lt {f : cau_seq α abs} {x : α} (h : f < cau_seq.const abs x) : lim f < x := cau_seq.const_lt.1 $ cau_seq.lt_of_eq_of_lt (setoid.symm (equiv_lim f)) h end end cau_seq
1f2e14ecc009ef0d1b4e67cdfaac5e014c5d7464
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/def_ite_value.lean
7d99444a540bd17fbd6b8cd2f295449e68a5ea93
[ "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
557
lean
inductive bv : nat → Type | nil : bv 0 | cons : Π n, bool → bv n → bv (n+1) open bv definition f : ∀ n : nat, bv n → nat → nat | (n+1) (cons .(n) b v) 1000000 := f n v 0 | (n+1) (cons .(n) b v) x := f n v (x + 1) | _ _ _ := 1 set_option pp.binder_types true #check @f._main.equations._eqn_1 #check @f._main.equations._eqn_2 #check @f._main.equations._eqn_3 example (n : nat) (b : bool) (v : bv n) (x : nat) : x ≠ 1000000 → f (n+1) (cons n b v) x = f n v (x + 1) := assume H, f.equations._eqn_3 n b v x H
8de157525f4c48a906dc4cacb98ecf77154d9cb6
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/normed/group/completion.lean
de99d3ecfcde7a726dc2a9c6a6c17d21b6b01ef1
[ "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
1,338
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import analysis.normed.group.basic import topology.algebra.group_completion import topology.metric_space.completion /-! # Completion of a normed group In this file we prove that the completion of a (semi)normed group is a normed group. ## Tags normed group, completion -/ noncomputable theory namespace uniform_space namespace completion variables (E : Type*) instance [uniform_space E] [has_norm E] : has_norm (completion E) := { norm := completion.extension has_norm.norm } @[simp] lemma norm_coe {E} [seminormed_add_comm_group E] (x : E) : ∥(x : completion E)∥ = ∥x∥ := completion.extension_coe uniform_continuous_norm x instance [seminormed_add_comm_group E] : normed_add_comm_group (completion E) := { dist_eq := begin intros x y, apply completion.induction_on₂ x y; clear x y, { refine is_closed_eq (completion.uniform_continuous_extension₂ _).continuous _, exact continuous.comp completion.continuous_extension continuous_sub }, { intros x y, rw [← completion.coe_sub, norm_coe, completion.dist_eq, dist_eq_norm] } end, .. completion.add_comm_group, .. completion.metric_space } end completion end uniform_space
3d7c2e05b412a9425b1ae755f3e6def876c8e09f
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/Parser/Do.lean
bbb42a9d41506d94fd168d2eed0d615149e3a213
[ "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
4,482
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.Parser.Term namespace Lean namespace Parser @[init] def regBuiltinDoElemParserAttr : IO Unit := registerBuiltinParserAttribute `builtinDoElemParser `doElem @[init] def regDoElemParserAttribute : IO Unit := registerBuiltinDynamicParserAttribute `doElemParser `doElem @[inline] def doElemParser (rbp : Nat := 0) : Parser := categoryParser `doElem rbp namespace Term def leftArrow : Parser := unicodeSymbol " ← " " <- " @[builtinTermParser] def liftMethod := parser!:0 leftArrow >> termParser def doSeqIndent := parser! many1Indent $ doElemParser >> optional "; " def doSeqBracketed := parser! "{" >> withoutPosition (many1 (doElemParser >> optional "; ")) >> "}" def doSeq := doSeqBracketed <|> doSeqIndent def notFollowedByRedefinedTermToken := notFollowedBy ("if" <|> "match" <|> "let" <|> "have" <|> "do" <|> "dbgTrace!" <|> "assert!") @[builtinDoElemParser] def doLet := parser! "let " >> letDecl @[builtinDoElemParser] def doLetRec := parser! group ("let " >> nonReservedSymbol "rec ") >> letRecDecls def doIdDecl := parser! «try» (ident >> optType >> leftArrow) >> termParser def doPatDecl := parser! «try» (termParser >> leftArrow) >> termParser >> optional (" | " >> termParser) @[builtinDoElemParser] def doLetArrow := parser! "let " >> (doIdDecl <|> doPatDecl) @[builtinDoElemParser] def doReassign := parser! notFollowedByRedefinedTermToken >> (letIdDecl <|> letPatDecl) @[builtinDoElemParser] def doReassignArrow := parser! notFollowedByRedefinedTermToken >> (doIdDecl <|> doPatDecl) @[builtinDoElemParser] def doHave := parser! "have " >> Term.haveDecl /- In `do` blocks, we support `if` without an `else`. Thus, we use indentation to prevent examples such as ``` if c_1 then if c_2 then action_1 else action_2 ``` from being parsed as ``` if c_1 then { if c_2 then { action_1 } else { action_2 } } ``` We also have special support for `else if` because we don't want to write ``` if c_1 then action_1 else if c_2 then action_2 else action_3 ``` -/ @[builtinDoElemParser] def doIf := parser! withPosition $ "if " >> optIdent >> termParser >> " then " >> doSeq >> many (checkColGe "'else if' in 'do' must be indented" >> group («try» (group (" else " >> " if ")) >> optIdent >> termParser >> " then " >> doSeq)) >> optional (checkColGe "'else' in 'do' must be indented" >> " else " >> doSeq) @[builtinDoElemParser] def doUnless := parser! "unless " >> withPosition (termParser >> optional ",") >> doSeq @[builtinDoElemParser] def doFor := parser! "for " >> termParser >> " in " >> withPosition (termParser >> optional ", ") >> doSeq /- `match`-expression where the right-hand-side of alternatives is a `doSeq` instead of a `term` -/ def doMatchAlt : Parser := sepBy1 termParser ", " >> darrow >> doSeq def doMatchAlts : Parser := parser! withPosition $ (optional "| ") >> sepBy1 doMatchAlt (checkColGe "alternatives must be indented" >> "|") @[builtinDoElemParser] def doMatch := parser!:leadPrec "match " >> sepBy1 matchDiscr ", " >> optType >> " with " >> doMatchAlts def doCatch := parser! «try» ("catch " >> binderIdent) >> optional binderType >> darrow >> doSeq def doCatchMatch := parser! "catch " >> doMatchAlts def doFinally := parser! "finally " >> doSeq @[builtinDoElemParser] def doTry := parser! "try " >> doSeq >> many (doCatch <|> doCatchMatch) >> optional doFinally @[builtinDoElemParser] def doBreak := parser! "break" @[builtinDoElemParser] def doContinue := parser! "continue" @[builtinDoElemParser] def doReturn := parser!:leadPrec "return " >> optional termParser @[builtinDoElemParser] def doDbgTrace := parser!:leadPrec "dbgTrace! " >> termParser @[builtinDoElemParser] def doAssert := parser!:leadPrec "assert! " >> termParser /- We use `notFollowedBy` to avoid counterintuitive behavior. For example, the `if`-term parser doesn't enforce indentation restrictions, but we don't want it to be used when `doIf` fails. Note that parser priorities would not solve this problem since the `doIf` parser is failing while the `if` parser is succeeding. -/ @[builtinDoElemParser] def doExpr := parser! notFollowedByRedefinedTermToken >> termParser @[builtinTermParser] def «do» := parser!:maxPrec "do " >> doSeq end Term end Parser end Lean
b74384f068e36fc6d0cbbcd48f8be52f57ca4a4d
5fbbd711f9bfc21ee168f46a4be146603ece8835
/lean/natural_number_game/power/2.lean
52e835ab1412f2bdfaad5c9cd6096a927473e18a
[ "LicenseRef-scancode-warranty-disclaimer" ]
no_license
goedel-gang/maths
22596f71e3fde9c088e59931f128a3b5efb73a2c
a20a6f6a8ce800427afd595c598a5ad43da1408d
refs/heads/master
1,623,055,941,960
1,621,599,441,000
1,621,599,441,000
169,335,840
0
0
null
null
null
null
UTF-8
Lean
false
false
96
lean
lemma zero_pow_succ (m : mynat) : (0 : mynat) ^ (succ m) = 0 := rwa [pow_succ, mul_zero], end
71276179aefeaaebf90fc7976ac3a562f56c0563
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/topology/bounded_continuous_function.lean
c8606ae1ef083554b705f28091d034b840e0776c
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
29,002
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Mario Carneiro, Yury Kudryashov, Heather Macbeth -/ import analysis.normed_space.basic /-! # Bounded continuous functions The type of bounded continuous functions taking values in a metric space, with the uniform distance. -/ noncomputable theory open_locale topological_space classical open set filter metric universes u v w variables {α : Type u} {β : Type v} {γ : Type w} /-- The type of bounded continuous functions from a topological space to a metric space -/ def bounded_continuous_function (α : Type u) (β : Type v) [topological_space α] [metric_space β] : Type (max u v) := {f : α → β // continuous f ∧ ∃C, ∀x y:α, dist (f x) (f y) ≤ C} local infixr ` →ᵇ `:25 := bounded_continuous_function namespace bounded_continuous_function section basics variables [topological_space α] [metric_space β] [metric_space γ] variables {f g : α →ᵇ β} {x : α} {C : ℝ} instance : has_coe_to_fun (α →ᵇ β) := ⟨_, subtype.val⟩ lemma bounded_range : bounded (range f) := bounded_range_iff.2 f.2.2 /-- If a function is continuous on a compact space, it is automatically bounded, and therefore gives rise to an element of the type of bounded continuous functions -/ def mk_of_compact [compact_space α] (f : α → β) (hf : continuous f) : α →ᵇ β := ⟨f, hf, bounded_range_iff.1 $ bounded_of_compact $ compact_range hf⟩ /-- If a function is bounded on a discrete space, it is automatically continuous, and therefore gives rise to an element of the type of bounded continuous functions -/ def mk_of_discrete [discrete_topology α] (f : α → β) (hf : ∃C, ∀x y, dist (f x) (f y) ≤ C) : α →ᵇ β := ⟨f, continuous_of_discrete_topology, hf⟩ /-- The uniform distance between two bounded continuous functions -/ instance : has_dist (α →ᵇ β) := ⟨λf g, Inf {C | 0 ≤ C ∧ ∀ x : α, dist (f x) (g x) ≤ C}⟩ lemma dist_eq : dist f g = Inf {C | 0 ≤ C ∧ ∀ x : α, dist (f x) (g x) ≤ C} := rfl lemma dist_set_exists : ∃ C, 0 ≤ C ∧ ∀ x : α, dist (f x) (g x) ≤ C := begin refine if h : nonempty α then _ else ⟨0, le_refl _, λ x, h.elim ⟨x⟩⟩, cases h with x, rcases f.2 with ⟨_, Cf, hCf⟩, /- hCf : ∀ (x y : α), dist (f.val x) (f.val y) ≤ Cf -/ rcases g.2 with ⟨_, Cg, hCg⟩, /- hCg : ∀ (x y : α), dist (g.val x) (g.val y) ≤ Cg -/ let C := max 0 (dist (f x) (g x) + (Cf + Cg)), exact ⟨C, le_max_left _ _, λ y, calc dist (f y) (g y) ≤ dist (f x) (g x) + (dist (f x) (f y) + dist (g x) (g y)) : dist_triangle4_left _ _ _ _ ... ≤ dist (f x) (g x) + (Cf + Cg) : add_le_add_left (add_le_add (hCf _ _) (hCg _ _)) _ ... ≤ C : le_max_right _ _⟩ end /-- The pointwise distance is controlled by the distance between functions, by definition -/ lemma dist_coe_le_dist (x : α) : dist (f x) (g x) ≤ dist f g := le_cInf dist_set_exists $ λb hb, hb.2 x @[ext] lemma ext (H : ∀x, f x = g x) : f = g := subtype.eq $ funext H lemma ext_iff : f = g ↔ ∀ x, f x = g x := ⟨λ h, λ x, h ▸ rfl, ext⟩ /- This lemma will be needed in the proof of the metric space instance, but it will become useless afterwards as it will be superceded by the general result that the distance is nonnegative is metric spaces. -/ private lemma dist_nonneg' : 0 ≤ dist f g := le_cInf dist_set_exists (λ C, and.left) /-- 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 := ⟨λ h x, le_trans (dist_coe_le_dist x) h, λ H, cInf_le ⟨0, λ C, and.left⟩ ⟨C0, H⟩⟩ /-- On an empty space, bounded continuous functions are at distance 0 -/ lemma dist_zero_of_empty (e : ¬ nonempty α) : dist f g = 0 := le_antisymm ((dist_le (le_refl _)).2 $ λ x, e.elim ⟨x⟩) dist_nonneg' /-- The type of bounded continuous functions, with the uniform distance, is a metric space. -/ instance : metric_space (α →ᵇ β) := { dist_self := λ f, le_antisymm ((dist_le (le_refl _)).2 $ λ x, by simp) dist_nonneg', eq_of_dist_eq_zero := λ f g hfg, by ext x; exact eq_of_dist_eq_zero (le_antisymm (hfg ▸ dist_coe_le_dist _) dist_nonneg), dist_comm := λ f g, by simp [dist_eq, dist_comm], dist_triangle := λ f g h, (dist_le (add_nonneg dist_nonneg' dist_nonneg')).2 $ λ x, le_trans (dist_triangle _ _ _) (add_le_add (dist_coe_le_dist _) (dist_coe_le_dist _)) } variable (α) /-- Constant as a continuous bounded function. -/ def const (b : β) : α →ᵇ β := ⟨λx, b, continuous_const, 0, by simp [le_refl]⟩ variable {α} @[simp] lemma coe_const (b : β) : ⇑(const α b) = function.const α b := rfl lemma const_apply (a : α) (b : β) : (const α b : α → β) a = b := rfl /-- If the target space is inhabited, so is the space of bounded continuous functions -/ instance [inhabited β] : inhabited (α →ᵇ β) := ⟨const α (default β)⟩ /-- The evaluation map is continuous, as a joint function of `u` and `x` -/ theorem continuous_eval : continuous (λ p : (α →ᵇ β) × α, p.1 p.2) := continuous_iff'.2 $ λ ⟨f, x⟩ ε ε0, /- use the continuity of `f` to find a neighborhood of `x` where it varies at most by ε/2 -/ have Hs : _ := continuous_iff'.1 f.2.1 x (ε/2) (half_pos ε0), mem_sets_of_superset (prod_mem_nhds_sets (ball_mem_nhds _ (half_pos ε0)) Hs) $ λ ⟨g, y⟩ ⟨hg, hy⟩, calc dist (g y) (f x) ≤ dist (g y) (f y) + dist (f y) (f x) : dist_triangle _ _ _ ... < ε/2 + ε/2 : add_lt_add (lt_of_le_of_lt (dist_coe_le_dist _) hg) hy ... = ε : add_halves _ /-- In particular, when `x` is fixed, `f → f x` is continuous -/ theorem continuous_evalx {x : α} : continuous (λ f : α →ᵇ β, f x) := continuous_eval.comp (continuous_id.prod_mk continuous_const) /-- When `f` is fixed, `x → f x` is also continuous, by definition -/ theorem continuous_evalf {f : α →ᵇ β} : continuous f := f.2.1 /-- Bounded continuous functions taking values in a complete space form a complete space. -/ instance [complete_space β] : complete_space (α →ᵇ β) := complete_of_cauchy_seq_tendsto $ λ (f : ℕ → α →ᵇ β) (hf : cauchy_seq f), begin /- We have to show that `f n` converges to a bounded continuous function. For this, we prove pointwise convergence to define the limit, then check it is a continuous bounded function, and then check the norm convergence. -/ rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, b0, b_bound, b_lim⟩, have f_bdd := λx n m N hn hm, le_trans (dist_coe_le_dist x) (b_bound n m N hn hm), have fx_cau : ∀x, cauchy_seq (λn, f n x) := λx, cauchy_seq_iff_le_tendsto_0.2 ⟨b, b0, f_bdd x, b_lim⟩, choose F hF using λx, cauchy_seq_tendsto_of_complete (fx_cau x), /- F : α → β, hF : ∀ (x : α), tendsto (λ (n : ℕ), f n x) at_top (𝓝 (F x)) `F` is the desired limit function. Check that it is uniformly approximated by `f N` -/ have fF_bdd : ∀x N, dist (f N x) (F x) ≤ b N := λ x N, le_of_tendsto (by simp) (tendsto_const_nhds.dist (hF x)) (filter.eventually_at_top.2 ⟨N, λn hn, f_bdd x N n N (le_refl N) hn⟩), refine ⟨⟨F, _, _⟩, _⟩, { /- Check that `F` is continuous, as a uniform limit of continuous functions -/ have : tendsto_uniformly (λn x, f n x) F at_top, { refine metric.tendsto_uniformly_iff.2 (λ ε ε0, _), refine ((tendsto_order.1 b_lim).2 ε ε0).mono (λ n hn x, _), rw dist_comm, exact lt_of_le_of_lt (fF_bdd x n) hn }, exact this.continuous (λN, (f N).2.1) at_top_ne_bot }, { /- Check that `F` is bounded -/ rcases (f 0).2.2 with ⟨C, hC⟩, exact ⟨C + (b 0 + b 0), λ x y, calc dist (F x) (F y) ≤ dist (f 0 x) (f 0 y) + (dist (f 0 x) (F x) + dist (f 0 y) (F y)) : dist_triangle4_left _ _ _ _ ... ≤ C + (b 0 + b 0) : add_le_add (hC x y) (add_le_add (fF_bdd x 0) (fF_bdd y 0))⟩ }, { /- Check that `F` is close to `f N` in distance terms -/ refine tendsto_iff_dist_tendsto_zero.2 (squeeze_zero (λ _, dist_nonneg) _ b_lim), exact λ N, (dist_le (b0 _)).2 (λx, fF_bdd x N) } end /-- Composition (in the target) of a bounded continuous function with a Lipschitz map again gives a bounded continuous function -/ def comp (G : β → γ) {C : nnreal} (H : lipschitz_with C G) (f : α →ᵇ β) : α →ᵇ γ := ⟨λx, G (f x), H.continuous.comp f.2.1, let ⟨D, hD⟩ := f.2.2 in ⟨max C 0 * D, λ x y, calc dist (G (f x)) (G (f y)) ≤ C * dist (f x) (f y) : H.dist_le_mul _ _ ... ≤ max C 0 * dist (f x) (f y) : mul_le_mul_of_nonneg_right (le_max_left C 0) dist_nonneg ... ≤ max C 0 * D : mul_le_mul_of_nonneg_left (hD _ _) (le_max_right C 0)⟩⟩ /-- The composition operator (in the target) with a Lipschitz map is Lipschitz -/ lemma lipschitz_comp {G : β → γ} {C : nnreal} (H : lipschitz_with C G) : lipschitz_with C (comp G H : (α →ᵇ β) → α →ᵇ γ) := lipschitz_with.of_dist_le_mul $ λ f g, (dist_le (mul_nonneg C.2 dist_nonneg)).2 $ λ x, calc dist (G (f x)) (G (g x)) ≤ C * dist (f x) (g x) : H.dist_le_mul _ _ ... ≤ C * dist f g : mul_le_mul_of_nonneg_left (dist_coe_le_dist _) C.2 /-- The composition operator (in the target) with a Lipschitz map is uniformly continuous -/ lemma uniform_continuous_comp {G : β → γ} {C : nnreal} (H : lipschitz_with C G) : uniform_continuous (comp G H : (α →ᵇ β) → α →ᵇ γ) := (lipschitz_comp H).uniform_continuous /-- The composition operator (in the target) with a Lipschitz map is continuous -/ lemma continuous_comp {G : β → γ} {C : nnreal} (H : lipschitz_with C G) : continuous (comp G H : (α →ᵇ β) → α →ᵇ γ) := (lipschitz_comp H).continuous /-- Restriction (in the target) of a bounded continuous function taking values in a subset -/ def cod_restrict (s : set β) (f : α →ᵇ β) (H : ∀x, f x ∈ s) : α →ᵇ s := ⟨s.cod_restrict f H, continuous_subtype_mk _ f.2.1, f.2.2⟩ end basics section arzela_ascoli variables [topological_space α] [compact_space α] [metric_space β] variables {f g : α →ᵇ β} {x : α} {C : ℝ} /- Arzela-Ascoli theorem asserts that, on a compact space, a set of functions sharing a common modulus of continuity and taking values in a compact set forms a compact subset for the topology of uniform convergence. In this section, we prove this theorem and several useful variations around it. -/ /-- First version, with pointwise equicontinuity and range in a compact space -/ theorem arzela_ascoli₁ [compact_space β] (A : set (α →ᵇ β)) (closed : is_closed A) (H : ∀ (x:α) (ε > 0), ∃U ∈ 𝓝 x, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε) : is_compact A := begin refine compact_of_totally_bounded_is_closed _ closed, refine totally_bounded_of_finite_discretization (λ ε ε0, _), rcases dense ε0 with ⟨ε₁, ε₁0, εε₁⟩, let ε₂ := ε₁/2/2, /- We have to find a finite discretization of `u`, i.e., finite information that is sufficient to reconstruct `u` up to ε. This information will be provided by the values of `u` on a sufficiently dense set tα, slightly translated to fit in a finite ε₂-dense set tβ in the image. Such sets exist by compactness of the source and range. Then, to check that these data determine the function up to ε, one uses the control on the modulus of continuity to extend the closeness on tα to closeness everywhere. -/ have ε₂0 : ε₂ > 0 := half_pos (half_pos ε₁0), have : ∀x:α, ∃U, x ∈ U ∧ is_open U ∧ ∀ (y z ∈ U) {f : α →ᵇ β}, f ∈ A → dist (f y) (f z) < ε₂ := λ x, let ⟨U, nhdsU, hU⟩ := H x _ ε₂0, ⟨V, VU, openV, xV⟩ := mem_nhds_sets_iff.1 nhdsU in ⟨V, xV, openV, λy z hy hz f hf, hU y z (VU hy) (VU hz) f hf⟩, choose U hU using this, /- For all x, the set hU x is an open set containing x on which the elements of A fluctuate by at most ε₂. We extract finitely many of these sets that cover the whole space, by compactness -/ rcases compact_univ.elim_finite_subcover_image (λx _, (hU x).2.1) (λx hx, mem_bUnion (mem_univ _) (hU x).1) with ⟨tα, _, ⟨_⟩, htα⟩, /- tα : set α, htα : univ ⊆ ⋃x ∈ tα, U x -/ rcases @finite_cover_balls_of_compact β _ _ compact_univ _ ε₂0 with ⟨tβ, _, ⟨_⟩, htβ⟩, resetI, /- tβ : set β, htβ : univ ⊆ ⋃y ∈ tβ, ball y ε₂ -/ /- Associate to every point `y` in the space a nearby point `F y` in tβ -/ choose F hF using λy, show ∃z∈tβ, dist y z < ε₂, by simpa using htβ (mem_univ y), /- F : β → β, hF : ∀ (y : β), F y ∈ tβ ∧ dist y (F y) < ε₂ -/ /- Associate to every function a discrete approximation, mapping each point in `tα` to a point in `tβ` close to its true image by the function. -/ refine ⟨tα → tβ, by apply_instance, λ f a, ⟨F (f a), (hF (f a)).1⟩, _⟩, rintro ⟨f, hf⟩ ⟨g, hg⟩ f_eq_g, /- If two functions have the same approximation, then they are within distance ε -/ refine lt_of_le_of_lt ((dist_le $ le_of_lt ε₁0).2 (λ x, _)) εε₁, obtain ⟨x', x'tα, hx'⟩ : ∃x' ∈ tα, x ∈ U x' := mem_bUnion_iff.1 (htα (mem_univ x)), refine calc dist (f x) (g x) ≤ dist (f x) (f x') + dist (g x) (g x') + dist (f x') (g x') : dist_triangle4_right _ _ _ _ ... ≤ ε₂ + ε₂ + ε₁/2 : le_of_lt (add_lt_add (add_lt_add _ _) _) ... = ε₁ : by rw [add_halves, add_halves], { exact (hU x').2.2 _ _ hx' ((hU x').1) hf }, { exact (hU x').2.2 _ _ hx' ((hU x').1) hg }, { have F_f_g : F (f x') = F (g x') := (congr_arg (λ f:tα → tβ, (f ⟨x', x'tα⟩ : β)) f_eq_g : _), calc dist (f x') (g x') ≤ dist (f x') (F (f x')) + dist (g x') (F (f x')) : dist_triangle_right _ _ _ ... = dist (f x') (F (f x')) + dist (g x') (F (g x')) : by rw F_f_g ... < ε₂ + ε₂ : add_lt_add (hF (f x')).2 (hF (g x')).2 ... = ε₁/2 : add_halves _ } end /-- Second version, with pointwise equicontinuity and range in a compact subset -/ theorem arzela_ascoli₂ (s : set β) (hs : is_compact s) (A : set (α →ᵇ β)) (closed : is_closed A) (in_s : ∀(f : α →ᵇ β) (x : α), f ∈ A → f x ∈ s) (H : ∀(x:α) (ε > 0), ∃U ∈ 𝓝 x, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε) : is_compact A := /- This version is deduced from the previous one by restricting to the compact type in the target, using compactness there and then lifting everything to the original space. -/ begin have M : lipschitz_with 1 coe := lipschitz_with.subtype_coe s, let F : (α →ᵇ s) → α →ᵇ β := comp coe M, refine compact_of_is_closed_subset ((_ : is_compact (F ⁻¹' A)).image (continuous_comp M)) closed (λ f hf, _), { haveI : compact_space s := compact_iff_compact_space.1 hs, refine arzela_ascoli₁ _ (continuous_iff_is_closed.1 (continuous_comp M) _ closed) (λ x ε ε0, bex.imp_right (λ U U_nhds hU y z hy hz f hf, _) (H x ε ε0)), calc dist (f y) (f z) = dist (F f y) (F f z) : rfl ... < ε : hU y z hy hz (F f) hf }, { let g := cod_restrict s f (λx, in_s f x hf), rw [show f = F g, by ext; refl] at hf ⊢, exact ⟨g, hf, rfl⟩ } end /-- Third (main) version, with pointwise equicontinuity and range in a compact subset, but without closedness. The closure is then compact -/ theorem arzela_ascoli (s : set β) (hs : is_compact s) (A : set (α →ᵇ β)) (in_s : ∀(f : α →ᵇ β) (x : α), f ∈ A → f x ∈ s) (H : ∀(x:α) (ε > 0), ∃U ∈ 𝓝 x, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε) : is_compact (closure A) := /- This version is deduced from the previous one by checking that the closure of A, in addition to being closed, still satisfies the properties of compact range and equicontinuity -/ arzela_ascoli₂ s hs (closure A) is_closed_closure (λ f x hf, (mem_of_closed' hs.is_closed).2 $ λ ε ε0, let ⟨g, gA, dist_fg⟩ := metric.mem_closure_iff.1 hf ε ε0 in ⟨g x, in_s g x gA, lt_of_le_of_lt (dist_coe_le_dist _) dist_fg⟩) (λ x ε ε0, show ∃ U ∈ 𝓝 x, ∀ y z ∈ U, ∀ (f : α →ᵇ β), f ∈ closure A → dist (f y) (f z) < ε, begin refine bex.imp_right (λ U U_set hU y z hy hz f hf, _) (H x (ε/2) (half_pos ε0)), rcases metric.mem_closure_iff.1 hf (ε/2/2) (half_pos (half_pos ε0)) with ⟨g, gA, dist_fg⟩, replace dist_fg := λ x, lt_of_le_of_lt (dist_coe_le_dist x) dist_fg, calc dist (f y) (f z) ≤ dist (f y) (g y) + dist (f z) (g z) + dist (g y) (g z) : dist_triangle4_right _ _ _ _ ... < ε/2/2 + ε/2/2 + ε/2 : add_lt_add (add_lt_add (dist_fg y) (dist_fg z)) (hU y z hy hz g gA) ... = ε : by rw [add_halves, add_halves] end) /- To apply the previous theorems, one needs to check the equicontinuity. An important instance is when the source space is a metric space, and there is a fixed modulus of continuity for all the functions in the set A -/ lemma equicontinuous_of_continuity_modulus {α : Type u} [metric_space α] (b : ℝ → ℝ) (b_lim : tendsto b (𝓝 0) (𝓝 0)) (A : set (α →ᵇ β)) (H : ∀(x y:α) (f : α →ᵇ β), f ∈ A → dist (f x) (f y) ≤ b (dist x y)) (x:α) (ε : ℝ) (ε0 : ε > 0) : ∃U ∈ 𝓝 x, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε := begin rcases tendsto_nhds_nhds.1 b_lim ε ε0 with ⟨δ, δ0, hδ⟩, refine ⟨ball x (δ/2), ball_mem_nhds x (half_pos δ0), λ y z hy hz f hf, _⟩, have : dist y z < δ := calc dist y z ≤ dist y x + dist z x : dist_triangle_right _ _ _ ... < δ/2 + δ/2 : add_lt_add hy hz ... = δ : add_halves _, calc dist (f y) (f z) ≤ b (dist y z) : H y z f hf ... ≤ abs (b (dist y z)) : le_abs_self _ ... = dist (b (dist y z)) 0 : by simp [real.dist_eq] ... < ε : hδ (by simpa [real.dist_eq] using this), end end arzela_ascoli section normed_group /- In this section, if β is a normed group, then we show that the space of bounded continuous functions from α to β inherits a normed group structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables [topological_space α] [normed_group β] variables (f g : α →ᵇ β) {x : α} {C : ℝ} instance : has_zero (α →ᵇ β) := ⟨const α 0⟩ @[simp] lemma coe_zero : (0 : α →ᵇ β) x = 0 := rfl instance : has_norm (α →ᵇ β) := ⟨λu, dist u 0⟩ lemma norm_def : ∥f∥ = dist f 0 := rfl /-- The norm of a bounded continuous function is the supremum of `∥f x∥`. We use `Inf` to ensure that the definition works if `α` has no elements. -/ lemma norm_eq (f : α →ᵇ β) : ∥f∥ = Inf {C : ℝ | 0 ≤ C ∧ ∀ (x : α), ∥f x∥ ≤ C} := by simp [norm_def, bounded_continuous_function.dist_eq] lemma norm_coe_le_norm (x : α) : ∥f x∥ ≤ ∥f∥ := calc ∥f x∥ = dist (f x) ((0 : α →ᵇ β) x) : by simp [dist_zero_right] ... ≤ ∥f∥ : dist_coe_le_dist _ lemma dist_le_two_norm' {f : γ → β} {C : ℝ} (hC : ∀ x, ∥f x∥ ≤ C) (x y : γ) : dist (f x) (f y) ≤ 2 * C := calc dist (f x) (f y) ≤ ∥f x∥ + ∥f y∥ : dist_le_norm_add_norm _ _ ... ≤ C + C : add_le_add (hC x) (hC y) ... = 2 * C : (two_mul _).symm /-- 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∥ := dist_le_two_norm' f.norm_coe_le_norm x y variable {f} /-- The norm of a function is controlled by the supremum of the pointwise norms -/ lemma norm_le (C0 : (0 : ℝ) ≤ C) : ∥f∥ ≤ C ↔ ∀x:α, ∥f x∥ ≤ C := by simpa only [coe_zero, dist_zero_right] using @dist_le _ _ _ _ f 0 _ C0 variable (f) /-- Norm of `const α b` is less than or equal to `∥b∥`. If `α` is nonempty, then it is equal to `∥b∥`. -/ lemma norm_const_le (b : β) : ∥const α b∥ ≤ ∥b∥ := (norm_le (norm_nonneg b)).2 $ λ x, le_refl _ @[simp] lemma norm_const_eq [h : nonempty α] (b : β) : ∥const α b∥ = ∥b∥ := le_antisymm (norm_const_le b) $ h.elim $ λ x, (const α b).norm_coe_le_norm x /-- Constructing a bounded continuous function from a uniformly bounded continuous function taking values in a normed group. -/ def of_normed_group {α : Type u} {β : Type v} [topological_space α] [normed_group β] (f : α → β) (Hf : continuous f) (C : ℝ) (H : ∀x, ∥f x∥ ≤ C) : α →ᵇ β := ⟨λn, f n, ⟨Hf, ⟨_, dist_le_two_norm' H⟩⟩⟩ lemma norm_of_normed_group_le {f : α → β} (hfc : continuous f) {C : ℝ} (hC : 0 ≤ C) (hfC : ∀ x, ∥f x∥ ≤ C) : ∥of_normed_group f hfc C hfC∥ ≤ C := (norm_le hC).2 hfC /-- Constructing a bounded continuous function from a uniformly bounded function on a discrete space, taking values in a normed group -/ def of_normed_group_discrete {α : Type u} {β : Type v} [topological_space α] [discrete_topology α] [normed_group β] (f : α → β) (C : ℝ) (H : ∀x, norm (f x) ≤ C) : α →ᵇ β := of_normed_group f continuous_of_discrete_topology C H /-- The pointwise sum of two bounded continuous functions is again bounded continuous. -/ instance : has_add (α →ᵇ β) := ⟨λf g, of_normed_group (f + g) (f.2.1.add g.2.1) (∥f∥ + ∥g∥) $ λ x, le_trans (norm_add_le _ _) (add_le_add (f.norm_coe_le_norm x) (g.norm_coe_le_norm x))⟩ /-- The pointwise opposite of a bounded continuous function is again bounded continuous. -/ instance : has_neg (α →ᵇ β) := ⟨λf, of_normed_group (-f) f.2.1.neg ∥f∥ $ λ x, trans_rel_right _ (norm_neg _) (f.norm_coe_le_norm x)⟩ @[simp] lemma coe_add : ⇑(f + g) = λ x, f x + g x := rfl lemma add_apply : (f + g) x = f x + g x := rfl @[simp] lemma coe_neg : ⇑(-f) = λ x, - f x := rfl lemma neg_apply : (-f) x = -f x := rfl lemma forall_coe_zero_iff_zero : (∀x, f x = 0) ↔ f = 0 := (@ext_iff _ _ _ _ f 0).symm instance : add_comm_group (α →ᵇ β) := { add_assoc := assume f g h, by ext; simp [add_assoc], zero_add := assume f, by ext; simp, add_zero := assume f, by ext; simp, add_left_neg := assume f, by ext; simp, add_comm := assume f g, by ext; simp [add_comm], ..bounded_continuous_function.has_add, ..bounded_continuous_function.has_neg, ..bounded_continuous_function.has_zero } @[simp] lemma coe_sub : ⇑(f - g) = λ x, f x - g x := rfl lemma sub_apply : (f - g) x = f x - g x := rfl instance : normed_group (α →ᵇ β) := { dist_eq := λ f g, by simp only [norm_eq, dist_eq, dist_eq_norm, sub_apply] } lemma abs_diff_coe_le_dist : ∥f x - g x∥ ≤ dist f g := by { rw dist_eq_norm, exact (f - g).norm_coe_le_norm x } lemma coe_le_coe_add_dist {f g : α →ᵇ ℝ} : f x ≤ g x + dist f g := sub_le_iff_le_add'.1 $ (abs_le.1 $ @dist_coe_le_dist _ _ _ _ f g x).2 end normed_group section normed_space /-! ### Normed space structure In this section, if `β` is a normed space, then we show that the space of bounded continuous functions from `α` to `β` inherits a normed space structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables {𝕜 : Type*} [normed_field 𝕜] variables [topological_space α] [normed_group β] [normed_space 𝕜 β] variables {f g : α →ᵇ β} {x : α} {C : ℝ} instance : has_scalar 𝕜 (α →ᵇ β) := ⟨λ c f, of_normed_group (c • f) (continuous_const.smul f.2.1) (∥c∥ * ∥f∥) $ λ x, trans_rel_right _ (norm_smul _ _) (mul_le_mul_of_nonneg_left (f.norm_coe_le_norm _) (norm_nonneg _))⟩ @[simp] lemma coe_smul (c : 𝕜) (f : α →ᵇ β) : ⇑(c • f) = λ x, c • (f x) := rfl lemma smul_apply (c : 𝕜) (f : α →ᵇ β) (x : α) : (c • f) x = c • f x := rfl instance : semimodule 𝕜 (α →ᵇ β) := semimodule.of_core $ { smul := (•), smul_add := λ c f g, ext $ λ x, smul_add c (f x) (g x), add_smul := λ c₁ c₂ f, ext $ λ x, add_smul c₁ c₂ (f x), mul_smul := λ c₁ c₂ f, ext $ λ x, mul_smul c₁ c₂ (f x), one_smul := λ f, ext $ λ x, one_smul 𝕜 (f x) } instance : normed_space 𝕜 (α →ᵇ β) := ⟨λ c f, norm_of_normed_group_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _⟩ end normed_space section normed_ring /-! ### Normed ring structure In this section, if `R` is a normed ring, then we show that the space of bounded continuous functions from `α` to `R` inherits a normed ring structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables [topological_space α] {R : Type*} [normed_ring R] instance : ring (α →ᵇ R) := { one := const α 1, mul := λ f g, of_normed_group (f * g) (f.2.1.mul g.2.1) (∥f∥ * ∥g∥) $ λ x, le_trans (normed_ring.norm_mul (f x) (g x)) $ mul_le_mul (f.norm_coe_le_norm x) (g.norm_coe_le_norm x) (norm_nonneg _) (norm_nonneg _), one_mul := λ f, ext $ λ x, one_mul (f x), mul_one := λ f, ext $ λ x, mul_one (f x), mul_assoc := λ f₁ f₂ f₃, ext $ λ x, mul_assoc _ _ _, left_distrib := λ f₁ f₂ f₃, ext $ λ x, left_distrib _ _ _, right_distrib := λ f₁ f₂ f₃, ext $ λ x, right_distrib _ _ _, .. bounded_continuous_function.add_comm_group } instance : normed_ring (α →ᵇ R) := { norm_mul := λ f g, norm_of_normed_group_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _, .. bounded_continuous_function.normed_group } end normed_ring section normed_algebra /-! ### Normed algebra structure In this section, if `γ` is a normed algebra, then we show that the space of bounded continuous functions from `α` to `γ` inherits a normed algebra structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables {𝕜 : Type*} [normed_field 𝕜] variables [topological_space α] [normed_group β] [normed_space 𝕜 β] variables [normed_ring γ] [normed_algebra 𝕜 γ] variables {f g : α →ᵇ γ} {x : α} {c : 𝕜} /-- `bounded_continuous_function.const` as a `ring_hom`. -/ def C : 𝕜 →+* (α →ᵇ γ) := { to_fun := λ (c : 𝕜), const α ((algebra_map 𝕜 γ) c), map_one' := ext $ λ x, (algebra_map 𝕜 γ).map_one, map_mul' := λ c₁ c₂, ext $ λ x, (algebra_map 𝕜 γ).map_mul _ _, map_zero' := ext $ λ x, (algebra_map 𝕜 γ).map_zero, map_add' := λ c₁ c₂, ext $ λ x, (algebra_map 𝕜 γ).map_add _ _ } instance : algebra 𝕜 (α →ᵇ γ) := { to_ring_hom := C, commutes' := λ c f, ext $ λ x, algebra.commutes' _ _, smul_def' := λ c f, ext $ λ x, algebra.smul_def' _ _, ..bounded_continuous_function.semimodule, ..bounded_continuous_function.ring } instance [nonempty α] : normed_algebra 𝕜 (α →ᵇ γ) := { norm_algebra_map_eq := λ c, begin calc ∥ (algebra_map 𝕜 (α →ᵇ γ)).to_fun c∥ = ∥(algebra_map 𝕜 γ) c∥ : _ ... = ∥c∥ : norm_algebra_map_eq _ _, apply norm_const_eq ((algebra_map 𝕜 γ) c), assumption, end, ..bounded_continuous_function.algebra } /-! ### Structure as normed module over scalar functions If `β` is a normed `𝕜`-space, then we show that the space of bounded continuous functions from `α` to `β` is naturally a module over the algebra of bounded continuous functions from `α` to `𝕜`. -/ instance has_scalar' : has_scalar (α →ᵇ 𝕜) (α →ᵇ β) := ⟨λ (f : α →ᵇ 𝕜) (g : α →ᵇ β), of_normed_group (λ x, (f x) • (g x)) (continuous.smul f.2.1 g.2.1) (∥f∥ * ∥g∥) (λ x, calc ∥f x • g x∥ ≤ ∥f x∥ * ∥g x∥ : normed_space.norm_smul_le _ _ ... ≤ ∥f∥ * ∥g∥ : mul_le_mul (f.norm_coe_le_norm _) (g.norm_coe_le_norm _) (norm_nonneg _) (norm_nonneg _)) ⟩ instance module' : module (α →ᵇ 𝕜) (α →ᵇ β) := semimodule.of_core $ { smul := (•), smul_add := λ c f₁ f₂, ext $ λ x, smul_add _ _ _, add_smul := λ c₁ c₂ f, ext $ λ x, add_smul _ _ _, mul_smul := λ c₁ c₂ f, ext $ λ x, mul_smul _ _ _, one_smul := λ f, ext $ λ x, one_smul 𝕜 (f x) } lemma norm_smul_le (f : α →ᵇ 𝕜) (g : α →ᵇ β) : ∥f • g∥ ≤ ∥f∥ * ∥g∥ := norm_of_normed_group_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _ /- TODO: When `normed_module` has been added to `normed_space.basic`, the above facts show that the space of bounded continuous functions from `α` to `β` is naturally a normed module over the algebra of bounded continuous functions from `α` to `𝕜`. -/ end normed_algebra end bounded_continuous_function
c67032ca30f0eb35cfeb3a983793051ad5123eb3
fc086f79b20cf002d6f34b023749998408e94fbf
/test/terminal_goal.lean
9deab9da172703b5979244c2d8f4c7594b2ca80a
[]
no_license
semorrison/lean-tidy
f039460136b898fb282f75efedd92f2d5c5d90f8
6c1d46de6cff05e1c2c4c9692af812bca3e13b6c
refs/heads/master
1,624,461,332,392
1,559,655,744,000
1,559,655,744,000
96,569,994
9
4
null
1,538,287,895,000
1,499,455,306,000
Lean
UTF-8
Lean
false
false
1,453
lean
-- Copyright (c) 2018 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison import tactic.basic open tactic structure C := ( w : Type ) ( x : list w ) ( y : Type ) ( z : prod w y ) def test_terminal_goal : C := begin fsplit, success_if_fail { terminal_goal }, exact ℕ, terminal_goal, exact [], success_if_fail { terminal_goal }, exact bool, terminal_goal, exact (0, tt) end -- verifying that terminal_goal correctly considers all propositional goals as terminal? structure foo := (x : ℕ) (p : x = 0) open tactic lemma bar : ∃ F : foo, F = ⟨ 0, by refl ⟩ := begin split, swap, split, terminal_goal, swap, success_if_fail { terminal_goal }, exact 0, refl, refl, end structure D := ( w : ℕ → Type ) ( x : list (w 0) ) def test_terminal_goal' : D := begin split, swap, success_if_fail { terminal_goal }, intros, success_if_fail { terminal_goal }, exact ℕ, exact [] end def f : unit → Type := λ _, ℕ def test_terminal_goal'' : Σ x : unit, f x := begin split, terminal_goal, swap, terminal_goal, exact (), dsimp [f], exact 0 end def test_subsingleton_goal : 0 = 0 := begin subsingleton_goal, refl end def test_subsingleton_goal' : list ℕ := begin success_if_fail { subsingleton_goal }, exact [] end
44302b7a09b8f25ab51cb75688ec5bcd55382974
7c2dd01406c42053207061adb11703dc7ce0b5e5
/src/exercises/07bis_abstract_negations.lean
c4349b844f22679eef5f9b02b2cc39b2bad36794
[ "Apache-2.0" ]
permissive
leanprover-community/tutorials
50ec79564cbf2ad1afd1ac43d8ee3c592c2883a8
79a6872a755c4ae0c2aca57e1adfdac38b1d8bb1
refs/heads/master
1,687,466,144,386
1,672,061,276,000
1,672,061,276,000
189,169,918
186
81
Apache-2.0
1,686,350,300,000
1,559,113,678,000
Lean
UTF-8
Lean
false
false
1,228
lean
import data.real.basic open_locale classical /- Theoretical negations. This file is for people interested in logic who want to fully understand negations. Here we don't use `contrapose` or `push_neg`. The goal is to prove lemmas that are used by those tactics. Of course we can use `exfalso`, `by_contradiction` and `by_cases`. If this doesn't sound like fun then skip ahead to the next file. -/ section negation_prop variables P Q : Prop -- 0055 example : (P → Q) ↔ (¬ Q → ¬ P) := begin sorry end -- 0056 lemma non_imp (P Q : Prop) : ¬ (P → Q) ↔ P ∧ ¬ Q := begin sorry end -- In the next one, let's use the axiom -- propext {P Q : Prop} : (P ↔ Q) → P = Q -- 0057 example (P : Prop) : ¬ P ↔ P = false := begin sorry end end negation_prop section negation_quantifiers variables (X : Type) (P : X → Prop) -- 0058 example : ¬ (∀ x, P x) ↔ ∃ x, ¬ P x := begin sorry end -- 0059 example : ¬ (∃ x, P x) ↔ ∀ x, ¬ P x := begin sorry end -- 0060 example (P : ℝ → Prop) : ¬ (∃ ε > 0, P ε) ↔ ∀ ε > 0, ¬ P ε := begin sorry end -- 0061 example (P : ℝ → Prop) : ¬ (∀ x > 0, P x) ↔ ∃ x > 0, ¬ P x := begin sorry end end negation_quantifiers
a6ff8088aac8fc4337b0c0cf44f38bb72a018837
82e44445c70db0f03e30d7be725775f122d72f3e
/src/ring_theory/integral_domain.lean
f5aced71b5c6a69a769de7a678bca94610a80353
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
6,594
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Chris Hughes -/ import data.fintype.card import data.polynomial.ring_division import group_theory.specific_groups.cyclic import algebra.geom_sum /-! # Integral domains Assorted theorems about integral domains. ## Main theorems * `is_cyclic_of_subgroup_integral_domain` : A finite subgroup of the units of an integral domain is cyclic. * `field_of_integral_domain` : A finite integral domain is a field. ## Tags integral domain, finite integral domain, finite field -/ section open finset polynomial function open_locale big_operators nat variables {R : Type*} {G : Type*} [integral_domain R] [group G] [fintype G] lemma card_nth_roots_subgroup_units (f : G →* R) (hf : injective f) {n : ℕ} (hn : 0 < n) (g₀ : G) : ({g ∈ univ | g ^ n = g₀} : finset G).card ≤ (nth_roots n (f g₀)).card := begin haveI : decidable_eq R := classical.dec_eq _, refine le_trans _ (nth_roots n (f g₀)).to_finset_card_le, apply card_le_card_of_inj_on f, { intros g hg, rw [sep_def, mem_filter] at hg, rw [multiset.mem_to_finset, mem_nth_roots hn, ← f.map_pow, hg.2] }, { intros, apply hf, assumption } end /-- A finite subgroup of the unit group of an integral domain is cyclic. -/ lemma is_cyclic_of_subgroup_integral_domain (f : G →* R) (hf : injective f) : is_cyclic G := begin classical, apply is_cyclic_of_card_pow_eq_one_le, intros n hn, convert (le_trans (card_nth_roots_subgroup_units f hf hn 1) (card_nth_roots n (f 1))) end /-- The unit group of a finite integral domain is cyclic. -/ instance [fintype R] : is_cyclic (units R) := is_cyclic_of_subgroup_integral_domain (units.coe_hom R) $ units.ext /-- Every finite integral domain is a field. -/ def field_of_integral_domain [decidable_eq R] [fintype R] : field R := { inv := λ a, if h : a = 0 then 0 else fintype.bij_inv (show function.bijective (* a), from fintype.injective_iff_bijective.1 $ λ _ _, mul_right_cancel' h) 1, mul_inv_cancel := λ a ha, show a * dite _ _ _ = _, by rw [dif_neg ha, mul_comm]; exact fintype.right_inverse_bij_inv (show function.bijective (* a), from _) 1, inv_zero := dif_pos rfl, ..show integral_domain R, by apply_instance } section variables (S : subgroup (units R)) [fintype S] /-- A finite subgroup of the units of an integral domain is cyclic. -/ instance subgroup_units_cyclic : is_cyclic S := begin refine is_cyclic_of_subgroup_integral_domain ⟨(coe : S → R), _, _⟩ (units.ext.comp subtype.val_injective), { simp }, { intros, simp }, end end lemma card_fiber_eq_of_mem_range {H : Type*} [group H] [decidable_eq H] (f : G →* H) {x y : H} (hx : x ∈ set.range f) (hy : y ∈ set.range f) : (univ.filter $ λ g, f g = x).card = (univ.filter $ λ g, f g = y).card := begin rcases hx with ⟨x, rfl⟩, rcases hy with ⟨y, rfl⟩, refine card_congr (λ g _, g * x⁻¹ * y) _ _ (λ g hg, ⟨g * y⁻¹ * x, _⟩), { simp only [mem_filter, one_mul, monoid_hom.map_mul, mem_univ, mul_right_inv, eq_self_iff_true, monoid_hom.map_mul_inv, and_self, forall_true_iff] {contextual := tt} }, { simp only [mul_left_inj, imp_self, forall_2_true_iff], }, { simp only [true_and, mem_filter, mem_univ] at hg, simp only [hg, mem_filter, one_mul, monoid_hom.map_mul, mem_univ, mul_right_inv, eq_self_iff_true, exists_prop_of_true, monoid_hom.map_mul_inv, and_self, mul_inv_cancel_right, inv_mul_cancel_right], } end /-- In an integral domain, a sum indexed by a nontrivial homomorphism from a finite group is zero. -/ lemma sum_hom_units_eq_zero (f : G →* R) (hf : f ≠ 1) : ∑ g : G, f g = 0 := begin classical, obtain ⟨x, hx⟩ : ∃ x : monoid_hom.range f.to_hom_units, ∀ y : monoid_hom.range f.to_hom_units, y ∈ submonoid.powers x, from is_cyclic.exists_monoid_generator, have hx1 : x ≠ 1, { rintro rfl, apply hf, ext g, rw [monoid_hom.one_apply], cases hx ⟨f.to_hom_units g, g, rfl⟩ with n hn, rwa [subtype.ext_iff, units.ext_iff, subtype.coe_mk, monoid_hom.coe_to_hom_units, one_pow, eq_comm] at hn, }, replace hx1 : (x : R) - 1 ≠ 0, from λ h, hx1 (subtype.eq (units.ext (sub_eq_zero.1 h))), let c := (univ.filter (λ g, f.to_hom_units g = 1)).card, calc ∑ g : G, f g = ∑ g : G, f.to_hom_units g : rfl ... = ∑ u : units R in univ.image f.to_hom_units, (univ.filter (λ g, f.to_hom_units g = u)).card • u : sum_comp (coe : units R → R) f.to_hom_units ... = ∑ u : units R in univ.image f.to_hom_units, c • u : sum_congr rfl (λ u hu, congr_arg2 _ _ rfl) -- remaining goal 1, proven below ... = ∑ b : monoid_hom.range f.to_hom_units, c • ↑b : finset.sum_subtype _ (by simp ) _ ... = c • ∑ b : monoid_hom.range f.to_hom_units, (b : R) : smul_sum.symm ... = c • 0 : congr_arg2 _ rfl _ -- remaining goal 2, proven below ... = 0 : smul_zero _, { -- remaining goal 1 show (univ.filter (λ (g : G), f.to_hom_units g = u)).card = c, apply card_fiber_eq_of_mem_range f.to_hom_units, { simpa only [mem_image, mem_univ, exists_prop_of_true, set.mem_range] using hu, }, { exact ⟨1, f.to_hom_units.map_one⟩ } }, -- remaining goal 2 show ∑ b : monoid_hom.range f.to_hom_units, (b : R) = 0, calc ∑ b : monoid_hom.range f.to_hom_units, (b : R) = ∑ n in range (order_of x), x ^ n : eq.symm $ sum_bij (λ n _, x ^ n) (by simp only [mem_univ, forall_true_iff]) (by simp only [implies_true_iff, eq_self_iff_true, subgroup.coe_pow, units.coe_pow, coe_coe]) (λ m n hm hn, pow_injective_of_lt_order_of _ (by simpa only [mem_range] using hm) (by simpa only [mem_range] using hn)) (λ b hb, let ⟨n, hn⟩ := hx b in ⟨n % order_of x, mem_range.2 (nat.mod_lt _ (order_of_pos _)), by rw [← pow_eq_mod_order_of, hn]⟩) ... = 0 : _, rw [← mul_left_inj' hx1, zero_mul, ← geom_sum, geom_sum_mul, coe_coe], norm_cast, simp [pow_order_of_eq_one], end /-- In an integral domain, a sum indexed by a homomorphism from a finite group is zero, unless the homomorphism is trivial, in which case the sum is equal to the cardinality of the group. -/ lemma sum_hom_units (f : G →* R) [decidable (f = 1)] : ∑ g : G, f g = if f = 1 then fintype.card G else 0 := begin split_ifs with h h, { simp [h, card_univ] }, { exact sum_hom_units_eq_zero f h } end end
0b71c03a144ac08cfc927ab8b880f27f8d3383c4
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/deceq_vec.lean
bc9f46f29a85ccbe31868a3854cd42e78c5b7b54
[ "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
256
lean
import data.examples.vector open vector nat decidable variable A : Type definition foo [H : decidable_eq A] : ∀ {n : nat} (v₁ v₂ : vector A n), decidable (v₁ = v₂) | ⌞0⌟ [] [] := sorry | ⌞n+1⌟ (a::v₁) (b::v₂) := sorry
e957f3e4cd38b3fe2e2a58b3f49115f15100562f
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/list_vect_numerals.lean
167b98cd29950972b709bee2797c6e1e59acb1ad
[ "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
502
lean
import data.list data.examples.vector data.real open nat int real list vector variables n m : nat variables i j : int variables x y : real variables v : vector real 3 check [n, m] -- list nat check [n, i] -- list int check [i, n] -- list int check [i, n, x] -- list real check ([i, n, x, y] : vector _ _) -- vector of reals check (tail [i, n, x, y] = v) check [i, n, x] = v set_option pp.notation false set_option pp.full_names true check [i, n, x] = v check (tail [i, n, x, y] = v)
bbab6053135e846277c0c99830e5a76af07f360e
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/control/applicative.lean
c2f519392b7b83a6eab4a36489b78a3f8073dae5
[ "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,033
lean
/- Copyright (c) 2017 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import algebra.group.defs import control.functor /-! # `applicative` instances > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > https://github.com/leanprover-community/mathlib4/pull/798 > Any changes to this file require a corresponding PR to mathlib4. This file provides `applicative` instances for concrete functors: * `id` * `functor.comp` * `functor.const` * `functor.add_const` -/ universes u v w section lemmas open function variables {F : Type u → Type v} variables [applicative F] [is_lawful_applicative F] variables {α β γ σ : Type u} lemma applicative.map_seq_map (f : α → β → γ) (g : σ → β) (x : F α) (y : F σ) : (f <$> x) <*> (g <$> y) = (flip (∘) g ∘ f) <$> x <*> y := by simp [flip] with functor_norm lemma applicative.pure_seq_eq_map' (f : α → β) : (<*>) (pure f : F (α → β)) = (<$>) f := by ext; simp with functor_norm theorem applicative.ext {F} : ∀ {A1 : applicative F} {A2 : applicative F} [@is_lawful_applicative F A1] [@is_lawful_applicative F A2] (H1 : ∀ {α : Type u} (x : α), @has_pure.pure _ A1.to_has_pure _ x = @has_pure.pure _ A2.to_has_pure _ x) (H2 : ∀ {α β : Type u} (f : F (α → β)) (x : F α), @has_seq.seq _ A1.to_has_seq _ _ f x = @has_seq.seq _ A2.to_has_seq _ _ f x), A1 = A2 | {to_functor := F1, seq := s1, pure := p1, seq_left := sl1, seq_right := sr1} {to_functor := F2, seq := s2, pure := p2, seq_left := sl2, seq_right := sr2} L1 L2 H1 H2 := begin obtain rfl : @p1 = @p2, {funext α x, apply H1}, obtain rfl : @s1 = @s2, {funext α β f x, apply H2}, cases L1, cases L2, obtain rfl : F1 = F2, { resetI, apply functor.ext, intros, exact (L1_pure_seq_eq_map _ _).symm.trans (L2_pure_seq_eq_map _ _) }, congr; funext α β x y, { exact (L1_seq_left_eq _ _).trans (L2_seq_left_eq _ _).symm }, { exact (L1_seq_right_eq _ _).trans (L2_seq_right_eq _ _).symm } end end lemmas instance : is_comm_applicative id := by refine { .. }; intros; refl namespace functor namespace comp open function (hiding comp) open functor variables {F : Type u → Type w} {G : Type v → Type u} variables [applicative F] [applicative G] variables [is_lawful_applicative F] [is_lawful_applicative G] variables {α β γ : Type v} lemma map_pure (f : α → β) (x : α) : (f <$> pure x : comp F G β) = pure (f x) := comp.ext $ by simp lemma seq_pure (f : comp F G (α → β)) (x : α) : f <*> pure x = (λ g : α → β, g x) <$> f := comp.ext $ by simp [(∘)] with functor_norm lemma seq_assoc (x : comp F G α) (f : comp F G (α → β)) (g : comp F G (β → γ)) : g <*> (f <*> x) = (@function.comp α β γ <$> g) <*> f <*> x := comp.ext $ by simp [(∘)] with functor_norm lemma pure_seq_eq_map (f : α → β) (x : comp F G α) : pure f <*> x = f <$> x := comp.ext $ by simp [applicative.pure_seq_eq_map'] with functor_norm instance : is_lawful_applicative (comp F G) := { pure_seq_eq_map := @comp.pure_seq_eq_map F G _ _ _ _, map_pure := @comp.map_pure F G _ _ _ _, seq_pure := @comp.seq_pure F G _ _ _ _, seq_assoc := @comp.seq_assoc F G _ _ _ _ } theorem applicative_id_comp {F} [AF : applicative F] [LF : is_lawful_applicative F] : @comp.applicative id F _ _ = AF := @applicative.ext F _ _ (@comp.is_lawful_applicative id F _ _ _ _) _ (λ α x, rfl) (λ α β f x, rfl) theorem applicative_comp_id {F} [AF : applicative F] [LF : is_lawful_applicative F] : @comp.applicative F id _ _ = AF := @applicative.ext F _ _ (@comp.is_lawful_applicative F id _ _ _ _) _ (λ α x, rfl) (λ α β f x, show id <$> f <*> x = f <*> x, by rw id_map) open is_comm_applicative instance {f : Type u → Type w} {g : Type v → Type u} [applicative f] [applicative g] [is_comm_applicative f] [is_comm_applicative g] : is_comm_applicative (comp f g) := by { refine { .. @comp.is_lawful_applicative f g _ _ _ _, .. }, intros, casesm* comp _ _ _, simp! [map,has_seq.seq] with functor_norm, rw [commutative_map], simp [comp.mk,flip,(∘)] with functor_norm, congr, funext, rw [commutative_map], congr } end comp end functor open functor @[functor_norm] lemma comp.seq_mk {α β : Type w} {f : Type u → Type v} {g : Type w → Type u} [applicative f] [applicative g] (h : f (g (α → β))) (x : f (g α)) : comp.mk h <*> comp.mk x = comp.mk (has_seq.seq <$> h <*> x) := rfl instance {α} [has_one α] [has_mul α] : applicative (const α) := { pure := λ β x, (1 : α), seq := λ β γ f x, (f * x : α) } instance {α} [monoid α] : is_lawful_applicative (const α) := by refine { .. }; intros; simp [mul_assoc, (<$>), (<*>), pure] instance {α} [has_zero α] [has_add α] : applicative (add_const α) := { pure := λ β x, (0 : α), seq := λ β γ f x, (f + x : α) } instance {α} [add_monoid α] : is_lawful_applicative (add_const α) := by refine { .. }; intros; simp [add_assoc, (<$>), (<*>), pure]
2d52a6efb960d4ca37faaee202b1b0a1afe5ee6b
19cc34575500ee2e3d4586c15544632aa07a8e66
/src/measure_theory/l1_space.lean
f6e4fc5f4375af3cd3f95671c97b9d621af329dd
[ "Apache-2.0" ]
permissive
LibertasSpZ/mathlib
b9fcd46625eb940611adb5e719a4b554138dade6
33f7870a49d7cc06d2f3036e22543e6ec5046e68
refs/heads/master
1,672,066,539,347
1,602,429,158,000
1,602,429,158,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
39,940
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import measure_theory.ae_eq_fun /-! # Integrable functions and `L¹` space In the first part of this file, the predicate `integrable` is defined and basic properties of integrable functions are proved. In the second part, the space `L¹` of equivalence classes of integrable functions under the relation of being almost everywhere equal is defined as a subspace of the space `L⁰`. See the file `src/measure_theory/ae_eq_fun.lean` for information on `L⁰` space. ## Notation * `α →₁ β` is the type of `L¹` space, where `α` is a `measure_space` and `β` is a `normed_group` with a `second_countable_topology`. `f : α →ₘ β` is a "function" in `L¹`. In comments, `[f]` is also used to denote an `L¹` function. `₁` can be typed as `\1`. ## Main definitions * Let `f : α → β` be a function, where `α` is a `measure_space` and `β` a `normed_group`. Then `has_finite_integral f` means `(∫⁻ a, nnnorm (f a)) < ⊤`. * If `β` is moreover a `measurable_space` then `f` is called `integrable` if `f` is `measurable` and `has_finite_integral f` holds. * The space `L¹` is defined as a subspace of `L⁰` : An `ae_eq_fun` `[f] : α →ₘ β` is in the space `L¹` if `edist [f] 0 < ⊤`, which means `(∫⁻ a, edist (f a) 0) < ⊤` if we expand the definition of `edist` in `L⁰`. ## Main statements `L¹`, as a subspace, inherits most of the structures of `L⁰`. ## Implementation notes Maybe `integrable f` should be mean `(∫⁻ a, edist (f a) 0) < ⊤`, so that `integrable` and `ae_eq_fun.integrable` are more aligned. But in the end one can use the lemma `lintegral_nnnorm_eq_lintegral_edist : (∫⁻ a, nnnorm (f a)) = (∫⁻ a, edist (f a) 0)` to switch the two forms. To prove something for an arbitrary integrable + measurable function, a useful theorem is `integrable.induction` in the file `set_integral`. ## Tags integrable, function space, l1 -/ noncomputable theory open_locale classical topological_space big_operators open set filter topological_space ennreal emetric measure_theory variables {α β γ δ : Type*} [measurable_space α] {μ ν : measure α} variables [normed_group β] variables [normed_group γ] namespace measure_theory /-! ### Some results about the Lebesgue integral involving a normed group -/ lemma lintegral_nnnorm_eq_lintegral_edist (f : α → β) : ∫⁻ a, nnnorm (f a) ∂μ = ∫⁻ a, edist (f a) 0 ∂μ := by simp only [edist_eq_coe_nnnorm] lemma lintegral_norm_eq_lintegral_edist (f : α → β) : ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ = ∫⁻ a, edist (f a) 0 ∂μ := by simp only [of_real_norm_eq_coe_nnnorm, edist_eq_coe_nnnorm] lemma lintegral_edist_triangle [second_countable_topology β] [measurable_space β] [opens_measurable_space β] {f g h : α → β} (hf : measurable f) (hg : measurable g) (hh : measurable h) : ∫⁻ a, edist (f a) (g a) ∂μ ≤ ∫⁻ a, edist (f a) (h a) ∂μ + ∫⁻ a, edist (g a) (h a) ∂μ := begin rw ← lintegral_add (hf.edist hh) (hg.edist hh), refine lintegral_mono (λ a, _), apply edist_triangle_right end lemma lintegral_nnnorm_zero : ∫⁻ a : α, nnnorm (0 : β) ∂μ = 0 := by simp lemma lintegral_nnnorm_add [measurable_space β] [opens_measurable_space β] [measurable_space γ] [opens_measurable_space γ] {f : α → β} {g : α → γ} (hf : measurable f) (hg : measurable g) : ∫⁻ a, nnnorm (f a) + nnnorm (g a) ∂μ = ∫⁻ a, nnnorm (f a) ∂μ + ∫⁻ a, nnnorm (g a) ∂μ := lintegral_add hf.ennnorm hg.ennnorm lemma lintegral_nnnorm_neg {f : α → β} : ∫⁻ a, nnnorm ((-f) a) ∂μ = ∫⁻ a, nnnorm (f a) ∂μ := by simp only [pi.neg_apply, nnnorm_neg] /-! ### The predicate `has_finite_integral` -/ /-- `has_finite_integral f μ` means that the integral `∫⁻ a, ∥f a∥ ∂μ` is finite. `has_finite_integral f` means `has_finite_integral f volume`. -/ def has_finite_integral (f : α → β) (μ : measure α . volume_tac) : Prop := ∫⁻ a, nnnorm (f a) ∂μ < ⊤ lemma has_finite_integral_iff_norm (f : α → β) : has_finite_integral f μ ↔ ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ < ⊤ := by simp only [has_finite_integral, of_real_norm_eq_coe_nnnorm] lemma has_finite_integral_iff_edist (f : α → β) : has_finite_integral f μ ↔ ∫⁻ a, edist (f a) 0 ∂μ < ⊤ := by simp only [has_finite_integral_iff_norm, edist_dist, dist_zero_right] lemma has_finite_integral_iff_of_real {f : α → ℝ} (h : 0 ≤ᵐ[μ] f) : has_finite_integral f μ ↔ ∫⁻ a, ennreal.of_real (f a) ∂μ < ⊤ := have lintegral_eq : ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ = ∫⁻ a, ennreal.of_real (f a) ∂μ := begin refine lintegral_congr_ae (h.mono $ λ a h, _), rwa [real.norm_eq_abs, abs_of_nonneg] end, by rw [has_finite_integral_iff_norm, lintegral_eq] lemma has_finite_integral.mono {f : α → β} {g : α → γ} (hg : has_finite_integral g μ) (h : ∀ᵐ a ∂μ, ∥f a∥ ≤ ∥g a∥) : has_finite_integral f μ := begin simp only [has_finite_integral_iff_norm] at *, calc ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ ≤ ∫⁻ (a : α), (ennreal.of_real ∥g a∥) ∂μ : lintegral_mono_ae (h.mono $ assume a h, of_real_le_of_real h) ... < ⊤ : hg end lemma has_finite_integral.mono' {f : α → β} {g : α → ℝ} (hg : has_finite_integral g μ) (h : ∀ᵐ a ∂μ, ∥f a∥ ≤ g a) : has_finite_integral f μ := hg.mono $ h.mono $ λ x hx, le_trans hx (le_abs_self _) lemma has_finite_integral.congr' {f : α → β} {g : α → γ} (hf : has_finite_integral f μ) (h : ∀ᵐ a ∂μ, ∥f a∥ = ∥g a∥) : has_finite_integral g μ := hf.mono $ eventually_eq.le $ eventually_eq.symm h lemma has_finite_integral_congr' {f : α → β} {g : α → γ} (h : ∀ᵐ a ∂μ, ∥f a∥ = ∥g a∥) : has_finite_integral f μ ↔ has_finite_integral g μ := ⟨λ hf, hf.congr' h, λ hg, hg.congr' $ eventually_eq.symm h⟩ lemma has_finite_integral.congr {f g : α → β} (hf : has_finite_integral f μ) (h : f =ᵐ[μ] g) : has_finite_integral g μ := hf.congr' $ h.fun_comp norm lemma has_finite_integral_congr {f g : α → β} (h : f =ᵐ[μ] g) : has_finite_integral f μ ↔ has_finite_integral g μ := has_finite_integral_congr' $ h.fun_comp norm lemma has_finite_integral_const_iff {c : β} : has_finite_integral (λ x : α, c) μ ↔ c = 0 ∨ μ univ < ⊤ := begin simp only [has_finite_integral, lintegral_const], by_cases hc : c = 0, { simp [hc] }, { simp only [hc, false_or], refine ⟨λ h, _, λ h, mul_lt_top coe_lt_top h⟩, replace h := mul_lt_top (@coe_lt_top $ (nnnorm c)⁻¹) h, rwa [← mul_assoc, ← coe_mul, _root_.inv_mul_cancel, coe_one, one_mul] at h, rwa [ne.def, nnnorm_eq_zero] } end lemma has_finite_integral_const [finite_measure μ] (c : β) : has_finite_integral (λ x : α, c) μ := has_finite_integral_const_iff.2 (or.inr $ measure_lt_top _ _) lemma has_finite_integral_of_bounded [finite_measure μ] {f : α → β} {C : ℝ} (hC : ∀ᵐ a ∂μ, ∥f a∥ ≤ C) : has_finite_integral f μ := (has_finite_integral_const C).mono' hC lemma has_finite_integral.mono_measure {f : α → β} (h : has_finite_integral f ν) (hμ : μ ≤ ν) : has_finite_integral f μ := lt_of_le_of_lt (lintegral_mono' hμ (le_refl _)) h lemma has_finite_integral.add_measure {f : α → β} (hμ : has_finite_integral f μ) (hν : has_finite_integral f ν) : has_finite_integral f (μ + ν) := begin simp only [has_finite_integral, lintegral_add_measure] at *, exact add_lt_top.2 ⟨hμ, hν⟩ end lemma has_finite_integral.left_of_add_measure {f : α → β} (h : has_finite_integral f (μ + ν)) : has_finite_integral f μ := h.mono_measure $ measure.le_add_right $ le_refl _ lemma has_finite_integral.right_of_add_measure {f : α → β} (h : has_finite_integral f (μ + ν)) : has_finite_integral f ν := h.mono_measure $ measure.le_add_left $ le_refl _ @[simp] lemma has_finite_integral_add_measure {f : α → β} : has_finite_integral f (μ + ν) ↔ has_finite_integral f μ ∧ has_finite_integral f ν := ⟨λ h, ⟨h.left_of_add_measure, h.right_of_add_measure⟩, λ h, h.1.add_measure h.2⟩ lemma has_finite_integral.smul_measure {f : α → β} (h : has_finite_integral f μ) {c : ennreal} (hc : c < ⊤) : has_finite_integral f (c • μ) := begin simp only [has_finite_integral, lintegral_smul_measure] at *, exact mul_lt_top hc h end @[simp] lemma has_finite_integral_zero_measure (f : α → β) : has_finite_integral f 0 := by simp only [has_finite_integral, lintegral_zero_measure, with_top.zero_lt_top] variables (α β μ) @[simp] lemma has_finite_integral_zero : has_finite_integral (λa:α, (0:β)) μ := by simp [has_finite_integral] variables {α β μ} lemma has_finite_integral.neg {f : α → β} (hfi : has_finite_integral f μ) : has_finite_integral (-f) μ := by simpa [has_finite_integral] using hfi @[simp] lemma has_finite_integral_neg_iff {f : α → β} : has_finite_integral (-f) μ ↔ has_finite_integral f μ := ⟨λ h, neg_neg f ▸ h.neg, has_finite_integral.neg⟩ lemma has_finite_integral.norm {f : α → β} (hfi : has_finite_integral f μ) : has_finite_integral (λa, ∥f a∥) μ := have eq : (λa, (nnnorm ∥f a∥ : ennreal)) = λa, (nnnorm (f a) : ennreal), by { funext, rw nnnorm_norm }, by { rwa [has_finite_integral, eq] } lemma has_finite_integral_norm_iff (f : α → β) : has_finite_integral (λa, ∥f a∥) μ ↔ has_finite_integral f μ := has_finite_integral_congr' $ eventually_of_forall $ λ x, norm_norm (f x) section dominated_convergence variables {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} lemma all_ae_of_real_F_le_bound (h : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) : ∀ n, ∀ᵐ a ∂μ, ennreal.of_real ∥F n a∥ ≤ ennreal.of_real (bound a) := λn, (h n).mono $ λ a h, ennreal.of_real_le_of_real h lemma all_ae_tendsto_of_real_norm (h : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top $ 𝓝 $ f a) : ∀ᵐ a ∂μ, tendsto (λn, ennreal.of_real ∥F n a∥) at_top $ 𝓝 $ ennreal.of_real ∥f a∥ := h.mono $ λ a h, tendsto_of_real $ tendsto.comp (continuous.tendsto continuous_norm _) h lemma all_ae_of_real_f_le_bound (h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : ∀ᵐ a ∂μ, ennreal.of_real ∥f a∥ ≤ ennreal.of_real (bound a) := begin have F_le_bound := all_ae_of_real_F_le_bound h_bound, rw ← ae_all_iff at F_le_bound, apply F_le_bound.mp ((all_ae_tendsto_of_real_norm h_lim).mono _), assume a tendsto_norm F_le_bound, exact le_of_tendsto' tendsto_norm (F_le_bound) end lemma has_finite_integral_of_dominated_convergence {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} (bound_has_finite_integral : has_finite_integral bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : has_finite_integral f μ := /- `∥F n a∥ ≤ bound a` and `∥F n a∥ --> ∥f a∥` implies `∥f a∥ ≤ bound a`, and so `∫ ∥f∥ ≤ ∫ bound < ⊤` since `bound` is has_finite_integral -/ begin rw has_finite_integral_iff_norm, calc ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ ≤ ∫⁻ a, ennreal.of_real (bound a) ∂μ : lintegral_mono_ae $ all_ae_of_real_f_le_bound h_bound h_lim ... < ⊤ : begin rw ← has_finite_integral_iff_of_real, { exact bound_has_finite_integral }, exact (h_bound 0).mono (λ a h, le_trans (norm_nonneg _) h) end end lemma tendsto_lintegral_norm_of_dominated_convergence [measurable_space β] [borel_space β] [second_countable_topology β] {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} (F_measurable : ∀ n, measurable (F n)) (f_measurable : measurable f) (bound_has_finite_integral : has_finite_integral bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫⁻ a, (ennreal.of_real ∥F n a - f a∥) ∂μ) at_top (𝓝 0) := let b := λa, 2 * ennreal.of_real (bound a) in /- `∥F n a∥ ≤ bound a` and `F n a --> f a` implies `∥f a∥ ≤ bound a`, and thus by the triangle inequality, have `∥F n a - f a∥ ≤ 2 * (bound a). -/ have hb : ∀ n, ∀ᵐ a ∂μ, ennreal.of_real ∥F n a - f a∥ ≤ b a, begin assume n, filter_upwards [all_ae_of_real_F_le_bound h_bound n, all_ae_of_real_f_le_bound h_bound h_lim], assume a h₁ h₂, calc ennreal.of_real ∥F n a - f a∥ ≤ (ennreal.of_real ∥F n a∥) + (ennreal.of_real ∥f a∥) : begin rw [← ennreal.of_real_add], apply of_real_le_of_real, { apply norm_sub_le }, { exact norm_nonneg _ }, { exact norm_nonneg _ } end ... ≤ (ennreal.of_real (bound a)) + (ennreal.of_real (bound a)) : add_le_add h₁ h₂ ... = b a : by rw ← two_mul end, /- On the other hand, `F n a --> f a` implies that `∥F n a - f a∥ --> 0` -/ have h : ∀ᵐ a ∂μ, tendsto (λ n, ennreal.of_real ∥F n a - f a∥) at_top (𝓝 0), begin rw ← ennreal.of_real_zero, refine h_lim.mono (λ a h, (continuous_of_real.tendsto _).comp _), rwa ← tendsto_iff_norm_tendsto_zero end, /- Therefore, by the dominated convergence theorem for nonnegative integration, have ` ∫ ∥f a - F n a∥ --> 0 ` -/ begin suffices h : tendsto (λn, ∫⁻ a, (ennreal.of_real ∥F n a - f a∥) ∂μ) at_top (𝓝 (∫⁻ (a:α), 0 ∂μ)), { rwa lintegral_zero at h }, -- Using the dominated convergence theorem. refine tendsto_lintegral_of_dominated_convergence _ _ hb _ _, -- Show `λa, ∥f a - F n a∥` is measurable for all `n` { exact λn, measurable_of_real.comp ((F_measurable n).sub f_measurable).norm }, -- Show `2 * bound` is has_finite_integral { rw has_finite_integral_iff_of_real at bound_has_finite_integral, { calc ∫⁻ a, b a ∂μ = 2 * ∫⁻ a, ennreal.of_real (bound a) ∂μ : by { rw lintegral_const_mul', exact coe_ne_top } ... < ⊤ : mul_lt_top (coe_lt_top) bound_has_finite_integral }, filter_upwards [h_bound 0] λ a h, le_trans (norm_nonneg _) h }, -- Show `∥f a - F n a∥ --> 0` { exact h } end end dominated_convergence section pos_part /-! Lemmas used for defining the positive part of a `L¹` function -/ lemma has_finite_integral.max_zero {f : α → ℝ} (hf : has_finite_integral f μ) : has_finite_integral (λa, max (f a) 0) μ := hf.mono $ eventually_of_forall $ λ x, by simp [real.norm_eq_abs, abs_le, abs_nonneg, le_abs_self] lemma has_finite_integral.min_zero {f : α → ℝ} (hf : has_finite_integral f μ) : has_finite_integral (λa, min (f a) 0) μ := hf.mono $ eventually_of_forall $ λ x, by simp [real.norm_eq_abs, abs_le, abs_nonneg, neg_le, neg_le_abs_self] end pos_part section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma has_finite_integral.smul (c : 𝕜) {f : α → β} : has_finite_integral f μ → has_finite_integral (c • f) μ := begin simp only [has_finite_integral], assume hfi, calc ∫⁻ (a : α), nnnorm (c • f a) ∂μ = ∫⁻ (a : α), (nnnorm c) * nnnorm (f a) ∂μ : by simp only [nnnorm_smul, ennreal.coe_mul] ... < ⊤ : begin rw lintegral_const_mul', exacts [mul_lt_top coe_lt_top hfi, coe_ne_top] end end lemma has_finite_integral_smul_iff {c : 𝕜} (hc : c ≠ 0) (f : α → β) : has_finite_integral (c • f) μ ↔ has_finite_integral f μ := begin split, { assume h, simpa only [smul_smul, inv_mul_cancel hc, one_smul] using h.smul c⁻¹ }, exact has_finite_integral.smul _ end lemma has_finite_integral.const_mul {f : α → ℝ} (h : has_finite_integral f μ) (c : ℝ) : has_finite_integral (λ x, c * f x) μ := (has_finite_integral.smul c h : _) lemma has_finite_integral.mul_const {f : α → ℝ} (h : has_finite_integral f μ) (c : ℝ) : has_finite_integral (λ x, f x * c) μ := by simp_rw [mul_comm, h.const_mul _] end normed_space /-! ### The predicate `integrable` -/ variables [measurable_space β] [measurable_space γ] [measurable_space δ] /-- `integrable f μ` means that `f` is measurable and that the integral `∫⁻ a, ∥f a∥ ∂μ` is finite. `integrable f` means `integrable f volume`. -/ def integrable (f : α → β) (μ : measure α . volume_tac) : Prop := measurable f ∧ has_finite_integral f μ lemma integrable.measurable {f : α → β} (hf : integrable f μ) : measurable f := hf.1 lemma integrable.has_finite_integral {f : α → β} (hf : integrable f μ) : has_finite_integral f μ := hf.2 lemma integrable.mono {f : α → β} {g : α → γ} (hg : integrable g μ) (hf : measurable f) (h : ∀ᵐ a ∂μ, ∥f a∥ ≤ ∥g a∥) : integrable f μ := ⟨hf, hg.has_finite_integral.mono h⟩ lemma integrable.mono' {f : α → β} {g : α → ℝ} (hg : integrable g μ) (hf : measurable f) (h : ∀ᵐ a ∂μ, ∥f a∥ ≤ g a) : integrable f μ := ⟨hf, hg.has_finite_integral.mono' h⟩ lemma integrable.congr' {f : α → β} {g : α → γ} (hf : integrable f μ) (hg : measurable g) (h : ∀ᵐ a ∂μ, ∥f a∥ = ∥g a∥) : integrable g μ := ⟨hg, hf.has_finite_integral.congr' h⟩ lemma integrable_congr' {f : α → β} {g : α → γ} (hf : measurable f) (hg : measurable g) (h : ∀ᵐ a ∂μ, ∥f a∥ = ∥g a∥) : integrable f μ ↔ integrable g μ := ⟨λ h2f, h2f.congr' hg h, λ h2g, h2g.congr' hf $ eventually_eq.symm h⟩ lemma integrable.congr {f g : α → β} (hf : integrable f μ) (hg : measurable g) (h : f =ᵐ[μ] g) : integrable g μ := hf.congr' hg $ h.fun_comp norm lemma integrable_congr {f g : α → β} (hf : measurable f) (hg : measurable g) (h : f =ᵐ[μ] g) : integrable f μ ↔ integrable g μ := integrable_congr' hf hg $ h.fun_comp norm lemma integrable_const_iff {c : β} : integrable (λ x : α, c) μ ↔ c = 0 ∨ μ univ < ⊤ := by rw [integrable, and_iff_right measurable_const, has_finite_integral_const_iff] lemma integrable_const [finite_measure μ] (c : β) : integrable (λ x : α, c) μ := integrable_const_iff.2 $ or.inr $ measure_lt_top _ _ lemma integrable.mono_measure {f : α → β} (h : integrable f ν) (hμ : μ ≤ ν) : integrable f μ := ⟨h.measurable, h.has_finite_integral.mono_measure hμ⟩ lemma integrable.add_measure {f : α → β} (hμ : integrable f μ) (hν : integrable f ν) : integrable f (μ + ν) := ⟨hμ.measurable, hμ.has_finite_integral.add_measure hν.has_finite_integral⟩ lemma integrable.left_of_add_measure {f : α → β} (h : integrable f (μ + ν)) : integrable f μ := h.mono_measure $ measure.le_add_right $ le_refl _ lemma integrable.right_of_add_measure {f : α → β} (h : integrable f (μ + ν)) : integrable f ν := h.mono_measure $ measure.le_add_left $ le_refl _ @[simp] lemma integrable_add_measure {f : α → β} : integrable f (μ + ν) ↔ integrable f μ ∧ integrable f ν := ⟨λ h, ⟨h.left_of_add_measure, h.right_of_add_measure⟩, λ h, h.1.add_measure h.2⟩ lemma integrable.smul_measure {f : α → β} (h : integrable f μ) {c : ennreal} (hc : c < ⊤) : integrable f (c • μ) := ⟨h.measurable, h.has_finite_integral.smul_measure hc⟩ lemma integrable_map_measure [opens_measurable_space β] {f : α → δ} {g : δ → β} (hf : measurable f) (hg : measurable g) : integrable g (measure.map f μ) ↔ integrable (g ∘ f) μ := by { simp only [integrable, has_finite_integral, lintegral_map hg.ennnorm hf, hf, hg, hg.comp hf] } lemma lintegral_edist_lt_top [second_countable_topology β] [opens_measurable_space β] {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : ∫⁻ a, edist (f a) (g a) ∂μ < ⊤ := lt_of_le_of_lt (lintegral_edist_triangle hf.measurable hg.measurable (measurable_const : measurable (λa, (0 : β)))) (ennreal.add_lt_top.2 $ by { simp_rw ← has_finite_integral_iff_edist, exact ⟨hf.has_finite_integral, hg.has_finite_integral⟩ }) variables (α β μ) @[simp] lemma integrable_zero : integrable (λ _, (0 : β)) μ := by simp [integrable, measurable_const] variables {α β μ} lemma integrable.add' [opens_measurable_space β] {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : has_finite_integral (f + g) μ := calc ∫⁻ a, nnnorm (f a + g a) ∂μ ≤ ∫⁻ a, nnnorm (f a) + nnnorm (g a) ∂μ : lintegral_mono (λ a, by exact_mod_cast nnnorm_add_le _ _) ... = _ : lintegral_nnnorm_add hf.measurable hg.measurable ... < ⊤ : add_lt_top.2 ⟨hf.has_finite_integral, hg.has_finite_integral⟩ lemma integrable.add [borel_space β] [second_countable_topology β] {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : integrable (f + g) μ := ⟨hf.measurable.add hg.measurable, hf.add' hg⟩ lemma integrable_finset_sum {ι} [borel_space β] [second_countable_topology β] (s : finset ι) {f : ι → α → β} (hf : ∀ i, integrable (f i) μ) : integrable (λ a, ∑ i in s, f i a) μ := begin refine finset.induction_on s _ _, { simp only [finset.sum_empty, integrable_zero] }, { assume i s his ih, simp only [his, finset.sum_insert, not_false_iff], exact (hf _).add ih } end lemma integrable.neg [borel_space β] {f : α → β} (hf : integrable f μ) : integrable (-f) μ := ⟨hf.measurable.neg, hf.has_finite_integral.neg⟩ @[simp] lemma integrable_neg_iff [borel_space β] {f : α → β} : integrable (-f) μ ↔ integrable f μ := ⟨λ h, neg_neg f ▸ h.neg, integrable.neg⟩ lemma integrable.sub' [opens_measurable_space β] {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : has_finite_integral (f - g) μ := calc ∫⁻ a, nnnorm (f a - g a) ∂μ ≤ ∫⁻ a, nnnorm (f a) + nnnorm (-g a) ∂μ : lintegral_mono (assume a, by exact_mod_cast nnnorm_add_le _ _ ) ... = _ : by { simp only [nnnorm_neg], exact lintegral_nnnorm_add hf.measurable hg.measurable } ... < ⊤ : add_lt_top.2 ⟨hf.has_finite_integral, hg.has_finite_integral⟩ lemma integrable.sub [borel_space β] [second_countable_topology β] {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : integrable (f - g) μ := hf.add hg.neg lemma integrable.norm [opens_measurable_space β] {f : α → β} (hf : integrable f μ) : integrable (λa, ∥f a∥) μ := ⟨hf.measurable.norm, hf.has_finite_integral.norm⟩ lemma integrable_norm_iff [opens_measurable_space β] {f : α → β} (hf : measurable f) : integrable (λa, ∥f a∥) μ ↔ integrable f μ := by simp_rw [integrable, and_iff_right hf, and_iff_right hf.norm, has_finite_integral_norm_iff] lemma integrable.prod_mk [opens_measurable_space β] [opens_measurable_space γ] {f : α → β} {g : α → γ} (hf : integrable f μ) (hg : integrable g μ) : integrable (λ x, (f x, g x)) μ := ⟨hf.measurable.prod_mk hg.measurable, (hf.norm.add' hg.norm).mono $ eventually_of_forall $ λ x, calc max ∥f x∥ ∥g x∥ ≤ ∥f x∥ + ∥g x∥ : max_le_add_of_nonneg (norm_nonneg _) (norm_nonneg _) ... ≤ ∥(∥f x∥ + ∥g x∥)∥ : le_abs_self _⟩ section pos_part /-! ### Lemmas used for defining the positive part of a `L¹` function -/ lemma integrable.max_zero {f : α → ℝ} (hf : integrable f μ) : integrable (λa, max (f a) 0) μ := ⟨hf.measurable.max measurable_const, hf.has_finite_integral.max_zero⟩ lemma integrable.min_zero {f : α → ℝ} (hf : integrable f μ) : integrable (λa, min (f a) 0) μ := ⟨hf.measurable.min measurable_const, hf.has_finite_integral.min_zero⟩ end pos_part section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma integrable.smul [borel_space β] (c : 𝕜) {f : α → β} (hf : integrable f μ) : integrable (c • f) μ := ⟨hf.measurable.const_smul c, hf.has_finite_integral.smul c⟩ lemma integrable_smul_iff [borel_space β] {c : 𝕜} (hc : c ≠ 0) (f : α → β) : integrable (c • f) μ ↔ integrable f μ := and_congr (measurable_const_smul_iff hc) (has_finite_integral_smul_iff hc f) lemma integrable.const_mul {f : α → ℝ} (h : integrable f μ) (c : ℝ) : integrable (λ x, c * f x) μ := integrable.smul c h lemma integrable.mul_const {f : α → ℝ} (h : integrable f μ) (c : ℝ) : integrable (λ x, f x * c) μ := by simp_rw [mul_comm, h.const_mul _] end normed_space section normed_space_over_complete_field variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] [measurable_space 𝕜] variables [borel_space 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] [measurable_space E] [borel_space E] lemma integrable_smul_const {f : α → 𝕜} {c : E} (hc : c ≠ 0) : integrable (λ x, f x • c) μ ↔ integrable f μ := begin simp_rw [integrable, measurable_smul_const hc, and.congr_right_iff, has_finite_integral, nnnorm_smul, ennreal.coe_mul], intro hf, rw [lintegral_mul_const' _ _ ennreal.coe_ne_top, ennreal.mul_lt_top_iff], have : ∀ x : ennreal, x = 0 → x < ⊤ := by simp, simp [hc, or_iff_left_of_imp (this _)] end end normed_space_over_complete_field variables [second_countable_topology β] /-! ### The predicate `integrable` on measurable functions modulo a.e.-equality -/ namespace ae_eq_fun section variable [opens_measurable_space β] /-- A class of almost everywhere equal functions is `integrable` if it has a finite distance to the origin. It means the same thing as the predicate `integrable` over functions. -/ def integrable (f : α →ₘ[μ] β) : Prop := f ∈ ball (0 : α →ₘ[μ] β) ⊤ lemma integrable_mk {f : α → β} (hf : measurable f) : (integrable (mk f hf : α →ₘ[μ] β)) ↔ measure_theory.integrable f μ := by simp [integrable, zero_def, edist_mk_mk', measure_theory.integrable, nndist_eq_nnnorm, has_finite_integral, hf] lemma integrable_coe_fn {f : α →ₘ[μ] β} : (measure_theory.integrable f μ) ↔ integrable f := by rw [← integrable_mk, mk_coe_fn] lemma integrable_zero : integrable (0 : α →ₘ[μ] β) := mem_ball_self coe_lt_top end section variable [borel_space β] lemma integrable.add {f g : α →ₘ[μ] β} : integrable f → integrable g → integrable (f + g) := begin refine induction_on₂ f g (λ f hf g hg hfi hgi, _), simp only [integrable_mk, mk_add_mk] at hfi hgi ⊢, exact hfi.add hgi end lemma integrable.neg {f : α →ₘ[μ] β} : integrable f → integrable (-f) := induction_on f $ λ f hfm hfi, (integrable_mk _).2 ((integrable_mk hfm).1 hfi).neg lemma integrable.sub {f g : α →ₘ[μ] β} (hf : integrable f) (hg : integrable g) : integrable (f - g) := hf.add hg.neg protected lemma is_add_subgroup : is_add_subgroup (ball (0 : α →ₘ[μ] β) ⊤) := { zero_mem := integrable_zero, add_mem := λ _ _, integrable.add, neg_mem := λ _, integrable.neg } section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma integrable.smul {c : 𝕜} {f : α →ₘ[μ] β} : integrable f → integrable (c • f) := induction_on f $ λ f hfm hfi, (integrable_mk _).2 $ ((integrable_mk hfm).1 hfi).smul _ end normed_space end end ae_eq_fun /-! ### The `L¹` space of functions -/ variables (α β) /-- The space of equivalence classes of integrable (and measurable) functions, where two integrable functions are equivalent if they agree almost everywhere, i.e., they differ on a set of measure `0`. -/ def l1 [opens_measurable_space β] (μ : measure α) : Type* := {f : α →ₘ[μ] β // f.integrable} notation α ` →₁[`:25 μ `] ` β := l1 α β μ variables {α β} namespace l1 open ae_eq_fun local attribute [instance] ae_eq_fun.is_add_subgroup section variable [opens_measurable_space β] instance : has_coe (α →₁[μ] β) (α →ₘ[μ] β) := coe_subtype instance : has_coe_to_fun (α →₁[μ] β) := ⟨λ f, α → β, λ f, ⇑(f : α →ₘ[μ] β)⟩ @[simp, norm_cast] lemma coe_coe (f : α →₁[μ] β) : ⇑(f : α →ₘ[μ] β) = f := rfl protected lemma eq {f g : α →₁[μ] β} : (f : α →ₘ[μ] β) = (g : α →ₘ[μ] β) → f = g := subtype.eq @[norm_cast] protected lemma eq_iff {f g : α →₁[μ] β} : (f : α →ₘ[μ] β) = (g : α →ₘ[μ] β) ↔ f = g := iff.intro (l1.eq) (congr_arg coe) /- TODO : order structure of l1-/ /-- `L¹` space forms a `emetric_space`, with the emetric being inherited from almost everywhere functions, i.e., `edist f g = ∫⁻ a, edist (f a) (g a)`. -/ instance : emetric_space (α →₁[μ] β) := subtype.emetric_space /-- `L¹` space forms a `metric_space`, with the metric being inherited from almost everywhere functions, i.e., `edist f g = ennreal.to_real (∫⁻ a, edist (f a) (g a))`. -/ instance : metric_space (α →₁[μ] β) := metric_space_emetric_ball 0 ⊤ end variable [borel_space β] instance : add_comm_group (α →₁[μ] β) := subtype.add_comm_group instance : inhabited (α →₁[μ] β) := ⟨0⟩ @[simp, norm_cast] lemma coe_zero : ((0 : α →₁[μ] β) : α →ₘ[μ] β) = 0 := rfl @[simp, norm_cast] lemma coe_add (f g : α →₁[μ] β) : ((f + g : α →₁[μ] β) : α →ₘ[μ] β) = f + g := rfl @[simp, norm_cast] lemma coe_neg (f : α →₁[μ] β) : ((-f : α →₁[μ] β) : α →ₘ[μ] β) = -f := rfl @[simp, norm_cast] lemma coe_sub (f g : α →₁[μ] β) : ((f - g : α →₁[μ] β) : α →ₘ[μ] β) = f - g := rfl @[simp] lemma edist_eq (f g : α →₁[μ] β) : edist f g = edist (f : α →ₘ[μ] β) (g : α →ₘ[μ] β) := rfl lemma dist_eq (f g : α →₁[μ] β) : dist f g = ennreal.to_real (edist (f : α →ₘ[μ] β) (g : α →ₘ[μ] β)) := rfl /-- The norm on `L¹` space is defined to be `∥f∥ = ∫⁻ a, edist (f a) 0`. -/ instance : has_norm (α →₁[μ] β) := ⟨λ f, dist f 0⟩ lemma norm_eq (f : α →₁[μ] β) : ∥f∥ = ennreal.to_real (edist (f : α →ₘ[μ] β) 0) := rfl instance : normed_group (α →₁[μ] β) := normed_group.of_add_dist (λ x, rfl) $ by { intros, simp only [dist_eq, coe_add], rw edist_add_right } section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] instance : has_scalar 𝕜 (α →₁[μ] β) := ⟨λ x f, ⟨x • (f : α →ₘ[μ] β), ae_eq_fun.integrable.smul f.2⟩⟩ @[simp, norm_cast] lemma coe_smul (c : 𝕜) (f : α →₁[μ] β) : ((c • f : α →₁[μ] β) : α →ₘ[μ] β) = c • (f : α →ₘ[μ] β) := rfl instance : semimodule 𝕜 (α →₁[μ] β) := { one_smul := λf, l1.eq (by { simp only [coe_smul], exact one_smul _ _ }), mul_smul := λx y f, l1.eq (by { simp only [coe_smul], exact mul_smul _ _ _ }), smul_add := λx f g, l1.eq (by { simp only [coe_smul, coe_add], exact smul_add _ _ _ }), smul_zero := λx, l1.eq (by { simp only [coe_zero, coe_smul], exact smul_zero _ }), add_smul := λx y f, l1.eq (by { simp only [coe_smul], exact add_smul _ _ _ }), zero_smul := λf, l1.eq (by { simp only [coe_smul], exact zero_smul _ _ }) } instance : normed_space 𝕜 (α →₁[μ] β) := ⟨ begin rintros x ⟨f, hf⟩, show ennreal.to_real (edist (x • f) 0) ≤ ∥x∥ * ennreal.to_real (edist f 0), rw [edist_smul, to_real_of_real_mul], exact norm_nonneg _ end ⟩ end normed_space section of_fun /-- Construct the equivalence class `[f]` of a measurable and integrable function `f`. -/ def of_fun (f : α → β) (hf : integrable f μ) : (α →₁[μ] β) := ⟨mk f hf.measurable, by { rw integrable_mk, exact hf }⟩ @[simp] lemma of_fun_eq_mk (f : α → β) (hf : integrable f μ) : (of_fun f hf : α →ₘ[μ] β) = mk f hf.measurable := rfl lemma of_fun_eq_of_fun (f g : α → β) (hf : integrable f μ) (hg : integrable g μ) : of_fun f hf = of_fun g hg ↔ f =ᵐ[μ] g := by { rw ← l1.eq_iff, simp only [of_fun_eq_mk, mk_eq_mk] } lemma of_fun_zero : of_fun (λ _, (0 : β)) (integrable_zero α β μ) = 0 := rfl lemma of_fun_add (f g : α → β) (hf : integrable f μ) (hg : integrable g μ) : of_fun (f + g) (hf.add hg) = of_fun f hf + of_fun g hg := rfl lemma of_fun_neg (f : α → β) (hf : integrable f μ) : of_fun (- f) (integrable.neg hf) = - of_fun f hf := rfl lemma of_fun_sub (f g : α → β) (hf : integrable f μ) (hg : integrable g μ) : of_fun (f - g) (hf.sub hg) = of_fun f hf - of_fun g hg := rfl lemma norm_of_fun (f : α → β) (hf : integrable f μ) : ∥ of_fun f hf ∥ = ennreal.to_real (∫⁻ a, edist (f a) 0 ∂μ) := rfl lemma norm_of_fun_eq_lintegral_norm (f : α → β) (hf : integrable f μ) : ∥ of_fun f hf ∥ = ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) := by { rw [norm_of_fun, lintegral_norm_eq_lintegral_edist] } variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma of_fun_smul (f : α → β) (hf : integrable f μ) (k : 𝕜) : of_fun (λa, k • f a) (hf.smul k) = k • of_fun f hf := rfl end of_fun section to_fun protected lemma measurable (f : α →₁[μ] β) : measurable f := f.1.measurable lemma measurable_norm (f : α →₁[μ] β) : measurable (λ a, ∥f a∥) := f.measurable.norm protected lemma integrable (f : α →₁[μ] β) : integrable ⇑f μ := integrable_coe_fn.2 f.2 protected lemma has_finite_integral (f : α →₁[μ] β) : has_finite_integral ⇑f μ := f.integrable.has_finite_integral lemma integrable_norm (f : α →₁[μ] β) : integrable (λ a, ∥f a∥) μ := (integrable_norm_iff f.measurable).mpr f.integrable lemma of_fun_to_fun (f : α →₁[μ] β) : of_fun f f.integrable = f := subtype.ext (f : α →ₘ[μ] β).mk_coe_fn lemma mk_to_fun (f : α →₁[μ] β) : (mk f f.measurable : α →ₘ[μ] β) = f := by { rw ← of_fun_eq_mk, rw l1.eq_iff, exact of_fun_to_fun f } lemma to_fun_of_fun (f : α → β) (hf : integrable f μ) : ⇑(of_fun f hf : α →₁[μ] β) =ᵐ[μ] f := coe_fn_mk f hf.measurable variables (α β) lemma zero_to_fun : ⇑(0 : α →₁[μ] β) =ᵐ[μ] 0 := ae_eq_fun.coe_fn_zero variables {α β} lemma add_to_fun (f g : α →₁[μ] β) : ⇑(f + g) =ᵐ[μ] f + g := ae_eq_fun.coe_fn_add _ _ lemma neg_to_fun (f : α →₁[μ] β) : ⇑(-f) =ᵐ[μ] -⇑f := ae_eq_fun.coe_fn_neg _ lemma sub_to_fun (f g : α →₁[μ] β) : ⇑(f - g) =ᵐ[μ] ⇑f - ⇑g := ae_eq_fun.coe_fn_sub _ _ lemma dist_to_fun (f g : α →₁[μ] β) : dist f g = ennreal.to_real (∫⁻ x, edist (f x) (g x) ∂μ) := by { simp only [← coe_coe, dist_eq, edist_eq_coe] } lemma norm_eq_nnnorm_to_fun (f : α →₁[μ] β) : ∥f∥ = ennreal.to_real (∫⁻ a, nnnorm (f a) ∂μ) := by { rw [← coe_coe, lintegral_nnnorm_eq_lintegral_edist, ← edist_zero_eq_coe], refl } lemma norm_eq_norm_to_fun (f : α →₁[μ] β) : ∥f∥ = ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) := by { rw norm_eq_nnnorm_to_fun, congr, funext, rw of_real_norm_eq_coe_nnnorm } lemma lintegral_edist_to_fun_lt_top (f g : α →₁[μ] β) : (∫⁻ a, edist (f a) (g a) ∂μ) < ⊤ := lintegral_edist_lt_top f.integrable g.integrable variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma smul_to_fun (c : 𝕜) (f : α →₁[μ] β) : ⇑(c • f) =ᵐ[μ] c • f := ae_eq_fun.coe_fn_smul _ _ lemma norm_eq_lintegral (f : α →₁[μ] β) : ∥f∥ = (∫⁻ x, (nnnorm (f x) : ennreal) ∂μ).to_real := by simp [l1.norm_eq, ae_eq_fun.edist_zero_eq_coe, ← edist_eq_coe_nnnorm] /-- Computing the norm of a difference between two L¹-functions. Note that this is not a special case of `norm_eq_lintegral` since `(f - g) x` and `f x - g x` are not equal (but only a.e.-equal). -/ lemma norm_sub_eq_lintegral (f g : α →₁[μ] β) : ∥f - g∥ = (∫⁻ x, (nnnorm (f x - g x) : ennreal) ∂μ).to_real := begin simp_rw [l1.norm_eq, ae_eq_fun.edist_zero_eq_coe, ← edist_eq_coe_nnnorm], rw lintegral_congr_ae, refine (ae_eq_fun.coe_fn_sub (f : α →ₘ[μ] β) g).mp _, apply eventually_of_forall, intros x hx, simp [hx] end lemma of_real_norm_eq_lintegral (f : α →₁[μ] β) : ennreal.of_real ∥f∥ = ∫⁻ x, (nnnorm (f x) : ennreal) ∂μ := by { rw [norm_eq_lintegral, ennreal.of_real_to_real], rw [← ennreal.lt_top_iff_ne_top], exact f.has_finite_integral } /-- Computing the norm of a difference between two L¹-functions. Note that this is not a special case of `of_real_norm_eq_lintegral` since `(f - g) x` and `f x - g x` are not equal (but only a.e.-equal). -/ lemma of_real_norm_sub_eq_lintegral (f g : α →₁[μ] β) : ennreal.of_real ∥f - g∥ = ∫⁻ x, (nnnorm (f x - g x) : ennreal) ∂μ := begin simp_rw [of_real_norm_eq_lintegral, ← edist_eq_coe_nnnorm], apply lintegral_congr_ae, refine (ae_eq_fun.coe_fn_sub (f : α →ₘ[μ] β) g).mp _, apply eventually_of_forall, intros x hx, simp only [l1.coe_coe, pi.sub_apply] at hx, simp_rw [← hx, ← l1.coe_sub, l1.coe_coe] end end to_fun section pos_part /-- Positive part of a function in `L¹` space. -/ def pos_part (f : α →₁[μ] ℝ) : α →₁[μ] ℝ := ⟨ae_eq_fun.pos_part f, begin rw [← ae_eq_fun.integrable_coe_fn, integrable_congr (ae_eq_fun.measurable _) (f.measurable.max measurable_const) (coe_fn_pos_part _)], exact f.integrable.max_zero end ⟩ /-- Negative part of a function in `L¹` space. -/ def neg_part (f : α →₁[μ] ℝ) : α →₁[μ] ℝ := pos_part (-f) @[norm_cast] lemma coe_pos_part (f : α →₁[μ] ℝ) : (f.pos_part : α →ₘ[μ] ℝ) = (f : α →ₘ[μ] ℝ).pos_part := rfl lemma pos_part_to_fun (f : α →₁[μ] ℝ) : ⇑(pos_part f) =ᵐ[μ] λ a, max (f a) 0 := ae_eq_fun.coe_fn_pos_part _ lemma neg_part_to_fun_eq_max (f : α →₁[μ] ℝ) : ∀ᵐ a ∂μ, neg_part f a = max (- f a) 0 := begin rw neg_part, filter_upwards [pos_part_to_fun (-f), neg_to_fun f], simp only [mem_set_of_eq], assume a h₁ h₂, rw [h₁, h₂, pi.neg_apply] end lemma neg_part_to_fun_eq_min (f : α →₁[μ] ℝ) : ∀ᵐ a ∂μ, neg_part f a = - min (f a) 0 := (neg_part_to_fun_eq_max f).mono $ assume a h, by rw [h, min_eq_neg_max_neg_neg, _root_.neg_neg, neg_zero] lemma norm_le_norm_of_ae_le {f g : α →₁[μ] β} (h : ∀ᵐ a ∂μ, ∥f a∥ ≤ ∥g a∥) : ∥f∥ ≤ ∥g∥ := begin simp only [l1.norm_eq_norm_to_fun], rw to_real_le_to_real, { apply lintegral_mono_ae, exact h.mono (λ a h, of_real_le_of_real h) }, { rw [← lt_top_iff_ne_top, ← has_finite_integral_iff_norm], exact f.has_finite_integral }, { rw [← lt_top_iff_ne_top, ← has_finite_integral_iff_norm], exact g.has_finite_integral } end lemma continuous_pos_part : continuous $ λf : α →₁[μ] ℝ, pos_part f := begin simp only [metric.continuous_iff], assume g ε hε, use ε, use hε, simp only [dist_eq_norm], assume f hfg, refine lt_of_le_of_lt (norm_le_norm_of_ae_le _) hfg, filter_upwards [l1.sub_to_fun f g, l1.sub_to_fun (pos_part f) (pos_part g), pos_part_to_fun f, pos_part_to_fun g], simp only [mem_set_of_eq], assume a h₁ h₂ h₃ h₄, simp only [real.norm_eq_abs, h₁, h₂, h₃, h₄, pi.sub_apply], exact abs_max_sub_max_le_abs _ _ _ end lemma continuous_neg_part : continuous $ λf : α →₁[μ] ℝ, neg_part f := have eq : (λf : α →₁[μ] ℝ, neg_part f) = (λf : α →₁[μ] ℝ, pos_part (-f)) := rfl, by { rw eq, exact continuous_pos_part.comp continuous_neg } end pos_part /- TODO: l1 is a complete space -/ end l1 end measure_theory open measure_theory lemma measurable.integrable_zero [measurable_space β] {f : α → β} (hf : measurable f) : integrable f 0 := ⟨hf, has_finite_integral_zero_measure f⟩
7b92168a26e3237805581cb241e3ca8d90e0b751
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/category_theory/limits/shapes/images.lean
af9773c1e04cfa1fe190d10fe9ef51fbda618b7f
[ "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
25,165
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Markus Himmel -/ import category_theory.limits.shapes.equalizers import category_theory.limits.shapes.strong_epi /-! # Categorical images We define the categorical image of `f` as a factorisation `f = e ≫ m` through a monomorphism `m`, so that `m` factors through the `m'` in any other such factorisation. ## Main definitions * A `mono_factorisation` is a factorisation `f = e ≫ m`, where `m` is a monomorphism * `is_image F` means that a given mono factorisation `F` has the universal property of the image. * `has_image f` means that we have chosen an image for the morphism `f : X ⟶ Y`. * In this case, `image f` is the image object, `image.ι f : image f ⟶ Y` is the monomorphism `m` of the factorisation and `factor_thru_image f : X ⟶ image f` is the morphism `e`. * `has_images C` means that every morphism in `C` has an image. * Let `f : X ⟶ Y` and `g : P ⟶ Q` be morphisms in `C`, which we will represent as objects of the arrow category `arrow C`. Then `sq : f ⟶ g` is a commutative square in `C`. If `f` and `g` have images, then `has_image_map sq` represents the fact that there is a morphism `i : image f ⟶ image g` making the diagram X ----→ image f ----→ Y | | | | | | ↓ ↓ ↓ P ----→ image g ----→ Q commute, where the top row is the image factorisation of `f`, the bottom row is the image factorisation of `g`, and the outer rectangle is the commutative square `sq`. * If a category `has_images`, then `has_image_maps` means that every commutative square admits an image map. * If a category `has_images`, then `has_strong_epi_images` means that the morphism to the image is always a strong epimorphism. ## Main statements * When `C` has equalizers, the morphism `e` appearing in an image factorisation is an epimorphism. * When `C` has strong epi images, then these images admit image maps. ## Future work * TODO: coimages, and abelian categories. * TODO: connect this with existing working in the group theory and ring theory libraries. -/ noncomputable theory universes v u open category_theory open category_theory.limits.walking_parallel_pair namespace category_theory.limits variables {C : Type u} [category.{v} C] variables {X Y : C} (f : X ⟶ Y) /-- A factorisation of a morphism `f = e ≫ m`, with `m` monic. -/ structure mono_factorisation (f : X ⟶ Y) := (I : C) (m : I ⟶ Y) [m_mono : mono m] (e : X ⟶ I) (fac' : e ≫ m = f . obviously) restate_axiom mono_factorisation.fac' attribute [simp, reassoc] mono_factorisation.fac attribute [instance] mono_factorisation.m_mono attribute [instance] mono_factorisation.m_mono namespace mono_factorisation /-- The obvious factorisation of a monomorphism through itself. -/ def self [mono f] : mono_factorisation f := { I := X, m := f, e := 𝟙 X } -- I'm not sure we really need this, but the linter says that an inhabited instance ought to exist... instance [mono f] : inhabited (mono_factorisation f) := ⟨self f⟩ /-- The morphism `m` in a factorisation `f = e ≫ m` through a monomorphism is uniquely determined. -/ @[ext] lemma ext {F F' : mono_factorisation f} (hI : F.I = F'.I) (hm : F.m = (eq_to_hom hI) ≫ F'.m) : F = F' := begin cases F, cases F', cases hI, simp at hm, dsimp at F_fac' F'_fac', congr, { assumption }, { resetI, apply (cancel_mono F_m).1, rw [F_fac', hm, F'_fac'], } end end mono_factorisation variable {f} /-- Data exhibiting that a given factorisation through a mono is initial. -/ structure is_image (F : mono_factorisation f) := (lift : Π (F' : mono_factorisation f), F.I ⟶ F'.I) (lift_fac' : Π (F' : mono_factorisation f), lift F' ≫ F'.m = F.m . obviously) restate_axiom is_image.lift_fac' attribute [simp, reassoc] is_image.lift_fac @[simp, reassoc] lemma is_image.fac_lift {F : mono_factorisation f} (hF : is_image F) (F' : mono_factorisation f) : F.e ≫ hF.lift F' = F'.e := (cancel_mono F'.m).1 $ by simp variable (f) namespace is_image /-- The trivial factorisation of a monomorphism satisfies the universal property. -/ @[simps] def self [mono f] : is_image (mono_factorisation.self f) := { lift := λ F', F'.e } instance [mono f] : inhabited (is_image (mono_factorisation.self f)) := ⟨self f⟩ variable {f} /-- Two factorisations through monomorphisms satisfying the universal property must factor through isomorphic objects. -/ -- TODO this is another good candidate for a future `unique_up_to_canonical_iso`. @[simps] def iso_ext {F F' : mono_factorisation f} (hF : is_image F) (hF' : is_image F') : F.I ≅ F'.I := { hom := hF.lift F', inv := hF'.lift F, hom_inv_id' := (cancel_mono F.m).1 (by simp), inv_hom_id' := (cancel_mono F'.m).1 (by simp) } variables {F F' : mono_factorisation f} (hF : is_image F) (hF' : is_image F') lemma iso_ext_hom_m : (iso_ext hF hF').hom ≫ F'.m = F.m := by simp lemma iso_ext_inv_m : (iso_ext hF hF').inv ≫ F.m = F'.m := by simp lemma e_iso_ext_hom : F.e ≫ (iso_ext hF hF').hom = F'.e := by simp lemma e_iso_ext_inv : F'.e ≫ (iso_ext hF hF').inv = F.e := by simp end is_image /-- Data exhibiting that a morphism `f` has an image. -/ structure image_factorisation (f : X ⟶ Y) := (F : mono_factorisation f) (is_image : is_image F) instance inhabited_image_factorisation (f : X ⟶ Y) [mono f] : inhabited (image_factorisation f) := ⟨⟨_, is_image.self f⟩⟩ /-- `has_image f` means that there exists an image factorisation of `f`. -/ class has_image (f : X ⟶ Y) : Prop := mk' :: (exists_image : nonempty (image_factorisation f)) lemma has_image.mk {f : X ⟶ Y} (F : image_factorisation f) : has_image f := ⟨nonempty.intro F⟩ section variable [has_image f] /-- The chosen factorisation of `f` through a monomorphism. -/ def image.mono_factorisation : mono_factorisation f := (classical.choice (has_image.exists_image)).F /-- The witness of the universal property for the chosen factorisation of `f` through a monomorphism. -/ def image.is_image : is_image (image.mono_factorisation f) := (classical.choice (has_image.exists_image)).is_image /-- The categorical image of a morphism. -/ def image : C := (image.mono_factorisation f).I /-- The inclusion of the image of a morphism into the target. -/ def image.ι : image f ⟶ Y := (image.mono_factorisation f).m @[simp] lemma image.as_ι : (image.mono_factorisation f).m = image.ι f := rfl instance : mono (image.ι f) := (image.mono_factorisation f).m_mono /-- The map from the source to the image of a morphism. -/ def factor_thru_image : X ⟶ image f := (image.mono_factorisation f).e /-- Rewrite in terms of the `factor_thru_image` interface. -/ @[simp] lemma as_factor_thru_image : (image.mono_factorisation f).e = factor_thru_image f := rfl @[simp, reassoc] lemma image.fac : factor_thru_image f ≫ image.ι f = f := (image.mono_factorisation f).fac' variable {f} /-- Any other factorisation of the morphism `f` through a monomorphism receives a map from the image. -/ def image.lift (F' : mono_factorisation f) : image f ⟶ F'.I := (image.is_image f).lift F' @[simp, reassoc] lemma image.lift_fac (F' : mono_factorisation f) : image.lift F' ≫ F'.m = image.ι f := (image.is_image f).lift_fac' F' @[simp, reassoc] lemma image.fac_lift (F' : mono_factorisation f) : factor_thru_image f ≫ image.lift F' = F'.e := (image.is_image f).fac_lift F' @[simp, reassoc] lemma is_image.lift_ι {F : mono_factorisation f} (hF : is_image F) : hF.lift (image.mono_factorisation f) ≫ image.ι f = F.m := hF.lift_fac _ -- TODO we could put a category structure on `mono_factorisation f`, -- with the morphisms being `g : I ⟶ I'` commuting with the `m`s -- (they then automatically commute with the `e`s) -- and show that an `image_of f` gives an initial object there -- (uniqueness of the lift comes for free). instance lift_mono (F' : mono_factorisation f) : mono (image.lift F') := begin split, intros Z a b w, have w' : a ≫ image.ι f = b ≫ image.ι f := calc a ≫ image.ι f = a ≫ (image.lift F' ≫ F'.m) : by simp ... = (a ≫ image.lift F') ≫ F'.m : by rw [category.assoc] ... = (b ≫ image.lift F') ≫ F'.m : by rw w ... = b ≫ (image.lift F' ≫ F'.m) : by rw [←category.assoc] ... = b ≫ image.ι f : by simp, exact (cancel_mono (image.ι f)).1 w', end lemma has_image.uniq (F' : mono_factorisation f) (l : image f ⟶ F'.I) (w : l ≫ F'.m = image.ι f) : l = image.lift F' := (cancel_mono F'.m).1 (by simp [w]) end section variables (C) /-- `has_images` represents a choice of image for every morphism -/ class has_images := (has_image : Π {X Y : C} (f : X ⟶ Y), has_image f) attribute [instance, priority 100] has_images.has_image end section variables (f) [has_image f] /-- The image of a monomorphism is isomorphic to the source. -/ def image_mono_iso_source [mono f] : image f ≅ X := is_image.iso_ext (image.is_image f) (is_image.self f) @[simp, reassoc] lemma image_mono_iso_source_inv_ι [mono f] : (image_mono_iso_source f).inv ≫ image.ι f = f := by simp [image_mono_iso_source] @[simp, reassoc] lemma image_mono_iso_source_hom_self [mono f] : (image_mono_iso_source f).hom ≫ f = image.ι f := begin conv { to_lhs, congr, skip, rw ←image_mono_iso_source_inv_ι f, }, rw [←category.assoc, iso.hom_inv_id, category.id_comp], end -- This is the proof that `factor_thru_image f` is an epimorphism -- from https://en.wikipedia.org/wiki/Image_(category_theory), which is in turn taken from: -- Mitchell, Barry (1965), Theory of categories, MR 0202787, p.12, Proposition 10.1 @[ext] lemma image.ext {W : C} {g h : image f ⟶ W} [has_limit (parallel_pair g h)] (w : factor_thru_image f ≫ g = factor_thru_image f ≫ h) : g = h := begin let q := equalizer.ι g h, let e' := equalizer.lift _ w, let F' : mono_factorisation f := { I := equalizer g h, m := q ≫ image.ι f, m_mono := by apply mono_comp, e := e' }, let v := image.lift F', have t₀ : v ≫ q ≫ image.ι f = image.ι f := image.lift_fac F', have t : v ≫ q = 𝟙 (image f) := (cancel_mono_id (image.ι f)).1 (by { convert t₀ using 1, rw category.assoc }), -- The proof from wikipedia next proves `q ≫ v = 𝟙 _`, -- and concludes that `equalizer g h ≅ image f`, -- but this isn't necessary. calc g = 𝟙 (image f) ≫ g : by rw [category.id_comp] ... = v ≫ q ≫ g : by rw [←t, category.assoc] ... = v ≫ q ≫ h : by rw [equalizer.condition g h] ... = 𝟙 (image f) ≫ h : by rw [←category.assoc, t] ... = h : by rw [category.id_comp] end instance [Π {Z : C} (g h : image f ⟶ Z), has_limit (parallel_pair g h)] : epi (factor_thru_image f) := ⟨λ Z g h w, image.ext f w⟩ lemma epi_image_of_epi {X Y : C} (f : X ⟶ Y) [has_image f] [E : epi f] : epi (image.ι f) := begin rw ←image.fac f at E, resetI, exact epi_of_epi (factor_thru_image f) (image.ι f), end lemma epi_of_epi_image {X Y : C} (f : X ⟶ Y) [has_image f] [epi (image.ι f)] [epi (factor_thru_image f)] : epi f := by { rw [←image.fac f], apply epi_comp, } end section variables {f} {f' : X ⟶ Y} [has_image f] [has_image f'] /-- An equation between morphisms gives a comparison map between the images (which momentarily we prove is an iso). -/ def image.eq_to_hom (h : f = f') : image f ⟶ image f' := image.lift { I := image f', m := image.ι f', e := factor_thru_image f', }. instance (h : f = f') : is_iso (image.eq_to_hom h) := { inv := image.eq_to_hom h.symm, hom_inv_id' := (cancel_mono (image.ι f)).1 (by simp [image.eq_to_hom]), inv_hom_id' := (cancel_mono (image.ι f')).1 (by simp [image.eq_to_hom]), } /-- An equation between morphisms gives an isomorphism between the images. -/ def image.eq_to_iso (h : f = f') : image f ≅ image f' := as_iso (image.eq_to_hom h) /-- As long as the category has equalizers, the image inclusion maps commute with `image.eq_to_iso`. -/ lemma image.eq_fac [has_equalizers C] (h : f = f') : image.ι f = (image.eq_to_iso h).hom ≫ image.ι f' := by { ext, simp [image.eq_to_iso, image.eq_to_hom], } end section variables {Z : C} (g : Y ⟶ Z) /-- The comparison map `image (f ≫ g) ⟶ image g`. -/ def image.pre_comp [has_image g] [has_image (f ≫ g)] : image (f ≫ g) ⟶ image g := image.lift { I := image g, m := image.ι g, e := f ≫ factor_thru_image g } @[simp, reassoc] lemma image.factor_thru_image_pre_comp [has_image g] [has_image (f ≫ g)] : factor_thru_image (f ≫ g) ≫ image.pre_comp f g = f ≫ factor_thru_image g := by simp [image.pre_comp] /-- The two step comparison map `image (f ≫ (g ≫ h)) ⟶ image (g ≫ h) ⟶ image h` agrees with the one step comparison map `image (f ≫ (g ≫ h)) ≅ image ((f ≫ g) ≫ h) ⟶ image h`. -/ lemma image.pre_comp_comp {W : C} (h : Z ⟶ W) [has_image (g ≫ h)] [has_image (f ≫ g ≫ h)] [has_image h] [has_image ((f ≫ g) ≫ h)] : image.pre_comp f (g ≫ h) ≫ image.pre_comp g h = image.eq_to_hom (category.assoc f g h).symm ≫ (image.pre_comp (f ≫ g) h) := begin apply (cancel_mono (image.ι h)).1, simp [image.pre_comp, image.eq_to_hom], end variables [has_equalizers C] /-- `image.pre_comp f g` is an isomorphism when `f` is an isomorphism (we need `C` to have equalizers to prove this). -/ instance image.is_iso_precomp_iso (f : X ≅ Y) [has_image g] [has_image (f.hom ≫ g)] : is_iso (image.pre_comp f.hom g) := { inv := image.lift { I := image (f.hom ≫ g), m := image.ι (f.hom ≫ g), e := f.inv ≫ factor_thru_image (f.hom ≫ g) }, hom_inv_id' := by { ext, simp [image.pre_comp], }, inv_hom_id' := by { ext, simp [image.pre_comp], }, } -- Note that in general we don't have the other comparison map you might expect -- `image f ⟶ image (f ≫ g)`. end end category_theory.limits namespace category_theory.limits variables {C : Type u} [category.{v} C] section instance {X Y : C} (f : X ⟶ Y) [has_image f] : has_image (arrow.mk f).hom := show has_image f, by apply_instance end section has_image_map /-- An image map is a morphism `image f → image g` fitting into a commutative square and satisfying the obvious commutativity conditions. -/ structure image_map {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟶ g) := (map : image f.hom ⟶ image g.hom) (map_ι' : map ≫ image.ι g.hom = image.ι f.hom ≫ sq.right . obviously) instance inhabited_image_map {f : arrow C} [has_image f.hom] : inhabited (image_map (𝟙 f)) := ⟨⟨𝟙 _, by tidy⟩⟩ restate_axiom image_map.map_ι' attribute [simp, reassoc] image_map.map_ι @[simp, reassoc] lemma image_map.factor_map {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟶ g) (m : image_map sq) : factor_thru_image f.hom ≫ m.map = sq.left ≫ factor_thru_image g.hom := (cancel_mono (image.ι g.hom)).1 $ by simp [arrow.w] /-- To give an image map for a commutative square with `f` at the top and `g` at the bottom, it suffices to give a map between any mono factorisation of `f` and any image factorisation of `g`. -/ def image_map.transport {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟶ g) (F : mono_factorisation f.hom) {F' : mono_factorisation g.hom} (hF' : is_image F') {map : F.I ⟶ F'.I} (map_ι : map ≫ F'.m = F.m ≫ sq.right) : image_map sq := { map := image.lift F ≫ map ≫ hF'.lift (image.mono_factorisation g.hom), map_ι' := by simp [map_ι] } /-- `has_image_map sq` means that there is an `image_map` for the square `sq`. -/ class has_image_map {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟶ g) : Prop := mk' :: (has_image_map : nonempty (image_map sq)) lemma has_image_map.mk {f g : arrow C} [has_image f.hom] [has_image g.hom] {sq : f ⟶ g} (m : image_map sq) : has_image_map sq := ⟨nonempty.intro m⟩ lemma has_image_map.transport {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟶ g) (F : mono_factorisation f.hom) {F' : mono_factorisation g.hom} (hF' : is_image F') (map : F.I ⟶ F'.I) (map_ι : map ≫ F'.m = F.m ≫ sq.right) : has_image_map sq := has_image_map.mk $ image_map.transport sq F hF' map_ι /-- Obtain an `image_map` from a `has_image_map` instance. -/ def has_image_map.image_map {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟶ g) [has_image_map sq] : image_map sq := classical.choice $ @has_image_map.has_image_map _ _ _ _ _ _ sq _ variables {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟶ g) section local attribute [ext] image_map instance : subsingleton (image_map sq) := subsingleton.intro $ λ a b, image_map.ext a b $ (cancel_mono (image.ι g.hom)).1 $ by simp only [image_map.map_ι] end variable [has_image_map sq] /-- The map on images induced by a commutative square. -/ abbreviation image.map : image f.hom ⟶ image g.hom := (has_image_map.image_map sq).map lemma image.factor_map : factor_thru_image f.hom ≫ image.map sq = sq.left ≫ factor_thru_image g.hom := by simp lemma image.map_ι : image.map sq ≫ image.ι g.hom = image.ι f.hom ≫ sq.right := by simp lemma image.map_hom_mk'_ι {X Y P Q : C} {k : X ⟶ Y} [has_image k] {l : P ⟶ Q} [has_image l] {m : X ⟶ P} {n : Y ⟶ Q} (w : m ≫ l = k ≫ n) [has_image_map (arrow.hom_mk' w)] : image.map (arrow.hom_mk' w) ≫ image.ι l = image.ι k ≫ n := image.map_ι _ section variables {h : arrow C} [has_image h.hom] (sq' : g ⟶ h) variables [has_image_map sq'] /-- Image maps for composable commutative squares induce an image map in the composite square. -/ def image_map_comp : image_map (sq ≫ sq') := { map := image.map sq ≫ image.map sq' } @[simp] lemma image.map_comp [has_image_map (sq ≫ sq')] : image.map (sq ≫ sq') = image.map sq ≫ image.map sq' := show (has_image_map.image_map (sq ≫ sq')).map = (image_map_comp sq sq').map, by congr end section variables (f) /-- The identity `image f ⟶ image f` fits into the commutative square represented by the identity morphism `𝟙 f` in the arrow category. -/ def image_map_id : image_map (𝟙 f) := { map := 𝟙 (image f.hom) } @[simp] lemma image.map_id [has_image_map (𝟙 f)] : image.map (𝟙 f) = 𝟙 (image f.hom) := show (has_image_map.image_map (𝟙 f)).map = (image_map_id f).map, by congr end end has_image_map section variables (C) [has_images C] /-- If a category `has_image_maps`, then all commutative squares induce morphisms on images. -/ class has_image_maps := (has_image_map : Π {f g : arrow C} (st : f ⟶ g), has_image_map st) attribute [instance, priority 100] has_image_maps.has_image_map end section has_image_maps variables [has_images C] [has_image_maps C] /-- The functor from the arrow category of `C` to `C` itself that maps a morphism to its image and a commutative square to the induced morphism on images. -/ @[simps] def im : arrow C ⥤ C := { obj := λ f, image f.hom, map := λ _ _ st, image.map st } end has_image_maps section strong_epi_mono_factorisation /-- A strong epi-mono factorisation is a decomposition `f = e ≫ m` with `e` a strong epimorphism and `m` a monomorphism. -/ structure strong_epi_mono_factorisation {X Y : C} (f : X ⟶ Y) extends mono_factorisation f := [e_strong_epi : strong_epi e] attribute [instance] strong_epi_mono_factorisation.e_strong_epi /-- Satisfying the inhabited linter -/ instance strong_epi_mono_factorisation_inhabited {X Y : C} (f : X ⟶ Y) [strong_epi f] : inhabited (strong_epi_mono_factorisation f) := ⟨⟨⟨Y, 𝟙 Y, f, by simp⟩⟩⟩ /-- A mono factorisation coming from a strong epi-mono factorisation always has the universal property of the image. -/ def strong_epi_mono_factorisation.to_mono_is_image {X Y : C} {f : X ⟶ Y} (F : strong_epi_mono_factorisation f) : is_image F.to_mono_factorisation := { lift := λ G, arrow.lift $ arrow.hom_mk' $ show G.e ≫ G.m = F.e ≫ F.m, by rw [F.to_mono_factorisation.fac, G.fac] } variable (C) /-- A category has strong epi-mono factorisations if every morphism admits a strong epi-mono factorisation. -/ class has_strong_epi_mono_factorisations : Prop := mk' :: (has_fac : Π {X Y : C} (f : X ⟶ Y), nonempty (strong_epi_mono_factorisation f)) variable {C} lemma has_strong_epi_mono_factorisations.mk (d : Π {X Y : C} (f : X ⟶ Y), strong_epi_mono_factorisation f) : has_strong_epi_mono_factorisations C := ⟨λ X Y f, nonempty.intro $ d f⟩ @[priority 100] instance has_images_of_has_strong_epi_mono_factorisations [has_strong_epi_mono_factorisations C] : has_images C := { has_image := λ X Y f, let F' := classical.choice (has_strong_epi_mono_factorisations.has_fac f) in has_image.mk { F := F'.to_mono_factorisation, is_image := F'.to_mono_is_image } } end strong_epi_mono_factorisation section has_strong_epi_images variables (C) [has_images C] /-- A category has strong epi images if it has all images and `factor_thru_image f` is a strong epimorphism for all `f`. -/ class has_strong_epi_images : Prop := (strong_factor_thru_image : Π {X Y : C} (f : X ⟶ Y), strong_epi (factor_thru_image f)) attribute [instance] has_strong_epi_images.strong_factor_thru_image end has_strong_epi_images section has_strong_epi_images /-- If there is a single strong epi-mono factorisation of `f`, then every image factorisation is a strong epi-mono factorisation. -/ lemma strong_epi_of_strong_epi_mono_factorisation {X Y : C} {f : X ⟶ Y} (F : strong_epi_mono_factorisation f) {F' : mono_factorisation f} (hF' : is_image F') : strong_epi F'.e := by { rw ←is_image.e_iso_ext_hom F.to_mono_is_image hF', apply strong_epi_comp } lemma strong_epi_factor_thru_image_of_strong_epi_mono_factorisation {X Y : C} {f : X ⟶ Y} [has_image f] (F : strong_epi_mono_factorisation f) : strong_epi (factor_thru_image f) := strong_epi_of_strong_epi_mono_factorisation F $ image.is_image f /-- If we constructed our images from strong epi-mono factorisations, then these images are strong epi images. -/ @[priority 100] instance has_strong_epi_images_of_has_strong_epi_mono_factorisations [has_strong_epi_mono_factorisations C] : has_strong_epi_images C := { strong_factor_thru_image := λ X Y f, strong_epi_factor_thru_image_of_strong_epi_mono_factorisation $ classical.choice $ has_strong_epi_mono_factorisations.has_fac f } end has_strong_epi_images section has_strong_epi_images variables [has_images C] /-- A category with strong epi images has image maps. The construction is taken from Borceux, Handbook of Categorical Algebra 1, Proposition 4.4.5. -/ @[priority 100] instance has_image_maps_of_has_strong_epi_images [has_strong_epi_images C] : has_image_maps C := { has_image_map := λ f g st, let I := image (image.ι f.hom ≫ st.right) in let I' := image (st.left ≫ factor_thru_image g.hom) in let upper : strong_epi_mono_factorisation (f.hom ≫ st.right) := { I := I, e := factor_thru_image f.hom ≫ factor_thru_image (image.ι f.hom ≫ st.right), m := image.ι (image.ι f.hom ≫ st.right), e_strong_epi := strong_epi_comp _ _, m_mono := by apply_instance } in let lower : strong_epi_mono_factorisation (f.hom ≫ st.right) := { I := I', e := factor_thru_image (st.left ≫ factor_thru_image g.hom), m := image.ι (st.left ≫ factor_thru_image g.hom) ≫ image.ι g.hom, fac' := by simp [arrow.w], e_strong_epi := by apply_instance, m_mono := mono_comp _ _ } in let s : I ⟶ I' := is_image.lift upper.to_mono_is_image lower.to_mono_factorisation in has_image_map.mk { map := factor_thru_image (image.ι f.hom ≫ st.right) ≫ s ≫ image.ι (st.left ≫ factor_thru_image g.hom), map_ι' := by rw [category.assoc, category.assoc, is_image.lift_fac upper.to_mono_is_image lower.to_mono_factorisation, image.fac] } } end has_strong_epi_images variables [has_strong_epi_mono_factorisations.{v} C] variables {X Y : C} {f : X ⟶ Y} /-- If `C` has strong epi mono factorisations, then the image is unique up to isomorphism, in that if `f` factors as a strong epi followed by a mono, this factorisation is essentially the image factorisation. -/ def image.iso_strong_epi_mono {I' : C} (e : X ⟶ I') (m : I' ⟶ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] : I' ≅ image f := is_image.iso_ext {strong_epi_mono_factorisation . I := I', m := m, e := e}.to_mono_is_image (image.is_image f) @[simp] lemma image.iso_strong_epi_mono_hom_comp_ι {I' : C} (e : X ⟶ I') (m : I' ⟶ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] : (image.iso_strong_epi_mono e m comm).hom ≫ image.ι f = m := is_image.lift_fac _ _ @[simp] lemma image.iso_strong_epi_mono_inv_comp_mono {I' : C} (e : X ⟶ I') (m : I' ⟶ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] : (image.iso_strong_epi_mono e m comm).inv ≫ m = image.ι f := image.lift_fac _ end category_theory.limits
87bf623b4a79cf09e8f713ad3bd5c442daa2948e
af6139dd14451ab8f69cf181cf3a20f22bd699be
/tests/lean/run/cpdt.lean
4f9fce52324d0378151419de7bcb82fdb3af8cab
[ "Apache-2.0" ]
permissive
gitter-badger/lean-1
1cca01252d3113faa45681b6a00e1b5e3a0f6203
5c7ade4ee4f1cdf5028eabc5db949479d6737c85
refs/heads/master
1,611,425,383,521
1,487,871,140,000
1,487,871,140,000
82,995,612
0
0
null
1,487,905,618,000
1,487,905,618,000
null
UTF-8
Lean
false
false
1,075
lean
import tools.mini_crush /- "Proving in the Large" chapter of CPDT -/ inductive exp : Type | Const (n : nat) : exp | Plus (e1 e2 : exp) : exp | Mult (e1 e2 : exp) : exp open exp def exp_eval : exp → nat | (Const n) := n | (Plus e1 e2) := exp_eval e1 + exp_eval e2 | (Mult e1 e2) := exp_eval e1 * exp_eval e2 def times (k : nat) : exp → exp | (Const n) := Const (k * n) | (Plus e1 e2) := Plus (times e1) (times e2) | (Mult e1 e2) := Mult (times e1) e2 attribute [simp] mul_add @[simp] theorem eval_times : ∀ (k e), exp_eval (times k e) = k * exp_eval e := by mini_crush def reassoc : exp → exp | (Const n) := (Const n) | (Plus e1 e2) := let e1' := reassoc e1 in let e2' := reassoc e2 in match e2' with | (Plus e21 e22) := Plus (Plus e1' e21) e22 | _ := Plus e1' e2' end | (Mult e1 e2) := let e1' := reassoc e1 in let e2' := reassoc e2 in match e2' with | (Mult e21 e22) := Mult (Mult e1' e21) e22 | _ := Mult e1' e2' end theorem reassoc_correct (e) : exp_eval (reassoc e) = exp_eval e := by mini_crush
303974541df30f28e28dfa1fc3637fcf7752eab6
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Meta/CollectMVars.lean
adc6c5c7c5282434e33a4bf136fe00cd84f1cb0e
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,692
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.Util.CollectMVars import Lean.Meta.Basic namespace Lean.Meta /-- Collect unassigned metavariables occuring in the given expression. Remark: if `e` contains `?m` and there is a `t` assigned to `?m`, we collect unassigned metavariables occurring in `t`. Remark: if `e` contains `?m` and `?m` is delayed assigned to some term `t`, we collect `?m` and unassigned metavariables occurring in `t`. We collect `?m` because it has not been assigned yet. -/ partial def collectMVars (e : Expr) : StateRefT CollectMVars.State MetaM Unit := do let e ← instantiateMVars e let s ← get let resultSavedSize := s.result.size let s := e.collectMVars s set s for mvarId in s.result[resultSavedSize:] do match (← getDelayedMVarAssignment? mvarId) with | none => pure () | some d => collectMVars (mkMVar d.mvarIdPending) /-- Return metavariables in occuring the given expression. See `collectMVars` -/ def getMVars (e : Expr) : MetaM (Array MVarId) := do let (_, s) ← (collectMVars e).run {} pure s.result /-- Similar to getMVars, but removes delayed assignments. -/ def getMVarsNoDelayed (e : Expr) : MetaM (Array MVarId) := do let mvarIds ← getMVars e mvarIds.filterM fun mvarId => not <$> mvarId.isDelayedAssigned def collectMVarsAtDecl (d : Declaration) : StateRefT CollectMVars.State MetaM Unit := d.forExprM collectMVars def getMVarsAtDecl (d : Declaration) : MetaM (Array MVarId) := do let (_, s) ← (collectMVarsAtDecl d).run {} pure s.result end Lean.Meta
80f0507f97576e6991cf459772e59531b9190cc9
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/logic/unnamed_211.lean
942dc3c45069b8f365b70635e71aa7af4122ada9
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
371
lean
import data.real.basic def fn_ub (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, f x ≤ a def fn_lb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, a ≤ f x variables (f g : ℝ → ℝ) (a b : ℝ) -- BEGIN example (hfa : fn_ub f a) (hgb : fn_ub g b) : fn_ub (λ x, f x + g x) (a + b) := begin intro x, dsimp, apply add_le_add, apply hfa, apply hgb end -- END
7f0de194b4810286a08646124f7b85bdaf951283
491068d2ad28831e7dade8d6dff871c3e49d9431
/library/data/nat/gcd.lean
f9f6de6d76beb1f82a6e2893b24ab6d90baf697e
[ "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
15,557
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 Definitions and properties of gcd, lcm, and coprime. -/ import .div open eq.ops well_founded decidable prod namespace nat /- gcd -/ private definition pair_nat.lt : nat × nat → nat × nat → Prop := measure pr₂ private definition pair_nat.lt.wf : well_founded pair_nat.lt := intro_k (measure.wf pr₂) 20 -- we use intro_k to be able to execute gcd efficiently in the kernel local attribute pair_nat.lt.wf [instance] -- instance will not be saved in .olean local infixl ` ≺ `:50 := pair_nat.lt private definition gcd.lt.dec (x y₁ : nat) : (succ y₁, x mod succ y₁) ≺ (x, succ y₁) := !mod_lt (succ_pos y₁) definition gcd.F : Π (p₁ : nat × nat), (Π p₂ : nat × nat, p₂ ≺ p₁ → nat) → nat | (x, 0) f := x | (x, succ y) f := f (succ y, x mod succ y) !gcd.lt.dec definition gcd (x y : nat) := fix gcd.F (x, y) theorem gcd_zero_right (x : nat) : gcd x 0 = x := rfl theorem gcd_succ (x y : nat) : gcd x (succ y) = gcd (succ y) (x mod succ y) := well_founded.fix_eq gcd.F (x, succ y) theorem gcd_one_right (n : ℕ) : gcd n 1 = 1 := calc gcd n 1 = gcd 1 (n mod 1) : gcd_succ ... = gcd 1 0 : mod_one theorem gcd_def (x : ℕ) : Π (y : ℕ), gcd x y = if y = 0 then x else gcd y (x mod y) | 0 := !gcd_zero_right | (succ y) := !gcd_succ ⬝ (if_neg !succ_ne_zero)⁻¹ theorem gcd_self : Π (n : ℕ), gcd n n = n | 0 := rfl | (succ n₁) := calc gcd (succ n₁) (succ n₁) = gcd (succ n₁) (succ n₁ mod succ n₁) : gcd_succ ... = gcd (succ n₁) 0 : mod_self theorem gcd_zero_left : Π (n : ℕ), gcd 0 n = n | 0 := rfl | (succ n₁) := calc gcd 0 (succ n₁) = gcd (succ n₁) (0 mod succ n₁) : gcd_succ ... = gcd (succ n₁) 0 : zero_mod theorem gcd_of_pos (m : ℕ) {n : ℕ} (H : n > 0) : gcd m n = gcd n (m mod n) := gcd_def m n ⬝ if_neg (ne_zero_of_pos H) theorem gcd_rec (m n : ℕ) : gcd m n = gcd n (m mod n) := by_cases_zero_pos n (calc m = gcd 0 m : gcd_zero_left ... = gcd 0 (m mod 0) : mod_zero) (take n, assume H : 0 < n, gcd_of_pos m H) theorem gcd.induction {P : ℕ → ℕ → Prop} (m n : ℕ) (H0 : ∀m, P m 0) (H1 : ∀m n, 0 < n → P n (m mod n) → P m n) : P m n := induction (m, n) (prod.rec (λm, nat.rec (λ IH, H0 m) (λ n₁ v (IH : ∀p₂, p₂ ≺ (m, succ n₁) → P (pr₁ p₂) (pr₂ p₂)), H1 m (succ n₁) !succ_pos (IH _ !gcd.lt.dec)))) theorem gcd_dvd (m n : ℕ) : (gcd m n ∣ m) ∧ (gcd m n ∣ n) := gcd.induction m n (take m, and.intro (!one_mul ▸ !dvd_mul_left) !dvd_zero) (take m n (npos : 0 < n), and.rec (assume (IH₁ : gcd n (m mod n) ∣ n) (IH₂ : gcd n (m mod n) ∣ (m mod n)), have H : (gcd n (m mod n) ∣ (m div n * n + m mod n)), from dvd_add (dvd.trans IH₁ !dvd_mul_left) IH₂, have H1 : (gcd n (m mod n) ∣ m), from !eq_div_mul_add_mod⁻¹ ▸ H, show (gcd m n ∣ m) ∧ (gcd m n ∣ n), from !gcd_rec⁻¹ ▸ (and.intro H1 IH₁))) theorem gcd_dvd_left (m n : ℕ) : gcd m n ∣ m := and.left !gcd_dvd theorem gcd_dvd_right (m n : ℕ) : gcd m n ∣ n := and.right !gcd_dvd theorem dvd_gcd {m n k : ℕ} : k ∣ m → k ∣ n → k ∣ gcd m n := gcd.induction m n (take m, imp.intro) (take m n (npos : n > 0) (IH : k ∣ n → k ∣ m mod n → k ∣ gcd n (m mod n)) (H1 : k ∣ m) (H2 : k ∣ n), have H3 : k ∣ m div n * n + m mod n, from !eq_div_mul_add_mod ▸ H1, have H4 : k ∣ m mod n, from dvd_of_dvd_add_left H3 (dvd.trans H2 !dvd_mul_left), !gcd_rec⁻¹ ▸ IH H2 H4) theorem gcd.comm (m n : ℕ) : gcd m n = gcd n m := dvd.antisymm (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 (dvd_gcd (dvd.trans !gcd_dvd_left !gcd_dvd_left) (dvd_gcd (dvd.trans !gcd_dvd_left !gcd_dvd_right) !gcd_dvd_right)) (dvd_gcd (dvd_gcd !gcd_dvd_left (dvd.trans !gcd_dvd_right !gcd_dvd_left)) (dvd.trans !gcd_dvd_right !gcd_dvd_right)) theorem gcd_one_left (m : ℕ) : gcd 1 m = 1 := !gcd.comm ⬝ !gcd_one_right theorem gcd_mul_left (m n k : ℕ) : gcd (m * n) (m * k) = m * gcd n k := gcd.induction n k (take n, calc gcd (m * n) (m * 0) = gcd (m * n) 0 : mul_zero) (take n k, assume H : 0 < k, assume IH : gcd (m * k) (m * (n mod k)) = m * gcd k (n mod k), calc gcd (m * n) (m * k) = gcd (m * k) (m * n mod (m * k)) : !gcd_rec ... = gcd (m * k) (m * (n mod k)) : mul_mod_mul_left ... = m * gcd k (n mod k) : IH ... = m * gcd n k : !gcd_rec) theorem gcd_mul_right (m n k : ℕ) : gcd (m * n) (k * n) = gcd m k * n := calc gcd (m * n) (k * n) = gcd (n * m) (k * n) : mul.comm ... = gcd (n * m) (n * k) : mul.comm ... = n * gcd m k : gcd_mul_left ... = gcd m k * n : mul.comm theorem gcd_pos_of_pos_left {m : ℕ} (n : ℕ) (mpos : m > 0) : gcd m n > 0 := pos_of_dvd_of_pos !gcd_dvd_left mpos theorem gcd_pos_of_pos_right (m : ℕ) {n : ℕ} (npos : n > 0) : gcd m n > 0 := pos_of_dvd_of_pos !gcd_dvd_right npos theorem eq_zero_of_gcd_eq_zero_left {m n : ℕ} (H : gcd m n = 0) : m = 0 := or.elim (eq_zero_or_pos m) (assume H1, H1) (assume H1 : m > 0, absurd H⁻¹ (ne_of_lt (!gcd_pos_of_pos_left H1))) theorem eq_zero_of_gcd_eq_zero_right {m n : ℕ} (H : gcd m n = 0) : n = 0 := eq_zero_of_gcd_eq_zero_left (!gcd.comm ▸ H) theorem gcd_div {m n k : ℕ} (H1 : k ∣ m) (H2 : k ∣ n) : gcd (m div k) (n div k) = gcd m n div k := or.elim (eq_zero_or_pos k) (assume H3 : k = 0, by subst k; rewrite *div_zero) (assume H3 : k > 0, (div_eq_of_eq_mul_left H3 (calc gcd m n = gcd m (n div k * k) : div_mul_cancel H2 ... = gcd (m div k * k) (n div k * k) : div_mul_cancel H1 ... = gcd (m div k) (n div k) * k : gcd_mul_right))⁻¹) theorem gcd_dvd_gcd_mul_left (m n k : ℕ) : gcd m n ∣ gcd (k * m) n := dvd_gcd (dvd.trans !gcd_dvd_left !dvd_mul_left) !gcd_dvd_right theorem gcd_dvd_gcd_mul_right (m n k : ℕ) : gcd m n ∣ gcd (m * k) n := !mul.comm ▸ !gcd_dvd_gcd_mul_left theorem gcd_dvd_gcd_mul_left_right (m n k : ℕ) : gcd m n ∣ gcd m (k * n) := dvd_gcd !gcd_dvd_left (dvd.trans !gcd_dvd_right !dvd_mul_left) theorem gcd_dvd_gcd_mul_right_right (m n k : ℕ) : gcd m n ∣ gcd m (n * k) := !mul.comm ▸ !gcd_dvd_gcd_mul_left_right /- lcm -/ definition lcm (m n : ℕ) : ℕ := m * n div (gcd m n) theorem lcm.comm (m n : ℕ) : lcm m n = lcm n m := calc lcm m n = m * n div gcd m n : rfl ... = n * m div gcd m n : mul.comm ... = n * m div gcd n m : gcd.comm ... = lcm n m : rfl theorem lcm_zero_left (m : ℕ) : lcm 0 m = 0 := calc lcm 0 m = 0 * m div gcd 0 m : rfl ... = 0 div gcd 0 m : zero_mul ... = 0 : zero_div theorem lcm_zero_right (m : ℕ) : lcm m 0 = 0 := !lcm.comm ▸ !lcm_zero_left theorem lcm_one_left (m : ℕ) : lcm 1 m = m := calc lcm 1 m = 1 * m div gcd 1 m : rfl ... = m div gcd 1 m : one_mul ... = m div 1 : gcd_one_left ... = m : div_one theorem lcm_one_right (m : ℕ) : lcm m 1 = m := !lcm.comm ▸ !lcm_one_left theorem lcm_self (m : ℕ) : lcm m m = m := have H : m * m div m = m, from by_cases_zero_pos m !div_zero (take m, assume H1 : m > 0, !mul_div_cancel H1), calc lcm m m = m * m div gcd m m : rfl ... = m * m div m : gcd_self ... = m : H theorem dvd_lcm_left (m n : ℕ) : m ∣ lcm m n := have H : lcm m n = m * (n div gcd m n), from mul_div_assoc _ !gcd_dvd_right, dvd.intro H⁻¹ theorem dvd_lcm_right (m n : ℕ) : n ∣ lcm m n := !lcm.comm ▸ !dvd_lcm_left theorem gcd_mul_lcm (m n : ℕ) : gcd m n * lcm m n = m * n := eq.symm (eq_mul_of_div_eq_right (dvd.trans !gcd_dvd_left !dvd_mul_right) rfl) theorem lcm_dvd {m n k : ℕ} (H1 : m ∣ k) (H2 : n ∣ k) : lcm m n ∣ k := or.elim (eq_zero_or_pos k) (assume kzero : k = 0, !kzero⁻¹ ▸ !dvd_zero) (assume kpos : k > 0, have mpos : m > 0, from pos_of_dvd_of_pos H1 kpos, have npos : n > 0, from pos_of_dvd_of_pos H2 kpos, have gcd_pos : gcd m n > 0, from !gcd_pos_of_pos_left mpos, obtain p (km : k = m * p), from exists_eq_mul_right_of_dvd H1, obtain q (kn : k = n * q), from exists_eq_mul_right_of_dvd H2, have ppos : p > 0, from pos_of_mul_pos_left (km ▸ kpos), have qpos : q > 0, from pos_of_mul_pos_left (kn ▸ kpos), have H3 : p * q * (m * n * gcd p q) = p * q * (gcd m n * k), from calc p * q * (m * n * gcd p q) = m * p * (n * q * gcd p q) : by rewrite [*mul.assoc, *mul.left_comm q, mul.left_comm p] ... = k * (k * gcd p q) : by rewrite [-kn, -km] ... = k * gcd (k * p) (k * q) : by rewrite gcd_mul_left ... = k * gcd (n * q * p) (m * p * q) : by rewrite [-kn, -km] ... = k * (gcd n m * (p * q)) : by rewrite [*mul.assoc, mul.comm q, gcd_mul_right] ... = p * q * (gcd m n * k) : by rewrite [mul.comm, mul.comm (gcd n m), gcd.comm, *mul.assoc], have H4 : m * n * gcd p q = gcd m n * k, from !eq_of_mul_eq_mul_left (mul_pos ppos qpos) H3, have H5 : gcd m n * (lcm m n * gcd p q) = gcd m n * k, from !mul.assoc ▸ !gcd_mul_lcm⁻¹ ▸ H4, have H6 : lcm m n * gcd p q = k, from !eq_of_mul_eq_mul_left gcd_pos H5, dvd.intro H6) theorem lcm.assoc (m n k : ℕ) : lcm (lcm m n) k = lcm m (lcm n k) := dvd.antisymm (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)) /- coprime -/ definition coprime [reducible] (m n : ℕ) : Prop := gcd m n = 1 theorem coprime_swap {m n : ℕ} (H : coprime n m) : coprime m n := !gcd.comm ▸ H theorem dvd_of_coprime_of_dvd_mul_right {m n k : ℕ} (H1 : coprime k n) (H2 : k ∣ m * n) : k ∣ m := have H3 : gcd (m * k) (m * n) = m, from calc gcd (m * k) (m * n) = m * gcd k n : gcd_mul_left ... = m * 1 : H1 ... = m : mul_one, have H4 : (k ∣ gcd (m * k) (m * n)), from dvd_gcd !dvd_mul_left H2, H3 ▸ H4 theorem dvd_of_coprime_of_dvd_mul_left {m n k : ℕ} (H1 : coprime k m) (H2 : k ∣ m * n) : k ∣ n := dvd_of_coprime_of_dvd_mul_right H1 (!mul.comm ▸ H2) theorem gcd_mul_left_cancel_of_coprime {k : ℕ} (m : ℕ) {n : ℕ} (H : coprime k n) : gcd (k * m) n = gcd m n := have H1 : coprime (gcd (k * m) n) k, from calc gcd (gcd (k * m) n) k = gcd (k * gcd 1 m) n : by rewrite [-gcd_mul_left, mul_one, gcd.comm, gcd.assoc] ... = 1 : by rewrite [gcd_one_left, mul_one, ↑coprime at H, H], dvd.antisymm (dvd_gcd (dvd_of_coprime_of_dvd_mul_left H1 !gcd_dvd_left) !gcd_dvd_right) (dvd_gcd (dvd.trans !gcd_dvd_left !dvd_mul_left) !gcd_dvd_right) theorem gcd_mul_right_cancel_of_coprime (m : ℕ) {k n : ℕ} (H : coprime k n) : gcd (m * k) n = gcd m n := !mul.comm ▸ !gcd_mul_left_cancel_of_coprime H theorem gcd_mul_left_cancel_of_coprime_right {k m : ℕ} (n : ℕ) (H : coprime k m) : gcd m (k * n) = gcd m n := !gcd.comm ▸ !gcd.comm ▸ !gcd_mul_left_cancel_of_coprime H theorem gcd_mul_right_cancel_of_coprime_right {k m : ℕ} (n : ℕ) (H : coprime k m) : gcd m (n * k) = gcd m n := !gcd.comm ▸ !gcd.comm ▸ !gcd_mul_right_cancel_of_coprime H theorem coprime_div_gcd_div_gcd {m n : ℕ} (H : gcd m n > 0) : coprime (m div gcd m n) (n div gcd m n) := calc gcd (m div gcd m n) (n div gcd m n) = gcd m n div gcd m n : gcd_div !gcd_dvd_left !gcd_dvd_right ... = 1 : div_self H theorem not_coprime_of_dvd_of_dvd {m n d : ℕ} (dgt1 : d > 1) (Hm : d ∣ m) (Hn : d ∣ n) : ¬ coprime m n := assume co : coprime m n, assert d ∣ gcd m n, from dvd_gcd Hm Hn, have d ∣ 1, by rewrite [↑coprime at co, co at this]; apply this, have d ≤ 1, from le_of_dvd dec_trivial this, show false, from not_lt_of_ge `d ≤ 1` `d > 1` theorem exists_coprime {m n : ℕ} (H : gcd m n > 0) : exists m' n', coprime m' n' ∧ m = m' * gcd m n ∧ n = n' * gcd m n := have H1 : m = (m div gcd m n) * gcd m n, from (div_mul_cancel !gcd_dvd_left)⁻¹, have H2 : n = (n div gcd m n) * gcd m n, from (div_mul_cancel !gcd_dvd_right)⁻¹, exists.intro _ (exists.intro _ (and.intro (coprime_div_gcd_div_gcd H) (and.intro H1 H2))) theorem coprime_mul {m n k : ℕ} (H1 : coprime m k) (H2 : coprime n k) : coprime (m * n) k := calc gcd (m * n) k = gcd n k : !gcd_mul_left_cancel_of_coprime H1 ... = 1 : H2 theorem coprime_mul_right {k m n : ℕ} (H1 : coprime k m) (H2 : coprime k n) : coprime k (m * n) := coprime_swap (coprime_mul (coprime_swap H1) (coprime_swap H2)) theorem coprime_of_coprime_mul_left {k m n : ℕ} (H : coprime (k * m) n) : coprime m n := have H1 : (gcd m n ∣ gcd (k * m) n), from !gcd_dvd_gcd_mul_left, eq_one_of_dvd_one (H ▸ H1) theorem coprime_of_coprime_mul_right {k m n : ℕ} (H : coprime (m * k) n) : coprime m n := coprime_of_coprime_mul_left (!mul.comm ▸ H) theorem coprime_of_coprime_mul_left_right {k m n : ℕ} (H : coprime m (k * n)) : coprime m n := coprime_swap (coprime_of_coprime_mul_left (coprime_swap H)) theorem coprime_of_coprime_mul_right_right {k m n : ℕ} (H : coprime m (n * k)) : coprime m n := coprime_of_coprime_mul_left_right (!mul.comm ▸ H) theorem comprime_one_left : ∀ n, coprime 1 n := λ n, !gcd_one_left theorem comprime_one_right : ∀ n, coprime n 1 := λ n, !gcd_one_right theorem exists_eq_prod_and_dvd_and_dvd {m n k} (H : k ∣ m * n) : ∃ m' n', k = m' * n' ∧ m' ∣ m ∧ n' ∣ n := or.elim (eq_zero_or_pos (gcd k m)) (assume H1 : gcd k m = 0, have H2 : k = 0, from eq_zero_of_gcd_eq_zero_left H1, have H3 : m = 0, from eq_zero_of_gcd_eq_zero_right H1, have H4 : k = 0 * n, from H2 ⬝ !zero_mul⁻¹, have H5 : 0 ∣ m, from H3⁻¹ ▸ !dvd.refl, have H6 : n ∣ n, from !dvd.refl, exists.intro _ (exists.intro _ (and.intro H4 (and.intro H5 H6)))) (assume H1 : gcd k m > 0, have H2 : gcd k m ∣ k, from !gcd_dvd_left, have H3 : k div gcd k m ∣ (m * n) div gcd k m, from div_dvd_div H2 H, have H4 : (m * n) div gcd k m = (m div gcd k m) * n, from calc m * n div gcd k m = n * m div gcd k m : mul.comm ... = n * (m div gcd k m) : !mul_div_assoc !gcd_dvd_right ... = m div gcd k m * n : mul.comm, have H5 : k div gcd k m ∣ (m div gcd k m) * n, from H4 ▸ H3, have H6 : coprime (k div gcd k m) (m div gcd k m), from coprime_div_gcd_div_gcd H1, have H7 : k div gcd k m ∣ n, from dvd_of_coprime_of_dvd_mul_left H6 H5, have H8 : k = gcd k m * (k div gcd k m), from (mul_div_cancel' H2)⁻¹, exists.intro _ (exists.intro _ (and.intro H8 (and.intro !gcd_dvd_right H7)))) end nat
5daecde78e10a00dd420d1a868b1c7f3e2fb963f
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/meta/widget/basic.lean
6c6ccec04d2ffbc8a6b2a1c26e8d9e3059717e6e
[ "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
16,436
lean
/- Copyright (c) E.W.Ayers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: E.W.Ayers -/ prelude import init.function import init.data.option.basic import init.util import init.meta.tactic import init.meta.mk_dec_eq_instance import init.meta.json /-! A component is a piece of UI which may contain internal state. Use component.mk to build new components. ## Using widgets. To make a widget, you need to make a custom executor object and then instead of calling `save_info_thunk` you call `save_widget`. Additionally, you will need a compatible build of the vscode extension or web app to use widgets in vscode. ## How it works: The design is inspired by React. If you are familiar with using React or Elm or a similar functional UI framework then that's helpful for this. The [React article on reconciliation](https://reactjs.org/docs/reconciliation.html) might be helpful. One can imagine making a UI for a particular object as just being a function `f : α → UI` where `UI` is some inductive datatype for buttons, textboxes, lists and so on. The process of evaluating `f` is called __rendering__. So for example `α` could be `tactic_state` and the function renders a goal view. ## HTML For our purposes, `UI` is an HTML tree and is written `html α : Type`. I'm going to assume some familiarity with HTML for the purposes of this document. An HTML tree is composed of elements and strings. Each element has a tag such as "div", "span", "article" and so on and a set of attributes and child html. Use the helper function `h : string → list (attr α) → list (html α) → html α` to build new pieces of `html`. So for example: ```lean h "ul" [] [ h "li" [] ["this is list item 1"], h "li" [style [("color", "blue")]] ["this is list item 2"], h "hr" [] [], h "li" [] [ h "span" [] ["there is a button here"], h "button" [on_click (λ _, 3)] ["click me!"] ] ] ``` Has the type `html nat`. The `nat` type is called the __action__ and whenever the user interacts with the UI, the html will emit an object of type `nat`. So for example if the user clicks the button above, the html will 'emit' `3`. The above example is compiled to the following piece of html: ```html <ul> <li>this is list item 1</li> <li style="{ color: blue; }">this is list item 2</li> <hr/> <li> <span>There is a button here</span> <button onClick="[handler]">click me!</button> </li> </ul> ``` ## Components In order for the UI to react to events, you need to be able to take these actions α and alter some state. To do this we use __components__. `component` takes two type arguments: `π` and `α`. `α` is called the 'action' and `π` are the 'props'. The props can be thought of as a kind of wrapped function domain for `component`. So given `C : component nat α`, one can turn this into html with `html.of_component 4 C : html α`. The base constructor for a component is `pure`: ```lean meta def Hello : component string α := component.pure (λ s, ["hello, ", s, ", good day!"]) #html Hello "lean" -- renders "hello, lean, good day!" ``` So here a pure component is just a simple function `π → list (html α)`. However, one can augment components with __hooks__. The hooks available for compoenents are listed in the inductive definition for component. Here we will just look at the `with_state` hook, which can be used to build components with inner state. ``` meta inductive my_action | increment | decrement open my_action meta def Counter : component unit α := component.with_state my_action -- the action of the inner component int -- the state (λ _, 0) -- initialise the state (λ _ _ s, s) -- update the state if the props change (λ _ s a, -- update the state if an action was received match a with | increment := (s + 1, none) -- replace `none` with `some _` to emit an action | decrement := (s - 1, none) end ) $ component.pure (λ ⟨state, ⟨⟩⟩, [ button "+" (λ _, increment), to_string state, button "-" (λ _, decrement) ]) #html Counter () ``` You can add many hooks to a component. - `filter_map_action` lets you filter or map actions that are emmitted by the component - `map_props` lets you map the props. - `with_should_update` will not re-render the child component if the given test returns false. This can be useful for efficiency. - `with_state` discussed above.` - `with_mouse` subscribes the component to the mouse state, for example whether or not the mouse is over the component. See the `tests/lean/widget/widget_mouse.lean` test for an example. Given an active document, Lean (in server mode) maintains a set of __widgets__ for the document. A widget is a component `c`, some `p : Props` and an internal state-manager which manages the states of the component and subcomponents and also handles the routing of events from the UI. ## Reconciliation If a parent component's state changes, this can cause child components to change position or to appear and dissappear. However we want to preserve the state of these child components where we can. The UI system will try to match up these child components through a process called __reconciliation__. Reconciliation will make sure that the states are carried over correctly and will also not rerender subcomponents if they haven't changed their props or state. To compute whether two components are the same, the system will perform a hash on their VM objects. Not all VM objects can be hashed, so it's important to make sure that any items that you expect to change over the lifetime of the component are fed through the 'Props' argument. This is why we need the props argument on `component`. The reconciliation engine uses the `props_eq` predicate passed to the component constructor to determine whether the props have changed and hence whether the component should be re-rendered. ## Keys If you have some list of components and the list changes according to some state, it is important to add keys to the components so that if two components change order in the list their states are preserved. If you don't provide keys or there are duplicate keys then you may get some strange behaviour in both the Lean widget engine and react. It is possible to use incorrect HTML tags and attributes, there is (currently) no type checking that the result is a valid piece of HTML. So for example, the client widget system will error if you add a `text_change_event` attribute to anything other than an element tagged with `input`. ## Styles with Tachyons The widget system assumes that a stylesheet called 'tachyons' is present. You can find documentation for this stylesheet at [Tachyons.io](http://tachyons.io/). Tachyons was chosen because it is very terse and allows arbitrary styling without using inline styles and without needing to dynamically load a stylesheet. ## Further work (up for grabs!) - Add type checking for html. - Better error handling when the html tree is malformed. - Better error handling when keys are malformed. - Add a 'with_task' which lets long-running operations (eg running `simp`) not block the UI update. - Timers, animation (ambitious). - More event handlers - Drag and drop support. - The current perf bottleneck is sending the full UI across to the server for every update. Instead, it should be possible to send a smaller [JSON Patch](http://jsonpatch.com). Which is already supported by `json.hpp` and javascript ecosystem. -/ namespace widget inductive mouse_event_kind | on_click | on_mouse_enter | on_mouse_leave /-- An effect is some change that the widget makes outside of its own state. Usually, giving instructions to the editor to perform some task. - `insert_text_relative` will insert at a line relative to the position of the widget. - `insert_text_absolute` will insert text at the precise position given. - `reveal_position` will move the editor to view the given position. - `highlight_position` will add a text highlight to the given position. - `clear_highlighting` will remove all highlights created with `highlight_position`. - `copy_text` will copy the given text to the clipboard. - `custom` can be used to pass custom effects to the client without having to recompile Lean. -/ meta inductive effect : Type | insert_text_absolute (file_name : option string) (p : pos) (text : string) | insert_text_relative (relative_line : int) (text : string) | reveal_position (file_name : option string) (p : pos) | highlight_position (file_name : option string) (p : pos) | clear_highlighting | copy_text (text : string) | custom (key : string) (value : string) meta def effects := list effect meta mutual inductive component, html, attr with component : Type → Type → Type | pure {Props Action : Type} (view : Props → list (html Action)) : component Props Action | filter_map_action {Props InnerAction OuterAction} (action_map : Props → InnerAction → option OuterAction) : component Props InnerAction → component Props OuterAction | map_props {Props1 Props2 Action} (map : Props2 → Props1) : component Props1 Action → component Props2 Action | with_should_update {Props Action : Type} (should_update : Π (old new : Props), bool) : component Props Action → component Props Action | with_state {Props Action : Type} (InnerAction State : Type) (init : Props → State) (props_changed : Props → Props → State → State) (update : Props → State → InnerAction → State × option Action) : component (State × Props) InnerAction → component Props Action | with_effects {Props Action : Type} (emit : Props → Action → effects) : component Props Action → component Props Action with html : Type → Type | element {α : Type} (tag : string) (attrs : list (attr α)) (children : list (html α)) : html α | of_string {α : Type} : string → html α | of_component {α : Type} {Props : Type} : Props → component Props α → html α with attr : Type → Type | val {α : Type} (name : string) (value : json) : attr α | mouse_event {α : Type} (kind : mouse_event_kind) (handler : unit → α) : attr α | style {α : Type} : list (string × string) → attr α | tooltip {α : Type} : html α → attr α | text_change_event {α : Type} (handler : string → α) : attr α variables {α β : Type} {π : Type} namespace component meta def map_action (f : α → β) : component π α → component π β | c := filter_map_action (λ p a, some $ f a) c /-- Returns a component that will never trigger an action. -/ meta def ignore_action : component π α → component π β | c := component.filter_map_action (λ p a, none) c meta def ignore_props : component unit α → component π α | c := with_should_update (λ a b, ff) $ component.map_props (λ p, ()) c meta instance : has_coe (component π empty) (component π α) := ⟨component.filter_map_action (λ p x, none)⟩ meta instance : has_coe_to_fun (component π α) (λ c, π → html α) := ⟨λ c p, html.of_component p c⟩ meta def stateful {π α : Type} (β σ : Type) (init : π → option σ → σ) (update : π → σ → β → σ × option α) (view : π → σ → list (html β)) : component π α := with_state β σ (λ p, init p none) (λ _ p s, init p $ some s) update (component.pure (λ ⟨s,p⟩, view p s)) meta def stateless {π α : Type} [decidable_eq π] (view : π → list (html α)) : component π α := component.with_should_update (λ p1 p2, p1 ≠ p2) $ component.pure view /-- Causes the component to only update on a props change when `test old_props new_props` yields `ff`. -/ meta def with_props_eq (test : π → π → bool) : component π α → component π α | c := component.with_should_update (λ x y, bnot $ test x y) c end component meta mutual def attr.map_action, html.map_action (f : α → β) with attr.map_action : attr α → attr β | (attr.val k v) := attr.val k v | (attr.style s) := attr.style s | (attr.tooltip h) := attr.tooltip $ html.map_action h | (attr.mouse_event k a) := attr.mouse_event k (f ∘ a) | (attr.text_change_event a) := attr.text_change_event (f ∘ a) with html.map_action : html α → html β | (html.element t a c) := html.element t (list.map attr.map_action a) (list.map html.map_action c) | (html.of_string s) := html.of_string s | (html.of_component p c) := html.of_component p $ component.map_action f c meta instance attr.is_functor : functor attr := { map := @attr.map_action } meta instance html.is_functor : functor html := { map := λ _ _, html.map_action } namespace html /-- See Note [use has_coe_t]. -/ meta instance to_string_coe [has_to_string β] : has_coe_t β (html α) := ⟨html.of_string ∘ to_string⟩ meta instance : has_emptyc (html α) := ⟨of_string ""⟩ meta instance list_coe : has_coe (html α) (list (html α)) := ⟨λ x, [x]⟩ end html meta def as_element : html α → option (string × list (attr α) × list (html α)) | (html.element t a c) := some ⟨t,a,c⟩ | _ := none meta def key [has_to_string β] : β → attr α | s := attr.val "key" $ to_string s meta def className : string → attr α | s := attr.val "className" $ s meta def on_click : (unit → α) → attr α | a := attr.mouse_event mouse_event_kind.on_click a meta def on_mouse_enter : (unit → α) → attr α | a := attr.mouse_event mouse_event_kind.on_mouse_enter a meta def on_mouse_leave : (unit → α) → attr α | a := attr.mouse_event mouse_event_kind.on_mouse_leave a /-- Alias for `html.element`. -/ meta def h : string → list (attr α) → list (html α) → html α := html.element /-- Alias for className. -/ meta def cn : string → attr α := className meta def button : string → thunk α → html α | s t := h "button" [on_click t] [s] meta def textbox : string → (string → α) → html α | s t := h "input" [attr.val "type" "text", attr.val "value" s, attr.text_change_event t] [] meta structure select_item (α : Type) := (result : α) (key : string) (view : list (html α)) /-- Choose from a dropdown selection list. -/ meta def select {α} [decidable_eq α] : list (select_item α) → α → html α | items value := let k := match list.filter (λ i, select_item.result i = value) items with | [] := "" | (h::_) := select_item.key h end in h "select" [ attr.val "value" k, attr.val "key" k, attr.text_change_event (λ k, match items.filter (λ i, select_item.key i = k) with | [] := undefined | (h::_) := h.result end )] $ items.map (λ i, h "option" [attr.val "value" i.key] $ select_item.view i) /-- If the html is not an of_element it will wrap it in a div. -/ meta def with_attrs : list (attr α) → html α → html α | a x := match as_element x with | (some ⟨t,as,c⟩) := html.element t (a ++ as) c | none := html.element "div" a [x] end /-- If the html is not an of_element it will wrap it in a div. -/ meta def with_attr : attr α → html α → html α | a x := with_attrs [a] x meta def with_style : string → string → html α → html α | k v h := with_attr (attr.style [(k,v)]) h meta def with_cn : string → html α → html α | s h := with_attr (className s) h meta def with_key {β} [has_to_string β] : β → html α → html α | s h := with_attr (key s) h meta def effect.insert_text : string → effect := effect.insert_text_relative 0 end widget namespace tactic /-- Same as `tactic.save_info_thunk` except saves a widget to be displayed by a compatible infoviewer. -/ meta constant save_widget : pos → widget.component tactic_state empty → tactic unit /-- Outputs a widget trace position at the given position. -/ meta constant trace_widget_at (p : pos) (w : widget.component tactic_state empty) (text := "(widget)") : tactic unit /-- Outputs a widget trace position at the current default trace position. -/ meta def trace_widget (w : widget.component tactic_state empty) (text := "(widget)") : tactic unit := do p ← get_trace_msg_pos, trace_widget_at p w text end tactic
2611f4eeb400a34794cb08df0bdfca91f2bc2c9b
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/data/polynomial/algebra_map.lean
08d08bb3ab3f7b132839a7e30c3e6109694ecfe3
[ "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
7,602
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.eval /-! # Theory of univariate polynomials We show that `polynomial A` is an R-algebra when `A` is an R-algebra. We promote `eval₂` to an algebra hom in `aeval`. -/ noncomputable theory open finset namespace polynomial universes u v w z variables {R : Type u} {S : Type v} {T : Type w} {A : Type z} {a b : R} {n : ℕ} section comm_semiring variables [comm_semiring R] {p q r : polynomial R} variables [semiring A] [algebra R A] /-- Note that this instance also provides `algebra R (polynomial R)`. -/ instance algebra_of_algebra : algebra R (polynomial A) := add_monoid_algebra.algebra lemma algebra_map_apply (r : R) : algebra_map R (polynomial A) r = C (algebra_map R A r) := rfl /-- When we have `[comm_ring R]`, the function `C` is the same as `algebra_map R (polynomial R)`. (But note that `C` is defined when `R` is not necessarily commutative, in which case `algebra_map` is not available.) -/ lemma C_eq_algebra_map {R : Type*} [comm_ring R] (r : R) : C r = algebra_map R (polynomial R) r := rfl @[simp] lemma alg_hom_eval₂_algebra_map {R A B : Type*} [comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B] (p : polynomial R) (f : A →ₐ[R] B) (a : A) : f (eval₂ (algebra_map R A) a p) = eval₂ (algebra_map R B) (f a) p := begin dsimp [eval₂, finsupp.sum], simp only [f.map_sum, f.map_mul, f.map_pow, ring_hom.eq_int_cast, ring_hom.map_int_cast, alg_hom.commutes], end @[simp] lemma eval₂_algebra_map_X {R A : Type*} [comm_ring R] [ring A] [algebra R A] (p : polynomial R) (f : polynomial R →ₐ[R] A) : eval₂ (algebra_map R A) (f X) p = f p := begin conv_rhs { rw [←polynomial.sum_C_mul_X_eq p], }, dsimp [eval₂, finsupp.sum], simp only [f.map_sum, f.map_mul, f.map_pow, ring_hom.eq_int_cast, ring_hom.map_int_cast], simp [polynomial.C_eq_algebra_map], end @[simp] lemma ring_hom_eval₂_algebra_map_int {R S : Type*} [ring R] [ring S] (p : polynomial ℤ) (f : R →+* S) (r : R) : f (eval₂ (algebra_map ℤ R) r p) = eval₂ (algebra_map ℤ S) (f r) p := alg_hom_eval₂_algebra_map p f.to_int_alg_hom r @[simp] lemma eval₂_algebra_map_int_X {R : Type*} [ring R] (p : polynomial ℤ) (f : polynomial ℤ →+* R) : eval₂ (algebra_map ℤ R) (f X) p = f p := -- Unfortunately `f.to_int_alg_hom` doesn't work here, as typeclasses don't match up correctly. eval₂_algebra_map_X p { commutes' := λ n, by simp, .. f } section comp lemma eval₂_comp [comm_semiring S] (f : R →+* S) {x : S} : (p.comp q).eval₂ f x = p.eval₂ f (q.eval₂ f x) := by rw [comp, p.as_sum]; simp only [eval₂_mul, eval₂_C, eval₂_pow, eval₂_finset_sum, eval₂_X] lemma eval_comp : (p.comp q).eval a = p.eval (q.eval a) := eval₂_comp _ instance : is_semiring_hom (λ q : polynomial R, q.comp p) := by unfold comp; apply_instance @[simp] lemma mul_comp : (p * q).comp r = p.comp r * q.comp r := eval₂_mul _ _ end comp end comm_semiring section aeval variables [comm_semiring R] {p : polynomial R} -- TODO this could be generalized: there's no need for `A` to be commutative, -- we just need that `x` is central. variables [comm_semiring A] [algebra R A] variables {B : Type*} [comm_semiring B] [algebra R B] variables (x : A) /-- Given a valuation `x` of the variable in an `R`-algebra `A`, `aeval R A x` is the unique `R`-algebra homomorphism from `R[X]` to `A` sending `X` to `x`. -/ def aeval : polynomial R →ₐ[R] A := { commutes' := λ r, eval₂_C _ _, ..eval₂_ring_hom (algebra_map R A) x } variables {R A} theorem aeval_def (p : polynomial R) : aeval x p = eval₂ (algebra_map R A) x p := rfl @[simp] lemma aeval_X : aeval x (X : polynomial R) = x := eval₂_X _ x @[simp] lemma aeval_C (r : R) : aeval x (C r) = algebra_map R A r := eval₂_C _ x theorem eval_unique (φ : polynomial R →ₐ[R] A) (p) : φ p = eval₂ (algebra_map R A) (φ X) p := begin apply polynomial.induction_on p, { intro r, rw eval₂_C, exact φ.commutes r }, { intros f g ih1 ih2, rw [φ.map_add, ih1, ih2, eval₂_add] }, { intros n r ih, rw [pow_succ', ← mul_assoc, φ.map_mul, eval₂_mul (algebra_map R A), eval₂_X, ih] } end theorem aeval_alg_hom (f : A →ₐ[R] B) (x : A) : aeval (f x) = f.comp (aeval x) := alg_hom.ext $ λ p, by rw [eval_unique (f.comp (aeval x)), alg_hom.comp_apply, aeval_X, aeval_def] theorem aeval_alg_hom_apply (f : A →ₐ[R] B) (x : A) (p : polynomial R) : aeval (f x) p = f (aeval x p) := alg_hom.ext_iff.1 (aeval_alg_hom f x) p @[simp] lemma coe_aeval_eq_eval (r : R) : (aeval r : polynomial R → R) = eval r := rfl lemma coeff_zero_eq_aeval_zero (p : polynomial R) : p.coeff 0 = aeval 0 p := by simp [coeff_zero_eq_eval_zero] lemma pow_comp (p q : polynomial R) (k : ℕ) : (p ^ k).comp q = (p.comp q) ^ k := by { unfold comp, rw ← coe_eval₂_ring_hom, apply ring_hom.map_pow } variables [comm_ring S] {f : R →+* S} lemma is_root_of_eval₂_map_eq_zero (hf : function.injective f) {r : R} : eval₂ f (f r) p = 0 → p.is_root r := begin intro h, apply hf, rw [←eval₂_hom, h, f.map_zero], end lemma is_root_of_aeval_algebra_map_eq_zero [algebra R S] {p : polynomial R} (inj : function.injective (algebra_map R S)) {r : R} (hr : aeval (algebra_map R S r) p = 0) : p.is_root r := is_root_of_eval₂_map_eq_zero inj hr lemma dvd_term_of_dvd_eval_of_dvd_terms {z p : S} {f : polynomial S} (i : ℕ) (dvd_eval : p ∣ f.eval z) (dvd_terms : ∀ (j ≠ i), p ∣ f.coeff j * z ^ j) : p ∣ f.coeff i * z ^ i := begin by_cases hf : f = 0, { simp [hf] }, by_cases hi : i ∈ f.support, { unfold polynomial.eval polynomial.eval₂ finsupp.sum id at dvd_eval, rw [←finset.insert_erase hi, finset.sum_insert (finset.not_mem_erase _ _)] at dvd_eval, refine (dvd_add_left _).mp dvd_eval, apply finset.dvd_sum, intros j hj, exact dvd_terms j (finset.ne_of_mem_erase hj) }, { convert dvd_zero p, convert _root_.zero_mul _, exact finsupp.not_mem_support_iff.mp hi } end lemma dvd_term_of_is_root_of_dvd_terms {r p : S} {f : polynomial S} (i : ℕ) (hr : f.is_root r) (h : ∀ (j ≠ i), p ∣ f.coeff j * r ^ j) : p ∣ f.coeff i * r ^ i := dvd_term_of_dvd_eval_of_dvd_terms i (eq.symm hr ▸ dvd_zero p) h end aeval section ring variables [ring R] /-- The evaluation map is not generally multiplicative when the coefficient ring is noncommutative, but nevertheless any polynomial of the form `p * (X - monomial 0 r)` is sent to zero when evaluated at `r`. This is the key step in our proof of the Cayley-Hamilton theorem. -/ lemma eval_mul_X_sub_C {p : polynomial R} (r : R) : (p * (X - C r)).eval r = 0 := begin simp only [eval, eval₂, ring_hom.id_apply], have bound := calc (p * (X - C r)).nat_degree ≤ p.nat_degree + (X - C r).nat_degree : nat_degree_mul_le ... ≤ p.nat_degree + 1 : add_le_add_left nat_degree_X_sub_C_le _ ... < p.nat_degree + 2 : lt_add_one _, rw sum_over_range' _ _ (p.nat_degree + 2) bound, swap, { simp, }, rw sum_range_succ', conv_lhs { congr, apply_congr, skip, rw [coeff_mul_X_sub_C, sub_mul, mul_assoc, ←pow_succ], }, simp [sum_range_sub', coeff_single], end theorem not_is_unit_X_sub_C [nontrivial R] {r : R} : ¬ is_unit (X - C r) := λ ⟨⟨_, g, hfg, hgf⟩, rfl⟩, @zero_ne_one R _ _ $ by erw [← eval_mul_X_sub_C, hgf, eval_one] end ring end polynomial
c2dcc8305fbb53dd28f4dad79bdf4e1178128f56
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/linear_algebra/clifford_algebra/default.lean
0157342a1887e0eff6682992eb058eef527fbaf4
[ "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
96
lean
import linear_algebra.clifford_algebra.basic import linear_algebra.clifford_algebra.conjugation
974c5aa603ea869e3aedc7bd8f54d3915f63fe12
e8d53a7b78545d183a23dd7bd921bc7ff312989f
/types/prod.hlean
f3c8217e5e8a8e7e24dbc7d80c87247a9feb7461
[]
no_license
Sumit0730/Impredicative
857007626592440a27cf4440aa9a226d0ede7f3e
a75cb9989a684133d31d4889a746ee4fa7b66cea
refs/heads/master
1,631,994,804,745
1,531,980,761,000
1,531,980,761,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,377
hlean
/- 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, Jakob von Raumer Ported from Coq HoTT Theorems about products -/ open eq equiv is_equiv is_trunc prod prod.ops unit variables {A A' B B' C D : Type} {P Q : A → Type} {a a' a'' : A} {b b₁ b₂ b' b'' : B} {u v w : A × B} namespace prod /- Paths in a product space -/ protected definition eta [unfold 3] (u : A × B) : (pr₁ u, pr₂ u) = u := by cases u; reflexivity definition pair_eq [unfold 7 8] (pa : a = a') (pb : b = b') : (a, b) = (a', b') := ap011 prod.mk pa pb definition prod_eq [unfold 3 4 5 6] (H₁ : u.1 = v.1) (H₂ : u.2 = v.2) : u = v := by cases u; cases v; exact pair_eq H₁ H₂ definition eq_pr1 [unfold 5] (p : u = v) : u.1 = v.1 := ap pr1 p definition eq_pr2 [unfold 5] (p : u = v) : u.2 = v.2 := ap pr2 p namespace ops postfix `..1`:(max+1) := eq_pr1 postfix `..2`:(max+1) := eq_pr2 end ops open ops protected definition ap_pr1 (p : u = v) : ap pr1 p = p..1 := idp protected definition ap_pr2 (p : u = v) : ap pr2 p = p..2 := idp definition pair_prod_eq (p : u.1 = v.1) (q : u.2 = v.2) : ((prod_eq p q)..1, (prod_eq p q)..2) = (p, q) := by induction u; induction v; esimp at *; induction p; induction q; reflexivity definition prod_eq_pr1 (p : u.1 = v.1) (q : u.2 = v.2) : (prod_eq p q)..1 = p := (pair_prod_eq p q)..1 definition prod_eq_pr2 (p : u.1 = v.1) (q : u.2 = v.2) : (prod_eq p q)..2 = q := (pair_prod_eq p q)..2 definition prod_eq_eta (p : u = v) : prod_eq (p..1) (p..2) = p := by induction p; induction u; reflexivity -- the uncurried version of prod_eq. We will prove that this is an equivalence definition prod_eq_unc [unfold 5] (H : u.1 = v.1 × u.2 = v.2) : u = v := by cases H with H₁ H₂; exact prod_eq H₁ H₂ definition pair_prod_eq_unc : Π(pq : u.1 = v.1 × u.2 = v.2), ((prod_eq_unc pq)..1, (prod_eq_unc pq)..2) = pq | pair_prod_eq_unc (pq₁, pq₂) := pair_prod_eq pq₁ pq₂ definition prod_eq_unc_pr1 (pq : u.1 = v.1 × u.2 = v.2) : (prod_eq_unc pq)..1 = pq.1 := (pair_prod_eq_unc pq)..1 definition prod_eq_unc_pr2 (pq : u.1 = v.1 × u.2 = v.2) : (prod_eq_unc pq)..2 = pq.2 := (pair_prod_eq_unc pq)..2 definition prod_eq_unc_eta (p : u = v) : prod_eq_unc (p..1, p..2) = p := prod_eq_eta p definition is_equiv_prod_eq [instance] [constructor] (u v : A × B) : is_equiv (prod_eq_unc : u.1 = v.1 × u.2 = v.2 → u = v) := adjointify prod_eq_unc (λp, (p..1, p..2)) prod_eq_unc_eta pair_prod_eq_unc definition prod_eq_equiv [constructor] (u v : A × B) : (u = v) ≃ (u.1 = v.1 × u.2 = v.2) := (equiv.mk prod_eq_unc _)⁻¹ᵉ definition pair_eq_pair_equiv [constructor] (a a' : A) (b b' : B) : ((a, b) = (a', b')) ≃ (a = a' × b = b') := prod_eq_equiv (a, b) (a', b') definition ap_prod_mk_left (p : a = a') : ap (λa, prod.mk a b) p = prod_eq p idp := ap_eq_ap011_left prod.mk p b definition ap_prod_mk_right (p : b = b') : ap (λb, prod.mk a b) p = prod_eq idp p := ap_eq_ap011_right prod.mk a p definition pair_eq_eta {A B : Type} {u v : A × B} (p : u = v) : pair_eq (p..1) (p..2) = prod.eta u ⬝ p ⬝ (prod.eta v)⁻¹ := by induction p; induction u; reflexivity definition prod_eq_eq {A B : Type} {u v : A × B} {p₁ q₁ : u.1 = v.1} {p₂ q₂ : u.2 = v.2} (α₁ : p₁ = q₁) (α₂ : p₂ = q₂) : prod_eq p₁ p₂ = prod_eq q₁ q₂ := by cases α₁; cases α₂; reflexivity definition prod_eq_assemble {A B : Type} {u v : A × B} {p q : u = v} (α₁ : p..1 = q..1) (α₂ : p..2 = q..2) : p = q := (prod_eq_eta p)⁻¹ ⬝ prod.prod_eq_eq α₁ α₂ ⬝ prod_eq_eta q definition eq_pr1_concat {A B : Type} {u v w : A × B} (p : u = v) (q : v = w) : (p ⬝ q)..1 = p..1 ⬝ q..1 := by cases q; reflexivity definition eq_pr2_concat {A B : Type} {u v w : A × B} (p : u = v) (q : v = w) : (p ⬝ q)..2 = p..2 ⬝ q..2 := by cases q; reflexivity /- Groupoid structure -/ definition prod_eq_inv (p : a = a') (q : b = b') : (prod_eq p q)⁻¹ = prod_eq p⁻¹ q⁻¹ := by cases p; cases q; reflexivity definition prod_eq_concat (p : a = a') (p' : a' = a'') (q : b = b') (q' : b' = b'') : prod_eq p q ⬝ prod_eq p' q' = prod_eq (p ⬝ p') (q ⬝ q') := by cases p; cases q; cases p'; cases q'; reflexivity definition prod_eq_concat_idp (p : a = a') (q : b = b') : prod_eq p idp ⬝ prod_eq idp q = prod_eq p q := by cases p; cases q; reflexivity /- Transport -/ definition prod_transport (p : a = a') (u : P a × Q a) : p ▸ u = (p ▸ u.1, p ▸ u.2) := by induction p; induction u; reflexivity definition prod_eq_transport (p : a = a') (q : b = b') {R : A × B → Type} (r : R (a, b)) : (prod_eq p q) ▸ r = p ▸ q ▸ r := by induction p; induction q; reflexivity /- Pathovers -/ definition etao (p : a = a') (bc : P a × Q a) : bc =[p] (p ▸ bc.1, p ▸ bc.2) := by induction p; induction bc; apply idpo definition prod_pathover (p : a = a') (u : P a × Q a) (v : P a' × Q a') (r : u.1 =[p] v.1) (s : u.2 =[p] v.2) : u =[p] v := begin induction u, induction v, esimp at *, induction r, induction s using idp_rec_on, apply idpo end open prod.ops definition prod_pathover_equiv {A : Type} {B C : A → Type} {a a' : A} (p : a = a') (x : B a × C a) (x' : B a' × C a') : x =[p] x' ≃ x.1 =[p] x'.1 × x.2 =[p] x'.2 := begin fapply equiv.MK, { intro q, induction q, constructor: constructor }, { intro v, induction v with q r, exact prod_pathover _ _ _ q r }, { intro v, induction v with q r, induction x with b c, induction x' with b' c', esimp at *, induction q, refine idp_rec_on r _, reflexivity }, { intro q, induction q, induction x with b c, reflexivity } end /- TODO: * define the projections from the type u =[p] v * show that the uncurried version of prod_pathover is an equivalence -/ /- Functorial action -/ variables (f : A → A') (g : B → B') definition prod_functor [unfold 7] (u : A × B) : A' × B' := (f u.1, g u.2) definition ap_prod_functor (p : u.1 = v.1) (q : u.2 = v.2) : ap (prod_functor f g) (prod_eq p q) = prod_eq (ap f p) (ap g q) := by induction u; induction v; esimp at *; induction p; induction q; reflexivity /- Helpers for functions of two arguments -/ definition ap_diagonal {a a' : A} (p : a = a') : ap (λx : A, (x,x)) p = prod_eq p p := by cases p; constructor definition ap_binary (m : A → B → C) (p : a = a') (q : b = b') : ap (λz : A × B, m z.1 z.2) (prod_eq p q) = ap (m a) q ⬝ ap (λx : A, m x b') p := by cases p; cases q; constructor definition ap_prod_elim {A B C : Type} {a a' : A} {b b' : B} (m : A → B → C) (p : a = a') (q : b = b') : ap (prod.rec m) (prod_eq p q) = ap (m a) q ⬝ ap (λx : A, m x b') p := by cases p; cases q; constructor definition ap_prod_elim_idp {A B C : Type} {a a' : A} (m : A → B → C) (p : a = a') (b : B) : ap (prod.rec m) (prod_eq p idp) = ap (λx : A, m x b) p := ap_prod_elim m p idp ⬝ !idp_con /- Equivalences -/ definition is_equiv_prod_functor [instance] [constructor] [H : is_equiv f] [H : is_equiv g] : is_equiv (prod_functor f g) := begin apply adjointify _ (prod_functor f⁻¹ g⁻¹), intro u, induction u, rewrite [▸*,right_inv f,right_inv g], intro u, induction u, rewrite [▸*,left_inv f,left_inv g], end definition prod_equiv_prod_of_is_equiv [constructor] [H : is_equiv f] [H : is_equiv g] : A × B ≃ A' × B' := equiv.mk (prod_functor f g) _ definition prod_equiv_prod [constructor] (f : A ≃ A') (g : B ≃ B') : A × B ≃ A' × B' := equiv.mk (prod_functor f g) _ -- rename definition prod_equiv_prod_left [constructor] (g : B ≃ B') : A × B ≃ A × B' := prod_equiv_prod equiv.rfl g -- rename definition prod_equiv_prod_right [constructor] (f : A ≃ A') : A × B ≃ A' × B := prod_equiv_prod f equiv.rfl /- Symmetry -/ definition is_equiv_flip [instance] [constructor] (A B : Type) : is_equiv (flip : A × B → B × A) := adjointify flip flip (λu, destruct u (λb a, idp)) (λu, destruct u (λa b, idp)) definition prod_comm_equiv [constructor] (A B : Type) : A × B ≃ B × A := equiv.mk flip _ /- Associativity -/ definition prod_assoc_equiv [constructor] (A B C : Type) : A × (B × C) ≃ (A × B) × C := begin fapply equiv.MK, { intro z, induction z with a z, induction z with b c, exact (a, b, c)}, { intro z, induction z with z c, induction z with a b, exact (a, (b, c))}, { intro z, induction z with z c, induction z with a b, reflexivity}, { intro z, induction z with a z, induction z with b c, reflexivity}, end definition prod_contr_equiv [constructor] (A B : Type) [H : is_contr B] : A × B ≃ A := equiv.MK pr1 (λx, (x, !center)) (λx, idp) (λx, by cases x with a b; exact pair_eq idp !center_eq) definition prod_unit_equiv [constructor] (A : Type) : A × unit ≃ A := !prod_contr_equiv definition prod_empty_equiv (A : Type) : A × empty ≃ empty := begin fapply equiv.MK, { intro x, cases x with a e, cases e }, { intro e, cases e }, { intro e, cases e }, { intro x, cases x with a e, cases e } end /- Universal mapping properties -/ definition is_equiv_prod_rec [instance] [constructor] (P : A × B → Type) : is_equiv (prod.rec : (Πa b, P (a, b)) → Πu, P u) := adjointify _ (λg a b, g (a, b)) (λg, eq_of_homotopy (λu, by induction u;reflexivity)) (λf, idp) definition equiv_prod_rec [constructor] (P : A × B → Type) : (Πa b, P (a, b)) ≃ (Πu, P u) := equiv.mk prod.rec _ definition imp_imp_equiv_prod_imp [constructor] (A B C : Type) : (A → B → C) ≃ (A × B → C) := !equiv_prod_rec definition prod_corec_unc [unfold 4] {P Q : A → Type} (u : (Πa, P a) × (Πa, Q a)) (a : A) : P a × Q a := (u.1 a, u.2 a) definition is_equiv_prod_corec [constructor] (P Q : A → Type) : is_equiv (prod_corec_unc : (Πa, P a) × (Πa, Q a) → Πa, P a × Q a) := adjointify _ (λg, (λa, (g a).1, λa, (g a).2)) (by intro g; apply eq_of_homotopy; intro a; esimp; induction (g a); reflexivity) (by intro h; induction h with f g; reflexivity) definition equiv_prod_corec [constructor] (P Q : A → Type) : ((Πa, P a) × (Πa, Q a)) ≃ (Πa, P a × Q a) := equiv.mk _ !is_equiv_prod_corec definition imp_prod_imp_equiv_imp_prod [constructor] (A B C : Type) : (A → B) × (A → C) ≃ (A → (B × C)) := !equiv_prod_corec theorem is_trunc_prod (A B : Type) (n : trunc_index) [HA : is_trunc n A] [HB : is_trunc n B] : is_trunc n (A × B) := begin revert A B HA HB, induction n with n IH, all_goals intro A B HA HB, { fapply is_contr.mk, exact (!center, !center), intro u, apply prod_eq, all_goals apply center_eq}, { apply is_trunc_succ_intro, intro u v, apply is_trunc_equiv_closed_rev, apply prod_eq_equiv, exact IH _ _ _ _} end end prod attribute prod.is_trunc_prod [instance] [priority 1510] namespace prod /- pointed products -/ open pointed definition pointed_prod [instance] [constructor] (A B : Type) [H1 : pointed A] [H2 : pointed B] : pointed (A × B) := pointed.mk (pt,pt) definition pprod [constructor] (A B : Type*) : Type* := pointed.mk' (A × B) infixr ` ×* `:35 := pprod definition ppr1 [constructor] {A B : Type*} : A ×* B →* A := pmap.mk pr1 idp definition ppr2 [constructor] {A B : Type*} : A ×* B →* B := pmap.mk pr2 idp definition tprod [constructor] {n : trunc_index} (A B : n-Type) : n-Type := trunctype.mk (A × B) _ infixr `×t`:30 := tprod definition ptprod [constructor] {n : ℕ₋₂} (A B : n-Type*) : n-Type* := ptrunctype.mk' n (A × B) definition pprod_functor [constructor] {A B C D : Type*} (f : A →* C) (g : B →* D) : A ×* B →* C ×* D := pmap.mk (prod_functor f g) (prod_eq (respect_pt f) (respect_pt g)) end prod
d9286835c861c9580e0a3ddacc8c9d16ded985fb
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/fin_enum.lean
5bb070e8df17f520f83ff41eba388122b7d9c961
[ "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
8,841
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import control.monad.basic import data.fintype.basic import data.list.prod_sigma /-! Type class for finitely enumerable types. The property is stronger than `fintype` in that it assigns each element a rank in a finite enumeration. -/ universes u v open finset /-- `fin_enum α` means that `α` is finite and can be enumerated in some order, i.e. `α` has an explicit bijection with `fin n` for some n. -/ class fin_enum (α : Sort*) := (card : ℕ) (equiv [] : α ≃ fin card) [dec_eq : decidable_eq α] attribute [instance, priority 100] fin_enum.dec_eq namespace fin_enum variables {α : Type u} {β : α → Type v} /-- transport a `fin_enum` instance across an equivalence -/ def of_equiv (α) {β} [fin_enum α] (h : β ≃ α) : fin_enum β := { card := card α, equiv := h.trans (equiv α), dec_eq := (h.trans (equiv _)).decidable_eq } /-- create a `fin_enum` instance from an exhaustive list without duplicates -/ def of_nodup_list [decidable_eq α] (xs : list α) (h : ∀ x : α, x ∈ xs) (h' : list.nodup xs) : fin_enum α := { card := xs.length, equiv := ⟨λ x, ⟨xs.index_of x,by rw [list.index_of_lt_length]; apply h⟩, λ ⟨i,h⟩, xs.nth_le _ h, λ x, by simp [of_nodup_list._match_1], λ ⟨i,h⟩, by simp [of_nodup_list._match_1,*]; rw list.nth_le_index_of; apply list.nodup_dedup ⟩ } /-- create a `fin_enum` instance from an exhaustive list; duplicates are removed -/ def of_list [decidable_eq α] (xs : list α) (h : ∀ x : α, x ∈ xs) : fin_enum α := of_nodup_list xs.dedup (by simp *) (list.nodup_dedup _) /-- create an exhaustive list of the values of a given type -/ def to_list (α) [fin_enum α] : list α := (list.fin_range (card α)).map (equiv α).symm open function @[simp] lemma mem_to_list [fin_enum α] (x : α) : x ∈ to_list α := by simp [to_list]; existsi equiv α x; simp @[simp] lemma nodup_to_list [fin_enum α] : list.nodup (to_list α) := by simp [to_list]; apply list.nodup.map; [apply equiv.injective, apply list.nodup_fin_range] /-- create a `fin_enum` instance using a surjection -/ def of_surjective {β} (f : β → α) [decidable_eq α] [fin_enum β] (h : surjective f) : fin_enum α := of_list ((to_list β).map f) (by intro; simp; exact h _) /-- create a `fin_enum` instance using an injection -/ noncomputable def of_injective {α β} (f : α → β) [decidable_eq α] [fin_enum β] (h : injective f) : fin_enum α := of_list ((to_list β).filter_map (partial_inv f)) begin intro x, simp only [mem_to_list, true_and, list.mem_filter_map], use f x, simp only [h, function.partial_inv_left], end instance pempty : fin_enum pempty := of_list [] (λ x, pempty.elim x) instance empty : fin_enum empty := of_list [] (λ x, empty.elim x) instance punit : fin_enum punit := of_list [punit.star] (λ x, by cases x; simp) instance prod {β} [fin_enum α] [fin_enum β] : fin_enum (α × β) := of_list (to_list α ×ˢ to_list β) (λ x, by cases x; simp) instance sum {β} [fin_enum α] [fin_enum β] : fin_enum (α ⊕ β) := of_list ( (to_list α).map sum.inl ++ (to_list β).map sum.inr ) (λ x, by cases x; simp) instance fin {n} : fin_enum (fin n) := of_list (list.fin_range _) (by simp) instance quotient.enum [fin_enum α] (s : setoid α) [decidable_rel ((≈) : α → α → Prop)] : fin_enum (quotient s) := fin_enum.of_surjective quotient.mk (λ x, quotient.induction_on x (λ x, ⟨x, rfl⟩)) /-- enumerate all finite sets of a given type -/ def finset.enum [decidable_eq α] : list α → list (finset α) | [] := [∅] | (x :: xs) := do r ← finset.enum xs, [r,{x} ∪ r] @[simp]lemma finset.mem_enum [decidable_eq α] (s : finset α) (xs : list α) : s ∈ finset.enum xs ↔ ∀ x ∈ s, x ∈ xs := begin induction xs generalizing s; simp [*,finset.enum], { simp [finset.eq_empty_iff_forall_not_mem,(∉)], refl }, { split, rintro ⟨a,h,h'⟩ x hx, cases h', { right, apply h, subst a, exact hx, }, { simp only [h', mem_union, mem_singleton] at hx ⊢, cases hx, { exact or.inl hx }, { exact or.inr (h _ hx) } }, intro h, existsi s \ ({xs_hd} : finset α), simp only [and_imp, union_comm, mem_sdiff, mem_singleton], simp only [or_iff_not_imp_left] at h, existsi h, by_cases xs_hd ∈ s, { have : {xs_hd} ⊆ s, simp only [has_subset.subset, *, forall_eq, mem_singleton], simp only [union_sdiff_of_subset this, or_true, finset.union_sdiff_of_subset, eq_self_iff_true], }, { left, symmetry, simp only [sdiff_eq_self], intro a, simp only [and_imp, mem_inter, mem_singleton, not_mem_empty], rintro h₀ rfl, apply h h₀, } } end instance finset.fin_enum [fin_enum α] : fin_enum (finset α) := of_list (finset.enum (to_list α)) (by intro; simp) instance subtype.fin_enum [fin_enum α] (p : α → Prop) [decidable_pred p] : fin_enum {x // p x} := of_list ((to_list α).filter_map $ λ x, if h : p x then some ⟨_,h⟩ else none) (by rintro ⟨x,h⟩; simp; existsi x; simp *) instance (β : α → Type v) [fin_enum α] [∀ a, fin_enum (β a)] : fin_enum (sigma β) := of_list ((to_list α).bind $ λ a, (to_list (β a)).map $ sigma.mk a) (by intro x; cases x; simp) instance psigma.fin_enum [fin_enum α] [∀ a, fin_enum (β a)] : fin_enum (Σ' a, β a) := fin_enum.of_equiv _ (equiv.psigma_equiv_sigma _) instance psigma.fin_enum_prop_left {α : Prop} {β : α → Type v} [∀ a, fin_enum (β a)] [decidable α] : fin_enum (Σ' a, β a) := if h : α then of_list ((to_list (β h)).map $ psigma.mk h) (λ ⟨a,Ba⟩, by simp) else of_list [] (λ ⟨a,Ba⟩, (h a).elim) instance psigma.fin_enum_prop_right {β : α → Prop} [fin_enum α] [∀ a, decidable (β a)] : fin_enum (Σ' a, β a) := fin_enum.of_equiv {a // β a} ⟨λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, rfl, λ ⟨x, y⟩, rfl⟩ instance psigma.fin_enum_prop_prop {α : Prop} {β : α → Prop} [decidable α] [∀ a, decidable (β a)] : fin_enum (Σ' a, β a) := if h : ∃ a, β a then of_list [⟨h.fst,h.snd⟩] (by rintro ⟨⟩; simp) else of_list [] (λ a, (h ⟨a.fst,a.snd⟩).elim) @[priority 100] instance [fin_enum α] : fintype α := { elems := univ.map (equiv α).symm.to_embedding, complete := by intros; simp; existsi (equiv α x); simp } /-- For `pi.cons x xs y f` create a function where every `i ∈ xs` is mapped to `f i` and `x` is mapped to `y` -/ def pi.cons [decidable_eq α] (x : α) (xs : list α) (y : β x) (f : Π a, a ∈ xs → β a) : Π a, a ∈ (x :: xs : list α) → β a | b h := if h' : b = x then cast (by rw h') y else f b (list.mem_of_ne_of_mem h' h) /-- Given `f` a function whose domain is `x :: xs`, produce a function whose domain is restricted to `xs`. -/ def pi.tail {x : α} {xs : list α} (f : Π a, a ∈ (x :: xs : list α) → β a) : Π a, a ∈ xs → β a | a h := f a (list.mem_cons_of_mem _ h) /-- `pi xs f` creates the list of functions `g` such that, for `x ∈ xs`, `g x ∈ f x` -/ def pi {β : α → Type (max u v)} [decidable_eq α] : Π xs : list α, (Π a, list (β a)) → list (Π a, a ∈ xs → β a) | [] fs := [λ x h, h.elim] | (x :: xs) fs := fin_enum.pi.cons x xs <$> fs x <*> pi xs fs lemma mem_pi {β : α → Type (max u v)} [fin_enum α] [∀a, fin_enum (β a)] (xs : list α) (f : Π a, a ∈ xs → β a) : f ∈ pi xs (λ x, to_list (β x)) := begin induction xs; simp [pi,-list.map_eq_map] with monad_norm functor_norm, { ext a ⟨ ⟩ }, { existsi pi.cons xs_hd xs_tl (f _ (list.mem_cons_self _ _)), split, exact ⟨_,rfl⟩, existsi pi.tail f, split, { apply xs_ih, }, { ext x h, simp [pi.cons], split_ifs, subst x, refl, refl }, } end /-- enumerate all functions whose domain and range are finitely enumerable -/ def pi.enum (β : α → Type (max u v)) [fin_enum α] [∀a, fin_enum (β a)] : list (Π a, β a) := (pi (to_list α) (λ x, to_list (β x))).map (λ f x, f x (mem_to_list _)) lemma pi.mem_enum {β : α → Type (max u v)} [fin_enum α] [∀a, fin_enum (β a)] (f : Π a, β a) : f ∈ pi.enum β := by simp [pi.enum]; refine ⟨λ a h, f a, mem_pi _ _, rfl⟩ instance pi.fin_enum {β : α → Type (max u v)} [fin_enum α] [∀a, fin_enum (β a)] : fin_enum (Πa, β a) := of_list (pi.enum _) (λ x, pi.mem_enum _) instance pfun_fin_enum (p : Prop) [decidable p] (α : p → Type*) [Π hp, fin_enum (α hp)] : fin_enum (Π hp : p, α hp) := if hp : p then of_list ( (to_list (α hp)).map $ λ x hp', x ) (by intro; simp; exact ⟨x hp,rfl⟩) else of_list [λ hp', (hp hp').elim] (by intro; simp; ext hp'; cases hp hp') end fin_enum
1acefb4b0ff7f47fe106f2aa963ba2562913b9f6
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/ring_theory/dedekind_domain.lean
6f8aef637a01e25aad33b5993d570605d72b60e9
[ "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
44,428
lean
/- Copyright (c) 2020 Kenji Nakagawa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenji Nakagawa, Anne Baanen, Filippo A. E. Nuccio -/ import ring_theory.discrete_valuation_ring import ring_theory.fractional_ideal import ring_theory.ideal.over import ring_theory.integrally_closed import ring_theory.polynomial.rational_root import ring_theory.trace import algebra.associated import algebraic_geometry.prime_spectrum.noetherian /-! # Dedekind domains This file defines the notion of a Dedekind domain (or Dedekind ring), giving three equivalent definitions (TODO: and shows that they are equivalent). ## Main definitions - `is_dedekind_domain` defines a Dedekind domain as a commutative ring that is Noetherian, integrally closed in its field of fractions and has Krull dimension at most one. `is_dedekind_domain_iff` shows that this does not depend on the choice of field of fractions. - `is_dedekind_domain_dvr` alternatively defines a Dedekind domain as an integral domain that is Noetherian, and the localization at every nonzero prime ideal is a DVR. - `is_dedekind_domain_inv` alternatively defines a Dedekind domain as an integral domain where every nonzero fractional ideal is invertible. - `is_dedekind_domain_inv_iff` shows that this does note depend on the choice of field of fractions. ## Implementation notes The definitions that involve a field of fractions choose a canonical field of fractions, but are independent of that choice. The `..._iff` lemmas express this independence. Often, definitions assume that Dedekind domains are not fields. We found it more practical to add a `(h : ¬ is_field A)` assumption whenever this is explicitly needed. ## References * [D. Marcus, *Number Fields*][marcus1977number] * [J.W.S. Cassels, A. Frölich, *Algebraic Number Theory*][cassels1967algebraic] * [J. Neukirch, *Algebraic Number Theory*][Neukirch1992] ## Tags dedekind domain, dedekind ring -/ variables (R A K : Type*) [comm_ring R] [comm_ring A] [field K] open_locale non_zero_divisors /-- A ring `R` has Krull dimension at most one if all nonzero prime ideals are maximal. -/ def ring.dimension_le_one : Prop := ∀ p ≠ (⊥ : ideal R), p.is_prime → p.is_maximal open ideal ring namespace ring lemma dimension_le_one.principal_ideal_ring [is_domain A] [is_principal_ideal_ring A] : dimension_le_one A := λ p nonzero prime, by { haveI := prime, exact is_prime.to_maximal_ideal nonzero } lemma dimension_le_one.is_integral_closure (B : Type*) [comm_ring B] [is_domain B] [nontrivial R] [algebra R A] [algebra R B] [algebra B A] [is_scalar_tower R B A] [is_integral_closure B R A] (h : dimension_le_one R) : dimension_le_one B := λ p ne_bot prime, by exactI is_integral_closure.is_maximal_of_is_maximal_comap A p (h _ (is_integral_closure.comap_ne_bot A ne_bot) infer_instance) lemma dimension_le_one.integral_closure [nontrivial R] [is_domain A] [algebra R A] (h : dimension_le_one R) : dimension_le_one (integral_closure R A) := h.is_integral_closure R A (integral_closure R A) end ring variables [is_domain A] /-- A Dedekind domain is an integral domain that is Noetherian, integrally closed, and has Krull dimension at most one. This is definition 3.2 of [Neukirch1992]. The integral closure condition is independent of the choice of field of fractions: use `is_dedekind_domain_iff` to prove `is_dedekind_domain` for a given `fraction_map`. This is the default implementation, but there are equivalent definitions, `is_dedekind_domain_dvr` and `is_dedekind_domain_inv`. TODO: Prove that these are actually equivalent definitions. -/ class is_dedekind_domain : Prop := (is_noetherian_ring : is_noetherian_ring A) (dimension_le_one : dimension_le_one A) (is_integrally_closed : is_integrally_closed A) -- See library note [lower instance priority] attribute [instance, priority 100] is_dedekind_domain.is_noetherian_ring is_dedekind_domain.is_integrally_closed /-- An integral domain is a Dedekind domain iff and only if it is Noetherian, has dimension ≤ 1, and is integrally closed in a given fraction field. In particular, this definition does not depend on the choice of this fraction field. -/ lemma is_dedekind_domain_iff (K : Type*) [field K] [algebra A K] [is_fraction_ring A K] : is_dedekind_domain A ↔ is_noetherian_ring A ∧ dimension_le_one A ∧ (∀ {x : K}, is_integral A x → ∃ y, algebra_map A K y = x) := ⟨λ ⟨hr, hd, hi⟩, ⟨hr, hd, λ x, (is_integrally_closed_iff K).mp hi⟩, λ ⟨hr, hd, hi⟩, ⟨hr, hd, (is_integrally_closed_iff K).mpr @hi⟩⟩ @[priority 100] -- See library note [lower instance priority] instance is_principal_ideal_ring.is_dedekind_domain [is_principal_ideal_ring A] : is_dedekind_domain A := ⟨principal_ideal_ring.is_noetherian_ring, ring.dimension_le_one.principal_ideal_ring A, unique_factorization_monoid.is_integrally_closed⟩ /-- A Dedekind domain is an integral domain that is Noetherian, and the localization at every nonzero prime is a discrete valuation ring. This is equivalent to `is_dedekind_domain`. TODO: prove the equivalence. -/ structure is_dedekind_domain_dvr : Prop := (is_noetherian_ring : is_noetherian_ring A) (is_dvr_at_nonzero_prime : ∀ P ≠ (⊥ : ideal A), P.is_prime → discrete_valuation_ring (localization.at_prime P)) section inverse variables {R₁ : Type*} [comm_ring R₁] [is_domain R₁] [algebra R₁ K] [is_fraction_ring R₁ K] variables {I J : fractional_ideal R₁⁰ K} noncomputable instance : has_inv (fractional_ideal R₁⁰ K) := ⟨λ I, 1 / I⟩ lemma inv_eq : I⁻¹ = 1 / I := rfl lemma inv_zero' : (0 : fractional_ideal R₁⁰ K)⁻¹ = 0 := fractional_ideal.div_zero lemma inv_nonzero {J : fractional_ideal R₁⁰ K} (h : J ≠ 0) : J⁻¹ = ⟨(1 : fractional_ideal R₁⁰ K) / J, fractional_ideal.fractional_div_of_nonzero h⟩ := fractional_ideal.div_nonzero _ lemma coe_inv_of_nonzero {J : fractional_ideal R₁⁰ K} (h : J ≠ 0) : (↑J⁻¹ : submodule R₁ K) = is_localization.coe_submodule K ⊤ / J := by { rwa inv_nonzero _, refl, assumption } variables {K} lemma mem_inv_iff (hI : I ≠ 0) {x : K} : x ∈ I⁻¹ ↔ ∀ y ∈ I, x * y ∈ (1 : fractional_ideal R₁⁰ K) := fractional_ideal.mem_div_iff_of_nonzero hI lemma inv_anti_mono (hI : I ≠ 0) (hJ : J ≠ 0) (hIJ : I ≤ J) : J⁻¹ ≤ I⁻¹ := λ x, by { simp only [mem_inv_iff hI, mem_inv_iff hJ], exact λ h y hy, h y (hIJ hy) } lemma le_self_mul_inv {I : fractional_ideal R₁⁰ K} (hI : I ≤ (1 : fractional_ideal R₁⁰ K)) : I ≤ I * I⁻¹ := fractional_ideal.le_self_mul_one_div hI variables (K) lemma coe_ideal_le_self_mul_inv (I : ideal R₁) : (I : fractional_ideal R₁⁰ K) ≤ I * I⁻¹ := le_self_mul_inv fractional_ideal.coe_ideal_le_one /-- `I⁻¹` is the inverse of `I` if `I` has an inverse. -/ theorem right_inverse_eq (I J : fractional_ideal R₁⁰ K) (h : I * J = 1) : J = I⁻¹ := begin have hI : I ≠ 0 := fractional_ideal.ne_zero_of_mul_eq_one I J h, suffices h' : I * (1 / I) = 1, { exact (congr_arg units.inv $ @units.ext _ _ (units.mk_of_mul_eq_one _ _ h) (units.mk_of_mul_eq_one _ _ h') rfl) }, apply le_antisymm, { apply fractional_ideal.mul_le.mpr _, intros x hx y hy, rw mul_comm, exact (fractional_ideal.mem_div_iff_of_nonzero hI).mp hy x hx }, rw ← h, apply fractional_ideal.mul_left_mono I, apply (fractional_ideal.le_div_iff_of_nonzero hI).mpr _, intros y hy x hx, rw mul_comm, exact fractional_ideal.mul_mem_mul hx hy end theorem mul_inv_cancel_iff {I : fractional_ideal R₁⁰ K} : I * I⁻¹ = 1 ↔ ∃ J, I * J = 1 := ⟨λ h, ⟨I⁻¹, h⟩, λ ⟨J, hJ⟩, by rwa ← right_inverse_eq K I J hJ⟩ lemma mul_inv_cancel_iff_is_unit {I : fractional_ideal R₁⁰ K} : I * I⁻¹ = 1 ↔ is_unit I := (mul_inv_cancel_iff K).trans is_unit_iff_exists_inv.symm variables {K' : Type*} [field K'] [algebra R₁ K'] [is_fraction_ring R₁ K'] @[simp] lemma map_inv (I : fractional_ideal R₁⁰ K) (h : K ≃ₐ[R₁] K') : (I⁻¹).map (h : K →ₐ[R₁] K') = (I.map h)⁻¹ := by rw [inv_eq, fractional_ideal.map_div, fractional_ideal.map_one, inv_eq] open submodule submodule.is_principal @[simp] lemma span_singleton_inv (x : K) : (fractional_ideal.span_singleton R₁⁰ x)⁻¹ = fractional_ideal.span_singleton _ (x⁻¹) := fractional_ideal.one_div_span_singleton x lemma mul_generator_self_inv {R₁ : Type*} [comm_ring R₁] [algebra R₁ K] [is_localization R₁⁰ K] (I : fractional_ideal R₁⁰ K) [submodule.is_principal (I : submodule R₁ K)] (h : I ≠ 0) : I * fractional_ideal.span_singleton _ (generator (I : submodule R₁ K))⁻¹ = 1 := begin -- Rewrite only the `I` that appears alone. conv_lhs { congr, rw fractional_ideal.eq_span_singleton_of_principal I }, rw [fractional_ideal.span_singleton_mul_span_singleton, mul_inv_cancel, fractional_ideal.span_singleton_one], intro generator_I_eq_zero, apply h, rw [fractional_ideal.eq_span_singleton_of_principal I, generator_I_eq_zero, fractional_ideal.span_singleton_zero] end lemma invertible_of_principal (I : fractional_ideal R₁⁰ K) [submodule.is_principal (I : submodule R₁ K)] (h : I ≠ 0) : I * I⁻¹ = 1 := (fractional_ideal.mul_div_self_cancel_iff).mpr ⟨fractional_ideal.span_singleton _ (generator (I : submodule R₁ K))⁻¹, mul_generator_self_inv _ I h⟩ lemma invertible_iff_generator_nonzero (I : fractional_ideal R₁⁰ K) [submodule.is_principal (I : submodule R₁ K)] : I * I⁻¹ = 1 ↔ generator (I : submodule R₁ K) ≠ 0 := begin split, { intros hI hg, apply fractional_ideal.ne_zero_of_mul_eq_one _ _ hI, rw [fractional_ideal.eq_span_singleton_of_principal I, hg, fractional_ideal.span_singleton_zero] }, { intro hg, apply invertible_of_principal, rw [fractional_ideal.eq_span_singleton_of_principal I], intro hI, have := fractional_ideal.mem_span_singleton_self _ (generator (I : submodule R₁ K)), rw [hI, fractional_ideal.mem_zero_iff] at this, contradiction } end lemma is_principal_inv (I : fractional_ideal R₁⁰ K) [submodule.is_principal (I : submodule R₁ K)] (h : I ≠ 0) : submodule.is_principal (I⁻¹).1 := begin rw [fractional_ideal.val_eq_coe, fractional_ideal.is_principal_iff], use (generator (I : submodule R₁ K))⁻¹, have hI : I * fractional_ideal.span_singleton _ ((generator (I : submodule R₁ K))⁻¹) = 1, apply mul_generator_self_inv _ I h, exact (right_inverse_eq _ I (fractional_ideal.span_singleton _ ((generator (I : submodule R₁ K))⁻¹)) hI).symm end @[simp] lemma fractional_ideal.one_inv : (1⁻¹ : fractional_ideal R₁⁰ K) = 1 := fractional_ideal.div_one /-- A Dedekind domain is an integral domain such that every fractional ideal has an inverse. This is equivalent to `is_dedekind_domain`. In particular we provide a `fractional_ideal.comm_group_with_zero` instance, assuming `is_dedekind_domain A`, which implies `is_dedekind_domain_inv`. For **integral** ideals, `is_dedekind_domain`(`_inv`) implies only `ideal.comm_cancel_monoid_with_zero`. -/ def is_dedekind_domain_inv : Prop := ∀ I ≠ (⊥ : fractional_ideal A⁰ (fraction_ring A)), I * I⁻¹ = 1 open fractional_ideal variables {R A K} lemma is_dedekind_domain_inv_iff [algebra A K] [is_fraction_ring A K] : is_dedekind_domain_inv A ↔ (∀ I ≠ (⊥ : fractional_ideal A⁰ K), I * I⁻¹ = 1) := begin set h := fraction_ring.alg_equiv A K, split; rintros hi I hI, { refine fractional_ideal.map_injective h.symm.to_alg_hom h.symm.injective _, rw [alg_equiv.to_alg_hom_eq_coe, inv_eq, fractional_ideal.map_mul, fractional_ideal.map_one_div, fractional_ideal.map_one, ← inv_eq, hi], exact fractional_ideal.map_ne_zero _ hI }, { refine fractional_ideal.map_injective h.to_alg_hom h.injective _, rw [alg_equiv.to_alg_hom_eq_coe, inv_eq, fractional_ideal.map_mul, fractional_ideal.map_one_div, fractional_ideal.map_one, ← inv_eq, hi], exact fractional_ideal.map_ne_zero _ hI }, end lemma fractional_ideal.adjoin_integral_eq_one_of_is_unit [algebra A K] [is_fraction_ring A K] (x : K) (hx : is_integral A x) (hI : is_unit (adjoin_integral A⁰ x hx)) : adjoin_integral A⁰ x hx = 1 := begin set I := adjoin_integral A⁰ x hx, have mul_self : I * I = I, { apply fractional_ideal.coe_to_submodule_injective, simp }, convert congr_arg (* I⁻¹) mul_self; simp only [(mul_inv_cancel_iff_is_unit K).mpr hI, mul_assoc, mul_one], end namespace is_dedekind_domain_inv variables [algebra A K] [is_fraction_ring A K] (h : is_dedekind_domain_inv A) include h lemma mul_inv_eq_one {I : fractional_ideal A⁰ K} (hI : I ≠ 0) : I * I⁻¹ = 1 := is_dedekind_domain_inv_iff.mp h I hI lemma inv_mul_eq_one {I : fractional_ideal A⁰ K} (hI : I ≠ 0) : I⁻¹ * I = 1 := (mul_comm _ _).trans (h.mul_inv_eq_one hI) protected lemma is_unit {I : fractional_ideal A⁰ K} (hI : I ≠ 0) : is_unit I := is_unit_of_mul_eq_one _ _ (h.mul_inv_eq_one hI) lemma is_noetherian_ring : is_noetherian_ring A := begin refine is_noetherian_ring_iff.mpr ⟨λ (I : ideal A), _⟩, by_cases hI : I = ⊥, { rw hI, apply submodule.fg_bot }, have hI : (I : fractional_ideal A⁰ (fraction_ring A)) ≠ 0 := (coe_to_fractional_ideal_ne_zero (le_refl (non_zero_divisors A))).mpr hI, exact I.fg_of_is_unit (is_fraction_ring.injective A (fraction_ring A)) (h.is_unit hI) end lemma integrally_closed : is_integrally_closed A := begin -- It suffices to show that for integral `x`, -- `A[x]` (which is a fractional ideal) is in fact equal to `A`. refine ⟨λ x hx, _⟩, rw [← set.mem_range, ← algebra.mem_bot, ← subalgebra.mem_to_submodule, algebra.to_submodule_bot, ← coe_span_singleton A⁰ (1 : fraction_ring A), fractional_ideal.span_singleton_one, ← fractional_ideal.adjoin_integral_eq_one_of_is_unit x hx (h.is_unit _)], { exact mem_adjoin_integral_self A⁰ x hx }, { exact λ h, one_ne_zero (eq_zero_iff.mp h 1 (subalgebra.one_mem _)) }, end lemma dimension_le_one : dimension_le_one A := begin -- We're going to show that `P` is maximal because any (maximal) ideal `M` -- that is strictly larger would be `⊤`. rintros P P_ne hP, refine ideal.is_maximal_def.mpr ⟨hP.ne_top, λ M hM, _⟩, -- We may assume `P` and `M` (as fractional ideals) are nonzero. have P'_ne : (P : fractional_ideal A⁰ (fraction_ring A)) ≠ 0 := (coe_to_fractional_ideal_ne_zero (le_refl (non_zero_divisors A))).mpr P_ne, have M'_ne : (M : fractional_ideal A⁰ (fraction_ring A)) ≠ 0 := (coe_to_fractional_ideal_ne_zero (le_refl (non_zero_divisors A))).mpr (lt_of_le_of_lt bot_le hM).ne', -- In particular, we'll show `M⁻¹ * P ≤ P` suffices : (M⁻¹ * P : fractional_ideal A⁰ (fraction_ring A)) ≤ P, { rw [eq_top_iff, ← coe_ideal_le_coe_ideal (fraction_ring A), fractional_ideal.coe_ideal_top], calc (1 : fractional_ideal A⁰ (fraction_ring A)) = _ * _ * _ : _ ... ≤ _ * _ : mul_right_mono (P⁻¹ * M : fractional_ideal A⁰ (fraction_ring A)) this ... = M : _, { rw [mul_assoc, ← mul_assoc ↑P, h.mul_inv_eq_one P'_ne, one_mul, h.inv_mul_eq_one M'_ne] }, { rw [← mul_assoc ↑P, h.mul_inv_eq_one P'_ne, one_mul] }, { apply_instance } }, -- Suppose we have `x ∈ M⁻¹ * P`, then in fact `x = algebra_map _ _ y` for some `y`. intros x hx, have le_one : (M⁻¹ * P : fractional_ideal A⁰ (fraction_ring A)) ≤ 1, { rw [← h.inv_mul_eq_one M'_ne], exact fractional_ideal.mul_left_mono _ ((coe_ideal_le_coe_ideal (fraction_ring A)).mpr hM.le) }, obtain ⟨y, hy, rfl⟩ := (mem_coe_ideal _).mp (le_one hx), -- Since `M` is strictly greater than `P`, let `z ∈ M \ P`. obtain ⟨z, hzM, hzp⟩ := set_like.exists_of_lt hM, -- We have `z * y ∈ M * (M⁻¹ * P) = P`. have zy_mem := fractional_ideal.mul_mem_mul (mem_coe_ideal_of_mem A⁰ hzM) hx, rw [← ring_hom.map_mul, ← mul_assoc, h.mul_inv_eq_one M'_ne, one_mul] at zy_mem, obtain ⟨zy, hzy, zy_eq⟩ := (mem_coe_ideal A⁰).mp zy_mem, rw is_fraction_ring.injective A (fraction_ring A) zy_eq at hzy, -- But `P` is a prime ideal, so `z ∉ P` implies `y ∈ P`, as desired. exact mem_coe_ideal_of_mem A⁰ (or.resolve_left (hP.mem_or_mem hzy) hzp) end /-- Showing one side of the equivalence between the definitions `is_dedekind_domain_inv` and `is_dedekind_domain` of Dedekind domains. -/ theorem is_dedekind_domain : is_dedekind_domain A := ⟨h.is_noetherian_ring, h.dimension_le_one, h.integrally_closed⟩ end is_dedekind_domain_inv variables [algebra A K] [is_fraction_ring A K] /-- Specialization of `exists_prime_spectrum_prod_le_and_ne_bot_of_domain` to Dedekind domains: Let `I : ideal A` be a nonzero ideal, where `A` is a Dedekind domain that is not a field. Then `exists_prime_spectrum_prod_le_and_ne_bot_of_domain` states we can find a product of prime ideals that is contained within `I`. This lemma extends that result by making the product minimal: let `M` be a maximal ideal that contains `I`, then the product including `M` is contained within `I` and the product excluding `M` is not contained within `I`. -/ lemma exists_multiset_prod_cons_le_and_prod_not_le [is_dedekind_domain A] (hNF : ¬ is_field A) {I M : ideal A} (hI0 : I ≠ ⊥) (hIM : I ≤ M) [hM : M.is_maximal] : ∃ (Z : multiset (prime_spectrum A)), (M ::ₘ (Z.map prime_spectrum.as_ideal)).prod ≤ I ∧ ¬ (multiset.prod (Z.map prime_spectrum.as_ideal) ≤ I) := begin -- Let `Z` be a minimal set of prime ideals such that their product is contained in `J`. obtain ⟨Z₀, hZ₀⟩ := prime_spectrum.exists_prime_spectrum_prod_le_and_ne_bot_of_domain hNF hI0, obtain ⟨Z, ⟨hZI, hprodZ⟩, h_eraseZ⟩ := multiset.well_founded_lt.has_min (λ Z, (Z.map prime_spectrum.as_ideal).prod ≤ I ∧ (Z.map prime_spectrum.as_ideal).prod ≠ ⊥) ⟨Z₀, hZ₀⟩, have hZM : multiset.prod (Z.map prime_spectrum.as_ideal) ≤ M := le_trans hZI hIM, have hZ0 : Z ≠ 0, { rintro rfl, simpa [hM.ne_top] using hZM }, obtain ⟨_, hPZ', hPM⟩ := (hM.is_prime.multiset_prod_le (mt multiset.map_eq_zero.mp hZ0)).mp hZM, -- Then in fact there is a `P ∈ Z` with `P ≤ M`. obtain ⟨P, hPZ, rfl⟩ := multiset.mem_map.mp hPZ', letI := classical.dec_eq (ideal A), have := multiset.map_erase prime_spectrum.as_ideal subtype.coe_injective P Z, obtain ⟨hP0, hZP0⟩ : P.as_ideal ≠ ⊥ ∧ ((Z.erase P).map prime_spectrum.as_ideal).prod ≠ ⊥, { rwa [ne.def, ← multiset.cons_erase hPZ', multiset.prod_cons, ideal.mul_eq_bot, not_or_distrib, ← this] at hprodZ }, -- By maximality of `P` and `M`, we have that `P ≤ M` implies `P = M`. have hPM' := (is_dedekind_domain.dimension_le_one _ hP0 P.is_prime).eq_of_le hM.ne_top hPM, tactic.unfreeze_local_instances, subst hPM', -- By minimality of `Z`, erasing `P` from `Z` is exactly what we need. refine ⟨Z.erase P, _, _⟩, { convert hZI, rw [this, multiset.cons_erase hPZ'] }, { refine λ h, h_eraseZ (Z.erase P) ⟨h, _⟩ (multiset.erase_lt.mpr hPZ), exact hZP0 } end namespace fractional_ideal lemma exists_not_mem_one_of_ne_bot [is_dedekind_domain A] (hNF : ¬ is_field A) {I : ideal A} (hI0 : I ≠ ⊥) (hI1 : I ≠ ⊤) : ∃ x : K, x ∈ (I⁻¹ : fractional_ideal A⁰ K) ∧ x ∉ (1 : fractional_ideal A⁰ K) := begin -- WLOG, let `I` be maximal. suffices : ∀ {M : ideal A} (hM : M.is_maximal), ∃ x : K, x ∈ (M⁻¹ : fractional_ideal A⁰ K) ∧ x ∉ (1 : fractional_ideal A⁰ K), { obtain ⟨M, hM, hIM⟩ : ∃ (M : ideal A), is_maximal M ∧ I ≤ M := ideal.exists_le_maximal I hI1, resetI, have hM0 := (M.bot_lt_of_maximal hNF).ne', obtain ⟨x, hxM, hx1⟩ := this hM, refine ⟨x, inv_anti_mono _ _ ((coe_ideal_le_coe_ideal _).mpr hIM) hxM, hx1⟩; apply fractional_ideal.coe_ideal_ne_zero; assumption }, -- Let `a` be a nonzero element of `M` and `J` the ideal generated by `a`. intros M hM, resetI, obtain ⟨⟨a, haM⟩, ha0⟩ := submodule.nonzero_mem_of_bot_lt (M.bot_lt_of_maximal hNF), replace ha0 : a ≠ 0 := subtype.coe_injective.ne ha0, let J : ideal A := ideal.span {a}, have hJ0 : J ≠ ⊥ := mt ideal.span_singleton_eq_bot.mp ha0, have hJM : J ≤ M := ideal.span_le.mpr (set.singleton_subset_iff.mpr haM), have hM0 : ⊥ < M := M.bot_lt_of_maximal hNF, -- Then we can find a product of prime (hence maximal) ideals contained in `J`, -- such that removing element `M` from the product is not contained in `J`. obtain ⟨Z, hle, hnle⟩ := exists_multiset_prod_cons_le_and_prod_not_le hNF hJ0 hJM, -- Choose an element `b` of the product that is not in `J`. obtain ⟨b, hbZ, hbJ⟩ := set_like.not_le_iff_exists.mp hnle, have hnz_fa : algebra_map A K a ≠ 0 := mt ((ring_hom.injective_iff _).mp (is_fraction_ring.injective A K) a) ha0, have hb0 : algebra_map A K b ≠ 0 := mt ((ring_hom.injective_iff _).mp (is_fraction_ring.injective A K) b) (λ h, hbJ $ h.symm ▸ J.zero_mem), -- Then `b a⁻¹ : K` is in `M⁻¹` but not in `1`. refine ⟨algebra_map A K b * (algebra_map A K a)⁻¹, (mem_inv_iff _).mpr _, _⟩, { exact (fractional_ideal.coe_to_fractional_ideal_ne_zero (le_refl _)).mpr hM0.ne' }, { rintro y₀ hy₀, obtain ⟨y, h_Iy, rfl⟩ := (fractional_ideal.mem_coe_ideal _).mp hy₀, rw [mul_comm, ← mul_assoc, ← ring_hom.map_mul], have h_yb : y * b ∈ J, { apply hle, rw multiset.prod_cons, exact submodule.smul_mem_smul h_Iy hbZ }, rw ideal.mem_span_singleton' at h_yb, rcases h_yb with ⟨c, hc⟩, rw [← hc, ring_hom.map_mul, mul_assoc, mul_inv_cancel hnz_fa, mul_one], apply fractional_ideal.coe_mem_one }, { refine mt (fractional_ideal.mem_one_iff _).mp _, rintros ⟨x', h₂_abs⟩, rw [← div_eq_mul_inv, eq_div_iff_mul_eq hnz_fa, ← ring_hom.map_mul] at h₂_abs, have := ideal.mem_span_singleton'.mpr ⟨x', is_fraction_ring.injective A K h₂_abs⟩, contradiction }, end lemma one_mem_inv_coe_ideal {I : ideal A} (hI : I ≠ ⊥) : (1 : K) ∈ (I : fractional_ideal A⁰ K)⁻¹ := begin rw mem_inv_iff (fractional_ideal.coe_ideal_ne_zero hI), intros y hy, rw one_mul, exact coe_ideal_le_one hy, assumption end lemma mul_inv_cancel_of_le_one [h : is_dedekind_domain A] {I : ideal A} (hI0 : I ≠ ⊥) (hI : ((I * I⁻¹)⁻¹ : fractional_ideal A⁰ K) ≤ 1) : (I * I⁻¹ : fractional_ideal A⁰ K) = 1 := begin -- Handle a few trivial cases. by_cases hI1 : I = ⊤, { rw [hI1, coe_ideal_top, one_mul, fractional_ideal.one_inv] }, by_cases hNF : is_field A, { letI := hNF.to_field A, rcases hI1 (I.eq_bot_or_top.resolve_left hI0) }, -- We'll show a contradiction with `exists_not_mem_one_of_ne_bot`: -- `J⁻¹ = (I * I⁻¹)⁻¹` cannot have an element `x ∉ 1`, so it must equal `1`. obtain ⟨J, hJ⟩ : ∃ (J : ideal A), (J : fractional_ideal A⁰ K) = I * I⁻¹ := le_one_iff_exists_coe_ideal.mp mul_one_div_le_one, by_cases hJ0 : J = ⊥, { subst hJ0, refine absurd _ hI0, rw [eq_bot_iff, ← coe_ideal_le_coe_ideal K, hJ], exact coe_ideal_le_self_mul_inv K I, apply_instance }, by_cases hJ1 : J = ⊤, { rw [← hJ, hJ1, coe_ideal_top] }, obtain ⟨x, hx, hx1⟩ : ∃ (x : K), x ∈ (J : fractional_ideal A⁰ K)⁻¹ ∧ x ∉ (1 : fractional_ideal A⁰ K) := exists_not_mem_one_of_ne_bot hNF hJ0 hJ1, contrapose! hx1 with h_abs, rw hJ at hx, exact hI hx, end /-- Nonzero integral ideals in a Dedekind domain are invertible. We will use this to show that nonzero fractional ideals are invertible, and finally conclude that fractional ideals in a Dedekind domain form a group with zero. -/ lemma coe_ideal_mul_inv [h : is_dedekind_domain A] (I : ideal A) (hI0 : I ≠ ⊥) : (I * I⁻¹ : fractional_ideal A⁰ K) = 1 := begin -- We'll show `1 ≤ J⁻¹ = (I * I⁻¹)⁻¹ ≤ 1`. apply mul_inv_cancel_of_le_one hI0, by_cases hJ0 : (I * I⁻¹ : fractional_ideal A⁰ K) = 0, { rw [hJ0, inv_zero'], exact fractional_ideal.zero_le _ }, intros x hx, -- In particular, we'll show all `x ∈ J⁻¹` are integral. suffices : x ∈ integral_closure A K, { rwa [is_integrally_closed.integral_closure_eq_bot, algebra.mem_bot, set.mem_range, ← fractional_ideal.mem_one_iff] at this; assumption }, -- For that, we'll find a subalgebra that is f.g. as a module and contains `x`. -- `A` is a noetherian ring, so we just need to find a subalgebra between `{x}` and `I⁻¹`. rw mem_integral_closure_iff_mem_fg, have x_mul_mem : ∀ b ∈ (I⁻¹ : fractional_ideal A⁰ K), x * b ∈ (I⁻¹ : fractional_ideal A⁰ K), { intros b hb, rw mem_inv_iff at ⊢ hx, swap, { exact fractional_ideal.coe_ideal_ne_zero hI0 }, swap, { exact hJ0 }, simp only [mul_assoc, mul_comm b] at ⊢ hx, intros y hy, exact hx _ (fractional_ideal.mul_mem_mul hy hb) }, -- It turns out the subalgebra consisting of all `p(x)` for `p : polynomial A` works. refine ⟨alg_hom.range (polynomial.aeval x : polynomial A →ₐ[A] K), is_noetherian_submodule.mp (fractional_ideal.is_noetherian I⁻¹) _ (λ y hy, _), ⟨polynomial.X, polynomial.aeval_X x⟩⟩, obtain ⟨p, rfl⟩ := (alg_hom.mem_range _).mp hy, rw polynomial.aeval_eq_sum_range, refine submodule.sum_mem _ (λ i hi, submodule.smul_mem _ _ _), clear hi, induction i with i ih, { rw pow_zero, exact one_mem_inv_coe_ideal hI0 }, { show x ^ i.succ ∈ (I⁻¹ : fractional_ideal A⁰ K), rw pow_succ, exact x_mul_mem _ ih }, end /-- Nonzero fractional ideals in a Dedekind domain are units. This is also available as `_root_.mul_inv_cancel`, using the `comm_group_with_zero` instance defined below. -/ protected theorem mul_inv_cancel [is_dedekind_domain A] {I : fractional_ideal A⁰ K} (hne : I ≠ 0) : I * I⁻¹ = 1 := begin obtain ⟨a, J, ha, hJ⟩ : ∃ (a : A) (aI : ideal A), a ≠ 0 ∧ I = span_singleton A⁰ (algebra_map _ _ a)⁻¹ * aI := exists_eq_span_singleton_mul I, suffices h₂ : I * (span_singleton A⁰ (algebra_map _ _ a) * J⁻¹) = 1, { rw mul_inv_cancel_iff, exact ⟨span_singleton A⁰ (algebra_map _ _ a) * J⁻¹, h₂⟩ }, subst hJ, rw [mul_assoc, mul_left_comm (J : fractional_ideal A⁰ K), coe_ideal_mul_inv, mul_one, fractional_ideal.span_singleton_mul_span_singleton, inv_mul_cancel, fractional_ideal.span_singleton_one], { exact mt ((algebra_map A K).injective_iff.mp (is_fraction_ring.injective A K) _) ha }, { exact fractional_ideal.coe_ideal_ne_zero_iff.mp (right_ne_zero_of_mul hne) } end lemma mul_right_le_iff [is_dedekind_domain A] {J : fractional_ideal A⁰ K} (hJ : J ≠ 0) : ∀ {I I'}, I * J ≤ I' * J ↔ I ≤ I' := begin intros I I', split, { intros h, convert mul_right_mono J⁻¹ h; rw [mul_assoc, fractional_ideal.mul_inv_cancel hJ, mul_one] }, { exact λ h, mul_right_mono J h } end lemma mul_left_le_iff [is_dedekind_domain A] {J : fractional_ideal A⁰ K} (hJ : J ≠ 0) {I I'} : J * I ≤ J * I' ↔ I ≤ I' := by convert fractional_ideal.mul_right_le_iff hJ using 1; simp only [mul_comm] lemma mul_right_strict_mono [is_dedekind_domain A] {I : fractional_ideal A⁰ K} (hI : I ≠ 0) : strict_mono (* I) := strict_mono_of_le_iff_le (λ _ _, (mul_right_le_iff hI).symm) lemma mul_left_strict_mono [is_dedekind_domain A] {I : fractional_ideal A⁰ K} (hI : I ≠ 0) : strict_mono ((*) I) := strict_mono_of_le_iff_le (λ _ _, (mul_left_le_iff hI).symm) /-- This is also available as `_root_.div_eq_mul_inv`, using the `comm_group_with_zero` instance defined below. -/ protected lemma div_eq_mul_inv [is_dedekind_domain A] (I J : fractional_ideal A⁰ K) : I / J = I * J⁻¹ := begin by_cases hJ : J = 0, { rw [hJ, div_zero, inv_zero', mul_zero] }, refine le_antisymm ((mul_right_le_iff hJ).mp _) ((le_div_iff_mul_le hJ).mpr _), { rw [mul_assoc, mul_comm J⁻¹, fractional_ideal.mul_inv_cancel hJ, mul_one, mul_le], intros x hx y hy, rw [mem_div_iff_of_nonzero hJ] at hx, exact hx y hy }, rw [mul_assoc, mul_comm J⁻¹, fractional_ideal.mul_inv_cancel hJ, mul_one], exact le_refl I end end fractional_ideal /-- `is_dedekind_domain` and `is_dedekind_domain_inv` are equivalent ways to express that an integral domain is a Dedekind domain. -/ theorem is_dedekind_domain_iff_is_dedekind_domain_inv : is_dedekind_domain A ↔ is_dedekind_domain_inv A := ⟨λ h I hI, by exactI fractional_ideal.mul_inv_cancel hI, λ h, h.is_dedekind_domain⟩ end inverse section is_dedekind_domain variables {R A} [is_dedekind_domain A] [algebra A K] [is_fraction_ring A K] open fractional_ideal noncomputable instance fractional_ideal.comm_group_with_zero : comm_group_with_zero (fractional_ideal A⁰ K) := { inv := λ I, I⁻¹, inv_zero := inv_zero' _, div := (/), div_eq_mul_inv := fractional_ideal.div_eq_mul_inv, exists_pair_ne := ⟨0, 1, (coe_to_fractional_ideal_injective (le_refl _)).ne (by simpa using @zero_ne_one (ideal A) _ _)⟩, mul_inv_cancel := λ I, fractional_ideal.mul_inv_cancel, .. fractional_ideal.comm_semiring } noncomputable instance ideal.comm_cancel_monoid_with_zero : comm_cancel_monoid_with_zero (ideal A) := function.injective.comm_cancel_monoid_with_zero (coe_ideal_hom A⁰ (fraction_ring A)) coe_ideal_injective (ring_hom.map_zero _) (ring_hom.map_one _) (ring_hom.map_mul _) /-- For ideals in a Dedekind domain, to divide is to contain. -/ lemma ideal.dvd_iff_le {I J : ideal A} : (I ∣ J) ↔ J ≤ I := ⟨ideal.le_of_dvd, λ h, begin by_cases hI : I = ⊥, { have hJ : J = ⊥, { rwa [hI, ← eq_bot_iff] at h }, rw [hI, hJ] }, have hI' : (I : fractional_ideal A⁰ (fraction_ring A)) ≠ 0 := (fractional_ideal.coe_to_fractional_ideal_ne_zero (le_refl (non_zero_divisors A))).mpr hI, have : (I : fractional_ideal A⁰ (fraction_ring A))⁻¹ * J ≤ 1 := le_trans (fractional_ideal.mul_left_mono (↑I)⁻¹ ((coe_ideal_le_coe_ideal _).mpr h)) (le_of_eq (inv_mul_cancel hI')), obtain ⟨H, hH⟩ := fractional_ideal.le_one_iff_exists_coe_ideal.mp this, use H, refine coe_to_fractional_ideal_injective (le_refl (non_zero_divisors A)) (show (J : fractional_ideal A⁰ (fraction_ring A)) = _, from _), rw [fractional_ideal.coe_ideal_mul, hH, ← mul_assoc, mul_inv_cancel hI', one_mul] end⟩ lemma ideal.dvd_not_unit_iff_lt {I J : ideal A} : dvd_not_unit I J ↔ J < I := ⟨λ ⟨hI, H, hunit, hmul⟩, lt_of_le_of_ne (ideal.dvd_iff_le.mp ⟨H, hmul⟩) (mt (λ h, have H = 1, from mul_left_cancel₀ hI (by rw [← hmul, h, mul_one]), show is_unit H, from this.symm ▸ is_unit_one) hunit), λ h, dvd_not_unit_of_dvd_of_not_dvd (ideal.dvd_iff_le.mpr (le_of_lt h)) (mt ideal.dvd_iff_le.mp (not_le_of_lt h))⟩ instance : wf_dvd_monoid (ideal A) := { well_founded_dvd_not_unit := have well_founded ((>) : ideal A → ideal A → Prop) := is_noetherian_iff_well_founded.mp (is_noetherian_ring_iff.mp is_dedekind_domain.is_noetherian_ring), by { convert this, ext, rw ideal.dvd_not_unit_iff_lt } } instance ideal.unique_factorization_monoid : unique_factorization_monoid (ideal A) := { irreducible_iff_prime := λ P, ⟨λ hirr, ⟨hirr.ne_zero, hirr.not_unit, λ I J, begin have : P.is_maximal, { refine ⟨⟨mt ideal.is_unit_iff.mpr hirr.not_unit, _⟩⟩, intros J hJ, obtain ⟨J_ne, H, hunit, P_eq⟩ := ideal.dvd_not_unit_iff_lt.mpr hJ, exact ideal.is_unit_iff.mp ((hirr.is_unit_or_is_unit P_eq).resolve_right hunit) }, rw [ideal.dvd_iff_le, ideal.dvd_iff_le, ideal.dvd_iff_le, set_like.le_def, set_like.le_def, set_like.le_def], contrapose!, rintros ⟨⟨x, x_mem, x_not_mem⟩, ⟨y, y_mem, y_not_mem⟩⟩, exact ⟨x * y, ideal.mul_mem_mul x_mem y_mem, mt this.is_prime.mem_or_mem (not_or x_not_mem y_not_mem)⟩, end⟩, prime.irreducible⟩, .. ideal.wf_dvd_monoid } noncomputable instance ideal.normalization_monoid : normalization_monoid (ideal A) := normalization_monoid_of_unique_units @[simp] lemma ideal.dvd_span_singleton {I : ideal A} {x : A} : I ∣ ideal.span {x} ↔ x ∈ I := ideal.dvd_iff_le.trans (ideal.span_le.trans set.singleton_subset_iff) lemma ideal.is_prime_of_prime {P : ideal A} (h : prime P) : is_prime P := begin refine ⟨_, λ x y hxy, _⟩, { unfreezingI { rintro rfl }, rw ← ideal.one_eq_top at h, exact h.not_unit is_unit_one }, { simp only [← ideal.dvd_span_singleton, ← ideal.span_singleton_mul_span_singleton] at ⊢ hxy, exact h.dvd_or_dvd hxy } end theorem ideal.prime_of_is_prime {P : ideal A} (hP : P ≠ ⊥) (h : is_prime P) : prime P := begin refine ⟨hP, mt ideal.is_unit_iff.mp h.ne_top, λ I J hIJ, _⟩, simpa only [ideal.dvd_iff_le] using (h.mul_le.mp (ideal.le_of_dvd hIJ)), end /-- In a Dedekind domain, the (nonzero) prime elements of the monoid with zero `ideal A` are exactly the prime ideals. -/ theorem ideal.prime_iff_is_prime {P : ideal A} (hP : P ≠ ⊥) : prime P ↔ is_prime P := ⟨ideal.is_prime_of_prime, ideal.prime_of_is_prime hP⟩ end is_dedekind_domain section is_integral_closure /-! ### `is_integral_closure` section We show that an integral closure of a Dedekind domain in a finite separable field extension is again a Dedekind domain. This implies the ring of integers of a number field is a Dedekind domain. -/ open algebra open_locale big_operators variables {A K} [algebra A K] [is_fraction_ring A K] variables {L : Type*} [field L] (C : Type*) [comm_ring C] variables [algebra K L] [finite_dimensional K L] [algebra A L] [is_scalar_tower A K L] variables [algebra C L] [is_integral_closure C A L] [algebra A C] [is_scalar_tower A C L] lemma is_integral_closure.range_le_span_dual_basis [is_separable K L] {ι : Type*} [fintype ι] [decidable_eq ι] (b : basis ι K L) (hb_int : ∀ i, is_integral A (b i)) [is_integrally_closed A] : ((algebra.linear_map C L).restrict_scalars A).range ≤ submodule.span A (set.range $ (trace_form K L).dual_basis (trace_form_nondegenerate K L) b) := begin let db := (trace_form K L).dual_basis (trace_form_nondegenerate K L) b, rintros _ ⟨x, rfl⟩, simp only [linear_map.coe_restrict_scalars_eq_coe, algebra.linear_map_apply], have hx : is_integral A (algebra_map C L x) := (is_integral_closure.is_integral A L x).algebra_map, suffices : ∃ (c : ι → A), algebra_map C L x = ∑ i, c i • db i, { obtain ⟨c, x_eq⟩ := this, rw x_eq, refine submodule.sum_mem _ (λ i _, submodule.smul_mem _ _ (submodule.subset_span _)), rw set.mem_range, exact ⟨i, rfl⟩ }, suffices : ∃ (c : ι → K), ((∀ i, is_integral A (c i)) ∧ algebra_map C L x = ∑ i, c i • db i), { obtain ⟨c, hc, hx⟩ := this, have hc' : ∀ i, is_localization.is_integer A (c i) := λ i, is_integrally_closed.is_integral_iff.mp (hc i), use λ i, classical.some (hc' i), refine hx.trans (finset.sum_congr rfl (λ i _, _)), conv_lhs { rw [← classical.some_spec (hc' i)] }, rw [← is_scalar_tower.algebra_map_smul K (classical.some (hc' i)) (db i)] }, refine ⟨λ i, db.repr (algebra_map C L x) i, (λ i, _), (db.sum_repr _).symm⟩, rw bilin_form.dual_basis_repr_apply, exact is_integral_trace (is_integral_mul hx (hb_int i)) end lemma integral_closure_le_span_dual_basis [is_separable K L] {ι : Type*} [fintype ι] [decidable_eq ι] (b : basis ι K L) (hb_int : ∀ i, is_integral A (b i)) [is_integrally_closed A] : (integral_closure A L).to_submodule ≤ submodule.span A (set.range $ (trace_form K L).dual_basis (trace_form_nondegenerate K L) b) := begin refine le_trans _ (is_integral_closure.range_le_span_dual_basis (integral_closure A L) b hb_int), intros x hx, exact ⟨⟨x, hx⟩, rfl⟩ end variables (A) (K) include K /-- Send a set of `x`'es in a finite extension `L` of the fraction field of `R` to `(y : R) • x ∈ integral_closure R L`. -/ lemma exists_integral_multiples (s : finset L) : ∃ (y ≠ (0 : A)), ∀ x ∈ s, is_integral A (y • x) := begin haveI := classical.dec_eq L, refine s.induction _ _, { use [1, one_ne_zero], rintros x ⟨⟩ }, { rintros x s hx ⟨y, hy, hs⟩, obtain ⟨x', y', hy', hx'⟩ := exists_integral_multiple ((is_fraction_ring.is_algebraic_iff A K).mpr (algebra.is_algebraic_of_finite x)) ((algebra_map A L).injective_iff.mp _), refine ⟨y * y', mul_ne_zero hy hy', λ x'' hx'', _⟩, rcases finset.mem_insert.mp hx'' with (rfl | hx''), { rw [mul_smul, algebra.smul_def, algebra.smul_def, mul_comm _ x'', hx'], exact is_integral_mul is_integral_algebra_map x'.2 }, { rw [mul_comm, mul_smul, algebra.smul_def], exact is_integral_mul is_integral_algebra_map (hs _ hx'') }, { rw is_scalar_tower.algebra_map_eq A K L, apply (algebra_map K L).injective.comp, exact is_fraction_ring.injective _ _ } } end variables (L) /-- If `L` is a finite extension of `K = Frac(A)`, then `L` has a basis over `A` consisting of integral elements. -/ lemma finite_dimensional.exists_is_basis_integral : ∃ (s : finset L) (b : basis s K L), (∀ x, is_integral A (b x)) := begin letI := classical.dec_eq L, letI : is_noetherian K L := is_noetherian.iff_fg.2 infer_instance, let s' := is_noetherian.finset_basis_index K L, let bs' := is_noetherian.finset_basis K L, obtain ⟨y, hy, his'⟩ := exists_integral_multiples A K (finset.univ.image bs'), have hy' : algebra_map A L y ≠ 0, { refine mt ((algebra_map A L).injective_iff.mp _ _) hy, rw is_scalar_tower.algebra_map_eq A K L, exact (algebra_map K L).injective.comp (is_fraction_ring.injective A K) }, refine ⟨s', bs'.map { to_fun := λ x, algebra_map A L y * x, inv_fun := λ x, (algebra_map A L y)⁻¹ * x, left_inv := _, right_inv := _, .. algebra.lmul _ _ (algebra_map A L y) }, _⟩, { intros x, simp only [inv_mul_cancel_left₀ hy'] }, { intros x, simp only [mul_inv_cancel_left₀ hy'] }, { rintros ⟨x', hx'⟩, simp only [algebra.smul_def, finset.mem_image, exists_prop, finset.mem_univ, true_and] at his', simp only [basis.map_apply, linear_equiv.coe_mk], exact his' _ ⟨_, rfl⟩ } end variables (A K L) [is_separable K L] include L /- If `L` is a finite separable extension of `K = Frac(A)`, where `A` is integrally closed and Noetherian, the integral closure `C` of `A` in `L` is Noetherian. -/ lemma is_integral_closure.is_noetherian_ring [is_integrally_closed A] [is_noetherian_ring A] : is_noetherian_ring C := begin haveI := classical.dec_eq L, obtain ⟨s, b, hb_int⟩ := finite_dimensional.exists_is_basis_integral A K L, rw is_noetherian_ring_iff, let b' := (trace_form K L).dual_basis (trace_form_nondegenerate K L) b, letI := is_noetherian_span_of_finite A (set.finite_range b'), let f : C →ₗ[A] submodule.span A (set.range b') := (submodule.of_le (is_integral_closure.range_le_span_dual_basis C b hb_int)).comp ((algebra.linear_map C L).restrict_scalars A).range_restrict, refine is_noetherian_of_tower A (is_noetherian_of_ker_bot f _), rw [linear_map.ker_comp, submodule.ker_of_le, submodule.comap_bot, linear_map.ker_cod_restrict], exact linear_map.ker_eq_bot_of_injective (is_integral_closure.algebra_map_injective C A L) end variables {A K} /- If `L` is a finite separable extension of `K = Frac(A)`, where `A` is integrally closed and Noetherian, the integral closure of `A` in `L` is Noetherian. -/ lemma integral_closure.is_noetherian_ring [is_integrally_closed A] [is_noetherian_ring A] : is_noetherian_ring (integral_closure A L) := is_integral_closure.is_noetherian_ring A K L (integral_closure A L) variables (A K) [is_domain C] /- If `L` is a finite separable extension of `K = Frac(A)`, where `A` is a Dedekind domain, the integral closure `C` of `A` in `L` is a Dedekind domain. Can't be an instance since `A`, `K` or `L` can't be inferred. See also the instance `integral_closure.is_dedekind_domain_fraction_ring` where `K := fraction_ring A` and `C := integral_closure A L`. -/ lemma is_integral_closure.is_dedekind_domain [h : is_dedekind_domain A] : is_dedekind_domain C := begin haveI : is_fraction_ring C L := is_integral_closure.is_fraction_ring_of_finite_extension A K L C, exact ⟨is_integral_closure.is_noetherian_ring A K L C, h.dimension_le_one.is_integral_closure _ L _, (is_integrally_closed_iff L).mpr (λ x hx, ⟨is_integral_closure.mk' C x (is_integral_trans (is_integral_closure.is_integral_algebra A L) _ hx), is_integral_closure.algebra_map_mk' _ _ _⟩)⟩ end /- If `L` is a finite separable extension of `K = Frac(A)`, where `A` is a Dedekind domain, the integral closure of `A` in `L` is a Dedekind domain. Can't be an instance since `K` can't be inferred. See also the instance `integral_closure.is_dedekind_domain_fraction_ring` where `K := fraction_ring A`. -/ lemma integral_closure.is_dedekind_domain [h : is_dedekind_domain A] : is_dedekind_domain (integral_closure A L) := is_integral_closure.is_dedekind_domain A K L (integral_closure A L) omit K variables [algebra (fraction_ring A) L] [is_scalar_tower A (fraction_ring A) L] variables [finite_dimensional (fraction_ring A) L] [is_separable (fraction_ring A) L] /- If `L` is a finite separable extension of `Frac(A)`, where `A` is a Dedekind domain, the integral closure of `A` in `L` is a Dedekind domain. See also the lemma `integral_closure.is_dedekind_domain` where you can choose the field of fractions yourself. -/ instance integral_closure.is_dedekind_domain_fraction_ring [is_dedekind_domain A] : is_dedekind_domain (integral_closure A L) := integral_closure.is_dedekind_domain A (fraction_ring A) L end is_integral_closure section is_dedekind_domain variables {T : Type*} [comm_ring T] [is_domain T] [is_dedekind_domain T] (I J : ideal T) open_locale classical open multiset unique_factorization_monoid ideal lemma prod_normalized_factors_eq_self {I : ideal T} (hI : I ≠ ⊥) : (normalized_factors I).prod = I := associated_iff_eq.1 (normalized_factors_prod hI) lemma normalized_factors_prod {α : multiset (ideal T)} (h : ∀ p ∈ α, prime p) : normalized_factors α.prod = α := by { simp_rw [← multiset.rel_eq, ← associated_eq_eq], exact prime_factors_unique (prime_of_normalized_factor) h (normalized_factors_prod (α.prod_ne_zero_of_prime h)) } lemma count_le_of_ideal_ge {I J : ideal T} (h : I ≤ J) (hI : I ≠ ⊥) (K : ideal T) : count K (normalized_factors J) ≤ count K (normalized_factors I) := le_iff_count.1 ((dvd_iff_normalized_factors_le_normalized_factors (ne_bot_of_le_ne_bot hI h) hI).1 (dvd_iff_le.2 h)) _ lemma sup_eq_prod_inf_factors (hI : I ≠ ⊥) (hJ : J ≠ ⊥) : I ⊔ J = (normalized_factors I ∩ normalized_factors J).prod := begin have H : normalized_factors (normalized_factors I ∩ normalized_factors J).prod = normalized_factors I ∩ normalized_factors J, { apply _root_.normalized_factors_prod, intros p hp, rw mem_inter at hp, exact prime_of_normalized_factor p hp.left }, have := (multiset.prod_ne_zero_of_prime (normalized_factors I ∩ normalized_factors J) (λ _ h, prime_of_normalized_factor _ (multiset.mem_inter.1 h).1)), apply le_antisymm, { rw [sup_le_iff, ← dvd_iff_le, ← dvd_iff_le], split, { rw [dvd_iff_normalized_factors_le_normalized_factors this hI, H], exact inf_le_left }, { rw [dvd_iff_normalized_factors_le_normalized_factors this hJ, H], exact inf_le_right } }, { rw [← dvd_iff_le, dvd_iff_normalized_factors_le_normalized_factors, _root_.normalized_factors_prod, le_iff_count], { intro a, rw multiset.count_inter, exact le_min (count_le_of_ideal_ge le_sup_left hI a) (count_le_of_ideal_ge le_sup_right hJ a) }, { intros p hp, rw mem_inter at hp, exact prime_of_normalized_factor p hp.left }, { exact ne_bot_of_le_ne_bot hI le_sup_left }, { exact this } }, end end is_dedekind_domain
f767ed0220264f675a13d262fa56c840f55907f3
9dc8cecdf3c4634764a18254e94d43da07142918
/src/ring_theory/dedekind_domain/adic_valuation.lean
68fb94cfe8eb23c75b8f05001b9b78a5c5bbcb04
[ "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,657
lean
/- Copyright (c) 2022 María Inés de Frutos-Fernández. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: María Inés de Frutos-Fernández -/ import ring_theory.dedekind_domain.ideal import ring_theory.valuation.extend_to_localization import ring_theory.valuation.valuation_subring import topology.algebra.valued_field /-! # Adic valuations on Dedekind domains Given a Dedekind domain `R` of Krull dimension 1 and a maximal ideal `v` of `R`, we define the `v`-adic valuation on `R` and its extension to the field of fractions `K` of `R`. We prove several properties of this valuation, including the existence of uniformizers. We define the completion of `K` with respect to the `v`-adic valuation, denoted `v.adic_completion`,and its ring of integers, denoted `v.adic_completion_integers`. ## Main definitions - `is_dedekind_domain.height_one_spectrum.int_valuation v` is the `v`-adic valuation on `R`. - `is_dedekind_domain.height_one_spectrum.valuation v` is the `v`-adic valuation on `K`. - `is_dedekind_domain.height_one_spectrum.adic_completion v` is the completion of `K` with respect to its `v`-adic valuation. - `is_dedekind_domain.height_one_spectrum.adic_completion_integers v` is the ring of integers of `v.adic_completion`. ## Main results - `is_dedekind_domain.height_one_spectrum.int_valuation_le_one` : The `v`-adic valuation on `R` is bounded above by 1. - `is_dedekind_domain.height_one_spectrum.int_valuation_lt_one_iff_dvd` : The `v`-adic valuation of `r ∈ R` is less than 1 if and only if `v` divides the ideal `(r)`. - `is_dedekind_domain.height_one_spectrum.int_valuation_le_pow_iff_dvd` : The `v`-adic valuation of `r ∈ R` is less than or equal to `multiplicative.of_add (-n)` if and only if `vⁿ` divides the ideal `(r)`. - `is_dedekind_domain.height_one_spectrum.int_valuation_exists_uniformizer` : There exists `π ∈ R` with `v`-adic valuation `multiplicative.of_add (-1)`. - `is_dedekind_domain.height_one_spectrum.int_valuation_div_eq_div` : The valuation of `k ∈ K` is independent on how we express `k` as a fraction. - `is_dedekind_domain.height_one_spectrum.valuation_of_mk'` : The `v`-adic valuation of `r/s ∈ K` is the valuation of `r` divided by the valuation of `s`. - `is_dedekind_domain.height_one_spectrum.valuation_of_algebra_map` : The `v`-adic valuation on `K` extends the `v`-adic valuation on `R`. - `is_dedekind_domain.height_one_spectrum.valuation_exists_uniformizer` : There exists `π ∈ K` with `v`-adic valuation `multiplicative.of_add (-1)`. ## Implementation notes We are only interested in Dedekind domains with Krull dimension 1. ## References * [G. J. Janusz, *Algebraic Number Fields*][janusz1996] * [J.W.S. Cassels, A. Frölich, *Algebraic Number Theory*][cassels1967algebraic] * [J. Neukirch, *Algebraic Number Theory*][Neukirch1992] ## Tags dedekind domain, dedekind ring, adic valuation -/ noncomputable theory open_locale classical discrete_valuation open multiplicative is_dedekind_domain variables {R : Type*} [comm_ring R] [is_domain R] [is_dedekind_domain R] {K : Type*} [field K] [algebra R K] [is_fraction_ring R K] (v : height_one_spectrum R) namespace is_dedekind_domain.height_one_spectrum /-! ### Adic valuations on the Dedekind domain R -/ /-- The additive `v`-adic valuation of `r ∈ R` is the exponent of `v` in the factorization of the ideal `(r)`, if `r` is nonzero, or infinity, if `r = 0`. `int_valuation_def` is the corresponding multiplicative valuation. -/ def int_valuation_def (r : R) : ℤₘ₀ := if r = 0 then 0 else multiplicative.of_add (-(associates.mk v.as_ideal).count (associates.mk (ideal.span {r} : ideal R)).factors : ℤ) lemma int_valuation_def_if_pos {r : R} (hr : r = 0) : v.int_valuation_def r = 0 := if_pos hr lemma int_valuation_def_if_neg {r : R} (hr : r ≠ 0) : v.int_valuation_def r = (multiplicative.of_add (-(associates.mk v.as_ideal).count (associates.mk (ideal.span {r} : ideal R)).factors : ℤ)) := if_neg hr /-- Nonzero elements have nonzero adic valuation. -/ lemma int_valuation_ne_zero (x : R) (hx : x ≠ 0) : v.int_valuation_def x ≠ 0 := begin rw [int_valuation_def, if_neg hx], exact with_zero.coe_ne_zero, end /-- Nonzero divisors have nonzero valuation. -/ lemma int_valuation_ne_zero' (x : non_zero_divisors R) : v.int_valuation_def x ≠ 0 := v.int_valuation_ne_zero x (non_zero_divisors.coe_ne_zero x) /-- Nonzero divisors have valuation greater than zero. -/ lemma int_valuation_zero_le (x : non_zero_divisors R) : 0 < v.int_valuation_def x := begin rw [v.int_valuation_def_if_neg (non_zero_divisors.coe_ne_zero x)], exact with_zero.zero_lt_coe _, end /-- The `v`-adic valuation on `R` is bounded above by 1. -/ lemma int_valuation_le_one (x : R) : v.int_valuation_def x ≤ 1 := begin rw int_valuation_def, by_cases hx : x = 0, { rw if_pos hx, exact with_zero.zero_le 1 }, { rw [if_neg hx, ← with_zero.coe_one, ← of_add_zero, with_zero.coe_le_coe, of_add_le, right.neg_nonpos_iff], exact int.coe_nat_nonneg _ } end /-- The `v`-adic valuation of `r ∈ R` is less than 1 if and only if `v` divides the ideal `(r)`. -/ lemma int_valuation_lt_one_iff_dvd (r : R) : v.int_valuation_def r < 1 ↔ v.as_ideal ∣ ideal.span {r} := begin rw int_valuation_def, split_ifs with hr, { simpa [hr] using (with_zero.zero_lt_coe _) }, { rw [← with_zero.coe_one, ← of_add_zero, with_zero.coe_lt_coe, of_add_lt, neg_lt_zero, ← int.coe_nat_zero, int.coe_nat_lt, zero_lt_iff], have h : (ideal.span {r} : ideal R) ≠ 0, { rw [ne.def, ideal.zero_eq_bot, ideal.span_singleton_eq_bot], exact hr }, apply associates.count_ne_zero_iff_dvd h (by apply v.irreducible) } end /-- The `v`-adic valuation of `r ∈ R` is less than `multiplicative.of_add (-n)` if and only if `vⁿ` divides the ideal `(r)`. -/ lemma int_valuation_le_pow_iff_dvd (r : R) (n : ℕ) : v.int_valuation_def r ≤ multiplicative.of_add (-(n : ℤ)) ↔ v.as_ideal^n ∣ ideal.span {r} := begin rw int_valuation_def, split_ifs with hr, { simp_rw [hr, ideal.dvd_span_singleton, zero_le', submodule.zero_mem], }, { rw [with_zero.coe_le_coe, of_add_le, neg_le_neg_iff, int.coe_nat_le, ideal.dvd_span_singleton, ← associates.le_singleton_iff, associates.prime_pow_dvd_iff_le (associates.mk_ne_zero'.mpr hr) (by apply v.associates_irreducible)] } end /-- The `v`-adic valuation of `0 : R` equals 0. -/ lemma int_valuation.map_zero' : v.int_valuation_def 0 = 0 := v.int_valuation_def_if_pos (eq.refl 0) /-- The `v`-adic valuation of `1 : R` equals 1. -/ lemma int_valuation.map_one' : v.int_valuation_def 1 = 1 := by rw [v.int_valuation_def_if_neg (zero_ne_one.symm : (1 : R) ≠ 0), ideal.span_singleton_one, ← ideal.one_eq_top, associates.mk_one, associates.factors_one, associates.count_zero (by apply v.associates_irreducible), int.coe_nat_zero, neg_zero, of_add_zero, with_zero.coe_one] /-- The `v`-adic valuation of a product equals the product of the valuations. -/ lemma int_valuation.map_mul' (x y : R) : v.int_valuation_def (x * y) = v.int_valuation_def x * v.int_valuation_def y := begin simp only [int_valuation_def], by_cases hx : x = 0, { rw [hx, zero_mul, if_pos (eq.refl _), zero_mul] }, { by_cases hy : y = 0, { rw [hy, mul_zero, if_pos (eq.refl _), mul_zero] }, { rw [if_neg hx, if_neg hy, if_neg (mul_ne_zero hx hy), ← with_zero.coe_mul, with_zero.coe_inj, ← of_add_add, ← ideal.span_singleton_mul_span_singleton, ← associates.mk_mul_mk, ← neg_add, associates.count_mul (by apply associates.mk_ne_zero'.mpr hx) (by apply associates.mk_ne_zero'.mpr hy) (by apply v.associates_irreducible)], refl }} end lemma int_valuation.le_max_iff_min_le {a b c : ℕ} : multiplicative.of_add(-c : ℤ) ≤ max (multiplicative.of_add(-a : ℤ)) (multiplicative.of_add(-b : ℤ)) ↔ min a b ≤ c := by rw [le_max_iff, of_add_le, of_add_le, neg_le_neg_iff, neg_le_neg_iff, int.coe_nat_le, int.coe_nat_le, ← min_le_iff] /-- The `v`-adic valuation of a sum is bounded above by the maximum of the valuations. -/ lemma int_valuation.map_add_le_max' (x y : R) : v.int_valuation_def (x + y) ≤ max (v.int_valuation_def x) (v.int_valuation_def y) := begin by_cases hx : x = 0, { rw [hx, zero_add], conv_rhs {rw [int_valuation_def, if_pos (eq.refl _)]}, rw max_eq_right (with_zero.zero_le (v.int_valuation_def y)), exact le_refl _, }, { by_cases hy : y = 0, { rw [hy, add_zero], conv_rhs {rw [max_comm, int_valuation_def, if_pos (eq.refl _)]}, rw max_eq_right (with_zero.zero_le (v.int_valuation_def x)), exact le_refl _ }, { by_cases hxy : x + y = 0, { rw [int_valuation_def, if_pos hxy], exact zero_le',}, { rw [v.int_valuation_def_if_neg hxy, v.int_valuation_def_if_neg hx, v.int_valuation_def_if_neg hy, with_zero.le_max_iff, int_valuation.le_max_iff_min_le], set nmin := min ((associates.mk v.as_ideal).count (associates.mk (ideal.span {x})).factors) ((associates.mk v.as_ideal).count (associates.mk (ideal.span {y})).factors), have h_dvd_x : x ∈ v.as_ideal ^ (nmin), { rw [← associates.le_singleton_iff x nmin _, associates.prime_pow_dvd_iff_le (associates.mk_ne_zero'.mpr hx) _], exact min_le_left _ _, apply v.associates_irreducible }, have h_dvd_y : y ∈ v.as_ideal ^ nmin, { rw [← associates.le_singleton_iff y nmin _, associates.prime_pow_dvd_iff_le (associates.mk_ne_zero'.mpr hy) _], exact min_le_right _ _, apply v.associates_irreducible }, have h_dvd_xy : associates.mk v.as_ideal^nmin ≤ associates.mk (ideal.span {x + y}), { rw associates.le_singleton_iff, exact ideal.add_mem (v.as_ideal^nmin) h_dvd_x h_dvd_y, }, rw (associates.prime_pow_dvd_iff_le (associates.mk_ne_zero'.mpr hxy) _) at h_dvd_xy, exact h_dvd_xy, apply v.associates_irreducible, }}} end /-- The `v`-adic valuation on `R`. -/ def int_valuation : valuation R ℤₘ₀ := { to_fun := v.int_valuation_def, map_zero' := int_valuation.map_zero' v, map_one' := int_valuation.map_one' v, map_mul' := int_valuation.map_mul' v, map_add_le_max' := int_valuation.map_add_le_max' v } /-- There exists `π ∈ R` with `v`-adic valuation `multiplicative.of_add (-1)`. -/ lemma int_valuation_exists_uniformizer : ∃ (π : R), v.int_valuation_def π = multiplicative.of_add (-1 : ℤ) := begin have hv : _root_.irreducible (associates.mk v.as_ideal) := v.associates_irreducible, have hlt : v.as_ideal^2 < v.as_ideal, { rw ← ideal.dvd_not_unit_iff_lt, exact ⟨v.ne_bot, v.as_ideal, (not_congr ideal.is_unit_iff).mpr (ideal.is_prime.ne_top v.is_prime), sq v.as_ideal⟩ } , obtain ⟨π, mem, nmem⟩ := set_like.exists_of_lt hlt, have hπ : associates.mk (ideal.span {π}) ≠ 0, { rw associates.mk_ne_zero', intro h, rw h at nmem, exact nmem (submodule.zero_mem (v.as_ideal^2)), }, use π, rw [int_valuation_def, if_neg (associates.mk_ne_zero'.mp hπ), with_zero.coe_inj], apply congr_arg, rw [neg_inj, ← int.coe_nat_one, int.coe_nat_inj'], rw [← ideal.dvd_span_singleton, ← associates.mk_le_mk_iff_dvd_iff] at mem nmem, rw [← pow_one (associates.mk v.as_ideal), associates.prime_pow_dvd_iff_le hπ hv] at mem, rw [associates.mk_pow, associates.prime_pow_dvd_iff_le hπ hv, not_le] at nmem, exact nat.eq_of_le_of_lt_succ mem nmem, end /-! ### Adic valuations on the field of fractions `K` -/ /-- The `v`-adic valuation of `x ∈ K` is the valuation of `r` divided by the valuation of `s`, where `r` and `s` are chosen so that `x = r/s`. -/ def valuation (v : height_one_spectrum R) : valuation K ℤₘ₀ := v.int_valuation.extend_to_localization (λ r hr, set.mem_compl $ v.int_valuation_ne_zero' ⟨r, hr⟩) K lemma valuation_def (x : K) : v.valuation x = v.int_valuation.extend_to_localization (λ r hr, set.mem_compl (v.int_valuation_ne_zero' ⟨r, hr⟩)) K x := rfl /-- The `v`-adic valuation of `r/s ∈ K` is the valuation of `r` divided by the valuation of `s`. -/ lemma valuation_of_mk' {r : R} {s : non_zero_divisors R} : v.valuation (is_localization.mk' K r s) = v.int_valuation r / v.int_valuation s := begin erw [valuation_def, (is_localization.to_localization_map (non_zero_divisors R) K).lift_mk', div_eq_mul_inv, mul_eq_mul_left_iff], left, rw [units.coe_inv, inv_inj], refl, end /-- The `v`-adic valuation on `K` extends the `v`-adic valuation on `R`. -/ lemma valuation_of_algebra_map (r : R) : v.valuation (algebra_map R K r) = v.int_valuation r := by rw [valuation_def, valuation.extend_to_localization_apply_map_apply] /-- The `v`-adic valuation on `R` is bounded above by 1. -/ lemma valuation_le_one (r : R) : v.valuation (algebra_map R K r) ≤ 1 := by { rw valuation_of_algebra_map, exact v.int_valuation_le_one r } /-- The `v`-adic valuation of `r ∈ R` is less than 1 if and only if `v` divides the ideal `(r)`. -/ lemma valuation_lt_one_iff_dvd (r : R) : v.valuation (algebra_map R K r) < 1 ↔ v.as_ideal ∣ ideal.span {r} := by { rw valuation_of_algebra_map, exact v.int_valuation_lt_one_iff_dvd r } variable (K) /-- There exists `π ∈ K` with `v`-adic valuation `multiplicative.of_add (-1)`. -/ lemma valuation_exists_uniformizer : ∃ (π : K), v.valuation π = multiplicative.of_add (-1 : ℤ) := begin obtain ⟨r, hr⟩ := v.int_valuation_exists_uniformizer, use algebra_map R K r, rw [valuation_def, valuation.extend_to_localization_apply_map_apply], exact hr, end /-- Uniformizers are nonzero. -/ lemma valuation_uniformizer_ne_zero : (classical.some (v.valuation_exists_uniformizer K)) ≠ 0 := begin have hu := classical.some_spec (v.valuation_exists_uniformizer K), exact (valuation.ne_zero_iff _).mp (ne_of_eq_of_ne hu with_zero.coe_ne_zero), end /-! ### Completions with respect to adic valuations Given a Dedekind domain `R` with field of fractions `K` and a maximal ideal `v` of `R`, we define the completion of `K` with respect to its `v`-adic valuation, denoted `v.adic_completion`, and its ring of integers, denoted `v.adic_completion_integers`. -/ variable {K} /-- `K` as a valued field with the `v`-adic valuation. -/ def adic_valued : valued K ℤₘ₀ := valued.mk' v.valuation lemma adic_valued_apply {x : K} : (v.adic_valued.v : _) x = v.valuation x := rfl variables (K) /-- The completion of `K` with respect to its `v`-adic valuation. -/ def adic_completion := @uniform_space.completion K v.adic_valued.to_uniform_space instance : field (v.adic_completion K) := @uniform_space.completion.field K _ v.adic_valued.to_uniform_space _ _ v.adic_valued.to_uniform_add_group instance : inhabited (v.adic_completion K) := ⟨0⟩ instance valued_adic_completion : valued (v.adic_completion K) ℤₘ₀ := @valued.valued_completion _ _ _ _ v.adic_valued lemma valued_adic_completion_def {x : v.adic_completion K} : valued.v x = @valued.extension K _ _ _ (adic_valued v) x := rfl instance adic_completion_complete_space : complete_space (v.adic_completion K) := @uniform_space.completion.complete_space K v.adic_valued.to_uniform_space instance adic_completion.has_lift_t : has_lift_t K (v.adic_completion K) := (infer_instance : has_lift_t K (@uniform_space.completion K v.adic_valued.to_uniform_space)) /-- The ring of integers of `adic_completion`. -/ def adic_completion_integers : valuation_subring (v.adic_completion K) := valued.v.valuation_subring end is_dedekind_domain.height_one_spectrum
e85e997047fb347ad7368521beb870258f0c1fd3
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/topology/uniform_space/compact_separated.lean
11f50eb72e6a4d11ea15494eb3f4aa126cc1c8c1
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,617
lean
/- Copyright (c) 2020 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import topology.uniform_space.separation /-! # Compact separated uniform spaces ## Main statements * `compact_space_uniformity`: On a separated compact uniform space, the topology determines the uniform structure, entourages are exactly the neighborhoods of the diagonal. * `uniform_space_of_compact_t2`: every compact T2 topological structure is induced by a uniform structure. This uniform structure is described in the previous item. * Heine-Cantor theorem: continuous functions on compact separated uniform spaces with values in uniform spaces are automatically uniformly continuous. There are several variations, the main one is `compact_space.uniform_continuous_of_continuous`. ## Implementation notes The construction `uniform_space_of_compact_t2` is not declared as an instance, as it would badly loop. ## tags uniform space, uniform continuity, compact space -/ open_locale classical uniformity topological_space filter open filter uniform_space set variables {α β γ : Type*} [uniform_space α] [uniform_space β] /-! ### Uniformity on compact separated spaces -/ /-- On a separated compact uniform space, the topology determines the uniform structure, entourages are exactly the neighborhoods of the diagonal. -/ lemma compact_space_uniformity [compact_space α] [separated_space α] : 𝓤 α = ⨆ x : α, 𝓝 (x, x) := begin symmetry, refine le_antisymm supr_nhds_le_uniformity _, by_contra H, obtain ⟨V, hV, h⟩ : ∃ V : set (α × α), (∀ x : α, V ∈ 𝓝 (x, x)) ∧ 𝓤 α ⊓ 𝓟 Vᶜ ≠ ⊥, { simpa [le_iff_forall_inf_principal_compl] using H }, let F := 𝓤 α ⊓ 𝓟 Vᶜ, haveI : ne_bot F := ⟨h⟩, obtain ⟨⟨x, y⟩, hx⟩ : ∃ (p : α × α), cluster_pt p F := cluster_point_of_compact F, have : cluster_pt (x, y) (𝓤 α) := hx.of_inf_left, have hxy : x = y := eq_of_uniformity_inf_nhds this, subst hxy, have : cluster_pt (x, x) (𝓟 Vᶜ) := hx.of_inf_right, have : (x, x) ∉ interior V, { have : (x, x) ∈ closure Vᶜ, by rwa mem_closure_iff_cluster_pt, rwa closure_compl at this }, have : (x, x) ∈ interior V, { rw mem_interior_iff_mem_nhds, exact hV x }, contradiction end lemma unique_uniformity_of_compact_t2 [t : topological_space γ] [compact_space γ] [t2_space γ] {u u' : uniform_space γ} (h : u.to_topological_space = t) (h' : u'.to_topological_space = t) : u = u' := begin apply uniform_space_eq, change uniformity _ = uniformity _, haveI : @compact_space γ u.to_topological_space, { rw h ; assumption }, haveI : @compact_space γ u'.to_topological_space, { rw h' ; assumption }, haveI : @separated_space γ u, { rwa [separated_iff_t2, h] }, haveI : @separated_space γ u', { rwa [separated_iff_t2, h'] }, rw [compact_space_uniformity, compact_space_uniformity, h, h'] end /-- The unique uniform structure inducing a given compact Hausdorff topological structure. -/ def uniform_space_of_compact_t2 [topological_space γ] [compact_space γ] [t2_space γ] : uniform_space γ := { uniformity := ⨆ x, 𝓝 (x, x), refl := begin simp_rw [filter.principal_le_iff, mem_supr], rintros V V_in ⟨x, _⟩ ⟨⟩, exact mem_of_mem_nhds (V_in x), end, symm := begin refine le_of_eq _, rw map_supr, congr' with x : 1, erw [nhds_prod_eq, ← prod_comm], end, comp := begin /- This is the difficult part of the proof. We need to prove that, for each neighborhood W of the diagonal Δ, W ○ W is still a neighborhood of the diagonal. -/ set 𝓝Δ := ⨆ x : γ, 𝓝 (x, x), -- The filter of neighborhoods of Δ set F := 𝓝Δ.lift' (λ (s : set (γ × γ)), s ○ s), -- Compositions of neighborhoods of Δ -- If this weren't true, then there would be V ∈ 𝓝Δ such that F ⊓ 𝓟 Vᶜ ≠ ⊥ rw le_iff_forall_inf_principal_compl, intros V V_in, by_contra H, haveI : ne_bot (F ⊓ 𝓟 Vᶜ) := ⟨H⟩, -- Hence compactness would give us a cluster point (x, y) for F ⊓ 𝓟 Vᶜ obtain ⟨⟨x, y⟩, hxy⟩ : ∃ (p : γ × γ), cluster_pt p (F ⊓ 𝓟 Vᶜ) := cluster_point_of_compact _, -- In particular (x, y) is a cluster point of 𝓟 Vᶜ, hence is not in the interior of V, -- and a fortiori not in Δ, so x ≠ y have clV : cluster_pt (x, y) (𝓟 $ Vᶜ) := hxy.of_inf_right, have : (x, y) ∉ interior V, { have : (x, y) ∈ closure (Vᶜ), by rwa mem_closure_iff_cluster_pt, rwa closure_compl at this }, have diag_subset : diagonal γ ⊆ interior V, { rw subset_interior_iff_nhds, rintros ⟨x, x⟩ ⟨⟩, exact (mem_supr.mp V_in : _) x }, have x_ne_y : x ≠ y, { intro h, apply this, apply diag_subset, simp [h] }, -- Since γ is compact and Hausdorff, it is normal, hence regular. haveI : normal_space γ := normal_of_compact_t2, -- So there are closed neighboords V₁ and V₂ of x and y contained in disjoint open neighborhoods -- U₁ and U₂. obtain ⟨U₁, V₁, U₁_in, V₁_in, U₂, V₂, U₂_in₂, V₂_in, V₁_cl, V₂_cl, U₁_op, U₂_op, VU₁, VU₂, hU₁₂⟩ : ∃ (U₁ V₁ ∈ 𝓝 x) (U₂ V₂ ∈ 𝓝 y), is_closed V₁ ∧ is_closed V₂ ∧ is_open U₁ ∧ is_open U₂ ∧ V₁ ⊆ U₁ ∧ V₂ ⊆ U₂ ∧ U₁ ∩ U₂ = ∅ := disjoint_nested_nhds x_ne_y, -- We set U₃ := (V₁ ∪ V₂)ᶜ so that W := (U₁.prod U₁) ∪ (U₂.prod U₂) ∪ (U₃.prod U₃) is an open -- neighborhood of Δ. let U₃ := (V₁ ∪ V₂)ᶜ, have U₃_op : is_open U₃ := is_open_compl_iff.mpr (is_closed.union V₁_cl V₂_cl), let W := (U₁.prod U₁) ∪ (U₂.prod U₂) ∪ (U₃.prod U₃), have W_in : W ∈ 𝓝Δ, { rw mem_supr, intros x, apply is_open.mem_nhds (is_open.union (is_open.union _ _) _), { by_cases hx : x ∈ V₁ ∪ V₂, { left, cases hx with hx hx ; [left, right] ; split ; tauto }, { right, rw mem_prod, tauto }, }, all_goals { simp only [is_open.prod, *] } }, -- So W ○ W ∈ F by definition of F have : W ○ W ∈ F, by simpa only using mem_lift' W_in, -- And V₁.prod V₂ ∈ 𝓝 (x, y) have hV₁₂ : V₁.prod V₂ ∈ 𝓝 (x, y) := prod_is_open.mem_nhds V₁_in V₂_in, -- But (x, y) is also a cluster point of F so (V₁.prod V₂) ∩ (W ○ W) ≠ ∅ have clF : cluster_pt (x, y) F := hxy.of_inf_left, obtain ⟨p, p_in⟩ : ∃ p, p ∈ (V₁.prod V₂) ∩ (W ○ W) := cluster_pt_iff.mp clF hV₁₂ this, -- However the construction of W implies (V₁.prod V₂) ∩ (W ○ W) = ∅. -- Indeed assume for contradiction there is some (u, v) in the intersection. -- So u ∈ V₁, v ∈ V₂, and there exists some w such that (u, w) ∈ W and (w ,v) ∈ W. -- Because u is in V₁ which is disjoint from U₂ and U₃, (u, w) ∈ W forces (u, w) ∈ U₁.prod U₁. -- Similarly, because v ∈ V₂, (w ,v) ∈ W forces (w, v) ∈ U₂.prod U₂. -- Hence w ∈ U₁ ∩ U₂ which is empty. have inter_empty : (V₁.prod V₂) ∩ (W ○ W) = ∅, { rw eq_empty_iff_forall_not_mem, rintros ⟨u, v⟩ ⟨⟨u_in, v_in⟩, w, huw, hwv⟩, have uw_in : (u, w) ∈ U₁.prod U₁ := set.mem_prod.2 ((huw.resolve_right (λ h, (h.1 $ or.inl u_in))).resolve_right (λ h, have u ∈ U₁ ∩ U₂, from ⟨VU₁ u_in, h.1⟩, by rwa hU₁₂ at this)), have wv_in : (w, v) ∈ U₂.prod U₂ := set.mem_prod.2 ((hwv.resolve_right (λ h, (h.2 $ or.inr v_in))).resolve_left (λ h, have v ∈ U₁ ∩ U₂, from ⟨h.2, VU₂ v_in⟩, by rwa hU₁₂ at this)), have : w ∈ U₁ ∩ U₂ := ⟨uw_in.2, wv_in.1⟩, rwa hU₁₂ at this }, -- So we have a contradiction rwa inter_empty at p_in, end, is_open_uniformity := begin -- Here we need to prove the topology induced by the constructed uniformity is the -- topology we started with. suffices : ∀ x : γ, filter.comap (prod.mk x) (⨆ y, 𝓝 (y ,y)) = 𝓝 x, { intros s, change is_open s ↔ _, simp_rw [is_open_iff_mem_nhds, nhds_eq_comap_uniformity_aux, this] }, intros x, simp_rw [comap_supr, nhds_prod_eq, comap_prod, show prod.fst ∘ prod.mk x = λ y : γ, x, by ext ; simp, show prod.snd ∘ (prod.mk x) = (id : γ → γ), by ext ; refl, comap_id], rw [supr_split_single _ x, comap_const_of_mem (λ V, mem_of_mem_nhds)], suffices : ∀ y ≠ x, comap (λ (y : γ), x) (𝓝 y) ⊓ 𝓝 y ≤ 𝓝 x, by simpa, intros y hxy, simp [comap_const_of_not_mem (compl_singleton_mem_nhds hxy) (by simp)], end } /-! ### Heine-Cantor theorem -/ /-- Heine-Cantor: a continuous function on a compact separated uniform space is uniformly continuous. -/ lemma compact_space.uniform_continuous_of_continuous [compact_space α] [separated_space α] {f : α → β} (h : continuous f) : uniform_continuous f := calc map (prod.map f f) (𝓤 α) = map (prod.map f f) (⨆ x, 𝓝 (x, x)) : by rw compact_space_uniformity ... = ⨆ x, map (prod.map f f) (𝓝 (x, x)) : by rw map_supr ... ≤ ⨆ x, 𝓝 (f x, f x) : supr_le_supr (λ x, (h.prod_map h).continuous_at) ... ≤ ⨆ y, 𝓝 (y, y) : supr_comp_le (λ y, 𝓝 (y, y)) f ... ≤ 𝓤 β : supr_nhds_le_uniformity /-- Heine-Cantor: a continuous function on a compact separated set of a uniform space is uniformly continuous. -/ lemma is_compact.uniform_continuous_on_of_continuous' {s : set α} {f : α → β} (hs : is_compact s) (hs' : is_separated s) (hf : continuous_on f s) : uniform_continuous_on f s := begin rw uniform_continuous_on_iff_restrict, rw is_separated_iff_induced at hs', rw is_compact_iff_compact_space at hs, rw continuous_on_iff_continuous_restrict at hf, resetI, exact compact_space.uniform_continuous_of_continuous hf, end /-- Heine-Cantor: a continuous function on a compact set of a separated uniform space is uniformly continuous. -/ lemma is_compact.uniform_continuous_on_of_continuous [separated_space α] {s : set α} {f : α → β} (hs : is_compact s) (hf : continuous_on f s) : uniform_continuous_on f s := hs.uniform_continuous_on_of_continuous' (is_separated_of_separated_space s) hf
1862177f354bc1d4f3cf8eddd626dc96ae491aee
130c49f47783503e462c16b2eff31933442be6ff
/stage0/src/Lean/PrettyPrinter/Formatter.lean
3496c6605d9a1025be8eff7cae288c2cee25dbda
[ "Apache-2.0" ]
permissive
Hazel-Brown/lean4
8aa5860e282435ffc30dcdfccd34006c59d1d39c
79e6732fc6bbf5af831b76f310f9c488d44e7a16
refs/heads/master
1,689,218,208,951
1,629,736,869,000
1,629,736,896,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
21,179
lean
/- Copyright (c) 2020 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.CoreM import Lean.Parser.Extension import Lean.KeyedDeclsAttribute import Lean.ParserCompiler.Attribute import Lean.PrettyPrinter.Basic /-! The formatter turns a `Syntax` tree into a `Format` object, inserting both mandatory whitespace (to separate adjacent tokens) as well as "pretty" optional whitespace. The basic approach works much like the parenthesizer: A right-to-left traversal over the syntax tree, driven by parser-specific handlers registered via attributes. The traversal is right-to-left so that when emitting a token, we already know the text following it and can decide whether or not whitespace between the two is necessary. -/ namespace Lean namespace PrettyPrinter namespace Formatter structure Context where options : Options table : Parser.TokenTable structure State where stxTrav : Syntax.Traverser -- Textual content of `stack` up to the first whitespace (not enclosed in an escaped ident). We assume that the textual -- content of `stack` is modified only by `pushText` and `pushLine`, so `leadWord` is adjusted there accordingly. leadWord : String := "" -- Stack of generated Format objects, analogous to the Syntax stack in the parser. -- Note, however, that the stack is reversed because of the right-to-left traversal. stack : Array Format := #[] end Formatter abbrev FormatterM := ReaderT Formatter.Context $ StateRefT Formatter.State CoreM @[inline] def FormatterM.orelse {α} (p₁ p₂ : FormatterM α) : FormatterM α := do let s ← get catchInternalId backtrackExceptionId p₁ (fun _ => do set s; p₂) instance {α} : OrElse (FormatterM α) := ⟨FormatterM.orelse⟩ abbrev Formatter := FormatterM Unit unsafe def mkFormatterAttribute : IO (KeyedDeclsAttribute Formatter) := KeyedDeclsAttribute.init { builtinName := `builtinFormatter, name := `formatter, descr := "Register a formatter for a parser. [formatter k] registers a declaration of type `Lean.PrettyPrinter.Formatter` for the `SyntaxNodeKind` `k`.", valueTypeName := `Lean.PrettyPrinter.Formatter, evalKey := fun builtin stx => do let env ← getEnv let id ← Attribute.Builtin.getId stx -- `isValidSyntaxNodeKind` is updated only in the next stage for new `[builtin*Parser]`s, but we try to -- synthesize a formatter for it immediately, so we just check for a declaration in this case if (builtin && (env.find? id).isSome) || Parser.isValidSyntaxNodeKind env id then pure id else throwError "invalid [formatter] argument, unknown syntax kind '{id}'" } `Lean.PrettyPrinter.formatterAttribute @[builtinInit mkFormatterAttribute] constant formatterAttribute : KeyedDeclsAttribute Formatter unsafe def mkCombinatorFormatterAttribute : IO ParserCompiler.CombinatorAttribute := ParserCompiler.registerCombinatorAttribute `combinatorFormatter "Register a formatter for a parser combinator. [combinatorFormatter c] registers a declaration of type `Lean.PrettyPrinter.Formatter` for the `Parser` declaration `c`. Note that, unlike with [formatter], this is not a node kind since combinators usually do not introduce their own node kinds. The tagged declaration may optionally accept parameters corresponding to (a prefix of) those of `c`, where `Parser` is replaced with `Formatter` in the parameter types." @[builtinInit mkCombinatorFormatterAttribute] constant combinatorFormatterAttribute : ParserCompiler.CombinatorAttribute namespace Formatter open Lean.Core open Lean.Parser def throwBacktrack {α} : FormatterM α := throw $ Exception.internal backtrackExceptionId instance : Syntax.MonadTraverser FormatterM := ⟨{ get := State.stxTrav <$> get, set := fun t => modify (fun st => { st with stxTrav := t }), modifyGet := fun f => modifyGet (fun st => let (a, t) := f st.stxTrav; (a, { st with stxTrav := t })) }⟩ open Syntax.MonadTraverser def getStack : FormatterM (Array Format) := do let st ← get pure st.stack def getStackSize : FormatterM Nat := do let stack ← getStack; pure stack.size def setStack (stack : Array Format) : FormatterM Unit := modify fun st => { st with stack := stack } def push (f : Format) : FormatterM Unit := modify fun st => { st with stack := st.stack.push f } def pushLine : FormatterM Unit := do push Format.line; modify fun st => { st with leadWord := "" } /-- Execute `x` at the right-most child of the current node, if any, then advance to the left. -/ def visitArgs (x : FormatterM Unit) : FormatterM Unit := do let stx ← getCur if stx.getArgs.size > 0 then goDown (stx.getArgs.size - 1) *> x <* goUp goLeft /-- Execute `x`, pass array of generated Format objects to `fn`, and push result. -/ def fold (fn : Array Format → Format) (x : FormatterM Unit) : FormatterM Unit := do let sp ← getStackSize x let stack ← getStack let f := fn $ stack.extract sp stack.size setStack $ (stack.shrink sp).push f /-- Execute `x` and concatenate generated Format objects. -/ def concat (x : FormatterM Unit) : FormatterM Unit := do fold (Array.foldl (fun acc f => if acc.isNil then f else f ++ acc) Format.nil) x def indent (x : Formatter) (indent : Option Int := none) : Formatter := do concat x let ctx ← read let indent := indent.getD $ Std.Format.getIndent ctx.options modify fun st => { st with stack := st.stack.pop.push (Format.nest indent st.stack.back) } def group (x : Formatter) : Formatter := do concat x modify fun st => { st with stack := st.stack.pop.push (Format.fill st.stack.back) } /-- Tags the top of the stack with `stx`'s position, if it has one. -/ def tagTop (stx : Syntax) : Formatter := do if let some p := stx.getPos? then modify fun st => { st with stack := st.stack.pop.push (Format.tag p st.stack.back) } @[combinatorFormatter Lean.Parser.orelse] def orelse.formatter (p1 p2 : Formatter) : Formatter := -- HACK: We have no (immediate) information on which side of the orelse could have produced the current node, so try -- them in turn. Uses the syntax traverser non-linearly! p1 <|> p2 -- `mkAntiquot` is quite complex, so we'd rather have its formatter synthesized below the actual parser definition. -- Note that there is a mutual recursion -- `categoryParser -> mkAntiquot -> termParser -> categoryParser`, so we need to introduce an indirection somewhere -- anyway. @[extern "lean_mk_antiquot_formatter"] constant mkAntiquot.formatter' (name : String) (kind : Option SyntaxNodeKind) (anonymous := true) : Formatter -- break up big mutual recursion @[extern "lean_pretty_printer_formatter_interpret_parser_descr"] constant interpretParserDescr' : ParserDescr → CoreM Formatter unsafe def formatterForKindUnsafe (k : SyntaxNodeKind) : Formatter := do if k == `missing then push "<missing>" goLeft else let f ← runForNodeKind formatterAttribute k interpretParserDescr' let stx ← getCur concat f tagTop stx @[implementedBy formatterForKindUnsafe] constant formatterForKind (k : SyntaxNodeKind) : Formatter @[combinatorFormatter Lean.Parser.withAntiquot] def withAntiquot.formatter (antiP p : Formatter) : Formatter := -- TODO: could be optimized using `isAntiquot` (which would have to be moved), but I'd rather -- fix the backtracking hack outright. orelse.formatter antiP p @[combinatorFormatter Lean.Parser.withAntiquotSuffixSplice] def withAntiquotSuffixSplice.formatter (k : SyntaxNodeKind) (p suffix : Formatter) : Formatter := do if (← getCur).isAntiquotSuffixSplice then visitArgs <| suffix *> p else p @[combinatorFormatter Lean.Parser.tokenWithAntiquot] def tokenWithAntiquot.formatter (p : Formatter) : Formatter := do if (← getCur).isTokenAntiquot then visitArgs p else p @[combinatorFormatter Lean.Parser.categoryParser] def categoryParser.formatter (cat : Name) : Formatter := group $ indent do let stx ← getCur trace[PrettyPrinter.format] "formatting {indentD (format stx)}" if stx.getKind == `choice then visitArgs do -- format only last choice -- TODO: We could use elaborator data here to format the chosen child when available formatterForKind (← getCur).getKind else withAntiquot.formatter (mkAntiquot.formatter' cat.toString none) (formatterForKind stx.getKind) @[combinatorFormatter Lean.Parser.categoryParserOfStack] def categoryParserOfStack.formatter (offset : Nat) : Formatter := do let st ← get let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset) categoryParser.formatter stx.getId @[combinatorFormatter Lean.Parser.parserOfStack] def parserOfStack.formatter (offset : Nat) (prec : Nat := 0) : Formatter := do let st ← get let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset) formatterForKind stx.getKind @[combinatorFormatter Lean.Parser.error] def error.formatter (msg : String) : Formatter := pure () @[combinatorFormatter Lean.Parser.errorAtSavedPos] def errorAtSavedPos.formatter (msg : String) (delta : Bool) : Formatter := pure () @[combinatorFormatter Lean.Parser.atomic] def atomic.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.lookahead] def lookahead.formatter (p : Formatter) : Formatter := pure () @[combinatorFormatter Lean.Parser.notFollowedBy] def notFollowedBy.formatter (p : Formatter) : Formatter := pure () @[combinatorFormatter Lean.Parser.andthen] def andthen.formatter (p1 p2 : Formatter) : Formatter := p2 *> p1 def checkKind (k : SyntaxNodeKind) : FormatterM Unit := do let stx ← getCur if k != stx.getKind then trace[PrettyPrinter.format.backtrack] "unexpected node kind '{stx.getKind}', expected '{k}'" throwBacktrack @[combinatorFormatter Lean.Parser.node] def node.formatter (k : SyntaxNodeKind) (p : Formatter) : Formatter := do checkKind k; visitArgs p @[combinatorFormatter Lean.Parser.trailingNode] def trailingNode.formatter (k : SyntaxNodeKind) (_ _ : Nat) (p : Formatter) : Formatter := do checkKind k visitArgs do p; -- leading term, not actually produced by `p` categoryParser.formatter `foo def parseToken (s : String) : FormatterM ParserState := do -- include comment tokens, e.g. when formatting `- -0` (Parser.andthenFn Parser.whitespace (Parser.tokenFn [])) { input := s, fileName := "", fileMap := FileMap.ofString "", prec := 0, env := ← getEnv, options := ← getOptions, tokens := (← read).table } (Parser.mkParserState s) def pushTokenCore (tk : String) : FormatterM Unit := do if tk.toSubstring.dropRightWhile (fun s => s == ' ') == tk.toSubstring then push tk else pushLine push tk.trimRight def pushToken (info : SourceInfo) (tk : String) : FormatterM Unit := do match info with | SourceInfo.original _ _ ss _ => -- preserve non-whitespace content (i.e. comments) let ss' := ss.trim if !ss'.isEmpty then let ws := { ss with startPos := ss'.stopPos } if ws.contains '\n' then push s!"\n{ss'}" else push s!" {ss'}" modify fun st => { st with leadWord := "" } | _ => pure () let st ← get -- If there is no space between `tk` and the next word, see if we would parse more than `tk` as a single token if st.leadWord != "" && tk.trimRight == tk then let tk' := tk.trimLeft let t ← parseToken $ tk' ++ st.leadWord if t.pos <= tk'.bsize then -- stopped within `tk` => use it as is, extend `leadWord` if not prefixed by whitespace pushTokenCore tk modify fun st => { st with leadWord := if tk.trimLeft == tk then tk ++ st.leadWord else "" } else -- stopped after `tk` => add space pushTokenCore $ tk ++ " " modify fun st => { st with leadWord := if tk.trimLeft == tk then tk else "" } else -- already separated => use `tk` as is pushTokenCore tk modify fun st => { st with leadWord := if tk.trimLeft == tk then tk else "" } match info with | SourceInfo.original ss _ _ _ => -- preserve non-whitespace content (i.e. comments) let ss' := ss.trim if !ss'.isEmpty then let ws := { ss with startPos := ss'.stopPos } if ws.contains '\n' then do -- Indentation is automatically increased when entering a category, but comments should be aligned -- with the actual token, so dedent indent (push s!"{ss'}\n") (some ((0:Int) - Std.Format.getIndent (← getOptions))) else push s!"{ss'} " modify fun st => { st with leadWord := "" } | _ => pure () @[combinatorFormatter Lean.Parser.symbolNoAntiquot] def symbolNoAntiquot.formatter (sym : String) : Formatter := do let stx ← getCur if stx.isToken sym then do let (Syntax.atom info _) ← pure stx | unreachable! pushToken info sym goLeft else do trace[PrettyPrinter.format.backtrack] "unexpected syntax '{format stx}', expected symbol '{sym}'" throwBacktrack @[combinatorFormatter Lean.Parser.nonReservedSymbolNoAntiquot] def nonReservedSymbolNoAntiquot.formatter := symbolNoAntiquot.formatter @[combinatorFormatter Lean.Parser.rawCh] def rawCh.formatter (ch : Char) := symbolNoAntiquot.formatter ch.toString @[combinatorFormatter Lean.Parser.unicodeSymbolNoAntiquot] def unicodeSymbolNoAntiquot.formatter (sym asciiSym : String) : Formatter := do let Syntax.atom info val ← getCur | throwError m!"not an atom: {← getCur}" if val == sym.trim then pushToken info sym else pushToken info asciiSym; goLeft @[combinatorFormatter Lean.Parser.identNoAntiquot] def identNoAntiquot.formatter : Formatter := do checkKind identKind let Syntax.ident info _ id _ ← getCur | throwError m!"not an ident: {← getCur}" let id := id.simpMacroScopes pushToken info id.toString goLeft @[combinatorFormatter Lean.Parser.rawIdentNoAntiquot] def rawIdentNoAntiquot.formatter : Formatter := do checkKind identKind let Syntax.ident info _ id _ ← getCur | throwError m!"not an ident: {← getCur}" pushToken info id.toString goLeft @[combinatorFormatter Lean.Parser.identEq] def identEq.formatter (id : Name) := rawIdentNoAntiquot.formatter def visitAtom (k : SyntaxNodeKind) : Formatter := do let stx ← getCur if k != Name.anonymous then checkKind k let Syntax.atom info val ← pure $ stx.ifNode (fun n => n.getArg 0) (fun _ => stx) | throwError m!"not an atom: {stx}" pushToken info val goLeft @[combinatorFormatter Lean.Parser.charLitNoAntiquot] def charLitNoAntiquot.formatter := visitAtom charLitKind @[combinatorFormatter Lean.Parser.strLitNoAntiquot] def strLitNoAntiquot.formatter := visitAtom strLitKind @[combinatorFormatter Lean.Parser.nameLitNoAntiquot] def nameLitNoAntiquot.formatter := visitAtom nameLitKind @[combinatorFormatter Lean.Parser.numLitNoAntiquot] def numLitNoAntiquot.formatter := visitAtom numLitKind @[combinatorFormatter Lean.Parser.scientificLitNoAntiquot] def scientificLitNoAntiquot.formatter := visitAtom scientificLitKind @[combinatorFormatter Lean.Parser.fieldIdx] def fieldIdx.formatter := visitAtom fieldIdxKind @[combinatorFormatter Lean.Parser.manyNoAntiquot] def manyNoAntiquot.formatter (p : Formatter) : Formatter := do let stx ← getCur visitArgs $ stx.getArgs.size.forM fun _ => p @[combinatorFormatter Lean.Parser.many1NoAntiquot] def many1NoAntiquot.formatter (p : Formatter) : Formatter := manyNoAntiquot.formatter p @[combinatorFormatter Lean.Parser.optionalNoAntiquot] def optionalNoAntiquot.formatter (p : Formatter) : Formatter := visitArgs p @[combinatorFormatter Lean.Parser.many1Unbox] def many1Unbox.formatter (p : Formatter) : Formatter := do let stx ← getCur if stx.getKind == nullKind then do manyNoAntiquot.formatter p else p @[combinatorFormatter Lean.Parser.sepByNoAntiquot] def sepByNoAntiquot.formatter (p pSep : Formatter) : Formatter := do let stx ← getCur visitArgs $ (List.range stx.getArgs.size).reverse.forM $ fun i => if i % 2 == 0 then p else pSep @[combinatorFormatter Lean.Parser.sepBy1NoAntiquot] def sepBy1NoAntiquot.formatter := sepByNoAntiquot.formatter @[combinatorFormatter Lean.Parser.withPosition] def withPosition.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withoutPosition] def withoutPosition.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withForbidden] def withForbidden.formatter (tk : Token) (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withoutForbidden] def withoutForbidden.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.withoutInfo] def withoutInfo.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.setExpected] def setExpected.formatter (expected : List String) (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.incQuotDepth] def incQuotDepth.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.decQuotDepth] def decQuotDepth.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.suppressInsideQuot] def suppressInsideQuot.formatter (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.evalInsideQuot] def evalInsideQuot.formatter (declName : Name) (p : Formatter) : Formatter := p @[combinatorFormatter Lean.Parser.checkWsBefore] def checkWsBefore.formatter : Formatter := do let st ← get if st.leadWord != "" then pushLine @[combinatorFormatter Lean.Parser.checkPrec] def checkPrec.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkLhsPrec] def checkLhsPrec.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.setLhsPrec] def setLhsPrec.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkStackTop] def checkStackTop.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkNoWsBefore] def checkNoWsBefore.formatter : Formatter := -- prevent automatic whitespace insertion modify fun st => { st with leadWord := "" } @[combinatorFormatter Lean.Parser.checkLinebreakBefore] def checkLinebreakBefore.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkTailWs] def checkTailWs.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkColGe] def checkColGe.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkColGt] def checkColGt.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkLineEq] def checkLineEq.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.eoi] def eoi.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.notFollowedByCategoryToken] def notFollowedByCategoryToken.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkNoImmediateColon] def checkNoImmediateColon.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkInsideQuot] def checkInsideQuot.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.checkOutsideQuot] def checkOutsideQuot.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.skip] def skip.formatter : Formatter := pure () @[combinatorFormatter Lean.Parser.pushNone] def pushNone.formatter : Formatter := goLeft @[combinatorFormatter Lean.Parser.interpolatedStr] def interpolatedStr.formatter (p : Formatter) : Formatter := do visitArgs $ (← getCur).getArgs.reverse.forM fun chunk => match chunk.isLit? interpolatedStrLitKind with | some str => push str *> goLeft | none => p @[combinatorFormatter Lean.Parser.dbgTraceState] def dbgTraceState.formatter (label : String) (p : Formatter) : Formatter := p @[combinatorFormatter ite, macroInline] def ite {α : Type} (c : Prop) [h : Decidable c] (t e : Formatter) : Formatter := if c then t else e abbrev FormatterAliasValue := AliasValue Formatter builtin_initialize formatterAliasesRef : IO.Ref (NameMap FormatterAliasValue) ← IO.mkRef {} def registerAlias (aliasName : Name) (v : FormatterAliasValue) : IO Unit := do Parser.registerAliasCore formatterAliasesRef aliasName v instance : Coe Formatter FormatterAliasValue := { coe := AliasValue.const } instance : Coe (Formatter → Formatter) FormatterAliasValue := { coe := AliasValue.unary } instance : Coe (Formatter → Formatter → Formatter) FormatterAliasValue := { coe := AliasValue.binary } end Formatter open Formatter def format (formatter : Formatter) (stx : Syntax) : CoreM Format := do trace[PrettyPrinter.format.input] "{Std.format stx}" let options ← getOptions let table ← Parser.builtinTokenTable.get catchInternalId backtrackExceptionId (do let (_, st) ← (concat formatter { table := table, options := options }).run { stxTrav := Syntax.Traverser.fromSyntax stx }; pure $ Format.fill $ st.stack.get! 0) (fun _ => throwError "format: uncaught backtrack exception") def formatTerm := format $ categoryParser.formatter `term def formatCommand := format $ categoryParser.formatter `command builtin_initialize registerTraceClass `PrettyPrinter.format; end PrettyPrinter end Lean
b2ea71d41f9093e742fe4e0440747a3e0599fd6d
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/algebra/group/pi.lean
b7623c6ae0c11aa03bdb7b093be5ca3877297147
[ "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
11,693
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot -/ import data.pi import data.set.function import tactic.pi_instances import algebra.group.hom_instances /-! # Pi instances for groups and monoids This file defines instances for group, monoid, semigroup and related structures on Pi types. -/ universes u v w variable {I : Type u} -- The indexing type variable {f : I → Type v} -- The family of types already equipped with instances variables (x y : Π i, f i) (i : I) namespace pi @[to_additive] instance semigroup [∀ i, semigroup $ f i] : semigroup (Π i : I, f i) := by refine_struct { mul := (*), .. }; tactic.pi_instance_derive_field instance semigroup_with_zero [∀ i, semigroup_with_zero $ f i] : semigroup_with_zero (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), mul := (*), .. }; tactic.pi_instance_derive_field @[to_additive] instance comm_semigroup [∀ i, comm_semigroup $ f i] : comm_semigroup (Π i : I, f i) := by refine_struct { mul := (*), .. }; tactic.pi_instance_derive_field @[to_additive] instance mul_one_class [∀ i, mul_one_class $ f i] : mul_one_class (Π i : I, f i) := by refine_struct { one := (1 : Π i, f i), mul := (*), .. }; tactic.pi_instance_derive_field @[to_additive] instance monoid [∀ i, monoid $ f i] : monoid (Π i : I, f i) := by refine_struct { one := (1 : Π i, f i), mul := (*), npow := λ n x i, (x i) ^ n }; tactic.pi_instance_derive_field @[simp] lemma pow_apply [∀ i, monoid $ f i] (n : ℕ) : (x^n) i = (x i)^n := begin induction n with n ih, { simp, }, { simp [pow_succ, ih], }, end @[to_additive] instance comm_monoid [∀ i, comm_monoid $ f i] : comm_monoid (Π i : I, f i) := by refine_struct { one := (1 : Π i, f i), mul := (*), npow := monoid.npow }; tactic.pi_instance_derive_field @[to_additive] instance div_inv_monoid [∀ i, div_inv_monoid $ f i] : div_inv_monoid (Π i : I, f i) := by refine_struct { one := (1 : Π i, f i), mul := (*), inv := has_inv.inv, div := has_div.div, npow := monoid.npow, zpow := λ z x i, (x i) ^ z }; tactic.pi_instance_derive_field @[to_additive] instance group [∀ i, group $ f i] : group (Π i : I, f i) := by refine_struct { one := (1 : Π i, f i), mul := (*), inv := has_inv.inv, div := has_div.div, npow := monoid.npow, zpow := div_inv_monoid.zpow }; tactic.pi_instance_derive_field @[to_additive] instance comm_group [∀ i, comm_group $ f i] : comm_group (Π i : I, f i) := by refine_struct { one := (1 : Π i, f i), mul := (*), inv := has_inv.inv, div := has_div.div, npow := monoid.npow, zpow := div_inv_monoid.zpow }; tactic.pi_instance_derive_field @[to_additive add_left_cancel_semigroup] instance left_cancel_semigroup [∀ i, left_cancel_semigroup $ f i] : left_cancel_semigroup (Π i : I, f i) := by refine_struct { mul := (*) }; tactic.pi_instance_derive_field @[to_additive add_right_cancel_semigroup] instance right_cancel_semigroup [∀ i, right_cancel_semigroup $ f i] : right_cancel_semigroup (Π i : I, f i) := by refine_struct { mul := (*) }; tactic.pi_instance_derive_field @[to_additive add_left_cancel_monoid] instance left_cancel_monoid [∀ i, left_cancel_monoid $ f i] : left_cancel_monoid (Π i : I, f i) := by refine_struct { one := (1 : Π i, f i), mul := (*), npow := monoid.npow }; tactic.pi_instance_derive_field @[to_additive add_right_cancel_monoid] instance right_cancel_monoid [∀ i, right_cancel_monoid $ f i] : right_cancel_monoid (Π i : I, f i) := by refine_struct { one := (1 : Π i, f i), mul := (*), npow := monoid.npow, .. }; tactic.pi_instance_derive_field @[to_additive add_cancel_monoid] instance cancel_monoid [∀ i, cancel_monoid $ f i] : cancel_monoid (Π i : I, f i) := by refine_struct { one := (1 : Π i, f i), mul := (*), npow := monoid.npow }; tactic.pi_instance_derive_field @[to_additive add_cancel_comm_monoid] instance cancel_comm_monoid [∀ i, cancel_comm_monoid $ f i] : cancel_comm_monoid (Π i : I, f i) := by refine_struct { one := (1 : Π i, f i), mul := (*), npow := monoid.npow }; tactic.pi_instance_derive_field instance mul_zero_class [∀ i, mul_zero_class $ f i] : mul_zero_class (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), mul := (*), .. }; tactic.pi_instance_derive_field instance mul_zero_one_class [∀ i, mul_zero_one_class $ f i] : mul_zero_one_class (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), one := (1 : Π i, f i), mul := (*), .. }; tactic.pi_instance_derive_field instance monoid_with_zero [∀ i, monoid_with_zero $ f i] : monoid_with_zero (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), one := (1 : Π i, f i), mul := (*), npow := monoid.npow }; tactic.pi_instance_derive_field instance comm_monoid_with_zero [∀ i, comm_monoid_with_zero $ f i] : comm_monoid_with_zero (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), one := (1 : Π i, f i), mul := (*), npow := monoid.npow }; tactic.pi_instance_derive_field section instance_lemmas open function variables {α β γ : Type*} @[simp, to_additive] lemma const_one [has_one β] : const α (1 : β) = 1 := rfl @[simp, to_additive] lemma comp_one [has_one β] {f : β → γ} : f ∘ 1 = const α (f 1) := rfl @[simp, to_additive] lemma one_comp [has_one γ] {f : α → β} : (1 : β → γ) ∘ f = 1 := rfl end instance_lemmas end pi section monoid_hom variables (f) [Π i, mul_one_class (f i)] /-- Evaluation of functions into an indexed collection of monoids at a point is a monoid homomorphism. This is `function.eval i` as a `monoid_hom`. -/ @[to_additive "Evaluation of functions into an indexed collection of additive monoids at a point is an additive monoid homomorphism. This is `function.eval i` as an `add_monoid_hom`.", simps] def pi.eval_monoid_hom (i : I) : (Π i, f i) →* f i := { to_fun := λ g, g i, map_one' := pi.one_apply i, map_mul' := λ x y, pi.mul_apply _ _ i, } /-- `function.const` as a `monoid_hom`. -/ @[to_additive "`function.const` as an `add_monoid_hom`.", simps] def pi.const_monoid_hom (α β : Type*) [mul_one_class β] : β →* (α → β) := { to_fun := function.const α, map_one' := rfl, map_mul' := λ _ _, rfl } /-- Coercion of a `monoid_hom` into a function is itself a `monoid_hom`. See also `monoid_hom.eval`. -/ @[to_additive "Coercion of an `add_monoid_hom` into a function is itself a `add_monoid_hom`. See also `add_monoid_hom.eval`. ", simps] def monoid_hom.coe_fn (α β : Type*) [mul_one_class α] [comm_monoid β] : (α →* β) →* (α → β) := { to_fun := λ g, g, map_one' := rfl, map_mul' := λ x y, rfl, } /-- Monoid homomorphism between the function spaces `I → α` and `I → β`, induced by a monoid homomorphism `f` between `α` and `β`. -/ @[to_additive "Additive monoid homomorphism between the function spaces `I → α` and `I → β`, induced by an additive monoid homomorphism `f` between `α` and `β`", simps] protected def monoid_hom.comp_left {α β : Type*} [mul_one_class α] [mul_one_class β] (f : α →* β) (I : Type*) : (I → α) →* (I → β) := { to_fun := λ h, f ∘ h, map_one' := by ext; simp, map_mul' := λ _ _, by ext; simp } end monoid_hom section single variables [decidable_eq I] open pi variables (f) /-- The zero-preserving homomorphism including a single value into a dependent family of values, as functions supported at a point. This is the `zero_hom` version of `pi.single`. -/ @[simps] def zero_hom.single [Π i, has_zero $ f i] (i : I) : zero_hom (f i) (Π i, f i) := { to_fun := single i, map_zero' := single_zero i } /-- The additive monoid homomorphism including a single additive monoid into a dependent family of additive monoids, as functions supported at a point. This is the `add_monoid_hom` version of `pi.single`. -/ @[simps] def add_monoid_hom.single [Π i, add_zero_class $ f i] (i : I) : f i →+ Π i, f i := { to_fun := single i, map_add' := single_op₂ (λ _, (+)) (λ _, zero_add _) _, .. (zero_hom.single f i) } /-- The multiplicative homomorphism including a single `mul_zero_class` into a dependent family of `mul_zero_class`es, as functions supported at a point. This is the `mul_hom` version of `pi.single`. -/ @[simps] def mul_hom.single [Π i, mul_zero_class $ f i] (i : I) : mul_hom (f i) (Π i, f i) := { to_fun := single i, map_mul' := single_op₂ (λ _, (*)) (λ _, zero_mul _) _, } variables {f} lemma pi.single_add [Π i, add_zero_class $ f i] (i : I) (x y : f i) : single i (x + y) = single i x + single i y := (add_monoid_hom.single f i).map_add x y lemma pi.single_neg [Π i, add_group $ f i] (i : I) (x : f i) : single i (-x) = -single i x := (add_monoid_hom.single f i).map_neg x lemma pi.single_sub [Π i, add_group $ f i] (i : I) (x y : f i) : single i (x - y) = single i x - single i y := (add_monoid_hom.single f i).map_sub x y lemma pi.single_mul [Π i, mul_zero_class $ f i] (i : I) (x y : f i) : single i (x * y) = single i x * single i y := (mul_hom.single f i).map_mul x y lemma pi.update_eq_sub_add_single [Π i, add_group $ f i] (g : Π (i : I), f i) (x : f i) : function.update g i x = g - single i (g i) + single i x := begin ext j, rcases eq_or_ne i j with rfl|h, { simp }, { simp [function.update_noteq h.symm, h] } end end single namespace function @[simp, to_additive] lemma update_one [Π i, has_one (f i)] [decidable_eq I] (i : I) : update (1 : Π i, f i) i 1 = 1 := update_eq_self i 1 @[to_additive] lemma update_mul [Π i, has_mul (f i)] [decidable_eq I] (f₁ f₂ : Π i, f i) (i : I) (x₁ : f i) (x₂ : f i) : update (f₁ * f₂) i (x₁ * x₂) = update f₁ i x₁ * update f₂ i x₂ := funext $ λ j, (apply_update₂ (λ i, (*)) f₁ f₂ i x₁ x₂ j).symm @[to_additive] lemma update_inv [Π i, has_inv (f i)] [decidable_eq I] (f₁ : Π i, f i) (i : I) (x₁ : f i) : update (f₁⁻¹) i (x₁⁻¹) = (update f₁ i x₁)⁻¹ := funext $ λ j, (apply_update (λ i, has_inv.inv) f₁ i x₁ j).symm @[to_additive] lemma update_div [Π i, has_div (f i)] [decidable_eq I] (f₁ f₂ : Π i, f i) (i : I) (x₁ : f i) (x₂ : f i) : update (f₁ / f₂) i (x₁ / x₂) = update f₁ i x₁ / update f₂ i x₂ := funext $ λ j, (apply_update₂ (λ i, (/)) f₁ f₂ i x₁ x₂ j).symm end function section piecewise @[to_additive] lemma set.piecewise_mul [Π i, has_mul (f i)] (s : set I) [Π i, decidable (i ∈ s)] (f₁ f₂ g₁ g₂ : Π i, f i) : s.piecewise (f₁ * f₂) (g₁ * g₂) = s.piecewise f₁ g₁ * s.piecewise f₂ g₂ := s.piecewise_op₂ _ _ _ _ (λ _, (*)) @[to_additive] lemma set.piecewise_inv [Π i, has_inv (f i)] (s : set I) [Π i, decidable (i ∈ s)] (f₁ g₁ : Π i, f i) : s.piecewise (f₁⁻¹) (g₁⁻¹) = (s.piecewise f₁ g₁)⁻¹ := s.piecewise_op f₁ g₁ (λ _ x, x⁻¹) @[to_additive] lemma set.piecewise_div [Π i, has_div (f i)] (s : set I) [Π i, decidable (i ∈ s)] (f₁ f₂ g₁ g₂ : Π i, f i) : s.piecewise (f₁ / f₂) (g₁ / g₂) = s.piecewise f₁ g₁ / s.piecewise f₂ g₂ := s.piecewise_op₂ _ _ _ _ (λ _, (/)) end piecewise section extend variables {ι : Type u} {η : Type v} (R : Type w) (s : ι → η) /-- `function.extend s f 1` as a bundled hom. -/ @[to_additive function.extend_by_zero.hom "`function.extend s f 0` as a bundled hom.", simps] noncomputable def function.extend_by_one.hom [mul_one_class R] : (ι → R) →* (η → R) := { to_fun := λ f, function.extend s f 1, map_one' := function.extend_one s, map_mul' := λ f g, by { simpa using function.extend_mul s f g 1 1 } } end extend
7ef70f38328ca2add3a4f5e11a2227e6eb455b81
97c8e5d8aca4afeebb5b335f26a492c53680efc8
/ground_zero/algebra.lean
e754df45586c4e950f113c2ed84585c48ab39844
[]
no_license
jfrancese/lean
cf32f0d8d5520b6f0e9d3987deb95841c553c53c
06e7efaecce4093d97fb5ecc75479df2ef1dbbdb
refs/heads/master
1,587,915,151,351
1,551,012,140,000
1,551,012,140,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,451
lean
import ground_zero.HITs.ntrunc open ground_zero.types.eq (pointed) open ground_zero.structures (hset) hott theory namespace ground_zero.algebra universes u v class magma (α : Type u) := (m : α → α → α) class pointed_magma (α : Type u) extends magma α := (e : α) class monoid (α : Type u) := (truncation : hset α) (mul : α → α → α) (e : α) (left_unit : Π (x : α), mul x e = x) (right_unit : Π (x : α), mul e x = x) (assoc : Π (x y z : α), mul x (mul y z) = mul (mul x y) z) infix ` · `:70 := monoid.mul class group (α : Type u) extends monoid α := (inv : α → α) (left_inv : Π (x : α), x · (inv x) = e) (right_inv : Π (x : α), (inv x) · x = e) def e {α : Type u} [group α] : α := monoid.e α theorem group_unit_is_unique {α : Type u} [group α] (e' : α) (left_unit' : Π x, x · e' = x) (right_unit' : Π x, e' · x = x) (H : e = e' → empty) : empty := begin have p := monoid.left_unit e', have q := right_unit' e, exact H (q⁻¹ ⬝ p) end section variables {α : Type u} {β : Type v} [group α] [group β] def is_homo (φ : α → β) := Π a b, φ (a · b) = φ a · φ b def Ker (φ : α → β) (H : is_homo φ) := Σ g, φ g = e def Im (φ : α → β) (H : is_homo φ) := Σ g f, φ g = f end prefix ⁻¹ := group.inv --def homotopy_group (X : pointed) (n : ℕ) := ∥Ω[n], X∥₀ --notation `π[` n `]` `, ` X := homotopy_group X n end ground_zero.algebra
476f68258224f3cba533739cabb72bc110621b2e
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/topology/algebra/uniform_filter_basis.lean
f0a56cb1d9e76892fd0785233167fbcf70114d40
[ "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
1,831
lean
/- Copyright (c) 2021 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import topology.algebra.filter_basis import topology.algebra.uniform_group /-! # Uniform properties of neighborhood bases in topological algebra This files contains properties of filter bases on algebraic structures that also require the theory of uniform spaces. The only result so far is a characterization of Cauchy filters in topological groups. -/ open_locale uniformity filter open filter namespace add_group_filter_basis variables {G : Type*} [add_comm_group G] (B : add_group_filter_basis G) /-- The uniform space structure associated to an abelian group filter basis via the associated topological abelian group structure. -/ protected def uniform_space : uniform_space G := @topological_add_group.to_uniform_space G _ B.topology B.is_topological_add_group /-- The uniform space structure associated to an abelian group filter basis via the associated topological abelian group structure is compatible with its group structure. -/ protected lemma uniform_add_group : @uniform_add_group G B.uniform_space _:= @topological_add_group_is_uniform G _ B.topology B.is_topological_add_group lemma cauchy_iff {F : filter G} : @cauchy G B.uniform_space F ↔ F.ne_bot ∧ ∀ U ∈ B, ∃ M ∈ F, ∀ x y ∈ M, y - x ∈ U := begin letI := B.uniform_space, haveI := B.uniform_add_group, suffices : F ×ᶠ F ≤ 𝓤 G ↔ ∀ U ∈ B, ∃ M ∈ F, ∀ x y ∈ M, y - x ∈ U, by split ; rintros ⟨h', h⟩ ; refine ⟨h', _⟩ ; [rwa ← this, rwa this], rw [uniformity_eq_comap_nhds_zero G, ← map_le_iff_le_comap], change tendsto _ _ _ ↔ _, simp [(basis_sets F).prod_self.tendsto_iff B.nhds_zero_has_basis] end end add_group_filter_basis
dd029ff19b16c145fad74ed19674d20be1ac393d
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/probability/martingale/centering.lean
06bbc68ec6a4af5b3147c94612ca89136555d905
[ "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
9,375
lean
/- Copyright (c) 2022 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import probability.martingale.basic /-! # Centering lemma for stochastic processes > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Any `ℕ`-indexed stochastic process which is adapted and integrable can be written as the sum of a martingale and a predictable process. This result is also known as **Doob's decomposition theorem**. From a process `f`, a filtration `ℱ` and a measure `μ`, we define two processes `martingale_part f ℱ μ` and `predictable_part f ℱ μ`. ## Main definitions * `measure_theory.predictable_part f ℱ μ`: a predictable process such that `f = predictable_part f ℱ μ + martingale_part f ℱ μ` * `measure_theory.martingale_part f ℱ μ`: a martingale such that `f = predictable_part f ℱ μ + martingale_part f ℱ μ` ## Main statements * `measure_theory.adapted_predictable_part`: `(λ n, predictable_part f ℱ μ (n+1))` is adapted. That is, `predictable_part` is predictable. * `measure_theory.martingale_martingale_part`: `martingale_part f ℱ μ` is a martingale. -/ open topological_space filter open_locale nnreal ennreal measure_theory probability_theory big_operators namespace measure_theory variables {Ω E : Type*} {m0 : measurable_space Ω} {μ : measure Ω} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] {f : ℕ → Ω → E} {ℱ : filtration ℕ m0} {n : ℕ} /-- Any `ℕ`-indexed stochastic process can be written as the sum of a martingale and a predictable process. This is the predictable process. See `martingale_part` for the martingale. -/ noncomputable def predictable_part {m0 : measurable_space Ω} (f : ℕ → Ω → E) (ℱ : filtration ℕ m0) (μ : measure Ω . volume_tac) : ℕ → Ω → E := λ n, ∑ i in finset.range n, μ[f (i+1) - f i | ℱ i] @[simp] lemma predictable_part_zero : predictable_part f ℱ μ 0 = 0 := by simp_rw [predictable_part, finset.range_zero, finset.sum_empty] lemma adapted_predictable_part : adapted ℱ (λ n, predictable_part f ℱ μ (n+1)) := λ n, finset.strongly_measurable_sum' _ (λ i hin, strongly_measurable_condexp.mono (ℱ.mono (finset.mem_range_succ_iff.mp hin))) lemma adapted_predictable_part' : adapted ℱ (λ n, predictable_part f ℱ μ n) := λ n, finset.strongly_measurable_sum' _ (λ i hin, strongly_measurable_condexp.mono (ℱ.mono (finset.mem_range_le hin))) /-- Any `ℕ`-indexed stochastic process can be written as the sum of a martingale and a predictable process. This is the martingale. See `predictable_part` for the predictable process. -/ noncomputable def martingale_part {m0 : measurable_space Ω} (f : ℕ → Ω → E) (ℱ : filtration ℕ m0) (μ : measure Ω . volume_tac) : ℕ → Ω → E := λ n, f n - predictable_part f ℱ μ n lemma martingale_part_add_predictable_part (ℱ : filtration ℕ m0) (μ : measure Ω) (f : ℕ → Ω → E) : martingale_part f ℱ μ + predictable_part f ℱ μ = f := sub_add_cancel _ _ lemma martingale_part_eq_sum : martingale_part f ℱ μ = λ n, f 0 + ∑ i in finset.range n, (f (i+1) - f i - μ[f (i+1) - f i | ℱ i]) := begin rw [martingale_part, predictable_part], ext1 n, rw [finset.eq_sum_range_sub f n, ← add_sub, ← finset.sum_sub_distrib], end lemma adapted_martingale_part (hf : adapted ℱ f) : adapted ℱ (martingale_part f ℱ μ) := adapted.sub hf adapted_predictable_part' lemma integrable_martingale_part (hf_int : ∀ n, integrable (f n) μ) (n : ℕ) : integrable (martingale_part f ℱ μ n) μ := begin rw martingale_part_eq_sum, exact (hf_int 0).add (integrable_finset_sum' _ (λ i hi, ((hf_int _).sub (hf_int _)).sub integrable_condexp)), end lemma martingale_martingale_part (hf : adapted ℱ f) (hf_int : ∀ n, integrable (f n) μ) [sigma_finite_filtration μ ℱ] : martingale (martingale_part f ℱ μ) ℱ μ := begin refine ⟨adapted_martingale_part hf, λ i j hij, _⟩, -- ⊢ μ[martingale_part f ℱ μ j | ℱ i] =ᵐ[μ] martingale_part f ℱ μ i have h_eq_sum : μ[martingale_part f ℱ μ j | ℱ i] =ᵐ[μ] f 0 + ∑ k in finset.range j, (μ[f (k+1) - f k | ℱ i] - μ[μ[f (k+1) - f k | ℱ k] | ℱ i]), { rw martingale_part_eq_sum, refine (condexp_add (hf_int 0) _).trans _, { exact integrable_finset_sum' _ (λ i hij, ((hf_int _).sub (hf_int _)).sub integrable_condexp), }, refine (eventually_eq.add eventually_eq.rfl (condexp_finset_sum (λ i hij, _))).trans _, { exact ((hf_int _).sub (hf_int _)).sub integrable_condexp, }, refine eventually_eq.add _ _, { rw condexp_of_strongly_measurable (ℱ.le _) _ (hf_int 0), { apply_instance, }, { exact (hf 0).mono (ℱ.mono (zero_le i)), }, }, { exact eventually_eq_sum (λ k hkj, condexp_sub ((hf_int _).sub (hf_int _)) integrable_condexp), }, }, refine h_eq_sum.trans _, have h_ge : ∀ k, i ≤ k → μ[f (k + 1) - f k|ℱ i] - μ[μ[f (k + 1) - f k|ℱ k]|ℱ i] =ᵐ[μ] 0, { intros k hk, have : μ[μ[f (k + 1) - f k|ℱ k]|ℱ i] =ᵐ[μ] μ[f (k + 1) - f k|ℱ i], { exact condexp_condexp_of_le (ℱ.mono hk) (ℱ.le k), }, filter_upwards [this] with x hx, rw [pi.sub_apply, pi.zero_apply, hx, sub_self], }, have h_lt : ∀ k, k < i → μ[f (k + 1) - f k|ℱ i] - μ[μ[f (k + 1) - f k|ℱ k]|ℱ i] =ᵐ[μ] f (k + 1) - f k - μ[f (k + 1) - f k|ℱ k], { refine λ k hk, eventually_eq.sub _ _, { rw condexp_of_strongly_measurable, { exact ((hf (k+1)).mono (ℱ.mono (nat.succ_le_of_lt hk))).sub ((hf k).mono (ℱ.mono hk.le)), }, { exact (hf_int _).sub (hf_int _), }, }, { rw condexp_of_strongly_measurable, { exact strongly_measurable_condexp.mono (ℱ.mono hk.le), }, { exact integrable_condexp, }, }, }, rw martingale_part_eq_sum, refine eventually_eq.add eventually_eq.rfl _, rw [← finset.sum_range_add_sum_Ico _ hij, ← add_zero (∑ i in finset.range i, (f (i + 1) - f i - μ[f (i + 1) - f i | ℱ i]))], refine (eventually_eq_sum (λ k hk, h_lt k (finset.mem_range.mp hk))).add _, refine (eventually_eq_sum (λ k hk, h_ge k (finset.mem_Ico.mp hk).1)).trans _, simp only [finset.sum_const_zero, pi.zero_apply], refl, end -- The following two lemmas demonstrate the essential uniqueness of the decomposition lemma martingale_part_add_ae_eq [sigma_finite_filtration μ ℱ] {f g : ℕ → Ω → E} (hf : martingale f ℱ μ) (hg : adapted ℱ (λ n, g (n + 1))) (hg0 : g 0 = 0) (hgint : ∀ n, integrable (g n) μ) (n : ℕ) : martingale_part (f + g) ℱ μ n =ᵐ[μ] f n := begin set h := f - martingale_part (f + g) ℱ μ with hhdef, have hh : h = predictable_part (f + g) ℱ μ - g, { rw [hhdef, sub_eq_sub_iff_add_eq_add, add_comm (predictable_part (f + g) ℱ μ), martingale_part_add_predictable_part] }, have hhpred : adapted ℱ (λ n, h (n + 1)), { rw hh, exact adapted_predictable_part.sub hg }, have hhmgle : martingale h ℱ μ := hf.sub (martingale_martingale_part (hf.adapted.add $ predictable.adapted hg $ hg0.symm ▸ strongly_measurable_zero) $ λ n, (hf.integrable n).add $ hgint n), refine (eventually_eq_iff_sub.2 _).symm, filter_upwards [hhmgle.eq_zero_of_predictable hhpred n] with ω hω, rw [hhdef, pi.sub_apply] at hω, rw [hω, pi.sub_apply, martingale_part], simp [hg0], end lemma predictable_part_add_ae_eq [sigma_finite_filtration μ ℱ] {f g : ℕ → Ω → E} (hf : martingale f ℱ μ) (hg : adapted ℱ (λ n, g (n + 1))) (hg0 : g 0 = 0) (hgint : ∀ n, integrable (g n) μ) (n : ℕ) : predictable_part (f + g) ℱ μ n =ᵐ[μ] g n := begin filter_upwards [martingale_part_add_ae_eq hf hg hg0 hgint n] with ω hω, rw ← add_right_inj (f n ω), conv_rhs { rw [← pi.add_apply, ← pi.add_apply, ← martingale_part_add_predictable_part ℱ μ (f + g)] }, rw [pi.add_apply, pi.add_apply, hω], end section difference lemma predictable_part_bdd_difference {R : ℝ≥0} {f : ℕ → Ω → ℝ} (ℱ : filtration ℕ m0) (hbdd : ∀ᵐ ω ∂μ, ∀ i, |f (i + 1) ω - f i ω| ≤ R) : ∀ᵐ ω ∂μ, ∀ i, |predictable_part f ℱ μ (i + 1) ω - predictable_part f ℱ μ i ω| ≤ R := begin simp_rw [predictable_part, finset.sum_apply, finset.sum_range_succ_sub_sum], exact ae_all_iff.2 (λ i, ae_bdd_condexp_of_ae_bdd $ ae_all_iff.1 hbdd i), end lemma martingale_part_bdd_difference {R : ℝ≥0} {f : ℕ → Ω → ℝ} (ℱ : filtration ℕ m0) (hbdd : ∀ᵐ ω ∂μ, ∀ i, |f (i + 1) ω - f i ω| ≤ R) : ∀ᵐ ω ∂μ, ∀ i, |martingale_part f ℱ μ (i + 1) ω - martingale_part f ℱ μ i ω| ≤ ↑(2 * R) := begin filter_upwards [hbdd, predictable_part_bdd_difference ℱ hbdd] with ω hω₁ hω₂ i, simp only [two_mul, martingale_part, pi.sub_apply], have : |f (i + 1) ω - predictable_part f ℱ μ (i + 1) ω - (f i ω - predictable_part f ℱ μ i ω)| = |(f (i + 1) ω - f i ω) - (predictable_part f ℱ μ (i + 1) ω - predictable_part f ℱ μ i ω)|, { ring_nf }, -- `ring` suggests `ring_nf` despite proving the goal rw this, exact (abs_sub _ _).trans (add_le_add (hω₁ i) (hω₂ i)), end end difference end measure_theory
62100b677154fa7e0349bf13b33a5c3af5c10250
e61a235b8468b03aee0120bf26ec615c045005d2
/stage0/src/Init/Lean/Elab/ResolveName.lean
bf6208a7b6544f83613807956ac80a3cf3c9feed
[ "Apache-2.0" ]
permissive
SCKelemen/lean4
140dc63a80539f7c61c8e43e1c174d8500ec3230
e10507e6615ddbef73d67b0b6c7f1e4cecdd82bc
refs/heads/master
1,660,973,595,917
1,590,278,033,000
1,590,278,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,679
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ prelude import Init.Lean.Hygiene import Init.Lean.Modifiers import Init.Lean.Elab.Alias namespace Lean namespace Elab inductive OpenDecl | simple (ns : Name) (except : List Name) | explicit (id : Name) (declName : Name) namespace OpenDecl instance : Inhabited OpenDecl := ⟨simple Name.anonymous []⟩ instance : HasToString OpenDecl := ⟨fun decl => match decl with | explicit id decl => toString id ++ " → " ++ toString decl | simple ns ex => toString ns ++ (if ex == [] then "" else " hiding " ++ toString ex)⟩ end OpenDecl def rootNamespace := `_root_ def removeRoot (n : Name) : Name := n.replacePrefix rootNamespace Name.anonymous /- Global name resolution -/ /- Check whether `ns ++ id` is a valid namepace name and/or there are aliases names `ns ++ id`. -/ private def resolveQualifiedName (env : Environment) (ns : Name) (id : Name) : List Name := let resolvedId := ns ++ id; let resolvedIds := getAliases env resolvedId; if env.contains resolvedId && (!id.isAtomic || !isProtected env resolvedId) then resolvedId :: resolvedIds else -- Check whether environment contains the private version. That is, `_private.<module_name>.ns.id`. let resolvedIdPrv := mkPrivateName env resolvedId; if env.contains resolvedIdPrv then resolvedIdPrv :: resolvedIds else resolvedIds /- Check surrounding namespaces -/ private def resolveUsingNamespace (env : Environment) (id : Name) : Name → List Name | ns@(Name.str p _ _) => match resolveQualifiedName env ns id with | [] => resolveUsingNamespace p | resolvedIds => resolvedIds | _ => [] /- Check exact name -/ private def resolveExact (env : Environment) (id : Name) : Option Name := if id.isAtomic then none else let resolvedId := id.replacePrefix rootNamespace Name.anonymous; if env.contains resolvedId then some resolvedId else -- We also allow `_root` when accessing private declarations. -- If we change our minds, we should just replace `resolvedId` with `id` let resolvedIdPrv := mkPrivateName env resolvedId; if env.contains resolvedIdPrv then some resolvedIdPrv else none /- Check open namespaces -/ private def resolveOpenDecls (env : Environment) (id : Name) : List OpenDecl → List Name → List Name | [], resolvedIds => resolvedIds | OpenDecl.simple ns exs :: openDecls, resolvedIds => if exs.elem id then resolveOpenDecls openDecls resolvedIds else let newResolvedIds := resolveQualifiedName env ns id; resolveOpenDecls openDecls (newResolvedIds ++ resolvedIds) | OpenDecl.explicit openedId resolvedId :: openDecls, resolvedIds => let resolvedIds := if id == openedId then resolvedId :: resolvedIds else resolvedIds; resolveOpenDecls openDecls resolvedIds private def resolveGlobalNameAux (env : Environment) (ns : Name) (openDecls : List OpenDecl) (scpView : MacroScopesView) : Name → List String → List (Name × List String) | id@(Name.str p s _), projs => -- NOTE: we assume that macro scopes always belong to the projected constant, not the projections let id := { scpView with name := id }.review; match resolveUsingNamespace env id ns with | resolvedIds@(_ :: _) => resolvedIds.eraseDups.map $ fun id => (id, projs) | [] => match resolveExact env id with | some newId => [(newId, projs)] | none => let resolvedIds := if env.contains id then [id] else []; let resolvedIds := resolveOpenDecls env id openDecls resolvedIds; let resolvedIds := getAliases env id ++ resolvedIds; match resolvedIds with | resolvedIds@(_ :: _) => resolvedIds.eraseDups.map $ fun id => (id, projs) | [] => resolveGlobalNameAux p (s::projs) | _, _ => [] def resolveGlobalName (env : Environment) (ns : Name) (openDecls : List OpenDecl) (id : Name) : List (Name × List String) := -- decode macro scopes from name before recursion let extractionResult := extractMacroScopes id; resolveGlobalNameAux env ns openDecls extractionResult extractionResult.name [] /- Namespace resolution -/ def resolveNamespaceUsingScope (env : Environment) (n : Name) : Name → Option Name | Name.anonymous => none | ns@(Name.str p _ _) => if isNamespace env (ns ++ n) then some (ns ++ n) else resolveNamespaceUsingScope p | _ => unreachable! def resolveNamespaceUsingOpenDecls (env : Environment) (n : Name) : List OpenDecl → Option Name | [] => none | OpenDecl.simple ns [] :: ds => if isNamespace env (ns ++ n) then some (ns ++ n) else resolveNamespaceUsingOpenDecls ds | _ :: ds => resolveNamespaceUsingOpenDecls ds /- Given a name `id` try to find namespace it refers to. The resolution procedure works as follows 1- If `id` is the extact name of an existing namespace, then return `id` 2- If `id` is in the scope of `namespace` commands the namespace `s_1. ... . s_n`, then return `s_1 . ... . s_i ++ n` if it is the name of an existing namespace. We search "backwards". 3- Finally, for each command `open N`, return `N ++ n` if it is the name of an existing namespace. We search "backwards" again. That is, we try the most recent `open` command first. We only consider simple `open` commands. -/ def resolveNamespace (env : Environment) (ns : Name) (openDecls : List OpenDecl) (id : Name) : Option Name := if isNamespace env id then some id else match resolveNamespaceUsingScope env id ns with | some n => some n | none => match resolveNamespaceUsingOpenDecls env id openDecls with | some n => some n | none => none end Elab end Lean
4295d1a53b62d70af3bbd457678adf5b33e0d432
9d2e3d5a2e2342a283affd97eead310c3b528a24
/src/for_mathlib/category_theory/natural_isomorphism.lean
b438dbceb89baf52a5a16fddf1126ae64cb0b87a
[]
permissive
Vtec234/lftcm2020
ad2610ab614beefe44acc5622bb4a7fff9a5ea46
bbbd4c8162f8c2ef602300ab8fdeca231886375d
refs/heads/master
1,668,808,098,623
1,594,989,081,000
1,594,990,079,000
280,423,039
0
0
MIT
1,594,990,209,000
1,594,990,209,000
null
UTF-8
Lean
false
false
1,908
lean
import category_theory.equivalence open category_theory universes v₁ v₂ u₁ u₂ variables {C : Type u₁} [category.{v₁} C] variables {D : Type u₂} [category.{v₂} D] variables (F G : C ⥤ D) (α : F ≅ G) /-! Unfortunately we need a separate set of cancellation lemmas for components of natural isomorphisms, because the `simp` normal form is `α.hom.app X`, rather than `α.app.hom X`. (With the later, the morphism would be visibly part of an isomorphism, so general lemmas about isomorphisms would apply.) In the future, we should consider a redesign that changes this simp norm form, but for now it breaks too many proofs. -/ namespace category_theory.iso @[simp] lemma cancel_nat_iso_hom_left {X : C} {Z : D} (g g' : G.obj X ⟶ Z) : α.hom.app X ≫ g = α.hom.app X ≫ g' ↔ g = g' := by simp only [cancel_epi] @[simp] lemma cancel_nat_iso_inv_left {X : C} {Z : D} (g g' : F.obj X ⟶ Z) : α.inv.app X ≫ g = α.inv.app X ≫ g' ↔ g = g' := by simp only [cancel_epi] @[simp] lemma cancel_nat_iso_hom_right {X : D} {Y : C} (f f' : X ⟶ F.obj Y) : f ≫ α.hom.app Y = f' ≫ α.hom.app Y ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_nat_iso_inv_right {X : D} {Y : C} (f f' : X ⟶ G.obj Y) : f ≫ α.inv.app Y = f' ≫ α.inv.app Y ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_nat_iso_hom_right_assoc {W X X' : D} {Y : C} (f : W ⟶ X) (g : X ⟶ F.obj Y) (f' : W ⟶ X') (g' : X' ⟶ F.obj Y) : f ≫ g ≫ α.hom.app Y = f' ≫ g' ≫ α.hom.app Y ↔ f ≫ g = f' ≫ g' := by simp only [←category.assoc, cancel_mono] @[simp] lemma cancel_nat_iso_inv_right_assoc {W X X' : D} {Y : C} (f : W ⟶ X) (g : X ⟶ G.obj Y) (f' : W ⟶ X') (g' : X' ⟶ G.obj Y) : f ≫ g ≫ α.inv.app Y = f' ≫ g' ≫ α.inv.app Y ↔ f ≫ g = f' ≫ g' := by simp only [←category.assoc, cancel_mono] end category_theory.iso
1bea669ad6a3602852b212128899453f44d188f3
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/meta/derive.lean
d9a42d0adbcdcde02b2e48d0a0c428799dc91161
[ "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
4,112
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich Attribute that can automatically derive typeclass instances. -/ prelude import init.meta.attribute import init.meta.interactive_base import init.meta.mk_has_reflect_instance import init.meta.mk_has_sizeof_instance import init.meta.mk_inhabited_instance open lean open interactive.types open tactic /-- A handler that may or may not be able to implement the typeclass `cls` for `decl`. It should return `tt` if it was able to derive `cls` and `ff` if it does not know how to derive `cls`, in which case lower-priority handlers will be tried next. -/ meta def derive_handler := Π (cls : pexpr) (decl : name), tactic bool @[user_attribute] meta def derive_handler_attr : user_attribute := { name := `derive_handler, descr := "register a definition of type `derive_handler` for use in the [derive] attribute" } private meta def try_handlers (p : pexpr) (n : name) : list derive_handler → tactic unit | [] := fail format!"failed to find a derive handler for '{p}'" | (h::hs) := do success ← h p n, when (¬success) $ try_handlers hs @[user_attribute] meta def derive_attr : user_attribute unit (list pexpr) := { name := `derive, descr := "automatically derive typeclass instances", parser := pexpr_list_or_texpr, after_set := some (λ n _ _, do ps ← derive_attr.get_param n, handlers ← attribute.get_instances `derive_handler, handlers ← handlers.mmap (λ n, eval_expr derive_handler (expr.const n [])), ps.mmap' (λ p, try_handlers p n handlers)) } /-- Given a tactic `tac` that can solve an application of `cls` in the right context, `instance_derive_handler` uses it to build an instance declaration of `cls n`. -/ meta def instance_derive_handler (cls : name) (tac : tactic unit) (univ_poly := tt) (modify_target : name → list expr → expr → tactic expr := λ _ _, pure) : derive_handler := λ p n, if p.is_constant_of cls then do decl ← get_decl n, cls_decl ← get_decl cls, env ← get_env, guard (env.is_inductive n) <|> fail format!"failed to derive '{cls}', '{n}' is not an inductive type", let ls := decl.univ_params.map $ λ n, if univ_poly then level.param n else level.zero, -- incrementally build up target expression `Π (hp : p) [cls hp] ..., cls (n.{ls} hp ...)` -- where `p ...` are the inductive parameter types of `n` let tgt : expr := expr.const n ls, ⟨params, _⟩ ← mk_local_pis (decl.type.instantiate_univ_params (decl.univ_params.zip ls)), let tgt := tgt.mk_app params, tgt ← mk_app cls [tgt], tgt ← modify_target n params tgt, tgt ← params.enum.mfoldr (λ ⟨i, param⟩ tgt, do -- add typeclass hypothesis for each inductive parameter tgt ← do { guard $ i < env.inductive_num_params n, param_cls ← mk_app cls [param], -- TODO(sullrich): omit some typeclass parameters based on usage of `param`? pure $ expr.pi `a binder_info.inst_implicit param_cls tgt } <|> pure tgt, pure $ tgt.bind_pi param ) tgt, (_, val) ← tactic.solve_aux tgt (intros >> tac), val ← instantiate_mvars val, let trusted := decl.is_trusted ∧ cls_decl.is_trusted, add_protected_decl (declaration.defn (n ++ cls) (if univ_poly then decl.univ_params else []) tgt val reducibility_hints.abbrev trusted), set_basic_attribute `instance (n ++ cls) tt, pure true else pure false @[derive_handler] meta def has_reflect_derive_handler := instance_derive_handler ``has_reflect mk_has_reflect_instance ff (λ n params tgt, -- add additional `reflected` assumption for each parameter params.mfoldr (λ param tgt, do param_cls ← mk_mapp `reflected [none, some param], pure $ expr.pi `a binder_info.inst_implicit param_cls tgt ) tgt) @[derive_handler] meta def has_sizeof_derive_handler := instance_derive_handler ``has_sizeof mk_has_sizeof_instance attribute [derive has_reflect] bool prod sum option interactive.loc pos
5d204390c99f759e8f036ae3dd8cf0396dd12ae5
922aabadca677ef935a2394c546f8ec097ee04a8
/src/and, or.lean
797ef22512a774bda3c3965dbe32006998fb644a
[ "MIT" ]
permissive
apurvanakade/uwo2021-CUMC
14afe4201c54cf42e2bdbc640a5e5e15aec720ba
0be9402011feda35e510725449686c0af3c3761e
refs/heads/master
1,688,802,880,363
1,629,752,690,000
1,629,752,690,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,414
lean
variables P Q R : Prop /-------------------------------------------------------------------------- ``cases`` ``cases`` is a tactic that breaks up complicated terms. If ``hpq`` is a proof of ``P ∧ Q`` or ``P ∨ Q``, then use ``cases hpq,``. English translation: If ``P ∧ Q`` is true then both ``P`` and ``Q`` are true. If ``P ∨ Q`` is true then either ``P`` or ``Q`` is true. --------------------------------------------------------------------------/ theorem ex1 : P ∧ Q → P := begin intro hpq, cases hpq, exact hpq_left, end theorem ex2 : P ∨ P → P := begin intro hpp, cases hpp, exact hpp, exact hpp, end /-------------------------------------------------------------------------- ``split`` If the current target is ``P ∧ Q`` then use ``split,``. English translation: To prove ``P ∧ Q`` we need to prove ``P`` and ``Q`` separately. --------------------------------------------------------------------------/ theorem ex3 (hp : P) (hq : Q) : P ∧ Q := begin split, exact hp, exact hq, end /-------------------------------------------------------------------------- ``left``/``right`` If the current target is ``P ∨ Q``, then use either ``left,`` or ``right,`` (choose wisely). English translation: To prove ``P ∨ Q`` we need to prove either ``P`` (left) or ``Q`` (right). --------------------------------------------------------------------------/ theorem ex4 : P → (P ∨ Q) := begin intro hp, left, exact hp, end /-------------------------------------------------------------------------- Delete the ``sorry,`` below and replace them with valid proofs. Don't forget the ``,`` at the end of each tactic. --------------------------------------------------------------------------/ theorem comm_and : P ∧ Q → Q ∧ P := begin sorry, end -- Be careful: the order of tactics matters, just like in math! theorem comm_or : P ∨ Q → Q ∨ P := begin sorry, end theorem currying : (P ∧ Q → R) → (P → (Q → R)) := begin sorry, end theorem or_implies : ((P ∨ Q) → R) → (P → R) ∧ (Q → R) := begin sorry, end theorem or_implies_converse : (P → R) ∧ (Q → R) → ((P ∨ Q) → R):= begin sorry, end -- you can also use ``apply`` for a sequence of implications ``P → Q → R`` -- if the target is ``R``. theorem uncurrying : (P → (Q → R)) → (P ∧ Q → R) := begin sorry, end
5277bdda56d6041469beb1b405915a907496d23f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/field/defs.lean
73ddad343d0d0e94537c3ec65067776dc3f2de2f
[ "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
9,408
lean
/- Copyright (c) 2014 Robert Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis, Leonardo de Moura, Johannes Hölzl, Mario Carneiro -/ import data.rat.init import algebra.ring.defs /-! # Division (semi)rings and (semi)fields > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file introduces fields and division rings (also known as skewfields) and proves some basic statements about them. For a more extensive theory of fields, see the `field_theory` folder. ## Main definitions * `division_semiring`: Nontrivial semiring with multiplicative inverses for nonzero elements. * `division_ring`: : Nontrivial ring with multiplicative inverses for nonzero elements. * `semifield`: Commutative division semiring. * `field`: Commutative division ring. * `is_field`: Predicate on a (semi)ring that it is a (semi)field, i.e. that the multiplication is commutative, that it has more than one element and that all non-zero elements have a multiplicative inverse. In contrast to `field`, which contains the data of a function associating to an element of the field its multiplicative inverse, this predicate only assumes the existence and can therefore more easily be used to e.g. transfer along ring isomorphisms. ## Implementation details By convention `0⁻¹ = 0` in a field or division ring. This is due to the fact that working with total functions has the advantage of not constantly having to check that `x ≠ 0` when writing `x⁻¹`. With this convention in place, some statements like `(a + b) * c⁻¹ = a * c⁻¹ + b * c⁻¹` still remain true, while others like the defining property `a * a⁻¹ = 1` need the assumption `a ≠ 0`. If you are a beginner in using Lean and are confused by that, you can read more about why this convention is taken in Kevin Buzzard's [blogpost](https://xenaproject.wordpress.com/2020/07/05/division-by-zero-in-type-theory-a-faq/) A division ring or field is an example of a `group_with_zero`. If you cannot find a division ring / field lemma that does not involve `+`, you can try looking for a `group_with_zero` lemma instead. ## Tags field, division ring, skew field, skew-field, skewfield -/ open function set set_option old_structure_cmd true universe u variables {α β K : Type*} /-- The default definition of the coercion `(↑(a : ℚ) : K)` for a division ring `K` is defined as `(a / b : K) = (a : K) * (b : K)⁻¹`. Use `coe` instead of `rat.cast_rec` for better definitional behaviour. -/ def rat.cast_rec [has_lift_t ℕ K] [has_lift_t ℤ K] [has_mul K] [has_inv K] : ℚ → K | ⟨a, b, _, _⟩ := ↑a * (↑b)⁻¹ /-- Type class for the canonical homomorphism `ℚ → K`. -/ @[protect_proj] class has_rat_cast (K : Type u) := (rat_cast : ℚ → K) /-- The default definition of the scalar multiplication `(a : ℚ) • (x : K)` for a division ring `K` is given by `a • x = (↑ a) * x`. Use `(a : ℚ) • (x : K)` instead of `qsmul_rec` for better definitional behaviour. -/ def qsmul_rec (coe : ℚ → K) [has_mul K] (a : ℚ) (x : K) : K := coe a * x /-- A `division_semiring` is a `semiring` with multiplicative inverses for nonzero elements. -/ @[protect_proj, ancestor semiring group_with_zero] class division_semiring (α : Type*) extends semiring α, group_with_zero α /-- A `division_ring` is a `ring` with multiplicative inverses for nonzero elements. An instance of `division_ring K` includes maps `rat_cast : ℚ → K` and `qsmul : ℚ → K → K`. If the division ring has positive characteristic p, we define `rat_cast (1 / p) = 1 / 0 = 0` for consistency with our division by zero convention. The fields `rat_cast` and `qsmul` are needed to implement the `algebra_rat [division_ring K] : algebra ℚ K` instance, since we need to control the specific definitions for some special cases of `K` (in particular `K = ℚ` itself). See also Note [forgetful inheritance]. -/ @[protect_proj, ancestor ring div_inv_monoid nontrivial] class division_ring (K : Type u) extends ring K, div_inv_monoid K, nontrivial K, has_rat_cast K := (mul_inv_cancel : ∀ {a : K}, a ≠ 0 → a * a⁻¹ = 1) (inv_zero : (0 : K)⁻¹ = 0) (rat_cast := rat.cast_rec) (rat_cast_mk : ∀ (a : ℤ) (b : ℕ) h1 h2, rat_cast ⟨a, b, h1, h2⟩ = a * b⁻¹ . try_refl_tac) (qsmul : ℚ → K → K := qsmul_rec rat_cast) (qsmul_eq_mul' : ∀ (a : ℚ) (x : K), qsmul a x = rat_cast a * x . try_refl_tac) @[priority 100] -- see Note [lower instance priority] instance division_ring.to_division_semiring [division_ring α] : division_semiring α := { ..‹division_ring α›, ..(infer_instance : semiring α) } /-- A `semifield` is a `comm_semiring` with multiplicative inverses for nonzero elements. -/ @[protect_proj, ancestor comm_semiring division_semiring comm_group_with_zero] class semifield (α : Type*) extends comm_semiring α, division_semiring α, comm_group_with_zero α /-- A `field` is a `comm_ring` with multiplicative inverses for nonzero elements. An instance of `field K` includes maps `of_rat : ℚ → K` and `qsmul : ℚ → K → K`. If the field has positive characteristic p, we define `of_rat (1 / p) = 1 / 0 = 0` for consistency with our division by zero convention. The fields `of_rat` and `qsmul are needed to implement the `algebra_rat [division_ring K] : algebra ℚ K` instance, since we need to control the specific definitions for some special cases of `K` (in particular `K = ℚ` itself). See also Note [forgetful inheritance]. -/ @[protect_proj, ancestor comm_ring div_inv_monoid nontrivial] class field (K : Type u) extends comm_ring K, division_ring K section division_ring variables [division_ring K] {a b : K} namespace rat /-- Construct the canonical injection from `ℚ` into an arbitrary division ring. If the field has positive characteristic `p`, we define `1 / p = 1 / 0 = 0` for consistency with our division by zero convention. -/ -- see Note [coercion into rings] @[priority 900] instance cast_coe {K : Type*} [has_rat_cast K] : has_coe_t ℚ K := ⟨has_rat_cast.rat_cast⟩ theorem cast_mk' (a b h1 h2) : ((⟨a, b, h1, h2⟩ : ℚ) : K) = a * b⁻¹ := division_ring.rat_cast_mk _ _ _ _ theorem cast_def : ∀ (r : ℚ), (r : K) = r.num / r.denom | ⟨a, b, h1, h2⟩ := (cast_mk' _ _ _ _).trans (div_eq_mul_inv _ _).symm @[priority 100] instance smul_division_ring : has_smul ℚ K := ⟨division_ring.qsmul⟩ lemma smul_def (a : ℚ) (x : K) : a • x = ↑a * x := division_ring.qsmul_eq_mul' a x @[simp] lemma smul_one_eq_coe (A : Type*) [division_ring A] (m : ℚ) : m • (1 : A) = ↑m := by rw [rat.smul_def, mul_one] end rat end division_ring section field variable [field K] @[priority 100] -- see Note [lower instance priority] instance field.to_semifield : semifield K := { .. ‹field K›, .. (infer_instance : semiring K) } end field section is_field /-- A predicate to express that a (semi)ring is a (semi)field. This is mainly useful because such a predicate does not contain data, and can therefore be easily transported along ring isomorphisms. Additionaly, this is useful when trying to prove that a particular ring structure extends to a (semi)field. -/ structure is_field (R : Type u) [semiring R] : Prop := (exists_pair_ne : ∃ (x y : R), x ≠ y) (mul_comm : ∀ (x y : R), x * y = y * x) (mul_inv_cancel : ∀ {a : R}, a ≠ 0 → ∃ b, a * b = 1) /-- Transferring from `semifield` to `is_field`. -/ lemma semifield.to_is_field (R : Type u) [semifield R] : is_field R := { mul_inv_cancel := λ a ha, ⟨a⁻¹, mul_inv_cancel ha⟩, ..‹semifield R› } /-- Transferring from `field` to `is_field`. -/ lemma field.to_is_field (R : Type u) [field R] : is_field R := semifield.to_is_field _ @[simp] lemma is_field.nontrivial {R : Type u} [semiring R] (h : is_field R) : nontrivial R := ⟨h.exists_pair_ne⟩ @[simp] lemma not_is_field_of_subsingleton (R : Type u) [semiring R] [subsingleton R] : ¬is_field R := λ h, let ⟨x, y, h⟩ := h.exists_pair_ne in h (subsingleton.elim _ _) open_locale classical /-- Transferring from `is_field` to `semifield`. -/ noncomputable def is_field.to_semifield {R : Type u} [semiring R] (h : is_field R) : semifield R := { inv := λ a, if ha : a = 0 then 0 else classical.some (is_field.mul_inv_cancel h ha), inv_zero := dif_pos rfl, mul_inv_cancel := λ a ha, begin convert classical.some_spec (is_field.mul_inv_cancel h ha), exact dif_neg ha end, .. ‹semiring R›, ..h } /-- Transferring from `is_field` to `field`. -/ noncomputable def is_field.to_field {R : Type u} [ring R] (h : is_field R) : field R := { .. ‹ring R›, ..is_field.to_semifield h } /-- For each field, and for each nonzero element of said field, there is a unique inverse. Since `is_field` doesn't remember the data of an `inv` function and as such, a lemma that there is a unique inverse could be useful. -/ lemma uniq_inv_of_is_field (R : Type u) [ring R] (hf : is_field R) : ∀ (x : R), x ≠ 0 → ∃! (y : R), x * y = 1 := begin intros x hx, apply exists_unique_of_exists_of_unique, { exact hf.mul_inv_cancel hx }, { intros y z hxy hxz, calc y = y * (x * z) : by rw [hxz, mul_one] ... = (x * y) * z : by rw [← mul_assoc, hf.mul_comm y x] ... = z : by rw [hxy, one_mul] } end end is_field
3ee0338d154b94685a282efec74b2fcdf67773c6
7b66d83f3b69dae0a3dfb684d7ebe5e9e3f3c913
/src/demos/linalg.lean
e5e04367a0f7b31f00df8b92947ed14f5ac63bc7
[]
permissive
dpochekutov/lftcm2020
58a09e10f0e638075b97884d3c2612eb90296adb
cdfbf1ac089f21058e523db73f2476c9c98ed16a
refs/heads/master
1,669,226,265,076
1,594,629,725,000
1,594,629,725,000
279,213,346
1
0
MIT
1,594,622,757,000
1,594,615,843,000
null
UTF-8
Lean
false
false
11,081
lean
import algebra.module import algebra.pi_instances import analysis.normed_space.real_inner_product import data.matrix.notation import linear_algebra.basic import linear_algebra.bilinear_form import linear_algebra.quadratic_form import linear_algebra.finsupp import tactic /- According to Wikipedia, everyone's favourite reliable source of knowledge, linear algebra studies linear equations and linear maps, representing them in vector spaces and through matrices. So let us start with printing the representation of vector spaces in mathlib: -/ -- import algebra.module #print vector_space -- abbreviation vector_space (R : Type u) (M : Type v) [field R] [add_comm_group M] := semimodule R M /- So vector spaces are represented by putting a `vector_space` typeclass on a type `R` and `M`, where `R` has a field structure and `M` an additive commutative group. And the comment helpfully explains that a `vector_space` is just a `module`, except the scalar ring is a field. -/ #print module -- abbreviation module (R : Type u) (M : Type v) [ring R] [add_comm_group M] := semimodule R M /- When we print the definition of `module`, we see it is a special case of a `semimodule``. Finally, we print the definition of `semimodule`: -/ #print semimodule -- class semimodule (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] -- extends distrib_mul_action R M := ... /- In other words: let `R` be a semiring and `M` have `0` and a commutative operator `+`, then a semimodule structure over `R` on `M` has a scalar multiplication `•` (`has_scalar.smul`), which satisfies the following identities: -/ #check add_smul -- ∀ (r s : R) (x : M), (r + s) • x = r • x + s • x #check smul_add -- ∀ (r : R) (x y : M), r • (x + y) = r • x + r • y #check mul_smul -- ∀ (r s : R) (x : M), (r * s) • x = r • (s • x) #check one_smul -- ∀ (x : M), 1 • x = x #check zero_smul -- ∀ (x : M), 0 • x = 0 #check smul_zero -- ∀ (r : R), r • 0 = 0 /- These equations define semimodules, modules and vector spaces. To save typing, and because semimodules were introduced later, mathlib tends to use the word "module" to refer to all three as long as this is not ambiguous. -/ /- The last two identities follow automatically from the previous if `M` has a negation operator, turning it into an additive group, so the function `semimodule.of_core` does the proofs for you: -/ #check semimodule.of_core section module /- Typical examples of semimodules, modules and vector spaces: -/ -- import algebra.pi_instances variables {n : Type} [fintype n] example : semimodule ℕ (n → ℕ) := infer_instance -- Or as mathematicians commonly know it: `ℕ^n`. example : module ℤ (n → ℤ) := infer_instance example : vector_space ℚ (n → ℚ) := infer_instance example : semimodule ℚ (n → ℚ) := infer_instance -- A vector space is automatically a semimodule. /- If you want a specifically `k`-dimensional module, use `fin k` as the `fintype`. -/ example {k : ℕ} : semimodule ℤ (fin k → ℤ) := infer_instance variables {R M N : Type} [ring R] [add_comm_group M] [add_comm_group N] [module R M] [module R N] example : module R R := infer_instance example : module ℤ R := infer_instance example : module R (M × N) := infer_instance example : module R (M × N) := infer_instance example {R' : Type} [comm_ring R'] (f : R →+* R') : module R R' := ring_hom.to_semimodule f /- To explicitly construct elements of `fin k → R`, use the following notation: -/ -- import data.matrix.notation example : fin 4 → ℤ := ![1, 2, -4, 3] end module section linear_map variables {R M : Type} [comm_ring R] [add_comm_group M] [module R M] /- Maps between semimodules that respect `+` and `•` are called `linear_map`, and an `R`-linear map from `M` to `N` has notation `M →ₗ[R] N`: -/ #print linear_map /- They are bundled, meaning we define them by giving the map and the proofs simultaneously: -/ def twice : M →ₗ[R] M := { to_fun := λ x, (2 : R) • x, map_add' := λ x y, smul_add 2 x y, map_smul' := λ s x, smul_comm 2 s x } /- Linear maps can be applied as if they were functions: -/ #check twice (![37, 42] : fin 2 → ℚ) /- Some basic operations on linear maps: -/ -- import linear_algebra.basic #check linear_map.comp -- composition #check linear_map.has_zero -- 0 #check linear_map.has_add -- (+) #check linear_map.has_scalar -- (•) /- A linear equivalence is an invertible linear map. These are the correct notion of "isomorphism of modules". -/ #print linear_equiv /- The identity function is defined twice: once as linear map and once as linear equivalence. -/ #check linear_map.id #check linear_equiv.refl end linear_map section submodule variables {R M : Type} [comm_ring R] [add_comm_group M] [module R M] /- The submodules of a semimodule `M` are subsets of `M` (i.e. elements of `set M`) that are closed under the module operations `0`, `+` and `•`. Just like `vector_space` is defined to be a special case of `semimodule`, `subspace` is defined to be a special case of `submodule`. -/ #print submodule #print subspace /- Note that the `ideal`s of a ring `R` are defined to be exactly the `R`-submodules of `R`. This should save us a lot of re-definition work. -/ #print ideal /- You can directly define a submodule by giving its carrier subset and proving that the carrier is closed under each operation: -/ def zero_submodule : submodule R M := { carrier := {0}, zero_mem' := by simp, add_mem' := by { intros x y hx hy, simp at hx hy, simp [hx, hy] }, smul_mem' := by { intros r x hx, simp at hx, simp [hx] } } /- There are many library functions for defining submodules: -/ variables (S T : submodule R M) #check (twice.range : submodule R M) -- the image of `twice` in `M` #check (twice.ker : submodule R M) -- the kernel of `twice` in `M` #check submodule.span ℤ {(2 : ℤ)} -- also known as 2ℤ #check S.map twice -- also known as {twice x | x ∈ S} #check S.comap twice -- also known as {x | twice x ∈ S} /- For submodule inclusion, we write `≤`: -/ #check S ≤ T #check S < T /- The zero submodule is written `⊥` and the whole module as a submodule is written `⊤`: -/ example {x : M} : x ∈ (⊥ : submodule R M) ↔ x = 0 := submodule.mem_bot R example {x : M} : x ∈ (⊤ : submodule R M) := submodule.mem_top /- Intersection and sum of submodules are usually written with the lattice operators `⊓` and `⊔`: -/ #check submodule.mem_inf -- x ∈ S ⊓ T ↔ x ∈ S ∧ x ∈ T #check submodule.mem_sup -- x ∈ S ⊔ T ↔ ∃ (y ∈ S) (z ∈ T), x = y + z /- The embedding of a submodule in the ambient space, is called `subtype`: -/ #check submodule.subtype /- Finally, we can take the quotient modulo a submodule: -/ #check (submodule.span ℤ {(2 : ℤ)}).quotient -- also known as ℤ / 2ℤ end submodule section forms variables {n R : Type} [comm_ring R] [fintype n] /- In addition to linear maps, there are bilinear forms, quadratic forms and sesquilinear forms. -/ -- import linear_algebra.bilinear_form -- import linear_algebra.quadratic_form #check bilin_form /- Defining a bilinear form works similarly to defining a linear map: -/ def dot_product : bilin_form R (n → R) := { bilin := λ x y, matrix.dot_product x y, bilin_add_left := matrix.add_dot_product, bilin_smul_left := matrix.smul_dot_product, bilin_add_right := matrix.dot_product_add, bilin_smul_right := matrix.dot_product_smul } /- Some other constructions on forms: -/ #check bilin_form.to_quadratic_form #check quadratic_form.associated #check quadratic_form.has_scalar #check quadratic_form.proj end forms section matrix variables {m n R : Type} [fintype m] [fintype n] [comm_ring R] /- Matrices in mathlib are basically no more than a rectangular block of entries. Under the hood, they are specified by a function taking a row and column, and returning the entry at that index. They are useful when you want to compute an invariant such as the determinant, as these are typically noncomputable for linear maps. A type of matrices `matrix m n α` requires that the types `m` and `n` of the indices are `fintype`s, and there is no restriction on the type `α` of the entries. -/ #print matrix /- Like vectors in `n → R`, matrices are typically indexed over `fin k`. To define a matrix, you map the indexes to the entry: -/ def example_matrix : matrix (fin 2) (fin 3) ℤ := λ i j, i + j #eval example_matrix 1 2 /- Like vectors, we can use `![...]` notation to define matrices: -/ def other_example_matrix : matrix (fin 3) (fin 2) ℤ := ![![0, 1], ![1, 2], ![2, 3]] /- We have the 0 matrix and the sum of two matrices: -/ example (i j) : (0 : matrix m n R) i j = 0 := matrix.zero_val i j example (A B : matrix m n R) (i j) : (A + B) i j = A i j + B i j := matrix.add_val A B i j /- Matrices have multiplication and transpose operators `matrix.mul` and `matrix.transpose`. The following line allows `⬝` and `ᵀ` to stand for these two respectively: -/ open_locale matrix #check example_matrix ⬝ other_example_matrix #check example_matrixᵀ /- On square matrices, we have a semiring structure with `(*) = (⬝)` and `1` as the identity matrix. -/ #check matrix.semiring /- When working with matrices, a "vector" is always of the form `n → R` where `n` is a `fintype`. The operations between matrices and vectors are defined. -/ #check matrix.col -- turn a vector into a column matrix #check matrix.row -- turn a vector into a row matrix #check matrix.vec_mul_vec -- column vector times row vector /- You have to explicitly specify whether vectors are multiplied on the left or on the right: -/ #check example_matrix.mul_vec -- (fin 3 → ℤ) → (fin 2 → ℤ), right multiplication #check example_matrix.vec_mul -- (fin 2 → ℤ) → (fin 3 → ℤ), left multiplication /- You can convert a matrix to a linear map, which acts by right multiplication of vectors. -/ variables {M N : Type} [add_comm_group M] [add_comm_group N] [module R M] [module R N] #check matrix.eval -- matrix m n R → ((m → R) →ₗ[R] (n → R)) /- Going between linear maps and matrices is an isomorphism, as long as you have chosen a basis for each module. -/ variables [decidable_eq m] [decidable_eq n] variables {v : m → M} (v_is_basis : is_basis R v) {w : n → N} (w_is_basis : is_basis R w) #check linear_equiv_matrix v_is_basis w_is_basis -- (M →ₗ[R] N) ≈ₗ[R] matrix m n R /- Invertible (i.e. nonsingular) matrices have an inverse operation `nonsing_inv`. The notation `⁻¹` has been reserved for the more general pseudoinverse, which unfortunately has not yet been defined. -/ #check matrix.nonsing_inv end matrix section odds_and_ends /- Other useful parts of the library: -/ -- import analysis.normed_space.real_inner_product #print normed_space -- semimodule with a norm #print inner_product_space -- normed space with an inner product in ℝ #print finite_dimensional.findim -- the dimension of a space, as a natural number (infinity -> 0) #print vector_space.dim -- the dimension of a space, as a cardinal end odds_and_ends
98a415db29b553fe429fd4b61c267e772ae4538a
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/playground/forIn2.lean
d5601bc12eb240830e05f5bc8c91c7841ca95709
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,089
lean
def tst1 : IO Nat := [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 14].forIn 0 fun a b => if a % 2 == 0 then do IO.println (">> " ++ toString a ++ " " ++ toString b) (if b > 20 then pure $ ForInStep.done b else pure $ ForInStep.yield (a+b)) else pure $ ForInStep.yield b #eval tst1 structure Range := (lower upper : Nat) def Range.forIn {β m} [Monad m] (range : Range) (init : β) (f : Nat → β → m (ForInStep β)) : m β := let base := range.lower + range.upper - 2 let rec @[specialize] loop | 0, b => pure b | i+1, b => let j := base - i if j >= range.upper then pure b else do let s ← f j b (match s with | ForInStep.done b => pure b | ForInStep.yield b => loop i b) loop (range.upper - 1) init @[inline] def range (a : Nat) (b : Option Nat := none) : Range := match b with | none => ⟨0, a⟩ | some b => ⟨a, b⟩ instance : OfNat (Option Nat) := ⟨fun n => some n⟩ def tst3 : IO Nat := (range 5 10).forIn 0 fun i s => do IO.println (">> " ++ toString i) pure $ ForInStep.yield (s+i) #eval tst3
d77800394745f0848afa2a0a76a0f343b64e6f86
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/ring_theory/polynomial/basic.lean
3fa3907efdc107e45e5314cb6042f83bcf222336
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
41,736
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.char_p.basic import data.mv_polynomial.comm_ring import data.mv_polynomial.equiv import data.polynomial.field_division import ring_theory.principal_ideal_domain import ring_theory.polynomial.content /-! # Ring-theoretic supplement of data.polynomial. ## Main results * `mv_polynomial.integral_domain`: If a ring is an integral domain, then so is its polynomial ring over finitely many variables. * `polynomial.is_noetherian_ring`: Hilbert basis theorem, that if a ring is noetherian then so is its polynomial ring. * `polynomial.wf_dvd_monoid`: If an integral domain is a `wf_dvd_monoid`, then so is its polynomial ring. * `polynomial.unique_factorization_monoid`: If an integral domain is a `unique_factorization_monoid`, then so is its polynomial ring. -/ noncomputable theory open_locale classical big_operators universes u v w namespace polynomial instance {R : Type u} [semiring R] (p : ℕ) [h : char_p R p] : char_p (polynomial R) p := let ⟨h⟩ := h in ⟨λ n, by rw [← C.map_nat_cast, ← C_0, C_inj, h]⟩ variables (R : Type u) [comm_ring R] /-- The `R`-submodule of `R[X]` consisting of polynomials of degree ≤ `n`. -/ def degree_le (n : with_bot ℕ) : submodule R (polynomial R) := ⨅ k : ℕ, ⨅ h : ↑k > n, (lcoeff R k).ker /-- The `R`-submodule of `R[X]` consisting of polynomials of degree < `n`. -/ def degree_lt (n : ℕ) : submodule R (polynomial R) := ⨅ k : ℕ, ⨅ h : k ≥ n, (lcoeff R k).ker variable {R} theorem mem_degree_le {n : with_bot ℕ} {f : polynomial R} : f ∈ degree_le R n ↔ degree f ≤ n := by simp only [degree_le, submodule.mem_infi, degree_le_iff_coeff_zero, linear_map.mem_ker]; refl @[mono] theorem degree_le_mono {m n : with_bot ℕ} (H : m ≤ n) : degree_le R m ≤ degree_le R n := λ f hf, mem_degree_le.2 (le_trans (mem_degree_le.1 hf) H) theorem degree_le_eq_span_X_pow {n : ℕ} : degree_le R n = submodule.span R ↑((finset.range (n+1)).image (λ n, (X : polynomial R)^n)) := begin apply le_antisymm, { intros p hp, replace hp := mem_degree_le.1 hp, rw [← polynomial.sum_monomial_eq p, polynomial.sum], refine submodule.sum_mem _ (λ k hk, _), show monomial _ _ ∈ _, have := with_bot.coe_le_coe.1 (finset.sup_le_iff.1 hp k hk), rw [monomial_eq_C_mul_X, C_mul'], refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $ finset.mem_image.2 ⟨_, finset.mem_range.2 (nat.lt_succ_of_le this), rfl⟩) }, rw [submodule.span_le, finset.coe_image, set.image_subset_iff], intros k hk, apply mem_degree_le.2, exact (degree_X_pow_le _).trans (with_bot.coe_le_coe.2 $ nat.le_of_lt_succ $ finset.mem_range.1 hk) end theorem mem_degree_lt {n : ℕ} {f : polynomial R} : f ∈ degree_lt R n ↔ degree f < n := by { simp_rw [degree_lt, submodule.mem_infi, linear_map.mem_ker, degree, finset.sup_lt_iff (with_bot.bot_lt_coe n), mem_support_iff, with_bot.some_eq_coe, with_bot.coe_lt_coe, lt_iff_not_ge', ne, not_imp_not], refl } @[mono] theorem degree_lt_mono {m n : ℕ} (H : m ≤ n) : degree_lt R m ≤ degree_lt R n := λ f hf, mem_degree_lt.2 (lt_of_lt_of_le (mem_degree_lt.1 hf) $ with_bot.coe_le_coe.2 H) theorem degree_lt_eq_span_X_pow {n : ℕ} : degree_lt R n = submodule.span R ↑((finset.range n).image (λ n, X^n) : finset (polynomial R)) := begin apply le_antisymm, { intros p hp, replace hp := mem_degree_lt.1 hp, rw [← polynomial.sum_monomial_eq p, polynomial.sum], refine submodule.sum_mem _ (λ k hk, _), show monomial _ _ ∈ _, have := with_bot.coe_lt_coe.1 ((finset.sup_lt_iff $ with_bot.bot_lt_coe n).1 hp k hk), rw [monomial_eq_C_mul_X, C_mul'], refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $ finset.mem_image.2 ⟨_, finset.mem_range.2 this, rfl⟩) }, rw [submodule.span_le, finset.coe_image, set.image_subset_iff], intros k hk, apply mem_degree_lt.2, exact lt_of_le_of_lt (degree_X_pow_le _) (with_bot.coe_lt_coe.2 $ finset.mem_range.1 hk) end /-- The first `n` coefficients on `degree_lt n` form a linear equivalence with `fin n → F`. -/ def degree_lt_equiv (F : Type*) [field F] (n : ℕ) : degree_lt F n ≃ₗ[F] (fin n → F) := { to_fun := λ p n, (↑p : polynomial F).coeff n, inv_fun := λ f, ⟨∑ i : fin n, monomial i (f i), (degree_lt F n).sum_mem (λ i _, mem_degree_lt.mpr (lt_of_le_of_lt (degree_monomial_le i (f i)) (with_bot.coe_lt_coe.mpr i.is_lt)))⟩, map_add' := λ p q, by { ext, rw [submodule.coe_add, coeff_add], refl }, map_smul' := λ x p, by { ext, rw [submodule.coe_smul, coeff_smul], refl }, left_inv := begin rintro ⟨p, hp⟩, ext1, simp only [submodule.coe_mk], by_cases hp0 : p = 0, { subst hp0, simp only [coeff_zero, linear_map.map_zero, finset.sum_const_zero] }, rw [mem_degree_lt, degree_eq_nat_degree hp0, with_bot.coe_lt_coe] at hp, conv_rhs { rw [p.as_sum_range' n hp, ← fin.sum_univ_eq_sum_range] }, end, right_inv := begin intro f, ext i, simp only [finset_sum_coeff, submodule.coe_mk], rw [finset.sum_eq_single i, coeff_monomial, if_pos rfl], { rintro j - hji, rw [coeff_monomial, if_neg], rwa [← subtype.ext_iff] }, { intro h, exact (h (finset.mem_univ _)).elim } end } /-- The finset of nonzero coefficients of a polynomial. -/ def frange (p : polynomial R) : finset R := finset.image (λ n, p.coeff n) p.support lemma frange_zero : frange (0 : polynomial R) = ∅ := rfl lemma mem_frange_iff {p : polynomial R} {c : R} : c ∈ p.frange ↔ ∃ n ∈ p.support, c = p.coeff n := by simp [frange, eq_comm] lemma frange_one : frange (1 : polynomial R) ⊆ {1} := begin simp [frange, finset.image_subset_iff], simp only [← C_1, coeff_C], assume n hn, simp only [exists_prop, ite_eq_right_iff, not_forall] at hn, simp [hn], end lemma coeff_mem_frange (p : polynomial R) (n : ℕ) (h : p.coeff n ≠ 0) : p.coeff n ∈ p.frange := begin simp only [frange, exists_prop, mem_support_iff, finset.mem_image, ne.def], exact ⟨n, h, rfl⟩, end /-- Given a polynomial, return the polynomial whose coefficients are in the ring closure of the original coefficients. -/ def restriction (p : polynomial R) : polynomial (subring.closure (↑p.frange : set R)) := ∑ i in p.support, monomial i (⟨p.coeff i, if H : p.coeff i = 0 then H.symm ▸ (subring.closure _).zero_mem else subring.subset_closure (p.coeff_mem_frange _ H)⟩ : (subring.closure (↑p.frange : set R))) @[simp] theorem coeff_restriction {p : polynomial R} {n : ℕ} : ↑(coeff (restriction p) n) = coeff p n := begin simp only [restriction, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq', ne.def, ite_not], split_ifs, { rw h, refl }, { refl } end @[simp] theorem coeff_restriction' {p : polynomial R} {n : ℕ} : (coeff (restriction p) n).1 = coeff p n := coeff_restriction @[simp] lemma support_restriction (p : polynomial R) : support (restriction p) = support p := begin ext i, simp only [mem_support_iff, not_iff_not, ne.def], conv_rhs { rw [← coeff_restriction] }, exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end @[simp] theorem map_restriction (p : polynomial R) : p.restriction.map (algebra_map _ _) = p := ext $ λ n, by rw [coeff_map, algebra.algebra_map_of_subring_apply, coeff_restriction] @[simp] theorem degree_restriction {p : polynomial R} : (restriction p).degree = p.degree := by simp [degree] @[simp] theorem nat_degree_restriction {p : polynomial R} : (restriction p).nat_degree = p.nat_degree := by simp [nat_degree] @[simp] theorem monic_restriction {p : polynomial R} : monic (restriction p) ↔ monic p := begin simp only [monic, leading_coeff, nat_degree_restriction], rw [←@coeff_restriction _ _ p], exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end @[simp] theorem restriction_zero : restriction (0 : polynomial R) = 0 := by simp only [restriction, finset.sum_empty, support_zero] @[simp] theorem restriction_one : restriction (1 : polynomial R) = 1 := ext $ λ i, subtype.eq $ by rw [coeff_restriction', coeff_one, coeff_one]; split_ifs; refl variables {S : Type v} [ring S] {f : R →+* S} {x : S} theorem eval₂_restriction {p : polynomial R} : eval₂ f x p = eval₂ (f.comp (subring.subtype _)) x p.restriction := begin simp only [eval₂_eq_sum, sum, support_restriction, ←@coeff_restriction _ _ p], refl, end section to_subring variables (p : polynomial R) (T : subring R) /-- Given a polynomial `p` and a subring `T` that contains the coefficients of `p`, return the corresponding polynomial whose coefficients are in `T. -/ def to_subring (hp : (↑p.frange : set R) ⊆ T) : polynomial T := ∑ i in p.support, monomial i (⟨p.coeff i, if H : p.coeff i = 0 then H.symm ▸ T.zero_mem else hp (p.coeff_mem_frange _ H)⟩ : T) variables (hp : (↑p.frange : set R) ⊆ T) include hp @[simp] theorem coeff_to_subring {n : ℕ} : ↑(coeff (to_subring p T hp) n) = coeff p n := begin simp only [to_subring, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq', ne.def, ite_not], split_ifs, { rw h, refl }, { refl } end @[simp] theorem coeff_to_subring' {n : ℕ} : (coeff (to_subring p T hp) n).1 = coeff p n := coeff_to_subring _ _ hp @[simp] lemma support_to_subring : support (to_subring p T hp) = support p := begin ext i, simp only [mem_support_iff, not_iff_not, ne.def], conv_rhs { rw [← coeff_to_subring p T hp] }, exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end @[simp] theorem degree_to_subring : (to_subring p T hp).degree = p.degree := by simp [degree] @[simp] theorem nat_degree_to_subring : (to_subring p T hp).nat_degree = p.nat_degree := by simp [nat_degree] @[simp] theorem monic_to_subring : monic (to_subring p T hp) ↔ monic p := begin simp_rw [monic, leading_coeff, nat_degree_to_subring, ← coeff_to_subring p T hp], exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end omit hp @[simp] theorem to_subring_zero : to_subring (0 : polynomial R) T (by simp [frange_zero]) = 0 := by { ext i, simp } @[simp] theorem to_subring_one : to_subring (1 : polynomial R) T (set.subset.trans frange_one $finset.singleton_subset_set_iff.2 T.one_mem) = 1 := ext $ λ i, subtype.eq $ by rw [coeff_to_subring', coeff_one, coeff_one]; split_ifs; refl @[simp] theorem map_to_subring : (p.to_subring T hp).map (subring.subtype T) = p := by { ext n, simp [coeff_map] } end to_subring variables (T : subring R) /-- Given a polynomial whose coefficients are in some subring, return the corresponding polynomial whose coefficients are in the ambient ring. -/ def of_subring (p : polynomial T) : polynomial R := ∑ i in p.support, monomial i (p.coeff i : R) lemma coeff_of_subring (p : polynomial T) (n : ℕ) : coeff (of_subring T p) n = (coeff p n : T) := begin simp only [of_subring, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq', ite_eq_right_iff, ne.def, ite_not, not_not, ite_eq_left_iff], assume h, rw h, refl end @[simp] theorem frange_of_subring {p : polynomial T} : (↑(p.of_subring T).frange : set R) ⊆ T := begin assume i hi, simp only [frange, set.mem_image, mem_support_iff, ne.def, finset.mem_coe, finset.coe_image] at hi, rcases hi with ⟨n, hn, h'n⟩, rw [← h'n, coeff_of_subring], exact subtype.mem (coeff p n : T) end end polynomial variables {R : Type u} {σ : Type v} {M : Type w} [comm_ring R] [add_comm_group M] [module R M] namespace ideal open polynomial /-- If every coefficient of a polynomial is in an ideal `I`, then so is the polynomial itself -/ lemma polynomial_mem_ideal_of_coeff_mem_ideal (I : ideal (polynomial R)) (p : polynomial R) (hp : ∀ (n : ℕ), (p.coeff n) ∈ I.comap C) : p ∈ I := sum_C_mul_X_eq p ▸ submodule.sum_mem I (λ n hn, I.mul_mem_right _ (hp n)) /-- The push-forward of an ideal `I` of `R` to `polynomial R` via inclusion is exactly the set of polynomials whose coefficients are in `I` -/ theorem mem_map_C_iff {I : ideal R} {f : polynomial R} : f ∈ (ideal.map C I : ideal (polynomial R)) ↔ ∀ n : ℕ, f.coeff n ∈ I := begin split, { intros hf, apply submodule.span_induction hf, { intros f hf n, cases (set.mem_image _ _ _).mp hf with x hx, rw [← hx.right, coeff_C], by_cases (n = 0), { simpa [h] using hx.left }, { simp [h] } }, { simp }, { exact λ f g hf hg n, by simp [I.add_mem (hf n) (hg n)] }, { refine λ f g hg n, _, rw [smul_eq_mul, coeff_mul], exact I.sum_mem (λ c hc, I.smul_mem (f.coeff c.fst) (hg c.snd)) } }, { intros hf, rw ← sum_monomial_eq f, refine (I.map C : ideal (polynomial R)).sum_mem (λ n hn, _), simp [monomial_eq_C_mul_X], rw mul_comm, exact (I.map C : ideal (polynomial R)).mul_mem_left _ (mem_map_of_mem _ (hf n)) } end lemma quotient_map_C_eq_zero {I : ideal R} : ∀ a ∈ I, ((quotient.mk (map C I : ideal (polynomial R))).comp C) a = 0 := begin intros a ha, rw [ring_hom.comp_apply, quotient.eq_zero_iff_mem], exact mem_map_of_mem _ ha, end lemma eval₂_C_mk_eq_zero {I : ideal R} : ∀ f ∈ (map C I : ideal (polynomial R)), eval₂_ring_hom (C.comp (quotient.mk I)) X f = 0 := begin intros a ha, rw ← sum_monomial_eq a, dsimp, rw eval₂_sum, refine finset.sum_eq_zero (λ n hn, _), dsimp, rw eval₂_monomial (C.comp (quotient.mk I)) X, refine mul_eq_zero_of_left (polynomial.ext (λ m, _)) (X ^ n), erw coeff_C, by_cases h : m = 0, { simpa [h] using quotient.eq_zero_iff_mem.2 ((mem_map_C_iff.1 ha) n) }, { simp [h] } end /-- If `I` is an ideal of `R`, then the ring polynomials over the quotient ring `I.quotient` is isomorphic to the quotient of `polynomial R` by the ideal `map C I`, where `map C I` contains exactly the polynomials whose coefficients all lie in `I` -/ def polynomial_quotient_equiv_quotient_polynomial (I : ideal R) : polynomial (I.quotient) ≃+* (map C I : ideal (polynomial R)).quotient := { to_fun := eval₂_ring_hom (quotient.lift I ((quotient.mk (map C I : ideal (polynomial R))).comp C) quotient_map_C_eq_zero) ((quotient.mk (map C I : ideal (polynomial R)) X)), inv_fun := quotient.lift (map C I : ideal (polynomial R)) (eval₂_ring_hom (C.comp (quotient.mk I)) X) eval₂_C_mk_eq_zero, map_mul' := λ f g, by simp, map_add' := λ f g, by simp, left_inv := begin intro f, apply polynomial.induction_on' f, { simp_intros p q hp hq, rw [hp, hq] }, { rintros n ⟨x⟩, simp [monomial_eq_smul_X, C_mul'] } end, right_inv := begin rintro ⟨f⟩, apply polynomial.induction_on' f, { simp_intros p q hp hq, rw [hp, hq] }, { intros n a, simp [monomial_eq_smul_X, ← C_mul' a (X ^ n)] }, end, } /-- If `P` is a prime ideal of `R`, then `R[x]/(P)` is an integral domain. -/ lemma is_integral_domain_map_C_quotient {P : ideal R} (H : is_prime P) : is_integral_domain (quotient (map C P : ideal (polynomial R))) := ring_equiv.is_integral_domain (polynomial (quotient P)) (integral_domain.to_is_integral_domain (polynomial (quotient P))) (polynomial_quotient_equiv_quotient_polynomial P).symm /-- If `P` is a prime ideal of `R`, then `P.R[x]` is a prime ideal of `R[x]`. -/ lemma is_prime_map_C_of_is_prime {P : ideal R} (H : is_prime P) : is_prime (map C P : ideal (polynomial R)) := (quotient.is_integral_domain_iff_prime (map C P : ideal (polynomial R))).mp (is_integral_domain_map_C_quotient H) /-- Given any ring `R` and an ideal `I` of `polynomial R`, we get a map `R → R[x] → R[x]/I`. If we let `R` be the image of `R` in `R[x]/I` then we also have a map `R[x] → R'[x]`. In particular we can map `I` across this map, to get `I'` and a new map `R' → R'[x] → R'[x]/I`. This theorem shows `I'` will not contain any non-zero constant polynomials -/ lemma eq_zero_of_polynomial_mem_map_range (I : ideal (polynomial R)) (x : ((quotient.mk I).comp C).range) (hx : C x ∈ (I.map (polynomial.map_ring_hom ((quotient.mk I).comp C).range_restrict))) : x = 0 := begin let i := ((quotient.mk I).comp C).range_restrict, have hi' : (polynomial.map_ring_hom i).ker ≤ I, { refine λ f hf, polynomial_mem_ideal_of_coeff_mem_ideal I f (λ n, _), rw [mem_comap, ← quotient.eq_zero_iff_mem, ← ring_hom.comp_apply], rw [ring_hom.mem_ker, coe_map_ring_hom] at hf, replace hf := congr_arg (λ (f : polynomial _), f.coeff n) hf, simp only [coeff_map, coeff_zero] at hf, rwa [subtype.ext_iff, ring_hom.coe_range_restrict] at hf }, obtain ⟨x, hx'⟩ := x, obtain ⟨y, rfl⟩ := (ring_hom.mem_range).1 hx', refine subtype.eq _, simp only [ring_hom.comp_apply, quotient.eq_zero_iff_mem, subring.coe_zero, subtype.val_eq_coe], suffices : C (i y) ∈ (I.map (polynomial.map_ring_hom i)), { obtain ⟨f, hf⟩ := mem_image_of_mem_map_of_surjective (polynomial.map_ring_hom i) (polynomial.map_surjective _ (((quotient.mk I).comp C).range_restrict_surjective)) this, refine sub_add_cancel (C y) f ▸ I.add_mem (hi' _ : (C y - f) ∈ I) hf.1, rw [ring_hom.mem_ker, ring_hom.map_sub, hf.2, sub_eq_zero, coe_map_ring_hom, map_C] }, exact hx, end /-- `polynomial R` is never a field for any ring `R`. -/ lemma polynomial_not_is_field : ¬ is_field (polynomial R) := begin by_contradiction hR, by_cases hR' : ∃ (x y : R), x ≠ y, { haveI : nontrivial R := let ⟨x, y, hxy⟩ := hR' in nontrivial_of_ne x y hxy, obtain ⟨p, hp⟩ := hR.mul_inv_cancel X_ne_zero, by_cases hp0 : p = 0, { replace hp := congr_arg degree hp, rw [hp0, mul_zero, degree_zero, degree_one] at hp, contradiction }, { have : p.degree < (X * p).degree := (mul_comm p X) ▸ degree_lt_degree_mul_X hp0, rw [congr_arg degree hp, degree_one, nat.with_bot.lt_zero_iff, degree_eq_bot] at this, exact hp0 this } }, { push_neg at hR', exact let ⟨x, y, hxy⟩ := hR.exists_pair_ne in hxy (polynomial.ext (λ n, hR' _ _)) } end /-- The only constant in a maximal ideal over a field is `0`. -/ lemma eq_zero_of_constant_mem_of_maximal (hR : is_field R) (I : ideal (polynomial R)) [hI : I.is_maximal] (x : R) (hx : C x ∈ I) : x = 0 := begin refine classical.by_contradiction (λ hx0, hI.ne_top ((eq_top_iff_one I).2 _)), obtain ⟨y, hy⟩ := hR.mul_inv_cancel hx0, convert I.smul_mem (C y) hx, rw [smul_eq_mul, ← C.map_mul, mul_comm y x, hy, ring_hom.map_one], end /-- Transport an ideal of `R[X]` to an `R`-submodule of `R[X]`. -/ def of_polynomial (I : ideal (polynomial R)) : submodule R (polynomial R) := { carrier := I.carrier, zero_mem' := I.zero_mem, add_mem' := λ _ _, I.add_mem, smul_mem' := λ c x H, by { rw [← C_mul'], exact I.mul_mem_left _ H } } variables {I : ideal (polynomial R)} theorem mem_of_polynomial (x) : x ∈ I.of_polynomial ↔ x ∈ I := iff.rfl variables (I) /-- Given an ideal `I` of `R[X]`, make the `R`-submodule of `I` consisting of polynomials of degree ≤ `n`. -/ def degree_le (n : with_bot ℕ) : submodule R (polynomial R) := degree_le R n ⊓ I.of_polynomial /-- Given an ideal `I` of `R[X]`, make the ideal in `R` of leading coefficients of polynomials in `I` with degree ≤ `n`. -/ def leading_coeff_nth (n : ℕ) : ideal R := (I.degree_le n).map $ lcoeff R n theorem mem_leading_coeff_nth (n : ℕ) (x) : x ∈ I.leading_coeff_nth n ↔ ∃ p ∈ I, degree p ≤ n ∧ leading_coeff p = x := begin simp only [leading_coeff_nth, degree_le, submodule.mem_map, lcoeff_apply, submodule.mem_inf, mem_degree_le], split, { rintro ⟨p, ⟨hpdeg, hpI⟩, rfl⟩, cases lt_or_eq_of_le hpdeg with hpdeg hpdeg, { refine ⟨0, I.zero_mem, bot_le, _⟩, rw [leading_coeff_zero, eq_comm], exact coeff_eq_zero_of_degree_lt hpdeg }, { refine ⟨p, hpI, le_of_eq hpdeg, _⟩, rw [leading_coeff, nat_degree, hpdeg], refl } }, { rintro ⟨p, hpI, hpdeg, rfl⟩, have : nat_degree p + (n - nat_degree p) = n, { exact nat.add_sub_cancel' (nat_degree_le_of_degree_le hpdeg) }, refine ⟨p * X ^ (n - nat_degree p), ⟨_, I.mul_mem_right _ hpI⟩, _⟩, { apply le_trans (degree_mul_le _ _) _, apply le_trans (add_le_add (degree_le_nat_degree) (degree_X_pow_le _)) _, rw [← with_bot.coe_add, this], exact le_refl _ }, { rw [leading_coeff, ← coeff_mul_X_pow p (n - nat_degree p), this] } } end theorem mem_leading_coeff_nth_zero (x) : x ∈ I.leading_coeff_nth 0 ↔ C x ∈ I := (mem_leading_coeff_nth _ _ _).trans ⟨λ ⟨p, hpI, hpdeg, hpx⟩, by rwa [← hpx, leading_coeff, nat.eq_zero_of_le_zero (nat_degree_le_of_degree_le hpdeg), ← eq_C_of_degree_le_zero hpdeg], λ hx, ⟨C x, hx, degree_C_le, leading_coeff_C x⟩⟩ theorem leading_coeff_nth_mono {m n : ℕ} (H : m ≤ n) : I.leading_coeff_nth m ≤ I.leading_coeff_nth n := begin intros r hr, simp only [set_like.mem_coe, mem_leading_coeff_nth] at hr ⊢, rcases hr with ⟨p, hpI, hpdeg, rfl⟩, refine ⟨p * X ^ (n - m), I.mul_mem_right _ hpI, _, leading_coeff_mul_X_pow⟩, refine le_trans (degree_mul_le _ _) _, refine le_trans (add_le_add hpdeg (degree_X_pow_le _)) _, rw [← with_bot.coe_add, nat.add_sub_cancel' H], exact le_refl _ end /-- Given an ideal `I` in `R[X]`, make the ideal in `R` of the leading coefficients in `I`. -/ def leading_coeff : ideal R := ⨆ n : ℕ, I.leading_coeff_nth n theorem mem_leading_coeff (x) : x ∈ I.leading_coeff ↔ ∃ p ∈ I, polynomial.leading_coeff p = x := begin rw [leading_coeff, submodule.mem_supr_of_directed], simp only [mem_leading_coeff_nth], { split, { rintro ⟨i, p, hpI, hpdeg, rfl⟩, exact ⟨p, hpI, rfl⟩ }, rintro ⟨p, hpI, rfl⟩, exact ⟨nat_degree p, p, hpI, degree_le_nat_degree, rfl⟩ }, intros i j, exact ⟨i + j, I.leading_coeff_nth_mono (nat.le_add_right _ _), I.leading_coeff_nth_mono (nat.le_add_left _ _)⟩ end theorem is_fg_degree_le [is_noetherian_ring R] (n : ℕ) : submodule.fg (I.degree_le n) := is_noetherian_submodule_left.1 (is_noetherian_of_fg_of_noetherian _ ⟨_, degree_le_eq_span_X_pow.symm⟩) _ end ideal namespace polynomial @[priority 100] instance {R : Type*} [integral_domain R] [wf_dvd_monoid R] : wf_dvd_monoid (polynomial R) := { well_founded_dvd_not_unit := begin classical, refine rel_hom.well_founded ⟨λ p, (if p = 0 then ⊤ else ↑p.degree, p.leading_coeff), _⟩ (prod.lex_wf (with_top.well_founded_lt $ with_bot.well_founded_lt nat.lt_wf) _inst_5.well_founded_dvd_not_unit), rintros a b ⟨ane0, ⟨c, ⟨not_unit_c, rfl⟩⟩⟩, rw [polynomial.degree_mul, if_neg ane0], split_ifs with hac, { rw [hac, polynomial.leading_coeff_zero], apply prod.lex.left, exact lt_of_le_of_ne le_top with_top.coe_ne_top }, have cne0 : c ≠ 0 := right_ne_zero_of_mul hac, simp only [cne0, ane0, polynomial.leading_coeff_mul], by_cases hdeg : c.degree = 0, { simp only [hdeg, add_zero], refine prod.lex.right _ ⟨_, ⟨c.leading_coeff, (λ unit_c, not_unit_c _), rfl⟩⟩, { rwa [ne, polynomial.leading_coeff_eq_zero] }, rw [polynomial.is_unit_iff, polynomial.eq_C_of_degree_eq_zero hdeg], use [c.leading_coeff, unit_c], rw [polynomial.leading_coeff, polynomial.nat_degree_eq_of_degree_eq_some hdeg] }, { apply prod.lex.left, rw polynomial.degree_eq_nat_degree cne0 at *, rw [with_top.coe_lt_coe, polynomial.degree_eq_nat_degree ane0, ← with_bot.coe_add, with_bot.coe_lt_coe], exact lt_add_of_pos_right _ (nat.pos_of_ne_zero (λ h, hdeg (h.symm ▸ with_bot.coe_zero))) }, end } end polynomial /-- Hilbert basis theorem: a polynomial ring over a noetherian ring is a noetherian ring. -/ protected theorem polynomial.is_noetherian_ring [is_noetherian_ring R] : is_noetherian_ring (polynomial R) := is_noetherian_ring_iff.2 ⟨assume I : ideal (polynomial R), let M := well_founded.min (is_noetherian_iff_well_founded.1 (by apply_instance)) (set.range I.leading_coeff_nth) ⟨_, ⟨0, rfl⟩⟩ in have hm : M ∈ set.range I.leading_coeff_nth := well_founded.min_mem _ _ _, let ⟨N, HN⟩ := hm, ⟨s, hs⟩ := I.is_fg_degree_le N in have hm2 : ∀ k, I.leading_coeff_nth k ≤ M := λ k, or.cases_on (le_or_lt k N) (λ h, HN ▸ I.leading_coeff_nth_mono h) (λ h x hx, classical.by_contradiction $ λ hxm, have ¬M < I.leading_coeff_nth k, by refine well_founded.not_lt_min (well_founded_submodule_gt _ _) _ _ _; exact ⟨k, rfl⟩, this ⟨HN ▸ I.leading_coeff_nth_mono (le_of_lt h), λ H, hxm (H hx)⟩), have hs2 : ∀ {x}, x ∈ I.degree_le N → x ∈ ideal.span (↑s : set (polynomial R)), from hs ▸ λ x hx, submodule.span_induction hx (λ _ hx, ideal.subset_span hx) (ideal.zero_mem _) (λ _ _, ideal.add_mem _) (λ c f hf, f.C_mul' c ▸ ideal.mul_mem_left _ _ hf), ⟨s, le_antisymm (ideal.span_le.2 $ λ x hx, have x ∈ I.degree_le N, from hs ▸ submodule.subset_span hx, this.2) $ begin have : submodule.span (polynomial R) ↑s = ideal.span ↑s, by refl, rw this, intros p hp, generalize hn : p.nat_degree = k, induction k using nat.strong_induction_on with k ih generalizing p, cases le_or_lt k N, { subst k, refine hs2 ⟨polynomial.mem_degree_le.2 (le_trans polynomial.degree_le_nat_degree $ with_bot.coe_le_coe.2 h), hp⟩ }, { have hp0 : p ≠ 0, { rintro rfl, cases hn, exact nat.not_lt_zero _ h }, have : (0 : R) ≠ 1, { intro h, apply hp0, ext i, refine (mul_one _).symm.trans _, rw [← h, mul_zero], refl }, haveI : nontrivial R := ⟨⟨0, 1, this⟩⟩, have : p.leading_coeff ∈ I.leading_coeff_nth N, { rw HN, exact hm2 k ((I.mem_leading_coeff_nth _ _).2 ⟨_, hp, hn ▸ polynomial.degree_le_nat_degree, rfl⟩) }, rw I.mem_leading_coeff_nth at this, rcases this with ⟨q, hq, hdq, hlqp⟩, have hq0 : q ≠ 0, { intro H, rw [← polynomial.leading_coeff_eq_zero] at H, rw [hlqp, polynomial.leading_coeff_eq_zero] at H, exact hp0 H }, have h1 : p.degree = (q * polynomial.X ^ (k - q.nat_degree)).degree, { rw [polynomial.degree_mul', polynomial.degree_X_pow], rw [polynomial.degree_eq_nat_degree hp0, polynomial.degree_eq_nat_degree hq0], rw [← with_bot.coe_add, nat.add_sub_cancel', hn], { refine le_trans (polynomial.nat_degree_le_of_degree_le hdq) (le_of_lt h) }, rw [polynomial.leading_coeff_X_pow, mul_one], exact mt polynomial.leading_coeff_eq_zero.1 hq0 }, have h2 : p.leading_coeff = (q * polynomial.X ^ (k - q.nat_degree)).leading_coeff, { rw [← hlqp, polynomial.leading_coeff_mul_X_pow] }, have := polynomial.degree_sub_lt h1 hp0 h2, rw [polynomial.degree_eq_nat_degree hp0] at this, rw ← sub_add_cancel p (q * polynomial.X ^ (k - q.nat_degree)), refine (ideal.span ↑s).add_mem _ ((ideal.span ↑s).mul_mem_right _ _), { by_cases hpq : p - q * polynomial.X ^ (k - q.nat_degree) = 0, { rw hpq, exact ideal.zero_mem _ }, refine ih _ _ (I.sub_mem hp (I.mul_mem_right _ hq)) rfl, rwa [polynomial.degree_eq_nat_degree hpq, with_bot.coe_lt_coe, hn] at this }, exact hs2 ⟨polynomial.mem_degree_le.2 hdq, hq⟩ } end⟩⟩ attribute [instance] polynomial.is_noetherian_ring namespace polynomial theorem exists_irreducible_of_degree_pos {R : Type u} [integral_domain R] [wf_dvd_monoid R] {f : polynomial R} (hf : 0 < f.degree) : ∃ g, irreducible g ∧ g ∣ f := wf_dvd_monoid.exists_irreducible_factor (λ huf, ne_of_gt hf $ degree_eq_zero_of_is_unit huf) (λ hf0, not_lt_of_lt hf $ hf0.symm ▸ (@degree_zero R _).symm ▸ with_bot.bot_lt_coe _) theorem exists_irreducible_of_nat_degree_pos {R : Type u} [integral_domain R] [wf_dvd_monoid R] {f : polynomial R} (hf : 0 < f.nat_degree) : ∃ g, irreducible g ∧ g ∣ f := exists_irreducible_of_degree_pos $ by { contrapose! hf, exact nat_degree_le_of_degree_le hf } theorem exists_irreducible_of_nat_degree_ne_zero {R : Type u} [integral_domain R] [wf_dvd_monoid R] {f : polynomial R} (hf : f.nat_degree ≠ 0) : ∃ g, irreducible g ∧ g ∣ f := exists_irreducible_of_nat_degree_pos $ nat.pos_of_ne_zero hf lemma linear_independent_powers_iff_aeval (f : M →ₗ[R] M) (v : M) : linear_independent R (λ n : ℕ, (f ^ n) v) ↔ ∀ (p : polynomial R), aeval f p v = 0 → p = 0 := begin rw linear_independent_iff, simp only [finsupp.total_apply, aeval_endomorphism, forall_iff_forall_finsupp, sum, support, coeff, ← zero_to_finsupp], exact iff.rfl, end lemma disjoint_ker_aeval_of_coprime (f : M →ₗ[R] M) {p q : polynomial R} (hpq : is_coprime p q) : disjoint (aeval f p).ker (aeval f q).ker := begin intros v hv, rcases hpq with ⟨p', q', hpq'⟩, simpa [linear_map.mem_ker.1 (submodule.mem_inf.1 hv).1, linear_map.mem_ker.1 (submodule.mem_inf.1 hv).2] using congr_arg (λ p : polynomial R, aeval f p v) hpq'.symm, end lemma sup_aeval_range_eq_top_of_coprime (f : M →ₗ[R] M) {p q : polynomial R} (hpq : is_coprime p q) : (aeval f p).range ⊔ (aeval f q).range = ⊤ := begin rw eq_top_iff, intros v hv, rw submodule.mem_sup, rcases hpq with ⟨p', q', hpq'⟩, use aeval f (p * p') v, use linear_map.mem_range.2 ⟨aeval f p' v, by simp only [linear_map.mul_apply, aeval_mul]⟩, use aeval f (q * q') v, use linear_map.mem_range.2 ⟨aeval f q' v, by simp only [linear_map.mul_apply, aeval_mul]⟩, simpa only [mul_comm p p', mul_comm q q', aeval_one, aeval_add] using congr_arg (λ p : polynomial R, aeval f p v) hpq' end lemma sup_ker_aeval_le_ker_aeval_mul {f : M →ₗ[R] M} {p q : polynomial R} : (aeval f p).ker ⊔ (aeval f q).ker ≤ (aeval f (p * q)).ker := begin intros v hv, rcases submodule.mem_sup.1 hv with ⟨x, hx, y, hy, hxy⟩, have h_eval_x : aeval f (p * q) x = 0, { rw [mul_comm, aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hx, linear_map.map_zero] }, have h_eval_y : aeval f (p * q) y = 0, { rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hy, linear_map.map_zero] }, rw [linear_map.mem_ker, ←hxy, linear_map.map_add, h_eval_x, h_eval_y, add_zero], end lemma sup_ker_aeval_eq_ker_aeval_mul_of_coprime (f : M →ₗ[R] M) {p q : polynomial R} (hpq : is_coprime p q) : (aeval f p).ker ⊔ (aeval f q).ker = (aeval f (p * q)).ker := begin apply le_antisymm sup_ker_aeval_le_ker_aeval_mul, intros v hv, rw submodule.mem_sup, rcases hpq with ⟨p', q', hpq'⟩, have h_eval₂_qpp' := calc aeval f (q * (p * p')) v = aeval f (p' * (p * q)) v : by rw [mul_comm, mul_assoc, mul_comm, mul_assoc, mul_comm q p] ... = 0 : by rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hv, linear_map.map_zero], have h_eval₂_pqq' := calc aeval f (p * (q * q')) v = aeval f (q' * (p * q)) v : by rw [←mul_assoc, mul_comm] ... = 0 : by rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hv, linear_map.map_zero], rw aeval_mul at h_eval₂_qpp' h_eval₂_pqq', refine ⟨aeval f (q * q') v, linear_map.mem_ker.1 h_eval₂_pqq', aeval f (p * p') v, linear_map.mem_ker.1 h_eval₂_qpp', _⟩, rw [add_comm, mul_comm p p', mul_comm q q'], simpa using congr_arg (λ p : polynomial R, aeval f p v) hpq' end end polynomial namespace mv_polynomial lemma is_noetherian_ring_fin_0 [is_noetherian_ring R] : is_noetherian_ring (mv_polynomial (fin 0) R) := is_noetherian_ring_of_ring_equiv R ((mv_polynomial.is_empty_ring_equiv R pempty).symm.trans (rename_equiv R fin_zero_equiv'.symm).to_ring_equiv) theorem is_noetherian_ring_fin [is_noetherian_ring R] : ∀ {n : ℕ}, is_noetherian_ring (mv_polynomial (fin n) R) | 0 := is_noetherian_ring_fin_0 | (n+1) := @is_noetherian_ring_of_ring_equiv (polynomial (mv_polynomial (fin n) R)) _ _ _ (mv_polynomial.fin_succ_equiv _ n).to_ring_equiv.symm (@polynomial.is_noetherian_ring (mv_polynomial (fin n) R) _ (is_noetherian_ring_fin)) /-- The multivariate polynomial ring in finitely many variables over a noetherian ring is itself a noetherian ring. -/ instance is_noetherian_ring [fintype σ] [is_noetherian_ring R] : is_noetherian_ring (mv_polynomial σ R) := @is_noetherian_ring_of_ring_equiv (mv_polynomial (fin (fintype.card σ)) R) _ _ _ (rename_equiv R (fintype.equiv_fin σ).symm).to_ring_equiv is_noetherian_ring_fin lemma is_integral_domain_fin_zero (R : Type u) [comm_ring R] (hR : is_integral_domain R) : is_integral_domain (mv_polynomial (fin 0) R) := ring_equiv.is_integral_domain R hR ((rename_equiv R fin_zero_equiv').to_ring_equiv.trans (mv_polynomial.is_empty_ring_equiv R pempty)) /-- Auxiliary lemma: Multivariate polynomials over an integral domain with variables indexed by `fin n` form an integral domain. This fact is proven inductively, and then used to prove the general case without any finiteness hypotheses. See `mv_polynomial.integral_domain` for the general case. -/ lemma is_integral_domain_fin (R : Type u) [comm_ring R] (hR : is_integral_domain R) : ∀ (n : ℕ), is_integral_domain (mv_polynomial (fin n) R) | 0 := is_integral_domain_fin_zero R hR | (n+1) := ring_equiv.is_integral_domain (polynomial (mv_polynomial (fin n) R)) (is_integral_domain_fin n).polynomial (mv_polynomial.fin_succ_equiv _ n).to_ring_equiv lemma is_integral_domain_fintype (R : Type u) (σ : Type v) [comm_ring R] [fintype σ] (hR : is_integral_domain R) : is_integral_domain (mv_polynomial σ R) := @ring_equiv.is_integral_domain _ (mv_polynomial (fin $ fintype.card σ) R) _ _ (mv_polynomial.is_integral_domain_fin _ hR _) (rename_equiv R (fintype.equiv_fin σ)).to_ring_equiv /-- Auxiliary definition: Multivariate polynomials in finitely many variables over an integral domain form an integral domain. This fact is proven by transport of structure from the `mv_polynomial.integral_domain_fin`, and then used to prove the general case without finiteness hypotheses. See `mv_polynomial.integral_domain` for the general case. -/ def integral_domain_fintype (R : Type u) (σ : Type v) [integral_domain R] [fintype σ] : integral_domain (mv_polynomial σ R) := @is_integral_domain.to_integral_domain _ _ $ mv_polynomial.is_integral_domain_fintype R σ $ integral_domain.to_is_integral_domain R protected theorem eq_zero_or_eq_zero_of_mul_eq_zero {R : Type u} [integral_domain R] {σ : Type v} (p q : mv_polynomial σ R) (h : p * q = 0) : p = 0 ∨ q = 0 := begin obtain ⟨s, p, rfl⟩ := exists_finset_rename p, obtain ⟨t, q, rfl⟩ := exists_finset_rename q, have : rename (subtype.map id (finset.subset_union_left s t) : {x // x ∈ s} → {x // x ∈ s ∪ t}) p * rename (subtype.map id (finset.subset_union_right s t) : {x // x ∈ t} → {x // x ∈ s ∪ t}) q = 0, { apply rename_injective _ subtype.val_injective, simpa using h }, letI := mv_polynomial.integral_domain_fintype R {x // x ∈ (s ∪ t)}, rw mul_eq_zero at this, cases this; [left, right], all_goals { simpa using congr_arg (rename subtype.val) this } end /-- The multivariate polynomial ring over an integral domain is an integral domain. -/ instance {R : Type u} {σ : Type v} [integral_domain R] : integral_domain (mv_polynomial σ R) := { eq_zero_or_eq_zero_of_mul_eq_zero := mv_polynomial.eq_zero_or_eq_zero_of_mul_eq_zero, exists_pair_ne := ⟨0, 1, λ H, begin have : eval₂ (ring_hom.id _) (λ s, (0:R)) (0 : mv_polynomial σ R) = eval₂ (ring_hom.id _) (λ s, (0:R)) (1 : mv_polynomial σ R), { congr, exact H }, simpa, end⟩, .. (by apply_instance : comm_ring (mv_polynomial σ R)) } lemma map_mv_polynomial_eq_eval₂ {S : Type*} [comm_ring S] [fintype σ] (ϕ : mv_polynomial σ R →+* S) (p : mv_polynomial σ R) : ϕ p = mv_polynomial.eval₂ (ϕ.comp mv_polynomial.C) (λ s, ϕ (mv_polynomial.X s)) p := begin refine trans (congr_arg ϕ (mv_polynomial.as_sum p)) _, rw [mv_polynomial.eval₂_eq', ϕ.map_sum], congr, ext, simp only [monomial_eq, ϕ.map_pow, ϕ.map_prod, ϕ.comp_apply, ϕ.map_mul, finsupp.prod_pow], end lemma quotient_map_C_eq_zero {I : ideal R} {i : R} (hi : i ∈ I) : (ideal.quotient.mk (ideal.map C I : ideal (mv_polynomial σ R))).comp C i = 0 := begin simp only [function.comp_app, ring_hom.coe_comp, ideal.quotient.eq_zero_iff_mem], exact ideal.mem_map_of_mem _ hi end /-- If every coefficient of a polynomial is in an ideal `I`, then so is the polynomial itself, multivariate version. -/ lemma mem_ideal_of_coeff_mem_ideal (I : ideal (mv_polynomial σ R)) (p : mv_polynomial σ R) (hcoe : ∀ (m : σ →₀ ℕ), p.coeff m ∈ I.comap C) : p ∈ I := begin rw as_sum p, suffices : ∀ m ∈ p.support, monomial m (mv_polynomial.coeff m p) ∈ I, { exact submodule.sum_mem I this }, intros m hm, rw [← mul_one (coeff m p), ← C_mul_monomial], suffices : C (coeff m p) ∈ I, { exact I.mul_mem_right (monomial m 1) this }, simpa [ideal.mem_comap] using hcoe m end /-- The push-forward of an ideal `I` of `R` to `mv_polynomial σ R` via inclusion is exactly the set of polynomials whose coefficients are in `I` -/ theorem mem_map_C_iff {I : ideal R} {f : mv_polynomial σ R} : f ∈ (ideal.map C I : ideal (mv_polynomial σ R)) ↔ ∀ (m : σ →₀ ℕ), f.coeff m ∈ I := begin split, { intros hf, apply submodule.span_induction hf, { intros f hf n, cases (set.mem_image _ _ _).mp hf with x hx, rw [← hx.right, coeff_C], by_cases (n = 0), { simpa [h] using hx.left }, { simp [ne.symm h] } }, { simp }, { exact λ f g hf hg n, by simp [I.add_mem (hf n) (hg n)] }, { refine λ f g hg n, _, rw [smul_eq_mul, coeff_mul], exact I.sum_mem (λ c hc, I.smul_mem (f.coeff c.fst) (hg c.snd)) } }, { intros hf, rw as_sum f, suffices : ∀ m ∈ f.support, monomial m (coeff m f) ∈ (ideal.map C I : ideal (mv_polynomial σ R)), { exact submodule.sum_mem _ this }, intros m hm, rw [← mul_one (coeff m f), ← C_mul_monomial], suffices : C (coeff m f) ∈ (ideal.map C I : ideal (mv_polynomial σ R)), { exact ideal.mul_mem_right _ _ this }, apply ideal.mem_map_of_mem _, exact hf m } end lemma eval₂_C_mk_eq_zero {I : ideal R} {a : mv_polynomial σ R} (ha : a ∈ (ideal.map C I : ideal (mv_polynomial σ R))) : eval₂_hom (C.comp (ideal.quotient.mk I)) X a = 0 := begin rw as_sum a, rw [coe_eval₂_hom, eval₂_sum], refine finset.sum_eq_zero (λ n hn, _), simp only [eval₂_monomial, function.comp_app, ring_hom.coe_comp], refine mul_eq_zero_of_left _ _, suffices : coeff n a ∈ I, { rw [← @ideal.mk_ker R _ I, ring_hom.mem_ker] at this, simp only [this, C_0] }, exact mem_map_C_iff.1 ha n end /-- If `I` is an ideal of `R`, then the ring `mv_polynomial σ I.quotient` is isomorphic as an `R`-algebra to the quotient of `mv_polynomial σ R` by the ideal generated by `I`. -/ def quotient_equiv_quotient_mv_polynomial (I : ideal R) : mv_polynomial σ I.quotient ≃ₐ[R] (ideal.map C I : ideal (mv_polynomial σ R)).quotient := { to_fun := eval₂_hom (ideal.quotient.lift I ((ideal.quotient.mk (ideal.map C I : ideal (mv_polynomial σ R))).comp C) (λ i hi, quotient_map_C_eq_zero hi)) (λ i, ideal.quotient.mk (ideal.map C I : ideal (mv_polynomial σ R)) (X i)), inv_fun := ideal.quotient.lift (ideal.map C I : ideal (mv_polynomial σ R)) (eval₂_hom (C.comp (ideal.quotient.mk I)) X) (λ a ha, eval₂_C_mk_eq_zero ha), map_mul' := ring_hom.map_mul _, map_add' := ring_hom.map_add _, left_inv := begin intro f, apply induction_on f, { rintro ⟨r⟩, rw [coe_eval₂_hom, eval₂_C], simp only [eval₂_hom_eq_bind₂, submodule.quotient.quot_mk_eq_mk, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk, bind₂_C_right, ring_hom.coe_comp] }, { simp_intros p q hp hq only [ring_hom.map_add, mv_polynomial.coe_eval₂_hom, coe_eval₂_hom, mv_polynomial.eval₂_add, mv_polynomial.eval₂_hom_eq_bind₂, eval₂_hom_eq_bind₂], rw [hp, hq] }, { simp_intros p i hp only [eval₂_hom_eq_bind₂, coe_eval₂_hom], simp only [hp, eval₂_hom_eq_bind₂, coe_eval₂_hom, ideal.quotient.lift_mk, bind₂_X_right, eval₂_mul, ring_hom.map_mul, eval₂_X] } end, right_inv := begin rintro ⟨f⟩, apply induction_on f, { intros r, simp only [submodule.quotient.quot_mk_eq_mk, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk, ring_hom.coe_comp, eval₂_hom_C] }, { simp_intros p q hp hq only [eval₂_hom_eq_bind₂, submodule.quotient.quot_mk_eq_mk, eval₂_add, ring_hom.map_add, coe_eval₂_hom, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk], rw [hp, hq] }, { simp_intros p i hp only [eval₂_hom_eq_bind₂, submodule.quotient.quot_mk_eq_mk, coe_eval₂_hom, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk, bind₂_X_right, eval₂_mul, ring_hom.map_mul, eval₂_X], simp only [hp] } end, commutes' := λ r, eval₂_hom_C _ _ (ideal.quotient.mk I r) } end mv_polynomial namespace polynomial open unique_factorization_monoid variables {D : Type u} [integral_domain D] [unique_factorization_monoid D] @[priority 100] instance unique_factorization_monoid : unique_factorization_monoid (polynomial D) := begin haveI := arbitrary (normalization_monoid D), haveI := to_gcd_monoid D, exact ufm_of_gcd_of_wf_dvd_monoid end end polynomial
f96ebec29e0fa8760665ebe5ff6b6cd8e4469bcb
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Util/Profile.lean
52640e45e5192eb02b991b14535989730514e8e6
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
1,055
lean
/- Copyright (c) 2019 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sebastian Ullrich -/ import Lean.Data.Position namespace Lean /-- Print and accumulate run time of `act` when option `profiler` is set to `true`. -/ @[extern "lean_profileit"] def profileit {α : Type} (category : @& String) (opts : @& Options) (fn : Unit → α) : α := fn () unsafe def profileitIOUnsafe {ε α : Type} (category : String) (opts : Options) (act : EIO ε α) : EIO ε α := match profileit category opts fun _ => unsafeEIO act with | Except.ok a => pure a | Except.error e => throw e @[implementedBy profileitIOUnsafe] def profileitIO {ε α : Type} (category : String) (opts : Options) (act : EIO ε α) : EIO ε α := act -- impossible to infer `ε` def profileitM {m : Type → Type} (ε : Type) [MonadFunctorT (EIO ε) m] {α : Type} (category : String) (opts : Options) (act : m α) : m α := monadMap (fun {β} => profileitIO (ε := ε) (α := β) category opts) act end Lean
645225c5245743112421c6f92af6b5390391ccf2
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/fun_like/equiv.lean
e6c2dc8d72dbff518674148b2d57d10c40af99ee
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
8,173
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import data.fun_like.embedding /-! # Typeclass for a type `F` with an injective map to `A ≃ B` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > https://github.com/leanprover-community/mathlib4/pull/541 > Any changes to this file require a corresponding PR to mathlib4. This typeclass is primarily for use by isomorphisms like `monoid_equiv` and `linear_equiv`. ## Basic usage of `equiv_like` A typical type of morphisms should be declared as: ``` structure my_iso (A B : Type*) [my_class A] [my_class B] extends equiv A B := (map_op' : ∀ {x y : A}, to_fun (my_class.op x y) = my_class.op (to_fun x) (to_fun y)) namespace my_iso variables (A B : Type*) [my_class A] [my_class B] -- This instance is optional if you follow the "Isomorphism class" design below: instance : equiv_like (my_iso A B) A (λ _, B) := { coe := my_iso.to_equiv.to_fun, inv := my_iso.to_equiv.inv_fun, left_inv := my_iso.to_equiv.left_inv, right_inv := my_iso.to_equiv.right_inv, coe_injective' := λ f g h, by cases f; cases g; congr' } /-- Helper instance for when there's too many metavariables to apply `equiv_like.coe` directly. -/ instance : has_coe_to_fun (my_iso A B) := to_fun.to_coe_fn @[simp] lemma to_fun_eq_coe {f : my_iso A B} : f.to_fun = (f : A → B) := rfl @[ext] theorem ext {f g : my_iso A B} (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h /-- Copy of a `my_iso` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : my_iso A B) (f' : A → B) (f_inv : B → A) (h : f' = ⇑f) : my_iso A B := { to_fun := f', inv_fun := f_inv, left_inv := h.symm ▸ f.left_inv, right_inv := h.symm ▸ f.right_inv, map_op' := h.symm ▸ f.map_op' } end my_iso ``` This file will then provide a `has_coe_to_fun` instance and various extensionality and simp lemmas. ## Isomorphism classes extending `equiv_like` The `equiv_like` design provides further benefits if you put in a bit more work. The first step is to extend `equiv_like` to create a class of those types satisfying the axioms of your new type of isomorphisms. Continuing the example above: ``` section set_option old_structure_cmd true /-- `my_iso_class F A B` states that `F` is a type of `my_class.op`-preserving morphisms. You should extend this class when you extend `my_iso`. -/ class my_iso_class (F : Type*) (A B : out_param $ Type*) [my_class A] [my_class B] extends equiv_like F A (λ _, B), my_hom_class F A B. end -- You can replace `my_iso.equiv_like` with the below instance: instance : my_iso_class (my_iso A B) A B := { coe := my_iso.to_fun, inv := my_iso.inv_fun, left_inv := my_iso.left_inv, right_inv := my_iso.right_inv, coe_injective' := λ f g h, by cases f; cases g; congr', map_op := my_iso.map_op' } -- [Insert `has_coe_to_fun`, `to_fun_eq_coe`, `ext` and `copy` here] ``` The second step is to add instances of your new `my_iso_class` for all types extending `my_iso`. Typically, you can just declare a new class analogous to `my_iso_class`: ``` structure cooler_iso (A B : Type*) [cool_class A] [cool_class B] extends my_iso A B := (map_cool' : to_fun cool_class.cool = cool_class.cool) section set_option old_structure_cmd true class cooler_iso_class (F : Type*) (A B : out_param $ Type*) [cool_class A] [cool_class B] extends my_iso_class F A B := (map_cool : ∀ (f : F), f cool_class.cool = cool_class.cool) end @[simp] lemma map_cool {F A B : Type*} [cool_class A] [cool_class B] [cooler_iso_class F A B] (f : F) : f cool_class.cool = cool_class.cool := my_iso_class.map_op -- You can also replace `my_iso.equiv_like` with the below instance: instance : cool_iso_class (cool_iso A B) A B := { coe := cool_iso.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_op := cool_iso.map_op', map_cool := cool_iso.map_cool' } -- [Insert `has_coe_to_fun`, `to_fun_eq_coe`, `ext` and `copy` here] ``` Then any declaration taking a specific type of morphisms as parameter can instead take the class you just defined: ``` -- Compare with: lemma do_something (f : my_iso A B) : sorry := sorry lemma do_something {F : Type*} [my_iso_class F A B] (f : F) : sorry := sorry ``` This means anything set up for `my_iso`s will automatically work for `cool_iso_class`es, and defining `cool_iso_class` only takes a constant amount of effort, instead of linearly increasing the work per `my_iso`-related declaration. -/ /-- The class `equiv_like E α β` expresses that terms of type `E` have an injective coercion to bijections between `α` and `β`. This typeclass is used in the definition of the homomorphism typeclasses, such as `zero_equiv_class`, `mul_equiv_class`, `monoid_equiv_class`, .... -/ class equiv_like (E : Sort*) (α β : out_param Sort*) := (coe : E → α → β) (inv : E → β → α) (left_inv : ∀ e, function.left_inverse (inv e) (coe e)) (right_inv : ∀ e, function.right_inverse (inv e) (coe e)) -- The `inv` hypothesis makes this easier to prove with `congr'` (coe_injective' : ∀ e g, coe e = coe g → inv e = inv g → e = g) namespace equiv_like variables {E F α β γ : Sort*} [iE : equiv_like E α β] [iF : equiv_like F β γ] include iE lemma inv_injective : function.injective (equiv_like.inv : E → (β → α)) := λ e g h, coe_injective' e g ((right_inv e).eq_right_inverse (h.symm ▸ left_inv g)) h @[priority 100] instance to_embedding_like : embedding_like E α β := { coe := (coe : E → α → β), coe_injective' := λ e g h, coe_injective' e g h ((left_inv e).eq_right_inverse (h.symm ▸ right_inv g)), injective' := λ e, (left_inv e).injective } protected lemma injective (e : E) : function.injective e := embedding_like.injective e protected lemma surjective (e : E) : function.surjective e := (right_inv e).surjective protected lemma bijective (e : E) : function.bijective (e : α → β) := ⟨equiv_like.injective e, equiv_like.surjective e⟩ theorem apply_eq_iff_eq (f : E) {x y : α} : f x = f y ↔ x = y := embedding_like.apply_eq_iff_eq f @[simp] lemma injective_comp (e : E) (f : β → γ) : function.injective (f ∘ e) ↔ function.injective f := function.injective.of_comp_iff' f (equiv_like.bijective e) @[simp] lemma surjective_comp (e : E) (f : β → γ) : function.surjective (f ∘ e) ↔ function.surjective f := (equiv_like.surjective e).of_comp_iff f @[simp] lemma bijective_comp (e : E) (f : β → γ) : function.bijective (f ∘ e) ↔ function.bijective f := (equiv_like.bijective e).of_comp_iff f /-- This lemma is only supposed to be used in the generic context, when working with instances of classes extending `equiv_like`. For concrete isomorphism types such as `equiv`, you should use `equiv.symm_apply_apply` or its equivalent. TODO: define a generic form of `equiv.symm`. -/ @[simp] lemma inv_apply_apply (e : E) (a : α) : equiv_like.inv e (e a) = a := left_inv _ _ /-- This lemma is only supposed to be used in the generic context, when working with instances of classes extending `equiv_like`. For concrete isomorphism types such as `equiv`, you should use `equiv.apply_symm_apply` or its equivalent. TODO: define a generic form of `equiv.symm`. -/ @[simp] lemma apply_inv_apply (e : E) (b : β) : e (equiv_like.inv e b) = b := right_inv _ _ omit iE include iF lemma comp_injective (f : α → β) (e : F) : function.injective (e ∘ f) ↔ function.injective f := embedding_like.comp_injective f e @[simp] lemma comp_surjective (f : α → β) (e : F) : function.surjective (e ∘ f) ↔ function.surjective f := function.surjective.of_comp_iff' (equiv_like.bijective e) f @[simp] lemma comp_bijective (f : α → β) (e : F) : function.bijective (e ∘ f) ↔ function.bijective f := (equiv_like.bijective e).of_comp_iff' f /-- This is not an instance to avoid slowing down every single `subsingleton` typeclass search.-/ lemma subsingleton_dom [subsingleton β] : subsingleton F := ⟨λ f g, fun_like.ext f g $ λ x, (right_inv f).injective $ subsingleton.elim _ _⟩ end equiv_like
23e7e18c3c3ad47900c45eee4eff429d8855c12d
6214e13b31733dc9aeb4833db6a6466005763162
/src/substitution.lean
188699b78a9b0969060c85c2498bf32560cc95f0
[]
no_license
joshua0pang/esverify-theory
272a250445f3aeea49a7e72d1ab58c2da6618bbe
8565b123c87b0113f83553d7732cd6696c9b5807
refs/heads/master
1,585,873,849,081
1,527,304,393,000
1,527,304,393,000
154,901,199
1
0
null
1,540,593,067,000
1,540,593,067,000
null
UTF-8
Lean
false
false
237,213
lean
-- lemmas about substitution import .definitions3 .freevars .others lemma env.contains.inv {σ: env} {x y: var} {v: value}: x ∈ (σ[y↦v]) → (x = y ∨ x ∈ σ) := assume x_in: x ∈ (σ[y↦v]), show x = y ∨ x ∈ σ, by { cases x_in, left, refl, right, from a } lemma env.contains.same.inv {σ: env} {x y: var} {v: value}: x ∉ (σ[y↦v]) → ¬ (x = y ∨ x ∈ σ) := assume x_not_in: x ∉ (σ[y↦v]), assume : (x = y ∨ x ∈ σ), this.elim ( assume x_is_y: x = y, have x ∈ (σ[x↦v]), from env.contains.same, have x ∈ (σ[y↦v]), from @eq.subst var (λa, x ∈ (σ[a↦v])) x y x_is_y this, show «false», from x_not_in this ) ( assume : x ∈ σ, have x ∈ (σ[y↦v]), from env.contains.rest this, show «false», from x_not_in this ) lemma env.contains_apply_equiv {σ: env} {x: var}: ((σ x = none) ↔ (x ∉ σ)) ∧ ((∃v, σ x = some v) ↔ (x ∈ σ)) := begin induction σ with σ' y v' ih, show ((env.empty x = none) ↔ (x ∉ env.empty)) ∧ ((∃v, env.empty x = some v) ↔ (x ∈ env.empty)), by begin split, show (env.empty x = none) ↔ (x ∉ env.empty), by begin split, show (env.empty x = none) → (x ∉ env.empty), by begin assume : (env.empty x = none), by_contradiction h, cases h end, show (x ∉ env.empty) → (env.empty x = none), by begin assume : (x ∉ env.empty), have : (env.apply env.empty x = none), by unfold env.apply, show (env.empty x = none), from this end end, show (∃v, env.empty x = some v) ↔ (x ∈ env.empty), by begin split, show (∃v, env.empty x = some v) → (x ∈ env.empty), from ( assume : (∃v, env.empty x = some v), let (⟨v, h0⟩) := this in have h1: env.apply env.empty x = some v, from h0, have h2: env.apply env.empty x = none, by unfold env.apply, have some v = none, from eq.trans h1.symm h2, show (x ∈ env.empty), by contradiction ), show (x ∈ env.empty) → (∃v,env.empty x = some v), by begin assume h: x ∈ env.empty, cases h end end end, show (((σ'[y↦v']) x = none) ↔ (x ∉ (σ'[y↦v']))) ∧ ((∃v, (σ'[y↦v']) x = some v) ↔ (x ∈ (σ'[y↦v']))), by begin split, show (((σ'[y↦v']) x = none) ↔ (x ∉ (σ'[y↦v']))), by begin split, show (((σ'[y↦v']) x = none) → (x ∉ (σ'[y↦v']))), by begin assume h: ((σ'[y↦v']) x = none), have h2: (env.apply (σ'[y↦v']) x = (if y = x ∧ option.is_none (σ'.apply x) then v' else σ'.apply x)), by unfold env.apply, have h3: ((if y = x ∧ option.is_none (σ'.apply x) then ↑v' else σ'.apply x) = none), from eq.trans h2.symm h, have h4: (σ'.apply x = none), by begin by_cases (y = x ∧ option.is_none (σ'.apply x)), show (σ'.apply x = none), by begin have : ((if y = x ∧ option.is_none (σ'.apply x) then ↑v' else σ'.apply x) = ↑v'), by simp[h], have : (none = ↑v'), from eq.trans h3.symm this, contradiction end, show (σ'.apply x = none), by begin have : ((if y = x ∧ option.is_none (σ'.apply x) then ↑v' else σ'.apply x) = σ'.apply x), by simp[h], show (σ'.apply x = none), from eq.trans this.symm h3 end end, have : x ∉ σ', from ih.left.mp h4, have h5: ¬ (x = y), by begin by_contradiction, have h6: (option.is_none (σ'.apply x) = tt), from option.is_none.inv.mp h4, have : (y = x ∧ option.is_none (σ'.apply x)), from ⟨a.symm, h6⟩, have : ((if y = x ∧ option.is_none (σ'.apply x) then ↑v' else σ'.apply x) = ↑v'), by simp[this], have : (none = ↑v'), from eq.trans h3.symm this, contradiction end, by_contradiction a, cases a, case env.contains.same x_is_x { contradiction }, case env.contains.rest x_is_x { contradiction } end, show (x ∉ (σ'[y↦v'])) → (((σ'[y↦v']) x = none)), by begin assume : (x ∉ (σ'[y↦v'])), have h7: ¬ (x = y ∨ x ∈ σ'), from env.contains.same.inv this, have : x ≠ y, from (not_or_distrib.mp h7).left, have h8: y ≠ x, from ne.symm this, have h9: x ∉ σ', from (not_or_distrib.mp h7).right, have h10: (σ'.apply x = none), from ih.left.mpr h9, have h11: (env.apply (σ'[y↦v']) x = (if y = x ∧ option.is_none (σ'.apply x) then v' else σ'.apply x)), by unfold env.apply, have h12: ((if y = x ∧ option.is_none (σ'.apply x) then ↑v' else σ'.apply x) = σ'.apply x), by simp[h8], show ((σ'[y↦v']) x = none), from eq.trans (eq.trans h11 h12) h10 end end, show ((∃v, (σ'[y↦v']) x = some v) ↔ (x ∈ (σ'[y↦v']))), by begin split, show ((∃v, (σ'[y↦v']) x = some v) → (x ∈ (σ'[y↦v']))), from ( assume : (∃v, (σ'[y↦v']) x = some v), let ⟨v, h13⟩ := this in begin have h14: (env.apply (σ'[y↦v']) x = (if y = x ∧ option.is_none (σ'.apply x) then v' else σ'.apply x)), by unfold env.apply, by_cases (y = x ∧ option.is_none (σ'.apply x)) with h15, show (x ∈ (σ'[y↦v'])), by begin have x_is_y: (y = x), from h15.left, have : (x ∈ (σ'[x↦v'])), from env.contains.same, show x ∈ (σ'[y↦v']), from @eq.subst var (λa, x ∈ (σ'[a↦v'])) x y x_is_y.symm this end, show (x ∈ (σ'[y↦v'])), by begin have : ((if y = x ∧ option.is_none (σ'.apply x) then ↑v' else σ'.apply x) = σ'.apply x), by simp[h15], have : (σ'.apply x = v), from eq.trans (eq.trans this.symm h14.symm) h13, have : x ∈ σ', from ih.right.mp (exists.intro v this), show x ∈ (σ'[y↦v']), from env.contains.rest this end end), show (x ∈ (σ'[y↦v'])) → (∃v, (σ'[y↦v']) x = some v), by begin assume h16: (x ∈ (σ'[y↦v'])), have h17: (env.apply (σ'[y↦v']) x = (if y = x ∧ option.is_none (σ'.apply x) then v' else σ'.apply x)), by unfold env.apply, cases h16, case env.contains.same { by_cases (x = x ∧ option.is_none (σ'.apply x)), show (∃v, (σ'[x↦v']) x = some v), by begin have : ((if x = x ∧ option.is_none (σ'.apply x) then ↑v' else σ'.apply x) = v'), by { simp[h] }, show (∃v, (σ'[x↦v']) x = some v), from exists.intro v' (eq.trans h17 this) end, show (∃v, (σ'[x↦v']) x = some v), by begin have h19: ¬option.is_none (σ'.apply x), by begin by_contradiction h18, have : (x = x ∧ option.is_none (σ'.apply x)), from ⟨rfl, h18⟩, exact h this end, have : ((option.is_some (σ'.apply x)):Prop), from option.some_iff_not_none.mpr h19, have : ∃v, (σ'.apply x) = some v, from option.is_some_iff_exists.mp this, cases this with v h20, have : ((if x = x ∧ option.is_none (σ'.apply x) then ↑v' else σ'.apply x) = σ'.apply x), by { simp[h], simp[h19] }, show (∃v, (σ'[x↦v']) x = some v), from exists.intro v (eq.trans (eq.trans h17 this) h20) end }, case env.contains.rest h27 { have : (∃v, σ'.apply x = some v), from ih.right.mpr h27, cases this with v h28, have : ¬ (option.is_none (σ'.apply x)), from option.some_iff_not_none.mp (option.is_some_iff_exists.mpr (exists.intro v h28)), have : ((if y = x ∧ option.is_none (σ'.apply x) then ↑v' else σ'.apply x) = σ'.apply x), by simp[this], show (∃v, (σ'[y↦v']) x = some v), from exists.intro v (eq.trans (eq.trans h17 this) h28) } end end end end instance {σ: env} {x: var} : decidable (env.contains σ x) := let r := env.apply σ x in have h: r = env.apply σ x, from rfl, @option.rec_on value (λa, (r = a) → decidable (env.contains σ x)) r ( assume : r = none, have env.apply σ x = none, from eq.trans h this, have ¬ (x ∈ σ), from env.contains_apply_equiv.left.mp this, is_false this ) ( assume v: value, assume : r = some v, have env.apply σ x = some v, from eq.trans h this, have ∃v, env.apply σ x = some v, from exists.intro v this, have x ∈ σ, from env.contains_apply_equiv.right.mp this, is_true this ) rfl lemma term.subst.congr {x: var} {v: value} {t₁ t₂: term}: (t₁ = t₂) → (term.subst x v t₁ = term.subst x v t₂) := begin assume h1, congr, from h1 end lemma env.contains_without.inv {σ: env} {x y: var}: (x ∈ σ.without y) → (x ≠ y) ∧ x ∈ σ := begin assume h1, induction σ with σ' z v ih, unfold env.without at h1, cases h1, unfold env.without at h1, by_cases (z = y) with h2, simp[h2] at h1, have h3, from ih h1, split, from h3.left, apply env.contains.rest, from h3.right, simp[h2] at h1, have h3, from env.contains.inv h1, cases h3 with h4 h5, rw[h4], split, from h2, from env.contains.same, have h3, from ih h5, split, from h3.left, apply env.contains.rest, from h3.right end lemma env.contains_without.rinv {σ: env} {x y: var}: x ∈ σ ∧ (x ≠ y) → x ∈ σ.without y := begin assume h1, induction σ with σ' z v ih, cases h1.left, unfold env.without, by_cases (z = y) with h2, simp[h2], have h3, from env.contains.inv h1.left, cases h3 with h4 h5, have : (x = y), from eq.trans h4 h2, have : (y ≠ y), from @eq.subst var (λa, a ≠ y) x y this h1.right, contradiction, from ih ⟨h5, h1.right⟩, simp[h2], have h3, from env.contains.inv h1.left, cases h3 with h4 h5, rw[h4], apply env.contains.same, apply env.contains.rest, from ih ⟨h5, h1.right⟩, end lemma env.without_equiv {σ: env} {x y: var} {v: value}: (x ∉ σ) ∨ (σ x = v) → (x ∉ σ.without y ∨ (σ.without y x = v)) := begin assume h1, induction σ with σ' z v' ih, cases h1 with h2 h3, left, unfold env.without, assume h4, cases h4, cases h3, cases h1 with h2 h3, left, unfold env.without, by_cases (z = y) with h4, simp[h4], assume h5, have h6, from env.contains_without.inv h5, have : x ∈ (σ'[z↦v']), from env.contains.rest h6.right, contradiction, simp[h4], assume h5, have h6, from env.contains.inv h5, cases h6 with h7 h8, rw[h7] at h2, have : z ∈ (σ'[z↦v']), from env.contains.same, contradiction, have h9, from env.contains_without.inv h8, have : x ∈ (σ'[z↦v']), from env.contains.rest h9.right, contradiction, by_cases (x = y) with h4, left, unfold env.without, by_cases (z = y) with h5, simp[h5], rw[h4], assume h6, have h7, from env.contains_without.inv h6, have : ¬ (y = y), from h7.left, contradiction, simp[h5], assume h6, have h7, from env.contains.inv h6, cases h7 with h8 h9, have : (y = z), from eq.trans h4.symm h8, have : ¬ (z = z), from @eq.subst var (λa, ¬ (z = a)) y z this h5, contradiction, rw[h4] at h9, have h10, from env.contains_without.inv h9, have : ¬ (y = y), from h10.left, contradiction, right, have h5: (env.apply (σ'[z↦v']) x = some v), from h3, unfold env.apply at h5, by_cases (z = x ∧ (option.is_none (env.apply σ' x))) with h6, simp[h6] at h5, have : (some v' = some v), from h5, have h7: (v' = v), from option.some.inj this, have h8, from env.contains_apply_equiv.left.mp (option.is_none.inv.mpr h6.right), have h9, from ih (or.inl h8), let a' := ((env.without σ' y)[z↦v']), have h12: (env.without (σ'[z↦v']) y = (if z = y then (env.without σ' y) else a')), by unfold env.without, rw[h12], change ((ite (z = y) (env.without σ' y) a') x = ↑v), have : ¬ (z = y), from @eq.subst var (λa, ¬ (a = y)) x z h6.left.symm h4, have : (ite (z = y) (env.without σ' y) a' = ((env.without σ' y)[z↦v'])), by simp[this], have h13: ( ((ite (z = y) (env.without σ' y) a') x = ↑v) = (((env.without σ' y)[z↦v']) x = ↑v) ), by rw[this], rw[h13], change (env.apply (env.without σ' y[z↦v']) x = ↑v), unfold env.apply, cases h9 with h10 h11, have h14, from env.contains_apply_equiv.left.mpr h10, have h15, from option.is_none.inv.mp h14, have h16: (z = x ∧ (option.is_none (env.apply (env.without σ' y) x))), from and.intro h6.left h15, simp[h16], from some.inj.inv h7, have h14, from option.is_some_iff_exists.mpr (exists.intro v h11), have h15, from option.some_iff_not_none.mp h14, have h16: ¬ (z = x ∧ option.is_none (env.apply (env.without σ' y) x)), from not_and_distrib.mpr (or.inr h15), simp[h16], from h11, simp[h6] at h5, let a' := ((env.without σ' y)[z↦v']), have h7: (env.without (σ'[z↦v']) y = (if z = y then (env.without σ' y) else a')), by unfold env.without, rw[h7], by_cases (z = y) with h8, have : (ite (z = y) (env.without σ' y) ((env.without σ' y)[z↦v']) = (env.without σ' y)), by simp[h8], rw[this], have h8, from ih (or.inr h5), cases h8 with h9 h10, have : x ∈ σ', from env.contains_apply_equiv.right.mp (exists.intro v h5), have h9: x ∈ env.without σ' y, from env.contains_without.rinv ⟨this, h4⟩, contradiction, from h10, have : (ite (z = y) (env.without σ' y) ((env.without σ' y)[z↦v']) = ((env.without σ' y)[z↦v'])), by simp[h8], rw[this], change (env.apply ((env.without σ' y)[z↦v']) x = ↑v), unfold env.apply, have h10b: x ∈ σ', from env.contains_apply_equiv.right.mp (exists.intro v h5), have h11: x ∈ env.without σ' y, from env.contains_without.rinv ⟨h10b, h4⟩, have h12, from env.contains_apply_equiv.right.mpr h11, have h13, from option.is_some_iff_exists.mpr h12, have h14, from option.some_iff_not_none.mp h13, have h15: ¬ (z = x ∧ option.is_none (env.apply (env.without σ' y) x)), from not_and_distrib.mpr (or.inr h14), simp[h15], have h16, from ih (or.inr h5), cases h16 with h17 h18, contradiction, from h18 end lemma env.not_in_without {σ: env} {x y: var}: x ∉ σ → x ∉ σ.without y := begin assume h1, induction σ with σ' z v' ih, unfold env.without, assume h4, cases h4, unfold env.without, by_cases (z = y) with h4, simp[h4], assume h5, have h6, from env.contains_without.inv h5, have : x ∈ (σ'[z↦v']), from env.contains.rest h6.right, contradiction, simp[h4], assume h5, have h6, from env.contains.inv h5, cases h6 with h7 h8, rw[h7] at h1, have : z ∈ (σ'[z↦v']), from env.contains.same, contradiction, have h9, from env.contains_without.inv h8, have : x ∈ (σ'[z↦v']), from env.contains.rest h9.right, contradiction end lemma env.not_contains_without {σ: env} {x: var}: x ∉ σ.without x := assume : x ∈ σ.without x, have (x ≠ x) ∧ x ∈ σ, from env.contains_without.inv this, show «false», from this.left (eq.refl x) lemma env.without_equiv_with {σ: env} {x: var}: ∀y, y ∈ σ.without x → (σ.without x y = σ y) := assume y: var, assume h1: y ∈ σ.without x, have y ≠ x ∧ y ∈ σ, from env.contains_without.inv h1, have ∃v: value, σ y = v, from env.contains_apply_equiv.right.mpr this.right, let ⟨v, σ_y_is_v⟩ := this in have y ∉ σ.without x ∨ (σ.without x y = v), from env.without_equiv (or.inr σ_y_is_v), or.elim this ( assume : y ∉ σ.without x, show σ.without x y = σ y, from absurd h1 this ) ( assume : σ.without x y = v, show σ.without x y = σ y, from eq.trans this σ_y_is_v.symm ) lemma env.without_nonexisting {σ: env} {x: var}: x ∉ σ → (σ.without x = σ) := begin assume h1, induction σ with σ' z v' ih, unfold env.without, unfold env.without, have h2: (z ≠ x), by begin assume h3, rw[h3] at h1, have h4: x ∈ (σ'[x↦v']), from env.contains.same, contradiction end, simp[h2], congr, apply ih, by_contradiction h3, have h4: x ∈ (σ'[z↦v']), from env.contains.rest h3, contradiction end lemma unchanged_of_subst_nonfree_term {t: term} {x: var} {v: value}: x ∉ FV t → (term.subst x v t = t) := assume x_not_free: ¬ free_in_term x t, begin induction t with v' y unop t₁ t₁_ih binop t₂ t₃ t₂_ih t₃_ih t₄ t₅ t₄_ih t₅_ih, show (term.subst x v (term.value v') = ↑v'), by unfold term.subst, show (term.subst x v (term.var y) = (term.var y)), from ( have h: term.subst x v (term.var y) = (if x = y then v else y), by unfold term.subst, if x_is_y: x = y then ( have free_in_term y (term.var y), from free_in_term.var y, have free_in_term x (term.var y), from x_is_y.symm ▸ this, show term.subst x v (term.var y) = y, from absurd this x_not_free ) else ( show term.subst x v (term.var y) = y, by { simp[x_is_y] at h, assumption } ) ), show (term.subst x v (term.unop unop t₁) = term.unop unop t₁), from ( have h: term.subst x v (term.unop unop t₁) = term.unop unop (term.subst x v t₁), by unfold term.subst, have ¬ free_in_term x t₁, from ( assume : free_in_term x t₁, have free_in_term x (term.unop unop t₁), from free_in_term.unop this, show «false», from x_not_free this ), have term.subst x v t₁ = t₁, from t₁_ih this, show term.subst x v (term.unop unop t₁) = term.unop unop t₁, from @eq.subst term (λa, term.subst x v (term.unop unop t₁) = term.unop unop a) (term.subst x v t₁) t₁ this h ), show (term.subst x v (term.binop binop t₂ t₃) = term.binop binop t₂ t₃), from ( have h: term.subst x v (term.binop binop t₂ t₃) = term.binop binop (term.subst x v t₂) (term.subst x v t₃), by unfold term.subst, have ¬ free_in_term x t₂, from ( assume : free_in_term x t₂, have free_in_term x (term.binop binop t₂ t₃), from free_in_term.binop₁ this, show «false», from x_not_free this ), have t2_subst: term.subst x v t₂ = t₂, from t₂_ih this, have ¬ free_in_term x t₃, from ( assume : free_in_term x t₃, have free_in_term x (term.binop binop t₂ t₃), from free_in_term.binop₂ this, show «false», from x_not_free this ), have t3_subst: term.subst x v t₃ = t₃, from t₃_ih this, have term.subst x v (term.binop binop t₂ t₃) = term.binop binop t₂ (term.subst x v t₃), from @eq.subst term (λa, term.subst x v (term.binop binop t₂ t₃) = term.binop binop a (term.subst x v t₃)) (term.subst x v t₂) t₂ t2_subst h, show term.subst x v (term.binop binop t₂ t₃) = term.binop binop t₂ t₃, from @eq.subst term (λa, term.subst x v (term.binop binop t₂ t₃) = term.binop binop t₂ a) (term.subst x v t₃) t₃ t3_subst this ), show (term.subst x v (term.app t₄ t₅) = term.app t₄ t₅), from ( have h: term.subst x v (term.app t₄ t₅) = term.app (term.subst x v t₄) (term.subst x v t₅), by unfold term.subst, have ¬ free_in_term x t₄, from ( assume : free_in_term x t₄, have free_in_term x (term.app t₄ t₅), from free_in_term.app₁ this, show «false», from x_not_free this ), have t4_subst: term.subst x v t₄ = t₄, from t₄_ih this, have ¬ free_in_term x t₅, from ( assume : free_in_term x t₅, have free_in_term x (term.app t₄ t₅), from free_in_term.app₂ this, show «false», from x_not_free this ), have t5_subst: term.subst x v t₅ = t₅, from t₅_ih this, have term.subst x v (term.app t₄ t₅) = term.app t₄ (term.subst x v t₅), from @eq.subst term (λa, term.subst x v (term.app t₄ t₅) = term.app a (term.subst x v t₅)) (term.subst x v t₄) t₄ t4_subst h, show term.subst x v (term.app t₄ t₅) = term.app t₄ t₅, from @eq.subst term (λa, term.subst x v (term.app t₄ t₅) = term.app t₄ a) (term.subst x v t₅) t₅ t5_subst this ) end lemma unchanged_of_substt_nonfree_term {t: term} {x: var} {t': term}: x ∉ FV t → (term.substt x t' t = t) := assume x_not_free: ¬ free_in_term x t, begin induction t with v y unop t₁ t₁_ih binop t₂ t₃ t₂_ih t₃_ih t₄ t₅ t₄_ih t₅_ih, show (term.substt x t' (term.value v) = ↑v), by unfold term.substt, show (term.substt x t' (term.var y) = (term.var y)), from ( have h: term.substt x t' (term.var y) = (if x = y then t' else y), by unfold term.substt, if x_is_y: x = y then ( have free_in_term y (term.var y), from free_in_term.var y, have free_in_term x (term.var y), from x_is_y.symm ▸ this, show term.substt x t' (term.var y) = y, from absurd this x_not_free ) else ( show term.substt x t' (term.var y) = y, by { simp[x_is_y] at h, assumption } ) ), show (term.substt x t' (term.unop unop t₁) = term.unop unop t₁), from ( have h: term.substt x t' (term.unop unop t₁) = term.unop unop (term.substt x t' t₁), by unfold term.substt, have ¬ free_in_term x t₁, from ( assume : free_in_term x t₁, have free_in_term x (term.unop unop t₁), from free_in_term.unop this, show «false», from x_not_free this ), have term.substt x t' t₁ = t₁, from t₁_ih this, show term.substt x t' (term.unop unop t₁) = term.unop unop t₁, from @eq.subst term (λa, term.substt x t' (term.unop unop t₁) = term.unop unop a) (term.substt x t' t₁) t₁ this h ), show (term.substt x t' (term.binop binop t₂ t₃) = term.binop binop t₂ t₃), from ( have h: term.substt x t' (term.binop binop t₂ t₃) = term.binop binop (term.substt x t' t₂) (term.substt x t' t₃), by unfold term.substt, have ¬ free_in_term x t₂, from ( assume : free_in_term x t₂, have free_in_term x (term.binop binop t₂ t₃), from free_in_term.binop₁ this, show «false», from x_not_free this ), have t2_substt: term.substt x t' t₂ = t₂, from t₂_ih this, have ¬ free_in_term x t₃, from ( assume : free_in_term x t₃, have free_in_term x (term.binop binop t₂ t₃), from free_in_term.binop₂ this, show «false», from x_not_free this ), have t3_substt: term.substt x t' t₃ = t₃, from t₃_ih this, have term.substt x t' (term.binop binop t₂ t₃) = term.binop binop t₂ (term.substt x t' t₃), from @eq.subst term (λa, term.substt x t' (term.binop binop t₂ t₃) = term.binop binop a (term.substt x t' t₃)) (term.substt x t' t₂) t₂ t2_substt h, show term.substt x t' (term.binop binop t₂ t₃) = term.binop binop t₂ t₃, from @eq.subst term (λa, term.substt x t' (term.binop binop t₂ t₃) = term.binop binop t₂ a) (term.substt x t' t₃) t₃ t3_substt this ), show (term.substt x t' (term.app t₄ t₅) = term.app t₄ t₅), from ( have h: term.substt x t' (term.app t₄ t₅) = term.app (term.substt x t' t₄) (term.substt x t' t₅), by unfold term.substt, have ¬ free_in_term x t₄, from ( assume : free_in_term x t₄, have free_in_term x (term.app t₄ t₅), from free_in_term.app₁ this, show «false», from x_not_free this ), have t4_substt: term.substt x t' t₄ = t₄, from t₄_ih this, have ¬ free_in_term x t₅, from ( assume : free_in_term x t₅, have free_in_term x (term.app t₄ t₅), from free_in_term.app₂ this, show «false», from x_not_free this ), have t5_substt: term.substt x t' t₅ = t₅, from t₅_ih this, have term.substt x t' (term.app t₄ t₅) = term.app t₄ (term.substt x t' t₅), from @eq.subst term (λa, term.substt x t' (term.app t₄ t₅) = term.app a (term.substt x t' t₅)) (term.substt x t' t₄) t₄ t4_substt h, show term.substt x t' (term.app t₄ t₅) = term.app t₄ t₅, from @eq.subst term (λa, term.substt x t' (term.app t₄ t₅) = term.app t₄ a) (term.substt x t' t₅) t₅ t5_substt this ) end lemma unchanged_of_subst_env_nonfree_term {t: term}: closed t → (∀σ, term.subst_env σ t = t) := assume x_not_free: (∀x, x ∉ FV t), assume σ: env, begin induction σ with σ' x v ih, show (term.subst_env env.empty t = t), by unfold term.subst_env, show (term.subst_env (σ'[x↦v]) t = t), by calc term.subst_env (σ'[x↦v]) t = term.subst x v (term.subst_env σ' t) : by unfold term.subst_env ... = term.subst x v t : by rw[ih] ... = t : unchanged_of_subst_nonfree_term (x_not_free x) end lemma term.subst.var.same {x: var} {v: value}: term.subst x v x = v := have h: term.subst x v (term.var x) = (if x = x then v else x), by unfold term.subst, have (if x = x then (term.value v) else (term.var x)) = (term.value v), by simp, show term.subst x v x = v, from eq.trans h this lemma term.subst.var.diff {x y: var} {v: value}: (x ≠ y) → (term.subst x v y = y) := assume x_neq_y: x ≠ y, have h: term.subst x v (term.var y) = (if x = y then v else y), by unfold term.subst, have (if x = y then (term.value v) else (term.var y)) = (term.var y), by simp[x_neq_y], show term.subst x v y = y, from eq.trans h this lemma term.substt.var.diff {x y: var} {t: term}: (x ≠ y) → (term.substt x t y = y) := assume x_neq_y: x ≠ y, have h: term.substt x t (term.var y) = (if x = y then t else y), by unfold term.substt, have (if x = y then t else (term.var y)) = (term.var y), by simp[x_neq_y], show term.substt x t y = y, from eq.trans h this lemma unchanged_of_subst_nonfree_prop {P: prop} {x: var} {v: value}: x ∉ FV P → (prop.subst x v P = P) := assume x_not_free: ¬ free_in_prop x P, begin induction P, case prop.term t { from ( have h: prop.subst x v (prop.term t) = term.subst x v t, by unfold prop.subst, have ¬ free_in_term x t, from ( assume : free_in_term x t, have free_in_prop x (prop.term t), from free_in_prop.term this, show «false», from x_not_free this ), have term.subst x v t = t, from unchanged_of_subst_nonfree_term this, show prop.subst x v t = prop.term t, from @eq.subst term (λa, prop.subst x v (prop.term t) = prop.term a) (term.subst x v t) t this h )}, case prop.not P₁ ih { from ( have h: prop.subst x v P₁.not = (prop.subst x v P₁).not, by unfold prop.subst, have ¬ free_in_prop x P₁, from ( assume : free_in_prop x P₁, have free_in_prop x P₁.not, from free_in_prop.not this, show «false», from x_not_free this ), have prop.subst x v P₁ = P₁, from ih this, show prop.subst x v P₁.not = P₁.not, from @eq.subst prop (λa, prop.subst x v P₁.not = prop.not a) (prop.subst x v P₁) P₁ this h )}, case prop.and P₁ P₂ P₁_ih P₂_ih { from ( have h: prop.subst x v (prop.and P₁ P₂) = (prop.subst x v P₁ ⋀ prop.subst x v P₂), by unfold prop.subst, have ¬ free_in_prop x P₁, from ( assume : free_in_prop x P₁, have free_in_prop x (P₁ ⋀ P₂), from free_in_prop.and₁ this, show «false», from x_not_free this ), have h1: prop.subst x v P₁ = P₁, from P₁_ih this, have ¬ free_in_prop x P₂, from ( assume : free_in_prop x P₂, have free_in_prop x (P₁ ⋀ P₂), from free_in_prop.and₂ this, show «false», from x_not_free this ), have h2: prop.subst x v P₂ = P₂, from P₂_ih this, have prop.subst x v (P₁ ⋀ P₂) = (P₁ ⋀ prop.subst x v P₂), from @eq.subst prop (λa, prop.subst x v (prop.and P₁ P₂) = (a ⋀ prop.subst x v P₂)) (prop.subst x v P₁) P₁ h1 h, show prop.subst x v (P₁ ⋀ P₂) = (P₁ ⋀ P₂), from @eq.subst prop (λa, prop.subst x v (prop.and P₁ P₂) = (P₁ ⋀ a)) (prop.subst x v P₂) P₂ h2 this )}, case prop.or P₁ P₂ P₁_ih P₂_ih { from ( have h: prop.subst x v (prop.or P₁ P₂) = (prop.subst x v P₁ ⋁ prop.subst x v P₂), by unfold prop.subst, have ¬ free_in_prop x P₁, from ( assume : free_in_prop x P₁, have free_in_prop x (P₁ ⋁ P₂), from free_in_prop.or₁ this, show «false», from x_not_free this ), have h1: prop.subst x v P₁ = P₁, from P₁_ih this, have ¬ free_in_prop x P₂, from ( assume : free_in_prop x P₂, have free_in_prop x (P₁ ⋁ P₂), from free_in_prop.or₂ this, show «false», from x_not_free this ), have h2: prop.subst x v P₂ = P₂, from P₂_ih this, have prop.subst x v (P₁ ⋁ P₂) = (P₁ ⋁ prop.subst x v P₂), from @eq.subst prop (λa, prop.subst x v (prop.or P₁ P₂) = (a ⋁ prop.subst x v P₂)) (prop.subst x v P₁) P₁ h1 h, show prop.subst x v (P₁ ⋁ P₂) = (P₁ ⋁ P₂), from @eq.subst prop (λa, prop.subst x v (prop.or P₁ P₂) = (P₁ ⋁ a)) (prop.subst x v P₂) P₂ h2 this )}, case prop.pre t₁ t₂ { from ( have h: prop.subst x v (prop.pre t₁ t₂) = prop.pre (term.subst x v t₁) (term.subst x v t₂), by unfold prop.subst, have ¬ free_in_term x t₁, from ( assume : free_in_term x t₁, have free_in_prop x (prop.pre t₁ t₂), from free_in_prop.pre₁ this, show «false», from x_not_free this ), have h1: term.subst x v t₁ = t₁, from unchanged_of_subst_nonfree_term this, have ¬ free_in_term x t₂, from ( assume : free_in_term x t₂, have free_in_prop x (prop.pre t₁ t₂), from free_in_prop.pre₂ this, show «false», from x_not_free this ), have h2: term.subst x v t₂ = t₂, from unchanged_of_subst_nonfree_term this, have prop.subst x v (prop.pre t₁ t₂) = prop.pre t₁ (term.subst x v t₂), from @eq.subst term (λa, prop.subst x v (prop.pre t₁ t₂) = prop.pre a (term.subst x v t₂)) (term.subst x v t₁) t₁ h1 h, show prop.subst x v (prop.pre t₁ t₂) = prop.pre t₁ t₂, from @eq.subst term (λa, prop.subst x v (prop.pre t₁ t₂) = prop.pre t₁ a) (term.subst x v t₂) t₂ h2 this )}, case prop.pre₁ op t { from ( have h: prop.subst x v (prop.pre₁ op t) = prop.pre₁ op (term.subst x v t), by unfold prop.subst, have ¬ free_in_term x t, from ( assume : free_in_term x t, have free_in_prop x (prop.pre₁ op t), from free_in_prop.preop this, show «false», from x_not_free this ), have term.subst x v t = t, from unchanged_of_subst_nonfree_term this, show prop.subst x v (prop.pre₁ op t) = prop.pre₁ op t, from @eq.subst term (λa, prop.subst x v (prop.pre₁ op t) = prop.pre₁ op a) (term.subst x v t) t this h )}, case prop.pre₂ op t₁ t₂ { from ( have h: prop.subst x v (prop.pre₂ op t₁ t₂) = prop.pre₂ op (term.subst x v t₁) (term.subst x v t₂), by unfold prop.subst, have ¬ free_in_term x t₁, from ( assume : free_in_term x t₁, have free_in_prop x (prop.pre₂ op t₁ t₂), from free_in_prop.preop₁ this, show «false», from x_not_free this ), have h1: term.subst x v t₁ = t₁, from unchanged_of_subst_nonfree_term this, have ¬ free_in_term x t₂, from ( assume : free_in_term x t₂, have free_in_prop x (prop.pre₂ op t₁ t₂), from free_in_prop.preop₂ this, show «false», from x_not_free this ), have h2: term.subst x v t₂ = t₂, from unchanged_of_subst_nonfree_term this, have prop.subst x v (prop.pre₂ op t₁ t₂) = prop.pre₂ op t₁ (term.subst x v t₂), from @eq.subst term (λa, prop.subst x v (prop.pre₂ op t₁ t₂) = prop.pre₂ op a (term.subst x v t₂)) (term.subst x v t₁) t₁ h1 h, show prop.subst x v (prop.pre₂ op t₁ t₂) = prop.pre₂ op t₁ t₂, from @eq.subst term (λa, prop.subst x v (prop.pre₂ op t₁ t₂) = prop.pre₂ op t₁ a) (term.subst x v t₂) t₂ h2 this )}, case prop.call t { from ( have h: prop.subst x v (prop.call t) = prop.call (term.subst x v t), by unfold prop.subst, have ¬ free_in_term x t, from ( assume : free_in_term x t, have free_in_prop x (prop.call t), from free_in_prop.call this, show «false», from x_not_free this ), have h1: term.subst x v t = t, from unchanged_of_subst_nonfree_term this, show prop.subst x v (prop.call t) = prop.call t, from @eq.subst term (λa, prop.subst x v (prop.call t) = prop.call a) (term.subst x v t) t h1 h )}, case prop.post t₁ t₂ { from ( have h: prop.subst x v (prop.post t₁ t₂) = prop.post (term.subst x v t₁) (term.subst x v t₂), by unfold prop.subst, have ¬ free_in_term x t₁, from ( assume : free_in_term x t₁, have free_in_prop x (prop.post t₁ t₂), from free_in_prop.post₁ this, show «false», from x_not_free this ), have h1: term.subst x v t₁ = t₁, from unchanged_of_subst_nonfree_term this, have ¬ free_in_term x t₂, from ( assume : free_in_term x t₂, have free_in_prop x (prop.post t₁ t₂), from free_in_prop.post₂ this, show «false», from x_not_free this ), have h2: term.subst x v t₂ = t₂, from unchanged_of_subst_nonfree_term this, have prop.subst x v (prop.post t₁ t₂) = prop.post t₁ (term.subst x v t₂), from @eq.subst term (λa, prop.subst x v (prop.post t₁ t₂) = prop.post a (term.subst x v t₂)) (term.subst x v t₁) t₁ h1 h, show prop.subst x v (prop.post t₁ t₂) = prop.post t₁ t₂, from @eq.subst term (λa, prop.subst x v (prop.post t₁ t₂) = prop.post t₁ a) (term.subst x v t₂) t₂ h2 this )}, case prop.forallc y P' P'_ih { from ( have h: prop.subst x v (prop.forallc y P') = prop.forallc y (if x = y then P' else prop.subst x v P'), by unfold prop.subst, if x_eq_y: x = y then ( have (if x = y then P' else prop.subst x v P') = P', by simp[x_eq_y], show prop.subst x v (prop.forallc y P') = prop.forallc y P', from @eq.subst prop (λa, prop.subst x v (prop.forallc y P') = prop.forallc y a) (if x = y then P' else prop.subst x v P') P' this h ) else ( have (if x = y then P' else prop.subst x v P') = prop.subst x v P', by simp[x_eq_y], have h4: prop.subst x v (prop.forallc y P') = prop.forallc y (prop.subst x v P'), from @eq.subst prop (λa, prop.subst x v (prop.forallc y P') = prop.forallc y a) (if x = y then P' else prop.subst x v P') (prop.subst x v P') this h, have ¬ free_in_prop x P', from ( assume : free_in_prop x P', have free_in_prop x (prop.forallc y P'), from free_in_prop.forallc x_eq_y this, show «false», from x_not_free this ), have prop.subst x v P' = P', from P'_ih this, show prop.subst x v (prop.forallc y P') = prop.forallc y P', from @eq.subst prop (λa, prop.subst x v (prop.forallc y P') = prop.forallc y a) (prop.subst x v P') P' this h4 ) )}, case prop.exis y P' P'_ih { from ( have h: prop.subst x v (prop.exis y P') = prop.exis y (if x = y then P' else prop.subst x v P'), by unfold prop.subst, if x_eq_y: x = y then ( have (if x = y then P' else prop.subst x v P') = P', by simp[x_eq_y], show prop.subst x v (prop.exis y P') = prop.exis y P', from @eq.subst prop (λa, prop.subst x v (prop.exis y P') = prop.exis y a) (if x = y then P' else prop.subst x v P') P' this h ) else ( have (if x = y then P' else prop.subst x v P') = prop.subst x v P', by simp[x_eq_y], have h2: prop.subst x v (prop.exis y P') = prop.exis y (prop.subst x v P'), from @eq.subst prop (λa, prop.subst x v (prop.exis y P') = prop.exis y a) (if x = y then P' else prop.subst x v P') (prop.subst x v P') this h, have ¬ free_in_prop x P', from ( assume : free_in_prop x P', have free_in_prop x (prop.exis y P'), from free_in_prop.exis x_eq_y this, show «false», from x_not_free this ), have prop.subst x v P' = P', from P'_ih this, show prop.subst x v (prop.exis y P') = prop.exis y P', from @eq.subst prop (λa, prop.subst x v (prop.exis y P') = prop.exis y a) (prop.subst x v P') P' this h2 ) )} end lemma unchanged_of_subst_nonfree_vc {P: vc} {x: var} {v: value}: x ∉ FV P → (vc.subst x v P = P) := assume x_not_free: ¬ free_in_vc x P, begin induction P, case vc.term t { from ( have h: vc.subst x v (vc.term t) = term.subst x v t, by unfold vc.subst, have ¬ free_in_term x t, from ( assume : free_in_term x t, have free_in_vc x (vc.term t), from free_in_vc.term this, show «false», from x_not_free this ), have term.subst x v t = t, from unchanged_of_subst_nonfree_term this, show vc.subst x v t = vc.term t, from @eq.subst term (λa, vc.subst x v (vc.term t) = vc.term a) (term.subst x v t) t this h )}, case vc.not P₁ ih { from ( have h: vc.subst x v P₁.not = (vc.subst x v P₁).not, by unfold vc.subst, have ¬ free_in_vc x P₁, from ( assume : free_in_vc x P₁, have free_in_vc x P₁.not, from free_in_vc.not this, show «false», from x_not_free this ), have vc.subst x v P₁ = P₁, from ih this, show vc.subst x v P₁.not = P₁.not, from @eq.subst vc (λa, vc.subst x v P₁.not = vc.not a) (vc.subst x v P₁) P₁ this h )}, case vc.and P₁ P₂ P₁_ih P₂_ih { from ( have h: vc.subst x v (vc.and P₁ P₂) = (vc.subst x v P₁ ⋀ vc.subst x v P₂), by unfold vc.subst, have ¬ free_in_vc x P₁, from ( assume : free_in_vc x P₁, have free_in_vc x (P₁ ⋀ P₂), from free_in_vc.and₁ this, show «false», from x_not_free this ), have h1: vc.subst x v P₁ = P₁, from P₁_ih this, have ¬ free_in_vc x P₂, from ( assume : free_in_vc x P₂, have free_in_vc x (P₁ ⋀ P₂), from free_in_vc.and₂ this, show «false», from x_not_free this ), have h2: vc.subst x v P₂ = P₂, from P₂_ih this, have vc.subst x v (P₁ ⋀ P₂) = (P₁ ⋀ vc.subst x v P₂), from @eq.subst vc (λa, vc.subst x v (vc.and P₁ P₂) = (a ⋀ vc.subst x v P₂)) (vc.subst x v P₁) P₁ h1 h, show vc.subst x v (P₁ ⋀ P₂) = (P₁ ⋀ P₂), from @eq.subst vc (λa, vc.subst x v (vc.and P₁ P₂) = (P₁ ⋀ a)) (vc.subst x v P₂) P₂ h2 this )}, case vc.or P₁ P₂ P₁_ih P₂_ih { from ( have h: vc.subst x v (vc.or P₁ P₂) = (vc.subst x v P₁ ⋁ vc.subst x v P₂), by unfold vc.subst, have ¬ free_in_vc x P₁, from ( assume : free_in_vc x P₁, have free_in_vc x (P₁ ⋁ P₂), from free_in_vc.or₁ this, show «false», from x_not_free this ), have h1: vc.subst x v P₁ = P₁, from P₁_ih this, have ¬ free_in_vc x P₂, from ( assume : free_in_vc x P₂, have free_in_vc x (P₁ ⋁ P₂), from free_in_vc.or₂ this, show «false», from x_not_free this ), have h2: vc.subst x v P₂ = P₂, from P₂_ih this, have vc.subst x v (P₁ ⋁ P₂) = (P₁ ⋁ vc.subst x v P₂), from @eq.subst vc (λa, vc.subst x v (vc.or P₁ P₂) = (a ⋁ vc.subst x v P₂)) (vc.subst x v P₁) P₁ h1 h, show vc.subst x v (P₁ ⋁ P₂) = (P₁ ⋁ P₂), from @eq.subst vc (λa, vc.subst x v (vc.or P₁ P₂) = (P₁ ⋁ a)) (vc.subst x v P₂) P₂ h2 this )}, case vc.pre t₁ t₂ { from ( have h: vc.subst x v (vc.pre t₁ t₂) = vc.pre (term.subst x v t₁) (term.subst x v t₂), by unfold vc.subst, have ¬ free_in_term x t₁, from ( assume : free_in_term x t₁, have free_in_vc x (vc.pre t₁ t₂), from free_in_vc.pre₁ this, show «false», from x_not_free this ), have h1: term.subst x v t₁ = t₁, from unchanged_of_subst_nonfree_term this, have ¬ free_in_term x t₂, from ( assume : free_in_term x t₂, have free_in_vc x (vc.pre t₁ t₂), from free_in_vc.pre₂ this, show «false», from x_not_free this ), have h2: term.subst x v t₂ = t₂, from unchanged_of_subst_nonfree_term this, have vc.subst x v (vc.pre t₁ t₂) = vc.pre t₁ (term.subst x v t₂), from @eq.subst term (λa, vc.subst x v (vc.pre t₁ t₂) = vc.pre a (term.subst x v t₂)) (term.subst x v t₁) t₁ h1 h, show vc.subst x v (vc.pre t₁ t₂) = vc.pre t₁ t₂, from @eq.subst term (λa, vc.subst x v (vc.pre t₁ t₂) = vc.pre t₁ a) (term.subst x v t₂) t₂ h2 this )}, case vc.pre₁ op t { from ( have h: vc.subst x v (vc.pre₁ op t) = vc.pre₁ op (term.subst x v t), by unfold vc.subst, have ¬ free_in_term x t, from ( assume : free_in_term x t, have free_in_vc x (vc.pre₁ op t), from free_in_vc.preop this, show «false», from x_not_free this ), have term.subst x v t = t, from unchanged_of_subst_nonfree_term this, show vc.subst x v (vc.pre₁ op t) = vc.pre₁ op t, from @eq.subst term (λa, vc.subst x v (vc.pre₁ op t) = vc.pre₁ op a) (term.subst x v t) t this h )}, case vc.pre₂ op t₁ t₂ { from ( have h: vc.subst x v (vc.pre₂ op t₁ t₂) = vc.pre₂ op (term.subst x v t₁) (term.subst x v t₂), by unfold vc.subst, have ¬ free_in_term x t₁, from ( assume : free_in_term x t₁, have free_in_vc x (vc.pre₂ op t₁ t₂), from free_in_vc.preop₁ this, show «false», from x_not_free this ), have h1: term.subst x v t₁ = t₁, from unchanged_of_subst_nonfree_term this, have ¬ free_in_term x t₂, from ( assume : free_in_term x t₂, have free_in_vc x (vc.pre₂ op t₁ t₂), from free_in_vc.preop₂ this, show «false», from x_not_free this ), have h2: term.subst x v t₂ = t₂, from unchanged_of_subst_nonfree_term this, have vc.subst x v (vc.pre₂ op t₁ t₂) = vc.pre₂ op t₁ (term.subst x v t₂), from @eq.subst term (λa, vc.subst x v (vc.pre₂ op t₁ t₂) = vc.pre₂ op a (term.subst x v t₂)) (term.subst x v t₁) t₁ h1 h, show vc.subst x v (vc.pre₂ op t₁ t₂) = vc.pre₂ op t₁ t₂, from @eq.subst term (λa, vc.subst x v (vc.pre₂ op t₁ t₂) = vc.pre₂ op t₁ a) (term.subst x v t₂) t₂ h2 this )}, case vc.post t₁ t₂ { from ( have h: vc.subst x v (vc.post t₁ t₂) = vc.post (term.subst x v t₁) (term.subst x v t₂), by unfold vc.subst, have ¬ free_in_term x t₁, from ( assume : free_in_term x t₁, have free_in_vc x (vc.post t₁ t₂), from free_in_vc.post₁ this, show «false», from x_not_free this ), have h1: term.subst x v t₁ = t₁, from unchanged_of_subst_nonfree_term this, have ¬ free_in_term x t₂, from ( assume : free_in_term x t₂, have free_in_vc x (vc.post t₁ t₂), from free_in_vc.post₂ this, show «false», from x_not_free this ), have h2: term.subst x v t₂ = t₂, from unchanged_of_subst_nonfree_term this, have vc.subst x v (vc.post t₁ t₂) = vc.post t₁ (term.subst x v t₂), from @eq.subst term (λa, vc.subst x v (vc.post t₁ t₂) = vc.post a (term.subst x v t₂)) (term.subst x v t₁) t₁ h1 h, show vc.subst x v (vc.post t₁ t₂) = vc.post t₁ t₂, from @eq.subst term (λa, vc.subst x v (vc.post t₁ t₂) = vc.post t₁ a) (term.subst x v t₂) t₂ h2 this )}, case vc.univ y P' P'_ih { from ( have h: vc.subst x v (vc.univ y P') = vc.univ y (if x = y then P' else vc.subst x v P'), by unfold vc.subst, if x_eq_y: x = y then ( have (if x = y then P' else vc.subst x v P') = P', by simp[x_eq_y], show vc.subst x v (vc.univ y P') = vc.univ y P', from @eq.subst vc (λa, vc.subst x v (vc.univ y P') = vc.univ y a) (if x = y then P' else vc.subst x v P') P' this h ) else ( have (if x = y then P' else vc.subst x v P') = vc.subst x v P', by simp[x_eq_y], have h2: vc.subst x v (vc.univ y P') = vc.univ y (vc.subst x v P'), from @eq.subst vc (λa, vc.subst x v (vc.univ y P') = vc.univ y a) (if x = y then P' else vc.subst x v P') (vc.subst x v P') this h, have ¬ free_in_vc x P', from ( assume : free_in_vc x P', have free_in_vc x (vc.univ y P'), from free_in_vc.univ x_eq_y this, show «false», from x_not_free this ), have vc.subst x v P' = P', from P'_ih this, show vc.subst x v (vc.univ y P') = vc.univ y P', from @eq.subst vc (λa, vc.subst x v (vc.univ y P') = vc.univ y a) (vc.subst x v P') P' this h2 ) )} end lemma unchanged_of_substt_nonfree_vc {P: vc} {x: var} {t: term}: x ∉ FV P → (vc.substt x t P = P) := assume x_not_free: ¬ free_in_vc x P, begin induction P, case vc.term t₁ { from ( have h: vc.substt x t (vc.term t₁) = term.substt x t t₁, by unfold vc.substt, have ¬ free_in_term x t₁, from ( assume : free_in_term x t₁, have free_in_vc x (vc.term t₁), from free_in_vc.term this, show «false», from x_not_free this ), have term.substt x t t₁ = t₁, from unchanged_of_substt_nonfree_term this, show vc.substt x t t₁ = vc.term t₁, from @eq.subst term (λa, vc.substt x t (vc.term t₁) = vc.term a) (term.substt x t t₁) t₁ this h )}, case vc.not P₁ ih { from ( have h: vc.substt x t P₁.not = (vc.substt x t P₁).not, by unfold vc.substt, have ¬ free_in_vc x P₁, from ( assume : free_in_vc x P₁, have free_in_vc x P₁.not, from free_in_vc.not this, show «false», from x_not_free this ), have vc.substt x t P₁ = P₁, from ih this, show vc.substt x t P₁.not = P₁.not, from @eq.subst vc (λa, vc.substt x t P₁.not = vc.not a) (vc.substt x t P₁) P₁ this h )}, case vc.and P₁ P₂ P₁_ih P₂_ih { from ( have h: vc.substt x t (vc.and P₁ P₂) = (vc.substt x t P₁ ⋀ vc.substt x t P₂), by unfold vc.substt, have ¬ free_in_vc x P₁, from ( assume : free_in_vc x P₁, have free_in_vc x (P₁ ⋀ P₂), from free_in_vc.and₁ this, show «false», from x_not_free this ), have h1: vc.substt x t P₁ = P₁, from P₁_ih this, have ¬ free_in_vc x P₂, from ( assume : free_in_vc x P₂, have free_in_vc x (P₁ ⋀ P₂), from free_in_vc.and₂ this, show «false», from x_not_free this ), have h2: vc.substt x t P₂ = P₂, from P₂_ih this, have vc.substt x t (P₁ ⋀ P₂) = (P₁ ⋀ vc.substt x t P₂), from @eq.subst vc (λa, vc.substt x t (vc.and P₁ P₂) = (a ⋀ vc.substt x t P₂)) (vc.substt x t P₁) P₁ h1 h, show vc.substt x t (P₁ ⋀ P₂) = (P₁ ⋀ P₂), from @eq.subst vc (λa, vc.substt x t (vc.and P₁ P₂) = (P₁ ⋀ a)) (vc.substt x t P₂) P₂ h2 this )}, case vc.or P₁ P₂ P₁_ih P₂_ih { from ( have h: vc.substt x t (vc.or P₁ P₂) = (vc.substt x t P₁ ⋁ vc.substt x t P₂), by unfold vc.substt, have ¬ free_in_vc x P₁, from ( assume : free_in_vc x P₁, have free_in_vc x (P₁ ⋁ P₂), from free_in_vc.or₁ this, show «false», from x_not_free this ), have h1: vc.substt x t P₁ = P₁, from P₁_ih this, have ¬ free_in_vc x P₂, from ( assume : free_in_vc x P₂, have free_in_vc x (P₁ ⋁ P₂), from free_in_vc.or₂ this, show «false», from x_not_free this ), have h2: vc.substt x t P₂ = P₂, from P₂_ih this, have vc.substt x t (P₁ ⋁ P₂) = (P₁ ⋁ vc.substt x t P₂), from @eq.subst vc (λa, vc.substt x t (vc.or P₁ P₂) = (a ⋁ vc.substt x t P₂)) (vc.substt x t P₁) P₁ h1 h, show vc.substt x t (P₁ ⋁ P₂) = (P₁ ⋁ P₂), from @eq.subst vc (λa, vc.substt x t (vc.or P₁ P₂) = (P₁ ⋁ a)) (vc.substt x t P₂) P₂ h2 this )}, case vc.pre t₁ t₂ { from ( have h: vc.substt x t (vc.pre t₁ t₂) = vc.pre (term.substt x t t₁) (term.substt x t t₂), by unfold vc.substt, have ¬ free_in_term x t₁, from ( assume : free_in_term x t₁, have free_in_vc x (vc.pre t₁ t₂), from free_in_vc.pre₁ this, show «false», from x_not_free this ), have h1: term.substt x t t₁ = t₁, from unchanged_of_substt_nonfree_term this, have ¬ free_in_term x t₂, from ( assume : free_in_term x t₂, have free_in_vc x (vc.pre t₁ t₂), from free_in_vc.pre₂ this, show «false», from x_not_free this ), have h2: term.substt x t t₂ = t₂, from unchanged_of_substt_nonfree_term this, have vc.substt x t (vc.pre t₁ t₂) = vc.pre t₁ (term.substt x t t₂), from @eq.subst term (λa, vc.substt x t (vc.pre t₁ t₂) = vc.pre a (term.substt x t t₂)) (term.substt x t t₁) t₁ h1 h, show vc.substt x t (vc.pre t₁ t₂) = vc.pre t₁ t₂, from @eq.subst term (λa, vc.substt x t (vc.pre t₁ t₂) = vc.pre t₁ a) (term.substt x t t₂) t₂ h2 this )}, case vc.pre₁ op t₁ { from ( have h: vc.substt x t (vc.pre₁ op t₁) = vc.pre₁ op (term.substt x t t₁), by unfold vc.substt, have ¬ free_in_term x t₁, from ( assume : free_in_term x t₁, have free_in_vc x (vc.pre₁ op t₁), from free_in_vc.preop this, show «false», from x_not_free this ), have term.substt x t t₁ = t₁, from unchanged_of_substt_nonfree_term this, show vc.substt x t (vc.pre₁ op t₁ ) = vc.pre₁ op t₁, from @eq.subst term (λa, vc.substt x t (vc.pre₁ op t₁) = vc.pre₁ op a) (term.substt x t t₁) t₁ this h )}, case vc.pre₂ op t₁ t₂ { from ( have h: vc.substt x t (vc.pre₂ op t₁ t₂) = vc.pre₂ op (term.substt x t t₁) (term.substt x t t₂), by unfold vc.substt, have ¬ free_in_term x t₁, from ( assume : free_in_term x t₁, have free_in_vc x (vc.pre₂ op t₁ t₂), from free_in_vc.preop₁ this, show «false», from x_not_free this ), have h1: term.substt x t t₁ = t₁, from unchanged_of_substt_nonfree_term this, have ¬ free_in_term x t₂, from ( assume : free_in_term x t₂, have free_in_vc x (vc.pre₂ op t₁ t₂), from free_in_vc.preop₂ this, show «false», from x_not_free this ), have h2: term.substt x t t₂ = t₂, from unchanged_of_substt_nonfree_term this, have vc.substt x t (vc.pre₂ op t₁ t₂) = vc.pre₂ op t₁ (term.substt x t t₂), from @eq.subst term (λa, vc.substt x t (vc.pre₂ op t₁ t₂) = vc.pre₂ op a (term.substt x t t₂)) (term.substt x t t₁) t₁ h1 h, show vc.substt x t (vc.pre₂ op t₁ t₂) = vc.pre₂ op t₁ t₂, from @eq.subst term (λa, vc.substt x t (vc.pre₂ op t₁ t₂) = vc.pre₂ op t₁ a) (term.substt x t t₂) t₂ h2 this )}, case vc.post t₁ t₂ { from ( have h: vc.substt x t (vc.post t₁ t₂) = vc.post (term.substt x t t₁) (term.substt x t t₂), by unfold vc.substt, have ¬ free_in_term x t₁, from ( assume : free_in_term x t₁, have free_in_vc x (vc.post t₁ t₂), from free_in_vc.post₁ this, show «false», from x_not_free this ), have h1: term.substt x t t₁ = t₁, from unchanged_of_substt_nonfree_term this, have ¬ free_in_term x t₂, from ( assume : free_in_term x t₂, have free_in_vc x (vc.post t₁ t₂), from free_in_vc.post₂ this, show «false», from x_not_free this ), have h2: term.substt x t t₂ = t₂, from unchanged_of_substt_nonfree_term this, have vc.substt x t (vc.post t₁ t₂) = vc.post t₁ (term.substt x t t₂), from @eq.subst term (λa, vc.substt x t (vc.post t₁ t₂) = vc.post a (term.substt x t t₂)) (term.substt x t t₁) t₁ h1 h, show vc.substt x t (vc.post t₁ t₂) = vc.post t₁ t₂, from @eq.subst term (λa, vc.substt x t (vc.post t₁ t₂) = vc.post t₁ a) (term.substt x t t₂) t₂ h2 this )}, case vc.univ y P' P'_ih { from ( have h: vc.substt x t (vc.univ y P') = vc.univ y (if x = y then P' else vc.substt x t P'), by unfold vc.substt, if x_eq_y: x = y then ( have (if x = y then P' else vc.substt x t P') = P', by simp[x_eq_y], show vc.substt x t (vc.univ y P') = vc.univ y P', from @eq.subst vc (λa, vc.substt x t (vc.univ y P') = vc.univ y a) (if x = y then P' else vc.substt x t P') P' this h ) else ( have (if x = y then P' else vc.substt x t P') = vc.substt x t P', by simp[x_eq_y], have h2: vc.substt x t (vc.univ y P') = vc.univ y (vc.substt x t P'), from @eq.subst vc (λa, vc.substt x t (vc.univ y P') = vc.univ y a) (if x = y then P' else vc.substt x t P') (vc.substt x t P') this h, have ¬ free_in_vc x P', from ( assume : free_in_vc x P', have free_in_vc x (vc.univ y P'), from free_in_vc.univ x_eq_y this, show «false», from x_not_free this ), have vc.substt x t P' = P', from P'_ih this, show vc.substt x t (vc.univ y P') = vc.univ y P', from @eq.subst vc (λa, vc.substt x t (vc.univ y P') = vc.univ y a) (vc.substt x t P') P' this h2 ) )} end lemma unchanged_of_subst_env_nonfree_vc {P: vc}: closed P → (∀σ, vc.subst_env σ P = P) := assume x_not_free: (∀x, x ∉ FV P), assume σ: env, begin induction σ with σ' x v ih, show (vc.subst_env env.empty P = P), by unfold vc.subst_env, show (vc.subst_env (σ'[x↦v]) P = P), by calc vc.subst_env (σ'[x↦v]) P = vc.subst x v (vc.subst_env σ' P) : by unfold vc.subst_env ... = vc.subst x v P : by rw[ih] ... = P : unchanged_of_subst_nonfree_vc (x_not_free x) end lemma free_of_subst_term {t: term} {x y: var} {v: value}: free_in_term x (term.subst y v t) → x ≠ y ∧ free_in_term x t := assume x_free_in_subst: free_in_term x (term.subst y v t), begin induction t with v' z unop t₁ t₁_ih binop t₂ t₃ t₂_ih t₃_ih t₄ t₅ t₄_ih t₅_ih, show x ≠ y ∧ free_in_term x (term.value v'), from ( have term.subst y v (term.value v') = v', by unfold term.subst, have free_in_term x v', from this ▸ x_free_in_subst, show x ≠ y ∧ free_in_term x (term.value v'), from absurd this free_in_term.value.inv ), show x ≠ y ∧ free_in_term x (term.var z), from ( have hite: term.subst y v (term.var z) = (if y = z then v else z), by unfold term.subst, if y_is_z: y = z then have term.subst y v (term.var z) = v, by { simp[y_is_z] at hite, rw[y_is_z], from hite }, have free_in_term x v, from this ▸ x_free_in_subst, show x ≠ y ∧ free_in_term x (term.var z), from absurd this free_in_term.value.inv else have term.subst y v (term.var z) = z, by { simp[y_is_z] at hite, from hite }, have free_in_term x z, from this ▸ x_free_in_subst, have x_is_z: x = z, from free_in_term.var.inv this, have x ≠ y, from x_is_z.symm ▸ (ne.symm y_is_z), show x ≠ y ∧ free_in_term x (term.var z), from ⟨this, x_is_z ▸ free_in_term.var x⟩ ), show x ≠ y ∧ free_in_term x (term.unop unop t₁), from ( have term.subst y v (term.unop unop t₁) = term.unop unop (term.subst y v t₁), by unfold term.subst, have free_in_term x (term.unop unop (term.subst y v t₁)), from this ▸ x_free_in_subst, have free_in_term x (term.subst y v t₁), from free_in_term.unop.inv this, have x ≠ y ∧ free_in_term x t₁, from t₁_ih this, show x ≠ y ∧ free_in_term x (term.unop unop t₁), from ⟨this.left, free_in_term.unop this.right⟩ ), show x ≠ y ∧ free_in_term x (term.binop binop t₂ t₃), from ( have term.subst y v (term.binop binop t₂ t₃) = term.binop binop (term.subst y v t₂) (term.subst y v t₃), by unfold term.subst, have free_in_term x (term.binop binop (term.subst y v t₂) (term.subst y v t₃)), from this ▸ x_free_in_subst, have free_in_term x (term.subst y v t₂) ∨ free_in_term x (term.subst y v t₃), from free_in_term.binop.inv this, or.elim this ( assume : free_in_term x (term.subst y v t₂), have x ≠ y ∧ free_in_term x t₂, from t₂_ih this, show x ≠ y ∧ free_in_term x (term.binop binop t₂ t₃), from ⟨this.left, free_in_term.binop₁ this.right⟩ ) ( assume : free_in_term x (term.subst y v t₃), have x ≠ y ∧ free_in_term x t₃, from t₃_ih this, show x ≠ y ∧ free_in_term x (term.binop binop t₂ t₃), from ⟨this.left, free_in_term.binop₂ this.right⟩ ) ), show x ≠ y ∧ free_in_term x (term.app t₄ t₅), from ( have term.subst y v (term.app t₄ t₅) = term.app (term.subst y v t₄) (term.subst y v t₅), by unfold term.subst, have free_in_term x (term.app (term.subst y v t₄) (term.subst y v t₅)), from this ▸ x_free_in_subst, have free_in_term x (term.subst y v t₄) ∨ free_in_term x (term.subst y v t₅), from free_in_term.app.inv this, or.elim this ( assume : free_in_term x (term.subst y v t₄), have x ≠ y ∧ free_in_term x t₄, from t₄_ih this, show x ≠ y ∧ free_in_term x (term.app t₄ t₅), from ⟨this.left, free_in_term.app₁ this.right⟩ ) ( assume : free_in_term x (term.subst y v t₅), have x ≠ y ∧ free_in_term x t₅, from t₅_ih this, show x ≠ y ∧ free_in_term x (term.app t₄ t₅), from ⟨this.left, free_in_term.app₂ this.right⟩ ) ) end lemma free_of_substt_same_term {t t': term} {x: var}: free_in_term x (term.substt x t' t) → free_in_term x t' := assume x_free_in_subst: free_in_term x (term.substt x t' t), begin induction t with v' z unop t₁ t₁_ih binop t₂ t₃ t₂_ih t₃_ih t₄ t₅ t₄_ih t₅_ih, show free_in_term x t', from ( have term.substt x t' (term.value v') = v', by unfold term.substt, have free_in_term x v', from this ▸ x_free_in_subst, show free_in_term x t', from absurd this free_in_term.value.inv ), show free_in_term x t', from ( have hite: term.substt x t' (term.var z) = (if x = z then t' else z), by unfold term.substt, if x_is_z: x = z then have term.substt x t' (term.var z) = t', by { simp[x_is_z] at hite, rw[x_is_z], from hite }, show free_in_term x t', from this ▸ x_free_in_subst else have term.substt x t' (term.var z) = z, by { simp[x_is_z] at hite, from hite }, have free_in_term x z, from this ▸ x_free_in_subst, have x_iss_z: x = z, from free_in_term.var.inv this, show free_in_term x t', from absurd x_iss_z x_is_z ), show free_in_term x t', from ( have term.substt x t' (term.unop unop t₁) = term.unop unop (term.substt x t' t₁), by unfold term.substt, have free_in_term x (term.unop unop (term.substt x t' t₁)), from this ▸ x_free_in_subst, have free_in_term x (term.substt x t' t₁), from free_in_term.unop.inv this, show free_in_term x t', from t₁_ih this ), show free_in_term x t', from ( have term.substt x t' (term.binop binop t₂ t₃) = term.binop binop (term.substt x t' t₂) (term.substt x t' t₃), by unfold term.substt, have free_in_term x (term.binop binop (term.substt x t' t₂) (term.substt x t' t₃)), from this ▸ x_free_in_subst, have free_in_term x (term.substt x t' t₂) ∨ free_in_term x (term.substt x t' t₃), from free_in_term.binop.inv this, or.elim this ( assume : free_in_term x (term.substt x t' t₂), show free_in_term x t', from t₂_ih this ) ( assume : free_in_term x (term.substt x t' t₃), show free_in_term x t', from t₃_ih this ) ), show free_in_term x t', from ( have term.substt x t' (term.app t₄ t₅) = term.app (term.substt x t' t₄) (term.substt x t' t₅), by unfold term.substt, have free_in_term x (term.app (term.substt x t' t₄) (term.substt x t' t₅)), from this ▸ x_free_in_subst, have free_in_term x (term.substt x t' t₄) ∨ free_in_term x (term.substt x t' t₅), from free_in_term.app.inv this, or.elim this ( assume : free_in_term x (term.substt x t' t₄), show free_in_term x t', from t₄_ih this ) ( assume : free_in_term x (term.substt x t' t₅), show free_in_term x t', from t₅_ih this ) ) end lemma free_of_subst_env_term_step {t: term} {σ: env} {x y: var} {v: value}: free_in_term x (term.subst_env (σ[y↦v]) t) → x ≠ y ∧ free_in_term x (term.subst_env σ t) := assume x_free: free_in_term x (term.subst_env (σ[y↦v]) t), have term.subst_env (σ[y↦v]) t = term.subst y v (term.subst_env σ t), by unfold term.subst_env, have free_in_term x (term.subst y v (term.subst_env σ t)), from this ▸ x_free, show x ≠ y ∧ free_in_term x (term.subst_env σ t), from free_of_subst_term this lemma free_of_subst_env_term {t: term} {σ: env} {x: var}: free_in_term x (term.subst_env σ t) → free_in_term x t ∧ x ∉ σ := assume x_free_in_subst: free_in_term x (term.subst_env σ t), begin induction σ with σ' y v ih, show free_in_term x t ∧ x ∉ env.empty, from ( have h2: x ∉ env.empty, by begin assume : x ∈ env.empty, have h3: env.contains env.empty x, from this, cases h3 end, have term.subst_env env.empty t = t, by unfold term.subst_env, show free_in_term x t ∧ x ∉ env.empty, from ⟨this ▸ x_free_in_subst, h2⟩ ), show free_in_term x t ∧ x ∉ (σ'[y↦v]), from ( have h1: x ≠ y ∧ free_in_term x (term.subst_env σ' t), from free_of_subst_env_term_step x_free_in_subst, have h2: free_in_term x t ∧ x ∉ σ', from ih h1.right, have h3: x ∉ (σ'[y↦v]), by begin assume : x ∈ (σ'[y↦v]), have h3: env.contains (σ'[y↦v]) x, from this, cases h3, have h4: x ≠ x, from h1.left, contradiction, have h5: ¬ env.contains σ' x, from h2.right, contradiction end, show free_in_term x t ∧ x ∉ (σ'[y↦v]), from ⟨h2.left, h3⟩ ) end lemma free_of_subst_prop {P: prop} {x y: var} {v: value}: free_in_prop x (prop.subst y v P) → x ≠ y ∧ free_in_prop x P := assume x_free_in_subst: free_in_prop x (prop.subst y v P), begin induction P, case prop.term t { from ( have prop.subst y v (prop.term t) = (term.subst y v t), by unfold prop.subst, have free_in_prop x (term.subst y v t), from this ▸ x_free_in_subst, have free_in_term x (term.subst y v t), from free_in_prop.term.inv this, have x ≠ y ∧ free_in_term x t, from free_of_subst_term this, show x ≠ y ∧ free_in_prop x (prop.term t), from ⟨this.left, free_in_prop.term this.right⟩ )}, case prop.not P₁ P₁_ih { from ( have (prop.subst y v P₁.not = (prop.subst y v P₁).not), by unfold prop.subst, have free_in_prop x (prop.subst y v P₁).not, from this ▸ x_free_in_subst, have free_in_prop x (prop.subst y v P₁), from free_in_prop.not.inv this, have x ≠ y ∧ free_in_prop x P₁, from P₁_ih this, show x ≠ y ∧ free_in_prop x P₁.not, from ⟨this.left, free_in_prop.not this.right⟩ )}, case prop.and P₂ P₃ P₂_ih P₃_ih { from ( have prop.subst y v (prop.and P₂ P₃) = (prop.subst y v P₂ ⋀ prop.subst y v P₃), by unfold prop.subst, have free_in_prop x ((prop.subst y v P₂) ⋀ (prop.subst y v P₃)), from this ▸ x_free_in_subst, have free_in_prop x (prop.subst y v P₂) ∨ free_in_prop x (prop.subst y v P₃), from free_in_prop.and.inv this, or.elim this ( assume : free_in_prop x (prop.subst y v P₂), have x ≠ y ∧ free_in_prop x P₂, from P₂_ih this, show x ≠ y ∧ free_in_prop x (P₂ ⋀ P₃), from ⟨this.left, free_in_prop.and₁ this.right⟩ ) ( assume : free_in_prop x (prop.subst y v P₃), have x ≠ y ∧ free_in_prop x P₃, from P₃_ih this, show x ≠ y ∧ free_in_prop x (P₂ ⋀ P₃), from ⟨this.left, free_in_prop.and₂ this.right⟩ ) )}, case prop.or P₄ P₅ P₄_ih P₅_ih { from ( have prop.subst y v (prop.or P₄ P₅) = (prop.subst y v P₄ ⋁ prop.subst y v P₅), by unfold prop.subst, have free_in_prop x (prop.or (prop.subst y v P₄) (prop.subst y v P₅)), from this ▸ x_free_in_subst, have free_in_prop x (prop.subst y v P₄) ∨ free_in_prop x (prop.subst y v P₅), from free_in_prop.or.inv this, or.elim this ( assume : free_in_prop x (prop.subst y v P₄), have x ≠ y ∧ free_in_prop x P₄, from P₄_ih this, show x ≠ y ∧ free_in_prop x (prop.or P₄ P₅), from ⟨this.left, free_in_prop.or₁ this.right⟩ ) ( assume : free_in_prop x (prop.subst y v P₅), have x ≠ y ∧ free_in_prop x P₅, from P₅_ih this, show x ≠ y ∧ free_in_prop x (prop.or P₄ P₅), from ⟨this.left, free_in_prop.or₂ this.right⟩ ) )}, case prop.pre t₁ t₂ { from ( have prop.subst y v (prop.pre t₁ t₂) = prop.pre (term.subst y v t₁) (term.subst y v t₂), by unfold prop.subst, have free_in_prop x (prop.pre (term.subst y v t₁) (term.subst y v t₂)), from this ▸ x_free_in_subst, have free_in_term x (term.subst y v t₁) ∨ free_in_term x (term.subst y v t₂), from free_in_prop.pre.inv this, or.elim this ( assume : free_in_term x (term.subst y v t₁), have x ≠ y ∧ free_in_term x t₁, from free_of_subst_term this, show x ≠ y ∧ free_in_prop x (prop.pre t₁ t₂), from ⟨this.left, free_in_prop.pre₁ this.right⟩ ) ( assume : free_in_term x (term.subst y v t₂), have x ≠ y ∧ free_in_term x t₂, from free_of_subst_term this, show x ≠ y ∧ free_in_prop x (prop.pre t₁ t₂), from ⟨this.left, free_in_prop.pre₂ this.right⟩ ) )}, case prop.pre₁ op t { from ( have prop.subst y v (prop.pre₁ op t) = prop.pre₁ op (term.subst y v t), by unfold prop.subst, have free_in_prop x (prop.pre₁ op (term.subst y v t)), from this ▸ x_free_in_subst, have free_in_term x (term.subst y v t), from free_in_prop.pre₁.inv this, have x ≠ y ∧ free_in_term x t, from free_of_subst_term this, show x ≠ y ∧ free_in_prop x (prop.pre₁ op t), from ⟨this.left, free_in_prop.preop this.right⟩ )}, case prop.pre₂ op t₁ t₂ { from ( have prop.subst y v (prop.pre₂ op t₁ t₂) = prop.pre₂ op (term.subst y v t₁) (term.subst y v t₂), by unfold prop.subst, have free_in_prop x (prop.pre₂ op (term.subst y v t₁) (term.subst y v t₂)), from this ▸ x_free_in_subst, have free_in_term x (term.subst y v t₁) ∨ free_in_term x (term.subst y v t₂), from free_in_prop.pre₂.inv this, or.elim this ( assume : free_in_term x (term.subst y v t₁), have x ≠ y ∧ free_in_term x t₁, from free_of_subst_term this, show x ≠ y ∧ free_in_prop x (prop.pre₂ op t₁ t₂), from ⟨this.left, free_in_prop.preop₁ this.right⟩ ) ( assume : free_in_term x (term.subst y v t₂), have x ≠ y ∧ free_in_term x t₂, from free_of_subst_term this, show x ≠ y ∧ free_in_prop x (prop.pre₂ op t₁ t₂), from ⟨this.left, free_in_prop.preop₂ this.right⟩ ) )}, case prop.post t₁ t₂ { from ( have prop.subst y v (prop.post t₁ t₂) = prop.post (term.subst y v t₁) (term.subst y v t₂), by unfold prop.subst, have free_in_prop x (prop.post (term.subst y v t₁) (term.subst y v t₂)), from this ▸ x_free_in_subst, have free_in_term x (term.subst y v t₁) ∨ free_in_term x (term.subst y v t₂), from free_in_prop.post.inv this, or.elim this ( assume : free_in_term x (term.subst y v t₁), have x ≠ y ∧ free_in_term x t₁, from free_of_subst_term this, show x ≠ y ∧ free_in_prop x (prop.post t₁ t₂), from ⟨this.left, free_in_prop.post₁ this.right⟩ ) ( assume : free_in_term x (term.subst y v t₂), have x ≠ y ∧ free_in_term x t₂, from free_of_subst_term this, show x ≠ y ∧ free_in_prop x (prop.post t₁ t₂), from ⟨this.left, free_in_prop.post₂ this.right⟩ ) )}, case prop.call t { from ( have prop.subst y v (prop.call t) = prop.call (term.subst y v t), by unfold prop.subst, have free_in_prop x (prop.call (term.subst y v t)), from this ▸ x_free_in_subst, have free_in_term x (term.subst y v t), from free_in_prop.call.inv this, have x ≠ y ∧ free_in_term x t, from free_of_subst_term this, show x ≠ y ∧ free_in_prop x (prop.call t), from ⟨this.left, free_in_prop.call this.right⟩ )}, case prop.forallc z P ih { from ( have prop.subst y v (prop.forallc z P) = prop.forallc z (if y = z then P else P.subst y v), by unfold prop.subst, have free_in_prop x (prop.forallc z (if y = z then P else P.subst y v)), from this ▸ x_free_in_subst, have x_neq_z: x ≠ z, from (free_in_prop.forallc.inv this).left, have fre_ite: free_in_prop x (if y = z then P else P.subst y v), from (free_in_prop.forallc.inv this).right, if y_eq_z: y = z then ( have x_neq_y: x ≠ y, from y_eq_z.symm ▸ x_neq_z, have free_in_prop x P, by { simp[y_eq_z] at fre_ite, from fre_ite }, show x ≠ y ∧ free_in_prop x (prop.forallc z P), from ⟨x_neq_y, free_in_prop.forallc x_neq_z this⟩ ) else ( have free_in_prop x (P.subst y v), by { simp[y_eq_z] at fre_ite, from fre_ite }, have x ≠ y ∧ free_in_prop x P, from ih this, show x ≠ y ∧ free_in_prop x (prop.forallc z P), from ⟨this.left, free_in_prop.forallc x_neq_z this.right⟩ ) )}, case prop.exis z P ih { from ( have prop.subst y v (prop.exis z P) = prop.exis z (if y = z then P else P.subst y v), by unfold prop.subst, have free_in_prop x (prop.exis z (if y = z then P else P.subst y v)), from this ▸ x_free_in_subst, have x_neq_z: x ≠ z, from (free_in_prop.exis.inv this).left, have fre_ite: free_in_prop x (if y = z then P else P.subst y v), from (free_in_prop.exis.inv this).right, if y_eq_z: y = z then ( have x_neq_y: x ≠ y, from y_eq_z.symm ▸ x_neq_z, have free_in_prop x P, by { simp[y_eq_z] at fre_ite, from fre_ite }, show x ≠ y ∧ free_in_prop x (prop.exis z P), from ⟨x_neq_y, free_in_prop.exis x_neq_z this⟩ ) else ( have free_in_prop x (P.subst y v), by { simp[y_eq_z] at fre_ite, from fre_ite }, have x ≠ y ∧ free_in_prop x P, from ih this, show x ≠ y ∧ free_in_prop x (prop.exis z P), from ⟨this.left, free_in_prop.exis x_neq_z this.right⟩ ) )} end lemma free_of_subst_env_prop {P: prop} {σ: env} {x y: var} {v: value}: free_in_prop x (prop.subst_env (σ[y↦v]) P) → x ≠ y ∧ free_in_prop x (prop.subst_env σ P) := assume x_free: free_in_prop x (prop.subst_env (σ[y↦v]) P), have prop.subst_env (σ[y↦v]) P = prop.subst y v (prop.subst_env σ P), by unfold prop.subst_env, have free_in_prop x (prop.subst y v (prop.subst_env σ P)), from this ▸ x_free, show x ≠ y ∧ free_in_prop x (prop.subst_env σ P), from free_of_subst_prop this lemma free_of_subst_env {P: prop} {σ: env} {x: var}: free_in_prop x (prop.subst_env σ P) → free_in_prop x P := assume x_free_in_subst: free_in_prop x (prop.subst_env σ P), begin induction σ with σ' y v ih, show free_in_prop x P, from ( have prop.subst_env env.empty P = P, by unfold prop.subst_env, show free_in_prop x P, from this ▸ x_free_in_subst ), show free_in_prop x P, from ( have free_in_prop x (prop.subst_env σ' P), from (free_of_subst_env_prop x_free_in_subst).right, show free_in_prop x P, from ih this ) end lemma free_in_vc.subst {P: vc} {x y: var} {v: value}: free_in_vc x (vc.subst y v P) → x ≠ y ∧ free_in_vc x P := assume x_free_in_subst: free_in_vc x (vc.subst y v P), begin induction P, case vc.term t { from ( have vc.subst y v (vc.term t) = term.subst y v t, by unfold vc.subst, have free_in_vc x (vc.term (term.subst y v t)), from this ▸ x_free_in_subst, have free_in_term x (term.subst y v t), from free_in_vc.term.inv this, have x ≠ y ∧ free_in_term x t, from free_of_subst_term this, show x ≠ y ∧ free_in_vc x (vc.term t), from ⟨this.left, free_in_vc.term this.right⟩ )}, case vc.not P₁ ih { from ( have (vc.subst y v P₁.not = (vc.subst y v P₁).not), by unfold vc.subst, have free_in_vc x (vc.subst y v P₁).not, from this ▸ x_free_in_subst, have free_in_vc x (vc.subst y v P₁), from free_in_vc.not.inv this, have x ≠ y ∧ free_in_vc x P₁, from ih this, show x ≠ y ∧ free_in_vc x P₁.not, from ⟨this.left, free_in_vc.not this.right⟩ )}, case vc.and P₁ P₂ P₁_ih P₂_ih { from ( have vc.subst y v (vc.and P₁ P₂) = (vc.subst y v P₁ ⋀ vc.subst y v P₂), by unfold vc.subst, have free_in_vc x (vc.subst y v P₁ ⋀ vc.subst y v P₂), from this ▸ x_free_in_subst, have free_in_vc x (vc.subst y v P₁) ∨ free_in_vc x (vc.subst y v P₂), from free_in_vc.and.inv this, or.elim this ( assume : free_in_vc x (vc.subst y v P₁), have x ≠ y ∧ free_in_vc x P₁, from P₁_ih this, show x ≠ y ∧ free_in_vc x (P₁ ⋀ P₂), from ⟨this.left, free_in_vc.and₁ this.right⟩ ) ( assume : free_in_vc x (vc.subst y v P₂), have x ≠ y ∧ free_in_vc x P₂, from P₂_ih this, show x ≠ y ∧ free_in_vc x (P₁ ⋀ P₂), from ⟨this.left, free_in_vc.and₂ this.right⟩ ) )}, case vc.or P₁ P₂ P₁_ih P₂_ih { from ( have vc.subst y v (vc.or P₁ P₂) = (vc.subst y v P₁ ⋁ vc.subst y v P₂), by unfold vc.subst, have free_in_vc x (vc.or (vc.subst y v P₁) (vc.subst y v P₂)), from this ▸ x_free_in_subst, have free_in_vc x (vc.subst y v P₁) ∨ free_in_vc x (vc.subst y v P₂), from free_in_vc.or.inv this, or.elim this ( assume : free_in_vc x (vc.subst y v P₁), have x ≠ y ∧ free_in_vc x P₁, from P₁_ih this, show x ≠ y ∧ free_in_vc x (vc.or P₁ P₂), from ⟨this.left, free_in_vc.or₁ this.right⟩ ) ( assume : free_in_vc x (vc.subst y v P₂), have x ≠ y ∧ free_in_vc x P₂, from P₂_ih this, show x ≠ y ∧ free_in_vc x (vc.or P₁ P₂), from ⟨this.left, free_in_vc.or₂ this.right⟩ ) )}, case vc.pre t₁ t₂ { from ( have vc.subst y v (vc.pre t₁ t₂) = vc.pre (term.subst y v t₁) (term.subst y v t₂), by unfold vc.subst, have free_in_vc x (vc.pre (term.subst y v t₁) (term.subst y v t₂)), from this ▸ x_free_in_subst, have free_in_term x (term.subst y v t₁) ∨ free_in_term x (term.subst y v t₂), from free_in_vc.pre.inv this, or.elim this ( assume : free_in_term x (term.subst y v t₁), have x ≠ y ∧ free_in_term x t₁, from free_of_subst_term this, show x ≠ y ∧ free_in_vc x (vc.pre t₁ t₂), from ⟨this.left, free_in_vc.pre₁ this.right⟩ ) ( assume : free_in_term x (term.subst y v t₂), have x ≠ y ∧ free_in_term x t₂, from free_of_subst_term this, show x ≠ y ∧ free_in_vc x (vc.pre t₁ t₂), from ⟨this.left, free_in_vc.pre₂ this.right⟩ ) )}, case vc.pre₁ op t { from ( have vc.subst y v (vc.pre₁ op t) = vc.pre₁ op (term.subst y v t), by unfold vc.subst, have free_in_vc x (vc.pre₁ op (term.subst y v t)), from this ▸ x_free_in_subst, have free_in_term x (term.subst y v t), from free_in_vc.pre₁.inv this, have x ≠ y ∧ free_in_term x t, from free_of_subst_term this, show x ≠ y ∧ free_in_vc x (vc.pre₁ op t), from ⟨this.left, free_in_vc.preop this.right⟩ )}, case vc.pre₂ op t₁ t₂ { from ( have vc.subst y v (vc.pre₂ op t₁ t₂) = vc.pre₂ op (term.subst y v t₁) (term.subst y v t₂), by unfold vc.subst, have free_in_vc x (vc.pre₂ op (term.subst y v t₁) (term.subst y v t₂)), from this ▸ x_free_in_subst, have free_in_term x (term.subst y v t₁) ∨ free_in_term x (term.subst y v t₂), from free_in_vc.pre₂.inv this, or.elim this ( assume : free_in_term x (term.subst y v t₁), have x ≠ y ∧ free_in_term x t₁, from free_of_subst_term this, show x ≠ y ∧ free_in_vc x (vc.pre₂ op t₁ t₂), from ⟨this.left, free_in_vc.preop₁ this.right⟩ ) ( assume : free_in_term x (term.subst y v t₂), have x ≠ y ∧ free_in_term x t₂, from free_of_subst_term this, show x ≠ y ∧ free_in_vc x (vc.pre₂ op t₁ t₂), from ⟨this.left, free_in_vc.preop₂ this.right⟩ ) )}, case vc.post t₁ t₂ { from ( have vc.subst y v (vc.post t₁ t₂) = vc.post (term.subst y v t₁) (term.subst y v t₂), by unfold vc.subst, have free_in_vc x (vc.post (term.subst y v t₁) (term.subst y v t₂)), from this ▸ x_free_in_subst, have free_in_term x (term.subst y v t₁) ∨ free_in_term x (term.subst y v t₂), from free_in_vc.post.inv this, or.elim this ( assume : free_in_term x (term.subst y v t₁), have x ≠ y ∧ free_in_term x t₁, from free_of_subst_term this, show x ≠ y ∧ free_in_vc x (vc.post t₁ t₂), from ⟨this.left, free_in_vc.post₁ this.right⟩ ) ( assume : free_in_term x (term.subst y v t₂), have x ≠ y ∧ free_in_term x t₂, from free_of_subst_term this, show x ≠ y ∧ free_in_vc x (vc.post t₁ t₂), from ⟨this.left, free_in_vc.post₂ this.right⟩ ) )}, case vc.univ z P' P'_ih { from ( have h: vc.subst y v (vc.univ z P') = vc.univ z (if y = z then P' else vc.subst y v P'), by unfold vc.subst, if y_eq_z: y = z then ( have (if y = z then P' else vc.subst y v P') = P', by simp[y_eq_z], have vc.subst y v (vc.univ z P') = vc.univ z P', from @eq.subst vc (λa, vc.subst y v (vc.univ z P') = vc.univ z a) (if y = z then P' else vc.subst y v P') P' this h, have h2: free_in_vc x (vc.univ z P'), from @eq.subst vc (λa, free_in_vc x a) (vc.subst y v (vc.univ z P')) (vc.univ z P') this x_free_in_subst, have x ≠ y, from ( assume : x = y, have x = z, from eq.trans this y_eq_z, have free_in_vc x (vc.univ x P'), from @eq.subst var (λa, free_in_vc x (vc.univ a P')) z x this.symm h2, show «false», from (free_in_vc.univ.same.inv) this ), show x ≠ y ∧ free_in_vc x (vc.univ z P'), from ⟨this, h2⟩ ) else ( have (if y = z then P' else vc.subst y v P') = vc.subst y v P', by simp[y_eq_z], have vc.subst y v (vc.univ z P') = vc.univ z (vc.subst y v P'), from @eq.subst vc (λa, vc.subst y v (vc.univ z P') = vc.univ z a) (if y = z then P' else vc.subst y v P') (vc.subst y v P') this h, have free_in_vc x (vc.univ z (vc.subst y v P')), from this ▸ x_free_in_subst, have h2: x ≠ z ∧ free_in_vc x (vc.subst y v P'), from free_in_vc.univ.inv this, have x ≠ y ∧ free_in_vc x P', from P'_ih h2.right, show x ≠ y ∧ free_in_vc x (vc.univ z P'), from ⟨this.left, free_in_vc.univ h2.left this.right⟩ ) )} end lemma free_in_vc.subst2 {P: vc} {σ: env} {x y: var} {v: value}: free_in_vc x (vc.subst_env (σ[y↦v]) P) → x ≠ y ∧ free_in_vc x (vc.subst_env σ P) := assume x_free: free_in_vc x (vc.subst_env (σ[y↦v]) P), have vc.subst_env (σ[y↦v]) P = vc.subst y v (vc.subst_env σ P), by unfold vc.subst_env, have free_in_vc x (vc.subst y v (vc.subst_env σ P)), from this ▸ x_free, show x ≠ y ∧ free_in_vc x (vc.subst_env σ P), from free_in_vc.subst this lemma free_in_vc.subst_env {P: vc} {σ: env} {x: var}: free_in_vc x (vc.subst_env σ P) → free_in_vc x P := assume x_free_in_subst: free_in_vc x (vc.subst_env σ P), begin induction σ with σ' y v ih, show free_in_vc x P, from ( have vc.subst_env env.empty P = P, by unfold vc.subst_env, show free_in_vc x P, from this ▸ x_free_in_subst ), show free_in_vc x P, from ( have free_in_vc x (vc.subst_env σ' P), from (free_in_vc.subst2 x_free_in_subst).right, show free_in_vc x P, from ih this ) end lemma term.subst_env.var.inv {x: var} {σ: env}: (term.subst_env σ x = x) ∨ (∃v:value, term.subst_env σ x = v) := begin induction σ with σ' y v' ih, show (term.subst_env env.empty x = x) ∨ (∃v:value, term.subst_env env.empty x = v), from ( have (term.subst_env env.empty x = x), by unfold term.subst_env, show (term.subst_env env.empty x = x) ∨ (∃v:value, term.subst_env env.empty x = v), from or.inl this ), show (term.subst_env (σ'[y↦v']) x = x) ∨ (∃v:value, term.subst_env (σ'[y↦v']) x = v), from ( have tsubst: (term.subst_env (σ'[y↦v']) x = term.subst y v' (term.subst_env σ' x)), by unfold term.subst_env, have (term.subst_env σ' ↑x = ↑x ∨ ∃ (v : value), term.subst_env σ' ↑x = ↑v), from ih, or.elim this ( assume σ'_x_is_x: term.subst_env σ' ↑x = ↑x, have h: (term.subst_env (σ'[y↦v']) x = term.subst y v' ↑x), from @eq.subst term (λa, term.subst_env (σ'[y↦v']) x = term.subst y v' a) (term.subst_env σ' x) x σ'_x_is_x tsubst, have h2: term.subst y v' (term.var x) = (if y = x then v' else x), by unfold term.subst, decidable.by_cases ( assume x_is_y: x = y, have term.subst y v' (term.var x) = v', by { rw[x_is_y], simp[x_is_y] at h2, from h2 }, have term.subst_env (σ'[y↦v']) x = v', from eq.trans h this, have (∃v:value, term.subst_env (σ'[y↦v']) x = v), from exists.intro v' this, show (term.subst_env (σ'[y↦v']) x = x) ∨ (∃v:value, term.subst_env (σ'[y↦v']) x = v), from or.inr this ) ( assume : ¬(x = y), have ¬(y = x), from ne.symm this, have term.subst y v' (term.var x) = x, by { simp[this] at h2, from h2 }, have term.subst_env (σ'[y↦v']) x = x, from eq.trans h this, show (term.subst_env (σ'[y↦v']) x = x) ∨ (∃v:value, term.subst_env (σ'[y↦v']) x = v), from or.inl this ) ) ( assume : ∃ (v : value), term.subst_env σ' ↑x = ↑v, let ⟨v, σ'_x_is_v⟩ := this in have h: (term.subst_env (σ'[y↦v']) x = term.subst y v' ↑v), from @eq.subst term (λa, term.subst_env (σ'[y↦v']) x = term.subst y v' a) (term.subst_env σ' x) v σ'_x_is_v tsubst, have term.subst y v' (term.value v) = ↑v, by unfold term.subst, have term.subst_env (σ'[y↦v']) x = v, from eq.trans h this, have (∃v:value, term.subst_env (σ'[y↦v']) x = v), from exists.intro v this, show (term.subst_env (σ'[y↦v']) x = x) ∨ (∃v:value, term.subst_env (σ'[y↦v']) x = v), from or.inr this ) ) end lemma term.subst_env.value {σ: env} {v: value}: term.subst_env σ v = v := begin induction σ with σ' x v' ih, show (term.subst_env env.empty v = v), by unfold term.subst_env, show (term.subst_env (σ'[x↦v']) v = v), from ( have h: term.subst_env σ' v = v, from ih, have term.subst_env (σ'[x↦v']) v = term.subst x v' (term.subst_env σ' v), by unfold term.subst_env, have h2: term.subst_env (σ'[x↦v']) v = term.subst x v' v, from @eq.subst term (λa, term.subst_env (σ'[x↦v']) v = term.subst x v' a) (term.subst_env σ' v) v h this, have term.subst x v' (term.value v) = ↑v, by unfold term.subst, show term.subst_env (σ'[x↦v']) v = v, from eq.trans h2 this ) end lemma term.subst_env.closed {σ: env} {t: term}: closed t → (term.subst_env σ t = t) := begin assume t_closed: closed t, induction σ with σ' x v' ih, show (term.subst_env env.empty t = t), by unfold term.subst_env, show (term.subst_env (σ'[x↦v']) t = t), from ( have h: term.subst_env σ' t = t, from ih, have term.subst_env (σ'[x↦v']) t = term.subst x v' (term.subst_env σ' t), by unfold term.subst_env, have h2: term.subst_env (σ'[x↦v']) t = term.subst x v' t, from @eq.subst term (λa, term.subst_env (σ'[x↦v']) t = term.subst x v' a) (term.subst_env σ' t) t h this, have term.subst x v' t = t, from unchanged_of_subst_nonfree_term (t_closed x), show term.subst_env (σ'[x↦v']) t = t, from eq.trans h2 this ) end lemma term.subst_env.var {σ: env} {x: var}: ((σ x = none) ↔ (term.subst_env σ x = x)) ∧ (∀v, (σ x = some v) ↔ (term.subst_env σ x = v)) := begin induction σ with σ' y v' ih, show (((env.empty x = none) ↔ (term.subst_env env.empty x = x)) ∧ (∀v, (env.empty x = some v) ↔ (term.subst_env env.empty x = v))), by begin split, show ((env.empty x = none) ↔ (term.subst_env env.empty x = x)), by begin split, show ((env.empty x = none) → (term.subst_env env.empty x = x)), by begin assume _, show (term.subst_env env.empty x = x), by unfold term.subst_env end, show ((term.subst_env env.empty x = x) → (env.empty x = none)), by begin assume _, show (env.apply env.empty x = none), by unfold env.apply end end, show ∀v, ((env.empty x = some v) ↔ (term.subst_env env.empty x = v)), by begin assume v, split, show ((env.empty x = some v) → (term.subst_env env.empty x = v)), by begin assume env_has_some: (env.apply (env.empty) x = some v), have env_has_none: (env.apply env.empty x = none), by unfold env.apply, have : (some v = none), from env_has_some ▸ env_has_none, contradiction end, show ((term.subst_env env.empty x = v) → (env.empty x = some v)), by begin assume subst_is_v: (term.subst_env env.empty x = v), have : (term.subst_env env.empty x = x), by unfold term.subst_env, have : (↑v = ↑x), from eq.trans subst_is_v.symm this, contradiction end end end, show ((((σ'[y↦v']) x = none) ↔ (term.subst_env (σ'[y↦v']) x = x)) ∧ (∀v, ((σ'[y↦v']) x = some v) ↔ (term.subst_env (σ'[y↦v']) x = v))), by begin have tsubst: (term.subst_env (σ'[y↦v']) x = term.subst y v' (term.subst_env σ' x)), by unfold term.subst_env, have app: ((σ'[y↦v']).apply x = (if y = x ∧ option.is_none (σ'.apply x) then v' else σ'.apply x)), by unfold env.apply, split, show (((σ'[y↦v']) x = none) ↔ (term.subst_env (σ'[y↦v']) x = x)), by begin split, show (((σ'[y↦v']) x = none) → (term.subst_env (σ'[y↦v']) x = x)), by begin assume σ'_does_not_have_x: ((σ'[y↦v']) x = none), by_cases (y = x ∧ option.is_none (σ'.apply x)) with h, show (term.subst_env (σ'[y↦v']) x = x), from have ((σ'[y↦v']).apply x) = v', by { simp[h] at app, rw[h.left], from app }, have some v' = none, from eq.trans this.symm σ'_does_not_have_x, by contradiction, show (term.subst_env (σ'[y↦v']) x = x), from have ((σ'[y↦v']).apply x) = σ'.apply x, by { simp[h] at app, from app }, have σ'_x_is_none: σ'.apply x = none, from eq.trans this.symm σ'_does_not_have_x, have term.subst_env σ' x = x, from ih.left.mp σ'_x_is_none, have h2: term.subst_env (σ'[y↦v']) x = term.subst y v' x, from @eq.subst term (λa, term.subst_env (σ'[y↦v']) x = term.subst y v' a) (term.subst_env σ' x) x this tsubst, have h3: term.subst y v' (term.var x) = (if y = x then v' else x), by unfold term.subst, have ¬(y = x) ∨ ¬(option.is_none (env.apply σ' x)) , from not_and_distrib.mp h, have ¬(y = x), from this.elim id ( assume : ¬(option.is_none (env.apply σ' x)), have (env.apply σ' x) ≠ none, from option.is_none.ninv.mpr this, show ¬(y = x), from absurd σ'_x_is_none this ), have term.subst y v' (term.var x) = x, by { simp[this] at h3, from h3 }, show (term.subst_env (σ'[y↦v']) x = x), from eq.trans h2 this end, show ((term.subst_env (σ'[y↦v']) x = x) → ((σ'[y↦v']) x = none)), from ( assume h: term.subst_env (σ'[y↦v']) x = x, have h2: term.subst y v' (term.subst_env σ' x) = x, from eq.trans tsubst.symm h, have (term.subst_env σ' x = x) ∨ (∃v:value, term.subst_env σ' x = v), from term.subst_env.var.inv, or.elim this ( assume : term.subst_env σ' x = x, have σ'_x_is_none: σ' x = none, from ih.left.mpr this, have h3: term.subst_env (σ'[y↦v']) x = term.subst y v' x, from @eq.subst term (λa, term.subst_env (σ'[y↦v']) x = term.subst y v' a) (term.subst_env σ' x) x this tsubst, have h4: term.subst y v' (term.var x) = (if y = x then v' else x), by unfold term.subst, decidable.by_cases ( assume : x = y, have y_eq_x: y = x, from eq.symm this, have term.subst y v' (term.var x) = v', by { simp[y_eq_x] at h4, rw[y_eq_x], from h4 }, have term.subst_env (σ'[y↦v']) x = v', from eq.trans h3 this, have ↑x = ↑v', from eq.trans h.symm this, show (σ'[y↦v']) x = none, by contradiction ) ( assume : ¬(x = y), have y_neq_x: ¬(y = x), from ne.symm this, have (σ'[y↦v']).apply x = σ'.apply x, by { simp[y_neq_x] at app, from app }, show (σ'[y↦v']).apply x = none, from eq.trans this σ'_x_is_none ) ) ( assume : (∃v'':value, term.subst_env σ' x = v''), let ⟨v'', σ'_x_is_v''⟩ := this in have h3: (term.subst_env (σ'[y↦v']) x = term.subst y v' ↑v''), from @eq.subst term (λa, term.subst_env (σ'[y↦v']) x = term.subst y v' a) (term.subst_env σ' x) v'' σ'_x_is_v'' tsubst, have term.subst y v' (term.value v'') = ↑v'', by unfold term.subst, have term.subst_env (σ'[y↦v']) x = ↑v'', from eq.trans h3 this, have ↑x = ↑v'', from eq.trans h.symm this, show (σ'[y↦v']) x = none, by contradiction ) ) end, show (∀v, ((σ'[y↦v']) x = some v) ↔ (term.subst_env (σ'[y↦v']) x = v)), by begin assume v, split, show (((σ'[y↦v']) x = some v) → (term.subst_env (σ'[y↦v']) x = v)), by begin assume env_has_x: ((σ'[y↦v']) x = some v), have app: ((σ'[y↦v']).apply x = (if y = x ∧ option.is_none (σ'.apply x) then v' else σ'.apply x)), by unfold env.apply, by_cases (y = x ∧ option.is_none (σ'.apply x)) with h, show (term.subst_env (σ'[y↦v']) ↑x = ↑v), from ( have ((σ'[y↦v']).apply x = v'), by { simp[h] at app, rw[h.left], from app }, have some v' = some v, from eq.trans this.symm env_has_x, have v'_is_v: v' = v, by injection this, have option.is_none (σ'.apply x), from h.right, have σ'.apply x = none, from option.is_none.inv.mpr this, have σ'_x_is_x: term.subst_env σ' x = x, from ih.left.mp this, have term.subst_env (σ'[y↦v']) x = term.subst y v' (term.subst_env σ' x), by unfold term.subst_env, have h2: term.subst_env (σ'[y↦v']) x = term.subst y v' (term.var x), from @eq.subst term (λa, term.subst_env (σ'[y↦v']) x = term.subst y v' a) (term.subst_env σ' x) x σ'_x_is_x tsubst, have h3: term.subst y v' (term.var x) = (if y = x then v' else x), by unfold term.subst, have term.subst y v' (term.var x) = v', by { simp[h.left] at h3, rw[h.left], from h3 }, show term.subst_env (σ'[y↦v']) x = v, from v'_is_v ▸ eq.trans h2 this ), show (term.subst_env (σ'[y↦v']) ↑x = ↑v), from ( have (σ'[y↦v']).apply x = σ'.apply x, by { simp [h] at app, from app }, have σ'.apply x = v, from eq.trans this.symm env_has_x, have σ'_x_is_v: term.subst_env σ' ↑x = ↑v, from (ih.right v).mp this, have term.subst_env (σ'[y↦v']) x = term.subst y v' (term.subst_env σ' x), by unfold term.subst_env, have h2: term.subst_env (σ'[y↦v']) x = term.subst y v' v, from @eq.subst term (λa, term.subst_env (σ'[y↦v']) x = term.subst y v' a) (term.subst_env σ' x) v σ'_x_is_v tsubst, have term.subst y v' (term.value v) = ↑v, by unfold term.subst, show term.subst_env (σ'[y↦v']) x = v, from eq.trans h2 this ) end, show ((term.subst_env (σ'[y↦v']) x = v) → ((σ'[y↦v']) x = some v)), from ( assume h: term.subst_env (σ'[y↦v']) x = v, have h2: term.subst y v' (term.subst_env σ' x) = v, from eq.trans tsubst.symm h, have (term.subst_env σ' x = x) ∨ (∃v:value, term.subst_env σ' x = v), from term.subst_env.var.inv, or.elim this ( assume : term.subst_env σ' x = x, have σ'_x_is_none: σ' x = none, from ih.left.mpr this, have h3: term.subst_env (σ'[y↦v']) x = term.subst y v' x, from @eq.subst term (λa, term.subst_env (σ'[y↦v']) x = term.subst y v' a) (term.subst_env σ' x) x this tsubst, have h4: term.subst y v' (term.var x) = (if y = x then v' else x), by unfold term.subst, decidable.by_cases ( assume x_is_y: x = y, have term.subst y v' (term.var x) = (if x = x then v' else x), from @eq.subst var (λa, term.subst y v' (term.var x) = (if a = x then v' else x)) y x x_is_y.symm h4, have term.subst y v' (term.var x) = v', by { simp at this, from this }, have term.subst_env (σ'[y↦v']) x = v', from eq.trans h3 this, have ↑v = ↑v', from eq.trans h.symm this, have v_is_v': v = v', by injection this, have opt_is_none: option.is_none (env.apply σ' x), from option.is_none.inv.mp σ'_x_is_none, have (if y = x ∧ option.is_none (σ'.apply x) then ↑v' else σ'.apply x) = v', by { simp[x_is_y.symm], simp[opt_is_none] }, have (σ'[y↦v']).apply x = v', from eq.trans app this, have (σ'[y↦v']) x = some v', from this, show (σ'[y↦v']) x = some v, from @eq.subst value (λa, (σ'[y↦v']) x = some a) v' v v_is_v'.symm this ) ( assume : ¬(x = y), have ¬(y = x), from ne.symm this, have term.subst y v' (term.var x) = x, by { simp[this] at h4, from h4 }, have ↑v = ↑x, from eq.trans (eq.trans h.symm h3) this, show ((σ'[y↦v']) x = some v), by contradiction ) ) ( assume : (∃v'':value, term.subst_env σ' x = v''), let ⟨v'', σ'_x_is_v''⟩ := this in have h3: (term.subst_env (σ'[y↦v']) x = term.subst y v' ↑v''), from @eq.subst term (λa, term.subst_env (σ'[y↦v']) x = term.subst y v' a) (term.subst_env σ' x) v'' σ'_x_is_v'' tsubst, have term.subst y v' (term.value v'') = ↑v'', by unfold term.subst, have term.subst_env (σ'[y↦v']) x = ↑v'', from eq.trans h3 this, have v_is_v'': ↑v = ↑v'', from eq.trans h.symm this, have term.subst_env σ' x = v, from @eq.subst term (λa, term.subst_env σ' x = a) v'' v v_is_v''.symm σ'_x_is_v'', have σ'_x_app_is_v: env.apply σ' x = some v, from (ih.right v).mpr this, have opt_is_not_none: ¬ option.is_none (env.apply σ' x), from option.some_iff_not_none.mp (option.is_some_iff_exists.mpr (exists.intro v σ'_x_app_is_v)), have (if y = x ∧ option.is_none (σ'.apply x) then ↑v' else σ'.apply x) = σ'.apply x, by { simp[opt_is_not_none] }, have (σ'[y↦v']) x = σ'.apply x, from eq.trans app this, show (σ'[y↦v']) x = some v, from eq.trans this σ'_x_app_is_v ) ) end end end lemma term.not_free_of_subst {x: var} {v: value} {t: term}: x ∉ FV (term.subst x v t) := assume x_free: x ∈ FV (term.subst x v t), begin induction t with a, show «false», by begin -- term.value unfold term.subst at x_free, cases x_free end, show «false», by begin -- term.var unfold term.subst at x_free, by_cases (x = a) with h, simp[h] at x_free, cases x_free, simp[h] at x_free, cases x_free, contradiction end, show «false», by begin -- term.unop unfold term.subst at x_free, have h, from free_in_term.unop.inv x_free, contradiction end, show «false», by begin -- term.binop unfold term.subst at x_free, have h, from free_in_term.binop.inv x_free, cases h with h1 h2, contradiction, contradiction end, show «false», by begin -- term.app unfold term.subst at x_free, have h, from free_in_term.app.inv x_free, cases h with h1 h2, contradiction, contradiction end end lemma term.not_free_of_substt {x: var} {t₁ t₂: term}: closed t₁ → x ∉ FV (term.substt x t₁ t₂) := assume t_closed: closed t₁, assume x_free: x ∈ FV (term.substt x t₁ t₂), begin induction t₂ with a, show «false», by begin -- term.value unfold term.substt at x_free, cases x_free end, show «false», by begin -- term.var unfold term.substt at x_free, by_cases (x = a) with h, simp[h] at x_free, have : a ∉ FV t₁, from t_closed a, contradiction, simp[h] at x_free, cases x_free, contradiction end, show «false», by begin -- term.unop unfold term.substt at x_free, have h, from free_in_term.unop.inv x_free, contradiction end, show «false», by begin -- term.binop unfold term.substt at x_free, have h, from free_in_term.binop.inv x_free, cases h with h1 h2, contradiction, contradiction end, show «false», by begin -- term.app unfold term.substt at x_free, have h, from free_in_term.app.inv x_free, cases h with h1 h2, contradiction, contradiction end end lemma term.not_free_of_subst_env {x: var} {σ: env} {t: term}: x ∈ σ → x ∉ FV (term.subst_env σ t) := assume x_in_σ: x ∈ σ, assume x_free: x ∈ FV (term.subst_env σ t), begin induction σ with σ' y v ih, -- env.empty show «false», by cases x_in_σ, -- σ'[x↦v] show «false», from ( have term.subst_env (σ'[y↦v]) t = term.subst y v (term.subst_env σ' t), by unfold term.subst_env, have x ∈ FV (term.subst y v (term.subst_env σ' t)), from this ▸ x_free, have x_neq_y: x ≠ y, from (free_of_subst_term this).left, have h: x ∈ FV (term.subst_env σ' t), from (free_of_subst_term this).right, have x = y ∨ x ∈ σ', from env.contains.inv x_in_σ, or.elim this ( assume : x = y, show «false», from x_neq_y this ) ( assume : x ∈ σ', have x ∉ FV (term.subst_env σ' t), from ih this, show «false», from this h ) ) end lemma prop.not_free_of_subst {x: var} {v: value} {P: prop}: x ∉ FV (prop.subst x v P) := assume x_free: x ∈ FV (prop.subst x v P), begin induction P, case prop.term t { from ( have prop.subst x v (prop.term t) = (term.subst x v t), by unfold prop.subst, have free_in_prop x (prop.term (term.subst x v t)), from this ▸ x_free, have x ∈ FV (term.subst x v t), from free_in_prop.term.inv this, show «false», from term.not_free_of_subst this )}, case prop.not P₁ P₁_ih { from ( have prop.subst x v (prop.not P₁) = (P₁.subst x v).not, by unfold prop.subst, have x ∈ FV (P₁.subst x v).not, from this ▸ x_free, have x ∈ FV (P₁.subst x v), from free_in_prop.not.inv this, show «false», from P₁_ih this )}, case prop.and P₁ P₂ P₁_ih P₂_ih { from ( have prop.subst x v (prop.and P₁ P₂) = (P₁.subst x v ⋀ P₂.subst x v), by unfold prop.subst, have x ∈ FV (P₁.subst x v ⋀ P₂.subst x v), from this ▸ x_free, or.elim (free_in_prop.and.inv this) ( assume : x ∈ FV (P₁.subst x v), show «false», from P₁_ih this ) ( assume : x ∈ FV (P₂.subst x v), show «false», from P₂_ih this ) )}, case prop.or P₁ P₂ P₁_ih P₂_ih { from ( have prop.subst x v (prop.or P₁ P₂) = (P₁.subst x v ⋁ P₂.subst x v), by unfold prop.subst, have x ∈ FV (P₁.subst x v ⋁ P₂.subst x v), from this ▸ x_free, or.elim (free_in_prop.or.inv this) ( assume : x ∈ FV (P₁.subst x v), show «false», from P₁_ih this ) ( assume : x ∈ FV (P₂.subst x v), show «false», from P₂_ih this ) )}, case prop.pre t₁ t₂ { from ( have prop.subst x v (prop.pre t₁ t₂) = prop.pre (t₁.subst x v) (t₂.subst x v), by unfold prop.subst, have x ∈ FV (prop.pre (t₁.subst x v) (t₂.subst x v)), from this ▸ x_free, or.elim (free_in_prop.pre.inv this) ( assume : x ∈ FV (t₁.subst x v), show «false», from term.not_free_of_subst this ) ( assume : x ∈ FV (t₂.subst x v), show «false», from term.not_free_of_subst this ) )}, case prop.pre₁ op t { from ( have prop.subst x v (prop.pre₁ op t) = prop.pre₁ op (t.subst x v), by unfold prop.subst, have x ∈ FV (prop.pre₁ op (t.subst x v)), from this ▸ x_free, have x ∈ FV (t.subst x v), from free_in_prop.pre₁.inv this, show «false», from term.not_free_of_subst this )}, case prop.pre₂ op t₁ t₂ { from ( have prop.subst x v (prop.pre₂ op t₁ t₂) = prop.pre₂ op (t₁.subst x v) (t₂.subst x v), by unfold prop.subst, have x ∈ FV (prop.pre₂ op (t₁.subst x v) (t₂.subst x v)), from this ▸ x_free, or.elim (free_in_prop.pre₂.inv this) ( assume : x ∈ FV (t₁.subst x v), show «false», from term.not_free_of_subst this ) ( assume : x ∈ FV (t₂.subst x v), show «false», from term.not_free_of_subst this ) )}, case prop.post t₁ t₂ { from ( have prop.subst x v (prop.post t₁ t₂) = prop.post (t₁.subst x v) (t₂.subst x v), by unfold prop.subst, have x ∈ FV (prop.post (t₁.subst x v) (t₂.subst x v)), from this ▸ x_free, or.elim (free_in_prop.post.inv this) ( assume : x ∈ FV (t₁.subst x v), show «false», from term.not_free_of_subst this ) ( assume : x ∈ FV (t₂.subst x v), show «false», from term.not_free_of_subst this ) )}, case prop.call t { from ( have prop.subst x v (prop.call t) = prop.call (t.subst x v), by unfold prop.subst, have x ∈ FV (prop.call (t.subst x v)), from this ▸ x_free, have x ∈ FV (t.subst x v), from free_in_prop.call.inv this, show «false», from term.not_free_of_subst this )}, case prop.forallc y P₁ P₁_ih { from ( have prop.subst x v (prop.forallc y P₁) = prop.forallc y (if x = y then P₁ else P₁.subst x v), by unfold prop.subst, have x ∈ FV (prop.forallc y (if x = y then P₁ else P₁.subst x v)), from this ▸ x_free, have y_neq_x: x ≠ y, from (free_in_prop.forallc.inv this).left, have x ∈ FV (prop.forallc y (P₁.subst x v)), by { simp[y_neq_x] at this, from this }, have x ∈ FV (P₁.subst x v), from (free_in_prop.forallc.inv this).right, show «false», from P₁_ih this )}, case prop.exis y P₁ P₁_ih { from ( have prop.subst x v (prop.exis y P₁) = prop.exis y (if x = y then P₁ else P₁.subst x v), by unfold prop.subst, have x ∈ FV (prop.exis y (if x = y then P₁ else P₁.subst x v)), from this ▸ x_free, have y_neq_x: x ≠ y, from (free_in_prop.exis.inv this).left, have x ∈ FV (prop.exis y (P₁.subst x v)), by { simp[y_neq_x] at this, from this }, have x ∈ FV (P₁.subst x v), from (free_in_prop.exis.inv this).right, show «false», from P₁_ih this )} end lemma prop.not_free_of_subst_env {x: var} {σ: env} {P: prop}: x ∈ σ → x ∉ FV (prop.subst_env σ P) := assume x_in_σ: x ∈ σ, assume x_free: x ∈ FV (prop.subst_env σ P), begin induction σ with σ' y v ih, -- env.empty show «false», by cases x_in_σ, -- σ'[x↦v] show «false», from ( have prop.subst_env (σ'[y↦v]) P = prop.subst y v (prop.subst_env σ' P), by unfold prop.subst_env, have x ∈ FV (prop.subst y v (prop.subst_env σ' P)), from this ▸ x_free, have x_neq_y: x ≠ y, from (free_of_subst_prop this).left, have h: x ∈ FV (prop.subst_env σ' P), from (free_of_subst_prop this).right, have x = y ∨ x ∈ σ', from env.contains.inv x_in_σ, or.elim this ( assume : x = y, show «false», from x_neq_y this ) ( assume : x ∈ σ', have x ∉ FV (prop.subst_env σ' P), from ih this, show «false», from this h ) ) end lemma term.closed_of_closed_subst {σ: env} {t: term}: closed_subst σ t → closed (term.subst_env σ t) := assume t_closed_subst: closed_subst σ t, show closed (term.subst_env σ t), from ( assume x: var, assume h1: x ∈ FV (term.subst_env σ t), have x ∈ FV t, from (free_of_subst_env_term h1).left, have x ∈ σ.dom, from t_closed_subst this, have x ∈ σ, from this, have h2: x ∉ FV (term.subst_env σ t), from term.not_free_of_subst_env this, show «false», from h2 h1 ) lemma prop.closed_of_closed_subst {σ: env} {P: prop}: closed_subst σ P → closed (prop.subst_env σ P) := assume P_closed_subst: closed_subst σ P, show closed (prop.subst_env σ P), from ( assume x: var, assume h1: x ∈ FV (prop.subst_env σ P), have x ∈ FV P, from free_of_subst_env h1, have x ∈ σ.dom, from P_closed_subst this, have x ∈ σ, from this, have h2: x ∉ FV (prop.subst_env σ P), from prop.not_free_of_subst_env this, show «false», from h2 h1 ) lemma vc.not_free_of_subst {x: var} {v: value} {P: vc}: x ∉ FV (vc.subst x v P) := assume x_free: x ∈ FV (vc.subst x v P), begin induction P, case vc.term t { from ( have vc.subst x v (vc.term t) = (term.subst x v t), by unfold vc.subst, have free_in_vc x (vc.term (term.subst x v t)), from this ▸ x_free, have x ∈ FV (term.subst x v t), from free_in_vc.term.inv this, show «false», from term.not_free_of_subst this )}, case vc.not P₁ P₁_ih { from ( have vc.subst x v (vc.not P₁) = (P₁.subst x v).not, by unfold vc.subst, have x ∈ FV (P₁.subst x v).not, from this ▸ x_free, have x ∈ FV (P₁.subst x v), from free_in_vc.not.inv this, show «false», from P₁_ih this )}, case vc.and P₁ P₂ P₁_ih P₂_ih { from ( have vc.subst x v (vc.and P₁ P₂) = (P₁.subst x v ⋀ P₂.subst x v), by unfold vc.subst, have x ∈ FV (P₁.subst x v ⋀ P₂.subst x v), from this ▸ x_free, or.elim (free_in_vc.and.inv this) ( assume : x ∈ FV (P₁.subst x v), show «false», from P₁_ih this ) ( assume : x ∈ FV (P₂.subst x v), show «false», from P₂_ih this ) )}, case vc.or P₁ P₂ P₁_ih P₂_ih { from ( have vc.subst x v (vc.or P₁ P₂) = (P₁.subst x v ⋁ P₂.subst x v), by unfold vc.subst, have x ∈ FV (P₁.subst x v ⋁ P₂.subst x v), from this ▸ x_free, or.elim (free_in_vc.or.inv this) ( assume : x ∈ FV (P₁.subst x v), show «false», from P₁_ih this ) ( assume : x ∈ FV (P₂.subst x v), show «false», from P₂_ih this ) )}, case vc.pre t₁ t₂ { from ( have vc.subst x v (vc.pre t₁ t₂) = vc.pre (t₁.subst x v) (t₂.subst x v), by unfold vc.subst, have x ∈ FV (vc.pre (t₁.subst x v) (t₂.subst x v)), from this ▸ x_free, or.elim (free_in_vc.pre.inv this) ( assume : x ∈ FV (t₁.subst x v), show «false», from term.not_free_of_subst this ) ( assume : x ∈ FV (t₂.subst x v), show «false», from term.not_free_of_subst this ) )}, case vc.pre₁ op t { from ( have vc.subst x v (vc.pre₁ op t) = vc.pre₁ op (t.subst x v), by unfold vc.subst, have x ∈ FV (vc.pre₁ op (t.subst x v)), from this ▸ x_free, have x ∈ FV (t.subst x v), from free_in_vc.pre₁.inv this, show «false», from term.not_free_of_subst this )}, case vc.pre₂ op t₁ t₂ { from ( have vc.subst x v (vc.pre₂ op t₁ t₂) = vc.pre₂ op (t₁.subst x v) (t₂.subst x v), by unfold vc.subst, have x ∈ FV (vc.pre₂ op (t₁.subst x v) (t₂.subst x v)), from this ▸ x_free, or.elim (free_in_vc.pre₂.inv this) ( assume : x ∈ FV (t₁.subst x v), show «false», from term.not_free_of_subst this ) ( assume : x ∈ FV (t₂.subst x v), show «false», from term.not_free_of_subst this ) )}, case vc.post t₁ t₂ { from ( have vc.subst x v (vc.post t₁ t₂) = vc.post (t₁.subst x v) (t₂.subst x v), by unfold vc.subst, have x ∈ FV (vc.post (t₁.subst x v) (t₂.subst x v)), from this ▸ x_free, or.elim (free_in_vc.post.inv this) ( assume : x ∈ FV (t₁.subst x v), show «false», from term.not_free_of_subst this ) ( assume : x ∈ FV (t₂.subst x v), show «false», from term.not_free_of_subst this ) )}, case vc.univ y P₁ P₁_ih { from ( have vc.subst x v (vc.univ y P₁) = vc.univ y (if x = y then P₁ else P₁.subst x v), by unfold vc.subst, have x ∈ FV (vc.univ y (if x = y then P₁ else P₁.subst x v)), from this ▸ x_free, have y_neq_x: x ≠ y, from (free_in_vc.univ.inv this).left, have x ∈ FV (vc.univ y (P₁.subst x v)), by { simp[y_neq_x] at this, from this }, have x ∈ FV (P₁.subst x v), from (free_in_vc.univ.inv this).right, show «false», from P₁_ih this )} end lemma vc.not_free_of_substt {x: var} {t: term} {P: vc}: closed t → x ∉ FV (vc.substt x t P) := assume t_closed: closed t, assume x_free: x ∈ FV (vc.substt x t P), begin induction P, case vc.term t₂ { from ( have vc.substt x t (vc.term t₂) = (term.substt x t t₂), by unfold vc.substt, have free_in_vc x (vc.term (term.substt x t t₂)), from this ▸ x_free, have x ∈ FV (term.substt x t t₂), from free_in_vc.term.inv this, show «false», from term.not_free_of_substt t_closed this )}, case vc.not P₁ P₁_ih { from ( have vc.substt x t (vc.not P₁) = (P₁.substt x t).not, by unfold vc.substt, have x ∈ FV (P₁.substt x t).not, from this ▸ x_free, have x ∈ FV (P₁.substt x t), from free_in_vc.not.inv this, show «false», from P₁_ih this )}, case vc.and P₁ P₂ P₁_ih P₂_ih { from ( have vc.substt x t (vc.and P₁ P₂) = (P₁.substt x t ⋀ P₂.substt x t), by unfold vc.substt, have x ∈ FV (P₁.substt x t ⋀ P₂.substt x t), from this ▸ x_free, or.elim (free_in_vc.and.inv this) ( assume : x ∈ FV (P₁.substt x t), show «false», from P₁_ih this ) ( assume : x ∈ FV (P₂.substt x t), show «false», from P₂_ih this ) )}, case vc.or P₁ P₂ P₁_ih P₂_ih { from ( have vc.substt x t (vc.or P₁ P₂) = (P₁.substt x t ⋁ P₂.substt x t), by unfold vc.substt, have x ∈ FV (P₁.substt x t ⋁ P₂.substt x t), from this ▸ x_free, or.elim (free_in_vc.or.inv this) ( assume : x ∈ FV (P₁.substt x t), show «false», from P₁_ih this ) ( assume : x ∈ FV (P₂.substt x t), show «false», from P₂_ih this ) )}, case vc.pre t₁ t₂ { from ( have vc.substt x t (vc.pre t₁ t₂) = vc.pre (term.substt x t t₁) (term.substt x t t₂), by unfold vc.substt, have x ∈ FV (vc.pre (term.substt x t t₁) (term.substt x t t₂)), from this ▸ x_free, or.elim (free_in_vc.pre.inv this) ( assume : x ∈ FV (term.substt x t t₁), show «false», from term.not_free_of_substt t_closed this ) ( assume : x ∈ FV (term.substt x t t₂), show «false», from term.not_free_of_substt t_closed this ) )}, case vc.pre₁ op t₁ { from ( have vc.substt x t (vc.pre₁ op t₁) = vc.pre₁ op (term.substt x t t₁), by unfold vc.substt, have x ∈ FV (vc.pre₁ op (term.substt x t t₁)), from this ▸ x_free, have x ∈ FV (term.substt x t t₁), from free_in_vc.pre₁.inv this, show «false», from term.not_free_of_substt t_closed this )}, case vc.pre₂ op t₁ t₂ { from ( have vc.substt x t (vc.pre₂ op t₁ t₂) = vc.pre₂ op (term.substt x t t₁) (term.substt x t t₂), by unfold vc.substt, have x ∈ FV (vc.pre₂ op (term.substt x t t₁) (term.substt x t t₂)), from this ▸ x_free, or.elim (free_in_vc.pre₂.inv this) ( assume : x ∈ FV (term.substt x t t₁), show «false», from term.not_free_of_substt t_closed this ) ( assume : x ∈ FV (term.substt x t t₂), show «false», from term.not_free_of_substt t_closed this ) )}, case vc.post t₁ t₂ { from ( have vc.substt x t (vc.post t₁ t₂) = vc.post (term.substt x t t₁) (term.substt x t t₂), by unfold vc.substt, have x ∈ FV (vc.post (term.substt x t t₁) (term.substt x t t₂)), from this ▸ x_free, or.elim (free_in_vc.post.inv this) ( assume : x ∈ FV (term.substt x t t₁), show «false», from term.not_free_of_substt t_closed this ) ( assume : x ∈ FV (term.substt x t t₂), show «false», from term.not_free_of_substt t_closed this ) )}, case vc.univ y P₁ P₁_ih { from ( have vc.substt x t (vc.univ y P₁) = vc.univ y (if x = y then P₁ else P₁.substt x t), by unfold vc.substt, have x ∈ FV (vc.univ y (if x = y then P₁ else P₁.substt x t)), from this ▸ x_free, have y_neq_x: x ≠ y, from (free_in_vc.univ.inv this).left, have x ∈ FV (vc.univ y (P₁.substt x t)), by { simp[y_neq_x] at this, from this }, have x ∈ FV (P₁.substt x t), from (free_in_vc.univ.inv this).right, show «false», from P₁_ih this )} end lemma vc.not_free_of_subst_env {x: var} {σ: env} {P: vc}: x ∈ σ → x ∉ FV (vc.subst_env σ P) := assume x_in_σ: x ∈ σ, assume x_free: x ∈ FV (vc.subst_env σ P), begin induction σ with σ' y v ih, -- env.empty show «false», by cases x_in_σ, -- σ'[x↦v] show «false», from ( have vc.subst_env (σ'[y↦v]) P = vc.subst y v (vc.subst_env σ' P), by unfold vc.subst_env, have x ∈ FV (vc.subst y v (vc.subst_env σ' P)), from this ▸ x_free, have x_neq_y: x ≠ y, from (free_in_vc.subst this).left, have h: x ∈ FV (vc.subst_env σ' P), from (free_in_vc.subst this).right, have x = y ∨ x ∈ σ', from env.contains.inv x_in_σ, or.elim this ( assume : x = y, show «false», from x_neq_y this ) ( assume : x ∈ σ', have x ∉ FV (vc.subst_env σ' P), from ih this, show «false», from this h ) ) end lemma vc.closed_of_closed_subst {σ: env} {P: vc}: closed_subst σ P → closed (vc.subst_env σ P) := assume P_closed_subst: closed_subst σ P, show closed (vc.subst_env σ P), from ( assume x: var, assume h1: x ∈ FV (vc.subst_env σ P), have x ∈ FV P, from free_in_vc.subst_env h1, have x ∈ σ.dom, from P_closed_subst this, have x ∈ σ, from this, have h2: x ∉ FV (vc.subst_env σ P), from vc.not_free_of_subst_env this, show «false», from h2 h1 ) lemma term.free_of_diff_subst {x y: var} {v: value} {t: term}: x ∈ FV t → x ≠ y → x ∈ FV (term.subst y v t) := assume x_free: x ∈ FV t, assume x_neq_y: x ≠ y, show x ∈ FV (term.subst y v t), from begin induction t with v' z unop t₁ t₁_ih binop t₂ t₃ t₂_ih t₃_ih t₄ t₅ t₄_ih t₅_ih, show x ∈ FV (term.subst y v (term.value v')), by begin -- term.value unfold term.subst, from x_free end, show x ∈ FV (term.subst y v (term.var z)), by begin -- term.var have : (x = z), from free_in_term.var.inv x_free, rw[this] at x_neq_y, have : (term.subst y v z = z), from term.subst.var.diff x_neq_y.symm, change x ∈ FV (term.subst y v ↑z), rw[this], from x_free end, show x ∈ FV (term.subst y v (term.unop unop t₁)), by begin -- term.unop unfold term.subst, apply free_in_term.unop, have : x ∈ FV t₁, from free_in_term.unop.inv x_free, from t₁_ih this end, show x ∈ FV (term.subst y v (term.binop binop t₂ t₃)), by begin -- term.binop unfold term.subst, have : x ∈ FV t₂ ∨ x ∈ FV t₃, from free_in_term.binop.inv x_free, cases this with h, apply free_in_term.binop₁, from t₂_ih h, apply free_in_term.binop₂, from t₃_ih a end, show x ∈ FV (term.subst y v (term.app t₄ t₅)), by begin -- term.binop unfold term.subst, have : x ∈ FV t₄ ∨ x ∈ FV t₅, from free_in_term.app.inv x_free, cases this with h, apply free_in_term.app₁, from t₄_ih h, apply free_in_term.app₂, from t₅_ih a end, end lemma vc.free_of_diff_subst {x y: var} {v: value} {P: vc}: x ∈ FV P → x ≠ y → x ∈ FV (vc.subst y v P) := assume x_free: x ∈ FV P, assume x_neq_y: x ≠ y, show x ∈ FV (vc.subst y v P), from begin induction P, case vc.term t { unfold vc.subst, apply free_in_vc.term, have h2, from free_in_vc.term.inv x_free, from term.free_of_diff_subst h2 x_neq_y }, case vc.not P₁ P₁_ih { unfold vc.subst, apply free_in_vc.not, have h2, from free_in_vc.not.inv x_free, from P₁_ih h2 }, case vc.and P₁ P₂ P₁_ih P₂_ih { unfold vc.subst, have : x ∈ FV P₁ ∨ x ∈ FV P₂, from free_in_vc.and.inv x_free, cases this with h, apply free_in_vc.and₁, from P₁_ih h, apply free_in_vc.and₂, from P₂_ih a }, case vc.or P₁ P₂ P₁_ih P₂_ih { unfold vc.subst, have : x ∈ FV P₁ ∨ x ∈ FV P₂, from free_in_vc.or.inv x_free, cases this with h, apply free_in_vc.or₁, from P₁_ih h, apply free_in_vc.or₂, from P₂_ih a }, case vc.pre t₁ t₂ { unfold vc.subst, have : x ∈ FV t₁ ∨ x ∈ FV t₂, from free_in_vc.pre.inv x_free, cases this with h, apply free_in_vc.pre₁, from term.free_of_diff_subst h x_neq_y, apply free_in_vc.pre₂, from term.free_of_diff_subst a x_neq_y }, case vc.pre₁ op t { unfold vc.subst, apply free_in_vc.preop, have h2, from free_in_vc.pre₁.inv x_free, from term.free_of_diff_subst h2 x_neq_y }, case vc.pre₂ op t₁ t₂ { unfold vc.subst, have : x ∈ FV t₁ ∨ x ∈ FV t₂, from free_in_vc.pre₂.inv x_free, cases this with h, apply free_in_vc.preop₁, from term.free_of_diff_subst h x_neq_y, apply free_in_vc.preop₂, from term.free_of_diff_subst a x_neq_y }, case vc.post t₁ t₂ { unfold vc.subst, have : x ∈ FV t₁ ∨ x ∈ FV t₂, from free_in_vc.post.inv x_free, cases this with h, apply free_in_vc.post₁, from term.free_of_diff_subst h x_neq_y, apply free_in_vc.post₂, from term.free_of_diff_subst a x_neq_y }, case vc.univ z P₁ P₁_ih { unfold vc.subst, have h2, from free_in_vc.univ.inv x_free, apply free_in_vc.univ, from h2.left, by_cases (y = z), rw[h], simp, from h2.right, simp[h], from P₁_ih h2.right } end lemma term.free_of_subst_env {x: var} {σ: env} {t: term}: x ∈ FV t → x ∉ σ → x ∈ FV (term.subst_env σ t) := assume x_free: x ∈ FV t, assume x_not_in_σ: x ∉ σ, show x ∈ FV (term.subst_env σ t), begin induction σ with σ' y v ih, -- env.empty show x ∈ FV (term.subst_env env.empty t), begin unfold term.subst_env, from x_free end, -- σ'[x↦v] show x ∈ FV (term.subst_env (σ'[y↦v]) t), begin unfold term.subst_env, by_cases (x = y), begin -- x = y rw[h] at x_not_in_σ, have : y ∈ (σ'[y↦v]), from env.contains.same, contradiction end, begin -- x ≠ y by_cases (x ∈ σ') with h2, begin -- x ∈ σ' have : x ∈ (σ'[y↦v]), from env.contains.rest h2, contradiction end, begin -- x ∉ σ' have : x ∈ FV (term.subst_env σ' t), from ih h2, from term.free_of_diff_subst this h end, end end end lemma vc.free_of_subst_env {x: var} {σ: env} {P: vc}: x ∈ FV P → x ∉ σ → x ∈ FV (vc.subst_env σ P) := assume x_free: x ∈ FV P, assume x_not_in_σ: x ∉ σ, show x ∈ FV (vc.subst_env σ P), begin induction σ with σ' y v ih, -- env.empty show x ∈ FV (vc.subst_env env.empty P), begin unfold vc.subst_env, from x_free end, -- σ'[x↦v] show x ∈ FV (vc.subst_env (σ'[y↦v]) P), begin unfold vc.subst_env, by_cases (x = y), begin -- x = y rw[h] at x_not_in_σ, have : y ∈ (σ'[y↦v]), from env.contains.same, contradiction end, begin -- x ≠ y by_cases (x ∈ σ') with h2, begin -- x ∈ σ' have : x ∈ (σ'[y↦v]), from env.contains.rest h2, contradiction end, begin -- x ∉ σ' have : x ∈ FV (vc.subst_env σ' P), from ih h2, from vc.free_of_diff_subst this h end, end end end lemma term.free_of_free_in_subst {x y: var} {v: value} {t: term}: x ∈ FV (term.subst y v t) → x ∈ FV t := begin assume h1, induction t with v' z unop t₁ t₁_ih binop t₂ t₃ t₂_ih t₃_ih t₄ t₅ t₄_ih t₅_ih, show x ∈ FV (term.value v'), by begin unfold term.subst at h1, cases h1 end, show x ∈ FV (term.var z), by begin unfold term.subst at h1, by_cases (y = z) with h2, simp[h2] at h1, cases h1, simp[h2] at h1, from h1 end, show x ∈ FV (term.unop unop t₁), by begin unfold term.subst at h1, apply free_in_term.unop, have h2, from free_in_term.unop.inv h1, from t₁_ih h2 end, show x ∈ FV (term.binop binop t₂ t₃), by begin unfold term.subst at h1, have h2, from free_in_term.binop.inv h1, cases h2, apply free_in_term.binop₁, from t₂_ih a, apply free_in_term.binop₂, from t₃_ih a end, show x ∈ FV (term.app t₄ t₅), by begin unfold term.subst at h1, have h2, from free_in_term.app.inv h1, cases h2, apply free_in_term.app₁, from t₄_ih a, apply free_in_term.app₂, from t₅_ih a end end lemma term.closed_subst_of_closed {σ: env} {t: term}: closed (term.subst_env σ t) → closed_subst σ t := assume t_closed_subst: closed (term.subst_env σ t), show closed_subst σ t, from ( assume x: var, assume h1: x ∈ FV t, have ¬ x ∉ σ, from mt (term.free_of_subst_env h1) (t_closed_subst x), have x ∈ σ, from of_not_not this, show x ∈ σ.dom, from this ) lemma term.substt_value_eq_subst {x: var} {v: value} {t: term}: term.substt x v t = term.subst x v t := begin induction t with v' z unop t₁ t₁_ih binop t₂ t₃ t₂_ih t₃_ih t₄ t₅ t₄_ih t₅_ih, show (term.substt x v (term.value v') = term.subst x v (term.value v')), by begin unfold term.substt, unfold term.subst end, show (term.substt x v (term.var z) = term.subst x v (term.var z)), by begin unfold term.substt, unfold term.subst end, show (term.substt x ↑v (term.unop unop t₁) = term.subst x v (term.unop unop t₁)), by begin unfold term.substt, unfold term.subst, congr end, show (term.substt x v (term.binop binop t₂ t₃) = term.subst x v (term.binop binop t₂ t₃)), by begin unfold term.substt, unfold term.subst, congr end, show (term.substt x ↑v (term.app t₄ t₅) = term.subst x v (term.app t₄ t₅)), by begin unfold term.substt, unfold term.subst, congr end end lemma vc.substt_value_eq_subst {x: var} {v: value} {P: vc}: vc.substt x v P = vc.subst x v P := begin induction P, case vc.term t { unfold vc.substt, unfold vc.subst, congr }, case vc.not P₁ ih { unfold vc.substt, unfold vc.subst, congr, from ih }, case vc.and P₁ P₂ P₁_ih P₂_ih { unfold vc.substt, unfold vc.subst, congr, from P₁_ih, from P₂_ih }, case vc.or P₁ P₂ P₁_ih P₂_ih { unfold vc.substt, unfold vc.subst, congr, from P₁_ih, from P₂_ih }, case vc.pre t₁ t₂ { unfold vc.substt, unfold vc.subst, congr }, case vc.pre₁ op t { unfold vc.substt, unfold vc.subst, congr }, case vc.pre₂ op t₁ t₂ { unfold vc.substt, unfold vc.subst, congr }, case vc.post t₁ t₂ { unfold vc.substt, unfold vc.subst, congr }, case vc.univ z P' P'_ih { unfold vc.substt, unfold vc.subst, congr, from P'_ih } end lemma prop.free_of_free_in_subst {x y: var} {v: value} {P: prop}: x ∈ FV (prop.subst y v P) → x ∈ FV P := begin assume h1, induction P, case prop.term t { apply free_in_prop.term, have h2, from free_in_prop.term.inv h1, from term.free_of_free_in_subst h2 }, case prop.not P₁ ih { apply free_in_prop.not, unfold prop.subst at h1, have h2, from free_in_prop.not.inv h1, from ih h2 }, case prop.and P₁ P₂ P₁_ih P₂_ih { unfold prop.subst at h1, have h2, from free_in_prop.and.inv h1, cases h2, apply free_in_prop.and₁, from P₁_ih a, apply free_in_prop.and₂, from P₂_ih a }, case prop.or P₁ P₂ P₁_ih P₂_ih { unfold prop.subst at h1, have h2, from free_in_prop.or.inv h1, cases h2, apply free_in_prop.or₁, from P₁_ih a, apply free_in_prop.or₂, from P₂_ih a }, case prop.pre t₁ t₂ { unfold prop.subst at h1, have h2, from free_in_prop.pre.inv h1, cases h2, apply free_in_prop.pre₁, from term.free_of_free_in_subst a, apply free_in_prop.pre₂, from term.free_of_free_in_subst a }, case prop.pre₁ op t { unfold prop.subst at h1, have h2, from free_in_prop.pre₁.inv h1, apply free_in_prop.preop, from term.free_of_free_in_subst h2 }, case prop.pre₂ op t₁ t₂ { unfold prop.subst at h1, have h2, from free_in_prop.pre₂.inv h1, cases h2, apply free_in_prop.preop₁, from term.free_of_free_in_subst a, apply free_in_prop.preop₂, from term.free_of_free_in_subst a }, case prop.call t { unfold prop.subst at h1, have h2, from free_in_prop.call.inv h1, apply free_in_prop.call, from term.free_of_free_in_subst h2 }, case prop.post t₁ t₂ { unfold prop.subst at h1, have h2, from free_in_prop.post.inv h1, cases h2, apply free_in_prop.post₁, from term.free_of_free_in_subst a, apply free_in_prop.post₂, from term.free_of_free_in_subst a }, case prop.forallc z P' P'_ih { unfold prop.subst at h1, have h2, from free_in_prop.forallc.inv h1, by_cases (y = z) with h3, have h4, from h2.right, rw[h3] at h4, simp at h4, apply free_in_prop.forallc, from h2.left, from h4, have h4, from h2.right, simp[h3] at h4, apply free_in_prop.forallc, from h2.left, simp[h3] at h4, from P'_ih h4 }, case prop.exis z P' P'_ih { unfold prop.subst at h1, have h2, from free_in_prop.exis.inv h1, by_cases (y = z) with h3, have h4, from h2.right, rw[h3] at h4, simp at h4, apply free_in_prop.exis, from h2.left, from h4, have h4, from h2.right, simp[h3] at h4, have : ((ite (y = z) P' (prop.subst y v P')) = (prop.subst y v P')), by simp[h3], rw[this] at h4, apply free_in_prop.exis, from h2.left, from P'_ih h4 } end lemma prop.free_of_free_subst_env {x: var} {σ: env} {P: prop}: x ∈ FV (prop.subst_env σ P) → x ∈ FV P := assume x_free: x ∈ FV (prop.subst_env σ P), show x ∈ FV P, begin induction σ with σ' y v ih, -- env.empty show x ∈ FV P, begin unfold prop.subst_env at x_free, from x_free end, -- σ'[x↦v] show x ∈ FV P, begin unfold prop.subst_env at x_free, have h1: x ∈ FV (prop.subst_env σ' P), from prop.free_of_free_in_subst x_free, from ih h1 end end lemma vc.free_of_free_in_subst {x y: var} {v: value} {P: vc}: x ∈ FV (vc.subst y v P) → x ∈ FV P := begin assume h1, induction P, case vc.term t { apply free_in_vc.term, have h2, from free_in_vc.term.inv h1, from term.free_of_free_in_subst h2 }, case vc.not P₁ ih { apply free_in_vc.not, unfold vc.subst at h1, have h2, from free_in_vc.not.inv h1, from ih h2 }, case vc.and P₁ P₂ P₁_ih P₂_ih { unfold vc.subst at h1, have h2, from free_in_vc.and.inv h1, cases h2, apply free_in_vc.and₁, from P₁_ih a, apply free_in_vc.and₂, from P₂_ih a }, case vc.or P₁ P₂ P₁_ih P₂_ih { unfold vc.subst at h1, have h2, from free_in_vc.or.inv h1, cases h2, apply free_in_vc.or₁, from P₁_ih a, apply free_in_vc.or₂, from P₂_ih a }, case vc.pre t₁ t₂ { unfold vc.subst at h1, have h2, from free_in_vc.pre.inv h1, cases h2, apply free_in_vc.pre₁, from term.free_of_free_in_subst a, apply free_in_vc.pre₂, from term.free_of_free_in_subst a }, case vc.pre₁ op t { unfold vc.subst at h1, have h2, from free_in_vc.pre₁.inv h1, apply free_in_vc.preop, from term.free_of_free_in_subst h2 }, case vc.pre₂ op t₁ t₂ { unfold vc.subst at h1, have h2, from free_in_vc.pre₂.inv h1, cases h2, apply free_in_vc.preop₁, from term.free_of_free_in_subst a, apply free_in_vc.preop₂, from term.free_of_free_in_subst a }, case vc.post t₁ t₂ { unfold vc.subst at h1, have h2, from free_in_vc.post.inv h1, cases h2, apply free_in_vc.post₁, from term.free_of_free_in_subst a, apply free_in_vc.post₂, from term.free_of_free_in_subst a }, case vc.univ z P' P'_ih { unfold vc.subst at h1, have h2, from free_in_vc.univ.inv h1, by_cases (y = z) with h3, have h4, from h2.right, rw[h3] at h4, simp at h4, apply free_in_vc.univ, from h2.left, from h4, have h4, from h2.right, simp[h3] at h4, have : ((ite (y = z) P' (vc.subst y v P')) = (vc.subst y v P')), by simp[h3], rw[this] at h4, apply free_in_vc.univ, from h2.left, from P'_ih h4 } end lemma vc.free_of_free_subst_env {x: var} {σ: env} {P: vc}: x ∈ FV (vc.subst_env σ P) → x ∈ FV P := assume x_free: x ∈ FV (vc.subst_env σ P), show x ∈ FV P, begin induction σ with σ' y v ih, -- env.empty show x ∈ FV P, begin unfold vc.subst_env at x_free, from x_free end, -- σ'[x↦v] show x ∈ FV P, begin unfold vc.subst_env at x_free, have h1: x ∈ FV (vc.subst_env σ' P), from vc.free_of_free_in_subst x_free, from ih h1 end end lemma vc.closed_subst_of_closed {σ: env} {P: vc}: closed (vc.subst_env σ P) → closed_subst σ P := assume P_closed_subst: closed (vc.subst_env σ P), show closed_subst σ P, from ( assume x: var, assume h1: x ∈ FV P, have ¬ x ∉ σ, from mt (vc.free_of_subst_env h1) (P_closed_subst x), have x ∈ σ, from of_not_not this, show x ∈ σ.dom, from this ) lemma prop.closed_any_subst_of_closed {σ: env} {P: prop}: closed P → closed_subst σ P := assume P_closed: closed P, show closed_subst σ P, from ( assume x: var, assume : x ∈ FV P, show x ∈ σ.dom, from absurd this (P_closed x) ) lemma term.subst_env.unop {σ: env} {op: unop} {t: term}: term.subst_env σ (term.unop op t) = term.unop op (term.subst_env σ t) := begin induction σ with σ' x v ih, show (term.subst_env env.empty (term.unop op t) = term.unop op (term.subst_env env.empty t)), by calc term.subst_env env.empty (term.unop op t) = (term.unop op t) : by unfold term.subst_env ... = (term.unop op (term.subst_env env.empty t)) : by unfold term.subst_env, show (term.subst_env (σ'[x↦v]) (term.unop op t) = (term.unop op (term.subst_env (σ'[x↦v]) t))), by calc term.subst_env (σ'[x↦v]) (term.unop op t) = term.subst x v (term.subst_env σ' (term.unop op t)) : by unfold term.subst_env ... = term.subst x v (term.unop op (term.subst_env σ' t)) : by rw[ih] ... = term.unop op (term.subst x v (term.subst_env σ' t)) : by unfold term.subst ... = term.unop op (term.subst_env (σ'[x↦v]) t) : by unfold term.subst_env end lemma term.subst_env.binop {σ: env} {op: binop} {t₁ t₂: term}: term.subst_env σ (term.binop op t₁ t₂) = term.binop op (term.subst_env σ t₁) (term.subst_env σ t₂) := begin induction σ with σ' x v ih, show (term.subst_env env.empty (term.binop op t₁ t₂) = term.binop op (term.subst_env env.empty t₁) (term.subst_env env.empty t₂)), by calc term.subst_env env.empty (term.binop op t₁ t₂) = (term.binop op t₁ t₂) : by unfold term.subst_env ... = (term.binop op (term.subst_env env.empty t₁) t₂) : by unfold term.subst_env ... = (term.binop op (term.subst_env env.empty t₁) (term.subst_env env.empty t₂)) : by unfold term.subst_env, show (term.subst_env (σ'[x↦v]) (term.binop op t₁ t₂) = (term.binop op (term.subst_env (σ'[x↦v]) t₁) (term.subst_env (σ'[x↦v]) t₂))), by calc term.subst_env (σ'[x↦v]) (term.binop op t₁ t₂) = term.subst x v (term.subst_env σ' (term.binop op t₁ t₂)) : by unfold term.subst_env ... = term.subst x v (term.binop op (term.subst_env σ' t₁) (term.subst_env σ' t₂)) : by rw[ih] ... = term.binop op (term.subst x v (term.subst_env σ' t₁)) (term.subst x v (term.subst_env σ' t₂)) : by unfold term.subst ... = term.binop op (term.subst_env (σ'[x↦v]) t₁) (term.subst x v (term.subst_env σ' t₂)) : by unfold term.subst_env ... = term.binop op (term.subst_env (σ'[x↦v]) t₁) (term.subst_env (σ'[x↦v]) t₂) : by unfold term.subst_env end lemma term.subst_env.app {σ: env} {t₁ t₂: term}: term.subst_env σ (term.app t₁ t₂) = term.app (term.subst_env σ t₁) (term.subst_env σ t₂) := begin induction σ with σ' x v ih, show (term.subst_env env.empty (term.app t₁ t₂) = term.app (term.subst_env env.empty t₁) (term.subst_env env.empty t₂)), by calc term.subst_env env.empty (term.app t₁ t₂) = (term.app t₁ t₂) : by unfold term.subst_env ... = (term.app (term.subst_env env.empty t₁) t₂) : by unfold term.subst_env ... = (term.app (term.subst_env env.empty t₁) (term.subst_env env.empty t₂)) : by unfold term.subst_env, show (term.subst_env (σ'[x↦v]) (term.app t₁ t₂) = (term.app (term.subst_env (σ'[x↦v]) t₁) (term.subst_env (σ'[x↦v]) t₂))), by calc term.subst_env (σ'[x↦v]) (term.app t₁ t₂) = term.subst x v (term.subst_env σ' (term.app t₁ t₂)) : by unfold term.subst_env ... = term.subst x v (term.app (term.subst_env σ' t₁) (term.subst_env σ' t₂)) : by rw[ih] ... = term.app (term.subst x v (term.subst_env σ' t₁)) (term.subst x v (term.subst_env σ' t₂)) : by unfold term.subst ... = term.app (term.subst_env (σ'[x↦v]) t₁) (term.subst x v (term.subst_env σ' t₂)) : by unfold term.subst_env ... = term.app (term.subst_env (σ'[x↦v]) t₁) (term.subst_env (σ'[x↦v]) t₂) : by unfold term.subst_env end lemma prop.subst_env.term {σ: env} {t: term}: prop.subst_env σ t = prop.term (term.subst_env σ t) := begin induction σ with σ' x v ih, show (prop.subst_env env.empty t = prop.term (term.subst_env env.empty t)), by begin have : (term.subst_env env.empty t = t), by unfold term.subst_env, have h2: (prop.term (term.subst_env env.empty t) = prop.term t), by simp[this], calc prop.subst_env env.empty t = t : by unfold prop.subst_env ... = prop.term t : by refl ... = prop.term (term.subst_env env.empty t) : by rw[←h2] end, show (prop.subst_env (σ'[x↦v]) t = prop.term (term.subst_env (σ'[x↦v]) t)), by calc prop.subst_env (σ'[x↦v]) t = prop.subst x v (prop.subst_env σ' t) : by unfold prop.subst_env ... = prop.subst x v (prop.term (term.subst_env σ' t)) : by rw[ih] ... = term.subst x v (term.subst_env σ' t) : by unfold prop.subst ... = term.subst_env (σ'[x↦v]) t : by unfold term.subst_env end lemma prop.subst_env.not {σ: env} {P: prop}: prop.subst_env σ P.not = (prop.subst_env σ P).not := begin induction σ with σ' x v ih, show (prop.subst_env env.empty P.not = (prop.subst_env env.empty P).not), by calc prop.subst_env env.empty P.not = P.not : by unfold prop.subst_env ... = (prop.subst_env env.empty P).not : by unfold prop.subst_env, show (prop.subst_env (σ'[x↦v]) P.not = (prop.subst_env (σ'[x↦v]) P).not), by calc prop.subst_env (σ'[x↦v]) P.not = prop.subst x v (prop.subst_env σ' P.not) : by unfold prop.subst_env ... = prop.subst x v (prop.subst_env σ' P).not : by rw[ih] ... = (prop.subst x v (prop.subst_env σ' P)).not : by unfold prop.subst ... = (prop.subst_env (σ'[x↦v]) P).not : by unfold prop.subst_env end lemma prop.subst_env.and {σ: env} {P Q: prop}: prop.subst_env σ (P ⋀ Q) = (prop.subst_env σ P ⋀ prop.subst_env σ Q) := begin induction σ with σ' x v ih, show (prop.subst_env env.empty (P ⋀ Q) = (prop.subst_env env.empty P ⋀ prop.subst_env env.empty Q)), by calc prop.subst_env env.empty (P ⋀ Q) = (P ⋀ Q) : by unfold prop.subst_env ... = (prop.subst_env env.empty P ⋀ Q) : by unfold prop.subst_env ... = (prop.subst_env env.empty P ⋀ prop.subst_env env.empty Q) : by unfold prop.subst_env, show (prop.subst_env (σ'[x↦v]) (P ⋀ Q) = (prop.subst_env (σ'[x↦v]) P ⋀ prop.subst_env (σ'[x↦v]) Q)), by calc prop.subst_env (σ'[x↦v]) (P ⋀ Q) = prop.subst x v (prop.subst_env σ' (P ⋀ Q)) : by unfold prop.subst_env ... = prop.subst x v (prop.subst_env σ' P ⋀ prop.subst_env σ' Q) : by rw[ih] ... = (prop.subst x v (prop.subst_env σ' P) ⋀ prop.subst x v (prop.subst_env σ' Q)) : by refl ... = (prop.subst_env (σ'[x↦v]) P ⋀ prop.subst x v (prop.subst_env σ' Q)) : by unfold prop.subst_env ... = (prop.subst_env (σ'[x↦v]) P ⋀ prop.subst_env (σ'[x↦v]) Q) : by unfold prop.subst_env end lemma prop.subst_env.or {σ: env} {P Q: prop}: prop.subst_env σ (P ⋁ Q) = (prop.subst_env σ P ⋁ prop.subst_env σ Q) := begin induction σ with σ' x v ih, show (prop.subst_env env.empty (P ⋁ Q) = (prop.subst_env env.empty P ⋁ prop.subst_env env.empty Q)), by calc prop.subst_env env.empty (P ⋁ Q) = (P ⋁ Q) : by unfold prop.subst_env ... = (prop.subst_env env.empty P ⋁ Q) : by by unfold prop.subst_env ... = (prop.subst_env env.empty P ⋁ prop.subst_env env.empty Q) : by unfold prop.subst_env, show (prop.subst_env (σ'[x↦v]) (P ⋁ Q) = (prop.subst_env (σ'[x↦v]) P ⋁ prop.subst_env (σ'[x↦v]) Q)), by calc prop.subst_env (σ'[x↦v]) (P ⋁ Q) = prop.subst x v (prop.subst_env σ' (P ⋁ Q)) : by unfold prop.subst_env ... = prop.subst x v (prop.subst_env σ' P ⋁ prop.subst_env σ' Q) : by rw[ih] ... = (prop.subst x v (prop.subst_env σ' P) ⋁ prop.subst x v (prop.subst_env σ' Q)) : by refl ... = (prop.subst_env (σ'[x↦v]) P ⋁ prop.subst x v (prop.subst_env σ' Q)) : by unfold prop.subst_env ... = (prop.subst_env (σ'[x↦v]) P ⋁ prop.subst_env (σ'[x↦v]) Q) : by unfold prop.subst_env end lemma prop.subst_env.implies {σ: env} {P Q: prop}: prop.subst_env σ (prop.implies P Q) = prop.implies (prop.subst_env σ P) (prop.subst_env σ Q) := have h1: prop.subst_env σ (prop.implies P Q) = prop.subst_env σ (P.not ⋁ Q), by refl, have prop.subst_env σ (P.not ⋁ Q) = (prop.subst_env σ P.not ⋁ prop.subst_env σ Q), from prop.subst_env.or, have h2: prop.subst_env σ (prop.implies P Q) = (prop.subst_env σ P.not ⋁ prop.subst_env σ Q), from this ▸ h1, have prop.subst_env σ P.not = prop.not (prop.subst_env σ P), from prop.subst_env.not, have prop.subst_env σ (prop.implies P Q) = (prop.not (prop.subst_env σ P) ⋁ prop.subst_env σ Q), from this ▸ h2, show prop.subst_env σ (prop.implies P Q) = prop.implies (prop.subst_env σ P) (prop.subst_env σ Q), from this lemma prop.subst_env.pre {σ: env} {t₁ t₂: term}: prop.subst_env σ (prop.pre t₁ t₂) = prop.pre (term.subst_env σ t₁) (term.subst_env σ t₂) := begin induction σ with σ' x v ih, show (prop.subst_env env.empty (prop.pre t₁ t₂) = prop.pre (term.subst_env env.empty t₁) (term.subst_env env.empty t₂)), by calc prop.subst_env env.empty (prop.pre t₁ t₂) = (prop.pre t₁ t₂) : by unfold prop.subst_env ... = (prop.pre (term.subst_env env.empty t₁) t₂) : by unfold term.subst_env ... = (prop.pre (term.subst_env env.empty t₁) (term.subst_env env.empty t₂)) : by unfold term.subst_env, show (prop.subst_env (σ'[x↦v]) (prop.pre t₁ t₂) = prop.pre (term.subst_env (σ'[x↦v]) t₁) (term.subst_env (σ'[x↦v]) t₂)), by calc prop.subst_env (σ'[x↦v]) (prop.pre t₁ t₂) = prop.subst x v (prop.subst_env σ' (prop.pre t₁ t₂)) : by unfold prop.subst_env ... = prop.subst x v (prop.pre (term.subst_env σ' t₁) (term.subst_env σ' t₂)) : by rw[ih] ... = prop.pre (term.subst x v (term.subst_env σ' t₁)) (term.subst x v (term.subst_env σ' t₂)) : by unfold prop.subst ... = prop.pre (term.subst_env (σ'[x↦v]) t₁) (term.subst x v (term.subst_env σ' t₂)) : by unfold term.subst_env ... = prop.pre (term.subst_env (σ'[x↦v]) t₁) (term.subst_env (σ'[x↦v]) t₂) : by unfold term.subst_env end lemma prop.subst_env.post {σ: env} {t₁ t₂: term}: prop.subst_env σ (prop.post t₁ t₂) = prop.post (term.subst_env σ t₁) (term.subst_env σ t₂) := begin induction σ with σ' x v ih, show (prop.subst_env env.empty (prop.post t₁ t₂) = prop.post (term.subst_env env.empty t₁) (term.subst_env env.empty t₂)), by calc prop.subst_env env.empty (prop.post t₁ t₂) = (prop.post t₁ t₂) : by unfold prop.subst_env ... = (prop.post (term.subst_env env.empty t₁) t₂) : by unfold term.subst_env ... = (prop.post (term.subst_env env.empty t₁) (term.subst_env env.empty t₂)) : by unfold term.subst_env, show (prop.subst_env (σ'[x↦v]) (prop.post t₁ t₂) = prop.post (term.subst_env (σ'[x↦v]) t₁) (term.subst_env (σ'[x↦v]) t₂)), by calc prop.subst_env (σ'[x↦v]) (prop.post t₁ t₂) = prop.subst x v (prop.subst_env σ' (prop.post t₁ t₂)) : by unfold prop.subst_env ... = prop.subst x v (prop.post (term.subst_env σ' t₁) (term.subst_env σ' t₂)) : by rw[ih] ... = prop.post (term.subst x v (term.subst_env σ' t₁)) (term.subst x v (term.subst_env σ' t₂)) : by unfold prop.subst ... = prop.post (term.subst_env (σ'[x↦v]) t₁) (term.subst x v (term.subst_env σ' t₂)) : by unfold term.subst_env ... = prop.post (term.subst_env (σ'[x↦v]) t₁) (term.subst_env (σ'[x↦v]) t₂) : by unfold term.subst_env end lemma prop.subst_env.forallc_not_in {σ: env} {x: var} {P: prop}: (x ∉ σ) → (prop.subst_env σ (prop.forallc x P) = prop.forallc x (prop.subst_env σ P)) := begin assume x_not_in_σ, induction σ with σ' y v ih, show (prop.subst_env env.empty (prop.forallc x P) = prop.forallc x (prop.subst_env env.empty P)), by calc prop.subst_env env.empty (prop.forallc x P) = prop.forallc x P : by unfold prop.subst_env ... = prop.forallc x (prop.subst_env env.empty P) : by unfold prop.subst_env, show (prop.subst_env (σ'[y↦v]) (prop.forallc x P) = prop.forallc x (prop.subst_env (σ'[y↦v]) P)), from ( have ¬ (x = y ∨ x ∈ σ'), from env.contains.same.inv x_not_in_σ, have x_neq_y: x ≠ y, from (not_or_distrib.mp this).left, have x ∉ σ', from (not_or_distrib.mp this).right, have h: prop.subst_env σ' (prop.forallc x P) = prop.forallc x (prop.subst_env σ' P), from ih this, calc prop.subst_env (σ'[y↦v]) (prop.forallc x P) = prop.subst y v (prop.subst_env σ' (prop.forallc x P)) : by unfold prop.subst_env ... = prop.subst y v (prop.forallc x (prop.subst_env σ' P)) : by rw[h] ... = prop.forallc x (if y = x then prop.subst_env σ' P else (prop.subst_env σ' P).subst y v) : by unfold prop.subst ... = prop.forallc x ((prop.subst_env σ' P).subst y v) : by simp[x_neq_y.symm] ... = prop.forallc x (prop.subst_env (σ'[y↦v]) P) : by unfold prop.subst_env ) end lemma prop.subst_env.forallc {σ: env} {x: var} {P: prop}: (prop.subst_env σ (prop.forallc x P) = prop.forallc x (prop.subst_env (σ.without x) P)) := begin induction σ with σ' y v ih, show (prop.subst_env env.empty (prop.forallc x P) = prop.forallc x (prop.subst_env (env.empty.without x) P)), by calc prop.subst_env env.empty (prop.forallc x P) = (prop.forallc x P) : by unfold prop.subst_env ... = prop.forallc x (prop.subst_env env.empty P) : by unfold prop.subst_env, show (prop.subst_env (σ'[y↦v]) (prop.forallc x P) = prop.forallc x (prop.subst_env ((σ'[y↦v]).without x) P)), by begin unfold prop.subst_env, by_cases (y = x) with h1, rw[←h1], rw[←h1] at ih, unfold env.without, simp, have : y ∉ FV (prop.subst_env σ' (prop.forallc y P)), from ( assume : y ∈ FV (prop.subst_env σ' (prop.forallc y P)), have y ∈ FV (prop.forallc y P), from prop.free_of_free_subst_env this, show «false», from free_in_prop.forallc.same.inv this ), have h2: (prop.subst y v (prop.subst_env σ' (prop.forallc y P)) = prop.subst_env σ' (prop.forallc y P)), from unchanged_of_subst_nonfree_prop this, rw[h2], from ih, unfold env.without, simp[h1], unfold prop.subst_env, have : (prop.subst y v (prop.forallc x (prop.subst_env (env.without σ' x) P)) = prop.forallc x (prop.subst y v (prop.subst_env (env.without σ' x) P))), by { unfold prop.subst, simp[h1] }, rw[←this], congr, from ih end end lemma vc.subst.implies {x: var} {v: value} {P Q: vc}: vc.subst x v (vc.implies P Q) = vc.implies (vc.subst x v P) (vc.subst x v Q) := by calc vc.subst x v (vc.implies P Q) = vc.subst x v (vc.or (vc.not P) Q) : rfl ... = (vc.subst x v (vc.not P) ⋁ vc.subst x v Q) : by unfold vc.subst ... = ((vc.subst x v P).not ⋁ vc.subst x v Q) : by unfold vc.subst lemma vc.subst_env.term {σ: env} {t: term}: vc.subst_env σ t = vc.term (term.subst_env σ t) := begin induction σ with σ' x v ih, show (vc.subst_env env.empty t = vc.term (term.subst_env env.empty t)), by begin have : (term.subst_env env.empty t = t), by unfold term.subst_env, have h2: (vc.term (term.subst_env env.empty t) = vc.term t), by simp[this], calc vc.subst_env env.empty t = t : by unfold vc.subst_env ... = vc.term t : by refl ... = vc.term (term.subst_env env.empty t) : by rw[←h2] end, show (vc.subst_env (σ'[x↦v]) t = vc.term (term.subst_env (σ'[x↦v]) t)), by calc vc.subst_env (σ'[x↦v]) t = vc.subst x v (vc.subst_env σ' t) : by unfold vc.subst_env ... = vc.subst x v (vc.term (term.subst_env σ' t)) : by rw[ih] ... = term.subst x v (term.subst_env σ' t) : by unfold vc.subst ... = term.subst_env (σ'[x↦v]) t : by unfold term.subst_env end lemma vc.subst_env.not {σ: env} {P: vc}: vc.subst_env σ P.not = (vc.subst_env σ P).not := begin induction σ with σ' x v ih, show (vc.subst_env env.empty P.not = (vc.subst_env env.empty P).not), by calc vc.subst_env env.empty P.not = P.not : by unfold vc.subst_env ... = (vc.subst_env env.empty P).not : by unfold vc.subst_env, show (vc.subst_env (σ'[x↦v]) P.not = (vc.subst_env (σ'[x↦v]) P).not), by calc vc.subst_env (σ'[x↦v]) P.not = vc.subst x v (vc.subst_env σ' P.not) : by unfold vc.subst_env ... = vc.subst x v (vc.subst_env σ' P).not : by rw[ih] ... = (vc.subst x v (vc.subst_env σ' P)).not : by unfold vc.subst ... = (vc.subst_env (σ'[x↦v]) P).not : by unfold vc.subst_env end lemma vc.subst_env.and {σ: env} {P Q: vc}: vc.subst_env σ (P ⋀ Q) = (vc.subst_env σ P ⋀ vc.subst_env σ Q) := begin induction σ with σ' x v ih, show (vc.subst_env env.empty (P ⋀ Q) = (vc.subst_env env.empty P ⋀ vc.subst_env env.empty Q)), by calc vc.subst_env env.empty (P ⋀ Q) = (P ⋀ Q) : by unfold vc.subst_env ... = (vc.subst_env env.empty P ⋀ Q) : by unfold vc.subst_env ... = (vc.subst_env env.empty P ⋀ vc.subst_env env.empty Q) : by unfold vc.subst_env, show (vc.subst_env (σ'[x↦v]) (P ⋀ Q) = (vc.subst_env (σ'[x↦v]) P ⋀ vc.subst_env (σ'[x↦v]) Q)), by calc vc.subst_env (σ'[x↦v]) (P ⋀ Q) = vc.subst x v (vc.subst_env σ' (P ⋀ Q)) : by unfold vc.subst_env ... = vc.subst x v (vc.subst_env σ' P ⋀ vc.subst_env σ' Q) : by rw[ih] ... = (vc.subst x v (vc.subst_env σ' P) ⋀ vc.subst x v (vc.subst_env σ' Q)) : by refl ... = (vc.subst_env (σ'[x↦v]) P ⋀ vc.subst x v (vc.subst_env σ' Q)) : by unfold vc.subst_env ... = (vc.subst_env (σ'[x↦v]) P ⋀ vc.subst_env (σ'[x↦v]) Q) : by unfold vc.subst_env end lemma vc.subst_env.or {σ: env} {P Q: vc}: vc.subst_env σ (P ⋁ Q) = (vc.subst_env σ P ⋁ vc.subst_env σ Q) := begin induction σ with σ' x v ih, show (vc.subst_env env.empty (P ⋁ Q) = (vc.subst_env env.empty P ⋁ vc.subst_env env.empty Q)), by calc vc.subst_env env.empty (P ⋁ Q) = (P ⋁ Q) : by unfold vc.subst_env ... = (vc.subst_env env.empty P ⋁ Q) : by by unfold vc.subst_env ... = (vc.subst_env env.empty P ⋁ vc.subst_env env.empty Q) : by unfold vc.subst_env, show (vc.subst_env (σ'[x↦v]) (P ⋁ Q) = (vc.subst_env (σ'[x↦v]) P ⋁ vc.subst_env (σ'[x↦v]) Q)), by calc vc.subst_env (σ'[x↦v]) (P ⋁ Q) = vc.subst x v (vc.subst_env σ' (P ⋁ Q)) : by unfold vc.subst_env ... = vc.subst x v (vc.subst_env σ' P ⋁ vc.subst_env σ' Q) : by rw[ih] ... = (vc.subst x v (vc.subst_env σ' P) ⋁ vc.subst x v (vc.subst_env σ' Q)) : by refl ... = (vc.subst_env (σ'[x↦v]) P ⋁ vc.subst x v (vc.subst_env σ' Q)) : by unfold vc.subst_env ... = (vc.subst_env (σ'[x↦v]) P ⋁ vc.subst_env (σ'[x↦v]) Q) : by unfold vc.subst_env end lemma vc.subst_env.implies {σ: env} {P Q: vc}: vc.subst_env σ (vc.implies P Q) = vc.implies (vc.subst_env σ P) (vc.subst_env σ Q) := have h1: vc.subst_env σ (vc.implies P Q) = vc.subst_env σ (vc.or P.not Q), from rfl, have vc.subst_env σ (vc.or P.not Q) = vc.or (vc.subst_env σ P.not) (vc.subst_env σ Q), from vc.subst_env.or, have h2: vc.subst_env σ (vc.implies P Q) = vc.or (vc.subst_env σ P.not) (vc.subst_env σ Q), from eq.trans h1 this, have vc.subst_env σ (vc.not P) = vc.not (vc.subst_env σ P), from vc.subst_env.not, show vc.subst_env σ (vc.implies P Q) = vc.or (vc.subst_env σ P).not (vc.subst_env σ Q), from this ▸ h2 lemma vc.subst_env.pre₁ {σ: env} {op: unop} {t: term}: vc.subst_env σ (vc.pre₁ op t) = vc.pre₁ op (term.subst_env σ t) := begin induction σ with σ' x v ih, show (vc.subst_env env.empty (vc.pre₁ op t) = vc.pre₁ op (term.subst_env env.empty t)), by calc vc.subst_env env.empty (vc.pre₁ op t) = (vc.pre₁ op t) : by unfold vc.subst_env ... = (vc.pre₁ op (term.subst_env env.empty t)) : by unfold term.subst_env, show (vc.subst_env (σ'[x↦v]) (vc.pre₁ op t) = vc.pre₁ op (term.subst_env (σ'[x↦v]) t)), by calc vc.subst_env (σ'[x↦v]) (vc.pre₁ op t) = vc.subst x v (vc.subst_env σ' (vc.pre₁ op t)) : by unfold vc.subst_env ... = vc.subst x v (vc.pre₁ op (term.subst_env σ' t)) : by rw[ih] ... = vc.pre₁ op (term.subst x v (term.subst_env σ' t)) : by unfold vc.subst ... = vc.pre₁ op (term.subst_env (σ'[x↦v]) t) : by unfold term.subst_env end lemma vc.subst_env.pre₂ {σ: env} {op: binop} {t₁ t₂: term}: vc.subst_env σ (vc.pre₂ op t₁ t₂) = vc.pre₂ op (term.subst_env σ t₁) (term.subst_env σ t₂) := begin induction σ with σ' x v ih, show (vc.subst_env env.empty (vc.pre₂ op t₁ t₂) = vc.pre₂ op (term.subst_env env.empty t₁) (term.subst_env env.empty t₂)), by calc vc.subst_env env.empty (vc.pre₂ op t₁ t₂) = (vc.pre₂ op t₁ t₂) : by unfold vc.subst_env ... = (vc.pre₂ op (term.subst_env env.empty t₁) t₂) : by unfold term.subst_env ... = (vc.pre₂ op (term.subst_env env.empty t₁) (term.subst_env env.empty t₂)) : by unfold term.subst_env, show (vc.subst_env (σ'[x↦v]) (vc.pre₂ op t₁ t₂) = vc.pre₂ op (term.subst_env (σ'[x↦v]) t₁) (term.subst_env (σ'[x↦v]) t₂)), by calc vc.subst_env (σ'[x↦v]) (vc.pre₂ op t₁ t₂) = vc.subst x v (vc.subst_env σ' (vc.pre₂ op t₁ t₂)) : by unfold vc.subst_env ... = vc.subst x v (vc.pre₂ op (term.subst_env σ' t₁) (term.subst_env σ' t₂)) : by rw[ih] ... = vc.pre₂ op (term.subst x v (term.subst_env σ' t₁)) (term.subst x v (term.subst_env σ' t₂)) : by unfold vc.subst ... = vc.pre₂ op (term.subst_env (σ'[x↦v]) t₁) (term.subst x v (term.subst_env σ' t₂)) : by unfold term.subst_env ... = vc.pre₂ op (term.subst_env (σ'[x↦v]) t₁) (term.subst_env (σ'[x↦v]) t₂) : by unfold term.subst_env end lemma vc.subst_env.pre {σ: env} {t₁ t₂: term}: vc.subst_env σ (vc.pre t₁ t₂) = vc.pre (term.subst_env σ t₁) (term.subst_env σ t₂) := begin induction σ with σ' x v ih, show (vc.subst_env env.empty (vc.pre t₁ t₂) = vc.pre (term.subst_env env.empty t₁) (term.subst_env env.empty t₂)), by calc vc.subst_env env.empty (vc.pre t₁ t₂) = (vc.pre t₁ t₂) : by unfold vc.subst_env ... = (vc.pre (term.subst_env env.empty t₁) t₂) : by unfold term.subst_env ... = (vc.pre (term.subst_env env.empty t₁) (term.subst_env env.empty t₂)) : by unfold term.subst_env, show (vc.subst_env (σ'[x↦v]) (vc.pre t₁ t₂) = vc.pre (term.subst_env (σ'[x↦v]) t₁) (term.subst_env (σ'[x↦v]) t₂)), by calc vc.subst_env (σ'[x↦v]) (vc.pre t₁ t₂) = vc.subst x v (vc.subst_env σ' (vc.pre t₁ t₂)) : by unfold vc.subst_env ... = vc.subst x v (vc.pre (term.subst_env σ' t₁) (term.subst_env σ' t₂)) : by rw[ih] ... = vc.pre (term.subst x v (term.subst_env σ' t₁)) (term.subst x v (term.subst_env σ' t₂)) : by unfold vc.subst ... = vc.pre (term.subst_env (σ'[x↦v]) t₁) (term.subst x v (term.subst_env σ' t₂)) : by unfold term.subst_env ... = vc.pre (term.subst_env (σ'[x↦v]) t₁) (term.subst_env (σ'[x↦v]) t₂) : by unfold term.subst_env end lemma vc.subst_env.post {σ: env} {t₁ t₂: term}: vc.subst_env σ (vc.post t₁ t₂) = vc.post (term.subst_env σ t₁) (term.subst_env σ t₂) := begin induction σ with σ' x v ih, show (vc.subst_env env.empty (vc.post t₁ t₂) = vc.post (term.subst_env env.empty t₁) (term.subst_env env.empty t₂)), by calc vc.subst_env env.empty (vc.post t₁ t₂) = (vc.post t₁ t₂) : by unfold vc.subst_env ... = (vc.post (term.subst_env env.empty t₁) t₂) : by unfold term.subst_env ... = (vc.post (term.subst_env env.empty t₁) (term.subst_env env.empty t₂)) : by unfold term.subst_env, show (vc.subst_env (σ'[x↦v]) (vc.post t₁ t₂) = vc.post (term.subst_env (σ'[x↦v]) t₁) (term.subst_env (σ'[x↦v]) t₂)), by calc vc.subst_env (σ'[x↦v]) (vc.post t₁ t₂) = vc.subst x v (vc.subst_env σ' (vc.post t₁ t₂)) : by unfold vc.subst_env ... = vc.subst x v (vc.post (term.subst_env σ' t₁) (term.subst_env σ' t₂)) : by rw[ih] ... = vc.post (term.subst x v (term.subst_env σ' t₁)) (term.subst x v (term.subst_env σ' t₂)) : by unfold vc.subst ... = vc.post (term.subst_env (σ'[x↦v]) t₁) (term.subst x v (term.subst_env σ' t₂)) : by unfold term.subst_env ... = vc.post (term.subst_env (σ'[x↦v]) t₁) (term.subst_env (σ'[x↦v]) t₂) : by unfold term.subst_env end lemma vc.subst_env.univ_not_in {σ: env} {x: var} {P: vc}: (x ∉ σ) → (vc.subst_env σ (vc.univ x P) = vc.univ x (vc.subst_env σ P)) := begin assume x_not_in_σ, induction σ with σ' y v ih, show (vc.subst_env env.empty (vc.univ x P) = vc.univ x (vc.subst_env env.empty P)), by calc vc.subst_env env.empty (vc.univ x P) = (vc.univ x P) : by unfold vc.subst_env ... = vc.univ x (vc.subst_env env.empty P) : by unfold vc.subst_env, show (vc.subst_env (σ'[y↦v]) (vc.univ x P) = vc.univ x (vc.subst_env (σ'[y↦v]) P)), from ( have ¬ (x = y ∨ x ∈ σ'), from env.contains.same.inv x_not_in_σ, have x_neq_y: x ≠ y, from (not_or_distrib.mp this).left, have x ∉ σ', from (not_or_distrib.mp this).right, have h: vc.subst_env σ' (vc.univ x P) = vc.univ x (vc.subst_env σ' P), from ih this, calc vc.subst_env (σ'[y↦v]) (vc.univ x P) = vc.subst y v (vc.subst_env σ' (vc.univ x P)) : by unfold vc.subst_env ... = vc.subst y v (vc.univ x (vc.subst_env σ' P)) : by rw[h] ... = vc.univ x (if y = x then vc.subst_env σ' P else (vc.subst_env σ' P).subst y v) : by unfold vc.subst ... = vc.univ x ((vc.subst_env σ' P).subst y v) : by simp[x_neq_y.symm] ... = vc.univ x (vc.subst_env (σ'[y↦v]) P) : by unfold vc.subst_env ) end lemma vc.subst_env.univ {σ: env} {x: var} {P: vc}: (vc.subst_env σ (vc.univ x P) = vc.univ x (vc.subst_env (σ.without x) P)) := begin induction σ with σ' y v ih, show (vc.subst_env env.empty (vc.univ x P) = vc.univ x (vc.subst_env (env.empty.without x) P)), by calc vc.subst_env env.empty (vc.univ x P) = (vc.univ x P) : by unfold vc.subst_env ... = vc.univ x (vc.subst_env env.empty P) : by unfold vc.subst_env, show (vc.subst_env (σ'[y↦v]) (vc.univ x P) = vc.univ x (vc.subst_env ((σ'[y↦v]).without x) P)), by begin unfold vc.subst_env, by_cases (y = x) with h1, rw[←h1], rw[←h1] at ih, unfold env.without, simp, have : y ∉ FV (vc.subst_env σ' (vc.univ y P)), from ( assume : y ∈ FV (vc.subst_env σ' (vc.univ y P)), have y ∈ FV (vc.univ y P), from vc.free_of_free_subst_env this, show «false», from free_in_vc.univ.same.inv this ), have h2: (vc.subst y v (vc.subst_env σ' (vc.univ y P)) = vc.subst_env σ' (vc.univ y P)), from unchanged_of_subst_nonfree_vc this, rw[h2], from ih, unfold env.without, simp[h1], unfold vc.subst_env, have : (vc.subst y v (vc.univ x (vc.subst_env (env.without σ' x) P)) = vc.univ x (vc.subst y v (vc.subst_env (env.without σ' x) P))), by { unfold vc.subst, simp[h1] }, rw[←this], congr, from ih end end lemma term.closed_subst.value {v: value} {σ: env}: closed_subst σ (term.value v) := assume x: var, assume : x ∈ FV (term.value v), show x ∈ σ.dom, from absurd this free_in_term.value.inv lemma prop.closed_subst.term {t: term} {σ: env}: closed_subst σ t → closed_subst σ (prop.term t) := assume t_closed: closed_subst σ t, show closed_subst σ (prop.term t), from ( assume x: var, assume : x ∈ FV (prop.term t), have free_in_term x t, from free_in_prop.term.inv this, show x ∈ σ.dom, from t_closed this ) lemma prop.closed_subst.and {P Q: prop} {σ: env}: closed_subst σ P → closed_subst σ Q → closed_subst σ (P ⋀ Q) := assume P_closed: closed_subst σ P, assume Q_closed: closed_subst σ Q, show closed_subst σ (P ⋀ Q), from ( assume x: var, assume : x ∈ FV (P ⋀ Q), or.elim (free_in_prop.and.inv this) ( assume : x ∈ FV P, show x ∈ σ.dom, from P_closed this ) ( assume : x ∈ FV Q, show x ∈ σ.dom, from Q_closed this ) ) lemma prop.closed_subst.or {P Q: prop} {σ: env}: closed_subst σ P → closed_subst σ Q → closed_subst σ (P ⋁ Q) := assume P_closed_subst: closed_subst σ P, assume Q_closed_subst: closed_subst σ Q, show closed_subst σ (P ⋁ Q), from ( assume x: var, assume : x ∈ FV (P ⋁ Q), or.elim (free_in_prop.or.inv this) ( assume : x ∈ FV P, show x ∈ σ.dom, from P_closed_subst this ) ( assume : x ∈ FV Q, show x ∈ σ.dom, from Q_closed_subst this ) ) lemma prop.closed_subst.not {P: prop} {σ: env}: closed_subst σ P → closed_subst σ P.not := assume P_closed_subst: closed_subst σ P, show closed_subst σ P.not, from ( assume x: var, assume : x ∈ FV P.not, have x ∈ FV P, from free_in_prop.not.inv this, show x ∈ σ.dom, from P_closed_subst this ) lemma prop.closed_subst.implies {P Q: prop} {σ: env}: closed_subst σ P → closed_subst σ Q → closed_subst σ (prop.implies P Q) := assume P_closed_subst: closed_subst σ P, have P_not_closed_subst: closed_subst σ P.not, from prop.closed_subst.not P_closed_subst, assume Q_closed_subst: closed_subst σ Q, show closed_subst σ (P.not ⋁ Q), from prop.closed_subst.or P_not_closed_subst Q_closed_subst lemma prop.closed_subst.and.inv {P Q: prop} {σ: env}: closed_subst σ (P ⋀ Q) → (closed_subst σ P ∧ closed_subst σ Q) := assume P_and_Q_closed_subst: closed_subst σ (P ⋀ Q), have P_closed_subst: closed_subst σ P, from ( assume x: var, assume : x ∈ FV P, have x ∈ FV (P ⋀ Q), from free_in_prop.and₁ this, show x ∈ σ.dom, from P_and_Q_closed_subst this ), have Q_closed_subst: closed_subst σ Q, from ( assume x: var, assume : x ∈ FV Q, have x ∈ FV (P ⋀ Q), from free_in_prop.and₂ this, show x ∈ σ.dom, from P_and_Q_closed_subst this ), ⟨P_closed_subst, Q_closed_subst⟩ lemma prop.closed_subst.or.inv {P Q: prop} {σ: env}: closed_subst σ (P ⋁ Q) → (closed_subst σ P ∧ closed_subst σ Q) := assume P_or_Q_closed_subst: closed_subst σ (P ⋁ Q), have P_closed_subst: closed_subst σ P, from ( assume x: var, assume : x ∈ FV P, have x ∈ FV (P ⋁ Q), from free_in_prop.or₁ this, show x ∈ σ.dom, from P_or_Q_closed_subst this ), have Q_closed_subst: closed_subst σ Q, from ( assume x: var, assume : x ∈ FV Q, have x ∈ FV (P ⋁ Q), from free_in_prop.or₂ this, show x ∈ σ.dom, from P_or_Q_closed_subst this ), ⟨P_closed_subst, Q_closed_subst⟩ lemma prop.closed_subst.not.inv {P: prop} {σ: env}: closed_subst σ P.not → closed_subst σ P := assume P_not_closed_subst: closed_subst σ P.not, show closed_subst σ P, from ( assume x: var, assume : x ∈ FV P, have x ∈ FV P.not, from free_in_prop.not this, show x ∈ σ.dom, from P_not_closed_subst this ) lemma prop.closed_subst.implies.inv {P Q: prop} {σ: env}: closed_subst σ (prop.implies P Q) → closed_subst σ P ∧ closed_subst σ Q := assume P_not_or_Q_closed_subst: closed_subst σ (P.not ⋁ Q), have P_not_closed_subst: closed_subst σ P.not, from (prop.closed_subst.or.inv P_not_or_Q_closed_subst).left, have P_closed_subst: closed_subst σ P, from prop.closed_subst.not.inv P_not_closed_subst, have Q_closed_subst: closed_subst σ Q, from (prop.closed_subst.or.inv P_not_or_Q_closed_subst).right, ⟨P_closed_subst, Q_closed_subst⟩ lemma prop.closed_subst.subst {P: prop} {σ: env} {x: var} {v: value}: closed_subst σ P → closed_subst σ (prop.subst x v P) := assume P_closed_subst: closed_subst σ P, show closed_subst σ (prop.subst x v P), from ( assume y: var, assume : y ∈ FV (prop.subst x v P), have y ∈ FV P, from (free_of_subst_prop this).right, show y ∈ σ.dom, from P_closed_subst this ) lemma vc.closed_subst.term {t: term} {σ: env}: closed_subst σ t → closed_subst σ (vc.term t) := assume t_closed: closed_subst σ t, show closed_subst σ (vc.term t), from ( assume x: var, assume : x ∈ FV (vc.term t), have free_in_term x t, from free_in_vc.term.inv this, show x ∈ σ.dom, from t_closed this ) lemma vc.closed_subst.and {P Q: vc} {σ: env}: closed_subst σ P → closed_subst σ Q → closed_subst σ (P ⋀ Q) := assume P_closed_subst: closed_subst σ P, assume Q_closed_subst: closed_subst σ Q, show closed_subst σ (P ⋀ Q), from ( assume x: var, assume : x ∈ FV (P ⋀ Q), or.elim (free_in_vc.and.inv this) ( assume : x ∈ FV P, show x ∈ σ.dom, from P_closed_subst this ) ( assume : x ∈ FV Q, show x ∈ σ.dom, from Q_closed_subst this ) ) lemma vc.closed_subst.or {P Q: vc} {σ: env}: closed_subst σ P → closed_subst σ Q → closed_subst σ (P ⋁ Q) := assume P_closed_subst: closed_subst σ P, assume Q_closed_subst: closed_subst σ Q, show closed_subst σ (P ⋁ Q), from ( assume x: var, assume : x ∈ FV (P ⋁ Q), or.elim (free_in_vc.or.inv this) ( assume : x ∈ FV P, show x ∈ σ.dom, from P_closed_subst this ) ( assume : x ∈ FV Q, show x ∈ σ.dom, from Q_closed_subst this ) ) lemma vc.closed_subst.not {P: vc} {σ: env}: closed_subst σ P → closed_subst σ P.not := assume P_closed_subst: closed_subst σ P, show closed_subst σ P.not, from ( assume x: var, assume : x ∈ FV P.not, have x ∈ FV P, from free_in_vc.not.inv this, show x ∈ σ.dom, from P_closed_subst this ) lemma vc.closed_subst.implies {P Q: vc} {σ: env}: closed_subst σ P → closed_subst σ Q → closed_subst σ (vc.implies P Q) := assume P_closed_subst: closed_subst σ P, have P_not_closed_subst: closed_subst σ P.not, from vc.closed_subst.not P_closed_subst, assume Q_closed_subst: closed_subst σ Q, show closed_subst σ (P.not ⋁ Q), from vc.closed_subst.or P_not_closed_subst Q_closed_subst lemma vc.closed_subst.term.inv {t: term} {σ: env}: closed_subst σ (vc.term t) → closed_subst σ t := assume t_closed: closed_subst σ (vc.term t), show closed_subst σ t, from ( assume x: var, assume : x ∈ FV t, have free_in_vc x (vc.term t), from free_in_vc.term this, show x ∈ σ.dom, from t_closed this ) lemma vc.closed_subst.and.inv {P Q: vc} {σ: env}: closed_subst σ (P ⋀ Q) → (closed_subst σ P ∧ closed_subst σ Q) := assume P_and_Q_closed_subst: closed_subst σ (P ⋀ Q), have P_closed_subst: closed_subst σ P, from ( assume x: var, assume : x ∈ FV P, have x ∈ FV (P ⋀ Q), from free_in_vc.and₁ this, show x ∈ σ.dom, from P_and_Q_closed_subst this ), have Q_closed_subst: closed_subst σ Q, from ( assume x: var, assume : x ∈ FV Q, have x ∈ FV (P ⋀ Q), from free_in_vc.and₂ this, show x ∈ σ.dom, from P_and_Q_closed_subst this ), ⟨P_closed_subst, Q_closed_subst⟩ lemma vc.closed_subst.or.inv {P Q: vc} {σ: env}: closed_subst σ (P ⋁ Q) → (closed_subst σ P ∧ closed_subst σ Q) := assume P_or_Q_closed_subst: closed_subst σ (P ⋁ Q), have P_closed_subst: closed_subst σ P, from ( assume x: var, assume : x ∈ FV P, have x ∈ FV (P ⋁ Q), from free_in_vc.or₁ this, show x ∈ σ.dom, from P_or_Q_closed_subst this ), have Q_closed_subst: closed_subst σ Q, from ( assume x: var, assume : x ∈ FV Q, have x ∈ FV (P ⋁ Q), from free_in_vc.or₂ this, show x ∈ σ.dom, from P_or_Q_closed_subst this ), ⟨P_closed_subst, Q_closed_subst⟩ lemma vc.closed_subst.not.inv {P: vc} {σ: env}: closed_subst σ P.not → closed_subst σ P := assume P_not_closed_subst: closed_subst σ P.not, show closed_subst σ P, from ( assume x: var, assume : x ∈ FV P, have x ∈ FV P.not, from free_in_vc.not this, show x ∈ σ.dom, from P_not_closed_subst this ) lemma vc.closed_subst.implies.inv {P Q: vc} {σ: env}: closed_subst σ (vc.implies P Q) → closed_subst σ P ∧ closed_subst σ Q := assume P_not_or_Q_closed_subst: closed_subst σ (P.not ⋁ Q), have P_not_closed_subst: closed_subst σ P.not, from (vc.closed_subst.or.inv P_not_or_Q_closed_subst).left, have P_closed_subst: closed_subst σ P, from vc.closed_subst.not.inv P_not_closed_subst, have Q_closed_subst: closed_subst σ Q, from (vc.closed_subst.or.inv P_not_or_Q_closed_subst).right, ⟨P_closed_subst, Q_closed_subst⟩ lemma to_vc_closed_from_prop_closed {P: prop} {σ: env}: closed_subst σ P → closed_subst σ P.to_vc := assume h1: closed_subst σ P, show closed_subst σ P.to_vc, from ( assume x: var, assume : x ∈ FV P.to_vc, have x ∈ FV P, from set.mem_of_mem_of_subset this free_in_prop_of_free_in_to_vc, show x ∈ σ.dom, from h1 this ) lemma erased_p_closed_from_prop_closed {P: prop} {σ: env}: closed_subst σ P → closed_subst σ P.erased_p := assume h1: closed_subst σ P, show closed_subst σ P.erased_p, from ( assume x: var, assume : x ∈ FV P.erased_p, have x ∈ FV P, from set.mem_of_mem_of_subset this free_in_prop_of_free_in_erased.left, show x ∈ σ.dom, from h1 this ) lemma erased_n_closed_from_prop_closed {P: prop} {σ: env}: closed_subst σ P → closed_subst σ P.erased_n := assume h1: closed_subst σ P, show closed_subst σ P.erased_n, from ( assume x: var, assume : x ∈ FV P.erased_n, have x ∈ FV P, from set.mem_of_mem_of_subset this free_in_prop_of_free_in_erased.right, show x ∈ σ.dom, from h1 this ) lemma subst_closed_of_forall_closed {P: prop} {σ: env} {x: var} {v: value}: closed_subst σ (prop.forallc x P) → closed_subst σ (prop.subst x v P) := assume h1: closed_subst σ (prop.forallc x P), show closed_subst σ (prop.subst x v P), from ( assume y: var, assume : y ∈ FV (prop.subst x v P), have y ≠ x ∧ y ∈ FV P, from free_of_subst_prop this, have y ∈ FV (prop.forallc x P), from free_in_prop.forallc this.left this.right, show y ∈ σ, from h1 this ) lemma contains_of_free_in_nonempty_env {σ: env} {x y: var} {v: value}: (x ≠ y → y ∈ σ) → y ∈ (σ[x↦v]) := assume ih: x ≠ y → y ∈ σ, if x_eq_y: x = y ∧ option.is_none (σ.apply y) then ( have h: σ[x↦v].apply x = (if x = x ∧ option.is_none (σ.apply x) then ↑v else σ.apply x), by unfold env.apply, have (if x = x ∧ option.is_none (σ.apply x) then ↑v else σ.apply x) = ↑v, by simp [x_eq_y], have σ[x↦v].apply x = ↑v, from eq.trans h this, have σ[x↦v].apply y = some v, from x_eq_y.left ▸ this, have ∃v', σ[x↦v] y = some v', from exists.intro v this, show y ∈ (σ[x↦v]), from env.contains_apply_equiv.right.mp this ) else ( have y ∈ σ, from ( have ¬(x = y) ∨ ¬(option.is_none (σ.apply y)), from not_and_distrib.mp x_eq_y, this.elim ( assume : x ≠ y, show y ∈ σ, from ih this ) ( assume : ¬(option.is_none (env.apply σ y)), have ¬(option.is_none (σ y)), from this, have option.is_some (σ y), from option.some_iff_not_none.mpr this, have ∃v', σ y = some v', from option.is_some_iff_exists.mp this, show y ∈ σ, from env.contains_apply_equiv.right.mp this ) ), let ⟨v', σ_has_y⟩ := (env.contains_apply_equiv.right.mpr this) in have h: σ[x↦v].apply y = (if x = y ∧ option.is_none (σ.apply y) then ↑v else σ.apply y), by unfold env.apply, have (if x = y ∧ option.is_none (σ.apply y) then ↑v else σ.apply y) = σ.apply y, by simp *, have σ[x↦v].apply y = σ.apply y, from this ▸ h, have σ[x↦v].apply y = some v', from eq.trans this σ_has_y, have ∃v', σ[x↦v] y = some v', from exists.intro v' this, show y ∈ (σ[x↦v]), from env.contains_apply_equiv.right.mp this ) lemma contains_of_free_eq_value {P: prop} {σ: env} {x y: var} {v: value}: x ∈ FV (P ⋀ (y ≡ v)) → (x ∈ FV P → x ∈ σ) → x ∈ (σ[y↦v]) := assume x_free_in_P: x ∈ FV (P ⋀ (y ≡ v)), assume ih : x ∈ FV P → x ∈ σ, contains_of_free_in_nonempty_env ( assume x'_is_not_x: y ≠ x, have free_in_prop x P ∨ free_in_prop x (y ≡ v), from free_in_prop.and.inv x_free_in_P, or.elim this ( assume x_free_in_P: free_in_prop x P, show x ∈ σ, from ih x_free_in_P ) ( assume x_free_in_eq_v: free_in_prop x (y ≡ v), show x ∈ σ, by begin cases x_free_in_eq_v, case free_in_prop.term x_free_in_eq { cases x_free_in_eq, case free_in_term.binop₁ free_in_y { have y_is_x: (y = x), from (free_in_term.var.inv free_in_y).symm, contradiction }, case free_in_term.binop₂ free_in_v { cases free_in_v } } end ) ) lemma env.dom.inv {σ: env} {x: var} {v: value}: (σ[x↦v]).dom = (σ.dom ∪ set.insert x ∅) := set.eq_of_subset_of_subset ( assume y: var, assume : y ∈ (σ[x↦v]).dom, have y ∈ (σ[x↦v]), from this, or.elim (env.contains.inv this) ( assume : y = x, have y ∈ set.insert x ∅, from set.mem_singleton_of_eq this, show y ∈ (σ.dom ∪ set.insert x ∅), from set.mem_union_right σ.dom this ) ( assume : y ∈ σ, have y ∈ σ.dom, from this, show y ∈ (σ.dom ∪ set.insert x ∅), from set.mem_union_left (set.insert x ∅) this ) ) ( assume y: var, assume : y ∈ (σ.dom ∪ set.insert x ∅), or.elim (set.mem_or_mem_of_mem_union this) ( assume : y ∈ σ.dom, have y ∈ σ, from this, have y ∈ (σ[x↦v]), from env.contains.rest this, show y ∈ (σ[x↦v]).dom, from this ) ( assume : y ∈ set.insert x ∅, have y = x, from (set.mem_singleton_iff y x).mp this, have y ∈ (σ[x↦v]), from this ▸ env.contains.same, show y ∈ (σ[x↦v]).dom, from this ) ) lemma env.dom.two_elems {σ: env} {x y: var} {v₁ v₂: value}: (σ[x↦v₁][y↦v₂]).dom = σ.dom ∪ {x, y} := by calc (σ[x↦v₁][y↦v₂]).dom = (σ[x↦v₁]).dom ∪ set.insert y ∅ : env.dom.inv ... = σ.dom ∪ set.insert x ∅ ∪ set.insert y ∅ : by rw[env.dom.inv] ... = σ.dom ∪ (set.insert x ∅ ∪ set.insert y ∅) : by rw[set.union_assoc] ... = σ.dom ∪ {x, y} : by rw[set.two_elems_of_insert] lemma env.apply_of_contains {σ: env} {x: var} {v: value}: x ∉ σ → ((σ[x↦v]) x = v) := begin intro h, change (env.apply (σ[x↦v]) x = some v), unfold env.apply, by_cases (x = x ∧ (option.is_none (env.apply σ x))) with h2, simp[h2], refl, simp at h2, have h3, from env.contains_apply_equiv.left.mpr h, have h4: (env.apply σ x = none), from h3, rw[h4] at h2, unfold option.is_none at h2, have h5: (↑tt = «false»), from eq_false_intro h2, have h6: (↑tt = «true»), by simp, have h7: («false» = «true»), from eq.trans h5.symm h6, have h8: «true», from trivial, have r9: «false», from h7.symm ▸ h8, contradiction end lemma env.equiv_of_rest_and_same {σ σ': env} {x: var} {v: value}: (∀y, y ∈ σ → (σ y = σ' y)) → x ∉ σ → (σ' x = v) → (∀y, y ∈ (σ[x↦v]) → ((σ[x↦v]) y = σ' y)) := assume h1: (∀y, y ∈ σ → (σ y = σ' y)), assume h2: x ∉ σ, assume h3: σ' x = v, assume y: var, assume h4: y ∈ (σ[x↦v]), if h: x = y then ( have h5: (σ[x↦v]) y = v, from h ▸ env.apply_of_contains h2, show ((σ[x↦v]) y = σ' y), from eq.trans h5 (h ▸ h3.symm) ) else ( have y ∈ σ, from ( have y = x ∨ y ∈ σ, from env.contains.inv h4, or.elim this.symm id ( assume : y = x, show y ∈ σ, from absurd this.symm h ) ), have h6: σ y = σ' y, from h1 y this, have env.apply (σ[x↦v]) y = σ.apply y, by { unfold env.apply, simp[h] }, have (σ[x↦v]) y = σ y, from this, show ((σ[x↦v]) y = σ' y), from this.symm ▸ h6 ) lemma env.equiv_of_not_contains {σ σ': env} {x: var} {v: value}: (∀y, y ∈ σ → (σ y = σ' y)) → x ∉ σ → (∀y, y ∈ σ → (σ y = (σ'[x↦v]) y)) := assume h1: (∀y, y ∈ σ → (σ y = σ' y)), assume h2: x ∉ σ, assume y: var, assume h4: y ∈ σ, if h: x = y then ( have x ∈ σ, from h.symm ▸ h4, show σ y = (σ'[x↦v]) y, from absurd this h2 ) else ( have h2: σ y = σ' y, from h1 y h4, have (∃v, σ y = some v), from env.contains_apply_equiv.right.mpr h4, have option.is_some (σ y), from option.is_some_iff_exists.mpr this, have ¬ option.is_none (σ y), from option.some_iff_not_none.mp this, have h5: ¬ (x = y ∧ option.is_none (env.apply σ' y)), from not_and_distrib.mpr (or.inl h), have env.apply (σ'[x↦v]) y = σ' y, by { unfold env.apply, simp[h5], refl }, show σ y = (σ'[x↦v]) y, from eq.trans h2 this.symm ) lemma env.apply_of_rest_apply {σ: env} {x y: var} {vx vy: value}: (σ x = vx) → ((σ[y↦vy]) x = vx) := begin assume h1: (env.apply σ x = some vx), change (env.apply (σ[y↦vy]) x = ↑vx), unfold env.apply, have h2, from option.is_some_iff_exists.mpr (exists.intro vx h1), have h3, from option.some_iff_not_none.mp h2, have h4: ¬ (y = x ∧ (option.is_none (env.apply σ x))), from not_and_distrib.mpr (or.inr h3), simp[h4], from h1 end lemma term.subst_env.order {t: term} {σ: env} {x: var} {v: value}: (x ∉ σ) ∨ (σ x = v) → (term.subst_env σ (term.subst x v t) = term.subst x v (term.subst_env σ t)) := begin assume h1, induction t with v' y unop t₁ t₁_ih binop t₂ t₃ t₂_ih t₃_ih t₄ t₅ t₄_ih t₅_ih, show (term.subst_env σ (term.subst x v (term.value v')) = term.subst x v (term.subst_env σ (term.value v'))), by begin change (term.subst_env σ (term.subst x v (term.value v')) = term.subst x v (term.subst_env σ v')), rw[term.subst_env.value], unfold term.subst, rw[term.subst_env.value], change (↑v' = term.subst x v (term.value v')), unfold term.subst end, show (term.subst_env σ (term.subst x v (term.var y)) = term.subst x v (term.subst_env σ (term.var y))), by begin by_cases (x = y) with h, simp[h], rw[h] at h1, unfold term.subst, simp, cases h1, have : (σ y = none), from env.contains_apply_equiv.left.mpr a, have h2: (term.subst_env σ (term.var y) = y), from term.subst_env.var.left.mp this, simp[h2], rw[term.subst_env.value], change (↑v = term.subst y v (term.var y)), unfold term.subst, simp, have h2: (term.subst_env σ (term.var y) = v), from (term.subst_env.var.right v).mp a, rw[h2], change (term.subst_env σ ↑v = term.subst y v (term.value v)), unfold term.subst, rw[term.subst_env.value], have h2: (term.subst x v (term.var y) = y), from term.subst.var.diff h, rw[h2], by_cases (y ∈ σ) with h3, have h4, from env.contains_apply_equiv.right.mpr h3, cases h4 with v' h5, have h6: (term.subst_env σ y = v'), from (term.subst_env.var.right v').mp h5, rw[h6], change (↑v' = term.subst x v (term.subst_env σ ↑y)), rw[h6], change (↑v' = term.subst x v (term.value v')), unfold term.subst, have : (σ y = none), from env.contains_apply_equiv.left.mpr h3, have h4: (term.subst_env σ (term.var y) = y), from term.subst_env.var.left.mp this, simp[h4], change (term.subst_env σ (term.var y) = term.subst x v (term.var y)), rw[h2], rw[h4] end, show (term.subst_env σ (term.subst x v (term.unop unop t₁)) = term.subst x v (term.subst_env σ (term.unop unop t₁))), by begin rw[term.subst_env.unop], unfold term.subst, rw[term.subst_env.unop], congr, from t₁_ih end, show (term.subst_env σ (term.subst x v (term.binop binop t₂ t₃)) = term.subst x v (term.subst_env σ (term.binop binop t₂ t₃))), by begin rw[term.subst_env.binop], unfold term.subst, rw[term.subst_env.binop], congr, rw[t₂_ih], rw[t₃_ih] end, show (term.subst_env σ (term.subst x v (term.app t₄ t₅)) = term.subst x v (term.subst_env σ (term.app t₄ t₅))), by begin rw[term.subst_env.app], unfold term.subst, rw[term.subst_env.app], congr, rw[t₄_ih], rw[t₅_ih] end end lemma term.substt_env.order {t' t: term} {σ: env} {x: var}: closed t' → (x ∉ σ) → (term.subst_env σ (term.substt x t' t) = term.substt x t' (term.subst_env σ t)) := begin assume t'_closed, assume h1, induction t with v y unop t₁ t₁_ih binop t₂ t₃ t₂_ih t₃_ih t₄ t₅ t₄_ih t₅_ih, show (term.subst_env σ (term.substt x t' (term.value v)) = term.substt x t' (term.subst_env σ (term.value v))), by begin change (term.subst_env σ (term.substt x t' (term.value v)) = term.substt x t' (term.subst_env σ v)), rw[term.subst_env.value], unfold term.substt, rw[term.subst_env.value], change (↑v = term.substt x t' (term.value v)), unfold term.substt end, show (term.subst_env σ (term.substt x t' (term.var y)) = term.substt x t' (term.subst_env σ (term.var y))), by begin by_cases (x = y) with h, simp[h], rw[h] at h1, unfold term.substt, simp, have : (σ y = none), from env.contains_apply_equiv.left.mpr h1, have h2: (term.subst_env σ (term.var y) = y), from term.subst_env.var.left.mp this, simp[h2], rw[term.subst_env.closed t'_closed], change (t' = term.substt y t' (term.var y)), unfold term.substt, simp, have h2: (term.substt x t' (term.var y) = y), from term.substt.var.diff h, rw[h2], by_cases (y ∈ σ) with h3, have h4, from env.contains_apply_equiv.right.mpr h3, cases h4 with v' h5, have h6: (term.subst_env σ y = v'), from (term.subst_env.var.right v').mp h5, rw[h6], change (↑v' = term.substt x t' (term.subst_env σ ↑y)), rw[h6], change (↑v' = term.substt x t' (term.value v')), unfold term.substt, have : (σ y = none), from env.contains_apply_equiv.left.mpr h3, have h4: (term.subst_env σ (term.var y) = y), from term.subst_env.var.left.mp this, simp[h4], change (term.subst_env σ (term.var y) = term.substt x t' (term.var y)), rw[h2], rw[h4] end, show (term.subst_env σ (term.substt x t' (term.unop unop t₁)) = term.substt x t' (term.subst_env σ (term.unop unop t₁))), by begin rw[term.subst_env.unop], unfold term.substt, rw[term.subst_env.unop], congr, from t₁_ih end, show (term.subst_env σ (term.substt x t' (term.binop binop t₂ t₃)) = term.substt x t' (term.subst_env σ (term.binop binop t₂ t₃))), by begin rw[term.subst_env.binop], unfold term.substt, rw[term.subst_env.binop], congr, rw[t₂_ih], rw[t₃_ih] end, show (term.subst_env σ (term.substt x t' (term.app t₄ t₅)) = term.substt x t' (term.subst_env σ (term.app t₄ t₅))), by begin rw[term.subst_env.app], unfold term.substt, rw[term.subst_env.app], congr, rw[t₄_ih], rw[t₅_ih] end end lemma term.subst_env_inner {t: term} {σ: env} {x: var} {v: value}: (σ x = some v) → (term.subst_env σ (term.subst x v t) = term.subst_env σ t) := begin assume x_is_v, induction σ with σ₁ y v' ih, show (term.subst_env env.empty (term.subst x v t) = term.subst_env env.empty t), by cases x_is_v, show (term.subst_env (σ₁[y↦v']) (term.subst x v t) = term.subst_env (σ₁[y↦v']) t), by begin unfold term.subst_env, have h2: (env.apply (σ₁[y↦v']) x = some v), from x_is_v, unfold env.apply at h2, by_cases (y = x ∧ (option.is_none (env.apply σ₁ x))) with h3, simp[h3] at h2, have h4: (v' = v), from option.some.inj h2, simp[h3], have h5: (σ₁ x = none), from option.is_none.inv.mpr h3.right, have h6: x ∉ σ₁, from env.contains_apply_equiv.left.mp h5, rw[h4], have h7: x ∉ FV (term.subst x v t), from term.not_free_of_subst, have h8: x ∉ FV (term.subst_env σ₁ (term.subst x v t)), have : ¬(free_in_term x (term.subst x v t) ∧ x ∉ σ₁), by begin assume : free_in_term x (term.subst x v t) ∧ x ∉ σ₁, show «false», from h7 this.left end, from mt free_of_subst_env_term this, have h9: (term.subst x v (term.subst_env σ₁ (term.subst x v t)) = (term.subst_env σ₁ (term.subst x v t))), from unchanged_of_subst_nonfree_term h8, rw[h9], from term.subst_env.order (or.inl h6), simp[h3] at h2, have h4, from ih h2, congr, from h4 end end lemma term.subst_env_twice {t: term} {σ: env}: term.subst_env σ (term.subst_env σ t) = term.subst_env σ t := begin induction σ with σ' x v ih, show (term.subst_env env.empty (term.subst_env env.empty t) = term.subst_env env.empty t), by begin unfold term.subst_env end, show (term.subst_env (σ'[x↦v]) (term.subst_env (σ'[x↦v]) t) = term.subst_env (σ'[x↦v]) t), by begin by_cases (x ∈ σ') with h1, unfold term.subst_env, have h2: x ∉ FV (term.subst_env σ' t), from term.not_free_of_subst_env h1, have h3: (term.subst x v (term.subst_env σ' t) = (term.subst_env σ' t)), from unchanged_of_subst_nonfree_term h2, rw[h3], have h4: x ∉ FV (term.subst_env σ' (term.subst_env σ' t)), from term.not_free_of_subst_env h1, have h5: (term.subst x v (term.subst_env σ' (term.subst_env σ' t)) = (term.subst_env σ' (term.subst_env σ' t))), from unchanged_of_subst_nonfree_term h4, rw[h5], from ih, have h6: (term.subst_env (σ'[x↦v]) t = term.subst x v (term.subst_env σ' t)), by unfold term.subst_env, rw[h6], have h7: (env.apply (σ'[x↦v]) x = v), from env.apply_of_contains h1, have h8: (term.subst_env (σ'[x↦v]) (term.subst x v (term.subst_env σ' t)) = term.subst_env (σ'[x↦v]) (term.subst_env σ' t)), from term.subst_env_inner h7, rw[h8], unfold term.subst_env, congr, from ih end end lemma env.dom_subset_of_equivalent_env {σ₁ σ₂: env}: (∀z, z ∈ σ₁ → (σ₁ z = σ₂ z)) → (σ₁.dom ⊆ σ₂.dom) := assume env_equiv: (∀z, z ∈ σ₁ → (σ₁ z = σ₂ z)), assume x: var, assume : x ∈ σ₁.dom, have h1: x ∈ σ₁, from this, have ∃v, σ₁ x = some v, from env.contains_apply_equiv.right.mpr h1, let ⟨v, h2⟩ := this in have σ₁ x = σ₂ x, from env_equiv x h1, have σ₂ x = some v, from eq.trans this.symm h2, show x ∈ σ₂, from env.contains_apply_equiv.right.mp (exists.intro v this) lemma env.empty_of_dom_empty {σ: env}: (σ.dom = ∅) → (σ = env.empty) := begin assume h1: (σ.dom = ∅), cases σ with σ' x v, refl, have h2, from set.subset_of_eq h1, have h3: x ∈ (σ'[x↦v]), from env.contains.same, have h4: x ∈ (σ'[x↦v]).dom, from h3, have h5, from set.mem_of_subset_of_mem h2 h4, have : x ∉ ∅, from set.not_mem_empty x, contradiction end lemma env.empty_dom_is_empty: (env.empty.dom = ∅) := begin apply set.eq_of_subset_of_subset, assume x: var, assume : x ∈ env.dom env.empty, have h2: x ∈ env.empty, from this, cases h2, assume x: var, assume : x ∈ ∅, have : x ∉ ∅, from set.not_mem_empty x, contradiction end lemma term.subst_env_twice_equiv {t: term} {σ₁ σ₂: env}: (∀z, z ∈ σ₁ → (σ₁ z = σ₂ z)) → (term.subst_env σ₁ (term.subst_env σ₂ t) = term.subst_env σ₂ t) := begin assume env_equiv: ∀z, z ∈ σ₁ → (σ₁ z = σ₂ z), -- have env_subst: σ₁.dom ⊆ σ₂.dom, from env.dom_subset_of_equivalent_env env_equiv, induction σ₁ with σ' x v ih, show (term.subst_env env.empty (term.subst_env σ₂ t) = term.subst_env σ₂ t), by unfold term.subst_env, show (term.subst_env (σ'[x↦v]) (term.subst_env σ₂ t) = term.subst_env σ₂ t), by begin unfold term.subst_env, have h1: (∀ (z : var), z ∈ σ' → (σ' z = σ₂ z)), by begin assume z: var, assume h2: z ∈ σ', have h3: (env.apply (σ'[x↦v]) z = σ₂ z), from env_equiv z (env.contains.rest h2), unfold env.apply at h3, have h4, from env.contains_apply_equiv.right.mpr h2, have h5, from option.is_some_iff_exists.mpr h4, have h6, from option.some_iff_not_none.mp h5, have h7, from not_and_of_not_right (x = z) h6, have h8: (ite (x = z ∧ (option.is_none (env.apply σ' z))) ↑v (env.apply σ' z) = (env.apply σ' z)), from ite.if_false h7, rw[h8] at h3, from h3 end, have h2: (term.subst_env σ' (term.subst_env σ₂ t) = term.subst_env σ₂ t), from ih h1, rw[h2], have h3: x ∈ σ₂, by begin have h3: (env.apply (σ'[x↦v]) x = σ₂ x), from env_equiv x env.contains.same, unfold env.apply at h3, by_cases (x ∈ σ') with h4, have h5, from env.contains_apply_equiv.right.mpr h4, have h6, from option.is_some_iff_exists.mpr h5, have h7, from option.some_iff_not_none.mp h6, have h8, from not_and_of_not_right (x = x) h7, have h9: (ite (x = x ∧ (option.is_none (env.apply σ' x))) ↑v (env.apply σ' x) = (env.apply σ' x)), from ite.if_false h8, rw[h9] at h3, have h10: (σ' x = σ₂ x), from h3, rw[h10] at h5, from env.contains_apply_equiv.right.mp h5, have h5, from env.contains_apply_equiv.left.mpr h4, have h6, from option.is_none.inv.mp h5, have h7: (ite (x = x ∧ (option.is_none (env.apply σ' x))) ↑v (env.apply σ' x) = v), from ite.if_true ⟨rfl, h6⟩, rw[h7] at h3, from env.contains_apply_equiv.right.mp (exists.intro v h3.symm) end, have : x ∉ FV (term.subst_env σ₂ t), from term.not_free_of_subst_env h3, from unchanged_of_subst_nonfree_term this end end lemma term.substte_env.order {t' t: term} {σ₁ σ₂: env} {x: var}: (∀z, z ∈ σ₁ → (σ₁ z = σ₂ z)) → (x ∉ σ₁) → (term.subst_env σ₁ (term.substt x (term.subst_env σ₂ t') t) = term.substt x (term.subst_env σ₂ t') (term.subst_env σ₁ t)) := begin assume env_equiv, assume h1, induction t with v y unop t₁ t₁_ih binop t₂ t₃ t₂_ih t₃_ih t₄ t₅ t₄_ih t₅_ih, show (term.subst_env σ₁ (term.substt x (term.subst_env σ₂ t') (term.value v)) = term.substt x (term.subst_env σ₂ t') (term.subst_env σ₁ (term.value v))), by begin change (term.subst_env σ₁ (term.substt x (term.subst_env σ₂ t') (term.value v)) = term.substt x (term.subst_env σ₂ t') (term.subst_env σ₁ v)), rw[term.subst_env.value], unfold term.substt, rw[term.subst_env.value], change (↑v = term.substt x (term.subst_env σ₂ t') (term.value v)), unfold term.substt end, show (term.subst_env σ₁ (term.substt x (term.subst_env σ₂ t') (term.var y)) = term.substt x (term.subst_env σ₂ t') (term.subst_env σ₁ (term.var y))), by begin by_cases (x = y) with h, simp[h], rw[h] at h1, unfold term.substt, simp, have : (σ₁ y = none), from env.contains_apply_equiv.left.mpr h1, have h2: (term.subst_env σ₁ (term.var y) = y), from term.subst_env.var.left.mp this, simp[h2], have h3: (term.subst_env σ₁ (term.subst_env σ₂ t') = (term.subst_env σ₂ t')), from term.subst_env_twice_equiv env_equiv, rw[h3], change (term.subst_env σ₂ t' = term.substt y (term.subst_env σ₂ t') (term.var y)), unfold term.substt, simp, unfold term.substt, simp[h], have h3: (term.subst_env σ₁ y = y ∨ ∃v: value, term.subst_env σ₁ y = v), from term.subst_env.var.inv, cases h3 with h4 h5, change (term.subst_env σ₁ y = term.substt x (term.subst_env σ₂ t') (term.subst_env σ₁ y)), rw[h4], change (↑y = term.substt x (term.subst_env σ₂ t') (term.var y)), unfold term.substt, simp[h], cases h5 with v' h6, change (term.subst_env σ₁ y = term.substt x (term.subst_env σ₂ t') (term.subst_env σ₁ y)), rw[h6], change (↑v' = term.substt x (term.subst_env σ₂ t') (term.value v')), unfold term.substt end, show (term.subst_env σ₁ (term.substt x (term.subst_env σ₂ t') (term.unop unop t₁)) = term.substt x (term.subst_env σ₂ t') (term.subst_env σ₁ (term.unop unop t₁))), by begin rw[term.subst_env.unop], unfold term.substt, rw[term.subst_env.unop], congr, from t₁_ih end, show (term.subst_env σ₁ (term.substt x (term.subst_env σ₂ t') (term.binop binop t₂ t₃)) = term.substt x (term.subst_env σ₂ t') (term.subst_env σ₁ (term.binop binop t₂ t₃))), by begin rw[term.subst_env.binop], unfold term.substt, rw[term.subst_env.binop], congr, rw[t₂_ih], rw[t₃_ih] end, show (term.subst_env σ₁ (term.substt x (term.subst_env σ₂ t') (term.app t₄ t₅)) = term.substt x (term.subst_env σ₂ t') (term.subst_env σ₁ (term.app t₄ t₅))), by begin rw[term.subst_env.app], unfold term.substt, rw[term.subst_env.app], congr, rw[t₄_ih], rw[t₅_ih] end end lemma vc.subst_env.order {P: vc}: ∀ {σ: env} {x: var} {v: value}, (x ∉ σ) ∨ (σ x = v) → (vc.subst_env σ (vc.subst x v P) = vc.subst x v (vc.subst_env σ P)) := begin induction P, case vc.term t { assume σ x v, assume h1, change (vc.subst_env σ (vc.subst x v (vc.term t)) = vc.subst x v (vc.subst_env σ ↑t)), rw[vc.subst_env.term], unfold vc.subst, rw[vc.subst_env.term], congr, from term.subst_env.order h1 }, case vc.not P₁ ih { assume σ x v, assume h1, rw[vc.subst_env.not], unfold vc.subst, rw[vc.subst_env.not], congr, from ih h1 }, case vc.and P₁ P₂ P₁_ih P₂_ih { assume σ x v, assume h1, change (vc.subst_env σ (vc.subst x v (vc.and P₁ P₂)) = vc.subst x v (vc.subst_env σ (P₁ ⋀ P₂))), rw[vc.subst_env.and], unfold vc.subst, rw[vc.subst_env.and], congr, from P₁_ih h1, from P₂_ih h1 }, case vc.or P₁ P₂ P₁_ih P₂_ih { assume σ x v, assume h1, change (vc.subst_env σ (vc.subst x v (vc.or P₁ P₂)) = vc.subst x v (vc.subst_env σ (P₁ ⋁ P₂))), rw[vc.subst_env.or], unfold vc.subst, rw[vc.subst_env.or], congr, from P₁_ih h1, from P₂_ih h1 }, case vc.pre t₁ t₂ { assume σ x v, assume h1, rw[vc.subst_env.pre], unfold vc.subst, rw[vc.subst_env.pre], congr, from term.subst_env.order h1, from term.subst_env.order h1 }, case vc.pre₁ op t { assume σ x v, assume h1, rw[vc.subst_env.pre₁], unfold vc.subst, rw[vc.subst_env.pre₁], congr, from term.subst_env.order h1 }, case vc.pre₂ op t₁ t₂ { assume σ x v, assume h1, rw[vc.subst_env.pre₂], unfold vc.subst, rw[vc.subst_env.pre₂], congr, from term.subst_env.order h1, from term.subst_env.order h1 }, case vc.post t₁ t₂ { assume σ x v, assume h1, rw[vc.subst_env.post], unfold vc.subst, rw[vc.subst_env.post], congr, from term.subst_env.order h1, from term.subst_env.order h1 }, case vc.univ z P' P'_ih { assume σ x v, assume h1, rw[vc.subst_env.univ], unfold vc.subst, by_cases (x = z) with h2, simp[h2], rw[vc.subst_env.univ], simp[h2], rw[vc.subst_env.univ], congr, have h2: (x ∉ σ.without z ∨ (σ.without z x = v)), from env.without_equiv h1, have h3: (vc.subst_env (σ.without z) (vc.subst x v P') = vc.subst x v (vc.subst_env (σ.without z) P')), from P'_ih h2, rw[h3] } end lemma vc.substt_env.order {P: vc}: ∀ {σ: env} {x: var} {t: term}, closed t → (x ∉ σ) → (vc.subst_env σ (vc.substt x t P) = vc.substt x t (vc.subst_env σ P)) := begin induction P, case vc.term t' { assume σ x t, assume t_closed, assume h1, change (vc.subst_env σ (vc.substt x t (vc.term t')) = vc.substt x t (vc.subst_env σ t')), rw[vc.subst_env.term], unfold vc.substt, rw[vc.subst_env.term], congr, from term.substt_env.order t_closed h1 }, case vc.not P₁ ih { assume σ x t, assume t_closed, assume h1, rw[vc.subst_env.not], unfold vc.substt, rw[vc.subst_env.not], congr, from ih t_closed h1 }, case vc.and P₁ P₂ P₁_ih P₂_ih { assume σ x t, assume t_closed, assume h1, change (vc.subst_env σ (vc.substt x t (vc.and P₁ P₂)) = vc.substt x t (vc.subst_env σ (P₁ ⋀ P₂))), rw[vc.subst_env.and], unfold vc.substt, rw[vc.subst_env.and], congr, from P₁_ih t_closed h1, from P₂_ih t_closed h1 }, case vc.or P₁ P₂ P₁_ih P₂_ih { assume σ x t, assume t_closed, assume h1, change (vc.subst_env σ (vc.substt x t (vc.or P₁ P₂)) = vc.substt x t (vc.subst_env σ (P₁ ⋁ P₂))), rw[vc.subst_env.or], unfold vc.substt, rw[vc.subst_env.or], congr, from P₁_ih t_closed h1, from P₂_ih t_closed h1 }, case vc.pre t₁ t₂ { assume σ x t, assume t_closed, assume h1, rw[vc.subst_env.pre], unfold vc.substt, rw[vc.subst_env.pre], congr, from term.substt_env.order t_closed h1, from term.substt_env.order t_closed h1 }, case vc.pre₁ op t { assume σ x t, assume t_closed, assume h1, rw[vc.subst_env.pre₁], unfold vc.substt, rw[vc.subst_env.pre₁], congr, from term.substt_env.order t_closed h1 }, case vc.pre₂ op t₁ t₂ { assume σ x t, assume t_closed, assume h1, rw[vc.subst_env.pre₂], unfold vc.substt, rw[vc.subst_env.pre₂], congr, from term.substt_env.order t_closed h1, from term.substt_env.order t_closed h1 }, case vc.post t₁ t₂ { assume σ x t, assume t_closed, assume h1, rw[vc.subst_env.post], unfold vc.substt, rw[vc.subst_env.post], congr, from term.substt_env.order t_closed h1, from term.substt_env.order t_closed h1 }, case vc.univ z P' P'_ih { assume σ x t, assume t_closed, assume h1, rw[vc.subst_env.univ], unfold vc.substt, by_cases (x = z) with h2, simp[h2], rw[vc.subst_env.univ], simp[h2], rw[vc.subst_env.univ], congr, have h2: (x ∉ σ.without z), from env.not_in_without h1, have h3: (vc.subst_env (σ.without z) (vc.substt x t P') = vc.substt x t (vc.subst_env (σ.without z) P')), from P'_ih t_closed h2, rw[h3] } end lemma vc.subst_env_inner {P: vc} {σ: env} {x: var} {v: value}: (σ x = some v) → (vc.subst_env σ (vc.subst x v P) = vc.subst_env σ P) := begin assume x_is_v, induction σ with σ₁ y v' ih, show (vc.subst_env env.empty (vc.subst x v P) = vc.subst_env env.empty P), by cases x_is_v, show (vc.subst_env (σ₁[y↦v']) (vc.subst x v P) = vc.subst_env (σ₁[y↦v']) P), by begin unfold vc.subst_env, have h2: (env.apply (σ₁[y↦v']) x = some v), from x_is_v, unfold env.apply at h2, by_cases (y = x ∧ (option.is_none (env.apply σ₁ x))) with h3, simp[h3] at h2, have h4: (v' = v), from option.some.inj h2, simp[h3], have h5: (σ₁ x = none), from option.is_none.inv.mpr h3.right, have h6: x ∉ σ₁, from env.contains_apply_equiv.left.mp h5, rw[h4], have h7: x ∉ FV (vc.subst x v P), from vc.not_free_of_subst, have h8: x ∉ FV (vc.subst_env σ₁ (vc.subst x v P)), from mt free_in_vc.subst_env h7, have h9: (vc.subst x v (vc.subst_env σ₁ (vc.subst x v P)) = (vc.subst_env σ₁ (vc.subst x v P))), from unchanged_of_subst_nonfree_vc h8, rw[h9], from vc.subst_env.order (or.inl h6), simp[h3] at h2, have h4, from ih h2, congr, from h4 end end lemma vc.subst_env_with_equivalent_env {P: vc} {σ₁ σ₂: env}: (∀z, z ∈ σ₁ → (σ₁ z = σ₂ z)) → (vc.subst_env σ₂ (vc.subst_env σ₁ P) = vc.subst_env σ₂ P) := begin assume env_equiv, induction σ₁ with σ₁' x v ih, show (vc.subst_env σ₂ (vc.subst_env env.empty P) = vc.subst_env σ₂ P), from ( have vc.subst_env env.empty P = P, by unfold vc.subst_env, show vc.subst_env σ₂ (vc.subst_env env.empty P) = vc.subst_env σ₂ P, from this.symm ▸ rfl ), show (vc.subst_env σ₂ (vc.subst_env (σ₁'[x↦v]) P) = vc.subst_env σ₂ P), by begin unfold vc.subst_env, have h0: (∀ (z : var), z ∈ σ₁' → (σ₁' z = σ₂ z)), from ( assume z: var, assume h1: z ∈ σ₁', have ∃v, σ₁' z = some v, from env.contains_apply_equiv.right.mpr h1, let ⟨v', h2⟩ := this in have option.is_some (σ₁' z), from option.is_some_iff_exists.mpr this, have ¬ option.is_none (σ₁' z), from option.some_iff_not_none.mp this, have ¬ (x = z ∧ option.is_none (env.apply σ₁' z)), from not_and_distrib.mpr (or.inr this), have h3: env.apply (σ₁'[x↦v]) z = σ₁' z, by { unfold env.apply, simp[this], refl }, have z ∈ (σ₁'[x↦v]), from env.contains.rest h1, show σ₁' z = σ₂ z, from h3 ▸ (env_equiv z this) ), by_cases (x ∈ σ₁') with h1, have h2: x ∉ FV (vc.subst_env σ₁' P), from vc.not_free_of_subst_env h1, have h3: (vc.subst x v (vc.subst_env σ₁' P) = (vc.subst_env σ₁' P)), from unchanged_of_subst_nonfree_vc h2, rw[h3], from ih h0, have h2: x ∈ (σ₁'[x↦v]), from env.contains.same, have h3: ((σ₁'[x↦v]) x = σ₂ x), from env_equiv x h2, have h4: (env.apply (σ₁'[x↦v]) x = v), from env.apply_of_contains h1, have h5: (σ₂ x = some v), from eq.trans h3.symm h4, have h6: (vc.subst_env σ₂ (vc.subst x v (vc.subst_env σ₁' P)) = vc.subst_env σ₂ (vc.subst_env σ₁' P)), from vc.subst_env_inner h5, rw[h6], from ih h0 end end lemma vc.subst_env_equivalent_env {P: vc} {σ₁ σ₂: env}: (∀z, z ∈ σ₁ → (σ₁ z = σ₂ z)) → closed_subst σ₁ P → (vc.subst_env σ₁ P = vc.subst_env σ₂ P) := assume h1: (∀z, z ∈ σ₁ → (σ₁ z = σ₂ z)), assume P_closed: closed_subst σ₁ P, have closed (vc.subst_env σ₁ P), from vc.closed_of_closed_subst P_closed, have h2: vc.subst_env σ₂ (vc.subst_env σ₁ P) = (vc.subst_env σ₁ P), from unchanged_of_subst_env_nonfree_vc this σ₂, have vc.subst_env σ₂ (vc.subst_env σ₁ P) = vc.subst_env σ₂ P, from vc.subst_env_with_equivalent_env h1, show vc.subst_env σ₁ P = vc.subst_env σ₂ P, from h2 ▸ this lemma env.remove_unimportant_equivalence {σ₁ σ₂: env} {x: var}: (∀y, y ∈ σ₁ → (σ₁ y = σ₂ y)) → x ∉ σ₁ → (∀y, y ∈ σ₁ → (σ₁ y = σ₂.without x y)) := assume h1: (∀y, y ∈ σ₁ → (σ₁ y = σ₂ y)), assume h2: x ∉ σ₁, assume y: var, assume h3: y ∈ σ₁, have ∃v, σ₁ y = some v, from env.contains_apply_equiv.right.mpr h3, let ⟨v, h4⟩ := this in have σ₁ y = σ₂ y, from h1 y h3, have h5: σ₂ y = v, from eq.trans this.symm h4, have h6: x ≠ y, from ( assume : x = y, have x ∈ σ₁, from this.symm ▸ h3, show «false», from h2 this ), have y ∈ σ₁.dom, from h3, have y ∈ σ₂.dom, from set.mem_of_subset_of_mem (env.dom_subset_of_equivalent_env h1) this, have y ∈ σ₂, from this, have h7: y ∈ σ₂.without x, from env.contains_without.rinv ⟨this, h6.symm⟩, -- have ∃v', σ₂.without x y = some v', from env.contains_apply_equiv.right.mpr this, have y ∉ σ₂.without x ∨ (σ₂.without x y = v), from env.without_equiv (or.inr h5), or.elim this ( assume : y ∉ σ₂.without x, show σ₁ y = σ₂.without x y, from absurd h7 this ) ( assume : σ₂.without x y = v, show σ₁ y = σ₂.without x y, from eq.trans h4 this.symm ) lemma vc.subst_env_without_nonfree {σ: env} {P: vc} {x: var}: x ∉ FV P → (vc.subst_env σ P = vc.subst_env (σ.without x) P) := begin assume h1: x ∉ FV P, induction σ with σ' y v ih, show (vc.subst_env env.empty P = vc.subst_env (env.without env.empty x) P), by begin unfold env.without end, show (vc.subst_env (σ'[y↦v]) P = vc.subst_env (env.without (σ'[y↦v]) x) P), by begin unfold env.without, by_cases (y = x) with h2, simp[h2], unfold vc.subst_env, have h3: x ∉ FV (vc.subst_env σ' P), by begin assume : x ∈ FV (vc.subst_env σ' P), have : x ∈ FV P, from free_in_vc.subst_env this, show «false», from h1 this end, have h4: (vc.subst x v (vc.subst_env σ' P) = (vc.subst_env σ' P)), from unchanged_of_subst_nonfree_vc h3, from eq.trans h4 ih, simp[h2], unfold vc.subst_env, rw[ih] end end lemma vc.subst_env.reorder {σ: env} {x: var} {v: value} {P: vc}: (vc.subst_env σ (vc.subst x v P) = vc.subst x v (vc.subst_env (σ.without x) P)) := have x ∉ FV (vc.subst x v P), from vc.not_free_of_subst, have h1: vc.subst_env σ (vc.subst x v P) = vc.subst_env (σ.without x) (vc.subst x v P), from vc.subst_env_without_nonfree this, have x ∉ σ.without x, from env.not_contains_without, have h2: (vc.subst_env (σ.without x) (vc.subst x v P) = vc.subst x v (vc.subst_env (σ.without x) P)), from vc.subst_env.order (or.inl this), show vc.subst_env σ (vc.subst x v P) = vc.subst x v (vc.subst_env (σ.without x) P), from eq.trans h1 h2 lemma vc.substt_env.reorder {σ: env} {x: var} {t: term} {P: vc}: closed t → (vc.subst_env σ (vc.substt x t P) = vc.substt x t (vc.subst_env (σ.without x) P)) := assume t_closed: closed t, have x ∉ FV (vc.substt x t P), from vc.not_free_of_substt t_closed, have h1: vc.subst_env σ (vc.substt x t P) = vc.subst_env (σ.without x) (vc.substt x t P), from vc.subst_env_without_nonfree this, have x ∉ σ.without x, from env.not_contains_without, have h2: (vc.subst_env (σ.without x) (vc.substt x t P) = vc.substt x t (vc.subst_env (σ.without x) P)), from vc.substt_env.order t_closed this, show vc.subst_env σ (vc.substt x t P) = vc.substt x t (vc.subst_env (σ.without x) P), from eq.trans h1 h2 lemma term.substt_env.redundant {σ: env} {x: var} {t t': term}: (term.subst_env σ (term.substt x (term.subst_env σ t) t') = term.subst_env σ (term.substt x t t')) := begin induction t' with v y unop t₁ t₁_ih binop t₂ t₃ t₂_ih t₃_ih t₄ t₅ t₄_ih t₅_ih, show (term.subst_env σ (term.substt x (term.subst_env σ t) (term.value v)) = term.subst_env σ (term.substt x t (term.value v))), by begin unfold term.substt end, show (term.subst_env σ (term.substt x (term.subst_env σ t) (term.var y)) = term.subst_env σ (term.substt x t (term.var y))), by begin unfold term.substt, by_cases (x = y) with h1, simp[h1], from term.subst_env_twice, simp[h1] end, show (term.subst_env σ (term.substt x (term.subst_env σ t) (term.unop unop t₁)) = term.subst_env σ (term.substt x t (term.unop unop t₁))), by begin unfold term.substt, rw[term.subst_env.unop], rw[term.subst_env.unop], congr, from t₁_ih end, show (term.subst_env σ (term.substt x (term.subst_env σ t) (term.binop binop t₂ t₃)) = term.subst_env σ (term.substt x t (term.binop binop t₂ t₃))), by begin unfold term.substt, rw[term.subst_env.binop], rw[term.subst_env.binop], congr, from t₂_ih, from t₃_ih end, show (term.subst_env σ (term.substt x (term.subst_env σ t) (term.app t₄ t₅)) = term.subst_env σ (term.substt x t (term.app t₄ t₅))), by begin unfold term.substt, rw[term.subst_env.app], rw[term.subst_env.app], congr, from t₄_ih, from t₅_ih end end lemma term.substt_var_cancel {x y: var} {t: term}: x ∉ FV t → (term.substt x y (term.substt y x t) = t) := begin assume h1, induction t with v z unop t₁ t₁_ih binop t₂ t₃ t₂_ih t₃_ih t₄ t₅ t₄_ih t₅_ih, show (term.substt x y (term.substt y x (term.value v)) = term.value v), by begin unfold term.substt, change (term.substt x ↑y (term.value v) = term.value v), unfold term.substt, refl end, show (term.substt x ↑y (term.substt y ↑x (term.var z)) = term.var z), by begin have h2: (x ≠ z), by begin assume h3, rw[h3] at h1, have h4, from free_in_term.var z, contradiction end, unfold term.substt, by_cases (y = z) with h3, simp[h3], change (term.substt x ↑z (term.var x) = term.var z), unfold term.substt, simp, refl, simp[h3], change (term.substt x ↑y (term.var z) = term.var z), unfold term.substt, simp[h2], refl end, show (term.substt x y (term.substt y x (term.unop unop t₁)) = term.unop unop t₁), by begin unfold term.substt, congr, apply t₁_ih, assume h1, have h2: x ∈ FV (term.unop unop t₁), from free_in_term.unop h1, contradiction end, show (term.substt x y (term.substt y x (term.binop binop t₂ t₃)) = term.binop binop t₂ t₃), by begin unfold term.substt, congr, apply t₂_ih, assume h1, have h2: x ∈ FV (term.binop binop t₂ t₃), from free_in_term.binop₁ h1, contradiction, apply t₃_ih, assume h1, have h2: x ∈ FV (term.binop binop t₂ t₃), from free_in_term.binop₂ h1, contradiction end, show (term.substt x y (term.substt y x (term.app t₄ t₅)) = term.app t₄ t₅), by begin unfold term.substt, congr, apply t₄_ih, assume h1, have h2: x ∈ FV (term.app t₄ t₅), from free_in_term.app₁ h1, contradiction, apply t₅_ih, assume h1, have h2: x ∈ FV (term.app t₄ t₅), from free_in_term.app₂ h1, contradiction end end lemma vc.substt_var_cancel {x y: var} {P: vc}: ¬ vc.uses_var x P → (vc.substt x y (vc.substt y x P) = P) := begin assume h1, induction P, case vc.term t { unfold vc.substt, change (vc.substt x ↑y (vc.term (term.substt y ↑x t)) = vc.term t), unfold vc.substt, congr, apply term.substt_var_cancel, assume h1, have h2: vc.uses_var x t, from vc.uses_var.term h1, contradiction }, case vc.not P₁ P₁_ih { unfold vc.substt, congr, apply P₁_ih, assume h1, have h2: vc.uses_var x (vc.not P₁), from vc.uses_var.not h1, contradiction }, case vc.and P₁ P₂ P₁_ih P₂_ih { unfold vc.substt, change (vc.substt x ↑y (vc.and (vc.substt y ↑x P₁) (vc.substt y ↑x P₂)) = vc.and P₁ P₂), unfold vc.substt, congr, apply P₁_ih, assume h1, have h2: vc.uses_var x (vc.and P₁ P₂), from vc.uses_var.and₁ h1, contradiction, apply P₂_ih, assume h1, have h2: vc.uses_var x (vc.and P₁ P₂), from vc.uses_var.and₂ h1, contradiction }, case vc.or P₁ P₂ P₁_ih P₂_ih { unfold vc.substt, change (vc.substt x ↑y (vc.or (vc.substt y ↑x P₁) (vc.substt y ↑x P₂)) = vc.or P₁ P₂), unfold vc.substt, congr, apply P₁_ih, assume h1, have h2: vc.uses_var x (vc.or P₁ P₂), from vc.uses_var.or₁ h1, contradiction, apply P₂_ih, assume h1, have h2: vc.uses_var x (vc.or P₁ P₂), from vc.uses_var.or₂ h1, contradiction }, case vc.pre t₁ t₂ { unfold vc.substt, congr, apply term.substt_var_cancel, assume h1, have h2: vc.uses_var x (vc.pre t₁ t₂), from vc.uses_var.pre₁ h1, contradiction, apply term.substt_var_cancel, assume h1, have h2: vc.uses_var x (vc.pre t₁ t₂), from vc.uses_var.pre₂ h1, contradiction }, case vc.pre₁ op t { unfold vc.substt, congr, apply term.substt_var_cancel, assume h1, have h2: vc.uses_var x (vc.pre₁ op t), from vc.uses_var.preop h1, contradiction }, case vc.pre₂ op t₁ t₂ { unfold vc.substt, congr, apply term.substt_var_cancel, assume h1, have h2: vc.uses_var x (vc.pre₂ op t₁ t₂), from vc.uses_var.preop₁ h1, contradiction, apply term.substt_var_cancel, assume h1, have h2: vc.uses_var x (vc.pre₂ op t₁ t₂), from vc.uses_var.preop₂ h1, contradiction }, case vc.post t₁ t₂ { unfold vc.substt, congr, apply term.substt_var_cancel, assume h1, have h2: vc.uses_var x (vc.post t₁ t₂), from vc.uses_var.post₁ h1, contradiction, apply term.substt_var_cancel, assume h1, have h2: vc.uses_var x (vc.post t₁ t₂), from vc.uses_var.post₂ h1, contradiction }, case vc.univ z P₁ P₁_ih { unfold vc.substt, congr, have h1: (x ≠ z), by begin assume h2, rw[h2] at h1, have h3: vc.uses_var z (vc.univ z P₁), from vc.uses_var.quantified z, contradiction end, by_cases (y = z) with h2, simp[h1], simp[h2], have h3: x ∉ FV P₁, by begin assume h4, have h5, from vc.uses_var_of_free h4, have h6: vc.uses_var x (vc.univ z P₁), from vc.uses_var.univ h5, contradiction end, from unchanged_of_substt_nonfree_vc h3, simp[h1], simp[h2], have h3: ¬vc.uses_var x P₁, by begin assume h4, have h5: vc.uses_var x (vc.univ z P₁), from vc.uses_var.univ h4, contradiction end, from P₁_ih h3 } end lemma subst_distrib_to_vc {P: prop} {x: var} {v: value}: (vc.subst x v (prop.to_vc P) = prop.to_vc (prop.subst x v P)) := begin induction P, case prop.term t { unfold prop.subst, unfold prop.to_vc, unfold vc.subst, change (vc.term (term.subst x v t) = prop.to_vc (prop.term (term.subst x v t))), unfold prop.to_vc }, case prop.not P₁ ih { unfold prop.subst, unfold prop.to_vc, unfold vc.subst, congr, from ih }, case prop.and P₁ P₂ P₁_ih P₂_ih { unfold prop.subst, unfold prop.to_vc, change (vc.subst x v (vc.and (prop.to_vc P₁) (prop.to_vc P₂)) = prop.to_vc (prop.and (prop.subst x v P₁) (prop.subst x v P₂))), unfold vc.subst, unfold prop.to_vc, congr, from P₁_ih, from P₂_ih }, case prop.or P₁ P₂ P₁_ih P₂_ih { unfold prop.subst, unfold prop.to_vc, change (vc.subst x v (vc.or (prop.to_vc P₁) (prop.to_vc P₂)) = prop.to_vc (prop.or (prop.subst x v P₁) (prop.subst x v P₂))), unfold vc.subst, unfold prop.to_vc, congr, from P₁_ih, from P₂_ih }, case prop.pre t₁ t₂ { unfold prop.subst, unfold prop.to_vc, unfold vc.subst }, case prop.pre₁ op t { unfold prop.subst, unfold prop.to_vc, unfold vc.subst }, case prop.pre₂ op t₁ t₂ { unfold prop.subst, unfold prop.to_vc, unfold vc.subst }, case prop.call t { unfold prop.subst, unfold prop.to_vc, unfold vc.subst, congr }, case prop.post t₁ t₂ { unfold prop.subst, unfold prop.to_vc, unfold vc.subst }, case prop.forallc y P₁ P₁_ih { unfold prop.subst, unfold prop.to_vc, unfold vc.subst, congr, by_cases (x = y) with h1, simp[h1], simp[h1], from P₁_ih }, case prop.exis y P₁ P₁_ih { unfold prop.subst, unfold prop.to_vc, unfold vc.subst, congr, by_cases (x = y) with h1, simp[h1], simp[h1], congr, from P₁_ih } end lemma subst_env_distrib_to_vc {P: prop} {σ: env}: (vc.subst_env σ (prop.to_vc P) = prop.to_vc (prop.subst_env σ P)) := begin induction σ with σ₁ y v' ih, show (vc.subst_env env.empty (prop.to_vc P) = prop.to_vc (prop.subst_env env.empty P)), by begin unfold prop.subst_env, unfold vc.subst_env end, show (vc.subst_env (σ₁[y↦v']) (prop.to_vc P) = prop.to_vc (prop.subst_env (σ₁[y↦v']) P)), by begin unfold prop.subst_env, unfold vc.subst_env, rw[ih], from subst_distrib_to_vc end end lemma substt_distrib_to_vc {P: prop} {x: var} {t: term}: (vc.substt x t (prop.to_vc P) = prop.to_vc (prop.substt x t P)) := begin induction P, case prop.term t₂ { unfold prop.substt, unfold prop.to_vc, unfold vc.substt, change (vc.term (term.substt x t t₂) = prop.to_vc (prop.term (term.substt x t t₂))), unfold prop.to_vc }, case prop.not P₁ ih { unfold prop.substt, unfold prop.to_vc, unfold vc.substt, congr, from ih }, case prop.and P₁ P₂ P₁_ih P₂_ih { unfold prop.substt, unfold prop.to_vc, change (vc.substt x t (vc.and (prop.to_vc P₁) (prop.to_vc P₂)) = prop.to_vc (prop.and (prop.substt x t P₁) (prop.substt x t P₂))), unfold vc.substt, unfold prop.to_vc, congr, from P₁_ih, from P₂_ih }, case prop.or P₁ P₂ P₁_ih P₂_ih { unfold prop.substt, unfold prop.to_vc, change (vc.substt x t (vc.or (prop.to_vc P₁) (prop.to_vc P₂)) = prop.to_vc (prop.or (prop.substt x t P₁) (prop.substt x t P₂))), unfold vc.substt, unfold prop.to_vc, congr, from P₁_ih, from P₂_ih }, case prop.pre t₁ t₂ { unfold prop.substt, unfold prop.to_vc, unfold vc.substt }, case prop.pre₁ op t₁ { unfold prop.substt, unfold prop.to_vc, unfold vc.substt }, case prop.pre₂ op t₁ t₂ { unfold prop.substt, unfold prop.to_vc, unfold vc.substt }, case prop.call t₁ { unfold prop.substt, unfold prop.to_vc, unfold vc.substt, congr }, case prop.post t₁ t₂ { unfold prop.substt, unfold prop.to_vc, unfold vc.substt }, case prop.forallc y P₁ P₁_ih { unfold prop.substt, unfold prop.to_vc, unfold vc.substt, congr, by_cases (x = y) with h1, simp[h1], simp[h1], from P₁_ih }, case prop.exis y P₁ P₁_ih { unfold prop.substt, unfold prop.to_vc, unfold vc.substt, congr, by_cases (x = y) with h1, simp[h1], simp[h1], congr, from P₁_ih } end lemma subst_distrib_erased {P: prop} {x: var} {v: value}: (vc.subst x v (prop.erased_p P) = prop.erased_p (prop.subst x v P)) ∧ (vc.subst x v (prop.erased_n P) = prop.erased_n (prop.subst x v P)) := begin induction P, case prop.term t { split, unfold prop.subst, unfold prop.erased_p, unfold vc.subst, change (vc.term (term.subst x v t) = prop.erased_p (prop.term (term.subst x v t))), unfold prop.erased_p, unfold prop.subst, unfold prop.erased_n, unfold vc.subst, change (vc.term (term.subst x v t) = prop.erased_n (prop.term (term.subst x v t))), unfold prop.erased_n }, case prop.not P₁ ih { split, unfold prop.subst, unfold prop.erased_p, unfold vc.subst, congr, from ih.right, unfold prop.subst, unfold prop.erased_n, unfold vc.subst, congr, from ih.left }, case prop.and P₁ P₂ P₁_ih P₂_ih { split, unfold prop.subst, unfold prop.erased_p, change (vc.subst x v (vc.and (prop.erased_p P₁) (prop.erased_p P₂)) = prop.erased_p (prop.and (prop.subst x v P₁) (prop.subst x v P₂))), unfold vc.subst, unfold prop.erased_p, congr, from P₁_ih.left, from P₂_ih.left, unfold prop.subst, unfold prop.erased_n, change (vc.subst x v (vc.and (prop.erased_n P₁) (prop.erased_n P₂)) = prop.erased_n (prop.and (prop.subst x v P₁) (prop.subst x v P₂))), unfold vc.subst, unfold prop.erased_n, congr, from P₁_ih.right, from P₂_ih.right }, case prop.or P₁ P₂ P₁_ih P₂_ih { split, unfold prop.subst, unfold prop.erased_p, change (vc.subst x v (vc.or (prop.erased_p P₁) (prop.erased_p P₂)) = prop.erased_p (prop.or (prop.subst x v P₁) (prop.subst x v P₂))), unfold vc.subst, unfold prop.erased_p, congr, from P₁_ih.left, from P₂_ih.left, unfold prop.subst, unfold prop.erased_n, change (vc.subst x v (vc.or (prop.erased_n P₁) (prop.erased_n P₂)) = prop.erased_n (prop.or (prop.subst x v P₁) (prop.subst x v P₂))), unfold vc.subst, unfold prop.erased_n, congr, from P₁_ih.right, from P₂_ih.right }, case prop.pre t₁ t₂ { split, unfold prop.subst, unfold prop.erased_p, unfold vc.subst, unfold prop.subst, unfold prop.erased_n, unfold vc.subst }, case prop.pre₁ op t { split, unfold prop.subst, unfold prop.erased_p, unfold vc.subst, unfold prop.subst, unfold prop.erased_n, unfold vc.subst }, case prop.pre₂ op t₁ t₂ { split, unfold prop.subst, unfold prop.erased_p, unfold vc.subst, unfold prop.subst, unfold prop.erased_n, unfold vc.subst }, case prop.call t { split, unfold prop.subst, unfold prop.erased_p, unfold vc.subst, congr, unfold prop.subst, unfold prop.erased_n, unfold vc.subst, congr }, case prop.post t₁ t₂ { split, unfold prop.subst, unfold prop.erased_p, unfold vc.subst, unfold prop.subst, unfold prop.erased_n, unfold vc.subst }, case prop.forallc y P₁ P₁_ih { split, unfold prop.subst, unfold prop.erased_p, unfold vc.subst, congr, unfold prop.subst, unfold prop.erased_n, unfold vc.subst, congr, by_cases (x = y) with h1, simp[h1], simp[h1], from P₁_ih.right }, case prop.exis y P₁ P₁_ih { split, unfold prop.subst, unfold prop.erased_p, unfold vc.subst, congr, by_cases (x = y) with h1, simp[h1], simp[h1], congr, from P₁_ih.left, unfold prop.subst, unfold prop.erased_n, unfold vc.subst, congr, by_cases (x = y) with h1, simp[h1], simp[h1], congr, from P₁_ih.right } end lemma dom_eq_of_equiv {σ₁ σ₂: env}: (∀x: var, σ₁ x = σ₂ x) → (σ₁.dom = σ₂.dom) := begin assume h1, apply set.eq_of_subset_of_subset, apply env.dom_subset_of_equivalent_env, assume z, assume _, from h1 z, apply env.dom_subset_of_equivalent_env, assume z, assume _, from (h1 z).symm end lemma vc.subst_env_unchanged {P: vc} {σ₁ σ₂: env}: σ₁.dom ⊆ σ₂.dom → (vc.subst_env σ₁ (vc.subst_env σ₂ P) = vc.subst_env σ₂ P) := begin assume h1, induction σ₁ with σ₁' x v ih, show (vc.subst_env env.empty (vc.subst_env σ₂ P) = vc.subst_env σ₂ P), by begin unfold vc.subst_env end, show (vc.subst_env (σ₁'[x↦v]) (vc.subst_env σ₂ P) = vc.subst_env σ₂ P), by begin have h2: x ∈ (σ₁'[x↦v]).dom, from env.contains.same, have h3: x ∈ σ₂.dom, from set.mem_of_mem_of_subset h2 h1, unfold vc.subst_env, have h4: x ∉ FV (vc.subst_env σ₂ P), from vc.not_free_of_subst_env h3, have h5: x ∉ FV (vc.subst_env σ₁' (vc.subst_env σ₂ P)), by begin assume h6, have h7, from vc.free_of_free_subst_env h6, contradiction end, have h6: (vc.subst x v (vc.subst_env σ₁' (vc.subst_env σ₂ P)) = (vc.subst_env σ₁' (vc.subst_env σ₂ P))), from unchanged_of_subst_nonfree_vc h5, rw[h6], have h7: env.dom σ₁' ⊆ env.dom σ₂, by begin assume z, assume h8, have h9: z ∈ σ₁', from h8, have h10: z ∈ (σ₁'[x↦v]), from env.contains.rest h9, from set.mem_of_mem_of_subset h10 h1 end, from ih h7 end end lemma vc.subst_env_exact_equivalent_env {P: vc} {σ₁ σ₂: env}: (∀z, σ₁ z = σ₂ z) → (vc.subst_env σ₁ P = vc.subst_env σ₂ P) := assume h1: (∀z, σ₁ z = σ₂ z), have h2: vc.subst_env σ₁ (vc.subst_env σ₂ P) = (vc.subst_env σ₂ P), from vc.subst_env_unchanged (set.subset_of_eq (dom_eq_of_equiv h1)), have vc.subst_env σ₁ (vc.subst_env σ₂ P) = vc.subst_env σ₁ P, from vc.subst_env_with_equivalent_env (λz _, (h1 z).symm), show vc.subst_env σ₁ P = vc.subst_env σ₂ P, from eq.trans this.symm h2 lemma vc.subst_env_with_without_equivalent {P: vc} {σ: env} {x: var} {v: value}: (σ x = v) → (vc.subst_env ((σ.without x)[x↦v]) P = vc.subst_env σ P) := assume h1: σ x = v, have (∀z, ((σ.without x)[x↦v]) z = σ z), by begin assume z, change (env.apply (env.without σ x[x↦v]) z = σ z), unfold env.apply, by_cases (x = z) with h2, rw[h2], simp, have h3: z ∉ σ.without z, from env.not_contains_without, have h4, from env.contains_apply_equiv.left.mpr h3, have h5, from option.is_none.inv.mp h4, rw[h2] at h1, rw[h1], apply ite.if_true, from h5, by_cases z ∈ σ with h6, have h7: x ≠ z, from h2, have h8, from env.contains_without.rinv ⟨h6, h7.symm⟩, have h9: (env.apply (env.without σ x) z = σ z), from env.without_equiv_with z h8, rw[h9], apply ite.if_false, by_contradiction h10, have h11, from h10.left, contradiction, have h7, from env.contains_apply_equiv.left.mpr h6, have h8: z ∉ (env.without σ x), from env.not_in_without h6, have h9: (env.apply (env.without σ x) z = none), from env.contains_apply_equiv.left.mpr h8, have h10: (env.apply (env.without σ x) z = σ z), from eq.trans h9 h7.symm, rw[h10], apply ite.if_false, by_contradiction h10, have h11, from h10.left, contradiction, end, vc.subst_env_exact_equivalent_env this lemma eq_value_of_equiv_subst {σ₁ σ₂: env} {x: var} {v: value}: (∀z, z ∈ σ₁ → (σ₁ z = σ₂ z)) → (σ₁ x = v) → (σ₂ x = v) := assume env_equiv: ∀z, z ∈ σ₁ → (σ₁ z = σ₂ z), assume x_is_v: σ₁ x = v, have x ∈ σ₁, from env.contains_apply_equiv.right.mp (exists.intro v x_is_v), have σ₁ x = σ₂ x, from env_equiv x this, show σ₂ x = v, from this ▸ x_is_v lemma to_vc_closed_subst_of_closed {σ: env} {P: prop}: closed_subst σ P → closed_subst σ P.to_vc := begin assume h1, assume x, assume h2, have h3, from free_in_prop_of_free_in_to_vc h2, from h1 h3 end
d550e804ae6987ce4edf20bba2fa6eec74b8821c
4a092885406df4e441e9bb9065d9405dacb94cd8
/src/for_mathlib/is_cover.lean
9ebec6d7d2c06595e5f8a79a994b397efa08e19d
[ "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
252
lean
import topology.basic definition is_cover {X γ : Type} (U : γ → set X) := ∀ x, ∃ i, x ∈ U i structure is_open_cover {X γ : Type} [H : topological_space X] (U : γ → set X) := (is_open : ∀ i, H.is_open (U i)) (is_cover : is_cover U)
2bf7a8c6ad85755a5b5b57342bbe317ba51ad7e2
4e3bf8e2b29061457a887ac8889e88fa5aa0e34c
/lean/love03_structured_proofs_and_proof_terms_exercise_sheet.lean
273886e87948e1be4ead854cc05396e780e94c46
[]
no_license
mukeshtiwari/logical_verification_2019
9f964c067a71f65eb8884743273fbeef99e6503d
16f62717f55ed5b7b87e03ae0134791a9bef9b9a
refs/heads/master
1,619,158,844,208
1,585,139,500,000
1,585,139,500,000
249,906,380
0
0
null
1,585,118,728,000
1,585,118,727,000
null
UTF-8
Lean
false
false
1,636
lean
/- LoVe Exercise 3: Structured Proofs and Proof Terms -/ import .lovelib namespace LoVe /- Question 1: Chain of Equalities -/ /- 1.1. Write the following proof using `calc`. (a + b) * (a + b) = a * (a + b) + b * (a + b) = a * a + a * b + b * a + b * b = a * a + a * b + a * b + b * b = a * a + 2 * a * b + b * b Hint: You might need `rw`, `simp`, `ac_refl`, and the lemmas `mul_add`, `add_mul`, and `two_mul`. -/ lemma binomial_square (a b : ℕ) : (a + b) * (a + b) = a * a + 2 * a * b + b * b := sorry /- 1.2. Prove the same argument again, this time as a structured proof. Try to reuse as much of the above proof idea as possible. -/ lemma binomial_square₂ (a b : ℕ) : (a + b) * (a + b) = a * a + 2 * a * b + b * b := sorry /- 1.3 (**optional**). Prove the same lemma again, this time using tactics. -/ lemma binomial_square₃ (a b : ℕ) : (a + b) * (a + b) = a * a + 2 * a * b + b * b := begin sorry end /- Question 2: Connectives and Quantifiers -/ /- 2.1. Supply structured proofs of the following lemmas. -/ lemma I (a : Prop) : a → a := sorry lemma K (a b : Prop) : a → b → b := sorry lemma C (a b c : Prop) : (a → b → c) → b → a → c := sorry lemma proj_1st (a : Prop) : a → a → a := sorry -- please give a different answer than for `proj_1st` lemma proj_2nd (a : Prop) : a → a → a := sorry lemma some_nonsense (a b c : Prop) : (a → b → c) → a → (a → c) → b → c := sorry /- 2.2. Supply a structured proof of the contraposition rule. -/ lemma contrapositive (a b : Prop) : (a → b) → ¬ b → ¬ a := sorry end LoVe
204e9176ec2f13c286927751a1dcfc6f28336216
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/group/fundamental_domain.lean
a22cb7b53bd5ddb39aebd8573a61440d48f4b33a
[ "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
22,106
lean
/- Copyright (c) 2021 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import measure_theory.group.action import measure_theory.integral.set_integral /-! # Fundamental domain of a group action A set `s` is said to be a *fundamental domain* of an action of a group `G` on a measurable space `α` with respect to a measure `μ` if * `s` is a measurable set; * the sets `g • s` over all `g : G` cover almost all points of the whole space; * the sets `g • s`, are pairwise a.e. disjoint, i.e., `μ (g₁ • s ∩ g₂ • s) = 0` whenever `g₁ ≠ g₂`; we require this for `g₂ = 1` in the definition, then deduce it for any two `g₁ ≠ g₂`. In this file we prove that in case of a countable group `G` and a measure preserving action, any two fundamental domains have the same measure, and for a `G`-invariant function, its integrals over any two fundamental domains are equal to each other. We also generate additive versions of all theorems in this file using the `to_additive` attribute. -/ open_locale ennreal pointwise topological_space nnreal ennreal measure_theory open measure_theory measure_theory.measure set function topological_space filter namespace measure_theory /-- A measurable set `s` is a *fundamental domain* for an additive action of an additive group `G` on a measurable space `α` with respect to a measure `α` if the sets `g +ᵥ s`, `g : G`, are pairwise a.e. disjoint and cover the whole space. -/ @[protect_proj] structure is_add_fundamental_domain (G : Type*) {α : Type*} [has_zero G] [has_vadd G α] [measurable_space α] (s : set α) (μ : measure α . volume_tac) : Prop := (null_measurable_set : null_measurable_set s μ) (ae_covers : ∀ᵐ x ∂μ, ∃ g : G, g +ᵥ x ∈ s) (ae_disjoint : ∀ g ≠ (0 : G), ae_disjoint μ (g +ᵥ s) s) /-- A measurable set `s` is a *fundamental domain* for an action of a group `G` on a measurable space `α` with respect to a measure `α` if the sets `g • s`, `g : G`, are pairwise a.e. disjoint and cover the whole space. -/ @[protect_proj, to_additive is_add_fundamental_domain] structure is_fundamental_domain (G : Type*) {α : Type*} [has_one G] [has_smul G α] [measurable_space α] (s : set α) (μ : measure α . volume_tac) : Prop := (null_measurable_set : null_measurable_set s μ) (ae_covers : ∀ᵐ x ∂μ, ∃ g : G, g • x ∈ s) (ae_disjoint : ∀ g ≠ (1 : G), ae_disjoint μ (g • s) s) namespace is_fundamental_domain variables {G H α β E : Type*} [group G] [group H] [mul_action G α] [measurable_space α] [mul_action H β] [measurable_space β] [normed_add_comm_group E] {s t : set α} {μ : measure α} /-- If for each `x : α`, exactly one of `g • x`, `g : G`, belongs to a measurable set `s`, then `s` is a fundamental domain for the action of `G` on `α`. -/ @[to_additive "If for each `x : α`, exactly one of `g +ᵥ x`, `g : G`, belongs to a measurable set `s`, then `s` is a fundamental domain for the additive action of `G` on `α`."] lemma mk' (h_meas : null_measurable_set s μ) (h_exists : ∀ x : α, ∃! g : G, g • x ∈ s) : is_fundamental_domain G s μ := { null_measurable_set := h_meas, ae_covers := eventually_of_forall $ λ x, (h_exists x).exists, ae_disjoint := λ g hne, disjoint.ae_disjoint $ disjoint_left.2 begin rintro _ ⟨x, hx, rfl⟩ hgx, rw ← one_smul G x at hx, exact hne ((h_exists x).unique hgx hx) end } /-- If a measurable space has a finite measure `μ` and a countable group `G` acts quasi-measure-preservingly, then to show that a set `s` is a fundamental domain, it is sufficient to check that its translates `g • s` are (almost) disjoint and that the sum `∑' g, μ (g • s)` is sufficiently large. -/ @[to_additive measure_theory.is_add_fundamental_domain.mk_of_measure_univ_le " If a measurable space has a finite measure `μ` and a countable additive group `G` acts quasi-measure-preservingly, then to show that a set `s` is a fundamental domain, it is sufficient to check that its translates `g +ᵥ s` are (almost) disjoint and that the sum `∑' g, μ (g +ᵥ s)` is sufficiently large."] lemma mk_of_measure_univ_le [is_finite_measure μ] [countable G] (h_meas : null_measurable_set s μ) (h_ae_disjoint : ∀ g ≠ (1 : G), ae_disjoint μ (g • s) s) (h_qmp : ∀ (g : G), quasi_measure_preserving ((•) g : α → α) μ μ) (h_measure_univ_le : μ (univ : set α) ≤ ∑' (g : G), μ (g • s)) : is_fundamental_domain G s μ := { null_measurable_set := h_meas, ae_disjoint := h_ae_disjoint, ae_covers := begin replace ae_disjoint : pairwise (ae_disjoint μ on (λ (g : G), g • s)) := pairwise_ae_disjoint_of_ae_disjoint_forall_ne_one h_ae_disjoint h_qmp, replace h_meas : ∀ (g : G), null_measurable_set (g • s) μ := λ g, by { rw [← inv_inv g, ← preimage_smul], exact h_meas.preimage (h_qmp g⁻¹), }, have h_meas' : null_measurable_set {a | ∃ (g : G), g • a ∈ s} μ, { rw ← Union_smul_eq_set_of_exists, exact null_measurable_set.Union h_meas, }, rw [ae_iff_measure_eq h_meas', ← Union_smul_eq_set_of_exists], refine le_antisymm (measure_mono $ subset_univ _) _, rw measure_Union₀ ae_disjoint h_meas, exact h_measure_univ_le, end } @[to_additive] lemma Union_smul_ae_eq (h : is_fundamental_domain G s μ) : (⋃ g : G, g • s) =ᵐ[μ] univ := eventually_eq_univ.2 $ h.ae_covers.mono $ λ x ⟨g, hg⟩, mem_Union.2 ⟨g⁻¹, _, hg, inv_smul_smul _ _⟩ @[to_additive] lemma mono (h : is_fundamental_domain G s μ) {ν : measure α} (hle : ν ≪ μ) : is_fundamental_domain G s ν := ⟨h.1.mono_ac hle, hle h.2, λ g hg, hle (h.3 g hg)⟩ variables [measurable_space G] [has_measurable_smul G α] [smul_invariant_measure G α μ] @[to_additive] lemma null_measurable_set_smul (h : is_fundamental_domain G s μ) (g : G) : null_measurable_set (g • s) μ := h.null_measurable_set.smul g @[to_additive] lemma restrict_restrict (h : is_fundamental_domain G s μ) (g : G) (t : set α) : (μ.restrict t).restrict (g • s) = μ.restrict (g • s ∩ t) := restrict_restrict₀ ((h.null_measurable_set_smul g).mono restrict_le_self) @[to_additive] lemma pairwise_ae_disjoint (h : is_fundamental_domain G s μ) : pairwise (λ g₁ g₂ : G, ae_disjoint μ (g₁ • s) (g₂ • s)) := pairwise_ae_disjoint_of_ae_disjoint_forall_ne_one h.ae_disjoint (λ g, measure_preserving.quasi_measure_preserving $ by simp) @[to_additive] lemma pairwise_ae_disjoint_of_ac {ν} (h : is_fundamental_domain G s μ) (hν : ν ≪ μ) : pairwise (λ g₁ g₂ : G, ae_disjoint ν (g₁ • s) (g₂ • s)) := h.pairwise_ae_disjoint.mono $ λ g₁ g₂ H, hν H @[to_additive] lemma preimage_of_equiv {ν : measure β} (h : is_fundamental_domain G s μ) {f : β → α} (hf : quasi_measure_preserving f ν μ) {e : G → H} (he : bijective e) (hef : ∀ g, semiconj f ((•) (e g)) ((•) g)) : is_fundamental_domain H (f ⁻¹' s) ν := { null_measurable_set := h.null_measurable_set.preimage hf, ae_covers := (hf.ae h.ae_covers).mono $ λ x ⟨g, hg⟩, ⟨e g, by rwa [mem_preimage, hef g x]⟩, ae_disjoint := λ g hg, begin lift e to G ≃ H using he, have : (e.symm g⁻¹)⁻¹ ≠ (e.symm 1)⁻¹, by simp [hg], convert (h.pairwise_ae_disjoint this).preimage hf using 1, { simp only [← preimage_smul_inv, preimage_preimage, ← hef _ _, e.apply_symm_apply, inv_inv] }, { ext1 x, simp only [mem_preimage, ← preimage_smul, ← hef _ _, e.apply_symm_apply, one_smul] } end } @[to_additive] lemma image_of_equiv {ν : measure β} (h : is_fundamental_domain G s μ) (f : α ≃ β) (hf : quasi_measure_preserving f.symm ν μ) (e : H ≃ G) (hef : ∀ g, semiconj f ((•) (e g)) ((•) g)) : is_fundamental_domain H (f '' s) ν := begin rw f.image_eq_preimage, refine h.preimage_of_equiv hf e.symm.bijective (λ g x, _), rcases f.surjective x with ⟨x, rfl⟩, rw [← hef _ _, f.symm_apply_apply, f.symm_apply_apply, e.apply_symm_apply] end @[to_additive] lemma smul (h : is_fundamental_domain G s μ) (g : G) : is_fundamental_domain G (g • s) μ := h.image_of_equiv (mul_action.to_perm g) (measure_preserving_smul _ _).quasi_measure_preserving ⟨λ g', g⁻¹ * g' * g, λ g', g * g' * g⁻¹, λ g', by simp [mul_assoc], λ g', by simp [mul_assoc]⟩ $ λ g' x, by simp [smul_smul, mul_assoc] @[to_additive] lemma smul_of_comm {G' : Type*} [group G'] [mul_action G' α] [measurable_space G'] [has_measurable_smul G' α] [smul_invariant_measure G' α μ] [smul_comm_class G' G α] (h : is_fundamental_domain G s μ) (g : G') : is_fundamental_domain G (g • s) μ := h.image_of_equiv (mul_action.to_perm g) (measure_preserving_smul _ _).quasi_measure_preserving (equiv.refl _) $ smul_comm g variables [countable G] {ν : measure α} @[to_additive] lemma sum_restrict_of_ac (h : is_fundamental_domain G s μ) (hν : ν ≪ μ) : sum (λ g : G, ν.restrict (g • s)) = ν := by rw [← restrict_Union_ae (h.pairwise_ae_disjoint.mono $ λ i j h, hν h) (λ g, (h.null_measurable_set_smul g).mono_ac hν), restrict_congr_set (hν h.Union_smul_ae_eq), restrict_univ] @[to_additive] lemma lintegral_eq_tsum_of_ac (h : is_fundamental_domain G s μ) (hν : ν ≪ μ) (f : α → ℝ≥0∞) : ∫⁻ x, f x ∂ν = ∑' g : G, ∫⁻ x in g • s, f x ∂ν := by rw [← lintegral_sum_measure, h.sum_restrict_of_ac hν] @[to_additive] lemma sum_restrict (h : is_fundamental_domain G s μ) : sum (λ g : G, μ.restrict (g • s)) = μ := h.sum_restrict_of_ac (refl _) @[to_additive] lemma lintegral_eq_tsum (h : is_fundamental_domain G s μ) (f : α → ℝ≥0∞) : ∫⁻ x, f x ∂μ = ∑' g : G, ∫⁻ x in g • s, f x ∂μ := h.lintegral_eq_tsum_of_ac (refl _) f @[to_additive] lemma set_lintegral_eq_tsum' (h : is_fundamental_domain G s μ) (f : α → ℝ≥0∞) (t : set α) : ∫⁻ x in t, f x ∂μ = ∑' g : G, ∫⁻ x in t ∩ g • s, f x ∂μ := calc ∫⁻ x in t, f x ∂μ = ∑' g : G, ∫⁻ x in g • s, f x ∂(μ.restrict t) : h.lintegral_eq_tsum_of_ac restrict_le_self.absolutely_continuous _ ... = ∑' g : G, ∫⁻ x in t ∩ g • s, f x ∂μ : by simp only [h.restrict_restrict, inter_comm] @[to_additive] lemma set_lintegral_eq_tsum (h : is_fundamental_domain G s μ) (f : α → ℝ≥0∞) (t : set α) : ∫⁻ x in t, f x ∂μ = ∑' g : G, ∫⁻ x in g • t ∩ s, f (g⁻¹ • x) ∂μ := calc ∫⁻ x in t, f x ∂μ = ∑' g : G, ∫⁻ x in t ∩ g • s, f x ∂μ : h.set_lintegral_eq_tsum' f t ... = ∑' g : G, ∫⁻ x in t ∩ g⁻¹ • s, f x ∂μ : ((equiv.inv G).tsum_eq _).symm ... = ∑' g : G, ∫⁻ x in g⁻¹ • (g • t ∩ s), f (x) ∂μ : by simp only [smul_set_inter, inv_smul_smul] ... = ∑' g : G, ∫⁻ x in g • t ∩ s, f (g⁻¹ • x) ∂μ : tsum_congr $ λ g, ((measure_preserving_smul g⁻¹ μ).set_lintegral_comp_emb (measurable_embedding_const_smul _) _ _).symm @[to_additive] lemma measure_eq_tsum_of_ac (h : is_fundamental_domain G s μ) (hν : ν ≪ μ) (t : set α) : ν t = ∑' g : G, ν (t ∩ g • s) := have H : ν.restrict t ≪ μ, from measure.restrict_le_self.absolutely_continuous.trans hν, by simpa only [set_lintegral_one, pi.one_def, measure.restrict_apply₀ ((h.null_measurable_set_smul _).mono_ac H), inter_comm] using h.lintegral_eq_tsum_of_ac H 1 @[to_additive] lemma measure_eq_tsum' (h : is_fundamental_domain G s μ) (t : set α) : μ t = ∑' g : G, μ (t ∩ g • s) := h.measure_eq_tsum_of_ac absolutely_continuous.rfl t @[to_additive] lemma measure_eq_tsum (h : is_fundamental_domain G s μ) (t : set α) : μ t = ∑' g : G, μ (g • t ∩ s) := by simpa only [set_lintegral_one] using h.set_lintegral_eq_tsum (λ _, 1) t @[to_additive] lemma measure_zero_of_invariant (h : is_fundamental_domain G s μ) (t : set α) (ht : ∀ g : G, g • t = t) (hts : μ (t ∩ s) = 0) : μ t = 0 := by simp [measure_eq_tsum h, ht, hts] /-- Given a measure space with an action of a finite group `G`, the measure of any `G`-invariant set is determined by the measure of its intersection with a fundamental domain for the action of `G`. -/ @[to_additive measure_eq_card_smul_of_vadd_ae_eq_self "Given a measure space with an action of a finite additive group `G`, the measure of any `G`-invariant set is determined by the measure of its intersection with a fundamental domain for the action of `G`."] lemma measure_eq_card_smul_of_smul_ae_eq_self [finite G] (h : is_fundamental_domain G s μ) (t : set α) (ht : ∀ g : G, (g • t : set α) =ᵐ[μ] t) : μ t = nat.card G • μ (t ∩ s) := begin haveI : fintype G := fintype.of_finite G, rw h.measure_eq_tsum, replace ht : ∀ g : G, ((g • t) ∩ s : set α) =ᵐ[μ] (t ∩ s : set α) := λ g, ae_eq_set_inter (ht g) (ae_eq_refl s), simp_rw [measure_congr (ht _), tsum_fintype, finset.sum_const, nat.card_eq_fintype_card, finset.card_univ], end @[to_additive] protected lemma set_lintegral_eq (hs : is_fundamental_domain G s μ) (ht : is_fundamental_domain G t μ) (f : α → ℝ≥0∞) (hf : ∀ (g : G) x, f (g • x) = f x) : ∫⁻ x in s, f x ∂μ = ∫⁻ x in t, f x ∂μ := calc ∫⁻ x in s, f x ∂μ = ∑' g : G, ∫⁻ x in s ∩ g • t, f x ∂μ : ht.set_lintegral_eq_tsum' _ _ ... = ∑' g : G, ∫⁻ x in g • t ∩ s, f (g⁻¹ • x) ∂μ : by simp only [hf, inter_comm] ... = ∫⁻ x in t, f x ∂μ : (hs.set_lintegral_eq_tsum _ _).symm @[to_additive] lemma measure_set_eq (hs : is_fundamental_domain G s μ) (ht : is_fundamental_domain G t μ) {A : set α} (hA₀ : measurable_set A) (hA : ∀ (g : G), (λ x, g • x) ⁻¹' A = A) : μ (A ∩ s) = μ (A ∩ t) := begin have : ∫⁻ x in s, A.indicator 1 x ∂μ = ∫⁻ x in t, A.indicator 1 x ∂μ, { refine hs.set_lintegral_eq ht (set.indicator A (λ _, 1)) _, intros g x, convert (set.indicator_comp_right (λ x : α, g • x)).symm, rw hA g }, simpa [measure.restrict_apply hA₀, lintegral_indicator _ hA₀] using this end /-- If `s` and `t` are two fundamental domains of the same action, then their measures are equal. -/ @[to_additive "If `s` and `t` are two fundamental domains of the same action, then their measures are equal."] protected lemma measure_eq (hs : is_fundamental_domain G s μ) (ht : is_fundamental_domain G t μ) : μ s = μ t := by simpa only [set_lintegral_one] using hs.set_lintegral_eq ht (λ _, 1) (λ _ _, rfl) @[to_additive] protected lemma ae_strongly_measurable_on_iff {β : Type*} [topological_space β] [pseudo_metrizable_space β] (hs : is_fundamental_domain G s μ) (ht : is_fundamental_domain G t μ) {f : α → β} (hf : ∀ (g : G) x, f (g • x) = f x) : ae_strongly_measurable f (μ.restrict s) ↔ ae_strongly_measurable f (μ.restrict t) := calc ae_strongly_measurable f (μ.restrict s) ↔ ae_strongly_measurable f (measure.sum $ λ g : G, (μ.restrict (g • t ∩ s))) : by simp only [← ht.restrict_restrict, ht.sum_restrict_of_ac restrict_le_self.absolutely_continuous] ... ↔ ∀ g : G, ae_strongly_measurable f (μ.restrict (g • (g⁻¹ • s ∩ t))) : by simp only [smul_set_inter, inter_comm, smul_inv_smul, ae_strongly_measurable_sum_measure_iff] ... ↔ ∀ g : G, ae_strongly_measurable f (μ.restrict (g⁻¹ • (g⁻¹⁻¹ • s ∩ t))) : inv_surjective.forall ... ↔ ∀ g : G, ae_strongly_measurable f (μ.restrict (g⁻¹ • (g • s ∩ t))) : by simp only [inv_inv] ... ↔ ∀ g : G, ae_strongly_measurable f (μ.restrict (g • s ∩ t)) : begin refine forall_congr (λ g, _), have he : measurable_embedding ((•) g⁻¹ : α → α) := measurable_embedding_const_smul _, rw [← image_smul, ← ((measure_preserving_smul g⁻¹ μ).restrict_image_emb he _).ae_strongly_measurable_comp_iff he], simp only [(∘), hf] end ... ↔ ae_strongly_measurable f (μ.restrict t) : by simp only [← ae_strongly_measurable_sum_measure_iff, ← hs.restrict_restrict, hs.sum_restrict_of_ac restrict_le_self.absolutely_continuous] @[to_additive] protected lemma has_finite_integral_on_iff (hs : is_fundamental_domain G s μ) (ht : is_fundamental_domain G t μ) {f : α → E} (hf : ∀ (g : G) x, f (g • x) = f x) : has_finite_integral f (μ.restrict s) ↔ has_finite_integral f (μ.restrict t) := begin dunfold has_finite_integral, rw hs.set_lintegral_eq ht, intros g x, rw hf end @[to_additive] protected lemma integrable_on_iff (hs : is_fundamental_domain G s μ) (ht : is_fundamental_domain G t μ) {f : α → E} (hf : ∀ (g : G) x, f (g • x) = f x) : integrable_on f s μ ↔ integrable_on f t μ := and_congr (hs.ae_strongly_measurable_on_iff ht hf) (hs.has_finite_integral_on_iff ht hf) variables [normed_space ℝ E] [complete_space E] @[to_additive] protected lemma set_integral_eq (hs : is_fundamental_domain G s μ) (ht : is_fundamental_domain G t μ) {f : α → E} (hf : ∀ (g : G) x, f (g • x) = f x) : ∫ x in s, f x ∂μ = ∫ x in t, f x ∂μ := begin by_cases hfs : integrable_on f s μ, { have hft : integrable_on f t μ, by rwa ht.integrable_on_iff hs hf, have hac : ∀ {u}, μ.restrict u ≪ μ := λ u, restrict_le_self.absolutely_continuous, calc ∫ x in s, f x ∂μ = ∫ x in ⋃ g : G, g • t, f x ∂(μ.restrict s) : by rw [restrict_congr_set (hac ht.Union_smul_ae_eq), restrict_univ] ... = ∑' g : G, ∫ x in g • t, f x ∂(μ.restrict s) : integral_Union_ae (λ g, (ht.null_measurable_set_smul g).mono_ac hac) (ht.pairwise_ae_disjoint_of_ac hac) hfs.integrable.integrable_on ... = ∑' g : G, ∫ x in s ∩ g • t, f x ∂μ : by simp only [ht.restrict_restrict, inter_comm] ... = ∑' g : G, ∫ x in s ∩ g⁻¹ • t, f x ∂μ : ((equiv.inv G).tsum_eq _).symm ... = ∑' g : G, ∫ x in g⁻¹ • (g • s ∩ t), f x ∂μ : by simp only [smul_set_inter, inv_smul_smul] ... = ∑' g : G, ∫ x in g • s ∩ t, f (g⁻¹ • x) ∂μ : tsum_congr $ λ g, (measure_preserving_smul g⁻¹ μ).set_integral_image_emb (measurable_embedding_const_smul _) _ _ ... = ∑' g : G, ∫ x in g • s, f x ∂(μ.restrict t) : by simp only [hf, hs.restrict_restrict] ... = ∫ x in ⋃ g : G, g • s, f x ∂(μ.restrict t) : (integral_Union_ae (λ g, (hs.null_measurable_set_smul g).mono_ac hac) (hs.pairwise_ae_disjoint.mono $ λ i j h, hac h) hft.integrable.integrable_on).symm ... = ∫ x in t, f x ∂μ : by rw [restrict_congr_set (hac hs.Union_smul_ae_eq), restrict_univ] }, { rw [integral_undef hfs, integral_undef], rwa [hs.integrable_on_iff ht hf] at hfs } end /-- If the action of a countable group `G` admits an invariant measure `μ` with a fundamental domain `s`, then every null-measurable set `t` such that the sets `g • t ∩ s` are pairwise a.e.-disjoint has measure at most `μ s`. -/ @[to_additive "If the additive action of a countable group `G` admits an invariant measure `μ` with a fundamental domain `s`, then every null-measurable set `t` such that the sets `g +ᵥ t ∩ s` are pairwise a.e.-disjoint has measure at most `μ s`."] lemma measure_le_of_pairwise_disjoint (hs : is_fundamental_domain G s μ) (ht : null_measurable_set t μ) (hd : pairwise (ae_disjoint μ on (λ g : G, g • t ∩ s))) : μ t ≤ μ s := calc μ t = ∑' g : G, μ (g • t ∩ s) : hs.measure_eq_tsum t ... = μ (⋃ g : G, g • t ∩ s) : eq.symm $ measure_Union₀ hd $ λ g, (ht.smul _).inter hs.null_measurable_set ... ≤ μ s : measure_mono (Union_subset $ λ g, inter_subset_right _ _) /-- If the action of a countable group `G` admits an invariant measure `μ` with a fundamental domain `s`, then every null-measurable set `t` of measure strictly greater than `μ s` contains two points `x y` such that `g • x = y` for some `g ≠ 1`. -/ @[to_additive "If the additive action of a countable group `G` admits an invariant measure `μ` with a fundamental domain `s`, then every null-measurable set `t` of measure strictly greater than `μ s` contains two points `x y` such that `g +ᵥ x = y` for some `g ≠ 0`."] lemma exists_ne_one_smul_eq (hs : is_fundamental_domain G s μ) (htm : null_measurable_set t μ) (ht : μ s < μ t) : ∃ (x y ∈ t) (g ≠ (1 : G)), g • x = y := begin contrapose! ht, refine hs.measure_le_of_pairwise_disjoint htm (pairwise.ae_disjoint $ λ g₁ g₂ hne, _), dsimp [function.on_fun], refine (disjoint.inf_left _ _).inf_right _, rw set.disjoint_left, rintro _ ⟨x, hx, rfl⟩ ⟨y, hy, hxy⟩, refine ht x hx y hy (g₂⁻¹ * g₁) (mt inv_mul_eq_one.1 hne.symm) _, rw [mul_smul, ← hxy, inv_smul_smul] end /-- If `f` is invariant under the action of a countable group `G`, and `μ` is a `G`-invariant measure with a fundamental domain `s`, then the `ess_sup` of `f` restricted to `s` is the same as that of `f` on all of its domain. -/ @[to_additive "If `f` is invariant under the action of a countable additive group `G`, and `μ` is a `G`-invariant measure with a fundamental domain `s`, then the `ess_sup` of `f` restricted to `s` is the same as that of `f` on all of its domain."] lemma ess_sup_measure_restrict (hs : is_fundamental_domain G s μ) {f : α → ℝ≥0∞} (hf : ∀ γ : G, ∀ x: α, f (γ • x) = f x) : ess_sup f (μ.restrict s) = ess_sup f μ := begin refine le_antisymm (ess_sup_mono_measure' measure.restrict_le_self) _, rw [ess_sup_eq_Inf (μ.restrict s) f, ess_sup_eq_Inf μ f], refine Inf_le_Inf _, rintro a (ha : (μ.restrict s) {x : α | a < f x} = 0), rw measure.restrict_apply₀' hs.null_measurable_set at ha, refine measure_zero_of_invariant hs _ _ ha, intros γ, ext x, rw mem_smul_set_iff_inv_smul_mem, simp only [mem_set_of_eq, hf (γ⁻¹) x], end end is_fundamental_domain end measure_theory
965552ca605a992a2703ba451e95672a6c8292ef
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/field_theory/splitting_field.lean
96768919418fee05a392221597c947af8ba1e37b
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
18,556
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes Definition of splitting fields, and definition of homomorphism into any field that splits -/ import ring_theory.adjoin_root import ring_theory.algebra_tower import ring_theory.polynomial import field_theory.minimal_polynomial universes u v w variables {α : Type u} {β : Type v} {γ : Type w} namespace polynomial noncomputable theory open_locale classical big_operators variables [field α] [field β] [field γ] open polynomial section splits variables (i : α →+* β) /-- a polynomial `splits` iff it is zero or all of its irreducible factors have `degree` 1 -/ def splits (f : polynomial α) : Prop := f = 0 ∨ ∀ {g : polynomial β}, irreducible g → g ∣ f.map i → degree g = 1 @[simp] lemma splits_zero : splits i (0 : polynomial α) := or.inl rfl @[simp] lemma splits_C (a : α) : splits i (C a) := if ha : a = 0 then ha.symm ▸ (@C_0 α _).symm ▸ splits_zero i else have hia : i a ≠ 0, from mt ((is_add_group_hom.injective_iff i).1 i.injective _) ha, or.inr $ λ g hg ⟨p, hp⟩, absurd hg.1 (classical.not_not.2 (is_unit_iff_degree_eq_zero.2 $ by have := congr_arg degree hp; simp [degree_C hia, @eq_comm (with_bot ℕ) 0, nat.with_bot.add_eq_zero_iff] at this; clear _fun_match; tautology)) lemma splits_of_degree_eq_one {f : polynomial α} (hf : degree f = 1) : splits i f := or.inr $ λ g hg ⟨p, hp⟩, by have := congr_arg degree hp; simp [nat.with_bot.add_eq_one_iff, hf, @eq_comm (with_bot ℕ) 1, mt is_unit_iff_degree_eq_zero.2 hg.1] at this; clear _fun_match; tauto lemma splits_of_degree_le_one {f : polynomial α} (hf : degree f ≤ 1) : splits i f := begin cases h : degree f with n, { rw [degree_eq_bot.1 h]; exact splits_zero i }, { cases n with n, { rw [eq_C_of_degree_le_zero (trans_rel_right (≤) h (le_refl _))]; exact splits_C _ _ }, { have hn : n = 0, { rw h at hf, cases n, { refl }, { exact absurd hf dec_trivial } }, exact splits_of_degree_eq_one _ (by rw [h, hn]; refl) } } end lemma splits_mul {f g : polynomial α} (hf : splits i f) (hg : splits i g) : splits i (f * g) := if h : f * g = 0 then by simp [h] else or.inr $ λ p hp hpf, ((principal_ideal_ring.irreducible_iff_prime.1 hp).2.2 _ _ (show p ∣ map i f * map i g, by convert hpf; rw polynomial.map_mul)).elim (hf.resolve_left (λ hf, by simpa [hf] using h) hp) (hg.resolve_left (λ hg, by simpa [hg] using h) hp) lemma splits_of_splits_mul {f g : polynomial α} (hfg : f * g ≠ 0) (h : splits i (f * g)) : splits i f ∧ splits i g := ⟨or.inr $ λ g hgi hg, or.resolve_left h hfg hgi (by rw map_mul; exact dvd.trans hg (dvd_mul_right _ _)), or.inr $ λ g hgi hg, or.resolve_left h hfg hgi (by rw map_mul; exact dvd.trans hg (dvd_mul_left _ _))⟩ lemma splits_of_splits_of_dvd {f g : polynomial α} (hf0 : f ≠ 0) (hf : splits i f) (hgf : g ∣ f) : splits i g := by { obtain ⟨f, rfl⟩ := hgf, exact (splits_of_splits_mul i hf0 hf).1 } lemma splits_map_iff (j : β →+* γ) {f : polynomial α} : splits j (f.map i) ↔ splits (j.comp i) f := by simp [splits, polynomial.map_map] theorem splits_one : splits i 1 := splits_C i 1 theorem splits_X_sub_C {x : α} : (X - C x).splits i := splits_of_degree_eq_one _ $ degree_X_sub_C x theorem splits_id_iff_splits {f : polynomial α} : (f.map i).splits (ring_hom.id β) ↔ f.splits i := by rw [splits_map_iff, ring_hom.id_comp] theorem splits_mul_iff {f g : polynomial α} (hf : f ≠ 0) (hg : g ≠ 0) : (f * g).splits i ↔ f.splits i ∧ g.splits i := ⟨splits_of_splits_mul i (mul_ne_zero hf hg), λ ⟨hfs, hgs⟩, splits_mul i hfs hgs⟩ theorem splits_prod {ι : Type w} {s : ι → polynomial α} {t : finset ι} : (∀ j ∈ t, (s j).splits i) → (∏ x in t, s x).splits i := begin refine finset.induction_on t (λ _, splits_one i) (λ a t hat ih ht, _), rw finset.forall_mem_insert at ht, rw finset.prod_insert hat, exact splits_mul i ht.1 (ih ht.2) end theorem splits_prod_iff {ι : Type w} {s : ι → polynomial α} {t : finset ι} : (∀ j ∈ t, s j ≠ 0) → ((∏ x in t, s x).splits i ↔ ∀ j ∈ t, (s j).splits i) := begin refine finset.induction_on t (λ _, ⟨λ _ _ h, h.elim, λ _, splits_one i⟩) (λ a t hat ih ht, _), rw finset.forall_mem_insert at ht ⊢, rw [finset.prod_insert hat, splits_mul_iff i ht.1 (finset.prod_ne_zero_iff.2 ht.2), ih ht.2] end lemma exists_root_of_splits {f : polynomial α} (hs : splits i f) (hf0 : degree f ≠ 0) : ∃ x, eval₂ i x f = 0 := if hf0 : f = 0 then ⟨37, by simp [hf0]⟩ else let ⟨g, hg⟩ := is_noetherian_ring.exists_irreducible_factor (show ¬ is_unit (f.map i), from mt is_unit_iff_degree_eq_zero.1 (by rwa degree_map)) (by rw [ne.def, map_eq_zero]; exact hf0) in let ⟨x, hx⟩ := exists_root_of_degree_eq_one (hs.resolve_left hf0 hg.1 hg.2) in let ⟨i, hi⟩ := hg.2 in ⟨x, by rw [← eval_map, hi, eval_mul, show _ = _, from hx, zero_mul]⟩ lemma exists_multiset_of_splits {f : polynomial α} : splits i f → ∃ (s : multiset β), f.map i = C (i f.leading_coeff) * (s.map (λ a : β, (X : polynomial β) - C a)).prod := suffices splits (ring_hom.id _) (f.map i) → ∃ s : multiset β, f.map i = (C (f.map i).leading_coeff) * (s.map (λ a : β, (X : polynomial β) - C a)).prod, by rwa [splits_map_iff, leading_coeff_map i] at this, is_noetherian_ring.irreducible_induction_on (f.map i) (λ _, ⟨{37}, by simp [i.map_zero]⟩) (λ u hu _, ⟨0, by conv_lhs { rw eq_C_of_degree_eq_zero (is_unit_iff_degree_eq_zero.1 hu) }; simp [leading_coeff, nat_degree_eq_of_degree_eq_some (is_unit_iff_degree_eq_zero.1 hu)]⟩) (λ f p hf0 hp ih hfs, have hpf0 : p * f ≠ 0, from mul_ne_zero hp.ne_zero hf0, let ⟨s, hs⟩ := ih (splits_of_splits_mul _ hpf0 hfs).2 in ⟨-(p * norm_unit p).coeff 0 :: s, have hp1 : degree p = 1, from hfs.resolve_left hpf0 hp (by simp), begin rw [multiset.map_cons, multiset.prod_cons, leading_coeff_mul, C_mul, mul_assoc, mul_left_comm (C f.leading_coeff), ← hs, ← mul_assoc, mul_left_inj' hf0], conv_lhs {rw eq_X_add_C_of_degree_eq_one hp1}, simp only [mul_add, coe_norm_unit hp.ne_zero, mul_comm p, coeff_neg, C_neg, sub_eq_add_neg, neg_neg, coeff_C_mul, (mul_assoc _ _ _).symm, C_mul.symm, mul_inv_cancel (show p.leading_coeff ≠ 0, from mt leading_coeff_eq_zero.1 hp.ne_zero), one_mul], end⟩) section UFD local attribute [instance, priority 10] principal_ideal_ring.to_unique_factorization_domain local infix ` ~ᵤ ` : 50 := associated open unique_factorization_domain associates lemma splits_of_exists_multiset {f : polynomial α} {s : multiset β} (hs : f.map i = C (i f.leading_coeff) * (s.map (λ a : β, (X : polynomial β) - C a)).prod) : splits i f := if hf0 : f = 0 then or.inl hf0 else or.inr $ λ p hp hdp, have ht : multiset.rel associated (factors (f.map i)) (s.map (λ a : β, (X : polynomial β) - C a)) := unique (λ p hp, irreducible_factors (mt (map_eq_zero i).1 hf0) _ hp) (λ p' m, begin obtain ⟨a,m,rfl⟩ := multiset.mem_map.1 m, exact irreducible_of_degree_eq_one (degree_X_sub_C _), end) (associated.symm $ calc _ ~ᵤ f.map i : ⟨(units.map' C : units β →* units (polynomial β)) (units.mk0 (f.map i).leading_coeff (mt leading_coeff_eq_zero.1 (mt (map_eq_zero i).1 hf0))), by conv_rhs {rw [hs, ← leading_coeff_map i, mul_comm]}; refl⟩ ... ~ᵤ _ : associated.symm (unique_factorization_domain.factors_prod (by simpa using hf0))), let ⟨q, hq, hpq⟩ := exists_mem_factors_of_dvd (by simpa) hp hdp in let ⟨q', hq', hqq'⟩ := multiset.exists_mem_of_rel_of_mem ht hq in let ⟨a, ha⟩ := multiset.mem_map.1 hq' in by rw [← degree_X_sub_C a, ha.2]; exact degree_eq_degree_of_associated (hpq.trans hqq') lemma splits_of_splits_id {f : polynomial α} : splits (ring_hom.id _) f → splits i f := unique_factorization_domain.induction_on_prime f (λ _, splits_zero _) (λ _ hu _, splits_of_degree_le_one _ ((is_unit_iff_degree_eq_zero.1 hu).symm ▸ dec_trivial)) (λ a p ha0 hp ih hfi, splits_mul _ (splits_of_degree_eq_one _ ((splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).1.resolve_left hp.1 (irreducible_of_prime hp) (by rw map_id))) (ih (splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).2)) end UFD lemma splits_iff_exists_multiset {f : polynomial α} : splits i f ↔ ∃ (s : multiset β), f.map i = C (i f.leading_coeff) * (s.map (λ a : β, (X : polynomial β) - C a)).prod := ⟨exists_multiset_of_splits i, λ ⟨s, hs⟩, splits_of_exists_multiset i hs⟩ lemma splits_comp_of_splits (j : β →+* γ) {f : polynomial α} (h : splits i f) : splits (j.comp i) f := begin change i with ((ring_hom.id _).comp i) at h, rw [← splits_map_iff], rw [← splits_map_iff i] at h, exact splits_of_splits_id _ h end end splits section splitting_field /-- Non-computably choose an irreducible factor from a polynomial. -/ def factor (f : polynomial α) : polynomial α := if H : ∃ g, irreducible g ∧ g ∣ f then classical.some H else X instance irreducible_factor (f : polynomial α) : irreducible (factor f) := begin rw factor, split_ifs with H, { exact (classical.some_spec H).1 }, { exact irreducible_X } end theorem factor_dvd_of_not_is_unit {f : polynomial α} (hf1 : ¬is_unit f) : factor f ∣ f := begin by_cases hf2 : f = 0, { rw hf2, exact dvd_zero _ }, rw [factor, dif_pos (is_noetherian_ring.exists_irreducible_factor hf1 hf2)], exact (classical.some_spec $ is_noetherian_ring.exists_irreducible_factor hf1 hf2).2 end theorem factor_dvd_of_degree_ne_zero {f : polynomial α} (hf : f.degree ≠ 0) : factor f ∣ f := factor_dvd_of_not_is_unit (mt degree_eq_zero_of_is_unit hf) theorem factor_dvd_of_nat_degree_ne_zero {f : polynomial α} (hf : f.nat_degree ≠ 0) : factor f ∣ f := factor_dvd_of_degree_ne_zero (mt nat_degree_eq_of_degree_eq_some hf) /-- Divide a polynomial f by X - C r where r is a root of f in a bigger field extension. -/ def remove_factor (f : polynomial α) : polynomial (adjoin_root $ factor f) := map (adjoin_root.of f.factor) f /ₘ (X - C (adjoin_root.root f.factor)) theorem X_sub_C_mul_remove_factor (f : polynomial α) (hf : f.nat_degree ≠ 0) : (X - C (adjoin_root.root f.factor)) * f.remove_factor = map (adjoin_root.of f.factor) f := let ⟨g, hg⟩ := factor_dvd_of_nat_degree_ne_zero hf in mul_div_by_monic_eq_iff_is_root.2 $ by rw [is_root.def, eval_map, hg, eval₂_mul, ← hg, adjoin_root.eval₂_root, zero_mul] theorem nat_degree_remove_factor (f : polynomial α) : f.remove_factor.nat_degree = f.nat_degree - 1 := by rw [remove_factor, nat_degree_div_by_monic _ (monic_X_sub_C _), nat_degree_map, nat_degree_X_sub_C] theorem nat_degree_remove_factor' {f : polynomial α} {n : ℕ} (hfn : f.nat_degree = n+1) : f.remove_factor.nat_degree = n := by rw [nat_degree_remove_factor, hfn, n.add_sub_cancel] /-- Auxiliary construction to a splitting field of a polynomial. Uses induction on the degree. -/ def splitting_field_aux (n : ℕ) : Π {α : Type u} [field α], by exactI Π (f : polynomial α), f.nat_degree = n → Type u := nat.rec_on n (λ α _ _ _, α) $ λ n ih α _ f hf, by exactI ih f.remove_factor (nat_degree_remove_factor' hf) namespace splitting_field_aux theorem succ (n : ℕ) (f : polynomial α) (hfn : f.nat_degree = n + 1) : splitting_field_aux (n+1) f hfn = splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn) := rfl instance field (n : ℕ) : Π {α : Type u} [field α], by exactI Π {f : polynomial α} (hfn : f.nat_degree = n), field (splitting_field_aux n f hfn) := nat.rec_on n (λ α _ _ _, ‹field α›) $ λ n ih α _ f hf, ih _ instance inhabited {n : ℕ} {f : polynomial α} (hfn : f.nat_degree = n) : inhabited (splitting_field_aux n f hfn) := ⟨37⟩ instance algebra (n : ℕ) : Π {α : Type u} [field α], by exactI Π {f : polynomial α} (hfn : f.nat_degree = n), algebra α (splitting_field_aux n f hfn) := nat.rec_on n (λ α _ _ _, by exactI algebra.id α) $ λ n ih α _ f hfn, by exactI @@algebra.comap.algebra _ _ _ _ _ _ _ (ih _) instance algebra' {n : ℕ} {f : polynomial α} (hfn : f.nat_degree = n + 1) : algebra (adjoin_root f.factor) (splitting_field_aux _ _ hfn) := splitting_field_aux.algebra n _ instance algebra'' {n : ℕ} {f : polynomial α} (hfn : f.nat_degree = n + 1) : algebra α (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)) := splitting_field_aux.algebra (n+1) hfn instance algebra''' {n : ℕ} {f : polynomial α} (hfn : f.nat_degree = n + 1) : algebra (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)) := splitting_field_aux.algebra n _ instance algebra_tower {n : ℕ} {f : polynomial α} (hfn : f.nat_degree = n + 1) : is_algebra_tower α (adjoin_root f.factor) (splitting_field_aux _ _ hfn) := is_algebra_tower.of_algebra_map_eq $ λ x, rfl instance algebra_tower' {n : ℕ} {f : polynomial α} (hfn : f.nat_degree = n + 1) : is_algebra_tower α (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)) := is_algebra_tower.of_algebra_map_eq $ λ x, rfl theorem algebra_map_succ (n : ℕ) (f : polynomial α) (hfn : f.nat_degree = n + 1) : by exact algebra_map α (splitting_field_aux _ _ hfn) = (algebra_map (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn))).comp (adjoin_root.of f.factor) := rfl protected theorem splits (n : ℕ) : ∀ {α : Type u} [field α], by exactI ∀ (f : polynomial α) (hfn : f.nat_degree = n), splits (algebra_map α $ splitting_field_aux n f hfn) f := nat.rec_on n (λ α _ _ hf, by exactI splits_of_degree_le_one _ (le_trans degree_le_nat_degree $ hf.symm ▸ with_bot.coe_le_coe.2 zero_le_one)) $ λ n ih α _ f hf, by { resetI, rw [← splits_id_iff_splits, algebra_map_succ, ← map_map, splits_id_iff_splits, ← X_sub_C_mul_remove_factor f (λ h, by { rw h at hf, cases hf })], exact splits_mul _ (splits_X_sub_C _) (ih _ _) } theorem exists_lift (n : ℕ) : ∀ {α : Type u} [field α], by exactI ∀ (f : polynomial α) (hfn : f.nat_degree = n) {β : Type*} [field β], by exactI ∀ (j : α →+* β) (hf : splits j f), ∃ k : splitting_field_aux n f hfn →+* β, k.comp (algebra_map _ _) = j := nat.rec_on n (λ α _ _ _ β _ j _, by exactI ⟨j, j.comp_id⟩) $ λ n ih α _ f hf β _ j hj, by exactI have hndf : f.nat_degree ≠ 0, by { intro h, rw h at hf, cases hf }, have hfn0 : f ≠ 0, by { intro h, rw h at hndf, exact hndf rfl }, let ⟨r, hr⟩ := exists_root_of_splits _ (splits_of_splits_of_dvd j hfn0 hj (factor_dvd_of_nat_degree_ne_zero hndf)) (mt is_unit_iff_degree_eq_zero.2 f.irreducible_factor.1) in have hmf0 : map (adjoin_root.of f.factor) f ≠ 0, from mt (map_eq_zero _).1 hfn0, have hsf : splits (adjoin_root.lift j r hr) f.remove_factor, by { rw ← X_sub_C_mul_remove_factor _ hndf at hmf0, refine (splits_of_splits_mul _ hmf0 _).2, rwa [X_sub_C_mul_remove_factor _ hndf, ← splits_id_iff_splits, map_map, adjoin_root.lift_comp_of, splits_id_iff_splits] }, let ⟨k, hk⟩ := ih f.remove_factor (nat_degree_remove_factor' hf) (adjoin_root.lift j r hr) hsf in ⟨k, by rw [algebra_map_succ, ← ring_hom.comp_assoc, hk, adjoin_root.lift_comp_of]⟩ theorem adjoin_roots (n : ℕ) : ∀ {α : Type u} [field α], by exactI ∀ (f : polynomial α) (hfn : f.nat_degree = n), algebra.adjoin α (↑(f.map $ algebra_map α $ splitting_field_aux n f hfn).roots : set (splitting_field_aux n f hfn)) = ⊤ := nat.rec_on n (λ α _ f hf, by exactI algebra.eq_top_iff.2 (λ x, subalgebra.range_le _ ⟨x, rfl⟩)) $ λ n ih α _ f hfn, by exactI have hndf : f.nat_degree ≠ 0, by { intro h, rw h at hfn, cases hfn }, have hfn0 : f ≠ 0, by { intro h, rw h at hndf, exact hndf rfl }, have hmf0 : map (algebra_map α (splitting_field_aux n.succ f hfn)) f ≠ 0 := mt (map_eq_zero _).1 hfn0, by { rw [algebra_map_succ, ← map_map, ← X_sub_C_mul_remove_factor _ hndf, map_mul] at hmf0 ⊢, rw [roots_mul hmf0, map_sub, map_X, map_C, roots_X_sub_C, finset.coe_union, finset.coe_singleton, algebra.adjoin_union, ← set.image_singleton, algebra.adjoin_algebra_map α (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)), adjoin_root.adjoin_root_eq_top, algebra.map_top, is_algebra_tower.range_under_adjoin α (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)), ih, is_algebra_tower.subalgebra_comap_top] } end splitting_field_aux /-- A splitting field of a polynomial. -/ def splitting_field (f : polynomial α) := splitting_field_aux _ f rfl namespace splitting_field variables (f : polynomial α) instance : field (splitting_field f) := splitting_field_aux.field _ _ instance inhabited : inhabited (splitting_field f) := ⟨37⟩ instance : algebra α (splitting_field f) := splitting_field_aux.algebra _ _ protected theorem splits : splits (algebra_map α (splitting_field f)) f := splitting_field_aux.splits _ _ _ variables [algebra α β] (hb : splits (algebra_map α β) f) /-- Embeds the splitting field into any other field that splits the polynomial. -/ def lift : splitting_field f →ₐ[α] β := { commutes' := λ r, by { have := classical.some_spec (splitting_field_aux.exists_lift _ _ _ _ hb), exact ring_hom.ext_iff.1 this r }, .. classical.some (splitting_field_aux.exists_lift _ _ _ _ hb) } theorem adjoin_roots : algebra.adjoin α (↑(f.map (algebra_map α $ splitting_field f)).roots : set (splitting_field f)) = ⊤ := splitting_field_aux.adjoin_roots _ _ _ end splitting_field variables (α β) [algebra α β] /-- Typeclass characterising splitting fields. -/ class is_splitting_field (f : polynomial α) : Prop := (splits : splits (algebra_map α β) f) (adjoin_roots : algebra.adjoin α (↑(f.map (algebra_map α β)).roots : set β) = ⊤) variables {α} instance is_splitting_field_splitting_field (f : polynomial α) : is_splitting_field α (splitting_field f) f := ⟨splitting_field.splits f, splitting_field.adjoin_roots f⟩ end splitting_field end polynomial
c7125b69eab3bfb082201e5cd9ba1d6f96649ced
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/polynomial/degree/definitions.lean
75174d45d8162b8b6071a55eee18f36882218bff
[ "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
40,442
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.nat.with_bot import data.polynomial.induction import data.polynomial.monomial /-! # Theory of univariate polynomials The definitions include `degree`, `monic`, `leading_coeff` Results include - `degree_mul` : The degree of the product is the sum of degrees - `leading_coeff_add_of_degree_eq` and `leading_coeff_add_of_degree_lt` : The leading_coefficient of a sum is determined by the leading coefficients and degrees -/ noncomputable theory open finsupp finset open_locale big_operators classical namespace polynomial universes u v variables {R : Type u} {S : Type v} {a b : R} {n m : ℕ} section semiring variables [semiring R] {p q r : polynomial R} /-- `degree p` is the degree of the polynomial `p`, i.e. the largest `X`-exponent in `p`. `degree p = some n` when `p ≠ 0` and `n` is the highest power of `X` that appears in `p`, otherwise `degree 0 = ⊥`. -/ def degree (p : polynomial R) : with_bot ℕ := p.support.sup some lemma degree_lt_wf : well_founded (λp q : polynomial R, degree p < degree q) := inv_image.wf degree (with_bot.well_founded_lt nat.lt_wf) instance : has_well_founded (polynomial R) := ⟨_, degree_lt_wf⟩ /-- `nat_degree p` forces `degree p` to ℕ, by defining nat_degree 0 = 0. -/ def nat_degree (p : polynomial R) : ℕ := (degree p).get_or_else 0 /-- `leading_coeff p` gives the coefficient of the highest power of `X` in `p`-/ def leading_coeff (p : polynomial R) : R := coeff p (nat_degree p) /-- a polynomial is `monic` if its leading coefficient is 1 -/ def monic (p : polynomial R) := leading_coeff p = (1 : R) @[nontriviality] lemma monic_of_subsingleton [subsingleton R] (p : polynomial R) : monic p := subsingleton.elim _ _ lemma monic.def : monic p ↔ leading_coeff p = 1 := iff.rfl instance monic.decidable [decidable_eq R] : decidable (monic p) := by unfold monic; apply_instance @[simp] lemma monic.leading_coeff {p : polynomial R} (hp : p.monic) : leading_coeff p = 1 := hp lemma monic.coeff_nat_degree {p : polynomial R} (hp : p.monic) : p.coeff p.nat_degree = 1 := hp @[simp] lemma degree_zero : degree (0 : polynomial R) = ⊥ := rfl @[simp] lemma nat_degree_zero : nat_degree (0 : polynomial R) = 0 := rfl @[simp] lemma coeff_nat_degree : coeff p (nat_degree p) = leading_coeff p := rfl lemma degree_eq_bot : degree p = ⊥ ↔ p = 0 := ⟨λ h, by rw [degree, ← max_eq_sup_with_bot] at h; exact support_eq_empty.1 (max_eq_none.1 h), λ h, h.symm ▸ rfl⟩ @[nontriviality] lemma degree_of_subsingleton [subsingleton R] : degree p = ⊥ := by rw [subsingleton.elim p 0, degree_zero] @[nontriviality] lemma nat_degree_of_subsingleton [subsingleton R] : nat_degree p = 0 := by rw [subsingleton.elim p 0, nat_degree_zero] lemma degree_eq_nat_degree (hp : p ≠ 0) : degree p = (nat_degree p : with_bot ℕ) := let ⟨n, hn⟩ := not_forall.1 (mt option.eq_none_iff_forall_not_mem.2 (mt degree_eq_bot.1 hp)) in have hn : degree p = some n := not_not.1 hn, by rw [nat_degree, hn]; refl lemma degree_eq_iff_nat_degree_eq {p : polynomial R} {n : ℕ} (hp : p ≠ 0) : p.degree = n ↔ p.nat_degree = n := by rw [degree_eq_nat_degree hp, with_bot.coe_eq_coe] lemma degree_eq_iff_nat_degree_eq_of_pos {p : polynomial R} {n : ℕ} (hn : 0 < n) : p.degree = n ↔ p.nat_degree = n := begin split, { intro H, rwa ← degree_eq_iff_nat_degree_eq, rintro rfl, rw degree_zero at H, exact option.no_confusion H }, { intro H, rwa degree_eq_iff_nat_degree_eq, rintro rfl, rw nat_degree_zero at H, rw H at hn, exact lt_irrefl _ hn } end lemma nat_degree_eq_of_degree_eq_some {p : polynomial R} {n : ℕ} (h : degree p = n) : nat_degree p = n := have hp0 : p ≠ 0, from λ hp0, by rw hp0 at h; exact option.no_confusion h, option.some_inj.1 $ show (nat_degree p : with_bot ℕ) = n, by rwa [← degree_eq_nat_degree hp0] @[simp] lemma degree_le_nat_degree : degree p ≤ nat_degree p := with_bot.gi_get_or_else_bot.gc.le_u_l _ lemma nat_degree_eq_of_degree_eq [semiring S] {q : polynomial S} (h : degree p = degree q) : nat_degree p = nat_degree q := by unfold nat_degree; rw h lemma le_degree_of_ne_zero (h : coeff p n ≠ 0) : (n : with_bot ℕ) ≤ degree p := show @has_le.le (with_bot ℕ) _ (some n : with_bot ℕ) (p.support.sup some : with_bot ℕ), from finset.le_sup (mem_support_iff.2 h) lemma le_nat_degree_of_ne_zero (h : coeff p n ≠ 0) : n ≤ nat_degree p := begin rw [← with_bot.coe_le_coe, ← degree_eq_nat_degree], exact le_degree_of_ne_zero h, { assume h, subst h, exact h rfl } end lemma le_nat_degree_of_mem_supp (a : ℕ) : a ∈ p.support → a ≤ nat_degree p:= le_nat_degree_of_ne_zero ∘ mem_support_iff.mp lemma supp_subset_range (h : nat_degree p < m) : p.support ⊆ finset.range m := λ n hn, mem_range.2 $ (le_nat_degree_of_mem_supp _ hn).trans_lt h lemma supp_subset_range_nat_degree_succ : p.support ⊆ finset.range (nat_degree p + 1) := supp_subset_range (nat.lt_succ_self _) lemma degree_le_degree (h : coeff q (nat_degree p) ≠ 0) : degree p ≤ degree q := begin by_cases hp : p = 0, { rw hp, exact bot_le }, { rw degree_eq_nat_degree hp, exact le_degree_of_ne_zero h } end lemma degree_ne_of_nat_degree_ne {n : ℕ} : p.nat_degree ≠ n → degree p ≠ n := mt $ λ h, by rw [nat_degree, h, option.get_or_else_coe] theorem nat_degree_le_iff_degree_le {n : ℕ} : nat_degree p ≤ n ↔ degree p ≤ n := with_bot.get_or_else_bot_le_iff alias polynomial.nat_degree_le_iff_degree_le ↔ . . lemma nat_degree_le_nat_degree [semiring S] {q : polynomial S} (hpq : p.degree ≤ q.degree) : p.nat_degree ≤ q.nat_degree := with_bot.gi_get_or_else_bot.gc.monotone_l hpq @[simp] lemma degree_C (ha : a ≠ 0) : degree (C a) = (0 : with_bot ℕ) := by { rw [degree, ← monomial_zero_left, support_monomial 0 _ ha, sup_singleton], refl } lemma degree_C_le : degree (C a) ≤ 0 := by by_cases h : a = 0; [rw [h, C_0], rw [degree_C h]]; [exact bot_le, exact le_refl _] lemma degree_one_le : degree (1 : polynomial R) ≤ (0 : with_bot ℕ) := by rw [← C_1]; exact degree_C_le @[simp] lemma nat_degree_C (a : R) : nat_degree (C a) = 0 := begin by_cases ha : a = 0, { have : C a = 0, { rw [ha, C_0] }, rw [nat_degree, degree_eq_bot.2 this], refl }, { rw [nat_degree, degree_C ha], refl } end @[simp] lemma nat_degree_one : nat_degree (1 : polynomial R) = 0 := nat_degree_C 1 @[simp] lemma nat_degree_nat_cast (n : ℕ) : nat_degree (n : polynomial R) = 0 := by simp only [←C_eq_nat_cast, nat_degree_C] @[simp] lemma degree_monomial (n : ℕ) (ha : a ≠ 0) : degree (monomial n a) = n := by rw [degree, support_monomial _ _ ha]; refl @[simp] lemma degree_C_mul_X_pow (n : ℕ) (ha : a ≠ 0) : degree (C a * X ^ n) = n := by rw [← monomial_eq_C_mul_X, degree_monomial n ha] lemma degree_monomial_le (n : ℕ) (a : R) : degree (monomial n a) ≤ n := if h : a = 0 then by rw [h, (monomial n).map_zero]; exact bot_le else le_of_eq (degree_monomial n h) lemma degree_C_mul_X_pow_le (n : ℕ) (a : R) : degree (C a * X ^ n) ≤ n := by { rw C_mul_X_pow_eq_monomial, apply degree_monomial_le } lemma degree_C_mul_X_le (a : R) : degree (C a * X) ≤ 1 := by simpa only [pow_one] using degree_C_mul_X_pow_le 1 a @[simp] lemma nat_degree_C_mul_X_pow (n : ℕ) (a : R) (ha : a ≠ 0) : nat_degree (C a * X ^ n) = n := nat_degree_eq_of_degree_eq_some (degree_C_mul_X_pow n ha) @[simp] lemma nat_degree_C_mul_X (a : R) (ha : a ≠ 0) : nat_degree (C a * X) = 1 := by simpa only [pow_one] using nat_degree_C_mul_X_pow 1 a ha @[simp] lemma nat_degree_monomial (i : ℕ) (r : R) (hr : r ≠ 0) : nat_degree (monomial i r) = i := by rw [← C_mul_X_pow_eq_monomial, nat_degree_C_mul_X_pow i r hr] lemma coeff_eq_zero_of_degree_lt (h : degree p < n) : coeff p n = 0 := not_not.1 (mt le_degree_of_ne_zero (not_le_of_gt h)) lemma coeff_eq_zero_of_nat_degree_lt {p : polynomial R} {n : ℕ} (h : p.nat_degree < n) : p.coeff n = 0 := begin apply coeff_eq_zero_of_degree_lt, by_cases hp : p = 0, { subst hp, exact with_bot.bot_lt_coe n }, { rwa [degree_eq_nat_degree hp, with_bot.coe_lt_coe] } end @[simp] lemma coeff_nat_degree_succ_eq_zero {p : polynomial R} : p.coeff (p.nat_degree + 1) = 0 := coeff_eq_zero_of_nat_degree_lt (lt_add_one _) -- We need the explicit `decidable` argument here because an exotic one shows up in a moment! lemma ite_le_nat_degree_coeff (p : polynomial R) (n : ℕ) (I : decidable (n < 1 + nat_degree p)) : @ite _ (n < 1 + nat_degree p) I (coeff p n) 0 = coeff p n := begin split_ifs, { refl }, { exact (coeff_eq_zero_of_nat_degree_lt (not_le.1 (λ w, h (nat.lt_one_add_iff.2 w)))).symm, } end lemma as_sum_support (p : polynomial R) : p = ∑ i in p.support, monomial i (p.coeff i) := (sum_monomial_eq p).symm lemma as_sum_support_C_mul_X_pow (p : polynomial R) : p = ∑ i in p.support, C (p.coeff i) * X^i := trans p.as_sum_support $ by simp only [C_mul_X_pow_eq_monomial] /-- We can reexpress a sum over `p.support` as a sum over `range n`, for any `n` satisfying `p.nat_degree < n`. -/ lemma sum_over_range' [add_comm_monoid S] (p : polynomial R) {f : ℕ → R → S} (h : ∀ n, f n 0 = 0) (n : ℕ) (w : p.nat_degree < n) : p.sum f = ∑ (a : ℕ) in range n, f a (coeff p a) := begin rcases p, have := supp_subset_range w, simp only [polynomial.sum, support, coeff, nat_degree, degree] at ⊢ this, exact finsupp.sum_of_support_subset _ this _ (λ n hn, h n) end /-- We can reexpress a sum over `p.support` as a sum over `range (p.nat_degree + 1)`. -/ lemma sum_over_range [add_comm_monoid S] (p : polynomial R) {f : ℕ → R → S} (h : ∀ n, f n 0 = 0) : p.sum f = ∑ (a : ℕ) in range (p.nat_degree + 1), f a (coeff p a) := sum_over_range' p h (p.nat_degree + 1) (lt_add_one _) lemma as_sum_range' (p : polynomial R) (n : ℕ) (w : p.nat_degree < n) : p = ∑ i in range n, monomial i (coeff p i) := p.sum_monomial_eq.symm.trans $ p.sum_over_range' monomial_zero_right _ w lemma as_sum_range (p : polynomial R) : p = ∑ i in range (p.nat_degree + 1), monomial i (coeff p i) := p.sum_monomial_eq.symm.trans $ p.sum_over_range $ monomial_zero_right lemma as_sum_range_C_mul_X_pow (p : polynomial R) : p = ∑ i in range (p.nat_degree + 1), C (coeff p i) * X ^ i := p.as_sum_range.trans $ by simp only [C_mul_X_pow_eq_monomial] lemma coeff_ne_zero_of_eq_degree (hn : degree p = n) : coeff p n ≠ 0 := λ h, mem_support_iff.mp (mem_of_max hn) h lemma eq_X_add_C_of_degree_le_one (h : degree p ≤ 1) : p = C (p.coeff 1) * X + C (p.coeff 0) := ext (λ n, nat.cases_on n (by simp) (λ n, nat.cases_on n (by simp [coeff_C]) (λ m, have degree p < m.succ.succ, from lt_of_le_of_lt h dec_trivial, by simp [coeff_eq_zero_of_degree_lt this, coeff_C, nat.succ_ne_zero, coeff_X, nat.succ_inj', @eq_comm ℕ 0]))) lemma eq_X_add_C_of_degree_eq_one (h : degree p = 1) : p = C (p.leading_coeff) * X + C (p.coeff 0) := (eq_X_add_C_of_degree_le_one (show degree p ≤ 1, from h ▸ le_refl _)).trans (by simp [leading_coeff, nat_degree_eq_of_degree_eq_some h]) lemma eq_X_add_C_of_nat_degree_le_one (h : nat_degree p ≤ 1) : p = C (p.coeff 1) * X + C (p.coeff 0) := eq_X_add_C_of_degree_le_one $ degree_le_of_nat_degree_le h lemma exists_eq_X_add_C_of_nat_degree_le_one (h : nat_degree p ≤ 1) : ∃ a b, p = C a * X + C b := ⟨p.coeff 1, p.coeff 0, eq_X_add_C_of_nat_degree_le_one h⟩ theorem degree_X_pow_le (n : ℕ) : degree (X^n : polynomial R) ≤ n := by simpa only [C_1, one_mul] using degree_C_mul_X_pow_le n (1:R) theorem degree_X_le : degree (X : polynomial R) ≤ 1 := degree_monomial_le _ _ lemma nat_degree_X_le : (X : polynomial R).nat_degree ≤ 1 := nat_degree_le_of_degree_le degree_X_le lemma support_C_mul_X_pow (c : R) (n : ℕ) : (C c * X ^ n).support ⊆ singleton n := begin rw [C_mul_X_pow_eq_monomial], exact support_monomial' _ _ end lemma mem_support_C_mul_X_pow {n a : ℕ} {c : R} (h : a ∈ (C c * X ^ n).support) : a = n := mem_singleton.1 $ support_C_mul_X_pow _ _ h lemma card_support_C_mul_X_pow_le_one {c : R} {n : ℕ} : (C c * X ^ n).support.card ≤ 1 := begin rw ← card_singleton n, apply card_le_of_subset (support_C_mul_X_pow c n), end lemma card_supp_le_succ_nat_degree (p : polynomial R) : p.support.card ≤ p.nat_degree + 1 := begin rw ← finset.card_range (p.nat_degree + 1), exact finset.card_le_of_subset supp_subset_range_nat_degree_succ, end lemma le_degree_of_mem_supp (a : ℕ) : a ∈ p.support → ↑a ≤ degree p := le_degree_of_ne_zero ∘ mem_support_iff.mp lemma nonempty_support_iff : p.support.nonempty ↔ p ≠ 0 := by rw [ne.def, nonempty_iff_ne_empty, ne.def, ← support_eq_empty] lemma support_C_mul_X_pow_nonzero {c : R} {n : ℕ} (h : c ≠ 0) : (C c * X ^ n).support = singleton n := begin rw [C_mul_X_pow_eq_monomial], exact support_monomial _ _ h end end semiring section nonzero_semiring variables [semiring R] [nontrivial R] {p q : polynomial R} @[simp] lemma degree_one : degree (1 : polynomial R) = (0 : with_bot ℕ) := degree_C (show (1 : R) ≠ 0, from zero_ne_one.symm) @[simp] lemma degree_X : degree (X : polynomial R) = 1 := degree_monomial _ one_ne_zero @[simp] lemma nat_degree_X : (X : polynomial R).nat_degree = 1 := nat_degree_eq_of_degree_eq_some degree_X end nonzero_semiring section ring variables [ring R] lemma coeff_mul_X_sub_C {p : polynomial R} {r : R} {a : ℕ} : coeff (p * (X - C r)) (a + 1) = coeff p a - coeff p (a + 1) * r := by simp [mul_sub] lemma C_eq_int_cast (n : ℤ) : C (n : R) = n := (C : R →+* _).map_int_cast n @[simp] lemma degree_neg (p : polynomial R) : degree (-p) = degree p := by unfold degree; rw support_neg @[simp] lemma nat_degree_neg (p : polynomial R) : nat_degree (-p) = nat_degree p := by simp [nat_degree] @[simp] lemma nat_degree_int_cast (n : ℤ) : nat_degree (n : polynomial R) = 0 := by simp only [←C_eq_int_cast, nat_degree_C] end ring section semiring variables [semiring R] /-- The second-highest coefficient, or 0 for constants -/ def next_coeff (p : polynomial R) : R := if p.nat_degree = 0 then 0 else p.coeff (p.nat_degree - 1) @[simp] lemma next_coeff_C_eq_zero (c : R) : next_coeff (C c) = 0 := by { rw next_coeff, simp } lemma next_coeff_of_pos_nat_degree (p : polynomial R) (hp : 0 < p.nat_degree) : next_coeff p = p.coeff (p.nat_degree - 1) := by { rw [next_coeff, if_neg], contrapose! hp, simpa } end semiring section semiring variables [semiring R] {p q : polynomial R} {ι : Type*} lemma coeff_nat_degree_eq_zero_of_degree_lt (h : degree p < degree q) : coeff p (nat_degree q) = 0 := coeff_eq_zero_of_degree_lt (lt_of_lt_of_le h degree_le_nat_degree) lemma ne_zero_of_degree_gt {n : with_bot ℕ} (h : n < degree p) : p ≠ 0 := mt degree_eq_bot.2 (ne.symm (ne_of_lt (lt_of_le_of_lt bot_le h))) lemma ne_zero_of_degree_ge_degree (hpq : p.degree ≤ q.degree) (hp : p ≠ 0) : q ≠ 0 := polynomial.ne_zero_of_degree_gt (lt_of_lt_of_le (bot_lt_iff_ne_bot.mpr (by rwa [ne.def, polynomial.degree_eq_bot])) hpq : q.degree > ⊥) lemma ne_zero_of_nat_degree_gt {n : ℕ} (h : n < nat_degree p) : p ≠ 0 := λ H, by simpa [H, nat.not_lt_zero] using h lemma degree_lt_degree (h : nat_degree p < nat_degree q) : degree p < degree q := begin by_cases hp : p = 0, { simp [hp], rw bot_lt_iff_ne_bot, intro hq, simpa [hp, degree_eq_bot.mp hq, lt_irrefl] using h }, { rw [degree_eq_nat_degree hp, degree_eq_nat_degree $ ne_zero_of_nat_degree_gt h], exact_mod_cast h } end lemma nat_degree_lt_nat_degree_iff (hp : p ≠ 0) : nat_degree p < nat_degree q ↔ degree p < degree q := ⟨degree_lt_degree, begin intro h, have hq : q ≠ 0 := ne_zero_of_degree_gt h, rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq] at h, exact_mod_cast h end⟩ lemma eq_C_of_degree_le_zero (h : degree p ≤ 0) : p = C (coeff p 0) := begin ext (_|n), { simp }, rw [coeff_C, if_neg (nat.succ_ne_zero _), coeff_eq_zero_of_degree_lt], exact h.trans_lt (with_bot.some_lt_some.2 n.succ_pos), end lemma eq_C_of_degree_eq_zero (h : degree p = 0) : p = C (coeff p 0) := eq_C_of_degree_le_zero (h ▸ le_refl _) lemma degree_le_zero_iff : degree p ≤ 0 ↔ p = C (coeff p 0) := ⟨eq_C_of_degree_le_zero, λ h, h.symm ▸ degree_C_le⟩ lemma degree_add_le (p q : polynomial R) : degree (p + q) ≤ max (degree p) (degree q) := calc degree (p + q) = ((p + q).support).sup some : rfl ... ≤ (p.support ∪ q.support).sup some : sup_mono support_add ... = p.support.sup some ⊔ q.support.sup some : sup_union lemma nat_degree_add_le (p q : polynomial R) : nat_degree (p + q) ≤ max (nat_degree p) (nat_degree q) := begin cases le_max_iff.1 (degree_add_le p q); simp [nat_degree_le_nat_degree h] end @[simp] lemma leading_coeff_zero : leading_coeff (0 : polynomial R) = 0 := rfl @[simp] lemma leading_coeff_eq_zero : leading_coeff p = 0 ↔ p = 0 := ⟨λ h, by_contradiction $ λ hp, mt mem_support_iff.1 (not_not.2 h) (mem_of_max (degree_eq_nat_degree hp)), λ h, h.symm ▸ leading_coeff_zero⟩ lemma leading_coeff_ne_zero : leading_coeff p ≠ 0 ↔ p ≠ 0 := by rw [ne.def, leading_coeff_eq_zero] lemma leading_coeff_eq_zero_iff_deg_eq_bot : leading_coeff p = 0 ↔ degree p = ⊥ := by rw [leading_coeff_eq_zero, degree_eq_bot] lemma nat_degree_mem_support_of_nonzero (H : p ≠ 0) : p.nat_degree ∈ p.support := by { rw mem_support_iff, exact (not_congr leading_coeff_eq_zero).mpr H } lemma nat_degree_eq_support_max' (h : p ≠ 0) : p.nat_degree = p.support.max' (nonempty_support_iff.mpr h) := (le_max' _ _ $ nat_degree_mem_support_of_nonzero h).antisymm $ max'_le _ _ _ le_nat_degree_of_mem_supp lemma nat_degree_C_mul_X_pow_le (a : R) (n : ℕ) : nat_degree (C a * X ^ n) ≤ n := nat_degree_le_iff_degree_le.2 $ degree_C_mul_X_pow_le _ _ lemma degree_add_eq_left_of_degree_lt (h : degree q < degree p) : degree (p + q) = degree p := le_antisymm (max_eq_left_of_lt h ▸ degree_add_le _ _) $ degree_le_degree $ begin rw [coeff_add, coeff_nat_degree_eq_zero_of_degree_lt h, add_zero], exact mt leading_coeff_eq_zero.1 (ne_zero_of_degree_gt h) end lemma degree_add_eq_right_of_degree_lt (h : degree p < degree q) : degree (p + q) = degree q := by rw [add_comm, degree_add_eq_left_of_degree_lt h] lemma degree_add_C (hp : 0 < degree p) : degree (p + C a) = degree p := add_comm (C a) p ▸ degree_add_eq_right_of_degree_lt $ lt_of_le_of_lt degree_C_le hp lemma degree_add_eq_of_leading_coeff_add_ne_zero (h : leading_coeff p + leading_coeff q ≠ 0) : degree (p + q) = max p.degree q.degree := le_antisymm (degree_add_le _ _) $ match lt_trichotomy (degree p) (degree q) with | or.inl hlt := by rw [degree_add_eq_right_of_degree_lt hlt, max_eq_right_of_lt hlt]; exact le_refl _ | or.inr (or.inl heq) := le_of_not_gt $ assume hlt : max (degree p) (degree q) > degree (p + q), h $ show leading_coeff p + leading_coeff q = 0, begin rw [heq, max_self] at hlt, rw [leading_coeff, leading_coeff, nat_degree_eq_of_degree_eq heq, ← coeff_add], exact coeff_nat_degree_eq_zero_of_degree_lt hlt end | or.inr (or.inr hlt) := by rw [degree_add_eq_left_of_degree_lt hlt, max_eq_left_of_lt hlt]; exact le_refl _ end lemma degree_erase_le (p : polynomial R) (n : ℕ) : degree (p.erase n) ≤ degree p := by { rcases p, simp only [erase, degree, coeff, support], convert sup_mono (erase_subset _ _) } lemma degree_erase_lt (hp : p ≠ 0) : degree (p.erase (nat_degree p)) < degree p := begin apply lt_of_le_of_ne (degree_erase_le _ _), rw [degree_eq_nat_degree hp, degree, support_erase], exact λ h, not_mem_erase _ _ (mem_of_max h), end lemma degree_update_le (p : polynomial R) (n : ℕ) (a : R) : degree (p.update n a) ≤ max (degree p) n := begin simp only [degree, coeff_update_apply, le_max_iff, finset.sup_le_iff, mem_support_iff], intros b hb, split_ifs at hb with h, { subst b, exact or.inr le_rfl }, { exact or.inl (le_degree_of_ne_zero hb) } end lemma degree_sum_le (s : finset ι) (f : ι → polynomial R) : degree (∑ i in s, f i) ≤ s.sup (λ b, degree (f b)) := finset.induction_on s (by simp only [sum_empty, sup_empty, degree_zero, le_refl]) $ assume a s has ih, calc degree (∑ i in insert a s, f i) ≤ max (degree (f a)) (degree (∑ i in s, f i)) : by rw sum_insert has; exact degree_add_le _ _ ... ≤ _ : by rw [sup_insert, sup_eq_max]; exact max_le_max le_rfl ih lemma degree_mul_le (p q : polynomial R) : degree (p * q) ≤ degree p + degree q := calc degree (p * q) ≤ (p.support).sup (λi, degree (sum q (λj a, C (coeff p i * a) * X ^ (i + j)))) : begin simp only [monomial_eq_C_mul_X.symm], convert degree_sum_le _ _, exact mul_eq_sum_sum end ... ≤ p.support.sup (λi, q.support.sup (λj, degree (C (coeff p i * coeff q j) * X ^ (i + j)))) : finset.sup_mono_fun (assume i hi, degree_sum_le _ _) ... ≤ degree p + degree q : begin refine finset.sup_le (λ a ha, finset.sup_le (λ b hb, le_trans (degree_C_mul_X_pow_le _ _) _)), rw [with_bot.coe_add], rw mem_support_iff at ha hb, exact add_le_add (le_degree_of_ne_zero ha) (le_degree_of_ne_zero hb) end lemma degree_pow_le (p : polynomial R) : ∀ (n : ℕ), degree (p ^ n) ≤ n • (degree p) | 0 := by rw [pow_zero, zero_nsmul]; exact degree_one_le | (n+1) := calc degree (p ^ (n + 1)) ≤ degree p + degree (p ^ n) : by rw pow_succ; exact degree_mul_le _ _ ... ≤ _ : by rw succ_nsmul; exact add_le_add (le_refl _) (degree_pow_le _) @[simp] lemma leading_coeff_monomial (a : R) (n : ℕ) : leading_coeff (monomial n a) = a := begin by_cases ha : a = 0, { simp only [ha, (monomial n).map_zero, leading_coeff_zero] }, { rw [leading_coeff, nat_degree_monomial _ _ ha, coeff_monomial], simp } end lemma leading_coeff_C_mul_X_pow (a : R) (n : ℕ) : leading_coeff (C a * X ^ n) = a := by rw [C_mul_X_pow_eq_monomial, leading_coeff_monomial] @[simp] lemma leading_coeff_C (a : R) : leading_coeff (C a) = a := leading_coeff_monomial a 0 @[simp] lemma leading_coeff_X_pow (n : ℕ) : leading_coeff ((X : polynomial R) ^ n) = 1 := by simpa only [C_1, one_mul] using leading_coeff_C_mul_X_pow (1 : R) n @[simp] lemma leading_coeff_X : leading_coeff (X : polynomial R) = 1 := by simpa only [pow_one] using @leading_coeff_X_pow R _ 1 @[simp] lemma monic_X_pow (n : ℕ) : monic (X ^ n : polynomial R) := leading_coeff_X_pow n @[simp] lemma monic_X : monic (X : polynomial R) := leading_coeff_X @[simp] lemma leading_coeff_one : leading_coeff (1 : polynomial R) = 1 := leading_coeff_C 1 @[simp] lemma monic_one : monic (1 : polynomial R) := leading_coeff_C _ lemma monic.ne_zero {R : Type*} [semiring R] [nontrivial R] {p : polynomial R} (hp : p.monic) : p ≠ 0 := by { rintro rfl, simpa [monic] using hp } lemma monic.ne_zero_of_ne (h : (0:R) ≠ 1) {p : polynomial R} (hp : p.monic) : p ≠ 0 := by { nontriviality R, exact hp.ne_zero } lemma monic.ne_zero_of_polynomial_ne {r} (hp : monic p) (hne : q ≠ r) : p ≠ 0 := by { haveI := nontrivial.of_polynomial_ne hne, exact hp.ne_zero } lemma leading_coeff_add_of_degree_lt (h : degree p < degree q) : leading_coeff (p + q) = leading_coeff q := have coeff p (nat_degree q) = 0, from coeff_nat_degree_eq_zero_of_degree_lt h, by simp only [leading_coeff, nat_degree_eq_of_degree_eq (degree_add_eq_right_of_degree_lt h), this, coeff_add, zero_add] lemma leading_coeff_add_of_degree_eq (h : degree p = degree q) (hlc : leading_coeff p + leading_coeff q ≠ 0) : leading_coeff (p + q) = leading_coeff p + leading_coeff q := have nat_degree (p + q) = nat_degree p, by apply nat_degree_eq_of_degree_eq; rw [degree_add_eq_of_leading_coeff_add_ne_zero hlc, h, max_self], by simp only [leading_coeff, this, nat_degree_eq_of_degree_eq h, coeff_add] @[simp] lemma coeff_mul_degree_add_degree (p q : polynomial R) : coeff (p * q) (nat_degree p + nat_degree q) = leading_coeff p * leading_coeff q := calc coeff (p * q) (nat_degree p + nat_degree q) = ∑ x in nat.antidiagonal (nat_degree p + nat_degree q), coeff p x.1 * coeff q x.2 : coeff_mul _ _ _ ... = coeff p (nat_degree p) * coeff q (nat_degree q) : begin refine finset.sum_eq_single (nat_degree p, nat_degree q) _ _, { rintro ⟨i,j⟩ h₁ h₂, rw nat.mem_antidiagonal at h₁, by_cases H : nat_degree p < i, { rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 H)), zero_mul] }, { rw not_lt_iff_eq_or_lt at H, cases H, { subst H, rw add_left_cancel_iff at h₁, dsimp at h₁, subst h₁, exfalso, exact h₂ rfl }, { suffices : nat_degree q < j, { rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 this)), mul_zero] }, { by_contra H', rw not_lt at H', exact ne_of_lt (nat.lt_of_lt_of_le (nat.add_lt_add_right H j) (nat.add_le_add_left H' _)) h₁ } } } }, { intro H, exfalso, apply H, rw nat.mem_antidiagonal } end lemma degree_mul' (h : leading_coeff p * leading_coeff q ≠ 0) : degree (p * q) = degree p + degree q := have hp : p ≠ 0 := by refine mt _ h; exact λ hp, by rw [hp, leading_coeff_zero, zero_mul], have hq : q ≠ 0 := by refine mt _ h; exact λ hq, by rw [hq, leading_coeff_zero, mul_zero], le_antisymm (degree_mul_le _ _) begin rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq], refine le_degree_of_ne_zero _, rwa coeff_mul_degree_add_degree end lemma monic.degree_mul (hq : monic q) : degree (p * q) = degree p + degree q := if hp : p = 0 then by simp [hp] else degree_mul' $ by rwa [hq.leading_coeff, mul_one, ne.def, leading_coeff_eq_zero] lemma nat_degree_mul' (h : leading_coeff p * leading_coeff q ≠ 0) : nat_degree (p * q) = nat_degree p + nat_degree q := have hp : p ≠ 0 := mt leading_coeff_eq_zero.2 (λ h₁, h $ by rw [h₁, zero_mul]), have hq : q ≠ 0 := mt leading_coeff_eq_zero.2 (λ h₁, h $ by rw [h₁, mul_zero]), nat_degree_eq_of_degree_eq_some $ by rw [degree_mul' h, with_bot.coe_add, degree_eq_nat_degree hp, degree_eq_nat_degree hq] lemma leading_coeff_mul' (h : leading_coeff p * leading_coeff q ≠ 0) : leading_coeff (p * q) = leading_coeff p * leading_coeff q := begin unfold leading_coeff, rw [nat_degree_mul' h, coeff_mul_degree_add_degree], refl end lemma monomial_nat_degree_leading_coeff_eq_self (h : p.support.card ≤ 1) : monomial p.nat_degree p.leading_coeff = p := begin rcases card_support_le_one_iff_monomial.1 h with ⟨n, a, rfl⟩, by_cases ha : a = 0; simp [ha] end lemma C_mul_X_pow_eq_self (h : p.support.card ≤ 1) : C p.leading_coeff * X^p.nat_degree = p := by rw [C_mul_X_pow_eq_monomial, monomial_nat_degree_leading_coeff_eq_self h] lemma leading_coeff_pow' : leading_coeff p ^ n ≠ 0 → leading_coeff (p ^ n) = leading_coeff p ^ n := nat.rec_on n (by simp) $ λ n ih h, have h₁ : leading_coeff p ^ n ≠ 0 := λ h₁, h $ by rw [pow_succ, h₁, mul_zero], have h₂ : leading_coeff p * leading_coeff (p ^ n) ≠ 0 := by rwa [pow_succ, ← ih h₁] at h, by rw [pow_succ, pow_succ, leading_coeff_mul' h₂, ih h₁] lemma degree_pow' : ∀ {n : ℕ}, leading_coeff p ^ n ≠ 0 → degree (p ^ n) = n • (degree p) | 0 := λ h, by rw [pow_zero, ← C_1] at *; rw [degree_C h, zero_nsmul] | (n+1) := λ h, have h₁ : leading_coeff p ^ n ≠ 0 := λ h₁, h $ by rw [pow_succ, h₁, mul_zero], have h₂ : leading_coeff p * leading_coeff (p ^ n) ≠ 0 := by rwa [pow_succ, ← leading_coeff_pow' h₁] at h, by rw [pow_succ, degree_mul' h₂, succ_nsmul, degree_pow' h₁] lemma nat_degree_pow' {n : ℕ} (h : leading_coeff p ^ n ≠ 0) : nat_degree (p ^ n) = n * nat_degree p := if hp0 : p = 0 then if hn0 : n = 0 then by simp * else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp else have hpn : p ^ n ≠ 0, from λ hpn0, have h1 : _ := h, by rw [← leading_coeff_pow' h1, hpn0, leading_coeff_zero] at h; exact h rfl, option.some_inj.1 $ show (nat_degree (p ^ n) : with_bot ℕ) = (n * nat_degree p : ℕ), by rw [← degree_eq_nat_degree hpn, degree_pow' h, degree_eq_nat_degree hp0, ← with_bot.coe_nsmul]; simp theorem leading_coeff_mul_monic {p q : polynomial R} (hq : monic q) : leading_coeff (p * q) = leading_coeff p := decidable.by_cases (λ H : leading_coeff p = 0, by rw [H, leading_coeff_eq_zero.1 H, zero_mul, leading_coeff_zero]) (λ H : leading_coeff p ≠ 0, by rw [leading_coeff_mul', hq.leading_coeff, mul_one]; rwa [hq.leading_coeff, mul_one]) @[simp] theorem leading_coeff_mul_X_pow {p : polynomial R} {n : ℕ} : leading_coeff (p * X ^ n) = leading_coeff p := leading_coeff_mul_monic (monic_X_pow n) @[simp] theorem leading_coeff_mul_X {p : polynomial R} : leading_coeff (p * X) = leading_coeff p := leading_coeff_mul_monic monic_X lemma nat_degree_mul_le {p q : polynomial R} : nat_degree (p * q) ≤ nat_degree p + nat_degree q := begin apply nat_degree_le_of_degree_le, apply le_trans (degree_mul_le p q), rw with_bot.coe_add, refine add_le_add _ _; apply degree_le_nat_degree, end lemma subsingleton_of_monic_zero (h : monic (0 : polynomial R)) : (∀ p q : polynomial R, p = q) ∧ (∀ a b : R, a = b) := by rw [monic.def, leading_coeff_zero] at h; exact ⟨λ p q, by rw [← mul_one p, ← mul_one q, ← C_1, ← h, C_0, mul_zero, mul_zero], λ a b, by rw [← mul_one a, ← mul_one b, ← h, mul_zero, mul_zero]⟩ lemma zero_le_degree_iff {p : polynomial R} : 0 ≤ degree p ↔ p ≠ 0 := by rw [ne.def, ← degree_eq_bot]; cases degree p; exact dec_trivial lemma degree_nonneg_iff_ne_zero : 0 ≤ degree p ↔ p ≠ 0 := ⟨λ h0p hp0, absurd h0p (by rw [hp0, degree_zero]; exact dec_trivial), λ hp0, le_of_not_gt (λ h, by simp [gt, degree_eq_bot, *] at *)⟩ lemma nat_degree_eq_zero_iff_degree_le_zero : p.nat_degree = 0 ↔ p.degree ≤ 0 := by rw [← nonpos_iff_eq_zero, nat_degree_le_iff_degree_le, with_bot.coe_zero] theorem degree_le_iff_coeff_zero (f : polynomial R) (n : with_bot ℕ) : degree f ≤ n ↔ ∀ m : ℕ, n < m → coeff f m = 0 := ⟨λ (H : finset.sup (f.support) some ≤ n) m (Hm : n < (m : with_bot ℕ)), decidable.of_not_not $ λ H4, have H1 : m ∉ f.support, from λ H2, not_lt_of_ge ((finset.sup_le_iff.1 H) m H2 : ((m : with_bot ℕ) ≤ n)) Hm, H1 $ mem_support_iff.2 H4, λ H, finset.sup_le $ λ b Hb, decidable.of_not_not $ λ Hn, mem_support_iff.1 Hb $ H b $ lt_of_not_ge Hn⟩ theorem degree_lt_iff_coeff_zero (f : polynomial R) (n : ℕ) : degree f < n ↔ ∀ m : ℕ, n ≤ m → coeff f m = 0 := begin refine ⟨λ hf m hm, coeff_eq_zero_of_degree_lt (lt_of_lt_of_le hf (with_bot.coe_le_coe.2 hm)), _⟩, simp only [degree, finset.sup_lt_iff (with_bot.bot_lt_coe n), mem_support_iff, with_bot.some_eq_coe, with_bot.coe_lt_coe, ← @not_le ℕ], exact λ h m, mt (h m), end lemma degree_smul_le (a : R) (p : polynomial R) : degree (a • p) ≤ degree p := begin apply (degree_le_iff_coeff_zero _ _).2 (λ m hm, _), rw degree_lt_iff_coeff_zero at hm, simp [hm m (le_refl _)], end lemma nat_degree_smul_le (a : R) (p : polynomial R) : nat_degree (a • p) ≤ nat_degree p := nat_degree_le_nat_degree (degree_smul_le a p) lemma degree_lt_degree_mul_X (hp : p ≠ 0) : p.degree < (p * X).degree := by haveI := nontrivial.of_polynomial_ne hp; exact have leading_coeff p * leading_coeff X ≠ 0, by simpa, by erw [degree_mul' this, degree_eq_nat_degree hp, degree_X, ← with_bot.coe_one, ← with_bot.coe_add, with_bot.coe_lt_coe]; exact nat.lt_succ_self _ lemma nat_degree_pos_iff_degree_pos : 0 < nat_degree p ↔ 0 < degree p := lt_iff_lt_of_le_iff_le nat_degree_le_iff_degree_le lemma eq_C_of_nat_degree_le_zero (h : nat_degree p ≤ 0) : p = C (coeff p 0) := eq_C_of_degree_le_zero $ degree_le_of_nat_degree_le h lemma eq_C_of_nat_degree_eq_zero (h : nat_degree p = 0) : p = C (coeff p 0) := eq_C_of_nat_degree_le_zero h.le lemma ne_zero_of_coe_le_degree (hdeg : ↑n ≤ p.degree) : p ≠ 0 := by rw ← degree_nonneg_iff_ne_zero; exact trans (by exact_mod_cast n.zero_le) hdeg lemma le_nat_degree_of_coe_le_degree (hdeg : ↑n ≤ p.degree) : n ≤ p.nat_degree := with_bot.coe_le_coe.mp ((degree_eq_nat_degree $ ne_zero_of_coe_le_degree hdeg) ▸ hdeg) end semiring section nontrivial_semiring variables [semiring R] [nontrivial R] {p q : polynomial R} @[simp] lemma degree_X_pow (n : ℕ) : degree ((X : polynomial R) ^ n) = n := by rw [X_pow_eq_monomial, degree_monomial _ (@one_ne_zero R _ _)] @[simp] lemma nat_degree_X_pow (n : ℕ) : nat_degree ((X : polynomial R) ^ n) = n := nat_degree_eq_of_degree_eq_some (degree_X_pow n) theorem not_is_unit_X : ¬ is_unit (X : polynomial R) := λ ⟨⟨_, g, hfg, hgf⟩, rfl⟩, @zero_ne_one R _ _ $ by { change g * monomial 1 1 = 1 at hgf, rw [← coeff_one_zero, ← hgf], simp } @[simp] lemma degree_mul_X : degree (p * X) = degree p + 1 := by simp [monic_X.degree_mul] @[simp] lemma degree_mul_X_pow : degree (p * X ^ n) = degree p + n := by simp [(monic_X_pow n).degree_mul] end nontrivial_semiring section ring variables [ring R] {p q : polynomial R} lemma degree_sub_le (p q : polynomial R) : degree (p - q) ≤ max (degree p) (degree q) := by simpa only [sub_eq_add_neg, degree_neg q] using degree_add_le p (-q) lemma degree_sub_lt (hd : degree p = degree q) (hp0 : p ≠ 0) (hlc : leading_coeff p = leading_coeff q) : degree (p - q) < degree p := have hp : monomial (nat_degree p) (leading_coeff p) + p.erase (nat_degree p) = p := monomial_add_erase _ _, have hq : monomial (nat_degree q) (leading_coeff q) + q.erase (nat_degree q) = q := monomial_add_erase _ _, have hd' : nat_degree p = nat_degree q := by unfold nat_degree; rw hd, have hq0 : q ≠ 0 := mt degree_eq_bot.2 (hd ▸ mt degree_eq_bot.1 hp0), calc degree (p - q) = degree (erase (nat_degree q) p + -erase (nat_degree q) q) : by conv { to_lhs, rw [← hp, ← hq, hlc, hd', add_sub_add_left_eq_sub, sub_eq_add_neg] } ... ≤ max (degree (erase (nat_degree q) p)) (degree (erase (nat_degree q) q)) : degree_neg (erase (nat_degree q) q) ▸ degree_add_le _ _ ... < degree p : max_lt_iff.2 ⟨hd' ▸ degree_erase_lt hp0, hd.symm ▸ degree_erase_lt hq0⟩ lemma nat_degree_X_sub_C_le {r : R} : (X - C r).nat_degree ≤ 1 := nat_degree_le_iff_degree_le.2 $ le_trans (degree_sub_le _ _) $ max_le degree_X_le $ le_trans degree_C_le $ with_bot.coe_le_coe.2 zero_le_one lemma degree_sum_fin_lt {n : ℕ} (f : fin n → R) : degree (∑ i : fin n, C (f i) * X ^ (i : ℕ)) < n := begin haveI : is_commutative (with_bot ℕ) max := ⟨max_comm⟩, haveI : is_associative (with_bot ℕ) max := ⟨max_assoc⟩, calc (∑ i, C (f i) * X ^ (i : ℕ)).degree ≤ finset.univ.fold (⊔) ⊥ (λ i, (C (f i) * X ^ (i : ℕ)).degree) : degree_sum_le _ _ ... = finset.univ.fold max ⊥ (λ i, (C (f i) * X ^ (i : ℕ)).degree) : rfl ... < n : (finset.fold_max_lt (n : with_bot ℕ)).mpr ⟨with_bot.bot_lt_coe _, _⟩, rintros ⟨i, hi⟩ -, calc (C (f ⟨i, hi⟩) * X ^ i).degree ≤ (C _).degree + (X ^ i).degree : degree_mul_le _ _ ... ≤ 0 + i : add_le_add degree_C_le (degree_X_pow_le i) ... = i : zero_add _ ... < n : with_bot.some_lt_some.mpr hi, end lemma degree_sub_eq_left_of_degree_lt (h : degree q < degree p) : degree (p - q) = degree p := by { rw ← degree_neg q at h, rw [sub_eq_add_neg, degree_add_eq_left_of_degree_lt h] } lemma degree_sub_eq_right_of_degree_lt (h : degree p < degree q) : degree (p - q) = degree q := by { rw ← degree_neg q at h, rw [sub_eq_add_neg, degree_add_eq_right_of_degree_lt h, degree_neg] } end ring section nonzero_ring variables [nontrivial R] [ring R] @[simp] lemma degree_X_sub_C (a : R) : degree (X - C a) = 1 := have degree (C a) < degree (X : polynomial R), from calc degree (C a) ≤ 0 : degree_C_le ... < 1 : with_bot.some_lt_some.mpr zero_lt_one ... = degree X : degree_X.symm, by rw [degree_sub_eq_left_of_degree_lt this, degree_X] @[simp] lemma degree_X_add_C (a : R) : degree (X + C a) = 1 := have degree (C a) < degree (X : polynomial R), from calc degree (C a) ≤ 0 : degree_C_le ... < 1 : with_bot.some_lt_some.mpr zero_lt_one ... = degree X : degree_X.symm, by rw [degree_add_eq_left_of_degree_lt this, degree_X] @[simp] lemma nat_degree_X_sub_C (x : R) : (X - C x).nat_degree = 1 := nat_degree_eq_of_degree_eq_some $ degree_X_sub_C x @[simp] lemma next_coeff_X_sub_C (c : R) : next_coeff (X - C c) = - c := by simp [next_coeff_of_pos_nat_degree] lemma degree_X_pow_sub_C {n : ℕ} (hn : 0 < n) (a : R) : degree ((X : polynomial R) ^ n - C a) = n := have degree (C a) < degree ((X : polynomial R) ^ n), from calc degree (C a) ≤ 0 : degree_C_le ... < degree ((X : polynomial R) ^ n) : by rwa [degree_X_pow]; exact with_bot.coe_lt_coe.2 hn, by rw [degree_sub_eq_left_of_degree_lt this, degree_X_pow] lemma X_pow_sub_C_ne_zero {n : ℕ} (hn : 0 < n) (a : R) : (X : polynomial R) ^ n - C a ≠ 0 := mt degree_eq_bot.2 (show degree ((X : polynomial R) ^ n - C a) ≠ ⊥, by rw degree_X_pow_sub_C hn a; exact dec_trivial) theorem X_sub_C_ne_zero (r : R) : X - C r ≠ 0 := pow_one (X : polynomial R) ▸ X_pow_sub_C_ne_zero zero_lt_one r theorem zero_nmem_multiset_map_X_sub_C {α : Type*} (m : multiset α) (f : α → R) : (0 : polynomial R) ∉ m.map (λ a, X - C (f a)) := λ mem, let ⟨a, _, ha⟩ := multiset.mem_map.mp mem in X_sub_C_ne_zero _ ha lemma nat_degree_X_pow_sub_C {n : ℕ} {r : R} : (X ^ n - C r).nat_degree = n := begin by_cases hn : n = 0, { rw [hn, pow_zero, ←C_1, ←ring_hom.map_sub, nat_degree_C] }, { exact nat_degree_eq_of_degree_eq_some (degree_X_pow_sub_C (pos_iff_ne_zero.mpr hn) r) }, end @[simp] lemma leading_coeff_X_pow_sub_C {n : ℕ} (hn : 0 < n) {r : R} : (X ^ n - C r).leading_coeff = 1 := by rw [leading_coeff, nat_degree_X_pow_sub_C, coeff_sub, coeff_X_pow_self, coeff_C, if_neg (pos_iff_ne_zero.mp hn), sub_zero] @[simp] lemma leading_coeff_X_pow_sub_one {n : ℕ} (hn : 0 < n) : (X ^ n - 1 : polynomial R).leading_coeff = 1 := leading_coeff_X_pow_sub_C hn end nonzero_ring section no_zero_divisors variables [semiring R] [no_zero_divisors R] {p q : polynomial R} @[simp] lemma degree_mul : degree (p * q) = degree p + degree q := if hp0 : p = 0 then by simp only [hp0, degree_zero, zero_mul, with_bot.bot_add] else if hq0 : q = 0 then by simp only [hq0, degree_zero, mul_zero, with_bot.add_bot] else degree_mul' $ mul_ne_zero (mt leading_coeff_eq_zero.1 hp0) (mt leading_coeff_eq_zero.1 hq0) @[simp] lemma degree_pow [nontrivial R] (p : polynomial R) (n : ℕ) : degree (p ^ n) = n • (degree p) := by induction n; [simp only [pow_zero, degree_one, zero_nsmul], simp only [*, pow_succ, succ_nsmul, degree_mul]] @[simp] lemma leading_coeff_mul (p q : polynomial R) : leading_coeff (p * q) = leading_coeff p * leading_coeff q := begin by_cases hp : p = 0, { simp only [hp, zero_mul, leading_coeff_zero] }, { by_cases hq : q = 0, { simp only [hq, mul_zero, leading_coeff_zero] }, { rw [leading_coeff_mul'], exact mul_ne_zero (mt leading_coeff_eq_zero.1 hp) (mt leading_coeff_eq_zero.1 hq) } } end @[simp] lemma leading_coeff_X_add_C [nontrivial R] (a b : R) (ha : a ≠ 0): leading_coeff (C a * X + C b) = a := begin rw [add_comm, leading_coeff_add_of_degree_lt], { simp }, { simpa [degree_C ha] using lt_of_le_of_lt degree_C_le (with_bot.coe_lt_coe.2 zero_lt_one)} end /-- `polynomial.leading_coeff` bundled as a `monoid_hom` when `R` has `no_zero_divisors`, and thus `leading_coeff` is multiplicative -/ def leading_coeff_hom : polynomial R →* R := { to_fun := leading_coeff, map_one' := by simp, map_mul' := leading_coeff_mul } @[simp] lemma leading_coeff_hom_apply (p : polynomial R) : leading_coeff_hom p = leading_coeff p := rfl @[simp] lemma leading_coeff_pow (p : polynomial R) (n : ℕ) : leading_coeff (p ^ n) = leading_coeff p ^ n := (leading_coeff_hom : polynomial R →* R).map_pow p n end no_zero_divisors end polynomial
88a27df7ad33a07b5fba1cf9de1a35dd426116f7
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Lean/Server/FileWorker/Utils.lean
c72c123586c8ecede301176644e8bed5cdd378d5
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
1,557
lean
/- Copyright (c) 2020 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki, Marc Huisinga -/ import Lean.Server.Utils import Lean.Server.Snapshots import Lean.Server.AsyncList namespace Lean.Server.FileWorker open Snapshots open IO def logSnapContent (s : Snapshot) (text : FileMap) : IO Unit := IO.eprintln s!"[{s.beginPos}, {s.endPos}]: ```\n{text.source.extract s.beginPos (s.endPos-1)}\n```" inductive ElabTaskError where | aborted | eof | ioError (e : IO.Error) instance : Coe IO.Error ElabTaskError := ⟨ElabTaskError.ioError⟩ structure CancelToken where ref : IO.Ref Bool deriving Inhabited namespace CancelToken def new : IO CancelToken := CancelToken.mk <$> IO.mkRef false def check [MonadExceptOf ElabTaskError m] [MonadLiftT (ST RealWorld) m] [Monad m] (tk : CancelToken) : m Unit := do let c ← tk.ref.get if c then throw ElabTaskError.aborted def set (tk : CancelToken) : IO Unit := tk.ref.set true end CancelToken /-- A document editable in the sense that we track the environment and parser state after each command so that edits can be applied without recompiling code appearing earlier in the file. -/ structure EditableDocument where meta : DocumentMeta /- The first snapshot is that after the header. -/ headerSnap : Snapshot /- Subsequent snapshots occur after each command. -/ cmdSnaps : AsyncList ElabTaskError Snapshot cancelTk : CancelToken deriving Inhabited end Lean.Server.FileWorker
757c2850c9feb38b3139e4eda64c07c8c3eab025
646fc4b41ca4adb82b3f7fbae3ea3c58ff496b4c
/test/out/ex/ex0.lean
60b493cead07cbae6e3291e1fc5b1439676522d2
[]
no_license
cpehle/lean4-plugin-example
82e63420463483381c91de0705b5626a336863fa
d32d3b310645cfecf4c33acafe996755f67cf30b
refs/heads/main
1,690,367,192,612
1,631,531,751,000
1,631,531,751,000
405,148,785
2
1
null
null
null
null
UTF-8
Lean
false
false
36
lean
import Example #eval Example.funA 2
f6a7897e30fcf1b9d5bb3d20c2493c1916158d9c
b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77
/src/ring_theory/power_basis.lean
36a0e0fdbd2c55df9e33d4ee7c97714ecc33bbed
[ "Apache-2.0" ]
permissive
molodiuc/mathlib
cae2ba3ef1601c1f42ca0b625c79b061b63fef5b
98ebe5a6739fbe254f9ee9d401882d4388f91035
refs/heads/master
1,674,237,127,059
1,606,353,533,000
1,606,353,533,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
23,472
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import field_theory.adjoin import field_theory.minimal_polynomial import ring_theory.adjoin import ring_theory.adjoin_root import ring_theory.algebraic /-! # Power basis This file defines a structure `power_basis R S`, giving a basis of the `R`-algebra `S` as a finite list of powers `1, x, ..., x^n`. There are also constructors for `power_basis` when adjoining an algebraic element to a ring/field. ## Definitions * `power_basis R A`: a structure containing an `x` and an `n` such that `1, x, ..., x^n` is a basis for the `R`-algebra `A` (viewed as an `R`-module). * `findim (hf : f ≠ 0) : finite_dimensional.findim K (adjoin_root f) = f.nat_degree`, the dimension of `adjoin_root f` equals the degree of `f` * `power_basis.lift (pb : power_basis R S)`: if `y : S'` satisfies the same equations as `pb.gen`, this is the map `S →ₐ[R] S'` sending `pb.gen` to `y` * `power_basis.equiv`: if two power bases satisfy the same equations, they are equivalent as algebras ## Implementation notes Throughout this file, `R`, `S`, ... are `comm_ring`s, `A`, `B`, ... are `integral_domain`s and `K`, `L`, ... are `field`s. `S` is an `R`-algebra, `B` is an `A`-algebra, `L` is a `K`-algebra. ## Tags power basis, powerbasis -/ open polynomial variables {R S T : Type*} [comm_ring R] [comm_ring S] [comm_ring T] variables [algebra R S] [algebra S T] [algebra R T] [is_scalar_tower R S T] variables {A B : Type*} [integral_domain A] [integral_domain B] [algebra A B] variables {K L : Type*} [field K] [field L] [algebra K L] /-- `pb : power_basis R S` states that `1, pb.gen, ..., pb.gen ^ (pb.dim - 1)` is a basis for the `R`-algebra `S` (viewed as `R`-module). This is a structure, not a class, since the same algebra can have many power bases. For the common case where `S` is defined by adjoining an integral element to `R`, the canonical power basis is given by `{algebra,intermediate_field}.adjoin.power_basis`. -/ @[nolint has_inhabited_instance] structure power_basis (R S : Type*) [comm_ring R] [ring S] [algebra R S] := (gen : S) (dim : ℕ) (is_basis : is_basis R (λ (i : fin dim), gen ^ (i : ℕ))) namespace power_basis /-- Cannot be an instance because `power_basis` cannot be a class. -/ lemma finite_dimensional [algebra K S] (pb : power_basis K S) : finite_dimensional K S := finite_dimensional.of_fintype_basis pb.is_basis lemma findim [algebra K S] (pb : power_basis K S) : finite_dimensional.findim K S = pb.dim := by rw [finite_dimensional.findim_eq_card_basis pb.is_basis, fintype.card_fin] /-- TODO: this mixes `polynomial` and `finsupp`, we should hide this behind a new function `polynomial.of_finsupp`. -/ lemma polynomial.mem_supported_range {f : polynomial R} {d : ℕ} : (f : finsupp ℕ R) ∈ finsupp.supported R R (↑(finset.range d) : set ℕ) ↔ f.degree < d := by { simp_rw [finsupp.mem_supported', finset.mem_coe, finset.mem_range, not_lt, degree_lt_iff_coeff_zero], refl } lemma mem_span_pow' {x y : S} {d : ℕ} : y ∈ submodule.span R (set.range (λ (i : fin d), x ^ (i : ℕ))) ↔ ∃ f : polynomial R, f.degree < d ∧ y = aeval x f := begin have : set.range (λ (i : fin d), x ^ (i : ℕ)) = (λ (i : ℕ), x ^ i) '' ↑(finset.range d), { ext n, simp_rw [set.mem_range, set.mem_image, finset.mem_coe, finset.mem_range], exact ⟨λ ⟨⟨i, hi⟩, hy⟩, ⟨i, hi, hy⟩, λ ⟨i, hi, hy⟩, ⟨⟨i, hi⟩, hy⟩⟩ }, rw [this, finsupp.mem_span_iff_total], -- In the next line we use that `polynomial R := finsupp ℕ R`. -- It would be nice to have a function `polynomial.of_finsupp`. apply exists_congr, rintro (f : polynomial R), simp only [exists_prop, polynomial.mem_supported_range, eq_comm], apply and_congr iff.rfl, split; { rintro rfl; rw [finsupp.total_apply, aeval_def, eval₂_eq_sum, eq_comm], apply finset.sum_congr rfl, rintro i -, simp only [algebra.smul_def] } end lemma mem_span_pow {x y : S} {d : ℕ} (hd : d ≠ 0) : y ∈ submodule.span R (set.range (λ (i : fin d), x ^ (i : ℕ))) ↔ ∃ f : polynomial R, f.nat_degree < d ∧ y = aeval x f := begin rw mem_span_pow', split; { rintros ⟨f, h, hy⟩, refine ⟨f, _, hy⟩, by_cases hf : f = 0, { simp only [hf, nat_degree_zero, degree_zero] at h ⊢, exact lt_of_le_of_ne (nat.zero_le d) hd.symm <|> exact with_bot.bot_lt_some d }, simpa only [degree_eq_nat_degree hf, with_bot.coe_lt_coe] using h }, end lemma dim_ne_zero [nontrivial S] (pb : power_basis R S) : pb.dim ≠ 0 := λ h, one_ne_zero $ show (1 : S) = 0, by { rw [← pb.is_basis.total_repr 1, finsupp.total_apply, finsupp.sum_fintype], { refine finset.sum_eq_zero (λ x hx, _), cases x with x x_lt, rw h at x_lt, cases x_lt }, { simp } } lemma exists_eq_aeval [nontrivial S] (pb : power_basis R S) (y : S) : ∃ f : polynomial R, f.nat_degree < pb.dim ∧ y = aeval pb.gen f := (mem_span_pow pb.dim_ne_zero).mp (pb.is_basis.mem_span y) section minpoly open_locale big_operators variable [algebra A S] /-- `pb.minpoly_gen` is a minimal polynomial for `pb.gen`. If `A` is not a field, it might not necessarily be *the* minimal polynomial, however `nat_degree_minimal_polynomial` shows its degree is indeed minimal. -/ noncomputable def minpoly_gen (pb : power_basis A S) : polynomial A := X ^ pb.dim - ∑ (i : fin pb.dim), C (pb.is_basis.repr (pb.gen ^ pb.dim) i) * X ^ (i : ℕ) @[simp] lemma nat_degree_minpoly_gen (pb : power_basis A S) : nat_degree (minpoly_gen pb) = pb.dim := begin unfold minpoly_gen, apply nat_degree_eq_of_degree_eq_some, rw degree_sub_eq_left_of_degree_lt; rw degree_X_pow, apply degree_sum_fin_lt end lemma minpoly_gen_monic (pb : power_basis A S) : monic (minpoly_gen pb) := begin apply monic_add_of_left (monic_pow (monic_X) _), rw [degree_neg, degree_X_pow], exact degree_sum_fin_lt _ end @[simp] lemma aeval_minpoly_gen (pb : power_basis A S) : aeval pb.gen (minpoly_gen pb) = 0 := begin simp_rw [minpoly_gen, alg_hom.map_sub, alg_hom.map_sum, alg_hom.map_mul, alg_hom.map_pow, aeval_C, ← algebra.smul_def, aeval_X], refine sub_eq_zero.mpr ((pb.is_basis.total_repr (pb.gen ^ pb.dim)).symm.trans _), rw [finsupp.total_apply, finsupp.sum_fintype], intro i, rw zero_smul end lemma is_integral_gen (pb : power_basis A S) : is_integral A pb.gen := ⟨minpoly_gen pb, minpoly_gen_monic pb, aeval_minpoly_gen pb⟩ lemma dim_le_nat_degree_of_root (h : power_basis A S) {p : polynomial A} (ne_zero : p ≠ 0) (root : aeval h.gen p = 0) : h.dim ≤ p.nat_degree := begin refine le_of_not_lt (λ hlt, ne_zero _), let p_coeff : fin (h.dim) → A := λ i, p.coeff i, suffices : ∀ i, p_coeff i = 0, { ext i, by_cases hi : i < h.dim, { exact this ⟨i, hi⟩ }, exact coeff_eq_zero_of_nat_degree_lt (lt_of_lt_of_le hlt (le_of_not_gt hi)) }, intro i, refine linear_independent_iff'.mp h.is_basis.1 finset.univ _ _ i (finset.mem_univ _), rw aeval_eq_sum_range' hlt at root, rw finset.sum_fin_eq_sum_range, convert root, ext i, split_ifs with hi, { refl }, { rw [coeff_eq_zero_of_nat_degree_lt (lt_of_lt_of_le hlt (le_of_not_gt hi)), zero_smul] } end @[simp] lemma nat_degree_minimal_polynomial (pb : power_basis A S) : (minimal_polynomial pb.is_integral_gen).nat_degree = pb.dim := begin refine le_antisymm _ (dim_le_nat_degree_of_root pb (minimal_polynomial.ne_zero _) (minimal_polynomial.aeval _)), rw ← nat_degree_minpoly_gen, apply nat_degree_le_of_degree_le, rw ← degree_eq_nat_degree (minpoly_gen_monic pb).ne_zero, exact minimal_polynomial.min _ (minpoly_gen_monic pb) (aeval_minpoly_gen pb) end end minpoly section equiv variables [algebra A S] {S' : Type*} [comm_ring S'] [algebra A S'] lemma nat_degree_lt_nat_degree {p q : polynomial R} (hp : p ≠ 0) (hpq : p.degree < q.degree) : p.nat_degree < q.nat_degree := begin by_cases hq : q = 0, { rw [hq, degree_zero] at hpq, have := not_lt_bot hpq, contradiction }, rwa [degree_eq_nat_degree hp, degree_eq_nat_degree hq, with_bot.coe_lt_coe] at hpq end lemma constr_pow_aeval (pb : power_basis A S) {y : S'} (hy : aeval y (minimal_polynomial pb.is_integral_gen) = 0) (f : polynomial A) : pb.is_basis.constr (λ i, y ^ (i : ℕ)) (aeval pb.gen f) = aeval y f := begin rw [← aeval_mod_by_monic_eq_self_of_root (minimal_polynomial.monic pb.is_integral_gen) (minimal_polynomial.aeval _), ← @aeval_mod_by_monic_eq_self_of_root _ _ _ _ _ f _ (minimal_polynomial.monic pb.is_integral_gen) y hy], by_cases hf : f %ₘ minimal_polynomial (pb.is_integral_gen) = 0, { simp only [hf, alg_hom.map_zero, linear_map.map_zero] }, have : (f %ₘ minimal_polynomial _).nat_degree < pb.dim, { rw ← pb.nat_degree_minimal_polynomial, apply nat_degree_lt_nat_degree hf, exact degree_mod_by_monic_lt _ (minimal_polynomial.monic _) (minimal_polynomial.ne_zero _) }, rw [aeval_eq_sum_range' this, aeval_eq_sum_range' this, linear_map.map_sum], refine finset.sum_congr rfl (λ i (hi : i ∈ finset.range pb.dim), _), rw finset.mem_range at hi, rw linear_map.map_smul, congr, exact @constr_basis _ _ _ _ _ _ _ _ _ _ _ (⟨i, hi⟩ : fin pb.dim) pb.is_basis, end lemma constr_pow_gen (pb : power_basis A S) {y : S'} (hy : aeval y (minimal_polynomial pb.is_integral_gen) = 0) : pb.is_basis.constr (λ i, y ^ (i : ℕ)) pb.gen = y := by { convert pb.constr_pow_aeval hy X; rw aeval_X } lemma constr_pow_algebra_map (pb : power_basis A S) {y : S'} (hy : aeval y (minimal_polynomial pb.is_integral_gen) = 0) (x : A) : pb.is_basis.constr (λ i, y ^ (i : ℕ)) (algebra_map A S x) = algebra_map A S' x := by { convert pb.constr_pow_aeval hy (C x); rw aeval_C } lemma constr_pow_mul [nontrivial S] (pb : power_basis A S) {y : S'} (hy : aeval y (minimal_polynomial pb.is_integral_gen) = 0) (x x' : S) : pb.is_basis.constr (λ i, y ^ (i : ℕ)) (x * x') = pb.is_basis.constr (λ i, y ^ (i : ℕ)) x * pb.is_basis.constr (λ i, y ^ (i : ℕ)) x' := begin obtain ⟨f, hf, rfl⟩ := pb.exists_eq_aeval x, obtain ⟨g, hg, rfl⟩ := pb.exists_eq_aeval x', simp only [← aeval_mul, pb.constr_pow_aeval hy] end /-- `pb.lift y hy` is the algebra map sending `pb.gen` to `y`, where `hy` states the higher powers of `y` are the same as the higher powers of `pb.gen`. -/ noncomputable def lift [nontrivial S] (pb : power_basis A S) (y : S') (hy : aeval y (minimal_polynomial pb.is_integral_gen) = 0) : S →ₐ[A] S' := { map_one' := by { convert pb.constr_pow_algebra_map hy 1 using 2; rw ring_hom.map_one }, map_zero' := by { convert pb.constr_pow_algebra_map hy 0 using 2; rw ring_hom.map_zero }, map_mul' := pb.constr_pow_mul hy, commutes' := pb.constr_pow_algebra_map hy, .. pb.is_basis.constr (λ i, y ^ (i : ℕ)) } @[simp] lemma lift_gen [nontrivial S] (pb : power_basis A S) (y : S') (hy : aeval y (minimal_polynomial pb.is_integral_gen) = 0) : pb.lift y hy pb.gen = y := pb.constr_pow_gen hy @[simp] lemma lift_aeval [nontrivial S] (pb : power_basis A S) (y : S') (hy : aeval y (minimal_polynomial pb.is_integral_gen) = 0) (f : polynomial A) : pb.lift y hy (aeval pb.gen f) = aeval y f := pb.constr_pow_aeval hy f /-- `pb.equiv pb' h` is an equivalence of algebras with the same power basis. -/ noncomputable def equiv [nontrivial S] [nontrivial S'] (pb : power_basis A S) (pb' : power_basis A S') (h : minimal_polynomial pb.is_integral_gen = minimal_polynomial pb'.is_integral_gen) : S ≃ₐ[A] S' := alg_equiv.of_alg_hom (pb.lift pb'.gen (h.symm ▸ minimal_polynomial.aeval pb'.is_integral_gen)) (pb'.lift pb.gen (h ▸ minimal_polynomial.aeval pb.is_integral_gen)) (by { ext x, obtain ⟨f, hf, rfl⟩ := pb'.exists_eq_aeval x, simp }) (by { ext x, obtain ⟨f, hf, rfl⟩ := pb.exists_eq_aeval x, simp }) @[simp] lemma equiv_aeval [nontrivial S] [nontrivial S'] (pb : power_basis A S) (pb' : power_basis A S') (h : minimal_polynomial pb.is_integral_gen = minimal_polynomial pb'.is_integral_gen) (f : polynomial A) : pb.equiv pb' h (aeval pb.gen f) = aeval pb'.gen f := pb.lift_aeval _ (h.symm ▸ minimal_polynomial.aeval _) _ @[simp] lemma equiv_gen [nontrivial S] [nontrivial S'] (pb : power_basis A S) (pb' : power_basis A S') (h : minimal_polynomial pb.is_integral_gen = minimal_polynomial pb'.is_integral_gen) : pb.equiv pb' h pb.gen = pb'.gen := pb.lift_gen _ (h.symm ▸ minimal_polynomial.aeval _) local attribute [irreducible] power_basis.lift @[simp] lemma equiv_symm [nontrivial S] [nontrivial S'] (pb : power_basis A S) (pb' : power_basis A S') (h : minimal_polynomial pb.is_integral_gen = minimal_polynomial pb'.is_integral_gen) : (pb.equiv pb' h).symm = pb'.equiv pb h.symm := rfl end equiv end power_basis lemma is_integral_algebra_map_iff {x : S} (hST : function.injective (algebra_map S T)) : is_integral R (algebra_map S T x) ↔ is_integral R x := begin split; rintros ⟨f, hf, hx⟩; use [f, hf], { exact is_scalar_tower.aeval_eq_zero_of_aeval_algebra_map_eq_zero R S T hST hx }, { rw [is_scalar_tower.algebra_map_eq R S T, ← hom_eval₂, hx, ring_hom.map_zero] } end /-- If `y` is the image of `x` in an extension, their minimal polynomials coincide. We take `h : y = algebra_map L T x` as an argument because `rw h` typically fails since `is_integral R y` depends on y. -/ lemma minimal_polynomial.eq_of_algebra_map_eq [algebra K S] [algebra K T] [is_scalar_tower K S T] (hST : function.injective (algebra_map S T)) {x : S} {y : T} (hx : is_integral K x) (hy : is_integral K y) (h : y = algebra_map S T x) : minimal_polynomial hx = minimal_polynomial hy := minimal_polynomial.unique hy (minimal_polynomial.monic hx) (by rw [h, ← is_scalar_tower.algebra_map_aeval, minimal_polynomial.aeval hx, ring_hom.map_zero]) (λ q q_monic root_q, minimal_polynomial.min _ q_monic (is_scalar_tower.aeval_eq_zero_of_aeval_algebra_map_eq_zero K S T hST (h ▸ root_q : aeval (algebra_map S T x) q = 0))) namespace algebra open power_basis lemma mem_span_power_basis [nontrivial R] {x y : S} (hx : _root_.is_integral R x) (hy : ∃ f : polynomial R, y = aeval x f) : y ∈ submodule.span R (set.range (λ (i : fin (minimal_polynomial hx).nat_degree), x ^ (i : ℕ))) := begin obtain ⟨f, rfl⟩ := hy, rw mem_span_pow', have := minimal_polynomial.monic hx, refine ⟨f.mod_by_monic (minimal_polynomial hx), lt_of_lt_of_le (degree_mod_by_monic_lt _ this (ne_zero_of_monic this)) degree_le_nat_degree, _⟩, conv_lhs { rw ← mod_by_monic_add_div f this }, simp only [add_zero, zero_mul, minimal_polynomial.aeval, aeval_add, alg_hom.map_mul] end lemma linear_independent_power_basis [algebra K S] {x : S} (hx : _root_.is_integral K x) : linear_independent K (λ (i : fin (minimal_polynomial hx).nat_degree), x ^ (i : ℕ)) := begin rw linear_independent_iff, intros p hp, let f : polynomial K := p.sum (λ i, monomial i), have f_def : ∀ (i : fin _), f.coeff i = p i, { intro i, -- TODO: how can we avoid unfolding here? change (p.sum (λ i pi, finsupp.single i pi) : ℕ →₀ K) i = p i, simp_rw [finsupp.sum_apply, finsupp.single_apply, finsupp.sum], rw [finset.sum_eq_single, if_pos rfl], { intros b _ hb, rw if_neg (mt (λ h, _) hb), exact fin.coe_injective h }, { intro hi, split_ifs; { exact finsupp.not_mem_support_iff.mp hi } } }, have f_def' : ∀ i, f.coeff i = if hi : i < _ then p ⟨i, hi⟩ else 0, { intro i, split_ifs with hi, { exact f_def ⟨i, hi⟩ }, -- TODO: how can we avoid unfolding here? change (p.sum (λ i pi, finsupp.single i pi) : ℕ →₀ K) i = 0, simp_rw [finsupp.sum_apply, finsupp.single_apply, finsupp.sum], apply finset.sum_eq_zero, rintro ⟨j, hj⟩ -, apply if_neg (mt _ hi), rintro rfl, exact hj }, suffices : f = 0, { ext i, rw [← f_def, this, coeff_zero, finsupp.zero_apply] }, contrapose hp with hf, intro h, have : (minimal_polynomial hx).degree ≤ f.degree, { apply minimal_polynomial.degree_le_of_ne_zero hx hf, convert h, rw [finsupp.total_apply, aeval_def, eval₂_eq_sum, finsupp.sum_sum_index], { apply finset.sum_congr rfl, rintro i -, simp only [algebra.smul_def, monomial, finsupp.lsingle_apply, zero_mul, ring_hom.map_zero, finsupp.sum_single_index] }, { intro, simp only [ring_hom.map_zero, zero_mul] }, { intros, simp only [ring_hom.map_add, add_mul] } }, have : ¬ (minimal_polynomial hx).degree ≤ f.degree, { apply not_le_of_lt, rw [degree_eq_nat_degree (minimal_polynomial.ne_zero hx), degree_lt_iff_coeff_zero], intros i hi, rw [f_def' i, dif_neg], exact not_lt_of_ge hi }, contradiction end lemma power_basis_is_basis [algebra K S] {x : S} (hx : _root_.is_integral K x) : is_basis K (λ (i : fin (minimal_polynomial hx).nat_degree), (⟨x, subset_adjoin (set.mem_singleton x)⟩ ^ (i : ℕ) : adjoin K ({x} : set S))) := begin have hST : function.injective (algebra_map (adjoin K ({x} : set S)) S) := subtype.coe_injective, have hx' : _root_.is_integral K (show adjoin K ({x} : set S), from ⟨x, subset_adjoin (set.mem_singleton x)⟩), { apply (is_integral_algebra_map_iff hST).mp, convert hx, apply_instance }, have minpoly_eq := minimal_polynomial.eq_of_algebra_map_eq hST hx' hx, refine ⟨_, _root_.eq_top_iff.mpr _⟩, { have := linear_independent_power_basis hx', rwa minpoly_eq at this, refl }, { rintros ⟨y, hy⟩ _, have := mem_span_power_basis hx', rw minpoly_eq at this, apply this, { rw [adjoin_singleton_eq_range] at hy, obtain ⟨f, rfl⟩ := (aeval x).mem_range.mp hy, use f, ext, exact (is_scalar_tower.algebra_map_aeval K (adjoin K {x}) S ⟨x, _⟩ _).symm }, { refl } } end /-- The power basis `1, x, ..., x ^ (d - 1)` for `K[x]`, where `d` is the degree of the minimal polynomial of `x`. -/ noncomputable def adjoin.power_basis [algebra K S] {x : S} (hx : _root_.is_integral K x) : power_basis K (adjoin K ({x} : set S)) := { gen := ⟨x, subset_adjoin (set.mem_singleton x)⟩, dim := (minimal_polynomial hx).nat_degree, is_basis := power_basis_is_basis hx } end algebra namespace adjoin_root variables {f : polynomial K} lemma power_basis_is_basis (hf : f ≠ 0) : is_basis K (λ (i : fin f.nat_degree), (root f ^ i.val)) := begin set f' := f * C (f.leading_coeff⁻¹) with f'_def, have deg_f' : f'.nat_degree = f.nat_degree, { rw [nat_degree_mul hf, nat_degree_C, add_zero], { rwa [ne.def, C_eq_zero, inv_eq_zero, leading_coeff_eq_zero] } }, have f'_monic : monic f' := monic_mul_leading_coeff_inv hf, have aeval_f' : aeval (root f) f' = 0, { rw [f'_def, alg_hom.map_mul, aeval_eq, mk_self, zero_mul] }, have hx : is_integral K (root f) := ⟨f', f'_monic, aeval_f'⟩, have minpoly_eq : f' = minimal_polynomial hx, { apply minimal_polynomial.unique hx f'_monic aeval_f', intros q q_monic q_aeval, have commutes : (lift (algebra_map K (adjoin_root f)) (root f) q_aeval).comp (mk q) = mk f, { ext, { simp only [ring_hom.comp_apply, mk_C, lift_of], refl }, { simp only [ring_hom.comp_apply, mk_X, lift_root] } }, rw [degree_eq_nat_degree f'_monic.ne_zero, degree_eq_nat_degree q_monic.ne_zero, with_bot.coe_le_coe, deg_f'], apply nat_degree_le_of_dvd, { rw [←ideal.mem_span_singleton, ←ideal.quotient.eq_zero_iff_mem], change mk f q = 0, rw [←commutes, ring_hom.comp_apply, mk_self, ring_hom.map_zero] }, { exact q_monic.ne_zero } }, refine ⟨_, eq_top_iff.mpr _⟩, { rw [←deg_f', minpoly_eq], exact algebra.linear_independent_power_basis hx, }, { rintros y -, rw [←deg_f', minpoly_eq], apply algebra.mem_span_power_basis hx, obtain ⟨g⟩ := y, use g, rw aeval_eq, refl } end /-- The power basis `1, root f, ..., root f ^ (d - 1)` for `adjoin_root f`, where `f` is an irreducible polynomial over a field of degree `d`. -/ noncomputable def power_basis (hf : f ≠ 0) : power_basis K (adjoin_root f) := { gen := root f, dim := f.nat_degree, is_basis := power_basis_is_basis hf } end adjoin_root namespace intermediate_field lemma power_basis_is_basis {x : L} (hx : is_integral K x) : is_basis K (λ (i : fin (minimal_polynomial hx).nat_degree), (adjoin_simple.gen K x ^ (i : ℕ))) := begin let ϕ := (adjoin_root_equiv_adjoin K hx).to_linear_equiv, have key : ϕ (adjoin_root.root (minimal_polynomial hx)) = adjoin_simple.gen K x, { exact intermediate_field.adjoin_root_equiv_adjoin_apply_root K hx }, suffices : ϕ ∘ (λ (i : fin (minimal_polynomial hx).nat_degree), adjoin_root.root (minimal_polynomial hx) ^ (i.val)) = (λ (i : fin (minimal_polynomial hx).nat_degree), (adjoin_simple.gen K x) ^ ↑i), { rw ← this, exact linear_equiv.is_basis (adjoin_root.power_basis_is_basis (minimal_polynomial.ne_zero hx)) ϕ }, ext y, rw [function.comp_app, fin.val_eq_coe, alg_equiv.to_linear_equiv_apply, alg_equiv.map_pow], rw intermediate_field.adjoin_root_equiv_adjoin_apply_root K hx, end /-- The power basis `1, x, ..., x ^ (d - 1)` for `K⟮x⟯`, where `d` is the degree of the minimal polynomial of `x`. -/ noncomputable def adjoin.power_basis {x : L} (hx : is_integral K x) : power_basis K K⟮x⟯ := { gen := adjoin_simple.gen K x, dim := (minimal_polynomial hx).nat_degree, is_basis := power_basis_is_basis hx } @[simp] lemma adjoin.power_basis.gen_eq {x : L} (hx : is_integral K x) : (adjoin.power_basis hx).gen = adjoin_simple.gen K x := rfl lemma adjoin.finite_dimensional {x : L} (hx : is_integral K x) : finite_dimensional K K⟮x⟯ := power_basis.finite_dimensional (adjoin.power_basis hx) lemma adjoin.findim {x : L} (hx : is_integral K x) : finite_dimensional.findim K K⟮x⟯ = (minimal_polynomial hx).nat_degree := begin rw power_basis.findim (adjoin.power_basis hx), refl, end end intermediate_field namespace power_basis open intermediate_field /-- `pb.equiv_adjoin_simple` is the equivalence between `K⟮pb.gen⟯` and `L` itself. -/ noncomputable def equiv_adjoin_simple (pb : power_basis K L) : K⟮pb.gen⟯ ≃ₐ[K] L := (adjoin.power_basis pb.is_integral_gen).equiv pb (minimal_polynomial.eq_of_algebra_map_eq (algebra_map K⟮pb.gen⟯ L).injective _ _ (by rw [adjoin.power_basis.gen_eq, adjoin_simple.algebra_map_gen])) @[simp] lemma equiv_adjoin_simple_aeval (pb : power_basis K L) (f : polynomial K) : pb.equiv_adjoin_simple (aeval (adjoin_simple.gen K pb.gen) f) = aeval pb.gen f := equiv_aeval _ pb _ f @[simp] lemma equiv_adjoin_simple_gen (pb : power_basis K L) : pb.equiv_adjoin_simple (adjoin_simple.gen K pb.gen) = pb.gen := equiv_gen _ pb _ @[simp] lemma equiv_adjoin_simple_symm_aeval (pb : power_basis K L) (f : polynomial K) : pb.equiv_adjoin_simple.symm (aeval pb.gen f) = aeval (adjoin_simple.gen K pb.gen) f := by rw [equiv_adjoin_simple, equiv_symm, equiv_aeval, adjoin.power_basis.gen_eq] @[simp] lemma equiv_adjoin_simple_symm_gen (pb : power_basis K L) : pb.equiv_adjoin_simple.symm pb.gen = (adjoin_simple.gen K pb.gen) := by rw [equiv_adjoin_simple, equiv_symm, equiv_gen, adjoin.power_basis.gen_eq] end power_basis
7748c09dc8ceb8c18e690ee1abaef288d660b78e
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/2095.lean
6a6142da9eef91dfe86f61108a18796cbf82494f
[ "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
166
lean
-- works example : 3 < 5 := calc _ < 4 := by decide _ < _ := by decide -- fails example : 3 < 5 := by calc _ < 4 := by decide _ < _ := by decide
d42775044b8f691fe340e291f0c69494d7ea1fef
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/category/Group/zero.lean
f16544873c4d544eb4447fe8f496555a33bacce3
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
1,462
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.category.Group.basic import category_theory.limits.shapes.zero_objects /-! # The category of (commutative) (additive) groups has a zero object. `AddCommGroup` also has zero morphisms. For definitional reasons, we infer this from preadditivity rather than from the existence of a zero object. -/ open category_theory open category_theory.limits universe u namespace Group @[to_additive] lemma is_zero_of_subsingleton (G : Group) [subsingleton G] : is_zero G := begin refine ⟨λ X, ⟨⟨⟨1⟩, λ f, _⟩⟩, λ X, ⟨⟨⟨1⟩, λ f, _⟩⟩⟩, { ext, have : x = 1 := subsingleton.elim _ _, rw [this, map_one, map_one], }, { ext, apply subsingleton.elim } end @[to_additive AddGroup.has_zero_object] instance : has_zero_object Group := ⟨⟨of punit, is_zero_of_subsingleton _⟩⟩ end Group namespace CommGroup @[to_additive] lemma is_zero_of_subsingleton (G : CommGroup) [subsingleton G] : is_zero G := begin refine ⟨λ X, ⟨⟨⟨1⟩, λ f, _⟩⟩, λ X, ⟨⟨⟨1⟩, λ f, _⟩⟩⟩, { ext, have : x = 1 := subsingleton.elim _ _, rw [this, map_one, map_one], }, { ext, apply subsingleton.elim } end @[to_additive AddCommGroup.has_zero_object] instance : has_zero_object CommGroup := ⟨⟨of punit, is_zero_of_subsingleton _⟩⟩ end CommGroup
a65d076baf1af36e418896678b3652fd6a0b0151
b19a1b7dc79c802247fdce4c04708e070863b4d2
/equality.lean
e718e050a86789f32cd3827030cf102e40648d2e
[]
no_license
utanapishtim/promethazine
99a1e80311fb20251a54ba78a534b23852b88c40
08a6f9bd6dd08feb3df8d4697e19ffc8d333b249
refs/heads/master
1,653,595,504,487
1,480,129,933,000
1,480,129,933,000
74,801,596
0
0
null
null
null
null
UTF-8
Lean
false
false
2,286
lean
import data.nat data.prod open nat prod check eq.refl check eq.symm check eq.trans section variables (A : Type) (a b c d : A) premises (Hab : a = b) (Hcb : c = b) (Hcd : c = d) example : a = d := eq.trans (eq.trans Hab (eq.symm Hcb)) Hcd end section variables (A : Type) (a b c d : A) premises (Hab : a = b) (Hcb : c = b) (Hcd : c = d) --BEGIN open eq.ops example : a = d := Hab ⬝ Hcb⁻¹ ⬝ Hcd end section variables (A B : Type) example (f : A → B) (a : A) : (λ x, f x) a = f a := rfl example (a : A) (b : A) : pr1 (a, b) = a := rfl example : 2 + 3 = (5 : ℕ) := rfl end -- equality is more than equivalence relation, it has the property that every -- assertion respects the equivalence in that we can substitude equal expressions -- with no effect on truth value section example (A : Type) (a b : A) (P : A → Prop) (H1 : a = b) (H2 : P a) : P b := eq.subst H1 H2 end section open eq.ops example (A : Type) (a b : A) (P : A → Prop) (H1 : a = b) (H2 : P a) : P b := H1 ▸ H2 end open nat eq.ops algebra -- '!'s infer explicit arguments to a theorem from the context example (x y : ℕ ) : (x + y) * (x + y) = x * x + y * x + y * y := have H1 : (x + y) * (x + y) = (x + y) * x + (x + y) * y, from !left_distrib, have H2 : (x + y) * (x + y) = x * x + (x * y + y * y), from (right_distrib x y x ▸ !right_distrib ▸ H1, !add.assoc⁻¹ ▸ H2 section variables (a b c d e : ℕ ) variable H1 : a = b variable H2 : b = c + 1 variable H3 : c = d variable H4 : e = 1 + d theorem T : a = e := calc a = b : H1 ... = c + 1 : H2 ... = d + 1 : {H3} ... = 1 + d : add.comm d 1 ... = e : eq.symm H4 end section theorem T2 (a b c : nat) (H1 : a = b) (H2 : b = c + 1) : a ≠ 0 := calc a = b : H1 ... = c + 1 : H2 ... = succ c : add_one c ... ≠ 0 : succ_ne_zero c end section example (x y : ℕ ) : (x + y) * (x + y) = x * x + y * x + x * y + y * y := calc (x + y) * (x + y) = (x + y) * x + (x + y) * y : left_distrib ... = x * x + y * x + (x + y) * y : right_distrib ... = x * x + y * x + (x * y + y * y) : right_distrib ... = x * x + y * x + x * y + y * y : add.assoc end
60f4fdb63d99f0994ab4a1e109caf8c923c2b595
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/num/lemmas.lean
0829d392077b4cc65b840bfd5b2ef1c128a27ce7
[ "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
53,574
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.num.bitwise import data.int.char_zero import data.nat.gcd.basic import data.nat.psub import data.nat.size /-! # Properties of the binary representation of integers > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ local attribute [simp] add_assoc namespace pos_num variables {α : Type*} @[simp, norm_cast] theorem cast_one [has_one α] [has_add α] : ((1 : pos_num) : α) = 1 := rfl @[simp] theorem cast_one' [has_one α] [has_add α] : (pos_num.one : α) = 1 := rfl @[simp, norm_cast] theorem cast_bit0 [has_one α] [has_add α] (n : pos_num) : (n.bit0 : α) = _root_.bit0 n := rfl @[simp, norm_cast] theorem cast_bit1 [has_one α] [has_add α] (n : pos_num) : (n.bit1 : α) = _root_.bit1 n := rfl @[simp, norm_cast] theorem cast_to_nat [add_monoid_with_one α] : ∀ n : pos_num, ((n : ℕ) : α) = n | 1 := nat.cast_one | (bit0 p) := (nat.cast_bit0 _).trans $ congr_arg _root_.bit0 p.cast_to_nat | (bit1 p) := (nat.cast_bit1 _).trans $ congr_arg _root_.bit1 p.cast_to_nat @[simp, norm_cast] theorem to_nat_to_int (n : pos_num) : ((n : ℕ) : ℤ) = n := cast_to_nat _ @[simp, norm_cast] theorem cast_to_int [add_group_with_one α] (n : pos_num) : ((n : ℤ) : α) = n := by rw [← to_nat_to_int, int.cast_coe_nat, cast_to_nat] theorem succ_to_nat : ∀ n, (succ n : ℕ) = n + 1 | 1 := rfl | (bit0 p) := rfl | (bit1 p) := (congr_arg _root_.bit0 (succ_to_nat p)).trans $ show ↑p + 1 + ↑p + 1 = ↑p + ↑p + 1 + 1, by simp [add_left_comm] theorem one_add (n : pos_num) : 1 + n = succ n := by cases n; refl theorem add_one (n : pos_num) : n + 1 = succ n := by cases n; refl @[norm_cast] theorem add_to_nat : ∀ m n, ((m + n : pos_num) : ℕ) = m + n | 1 b := by rw [one_add b, succ_to_nat, add_comm]; refl | a 1 := by rw [add_one a, succ_to_nat]; refl | (bit0 a) (bit0 b) := (congr_arg _root_.bit0 (add_to_nat a b)).trans $ add_add_add_comm _ _ _ _ | (bit0 a) (bit1 b) := (congr_arg _root_.bit1 (add_to_nat a b)).trans $ show ((a + b) + (a + b) + 1 : ℕ) = (a + a) + (b + b + 1), by simp [add_left_comm] | (bit1 a) (bit0 b) := (congr_arg _root_.bit1 (add_to_nat a b)).trans $ show ((a + b) + (a + b) + 1 : ℕ) = (a + a + 1) + (b + b), by simp [add_comm, add_left_comm] | (bit1 a) (bit1 b) := show (succ (a + b) + succ (a + b) : ℕ) = (a + a + 1) + (b + b + 1), by rw [succ_to_nat, add_to_nat]; simp [add_left_comm] theorem add_succ : ∀ (m n : pos_num), m + succ n = succ (m + n) | 1 b := by simp [one_add] | (bit0 a) 1 := congr_arg bit0 (add_one a) | (bit1 a) 1 := congr_arg bit1 (add_one a) | (bit0 a) (bit0 b) := rfl | (bit0 a) (bit1 b) := congr_arg bit0 (add_succ a b) | (bit1 a) (bit0 b) := rfl | (bit1 a) (bit1 b) := congr_arg bit1 (add_succ a b) theorem bit0_of_bit0 : Π n, _root_.bit0 n = bit0 n | 1 := rfl | (bit0 p) := congr_arg bit0 (bit0_of_bit0 p) | (bit1 p) := show bit0 (succ (_root_.bit0 p)) = _, by rw bit0_of_bit0; refl theorem bit1_of_bit1 (n : pos_num) : _root_.bit1 n = bit1 n := show _root_.bit0 n + 1 = bit1 n, by rw [add_one, bit0_of_bit0]; refl @[norm_cast] theorem mul_to_nat (m) : ∀ n, ((m * n : pos_num) : ℕ) = m * n | 1 := (mul_one _).symm | (bit0 p) := show (↑(m * p) + ↑(m * p) : ℕ) = ↑m * (p + p), by rw [mul_to_nat, left_distrib] | (bit1 p) := (add_to_nat (bit0 (m * p)) m).trans $ show (↑(m * p) + ↑(m * p) + ↑m : ℕ) = ↑m * (p + p) + m, by rw [mul_to_nat, left_distrib] theorem to_nat_pos : ∀ n : pos_num, 0 < (n : ℕ) | 1 := zero_lt_one | (bit0 p) := let h := to_nat_pos p in add_pos h h | (bit1 p) := nat.succ_pos _ theorem cmp_to_nat_lemma {m n : pos_num} : (m:ℕ) < n → (bit1 m : ℕ) < bit0 n := show (m:ℕ) < n → (m + m + 1 + 1 : ℕ) ≤ n + n, by intro h; rw [nat.add_right_comm m m 1, add_assoc]; exact add_le_add h h theorem cmp_swap (m) : ∀n, (cmp m n).swap = cmp n m := by induction m with m IH m IH; intro n; cases n with n n; try {unfold cmp}; try {refl}; rw ←IH; cases cmp m n; refl theorem cmp_to_nat : ∀ (m n), (ordering.cases_on (cmp m n) ((m:ℕ) < n) (m = n) ((n:ℕ) < m) : Prop) | 1 1 := rfl | (bit0 a) 1 := let h : (1:ℕ) ≤ a := to_nat_pos a in add_le_add h h | (bit1 a) 1 := nat.succ_lt_succ $ to_nat_pos $ bit0 a | 1 (bit0 b) := let h : (1:ℕ) ≤ b := to_nat_pos b in add_le_add h h | 1 (bit1 b) := nat.succ_lt_succ $ to_nat_pos $ bit0 b | (bit0 a) (bit0 b) := begin have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro, { exact add_lt_add this this }, { rw this }, { exact add_lt_add this this } end | (bit0 a) (bit1 b) := begin dsimp [cmp], have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro, { exact nat.le_succ_of_le (add_lt_add this this) }, { rw this, apply nat.lt_succ_self }, { exact cmp_to_nat_lemma this } end | (bit1 a) (bit0 b) := begin dsimp [cmp], have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro, { exact cmp_to_nat_lemma this }, { rw this, apply nat.lt_succ_self }, { exact nat.le_succ_of_le (add_lt_add this this) }, end | (bit1 a) (bit1 b) := begin have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro, { exact nat.succ_lt_succ (add_lt_add this this) }, { rw this }, { exact nat.succ_lt_succ (add_lt_add this this) } end @[norm_cast] theorem lt_to_nat {m n : pos_num} : (m:ℕ) < n ↔ m < n := show (m:ℕ) < n ↔ cmp m n = ordering.lt, from match cmp m n, cmp_to_nat m n with | ordering.lt, h := by simp at h; simp [h] | ordering.eq, h := by simp at h; simp [h, lt_irrefl]; exact dec_trivial | ordering.gt, h := by simp [not_lt_of_gt h]; exact dec_trivial end @[norm_cast] theorem le_to_nat {m n : pos_num} : (m:ℕ) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr lt_to_nat end pos_num namespace num variables {α : Type*} open pos_num theorem add_zero (n : num) : n + 0 = n := by cases n; refl theorem zero_add (n : num) : 0 + n = n := by cases n; refl theorem add_one : ∀ n : num, n + 1 = succ n | 0 := rfl | (pos p) := by cases p; refl theorem add_succ : ∀ (m n : num), m + succ n = succ (m + n) | 0 n := by simp [zero_add] | (pos p) 0 := show pos (p + 1) = succ (pos p + 0), by rw [pos_num.add_one, add_zero]; refl | (pos p) (pos q) := congr_arg pos (pos_num.add_succ _ _) theorem bit0_of_bit0 : ∀ n : num, bit0 n = n.bit0 | 0 := rfl | (pos p) := congr_arg pos p.bit0_of_bit0 theorem bit1_of_bit1 : ∀ n : num, bit1 n = n.bit1 | 0 := rfl | (pos p) := congr_arg pos p.bit1_of_bit1 @[simp] lemma of_nat'_zero : num.of_nat' 0 = 0 := by simp [num.of_nat'] lemma of_nat'_bit (b n) : of_nat' (nat.bit b n) = cond b num.bit1 num.bit0 (of_nat' n) := nat.binary_rec_eq rfl _ _ @[simp] lemma of_nat'_one : num.of_nat' 1 = 1 := by erw [of_nat'_bit tt 0, cond, of_nat'_zero]; refl lemma bit1_succ : ∀ n : num, n.bit1.succ = n.succ.bit0 | 0 := rfl | (pos n) := rfl lemma of_nat'_succ : ∀ {n}, of_nat' (n + 1) = of_nat' n + 1 := nat.binary_rec (by simp; refl) $ λ b n ih, begin cases b, { erw [of_nat'_bit tt n, of_nat'_bit], simp only [← bit1_of_bit1, ← bit0_of_bit0, cond, _root_.bit1] }, { erw [show n.bit tt + 1 = (n + 1).bit ff, by simp [nat.bit, _root_.bit1, _root_.bit0]; cc, of_nat'_bit, of_nat'_bit, ih], simp only [cond, add_one, bit1_succ], }, end @[simp] theorem add_of_nat' (m n) : num.of_nat' (m + n) = num.of_nat' m + num.of_nat' n := by induction n; simp [nat.add_zero, of_nat'_succ, add_zero, nat.add_succ, add_one, add_succ, *] @[simp, norm_cast] theorem cast_zero [has_zero α] [has_one α] [has_add α] : ((0 : num) : α) = 0 := rfl @[simp] theorem cast_zero' [has_zero α] [has_one α] [has_add α] : (num.zero : α) = 0 := rfl @[simp, norm_cast] theorem cast_one [has_zero α] [has_one α] [has_add α] : ((1 : num) : α) = 1 := rfl @[simp] theorem cast_pos [has_zero α] [has_one α] [has_add α] (n : pos_num) : (num.pos n : α) = n := rfl theorem succ'_to_nat : ∀ n, (succ' n : ℕ) = n + 1 | 0 := (_root_.zero_add _).symm | (pos p) := pos_num.succ_to_nat _ theorem succ_to_nat (n) : (succ n : ℕ) = n + 1 := succ'_to_nat n @[simp, norm_cast] theorem cast_to_nat [add_monoid_with_one α] : ∀ n : num, ((n : ℕ) : α) = n | 0 := nat.cast_zero | (pos p) := p.cast_to_nat @[norm_cast] theorem add_to_nat : ∀ m n, ((m + n : num) : ℕ) = m + n | 0 0 := rfl | 0 (pos q) := (_root_.zero_add _).symm | (pos p) 0 := rfl | (pos p) (pos q) := pos_num.add_to_nat _ _ @[norm_cast] theorem mul_to_nat : ∀ m n, ((m * n : num) : ℕ) = m * n | 0 0 := rfl | 0 (pos q) := (zero_mul _).symm | (pos p) 0 := rfl | (pos p) (pos q) := pos_num.mul_to_nat _ _ theorem cmp_to_nat : ∀ (m n), (ordering.cases_on (cmp m n) ((m:ℕ) < n) (m = n) ((n:ℕ) < m) : Prop) | 0 0 := rfl | 0 (pos b) := to_nat_pos _ | (pos a) 0 := to_nat_pos _ | (pos a) (pos b) := by { have := pos_num.cmp_to_nat a b; revert this; dsimp [cmp]; cases pos_num.cmp a b, exacts [id, congr_arg pos, id] } @[norm_cast] theorem lt_to_nat {m n : num} : (m:ℕ) < n ↔ m < n := show (m:ℕ) < n ↔ cmp m n = ordering.lt, from match cmp m n, cmp_to_nat m n with | ordering.lt, h := by simp at h; simp [h] | ordering.eq, h := by simp at h; simp [h, lt_irrefl]; exact dec_trivial | ordering.gt, h := by simp [not_lt_of_gt h]; exact dec_trivial end @[norm_cast] theorem le_to_nat {m n : num} : (m:ℕ) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr lt_to_nat end num namespace pos_num @[simp] theorem of_to_nat' : Π (n : pos_num), num.of_nat' (n : ℕ) = num.pos n | 1 := by erw [@num.of_nat'_bit tt 0, num.of_nat'_zero]; refl | (bit0 p) := by erw [@num.of_nat'_bit ff, of_to_nat']; refl | (bit1 p) := by erw [@num.of_nat'_bit tt, of_to_nat']; refl end pos_num namespace num @[simp, norm_cast] theorem of_to_nat' : Π (n : num), num.of_nat' (n : ℕ) = n | 0 := of_nat'_zero | (pos p) := p.of_to_nat' @[norm_cast] theorem to_nat_inj {m n : num} : (m : ℕ) = n ↔ m = n := ⟨λ h, function.left_inverse.injective of_to_nat' h, congr_arg _⟩ /-- This tactic tries to turn an (in)equality about `num`s to one about `nat`s by rewriting. ```lean example (n : num) (m : num) : n ≤ n + m := begin num.transfer_rw, exact nat.le_add_right _ _ end ``` -/ meta def transfer_rw : tactic unit := `[repeat {rw ← to_nat_inj <|> rw ← lt_to_nat <|> rw ← le_to_nat}, repeat {rw add_to_nat <|> rw mul_to_nat <|> rw cast_one <|> rw cast_zero}] /-- This tactic tries to prove (in)equalities about `num`s by transfering them to the `nat` world and then trying to call `simp`. ```lean example (n : num) (m : num) : n ≤ n + m := by num.transfer ``` -/ meta def transfer : tactic unit := `[intros, transfer_rw, try {simp}] instance : add_monoid num := { add := (+), zero := 0, zero_add := zero_add, add_zero := add_zero, add_assoc := by transfer } instance : add_monoid_with_one num := { nat_cast := num.of_nat', one := 1, nat_cast_zero := of_nat'_zero, nat_cast_succ := λ _, of_nat'_succ, .. num.add_monoid } instance : comm_semiring num := by refine_struct { mul := (*), one := 1, add := (+), zero := 0, npow := @npow_rec num ⟨1⟩ ⟨(*)⟩, .. num.add_monoid, .. num.add_monoid_with_one }; try { intros, refl }; try { transfer }; simp [add_comm, mul_add, add_mul, mul_assoc, mul_comm, mul_left_comm] instance : ordered_cancel_add_comm_monoid num := { lt := (<), lt_iff_le_not_le := by {intros a b, transfer_rw, apply lt_iff_le_not_le}, le := (≤), le_refl := by transfer, le_trans := by {intros a b c, transfer_rw, apply le_trans}, le_antisymm := by {intros a b, transfer_rw, apply le_antisymm}, add_le_add_left := by {intros a b h c, revert h, transfer_rw, exact λ h, add_le_add_left h c}, le_of_add_le_add_left := by {intros a b c, transfer_rw, apply le_of_add_le_add_left}, ..num.comm_semiring } instance : linear_ordered_semiring num := { le_total := by {intros a b, transfer_rw, apply le_total}, zero_le_one := dec_trivial, mul_lt_mul_of_pos_left := by {intros a b c, transfer_rw, apply mul_lt_mul_of_pos_left}, mul_lt_mul_of_pos_right := by {intros a b c, transfer_rw, apply mul_lt_mul_of_pos_right}, decidable_lt := num.decidable_lt, decidable_le := num.decidable_le, decidable_eq := num.decidable_eq, exists_pair_ne := ⟨0, 1, dec_trivial⟩, ..num.comm_semiring, ..num.ordered_cancel_add_comm_monoid } @[simp, norm_cast] theorem add_of_nat (m n) : ((m + n : ℕ) : num) = m + n := add_of_nat' _ _ @[simp, norm_cast] theorem to_nat_to_int (n : num) : ((n : ℕ) : ℤ) = n := cast_to_nat _ @[simp, norm_cast] theorem cast_to_int {α} [add_group_with_one α] (n : num) : ((n : ℤ) : α) = n := by rw [← to_nat_to_int, int.cast_coe_nat, cast_to_nat] theorem to_of_nat : Π (n : ℕ), ((n : num) : ℕ) = n | 0 := by rw [nat.cast_zero, cast_zero] | (n+1) := by rw [nat.cast_succ, add_one, succ_to_nat, to_of_nat] @[simp, norm_cast] theorem of_nat_cast {α} [add_monoid_with_one α] (n : ℕ) : ((n : num) : α) = n := by rw [← cast_to_nat, to_of_nat] @[simp, norm_cast] theorem of_nat_inj {m n : ℕ} : (m : num) = n ↔ m = n := ⟨λ h, function.left_inverse.injective to_of_nat h, congr_arg _⟩ @[simp, norm_cast] theorem of_to_nat : Π (n : num), ((n : ℕ) : num) = n := of_to_nat' @[norm_cast] theorem dvd_to_nat (m n : num) : (m : ℕ) ∣ n ↔ m ∣ n := ⟨λ ⟨k, e⟩, ⟨k, by rw [← of_to_nat n, e]; simp⟩, λ ⟨k, e⟩, ⟨k, by simp [e, mul_to_nat]⟩⟩ end num namespace pos_num variables {α : Type*} open num @[simp, norm_cast] theorem of_to_nat : Π (n : pos_num), ((n : ℕ) : num) = num.pos n := of_to_nat' @[norm_cast] theorem to_nat_inj {m n : pos_num} : (m : ℕ) = n ↔ m = n := ⟨λ h, num.pos.inj $ by rw [← pos_num.of_to_nat, ← pos_num.of_to_nat, h], congr_arg _⟩ theorem pred'_to_nat : ∀ n, (pred' n : ℕ) = nat.pred n | 1 := rfl | (bit0 n) := have nat.succ ↑(pred' n) = ↑n, by rw [pred'_to_nat n, nat.succ_pred_eq_of_pos (to_nat_pos n)], match pred' n, this : ∀ k : num, nat.succ ↑k = ↑n → ↑(num.cases_on k 1 bit1 : pos_num) = nat.pred (_root_.bit0 n) with | 0, (h : ((1:num):ℕ) = n) := by rw ← to_nat_inj.1 h; refl | num.pos p, (h : nat.succ ↑p = n) := by rw ← h; exact (nat.succ_add p p).symm end | (bit1 n) := rfl @[simp] theorem pred'_succ' (n) : pred' (succ' n) = n := num.to_nat_inj.1 $ by rw [pred'_to_nat, succ'_to_nat, nat.add_one, nat.pred_succ] @[simp] theorem succ'_pred' (n) : succ' (pred' n) = n := to_nat_inj.1 $ by rw [succ'_to_nat, pred'_to_nat, nat.add_one, nat.succ_pred_eq_of_pos (to_nat_pos _)] instance : has_dvd pos_num := ⟨λ m n, pos m ∣ pos n⟩ @[norm_cast] theorem dvd_to_nat {m n : pos_num} : (m:ℕ) ∣ n ↔ m ∣ n := num.dvd_to_nat (pos m) (pos n) theorem size_to_nat : ∀ n, (size n : ℕ) = nat.size n | 1 := nat.size_one.symm | (bit0 n) := by rw [size, succ_to_nat, size_to_nat, cast_bit0, nat.size_bit0 $ ne_of_gt $ to_nat_pos n] | (bit1 n) := by rw [size, succ_to_nat, size_to_nat, cast_bit1, nat.size_bit1] theorem size_eq_nat_size : ∀ n, (size n : ℕ) = nat_size n | 1 := rfl | (bit0 n) := by rw [size, succ_to_nat, nat_size, size_eq_nat_size] | (bit1 n) := by rw [size, succ_to_nat, nat_size, size_eq_nat_size] theorem nat_size_to_nat (n) : nat_size n = nat.size n := by rw [← size_eq_nat_size, size_to_nat] theorem nat_size_pos (n) : 0 < nat_size n := by cases n; apply nat.succ_pos /-- This tactic tries to turn an (in)equality about `pos_num`s to one about `nat`s by rewriting. ```lean example (n : pos_num) (m : pos_num) : n ≤ n + m := begin pos_num.transfer_rw, exact nat.le_add_right _ _ end ``` -/ meta def transfer_rw : tactic unit := `[repeat {rw ← to_nat_inj <|> rw ← lt_to_nat <|> rw ← le_to_nat}, repeat {rw add_to_nat <|> rw mul_to_nat <|> rw cast_one <|> rw cast_zero}] /-- This tactic tries to prove (in)equalities about `pos_num`s by transferring them to the `nat` world and then trying to call `simp`. ```lean example (n : pos_num) (m : pos_num) : n ≤ n + m := by pos_num.transfer ``` -/ meta def transfer : tactic unit := `[intros, transfer_rw, try {simp [add_comm, add_left_comm, mul_comm, mul_left_comm]}] instance : add_comm_semigroup pos_num := by refine {add := (+), ..}; transfer instance : comm_monoid pos_num := by refine_struct {mul := (*), one := (1 : pos_num), npow := @npow_rec pos_num ⟨1⟩ ⟨(*)⟩}; try { intros, refl }; transfer instance : distrib pos_num := by refine {add := (+), mul := (*), ..}; {transfer, simp [mul_add, mul_comm]} instance : linear_order pos_num := { lt := (<), lt_iff_le_not_le := by {intros a b, transfer_rw, apply lt_iff_le_not_le}, le := (≤), le_refl := by transfer, le_trans := by {intros a b c, transfer_rw, apply le_trans}, le_antisymm := by {intros a b, transfer_rw, apply le_antisymm}, le_total := by {intros a b, transfer_rw, apply le_total}, decidable_lt := by apply_instance, decidable_le := by apply_instance, decidable_eq := by apply_instance } @[simp] theorem cast_to_num (n : pos_num) : ↑n = num.pos n := by rw [← cast_to_nat, ← of_to_nat n] @[simp, norm_cast] theorem bit_to_nat (b n) : (bit b n : ℕ) = nat.bit b n := by cases b; refl @[simp, norm_cast] theorem cast_add [add_monoid_with_one α] (m n) : ((m + n : pos_num) : α) = m + n := by rw [← cast_to_nat, add_to_nat, nat.cast_add, cast_to_nat, cast_to_nat] @[simp, norm_cast, priority 500] theorem cast_succ [add_monoid_with_one α] (n : pos_num) : (succ n : α) = n + 1 := by rw [← add_one, cast_add, cast_one] @[simp, norm_cast] theorem cast_inj [add_monoid_with_one α] [char_zero α] {m n : pos_num} : (m:α) = n ↔ m = n := by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_inj, to_nat_inj] @[simp] theorem one_le_cast [linear_ordered_semiring α] (n : pos_num) : (1 : α) ≤ n := by rw [← cast_to_nat, ← nat.cast_one, nat.cast_le]; apply to_nat_pos @[simp] theorem cast_pos [linear_ordered_semiring α] (n : pos_num) : 0 < (n : α) := lt_of_lt_of_le zero_lt_one (one_le_cast n) @[simp, norm_cast] theorem cast_mul [semiring α] (m n) : ((m * n : pos_num) : α) = m * n := by rw [← cast_to_nat, mul_to_nat, nat.cast_mul, cast_to_nat, cast_to_nat] @[simp] theorem cmp_eq (m n) : cmp m n = ordering.eq ↔ m = n := begin have := cmp_to_nat m n, cases cmp m n; simp at this ⊢; try {exact this}; { simp [show m ≠ n, from λ e, by rw e at this; exact lt_irrefl _ this] } end @[simp, norm_cast] theorem cast_lt [linear_ordered_semiring α] {m n : pos_num} : (m:α) < n ↔ m < n := by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_lt, lt_to_nat] @[simp, norm_cast] theorem cast_le [linear_ordered_semiring α] {m n : pos_num} : (m:α) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr cast_lt end pos_num namespace num variables {α : Type*} open pos_num theorem bit_to_nat (b n) : (bit b n : ℕ) = nat.bit b n := by cases b; cases n; refl theorem cast_succ' [add_monoid_with_one α] (n) : (succ' n : α) = n + 1 := by rw [← pos_num.cast_to_nat, succ'_to_nat, nat.cast_add_one, cast_to_nat] theorem cast_succ [add_monoid_with_one α] (n) : (succ n : α) = n + 1 := cast_succ' n @[simp, norm_cast] theorem cast_add [semiring α] (m n) : ((m + n : num) : α) = m + n := by rw [← cast_to_nat, add_to_nat, nat.cast_add, cast_to_nat, cast_to_nat] @[simp, norm_cast] theorem cast_bit0 [semiring α] (n : num) : (n.bit0 : α) = _root_.bit0 n := by rw [← bit0_of_bit0, _root_.bit0, cast_add]; refl @[simp, norm_cast] theorem cast_bit1 [semiring α] (n : num) : (n.bit1 : α) = _root_.bit1 n := by rw [← bit1_of_bit1, _root_.bit1, bit0_of_bit0, cast_add, cast_bit0]; refl @[simp, norm_cast] theorem cast_mul [semiring α] : ∀ m n, ((m * n : num) : α) = m * n | 0 0 := (zero_mul _).symm | 0 (pos q) := (zero_mul _).symm | (pos p) 0 := (mul_zero _).symm | (pos p) (pos q) := pos_num.cast_mul _ _ theorem size_to_nat : ∀ n, (size n : ℕ) = nat.size n | 0 := nat.size_zero.symm | (pos p) := p.size_to_nat theorem size_eq_nat_size : ∀ n, (size n : ℕ) = nat_size n | 0 := rfl | (pos p) := p.size_eq_nat_size theorem nat_size_to_nat (n) : nat_size n = nat.size n := by rw [← size_eq_nat_size, size_to_nat] @[simp, priority 999] theorem of_nat'_eq : ∀ n, num.of_nat' n = n := nat.binary_rec (by simp) $ λ b n IH, begin rw of_nat' at IH ⊢, rw [nat.binary_rec_eq, IH], { cases b; simp [nat.bit, bit0_of_bit0, bit1_of_bit1] }, { refl } end theorem zneg_to_znum (n : num) : -n.to_znum = n.to_znum_neg := by cases n; refl theorem zneg_to_znum_neg (n : num) : -n.to_znum_neg = n.to_znum := by cases n; refl theorem to_znum_inj {m n : num} : m.to_znum = n.to_znum ↔ m = n := ⟨λ h, by cases m; cases n; cases h; refl, congr_arg _⟩ @[simp, norm_cast squash] theorem cast_to_znum [has_zero α] [has_one α] [has_add α] [has_neg α] : ∀ n : num, (n.to_znum : α) = n | 0 := rfl | (num.pos p) := rfl @[simp] theorem cast_to_znum_neg [add_group α] [has_one α] : ∀ n : num, (n.to_znum_neg : α) = -n | 0 := neg_zero.symm | (num.pos p) := rfl @[simp] theorem add_to_znum (m n : num) : num.to_znum (m + n) = m.to_znum + n.to_znum := by cases m; cases n; refl end num namespace pos_num open num theorem pred_to_nat {n : pos_num} (h : 1 < n) : (pred n : ℕ) = nat.pred n := begin unfold pred, have := pred'_to_nat n, cases e : pred' n, { have : (1:ℕ) ≤ nat.pred n := nat.pred_le_pred ((@cast_lt ℕ _ _ _).2 h), rw [← pred'_to_nat, e] at this, exact absurd this dec_trivial }, { rw [← pred'_to_nat, e], refl } end theorem sub'_one (a : pos_num) : sub' a 1 = (pred' a).to_znum := by cases a; refl theorem one_sub' (a : pos_num) : sub' 1 a = (pred' a).to_znum_neg := by cases a; refl theorem lt_iff_cmp {m n} : m < n ↔ cmp m n = ordering.lt := iff.rfl theorem le_iff_cmp {m n} : m ≤ n ↔ cmp m n ≠ ordering.gt := not_congr $ lt_iff_cmp.trans $ by rw ← cmp_swap; cases cmp m n; exact dec_trivial end pos_num namespace num variables {α : Type*} open pos_num theorem pred_to_nat : ∀ (n : num), (pred n : ℕ) = nat.pred n | 0 := rfl | (pos p) := by rw [pred, pos_num.pred'_to_nat]; refl theorem ppred_to_nat : ∀ (n : num), coe <$> ppred n = nat.ppred n | 0 := rfl | (pos p) := by rw [ppred, option.map_some, nat.ppred_eq_some.2]; rw [pos_num.pred'_to_nat, nat.succ_pred_eq_of_pos (pos_num.to_nat_pos _)]; refl theorem cmp_swap (m n) : (cmp m n).swap = cmp n m := by cases m; cases n; try {unfold cmp}; try {refl}; apply pos_num.cmp_swap theorem cmp_eq (m n) : cmp m n = ordering.eq ↔ m = n := begin have := cmp_to_nat m n, cases cmp m n; simp at this ⊢; try {exact this}; { simp [show m ≠ n, from λ e, by rw e at this; exact lt_irrefl _ this] } end @[simp, norm_cast] theorem cast_lt [linear_ordered_semiring α] {m n : num} : (m:α) < n ↔ m < n := by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_lt, lt_to_nat] @[simp, norm_cast] theorem cast_le [linear_ordered_semiring α] {m n : num} : (m:α) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr cast_lt @[simp, norm_cast] theorem cast_inj [linear_ordered_semiring α] {m n : num} : (m:α) = n ↔ m = n := by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_inj, to_nat_inj] theorem lt_iff_cmp {m n} : m < n ↔ cmp m n = ordering.lt := iff.rfl theorem le_iff_cmp {m n} : m ≤ n ↔ cmp m n ≠ ordering.gt := not_congr $ lt_iff_cmp.trans $ by rw ← cmp_swap; cases cmp m n; exact dec_trivial theorem bitwise_to_nat {f : num → num → num} {g : bool → bool → bool} (p : pos_num → pos_num → num) (gff : g ff ff = ff) (f00 : f 0 0 = 0) (f0n : ∀ n, f 0 (pos n) = cond (g ff tt) (pos n) 0) (fn0 : ∀ n, f (pos n) 0 = cond (g tt ff) (pos n) 0) (fnn : ∀ m n, f (pos m) (pos n) = p m n) (p11 : p 1 1 = cond (g tt tt) 1 0) (p1b : ∀ b n, p 1 (pos_num.bit b n) = bit (g tt b) (cond (g ff tt) (pos n) 0)) (pb1 : ∀ a m, p (pos_num.bit a m) 1 = bit (g a tt) (cond (g tt ff) (pos m) 0)) (pbb : ∀ a b m n, p (pos_num.bit a m) (pos_num.bit b n) = bit (g a b) (p m n)) : ∀ m n : num, (f m n : ℕ) = nat.bitwise g m n := begin intros, cases m with m; cases n with n; try { change zero with 0 }; try { change ((0:num):ℕ) with 0 }, { rw [f00, nat.bitwise_zero]; refl }, { unfold nat.bitwise, rw [f0n, nat.binary_rec_zero], cases g ff tt; refl }, { unfold nat.bitwise, generalize h : (pos m : ℕ) = m', revert h, apply nat.bit_cases_on m' _, intros b m' h, rw [fn0, nat.binary_rec_eq, nat.binary_rec_zero, ←h], cases g tt ff; refl, apply nat.bitwise_bit_aux gff }, { rw fnn, have : ∀b (n : pos_num), (cond b ↑n 0 : ℕ) = ↑(cond b (pos n) 0 : num) := by intros; cases b; refl, induction m with m IH m IH generalizing n; cases n with n n, any_goals { change one with 1 }, any_goals { change pos 1 with 1 }, any_goals { change pos_num.bit0 with pos_num.bit ff }, any_goals { change pos_num.bit1 with pos_num.bit tt }, any_goals { change ((1:num):ℕ) with nat.bit tt 0 }, all_goals { repeat { rw show ∀ b n, (pos (pos_num.bit b n) : ℕ) = nat.bit b ↑n, by intros; cases b; refl }, rw nat.bitwise_bit }, any_goals { assumption }, any_goals { rw [nat.bitwise_zero, p11], cases g tt tt; refl }, any_goals { rw [nat.bitwise_zero_left, this, ← bit_to_nat, p1b] }, any_goals { rw [nat.bitwise_zero_right _ gff, this, ← bit_to_nat, pb1] }, all_goals { rw [← show ∀ n, ↑(p m n) = nat.bitwise g ↑m ↑n, from IH], rw [← bit_to_nat, pbb] } } end @[simp, norm_cast] theorem lor_to_nat : ∀ m n, (lor m n : ℕ) = nat.lor m n := by apply bitwise_to_nat (λx y, pos (pos_num.lor x y)); intros; try {cases a}; try {cases b}; refl @[simp, norm_cast] theorem land_to_nat : ∀ m n, (land m n : ℕ) = nat.land m n := by apply bitwise_to_nat pos_num.land; intros; try {cases a}; try {cases b}; refl @[simp, norm_cast] theorem ldiff_to_nat : ∀ m n, (ldiff m n : ℕ) = nat.ldiff m n := by apply bitwise_to_nat pos_num.ldiff; intros; try {cases a}; try {cases b}; refl @[simp, norm_cast] theorem lxor_to_nat : ∀ m n, (lxor m n : ℕ) = nat.lxor m n := by apply bitwise_to_nat pos_num.lxor; intros; try {cases a}; try {cases b}; refl @[simp, norm_cast] theorem shiftl_to_nat (m n) : (shiftl m n : ℕ) = nat.shiftl m n := begin cases m; dunfold shiftl, {symmetry, apply nat.zero_shiftl}, simp, induction n with n IH, {refl}, simp [pos_num.shiftl, nat.shiftl_succ], rw ←IH end @[simp, norm_cast] theorem shiftr_to_nat (m n) : (shiftr m n : ℕ) = nat.shiftr m n := begin cases m with m; dunfold shiftr, {symmetry, apply nat.zero_shiftr}, induction n with n IH generalizing m, {cases m; refl}, cases m with m m; dunfold pos_num.shiftr, { rw [nat.shiftr_eq_div_pow], symmetry, apply nat.div_eq_of_lt, exact @nat.pow_lt_pow_of_lt_right 2 dec_trivial 0 (n+1) (nat.succ_pos _) }, { transitivity, apply IH, change nat.shiftr m n = nat.shiftr (bit1 m) (n+1), rw [add_comm n 1, nat.shiftr_add], apply congr_arg (λx, nat.shiftr x n), unfold nat.shiftr, change (bit1 ↑m : ℕ) with nat.bit tt m, rw nat.div2_bit }, { transitivity, apply IH, change nat.shiftr m n = nat.shiftr (bit0 m) (n + 1), rw [add_comm n 1, nat.shiftr_add], apply congr_arg (λx, nat.shiftr x n), unfold nat.shiftr, change (bit0 ↑m : ℕ) with nat.bit ff m, rw nat.div2_bit } end @[simp] theorem test_bit_to_nat (m n) : test_bit m n = nat.test_bit m n := begin cases m with m; unfold test_bit nat.test_bit, { change (zero : nat) with 0, rw nat.zero_shiftr, refl }, induction n with n IH generalizing m; cases m; dunfold pos_num.test_bit, {refl}, { exact (nat.bodd_bit _ _).symm }, { exact (nat.bodd_bit _ _).symm }, { change ff = nat.bodd (nat.shiftr 1 (n + 1)), rw [add_comm, nat.shiftr_add], change nat.shiftr 1 1 with 0, rw nat.zero_shiftr; refl }, { change pos_num.test_bit m n = nat.bodd (nat.shiftr (nat.bit tt m) (n + 1)), rw [add_comm, nat.shiftr_add], unfold nat.shiftr, rw nat.div2_bit, apply IH }, { change pos_num.test_bit m n = nat.bodd (nat.shiftr (nat.bit ff m) (n + 1)), rw [add_comm, nat.shiftr_add], unfold nat.shiftr, rw nat.div2_bit, apply IH }, end end num namespace znum variables {α : Type*} open pos_num @[simp, norm_cast] theorem cast_zero [has_zero α] [has_one α] [has_add α] [has_neg α] : ((0 : znum) : α) = 0 := rfl @[simp] theorem cast_zero' [has_zero α] [has_one α] [has_add α] [has_neg α] : (znum.zero : α) = 0 := rfl @[simp, norm_cast] theorem cast_one [has_zero α] [has_one α] [has_add α] [has_neg α] : ((1 : znum) : α) = 1 := rfl @[simp] theorem cast_pos [has_zero α] [has_one α] [has_add α] [has_neg α] (n : pos_num) : (pos n : α) = n := rfl @[simp] theorem cast_neg [has_zero α] [has_one α] [has_add α] [has_neg α] (n : pos_num) : (neg n : α) = -n := rfl @[simp, norm_cast] theorem cast_zneg [add_group α] [has_one α] : ∀ n, ((-n : znum) : α) = -n | 0 := neg_zero.symm | (pos p) := rfl | (neg p) := (neg_neg _).symm theorem neg_zero : (-0 : znum) = 0 := rfl theorem zneg_pos (n : pos_num) : -pos n = neg n := rfl theorem zneg_neg (n : pos_num) : -neg n = pos n := rfl theorem zneg_zneg (n : znum) : - -n = n := by cases n; refl theorem zneg_bit1 (n : znum) : -n.bit1 = (-n).bitm1 := by cases n; refl theorem zneg_bitm1 (n : znum) : -n.bitm1 = (-n).bit1 := by cases n; refl theorem zneg_succ (n : znum) : -n.succ = (-n).pred := by cases n; try {refl}; rw [succ, num.zneg_to_znum_neg]; refl theorem zneg_pred (n : znum) : -n.pred = (-n).succ := by rw [← zneg_zneg (succ (-n)), zneg_succ, zneg_zneg] @[simp] theorem abs_to_nat : ∀ n, (abs n : ℕ) = int.nat_abs n | 0 := rfl | (pos p) := congr_arg int.nat_abs p.to_nat_to_int | (neg p) := show int.nat_abs ((p:ℕ):ℤ) = int.nat_abs (- p), by rw [p.to_nat_to_int, int.nat_abs_neg] @[simp] theorem abs_to_znum : ∀ n : num, abs n.to_znum = n | 0 := rfl | (num.pos p) := rfl @[simp, norm_cast] theorem cast_to_int [add_group_with_one α] : ∀ n : znum, ((n : ℤ) : α) = n | 0 := by rw [cast_zero, cast_zero, int.cast_zero] | (pos p) := by rw [cast_pos, cast_pos, pos_num.cast_to_int] | (neg p) := by rw [cast_neg, cast_neg, int.cast_neg, pos_num.cast_to_int] theorem bit0_of_bit0 : ∀ n : znum, _root_.bit0 n = n.bit0 | 0 := rfl | (pos a) := congr_arg pos a.bit0_of_bit0 | (neg a) := congr_arg neg a.bit0_of_bit0 theorem bit1_of_bit1 : ∀ n : znum, _root_.bit1 n = n.bit1 | 0 := rfl | (pos a) := congr_arg pos a.bit1_of_bit1 | (neg a) := show pos_num.sub' 1 (_root_.bit0 a) = _, by rw [pos_num.one_sub', a.bit0_of_bit0]; refl @[simp, norm_cast] theorem cast_bit0 [add_group_with_one α] : ∀ n : znum, (n.bit0 : α) = bit0 n | 0 := (add_zero _).symm | (pos p) := by rw [znum.bit0, cast_pos, cast_pos]; refl | (neg p) := by rw [znum.bit0, cast_neg, cast_neg, pos_num.cast_bit0, _root_.bit0, _root_.bit0, neg_add_rev] @[simp, norm_cast] theorem cast_bit1 [add_group_with_one α] : ∀ n : znum, (n.bit1 : α) = bit1 n | 0 := by simp [znum.bit1, _root_.bit1, _root_.bit0] | (pos p) := by rw [znum.bit1, cast_pos, cast_pos]; refl | (neg p) := begin rw [znum.bit1, cast_neg, cast_neg], cases e : pred' p with a; have : p = _ := (succ'_pred' p).symm.trans (congr_arg num.succ' e), { change p=1 at this, subst p, simp [_root_.bit1, _root_.bit0] }, { rw [num.succ'] at this, subst p, have : (↑(-↑a:ℤ) : α) = -1 + ↑(-↑a + 1 : ℤ), {simp [add_comm]}, simpa [_root_.bit1, _root_.bit0, -add_comm] }, end @[simp] theorem cast_bitm1 [add_group_with_one α] (n : znum) : (n.bitm1 : α) = bit0 n - 1 := begin conv { to_lhs, rw ← zneg_zneg n }, rw [← zneg_bit1, cast_zneg, cast_bit1], have : ((-1 + n + n : ℤ) : α) = (n + n + -1 : ℤ), {simp [add_comm, add_left_comm]}, simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg, -int.add_neg_one] end theorem add_zero (n : znum) : n + 0 = n := by cases n; refl theorem zero_add (n : znum) : 0 + n = n := by cases n; refl theorem add_one : ∀ n : znum, n + 1 = succ n | 0 := rfl | (pos p) := congr_arg pos p.add_one | (neg p) := by cases p; refl end znum namespace pos_num variables {α : Type*} theorem cast_to_znum : ∀ n : pos_num, (n : znum) = znum.pos n | 1 := rfl | (bit0 p) := (znum.bit0_of_bit0 p).trans $ congr_arg _ (cast_to_znum p) | (bit1 p) := (znum.bit1_of_bit1 p).trans $ congr_arg _ (cast_to_znum p) local attribute [-simp] int.add_neg_one theorem cast_sub' [add_group_with_one α] : ∀ m n : pos_num, (sub' m n : α) = m - n | a 1 := by rw [sub'_one, num.cast_to_znum, ← num.cast_to_nat, pred'_to_nat, ← nat.sub_one]; simp [pos_num.cast_pos] | 1 b := by rw [one_sub', num.cast_to_znum_neg, ← neg_sub, neg_inj, ← num.cast_to_nat, pred'_to_nat, ← nat.sub_one]; simp [pos_num.cast_pos] | (bit0 a) (bit0 b) := begin rw [sub', znum.cast_bit0, cast_sub'], have : ((a + -b + (a + -b) : ℤ) : α) = a + a + (-b + -b), {simp [add_left_comm]}, simpa [_root_.bit0, sub_eq_add_neg] end | (bit0 a) (bit1 b) := begin rw [sub', znum.cast_bitm1, cast_sub'], have : ((-b + (a + (-b + -1)) : ℤ) : α) = (a + -1 + (-b + -b):ℤ), { simp [add_comm, add_left_comm] }, simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg] end | (bit1 a) (bit0 b) := begin rw [sub', znum.cast_bit1, cast_sub'], have : ((-b + (a + (-b + 1)) : ℤ) : α) = (a + 1 + (-b + -b):ℤ), { simp [add_comm, add_left_comm] }, simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg] end | (bit1 a) (bit1 b) := begin rw [sub', znum.cast_bit0, cast_sub'], have : ((-b + (a + -b) : ℤ) : α) = a + (-b + -b), {simp [add_left_comm]}, simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg] end theorem to_nat_eq_succ_pred (n : pos_num) : (n:ℕ) = n.pred' + 1 := by rw [← num.succ'_to_nat, n.succ'_pred'] theorem to_int_eq_succ_pred (n : pos_num) : (n:ℤ) = (n.pred' : ℕ) + 1 := by rw [← n.to_nat_to_int, to_nat_eq_succ_pred]; refl end pos_num namespace num variables {α : Type*} @[simp] theorem cast_sub' [add_group_with_one α] : ∀ m n : num, (sub' m n : α) = m - n | 0 0 := (sub_zero _).symm | (pos a) 0 := (sub_zero _).symm | 0 (pos b) := (zero_sub _).symm | (pos a) (pos b) := pos_num.cast_sub' _ _ theorem to_znum_succ : ∀ n : num, n.succ.to_znum = n.to_znum.succ | 0 := rfl | (pos n) := rfl theorem to_znum_neg_succ : ∀ n : num, n.succ.to_znum_neg = n.to_znum_neg.pred | 0 := rfl | (pos n) := rfl @[simp] theorem pred_succ : ∀ n : znum, n.pred.succ = n | 0 := rfl | (znum.neg p) := show to_znum_neg (pos p).succ'.pred' = _, by rw [pos_num.pred'_succ']; refl | (znum.pos p) := by rw [znum.pred, ← to_znum_succ, num.succ, pos_num.succ'_pred', to_znum] theorem succ_of_int' : ∀ n, znum.of_int' (n + 1) = znum.of_int' n + 1 | (n : ℕ) := by erw [znum.of_int', znum.of_int', num.of_nat'_succ, num.add_one, to_znum_succ, znum.add_one] | -[1+ 0] := by erw [znum.of_int', znum.of_int', of_nat'_succ, of_nat'_zero]; refl | -[1+ n+1] := by erw [znum.of_int', znum.of_int', @num.of_nat'_succ (n+1), num.add_one, to_znum_neg_succ, @of_nat'_succ n, num.add_one, znum.add_one, pred_succ] theorem of_int'_to_znum : ∀ n : ℕ, to_znum n = znum.of_int' n | 0 := rfl | (n+1) := by rw [nat.cast_succ, num.add_one, to_znum_succ, of_int'_to_znum, nat.cast_succ, succ_of_int', znum.add_one] theorem mem_of_znum' : ∀ {m : num} {n : znum}, m ∈ of_znum' n ↔ n = to_znum m | 0 0 := ⟨λ _, rfl, λ _, rfl⟩ | (pos m) 0 := ⟨λ h, by cases h, λ h, by cases h⟩ | m (znum.pos p) := option.some_inj.trans $ by cases m; split; intro h; try {cases h}; refl | m (znum.neg p) := ⟨λ h, by cases h, λ h, by cases m; cases h⟩ theorem of_znum'_to_nat : ∀ (n : znum), coe <$> of_znum' n = int.to_nat' n | 0 := rfl | (znum.pos p) := show _ = int.to_nat' p, by rw [← pos_num.to_nat_to_int p]; refl | (znum.neg p) := congr_arg (λ x, int.to_nat' (-x)) $ show ((p.pred' + 1 : ℕ) : ℤ) = p, by rw ← succ'_to_nat; simp @[simp] theorem of_znum_to_nat : ∀ (n : znum), (of_znum n : ℕ) = int.to_nat n | 0 := rfl | (znum.pos p) := show _ = int.to_nat p, by rw [← pos_num.to_nat_to_int p]; refl | (znum.neg p) := congr_arg (λ x, int.to_nat (-x)) $ show ((p.pred' + 1 : ℕ) : ℤ) = p, by rw ← succ'_to_nat; simp @[simp] theorem cast_of_znum [add_group_with_one α] (n : znum) : (of_znum n : α) = int.to_nat n := by rw [← cast_to_nat, of_znum_to_nat] @[simp, norm_cast] theorem sub_to_nat (m n) : ((m - n : num) : ℕ) = m - n := show (of_znum _ : ℕ) = _, by rw [of_znum_to_nat, cast_sub', ← to_nat_to_int, ← to_nat_to_int, int.to_nat_sub] end num namespace znum variables {α : Type*} @[simp, norm_cast] theorem cast_add [add_group_with_one α] : ∀ m n, ((m + n : znum) : α) = m + n | 0 a := by cases a; exact (_root_.zero_add _).symm | b 0 := by cases b; exact (_root_.add_zero _).symm | (pos a) (pos b) := pos_num.cast_add _ _ | (pos a) (neg b) := by simpa only [sub_eq_add_neg] using pos_num.cast_sub' _ _ | (neg a) (pos b) := have (↑b + -↑a : α) = -↑a + ↑b, by rw [← pos_num.cast_to_int a, ← pos_num.cast_to_int b, ← int.cast_neg, ← int.cast_add (-a)]; simp [add_comm], (pos_num.cast_sub' _ _).trans $ (sub_eq_add_neg _ _).trans this | (neg a) (neg b) := show -(↑(a + b) : α) = -a + -b, by rw [ pos_num.cast_add, neg_eq_iff_eq_neg, neg_add_rev, neg_neg, neg_neg, ← pos_num.cast_to_int a, ← pos_num.cast_to_int b, ← int.cast_add, ← int.cast_add, add_comm] @[simp] theorem cast_succ [add_group_with_one α] (n) : ((succ n : znum) : α) = n + 1 := by rw [← add_one, cast_add, cast_one] @[simp, norm_cast] theorem mul_to_int : ∀ m n, ((m * n : znum) : ℤ) = m * n | 0 a := by cases a; exact (_root_.zero_mul _).symm | b 0 := by cases b; exact (_root_.mul_zero _).symm | (pos a) (pos b) := pos_num.cast_mul a b | (pos a) (neg b) := show -↑(a * b) = ↑a * -↑b, by rw [pos_num.cast_mul, neg_mul_eq_mul_neg] | (neg a) (pos b) := show -↑(a * b) = -↑a * ↑b, by rw [pos_num.cast_mul, neg_mul_eq_neg_mul] | (neg a) (neg b) := show ↑(a * b) = -↑a * -↑b, by rw [pos_num.cast_mul, neg_mul_neg] theorem cast_mul [ring α] (m n) : ((m * n : znum) : α) = m * n := by rw [← cast_to_int, mul_to_int, int.cast_mul, cast_to_int, cast_to_int] theorem of_int'_neg : ∀ n : ℤ, of_int' (-n) = -of_int' n | -[1+ n] := show of_int' (n + 1 : ℕ) = _, by simp only [of_int', num.zneg_to_znum_neg] | 0 := show num.to_znum _ = -num.to_znum _, by rw [num.of_nat'_zero]; refl | (n+1 : ℕ) := show num.to_znum_neg _ = -num.to_znum _, by rw [num.zneg_to_znum]; refl theorem of_to_int' : ∀ (n : znum), znum.of_int' n = n | 0 := by erw [of_int', num.of_nat'_zero, num.to_znum] | (pos a) := by rw [cast_pos, ← pos_num.cast_to_nat, ← num.of_int'_to_znum, pos_num.of_to_nat]; refl | (neg a) := by rw [cast_neg, of_int'_neg, ← pos_num.cast_to_nat, ← num.of_int'_to_znum, pos_num.of_to_nat]; refl theorem to_int_inj {m n : znum} : (m : ℤ) = n ↔ m = n := ⟨λ h, function.left_inverse.injective of_to_int' h, congr_arg _⟩ theorem cmp_to_int : ∀ (m n), (ordering.cases_on (cmp m n) ((m:ℤ) < n) (m = n) ((n:ℤ) < m) : Prop) | 0 0 := rfl | (pos a) (pos b) := begin have := pos_num.cmp_to_nat a b; revert this; dsimp [cmp]; cases pos_num.cmp a b; dsimp; [simp, exact congr_arg pos, simp [gt]] end | (neg a) (neg b) := begin have := pos_num.cmp_to_nat b a; revert this; dsimp [cmp]; cases pos_num.cmp b a; dsimp; [simp, simp {contextual := tt}, simp [gt]] end | (pos a) 0 := pos_num.cast_pos _ | (pos a) (neg b) := lt_trans (neg_lt_zero.2 $ pos_num.cast_pos _) (pos_num.cast_pos _) | 0 (neg b) := neg_lt_zero.2 $ pos_num.cast_pos _ | (neg a) 0 := neg_lt_zero.2 $ pos_num.cast_pos _ | (neg a) (pos b) := lt_trans (neg_lt_zero.2 $ pos_num.cast_pos _) (pos_num.cast_pos _) | 0 (pos b) := pos_num.cast_pos _ @[norm_cast] theorem lt_to_int {m n : znum} : (m:ℤ) < n ↔ m < n := show (m:ℤ) < n ↔ cmp m n = ordering.lt, from match cmp m n, cmp_to_int m n with | ordering.lt, h := by simp at h; simp [h] | ordering.eq, h := by simp at h; simp [h, lt_irrefl]; exact dec_trivial | ordering.gt, h := by simp [not_lt_of_gt h]; exact dec_trivial end theorem le_to_int {m n : znum} : (m:ℤ) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr lt_to_int @[simp, norm_cast] theorem cast_lt [linear_ordered_ring α] {m n : znum} : (m:α) < n ↔ m < n := by rw [← cast_to_int m, ← cast_to_int n, int.cast_lt, lt_to_int] @[simp, norm_cast] theorem cast_le [linear_ordered_ring α] {m n : znum} : (m:α) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr cast_lt @[simp, norm_cast] theorem cast_inj [linear_ordered_ring α] {m n : znum} : (m:α) = n ↔ m = n := by rw [← cast_to_int m, ← cast_to_int n, int.cast_inj, to_int_inj] /-- This tactic tries to turn an (in)equality about `znum`s to one about `int`s by rewriting. ```lean example (n : znum) (m : znum) : n ≤ n + m * m := begin znum.transfer_rw, exact le_add_of_nonneg_right (mul_self_nonneg _) end ``` -/ meta def transfer_rw : tactic unit := `[repeat {rw ← to_int_inj <|> rw ← lt_to_int <|> rw ← le_to_int}, repeat {rw cast_add <|> rw mul_to_int <|> rw cast_one <|> rw cast_zero}] /-- This tactic tries to prove (in)equalities about `znum`s by transfering them to the `int` world and then trying to call `simp`. ```lean example (n : znum) (m : znum) : n ≤ n + m * m := begin znum.transfer, exact mul_self_nonneg _ end ``` -/ meta def transfer : tactic unit := `[intros, transfer_rw, try {simp [add_comm, add_left_comm, mul_comm, mul_left_comm]}] instance : linear_order znum := { lt := (<), lt_iff_le_not_le := by {intros a b, transfer_rw, apply lt_iff_le_not_le}, le := (≤), le_refl := by transfer, le_trans := by {intros a b c, transfer_rw, apply le_trans}, le_antisymm := by {intros a b, transfer_rw, apply le_antisymm}, le_total := by {intros a b, transfer_rw, apply le_total}, decidable_eq := znum.decidable_eq, decidable_le := znum.decidable_le, decidable_lt := znum.decidable_lt } instance : add_comm_group znum := { add := (+), add_assoc := by transfer, zero := 0, zero_add := zero_add, add_zero := add_zero, add_comm := by transfer, neg := has_neg.neg, add_left_neg := by transfer } instance : add_monoid_with_one znum := { one := 1, nat_cast := λ n, znum.of_int' n, nat_cast_zero := show (num.of_nat' 0).to_znum = 0, by rw num.of_nat'_zero; refl, nat_cast_succ := λ n, show (num.of_nat' (n+1)).to_znum = (num.of_nat' n).to_znum + 1, by rw [num.of_nat'_succ, num.add_one, num.to_znum_succ, znum.add_one], .. znum.add_comm_group } instance : linear_ordered_comm_ring znum := { mul := (*), mul_assoc := by transfer, one := 1, one_mul := by transfer, mul_one := by transfer, left_distrib := by {transfer, simp [mul_add]}, right_distrib := by {transfer, simp [mul_add, mul_comm]}, mul_comm := by transfer, exists_pair_ne := ⟨0, 1, dec_trivial⟩, add_le_add_left := by {intros a b h c, revert h, transfer_rw, exact λ h, add_le_add_left h c}, mul_pos := λ a b, show 0 < a → 0 < b → 0 < a * b, by {transfer_rw, apply mul_pos}, zero_le_one := dec_trivial, ..znum.linear_order, ..znum.add_comm_group, ..znum.add_monoid_with_one } @[simp, norm_cast] theorem cast_sub [ring α] (m n) : ((m - n : znum) : α) = m - n := by simp [sub_eq_neg_add] @[simp, norm_cast] theorem neg_of_int : ∀ n, ((-n : ℤ) : znum) = -n | (n+1:ℕ) := rfl | 0 := by rw [int.cast_neg, int.cast_zero] | -[1+n] := (zneg_zneg _).symm @[simp] theorem of_int'_eq : ∀ n : ℤ, znum.of_int' n = n | (n : ℕ) := rfl | -[1+ n] := begin show num.to_znum_neg (n+1 : ℕ) = -(n+1 : ℕ), rw [← neg_inj, neg_neg, nat.cast_succ, num.add_one, num.zneg_to_znum_neg, num.to_znum_succ, nat.cast_succ, znum.add_one], refl end @[simp] theorem of_nat_to_znum (n : ℕ) : num.to_znum n = n := rfl @[simp, norm_cast] theorem of_to_int (n : znum) : ((n : ℤ) : znum) = n := by rw [← of_int'_eq, of_to_int'] theorem to_of_int (n : ℤ) : ((n : znum) : ℤ) = n := int.induction_on' n 0 (by simp) (by simp) (by simp) @[simp] theorem of_nat_to_znum_neg (n : ℕ) : num.to_znum_neg n = -n := by rw [← of_nat_to_znum, num.zneg_to_znum] @[simp, norm_cast] theorem of_int_cast [add_group_with_one α] (n : ℤ) : ((n : znum) : α) = n := by rw [← cast_to_int, to_of_int] @[simp, norm_cast] theorem of_nat_cast [add_group_with_one α] (n : ℕ) : ((n : znum) : α) = n := by rw [← int.cast_coe_nat, of_int_cast, int.cast_coe_nat] @[simp, norm_cast] theorem dvd_to_int (m n : znum) : (m : ℤ) ∣ n ↔ m ∣ n := ⟨λ ⟨k, e⟩, ⟨k, by rw [← of_to_int n, e]; simp⟩, λ ⟨k, e⟩, ⟨k, by simp [e]⟩⟩ end znum namespace pos_num theorem divmod_to_nat_aux {n d : pos_num} {q r : num} (h₁ : (r:ℕ) + d * _root_.bit0 q = n) (h₂ : (r:ℕ) < 2 * d) : ((divmod_aux d q r).2 + d * (divmod_aux d q r).1 : ℕ) = ↑n ∧ ((divmod_aux d q r).2 : ℕ) < d := begin unfold divmod_aux, have : ∀ {r₂}, num.of_znum' (num.sub' r (num.pos d)) = some r₂ ↔ (r : ℕ) = r₂ + d, { intro r₂, apply num.mem_of_znum'.trans, rw [← znum.to_int_inj, num.cast_to_znum, num.cast_sub', sub_eq_iff_eq_add, ← int.coe_nat_inj'], simp }, cases e : num.of_znum' (num.sub' r (num.pos d)) with r₂; simp [divmod_aux], { refine ⟨h₁, lt_of_not_ge (λ h, _)⟩, cases nat.le.dest h with r₂ e', rw [← num.to_of_nat r₂, add_comm] at e', cases e.symm.trans (this.2 e'.symm) }, { have := this.1 e, split, { rwa [_root_.bit1, add_comm _ 1, mul_add, mul_one, ← add_assoc, ← this] }, { rwa [this, two_mul, add_lt_add_iff_right] at h₂ } } end theorem divmod_to_nat (d n : pos_num) : (n / d : ℕ) = (divmod d n).1 ∧ (n % d : ℕ) = (divmod d n).2 := begin rw nat.div_mod_unique (pos_num.cast_pos _), induction n with n IH n IH, { exact divmod_to_nat_aux (by simp; refl) (nat.mul_le_mul_left 2 (pos_num.cast_pos d : (0 : ℕ) < d)) }, { unfold divmod, cases divmod d n with q r, simp only [divmod] at IH ⊢, apply divmod_to_nat_aux; simp, { rw [_root_.bit1, _root_.bit1, add_right_comm, bit0_eq_two_mul (n : ℕ), ← IH.1, mul_add, ← bit0_eq_two_mul, mul_left_comm, ← bit0_eq_two_mul] }, { rw ← bit0_eq_two_mul, exact nat.bit1_lt_bit0 IH.2 } }, { unfold divmod, cases divmod d n with q r, simp only [divmod] at IH ⊢, apply divmod_to_nat_aux; simp, { rw [bit0_eq_two_mul (n : ℕ), ← IH.1, mul_add, ← bit0_eq_two_mul, mul_left_comm, ← bit0_eq_two_mul] }, { rw ← bit0_eq_two_mul, exact nat.bit0_lt IH.2 } } end @[simp] theorem div'_to_nat (n d) : (div' n d : ℕ) = n / d := (divmod_to_nat _ _).1.symm @[simp] theorem mod'_to_nat (n d) : (mod' n d : ℕ) = n % d := (divmod_to_nat _ _).2.symm end pos_num namespace num @[simp] protected lemma div_zero (n : num) : n / 0 = 0 := show n.div 0 = 0, by { cases n, refl, simp [num.div] } @[simp, norm_cast] theorem div_to_nat : ∀ n d, ((n / d : num) : ℕ) = n / d | 0 0 := by simp | 0 (pos d) := (nat.zero_div _).symm | (pos n) 0 := (nat.div_zero _).symm | (pos n) (pos d) := pos_num.div'_to_nat _ _ @[simp] protected lemma mod_zero (n : num) : n % 0 = n := show n.mod 0 = n, by { cases n, refl, simp [num.mod] } @[simp, norm_cast] theorem mod_to_nat : ∀ n d, ((n % d : num) : ℕ) = n % d | 0 0 := by simp | 0 (pos d) := (nat.zero_mod _).symm | (pos n) 0 := (nat.mod_zero _).symm | (pos n) (pos d) := pos_num.mod'_to_nat _ _ theorem gcd_to_nat_aux : ∀ {n} {a b : num}, a ≤ b → (a * b).nat_size ≤ n → (gcd_aux n a b : ℕ) = nat.gcd a b | 0 0 b ab h := (nat.gcd_zero_left _).symm | 0 (pos a) 0 ab h := (not_lt_of_ge ab).elim rfl | 0 (pos a) (pos b) ab h := (not_lt_of_le h).elim $ pos_num.nat_size_pos _ | (nat.succ n) 0 b ab h := (nat.gcd_zero_left _).symm | (nat.succ n) (pos a) b ab h := begin simp [gcd_aux], rw [nat.gcd_rec, gcd_to_nat_aux, mod_to_nat], {refl}, { rw [← le_to_nat, mod_to_nat], exact le_of_lt (nat.mod_lt _ (pos_num.cast_pos _)) }, rw [nat_size_to_nat, mul_to_nat, nat.size_le] at h ⊢, rw [mod_to_nat, mul_comm], rw [pow_succ', ← nat.mod_add_div b (pos a)] at h, refine lt_of_mul_lt_mul_right (lt_of_le_of_lt _ h) (nat.zero_le 2), rw [mul_two, mul_add], refine add_le_add_left (nat.mul_le_mul_left _ (le_trans (le_of_lt (nat.mod_lt _ (pos_num.cast_pos _))) _)) _, suffices : 1 ≤ _, simpa using nat.mul_le_mul_left (pos a) this, rw [nat.le_div_iff_mul_le a.cast_pos, one_mul], exact le_to_nat.2 ab end @[simp] theorem gcd_to_nat : ∀ a b, (gcd a b : ℕ) = nat.gcd a b := have ∀ a b : num, (a * b).nat_size ≤ a.nat_size + b.nat_size, begin intros, simp [nat_size_to_nat], rw [nat.size_le, pow_add], exact mul_lt_mul'' (nat.lt_size_self _) (nat.lt_size_self _) (nat.zero_le _) (nat.zero_le _) end, begin intros, unfold gcd, split_ifs, { exact gcd_to_nat_aux h (this _ _) }, { rw nat.gcd_comm, exact gcd_to_nat_aux (le_of_not_le h) (this _ _) } end theorem dvd_iff_mod_eq_zero {m n : num} : m ∣ n ↔ n % m = 0 := by rw [← dvd_to_nat, nat.dvd_iff_mod_eq_zero, ← to_nat_inj, mod_to_nat]; refl instance decidable_dvd : decidable_rel ((∣) : num → num → Prop) | a b := decidable_of_iff' _ dvd_iff_mod_eq_zero end num instance pos_num.decidable_dvd : decidable_rel ((∣) : pos_num → pos_num → Prop) | a b := num.decidable_dvd _ _ namespace znum @[simp] protected lemma div_zero (n : znum) : n / 0 = 0 := show n.div 0 = 0, by cases n; refl <|> simp [znum.div] @[simp, norm_cast] theorem div_to_int : ∀ n d, ((n / d : znum) : ℤ) = n / d | 0 0 := by simp [int.div_zero] | 0 (pos d) := (int.zero_div _).symm | 0 (neg d) := (int.zero_div _).symm | (pos n) 0 := (int.div_zero _).symm | (neg n) 0 := (int.div_zero _).symm | (pos n) (pos d) := (num.cast_to_znum _).trans $ by rw ← num.to_nat_to_int; simp | (pos n) (neg d) := (num.cast_to_znum_neg _).trans $ by rw ← num.to_nat_to_int; simp | (neg n) (pos d) := show - _ = (-_/↑d), begin rw [n.to_int_eq_succ_pred, d.to_int_eq_succ_pred, ← pos_num.to_nat_to_int, num.succ'_to_nat, num.div_to_nat], change -[1+ n.pred' / ↑d] = -[1+ n.pred' / (d.pred' + 1)], rw d.to_nat_eq_succ_pred end | (neg n) (neg d) := show ↑(pos_num.pred' n / num.pos d).succ' = (-_ / -↑d), begin rw [n.to_int_eq_succ_pred, d.to_int_eq_succ_pred, ← pos_num.to_nat_to_int, num.succ'_to_nat, num.div_to_nat], change (nat.succ (_/d) : ℤ) = nat.succ (n.pred'/(d.pred' + 1)), rw d.to_nat_eq_succ_pred end @[simp, norm_cast] theorem mod_to_int : ∀ n d, ((n % d : znum) : ℤ) = n % d | 0 d := (int.zero_mod _).symm | (pos n) d := (num.cast_to_znum _).trans $ by rw [← num.to_nat_to_int, cast_pos, num.mod_to_nat, ← pos_num.to_nat_to_int, abs_to_nat]; refl | (neg n) d := (num.cast_sub' _ _).trans $ by rw [← num.to_nat_to_int, cast_neg, ← num.to_nat_to_int, num.succ_to_nat, num.mod_to_nat, abs_to_nat, ← int.sub_nat_nat_eq_coe, n.to_int_eq_succ_pred]; refl @[simp] theorem gcd_to_nat (a b) : (gcd a b : ℕ) = int.gcd a b := (num.gcd_to_nat _ _).trans $ by simpa theorem dvd_iff_mod_eq_zero {m n : znum} : m ∣ n ↔ n % m = 0 := by rw [← dvd_to_int, int.dvd_iff_mod_eq_zero, ← to_int_inj, mod_to_int]; refl instance : decidable_rel ((∣) : znum → znum → Prop) | a b := decidable_of_iff' _ dvd_iff_mod_eq_zero end znum namespace int /-- Cast a `snum` to the corresponding integer. -/ def of_snum : snum → ℤ := snum.rec' (λ a, cond a (-1) 0) (λa p IH, cond a (bit1 IH) (bit0 IH)) instance snum_coe : has_coe snum ℤ := ⟨of_snum⟩ end int instance : has_lt snum := ⟨λa b, (a : ℤ) < b⟩ instance : has_le snum := ⟨λa b, (a : ℤ) ≤ b⟩
5e19c878818e4669f229b87067a1d7b2a79c3747
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/analysis/normed_space/dual.lean
83b615206e138254eed9b1042be8e8bb4d3eef94
[ "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,408
lean
/- Copyright (c) 2020 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import analysis.normed_space.hahn_banach /-! # The topological dual of a normed space In this file we define the topological dual `normed_space.dual` of a normed space, and the continuous linear map `normed_space.inclusion_in_double_dual` from a normed space into its double dual. For base field `𝕜 = ℝ` or `𝕜 = ℂ`, this map is actually an isometric embedding; we provide a version `normed_space.inclusion_in_double_dual_li` of the map which is of type a bundled linear isometric embedding, `E →ₗᵢ[𝕜] (dual 𝕜 (dual 𝕜 E))`. Since a lot of elementary properties don't require `eq_of_dist_eq_zero` we start setting up the theory for `semi_normed_space` and we specialize to `normed_space` when needed. ## Tags dual -/ noncomputable theory open_locale classical universes u v namespace normed_space section general variables (𝕜 : Type*) [nondiscrete_normed_field 𝕜] variables (E : Type*) [semi_normed_group E] [semi_normed_space 𝕜 E] variables (F : Type*) [normed_group F] [normed_space 𝕜 F] /-- The topological dual of a seminormed space `E`. -/ @[derive [inhabited, semi_normed_group, semi_normed_space 𝕜]] def dual := E →L[𝕜] 𝕜 instance : has_coe_to_fun (dual 𝕜 E) (λ _, E → 𝕜) := continuous_linear_map.to_fun instance : normed_group (dual 𝕜 F) := continuous_linear_map.to_normed_group instance : normed_space 𝕜 (dual 𝕜 F) := continuous_linear_map.to_normed_space instance [finite_dimensional 𝕜 E] : finite_dimensional 𝕜 (dual 𝕜 E) := continuous_linear_map.finite_dimensional /-- The inclusion of a normed space in its double (topological) dual, considered as a bounded linear map. -/ def inclusion_in_double_dual : E →L[𝕜] (dual 𝕜 (dual 𝕜 E)) := continuous_linear_map.apply 𝕜 𝕜 @[simp] lemma dual_def (x : E) (f : dual 𝕜 E) : inclusion_in_double_dual 𝕜 E x f = f x := rfl lemma inclusion_in_double_dual_norm_eq : ∥inclusion_in_double_dual 𝕜 E∥ = ∥(continuous_linear_map.id 𝕜 (dual 𝕜 E))∥ := continuous_linear_map.op_norm_flip _ lemma inclusion_in_double_dual_norm_le : ∥inclusion_in_double_dual 𝕜 E∥ ≤ 1 := by { rw inclusion_in_double_dual_norm_eq, exact continuous_linear_map.norm_id_le } lemma double_dual_bound (x : E) : ∥(inclusion_in_double_dual 𝕜 E) x∥ ≤ ∥x∥ := by simpa using continuous_linear_map.le_of_op_norm_le _ (inclusion_in_double_dual_norm_le 𝕜 E) x end general section bidual_isometry variables (𝕜 : Type v) [is_R_or_C 𝕜] {E : Type u} [normed_group E] [normed_space 𝕜 E] /-- If one controls the norm of every `f x`, then one controls the norm of `x`. Compare `continuous_linear_map.op_norm_le_bound`. -/ lemma norm_le_dual_bound (x : E) {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ (f : dual 𝕜 E), ∥f x∥ ≤ M * ∥f∥) : ∥x∥ ≤ M := begin classical, by_cases h : x = 0, { simp only [h, hMp, norm_zero] }, { obtain ⟨f, hf⟩ : ∃ g : E →L[𝕜] 𝕜, _ := exists_dual_vector 𝕜 x h, calc ∥x∥ = ∥norm' 𝕜 x∥ : (norm_norm' _ _ _).symm ... = ∥f x∥ : by rw hf.2 ... ≤ M * ∥f∥ : hM f ... = M : by rw [hf.1, mul_one] } end lemma eq_zero_of_forall_dual_eq_zero {x : E} (h : ∀ f : dual 𝕜 E, f x = (0 : 𝕜)) : x = 0 := norm_eq_zero.mp (le_antisymm (norm_le_dual_bound 𝕜 x le_rfl (λ f, by simp [h f])) (norm_nonneg _)) lemma eq_zero_iff_forall_dual_eq_zero (x : E) : x = 0 ↔ ∀ g : dual 𝕜 E, g x = 0 := ⟨λ hx, by simp [hx], λ h, eq_zero_of_forall_dual_eq_zero 𝕜 h⟩ lemma eq_iff_forall_dual_eq {x y : E} : x = y ↔ ∀ g : dual 𝕜 E, g x = g y := begin rw [← sub_eq_zero, eq_zero_iff_forall_dual_eq_zero 𝕜 (x - y)], simp [sub_eq_zero], end /-- The inclusion of a normed space in its double dual is an isometry onto its image.-/ def inclusion_in_double_dual_li : E →ₗᵢ[𝕜] (dual 𝕜 (dual 𝕜 E)) := { norm_map' := begin intros x, apply le_antisymm, { exact double_dual_bound 𝕜 E x }, rw continuous_linear_map.norm_def, apply le_cInf continuous_linear_map.bounds_nonempty, rintros c ⟨hc1, hc2⟩, exact norm_le_dual_bound 𝕜 x hc1 hc2 end, .. inclusion_in_double_dual 𝕜 E } end bidual_isometry end normed_space
9599d7ce0e10b861686874f58fc6fa5605021304
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/algebra/char_p/invertible.lean
5bd083a0cf6ea77a16c801dcf09068a615bc5653
[ "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
2,104
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import algebra.invertible import algebra.field import algebra.char_p.basic /-! # Invertibility of elements given a characteristic This file includes some instances of `invertible` for specific numbers in characteristic zero. Some more cases are given as a `def`, to be included only when needed. To construct instances for concrete numbers, `invertible_of_nonzero` is a useful definition. -/ variables {K : Type*} section field variables [field K] /-- A natural number `t` is invertible in a field `K` if the charactistic of `K` does not divide `t`. -/ def invertible_of_ring_char_not_dvd {t : ℕ} (not_dvd : ¬(ring_char K ∣ t)) : invertible (t : K) := invertible_of_nonzero (λ h, not_dvd ((ring_char.spec K t).mp h)) lemma ring_char_not_dvd_of_invertible {t : ℕ} [invertible (t : K)] : ¬(ring_char K ∣ t) := begin rw [← ring_char.spec, ← ne.def], exact nonzero_of_invertible (t : K) end /-- A natural number `t` is invertible in a field `K` of charactistic `p` if `p` does not divide `t`. -/ def invertible_of_char_p_not_dvd {p : ℕ} [char_p K p] {t : ℕ} (not_dvd : ¬(p ∣ t)) : invertible (t : K) := invertible_of_nonzero (λ h, not_dvd ((char_p.cast_eq_zero_iff K p t).mp h)) instance invertible_of_pos [char_zero K] (n : ℕ) [h : fact (0 < n)] : invertible (n : K) := invertible_of_nonzero $ by simpa [pos_iff_ne_zero] using h.out end field section division_ring variables [division_ring K] [char_zero K] instance invertible_succ (n : ℕ) : invertible (n.succ : K) := invertible_of_nonzero (nat.cast_ne_zero.mpr (nat.succ_ne_zero _)) /-! A few `invertible n` instances for small numerals `n`. Feel free to add your own number when you need its inverse. -/ instance invertible_two : invertible (2 : K) := invertible_of_nonzero (by exact_mod_cast (dec_trivial : 2 ≠ 0)) instance invertible_three : invertible (3 : K) := invertible_of_nonzero (by exact_mod_cast (dec_trivial : 3 ≠ 0)) end division_ring
ec32d8f012e20abb2ee68c8ecc379e664c28c6a0
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/tactic/interactive.lean
5b70a3623d08bc1bd74cbe40400dd98ab3503a74
[ "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
39,309
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Simon Hudon, Sébastien Gouëzel, Scott Morrison -/ import logic.nonempty import tactic.lint import tactic.dependencies setup_tactic_parser namespace tactic namespace interactive open interactive interactive.types expr /-- Similar to `constructor`, but does not reorder goals. -/ meta def fconstructor : tactic unit := concat_tags tactic.fconstructor add_tactic_doc { name := "fconstructor", category := doc_category.tactic, decl_names := [`tactic.interactive.fconstructor], tags := ["logic", "goal management"] } /-- `try_for n { tac }` executes `tac` for `n` ticks, otherwise uses `sorry` to close the goal. Never fails. Useful for debugging. -/ meta def try_for (max : parse parser.pexpr) (tac : itactic) : tactic unit := do max ← i_to_expr_strict max >>= tactic.eval_expr nat, λ s, match _root_.try_for max (tac s) with | some r := r | none := (tactic.trace "try_for timeout, using sorry" >> admit) s end /-- Multiple `subst`. `substs x y z` is the same as `subst x, subst y, subst z`. -/ meta def substs (l : parse ident*) : tactic unit := propagate_tags $ l.mmap' (λ h, get_local h >>= tactic.subst) >> try (tactic.reflexivity reducible) add_tactic_doc { name := "substs", category := doc_category.tactic, decl_names := [`tactic.interactive.substs], tags := ["rewriting"] } /-- Unfold coercion-related definitions -/ meta def unfold_coes (loc : parse location) : tactic unit := unfold [ ``coe, ``coe_t, ``has_coe_t.coe, ``coe_b,``has_coe.coe, ``lift, ``has_lift.lift, ``lift_t, ``has_lift_t.lift, ``coe_fn, ``has_coe_to_fun.coe, ``coe_sort, ``has_coe_to_sort.coe] loc add_tactic_doc { name := "unfold_coes", category := doc_category.tactic, decl_names := [`tactic.interactive.unfold_coes], tags := ["simplification"] } /-- Unfold `has_well_founded.r`, `sizeof` and other such definitions. -/ meta def unfold_wf := propagate_tags (well_founded_tactics.unfold_wf_rel; well_founded_tactics.unfold_sizeof) /-- Unfold auxiliary definitions associated with the current declaration. -/ meta def unfold_aux : tactic unit := do tgt ← target, name ← decl_name, let to_unfold := (tgt.list_names_with_prefix name), guard (¬ to_unfold.empty), -- should we be using simp_lemmas.mk_default? simp_lemmas.mk.dsimplify to_unfold.to_list tgt >>= tactic.change /-- For debugging only. This tactic checks the current state for any missing dropped goals and restores them. Useful when there are no goals to solve but "result contains meta-variables". -/ meta def recover : tactic unit := metavariables >>= tactic.set_goals /-- Like `try { tac }`, but in the case of failure it continues from the failure state instead of reverting to the original state. -/ meta def continue (tac : itactic) : tactic unit := λ s, result.cases_on (tac s) (λ a, result.success ()) (λ e ref, result.success ()) /-- `id { tac }` is the same as `tac`, but it is useful for creating a block scope without requiring the goal to be solved at the end like `{ tac }`. It can also be used to enclose a non-interactive tactic for patterns like `tac1; id {tac2}` where `tac2` is non-interactive. -/ @[inline] protected meta def id (tac : itactic) : tactic unit := tac /-- `work_on_goal n { tac }` creates a block scope for the `n`-goal (indexed from zero), and does not require that the goal be solved at the end (any remaining subgoals are inserted back into the list of goals). Typically usage might look like: ```` intros, simp, apply lemma_1, work_on_goal 2 { dsimp, simp }, refl ```` See also `id { tac }`, which is equivalent to `work_on_goal 0 { tac }`. -/ meta def work_on_goal : parse small_nat → itactic → tactic unit | n t := do goals ← get_goals, let earlier_goals := goals.take n, let later_goals := goals.drop (n+1), set_goals (goals.nth n).to_list, t, new_goals ← get_goals, set_goals (earlier_goals ++ new_goals ++ later_goals) /-- `swap n` will move the `n`th goal to the front. `swap` defaults to `swap 2`, and so interchanges the first and second goals. See also `tactic.interactive.rotate`, which moves the first `n` goals to the back. -/ meta def swap (n := 2) : tactic unit := do gs ← get_goals, match gs.nth (n-1) with | (some g) := set_goals (g :: gs.remove_nth (n-1)) | _ := skip end add_tactic_doc { name := "swap", category := doc_category.tactic, decl_names := [`tactic.interactive.swap], tags := ["goal management"] } /-- `rotate` moves the first goal to the back. `rotate n` will do this `n` times. See also `tactic.interactive.swap`, which moves the `n`th goal to the front. -/ meta def rotate (n := 1) : tactic unit := tactic.rotate n add_tactic_doc { name := "rotate", category := doc_category.tactic, decl_names := [`tactic.interactive.rotate], tags := ["goal management"] } /-- Clear all hypotheses starting with `_`, like `_match` and `_let_match`. -/ meta def clear_ : tactic unit := tactic.repeat $ do l ← local_context, l.reverse.mfirst $ λ h, do name.mk_string s p ← return $ local_pp_name h, guard (s.front = '_'), cl ← infer_type h >>= is_class, guard (¬ cl), tactic.clear h add_tactic_doc { name := "clear_", category := doc_category.tactic, decl_names := [`tactic.interactive.clear_], tags := ["context management"] } /-- Acts like `have`, but removes a hypothesis with the same name as this one. For example if the state is `h : p ⊢ goal` and `f : p → q`, then after `replace h := f h` the goal will be `h : q ⊢ goal`, where `have h := f h` would result in the state `h : p, h : q ⊢ goal`. This can be used to simulate the `specialize` and `apply at` tactics of Coq. -/ meta def replace (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : tactic unit := do let h := h.get_or_else `this, old ← try_core (get_local h), «have» h q₁ q₂, match old, q₂ with | none, _ := skip | some o, some _ := tactic.clear o | some o, none := swap >> tactic.clear o >> swap end add_tactic_doc { name := "replace", category := doc_category.tactic, decl_names := [`tactic.interactive.replace], tags := ["context management"] } /-- Make every proposition in the context decidable. -/ meta def classical := tactic.classical add_tactic_doc { name := "classical", category := doc_category.tactic, decl_names := [`tactic.interactive.classical], tags := ["classical logic", "type class"] } private meta def generalize_arg_p_aux : pexpr → parser (pexpr × name) | (app (app (macro _ [const `eq _ ]) h) (local_const x _ _ _)) := pure (h, x) | _ := fail "parse error" private meta def generalize_arg_p : parser (pexpr × name) := with_desc "expr = id" $ parser.pexpr 0 >>= generalize_arg_p_aux @[nolint def_lemma] lemma {u} generalize_a_aux {α : Sort u} (h : ∀ x : Sort u, (α → x) → x) : α := h α id /-- Like `generalize` but also considers assumptions specified by the user. The user can also specify to omit the goal. -/ meta def generalize_hyp (h : parse ident?) (_ : parse $ tk ":") (p : parse generalize_arg_p) (l : parse location) : tactic unit := do h' ← get_unused_name `h, x' ← get_unused_name `x, g ← if ¬ l.include_goal then do refine ``(generalize_a_aux _), some <$> (prod.mk <$> tactic.intro x' <*> tactic.intro h') else pure none, n ← l.get_locals >>= tactic.revert_lst, generalize h () p, intron n, match g with | some (x',h') := do tactic.apply h', tactic.clear h', tactic.clear x' | none := return () end add_tactic_doc { name := "generalize_hyp", category := doc_category.tactic, decl_names := [`tactic.interactive.generalize_hyp], tags := ["context management"] } meta def compact_decl_aux : list name → binder_info → expr → list expr → tactic (list (list name × binder_info × expr)) | ns bi t [] := pure [(ns.reverse, bi, t)] | ns bi t (v'@(local_const n pp bi' t') :: xs) := do t' ← infer_type v', if bi = bi' ∧ t = t' then compact_decl_aux (pp :: ns) bi t xs else do vs ← compact_decl_aux [pp] bi' t' xs, pure $ (ns.reverse, bi, t) :: vs | ns bi t (_ :: xs) := compact_decl_aux ns bi t xs /-- go from (x₀ : t₀) (x₁ : t₀) (x₂ : t₀) to (x₀ x₁ x₂ : t₀) -/ meta def compact_decl : list expr → tactic (list (list name × binder_info × expr)) | [] := pure [] | (v@(local_const n pp bi t) :: xs) := do t ← infer_type v, compact_decl_aux [pp] bi t xs | (_ :: xs) := compact_decl xs /-- Remove identity functions from a term. These are normally automatically generated with terms like `show t, from p` or `(p : t)` which translate to some variant on `@id t p` in order to retain the type. -/ meta def clean (q : parse texpr) : tactic unit := do tgt : expr ← target, e ← i_to_expr_strict ``(%%q : %%tgt), tactic.exact $ e.clean meta def source_fields (missing : list name) (e : pexpr) : tactic (list (name × pexpr)) := do e ← to_expr e, t ← infer_type e, let struct_n : name := t.get_app_fn.const_name, fields ← expanded_field_list struct_n, let exp_fields := fields.filter (λ x, x.2 ∈ missing), exp_fields.mmap $ λ ⟨p,n⟩, (prod.mk n ∘ to_pexpr) <$> mk_mapp (n.update_prefix p) [none,some e] meta def collect_struct' : pexpr → state_t (list $ expr×structure_instance_info) tactic pexpr | e := do some str ← pure (e.get_structure_instance_info) | e.traverse collect_struct', v ← monad_lift mk_mvar, modify (list.cons (v,str)), pure $ to_pexpr v meta def collect_struct (e : pexpr) : tactic $ pexpr × list (expr×structure_instance_info) := prod.map id list.reverse <$> (collect_struct' e).run [] meta def refine_one (str : structure_instance_info) : tactic $ list (expr×structure_instance_info) := do tgt ← target >>= whnf, let struct_n : name := tgt.get_app_fn.const_name, exp_fields ← expanded_field_list struct_n, let missing_f := exp_fields.filter (λ f, (f.2 : name) ∉ str.field_names), (src_field_names,src_field_vals) ← (@list.unzip name _ ∘ list.join) <$> str.sources.mmap (source_fields $ missing_f.map prod.snd), let provided := exp_fields.filter (λ f, (f.2 : name) ∈ str.field_names), let missing_f' := missing_f.filter (λ x, x.2 ∉ src_field_names), vs ← mk_mvar_list missing_f'.length, (field_values,new_goals) ← list.unzip <$> (str.field_values.mmap collect_struct : tactic _), e' ← to_expr $ pexpr.mk_structure_instance { struct := some struct_n , field_names := str.field_names ++ missing_f'.map prod.snd ++ src_field_names , field_values := field_values ++ vs.map to_pexpr ++ src_field_vals }, tactic.exact e', gs ← with_enable_tags ( mzip_with (λ (n : name × name) v, do set_goals [v], try (dsimp_target simp_lemmas.mk), apply_auto_param <|> apply_opt_param <|> (set_main_tag [`_field,n.2,n.1]), get_goals) missing_f' vs), set_goals gs.join, return new_goals.join meta def refine_recursively : expr × structure_instance_info → tactic (list expr) | (e,str) := do set_goals [e], rs ← refine_one str, gs ← get_goals, gs' ← rs.mmap refine_recursively, return $ gs'.join ++ gs /-- `refine_struct { .. }` acts like `refine` but works only with structure instance literals. It creates a goal for each missing field and tags it with the name of the field so that `have_field` can be used to generically refer to the field currently being refined. As an example, we can use `refine_struct` to automate the construction of semigroup instances: ```lean refine_struct ( { .. } : semigroup α ), -- case semigroup, mul -- α : Type u, -- ⊢ α → α → α -- case semigroup, mul_assoc -- α : Type u, -- ⊢ ∀ (a b c : α), a * b * c = a * (b * c) ``` `have_field`, used after `refine_struct _`, poses `field` as a local constant with the type of the field of the current goal: ```lean refine_struct ({ .. } : semigroup α), { have_field, ... }, { have_field, ... }, ``` behaves like ```lean refine_struct ({ .. } : semigroup α), { have field := @semigroup.mul, ... }, { have field := @semigroup.mul_assoc, ... }, ``` -/ meta def refine_struct : parse texpr → tactic unit | e := do (x,xs) ← collect_struct e, refine x, gs ← get_goals, xs' ← xs.mmap refine_recursively, set_goals (xs'.join ++ gs) /-- `guard_hyp' h : t` fails if the hypothesis `h` does not have type `t`. We use this tactic for writing tests. Fixes `guard_hyp` by instantiating meta variables -/ meta def guard_hyp' (n : parse ident) (p : parse $ tk ":" *> texpr) : tactic unit := do h ← get_local n >>= infer_type >>= instantiate_mvars, guard_expr_eq h p /-- `match_hyp h : t` fails if the hypothesis `h` does not match the type `t` (which may be a pattern). We use this tactic for writing tests. -/ meta def match_hyp (n : parse ident) (p : parse $ tk ":" *> texpr) (m := reducible) : tactic (list expr) := do h ← get_local n >>= infer_type >>= instantiate_mvars, match_expr p h m /-- `guard_expr_strict t := e` fails if the expr `t` is not equal to `e`. By contrast to `guard_expr`, this tests strict (syntactic) equality. We use this tactic for writing tests. -/ meta def guard_expr_strict (t : expr) (p : parse $ tk ":=" *> texpr) : tactic unit := do e ← to_expr p, guard (t = e) /-- `guard_target_strict t` fails if the target of the main goal is not syntactically `t`. We use this tactic for writing tests. -/ meta def guard_target_strict (p : parse texpr) : tactic unit := do t ← target, guard_expr_strict t p /-- `guard_hyp_strict h : t` fails if the hypothesis `h` does not have type syntactically equal to `t`. We use this tactic for writing tests. -/ meta def guard_hyp_strict (n : parse ident) (p : parse $ tk ":" *> texpr) : tactic unit := do h ← get_local n >>= infer_type >>= instantiate_mvars, guard_expr_strict h p /-- Tests that there are `n` hypotheses in the current context. -/ meta def guard_hyp_nums (n : ℕ) : tactic unit := do k ← local_context, guard (n = k.length) <|> fail format!"{k.length} hypotheses found" /-- Test that `t` is the tag of the main goal. -/ meta def guard_tags (tags : parse ident*) : tactic unit := do (t : list name) ← get_main_tag, guard (t = tags) /-- `guard_proof_term { t } e` applies tactic `t` and tests whether the resulting proof term unifies with `p`. -/ meta def guard_proof_term (t : itactic) (p : parse texpr) : itactic := do g :: _ ← get_goals, e ← to_expr p, t, g ← instantiate_mvars g, unify e g /-- `success_if_fail_with_msg { tac } msg` succeeds if the interactive tactic `tac` fails with error message `msg` (for test writing purposes). -/ meta def success_if_fail_with_msg (tac : tactic.interactive.itactic) := tactic.success_if_fail_with_msg tac /-- Get the field of the current goal. -/ meta def get_current_field : tactic name := do [_,field,str] ← get_main_tag, expr.const_name <$> resolve_name (field.update_prefix str) meta def field (n : parse ident) (tac : itactic) : tactic unit := do gs ← get_goals, ts ← gs.mmap get_tag, ([g],gs') ← pure $ (list.zip gs ts).partition (λ x, x.snd.nth 1 = some n), set_goals [g.1], tac, done, set_goals $ gs'.map prod.fst /-- `have_field`, used after `refine_struct _` poses `field` as a local constant with the type of the field of the current goal: ```lean refine_struct ({ .. } : semigroup α), { have_field, ... }, { have_field, ... }, ``` behaves like ```lean refine_struct ({ .. } : semigroup α), { have field := @semigroup.mul, ... }, { have field := @semigroup.mul_assoc, ... }, ``` -/ meta def have_field : tactic unit := propagate_tags $ get_current_field >>= mk_const >>= note `field none >> return () /-- `apply_field` functions as `have_field, apply field, clear field` -/ meta def apply_field : tactic unit := propagate_tags $ get_current_field >>= applyc add_tactic_doc { name := "refine_struct", category := doc_category.tactic, decl_names := [`tactic.interactive.refine_struct, `tactic.interactive.apply_field, `tactic.interactive.have_field], tags := ["structures"], inherit_description_from := `tactic.interactive.refine_struct } /-- `apply_rules hs n` applies the list of lemmas `hs` and `assumption` on the first goal and the resulting subgoals, iteratively, at most `n` times. `n` is optional, equal to 50 by default. You can pass an `apply_cfg` option argument as `apply_rules hs n opt`. (A typical usage would be with `apply_rules hs n { md := reducible })`, which asks `apply_rules` to not unfold `semireducible` definitions (i.e. most) when checking if a lemma matches the goal.) `hs` can contain user attributes: in this case all theorems with this attribute are added to the list of rules. For instance: ```lean @[user_attribute] meta def mono_rules : user_attribute := { name := `mono_rules, descr := "lemmas usable to prove monotonicity" } attribute [mono_rules] add_le_add mul_le_mul_of_nonneg_right lemma my_test {a b c d e : real} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := -- any of the following lines solve the goal: add_le_add (add_le_add (add_le_add (add_le_add h1 (mul_le_mul_of_nonneg_right h2 h3)) h1 ) h2) h3 by apply_rules [add_le_add, mul_le_mul_of_nonneg_right] by apply_rules [mono_rules] by apply_rules mono_rules ``` -/ meta def apply_rules (hs : parse pexpr_list_or_texpr) (n : nat := 50) (opt : apply_cfg := {}) : tactic unit := tactic.apply_rules hs n opt add_tactic_doc { name := "apply_rules", category := doc_category.tactic, decl_names := [`tactic.interactive.apply_rules], tags := ["lemma application"] } meta def return_cast (f : option expr) (t : option (expr × expr)) (es : list (expr × expr × expr)) (e x x' eq_h : expr) : tactic (option (expr × expr) × list (expr × expr × expr)) := (do guard (¬ e.has_var), unify x x', u ← mk_meta_univ, f ← f <|> mk_mapp ``_root_.id [(expr.sort u : expr)], t' ← infer_type e, some (f',t) ← pure t | return (some (f,t'), (e,x',eq_h) :: es), infer_type e >>= is_def_eq t, unify f f', return (some (f,t), (e,x',eq_h) :: es)) <|> return (t, es) meta def list_cast_of_aux (x : expr) (t : option (expr × expr)) (es : list (expr × expr × expr)) : expr → tactic (option (expr × expr) × list (expr × expr × expr)) | e@`(cast %%eq_h %%x') := return_cast none t es e x x' eq_h | e@`(eq.mp %%eq_h %%x') := return_cast none t es e x x' eq_h | e@`(eq.mpr %%eq_h %%x') := mk_eq_symm eq_h >>= return_cast none t es e x x' | e@`(@eq.subst %%α %%p %%a %%b %%eq_h %%x') := return_cast p t es e x x' eq_h | e@`(@eq.substr %%α %%p %%a %%b %%eq_h %%x') := mk_eq_symm eq_h >>= return_cast p t es e x x' | e@`(@eq.rec %%α %%a %%f %%x' _ %%eq_h) := return_cast f t es e x x' eq_h | e@`(@eq.rec_on %%α %%a %%f %%b %%eq_h %%x') := return_cast f t es e x x' eq_h | e := return (t,es) meta def list_cast_of (x tgt : expr) : tactic (list (expr × expr × expr)) := (list.reverse ∘ prod.snd) <$> tgt.mfold (none, []) (λ e i es, list_cast_of_aux x es.1 es.2 e) private meta def h_generalize_arg_p_aux : pexpr → parser (pexpr × name) | (app (app (macro _ [const `heq _ ]) h) (local_const x _ _ _)) := pure (h, x) | _ := fail "parse error" private meta def h_generalize_arg_p : parser (pexpr × name) := with_desc "expr == id" $ parser.pexpr 0 >>= h_generalize_arg_p_aux /-- `h_generalize Hx : e == x` matches on `cast _ e` in the goal and replaces it with `x`. It also adds `Hx : e == x` as an assumption. If `cast _ e` appears multiple times (not necessarily with the same proof), they are all replaced by `x`. `cast` `eq.mp`, `eq.mpr`, `eq.subst`, `eq.substr`, `eq.rec` and `eq.rec_on` are all treated as casts. - `h_generalize Hx : e == x with h` adds hypothesis `α = β` with `e : α, x : β`; - `h_generalize Hx : e == x with _` chooses automatically chooses the name of assumption `α = β`; - `h_generalize! Hx : e == x` reverts `Hx`; - when `Hx` is omitted, assumption `Hx : e == x` is not added. -/ meta def h_generalize (rev : parse (tk "!")?) (h : parse ident_?) (_ : parse (tk ":")) (arg : parse h_generalize_arg_p) (eqs_h : parse ( (tk "with" >> pure <$> ident_) <|> pure [])) : tactic unit := do let (e,n) := arg, let h' := if h = `_ then none else h, h' ← (h' : tactic name) <|> get_unused_name ("h" ++ n.to_string : string), e ← to_expr e, tgt ← target, ((e,x,eq_h)::es) ← list_cast_of e tgt | fail "no cast found", interactive.generalize h' () (to_pexpr e, n), asm ← get_local h', v ← get_local n, hs ← es.mmap (λ ⟨e,_⟩, mk_app `eq [e,v]), (eqs_h.zip [e]).mmap' (λ ⟨h,e⟩, do h ← if h ≠ `_ then pure h else get_unused_name `h, () <$ note h none eq_h ), hs.mmap' (λ h, do h' ← assert `h h, tactic.exact asm, try (rewrite_target h'), tactic.clear h' ), when h.is_some (do (to_expr ``(heq_of_eq_rec_left %%eq_h %%asm) <|> to_expr ``(heq_of_cast_eq %%eq_h %%asm)) >>= note h' none >> pure ()), tactic.clear asm, when rev.is_some (interactive.revert [n]) add_tactic_doc { name := "h_generalize", category := doc_category.tactic, decl_names := [`tactic.interactive.h_generalize], tags := ["context management"] } /-- Tests whether `t` is definitionally equal to `p`. The difference with `guard_expr_eq` is that this uses definitional equality instead of alpha-equivalence. -/ meta def guard_expr_eq' (t : expr) (p : parse $ tk ":=" *> texpr) : tactic unit := do e ← to_expr p, is_def_eq t e /-- `guard_target' t` fails if the target of the main goal is not definitionally equal to `t`. We use this tactic for writing tests. The difference with `guard_target` is that this uses definitional equality instead of alpha-equivalence. -/ meta def guard_target' (p : parse texpr) : tactic unit := do t ← target, guard_expr_eq' t p add_tactic_doc { name := "guard_target'", category := doc_category.tactic, decl_names := [`tactic.interactive.guard_target'], tags := ["testing"] } /-- Tries to solve the goal using a canonical proof of `true` or the `reflexivity` tactic. Unlike `trivial` or `trivial'`, does not the `contradiction` tactic. -/ meta def triv : tactic unit := tactic.triv <|> tactic.reflexivity <|> fail "triv tactic failed" add_tactic_doc { name := "triv", category := doc_category.tactic, decl_names := [`tactic.interactive.triv], tags := ["finishing"] } /-- A weaker version of `trivial` that tries to solve the goal using a canonical proof of `true` or the `reflexivity` tactic (unfolding only `reducible` constants, so can fail faster than `trivial`), and otherwise tries the `contradiction` tactic. -/ meta def trivial' : tactic unit := tactic.triv' <|> tactic.reflexivity reducible <|> tactic.contradiction <|> fail "trivial' tactic failed" add_tactic_doc { name := "trivial'", category := doc_category.tactic, decl_names := [`tactic.interactive.trivial'], tags := ["finishing"] } /-- Similar to `existsi`. `use x` will instantiate the first term of an `∃` or `Σ` goal with `x`. It will then try to close the new goal using `trivial'`, or try to simplify it by applying `exists_prop`. Unlike `existsi`, `x` is elaborated with respect to the expected type. `use` will alternatively take a list of terms `[x0, ..., xn]`. `use` will work with constructors of arbitrary inductive types. Examples: ```lean example (α : Type) : ∃ S : set α, S = S := by use ∅ example : ∃ x : ℤ, x = x := by use 42 example : ∃ n > 0, n = n := begin use 1, -- goal is now 1 > 0 ∧ 1 = 1, whereas it would be ∃ (H : 1 > 0), 1 = 1 after existsi 1. exact ⟨zero_lt_one, rfl⟩, end example : ∃ a b c : ℤ, a + b + c = 6 := by use [1, 2, 3] example : ∃ p : ℤ × ℤ, p.1 = 1 := by use ⟨1, 42⟩ example : Σ x y : ℤ, (ℤ × ℤ) × ℤ := by use [1, 2, 3, 4, 5] inductive foo | mk : ℕ → bool × ℕ → ℕ → foo example : foo := by use [100, tt, 4, 3] ``` -/ meta def use (l : parse pexpr_list_or_texpr) : tactic unit := focus1 $ tactic.use l; try (trivial' <|> (do `(Exists %%p) ← target, to_expr ``(exists_prop.mpr) >>= tactic.apply >> skip)) add_tactic_doc { name := "use", category := doc_category.tactic, decl_names := [`tactic.interactive.use, `tactic.interactive.existsi], tags := ["logic"], inherit_description_from := `tactic.interactive.use } /-- `clear_aux_decl` clears every `aux_decl` in the local context for the current goal. This includes the induction hypothesis when using the equation compiler and `_let_match` and `_fun_match`. It is useful when using a tactic such as `finish`, `simp *` or `subst` that may use these auxiliary declarations, and produce an error saying the recursion is not well founded. ```lean example (n m : ℕ) (h₁ : n = m) (h₂ : ∃ a : ℕ, a = n ∧ a = m) : 2 * m = 2 * n := let ⟨a, ha⟩ := h₂ in begin clear_aux_decl, -- subst will fail without this line subst h₁ end example (x y : ℕ) (h₁ : ∃ n : ℕ, n * 1 = 2) (h₂ : 1 + 1 = 2 → x * 1 = y) : x = y := let ⟨n, hn⟩ := h₁ in begin clear_aux_decl, -- finish produces an error without this line finish end ``` -/ meta def clear_aux_decl : tactic unit := tactic.clear_aux_decl add_tactic_doc { name := "clear_aux_decl", category := doc_category.tactic, decl_names := [`tactic.interactive.clear_aux_decl, `tactic.clear_aux_decl], tags := ["context management"], inherit_description_from := `tactic.interactive.clear_aux_decl } meta def loc.get_local_pp_names : loc → tactic (list name) | loc.wildcard := list.map expr.local_pp_name <$> local_context | (loc.ns l) := return l.reduce_option meta def loc.get_local_uniq_names (l : loc) : tactic (list name) := list.map expr.local_uniq_name <$> l.get_locals /-- The logic of `change x with y at l` fails when there are dependencies. `change'` mimics the behavior of `change`, except in the case of `change x with y at l`. In this case, it will correctly replace occurences of `x` with `y` at all possible hypotheses in `l`. As long as `x` and `y` are defeq, it should never fail. -/ meta def change' (q : parse texpr) : parse (tk "with" *> texpr)? → parse location → tactic unit | none (loc.ns [none]) := do e ← i_to_expr q, change_core e none | none (loc.ns [some h]) := do eq ← i_to_expr q, eh ← get_local h, change_core eq (some eh) | none _ := fail "change-at does not support multiple locations" | (some w) l := do l' ← loc.get_local_pp_names l, l'.mmap' (λ e, try (change_with_at q w e)), when l.include_goal $ change q w (loc.ns [none]) add_tactic_doc { name := "change'", category := doc_category.tactic, decl_names := [`tactic.interactive.change', `tactic.interactive.change], tags := ["renaming"], inherit_description_from := `tactic.interactive.change' } private meta def opt_dir_with : parser (option (bool × name)) := (do tk "with", arrow ← (tk "<-")?, h ← ident, return (arrow.is_some, h)) <|> return none /-- `set a := t with h` is a variant of `let a := t`. It adds the hypothesis `h : a = t` to the local context and replaces `t` with `a` everywhere it can. `set a := t with ←h` will add `h : t = a` instead. `set! a := t with h` does not do any replacing. ```lean example (x : ℕ) (h : x = 3) : x + x + x = 9 := begin set y := x with ←h_xy, /- x : ℕ, y : ℕ := x, h_xy : x = y, h : y = 3 ⊢ y + y + y = 9 -/ end ``` -/ meta def set (h_simp : parse (tk "!")?) (a : parse ident) (tp : parse ((tk ":") >> texpr)?) (_ : parse (tk ":=")) (pv : parse texpr) (rev_name : parse opt_dir_with) := do tp ← i_to_expr $ tp.get_or_else pexpr.mk_placeholder, pv ← to_expr ``(%%pv : %%tp), tp ← instantiate_mvars tp, definev a tp pv, when h_simp.is_none $ change' ``(%%pv) (some (expr.const a [])) $ interactive.loc.wildcard, match rev_name with | some (flip, id) := do nv ← get_local a, mk_app `eq (cond flip [pv, nv] [nv, pv]) >>= assert id, reflexivity | none := skip end add_tactic_doc { name := "set", category := doc_category.tactic, decl_names := [`tactic.interactive.set], tags := ["context management"] } /-- `clear_except h₀ h₁` deletes all the assumptions it can except for `h₀` and `h₁`. -/ meta def clear_except (xs : parse ident *) : tactic unit := do n ← xs.mmap (try_core ∘ get_local) >>= revert_lst ∘ list.filter_map id, ls ← local_context, ls.reverse.mmap' $ try ∘ tactic.clear, intron_no_renames n add_tactic_doc { name := "clear_except", category := doc_category.tactic, decl_names := [`tactic.interactive.clear_except], tags := ["context management"] } meta def format_names (ns : list name) : format := format.join $ list.intersperse " " (ns.map to_fmt) private meta def indent_bindents (l r : string) : option (list name) → expr → tactic format | none e := do e ← pp e, pformat!"{l}{format.nest l.length e}{r}" | (some ns) e := do e ← pp e, let ns := format_names ns, let margin := l.length + ns.to_string.length + " : ".length, pformat!"{l}{ns} : {format.nest margin e}{r}" private meta def format_binders : list name × binder_info × expr → tactic format | (ns, binder_info.default, t) := indent_bindents "(" ")" ns t | (ns, binder_info.implicit, t) := indent_bindents "{" "}" ns t | (ns, binder_info.strict_implicit, t) := indent_bindents "⦃" "⦄" ns t | ([n], binder_info.inst_implicit, t) := if "_".is_prefix_of n.to_string then indent_bindents "[" "]" none t else indent_bindents "[" "]" [n] t | (ns, binder_info.inst_implicit, t) := indent_bindents "[" "]" ns t | (ns, binder_info.aux_decl, t) := indent_bindents "(" ")" ns t private meta def partition_vars' (s : name_set) : list expr → list expr → list expr → tactic (list expr × list expr) | [] as bs := pure (as.reverse, bs.reverse) | (x :: xs) as bs := do t ← infer_type x, if t.has_local_in s then partition_vars' xs as (x :: bs) else partition_vars' xs (x :: as) bs private meta def partition_vars : tactic (list expr × list expr) := do ls ← local_context, partition_vars' (name_set.of_list $ ls.map expr.local_uniq_name) ls [] [] /-- Format the current goal as a stand-alone example. Useful for testing tactics or creating [minimal working examples](https://leanprover-community.github.io/mwe.html). * `extract_goal`: formats the statement as an `example` declaration * `extract_goal my_decl`: formats the statement as a `lemma` or `def` declaration called `my_decl` * `extract_goal with i j k:` only use local constants `i`, `j`, `k` in the declaration Examples: ```lean example (i j k : ℕ) (h₀ : i ≤ j) (h₁ : j ≤ k) : i ≤ k := begin extract_goal, -- prints: -- example (i j k : ℕ) (h₀ : i ≤ j) (h₁ : j ≤ k) : i ≤ k := -- begin -- admit, -- end extract_goal my_lemma -- prints: -- lemma my_lemma (i j k : ℕ) (h₀ : i ≤ j) (h₁ : j ≤ k) : i ≤ k := -- begin -- admit, -- end end example {i j k x y z w p q r m n : ℕ} (h₀ : i ≤ j) (h₁ : j ≤ k) (h₁ : k ≤ p) (h₁ : p ≤ q) : i ≤ k := begin extract_goal my_lemma, -- prints: -- lemma my_lemma {i j k x y z w p q r m n : ℕ} -- (h₀ : i ≤ j) -- (h₁ : j ≤ k) -- (h₁ : k ≤ p) -- (h₁ : p ≤ q) : -- i ≤ k := -- begin -- admit, -- end extract_goal my_lemma with i j k -- prints: -- lemma my_lemma {p i j k : ℕ} -- (h₀ : i ≤ j) -- (h₁ : j ≤ k) -- (h₁ : k ≤ p) : -- i ≤ k := -- begin -- admit, -- end end example : true := begin let n := 0, have m : ℕ, admit, have k : fin n, admit, have : n + m + k.1 = 0, extract_goal, -- prints: -- example (m : ℕ) : let n : ℕ := 0 in ∀ (k : fin n), n + m + k.val = 0 := -- begin -- intros n k, -- admit, -- end end ``` -/ meta def extract_goal (print_use : parse $ tt <$ tk "!" <|> pure ff) (n : parse ident?) (vs : parse (tk "with" *> ident*)?) : tactic unit := do tgt ← target, solve_aux tgt $ do { ((cxt₀,cxt₁,ls,tgt),_) ← solve_aux tgt $ do { vs.mmap clear_except, ls ← local_context, ls ← ls.mfilter $ succeeds ∘ is_local_def, n ← revert_lst ls, (c₀,c₁) ← partition_vars, tgt ← target, ls ← intron' n, pure (c₀,c₁,ls,tgt) }, is_prop ← is_prop tgt, let title := match n, is_prop with | none, _ := to_fmt "example" | (some n), tt := format!"lemma {n}" | (some n), ff := format!"def {n}" end, cxt₀ ← compact_decl cxt₀ >>= list.mmap format_binders, cxt₁ ← compact_decl cxt₁ >>= list.mmap format_binders, stmt ← pformat!"{tgt} :=", let fmt := format.group $ format.nest 2 $ title ++ cxt₀.foldl (λ acc x, acc ++ format.group (format.line ++ x)) "" ++ format.join (list.map (λ x, format.line ++ x) cxt₁) ++ " :" ++ format.line ++ stmt, trace $ fmt.to_string $ options.mk.set_nat `pp.width 80, let var_names := format.intercalate " " $ ls.map (to_fmt ∘ local_pp_name), let call_intron := if ls.empty then to_fmt "" else format!"\n intros {var_names},", trace!"begin{call_intron}\n admit,\nend\n" }, skip add_tactic_doc { name := "extract_goal", category := doc_category.tactic, decl_names := [`tactic.interactive.extract_goal], tags := ["goal management", "proof extraction", "debugging"] } /-- `inhabit α` tries to derive a `nonempty α` instance and then upgrades this to an `inhabited α` instance. If the target is a `Prop`, this is done constructively; otherwise, it uses `classical.choice`. ```lean example (α) [nonempty α] : ∃ a : α, true := begin inhabit α, existsi default, trivial end ``` -/ meta def inhabit (t : parse parser.pexpr) (inst_name : parse ident?) : tactic unit := do ty ← i_to_expr t, nm ← returnopt inst_name <|> get_unused_name `inst, tgt ← target, tgt_is_prop ← is_prop tgt, if tgt_is_prop then do decorate_error "could not infer nonempty instance:" $ mk_mapp ``nonempty.elim_to_inhabited [ty, none, tgt] >>= tactic.apply, introI nm else do decorate_error "could not infer nonempty instance:" $ mk_mapp ``classical.inhabited_of_nonempty' [ty, none] >>= note nm none, resetI add_tactic_doc { name := "inhabit", category := doc_category.tactic, decl_names := [`tactic.interactive.inhabit], tags := ["context management", "type class"] } /-- `revert_deps n₁ n₂ ...` reverts all the hypotheses that depend on one of `n₁, n₂, ...` It does not revert `n₁, n₂, ...` themselves (unless they depend on another `nᵢ`). -/ meta def revert_deps (ns : parse ident*) : tactic unit := propagate_tags $ ns.mmap get_local >>= revert_reverse_dependencies_of_hyps >> skip add_tactic_doc { name := "revert_deps", category := doc_category.tactic, decl_names := [`tactic.interactive.revert_deps], tags := ["context management", "goal management"] } /-- `revert_after n` reverts all the hypotheses after `n`. -/ meta def revert_after (n : parse ident) : tactic unit := propagate_tags $ get_local n >>= tactic.revert_after >> skip add_tactic_doc { name := "revert_after", category := doc_category.tactic, decl_names := [`tactic.interactive.revert_after], tags := ["context management", "goal management"] } /-- Reverts all local constants on which the target depends (recursively). -/ meta def revert_target_deps : tactic unit := propagate_tags $ tactic.revert_target_deps >> skip add_tactic_doc { name := "revert_target_deps", category := doc_category.tactic, decl_names := [`tactic.interactive.revert_target_deps], tags := ["context management", "goal management"] } /-- `clear_value n₁ n₂ ...` clears the bodies of the local definitions `n₁, n₂ ...`, changing them into regular hypotheses. A hypothesis `n : α := t` is changed to `n : α`. -/ meta def clear_value (ns : parse ident*) : tactic unit := propagate_tags $ ns.reverse.mmap get_local >>= tactic.clear_value add_tactic_doc { name := "clear_value", category := doc_category.tactic, decl_names := [`tactic.interactive.clear_value], tags := ["context management"] } /-- `generalize' : e = x` replaces all occurrences of `e` in the target with a new hypothesis `x` of the same type. `generalize' h : e = x` in addition registers the hypothesis `h : e = x`. `generalize'` is similar to `generalize`. The difference is that `generalize' : e = x` also succeeds when `e` does not occur in the goal. It is similar to `set`, but the resulting hypothesis `x` is not a local definition. -/ meta def generalize' (h : parse ident?) (_ : parse $ tk ":") (p : parse generalize_arg_p) : tactic unit := propagate_tags $ do let (p, x) := p, e ← i_to_expr p, some h ← pure h | tactic.generalize' e x >> skip, -- `h` is given, the regular implementation of `generalize` works. tgt ← target, tgt' ← do { ⟨tgt', _⟩ ← solve_aux tgt (tactic.generalize e x >> target), to_expr ``(Π x, %%e = x → %%(tgt'.binding_body.lift_vars 0 1)) } <|> to_expr ``(Π x, %%e = x → %%tgt), t ← assert h tgt', swap, exact ``(%%t %%e rfl), intro x, intro h add_tactic_doc { name := "generalize'", category := doc_category.tactic, decl_names := [`tactic.interactive.generalize'], tags := ["context management"] } /-- If the expression `q` is a local variable with type `x = t` or `t = x`, where `x` is a local constant, `tactic.interactive.subst' q` substitutes `x` by `t` everywhere in the main goal and then clears `q`. If `q` is another local variable, then we find a local constant with type `q = t` or `t = q` and substitute `t` for `q`. Like `tactic.interactive.subst`, but fails with a nicer error message if the substituted variable is a local definition. It is trickier to fix this in core, since `tactic.is_local_def` is in mathlib. -/ meta def subst' (q : parse texpr) : tactic unit := do i_to_expr q >>= tactic.subst' >> try (tactic.reflexivity reducible) add_tactic_doc { name := "subst'", category := doc_category.tactic, decl_names := [`tactic.interactive.subst'], tags := ["context management"] } end interactive end tactic
122d4fd2fed7fad64dc764af9f4faf61a889c86b
618003631150032a5676f229d13a079ac875ff77
/src/tactic/omega/nat/sub_elim.lean
92002af7200595f340f7546b52ff94a4d38b804d
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
5,747
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek Subtraction elimination for linear natural number arithmetic. Works by repeatedly rewriting goals of the preform `P[t-s]` into `P[x] ∧ (t = s + x ∨ (t ≤ s ∧ x = 0))`, where `x` is fresh. -/ import tactic.omega.nat.form namespace omega namespace nat open_locale omega.nat namespace preterm /-- Find subtraction inside preterm and return its operands -/ def sub_terms : preterm → option (preterm × preterm) | (& i) := none | (i ** n) := none | (t +* s) := t.sub_terms <|> s.sub_terms | (t -* s) := t.sub_terms <|> s.sub_terms <|> some (t,s) /-- Find (t - s) inside a preterm and replace it with variable k -/ def sub_subst (t s : preterm) (k : nat) : preterm → preterm | t@(& m) := t | t@(m ** n) := t | (x +* y) := x.sub_subst +* y.sub_subst | (x -* y) := if x = t ∧ y = s then (1 ** k) else x.sub_subst -* y.sub_subst lemma val_sub_subst {k : nat} {x y : preterm} {v : nat → nat} : ∀ {t : preterm}, t.fresh_index ≤ k → (sub_subst x y k t).val (update k (x.val v - y.val v) v) = t.val v | (& m) h1 := rfl | (m ** n) h1 := begin have h2 : n ≠ k := ne_of_lt h1, simp only [sub_subst, preterm.val], rw update_eq_of_ne _ h2, end | (t +* s) h1 := begin simp only [sub_subst, val_add], apply fun_mono_2; apply val_sub_subst (le_trans _ h1), apply le_max_left, apply le_max_right end | (t -* s) h1 := begin simp only [sub_subst, val_sub], by_cases h2 : t = x ∧ s = y, { rw if_pos h2, simp only [val_var, one_mul], rw [update_eq, h2.left, h2.right] }, { rw if_neg h2, simp only [val_sub, sub_subst], apply fun_mono_2; apply val_sub_subst (le_trans _ h1), apply le_max_left, apply le_max_right, } end end preterm namespace preform /-- Find subtraction inside preform and return its operands -/ def sub_terms : preform → option (preterm × preterm) | (t =* s) := t.sub_terms <|> s.sub_terms | (t ≤* s) := t.sub_terms <|> s.sub_terms | (¬* p) := p.sub_terms | (p ∨* q) := p.sub_terms <|> q.sub_terms | (p ∧* q) := p.sub_terms <|> q.sub_terms /-- Find (t - s) inside a preform and replace it with variable k -/ @[simp] def sub_subst (x y : preterm) (k : nat) : preform → preform | (t =* s) := preterm.sub_subst x y k t =* preterm.sub_subst x y k s | (t ≤* s) := preterm.sub_subst x y k t ≤* preterm.sub_subst x y k s | (¬* p) := ¬* p.sub_subst | (p ∨* q) := p.sub_subst ∨* q.sub_subst | (p ∧* q) := p.sub_subst ∧* q.sub_subst end preform /-- Preform which asserts that the value of variable k is the truncated difference between preterms t and s -/ def is_diff (t s : preterm) (k : nat) : preform := ((t =* (s +* (1 ** k))) ∨* (t ≤* s ∧* ((1 ** k) =* &0))) lemma holds_is_diff {t s : preterm} {k : nat} {v : nat → nat} : v k = t.val v - s.val v → (is_diff t s k).holds v := begin intro h1, simp only [preform.holds, is_diff, if_pos (eq.refl 1), preterm.val_add, preterm.val_var, preterm.val_const], by_cases h2 : t.val v ≤ s.val v, { right, refine ⟨h2,_⟩, rw [h1, one_mul, nat.sub_eq_zero_iff_le], exact h2 }, { left, rw [h1, one_mul, add_comm, nat.sub_add_cancel _], rw not_le at h2, apply le_of_lt h2 } end /-- Helper function for sub_elim -/ def sub_elim_core (t s : preterm) (k : nat) (p : preform) : preform := (preform.sub_subst t s k p) ∧* (is_diff t s k) /-- Return de Brujin index of fresh variable that does not occur in any of the arguments -/ def sub_fresh_index (t s : preterm) (p : preform) : nat := max p.fresh_index (max t.fresh_index s.fresh_index) /-- Return a new preform with all subtractions eliminated -/ def sub_elim (t s : preterm) (p : preform) : preform := sub_elim_core t s (sub_fresh_index t s p) p lemma sub_subst_equiv {k : nat} {x y : preterm} {v : nat → nat} : ∀ p : preform, p.fresh_index ≤ k → ((preform.sub_subst x y k p).holds (update k (x.val v - y.val v) v) ↔ (p.holds v)) | (t =* s) h1 := begin simp only [preform.holds, preform.sub_subst], apply pred_mono_2; apply preterm.val_sub_subst (le_trans _ h1), apply le_max_left, apply le_max_right end | (t ≤* s) h1 := begin simp only [preform.holds, preform.sub_subst], apply pred_mono_2; apply preterm.val_sub_subst (le_trans _ h1), apply le_max_left, apply le_max_right end | (¬* p) h1 := by { apply not_iff_not_of_iff, apply sub_subst_equiv p h1 } | (p ∨* q) h1 := begin simp only [preform.holds, preform.sub_subst], apply pred_mono_2; apply propext; apply sub_subst_equiv _ (le_trans _ h1), apply le_max_left, apply le_max_right end | (p ∧* q) h1 := begin simp only [preform.holds, preform.sub_subst], apply pred_mono_2; apply propext; apply sub_subst_equiv _ (le_trans _ h1), apply le_max_left, apply le_max_right end lemma sat_sub_elim {t s : preterm} {p : preform} : p.sat → (sub_elim t s p).sat := begin intro h1, simp only [sub_elim, sub_elim_core], cases h1 with v h1, refine ⟨update (sub_fresh_index t s p) (t.val v - s.val v) v, _⟩, constructor, { apply (sub_subst_equiv p _).elim_right h1, apply le_max_left }, { apply holds_is_diff, rw update_eq, apply fun_mono_2; apply preterm.val_constant; intros x h2; rw update_eq_of_ne _ (ne.symm (ne_of_gt _)); apply lt_of_lt_of_le h2; apply le_trans _ (le_max_right _ _), apply le_max_left, apply le_max_right } end lemma unsat_of_unsat_sub_elim (t s : preterm) (p : preform) : (sub_elim t s p).unsat → p.unsat := (@not_imp_not _ _ (classical.dec _)).elim_right sat_sub_elim end nat end omega
b7f8bf118f62edefe0f21c70f3eecad8117b42fc
761d983a78bc025071bac14a3facced881cf5e53
/affine/affine_transformation.lean
aeb45fa40c057259c18cf4b6a39d79c8585ec1e7
[]
no_license
rohanrajnair/affine_lib
bcf22ff892cf74ccb36a95bc9b7fff8e0adb2d0d
83076864245ac547b9d615bc6a23804b1b4a8f70
refs/heads/master
1,673,320,928,343
1,603,036,653,000
1,603,036,653,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
824
lean
import .aff_coord_space universes u v w variables (X : Type u) (K : Type v) (V : Type w) (n : ℕ) (id : ℕ) (k : K) [inhabited K] [field K] [add_comm_group V] [vector_space K V] [affine_space X K V] #check linear_map structure aff_auto := (f_v : V → V) (f_p : X → X) (linear : ∃ g : linear_map K V V, ∃ x : V, ∀ v : V, f_v v = g v + x) structure aff_coord_map := (f_v : aff_vec K n → aff_vec K n) (f_p : aff_pt K n → aff_pt K n) (linear_part : ∃ g : linear_map K (aff_vec K n) (aff_vec K n), ∀ v : aff_vec K n, ∃ x, f_v v = g v + x) structure aff_coord_map' := (to_fun : affine_space X K V → affine_space X K V) -- doesn't work. maps one prop to another. /- want to say f_v = f_p want to say f_v is linear up to some translation v = x - 0 f_v v = f_p x - 0 -/
7b1de3ea4e480544c5badaba4726c40ea7c3720e
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/choice_ctx.lean
8d76f2bc4e3880b816b643769d995870c87c06dd
[ "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
336
lean
namespace test open nat set_option pp.coercions true namespace foo theorem trans {a b c : nat} (H1 : a = b) (H2 : b = c) : a = c := eq.trans H1 H2 end foo open foo theorem tst (a b : nat) (H0 : b = a) (H : b = 0) : a = 0 := have H1 : a = b, from eq.symm H0, foo.trans H1 H definition f (a b : nat) := let x := 3 in a + x end test
153c676d729fa32f99d3b25a735d915f5cbf84a3
7cef822f3b952965621309e88eadf618da0c8ae9
/src/geometry/manifold/real_instances.lean
61a3d3f6808ed8475efa819fe30218121fac0c1f
[ "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
14,809
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 geometry.manifold.smooth_manifold_with_corners linear_algebra.finite_dimensional /-! # Constructing examples of manifolds over ℝ We introduce the necessary bits to be able to define manifolds modelled over ℝ^n, boundaryless or with boundary or with corners. As a concrete example, we construct explicitly the manifold with boundary structure on the real interval [x, y]. More specifically, we introduce * `euclidean_space n` for a model vector space of dimension `n`. * `model_with_corners ℝ (euclidean_space n) (euclidean_half_space n)` for the model space used to define `n`-dimensional real manifolds with boundary * `model_with_corners ℝ (euclidean_space n) (euclidean_quadrant n)` for the model space used to define `n`-dimensional real manifolds with corners ## Implementation notes The manifold structure on the interval [x, y] = Icc x y requires the assumption `x < y`. We introduce a dummy class `lt_class x y` for this, to make such an assumption available to typeclass search. This should hopefully not be necessary any more in Lean 4. -/ noncomputable theory open set /-- The space ℝ^n. Note that the name is slightly misleading, as we only need a normed space structure on ℝ^n, but the one we use here is the sup norm and not the euclidean one -- this is not a problem for the manifold applications, but should probably be refactored at some point. -/ def euclidean_space (n : ℕ) : Type := (fin n → ℝ) /-- The half-space in ℝ^n, used to model manifolds with boundary. We only define it when `1 ≤ n`, as the definition only makes sense in this case. -/ def euclidean_half_space (n : ℕ) [has_zero (fin n)] : Type := {x : euclidean_space n // 0 ≤ x 0} /-- The quadrant in ℝ^n, used to model manifolds with corners, made of all vectors with nonnegative coordinates. -/ def euclidean_quadrant (n : ℕ) : Type := {x : euclidean_space n // ∀i:fin n, 0 ≤ x i} section /- Register class instances for euclidean space and half-space and quadrant -/ local attribute [reducible] euclidean_space euclidean_half_space euclidean_quadrant variable {n : ℕ} -- short-circuit type class inference instance : vector_space ℝ (euclidean_space n) := by apply_instance instance : normed_group (euclidean_space n) := by apply_instance instance : normed_space ℝ (euclidean_space n) := by apply_instance instance [has_zero (fin n)] : topological_space (euclidean_half_space n) := by apply_instance instance : topological_space (euclidean_quadrant n) := by apply_instance instance : finite_dimensional ℝ (euclidean_space n) := by apply_instance @[simp] lemma findim_euclidean_space : finite_dimensional.findim ℝ (euclidean_space n) = n := by simp lemma range_half_space (n : ℕ) [has_zero (fin n)] : range (λx : euclidean_half_space n, x.val) = {y | 0 ≤ y 0 } := by simp lemma range_quadrant (n : ℕ) : range (λx : euclidean_quadrant n, x.val) = {y | ∀i:fin n, 0 ≤ y i } := by simp end /-- Definition of the model with corners (euclidean_space n, euclidean_half_space n), used as a model for manifolds with boundary. -/ def model_with_corners_euclidean_half_space (n : ℕ) [has_zero (fin n)] : model_with_corners ℝ (euclidean_space n) (euclidean_half_space n) := { to_fun := λx, x.val, inv_fun := λx, ⟨λi, if h : i = 0 then max (x i) 0 else x i, by simp [le_refl]⟩, source := univ, target := range (λx : euclidean_half_space n, x.val), map_source := λx hx, by simpa [-mem_range, mem_range_self] using x.property, map_target := λx hx, mem_univ _, left_inv := λ⟨xval, xprop⟩ hx, begin rw subtype.mk_eq_mk, ext1 i, by_cases hi : i = 0; simp [hi, xprop] end, right_inv := λx hx, begin simp [range_half_space] at hx, ext1 i, by_cases hi : i = 0; simp [hi, hx] end, source_eq := rfl, unique_diff := begin /- To check that the half-space has the unique differentiability property, we use the criterion `unique_diff_on_convex`: it suffices to check that it is convex and with nonempty interior. -/ rw range_half_space, apply unique_diff_on_convex, show convex {y : euclidean_space n | 0 ≤ y 0}, { assume x y a b hx hy ha hb hab, simpa using add_le_add (mul_nonneg' ha hx) (mul_nonneg' hb hy) }, show interior {y : euclidean_space n | 0 ≤ y 0} ≠ ∅, { rw ne_empty_iff_exists_mem, use (λi, 1), rw mem_interior, refine ⟨(pi (univ : set (fin n)) (λi, (Ioi 0 : set ℝ))), _, is_open_set_pi finite_univ (λa ha, is_open_Ioi), _⟩, { assume x hx, simp [pi] at hx, exact le_of_lt (hx 0) }, { simp only [pi, forall_prop_of_true, mem_univ, mem_Ioi], assume i, exact zero_lt_one } } end, continuous_to_fun := continuous_subtype_val, continuous_inv_fun := begin apply continuous_subtype_mk, apply continuous_pi, assume i, by_cases h : i = 0, { simp only [h, dif_pos], have : continuous (λx:ℝ, max x 0) := continuous_id.max continuous_const, exact this.comp (continuous_apply 0) }, { simp [h], exact continuous_apply i } end } /-- Definition of the model with corners (euclidean_space n, euclidean_quadrant n), used as a model for manifolds with corners -/ def model_with_corners_euclidean_quadrant (n : ℕ) : model_with_corners ℝ (euclidean_space n) (euclidean_quadrant n) := { to_fun := λx, x.val, inv_fun := λx, ⟨λi, max (x i) 0, λi, by simp [le_refl]⟩, source := univ, target := range (λx : euclidean_quadrant n, x.val), map_source := λx hx, by simpa [-mem_range, mem_range_self] using x.property, map_target := λx hx, mem_univ _, left_inv := λ⟨xval, xprop⟩ hx, begin rw subtype.mk_eq_mk, ext1 i, simp [xprop i] end, right_inv := λx hx, begin rw range_quadrant at hx, ext1 i, simp [hx i] end, source_eq := rfl, unique_diff := begin /- To check that the quadrant has the unique differentiability property, we use the criterion `unique_diff_on_convex`: it suffices to check that it is convex and with nonempty interior. -/ rw range_quadrant, apply unique_diff_on_convex, show convex {y : euclidean_space n | ∀ (i : fin n), 0 ≤ y i}, { assume x y a b hx hy ha hb hab i, simpa using add_le_add (mul_nonneg' ha (hx i)) (mul_nonneg' hb (hy i)) }, show interior {y : euclidean_space n | ∀ (i : fin n), 0 ≤ y i} ≠ ∅, { rw ne_empty_iff_exists_mem, use (λi, 1), rw mem_interior, refine ⟨(pi (univ : set (fin n)) (λi, (Ioi 0 : set ℝ))), _, is_open_set_pi finite_univ (λa ha, is_open_Ioi), _⟩, { assume x hx i, simp [pi] at hx, exact le_of_lt (hx i) }, { simp only [pi, forall_prop_of_true, mem_univ, mem_Ioi], assume i, exact zero_lt_one } } end, continuous_to_fun := continuous_subtype_val, continuous_inv_fun := begin apply continuous_subtype_mk, apply continuous_pi, assume i, have : continuous (λx:ℝ, max x 0) := continuous.max continuous_id continuous_const, exact this.comp (continuous_apply i) end } /-- Dummy class to make an assumption such as `x < y` available to typeclass search. Such an assumption is used to define a manifold structure on [x, y] when `x < y`. Should be fixed in Lean 4. -/ def lt_class {α : Type*} [has_lt α] (x y : α) : Prop := x < y attribute [class] lt_class /-- The left chart for the topological space [x, y], defined on [x,y) and sending x to 0 in `euclidean_half_space 1`. -/ def Icc_left_chart (x y : ℝ) [h : lt_class x y] : local_homeomorph (Icc x y) (euclidean_half_space 1) := { source := {z : Icc x y | z.val < y}, target := {z : euclidean_half_space 1 | z.val 0 < y - x}, to_fun := λ(z : Icc x y), ⟨λi, z.val - x, sub_nonneg.mpr z.property.1⟩, inv_fun := λz, ⟨min (z.val 0 + x) y, by simp [le_refl, z.property, le_of_lt h]⟩, map_source := by simp, map_target := by { simp, assume z hz, left, linarith }, left_inv := by { rintros ⟨z, hz⟩ h'z, simp at hz h'z, simp [hz, h'z] }, right_inv := begin rintros ⟨z, hz⟩ h'z, rw subtype.mk_eq_mk, funext, simp at hz h'z, have A : x + z 0 ≤ y, by linarith, rw subsingleton.elim i 0, simp [A], end, open_source := begin have : is_open {z : ℝ | z < y} := is_open_Iio, exact continuous_subtype_val _ this end, open_target := begin have : is_open {z : ℝ | z < y - x} := is_open_Iio, have : is_open {z : fin 1 → ℝ | z 0 < y - x} := (continuous_apply 0) _ this, exact continuous_subtype_val _ this end, continuous_to_fun := begin apply continuous.continuous_on, apply continuous_subtype_mk, have : continuous (λ (z : ℝ) (i : fin 1), z - x) := continuous.sub (continuous_pi $ λi, continuous_id) continuous_const, exact this.comp continuous_subtype_val, end, continuous_inv_fun := begin apply continuous.continuous_on, apply continuous_subtype_mk, have A : continuous (λ z : ℝ, min (z + x) y) := (continuous_id.add continuous_const).min continuous_const, have B : continuous (λz : fin 1 → ℝ, z 0) := continuous_apply 0, exact (A.comp B).comp continuous_subtype_val end } /-- The right chart for the topological space [x, y], defined on (x,y] and sending y to 0 in `euclidean_half_space 1`. -/ def Icc_right_chart (x y : ℝ) [h : lt_class x y] : local_homeomorph (Icc x y) (euclidean_half_space 1) := { source := {z : Icc x y | x < z.val}, target := {z : euclidean_half_space 1 | z.val 0 < y - x}, to_fun := λ(z : Icc x y), ⟨λi, y - z.val, sub_nonneg.mpr z.property.2⟩, inv_fun := λz, ⟨max (y - z.val 0) x, by simp [le_refl, z.property, le_of_lt h]⟩, map_source := by simp, map_target := by { simp, assume z hz, left, linarith }, left_inv := by { rintros ⟨z, hz⟩ h'z, simp at hz h'z, simp [hz, h'z] }, right_inv := begin rintros ⟨z, hz⟩ h'z, rw subtype.mk_eq_mk, funext, simp at hz h'z, have A : x ≤ y + -z 0, by linarith, rw subsingleton.elim i 0, simp [A], end, open_source := begin have : is_open {z : ℝ | x < z} := is_open_Ioi, exact continuous_subtype_val _ this end, open_target := begin have : is_open {z : ℝ | z < y - x} := is_open_Iio, have : is_open {z : fin 1 → ℝ | z 0 < y - x} := (continuous_apply 0) _ this, exact continuous_subtype_val _ this end, continuous_to_fun := begin apply continuous.continuous_on, apply continuous_subtype_mk, have : continuous (λ (z : ℝ) (i : fin 1), y - z) := continuous_const.sub (continuous_pi (λi, continuous_id)), exact this.comp continuous_subtype_val, end, continuous_inv_fun := begin apply continuous.continuous_on, apply continuous_subtype_mk, have A : continuous (λ z : ℝ, max (y - z) x) := (continuous_const.sub continuous_id).max continuous_const, have B : continuous (λz : fin 1 → ℝ, z 0) := continuous_apply 0, exact (A.comp B).comp continuous_subtype_val end } /-- Manifold with boundary structure on [x, y], using only two charts. -/ instance Icc_manifold (x y : ℝ) [h : lt_class x y] : manifold (euclidean_half_space 1) (Icc x y) := { atlas := {Icc_left_chart x y, Icc_right_chart x y}, chart_at := λz, if z.val < y then Icc_left_chart x y else Icc_right_chart x y, mem_chart_source := λz, begin by_cases h' : z.val < y, { simp only [h', if_true], exact h' }, { simp only [h', if_false], apply lt_of_lt_of_le h, simpa using h' } end, chart_mem_atlas := λz, by { by_cases h' : z.val < y; simp [h'] } } /-- The manifold structure on [x, y] is smooth. -/ instance Icc_smooth_manifold (x y : ℝ) [lt_class x y] : smooth_manifold_with_corners (model_with_corners_euclidean_half_space 1) (Icc x y) := begin have M : times_cont_diff_on ℝ ⊤ (λz : fin 1 → ℝ, (λi : fin 1, y - x) - z) univ, { rw times_cont_diff_on_univ, exact times_cont_diff.sub times_cont_diff_const times_cont_diff_id }, haveI : has_groupoid (Icc x y) (times_cont_diff_groupoid ⊤ (model_with_corners_euclidean_half_space 1)) := begin apply has_groupoid_of_pregroupoid, assume e e' he he', simp [atlas] at he he', /- We need to check that any composition of two charts gives a C^∞ function. Each chart can be either the left chart or the right chart, leaving 4 possibilities that we handle successively. -/ rcases he with rfl | rfl; rcases he' with rfl | rfl, { -- e = right chart, e' = right chart refine ((mem_groupoid_of_pregroupoid _ _).mpr _).1, exact symm_trans_mem_times_cont_diff_groupoid _ _ _ }, { -- e = right chart, e' = left chart apply M.congr_mono _ _ (subset_univ _), { rw inter_comm, refine unique_diff_on.inter (model_with_corners.unique_diff _) _, exact model_with_corners.continuous_inv_fun _ _ (local_homeomorph.open_source _) }, { assume z hz, simp [-mem_range, range_half_space, model_with_corners_euclidean_half_space, local_equiv.trans_source, Icc_left_chart, Icc_right_chart] at hz, have A : 0 ≤ z 0 := hz.2, have B : x ≤ y + -z 0, by { have := hz.1.1.1, linarith }, have C : y + (-x + -z 0) = ((λ (i : fin 1), y + -x) + -z) 0, by { change y + (-x - z 0) = (y - x) - z 0, ring }, ext i, rw subsingleton.elim i 0, simpa [model_with_corners_euclidean_half_space, Icc_left_chart, Icc_right_chart, A, B] } }, { -- e = left chart, e' = right chart apply M.congr_mono _ _ (subset_univ _), { rw inter_comm, refine unique_diff_on.inter (model_with_corners.unique_diff _) _, exact model_with_corners.continuous_inv_fun _ _ (local_homeomorph.open_source _) }, { assume z hz, simp [-mem_range, range_half_space, model_with_corners_euclidean_half_space, local_equiv.trans_source, Icc_left_chart, Icc_right_chart] at hz, have A : 0 ≤ z 0 := hz.2, have B : x + z 0 ≤ y, by { have := hz.1.1.1, linarith }, have C : y + (-x + -z 0) = ((λ (i : fin 1), y + -x) + -z) 0, by { change y + (-x - z 0) = (y - x) - z 0, ring }, ext i, rw subsingleton.elim i 0, simpa [model_with_corners_euclidean_half_space, Icc_left_chart, Icc_right_chart, A, B] } }, { -- e = left chart, e' = left chart refine ((mem_groupoid_of_pregroupoid _ _).mpr _).1, exact symm_trans_mem_times_cont_diff_groupoid _ _ _ } end, constructor end
254ce576784dadadaa8a408bc7a84797aeb847e4
92b50235facfbc08dfe7f334827d47281471333b
/library/data/fintype/basic.lean
8375f9309aac97c83f6e60b6c92cc88a6c1cd93e
[ "Apache-2.0" ]
permissive
htzh/lean
24f6ed7510ab637379ec31af406d12584d31792c
d70c79f4e30aafecdfc4a60b5d3512199200ab6e
refs/heads/master
1,607,677,731,270
1,437,089,952,000
1,437,089,952,000
37,078,816
0
0
null
1,433,780,956,000
1,433,780,955,000
null
UTF-8
Lean
false
false
9,180
lean
/- Copyright (c) 2015 Leonardo de Moura. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura Finite type (type class). -/ import data.list.perm data.list.as_type data.bool data.equiv open list bool unit decidable option function structure fintype [class] (A : Type) : Type := (elems : list A) (unique : nodup elems) (complete : ∀ a, a ∈ elems) definition elements_of (A : Type) [h : fintype A] : list A := @fintype.elems A h section open equiv definition fintype_of_equiv {A B : Type} [h : fintype A] : A ≃ B → fintype B | (mk f g l r) := fintype.mk (map f (elements_of A)) (nodup_map (injective_of_left_inverse l) !fintype.unique) (λ b, have h₁ : g b ∈ elements_of A, from fintype.complete (g b), assert h₂ : f (g b) ∈ map f (elements_of A), from mem_map f h₁, by rewrite r at h₂; exact h₂) end definition fintype_unit [instance] : fintype unit := fintype.mk [star] dec_trivial (λ u, match u with star := dec_trivial end) definition fintype_bool [instance] : fintype bool := fintype.mk [ff, tt] dec_trivial (λ b, match b with | tt := dec_trivial | ff := dec_trivial end) definition fintype_product [instance] {A B : Type} : Π [h₁ : fintype A] [h₂ : fintype B], fintype (A × B) | (fintype.mk e₁ u₁ c₁) (fintype.mk e₂ u₂ c₂) := fintype.mk (product e₁ e₂) (nodup_product u₁ u₂) (λ p, match p with (a, b) := mem_product (c₁ a) (c₂ b) end) /- auxiliary function for finding 'a' s.t. f a ≠ g a -/ section find_discr variables {A B : Type} variable [h : decidable_eq B] include h definition find_discr (f g : A → B) : list A → option A | [] := none | (a::l) := if f a = g a then find_discr l else some a theorem find_discr_nil (f g : A → B) : find_discr f g [] = none := rfl theorem find_discr_cons_of_ne {f g : A → B} {a : A} (l : list A) : f a ≠ g a → find_discr f g (a::l) = some a := assume ne, if_neg ne theorem find_discr_cons_of_eq {f g : A → B} {a : A} (l : list A) : f a = g a → find_discr f g (a::l) = find_discr f g l := assume eq, if_pos eq theorem ne_of_find_discr_eq_some {f g : A → B} {a : A} : ∀ {l}, find_discr f g l = some a → f a ≠ g a | [] e := by contradiction | (x::l) e := by_cases (λ h : f x = g x, have aux : find_discr f g l = some a, by rewrite [find_discr_cons_of_eq l h at e]; exact e, ne_of_find_discr_eq_some aux) (λ h : f x ≠ g x, have aux : some x = some a, by rewrite [find_discr_cons_of_ne l h at e]; exact e, option.no_confusion aux (λ xeqa : x = a, eq.rec_on xeqa h)) theorem all_eq_of_find_discr_eq_none {f g : A → B} : ∀ {l}, find_discr f g l = none → ∀ a, a ∈ l → f a = g a | [] e a i := absurd i !not_mem_nil | (x::l) e a i := by_cases (λ fx_eq_gx : f x = g x, have aux : find_discr f g l = none, by rewrite [find_discr_cons_of_eq l fx_eq_gx at e]; exact e, or.elim (eq_or_mem_of_mem_cons i) (λ aeqx : a = x, by rewrite [-aeqx at fx_eq_gx]; exact fx_eq_gx) (λ ainl : a ∈ l, all_eq_of_find_discr_eq_none aux a ainl)) (λ fx_ne_gx : f x ≠ g x, by rewrite [find_discr_cons_of_ne l fx_ne_gx at e]; contradiction) end find_discr definition decidable_eq_fun [instance] {A B : Type} [h₁ : fintype A] [h₂ : decidable_eq B] : decidable_eq (A → B) := λ f g, match h₁ with | fintype.mk e u c := match find_discr f g e with | some a := λ h : find_discr f g e = some a, inr (λ f_eq_g : f = g, absurd (by rewrite f_eq_g; reflexivity) (ne_of_find_discr_eq_some h)) | none := λ h : find_discr f g e = none, inl (show f = g, from funext (λ a : A, all_eq_of_find_discr_eq_none h a (c a))) end rfl end section check_pred variables {A : Type} definition check_pred (p : A → Prop) [h : decidable_pred p] : list A → bool | [] := tt | (a::l) := if p a then check_pred l else ff theorem check_pred_cons_of_pos {p : A → Prop} [h : decidable_pred p] {a : A} (l : list A) : p a → check_pred p (a::l) = check_pred p l := assume pa, if_pos pa theorem check_pred_cons_of_neg {p : A → Prop} [h : decidable_pred p] {a : A} (l : list A) : ¬ p a → check_pred p (a::l) = ff := assume npa, if_neg npa theorem all_of_check_pred_eq_tt {p : A → Prop} [h : decidable_pred p] : ∀ {l : list A}, check_pred p l = tt → ∀ {a}, a ∈ l → p a | [] eqtt a ainl := absurd ainl !not_mem_nil | (b::l) eqtt a ainbl := by_cases (λ pb : p b, or.elim (eq_or_mem_of_mem_cons ainbl) (λ aeqb : a = b, by rewrite [aeqb]; exact pb) (λ ainl : a ∈ l, have eqtt₁ : check_pred p l = tt, by rewrite [check_pred_cons_of_pos _ pb at eqtt]; exact eqtt, all_of_check_pred_eq_tt eqtt₁ ainl)) (λ npb : ¬ p b, by rewrite [check_pred_cons_of_neg _ npb at eqtt]; exact (bool.no_confusion eqtt)) theorem ex_of_check_pred_eq_ff {p : A → Prop} [h : decidable_pred p] : ∀ {l : list A}, check_pred p l = ff → ∃ w, ¬ p w | [] eqtt := bool.no_confusion eqtt | (a::l) eqtt := by_cases (λ pa : p a, have eqtt₁ : check_pred p l = ff, by rewrite [check_pred_cons_of_pos _ pa at eqtt]; exact eqtt, ex_of_check_pred_eq_ff eqtt₁) (λ npa : ¬ p a, exists.intro a npa) end check_pred definition decidable_forall_finite [instance] {A : Type} {p : A → Prop} [h₁ : fintype A] [h₂ : decidable_pred p] : decidable (∀ x : A, p x) := match h₁ with | fintype.mk e u c := match check_pred p e with | tt := λ h : check_pred p e = tt, inl (λ a : A, all_of_check_pred_eq_tt h (c a)) | ff := λ h : check_pred p e = ff, inr (λ n : (∀ x, p x), obtain (a : A) (w : ¬ p a), from ex_of_check_pred_eq_ff h, absurd (n a) w) end rfl end definition decidable_exists_finite [instance] {A : Type} {p : A → Prop} [h₁ : fintype A] [h₂ : decidable_pred p] : decidable (∃ x : A, p x) := match h₁ with | fintype.mk e u c := match check_pred (λ a, ¬ p a) e with | tt := λ h : check_pred (λ a, ¬ p a) e = tt, inr (λ ex : (∃ x, p x), obtain x px, from ex, absurd px (all_of_check_pred_eq_tt h (c x))) | ff := λ h : check_pred (λ a, ¬ p a) e = ff, inl ( assert aux₁ : ∃ x, ¬¬p x, from ex_of_check_pred_eq_ff h, obtain x nnpx, from aux₁, exists.intro x (not_not_elim nnpx)) end rfl end open list.as_type -- Auxiliary function for returning a list with all elements of the type: (list.as_type l) -- Remark ⟪s⟫ is notation for (list.as_type l) -- We use this function to define the instance for (fintype ⟪s⟫) private definition ltype_elems {A : Type} {s : list A} : Π {l : list A}, l ⊆ s → list ⟪s⟫ | [] h := [] | (a::l) h := lval a (h a !mem_cons) :: ltype_elems (sub_of_cons_sub h) private theorem mem_of_mem_ltype_elems {A : Type} {a : A} {s : list A} : Π {l : list A} {h : l ⊆ s} {m : a ∈ s}, mk a m ∈ ltype_elems h → a ∈ l | [] h m lin := absurd lin !not_mem_nil | (b::l) h m lin := or.elim (eq_or_mem_of_mem_cons lin) (λ leq : mk a m = mk b (h b (mem_cons b l)), as_type.no_confusion leq (λ aeqb em, by rewrite [aeqb]; exact !mem_cons)) (λ linl : mk a m ∈ ltype_elems (sub_of_cons_sub h), have ainl : a ∈ l, from mem_of_mem_ltype_elems linl, mem_cons_of_mem _ ainl) private theorem nodup_ltype_elems {A : Type} {s : list A} : Π {l : list A} (d : nodup l) (h : l ⊆ s), nodup (ltype_elems h) | [] d h := nodup_nil | (a::l) d h := have d₁ : nodup l, from nodup_of_nodup_cons d, have nainl : a ∉ l, from not_mem_of_nodup_cons d, let h₁ : l ⊆ s := sub_of_cons_sub h in have d₂ : nodup (ltype_elems h₁), from nodup_ltype_elems d₁ h₁, have nin : mk a (h a (mem_cons a l)) ∉ ltype_elems h₁, from assume ab, absurd (mem_of_mem_ltype_elems ab) nainl, nodup_cons nin d₂ private theorem mem_ltype_elems {A : Type} {s : list A} {a : ⟪s⟫} : Π {l : list A} (h : l ⊆ s), value a ∈ l → a ∈ ltype_elems h | [] h vainl := absurd vainl !not_mem_nil | (b::l) h vainbl := or.elim (eq_or_mem_of_mem_cons vainbl) (λ vaeqb : value a = b, begin revert vaeqb h, -- TODO(Leo): check why 'cases a with va, ma' produces an incorrect proof eapply as_type.cases_on a, intro va ma vaeqb, rewrite -vaeqb, intro h, apply mem_cons end) (λ vainl : value a ∈ l, have aux : a ∈ ltype_elems (sub_of_cons_sub h), from mem_ltype_elems (sub_of_cons_sub h) vainl, mem_cons_of_mem _ aux) definition fintype_list_as_type [instance] {A : Type} [h : decidable_eq A] {s : list A} : fintype ⟪s⟫ := let nds : list A := erase_dup s in have sub₁ : nds ⊆ s, from erase_dup_sub s, have sub₂ : s ⊆ nds, from sub_erase_dup s, have dnds : nodup nds, from nodup_erase_dup s, let e : list ⟪s⟫ := ltype_elems sub₁ in fintype.mk e (nodup_ltype_elems dnds sub₁) (λ a : ⟪s⟫, show a ∈ e, from have vains : value a ∈ s, from is_member a, have vainnds : value a ∈ nds, from sub₂ vains, mem_ltype_elems sub₁ vainnds)
e32b81adbc33f7b2ccb811ed2bbc0df2ad32e478
9cb9db9d79fad57d80ca53543dc07efb7c4f3838
/src/facts/default.lean
8efd6a0203ab701a4b77317595a35ce4394ac871
[]
no_license
mr-infty/lean-liquid
3ff89d1f66244b434654c59bdbd6b77cb7de0109
a8db559073d2101173775ccbd85729d3a4f1ed4d
refs/heads/master
1,678,465,145,334
1,614,565,310,000
1,614,565,310,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
46
lean
import facts.nnreal import facts.normed_group
e8639056a12329ac961fe13657f4be04b085c9d9
5ae26df177f810c5006841e9c73dc56e01b978d7
/test/linarith.lean
3b50bc642edea248f792eef7ec6b2bc0ffeeee89
[ "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
4,296
lean
import tactic.linarith example (e b c a v0 v1 : ℚ) (h1 : v0 = 5*a) (h2 : v1 = 3*b) (h3 : v0 + v1 + c = 10) : v0 + 5 + (v1 - 3) + (c - 2) = 10 := by linarith example (ε : ℚ) (h1 : ε > 0) : ε / 2 + ε / 3 + ε / 7 < ε := by linarith example (x y z : ℚ) (h1 : 2*x < 3*y) (h2 : -4*x + z/2 < 0) (h3 : 12*y - z < 0) : false := by linarith example (ε : ℚ) (h1 : ε > 0) : ε / 2 < ε := by linarith example (ε : ℚ) (h1 : ε > 0) : ε / 3 + ε / 3 + ε / 3 = ε := by linarith example (a b c : ℚ) (h2 : b + 2 > 3 + b) : false := by linarith {discharger := `[ring SOP]} example (a b c : ℚ) (h2 : b + 2 > 3 + b) : false := by linarith example (a b c : ℚ) (x y : ℤ) (h1 : x ≤ 3*y) (h2 : b + 2 > 3 + b) : false := by linarith {restrict_type := ℚ} example (g v V c h : ℚ) (h1 : h = 0) (h2 : v = V) (h3 : V > 0) (h4 : g > 0) (h5 : 0 ≤ c) (h6 : c < 1) : v ≤ V := by linarith example (x y z : ℚ) (h1 : 2*x + ((-3)*y) < 0) (h2 : (-4)*x + 2*z < 0) (h3 : 12*y + (-4)* z < 0) (h4 : nat.prime 7) : false := by linarith example (x y z : ℚ) (h1 : 2*1*x + (3)*(y*(-1)) < 0) (h2 : (-2)*x*2 < -(z + z)) (h3 : 12*y + (-4)* z < 0) (h4 : nat.prime 7) : false := by linarith example (x y z : ℤ) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : 12*y - 4* z < 0) : false := by linarith example (x y z : ℤ) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : x*y < 5) (h3 : 12*y - 4* z < 0) : false := by linarith example (x y z : ℤ) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : x*y < 5) : ¬ 12*y - 4* z < 0 := by linarith example (w x y z : ℤ) (h1 : 4*x + (-3)*y + 6*w ≤ 0) (h2 : (-1)*x < 0) (h3 : y < 0) (h4 : w ≥ 0) (h5 : nat.prime x.nat_abs) : false := by linarith example (a b c : ℚ) (h1 : a > 0) (h2 : b > 5) (h3 : c < -10) (h4 : a + b - c < 3) : false := by linarith example (a b c : ℚ) (h2 : b > 0) (h3 : ¬ b ≥ 0) : false := by linarith example (a b c : ℚ) (h2 : (2 : ℚ) > 3) : a + b - c ≥ 3 := by linarith {exfalso := ff} example (x : ℚ) (hx : x > 0) (h : x.num < 0) : false := by linarith [rat.num_pos_iff_pos.mpr hx, h] example (x : ℚ) (hx : x > 0) (h : x.num < 0) : false := by linarith only [rat.num_pos_iff_pos.mpr hx, h] example (x y z : ℚ) (hx : x ≤ 3*y) (h2 : y ≤ 2*z) (h3 : x ≥ 6*z) : x = 3*y := by linarith example (x y z : ℕ) (hx : x ≤ 3*y) (h2 : y ≤ 2*z) (h3 : x ≥ 6*z) : x = 3*y := by linarith example (x y z : ℚ) (hx : ¬ x > 3*y) (h2 : ¬ y > 2*z) (h3 : x ≥ 6*z) : x = 3*y := by linarith example (h1 : (1 : ℕ) < 1) : false := by linarith example (a b c : ℚ) (h2 : b > 0) (h3 : b < 0) : nat.prime 10 := by linarith example (a b c : ℕ) : a + b ≥ a := by linarith example (a b c : ℕ) : ¬ a + b < a := by linarith example (x y : ℚ) (h : 6 + ((x + 4) * x + (6 + 3 * y) * y) = 3) (h' : (x + 4) * x ≥ 0) (h'' : (6 + 3 * y) * y ≥ 0) : false := by linarith example (x y : ℕ) (h : 6 + ((x + 4) * x + (6 + 3 * y) * y) = 3) : false := by linarith example (a b i : ℕ) (h1 : ¬ a < i) (h2 : b < i) (h3 : a ≤ b) : false := by linarith example (n : ℕ) (h1 : n ≤ 3) (h2 : n > 2) : n = 3 := by linarith example (z : ℕ) (hz : ¬ z ≥ 2) (h2 : ¬ z + 1 ≤ 2) : false := by linarith example (z : ℕ) (hz : ¬ z ≥ 2) : z + 1 ≤ 2 := by linarith example (a b c : ℚ) (h1 : 1 / a < b) (h2 : b < c) : 1 / a < c := by linarith example (N : ℕ) (n : ℕ) (Hirrelevant : n > N) (A : ℚ) (l : ℚ) (h : A - l ≤ -(A - l)) (h_1 : ¬A ≤ -A) (h_2 : ¬l ≤ -l) (h_3 : -(A - l) < 1) : A < l + 1 := by linarith example (d : ℚ) (q n : ℕ) (h1 : ((q : ℚ) - 1)*n ≥ 0) (h2 : d = 2/3*(((q : ℚ) - 1)*n)) : d ≤ ((q : ℚ) - 1)*n := by linarith example (d : ℚ) (q n : ℕ) (h1 : ((q : ℚ) - 1)*n ≥ 0) (h2 : d = 2/3*(((q : ℚ) - 1)*n)) : ((q : ℚ) - 1)*n - d = 1/3 * (((q : ℚ) - 1)*n) := by linarith example (a : ℚ) (ha : 0 ≤ a) : 0 * 0 ≤ 2 * a := by linarith example (x : ℚ) : id x ≥ x := by success_if_fail {linarith}; linarith! example (x y z : ℚ) (hx : x < 5) (hx2 : x > 5) (hy : y < 5000000000) (hz : z > 34*y) : false := by linarith only [hx, hx2] example (x y z : ℚ) (hx : x < 5) (hy : y < 5000000000) (hz : z > 34*y) : x ≤ 5 := by linarith only [hx]
da9ccadd676166724e794c2eb17f59d2702c10d8
b00eb947a9c4141624aa8919e94ce6dcd249ed70
/src/Lean/Elab/Tactic/Simp.lean
7f295623d79c0f950128af2985154d36b52c518d
[ "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
6,549
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Tactic.Simp import Lean.Elab.Tactic.Basic import Lean.Elab.Tactic.ElabTerm import Lean.Elab.Tactic.Location import Lean.Meta.Tactic.Replace import Lean.Elab.BuiltinNotation namespace Lean.Elab.Tactic open Meta unsafe def evalSimpConfigUnsafe (e : Expr) : TermElabM Meta.Simp.Config := Term.evalExpr Meta.Simp.Config ``Meta.Simp.Config e @[implementedBy evalSimpConfigUnsafe] constant evalSimpConfig (e : Expr) : TermElabM Meta.Simp.Config unsafe def evalSimpConfigCtxUnsafe (e : Expr) : TermElabM Meta.Simp.ConfigCtx := Term.evalExpr Meta.Simp.ConfigCtx ``Meta.Simp.ConfigCtx e @[implementedBy evalSimpConfigCtxUnsafe] constant evalSimpConfigCtx (e : Expr) : TermElabM Meta.Simp.ConfigCtx /- `optConfig` is of the form `("(" "config" ":=" term ")")?` If `ctx == false`, the argument is assumed to have type `Meta.Simp.Config`, and `Meta.Simp.ConfigCtx` otherwise. -/ def elabSimpConfig (optConfig : Syntax) (ctx : Bool) : TermElabM Meta.Simp.Config := do if optConfig.isNone then if ctx then return { : Meta.Simp.ConfigCtx }.toConfig else return {} else withoutModifyingState <| withLCtx {} {} <| Term.withSynthesize do let c ← Term.elabTermEnsuringType optConfig[3] (Lean.mkConst (if ctx then ``Meta.Simp.ConfigCtx else ``Meta.Simp.Config)) if ctx then return (← evalSimpConfigCtx (← instantiateMVars c)).toConfig else evalSimpConfig (← instantiateMVars c) private def addDeclToUnfoldOrLemma (lemmas : Meta.SimpLemmas) (e : Expr) (post : Bool) : MetaM Meta.SimpLemmas := do if e.isConst then let declName := e.constName! let info ← getConstInfo declName if (← isProp info.type) then lemmas.addConst declName post else lemmas.addDeclToUnfold declName else lemmas.add #[] e post private def addSimpLemma (lemmas : Meta.SimpLemmas) (stx : Syntax) (post : Bool) : TermElabM Meta.SimpLemmas := do let (levelParams, proof) ← Term.withoutModifyingElabMetaState <| withRef stx <| Term.withoutErrToSorry do let e ← Term.elabTerm stx none Term.synthesizeSyntheticMVarsUsingDefault let e ← instantiateMVars e let e := e.eta if e.hasMVar then let r ← abstractMVars e return (r.paramNames, r.expr) else return (#[], e) lemmas.add levelParams proof /-- Elaborate extra simp lemmas provided to `simp`. `stx` is of the `simpLemma,*` If `eraseLocal == true`, then we consider local declarations when resolving names for erased lemmas (`- id`), this option only makes sense for `simp_all`. -/ private def elabSimpLemmas (stx : Syntax) (ctx : Simp.Context) (eraseLocal : Bool) : TacticM Simp.Context := do if stx.isNone then return ctx else /- syntax simpPre := "↓" syntax simpPost := "↑" syntax simpLemma := (simpPre <|> simpPost)? term syntax simpErase := "-" ident -/ withMainContext do let mut lemmas := ctx.simpLemmas for arg in stx[1].getSepArgs do if arg.getKind == ``Lean.Parser.Tactic.simpErase then if eraseLocal && (← Term.isLocalIdent? arg[1]).isSome then -- We use `eraseCore` because the simp lemma for the hypothesis was not added yet lemmas ← lemmas.eraseCore arg[1].getId else let declName ← resolveGlobalConstNoOverloadWithInfo arg[1] lemmas ← lemmas.erase declName else let post := if arg[0].isNone then true else arg[0][0].getKind == ``Parser.Tactic.simpPost match (← resolveSimpIdLemma? arg[1]) with | some e => lemmas ← addDeclToUnfoldOrLemma lemmas e post | _ => lemmas ← addSimpLemma lemmas arg[1] post return { ctx with simpLemmas := lemmas } where resolveSimpIdLemma? (simpArgTerm : Syntax) : TacticM (Option Expr) := do if simpArgTerm.isIdent then try Term.resolveId? simpArgTerm (withInfo := true) catch _ => return none else Term.elabCDotFunctionAlias? simpArgTerm -- If `ctx == false`, the argument is assumed to have type `Meta.Simp.Config`, and `Meta.Simp.ConfigCtx` otherwise. -/ private def mkSimpContext (stx : Syntax) (eraseLocal : Bool) (ctx := false) : TacticM Simp.Context := do let simpOnly := !stx[2].isNone elabSimpLemmas stx[3] (eraseLocal := eraseLocal) { config := (← elabSimpConfig stx[1] (ctx := ctx)) simpLemmas := if simpOnly then {} else (← getSimpLemmas) congrLemmas := (← getCongrLemmas) } /- "simp " ("(" "config" ":=" term ")")? ("only ")? ("[" simpLemma,* "]")? (location)? -/ @[builtinTactic Lean.Parser.Tactic.simp] def evalSimp : Tactic := fun stx => do let ctx ← mkSimpContext stx (eraseLocal := false) -- trace[Meta.debug] "Lemmas {← toMessageData ctx.simpLemmas.post}" let loc := expandOptLocation stx[4] match loc with | Location.targets hUserNames simpTarget => withMainContext do let fvarIds ← hUserNames.mapM fun hUserName => return (← getLocalDeclFromUserName hUserName).fvarId go ctx fvarIds simpTarget | Location.wildcard => withMainContext do go ctx (← getNondepPropHyps (← getMainGoal)) true where go (ctx : Simp.Context) (fvarIdsToSimp : Array FVarId) (simpType : Bool) : TacticM Unit := do let mut mvarId ← getMainGoal let mut toAssert : Array Hypothesis := #[] for fvarId in fvarIdsToSimp do let localDecl ← getLocalDecl fvarId let type ← instantiateMVars localDecl.type match (← simpStep mvarId (mkFVar fvarId) type ctx) with | none => replaceMainGoal []; return () | some (value, type) => toAssert := toAssert.push { userName := localDecl.userName, type := type, value := value } if simpType then match (← simpTarget mvarId ctx) with | none => replaceMainGoal []; return () | some mvarIdNew => mvarId := mvarIdNew let (_, mvarIdNew) ← assertHypotheses mvarId toAssert let mvarIdNew ← tryClearMany mvarIdNew fvarIdsToSimp replaceMainGoal [mvarIdNew] @[builtinTactic Lean.Parser.Tactic.simpAll] def evalSimpAll : Tactic := fun stx => do let ctx ← mkSimpContext stx (eraseLocal := true) (ctx := true) match (← simpAll (← getMainGoal) ctx) with | none => replaceMainGoal [] | some mvarId => replaceMainGoal [mvarId] end Lean.Elab.Tactic
e3446c045e3a603c5c3ec6656fa7832e9486f46a
a2ee6a66690e8da666951cac0c243d42db11f9f3
/src/data/option/basic.lean
601102bcc9e82d702518e5691edd6360be35bd5d
[ "Apache-2.0" ]
permissive
shyamalschandra/mathlib
6d414d7c334bf383e764336843f065bd14c44273
ca679acad147870b2c5087d90fe3550f107dea49
refs/heads/master
1,671,730,354,335
1,601,883,576,000
1,601,883,576,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,668
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 tactic.basic namespace option variables {α : Type*} {β : Type*} {γ : Type*} lemma some_ne_none (x : α) : some x ≠ none := λ h, option.no_confusion h @[simp] theorem get_mem : ∀ {o : option α} (h : is_some o), option.get h ∈ o | (some a) _ := rfl theorem get_of_mem {a : α} : ∀ {o : option α} (h : is_some o), a ∈ o → option.get h = a | _ _ rfl := rfl @[simp] lemma not_mem_none (a : α) : a ∉ (none : option α) := λ h, option.no_confusion h @[simp] lemma some_get : ∀ {x : option α} (h : is_some x), some (option.get h) = x | (some x) hx := rfl @[simp] lemma get_some (x : α) (h : is_some (some x)) : option.get h = x := rfl @[simp] lemma get_or_else_some (x y : α) : option.get_or_else (some x) y = x := rfl lemma get_or_else_of_ne_none {x : option α} (hx : x ≠ none) (y : α) : some (x.get_or_else y) = x := by cases x; [contradiction, rw get_or_else_some] theorem mem_unique {o : option α} {a b : α} (ha : a ∈ o) (hb : b ∈ o) : a = b := option.some.inj $ ha.symm.trans hb theorem some_injective (α : Type*) : function.injective (@some α) := λ _ _, some_inj.mp /-- `option.map f` is injective if `f` is injective. -/ theorem map_injective {f : α → β} (Hf : function.injective f) : function.injective (option.map f) | none none H := rfl | (some a₁) (some a₂) H := by rw Hf (option.some.inj H) @[ext] theorem ext : ∀ {o₁ o₂ : option α}, (∀ a, a ∈ o₁ ↔ a ∈ o₂) → o₁ = o₂ | none none H := rfl | (some a) o H := ((H _).1 rfl).symm | o (some b) H := (H _).2 rfl theorem eq_none_iff_forall_not_mem {o : option α} : o = none ↔ (∀ a, a ∉ o) := ⟨λ e a h, by rw e at h; cases h, λ h, ext $ by simpa⟩ @[simp] theorem none_bind {α β} (f : α → option β) : none >>= f = none := rfl @[simp] theorem some_bind {α β} (a : α) (f : α → option β) : some a >>= f = f a := rfl @[simp] theorem none_bind' (f : α → option β) : none.bind f = none := rfl @[simp] theorem some_bind' (a : α) (f : α → option β) : (some a).bind f = f a := rfl @[simp] theorem bind_some : ∀ x : option α, x >>= some = x := @bind_pure α option _ _ @[simp] theorem bind_eq_some {α β} {x : option α} {f : α → option β} {b : β} : x >>= f = some b ↔ ∃ a, x = some a ∧ f a = some b := by cases x; simp @[simp] theorem bind_eq_some' {x : option α} {f : α → option β} {b : β} : x.bind f = some b ↔ ∃ a, x = some a ∧ f a = some b := by cases x; simp @[simp] theorem bind_eq_none' {o : option α} {f : α → option β} : o.bind f = none ↔ (∀ b a, a ∈ o → b ∉ f a) := by simp only [eq_none_iff_forall_not_mem, not_exists, not_and, mem_def, bind_eq_some'] @[simp] theorem bind_eq_none {α β} {o : option α} {f : α → option β} : o >>= f = none ↔ (∀ b a, a ∈ o → b ∉ f a) := bind_eq_none' lemma bind_comm {α β γ} {f : α → β → option γ} (a : option α) (b : option β) : a.bind (λx, b.bind (f x)) = b.bind (λy, a.bind (λx, f x y)) := by cases a; cases b; refl lemma bind_assoc (x : option α) (f : α → option β) (g : β → option γ) : (x.bind f).bind g = x.bind (λ y, (f y).bind g) := by cases x; refl @[simp] theorem map_none {α β} {f : α → β} : f <$> none = none := rfl @[simp] theorem map_some {α β} {a : α} {f : α → β} : f <$> some a = some (f a) := rfl @[simp] theorem map_none' {f : α → β} : option.map f none = none := rfl @[simp] theorem map_some' {a : α} {f : α → β} : option.map f (some a) = some (f a) := rfl @[simp] theorem map_eq_some {α β} {x : option α} {f : α → β} {b : β} : f <$> x = some b ↔ ∃ a, x = some a ∧ f a = b := by cases x; simp @[simp] theorem map_eq_some' {x : option α} {f : α → β} {b : β} : x.map f = some b ↔ ∃ a, x = some a ∧ f a = b := by cases x; simp @[simp] theorem map_id' : option.map (@id α) = id := map_id @[simp] theorem seq_some {α β} {a : α} {f : α → β} : some f <*> some a = some (f a) := rfl @[simp] theorem some_orelse' (a : α) (x : option α) : (some a).orelse x = some a := rfl @[simp] theorem some_orelse (a : α) (x : option α) : (some a <|> x) = some a := rfl @[simp] theorem none_orelse' (x : option α) : none.orelse x = x := by cases x; refl @[simp] theorem none_orelse (x : option α) : (none <|> x) = x := none_orelse' x @[simp] theorem orelse_none' (x : option α) : x.orelse none = x := by cases x; refl @[simp] theorem orelse_none (x : option α) : (x <|> none) = x := orelse_none' x @[simp] theorem is_some_none : @is_some α none = ff := rfl @[simp] theorem is_some_some {a : α} : is_some (some a) = tt := rfl theorem is_some_iff_exists {x : option α} : is_some x ↔ ∃ a, x = some a := by cases x; simp [is_some]; exact ⟨_, rfl⟩ @[simp] theorem is_none_none : @is_none α none = tt := rfl @[simp] theorem is_none_some {a : α} : is_none (some a) = ff := rfl @[simp] theorem not_is_some {a : option α} : is_some a = ff ↔ a.is_none = tt := by cases a; simp lemma eq_some_iff_get_eq {o : option α} {a : α} : o = some a ↔ ∃ h : o.is_some, option.get h = a := by cases o; simp lemma not_is_some_iff_eq_none {o : option α} : ¬o.is_some ↔ o = none := by cases o; simp lemma ne_none_iff_is_some {o : option α} : o ≠ none ↔ o.is_some := by cases o; simp lemma ne_none_iff_exists {o : option α} : o ≠ none ↔ ∃ (x : α), some x = o := by {cases o; simp} lemma ne_none_iff_exists' {o : option α} : o ≠ none ↔ ∃ (x : α), o = some x := ne_none_iff_exists.trans $ exists_congr $ λ _, eq_comm lemma bex_ne_none {p : option α → Prop} : (∃ x ≠ none, p x) ↔ ∃ x, p (some x) := ⟨λ ⟨x, hx, hp⟩, ⟨get $ ne_none_iff_is_some.1 hx, by rwa [some_get]⟩, λ ⟨x, hx⟩, ⟨some x, some_ne_none x, hx⟩⟩ lemma ball_ne_none {p : option α → Prop} : (∀ x ≠ none, p x) ↔ ∀ x, p (some x) := ⟨λ h x, h (some x) (some_ne_none x), λ h x hx, by simpa only [some_get] using h (get $ ne_none_iff_is_some.1 hx)⟩ theorem iget_mem [inhabited α] : ∀ {o : option α}, is_some o → o.iget ∈ o | (some a) _ := rfl theorem iget_of_mem [inhabited α] {a : α} : ∀ {o : option α}, a ∈ o → o.iget = a | _ rfl := rfl @[simp] theorem guard_eq_some {p : α → Prop} [decidable_pred p] {a b : α} : guard p a = some b ↔ a = b ∧ p a := by by_cases p a; simp [option.guard, h]; intro; contradiction @[simp] theorem guard_eq_some' {p : Prop} [decidable p] : ∀ u, _root_.guard p = some u ↔ p | () := by by_cases p; simp [guard, h, pure]; intro; contradiction theorem lift_or_get_choice {f : α → α → α} (h : ∀ a b, f a b = a ∨ f a b = b) : ∀ o₁ o₂, lift_or_get f o₁ o₂ = o₁ ∨ lift_or_get f o₁ o₂ = o₂ | none none := or.inl rfl | (some a) none := or.inl rfl | none (some b) := or.inr rfl | (some a) (some b) := by simpa [lift_or_get] using h a b @[simp] lemma lift_or_get_none_left {f} {b : option α} : lift_or_get f none b = b := by cases b; refl @[simp] lemma lift_or_get_none_right {f} {a : option α} : lift_or_get f a none = a := by cases a; refl @[simp] lemma lift_or_get_some_some {f} {a b : α} : lift_or_get f (some a) (some b) = f a b := rfl /-- given an element of `a : option α`, a default element `b : β` and a function `α → β`, apply this function to `a` if it comes from `α`, and return `b` otherwise. -/ def cases_on' : option α → β → (α → β) → β | none n s := n | (some a) n s := s a end option
dba2939aaa75da3c12d06fe682a399565daebf2a
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/topology/metric_space/kuratowski.lean
c5e02bafa4427dc321537c04a09bff33eda136c7
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
4,951
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import topology.metric_space.isometry import topology.continuous_function.bounded import topology.compacts /-! # The Kuratowski embedding Any separable metric space can be embedded isometrically in `ℓ^∞(ℝ)`. -/ noncomputable theory open set universes u v w variables {α : Type u} {β : Type v} {γ : Type w} /-- The space of bounded sequences, with its sup norm -/ @[reducible] def ℓ_infty_ℝ : Type := bounded_continuous_function ℕ ℝ open bounded_continuous_function metric topological_space namespace Kuratowski_embedding /-! ### Any separable metric space can be embedded isometrically in ℓ^∞(ℝ) -/ variables {f g : ℓ_infty_ℝ} {n : ℕ} {C : ℝ} [metric_space α] (x : ℕ → α) (a b : α) /-- A metric space can be embedded in `l^∞(ℝ)` via the distances to points in a fixed countable set, if this set is dense. This map is given in `Kuratowski_embedding`, without density assumptions. -/ def embedding_of_subset : ℓ_infty_ℝ := of_normed_group_discrete (λn, dist a (x n) - dist (x 0) (x n)) (dist a (x 0)) (λ_, abs_dist_sub_le _ _ _) lemma embedding_of_subset_coe : embedding_of_subset x a n = dist a (x n) - dist (x 0) (x n) := rfl /-- The embedding map is always a semi-contraction. -/ lemma embedding_of_subset_dist_le (a b : α) : dist (embedding_of_subset x a) (embedding_of_subset x b) ≤ dist a b := begin refine (dist_le dist_nonneg).2 (λn, _), simp only [embedding_of_subset_coe, real.dist_eq], convert abs_dist_sub_le a b (x n) using 2, ring end /-- When the reference set is dense, the embedding map is an isometry on its image. -/ lemma embedding_of_subset_isometry (H : dense_range x) : isometry (embedding_of_subset x) := begin refine isometry_emetric_iff_metric.2 (λa b, _), refine (embedding_of_subset_dist_le x a b).antisymm (le_of_forall_pos_le_add (λe epos, _)), /- First step: find n with dist a (x n) < e -/ rcases metric.mem_closure_range_iff.1 (H a) (e/2) (half_pos epos) with ⟨n, hn⟩, /- Second step: use the norm control at index n to conclude -/ have C : dist b (x n) - dist a (x n) = embedding_of_subset x b n - embedding_of_subset x a n := by { simp only [embedding_of_subset_coe, sub_sub_sub_cancel_right] }, have := calc dist a b ≤ dist a (x n) + dist (x n) b : dist_triangle _ _ _ ... = 2 * dist a (x n) + (dist b (x n) - dist a (x n)) : by { simp [dist_comm], ring } ... ≤ 2 * dist a (x n) + abs (dist b (x n) - dist a (x n)) : by apply_rules [add_le_add_left, le_abs_self] ... ≤ 2 * (e/2) + abs (embedding_of_subset x b n - embedding_of_subset x a n) : begin rw C, apply_rules [add_le_add, mul_le_mul_of_nonneg_left, hn.le, le_refl], norm_num end ... ≤ 2 * (e/2) + dist (embedding_of_subset x b) (embedding_of_subset x a) : by simp [← real.dist_eq, dist_coe_le_dist] ... = dist (embedding_of_subset x b) (embedding_of_subset x a) + e : by ring, simpa [dist_comm] using this end /-- Every separable metric space embeds isometrically in `ℓ_infty_ℝ`. -/ theorem exists_isometric_embedding (α : Type u) [metric_space α] [separable_space α] : ∃(f : α → ℓ_infty_ℝ), isometry f := begin cases (univ : set α).eq_empty_or_nonempty with h h, { use (λ_, 0), assume x, exact absurd h (nonempty.ne_empty ⟨x, mem_univ x⟩) }, { /- We construct a map x : ℕ → α with dense image -/ rcases h with ⟨basepoint⟩, haveI : inhabited α := ⟨basepoint⟩, have : ∃s:set α, countable s ∧ dense s := exists_countable_dense α, rcases this with ⟨S, ⟨S_countable, S_dense⟩⟩, rcases countable_iff_exists_surjective.1 S_countable with ⟨x, x_range⟩, /- Use embedding_of_subset to construct the desired isometry -/ exact ⟨embedding_of_subset x, embedding_of_subset_isometry x (S_dense.mono x_range)⟩ } end end Kuratowski_embedding open topological_space Kuratowski_embedding /-- The Kuratowski embedding is an isometric embedding of a separable metric space in `ℓ^∞(ℝ)`. -/ def Kuratowski_embedding (α : Type u) [metric_space α] [separable_space α] : α → ℓ_infty_ℝ := classical.some (Kuratowski_embedding.exists_isometric_embedding α) /-- The Kuratowski embedding is an isometry. -/ protected lemma Kuratowski_embedding.isometry (α : Type u) [metric_space α] [separable_space α] : isometry (Kuratowski_embedding α) := classical.some_spec (exists_isometric_embedding α) /-- Version of the Kuratowski embedding for nonempty compacts -/ def nonempty_compacts.Kuratowski_embedding (α : Type u) [metric_space α] [compact_space α] [nonempty α] : nonempty_compacts ℓ_infty_ℝ := ⟨range (Kuratowski_embedding α), range_nonempty _, is_compact_range (Kuratowski_embedding.isometry α).continuous⟩
dc4e56f8cdeb3039d3ac8fd0de5baa143c02676b
842b7df4a999c5c50bbd215b8617dd705e43c2e1
/LeanTutorial/Chapter2.lean
8d3a97f6fdf60bf3e376d15e68432a1fddb3e250
[]
no_license
Samyak-Surti/LeanCode
1c245631f74b00057d20483c8ac75916e8643b14
944eac3e5f43e2614ed246083b97fbdf24181d83
refs/heads/master
1,669,023,730,828
1,595,534,784,000
1,595,534,784,000
282,037,186
0
0
null
null
null
null
UTF-8
Lean
false
false
228
lean
def foo : (ℕ → ℕ) → ℕ := λ f, f 0 #check foo #print foo def foo' := λ f : ℕ → ℕ, f 0 -- General Form: def foo : α := bar def double (x : ℕ) : ℕ := x + x #print double #check double 3 #reduce double 3
fce3b45f642a6682b5a9045a3b570e17ab592b00
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/PrettyPrinter/Delaborator/Basic.lean
d92816b0674ef217e9593927346c0f7b3ff69f9c
[ "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
11,988
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.KeyedDeclsAttribute import Lean.ProjFns import Lean.Syntax import Lean.Meta.Match.Match import Lean.Elab.Term import Lean.PrettyPrinter.Delaborator.Options import Lean.PrettyPrinter.Delaborator.SubExpr import Lean.PrettyPrinter.Delaborator.TopDownAnalyze /-! The delaborator is the first stage of the pretty printer, and the inverse of the elaborator: it turns fully elaborated `Expr` core terms back into surface-level `Syntax`, omitting some implicit information again and using higher-level syntax abstractions like notations where possible. The exact behavior can be customized using pretty printer options; activating `pp.all` should guarantee that the delaborator is injective and that re-elaborating the resulting `Syntax` round-trips. Pretty printer options can be given not only for the whole term, but also specific subterms. This is used both when automatically refining pp options until round-trip and when interactively selecting pp options for a subterm (both TBD). The association of options to subterms is done by assigning a unique, synthetic Nat position to each subterm derived from its position in the full term. This position is added to the corresponding Syntax object so that elaboration errors and interactions with the pretty printer output can be traced back to the subterm. The delaborator is extensible via the `[delab]` attribute. -/ namespace Lean.PrettyPrinter.Delaborator open Lean.Meta SubExpr open Lean.Elab (Info TermInfo Info.ofTermInfo) structure Context where defaultOptions : Options optionsPerPos : OptionsPerPos currNamespace : Name openDecls : List OpenDecl inPattern : Bool := false -- true when delaborating `match` patterns subExpr : SubExpr structure State where /-- We attach `Elab.Info` at various locations in the `Syntax` output in order to convey its semantics. While the elaborator emits `InfoTree`s, here we have no real text location tree to traverse, so we use a flattened map. -/ infos : Std.RBMap Pos Info compare := {} /-- See `SubExpr.nextExtraPos`. -/ holeIter : SubExpr.HoleIterator := {} -- Exceptions from delaborators are not expected. We use an internal exception to signal whether -- the delaborator was able to produce a Syntax object. builtin_initialize delabFailureId : InternalExceptionId ← registerInternalExceptionId `delabFailure abbrev DelabM := ReaderT Context (StateRefT State MetaM) abbrev Delab := DelabM Syntax instance : Inhabited (DelabM α) where default := throw arbitrary @[inline] protected def orElse (d₁ : DelabM α) (d₂ : Unit → DelabM α) : DelabM α := do catchInternalId delabFailureId d₁ fun _ => d₂ () protected def failure : DelabM α := throw $ Exception.internal delabFailureId instance : Alternative DelabM where orElse := Delaborator.orElse failure := Delaborator.failure -- HACK: necessary since it would otherwise prefer the instance from MonadExcept instance {α} : OrElse (DelabM α) := ⟨Delaborator.orElse⟩ -- Low priority instances so `read`/`get`/etc default to the whole `Context`/`State` instance (priority := low) : MonadReaderOf SubExpr DelabM where read := Context.subExpr <$> read instance (priority := low) : MonadWithReaderOf SubExpr DelabM where withReader f x := fun ctx => x { ctx with subExpr := f ctx.subExpr } instance (priority := low) : MonadStateOf SubExpr.HoleIterator DelabM where get := State.holeIter <$> get set iter := modify fun ⟨infos, _⟩ => ⟨infos, iter⟩ modifyGet f := modifyGet fun ⟨infos, iter⟩ => let (ret, iter') := f iter; (ret, ⟨infos, iter'⟩) -- Macro scopes in the delaborator output are ultimately ignored by the pretty printer, -- so give a trivial implementation. instance : MonadQuotation DelabM := { getCurrMacroScope := pure arbitrary, getMainModule := pure arbitrary, withFreshMacroScope := fun x => x } unsafe def mkDelabAttribute : IO (KeyedDeclsAttribute Delab) := KeyedDeclsAttribute.init { builtinName := `builtinDelab, name := `delab, descr := "Register a delaborator. [delab k] registers a declaration of type `Lean.PrettyPrinter.Delaborator.Delab` for the `Lean.Expr` constructor `k`. Multiple delaborators for a single constructor are tried in turn until the first success. If the term to be delaborated is an application of a constant `c`, elaborators for `app.c` are tried first; this is also done for `Expr.const`s (\"nullary applications\") to reduce special casing. If the term is an `Expr.mdata` with a single key `k`, `mdata.k` is tried first.", valueTypeName := `Lean.PrettyPrinter.Delaborator.Delab } `Lean.PrettyPrinter.Delaborator.delabAttribute @[builtinInit mkDelabAttribute] constant delabAttribute : KeyedDeclsAttribute Delab def getExprKind : DelabM Name := do let e ← getExpr pure $ match e with | Expr.bvar _ _ => `bvar | Expr.fvar _ _ => `fvar | Expr.mvar _ _ => `mvar | Expr.sort _ _ => `sort | Expr.const c _ _ => -- we identify constants as "nullary applications" to reduce special casing `app ++ c | Expr.app fn _ _ => match fn.getAppFn with | Expr.const c _ _ => `app ++ c | _ => `app | Expr.lam _ _ _ _ => `lam | Expr.forallE _ _ _ _ => `forallE | Expr.letE _ _ _ _ _ => `letE | Expr.lit _ _ => `lit | Expr.mdata m _ _ => match m.entries with | [(key, _)] => `mdata ++ key | _ => `mdata | Expr.proj _ _ _ _ => `proj def getOptionsAtCurrPos : DelabM Options := do let ctx ← read let mut opts := ctx.defaultOptions if let some opts' ← ctx.optionsPerPos.find? (← getPos) then for (k, v) in opts' do opts := opts.insert k v opts /-- Evaluate option accessor, using subterm-specific options if set. -/ def getPPOption (opt : Options → Bool) : DelabM Bool := do return opt (← getOptionsAtCurrPos) def whenPPOption (opt : Options → Bool) (d : Delab) : Delab := do let b ← getPPOption opt if b then d else failure def whenNotPPOption (opt : Options → Bool) (d : Delab) : Delab := do let b ← getPPOption opt if b then failure else d def annotatePos (pos : Nat) (stx : Syntax) : Syntax := stx.setInfo (SourceInfo.synthetic pos pos) def annotateCurPos (stx : Syntax) : Delab := do annotatePos (← getPos) stx def getUnusedName (suggestion : Name) (body : Expr) : DelabM Name := do -- Use a nicer binder name than `[anonymous]`. We probably shouldn't do this in all LocalContext use cases, so do it here. let suggestion := if suggestion.isAnonymous then `a else suggestion; let suggestion := suggestion.eraseMacroScopes let lctx ← getLCtx if !lctx.usesUserName suggestion then return suggestion else if (← getPPOption getPPSafeShadowing) && !bodyUsesSuggestion lctx suggestion then return suggestion else return lctx.getUnusedName suggestion where bodyUsesSuggestion (lctx : LocalContext) (suggestion' : Name) : Bool := Option.isSome <| body.find? fun | Expr.fvar fvarId _ => match lctx.find? fvarId with | none => false | some decl => decl.userName == suggestion' | _ => false def withBindingBodyUnusedName {α} (d : Syntax → DelabM α) : DelabM α := do let n ← getUnusedName (← getExpr).bindingName! (← getExpr).bindingBody! let stxN ← annotateCurPos (mkIdent n) withBindingBody n $ d stxN @[inline] def liftMetaM {α} (x : MetaM α) : DelabM α := liftM x def addTermInfo (pos : Pos) (stx : Syntax) (e : Expr) (isBinder : Bool := false) : DelabM Unit := do let info ← mkTermInfo stx e isBinder modify fun s => { s with infos := s.infos.insert pos info } where mkTermInfo stx e isBinder := do Info.ofTermInfo { elaborator := `Delab, stx := stx, lctx := (← getLCtx), expectedType? := none, expr := e, isBinder := isBinder } def addFieldInfo (pos : Pos) (projName fieldName : Name) (stx : Syntax) (val : Expr) : DelabM Unit := do let info ← mkFieldInfo projName fieldName stx val modify fun s => { s with infos := s.infos.insert pos info } where mkFieldInfo projName fieldName stx val := do Info.ofFieldInfo { projName := projName, fieldName := fieldName, lctx := (← getLCtx), val := val, stx := stx } partial def delabFor : Name → Delab | Name.anonymous => failure | k => (do let stx ← (delabAttribute.getValues (← getEnv) k).firstM id let stx ← annotateCurPos stx addTermInfo (← getPos) stx (← getExpr) stx) -- have `app.Option.some` fall back to `app` etc. <|> delabFor k.getRoot partial def delab : Delab := do checkMaxHeartbeats "delab" let e ← getExpr -- no need to hide atomic proofs if ← !e.isAtomic <&&> !(← getPPOption getPPProofs) <&&> (try Meta.isProof e catch ex => false) then if ← getPPOption getPPProofsWithType then let stx ← withType delab return ← ``((_ : $stx)) else return ← ``(_) let k ← getExprKind let stx ← delabFor k <|> (liftM $ show MetaM Syntax from throwError "don't know how to delaborate '{k}'") if ← getPPOption getPPAnalyzeTypeAscriptions <&&> getPPOption getPPAnalysisNeedsType <&&> !e.isMData then let typeStx ← withType delab `(($stx:term : $typeStx:term)) >>= annotateCurPos else stx unsafe def mkAppUnexpanderAttribute : IO (KeyedDeclsAttribute Unexpander) := KeyedDeclsAttribute.init { name := `appUnexpander, descr := "Register an unexpander for applications of a given constant. [appUnexpander c] registers a `Lean.PrettyPrinter.Unexpander` for applications of the constant `c`. The unexpander is passed the result of pre-pretty printing the application *without* implicitly passed arguments. If `pp.explicit` is set to true or `pp.notation` is set to false, it will not be called at all.", valueTypeName := `Lean.PrettyPrinter.Unexpander evalKey := fun _ stx => do resolveGlobalConstNoOverloadCore (← Attribute.Builtin.getId stx) } `Lean.PrettyPrinter.Delaborator.appUnexpanderAttribute @[builtinInit mkAppUnexpanderAttribute] constant appUnexpanderAttribute : KeyedDeclsAttribute Unexpander end Delaborator open Delaborator (OptionsPerPos topDownAnalyze Pos) def delabCore (currNamespace : Name) (openDecls : List OpenDecl) (e : Expr) (optionsPerPos : OptionsPerPos := {}) : MetaM (Syntax × Std.RBMap Pos Elab.Info compare) := do trace[PrettyPrinter.delab.input] "{Std.format e}" let mut opts ← MonadOptions.getOptions -- default `pp.proofs` to `true` if `e` is a proof if pp.proofs.get? opts == none then try if ← Meta.isProof e then opts := pp.proofs.set opts true catch _ => pure () let e ← if getPPInstantiateMVars opts then ← Meta.instantiateMVars e else e let optionsPerPos ← if !getPPAll opts && getPPAnalyze opts && optionsPerPos.isEmpty then withTheReader Core.Context (fun ctx => { ctx with options := opts }) do topDownAnalyze e else optionsPerPos let (stx, {infos := infos, ..}) ← catchInternalId Delaborator.delabFailureId (Delaborator.delab { defaultOptions := opts optionsPerPos := optionsPerPos currNamespace := currNamespace openDecls := openDecls subExpr := Delaborator.SubExpr.mkRoot e } |>.run { : Delaborator.State }) (fun _ => unreachable!) return (stx, infos) /-- "Delaborate" the given term into surface-level syntax using the default and given subterm-specific options. -/ def delab (currNamespace : Name) (openDecls : List OpenDecl) (e : Expr) (optionsPerPos : OptionsPerPos := {}) : MetaM Syntax := do let (stx, _) ← delabCore currNamespace openDecls e optionsPerPos stx builtin_initialize registerTraceClass `PrettyPrinter.delab end Lean.PrettyPrinter
5d0bb2f07a187831e9005361acc48b99cfc69d37
97c8e5d8aca4afeebb5b335f26a492c53680efc8
/ground_zero/types/integers.lean
9aa7b66f59b490dc270b1fd9f2908080a1de8491
[]
no_license
jfrancese/lean
cf32f0d8d5520b6f0e9d3987deb95841c553c53c
06e7efaecce4093d97fb5ecc75479df2ef1dbbdb
refs/heads/master
1,587,915,151,351
1,551,012,140,000
1,551,012,140,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
977
lean
import ground_zero.types.equiv namespace ground_zero.types /- +2 = pos 2 +1 = pos 1 0 = pos 0 −1 = neg 0 −2 = neg 1 -/ inductive integers | pos : ℕ → integers | neg : ℕ → integers namespace integers instance : has_zero integers := ⟨pos 0⟩ instance : has_one integers := ⟨pos 1⟩ instance : has_repr integers := ⟨λ x, match x with | (pos n) := to_string n | (neg n) := "−" ++ to_string (n + 1) end⟩ def auxsucc : ℕ → integers | 0 := pos 0 | (n + 1) := neg n def succ : integers → integers | (neg u) := auxsucc u | (pos v) := pos (v + 1) def auxpred : ℕ → integers | 0 := neg 0 | (n + 1) := pos n def pred : integers → integers | (neg u) := neg (u + 1) | (pos v) := auxpred v def succ_equiv : integers ≃ integers := begin existsi succ, split; existsi pred, repeat { intro n, induction n, repeat { trivial }, { induction n with n ih, repeat { trivial } } } end end integers end ground_zero.types
d76e9f76502c0642eaff432a86ba3a280909deb8
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/src/Init/Data/Array/BasicAux.lean
848f025cae5e85e4b75b68a5a1dc0f44fa9605e6
[ "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
2,954
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Data.Array.Basic import Init.Data.Nat.Linear import Init.NotationExtra theorem Array.of_push_eq_push {as bs : Array α} (h : as.push a = bs.push b) : as = bs ∧ a = b := by simp [push] at h have ⟨h₁, h₂⟩ := List.of_concat_eq_concat h cases as; cases bs simp_all private theorem List.size_toArrayAux (as : List α) (bs : Array α) : (as.toArrayAux bs).size = as.length + bs.size := by induction as generalizing bs with | nil => simp [toArrayAux] | cons a as ih => simp_arith [toArrayAux, *] private theorem List.of_toArrayAux_eq_toArrayAux {as bs : List α} {cs ds : Array α} (h : as.toArrayAux cs = bs.toArrayAux ds) (hlen : cs.size = ds.size) : as = bs ∧ cs = ds := by match as, bs with | [], [] => simp [toArrayAux] at h; simp [h] | a::as, [] => simp [toArrayAux] at h; rw [← h] at hlen; simp_arith [size_toArrayAux] at hlen | [], b::bs => simp [toArrayAux] at h; rw [h] at hlen; simp_arith [size_toArrayAux] at hlen | a::as, b::bs => simp [toArrayAux] at h have : (cs.push a).size = (ds.push b).size := by simp [*] have ⟨ih₁, ih₂⟩ := of_toArrayAux_eq_toArrayAux h this simp [ih₁] have := Array.of_push_eq_push ih₂ simp [this] @[simp] theorem List.toArray_eq_toArray_eq (as bs : List α) : (as.toArray = bs.toArray) = (as = bs) := by apply propext; apply Iff.intro · intro h; simp [toArray] at h; have := of_toArrayAux_eq_toArrayAux h rfl; exact this.1 · intro h; rw [h] def Array.mapM' [Monad m] (f : α → m β) (as : Array α) : m { bs : Array β // bs.size = as.size } := go 0 ⟨mkEmpty as.size, rfl⟩ (by simp_arith) where go (i : Nat) (acc : { bs : Array β // bs.size = i }) (hle : i ≤ as.size) : m { bs : Array β // bs.size = as.size } := do if h : i = as.size then return h ▸ acc else have hlt : i < as.size := Nat.lt_of_le_of_ne hle h let b ← f as[i] go (i+1) ⟨acc.val.push b, by simp [acc.property]⟩ hlt termination_by go i _ _ => as.size - i @[inline] private unsafe def mapMonoMImp [Monad m] (as : Array α) (f : α → m α) : m (Array α) := go 0 as where @[specialize] go (i : Nat) (as : Array α) : m (Array α) := do if h : i < as.size then let a := as[i] let b ← f a if ptrEq a b then go (i+1) as else go (i+1) (as.set ⟨i, h⟩ b) else return as /-- Monomorphic `Array.mapM`. The internal implementation uses pointer equality, and does not allocate a new array if the result of each `f a` is a pointer equal value `a`. -/ @[implementedBy mapMonoMImp] def Array.mapMonoM [Monad m] (as : Array α) (f : α → m α) : m (Array α) := as.mapM f @[inline] def Array.mapMono (as : Array α) (f : α → α) : Array α := Id.run <| as.mapMonoM f
98e9ac6b48656adf5f4129ba30ee5ffa1b062bca
d406927ab5617694ec9ea7001f101b7c9e3d9702
/counterexamples/zero_divisors_in_add_monoid_algebras.lean
da188dfa571cbf074dd8ae0ff160cccfa2110c14
[ "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
11,313
lean
/- Copyright (c) 2022 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import algebra.geom_sum import algebra.group.unique_prods import algebra.monoid_algebra.basic import data.finsupp.lex import data.zmod.basic /-! # Examples of zero-divisors in `add_monoid_algebra`s This file contains an easy source of zero-divisors in an `add_monoid_algebra`. If `k` is a field and `G` is an additive group containing a non-zero torsion element, then `add_monoid_algebra k G` contains non-zero zero-divisors: this is lemma `zero_divisors_of_torsion`. There is also a version for periodic elements of an additive monoid: `zero_divisors_of_periodic`. The converse of this statement is [Kaplansky's zero divisor conjecture](https://en.wikipedia.org/wiki/Kaplansky%27s_conjectures). The formalized example generalizes in trivial ways the assumptions: the field `k` can be any nontrivial ring `R` and the additive group `G` with a torsion element can be any additive monoid `A` with a non-zero periodic element. Besides this example, we also address a comment in `data.finsupp.lex` to the effect that the proof that addition is monotone on `α →₀ N` uses that it is *strictly* monotone on `N`. The specific statement is about `finsupp.lex.covariant_class_le_left` and its analogue `finsupp.lex.covariant_class_le_right`. We do not need two separate counterexamples, since the operation is commutative. The example is very simple. Let `F = {0, 1}` with order determined by `0 < 1` and absorbing addition (which is the same as `max` in this case). We denote a function `f : F → F` (which is automatically finitely supported!) by `[f 0, f 1]`, listing its values. Recall that the order on finitely supported function is lexicographic, matching the list notation. The inequality `[0, 1] ≤ [1, 0]` holds. However, adding `[1, 0]` to both sides yields the *reversed* inequality `[1, 1] > [1, 0]`. -/ open finsupp add_monoid_algebra /-- This is a simple example showing that if `R` is a non-trivial ring and `A` is an additive monoid with an element `a` satisfying `n • a = a` and `(n - 1) • a ≠ a`, for some `2 ≤ n`, then `add_monoid_algebra R A` contains non-zero zero-divisors. The elements are easy to write down: `[a]` and `[a] ^ (n - 1) - 1` are non-zero elements of `add_monoid_algebra R A` whose product is zero. Observe that such an element `a` *cannot* be invertible. In particular, this lemma never applies if `A` is a group. -/ lemma zero_divisors_of_periodic {R A} [nontrivial R] [ring R] [add_monoid A] {n : ℕ} (a : A) (n2 : 2 ≤ n) (na : n • a = a) (na1 : (n - 1) • a ≠ 0) : ∃ f g : add_monoid_algebra R A, f ≠ 0 ∧ g ≠ 0 ∧ f * g = 0 := begin refine ⟨single a 1, single ((n - 1) • a) 1 - single 0 1, by simp, _, _⟩, { exact sub_ne_zero.mpr (by simpa [single_eq_single_iff]) }, { rw [mul_sub, add_monoid_algebra.single_mul_single, add_monoid_algebra.single_mul_single, sub_eq_zero, add_zero, ← succ_nsmul, nat.sub_add_cancel (one_le_two.trans n2), na] }, end lemma single_zero_one {R A} [semiring R] [has_zero A] : single (0 : A) (1 : R) = (1 : add_monoid_algebra R A) := rfl /-- This is a simple example showing that if `R` is a non-trivial ring and `A` is an additive monoid with a non-zero element `a` of finite order `oa`, then `add_monoid_algebra R A` contains non-zero zero-divisors. The elements are easy to write down: `∑ i in finset.range oa, [a] ^ i` and `[a] - 1` are non-zero elements of `add_monoid_algebra R A` whose product is zero. In particular, this applies whenever the additive monoid `A` is an additive group with a non-zero torsion element. -/ lemma zero_divisors_of_torsion {R A} [nontrivial R] [ring R] [add_monoid A] (a : A) (o2 : 2 ≤ add_order_of a) : ∃ f g : add_monoid_algebra R A, f ≠ 0 ∧ g ≠ 0 ∧ f * g = 0 := begin refine ⟨(finset.range (add_order_of a)).sum (λ (i : ℕ), (single a 1) ^ i), single a 1 - single 0 1, _, _, _⟩, { apply_fun (λ x : add_monoid_algebra R A, x 0), refine ne_of_eq_of_ne (_ : (_ : R) = 1) one_ne_zero, simp_rw finset.sum_apply', refine (finset.sum_eq_single 0 _ _).trans _, { intros b hb b0, rw [single_pow, one_pow, single_eq_of_ne], exact nsmul_ne_zero_of_lt_add_order_of' b0 (finset.mem_range.mp hb) }, { simp only [(zero_lt_two.trans_le o2).ne', finset.mem_range, not_lt, le_zero_iff, false_implies_iff] }, { rw [single_pow, one_pow, zero_smul, single_eq_same] } }, { apply_fun (λ x : add_monoid_algebra R A, x 0), refine sub_ne_zero.mpr (ne_of_eq_of_ne (_ : (_ : R) = 0) _), { have a0 : a ≠ 0 := ne_of_eq_of_ne (one_nsmul a).symm (nsmul_ne_zero_of_lt_add_order_of' one_ne_zero (nat.succ_le_iff.mp o2)), simp only [a0, single_eq_of_ne, ne.def, not_false_iff] }, { simpa only [single_eq_same] using zero_ne_one, } }, { convert commute.geom_sum₂_mul _ (add_order_of a), { ext, rw [single_zero_one, one_pow, mul_one] }, { rw [single_pow, one_pow, add_order_of_nsmul_eq_zero, single_zero_one, one_pow, sub_self] }, { simp only [single_zero_one, commute.one_right] } }, end example {R} [ring R] [nontrivial R] (n : ℕ) (n0 : 2 ≤ n) : ∃ f g : add_monoid_algebra R (zmod n), f ≠ 0 ∧ g ≠ 0 ∧ f * g = 0 := zero_divisors_of_torsion (1 : zmod n) (n0.trans_eq (zmod.add_order_of_one _).symm) /-- `F` is the type with two elements `zero` and `one`. We define the "obvious" linear order and absorbing addition on it to generate our counterexample. -/ @[derive [decidable_eq, inhabited]] inductive F | zero | one /-- The same as `list.get_rest`, except that we take the "rest" from the first match, rather than from the beginning, returning `[]` if there is no match. For instance, ```lean #eval [1,2].drop_until [3,1,2,4,1,2] -- [4, 1, 2] ``` -/ def list.drop_until {α} [decidable_eq α] : list α → list α → list α | l [] := [] | l (a::as) := ((a::as).get_rest l).get_or_else (l.drop_until as) /-- `guard_decl_in_file na loc` makes sure that the declaration with name `na` is in the file with relative path `"src/" ++ "/".intercalate loc ++ ".lean"`. ```lean #eval guard_decl_in_file `nat.nontrivial ["data", "nat", "basic"] -- does nothing #eval guard_decl_in_file `nat.nontrivial ["not", "in", "here"] -- fails giving the location 'data/nat/basic.lean' ``` This test makes sure that the comment referring to this example is in the file claimed in the doc-module to this counterexample. -/ meta def guard_decl_in_file (na : name) (loc : list string) : tactic unit := do env ← tactic.get_env, some fil ← pure $ env.decl_olean na | fail!"the instance `{na}` is not imported!", let path : string := ⟨list.drop_until "/src/".to_list fil.to_list⟩, let locdot : string := ".".intercalate loc, guard (fil.ends_with ("src/" ++ "/".intercalate loc ++ ".lean")) <|> fail!("instance `{na}` is no longer in `{locdot}`.\n\n" ++ "Please, update the doc-module and this check with the correct location:\n\n'{path}'\n") #eval guard_decl_in_file `finsupp.lex.covariant_class_le_left ["data", "finsupp", "lex"] #eval guard_decl_in_file `finsupp.lex.covariant_class_le_right ["data", "finsupp", "lex"] namespace F instance : has_zero F := ⟨F.zero⟩ /-- `1` is not really needed, but it is nice to use the notation. -/ instance : has_one F := ⟨F.one⟩ /-- A tactic to prove trivial goals by enumeration. -/ meta def boom : tactic unit := `[ repeat { rintro ⟨⟩ }; dec_trivial ] /-- `val` maps `0 1 : F` to their counterparts in `ℕ`. We use it to lift the linear order on `ℕ`. -/ def val : F → ℕ | 0 := 0 | 1 := 1 instance : linear_order F := linear_order.lift' val (by boom) @[simp] lemma z01 : (0 : F) < 1 := by boom /-- `F` would be a `comm_semiring`, using `min` as multiplication. Again, we do not need this. -/ instance : add_comm_monoid F := { add := max, add_assoc := by boom, zero := 0, zero_add := by boom, add_zero := by boom, add_comm := by boom } /-- The `covariant_class`es asserting monotonicity of addition hold for `F`. -/ instance covariant_class_add_le : covariant_class F F (+) (≤) := ⟨by boom⟩ example : covariant_class F F (function.swap (+)) (≤) := by apply_instance /-- The following examples show that `F` has all the typeclasses used by `finsupp.lex.covariant_class_le_left`... -/ example : linear_order F := by apply_instance example : add_monoid F := by apply_instance /-- ... except for the strict monotonicity of addition, the crux of the matter. -/ example : ¬ covariant_class F F (+) (<) := λ h, lt_irrefl 1 $ (h.elim : covariant F F (+) (<)) 1 z01 /-- A few `simp`-lemmas to take care of trivialities in the proof of the example below. -/ @[simp] lemma f1 : ∀ (a : F), 1 + a = 1 := by boom @[simp] lemma f011 : of_lex (single (0 : F) (1 : F)) 1 = 0 := single_apply_eq_zero.mpr (λ h, h) @[simp] lemma f010 : of_lex (single (0 : F) (1 : F)) 0 = 1 := single_eq_same @[simp] lemma f111 : of_lex (single (1 : F) (1 : F)) 1 = 1 := single_eq_same @[simp] lemma f110 : of_lex (single (1 : F) (1 : F)) 0 = 0 := single_apply_eq_zero.mpr (λ h, h.symm) /-- Here we see that (not-necessarily strict) monotonicity of addition on `lex (F →₀ F)` is not a consequence of monotonicity of addition on `F`. Strict monotonicity of addition on `F` is enough and is the content of `finsupp.lex.covariant_class_le_left`. -/ example : ¬ covariant_class (lex (F →₀ F)) (lex (F →₀ F)) (+) (≤) := begin rintro ⟨h⟩, refine not_lt.mpr (h (single (0 : F) (1 : F)) (_ : single 1 1 ≤ single 0 1)) ⟨1, _⟩, { exact or.inr ⟨0, by simp [(by boom : ∀ j : F, j < 0 ↔ false)]⟩ }, { simp only [(by boom : ∀ j : F, j < 1 ↔ j = 0), of_lex_add, coe_add, pi.to_lex_apply, pi.add_apply, forall_eq, f010, f1, eq_self_iff_true, f011, f111, zero_add, and_self] }, end example {α} [ring α] [nontrivial α] : ∃ f g : add_monoid_algebra α F, f ≠ 0 ∧ g ≠ 0 ∧ f * g = 0 := zero_divisors_of_periodic (1 : F) le_rfl (by simp [two_smul]) (z01.ne') example {α} [has_zero α] : 2 • (single 0 1 : α →₀ F) = single 0 1 ∧ (single 0 1 : α →₀ F) ≠ 0 := ⟨smul_single _ _ _, by simpa only [ne.def, single_eq_zero] using z01.ne⟩ end F /-- A Type that does not have `unique_prods`. -/ example : ¬ unique_prods ℕ := begin rintros ⟨h⟩, refine not_not.mpr (h (finset.singleton_nonempty 0) (finset.insert_nonempty 0 {1})) _, suffices : (∃ (x : ℕ), (x = 0 ∨ x = 1) ∧ ¬x = 0) ∧ ∃ (x : ℕ), (x = 0 ∨ x = 1) ∧ ¬x = 1, { simpa [unique_mul] }, exact ⟨⟨1, by simp⟩, ⟨0, by simp⟩⟩, end /-- Some Types that do not have `unique_sums`. -/ example (n : ℕ) (n2 : 2 ≤ n): ¬ unique_sums (zmod n) := begin haveI : fintype (zmod n) := @zmod.fintype n ⟨(zero_lt_two.trans_le n2).ne'⟩, haveI : nontrivial (zmod n) := char_p.nontrivial_of_char_ne_one (one_lt_two.trans_le n2).ne', rintros ⟨h⟩, refine not_not.mpr (h finset.univ_nonempty finset.univ_nonempty) _, suffices : ∀ (x y : zmod n), ∃ (x' y' : zmod n), x' + y' = x + y ∧ (x' = x → ¬y' = y), { simpa [unique_add] }, exact λ x y, ⟨x - 1, y + 1, sub_add_add_cancel _ _ _, by simp⟩, end
3f72474c82763180294a0c3377635145af31cb81
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/linear_algebra/basic.lean
039f434125b7f20cb54650895ca6baa4b62205cc
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
80,324
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, Kevin Buzzard -/ import algebra.pi_instances import data.finsupp /-! # Linear algebra This file defines the basics of linear algebra. It sets up the "categorical/lattice structure" of modules over a ring, submodules, and linear maps. If `p` and `q` are submodules of a module, `p ≤ q` means that `p ⊆ q`. Many of the relevant definitions, including `module`, `submodule`, and `linear_map`, are found in `src/algebra/module.lean`. ## Main definitions * Many constructors for linear maps, including `prod` and `coprod` * `submodule.span s` is defined to be the smallest submodule containing the set `s`. * If `p` is a submodule of `M`, `submodule.quotient p` is the quotient of `M` with respect to `p`: that is, elements of `M` are identified if their difference is in `p`. This is itself a module. * The kernel `ker` and range `range` of a linear map are submodules of the domain and codomain respectively. * `linear_equiv M M₂`, the type of linear equivalences between `M` and `M₂`, is a structure that extends `linear_map` and `equiv`. * The general linear group is defined to be the group of invertible linear maps from `M` to itself. ## Main statements * The first and second isomorphism laws for modules are proved as `quot_ker_equiv_range` and `quotient_inf_equiv_sup_quotient`. ## Notations * We continue to use the notation `M →ₗ[R] M₂` for the type of linear maps from `M` to `M₂` over the ring `R`. * We introduce the notations `M ≃ₗ M₂` and `M ≃ₗ[R] M₂` for `linear_equiv M M₂`. In the first, the ring `R` is implicit. ## Implementation notes We note that, when constructing linear maps, it is convenient to use operations defined on bundled maps (`prod`, `coprod`, arithmetic operations like `+`) instead of defining a function and proving it is linear. ## Tags linear algebra, vector space, module -/ open function reserve infix ` ≃ₗ `:25 universes u v w x y z u' v' w' y' variables {R : Type u} {K : Type u'} {M : Type v} {V : Type v'} {M₂ : Type w} {V₂ : Type w'} variables {M₃ : Type y} {V₃ : Type y'} {M₄ : Type z} {ι : Type x} namespace finsupp lemma smul_sum {α : Type u} {β : Type v} {R : Type w} {M : Type y} [has_zero β] [ring R] [add_comm_group M] [module R M] {v : α →₀ β} {c : R} {h : α → β → M} : c • (v.sum h) = v.sum (λa b, c • h a b) := finset.smul_sum end finsupp section open_locale classical /-- decomposing `x : ι → R` as a sum along the canonical basis -/ lemma pi_eq_sum_univ {ι : Type u} [fintype ι] {R : Type v} [semiring R] (x : ι → R) : x = finset.sum finset.univ (λi:ι, x i • (λj, if i = j then 1 else 0)) := begin ext k, rw pi.finset_sum_apply, have : finset.sum finset.univ (λ (x_1 : ι), x x_1 * ite (k = x_1) 1 0) = x k, by { have := finset.sum_mul_boole finset.univ x k, rwa if_pos (finset.mem_univ _) at this }, rw ← this, apply finset.sum_congr rfl (λl hl, _), simp only [smul_eq_mul, mul_ite, pi.smul_apply], conv_lhs { rw eq_comm } end end namespace linear_map section variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] [add_comm_group M₄] variables [module R M] [module R M₂] [module R M₃] [module R M₄] variables (f g : M →ₗ[R] M₂) include R @[simp] theorem comp_id : f.comp id = f := linear_map.ext $ λ x, rfl @[simp] theorem id_comp : id.comp f = f := linear_map.ext $ λ x, rfl theorem comp_assoc (g : M₂ →ₗ[R] M₃) (h : M₃ →ₗ[R] M₄) : (h.comp g).comp f = h.comp (g.comp f) := rfl /-- A linear map `f : M₂ → M` whose values lie in a submodule `p ⊆ M` can be restricted to a linear map M₂ → p. -/ def cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (h : ∀c, f c ∈ p) : M₂ →ₗ[R] p := by refine {to_fun := λc, ⟨f c, h c⟩, ..}; intros; apply set_coe.ext; simp @[simp] theorem cod_restrict_apply (p : submodule R M) (f : M₂ →ₗ[R] M) {h} (x : M₂) : (cod_restrict p f h x : M) = f x := rfl @[simp] lemma comp_cod_restrict (p : submodule R M₂) (h : ∀b, f b ∈ p) (g : M₃ →ₗ[R] M) : (cod_restrict p f h).comp g = cod_restrict p (f.comp g) (assume b, h _) := ext $ assume b, rfl @[simp] lemma subtype_comp_cod_restrict (p : submodule R M₂) (h : ∀b, f b ∈ p) : p.subtype.comp (cod_restrict p f h) = f := ext $ assume b, rfl /-- If a function `g` is a left and right inverse of a linear map `f`, then `g` is linear itself. -/ def inverse (g : M₂ → M) (h₁ : left_inverse g f) (h₂ : right_inverse g f) : M₂ →ₗ[R] M := by dsimp [left_inverse, function.right_inverse] at h₁ h₂; exact ⟨g, λ x y, by rw [← h₁ (g (x + y)), ← h₁ (g x + g y)]; simp [h₂], λ a b, by rw [← h₁ (g (a • b)), ← h₁ (a • g b)]; simp [h₂]⟩ /-- The constant 0 map is linear. -/ instance : has_zero (M →ₗ[R] M₂) := ⟨⟨λ _, 0, by simp, by simp⟩⟩ instance : inhabited (M →ₗ[R] M₂) := ⟨0⟩ @[simp] lemma zero_apply (x : M) : (0 : M →ₗ[R] M₂) x = 0 := rfl /-- The negation of a linear map is linear. -/ instance : has_neg (M →ₗ[R] M₂) := ⟨λ f, ⟨λ b, - f b, by simp [add_comm], by simp⟩⟩ @[simp] lemma neg_apply (x : M) : (- f) x = - f x := rfl /-- The sum of two linear maps is linear. -/ instance : has_add (M →ₗ[R] M₂) := ⟨λ f g, ⟨λ b, f b + g b, by simp [add_comm, add_left_comm], by simp [smul_add]⟩⟩ @[simp] lemma add_apply (x : M) : (f + g) x = f x + g x := rfl /-- The type of linear maps is an additive group. -/ instance : add_comm_group (M →ₗ[R] M₂) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext; simp [add_comm, add_left_comm] instance linear_map.is_add_group_hom : is_add_group_hom f := { map_add := f.add } instance linear_map_apply_is_add_group_hom (a : M) : is_add_group_hom (λ f : M →ₗ[R] M₂, f a) := { map_add := λ f g, linear_map.add_apply f g a } lemma sum_apply (t : finset ι) (f : ι → M →ₗ[R] M₂) (b : M) : t.sum f b = t.sum (λd, f d b) := (t.sum_hom (λ g : M →ₗ[R] M₂, g b)).symm @[simp] lemma sub_apply (x : M) : (f - g) x = f x - g x := rfl /-- `λb, f b • x` is a linear map. -/ def smul_right (f : M₂ →ₗ[R] R) (x : M) : M₂ →ₗ[R] M := ⟨λb, f b • x, by simp [add_smul], by simp [smul_smul]⟩. @[simp] theorem smul_right_apply (f : M₂ →ₗ[R] R) (x : M) (c : M₂) : (smul_right f x : M₂ → M) c = f c • x := rfl instance : has_one (M →ₗ[R] M) := ⟨linear_map.id⟩ instance : has_mul (M →ₗ[R] M) := ⟨linear_map.comp⟩ @[simp] lemma one_app (x : M) : (1 : M →ₗ[R] M) x = x := rfl @[simp] lemma mul_app (A B : M →ₗ[R] M) (x : M) : (A * B) x = A (B x) := rfl @[simp] theorem comp_zero : f.comp (0 : M₃ →ₗ[R] M) = 0 := ext $ assume c, by rw [comp_apply, zero_apply, zero_apply, f.map_zero] @[simp] theorem zero_comp : (0 : M₂ →ₗ[R] M₃).comp f = 0 := rfl @[norm_cast] lemma coe_fn_sum {ι : Type*} (t : finset ι) (f : ι → M →ₗ[R] M₂) : ⇑(t.sum f) = t.sum (λ i, (f i : M → M₂)) := add_monoid_hom.map_sum ⟨@to_fun R M M₂ _ _ _ _ _, rfl, λ x y, rfl⟩ _ _ section variables (R M) include M instance endomorphism_ring : ring (M →ₗ[R] M) := by refine {mul := (*), one := 1, ..linear_map.add_comm_group, ..}; { intros, apply linear_map.ext, simp {proj := ff} } end section open_locale classical /-- A linear map `f` applied to `x : ι → R` can be computed using the image under `f` of elements of the canonical basis. -/ lemma pi_apply_eq_sum_univ [fintype ι] (f : (ι → R) →ₗ[R] M) (x : ι → R) : f x = finset.sum finset.univ (λi:ι, x i • (f (λj, if i = j then 1 else 0))) := begin conv_lhs { rw [pi_eq_sum_univ x, f.map_sum] }, apply finset.sum_congr rfl (λl hl, _), rw f.map_smul end end section variables (R M M₂) /-- The first projection of a product is a linear map. -/ def fst : M × M₂ →ₗ[R] M := ⟨prod.fst, λ x y, rfl, λ x y, rfl⟩ /-- The second projection of a product is a linear map. -/ def snd : M × M₂ →ₗ[R] M₂ := ⟨prod.snd, λ x y, rfl, λ x y, rfl⟩ end @[simp] theorem fst_apply (x : M × M₂) : fst R M M₂ x = x.1 := rfl @[simp] theorem snd_apply (x : M × M₂) : snd R M M₂ x = x.2 := rfl /-- The prod of two linear maps is a linear map. -/ def prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : M →ₗ[R] M₂ × M₃ := { to_fun := λ x, (f x, g x), add := λ x y, by simp only [prod.mk_add_mk, map_add], smul := λ c x, by simp only [prod.smul_mk, map_smul] } @[simp] theorem prod_apply (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) (x : M) : prod f g x = (f x, g x) := rfl @[simp] theorem fst_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (fst R M₂ M₃).comp (prod f g) = f := by ext; refl @[simp] theorem snd_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (snd R M₂ M₃).comp (prod f g) = g := by ext; refl @[simp] theorem pair_fst_snd : prod (fst R M M₂) (snd R M M₂) = linear_map.id := by ext; refl section variables (R M M₂) /-- The left injection into a product is a linear map. -/ def inl : M →ₗ[R] M × M₂ := by refine ⟨prod.inl, _, _⟩; intros; simp [prod.inl] /-- The right injection into a product is a linear map. -/ def inr : M₂ →ₗ[R] M × M₂ := by refine ⟨prod.inr, _, _⟩; intros; simp [prod.inr] end @[simp] theorem inl_apply (x : M) : inl R M M₂ x = (x, 0) := rfl @[simp] theorem inr_apply (x : M₂) : inr R M M₂ x = (0, x) := rfl /-- The coprod function `λ x : M × M₂, f x.1 + g x.2` is a linear map. -/ def coprod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : M × M₂ →ₗ[R] M₃ := { to_fun := λ x, f x.1 + g x.2, add := λ x y, by simp only [map_add, prod.snd_add, prod.fst_add]; cc, smul := λ x y, by simp only [smul_add, prod.smul_snd, prod.smul_fst, map_smul] } @[simp] theorem coprod_apply (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (x : M) (y : M₂) : coprod f g (x, y) = f x + g y := rfl @[simp] theorem coprod_inl (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (coprod f g).comp (inl R M M₂) = f := by ext; simp only [map_zero, add_zero, coprod_apply, inl_apply, comp_apply] @[simp] theorem coprod_inr (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (coprod f g).comp (inr R M M₂) = g := by ext; simp only [map_zero, coprod_apply, inr_apply, zero_add, comp_apply] @[simp] theorem coprod_inl_inr : coprod (inl R M M₂) (inr R M M₂) = linear_map.id := by ext ⟨x, y⟩; simp only [prod.mk_add_mk, add_zero, id_apply, coprod_apply, inl_apply, inr_apply, zero_add] theorem fst_eq_coprod : fst R M M₂ = coprod linear_map.id 0 := by ext ⟨x, y⟩; simp theorem snd_eq_coprod : snd R M M₂ = coprod 0 linear_map.id := by ext ⟨x, y⟩; simp theorem inl_eq_prod : inl R M M₂ = prod linear_map.id 0 := rfl theorem inr_eq_prod : inr R M M₂ = prod 0 linear_map.id := rfl /-- `prod.map` of two linear maps. -/ def prod_map (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) : (M × M₂) →ₗ[R] (M₃ × M₄) := (f.comp (fst R M M₂)).prod (g.comp (snd R M M₂)) @[simp] theorem prod_map_apply (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) (x) : f.prod_map g x = (f x.1, g x.2) := rfl end section comm_ring variables [comm_ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] variables (f g : M →ₗ[R] M₂) include R instance : has_scalar R (M →ₗ[R] M₂) := ⟨λ a f, ⟨λ b, a • f b, by simp [smul_add], by simp [smul_smul, mul_comm]⟩⟩ @[simp] lemma smul_apply (a : R) (x : M) : (a • f) x = a • f x := rfl instance : module R (M →ₗ[R] M₂) := module.of_core $ by refine { smul := (•), ..}; intros; ext; simp [smul_add, add_smul, smul_smul] /-- Composition by `f : M₂ → M₃` is a linear map from the space of linear maps `M → M₂` to the space of linear maps `M₂ → M₃`. -/ def comp_right (f : M₂ →ₗ[R] M₃) : (M →ₗ[R] M₂) →ₗ[R] (M →ₗ[R] M₃) := ⟨linear_map.comp f, λ _ _, linear_map.ext $ λ _, f.2 _ _, λ _ _, linear_map.ext $ λ _, f.3 _ _⟩ theorem smul_comp (g : M₂ →ₗ[R] M₃) (a : R) : (a • g).comp f = a • (g.comp f) := rfl theorem comp_smul (g : M₂ →ₗ[R] M₃) (a : R) : g.comp (a • f) = a • (g.comp f) := ext $ assume b, by rw [comp_apply, smul_apply, g.map_smul]; refl /-- The family of linear maps `M₂ → M` parameterised by `f ∈ M₂ → R`, `x ∈ M`, is linear in `f`, `x`. -/ def smul_rightₗ : (M₂ →ₗ[R] R) →ₗ[R] M →ₗ[R] M₂ →ₗ[R] M := { to_fun := λ f, { to_fun := linear_map.smul_right f, add := λ m m', by { ext, apply smul_add, }, smul := λ c m, by { ext, apply smul_comm, } }, add := λ f f', by { ext, apply add_smul, }, smul := λ c f, by { ext, apply mul_smul, } } @[simp] lemma smul_rightₗ_apply (f : M₂ →ₗ[R] R) (x : M) (c : M₂) : (smul_rightₗ : (M₂ →ₗ R) →ₗ M →ₗ M₂ →ₗ M) f x c = (f c) • x := rfl end comm_ring end linear_map namespace submodule variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] variables (p p' : submodule R M) (q q' : submodule R M₂) variables {r : R} {x y : M} open set instance : partial_order (submodule R M) := partial_order.lift (coe : submodule R M → set M) (λ a b, ext') (by apply_instance) variables {p p'} lemma le_def : p ≤ p' ↔ (p : set M) ⊆ p' := iff.rfl lemma le_def' : p ≤ p' ↔ ∀ x ∈ p, x ∈ p' := iff.rfl lemma lt_def : p < p' ↔ (p : set M) ⊂ p' := iff.rfl lemma not_le_iff_exists : ¬ (p ≤ p') ↔ ∃ x ∈ p, x ∉ p' := not_subset lemma exists_of_lt {p p' : submodule R M} : p < p' → ∃ x ∈ p', x ∉ p := exists_of_ssubset lemma lt_iff_le_and_exists : p < p' ↔ p ≤ p' ∧ ∃ x ∈ p', x ∉ p := by rw [lt_iff_le_not_le, not_le_iff_exists] /-- If two submodules p and p' satisfy p ⊆ p', then `of_le p p'` is the linear map version of this inclusion. -/ def of_le (h : p ≤ p') : p →ₗ[R] p' := p.subtype.cod_restrict p' $ λ ⟨x, hx⟩, h hx @[simp] theorem coe_of_le (h : p ≤ p') (x : p) : (of_le h x : M) = x := rfl theorem of_le_apply (h : p ≤ p') (x : p) : of_le h x = ⟨x, h x.2⟩ := rfl variables (p p') lemma subtype_comp_of_le (p q : submodule R M) (h : p ≤ q) : q.subtype.comp (of_le h) = p.subtype := by { ext ⟨b, hb⟩, refl } /-- The set `{0}` is the bottom element of the lattice of submodules. -/ instance : has_bot (submodule R M) := ⟨by split; try {exact {0}}; simp {contextual := tt}⟩ instance inhabited' : inhabited (submodule R M) := ⟨⊥⟩ @[simp] lemma bot_coe : ((⊥ : submodule R M) : set M) = {0} := rfl section variables (R) @[simp] lemma mem_bot : x ∈ (⊥ : submodule R M) ↔ x = 0 := mem_singleton_iff end instance : order_bot (submodule R M) := { bot := ⊥, bot_le := λ p x, by simp {contextual := tt}, ..submodule.partial_order } /-- The universal set is the top element of the lattice of submodules. -/ instance : has_top (submodule R M) := ⟨by split; try {exact set.univ}; simp⟩ @[simp] lemma top_coe : ((⊤ : submodule R M) : set M) = univ := rfl @[simp] lemma mem_top : x ∈ (⊤ : submodule R M) := trivial lemma eq_bot_of_zero_eq_one (zero_eq_one : (0 : R) = 1) : p = ⊥ := by ext x; simp [semimodule.eq_zero_of_zero_eq_one x zero_eq_one] instance : order_top (submodule R M) := { top := ⊤, le_top := λ p x _, trivial, ..submodule.partial_order } instance : has_Inf (submodule R M) := ⟨λ S, { carrier := ⋂ s ∈ S, ↑s, zero := by simp, add := by simp [add_mem] {contextual := tt}, smul := by simp [smul_mem] {contextual := tt} }⟩ private lemma Inf_le' {S : set (submodule R M)} {p} : p ∈ S → Inf S ≤ p := bInter_subset_of_mem private lemma le_Inf' {S : set (submodule R M)} {p} : (∀p' ∈ S, p ≤ p') → p ≤ Inf S := subset_bInter instance : has_inf (submodule R M) := ⟨λ p p', { carrier := p ∩ p', zero := by simp, add := by simp [add_mem] {contextual := tt}, smul := by simp [smul_mem] {contextual := tt} }⟩ instance : complete_lattice (submodule R M) := { sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x}, le_sup_left := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, ha, le_sup_right := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, hb, sup_le := λ a b c h₁ h₂, Inf_le' ⟨h₁, h₂⟩, inf := (⊓), le_inf := λ a b c, subset_inter, inf_le_left := λ a b, inter_subset_left _ _, inf_le_right := λ a b, inter_subset_right _ _, Sup := λtt, Inf {t | ∀t'∈tt, t' ≤ t}, le_Sup := λ s p hs, le_Inf' $ λ p' hp', hp' _ hs, Sup_le := λ s p hs, Inf_le' hs, Inf := Inf, le_Inf := λ s a, le_Inf', Inf_le := λ s a, Inf_le', ..submodule.order_top, ..submodule.order_bot } instance : add_comm_monoid (submodule R M) := { add := (⊔), add_assoc := λ _ _ _, sup_assoc, zero := ⊥, zero_add := λ _, bot_sup_eq, add_zero := λ _, sup_bot_eq, add_comm := λ _ _, sup_comm } @[simp] lemma add_eq_sup (p q : submodule R M) : p + q = p ⊔ q := rfl @[simp] lemma zero_eq_bot : (0 : submodule R M) = ⊥ := rfl lemma eq_top_iff' {p : submodule R M} : p = ⊤ ↔ ∀ x, x ∈ p := eq_top_iff.trans ⟨λ h x, @h x trivial, λ h x _, h x⟩ @[simp] theorem inf_coe : (p ⊓ p' : set M) = p ∩ p' := rfl @[simp] theorem mem_inf {p p' : submodule R M} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[simp] theorem Inf_coe (P : set (submodule R M)) : (↑(Inf P) : set M) = ⋂ p ∈ P, ↑p := rfl @[simp] theorem infi_coe {ι} (p : ι → submodule R M) : (↑⨅ i, p i : set M) = ⋂ i, ↑(p i) := by rw [infi, Inf_coe]; ext a; simp; exact ⟨λ h i, h _ i rfl, λ h i x e, e ▸ h _⟩ @[simp] theorem mem_infi {ι} (p : ι → submodule R M) : x ∈ (⨅ i, p i) ↔ ∀ i, x ∈ p i := by rw [← mem_coe, infi_coe, mem_Inter]; refl theorem disjoint_def {p p' : submodule R M} : disjoint p p' ↔ ∀ x ∈ p, x ∈ p' → x = (0:M) := show (∀ x, x ∈ p ∧ x ∈ p' → x ∈ ({0} : set M)) ↔ _, by simp /-- The pushforward of a submodule `p ⊆ M` by `f : M → M₂` -/ def map (f : M →ₗ[R] M₂) (p : submodule R M) : submodule R M₂ := { carrier := f '' p, zero := ⟨0, p.zero_mem, f.map_zero⟩, add := by rintro _ _ ⟨b₁, hb₁, rfl⟩ ⟨b₂, hb₂, rfl⟩; exact ⟨_, p.add_mem hb₁ hb₂, f.map_add _ _⟩, smul := by rintro a _ ⟨b, hb, rfl⟩; exact ⟨_, p.smul_mem _ hb, f.map_smul _ _⟩ } @[simp] lemma map_coe (f : M →ₗ[R] M₂) (p : submodule R M) : (map f p : set M₂) = f '' p := rfl @[simp] lemma mem_map {f : M →ₗ[R] M₂} {p : submodule R M} {x : M₂} : x ∈ map f p ↔ ∃ y, y ∈ p ∧ f y = x := iff.rfl theorem mem_map_of_mem {f : M →ₗ[R] M₂} {p : submodule R M} {r} (h : r ∈ p) : f r ∈ map f p := set.mem_image_of_mem _ h lemma map_id : map linear_map.id p = p := submodule.ext $ λ a, by simp lemma map_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) (p : submodule R M) : map (g.comp f) p = map g (map f p) := submodule.ext' $ by simp [map_coe]; rw ← image_comp lemma map_mono {f : M →ₗ[R] M₂} {p p' : submodule R M} : p ≤ p' → map f p ≤ map f p' := image_subset _ @[simp] lemma map_zero : map (0 : M →ₗ[R] M₂) p = ⊥ := have ∃ (x : M), x ∈ p := ⟨0, p.zero_mem⟩, ext $ by simp [this, eq_comm] /-- The pullback of a submodule `p ⊆ M₂` along `f : M → M₂` -/ def comap (f : M →ₗ[R] M₂) (p : submodule R M₂) : submodule R M := { carrier := f ⁻¹' p, zero := by simp, add := λ x y h₁ h₂, by simp [p.add_mem h₁ h₂], smul := λ a x h, by simp [p.smul_mem _ h] } @[simp] lemma comap_coe (f : M →ₗ[R] M₂) (p : submodule R M₂) : (comap f p : set M) = f ⁻¹' p := rfl @[simp] lemma mem_comap {f : M →ₗ[R] M₂} {p : submodule R M₂} : x ∈ comap f p ↔ f x ∈ p := iff.rfl lemma comap_id : comap linear_map.id p = p := submodule.ext' rfl lemma comap_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) (p : submodule R M₃) : comap (g.comp f) p = comap f (comap g p) := rfl lemma comap_mono {f : M →ₗ[R] M₂} {q q' : submodule R M₂} : q ≤ q' → comap f q ≤ comap f q' := preimage_mono lemma map_le_iff_le_comap {f : M →ₗ[R] M₂} {p : submodule R M} {q : submodule R M₂} : map f p ≤ q ↔ p ≤ comap f q := image_subset_iff lemma gc_map_comap (f : M →ₗ[R] M₂) : galois_connection (map f) (comap f) | p q := map_le_iff_le_comap @[simp] lemma map_bot (f : M →ₗ[R] M₂) : map f ⊥ = ⊥ := (gc_map_comap f).l_bot @[simp] lemma map_sup (f : M →ₗ[R] M₂) : map f (p ⊔ p') = map f p ⊔ map f p' := (gc_map_comap f).l_sup @[simp] lemma map_supr {ι : Sort*} (f : M →ₗ[R] M₂) (p : ι → submodule R M) : map f (⨆i, p i) = (⨆i, map f (p i)) := (gc_map_comap f).l_supr @[simp] lemma comap_top (f : M →ₗ[R] M₂) : comap f ⊤ = ⊤ := rfl @[simp] lemma comap_inf (f : M →ₗ[R] M₂) : comap f (q ⊓ q') = comap f q ⊓ comap f q' := rfl @[simp] lemma comap_infi {ι : Sort*} (f : M →ₗ[R] M₂) (p : ι → submodule R M₂) : comap f (⨅i, p i) = (⨅i, comap f (p i)) := (gc_map_comap f).u_infi @[simp] lemma comap_zero : comap (0 : M →ₗ[R] M₂) q = ⊤ := ext $ by simp lemma map_comap_le (f : M →ₗ[R] M₂) (q : submodule R M₂) : map f (comap f q) ≤ q := (gc_map_comap f).l_u_le _ lemma le_comap_map (f : M →ₗ[R] M₂) (p : submodule R M) : p ≤ comap f (map f p) := (gc_map_comap f).le_u_l _ --TODO(Mario): is there a way to prove this from order properties? lemma map_inf_eq_map_inf_comap {f : M →ₗ[R] M₂} {p : submodule R M} {p' : submodule R M₂} : map f p ⊓ p' = map f (p ⊓ comap f p') := le_antisymm (by rintro _ ⟨⟨x, h₁, rfl⟩, h₂⟩; exact ⟨_, ⟨h₁, h₂⟩, rfl⟩) (le_inf (map_mono inf_le_left) (map_le_iff_le_comap.2 inf_le_right)) lemma map_comap_subtype : map p.subtype (comap p.subtype p') = p ⊓ p' := ext $ λ x, ⟨by rintro ⟨⟨_, h₁⟩, h₂, rfl⟩; exact ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨⟨_, h₁⟩, h₂, rfl⟩⟩ lemma eq_zero_of_bot_submodule : ∀(b : (⊥ : submodule R M)), b = 0 | ⟨b', hb⟩ := subtype.eq $ show b' = 0, from (mem_bot R).1 hb section variables (R) /-- The span of a set `s ⊆ M` is the smallest submodule of M that contains `s`. -/ def span (s : set M) : submodule R M := Inf {p | s ⊆ p} end variables {s t : set M} lemma mem_span : x ∈ span R s ↔ ∀ p : submodule R M, s ⊆ p → x ∈ p := mem_bInter_iff lemma subset_span : s ⊆ span R s := λ x h, mem_span.2 $ λ p hp, hp h lemma span_le {p} : span R s ≤ p ↔ s ⊆ p := ⟨subset.trans subset_span, λ ss x h, mem_span.1 h _ ss⟩ lemma span_mono (h : s ⊆ t) : span R s ≤ span R t := span_le.2 $ subset.trans h subset_span lemma span_eq_of_le (h₁ : s ⊆ p) (h₂ : p ≤ span R s) : span R s = p := le_antisymm (span_le.2 h₁) h₂ @[simp] lemma span_eq : span R (p : set M) = p := span_eq_of_le _ (subset.refl _) subset_span /-- An induction principle for span membership. If `p` holds for 0 and all elements of `s`, and is preserved under addition and scalar multiplication, then `p` holds for all elements of the span of `s`. -/ @[elab_as_eliminator] lemma span_induction {p : M → Prop} (h : x ∈ span R s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : ∀ x y, p x → p y → p (x + y)) (H2 : ∀ (a:R) x, p x → p (a • x)) : p x := (@span_le _ _ _ _ _ _ ⟨p, H0, H1, H2⟩).2 Hs h section variables (R M) /-- `span` forms a Galois insertion with the coercion from submodule to set. -/ protected def gi : galois_insertion (@span R M _ _ _) coe := { choice := λ s _, span R s, gc := λ s t, span_le, le_l_u := λ s, subset_span, choice_eq := λ s h, rfl } end @[simp] lemma span_empty : span R (∅ : set M) = ⊥ := (submodule.gi R M).gc.l_bot @[simp] lemma span_univ : span R (univ : set M) = ⊤ := eq_top_iff.2 $ le_def.2 $ subset_span lemma span_union (s t : set M) : span R (s ∪ t) = span R s ⊔ span R t := (submodule.gi R M).gc.l_sup lemma span_Union {ι} (s : ι → set M) : span R (⋃ i, s i) = ⨆ i, span R (s i) := (submodule.gi R M).gc.l_supr @[simp] theorem coe_supr_of_directed {ι} [hι : nonempty ι] (S : ι → submodule R M) (H : directed (≤) S) : ((supr S : submodule R M) : set M) = ⋃ i, S i := begin refine subset.antisymm _ (Union_subset $ le_supr S), suffices : (span R (⋃ i, (S i : set M)) : set M) ⊆ ⋃ (i : ι), ↑(S i), by simpa only [span_Union, span_eq] using this, refine (λ x hx, span_induction hx (λ _, id) _ _ _); simp only [mem_Union, exists_imp_distrib], { exact hι.elim (λ i, ⟨i, (S i).zero_mem⟩) }, { intros x y i hi j hj, rcases H i j with ⟨k, ik, jk⟩, exact ⟨k, add_mem _ (ik hi) (jk hj)⟩ }, { exact λ a x i hi, ⟨i, smul_mem _ a hi⟩ }, end lemma mem_supr_of_mem {ι : Sort*} {b : M} (p : ι → submodule R M) (i : ι) (h : b ∈ p i) : b ∈ (⨆i, p i) := have p i ≤ (⨆i, p i) := le_supr p i, @this b h @[simp] theorem mem_supr_of_directed {ι} [nonempty ι] (S : ι → submodule R M) (H : directed (≤) S) {x} : x ∈ supr S ↔ ∃ i, x ∈ S i := by { rw [← mem_coe, coe_supr_of_directed S H, mem_Union], refl } theorem mem_Sup_of_directed {s : set (submodule R M)} {z} (hs : s.nonempty) (hdir : directed_on (≤) s) : z ∈ Sup s ↔ ∃ y ∈ s, z ∈ y := begin haveI : nonempty s := hs.to_subtype, rw [Sup_eq_supr, supr_subtype', mem_supr_of_directed, subtype.exists], exact (directed_on_iff_directed _).1 hdir end section variables {p p'} lemma mem_sup : x ∈ p ⊔ p' ↔ ∃ (y ∈ p) (z ∈ p'), y + z = x := ⟨λ h, begin rw [← span_eq p, ← span_eq p', ← span_union] at h, apply span_induction h, { rintro y (h | h), { exact ⟨y, h, 0, by simp, by simp⟩ }, { exact ⟨0, by simp, y, h, by simp⟩ } }, { exact ⟨0, by simp, 0, by simp⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩, exact ⟨_, add_mem _ hy₁ hy₂, _, add_mem _ hz₁ hz₂, by simp; cc⟩ }, { rintro a _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, smul_mem _ a hy, _, smul_mem _ a hz, by simp [smul_add]⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact add_mem _ ((le_sup_left : p ≤ p ⊔ p') hy) ((le_sup_right : p' ≤ p ⊔ p') hz)⟩ lemma mem_sup' : x ∈ p ⊔ p' ↔ ∃ (y : p) (z : p'), (y:M) + z = x := mem_sup.trans $ by simp only [submodule.exists, coe_mk] end lemma mem_span_singleton {y : M} : x ∈ span R ({y} : set M) ↔ ∃ a:R, a • y = x := ⟨λ h, begin apply span_induction h, { rintro y (rfl|⟨⟨⟩⟩), exact ⟨1, by simp⟩ }, { exact ⟨0, by simp⟩ }, { rintro _ _ ⟨a, rfl⟩ ⟨b, rfl⟩, exact ⟨a + b, by simp [add_smul]⟩ }, { rintro a _ ⟨b, rfl⟩, exact ⟨a * b, by simp [smul_smul]⟩ } end, by rintro ⟨a, y, rfl⟩; exact smul_mem _ _ (subset_span $ by simp)⟩ lemma span_singleton_eq_range (y : M) : (span R ({y} : set M) : set M) = range ((• y) : R → M) := set.ext $ λ x, mem_span_singleton lemma disjoint_span_singleton {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {s : submodule K E} {x : E} : disjoint s (span K {x}) ↔ (x ∈ s → x = 0) := begin refine disjoint_def.trans ⟨λ H hx, H x hx $ subset_span $ mem_singleton x, _⟩, assume H y hy hyx, obtain ⟨c, hc⟩ := mem_span_singleton.1 hyx, subst y, classical, by_cases hc : c = 0, by simp only [hc, zero_smul], rw [s.smul_mem_iff hc] at hy, rw [H hy, smul_zero] end lemma mem_span_insert {y} : x ∈ span R (insert y s) ↔ ∃ (a:R) (z ∈ span R s), x = a • y + z := begin simp only [← union_singleton, span_union, mem_sup, mem_span_singleton, exists_prop, exists_exists_eq_and], rw [exists_comm], simp only [eq_comm, add_comm, exists_and_distrib_left] end lemma mem_span_insert' {y} : x ∈ span R (insert y s) ↔ ∃(a:R), x + a • y ∈ span R s := begin rw mem_span_insert, split, { rintro ⟨a, z, hz, rfl⟩, exact ⟨-a, by simp [hz]⟩ }, { rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp [add_comm, add_left_comm]⟩ } end lemma span_insert_eq_span (h : x ∈ span R s) : span R (insert x s) = span R s := span_eq_of_le _ (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _) lemma span_span : span R (span R s : set M) = span R s := span_eq _ lemma span_eq_bot : span R (s : set M) = ⊥ ↔ ∀ x ∈ s, (x:M) = 0 := eq_bot_iff.trans ⟨ λ H x h, (mem_bot R).1 $ H $ subset_span h, λ H, span_le.2 (λ x h, (mem_bot R).2 $ H x h)⟩ lemma span_singleton_eq_bot : span R ({x} : set M) = ⊥ ↔ x = 0 := span_eq_bot.trans $ by simp @[simp] lemma span_image (f : M →ₗ[R] M₂) : span R (f '' s) = map f (span R s) := span_eq_of_le _ (image_subset _ subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ image_subset_iff.1 subset_span lemma linear_eq_on (s : set M) {f g : M →ₗ[R] M₂} (H : ∀x∈s, f x = g x) {x} (h : x ∈ span R s) : f x = g x := by apply span_induction h H; simp {contextual := tt} lemma supr_eq_span {ι : Sort w} (p : ι → submodule R M) : (⨆ (i : ι), p i) = submodule.span R (⋃ (i : ι), ↑(p i)) := le_antisymm (supr_le $ assume i, subset.trans (assume m hm, set.mem_Union.mpr ⟨i, hm⟩) subset_span) (span_le.mpr $ Union_subset_iff.mpr $ assume i m hm, mem_supr_of_mem _ i hm) lemma span_singleton_le_iff_mem (m : M) (p : submodule R M) : span R {m} ≤ p ↔ m ∈ p := by rw [span_le, singleton_subset_iff, mem_coe] lemma mem_supr {ι : Sort w} (p : ι → submodule R M) {m : M} : (m ∈ ⨆ i, p i) ↔ (∀ N, (∀ i, p i ≤ N) → m ∈ N) := begin rw [← span_singleton_le_iff_mem, le_supr_iff], simp only [span_singleton_le_iff_mem], end /-- The product of two submodules is a submodule. -/ def prod : submodule R (M × M₂) := { carrier := set.prod p q, zero := ⟨zero_mem _, zero_mem _⟩, add := by rintro ⟨x₁, y₁⟩ ⟨x₂, y₂⟩ ⟨hx₁, hy₁⟩ ⟨hx₂, hy₂⟩; exact ⟨add_mem _ hx₁ hx₂, add_mem _ hy₁ hy₂⟩, smul := by rintro a ⟨x, y⟩ ⟨hx, hy⟩; exact ⟨smul_mem _ a hx, smul_mem _ a hy⟩ } @[simp] lemma prod_coe : (prod p q : set (M × M₂)) = set.prod p q := rfl @[simp] lemma mem_prod {p : submodule R M} {q : submodule R M₂} {x : M × M₂} : x ∈ prod p q ↔ x.1 ∈ p ∧ x.2 ∈ q := set.mem_prod lemma span_prod_le (s : set M) (t : set M₂) : span R (set.prod s t) ≤ prod (span R s) (span R t) := span_le.2 $ set.prod_mono subset_span subset_span @[simp] lemma prod_top : (prod ⊤ ⊤ : submodule R (M × M₂)) = ⊤ := by ext; simp @[simp] lemma prod_bot : (prod ⊥ ⊥ : submodule R (M × M₂)) = ⊥ := by ext ⟨x, y⟩; simp [prod.zero_eq_mk] lemma prod_mono {p p' : submodule R M} {q q' : submodule R M₂} : p ≤ p' → q ≤ q' → prod p q ≤ prod p' q' := prod_mono @[simp] lemma prod_inf_prod : prod p q ⊓ prod p' q' = prod (p ⊓ p') (q ⊓ q') := ext' set.prod_inter_prod @[simp] lemma prod_sup_prod : prod p q ⊔ prod p' q' = prod (p ⊔ p') (q ⊔ q') := begin refine le_antisymm (sup_le (prod_mono le_sup_left le_sup_left) (prod_mono le_sup_right le_sup_right)) _, simp [le_def'], intros xx yy hxx hyy, rcases mem_sup.1 hxx with ⟨x, hx, x', hx', rfl⟩, rcases mem_sup.1 hyy with ⟨y, hy, y', hy', rfl⟩, refine mem_sup.2 ⟨(x, y), ⟨hx, hy⟩, (x', y'), ⟨hx', hy'⟩, rfl⟩ end -- TODO(Mario): Factor through add_subgroup /-- The equivalence relation associated to a submodule `p`, defined by `x ≈ y` iff `y - x ∈ p`. -/ def quotient_rel : setoid M := ⟨λ x y, x - y ∈ p, λ x, by simp, λ x y h, by simpa using neg_mem _ h, λ x y z h₁ h₂, by simpa [sub_eq_add_neg, add_left_comm] using add_mem _ h₁ h₂⟩ /-- The quotient of a module `M` by a submodule `p ⊆ M`. -/ def quotient : Type* := quotient (quotient_rel p) namespace quotient /-- Map associating to an element of `M` the corresponding element of `M/p`, when `p` is a submodule of `M`. -/ def mk {p : submodule R M} : M → quotient p := quotient.mk' @[simp] theorem mk_eq_mk {p : submodule R M} (x : M) : (quotient.mk x : quotient p) = mk x := rfl @[simp] theorem mk'_eq_mk {p : submodule R M} (x : M) : (quotient.mk' x : quotient p) = mk x := rfl @[simp] theorem quot_mk_eq_mk {p : submodule R M} (x : M) : (quot.mk _ x : quotient p) = mk x := rfl protected theorem eq {x y : M} : (mk x : quotient p) = mk y ↔ x - y ∈ p := quotient.eq' instance : has_zero (quotient p) := ⟨mk 0⟩ instance : inhabited (quotient p) := ⟨0⟩ @[simp] theorem mk_zero : mk 0 = (0 : quotient p) := rfl @[simp] theorem mk_eq_zero : (mk x : quotient p) = 0 ↔ x ∈ p := by simpa using (quotient.eq p : mk x = 0 ↔ _) instance : has_add (quotient p) := ⟨λ a b, quotient.lift_on₂' a b (λ a b, mk (a + b)) $ λ a₁ a₂ b₁ b₂ h₁ h₂, (quotient.eq p).2 $ by simpa [sub_eq_add_neg, add_left_comm, add_comm] using add_mem p h₁ h₂⟩ @[simp] theorem mk_add : (mk (x + y) : quotient p) = mk x + mk y := rfl instance : has_neg (quotient p) := ⟨λ a, quotient.lift_on' a (λ a, mk (-a)) $ λ a b h, (quotient.eq p).2 $ by simpa using neg_mem p h⟩ @[simp] theorem mk_neg : (mk (-x) : quotient p) = -mk x := rfl instance : add_comm_group (quotient p) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; repeat {rintro ⟨⟩}; simp [-mk_zero, (mk_zero p).symm, -mk_add, (mk_add p).symm, -mk_neg, (mk_neg p).symm]; cc instance : has_scalar R (quotient p) := ⟨λ a x, quotient.lift_on' x (λ x, mk (a • x)) $ λ x y h, (quotient.eq p).2 $ by simpa [smul_sub] using smul_mem p a h⟩ @[simp] theorem mk_smul : (mk (r • x) : quotient p) = r • mk x := rfl instance : module R (quotient p) := module.of_core $ by refine {smul := (•), ..}; repeat {rintro ⟨⟩ <|> intro}; simp [smul_add, add_smul, smul_smul, -mk_add, (mk_add p).symm, -mk_smul, (mk_smul p).symm] end quotient lemma quot_hom_ext ⦃f g : quotient p →ₗ[R] M₂⦄ (h : ∀ x, f (quotient.mk x) = g (quotient.mk x)) : f = g := linear_map.ext $ λ x, quotient.induction_on' x h end submodule namespace submodule variables [field K] variables [add_comm_group V] [vector_space K V] variables [add_comm_group V₂] [vector_space K V₂] lemma comap_smul (f : V →ₗ[K] V₂) (p : submodule K V₂) (a : K) (h : a ≠ 0) : p.comap (a • f) = p.comap f := by ext b; simp only [submodule.mem_comap, p.smul_mem_iff h, linear_map.smul_apply] lemma map_smul (f : V →ₗ[K] V₂) (p : submodule K V) (a : K) (h : a ≠ 0) : p.map (a • f) = p.map f := le_antisymm begin rw [map_le_iff_le_comap, comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end begin rw [map_le_iff_le_comap, ← comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end lemma comap_smul' (f : V →ₗ[K] V₂) (p : submodule K V₂) (a : K) : p.comap (a • f) = (⨅ h : a ≠ 0, p.comap f) := by classical; by_cases a = 0; simp [h, comap_smul] lemma map_smul' (f : V →ₗ[K] V₂) (p : submodule K V) (a : K) : p.map (a • f) = (⨆ h : a ≠ 0, p.map f) := by classical; by_cases a = 0; simp [h, map_smul] end submodule namespace linear_map variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] include R open submodule @[simp] lemma finsupp_sum {R M M₂ γ} [ring R] [add_comm_group M] [module R M] [add_comm_group M₂] [module R M₂] [has_zero γ] (f : M →ₗ[R] M₂) {t : ι →₀ γ} {g : ι → γ → M} : f (t.sum g) = t.sum (λi d, f (g i d)) := f.map_sum theorem map_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (h p') : submodule.map (cod_restrict p f h) p' = comap p.subtype (p'.map f) := submodule.ext $ λ ⟨x, hx⟩, by simp [subtype.coe_ext] theorem comap_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (hf p') : submodule.comap (cod_restrict p f hf) p' = submodule.comap f (map p.subtype p') := submodule.ext $ λ x, ⟨λ h, ⟨⟨_, hf x⟩, h, rfl⟩, by rintro ⟨⟨_, _⟩, h, ⟨⟩⟩; exact h⟩ /-- The range of a linear map `f : M → M₂` is a submodule of `M₂`. -/ def range (f : M →ₗ[R] M₂) : submodule R M₂ := map f ⊤ theorem range_coe (f : M →ₗ[R] M₂) : (range f : set M₂) = set.range f := set.image_univ @[simp] theorem mem_range {f : M →ₗ[R] M₂} : ∀ {x}, x ∈ range f ↔ ∃ y, f y = x := set.ext_iff.1 (range_coe f) theorem mem_range_self (f : M →ₗ[R] M₂) (x : M) : f x ∈ f.range := mem_range.2 ⟨x, rfl⟩ @[simp] theorem range_id : range (linear_map.id : M →ₗ[R] M) = ⊤ := map_id _ theorem range_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : range (g.comp f) = map g (range f) := map_comp _ _ _ theorem range_comp_le_range (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : range (g.comp f) ≤ range g := by rw range_comp; exact map_mono le_top theorem range_eq_top {f : M →ₗ[R] M₂} : range f = ⊤ ↔ surjective f := by rw [← submodule.ext'_iff, range_coe, top_coe, set.range_iff_surjective] lemma range_le_iff_comap {f : M →ₗ[R] M₂} {p : submodule R M₂} : range f ≤ p ↔ comap f p = ⊤ := by rw [range, map_le_iff_le_comap, eq_top_iff] lemma map_le_range {f : M →ₗ[R] M₂} {p : submodule R M} : map f p ≤ range f := map_mono le_top lemma range_coprod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (f.coprod g).range = f.range ⊔ g.range := submodule.ext $ λ x, by simp [mem_sup] lemma sup_range_inl_inr : (inl R M M₂).range ⊔ (inr R M M₂).range = ⊤ := begin refine eq_top_iff'.2 (λ x, mem_sup.2 _), rcases x with ⟨x₁, x₂⟩ , have h₁ : prod.mk x₁ (0 : M₂) ∈ (inl R M M₂).range, by simp, have h₂ : prod.mk (0 : M) x₂ ∈ (inr R M M₂).range, by simp, use [⟨x₁, 0⟩, h₁, ⟨0, x₂⟩, h₂], simp end /-- Restrict the codomain of a linear map `f` to `f.range`. -/ @[reducible] def range_restrict (f : M →ₗ[R] M₂) : M →ₗ[R] f.range := f.cod_restrict f.range f.mem_range_self /-- The kernel of a linear map `f : M → M₂` is defined to be `comap f ⊥`. This is equivalent to the set of `x : M` such that `f x = 0`. The kernel is a submodule of `M`. -/ def ker (f : M →ₗ[R] M₂) : submodule R M := comap f ⊥ @[simp] theorem mem_ker {f : M →ₗ[R] M₂} {y} : y ∈ ker f ↔ f y = 0 := mem_bot R @[simp] theorem ker_id : ker (linear_map.id : M →ₗ[R] M) = ⊥ := rfl @[simp] theorem map_coe_ker (f : M →ₗ[R] M₂) (x : ker f) : f x = 0 := mem_ker.1 x.2 theorem ker_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : ker (g.comp f) = comap f (ker g) := rfl theorem ker_le_ker_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : ker f ≤ ker (g.comp f) := by rw ker_comp; exact comap_mono bot_le theorem sub_mem_ker_iff {f : M →ₗ[R] M₂} {x y} : x - y ∈ f.ker ↔ f x = f y := by rw [mem_ker, map_sub, sub_eq_zero] theorem disjoint_ker {f : M →ₗ[R] M₂} {p : submodule R M} : disjoint p (ker f) ↔ ∀ x ∈ p, f x = 0 → x = 0 := by simp [disjoint_def] theorem disjoint_ker' {f : M →ₗ[R] M₂} {p : submodule R M} : disjoint p (ker f) ↔ ∀ x y ∈ p, f x = f y → x = y := disjoint_ker.trans ⟨λ H x y hx hy h, eq_of_sub_eq_zero $ H _ (sub_mem _ hx hy) (by simp [h]), λ H x h₁ h₂, H x 0 h₁ (zero_mem _) (by simpa using h₂)⟩ theorem inj_of_disjoint_ker {f : M →ₗ[R] M₂} {p : submodule R M} {s : set M} (h : s ⊆ p) (hd : disjoint p (ker f)) : ∀ x y ∈ s, f x = f y → x = y := λ x y hx hy, disjoint_ker'.1 hd _ _ (h hx) (h hy) lemma disjoint_inl_inr : disjoint (inl R M M₂).range (inr R M M₂).range := by simp [disjoint_def, @eq_comm M 0, @eq_comm M₂ 0] {contextual := tt}; intros; refl theorem ker_eq_bot {f : M →ₗ[R] M₂} : ker f = ⊥ ↔ injective f := by simpa [disjoint] using @disjoint_ker' _ _ _ _ _ _ _ _ f ⊤ theorem ker_eq_bot' {f : M →ₗ[R] M₂} : ker f = ⊥ ↔ (∀ m, f m = 0 → m = 0) := have h : (∀ m ∈ (⊤ : submodule R M), f m = 0 → m = 0) ↔ (∀ m, f m = 0 → m = 0), from ⟨λ h m, h m mem_top, λ h m _, h m⟩, by simpa [h, disjoint] using @disjoint_ker _ _ _ _ _ _ _ _ f ⊤ lemma le_ker_iff_map {f : M →ₗ[R] M₂} {p : submodule R M} : p ≤ ker f ↔ map f p = ⊥ := by rw [ker, eq_bot_iff, map_le_iff_le_comap] lemma ker_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (hf) : ker (cod_restrict p f hf) = ker f := by rw [ker, comap_cod_restrict, map_bot]; refl lemma range_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (hf) : range (cod_restrict p f hf) = comap p.subtype f.range := map_cod_restrict _ _ _ _ lemma map_comap_eq (f : M →ₗ[R] M₂) (q : submodule R M₂) : map f (comap f q) = range f ⊓ q := le_antisymm (le_inf (map_mono le_top) (map_comap_le _ _)) $ by rintro _ ⟨⟨x, _, rfl⟩, hx⟩; exact ⟨x, hx, rfl⟩ lemma map_comap_eq_self {f : M →ₗ[R] M₂} {q : submodule R M₂} (h : q ≤ range f) : map f (comap f q) = q := by rwa [map_comap_eq, inf_eq_right] lemma comap_map_eq (f : M →ₗ[R] M₂) (p : submodule R M) : comap f (map f p) = p ⊔ ker f := begin refine le_antisymm _ (sup_le (le_comap_map _ _) (comap_mono bot_le)), rintro x ⟨y, hy, e⟩, exact mem_sup.2 ⟨y, hy, x - y, by simpa using sub_eq_zero.2 e.symm, by simp⟩ end lemma comap_map_eq_self {f : M →ₗ[R] M₂} {p : submodule R M} (h : ker f ≤ p) : comap f (map f p) = p := by rwa [comap_map_eq, sup_eq_left] @[simp] theorem ker_zero : ker (0 : M →ₗ[R] M₂) = ⊤ := eq_top_iff'.2 $ λ x, by simp @[simp] theorem range_zero : range (0 : M →ₗ[R] M₂) = ⊥ := submodule.map_zero _ theorem ker_eq_top {f : M →ₗ[R] M₂} : ker f = ⊤ ↔ f = 0 := ⟨λ h, ext $ λ x, mem_ker.1 $ h.symm ▸ trivial, λ h, h.symm ▸ ker_zero⟩ lemma range_le_bot_iff (f : M →ₗ[R] M₂) : range f ≤ ⊥ ↔ f = 0 := by rw [range_le_iff_comap]; exact ker_eq_top lemma range_le_ker_iff {f : M →ₗ[R] M₂} {g : M₂ →ₗ[R] M₃} : range f ≤ ker g ↔ g.comp f = 0 := ⟨λ h, ker_eq_top.1 $ eq_top_iff'.2 $ λ x, h $ mem_map_of_mem trivial, λ h x hx, mem_ker.2 $ exists.elim hx $ λ y ⟨_, hy⟩, by rw [←hy, ←comp_apply, h, zero_apply]⟩ theorem map_le_map_iff {f : M →ₗ[R] M₂} (hf : ker f = ⊥) {p p'} : map f p ≤ map f p' ↔ p ≤ p' := ⟨λ H x hx, let ⟨y, hy, e⟩ := H ⟨x, hx, rfl⟩ in ker_eq_bot.1 hf e ▸ hy, map_mono⟩ theorem map_injective {f : M →ₗ[R] M₂} (hf : ker f = ⊥) : injective (map f) := λ p p' h, le_antisymm ((map_le_map_iff hf).1 (le_of_eq h)) ((map_le_map_iff hf).1 (ge_of_eq h)) theorem comap_le_comap_iff {f : M →ₗ[R] M₂} (hf : range f = ⊤) {p p'} : comap f p ≤ comap f p' ↔ p ≤ p' := ⟨λ H x hx, by rcases range_eq_top.1 hf x with ⟨y, hy, rfl⟩; exact H hx, comap_mono⟩ theorem comap_injective {f : M →ₗ[R] M₂} (hf : range f = ⊤) : injective (comap f) := λ p p' h, le_antisymm ((comap_le_comap_iff hf).1 (le_of_eq h)) ((comap_le_comap_iff hf).1 (ge_of_eq h)) theorem map_coprod_prod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (p : submodule R M) (q : submodule R M₂) : map (coprod f g) (p.prod q) = map f p ⊔ map g q := begin refine le_antisymm _ (sup_le (map_le_iff_le_comap.2 _) (map_le_iff_le_comap.2 _)), { rw le_def', rintro _ ⟨x, ⟨h₁, h₂⟩, rfl⟩, exact mem_sup.2 ⟨_, ⟨_, h₁, rfl⟩, _, ⟨_, h₂, rfl⟩, rfl⟩ }, { exact λ x hx, ⟨(x, 0), by simp [hx]⟩ }, { exact λ x hx, ⟨(0, x), by simp [hx]⟩ } end theorem comap_prod_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) (p : submodule R M₂) (q : submodule R M₃) : comap (prod f g) (p.prod q) = comap f p ⊓ comap g q := submodule.ext $ λ x, iff.rfl theorem prod_eq_inf_comap (p : submodule R M) (q : submodule R M₂) : p.prod q = p.comap (linear_map.fst R M M₂) ⊓ q.comap (linear_map.snd R M M₂) := submodule.ext $ λ x, iff.rfl theorem prod_eq_sup_map (p : submodule R M) (q : submodule R M₂) : p.prod q = p.map (linear_map.inl R M M₂) ⊔ q.map (linear_map.inr R M M₂) := by rw [← map_coprod_prod, coprod_inl_inr, map_id] lemma span_inl_union_inr {s : set M} {t : set M₂} : span R (prod.inl '' s ∪ prod.inr '' t) = (span R s).prod (span R t) := by rw [span_union, prod_eq_sup_map, ← span_image, ← span_image]; refl lemma ker_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : ker (prod f g) = ker f ⊓ ker g := by rw [ker, ← prod_bot, comap_prod_prod]; refl end linear_map lemma submodule.sup_eq_range [ring R] [add_comm_group M] [module R M] (p q : submodule R M) : p ⊔ q = (p.subtype.coprod q.subtype).range := submodule.ext $ λ x, by simp [submodule.mem_sup, submodule.exists] namespace linear_map variables [field K] variables [add_comm_group V] [vector_space K V] variables [add_comm_group V₂] [vector_space K V₂] lemma ker_smul (f : V →ₗ[K] V₂) (a : K) (h : a ≠ 0) : ker (a • f) = ker f := submodule.comap_smul f _ a h lemma ker_smul' (f : V →ₗ[K] V₂) (a : K) : ker (a • f) = ⨅(h : a ≠ 0), ker f := submodule.comap_smul' f _ a lemma range_smul (f : V →ₗ[K] V₂) (a : K) (h : a ≠ 0) : range (a • f) = range f := submodule.map_smul f _ a h lemma range_smul' (f : V →ₗ[K] V₂) (a : K) : range (a • f) = ⨆(h : a ≠ 0), range f := submodule.map_smul' f _ a end linear_map namespace is_linear_map lemma is_linear_map_add {R M : Type*} [ring R] [add_comm_group M] [module R M]: is_linear_map R (λ (x : M × M), x.1 + x.2) := begin apply is_linear_map.mk, { intros x y, simp, cc }, { intros x y, simp [smul_add] } end lemma is_linear_map_sub {R M : Type*} [ring R] [add_comm_group M] [module R M]: is_linear_map R (λ (x : M × M), x.1 - x.2) := begin apply is_linear_map.mk, { intros x y, simp [add_comm, add_left_comm, sub_eq_add_neg] }, { intros x y, simp [smul_sub] } end end is_linear_map namespace submodule variables {T : ring R} [add_comm_group M] [add_comm_group M₂] [module R M] [module R M₂] variables (p p' : submodule R M) (q : submodule R M₂) include T open linear_map @[simp] theorem map_top (f : M →ₗ[R] M₂) : map f ⊤ = range f := rfl @[simp] theorem comap_bot (f : M →ₗ[R] M₂) : comap f ⊥ = ker f := rfl @[simp] theorem ker_subtype : p.subtype.ker = ⊥ := ker_eq_bot.2 $ λ x y, subtype.eq' @[simp] theorem range_subtype : p.subtype.range = p := by simpa using map_comap_subtype p ⊤ lemma map_subtype_le (p' : submodule R p) : map p.subtype p' ≤ p := by simpa using (map_mono le_top : map p.subtype p' ≤ p.subtype.range) /-- Under the canonical linear map from a submodule `p` to the ambient space `M`, the image of the maximal submodule of `p` is just `p `. -/ @[simp] lemma map_subtype_top : map p.subtype (⊤ : submodule R p) = p := by simp @[simp] lemma comap_subtype_eq_top {p p' : submodule R M} : p'.comap p.subtype = ⊤ ↔ p ≤ p' := eq_top_iff.trans $ map_le_iff_le_comap.symm.trans $ by rw [map_subtype_top] @[simp] lemma comap_subtype_self : p.comap p.subtype = ⊤ := comap_subtype_eq_top.2 (le_refl _) @[simp] lemma range_range_restrict (f : M →ₗ[R] M₂) : f.range_restrict.range = ⊤ := by simp [range_cod_restrict] @[simp] theorem ker_of_le (p p' : submodule R M) (h : p ≤ p') : (of_le h).ker = ⊥ := by rw [of_le, ker_cod_restrict, ker_subtype] lemma range_of_le (p q : submodule R M) (h : p ≤ q) : (of_le h).range = comap q.subtype p := by rw [← map_top, of_le, linear_map.map_cod_restrict, map_top, range_subtype] lemma disjoint_iff_comap_eq_bot {p q : submodule R M} : disjoint p q ↔ comap p.subtype q = ⊥ := by rw [eq_bot_iff, ← map_le_map_iff p.ker_subtype, map_bot, map_comap_subtype]; refl /-- If N ⊆ M then submodules of N are the same as submodules of M contained in N -/ def map_subtype.order_iso : ((≤) : submodule R p → submodule R p → Prop) ≃o ((≤) : {p' : submodule R M // p' ≤ p} → {p' : submodule R M // p' ≤ p} → Prop) := { to_fun := λ p', ⟨map p.subtype p', map_subtype_le p _⟩, inv_fun := λ q, comap p.subtype q, left_inv := λ p', comap_map_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.eq' $ by simp [map_comap_subtype p, inf_of_le_right hq], ord' := λ p₁ p₂, (map_le_map_iff $ ker_subtype _).symm } /-- If `p ⊆ M` is a submodule, the ordering of submodules of `p` is embedded in the ordering of submodules of M. -/ def map_subtype.le_order_embedding : ((≤) : submodule R p → submodule R p → Prop) ≼o ((≤) : submodule R M → submodule R M → Prop) := (order_iso.to_order_embedding $ map_subtype.order_iso p).trans (subtype.order_embedding _ _) @[simp] lemma map_subtype_embedding_eq (p' : submodule R p) : map_subtype.le_order_embedding p p' = map p.subtype p' := rfl /-- If `p ⊆ M` is a submodule, the ordering of submodules of `p` is embedded in the ordering of submodules of M. -/ def map_subtype.lt_order_embedding : ((<) : submodule R p → submodule R p → Prop) ≼o ((<) : submodule R M → submodule R M → Prop) := (map_subtype.le_order_embedding p).lt_embedding_of_le_embedding @[simp] theorem map_inl : p.map (inl R M M₂) = prod p ⊥ := by ext ⟨x, y⟩; simp only [and.left_comm, eq_comm, mem_map, prod.mk.inj_iff, inl_apply, mem_bot, exists_eq_left', mem_prod] @[simp] theorem map_inr : q.map (inr R M M₂) = prod ⊥ q := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem comap_fst : p.comap (fst R M M₂) = prod p ⊤ := by ext ⟨x, y⟩; simp @[simp] theorem comap_snd : q.comap (snd R M M₂) = prod ⊤ q := by ext ⟨x, y⟩; simp @[simp] theorem prod_comap_inl : (prod p q).comap (inl R M M₂) = p := by ext; simp @[simp] theorem prod_comap_inr : (prod p q).comap (inr R M M₂) = q := by ext; simp @[simp] theorem prod_map_fst : (prod p q).map (fst R M M₂) = p := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ q)] @[simp] theorem prod_map_snd : (prod p q).map (snd R M M₂) = q := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ p)] @[simp] theorem ker_inl : (inl R M M₂).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inl] @[simp] theorem ker_inr : (inr R M M₂).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inr] @[simp] theorem range_fst : (fst R M M₂).range = ⊤ := by rw [range, ← prod_top, prod_map_fst] @[simp] theorem range_snd : (snd R M M₂).range = ⊤ := by rw [range, ← prod_top, prod_map_snd] /-- The map from a module `M` to the quotient of `M` by a submodule `p` as a linear map. -/ def mkq : M →ₗ[R] p.quotient := ⟨quotient.mk, by simp, by simp⟩ @[simp] theorem mkq_apply (x : M) : p.mkq x = quotient.mk x := rfl /-- The map from the quotient of `M` by a submodule `p` to `M₂` induced by a linear map `f : M → M₂` vanishing on `p`, as a linear map. -/ def liftq (f : M →ₗ[R] M₂) (h : p ≤ f.ker) : p.quotient →ₗ[R] M₂ := ⟨λ x, _root_.quotient.lift_on' x f $ λ a b (ab : a - b ∈ p), eq_of_sub_eq_zero $ by simpa using h ab, by rintro ⟨x⟩ ⟨y⟩; exact f.map_add x y, by rintro a ⟨x⟩; exact f.map_smul a x⟩ @[simp] theorem liftq_apply (f : M →ₗ[R] M₂) {h} (x : M) : p.liftq f h (quotient.mk x) = f x := rfl @[simp] theorem liftq_mkq (f : M →ₗ[R] M₂) (h) : (p.liftq f h).comp p.mkq = f := by ext; refl @[simp] theorem range_mkq : p.mkq.range = ⊤ := eq_top_iff'.2 $ by rintro ⟨x⟩; exact ⟨x, trivial, rfl⟩ @[simp] theorem ker_mkq : p.mkq.ker = p := by ext; simp lemma le_comap_mkq (p' : submodule R p.quotient) : p ≤ comap p.mkq p' := by simpa using (comap_mono bot_le : p.mkq.ker ≤ comap p.mkq p') @[simp] theorem mkq_map_self : map p.mkq p = ⊥ := by rw [eq_bot_iff, map_le_iff_le_comap, comap_bot, ker_mkq]; exact le_refl _ @[simp] theorem comap_map_mkq : comap p.mkq (map p.mkq p') = p ⊔ p' := by simp [comap_map_eq, sup_comm] /-- The map from the quotient of `M` by submodule `p` to the quotient of `M₂` by submodule `q` along `f : M → M₂` is linear. -/ def mapq (f : M →ₗ[R] M₂) (h : p ≤ comap f q) : p.quotient →ₗ[R] q.quotient := p.liftq (q.mkq.comp f) $ by simpa [ker_comp] using h @[simp] theorem mapq_apply (f : M →ₗ[R] M₂) {h} (x : M) : mapq p q f h (quotient.mk x) = quotient.mk (f x) := rfl theorem mapq_mkq (f : M →ₗ[R] M₂) {h} : (mapq p q f h).comp p.mkq = q.mkq.comp f := by ext x; refl theorem comap_liftq (f : M →ₗ[R] M₂) (h) : q.comap (p.liftq f h) = (q.comap f).map (mkq p) := le_antisymm (by rintro ⟨x⟩ hx; exact ⟨_, hx, rfl⟩) (by rw [map_le_iff_le_comap, ← comap_comp, liftq_mkq]; exact le_refl _) theorem map_liftq (f : M →ₗ[R] M₂) (h) (q : submodule R (quotient p)) : q.map (p.liftq f h) = (q.comap p.mkq).map f := le_antisymm (by rintro _ ⟨⟨x⟩, hxq, rfl⟩; exact ⟨x, hxq, rfl⟩) (by rintro _ ⟨x, hxq, rfl⟩; exact ⟨quotient.mk x, hxq, rfl⟩) theorem ker_liftq (f : M →ₗ[R] M₂) (h) : ker (p.liftq f h) = (ker f).map (mkq p) := comap_liftq _ _ _ _ theorem range_liftq (f : M →ₗ[R] M₂) (h) : range (p.liftq f h) = range f := map_liftq _ _ _ _ theorem ker_liftq_eq_bot (f : M →ₗ[R] M₂) (h) (h' : ker f ≤ p) : ker (p.liftq f h) = ⊥ := by rw [ker_liftq, le_antisymm h h', mkq_map_self] /-- The correspondence theorem for modules: there is an order isomorphism between submodules of the quotient of `M` by `p`, and submodules of `M` larger than `p`. -/ def comap_mkq.order_iso : ((≤) : submodule R p.quotient → submodule R p.quotient → Prop) ≃o ((≤) : {p' : submodule R M // p ≤ p'} → {p' : submodule R M // p ≤ p'} → Prop) := { to_fun := λ p', ⟨comap p.mkq p', le_comap_mkq p _⟩, inv_fun := λ q, map p.mkq q, left_inv := λ p', map_comap_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.eq' $ by simpa [comap_map_mkq p], ord' := λ p₁ p₂, (comap_le_comap_iff $ range_mkq _).symm } /-- The ordering on submodules of the quotient of `M` by `p` embeds into the ordering on submodules of `M`. -/ def comap_mkq.le_order_embedding : ((≤) : submodule R p.quotient → submodule R p.quotient → Prop) ≼o ((≤) : submodule R M → submodule R M → Prop) := (order_iso.to_order_embedding $ comap_mkq.order_iso p).trans (subtype.order_embedding _ _) @[simp] lemma comap_mkq_embedding_eq (p' : submodule R p.quotient) : comap_mkq.le_order_embedding p p' = comap p.mkq p' := rfl /-- The ordering on submodules of the quotient of `M` by `p` embeds into the ordering on submodules of `M`. -/ def comap_mkq.lt_order_embedding : ((<) : submodule R p.quotient → submodule R p.quotient → Prop) ≼o ((<) : submodule R M → submodule R M → Prop) := (comap_mkq.le_order_embedding p).lt_embedding_of_le_embedding end submodule section set_option old_structure_cmd true /-- A linear equivalence is an invertible linear map. -/ @[nolint has_inhabited_instance] structure linear_equiv (R : Type u) (M : Type v) (M₂ : Type w) [ring R] [add_comm_group M] [add_comm_group M₂] [module R M] [module R M₂] extends M →ₗ[R] M₂, M ≃ M₂ end infix ` ≃ₗ ` := linear_equiv _ notation M ` ≃ₗ[`:50 R `] ` M₂ := linear_equiv R M M₂ namespace linear_equiv section ring variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] section variables [module R M] [module R M₂] include R instance : has_coe (M ≃ₗ[R] M₂) (M →ₗ[R] M₂) := ⟨to_linear_map⟩ -- see Note [function coercion] instance : has_coe_to_fun (M ≃ₗ[R] M₂) := ⟨_, λ f, f.to_fun⟩ lemma to_equiv_injective : function.injective (to_equiv : (M ≃ₗ[R] M₂) → M ≃ M₂) := λ ⟨_, _, _, _, _, _⟩ ⟨_, _, _, _, _, _⟩ h, linear_equiv.mk.inj_eq.mpr (equiv.mk.inj h) end section variables {module_M : module R M} {module_M₂ : module R M₂} variables (e e' : M ≃ₗ[R] M₂) @[simp, norm_cast] theorem coe_coe : ⇑(e : M →ₗ[R] M₂) = e := rfl @[simp] lemma coe_to_equiv : ⇑(e.to_equiv) = e := rfl section variables {e e'} @[ext] lemma ext (h : (e : M → M₂) = e') : e = e' := to_equiv_injective (equiv.eq_of_to_fun_eq h) end section variables (M R) /-- The identity map is a linear equivalence. -/ @[refl] def refl [module R M] : M ≃ₗ[R] M := { .. linear_map.id, .. equiv.refl M } end @[simp] lemma refl_apply [module R M] (x : M) : refl R M x = x := rfl /-- Linear equivalences are symmetric. -/ @[symm] def symm : M₂ ≃ₗ[R] M := { .. e.to_linear_map.inverse e.inv_fun e.left_inv e.right_inv, .. e.to_equiv.symm } variables {module_M₃ : module R M₃} (e₁ : M ≃ₗ[R] M₂) (e₂ : M₂ ≃ₗ[R] M₃) /-- Linear equivalences are transitive. -/ @[trans] def trans : M ≃ₗ[R] M₃ := { .. e₂.to_linear_map.comp e₁.to_linear_map, .. e₁.to_equiv.trans e₂.to_equiv } /-- A linear equivalence is an additive equivalence. -/ def to_add_equiv : M ≃+ M₂ := { map_add' := e.add, .. e } @[simp] lemma coe_to_add_equiv : ⇑(e.to_add_equiv) = e := rfl @[simp] theorem trans_apply (c : M) : (e₁.trans e₂) c = e₂ (e₁ c) := rfl @[simp] theorem apply_symm_apply (c : M₂) : e (e.symm c) = c := e.6 c @[simp] theorem symm_apply_apply (b : M) : e.symm (e b) = b := e.5 b @[simp] lemma trans_refl : e.trans (refl R M₂) = e := to_equiv_injective e.to_equiv.trans_refl @[simp] lemma refl_trans : (refl R M).trans e = e := to_equiv_injective e.to_equiv.refl_trans lemma symm_apply_eq {x y} : e.symm x = y ↔ x = e y := e.to_equiv.symm_apply_eq lemma eq_symm_apply {x y} : y = e.symm x ↔ e y = x := e.to_equiv.eq_symm_apply @[simp] theorem map_add (a b : M) : e (a + b) = e a + e b := e.add a b @[simp] theorem map_zero : e 0 = 0 := e.to_linear_map.map_zero @[simp] theorem map_neg (a : M) : e (-a) = -e a := e.to_linear_map.map_neg a @[simp] theorem map_sub (a b : M) : e (a - b) = e a - e b := e.to_linear_map.map_sub a b @[simp] theorem map_smul (c : R) (x : M) : e (c • x) = c • e x := e.smul c x @[simp] theorem map_eq_zero_iff {x : M} : e x = 0 ↔ x = 0 := e.to_add_equiv.map_eq_zero_iff theorem map_ne_zero_iff {x : M} : e x ≠ 0 ↔ x ≠ 0 := e.to_add_equiv.map_ne_zero_iff @[simp] theorem symm_symm : e.symm.symm = e := by { cases e, refl } protected lemma bijective : function.bijective e := e.to_equiv.bijective protected lemma injective : function.injective e := e.to_equiv.injective protected lemma surjective : function.surjective e := e.to_equiv.surjective protected lemma image_eq_preimage (s : set M) : e '' s = e.symm ⁻¹' s := e.to_equiv.image_eq_preimage s lemma map_eq_comap {p : submodule R M} : (p.map e : submodule R M₂) = p.comap e.symm := submodule.ext' $ by simp [e.image_eq_preimage] end section prod variables [add_comm_group M₄] variables {module_M : module R M} {module_M₂ : module R M₂} variables {module_M₃ : module R M₃} {module_M₄ : module R M₄} variables (e₁ : M ≃ₗ[R] M₂) (e₂ : M₃ ≃ₗ[R] M₄) /-- Product of linear equivalences; the maps come from `equiv.prod_congr`. -/ protected def prod : (M × M₃) ≃ₗ[R] (M₂ × M₄) := { add := λ x y, prod.ext (e₁.map_add _ _) (e₂.map_add _ _), smul := λ c x, prod.ext (e₁.map_smul c _) (e₂.map_smul c _), .. equiv.prod_congr e₁.to_equiv e₂.to_equiv } lemma prod_symm : (e₁.prod e₂).symm = e₁.symm.prod e₂.symm := rfl @[simp] lemma prod_apply (p) : e₁.prod e₂ p = (e₁ p.1, e₂ p.2) := rfl @[simp, norm_cast] lemma coe_prod : (e₁.prod e₂ : (M × M₃) →ₗ[R] (M₂ × M₄)) = (e₁ : M →ₗ[R] M₂).prod_map (e₂ : M₃ →ₗ[R] M₄) := rfl /-- Equivalence given by a block lower diagonal matrix. `e₁` and `e₂` are diagonal square blocks, and `f` is a rectangular block below the diagonal. -/ protected def skew_prod (f : M →ₗ[R] M₄) : (M × M₃) ≃ₗ[R] M₂ × M₄ := { inv_fun := λ p : M₂ × M₄, (e₁.symm p.1, e₂.symm (p.2 - f (e₁.symm p.1))), left_inv := λ p, by simp, right_inv := λ p, by simp, .. ((e₁ : M →ₗ[R] M₂).comp (linear_map.fst R M M₃)).prod ((e₂ : M₃ →ₗ[R] M₄).comp (linear_map.snd R M M₃) + f.comp (linear_map.fst R M M₃)) } @[simp] lemma skew_prod_apply (f : M →ₗ[R] M₄) (x) : e₁.skew_prod e₂ f x = (e₁ x.1, e₂ x.2 + f x.1) := rfl @[simp] lemma skew_prod_symm_apply (f : M →ₗ[R] M₄) (x) : (e₁.skew_prod e₂ f).symm x = (e₁.symm x.1, e₂.symm (x.2 - f (e₁.symm x.1))) := rfl end prod section variables {module_M : module R M} {module_M₂ : module R M₂} variables (f : M →ₗ[R] M₂) /-- A bijective linear map is a linear equivalence. Here, bijectivity is described by saying that the kernel of `f` is `{0}` and the range is the universal set. -/ noncomputable def of_bijective (hf₁ : f.ker = ⊥) (hf₂ : f.range = ⊤) : M ≃ₗ[R] M₂ := { ..f, ..@equiv.of_bijective _ _ f ⟨linear_map.ker_eq_bot.1 hf₁, linear_map.range_eq_top.1 hf₂⟩ } @[simp] theorem of_bijective_apply {hf₁ hf₂} (x : M) : of_bijective f hf₁ hf₂ x = f x := rfl variables (g : M₂ →ₗ[R] M) /-- If a linear map has an inverse, it is a linear equivalence. -/ def of_linear (h₁ : f.comp g = linear_map.id) (h₂ : g.comp f = linear_map.id) : M ≃ₗ[R] M₂ := { inv_fun := g, left_inv := linear_map.ext_iff.1 h₂, right_inv := linear_map.ext_iff.1 h₁, ..f } @[simp] theorem of_linear_apply {h₁ h₂} (x : M) : of_linear f g h₁ h₂ x = f x := rfl @[simp] theorem of_linear_symm_apply {h₁ h₂} (x : M₂) : (of_linear f g h₁ h₂).symm x = g x := rfl variables (e : M ≃ₗ[R] M₂) @[simp] protected theorem ker : (e : M →ₗ[R] M₂).ker = ⊥ := linear_map.ker_eq_bot.2 e.to_equiv.injective @[simp] protected theorem range : (e : M →ₗ[R] M₂).range = ⊤ := linear_map.range_eq_top.2 e.to_equiv.surjective variables (p : submodule R M) /-- The top submodule of `M` is linearly equivalent to `M`. -/ def of_top (h : p = ⊤) : p ≃ₗ[R] M := { inv_fun := λ x, ⟨x, h.symm ▸ trivial⟩, left_inv := λ ⟨x, h⟩, rfl, right_inv := λ x, rfl, .. p.subtype } @[simp] theorem of_top_apply {h} (x : p) : of_top p h x = x := rfl @[simp] theorem of_top_symm_apply {h} (x : M) : (of_top p h).symm x = ⟨x, h.symm ▸ trivial⟩ := rfl lemma eq_bot_of_equiv [module R M₂] (e : p ≃ₗ[R] (⊥ : submodule R M₂)) : p = ⊥ := begin refine bot_unique (submodule.le_def'.2 $ assume b hb, (submodule.mem_bot R).2 _), rw [← p.mk_eq_zero hb, ← e.map_eq_zero_iff], apply submodule.eq_zero_of_bot_submodule end end end ring section comm_ring variables [comm_ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] include R open linear_map /-- Multiplying by a unit `a` of the ring `R` is a linear equivalence. -/ def smul_of_unit (a : units R) : M ≃ₗ[R] M := of_linear ((a:R) • 1 : M →ₗ M) (((a⁻¹ : units R) : R) • 1 : M →ₗ M) (by rw [smul_comp, comp_smul, smul_smul, units.mul_inv, one_smul]; refl) (by rw [smul_comp, comp_smul, smul_smul, units.inv_mul, one_smul]; refl) /-- A linear isomorphism between the domains and codomains of two spaces of linear maps gives a linear isomorphism between the two function spaces. -/ def arrow_congr {R M₁ M₂ M₂₁ M₂₂ : Sort*} [comm_ring R] [add_comm_group M₁] [add_comm_group M₂] [add_comm_group M₂₁] [add_comm_group M₂₂] [module R M₁] [module R M₂] [module R M₂₁] [module R M₂₂] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : M₂₁ ≃ₗ[R] M₂₂) : (M₁ →ₗ[R] M₂₁) ≃ₗ[R] (M₂ →ₗ[R] M₂₂) := { to_fun := λ f, e₂.to_linear_map.comp $ f.comp e₁.symm.to_linear_map, inv_fun := λ f, e₂.symm.to_linear_map.comp $ f.comp e₁.to_linear_map, left_inv := λ f, by { ext x, unfold_coes, change e₂.inv_fun (e₂.to_fun $ f.to_fun $ e₁.inv_fun $ e₁.to_fun x) = _, rw [e₁.left_inv, e₂.left_inv] }, right_inv := λ f, by { ext x, unfold_coes, change e₂.to_fun (e₂.inv_fun $ f.to_fun $ e₁.to_fun $ e₁.inv_fun x) = _, rw [e₁.right_inv, e₂.right_inv] }, add := λ f g, by { ext x, change e₂.to_fun ((f + g) (e₁.inv_fun x)) = _, rw [linear_map.add_apply, e₂.add], refl }, smul := λ c f, by { ext x, change e₂.to_fun ((c • f) (e₁.inv_fun x)) = _, rw [linear_map.smul_apply, e₂.smul], refl } } /-- If M₂ and M₃ are linearly isomorphic then the two spaces of linear maps from M into M₂ and M into M₃ are linearly isomorphic. -/ def congr_right (f : M₂ ≃ₗ[R] M₃) : (M →ₗ[R] M₂) ≃ₗ (M →ₗ M₃) := arrow_congr (linear_equiv.refl R M) f /-- If M and M₂ are linearly isomorphic then the two spaces of linear maps from M and M₂ to themselves are linearly isomorphic. -/ def conj (e : M ≃ₗ[R] M₂) : (module.End R M) ≃ₗ[R] (module.End R M₂) := arrow_congr e e end comm_ring section field variables [field K] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module K M] [module K M₂] [module K M₃] variable (M) open linear_map /-- Multiplying by a nonzero element `a` of the field `K` is a linear equivalence. -/ def smul_of_ne_zero (a : K) (ha : a ≠ 0) : M ≃ₗ[K] M := smul_of_unit $ units.mk0 a ha end field end linear_equiv namespace submodule variables [comm_ring R] [add_comm_group M] [add_comm_group M₂] [module R M] [module R M₂] variables (p : submodule R M) (q : submodule R M₂) lemma comap_le_comap_smul (f : M →ₗ[R] M₂) (c : R) : comap f q ≤ comap (c • f) q := begin rw le_def', intros m h, change c • (f m) ∈ q, change f m ∈ q at h, apply submodule.smul _ _ h, end lemma inf_comap_le_comap_add (f₁ f₂ : M →ₗ[R] M₂) : comap f₁ q ⊓ comap f₂ q ≤ comap (f₁ + f₂) q := begin rw le_def', intros m h, change f₁ m + f₂ m ∈ q, change f₁ m ∈ q ∧ f₂ m ∈ q at h, apply submodule.add _ h.1 h.2, end /-- Given modules `M`, `M₂` over a commutative ring, together with submodules `p ⊆ M`, `q ⊆ M₂`, the set of maps $\{f ∈ Hom(M, M₂) | f(p) ⊆ q \}$ is a submodule of `Hom(M, M₂)`. -/ def compatible_maps : submodule R (M →ₗ[R] M₂) := { carrier := {f | p ≤ comap f q}, zero := by { change p ≤ comap 0 q, rw comap_zero, refine le_top, }, add := λ f₁ f₂ h₁ h₂, by { apply le_trans _ (inf_comap_le_comap_add q f₁ f₂), rw le_inf_iff, exact ⟨h₁, h₂⟩, }, smul := λ c f h, le_trans h (comap_le_comap_smul q f c), } /-- Given modules `M`, `M₂` over a commutative ring, together with submodules `p ⊆ M`, `q ⊆ M₂`, the natural map $\{f ∈ Hom(M, M₂) | f(p) ⊆ q \} \to Hom(M/p, M₂/q)$ is linear. -/ def mapq_linear : compatible_maps p q →ₗ[R] p.quotient →ₗ[R] q.quotient := { to_fun := λ f, mapq _ _ f.val f.property, add := λ x y, by { ext m', apply quotient.induction_on' m', intros m, refl, }, smul := λ c f, by { ext m', apply quotient.induction_on' m', intros m, refl, } } end submodule namespace equiv variables [ring R] [add_comm_group M] [module R M] [add_comm_group M₂] [module R M₂] /-- An equivalence whose underlying function is linear is a linear equivalence. -/ def to_linear_equiv (e : M ≃ M₂) (h : is_linear_map R (e : M → M₂)) : M ≃ₗ[R] M₂ := { add := h.add, smul := h.smul, .. e} end equiv namespace linear_map variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] variables (f : M →ₗ[R] M₂) /-- The first isomorphism law for modules. The quotient of `M` by the kernel of `f` is linearly equivalent to the range of `f`. -/ noncomputable def quot_ker_equiv_range : f.ker.quotient ≃ₗ[R] f.range := have hr : ∀ x : f.range, ∃ y, f y = ↑x := λ x, x.2.imp $ λ _, and.right, let F : f.ker.quotient →ₗ[R] f.range := f.ker.liftq (cod_restrict f.range f $ λ x, ⟨x, trivial, rfl⟩) (λ x hx, by simp; apply subtype.coe_ext.2; simpa using hx) in { inv_fun := λx, submodule.quotient.mk (classical.some (hr x)), left_inv := by rintro ⟨x⟩; exact (submodule.quotient.eq _).2 (sub_mem_ker_iff.2 $ classical.some_spec $ hr $ F $ submodule.quotient.mk x), right_inv := λ x : range f, subtype.eq $ classical.some_spec (hr x), .. F } open submodule /-- Canonical linear map from the quotient `p/(p ∩ p')` to `(p+p')/p'`, mapping `x + (p ∩ p')` to `x + p'`, where `p` and `p'` are submodules of an ambient module. -/ def quotient_inf_to_sup_quotient (p p' : submodule R M) : (comap p.subtype (p ⊓ p')).quotient →ₗ[R] (comap (p ⊔ p').subtype p').quotient := (comap p.subtype (p ⊓ p')).liftq ((comap (p ⊔ p').subtype p').mkq.comp (of_le le_sup_left)) begin rw [ker_comp, of_le, comap_cod_restrict, ker_mkq, map_comap_subtype], exact comap_mono (inf_le_inf_right _ le_sup_left) end /-- Second Isomorphism Law : the canonical map from `p/(p ∩ p')` to `(p+p')/p'` as a linear isomorphism. -/ noncomputable def quotient_inf_equiv_sup_quotient (p p' : submodule R M) : (comap p.subtype (p ⊓ p')).quotient ≃ₗ[R] (comap (p ⊔ p').subtype p').quotient := { .. quotient_inf_to_sup_quotient p p', .. show (comap p.subtype (p ⊓ p')).quotient ≃ (comap (p ⊔ p').subtype p').quotient, from @equiv.of_bijective _ _ (quotient_inf_to_sup_quotient p p') begin constructor, { rw [← ker_eq_bot, quotient_inf_to_sup_quotient, ker_liftq_eq_bot], rw [ker_comp, ker_mkq], rintros ⟨x, hx1⟩ hx2, exact ⟨hx1, hx2⟩ }, rw [← range_eq_top, quotient_inf_to_sup_quotient, range_liftq, eq_top_iff'], rintros ⟨x, hx⟩, rcases mem_sup.1 hx with ⟨y, hy, z, hz, rfl⟩, use [⟨y, hy⟩, trivial], apply (submodule.quotient.eq _).2, change y - (y + z) ∈ p', rwa [sub_add_eq_sub_sub, sub_self, zero_sub, neg_mem_iff] end } section prod lemma is_linear_map_prod_iso {R M M₂ M₃ : Type*} [comm_ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] [module R M] [module R M₂] [module R M₃] : is_linear_map R (λ(p : (M →ₗ[R] M₂) × (M →ₗ[R] M₃)), (linear_map.prod p.1 p.2 : (M →ₗ[R] (M₂ × M₃)))) := ⟨λu v, rfl, λc u, rfl⟩ end prod section pi universe i variables {φ : ι → Type i} variables [∀i, add_comm_group (φ i)] [∀i, module R (φ i)] /-- `pi` construction for linear functions. From a family of linear functions it produces a linear function into a family of modules. -/ def pi (f : Πi, M₂ →ₗ[R] φ i) : M₂ →ₗ[R] (Πi, φ i) := ⟨λc i, f i c, assume c d, funext $ assume i, (f i).add _ _, assume c d, funext $ assume i, (f i).smul _ _⟩ @[simp] lemma pi_apply (f : Πi, M₂ →ₗ[R] φ i) (c : M₂) (i : ι) : pi f c i = f i c := rfl lemma ker_pi (f : Πi, M₂ →ₗ[R] φ i) : ker (pi f) = (⨅i:ι, ker (f i)) := by ext c; simp [funext_iff]; refl lemma pi_eq_zero (f : Πi, M₂ →ₗ[R] φ i) : pi f = 0 ↔ (∀i, f i = 0) := by simp only [linear_map.ext_iff, pi_apply, funext_iff]; exact ⟨λh a b, h b a, λh a b, h b a⟩ lemma pi_zero : pi (λi, 0 : Πi, M₂ →ₗ[R] φ i) = 0 := by ext; refl lemma pi_comp (f : Πi, M₂ →ₗ[R] φ i) (g : M₃ →ₗ[R] M₂) : (pi f).comp g = pi (λi, (f i).comp g) := rfl /-- The projections from a family of modules are linear maps. -/ def proj (i : ι) : (Πi, φ i) →ₗ[R] φ i := ⟨ λa, a i, assume f g, rfl, assume c f, rfl ⟩ @[simp] lemma proj_apply (i : ι) (b : Πi, φ i) : (proj i : (Πi, φ i) →ₗ[R] φ i) b = b i := rfl lemma proj_pi (f : Πi, M₂ →ₗ[R] φ i) (i : ι) : (proj i).comp (pi f) = f i := ext $ assume c, rfl lemma infi_ker_proj : (⨅i, ker (proj i) : submodule R (Πi, φ i)) = ⊥ := bot_unique $ submodule.le_def'.2 $ assume a h, begin simp only [mem_infi, mem_ker, proj_apply] at h, exact (mem_bot _).2 (funext $ assume i, h i) end section variables (R φ) /-- If `I` and `J` are disjoint index sets, the product of the kernels of the `J`th projections of `φ` is linearly equivalent to the product over `I`. -/ def infi_ker_proj_equiv {I J : set ι} [decidable_pred (λi, i ∈ I)] (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) : (⨅i ∈ J, ker (proj i) : submodule R (Πi, φ i)) ≃ₗ[R] (Πi:I, φ i) := begin refine linear_equiv.of_linear (pi $ λi, (proj (i:ι)).comp (submodule.subtype _)) (cod_restrict _ (pi $ λi, if h : i ∈ I then proj (⟨i, h⟩ : I) else 0) _) _ _, { assume b, simp only [mem_infi, mem_ker, funext_iff, proj_apply, pi_apply], assume j hjJ, have : j ∉ I := assume hjI, hd ⟨hjI, hjJ⟩, rw [dif_neg this, zero_apply] }, { simp only [pi_comp, comp_assoc, subtype_comp_cod_restrict, proj_pi, dif_pos, subtype.val_prop'], ext b ⟨j, hj⟩, refl }, { ext ⟨b, hb⟩, apply subtype.coe_ext.2, ext j, have hb : ∀i ∈ J, b i = 0, { simpa only [mem_infi, mem_ker, proj_apply] using (mem_infi _).1 hb }, simp only [comp_apply, pi_apply, id_apply, proj_apply, subtype_apply, cod_restrict_apply], split_ifs, { refl }, { exact (hb _ $ (hu trivial).resolve_left h).symm } } end end section variable [decidable_eq ι] /-- `diag i j` is the identity map if `i = j`. Otherwise it is the constant 0 map. -/ def diag (i j : ι) : φ i →ₗ[R] φ j := @function.update ι (λj, φ i →ₗ[R] φ j) _ 0 i id j lemma update_apply (f : Πi, M₂ →ₗ[R] φ i) (c : M₂) (i j : ι) (b : M₂ →ₗ[R] φ i) : (update f i b j) c = update (λi, f i c) i (b c) j := begin by_cases j = i, { rw [h, update_same, update_same] }, { rw [update_noteq h, update_noteq h] } end end section variable [decidable_eq ι] variables (R φ) /-- The standard basis of the product of `φ`. -/ def std_basis (i : ι) : φ i →ₗ[R] (Πi, φ i) := pi (diag i) lemma std_basis_apply (i : ι) (b : φ i) : std_basis R φ i b = update 0 i b := by ext j; rw [std_basis, pi_apply, diag, update_apply]; refl @[simp] lemma std_basis_same (i : ι) (b : φ i) : std_basis R φ i b i = b := by rw [std_basis_apply, update_same] lemma std_basis_ne (i j : ι) (h : j ≠ i) (b : φ i) : std_basis R φ i b j = 0 := by rw [std_basis_apply, update_noteq h]; refl lemma ker_std_basis (i : ι) : ker (std_basis R φ i) = ⊥ := ker_eq_bot.2 $ assume f g hfg, have std_basis R φ i f i = std_basis R φ i g i := hfg ▸ rfl, by simpa only [std_basis_same] lemma proj_comp_std_basis (i j : ι) : (proj i).comp (std_basis R φ j) = diag j i := by rw [std_basis, proj_pi] lemma proj_std_basis_same (i : ι) : (proj i).comp (std_basis R φ i) = id := by ext b; simp lemma proj_std_basis_ne (i j : ι) (h : i ≠ j) : (proj i).comp (std_basis R φ j) = 0 := by ext b; simp [std_basis_ne R φ _ _ h] lemma supr_range_std_basis_le_infi_ker_proj (I J : set ι) (h : disjoint I J) : (⨆i∈I, range (std_basis R φ i)) ≤ (⨅i∈J, ker (proj i)) := begin refine (supr_le $ assume i, supr_le $ assume hi, range_le_iff_comap.2 _), simp only [(ker_comp _ _).symm, eq_top_iff, le_def', mem_ker, comap_infi, mem_infi], assume b hb j hj, have : i ≠ j := assume eq, h ⟨hi, eq.symm ▸ hj⟩, rw [proj_std_basis_ne R φ j i this.symm, zero_apply] end lemma infi_ker_proj_le_supr_range_std_basis {I : finset ι} {J : set ι} (hu : set.univ ⊆ ↑I ∪ J) : (⨅ i∈J, ker (proj i)) ≤ (⨆i∈I, range (std_basis R φ i)) := submodule.le_def'.2 begin assume b hb, simp only [mem_infi, mem_ker, proj_apply] at hb, rw ← show I.sum (λi, std_basis R φ i (b i)) = b, { ext i, rw [pi.finset_sum_apply, ← std_basis_same R φ i (b i)], refine finset.sum_eq_single i (assume j hjI ne, std_basis_ne _ _ _ _ ne.symm _) _, assume hiI, rw [std_basis_same], exact hb _ ((hu trivial).resolve_left hiI) }, exact sum_mem _ (assume i hiI, mem_supr_of_mem _ i $ mem_supr_of_mem _ hiI $ (std_basis R φ i).mem_range_self (b i)) end lemma supr_range_std_basis_eq_infi_ker_proj {I J : set ι} (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) (hI : set.finite I) : (⨆i∈I, range (std_basis R φ i)) = (⨅i∈J, ker (proj i)) := begin refine le_antisymm (supr_range_std_basis_le_infi_ker_proj _ _ _ _ hd) _, have : set.univ ⊆ ↑hI.to_finset ∪ J, { rwa [hI.coe_to_finset] }, refine le_trans (infi_ker_proj_le_supr_range_std_basis R φ this) (supr_le_supr $ assume i, _), rw [set.finite.mem_to_finset], exact le_refl _ end lemma supr_range_std_basis [fintype ι] : (⨆i:ι, range (std_basis R φ i)) = ⊤ := have (set.univ : set ι) ⊆ ↑(finset.univ : finset ι) ∪ ∅ := by rw [finset.coe_univ, set.union_empty], begin apply top_unique, convert (infi_ker_proj_le_supr_range_std_basis R φ this), exact infi_emptyset.symm, exact (funext $ λi, (@supr_pos _ _ _ (λh, range (std_basis R φ i)) $ finset.mem_univ i).symm) end lemma disjoint_std_basis_std_basis (I J : set ι) (h : disjoint I J) : disjoint (⨆i∈I, range (std_basis R φ i)) (⨆i∈J, range (std_basis R φ i)) := begin refine disjoint.mono (supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ set.disjoint_compl I) (supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ set.disjoint_compl J) _, simp only [disjoint, submodule.le_def', mem_infi, mem_inf, mem_ker, mem_bot, proj_apply, funext_iff], rintros b ⟨hI, hJ⟩ i, classical, by_cases hiI : i ∈ I, { by_cases hiJ : i ∈ J, { exact (h ⟨hiI, hiJ⟩).elim }, { exact hJ i hiJ } }, { exact hI i hiI } end lemma std_basis_eq_single {a : R} : (λ (i : ι), (std_basis R (λ _ : ι, R) i) a) = λ (i : ι), (finsupp.single i a) := begin ext i j, rw [std_basis_apply, finsupp.single_apply], split_ifs, { rw [h, function.update_same] }, { rw [function.update_noteq (ne.symm h)], refl }, end end end pi variables (R M) instance automorphism_group : group (M ≃ₗ[R] M) := { mul := λ f g, g.trans f, one := linear_equiv.refl R M, inv := λ f, f.symm, mul_assoc := λ f g h, by {ext, refl}, mul_one := λ f, by {ext, refl}, one_mul := λ f, by {ext, refl}, mul_left_inv := λ f, by {ext, exact f.left_inv x} } instance automorphism_group.to_linear_map_is_monoid_hom : is_monoid_hom (linear_equiv.to_linear_map : (M ≃ₗ[R] M) → (M →ₗ[R] M)) := { map_one := rfl, map_mul := λ f g, rfl } /-- The group of invertible linear maps from `M` to itself -/ @[reducible] def general_linear_group := units (M →ₗ[R] M) namespace general_linear_group variables {R M} instance : has_coe_to_fun (general_linear_group R M) := by apply_instance /-- An invertible linear map `f` determines an equivalence from `M` to itself. -/ def to_linear_equiv (f : general_linear_group R M) : (M ≃ₗ[R] M) := { inv_fun := f.inv.to_fun, left_inv := λ m, show (f.inv * f.val) m = m, by erw f.inv_val; simp, right_inv := λ m, show (f.val * f.inv) m = m, by erw f.val_inv; simp, ..f.val } /-- An equivalence from `M` to itself determines an invertible linear map. -/ def of_linear_equiv (f : (M ≃ₗ[R] M)) : general_linear_group R M := { val := f, inv := f.symm, val_inv := linear_map.ext $ λ _, f.apply_symm_apply _, inv_val := linear_map.ext $ λ _, f.symm_apply_apply _ } variables (R M) /-- The general linear group on `R` and `M` is multiplicatively equivalent to the type of linear equivalences between `M` and itself. -/ def general_linear_equiv : general_linear_group R M ≃* (M ≃ₗ[R] M) := { to_fun := to_linear_equiv, inv_fun := of_linear_equiv, left_inv := λ f, by { ext, refl }, right_inv := λ f, by { ext, refl }, map_mul' := λ x y, by {ext, refl} } @[simp] lemma general_linear_equiv_to_linear_map (f : general_linear_group R M) : (general_linear_equiv R M f : M →ₗ[R] M) = f := by {ext, refl} end general_linear_group end linear_map
4f25a8c58f7209cbba67d164c052e0c195a6092c
c777c32c8e484e195053731103c5e52af26a25d1
/archive/100-theorems-list/45_partition.lean
0fe42d3f7c6f1ca53301767f00bd34deeb96826f
[ "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
20,974
lean
/- Copyright (c) 2020 Bhavik Mehta, Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Aaron Anderson -/ import ring_theory.power_series.basic import combinatorics.partition import data.nat.parity import data.finset.nat_antidiagonal import data.fin.tuple.nat_antidiagonal import tactic.interval_cases import tactic.apply_fun import tactic.congrm /-! # Euler's Partition Theorem This file proves Theorem 45 from the [100 Theorems List](https://www.cs.ru.nl/~freek/100/). The theorem concerns the counting of integer partitions -- ways of writing a positive integer `n` as a sum of positive integer parts. Specifically, Euler proved that the number of integer partitions of `n` into *distinct* parts equals the number of partitions of `n` into *odd* parts. ## Proof outline The proof is based on the generating functions for odd and distinct partitions, which turn out to be equal: $$\prod_{i=0}^\infty \frac {1}{1-X^{2i+1}} = \prod_{i=0}^\infty (1+X^{i+1})$$ In fact, we do not take a limit: it turns out that comparing the `n`'th coefficients of the partial products up to `m := n + 1` is sufficient. In particular, we 1. define the partial product for the generating function for odd partitions `partial_odd_gf m` := $$\prod_{i=0}^m \frac {1}{1-X^{2i+1}}$$; 2. prove `odd_gf_prop`: if `m` is big enough (`m * 2 > n`), the partial product's coefficient counts the number of odd partitions; 3. define the partial product for the generating function for distinct partitions `partial_distinct_gf m` := $$\prod_{i=0}^m (1+X^{i+1})$$; 4. prove `distinct_gf_prop`: if `m` is big enough (`m + 1 > n`), the `n`th coefficient of the partial product counts the number of distinct partitions of `n`; 5. prove `same_coeffs`: if m is big enough (`m ≥ n`), the `n`th coefficient of the partial products are equal; 6. combine the above in `partition_theorem`. ## References https://en.wikipedia.org/wiki/Partition_(number_theory)#Odd_parts_and_distinct_parts -/ open power_series noncomputable theory variables {α : Type*} open finset open_locale big_operators open_locale classical /-- The partial product for the generating function for odd partitions. TODO: As `m` tends to infinity, this converges (in the `X`-adic topology). If `m` is sufficiently large, the `i`th coefficient gives the number of odd partitions of the natural number `i`: proved in `odd_gf_prop`. It is stated for an arbitrary field `α`, though it usually suffices to use `ℚ` or `ℝ`. -/ def partial_odd_gf (m : ℕ) [field α] := ∏ i in range m, (1 - (X : power_series α)^(2*i+1))⁻¹ /-- The partial product for the generating function for distinct partitions. TODO: As `m` tends to infinity, this converges (in the `X`-adic topology). If `m` is sufficiently large, the `i`th coefficient gives the number of distinct partitions of the natural number `i`: proved in `distinct_gf_prop`. It is stated for an arbitrary commutative semiring `α`, though it usually suffices to use `ℕ`, `ℚ` or `ℝ`. -/ def partial_distinct_gf (m : ℕ) [comm_semiring α] := ∏ i in range m, (1 + (X : power_series α)^(i+1)) /-- Functions defined only on `s`, which sum to `n`. In other words, a partition of `n` indexed by `s`. Every function in here is finitely supported, and the support is a subset of `s`. This should be thought of as a generalisation of `finset.nat.antidiagonal_tuple` where `antidiagonal_tuple k n` is the same thing as `cut (s : finset.univ (fin k)) n`. -/ def cut {ι : Type*} (s : finset ι) (n : ℕ) : finset (ι → ℕ) := finset.filter (λ f, s.sum f = n) ((s.pi (λ _, range (n+1))).map ⟨λ f i, if h : i ∈ s then f i h else 0, λ f g h, by { ext i hi, simpa [dif_pos hi] using congr_fun h i }⟩) lemma mem_cut {ι : Type*} (s : finset ι) (n : ℕ) (f : ι → ℕ) : f ∈ cut s n ↔ s.sum f = n ∧ ∀ i ∉ s, f i = 0 := begin rw [cut, mem_filter, and_comm, and_congr_right], intro h, simp only [mem_map, exists_prop, function.embedding.coe_fn_mk, mem_pi], split, { rintro ⟨_, _, rfl⟩ _ _, simp [dif_neg H] }, { intro hf, refine ⟨λ i hi, f i, λ i hi, _, _⟩, { rw [mem_range, nat.lt_succ_iff, ← h], apply single_le_sum _ hi, simp }, { ext, rw [dite_eq_ite, ite_eq_left_iff, eq_comm], exact hf x } } end lemma cut_equiv_antidiag (n : ℕ) : equiv.finset_congr (equiv.bool_arrow_equiv_prod _) (cut univ n) = nat.antidiagonal n := begin ext ⟨x₁, x₂⟩, simp_rw [equiv.finset_congr_apply, mem_map, equiv.to_embedding, function.embedding.coe_fn_mk, ←equiv.eq_symm_apply], simp [mem_cut, add_comm], end lemma cut_univ_fin_eq_antidiagonal_tuple (n : ℕ) (k : ℕ) : cut univ n = nat.antidiagonal_tuple k n := by { ext, simp [nat.mem_antidiagonal_tuple, mem_cut] } /-- There is only one `cut` of 0. -/ @[simp] lemma cut_zero {ι : Type*} (s : finset ι) : cut s 0 = {0} := begin -- In general it's nice to prove things using `mem_cut` but in this case it's easier to just -- use the definition. rw [cut, range_one, pi_const_singleton, map_singleton, function.embedding.coe_fn_mk, filter_singleton, if_pos, singleton_inj], { ext, split_ifs; refl }, rw sum_eq_zero_iff, intros x hx, apply dif_pos hx, end @[simp] lemma cut_empty_succ {ι : Type*} (n : ℕ) : cut (∅ : finset ι) (n+1) = ∅ := begin apply eq_empty_of_forall_not_mem, intros x hx, rw [mem_cut, sum_empty] at hx, cases hx.1, end lemma cut_insert {ι : Type*} (n : ℕ) (a : ι) (s : finset ι) (h : a ∉ s) : cut (insert a s) n = (nat.antidiagonal n).bUnion (λ (p : ℕ × ℕ), (cut s p.snd).map ⟨λ f, f + λ t, if t = a then p.fst else 0, add_left_injective _⟩) := begin ext f, rw [mem_cut, mem_bUnion, sum_insert h], split, { rintro ⟨rfl, h₁⟩, simp only [exists_prop, function.embedding.coe_fn_mk, mem_map, nat.mem_antidiagonal, prod.exists], refine ⟨f a, s.sum f, rfl, λ i, if i = a then 0 else f i, _, _⟩, { rw [mem_cut], refine ⟨_, _⟩, { rw [sum_ite], have : (filter (λ x, x ≠ a) s) = s, { apply filter_true_of_mem, rintro i hi rfl, apply h hi }, simp [this] }, { intros i hi, rw ite_eq_left_iff, intro hne, apply h₁, simp [not_or_distrib, hne, hi] } }, { ext, obtain rfl|h := eq_or_ne x a, { simp }, { simp [if_neg h] } } }, { simp only [mem_insert, function.embedding.coe_fn_mk, mem_map, nat.mem_antidiagonal, prod.exists, exists_prop, mem_cut, not_or_distrib], rintro ⟨p, q, rfl, g, ⟨rfl, hg₂⟩, rfl⟩, refine ⟨_, _⟩, { simp [sum_add_distrib, if_neg h, hg₂ _ h, add_comm] }, { rintro i ⟨h₁, h₂⟩, simp [if_neg h₁, hg₂ _ h₂] } } end lemma coeff_prod_range [comm_semiring α] {ι : Type*} (s : finset ι) (f : ι → power_series α) (n : ℕ) : coeff α n (∏ j in s, f j) = ∑ l in cut s n, ∏ i in s, coeff α (l i) (f i) := begin revert n, apply finset.induction_on s, { rintro ⟨_ | n⟩, { simp }, simp [cut_empty_succ, if_neg (nat.succ_ne_zero _)] }, intros a s hi ih n, rw [cut_insert _ _ _ hi, prod_insert hi, coeff_mul, sum_bUnion], { congrm finset.sum _ (λ i, _), simp only [sum_map, pi.add_apply, function.embedding.coe_fn_mk, prod_insert hi, if_pos rfl, ih, mul_sum], apply sum_congr rfl _, intros x hx, rw mem_cut at hx, rw [hx.2 a hi, zero_add], congrm _ * _, apply prod_congr rfl, intros k hk, rw [if_neg, add_zero], exact ne_of_mem_of_not_mem hk hi }, { simp only [set.pairwise_disjoint, set.pairwise, prod.forall, not_and, ne.def, nat.mem_antidiagonal, disjoint_left, mem_map, exists_prop, function.embedding.coe_fn_mk, exists_imp_distrib, not_exists, finset.mem_coe, function.on_fun, mem_cut, and_imp], rintro p₁ q₁ rfl p₂ q₂ h t x p hp hp2 hp3 q hq hq2 hq3, have z := hp3.trans hq3.symm, have := sum_congr (eq.refl s) (λ x _, function.funext_iff.1 z x), obtain rfl : q₁ = q₂, { simpa [sum_add_distrib, hp, hq, if_neg hi] using this }, obtain rfl : p₂ = p₁, { simpa using h }, exact (t rfl).elim } end /-- A convenience constructor for the power series whose coefficients indicate a subset. -/ def indicator_series (α : Type*) [semiring α] (s : set ℕ) : power_series α := power_series.mk (λ n, if n ∈ s then 1 else 0) lemma coeff_indicator (s : set ℕ) [semiring α] (n : ℕ) : coeff α n (indicator_series _ s) = if n ∈ s then 1 else 0 := coeff_mk _ _ lemma coeff_indicator_pos (s : set ℕ) [semiring α] (n : ℕ) (h : n ∈ s): coeff α n (indicator_series _ s) = 1 := by rw [coeff_indicator, if_pos h] lemma coeff_indicator_neg (s : set ℕ) [semiring α] (n : ℕ) (h : n ∉ s): coeff α n (indicator_series _ s) = 0 := by rw [coeff_indicator, if_neg h] lemma constant_coeff_indicator (s : set ℕ) [semiring α] : constant_coeff α (indicator_series _ s) = if 0 ∈ s then 1 else 0 := rfl lemma two_series (i : ℕ) [semiring α] : (1 + (X : power_series α)^i.succ) = indicator_series α {0, i.succ} := begin ext, simp only [coeff_indicator, coeff_one, coeff_X_pow, set.mem_insert_iff, set.mem_singleton_iff, map_add], cases n with d, { simp [(nat.succ_ne_zero i).symm] }, { simp [nat.succ_ne_zero d], }, end lemma num_series' [field α] (i : ℕ) : (1 - (X : power_series α)^(i+1))⁻¹ = indicator_series α { k | i + 1 ∣ k } := begin rw power_series.inv_eq_iff_mul_eq_one, { ext, cases n, { simp [mul_sub, zero_pow, constant_coeff_indicator] }, { simp only [coeff_one, if_neg n.succ_ne_zero, mul_sub, mul_one, coeff_indicator, linear_map.map_sub], simp_rw [coeff_mul, coeff_X_pow, coeff_indicator, boole_mul, sum_ite, filter_filter, sum_const_zero, add_zero, sum_const, nsmul_eq_mul, mul_one, sub_eq_iff_eq_add, zero_add, filter_congr_decidable], symmetry, split_ifs, { suffices : ((nat.antidiagonal n.succ).filter (λ (a : ℕ × ℕ), i + 1 ∣ a.fst ∧ a.snd = i + 1)).card = 1, { simp only [set.mem_set_of_eq], rw this, norm_cast }, rw card_eq_one, cases h with p hp, refine ⟨((i+1) * (p-1), i+1), _⟩, ext ⟨a₁, a₂⟩, simp only [mem_filter, prod.mk.inj_iff, nat.mem_antidiagonal, mem_singleton], split, { rintro ⟨a_left, ⟨a, rfl⟩, rfl⟩, refine ⟨_, rfl⟩, rw [nat.mul_sub_left_distrib, ← hp, ← a_left, mul_one, nat.add_sub_cancel] }, { rintro ⟨rfl, rfl⟩, cases p, { rw mul_zero at hp, cases hp }, rw hp, simp [nat.succ_eq_add_one, mul_add] } }, { suffices : (filter (λ (a : ℕ × ℕ), i + 1 ∣ a.fst ∧ a.snd = i + 1) (nat.antidiagonal n.succ)).card = 0, { simp only [set.mem_set_of_eq], rw this, norm_cast }, rw card_eq_zero, apply eq_empty_of_forall_not_mem, simp only [prod.forall, mem_filter, not_and, nat.mem_antidiagonal], rintro _ h₁ h₂ ⟨a, rfl⟩ rfl, apply h, simp [← h₂] } } }, { simp [zero_pow] }, end def mk_odd : ℕ ↪ ℕ := ⟨λ i, 2 * i + 1, λ x y h, by linarith⟩ -- The main workhorse of the partition theorem proof. lemma partial_gf_prop (α : Type*) [comm_semiring α] (n : ℕ) (s : finset ℕ) (hs : ∀ i ∈ s, 0 < i) (c : ℕ → set ℕ) (hc : ∀ i ∉ s, 0 ∈ c i) : (finset.card ((univ : finset (nat.partition n)).filter (λ p, (∀ j, p.parts.count j ∈ c j) ∧ ∀ j ∈ p.parts, j ∈ s)) : α) = (coeff α n) (∏ (i : ℕ) in s, indicator_series α ((* i) '' c i)) := begin simp_rw [coeff_prod_range, coeff_indicator, prod_boole, sum_boole], congr' 1, refine finset.card_congr (λ p _ i, multiset.count i p.parts • i) _ _ _, { simp only [mem_filter, mem_cut, mem_univ, true_and, exists_prop, and_assoc, and_imp, smul_eq_zero, function.embedding.coe_fn_mk, exists_imp_distrib], rintro ⟨p, hp₁, hp₂⟩ hp₃ hp₄, dsimp only at *, refine ⟨_, _, _⟩, { rw [←hp₂, ←sum_multiset_count_of_subset p s (λ x hx, hp₄ _ (multiset.mem_to_finset.mp hx))] }, { intros i hi, left, exact multiset.count_eq_zero_of_not_mem (mt (hp₄ i) hi) }, { exact λ i hi, ⟨_, hp₃ i, rfl⟩ } }, { intros p₁ p₂ hp₁ hp₂ h, apply nat.partition.ext, simp only [true_and, mem_univ, mem_filter] at hp₁ hp₂, ext i, rw function.funext_iff at h, specialize h i, cases i, { rw multiset.count_eq_zero_of_not_mem, rw multiset.count_eq_zero_of_not_mem, intro a, exact nat.lt_irrefl 0 (hs 0 (hp₂.2 0 a)), intro a, exact nat.lt_irrefl 0 (hs 0 (hp₁.2 0 a)) }, { rwa [nat.nsmul_eq_mul, nat.nsmul_eq_mul, mul_left_inj' i.succ_ne_zero] at h } }, { simp only [mem_filter, mem_cut, mem_univ, exists_prop, true_and, and_assoc], rintros f ⟨hf₁, hf₂, hf₃⟩, refine ⟨⟨∑ i in s, multiset.replicate (f i / i) i, _, _⟩, _, _, _⟩, { intros i hi, simp only [exists_prop, mem_sum, mem_map, function.embedding.coe_fn_mk] at hi, rcases hi with ⟨t, ht, z⟩, apply hs, rwa multiset.eq_of_mem_replicate z }, { simp_rw [multiset.sum_sum, multiset.sum_replicate, nat.nsmul_eq_mul, ←hf₁], refine sum_congr rfl (λ i hi, nat.div_mul_cancel _), rcases hf₃ i hi with ⟨w, hw, hw₂⟩, rw ← hw₂, exact dvd_mul_left _ _ }, { intro i, simp_rw [multiset.count_sum', multiset.count_replicate, sum_ite_eq], split_ifs with h h, { rcases hf₃ i h with ⟨w, hw₁, hw₂⟩, rwa [← hw₂, nat.mul_div_cancel _ (hs i h)] }, { exact hc _ h } }, { intros i hi, rw mem_sum at hi, rcases hi with ⟨j, hj₁, hj₂⟩, rwa multiset.eq_of_mem_replicate hj₂ }, { ext i, simp_rw [multiset.count_sum', multiset.count_replicate, sum_ite_eq], split_ifs, { apply nat.div_mul_cancel, rcases hf₃ i h with ⟨w, hw, hw₂⟩, apply dvd.intro_left _ hw₂ }, { rw [zero_smul, hf₂ i h] } } }, end lemma partial_odd_gf_prop [field α] (n m : ℕ) : (finset.card ((univ : finset (nat.partition n)).filter (λ p, ∀ j ∈ p.parts, j ∈ (range m).map mk_odd)) : α) = coeff α n (partial_odd_gf m) := begin rw partial_odd_gf, convert partial_gf_prop α n ((range m).map mk_odd) _ (λ _, set.univ) (λ _ _, trivial) using 2, { congrm card (filter (λ p, _) _), simp only [true_and, forall_const, set.mem_univ] }, { rw finset.prod_map, simp_rw num_series', congrm finset.prod _ (λ x, indicator_series α _), ext k, split, { rintro ⟨p, rfl⟩, refine ⟨p, ⟨⟩, _⟩, apply mul_comm }, rintro ⟨a_w, -, rfl⟩, apply dvd.intro_left a_w rfl }, { intro i, rw mem_map, rintro ⟨a, -, rfl⟩, exact nat.succ_pos _ }, end /-- If m is big enough, the partial product's coefficient counts the number of odd partitions -/ theorem odd_gf_prop [field α] (n m : ℕ) (h : n < m * 2) : (finset.card (nat.partition.odds n) : α) = coeff α n (partial_odd_gf m) := begin rw [← partial_odd_gf_prop], congrm card (filter (λ p, (_ : Prop)) _), apply ball_congr, intros i hi, have hin : i ≤ n, { simpa [p.parts_sum] using multiset.single_le_sum (λ _ _, nat.zero_le _) _ hi }, simp only [mk_odd, exists_prop, mem_range, function.embedding.coe_fn_mk, mem_map], split, { intro hi₂, have := nat.mod_add_div i 2, rw nat.not_even_iff at hi₂, rw [hi₂, add_comm] at this, refine ⟨i / 2, _, this⟩, rw nat.div_lt_iff_lt_mul zero_lt_two, exact lt_of_le_of_lt hin h }, { rintro ⟨a, -, rfl⟩, rw even_iff_two_dvd, apply nat.two_not_dvd_two_mul_add_one }, end lemma partial_distinct_gf_prop [comm_semiring α] (n m : ℕ) : (finset.card ((univ : finset (nat.partition n)).filter (λ p, p.parts.nodup ∧ ∀ j ∈ p.parts, j ∈ (range m).map ⟨nat.succ, nat.succ_injective⟩)) : α) = coeff α n (partial_distinct_gf m) := begin rw partial_distinct_gf, convert partial_gf_prop α n ((range m).map ⟨nat.succ, nat.succ_injective⟩) _ (λ _, {0, 1}) (λ _ _, or.inl rfl) using 2, { congrm card (filter (λ p, _ ∧ _) _), rw multiset.nodup_iff_count_le_one, congrm ∀ (i : ℕ), (_ : Prop), rcases multiset.count i p.parts with _|_|ms; simp }, { simp_rw [finset.prod_map, two_series], congrm finset.prod _ (λ i, indicator_series _ _), simp [set.image_pair] }, { simp only [mem_map, function.embedding.coe_fn_mk], rintro i ⟨_, _, rfl⟩, apply nat.succ_pos } end /-- If m is big enough, the partial product's coefficient counts the number of distinct partitions -/ theorem distinct_gf_prop [comm_semiring α] (n m : ℕ) (h : n < m + 1) : ((nat.partition.distincts n).card : α) = coeff α n (partial_distinct_gf m) := begin erw [← partial_distinct_gf_prop], congrm card (filter (λ p, _) _), apply (and_iff_left _).symm, intros i hi, have : i ≤ n, { simpa [p.parts_sum] using multiset.single_le_sum (λ _ _, nat.zero_le _) _ hi }, simp only [mk_odd, exists_prop, mem_range, function.embedding.coe_fn_mk, mem_map], refine ⟨i-1, _, nat.succ_pred_eq_of_pos (p.parts_pos hi)⟩, rw tsub_lt_iff_right (nat.one_le_iff_ne_zero.mpr (p.parts_pos hi).ne'), exact lt_of_le_of_lt this h, end /-- The key proof idea for the partition theorem, showing that the generating functions for both sequences are ultimately the same (since the factor converges to 0 as m tends to infinity). It's enough to not take the limit though, and just consider large enough `m`. -/ lemma same_gf [field α] (m : ℕ) : partial_odd_gf m * (range m).prod (λ i, (1 - (X : power_series α)^(m+i+1))) = partial_distinct_gf m := begin rw [partial_odd_gf, partial_distinct_gf], induction m with m ih, { simp }, rw nat.succ_eq_add_one, set π₀ : power_series α := ∏ i in range m, (1 - X ^ (m + 1 + i + 1)) with hπ₀, set π₁ : power_series α := ∏ i in range m, (1 - X ^ (2 * i + 1))⁻¹ with hπ₁, set π₂ : power_series α := ∏ i in range m, (1 - X ^ (m + i + 1)) with hπ₂, set π₃ : power_series α := ∏ i in range m, (1 + X ^ (i + 1)) with hπ₃, rw ←hπ₃ at ih, have h : constant_coeff α (1 - X ^ (2 * m + 1)) ≠ 0, { rw [ring_hom.map_sub, ring_hom.map_pow, constant_coeff_one, constant_coeff_X, zero_pow (2 * m).succ_pos, sub_zero], exact one_ne_zero }, calc (∏ i in range (m + 1), (1 - X ^ (2 * i + 1))⁻¹) * ∏ i in range (m + 1), (1 - X ^ (m + 1 + i + 1)) = π₁ * (1 - X ^ (2 * m + 1))⁻¹ * (π₀ * (1 - X ^ (m + 1 + m + 1))) : by rw [prod_range_succ _ m, ←hπ₁, prod_range_succ _ m, ←hπ₀] ... = π₁ * (1 - X ^ (2 * m + 1))⁻¹ * (π₀ * ((1 + X ^ (m + 1)) * (1 - X ^ (m + 1)))) : by rw [←sq_sub_sq, one_pow, add_assoc _ m 1, ←two_mul (m + 1), pow_mul'] ... = π₀ * (1 - X ^ (m + 1)) * (1 - X ^ (2 * m + 1))⁻¹ * (π₁ * (1 + X ^ (m + 1))) : by ring ... = (∏ i in range (m + 1), (1 - X ^ (m + 1 + i))) * (1 - X ^ (2 * m + 1))⁻¹ * (π₁ * (1 + X ^ (m + 1))) : by { rw [prod_range_succ', add_zero, hπ₀], simp_rw ←add_assoc } ... = π₂ * (1 - X ^ (m + 1 + m)) * (1 - X ^ (2 * m + 1))⁻¹ * (π₁ * (1 + X ^ (m + 1))) : by { rw [add_right_comm, hπ₂, ←prod_range_succ], simp_rw [add_right_comm] } ... = π₂ * (1 - X ^ (2 * m + 1)) * (1 - X ^ (2 * m + 1))⁻¹ * (π₁ * (1 + X ^ (m + 1))) : by rw [two_mul, add_right_comm _ m 1] ... = (1 - X ^ (2 * m + 1)) * (1 - X ^ (2 * m + 1))⁻¹ * π₂ * (π₁ * (1 + X ^ (m + 1))) : by ring ... = π₂ * (π₁ * (1 + X ^ (m + 1))) : by rw [power_series.mul_inv_cancel _ h, one_mul] ... = π₁ * π₂ * (1 + X ^ (m + 1)) : by ring ... = π₃ * (1 + X ^ (m + 1)) : by rw ih ... = _ : by rw prod_range_succ, end lemma same_coeffs [field α] (m n : ℕ) (h : n ≤ m) : coeff α n (partial_odd_gf m) = coeff α n (partial_distinct_gf m) := begin rw [← same_gf, coeff_mul_prod_one_sub_of_lt_order], rintros i -, rw order_X_pow, exact_mod_cast nat.lt_succ_of_le (le_add_right h), end theorem partition_theorem (n : ℕ) : (nat.partition.odds n).card = (nat.partition.distincts n).card := begin -- We need the counts to live in some field (which contains ℕ), so let's just use ℚ suffices : ((nat.partition.odds n).card : ℚ) = (nat.partition.distincts n).card, { exact_mod_cast this }, rw distinct_gf_prop n (n+1) (by linarith), rw odd_gf_prop n (n+1) (by linarith), apply same_coeffs (n+1) n n.le_succ, end
93283833770325a91431155e2f545f95fd00aa10
4727251e0cd73359b15b664c3170e5d754078599
/src/ring_theory/localization/inv_submonoid.lean
4c1ba1919ad435c9a756cf45bfc8e14b943cfcad
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
4,010
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro, Johan Commelin, Amelia Livingston, Anne Baanen -/ import group_theory.submonoid.inverses import ring_theory.finiteness import ring_theory.localization.basic import tactic.ring_exp /-! # Submonoid of inverses ## Main definitions * `is_localization.inv_submonoid M S` is the submonoid of `S = M⁻¹R` consisting of inverses of each element `x ∈ M` ## Implementation notes See `src/ring_theory/localization/basic.lean` for a design overview. ## Tags localization, ring localization, commutative ring localization, characteristic predicate, commutative ring, field of fractions -/ variables {R : Type*} [comm_ring R] (M : submonoid R) (S : Type*) [comm_ring S] variables [algebra R S] {P : Type*} [comm_ring P] open function open_locale big_operators namespace is_localization section inv_submonoid variables (M S) /-- The submonoid of `S = M⁻¹R` consisting of `{ 1 / x | x ∈ M }`. -/ def inv_submonoid : submonoid S := (M.map (algebra_map R S : R →* S)).left_inv variable [is_localization M S] lemma submonoid_map_le_is_unit : M.map (algebra_map R S : R →* S) ≤ is_unit.submonoid S := by { rintros _ ⟨a, ha, rfl⟩, exact is_localization.map_units S ⟨_, ha⟩ } /-- There is an equivalence of monoids between the image of `M` and `inv_submonoid`. -/ noncomputable abbreviation equiv_inv_submonoid : M.map (algebra_map R S : R →* S) ≃* inv_submonoid M S := ((M.map (algebra_map R S : R →* S)).left_inv_equiv (submonoid_map_le_is_unit M S)).symm /-- There is a canonical map from `M` to `inv_submonoid` sending `x` to `1 / x`. -/ noncomputable def to_inv_submonoid : M →* inv_submonoid M S := (equiv_inv_submonoid M S).to_monoid_hom.comp ((algebra_map R S : R →* S).submonoid_map M) lemma to_inv_submonoid_surjective : function.surjective (to_inv_submonoid M S) := function.surjective.comp (equiv.surjective _) (monoid_hom.submonoid_map_surjective _ _) @[simp] lemma to_inv_submonoid_mul (m : M) : (to_inv_submonoid M S m : S) * (algebra_map R S m) = 1 := submonoid.left_inv_equiv_symm_mul _ _ _ @[simp] lemma mul_to_inv_submonoid (m : M) : (algebra_map R S m) * (to_inv_submonoid M S m : S) = 1 := submonoid.mul_left_inv_equiv_symm _ _ ⟨_, _⟩ @[simp] lemma smul_to_inv_submonoid (m : M) : m • (to_inv_submonoid M S m : S) = 1 := by { convert mul_to_inv_submonoid M S m, rw ← algebra.smul_def, refl } variables {S} lemma surj' (z : S) : ∃ (r : R) (m : M), z = r • to_inv_submonoid M S m := begin rcases is_localization.surj M z with ⟨⟨r, m⟩, e : z * _ = algebra_map R S r⟩, refine ⟨r, m, _⟩, rw [algebra.smul_def, ← e, mul_assoc], simp, end lemma to_inv_submonoid_eq_mk' (x : M) : (to_inv_submonoid M S x : S) = mk' S 1 x := by { rw ← (is_localization.map_units S x).mul_left_inj, simp } lemma mem_inv_submonoid_iff_exists_mk' (x : S) : x ∈ inv_submonoid M S ↔ ∃ m : M, mk' S 1 m = x := begin simp_rw ← to_inv_submonoid_eq_mk', exact ⟨λ h, ⟨_, congr_arg subtype.val (to_inv_submonoid_surjective M S ⟨x, h⟩).some_spec⟩, λ h, h.some_spec ▸ (to_inv_submonoid M S h.some).prop⟩ end variables (S) lemma span_inv_submonoid : submodule.span R (inv_submonoid M S : set S) = ⊤ := begin rw eq_top_iff, rintros x -, rcases is_localization.surj' M x with ⟨r, m, rfl⟩, exact submodule.smul_mem _ _ (submodule.subset_span (to_inv_submonoid M S m).prop), end lemma finite_type_of_monoid_fg [monoid.fg M] : algebra.finite_type R S := begin have := monoid.fg_of_surjective _ (to_inv_submonoid_surjective M S), rw monoid.fg_iff_submonoid_fg at this, rcases this with ⟨s, hs⟩, refine ⟨⟨s, _⟩⟩, rw eq_top_iff, rintro x -, change x ∈ ((algebra.adjoin R _ : subalgebra R S).to_submodule : set S), rw [algebra.adjoin_eq_span, hs, span_inv_submonoid], trivial end end inv_submonoid end is_localization
c421adb457bddca597bcdd1a15685a71ac50439d
9dc8cecdf3c4634764a18254e94d43da07142918
/src/measure_theory/covering/vitali_family.lean
bd0d9afdbf04ad37c74f4f3698f048f8e8018a95
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
11,206
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import measure_theory.measure.measure_space /-! # Vitali families On a metric space `X` with a measure `μ`, consider for each `x : X` a family of measurable sets with nonempty interiors, called `sets_at x`. This family is a Vitali family if it satisfies the following property: consider a (possibly non-measurable) set `s`, and for any `x` in `s` a subfamily `f x` of `sets_at x` containing sets of arbitrarily small diameter. Then one can extract a disjoint subfamily covering almost all `s`. Vitali families are provided by covering theorems such as the Besicovitch covering theorem or the Vitali covering theorem. They make it possible to formulate general versions of theorems on differentiations of measure that apply in both contexts. This file gives the basic definition of Vitali families. More interesting developments of this notion are deferred to other files: * constructions of specific Vitali families are provided by the Besicovitch covering theorem, in `besicovitch.vitali_family`, and by the Vitali covering theorem, in `vitali.vitali_family`. * The main theorem on differentiation of measures along a Vitali family is proved in `vitali_family.ae_tendsto_rn_deriv`. ## Main definitions * `vitali_family μ` is a structure made, for each `x : X`, of a family of sets around `x`, such that one can extract an almost everywhere disjoint covering from any subfamily containing sets of arbitrarily small diameters. Let `v` be such a Vitali family. * `v.fine_subfamily_on` describes the subfamilies of `v` from which one can extract almost everywhere disjoint coverings. This property, called `v.fine_subfamily_on.exists_disjoint_covering_ae`, is essentially a restatement of the definition of a Vitali family. We also provide an API to use efficiently such a disjoint covering. * `v.filter_at x` is a filter on sets of `X`, such that convergence with respect to this filter means convergence when sets in the Vitali family shrink towards `x`. ## References * [Herbert Federer, Geometric Measure Theory, Chapter 2.8][Federer1996] (Vitali families are called Vitali relations there) -/ open measure_theory metric set filter topological_space measure_theory.measure open_locale filter measure_theory topological_space variables {α : Type*} [metric_space α] /-- On a metric space `X` with a measure `μ`, consider for each `x : X` a family of measurable sets with nonempty interiors, called `sets_at x`. This family is a Vitali family if it satisfies the following property: consider a (possibly non-measurable) set `s`, and for any `x` in `s` a subfamily `f x` of `sets_at x` containing sets of arbitrarily small diameter. Then one can extract a disjoint subfamily covering almost all `s`. Vitali families are provided by covering theorems such as the Besicovitch covering theorem or the Vitali covering theorem. They make it possible to formulate general versions of theorems on differentiations of measure that apply in both contexts. -/ @[nolint has_nonempty_instance] structure vitali_family {m : measurable_space α} (μ : measure α) := (sets_at : Π (x : α), set (set α)) (measurable_set' : ∀ (x : α), ∀ (a : set α), a ∈ sets_at x → measurable_set a) (nonempty_interior : ∀ (x : α), ∀ (y : set α), y ∈ sets_at x → (interior y).nonempty) (nontrivial : ∀ (x : α) (ε > (0 : ℝ)), ∃ y ∈ sets_at x, y ⊆ closed_ball x ε) (covering : ∀ (s : set α) (f : Π (x : α), set (set α)), (∀ x ∈ s, f x ⊆ sets_at x) → (∀ (x ∈ s) (ε > (0 : ℝ)), ∃ a ∈ f x, a ⊆ closed_ball x ε) → ∃ (t : set α) (u : α → set α), t ⊆ s ∧ t.pairwise_disjoint u ∧ (∀ x ∈ t, u x ∈ f x) ∧ μ (s \ ⋃ x ∈ t, u x) = 0) namespace vitali_family variables {m0 : measurable_space α} {μ : measure α} include μ /-- A Vitali family for a measure `μ` is also a Vitali family for any measure absolutely continuous with respect to `μ`. -/ def mono (v : vitali_family μ) (ν : measure α) (hν : ν ≪ μ) : vitali_family ν := { sets_at := v.sets_at, measurable_set' := v.measurable_set', nonempty_interior := v.nonempty_interior, nontrivial := v.nontrivial, covering := λ s f h h', begin rcases v.covering s f h h' with ⟨t, u, ts, u_disj, uf, μu⟩, exact ⟨t, u, ts, u_disj, uf, hν μu⟩ end } /-- Given a Vitali family `v` for a measure `μ`, a family `f` is a fine subfamily on a set `s` if every point `x` in `s` belongs to arbitrarily small sets in `v.sets_at x ∩ f x`. This is precisely the subfamilies for which the Vitali family definition ensures that one can extract a disjoint covering of almost all `s`. -/ def fine_subfamily_on (v : vitali_family μ) (f : α → set (set α)) (s : set α) : Prop := ∀ x ∈ s, ∀ (ε > 0), ∃ a ∈ v.sets_at x ∩ f x, a ⊆ closed_ball x ε namespace fine_subfamily_on variables {v : vitali_family μ} {f : α → set (set α)} {s : set α} (h : v.fine_subfamily_on f s) include h theorem exists_disjoint_covering_ae : ∃ (t : set α) (u : α → set α), t ⊆ s ∧ t.pairwise_disjoint u ∧ (∀ x ∈ t, u x ∈ v.sets_at x ∩ f x) ∧ μ (s \ ⋃ x ∈ t, u x) = 0 := v.covering s (λ x, v.sets_at x ∩ f x) (λ x hx, inter_subset_left _ _) h /-- Given `h : v.fine_subfamily_on f s`, then `h.index` is a subset of `s` parametrizing a disjoint covering of almost every `s`. -/ protected def index : set α := h.exists_disjoint_covering_ae.some /-- Given `h : v.fine_subfamily_on f s`, then `h.covering x` is a set in the family, for `x ∈ h.index`, such that these sets form a disjoint covering of almost every `s`. -/ protected def covering : α → set α := h.exists_disjoint_covering_ae.some_spec.some lemma index_subset : h.index ⊆ s := h.exists_disjoint_covering_ae.some_spec.some_spec.1 lemma covering_disjoint : h.index.pairwise_disjoint h.covering := h.exists_disjoint_covering_ae.some_spec.some_spec.2.1 lemma covering_disjoint_subtype : pairwise (disjoint on (λ x : h.index, h.covering x)) := (pairwise_subtype_iff_pairwise_set _ _).2 h.covering_disjoint lemma covering_mem {x : α} (hx : x ∈ h.index) : h.covering x ∈ f x := (h.exists_disjoint_covering_ae.some_spec.some_spec.2.2.1 x hx).2 lemma covering_mem_family {x : α} (hx : x ∈ h.index) : h.covering x ∈ v.sets_at x := (h.exists_disjoint_covering_ae.some_spec.some_spec.2.2.1 x hx).1 lemma measure_diff_bUnion : μ (s \ ⋃ x ∈ h.index, h.covering x) = 0 := h.exists_disjoint_covering_ae.some_spec.some_spec.2.2.2 lemma index_countable [second_countable_topology α] : h.index.countable := h.covering_disjoint.countable_of_nonempty_interior (λ x hx, v.nonempty_interior _ _ (h.covering_mem_family hx)) protected lemma measurable_set_u {x : α} (hx : x ∈ h.index) : measurable_set (h.covering x) := v.measurable_set' x _ (h.covering_mem_family hx) lemma measure_le_tsum_of_absolutely_continuous [second_countable_topology α] {ρ : measure α} (hρ : ρ ≪ μ) : ρ s ≤ ∑' (x : h.index), ρ (h.covering x) := calc ρ s ≤ ρ ((s \ ⋃ (x ∈ h.index), h.covering x) ∪ (⋃ (x ∈ h.index), h.covering x)) : measure_mono (by simp only [subset_union_left, diff_union_self]) ... ≤ ρ (s \ ⋃ (x ∈ h.index), h.covering x) + ρ (⋃ (x ∈ h.index), h.covering x) : measure_union_le _ _ ... = ∑' (x : h.index), ρ (h.covering x) : by rw [hρ h.measure_diff_bUnion, measure_bUnion h.index_countable h.covering_disjoint (λ x hx, h.measurable_set_u hx), zero_add] lemma measure_le_tsum [second_countable_topology α] : μ s ≤ ∑' (x : h.index), μ (h.covering x) := h.measure_le_tsum_of_absolutely_continuous measure.absolutely_continuous.rfl end fine_subfamily_on variable (v : vitali_family μ) include v /-- Given a vitali family `v`, then `v.filter_at x` is the filter on `set α` made of those families that contain all sets of `v.sets_at x` of a sufficiently small diameter. This filter makes it possible to express limiting behavior when sets in `v.sets_at x` shrink to `x`. -/ def filter_at (x : α) : filter (set α) := ⨅ (ε ∈ Ioi (0 : ℝ)), 𝓟 {a ∈ v.sets_at x | a ⊆ closed_ball x ε} lemma mem_filter_at_iff {x : α} {s : set (set α)} : (s ∈ v.filter_at x) ↔ ∃ (ε > (0 : ℝ)), ∀ a ∈ v.sets_at x, a ⊆ closed_ball x ε → a ∈ s := begin simp only [filter_at, exists_prop, gt_iff_lt], rw mem_binfi_of_directed, { simp only [subset_def, and_imp, exists_prop, mem_sep_eq, mem_Ioi, mem_principal] }, { simp only [directed_on, exists_prop, ge_iff_le, le_principal_iff, mem_Ioi, order.preimage, mem_principal], assume x hx y hy, refine ⟨min x y, lt_min hx hy, λ a ha, ⟨ha.1, ha.2.trans (closed_ball_subset_closed_ball (min_le_left _ _))⟩, λ a ha, ⟨ha.1, ha.2.trans (closed_ball_subset_closed_ball (min_le_right _ _))⟩⟩ }, { exact ⟨(1 : ℝ), mem_Ioi.2 zero_lt_one⟩ } end instance filter_at_ne_bot (x : α) : (v.filter_at x).ne_bot := begin simp only [ne_bot_iff, ←empty_mem_iff_bot, mem_filter_at_iff, not_exists, exists_prop, mem_empty_eq, and_true, gt_iff_lt, not_and, ne.def, not_false_iff, not_forall], assume ε εpos, obtain ⟨w, w_sets, hw⟩ : ∃ (w ∈ v.sets_at x), w ⊆ closed_ball x ε := v.nontrivial x ε εpos, exact ⟨w, w_sets, hw⟩ end lemma eventually_filter_at_iff {x : α} {P : set α → Prop} : (∀ᶠ a in v.filter_at x, P a) ↔ ∃ (ε > (0 : ℝ)), ∀ a ∈ v.sets_at x, a ⊆ closed_ball x ε → P a := v.mem_filter_at_iff lemma eventually_filter_at_mem_sets (x : α) : ∀ᶠ a in v.filter_at x, a ∈ v.sets_at x := begin simp only [eventually_filter_at_iff, exists_prop, and_true, gt_iff_lt, implies_true_iff] {contextual := tt}, exact ⟨1, zero_lt_one⟩ end lemma eventually_filter_at_measurable_set (x : α) : ∀ᶠ a in v.filter_at x, measurable_set a := by { filter_upwards [v.eventually_filter_at_mem_sets x] with _ ha using v.measurable_set' _ _ ha } lemma frequently_filter_at_iff {x : α} {P : set α → Prop} : (∃ᶠ a in v.filter_at x, P a) ↔ ∀ (ε > (0 : ℝ)), ∃ a ∈ v.sets_at x, a ⊆ closed_ball x ε ∧ P a := by simp only [filter.frequently, eventually_filter_at_iff, not_exists, exists_prop, not_and, not_not, not_forall] lemma eventually_filter_at_subset_of_nhds {x : α} {o : set α} (hx : o ∈ 𝓝 x) : ∀ᶠ a in v.filter_at x, a ⊆ o := begin rw eventually_filter_at_iff, rcases metric.mem_nhds_iff.1 hx with ⟨ε, εpos, hε⟩, exact ⟨ε/2, half_pos εpos, λ a av ha, ha.trans ((closed_ball_subset_ball (half_lt_self εpos)).trans hε)⟩ end lemma fine_subfamily_on_of_frequently (v : vitali_family μ) (f : α → set (set α)) (s : set α) (h : ∀ x ∈ s, ∃ᶠ a in v.filter_at x, a ∈ f x) : v.fine_subfamily_on f s := begin assume x hx ε εpos, obtain ⟨a, av, ha, af⟩ : ∃ (a : set α) (H : a ∈ v.sets_at x), a ⊆ closed_ball x ε ∧ a ∈ f x := v.frequently_filter_at_iff.1 (h x hx) ε εpos, exact ⟨a, ⟨av, af⟩, ha⟩, end end vitali_family
d30886b00c95b80324f6694cb71c4c4885b7cb07
41e069072396dcd54bd9fdadb27cfd35fd07016a
/src/K/full_language.lean
1a1943c5ab82279cf93dca3a2a5a3a374b818353
[ "MIT" ]
permissive
semorrison/ModalTab
438ad601bd2631ab9cfe1e61f0d1337a36e2367e
cc94099194a2b69f84eb7a770b7aac711825179f
refs/heads/master
1,585,939,884,891
1,540,961,947,000
1,540,961,947,000
155,500,181
0
0
MIT
1,540,961,175,000
1,540,961,175,000
null
UTF-8
Lean
false
false
10,204
lean
import defs inductive fml | var (n : nat) | neg (φ : fml) | and (φ ψ : fml) | or (φ ψ : fml) | impl (φ ψ : fml) | box (φ : fml) | dia (φ : fml) open fml @[simp] def fml_size : fml → ℕ | (var n) := 1 | (neg φ) := fml_size φ + 1 | (and φ ψ) := fml_size φ + fml_size ψ + 1 | (or φ ψ) := fml_size φ + fml_size ψ + 1 | (impl φ ψ) := fml_size φ + fml_size ψ + 2 | (box φ) := fml_size φ + 1 | (dia φ) := fml_size φ + 1 @[simp] def fml_force {states : Type} (k : kripke states) : states → fml → Prop | s (var n) := k.val n s | s (neg φ) := ¬ fml_force s φ | s (and φ ψ) := fml_force s φ ∧ fml_force s ψ | s (or φ ψ) := fml_force s φ ∨ fml_force s ψ | s (impl φ ψ) := ¬ fml_force s φ ∨ fml_force s ψ | s (box φ) := ∀ s', k.rel s s' → fml_force s' φ | s (dia φ) := ∃ s', k.rel s s' ∧ fml_force s' φ @[simp] def fml.to_nnf : fml → nnf | (var n) := nnf.var n | (neg (var n)) := nnf.neg n | (neg (neg φ)) := fml.to_nnf φ | (neg (and φ ψ)) := nnf.or (fml.to_nnf (neg φ)) (fml.to_nnf (neg ψ)) | (neg (or φ ψ)) := nnf.and (fml.to_nnf (neg φ)) (fml.to_nnf (neg ψ)) | (neg (impl φ ψ)) := nnf.and (fml.to_nnf φ) (fml.to_nnf (neg ψ)) | (neg (box φ)) := nnf.dia (fml.to_nnf (neg φ)) | (neg (dia φ)) := nnf.box (fml.to_nnf (neg φ)) | (and φ ψ) := nnf.and (fml.to_nnf φ) (fml.to_nnf ψ) | (or φ ψ) := nnf.or (fml.to_nnf φ) (fml.to_nnf ψ) | (impl φ ψ) := nnf.or (fml.to_nnf (neg φ)) (fml.to_nnf ψ) | (box φ) := nnf.box (fml.to_nnf φ) | (dia φ) := nnf.dia (fml.to_nnf φ) using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf fml_size⟩]} namespace classical variables {α : Sort*} {p : α → Prop} variables {a b c d : Prop} local attribute [instance] prop_decidable theorem not_and_distrib : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if ha : a then or.inr (λ hb, h ⟨ha, hb⟩) else or.inl ha, not_and_of_not_or_not⟩ @[simp] theorem not_imp : ¬(a → b) ↔ a ∧ ¬b := ⟨λ h, ⟨of_not_imp h, not_of_not_imp h⟩, not_imp_of_and_not⟩ end classical @[simp] def trans_size_left {st} (k : kripke st) : (Σ' (s) (φ : fml), fml_force k s φ) → ℕ := λ h, fml_size h.snd.fst @[simp] def trans_size_right {st} (k : kripke st) : (Σ' (s : st) (φ : fml), force k s (fml.to_nnf φ)) → ℕ := λ h, fml_size h.snd.fst /- This direction requires choice -/ theorem trans_left {st} (k : kripke st) : Π s φ, fml_force k s φ → force k s (fml.to_nnf φ) | s (var n) h := h | s (neg (var n)) h := h | s (neg (neg φ)) h := by dsimp at h; rw classical.not_not at h; exact trans_left _ _ h | s (neg (and φ ψ)) h := begin have := trans_left s (neg φ), dsimp at h, rw classical.not_and_distrib at h, cases h with l r, {simp [trans_left _ (neg φ) l]}, {simp [trans_left _ (neg ψ) r]} end | s (neg (or φ ψ)) h := begin dsimp at h, rw not_or_distrib at h, simp [and.intro (trans_left _ (neg φ) h.1) (trans_left _ (neg ψ) h.2)] end | s (neg (impl φ ψ)) h := begin dsimp at h, rw not_or_distrib at h, have : force k s (fml.to_nnf φ), { apply trans_left, have := h.1, rw classical.not_not at this, exact this }, simp [and.intro this (trans_left _ (neg ψ) h.2)] end | s (neg (box φ)) h := begin dsimp at h, rw classical.not_forall at h, cases h with w hw, rw classical.not_imp at hw, simp, split, split, {exact hw.1}, {apply trans_left, exact hw.2} end | s (neg (dia φ)) h := begin dsimp at h, simp, intros s' hs', rw not_exists at h, have := h s', rw not_and at this, have hnf := this hs', apply trans_left, exact hnf end | s (and φ ψ) h := by dsimp at h; simp [and.intro (trans_left _ _ h.1) (trans_left _ _ h.2)] | s (or φ ψ) h := begin dsimp at h, cases h, {exact or.inl (trans_left _ _ h)}, {exact or.inr (trans_left _ _ h)} end | s (impl φ ψ) h := begin dsimp at h, simp, cases h, {left, apply trans_left, exact h}, {right, apply trans_left, exact h} end | s (box φ) h := begin dsimp at h, simp, intros s' hs', exact trans_left _ _ (h s' hs') end | s (dia φ) h := begin dsimp at h, simp, cases h with w hw, split, split, {exact hw.1}, {exact trans_left _ _ hw.2} end using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (trans_size_left k)⟩]} /- This direction is still constructive -/ theorem trans_right {st} (k : kripke st) : Π s φ, force k s (fml.to_nnf φ) → fml_force k s φ | s (var n) h := h | s (neg (var n)) h := h | s (neg (neg φ)) h := by dsimp; simp at h; exact not_not_intro (trans_right _ _ h) | s (neg (and φ ψ)) h := begin dsimp, simp at h, apply not_and_of_not_or_not, cases h, {left, exact trans_right _ _ h}, {right, exact trans_right _ _ h} end | s (neg (or φ ψ)) h := begin dsimp, simp at h, apply not_or, {exact trans_right _ _ h.1}, {exact trans_right _ _ h.2} end | s (neg (impl φ ψ)) h := begin dsimp, simp at h, apply not_or, {exact not_not_intro (trans_right _ _ h.1)}, {exact trans_right _ _ h.2} end | s (neg (box φ)) h := begin dsimp, simp at h, apply not_forall_of_exists_not, cases h with w hw, split, {apply not_imp_of_and_not, split, exact hw.1, exact trans_right _ _ hw.2} end | s (neg (dia φ)) h := begin dsimp, simp at h, rw not_exists, intros s' hs', have := h s' hs'.1, have hn := trans_right _ _ this, have := hs'.2, contradiction end | s (and φ ψ) h := begin dsimp, simp at h, split, {exact trans_right _ _ h.1}, {exact trans_right _ _ h.2} end | s (or φ ψ) h := begin dsimp, simp at h, cases h, {left, exact trans_right _ _ h}, {right, exact trans_right _ _ h} end | s (impl φ ψ) h := begin dsimp, simp at h, cases h, {left, exact trans_right _ _ h}, {right, exact trans_right _ _ h} end | s (box φ) h := begin dsimp, simp at h, intros s' hs', exact trans_right _ _ (h s' hs') end | s (dia φ) h := begin dsimp, simp at h, cases h with w hw, split, split, {exact hw.1}, {exact trans_right _ _ hw.2} end using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (trans_size_right k)⟩]} /- This is a classical result -/ theorem trans_iff {st} (k : kripke st) (s φ) : fml_force k s φ ↔ force k s (fml.to_nnf φ) := ⟨trans_left k s φ, trans_right k s φ⟩ def fml_sat {st} (k : kripke st) (s) (Γ : list fml) : Prop := ∀ φ ∈ Γ, fml_force k s φ theorem fml_sat_of_empty {st} (k : kripke st) (s) : fml_sat k s [] := λ φ h, absurd h $ list.not_mem_nil _ def fml_unsatisfiable (Γ : list fml) : Prop := ∀ (st) (k : kripke st) s, ¬ fml_sat k s Γ theorem trans_sat_iff {st} (k : kripke st) (s) : Π Γ, fml_sat k s Γ ↔ sat k s (list.map fml.to_nnf Γ) | [] := by simp [sat_of_empty, fml_sat_of_empty] | (hd::tl) := begin split, { intro h, dsimp, intros φ hφ, cases hφ, { rw hφ, rw ←trans_iff, apply h, simp }, { have : fml_sat k s tl, { intros ψ hψ, apply h, simp [hψ] }, rw trans_sat_iff at this, exact this _ hφ } }, { intros h φ hφ, cases hφ, { rw hφ, rw trans_iff, apply h, simp }, { have : sat k s (list.map fml.to_nnf tl), { intros ψ hψ, apply h, simp [hψ] }, rw ←trans_sat_iff at this, exact this _ hφ } } end theorem trans_unsat_iff (Γ : list fml) : fml_unsatisfiable Γ ↔ unsatisfiable (list.map fml.to_nnf Γ) := begin split, {intros h _ _ _ _, apply h, rw trans_sat_iff, assumption}, {intros h _ _ _ _, apply h, rw ←trans_sat_iff, assumption} end
55559ebeb808460a8c43d83f73264fecde0375be
95dcf8dea2baf2b4b0a60d438f27c35ae3dd3990
/src/topology/algebra/infinite_sum.lean
6f0546d7955512e1236947839f448499847fd265
[ "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
24,249
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 Infinite sum over a topological monoid This sum is known as unconditionally convergent, as it sums to the same value under all possible permutations. For Euclidean spaces (finite dimensional Banach spaces) this is equivalent to absolute convergence. Note: There are summable sequences which are not unconditionally convergent! The other way holds generally, see `tendsto_sum_nat_of_is_sum`. Reference: * Bourbaki: General Topology (1995), Chapter 3 §5 (Infinite sums in commutative groups) -/ import logic.function algebra.big_operators data.set data.finset topology.metric_space.basic topology.algebra.uniform_group topology.algebra.ring topology.algebra.ordered topology.instances.real noncomputable theory open lattice finset filter function classical local attribute [instance] prop_decidable def option.cases_on' {α β} : option α → β → (α → β) → β | none n s := n | (some a) n s := s a variables {α : Type*} {β : Type*} {γ : Type*} section is_sum variables [add_comm_monoid α] [topological_space α] [topological_add_monoid α] /-- Infinite sum on a topological monoid The `at_top` filter on `finset α` is the limit of all finite sets towards the entire type. So we sum up bigger and bigger sets. This sum operation is still invariant under reordering, and a absolute sum operator. This is based on Mario Carneiro's infinite sum in Metamath. -/ def is_sum (f : β → α) (a : α) : Prop := tendsto (λs:finset β, s.sum f) at_top (nhds a) /-- `has_sum f` means that `f` has some (infinite) sum. Use `tsum` to get the value. -/ def has_sum (f : β → α) : Prop := ∃a, is_sum f a /-- `tsum f` is the sum of `f` it exists, or 0 otherwise -/ def tsum (f : β → α) := if h : has_sum f then classical.some h else 0 notation `∑` binders `, ` r:(scoped f, tsum f) := r variables {f g : β → α} {a b : α} {s : finset β} lemma is_sum_tsum (ha : has_sum f) : is_sum f (∑b, f b) := by simp [ha, tsum]; exact some_spec ha lemma has_sum_spec (ha : is_sum f a) : has_sum f := ⟨a, ha⟩ lemma is_sum_zero : is_sum (λb, 0 : β → α) 0 := by simp [is_sum, tendsto_const_nhds] lemma has_sum_zero : has_sum (λb, 0 : β → α) := has_sum_spec is_sum_zero lemma is_sum_add (hf : is_sum f a) (hg : is_sum g b) : is_sum (λb, f b + g b) (a + b) := by simp [is_sum, sum_add_distrib]; exact tendsto_add hf hg lemma has_sum_add (hf : has_sum f) (hg : has_sum g) : has_sum (λb, f b + g b) := has_sum_spec $ is_sum_add (is_sum_tsum hf)(is_sum_tsum hg) lemma is_sum_sum {f : γ → β → α} {a : γ → α} {s : finset γ} : (∀i∈s, is_sum (f i) (a i)) → is_sum (λb, s.sum $ λi, f i b) (s.sum a) := finset.induction_on s (by simp [is_sum_zero]) (by simp [is_sum_add] {contextual := tt}) lemma has_sum_sum {f : γ → β → α} {s : finset γ} (hf : ∀i∈s, has_sum (f i)) : has_sum (λb, s.sum $ λi, f i b) := has_sum_spec $ is_sum_sum $ assume i hi, is_sum_tsum $ hf i hi lemma is_sum_sum_of_ne_finset_zero (hf : ∀b∉s, f b = 0) : is_sum f (s.sum f) := tendsto_infi' s $ tendsto.congr' (assume t (ht : s ⊆ t), show s.sum f = t.sum f, from sum_subset ht $ assume x _, hf _) tendsto_const_nhds lemma has_sum_sum_of_ne_finset_zero (hf : ∀b∉s, f b = 0) : has_sum f := has_sum_spec $ is_sum_sum_of_ne_finset_zero hf lemma is_sum_ite_eq (b : β) (a : α) : is_sum (λb', if b' = b then a else 0) a := suffices is_sum (λb', if b' = b then a else 0) (({b} : finset β).sum (λb', if b' = b then a else 0)), from by simpa, is_sum_sum_of_ne_finset_zero $ assume b' hb, have b' ≠ b, by simpa using hb, by rw [if_neg this] lemma is_sum_of_iso {j : γ → β} {i : β → γ} (hf : is_sum f a) (h₁ : ∀x, i (j x) = x) (h₂ : ∀x, j (i x) = x) : is_sum (f ∘ j) a := have ∀x y, j x = j y → x = y, from assume x y h, have i (j x) = i (j y), by rw [h], by rwa [h₁, h₁] at this, have (λs:finset γ, s.sum (f ∘ j)) = (λs:finset β, s.sum f) ∘ (λs:finset γ, s.image j), from funext $ assume s, (sum_image $ assume x _ y _, this x y).symm, show tendsto (λs:finset γ, s.sum (f ∘ j)) at_top (nhds a), by rw [this]; apply (tendsto_finset_image_at_top_at_top h₂).comp hf lemma is_sum_iff_is_sum_of_iso {j : γ → β} (i : β → γ) (h₁ : ∀x, i (j x) = x) (h₂ : ∀x, j (i x) = x) : is_sum (f ∘ j) a ↔ is_sum f a := iff.intro (assume hfj, have is_sum ((f ∘ j) ∘ i) a, from is_sum_of_iso hfj h₂ h₁, by simp [(∘), h₂] at this; assumption) (assume hf, is_sum_of_iso hf h₁ h₂) lemma is_sum_hom (g : α → γ) [add_comm_monoid γ] [topological_space γ] [topological_add_monoid γ] [is_add_monoid_hom g] (h₃ : continuous g) (hf : is_sum f a) : is_sum (g ∘ f) (g a) := have (λs:finset β, s.sum (g ∘ f)) = g ∘ (λs:finset β, s.sum f), from funext $ assume s, sum_hom g, show tendsto (λs:finset β, s.sum (g ∘ f)) at_top (nhds (g a)), by rw [this]; exact hf.comp (continuous_iff_continuous_at.mp h₃ a) lemma tendsto_sum_nat_of_is_sum {f : ℕ → α} (h : is_sum f a) : tendsto (λn:ℕ, (range n).sum f) at_top (nhds a) := suffices map (λ (n : ℕ), sum (range n) f) at_top ≤ map (λ (s : finset ℕ), sum s f) at_top, from le_trans this h, assume s (hs : {t : finset ℕ | t.sum f ∈ s} ∈ at_top), let ⟨t, ht⟩ := mem_at_top_sets.mp hs, ⟨n, hn⟩ := @exists_nat_subset_range t in mem_at_top_sets.mpr ⟨n, assume n' hn', ht _ $ finset.subset.trans hn $ range_subset.mpr hn'⟩ lemma is_sum_sigma [regular_space α] {γ : β → Type*} {f : (Σ b:β, γ b) → α} {g : β → α} {a : α} (hf : ∀b, is_sum (λc, f ⟨b, c⟩) (g b)) (ha : is_sum f a) : is_sum g a := assume s' hs', let ⟨s, hs, hss', hsc⟩ := nhds_is_closed hs', ⟨u, hu⟩ := mem_at_top_sets.mp $ ha $ hs, fsts := u.image sigma.fst, snds := λb, u.bind (λp, (if h : p.1 = b then {cast (congr_arg γ h) p.2} else ∅ : finset (γ b))) in have u_subset : u ⊆ fsts.sigma snds, from subset_iff.mpr $ assume ⟨b, c⟩ hu, have hb : b ∈ fsts, from finset.mem_image.mpr ⟨_, hu, rfl⟩, have hc : c ∈ snds b, from mem_bind.mpr ⟨_, hu, by simp; refl⟩, by simp [mem_sigma, hb, hc] , mem_at_top_sets.mpr $ exists.intro fsts $ assume bs (hbs : fsts ⊆ bs), have h : ∀cs : Π b ∈ bs, finset (γ b), (⋂b (hb : b ∈ bs), (λp:Πb, finset (γ b), p b) ⁻¹' {cs' | cs b hb ⊆ cs' }) ∩ (λp, bs.sum (λb, (p b).sum (λc, f ⟨b, c⟩))) ⁻¹' s ≠ ∅, from assume cs, let cs' := λb, (if h : b ∈ bs then cs b h else ∅) ∪ snds b in have sum_eq : bs.sum (λb, (cs' b).sum (λc, f ⟨b, c⟩)) = (bs.sigma cs').sum f, from sum_sigma.symm, have (bs.sigma cs').sum f ∈ s, from hu _ $ finset.subset.trans u_subset $ sigma_mono hbs $ assume b, @finset.subset_union_right (γ b) _ _ _, set.ne_empty_iff_exists_mem.mpr $ exists.intro cs' $ by simp [sum_eq, this]; { intros b hb, simp [cs', hb, finset.subset_union_right] }, have tendsto (λp:(Πb:β, finset (γ b)), bs.sum (λb, (p b).sum (λc, f ⟨b, c⟩))) (⨅b (h : b ∈ bs), at_top.comap (λp, p b)) (nhds (bs.sum g)), from tendsto_finset_sum bs $ assume c hc, tendsto_infi' c $ tendsto_infi' hc $ tendsto_comap.comp (hf c), have bs.sum g ∈ s, from mem_of_closed_of_tendsto' this hsc $ forall_sets_neq_empty_iff_neq_bot.mp $ by simp [mem_inf_sets, exists_imp_distrib, and_imp, forall_and_distrib, filter.mem_infi_sets_finset, mem_comap_sets, skolem, mem_at_top_sets, and_comm]; from assume s₁ s₂ s₃ hs₁ hs₃ p hs₂ p' hp cs hp', have (⋂b (h : b ∈ bs), (λp:(Πb, finset (γ b)), p b) ⁻¹' {cs' | cs b h ⊆ cs' }) ≤ (⨅b∈bs, p b), from infi_le_infi $ assume b, infi_le_infi $ assume hb, le_trans (set.preimage_mono $ hp' b hb) (hp b hb), neq_bot_of_le_neq_bot (h _) (le_trans (set.inter_subset_inter (le_trans this hs₂) hs₃) hs₁), hss' this lemma has_sum_sigma [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α} (hf : ∀b, has_sum (λc, f ⟨b, c⟩)) (ha : has_sum f) : has_sum (λb, ∑c, f ⟨b, c⟩):= has_sum_spec $ is_sum_sigma (assume b, is_sum_tsum $ hf b) (is_sum_tsum ha) end is_sum section is_sum_iff_is_sum_of_iso_ne_zero variables [add_comm_monoid α] [topological_space α] [topological_add_monoid α] variables {f : β → α} {g : γ → α} {a : α} lemma is_sum_of_is_sum (h_eq : ∀u:finset γ, ∃v:finset β, ∀v', v ⊆ v' → ∃u', u ⊆ u' ∧ u'.sum g = v'.sum f) (hf : is_sum g a) : is_sum f a := suffices at_top.map (λs:finset β, s.sum f) ≤ at_top.map (λs:finset γ, s.sum g), from le_trans this hf, by rw [map_at_top_eq, map_at_top_eq]; from (le_infi $ assume b, let ⟨v, hv⟩ := h_eq b in infi_le_of_le v $ by simp [set.image_subset_iff]; exact hv) lemma is_sum_iff_is_sum (h₁ : ∀u:finset γ, ∃v:finset β, ∀v', v ⊆ v' → ∃u', u ⊆ u' ∧ u'.sum g = v'.sum f) (h₂ : ∀v:finset β, ∃u:finset γ, ∀u', u ⊆ u' → ∃v', v ⊆ v' ∧ v'.sum f = u'.sum g) : is_sum f a ↔ is_sum g a := ⟨is_sum_of_is_sum h₂, is_sum_of_is_sum h₁⟩ variables (i : Π⦃c⦄, g c ≠ 0 → β) (hi : ∀⦃c⦄ (h : g c ≠ 0), f (i h) ≠ 0) (j : Π⦃b⦄, f b ≠ 0 → γ) (hj : ∀⦃b⦄ (h : f b ≠ 0), g (j h) ≠ 0) (hji : ∀⦃c⦄ (h : g c ≠ 0), j (hi h) = c) (hij : ∀⦃b⦄ (h : f b ≠ 0), i (hj h) = b) (hgj : ∀⦃b⦄ (h : f b ≠ 0), g (j h) = f b) include hi hj hji hij hgj lemma is_sum_of_is_sum_ne_zero : is_sum g a → is_sum f a := have j_inj : ∀x y (hx : f x ≠ 0) (hy : f y ≠ 0), (j hx = j hy ↔ x = y), from assume x y hx hy, ⟨assume h, have i (hj hx) = i (hj hy), by simp [h], by rwa [hij, hij] at this; assumption, by simp {contextual := tt}⟩, let ii : finset γ → finset β := λu, u.bind $ λc, if h : g c = 0 then ∅ else {i h} in let jj : finset β → finset γ := λv, v.bind $ λb, if h : f b = 0 then ∅ else {j h} in is_sum_of_is_sum $ assume u, exists.intro (ii u) $ assume v hv, exists.intro (u ∪ jj v) $ and.intro (subset_union_left _ _) $ have ∀c:γ, c ∈ u ∪ jj v → c ∉ jj v → g c = 0, from assume c hc hnc, classical.by_contradiction $ assume h : g c ≠ 0, have c ∈ u, from (finset.mem_union.1 hc).resolve_right hnc, have i h ∈ v, from hv $ by simp [mem_bind]; existsi c; simp [h, this], have j (hi h) ∈ jj v, by simp [mem_bind]; existsi i h; simp [h, hi, this], by rw [hji h] at this; exact hnc this, calc (u ∪ jj v).sum g = (jj v).sum g : (sum_subset (subset_union_right _ _) this).symm ... = v.sum _ : sum_bind $ by intros x hx y hy hxy; by_cases f x = 0; by_cases f y = 0; simp [*] ... = v.sum f : sum_congr rfl $ by intros x hx; by_cases f x = 0; simp [*] lemma is_sum_iff_is_sum_of_ne_zero : is_sum f a ↔ is_sum g a := iff.intro (is_sum_of_is_sum_ne_zero j hj i hi hij hji $ assume b hb, by rw [←hgj (hi _), hji]) (is_sum_of_is_sum_ne_zero i hi j hj hji hij hgj) lemma has_sum_iff_has_sum_ne_zero : has_sum g ↔ has_sum f := exists_congr $ assume a, is_sum_iff_is_sum_of_ne_zero j hj i hi hij hji $ assume b hb, by rw [←hgj (hi _), hji] end is_sum_iff_is_sum_of_iso_ne_zero section is_sum_iff_is_sum_of_bij_ne_zero variables [add_comm_monoid α] [topological_space α] [topological_add_monoid α] variables {f : β → α} {g : γ → α} {a : α} (i : Π⦃c⦄, g c ≠ 0 → β) (h₁ : ∀⦃c₁ c₂⦄ (h₁ : g c₁ ≠ 0) (h₂ : g c₂ ≠ 0), i h₁ = i h₂ → c₁ = c₂) (h₂ : ∀⦃b⦄, f b ≠ 0 → ∃c (h : g c ≠ 0), i h = b) (h₃ : ∀⦃c⦄ (h : g c ≠ 0), f (i h) = g c) include i h₁ h₂ h₃ lemma is_sum_iff_is_sum_of_ne_zero_bij : is_sum f a ↔ is_sum g a := have hi : ∀⦃c⦄ (h : g c ≠ 0), f (i h) ≠ 0, from assume c h, by simp [h₃, h], let j : Π⦃b⦄, f b ≠ 0 → γ := λb h, some $ h₂ h in have hj : ∀⦃b⦄ (h : f b ≠ 0), ∃(h : g (j h) ≠ 0), i h = b, from assume b h, some_spec $ h₂ h, have hj₁ : ∀⦃b⦄ (h : f b ≠ 0), g (j h) ≠ 0, from assume b h, let ⟨h₁, _⟩ := hj h in h₁, have hj₂ : ∀⦃b⦄ (h : f b ≠ 0), i (hj₁ h) = b, from assume b h, let ⟨h₁, h₂⟩ := hj h in h₂, is_sum_iff_is_sum_of_ne_zero i hi j hj₁ (assume c h, h₁ (hj₁ _) h $ hj₂ _) hj₂ (assume b h, by rw [←h₃ (hj₁ _), hj₂]) lemma has_sum_iff_has_sum_ne_zero_bij : has_sum f ↔ has_sum g := exists_congr $ assume a, is_sum_iff_is_sum_of_ne_zero_bij @i h₁ h₂ h₃ end is_sum_iff_is_sum_of_bij_ne_zero section tsum variables [add_comm_monoid α] [topological_space α] [topological_add_monoid α] [t2_space α] variables {f g : β → α} {a a₁ a₂ : α} lemma is_sum_unique : is_sum f a₁ → is_sum f a₂ → a₁ = a₂ := tendsto_nhds_unique at_top_ne_bot lemma tsum_eq_is_sum (ha : is_sum f a) : (∑b, f b) = a := is_sum_unique (is_sum_tsum ⟨a, ha⟩) ha lemma is_sum_iff_of_has_sum (h : has_sum f) : is_sum f a ↔ (∑b, f b) = a := iff.intro tsum_eq_is_sum (assume eq, eq ▸ is_sum_tsum h) @[simp] lemma tsum_zero : (∑b:β, 0:α) = 0 := tsum_eq_is_sum is_sum_zero lemma tsum_add (hf : has_sum f) (hg : has_sum g) : (∑b, f b + g b) = (∑b, f b) + (∑b, g b) := tsum_eq_is_sum $ is_sum_add (is_sum_tsum hf) (is_sum_tsum hg) lemma tsum_sum {f : γ → β → α} {s : finset γ} (hf : ∀i∈s, has_sum (f i)) : (∑b, s.sum (λi, f i b)) = s.sum (λi, ∑b, f i b) := tsum_eq_is_sum $ is_sum_sum $ assume i hi, is_sum_tsum $ hf i hi lemma tsum_eq_sum {f : β → α} {s : finset β} (hf : ∀b∉s, f b = 0) : (∑b, f b) = s.sum f := tsum_eq_is_sum $ is_sum_sum_of_ne_finset_zero hf lemma tsum_fintype [fintype β] (f : β → α) : (∑b, f b) = finset.univ.sum f := tsum_eq_sum $ λ a h, h.elim (mem_univ _) lemma tsum_eq_single {f : β → α} (b : β) (hf : ∀b' ≠ b, f b' = 0) : (∑b, f b) = f b := calc (∑b, f b) = (finset.singleton b).sum f : tsum_eq_sum $ by simp [hf] {contextual := tt} ... = f b : by simp lemma tsum_sigma [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α} (h₁ : ∀b, has_sum (λc, f ⟨b, c⟩)) (h₂ : has_sum f) : (∑p, f p) = (∑b c, f ⟨b, c⟩):= (tsum_eq_is_sum $ is_sum_sigma (assume b, is_sum_tsum $ h₁ b) $ is_sum_tsum h₂).symm @[simp] lemma tsum_ite_eq (b : β) (a : α) : (∑b', if b' = b then a else 0) = a := tsum_eq_is_sum (is_sum_ite_eq b a) lemma tsum_eq_tsum_of_is_sum_iff_is_sum {f : β → α} {g : γ → α} (h : ∀{a}, is_sum f a ↔ is_sum g a) : (∑b, f b) = (∑c, g c) := by_cases (assume : ∃a, is_sum f a, let ⟨a, hfa⟩ := this in have hga : is_sum g a, from h.mp hfa, by rw [tsum_eq_is_sum hfa, tsum_eq_is_sum hga]) (assume hf : ¬ has_sum f, have hg : ¬ has_sum g, from assume ⟨a, hga⟩, hf ⟨a, h.mpr hga⟩, by simp [tsum, hf, hg]) lemma tsum_eq_tsum_of_ne_zero {f : β → α} {g : γ → α} (i : Π⦃c⦄, g c ≠ 0 → β) (hi : ∀⦃c⦄ (h : g c ≠ 0), f (i h) ≠ 0) (j : Π⦃b⦄, f b ≠ 0 → γ) (hj : ∀⦃b⦄ (h : f b ≠ 0), g (j h) ≠ 0) (hji : ∀⦃c⦄ (h : g c ≠ 0), j (hi h) = c) (hij : ∀⦃b⦄ (h : f b ≠ 0), i (hj h) = b) (hgj : ∀⦃b⦄ (h : f b ≠ 0), g (j h) = f b) : (∑i, f i) = (∑j, g j) := tsum_eq_tsum_of_is_sum_iff_is_sum $ assume a, is_sum_iff_is_sum_of_ne_zero i hi j hj hji hij hgj lemma tsum_eq_tsum_of_ne_zero_bij {f : β → α} {g : γ → α} (i : Π⦃c⦄, g c ≠ 0 → β) (h₁ : ∀⦃c₁ c₂⦄ (h₁ : g c₁ ≠ 0) (h₂ : g c₂ ≠ 0), i h₁ = i h₂ → c₁ = c₂) (h₂ : ∀⦃b⦄, f b ≠ 0 → ∃c (h : g c ≠ 0), i h = b) (h₃ : ∀⦃c⦄ (h : g c ≠ 0), f (i h) = g c) : (∑i, f i) = (∑j, g j) := tsum_eq_tsum_of_is_sum_iff_is_sum $ assume a, is_sum_iff_is_sum_of_ne_zero_bij i h₁ h₂ h₃ lemma tsum_eq_tsum_of_iso (j : γ → β) (i : β → γ) (h₁ : ∀x, i (j x) = x) (h₂ : ∀x, j (i x) = x) : (∑c, f (j c)) = (∑b, f b) := tsum_eq_tsum_of_is_sum_iff_is_sum $ assume a, is_sum_iff_is_sum_of_iso i h₁ h₂ lemma tsum_equiv (j : γ ≃ β) : (∑c, f (j c)) = (∑b, f b) := tsum_eq_tsum_of_iso j j.symm (by simp) (by simp) end tsum section topological_group variables [add_comm_group α] [topological_space α] [topological_add_group α] variables {f g : β → α} {a a₁ a₂ : α} lemma is_sum_neg : is_sum f a → is_sum (λb, - f b) (- a) := is_sum_hom has_neg.neg continuous_neg' lemma has_sum_neg (hf : has_sum f) : has_sum (λb, - f b) := has_sum_spec $ is_sum_neg $ is_sum_tsum $ hf lemma is_sum_sub (hf : is_sum f a₁) (hg : is_sum g a₂) : is_sum (λb, f b - g b) (a₁ - a₂) := by simp; exact is_sum_add hf (is_sum_neg hg) lemma has_sum_sub (hf : has_sum f) (hg : has_sum g) : has_sum (λb, f b - g b) := has_sum_spec $ is_sum_sub (is_sum_tsum hf) (is_sum_tsum hg) section tsum variables [t2_space α] lemma tsum_neg (hf : has_sum f) : (∑b, - f b) = - (∑b, f b) := tsum_eq_is_sum $ is_sum_neg $ is_sum_tsum $ hf lemma tsum_sub (hf : has_sum f) (hg : has_sum g) : (∑b, f b - g b) = (∑b, f b) - (∑b, g b) := tsum_eq_is_sum $ is_sum_sub (is_sum_tsum hf) (is_sum_tsum hg) end tsum end topological_group section topological_semiring variables [semiring α] [topological_space α] [topological_semiring α] variables {f g : β → α} {a a₁ a₂ : α} lemma is_sum_mul_left (a₂) : is_sum f a₁ → is_sum (λb, a₂ * f b) (a₂ * a₁) := is_sum_hom _ (continuous_mul continuous_const continuous_id) lemma is_sum_mul_right (a₂) (hf : is_sum f a₁) : is_sum (λb, f b * a₂) (a₁ * a₂) := @is_sum_hom _ _ _ _ _ _ f a₁ (λa, a * a₂) _ _ _ _ (continuous_mul continuous_id continuous_const) hf lemma has_sum_mul_left (a) (hf : has_sum f) : has_sum (λb, a * f b) := has_sum_spec $ is_sum_mul_left _ $ is_sum_tsum hf lemma has_sum_mul_right (a) (hf : has_sum f) : has_sum (λb, f b * a) := has_sum_spec $ is_sum_mul_right _ $ is_sum_tsum hf section tsum variables [t2_space α] lemma tsum_mul_left (a) (hf : has_sum f) : (∑b, a * f b) = a * (∑b, f b) := tsum_eq_is_sum $ is_sum_mul_left _ $ is_sum_tsum hf lemma tsum_mul_right (a) (hf : has_sum f) : (∑b, f b * a) = (∑b, f b) * a := tsum_eq_is_sum $ is_sum_mul_right _ $ is_sum_tsum hf end tsum end topological_semiring section order_topology variables [ordered_comm_monoid α] [topological_space α] [ordered_topology α] [topological_add_monoid α] variables {f g : β → α} {a a₁ a₂ : α} lemma is_sum_le (h : ∀b, f b ≤ g b) (hf : is_sum f a₁) (hg : is_sum g a₂) : a₁ ≤ a₂ := le_of_tendsto_of_tendsto at_top_ne_bot hf hg $ univ_mem_sets' $ assume s, sum_le_sum' $ assume b _, h b lemma is_sum_le_inj {g : γ → α} (i : β → γ) (hi : injective i) (hs : ∀c∉set.range i, 0 ≤ g c) (h : ∀b, f b ≤ g (i b)) (hf : is_sum f a₁) (hg : is_sum g a₂) : a₁ ≤ a₂ := have is_sum (λc, (partial_inv i c).cases_on' 0 f) a₁, begin refine (is_sum_iff_is_sum_of_ne_zero_bij (λb _, i b) _ _ _).2 hf, { assume c₁ c₂ h₁ h₂ eq, exact hi eq }, { assume c hc, cases eq : partial_inv i c with b; rw eq at hc, { contradiction }, { rw [partial_inv_of_injective hi] at eq, exact ⟨b, hc, eq⟩ } }, { assume c hc, rw [partial_inv_left hi, option.cases_on'] } end, begin refine is_sum_le (assume c, _) this hg, by_cases c ∈ set.range i, { rcases h with ⟨b, rfl⟩, rw [partial_inv_left hi, option.cases_on'], exact h _ }, { have : partial_inv i c = none := dif_neg h, rw [this, option.cases_on'], exact hs _ h } end lemma tsum_le_tsum (h : ∀b, f b ≤ g b) (hf : has_sum f) (hg : has_sum g) : (∑b, f b) ≤ (∑b, g b) := is_sum_le h (is_sum_tsum hf) (is_sum_tsum hg) end order_topology section uniform_group variables [add_comm_group α] [uniform_space α] [complete_space α] [uniform_add_group α] variables (f g : β → α) {a a₁ a₂ : α} lemma has_sum_iff_cauchy : has_sum f ↔ cauchy (map (λ (s : finset β), sum s f) at_top) := (cauchy_map_iff_exists_tendsto at_top_ne_bot).symm lemma has_sum_iff_vanishing : has_sum f ↔ ∀ e ∈ nhds (0:α), (∃s:finset β, ∀t, disjoint t s → t.sum f ∈ e) := begin simp only [has_sum_iff_cauchy, cauchy_map_iff, and_iff_right at_top_ne_bot, prod_at_top_at_top_eq, uniformity_eq_comap_nhds_zero α, tendsto_comap_iff, (∘)], rw [tendsto_at_top' (_ : finset β × finset β → α)], split, { assume h e he, rcases h e he with ⟨⟨s₁, s₂⟩, h⟩, use [s₁ ∪ s₂], assume t ht, have : (s₁ ∪ s₂) ∩ t = ∅ := finset.disjoint_iff_inter_eq_empty.1 ht.symm, specialize h (s₁ ∪ s₂, (s₁ ∪ s₂) ∪ t) ⟨le_sup_left, le_sup_left_of_le le_sup_right⟩, simpa only [finset.sum_union this, add_sub_cancel'] using h }, { assume h e he, rcases exists_nhds_half_neg he with ⟨d, hd, hde⟩, rcases h d hd with ⟨s, h⟩, use [(s, s)], rintros ⟨t₁, t₂⟩ ⟨ht₁, ht₂⟩, have : t₂.sum f - t₁.sum f = (t₂ \ s).sum f - (t₁ \ s).sum f, { simp only [(finset.sum_sdiff ht₁).symm, (finset.sum_sdiff ht₂).symm, add_sub_add_right_eq_sub] }, simp only [this], exact hde _ _ (h _ finset.sdiff_disjoint) (h _ finset.sdiff_disjoint) } end /- TODO: generalize to monoid with a uniform continuous subtraction operator: `(a + b) - b = a` -/ lemma has_sum_of_has_sum_of_sub (hf : has_sum f) (h : ∀b, g b = 0 ∨ g b = f b) : has_sum g := (has_sum_iff_vanishing g).2 $ assume e he, let ⟨s, hs⟩ := (has_sum_iff_vanishing f).1 hf e he in ⟨s, assume t ht, have eq : (t.filter (λb, g b = f b)).sum f = t.sum g := calc (t.filter (λb, g b = f b)).sum f = (t.filter (λb, g b = f b)).sum g : finset.sum_congr rfl (assume b hb, (finset.mem_filter.1 hb).2.symm) ... = t.sum g : begin refine finset.sum_subset (finset.filter_subset _) _, assume b hbt hb, simp only [(∉), finset.mem_filter, and_iff_right hbt] at hb, exact (h b).resolve_right hb end, eq ▸ hs _ $ finset.disjoint_of_subset_left (finset.filter_subset _) ht⟩ lemma has_sum_comp_of_has_sum_of_injective {i : γ → β} (hf : has_sum f) (hi : injective i) : has_sum (f ∘ i) := suffices has_sum (λb, if b ∈ set.range i then f b else 0), begin refine (has_sum_iff_has_sum_ne_zero_bij (λc _, i c) _ _ _).1 this, { assume c₁ c₂ hc₁ hc₂ eq, exact hi eq }, { assume b hb, split_ifs at hb, { rcases h with ⟨c, rfl⟩, exact ⟨c, hb, rfl⟩ }, { contradiction } }, { assume c hc, exact if_pos (set.mem_range_self _) } end, has_sum_of_has_sum_of_sub _ _ hf $ assume b, by by_cases b ∈ set.range i; simp [h] end uniform_group section cauchy_seq open finset.Ico filter lemma cauchy_seq_of_has_sum_dist [metric_space α] {f : ℕ → α} (h : has_sum (λn, dist (f n) (f n.succ))) : cauchy_seq f := begin let d := λn, dist (f n) (f (n+1)), refine metric.cauchy_seq_iff'.2 (λε εpos, _), rcases (has_sum_iff_vanishing _).1 h {x : ℝ | x < ε} (gt_mem_nhds εpos) with ⟨s, hs⟩, have : ∃N:ℕ, ∀x ∈ s, x < N, { by_cases h : s = ∅, { use 0, simp [h]}, { use s.max' h + 1, exact λx hx, lt_of_le_of_lt (s.le_max' h x hx) (nat.lt_succ_self _) }}, rcases this with ⟨N, hN⟩, refine ⟨N, λn hn, _⟩, have : ∀n, n ≥ N → dist (f N) (f n) ≤ (Ico N n).sum d, { apply nat.le_induction, { simp }, { assume n hn hrec, calc dist (f N) (f (n+1)) ≤ dist (f N) (f n) + d n : dist_triangle _ _ _ ... ≤ (Ico N n).sum d + d n : add_le_add hrec (le_refl _) ... = (Ico N (n+1)).sum d : by rw [succ_top hn, sum_insert, add_comm]; simp }}, calc dist (f n) (f N) ≤ (Ico N n).sum d : by rw dist_comm; apply this n hn ... < ε : hs _ (finset.disjoint_iff_ne.2 (λa ha b hb, ne_of_gt (lt_of_lt_of_le (hN _ hb) (mem.1 ha).1))) end end cauchy_seq
2d20abeee21711609951793c64b3fd1147e89220
491068d2ad28831e7dade8d6dff871c3e49d9431
/hott/algebra/e_closure.hlean
9bcda26ef19b0764fbc252eedbd1a602d72e5ac6
[ "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
6,202
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 The "equivalence closure" of a type-valued relation. Given a binary type-valued relation (fibration), we add reflexivity, symmetry and transitivity terms -/ import .relation eq2 arity open eq inductive e_closure {A : Type} (R : A → A → Type) : A → A → Type := | of_rel : Π{a a'} (r : R a a'), e_closure R a a' | refl : Πa, e_closure R a a | symm : Π{a a'} (r : e_closure R a a'), e_closure R a' a | trans : Π{a a' a''} (r : e_closure R a a') (r' : e_closure R a' a''), e_closure R a a'' namespace e_closure infix ` ⬝r `:75 := e_closure.trans postfix `⁻¹ʳ`:(max+10) := e_closure.symm notation `[`:max a `]`:0 := e_closure.of_rel a abbreviation rfl {A : Type} {R : A → A → Type} {a : A} := refl R a end e_closure namespace relation section parameters {A : Type} (R : A → A → Type) local abbreviation T := e_closure R variables ⦃a a' a'' : A⦄ {s : R a a'} {r : T a a} {B C : Type} parameter {R} protected definition e_closure.elim [unfold 8] {f : A → B} (e : Π⦃a a' : A⦄, R a a' → f a = f a') (t : T a a') : f a = f a' := begin induction t, exact e r, reflexivity, exact v_0⁻¹, exact v_0 ⬝ v_1 end definition ap_e_closure_elim_h [unfold 12] {B C : Type} {f : A → B} {g : B → C} (e : Π⦃a a' : A⦄, R a a' → f a = f a') {e' : Π⦃a a' : A⦄, R a a' → g (f a) = g (f a')} (p : Π⦃a a' : A⦄ (s : R a a'), ap g (e s) = e' s) (t : T a a') : ap g (e_closure.elim e t) = e_closure.elim e' t := begin induction t, apply p, reflexivity, exact ap_inv g (e_closure.elim e r) ⬝ inverse2 v_0, exact ap_con g (e_closure.elim e r) (e_closure.elim e r') ⬝ (v_0 ◾ v_1) end definition ap_e_closure_elim {B C : Type} {f : A → B} (g : B → C) (e : Π⦃a a' : A⦄, R a a' → f a = f a') (t : T a a') : ap g (e_closure.elim e t) = e_closure.elim (λa a' r, ap g (e r)) t := ap_e_closure_elim_h e (λa a' s, idp) t definition ap_e_closure_elim_h_eq {B C : Type} {f : A → B} {g : B → C} (e : Π⦃a a' : A⦄, R a a' → f a = f a') {e' : Π⦃a a' : A⦄, R a a' → g (f a) = g (f a')} (p : Π⦃a a' : A⦄ (s : R a a'), ap g (e s) = e' s) (t : T a a') : ap_e_closure_elim_h e p t = ap_e_closure_elim g e t ⬝ ap (λx, e_closure.elim x t) (eq_of_homotopy3 p) := begin fapply homotopy3.rec_on p, intro q, esimp at q, induction q, esimp, rewrite eq_of_homotopy3_id end theorem ap_ap_e_closure_elim_h {B C D : Type} {f : A → B} {g : B → C} (h : C → D) (e : Π⦃a a' : A⦄, R a a' → f a = f a') {e' : Π⦃a a' : A⦄, R a a' → g (f a) = g (f a')} (p : Π⦃a a' : A⦄ (s : R a a'), ap g (e s) = e' s) (t : T a a') : square (ap (ap h) (ap_e_closure_elim_h e p t)) (ap_e_closure_elim_h e (λa a' s, ap_compose h g (e s)) t) (ap_compose h g (e_closure.elim e t))⁻¹ (ap_e_closure_elim_h e' (λa a' s, (ap (ap h) (p s))⁻¹) t) := begin induction t, { esimp, apply square_of_eq, exact !con.right_inv ⬝ !con.left_inv⁻¹}, { apply ids}, { rewrite [▸*,ap_con (ap h)], refine (transpose !ap_compose_inv)⁻¹ᵛ ⬝h _, rewrite [con_inv,inv_inv,-inv2_inv], exact !ap_inv2 ⬝v square_inv2 v_0}, { rewrite [▸*,ap_con (ap h)], refine (transpose !ap_compose_con)⁻¹ᵛ ⬝h _, rewrite [con_inv,inv_inv,con2_inv], refine !ap_con2 ⬝v square_con2 v_0 v_1}, end theorem ap_ap_e_closure_elim {B C D : Type} {f : A → B} (g : B → C) (h : C → D) (e : Π⦃a a' : A⦄, R a a' → f a = f a') (t : T a a') : square (ap (ap h) (ap_e_closure_elim g e t)) (ap_e_closure_elim_h e (λa a' s, ap_compose h g (e s)) t) (ap_compose h g (e_closure.elim e t))⁻¹ (ap_e_closure_elim h (λa a' r, ap g (e r)) t) := !ap_ap_e_closure_elim_h open e_closure definition is_equivalence_e_closure : is_equivalence T := begin constructor, intro a, exact rfl, intro a a' t, exact t⁻¹ʳ, intro a a' a'' t t', exact t ⬝r t', end definition e_closure.transport_left {f : A → B} (e : Π⦃a a' : A⦄, R a a' → f a = f a') (t : e_closure R a a') (p : a = a'') : e_closure.elim e (p ▸ t) = (ap f p)⁻¹ ⬝ e_closure.elim e t := by induction p; exact !idp_con⁻¹ definition e_closure.transport_right {f : A → B} (e : Π⦃a a' : A⦄, R a a' → f a = f a') (t : e_closure R a a') (p : a' = a'') : e_closure.elim e (p ▸ t) = e_closure.elim e t ⬝ (ap f p) := by induction p; reflexivity definition e_closure.transport_lr {f : A → B} (e : Π⦃a a' : A⦄, R a a' → f a = f a') (t : e_closure R a a) (p : a = a') : e_closure.elim e (p ▸ t) = (ap f p)⁻¹ ⬝ e_closure.elim e t ⬝ (ap f p) := by induction p; esimp; exact !idp_con⁻¹ --dependent elimination: variables {P : B → Type} {Q : C → Type} {f : A → B} {g : B → C} {f' : Π(a : A), P (f a)} protected definition e_closure.elimo [unfold 6] (p : Π⦃a a' : A⦄, R a a' → f a = f a') (po : Π⦃a a' : A⦄ (s : R a a'), f' a =[p s] f' a') (t : T a a') : f' a =[e_closure.elim p t] f' a' := begin induction t, exact po r, constructor, exact v_0⁻¹ᵒ, exact v_0 ⬝o v_1 end definition ap_e_closure_elimo_h [unfold 12] {g' : Πb, Q (g b)} (p : Π⦃a a' : A⦄, R a a' → f a = f a') --(po : Π⦃a a' : A⦄ (s : R a a'), f' a =[p s] f' a') (po : Π⦃a a' : A⦄ (s : R a a'), g' (f a) =[p s] g' (f a')) (q : Π⦃a a' : A⦄ (s : R a a'), apdo g' (p s) = po s) (t : T a a') : apdo g' (e_closure.elim p t) = e_closure.elimo p po t := begin induction t, apply q, reflexivity, esimp [e_closure.elim], exact apdo_inv g' (e_closure.elim p r) ⬝ v_0⁻²ᵒ, exact apdo_con g' (e_closure.elim p r) (e_closure.elim p r') ⬝ (v_0 ◾o v_1) end end end relation
e07fa7bf543a764b42560be3870b35b5f317df6b
4727251e0cd73359b15b664c3170e5d754078599
/src/measure_theory/lattice.lean
87b9da7e7679cb1c72cbe61fa99e77c98f502b7a
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
6,886
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 -/ import measure_theory.measure.ae_measurable /-! # Typeclasses for measurability of lattice operations In this file we define classes `has_measurable_sup` and `has_measurable_inf` and prove dot-style lemmas (`measurable.sup`, `ae_measurable.sup` etc). For binary operations we define two typeclasses: - `has_measurable_sup` says that both left and right sup are measurable; - `has_measurable_sup₂` says that `λ p : α × α, p.1 ⊔ p.2` is measurable, and similarly for other binary operations. The reason for introducing these classes is that in case of topological space `α` equipped with the Borel `σ`-algebra, instances for `has_measurable_sup₂` etc require `α` to have a second countable topology. For instances relating, e.g., `has_continuous_sup` to `has_measurable_sup` see file `measure_theory.borel_space`. ## Tags measurable function, lattice operation -/ open measure_theory /-- We say that a type `has_measurable_sup` if `((⊔) c)` and `(⊔ c)` are measurable functions. For a typeclass assuming measurability of `uncurry (⊔)` see `has_measurable_sup₂`. -/ class has_measurable_sup (M : Type*) [measurable_space M] [has_sup M] : Prop := (measurable_const_sup : ∀ c : M, measurable ((⊔) c)) (measurable_sup_const : ∀ c : M, measurable (⊔ c)) /-- We say that a type `has_measurable_sup₂` if `uncurry (⊔)` is a measurable functions. For a typeclass assuming measurability of `((⊔) c)` and `(⊔ c)` see `has_measurable_sup`. -/ class has_measurable_sup₂ (M : Type*) [measurable_space M] [has_sup M] : Prop := (measurable_sup : measurable (λ p : M × M, p.1 ⊔ p.2)) export has_measurable_sup₂ (measurable_sup) has_measurable_sup (measurable_const_sup measurable_sup_const) /-- We say that a type `has_measurable_inf` if `((⊓) c)` and `(⊓ c)` are measurable functions. For a typeclass assuming measurability of `uncurry (⊓)` see `has_measurable_inf₂`. -/ class has_measurable_inf (M : Type*) [measurable_space M] [has_inf M] : Prop := (measurable_const_inf : ∀ c : M, measurable ((⊓) c)) (measurable_inf_const : ∀ c : M, measurable (⊓ c)) /-- We say that a type `has_measurable_inf₂` if `uncurry (⊔)` is a measurable functions. For a typeclass assuming measurability of `((⊔) c)` and `(⊔ c)` see `has_measurable_inf`. -/ class has_measurable_inf₂ (M : Type*) [measurable_space M] [has_inf M] : Prop := (measurable_inf : measurable (λ p : M × M, p.1 ⊓ p.2)) export has_measurable_inf₂ (measurable_inf) has_measurable_inf (measurable_const_inf measurable_inf_const) variables {M : Type*} [measurable_space M] section order_dual @[priority 100] instance [has_inf M] [has_measurable_inf M] : has_measurable_sup Mᵒᵈ := ⟨@measurable_const_inf M _ _ _, @measurable_inf_const M _ _ _⟩ @[priority 100] instance [has_sup M] [has_measurable_sup M] : has_measurable_inf Mᵒᵈ := ⟨@measurable_const_sup M _ _ _, @measurable_sup_const M _ _ _⟩ @[priority 100] instance [has_inf M] [has_measurable_inf₂ M] : has_measurable_sup₂ Mᵒᵈ := ⟨@measurable_inf M _ _ _⟩ @[priority 100] instance [has_sup M] [has_measurable_sup₂ M] : has_measurable_inf₂ Mᵒᵈ := ⟨@measurable_sup M _ _ _⟩ end order_dual variables {α : Type*} {m : measurable_space α} {μ : measure α} {f g : α → M} include m section sup variables [has_sup M] section measurable_sup variables [has_measurable_sup M] @[measurability] lemma measurable.const_sup (hf : measurable f) (c : M) : measurable (λ x, c ⊔ f x) := (measurable_const_sup c).comp hf @[measurability] lemma ae_measurable.const_sup (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, c ⊔ f x) μ := (has_measurable_sup.measurable_const_sup c).comp_ae_measurable hf @[measurability] lemma measurable.sup_const (hf : measurable f) (c : M) : measurable (λ x, f x ⊔ c) := (measurable_sup_const c).comp hf @[measurability] lemma ae_measurable.sup_const (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, f x ⊔ c) μ := (measurable_sup_const c).comp_ae_measurable hf end measurable_sup section measurable_sup₂ variables [has_measurable_sup₂ M] @[measurability] lemma measurable.sup' (hf : measurable f) (hg : measurable g) : measurable (f ⊔ g) := measurable_sup.comp (hf.prod_mk hg) @[measurability] lemma measurable.sup (hf : measurable f) (hg : measurable g) : measurable (λ a, f a ⊔ g a) := measurable_sup.comp (hf.prod_mk hg) @[measurability] lemma ae_measurable.sup' (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (f ⊔ g) μ := measurable_sup.comp_ae_measurable (hf.prod_mk hg) @[measurability] lemma ae_measurable.sup (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, f a ⊔ g a) μ := measurable_sup.comp_ae_measurable (hf.prod_mk hg) omit m @[priority 100] instance has_measurable_sup₂.to_has_measurable_sup : has_measurable_sup M := ⟨λ c, measurable_const.sup measurable_id, λ c, measurable_id.sup measurable_const⟩ include m end measurable_sup₂ end sup section inf variables [has_inf M] section measurable_inf variables [has_measurable_inf M] @[measurability] lemma measurable.const_inf (hf : measurable f) (c : M) : measurable (λ x, c ⊓ f x) := (measurable_const_inf c).comp hf @[measurability] lemma ae_measurable.const_inf (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, c ⊓ f x) μ := (has_measurable_inf.measurable_const_inf c).comp_ae_measurable hf @[measurability] lemma measurable.inf_const (hf : measurable f) (c : M) : measurable (λ x, f x ⊓ c) := (measurable_inf_const c).comp hf @[measurability] lemma ae_measurable.inf_const (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, f x ⊓ c) μ := (measurable_inf_const c).comp_ae_measurable hf end measurable_inf section measurable_inf₂ variables [has_measurable_inf₂ M] @[measurability] lemma measurable.inf' (hf : measurable f) (hg : measurable g) : measurable (f ⊓ g) := measurable_inf.comp (hf.prod_mk hg) @[measurability] lemma measurable.inf (hf : measurable f) (hg : measurable g) : measurable (λ a, f a ⊓ g a) := measurable_inf.comp (hf.prod_mk hg) @[measurability] lemma ae_measurable.inf' (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (f ⊓ g) μ := measurable_inf.comp_ae_measurable (hf.prod_mk hg) @[measurability] lemma ae_measurable.inf (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, f a ⊓ g a) μ := measurable_inf.comp_ae_measurable (hf.prod_mk hg) omit m @[priority 100] instance has_measurable_inf₂.to_has_measurable_inf : has_measurable_inf M := ⟨λ c, measurable_const.inf measurable_id, λ c, measurable_id.inf measurable_const⟩ include m end measurable_inf₂ end inf
a46fd9b71804c2ba19cbf607b92950e66e324231
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/measure_theory/decomposition/lebesgue.lean
88083c885c89a2ee5ae7225a8a62323a4dbede4a
[ "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
28,073
lean
/- Copyright (c) 2021 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import measure_theory.decomposition.jordan /-! # Lebesgue decomposition This file proves the Lebesgue decomposition theorem. The Lebesgue decomposition theorem states that, given two finite measures `μ` and `ν`, there exists a finite measure `ξ` and a measurable function `f` such that `μ = ξ + fν` and `ξ` is mutually singular with respect to `ν`. The Lebesgue decomposition provides the Radon-Nikodym theorem readily. ## Main definitions * `measure_theory.measure.have_lebesgue_decomposition` : A pair of measures `μ` and `ν` is said to `have_lebesgue_decomposition` if there exists a measure `ξ` and a measurable function `f`, such that `ξ` is mutually singular with respect to `ν` and `μ = ξ + ν.with_density f` * `measure_theory.measure.singular_part` : If a pair of measures `have_lebesgue_decomposition`, then `singular_part` chooses the measure from `have_lebesgue_decomposition`, otherwise it returns the zero measure. * ``measure_theory.measure.radon_nikodym_deriv` : If a pair of measures `have_lebesgue_decomposition`, then `radon_nikodym_deriv` chooses the measurable function from `have_lebesgue_decomposition`, otherwise it returns the zero function. ## Main results * `measure_theory.measure.have_lebesgue_decomposition_of_finite_measure` : the Lebesgue decomposition theorem. * `measure_theory.measure.eq_singular_part` : Given measures `μ` and `ν`, if `s` is a measure mutually singular to `ν` and `f` is a measurable function such that `μ = s + fν`, then `s = singular_part μ ν`. * `measure_theory.measure.eq_radon_nikodym_deriv` : Given measures `μ` and `ν`, if `s` is a measure mutually singular to `ν` and `f` is a measurable function such that `μ = s + fν`, then `f = radon_nikodym_deriv μ ν`. ## To do The Lebesgue decomposition theorem can be generalized to σ-finite measures from the finite version. # Tags Lebesgue decomposition theorem -/ noncomputable theory open_locale classical measure_theory nnreal ennreal variables {α β : Type*} [measurable_space α] namespace measure_theory namespace measure /-- A pair of measures `μ` and `ν` is said to `have_lebesgue_decomposition` if there exists a measure `ξ` and a measurable function `f`, such that `ξ` is mutually singular with respect to `ν` and `μ = ξ + ν.with_density f`. -/ def have_lebesgue_decomposition (μ ν : measure α) : Prop := ∃ (p : measure α × (α → ℝ≥0∞)), measurable p.2 ∧ p.1 ⊥ₘ ν ∧ μ = p.1 + ν.with_density p.2 /-- If a pair of measures `have_lebesgue_decomposition`, then `singular_part` chooses the measure from `have_lebesgue_decomposition`, otherwise it returns the zero measure. -/ @[irreducible] def singular_part (μ ν : measure α) : measure α := if h : have_lebesgue_decomposition μ ν then (classical.some h).1 else 0 /-- If a pair of measures `have_lebesgue_decomposition`, then `radon_nikodym_deriv` chooses the measurable function from `have_lebesgue_decomposition`, otherwise it returns the zero function. -/ @[irreducible] def radon_nikodym_deriv (μ ν : measure α) : α → ℝ≥0∞ := if h : have_lebesgue_decomposition μ ν then (classical.some h).2 else 0 lemma have_lebesgue_decomposition_spec {μ ν : measure α} (h : have_lebesgue_decomposition μ ν) : measurable (radon_nikodym_deriv μ ν) ∧ (singular_part μ ν) ⊥ₘ ν ∧ μ = (singular_part μ ν) + ν.with_density (radon_nikodym_deriv μ ν) := begin rw [singular_part, radon_nikodym_deriv, dif_pos h, dif_pos h], exact classical.some_spec h, end lemma have_lebesgue_decomposition_add {μ ν : measure α} (h : have_lebesgue_decomposition μ ν) : μ = (singular_part μ ν) + ν.with_density (radon_nikodym_deriv μ ν) := (have_lebesgue_decomposition_spec h).2.2 @[measurability] lemma measurable_radon_nikodym_deriv (μ ν : measure α) : measurable $ radon_nikodym_deriv μ ν := begin by_cases h : have_lebesgue_decomposition μ ν, { exact (have_lebesgue_decomposition_spec h).1 }, { rw [radon_nikodym_deriv, dif_neg h], exact measurable_zero } end lemma mutually_singular_singular_part (μ ν : measure α) : singular_part μ ν ⊥ₘ ν := begin by_cases h : have_lebesgue_decomposition μ ν, { exact (have_lebesgue_decomposition_spec h).2.1 }, { rw [singular_part, dif_neg h], exact mutually_singular.zero.symm } end lemma singular_part_le (μ ν : measure α) : singular_part μ ν ≤ μ := begin by_cases h : have_lebesgue_decomposition μ ν, { obtain ⟨-, -, h⟩ := have_lebesgue_decomposition_spec h, conv_rhs { rw h }, exact measure.le_add_right (le_refl _) }, { rw [singular_part, dif_neg h], exact measure.zero_le μ } end lemma with_density_radon_nikodym_deriv_le (μ ν : measure α) : ν.with_density (radon_nikodym_deriv μ ν) ≤ μ := begin by_cases h : have_lebesgue_decomposition μ ν, { obtain ⟨-, -, h⟩ := have_lebesgue_decomposition_spec h, conv_rhs { rw h }, exact measure.le_add_left (le_refl _) }, { rw [radon_nikodym_deriv, dif_neg h, with_density_zero], exact measure.zero_le μ } end instance {μ ν : measure α} [finite_measure μ] : finite_measure (singular_part μ ν) := finite_measure_of_le μ $ singular_part_le μ ν instance {μ ν : measure α} [sigma_finite μ] : sigma_finite (singular_part μ ν) := sigma_finite_of_le μ $ singular_part_le μ ν instance {μ ν : measure α} [finite_measure μ] : finite_measure (ν.with_density $ radon_nikodym_deriv μ ν) := finite_measure_of_le μ $ with_density_radon_nikodym_deriv_le μ ν instance {μ ν : measure α} [sigma_finite μ] : sigma_finite (ν.with_density $ radon_nikodym_deriv μ ν) := sigma_finite_of_le μ $ with_density_radon_nikodym_deriv_le μ ν /-- Given measures `μ` and `ν`, if `s` is a measure mutually singular to `ν` and `f` is a measurable function such that `μ = s + fν`, then `s = singular_part μ ν`. This theorem provides the uniqueness of the `singular_part` in the Lebesgue decomposition theorem, while `measure_theory.measure.eq_radon_nikodym_deriv` provides the uniqueness of the `radon_nikodym_deriv`. -/ theorem eq_singular_part {μ ν : measure α} {s : measure α} {f : α → ℝ≥0∞} (hf : measurable f) (hs : s ⊥ₘ ν) (hadd : μ = s + ν.with_density f) : s = μ.singular_part ν := begin obtain ⟨hmeas, hsing, hadd'⟩ := have_lebesgue_decomposition_spec ⟨⟨s, f⟩, hf, hs, hadd⟩, obtain ⟨⟨S, hS₁, hS₂, hS₃⟩, ⟨T, hT₁, hT₂, hT₃⟩⟩ := ⟨hs, hsing⟩, rw hadd' at hadd, have hνinter : ν (S ∩ T)ᶜ = 0, { rw set.compl_inter, refine nonpos_iff_eq_zero.1 (le_trans (measure_union_le _ _) _), rw [hT₃, hS₃, add_zero], exact le_refl _ }, have heq : s.restrict (S ∩ T)ᶜ = (μ.singular_part ν).restrict (S ∩ T)ᶜ, { ext1 A hA, have hf : ν.with_density f (A ∩ (S ∩ T)ᶜ) = 0, { refine with_density_absolutely_continuous ν _ _, rw ← nonpos_iff_eq_zero, exact hνinter ▸ measure_mono (set.inter_subset_right _ _) }, have hrn : ν.with_density (μ.radon_nikodym_deriv ν) (A ∩ (S ∩ T)ᶜ) = 0, { refine with_density_absolutely_continuous ν _ _, rw ← nonpos_iff_eq_zero, exact hνinter ▸ measure_mono (set.inter_subset_right _ _) }, rw [restrict_apply hA, restrict_apply hA, ← add_zero (s (A ∩ (S ∩ T)ᶜ)), ← hf, ← add_apply, ← hadd, add_apply, hrn, add_zero] }, have heq' : ∀ A : set α, measurable_set A → s A = s.restrict (S ∩ T)ᶜ A, { intros A hA, have hsinter : s (A ∩ (S ∩ T)) = 0, { rw ← nonpos_iff_eq_zero, exact hS₂ ▸ measure_mono (set.subset.trans (set.inter_subset_right _ _) (set.inter_subset_left _ _)) }, rw [restrict_apply hA, ← add_zero (s (A ∩ (S ∩ T)ᶜ)), ← hsinter, ← measure_union, ← set.inter_union_distrib_left, set.compl_union_self, set.inter_univ], { exact disjoint.inter_left' _ ( disjoint.inter_right' _ disjoint_compl_left) }, { measurability }, { measurability } }, ext1 A hA, have hμinter : μ.singular_part ν (A ∩ (S ∩ T)) = 0, { rw ← nonpos_iff_eq_zero, exact hT₂ ▸ measure_mono (set.subset.trans (set.inter_subset_right _ _) (set.inter_subset_right _ _)) }, rw [heq' A hA, heq, ← add_zero ((μ.singular_part ν).restrict (S ∩ T)ᶜ A), ← hμinter, restrict_apply hA, ← measure_union, ← set.inter_union_distrib_left, set.compl_union_self, set.inter_univ], { exact disjoint.inter_left' _ ( disjoint.inter_right' _ disjoint_compl_left) }, { measurability }, { measurability } end /-- Given measures `μ` and `ν`, if `s` is a measure mutually singular to `ν` and `f` is a measurable function such that `μ = s + fν`, then `f = radon_nikodym_deriv μ ν`. This theorem provides the uniqueness of the `radon_nikodym_deriv` in the Lebesgue decomposition theorem, while `measure_theory.measure.eq_singular_part` provides the uniqueness of the `singular_part`. -/ theorem eq_radon_nikodym_deriv {μ ν : measure α} {s : measure α} {f : α → ℝ≥0∞} (hf : measurable f) (hs : s ⊥ₘ ν) (hadd : μ = s + ν.with_density f) : ν.with_density f = ν.with_density (μ.radon_nikodym_deriv ν) := begin obtain ⟨hmeas, hsing, hadd'⟩ := have_lebesgue_decomposition_spec ⟨⟨s, f⟩, hf, hs, hadd⟩, obtain ⟨⟨S, hS₁, hS₂, hS₃⟩, ⟨T, hT₁, hT₂, hT₃⟩⟩ := ⟨hs, hsing⟩, rw hadd' at hadd, have hνinter : ν (S ∩ T)ᶜ = 0, { rw set.compl_inter, refine nonpos_iff_eq_zero.1 (le_trans (measure_union_le _ _) _), rw [hT₃, hS₃, add_zero], exact le_refl _ }, have heq : (ν.with_density f).restrict (S ∩ T) = (ν.with_density (radon_nikodym_deriv μ ν)).restrict (S ∩ T), { ext1 A hA, have hs : s (A ∩ (S ∩ T)) = 0, { rw ← nonpos_iff_eq_zero, exact hS₂ ▸ measure_mono (set.subset.trans (set.inter_subset_right _ _) (set.inter_subset_left _ _)) }, have hsing : μ.singular_part ν (A ∩ (S ∩ T)) = 0, { rw ← nonpos_iff_eq_zero, exact hT₂ ▸ measure_mono (set.subset.trans (set.inter_subset_right _ _) (set.inter_subset_right _ _)) }, rw [restrict_apply hA, restrict_apply hA, ← add_zero (ν.with_density f (A ∩ (S ∩ T))), ← hs, ← add_apply, add_comm, ← hadd, add_apply, hsing, zero_add] }, have heq' : ∀ A : set α, measurable_set A → ν.with_density f A = (ν.with_density f).restrict (S ∩ T) A, { intros A hA, have hνfinter : ν.with_density f (A ∩ (S ∩ T)ᶜ) = 0, { rw ← nonpos_iff_eq_zero, exact with_density_absolutely_continuous ν f hνinter ▸ measure_mono (set.inter_subset_right _ _) }, rw [restrict_apply hA, ← add_zero (ν.with_density f (A ∩ (S ∩ T))), ← hνfinter, ← measure_union, ← set.inter_union_distrib_left, set.union_compl_self, set.inter_univ], { exact disjoint.inter_left' _ (disjoint.inter_right' _ disjoint_compl_right) }, { measurability }, { measurability } }, ext1 A hA, have hνrn : ν.with_density (μ.radon_nikodym_deriv ν) (A ∩ (S ∩ T)ᶜ) = 0, { rw ← nonpos_iff_eq_zero, exact with_density_absolutely_continuous ν (μ.radon_nikodym_deriv ν) hνinter ▸ measure_mono (set.inter_subset_right _ _) }, rw [heq' A hA, heq, ← add_zero ((ν.with_density (μ.radon_nikodym_deriv ν)).restrict (S ∩ T) A), ← hνrn, restrict_apply hA, ← measure_union, ← set.inter_union_distrib_left, set.union_compl_self, set.inter_univ], { exact disjoint.inter_left' _ (disjoint.inter_right' _ disjoint_compl_right) }, { measurability }, { measurability } end open vector_measure signed_measure /-- If two finite measures `μ` and `ν` are not mutually singular, there exists some `ε > 0` and a measurable set `E`, such that `ν(E) > 0` and `E` is positive with respect to `μ - εν`. This lemma is useful for the Lebesgue decomposition theorem. -/ lemma exists_positive_of_not_mutually_singular (μ ν : measure α) [finite_measure μ] [finite_measure ν] (h : ¬ μ ⊥ₘ ν) : ∃ ε : ℝ≥0, 0 < ε ∧ ∃ E : set α, measurable_set E ∧ 0 < ν E ∧ 0 ≤[E] μ.to_signed_measure - (ε • ν).to_signed_measure := begin -- for all `n : ℕ`, obtain the Hahn decomposition for `μ - (1 / n) ν` have : ∀ n : ℕ, ∃ i : set α, measurable_set i ∧ 0 ≤[i] (μ.to_signed_measure - ((1 / (n + 1) : ℝ≥0) • ν).to_signed_measure) ∧ (μ.to_signed_measure - ((1 / (n + 1) : ℝ≥0) • ν).to_signed_measure) ≤[iᶜ] 0, { intro, exact exists_compl_positive_negative _ }, choose f hf₁ hf₂ hf₃ using this, -- set `A` to be the intersection of all the negative parts of obtained Hahn decompositions -- and we show that `μ A = 0` set A := ⋂ n, (f n)ᶜ with hA₁, have hAmeas : measurable_set A, { exact measurable_set.Inter (λ n, (hf₁ n).compl) }, have hA₂ : ∀ n : ℕ, (μ.to_signed_measure - ((1 / (n + 1) : ℝ≥0) • ν).to_signed_measure) ≤[A] 0, { intro n, exact restrict_le_restrict_subset _ _ (hf₁ n).compl (hf₃ n) (set.Inter_subset _ _) }, have hA₃ : ∀ n : ℕ, μ A ≤ (1 / (n + 1) : ℝ≥0) * ν A, { intro n, have := nonpos_of_restrict_le_zero _ (hA₂ n), rwa [to_signed_measure_sub_apply hAmeas, sub_nonpos, ennreal.to_real_le_to_real] at this, exacts [ne_of_lt (measure_lt_top _ _), ne_of_lt (measure_lt_top _ _)] }, have hμ : μ A = 0, { lift μ A to ℝ≥0 using ne_of_lt (measure_lt_top _ _) with μA, lift ν A to ℝ≥0 using ne_of_lt (measure_lt_top _ _) with νA, rw ennreal.coe_eq_zero, by_cases hb : 0 < νA, { suffices : ∀ b, 0 < b → μA ≤ b, { by_contra, have h' := this (μA / 2) (nnreal.half_pos (zero_lt_iff.2 h)), rw ← @not_not (μA ≤ μA / 2) at h', exact h' (not_le.2 (nnreal.half_lt_self h)) }, intros c hc, have : ∃ n : ℕ, 1 / (n + 1 : ℝ) < c * νA⁻¹, refine exists_nat_one_div_lt _, { refine mul_pos hc _, rw _root_.inv_pos, exact hb }, rcases this with ⟨n, hn⟩, have hb₁ : (0 : ℝ) < νA⁻¹, { rw _root_.inv_pos, exact hb }, have h' : 1 / (↑n + 1) * νA < c, { rw [← nnreal.coe_lt_coe, ← mul_lt_mul_right hb₁, nnreal.coe_mul, mul_assoc, ← nnreal.coe_inv, ← nnreal.coe_mul, _root_.mul_inv_cancel, ← nnreal.coe_mul, mul_one, nnreal.coe_inv], { convert hn, simp }, { exact ne.symm (ne_of_lt hb) } }, refine le_trans _ (le_of_lt h'), rw [← ennreal.coe_le_coe, ennreal.coe_mul], exact hA₃ n }, { rw [not_lt, le_zero_iff] at hb, specialize hA₃ 0, simp [hb, le_zero_iff] at hA₃, assumption } }, -- since `μ` and `ν` are not mutually singular, `μ A = 0` implies `ν Aᶜ > 0` rw mutually_singular at h, push_neg at h, have := h _ hAmeas hμ, simp_rw [hA₁, set.compl_Inter, compl_compl] at this, -- as `Aᶜ = ⋃ n, f n`, `ν Aᶜ > 0` implies there exists some `n` such that `ν (f n) > 0` obtain ⟨n, hn⟩ := exists_measure_pos_of_not_measure_Union_null this, -- thus, choosing `f n` as the set `E` suffices exact ⟨1 / (n + 1), by simp, f n, hf₁ n, hn, hf₂ n⟩, end namespace lebesgue_decomposition /-- Given two measures `μ` and `ν`, `measurable_le μ ν` is the set of measurable functions `f`, such that, for all measurable sets `A`, `∫⁻ x in A, f x ∂μ ≤ ν A`. This is useful for the Lebesgue decomposition theorem. -/ def measurable_le (μ ν : measure α) : set (α → ℝ≥0∞) := { f | measurable f ∧ ∀ (A : set α) (hA : measurable_set A), ∫⁻ x in A, f x ∂μ ≤ ν A } variables {μ ν : measure α} lemma zero_mem_measurable_le : (0 : α → ℝ≥0∞) ∈ measurable_le μ ν := ⟨measurable_zero, λ A hA, by simp⟩ lemma max_measurable_le (f g : α → ℝ≥0∞) (hf : f ∈ measurable_le μ ν) (hg : g ∈ measurable_le μ ν) (A : set α) (hA : measurable_set A) : ∫⁻ a in A, max (f a) (g a) ∂μ ≤ ∫⁻ a in A ∩ { a | f a ≤ g a }, g a ∂μ + ∫⁻ a in A ∩ { a | g a < f a }, f a ∂μ := begin rw [← lintegral_indicator _ hA, ← lintegral_indicator f, ← lintegral_indicator g, ← lintegral_add], { refine lintegral_mono (λ a, _), by_cases haA : a ∈ A, { by_cases f a ≤ g a, { simp only, rw [set.indicator_of_mem haA, set.indicator_of_mem, set.indicator_of_not_mem, add_zero], simp only [le_refl, max_le_iff, and_true, h], { rintro ⟨_, hc⟩, exact false.elim ((not_lt.2 h) hc) }, { exact ⟨haA, h⟩ } }, { simp only, rw [set.indicator_of_mem haA, set.indicator_of_mem _ f, set.indicator_of_not_mem, zero_add], simp only [true_and, le_refl, max_le_iff, le_of_lt (not_le.1 h)], { rintro ⟨_, hc⟩, exact false.elim (h hc) }, { exact ⟨haA, not_le.1 h⟩ } } }, { simp [set.indicator_of_not_mem haA] } }, { exact measurable.indicator hg.1 (hA.inter (measurable_set_le hf.1 hg.1)) }, { exact measurable.indicator hf.1 (hA.inter (measurable_set_lt hg.1 hf.1)) }, { exact hA.inter (measurable_set_le hf.1 hg.1) }, { exact hA.inter (measurable_set_lt hg.1 hf.1) }, end lemma sup_mem_measurable_le {f g : α → ℝ≥0∞} (hf : f ∈ measurable_le μ ν) (hg : g ∈ measurable_le μ ν) : (λ a, f a ⊔ g a) ∈ measurable_le μ ν := begin simp_rw ennreal.sup_eq_max, refine ⟨measurable.max hf.1 hg.1, λ A hA, _⟩, have h₁ := hA.inter (measurable_set_le hf.1 hg.1), have h₂ := hA.inter (measurable_set_lt hg.1 hf.1), refine le_trans (max_measurable_le f g hf hg A hA) _, refine le_trans (add_le_add (hg.2 _ h₁) (hf.2 _ h₂)) _, { rw [← measure_union _ h₁ h₂], { refine le_of_eq _, congr, convert set.inter_union_compl A _, ext a, simpa }, rintro x ⟨⟨-, hx₁⟩, -, hx₂⟩, exact (not_le.2 hx₂) hx₁ } end lemma supr_succ_eq_sup {α} (f : ℕ → α → ℝ≥0∞) (m : ℕ) (a : α) : (⨆ (k : ℕ) (hk : k ≤ m + 1), f k a) = f m.succ a ⊔ ⨆ (k : ℕ) (hk : k ≤ m), f k a := begin ext x, simp only [option.mem_def, ennreal.some_eq_coe], split; intro h; rw ← h, symmetry, all_goals { set c := (⨆ (k : ℕ) (hk : k ≤ m + 1), f k a) with hc, set d := (f m.succ a ⊔ ⨆ (k : ℕ) (hk : k ≤ m), f k a) with hd, suffices : c ≤ d ∧ d ≤ c, { change c = d, -- removing this line breaks exact le_antisymm this.1 this.2 }, rw [hc, hd], refine ⟨_, _⟩, { refine bsupr_le (λ n hn, _), rcases nat.of_le_succ hn with (h | h), { exact le_sup_of_le_right (le_bsupr n h) }, { exact h ▸ le_sup_left } }, { refine sup_le _ _, { convert @le_bsupr _ _ _ (λ i, i ≤ m + 1) _ m.succ (le_refl _), refl }, { refine bsupr_le (λ n hn, _), have := (le_trans hn (nat.le_succ m)), -- replacing `this` below with the proof breaks exact (le_bsupr n this) } } }, end lemma supr_mem_measurable_le (f : ℕ → α → ℝ≥0∞) (hf : ∀ n, f n ∈ measurable_le μ ν) (n : ℕ) : (λ x, ⨆ k (hk : k ≤ n), f k x) ∈ measurable_le μ ν := begin induction n with m hm, { refine ⟨_, _⟩, { simp [(hf 0).1] }, { intros A hA, simp [(hf 0).2 A hA] } }, { have : (λ (a : α), ⨆ (k : ℕ) (hk : k ≤ m + 1), f k a) = (λ a, f m.succ a ⊔ ⨆ (k : ℕ) (hk : k ≤ m), f k a), { exact funext (λ _, supr_succ_eq_sup _ _ _) }, refine ⟨measurable_supr (λ n, measurable.supr_Prop _ (hf n).1), λ A hA, _⟩, rw this, exact (sup_mem_measurable_le (hf m.succ) hm).2 A hA } end lemma supr_mem_measurable_le' (f : ℕ → α → ℝ≥0∞) (hf : ∀ n, f n ∈ measurable_le μ ν) (n : ℕ) : (⨆ k (hk : k ≤ n), f k) ∈ measurable_le μ ν := begin convert supr_mem_measurable_le f hf n, ext, simp end lemma supr_monotone {α : Type*} (f : ℕ → α → ℝ≥0∞) : monotone (λ n x, ⨆ k (hk : k ≤ n), f k x) := begin intros n m hnm x, simp only, refine bsupr_le (λ k hk, _), have : k ≤ m := le_trans hk hnm, -- replacing `this` below with the proof breaks exact le_bsupr k this, end lemma supr_monotone' {α : Type*} (f : ℕ → α → ℝ≥0∞) (x : α) : monotone (λ n, ⨆ k (hk : k ≤ n), f k x) := λ n m hnm, supr_monotone f hnm x lemma supr_le_le {α : Type*} (f : ℕ → α → ℝ≥0∞) (n k : ℕ) (hk : k ≤ n) : f k ≤ λ x, ⨆ k (hk : k ≤ n), f k x := λ x, le_bsupr k hk /-- `measurable_le_eval μ ν` is the set of `∫⁻ x, f x ∂μ` for all `f ∈ measurable_le μ ν`. -/ def measurable_le_eval (μ ν : measure α) : set ℝ≥0∞ := (λ f : α → ℝ≥0∞, ∫⁻ x, f x ∂μ) '' measurable_le μ ν end lebesgue_decomposition open lebesgue_decomposition /-- **The Lebesgue decomposition theorem**: Any pair of finite measures `μ` and `ν` `have_lebesgue_decomposition`. That is to say, there exists a measure `ξ` and a measurable function `f`, such that `ξ` is mutually singular with respect to `ν` and `μ = ξ + ν.with_density f` -/ theorem have_lebesgue_decomposition_of_finite_measure (μ ν : measure α) [finite_measure μ] [finite_measure ν] : have_lebesgue_decomposition μ ν := begin have h := @exists_seq_tendsto_Sup _ _ _ _ _ (measurable_le_eval ν μ) ⟨0, 0, zero_mem_measurable_le, by simp⟩ (order_top.bdd_above _), choose g hmono hg₂ f hf₁ hf₂ using h, -- we set `ξ` to be the supremum of an increasing sequence of functions obtained from above set ξ := ⨆ n k (hk : k ≤ n), f k with hξ, -- we see that `ξ` has the largest integral among all functions in `measurable_le` have hξ₁ : Sup (measurable_le_eval ν μ) = ∫⁻ a, ξ a ∂ν, { have := @lintegral_tendsto_of_tendsto_of_monotone _ _ ν (λ n, ⨆ k (hk : k ≤ n), f k) (⨆ n k (hk : k ≤ n), f k) _ _ _, { refine tendsto_nhds_unique _ this, refine tendsto_of_tendsto_of_tendsto_of_le_of_le hg₂ tendsto_const_nhds _ _, { intro n, rw ← hf₂ n, apply lintegral_mono, simp only [supr_apply, supr_le_le f n n (le_refl _)] }, { intro n, exact le_Sup ⟨⨆ (k : ℕ) (hk : k ≤ n), f k, supr_mem_measurable_le' _ hf₁ _, rfl⟩ } }, { intro n, refine measurable.ae_measurable _, convert (supr_mem_measurable_le _ hf₁ n).1, ext, simp }, { refine filter.eventually_of_forall (λ a, _), simp [supr_monotone' f _] }, { refine filter.eventually_of_forall (λ a, _), simp [tendsto_at_top_supr (supr_monotone' f a)] } }, have hξm : measurable ξ, { convert measurable_supr (λ n, (supr_mem_measurable_le _ hf₁ n).1), ext, simp [hξ] }, -- `ξ` is the `f` in the theorem statement and we set `μ₁` to be `μ - ν.with_density ξ` -- since we need `μ₁ + ν.with_density ξ = μ` set μ₁ := μ - ν.with_density ξ with hμ₁, have hle : ν.with_density ξ ≤ μ, { intros B hB, rw [hξ, with_density_apply _ hB], simp_rw [supr_apply], rw lintegral_supr (λ i, (supr_mem_measurable_le _ hf₁ i).1) (supr_monotone _), exact supr_le (λ i, (supr_mem_measurable_le _ hf₁ i).2 B hB) }, haveI : finite_measure (ν.with_density ξ), { refine finite_measure_with_density _, have hle' := hle set.univ measurable_set.univ, rw [with_density_apply _ measurable_set.univ, measure.restrict_univ] at hle', exact lt_of_le_of_lt hle' (measure_lt_top _ _) }, refine ⟨⟨μ₁, ξ⟩, hξm, _, _⟩, { by_contra, -- if they are not mutually singular, then from `exists_positive_of_not_mutually_singular`, -- there exists some `ε > 0` and a measurable set `E`, such that `μ(E) > 0` and `E` is -- positive with respect to `ν - εμ` obtain ⟨ε, hε₁, E, hE₁, hE₂, hE₃⟩ := exists_positive_of_not_mutually_singular μ₁ ν h, simp_rw hμ₁ at hE₃, have hξle : ∀ A, measurable_set A → ∫⁻ a in A, ξ a ∂ν ≤ μ A, { intros A hA, rw hξ, simp_rw [supr_apply], rw lintegral_supr (λ n, (supr_mem_measurable_le _ hf₁ n).1) (supr_monotone _), exact supr_le (λ n, (supr_mem_measurable_le _ hf₁ n).2 A hA) }, -- since `E` is positive, we have `∫⁻ a in A ∩ E, ε + ξ a ∂ν ≤ μ (A ∩ E)` for all `A` have hε₂ : ∀ A : set α, measurable_set A → ∫⁻ a in A ∩ E, ε + ξ a ∂ν ≤ μ (A ∩ E), { intros A hA, have := subset_le_of_restrict_le_restrict _ _ hE₁ hE₃ (set.inter_subset_right A E), rwa [zero_apply, to_signed_measure_sub_apply (hA.inter hE₁), measure.sub_apply (hA.inter hE₁) hle, ennreal.to_real_sub_of_le _ (ne_of_lt (measure_lt_top _ _)), sub_nonneg, le_sub_iff_add_le, ← ennreal.to_real_add, ennreal.to_real_le_to_real, measure.coe_nnreal_smul, pi.smul_apply, with_density_apply _ (hA.inter hE₁), show ε • ν (A ∩ E) = (ε : ℝ≥0∞) * ν (A ∩ E), by refl, ← set_lintegral_const, ← lintegral_add measurable_const hξm] at this, { rw [ne.def, ennreal.add_eq_top, not_or_distrib], exact ⟨ne_of_lt (measure_lt_top _ _), ne_of_lt (measure_lt_top _ _)⟩ }, { exact ne_of_lt (measure_lt_top _ _) }, { exact ne_of_lt (measure_lt_top _ _) }, { exact ne_of_lt (measure_lt_top _ _) }, { rw with_density_apply _ (hA.inter hE₁), exact hξle (A ∩ E) (hA.inter hE₁) }, { apply_instance } }, -- from this, we can show `ξ + ε * E.indicator` is a function in `measurable_le` with -- integral greater than `ξ` have hξε : ξ + E.indicator (λ _, ε) ∈ measurable_le ν μ, { refine ⟨measurable.add hξm (measurable.indicator measurable_const hE₁), λ A hA, _⟩, have : ∫⁻ a in A, (ξ + E.indicator (λ _, ε)) a ∂ν = ∫⁻ a in A ∩ E, ε + ξ a ∂ν + ∫⁻ a in A ∩ Eᶜ, ξ a ∂ν, { rw [lintegral_add measurable_const hξm, add_assoc, ← lintegral_union (hA.inter hE₁) (hA.inter (hE₁.compl)) (disjoint.mono (set.inter_subset_right _ _) (set.inter_subset_right _ _) disjoint_compl_right), set.inter_union_compl], simp_rw [pi.add_apply], rw [lintegral_add hξm (measurable.indicator measurable_const hE₁), add_comm], refine congr_fun (congr_arg has_add.add _) _, rw [set_lintegral_const, lintegral_indicator _ hE₁, set_lintegral_const, measure.restrict_apply hE₁, set.inter_comm] }, conv_rhs { rw ← set.inter_union_compl A E }, rw [this, measure_union _ (hA.inter hE₁) (hA.inter hE₁.compl)], { exact add_le_add (hε₂ A hA) (hξle (A ∩ Eᶜ) (hA.inter hE₁.compl)) }, { exact disjoint.mono (set.inter_subset_right _ _) (set.inter_subset_right _ _) disjoint_compl_right } }, have : ∫⁻ a, ξ a + E.indicator (λ _, ε) a ∂ν ≤ Sup (measurable_le_eval ν μ) := le_Sup ⟨ξ + E.indicator (λ _, ε), hξε, rfl⟩, -- but this contradicts the maximality of `∫⁻ x, ξ x ∂ν` refine not_lt.2 this _, rw [hξ₁, lintegral_add hξm (measurable.indicator (measurable_const) hE₁), lintegral_indicator _ hE₁, set_lintegral_const], refine ennreal.lt_add_right _ (ennreal.mul_pos.2 ⟨ennreal.coe_pos.2 hε₁, hE₂⟩), have := measure_lt_top (ν.with_density ξ) set.univ, rwa [with_density_apply _ measurable_set.univ, measure.restrict_univ] at this }, -- since `ν.with_density ξ ≤ μ`, it is clear that `μ = μ₁ + ν.with_density ξ` { rw hμ₁, ext1 A hA, rw [measure.coe_add, pi.add_apply, measure.sub_apply hA hle, add_comm, ennreal.add_sub_cancel_of_le (hle A hA)] }, end end measure end measure_theory
01a007757711b996ec88be8484edf91d6b2dc36f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/polynomial/module.lean
d9e8cc96046d10b76b25303c61ad5d0df59bdd08
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
11,126
lean
/- Copyright (c) 2022 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import ring_theory.finite_type /-! # Polynomial module > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file, we define the polynomial module for an `R`-module `M`, i.e. the `R[X]`-module `M[X]`. This is defined as an type alias `polynomial_module R M := ℕ →₀ M`, since there might be different module structures on `ℕ →₀ M` of interest. See the docstring of `polynomial_module` for details. -/ universes u v open polynomial open_locale polynomial big_operators variables (R M : Type*) [comm_ring R] [add_comm_group M] [module R M] (I : ideal R) include R /-- The `R[X]`-module `M[X]` for an `R`-module `M`. This is isomorphic (as an `R`-module) to `M[X]` when `M` is a ring. We require all the module instances `module S (polynomial_module R M)` to factor through `R` except `module R[X] (polynomial_module R M)`. In this constraint, we have the following instances for example : - `R` acts on `polynomial_module R R[X]` - `R[X]` acts on `polynomial_module R R[X]` as `R[Y]` acting on `R[X][Y]` - `R` acts on `polynomial_module R[X] R[X]` - `R[X]` acts on `polynomial_module R[X] R[X]` as `R[X]` acting on `R[X][Y]` - `R[X][X]` acts on `polynomial_module R[X] R[X]` as `R[X][Y]` acting on itself This is also the reason why `R` is included in the alias, or else there will be two different instances of `module R[X] (polynomial_module R[X])`. See https://leanprover.zulipchat.com/#narrow/stream/144837-PR-reviews/topic/.2315065.20polynomial.20modules for the full discussion. -/ @[derive add_comm_group, derive inhabited, nolint unused_arguments] def polynomial_module := ℕ →₀ M omit R variables {M} variables {S : Type*} [comm_semiring S] [algebra S R] [module S M] [is_scalar_tower S R M] namespace polynomial_module /-- This is required to have the `is_scalar_tower S R M` instance to avoid diamonds. -/ @[nolint unused_arguments] noncomputable instance : module S (polynomial_module R M) := finsupp.module ℕ M instance : has_coe_to_fun (polynomial_module R M) (λ _, ℕ → M) := finsupp.has_coe_to_fun /-- The monomial `m * x ^ i`. This is defeq to `finsupp.single_add_hom`, and is redefined here so that it has the desired type signature. -/ noncomputable def single (i : ℕ) : M →+ polynomial_module R M := finsupp.single_add_hom i lemma single_apply (i : ℕ) (m : M) (n : ℕ) : single R i m n = ite (i = n) m 0 := finsupp.single_apply /-- `polynomial_module.single` as a linear map. -/ noncomputable def lsingle (i : ℕ) : M →ₗ[R] polynomial_module R M := finsupp.lsingle i lemma lsingle_apply (i : ℕ) (m : M) (n : ℕ) : lsingle R i m n = ite (i = n) m 0 := finsupp.single_apply lemma single_smul (i : ℕ) (r : R) (m : M) : single R i (r • m) = r • (single R i m) := (lsingle R i).map_smul r m variable {R} lemma induction_linear {P : polynomial_module R M → Prop} (f : polynomial_module R M) (h0 : P 0) (hadd : ∀ f g, P f → P g → P (f + g)) (hsingle : ∀ a b, P (single R a b)) : P f := finsupp.induction_linear f h0 hadd hsingle @[semireducible] noncomputable instance polynomial_module : module R[X] (polynomial_module R M) := module_polynomial_of_endo (finsupp.lmap_domain _ _ nat.succ) instance (M : Type u) [add_comm_group M] [module R M] [module S M] [is_scalar_tower S R M] : is_scalar_tower S R (polynomial_module R M) := finsupp.is_scalar_tower _ _ instance is_scalar_tower' (M : Type u) [add_comm_group M] [module R M] [module S M] [is_scalar_tower S R M] : is_scalar_tower S R[X] (polynomial_module R M) := begin haveI : is_scalar_tower R R[X] (polynomial_module R M) := module_polynomial_of_endo.is_scalar_tower _, constructor, intros x y z, rw [← @is_scalar_tower.algebra_map_smul S R, ← @is_scalar_tower.algebra_map_smul S R, smul_assoc], end @[simp] lemma monomial_smul_single (i : ℕ) (r : R) (j : ℕ) (m : M) : monomial i r • single R j m = single R (i + j) (r • m) := begin simp only [linear_map.mul_apply, polynomial.aeval_monomial, linear_map.pow_apply, module.algebra_map_End_apply, module_polynomial_of_endo_smul_def], induction i generalizing r j m, { simp [single] }, { rw [function.iterate_succ, function.comp_app, nat.succ_eq_add_one, add_assoc, ← i_ih], congr' 2, ext a, dsimp [single], rw [finsupp.map_domain_single, nat.succ_eq_one_add] } end @[simp] lemma monomial_smul_apply (i : ℕ) (r : R) (g : polynomial_module R M) (n : ℕ) : (monomial i r • g) n = ite (i ≤ n) (r • g (n - i)) 0 := begin induction g using polynomial_module.induction_linear with p q hp hq, { simp only [smul_zero, finsupp.zero_apply, if_t_t] }, { simp only [smul_add, finsupp.add_apply, hp, hq], split_ifs, exacts [rfl, zero_add 0] }, { rw [monomial_smul_single, single_apply, single_apply, smul_ite, smul_zero, ← ite_and], congr, rw eq_iff_iff, split, { rintro rfl, simp }, { rintro ⟨e, rfl⟩, rw [add_comm, tsub_add_cancel_of_le e] } } end @[simp] lemma smul_single_apply (i : ℕ) (f : R[X]) (m : M) (n : ℕ) : (f • single R i m) n = ite (i ≤ n) (f.coeff (n - i) • m) 0 := begin induction f using polynomial.induction_on' with p q hp hq, { rw [add_smul, finsupp.add_apply, hp, hq, coeff_add, add_smul], split_ifs, exacts [rfl, zero_add 0] }, { rw [monomial_smul_single, single_apply, coeff_monomial, ite_smul, zero_smul], by_cases h : i ≤ n, { simp_rw [eq_tsub_iff_add_eq_of_le h, if_pos h] }, { rw [if_neg h, ite_eq_right_iff], intro e, exfalso, linarith } } end lemma smul_apply (f : R[X]) (g : polynomial_module R M) (n : ℕ) : (f • g) n = ∑ x in finset.nat.antidiagonal n, f.coeff x.1 • g x.2 := begin induction f using polynomial.induction_on' with p q hp hq, { rw [add_smul, finsupp.add_apply, hp, hq, ← finset.sum_add_distrib], congr', ext, rw [coeff_add, add_smul] }, { rw [finset.nat.sum_antidiagonal_eq_sum_range_succ (λ i j, (monomial f_n f_a).coeff i • g j), monomial_smul_apply], dsimp [monomial], simp_rw [finsupp.single_smul, finsupp.single_apply], rw finset.sum_ite_eq, simp [nat.lt_succ_iff] } end /-- `polynomial_module R R` is isomorphic to `R[X]` as an `R[X]` module. -/ noncomputable def equiv_polynomial_self : polynomial_module R R ≃ₗ[R[X]] R[X] := { map_smul' := λ r x, begin induction r using polynomial.induction_on' with _ _ _ _ n p, { simp only [add_smul, map_add, ring_equiv.to_fun_eq_coe, *] at * }, { ext i, dsimp, rw [monomial_smul_apply, ← polynomial.C_mul_X_pow_eq_monomial, mul_assoc, polynomial.coeff_C_mul, polynomial.coeff_X_pow_mul', mul_ite, mul_zero], simp } end, ..(polynomial.to_finsupp_iso R).symm } /-- `polynomial_module R S` is isomorphic to `S[X]` as an `R` module. -/ noncomputable def equiv_polynomial {S : Type*} [comm_ring S] [algebra R S] : polynomial_module R S ≃ₗ[R] S[X] := { map_smul' := λ r x, rfl, ..(polynomial.to_finsupp_iso S).symm } variables (R' : Type*) {M' : Type*} [comm_ring R'] [add_comm_group M'] [module R' M'] variables [algebra R R'] [module R M'] [is_scalar_tower R R' M'] /-- The image of a polynomial under a linear map. -/ noncomputable def map (f : M →ₗ[R] M') : polynomial_module R M →ₗ[R] polynomial_module R' M' := finsupp.map_range.linear_map f @[simp] lemma map_single (f : M →ₗ[R] M') (i : ℕ) (m : M) : map R' f (single R i m) = single R' i (f m) := finsupp.map_range_single lemma map_smul (f : M →ₗ[R] M') (p : R[X]) (q : polynomial_module R M) : map R' f (p • q) = p.map (algebra_map R R') • map R' f q := begin apply induction_linear q, { rw [smul_zero, map_zero, smul_zero] }, { intros f g e₁ e₂, rw [smul_add, map_add, e₁, e₂, map_add, smul_add] }, intros i m, apply polynomial.induction_on' p, { intros p q e₁ e₂, rw [add_smul, map_add, e₁, e₂, polynomial.map_add, add_smul] }, { intros j s, rw [monomial_smul_single, map_single, polynomial.map_monomial, map_single, monomial_smul_single, f.map_smul, algebra_map_smul] } end /-- Evaulate a polynomial `p : polynomial_module R M` at `r : R`. -/ @[simps (lemmas_only)] def eval (r : R) : polynomial_module R M →ₗ[R] M := { to_fun := λ p, p.sum (λ i m, r ^ i • m), map_add' := λ x y, finsupp.sum_add_index' (λ _, smul_zero _) (λ _ _ _, smul_add _ _ _), map_smul' := λ s m, begin refine (finsupp.sum_smul_index' _).trans _, { exact λ i, smul_zero _ }, { simp_rw [← smul_comm s, ← finsupp.smul_sum], refl } end } @[simp] lemma eval_single (r : R) (i : ℕ) (m : M) : eval r (single R i m) = r ^ i • m := finsupp.sum_single_index (smul_zero _) @[simp] lemma eval_lsingle (r : R) (i : ℕ) (m : M) : eval r (lsingle R i m) = r ^ i • m := eval_single r i m lemma eval_smul (p : R[X]) (q : polynomial_module R M) (r : R) : eval r (p • q) = p.eval r • eval r q := begin apply induction_linear q, { rw [smul_zero, map_zero, smul_zero] }, { intros f g e₁ e₂, rw [smul_add, map_add, e₁, e₂, map_add, smul_add] }, intros i m, apply polynomial.induction_on' p, { intros p q e₁ e₂, rw [add_smul, map_add, polynomial.eval_add, e₁, e₂, add_smul] }, { intros j s, rw [monomial_smul_single, eval_single, polynomial.eval_monomial, eval_single, smul_comm, ← smul_smul, pow_add, mul_smul] } end @[simp] lemma eval_map (f : M →ₗ[R] M') (q : polynomial_module R M) (r : R) : eval (algebra_map R R' r) (map R' f q) = f (eval r q) := begin apply induction_linear q, { simp_rw map_zero }, { intros f g e₁ e₂, simp_rw [map_add, e₁, e₂] }, { intros i m, rw [map_single, eval_single, eval_single, f.map_smul, ← map_pow, algebra_map_smul] } end @[simp] lemma eval_map' (f : M →ₗ[R] M) (q : polynomial_module R M) (r : R) : eval r (map R f q) = f (eval r q) := eval_map R f q r /-- `comp p q` is the composition of `p : R[X]` and `q : M[X]` as `q(p(x))`. -/ @[simps] noncomputable def comp (p : R[X]) : polynomial_module R M →ₗ[R] polynomial_module R M := ((eval p).restrict_scalars R).comp (map R[X] (lsingle R 0)) lemma comp_single (p : R[X]) (i : ℕ) (m : M) : comp p (single R i m) = p ^ i • single R 0 m := begin rw comp_apply, erw [map_single, eval_single], refl end lemma comp_eval (p : R[X]) (q : polynomial_module R M) (r : R) : eval r (comp p q) = eval (p.eval r) q := begin rw ← linear_map.comp_apply, apply induction_linear q, { rw [map_zero, map_zero] }, { intros _ _ e₁ e₂, rw [map_add, map_add, e₁, e₂] }, { intros i m, rw [linear_map.comp_apply, comp_single, eval_single, eval_smul, eval_single, pow_zero, one_smul, polynomial.eval_pow] } end lemma comp_smul (p p' : R[X]) (q : polynomial_module R M) : comp p (p' • q) = p'.comp p • comp p q := begin rw [comp_apply, map_smul, eval_smul, polynomial.comp, polynomial.eval_map, comp_apply], refl end end polynomial_module
965dcc6ce7d485afd00e95532bc5b6e9c404902d
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/test/116/lakefile.lean
373440f71f1fdf09efaef63868cdce6bd1befa2f
[ "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
24
lean
-- improper config file
06a7dc391b9673697539cb0c3bc8a795f19b5907
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/isDefEqOffsetBug.lean
bb0bde7262cdbb4fc5739acb8bcff4a7c532a6ec
[ "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
677
lean
class Zero (α : Type u) where zero : α export Zero (zero) instance [Zero α] : OfNat α (natLit! 0) where ofNat := zero class AddGroup (α : Type u) extends Add α, Zero α, Neg α where addAssoc : {a b c : α} → a + b + c = a + (b + c) zeroAdd : {a : α} → 0 + a = a addZero : {a : α} → a + 0 = a negAdd : {a : α} → -a + a = 0 open AddGroup theorem negZero [AddGroup α] : -(0 : α) = 0 := by rw [←addZero (a := -(0 : α)), negAdd] theorem subZero [AddGroup α] {a : α} : a + -(0 : α) = a := by rw [← addZero (a := a)] rw addAssoc rw negZero rw addZero theorem shouldFail [AddGroup α] : ((0 : α) + 0) = 0 := rfl -- Error
9aa55eb7bc7fc905c8712f68d6be11ba48f2257d
958488bc7f3c2044206e0358e56d7690b6ae696c
/lean/calc.lean
cde4346993c816ad26d9c41a9b0c2eda60e6dd41
[]
no_license
possientis/Prog
a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4
d4b3debc37610a88e0dac3ac5914903604fd1d1f
refs/heads/master
1,692,263,717,723
1,691,757,179,000
1,691,757,179,000
40,361,602
3
0
null
1,679,896,438,000
1,438,953,859,000
Coq
UTF-8
Lean
false
false
1,657
lean
variables (a b c d e : ℕ) variable h1 : a = b variable h2 : b = c + 1 variable h3 : c = d variable h4 : e = 1 + d lemma L1 : a = e := calc a = b : h1 ... = c + 1 : h2 ... = d + 1 : h3 ▸ rfl ... = 1 + d : add_comm _ _ ... = e : eq.symm h4 --#check L1 include h1 h2 h3 h4 lemma L2 : a = e := calc a = b : by rw h1 ... = c + 1 : by rw h2 ... = d + 1 : by rw h3 ... = 1 + d : by rw add_comm ... = e : by rw h4 --#check L2 lemma L3 : a = e := calc a = d + 1 : by rw [h1,h2,h3] ... = 1 + d : by rw add_comm ... = e : by rw h4 --#check L3 lemma L4 : a = e := by rw [h1, h2, h3, add_comm, h4] --#check L4 lemma L5 : a = e := by simp [h1, h2, h3, h4] --#check L5 lemma L6 : ∀ (x y z t : ℕ), x = y → y ≤ z → z + 1 < t → x < t := assume x y z t p q r, show x < t, from calc x = y : p ... < y + 1 : nat.lt_succ_self y ... ≤ z + 1 : nat.succ_le_succ q ... < t : r --#check L6 lemma L7 : ∀ (x y : ℕ), (x + y)*(x + y) = x * x + y * x + x * y + y * y := assume x y, calc (x + y) * (x + y) = (x + y) * x + (x + y) * y : by rw mul_add ... = x * x + y * x + (x + y) * y : by rw add_mul ... = x * x + y * x + (x * y + y * y) : by rw add_mul ... = x * x + y * x + x * y + y * y : by rw ←add_assoc -- \l --#check L7 lemma L8 : ∀ (x y : ℕ), (x + y)*(x + y) = x * x + y * x + x * y + y * y := assume x y, by rw [mul_add, add_mul, add_mul, ←add_assoc] --#check L8 lemma L9 : ∀ (x y : ℕ), (x + y)*(x + y) = x * x + y * x + x * y + y * y := assume x y, by simp [mul_add, add_mul] --#check L9