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
03f4c23f718f33940920f815a482714229263b60
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/complex/schwarz.lean
3a51789fa3216049e58ecec05ae46d63a0ae55e3
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
10,143
lean
/- Copyright (c) 2022 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import analysis.complex.abs_max import analysis.complex.removable_singularity /-! # Schwarz lemma > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we prove several versions of the Schwarz lemma. * `complex.norm_deriv_le_div_of_maps_to_ball`, `complex.abs_deriv_le_div_of_maps_to_ball`: if `f : ℂ → E` sends an open disk with center `c` and a positive radius `R₁` to an open ball with center `f c` and radius `R₂`, then the absolute value of the derivative of `f` at `c` is at most the ratio `R₂ / R₁`; * `complex.dist_le_div_mul_dist_of_maps_to_ball`: if `f : ℂ → E` sends an open disk with center `c` and radius `R₁` to an open disk with center `f c` and radius `R₂`, then for any `z` in the former disk we have `dist (f z) (f c) ≤ (R₂ / R₁) * dist z c`; * `complex.abs_deriv_le_one_of_maps_to_ball`: if `f : ℂ → ℂ` sends an open disk of positive radius to itself and the center of this disk to itself, then the absolute value of the derivative of `f` at the center of this disk is at most `1`; * `complex.dist_le_dist_of_maps_to_ball`: if `f : ℂ → ℂ` sends an open disk to itself and the center `c` of this disk to itself, then for any point `z` of this disk we have `dist (f z) c ≤ dist z c`; * `complex.abs_le_abs_of_maps_to_ball`: if `f : ℂ → ℂ` sends an open disk with center `0` to itself, then for any point `z` of this disk we have `abs (f z) ≤ abs z`. ## Implementation notes We prove some versions of the Schwarz lemma for a map `f : ℂ → E` taking values in any normed space over complex numbers. ## TODO * Prove that these inequalities are strict unless `f` is an affine map. * Prove that any diffeomorphism of the unit disk to itself is a Möbius map. ## Tags Schwarz lemma -/ open metric set function filter topological_space open_locale topology namespace complex section space variables {E : Type*} [normed_add_comm_group E] [normed_space ℂ E] {R R₁ R₂ : ℝ} {f : ℂ → E} {c z z₀ : ℂ} /-- An auxiliary lemma for `complex.norm_dslope_le_div_of_maps_to_ball`. -/ lemma schwarz_aux {f : ℂ → ℂ} (hd : differentiable_on ℂ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) R₂)) (hz : z ∈ ball c R₁) : ‖dslope f c z‖ ≤ R₂ / R₁ := begin have hR₁ : 0 < R₁, from nonempty_ball.1 ⟨z, hz⟩, suffices : ∀ᶠ r in 𝓝[<] R₁, ‖dslope f c z‖ ≤ R₂ / r, { refine ge_of_tendsto _ this, exact (tendsto_const_nhds.div tendsto_id hR₁.ne').mono_left nhds_within_le_nhds }, rw mem_ball at hz, filter_upwards [Ioo_mem_nhds_within_Iio ⟨hz, le_rfl⟩] with r hr, have hr₀ : 0 < r, from dist_nonneg.trans_lt hr.1, replace hd : diff_cont_on_cl ℂ (dslope f c) (ball c r), { refine differentiable_on.diff_cont_on_cl _, rw closure_ball c hr₀.ne', exact ((differentiable_on_dslope $ ball_mem_nhds _ hR₁).mpr hd).mono (closed_ball_subset_ball hr.2) }, refine norm_le_of_forall_mem_frontier_norm_le bounded_ball hd _ _, { rw frontier_ball c hr₀.ne', intros z hz, have hz' : z ≠ c, from ne_of_mem_sphere hz hr₀.ne', rw [dslope_of_ne _ hz', slope_def_module, norm_smul, norm_inv, mem_sphere_iff_norm.1 hz, ← div_eq_inv_mul, div_le_div_right hr₀, ← dist_eq_norm], exact le_of_lt (h_maps (mem_ball.2 (by { rw mem_sphere.1 hz, exact hr.2 }))) }, { rw [closure_ball c hr₀.ne', mem_closed_ball], exact hr.1.le } end /-- Two cases of the **Schwarz Lemma** (derivative and distance), merged together. -/ lemma norm_dslope_le_div_of_maps_to_ball (hd : differentiable_on ℂ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) R₂)) (hz : z ∈ ball c R₁) : ‖dslope f c z‖ ≤ R₂ / R₁ := begin have hR₁ : 0 < R₁, from nonempty_ball.1 ⟨z, hz⟩, have hR₂ : 0 < R₂, from nonempty_ball.1 ⟨f z, h_maps hz⟩, cases eq_or_ne (dslope f c z) 0 with hc hc, { rw [hc, norm_zero], exact div_nonneg hR₂.le hR₁.le }, rcases exists_dual_vector ℂ _ hc with ⟨g, hg, hgf⟩, have hg' : ‖g‖₊ = 1, from nnreal.eq hg, have hg₀ : ‖g‖₊ ≠ 0, by simpa only [hg'] using one_ne_zero, calc ‖dslope f c z‖ = ‖dslope (g ∘ f) c z‖ : begin rw [g.dslope_comp, hgf, is_R_or_C.norm_of_real, abs_norm], exact λ _, hd.differentiable_at (ball_mem_nhds _ hR₁) end ... ≤ R₂ / R₁ : begin refine schwarz_aux (g.differentiable.comp_differentiable_on hd) (maps_to.comp _ h_maps) hz, simpa only [hg', nnreal.coe_one, one_mul] using g.lipschitz.maps_to_ball hg₀ (f c) R₂ end end /-- Equality case in the **Schwarz Lemma**: in the setup of `norm_dslope_le_div_of_maps_to_ball`, if `‖dslope f c z₀‖ = R₂ / R₁` holds at a point in the ball then the map `f` is affine. -/ lemma affine_of_maps_to_ball_of_exists_norm_dslope_eq_div [complete_space E] [strict_convex_space ℝ E] (hd : differentiable_on ℂ f (ball c R₁)) (h_maps : set.maps_to f (ball c R₁) (ball (f c) R₂)) (h_z₀ : z₀ ∈ ball c R₁) (h_eq : ‖dslope f c z₀‖ = R₂ / R₁) : set.eq_on f (λ z, f c + (z - c) • dslope f c z₀) (ball c R₁) := begin set g := dslope f c, rintro z hz, by_cases z = c, { simp [h] }, have h_R₁ : 0 < R₁ := nonempty_ball.mp ⟨_, h_z₀⟩, have g_le_div : ∀ z ∈ ball c R₁, ‖g z‖ ≤ R₂ / R₁, from λ z hz, norm_dslope_le_div_of_maps_to_ball hd h_maps hz, have g_max : is_max_on (norm ∘ g) (ball c R₁) z₀, from is_max_on_iff.mpr (λ z hz, by simpa [h_eq] using g_le_div z hz), have g_diff : differentiable_on ℂ g (ball c R₁), from (differentiable_on_dslope (is_open_ball.mem_nhds (mem_ball_self h_R₁))).mpr hd, have : g z = g z₀ := eq_on_of_is_preconnected_of_is_max_on_norm (convex_ball c R₁).is_preconnected is_open_ball g_diff h_z₀ g_max hz, simp [← this] end lemma affine_of_maps_to_ball_of_exists_norm_dslope_eq_div' [complete_space E] [strict_convex_space ℝ E] (hd : differentiable_on ℂ f (ball c R₁)) (h_maps : set.maps_to f (ball c R₁) (ball (f c) R₂)) (h_z₀ : ∃ z₀ ∈ ball c R₁, ‖dslope f c z₀‖ = R₂ / R₁) : ∃ C : E, ‖C‖ = R₂ / R₁ ∧ set.eq_on f (λ z, f c + (z - c) • C) (ball c R₁) := let ⟨z₀, h_z₀, h_eq⟩ := h_z₀ in ⟨dslope f c z₀, h_eq, affine_of_maps_to_ball_of_exists_norm_dslope_eq_div hd h_maps h_z₀ h_eq⟩ /-- The **Schwarz Lemma**: if `f : ℂ → E` sends an open disk with center `c` and a positive radius `R₁` to an open ball with center `f c` and radius `R₂`, then the absolute value of the derivative of `f` at `c` is at most the ratio `R₂ / R₁`. -/ lemma norm_deriv_le_div_of_maps_to_ball (hd : differentiable_on ℂ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) R₂)) (h₀ : 0 < R₁) : ‖deriv f c‖ ≤ R₂ / R₁ := by simpa only [dslope_same] using norm_dslope_le_div_of_maps_to_ball hd h_maps (mem_ball_self h₀) /-- The **Schwarz Lemma**: if `f : ℂ → E` sends an open disk with center `c` and radius `R₁` to an open ball with center `f c` and radius `R₂`, then for any `z` in the former disk we have `dist (f z) (f c) ≤ (R₂ / R₁) * dist z c`. -/ lemma dist_le_div_mul_dist_of_maps_to_ball (hd : differentiable_on ℂ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) R₂)) (hz : z ∈ ball c R₁) : dist (f z) (f c) ≤ (R₂ / R₁) * dist z c := begin rcases eq_or_ne z c with rfl|hne, { simp only [dist_self, mul_zero] }, simpa only [dslope_of_ne _ hne, slope_def_module, norm_smul, norm_inv, ← div_eq_inv_mul, ← dist_eq_norm, div_le_iff (dist_pos.2 hne)] using norm_dslope_le_div_of_maps_to_ball hd h_maps hz end end space variables {f : ℂ → ℂ} {c z : ℂ} {R R₁ R₂ : ℝ} /-- The **Schwarz Lemma**: if `f : ℂ → ℂ` sends an open disk with center `c` and a positive radius `R₁` to an open disk with center `f c` and radius `R₂`, then the absolute value of the derivative of `f` at `c` is at most the ratio `R₂ / R₁`. -/ lemma abs_deriv_le_div_of_maps_to_ball (hd : differentiable_on ℂ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) R₂)) (h₀ : 0 < R₁) : abs (deriv f c) ≤ R₂ / R₁ := norm_deriv_le_div_of_maps_to_ball hd h_maps h₀ /-- The **Schwarz Lemma**: if `f : ℂ → ℂ` sends an open disk of positive radius to itself and the center of this disk to itself, then the absolute value of the derivative of `f` at the center of this disk is at most `1`. -/ lemma abs_deriv_le_one_of_maps_to_ball (hd : differentiable_on ℂ f (ball c R)) (h_maps : maps_to f (ball c R) (ball c R)) (hc : f c = c) (h₀ : 0 < R) : abs (deriv f c) ≤ 1 := (norm_deriv_le_div_of_maps_to_ball hd (by rwa hc) h₀).trans_eq (div_self h₀.ne') /-- The **Schwarz Lemma**: if `f : ℂ → ℂ` sends an open disk to itself and the center `c` of this disk to itself, then for any point `z` of this disk we have `dist (f z) c ≤ dist z c`. -/ lemma dist_le_dist_of_maps_to_ball_self (hd : differentiable_on ℂ f (ball c R)) (h_maps : maps_to f (ball c R) (ball c R)) (hc : f c = c) (hz : z ∈ ball c R) : dist (f z) c ≤ dist z c := have hR : 0 < R, from nonempty_ball.1 ⟨z, hz⟩, by simpa only [hc, div_self hR.ne', one_mul] using dist_le_div_mul_dist_of_maps_to_ball hd (by rwa hc) hz /-- The **Schwarz Lemma**: if `f : ℂ → ℂ` sends an open disk with center `0` to itself, the for any point `z` of this disk we have `abs (f z) ≤ abs z`. -/ lemma abs_le_abs_of_maps_to_ball_self (hd : differentiable_on ℂ f (ball 0 R)) (h_maps : maps_to f (ball 0 R) (ball 0 R)) (h₀ : f 0 = 0) (hz : abs z < R) : abs (f z) ≤ abs z := begin replace hz : z ∈ ball (0 : ℂ) R, from mem_ball_zero_iff.2 hz, simpa only [dist_zero_right] using dist_le_dist_of_maps_to_ball_self hd h_maps h₀ hz end end complex
0cea8bad2a627ba809301848dac214aed8e4976f
da3a76c514d38801bae19e8a9e496dc31f8e5866
/library/init/algebra/norm_num.lean
2a4c76acfd9ee03adb0f2f7377a327db1dc12083
[ "Apache-2.0" ]
permissive
cipher1024/lean
270c1ac5781e6aee12f5c8d720d267563a164beb
f5cbdff8932dd30c6dd8eec68f3059393b4f8b3a
refs/heads/master
1,611,223,459,029
1,487,566,573,000
1,487,566,573,000
83,356,543
0
0
null
1,488,229,336,000
1,488,229,336,000
null
UTF-8
Lean
false
false
9,120
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis and Leonardo de Moura -/ prelude import init.algebra.field init.algebra.ordered_ring import init.data.nat.lemmas namespace norm_num universe u variable {α : Type u} def add1 [has_add α] [has_one α] (a : α) : α := a + 1 local attribute [reducible] bit0 bit1 add1 local attribute [simp] right_distrib left_distrib private meta def u : tactic unit := `[unfold bit0 bit1 add1] private meta def usimp : tactic unit := u >> `[simp] lemma mul_zero [mul_zero_class α] (a : α) : a * 0 = 0 := by simp lemma zero_mul [mul_zero_class α] (a : α) : 0 * a = 0 := by simp lemma mul_one [monoid α] (a : α) : a * 1 = a := by simp lemma mul_bit0 [distrib α] (a b : α) : a * (bit0 b) = bit0 (a * b) := by simp lemma mul_bit0_helper [distrib α] (a b t : α) (h : a * b = t) : a * (bit0 b) = bit0 t := begin rw [-h], simp end lemma mul_bit1 [semiring α] (a b : α) : a * (bit1 b) = bit0 (a * b) + a := by simp lemma mul_bit1_helper [semiring α] (a b s t : α) (hs : a * b = s) (ht : bit0 s + a = t) : a * (bit1 b) = t := by simp [hs, ht] lemma subst_into_prod [has_mul α] (l r tl tr t : α) (prl : l = tl) (prr : r = tr) (prt : tl * tr = t) : l * r = t := by simp [prl, prr, prt] lemma mk_cong (op : α → α) (a b : α) (h : a = b) : op a = op b := by simp [h] lemma neg_add_neg_eq_of_add_add_eq_zero [add_comm_group α] (a b c : α) (h : c + a + b = 0) : -a + -b = c := begin apply add_neg_eq_of_eq_add, apply neg_eq_of_add_eq_zero, simp at h, simp, assumption end lemma neg_add_neg_helper [add_comm_group α] (a b c : α) (h : a + b = c) : -a + -b = -c := begin apply @neg_inj α, simp [neg_add, neg_neg], assumption end lemma neg_add_pos_eq_of_eq_add [add_comm_group α] (a b c : α) (h : b = c + a) : -a + b = c := begin apply neg_add_eq_of_eq_add, simp at h, assumption end lemma neg_add_pos_helper1 [add_comm_group α] (a b c : α) (h : b + c = a) : -a + b = -c := begin apply neg_add_eq_of_eq_add, apply eq_add_neg_of_add_eq h end lemma neg_add_pos_helper2 [add_comm_group α] (a b c : α) (h : a + c = b) : -a + b = c := begin apply neg_add_eq_of_eq_add, rw h end lemma pos_add_neg_helper [add_comm_group α] (a b c : α) (h : b + a = c) : a + b = c := by rw [-h, add_comm a b] lemma sub_eq_add_neg_helper [add_comm_group α] (t₁ t₂ e w₁ w₂: α) (h₁ : t₁ = w₁) (h₂ : t₂ = w₂) (h₃ : w₁ + -w₂ = e) : t₁ - t₂ = e := by rw [h₁, h₂, sub_eq_add_neg, h₃] lemma pos_add_pos_helper [add_comm_group α] (a b c d₁ d₂ : α) (h₁ : a = d₁) (h₂ : b = d₂) (h₃ : d₁ + d₂ = c) : a + b = c := by rw [h₁, h₂, h₃] lemma subst_into_subtr [add_group α] (l r t : α) (h : l + -r = t) : l - r = t := by simp [h] lemma neg_neg_helper [add_group α] (a b : α) (h : a = -b) : -a = b := by simp [h] lemma neg_mul_neg_helper [ring α] (a b c : α) (h : a * b = c) : (-a) * (-b) = c := by simp [h] lemma neg_mul_pos_helper [ring α] (a b c : α) (h : a * b = c) : (-a) * b = -c := by simp [h] lemma pos_mul_neg_helper [ring α] (a b c : α) (h : a * b = c) : a * (-b) = -c := by simp [h] lemma div_add_helper [field α] (n d b c val : α) (hd : d ≠ 0) (h : n + b * d = val) (h2 : c * d = val) : n / d + b = c := begin apply eq_of_mul_eq_mul_of_nonzero_right hd, rw [h2, -h, right_distrib, div_mul_cancel _ hd] end lemma add_div_helper [field α] (n d b c val : α) (hd : d ≠ 0) (h : d * b + n = val) (h2 : d * c = val) : b + n / d = c := begin apply eq_of_mul_eq_mul_of_nonzero_left hd, rw [h2, -h, left_distrib, mul_div_cancel' _ hd] end lemma div_mul_helper [field α] (n d c v : α) (hd : d ≠ 0) (h : (n * c) / d = v) : (n / d) * c = v := by rw [-h, field.div_mul_eq_mul_div_comm _ _ hd, mul_div_assoc] lemma mul_div_helper [field α] (a n d v : α) (hd : d ≠ 0) (h : (a * n) / d = v) : a * (n / d) = v := by rw [-h, mul_div_assoc] lemma nonzero_of_div_helper [field α] (a b : α) (ha : a ≠ 0) (hb : b ≠ 0) : a / b ≠ 0 := begin intro hab, assert habb : (a / b) * b = 0, rw [hab, zero_mul], rw [div_mul_cancel _ hb] at habb, exact ha habb end lemma div_helper [field α] (n d v : α) (hd : d ≠ 0) (h : v * d = n) : n / d = v := begin apply eq_of_mul_eq_mul_of_nonzero_right hd, rw (div_mul_cancel _ hd), exact eq.symm h end lemma div_eq_div_helper [field α] (a b c d v : α) (h1 : a * d = v) (h2 : c * b = v) (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d := begin apply eq_div_of_mul_eq, exact hd, rw div_mul_eq_mul_div, apply eq.symm, apply eq_div_of_mul_eq, exact hb, rw [h1, h2] end lemma subst_into_div [has_div α] (a₁ b₁ a₂ b₂ v : α) (h : a₁ / b₁ = v) (h1 : a₂ = a₁) (h2 : b₂ = b₁) : a₂ / b₂ = v := by rw [h1, h2, h] lemma add_comm_four [add_comm_semigroup α] (a b : α) : a + a + (b + b) = (a + b) + (a + b) := by simp lemma add_comm_middle [add_comm_semigroup α] (a b c : α) : a + b + c = a + c + b := by simp lemma bit0_add_bit0 [add_comm_semigroup α] (a b : α) : bit0 a + bit0 b = bit0 (a + b) := by usimp lemma bit0_add_bit0_helper [add_comm_semigroup α] (a b t : α) (h : a + b = t) : bit0 a + bit0 b = bit0 t := begin rw -h, usimp end lemma bit1_add_bit0 [add_comm_semigroup α] [has_one α] (a b : α) : bit1 a + bit0 b = bit1 (a + b) := by usimp lemma bit1_add_bit0_helper [add_comm_semigroup α] [has_one α] (a b t : α) (h : a + b = t) : bit1 a + bit0 b = bit1 t := begin rw -h, usimp end lemma bit0_add_bit1 [add_comm_semigroup α] [has_one α] (a b : α) : bit0 a + bit1 b = bit1 (a + b) := by usimp lemma bit0_add_bit1_helper [add_comm_semigroup α] [has_one α] (a b t : α) (h : a + b = t) : bit0 a + bit1 b = bit1 t := begin rw -h, usimp end lemma bit1_add_bit1 [add_comm_semigroup α] [has_one α] (a b : α) : bit1 a + bit1 b = bit0 (add1 (a + b)) := by usimp lemma bit1_add_bit1_helper [add_comm_semigroup α] [has_one α] (a b t s : α) (h : (a + b) = t) (h2 : add1 t = s) : bit1 a + bit1 b = bit0 s := begin rw -h at h2, rw -h2, usimp end lemma bin_add_zero [add_monoid α] (a : α) : a + zero = a := by simp lemma bin_zero_add [add_monoid α] (a : α) : zero + a = a := by simp lemma one_add_bit0 [add_comm_semigroup α] [has_one α] (a : α) : one + bit0 a = bit1 a := begin unfold bit0 bit1, simp end lemma bit0_add_one [has_add α] [has_one α] (a : α) : bit0 a + one = bit1 a := rfl lemma bit1_add_one [has_add α] [has_one α] (a : α) : bit1 a + one = add1 (bit1 a) := rfl lemma bit1_add_one_helper [has_add α] [has_one α] (a t : α) (h : add1 (bit1 a) = t) : bit1 a + one = t := by rw -h lemma one_add_bit1 [add_comm_semigroup α] [has_one α] (a : α) : one + bit1 a = add1 (bit1 a) := begin unfold bit0 bit1 add1, simp end lemma one_add_bit1_helper [add_comm_semigroup α] [has_one α] (a t : α) (h : add1 (bit1 a) = t) : one + bit1 a = t := begin rw -h, usimp end lemma add1_bit0 [has_add α] [has_one α] (a : α) : add1 (bit0 a) = bit1 a := rfl lemma add1_bit1 [add_comm_semigroup α] [has_one α] (a : α) : add1 (bit1 a) = bit0 (add1 a) := by usimp lemma add1_bit1_helper [add_comm_semigroup α] [has_one α] (a t : α) (h : add1 a = t) : add1 (bit1 a) = bit0 t := begin rw -h, usimp end lemma add1_one [has_add α] [has_one α] : add1 (one : α) = bit0 one := rfl lemma add1_zero [add_monoid α] [has_one α] : add1 (zero : α) = one := by usimp lemma one_add_one [has_add α] [has_one α] : (one : α) + one = bit0 one := rfl lemma subst_into_sum [has_add α] (l r tl tr t : α) (prl : l = tl) (prr : r = tr) (prt : tl + tr = t) : l + r = t := by rw [-prt, prr, prl] lemma neg_zero_helper [add_group α] (a : α) (h : a = 0) : - a = 0 := begin rw h, simp end lemma pos_bit0_helper [linear_ordered_semiring α] (a : α) (h : a > 0) : bit0 a > 0 := begin u, apply add_pos h h end lemma nonneg_bit0_helper [linear_ordered_semiring α] (a : α) (h : a ≥ 0) : bit0 a ≥ 0 := begin u, apply add_nonneg h h end lemma pos_bit1_helper [linear_ordered_semiring α] (a : α) (h : a ≥ 0) : bit1 a > 0 := begin u, apply add_pos_of_nonneg_of_pos, apply nonneg_bit0_helper _ h, apply zero_lt_one end lemma nonneg_bit1_helper [linear_ordered_semiring α] (a : α) (h : a ≥ 0) : bit1 a ≥ 0 := begin apply le_of_lt, apply pos_bit1_helper _ h end lemma nonzero_of_pos_helper [linear_ordered_semiring α] (a : α) (h : a > 0) : a ≠ 0 := ne_of_gt h lemma nonzero_of_neg_helper [linear_ordered_ring α] (a : α) (h : a ≠ 0) : -a ≠ 0 := begin intro ha, apply h, apply neg_inj, rwa neg_zero end lemma sub_nat_zero_helper {a b c d: ℕ} (hac : a = c) (hbd : b = d) (hcd : c < d) : a - b = 0 := begin simp_using_hs, apply nat.sub_eq_zero_of_le, apply le_of_lt, assumption end lemma sub_nat_pos_helper {a b c d e : ℕ} (hac : a = c) (hbd : b = d) (hced : e + d = c) : a - b = e := begin simp_using_hs, rw [-hced, nat.add_sub_cancel] end end norm_num
f4f189e3f125eed50a6b86f17d5624b3a3f57aa4
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/module/prod_auto.lean
60d96e5f48a06e526de8d62d3d6aaef4e277d563
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,491
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.module.basic import Mathlib.PostPort universes u_1 u_2 u_3 namespace Mathlib /-! # Prod instances for module and multiplicative actions This file defines instances for binary product of modules -/ namespace prod protected instance has_scalar {α : Type u_1} {β : Type u_2} {γ : Type u_3} [has_scalar α β] [has_scalar α γ] : has_scalar α (β × γ) := has_scalar.mk fun (a : α) (p : β × γ) => (a • fst p, a • snd p) @[simp] theorem smul_fst {α : Type u_1} {β : Type u_2} {γ : Type u_3} [has_scalar α β] [has_scalar α γ] (a : α) (x : β × γ) : fst (a • x) = a • fst x := rfl @[simp] theorem smul_snd {α : Type u_1} {β : Type u_2} {γ : Type u_3} [has_scalar α β] [has_scalar α γ] (a : α) (x : β × γ) : snd (a • x) = a • snd x := rfl @[simp] theorem smul_mk {α : Type u_1} {β : Type u_2} {γ : Type u_3} [has_scalar α β] [has_scalar α γ] (a : α) (b : β) (c : γ) : a • (b, c) = (a • b, a • c) := rfl protected instance semimodule {α : Type u_1} {β : Type u_2} {γ : Type u_3} {r : semiring α} [add_comm_monoid β] [add_comm_monoid γ] [semimodule α β] [semimodule α γ] : semimodule α (β × γ) := semimodule.mk sorry sorry end Mathlib
55d330db53be14218adb324b69014dba1ff74ed7
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/order_iso_nat.lean
649a669d91279f8a01db2c337d13f369bb0b7fa3
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,109
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.nat.basic import Mathlib.order.rel_iso import Mathlib.logic.function.iterate import Mathlib.PostPort universes u_1 namespace Mathlib namespace rel_embedding /-- If `f` is a strictly `r`-increasing sequence, then this returns `f` as an order embedding. -/ def nat_lt {α : Type u_1} {r : α → α → Prop} [is_strict_order α r] (f : ℕ → α) (H : ∀ (n : ℕ), r (f n) (f (n + 1))) : Less ↪r r := of_monotone f sorry /-- If `f` is a strictly `r`-decreasing sequence, then this returns `f` as an order embedding. -/ def nat_gt {α : Type u_1} {r : α → α → Prop} [is_strict_order α r] (f : ℕ → α) (H : ∀ (n : ℕ), r (f (n + 1)) (f n)) : gt ↪r r := rel_embedding.swap (nat_lt f H) theorem well_founded_iff_no_descending_seq {α : Type u_1} {r : α → α → Prop} [is_strict_order α r] : well_founded r ↔ ¬Nonempty (gt ↪r r) := sorry
890303383b34ef74de583cd4c5fc14ddfea84c69
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/data/ulift.lean
33cf6c63f3e1707e5b3f65a5f57c7ed161dccc7c
[ "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
637
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison Transport through constant families. -/ universes u₁ u₂ @[simp] lemma plift.rec.constant {α : Sort u₁} {β : Sort u₂} (b : β) : @plift.rec α (λ _, β) (λ _, b) = λ _, b := funext (λ x, plift.cases_on x (λ a, eq.refl (plift.rec (λ a', b) {down := a}))) @[simp] lemma ulift.rec.constant {α : Type u₁} {β : Sort u₂} (b : β) : @ulift.rec α (λ _, β) (λ _, b) = λ _, b := funext (λ x, ulift.cases_on x (λ a, eq.refl (ulift.rec (λ a', b) {down := a})))
8cb975de60695efc33e149d034ec65346b2240c4
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/linear_algebra/pi.lean
25d76e8b7ca48e419441dc04339bbf3cc4a747ca
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,145
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, Yury Kudryashov, Eric Wieser -/ import linear_algebra.basic /-! # Pi types of modules This file defines constructors for linear maps whose domains or codomains are pi types. It contains theorems relating these to each other, as well as to `linear_map.ker`. ## Main definitions - pi types in the codomain: - `linear_map.pi` - `linear_map.single` - pi types in the domain: - `linear_map.proj` - `linear_map.diag` -/ universes u v w x y z u' v' w' x' 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} {ι' : Type x'} open function submodule open_locale big_operators namespace linear_map universe i variables [semiring R] [add_comm_monoid M₂] [module R M₂] [add_comm_monoid M₃] [module R M₃] {φ : ι → Type i} [∀i, add_comm_monoid (φ 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) := { to_fun := λ c i, f i c, map_add' := λ c d, funext $ λ i, (f i).map_add _ _, map_smul' := λ c d, funext $ λ i, (f i).map_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. Note: known here as `linear_map.proj`, this construction is in other categories called `eval`, for example `pi.eval_monoid_hom`, `pi.eval_ring_hom`. -/ def proj (i : ι) : (Πi, φ i) →ₗ[R] φ i := { to_fun := function.eval i, map_add' := λ f g, rfl, map_smul' := λ c f, rfl } @[simp] lemma coe_proj (i : ι) : ⇑(proj i : (Πi, φ i) →ₗ[R] φ i) = function.eval i := rfl 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 $ set_like.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 /-- Linear map between the function spaces `I → M₂` and `I → M₃`, induced by a linear map `f` between `M₂` and `M₃`. -/ @[simps] protected def comp_left (f : M₂ →ₗ[R] M₃) (I : Type*) : (I → M₂) →ₗ[R] (I → M₃) := { to_fun := λ h, f ∘ h, map_smul' := λ c h, by { ext x, exact f.map_smul' c (h x) }, .. f.to_add_monoid_hom.comp_left I } lemma apply_single [add_comm_monoid M] [module R M] [decidable_eq ι] (f : Π i, φ i →ₗ[R] M) (i j : ι) (x : φ i) : f j (pi.single i x j) = pi.single i (f i x) j := pi.apply_single (λ i, f i) (λ i, (f i).map_zero) _ _ _ /-- The `linear_map` version of `add_monoid_hom.single` and `pi.single`. -/ def single [decidable_eq ι] (i : ι) : φ i →ₗ[R] (Πi, φ i) := { to_fun := pi.single i, map_smul' := pi.single_smul i, .. add_monoid_hom.single φ i} @[simp] lemma coe_single [decidable_eq ι] (i : ι) : ⇑(single i : φ i →ₗ[R] (Π i, φ i)) = pi.single i := rfl variables (R φ) /-- The linear equivalence between linear functions on a finite product of modules and families of functions on these modules. See note [bundled maps over different rings]. -/ @[simps] def lsum (S) [add_comm_monoid M] [module R M] [fintype ι] [decidable_eq ι] [semiring S] [module S M] [smul_comm_class R S M] : (Π i, φ i →ₗ[R] M) ≃ₗ[S] ((Π i, φ i) →ₗ[R] M) := { to_fun := λ f, ∑ i : ι, (f i).comp (proj i), inv_fun := λ f i, f.comp (single i), map_add' := λ f g, by simp only [pi.add_apply, add_comp, finset.sum_add_distrib], map_smul' := λ c f, by simp only [pi.smul_apply, smul_comp, finset.smul_sum, ring_hom.id_apply], left_inv := λ f, by { ext i x, simp [apply_single] }, right_inv := λ f, begin ext, suffices : f (∑ j, pi.single j (x j)) = f x, by simpa [apply_single], rw finset.univ_sum_single end } variables {R φ} section ext variables [fintype ι] [decidable_eq ι] [add_comm_monoid M] [module R M] {f g : (Π i, φ i) →ₗ[R] M} lemma pi_ext (h : ∀ i x, f (pi.single i x) = g (pi.single i x)) : f = g := to_add_monoid_hom_injective $ add_monoid_hom.functions_ext _ _ _ h lemma pi_ext_iff : f = g ↔ ∀ i x, f (pi.single i x) = g (pi.single i x) := ⟨λ h i x, h ▸ rfl, pi_ext⟩ /-- This is used as the ext lemma instead of `linear_map.pi_ext` for reasons explained in note [partially-applied ext lemmas]. -/ @[ext] lemma pi_ext' (h : ∀ i, f.comp (single i) = g.comp (single i)) : f = g := begin refine pi_ext (λ i x, _), convert linear_map.congr_fun (h i) x end lemma pi_ext'_iff : f = g ↔ ∀ i, f.comp (single i) = g.comp (single i) := ⟨λ h i, h ▸ rfl, pi_ext'⟩ end ext 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, subtype.coe_prop], ext b ⟨j, hj⟩, simp only [dif_pos, function.comp_app, function.eval_apply, linear_map.cod_restrict_apply, linear_map.coe_comp, linear_map.coe_proj, linear_map.pi_apply, submodule.subtype_apply, subtype.coe_prop], refl }, { ext1 ⟨b, hb⟩, apply subtype.ext, 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 end linear_map namespace submodule variables [semiring R] {φ : ι → Type*} [∀ i, add_comm_monoid (φ i)] [∀ i, module R (φ i)] open linear_map /-- A version of `set.pi` for submodules. Given an index set `I` and a family of submodules `p : Π i, submodule R (φ i)`, `pi I s` is the submodule of dependent functions `f : Π i, φ i` such that `f i` belongs to `p a` whenever `i ∈ I`. -/ def pi (I : set ι) (p : Π i, submodule R (φ i)) : submodule R (Π i, φ i) := { carrier := set.pi I (λ i, p i), zero_mem' := λ i hi, (p i).zero_mem, add_mem' := λ x y hx hy i hi, (p i).add_mem (hx i hi) (hy i hi), smul_mem' := λ c x hx i hi, (p i).smul_mem c (hx i hi) } variables {I : set ι} {p : Π i, submodule R (φ i)} {x : Π i, φ i} @[simp] lemma mem_pi : x ∈ pi I p ↔ ∀ i ∈ I, x i ∈ p i := iff.rfl @[simp, norm_cast] lemma coe_pi : (pi I p : set (Π i, φ i)) = set.pi I (λ i, p i) := rfl lemma binfi_comap_proj : (⨅ i ∈ I, comap (proj i) (p i)) = pi I p := by { ext x, simp } lemma infi_comap_proj : (⨅ i, comap (proj i) (p i)) = pi set.univ p := by { ext x, simp } lemma supr_map_single [decidable_eq ι] [fintype ι] : (⨆ i, map (linear_map.single i) (p i)) = pi set.univ p := begin refine (supr_le $ λ i, _).antisymm _, { rintro _ ⟨x, hx : x ∈ p i, rfl⟩ j -, rcases em (j = i) with rfl|hj; simp * }, { intros x hx, rw [← finset.univ_sum_single x], exact sum_mem_supr (λ i, mem_map_of_mem (hx i trivial)) } end end submodule namespace linear_equiv variables [semiring R] {φ ψ χ : ι → Type*} [∀ i, add_comm_monoid (φ i)] [∀ i, module R (φ i)] variables [∀ i, add_comm_monoid (ψ i)] [∀ i, module R (ψ i)] variables [∀ i, add_comm_monoid (χ i)] [∀ i, module R (χ i)] /-- Combine a family of linear equivalences into a linear equivalence of `pi`-types. This is `equiv.Pi_congr_right` as a `linear_equiv` -/ @[simps apply] def Pi_congr_right (e : Π i, φ i ≃ₗ[R] ψ i) : (Π i, φ i) ≃ₗ[R] (Π i, ψ i) := { to_fun := λ f i, e i (f i), inv_fun := λ f i, (e i).symm (f i), map_smul' := λ c f, by { ext, simp }, .. add_equiv.Pi_congr_right (λ j, (e j).to_add_equiv) } @[simp] lemma Pi_congr_right_refl : Pi_congr_right (λ j, refl R (φ j)) = refl _ _ := rfl @[simp] lemma Pi_congr_right_symm (e : Π i, φ i ≃ₗ[R] ψ i) : (Pi_congr_right e).symm = (Pi_congr_right $ λ i, (e i).symm) := rfl @[simp] lemma Pi_congr_right_trans (e : Π i, φ i ≃ₗ[R] ψ i) (f : Π i, ψ i ≃ₗ[R] χ i) : (Pi_congr_right e).trans (Pi_congr_right f) = (Pi_congr_right $ λ i, (e i).trans (f i)) := rfl variables (R φ) /-- Transport dependent functions through an equivalence of the base space. This is `equiv.Pi_congr_left'` as a `linear_equiv`. -/ @[simps {simp_rhs := tt}] def Pi_congr_left' (e : ι ≃ ι') : (Π i', φ i') ≃ₗ[R] (Π i, φ $ e.symm i) := { map_add' := λ x y, rfl, map_smul' := λ x y, rfl, .. equiv.Pi_congr_left' φ e } /-- Transporting dependent functions through an equivalence of the base, expressed as a "simplification". This is `equiv.Pi_congr_left` as a `linear_equiv` -/ def Pi_congr_left (e : ι' ≃ ι) : (Π i', φ (e i')) ≃ₗ[R] (Π i, φ i) := (Pi_congr_left' R φ e.symm).symm /-- This is `equiv.pi_option_equiv_prod` as a `linear_equiv` -/ def pi_option_equiv_prod {ι : Type*} {M : option ι → Type*} [Π i, add_comm_group (M i)] [Π i, module R (M i)] : (Π i : option ι, M i) ≃ₗ[R] (M none × Π i : ι, M (some i)) := { map_add' := by simp [function.funext_iff], map_smul' := by simp [function.funext_iff], ..equiv.pi_option_equiv_prod } variables (ι R M) (S : Type*) [fintype ι] [decidable_eq ι] [semiring S] [add_comm_monoid M] [module R M] [module S M] [smul_comm_class R S M] /-- Linear equivalence between linear functions `Rⁿ → M` and `Mⁿ`. The spaces `Rⁿ` and `Mⁿ` are represented as `ι → R` and `ι → M`, respectively, where `ι` is a finite type. This as an `S`-linear equivalence, under the assumption that `S` acts on `M` commuting with `R`. When `R` is commutative, we can take this to be the usual action with `S = R`. Otherwise, `S = ℕ` shows that the equivalence is additive. See note [bundled maps over different rings]. -/ def pi_ring : ((ι → R) →ₗ[R] M) ≃ₗ[S] (ι → M) := (linear_map.lsum R (λ i : ι, R) S).symm.trans (Pi_congr_right $ λ i, linear_map.ring_lmap_equiv_self R M S) variables {ι R M} @[simp] lemma pi_ring_apply (f : (ι → R) →ₗ[R] M) (i : ι) : pi_ring R M ι S f i = f (pi.single i 1) := rfl @[simp] lemma pi_ring_symm_apply (f : ι → M) (g : ι → R) : (pi_ring R M ι S).symm f g = ∑ i, g i • f i := by simp [pi_ring, linear_map.lsum] /-- `equiv.sum_arrow_equiv_prod_arrow` as a linear equivalence. -/ -- TODO additive version? def sum_arrow_lequiv_prod_arrow (α β R M : Type*) [semiring R] [add_comm_monoid M] [module R M] : ((α ⊕ β) → M) ≃ₗ[R] (α → M) × (β → M) := { map_add' := by { intros f g, ext; refl }, map_smul' := by { intros r f, ext; refl, }, .. equiv.sum_arrow_equiv_prod_arrow α β M, } @[simp] lemma sum_arrow_lequiv_prod_arrow_apply_fst {α β} (f : (α ⊕ β) → M) (a : α) : (sum_arrow_lequiv_prod_arrow α β R M f).1 a = f (sum.inl a) := rfl @[simp] lemma sum_arrow_lequiv_prod_arrow_apply_snd {α β} (f : (α ⊕ β) → M) (b : β) : (sum_arrow_lequiv_prod_arrow α β R M f).2 b = f (sum.inr b) := rfl @[simp] lemma sum_arrow_lequiv_prod_arrow_symm_apply_inl {α β} (f : α → M) (g : β → M) (a : α) : ((sum_arrow_lequiv_prod_arrow α β R M).symm (f, g)) (sum.inl a) = f a := rfl @[simp] lemma sum_arrow_lequiv_prod_arrow_symm_apply_inr {α β} (f : α → M) (g : β → M) (b : β) : ((sum_arrow_lequiv_prod_arrow α β R M).symm (f, g)) (sum.inr b) = g b := rfl /-- If `ι` has a unique element, then `ι → M` is linearly equivalent to `M`. -/ @[simps { simp_rhs := tt, fully_applied := ff }] def fun_unique (ι R M : Type*) [unique ι] [semiring R] [add_comm_monoid M] [module R M] : (ι → M) ≃ₗ[R] M := { map_add' := λ f g, rfl, map_smul' := λ c f, rfl, .. equiv.fun_unique ι M } end linear_equiv section extend variables (R) {η : Type x} [semiring R] (s : ι → η) /-- `function.extend s f 0` as a bundled linear map. -/ @[simps] noncomputable def function.extend_by_zero.linear_map : (ι → R) →ₗ[R] (η → R) := { to_fun := λ f, function.extend s f 0, map_smul' := λ r f, by { simpa using function.extend_smul r s f 0 }, ..function.extend_by_zero.hom R s } end extend
8a50a1008039e4819a2cefece6890137bb4eadd4
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/logic/unnamed_1718.lean
905131e375d8bced924708149356c7d49be5ca75
[]
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
227
lean
import data.real.basic -- BEGIN theorem aux {x y : ℝ} (h : x^2 + y^2 = 0) : x = 0 := begin have h' : x^2 = 0, { sorry }, exact pow_eq_zero h' end example (x y : ℝ) : x^2 + y^2 = 0 ↔ x = 0 ∧ y = 0 := sorry -- END
dd2153f62bbb52f4771f0a7085ee4fc7d6b2c346
b794ca1df49bc5a3bd3fd5552eed3bc4f63b8b93
/src/mywork/lecture_10.lean
de36b489f66bfb62a57f13b83b4b218ec81d3380
[]
no_license
akraisinger/cs2120f21
8235ac98375e04ffcec504cff5cab7833ee69e54
4ef83d7151bb6a284028092aa4f1d509c0eb8237
refs/heads/main
1,691,714,771,612
1,632,889,465,000
1,632,889,465,000
399,946,508
0
0
null
null
null
null
UTF-8
Lean
false
false
10,118
lean
/- In today's class, we'll continue with our exploration of the proposition, "false", its elimination rule, and their vital uses in logical reasoning: especially in - proof of ¬P by negation - proof of P by false elimination Here are the inference rules in display notation: NEGATION INTRODUCTION The first, proof by negation, says that from a proof of P → false you can derive a proof of ¬P. Indeed! It's definitional. Recall: def not (P : Prop) := P → false. (P : Prop) (np : P → false) --------------------------- [by defn'n] (pf : ¬P) This rule is the foundation for "proof by negation." To prove ¬P you first assume P, is true, then you show that in this context you can derive a contradiction. What you have then shown, of course, is P → false. So, to prove ¬P, assume P and show that in this context there is a contradiction. This is proof by negation. It is not to be confused with proof by contradition, which is a different thing entirely. (Proof by contradiction. You can use this approach to a proposition, P, by assuming ¬P and showing that this assumption leads to a contradiction. That proves ¬¬P. Then you use the *indepedent* axiom of negation elimination to infer P from ¬¬P.) FALSE ELIMINATION The second rule says that if you have a proof of false and any other proposition, P, the logic is useless and so you might as well just admit that P is true. Why is the logic useless? Well, if false is true then there's no longer a difference! In this situation, tHere's sense in reasoning any further, and you can just dismiss it. A contradiction makes a logic inconsistent. (P : Prop) (f : false) ---------------------- (false_elim f) (pf : P) -/ /- We covered the relatively simpler notion of negation introduction last time, so today we will start by focusing on false elimination. Understanding why it's not magic and in fact makes perfect sense takes a bit of thought. -/ /- To make sense of false elimination, think in terms of case analysis. Proof by case analysis says that if the truth of some proposition, Y, follows from *any possible form of proof* of X, then you've proved X → Y. If X is a disjunction, P ∨ Q (so now you want to prove P ∨ Q → Y) you must consider two cases: one where P is assumed true and one where Q is assumed true. If X is a proposition with just one form of proof (e.g., the proposition, true), there's just one case to consider. -/ -- two cases example : true ∨ false → true := begin assume h, cases h, assumption, -- context has exact proof contradiction, -- context has contradiction end -- one case example : true → true := begin assume t, cases t, -- just one case exact true.intro, end /- How many cases must you consider if you putatively have a putative proof of false? ZERO! To consider *all* cases you need consider exactly zero cases. Proving all cases when the number of cases is zero is trivial, so the conclusion *follows*. -/ example : false → false := begin assume f, cases f, -- case analysis: there are no cases! end /- In fact, it doesn't matter what your conclusion is: it will always be true in a context in which you have a proof of false. And this makes sense, because if you have a proof of false, then false is true, so whether a given proposition is true or false, it's true, because even if it's false, well, false is true, so it's also true! -/ theorem false_elim : ∀ (P : Prop), false → P := begin assume P, assume f, cases f, end -- Ex false (falsum? lmao idk latin bro), quod libet -- false implies anything (out of false is anything) -- ex falso (short for the whole thingy) /- This, then, is the general principle for false elimination: ANYTHING FOLLOWS FROM FALSE. This principle gives you a powerful way to prove any proposition is true (conditional on your having a proof that can't exist). The theorem states that if you're given any proposition, P, and a proof, f, of false, then in that context, you can return a proof of P by using false elimination. The only problem with this super-power is that in reality, there is no proof of false, so there's no real danger of any old proposition being automatically true! The rule of false elimination tells you that if you're in a situation that can't happen, then no worries, be happy, you're done (just use false elimination). -/ /- The elimination principle for false is called false.elim in Lean. If you are given or can derive a proof, f, of false, then all you have to do to finish your proof is to say, "this is situation can't happen, so we need not consider it any further." Or, formally, (false.elim f). -/ -- Suppose P is *any* (an arbitrary) proposition axiom P : Prop example : false → P := begin assume f, exact false.elim f, -- Using Lean's version -- P is implicit argument end -- coolio mcschoolio /- No propsition cannot be both true and false. Prop: ?????? big bruh moment asdfghjkl For any propositiong of P, it is impossible to have a proof of both P and not P. -/ /- SOME THEOREMS INVOLVING FALSE AND NEGATION -/ theorem no_contradiction : ∀ (P : Prop), ¬(P ∧ ¬P) := begin assume P, -- ¬(P ∧ ¬P) just means P ∧ ¬P → false, so assume again assume h, have p := and.elim_left h, have notp := and.elim_right h, have f := notp p, -- applying not p to gives a proof of false? -- not p says that P → false, and then applying p to notp to get false. exact f, end -- if you have a proof of a negation, you have a function that something -- implies false. /- The so-called "law" (it's really an axiom) of the excluded middle says that any proposition is either true or false. There's no middle ground. But in the constructive logic of Lean, this isn't true. To prove P ∨ ¬P, as you recall, we need to have either a proof of P (in this case use or.intro_left) or a proof of ¬P, in which case we use or.intro_right to prove P ∨ ¬P. But what if we don't have a proof either way? There are many important questions in computer science and mathematics where we don't know either way. If you call one of those propositions, P, and try to prove P ∨ ¬P in Lean, you just get stuck. -/ theorem excluded_middle' : ∀ (P : Prop), (P ∨ ¬P) := begin assume P, -- we don't have a proof of either P or of ¬P! end /- Goldbach's conjecture: every even number greater than 2 is the sum of 2 prime numbers. (havent solved for EVERY number) Can't solve for all? Some are conjectures? So? Not everything is either true or false? bruhhhhh -/ axiom excluded_middle : ∀ (P: Prop), (P ∨ ¬P) -- for any proposition P, there is a proof that either P is true or not P is true -- this axiom must be added, because it is technically not true. /- Let P be the conjecture, "every even whole number greater than 2 is the sum of two prime numbers." This conjecture, dating (in a slightly different form) to a letter from Goldback to Euler in 1742 is still neither proved nor disproved! Below you will find a placeholder for a proof. Just fill it in (correctly) and you will win $1M and probably a Fields Medal (the "Nobel Prize" in mathematics). -/ axioms (ev : ℕ → Prop) (prime : ℕ → Prop) def goldbach_conjecture := ∀ (n : ℕ), n > 2 → (∃ h k : ℕ, n = h + k ∧ prime h ∧ prime k) theorem goldbach_conjecture_true : goldbach_conjecture := _ theorem goldbach_conjecture_false : ¬goldbach_conjecture := _ example : goldbach_conjecture ∨ ¬goldbach_conjecture := _ /- Our only options are or.intro_left or or.intro_right, but we don't have a required argument (proof) to use either of them! -/ /- The axioms of the constructive logic of Lean are not strong enough to prove the "law of the excluded middle." Rather, if we want to use it, we have to accept it as an additional axiom. We thus have two different logics: one without and one with the law of the excluded middle! -/ axiom excluded_middle : ∀ (P : Prop), (P ∨ ¬P) /- Now, for any proposition whatsoever, P, we can always prove P ∨ ¬P by applying excluded middle to P (using the elimination rule for ∀). What we get in return is a proof of P ∨ ¬P for whatever proposition P is. Here is an example where the proposition is ItsRaining. -/ axiom ItsRaining : Prop theorem example_em : ItsRaining ∨ ¬ItsRaining := begin apply excluded_middle ItsRaining, end /- PROOF BY NEGATION, EXAMPLE -/ /- Next we return to the negation connective, which in logic is pronounced "not." Given any proposition, P, ¬P is also a proposition; and what it means is, exactly, P → false. If P is true, then false is true, and false cannot possibly be true, so P must not be. Thus, to prove ¬P you prove P → false. Another way to read P → false is that, if we assume that P is true, we can derive a proof of false, which cannot exist, so P must not be true. MEMORIZE THIS REASONING. Again, to prove ¬P, *assume P and show that in that context, you can derive a contradiction in the form of a proof of false. This is the strategy call proof by contradiction. -/ /- How about we prove ¬(0 = 1) in English. Proof. By negation. Assume 0 = 1 and show that this leads to a contradiction. That is, show 0 = 1 → false. The rest of the proof is by case analysis on the assumed proof of 0 = 1. The only way to have a proof of equality is by the reflexive property of =. But the reflexive property doesn't imply that there's a proof of 0 = 1. So there can be no proof of 0 = 1, and the assumption that 0 = 1 is a contradiction. We finish the proof by case analysis on the possible proofs of 0 = 1, of which there are zero. So in all (zero!) cases, the conclusion (false) follows. Therefore 0 = 1 → false, which is to say, ¬(0 = 1) is proved and thus true. QED. -/ example : ¬0 = 1 := begin show 0 = 1 → false, -- rewrite goal to a definitionally equal goal assume h, cases h, -- there are *zero* ways to build a proof of 0 = 1 -- case analysis discharges our "proof obligation" end
7d4f3d7a5e35e9bf5b11707027ef40fdb1283313
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/stage0/src/Lean/ParserCompiler.lean
2404605ba778577b8495d57a0ed4a9e1ebd29c31
[ "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
7,385
lean
/- Copyright (c) 2020 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ /-! Gadgets for compiling parser declarations into other programs, such as pretty printers. -/ import Lean.Util.ReplaceExpr import Lean.Meta.Basic import Lean.Meta.WHNF import Lean.ParserCompiler.Attribute import Lean.Parser.Extension namespace Lean namespace ParserCompiler structure Context (α : Type) where varName : Name categoryAttr : KeyedDeclsAttribute α combinatorAttr : CombinatorAttribute def Context.tyName {α} (ctx : Context α) : Name := ctx.categoryAttr.defn.valueTypeName -- replace all references of `Parser` with `tyName` def replaceParserTy {α} (ctx : Context α) (e : Expr) : Expr := e.replace fun e => -- strip `optParam` let e := if e.isOptParam then e.appFn!.appArg! else e if e.isConstOf `Lean.Parser.Parser then mkConst ctx.tyName else none section open Meta variable {α} (ctx : Context α) (force : Bool := false) in /-- Translate an expression of type `Parser` into one of type `tyName`, tagging intermediary constants with `ctx.combinatorAttr`. If `force` is `false`, refuse to do so for imported constants. -/ partial def compileParserExpr (e : Expr) : MetaM Expr := do let e ← whnfCore e match e with | e@(Expr.lam _ _ _ _) => lambdaLetTelescope e fun xs b => compileParserExpr b >>= mkLambdaFVars xs | e@(Expr.fvar _ _) => pure e | _ => do let fn := e.getAppFn let Expr.const c _ _ ← pure fn | throwError! "call of unknown parser at '{e}'" let args := e.getAppArgs -- call the translated `p` with (a prefix of) the arguments of `e`, recursing for arguments -- of type `ty` (i.e. formerly `Parser`) let mkCall (p : Name) := do let ty ← inferType (mkConst p) forallTelescope ty fun params _ => do let mut p := mkConst p let args := e.getAppArgs for i in [:Nat.min params.size args.size] do let param := params[i] let arg := args[i] let paramTy ← inferType param let resultTy ← forallTelescope paramTy fun _ b => pure b let arg ← if resultTy.isConstOf ctx.tyName then compileParserExpr arg else pure arg p := mkApp p arg pure p let env ← getEnv match ctx.combinatorAttr.getDeclFor? env c with | some p => mkCall p | none => let c' := c ++ ctx.varName let cinfo ← getConstInfo c let resultTy ← forallTelescope cinfo.type fun _ b => pure b if resultTy.isConstOf `Lean.Parser.TrailingParser || resultTy.isConstOf `Lean.Parser.Parser then do -- synthesize a new `[combinatorAttr c]` let some value ← pure cinfo.value? | throwError! "don't know how to generate {ctx.varName} for non-definition '{e}'" unless (env.getModuleIdxFor? c).isNone || force do throwError! "refusing to generate code for imported parser declaration '{c}'; use `@[runParserAttributeHooks]` on its definition instead." let value ← compileParserExpr $ replaceParserTy ctx value let ty ← forallTelescope cinfo.type fun params _ => params.foldrM (init := mkConst ctx.tyName) fun param ty => do let paramTy ← replaceParserTy ctx <$> inferType param pure $ mkForall `_ BinderInfo.default paramTy ty let decl := Declaration.defnDecl { name := c', levelParams := [], type := ty, value := value, hints := ReducibilityHints.opaque, safety := DefinitionSafety.safe } let env ← getEnv let env ← match env.addAndCompile {} decl with | Except.ok env => pure env | Except.error kex => do throwError (← (kex.toMessageData {}).toString) setEnv $ ctx.combinatorAttr.setDeclFor env c c' mkCall c' else -- if this is a generic function, e.g. `AndThen.andthen`, it's easier to just unfold it until we are -- back to parser combinators let some e' ← unfoldDefinition? e | throwError! "don't know how to generate {ctx.varName} for non-parser combinator '{e}'" compileParserExpr e' end open Core /-- Compile the given declaration into a `[(builtin)categoryAttr declName]` -/ def compileCategoryParser {α} (ctx : Context α) (declName : Name) (builtin : Bool) : AttrM Unit := do -- This will also tag the declaration as a `[combinatorParenthesizer declName]` in case the parser is used by other parsers. -- Note that simply having `[(builtin)Parenthesizer]` imply `[combinatorParenthesizer]` is not ideal since builtin -- attributes are active only in the next stage, while `[combinatorParenthesizer]` is active immediately (since we never -- call them at compile time but only reference them). let (Expr.const c' _ _) ← (compileParserExpr ctx (mkConst declName) (force := false)).run' | unreachable! -- We assume that for tagged parsers, the kind is equal to the declaration name. This is automatically true for parsers -- using `parser!` or `syntax`. let kind := declName let attrName := if builtin then ctx.categoryAttr.defn.builtinName else ctx.categoryAttr.defn.name -- Create syntax node for a simple attribute of the form -- `def simple := parser! ident >> optional (ident <|> priorityParser)` let stx := Syntax.node `Lean.Parser.Attr.simple #[ mkIdent attrName, mkNullNode #[mkIdent kind] ] Attribute.add c' attrName stx variable {α} (ctx : Context α) in def compileEmbeddedParsers : ParserDescr → MetaM Unit | ParserDescr.const _ => pure () | ParserDescr.unary _ d => compileEmbeddedParsers d | ParserDescr.binary _ d₁ d₂ => compileEmbeddedParsers d₁ *> compileEmbeddedParsers d₂ | ParserDescr.parser constName => discard $ compileParserExpr ctx (mkConst constName) (force := false) | ParserDescr.node _ _ d => compileEmbeddedParsers d | ParserDescr.nodeWithAntiquot _ _ d => compileEmbeddedParsers d | ParserDescr.sepBy p _ psep _ => compileEmbeddedParsers p *> compileEmbeddedParsers psep | ParserDescr.sepBy1 p _ psep _ => compileEmbeddedParsers p *> compileEmbeddedParsers psep | ParserDescr.trailingNode _ _ d => compileEmbeddedParsers d | ParserDescr.symbol _ => pure () | ParserDescr.nonReservedSymbol _ _ => pure () | ParserDescr.cat _ _ => pure () /-- Precondition: `α` must match `ctx.tyName`. -/ unsafe def registerParserCompiler {α} (ctx : Context α) : IO Unit := do Parser.registerParserAttributeHook { postAdd := fun catName constName builtin => do let info ← getConstInfo constName if info.type.isConstOf `Lean.ParserDescr || info.type.isConstOf `Lean.TrailingParserDescr then let d ← evalConstCheck ParserDescr `Lean.ParserDescr constName <|> evalConstCheck TrailingParserDescr `Lean.TrailingParserDescr constName compileEmbeddedParsers ctx d |>.run' else if catName.isAnonymous then -- `[runBuiltinParserAttributeHooks]` => force compilation even if imported, do not apply `ctx.categoryAttr`. discard (compileParserExpr ctx (mkConst constName) (force := true)).run' else compileCategoryParser ctx constName builtin } end ParserCompiler end Lean
8162e21af983f974995ea5f0ce212bf2cd4bf34c
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/monad/kleisli.lean
0f3bff8e8f12b658138b55ccf2103d55bbac8200
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
5,640
lean
/- Copyright (c) 2020 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki, Bhavik Mehta -/ import category_theory.adjunction.basic import category_theory.monad.basic /-! # Kleisli category on a (co)monad > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the Kleisli category on a monad `(T, η_ T, μ_ T)` as well as the co-Kleisli category on a comonad `(U, ε_ U, δ_ U)`. It also defines the Kleisli adjunction which gives rise to the monad `(T, η_ T, μ_ T)` as well as the co-Kleisli adjunction which gives rise to the comonad `(U, ε_ U, δ_ U)`. ## References * [Riehl, *Category theory in context*, Definition 5.2.9][riehl2017] -/ namespace category_theory universes v u -- morphism levels before object levels. See note [category_theory universes]. variables {C : Type u} [category.{v} C] /-- The objects for the Kleisli category of the monad `T : monad C`, which are the same thing as objects of the base category `C`. -/ @[nolint unused_arguments] def kleisli (T : monad C) := C namespace kleisli variables (T : monad C) instance [inhabited C] (T : monad C) : inhabited (kleisli T) := ⟨(default : C)⟩ /-- The Kleisli category on a monad `T`. cf Definition 5.2.9 in [Riehl][riehl2017]. -/ instance kleisli.category : category (kleisli T) := { hom := λ (X Y : C), X ⟶ (T : C ⥤ C).obj Y, id := λ X, T.η.app X, comp := λ X Y Z f g, f ≫ (T : C ⥤ C).map g ≫ T.μ.app Z, id_comp' := λ X Y f, begin rw [← T.η.naturality_assoc f, T.left_unit], apply category.comp_id, end, assoc' := λ W X Y Z f g h, begin simp only [functor.map_comp, category.assoc, monad.assoc], erw T.μ.naturality_assoc, end } namespace adjunction /-- The left adjoint of the adjunction which induces the monad `(T, η_ T, μ_ T)`. -/ @[simps] def to_kleisli : C ⥤ kleisli T := { obj := λ X, (X : kleisli T), map := λ X Y f, (f ≫ T.η.app Y : _), map_comp' := λ X Y Z f g, by { unfold_projs, simp [← T.η.naturality g] } } /-- The right adjoint of the adjunction which induces the monad `(T, η_ T, μ_ T)`. -/ @[simps] def from_kleisli : kleisli T ⥤ C := { obj := λ X, T.obj X, map := λ X Y f, T.map f ≫ T.μ.app Y, map_id' := λ X, T.right_unit _, map_comp' := λ X Y Z f g, begin unfold_projs, simp only [functor.map_comp, category.assoc], erw [← T.μ.naturality_assoc g, T.assoc], refl, end } /-- The Kleisli adjunction which gives rise to the monad `(T, η_ T, μ_ T)`. cf Lemma 5.2.11 of [Riehl][riehl2017]. -/ def adj : to_kleisli T ⊣ from_kleisli T := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, equiv.refl (X ⟶ T.obj Y), hom_equiv_naturality_left_symm' := λ X Y Z f g, begin unfold_projs, dsimp, rw [category.assoc, ← T.η.naturality_assoc g, functor.id_map], dsimp, simp [monad.left_unit], end } /-- The composition of the adjunction gives the original functor. -/ def to_kleisli_comp_from_kleisli_iso_self : to_kleisli T ⋙ from_kleisli T ≅ T := nat_iso.of_components (λ X, iso.refl _) (λ X Y f, by { dsimp, simp }) end adjunction end kleisli /-- The objects for the co-Kleisli category of the comonad `U : monad C`, which are the same thing as objects of the base category `C`. -/ @[nolint unused_arguments] def cokleisli (U : comonad C) := C namespace cokleisli variables (U : comonad C) instance [inhabited C] (U : comonad C) : inhabited (cokleisli U) := ⟨(default : C)⟩ /-- The co-Kleisli category on a comonad `U`.-/ instance cokleisli.category : category (cokleisli U) := { hom := λ (X Y : C), (U : C ⥤ C).obj X ⟶ Y, id := λ X, U.ε.app X, comp := λ X Y Z f g, U.δ.app X ≫ (U : C ⥤ C).map f ≫ g, id_comp' := λ X Y f, by rw U.right_counit_assoc, assoc' := λ W X Y Z f g h, begin unfold_projs, simp only [functor.map_comp, ← category.assoc, U.δ.naturality_assoc, functor.comp_map, U.coassoc] end } namespace adjunction /-- The right adjoint of the adjunction which induces the comonad `(U, ε_ U, δ_ U)`. -/ @[simps] def to_cokleisli : C ⥤ cokleisli U := { obj := λ X, (X : cokleisli U), map := λ X Y f, (U.ε.app X ≫ f : _), map_comp' := λ X Y Z f g, by { unfold_projs, simp [← U.ε.naturality g] } } /-- The left adjoint of the adjunction which induces the comonad `(U, ε_ U, δ_ U)`. -/ @[simps] def from_cokleisli : cokleisli U ⥤ C := { obj := λ X, U.obj X, map := λ X Y f, U.δ.app X ≫ U.map f, map_id' := λ X, U.right_counit _, map_comp' := λ X Y Z f g, begin unfold_projs, dsimp, simp only [functor.map_comp, ← category.assoc], rw comonad.coassoc, simp only [category.assoc, nat_trans.naturality, functor.comp_map], end } /-- The co-Kleisli adjunction which gives rise to the monad `(U, ε_ U, δ_ U)`. -/ def adj : from_cokleisli U ⊣ to_cokleisli U := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, equiv.refl (U.obj X ⟶ Y), hom_equiv_naturality_right' := λ X Y Z f g, begin unfold_projs, dsimp, erw [← category.assoc (U.map f), U.ε.naturality], dsimp, simp only [← category.assoc, comonad.left_counit, category.id_comp] end } /-- The composition of the adjunction gives the original functor. -/ def to_cokleisli_comp_from_cokleisli_iso_self : to_cokleisli U ⋙ from_cokleisli U ≅ U := nat_iso.of_components (λ X, iso.refl _) (λ X Y f, by { dsimp, simp }) end adjunction end cokleisli end category_theory
db11b83221586d8428719dadfcbb8172df3999c5
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/logic/examples/double_negation_translation.lean
487df207360e89dc104fa4b5e724bb2b313ef2f7
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
3,560
lean
/- Simulating classical reasoning without assuming excluded middle. The idea is to use the double-negation translation. We define several "helper" theorems for double negated formulas. -/ variables {p q r : Prop} theorem not_and_of_or_not : ¬p ∨ ¬q → ¬(p ∧ q) := λ h hpq, or.elim h (λ hnp : ¬p, absurd (and.elim_left hpq) hnp) (λ hnq : ¬q, absurd (and.elim_right hpq) hnq) theorem not_or_elim_left : ¬(p ∨ q) → ¬p := λ hpq hp, absurd (or.inl hp) hpq theorem not_or_elim_right : ¬(p ∨ q) → ¬q := λ hpq hq, absurd (or.inr hq) hpq theorem not_imp_elim_right : ¬(p → q) → ¬q := λ h₁ hq, absurd (λ h, hq) h₁ theorem not_imp_elim_left : ¬(p → q) → ¬¬p := λ h₁ hnp, absurd (λ hp, by contradiction) h₁ theorem not_imp_intro : ¬¬p → ¬q → ¬(p → q) := λ hnnp hnq hpq, have hnp : ¬ p, from λ hp, absurd (hpq hp) hnq, absurd hnp hnnp /- Double negation introduction -/ theorem nn_intro : p → ¬¬p := λ hp hnp, absurd hp hnp /- Double negated implication -/ -- Introduction theorem nn_imp_intro : (¬¬p → ¬¬q) → ¬¬(p → q) := λ h hnpq, have hnnp : ¬¬p, from not_imp_elim_left hnpq, have hnq : ¬q, from not_imp_elim_right hnpq, have hnnq : ¬¬q, from h hnnp, absurd hnq hnnq -- Elimination (modus ponens) theorem nn_mp : ¬¬(p → q) → p → ¬¬q := λ hpq hp hnq, have aux : ¬(p → q), from not_imp_intro (nn_intro hp) hnq, absurd aux hpq -- Double negated modus tollens theorem nn_mt : ¬¬(p → q) → ¬q → ¬p := λ hpq hnq hp, absurd hnq (nn_mp hpq hp) /- Double negated disjuction -/ lemma not_or_of_not_of_not : ¬p → ¬q → ¬(p ∨ q) := λ hnp hnq hpq, or.elim hpq (λ hp, absurd hp hnp) (λ hq, absurd hq hnq) -- Elimination theorem nn_or_elim : ¬¬(p ∨ q) → (p → ¬¬r) → (q → ¬¬r) → ¬¬r := λ hpq hpr hqr hnr, have hnp : ¬p, from λhp, absurd hnr (hpr hp), have hnq : ¬q, from λhq, absurd hnr (hqr hq), have aux : ¬(p ∨ q), from not_or_of_not_of_not hnp hnq, absurd aux hpq -- Introduction theorem nn_or_inl : ¬¬p → ¬¬(p ∨ q) := λ h hnpq, absurd (not_or_elim_left hnpq) h theorem nn_or_inr : ¬¬q → ¬¬(p ∨ q) := λ h hnpq, absurd (not_or_elim_right hnpq) h /- Double negated conjunction -/ -- Elimination theorem nn_and_elim_left : ¬¬(p ∧ q) → ¬¬p := λ h hnp, absurd (not_and_of_or_not (or.inl hnp)) h theorem nn_and_elim_right : ¬¬(p ∧ q) → ¬¬q := λ h hnq, absurd (not_and_of_or_not (or.inr hnq)) h -- Introduction theorem nn_and_intro : ¬¬p → ¬¬q → ¬¬(p ∧ q) := λ hnnp hnnq hnpq, have h₁ : ¬(p → ¬q), from not_imp_intro hnnp hnnq, have h₂ : p → ¬q, from λ hp hq, absurd (and.intro hp hq) hnpq, absurd h₂ h₁ /- Double negated excluded middle -/ theorem nn_em : ¬¬(p ∨ ¬p) := λ hn, have hnp : ¬p, from not_or_elim_left hn, have hnnp : ¬¬p, from not_or_elim_right hn, absurd hnp hnnp /- Examples: the following two examples are classically valid. We can "simulate" the classical proofs using double negation. -/ example : ¬¬((p → q) → (¬p ∨ q)) := nn_imp_intro (λ h, nn_or_elim (@nn_em p) (λ hp : p, have hnnq : ¬¬q, from nn_mp h hp, nn_or_inr hnnq) (λ hnp : ¬p, nn_intro (or.inl hnp))) /- "Prove" Peirce's law -/ example : ¬¬(((p → q) → p) → p) := nn_imp_intro (λ h, nn_or_elim (@nn_em p) (λ hp : p, nn_intro hp) (λ hnp : ¬p, have h₁ : ¬(p → q), from nn_mt h hnp, have hnnp : ¬¬p, from not_imp_elim_left h₁, absurd hnp hnnp))
2f5a6ef8ee00525ef5457f885d3dec56ceb5ac92
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/BoundedLattice.lean
ba9e0632abf9382fc3b3ad3c2314ca302959960c
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
13,086
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section BoundedLattice structure BoundedLattice (A : Type) : Type := (times : (A → (A → A))) (plus : (A → (A → A))) (commutative_times : (∀ {x y : A} , (times x y) = (times y x))) (associative_times : (∀ {x y z : A} , (times (times x y) z) = (times x (times y z)))) (idempotent_times : (∀ {x : A} , (times x x) = x)) (commutative_plus : (∀ {x y : A} , (plus x y) = (plus y x))) (associative_plus : (∀ {x y z : A} , (plus (plus x y) z) = (plus x (plus y z)))) (idempotent_plus : (∀ {x : A} , (plus x x) = x)) (leftAbsorp_times_plus : (∀ {x y : A} , (times x (plus x y)) = x)) (leftAbsorp_plus_times : (∀ {x y : A} , (plus x (times x y)) = x)) (zero : A) (lunit_zero : (∀ {x : A} , (plus zero x) = x)) (runit_zero : (∀ {x : A} , (plus x zero) = x)) (one : A) (lunit_one : (∀ {x : A} , (times one x) = x)) (runit_one : (∀ {x : A} , (times x one) = x)) open BoundedLattice structure Sig (AS : Type) : Type := (timesS : (AS → (AS → AS))) (plusS : (AS → (AS → AS))) (zeroS : AS) (oneS : AS) structure Product (A : Type) : Type := (timesP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (plusP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (zeroP : (Prod A A)) (oneP : (Prod A A)) (commutative_timesP : (∀ {xP yP : (Prod A A)} , (timesP xP yP) = (timesP yP xP))) (associative_timesP : (∀ {xP yP zP : (Prod A A)} , (timesP (timesP xP yP) zP) = (timesP xP (timesP yP zP)))) (idempotent_timesP : (∀ {xP : (Prod A A)} , (timesP xP xP) = xP)) (commutative_plusP : (∀ {xP yP : (Prod A A)} , (plusP xP yP) = (plusP yP xP))) (associative_plusP : (∀ {xP yP zP : (Prod A A)} , (plusP (plusP xP yP) zP) = (plusP xP (plusP yP zP)))) (idempotent_plusP : (∀ {xP : (Prod A A)} , (plusP xP xP) = xP)) (leftAbsorp_times_plusP : (∀ {xP yP : (Prod A A)} , (timesP xP (plusP xP yP)) = xP)) (leftAbsorp_plus_timesP : (∀ {xP yP : (Prod A A)} , (plusP xP (timesP xP yP)) = xP)) (lunit_0P : (∀ {xP : (Prod A A)} , (plusP zeroP xP) = xP)) (runit_0P : (∀ {xP : (Prod A A)} , (plusP xP zeroP) = xP)) (lunit_1P : (∀ {xP : (Prod A A)} , (timesP oneP xP) = xP)) (runit_1P : (∀ {xP : (Prod A A)} , (timesP xP oneP) = xP)) structure Hom {A1 : Type} {A2 : Type} (Bo1 : (BoundedLattice A1)) (Bo2 : (BoundedLattice A2)) : Type := (hom : (A1 → A2)) (pres_times : (∀ {x1 x2 : A1} , (hom ((times Bo1) x1 x2)) = ((times Bo2) (hom x1) (hom x2)))) (pres_plus : (∀ {x1 x2 : A1} , (hom ((plus Bo1) x1 x2)) = ((plus Bo2) (hom x1) (hom x2)))) (pres_zero : (hom (zero Bo1)) = (zero Bo2)) (pres_one : (hom (one Bo1)) = (one Bo2)) structure RelInterp {A1 : Type} {A2 : Type} (Bo1 : (BoundedLattice A1)) (Bo2 : (BoundedLattice A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_times : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((times Bo1) x1 x2) ((times Bo2) y1 y2)))))) (interp_plus : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((plus Bo1) x1 x2) ((plus Bo2) y1 y2)))))) (interp_zero : (interp (zero Bo1) (zero Bo2))) (interp_one : (interp (one Bo1) (one Bo2))) inductive BoundedLatticeTerm : Type | timesL : (BoundedLatticeTerm → (BoundedLatticeTerm → BoundedLatticeTerm)) | plusL : (BoundedLatticeTerm → (BoundedLatticeTerm → BoundedLatticeTerm)) | zeroL : BoundedLatticeTerm | oneL : BoundedLatticeTerm open BoundedLatticeTerm inductive ClBoundedLatticeTerm (A : Type) : Type | sing : (A → ClBoundedLatticeTerm) | timesCl : (ClBoundedLatticeTerm → (ClBoundedLatticeTerm → ClBoundedLatticeTerm)) | plusCl : (ClBoundedLatticeTerm → (ClBoundedLatticeTerm → ClBoundedLatticeTerm)) | zeroCl : ClBoundedLatticeTerm | oneCl : ClBoundedLatticeTerm open ClBoundedLatticeTerm inductive OpBoundedLatticeTerm (n : ℕ) : Type | v : ((fin n) → OpBoundedLatticeTerm) | timesOL : (OpBoundedLatticeTerm → (OpBoundedLatticeTerm → OpBoundedLatticeTerm)) | plusOL : (OpBoundedLatticeTerm → (OpBoundedLatticeTerm → OpBoundedLatticeTerm)) | zeroOL : OpBoundedLatticeTerm | oneOL : OpBoundedLatticeTerm open OpBoundedLatticeTerm inductive OpBoundedLatticeTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpBoundedLatticeTerm2) | sing2 : (A → OpBoundedLatticeTerm2) | timesOL2 : (OpBoundedLatticeTerm2 → (OpBoundedLatticeTerm2 → OpBoundedLatticeTerm2)) | plusOL2 : (OpBoundedLatticeTerm2 → (OpBoundedLatticeTerm2 → OpBoundedLatticeTerm2)) | zeroOL2 : OpBoundedLatticeTerm2 | oneOL2 : OpBoundedLatticeTerm2 open OpBoundedLatticeTerm2 def simplifyCl {A : Type} : ((ClBoundedLatticeTerm A) → (ClBoundedLatticeTerm A)) | (plusCl zeroCl x) := x | (plusCl x zeroCl) := x | (timesCl oneCl x) := x | (timesCl x oneCl) := x | (timesCl x1 x2) := (timesCl (simplifyCl x1) (simplifyCl x2)) | (plusCl x1 x2) := (plusCl (simplifyCl x1) (simplifyCl x2)) | zeroCl := zeroCl | oneCl := oneCl | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpBoundedLatticeTerm n) → (OpBoundedLatticeTerm n)) | (plusOL zeroOL x) := x | (plusOL x zeroOL) := x | (timesOL oneOL x) := x | (timesOL x oneOL) := x | (timesOL x1 x2) := (timesOL (simplifyOpB x1) (simplifyOpB x2)) | (plusOL x1 x2) := (plusOL (simplifyOpB x1) (simplifyOpB x2)) | zeroOL := zeroOL | oneOL := oneOL | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpBoundedLatticeTerm2 n A) → (OpBoundedLatticeTerm2 n A)) | (plusOL2 zeroOL2 x) := x | (plusOL2 x zeroOL2) := x | (timesOL2 oneOL2 x) := x | (timesOL2 x oneOL2) := x | (timesOL2 x1 x2) := (timesOL2 (simplifyOp x1) (simplifyOp x2)) | (plusOL2 x1 x2) := (plusOL2 (simplifyOp x1) (simplifyOp x2)) | zeroOL2 := zeroOL2 | oneOL2 := oneOL2 | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((BoundedLattice A) → (BoundedLatticeTerm → A)) | Bo (timesL x1 x2) := ((times Bo) (evalB Bo x1) (evalB Bo x2)) | Bo (plusL x1 x2) := ((plus Bo) (evalB Bo x1) (evalB Bo x2)) | Bo zeroL := (zero Bo) | Bo oneL := (one Bo) def evalCl {A : Type} : ((BoundedLattice A) → ((ClBoundedLatticeTerm A) → A)) | Bo (sing x1) := x1 | Bo (timesCl x1 x2) := ((times Bo) (evalCl Bo x1) (evalCl Bo x2)) | Bo (plusCl x1 x2) := ((plus Bo) (evalCl Bo x1) (evalCl Bo x2)) | Bo zeroCl := (zero Bo) | Bo oneCl := (one Bo) def evalOpB {A : Type} {n : ℕ} : ((BoundedLattice A) → ((vector A n) → ((OpBoundedLatticeTerm n) → A))) | Bo vars (v x1) := (nth vars x1) | Bo vars (timesOL x1 x2) := ((times Bo) (evalOpB Bo vars x1) (evalOpB Bo vars x2)) | Bo vars (plusOL x1 x2) := ((plus Bo) (evalOpB Bo vars x1) (evalOpB Bo vars x2)) | Bo vars zeroOL := (zero Bo) | Bo vars oneOL := (one Bo) def evalOp {A : Type} {n : ℕ} : ((BoundedLattice A) → ((vector A n) → ((OpBoundedLatticeTerm2 n A) → A))) | Bo vars (v2 x1) := (nth vars x1) | Bo vars (sing2 x1) := x1 | Bo vars (timesOL2 x1 x2) := ((times Bo) (evalOp Bo vars x1) (evalOp Bo vars x2)) | Bo vars (plusOL2 x1 x2) := ((plus Bo) (evalOp Bo vars x1) (evalOp Bo vars x2)) | Bo vars zeroOL2 := (zero Bo) | Bo vars oneOL2 := (one Bo) def inductionB {P : (BoundedLatticeTerm → Type)} : ((∀ (x1 x2 : BoundedLatticeTerm) , ((P x1) → ((P x2) → (P (timesL x1 x2))))) → ((∀ (x1 x2 : BoundedLatticeTerm) , ((P x1) → ((P x2) → (P (plusL x1 x2))))) → ((P zeroL) → ((P oneL) → (∀ (x : BoundedLatticeTerm) , (P x)))))) | ptimesl pplusl p0l p1l (timesL x1 x2) := (ptimesl _ _ (inductionB ptimesl pplusl p0l p1l x1) (inductionB ptimesl pplusl p0l p1l x2)) | ptimesl pplusl p0l p1l (plusL x1 x2) := (pplusl _ _ (inductionB ptimesl pplusl p0l p1l x1) (inductionB ptimesl pplusl p0l p1l x2)) | ptimesl pplusl p0l p1l zeroL := p0l | ptimesl pplusl p0l p1l oneL := p1l def inductionCl {A : Type} {P : ((ClBoundedLatticeTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClBoundedLatticeTerm A)) , ((P x1) → ((P x2) → (P (timesCl x1 x2))))) → ((∀ (x1 x2 : (ClBoundedLatticeTerm A)) , ((P x1) → ((P x2) → (P (plusCl x1 x2))))) → ((P zeroCl) → ((P oneCl) → (∀ (x : (ClBoundedLatticeTerm A)) , (P x))))))) | psing ptimescl ppluscl p0cl p1cl (sing x1) := (psing x1) | psing ptimescl ppluscl p0cl p1cl (timesCl x1 x2) := (ptimescl _ _ (inductionCl psing ptimescl ppluscl p0cl p1cl x1) (inductionCl psing ptimescl ppluscl p0cl p1cl x2)) | psing ptimescl ppluscl p0cl p1cl (plusCl x1 x2) := (ppluscl _ _ (inductionCl psing ptimescl ppluscl p0cl p1cl x1) (inductionCl psing ptimescl ppluscl p0cl p1cl x2)) | psing ptimescl ppluscl p0cl p1cl zeroCl := p0cl | psing ptimescl ppluscl p0cl p1cl oneCl := p1cl def inductionOpB {n : ℕ} {P : ((OpBoundedLatticeTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpBoundedLatticeTerm n)) , ((P x1) → ((P x2) → (P (timesOL x1 x2))))) → ((∀ (x1 x2 : (OpBoundedLatticeTerm n)) , ((P x1) → ((P x2) → (P (plusOL x1 x2))))) → ((P zeroOL) → ((P oneOL) → (∀ (x : (OpBoundedLatticeTerm n)) , (P x))))))) | pv ptimesol pplusol p0ol p1ol (v x1) := (pv x1) | pv ptimesol pplusol p0ol p1ol (timesOL x1 x2) := (ptimesol _ _ (inductionOpB pv ptimesol pplusol p0ol p1ol x1) (inductionOpB pv ptimesol pplusol p0ol p1ol x2)) | pv ptimesol pplusol p0ol p1ol (plusOL x1 x2) := (pplusol _ _ (inductionOpB pv ptimesol pplusol p0ol p1ol x1) (inductionOpB pv ptimesol pplusol p0ol p1ol x2)) | pv ptimesol pplusol p0ol p1ol zeroOL := p0ol | pv ptimesol pplusol p0ol p1ol oneOL := p1ol def inductionOp {n : ℕ} {A : Type} {P : ((OpBoundedLatticeTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpBoundedLatticeTerm2 n A)) , ((P x1) → ((P x2) → (P (timesOL2 x1 x2))))) → ((∀ (x1 x2 : (OpBoundedLatticeTerm2 n A)) , ((P x1) → ((P x2) → (P (plusOL2 x1 x2))))) → ((P zeroOL2) → ((P oneOL2) → (∀ (x : (OpBoundedLatticeTerm2 n A)) , (P x)))))))) | pv2 psing2 ptimesol2 pplusol2 p0ol2 p1ol2 (v2 x1) := (pv2 x1) | pv2 psing2 ptimesol2 pplusol2 p0ol2 p1ol2 (sing2 x1) := (psing2 x1) | pv2 psing2 ptimesol2 pplusol2 p0ol2 p1ol2 (timesOL2 x1 x2) := (ptimesol2 _ _ (inductionOp pv2 psing2 ptimesol2 pplusol2 p0ol2 p1ol2 x1) (inductionOp pv2 psing2 ptimesol2 pplusol2 p0ol2 p1ol2 x2)) | pv2 psing2 ptimesol2 pplusol2 p0ol2 p1ol2 (plusOL2 x1 x2) := (pplusol2 _ _ (inductionOp pv2 psing2 ptimesol2 pplusol2 p0ol2 p1ol2 x1) (inductionOp pv2 psing2 ptimesol2 pplusol2 p0ol2 p1ol2 x2)) | pv2 psing2 ptimesol2 pplusol2 p0ol2 p1ol2 zeroOL2 := p0ol2 | pv2 psing2 ptimesol2 pplusol2 p0ol2 p1ol2 oneOL2 := p1ol2 def stageB : (BoundedLatticeTerm → (Staged BoundedLatticeTerm)) | (timesL x1 x2) := (stage2 timesL (codeLift2 timesL) (stageB x1) (stageB x2)) | (plusL x1 x2) := (stage2 plusL (codeLift2 plusL) (stageB x1) (stageB x2)) | zeroL := (Now zeroL) | oneL := (Now oneL) def stageCl {A : Type} : ((ClBoundedLatticeTerm A) → (Staged (ClBoundedLatticeTerm A))) | (sing x1) := (Now (sing x1)) | (timesCl x1 x2) := (stage2 timesCl (codeLift2 timesCl) (stageCl x1) (stageCl x2)) | (plusCl x1 x2) := (stage2 plusCl (codeLift2 plusCl) (stageCl x1) (stageCl x2)) | zeroCl := (Now zeroCl) | oneCl := (Now oneCl) def stageOpB {n : ℕ} : ((OpBoundedLatticeTerm n) → (Staged (OpBoundedLatticeTerm n))) | (v x1) := (const (code (v x1))) | (timesOL x1 x2) := (stage2 timesOL (codeLift2 timesOL) (stageOpB x1) (stageOpB x2)) | (plusOL x1 x2) := (stage2 plusOL (codeLift2 plusOL) (stageOpB x1) (stageOpB x2)) | zeroOL := (Now zeroOL) | oneOL := (Now oneOL) def stageOp {n : ℕ} {A : Type} : ((OpBoundedLatticeTerm2 n A) → (Staged (OpBoundedLatticeTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | (timesOL2 x1 x2) := (stage2 timesOL2 (codeLift2 timesOL2) (stageOp x1) (stageOp x2)) | (plusOL2 x1 x2) := (stage2 plusOL2 (codeLift2 plusOL2) (stageOp x1) (stageOp x2)) | zeroOL2 := (Now zeroOL2) | oneOL2 := (Now oneOL2) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (timesT : ((Repr A) → ((Repr A) → (Repr A)))) (plusT : ((Repr A) → ((Repr A) → (Repr A)))) (zeroT : (Repr A)) (oneT : (Repr A)) end BoundedLattice
7743a41cc4297a845c6a47e65053558b052449b2
ac2987d8c7832fb4a87edb6bee26141facbb6fa0
/Mathlib/Util/Export.lean
17abea3ccfd8fdef6c273b5986f09190fedfca9d
[ "Apache-2.0" ]
permissive
AurelienSaue/mathlib4
52204b9bd9d207c922fe0cf3397166728bb6c2e2
84271fe0875bafdaa88ac41f1b5a7c18151bd0d5
refs/heads/master
1,689,156,096,545
1,629,378,840,000
1,629,378,840,000
389,648,603
0
0
Apache-2.0
1,627,307,284,000
1,627,307,284,000
null
UTF-8
Lean
false
false
7,162
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import Lean import Std.Data.HashMap /- A rudimentary export format, adapted from <https://github.com/leanprover-community/lean/blob/master/doc/export_format.md> with support for lean 4 kernel primitives. -/ open Std (HashMap HashSet) namespace Lean namespace Export /- References -/ private constant MethodsRefPointed : PointedType.{0} private def MethodsRef : Type := MethodsRefPointed.type inductive Entry | name (n : Name) | level (n : Level) | expr (n : Expr) | defn (n : Name) deriving Inhabited instance : Coe Name Entry := ⟨Entry.name⟩ instance : Coe Level Entry := ⟨Entry.level⟩ instance : Coe Expr Entry := ⟨Entry.expr⟩ structure Alloc (α) [BEq α] [Hashable α] where map : HashMap α Nat next : Nat deriving Inhabited structure State where names : Alloc Name := ⟨HashMap.empty.insert Name.anonymous 0, 1⟩ levels : Alloc Level := ⟨HashMap.empty.insert levelZero 0, 1⟩ exprs : Alloc Expr defs : HashSet Name stk : Array (Bool × Entry) deriving Inhabited class OfState (α : Type) [BEq α] [Hashable α] where get : State → Alloc α modify : (Alloc α → Alloc α) → State → State instance : OfState Name where get s := s.names modify f s := { s with names := f s.names } instance : OfState Level where get s := s.levels modify f s := { s with levels := f s.levels } instance : OfState Expr where get s := s.exprs modify f s := { s with exprs := f s.exprs } end Export abbrev ExportM := StateT Export.State CoreM namespace Export def alloc {α} [BEq α] [Hashable α] [OfState α] (a : α) : ExportM Nat := do let n := (OfState.get (α := α) (← get)).next modify $ OfState.modify (α := α) fun s => {map := s.map.insert a n, next := n+1} n def exportName (n : Name) : ExportM Nat := do match (← get).names.map.find? n with | some i => pure i | none => match n with | Name.anonymous => pure 0 | Name.num p a _ => let i ← alloc n; IO.println s!"{i} #NI {← exportName p} {a}"; i | Name.str p s _ => let i ← alloc n; IO.println s!"{i} #NS {← exportName p} {s}"; i def exportLevel (L : Level) : ExportM Nat := do match (← get).levels.map.find? L with | some i => pure i | none => match L with | Level.zero _ => pure 0 | Level.succ l _ => let i ← alloc L; IO.println s!"{i} #US {← exportLevel l}"; i | Level.max l₁ l₂ _ => let i ← alloc L; IO.println s!"{i} #UM {← exportLevel l₁} {← exportLevel l₂}"; i | Level.imax l₁ l₂ _ => let i ← alloc L; IO.println s!"{i} #UIM {← exportLevel l₁} {← exportLevel l₂}"; i | Level.param n _ => let i ← alloc L; IO.println s!"{i} #UP {← exportName n}"; i | Level.mvar n _ => unreachable! def biStr : BinderInfo → String | BinderInfo.default => "#BD" | BinderInfo.implicit => "#BI" | BinderInfo.strictImplicit => "#BS" | BinderInfo.instImplicit => "#BC" | BinderInfo.auxDecl => unreachable! open ConstantInfo in mutual partial def exportExpr (E : Expr) : ExportM Nat := do match (← get).exprs.map.find? E with | some i => pure i | none => match E with | Expr.bvar n _ => let i ← alloc E; IO.println s!"{i} #EV {n}"; i | Expr.fvar _ _ => unreachable! | Expr.mvar _ _ => unreachable! | Expr.sort l _ => let i ← alloc E; IO.println s!"{i} #ES {← exportLevel l}"; i | Expr.const n ls _ => exportDef n let i ← alloc E let mut s ← s!"{i} #EC {← exportName n}" for l in ls do s := s ++ s!" {← exportLevel l}" IO.println s; i | Expr.app e₁ e₂ _ => let i ← alloc E; IO.println s!"{i} #EA {← exportExpr e₁} {← exportExpr e₂}"; i | Expr.lam n e₁ e₂ d => let i ← alloc E IO.println s!"{i} #EL {biStr d.binderInfo} {← exportExpr e₁} {← exportExpr e₂}"; i | Expr.forallE n e₁ e₂ d => let i ← alloc E IO.println s!"{i} #EP {biStr d.binderInfo} {← exportExpr e₁} {← exportExpr e₂}"; i | Expr.letE n e₁ e₂ e₃ _ => let i ← alloc E IO.println s!"{i} #EP {← exportExpr e₁} {← exportExpr e₂} {← exportExpr e₃}"; i | Expr.lit (Literal.natVal n) _ => let i ← alloc E; IO.println s!"{i} #EN {n}"; i | Expr.lit (Literal.strVal s) _ => let i ← alloc E; IO.println s!"{i} #ET {s}"; i | Expr.mdata _ e _ => unreachable! | Expr.proj n k e _ => let i ← alloc E; IO.println s!"{i} #EJ {← exportName n} {k} {← exportExpr e}"; i partial def exportDef (n : Name) : ExportM Unit := do if (← get).defs.contains n then return let ci ← getConstInfo n for c in ci.value!.getUsedConstants do unless (← get).defs.contains c do exportDef c match ci with | axiomInfo val => axdef "#AX" val.name val.type val.levelParams | defnInfo val => defn "#DEF" val.name val.type val.value val.levelParams | thmInfo val => defn "#THM" val.name val.type val.value val.levelParams | opaqueInfo val => defn "#CN" val.name val.type val.value val.levelParams | quotInfo val => IO.println "#QUOT" for n in [``Quot, ``Quot.mk, ``Quot.lift, ``Quot.ind] do insert n | inductInfo val => ind val.all | ctorInfo val => ind (← getConstInfoInduct val.induct).all | recInfo val => ind val.all where insert (n : Name) : ExportM Unit := modify fun s => { s with defs := s.defs.insert n } defn (ty : String) (n : Name) (t e : Expr) (ls : List Name) : ExportM Unit := do let mut s := s!"{ty} {← exportName n} {← exportExpr t} {← exportExpr e}" for l in ls do s := s ++ s!" {← exportName l}" IO.println s insert n axdef (ty : String) (n : Name) (t : Expr) (ls : List Name) : ExportM Unit := do let mut s := s!"{ty} {← exportName n} {← exportExpr t}" for l in ls do s := s ++ s!" {← exportName l}" IO.println s insert n ind : List Name → ExportM Unit | [] => unreachable! | is@(i::_) => do let val ← getConstInfoInduct i let mut s := match is.length with | 1 => s!"#IND {val.numParams}" | n => s!"#MUT {val.numParams} {n}" for j in is do insert j; insert (mkRecName j) for j in is do let vals ← getConstInfoInduct j s := s ++ s!" {← exportName val.name} {← exportExpr val.type} {val.ctors.length}" for c in val.ctors do insert c s := s ++ s!" {← exportName c} {← exportExpr (← getConstInfoCtor c).type}" for j in is do s ← indbody j s for l in val.levelParams do s := s ++ s!" {← exportName l}" IO.println s indbody (ind : Name) (s : String) : ExportM String := do let val ← getConstInfoInduct ind let mut s := s ++ s!" {← exportName ind} {← exportExpr val.type} {val.ctors.length}" for c in val.ctors do s := s ++ s!" {← exportName c} {← exportExpr (← getConstInfoCtor c).type}" s end def runExportM (m : ExportM α) : CoreM α := m.run' arbitrary -- #eval runExportM (exportDef `Lean.Expr) end Export end Lean
9adfa0cfeda40fa760434cc893237e341ee0545c
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/uniform_space/equiv.lean
5d7a81942d349c117c5daa71109befa8cf60e9ad
[ "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
12,167
lean
/- Copyright (c) 2022 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Patrick Massot, Sébastien Gouëzel, Zhouhang Zhou, Reid Barton, Anatole Dedecker -/ import topology.homeomorph import topology.uniform_space.uniform_embedding import topology.uniform_space.pi /-! # Uniform isomorphisms This file defines uniform isomorphisms between two uniform spaces. They are bijections with both directions uniformly continuous. We denote uniform isomorphisms with the notation `≃ᵤ`. # Main definitions * `uniform_equiv α β`: The type of uniform isomorphisms from `α` to `β`. This type can be denoted using the following notation: `α ≃ᵤ β`. -/ open set filter open_locale universes u v variables {α : Type u} {β : Type*} {γ : Type*} {δ : Type*} /-- Uniform isomorphism between `α` and `β` -/ @[nolint has_nonempty_instance] -- not all spaces are homeomorphic to each other structure uniform_equiv (α : Type*) (β : Type*) [uniform_space α] [uniform_space β] extends α ≃ β := (uniform_continuous_to_fun : uniform_continuous to_fun) (uniform_continuous_inv_fun : uniform_continuous inv_fun) infix ` ≃ᵤ `:25 := uniform_equiv namespace uniform_equiv variables [uniform_space α] [uniform_space β] [uniform_space γ] [uniform_space δ] instance : has_coe_to_fun (α ≃ᵤ β) (λ _, α → β) := ⟨λe, e.to_equiv⟩ @[simp] lemma uniform_equiv_mk_coe (a : equiv α β) (b c) : ((uniform_equiv.mk a b c) : α → β) = a := rfl /-- Inverse of a uniform isomorphism. -/ protected def symm (h : α ≃ᵤ β) : β ≃ᵤ α := { uniform_continuous_to_fun := h.uniform_continuous_inv_fun, uniform_continuous_inv_fun := h.uniform_continuous_to_fun, to_equiv := h.to_equiv.symm } /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply (h : α ≃ᵤ β) : α → β := h /-- See Note [custom simps projection] -/ def simps.symm_apply (h : α ≃ᵤ β) : β → α := h.symm initialize_simps_projections uniform_equiv (to_equiv_to_fun → apply, to_equiv_inv_fun → symm_apply, -to_equiv) @[simp] lemma coe_to_equiv (h : α ≃ᵤ β) : ⇑h.to_equiv = h := rfl @[simp] lemma coe_symm_to_equiv (h : α ≃ᵤ β) : ⇑h.to_equiv.symm = h.symm := rfl lemma to_equiv_injective : function.injective (to_equiv : α ≃ᵤ β → α ≃ β) | ⟨e, h₁, h₂⟩ ⟨e', h₁', h₂'⟩ rfl := rfl @[ext] lemma ext {h h' : α ≃ᵤ β} (H : ∀ x, h x = h' x) : h = h' := to_equiv_injective $ equiv.ext H /-- Identity map as a uniform isomorphism. -/ @[simps apply {fully_applied := ff}] protected def refl (α : Type*) [uniform_space α] : α ≃ᵤ α := { uniform_continuous_to_fun := uniform_continuous_id, uniform_continuous_inv_fun := uniform_continuous_id, to_equiv := equiv.refl α } /-- Composition of two uniform isomorphisms. -/ protected def trans (h₁ : α ≃ᵤ β) (h₂ : β ≃ᵤ γ) : α ≃ᵤ γ := { uniform_continuous_to_fun := h₂.uniform_continuous_to_fun.comp h₁.uniform_continuous_to_fun, uniform_continuous_inv_fun := h₁.uniform_continuous_inv_fun.comp h₂.uniform_continuous_inv_fun, to_equiv := equiv.trans h₁.to_equiv h₂.to_equiv } @[simp] lemma trans_apply (h₁ : α ≃ᵤ β) (h₂ : β ≃ᵤ γ) (a : α) : h₁.trans h₂ a = h₂ (h₁ a) := rfl @[simp] lemma uniform_equiv_mk_coe_symm (a : equiv α β) (b c) : ((uniform_equiv.mk a b c).symm : β → α) = a.symm := rfl @[simp] lemma refl_symm : (uniform_equiv.refl α).symm = uniform_equiv.refl α := rfl protected lemma uniform_continuous (h : α ≃ᵤ β) : uniform_continuous h := h.uniform_continuous_to_fun @[continuity] protected lemma continuous (h : α ≃ᵤ β) : continuous h := h.uniform_continuous.continuous protected lemma uniform_continuous_symm (h : α ≃ᵤ β) : uniform_continuous (h.symm) := h.uniform_continuous_inv_fun @[continuity] -- otherwise `by continuity` can't prove continuity of `h.to_equiv.symm` protected lemma continuous_symm (h : α ≃ᵤ β) : continuous (h.symm) := h.uniform_continuous_symm.continuous /-- A uniform isomorphism as a homeomorphism. -/ @[simps] protected def to_homeomorph (e : α ≃ᵤ β) : α ≃ₜ β := { continuous_to_fun := e.continuous, continuous_inv_fun := e.continuous_symm, .. e.to_equiv } @[simp] lemma apply_symm_apply (h : α ≃ᵤ β) (x : β) : h (h.symm x) = x := h.to_equiv.apply_symm_apply x @[simp] lemma symm_apply_apply (h : α ≃ᵤ β) (x : α) : h.symm (h x) = x := h.to_equiv.symm_apply_apply x protected lemma bijective (h : α ≃ᵤ β) : function.bijective h := h.to_equiv.bijective protected lemma injective (h : α ≃ᵤ β) : function.injective h := h.to_equiv.injective protected lemma surjective (h : α ≃ᵤ β) : function.surjective h := h.to_equiv.surjective /-- Change the uniform equiv `f` to make the inverse function definitionally equal to `g`. -/ def change_inv (f : α ≃ᵤ β) (g : β → α) (hg : function.right_inverse g f) : α ≃ᵤ β := have g = f.symm, from funext (λ x, calc g x = f.symm (f (g x)) : (f.left_inv (g x)).symm ... = f.symm x : by rw hg x), { to_fun := f, inv_fun := g, left_inv := by convert f.left_inv, right_inv := by convert f.right_inv, uniform_continuous_to_fun := f.uniform_continuous, uniform_continuous_inv_fun := by convert f.symm.uniform_continuous } @[simp] lemma symm_comp_self (h : α ≃ᵤ β) : ⇑h.symm ∘ ⇑h = id := funext h.symm_apply_apply @[simp] lemma self_comp_symm (h : α ≃ᵤ β) : ⇑h ∘ ⇑h.symm = id := funext h.apply_symm_apply @[simp] lemma range_coe (h : α ≃ᵤ β) : range h = univ := h.surjective.range_eq lemma image_symm (h : α ≃ᵤ β) : image h.symm = preimage h := funext h.symm.to_equiv.image_eq_preimage lemma preimage_symm (h : α ≃ᵤ β) : preimage h.symm = image h := (funext h.to_equiv.image_eq_preimage).symm @[simp] lemma image_preimage (h : α ≃ᵤ β) (s : set β) : h '' (h ⁻¹' s) = s := h.to_equiv.image_preimage s @[simp] lemma preimage_image (h : α ≃ᵤ β) (s : set α) : h ⁻¹' (h '' s) = s := h.to_equiv.preimage_image s protected lemma uniform_inducing (h : α ≃ᵤ β) : uniform_inducing h := uniform_inducing_of_compose h.uniform_continuous h.symm.uniform_continuous $ by simp only [symm_comp_self, uniform_inducing_id] lemma comap_eq (h : α ≃ᵤ β) : uniform_space.comap h ‹_› = ‹_› := by ext : 1; exact h.uniform_inducing.comap_uniformity protected lemma uniform_embedding (h : α ≃ᵤ β) : uniform_embedding h := ⟨h.uniform_inducing, h.injective⟩ /-- Uniform equiv given a uniform embedding. -/ noncomputable def of_uniform_embedding (f : α → β) (hf : uniform_embedding f) : α ≃ᵤ (set.range f) := { uniform_continuous_to_fun := hf.to_uniform_inducing.uniform_continuous.subtype_mk _, uniform_continuous_inv_fun := by simp [hf.to_uniform_inducing.uniform_continuous_iff, uniform_continuous_subtype_coe], to_equiv := equiv.of_injective f hf.inj } /-- If two sets are equal, then they are uniformly equivalent. -/ def set_congr {s t : set α} (h : s = t) : s ≃ᵤ t := { uniform_continuous_to_fun := uniform_continuous_subtype_val.subtype_mk _, uniform_continuous_inv_fun := uniform_continuous_subtype_val.subtype_mk _, to_equiv := equiv.set_congr h } /-- Product of two uniform isomorphisms. -/ def prod_congr (h₁ : α ≃ᵤ β) (h₂ : γ ≃ᵤ δ) : α × γ ≃ᵤ β × δ := { uniform_continuous_to_fun := (h₁.uniform_continuous.comp uniform_continuous_fst).prod_mk (h₂.uniform_continuous.comp uniform_continuous_snd), uniform_continuous_inv_fun := (h₁.symm.uniform_continuous.comp uniform_continuous_fst).prod_mk (h₂.symm.uniform_continuous.comp uniform_continuous_snd), to_equiv := h₁.to_equiv.prod_congr h₂.to_equiv } @[simp] lemma prod_congr_symm (h₁ : α ≃ᵤ β) (h₂ : γ ≃ᵤ δ) : (h₁.prod_congr h₂).symm = h₁.symm.prod_congr h₂.symm := rfl @[simp] lemma coe_prod_congr (h₁ : α ≃ᵤ β) (h₂ : γ ≃ᵤ δ) : ⇑(h₁.prod_congr h₂) = prod.map h₁ h₂ := rfl section variables (α β γ) /-- `α × β` is uniformly isomorphic to `β × α`. -/ def prod_comm : α × β ≃ᵤ β × α := { uniform_continuous_to_fun := uniform_continuous_snd.prod_mk uniform_continuous_fst, uniform_continuous_inv_fun := uniform_continuous_snd.prod_mk uniform_continuous_fst, to_equiv := equiv.prod_comm α β } @[simp] lemma prod_comm_symm : (prod_comm α β).symm = prod_comm β α := rfl @[simp] lemma coe_prod_comm : ⇑(prod_comm α β) = prod.swap := rfl /-- `(α × β) × γ` is uniformly isomorphic to `α × (β × γ)`. -/ def prod_assoc : (α × β) × γ ≃ᵤ α × (β × γ) := { uniform_continuous_to_fun := (uniform_continuous_fst.comp uniform_continuous_fst).prod_mk ((uniform_continuous_snd.comp uniform_continuous_fst).prod_mk uniform_continuous_snd), uniform_continuous_inv_fun := (uniform_continuous_fst.prod_mk (uniform_continuous_fst.comp uniform_continuous_snd)).prod_mk (uniform_continuous_snd.comp uniform_continuous_snd), to_equiv := equiv.prod_assoc α β γ } /-- `α × {*}` is uniformly isomorphic to `α`. -/ @[simps apply {fully_applied := ff}] def prod_punit : α × punit ≃ᵤ α := { to_equiv := equiv.prod_punit α, uniform_continuous_to_fun := uniform_continuous_fst, uniform_continuous_inv_fun := uniform_continuous_id.prod_mk uniform_continuous_const } /-- `{*} × α` is uniformly isomorphic to `α`. -/ def punit_prod : punit × α ≃ᵤ α := (prod_comm _ _).trans (prod_punit _) @[simp] lemma coe_punit_prod : ⇑(punit_prod α) = prod.snd := rfl /-- Uniform equivalence between `ulift α` and `α`. -/ def ulift : ulift.{v u} α ≃ᵤ α := { uniform_continuous_to_fun := uniform_continuous_comap, uniform_continuous_inv_fun := begin have hf : uniform_inducing (@equiv.ulift.{v u} α).to_fun, from ⟨rfl⟩, simp_rw [hf.uniform_continuous_iff], exact uniform_continuous_id, end, .. equiv.ulift } end /-- If `ι` has a unique element, then `ι → α` is homeomorphic to `α`. -/ @[simps { fully_applied := ff }] def fun_unique (ι α : Type*) [unique ι] [uniform_space α] : (ι → α) ≃ᵤ α := { to_equiv := equiv.fun_unique ι α, uniform_continuous_to_fun := Pi.uniform_continuous_proj _ _, uniform_continuous_inv_fun := uniform_continuous_pi.mpr (λ _, uniform_continuous_id) } /-- Uniform isomorphism between dependent functions `Π i : fin 2, α i` and `α 0 × α 1`. -/ @[simps { fully_applied := ff }] def pi_fin_two (α : fin 2 → Type u) [Π i, uniform_space (α i)] : (Π i, α i) ≃ᵤ α 0 × α 1 := { to_equiv := pi_fin_two_equiv α, uniform_continuous_to_fun := (Pi.uniform_continuous_proj _ 0).prod_mk (Pi.uniform_continuous_proj _ 1), uniform_continuous_inv_fun := uniform_continuous_pi.mpr $ fin.forall_fin_two.2 ⟨uniform_continuous_fst, uniform_continuous_snd⟩ } /-- Uniform isomorphism between `α² = fin 2 → α` and `α × α`. -/ @[simps { fully_applied := ff }] def fin_two_arrow : (fin 2 → α) ≃ᵤ α × α := { to_equiv := fin_two_arrow_equiv α, .. pi_fin_two (λ _, α) } /-- A subset of a uniform space is uniformly isomorphic to its image under a uniform isomorphism. -/ def image (e : α ≃ᵤ β) (s : set α) : s ≃ᵤ e '' s := { uniform_continuous_to_fun := (e.uniform_continuous.comp uniform_continuous_subtype_val).subtype_mk _, uniform_continuous_inv_fun := (e.symm.uniform_continuous.comp uniform_continuous_subtype_val).subtype_mk _, to_equiv := e.to_equiv.image s } end uniform_equiv /-- A uniform inducing equiv between uniform spaces is a uniform isomorphism. -/ @[simps] def equiv.to_uniform_equiv_of_uniform_inducing [uniform_space α] [uniform_space β] (f : α ≃ β) (hf : uniform_inducing f) : α ≃ᵤ β := { uniform_continuous_to_fun := hf.uniform_continuous, uniform_continuous_inv_fun := hf.uniform_continuous_iff.2 $ by simpa using uniform_continuous_id, .. f }
89165348729e608e6b9a2368933382c23bb39bab
9c2e8d73b5c5932ceb1333265f17febc6a2f0a39
/src/KT/vanilla.lean
a104f5a13bb2e79929b0865d3e508fd1fc0c3504
[ "MIT" ]
permissive
minchaowu/ModalTab
2150392108dfdcaffc620ff280a8b55fe13c187f
9bb0bf17faf0554d907ef7bdd639648742889178
refs/heads/master
1,626,266,863,244
1,592,056,874,000
1,592,056,874,000
153,314,364
12
1
null
null
null
null
UTF-8
Lean
false
false
4,020
lean
import .rules open psum nnf node set_option eqn_compiler.zeta true def tableau : Π Γ : seqt, node Γ | Γ := match get_contra_seqt Γ with | inl w := contra_rule_seqt w.2 | inr no_contra := match get_and_seqt Γ with | inl p := let Δ := p.1.1 :: p.1.2 :: Γ.main.erase (and p.val.1 p.val.2) in let inst := and_instance_seqt.cons p.2 in have h : prod.measure_lex seqt_size ⟨_,_,_,_⟩ Γ, begin apply split_lt_and_seqt, exact p.2 end, let d_delta : node ⟨Δ, Γ.hdld,_,_⟩ := tableau (and_child Γ p.2) in and_rule_seqt inst d_delta | inr no_and := match get_or_seqt Γ with | inl p := let Γ₁ := p.1.1 :: Γ.main.erase (nnf.or p.val.1 p.val.2) in let Γ₂ := p.1.2 :: Γ.main.erase (nnf.or p.val.1 p.val.2) in let inst := or_instance_seqt.cons p.2 in have h₁ : prod.measure_lex seqt_size ⟨Γ₁, Γ.hdld,_,_⟩ Γ, begin apply split_lt_or_seqt_left, exact p.2 end, have h₂ : prod.measure_lex seqt_size ⟨Γ₂, Γ.hdld,_,_⟩ Γ, begin apply split_lt_or_seqt_right, exact p.2 end, let d_Γ₁ : node ⟨Γ₁, Γ.hdld,_,_⟩ := tableau (or_child_left Γ p.2) in match d_Γ₁ with | closed pr := or_rule_seqt inst (closed pr) (tableau (or_child_right Γ p.2)) | open_ w := open_rule_seqt inst w.2 end | inr no_or := match get_box_seqt Γ with | inl p := let Γ₁ := p.1 :: Γ.main.erase (nnf.box p.1) in let inst := copy_instance_seqt.cons p.2 in have h : prod.measure_lex seqt_size ⟨Γ₁, box p.1 :: Γ.hdld,_,_⟩ Γ, begin apply copy_lt_seqt, exact p.2 end, let d_delta : node ⟨Γ₁, box p.1 :: Γ.hdld,_,_⟩ := tableau (box_child Γ p.2) in copy_rule_seqt inst d_delta | inr no_box := match get_dia_seqt Γ with | inl p := let ma : modal_applicable Γ := {satu := {no_and := no_and, no_or := no_or}, no_contra_main := no_contra, no_box_main := no_box, v := get_var Γ.main, hv := λ n, get_var_iff, φ := p.1, ex := p.2} in let l := @dia_rule_seqt (λ Δ, prod.measure_lex seqt_size Δ Γ) (λ x h, tableau x) (unmodal_seqt Γ) (unmodal_seqt_size Γ) in match l with | inl w := begin left, {exact unsat_of_unsat_unmodal ma w.1 w.2} end | inr w := begin right, split, apply sat_of_batch_sat, exact ma, exact w.2 end end | inr no_dia := let mc : model_constructible Γ := {satu := {no_and := no_and, no_or := no_or}, no_box_main := no_box, no_contra_main := no_contra, v := get_var Γ.main, hv := λ n, get_var_iff, no_dia := no_dia} in begin right, split, apply build_model_seqt, exact mc end end end end end end using_well_founded {rel_tac := λ _ _, `[exact ⟨_, prod.measure_lex_wf seqt_size⟩], dec_tac := `[assumption]} @[simp] def mk_seqt (Γ : list nnf) : seqt := { main := Γ, hdld := [], pmain := begin intros v l φ h hb hall, exfalso, apply list.not_mem_nil, exact hb end, phdld := box_only_nil } def is_sat (Γ : list nnf) : bool := match tableau (mk_seqt Γ) with | closed _ := ff | open_ _ := tt end theorem correctness (Γ : list nnf) : is_sat Γ = tt ↔ ∃ (st : Type) (k : KT st) s, sat k s Γ := begin cases h : is_sat Γ, constructor, {intro, contradiction}, {intro hsat, cases eq : tableau (mk_seqt Γ), rcases hsat with ⟨w, k, s, hsat⟩, apply false.elim, apply a, simp, exact hsat, {dsimp [is_sat] at h, dsimp at eq, rw eq at h, contradiction} }, {split, intro, dsimp [is_sat] at h, cases eq : tableau (mk_seqt Γ), { dsimp at eq, rw eq at h, contradiction }, { split, split, split, have := a_1.2, simp at this, exact this}, { simp } } end def test := [box (var 1), (neg 1)] #eval is_sat test
5e9ddc557bfd250a4fbf6b6ae431d822b4b8e41a
4727251e0cd73359b15b664c3170e5d754078599
/src/combinatorics/simple_graph/partition.lean
7ee17a2e6af7e43f95c202bc5d6d31dd8e931b5b
[ "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
5,058
lean
/- Copyright (c) 2021 Arthur Paulino. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Arthur Paulino, Kyle Miller -/ import combinatorics.simple_graph.coloring /-! # Graph partitions This module provides an interface for dealing with partitions on simple graphs. A partition of a graph `G`, with vertices `V`, is a set `P` of disjoint nonempty subsets of `V` such that: * The union of the subsets in `P` is `V`. * Each element of `P` is an independent set. (Each subset contains no pair of adjacent vertices.) Graph partitions are graph colorings that do not name their colors. They are adjoint in the following sense. Given a graph coloring, there is an associated partition from the set of color classes, and given a partition, there is an associated graph coloring from using the partition's subsets as colors. Going from graph colorings to partitions and back makes a coloring "canonical": all colors are given a canonical name and unused colors are removed. Going from partitions to graph colorings and back is the identity. ## Main definitions * `simple_graph.partition` is a structure to represent a partition of a simple graph * `simple_graph.partition.parts_card_le` is whether a given partition is an `n`-partition. (a partition with at most `n` parts). * `simple_graph.partitionable n` is whether a given graph is `n`-partite * `simple_graph.partition.to_coloring` creates colorings from partitions * `simple_graph.coloring.to_partition` creates partitions from colorings ## Main statements * `simple_graph.partitionable_iff_colorable` is that `n`-partitionability and `n`-colorability are equivalent. -/ universes u v namespace simple_graph variables {V : Type u} (G : simple_graph V) /-- A `partition` of a simple graph `G` is a structure constituted by * `parts`: a set of subsets of the vertices `V` of `G` * `is_partition`: a proof that `parts` is a proper partition of `V` * `independent`: a proof that each element of `parts` doesn't have a pair of adjacent vertices -/ structure partition := (parts : set (set V)) (is_partition : setoid.is_partition parts) (independent : ∀ (s ∈ parts), is_antichain G.adj s) /-- Whether a partition `P` has at most `n` parts. A graph with a partition satisfying this predicate called `n`-partite. (See `simple_graph.partitionable`.) -/ def partition.parts_card_le {G : simple_graph V} (P : G.partition) (n : ℕ) : Prop := ∃ (h : P.parts.finite), h.to_finset.card ≤ n /-- Whether a graph is `n`-partite, which is whether its vertex set can be partitioned in at most `n` independent sets. -/ def partitionable (n : ℕ) : Prop := ∃ (P : G.partition), P.parts_card_le n namespace partition variables {G} (P : G.partition) /-- The part in the partition that `v` belongs to -/ def part_of_vertex (v : V) : set V := classical.some (P.is_partition.2 v) lemma part_of_vertex_mem (v : V) : P.part_of_vertex v ∈ P.parts := by { obtain ⟨h, -⟩ := (P.is_partition.2 v).some_spec.1, exact h, } lemma mem_part_of_vertex (v : V) : v ∈ P.part_of_vertex v := by { obtain ⟨⟨h1, h2⟩, h3⟩ := (P.is_partition.2 v).some_spec, exact h2.1 } lemma part_of_vertex_ne_of_adj {v w : V} (h : G.adj v w) : P.part_of_vertex v ≠ P.part_of_vertex w := begin intro hn, have hw := P.mem_part_of_vertex w, rw ←hn at hw, exact P.independent _ (P.part_of_vertex_mem v) (P.mem_part_of_vertex v) hw (G.ne_of_adj h) h, end /-- Create a coloring using the parts themselves as the colors. Each vertex is colored by the part it's contained in. -/ def to_coloring : G.coloring P.parts := coloring.mk (λ v, ⟨P.part_of_vertex v, P.part_of_vertex_mem v⟩) $ λ _ _ hvw, by { rw [ne.def, subtype.mk_eq_mk], exact P.part_of_vertex_ne_of_adj hvw } /-- Like `simple_graph.partition.to_coloring` but uses `set V` as the coloring type. -/ def to_coloring' : G.coloring (set V) := coloring.mk P.part_of_vertex $ λ _ _ hvw, P.part_of_vertex_ne_of_adj hvw lemma to_colorable [fintype P.parts] : G.colorable (fintype.card P.parts) := P.to_coloring.to_colorable end partition variables {G} /-- Creates a partition from a coloring. -/ @[simps] def coloring.to_partition {α : Type v} (C : G.coloring α) : G.partition := { parts := C.color_classes, is_partition := C.color_classes_is_partition, independent := begin rintros s ⟨c, rfl⟩, apply C.color_classes_independent, end } instance : inhabited (partition G) := ⟨G.self_coloring.to_partition⟩ lemma partitionable_iff_colorable {n : ℕ} : G.partitionable n ↔ G.colorable n := begin split, { rintro ⟨P, hf, h⟩, haveI : fintype P.parts := hf.fintype, rw set.finite.card_to_finset at h, apply P.to_colorable.mono h, }, { rintro ⟨C⟩, refine ⟨C.to_partition, C.color_classes_finite_of_fintype, le_trans _ (fintype.card_fin n).le⟩, generalize_proofs h, haveI : fintype C.color_classes := C.color_classes_finite_of_fintype.fintype, rw h.card_to_finset, exact C.card_color_classes_le }, end end simple_graph
74f625a1f5cee01cf7795df44e9a35e0151fad80
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/stage0/src/Lean/Structure.lean
bf06c5fb54bc070ed59a3a1d202d65eda4ad2fc7
[ "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
9,141
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 Helper functions for retrieving structure information. -/ import Lean.Environment import Lean.ProjFns namespace Lean structure StructureFieldInfo where fieldName : Name projFn : Name /-- It is `some parentStructName` if it is a subobject, and `parentStructName` is the name of the parent structure -/ subobject? : Option Name binderInfo : BinderInfo autoParam? : Option Expr := none deriving Inhabited, Repr def StructureFieldInfo.lt (i₁ i₂ : StructureFieldInfo) : Bool := Name.quickLt i₁.fieldName i₂.fieldName structure StructureInfo where structName : Name fieldNames : Array Name := #[] -- sorted by field position in the structure fieldInfo : Array StructureFieldInfo := #[] -- sorted by `fieldName` deriving Inhabited def StructureInfo.lt (i₁ i₂ : StructureInfo) : Bool := Name.quickLt i₁.structName i₂.structName def StructureInfo.getProjFn? (info : StructureInfo) (i : Nat) : Option Name := if h : i < info.fieldNames.size then let fieldName := info.fieldNames.get ⟨i, h⟩ info.fieldInfo.binSearch { fieldName := fieldName, projFn := default, subobject? := none, binderInfo := default } StructureFieldInfo.lt |>.map (·.projFn) else none /-- Auxiliary state for structures defined in the current module. -/ private structure StructureState where map : PersistentHashMap Name StructureInfo := {} deriving Inhabited builtin_initialize structureExt : SimplePersistentEnvExtension StructureInfo StructureState ← registerSimplePersistentEnvExtension { name := `structExt addImportedFn := fun _ => {} addEntryFn := fun s e => { s with map := s.map.insert e.structName e } toArrayFn := fun es => es.toArray.qsort StructureInfo.lt } structure StructureDescr where structName : Name fields : Array StructureFieldInfo -- Should use the order the field appear in the constructor. deriving Inhabited def registerStructure (env : Environment) (e : StructureDescr) : Environment := structureExt.addEntry env { structName := e.structName fieldNames := e.fields.map fun e => e.fieldName fieldInfo := e.fields.qsort StructureFieldInfo.lt } def getStructureInfo? (env : Environment) (structName : Name) : Option StructureInfo := match env.getModuleIdxFor? structName with | some modIdx => structureExt.getModuleEntries env modIdx |>.binSearch { structName } StructureInfo.lt | none => structureExt.getState env |>.map.find? structName def getStructureCtor (env : Environment) (constName : Name) : ConstructorVal := match env.find? constName with | some (.inductInfo { isRec := false, ctors := [ctorName], .. }) => match env.find? ctorName with | some (ConstantInfo.ctorInfo val) => val | _ => panic! "ill-formed environment" | _ => panic! "structure expected" /-- Get direct field names for the given structure. -/ def getStructureFields (env : Environment) (structName : Name) : Array Name := if let some info := getStructureInfo? env structName then info.fieldNames else panic! "structure expected" def getFieldInfo? (env : Environment) (structName : Name) (fieldName : Name) : Option StructureFieldInfo := if let some info := getStructureInfo? env structName then info.fieldInfo.binSearch { fieldName := fieldName, projFn := default, subobject? := none, binderInfo := default } StructureFieldInfo.lt else none /-- If `fieldName` represents the relation to a parent structure `S`, return `S` -/ def isSubobjectField? (env : Environment) (structName : Name) (fieldName : Name) : Option Name := if let some fieldInfo := getFieldInfo? env structName fieldName then fieldInfo.subobject? else none /-- Return immediate parent structures -/ def getParentStructures (env : Environment) (structName : Name) : Array Name := let fieldNames := getStructureFields env structName; fieldNames.foldl (init := #[]) fun acc fieldName => match isSubobjectField? env structName fieldName with | some parentStructName => acc.push parentStructName | none => acc /-- Return all parent structures -/ partial def getAllParentStructures (env : Environment) (structName : Name) : Array Name := visit structName |>.run #[] |>.2 where visit (structName : Name) : StateT (Array Name) Id Unit := do for p in getParentStructures env structName do modify fun s => s.push p visit p /-- `findField? env S fname`. If `fname` is defined in a parent `S'` of `S`, return `S'` -/ partial def findField? (env : Environment) (structName : Name) (fieldName : Name) : Option Name := if (getStructureFields env structName).contains fieldName then some structName else getParentStructures env structName |>.findSome? fun parentStructName => findField? env parentStructName fieldName private partial def getStructureFieldsFlattenedAux (env : Environment) (structName : Name) (fullNames : Array Name) (includeSubobjectFields : Bool) : Array Name := (getStructureFields env structName).foldl (init := fullNames) fun fullNames fieldName => match isSubobjectField? env structName fieldName with | some parentStructName => let fullNames := if includeSubobjectFields then fullNames.push fieldName else fullNames getStructureFieldsFlattenedAux env parentStructName fullNames includeSubobjectFields | none => fullNames.push fieldName /-- Return field names for the given structure, including "flattened" fields from parent structures. To omit `toParent` projections, set `includeSubobjectFields := false`. For example, given `Bar` such that ```lean structure Foo where a : Nat structure Bar extends Foo where b : Nat ``` return `#[toFoo,a,b]` or `#[a,b]` with subobject fields omitted. -/ def getStructureFieldsFlattened (env : Environment) (structName : Name) (includeSubobjectFields := true) : Array Name := getStructureFieldsFlattenedAux env structName #[] includeSubobjectFields /-- Return true if `constName` is the name of an inductive datatype created using the `structure` or `class` commands. We perform the check by testing whether auxiliary projection functions have been created. -/ def isStructure (env : Environment) (constName : Name) : Bool := getStructureInfo? env constName |>.isSome def getProjFnForField? (env : Environment) (structName : Name) (fieldName : Name) : Option Name := if let some fieldInfo := getFieldInfo? env structName fieldName then some fieldInfo.projFn else none def getProjFnInfoForField? (env : Environment) (structName : Name) (fieldName : Name) : Option (Name × ProjectionFunctionInfo) := if let some projFn := getProjFnForField? env structName fieldName then (projFn, ·) <$> env.getProjectionFnInfo? projFn else none def mkDefaultFnOfProjFn (projFn : Name) : Name := projFn ++ `_default def getDefaultFnForField? (env : Environment) (structName : Name) (fieldName : Name) : Option Name := if let some projName := getProjFnForField? env structName fieldName then let defFn := mkDefaultFnOfProjFn projName if env.contains defFn then defFn else none else -- Check if we have a default function for a default values overridden by substructure. let defFn := mkDefaultFnOfProjFn (structName ++ fieldName) if env.contains defFn then defFn else none partial def getPathToBaseStructureAux (env : Environment) (baseStructName : Name) (structName : Name) (path : List Name) : Option (List Name) := if baseStructName == structName then some path.reverse else let fieldNames := getStructureFields env structName; fieldNames.findSome? fun fieldName => match isSubobjectField? env structName fieldName with | none => none | some parentStructName => match getProjFnForField? env structName fieldName with | none => none | some projFn => getPathToBaseStructureAux env baseStructName parentStructName (projFn :: path) /-- If `baseStructName` is an ancestor structure for `structName`, then return a sequence of projection functions to go from `structName` to `baseStructName`. -/ def getPathToBaseStructure? (env : Environment) (baseStructName : Name) (structName : Name) : Option (List Name) := getPathToBaseStructureAux env baseStructName structName [] /-- Return true iff `constName` is the a non-recursive inductive datatype that has only one constructor. -/ def isStructureLike (env : Environment) (constName : Name) : Bool := match env.find? constName with | some (.inductInfo { isRec := false, ctors := [_], numIndices := 0, .. }) => true | _ => false /-- Return number of fields for a structure-like type -/ def getStructureLikeNumFields (env : Environment) (constName : Name) : Nat := match env.find? constName with | some (.inductInfo { isRec := false, ctors := [ctor], numIndices := 0, .. }) => match env.find? ctor with | some (.ctorInfo { numFields := n, .. }) => n | _ => 0 | _ => 0 end Lean
8c4ccbb8198133c4ef4502ec6d812e0c85e92e95
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/complex/polynomial.lean
5065f84e1f5852fa20c5c1a3c8ce1d35928316d7
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
5,517
lean
/- Copyright (c) 2019 Chris Hughes All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import analysis.special_functions.pow import field_theory.is_alg_closed.basic import topology.algebra.polynomial /-! # The fundamental theorem of algebra This file proves that every nonconstant complex polynomial has a root. As a consequence, the complex numbers are algebraically closed. -/ open complex polynomial metric filter is_absolute_value set open_locale classical namespace complex /- The following proof uses the method given at <https://ncatlab.org/nlab/show/fundamental+theorem+of+algebra#classical_fta_via_advanced_calculus> -/ /-- **Fundamental theorem of algebra**: every non constant complex polynomial has a root -/ lemma exists_root {f : polynomial ℂ} (hf : 0 < degree f) : ∃ z : ℂ, is_root f z := let ⟨z₀, hz₀⟩ := f.exists_forall_norm_le in exists.intro z₀ $ classical.by_contradiction $ λ hf0, have hfX : f - C (f.eval z₀) ≠ 0, from mt sub_eq_zero.1 (λ h, not_le_of_gt hf (h.symm ▸ degree_C_le)), let n := root_multiplicity z₀ (f - C (f.eval z₀)) in let g := (f - C (f.eval z₀)) /ₘ ((X - C z₀) ^ n) in have hg0 : g.eval z₀ ≠ 0, from eval_div_by_monic_pow_root_multiplicity_ne_zero _ hfX, have hg : g * (X - C z₀) ^ n = f - C (f.eval z₀), from div_by_monic_mul_pow_root_multiplicity_eq _ _, have hn0 : n ≠ 0, from λ hn0, by simpa [g, hn0] using hg0, let ⟨δ', hδ'₁, hδ'₂⟩ := continuous_iff.1 (polynomial.continuous g) z₀ ((g.eval z₀).abs) (complex.abs_pos.2 hg0) in let δ := min (min (δ' / 2) 1) (((f.eval z₀).abs / (g.eval z₀).abs) / 2) in have hf0' : 0 < (f.eval z₀).abs, from complex.abs_pos.2 hf0, have hg0' : 0 < abs (eval z₀ g), from complex.abs_pos.2 hg0, have hfg0 : 0 < (f.eval z₀).abs / abs (eval z₀ g), from div_pos hf0' hg0', have hδ0 : 0 < δ, from lt_min (lt_min (half_pos hδ'₁) (by norm_num)) (half_pos hfg0), have hδ : ∀ z : ℂ, abs (z - z₀) = δ → abs (g.eval z - g.eval z₀) < (g.eval z₀).abs, from λ z hz, hδ'₂ z (by rw [complex.dist_eq, hz]; exact ((min_le_left _ _).trans (min_le_left _ _)).trans_lt (half_lt_self hδ'₁)), have hδ1 : δ ≤ 1, from le_trans (min_le_left _ _) (min_le_right _ _), let F : polynomial ℂ := C (f.eval z₀) + C (g.eval z₀) * (X - C z₀) ^ n in let z' := (-f.eval z₀ * (g.eval z₀).abs * δ ^ n / ((f.eval z₀).abs * g.eval z₀)) ^ (n⁻¹ : ℂ) + z₀ in have hF₁ : F.eval z' = f.eval z₀ - f.eval z₀ * (g.eval z₀).abs * δ ^ n / (f.eval z₀).abs, by simp only [F, cpow_nat_inv_pow _ hn0, div_eq_mul_inv, eval_pow, mul_assoc, mul_comm (g.eval z₀), mul_left_comm (g.eval z₀), mul_left_comm (g.eval z₀)⁻¹, mul_inv, inv_mul_cancel hg0, eval_C, eval_add, eval_neg, sub_eq_add_neg, eval_mul, eval_X, add_neg_cancel_right, neg_mul, mul_one, div_eq_mul_inv]; simp only [mul_comm, mul_left_comm, mul_assoc], have hδs : (g.eval z₀).abs * δ ^ n / (f.eval z₀).abs < 1, from (div_lt_one hf0').2 $ (lt_div_iff' hg0').1 $ calc δ ^ n ≤ δ : pow_le_of_le_one (le_of_lt hδ0) hδ1 hn0 ... ≤ ((f.eval z₀).abs / (g.eval z₀).abs) / 2 : min_le_right _ _ ... < _ : half_lt_self (div_pos hf0' hg0'), have hF₂ : (F.eval z').abs = (f.eval z₀).abs - (g.eval z₀).abs * δ ^ n, from calc (F.eval z').abs = (f.eval z₀ - f.eval z₀ * (g.eval z₀).abs * δ ^ n / (f.eval z₀).abs).abs : congr_arg abs hF₁ ... = abs (f.eval z₀) * complex.abs (1 - (g.eval z₀).abs * δ ^ n / (f.eval z₀).abs : ℝ) : by rw [← complex.abs_mul]; exact congr_arg complex.abs (by simp [mul_add, add_mul, mul_assoc, div_eq_mul_inv, sub_eq_add_neg]) ... = _ : by rw [complex.abs_of_nonneg (sub_nonneg.2 (le_of_lt hδs)), mul_sub, mul_div_cancel' _ (ne.symm (ne_of_lt hf0')), mul_one], have hef0 : abs (eval z₀ g) * (eval z₀ f).abs ≠ 0, from mul_ne_zero (mt complex.abs_eq_zero.1 hg0) (mt complex.abs_eq_zero.1 hf0), have hz'z₀ : abs (z' - z₀) = δ, by simp [z', mul_assoc, mul_left_comm _ (_ ^ n), mul_comm _ (_ ^ n), mul_comm (eval z₀ f).abs, _root_.mul_div_cancel _ hef0, of_real_mul, neg_mul, neg_div, is_absolute_value.abv_pow complex.abs, complex.abs_of_nonneg hδ0.le, real.pow_nat_rpow_nat_inv hδ0.le hn0], have hF₃ : (f.eval z' - F.eval z').abs < (g.eval z₀).abs * δ ^ n, from calc (f.eval z' - F.eval z').abs = (g.eval z' - g.eval z₀).abs * (z' - z₀).abs ^ n : by rw [← eq_sub_iff_add_eq.1 hg, ← is_absolute_value.abv_pow complex.abs, ← complex.abs_mul, sub_mul]; simp [F, eval_pow, eval_add, eval_mul, eval_sub, eval_C, eval_X, eval_neg, add_sub_cancel, sub_eq_add_neg, add_assoc] ... = (g.eval z' - g.eval z₀).abs * δ ^ n : by rw hz'z₀ ... < _ : (mul_lt_mul_right (pow_pos hδ0 _)).2 (hδ _ hz'z₀), lt_irrefl (f.eval z₀).abs $ calc (f.eval z₀).abs ≤ (f.eval z').abs : hz₀ _ ... = (F.eval z' + (f.eval z' - F.eval z')).abs : by simp ... ≤ (F.eval z').abs + (f.eval z' - F.eval z').abs : complex.abs_add _ _ ... < (f.eval z₀).abs - (g.eval z₀).abs * δ ^ n + (g.eval z₀).abs * δ ^ n : add_lt_add_of_le_of_lt (by rw hF₂) hF₃ ... = (f.eval z₀).abs : sub_add_cancel _ _ instance is_alg_closed : is_alg_closed ℂ := is_alg_closed.of_exists_root _ $ λ p _ hp, complex.exists_root $ degree_pos_of_irreducible hp end complex
eb1d65088b09703a86ecfaac9e8d847bf58a2e56
5b273b8c05e2f73fb74340ce100ce261900a98cd
/Zmod.lean
42ec5cdb1b81954d52732e8c9b6c918447810fe9
[]
no_license
ChrisHughes24/leanstuff1
2eba44bc48da6e544e07495b41e1703f81dc1c24
cbcd788b8b1d07b20b2fff4482c870077a13d1c0
refs/heads/master
1,631,670,333,297
1,527,093,981,000
1,527,093,981,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
18,239
lean
import .lagrange_four_square tactic.ring data.set.finite .int_gcd data.int.modeq data.int.basic data.nat.modeq data.equiv data.fintype data.nat.prime data.nat.gcd tactic.norm_num group_theory.order_of_element universe u local attribute [instance, priority 0] classical.prop_decidable open int int.modeq @[reducible] def Z_aux (n : ℤ) : Type := ℤ instance Zmod_setoid {n : ℤ} : setoid (Z_aux n) := { r := int.modeq n, iseqv := ⟨int.modeq.refl, @int.modeq.symm n, @int.modeq.trans n⟩ } def Zmod (n : ℤ) : Type := quotient (@Zmod_setoid n) lemma setoid_eq {α : Type*} : ∀ {a b : setoid α}, a.r = b.r → a = b | ⟨ar, ha⟩ ⟨br, hb⟩ rfl := rfl lemma Zmod_eq_Zmod_neg (n : ℤ) : Zmod n = (Zmod (-n)) := by unfold Zmod Zmod_setoid; exact congr rfl (setoid_eq (begin funext, unfold setoid.r int.modeq, rw [← int.mod_abs _ (- _), ← int.mod_abs _ (- _), abs_neg, ← int.mod_abs _ n, ← int.mod_abs _ n], end)) namespace Zmod private def add_aux {n : ℤ} (a b : Zmod n) : Zmod n := quotient.lift_on₂ a b (λ a b, ⟦a + b⟧) (λ a b c d hac hbd, quotient.sound (modeq_add hac hbd)) private def mul_aux {n : ℤ} (a b : Zmod n) : Zmod n := quotient.lift_on₂ a b (λ a b, ⟦a * b⟧) (λ a b c d hac hbd, quotient.sound (modeq_mul hac hbd)) private def neg_aux {n : ℤ} (a : Zmod n) : Zmod n := quotient.lift_on a (λ a, ⟦-a⟧) (λ a b hab, quotient.sound (modeq_neg hab)) instance (n : ℤ) : comm_ring (Zmod n) := { add := add_aux, add_assoc := λ a b c, quotient.induction_on₃ a b c (λ a b c, quotient.sound (by rw add_assoc)), zero := ⟦0⟧, zero_add := λ a, quotient.induction_on a (λ a, quotient.sound (by rw zero_add)), add_zero := λ a, quotient.induction_on a (λ a, quotient.sound (by rw add_zero)), neg := neg_aux, add_left_neg := λ a, quotient.induction_on a (λ a, quotient.sound (by rw add_left_neg)), add_comm := λ a b, quotient.induction_on₂ a b (λ a b, quotient.sound (by rw add_comm)), mul := mul_aux, mul_assoc := λ a b c, quotient.induction_on₃ a b c (λ a b c, quotient.sound (by rw mul_assoc)), one := ⟦1⟧, one_mul := λ a, quotient.induction_on a (λ a, quotient.sound (by rw one_mul)), mul_one := λ a, quotient.induction_on a (λ a, quotient.sound (by rw mul_one)), left_distrib := λ a b c, quotient.induction_on₃ a b c (λ a b c, quotient.sound (by rw left_distrib)), right_distrib := λ a b c, quotient.induction_on₃ a b c (λ a b c, quotient.sound (by rw right_distrib)), mul_comm := λ a b, quotient.induction_on₂ a b (λ a b, quotient.sound (by rw mul_comm)) } instance (n : ℤ) : has_coe ℤ (Zmod n) := ⟨quotient.mk⟩ instance Zmod_coe_nat (n : ℤ) : has_coe ℕ (Zmod n) := ⟨quotient.mk ∘ coe⟩ @[simp] lemma coe_nat_coe_int (n : ℤ) (a : ℕ) : (((a : ℕ) : ℤ) : Zmod n) = (a : Zmod n) := rfl @[simp] lemma mk_eq_coe (n a : ℤ) : (⟦a⟧ : Zmod n) = (a : Zmod n) := rfl @[simp] lemma coe_int_zero (n : ℤ) : ((0 : ℤ) : Zmod n) = 0 := rfl @[simp] lemma coe_int_one (n : ℤ) : ((1 : ℤ) : Zmod n) = 1 := rfl @[simp] lemma coe_int_bit0 (n a : ℤ) : ((bit0 a : ℤ) : Zmod n) = bit0 (a : Zmod n) := rfl @[simp] lemma coe_int_bit1 (n a : ℤ) : ((bit1 a : ℤ) : Zmod n) = bit1 (a : Zmod n) := rfl lemma coe_int_add (a b n : ℤ) : ((a + b : ℤ) : Zmod n) = a + b := rfl lemma coe_int_mul (a b n : ℤ) : ((a * b : ℤ) : Zmod n) = a * b := rfl lemma coe_int_neg (a n : ℤ) : ((-a : ℤ) : Zmod n) = -a := rfl @[simp] lemma coe_int_mod (a n : ℤ) : ((a % n : ℤ) : Zmod n) = a := quotient.sound (int.mod_mod _ _) lemma coe_int_pow (a n : ℤ) (k : ℕ) : ((a ^ k : ℤ) : Zmod n) = a ^ k := by induction k; simp [pow_succ, *, coe_int_mul] lemma coe_int_sub (a b n : ℤ) : ((a - b : ℤ) : Zmod n) = a - b := rfl @[simp] lemma coe_nat_zero (n : ℤ) : ((0 : ℕ) : Zmod n) = 0 := rfl @[simp] lemma coe_nat_one (n : ℤ) : ((1 : ℕ) : Zmod n) = 1 := rfl @[simp] lemma coe_nat_bit0 (n : ℤ) (a : ℕ) : ((bit0 a : ℕ) : Zmod n) = bit0 (a : Zmod n) := rfl @[simp] lemma coe_nat_bit1 (n : ℤ) (a : ℕ) : ((bit1 a : ℕ) : Zmod n) = bit1 (a : Zmod n) := rfl lemma coe_nat_add (a b : ℕ) (n : ℤ) : ((a + b : ℕ) : Zmod n) = a + b := rfl lemma coe_nat_mul (a b : ℕ) (n : ℤ) : ((a * b : ℕ) : Zmod n) = a * b := rfl lemma eq_iff_modeq_nat {n : ℤ} {a b : ℕ} : a ≡ b [MOD n.nat_abs] ↔ (a : Zmod n) = (b : Zmod n) := ⟨λ h, quotient.sound (int.modeq.modeq_iff_dvd.2 (int.nat_abs_dvd.1 (nat.modeq.modeq_iff_dvd.1 h))), λ h, nat.modeq.modeq_iff_dvd.2 (int.nat_abs_dvd.2 (int.modeq.modeq_iff_dvd.1 (quotient.exact h)))⟩ lemma eq_iff_modeq_int {n a b : ℤ} : a ≡ b [ZMOD n] ↔ (a : Zmod n) = b := ⟨λ h, quotient.sound h, λ h, quotient.exact h⟩ @[simp] lemma coe_nat_mod (a n : ℕ) : ((a % n : ℕ) : Zmod n) = a := eq_iff_modeq_nat.1 (nat.mod_mod _ _) lemma coe_nat_sub {a b : ℕ} (n : ℤ) (h : b ≤ a) : ((a - b : ℕ) : Zmod n) = a - b := show (((a - b : ℕ) : ℤ) : Zmod n) = a - b, by rw int.coe_nat_sub h; refl def to_nat {n : ℤ} (a : Zmod n) : ℕ := quotient.lift_on a (λ a, nat_abs (a % n)) (λ a b (hab : _ = _), or.cases_on (classical.em (n = 0)) (λ hn, by simp * at *) (λ hn, int.coe_nat_inj (by simpa [int.nat_abs_of_nonneg (mod_nonneg a hn), int.nat_abs_of_nonneg (mod_nonneg b hn)]))) lemma to_nat_lt {n : ℤ} (hn : n ≠ 0) (a : Zmod n) : a.to_nat < n.nat_abs := quotient.induction_on a (λ a, show (a % n).nat_abs < n.nat_abs, from int.coe_nat_lt.1 (by rw [int.nat_abs_of_nonneg (mod_nonneg a hn), ← abs_eq_nat_abs]; exact mod_lt _ hn)) @[simp] lemma coe_to_nat {n : ℤ} (hn : n ≠ 0) (a : Zmod n) : (a.to_nat : Zmod n) = a := quotient.induction_on a (λ a : ℤ, show (((a % n).nat_abs : ℤ) : Zmod n) = a, by rw [nat_abs_of_nonneg (mod_nonneg _ hn), coe_int_mod]) def equiv_fin {n : ℤ} (hn : n ≠ 0) : Zmod n ≃ fin (nat_abs n) := { to_fun := λ a, ⟨a.to_nat, to_nat_lt hn a⟩, inv_fun := λ a, ⟦a.1⟧, left_inv := λ a, quotient.induction_on a (λ a, quotient.sound (show ↑(a % n).nat_abs % n = _, by rw nat_abs_of_nonneg (mod_nonneg a hn); exact int.mod_mod _ _)), right_inv := λ ⟨a, ha⟩, have ha' : (a : ℤ) < abs n := (abs_eq_nat_abs n).symm ▸ int.coe_nat_lt.2 ha, fin.eq_of_veq (show ((a : ℤ) % n).to_nat = a, from int.coe_nat_inj (by rw [to_nat_of_nonneg (mod_nonneg a hn), ← mod_abs]; exact mod_eq_of_lt (int.coe_nat_le.2 (nat.zero_le a)) ha')) } def Zmod_fintype {n : ℤ} (hn : n ≠ 0) : fintype (Zmod n) := fintype.of_equiv _ (equiv_fin hn).symm lemma card_Zmod {n : ℤ} (hn : n ≠ 0) : @fintype.card (Zmod n) (Zmod_fintype hn) = n.nat_abs := fintype.card_fin n.nat_abs ▸ @fintype.card_congr _ _ (Zmod_fintype hn) _ (equiv_fin hn) private def inv_aux {n : ℤ} (a : Zmod n) : Zmod n := quotient.lift_on a (λ a, (⟦(nat.gcd_a (a % n).nat_abs n.nat_abs : ℤ)⟧ : Zmod n)) (λ a b (hab : _ = _), by unfold nat_mod; rw hab) instance (n : ℤ) : has_inv (Zmod n) := ⟨inv_aux⟩ @[simp] lemma int.gcd_neg (a b : ℤ) : gcd (-a) b = gcd a b := by unfold gcd; rw nat_abs_neg lemma gcd_a_modeq (a b : ℕ) : (a : ℤ) * nat.gcd_a a b ≡ nat.gcd a b [ZMOD b] := by rw [← add_zero ((a : ℤ) * _), nat.gcd_eq_gcd_ab]; exact int.modeq.modeq_add rfl (int.modeq.modeq_zero_iff.2 (dvd_mul_right _ _)).symm lemma mul_inv_eq_gcd_nat (n a : ℕ) : (a : Zmod n) * a⁻¹ = nat.gcd a n := show (a : Zmod n) * (nat.gcd_a (a % n) n) = nat.gcd a n, from by rw [nat.gcd_comm, nat.gcd_rec, ← coe_nat_coe_int _ (nat.gcd _ _), ← eq_iff_modeq_int.1 (gcd_a_modeq _ _), coe_int_mul, coe_nat_coe_int, coe_nat_mod] lemma mul_inv_eq_gcd_int (n a : ℤ) (hn : n ≠ 0) : (a : Zmod n) * a⁻¹ = int.gcd a n := have ha : (a : Zmod n) = (a % n.nat_abs).nat_abs := by rw [← coe_nat_coe_int, ← abs_eq_nat_abs n, mod_abs, int.nat_abs_of_nonneg (int.mod_nonneg _ hn), coe_int_mod], have h : ∀ a : ℕ, (a : ℤ) * nat.gcd_a a n.nat_abs ≡ nat.gcd a n.nat_abs [ZMOD n] :=λ a, begin unfold int.modeq, rw [← mod_abs _ n, ← mod_abs _ n, abs_eq_nat_abs], exact gcd_a_modeq _ _, end, show (a : Zmod n) * (nat.gcd_a (a % n).nat_abs n.nat_abs) = int.gcd a n, begin rw [ha, gcd_comm, ← int.gcd_mod, int.gcd, ← mod_abs _ n, abs_eq_nat_abs, ← coe_nat_coe_int _ (nat.gcd _ _), ← eq_iff_modeq_int.1 (h _)], refl, end private lemma mul_inv_cancel_aux {p : ℕ} (hp : nat.prime p) {a : Zmod p} : a ≠ 0 → a * a⁻¹ = (1 : Zmod p) := quotient.induction_on a (λ (a : ℤ) ha, begin rw [mk_eq_coe, ne.def, ← coe_int_zero, ← eq_iff_modeq_int, modeq_zero_iff, ← dvd_nat_abs, ← nat_abs_dvd, int.coe_nat_dvd] at ha, have : gcd p a = 1 := hp.coprime_iff_not_dvd.2 ha, rw [mk_eq_coe, mul_inv_eq_gcd_int _ _ (ne_of_lt (int.coe_nat_lt.2 hp.pos)).symm, gcd_comm, this, coe_nat_one], end) def Zmod_prime_field {p : ℕ} (hp : nat.prime p) : discrete_field (Zmod p) := { inv := has_inv.inv, zero_ne_one := λ h, let h : (0 : ℤ) % p = 1 % p := quotient.exact h in begin rw mod_eq_of_lt (le_refl (0 : ℤ)) (int.coe_nat_lt.2 hp.pos) at h, rw mod_eq_of_lt (show (0 : ℤ) ≤ 1, by norm_num) (int.coe_nat_lt.2 hp.gt_one) at h, exact absurd h dec_trivial, end, mul_inv_cancel := λ a ha, mul_inv_cancel_aux hp ha, inv_mul_cancel := λ a ha, by rw mul_comm; exact mul_inv_cancel_aux hp ha, has_decidable_eq := by apply_instance, inv_zero := show ((nat.xgcd_aux (nat_abs (0 % p)) 1 0 p 0 1).2.1 : Zmod p) = 0, begin rw zero_mod, change nat_abs 0 with 0, rw nat.xgcd_zero_left, refl, end, ..Zmod.comm_ring p } instance (p : {p // nat.prime p}) : discrete_field (Zmod p) := Zmod_prime_field p.2 instance (p : {p // nat.prime p}) : fintype (Zmod p) := Zmod_fintype (ne_of_lt (int.coe_nat_lt.2 p.2.pos)).symm noncomputable lemma fintype.of_injective {α β : Type*} [fintype α] (f : β → α) (hf : function.injective f) : fintype β := by haveI := classical.dec_eq β; exact classical.choice (or.cases_on (classical.em (nonempty β)) (λ h, by haveI : inhabited β := ⟨classical.choice h⟩; exact ⟨fintype.of_surjective (function.inv_fun f) (function.inv_fun_surjective hf)⟩) (λ h, ⟨⟨∅, λ x, false.elim (h ⟨x⟩)⟩⟩)) noncomputable instance fintype_units {α : Type*} [fintype α] [monoid α] : fintype (units α) := fintype.of_injective units.val (λ x y h, units.ext h) lemma fintype.card_pos {α : Type*} [fintype α] (a : α) : 0 < fintype.card α := nat.pos_of_ne_zero (λ h, begin have : α ≃ fin 0 := eq.rec_on h (trunc.out (fintype.equiv_fin α)), exact fin.elim0 (this.1 a), end) def units_equiv_ne_zero (p : {p // nat.prime p}) : units (Zmod p) ≃ {a : Zmod p // a ≠ 0} := { to_fun := λ ⟨a, b, h₁, h₂⟩, ⟨a, λ h, by rw [h, zero_mul] at h₁; exact zero_ne_one h₁⟩, inv_fun := λ ⟨a, ha⟩, ⟨a, a⁻¹, mul_inv_cancel ha, inv_mul_cancel ha⟩, left_inv := λ ⟨a, b, h₁, h₂⟩, have ha : a ≠ 0 := λ h, by rw [h, zero_mul] at h₁; exact zero_ne_one h₁, suffices h : a⁻¹ = b, by simp! [h], by rw [← one_mul (a⁻¹), ← h₂, mul_assoc, mul_inv_cancel ha, mul_one], right_inv := λ ⟨a, ha⟩, rfl } def equiv_univ (α : Type*) : α ≃ @set.univ α := ⟨λ x, ⟨x, trivial⟩, λ x, x.1, λ _, rfl, λ ⟨_, _⟩, rfl⟩ lemma fintype.card_ne {α : Type u} [fintype α] [decidable_eq α] (a : α) : @fintype.card {b | b ≠ a} (set_fintype {b | b ≠ a}) = fintype.card α - 1 := begin haveI := set_fintype {b | b ≠ a}, haveI := set_fintype (@set.univ α), haveI := classical.prop_decidable, have ha : a ∉ {b : α | b ≠ a} := by simp [set.mem_def], have h2 : 1 ≤ fintype.card (@set.univ α) := fintype.card_pos ⟨a, trivial⟩, rw [fintype.card_congr (equiv_univ α), eq_comm, nat.sub_eq_iff_eq_add, ← @set.card_insert _ _ a], simp [set.insert, classical.em], congr, assumption, assumption, end lemma card_units_prime {p : {p // nat.prime p}} : @fintype.card (units (Zmod p)) (@Zmod.fintype_units _ (Zmod_fintype (ne_of_lt (int.coe_nat_lt.2 p.2.pos)).symm) _) = p - 1 := begin change (p : ℕ) with ((p : ℤ).nat_abs), rw [← card_Zmod (show (p : ℤ) ≠ 0, from (ne_of_lt (int.coe_nat_lt.2 p.2.pos)).symm), fintype.card_congr (units_equiv_ne_zero _), ← fintype.card_ne (0 : Zmod p)], refl, end lemma units.pow_coe {α : Type*} [monoid α] {a : units α} {n : ℕ} : ((a ^ n : units α ) : α) = (a : α) ^ n := by induction n; simp [pow_succ, *] lemma fermat_little {p : {p // nat.prime p}} {a : Zmod p} (ha : a ≠ 0) : a ^ ((p : ℕ) - 1) = 1 := begin have := units.val_coe (units.mk0 _ ha), change (a : Zmod p) with ((units.mk0 _ ha).1 : Zmod p), rw [← units.val_coe, ← units.pow_coe, ← card_units_prime, pow_eq_mod_order_of, nat.mod_eq_zero_of_dvd (order_of_dvd_card_univ )], simp end lemma odd_sub_one_even {n : ℕ} (ho : n % 2 = 1) : 2 ∣ n - 1 := begin rw [← nat.mod_add_div n 2, ho, nat.add_sub_cancel_left], exact dvd_mul_right _ _ end lemma pow_half_prime_sub_one_eq_one_or_neg_one {p : {p // nat.prime p}} (hpo : (p : ℕ) % 2 = 1) {a : Zmod p} (ha : a ≠ 0) : a ^ (((p : ℕ) - 1) / 2) = 1 ∨ a ^ (((p : ℕ) - 1) / 2) = -1 := begin have := fermat_little ha, rw [← nat.mul_div_cancel' (odd_sub_one_even hpo), pow_mul, pow_two, mul_pow] at this, exact (mul_self_eq_one_iff _).1 this end instance units_comm (α : Type*) [comm_monoid α] : comm_group (units α) := { mul_comm := λ a b, units.ext (show ((a * b : units α) : α) = ((b * a : units α) : α), by simp [units.mul_coe, mul_comm]), ..units.group } open finset lemma int.coe_nat_ne_zero_of_pos {n : ℕ} : 0 < n → (n : ℤ) ≠ 0 := ne.symm ∘ ne_of_lt ∘ int.coe_nat_lt.2 lemma prod_finset_distinct_inv {α : Type*} [discrete_field α] {s : finset α} : (∀ x ∈ s, x⁻¹ ∈ s) → (∀ x ∈ s, x⁻¹ ≠ x) → s.prod id = 1 := finset.strong_induction_on s $ λ s ih h₁ h₂, or.cases_on (classical.em (s = ∅)) (λ h, by simp [h]) (λ h, have h0 : ∀ (y : α), y ∈ s → y ≠ 0 := λ y hy h, (show (0 : α)⁻¹ ≠ 0, from h ▸ (h₂ y hy)) (by simp), let ⟨x, hx⟩ := exists_mem_of_ne_empty h in have hxi : x⁻¹ ∈ s.erase x := mem_erase.2 ⟨h₂ x hx, h₁ x hx⟩, have ht : erase (erase s x) x⁻¹ ⊂ s := lt_iff_ssubset.1 (lt.trans (erase_ssubset hxi) (erase_ssubset hx)), have ht₁ : ∀ y ∈ erase (erase s x) x⁻¹, y⁻¹ ∈ erase (erase s x) x⁻¹ := λ y hy, by rw [mem_erase, mem_erase] at *; exact ⟨λ h, hy.2.1 (begin rw [inv_eq_one_div, inv_eq_one_div, div_eq_div_iff (h0 y hy.2.2) (h0 x hx), one_mul, one_mul] at h, exact h.symm, end), λ h, hy.1 (by rw[← h, inv_eq_one_div, inv_eq_one_div, one_div_div, div_one]), h₁ y hy.2.2⟩, have ht₂ : ∀ y ∈ erase (erase s x) x⁻¹, y⁻¹ ≠ y := λ y hy, by rw [mem_erase, mem_erase] at hy; exact h₂ y hy.2.2, begin rw [← insert_erase hx, prod_insert (not_mem_erase _ _), ← insert_erase hxi, prod_insert (not_mem_erase _ _), ih _ ht ht₁ ht₂], simp [mul_inv_cancel (h0 x hx)], end) lemma ne_neg {n : ℕ} (ho : n % 2 = 1) {a : Zmod n} (ha : a ≠ 0) : a ≠ -a := have hn : (n : ℤ) ≠ 0 := λ h, by rw int.coe_nat_inj h at ho; contradiction, begin rw [← coe_to_nat hn a, ne.def, eq_neg_iff_add_eq_zero, ← coe_nat_add, ← coe_nat_zero, ← eq_iff_modeq_nat, nat.modeq.modeq_zero_iff, nat_abs_of_nat], refine not_exists.2 (λ x h, _), rw ← mul_two at h, have : to_nat a * 2 < n * 2 := (mul_lt_mul_right (show 2 > 0, from dec_trivial)).2 (to_nat_lt hn _), rw h at this, cases x, { rw mul_zero at h, have := or.resolve_right (nat.eq_zero_of_mul_eq_zero h) dec_trivial, rw [← coe_to_nat hn a, this] at ha, contradiction }, { cases x, { rw mul_one at h, rw [← h, nat.mul_mod_left] at ho, contradiction }, { exact absurd (lt_of_mul_lt_mul_left this (nat.zero_le _)) dec_trivial } } end lemma wilson_lemma {p : {p // nat.prime p}} (hpo : (p : ℕ) % 2 = 1) : ((univ : finset (Zmod p)).erase 0).prod id = -1 := have h10 : (1 : Zmod p) ≠ 0 := by rw [← coe_nat_one, ← coe_nat_zero, ne.def, ← eq_iff_modeq_nat, nat.modeq.modeq_zero_iff]; exact p.2.not_dvd_one, have h₁ : (1 : Zmod p) ∈ univ.erase (0 : Zmod p) := mem_erase.2 ⟨h10, mem_univ _⟩, have h₂ : (-1 : Zmod p) ∈ (erase (erase univ (0 : Zmod p)) 1) := mem_erase.2 ⟨ne.symm (ne_neg hpo h10), mem_erase.2 ⟨(by rw [ne.def, neg_eq_iff_neg_eq, neg_zero]; exact h10.symm), mem_univ _⟩⟩, have h₃ : ∀ (x : Zmod ↑p), x ∈ erase (erase (erase univ (0 : Zmod p)) 1) (-1) → x⁻¹ ∈ erase (erase (erase univ (0 : Zmod p)) 1) (-1) := λ x hx, begin rw [mem_erase, mem_erase, mem_erase] at *, rw [ne.def, ne.def, ne.def, inv_eq_one_div, div_eq_iff_mul_eq hx.2.2.1, div_eq_iff_mul_eq hx.2.2.1, div_eq_iff_mul_eq hx.2.2.1, neg_mul_comm, one_mul, one_mul, zero_mul, neg_eq_iff_neg_eq], exact ⟨hx.1.symm, hx.2.1, h₁.1.symm, mem_univ _⟩, end, have h₄ : ∀ (x : Zmod ↑p), x ∈ erase (erase (erase univ (0 : Zmod p)) 1) (-1) → x⁻¹ ≠ x := λ x hx, begin rw [mem_erase, mem_erase, mem_erase] at hx, rw [ne.def, inv_eq_one_div, div_eq_iff_mul_eq hx.2.2.1, mul_self_eq_one_iff, not_or_distrib], exact ⟨hx.2.1, hx.1⟩ end, begin rw [← insert_erase h₁, prod_insert (not_mem_erase _ _), ← insert_erase h₂, prod_insert (not_mem_erase _ _), prod_finset_distinct_inv h₃ h₄], simp, end lemma euler_criterion {p : {p // nat.prime p}} (hpo : (p : ℕ) % 2 = 1) {a : units (Zmod p)} : (∃ x, x * x = a) ↔ a ^ (((p : ℕ) - 1) / 2) = 1 := have h₁ : {b : units (Zmod p) | ∃ x, x * x = b} ⊆ {b | b ^ (((p : ℕ) - 1) / 2) = 1} := λ b ⟨x, hx⟩, show b ^ (((p : ℕ) - 1) / 2) = 1, begin rw [← hx, ← pow_two, ← pow_mul, nat.mul_div_cancel' (odd_sub_one_even hpo)], exact fermat_little hx0, end, begin end #eval ((12345 : Zmod 145975)⁻¹).to_nat #eval 12345 * 3169 % 145975 end Zmod
aee977ee2747802e70316a3536665a0f17a9a0bb
491068d2ad28831e7dade8d6dff871c3e49d9431
/hott/algebra/category/groupoid.hlean
18153d38ae164353995aa3256a654e91a4a9a07d
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,698
hlean
/- Copyright (c) 2014 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer, Floris van Doorn Ported from Coq HoTT -/ import .iso ..group open eq is_trunc iso category algebra nat unit namespace category structure groupoid [class] (ob : Type) extends parent : precategory ob := mk' :: (all_iso : Π ⦃a b : ob⦄ (f : hom a b), @is_iso ob parent a b f) abbreviation all_iso := @groupoid.all_iso attribute groupoid.all_iso [instance] [priority 100000] definition groupoid.mk [reducible] {ob : Type} (C : precategory ob) (H : Π (a b : ob) (f : a ⟶ b), is_iso f) : groupoid ob := precategory.rec_on C groupoid.mk' H -- We can turn each group into a groupoid on the unit type definition groupoid_of_group.{l} (A : Type.{l}) [G : group A] : groupoid.{0 l} unit := begin fapply groupoid.mk, fapply precategory.mk, intros, exact A, intros, apply (@group.is_hset_carrier A G), intros [a, b, c, g, h], exact (@group.mul A G g h), intro a, exact (@group.one A G), intros, exact (@group.mul_assoc A G h g f)⁻¹, intros, exact (@group.one_mul A G f), intros, exact (@group.mul_one A G f), intros, esimp [precategory.mk], apply is_iso.mk, apply mul.left_inv, apply mul.right_inv, end definition hom_group {A : Type} [G : groupoid A] (a : A) : group (hom a a) := begin fapply group.mk, intro f g, apply (comp f g), apply is_hset_hom, intros f g h, apply (assoc f g h)⁻¹, apply (ID a), intro f, apply id_left, intro f, apply id_right, intro f, exact (iso.inverse f), intro f, exact (iso.left_inverse f), end definition group_of_is_contr_groupoid {ob : Type} [H : is_contr ob] [G : groupoid ob] : group (hom (center ob) (center ob)) := !hom_group definition group_of_groupoid_unit [G : groupoid unit] : group (hom ⋆ ⋆) := !hom_group -- Bundled version of categories -- we don't use Groupoid.carrier explicitly, but rather use Groupoid.carrier (to_Precategory C) structure Groupoid : Type := (carrier : Type) (struct : groupoid carrier) attribute Groupoid.struct [instance] [coercion] definition Groupoid.to_Precategory [coercion] [reducible] (C : Groupoid) : Precategory := Precategory.mk (Groupoid.carrier C) C definition groupoid.Mk [reducible] := Groupoid.mk definition groupoid.MK [reducible] (C : Precategory) (H : Π (a b : C) (f : a ⟶ b), is_iso f) : Groupoid := Groupoid.mk C (groupoid.mk C H) definition Groupoid.eta (C : Groupoid) : Groupoid.mk C C = C := Groupoid.rec (λob c, idp) C end category
1a54865900646127e345064174b3dac92ef4397b
19e80d9dcdc050ba32a44257170460f6eb2f174b
/gcdbasics.lean
65243fc2c1f5c3c29e86bc363ddf75b63fcb84a4
[]
no_license
petersonbill64/lean
d36d7d93d0f1e4e5618f1426d41567e33a0da6fc
07b7b69e15785c75ebba6629be4fad92799fd7de
refs/heads/master
1,609,627,927,456
1,438,537,533,000
1,438,537,533,000
40,084,227
0
0
null
null
null
null
UTF-8
Lean
false
false
17,171
lean
import data.nat.div data.nat.sub data.int tools.fake_simplifier data.int logic open nat int eq.ops well_founded decidable fake_simplifier prod 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 local attribute pair_nat.lt.wf [instance] 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₁) /- extended gcd definitions and properties -/ definition egcd_rec_f (z : int) : int → int → int × int := (λ s t, (t, int.sub s (int.mul t z))) definition egcdi.F (p₁ : nat × nat) : (Π p₂ : nat × nat, p₂ ≺ p₁ → (int × int)) → (int × int) := prod.cases_on p₁ (λ x y, nat.cases_on y ( λ f, pair 1 0 ) ( λ y₁ (f : Π p₂, p₂ ≺ (x, succ y₁) → (int × int)), let bz := f (succ y₁, x mod succ y₁) !gcd.lt.dec in prod.cases_on bz (egcd_rec_f (x div succ y₁)))) definition egcdi (x y : nat) := fix egcdi.F (pair x y) theorem egcdi_zero_right (x : nat) : egcdi x 0 = pair 1 0 := well_founded.fix_eq egcdi.F (x, 0) theorem egcdi_zero_right_1 (x : nat) : pr₁ (egcdi x 0) = 1 := eq.subst (egcdi_zero_right x) rfl theorem egcdi_zero_right_2 (x : nat) : pr₂ (egcdi x 0) = 0 := eq.subst (egcdi_zero_right x) rfl theorem egcdi_succ (x y : nat) : egcdi x (succ y) = prod.cases_on (egcdi (succ y) (x mod succ y)) (egcd_rec_f (x div succ y)) := well_founded.fix_eq egcdi.F (x, succ y) theorem preq {A : Type} : (Π (p : A × A), p = (pr₁ p, pr₂ p)) | (a, b) := have H11 : a = pr₁ (a, b), from rfl, have H12 : b = pr₂ (a, b), from rfl, have H21 : pr₁ (a, b) = pr₁ (pr₁ (a, b), pr₂ (a, b)), from rfl, have H22 : pr₂ (a, b) = pr₂ (pr₁ (a, b), pr₂ (a, b)), from rfl, have H1 : pr₁ (a, b) = pr₁ (pr₁ (a, b), pr₂ (a, b)), from eq.trans H11 H21, have H2 : pr₂ (a, b) = pr₂ (pr₁ (a, b), pr₂ (a, b)), from eq.trans H12 H22, prod.equal H1 H2 theorem pc1 (z s t : int) : prod.cases_on (s, t) (egcd_rec_f z) = (t, int.sub s (int.mul t z)) := have H1 : prod.cases_on (s, t) (egcd_rec_f z) = egcd_rec_f z s t, from rfl, have H2 : egcd_rec_f z s t = (t, int.sub s (int.mul t z)), from rfl, eq.trans H1 H2 theorem pc2 (z : int) (p : int × int) : prod.cases_on p (egcd_rec_f z) = (pr₂ p, int.sub (pr₁ p) (int.mul (pr₂ p) z)) := have H1 : prod.cases_on (pr₁ p, pr₂ p) (egcd_rec_f z) = (pr₂ p, int.sub (pr₁ p) (int.mul (pr₂ p) z)), from pc1 z (pr₁ p) (pr₂ p), eq.subst (preq p)⁻¹ H1 theorem egcdi_succ2 (x y : nat) : egcdi x (succ y) = (pr₂ (egcdi (succ y) (x mod succ y)), int.sub (pr₁ (egcdi (succ y) (x mod succ y))) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x div succ y))) := have H1 : egcdi x (succ y) = prod.cases_on (egcdi (succ y) (x mod succ y)) (egcd_rec_f (x div succ y)), from egcdi_succ x y, have H2 : prod.cases_on (egcdi (succ y) (x mod succ y)) (egcd_rec_f (x div succ y)) = (pr₂ (egcdi (succ y) (x mod succ y)), int.sub (pr₁ (egcdi (succ y) (x mod succ y))) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x div succ y))), from pc2 (x div succ y) (egcdi (succ y) (x mod succ y)), eq.trans H1 H2 theorem int_rearrange (a b c : int) : int.add a (int.sub b (int.sub a c)) = int.add b c := calc int.add a (int.sub b (int.sub a c)) = int.add a (int.add b (int.neg (int.sub a c))) : int.sub_eq_add_neg ... = int.add a (int.add b (int.sub c a)) : int.neg_sub ... = int.add a (int.add b (int.add c (int.neg a))) : int.sub_eq_add_neg ... = int.add a (int.add b (int.add (int.neg a) c)) : int.add.comm ... = int.add a (int.add (int.add (int.neg a) c) b) : int.add.comm ... = int.add (int.add a (int.add (int.neg a) c)) b : int.add.assoc ... = int.add c b : int.add_neg_cancel_left ... = int.add b c : int.add.comm theorem sub_mod_eq_int_sub_mod (x y : nat) : x - (x mod (succ y)) = int.sub x (x mod (succ y)) := (int.of_nat_sub_of_nat mod_le)⁻¹ theorem divmul (x y : nat) : int.mul (x div y) y = x - (x mod y) := calc int.mul (x div y) y = nat.mul (nat.divide x y) y : int.of_nat_mul_of_nat ... = (x div y) * y + (x mod y) - (x mod y) : (nat.add_sub_cancel ((x div y) * y) (x mod y))⁻¹ ... = x - (x mod y) : eq_div_mul_add_mod⁻¹ theorem egcdi_succ_first (x y : nat) : pr₁ (egcdi x (succ y)) = pr₂ (egcdi (succ y) (x mod succ y)) := calc pr₁ (egcdi x (succ y)) = pr₁ (pr₂ (egcdi (succ y) (x mod succ y)), int.sub (pr₁ (egcdi (succ y) (x mod succ y))) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x div succ y))) : egcdi_succ2 x y ... = pr₂ (egcdi (succ y) (x mod succ y)) : rfl theorem egcdi_succ_second (x y : nat) : pr₂ (egcdi x (succ y)) = int.sub (pr₁ (egcdi (succ y) (x mod succ y))) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x div succ y)) := calc pr₂ (egcdi x (succ y)) = pr₂ (pr₂ (egcdi (succ y) (x mod succ y)), int.sub (pr₁ (egcdi (succ y) (x mod succ y))) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x div succ y))) : egcdi_succ2 x y ... = int.sub (pr₁ (egcdi (succ y) (x mod succ y))) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x div succ y)) : rfl /- Inductive step -/ theorem egcdi_ind (x y : nat) : int.add (int.mul (pr₁ (egcdi (succ y) (x mod (succ y)))) (succ y)) (int.mul (pr₂ (egcdi (succ y) (x mod (succ y)))) (x mod (succ y))) = gcd (succ y) (x mod (succ y)) → int.add (int.mul (pr₁ (egcdi x (succ y))) x) (int.mul (pr₂ (egcdi x (succ y))) (succ y)) = gcd x (succ y) := assume IH : int.add (int.mul (pr₁ (egcdi (succ y) (x mod (succ y)))) (succ y)) (int.mul (pr₂ (egcdi (succ y) (x mod (succ y)))) (x mod (succ y))) = gcd (succ y) (x mod (succ y)), calc int.add (int.mul (pr₁ (egcdi x (succ y))) x) (int.mul (pr₂ (egcdi x (succ y))) (succ y)) = int.add (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x) (int.mul (pr₂ (egcdi x (succ y))) (succ y)) : egcdi_succ_first x y ... = int.add (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x) (int.mul (int.sub (pr₁ (egcdi (succ y) (x mod succ y))) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x div succ y))) (succ y)) : egcdi_succ_second x y ... = int.add (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x) (int.sub (int.mul (pr₁ (egcdi (succ y) (x mod succ y))) (succ y)) (int.mul (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x div succ y) ) (succ y) ) ) : int.mul_sub_right_distrib ... = int.add (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x) (int.sub (int.mul (pr₁ (egcdi (succ y) (x mod succ y))) (succ y)) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (int.mul (x div succ y) (succ y)) ) ) : int.mul.assoc ... = int.add (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x) (int.sub (int.mul (pr₁ (egcdi (succ y) (x mod succ y))) (succ y)) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x - (x mod (succ y))) ) ) : divmul x (succ y) ... = int.add (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x) (int.sub (int.mul (pr₁ (egcdi (succ y) (x mod succ y))) (succ y)) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (int.sub x (x mod (succ y))) ) ) : sub_mod_eq_int_sub_mod x y ... = int.add (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x) (int.sub (int.mul (pr₁ (egcdi (succ y) (x mod succ y))) (succ y)) (int.sub (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x mod (succ y))) ) ) : int.mul_sub_left_distrib ... = int.add (int.mul (pr₁ (egcdi (succ y) (x mod succ y))) (succ y)) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x mod (succ y))) : int_rearrange (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) x) (int.mul (pr₁ (egcdi (succ y) (x mod succ y))) (succ y)) (int.mul (pr₂ (egcdi (succ y) (x mod succ y))) (x mod (succ y))) ... = gcd (succ y) (x mod (succ y)) : IH ... = gcd x (succ y) : (gcd_rec x (succ y))⁻¹ /- Base case -/ theorem egcdi_base (x : nat) : int.add (int.mul (pr₁ (egcdi x 0)) x) (int.mul (pr₂ (egcdi x 0)) 0) = gcd x 0 := calc int.add (int.mul (pr₁ (egcdi x 0)) x) (int.mul (pr₂ (egcdi x 0)) 0) = int.add (int.mul 1 x) (int.mul (pr₂ (egcdi x 0)) 0) : egcdi_zero_right_1 ... = int.add (int.mul 1 x) (int.mul 0 0) : egcdi_zero_right_2 ... = int.add (int.mul x 1) (int.mul 0 0) : int.mul.comm ... = int.add x (int.mul 0 0) : int.mul_one ... = int.add x 0 : int.mul_zero ... = x + 0 : int.of_nat_add_of_nat ... = x : nat.add_zero ... = gcd x 0 : gcd_zero_right /- Conclusion -/ private definition P (p : nat × nat) := (pr₁ (egcdi (pr₁ p) (pr₂ p))) * (pr₁ p) + (pr₂ (egcdi (pr₁ p) (pr₂ p))) * (pr₂ p) = gcd (pr₁ p) (pr₂ p) private theorem Pbase (x : nat) : P (x, 0) := egcdi_base x private theorem Prec (x y : nat) : P (succ y, x mod (succ y)) → P (x, succ y) := egcdi_ind x y private definition P.F (p₁ : nat × nat) : (Π p₂ : nat × nat, p₂ ≺ p₁ → P p₂) → P p₁ := prod.cases_on p₁ (λx y, nat.cases_on y (λ f, Pbase x) (λ y₁ (f : Πp₂, p₂ ≺ (x, succ y₁) → P p₂), (Prec x y₁) (f (succ y₁, x mod succ y₁) !gcd.lt.dec) )) private definition Bezouts1 (x y : ℕ) : (pr₁ (egcdi x y)) * x + (pr₂ (egcdi x y)) * y = gcd x y := fix P.F (pair x y) theorem BezoutsLemma : ∀ x y : ℕ, ∃ a b : ℤ, a * x + b * y = gcd x y := take x y : ℕ, let a := pr₁ (egcdi x y), b := pr₂ (egcdi x y) in have H : a * x + b * y = gcd x y, from Bezouts1 x y, exists.intro a (exists.intro b H) check BezoutsLemma
f19d93c64692482783b075e745e10d0c65f37e67
bab2ace2b909818f20ac125499a8dd88ce812721
/src/2020/functions/bijection_game.lean
98782cb547bca0136ace35d10c2cb4394e85b9ed
[]
no_license
LaplaceKorea/M40001_lean
8a3cd411fb821a7665132c09e436f02f674cc666
116e9ed1fadf59dc2e78376fca92026859a03bf2
refs/heads/master
1,693,347,195,820
1,635,517,507,000
1,635,517,507,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,723
lean
import tactic -- experiments with bijections /- #check infinite #print prefix infinite #print infinite #print int.infinite #check infinite.of_injective example : infinite ℕ := by apply_instance example : infinite ℤ := by show_term {apply_instance} #check rat.of_int -/ example : infinite ℚ := infinite.of_injective (coe : ℤ → ℚ) begin intros a b, intro h, exact (rat.coe_int_inj a b).mp h, end namespace countably_infinite example (a b : ℕ) : 2 * a = 2 * b → a = b := by begin intro h, apply mul_left_cancel' _ h, norm_num, end def bool_times_nat : bool × ℕ ≃ ℕ := { to_fun := λ bn, if bn.1 = tt then bn.2 * 2 else bn.2 * 2 + 1, inv_fun := λ d, ⟨d % 2 = 0, d / 2⟩, left_inv := begin intro bn, cases bn with b n, cases b, -- TODO -- make snippet { suffices : ¬(1 + 2 * n) % 2 = 0 ∧ (1 + 2 * n) / 2 = n, simpa [mul_comm, add_comm], have h : (1 + 2 * n) % 2 = 1, simp, split, { simp }, { have h2 := nat.mod_add_div (1 + 2 * n) 2, have h3 : 2 * ((1 + 2 * n) / 2) = 2 * n → (1 + 2 * n) / 2 = n := λ h, mul_left_cancel' _ h, simp * at *, cc } }, { simp } end , right_inv := begin intro n, suffices : ite (n % 2 = 0) (n / 2 * 2) (n / 2 * 2 + 1) = n, simpa, split_ifs, { have h2 := nat.mod_add_div n 2, rwa [h, zero_add, mul_comm] at h2, }, { have h2 := nat.mod_add_div n 2, rw [add_comm, mul_comm] at h2, convert h2, rcases nat.mod_two_eq_zero_or_one n with (h3 | h3), { contradiction }, { rw h3 }} end } -- example (X : Type) (h : X ≃ ℕ) : X ≃ X × bool := sorry end countably_infinite
952114f4a558355119157b7511995325b2078f9a
e9078bde91465351e1b354b353c9f9d8b8a9c8c2
/superseded/disc.hlean
655077cb660cd6ff00eb2103459c0d256dfde1e0
[ "Apache-2.0" ]
permissive
EgbertRijke/leansnippets
09fb7a9813477471532fbdd50c99be8d8fe3e6c4
1d9a7059784c92c0281fcc7ce66ac7b3619c8661
refs/heads/master
1,610,743,957,626
1,442,532,603,000
1,442,532,603,000
41,563,379
0
0
null
1,440,787,514,000
1,440,787,514,000
null
UTF-8
Lean
false
false
8,039
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 Declaration of the disc -/ import hit.circle cubical.squareover eq2 open quotient eq function bool circle equiv namespace disc inductive predisc_rel : bool → bool → Type := | Rl : predisc_rel tt tt definition predisc := quotient predisc_rel namespace predisc open predisc_rel definition b [constructor] : predisc := class_of predisc_rel tt definition e [constructor] : predisc := class_of predisc_rel ff definition l : b = b := eq_of_rel predisc_rel Rl definition f [unfold 1] : S¹ → predisc := circle.elim b l protected definition rec [recursor] [unfold 5] {P : predisc → Type} (Pb : P b) (Pe : P e) (Pl : Pb =[l] Pb) (x : predisc) : P x := begin induction x with c, { cases c, exact Pe, exact Pb}, { cases H, exact Pl}, end protected definition elim [recursor 5] [unfold 5] {P : Type} (Pb : P) (Pe : P) (Pl : Pb = Pb) (x : predisc) : P := predisc.rec Pb Pe (pathover_of_eq Pl) x theorem rec_l {P : predisc → Type} (Pb : P b) (Pe : P e) (Pl : Pb =[l] Pb) : apdo (predisc.rec Pb Pe Pl) l = Pl := !rec_eq_of_rel theorem elim_l {P : Type} {Pb Pe : P} (Pl : Pb = Pb) : ap (predisc.elim Pb Pe Pl) l = Pl := begin apply eq_of_fn_eq_fn_inv !(pathover_constant l), rewrite [▸*,-apdo_eq_pathover_of_eq_ap,↑predisc.elim,rec_l], end end predisc open predisc inductive disc_rel : predisc → predisc → Type := | Rf : Π(x : circle), disc_rel (f x) e open disc_rel definition disc := quotient disc_rel private definition i [constructor] : predisc → disc := class_of disc_rel definition base [constructor] : disc := i b definition aux [constructor] : disc := i e definition lp : base = base := ap i l definition fill' (x : S¹) : i (f x) = aux := eq_of_rel disc_rel (Rf x) definition fill : lp = idp := (ap02 i (elim_loop b l)⁻¹) ⬝ (ap_compose i f loop)⁻¹ ⬝ ap_weakly_constant fill' loop ⬝ !con.right_inv local attribute disc f i base aux lp fill' [reducible] protected definition elim [unfold 5] {P : Type} (Pb : P) (Pl : Pb = Pb) (Pf : Pl = idp) (x : disc) : P := begin induction x, { induction a, { exact Pb}, { exact Pb}, { exact Pl}}, { exact abstract begin induction H, induction x, { exact idpath Pb}, { unfold f, apply eq_pathover, apply hdeg_square, exact abstract ap_compose (predisc.elim Pb Pb Pl) f loop ⬝ ap _ !elim_loop ⬝ !elim_l ⬝ Pf ⬝ !ap_constant⁻¹ end} end end}, end local attribute disc.elim [reducible] definition elim_lp {P : Type} {Pb : P} {Pl : Pb = Pb} (Pf : Pl = idp) : ap (disc.elim Pb Pl Pf) lp = Pl := !ap_compose⁻¹ ⬝ !elim_l definition elim_fill'_base {P : Type} {Pb : P} {Pl : Pb = Pb} (Pf : Pl = idp) : ap (disc.elim Pb Pl Pf) (fill' circle.base) = idpath Pb := !elim_eq_of_rel theorem elim_fill {P : Type} {Pb : P} {Pl : Pb = Pb} (Pf : Pl = idp) : square (ap02 (disc.elim Pb Pl Pf) fill) Pf (elim_lp Pf) idp := begin esimp [fill,ap02], rewrite [+ap_con (ap _),▸*,-ap_compose (ap _) (ap i),+ap_inv], xrewrite [eq_top_of_square ((ap_compose_natural (disc.elim Pb Pl Pf) i (elim_loop b l))⁻¹ʰ⁻¹ᵛ ⬝h (ap_ap_compose (disc.elim Pb Pl Pf) i f loop)⁻¹ʰ⁻¹ᵛ ⬝h ap_ap_weakly_constant (disc.elim Pb Pl Pf) fill' loop ⬝h ap_con_right_inv_sq (disc.elim Pb Pl Pf) (fill' circle.base)), ↑[elim_lp]], apply whisker_tl, rewrite [ap_weakly_constant_eq,naturality_apdo_eq (λx, !elim_eq_of_rel) loop,▸*,↑elim_2,rec_loop, square_of_pathover_concato_eq,square_of_pathover_eq_concato, eq_of_square_vconcat_eq,eq_of_square_eq_vconcat], -- rewriting here with -- rewrite [to_right_inv !pathover_eq_equiv_square (hdeg_square (elim_1 P Pb Pl Pf))], -- takes ~11 seconds apply eq_vconcat, { apply ap (λx, _ ⬝ eq_con_inv_of_con_eq ((_ ⬝ x ⬝ _)⁻¹ ⬝ _) ⬝ _), transitivity _, apply ap eq_of_square, apply to_right_inv !eq_pathover_equiv_square (hdeg_square (elim_1 P Pb Pl Pf)), transitivity _, apply eq_of_square_hdeg_square, unfold elim_1, reflexivity}, rewrite [+con_inv,whisker_left_inv,+inv_inv,-whisker_right_inv, con.assoc (whisker_left _ _),con.assoc _ (whisker_right _ _),▸*, whisker_right_con_whisker_left _ !ap_constant], xrewrite [-con.assoc _ _ (whisker_right _ _)], rewrite [con.assoc _ _ (whisker_left _ _),idp_con_whisker_left,▸*, con.assoc _ !ap_constant⁻¹,con.left_inv], xrewrite [eq_con_inv_of_con_eq_whisker_left,▸*], rewrite [+con.assoc _ _ !con.right_inv,right_inv_eq_idp (elim_fill'_base Pf),↑[whisker_left]], xrewrite [con2_con_con2], rewrite [idp_con,↑elim_fill'_base,con.left_inv,whisker_right_inv,↑whisker_right], xrewrite [con.assoc _ _ (_ ◾ _)], rewrite [con.left_inv,▸*,-+con.assoc,con.assoc _⁻¹,↑[disc.elim,function.compose],con.left_inv, ▸*,↑b,con.left_inv,idp_con], apply square_of_eq, reflexivity end -- protected definition rec_of_elim_sigma {P : disc → Type} (Pb : P base) (Pl : Pb =[lp] Pb) -- (Pf : Pl =[fill] idpo) (x : disc) : Σx, P x := -- begin -- refine disc.elim _ _ _ x, -- { exact ⟨base, Pb⟩}, -- { exact sigma_eq lp Pl}, -- { exact sigma_eq2 (!sigma_eq_pr1 ⬝ fill) (!sigma_eq_pr2 ⬝o Pf)} -- end -- protected definition rec_of_elim {P : disc → Type} (Pb : P base) (Pl : Pb =[lp] Pb) -- (Pf : Pl =[fill] idpo) (x : disc) : P x := -- _ ▸ pr2 (disc.rec_of_elim_sigma Pb Pl Pf x) exit -- set_option pp.notation false protected definition rec {P : disc → Type} (Pb : P base) (Pl : Pb =[lp] Pb) (Pf : Pl =[fill] idpo) (x : disc) : P x := begin induction x, { induction a, { exact Pb}, { exact transport P (fill' circle.base) Pb}, { apply to_inv !(pathover_compose P), exact Pl}}, { exact abstract begin induction H, induction x, { esimp, apply pathover_tr}, { apply pathover_pathover_fl, apply sorry} end end}, end exit { esimp, induction x, { unfold f, apply pathover_tr}, { /-apply pathover_pathover_fl,-/ apply sorry}} protected definition elim [unfold 5] {P : Type} (Pb : P) (Pl : Pb = Pb) (Pf : Pl = idp) (x : disc) : P := begin induction x, { induction a, { exact Pb}, { exact Pb}, { exact Pl}}, { induction H, induction x, { exact idpath Pb}, { exact abstract begin unfold f, apply pathover_eq, apply hdeg_square, exact abstract ap_compose (predisc.elim Pb Pb Pl) f loop ⬝ ap _ !elim_loop ⬝ !elim_l ⬝ Pf ⬝ !ap_constant⁻¹ end end end}}, end example {P : disc → Type} (Pb : P base) (Pl1 : Pb =[loop1] Pb) (Pl2 : Pb =[loop2] Pb) (Pf : squareover P fill Pl1 Pl1 Pl2 Pl2) : disc.rec Pb Pl1 Pl2 Pf base = Pb := idp definition rec_loop1 {P : disc → Type} (Pb : P base) (Pl1 : Pb =[loop1] Pb) (Pl2 : Pb =[loop2] Pb) (Pf : squareover P fill Pl1 Pl1 Pl2 Pl2) : apdo (disc.rec Pb Pl1 Pl2 Pf) loop1 = Pl1 := sorry definition rec_loop2 {P : disc → Type} (Pb : P base) (Pl1 : Pb =[loop1] Pb) (Pl2 : Pb =[loop2] Pb) (Pf : squareover P fill Pl1 Pl1 Pl2 Pl2) : apdo (disc.rec Pb Pl1 Pl2 Pf) loop2 = Pl2 := sorry definition rec_fill {P : disc → Type} (Pb : P base) (Pl1 : Pb =[loop1] Pb) (Pl2 : Pb =[loop2] Pb) (Pf : squareover P fill Pl1 Pl1 Pl2 Pl2) : cubeover P rfl1 (apds (disc.rec Pb Pl1 Pl2 Pf) fill) Pf (vdeg_squareover !rec_loop2) (vdeg_squareover !rec_loop2) (vdeg_squareover !rec_loop1) (vdeg_squareover !rec_loop1) := sorry end disc exit
b9ca4cd27dcb3ead4cf7cd8f9a8dfa2babd3d73c
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/algebra/regular.lean
c8efd5a345d2cabc2ec2e51d30f3b122449288a1
[ "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
12,365
lean
/- Copyright (c) 2021 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import algebra.group import algebra.group_power.basic import algebra.iterate_hom /-! # Regular elements We introduce left-regular, right-regular and regular elements. By definition, a regular element in a commutative ring is a non-zero divisor. Lemma `is_regular_of_ne_zero` implies that every non-zero element of an integral domain is regular. Since it assumes that the ring is a `cancel_monoid_with_zero` it applies also, for instance, to `ℕ`. The lemmas in Section `mul_zero_class` show that the `0` element is (left/right-)regular if and only if the `mul_zero_class` is trivial. This is useful when figuring out stopping conditions for regular sequences: if `0` is ever an element of a regular sequence, then we can extend the sequence by adding one further `0`. The final goal is to develop part of the API to prove, eventually, results about non-zero-divisors. -/ variables {R : Type*} {a b : R} section has_mul variable [has_mul R] /-- A left-regular element is an element `c` such that multiplication on the left by `c` is injective on the left. -/ def is_left_regular (c : R) := function.injective ((*) c) /-- A right-regular element is an element `c` such that multiplication on the right by `c` is injective on the right. -/ def is_right_regular (c : R) := function.injective (* c) /-- A regular element is an element `c` such that multiplication by `c` both on the left and on the right is injective. -/ structure is_regular (c : R) : Prop := (left : is_left_regular c) (right : is_right_regular c) end has_mul section semigroup variable [semigroup R] /-- In a semigroup, the product of left-regular elements is left-regular. -/ lemma is_left_regular.mul (lra : is_left_regular a) (lrb : is_left_regular b) : is_left_regular (a * b) := show function.injective ((*) (a * b)), from (comp_mul_left a b) ▸ lra.comp lrb /-- In a semigroup, the product of right-regular elements is right-regular. -/ lemma is_right_regular.mul (rra : is_right_regular a) (rrb : is_right_regular b) : is_right_regular (a * b) := show function.injective (* (a * b)), from (comp_mul_right b a) ▸ rrb.comp rra /-- If an element `b` becomes left-regular after multiplying it on the left by a left-regular element, then `b` is left-regular. -/ lemma is_left_regular.of_mul (ab : is_left_regular (a * b)) : is_left_regular b := function.injective.of_comp (by rwa comp_mul_left a b) /-- An element is left-regular if and only if multiplying it on the left by a left-regular element is left-regular. -/ @[simp] lemma mul_is_left_regular_iff (b : R) (ha : is_left_regular a) : is_left_regular (a * b) ↔ is_left_regular b := ⟨λ ab, is_left_regular.of_mul ab, λ ab, is_left_regular.mul ha ab⟩ /-- If an element `b` becomes right-regular after multiplying it on the right by a right-regular element, then `b` is right-regular. -/ lemma is_right_regular.of_mul (ab : is_right_regular (b * a)) : is_right_regular b := begin refine λ x y xy, ab (_ : x * (b * a) = y * (b * a)), rw [← mul_assoc, ← mul_assoc], exact congr_fun (congr_arg has_mul.mul xy) a, end /-- An element is right-regular if and only if multiplying it on the right with a right-regular element is right-regular. -/ @[simp] lemma mul_is_right_regular_iff (b : R) (ha : is_right_regular a) : is_right_regular (b * a) ↔ is_right_regular b := ⟨λ ab, is_right_regular.of_mul ab, λ ab, is_right_regular.mul ab ha⟩ /-- Two elements `a` and `b` are regular if and only if both products `a * b` and `b * a` are regular. -/ lemma is_regular_mul_and_mul_iff : is_regular (a * b) ∧ is_regular (b * a) ↔ is_regular a ∧ is_regular b := begin refine ⟨_, _⟩, { rintros ⟨ab, ba⟩, exact ⟨⟨is_left_regular.of_mul ba.left, is_right_regular.of_mul ab.right⟩, ⟨is_left_regular.of_mul ab.left, is_right_regular.of_mul ba.right⟩⟩ }, { rintros ⟨ha, hb⟩, exact ⟨⟨(mul_is_left_regular_iff _ ha.left).mpr hb.left, (mul_is_right_regular_iff _ hb.right).mpr ha.right⟩, ⟨(mul_is_left_regular_iff _ hb.left).mpr ha.left, (mul_is_right_regular_iff _ ha.right).mpr hb.right⟩⟩ } end /-- The "most used" implication of `mul_and_mul_iff`, with split hypotheses, instead of `∧`. -/ lemma is_regular.and_of_mul_of_mul (ab : is_regular (a * b)) (ba : is_regular (b * a)) : is_regular a ∧ is_regular b := is_regular_mul_and_mul_iff.mp ⟨ab, ba⟩ end semigroup section monoid variable [monoid R] /-- Any power of a left-regular element is left-regular. -/ lemma is_left_regular.pow (n : ℕ) (rla : is_left_regular a) : is_left_regular (a ^ n) := by simp [is_left_regular, ← mul_left_iterate, rla.iterate n] /-- Any power of a right-regular element is right-regular. -/ lemma is_right_regular.pow (n : ℕ) (rra : is_right_regular a) : is_right_regular (a ^ n) := by simp [is_right_regular, ← mul_right_iterate, rra.iterate n] /-- Any power of a regular element is regular. -/ lemma is_regular.pow (n : ℕ) (ra : is_regular a) : is_regular (a ^ n) := ⟨is_left_regular.pow n ra.left, is_right_regular.pow n ra.right⟩ /-- An element `a` is left-regular if and only if a positive power of `a` is left-regular. -/ lemma is_left_regular.pow_iff {n : ℕ} (n0 : 0 < n) : is_left_regular (a ^ n) ↔ is_left_regular a := begin refine ⟨_, is_left_regular.pow n⟩, rw [← nat.succ_pred_eq_of_pos n0, pow_succ'], exact is_left_regular.of_mul, end /-- An element `a` is right-regular if and only if a positive power of `a` is right-regular. -/ lemma is_right_regular.pow_iff {n : ℕ} (n0 : 0 < n) : is_right_regular (a ^ n) ↔ is_right_regular a := begin refine ⟨_, is_right_regular.pow n⟩, rw [← nat.succ_pred_eq_of_pos n0, pow_succ], exact is_right_regular.of_mul, end /-- An element `a` is regular if and only if a positive power of `a` is regular. -/ lemma is_regular.pow_iff {n : ℕ} (n0 : 0 < n) : is_regular (a ^ n) ↔ is_regular a := ⟨λ h, ⟨(is_left_regular.pow_iff n0).mp h.left, (is_right_regular.pow_iff n0).mp h.right⟩, λ h, ⟨is_left_regular.pow n h.left, is_right_regular.pow n h.right⟩⟩ end monoid section mul_zero_class variables [mul_zero_class R] /-- The element `0` is left-regular if and only if `R` is trivial. -/ lemma is_left_regular.subsingleton (h : is_left_regular (0 : R)) : subsingleton R := ⟨λ a b, h $ eq.trans (zero_mul a) (zero_mul b).symm⟩ /-- The element `0` is right-regular if and only if `R` is trivial. -/ lemma is_right_regular.subsingleton (h : is_right_regular (0 : R)) : subsingleton R := ⟨λ a b, h $ eq.trans (mul_zero a) (mul_zero b).symm⟩ /-- The element `0` is regular if and only if `R` is trivial. -/ lemma is_regular.subsingleton (h : is_regular (0 : R)) : subsingleton R := h.left.subsingleton /-- The element `0` is left-regular if and only if `R` is trivial. -/ lemma is_left_regular_zero_iff_subsingleton : is_left_regular (0 : R) ↔ subsingleton R := begin refine ⟨λ h, h.subsingleton, _⟩, intros H a b h, exact @subsingleton.elim _ H a b end /-- In a non-trivial `mul_zero_class`, the `0` element is not left-regular. -/ lemma not_is_left_regular_zero_iff : ¬ is_left_regular (0 : R) ↔ nontrivial R := begin rw [nontrivial_iff, not_iff_comm, is_left_regular_zero_iff_subsingleton, subsingleton_iff], push_neg, exact iff.rfl end /-- The element `0` is right-regular if and only if `R` is trivial. -/ lemma is_right_regular_zero_iff_subsingleton : is_right_regular (0 : R) ↔ subsingleton R := begin refine ⟨λ h, h.subsingleton, _⟩, intros H a b h, exact @subsingleton.elim _ H a b end /-- In a non-trivial `mul_zero_class`, the `0` element is not right-regular. -/ lemma not_is_right_regular_zero_iff : ¬ is_right_regular (0 : R) ↔ nontrivial R := begin rw [nontrivial_iff, not_iff_comm, is_right_regular_zero_iff_subsingleton, subsingleton_iff], push_neg, exact iff.rfl end /-- The element `0` is regular if and only if `R` is trivial. -/ lemma is_regular_iff_subsingleton : is_regular (0 : R) ↔ subsingleton R := ⟨λ h, h.left.subsingleton, λ h, ⟨is_left_regular_zero_iff_subsingleton.mpr h, is_right_regular_zero_iff_subsingleton.mpr h⟩⟩ /-- A left-regular element of a `nontrivial` `mul_zero_class` is non-zero. -/ lemma is_left_regular.ne_zero [nontrivial R] (la : is_left_regular a) : a ≠ 0 := begin rintro rfl, rcases exists_pair_ne R with ⟨x, y, xy⟩, refine xy (la _), rw [zero_mul, zero_mul] end /-- A right-regular element of a `nontrivial` `mul_zero_class` is non-zero. -/ lemma is_right_regular.ne_zero [nontrivial R] (ra : is_right_regular a) : a ≠ 0 := begin rintro rfl, rcases exists_pair_ne R with ⟨x, y, xy⟩, refine xy (ra (_ : x * 0 = y * 0)), rw [mul_zero, mul_zero] end /-- A regular element of a `nontrivial` `mul_zero_class` is non-zero. -/ lemma is_regular.ne_zero [nontrivial R] (la : is_regular a) : a ≠ 0 := la.left.ne_zero /-- In a non-trivial ring, the element `0` is not left-regular -- with typeclasses. -/ lemma not_is_left_regular_zero [nR : nontrivial R] : ¬ is_left_regular (0 : R) := not_is_left_regular_zero_iff.mpr nR /-- In a non-trivial ring, the element `0` is not right-regular -- with typeclasses. -/ lemma not_is_right_regular_zero [nR : nontrivial R] : ¬ is_right_regular (0 : R) := not_is_right_regular_zero_iff.mpr nR /-- In a non-trivial ring, the element `0` is not regular -- with typeclasses. -/ lemma not_is_regular_zero [nontrivial R] : ¬ is_regular (0 : R) := λ h, is_regular.ne_zero h rfl end mul_zero_class section comm_semigroup variable [comm_semigroup R] /-- A product is regular if and only if the factors are. -/ lemma is_regular_mul_iff : is_regular (a * b) ↔ is_regular a ∧ is_regular b := begin refine iff.trans _ is_regular_mul_and_mul_iff, refine ⟨λ ab, ⟨ab, by rwa mul_comm⟩, λ rab, rab.1⟩ end end comm_semigroup section monoid variables [monoid R] /-- In a monoid, `1` is regular. -/ lemma is_regular_one : is_regular (1 : R) := ⟨λ a b ab, (one_mul a).symm.trans (eq.trans ab (one_mul b)), λ a b ab, (mul_one a).symm.trans (eq.trans ab (mul_one b))⟩ /-- An element admitting a left inverse is left-regular. -/ lemma is_left_regular_of_mul_eq_one (h : b * a = 1) : is_left_regular a := @is_left_regular.of_mul R _ a _ (by { rw h, exact is_regular_one.left }) /-- An element admitting a right inverse is right-regular. -/ lemma is_right_regular_of_mul_eq_one (h : a * b = 1) : is_right_regular a := @is_right_regular.of_mul R _ a _ (by { rw h, exact is_regular_one.right }) /-- If `R` is a monoid, an element in `units R` is regular. -/ lemma units.is_regular (a : units R) : is_regular (a : R) := ⟨is_left_regular_of_mul_eq_one a.inv_mul, is_right_regular_of_mul_eq_one a.mul_inv⟩ /-- A unit in a monoid is regular. -/ lemma is_unit.is_regular (ua : is_unit a) : is_regular a := begin rcases ua with ⟨a, rfl⟩, exact units.is_regular a, end end monoid section left_or_right_cancel_semigroup /-- Elements of a left cancel semigroup are left regular. -/ lemma is_left_regular_of_left_cancel_semigroup [left_cancel_semigroup R] (g : R) : is_left_regular g := mul_right_injective g /-- Elements of a right cancel semigroup are right regular. -/ lemma is_right_regular_of_right_cancel_semigroup [right_cancel_semigroup R] (g : R) : is_right_regular g := mul_left_injective g end left_or_right_cancel_semigroup section cancel_monoid variables [cancel_monoid R] /-- Elements of a cancel monoid are regular. Cancel semigroups do not appear to exist. -/ lemma is_regular_of_cancel_monoid (g : R) : is_regular g := ⟨mul_right_injective g, mul_left_injective g⟩ end cancel_monoid section cancel_monoid_with_zero variables [cancel_monoid_with_zero R] /-- Non-zero elements of an integral domain are regular. -/ lemma is_regular_of_ne_zero (a0 : a ≠ 0) : is_regular a := ⟨λ b c, (mul_right_inj' a0).mp, λ b c, (mul_left_inj' a0).mp⟩ /-- In a non-trivial integral domain, an element is regular iff it is non-zero. -/ lemma is_regular_iff_ne_zero [nontrivial R] : is_regular a ↔ a ≠ 0 := ⟨is_regular.ne_zero, is_regular_of_ne_zero⟩ end cancel_monoid_with_zero
5db7b9fe2240326c89fca93ade149ee8d3b9f2cd
bcdb337a071a6642a282053e6c285336b9716772
/Tensor/Basic.lean
3eedd4f982ef855417a15ea0a2442e0721d69e1a
[]
no_license
kovach/lean4-sparse-matrix-test
87a8b3a0b7785976eccfaf69b0d289940549f446
be9f6029883c88bf1f7588a0e6d3cde033368809
refs/heads/master
1,678,513,001,774
1,614,027,522,000
1,614,027,522,000
341,329,194
1
0
null
null
null
null
UTF-8
Lean
false
false
5,120
lean
-- Scott Kovach import Init.Data.Array.Subarray import Init.Data.Float import Std.Data.RBMap inductive EitherOr (α β : Type _) | left : α → EitherOr α β | right : β → EitherOr α β | both : α → β → EitherOr α β namespace EitherOr instance [Repr α] [Repr β] : Repr (EitherOr α β) where reprPrec x _ := match x with | EitherOr.left a => "left " ++ repr a | EitherOr.right a => "right " ++ repr a | EitherOr.both a b => "both " ++ repr a ++ " " ++ repr b def reduce (unit : α) (op : α → α → α) : EitherOr α α → α | EitherOr.left x => op x unit | EitherOr.right y => op unit y | EitherOr.both x y => op x y end EitherOr open EitherOr -- could have used `partial def` def mergeUnion [DecidableEq β] [le : HasLessEq β] [DecidableRel le.LessEq] : Nat → List (β × α) → List (β × α₁) → Array (β × EitherOr α α₁) → Array (β × EitherOr α α₁) | 0, _, _, acc => acc | (n+1), [], ys, acc => ys.foldl (λ acc (i, a) => acc.push (i, right a)) acc | (n+1), xs, [], acc => xs.foldl (λ acc (i, a) => acc.push (i, left a)) acc | (n+1), l1@(⟨i, x⟩ :: xs), l2@(⟨j, y⟩ :: ys), acc => if i = j then mergeUnion n xs ys (acc.push ⟨i, both x y⟩) else if i ≤ j then mergeUnion n xs l2 (acc.push ⟨i, left x⟩) else mergeUnion n l1 ys (acc.push (j, right y)) def scaleArray [HMul α α₁ α₁] (r: α) (u : Array α₁) : Array α₁ := u.map (λ x => r * x) instance [Mul α] : HMul α (Array α) (Array α) := ⟨ scaleArray ⟩ @[reducible] def SparseVec ι α := Array (ι × α) namespace SparseVec -- needed without `reducible` attribute (also needs ForIn) -- instance [r : Repr (Array (ι × α))] : Repr (SparseVec ι α) := ⟨ r.reprPrec ⟩ -- instance [r : ToString (Array (ι × α))] : ToString (SparseVec ι α) := ⟨ r.toString ⟩ -- todo: use algebraic class instead of OfNat def sparseAdd [s : Add α] [OfNat α 0] [DecidableEq ι] [le : HasLessEq ι] [DecidableRel le.LessEq] (xs ys : SparseVec ι α ) : SparseVec ι α := do let mut res := Array.empty for (coord, values) in mergeUnion (xs.size + ys.size) xs.toList ys.toList Array.empty do res := res.push (coord, EitherOr.reduce 0 s.add values) return res #eval sparseAdd #[(1,3), (5,4)] #[(1,5), (2,1)] def map (f : α → β) (v : SparseVec ι α) : SparseVec ι β := Array.map (λ (c, x) => (c, f x)) v def ofDenseList : List α → SparseVec Nat α | values => Prod.snd $ values.foldr (init := (0, #[])) λ v (n, acc) => (n+1, acc.push (n,v)) instance [HMul α α₁ α₁] : HMul α (SparseVec ι α₁) (SparseVec ι α₁) where hMul r v := Array.map (λ (c, x) => (c, r * x)) v instance [OfNat α 0] [le: HasLessEq ι] [DecidableEq ι] [DecidableRel le.LessEq] [Add α] : Add (SparseVec ι α) where add u v := sparseAdd u v instance : OfNat (SparseVec ι α) 0 where ofNat := #[] instance : Inhabited (SparseVec ι α) := ⟨ #[] ⟩ def mergeIntersection [DecidableEq β] [le : HasLessEq β] [DecidableRel le.LessEq] (n : Nat) (xs : List (β × α₀)) (ys : List (β × α₁)) : Array (β × (α₀ × α₁)) := do let rec step | 0, _, _, acc => acc | (n+1), [], ys, acc => acc | (n+1), xs, [], acc => acc | (n+1), l1@(⟨i, x⟩ :: xs), l2@(⟨j, y⟩ :: ys), acc => if i = j then -- let _ := unsafeIO (IO.print 22) step n xs ys (acc.push (i, (x, y))) else if i ≤ j then step n xs l2 acc else step n l1 ys acc step n xs ys #[] def hMulDot [s : HMul α α₁ α₁] [Add α₁] [OfNat α₁ 0] [DecidableEq ι] [HasLessEq ι] [le : HasLessEq ι] [DecidableRel le.LessEq] (xs : SparseVec ι α) (ys : SparseVec ι α₁) : α₁ := do let mut res := 0 for (_, (scalar, vector)) in mergeIntersection (xs.size + ys.size) xs.toList ys.toList do res := res + scalar * vector return res def linearCombinationOfRows [DecidableEq ι] [le : HasLessEq ι] [DecidableRel le.LessEq] [s : HMul α β β] [Add β] [OfNat β 0] (A : SparseVec Nat (SparseVec ι α)) (B : SparseVec ι β) : SparseVec Nat β := do A.map (λ row => hMulDot (s := s) row B) instance [DecidableEq ι] [le : HasLessEq ι] [DecidableRel le.LessEq] [HMul α β β] [Add β] [OfNat β 0] : HMul (SparseVec Nat (SparseVec ι α)) (SparseVec ι β) (SparseVec Nat β) := ⟨ linearCombinationOfRows ⟩ def sparseTranspose (A : SparseVec Nat (SparseVec Nat α)) : SparseVec Nat (SparseVec Nat α) := do let mut temp := Std.mkRBMap Nat (SparseVec Nat α) (. < .) for (rowC, row) in A do for (coord, value) in row do temp := temp.insert coord (temp.findD coord #[] |>.push (rowC, value)) return temp.fold (fun ps k v => ps.push (k, v)) #[] postfix:max "ᵀ" => sparseTranspose def printMat (mat : SparseVec Nat (SparseVec Nat Float)) : IO Unit := do for (i, row) in mat do for (j, value) in row do IO.println s!"{i} {j} {value}" end SparseVec def Matrix := SparseVec Nat (SparseVec Nat Float)
11c25560dc68841df95781480e29b5efb5fdf012
9b9a16fa2cb737daee6b2785474678b6fa91d6d4
/src/algebra/field.lean
ca450fa40808b0665763b2d3af9fa33671e47c9a
[ "Apache-2.0" ]
permissive
johoelzl/mathlib
253f46daa30b644d011e8e119025b01ad69735c4
592e3c7a2dfbd5826919b4605559d35d4d75938f
refs/heads/master
1,625,657,216,488
1,551,374,946,000
1,551,374,946,000
98,915,829
0
0
Apache-2.0
1,522,917,267,000
1,501,524,499,000
Lean
UTF-8
Lean
false
false
8,114
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import algebra.ring logic.basic open set universe u variables {α : Type u} instance division_ring.to_domain [s : division_ring α] : domain α := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, classical.by_contradiction $ λ hn, division_ring.mul_ne_zero (mt or.inl hn) (mt or.inr hn) h ..s } namespace units variables [division_ring α] {a b : α} /-- Embed an element of a division ring into the unit group. By combining this function with the operations on units, or the `/ₚ` operation, it is possible to write a division as a partial function with three arguments. -/ def mk0 (a : α) (ha : a ≠ 0) : units α := ⟨a, a⁻¹, mul_inv_cancel ha, inv_mul_cancel ha⟩ @[simp] theorem inv_eq_inv (u : units α) : (↑u⁻¹ : α) = u⁻¹ := (mul_left_inj u).1 $ by rw [units.mul_inv, mul_inv_cancel]; apply units.ne_zero @[simp] theorem mk0_val (ha : a ≠ 0) : (mk0 a ha : α) = a := rfl @[simp] theorem mk0_inv (ha : a ≠ 0) : ((mk0 a ha)⁻¹ : α) = a⁻¹ := rfl @[simp] lemma units.mk0_inj [field α] {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : units.mk0 a ha = units.mk0 b hb ↔ a = b := ⟨λ h, by injection h, λ h, units.ext h⟩ end units section division_ring variables [s : division_ring α] {a b c : α} include s lemma div_eq_mul_inv : a / b = a * b⁻¹ := rfl attribute [simp] div_one zero_div div_self theorem divp_eq_div (a : α) (u : units α) : a /ₚ u = a / u := congr_arg _ $ units.inv_eq_inv _ @[simp] theorem divp_mk0 (a : α) {b : α} (hb : b ≠ 0) : a /ₚ units.mk0 b hb = a / b := divp_eq_div _ _ lemma inv_div (ha : a ≠ 0) (hb : b ≠ 0) : (a / b)⁻¹ = b / a := (mul_inv_eq (inv_ne_zero hb) ha).trans $ by rw division_ring.inv_inv hb; refl lemma inv_div_left (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ / b = (b * a)⁻¹ := (mul_inv_eq ha hb).symm lemma neg_inv (h : a ≠ 0) : - a⁻¹ = (- a)⁻¹ := by rw [inv_eq_one_div, inv_eq_one_div, div_neg_eq_neg_div _ h] lemma division_ring.inv_comm_of_comm (h : a ≠ 0) (H : a * b = b * a) : a⁻¹ * b = b * a⁻¹ := begin have : a⁻¹ * (b * a) * a⁻¹ = a⁻¹ * (a * b) * a⁻¹ := congr_arg (λ x:α, a⁻¹ * x * a⁻¹) H.symm, rwa [mul_assoc, mul_assoc, mul_inv_cancel, mul_one, ← mul_assoc, inv_mul_cancel, one_mul] at this; exact h end lemma div_ne_zero (ha : a ≠ 0) (hb : b ≠ 0) : a / b ≠ 0 := division_ring.mul_ne_zero ha (inv_ne_zero hb) lemma div_ne_zero_iff (hb : b ≠ 0) : a / b ≠ 0 ↔ a ≠ 0 := ⟨mt (λ h, by rw [h, zero_div]), λ ha, div_ne_zero ha hb⟩ lemma div_eq_zero_iff (hb : b ≠ 0) : a / b = 0 ↔ a = 0 := by haveI := classical.prop_decidable; exact not_iff_not.1 (div_ne_zero_iff hb) lemma add_div (a b c : α) : (a + b) / c = a / c + b / c := (div_add_div_same _ _ _).symm lemma div_right_inj (hc : c ≠ 0) : a / c = b / c ↔ a = b := by rw [← divp_mk0 _ hc, ← divp_mk0 _ hc, divp_right_inj] lemma sub_div (a b c : α) : (a - b) / c = a / c - b / c := (div_sub_div_same _ _ _).symm lemma division_ring.inv_inj (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ = b⁻¹ ↔ a = b := ⟨λ h, by rw [← division_ring.inv_inv ha, ← division_ring.inv_inv hb, h], congr_arg (λx,x⁻¹)⟩ lemma division_ring.inv_eq_iff (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ = b ↔ b⁻¹ = a := by rw [← division_ring.inv_inj (inv_ne_zero ha) hb, eq_comm, division_ring.inv_inv ha] lemma div_neg (a : α) (hb : b ≠ 0) : a / -b = -(a / b) := by rw [← division_ring.neg_div_neg_eq _ (neg_ne_zero.2 hb), neg_neg, neg_div] lemma div_eq_iff_mul_eq (hb : b ≠ 0) : a / b = c ↔ c * b = a := ⟨λ h, by rw [← h, div_mul_cancel _ hb], λ h, by rw [← h, mul_div_cancel _ hb]⟩ end division_ring instance field.to_integral_domain [F : field α] : integral_domain α := { ..F, ..division_ring.to_domain } section variables [field α] {a b c d : α} lemma div_eq_inv_mul : a / b = b⁻¹ * a := mul_comm _ _ lemma inv_add_inv {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ + b⁻¹ = (a + b) / (a * b) := by rw [inv_eq_one_div, inv_eq_one_div, one_div_add_one_div ha hb] lemma inv_sub_inv {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ - b⁻¹ = (b - a) / (a * b) := by rw [inv_eq_one_div, inv_eq_one_div, div_sub_div _ _ ha hb, one_mul, mul_one] lemma mul_div_right_comm (a b c : α) : (a * b) / c = (a / c) * b := (div_mul_eq_mul_div _ _ _).symm lemma mul_comm_div (a b c : α) : (a / b) * c = a * (c / b) := by rw [← mul_div_assoc, mul_div_right_comm] lemma div_mul_comm (a b c : α) : (a / b) * c = (c / b) * a := by rw [div_mul_eq_mul_div, mul_comm, mul_div_right_comm] lemma mul_div_comm (a b c : α) : a * (b / c) = b * (a / c) := by rw [← mul_div_assoc, mul_comm, mul_div_assoc] lemma field.div_right_comm (a : α) (hb : b ≠ 0) (hc : c ≠ 0) : (a / b) / c = (a / c) / b := by rw [field.div_div_eq_div_mul _ hb hc, field.div_div_eq_div_mul _ hc hb, mul_comm] lemma field.div_div_div_cancel_right (a : α) (hb : b ≠ 0) (hc : c ≠ 0) : (a / c) / (b / c) = a / b := by rw [field.div_div_eq_mul_div _ hb hc, div_mul_cancel _ hc] lemma field.div_mul_div_cancel (a : α) (hb : b ≠ 0) (hc : c ≠ 0) : (a / c) * (c / b) = a / b := by rw [← mul_div_assoc, div_mul_cancel _ hc] lemma div_eq_div_iff (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d ↔ a * d = c * b := (domain.mul_right_inj (mul_ne_zero' hb hd)).symm.trans $ by rw [← mul_assoc, div_mul_cancel _ hb, ← mul_assoc, mul_right_comm, div_mul_cancel _ hd] lemma field.div_div_cancel (ha : a ≠ 0) (hb : b ≠ 0) : a / (a / b) = b := by rw [div_eq_mul_inv, inv_div ha hb, mul_div_cancel' _ ha] end section variables [discrete_field α] {a b c : α} attribute [simp] inv_zero div_zero lemma div_right_comm (a b c : α) : (a / b) / c = (a / c) / b := if b0 : b = 0 then by simp only [b0, div_zero, zero_div] else if c0 : c = 0 then by simp only [c0, div_zero, zero_div] else field.div_right_comm _ b0 c0 lemma div_div_div_cancel_right (a b : α) (hc : c ≠ 0) : (a / c) / (b / c) = a / b := if b0 : b = 0 then by simp only [b0, div_zero, zero_div] else field.div_div_div_cancel_right _ b0 hc lemma div_mul_div_cancel (a : α) (hb : b ≠ 0) (hc : c ≠ 0) : (a / c) * (c / b) = a / b := if b0 : b = 0 then by simp only [b0, div_zero, mul_zero] else field.div_mul_div_cancel _ b0 hc lemma div_div_cancel (ha : a ≠ 0) : a / (a / b) = b := if b0 : b = 0 then by simp only [b0, div_zero] else field.div_div_cancel ha b0 end @[reducible] def is_field_hom {α β} [division_ring α] [division_ring β] (f : α → β) := is_ring_hom f namespace is_field_hom open is_ring_hom section variables {β : Type*} [division_ring α] [division_ring β] variables (f : α → β) [is_field_hom f] {x y : α} lemma map_ne_zero : f x ≠ 0 ↔ x ≠ 0 := ⟨mt $ λ h, h.symm ▸ map_zero f, λ x0 h, one_ne_zero $ calc 1 = f (x * x⁻¹) : by rw [mul_inv_cancel x0, map_one f] ... = 0 : by rw [map_mul f, h, zero_mul]⟩ lemma map_eq_zero : f x = 0 ↔ x = 0 := by haveI := classical.dec; exact not_iff_not.1 (map_ne_zero f) lemma map_inv' (h : x ≠ 0) : f x⁻¹ = (f x)⁻¹ := (domain.mul_left_inj ((map_ne_zero f).2 h)).1 $ by rw [mul_inv_cancel ((map_ne_zero f).2 h), ← map_mul f, mul_inv_cancel h, map_one f] lemma map_div' (h : y ≠ 0) : f (x / y) = f x / f y := (map_mul f).trans $ congr_arg _ $ map_inv' f h lemma injective : function.injective f := (is_add_group_hom.injective_iff _).2 (λ a ha, classical.by_contradiction $ λ ha0, by simpa [ha, is_ring_hom.map_mul f, is_ring_hom.map_one f, zero_ne_one] using congr_arg f (mul_inv_cancel ha0)) end section variables {β : Type*} [discrete_field α] [discrete_field β] variables (f : α → β) [is_field_hom f] {x y : α} lemma map_inv : f x⁻¹ = (f x)⁻¹ := classical.by_cases (by rintro rfl; simp only [map_zero f, inv_zero]) (map_inv' f) lemma map_div : f (x / y) = f x / f y := (map_mul f).trans $ congr_arg _ $ map_inv f end end is_field_hom
bf2c09e146dd7602bf534a077eaa5fe7d6f59a6d
eb9357a70318e50e095b58730bebfe0cffee457f
/lean/love12_basic_mathematical_structures_demo.lean
fa675c1b29b951fa926cef8af221064d9299b193
[]
no_license
Vierkantor/logical_verification_2021
7485dd916953131d501760f023d5b30fbb74d36a
9500b9c194e22a9ab4067321cfed7a1f445afcfc
refs/heads/main
1,692,560,845,086
1,624,721,275,000
1,624,721,275,000
416,354,079
0
0
null
null
null
null
UTF-8
Lean
false
false
12,426
lean
import .love05_inductive_predicates_demo /-! # LoVe Demo 12: Basic Mathematical Structures We introduce definitions and proofs about basic mathematical structures such as groups, fields, and linear orders. -/ set_option pp.beta true set_option pp.generalized_field_notation false namespace LoVe /-! ## Type Classes over a Single Binary Operator Mathematically, a __group__ is a set `G` with a binary operator `• : G × G → G` with the following properties, called __group axioms__: * Associativity: For all `a, b, c ∈ G`, we have `(a • b) • c = a • (b • c)`. * Identity element: There exists an element `e ∈ G` such that for all `a ∈ G`, we have `e • a = a`. * Inverse element: For each `a ∈ G`, there exists an inverse element `inv(a) ∈ G` such that `inv(a) • a = e`. Examples of groups are * `ℤ` with `+`; * `ℝ` with `+`; * `ℝ \ {0}` with `*`. In Lean, a type class for groups can be defined as follows: -/ namespace monolithic_group @[class] structure group (α : Type) := (mul : α → α → α) (one : α) (inv : α → α) (mul_assoc : ∀a b c, mul (mul a b) c = mul a (mul b c)) (one_mul : ∀a, mul one a = a) (mul_left_inv : ∀a, mul (inv a) a = one) end monolithic_group /-! In Lean, however, group is part of a larger hierarchy of algebraic structures: Type class | Properties | Examples ------------------------ | -----------------------------------------|------------------- `semigroup` | associativity of `*` | `ℝ`, `ℚ`, `ℤ`, `ℕ` `monoid` | `semigroup` with unit `1` | `ℝ`, `ℚ`, `ℤ`, `ℕ` `left_cancel_semigroup` | `semigroup` with `c * a = c * b → a = b` | `right_cancel_semigroup` | `semigroup` with `a * c = b * c → a = b` | `group` | `monoid` with inverse `⁻¹` | Most of these structures have commutative versions: `comm_semigroup`, `comm_monoid`, `comm_group`. The __multiplicative__ structures (over `*`, `1`, `⁻¹`) are copied to produce __additive__ versions (over `+`, `0`, `-`): Type class | Properties | Examples ---------------------------- | ---------------------------------------------|------------------- `add_semigroup` | associativity of `+` | `ℝ`, `ℚ`, `ℤ`, `ℕ` `add_monoid` | `add_semigroup` with unit `0` | `ℝ`, `ℚ`, `ℤ`, `ℕ` `add_left_cancel_semigroup` | `add_semigroup` with `c + a = c + b → a = b` | `ℝ`, `ℚ`, `ℤ`, `ℕ` `add_right_cancel_semigroup` | `add_semigroup` with `a + c = b + c → a = b` | `ℝ`, `ℚ`, `ℤ`, `ℕ` `add_group` | `add_monoid` with inverse `-` | `ℝ`, `ℚ`, `ℤ` -/ #print group #print add_group /-! Let us define our own type, of integers modulo 2, and register it as an additive group. -/ inductive int2 : Type | zero | one def int2.add : int2 → int2 → int2 | int2.zero a := a | a int2.zero := a | int2.one int2.one := int2.zero @[instance] def int2.add_group : add_group int2 := { add := int2.add, add_assoc := by intros a b c; cases' a; cases' b; cases' c; refl, zero := int2.zero, zero_add := by intro a; cases' a; refl, add_zero := by intro a; cases' a; refl, neg := λa, a, add_left_neg := by intro a; cases' a; refl } #reduce int2.one + 0 - 0 - int2.one lemma int2.add_right_neg: ∀a : int2, a + - a = 0 := add_right_neg /-! Another example: Lists are an `add_monoid`: -/ @[instance] def list.add_monoid {α : Type} : add_monoid (list α) := { zero := [], add := (++), add_assoc := list.append_assoc, zero_add := list.nil_append, add_zero := list.append_nil } /-! ## Type Classes with Two Binary Operators Mathematically, a __field__ is a set `F` such that * `F` forms a commutative group under an operator `+`, called addition, with identity element `0`. * `F\{0}` forms a commutative group under an operator `*`, called multiplication. * Multiplication distributes over addition—i.e., `a * (b + c) = a * b + a * c` for all `a, b, c ∈ F`. In Lean, fields are also part of a larger hierarchy: Type class | Properties | Examples -----------------|-----------------------------------------------------|------------------- `semiring` | `monoid` and `add_comm_monoid` with distributivity | `ℝ`, `ℚ`, `ℤ`, `ℕ` `comm_semiring` | `semiring` with commutativity of `*` | `ℝ`, `ℚ`, `ℤ`, `ℕ` `ring` | `monoid` and `add_comm_group` with distributivity | `ℝ`, `ℚ`, `ℤ` `comm_ring` | `ring` with commutativity of `*` | `ℝ`, `ℚ`, `ℤ` `division_ring` | `ring` with multiplicative inverse `⁻¹` | `ℝ`, `ℚ` `field` | `division_ring` with commutativity of `*` | `ℝ`, `ℚ` `discrete_field` | `field` with decidable equality and `∀n, n / 0 = 0` | `ℝ`, `ℚ` -/ #print field /-! Let us continue with our example: -/ def int2.mul : int2 → int2 → int2 | int2.one a := a | a int2.one := a | int2.zero int2.zero := int2.zero @[instance] def int2.field : field int2 := { one := int2.one, mul := int2.mul, inv := λa, a, add_comm := by intros a b; cases' a; cases' b; refl, exists_pair_ne := by apply exists.intro int2.zero; apply exists.intro int2.one; finish, one_mul := by intros a; cases' a; refl, mul_one := by intros a; cases' a; refl, mul_inv_cancel := by intros a h; cases' a; finish, inv_zero := by refl, mul_assoc := by intros a b c; cases' a; cases' b; cases' c; refl, mul_comm := by intros a b; cases' a; cases' b; refl, left_distrib := by intros a b c; cases' a; cases' b; cases' c; refl, right_distrib := by intros a b c; cases' a; cases' b; cases' c; refl, ..int2.add_group } #reduce (1 : int2) * 0 / (0 - 1) #reduce (3 : int2) lemma ring_example (a b : int2) : (a + b) ^ 3 = a ^ 3 + 3 * a ^ 2 * b + 3 * a * b ^ 2 + b ^ 3 := by ring lemma ring_exp_example (a b : int2) (n : ℕ): (a + b) ^ (2 + n) = (a + b) ^ n * (a ^ 2 + 2 * a * b + b ^ 2) := by ring_exp /-! `ring` and `ring_exp` prove equalities over commutative rings and semirings by normalizing expressions. The `ring_exp` variant also normalizes exponents. -/ lemma abel_example (a b : ℤ) : a + b + 0 - (b + a + a) = - a := by abel /-! `abel` proves equalities over additive commutative monoids and groups by normalizing expressions. ## Coercions When combining numbers form `ℕ`, `ℤ`, `ℚ`, and `ℝ`, we might want to cast from one type to another. Lean has a mechanism to automatically introduce coercions, represented by `coe` (syntactic sugar: `↑`). `coe` can be set up to provide implicit coercions between arbitrary types. Many coercions are already in place, including the following: * `coe : ℕ → α` casts `ℕ` to another semiring `α`; * `coe : ℤ → α` casts `ℤ` to another ring `α`; * `coe : ℚ → α` casts `ℚ` to another division ring `α`. For example, this works, although negation `- n` is not defined on natural numbers: -/ lemma neg_mul_neg_nat (n : ℕ) (z : ℤ) : (- z) * (- n) = z * n := neg_mul_neg z n /-! Notice how Lean introduced a `↑` coercion: -/ #print neg_mul_neg_nat /-! Another example: -/ lemma neg_nat_mul_neg (n : ℕ) (z : ℤ) : (- n : ℤ) * (- z) = n * z := neg_mul_neg n z #print neg_nat_mul_neg /-! In proofs involving coercions, the tactic `norm_cast` can be convenient. -/ lemma norm_cast_example_1 (m n : ℕ) (h : (m : ℤ) = (n : ℤ)) : m = n := begin norm_cast at h, exact h end lemma norm_cast_example_2 (m n : ℕ) : (m : ℤ) + (n : ℤ) = ((m + n : ℕ) : ℤ) := by norm_cast /-! `norm_cast` moves coercions towards the inside of expressions, as a form of simplification. Like `simp`, it will generally produce a subgoal. `norm_cast` relies on lemmas such as the following: -/ #check nat.cast_add #check int.cast_add #check rat.cast_add /-! ### Lists, Multisets and Finite Sets For finite collections of elements different structures are available: * lists: order and duplicates matter; * multisets: only duplicates matter; * finsets: neither order nor duplicates matter. -/ lemma list_duplicates_example : [2, 3, 3, 4] ≠ [2, 3, 4] := dec_trivial lemma list_order_example : [4, 3, 2] ≠ [2, 3, 4] := dec_trivial lemma multiset_duplicates_example : ({2, 3, 3, 4} : multiset ℕ) ≠ {2, 3, 4} := dec_trivial lemma multiset_order_example : ({2, 3, 4} : multiset ℕ) = {4, 3, 2} := dec_trivial lemma finset_duplicates_example : ({2, 3, 3, 4} : finset ℕ) = {2, 3, 4} := dec_trivial lemma finsetorder_example : ({2, 3, 4} : finset ℕ) = {4, 3, 2} := dec_trivial /-! `dec_trivial` is a special lemma that can be used on trivial decidable goals (e.g., true closed executable expressions). -/ def list.elems : btree ℕ → list ℕ | btree.empty := [] | (btree.node a l r) := a :: list.elems l ++ list.elems r def multiset.elems : btree ℕ → multiset ℕ | btree.empty := ∅ | (btree.node a l r) := {a} ∪ (multiset.elems l ∪ multiset.elems r) def finset.elems : btree ℕ → finset ℕ | btree.empty := ∅ | (btree.node a l r) := {a} ∪ (finset.elems l ∪ finset.elems r) #eval list.sum [2, 3, 4] -- result: 9 #eval multiset.sum ({2, 3, 4} : multiset ℕ) -- result: 9 #eval finset.sum ({2, 3, 4} : finset ℕ) (λn, n) -- result: 9 #eval list.prod [2, 3, 4] -- result: 24 #eval multiset.prod ({2, 3, 4} : multiset ℕ) -- result: 24 #eval finset.prod ({2, 3, 4} : finset ℕ) (λn, n) -- result: 24 /-! ## Order Type Classes Many of the structures introduced above can be ordered. For example, the well-known order on the natural numbers can be defined as follows: -/ inductive nat.le : ℕ → ℕ → Prop | refl : ∀a : ℕ, nat.le a a | step : ∀a b : ℕ, nat.le a b → nat.le a (b + 1) /-! This is an example of a linear order. A __linear order__ (or __total order__) is a binary relation `≤` such that for all `a`, `b`, `c`, the following properties hold: * Reflexivity: `a ≤ a`. * Transitivity: If `a ≤ b` and `b ≤ c`, then `a ≤ c`. * Antisymmetry: If `a ≤ b` and `b ≤ a`, then `a = b`. * Totality: `a ≤ b` or `b ≤ a`. If a relation has the first three properties, it is a __partial order__. An example is `⊆` on sets, finite sets, or multisets. If a relation has the first two properties, it is a __preorder__. An example is comparing lists by their length. In Lean, there are type classes for these different kinds of orders: `linear_order`, `partial_order`, and `preorder`. The `preorder` class has the fields `le : α → α → Prop` `le_refl : ∀a : α, le a a` `le_trans : ∀a b c : α, le a b → le b c → le a c` The `partial_order` class also has `le_antisymm : ∀a b : α, le a b → le b a → a = b` and `linear_order` also has `le_total : ∀a b : α, le a b ∨ le b a` We can declare the preorder on lists that compares lists by their length as follows: -/ @[instance] def list.length.preord {α : Type} : preorder (list α) := { le := λxs ys, list.length xs ≤ list.length ys, le_refl := by intro xs; exact nat.le_refl _, le_trans := by intros xs ys zs; exact nat.le_trans } /-! This instance introduces the infix syntax `≤` and the relations `≥`, `<`, and `>`: -/ lemma list.length.preord_example {α : Type} (c : α) : [c] > [] := dec_trivial /-! Complete lattices (lecture 10) are formalized as another type class, `complete_lattice`, which inherits from `partial_order`. Type classes combining orders and algebraic structures are also available: `ordered_cancel_comm_monoid` `ordered_comm_group ordered_semiring` `linear_ordered_semiring` `linear_ordered_comm_ring` `linear_ordered_field` All these mathematical structures relate `≤` and `<` with `0`, `1`, `+`, and `*` by monotonicity rules (e.g., `a ≤ b → c ≤ d → a + c ≤ b + d`) and cancellation rules (e.g., `c + a ≤ c + b → a ≤ b`). -/ end LoVe
44d3f1ce09650f58f59ea8be4c7f5a82345f0efa
ed27983dd289b3bcad416f0b1927105d6ef19db8
/src/inClassNotes/02_polymorphicTypes.lean
627eea0e7c69d02128270da052b93bb9c2fa11b2
[]
no_license
liuxin-James/complogic-s21
0d55b76dbe25024473d31d98b5b83655c365f811
13e03e0114626643b44015c654151fb651603486
refs/heads/master
1,681,109,264,463
1,618,848,261,000
1,618,848,261,000
337,599,491
0
0
null
1,613,141,619,000
1,612,925,555,000
null
UTF-8
Lean
false
false
5,725
lean
namespace hidden /- Inductive, aka algebraic, data types - sum types - product types and case analysis - parametrically polymorphic - sum of product types and destructuring -/ /- SUM TYPES A value of such a type is "one of these OR one of those OR one of something else." SUM means OR in this sense. -/ inductive romanNumeral : Type | I : romanNumeral | II : romanNumeral | III : romanNumeral | IV : romanNumeral | V : romanNumeral -- The empty data type inductive empty : Type -- Look Ma, no terms ("uninhabited") def x : empty := _ def funny : empty → empty := λ (e : empty), e -- The unit data type inductive unit : Type -- A type with one value (void) | star : unit -- The bool data type inductive bool : Type -- Two values | tt : bool | ff -- leave type to be inferred -- An inductive type with seven values inductive day : Type | sun | mon | tue | wed | thu | fri | sat open day /- CASE ANALYSIS We generally define functions that consume values of sum types by case analysis. To know what to return, we need to know what form of value we got as an argument. -/ def next_day : day → day | sun := mon | mon := tue | tue := wed | wed := thu | thu := fri | fri := sat | sat := sun /- The left side of a case is usually called a pattern. Argument values are matched to patterns in the order in which patterns are using a "unification" algorithm (more on that later). Function evaluation finds the first pattern that matches and return the result obtained by evaluating the expression on the right hand side of that pattern-matching rule. -/ #reduce next_day sun #reduce next_day sat /- The _ character can be used to match any value. All functions in Lean must be total. We often use _ to cover all other cases no covered explicitly so far. -/ def isWeekday : day → bool | sat := bool.ff | sun := bool.ff | _ := bool.tt /- PRODUCT TYPES (records, structures) -/ /- A product type has one constructor that takes and bundles up values of zero or more other types into records, or structures. In a value of a product type there a value for the first field of an object AND a value for the secon AND a value for the third, etc. PRODUCT in this sense means AND. -/ /- Note: The empty type can be viewed as also a product type with zero fields. -/ /- We now define a product type with one field. To understand such type definitions you need to understand that a constructor, C, is like a function, with zero or more arguments, the only thing it does is to package up its arguments into a term, (C a₀ ... aₙ). A value of our box type can thus be visualized as a box (term) with a single value, the argument to box.mk, inside. As usual there are several syntactic variants for defining inductive types. -/ inductive box_nat' : Type | mk (val : ℕ) -- : box_nat' inductive box_nat'' : Type | mk : ℕ → box_nat'' structure box_nat''' : Type := mk :: (val : ℕ) structure box_nat := -- readable (val : ℕ) -- Let's create such a value def aBox := box_nat.mk 3 -- What does the resulting term look like? #reduce aBox /- Given a box, we "destructure" it using "pattern matching" to get at the values that were used in its construction: in this case to access the ℕ inside a box. Here we see a more interesting form of unification. The key ideas are (1) the algorithm determines whether a pattern matches, (2) it binds names to parts of the object being matched accordingly. -/ def unbox_nat : box_nat → ℕ -- box_nat.mk 3 -- pattern matching, unification | (box_nat.mk n) := n #eval unbox_nat aBox /- When you use the "structure" syntax, Lean generates a projection (accessor) function for each field automatically. -/ #eval box_nat.val aBox #eval aBox.val -- Preferred notation /- Polymorphic types -/ /- We immediately see the same problem as with functions: the need for a many variants varying only in the type of value "contained in the box". The solution is analogous: make our type polymorphic by having it take a type-valued parameter and by making the value it contains of the type that is the value of that parameter. -/ structure box (α : Type) : Type := /-mk ::-/ (val : α) #check box def nat_box : box nat := box.mk 3 def bool_box : box bool := box.mk bool.tt #check nat_box #check box def str_box : box string:= box.mk "Hello, Lean!" def bool_box' := box.mk bool.tt #eval nat_box.val #eval str_box.val #eval bool_box.val def fun_box : box (nat → nat) := box.mk (nat.succ) def crazy_box : box (box (nat → nat)) := box.mk (box.mk nat.succ) #check fun_box #check crazy_box def f : ℕ → ℕ := box.val fun_box #eval f 5 def C : ℕ → ℕ := box.val (box.val crazy_box) #eval C 3 #check nat → nat #check nat.succ #eval nat.succ 4 /- Polymorphic product types with two fields -- the type of ordered pairs -- and two type parameters accordingly. -/ structure prod (α β : Type) : Type := (fst : α) (snd : β) -- Self-test: What's the type of prod? -- "Introduce" some pairs def pair1 := prod.mk 4 "Hi" #check pair1 #check prod #eval prod.fst pair1 #eval prod.snd pair1 #eval pair1.fst #eval pair1.snd def pair2 := prod.mk "Bye" tt -- "Eliminate" some pairs #eval pair1.fst #eval pair1.snd #eval pair2.fst #eval pair2.snd #check prod structure phythagorean_triple : Type := (a : ℕ) (b : ℕ) (c : ℕ) (cert: a*a + b*b = c*c) def py_tri : phythagorean_triple := phythagorean_triple.mk 3 4 5 rfl #reduce py_tri.cert -- construct proof that 25 = 25 def py_tri_bad : phythagorean_triple := phythagorean_triple.mk 3 4 6 rfl -- can't construct proof that 25 = 36 -- Try that in mere Haskell. No way! end hidden
3bb52700c5e33a5ec1418831839ed202a115cf9a
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/algebra/module/weak_dual.lean
ba75f3f95491634716f2464716ab2e5363c7475f
[ "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
11,572
lean
/- Copyright (c) 2021 Kalle Kytölä. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kalle Kytölä, Moritz Doll -/ import topology.algebra.module.basic /-! # Weak dual topology This file defines the weak topology given two vector spaces `E` and `F` over a commutative semiring `𝕜` and a bilinear form `B : E →ₗ[𝕜] F →ₗ[𝕜] 𝕜`. The weak topology on `E` is the coarsest topology such that for all `y : F` every map `λ x, B x y` is continuous. In the case that `F = E →L[𝕜] 𝕜` and `B` being the canonical pairing, we obtain the weak-* topology, `weak_dual 𝕜 E := (E →L[𝕜] 𝕜)`. Interchanging the arguments in the bilinear form yields the weak topology `weak_space 𝕜 E := E`. ## Main definitions The main definitions are the types `weak_bilin B` for the general case and the two special cases `weak_dual 𝕜 E` and `weak_space 𝕜 E` with the respective topology instances on it. * Given `B : E →ₗ[𝕜] F →ₗ[𝕜] 𝕜`, the type `weak_bilin B` is a type synonym for `E`. * The instance `weak_bilin.topological_space` is the weak topology induced by the bilinear form `B`. * `weak_dual 𝕜 E` is a type synonym for `dual 𝕜 E` (when the latter is defined): both are equal to the type `E →L[𝕜] 𝕜` of continuous linear maps from a module `E` over `𝕜` to the ring `𝕜`. * The instance `weak_dual.topological_space` is the weak-* topology on `weak_dual 𝕜 E`, i.e., the coarsest topology making the evaluation maps at all `z : E` continuous. * `weak_space 𝕜 E` is a type synonym for `E` (when the latter is defined). * The instance `weak_dual.topological_space` is the weak topology on `E`, i.e., the coarsest topology such that all `v : dual 𝕜 E` remain continuous. ## Main results We establish that `weak_bilin B` has the following structure: * `weak_bilin.has_continuous_add`: The addition in `weak_bilin B` is continuous. * `weak_bilin.has_continuous_smul`: The scalar multiplication in `weak_bilin B` is continuous. We prove the following results characterizing the weak topology: * `eval_continuous`: For any `y : F`, the evaluation mapping `λ x, B x y` is continuous. * `continuous_of_continuous_eval`: For a mapping to `weak_bilin B` to be continuous, it suffices that its compositions with pairing with `B` at all points `y : F` is continuous. * `tendsto_iff_forall_eval_tendsto`: Convergence in `weak_bilin B` can be characterized in terms of convergence of the evaluations at all points `y : F`. ## Notations No new notation is introduced. ## References * [H. H. Schaefer, *Topological Vector Spaces*][schaefer1966] ## Tags weak-star, weak dual, duality -/ noncomputable theory open filter open_locale topological_space variables {α 𝕜 𝕝 R E F M : Type*} section weak_topology /-- The space `E` equipped with the weak topology induced by the bilinear form `B`. -/ @[derive [add_comm_monoid, module 𝕜], nolint has_inhabited_instance unused_arguments] def weak_bilin [comm_semiring 𝕜] [add_comm_monoid E] [module 𝕜 E] [add_comm_monoid F] [module 𝕜 F] (B : E →ₗ[𝕜] F →ₗ[𝕜] 𝕜) := E namespace weak_bilin instance [comm_semiring 𝕜] [a : add_comm_group E] [module 𝕜 E] [add_comm_monoid F] [module 𝕜 F] (B : E →ₗ[𝕜] F →ₗ[𝕜] 𝕜) : add_comm_group (weak_bilin B) := a @[priority 100] instance module' [comm_semiring 𝕜] [comm_semiring 𝕝] [add_comm_group E] [module 𝕜 E] [add_comm_group F] [module 𝕜 F] [m : module 𝕝 E] (B : E →ₗ[𝕜] F →ₗ[𝕜] 𝕜) : module 𝕝 (weak_bilin B) := m instance [comm_semiring 𝕜] [comm_semiring 𝕝] [add_comm_group E] [module 𝕜 E] [add_comm_group F] [module 𝕜 F] [has_scalar 𝕝 𝕜] [module 𝕝 E] [s : is_scalar_tower 𝕝 𝕜 E] (B : E →ₗ[𝕜] F →ₗ[𝕜] 𝕜) : is_scalar_tower 𝕝 𝕜 (weak_bilin B) := s section semiring variables [topological_space 𝕜] [comm_semiring 𝕜] variables [add_comm_monoid E] [module 𝕜 E] variables [add_comm_monoid F] [module 𝕜 F] variables (B : E →ₗ[𝕜] F →ₗ[𝕜] 𝕜) instance : topological_space (weak_bilin B) := topological_space.induced (λ x y, B x y) Pi.topological_space lemma coe_fn_continuous : continuous (λ (x : weak_bilin B) y, B x y) := continuous_induced_dom lemma eval_continuous (y : F) : continuous (λ x : weak_bilin B, B x y) := ( continuous_pi_iff.mp (coe_fn_continuous B)) y lemma continuous_of_continuous_eval [topological_space α] {g : α → weak_bilin B} (h : ∀ y, continuous (λ a, B (g a) y)) : continuous g := continuous_induced_rng (continuous_pi_iff.mpr h) /-- The coercion `(λ x y, B x y) : E → (F → 𝕜)` is an embedding. -/ lemma embedding {B : E →ₗ[𝕜] F →ₗ[𝕜] 𝕜} (hB : function.injective B) : embedding (λ (x : weak_bilin B) y, B x y) := function.injective.embedding_induced $ linear_map.coe_injective.comp hB theorem tendsto_iff_forall_eval_tendsto {l : filter α} {f : α → (weak_bilin B)} {x : weak_bilin B} (hB : function.injective B) : tendsto f l (𝓝 x) ↔ ∀ y, tendsto (λ i, B (f i) y) l (𝓝 (B x y)) := by rw [← tendsto_pi_nhds, embedding.tendsto_nhds_iff (embedding hB)] /-- Addition in `weak_space B` is continuous. -/ instance [has_continuous_add 𝕜] : has_continuous_add (weak_bilin B) := begin refine ⟨continuous_induced_rng _⟩, refine cast (congr_arg _ _) (((coe_fn_continuous B).comp continuous_fst).add ((coe_fn_continuous B).comp continuous_snd)), ext, simp only [function.comp_app, pi.add_apply, map_add, linear_map.add_apply], end /-- Scalar multiplication by `𝕜` on `weak_bilin B` is continuous. -/ instance [has_continuous_smul 𝕜 𝕜] : has_continuous_smul 𝕜 (weak_bilin B) := begin refine ⟨continuous_induced_rng _⟩, refine cast (congr_arg _ _) (continuous_fst.smul ((coe_fn_continuous B).comp continuous_snd)), ext, simp only [function.comp_app, pi.smul_apply, linear_map.map_smulₛₗ, ring_hom.id_apply, linear_map.smul_apply], end end semiring section ring variables [topological_space 𝕜] [comm_ring 𝕜] variables [add_comm_group E] [module 𝕜 E] variables [add_comm_group F] [module 𝕜 F] variables (B : E →ₗ[𝕜] F →ₗ[𝕜] 𝕜) /-- `weak_space B` is a `topological_add_group`, meaning that addition and negation are continuous. -/ instance [has_continuous_add 𝕜] : topological_add_group (weak_bilin B) := { to_has_continuous_add := by apply_instance, continuous_neg := begin refine continuous_induced_rng (continuous_pi_iff.mpr (λ y, _)), refine cast (congr_arg _ _) (eval_continuous B (-y)), ext, simp only [map_neg, function.comp_app, linear_map.neg_apply], end } end ring end weak_bilin end weak_topology section weak_star_topology /-- The canonical pairing of a vector space and its topological dual. -/ def top_dual_pairing (𝕜 E) [comm_semiring 𝕜] [topological_space 𝕜] [has_continuous_add 𝕜] [add_comm_monoid E] [module 𝕜 E] [topological_space E] [has_continuous_const_smul 𝕜 𝕜] : (E →L[𝕜] 𝕜) →ₗ[𝕜] E →ₗ[𝕜] 𝕜 := continuous_linear_map.coe_lm 𝕜 variables [comm_semiring 𝕜] [topological_space 𝕜] [has_continuous_add 𝕜] variables [has_continuous_const_smul 𝕜 𝕜] variables [add_comm_monoid E] [module 𝕜 E] [topological_space E] lemma dual_pairing_apply (v : (E →L[𝕜] 𝕜)) (x : E) : top_dual_pairing 𝕜 E v x = v x := rfl /-- The weak star topology is the topology coarsest topology on `E →L[𝕜] 𝕜` such that all functionals `λ v, top_dual_pairing 𝕜 E v x` are continuous. -/ @[derive [add_comm_monoid, module 𝕜, topological_space, has_continuous_add]] def weak_dual (𝕜 E) [comm_semiring 𝕜] [topological_space 𝕜] [has_continuous_add 𝕜] [has_continuous_const_smul 𝕜 𝕜] [add_comm_monoid E] [module 𝕜 E] [topological_space E] := weak_bilin (top_dual_pairing 𝕜 E) namespace weak_dual instance : inhabited (weak_dual 𝕜 E) := continuous_linear_map.inhabited instance weak_dual.add_monoid_hom_class : add_monoid_hom_class (weak_dual 𝕜 E) E 𝕜 := continuous_linear_map.add_monoid_hom_class /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (weak_dual 𝕜 E) (λ _, E → 𝕜) := fun_like.has_coe_to_fun /-- If a monoid `M` distributively continuously acts on `𝕜` and this action commutes with multiplication on `𝕜`, then it acts on `weak_dual 𝕜 E`. -/ instance (M) [monoid M] [distrib_mul_action M 𝕜] [smul_comm_class 𝕜 M 𝕜] [has_continuous_const_smul M 𝕜] : mul_action M (weak_dual 𝕜 E) := continuous_linear_map.mul_action /-- If a monoid `M` distributively continuously acts on `𝕜` and this action commutes with multiplication on `𝕜`, then it acts distributively on `weak_dual 𝕜 E`. -/ instance (M) [monoid M] [distrib_mul_action M 𝕜] [smul_comm_class 𝕜 M 𝕜] [has_continuous_const_smul M 𝕜] : distrib_mul_action M (weak_dual 𝕜 E) := continuous_linear_map.distrib_mul_action /-- If `𝕜` is a topological module over a semiring `R` and scalar multiplication commutes with the multiplication on `𝕜`, then `weak_dual 𝕜 E` is a module over `R`. -/ instance module' (R) [semiring R] [module R 𝕜] [smul_comm_class 𝕜 R 𝕜] [has_continuous_const_smul R 𝕜] : module R (weak_dual 𝕜 E) := continuous_linear_map.module instance (M) [monoid M] [distrib_mul_action M 𝕜] [smul_comm_class 𝕜 M 𝕜] [has_continuous_const_smul M 𝕜] : has_continuous_const_smul M (weak_dual 𝕜 E) := ⟨λ m, continuous_induced_rng $ (weak_bilin.coe_fn_continuous (top_dual_pairing 𝕜 E)).const_smul m⟩ /-- If a monoid `M` distributively continuously acts on `𝕜` and this action commutes with multiplication on `𝕜`, then it continuously acts on `weak_dual 𝕜 E`. -/ instance (M) [monoid M] [distrib_mul_action M 𝕜] [smul_comm_class 𝕜 M 𝕜] [topological_space M] [has_continuous_smul M 𝕜] : has_continuous_smul M (weak_dual 𝕜 E) := ⟨continuous_induced_rng $ continuous_fst.smul ((weak_bilin.coe_fn_continuous (top_dual_pairing 𝕜 E)).comp continuous_snd)⟩ lemma coe_fn_continuous : continuous (λ (x : weak_dual 𝕜 E) y, x y) := continuous_induced_dom lemma eval_continuous (y : E) : continuous (λ x : weak_dual 𝕜 E, x y) := continuous_pi_iff.mp coe_fn_continuous y lemma continuous_of_continuous_eval [topological_space α] {g : α → weak_dual 𝕜 E} (h : ∀ y, continuous (λ a, (g a) y)) : continuous g := continuous_induced_rng (continuous_pi_iff.mpr h) end weak_dual /-- The weak topology is the topology coarsest topology on `E` such that all functionals `λ x, top_dual_pairing 𝕜 E v x` are continuous. -/ @[derive [add_comm_monoid, module 𝕜, topological_space, has_continuous_add], nolint has_inhabited_instance] def weak_space (𝕜 E) [comm_semiring 𝕜] [topological_space 𝕜] [has_continuous_add 𝕜] [has_continuous_const_smul 𝕜 𝕜] [add_comm_monoid E] [module 𝕜 E] [topological_space E] := weak_bilin (top_dual_pairing 𝕜 E).flip theorem tendsto_iff_forall_eval_tendsto_top_dual_pairing {l : filter α} {f : α → weak_dual 𝕜 E} {x : weak_dual 𝕜 E} : tendsto f l (𝓝 x) ↔ ∀ y, tendsto (λ i, top_dual_pairing 𝕜 E (f i) y) l (𝓝 (top_dual_pairing 𝕜 E x y)) := weak_bilin.tendsto_iff_forall_eval_tendsto _ continuous_linear_map.coe_injective end weak_star_topology
5d43cd0595f3a60c4ed52b83c024e086b93251dc
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/400.lean
afec0e81dcdbca203150c97df8b1a9a08a1a8227
[ "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
256
lean
example (n m k : ℕ) (h : n + 1 = m + 1) : m = k → n = k := begin injection h with x y z, guard_hyp n : ℕ, guard_hyp m : ℕ, guard_hyp k : ℕ, guard_hyp h : n + 1 = m + 1, guard_hyp x : n = m, guard_target m = k → n = k, admit end
89553a3accc52fb3d022963cb8a9082b2b0fcf5d
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/playground/uf1.lean
14ad302d976938a77ed417ba219ed5271fb84d8c
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
4,653
lean
def StateT (m : Type → Type) (σ : Type) (α : Type) := (Unit × σ) → m (α × σ) namespace StateT variable {m : Type → Type} [Monad m] {σ : Type} {α β : Type} @[inline] protected def pure (a : α) : StateT m σ α := λ ⟨_, s⟩, pure (a, s) @[inline] protected def bind (x : StateT m σ α) (f : α → StateT m σ β) : StateT m σ β := λ p, do (a, s') ← x p, f a ((), s') @[inline] def read : StateT m σ σ := λ ⟨_, s⟩, pure (s, s) @[inline] def write (s' : σ) : StateT m σ Unit := λ ⟨_, s⟩, pure ((), s') @[inline] def updt (f : σ → σ) : StateT m σ Unit := λ ⟨_, s⟩, pure ((), f s) instance : Monad (StateT m σ) := {pure := @StateT.pure _ _ _, bind := @StateT.bind _ _ _} end StateT def ExceptT (m : Type → Type) (ε : Type) (α : Type) := { e : Except ε Unit // e = Except.ok () } → m (Except ε α) namespace ExceptT variable {m : Type → Type} [Monad m] {ε : Type} {α β : Type} @[inline] protected def pure (a : α) : ExceptT m ε α := λ e, match e with | ⟨Except.ok _, h⟩ := pure (Except.ok a) | ⟨Except.error _, h⟩ := Except.noConfusion h @[inline] protected def bind (x : ExceptT m ε α) (f : α → ExceptT m ε β) : ExceptT m ε β := λ e, do v ← x e, match v with | Except.error e := pure (Except.error e) | Except.ok a := f a ⟨Except.ok (), rfl⟩ @[inline] def error (e : ε) : ExceptT m ε α := λ e', match e' with | ⟨Except.ok _, h⟩ := pure (Except.error e) | ⟨Except.error _, h⟩ := Except.noConfusion h @[inline] def lift (x : m α) : ExceptT m ε α := λ e, do {a ← x, pure (Except.ok a)} instance : Monad (ExceptT m ε) := {pure := @ExceptT.pure _ _ _, bind := @ExceptT.bind _ _ _} end ExceptT abbreviation Node := Nat structure nodeData := (find : Node) (rank : Nat := 0) abbreviation ufData := Array nodeData abbreviation M (α : Type) := ExceptT (StateT id ufData) String α @[inline] def read : M ufData := ExceptT.lift StateT.read @[inline] def write (s : ufData) : M Unit := ExceptT.lift (StateT.write s) @[inline] def updt (f : ufData → ufData) : M Unit := ExceptT.lift (StateT.updt f) @[inline] def error {α : Type} (e : String) : M α := ExceptT.error e def run {α : Type} (x : M α) (s : ufData := Array.nil) : Except String α × ufData := x ⟨Except.ok (), rfl⟩ ((), s) def capacity : M Nat := do d ← read, pure d.size def findEntryAux : Nat → Node → M nodeData | 0 n := error "out of fuel" | (i+1) n := do s ← read, if h : n < s.size then do { let e := s.read ⟨n, h⟩, if e.find = n then pure e else do e₁ ← findEntryAux i e.find, updt (λ s, s.write' n e₁), pure e₁ } else error "invalid Node" def findEntry (n : Node) : M nodeData := do c ← capacity, findEntryAux c n def find (n : Node) : M Node := do e ← findEntry n, pure e.find def mk : M Node := do n ← capacity, updt $ λ s, s.push {find := n, rank := 1}, pure n def union (n₁ n₂ : Node) : M Unit := do r₁ ← findEntry n₁, r₂ ← findEntry n₂, if r₁.find = r₂.find then pure () else updt $ λ s, if r₁.rank < r₂.rank then s.write' r₁.find { find := r₂.find } else if r₁.rank = r₂.rank then let s₁ := s.write' r₁.find { find := r₂.find } in s₁.write' r₂.find { rank := r₂.rank + 1, .. r₂} else s.write' r₂.find { find := r₁.find } def mkNodes : Nat → M Unit | 0 := pure () | (n+1) := mk *> mkNodes n def checkEq (n₁ n₂ : Node) : M Unit := do r₁ ← find n₁, r₂ ← find n₂, unless (r₁ = r₂) $ error "nodes are not equal" def mergePackAux : Nat → Nat → Nat → M Unit | 0 _ _ := pure () | (i+1) n d := do c ← capacity, if (n+d) < c then union n (n+d) *> mergePackAux i (n+1) d else pure () def mergePack (d : Nat) : M Unit := do c ← capacity, mergePackAux c 0 d def numEqsAux : Nat → Node → Nat → M Nat | 0 _ r := pure r | (i+1) n r := do c ← capacity, if n < c then do { n₁ ← find n, numEqsAux i (n+1) (if n = n₁ then r else r+1) } else pure r def numEqs : M Nat := do c ← capacity, numEqsAux c 0 0 def test (n : Nat) : M Nat := if n < 2 then error "input must be greater than 1" else do mkNodes n, mergePack 50000, mergePack 10000, mergePack 5000, mergePack 1000, numEqs def main (xs : List String) : IO UInt32 := let n := xs.head.toNat in match run (test n) with | (Except.ok v, s) := IO.println ("ok " ++ toString v) *> pure 0 | (Except.error e, s) := IO.println ("Error : " ++ e) *> pure 1
07ff431dc0e9da255ac3d15d14dbd2cf66b88b9e
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/int/parity.lean
7ba4dcc8a9749827ee84cec164f36e99ccbf938e
[ "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
6,975
lean
/- Copyright (c) 2019 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Benjamin Davidson -/ import data.nat.parity /-! # Parity of integers This file contains theorems about the `even` and `odd` predicates on the integers. ## Tags even, odd -/ namespace int variables {m n : ℤ} @[simp] theorem mod_two_ne_one : ¬ n % 2 = 1 ↔ n % 2 = 0 := by cases mod_two_eq_zero_or_one n with h h; simp [h] local attribute [simp] -- euclidean_domain.mod_eq_zero uses (2 ∣ n) as normal form theorem mod_two_ne_zero : ¬ n % 2 = 0 ↔ n % 2 = 1 := by cases mod_two_eq_zero_or_one n with h h; simp [h] theorem even_iff : even n ↔ n % 2 = 0 := ⟨λ ⟨m, hm⟩, by simp [hm], λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by simp [h])⟩⟩ theorem odd_iff : odd n ↔ n % 2 = 1 := ⟨λ ⟨m, hm⟩, by { rw [hm, add_mod], norm_num }, λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by { rw h, abel })⟩⟩ lemma not_even_iff : ¬ even n ↔ n % 2 = 1 := by rw [even_iff, mod_two_ne_zero] lemma not_odd_iff : ¬ odd n ↔ n % 2 = 0 := by rw [odd_iff, mod_two_ne_one] lemma even_iff_not_odd : even n ↔ ¬ odd n := by rw [not_odd_iff, even_iff] @[simp] lemma odd_iff_not_even : odd n ↔ ¬ even n := by rw [not_even_iff, odd_iff] lemma is_compl_even_odd : is_compl {n : ℤ | even n} {n | odd n} := by simp [← set.compl_set_of, is_compl_compl] lemma even_or_odd (n : ℤ) : even n ∨ odd n := or.imp_right odd_iff_not_even.2 $ em $ even n lemma even_or_odd' (n : ℤ) : ∃ k, n = 2 * k ∨ n = 2 * k + 1 := by simpa only [exists_or_distrib, ← odd, ← even] using even_or_odd n lemma even_xor_odd (n : ℤ) : xor (even n) (odd n) := begin cases even_or_odd n with h, { exact or.inl ⟨h, even_iff_not_odd.mp h⟩ }, { exact or.inr ⟨h, odd_iff_not_even.mp h⟩ }, end lemma even_xor_odd' (n : ℤ) : ∃ k, xor (n = 2 * k) (n = 2 * k + 1) := begin rcases even_or_odd n with ⟨k, rfl⟩ | ⟨k, rfl⟩; use k, { simpa only [xor, true_and, eq_self_iff_true, not_true, or_false, and_false] using (succ_ne_self (2*k)).symm }, { simp only [xor, add_right_eq_self, false_or, eq_self_iff_true, not_true, not_false_iff, one_ne_zero, and_self] }, end @[simp] theorem two_dvd_ne_zero : ¬ 2 ∣ n ↔ n % 2 = 1 := not_even_iff instance : decidable_pred (even : ℤ → Prop) := λ n, decidable_of_decidable_of_iff (by apply_instance) even_iff.symm instance decidable_pred_odd : decidable_pred (odd : ℤ → Prop) := λ n, decidable_of_decidable_of_iff (by apply_instance) odd_iff_not_even.symm @[simp] theorem even_zero : even (0 : ℤ) := ⟨0, dec_trivial⟩ @[simp] theorem not_even_one : ¬ even (1 : ℤ) := by rw even_iff; norm_num @[simp] theorem even_bit0 (n : ℤ) : even (bit0 n) := ⟨n, by rw [bit0, two_mul]⟩ @[parity_simps] theorem even_add : even (m + n) ↔ (even m ↔ even n) := by cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂; simp [even_iff, h₁, h₂, int.add_mod]; norm_num theorem even.add_even (hm : even m) (hn : even n) : even (m + n) := even_add.2 $ iff_of_true hm hn theorem even_add' : even (m + n) ↔ (odd m ↔ odd n) := by rw [even_add, even_iff_not_odd, even_iff_not_odd, not_iff_not] theorem odd.add_odd (hm : odd m) (hn : odd n) : even (m + n) := even_add'.2 $ iff_of_true hm hn @[simp] theorem not_even_bit1 (n : ℤ) : ¬ even (bit1 n) := by simp [bit1] with parity_simps lemma two_not_dvd_two_mul_add_one (n : ℤ) : ¬(2 ∣ 2 * n + 1) := by convert not_even_bit1 n; exact two_mul n @[parity_simps] theorem even_sub : even (m - n) ↔ (even m ↔ even n) := by simp [sub_eq_add_neg] with parity_simps theorem even.sub_even (hm : even m) (hn : even n) : even (m - n) := even_sub.2 $ iff_of_true hm hn theorem even_sub' : even (m - n) ↔ (odd m ↔ odd n) := by rw [even_sub, even_iff_not_odd, even_iff_not_odd, not_iff_not] theorem odd.sub_odd (hm : odd m) (hn : odd n) : even (m - n) := even_sub'.2 $ iff_of_true hm hn @[parity_simps] theorem even_add_one : even (n + 1) ↔ ¬ even n := by simp [even_add] @[parity_simps] theorem even_mul : even (m * n) ↔ even m ∨ even n := by cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂; simp [even_iff, h₁, h₂, int.mul_mod]; norm_num theorem odd_mul : odd (m * n) ↔ odd m ∧ odd n := by simp [not_or_distrib] with parity_simps theorem even.mul_left (hm : even m) (n : ℤ) : even (m * n) := even_mul.mpr $ or.inl hm theorem even.mul_right (m : ℤ) (hn : even n) : even (m * n) := even_mul.mpr $ or.inr hn theorem odd.mul (hm : odd m) (hn : odd n) : odd (m * n) := odd_mul.mpr ⟨hm, hn⟩ theorem odd.of_mul_left (h : odd (m * n)) : odd m := (odd_mul.mp h).1 theorem odd.of_mul_right (h : odd (m * n)) : odd n := (odd_mul.mp h).2 @[parity_simps] theorem even_pow {n : ℕ} : even (m ^ n) ↔ even m ∧ n ≠ 0 := by { induction n with n ih; simp [*, even_mul, pow_succ], tauto } theorem even_pow' {n : ℕ} (h : n ≠ 0) : even (m ^ n) ↔ even m := even_pow.trans $ and_iff_left h @[parity_simps] theorem odd_add : odd (m + n) ↔ (odd m ↔ even n) := by rw [odd_iff_not_even, even_add, not_iff, odd_iff_not_even] theorem odd.add_even (hm : odd m) (hn : even n) : odd (m + n) := odd_add.2 $ iff_of_true hm hn theorem odd_add' : odd (m + n) ↔ (odd n ↔ even m) := by rw [add_comm, odd_add] theorem even.add_odd (hm : even m) (hn : odd n) : odd (m + n) := odd_add'.2 $ iff_of_true hn hm lemma ne_of_odd_add (h : odd (m + n)) : m ≠ n := λ hnot, by simpa [hnot] with parity_simps using h @[parity_simps] theorem odd_sub : odd (m - n) ↔ (odd m ↔ even n) := by rw [odd_iff_not_even, even_sub, not_iff, odd_iff_not_even] theorem odd.sub_even (hm : odd m) (hn : even n) : odd (m - n) := odd_sub.2 $ iff_of_true hm hn theorem odd_sub' : odd (m - n) ↔ (odd n ↔ even m) := by rw [odd_iff_not_even, even_sub, not_iff, not_iff_comm, odd_iff_not_even] theorem even.sub_odd (hm : even m) (hn : odd n) : odd (m - n) := odd_sub'.2 $ iff_of_true hn hm lemma even_mul_succ_self (n : ℤ) : even (n * (n + 1)) := begin rw even_mul, convert n.even_or_odd, simp with parity_simps end @[simp, norm_cast] theorem even_coe_nat (n : ℕ) : even (n : ℤ) ↔ even n := by rw_mod_cast [even_iff, nat.even_iff] @[simp, norm_cast] theorem odd_coe_nat (n : ℕ) : odd (n : ℤ) ↔ odd n := by rw [odd_iff_not_even, nat.odd_iff_not_even, even_coe_nat] @[simp] theorem nat_abs_even : even n.nat_abs ↔ even n := coe_nat_dvd_left.symm @[simp] theorem nat_abs_odd : odd n.nat_abs ↔ odd n := by rw [odd_iff_not_even, nat.odd_iff_not_even, nat_abs_even] -- Here are examples of how `parity_simps` can be used with `int`. example (m n : ℤ) (h : even m) : ¬ even (n + 3) ↔ even (m^2 + m + n) := by simp [*, (dec_trivial : ¬ 2 = 0)] with parity_simps example : ¬ even (25394535 : ℤ) := by simp end int
0984d61b736d72a638c4d71dc712d6bf0c9e3c66
ed27983dd289b3bcad416f0b1927105d6ef19db8
/src/inClassNotes/predicate_logic/not.lean
76785c55186e265dbe02b0eebc1581aafaf841a9
[]
no_license
liuxin-James/complogic-s21
0d55b76dbe25024473d31d98b5b83655c365f811
13e03e0114626643b44015c654151fb651603486
refs/heads/master
1,681,109,264,463
1,618,848,261,000
1,618,848,261,000
337,599,491
0
0
null
1,613,141,619,000
1,612,925,555,000
null
UTF-8
Lean
false
false
1,632
lean
/- If P is a proposition, then so is ¬P. We judge ¬P to be true when P is false: when there is no proof of it at all. -/ #check not /- def not (a : Prop) := a → false prefix `¬` := not -/ lemma not_false' : ¬ false := λ f, f #check not_false' /- For any proposition, P, to prove ¬P, assume that P is true and show that that leads to a contradiction (in the form of a proof of false). This is called "proof by negation." Remember: ¬P means P → false. -/ -- neg elimination is not constructively valid theorem neg_elim : ∀ (P : Prop), ¬(¬P) → P := λ P h, _ -- have proof of this "(P → false) → false", how to get to a proof of P? -- not constructively valid -- law excluded middle /- P ¬ P → false ¬ (¬ P) → P -- negation elimination -- aka double negation elimination -/ /- ¬ P means (is defined as) P → false -/ #check false axiom P : Prop axiom func : P → false /- This proves ¬P! This is a definition: ¬P means P → false -/ #check not #check string.length #check (string.length "hello") -- impossibility elimination example : ¬ 1 = 0 := λ h, match h with -- secret sauce: there are no cases end example : ¬ 1 = 0 := -- 1=0 → false begin assume h, -- assume 1=0 -- show false in all cases -- but there are no cases -- so that is all of them cases h, end /- Contradictions are impossible. -/ lemma no_contra : ¬ (P ∧ ¬ P) := λ h, (h.right h.left) /- Nonsense implies nonsense. -/ example : 1 = 0 → 2 = 3 := begin assume h, cases h, end
b9de13285e3445ce4f0be886b47f32b5d2da36b2
626e312b5c1cb2d88fca108f5933076012633192
/src/linear_algebra/basic.lean
4edb1c16ba3fd7a3ce07d5055947e52071091e15
[ "Apache-2.0" ]
permissive
Bioye97/mathlib
9db2f9ee54418d29dd06996279ba9dc874fd6beb
782a20a27ee83b523f801ff34efb1a9557085019
refs/heads/master
1,690,305,956,488
1,631,067,774,000
1,631,067,774,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
119,856
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, Yury Kudryashov -/ import algebra.big_operators.pi import algebra.module.prod import algebra.module.submodule_lattice import data.dfinsupp import data.finsupp.basic import order.compactly_generated import order.omega_complete_partial_order /-! # 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. Many of the relevant definitions, including `module`, `submodule`, and `linear_map`, are found in `src/algebra/module`. ## Main definitions * Many constructors for linear maps * `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. * The general linear group is defined to be the group of invertible linear maps from `M` to itself. ## Main statements * The first, second and third isomorphism laws for modules are proved as `linear_map.quot_ker_equiv_range`, `linear_map.quotient_inf_equiv_sup_quotient` and `submodule.quotient_quotient_equiv_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. * We introduce the notation `R ∙ v` for the span of a singleton, `submodule.span R {v}`. This is `\.`, not the same as the scalar multiplication `•`/`\bub`. ## Implementation notes We note that, when constructing linear maps, it is convenient to use operations defined on bundled maps (`linear_map.prod`, `linear_map.coprod`, arithmetic operations like `+`) instead of defining a function and proving it is linear. ## Tags linear algebra, vector space, module -/ open function open_locale big_operators pointwise 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 β] [monoid R] [add_comm_monoid M] [distrib_mul_action R M] {v : α →₀ β} {c : R} {h : α → β → M} : c • (v.sum h) = v.sum (λa b, c • h a b) := finset.smul_sum @[simp] lemma sum_smul_index_linear_map' {α : Type u} {R : Type v} {M : Type w} {M₂ : Type x} [semiring R] [add_comm_monoid M] [module R M] [add_comm_monoid M₂] [module R M₂] {v : α →₀ M} {c : R} {h : α → M →ₗ[R] M₂} : (c • v).sum (λ a, h a) = c • (v.sum (λ a, h a)) := begin rw [finsupp.sum_smul_index', finsupp.smul_sum], { simp only [linear_map.map_smul], }, { intro i, exact (h i).map_zero }, end variables (α : Type*) [fintype α] variables (R M) [add_comm_monoid M] [semiring R] [module R M] /-- Given `fintype α`, `linear_equiv_fun_on_fintype R` is the natural `R`-linear equivalence between `α →₀ β` and `α → β`. -/ @[simps apply] noncomputable def linear_equiv_fun_on_fintype : (α →₀ M) ≃ₗ[R] (α → M) := { to_fun := coe_fn, map_add' := λ f g, by { ext, refl }, map_smul' := λ c f, by { ext, refl }, .. equiv_fun_on_fintype } @[simp] lemma linear_equiv_fun_on_fintype_single [decidable_eq α] (x : α) (m : M) : (linear_equiv_fun_on_fintype R M α) (single x m) = pi.single x m := begin ext a, change (equiv_fun_on_fintype (single x m)) a = _, convert _root_.congr_fun (equiv_fun_on_fintype_single x m) a, end @[simp] lemma linear_equiv_fun_on_fintype_symm_single [decidable_eq α] (x : α) (m : M) : (linear_equiv_fun_on_fintype R M α).symm (pi.single x m) = single x m := begin ext a, change (equiv_fun_on_fintype.symm (pi.single x m)) a = _, convert congr_fun (equiv_fun_on_fintype_symm_single x m) a, end @[simp] lemma linear_equiv_fun_on_fintype_symm_coe (f : α →₀ M) : (linear_equiv_fun_on_fintype R M α).symm f = f := by { ext, simp [linear_equiv_fun_on_fintype], } 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 = ∑ i, x i • (λj, if i = j then 1 else 0) := by { ext, simp } end /-! ### Properties of linear maps -/ namespace linear_map section add_comm_monoid variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables [module R M] [module R M₂] [module R M₃] [module R M₄] variables (f g : M →ₗ[R] M₂) include R theorem comp_assoc (g : M₂ →ₗ[R] M₃) (h : M₃ →ₗ[R] M₄) : (h.comp g).comp f = h.comp (g.comp f) := rfl /-- The restriction of a linear map `f : M → M₂` to a submodule `p ⊆ M` gives a linear map `p → M₂`. -/ def dom_restrict (f : M →ₗ[R] M₂) (p : submodule R M) : p →ₗ[R] M₂ := f.comp p.subtype @[simp] lemma dom_restrict_apply (f : M →ₗ[R] M₂) (p : submodule R M) (x : p) : f.dom_restrict p x = f x := 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 /-- Restrict domain and codomain of an endomorphism. -/ def restrict (f : M →ₗ[R] M) {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) : p →ₗ[R] p := (f.dom_restrict p).cod_restrict p $ set_like.forall.2 hf lemma restrict_apply {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) (x : p) : f.restrict hf x = ⟨f x, hf x.1 x.2⟩ := rfl lemma subtype_comp_restrict {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) : p.subtype.comp (f.restrict hf) = f.dom_restrict p := rfl lemma restrict_eq_cod_restrict_dom_restrict {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) : f.restrict hf = (f.dom_restrict p).cod_restrict p (λ x, hf x.1 x.2) := rfl lemma restrict_eq_dom_restrict_cod_restrict {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x, f x ∈ p) : f.restrict (λ x _, hf x) = (f.cod_restrict p hf).dom_restrict p := rfl /-- The constant 0 map is linear. -/ instance : has_zero (M →ₗ[R] M₂) := ⟨{ to_fun := 0, map_add' := by simp, map_smul' := by simp }⟩ instance : inhabited (M →ₗ[R] M₂) := ⟨0⟩ @[simp] lemma zero_apply (x : M) : (0 : M →ₗ[R] M₂) x = 0 := rfl @[simp] lemma default_def : default (M →ₗ[R] M₂) = 0 := rfl instance unique_of_left [subsingleton M] : unique (M →ₗ[R] M₂) := { uniq := λ f, ext $ λ x, by rw [subsingleton.elim x 0, map_zero, map_zero], .. linear_map.inhabited } instance unique_of_right [subsingleton M₂] : unique (M →ₗ[R] M₂) := coe_injective.unique /-- The sum of two linear maps is linear. -/ instance : has_add (M →ₗ[R] M₂) := ⟨λ f g, { to_fun := f + g, map_add' := by simp [add_comm, add_left_comm], map_smul' := 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 monoid. -/ instance : add_comm_monoid (M →ₗ[R] M₂) := { zero := 0, add := (+), add_assoc := by intros; ext; simp [add_comm, add_left_comm], zero_add := by intros; ext; simp [add_comm, add_left_comm], add_zero := by intros; ext; simp [add_comm, add_left_comm], add_comm := by intros; ext; simp [add_comm, add_left_comm], nsmul := λ n f, { to_fun := λ x, n • (f x), map_add' := λ x y, by rw [f.map_add, smul_add], map_smul' := λ c x, by rw [f.map_smul, smul_comm n c (f x)] }, nsmul_zero' := λ f, by { ext x, simp }, nsmul_succ' := λ n f, by { ext x, simp [nat.succ_eq_one_add, add_nsmul] } } /-- Evaluation of an `R`-linear map at a fixed `a`, as an `add_monoid_hom`. -/ def eval_add_monoid_hom (a : M) : (M →ₗ[R] M₂) →+ M₂ := { to_fun := λ f, f a, map_add' := λ f g, linear_map.add_apply f g a, map_zero' := rfl } lemma add_comp (g : M₂ →ₗ[R] M₃) (h : M₂ →ₗ[R] M₃) : (h + g).comp f = h.comp f + g.comp f := rfl lemma comp_add (g : M →ₗ[R] M₂) (h : M₂ →ₗ[R] M₃) : h.comp (f + g) = h.comp f + h.comp g := by { ext, simp } /-- `linear_map.to_add_monoid_hom` promoted to an `add_monoid_hom` -/ def to_add_monoid_hom' : (M →ₗ[R] M₂) →+ (M →+ M₂) := { to_fun := to_add_monoid_hom, map_zero' := by ext; refl, map_add' := by intros; ext; refl } lemma sum_apply (t : finset ι) (f : ι → M →ₗ[R] M₂) (b : M) : (∑ d in t, f d) b = ∑ d in t, f d b := add_monoid_hom.map_sum ((add_monoid_hom.eval b).comp to_add_monoid_hom') f _ section smul_right variables {S : Type*} [semiring S] [module R S] [module S M] [is_scalar_tower R S M] /-- When `f` is an `R`-linear map taking values in `S`, then `λb, f b • x` is an `R`-linear map. -/ def smul_right (f : M₂ →ₗ[R] S) (x : M) : M₂ →ₗ[R] M := { to_fun := λb, f b • x, map_add' := λ x y, by rw [f.map_add, add_smul], map_smul' := λ b y, by rw [f.map_smul, smul_assoc] } @[simp] theorem coe_smul_right (f : M₂ →ₗ[R] S) (x : M) : (smul_right f x : M₂ → M) = λ c, f c • x := rfl theorem smul_right_apply (f : M₂ →ₗ[R] S) (x : M) (c : M₂) : smul_right f x c = f c • x := rfl end smul_right instance : has_one (M →ₗ[R] M) := ⟨linear_map.id⟩ instance : has_mul (M →ₗ[R] M) := ⟨linear_map.comp⟩ lemma one_eq_id : (1 : M →ₗ[R] M) = id := rfl lemma mul_eq_comp (f g : M →ₗ[R] M) : f * g = f.comp g := rfl @[simp] lemma one_apply (x : M) : (1 : M →ₗ[R] M) x = x := rfl @[simp] lemma mul_apply (f g : M →ₗ[R] M) (x : M) : (f * g) x = f (g x) := rfl lemma coe_one : ⇑(1 : M →ₗ[R] M) = _root_.id := rfl lemma coe_mul (f g : M →ₗ[R] M) : ⇑(f * g) = f ∘ g := rfl instance [nontrivial M] : nontrivial (module.End R M) := begin obtain ⟨m, ne⟩ := (nontrivial_iff_exists_ne (0 : M)).mp infer_instance, exact nontrivial_of_ne 1 0 (λ p, ne (linear_map.congr_fun p m)), end @[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 @[simp, norm_cast] lemma coe_fn_sum {ι : Type*} (t : finset ι) (f : ι → M →ₗ[R] M₂) : ⇑(∑ i in t, f i) = ∑ i in t, (f i : M → M₂) := add_monoid_hom.map_sum ⟨@to_fun R M M₂ _ _ _ _ _, rfl, λ x y, rfl⟩ _ _ instance : monoid (M →ₗ[R] M) := by refine_struct { mul := (*), one := (1 : M →ₗ[R] M), npow := @npow_rec _ ⟨1⟩ ⟨(*)⟩ }; intros; try { refl }; apply linear_map.ext; simp {proj := ff} @[simp] lemma pow_apply (f : M →ₗ[R] M) (n : ℕ) (m : M) : (f^n) m = (f^[n] m) := begin induction n with n ih, { refl, }, { simp only [function.comp_app, function.iterate_succ, linear_map.mul_apply, pow_succ, ih], exact (function.commute.iterate_self _ _ m).symm, }, end lemma pow_map_zero_of_le {f : module.End R M} {m : M} {k l : ℕ} (hk : k ≤ l) (hm : (f^k) m = 0) : (f^l) m = 0 := by rw [← nat.sub_add_cancel hk, pow_add, mul_apply, hm, map_zero] lemma commute_pow_left_of_commute {f : M →ₗ[R] M₂} {g : module.End R M} {g₂ : module.End R M₂} (h : g₂.comp f = f.comp g) (k : ℕ) : (g₂^k).comp f = f.comp (g^k) := begin induction k with k ih, { simpa only [pow_zero], }, { rw [pow_succ, pow_succ, linear_map.mul_eq_comp, linear_map.comp_assoc, ih, ← linear_map.comp_assoc, h, linear_map.comp_assoc, linear_map.mul_eq_comp], }, end lemma submodule_pow_eq_zero_of_pow_eq_zero {N : submodule R M} {g : module.End R N} {G : module.End R M} (h : G.comp N.subtype = N.subtype.comp g) {k : ℕ} (hG : G^k = 0) : g^k = 0 := begin ext m, have hg : N.subtype.comp (g^k) m = 0, { rw [← commute_pow_left_of_commute h, hG, zero_comp, zero_apply], }, simp only [submodule.subtype_apply, comp_app, submodule.coe_eq_zero, coe_comp] at hg, rw [hg, linear_map.zero_apply], end lemma coe_pow (f : M →ₗ[R] M) (n : ℕ) : ⇑(f^n) = (f^[n]) := by { ext m, apply pow_apply, } @[simp] lemma id_pow (n : ℕ) : (id : M →ₗ[R] M)^n = id := one_pow n section variables {f' : M →ₗ[R] M} lemma iterate_succ (n : ℕ) : (f' ^ (n + 1)) = comp (f' ^ n) f' := by rw [pow_succ', mul_eq_comp] lemma iterate_surjective (h : surjective f') : ∀ n : ℕ, surjective ⇑(f' ^ n) | 0 := surjective_id | (n + 1) := by { rw [iterate_succ], exact surjective.comp (iterate_surjective n) h, } lemma iterate_injective (h : injective f') : ∀ n : ℕ, injective ⇑(f' ^ n) | 0 := injective_id | (n + 1) := by { rw [iterate_succ], exact injective.comp (iterate_injective n) h, } lemma iterate_bijective (h : bijective f') : ∀ n : ℕ, bijective ⇑(f' ^ n) | 0 := bijective_id | (n + 1) := by { rw [iterate_succ], exact bijective.comp (iterate_bijective n) h, } lemma injective_of_iterate_injective {n : ℕ} (hn : n ≠ 0) (h : injective ⇑(f' ^ n)) : injective f' := begin rw [← nat.succ_pred_eq_of_pos (pos_iff_ne_zero.mpr hn), iterate_succ, coe_comp] at h, exact injective.of_comp h, end lemma surjective_of_iterate_surjective {n : ℕ} (hn : n ≠ 0) (h : surjective ⇑(f' ^ n)) : surjective f' := begin rw [← nat.succ_pred_eq_of_pos (pos_iff_ne_zero.mpr hn), nat.succ_eq_add_one, add_comm, pow_add] at h, exact surjective.of_comp h, end 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 = ∑ 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 end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_monoid 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₄] (f g : M →ₗ[R] M₂) /-- The negation of a linear map is linear. -/ instance : has_neg (M →ₗ[R] M₂) := ⟨λ f, { to_fun := -f, map_add' := by simp [add_comm], map_smul' := by simp }⟩ @[simp] lemma neg_apply (x : M) : (- f) x = - f x := rfl @[simp] lemma comp_neg (g : M₂ →ₗ[R] M₃) : g.comp (- f) = - g.comp f := by { ext, simp } /-- The negation of a linear map is linear. -/ instance : has_sub (M →ₗ[R] M₂) := ⟨λ f g, { to_fun := f - g, map_add' := λ x y, by simp only [pi.sub_apply, map_add, add_sub_comm], map_smul' := λ r x, by simp only [pi.sub_apply, map_smul, smul_sub] }⟩ @[simp] lemma sub_apply (x : M) : (f - g) x = f x - g x := rfl lemma sub_comp (g : M₂ →ₗ[R] M₃) (h : M₂ →ₗ[R] M₃) : (g - h).comp f = g.comp f - h.comp f := rfl lemma comp_sub (g : M →ₗ[R] M₂) (h : M₂ →ₗ[R] M₃) : h.comp (g - f) = h.comp g - h.comp f := by { ext, simp } /-- 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, sub := has_sub.sub, sub_eq_add_neg := _, add_left_neg := _, nsmul := λ n f, { to_fun := λ x, n • (f x), map_add' := λ x y, by rw [f.map_add, smul_add], map_smul' := λ c x, by rw [f.map_smul, smul_comm n c (f x)] }, gsmul := λ n f, { to_fun := λ x, n • (f x), map_add' := λ x y, by rw [f.map_add, smul_add], map_smul' := λ c x, by rw [f.map_smul, smul_comm n c (f x)] }, gsmul_zero' := _, gsmul_succ' := _, gsmul_neg' := _, .. linear_map.add_comm_monoid }; intros; ext; simp [add_comm, add_left_comm, sub_eq_add_neg, add_smul, nat.succ_eq_add_one] end add_comm_group section has_scalar variables {S : Type*} [semiring R] [monoid S] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [module R M] [module R M₂] [module R M₃] [distrib_mul_action S M₂] [smul_comm_class R S M₂] (f : M →ₗ[R] M₂) instance : has_scalar S (M →ₗ[R] M₂) := ⟨λ a f, { to_fun := a • f, map_add' := λ x y, by simp only [pi.smul_apply, f.map_add, smul_add], map_smul' := λ c x, by simp only [pi.smul_apply, f.map_smul, smul_comm c] }⟩ @[simp] lemma smul_apply (a : S) (x : M) : (a • f) x = a • f x := rfl instance {T : Type*} [monoid T] [distrib_mul_action T M₂] [smul_comm_class R T M₂] [smul_comm_class S T M₂] : smul_comm_class S T (M →ₗ[R] M₂) := ⟨λ a b f, ext $ λ x, smul_comm _ _ _⟩ -- example application of this instance: if S -> T -> R are homomorphisms of commutative rings and -- M and M₂ are R-modules then the S-module and T-module structures on Hom_R(M,M₂) are compatible. instance {T : Type*} [monoid T] [has_scalar S T] [distrib_mul_action T M₂] [smul_comm_class R T M₂] [is_scalar_tower S T M₂] : is_scalar_tower S T (M →ₗ[R] M₂) := { smul_assoc := λ _ _ _, ext $ λ _, smul_assoc _ _ _ } instance : distrib_mul_action S (M →ₗ[R] M₂) := { one_smul := λ f, ext $ λ _, one_smul _ _, mul_smul := λ c c' f, ext $ λ _, mul_smul _ _ _, smul_add := λ c f g, ext $ λ x, smul_add _ _ _, smul_zero := λ c, ext $ λ x, smul_zero _ } theorem smul_comp (a : S) (g : M₃ →ₗ[R] M₂) (f : M →ₗ[R] M₃) : (a • g).comp f = a • (g.comp f) := rfl end has_scalar section module variables {S : Type*} [semiring R] [semiring S] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [module R M] [module R M₂] [module R M₃] [module S M₂] [module S M₃] [smul_comm_class R S M₂] [smul_comm_class R S M₃] (f : M →ₗ[R] M₂) instance : module S (M →ₗ[R] M₂) := { add_smul := λ a b f, ext $ λ x, add_smul _ _ _, zero_smul := λ f, ext $ λ x, zero_smul _ _ } variable (S) /-- Applying a linear map at `v : M`, seen as `S`-linear map from `M →ₗ[R] M₂` to `M₂`. See `linear_map.applyₗ` for a version where `S = R`. -/ @[simps] def applyₗ' : M →+ (M →ₗ[R] M₂) →ₗ[S] M₂ := { to_fun := λ v, { to_fun := λ f, f v, map_add' := λ f g, f.add_apply g v, map_smul' := λ x f, f.smul_apply x v }, map_zero' := linear_map.ext $ λ f, f.map_zero, map_add' := λ x y, linear_map.ext $ λ f, f.map_add _ _ } section variables (R M) /-- The equivalence between R-linear maps from `R` to `M`, and points of `M` itself. This says that the forgetful functor from `R`-modules to types is representable, by `R`. This as an `S`-linear equivalence, under the assumption that `S` acts on `M` commuting with `R`. When `R` is commutative, we can take this to be the usual action with `S = R`. Otherwise, `S = ℕ` shows that the equivalence is additive. See note [bundled maps over different rings]. -/ @[simps] def ring_lmap_equiv_self [module S M] [smul_comm_class R S M] : (R →ₗ[R] M) ≃ₗ[S] M := { to_fun := λ f, f 1, inv_fun := smul_right (1 : R →ₗ[R] R), left_inv := λ f, by { ext, simp }, right_inv := λ x, by simp, .. applyₗ' S (1 : R) } end end module section comm_semiring variables [comm_semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [module R M] [module R M₂] [module R M₃] variables (f g : M →ₗ[R] M₂) include R 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 /-- 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₃) := { to_fun := f.comp, map_add' := λ _ _, linear_map.ext $ λ _, f.map_add _ _, map_smul' := λ _ _, linear_map.ext $ λ _, f.map_smul _ _ } /-- Applying a linear map at `v : M`, seen as a linear map from `M →ₗ[R] M₂` to `M₂`. See also `linear_map.applyₗ'` for a version that works with two different semirings. This is the `linear_map` version of `add_monoid_hom.eval`. -/ @[simps] def applyₗ : M →ₗ[R] (M →ₗ[R] M₂) →ₗ[R] M₂ := { to_fun := λ v, { to_fun := λ f, f v, ..applyₗ' R v }, map_smul' := λ x y, linear_map.ext $ λ f, f.map_smul _ _, ..applyₗ' R } /-- Alternative version of `dom_restrict` as a linear map. -/ def dom_restrict' (p : submodule R M) : (M →ₗ[R] M₂) →ₗ[R] (p →ₗ[R] M₂) := { to_fun := λ φ, φ.dom_restrict p, map_add' := by simp [linear_map.ext_iff], map_smul' := by simp [linear_map.ext_iff] } @[simp] lemma dom_restrict'_apply (f : M →ₗ[R] M₂) (p : submodule R M) (x : p) : dom_restrict' p f x = f x := rfl end comm_semiring section semiring variables [semiring R] [add_comm_monoid M] [module R M] instance endomorphism_semiring : semiring (M →ₗ[R] M) := by refine_struct { mul := (*), one := (1 : M →ₗ[R] M), zero := 0, add := (+), npow := @npow_rec _ ⟨1⟩ ⟨(*)⟩, .. linear_map.add_comm_monoid, .. }; intros; try { refl }; apply linear_map.ext; simp {proj := ff} /-- The tautological action by `M →ₗ[R] M` on `M`. This generalizes `function.End.apply_mul_action`. -/ instance apply_module : module (M →ₗ[R] M) M := { smul := ($), smul_zero := linear_map.map_zero, smul_add := linear_map.map_add, add_smul := linear_map.add_apply, zero_smul := (linear_map.zero_apply : ∀ m, (0 : M →ₗ[R] M) m = 0), one_smul := λ _, rfl, mul_smul := λ _ _ _, rfl } @[simp] protected lemma smul_def (f : M →ₗ[R] M) (a : M) : f • a = f a := rfl /-- `linear_map.apply_module` is faithful. -/ instance apply_has_faithful_scalar : has_faithful_scalar (M →ₗ[R] M) M := ⟨λ _ _, linear_map.ext⟩ instance apply_smul_comm_class : smul_comm_class R (M →ₗ[R] M) M := { smul_comm := λ r e m, (e.map_smul r m).symm } instance apply_smul_comm_class' : smul_comm_class (M →ₗ[R] M) R M := { smul_comm := linear_map.map_smul } end semiring section ring variables [ring R] [add_comm_group M] [module R M] instance endomorphism_ring : ring (M →ₗ[R] M) := { ..linear_map.endomorphism_semiring, ..linear_map.add_comm_group } 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₃] /-- 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, map_add' := λ m m', by { ext, apply smul_add, }, map_smul' := λ c m, by { ext, apply smul_comm, } }, map_add' := λ f f', by { ext, apply add_smul, }, map_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] R) →ₗ[R] M →ₗ[R] M₂ →ₗ[R] M) f x c = (f c) • x := rfl end comm_ring end linear_map /-- The `ℕ`-linear equivalence between additive morphisms `A →+ B` and `ℕ`-linear morphisms `A →ₗ[ℕ] B`. -/ @[simps] def add_monoid_hom_lequiv_nat {A B : Type*} [add_comm_monoid A] [add_comm_monoid B] : (A →+ B) ≃ₗ[ℕ] (A →ₗ[ℕ] B) := { to_fun := add_monoid_hom.to_nat_linear_map, inv_fun := linear_map.to_add_monoid_hom, map_add' := by { intros, ext, refl }, map_smul' := by { intros, ext, refl }, left_inv := by { intros f, ext, refl }, right_inv := by { intros f, ext, refl } } /-- The `ℤ`-linear equivalence between additive morphisms `A →+ B` and `ℤ`-linear morphisms `A →ₗ[ℤ] B`. -/ @[simps] def add_monoid_hom_lequiv_int {A B : Type*} [add_comm_group A] [add_comm_group B] : (A →+ B) ≃ₗ[ℤ] (A →ₗ[ℤ] B) := { to_fun := add_monoid_hom.to_int_linear_map, inv_fun := linear_map.to_add_monoid_hom, map_add' := by { intros, ext, refl }, map_smul' := by { intros, ext, refl }, left_inv := by { intros f, ext, refl }, right_inv := by { intros f, ext, refl } } /-! ### Properties of submodules -/ namespace submodule section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid 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 variables {p p'} /-- 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 theorem of_le_injective (h : p ≤ p') : function.injective (of_le h) := λ x y h, subtype.val_injective (subtype.mk.inj h) 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 } instance add_comm_monoid_submodule : 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 variables (R) @[simp] lemma subsingleton_iff : subsingleton (submodule R M) ↔ subsingleton M := have h : subsingleton (submodule R M) ↔ subsingleton (add_submonoid M), { rw [←subsingleton_iff_bot_eq_top, ←subsingleton_iff_bot_eq_top], convert to_add_submonoid_eq.symm; refl, }, h.trans add_submonoid.subsingleton_iff @[simp] lemma nontrivial_iff : nontrivial (submodule R M) ↔ nontrivial M := not_iff_not.mp ( (not_nontrivial_iff_subsingleton.trans $ subsingleton_iff R).trans not_nontrivial_iff_subsingleton.symm) variables {R} instance [subsingleton M] : unique (submodule R M) := ⟨⟨⊥⟩, λ a, @subsingleton.elim _ ((subsingleton_iff R).mpr ‹_›) a _⟩ instance unique' [subsingleton R] : unique (submodule R M) := by haveI := module.subsingleton R M; apply_instance instance [nontrivial M] : nontrivial (submodule R M) := (nontrivial_iff R).mpr ‹_› 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 theorem disjoint_def' {p p' : submodule R M} : disjoint p p' ↔ ∀ (x ∈ p) (y ∈ p'), x = y → x = (0:M) := disjoint_def.trans ⟨λ h x hx y hy hxy, h x hx $ hxy.symm ▸ hy, λ h x hx hx', h _ hx x hx' rfl⟩ theorem mem_right_iff_eq_zero_of_disjoint {p p' : submodule R M} (h : disjoint p p') {x : p} : (x:M) ∈ p' ↔ x = 0 := ⟨λ hx, coe_eq_zero.1 $ disjoint_def.1 h x x.2 hx, λ h, h.symm ▸ p'.zero_mem⟩ theorem mem_left_iff_eq_zero_of_disjoint {p p' : submodule R M} (h : disjoint p p') {x : p'} : (x:M) ∈ p ↔ x = 0 := ⟨λ hx, coe_eq_zero.1 $ disjoint_def.1 h x hx x.2, λ h, h.symm ▸ p.zero_mem⟩ /-- 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, smul_mem' := by rintro a _ ⟨b, hb, rfl⟩; exact ⟨_, p.smul_mem _ hb, f.map_smul _ _⟩, .. p.to_add_submonoid.map f.to_add_monoid_hom } @[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 apply_coe_mem_map (f : M →ₗ[R] M₂) {p : submodule R M} (r : p) : f r ∈ map f p := mem_map_of_mem r.prop @[simp] 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) := set_like.coe_injective $ 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] lemma map_add_le (f g : M →ₗ[R] M₂) : map (f + g) p ≤ map f p + map g p := begin rintros x ⟨m, hm, rfl⟩, exact add_mem_sup (mem_map_of_mem hm) (mem_map_of_mem hm), end lemma range_map_nonempty (N : submodule R M) : (set.range (λ ϕ, submodule.map ϕ N : (M →ₗ[R] M₂) → submodule R M₂)).nonempty := ⟨_, set.mem_range.mpr ⟨0, rfl⟩⟩ /-- The pushforward of a submodule by an injective linear map is linearly equivalent to the original submodule. -/ noncomputable def equiv_map_of_injective (f : M →ₗ[R] M₂) (i : injective f) (p : submodule R M) : p ≃ₗ[R] p.map f := { map_add' := by { intros, simp, refl, }, map_smul' := by { intros, simp, refl, }, ..(equiv.set.image f p i) } @[simp] lemma coe_equiv_map_of_injective_apply (f : M →ₗ[R] M₂) (i : injective f) (p : submodule R M) (x : p) : (equiv_map_of_injective f i p x : M₂) = f x := rfl /-- 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, smul_mem' := λ a x h, by simp [p.smul_mem _ h], .. p.to_add_submonoid.comap f.to_add_monoid_hom } @[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 := set_like.coe_injective 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 _ section galois_insertion variables {f : M →ₗ[R] M₂} (hf : surjective f) include hf /-- `map f` and `comap f` form a `galois_insertion` when `f` is surjective. -/ def gi_map_comap : galois_insertion (map f) (comap f) := (gc_map_comap f).to_galois_insertion (λ S x hx, begin rcases hf x with ⟨y, rfl⟩, simp only [mem_map, mem_comap], exact ⟨y, hx, rfl⟩ end) lemma map_comap_eq_of_surjective (p : submodule R M₂) : (p.comap f).map f = p := (gi_map_comap hf).l_u_eq _ lemma map_surjective_of_surjective : function.surjective (map f) := (gi_map_comap hf).l_surjective lemma comap_injective_of_surjective : function.injective (comap f) := (gi_map_comap hf).u_injective lemma map_sup_comap_of_surjective (p q : submodule R M₂) : (p.comap f ⊔ q.comap f).map f = p ⊔ q := (gi_map_comap hf).l_sup_u _ _ lemma map_supr_comap_of_sujective (S : ι → submodule R M₂) : (⨆ i, (S i).comap f).map f = supr S := (gi_map_comap hf).l_supr_u _ lemma map_inf_comap_of_surjective (p q : submodule R M₂) : (p.comap f ⊓ q.comap f).map f = p ⊓ q := (gi_map_comap hf).l_inf_u _ _ lemma map_infi_comap_of_surjective (S : ι → submodule R M₂) : (⨅ i, (S i).comap f).map f = infi S := (gi_map_comap hf).l_infi_u _ lemma comap_le_comap_iff_of_surjective (p q : submodule R M₂) : p.comap f ≤ q.comap f ↔ p ≤ q := (gi_map_comap hf).u_le_u_iff lemma comap_strict_mono_of_surjective : strict_mono (comap f) := (gi_map_comap hf).strict_mono_u end galois_insertion section galois_coinsertion variables {f : M →ₗ[R] M₂} (hf : injective f) include hf /-- `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. -/ def gci_map_comap : galois_coinsertion (map f) (comap f) := (gc_map_comap f).to_galois_coinsertion (λ S x, by simp [mem_comap, mem_map, hf.eq_iff]) lemma comap_map_eq_of_injective (p : submodule R M) : (p.map f).comap f = p := (gci_map_comap hf).u_l_eq _ lemma comap_surjective_of_injective : function.surjective (comap f) := (gci_map_comap hf).u_surjective lemma map_injective_of_injective : function.injective (map f) := (gci_map_comap hf).l_injective lemma comap_inf_map_of_injective (p q : submodule R M) : (p.map f ⊓ q.map f).comap f = p ⊓ q := (gci_map_comap hf).u_inf_l _ _ lemma comap_infi_map_of_injective (S : ι → submodule R M) : (⨅ i, (S i).map f).comap f = infi S := (gci_map_comap hf).u_infi_l _ lemma comap_sup_map_of_injective (p q : submodule R M) : (p.map f ⊔ q.map f).comap f = p ⊔ q := (gci_map_comap hf).u_sup_l _ _ lemma comap_supr_map_of_injective (S : ι → submodule R M) : (⨆ i, (S i).map f).comap f = supr S := (gci_map_comap hf).u_supr_l _ lemma map_le_map_iff_of_injective (p q : submodule R M) : p.map f ≤ q.map f ↔ p ≤ q := (gci_map_comap hf).l_le_l_iff lemma map_strict_mono_of_injective : strict_mono (map f) := (gci_map_comap hf).strict_mono_l end galois_coinsertion --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 lemma map_span (f : M →ₗ[R] M₂) (s : set M) : (span R s).map f = span R (f '' s) := eq.symm $ span_eq_of_le _ (set.image_subset f subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ λ x hx, subset_span ⟨x, hx, rfl⟩ alias submodule.map_span ← linear_map.map_span lemma map_span_le {R M M₂ : Type*} [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module R M₂] (f : M →ₗ[R] M₂) (s : set M) (N : submodule R M₂) : map f (span R s) ≤ N ↔ ∀ m ∈ s, f m ∈ N := begin rw [f.map_span, span_le, set.image_subset_iff], exact iff.rfl end alias submodule.map_span_le ← linear_map.map_span_le /- See also `span_preimage_eq` below. -/ lemma span_preimage_le (f : M →ₗ[R] M₂) (s : set M₂) : span R (f ⁻¹' s) ≤ (span R s).comap f := by { rw [span_le, comap_coe], exact preimage_mono (subset_span), } alias submodule.span_preimage_le ← linear_map.span_preimage_le /-- 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 lemma span_nat_eq_add_submonoid_closure (s : set M) : (span ℕ s).to_add_submonoid = add_submonoid.closure s := begin refine eq.symm (add_submonoid.closure_eq_of_le subset_span _), apply add_submonoid.to_nat_submodule.symm.to_galois_connection.l_le _, rw span_le, exact add_submonoid.subset_closure, end @[simp] lemma span_nat_eq (s : add_submonoid M) : (span ℕ (s : set M)).to_add_submonoid = s := by rw [span_nat_eq_add_submonoid_closure, s.closure_eq] lemma span_int_eq_add_subgroup_closure {M : Type*} [add_comm_group M] (s : set M) : (span ℤ s).to_add_subgroup = add_subgroup.closure s := eq.symm $ add_subgroup.closure_eq_of_le _ subset_span $ λ x hx, span_induction hx (λ x hx, add_subgroup.subset_closure hx) (add_subgroup.zero_mem _) (λ _ _, add_subgroup.add_mem _) (λ _ _ _, add_subgroup.gsmul_mem _ ‹_› _) @[simp] lemma span_int_eq {M : Type*} [add_comm_group M] (s : add_subgroup M) : (span ℤ (s : set M)).to_add_subgroup = s := by rw [span_int_eq_add_subgroup_closure, s.closure_eq] 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 $ set_like.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 lemma span_eq_supr_of_singleton_spans (s : set M) : span R s = ⨆ x ∈ s, span R {x} := by simp only [←span_Union, set.bUnion_of_singleton s] @[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 @[simp] theorem mem_supr_of_directed {ι} [nonempty ι] (S : ι → submodule R M) (H : directed (≤) S) {x} : x ∈ supr S ↔ ∃ i, x ∈ S i := by { rw [← set_like.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, simp only [Sup_eq_supr', mem_supr_of_directed _ hdir.directed_coe, set_coe.exists, subtype.coe_mk] end @[norm_cast, simp] lemma coe_supr_of_chain (a : ℕ →ₘ submodule R M) : (↑(⨆ k, a k) : set M) = ⋃ k, (a k : set M) := coe_supr_of_directed a a.monotone.directed_le /-- We can regard `coe_supr_of_chain` as the statement that `coe : (submodule R M) → set M` is Scott continuous for the ω-complete partial order induced by the complete lattice structures. -/ lemma coe_scott_continuous : omega_complete_partial_order.continuous' (coe : submodule R M → set M) := ⟨set_like.coe_mono, coe_supr_of_chain⟩ @[simp] lemma mem_supr_of_chain (a : ℕ →ₘ submodule R M) (m : M) : m ∈ (⨆ k, a k) ↔ ∃ k, m ∈ a k := mem_supr_of_directed a a.monotone.directed_le 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 [add_assoc]; 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 [set_like.exists, coe_mk] lemma coe_sup : ↑(p ⊔ p') = (p + p' : set M) := by { ext, rw [set_like.mem_coe, mem_sup, set.mem_add], simp, } end /- This is the character `∙`, with escape sequence `\.`, and is thus different from the scalar multiplication character `•`, with escape sequence `\bub`. -/ notation R`∙`:1000 x := span R (@singleton _ _ set.has_singleton x) lemma mem_span_singleton_self (x : M) : x ∈ R ∙ x := subset_span rfl lemma nontrivial_span_singleton {x : M} (h : x ≠ 0) : nontrivial (R ∙ x) := ⟨begin use [0, x, submodule.mem_span_singleton_self x], intros H, rw [eq_comm, submodule.mk_eq_zero] at H, exact h H end⟩ lemma mem_span_singleton {y : M} : x ∈ (R ∙ y) ↔ ∃ 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 le_span_singleton_iff {s : submodule R M} {v₀ : M} : s ≤ (R ∙ v₀) ↔ ∀ v ∈ s, ∃ r : R, r • v₀ = v := by simp_rw [set_like.le_def, mem_span_singleton] lemma span_singleton_eq_top_iff (x : M) : (R ∙ x) = ⊤ ↔ ∀ v, ∃ r : R, r • x = v := begin rw [eq_top_iff, le_span_singleton_iff], finish, end @[simp] lemma span_zero_singleton : (R ∙ (0:M)) = ⊥ := by { ext, simp [mem_span_singleton, eq_comm] } lemma span_singleton_eq_range (y : M) : ↑(R ∙ y) = range ((• y) : R → M) := set.ext $ λ x, mem_span_singleton lemma span_singleton_smul_le (r : R) (x : M) : (R ∙ (r • x)) ≤ R ∙ x := begin rw [span_le, set.singleton_subset_iff, set_like.mem_coe], exact smul_mem _ _ (mem_span_singleton_self _) end lemma span_singleton_smul_eq {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {r : K} (x : E) (hr : r ≠ 0) : (K ∙ (r • x)) = K ∙ x := begin refine le_antisymm (span_singleton_smul_le r x) _, convert span_singleton_smul_le r⁻¹ (r • x), exact (inv_smul_smul' hr _).symm end 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 (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 disjoint_span_singleton' {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {p : submodule K E} {x : E} (x0 : x ≠ 0) : disjoint p (K ∙ x) ↔ x ∉ p := disjoint_span_singleton.trans ⟨λ h₁ h₂, x0 (h₁ h₂), λ h₁ h₂, (h₁ h₂).elim⟩ 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 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)⟩ @[simp] lemma span_singleton_eq_bot : (R ∙ x) = ⊥ ↔ x = 0 := span_eq_bot.trans $ by simp @[simp] lemma span_zero : span R (0 : set M) = ⊥ := by rw [←singleton_zero, span_singleton_eq_bot] @[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 apply_mem_span_image_of_mem_span (f : M →ₗ[R] M₂) {x : M} {s : set M} (h : x ∈ submodule.span R s) : f x ∈ submodule.span R (f '' s) := begin rw submodule.span_image, exact submodule.mem_map_of_mem h end /-- `f` is an explicit argument so we can `apply` this theorem and obtain `h` as a new goal. -/ lemma not_mem_span_of_apply_not_mem_span_image (f : M →ₗ[R] M₂) {x : M} {s : set M} (h : f x ∉ submodule.span R (f '' s)) : x ∉ submodule.span R s := not.imp h (apply_mem_span_image_of_mem_span f) 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) : (R ∙ m) ≤ p ↔ m ∈ p := by rw [span_le, singleton_subset_iff, set_like.mem_coe] lemma singleton_span_is_compact_element (x : M) : complete_lattice.is_compact_element (span R {x} : submodule R M) := begin rw complete_lattice.is_compact_element_iff_le_of_directed_Sup_le, intros d hemp hdir hsup, have : x ∈ Sup d, from (set_like.le_def.mp hsup) (mem_span_singleton_self x), obtain ⟨y, ⟨hyd, hxy⟩⟩ := (mem_Sup_of_directed hemp hdir).mp this, exact ⟨y, ⟨hyd, by simpa only [span_le, singleton_subset_iff]⟩⟩, end instance : is_compactly_generated (submodule R M) := ⟨λ s, ⟨(λ x, span R {x}) '' s, ⟨λ t ht, begin rcases (set.mem_image _ _ _).1 ht with ⟨x, hx, rfl⟩, apply singleton_span_is_compact_element, end, by rw [Sup_eq_supr, supr_image, ←span_eq_supr_of_singleton_spans, span_eq]⟩⟩⟩ lemma lt_add_iff_not_mem {I : submodule R M} {a : M} : I < I + (R ∙ a) ↔ a ∉ I := begin split, { intro h, by_contra akey, have h1 : I + (R ∙ a) ≤ I, { simp only [add_eq_sup, sup_le_iff], split, { exact le_refl I, }, { exact (span_singleton_le_iff_mem a I).mpr akey, } }, have h2 := gt_of_ge_of_gt h1 h, exact lt_irrefl I h2, }, { intro h, apply set_like.lt_iff_le_and_exists.mpr, split, simp only [add_eq_sup, le_sup_left], use a, split, swap, { assumption, }, { have : (R ∙ a) ≤ I + (R ∙ a) := le_sup_right, exact this (mem_span_singleton_self a), } }, end 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 section open_locale classical /-- For every element in the span of a set, there exists a finite subset of the set such that the element is contained in the span of the subset. -/ lemma mem_span_finite_of_mem_span {S : set M} {x : M} (hx : x ∈ span R S) : ∃ T : finset M, ↑T ⊆ S ∧ x ∈ span R (T : set M) := begin refine span_induction hx (λ x hx, _) _ _ _, { refine ⟨{x}, _, _⟩, { rwa [finset.coe_singleton, set.singleton_subset_iff] }, { rw finset.coe_singleton, exact submodule.mem_span_singleton_self x } }, { use ∅, simp }, { rintros x y ⟨X, hX, hxX⟩ ⟨Y, hY, hyY⟩, refine ⟨X ∪ Y, _, _⟩, { rw finset.coe_union, exact set.union_subset hX hY }, rw [finset.coe_union, span_union, mem_sup], exact ⟨x, hxX, y, hyY, rfl⟩, }, { rintros a x ⟨T, hT, h2⟩, exact ⟨T, hT, smul_mem _ _ h2⟩ } end end /-- The product of two submodules is a submodule. -/ def prod : submodule R (M × M₂) := { carrier := set.prod p q, smul_mem' := by rintro a ⟨x, y⟩ ⟨hx, hy⟩; exact ⟨smul_mem _ a hx, smul_mem _ a hy⟩, .. p.to_add_submonoid.prod q.to_add_submonoid } @[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') := set_like.coe_injective 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 [set_like.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 end add_comm_monoid 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 @[simp] lemma neg_coe : -(p : set M) = p := set.ext $ λ x, p.neg_mem_iff @[simp] protected lemma map_neg (f : M →ₗ[R] M₂) : map (-f) p = map f p := ext $ λ y, ⟨λ ⟨x, hx, hy⟩, hy ▸ ⟨-x, neg_mem _ hx, f.map_neg x⟩, λ ⟨x, hx, hy⟩, hy ▸ ⟨-x, neg_mem _ hx, ((-f).map_neg _).trans (neg_neg (f x))⟩⟩ @[simp] lemma span_neg (s : set M) : span R (-s) = span R s := calc span R (-s) = span R ((-linear_map.id : M →ₗ[R] M) '' s) : by simp ... = map (-linear_map.id) (span R s) : ((-linear_map.id).map_span _).symm ... = span R s : by simp lemma mem_span_insert' {y} {s : set M} : 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, add_assoc]⟩ }, { rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp [add_comm, add_left_comm]⟩ } 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, add_assoc] 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) : (@_root_.quotient.mk _ (quotient_rel p) x) = 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 : has_sub (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₁ (neg_mem p h₂)⟩ @[simp] theorem mk_sub : (mk (x - y) : quotient p) = mk x - mk y := rfl instance : add_comm_group (quotient p) := { zero := (0 : quotient p), add := (+), neg := has_neg.neg, sub := has_sub.sub, add_assoc := by { rintros ⟨x⟩ ⟨y⟩ ⟨z⟩, simp only [←mk_add p, quot_mk_eq_mk, add_assoc] }, zero_add := by { rintro ⟨x⟩, simp only [←mk_zero p, ←mk_add p, quot_mk_eq_mk, zero_add] }, add_zero := by { rintro ⟨x⟩, simp only [←mk_zero p, ←mk_add p, add_zero, quot_mk_eq_mk] }, add_comm := by { rintros ⟨x⟩ ⟨y⟩, simp only [←mk_add p, quot_mk_eq_mk, add_comm] }, add_left_neg := by { rintro ⟨x⟩, simp only [←mk_zero p, ←mk_add p, ←mk_neg p, quot_mk_eq_mk, add_left_neg] }, sub_eq_add_neg := by { rintros ⟨x⟩ ⟨y⟩, simp only [←mk_add p, ←mk_neg p, ←mk_sub p, sub_eq_add_neg, quot_mk_eq_mk] }, nsmul := λ n x, quotient.lift_on' x (λ x, mk (n • x)) $ λ x y h, (quotient.eq p).2 $ by simpa [smul_sub] using smul_of_tower_mem p n h, nsmul_zero' := by { rintros ⟨⟩, simp only [mk_zero, quot_mk_eq_mk, zero_smul], refl }, nsmul_succ' := by { rintros n ⟨⟩, simp only [nat.succ_eq_one_add, add_nsmul, mk_add, quot_mk_eq_mk, one_nsmul], refl }, gsmul := λ n x, quotient.lift_on' x (λ x, mk (n • x)) $ λ x y h, (quotient.eq p).2 $ by simpa [smul_sub] using smul_of_tower_mem p n h, gsmul_zero' := by { rintros ⟨⟩, simp only [mk_zero, quot_mk_eq_mk, zero_smul], refl }, gsmul_succ' := by { rintros n ⟨⟩, simp [nat.succ_eq_add_one, add_nsmul, mk_add, quot_mk_eq_mk, one_nsmul, add_smul, add_comm], refl }, gsmul_neg' := by { rintros n ⟨x⟩, simp_rw [gsmul_neg_succ_of_nat, gsmul_coe_nat], refl }, } 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 @[simp] theorem mk_nsmul (n : ℕ) : (mk (n • x) : quotient p) = n • 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] lemma mk_surjective : function.surjective (@mk _ _ _ _ _ p) := by { rintros ⟨x⟩, exact ⟨x, rfl⟩ } lemma nontrivial_of_lt_top (h : p < ⊤) : nontrivial (p.quotient) := begin obtain ⟨x, _, not_mem_s⟩ := set_like.exists_of_lt h, refine ⟨⟨mk x, 0, _⟩⟩, simpa using not_mem_s end 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] [module K V] variables [add_comm_group V₂] [module 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 /-! ### Properties of linear maps -/ namespace linear_map section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [module R M] [module R M₂] [module R M₃] include R open submodule /-- If two linear maps are equal on a set `s`, then they are equal on `submodule.span s`. See also `linear_map.eq_on_span'` for a version using `set.eq_on`. -/ lemma eq_on_span {s : set M} {f g : M →ₗ[R] M₂} (H : set.eq_on f g s) ⦃x⦄ (h : x ∈ span R s) : f x = g x := by apply span_induction h H; simp {contextual := tt} /-- If two linear maps are equal on a set `s`, then they are equal on `submodule.span s`. This version uses `set.eq_on`, and the hidden argument will expand to `h : x ∈ (span R s : set M)`. See `linear_map.eq_on_span` for a version that takes `h : x ∈ span R s` as an argument. -/ lemma eq_on_span' {s : set M} {f g : M →ₗ[R] M₂} (H : set.eq_on f g s) : set.eq_on f g (span R s : set M) := eq_on_span H /-- If `s` generates the whole module and linear maps `f`, `g` are equal on `s`, then they are equal. -/ lemma ext_on {s : set M} {f g : M →ₗ[R] M₂} (hv : span R s = ⊤) (h : set.eq_on f g s) : f = g := linear_map.ext (λ x, eq_on_span h (eq_top_iff'.1 hv _)) /-- If the range of `v : ι → M` generates the whole module and linear maps `f`, `g` are equal at each `v i`, then they are equal. -/ lemma ext_on_range {v : ι → M} {f g : M →ₗ[R] M₂} (hv : span R (set.range v) = ⊤) (h : ∀i, f (v i) = g (v i)) : f = g := ext_on hv (set.forall_range_iff.2 h) section finsupp variables {γ : Type*} [has_zero γ] @[simp] lemma map_finsupp_sum (f : M →ₗ[R] M₂) {t : ι →₀ γ} {g : ι → γ → M} : f (t.sum g) = t.sum (λ i d, f (g i d)) := f.map_sum lemma coe_finsupp_sum (t : ι →₀ γ) (g : ι → γ → M →ₗ[R] M₂) : ⇑(t.sum g) = t.sum (λ i d, g i d) := coe_fn_sum _ _ @[simp] lemma finsupp_sum_apply (t : ι →₀ γ) (g : ι → γ → M →ₗ[R] M₂) (b : M) : (t.sum g) b = t.sum (λ i d, g i d b) := sum_apply _ _ _ end finsupp section dfinsupp open dfinsupp variables {γ : ι → Type*} [decidable_eq ι] section sum variables [Π i, has_zero (γ i)] [Π i (x : γ i), decidable (x ≠ 0)] @[simp] lemma map_dfinsupp_sum (f : M →ₗ[R] M₂) {t : Π₀ i, γ i} {g : Π i, γ i → M} : f (t.sum g) = t.sum (λ i d, f (g i d)) := f.map_sum lemma coe_dfinsupp_sum (t : Π₀ i, γ i) (g : Π i, γ i → M →ₗ[R] M₂) : ⇑(t.sum g) = t.sum (λ i d, g i d) := coe_fn_sum _ _ @[simp] lemma dfinsupp_sum_apply (t : Π₀ i, γ i) (g : Π i, γ i → M →ₗ[R] M₂) (b : M) : (t.sum g) b = t.sum (λ i d, g i d b) := sum_apply _ _ _ end sum section sum_add_hom variables [Π i, add_zero_class (γ i)] @[simp] lemma map_dfinsupp_sum_add_hom (f : M →ₗ[R] M₂) {t : Π₀ i, γ i} {g : Π i, γ i →+ M} : f (sum_add_hom g t) = sum_add_hom (λ i, f.to_add_monoid_hom.comp (g i)) t := f.to_add_monoid_hom.map_dfinsupp_sum_add_hom _ _ end sum_add_hom end dfinsupp 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.ext_iff_val] 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₂`. See Note [range copy pattern]. -/ def range (f : M →ₗ[R] M₂) : submodule R M₂ := (map f ⊤).copy (set.range f) set.image_univ.symm theorem range_coe (f : M →ₗ[R] M₂) : (range f : set M₂) = set.range f := rfl @[simp] theorem mem_range {f : M →ₗ[R] M₂} {x} : x ∈ range f ↔ ∃ y, f y = x := iff.rfl lemma range_eq_map (f : M →ₗ[R] M₂) : f.range = map f ⊤ := by { ext, simp } theorem mem_range_self (f : M →ₗ[R] M₂) (x : M) : f x ∈ f.range := ⟨x, rfl⟩ @[simp] theorem range_id : range (linear_map.id : M →ₗ[R] M) = ⊤ := set_like.coe_injective set.range_id theorem range_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : range (g.comp f) = map g (range f) := set_like.coe_injective (set.range_comp g f) theorem range_comp_le_range (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : range (g.comp f) ≤ range g := set_like.coe_mono (set.range_comp_subset_range f g) theorem range_eq_top {f : M →ₗ[R] M₂} : range f = ⊤ ↔ surjective f := by rw [set_like.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_eq_map, 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 := set_like.coe_mono (set.image_subset_range f p) /-- The decreasing sequence of submodules consisting of the ranges of the iterates of a linear map. -/ @[simps] def iterate_range {R M} [ring R] [add_comm_group M] [module R M] (f : M →ₗ[R] M) : ℕ →ₘ order_dual (submodule R M) := ⟨λ n, (f ^ n).range, λ n m w x h, begin obtain ⟨c, rfl⟩ := le_iff_exists_add.mp w, rw linear_map.mem_range at h, obtain ⟨m, rfl⟩ := h, rw linear_map.mem_range, use (f ^ c) m, rw [pow_add, linear_map.mul_apply], end⟩ /-- Restrict the codomain of a linear map `f` to `f.range`. This is the bundled version of `set.range_factorization`. -/ @[reducible] def range_restrict (f : M →ₗ[R] M₂) : M →ₗ[R] f.range := f.cod_restrict f.range f.mem_range_self /-- The range of a linear map is finite if the domain is finite. Note: this instance can form a diamond with `subtype.fintype` in the presence of `fintype M₂`. -/ instance fintype_range [fintype M] [decidable_eq M₂] (f : M →ₗ[R] M₂) : fintype (range f) := set.fintype_range f section variables (R) (M) /-- Given an element `x` of a module `M` over `R`, the natural map from `R` to scalar multiples of `x`.-/ def to_span_singleton (x : M) : R →ₗ[R] M := linear_map.id.smul_right x /-- The range of `to_span_singleton x` is the span of `x`.-/ lemma span_singleton_eq_range (x : M) : (R ∙ x) = (to_span_singleton R M x).range := submodule.ext $ λ y, by {refine iff.trans _ mem_range.symm, exact mem_span_singleton } lemma to_span_singleton_one (x : M) : to_span_singleton R M x 1 = x := one_smul _ _ end /-- 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 lemma comp_ker_subtype (f : M →ₗ[R] M₂) : f.comp f.ker.subtype = 0 := linear_map.ext $ λ x, suffices f x = 0, by simp [this], 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 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 ker_eq_bot' {f : M →ₗ[R] M₂} : ker f = ⊥ ↔ (∀ m, f m = 0 → m = 0) := by simpa [disjoint] using @disjoint_ker _ _ _ _ _ _ _ _ f ⊤ theorem ker_eq_bot_of_inverse {f : M →ₗ[R] M₂} {g : M₂ →ₗ[R] M} (h : g.comp f = id) : ker f = ⊥ := ker_eq_bot'.2 $ λ m hm, by rw [← id_apply m, ← h, comp_apply, hm, g.map_zero] 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 := by simpa only [range_eq_map] using map_cod_restrict _ _ _ _ lemma ker_restrict {p : submodule R M} {f : M →ₗ[R] M} (hf : ∀ x : M, x ∈ p → f x ∈ p) : ker (f.restrict hf) = (f.dom_restrict p).ker := by rw [restrict_eq_cod_restrict_dom_restrict, ker_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_le_range (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] @[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₂) = ⊥ := by simpa only [range_eq_map] using 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 theorem range_eq_bot {f : M →ₗ[R] M₂} : range f = ⊥ ↔ f = 0 := by rw [← range_le_bot_iff, le_bot_iff] 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 $ ⟨_, rfl⟩, λ h x hx, mem_ker.2 $ exists.elim hx $ λ y hy, by rw [←hy, ←comp_apply, h, zero_apply]⟩ 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 ker_eq_bot_of_injective {f : M →ₗ[R] M₂} (hf : injective f) : ker f = ⊥ := begin have : disjoint ⊤ f.ker, by { rw [disjoint_ker, ← map_zero f], exact λ x hx H, hf H }, simpa [disjoint] end /-- The increasing sequence of submodules consisting of the kernels of the iterates of a linear map. -/ @[simps] def iterate_ker {R M} [ring R] [add_comm_group M] [module R M] (f : M →ₗ[R] M) : ℕ →ₘ submodule R M := ⟨λ n, (f ^ n).ker, λ n m w x h, begin obtain ⟨c, rfl⟩ := le_iff_exists_add.mp w, rw linear_map.mem_ker at h, rw [linear_map.mem_ker, add_comm, pow_add, linear_map.mul_apply, h, linear_map.map_zero], end⟩ end add_comm_monoid section add_comm_group variables [semiring 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 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 rw [comap_map_eq, sup_of_le_left h] theorem map_le_map_iff (f : M →ₗ[R] M₂) {p p'} : map f p ≤ map f p' ↔ p ≤ p' ⊔ ker f := by rw [map_le_iff_le_comap, comap_map_eq] theorem map_le_map_iff' {f : M →ₗ[R] M₂} (hf : ker f = ⊥) {p p'} : map f p ≤ map f p' ↔ p ≤ p' := by rw [map_le_map_iff, hf, sup_bot_eq] 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 map_eq_top_iff {f : M →ₗ[R] M₂} (hf : range f = ⊤) {p : submodule R M} : p.map f = ⊤ ↔ p ⊔ f.ker = ⊤ := by simp_rw [← top_le_iff, ← hf, range_eq_map, map_le_map_iff] end add_comm_group section ring 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₂} include R open submodule theorem sub_mem_ker_iff {x y} : x - y ∈ f.ker ↔ f x = f y := by rw [mem_ker, map_sub, sub_eq_zero] theorem disjoint_ker' {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 {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) theorem ker_eq_bot : ker f = ⊥ ↔ injective f := by simpa [disjoint] using @disjoint_ker' _ _ _ _ _ _ _ _ f ⊤ lemma ker_le_iff {p : submodule R M} : ker f ≤ p ↔ ∃ (y ∈ range f), f ⁻¹' {y} ⊆ p := begin split, { intros h, use 0, rw [← set_like.mem_coe, f.range_coe], exact ⟨⟨0, map_zero f⟩, h⟩, }, { rintros ⟨y, h₁, h₂⟩, rw set_like.le_def, intros z hz, simp only [mem_ker, set_like.mem_coe] at hz, rw [← set_like.mem_coe, f.range_coe, set.mem_range] at h₁, obtain ⟨x, hx⟩ := h₁, have hx' : x ∈ p, { exact h₂ hx, }, have hxz : z + x ∈ p, { apply h₂, simp [hx, hz], }, suffices : z + x - x ∈ p, { simpa only [this, add_sub_cancel], }, exact p.sub_mem hxz hx', }, end end ring section field variables [field K] variables [add_comm_group V] [module K V] variables [add_comm_group V₂] [module 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 := by simpa only [range_eq_map] using submodule.map_smul f _ a h lemma range_smul' (f : V →ₗ[K] V₂) (a : K) : range (a • f) = ⨆(h : a ≠ 0), range f := by simpa only [range_eq_map] using submodule.map_smul' f _ a lemma span_singleton_sup_ker_eq_top (f : V →ₗ[K] K) {x : V} (hx : f x ≠ 0) : (K ∙ x) ⊔ f.ker = ⊤ := eq_top_iff.2 (λ y hy, submodule.mem_sup.2 ⟨(f y * (f x)⁻¹) • x, submodule.mem_span_singleton.2 ⟨f y * (f x)⁻¹, rfl⟩, ⟨y - (f y * (f x)⁻¹) • x, by rw [linear_map.mem_ker, f.map_sub, f.map_smul, smul_eq_mul, mul_assoc, inv_mul_cancel hx, mul_one, sub_self], by simp only [add_sub_cancel'_right]⟩⟩) end field end linear_map namespace is_linear_map lemma is_linear_map_add [semiring R] [add_comm_monoid 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*} [semiring 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 section add_comm_monoid variables {T : semiring R} [add_comm_monoid M] [add_comm_monoid M₂] variables [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 := f.range_eq_map.symm @[simp] theorem comap_bot (f : M →ₗ[R] M₂) : comap f ⊥ = ker f := rfl @[simp] theorem ker_subtype : p.subtype.ker = ⊥ := ker_eq_bot_of_injective $ λ x y, subtype.ext_val @[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_le_range : 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} : comap p.subtype p' = ⊤ ↔ p ≤ p' := eq_top_iff.trans $ map_le_iff_le_comap.symm.trans $ by rw [map_subtype_top] @[simp] lemma comap_subtype_self : comap p.subtype p = ⊤ := comap_subtype_eq_top.2 (le_refl _) @[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] end add_comm_monoid section ring 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 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, disjoint] /-- If `N ⊆ M` then submodules of `N` are the same as submodules of `M` contained in `N` -/ def map_subtype.rel_iso : submodule R p ≃o {p' : submodule R M // p' ≤ p} := { 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.ext_val $ by simp [map_comap_subtype p, inf_of_le_right hq], map_rel_iff' := λ p₁ p₂, map_le_map_iff' (ker_subtype p) } /-- If `p ⊆ M` is a submodule, the ordering of submodules of `p` is embedded in the ordering of submodules of `M`. -/ def map_subtype.order_embedding : submodule R p ↪o submodule R M := (rel_iso.to_rel_embedding $ map_subtype.rel_iso p).trans (subtype.rel_embedding _ _) @[simp] lemma map_subtype_embedding_eq (p' : submodule R p) : map_subtype.order_embedding p p' = map p.subtype p' := rfl /-- 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 := { to_fun := quotient.mk, map_add' := by simp, map_smul' := by simp } @[simp] theorem mkq_apply (x : M) : p.mkq x = quotient.mk x := rfl /-- Two `linear_map`s from a quotient module are equal if their compositions with `submodule.mkq` are equal. See note [partially-applied ext lemmas]. -/ @[ext] lemma linear_map_qext ⦃f g : p.quotient →ₗ[R] M₂⦄ (h : f.comp p.mkq = g.comp p.mkq) : f = g := linear_map.ext $ λ x, quotient.induction_on' x $ (linear_map.congr_fun h : _) /-- 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₂ := { to_fun := λ x, _root_.quotient.lift_on' x f $ λ a b (ab : a - b ∈ p), eq_of_sub_eq_zero $ by simpa using h ab, map_add' := by rintro ⟨x⟩ ⟨y⟩; exact f.map_add x y, map_smul' := 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, 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] @[simp] theorem map_mkq_eq_top : map p.mkq p' = ⊤ ↔ p ⊔ p' = ⊤ := by simp only [map_eq_top_iff p.range_mkq, sup_comm, ker_mkq] /-- 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 := by simpa only [range_eq_map] using 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.rel_iso : submodule R p.quotient ≃o {p' : submodule R M // p ≤ p'} := { 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.ext_val $ by simpa [comap_map_mkq p], map_rel_iff' := λ p₁ p₂, comap_le_comap_iff $ range_mkq _ } /-- The ordering on submodules of the quotient of `M` by `p` embeds into the ordering on submodules of `M`. -/ def comap_mkq.order_embedding : submodule R p.quotient ↪o submodule R M := (rel_iso.to_rel_embedding $ comap_mkq.rel_iso p).trans (subtype.rel_embedding _ _) @[simp] lemma comap_mkq_embedding_eq (p' : submodule R p.quotient) : comap_mkq.order_embedding p p' = comap p.mkq p' := rfl lemma span_preimage_eq {f : M →ₗ[R] M₂} {s : set M₂} (h₀ : s.nonempty) (h₁ : s ⊆ range f) : span R (f ⁻¹' s) = (span R s).comap f := begin suffices : (span R s).comap f ≤ span R (f ⁻¹' s), { exact le_antisymm (span_preimage_le f s) this, }, have hk : ker f ≤ span R (f ⁻¹' s), { let y := classical.some h₀, have hy : y ∈ s, { exact classical.some_spec h₀, }, rw ker_le_iff, use [y, h₁ hy], rw ← set.singleton_subset_iff at hy, exact set.subset.trans subset_span (span_mono (set.preimage_mono hy)), }, rw ← left_eq_sup at hk, rw f.range_coe at h₁, rw [hk, ← map_le_map_iff, map_span, map_comap_eq, set.image_preimage_eq_of_subset h₁], exact inf_le_right, end end ring end submodule namespace linear_map section semiring variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [module R M] [module R M₂] [module R M₃] /-- A monomorphism is injective. -/ lemma ker_eq_bot_of_cancel {f : M →ₗ[R] M₂} (h : ∀ (u v : f.ker →ₗ[R] M), f.comp u = f.comp v → u = v) : f.ker = ⊥ := begin have h₁ : f.comp (0 : f.ker →ₗ[R] M) = 0 := comp_zero _, rw [←submodule.range_subtype f.ker, ←h 0 f.ker.subtype (eq.trans h₁ (comp_ker_subtype f).symm)], exact range_zero end lemma range_comp_of_range_eq_top {f : M →ₗ[R] M₂} (g : M₂ →ₗ[R] M₃) (hf : range f = ⊤) : range (g.comp f) = range g := by rw [range_comp, hf, submodule.map_top] lemma ker_comp_of_ker_eq_bot (f : M →ₗ[R] M₂) {g : M₂ →ₗ[R] M₃} (hg : ker g = ⊥) : ker (g.comp f) = ker f := by rw [ker_comp, hg, submodule.comap_bot] end semiring section ring variables [ring R] [add_comm_monoid M] [add_comm_group M₂] [add_comm_monoid M₃] variables [module R M] [module R M₂] [module R M₃] lemma range_mkq_comp (f : M →ₗ[R] M₂) : f.range.mkq.comp f = 0 := linear_map.ext $ λ x, by simp lemma ker_le_range_iff {f : M →ₗ[R] M₂} {g : M₂ →ₗ[R] M₃} : g.ker ≤ f.range ↔ f.range.mkq.comp g.ker.subtype = 0 := by rw [←range_le_ker_iff, submodule.ker_mkq, submodule.range_subtype] /-- An epimorphism is surjective. -/ lemma range_eq_top_of_cancel {f : M →ₗ[R] M₂} (h : ∀ (u v : M₂ →ₗ[R] f.range.quotient), u.comp f = v.comp f → u = v) : f.range = ⊤ := begin have h₁ : (0 : M₂ →ₗ[R] f.range.quotient).comp f = 0 := zero_comp _, rw [←submodule.ker_mkq f.range, ←h 0 f.range.mkq (eq.trans h₁ (range_mkq_comp _).symm)], exact ker_zero end end ring end linear_map @[simp] lemma linear_map.range_range_restrict [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module R M₂] (f : M →ₗ[R] M₂) : f.range_restrict.range = ⊤ := by simp [f.range_cod_restrict _] /-! ### Linear equivalences -/ namespace linear_equiv section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] section subsingleton variables [module R M] [module R M₂] [subsingleton M] [subsingleton M₂] /-- Between two zero modules, the zero map is an equivalence. -/ instance : has_zero (M ≃ₗ[R] M₂) := ⟨{ to_fun := 0, inv_fun := 0, right_inv := λ x, subsingleton.elim _ _, left_inv := λ x, subsingleton.elim _ _, ..(0 : M →ₗ[R] M₂)}⟩ -- Even though these are implied by `subsingleton.elim` via the `unique` instance below, they're -- nice to have as `rfl`-lemmas for `dsimp`. @[simp] lemma zero_symm : (0 : M ≃ₗ[R] M₂).symm = 0 := rfl @[simp] lemma coe_zero : ⇑(0 : M ≃ₗ[R] M₂) = 0 := rfl lemma zero_apply (x : M) : (0 : M ≃ₗ[R] M₂) x = 0 := rfl /-- Between two zero modules, the zero map is the only equivalence. -/ instance : unique (M ≃ₗ[R] M₂) := { uniq := λ f, to_linear_map_injective (subsingleton.elim _ _), default := 0 } end subsingleton section variables {module_M : module R M} {module_M₂ : module R M₂} variables (e e' : M ≃ₗ[R] M₂) lemma map_eq_comap {p : submodule R M} : (p.map e : submodule R M₂) = p.comap e.symm := set_like.coe_injective $ by simp [e.image_eq_preimage] /-- A linear equivalence of two modules restricts to a linear equivalence from any submodule `p` of the domain onto the image of that submodule. This is `linear_equiv.of_submodule'` but with `map` on the right instead of `comap` on the left. -/ def of_submodule (p : submodule R M) : p ≃ₗ[R] ↥(p.map ↑e : submodule R M₂) := { inv_fun := λ y, ⟨e.symm y, by { rcases y with ⟨y', hy⟩, rw submodule.mem_map at hy, rcases hy with ⟨x, hx, hxy⟩, subst hxy, simp only [symm_apply_apply, submodule.coe_mk, coe_coe, hx], }⟩, left_inv := λ x, by simp, right_inv := λ y, by { apply set_coe.ext, simp, }, ..((e : M →ₗ[R] M₂).dom_restrict p).cod_restrict (p.map ↑e) (λ x, ⟨x, by simp⟩) } @[simp] lemma of_submodule_apply (p : submodule R M) (x : p) : ↑(e.of_submodule p x) = e x := rfl @[simp] lemma of_submodule_symm_apply (p : submodule R M) (x : (p.map ↑e : submodule R M₂)) : ↑((e.of_submodule p).symm x) = e.symm x := rfl end section finsupp variables {γ : Type*} [module R M] [module R M₂] [has_zero γ] @[simp] lemma map_finsupp_sum (f : M ≃ₗ[R] M₂) {t : ι →₀ γ} {g : ι → γ → M} : f (t.sum g) = t.sum (λ i d, f (g i d)) := f.map_sum _ end finsupp section dfinsupp open dfinsupp variables {γ : ι → Type*} [decidable_eq ι] [module R M] [module R M₂] @[simp] lemma map_dfinsupp_sum [Π i, has_zero (γ i)] [Π i (x : γ i), decidable (x ≠ 0)] (f : M ≃ₗ[R] M₂) (t : Π₀ i, γ i) (g : Π i, γ i → M) : f (t.sum g) = t.sum (λ i d, f (g i d)) := f.map_sum _ @[simp] lemma map_dfinsupp_sum_add_hom [Π i, add_zero_class (γ i)] (f : M ≃ₗ[R] M₂) (t : Π₀ i, γ i) (g : Π i, γ i →+ M) : f (sum_add_hom g t) = sum_add_hom (λ i, f.to_add_equiv.to_add_monoid_hom.comp (g i)) t := f.to_add_equiv.map_dfinsupp_sum_add_hom _ _ end dfinsupp section uncurry variables (V V₂ R) /-- Linear equivalence between a curried and uncurried function. Differs from `tensor_product.curry`. -/ protected def curry : (V × V₂ → R) ≃ₗ[R] (V → V₂ → R) := { map_add' := λ _ _, by { ext, refl }, map_smul' := λ _ _, by { ext, refl }, .. equiv.curry _ _ _ } @[simp] lemma coe_curry : ⇑(linear_equiv.curry R V V₂) = curry := rfl @[simp] lemma coe_curry_symm : ⇑(linear_equiv.curry R V V₂).symm = uncurry := rfl end uncurry section variables {module_M : module R M} {module_M₂ : module R M₂} {module_M₃ : module R M₃} variables (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M) (e : M ≃ₗ[R] M₂) (h : M₂ →ₗ[R] M₃) (l : M₃ →ₗ[R] M) variables (p q : submodule R M) /-- Linear equivalence between two equal submodules. -/ def of_eq (h : p = q) : p ≃ₗ[R] q := { map_smul' := λ _ _, rfl, map_add' := λ _ _, rfl, .. equiv.set.of_eq (congr_arg _ h) } variables {p q} @[simp] lemma coe_of_eq_apply (h : p = q) (x : p) : (of_eq p q h x : M) = x := rfl @[simp] lemma of_eq_symm (h : p = q) : (of_eq p q h).symm = of_eq q p h.symm := rfl /-- A linear equivalence which maps a submodule of one module onto another, restricts to a linear equivalence of the two submodules. -/ def of_submodules (p : submodule R M) (q : submodule R M₂) (h : p.map ↑e = q) : p ≃ₗ[R] q := (e.of_submodule p).trans (linear_equiv.of_eq _ _ h) @[simp] lemma of_submodules_apply {p : submodule R M} {q : submodule R M₂} (h : p.map ↑e = q) (x : p) : ↑(e.of_submodules p q h x) = e x := rfl @[simp] lemma of_submodules_symm_apply {p : submodule R M} {q : submodule R M₂} (h : p.map ↑e = q) (x : q) : ↑((e.of_submodules p q h).symm x) = e.symm x := rfl /-- A linear equivalence of two modules restricts to a linear equivalence from the preimage of any submodule to that submodule. This is `linear_equiv.of_submodule` but with `comap` on the left instead of `map` on the right. -/ def of_submodule' [module R M] [module R M₂] (f : M ≃ₗ[R] M₂) (U : submodule R M₂) : U.comap (f : M →ₗ[R] M₂) ≃ₗ[R] U := (f.symm.of_submodules _ _ f.symm.map_eq_comap).symm lemma of_submodule'_to_linear_map [module R M] [module R M₂] (f : M ≃ₗ[R] M₂) (U : submodule R M₂) : (f.of_submodule' U).to_linear_map = (f.to_linear_map.dom_restrict _).cod_restrict _ subtype.prop := by { ext, refl } @[simp] lemma of_submodule'_apply [module R M] [module R M₂] (f : M ≃ₗ[R] M₂) (U : submodule R M₂) (x : U.comap (f : M →ₗ[R] M₂)) : (f.of_submodule' U x : M₂) = f (x : M) := rfl @[simp] lemma of_submodule'_symm_apply [module R M] [module R M₂] (f : M ≃ₗ[R] M₂) (U : submodule R M₂) (x : U) : ((f.of_submodule' U).symm x : M) = f.symm (x : M₂) := rfl variable (p) /-- 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 coe_of_top_symm_apply {h} (x : M) : ((of_top p h).symm x : M) = x := rfl theorem of_top_symm_apply {h} (x : M) : (of_top p h).symm x = ⟨x, h.symm ▸ trivial⟩ := rfl /-- 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 @[simp] protected theorem range : (e : M →ₗ[R] M₂).range = ⊤ := linear_map.range_eq_top.2 e.to_equiv.surjective lemma eq_bot_of_equiv [module R M₂] (e : p ≃ₗ[R] (⊥ : submodule R M₂)) : p = ⊥ := begin refine bot_unique (set_like.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 @[simp] protected theorem ker : (e : M →ₗ[R] M₂).ker = ⊥ := linear_map.ker_eq_bot_of_injective e.to_equiv.injective @[simp] theorem range_comp : (h.comp (e : M →ₗ[R] M₂)).range = h.range := linear_map.range_comp_of_range_eq_top _ e.range @[simp] theorem ker_comp : ((e : M →ₗ[R] M₂).comp l).ker = l.ker := linear_map.ker_comp_of_ker_eq_bot _ e.ker variables {f g} /-- An linear map `f : M →ₗ[R] M₂` with a left-inverse `g : M₂ →ₗ[R] M` defines a linear equivalence between `M` and `f.range`. This is a computable alternative to `linear_equiv.of_injective`, and a bidirectional version of `linear_map.range_restrict`. -/ def of_left_inverse {g : M₂ → M} (h : function.left_inverse g f) : M ≃ₗ[R] f.range := { to_fun := f.range_restrict, inv_fun := g ∘ f.range.subtype, left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := linear_map.mem_range.mp x.prop in show f (g x) = x, by rw [←hx', h x'], .. f.range_restrict } @[simp] lemma of_left_inverse_apply (h : function.left_inverse g f) (x : M) : ↑(of_left_inverse h x) = f x := rfl @[simp] lemma of_left_inverse_symm_apply (h : function.left_inverse g f) (x : f.range) : (of_left_inverse h).symm x = g x := rfl end end add_comm_monoid section add_comm_group variables [semiring R] variables [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] [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 e₁ : M ≃ₗ[R] M₂) (e₂ : M₃ ≃ₗ[R] M₄) @[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 end add_comm_group section neg variables (R) [semiring R] [add_comm_group M] [module R M] /-- `x ↦ -x` as a `linear_equiv` -/ def neg : M ≃ₗ[R] M := { .. equiv.neg M, .. (-linear_map.id : M →ₗ[R] M) } variable {R} @[simp] lemma coe_neg : ⇑(neg R : M ≃ₗ[R] M) = -id := rfl lemma neg_apply (x : M) : neg R x = -x := by simp @[simp] lemma symm_neg : (neg R : M ≃ₗ[R] M).symm = neg R := rfl end neg section ring variables [ring R] [add_comm_group M] [add_comm_group M₂] variables {module_M : module R M} {module_M₂ : module R M₂} variables (f : M →ₗ[R] M₂) (e : M ≃ₗ[R] M₂) /-- An `injective` linear map `f : M →ₗ[R] M₂` defines a linear equivalence between `M` and `f.range`. See also `linear_map.of_left_inverse`. -/ noncomputable def of_injective (h : f.ker = ⊥) : M ≃ₗ[R] f.range := of_left_inverse $ classical.some_spec (linear_map.ker_eq_bot.1 h).has_left_inverse @[simp] theorem of_injective_apply {h : f.ker = ⊥} (x : M) : ↑(of_injective f h x) = f x := rfl /-- 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₂ := (of_injective f hf₁).trans (of_top _ hf₂) @[simp] theorem of_bijective_apply {hf₁ hf₂} (x : M) : of_bijective f hf₁ hf₂ x = f x := rfl 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₃] 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 →ₗ[R] M) (((a⁻¹ : units R) : R) • 1 : M →ₗ[R] 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₂ : M₂₁ →ₗ[R] M₂₂).comp $ f.comp e₁.symm, inv_fun := λ f, (e₂.symm : M₂₂ →ₗ[R] M₂₁).comp $ f.comp e₁, left_inv := λ f, by { ext x, simp }, right_inv := λ f, by { ext x, simp }, map_add' := λ f g, by { ext x, simp }, map_smul' := λ c f, by { ext x, simp } } @[simp] lemma arrow_congr_apply {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₂₂) (f : M₁ →ₗ[R] M₂₁) (x : M₂) : arrow_congr e₁ e₂ f x = e₂ (f (e₁.symm x)) := rfl @[simp] lemma arrow_congr_symm_apply {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₂₂) (f : M₂ →ₗ[R] M₂₂) (x : M₁) : (arrow_congr e₁ e₂).symm f x = e₂.symm (f (e₁ x)) := rfl lemma arrow_congr_comp {N N₂ N₃ : Sort*} [add_comm_group N] [add_comm_group N₂] [add_comm_group N₃] [module R N] [module R N₂] [module R N₃] (e₁ : M ≃ₗ[R] N) (e₂ : M₂ ≃ₗ[R] N₂) (e₃ : M₃ ≃ₗ[R] N₃) (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : arrow_congr e₁ e₃ (g.comp f) = (arrow_congr e₂ e₃ g).comp (arrow_congr e₁ e₂ f) := by { ext, simp only [symm_apply_apply, arrow_congr_apply, linear_map.comp_apply], } lemma arrow_congr_trans {M₁ M₂ M₃ N₁ N₂ N₃ : Sort*} [add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂] [add_comm_group M₃] [module R M₃] [add_comm_group N₁] [module R N₁] [add_comm_group N₂] [module R N₂] [add_comm_group N₃] [module R N₃] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : N₁ ≃ₗ[R] N₂) (e₃ : M₂ ≃ₗ[R] M₃) (e₄ : N₂ ≃ₗ[R] N₃) : (arrow_congr e₁ e₂).trans (arrow_congr e₃ e₄) = arrow_congr (e₁.trans e₃) (e₂.trans e₄) := rfl /-- 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₂) ≃ₗ[R] (M →ₗ[R] 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 lemma conj_apply (e : M ≃ₗ[R] M₂) (f : module.End R M) : e.conj f = ((↑e : M →ₗ[R] M₂).comp f).comp e.symm := rfl lemma symm_conj_apply (e : M ≃ₗ[R] M₂) (f : module.End R M₂) : e.symm.conj f = ((↑e.symm : M₂ →ₗ[R] M).comp f).comp e := rfl lemma conj_comp (e : M ≃ₗ[R] M₂) (f g : module.End R M) : e.conj (g.comp f) = (e.conj g).comp (e.conj f) := arrow_congr_comp e e e f g lemma conj_trans (e₁ : M ≃ₗ[R] M₂) (e₂ : M₂ ≃ₗ[R] M₃) : e₁.conj.trans e₂.conj = (e₁.trans e₂).conj := by { ext f x, refl, } @[simp] lemma conj_id (e : M ≃ₗ[R] M₂) : e.conj linear_map.id = linear_map.id := by { ext, simp [conj_apply], } 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₃] variables (K) (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 section noncomputable theory open_locale classical lemma ker_to_span_singleton {x : M} (h : x ≠ 0) : (to_span_singleton K M x).ker = ⊥ := begin ext c, split, { intros hc, rw submodule.mem_bot, rw mem_ker at hc, by_contra hc', have : x = 0, calc x = c⁻¹ • (c • x) : by rw [← mul_smul, inv_mul_cancel hc', one_smul] ... = c⁻¹ • ((to_span_singleton K M x) c) : rfl ... = 0 : by rw [hc, smul_zero], tauto }, { rw [mem_ker, submodule.mem_bot], intros h, rw h, simp } end /-- Given a nonzero element `x` of a vector space `M` over a field `K`, the natural map from `K` to the span of `x`, with invertibility check to consider it as an isomorphism.-/ def to_span_nonzero_singleton (x : M) (h : x ≠ 0) : K ≃ₗ[K] (K ∙ x) := linear_equiv.trans (linear_equiv.of_injective (to_span_singleton K M x) (ker_to_span_singleton K M h)) (of_eq (to_span_singleton K M x).range (K ∙ x) (span_singleton_eq_range K M x).symm) lemma to_span_nonzero_singleton_one (x : M) (h : x ≠ 0) : to_span_nonzero_singleton K M x h 1 = (⟨x, submodule.mem_span_singleton_self x⟩ : K ∙ x) := begin apply set_like.coe_eq_coe.mp, have : ↑(to_span_nonzero_singleton K M x h 1) = to_span_singleton K M x 1 := rfl, rw [this, to_span_singleton_one, submodule.coe_mk], end /-- Given a nonzero element `x` of a vector space `M` over a field `K`, the natural map from the span of `x` to `K`.-/ abbreviation coord (x : M) (h : x ≠ 0) : (K ∙ x) ≃ₗ[K] K := (to_span_nonzero_singleton K M x h).symm lemma coord_self (x : M) (h : x ≠ 0) : (coord K M x h) (⟨x, submodule.mem_span_singleton_self x⟩ : K ∙ x) = 1 := by rw [← to_span_nonzero_singleton_one K M x h, symm_apply_apply] end end field end linear_equiv namespace submodule section module variables [semiring R] [add_comm_monoid M] [module R M] /-- Given `p` a submodule of the module `M` and `q` a submodule of `p`, `p.equiv_subtype_map q` is the natural `linear_equiv` between `q` and `q.map p.subtype`. -/ def equiv_subtype_map (p : submodule R M) (q : submodule R p) : q ≃ₗ[R] q.map p.subtype := { inv_fun := begin rintro ⟨x, hx⟩, refine ⟨⟨x, _⟩, _⟩; rcases hx with ⟨⟨_, h⟩, _, rfl⟩; assumption end, left_inv := λ ⟨⟨_, _⟩, _⟩, rfl, right_inv := λ ⟨x, ⟨_, h⟩, _, rfl⟩, rfl, .. (p.subtype.dom_restrict q).cod_restrict _ begin rintro ⟨x, hx⟩, refine ⟨x, hx, rfl⟩, end } @[simp] lemma equiv_subtype_map_apply {p : submodule R M} {q : submodule R p} (x : q) : (p.equiv_subtype_map q x : M) = p.subtype.dom_restrict q x := rfl @[simp] lemma equiv_subtype_map_symm_apply {p : submodule R M} {q : submodule R p} (x : q.map p.subtype) : ((p.equiv_subtype_map q).symm x : M) = x := by { cases x, refl } /-- If `s ≤ t`, then we can view `s` as a submodule of `t` by taking the comap of `t.subtype`. -/ @[simps] def comap_subtype_equiv_of_le {p q : submodule R M} (hpq : p ≤ q) : comap q.subtype p ≃ₗ[R] p := { to_fun := λ x, ⟨x, x.2⟩, inv_fun := λ x, ⟨⟨x, hpq x.2⟩, x.2⟩, left_inv := λ x, by simp only [coe_mk, set_like.eta, coe_coe], right_inv := λ x, by simp only [subtype.coe_mk, set_like.eta, coe_coe], map_add' := λ x y, rfl, map_smul' := λ c x, rfl } end module variables [ring R] [add_comm_group M] [module R M] variables (p : submodule R M) open linear_map /-- If `p = ⊥`, then `M / p ≃ₗ[R] M`. -/ def quot_equiv_of_eq_bot (hp : p = ⊥) : p.quotient ≃ₗ[R] M := linear_equiv.of_linear (p.liftq id $ hp.symm ▸ bot_le) p.mkq (liftq_mkq _ _ _) $ p.quot_hom_ext $ λ x, rfl @[simp] lemma quot_equiv_of_eq_bot_apply_mk (hp : p = ⊥) (x : M) : p.quot_equiv_of_eq_bot hp (quotient.mk x) = x := rfl @[simp] lemma quot_equiv_of_eq_bot_symm_apply (hp : p = ⊥) (x : M) : (p.quot_equiv_of_eq_bot hp).symm x = quotient.mk x := rfl @[simp] lemma coe_quot_equiv_of_eq_bot_symm (hp : p = ⊥) : ((p.quot_equiv_of_eq_bot hp).symm : M →ₗ[R] p.quotient) = p.mkq := rfl variables (q : submodule R M) /-- Quotienting by equal submodules gives linearly equivalent quotients. -/ def quot_equiv_of_eq (h : p = q) : p.quotient ≃ₗ[R] q.quotient := { map_add' := by { rintros ⟨x⟩ ⟨y⟩, refl }, map_smul' := by { rintros x ⟨y⟩, refl }, ..@quotient.congr _ _ (quotient_rel p) (quotient_rel q) (equiv.refl _) $ λ a b, by { subst h, refl } } @[simp] lemma quot_equiv_of_eq_mk (h : p = q) (x : M) : submodule.quot_equiv_of_eq p q h (submodule.quotient.mk x) = submodule.quotient.mk x := rfl end submodule 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₂) @[simp] lemma mem_map_equiv {e : M ≃ₗ[R] M₂} {x : M₂} : x ∈ p.map (e : M →ₗ[R] M₂) ↔ e.symm x ∈ p := begin rw submodule.mem_map, split, { rintros ⟨y, hy, hx⟩, simp [←hx, hy], }, { intros hx, refine ⟨e.symm x, hx, by simp⟩, }, end lemma map_equiv_eq_comap_symm (e : M ≃ₗ[R] M₂) (K : submodule R M) : K.map (e : M →ₗ[R] M₂) = K.comap e.symm := submodule.ext (λ _, by rw [mem_map_equiv, mem_comap, linear_equiv.coe_coe]) lemma comap_equiv_eq_map_symm (e : M ≃ₗ[R] M₂) (K : submodule R M₂) : K.comap (e : M →ₗ[R] M₂) = K.map e.symm := (map_equiv_eq_comap_symm e.symm K).symm lemma comap_le_comap_smul (f : M →ₗ[R] M₂) (c : R) : comap f q ≤ comap (c • f) q := begin rw set_like.le_def, intros m h, change c • (f m) ∈ q, change f m ∈ q at h, apply q.smul_mem _ 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 set_like.le_def, intros m h, change f₁ m + f₂ m ∈ q, change f₁ m ∈ q ∧ f₂ m ∈ q at h, apply q.add_mem 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_mem' := by { change p ≤ comap 0 q, rw comap_zero, refine le_top, }, add_mem' := λ f₁ f₂ h₁ h₂, by { apply le_trans _ (inf_comap_le_comap_add q f₁ f₂), rw le_inf_iff, exact ⟨h₁, h₂⟩, }, smul_mem' := λ 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, map_add' := λ x y, by { ext, refl, }, map_smul' := λ c f, by { ext, refl, } } end submodule namespace equiv variables [semiring R] [add_comm_monoid M] [module R M] [add_comm_monoid 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₂ := { .. e, .. h.mk' e} end equiv namespace add_equiv variables [semiring R] [add_comm_monoid M] [module R M] [add_comm_monoid M₂] [module R M₂] /-- An additive equivalence whose underlying function preserves `smul` is a linear equivalence. -/ def to_linear_equiv (e : M ≃+ M₂) (h : ∀ (c : R) x, e (c • x) = c • e x) : M ≃ₗ[R] M₂ := { map_smul' := h, .. e, } @[simp] lemma coe_to_linear_equiv (e : M ≃+ M₂) (h : ∀ (c : R) x, e (c • x) = c • e x) : ⇑(e.to_linear_equiv h) = e := rfl @[simp] lemma coe_to_linear_equiv_symm (e : M ≃+ M₂) (h : ∀ (c : R) x, e (c • x) = c • e x) : ⇑(e.to_linear_equiv h).symm = e.symm := rfl end add_equiv namespace linear_map open submodule section isomorphism_laws 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 := (linear_equiv.of_injective (f.ker.liftq f $ le_refl _) $ submodule.ker_liftq_eq_bot _ _ _ (le_refl f.ker)).trans (linear_equiv.of_eq _ _ $ submodule.range_liftq _ _ _) /-- The first isomorphism theorem for surjective linear maps. -/ noncomputable def quot_ker_equiv_of_surjective (f : M →ₗ[R] M₂) (hf : function.surjective f) : f.ker.quotient ≃ₗ[R] M₂ := f.quot_ker_equiv_range.trans (linear_equiv.of_top f.range (linear_map.range_eq_top.2 hf)) @[simp] lemma quot_ker_equiv_range_apply_mk (x : M) : (f.quot_ker_equiv_range (submodule.quotient.mk x) : M₂) = f x := rfl @[simp] lemma quot_ker_equiv_range_symm_apply_image (x : M) (h : f x ∈ f.range) : f.quot_ker_equiv_range.symm ⟨f x, h⟩ = f.ker.mkq x := f.quot_ker_equiv_range.symm_apply_apply (f.ker.mkq x) /-- 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 := linear_equiv.of_bijective (quotient_inf_to_sup_quotient p p') begin rw [quotient_inf_to_sup_quotient, ker_liftq_eq_bot], rw [ker_comp, ker_mkq], exact λ ⟨x, hx1⟩ hx2, ⟨hx1, hx2⟩ end begin rw [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⟩], apply (submodule.quotient.eq _).2, change y - (y + z) ∈ p', rwa [sub_add_eq_sub_sub, sub_self, zero_sub, neg_mem_iff] end @[simp] lemma coe_quotient_inf_to_sup_quotient (p p' : submodule R M) : ⇑(quotient_inf_to_sup_quotient p p') = quotient_inf_equiv_sup_quotient p p' := rfl @[simp] lemma quotient_inf_equiv_sup_quotient_apply_mk (p p' : submodule R M) (x : p) : quotient_inf_equiv_sup_quotient p p' (submodule.quotient.mk x) = submodule.quotient.mk (of_le (le_sup_left : p ≤ p ⊔ p') x) := rfl lemma quotient_inf_equiv_sup_quotient_symm_apply_left (p p' : submodule R M) (x : p ⊔ p') (hx : (x:M) ∈ p) : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = submodule.quotient.mk ⟨x, hx⟩ := (linear_equiv.symm_apply_eq _).2 $ by simp [of_le_apply] @[simp] lemma quotient_inf_equiv_sup_quotient_symm_apply_eq_zero_iff {p p' : submodule R M} {x : p ⊔ p'} : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = 0 ↔ (x:M) ∈ p' := (linear_equiv.symm_apply_eq _).trans $ by simp [of_le_apply] lemma quotient_inf_equiv_sup_quotient_symm_apply_right (p p' : submodule R M) {x : p ⊔ p'} (hx : (x:M) ∈ p') : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = 0 := quotient_inf_equiv_sup_quotient_symm_apply_eq_zero_iff.2 hx end isomorphism_laws end linear_map section fun_left variables (R M) [semiring R] [add_comm_monoid M] [module R M] variables {m n p : Type*} namespace linear_map /-- Given an `R`-module `M` and a function `m → n` between arbitrary types, construct a linear map `(n → M) →ₗ[R] (m → M)` -/ def fun_left (f : m → n) : (n → M) →ₗ[R] (m → M) := { to_fun := (∘ f), map_add' := λ _ _, rfl, map_smul' := λ _ _, rfl } @[simp] theorem fun_left_apply (f : m → n) (g : n → M) (i : m) : fun_left R M f g i = g (f i) := rfl @[simp] theorem fun_left_id (g : n → M) : fun_left R M _root_.id g = g := rfl theorem fun_left_comp (f₁ : n → p) (f₂ : m → n) : fun_left R M (f₁ ∘ f₂) = (fun_left R M f₂).comp (fun_left R M f₁) := rfl theorem fun_left_surjective_of_injective (f : m → n) (hf : injective f) : surjective (fun_left R M f) := begin classical, intro g, refine ⟨λ x, if h : ∃ y, f y = x then g h.some else 0, _⟩, { ext, dsimp only [fun_left_apply], split_ifs with w, { congr, exact hf w.some_spec, }, { simpa only [not_true, exists_apply_eq_apply] using w } }, end theorem fun_left_injective_of_surjective (f : m → n) (hf : surjective f) : injective (fun_left R M f) := begin obtain ⟨g, hg⟩ := hf.has_right_inverse, suffices : left_inverse (fun_left R M g) (fun_left R M f), { exact this.injective }, intro x, simp only [← linear_map.comp_apply, ← fun_left_comp, hg.id, fun_left_id] end end linear_map namespace linear_equiv open linear_map /-- Given an `R`-module `M` and an equivalence `m ≃ n` between arbitrary types, construct a linear equivalence `(n → M) ≃ₗ[R] (m → M)` -/ def fun_congr_left (e : m ≃ n) : (n → M) ≃ₗ[R] (m → M) := linear_equiv.of_linear (fun_left R M e) (fun_left R M e.symm) (linear_map.ext $ λ x, funext $ λ i, by rw [id_apply, ← fun_left_comp, equiv.symm_comp_self, fun_left_id]) (linear_map.ext $ λ x, funext $ λ i, by rw [id_apply, ← fun_left_comp, equiv.self_comp_symm, fun_left_id]) @[simp] theorem fun_congr_left_apply (e : m ≃ n) (x : n → M) : fun_congr_left R M e x = fun_left R M e x := rfl @[simp] theorem fun_congr_left_id : fun_congr_left R M (equiv.refl n) = linear_equiv.refl R (n → M) := rfl @[simp] theorem fun_congr_left_comp (e₁ : m ≃ n) (e₂ : n ≃ p) : fun_congr_left R M (equiv.trans e₁ e₂) = linear_equiv.trans (fun_congr_left R M e₂) (fun_congr_left R M e₁) := rfl @[simp] lemma fun_congr_left_symm (e : m ≃ n) : (fun_congr_left R M e).symm = fun_congr_left R M e.symm := rfl end linear_equiv end fun_left namespace linear_equiv variables [semiring R] [add_comm_monoid M] [module R M] 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} } /-- Restriction from `R`-linear automorphisms of `M` to `R`-linear endomorphisms of `M`, promoted to a monoid hom. -/ def automorphism_group.to_linear_map_monoid_hom : (M ≃ₗ[R] M) →* (M →ₗ[R] M) := { to_fun := coe, map_one' := rfl, map_mul' := λ _ _, rfl } /-- The tautological action by `M ≃ₗ[R] M` on `M`. This generalizes `function.End.apply_mul_action`. -/ instance apply_distrib_mul_action : distrib_mul_action (M ≃ₗ[R] M) M := { smul := ($), smul_zero := linear_equiv.map_zero, smul_add := linear_equiv.map_add, one_smul := λ _, rfl, mul_smul := λ _ _ _, rfl } @[simp] protected lemma smul_def (f : M ≃ₗ[R] M) (a : M) : f • a = f a := rfl /-- `linear_equiv.apply_distrib_mul_action` is faithful. -/ instance apply_has_faithful_scalar : has_faithful_scalar (M ≃ₗ[R] M) M := ⟨λ _ _, linear_equiv.ext⟩ instance apply_smul_comm_class : smul_comm_class R (M ≃ₗ[R] M) M := { smul_comm := λ r e m, (e.map_smul r m).symm } instance apply_smul_comm_class' : smul_comm_class (M ≃ₗ[R] M) R M := { smul_comm := linear_equiv.map_smul } end linear_equiv namespace linear_map variables [semiring R] [add_comm_monoid M] [module R M] variables (R M) /-- 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 namespace submodule variables [ring R] [add_comm_group M] [module R M] instance : is_modular_lattice (submodule R M) := ⟨λ x y z xz a ha, begin rw [mem_inf, mem_sup] at ha, rcases ha with ⟨⟨b, hb, c, hc, rfl⟩, haz⟩, rw mem_sup, refine ⟨b, hb, c, mem_inf.2 ⟨hc, _⟩, rfl⟩, rw [← add_sub_cancel c b, add_comm], apply z.sub_mem haz (xz hb), end⟩ section third_iso_thm variables (S T : submodule R M) (h : S ≤ T) /-- The map from the third isomorphism theorem for modules: `(M / S) / (T / S) → M / T`. -/ def quotient_quotient_equiv_quotient_aux : quotient (T.map S.mkq) →ₗ[R] quotient T := liftq _ (mapq S T linear_map.id h) (by { rintro _ ⟨x, hx, rfl⟩, rw [linear_map.mem_ker, mkq_apply, mapq_apply], exact (quotient.mk_eq_zero _).mpr hx }) @[simp] lemma quotient_quotient_equiv_quotient_aux_mk (x : S.quotient) : quotient_quotient_equiv_quotient_aux S T h (quotient.mk x) = mapq S T linear_map.id h x := liftq_apply _ _ _ @[simp] lemma quotient_quotient_equiv_quotient_aux_mk_mk (x : M) : quotient_quotient_equiv_quotient_aux S T h (quotient.mk (quotient.mk x)) = quotient.mk x := by rw [quotient_quotient_equiv_quotient_aux_mk, mapq_apply, linear_map.id_apply] /-- **Noether's third isomorphism theorem** for modules: `(M / S) / (T / S) ≃ M / T`. -/ def quotient_quotient_equiv_quotient : quotient (T.map S.mkq) ≃ₗ[R] quotient T := { to_fun := quotient_quotient_equiv_quotient_aux S T h, inv_fun := mapq _ _ (mkq S) (le_comap_map _ _), left_inv := λ x, quotient.induction_on' x $ λ x, quotient.induction_on' x $ λ x, by simp, right_inv := λ x, quotient.induction_on' x $ λ x, by simp, .. quotient_quotient_equiv_quotient_aux S T h } end third_iso_thm end submodule
42ac83c3809399c1e5aaf2fcd717401fd1d18c5e
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/trunc_cases.lean
49d91c88526a0a13355c64e8c1aede21d84a60eb
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
4,387
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 tactic.chain import data.quot namespace tactic /-- Auxiliary tactic for `trunc_cases`. -/ private meta def trunc_cases_subsingleton (e : expr) (ids : list name) : tactic expr := do -- When the target is a subsingleton, -- we can just use induction along `trunc.rec_on_subsingleton`, -- generating just a single goal. [(_, [e], _)] ← tactic.induction e ids `trunc.rec_on_subsingleton, return e /-- Auxiliary tactic for `trunc_cases`. -/ private meta def trunc_cases_nondependent (e : expr) (ids : list name) : tactic expr := do -- We may as well just use `trunc.lift_on`. -- (It would be nice if we could use the `induction` tactic with non-dependent recursors, too?) -- (In fact, the general strategy works just as well here, -- except that it leaves a beta redex in the invariance goal.) to_expr ``(trunc.lift_on %%e) >>= tactic.fapply, -- Replace the hypothesis `e` with the unboxed version. tactic.clear e, e ← tactic.intro e.local_pp_name, -- In the invariance goal, introduce the two arguments using the specified identifiers tactic.swap, match ids.nth 1 with | some n := tactic.intro n | none := tactic.intro1 end, match ids.nth 2 with | some n := tactic.intro n | none := tactic.intro1 end, tactic.swap, return e /-- Auxiliary tactic for `trunc_cases`. -/ private meta def trunc_cases_dependent (e : expr) (ids : list name) : tactic expr := do -- If all else fails, just use the general induction principle. [(_, [e], _), (_, [e_a, e_b, e_p], _)] ← tactic.induction e ids, -- However even now we can do something useful: -- the invariance goal has a useless `e_p : true` hypothesis, -- and after casing on that we may be able to simplify away -- the `eq.rec`. swap, (tactic.cases e_p >> `[try { simp only [eq_rec_constant] }]), swap, return e namespace interactive setup_tactic_parser /-- `trunc_cases e` performs case analysis on a `trunc` expression `e`, attempting the following strategies: 1. when the goal is a subsingleton, calling `induction e using trunc.rec_on_subsingleton`, 2. when the goal does not depend on `e`, calling `fapply trunc.lift_on e`, and using `intro` and `clear` afterwards to make the goals look like we used `induction`, 3. otherwise, falling through to `trunc.rec_on`, and in the new invariance goal calling `cases h_p` on the useless `h_p : true` hypothesis, and then attempting to simplify the `eq.rec`. `trunc_cases e with h` names the new hypothesis `h`. If `e` is a local hypothesis already, `trunc_cases` defaults to reusing the same name. `trunc_cases e with h h_a h_b` will use the names `h_a` and `h_b` for the new hypothesis in the invariance goal if `trunc_cases` uses `trunc.lift_on` or `trunc.rec_on`. Finally, if the new hypothesis from inside the `trunc` is a type class, `trunc_cases` resets the instance cache so that it is immediately available. -/ meta def trunc_cases (e : parse texpr) (ids : parse with_ident_list) : tactic unit := do e ← to_expr e, -- If `ids = []` and `e` is a local constant, we'll want to give -- the new unboxed hypothesis the same name. let ids := if ids = [] ∧ e.is_local_constant then [e.local_pp_name] else ids, -- Make a note of the expr `e`, or reuse `e` if it is already a local constant. e ← if e.is_local_constant then return e else (do n ← match ids.nth 0 with | some n := pure n | none := mk_fresh_name end, note n none e), -- Now check if the target is a subsingleton. tgt ← target, ss ← succeeds (mk_app `subsingleton [tgt] >>= mk_instance), -- In each branch here, we're going to capture the name of the new unboxed hypothesis -- so that we can later check if it's a typeclass and if so unfreeze local instances. e ← if ss then trunc_cases_subsingleton e ids else if e.occurs tgt then trunc_cases_dependent e ids else trunc_cases_nondependent e ids, c ← infer_type e >>= is_class, when c reset_instance_cache end interactive end tactic add_tactic_doc { name := "trunc_cases", category := doc_category.tactic, decl_names := [`tactic.interactive.trunc_cases], tags := ["case bashing"] }
3dc8f774dbc7a45e17add48bda3786a9f36675d9
1446f520c1db37e157b631385707cc28a17a595e
/stage0/src/Init/Lean/Elab/Util.lean
97feb5e7e99f9a56e6c232943da94a9c196647b8
[ "Apache-2.0" ]
permissive
bdbabiak/lean4
cab06b8a2606d99a168dd279efdd404edb4e825a
3f4d0d78b2ce3ef541cb643bbe21496bd6b057ac
refs/heads/master
1,615,045,275,530
1,583,793,696,000
1,583,793,696,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,440
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Lean.Util.Trace import Init.Lean.Parser namespace Lean def Syntax.prettyPrint (stx : Syntax) : Format := match stx.truncateTrailing.reprint with -- TODO use syntax pretty printer | some str => format str.toFormat | none => format stx def MacroScopesView.format (view : MacroScopesView) (mainModule : Name) : Format := format $ if view.scopes.isEmpty then view.name else if view.mainModule == mainModule then view.scopes.foldl mkNameNum (view.name ++ view.imported) else view.scopes.foldl mkNameNum (view.name ++ view.imported ++ view.mainModule) namespace Elab structure MacroStackElem := (before : Syntax) (after : Syntax) abbrev MacroStack := List MacroStackElem /- If `ref` does not have position information, then try to use macroStack -/ def getBetterRef (ref : Syntax) (macroStack : MacroStack) : Syntax := match ref.getPos with | some _ => ref | none => match macroStack.find? $ fun (elem : MacroStackElem) => elem.before.getPos != none with | some elem => elem.before | none => ref def addMacroStack (msgData : MessageData) (macroStack : MacroStack) : MessageData := match macroStack with | [] => msgData | stack@(top::_) => let topFmt := top.after.prettyPrint; let msgData := msgData ++ Format.line ++ "with resulting expansion" ++ MessageData.nest 2 (Format.line ++ topFmt); stack.foldl (fun (msgData : MessageData) (elem : MacroStackElem) => let macroFmt := elem.before.prettyPrint; msgData ++ Format.line ++ "while expanding" ++ MessageData.nest 2 (Format.line ++ macroFmt)) msgData def checkSyntaxNodeKind (env : Environment) (k : Name) : ExceptT String Id Name := if Parser.isValidSyntaxNodeKind env k then pure k else throw "failed" def checkSyntaxNodeKindAtNamespaces (env : Environment) (k : Name) : List Name → ExceptT String Id Name | [] => throw "failed" | n::ns => checkSyntaxNodeKind env (n ++ k) <|> checkSyntaxNodeKindAtNamespaces ns def syntaxNodeKindOfAttrParam (env : Environment) (defaultParserNamespace : Name) (arg : Syntax) : ExceptT String Id SyntaxNodeKind := match attrParamSyntaxToIdentifier arg with | some k => checkSyntaxNodeKind env k <|> checkSyntaxNodeKindAtNamespaces env k env.getNamespaces <|> checkSyntaxNodeKind env (defaultParserNamespace ++ k) <|> throw ("invalid syntax node kind '" ++ toString k ++ "'") | none => throw ("syntax node kind is missing") structure ElabAttributeOLeanEntry := (kind : SyntaxNodeKind) (constName : Name) structure ElabAttributeEntry (γ : Type) extends ElabAttributeOLeanEntry := (elabFn : γ) abbrev ElabFnTable (γ : Type) := SMap SyntaxNodeKind (List γ) def ElabFnTable.insert {γ} (table : ElabFnTable γ) (k : SyntaxNodeKind) (f : γ) : ElabFnTable γ := match table.find? k with | some fs => table.insert k (f::fs) | none => table.insert k [f] structure ElabAttributeExtensionState (γ : Type) := (newEntries : List ElabAttributeOLeanEntry := []) (table : ElabFnTable γ := {}) instance ElabAttributeExtensionState.inhabited (γ) : Inhabited (ElabAttributeExtensionState γ) := ⟨{}⟩ abbrev ElabAttributeExtension (γ) := PersistentEnvExtension ElabAttributeOLeanEntry (ElabAttributeEntry γ) (ElabAttributeExtensionState γ) structure ElabAttribute (γ : Type) := (attr : AttributeImpl) (ext : ElabAttributeExtension γ) (kind : String) instance ElabAttribute.inhabited {γ} : Inhabited (ElabAttribute γ) := ⟨{ attr := arbitrary _, ext := arbitrary _, kind := "" }⟩ private def ElabAttribute.mkInitial {γ} (builtinTableRef : IO.Ref (ElabFnTable γ)) : IO (ElabAttributeExtensionState γ) := do table ← builtinTableRef.get; pure { table := table } private def throwUnexpectedElabType {γ} (typeName : Name) (constName : Name) : ExceptT String Id γ := throw ("unexpected elaborator type at '" ++ toString constName ++ "', `" ++ toString typeName ++ "` expected") private unsafe def evalConstantUnsafe (γ) (env : Environment) (typeName : Name) (constName : Name) : ExceptT String Id γ := match env.find? constName with | none => throw ("unknow constant '" ++ toString constName ++ "'") | some info => match info.type with | Expr.const c _ _ => if c != typeName then throwUnexpectedElabType typeName constName else env.evalConst γ constName | _ => throwUnexpectedElabType typeName constName -- We mark `evalConstant` as private because it is only safe if `mkConst typeName` is definitionally equal to `γ`. @[implementedBy evalConstantUnsafe] private constant evalConstant (γ : Type) (env : Environment) (typeName : Name) (constName : Name) : ExceptT String Id γ := throw "" def evalSyntaxConstant (env : Environment) (constName : Name) : ExceptT String Id Syntax := evalConstant Syntax env `Lean.Syntax constName private def ElabAttribute.addImportedParsers {γ} (typeName : Name) (builtinTableRef : IO.Ref (ElabFnTable γ)) (env : Environment) (es : Array (Array ElabAttributeOLeanEntry)) : IO (ElabAttributeExtensionState γ) := do table ← builtinTableRef.get; table ← es.foldlM (fun table entries => entries.foldlM (fun (table : ElabFnTable γ) entry => match evalConstant γ env typeName entry.constName with | Except.ok f => pure $ table.insert entry.kind f | Except.error ex => throw (IO.userError ex)) table) table; pure { table := table } private def ElabAttribute.addExtensionEntry {γ} (s : ElabAttributeExtensionState γ) (e : ElabAttributeEntry γ) : ElabAttributeExtensionState γ := { table := s.table.insert e.kind e.elabFn, newEntries := e.toElabAttributeOLeanEntry :: s.newEntries } private def ElabAttribute.add {γ} (parserNamespace : Name) (typeName : Name) (ext : ElabAttributeExtension γ) (env : Environment) (constName : Name) (arg : Syntax) (persistent : Bool) : IO Environment := do match evalConstant γ env typeName constName with | Except.error ex => throw (IO.userError ex) | Except.ok f => do kind ← IO.ofExcept $ syntaxNodeKindOfAttrParam env parserNamespace arg; pure $ ext.addEntry env { kind := kind, elabFn := f, constName := constName } /- TODO: add support for scoped attributes -/ def mkElabAttributeAux (γ) (attrName : Name) (parserNamespace : Name) (typeName : Name) (descr : String) (kind : String) (builtinTableRef : IO.Ref (ElabFnTable γ)) : IO (ElabAttribute γ) := do ext : ElabAttributeExtension γ ← registerPersistentEnvExtension { name := attrName, mkInitial := ElabAttribute.mkInitial builtinTableRef, addImportedFn := ElabAttribute.addImportedParsers typeName builtinTableRef, addEntryFn := ElabAttribute.addExtensionEntry, exportEntriesFn := fun s => s.newEntries.reverse.toArray, statsFn := fun s => format "number of local entries: " ++ format s.newEntries.length }; let attrImpl : AttributeImpl := { name := attrName, descr := kind ++ " elaborator", add := ElabAttribute.add parserNamespace typeName ext, applicationTime := AttributeApplicationTime.afterCompilation }; registerBuiltinAttribute attrImpl; pure { ext := ext, attr := attrImpl, kind := kind } def mkElabAttribute (γ) (attrName : Name) (parserNamespace : Name) (typeName : Name) (kind : String) (builtinTableRef : IO.Ref (ElabFnTable γ)) : IO (ElabAttribute γ) := mkElabAttributeAux γ attrName parserNamespace typeName (kind ++ " elaborator") kind builtinTableRef abbrev MacroAttribute := ElabAttribute Macro abbrev MacroFnTable := ElabFnTable Macro def mkBuiltinMacroFnTable : IO (IO.Ref MacroFnTable) := IO.mkRef {} @[init mkBuiltinMacroFnTable] constant builtinMacroFnTable : IO.Ref MacroFnTable := arbitrary _ def addBuiltinMacro (k : SyntaxNodeKind) (macro : Macro) : IO Unit := do m ← builtinMacroFnTable.get; builtinMacroFnTable.modify $ fun m => m.insert k macro def declareBuiltinMacro (env : Environment) (kind : SyntaxNodeKind) (declName : Name) : IO Environment := let name := `_regBuiltinMacro ++ declName; let type := mkApp (mkConst `IO) (mkConst `Unit); let val := mkAppN (mkConst `Lean.Elab.addBuiltinMacro) #[toExpr kind, mkConst declName]; let decl := Declaration.defnDecl { name := name, lparams := [], type := type, value := val, hints := ReducibilityHints.opaque, isUnsafe := false }; match env.addAndCompile {} decl with -- TODO: pretty print error | Except.error _ => throw (IO.userError ("failed to emit registration code for builtin macro '" ++ toString declName ++ "'")) | Except.ok env => IO.ofExcept (setInitAttr env name) @[init] def registerBuiltinMacroAttr : IO Unit := registerBuiltinAttribute { name := `builtinMacro, descr := "Builtin macro", add := fun env declName arg persistent => do { unless persistent $ throw (IO.userError ("invalid attribute 'builtinMacro', must be persistent")); kind ← IO.ofExcept $ syntaxNodeKindOfAttrParam env `Lean.Parser.Term arg; match env.find? declName with | none => throw $ IO.userError "unknown declaration" | some decl => match decl.type with | Expr.const `Lean.Macro _ _ => declareBuiltinMacro env kind declName | _ => throw (IO.userError ("unexpected macro type at '" ++ toString declName ++ "' `Macro` expected")) }, applicationTime := AttributeApplicationTime.afterCompilation } def mkMacroAttribute : IO MacroAttribute := mkElabAttributeAux Macro `macro Name.anonymous `Lean.Macro "macros" "macro" builtinMacroFnTable @[init mkMacroAttribute] constant macroAttribute : MacroAttribute := arbitrary _ private def expandMacroFns (stx : Syntax) : List Macro → MacroM Syntax | [] => throw Macro.Exception.unsupportedSyntax | m::ms => m stx <|> expandMacroFns ms def getMacros (env : Environment) : Macro := fun stx => let k := stx.getKind; let table := (macroAttribute.ext.getState env).table; match table.find? k with | some macroFns => expandMacroFns stx macroFns | none => throw Macro.Exception.unsupportedSyntax class MonadMacroAdapter (m : Type → Type) := (getEnv {} : m Environment) (getCurrMacroScope {} : m MacroScope) (getNextMacroScope {} : m MacroScope) (setNextMacroScope {} : MacroScope → m Unit) (throwError {} {α : Type} : Syntax → MessageData → m α) (throwUnsupportedSyntax {} {α : Type} : m α) @[inline] def liftMacroM {α} {m : Type → Type} [Monad m] [MonadMacroAdapter m] (x : MacroM α) : m α := do scp ← MonadMacroAdapter.getCurrMacroScope; env ← MonadMacroAdapter.getEnv; next ← MonadMacroAdapter.getNextMacroScope; match x { currMacroScope := scp, mainModule := env.mainModule } next with | EStateM.Result.error Macro.Exception.unsupportedSyntax _ => MonadMacroAdapter.throwUnsupportedSyntax | EStateM.Result.error (Macro.Exception.error ref msg) _ => MonadMacroAdapter.throwError ref msg | EStateM.Result.ok a nextMacroScope => do MonadMacroAdapter.setNextMacroScope nextMacroScope; pure a @[inline] def adaptMacro {m : Type → Type} [Monad m] [MonadMacroAdapter m] (x : Macro) (stx : Syntax) : m Syntax := liftMacroM (x stx) @[init] private def regTraceClasses : IO Unit := do registerTraceClass `Elab; registerTraceClass `Elab.step end Elab end Lean
ecc0009fb487ba9aa7d9f168a6a9bfea61e1f57d
4f643cce24b2d005aeeb5004c2316a8d6cc7f3b1
/omin/ultra.lean
02fe09ef7e5497d8237aed64af9ee79bfe98700c
[]
no_license
rwbarton/lean-omin
da209ed061d64db65a8f7f71f198064986f30eb9
fd733c6d95ef6f4743aae97de5e15df79877c00e
refs/heads/master
1,674,408,673,325
1,607,343,535,000
1,607,343,535,000
285,150,399
9
0
null
null
null
null
UTF-8
Lean
false
false
3,848
lean
import topology.algebra.ordered import for_mathlib.filter_decides import .tame -- Ultrafilters on the boolean algebra of tame sets. -- Since mathlib doesn't have filters on general Boolean algebras -- we just use ordinary filters and only evaluate them on tame sets. instance preorder.topology.order_topology (α : Type*) [preorder α] : @order_topology α (preorder.topology α) _ := by letI := preorder.topology α; exact ⟨rfl⟩ open o_minimal open filter variables {R : Type*} [DUNLO R] def tame_ultra (F : filter R) : Prop := ∀ ⦃s : set R⦄ (ts : tame s), s ∈ F ∨ sᶜ ∈ F lemma tame_ultra_iff_eventually {F : filter R} : tame_ultra F ↔ ∀ ⦃s : set R⦄ (ts : tame s), (∀ᶠ x in F, x ∈ s) ∨ (∀ᶠ x in F, x ∉ s) := iff.rfl /-- We can check that a filter is `tame_ultra` by testing it on half-infinite intervals. -/ lemma tame_ultra_of_half_infinite {F : filter R} (hio : ∀ (b : R), F.decides (set.Iio b)) (hoi : ∀ (a : R), F.decides (set.Ioi a)) : tame_ultra F := begin unfold tame_ultra, rw tame_eq_gen_half_infinite_interval, apply filter.decides_gen, rintros _ (b|a); apply_assumption end lemma tame_bot : tame_ultra (at_bot : filter R) := begin apply tame_ultra_of_half_infinite, { intro b, left, apply Iio_mem_at_bot }, { intro a, right, rw set.compl_Ioi, apply mem_at_bot } end lemma contains_Iio_or_bdd_below {s : set R} (ts : tame s) : (∃ a, set.Iio a ⊆ s) ∨ bdd_below s := begin cases tame_bot ts with h h; obtain ⟨a, ha⟩ := mem_at_bot_sets.mp h, { -- TODO: for_mathlib: mem_at_bot_sets' using strict inequality exact or.inl ⟨a, λ b hab, ha b (le_of_lt hab)⟩ }, { right, -- TODO: for_mathlib? use a, intros b hb, apply le_of_lt, rw lt_iff_not_ge, intro H, exact ha b H hb } end -- similarly, tame_top local attribute [instance] preorder.topology def above (a : R) : filter R := nhds_within a (set.Ioi a) lemma mem_above_iff {s : set R} (a : R) : s ∈ above a ↔ ∃ b > a, set.Ioo a b ⊆ s := mem_nhds_within_Ioi_iff_exists_Ioo_subset lemma tame_above (a : R) : tame_ultra (above a) := begin apply tame_ultra_of_half_infinite, { intro b, -- TODO: float the cases/push_neg? by_cases h : a < b, { left, -- TODO: for_mathlib: Iio_mem_nhds_within_Ioi have H : a ∈ set.Ico a b := ⟨le_refl a, h⟩, exact mem_sets_of_superset (Ioo_mem_nhds_within_Ioi H) set.Ioo_subset_Iio_self }, { right, rw set.compl_Iio, push_neg at h, unfold above, -- TODO: for_mathlib: Ici_mem_nhds_within_Ioi rw mem_nhds_within, exact ⟨set.univ, is_open_univ, trivial, λ x ⟨_, hx⟩, le_trans h (le_of_lt hx)⟩ } }, { intro b, by_cases h : a < b, { right, rw set.compl_Ioi, -- TODO: for_mathlib: Iic_mem_nhds_within_Ioi refine mem_sets_of_superset _ set.Icc_subset_Iic_self, { exact a }, exact Icc_mem_nhds_within_Ioi ⟨le_refl a, h⟩ }, { left, push_neg at h, unfold above, -- TODO: for_mathlib: Ici_mem_nhds_within_Ioi rw mem_nhds_within, exact ⟨set.univ, is_open_univ, trivial, λ x ⟨_, hx⟩, lt_of_le_of_lt h hx⟩ } } end -- If a tame set doesn't contain its inf `a`, then it must contain some interval above `a`. lemma above_inf {s : set R} (ts : tame s) {a : R} (ha : is_glb s a) (has : a ∉ s) : s ∈ above a := (tame_above a ts).resolve_right $ λ h, begin rw mem_above_iff at h, rcases h with ⟨b, hab, hb⟩, have : b ∈ lower_bounds s, { intros x hx, apply le_of_not_lt, intro hxb, rcases lt_trichotomy a x with hax|rfl|hxa, { exact hb ⟨hax, hxb⟩ hx }, { exact has hx }, { revert hxa, exact not_lt_of_le (ha.1 hx) } }, revert hab, exact not_lt_of_le (ha.2 this) end -- similarly, below
24bf0352d5dd070d57361bd3f3a31f903dacc482
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/hom/units.lean
9e5a0f813322b8a9768a3fbba7d33e6d1f886b15
[ "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
13,521
lean
/- Copyright (c) 2018 Johan Commelin All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Chris Hughes, Kevin Buzzard -/ import algebra.hom.group import algebra.group.units /-! # Monoid homomorphisms and units This file allows to lift monoid homomorphisms to group homomorphisms of their units subgroups. It also contains unrelated results about `units` that depend on `monoid_hom`. ## Main declarations * `units.map`: Turn an homomorphism from `α` to `β` monoids into an homomorphism from `αˣ` to `βˣ`. * `monoid_hom.to_hom_units`: Turn an homomorphism from a group `α` to `β` into an homomorphism from `α` to `βˣ`. ## TODO The results that don't mention homomorphisms should be proved (earlier?) in a different file and be used to golf the basic `group` lemmas. -/ open function universes u v w @[to_additive] lemma group.is_unit {G} [group G] (g : G) : is_unit g := ⟨⟨g, g⁻¹, mul_inv_self g, inv_mul_self g⟩, rfl⟩ section monoid_hom_class /-- If two homomorphisms from a division monoid to a monoid are equal at a unit `x`, then they are equal at `x⁻¹`. -/ @[to_additive "If two homomorphisms from a subtraction monoid to an additive monoid are equal at an additive unit `x`, then they are equal at `-x`."] lemma is_unit.eq_on_inv {F G N} [division_monoid G] [monoid N] [monoid_hom_class F G N] {x : G} (hx : is_unit x) (f g : F) (h : f x = g x) : f x⁻¹ = g x⁻¹ := left_inv_eq_right_inv (map_mul_eq_one f hx.inv_mul_cancel) $ h.symm ▸ map_mul_eq_one g $ hx.mul_inv_cancel /-- If two homomorphism from a group to a monoid are equal at `x`, then they are equal at `x⁻¹`. -/ @[to_additive "If two homomorphism from an additive group to an additive monoid are equal at `x`, then they are equal at `-x`." ] lemma eq_on_inv {F G M} [group G] [monoid M] [monoid_hom_class F G M] (f g : F) {x : G} (h : f x = g x) : f x⁻¹ = g x⁻¹ := (group.is_unit x).eq_on_inv f g h end monoid_hom_class namespace units variables {α : Type*} {M : Type u} {N : Type v} {P : Type w} [monoid M] [monoid N] [monoid P] /-- The group homomorphism on units induced by a `monoid_hom`. -/ @[to_additive "The `add_group` homomorphism on `add_unit`s induced by an `add_monoid_hom`."] def map (f : M →* N) : Mˣ →* Nˣ := monoid_hom.mk' (λ u, ⟨f u.val, f u.inv, by rw [← f.map_mul, u.val_inv, f.map_one], by rw [← f.map_mul, u.inv_val, f.map_one]⟩) (λ x y, ext (f.map_mul x y)) @[simp, to_additive] lemma coe_map (f : M →* N) (x : Mˣ) : ↑(map f x) = f x := rfl @[simp, to_additive] lemma coe_map_inv (f : M →* N) (u : Mˣ) : ↑(map f u)⁻¹ = f ↑u⁻¹ := rfl @[simp, to_additive] lemma map_comp (f : M →* N) (g : N →* P) : map (g.comp f) = (map g).comp (map f) := rfl variables (M) @[simp, to_additive] lemma map_id : map (monoid_hom.id M) = monoid_hom.id Mˣ := by ext; refl /-- Coercion `Mˣ → M` as a monoid homomorphism. -/ @[to_additive "Coercion `add_units M → M` as an add_monoid homomorphism."] def coe_hom : Mˣ →* M := ⟨coe, coe_one, coe_mul⟩ variable {M} @[simp, to_additive] lemma coe_hom_apply (x : Mˣ) : coe_hom M x = ↑x := rfl @[simp, norm_cast, to_additive] lemma coe_pow (u : Mˣ) (n : ℕ) : ((u ^ n : Mˣ) : M) = u ^ n := (units.coe_hom M).map_pow u n section division_monoid variables [division_monoid α] @[simp, norm_cast, to_additive] lemma coe_div : ∀ u₁ u₂ : αˣ, ↑(u₁ / u₂) = (u₁ / u₂ : α) := (units.coe_hom α).map_div @[simp, norm_cast, to_additive] lemma coe_zpow : ∀ (u : αˣ) (n : ℤ), ((u ^ n : αˣ) : α) = u ^ n := (units.coe_hom α).map_zpow @[field_simps] lemma _root_.divp_eq_div (a : α) (u : αˣ) : a /ₚ u = a / u := by rw [div_eq_mul_inv, divp, u.coe_inv] @[simp, to_additive] lemma _root_.map_units_inv {F : Type*} [monoid_hom_class F M α] (f : F) (u : units M) : f ↑u⁻¹ = (f u)⁻¹ := ((f : M →* α).comp (units.coe_hom M)).map_inv u end division_monoid /-- If a map `g : M → Nˣ` agrees with a homomorphism `f : M →* N`, then this map is a monoid homomorphism too. -/ @[to_additive "If a map `g : M → add_units N` agrees with a homomorphism `f : M →+ N`, then this map is an add_monoid homomorphism too."] def lift_right (f : M →* N) (g : M → Nˣ) (h : ∀ x, ↑(g x) = f x) : M →* Nˣ := { to_fun := g, map_one' := units.ext $ (h 1).symm ▸ f.map_one, map_mul' := λ x y, units.ext $ by simp only [h, coe_mul, f.map_mul] } @[simp, to_additive] lemma coe_lift_right {f : M →* N} {g : M → Nˣ} (h : ∀ x, ↑(g x) = f x) (x) : (lift_right f g h x : N) = f x := h x @[simp, to_additive] lemma mul_lift_right_inv {f : M →* N} {g : M → Nˣ} (h : ∀ x, ↑(g x) = f x) (x) : f x * ↑(lift_right f g h x)⁻¹ = 1 := by rw [units.mul_inv_eq_iff_eq_mul, one_mul, coe_lift_right] @[simp, to_additive] lemma lift_right_inv_mul {f : M →* N} {g : M → Nˣ} (h : ∀ x, ↑(g x) = f x) (x) : ↑(lift_right f g h x)⁻¹ * f x = 1 := by rw [units.inv_mul_eq_iff_eq_mul, mul_one, coe_lift_right] end units namespace monoid_hom /-- If `f` is a homomorphism from a group `G` to a monoid `M`, then its image lies in the units of `M`, and `f.to_hom_units` is the corresponding monoid homomorphism from `G` to `Mˣ`. -/ @[to_additive "If `f` is a homomorphism from an additive group `G` to an additive monoid `M`, then its image lies in the `add_units` of `M`, and `f.to_hom_units` is the corresponding homomorphism from `G` to `add_units M`."] def to_hom_units {G M : Type*} [group G] [monoid M] (f : G →* M) : G →* Mˣ := units.lift_right f (λ g, ⟨f g, f g⁻¹, map_mul_eq_one f (mul_inv_self _), map_mul_eq_one f (inv_mul_self _)⟩) (λ g, rfl) @[simp, to_additive] lemma coe_to_hom_units {G M : Type*} [group G] [monoid M] (f : G →* M) (g : G) : (f.to_hom_units g : M) = f g := rfl end monoid_hom namespace is_unit variables {F G α M N : Type*} section monoid variables [monoid M] [monoid N] @[to_additive] lemma map [monoid_hom_class F M N] (f : F) {x : M} (h : is_unit x) : is_unit (f x) := by rcases h with ⟨y, rfl⟩; exact (units.map (f : M →* N) y).is_unit @[to_additive] lemma of_left_inverse [monoid_hom_class F M N] [monoid_hom_class G N M] {f : F} {x : M} (g : G) (hfg : function.left_inverse g f) (h : is_unit (f x)) : is_unit x := by simpa only [hfg x] using h.map g @[to_additive] lemma _root_.is_unit_map_of_left_inverse [monoid_hom_class F M N] [monoid_hom_class G N M] {f : F} {x : M} (g : G) (hfg : function.left_inverse g f) : is_unit (f x) ↔ is_unit x := ⟨of_left_inverse g hfg, map _⟩ /-- If a homomorphism `f : M →* N` sends each element to an `is_unit`, then it can be lifted to `f : M →* Nˣ`. See also `units.lift_right` for a computable version. -/ @[to_additive "If a homomorphism `f : M →+ N` sends each element to an `is_add_unit`, then it can be lifted to `f : M →+ add_units N`. See also `add_units.lift_right` for a computable version."] noncomputable def lift_right (f : M →* N) (hf : ∀ x, is_unit (f x)) : M →* Nˣ := units.lift_right f (λ x, (hf x).unit) $ λ x, rfl @[to_additive] lemma coe_lift_right (f : M →* N) (hf : ∀ x, is_unit (f x)) (x) : (is_unit.lift_right f hf x : N) = f x := rfl @[simp, to_additive] lemma mul_lift_right_inv (f : M →* N) (h : ∀ x, is_unit (f x)) (x) : f x * ↑(is_unit.lift_right f h x)⁻¹ = 1 := units.mul_lift_right_inv (λ y, rfl) x @[simp, to_additive] lemma lift_right_inv_mul (f : M →* N) (h : ∀ x, is_unit (f x)) (x) : ↑(is_unit.lift_right f h x)⁻¹ * f x = 1 := units.lift_right_inv_mul (λ y, rfl) x end monoid section division_monoid variables [division_monoid α] {a b c : α} /-- The element of the group of units, corresponding to an element of a monoid which is a unit. As opposed to `is_unit.unit`, the inverse is computable and comes from the inversion on `α`. This is useful to transfer properties of inversion in `units α` to `α`. See also `to_units`. -/ @[to_additive "The element of the additive group of additive units, corresponding to an element of an additive monoid which is an additive unit. As opposed to `is_add_unit.add_unit`, the negation is computable and comes from the negation on `α`. This is useful to transfer properties of negation in `add_units α` to `α`. See also `to_add_units`.", simps] def unit' (h : is_unit a) : αˣ := ⟨a, a⁻¹, h.mul_inv_cancel, h.inv_mul_cancel⟩ @[simp, to_additive] protected lemma mul_inv_cancel_left (h : is_unit a) : ∀ b, a * (a⁻¹ * b) = b := h.unit'.mul_inv_cancel_left @[simp, to_additive] protected lemma inv_mul_cancel_left (h : is_unit a) : ∀ b, a⁻¹ * (a * b) = b := h.unit'.inv_mul_cancel_left @[simp, to_additive] protected lemma mul_inv_cancel_right (h : is_unit b) (a : α) : a * b * b⁻¹ = a := h.unit'.mul_inv_cancel_right _ @[simp, to_additive] protected lemma inv_mul_cancel_right (h : is_unit b) (a : α) : a * b⁻¹ * b = a := h.unit'.inv_mul_cancel_right _ @[to_additive] protected lemma div_self (h : is_unit a) : a / a = 1 := by rw [div_eq_mul_inv, h.mul_inv_cancel] @[to_additive] protected lemma eq_mul_inv_iff_mul_eq (h : is_unit c) : a = b * c⁻¹ ↔ a * c = b := h.unit'.eq_mul_inv_iff_mul_eq @[to_additive] protected lemma eq_inv_mul_iff_mul_eq (h : is_unit b) : a = b⁻¹ * c ↔ b * a = c := h.unit'.eq_inv_mul_iff_mul_eq @[to_additive] protected lemma inv_mul_eq_iff_eq_mul (h : is_unit a) : a⁻¹ * b = c ↔ b = a * c := h.unit'.inv_mul_eq_iff_eq_mul @[to_additive] protected lemma mul_inv_eq_iff_eq_mul (h : is_unit b) : a * b⁻¹ = c ↔ a = c * b := h.unit'.mul_inv_eq_iff_eq_mul @[to_additive] protected lemma mul_inv_eq_one (h : is_unit b) : a * b⁻¹ = 1 ↔ a = b := @units.mul_inv_eq_one _ _ h.unit' _ @[to_additive] protected lemma inv_mul_eq_one (h : is_unit a) : a⁻¹ * b = 1 ↔ a = b := @units.inv_mul_eq_one _ _ h.unit' _ @[to_additive] protected lemma mul_eq_one_iff_eq_inv (h : is_unit b) : a * b = 1 ↔ a = b⁻¹ := @units.mul_eq_one_iff_eq_inv _ _ h.unit' _ @[to_additive] protected lemma mul_eq_one_iff_inv_eq (h : is_unit a) : a * b = 1 ↔ a⁻¹ = b := @units.mul_eq_one_iff_inv_eq _ _ h.unit' _ @[simp, to_additive] protected lemma div_mul_cancel (h : is_unit b) (a : α) : a / b * b = a := by rw [div_eq_mul_inv, h.inv_mul_cancel_right] @[simp, to_additive] protected lemma mul_div_cancel (h : is_unit b) (a : α) : a * b / b = a := by rw [div_eq_mul_inv, h.mul_inv_cancel_right] @[to_additive] protected lemma mul_one_div_cancel (h : is_unit a) : a * (1 / a) = 1 := by simp [h] @[to_additive] protected lemma one_div_mul_cancel (h : is_unit a) : (1 / a) * a = 1 := by simp [h] @[to_additive] lemma inv : is_unit a → is_unit a⁻¹ := by { rintro ⟨u, rfl⟩, rw ←units.coe_inv, exact units.is_unit _ } @[to_additive] lemma div (ha : is_unit a) (hb : is_unit b) : is_unit (a / b) := by { rw div_eq_mul_inv, exact ha.mul hb.inv } @[to_additive] protected lemma div_left_inj (h : is_unit c) : a / c = b / c ↔ a = b := by { simp_rw div_eq_mul_inv, exact units.mul_left_inj h.inv.unit' } @[to_additive] protected lemma div_eq_iff (h : is_unit b) : a / b = c ↔ a = c * b := by rw [div_eq_mul_inv, h.mul_inv_eq_iff_eq_mul] @[to_additive] protected lemma eq_div_iff (h : is_unit c) : a = b / c ↔ a * c = b := by rw [div_eq_mul_inv, h.eq_mul_inv_iff_mul_eq] @[to_additive] protected lemma div_eq_of_eq_mul (h : is_unit b) : a = c * b → a / b = c := h.div_eq_iff.2 @[to_additive] protected lemma eq_div_of_mul_eq (h : is_unit c) : a * c = b → a = b / c := h.eq_div_iff.2 @[to_additive] protected lemma div_eq_one_iff_eq (h : is_unit b) : a / b = 1 ↔ a = b := ⟨eq_of_div_eq_one, λ hab, hab.symm ▸ h.div_self⟩ @[to_additive] protected lemma div_mul_left (h : is_unit b) : b / (a * b) = 1 / a := by rw [div_eq_mul_inv, mul_inv_rev, h.mul_inv_cancel_left, one_div] @[to_additive] protected lemma mul_div_mul_right (h : is_unit c) (a b : α) : (a * c) / (b * c) = a / b := by simp only [div_eq_mul_inv, mul_inv_rev, mul_assoc, h.mul_inv_cancel_left] @[to_additive] protected lemma mul_mul_div (a : α) (h : is_unit b) : a * b * (1 / b) = a := by simp [h] end division_monoid section division_comm_monoid variables [division_comm_monoid α] {a b c d : α} @[to_additive] protected lemma div_mul_right (h : is_unit a) (b : α) : a / (a * b) = 1 / b := by rw [mul_comm, h.div_mul_left] @[to_additive] protected lemma mul_div_cancel_left (h : is_unit a) (b : α) : a * b / a = b := by rw [mul_comm, h.mul_div_cancel] @[to_additive] protected lemma mul_div_cancel' (h : is_unit a) (b : α) : a * (b / a) = b := by rw [mul_comm, h.div_mul_cancel] @[to_additive] protected lemma mul_div_mul_left (h : is_unit c) (a b : α) : (c * a) / (c * b) = a / b := by rw [mul_comm c, mul_comm c, h.mul_div_mul_right] @[to_additive] protected lemma mul_eq_mul_of_div_eq_div (hb : is_unit b) (hd : is_unit d) (a c : α) (h : a / b = c / d) : a * d = c * b := by rw [←mul_one a, ←hb.div_self, ←mul_comm_div, h, div_mul_eq_mul_div, hd.div_mul_cancel] @[to_additive] protected lemma div_eq_div_iff (hb : is_unit b) (hd : is_unit d) : a / b = c / d ↔ a * d = c * b := by rw [←(hb.mul hd).mul_left_inj, ←mul_assoc, hb.div_mul_cancel, ←mul_assoc, mul_right_comm, hd.div_mul_cancel] @[to_additive] protected lemma div_div_cancel (h : is_unit a) : a / (a / b) = b := by rw [div_div_eq_mul_div, h.mul_div_cancel_left] end division_comm_monoid end is_unit
35d46968eb56a05387b02e6deaf6499eebe293be
c062f1c97fdef9ac746f08754e7d766fd6789aa9
/tools/auto/auto.lean
9c38452f9698cb5c51a563021fb4984863fb7fb1
[]
no_license
emberian/library_dev
00c7a985b21bdebe912f4127a363f2874e1e7555
f3abd7db0238edc18a397540e361a1da2f51503c
refs/heads/master
1,624,153,474,804
1,490,147,180,000
1,490,147,180,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
45,981
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad Automated tableaux reasoners, inspired by the ones in Isabelle. clarify : applies safe rules only, doesn't split goals safe : applies safe rules only, though it may split goals auto : applies safe rules, and then does a backtracking search with unsafe rules. All can be called in classical or intuitionistic mode, and all can use the simplifier as well to simplify the goal and hypotheses. To do: - use attribute manager to keep track of rules - use tactics to build introduction and elimination rules automatically - need version of apply that doesn't instantiate metavariables - bound split depth in safe, backtracking depth in force, and iterations in clarify - rules should match goal w/ or w/out reduction to whnf (reducible) - improve tracing output by keeping track of backtracking depth - provide better error messages when the name of a theorem doesn't exist; for example, replace mk_const with mk_const_with_warning (when installing the rule?) - write a splitter, to help simp - use a backtracking version of reflexivity - do more instantiations of quantifiers - with bintro and belim rules, "num_subgoals" is really "num_branches" - for intuitionistic logic, add a safe elimination rule for A → B, A, and also for A ∨ B → C, and similarly for (∃ x, A x) → C - in fact, for intuitionistic logic, add the rules for Dyckhoff's LJT - add safe rules for quantifiers, e.g. ∀ x, P x |- P a Questions: - In backtracking search, use better selection of rules, e.g. to chose those with fewest subgoals? - Should calls to simplifier use clarify as the prover? - Use more sophisticated handling of quantifiers? - Should we ever call cases, or induction? Maybe with user hints? - Better handling of equality? E.g. use symmetry as an elim rule? - When backtracking, can we intelligently detect that some subgoals can be thrown away? For example, in the intuitionistic elim rule for A → B, we may end up not using B. The tactic that handles that can detect this. - Check and remove duplicate hypotheses? Note: the rules are not complete for intuitionistic propositional logic, which may require using an elim rule on A → B multiple times. -/ open expr tactic list nat universe uₗ declare_trace auto set_option trace.auto false /- logic rules for the tableau prover -/ theorem not_or_of_imp {A B : Prop} (H : A → B) : ¬ A ∨ B := or.elim (classical.em A) (λ H', or.inr (H H')) (λ H', or.inl H') lemma classical_swap {A : Prop} (B : Prop) (H₁ : ¬ A) (H₂ : ¬ B → A) : B := classical.by_contradiction (λ H, H₁ (H₂ H)) theorem imp_classical_elim {A B C : Prop} (H : A → B) (H₁ : ¬ A → C) (H₂ : B → C) : C := or.elim (not_or_of_imp H) (λ H', H₁ H') (λ H', H₂ H') theorem imp_intuit_elim {A B C : Prop} (H : A → B) (H₁ : A) (H₂ : B → C) : C := H₂ (H H₁) theorem or_classical_intro {A B : Prop} (H : ¬ A → B) : A ∨ B := or.elim (classical.em A) (λ H', or.inl H') (λ H', or.inr (H H')) theorem iff_elim {A B C : Prop} (H : A ↔ B) (H' : (A → B) → (B → A) → C) : C := iff.elim H' H theorem exists.intro2 {A : Type} {P : A → Prop} (a₁ a₂ : A) (H : P a₁ ∨ P a₂) : ∃ x, P x := or.elim H (λ H', exists.intro _ H') (λ H', exists.intro _ H') theorem forall_elim {A : Type} {P : A → Prop} {C : Prop} (H : ∀ x, P x) {y : A} (H' : P y → C) : C := H' (H y) theorem forall_elim2 {A : Type} {P : A → Prop} {C : Prop} (H : ∀ x, P x) {y₁ y₂ : A} (H' : P y₁ ∧ P y₂ → C) : C := H' (and.intro (H y₁) (H y₂)) theorem not_true_elim {C : Prop} (H : ¬ true) : C := false.elim (H trivial) theorem not_of_not_or_left {A B : Prop} (H : ¬ (A ∨ B)) : ¬ A := λ H', H (or.inl H') theorem not_of_not_or_right {A B : Prop} (H : ¬ (A ∨ B)) : ¬ B := λ H', H (or.inr H') theorem exists_not_of_not_forall {A : Type} {P : A → Prop} (H : ¬ ∀ x, P x) : ∃ x, ¬ P x := classical.by_contradiction (assume H' : ¬ ∃ x, ¬ P x, H (take x, show P x, from classical.by_contradiction (λ H'', H' (exists.intro x H'')))) theorem not_not_dest {A : Prop} (H : ¬ ¬ A) : A := classical.by_contradiction (λ H', H H') theorem not_not_not_dest {A : Prop} (H : ¬ ¬ ¬ A) : ¬ A := λ H', H (λ H'', H'' H') theorem not_not_of_not_imp {A B : Prop} (H : ¬ (A → B)) : ¬ ¬ A := λ H', H (λ H'', absurd H'' H') theorem of_not_imp {A B : Prop} (H : ¬ (A → B)) : A := not_not_dest (not_not_of_not_imp H) theorem not_of_not_imp {A B : Prop} (H : ¬ (A → B)) : ¬ B := λ H', H (λ H'', H') theorem not_or_not_of_not_and {A B : Prop} (H : ¬ (A ∧ B)) : ¬ A ∨ ¬ B := or.elim (classical.em A) (λ HA, or.inr (λ HB, H (and.intro HA HB))) (λ HnA, or.inl HnA) theorem contrapos {A B : Prop} (H : A → B) : ¬ B → ¬ A := λ H₁ H₂, H₁ (H H₂) theorem not_iff {A B : Prop} (H : ¬ (A ↔ B)) : ¬ ((A → B) ∧ (B → A)) := λ H', H ((iff_iff_implies_and_implies A B)^.mpr H') theorem not_of_imp_false {A : Prop} (H : A → false) : ¬ A := H theorem imp_of_or_imp_left {A B C : Prop} (H : A ∨ B → C) : A → C := λ H', H (or.inl H') theorem imp_of_or_imp_right {A B C : Prop} (H : A ∨ B → C) : B → C := λ H', H (or.inr H') namespace tactic /- utils -/ meta def collect_props : list expr → tactic (list expr) | [] := return [] | (h :: hs) := do props ← collect_props hs, ht ← infer_type h, htt ← infer_type ht, (unify htt ```(Prop) >> return (h :: props)) <|> return props meta def unfold_all (ns : list name) : tactic unit := do dunfold ns, local_context >>= collect_props >>= monad.mapm' (dunfold_at ns) meta def head_symbol : expr → name | (const n a) := n | (app e a) := match (get_app_fn e) with | (const n l) := n | a := `none end | (pi a₁ a₂ a₃ a₄) := `pi | a := `none private meta def whnf_red (e : expr) : tactic expr := whnf e reducible meta def is_forall (e : expr) : tactic bool := if head_symbol e ≠ `pi then return ff else do et ← infer_type e, if et ≠ ```(Prop) then return ff else do dt ← infer_type (binding_domain e), if dt ≠ ```(Prop) then return tt else return ff meta def is_negation (e : expr) : tactic bool := do e' ← whnf_red e, if head_symbol e' = `not then return tt else if is_pi e' = tt then (do b' ← whnf_red (binding_body e'), cond (is_false b') (return tt) (return ff)) else return ff meta def at_least_once (t : tactic unit) : tactic unit := t >> repeat t -- assert_fresh P infers the type T of P, creates a fresh name H, and -- asserts H : T meta def assert_fresh (P : expr) : tactic unit := do n ← mk_fresh_name, t ← infer_type P, assertv n t P meta def expr_with_type_to_string (h : expr) : tactic string := do ht ← infer_type h, pph ← pp h, ppht ← pp ht, return (to_string pph ++ " : " ++ to_string ppht) /- versions of the simplifier that call themselves recursively -/ -- FIXME(gabriel): figure out how to provide custom tactic to simplifier meta def simplify_goal' (t : command) (lemmas : list expr) : command := simp_using lemmas meta def simp_core_at' (t : command) (lemmas : list expr) (h : expr) : command := simp_at h lemmas -- simp_add_prove_max_depth l d uses the simplifier as its own prover, recursing up to depth d meta def simp_add_prove_max_depth (lemmas : list expr) : ℕ → tactic unit | 0 := failed | (succ d) := do l ← local_context >>= collect_props, simplify_goal' (simp_add_prove_max_depth d) (l ++ lemmas), triv meta def strong_simp_add (lemmas : list expr) : tactic unit := do l ← local_context >>= collect_props, simplify_goal' (simp_add_prove_max_depth lemmas 10) (l ++ lemmas), try triv meta def strong_simp : tactic unit := strong_simp_add [] meta def strong_simp_at_add (h : expr) (lemmas : list expr) : tactic unit := do simp_core_at' (simp_add_prove_max_depth lemmas 10) lemmas h meta def strong_simp_at (h : expr) : tactic unit := do strong_simp_at_add h [] -- TODO: how to inline this? private meta def strong_simp_hyps_add_aux (lemmas : list expr) : list expr → tactic unit | [] := skip | (h :: hs) := try (strong_simp_at_add h lemmas) >> strong_simp_hyps_add_aux hs meta def strong_simp_hyps_add (lemmas : list expr) : tactic unit := do l ← local_context, strong_simp_hyps_add_aux lemmas l meta def strong_simp_hyps : tactic unit := strong_simp_hyps_add [] /- These are for tracing. We use a thunk to avoid computing a string when it is not needed. -/ -- show a trace message meta def auto_trace (s : unit → string) : tactic unit := if is_trace_enabled_for `auto = tt then trace (s ()) else skip -- a version where the string is in the tactic monad meta def auto_traceM (s : unit → tactic string) : tactic unit := if is_trace_enabled_for `auto = tt then s () >>= trace else skip -- trace a step, e.g. an application of a rule, and show the result meta def auto_trace_step (tac : tactic unit) (s : unit → string) : tactic unit := if is_trace_enabled_for `auto = tt then do trace (s ()), (tac >> trace ("result:") >> trace_state >> trace "-----") <|> (trace ("failed:") >> trace_state >> trace "-----" >> failed) else tac -- a version where the string is in the tactic monad meta def auto_trace_stepM (tac : tactic unit) (s : unit → tactic string) : tactic unit := if is_trace_enabled_for `auto = tt then do s () >>= trace, (tac >> trace ("result:") >> trace_state >> trace "-----") <|> (trace ("failed:") >> trace_state >> trace "-----" >> failed) else tac -- this can be used to print a message after a tactic if it fails, e.g. a continuation. meta def auto_trace_with_fail_message (tac : tactic unit) (s : unit → string) : tactic unit := if is_trace_enabled_for `auto = tt then do tac <|> (trace (s ()) >> failed) else tac meta def auto_trace_with_fail_messageM (tac : tactic unit) (s : unit → tactic string) : tactic unit := if is_trace_enabled_for `auto = tt then do tac <|> (s () >>= trace >> failed) else tac /- Safe versions of some tactics, i.e. tactics that do not instantiate metavariables and hence can be applied in safe mode. -/ -- we really want: e₁ and e₂ can be unified without instantiating metavariables meta def unify_safe_core (t : transparency) (e₁ e₂ : expr) : tactic unit := cond (has_meta_var e₁ || has_meta_var e₂) failed (unify e₁ e₂ t) meta def unify_safe (e₁ e₂ : expr) : tactic unit := unify_safe_core semireducible e₁ e₂ -- we really want: try to apply e, without instantiation any metavariables in the goal -- maybe we also want the same for fapply? meta def apply_safe_core (t : transparency) (all : bool) (insts : bool) (e : expr) : tactic (list expr) := apply_core e {md := t, approx := tt, all := all, use_instances := insts } meta def apply_safe (e : expr) : tactic (list expr) := apply_core e {md := semireducible, approx := ff, all := tt} /- a safe version of assumption -/ meta def find_same_type_safe (e : expr) (l : list expr) : tactic expr := first $ list.for l (λ h, do ht ← infer_type h, unify_safe e ht >> return h) meta def find_hyp_with_type (e : expr) : tactic expr := local_context >>= find_same_type_safe e meta def assumption_safe : tactic unit := do goal ← target, h ← find_hyp_with_type goal, auto_trace_stepM (exact h) (λ u, do s ← expr_with_type_to_string h, return ("applying assumption " ++ s)) /- a safe version of contradiction -/ private meta def contra_A_not_A_safe : list expr → list expr → tactic unit | [] Hs := failed | (H1 :: Rs) Hs := do t_0 ← infer_type H1, t ← whnf t_0, (do a ← match_not t, H2 ← find_same_type_safe a Hs, tgt ← target, pr ← mk_app `absurd [tgt, H2, H1], auto_trace_stepM (exact pr) (λ u, do s2 ← expr_with_type_to_string H2, s1 ← expr_with_type_to_string H1, return ("using contradiction, " ++ s2 ++ ", " ++ s1))) <|> contra_A_not_A_safe Rs Hs meta def contradiction_safe : tactic unit := do ctx ← local_context, contra_A_not_A_safe ctx ctx /- The structure storing a rule has the following data: key : name := the head symbol that triggers the rule num_subgoals : nat := number of subgoals introduced classical : bool := whether to use in classical mode intuit : bool := whether to use in intuitionistic mode tac : ... := the tactic used to execute the rule Notice that all the work is done by tac, which is arbitrary. Helper functions build suitable tactics in common situations, but users can write more complicated ones. All the other data is used to find the rules quickly and decide when to apply them. Currently, the only thing that varies is the type of the tactic, so this is given as a parameter: intro_rule == tactic unit elim_rule == expr → tactic unit bintro_rule == tactic unit → tactic unit belim_rule == tactic unit → expr → tactic unit Intro rules are keyed on the head symbol of the goal. Elim rules are keyed on the head symbol of a hypothesis, and take that hypothesis as an argument. We actually have a separate rule database for rules where they head symbol is a negation, keyed to the next head symbol. The intro and elim rules should be safe, which is to say, they can be applied without backtracking. In the other rules, the letter "b" is for "backtracking." Those rules take continuations that carry out the rest of the search, so that they can backtrack on failure. Note that many some elimination rules that would otherwise be safe become unsafe when there are metavariables involved. For example, applying (or.elim H) is unsafe if H has metavariables; if those metavariables are not instantiated by the end of the search, then the attempt was unsuccessful, and needs to be retracted. So there are both safe and unsafe versions of the rule for or. -/ structure rule_data (A : Type) := (key : name) (num_subgoals : ℕ) (classical : bool) (intuit : bool) (tac : A) meta def rule_key {A : Type} : rule_data A → name := rule_data.key meta def intro_rule : Type := rule_data (tactic unit) meta def elim_rule : Type := rule_data (expr → tactic unit) meta def bintro_rule : Type := rule_data (tactic unit → tactic unit) meta def belim_rule : Type := rule_data (expr → tactic unit → tactic unit) meta def rule_database (A : Type) : Type := rb_lmap name (rule_data A) meta def intro_rule_database : Type := rb_lmap name intro_rule meta def elim_rule_database : Type := rb_lmap name elim_rule meta def nelim_rule_database : Type := rb_lmap name elim_rule meta def bintro_rule_database : Type := rb_lmap name bintro_rule meta def belim_rule_database : Type := rb_lmap name belim_rule meta def bnelim_rule_database : Type := rb_lmap name belim_rule meta def mk_rule_database (A : Type) : rule_database A := rb_lmap.mk _ _ meta def insert_rule {A : Type} (db : rule_database A) (r : rule_data A) : rule_database A := rb_lmap.insert db (rule_key r) r meta def insert_rule_list {A : Type} (db : rule_database A) : list (rule_data A) → rule_database A | [] := db | (r :: rs) := insert_rule (insert_rule_list rs) r meta def initialize_rule_database {A : Type} (l : list (rule_data A)) : rule_database A := insert_rule_list (mk_rule_database A) l meta def find_rules {A : Type} (db : rule_database A) (key : name) : list (rule_data A) := rb_lmap.find db key /- set up attributes -/ meta def intro_rule_database_of_list_name (ns : list name) : tactic (intro_rule_database) := do env ← get_env, rule_list ← monad.for ns (λ n, do e ← mk_const n, eval_expr intro_rule e), return (initialize_rule_database rule_list) meta def intro_rule_attr : caching_user_attribute (intro_rule_database) := { name := `auto.intro_rule, descr := "intro rule for tableau provers", mk_cache := intro_rule_database_of_list_name, dependencies := [] } run_cmd attribute.register ``intro_rule_attr meta def elim_rule_database_of_list_name (ns : list name) : tactic (elim_rule_database) := do env ← get_env, rule_list ← monad.for ns (λ n, do e ← mk_const n, eval_expr elim_rule e), return (initialize_rule_database rule_list) meta def elim_rule_attr : caching_user_attribute (elim_rule_database) := { name := `auto.elim_rule, descr := "elim rule for tableau provers", mk_cache := elim_rule_database_of_list_name, dependencies := [] } run_cmd attribute.register ``elim_rule_attr meta def nelim_rule_attr : caching_user_attribute (elim_rule_database) := { name := `auto.nelim_rule, descr := "negated elim rule for tableau provers", mk_cache := elim_rule_database_of_list_name, dependencies := [] } run_cmd attribute.register ``nelim_rule_attr meta def bintro_rule_database_of_list_name (ns : list name) : tactic (bintro_rule_database) := do env ← get_env, rule_list ← monad.for ns (λ n, do e ← mk_const n, eval_expr bintro_rule e), return (initialize_rule_database rule_list) meta def bintro_rule_attr : caching_user_attribute (bintro_rule_database) := { name := `auto.bintro_rule, descr := "backtracking intro rule for tableau provers", mk_cache := bintro_rule_database_of_list_name, dependencies := [] } run_cmd attribute.register ``bintro_rule_attr meta def belim_rule_database_of_list_name (ns : list name) : tactic (belim_rule_database) := do env ← get_env, rule_list ← monad.for ns (λ n, do e ← mk_const n, eval_expr belim_rule e), return (initialize_rule_database rule_list) meta def belim_rule_attr : caching_user_attribute (belim_rule_database) := { name := `auto.belim_rule, descr := "backtracking elim rule for tableau provers", mk_cache := belim_rule_database_of_list_name, dependencies := [] } run_cmd attribute.register ``belim_rule_attr meta def bnelim_rule_attr : caching_user_attribute (belim_rule_database) := { name := `auto.bnelim_rule, descr := "backtracking negated elim rule for tableau provers", mk_cache := belim_rule_database_of_list_name, dependencies := [] } run_cmd attribute.register ``bnelim_rule_attr /- intro rules -/ meta def apply_intro_rule (db : intro_rule_database) (max_subgoals : ℕ) (classical : bool) : tactic unit := do goal ← target >>= whnf_red, first $ list.for (find_rules db (head_symbol goal)) (λ r, if rule_data.num_subgoals r ≤ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r else failed) /- procedures for building particular intro rules -/ meta def deploy_intro (op : name) : tactic unit := auto_trace_step (mk_const op >>= apply) (λ u, "applying introduction " ++ to_string op) meta def deploy_intro_then_intros (op : name) : tactic unit := auto_trace_step (mk_const op >>= apply >> intros >> return ()) (λ u, "applying introduction " ++ to_string op) /- elim rules -/ meta def apply_elim_rule_at (edb : elim_rule_database) (nedb : nelim_rule_database) (h : expr) (max_subgoals : ℕ) (classical : bool) : tactic unit := do ht ← infer_type h >>= whnf_red, (first $ list.for (find_rules edb (head_symbol ht)) (λ r, if rule_data.num_subgoals r ≤ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r h else failed)) <|> if head_symbol ht = `not then do unneg ← return (app_arg ht) >>= whnf_red, first $ list.for (find_rules nedb (head_symbol (unneg))) (λ r, if rule_data.num_subgoals r ≤ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r h else failed) else failed meta def apply_elim_rule (edb : elim_rule_database) (nedb : nelim_rule_database) (max_subgoals : ℕ) (classical : bool) : tactic unit := do hs ← local_context >>= collect_props, first $ list.for hs (λ h, apply_elim_rule_at edb nedb h max_subgoals classical) /- procedures for building particular elim rules general elimination rules: All the arguments are assumed to be inferrable from the motive and major premise. The rule is not applied if the hypothesis has metavariables -- backtracking is needed for that. destruct rules: This eliminates a hypothesis by applying a single theorem or a list of theorems in the forward direction. The arguments are assume to be inferrable from the premise. It is safe even if the hypothesis has variables. -/ private meta def elim_instance_mapp_args (motive major : ℕ) (emotive emajor : expr) : list (option expr) := let diff := major - motive in nat.rec_on major [] (λ n l, if n = diff then some emotive :: l else if n = 0 then some emajor :: l else none :: l) meta def deploy_elim_at (op : name) (motive : ℕ) (major : ℕ) : expr → tactic unit := λ h : expr, do auto_trace_stepM (do goal ← target, el ← mk_mapp op (elim_instance_mapp_args motive major goal h), clear h, apply el ; (intros >> skip), return ()) (λ u, do s ← expr_with_type_to_string h, return ("applying elimination " ++ to_string op ++ " at " ++ s)) -- only apply the elim rule if there are no metavars meta def deploy_elim_at_safe (op : name) (motive : ℕ) (major : ℕ) : expr → tactic unit := λ h : expr, do ht ← infer_type h, when (has_meta_var ht = tt) failed, deploy_elim_at op motive major h private meta def dest_instance_mapp_args (prem : ℕ) (hyp : expr) : list (option expr) := nat.rec_on (prem - 1) [some hyp] (λ n l, none :: l) meta def deploy_dest_at (op : name) (prem : ℕ) : expr → tactic unit := λ h : expr, auto_trace_stepM (mk_mapp op (dest_instance_mapp_args prem h) >>= assert_fresh >> clear h) (λ u, do s ← expr_with_type_to_string h, return ("applying destructor " ++ to_string op ++ " at " ++ s)) meta def deploy_dests_at (ops : list (name × ℕ)) : expr → tactic unit := λ h : expr, auto_trace_stepM (monad.for' ops (λ p, mk_mapp (p.1) (dest_instance_mapp_args (p.2) h) >>= assert_fresh) >> clear h) (λ u, do s ← expr_with_type_to_string h, return ("applying destructors " ++ (map prod.fst ops)^.to_string ++ " at " ++ s)) meta def deploy_clear_at : expr → tactic unit := λ h : expr, auto_trace_stepM (clear h) (λ u, do s ← expr_with_type_to_string h, return ("clearing " ++ s)) -- convert (... h : ¬ A ... ==> B) to (... hn : ¬ B ... ==> A), where h' has a fresh name meta def do_classical_swap (h : expr) : tactic expr := do goal ← target, mk_mapp `classical_swap [none, some goal, some h] >>= apply, clear h, mk_fresh_name >>= intro meta def classical_apply_intro_rule_at (db : intro_rule_database) (h : expr) (max_subgoals : ℕ) (classical : bool) : tactic unit := do n ← mk_fresh_name, negated_concl ← do_classical_swap h, apply_intro_rule db max_subgoals classical ; (intros >> do_classical_swap negated_concl >> skip) /- backtracking intro rules -/ meta def apply_bintro_rule (db : bintro_rule_database) (max_subgoals : ℕ) (classical : bool) (cont : tactic unit) : tactic unit := do goal ← target >>= whnf_red, first $ list.for (find_rules db (head_symbol goal)) (λ r, if rule_data.num_subgoals r ≤ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r cont else failed) /- procedure for building particular bintro rules -/ meta def deploy_bintro_choices (l : list (tactic unit)) : tactic unit → tactic unit := take cont, first $ list.for l (λ t, do auto_trace (λ u, "setting backtracking point for intro rule"), t, auto_trace_with_fail_message cont (λ u, "backtracking intro rule")) /- backtracking elim rules -/ meta def classical_apply_bintro_rule_at (db : bintro_rule_database) (h : expr) (max_subgoals : ℕ) (classical : bool) (cont : tactic unit) : tactic unit := do n ← mk_fresh_name, negated_concl ← do_classical_swap h, apply_bintro_rule db max_subgoals classical (intros >> do_classical_swap negated_concl >> cont) meta def apply_belim_rule_at (bedb : belim_rule_database) (bnedb : belim_rule_database) (h : expr) (max_subgoals : ℕ) (classical : bool) (cont : tactic unit) : tactic unit := do ht ← infer_type h >>= whnf_red, (first $ list.for (find_rules bedb (head_symbol ht)) (λ r, if rule_data.num_subgoals r ≤ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r h cont else failed)) <|> (monad.cond (is_negation ht) (do dt ← infer_type (binding_domain h), first $ list.for (find_rules bnedb (head_symbol dt)) (λ r, if rule_data.num_subgoals r ≤ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r h cont else failed)) failed) meta def apply_belim_rule (bedb : belim_rule_database) (bnedb : belim_rule_database) (max_subgoals : ℕ) (classical : bool) (cont : tactic unit) : tactic unit := do hs ← local_context >>= collect_props, first (list.for hs (λ h, apply_belim_rule_at bedb bnedb h max_subgoals classical cont)) /- procedure for building particular belim rules -/ meta def deploy_belim_choices (l : list (expr → tactic unit)) : expr → tactic unit → tactic unit := take h cont, (first $ list.for l (λ t, do auto_traceM (λ u, do s ← expr_with_type_to_string h, return ("setting backtracking point for elim rule at " ++ s)), t h, auto_trace_with_fail_messageM cont (λ u, do s ← expr_with_type_to_string h, return ("backtracking elim rule at " ++ s)))) /- try to do a subst or injection on a hypothesis -/ meta def has_eq_type (h : expr) : tactic bool := do htype ← infer_type h >>= whnf_red, return (match (expr.is_eq htype) with (some _) := tt | none := ff end) meta def try_subst_and_injection_on_hyps : tactic unit := do ctx ← local_context, first $ list.for ctx (λ h, do b ← has_eq_type h, when (b = ff) failed, (do subst h, auto_trace_stepM skip (λ u, do s ← expr_with_type_to_string h, return ("performing subst with " ++ s)), clear h) <|> (do injection h, auto_trace_stepM skip (λ u, do s ← expr_with_type_to_string h, return ("performing injection with " ++ s)), clear h)) /- Standard rule sets -/ /- standard introduction rules -/ @[auto.intro_rule] meta def true_intro_rule : intro_rule := { key := ``true, num_subgoals := 0, classical := tt, intuit := tt, tac := deploy_intro ``true.intro } @[auto.intro_rule] meta def and_intro_rule : intro_rule := { key := ``and, num_subgoals := 2, classical := tt, intuit := tt, tac := deploy_intro ``and.intro } @[auto.intro_rule] meta def or_classical_intro_rule : intro_rule := { key := ``or, num_subgoals := 1, classical := tt, intuit := ff, tac := deploy_intro_then_intros ``or_classical_intro } -- TODO: eliminate trick to get the recursive call private meta def auto_intros_aux : unit → tactic unit | unit.star := do goal ← target >>= whnf_red, when (head_symbol goal = `pi ∨ head_symbol goal = `not) (do n ← mk_fresh_name, intro n, auto_intros_aux unit.star) meta def auto_intros : tactic unit := auto_intros_aux unit.star meta def deploy_intros : tactic unit := auto_trace_step auto_intros (λ u, "applying intros") @[auto.intro_rule] meta def Pi_intro_rule : intro_rule := { key := `pi, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_intros } @[auto.intro_rule] meta def not_intro_rule : intro_rule := { key := ``not, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_intros } @[auto.intro_rule] meta def iff_intro_rule : intro_rule := { key := ``iff, num_subgoals := 2, classical := tt, intuit := tt, tac := deploy_intro ``iff.intro } /- standard backtracking intro rules -/ @[auto.bintro_rule] meta def or_intuit_bintro_rule : bintro_rule := { key := ``or, num_subgoals := 2, classical := ff, intuit := tt, tac := deploy_bintro_choices [deploy_intro ``or.inl, deploy_intro ``or.inr] } @[auto.bintro_rule] meta def exists_bintro_rule : bintro_rule := { key := ``Exists, num_subgoals := 2, classical := tt, intuit := tt, tac := deploy_bintro_choices [deploy_intro ``exists.intro, deploy_intro ``false.elim] } /- standard elimination rules -/ @[auto.elim_rule] meta def and_elim_rule : elim_rule := { key := ``and, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dests_at [(``and.left, 3), (``and.right, 3)] } @[auto.elim_rule] meta def iff_elim_rule : elim_rule := { key := ``iff, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dests_at [(``iff.mp, 3), (``iff.mpr, 3)] } @[auto.elim_rule] meta def or_elim_rule : elim_rule := { key := ``or, num_subgoals := 2, classical := tt, intuit := tt, tac := deploy_elim_at_safe ``or.elim 3 4 } @[auto.elim_rule] meta def false_elim_rule : elim_rule := { key := ``false, num_subgoals := 0, classical := tt, intuit := tt, tac := deploy_elim_at ``false.elim 1 2 } @[auto.elim_rule] meta def exists_elim_rule : elim_rule := { key := ``Exists, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_elim_at_safe ``exists.elim 3 4 } -- given a hypothesis h with type ht, an implication, try to find something -- in the context to apply it to meta def try_modus_ponens_at (h : expr) (ht : expr) : tactic unit := do h' ← find_hyp_with_type (binding_domain ht), auto_trace_stepM (assert_fresh (expr.app h h') >> clear h) (λ u, do s₁ ← expr_with_type_to_string h, s₂ ← expr_with_type_to_string h', return ("applying " ++ s₁ ++ " to " ++ s₂)) -- if h is of the form A → B: -- if B = false, replace by h' : ¬ A -- if h' : A is in the context, apply h to h' -- if A if of the form C ∨ D, replace with C → B, D → B meta def deploy_imp_elim_at (h : expr) : tactic unit := do ht ← infer_type h >>= whnf_red, dt ← infer_type (binding_domain ht), if dt ≠ ```(Prop) then failed else do conc ← return (binding_body ht) >>= whnf_red, if head_symbol conc = ``false then deploy_dest_at ``not_of_imp_false 2 h else try_modus_ponens_at h ht <|> (do hyp ← return (binding_domain ht) >>= whnf_red, if head_symbol hyp = `or then deploy_dests_at [(``imp_of_or_imp_left, 4), (``imp_of_or_imp_right, 4)] h else failed) @[auto.elim_rule] meta def imp_elim_rule : elim_rule := { key := `pi, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_imp_elim_at } meta def deploy_imp_classical_elim_at (h : expr) : tactic unit := do ht ← infer_type h >>= whnf_red, dt ← infer_type (binding_domain ht), if dt ≠ ```(Prop) then failed else monad.cond (is_negation ht) failed (deploy_elim_at ``imp_classical_elim 3 4 h) @[auto.elim_rule] meta def imp_classical_elim_rule : elim_rule := { key := `pi, num_subgoals := 2, classical := tt, intuit := ff, tac := deploy_imp_classical_elim_at } -- try to find a contradiction meta def deploy_not_elim_at (h : expr) : tactic unit := do ht ← infer_type h, h' ← find_hyp_with_type (app_arg ht), goal ← target, t ← mk_app `absurd [goal, h', h], auto_trace_stepM (exact t) (λ u, do s₁ ← expr_with_type_to_string h', s₂ ← expr_with_type_to_string h, return ("using contradiction, " ++ s₁ ++ " and " ++ s₂)) @[auto.elim_rule] meta def not_elim_rule : elim_rule := { key := `not, num_subgoals := 0, classical := tt, intuit := tt, tac := deploy_not_elim_at } /- elimination rules for negated formulas -/ @[auto.nelim_rule] meta def not_true_elim_rule : elim_rule := { key := ``true, num_subgoals := 0, classical := tt, intuit := tt, tac := deploy_elim_at ``not_true_elim 1 2 } @[auto.nelim_rule] meta def not_or_elim_rule : elim_rule := { key := ``or, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dests_at [(``not_of_not_or_left, 3), (``not_of_not_or_right, 3)] } @[auto.nelim_rule] meta def not_and_elim_rule : elim_rule := { key := ``and, num_subgoals := 1, classical := tt, intuit := ff, tac := deploy_dest_at ``not_or_not_of_not_and 3 } @[auto.nelim_rule] meta def not_imp_elim_rule : elim_rule := { key := `pi, num_subgoals := 1, classical := tt, intuit := ff, tac := deploy_dests_at [(``of_not_imp, 3), (``not_of_not_imp, 3)] } @[auto.nelim_rule] meta def not_not_elim_rule : elim_rule := { key := ``not, num_subgoals := 1, classical := tt, intuit := ff, tac := deploy_dest_at ``not_not_dest 2 } @[auto.nelim_rule] meta def not_not_not_elim_rule : elim_rule := { key := ``not, num_subgoals := 1, classical := ff, intuit := tt, tac := deploy_dest_at ``not_not_not_dest 2 } @[auto.nelim_rule] meta def not_iff_elim_rule : elim_rule := { key := ``iff, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dest_at ``not_iff 3 } @[auto.nelim_rule] meta def not_exists_elim_rule : elim_rule := { key := ``Exists, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dest_at ``forall_not_of_not_exists 3 } /- standard backtracking elim rules -/ meta def deploy_imp_intuit_belim_at (h : expr) (cont : tactic unit) : tactic unit := do ht ← infer_type h >>= whnf_red, dt ← infer_type (binding_domain ht), if dt ≠ ```(Prop) then failed else deploy_belim_choices [deploy_elim_at ``imp_intuit_elim 3 4, deploy_clear_at] h cont @[auto.belim_rule] meta def imp_intuit_belim_rule : belim_rule := { key := `pi, num_subgoals := 2, classical := ff, intuit := tt, tac := deploy_imp_intuit_belim_at } --meta def imp_classical_belim_rule := --{ key := `pi, num_subgoals := 2, classical := tt, intuit := ff, -- tac := deploy_belim_choices [deploy_clear_at, deploy_imp_classical_elim_at] } @[auto.belim_rule] meta def or_belim_rule : belim_rule := { key := `or, num_subgoals := 2, classical := ff, intuit := tt, tac := deploy_belim_choices [deploy_clear_at, deploy_elim_at ``or.elim 3 4] } /- standard backtracking negated elim rules -/ -- none yet /- backtracking assumption tactic -/ meta def try_assumptions (cont : tactic unit) := do ctx ← local_context, goal ← target, first $ list.for ctx (λ h, do ht ← infer_type h, unify ht goal, auto_trace_stepM (exact h) (λ u, do s ← expr_with_type_to_string h, return ("try applying assumption " ++ s)), auto_trace_with_fail_messageM cont (λ u, do s ← expr_with_type_to_string h, return ("backtracking assumption " ++ s))) meta def try_contradictions (cont : tactic unit) := do ctx ← local_context, goal ← target, first $ list.for ctx (λ h, do ht ← infer_type h, unneg_ht ← match_not ht, first $ list.for ctx (λ h', do ht' ← infer_type h', unify ht' unneg_ht, t ← mk_app ``absurd [goal, h', h], auto_trace_stepM (exact t) (λ u, do s₁ ← expr_with_type_to_string h', s₂ ← expr_with_type_to_string h, return ("try using contradiction " ++ s₁ ++ ", " ++ s₂)), auto_trace_with_fail_messageM cont (λ u, do s₁ ← expr_with_type_to_string h', s₂ ← expr_with_type_to_string h, return ("backtracking contradiction " ++ s₁ ++ ", " ++ s₂)))) /- instantiating quantifiers in the backtracking search -/ meta def has_forall_type (h : expr) : tactic bool := do ht ← infer_type h, is_forall ht -- TODO: eliminate meta def apply_to_metavars_while_universal_aux : unit → expr → tactic expr | unit.star h := do ht ← infer_type h, if head_symbol ht ≠ `pi then return h else do htt ← infer_type ht, if htt ≠ ```(Prop) then return h else do dt ← infer_type (binding_domain ht), if dt = ```(Prop) then return h else do v ← mk_meta_var (binding_domain ht), apply_to_metavars_while_universal_aux unit.star (expr.app h v) meta def apply_to_metavars_while_universal (h : expr) : tactic expr := apply_to_metavars_while_universal_aux unit.star h meta def try_instantiate_quantifiers (cont : tactic unit) : tactic unit := do hs ← (local_context >>= monad.filter has_forall_type), gt ← target, when (hs = []/- ∧ head_symbol gt ≠ `Exists-/) failed, monad.for' hs (λ h, do h' ← apply_to_metavars_while_universal h, assert_fresh h'), -- when (head_symbol gt = `Exists) split, monad.for' hs clear, monad.whenb (is_trace_enabled_for `auto) (trace "instantiating quantifiers" >> trace_state >> trace "-----"), cont /- Safe automation. These operate on the first goal, and apply only safe rules (the new state is logically equivalent to the original one). They make whatever progress they can, and leave the user with zero or more goals. They take the following arguments: classical : classical or intuitionistic use_simp : whether to use the simplifier simp_lemmas : in the latter case, extra lemmas to use -/ -- perform safe rules that do not split the goal meta def clarify_core (classical : bool) (use_simp : bool) (idb : intro_rule_database) (edb : elim_rule_database) (nedb : nelim_rule_database) (simp_lemmas : list expr) : tactic unit := do repeat (apply_intro_rule idb 1 classical), repeat (apply_elim_rule edb nedb 1 classical), repeat try_subst_and_injection_on_hyps, (now <|> assumption_safe <|> -- contradiction_safe <|> when (use_simp = tt) (do when_tracing `auto (trace "calling simplifier"), try (strong_simp_hyps_add simp_lemmas), try (strong_simp_add simp_lemmas), try (now <|> assumption_safe))) -- perform safe rules -- TODO: fix recursion meta def safe_core (classical : bool) (use_simp : bool) (idb : intro_rule_database) (edb : elim_rule_database) (nedb : nelim_rule_database) (simp_lemmas : list expr) : unit → tactic unit | unit.star := do clarify_core classical use_simp idb edb nedb simp_lemmas, now <|> try ((apply_intro_rule idb 10 classical <|> apply_elim_rule edb nedb 10 classical) ; (safe_core /- classical use_simp idb edb nedb simp_lemmas -/ unit.star)) /- The backtracking tableau prover. The function force_all_core_aux is the heart of the tableau prover. It takes a list of goals, which are assumed to have been processed with the safe rules already and are not visible on the goal stack. It applies safe rules to the goals in the current goal list (if any), and then starts calling backtracking rules. This function is meant to be passed as a continuation to the backtracking tactics, which are called with a single goal. The tactics should operate on the goal, resulting in a new goal list. They should then call the continuation to finish the backtracking search. The function succeeds if all the goals are ultimately proved, and it fails otherwise. -/ -- TODO: fix recursion meta def force_all_core_aux (classical : bool) (use_simp : bool) (idb : intro_rule_database) (edb : elim_rule_database) (nedb : elim_rule_database) (bidb : bintro_rule_database) (bedb : belim_rule_database) (bnedb : belim_rule_database) (simp_lemmas : list expr) (final_check : tactic unit) /- (preprocessed_goals : list expr) -/ : unit → list expr → tactic unit | unit.star := let force_core_rec := force_all_core_aux /- classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas final_check -/ unit.star in let process_goals_with_backtracking : list expr → tactic unit := λ l, match l with | [] := final_check -- if it passes, we have success! | (g :: gs) := do {set_goals [g], try_assumptions (force_core_rec gs) <|> try_contradictions (force_core_rec gs) <|> try_instantiate_quantifiers (force_core_rec gs) <|> apply_bintro_rule bidb 10 classical (force_core_rec gs) <|> apply_belim_rule bedb bnedb 10 classical (force_core_rec gs)} end in λ preprocessed_goals, do n ← num_goals, if n ≠ 0 then do all_goals (safe_core classical use_simp idb edb nedb simp_lemmas unit.star), gs ← get_goals, set_goals [], force_all_core_aux /-classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas final_check-/ unit.star (gs ++ preprocessed_goals) else do process_goals_with_backtracking preprocessed_goals meta def final_check_for_metavariables (g : expr) : tactic unit := do result ← instantiate_mvars g, monad.whenb (has_meta_var result) (when_tracing `auto (trace_state >> trace "result has metavariables:" >> trace result) >> failed) -- the main tableaux prover: acts on one goal, makes sure there are no metavariables at the end meta def force_core (classical : bool) (use_simp : bool) (idb : intro_rule_database) (edb : elim_rule_database) (nedb : elim_rule_database) (bidb : bintro_rule_database) (bedb : belim_rule_database) (bnedb : belim_rule_database) (simp_lemmas : list expr) : tactic unit := do auto_trace_step skip (λ u, ">>> entering force"), gs ← get_goals, match gs with | [] := fail "force failed, there are no goals" | (g :: gs') := do set_goals [g], force_all_core_aux classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas (final_check_for_metavariables g) unit.star [], set_goals gs end /- front ends -/ /- -- TODO: a temporary hack: using trace_option to declare a boolean option declare_trace auto.classical set_option trace.auto.classical true declare_trace auto.use_simp set_option trace.auto.use_simp false -/ -- applies to first goal, never splits it, applies only safe rules, always succeeds meta def clarify (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (simp_lemmas : list expr) : tactic unit := do idb ← caching_user_attribute.get_cache intro_rule_attr, edb ← caching_user_attribute.get_cache elim_rule_attr, nedb ← caching_user_attribute.get_cache nelim_rule_attr, clarify_core classical use_simp idb edb nedb simp_lemmas -- applies to first goal, applies only safe rules, always succeeds meta def safe (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (simp_lemmas : list expr) : tactic unit := do idb ← caching_user_attribute.get_cache intro_rule_attr, edb ← caching_user_attribute.get_cache elim_rule_attr, nedb ← caching_user_attribute.get_cache nelim_rule_attr, safe_core classical use_simp idb edb nedb simp_lemmas unit.star -- applies safe to all goals meta def safe_all (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (simp_lemmas : list expr) : tactic unit := all_goals (safe classical use_simp irules erules nerules simp_lemmas) -- applies to first goal, fails if it does not solve it meta def force (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (birules : list bintro_rule) (berules : list belim_rule) (bnerules : list belim_rule) (simp_lemmas : list expr) : tactic unit := do idb ← caching_user_attribute.get_cache intro_rule_attr, edb ← caching_user_attribute.get_cache elim_rule_attr, nedb ← caching_user_attribute.get_cache nelim_rule_attr, bidb ← caching_user_attribute.get_cache bintro_rule_attr, bedb ← caching_user_attribute.get_cache belim_rule_attr, bnedb ← caching_user_attribute.get_cache bnelim_rule_attr, force_core classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas -- applies to all goals, always succeeds meta def auto (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (birules : list bintro_rule) (berules : list belim_rule) (bnerules : list belim_rule) (simp_lemmas : list expr) : tactic unit := safe_all classical use_simp irules erules nerules simp_lemmas >> all_goals (try (force classical use_simp irules erules nerules birules berules bnerules simp_lemmas)) /- for testing -/ meta def clarify' : tactic unit := clarify tt ff [] [] [] [] meta def safe' : tactic unit := safe tt ff [] [] [] [] meta def ssafe' : tactic unit := safe tt tt [] [] [] [] -- with simplification meta def force' : tactic unit := force tt ff [] [] [] [] [] [] [] meta def sforce' : tactic unit := force tt tt [] [] [] [] [] [] [] meta def auto' : tactic unit := auto tt ff [] [] [] [] [] [] [] meta def sauto' : tactic unit := auto tt tt [] [] [] [] [] [] [] meta def iauto' : tactic unit := auto ff ff [] [] [] [] [] [] [] meta def isauto' : tactic unit := auto ff tt [] [] [] [] [] [] [] end tactic
af9d4fe5561d72f07d03f785d43e4756b22d9fab
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/zmod/quotient.lean
5b69b21063bfdb0efbb8c1df97f982933bbe2dca
[ "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
2,381
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.zmod.basic import group_theory.quotient_group import ring_theory.int.basic /-! # `zmod n` and quotient groups / rings This file relates `zmod n` to the quotient group `quotient_add_group.quotient (add_subgroup.zmultiples n)` and to the quotient ring `(ideal.span {n}).quotient`. ## Main definitions - `zmod.quotient_zmultiples_nat_equiv_zmod` and `zmod.quotient_zmultiples_equiv_zmod`: `zmod n` is the group quotient of `ℤ` by `n ℤ := add_subgroup.zmultiples (n)`, (where `n : ℕ` and `n : ℤ` respectively) - `zmod.quotient_span_nat_equiv_zmod` and `zmod.quotient_span_equiv_zmod`: `zmod n` is the ring quotient of `ℤ` by `n ℤ : ideal.span {n}` (where `n : ℕ` and `n : ℤ` respectively) - `zmod.lift n f` is the map from `zmod n` induced by `f : ℤ →+ A` that maps `n` to `0`. ## Tags zmod, quotient group, quotient ring, ideal quotient -/ open quotient_add_group open zmod variables (n : ℕ) {A R : Type*} [add_group A] [ring R] namespace int /-- `ℤ` modulo multiples of `n : ℕ` is `zmod n`. -/ def quotient_zmultiples_nat_equiv_zmod : quotient (add_subgroup.zmultiples (n : ℤ)) ≃+ zmod n := (equiv_quotient_of_eq (zmod.ker_int_cast_add_hom _)).symm.trans $ quotient_ker_equiv_of_right_inverse (int.cast_add_hom (zmod n)) coe int_cast_zmod_cast /-- `ℤ` modulo multiples of `a : ℤ` is `zmod a.nat_abs`. -/ def quotient_zmultiples_equiv_zmod (a : ℤ) : quotient (add_subgroup.zmultiples a) ≃+ zmod a.nat_abs := (equiv_quotient_of_eq (zmultiples_nat_abs a)).symm.trans (quotient_zmultiples_nat_equiv_zmod a.nat_abs) /-- `ℤ` modulo the ideal generated by `n : ℕ` is `zmod n`. -/ def quotient_span_nat_equiv_zmod : (ideal.span {↑n}).quotient ≃+* zmod n := (ideal.quot_equiv_of_eq (zmod.ker_int_cast_ring_hom _)).symm.trans $ ring_hom.quotient_ker_equiv_of_right_inverse $ show function.right_inverse coe (int.cast_ring_hom (zmod n)), from int_cast_zmod_cast /-- `ℤ` modulo the ideal generated by `a : ℤ` is `zmod a.nat_abs`. -/ def quotient_span_equiv_zmod (a : ℤ) : (ideal.span ({a} : set ℤ)).quotient ≃+* zmod a.nat_abs := (ideal.quot_equiv_of_eq (span_nat_abs a)).symm.trans (quotient_span_nat_equiv_zmod a.nat_abs) end int
d4bc1ae8b4d5ed39e7efc10084d28baf5d1b3bbc
bb31430994044506fa42fd667e2d556327e18dfe
/src/measure_theory/group/fundamental_domain.lean
4d48c958b734da9ae01b4a583fb2ab619f8d9355
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
29,567
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. ## Main declarations * `measure_theory.is_fundamental_domain`: Predicate for a set to be a fundamental domain of the action of a group * `measure_theory.fundamental_frontier`: Fundamental frontier of a set under the action of a group. Elements of `s` that belong to some other translate of `s`. * `measure_theory.fundamental_interior`: Fundamental interior of a set under the action of a group. Elements of `s` that do not belong to any other translate of `s`. -/ 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 : pairwise $ ae_disjoint μ on λ g : G, g +ᵥ 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 : pairwise $ ae_disjoint μ on λ g : G, g • s) variables {G H α β E : Type*} namespace is_fundamental_domain variables [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 := λ a b hab, disjoint.ae_disjoint $ disjoint_left.2 $ λ x hxa hxb, begin rw mem_smul_set_iff_inv_smul_mem at hxa hxb, exact hab (inv_injective $ (h_exists x).unique hxa hxb), end } /-- For `s` to be a fundamental domain, it's enough to check `ae_disjoint (g • s) s` for `g ≠ 1`. -/ @[to_additive "For `s` to be a fundamental domain, it's enough to check `ae_disjoint (g +ᵥ s) s` for `g ≠ 0`."] lemma mk'' (h_meas : null_measurable_set s μ) (h_ae_covers : ∀ᵐ x ∂μ, ∃ g : G, g • x ∈ s) (h_ae_disjoint : ∀ g ≠ (1 : G), ae_disjoint μ (g • s) s) (h_qmp : ∀ (g : G), quasi_measure_preserving ((•) g : α → α) μ μ) : is_fundamental_domain G s μ := { null_measurable_set := h_meas, ae_covers := h_ae_covers, ae_disjoint := pairwise_ae_disjoint_of_ae_disjoint_forall_ne_one h_ae_disjoint h_qmp } /-- 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 μ := have ae_disjoint : pairwise (ae_disjoint μ on (λ (g : G), g • s)) := pairwise_ae_disjoint_of_ae_disjoint_forall_ne_one h_ae_disjoint h_qmp, { null_measurable_set := h_meas, ae_disjoint := ae_disjoint, ae_covers := begin 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, h.ae_disjoint.mono $ λ a b hab, hle hab⟩ @[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 := λ a b hab, begin lift e to G ≃ H using he, have : (e.symm a⁻¹)⁻¹ ≠ (e.symm b⁻¹)⁻¹, by simp [hab], convert (h.ae_disjoint this).preimage hf using 1, simp only [←preimage_smul_inv, preimage_preimage, ←hef _ _, e.apply_symm_apply, inv_inv], 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 pairwise_ae_disjoint_of_ac {ν} (h : is_fundamental_domain G s μ) (hν : ν ≪ μ) : pairwise (λ g₁ g₂ : G, ae_disjoint ν (g₁ • s) (g₂ • s)) := h.ae_disjoint.mono $ λ g₁ g₂ H, hν H @[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 [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 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] 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.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.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 /-! ### Interior/frontier of a fundamental domain -/ section measurable_space variables (G) [group G] [mul_action G α] (s : set α) {x : α} /-- The boundary of a fundamental domain, those points of the domain that also lie in a nontrivial translate. -/ @[to_additive measure_theory.add_fundamental_frontier "The boundary of a fundamental domain, those points of the domain that also lie in a nontrivial translate."] def fundamental_frontier : set α := s ∩ ⋃ (g : G) (hg : g ≠ 1), g • s /-- The interior of a fundamental domain, those points of the domain not lying in any translate. -/ @[to_additive measure_theory.add_fundamental_interior "The interior of a fundamental domain, those points of the domain not lying in any translate."] def fundamental_interior : set α := s \ ⋃ (g : G) (hg : g ≠ 1), g • s variables {G s} @[simp, to_additive measure_theory.mem_add_fundamental_frontier] lemma mem_fundamental_frontier : x ∈ fundamental_frontier G s ↔ x ∈ s ∧ ∃ (g : G) (hg : g ≠ 1), x ∈ g • s := by simp [fundamental_frontier] @[simp, to_additive measure_theory.mem_add_fundamental_interior] lemma mem_fundamental_interior : x ∈ fundamental_interior G s ↔ x ∈ s ∧ ∀ (g : G) (hg : g ≠ 1), x ∉ g • s := by simp [fundamental_interior] @[to_additive measure_theory.add_fundamental_frontier_subset] lemma fundamental_frontier_subset : fundamental_frontier G s ⊆ s := inter_subset_left _ _ @[to_additive measure_theory.add_fundamental_interior_subset] lemma fundamental_interior_subset : fundamental_interior G s ⊆ s := diff_subset _ _ variables (G s) @[to_additive measure_theory.disjoint_add_fundamental_interior_add_fundamental_frontier] lemma disjoint_fundamental_interior_fundamental_frontier : disjoint (fundamental_interior G s) (fundamental_frontier G s) := disjoint_sdiff_self_left.mono_right inf_le_right @[simp, to_additive measure_theory.add_fundamental_interior_union_add_fundamental_frontier] lemma fundamental_interior_union_fundamental_frontier : fundamental_interior G s ∪ fundamental_frontier G s = s := diff_union_inter _ _ @[simp, to_additive measure_theory.add_fundamental_interior_union_add_fundamental_frontier] lemma fundamental_frontier_union_fundamental_interior : fundamental_frontier G s ∪ fundamental_interior G s = s := inter_union_diff _ _ @[simp, to_additive measure_theory.sdiff_add_fundamental_interior] lemma sdiff_fundamental_interior : s \ fundamental_interior G s = fundamental_frontier G s := sdiff_sdiff_right_self @[simp, to_additive measure_theory.sdiff_add_fundamental_frontier] lemma sdiff_fundamental_frontier : s \ fundamental_frontier G s = fundamental_interior G s := diff_self_inter @[simp, to_additive measure_theory.add_fundamental_frontier_vadd] lemma fundamental_frontier_smul [group H] [mul_action H α] [smul_comm_class H G α] (g : H) : fundamental_frontier G (g • s) = g • fundamental_frontier G s := by simp_rw [fundamental_frontier, smul_set_inter, smul_set_Union, smul_comm g] @[simp, to_additive measure_theory.add_fundamental_interior_vadd] lemma fundamental_interior_smul [group H] [mul_action H α] [smul_comm_class H G α] (g : H) : fundamental_interior G (g • s) = g • fundamental_interior G s := by simp_rw [fundamental_interior, smul_set_sdiff, smul_set_Union, smul_comm g] @[to_additive measure_theory.pairwise_disjoint_add_fundamental_interior] lemma pairwise_disjoint_fundamental_interior : pairwise (disjoint on λ g : G, g • fundamental_interior G s) := begin refine λ a b hab, disjoint_left.2 _, rintro _ ⟨x, hx, rfl⟩ ⟨y, hy, hxy⟩, rw mem_fundamental_interior at hx hy, refine hx.2 (a⁻¹ * b) _ _, rwa [ne.def, inv_mul_eq_iff_eq_mul, mul_one, eq_comm], simpa [mul_smul, ←hxy, mem_inv_smul_set_iff] using hy.1, end variables [countable G] [measurable_space G] [measurable_space α] [has_measurable_smul G α] {μ : measure α} [smul_invariant_measure G α μ] @[to_additive measure_theory.null_measurable_set.add_fundamental_frontier] protected lemma null_measurable_set.fundamental_frontier (hs : null_measurable_set s μ) : null_measurable_set (fundamental_frontier G s) μ := hs.inter $ null_measurable_set.Union $ λ g, null_measurable_set.Union $ λ hg, hs.smul _ @[to_additive measure_theory.null_measurable_set.add_fundamental_interior] protected lemma null_measurable_set.fundamental_interior (hs : null_measurable_set s μ) : null_measurable_set (fundamental_interior G s) μ := hs.diff $ null_measurable_set.Union $ λ g, null_measurable_set.Union $ λ hg, hs.smul _ end measurable_space namespace is_fundamental_domain section group variables [countable G] [group G] [mul_action G α] [measurable_space α] {μ : measure α} {s : set α} (hs : is_fundamental_domain G s μ) include hs @[to_additive measure_theory.is_add_fundamental_domain.measure_add_fundamental_frontier] lemma measure_fundamental_frontier : μ (fundamental_frontier G s) = 0 := by simpa only [fundamental_frontier, Union₂_inter, measure_Union_null_iff', one_smul, measure_Union_null_iff, inter_comm s, function.on_fun] using λ g (hg : g ≠ 1), hs.ae_disjoint hg @[to_additive measure_theory.is_add_fundamental_domain.measure_add_fundamental_interior] lemma measure_fundamental_interior : μ (fundamental_interior G s) = μ s := measure_diff_null' hs.measure_fundamental_frontier end group variables [countable G] [group G] [mul_action G α] [measurable_space α] {μ : measure α} {s : set α} (hs : is_fundamental_domain G s μ) [measurable_space G] [has_measurable_smul G α] [smul_invariant_measure G α μ] include hs protected lemma fundamental_interior : is_fundamental_domain G (fundamental_interior G s) μ := { null_measurable_set := hs.null_measurable_set.fundamental_interior _ _, ae_covers := begin simp_rw [ae_iff, not_exists, ←mem_inv_smul_set_iff, set_of_forall, ←compl_set_of, set_of_mem_eq, ←compl_Union], have : (⋃ g : G, g⁻¹ • s) \ (⋃ g : G, g⁻¹ • fundamental_frontier G s) ⊆ ⋃ g : G, g⁻¹ • fundamental_interior G s, { simp_rw [diff_subset_iff, ←Union_union_distrib, ←smul_set_union, fundamental_frontier_union_fundamental_interior] }, refine eq_bot_mono (μ.mono $ compl_subset_compl.2 this) _, simp only [Union_inv_smul, outer_measure.measure_of_eq_coe, coe_to_outer_measure, compl_sdiff, ennreal.bot_eq_zero, himp_eq, sup_eq_union, @Union_smul_eq_set_of_exists _ _ _ _ s], exact measure_union_null (measure_Union_null $ λ _, measure_smul_null hs.measure_fundamental_frontier _) hs.ae_covers, end, ae_disjoint := (pairwise_disjoint_fundamental_interior _ _).mono $ λ _ _, disjoint.ae_disjoint } end is_fundamental_domain end measure_theory
084aa727f937037c0909e35857bc6dc3ee49eabd
453dcd7c0d1ef170b0843a81d7d8caedc9741dce
/tactic/wlog.lean
5be0472f970675dfdfc412637874d61605a874d5
[ "Apache-2.0" ]
permissive
amswerdlow/mathlib
9af77a1f08486d8fa059448ae2d97795bd12ec0c
27f96e30b9c9bf518341705c99d641c38638dfd0
refs/heads/master
1,585,200,953,598
1,534,275,532,000
1,534,275,532,000
144,564,700
0
0
null
1,534,156,197,000
1,534,156,197,000
null
UTF-8
Lean
false
false
9,364
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Without loss of generality tactic. -/ import tactic.basic tactic.interactive data.list.perm open expr tactic lean lean.parser local postfix `?`:9001 := optional local postfix *:9001 := many namespace tactic private meta def update_pp_name : expr → name → expr | (local_const n _ bi d) pp := local_const n pp bi d | e n := e private meta def elim_or : ℕ → expr → tactic (list expr) | 0 h := fail "zero cases" | 1 h := return [h] | (n + 1) h := do [(_, [hl], []), (_, [hr], [])] ← induction h, -- there should be no dependent terms [gl, gr] ← get_goals, set_goals [gr], hsr ← elim_or n hr, gsr ← get_goals, set_goals (gl :: gsr), return (hl :: hsr) private meta def dest_or : expr → tactic (list expr) | e := do `(%%a ∨ %%b) ← whnf e | return [e], lb ← dest_or b, return (a :: lb) private meta def match_perms (pat : pattern) : expr → tactic (list $ list expr) | t := (do m ← match_pattern pat t, guard (m.2.all expr.is_local_constant), return [m.2]) <|> (do `(%%l ∨ %%r) ← whnf t, m ← match_pattern pat l, rs ← match_perms r, return (m.2 :: rs)) private meta def update_type : expr → expr → expr | (local_const n pp bi d) t := local_const n pp bi t | e t := e private meta def intron' : ℕ → tactic (list expr) | 0 := return [] | (i + 1) := do n ← intro1, ls ← intron' i, return (n :: ls) meta def wlog (vars' : list expr) (h_cases fst_case : expr) (perms : list (list expr)) : tactic unit := do guard h_cases.is_local_constant, -- reorder s.t. context is Γ ⬝ vars ⬝ cases ⊢ ∀deps, … nr ← revert_lst (vars' ++ [h_cases]), vars ← intron' vars'.length, h_cases ← intro h_cases.local_pp_name, cases ← infer_type h_cases, h_fst_case ← mk_local_def h_cases.local_pp_name (fst_case.instantiate_locals $ (vars'.zip vars).map $ λ⟨o, n⟩, (o.local_uniq_name, n)), ((), pr) ← solve_aux cases (repeat $ exact h_fst_case <|> left >> skip), t ← target, fixed_vars ← vars.mmap (λv, do t ← infer_type v, return (update_type v t) ), let t' := (instantiate_local h_cases.local_uniq_name pr t).pis (fixed_vars ++ [h_fst_case]), (h, [g]) ← local_proof `this t' (do clear h_cases, vars.mmap clear, intron nr), h₀ :: hs ← elim_or perms.length h_cases, solve1 (do exact (h.mk_app $ vars ++ [h₀])), focus ((hs.zip perms.tail).map $ λ⟨h_case, perm⟩, do let p_v := (vars'.zip vars).map (λ⟨p, v⟩, (p.local_uniq_name, v)), let p := perm.map (λp, p.instantiate_locals p_v), note `this none (h.mk_app $ p ++ [h_case]), clear h, return ()), gs ← get_goals, set_goals (g :: gs) namespace interactive open interactive interactive.types expr private meta def parse_permutations : option (list (list name)) → tactic (list (list expr)) | none := return [] | (some []) := return [] | (some perms@(p₀ :: ps)) := do (guard p₀.nodup <|> fail "No permutation `xs_i` in `using [xs_1, …, xs_n]` should contain the same variable twice."), (guard (perms.all $ λp, p.perm p₀) <|> fail "The permutations `xs_i` in `using [xs_1, …, xs_n]` must be permutations of the same variables."), perms.mmap (λp, p.mmap get_local) /-- Without loss of generality: reduces to one goal under variables permutations. Given a goal of the form `g xs`, a predicate `p` over a set of variables, as well as variable permutations `xs_i`. Then `wlog` produces goals of the form The case goal, i.e. the permutation `xs_i` covers all possible cases: `⊢ p xs_0 ∨ ⋯ ∨ p xs_n` The main goal, i.e. the goal reduced to `xs_0`: `(h : p xs_0) ⊢ g xs_0` The invariant goals, i.e. `g` is invariant under `xs_i`: `(h : p xs_i) (this : g xs_0) ⊢ gs xs_i` Either the permutation is provided, or a proof of the disjunction is provided to compute the permutation. The disjunction need to be in assoc normal form, e.g. `p₀ ∨ (p₁ ∨ p₂)`. In many cases the invariant goals can be solved by AC rewriting using `cc` etc. Example: On a state `(n m : ℕ) ⊢ p n m` the tactic `wlog h : n ≤ m using [n m, m n]` produces the following states: `(n m : ℕ) ⊢ n ≤ m ∨ m ≤ n` `(n m : ℕ) (h : n ≤ m) ⊢ p n m` `(n m : ℕ) (h : m ≤ n) (this : p n m) ⊢ p m n` `wlog` supports different calling conventions. The name `h` is used to give a name to the introduced case hypothesis. If the name is avoided, the default will be `case`. (1) `wlog : p xs0 using [xs0, …, xsn]` Results in the case goal `p xs0 ∨ ⋯ ∨ ps xsn`, the main goal `(case : p xs0) ⊢ g xs0` and the invariance goals `(case : p xsi) (this : g xs0) ⊢ g xsi`. (2) `wlog : p xs0 := r using xs0` The expression `r` is a proof of the shape `p xs0 ∨ ⋯ ∨ p xsi`, it is also used to compute the variable permutations. (3) `wlog := r using xs0` The expression `r` is a proof of the shape `p xs0 ∨ ⋯ ∨ p xsi`, it is also used to compute the variable permutations. This is not as stable as (2), for example `p` cannot be a disjunction. (4) `wlog : R x y using x y` and `wlog : R x y` Produces the case `R x y ∨ R y x`. If `R` is ≤, then the disjunction discharged using linearity. If `using x y` is avoided then `x` and `y` are the last two variables appearing in the expression `R x y`. -/ meta def wlog (h : parse ident?) (pat : parse (tk ":" *> texpr)?) (cases : parse (tk ":=" *> texpr)?) (perms : parse (tk "using" *> (list_of (ident*) <|> (λx, [x]) <$> ident*))?) (discharger : tactic unit := (solve_by_elim <|> tauto <|> using_smt (smt_tactic.intros >> smt_tactic.solve_goals))) : tactic unit := do perms ← parse_permutations perms, (pat, cases_pr, cases_goal, vars, perms) ← (match cases with | some r := do vars::_ ← return perms | fail "At least one set of variables expected, i.e. `using x y` or `using [x y, y x]`.", cases_pr ← to_expr r, cases_pr ← (if cases_pr.is_local_constant then return $ match h with some n := update_pp_name cases_pr n | none := cases_pr end else do note (h.get_or_else `case) none cases_pr), cases ← infer_type cases_pr, (pat, perms') ← match pat with | some pat := do pat ← to_expr pat, let vars' := vars.filter $ λv, v.occurs pat, case_pat ← mk_pattern [] vars' pat [] vars', perms' ← match_perms case_pat cases, return (pat, perms') | none := do (p :: ps) ← dest_or cases, let vars' := vars.filter $ λv, v.occurs p, case_pat ← mk_pattern [] vars' p [] vars', perms' ← (p :: ps).mmap (λp, do m ← match_pattern case_pat p, return m.2), return (p, perms') end, let vars_name := vars.map local_uniq_name, guard (perms'.all $ λp, p.all $ λv, v.is_local_constant ∧ v.local_uniq_name ∈ vars_name) <|> fail "Cases contains variables not declared in `using x y z`", perms ← (if perms.length = 1 then do return (perms'.map $ λp, p ++ vars.filter (λv, p.all (λv', v'.local_uniq_name ≠ v.local_uniq_name))) else do guard (perms.length = perms'.length) <|> fail "The provided permutation list has a different length then the provided cases.", return perms), return (pat, cases_pr, @none expr, vars, perms) | none := do let name_h := h.get_or_else `case, some pat ← return pat | fail "Either specify cases or a pattern with permutations", pat ← to_expr pat, (do (x, y) ← match perms with | [] := do (x :: y :: _) ← return pat.list_local_const, return (x, y) | [[x, y]] := return (x, y) | _ := fail "" end, let cases := mk_or_lst [pat, pat.instantiate_locals [(x.local_uniq_name, y), (y.local_uniq_name, x)]], (do `(%%x ≤ %%y) ← return pat, (cases_pr, []) ← local_proof name_h cases (exact ``(le_total %%x %%y)), return (pat, cases_pr, none, [x, y], [[x, y], [y, x]])) <|> (do (cases_pr, [g]) ← local_proof name_h cases skip, return (pat, cases_pr, some g, [x, y], [[x, y], [y, x]]))) <|> (do guard (perms.length ≥ 2) <|> fail ("To generate cases at least two permutations are required, i.e. `using [x y, y x]`" ++ " or exactly 0 or 2 variables"), (vars :: perms') ← return perms, let names := vars.map local_uniq_name, let cases := mk_or_lst (pat :: perms'.map (λp, pat.instantiate_locals (names.zip p))), (cases_pr, [g]) ← local_proof name_h cases skip, return (pat, cases_pr, some g, vars, perms)) end), let name_fn := (if perms.length = 2 then λi, `invariant else λi, mk_simple_name ("invariant_" ++ to_string (i + 1))), with_enable_tags $ tactic.focus1 $ do t ← get_main_tag, tactic.wlog vars cases_pr pat perms, tactic.focus (set_main_tag (mk_num_name `_case 0 :: `main :: t) :: (list.range (perms.length - 1)).map (λi, do set_main_tag (mk_num_name `_case 0 :: name_fn i :: t), try discharger)), match cases_goal with | some g := do set_tag g (mk_num_name `_case 0 :: `cases :: t), gs ← get_goals, set_goals (g :: gs) | none := skip end end interactive end tactic
3244dcbca8e626672c36aedb9e350597b6f13071
4376c25f060c13471bb89cdb12aeac1d53e53876
/src/espaces-metriques/custom/negative_sets.lean
fb991e560e7415bd42c2a0d28b352abbc96296e8
[ "MIT" ]
permissive
RaitoBezarius/projet-maths-lean
8fa7df563d64c256561ab71893c523fc1424b85c
42356e980e021a20c3468f5ca1639fec01bb934f
refs/heads/master
1,613,002,128,339
1,589,289,282,000
1,589,289,282,000
244,431,534
0
1
MIT
1,584,312,574,000
1,583,169,883,000
TeX
UTF-8
Lean
false
false
2,556
lean
import data.set import algebra.pi_instances import .topology section negative_sets def negative_set (S: set ℝ): set ℝ := { x : ℝ | -x ∈ S} lemma negative_set.negative_set_eq_set (S: set ℝ): negative_set (negative_set S) = S := begin rw negative_set, rw negative_set, simp, end lemma negative_set.mem_iff {S: set ℝ} (x: ℝ): x ∈ S ↔ -x ∈ negative_set S := begin split, intro h, rw negative_set, finish, intro h, rw negative_set at h, finish, end lemma negative_set.nonempty {S: set ℝ}: S.nonempty → (negative_set S).nonempty := begin intro hne, obtain ⟨ x, h ⟩ := hne, use (-x), rw negative_set, finish, end lemma negative_set.bdd_above {S: set ℝ}: bdd_below S → bdd_above (negative_set S) := begin intro bdd, obtain ⟨ M, hm ⟩ := bdd, rw lower_bounds at hm, simp at hm, use (-M), rw negative_set, rw upper_bounds, simp, intros a ha, apply le_neg.2, exact hm ha, end lemma negative_set.bdd_below {S: set ℝ}: bdd_above S → bdd_below (negative_set S) := begin intro bdd, obtain ⟨ M, hm ⟩ := bdd, rw upper_bounds at hm, simp at hm, use (-M), rw negative_set, rw lower_bounds, simp, intros a ha, apply neg_le.1, exact hm ha, end lemma negative_set.bdd_above_iff {S: set ℝ}: bdd_below S ↔ bdd_above (negative_set S) := begin split, exact negative_set.bdd_above, set S' := negative_set S with hs, have Seq := negative_set.negative_set_eq_set S, rw ← Seq, rw ← hs, exact negative_set.bdd_below, end lemma negative_set.bdd_below_iff {S: set ℝ}: bdd_above S ↔ bdd_below (negative_set S) := begin split, exact negative_set.bdd_below, set S' := negative_set S with hs, have Seq := negative_set.negative_set_eq_set S, rw ← Seq, rw ← hs, exact negative_set.bdd_above, end lemma negative_set.adhere_ens {S: set ℝ} {l: ℝ}: adhere_ens S l → adhere_ens (negative_set S) (-l) := begin intro adh, rw adhere_ens at adh, obtain ⟨ x, hx ⟩ := adh, use (-x), split, simp, intro n, exact (negative_set.mem_iff (x n)).1 (hx.1 n), exact neg_converge hx.2, end lemma negative_set.adhere_ens_iff {S: set ℝ} {l: ℝ}: adhere_ens S l ↔ adhere_ens (negative_set S) (-l) := begin split, exact negative_set.adhere_ens, set S' := negative_set S with hs, have Seq := negative_set.negative_set_eq_set S, have leq: l = - -l := by simp, rw ← Seq, rw ← hs, set l' := -l with hl, rw leq, exact negative_set.adhere_ens, end end negative_sets
93ffb90160b479f014ecc0077b4d7800d5f71410
437dc96105f48409c3981d46fb48e57c9ac3a3e4
/src/linear_algebra/finsupp.lean
613e0d77618920c560aca5cc95d83d8d3b12bcbf
[ "Apache-2.0" ]
permissive
dan-c-k/mathlib
08efec79bd7481ee6da9cc44c24a653bff4fbe0d
96efc220f6225bc7a5ed8349900391a33a38cc56
refs/heads/master
1,658,082,847,093
1,589,013,201,000
1,589,013,201,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,027
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl Linear structures on function with finite support `α →₀ M`. -/ import data.monoid_algebra noncomputable theory open set linear_map submodule open_locale classical namespace finsupp variables {α : Type*} {M : Type*} {R : Type*} variables [ring R] [add_comm_group M] [module R M] def lsingle (a : α) : M →ₗ[R] (α →₀ M) := ⟨single a, assume a b, single_add, assume c b, (smul_single _ _ _).symm⟩ def lapply (a : α) : (α →₀ M) →ₗ[R] M := ⟨λg, g a, assume a b, rfl, assume a b, rfl⟩ section lsubtype_domain variables (s : set α) def lsubtype_domain : (α →₀ M) →ₗ[R] (s →₀ M) := ⟨subtype_domain (λx, x ∈ s), assume a b, subtype_domain_add, assume c a, ext $ assume a, rfl⟩ lemma lsubtype_domain_apply (f : α →₀ M) : (lsubtype_domain s : (α →₀ M) →ₗ[R] (s →₀ M)) f = subtype_domain (λx, x ∈ s) f := rfl end lsubtype_domain @[simp] lemma lsingle_apply (a : α) (b : M) : (lsingle a : M →ₗ[R] (α →₀ M)) b = single a b := rfl @[simp] lemma lapply_apply (a : α) (f : α →₀ M) : (lapply a : (α →₀ M) →ₗ[R] M) f = f a := rfl @[simp] lemma ker_lsingle (a : α) : (lsingle a : M →ₗ[R] (α →₀ M)).ker = ⊥ := ker_eq_bot.2 (injective_single a) lemma lsingle_range_le_ker_lapply (s t : set α) (h : disjoint s t) : (⨆a∈s, (lsingle a : M →ₗ[R] (α →₀ M)).range) ≤ (⨅a∈t, ker (lapply a)) := begin refine supr_le (assume a₁, supr_le $ assume h₁, range_le_iff_comap.2 _), simp only [(ker_comp _ _).symm, eq_top_iff, le_def', mem_ker, comap_infi, mem_infi], assume b hb a₂ h₂, have : a₁ ≠ a₂ := assume eq, h ⟨h₁, eq.symm ▸ h₂⟩, exact single_eq_of_ne this end lemma infi_ker_lapply_le_bot : (⨅a, ker (lapply a : (α →₀ M) →ₗ[R] M)) ≤ ⊥ := begin simp only [le_def', mem_infi, mem_ker, mem_bot, lapply_apply], exact assume a h, finsupp.ext h end lemma supr_lsingle_range : (⨆a, (lsingle a : M →ₗ[R] (α →₀ M)).range) = ⊤ := begin refine (eq_top_iff.2 $ le_def'.2 $ assume f _, _), rw [← sum_single f], refine sum_mem _ (assume a ha, submodule.mem_supr_of_mem _ a $ set.mem_image_of_mem _ trivial) end lemma disjoint_lsingle_lsingle (s t : set α) (hs : disjoint s t) : disjoint (⨆a∈s, (lsingle a : M →ₗ[R] (α →₀ M)).range) (⨆a∈t, (lsingle a).range) := begin refine disjoint.mono (lsingle_range_le_ker_lapply _ _ $ disjoint_compl s) (lsingle_range_le_ker_lapply _ _ $ disjoint_compl t) (le_trans (le_infi $ assume i, _) infi_ker_lapply_le_bot), classical, by_cases his : i ∈ s, { by_cases hit : i ∈ t, { exact (hs ⟨his, hit⟩).elim }, exact inf_le_right_of_le (infi_le_of_le i $ infi_le _ hit) }, exact inf_le_left_of_le (infi_le_of_le i $ infi_le _ his) end lemma span_single_image (s : set M) (a : α) : submodule.span R (single a '' s) = (submodule.span R s).map (lsingle a) := by rw ← span_image; refl variables (M R) def supported (s : set α) : submodule R (α →₀ M) := begin refine ⟨ {p | ↑p.support ⊆ s }, _, _, _ ⟩, { simp only [subset_def, finset.mem_coe, set.mem_set_of_eq, mem_support_iff, zero_apply], assume h ha, exact (ha rfl).elim }, { assume p q hp hq, refine subset.trans (subset.trans (finset.coe_subset.2 support_add) _) (union_subset hp hq), rw [finset.coe_union] }, { assume a p hp, refine subset.trans (finset.coe_subset.2 support_smul) hp } end variables {M} lemma mem_supported {s : set α} (p : α →₀ M) : p ∈ (supported M R s) ↔ ↑p.support ⊆ s := iff.rfl lemma mem_supported' {s : set α} (p : α →₀ M) : p ∈ supported M R s ↔ ∀ x ∉ s, p x = 0 := by haveI := classical.dec_pred (λ (x : α), x ∈ s); simp [mem_supported, set.subset_def, not_imp_comm] lemma single_mem_supported {s : set α} {a : α} (b : M) (h : a ∈ s) : single a b ∈ supported M R s := set.subset.trans support_single_subset (set.singleton_subset_iff.2 h) lemma supported_eq_span_single (s : set α) : supported R R s = span R ((λ i, single i 1) '' s) := begin refine (span_eq_of_le _ _ (le_def'.2 $ λ l hl, _)).symm, { rintro _ ⟨_, hp, rfl ⟩ , exact single_mem_supported R 1 hp }, { rw ← l.sum_single, refine sum_mem _ (λ i il, _), convert @smul_mem R (α →₀ R) _ _ _ _ (single i 1) (l i) _, { simp }, apply subset_span, apply set.mem_image_of_mem _ (hl il) } end variables (M R) def restrict_dom (s : set α) : (α →₀ M) →ₗ supported M R s := linear_map.cod_restrict _ { to_fun := filter (∈ s), add := λ l₁ l₂, filter_add, smul := λ a l, filter_smul } (λ l, (mem_supported' _ _).2 $ λ x, filter_apply_neg (∈ s) l) variables {M R} section @[simp] theorem restrict_dom_apply (s : set α) (l : α →₀ M) : ((restrict_dom M R s : (α →₀ M) →ₗ supported M R s) l : α →₀ M) = finsupp.filter (∈ s) l := rfl end theorem restrict_dom_comp_subtype (s : set α) : (restrict_dom M R s).comp (submodule.subtype _) = linear_map.id := begin ext l, apply subtype.coe_ext.2, simp, ext a, by_cases a ∈ s, { simp [h] }, { rw [filter_apply_neg (λ x, x ∈ s) _ h], exact ((mem_supported' R l.1).1 l.2 a h).symm } end theorem range_restrict_dom (s : set α) : (restrict_dom M R s).range = ⊤ := begin have := linear_map.range_comp (submodule.subtype _) (restrict_dom M R s), rw [restrict_dom_comp_subtype, linear_map.range_id] at this, exact eq_top_mono (submodule.map_mono le_top) this.symm end theorem supported_mono {s t : set α} (st : s ⊆ t) : supported M R s ≤ supported M R t := λ l h, set.subset.trans h st @[simp] theorem supported_empty : supported M R (∅ : set α) = ⊥ := eq_bot_iff.2 $ λ l h, (submodule.mem_bot R).2 $ by ext; simp [*, mem_supported'] at * @[simp] theorem supported_univ : supported M R (set.univ : set α) = ⊤ := eq_top_iff.2 $ λ l _, set.subset_univ _ theorem supported_Union {δ : Type*} (s : δ → set α) : supported M R (⋃ i, s i) = ⨆ i, supported M R (s i) := begin refine le_antisymm _ (supr_le $ λ i, supported_mono $ set.subset_Union _ _), haveI := classical.dec_pred (λ x, x ∈ (⋃ i, s i)), suffices : ((submodule.subtype _).comp (restrict_dom M R (⋃ i, s i))).range ≤ ⨆ i, supported M R (s i), { rwa [linear_map.range_comp, range_restrict_dom, map_top, range_subtype] at this }, rw [range_le_iff_comap, eq_top_iff], rintro l ⟨⟩, rw mem_coe, apply finsupp.induction l, {exact zero_mem _}, refine λ x a l hl a0, add_mem _ _, haveI := classical.dec_pred (λ x, ∃ i, x ∈ s i), by_cases (∃ i, x ∈ s i); simp [h], { cases h with i hi, exact le_supr (λ i, supported M R (s i)) i (single_mem_supported R _ hi) }, { rw filter_single_of_neg, { simp }, { exact h } } end theorem supported_union (s t : set α) : supported M R (s ∪ t) = supported M R s ⊔ supported M R t := by erw [set.union_eq_Union, supported_Union, supr_bool_eq]; refl theorem supported_Inter {ι : Type*} (s : ι → set α) : supported M R (⋂ i, s i) = ⨅ i, supported M R (s i) := begin refine le_antisymm (le_infi $ λ i, supported_mono $ set.Inter_subset _ _) _, simp [le_def, infi_coe, set.subset_def], exact λ l, set.subset_Inter end section def supported_equiv_finsupp (s : set α) : (supported M R s) ≃ₗ[R] (s →₀ M) := (restrict_support_equiv s).to_linear_equiv begin show is_linear_map R ((lsubtype_domain s : (α →₀ M) →ₗ[R] (s →₀ M)).comp (submodule.subtype (supported M R s))), exact linear_map.is_linear _ end end def lsum (f : α → R →ₗ[R] M) : (α →₀ R) →ₗ[R] M := ⟨λ d, d.sum (λ i, f i), assume d₁ d₂, by simp [sum_add_index], assume a d, by simp [sum_smul_index, smul_sum, -smul_eq_mul, smul_eq_mul.symm]⟩ @[simp] theorem lsum_apply (f : α → R →ₗ[R] M) (l : α →₀ R) : (finsupp.lsum f : (α →₀ R) →ₗ M) l = l.sum (λ b, f b) := rfl section lmap_domain variables {α' : Type*} {α'' : Type*} (M R) def lmap_domain (f : α → α') : (α →₀ M) →ₗ[R] (α' →₀ M) := ⟨map_domain f, assume a b, map_domain_add, map_domain_smul⟩ @[simp] theorem lmap_domain_apply (f : α → α') (l : α →₀ M) : (lmap_domain M R f : (α →₀ M) →ₗ[R] (α' →₀ M)) l = map_domain f l := rfl @[simp] theorem lmap_domain_id : (lmap_domain M R id : (α →₀ M) →ₗ[R] α →₀ M) = linear_map.id := linear_map.ext $ λ l, map_domain_id theorem lmap_domain_comp (f : α → α') (g : α' → α'') : lmap_domain M R (g ∘ f) = (lmap_domain M R g).comp (lmap_domain M R f) := linear_map.ext $ λ l, map_domain_comp theorem supported_comap_lmap_domain (f : α → α') (s : set α') : supported M R (f ⁻¹' s) ≤ (supported M R s).comap (lmap_domain M R f) := λ l (hl : ↑l.support ⊆ f ⁻¹' s), show ↑(map_domain f l).support ⊆ s, begin rw [← set.image_subset_iff, ← finset.coe_image] at hl, exact set.subset.trans map_domain_support hl end theorem lmap_domain_supported [nonempty α] (f : α → α') (s : set α) : (supported M R s).map (lmap_domain M R f) = supported M R (f '' s) := begin inhabit α, refine le_antisymm (map_le_iff_le_comap.2 $ le_trans (supported_mono $ set.subset_preimage_image _ _) (supported_comap_lmap_domain _ _ _ _)) _, intros l hl, refine ⟨(lmap_domain M R (function.inv_fun_on f s) : (α' →₀ M) →ₗ α →₀ M) l, λ x hx, _, _⟩, { rcases finset.mem_image.1 (map_domain_support hx) with ⟨c, hc, rfl⟩, exact function.inv_fun_on_mem (by simpa using hl hc) }, { rw [← linear_map.comp_apply, ← lmap_domain_comp], refine (map_domain_congr $ λ c hc, _).trans map_domain_id, exact function.inv_fun_on_eq (by simpa using hl hc) } end theorem lmap_domain_disjoint_ker (f : α → α') {s : set α} (H : ∀ a b ∈ s, f a = f b → a = b) : disjoint (supported M R s) (lmap_domain M R f).ker := begin rintro l ⟨h₁, h₂⟩, rw [mem_coe, mem_ker, lmap_domain_apply, map_domain] at h₂, simp, ext x, haveI := classical.dec_pred (λ x, x ∈ s), by_cases xs : x ∈ s, { have : finsupp.sum l (λ a, finsupp.single (f a)) (f x) = 0, {rw h₂, refl}, rw [finsupp.sum_apply, finsupp.sum, finset.sum_eq_single x] at this, { simpa [finsupp.single_apply] }, { intros y hy xy, simp [mt (H _ _ (h₁ hy) xs) xy] }, { simp {contextual := tt} } }, { by_contra h, exact xs (h₁ $ finsupp.mem_support_iff.2 h) } end end lmap_domain section total variables (α) {α' : Type*} (M) {M' : Type*} (R) [add_comm_group M'] [module R M'] (v : α → M) {v' : α' → M'} /-- Interprets (l : α →₀ R) as linear combination of the elements in the family (v : α → M) and evaluates this linear combination. -/ protected def total : (α →₀ R) →ₗ M := finsupp.lsum (λ i, linear_map.id.smul_right (v i)) variables {α M v} theorem total_apply (l : α →₀ R) : finsupp.total α M R v l = l.sum (λ i a, a • v i) := rfl @[simp] theorem total_single (c : R) (a : α) : finsupp.total α M R v (single a c) = c • (v a) := by simp [total_apply, sum_single_index] theorem total_range (h : function.surjective v) : (finsupp.total α M R v).range = ⊤ := begin apply range_eq_top.2, intros x, apply exists.elim (h x), exact λ i hi, ⟨single i 1, by simp [hi]⟩ end lemma range_total : (finsupp.total α M R v).range = span R (range v) := begin ext x, split, { intros hx, rw [linear_map.mem_range] at hx, rcases hx with ⟨l, hl⟩, rw ← hl, rw finsupp.total_apply, unfold finsupp.sum, apply sum_mem (span R (range v)), exact λ i hi, submodule.smul _ _ (subset_span (mem_range_self i)) }, { apply span_le.2, intros x hx, rcases hx with ⟨i, hi⟩, rw [mem_coe, linear_map.mem_range], use finsupp.single i 1, simp [hi] } end theorem lmap_domain_total (f : α → α') (g : M →ₗ[R] M') (h : ∀ i, g (v i) = v' (f i)) : (finsupp.total α' M' R v').comp (lmap_domain R R f) = g.comp (finsupp.total α M R v) := by ext l; simp [total_apply, finsupp.sum_map_domain_index, add_smul, h] theorem total_emb_domain (f : α ↪ α') (l : α →₀ R) : (finsupp.total α' M' R v') (emb_domain f l) = (finsupp.total α M' R (v' ∘ f)) l := by simp [total_apply, finsupp.sum, support_emb_domain, emb_domain_apply] theorem total_map_domain (f : α → α') (hf : function.injective f) (l : α →₀ R) : (finsupp.total α' M' R v') (map_domain f l) = (finsupp.total α M' R (v' ∘ f)) l := begin have : map_domain f l = emb_domain ⟨f, hf⟩ l, { rw emb_domain_eq_map_domain ⟨f, hf⟩, refl }, rw this, apply total_emb_domain R ⟨f, hf⟩ l end theorem span_eq_map_total (s : set α): span R (v '' s) = submodule.map (finsupp.total α M R v) (supported R R s) := begin apply span_eq_of_le, { intros x hx, rw set.mem_image at hx, apply exists.elim hx, intros i hi, exact ⟨_, finsupp.single_mem_supported R 1 hi.1, by simp [hi.2]⟩ }, { refine map_le_iff_le_comap.2 (λ z hz, _), have : ∀i, z i • v i ∈ span R (v '' s), { intro c, haveI := classical.dec_pred (λ x, x ∈ s), by_cases c ∈ s, { exact smul_mem _ _ (subset_span (set.mem_image_of_mem _ h)) }, { simp [(finsupp.mem_supported' R _).1 hz _ h] } }, refine sum_mem _ _, simp [this] } end theorem mem_span_iff_total {s : set α} {x : M} : x ∈ span R (v '' s) ↔ ∃ l ∈ supported R R s, finsupp.total α M R v l = x := by rw span_eq_map_total; simp variables (α) (M) (v) protected def total_on (s : set α) : supported R R s →ₗ[R] span R (v '' s) := linear_map.cod_restrict _ ((finsupp.total _ _ _ v).comp (submodule.subtype (supported R R s))) $ λ ⟨l, hl⟩, (mem_span_iff_total _).2 ⟨l, hl, rfl⟩ variables {α} {M} {v} theorem total_on_range (s : set α) : (finsupp.total_on α M R v s).range = ⊤ := by rw [finsupp.total_on, linear_map.range, linear_map.map_cod_restrict, ← linear_map.range_le_iff_comap, range_subtype, map_top, linear_map.range_comp, range_subtype]; exact le_of_eq (span_eq_map_total _ _) theorem total_comp (f : α' → α) : (finsupp.total α' M R (v ∘ f)) = (finsupp.total α M R v).comp (lmap_domain R R f) := begin ext l, simp [total_apply], rw sum_map_domain_index; simp [add_smul], end lemma total_comap_domain (f : α → α') (l : α' →₀ R) (hf : set.inj_on f (f ⁻¹' l.support.to_set)) : finsupp.total α M R v (finsupp.comap_domain f l hf) = (l.support.preimage hf).sum (λ i, (l (f i)) • (v i)) := by rw finsupp.total_apply; refl end total protected def dom_lcongr {α₁ : Type*} {α₂ : Type*} (e : α₁ ≃ α₂) : (α₁ →₀ M) ≃ₗ[R] (α₂ →₀ M) := (finsupp.dom_congr e).to_linear_equiv begin change is_linear_map R (lmap_domain M R e : (α₁ →₀ M) →ₗ[R] (α₂ →₀ M)), exact linear_map.is_linear _ end noncomputable def congr {α' : Type*} (s : set α) (t : set α') (e : s ≃ t) : supported M R s ≃ₗ[R] supported M R t := begin haveI := classical.dec_pred (λ x, x ∈ s), haveI := classical.dec_pred (λ x, x ∈ t), refine linear_equiv.trans (finsupp.supported_equiv_finsupp s) (linear_equiv.trans _ (finsupp.supported_equiv_finsupp t).symm), exact finsupp.dom_lcongr e end end finsupp variables {R : Type*} {M : Type*} {N : Type*} variables [ring R] [add_comm_group M] [module R M] [add_comm_group N] [module R N] lemma linear_map.map_finsupp_total (f : M →ₗ[R] N) {ι : Type*} {g : ι → M} (l : ι →₀ R) : f (finsupp.total ι M R g l) = finsupp.total ι N R (f ∘ g) l := by simp only [finsupp.total_apply, finsupp.total_apply, finsupp.sum, f.map_sum, f.map_smul] lemma submodule.exists_finset_of_mem_supr {ι : Sort*} (p : ι → submodule R M) {m : M} (hm : m ∈ ⨆ i, p i) : ∃ s : finset ι, m ∈ ⨆ i ∈ s, p i := begin obtain ⟨f, hf, rfl⟩ : ∃ f ∈ finsupp.supported R R (⋃ i, ↑(p i)), finsupp.total M M R id f = m, { have aux : (id : M → M) '' (⋃ (i : ι), ↑(p i)) = (⋃ (i : ι), ↑(p i)) := set.image_id _, rwa [supr_eq_span, ← aux, finsupp.mem_span_iff_total R] at hm }, let t : finset M := f.support, have ht : ∀ x : {x // x ∈ t}, ∃ i, ↑x ∈ p i, { intros x, rw finsupp.mem_supported at hf, specialize hf x.2, rwa set.mem_Union at hf }, choose g hg using ht, let s : finset ι := finset.univ.image g, use s, simp only [mem_supr, supr_le_iff], assume N hN, rw [finsupp.total_apply, finsupp.sum, ← submodule.mem_coe], apply is_add_submonoid.finset_sum_mem, assume x hx, apply submodule.smul_mem, let i : ι := g ⟨x, hx⟩, have hi : i ∈ s, { rw finset.mem_image, exact ⟨⟨x, hx⟩, finset.mem_univ _, rfl⟩ }, exact hN i hi (hg _), end
2fa70456185258512e88da2724f85d417ad19259
d436468d80b739ba7e06843c4d0d2070e43448e5
/src/category_theory/equivalence.lean
3abc5da8bc4a27d300974b214e0aa584d1186c9c
[ "Apache-2.0" ]
permissive
roro47/mathlib
761fdc002aef92f77818f3fef06bf6ec6fc1a28e
80aa7d52537571a2ca62a3fdf71c9533a09422cf
refs/heads/master
1,599,656,410,625
1,573,649,488,000
1,573,649,488,000
221,452,951
0
0
Apache-2.0
1,573,647,693,000
1,573,647,692,000
null
UTF-8
Lean
false
false
14,238
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn -/ import category_theory.fully_faithful import category_theory.whiskering import category_theory.natural_isomorphism import tactic.slice import tactic.converter.interactive namespace category_theory open category_theory.functor nat_iso category universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation /-- We define an equivalence as a (half)-adjoint equivalence, a pair of functors with a unit and counit which are natural isomorphisms and the triangle law `Fη ≫ εF = 1`, or in other words the composite `F ⟶ FGF ⟶ F` is the identity. The triangle equation is written as a family of equalities between morphisms, it is more complicated if we write it as an equality of natural transformations, because then we would have to insert natural transformations like `F ⟶ F1`. -/ structure equivalence (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D] := mk' :: (functor : C ⥤ D) (inverse : D ⥤ C) (unit_iso : 𝟭 C ≅ functor ⋙ inverse) (counit_iso : inverse ⋙ functor ≅ 𝟭 D) (functor_unit_iso_comp' : ∀(X : C), functor.map ((unit_iso.hom : 𝟭 C ⟶ functor ⋙ inverse).app X) ≫ counit_iso.hom.app (functor.obj X) = 𝟙 (functor.obj X) . obviously) restate_axiom equivalence.functor_unit_iso_comp' infixr ` ≌ `:10 := equivalence variables {C : Type u₁} [𝒞 : category.{v₁} C] {D : Type u₂} [𝒟 : category.{v₂} D] include 𝒞 𝒟 namespace equivalence @[simp] def unit (e : C ≌ D) : 𝟭 C ⟶ e.functor ⋙ e.inverse := e.unit_iso.hom @[simp] def counit (e : C ≌ D) : e.inverse ⋙ e.functor ⟶ 𝟭 D := e.counit_iso.hom @[simp] def unit_inv (e : C ≌ D) : e.functor ⋙ e.inverse ⟶ 𝟭 C := e.unit_iso.inv @[simp] def counit_inv (e : C ≌ D) : 𝟭 D ⟶ e.inverse ⋙ e.functor := e.counit_iso.inv lemma unit_def (e : C ≌ D) : e.unit_iso.hom = e.unit := rfl lemma counit_def (e : C ≌ D) : e.counit_iso.hom = e.counit := rfl lemma unit_inv_def (e : C ≌ D) : e.unit_iso.inv = e.unit_inv := rfl lemma counit_inv_def (e : C ≌ D) : e.counit_iso.inv = e.counit_inv := rfl @[simp] lemma functor_unit_comp (e : C ≌ D) (X : C) : e.functor.map (e.unit.app X) ≫ e.counit.app (e.functor.obj X) = 𝟙 (e.functor.obj X) := e.functor_unit_iso_comp X @[simp] lemma counit_inv_functor_comp (e : C ≌ D) (X : C) : e.counit_inv.app (e.functor.obj X) ≫ e.functor.map (e.unit_inv.app X) = 𝟙 (e.functor.obj X) := begin erw [iso.inv_eq_inv (e.functor.map_iso (e.unit_iso.app X) ≪≫ e.counit_iso.app (e.functor.obj X)) (iso.refl _)], exact e.functor_unit_comp X end lemma functor_unit (e : C ≌ D) (X : C) : e.functor.map (e.unit.app X) = e.counit_inv.app (e.functor.obj X) := by { erw [←iso.comp_hom_eq_id (e.counit_iso.app _), functor_unit_comp], refl } lemma counit_functor (e : C ≌ D) (X : C) : e.counit.app (e.functor.obj X) = e.functor.map (e.unit_inv.app X) := by { erw [←iso.hom_comp_eq_id (e.functor.map_iso (e.unit_iso.app X)), functor_unit_comp], refl } /-- The other triangle equality. The proof follows the following proof in Globular: http://globular.science/1905.001 -/ @[simp] lemma unit_inverse_comp (e : C ≌ D) (Y : D) : e.unit.app (e.inverse.obj Y) ≫ e.inverse.map (e.counit.app Y) = 𝟙 (e.inverse.obj Y) := begin rw [←id_comp _ (e.inverse.map _), ←map_id e.inverse, ←counit_inv_functor_comp, map_comp, ←iso.hom_inv_id_assoc (e.unit_iso.app _) (e.inverse.map (e.functor.map _)), app_hom, app_inv, unit_def, unit_inv_def], slice_lhs 2 3 { erw [e.unit.naturality] }, slice_lhs 1 2 { erw [e.unit.naturality] }, slice_lhs 4 4 { rw [←iso.hom_inv_id_assoc (e.inverse.map_iso (e.counit_iso.app _)) (e.unit_inv.app _)] }, slice_lhs 3 4 { erw [←map_comp e.inverse, e.counit.naturality], erw [(e.counit_iso.app _).hom_inv_id, map_id] }, erw [id_comp], slice_lhs 2 3 { erw [←map_comp e.inverse, e.counit_iso.inv.naturality, map_comp] }, slice_lhs 3 4 { erw [e.unit_inv.naturality] }, slice_lhs 4 5 { erw [←map_comp (e.functor ⋙ e.inverse), (e.unit_iso.app _).hom_inv_id, map_id] }, erw [id_comp], slice_lhs 3 4 { erw [←e.unit_inv.naturality] }, slice_lhs 2 3 { erw [←map_comp e.inverse, ←e.counit_iso.inv.naturality, (e.counit_iso.app _).hom_inv_id, map_id] }, erw [id_comp, (e.unit_iso.app _).hom_inv_id], refl end @[simp] lemma inverse_counit_inv_comp (e : C ≌ D) (Y : D) : e.inverse.map (e.counit_inv.app Y) ≫ e.unit_inv.app (e.inverse.obj Y) = 𝟙 (e.inverse.obj Y) := begin erw [iso.inv_eq_inv (e.unit_iso.app (e.inverse.obj Y) ≪≫ e.inverse.map_iso (e.counit_iso.app Y)) (iso.refl _)], exact e.unit_inverse_comp Y end lemma unit_inverse (e : C ≌ D) (Y : D) : e.unit.app (e.inverse.obj Y) = e.inverse.map (e.counit_inv.app Y) := by { erw [←iso.comp_hom_eq_id (e.inverse.map_iso (e.counit_iso.app Y)), unit_inverse_comp], refl } lemma inverse_counit (e : C ≌ D) (Y : D) : e.inverse.map (e.counit.app Y) = e.unit_inv.app (e.inverse.obj Y) := by { erw [←iso.hom_comp_eq_id (e.unit_iso.app _), unit_inverse_comp], refl } @[simp] lemma fun_inv_map (e : C ≌ D) (X Y : D) (f : X ⟶ Y) : e.functor.map (e.inverse.map f) = e.counit.app X ≫ f ≫ e.counit_inv.app Y := (nat_iso.naturality_2 (e.counit_iso) f).symm @[simp] lemma inv_fun_map (e : C ≌ D) (X Y : C) (f : X ⟶ Y) : e.inverse.map (e.functor.map f) = e.unit_inv.app X ≫ f ≫ e.unit.app Y := (nat_iso.naturality_1 (e.unit_iso) f).symm section -- In this section we convert an arbitrary equivalence to a half-adjoint equivalence. variables {F : C ⥤ D} {G : D ⥤ C} (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) def adjointify_η : 𝟭 C ≅ F ⋙ G := calc 𝟭 C ≅ F ⋙ G : η ... ≅ F ⋙ (𝟭 D ⋙ G) : iso_whisker_left F (left_unitor G).symm ... ≅ F ⋙ ((G ⋙ F) ⋙ G) : iso_whisker_left F (iso_whisker_right ε.symm G) ... ≅ F ⋙ (G ⋙ (F ⋙ G)) : iso_whisker_left F (associator G F G) ... ≅ (F ⋙ G) ⋙ (F ⋙ G) : (associator F G (F ⋙ G)).symm ... ≅ 𝟭 C ⋙ (F ⋙ G) : iso_whisker_right η.symm (F ⋙ G) ... ≅ F ⋙ G : left_unitor (F ⋙ G) lemma adjointify_η_ε (X : C) : F.map ((adjointify_η η ε).hom.app X) ≫ ε.hom.app (F.obj X) = 𝟙 (F.obj X) := begin dsimp [adjointify_η], simp, have := ε.hom.naturality (F.map (η.inv.app X)), dsimp at this, rw [this], clear this, rw [←assoc _ _ _ (F.map _)], have := ε.hom.naturality (ε.inv.app $ F.obj X), dsimp at this, rw [this], clear this, have := (ε.app $ F.obj X).hom_inv_id, dsimp at this, rw [this], clear this, rw [id_comp], have := (F.map_iso $ η.app X).hom_inv_id, dsimp at this, rw [this] end end protected definition mk (F : C ⥤ D) (G : D ⥤ C) (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) : C ≌ D := ⟨F, G, adjointify_η η ε, ε, adjointify_η_ε η ε⟩ omit 𝒟 @[refl] def refl : C ≌ C := equivalence.mk (𝟭 C) (𝟭 C) (iso.refl _) (iso.refl _) include 𝒟 @[symm] def symm (e : C ≌ D) : D ≌ C := ⟨e.inverse, e.functor, e.counit_iso.symm, e.unit_iso.symm, e.inverse_counit_inv_comp⟩ variables {E : Type u₃} [ℰ : category.{v₃} E] include ℰ @[trans] def trans (e : C ≌ D) (f : D ≌ E) : C ≌ E := begin apply equivalence.mk (e.functor ⋙ f.functor) (f.inverse ⋙ e.inverse), { refine iso.trans e.unit_iso _, exact iso_whisker_left e.functor (iso_whisker_right f.unit_iso e.inverse) }, { refine iso.trans _ f.counit_iso, exact iso_whisker_left f.inverse (iso_whisker_right e.counit_iso f.functor) } end def fun_inv_id_assoc (e : C ≌ D) (F : C ⥤ E) : e.functor ⋙ e.inverse ⋙ F ≅ F := (functor.associator _ _ _).symm ≪≫ iso_whisker_right e.unit_iso.symm F ≪≫ F.left_unitor @[simp] lemma fun_inv_id_assoc_hom_app (e : C ≌ D) (F : C ⥤ E) (X : C) : (fun_inv_id_assoc e F).hom.app X = F.map (e.unit_inv.app X) := by { dsimp [fun_inv_id_assoc], tidy } @[simp] lemma fun_inv_id_assoc_inv_app (e : C ≌ D) (F : C ⥤ E) (X : C) : (fun_inv_id_assoc e F).inv.app X = F.map (e.unit.app X) := by { dsimp [fun_inv_id_assoc], tidy } def inv_fun_id_assoc (e : C ≌ D) (F : D ⥤ E) : e.inverse ⋙ e.functor ⋙ F ≅ F := (functor.associator _ _ _).symm ≪≫ iso_whisker_right e.counit_iso F ≪≫ F.left_unitor @[simp] lemma inv_fun_id_assoc_hom_app (e : C ≌ D) (F : D ⥤ E) (X : D) : (inv_fun_id_assoc e F).hom.app X = F.map (e.counit.app X) := by { dsimp [inv_fun_id_assoc], tidy } @[simp] lemma inv_fun_id_assoc_inv_app (e : C ≌ D) (F : D ⥤ E) (X : D) : (inv_fun_id_assoc e F).inv.app X = F.map (e.counit_inv.app X) := by { dsimp [inv_fun_id_assoc], tidy } end equivalence /-- A functor that is part of a (half) adjoint equivalence -/ class is_equivalence (F : C ⥤ D) := mk' :: (inverse : D ⥤ C) (unit_iso : 𝟭 C ≅ F ⋙ inverse) (counit_iso : inverse ⋙ F ≅ 𝟭 D) (functor_unit_iso_comp' : ∀ (X : C), F.map ((unit_iso.hom : 𝟭 C ⟶ F ⋙ inverse).app X) ≫ counit_iso.hom.app (F.obj X) = 𝟙 (F.obj X) . obviously) restate_axiom is_equivalence.functor_unit_iso_comp' namespace is_equivalence instance of_equivalence (F : C ≌ D) : is_equivalence F.functor := { ..F } instance of_equivalence_inverse (F : C ≌ D) : is_equivalence F.inverse := is_equivalence.of_equivalence F.symm open equivalence protected definition mk {F : C ⥤ D} (G : D ⥤ C) (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) : is_equivalence F := ⟨G, adjointify_η η ε, ε, adjointify_η_ε η ε⟩ end is_equivalence namespace functor def as_equivalence (F : C ⥤ D) [is_equivalence F] : C ≌ D := ⟨F, is_equivalence.inverse F, is_equivalence.unit_iso F, is_equivalence.counit_iso F, is_equivalence.functor_unit_iso_comp F⟩ omit 𝒟 instance is_equivalence_refl : is_equivalence (𝟭 C) := is_equivalence.of_equivalence equivalence.refl include 𝒟 def inv (F : C ⥤ D) [is_equivalence F] : D ⥤ C := is_equivalence.inverse F instance is_equivalence_inv (F : C ⥤ D) [is_equivalence F] : is_equivalence F.inv := is_equivalence.of_equivalence F.as_equivalence.symm def fun_inv_id (F : C ⥤ D) [is_equivalence F] : F ⋙ F.inv ≅ 𝟭 C := (is_equivalence.unit_iso F).symm def inv_fun_id (F : C ⥤ D) [is_equivalence F] : F.inv ⋙ F ≅ 𝟭 D := is_equivalence.counit_iso F variables {E : Type u₃} [ℰ : category.{v₃} E] include ℰ instance is_equivalence_trans (F : C ⥤ D) (G : D ⥤ E) [is_equivalence F] [is_equivalence G] : is_equivalence (F ⋙ G) := is_equivalence.of_equivalence (equivalence.trans (as_equivalence F) (as_equivalence G)) end functor namespace is_equivalence @[simp] lemma fun_inv_map (F : C ⥤ D) [is_equivalence F] (X Y : D) (f : X ⟶ Y) : F.map (F.inv.map f) = (F.inv_fun_id.hom.app X) ≫ f ≫ (F.inv_fun_id.inv.app Y) := begin erw [nat_iso.naturality_2], refl end @[simp] lemma inv_fun_map (F : C ⥤ D) [is_equivalence F] (X Y : C) (f : X ⟶ Y) : F.inv.map (F.map f) = (F.fun_inv_id.hom.app X) ≫ f ≫ (F.fun_inv_id.inv.app Y) := begin erw [nat_iso.naturality_2], refl end -- We should probably restate many of the lemmas about `equivalence` for `is_equivalence`, -- but these are the only ones I need for now. @[simp] lemma functor_unit_comp (E : C ⥤ D) [is_equivalence E] (Y) : E.map (((is_equivalence.unit_iso E).hom).app Y) ≫ ((is_equivalence.counit_iso E).hom).app (E.obj Y) = 𝟙 _ := equivalence.functor_unit_comp (E.as_equivalence) Y @[simp] lemma counit_inv_functor_comp (E : C ⥤ D) [is_equivalence E] (Y) : ((is_equivalence.counit_iso E).inv).app (E.obj Y) ≫ E.map (((is_equivalence.unit_iso E).inv).app Y) = 𝟙 _ := eq_of_inv_eq_inv (functor_unit_comp _ _) end is_equivalence class ess_surj (F : C ⥤ D) := (obj_preimage (d : D) : C) (iso' (d : D) : F.obj (obj_preimage d) ≅ d . obviously) restate_axiom ess_surj.iso' namespace functor def obj_preimage (F : C ⥤ D) [ess_surj F] (d : D) : C := ess_surj.obj_preimage.{v₁ v₂} F d def fun_obj_preimage_iso (F : C ⥤ D) [ess_surj F] (d : D) : F.obj (F.obj_preimage d) ≅ d := ess_surj.iso F d end functor namespace equivalence def ess_surj_of_equivalence (F : C ⥤ D) [is_equivalence F] : ess_surj F := ⟨ λ Y : D, F.inv.obj Y, λ Y : D, (F.inv_fun_id.app Y) ⟩ instance faithful_of_equivalence (F : C ⥤ D) [is_equivalence F] : faithful F := { injectivity' := λ X Y f g w, begin have p := congr_arg (@category_theory.functor.map _ _ _ _ F.inv _ _) w, simpa only [cancel_epi, cancel_mono, is_equivalence.inv_fun_map] using p end }. instance full_of_equivalence (F : C ⥤ D) [is_equivalence F] : full F := { preimage := λ X Y f, (F.fun_inv_id.app X).inv ≫ (F.inv.map f) ≫ (F.fun_inv_id.app Y).hom, witness' := λ X Y f, begin apply F.inv.injectivity, /- obviously can finish from here... -/ dsimp, simp, dsimp, slice_lhs 4 6 { rw [←functor.map_comp, ←functor.map_comp], rw [←is_equivalence.fun_inv_map], }, slice_lhs 1 2 { simp }, dsimp, simp, slice_lhs 2 4 { rw [←functor.map_comp, ←functor.map_comp], erw [nat_iso.naturality_2], }, erw [nat_iso.naturality_1], refl end }. @[simp] private def equivalence_inverse (F : C ⥤ D) [full F] [faithful F] [ess_surj F] : D ⥤ C := { obj := λ X, F.obj_preimage X, map := λ X Y f, F.preimage ((F.fun_obj_preimage_iso X).hom ≫ f ≫ (F.fun_obj_preimage_iso Y).inv), map_id' := λ X, begin apply F.injectivity, tidy end, map_comp' := λ X Y Z f g, by apply F.injectivity; simp }. def equivalence_of_fully_faithfully_ess_surj (F : C ⥤ D) [full F] [faithful F] [ess_surj F] : is_equivalence F := is_equivalence.mk (equivalence_inverse F) (nat_iso.of_components (λ X, (preimage_iso $ F.fun_obj_preimage_iso $ F.obj X).symm) (λ X Y f, by { apply F.injectivity, obviously })) (nat_iso.of_components (λ Y, F.fun_obj_preimage_iso Y) (by obviously)) end equivalence end category_theory
22eccffe35f2eeb9caa01d531ca8a37113133d1c
1f6fe2f89976b14a4567ab298c35792b21f2e50b
/algebra/exactness.hlean
1af74b2ca45fd7a8546b9867d1698fd6ace95f81
[ "Apache-2.0" ]
permissive
jonas-frey/Spectral
e5c1c2f7bcac26aa55f7b1e041a81272a146198d
72d521091525a4bc9a31cac859840efe9461cf66
refs/heads/master
1,610,235,743,345
1,505,417,795,000
1,505,417,795,000
102,653,342
0
0
null
1,504,728,483,000
1,504,728,483,000
null
UTF-8
Lean
false
false
10,856
hlean
/- Copyright (c) 2017 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad Short exact sequences -/ import homotopy.chain_complex eq2 .quotient_group open pointed is_trunc equiv is_equiv eq algebra group trunc function fiber sigma property structure is_exact_t {A B : Type} {C : Type*} (f : A → B) (g : B → C) := ( im_in_ker : Π(a:A), g (f a) = pt) ( ker_in_im : Π(b:B), (g b = pt) → fiber f b) structure is_exact {A B : Type} {C : Type*} (f : A → B) (g : B → C) := ( im_in_ker : Π(a:A), g (f a) = pt) ( ker_in_im : Π(b:B), (g b = pt) → image f b) namespace algebra definition is_exact_g {A B C : Group} (f : A →g B) (g : B →g C) := is_exact f g definition is_exact_ag {A B C : AbGroup} (f : A →g B) (g : B →g C) := is_exact f g definition is_exact_g.mk {A B C : Group} {f : A →g B} {g : B →g C} (H₁ : Πa, g (f a) = 1) (H₂ : Πb, g b = 1 → image f b) : is_exact_g f g := is_exact.mk H₁ H₂ definition is_exact.im_in_ker2 {A B : Type} {C : Set*} {f : A → B} {g : B → C} (H : is_exact f g) {b : B} (h : image f b) : g b = pt := begin induction h with a p, exact ap g p⁻¹ ⬝ is_exact.im_in_ker H a end definition is_exact_homotopy {A B : Type} {C : Type*} {f f' : A → B} {g g' : B → C} (p : f ~ f') (q : g ~ g') (H : is_exact f g) : is_exact f' g' := begin induction p using homotopy.rec_on_idp, induction q using homotopy.rec_on_idp, exact H end definition is_exact_trunc_functor {A B : Type} {C : Type*} {f : A → B} {g : B → C} (H : is_exact_t f g) : @is_exact _ _ (ptrunc 0 C) (trunc_functor 0 f) (trunc_functor 0 g) := begin constructor, { intro a, esimp, induction a with a, exact ap tr (is_exact_t.im_in_ker H a) }, { intro b p, induction b with b, note q := !tr_eq_tr_equiv p, induction q with q, induction is_exact_t.ker_in_im H b q with a r, exact image.mk (tr a) (ap tr r) } end definition is_contr_middle_of_is_exact {A B : Type} {C : Type*} {f : A → B} {g : B → C} (H : is_exact f g) [is_contr A] [is_set B] [is_contr C] : is_contr B := begin apply is_contr.mk (f pt), intro b, induction is_exact.ker_in_im H b !is_prop.elim, exact ap f !is_prop.elim ⬝ p end definition is_surjective_of_is_exact_of_is_contr {A B : Type} {C : Type*} {f : A → B} {g : B → C} (H : is_exact f g) [is_contr C] : is_surjective f := λb, is_exact.ker_in_im H b !is_prop.elim definition is_embedding_of_is_exact_g {A B C : Group} {g : B →g C} {f : A →g B} (gf : is_exact_g f g) [is_contr A] : is_embedding g := begin apply to_is_embedding_homomorphism, intro a p, induction is_exact.ker_in_im gf a p with x q, exact q⁻¹ ⬝ ap f !is_prop.elim ⬝ to_respect_one f end definition map_left_of_is_exact {G₃' G₃ G₂ : Type} {G₁ : Type*} {g : G₃ → G₂} {g' : G₃' → G₂} {f : G₂ → G₁} (H1 : is_exact g f) (H2 : is_exact g' f) (Hg' : is_embedding g') : G₃ → G₃' := begin intro a, have fiber g' (g a), begin have is_prop (fiber g' (g a)), from !is_prop_fiber_of_is_embedding, induction is_exact.ker_in_im H2 (g a) (is_exact.im_in_ker H1 a) with a' p, exact fiber.mk a' p end, exact point this end definition map_left_of_is_exact_compute {G₃' G₃ G₂ : Type} {G₁ : Type*} {g : G₃ → G₂} {g' : G₃' → G₂} {f : G₂ → G₁} (H1 : is_exact g f) (H2 : is_exact g' f) (Hg' : is_embedding g') (a : G₃) : g' (map_left_of_is_exact H1 H2 Hg' a) = g a := @point_eq _ _ g' _ _ definition map_left_of_is_exact_compose {G₃'' G₃' G₃ G₂ : Type} {G₁ : Type*} {g : G₃ → G₂} {g' : G₃' → G₂} {g'' : G₃'' → G₂} {f : G₂ → G₁} (H1 : is_exact g f) (H2 : is_exact g' f) (H3 : is_exact g'' f) (Hg' : is_embedding g') (Hg'' : is_embedding g'') (a : G₃) : map_left_of_is_exact H2 H3 Hg'' (map_left_of_is_exact H1 H2 Hg' a) = map_left_of_is_exact H1 H3 Hg'' a := begin refine @is_injective_of_is_embedding _ _ g'' _ _ _ _, refine !map_left_of_is_exact_compute ⬝ _ ⬝ !map_left_of_is_exact_compute⁻¹, exact map_left_of_is_exact_compute H1 H2 Hg' a end definition map_left_of_is_exact_id {G₃ G₂ : Type} {G₁ : Type*} {g : G₃ → G₂} {f : G₂ → G₁} (H1 : is_exact g f) (Hg : is_embedding g) (a : G₃) : map_left_of_is_exact H1 H1 Hg a = a := begin refine @is_injective_of_is_embedding _ _ g _ _ _ _, exact map_left_of_is_exact_compute H1 H1 Hg a end definition map_left_of_is_exact_homotopy {G₃' G₃ G₂ : Type} {G₁ : Type*} {g : G₃ → G₂} {g' g'' : G₃' → G₂} {f : G₂ → G₁} (H1 : is_exact g f) (H2 : is_exact g' f) (H3 : is_exact g'' f) (Hg' : is_embedding g') (Hg'' : is_embedding g'') (p : g' ~ g'') : map_left_of_is_exact H1 H2 Hg' ~ map_left_of_is_exact H1 H3 Hg'' := begin intro a, refine @is_injective_of_is_embedding _ _ g' _ _ _ _, exact !map_left_of_is_exact_compute ⬝ (!p ⬝ !map_left_of_is_exact_compute)⁻¹, end definition homomorphism_left_of_is_exact_g {G₃' G₃ G₂ G₁ : Group} {g : G₃ →g G₂} {g' : G₃' →g G₂} {f : G₂ →g G₁} (H1 : is_exact_g g f) (H2 : is_exact_g g' f) (Hg' : is_embedding g') : G₃ →g G₃' := begin apply homomorphism.mk (map_left_of_is_exact H1 H2 Hg'), { intro a a', refine @is_injective_of_is_embedding _ _ g' _ _ _ _, exact !point_eq ⬝ to_respect_mul g a a' ⬝ (to_respect_mul g' _ _ ⬝ ap011 mul !point_eq !point_eq)⁻¹ } end definition isomorphism_left_of_is_exact_g {G₃' G₃ G₂ G₁ : Group} {g : G₃ →g G₂} {g' : G₃' →g G₂} {f : G₂ →g G₁} (H1 : is_exact g f) (H2 : is_exact g' f) (Hg : is_embedding g) (Hg' : is_embedding g') : G₃ ≃g G₃' := begin fapply isomorphism.mk, exact homomorphism_left_of_is_exact_g H1 H2 Hg', fapply adjointify, exact homomorphism_left_of_is_exact_g H2 H1 Hg, { intro a, refine @is_injective_of_is_embedding _ _ g' _ _ _ _, refine map_left_of_is_exact_compute H1 H2 Hg' _ ⬝ map_left_of_is_exact_compute H2 H1 Hg a }, { intro a, refine @is_injective_of_is_embedding _ _ g _ _ _ _, refine map_left_of_is_exact_compute H2 H1 Hg _ ⬝ map_left_of_is_exact_compute H1 H2 Hg' a }, end definition is_exact_incl_of_subgroup {G H : Group} (f : G →g H) : is_exact (incl_of_subgroup (kernel f)) f := begin apply is_exact.mk, { intro x, cases x with x p, exact p }, { intro x p, exact image.mk ⟨x, p⟩ idp } end definition isomorphism_kernel_of_is_exact {G₄ G₃ G₂ G₁ : Group} {h : G₄ →g G₃} {g : G₃ →g G₂} {f : G₂ →g G₁} (H1 : is_exact h g) (H2 : is_exact g f) (HG : is_contr G₄) : G₃ ≃g Kernel f := isomorphism_left_of_is_exact_g H2 (is_exact_incl_of_subgroup f) (is_embedding_of_is_exact_g H1) (is_embedding_incl_of_subgroup _) section chain_complex open succ_str chain_complex definition is_exact_of_is_exact_at {N : succ_str} {A : chain_complex N} {n : N} (H : is_exact_at A n) : is_exact (cc_to_fn A (S n)) (cc_to_fn A n) := is_exact.mk (cc_is_chain_complex A n) H end chain_complex structure is_short_exact {A B : Type} {C : Type*} (f : A → B) (g : B → C) := (is_emb : is_embedding f) (im_in_ker : Π(a:A), g (f a) = pt) (ker_in_im : Π(b:B), (g b = pt) → image f b) (is_surj : is_surjective g) structure is_short_exact_t {A B : Type} {C : Type*} (f : A → B) (g : B → C) := (is_emb : is_embedding f) (im_in_ker : Π(a:A), g (f a) = pt) (ker_in_im : Π(b:B), (g b = pt) → fiber f b) (is_surj : is_split_surjective g) lemma is_short_exact_of_is_exact {X A B C Y : Group} (k : X →g A) (f : A →g B) (g : B →g C) (l : C →g Y) (hX : is_contr X) (hY : is_contr Y) (kf : is_exact_g k f) (fg : is_exact_g f g) (gl : is_exact_g g l) : is_short_exact f g := begin constructor, { exact is_embedding_of_is_exact_g kf }, { exact is_exact.im_in_ker fg }, { exact is_exact.ker_in_im fg }, { intro c, exact is_exact.ker_in_im gl c !is_prop.elim }, end lemma is_short_exact_equiv {A B A' B' : Type} {C C' : Type*} {f' : A' → B'} {g' : B' → C'} (f : A → B) (g : B → C) (eA : A ≃ A') (eB : B ≃ B') (eC : C ≃* C') (h₁ : hsquare f f' eA eB) (h₂ : hsquare g g' eB eC) (H : is_short_exact f' g') : is_short_exact f g := begin constructor, { apply is_embedding_homotopy_closed_rev (homotopy_top_of_hsquare h₁), apply is_embedding_compose, apply is_embedding_of_is_equiv, apply is_embedding_compose, apply is_short_exact.is_emb H, apply is_embedding_of_is_equiv }, { intro a, refine homotopy_top_of_hsquare' (hhconcat h₁ h₂) a ⬝ _, refine ap eC⁻¹ _ ⬝ respect_pt eC⁻¹ᵉ*, exact is_short_exact.im_in_ker H (eA a) }, { intro b p, note q := eq_of_inv_eq ((homotopy_top_of_hsquare' h₂ b)⁻¹ ⬝ p) ⬝ respect_pt eC, induction is_short_exact.ker_in_im H (eB b) q with a' r, apply image.mk (eA⁻¹ a'), exact eq_of_fn_eq_fn eB ((homotopy_top_of_hsquare h₁⁻¹ʰᵗʸᵛ a')⁻¹ ⬝ r) }, { apply is_surjective_homotopy_closed_rev (homotopy_top_of_hsquare' h₂), apply is_surjective_compose, apply is_surjective_of_is_equiv, apply is_surjective_compose, apply is_short_exact.is_surj H, apply is_surjective_of_is_equiv } end lemma is_exact_of_is_short_exact {A B : Type} {C : Type*} {f : A → B} {g : B → C} (H : is_short_exact f g) : is_exact f g := begin constructor, { exact is_short_exact.im_in_ker H }, { exact is_short_exact.ker_in_im H } end /- TODO: move and remove other versions -/ definition is_surjective_qg_map {A : Group} (N : property A) [is_normal_subgroup A N] : is_surjective (qg_map N) := begin intro x, induction x, fapply image.mk, exact a, reflexivity, apply is_prop.elimo end definition is_surjective_ab_qg_map {A : AbGroup} (N : property A) [is_normal_subgroup A N] : is_surjective (ab_qg_map N) := is_surjective_ab_qg_map _ definition qg_map_eq_one {A : Group} {K : property A} [is_normal_subgroup A K] (g : A) (H : g ∈ K) : qg_map K g = 1 := begin apply set_quotient.eq_of_rel, have e : g * 1⁻¹ = g, from calc g * 1⁻¹ = g * 1 : one_inv ... = g : mul_one, exact transport (λx, K x) e⁻¹ H end definition ab_qg_map_eq_one {A : AbGroup} {K : property A} [is_subgroup A K] (g : A) (H : g ∈ K) : ab_qg_map K g = 1 := ab_qg_map_eq_one g H definition is_short_exact_normal_subgroup {G : Group} (S : property G) [is_normal_subgroup G S] : is_short_exact (incl_of_subgroup S) (qg_map S) := begin fconstructor, { exact is_embedding_incl_of_subgroup S }, { intro a, fapply qg_map_eq_one, induction a with b p, exact p }, { intro b p, fapply image.mk, { apply sigma.mk b, fapply rel_of_qg_map_eq_one, exact p }, reflexivity }, { exact is_surjective_qg_map S }, end end algebra
49341895f2838026ae52e218dddaa2b12f2ab918
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/category_theory/monoidal/unitors.lean
354f869e0b9b2d0e9b50247215566f6183d7d9ec
[ "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
5,739
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.monoidal.category /-! # The two morphisms `λ_ (𝟙_ C)` and `ρ_ (𝟙_ C)` from `𝟙_ C ⊗ 𝟙_ C` to `𝟙_ C` are equal. This is suprisingly difficult to prove directly from the usual axioms for a monoidal category! This proof follows the diagram given at https://people.math.osu.edu/penneys.2/QS2019/VicaryHandout.pdf It should be a consequence of the coherence theorem for monoidal categories (although quite possibly it is a necessary building block of any proof). -/ universes v u namespace category_theory.monoidal_category open category_theory open category_theory.category open category_theory.monoidal_category variables {C : Type u} [category.{v} C] [monoidal_category.{v} C] namespace unitors_equal lemma cells_1_2 : (ρ_ (𝟙_ C)).hom = (λ_ ((𝟙_ C) ⊗ (𝟙_ C))).inv ≫ ((𝟙 (𝟙_ C)) ⊗ (ρ_ (𝟙_ C)).hom) ≫ (λ_ (𝟙_ C)).hom := by rw [left_unitor_conjugation] lemma cells_4 : (λ_ ((𝟙_ C) ⊗ (𝟙_ C))).inv ≫ ((𝟙 (𝟙_ C)) ⊗ ((λ_ (𝟙_ C)).hom)) = (λ_ (𝟙_ C)).hom ≫ (λ_ (𝟙_ C)).inv := by rw [←left_unitor_inv_naturality, iso.hom_inv_id] lemma cells_4' : (λ_ ((𝟙_ C) ⊗ (𝟙_ C))).inv = (λ_ (𝟙_ C)).hom ≫ (λ_ (𝟙_ C)).inv ≫ ((𝟙 (𝟙_ C)) ⊗ ((λ_ (𝟙_ C)).inv)) := by rw [←assoc, ←cells_4, assoc, ←id_tensor_comp, iso.hom_inv_id, tensor_id, comp_id] lemma cells_3_4 : (λ_ ((𝟙_ C) ⊗ (𝟙_ C))).inv = (𝟙 (𝟙_ C)) ⊗ ((λ_ (𝟙_ C)).inv) := by rw [cells_4', ←assoc, iso.hom_inv_id, id_comp] lemma cells_1_4 : (ρ_ (𝟙_ C)).hom = ((𝟙 (𝟙_ C)) ⊗ ((λ_ (𝟙_ C)).inv)) ≫ ((𝟙 (𝟙_ C)) ⊗ (ρ_ (𝟙_ C)).hom) ≫ (λ_ (𝟙_ C)).hom := begin rw [←cells_3_4], conv_lhs { rw [cells_1_2] }, end lemma cells_6 : ((ρ_ (𝟙_ C)).inv ⊗ (𝟙 (𝟙_ C))) ≫ (ρ_ ((𝟙_ C) ⊗ (𝟙_ C))).hom = (ρ_ (𝟙_ C)).hom ≫ (ρ_ (𝟙_ C)).inv := by rw [right_unitor_naturality, iso.hom_inv_id] lemma cells_6' : ((ρ_ (𝟙_ C)).inv ⊗ (𝟙 (𝟙_ C))) = (ρ_ (𝟙_ C)).hom ≫ (ρ_ (𝟙_ C)).inv ≫ (ρ_ ((𝟙_ C) ⊗ (𝟙_ C))).inv := by {rw [←assoc, ←cells_6, assoc, iso.hom_inv_id, comp_id], } lemma cells_5_6 : ((ρ_ (𝟙_ C)).inv ⊗ (𝟙 (𝟙_ C))) = (ρ_ (𝟙_ C ⊗ 𝟙_ C)).inv := by rw [cells_6', ←assoc, iso.hom_inv_id, id_comp] lemma cells_7 : ((𝟙 (𝟙_ C)) ⊗ ((λ_ (𝟙_ C)).inv)) = ((ρ_ (𝟙_ C)).inv ⊗ (𝟙 (𝟙_ C))) ≫ (α_ (𝟙_ C) (𝟙_ C) (𝟙_ C)).hom := by simp only [triangle_assoc_comp_right_inv, tensor_left_iff] lemma cells_1_7 : (ρ_ (𝟙_ C)).hom = (ρ_ ((𝟙_ C) ⊗ (𝟙_ C))).inv ≫ (α_ (𝟙_ C) (𝟙_ C) (𝟙_ C)).hom ≫ ((𝟙 (𝟙_ C)) ⊗ (ρ_ (𝟙_ C)).hom) ≫ (λ_ (𝟙_ C)).hom := begin conv_lhs { rw [cells_1_4] }, conv_lhs { congr, rw [cells_7] }, conv_lhs { congr, congr, rw [cells_5_6] }, conv_rhs { rw [←assoc] } end lemma cells_8 : (α_ (𝟙_ C) (𝟙_ C) (𝟙_ C)).hom = (ρ_ (((𝟙_ C) ⊗ (𝟙_ C)) ⊗ (𝟙_ C))).inv ≫ ((α_ (𝟙_ C) (𝟙_ C) (𝟙_ C)).hom ⊗ 𝟙 (𝟙_ C)) ≫ (ρ_ ((𝟙_ C) ⊗ ((𝟙_ C) ⊗ (𝟙_ C)))).hom := by rw [right_unitor_conjugation]. lemma cells_14 : (ρ_ ((𝟙_ C) ⊗ (𝟙_ C))).inv ≫ (ρ_ (((𝟙_ C) ⊗ (𝟙_ C)) ⊗ (𝟙_ C))).inv = (ρ_ ((𝟙_ C) ⊗ (𝟙_ C))).inv ≫ ((ρ_ ((𝟙_ C) ⊗ (𝟙_ C))).inv ⊗ (𝟙 (𝟙_ C))) := by rw [right_unitor_inv_naturality] lemma cells_9 : ((α_ (𝟙_ C) (𝟙_ C) (𝟙_ C)).hom ⊗ 𝟙 (𝟙_ C)) = (α_ ((𝟙_ C) ⊗ (𝟙_ C)) (𝟙_ C) (𝟙_ C)).hom ≫ (α_ (𝟙_ C) (𝟙_ C) ((𝟙_ C) ⊗ (𝟙_ C))).hom ≫ ((𝟙 (𝟙_ C)) ⊗ (α_ (𝟙_ C) (𝟙_ C) (𝟙_ C)).inv) ≫ (α_ (𝟙_ C) ((𝟙_ C) ⊗ (𝟙_ C)) (𝟙_ C)).inv := begin slice_rhs 1 2 { rw ←(monoidal_category.pentagon (𝟙_ C) (𝟙_ C) (𝟙_ C) (𝟙_ C)) }, slice_rhs 3 4 { rw [←id_tensor_comp, iso.hom_inv_id], }, simp, end lemma cells_10_13 : ((ρ_ ((𝟙_ C) ⊗ (𝟙_ C))).inv ⊗ (𝟙 (𝟙_ C))) ≫ (α_ ((𝟙_ C) ⊗ (𝟙_ C)) (𝟙_ C) (𝟙_ C)).hom ≫ (α_ (𝟙_ C) (𝟙_ C) ((𝟙_ C) ⊗ (𝟙_ C))).hom ≫ ((𝟙 (𝟙_ C)) ⊗ (α_ (𝟙_ C) (𝟙_ C) (𝟙_ C)).inv) ≫ (α_ (𝟙_ C) ((𝟙_ C) ⊗ (𝟙_ C)) (𝟙_ C)).inv = ((𝟙 (𝟙_ C)) ⊗ (ρ_ (𝟙_ C)).inv) ⊗ (𝟙 (𝟙_ C)) := begin slice_lhs 1 2 { simp, }, slice_lhs 1 2 { rw [←tensor_id, associator_naturality], }, slice_lhs 2 3 { rw [←id_tensor_comp], simp, }, slice_lhs 1 2 { rw ←associator_naturality, }, simp, end lemma cells_9_13 : ((ρ_ ((𝟙_ C) ⊗ (𝟙_ C))).inv ⊗ (𝟙 (𝟙_ C))) ≫ ((α_ (𝟙_ C) (𝟙_ C) (𝟙_ C)).hom ⊗ 𝟙 (𝟙_ C)) = ((𝟙 (𝟙_ C)) ⊗ (ρ_ (𝟙_ C)).inv) ⊗ (𝟙 (𝟙_ C)) := begin rw [cells_9, ←cells_10_13] end lemma cells_15 : (ρ_ ((𝟙_ C) ⊗ (𝟙_ C))).inv ≫ (((𝟙 (𝟙_ C)) ⊗ (ρ_ (𝟙_ C)).inv) ⊗ (𝟙 (𝟙_ C))) ≫ (ρ_ ((𝟙_ C) ⊗ ((𝟙_ C) ⊗ (𝟙_ C)))).hom ≫ ((𝟙 (𝟙_ C)) ⊗ (ρ_ (𝟙_ C)).hom) = 𝟙 _ := begin slice_lhs 1 2 { rw [←right_unitor_inv_naturality] }, slice_lhs 2 3 { rw [iso.inv_hom_id] }, rw [id_comp, ←id_tensor_comp, iso.inv_hom_id, tensor_id], end end unitors_equal open unitors_equal lemma unitors_equal : (λ_ (𝟙_ C)).hom = (ρ_ (𝟙_ C)).hom := begin rw cells_1_7, rw cells_8, slice_rhs 1 2 { rw cells_14 }, slice_rhs 2 3 { rw cells_9_13 }, slice_rhs 1 4 { rw cells_15 }, rw id_comp, end end category_theory.monoidal_category
a3f204755c803342eca1f9e78b14d89ae6e9cb5b
74caf7451c921a8d5ab9c6e2b828c9d0a35aae95
/library/init/data/set.lean
63f2089cd8edd71d5e079ee75aa723665adb7f85
[ "Apache-2.0" ]
permissive
sakas--/lean
f37b6fad4fd4206f2891b89f0f8135f57921fc3f
570d9052820be1d6442a5cc58ece37397f8a9e4c
refs/heads/master
1,586,127,145,194
1,480,960,018,000
1,480,960,635,000
40,137,176
0
0
null
1,438,621,351,000
1,438,621,351,000
null
UTF-8
Lean
false
false
1,629
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.logic init.category.functor universe variables u v def set (α : Type u) := α → Prop def set_of {α : Type u} (p : α → Prop) : set α := p namespace set variables {α : Type u} {β : Type v} protected def mem (a : α) (s : set α) := s a instance : has_mem α set := ⟨set.mem⟩ protected def subset (s₁ s₂ : set α) := ∀ {a}, a ∈ s₁ → a ∈ s₂ instance : has_subset (set α) := ⟨set.subset⟩ protected def sep (p : α → Prop) (s : set α) : set α := {a | a ∈ s ∧ p a} instance : has_sep α set := ⟨set.sep⟩ instance : has_emptyc (set α) := ⟨λ a, false⟩ protected def insert (a : α) (s : set α) : set α := {b | b = a ∨ b ∈ s} instance : has_insert α set := ⟨set.insert⟩ protected def union (s₁ s₂ : set α) : set α := {a | a ∈ s₁ ∨ a ∈ s₂} instance : has_union (set α) := ⟨set.union⟩ protected def inter (s₁ s₂ : set α) : set α := {a | a ∈ s₁ ∧ a ∈ s₂} instance : has_inter (set α) := ⟨set.inter⟩ def compl (s : set α) : set α := {a | a ∉ s} instance : has_neg (set α) := ⟨compl⟩ protected def diff (s t : set α) : set α := {a ∈ s | a ∉ t} instance : has_sdiff (set α) := ⟨set.diff⟩ def powerset (s : set α) : set (set α) := {t | t ⊆ s} prefix `𝒫`:100 := powerset def image (f : α → β) (s : set α) : set β := {b | ∃ a, a ∈ s ∧ f a = b} instance : functor set := ⟨@set.image⟩ end set
9f1e2198cc609001d44b86991155cfc8f2a130f7
75c54c8946bb4203e0aaf196f918424a17b0de99
/old/language_list.lean
9f9794bbf74bde8fa36d563865fd1ab02e003d18
[ "Apache-2.0" ]
permissive
urkud/flypitch
261e2a45f1038130178575406df8aea78255ba77
2250f5eda14b6ef9fc3e4e1f4a9ac4005634de5c
refs/heads/master
1,653,266,469,246
1,577,819,679,000
1,577,819,679,000
259,862,235
1
0
Apache-2.0
1,588,147,244,000
1,588,147,244,000
null
UTF-8
Lean
false
false
9,363
lean
/- Along with the half-finished definition of substitution here is an attempt to define terms using lists instead of vectors. The problem is, since the type "list" does ot say anything about the size of the list, it is possible to use this definition to define terms that apply a function of any arity to a list of any length. To prevent this from happening, we might be able to use an auxiliary well_formed_term definition, and prove that it is preserved after operations like substitution. However, the same well-founded recursion problem reared its head at a certain point -- here it was after writing a really long theorem about the preservation of well-formed-ness. If you wait for it to compile, you can read the error message below on mutual theorem sub_term_well_formed, sub_term_list_well_formed It was too much to think about creating a well-founded relation over these theorems; it takes about two minutes just for Lean to read through the tactics as it is.. I'm pretty sure that this approach is not any easier. -/ structure Language := language :: (relations : Type) (functions : Type ) (arityF : functions → nat) (arityR : relations → nat) variable L : Language variable n : nat universe u inductive term | var : nat → term | apply: L.functions → list term → term mutual def well_formed_term, well_formed_term_list with well_formed_term : term L -> Prop | (term.var L _) := true | (term.apply f l) := ((L.arityF f) = list.length l) ∧ (well_formed_term_list l) with well_formed_term_list : list (term L) → Prop | [] := true | (list.cons t ts) := (well_formed_term t) ∧ well_formed_term_list ts def nat_list_union : list nat → list nat → list nat | [] l := l | (list.cons n ns) l := if n ∈ l then list.cons n (nat_list_union ns l) else nat_list_union ns l def nat_list_remove : nat → list nat → list nat | _ [] := [] | n (list.cons m ms) := if (n=m) then nat_list_remove n ms else list.cons n (nat_list_remove n ms) def nat_list_max : list nat → nat | [] := 0 | (list.cons n ns) := if n ≥ nat_list_max ns then n else nat_list_max ns lemma nat_list_max_is_max : ∀ (l : list nat) (n : nat), n ∈ l → (nat_list_max l) ≥ n := begin intros l n n_in_l, induction l, apply absurd, apply list.not_mem_nil, exact n_in_l, exact not_false, simp [nat_list_max], have ex_mid : (l_hd ≥ nat_list_max l_tl) ∨ ¬ (l_hd ≥ nat_list_max l_tl), by apply decidable.em, cases ex_mid, have n_or : (n = l_hd ∨ n ∈ l_tl), by {apply list.eq_or_mem_of_mem_cons, exact n_in_l}, rewrite if_pos, cases n_or, apply le_of_eq, exact n_or, apply le_trans, apply l_ih, exact n_or, exact ex_mid, exact ex_mid, rewrite if_neg, have n_or : (n = l_hd ∨ n ∈ l_tl), by {apply list.eq_or_mem_of_mem_cons, exact n_in_l}, cases n_or, apply le_trans, have hd_lt : l_hd < nat_list_max l_tl, by {apply iff.mpr, apply lt_iff_not_ge,exact ex_mid}, apply le_of_lt, simp [n_or], exact hd_lt, apply le_of_eq, apply eq.refl, apply l_ih, exact n_or, exact ex_mid end def new_nat_list : list nat → nat := λ l, nat_list_max l + 1 lemma new_is_new : ∀ (l : list nat), ¬ (new_nat_list l ∈ l) := begin simp [new_nat_list], intros l new_in, apply absurd, apply nat.lt_succ_self, exact nat_list_max l, apply not_lt_of_ge, apply nat_list_max_is_max, simp [nat.succ_eq_add_one], exact new_in end mutual def free_vars, free_vars_list with free_vars : (term L) → list nat | (term.var L n) := [n] | (term.apply f l) := free_vars_list l with free_vars_list : list (term L) → list nat | [] := [] | (list.cons t ts) := nat_list_union (free_vars t) (free_vars_list ts) inductive formula : Type | equal : term L → term L → formula | atomic : ∀ r : L.relations, list (term L) → formula | imp : formula → formula → formula | not : formula → formula | forallF : nat → formula → formula open formula def choice: list nat → nat := λ l, 1 + nat_list_max l def sub_list :Type := list (nat × term L) def in_sub_list : nat → sub_list L → Prop | n [] := false | n ((m, _) :: s_tl) := (n=m) ∨ (in_sub_list n s_tl) def find_term_in_sub_list : nat → sub_list L → term L | n [] := term.var L n | n ((m, t) :: sl_tl) := if n =m then t else find_term_in_sub_list n sl_tl def well_formed_sub_list : sub_list L → Prop | [] := true | ((m, t) :: l_tl) := (¬ in_sub_list L m l_tl) ∧ well_formed_term L t ∧ well_formed_sub_list l_tl theorem eq_or_mem_of_mem_cons {n m: nat} {l : sub_list L} {t : term L}: (in_sub_list L n ((m, t) :: l)) → n = m ∨ (in_sub_list L n l) := assume h, h mutual def substitute_term, substitute_term_list with substitute_term : sub_list L → term L → term L | sl (term.var L n) := find_term_in_sub_list L n sl | sl (term.apply f ts) := term.apply f (substitute_term_list sl ts) with substitute_term_list : sub_list L → list (term L ) → list (term L) | sl [] := [] | sl (t :: ts) := (substitute_term sl t) :: (substitute_term_list sl ts) lemma sub_term_list_preserves_length {sl : sub_list L} {ts : list (term L)} : list.length ts = list.length (substitute_term_list L sl ts) := begin induction ts, simp [list.length, substitute_term_list], simp [list.length_cons, substitute_term_list, ts_ih] end lemma rw_well_formed_term_definition : Π {f : L.functions} {ts : list (term L)}, well_formed_term L (term.apply f ts) → L.arityF f = list.length ts ∧ well_formed_term_list L ts := by {simp [well_formed_term], intros f ts, apply id} lemma rw_well_formed_term_list_definition : Π {t ts}, well_formed_term_list L (t :: ts) → well_formed_term L t ∧ (well_formed_term_list L ts) := by {simp [well_formed_term_list], intros t ts wftl,exact wftl} lemma rw_well_formed_sub_list_definition : Π {hd_fst hd_snd tl}, well_formed_sub_list L ((hd_fst, hd_snd) :: tl) → well_formed_term L hd_snd ∧ well_formed_sub_list L tl := by {simp [well_formed_sub_list], intros fst snd tl wfsl, apply and.elim_right, exact wfsl} lemma find_term_well_formed : Π {n : nat} {sl : sub_list L}, well_formed_sub_list L sl → well_formed_term L (find_term_in_sub_list L n sl) := begin intros n sl wfsl, induction sl, simp [find_term_in_sub_list, well_formed_term], cases sl_hd, simp [find_term_in_sub_list], have ex_mid : n = sl_hd_fst ∨ ¬ n = sl_hd_fst, by {apply decidable.em}, cases ex_mid, simp [if_pos ex_mid], apply and.elim_left, apply rw_well_formed_sub_list_definition, exact wfsl, simp [if_neg ex_mid], apply sl_ih, apply and.elim_right, apply rw_well_formed_sub_list_definition, exact wfsl end mutual theorem sub_term_well_formed, sub_term_list_well_formed with sub_term_well_formed : Π {t: term L} {sl : sub_list L}, well_formed_term L t → well_formed_sub_list L sl → well_formed_term L (substitute_term L sl t) | t sl wft wfsl := begin induction t, simp [substitute_term], induction sl, simp [find_term_in_sub_list, well_formed_term], cases sl_hd, simp [find_term_in_sub_list], have ex_mid : t = sl_hd_fst ∨ ¬ t = sl_hd_fst, by apply decidable.em, cases ex_mid, simp [if_pos ex_mid], apply and.elim_left, apply and.elim_right, exact wfsl, simp [if_neg ex_mid], have wf_sl : well_formed_sub_list L sl_tl, by {apply and.elim_right, apply and.elim_right, apply wfsl}, apply sl_ih, exact wf_sl, simp [substitute_term,well_formed_term], apply and.intro, apply eq.trans, apply and.elim_left, apply rw_well_formed_term_definition, exact wft, apply sub_term_list_preserves_length, apply sub_term_list_well_formed, apply and.elim_right, apply rw_well_formed_term_definition, exact wft, exact wfsl end with sub_term_list_well_formed : Π {ts : list (term L)} {sl : sub_list L}, well_formed_term_list L ts → well_formed_sub_list L sl → (well_formed_term_list L (substitute_term_list L sl ts)) | ts sl wftl wfsl := begin repeat {simp [substitute_term_list]}, induction ts, simp [substitute_term_list, substitute_term_list, substitute_term_list], simp [substitute_term_list, well_formed_term_list], apply and.intro, apply sub_term_well_formed, apply and.elim_left, apply rw_well_formed_term_list_definition, exact wftl, exact wfsl, apply ts_ih, apply and.elim_right, apply rw_well_formed_term_list_definition, exact wftl, end definition substitute_formula : list nat → list (term L) → formula L := sorry
87ebb00ca448776bb0bf7c44e3f22d851f2b6319
367134ba5a65885e863bdc4507601606690974c1
/src/data/polynomial/reverse.lean
6e6efd852b3edce0aa4b57d01069f717b7a97938
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
11,293
lean
/- Copyright (c) 2020 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import data.polynomial.erase_lead import data.polynomial.degree /-! # Reverse of a univariate polynomial The main definition is `reverse`. Applying `reverse` to a polynomial `f : polynomial R` produces the polynomial with a reversed list of coefficients, equivalent to `X^f.nat_degree * f(1/X)`. The main result is that `reverse (f * g) = reverse (f) * reverse (g)`, provided the leading coefficients of `f` and `g` do not multiply to zero. -/ namespace polynomial open polynomial finsupp finset open_locale classical section semiring variables {R : Type*} [semiring R] {f : polynomial R} /-- If `i ≤ N`, then `rev_at_fun N i` returns `N - i`, otherwise it returns `i`. This is the map used by the embedding `rev_at`. -/ def rev_at_fun (N i : ℕ) : ℕ := ite (i ≤ N) (N-i) i lemma rev_at_fun_invol {N i : ℕ} : rev_at_fun N (rev_at_fun N i) = i := begin unfold rev_at_fun, split_ifs with h j, { exact nat.sub_sub_self h, }, { exfalso, apply j, exact nat.sub_le N i, }, { refl, }, end lemma rev_at_fun_inj {N : ℕ} : function.injective (rev_at_fun N) := begin intros a b hab, rw [← @rev_at_fun_invol N a, hab, rev_at_fun_invol], end /-- If `i ≤ N`, then `rev_at N i` returns `N - i`, otherwise it returns `i`. Essentially, this embedding is only used for `i ≤ N`. The advantage of `rev_at N i` over `N - i` is that `rev_at` is an involution. -/ def rev_at (N : ℕ) : function.embedding ℕ ℕ := { to_fun := λ i , (ite (i ≤ N) (N-i) i), inj' := rev_at_fun_inj } /-- We prefer to use the bundled `rev_at` over unbundled `rev_at_fun`. -/ @[simp] lemma rev_at_fun_eq (N i : ℕ) : rev_at_fun N i = rev_at N i := rfl @[simp] lemma rev_at_invol {N i : ℕ} : (rev_at N) (rev_at N i) = i := rev_at_fun_invol @[simp] lemma rev_at_le {N i : ℕ} (H : i ≤ N) : rev_at N i = N - i := if_pos H lemma rev_at_add {N O n o : ℕ} (hn : n ≤ N) (ho : o ≤ O) : rev_at (N + O) (n + o) = rev_at N n + rev_at O o := begin rcases nat.le.dest hn with ⟨n', rfl⟩, rcases nat.le.dest ho with ⟨o', rfl⟩, repeat { rw rev_at_le (le_add_right rfl.le) }, rw [add_assoc, add_left_comm n' o, ← add_assoc, rev_at_le (le_add_right rfl.le)], repeat {rw nat.add_sub_cancel_left}, end /-- `reflect N f` is the polynomial such that `(reflect N f).coeff i = f.coeff (rev_at N i)`. In other words, the terms with exponent `[0, ..., N]` now have exponent `[N, ..., 0]`. In practice, `reflect` is only used when `N` is at least as large as the degree of `f`. Eventually, it will be used with `N` exactly equal to the degree of `f`. -/ noncomputable def reflect (N : ℕ) (f : polynomial R) : polynomial R := finsupp.emb_domain (rev_at N) f lemma reflect_support (N : ℕ) (f : polynomial R) : (reflect N f).support = image (rev_at N) f.support := begin ext1, rw [reflect, mem_image, support, support, support_emb_domain, mem_map], end @[simp] lemma coeff_reflect (N : ℕ) (f : polynomial R) (i : ℕ) : coeff (reflect N f) i = f.coeff (rev_at N i) := calc finsupp.emb_domain (rev_at N) f i = finsupp.emb_domain (rev_at N) f (rev_at N (rev_at N i)) : by rw rev_at_invol ... = f.coeff (rev_at N i) : finsupp.emb_domain_apply _ _ _ @[simp] lemma reflect_zero {N : ℕ} : reflect N (0 : polynomial R) = 0 := rfl @[simp] lemma reflect_eq_zero_iff {N : ℕ} {f : polynomial R} : reflect N (f : polynomial R) = 0 ↔ f = 0 := by simp [reflect] @[simp] lemma reflect_add (f g : polynomial R) (N : ℕ) : reflect N (f + g) = reflect N f + reflect N g := by { ext, simp only [coeff_add, coeff_reflect], } @[simp] lemma reflect_C_mul (f : polynomial R) (r : R) (N : ℕ) : reflect N (C r * f) = C r * (reflect N f) := by { ext, simp only [coeff_reflect, coeff_C_mul], } @[simp] lemma reflect_C_mul_X_pow (N n : ℕ) {c : R} : reflect N (C c * X ^ n) = C c * X ^ (rev_at N n) := begin ext, rw [reflect_C_mul, coeff_C_mul, coeff_C_mul, coeff_X_pow, coeff_reflect], split_ifs with h j, { rw [h, rev_at_invol, coeff_X_pow_self], }, { rw [not_mem_support_iff.mp], intro a, rw [← one_mul (X ^ n), ← C_1] at a, apply h, rw [← (mem_support_C_mul_X_pow a), rev_at_invol], }, end @[simp] lemma reflect_monomial (N n : ℕ) : reflect N ((X : polynomial R) ^ n) = X ^ (rev_at N n) := by rw [← one_mul (X ^ n), ← one_mul (X ^ (rev_at N n)), ← C_1, reflect_C_mul_X_pow] lemma reflect_mul_induction (cf cg : ℕ) : ∀ N O : ℕ, ∀ f g : polynomial R, f.support.card ≤ cf.succ → g.support.card ≤ cg.succ → f.nat_degree ≤ N → g.nat_degree ≤ O → (reflect (N + O) (f * g)) = (reflect N f) * (reflect O g) := begin induction cf with cf hcf, --first induction (left): base case { induction cg with cg hcg, -- second induction (right): base case { intros N O f g Cf Cg Nf Og, rw [← C_mul_X_pow_eq_self Cf, ← C_mul_X_pow_eq_self Cg], simp only [mul_assoc, X_pow_mul, ← pow_add X, reflect_C_mul, reflect_monomial, add_comm, rev_at_add Nf Og] }, -- second induction (right): induction step { intros N O f g Cf Cg Nf Og, by_cases g0 : g = 0, { rw [g0, reflect_zero, mul_zero, mul_zero, reflect_zero], }, rw [← erase_lead_add_C_mul_X_pow g, mul_add, reflect_add, reflect_add, mul_add, hcg, hcg]; try { assumption }, { exact le_add_left card_support_C_mul_X_pow_le_one }, { exact (le_trans (nat_degree_C_mul_X_pow_le g.leading_coeff g.nat_degree) Og) }, { exact nat.lt_succ_iff.mp (gt_of_ge_of_gt Cg (erase_lead_support_card_lt g0)) }, { exact le_trans erase_lead_nat_degree_le Og } } }, --first induction (left): induction step { intros N O f g Cf Cg Nf Og, by_cases f0 : f = 0, { rw [f0, reflect_zero, zero_mul, zero_mul, reflect_zero], }, rw [← erase_lead_add_C_mul_X_pow f, add_mul, reflect_add, reflect_add, add_mul, hcf, hcf]; try { assumption }, { exact le_add_left card_support_C_mul_X_pow_le_one }, { exact (le_trans (nat_degree_C_mul_X_pow_le f.leading_coeff f.nat_degree) Nf) }, { exact nat.lt_succ_iff.mp (gt_of_ge_of_gt Cf (erase_lead_support_card_lt f0)) }, { exact (le_trans erase_lead_nat_degree_le Nf) } }, end @[simp] theorem reflect_mul (f g : polynomial R) {F G : ℕ} (Ff : f.nat_degree ≤ F) (Gg : g.nat_degree ≤ G) : reflect (F + G) (f * g) = reflect F f * reflect G g := reflect_mul_induction _ _ F G f g f.support.card.le_succ g.support.card.le_succ Ff Gg /-- The reverse of a polynomial f is the polynomial obtained by "reading f backwards". Even though this is not the actual definition, reverse f = f (1/X) * X ^ f.nat_degree. -/ noncomputable def reverse (f : polynomial R) : polynomial R := reflect f.nat_degree f lemma coeff_reverse (f : polynomial R) (n : ℕ) : f.reverse.coeff n = f.coeff (rev_at f.nat_degree n) := by rw [reverse, coeff_reflect] @[simp] lemma coeff_zero_reverse (f : polynomial R) : coeff (reverse f) 0 = leading_coeff f := by rw [coeff_reverse, rev_at_le (zero_le f.nat_degree), nat.sub_zero, leading_coeff] @[simp] lemma reverse_zero : reverse (0 : polynomial R) = 0 := rfl @[simp] lemma reverse_eq_zero : f.reverse = 0 ↔ f = 0 := begin split, { rw [polynomial.ext_iff, polynomial.ext_iff], intros h n, specialize h (rev_at f.nat_degree n), rwa [coeff_zero, coeff_reverse, rev_at_invol] at h }, { intro h, rw [h, reverse_zero] }, end lemma reverse_nat_degree_le (f : polynomial R) : f.reverse.nat_degree ≤ f.nat_degree := begin rw [nat_degree_le_iff_degree_le, degree_le_iff_coeff_zero], intros n hn, rw with_bot.coe_lt_coe at hn, rw [coeff_reverse, rev_at, function.embedding.coe_fn_mk, if_neg (not_le_of_gt hn), coeff_eq_zero_of_nat_degree_lt hn], end lemma nat_degree_eq_reverse_nat_degree_add_nat_trailing_degree (f : polynomial R) : f.nat_degree = f.reverse.nat_degree + f.nat_trailing_degree := begin by_cases hf : f = 0, { rw [hf, reverse_zero, nat_degree_zero, nat_trailing_degree_zero] }, apply le_antisymm, { apply nat.le_add_of_sub_le_right, apply le_nat_degree_of_ne_zero, rw [reverse, coeff_reflect, ←rev_at_le f.nat_trailing_degree_le_nat_degree, rev_at_invol], exact trailing_coeff_nonzero_iff_nonzero.mpr hf }, { rw ← nat.le_sub_left_iff_add_le f.reverse_nat_degree_le, apply nat_trailing_degree_le_of_ne_zero, have key := mt leading_coeff_eq_zero.mp (mt reverse_eq_zero.mp hf), rwa [leading_coeff, coeff_reverse, rev_at_le f.reverse_nat_degree_le] at key }, end lemma reverse_nat_degree (f : polynomial R) : f.reverse.nat_degree = f.nat_degree - f.nat_trailing_degree := by rw [f.nat_degree_eq_reverse_nat_degree_add_nat_trailing_degree, nat.add_sub_cancel] lemma reverse_leading_coeff (f : polynomial R) : f.reverse.leading_coeff = f.trailing_coeff := by rw [leading_coeff, reverse_nat_degree, ←rev_at_le f.nat_trailing_degree_le_nat_degree, coeff_reverse, rev_at_invol, trailing_coeff] lemma reverse_nat_trailing_degree (f : polynomial R) : f.reverse.nat_trailing_degree = 0 := begin by_cases hf : f = 0, { rw [hf, reverse_zero, nat_trailing_degree_zero] }, { rw ← nat.le_zero_iff, apply nat_trailing_degree_le_of_ne_zero, rw [coeff_zero_reverse], exact mt leading_coeff_eq_zero.mp hf }, end lemma reverse_trailing_coeff (f : polynomial R) : f.reverse.trailing_coeff = f.leading_coeff := by rw [trailing_coeff, reverse_nat_trailing_degree, coeff_zero_reverse] theorem reverse_mul {f g : polynomial R} (fg : f.leading_coeff * g.leading_coeff ≠ 0) : reverse (f * g) = reverse f * reverse g := begin unfold reverse, rw [nat_degree_mul' fg, reflect_mul f g rfl.le rfl.le], end @[simp] lemma reverse_mul_of_domain {R : Type*} [domain R] (f g : polynomial R) : reverse (f * g) = reverse f * reverse g := begin by_cases f0 : f=0, { simp only [f0, zero_mul, reverse_zero], }, by_cases g0 : g=0, { rw [g0, mul_zero, reverse_zero, mul_zero], }, simp [reverse_mul, *], end lemma trailing_coeff_mul {R : Type*} [integral_domain R] (p q : polynomial R) : (p * q).trailing_coeff = p.trailing_coeff * q.trailing_coeff := by rw [←reverse_leading_coeff, reverse_mul_of_domain, leading_coeff_mul, reverse_leading_coeff, reverse_leading_coeff] @[simp] lemma coeff_one_reverse (f : polynomial R) : coeff (reverse f) 1 = next_coeff f := begin rw [coeff_reverse, next_coeff], split_ifs with hf, { have : coeff f 1 = 0 := coeff_eq_zero_of_nat_degree_lt (by simp only [hf, zero_lt_one]), simp [*, rev_at] }, { rw rev_at_le, exact nat.succ_le_iff.2 (pos_iff_ne_zero.2 hf) } end end semiring section ring variables {R : Type*} [ring R] @[simp] lemma reflect_neg (f : polynomial R) (N : ℕ) : reflect N (- f) = - reflect N f := by rw [neg_eq_neg_one_mul, ←C_1, ←C_neg, reflect_C_mul, C_neg, C_1, ←neg_eq_neg_one_mul] @[simp] lemma reflect_sub (f g : polynomial R) (N : ℕ) : reflect N (f - g) = reflect N f - reflect N g := by rw [sub_eq_add_neg, sub_eq_add_neg, reflect_add, reflect_neg] @[simp] lemma reverse_neg (f : polynomial R) : reverse (- f) = - reverse f := by rw [reverse, reverse, reflect_neg, nat_degree_neg] end ring end polynomial
db371086f2b6401f11fce125951ed6eea008bbd5
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1549.lean
faf97f20c19bc647e02aeff12d20ee3d47a26214
[ "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
688
lean
theorem not_mem_nil (a : Nat) : ¬ a ∈ [] := fun x => nomatch x theorem forall_prop_of_false {p : Prop} {q : p → Prop} (hn : ¬ p) : (∀ h' : p, q h') ↔ True := sorry example (R : Nat → Prop) : (∀ (a' : Nat), a' ∈ [] → R a') := by simp only [forall_prop_of_false (not_mem_nil _)] exact fun _ => True.intro def Not.elim {α : Sort _} (H1 : ¬a) (H2 : a) : α := absurd H2 H1 theorem iff_of_true (ha : a) (hb : b) : a ↔ b := ⟨fun _ => hb, fun _ => ha⟩ theorem iff_true_intro (h : a) : a ↔ True := iff_of_true h ⟨⟩ example {P : Prop} : ∀ (x : Nat) (_ : x ∈ []), P := by simp only [forall_prop_of_false (not_mem_nil _)] exact fun _ => True.intro
2a820778ac18f25b4ba5307f7f1f480bb90c42c6
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/num_tst2.lean
332861f403f232cb6444fd48011cecf8b8a0fb82
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
175
lean
import num tactic using num variables a b : num add_rewrite exp_zero exp_succ one_eq_succ_zero theorem T1 : b * exp a (succ (succ (succ one))) = a * a * a * a * b := by simp
dfb5a507d8d11894c86b84153c8f5270faef7211
94637389e03c919023691dcd05bd4411b1034aa5
/src/zzz_junk/lang/var_test.lean
ef5d71e0acab1ac38fab73bc2b3af70960ea09e8
[]
no_license
kevinsullivan/complogic-s21
7c4eef2105abad899e46502270d9829d913e8afc
99039501b770248c8ceb39890be5dfe129dc1082
refs/heads/master
1,682,985,669,944
1,621,126,241,000
1,621,126,241,000
335,706,272
0
38
null
1,618,325,669,000
1,612,374,118,000
Lean
UTF-8
Lean
false
false
240
lean
import .var -- some arithmetic variables def X : var nat := var.mk 0 def Y : var nat := var.mk 1 def Z : var nat := var.mk 2 -- some Boolean variables def P : var bool := var.mk 0 def Q : var bool := var.mk 1 def R : var bool := var.mk 2
c13b912acdfe648467c748c2102c78bdb07ac1ee
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/rewriter6.lean
75c3277d6e95d8345c8b92dcefdabf122feeccea
[ "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
349
lean
import data.int open int theorem abs_thm1 (a b : int) : a - b = a + -b := by rewrite ↑sub -- unfold sub theorem abs_thm2 (a b : int) : a - b = a + -b := by rewrite ^sub -- unfold sub definition double (x : int) := x + x theorem double_zero (x : int) : double (0 + x) = (1 + 1)*x := by rewrite [↑double, zero_add, mul.right_distrib, one_mul]
de7090b1a9bc82ccfeebc9468eaa0a8f80bcf2f8
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/lua16.lean
072a3b92c50c57f787ac5252eb63c46ec9e1fcfb
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
125
lean
import Int. variables a b : Int (* local ios = io_state() ios:print(parse_lean("a + b")) print(parse_lean("a + b")) *)
d852a05b31b487280cdaa90491b14590ce5b79c2
618003631150032a5676f229d13a079ac875ff77
/src/linear_algebra/basic.lean
3867f0b6f5d4102d018a915c4cfab01e84353bdf
[ "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
88,358
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, Yury Kudryashov -/ 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 β] [semiring R] [add_comm_monoid M] [semimodule 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 /-! ### Properties of linear maps -/ namespace linear_map section add_comm_monoid variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] [semimodule 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 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 monoid. -/ instance : add_comm_monoid (M →ₗ[R] M₂) := by refine {zero := 0, add := (+), ..}; intros; ext; simp [add_comm, add_left_comm] instance linear_map_apply_is_add_monoid_hom (a : M) : is_add_monoid_hom (λ f : M →ₗ[R] M₂, f a) := { map_add := λ f g, linear_map.add_apply f g a, map_zero := rfl } 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 /-- `λ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⟩ _ _ instance : monoid (M →ₗ[R] M) := by refine {mul := (*), one := 1, ..}; { intros, apply linear_map.ext, simp {proj := ff} } 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 add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] [add_comm_group M₄] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] [semimodule R M₄] variables (f g : M →ₗ[R] M₂) include R /-- 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 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_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 } @[simp] lemma sub_apply (x : M) : (f - g) x = f x - g x := rfl end add_comm_group section comm_semiring variables [comm_semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [semimodule R M] [semimodule R M₂] [semimodule 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 : semimodule R (M →ₗ[R] M₂) := 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 end comm_semiring section ring instance endomorphism_ring [ring R] [add_comm_group M] [semimodule R M] : ring (M →ₗ[R] M) := by refine {mul := (*), one := 1, ..linear_map.add_comm_group, ..}; { intros, apply linear_map.ext, simp {proj := ff} } end ring section comm_ring variables [comm_ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] /-- 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 /-! ### Properties of submodules -/ namespace submodule section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [semimodule R M] [semimodule R M₂] [semimodule 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) := { le := λ p p', ∀ ⦃x⦄, x ∈ p → x ∈ p', ..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 : 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 theorem mem_right_iff_eq_zero_of_disjoint {p p' : submodule R M} (h : disjoint p p') {x : p} : (x:M) ∈ p' ↔ x = 0 := ⟨λ hx, coe_eq_zero.1 $ disjoint_def.1 h x x.2 hx, λ h, h.symm ▸ p'.zero_mem⟩ theorem mem_left_iff_eq_zero_of_disjoint {p p' : submodule R M} (h : disjoint p p') {x : p'} : (x:M) ∈ p ↔ x = 0 := ⟨λ hx, coe_eq_zero.1 $ disjoint_def.1 h x hx x.2, λ h, h.symm ▸ p.zero_mem⟩ /-- 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 [add_assoc]; 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 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 end add_comm_monoid variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] variables (p p' : submodule R M) (q q' : submodule R M₂) variables {r : R} {x y : M} open set lemma mem_span_insert' {y} {s : set M} : 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, add_assoc]⟩ }, { rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp [add_comm, add_left_comm]⟩ } 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, add_assoc] 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 : semimodule R (quotient p) := semimodule.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 /-! ### Properties of linear maps -/ namespace linear_map section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] include R open submodule @[simp] lemma finsupp_sum {γ} [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 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] 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 = ⊥ ↔ (∀ 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] @[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 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 @[simp] 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 lemma range_prod_le (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : range (prod f g) ≤ (range f).prod (range g) := begin simp only [le_def', prod_apply, mem_range, mem_coe, mem_prod, exists_imp_distrib], rintro _ x rfl, exact ⟨⟨x, rfl⟩, ⟨x, rfl⟩⟩ end theorem ker_eq_bot_of_injective {f : M →ₗ[R] M₂} (hf : injective f) : ker f = ⊥ := begin have : disjoint ⊤ f.ker, by { rw [disjoint_ker, ← map_zero f], exact λ x hx H, hf H }, simpa [disjoint] end end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] include R open submodule 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 rw [comap_map_eq, sup_of_le_left h] theorem map_le_map_iff (f : M →ₗ[R] M₂) {p p'} : map f p ≤ map f p' ↔ p ≤ p' ⊔ ker f := by rw [map_le_iff_le_comap, comap_map_eq] theorem map_le_map_iff' {f : M →ₗ[R] M₂} (hf : ker f = ⊥) {p p'} : map f p ≤ map f p' ↔ p ≤ p' := by rw [map_le_map_iff, hf, sup_bot_eq] 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 map_eq_top_iff {f : M →ₗ[R] M₂} (hf : range f = ⊤) {p : submodule R M} : p.map f = ⊤ ↔ p ⊔ f.ker = ⊤ := by simp_rw [← top_le_iff, ← hf, range, map_le_map_iff] end add_comm_group section ring variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] include R open submodule 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 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) theorem ker_eq_bot {f : M →ₗ[R] M₂} : ker f = ⊥ ↔ injective f := by simpa [disjoint] using @disjoint_ker' _ _ _ _ _ _ _ _ f ⊤ /-- If the union of the kernels `ker f` and `ker g` spans the domain, then the range of `prod f g` is equal to the product of `range f` and `range g`. -/ lemma range_prod_eq {f : M →ₗ[R] M₂} {g : M →ₗ[R] M₃} (h : ker f ⊔ ker g = ⊤) : range (prod f g) = (range f).prod (range g) := begin refine le_antisymm (f.range_prod_le g) _, simp only [le_def', prod_apply, mem_range, mem_coe, mem_prod, exists_imp_distrib, and_imp, prod.forall], rintros _ _ x rfl y rfl, simp only [prod.mk.inj_iff, ← sub_mem_ker_iff], have : y - x ∈ ker f ⊔ ker g, { simp only [h, mem_top] }, rcases mem_sup.1 this with ⟨x', hx', y', hy', H⟩, refine ⟨x' + x, _, _⟩, { rwa add_sub_cancel }, { rwa [← eq_sub_iff_add_eq.1 H, add_sub_add_right_eq_sub, ← neg_mem_iff, neg_sub, add_sub_cancel'] } end end ring section field 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 field end linear_map lemma submodule.sup_eq_range [semiring R] [add_comm_monoid M] [semimodule 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 is_linear_map lemma is_linear_map_add [semiring R] [add_comm_monoid M] [semimodule 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*} [semiring R] [add_comm_group M] [semimodule 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 section add_comm_monoid variables {T : semiring R} [add_comm_monoid M] [add_comm_monoid M₂] [semimodule R M] [semimodule 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_of_injective $ λ 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] 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] @[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] end add_comm_monoid section ring variables {T : ring R} [add_comm_group M] [add_comm_group M₂] [semimodule R M] [semimodule R M₂] variables (p p' : submodule R M) (q : submodule R M₂) include T open linear_map 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, disjoint] /-- 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 p)).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 /-- 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] @[simp] theorem map_mkq_eq_top : map p.mkq p' = ⊤ ↔ p ⊔ p' = ⊤ := by simp only [map_eq_top_iff p.range_mkq, sup_comm, ker_mkq] /-- 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 ring end submodule @[simp] lemma linear_map.range_range_restrict [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [semimodule R M] [semimodule R M₂] (f : M →ₗ[R] M₂) : f.range_restrict.range = ⊤ := by simp [f.range_cod_restrict _] /-! ### Linear equivalences -/ 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) [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [semimodule R M] [semimodule 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 add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] section variables [semimodule R M] [semimodule R M₂] [semimodule 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 {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule 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 : ∀ x, e x = e' x) : e = e' := to_equiv_injective (equiv.ext h) end section variables (M R) /-- The identity map is a linear equivalence. -/ @[refl] def refl [semimodule R M] : M ≃ₗ[R] M := { .. linear_map.id, .. equiv.refl M } end @[simp] lemma refl_apply [semimodule 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 {semimodule_M₃ : semimodule 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_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 {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables {semimodule_M₃ : semimodule R M₃} {semimodule_M₄ : semimodule 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 end prod section variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M) (e : M ≃ₗ[R] M₂) variables (p q : submodule R M) /-- Linear equivalence between two equal submodules. -/ def of_eq (h : p = q) : p ≃ₗ[R] q := { smul := λ _ _, rfl, add := λ _ _, rfl, .. equiv.set.of_eq (congr_arg _ h) } variables {p q} @[simp] lemma coe_of_eq_apply (h : p = q) (x : p) : (of_eq p q h x : M) = x := rfl @[simp] lemma of_eq_symm (h : p = q) : (of_eq p q h).symm = of_eq q p h.symm := rfl variable (p) /-- 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 coe_of_top_symm_apply {h} (x : M) : ((of_top p h).symm x : M) = x := rfl theorem of_top_symm_apply {h} (x : M) : (of_top p h).symm x = ⟨x, h.symm ▸ trivial⟩ := rfl /-- 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 @[simp] protected theorem range : (e : M →ₗ[R] M₂).range = ⊤ := linear_map.range_eq_top.2 e.to_equiv.surjective lemma eq_bot_of_equiv [semimodule 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 @[simp] protected theorem ker : (e : M →ₗ[R] M₂).ker = ⊥ := linear_map.ker_eq_bot_of_injective e.to_equiv.injective end end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] [add_comm_group M₄] variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables {semimodule_M₃ : semimodule R M₃} {semimodule_M₄ : semimodule R M₄} variables (e e₁ : M ≃ₗ[R] M₂) (e₂ : M₃ ≃ₗ[R] M₄) @[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 /-- 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 add_comm_group section ring variables [ring R] [add_comm_group M] [add_comm_group M₂] variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables (f : M →ₗ[R] M₂) (e : M ≃ₗ[R] M₂) /-- An `injective` linear map `f : M →ₗ[R] M₂` defines a linear equivalence between `M` and `f.range`. -/ noncomputable def of_injective (h : f.ker = ⊥) : M ≃ₗ[R] f.range := { .. (equiv.set.range f $ linear_map.ker_eq_bot.1 h).trans (equiv.set.of_eq f.range_coe.symm), .. f.cod_restrict f.range (λ x, f.mem_range_self x) } /-- 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₂ := (of_injective f hf₁).trans (of_top _ hf₂) @[simp] theorem of_bijective_apply {hf₁ hf₂} (x : M) : of_bijective f hf₁ hf₂ x = f x := rfl end ring section comm_ring variables [comm_ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] 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 [ring R] [add_comm_group M] [module R M] variables (p : submodule R M) open linear_map /-- If `p = ⊥`, then `M / p ≃ₗ[R] M`. -/ def quot_equiv_of_eq_bot (hp : p = ⊥) : p.quotient ≃ₗ[R] M := linear_equiv.of_linear (p.liftq id $ hp.symm ▸ bot_le) p.mkq (liftq_mkq _ _ _) $ p.quot_hom_ext $ λ x, rfl @[simp] lemma quot_equiv_of_eq_bot_apply_mk (hp : p = ⊥) (x : M) : p.quot_equiv_of_eq_bot hp (quotient.mk x) = x := rfl @[simp] lemma quot_equiv_of_eq_bot_symm_apply (hp : p = ⊥) (x : M) : (p.quot_equiv_of_eq_bot hp).symm x = quotient.mk x := rfl @[simp] lemma coe_quot_equiv_of_eq_bot_symm (hp : p = ⊥) : ((p.quot_equiv_of_eq_bot hp).symm : M →ₗ[R] p.quotient) = p.mkq := rfl end submodule 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 [semiring R] [add_comm_monoid M] [semimodule R M] [add_comm_monoid M₂] [semimodule 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 open submodule section isomorphism_laws 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 := (linear_equiv.of_injective (f.ker.liftq f $ le_refl _) $ submodule.ker_liftq_eq_bot _ _ _ (le_refl f.ker)).trans (linear_equiv.of_eq _ _ $ submodule.range_liftq _ _ _) @[simp] lemma quot_ker_equiv_range_apply_mk (x : M) : (f.quot_ker_equiv_range (submodule.quotient.mk x) : M₂) = f x := rfl @[simp] lemma quot_ker_equiv_range_symm_apply_image (x : M) (h : f x ∈ f.range) : f.quot_ker_equiv_range.symm ⟨f x, h⟩ = f.ker.mkq x := f.quot_ker_equiv_range.symm_apply_apply (f.ker.mkq x) /-- 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 := linear_equiv.of_bijective (quotient_inf_to_sup_quotient p p') begin rw [quotient_inf_to_sup_quotient, ker_liftq_eq_bot], rw [ker_comp, ker_mkq], exact λ ⟨x, hx1⟩ hx2, ⟨hx1, hx2⟩ end begin rw [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 @[simp] lemma coe_quotient_inf_to_sup_quotient (p p' : submodule R M) : ⇑(quotient_inf_to_sup_quotient p p') = quotient_inf_equiv_sup_quotient p p' := rfl @[simp] lemma quotient_inf_equiv_sup_quotient_apply_mk (p p' : submodule R M) (x : p) : quotient_inf_equiv_sup_quotient p p' (submodule.quotient.mk x) = submodule.quotient.mk (of_le (le_sup_left : p ≤ p ⊔ p') x) := rfl lemma quotient_inf_equiv_sup_quotient_symm_apply_left (p p' : submodule R M) (x : p ⊔ p') (hx : (x:M) ∈ p) : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = submodule.quotient.mk ⟨x, hx⟩ := (linear_equiv.symm_apply_eq _).2 $ by simp [of_le_apply] @[simp] lemma quotient_inf_equiv_sup_quotient_symm_apply_eq_zero_iff {p p' : submodule R M} {x : p ⊔ p'} : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = 0 ↔ (x:M) ∈ p' := (linear_equiv.symm_apply_eq _).trans $ by simp [of_le_apply] lemma quotient_inf_equiv_sup_quotient_symm_apply_right (p p' : submodule R M) {x : p ⊔ p'} (hx : (x:M) ∈ p') : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = 0 := quotient_inf_equiv_sup_quotient_symm_apply_eq_zero_iff.2 hx end isomorphism_laws section prod lemma is_linear_map_prod_iso {R M M₂ M₃ : Type*} [comm_semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_group M₃] [semimodule R M] [semimodule R M₂] [semimodule 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 [semiring R] [add_comm_monoid M₂] [semimodule R M₂] [add_comm_monoid M₃] [semimodule R M₃] {φ : ι → Type i} [∀i, add_comm_monoid (φ i)] [∀i, semimodule 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_of_injective $ 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 universe i variables [semiring R] [add_comm_monoid M] [semimodule R M] 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
35f488d137a6660fff3f60f6ee7cbea21358df7a
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/section5.lean
223aeacfab330cd18f214b0e3174e3e0b9f33b39
[ "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
256
lean
section foo parameter A : Type variable a : A definition foo := a #check foo class point := (x : A) (y : A) end foo #check foo attribute [instance] definition point_nat : point nat := point.mk nat.zero nat.zero #print classes #check point
7a7eb03b94c19f5230d245c671a208f2e7251b7d
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/run/core.lean
70002f85170e83958f258581d05eae122a4f9a0f
[ "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
270
lean
import Lean.CoreM open Lean open Lean.Core def f : CoreM Nat := do let env ← getEnv; let cinfo ← getConstInfo `Nat.add; trace! `Elab "trace message"; IO.println $ toString cinfo.type; IO.println "testing..."; pure 10; #eval f set_option trace.Elab true #eval f
71dbe7d1717009ce494341c9642ba15b2b39a738
f618aea02cb4104ad34ecf3b9713065cc0d06103
/src/data/real/hyperreal.lean
fec81acc5be2619a2892c67deb31b21461153988
[ "Apache-2.0" ]
permissive
joehendrix/mathlib
84b6603f6be88a7e4d62f5b1b0cbb523bb82b9a5
c15eab34ad754f9ecd738525cb8b5a870e834ddc
refs/heads/master
1,589,606,591,630
1,555,946,393,000
1,555,946,393,000
182,813,854
0
0
null
1,555,946,309,000
1,555,946,308,000
null
UTF-8
Lean
false
false
4,687
lean
/- Copyright (c) 2019 Abhimanyu Pallavi Sudhir. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Abhimanyu Pallavi Sudhir Construction of the hyperreal numbers as an ultraproduct of real sequences. -/ import data.real.basic algebra.field order.filter.filter_product analysis.specific_limits local attribute [instance] classical.prop_decidable local attribute [instance, priority 0] nat.cast_coe open filter filter.filter_product /-- Hyperreal numbers on the ultrafilter extending the cofinite filter -/ @[reducible] def hyperreal := filter.filterprod ℝ (@hyperfilter ℕ) namespace hyperreal notation `ℝ*` := hyperreal private def U := is_ultrafilter_hyperfilter set.infinite_univ_nat noncomputable instance : discrete_linear_ordered_field ℝ* := filter_product.discrete_linear_ordered_field U /-- A sample infinitesimal hyperreal-/ noncomputable def epsilon : ℝ* := of_seq (λ n, n⁻¹) /-- A sample infinite hyperreal-/ noncomputable def omega : ℝ* := of_seq (λ n, n) local notation `ε` := epsilon local notation `ω` := omega theorem epsilon_eq_inv_omega : ε = ω⁻¹ := rfl theorem inv_epsilon_eq_omega : ε⁻¹ = ω := @inv_inv' _ _ ω lemma epsilon_pos : 0 < ε := have h0' : {n : ℕ | ¬ n > 0} = {0} := by simp only [not_lt, (set.set_of_eq_eq_singleton).symm]; ext; exact nat.le_zero_iff, begin rw lt_def U, show {i : ℕ | (0 : ℝ) < i⁻¹} ∈ hyperfilter.sets, simp only [inv_pos', nat.cast_pos], exact mem_hyperfilter_of_finite_compl set.infinite_univ_nat (by convert set.finite_singleton _), end lemma epsilon_ne_zero : ε ≠ 0 := ne_of_gt epsilon_pos lemma omega_pos : 0 < ω := by rw ←inv_epsilon_eq_omega; exact inv_pos epsilon_pos lemma omega_ne_zero : ω ≠ 0 := ne_of_gt omega_pos theorem epsilon_mul_omega : ε * ω = 1 := @inv_mul_cancel _ _ ω omega_ne_zero lemma lt_of_tendsto_zero_of_pos {f : ℕ → ℝ} (hf : tendsto f at_top (nhds 0)) : ∀ {r : ℝ}, r > 0 → of_seq f < (r : ℝ*) := begin simp only [metric.tendsto_at_top, dist_zero_right, norm, lt_def U] at hf ⊢, intros r hr, cases hf r hr with N hf', have hs : -{i : ℕ | f i < r} ⊆ {i : ℕ | i ≤ N} := λ i hi1, le_of_lt (by simp only [lt_iff_not_ge]; exact λ hi2, hi1 (lt_of_le_of_lt (le_abs_self _) (hf' i hi2)) : i < N), exact mem_hyperfilter_of_finite_compl set.infinite_univ_nat (set.finite_subset (set.finite_le_nat N) hs) end lemma neg_lt_of_tendsto_zero_of_neg {f : ℕ → ℝ} (hf : tendsto f at_top (nhds 0)) : ∀ {r : ℝ}, r > 0 → (-r : ℝ*) < of_seq f := λ r hr, have hg : _ := tendsto_neg hf, neg_lt_of_neg_lt (by rw [neg_zero] at hg; exact lt_of_tendsto_zero_of_pos hg hr) lemma gt_of_tendsto_zero_of_neg {f : ℕ → ℝ} (hf : tendsto f at_top (nhds 0)) : ∀ {r : ℝ}, r < 0 → (r : ℝ*) < of_seq f := λ r hr, have hn : (r : ℝ*) = -↑(-r) := by rw [←of_eq_coe, ←of_eq_coe, of_neg, neg_neg], by rw hn; exact neg_lt_of_tendsto_zero_of_neg hf (neg_pos.mpr hr) lemma epsilon_lt_pos (x : ℝ) : x > 0 → ε < of x := lt_of_tendsto_zero_of_pos tendsto_inverse_at_top_nhds_0_nat /-- Standard part predicate -/ def is_st (x : ℝ*) (r : ℝ) := ∀ δ : ℝ, δ > 0 → (r - δ : ℝ*) < x ∧ x < r + δ /-- Standard part function: like a "floor" to ℝ instead of ℤ -/ noncomputable def st : ℝ* → ℝ := λ x, dite (∃ r, is_st x r) classical.some $ λ h, 0 private lemma is_st_unique_1 (x : ℝ*) (r s : ℝ) (hr : is_st x r) (hs : is_st x s) (hrs : r < s) : false := have hrs' : _ := half_pos $ sub_pos_of_lt hrs, have hr' : _ := (hr _ hrs').2, have hs' : _ := (hs _ hrs').1, have h : s + -((s - r) / 2) = r + (s - r) / 2 := by linarith, by simp only [(of_eq_coe _).symm, sub_eq_add_neg (of s), (of_neg _).symm, (of_add _ _).symm, h] at hr' hs'; exact not_lt_of_lt hs' hr' theorem is_st_unique (x : ℝ*) (r s : ℝ) (hr : is_st x r) (hs : is_st x s) : r = s := begin rcases lt_trichotomy r s with h | h | h, { exact false.elim (is_st_unique_1 x r s hr hs h) }, { exact h }, { exact false.elim (is_st_unique_1 x s r hs hr h) } end /-- A hyperreal number is infinitesimal if its standard part is 0 -/ def infinitesimal (x : ℝ*) := is_st x 0 theorem infinitesimal_of_tendsto_zero {f : ℕ → ℝ} (hf : tendsto f at_top (nhds 0)) : infinitesimal (of_seq f) := λ d hd, by rw [←of_eq_coe, ←of_eq_coe, sub_eq_add_neg, ←of_neg, ←of_add, ←of_add, zero_add, zero_add, of_eq_coe, of_eq_coe]; exact ⟨neg_lt_of_tendsto_zero_of_neg hf hd, lt_of_tendsto_zero_of_pos hf hd⟩ theorem infinitesimal_epsilon : infinitesimal ε := infinitesimal_of_tendsto_zero tendsto_inverse_at_top_nhds_0_nat end hyperreal
8e8c5159231035d7a2517996b1a531b9e5ae9f24
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/normed_space/banach_auto.lean
3f993d5b282faaef0520ca5d533391b4b3a92a2f
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
6,599
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.metric_space.baire import Mathlib.analysis.normed_space.operator_norm import Mathlib.PostPort universes u_1 u_2 u_3 namespace Mathlib /-! # Banach open mapping theorem This file contains the Banach open mapping theorem, i.e., the fact that a bijective bounded linear map between Banach spaces has a bounded inverse. -/ /-- First step of the proof of the Banach open mapping theorem (using completeness of `F`): by Baire's theorem, there exists a ball in `E` whose image closure has nonempty interior. Rescaling everything, it follows that any `y ∈ F` is arbitrarily well approached by images of elements of norm at most `C * ∥y∥`. For further use, we will only need such an element whose image is within distance `∥y∥/2` of `y`, to apply an iterative process. -/ theorem exists_approx_preimage_norm_le {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : continuous_linear_map 𝕜 E F) [complete_space F] (surj : function.surjective ⇑f) : ∃ (C : ℝ), ∃ (H : C ≥ 0), ∀ (y : F), ∃ (x : E), dist (coe_fn f x) y ≤ 1 / bit0 1 * norm y ∧ norm x ≤ C * norm y := sorry /-- The Banach open mapping theorem: if a bounded linear map between Banach spaces is onto, then any point has a preimage with controlled norm. -/ theorem exists_preimage_norm_le {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : continuous_linear_map 𝕜 E F) [complete_space F] [complete_space E] (surj : function.surjective ⇑f) : ∃ (C : ℝ), ∃ (H : C > 0), ∀ (y : F), ∃ (x : E), coe_fn f x = y ∧ norm x ≤ C * norm y := sorry /-- The Banach open mapping theorem: a surjective bounded linear map between Banach spaces is open. -/ theorem open_mapping {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : continuous_linear_map 𝕜 E F) [complete_space F] [complete_space E] (surj : function.surjective ⇑f) : is_open_map ⇑f := sorry namespace linear_equiv /-- If a bounded linear map is a bijection, then its inverse is also a bounded linear map. -/ theorem continuous_symm {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] [complete_space E] (e : linear_equiv 𝕜 E F) (h : continuous ⇑e) : continuous ⇑(symm e) := sorry /-- Associating to a linear equivalence between Banach spaces a continuous linear equivalence when the direct map is continuous, thanks to the Banach open mapping theorem that ensures that the inverse map is also continuous. -/ def to_continuous_linear_equiv_of_continuous {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] [complete_space E] (e : linear_equiv 𝕜 E F) (h : continuous ⇑e) : continuous_linear_equiv 𝕜 E F := continuous_linear_equiv.mk (mk (to_fun e) sorry sorry (inv_fun e) sorry sorry) @[simp] theorem coe_fn_to_continuous_linear_equiv_of_continuous {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] [complete_space E] (e : linear_equiv 𝕜 E F) (h : continuous ⇑e) : ⇑(to_continuous_linear_equiv_of_continuous e h) = ⇑e := rfl @[simp] theorem coe_fn_to_continuous_linear_equiv_of_continuous_symm {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] [complete_space E] (e : linear_equiv 𝕜 E F) (h : continuous ⇑e) : ⇑(continuous_linear_equiv.symm (to_continuous_linear_equiv_of_continuous e h)) = ⇑(symm e) := rfl end linear_equiv namespace continuous_linear_equiv /-- Convert a bijective continuous linear map `f : E →L[𝕜] F` between two Banach spaces to a continuous linear equivalence. -/ def of_bijective {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] [complete_space E] (f : continuous_linear_map 𝕜 E F) (hinj : continuous_linear_map.ker f = ⊥) (hsurj : continuous_linear_map.range f = ⊤) : continuous_linear_equiv 𝕜 E F := linear_equiv.to_continuous_linear_equiv_of_continuous (linear_equiv.of_bijective (↑f) hinj hsurj) sorry @[simp] theorem coe_fn_of_bijective {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] [complete_space E] (f : continuous_linear_map 𝕜 E F) (hinj : continuous_linear_map.ker f = ⊥) (hsurj : continuous_linear_map.range f = ⊤) : ⇑(of_bijective f hinj hsurj) = ⇑f := rfl @[simp] theorem of_bijective_symm_apply_apply {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] [complete_space E] (f : continuous_linear_map 𝕜 E F) (hinj : continuous_linear_map.ker f = ⊥) (hsurj : continuous_linear_map.range f = ⊤) (x : E) : coe_fn (continuous_linear_equiv.symm (of_bijective f hinj hsurj)) (coe_fn f x) = x := symm_apply_apply (of_bijective f hinj hsurj) x @[simp] theorem of_bijective_apply_symm_apply {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space F] [complete_space E] (f : continuous_linear_map 𝕜 E F) (hinj : continuous_linear_map.ker f = ⊥) (hsurj : continuous_linear_map.range f = ⊤) (y : F) : coe_fn f (coe_fn (continuous_linear_equiv.symm (of_bijective f hinj hsurj)) y) = y := apply_symm_apply (of_bijective f hinj hsurj) y end Mathlib
28e6cac6fc0834ed5a541321d21b404e225f86f0
491068d2ad28831e7dade8d6dff871c3e49d9431
/library/init/function.lean
a918662e9f794157d9222a19b834254698676117
[ "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
5,701
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad, Haitao Zhang General operations on functions. -/ prelude import init.prod init.funext init.logic namespace function variables {A : Type} {B : Type} {C : Type} {D : Type} {E : Type} definition compose [reducible] [unfold-full] (f : B → C) (g : A → B) : A → C := λx, f (g x) definition compose_right [reducible] [unfold-full] (f : B → B → B) (g : A → B) : B → A → B := λ b a, f b (g a) definition compose_left [reducible] [unfold-full] (f : B → B → B) (g : A → B) : A → B → B := λ a b, f (g a) b definition id [reducible] [unfold-full] (a : A) : A := a definition on_fun [reducible] [unfold-full] (f : B → B → C) (g : A → B) : A → A → C := λx y, f (g x) (g y) definition combine [reducible] [unfold-full] (f : A → B → C) (op : C → D → E) (g : A → B → D) : A → B → E := λx y, op (f x y) (g x y) definition const [reducible] [unfold-full] (B : Type) (a : A) : B → A := λx, a definition dcompose [reducible] [unfold-full] {B : A → Type} {C : Π {x : A}, B x → Type} (f : Π {x : A} (y : B x), C y) (g : Πx, B x) : Πx, C (g x) := λx, f (g x) definition swap [reducible] [unfold-full] {C : A → B → Type} (f : Πx y, C x y) : Πy x, C x y := λy x, f x y definition app [reducible] {B : A → Type} (f : Πx, B x) (x : A) : B x := f x definition curry [reducible] [unfold-full] : (A × B → C) → A → B → C := λ f a b, f (a, b) definition uncurry [reducible] [unfold 5] : (A → B → C) → (A × B → C) := λ f p, match p with (a, b) := f a b end theorem curry_uncurry (f : A → B → C) : curry (uncurry f) = f := rfl theorem uncurry_curry (f : A × B → C) : uncurry (curry f) = f := funext (λ p, match p with (a, b) := rfl end) infixr ` ∘ ` := compose infixr ` ∘' `:60 := dcompose infixl ` on `:1 := on_fun infixr ` $ `:1 := app notation f ` -[` op `]- ` g := combine f op g lemma left_id (f : A → B) : id ∘ f = f := rfl lemma right_id (f : A → B) : f ∘ id = f := rfl theorem compose.assoc (f : C → D) (g : B → C) (h : A → B) : (f ∘ g) ∘ h = f ∘ (g ∘ h) := rfl theorem compose.left_id (f : A → B) : id ∘ f = f := rfl theorem compose.right_id (f : A → B) : f ∘ id = f := rfl theorem compose_const_right (f : B → C) (b : B) : f ∘ (const A b) = const A (f b) := rfl definition injective [reducible] (f : A → B) : Prop := ∀ ⦃a₁ a₂⦄, f a₁ = f a₂ → a₁ = a₂ theorem injective_compose {g : B → C} {f : A → B} (Hg : injective g) (Hf : injective f) : injective (g ∘ f) := take a₁ a₂, assume Heq, Hf (Hg Heq) definition surjective [reducible] (f : A → B) : Prop := ∀ b, ∃ a, f a = b theorem surjective_compose {g : B → C} {f : A → B} (Hg : surjective g) (Hf : surjective f) : surjective (g ∘ f) := take c, obtain b (Hb : g b = c), from Hg c, obtain a (Ha : f a = b), from Hf b, exists.intro a (eq.trans (congr_arg g Ha) Hb) definition bijective (f : A → B) := injective f ∧ surjective f theorem bijective_compose {g : B → C} {f : A → B} (Hg : bijective g) (Hf : bijective f) : bijective (g ∘ f) := obtain Hginj Hgsurj, from Hg, obtain Hfinj Hfsurj, from Hf, and.intro (injective_compose Hginj Hfinj) (surjective_compose Hgsurj Hfsurj) -- g is a left inverse to f definition left_inverse (g : B → A) (f : A → B) : Prop := ∀x, g (f x) = x definition id_of_left_inverse {g : B → A} {f : A → B} : left_inverse g f → g ∘ f = id := assume h, funext h definition has_left_inverse (f : A → B) : Prop := ∃ finv : B → A, left_inverse finv f -- g is a right inverse to f definition right_inverse (g : B → A) (f : A → B) : Prop := left_inverse f g definition id_of_righ_inverse {g : B → A} {f : A → B} : right_inverse g f → f ∘ g = id := assume h, funext h definition has_right_inverse (f : A → B) : Prop := ∃ finv : B → A, right_inverse finv f theorem injective_of_left_inverse {g : B → A} {f : A → B} : left_inverse g f → injective f := assume h, take a b, assume faeqfb, calc a = g (f a) : by rewrite h ... = g (f b) : faeqfb ... = b : by rewrite h theorem injective_of_has_left_inverse {f : A → B} : has_left_inverse f → injective f := assume h, obtain (finv : B → A) (inv : left_inverse finv f), from h, injective_of_left_inverse inv theorem right_inverse_of_injective_of_left_inverse {f : A → B} {g : B → A} (injf : injective f) (lfg : left_inverse f g) : right_inverse f g := take x, have H : f (g (f x)) = f x, from lfg (f x), injf H theorem surjective_of_has_right_inverse {f : A → B} : has_right_inverse f → surjective f := assume h, take b, obtain (finv : B → A) (inv : right_inverse finv f), from h, let a : A := finv b in have h : f a = b, from calc f a = (f ∘ finv) b : rfl ... = id b : by rewrite inv ... = b : rfl, exists.intro a h theorem left_inverse_of_surjective_of_right_inverse {f : A → B} {g : B → A} (surjf : surjective f) (rfg : right_inverse f g) : left_inverse f g := take y, obtain x (Hx : f x = y), from surjf y, calc f (g y) = f (g (f x)) : Hx ... = f x : rfg ... = y : Hx theorem injective_id : injective (@id A) := take a₁ a₂ H, H theorem surjective_id : surjective (@id A) := take a, exists.intro a rfl theorem bijective_id : bijective (@id A) := and.intro injective_id surjective_id end function -- copy reducible annotations to top-level export [reduce-hints] [unfold-hints] function
07aa25664c87c8ea2d34345c9645ec4b40a4526d
43390109ab88557e6090f3245c47479c123ee500
/src/category_theory/Problems/1_1.lean
86bc9075a6605d2a89d1062205fba77b03ba59a9
[ "Apache-2.0" ]
permissive
Ja1941/xena-UROP-2018
41f0956519f94d56b8bf6834a8d39473f4923200
b111fb87f343cf79eca3b886f99ee15c1dd9884b
refs/heads/master
1,662,355,955,139
1,590,577,325,000
1,590,577,325,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,085
lean
import categories.category import categories.isomorphism import categories.tactics import categories.functor import categories.ndefs open categories open categories.isomorphism open categories.functor open tactic --delaration of universes and variables universes u v u₁ v₁ variables (C : Type u₁) [𝒞 : category.{u₁ v₁} C] include 𝒞 -- 1a Show that identities in a category are unique theorem uniq_id (X : C) (id' : X ⟶ X) : (∀ {A : C} (g : X ⟶ A), id' ≫ g = g) → (∀ {A : C} (g : A ⟶ X), g ≫ id' = g) → (id' = 𝟙X) := begin intros hl hr, transitivity, symmetry, exact category.right_identity_lemma C id', exact hl(𝟙X) end -- 1b Show that a morphism with both a left inverse and right inverse is an isomorphism theorem landr_id (X Y Z : C) (f : X ⟶ Y) : (∃ gl : Y ⟶ X, gl ≫ f = 𝟙Y) → (∃ gr : Y ⟶ X, f ≫ gr = 𝟙X) → (is_Isomorphism' f) := begin intros, cases (classical.indefinite_description _ a) with gl hl, cases (classical.indefinite_description _ a_1) with gr hr, apply nonempty.intro (⟨gr, hr, begin simp, symmetry, exact calc 𝟙Y = gl ≫ f : eq.symm hl ... = gl ≫ 𝟙X ≫ f : by rw category.left_identity_lemma C f ... = (gl ≫ 𝟙X) ≫ f : by rw category.associativity_lemma ... = (gl ≫ (f ≫ gr)) ≫ f : by rw hr ... = ((gl ≫ f) ≫ gr) ≫ f : by rw category.associativity_lemma C gl f gr ... = (𝟙Y ≫ gr) ≫ f : by rw hl ... = gr ≫ f : by rw category.left_identity_lemma C gr end⟩ : is_Isomorphism f) end -- 1c Consider f : X ⟶ Y and g : Y ⟶ Z. Show that if two out of f, g and gf are isomorphisms,then so is the third. section Two_Out_Of_Three variables (X Y Z : C) variables (f : X ⟶ Y) (g : Y ⟶ Z) theorem tootfirsec : is_Isomorphism' f → is_Isomorphism' g → is_Isomorphism' (f ≫ g) := begin intros hf hg, apply hf.elim, apply hg.elim, intros Ig If, exact nonempty.intro ⟨Ig.1 ≫ If.1, begin simp, exact calc f ≫ g ≫ Ig.1 ≫ If.1 = f ≫ (g ≫ Ig.1) ≫ If.1 : by rw category.associativity_lemma ... = f ≫ 𝟙Y ≫ If.1 : by rw is_Isomorphism.witness_1_lemma ... = f ≫ If.1 : by rw category.left_identity_lemma ... = 𝟙X : by rw is_Isomorphism.witness_1_lemma end, begin simp, exact calc Ig.1 ≫ If.1 ≫ f ≫ g = Ig.1 ≫ (If.1 ≫ f) ≫ g : by rw category.associativity_lemma ... = Ig.1 ≫ 𝟙Y ≫ g : by rw is_Isomorphism.witness_2_lemma ... = Ig.1 ≫ g : by rw category.left_identity_lemma ... = 𝟙Z : by rw is_Isomorphism.witness_2_lemma end⟩ end theorem tootsecthi : is_Isomorphism' g → is_Isomorphism' (f ≫ g) → is_Isomorphism' f := begin intros hg hfg, apply hg.elim, apply hfg.elim, intros Ifg Ig, exact nonempty.intro ⟨g ≫ Ifg.1, begin simp, exact calc f ≫ g ≫ Ifg.1 = (f ≫ g) ≫ Ifg.1 : by rw category.associativity_lemma ... = 𝟙X : by rw is_Isomorphism.witness_1_lemma end, begin simp, exact calc g ≫ Ifg.1 ≫ f = (g ≫ Ifg.1 ≫ f) ≫ 𝟙Y : by rw category.right_identity_lemma ... = g ≫ (Ifg.1 ≫ f) ≫ 𝟙Y : by rw category.associativity_lemma ... = g ≫ Ifg.1 ≫ f ≫ 𝟙Y : by rw category.associativity_lemma ... = g ≫ Ifg.1 ≫ f ≫ g ≫ Ig.1 : by rw is_Isomorphism.witness_1_lemma ... = g ≫ (Ifg.1 ≫ ((f ≫ g) ≫ Ig.1)) : by rw category.associativity_lemma ... = g ≫ (Ifg.1 ≫ (f ≫ g)) ≫ Ig.1 : by rw (category.associativity_lemma C Ifg.1 (f ≫ g) Ig.1) ... = g ≫ 𝟙Z ≫ Ig.1 : by rw is_Isomorphism.witness_2_lemma ... = g ≫ Ig.1 : by rw category.left_identity_lemma ... = 𝟙Y : by rw is_Isomorphism.witness_1_lemma end⟩ end theorem tootfirthi : is_Isomorphism' f → is_Isomorphism' (f ≫ g) → is_Isomorphism' g := begin intros hf hfg, apply hf.elim, apply hfg.elim, intros Ifg If, exact nonempty.intro ⟨Ifg.1 ≫ f, begin simp, exact calc g ≫ Ifg.1 ≫ f = 𝟙Y ≫ g ≫ Ifg.1 ≫ f : by rw category.left_identity_lemma ... = (If.1 ≫ f) ≫ g ≫ Ifg.1 ≫ f : by rw is_Isomorphism.witness_2_lemma ... = ((If.1 ≫ f) ≫ g) ≫ Ifg.1 ≫ f : by rw (category.associativity_lemma C (If.1 ≫ f) g (Ifg.1 ≫ f)) ... = (If.1 ≫ (f ≫ g)) ≫ Ifg.1 ≫ f : by rw (category.associativity_lemma C If.1 f g) ... = If.1 ≫ (f ≫ g) ≫ Ifg.1 ≫ f : by rw category.associativity_lemma ... = If.1 ≫ ((f ≫ g) ≫ Ifg.1) ≫ f : by rw (category.associativity_lemma C (f ≫ g) Ifg.1 f) ... = If.1 ≫ 𝟙X ≫ f : by rw is_Isomorphism.witness_1_lemma ... = If.1 ≫ f : by rw category.left_identity_lemma ... = 𝟙Y : by rw is_Isomorphism.witness_2_lemma end, begin simp end⟩ end end Two_Out_Of_Three variables {D : Type u} [𝒟 : category.{u v} D] include 𝒟 -- 1d Show functors preserve isomorphisms theorem fun_id (F : C ↝ D) (X Y : C) (f : X ⟶ Y) : (is_Isomorphism' f) → (is_Isomorphism' (F &> f)) := begin intro hf, apply hf.elim, intro If, exact nonempty.intro /- ⟨F &> If.1, begin simp, exact calc (F &> f) ≫ (F &> If.1) = F &> (f ≫ If.1) : by rw Functor.functoriality_lemma ... = F &> 𝟙X : by rw is_Isomorphism.witness_1_lemma ... = 𝟙 (F +> X) : by rw Functor.identities end, begin simp, exact calc (F &> If.1) ≫ (F &> f) = F &> (If.1 ≫ f) : by rw Functor.functoriality_lemma ... = F &> 𝟙Y : by rw is_Isomorphism.witness_2_lemma ... = 𝟙 (F +> Y) : by rw Functor.identities end⟩ -/ (isomorphism.is_Isomorphism_of_Isomorphism (F.onIsomorphisms ⟨f , If.1, by simp, by simp⟩)) end -- 1e Show that if F : C ↝ D is full and faithful, and F &> f : F +> A ⟶ F +> B is an isomorphism in 𝒟, then f : A ⟶ B is an isomorphism in 𝒞 theorem reflecting_isomorphisms (F : C ↝ D) (X Y : C) (f : X ⟶ Y) : is_Full_Functor F → is_Faithful_Functor F → is_Isomorphism' (F &> f) → is_Isomorphism' f := begin intros hfu hfa hFf, apply hFf.elim, intro IFf, cases (classical.indefinite_description _ (hfu IFf.1)) with g hg, apply nonempty.intro (⟨g, begin simp, exact hfa (calc F &> (f ≫ g) = (F &> f) ≫ (F &> g) : by rw Functor.functoriality_lemma ... = 𝟙(F +> X) : by rw [hg, is_Isomorphism.witness_1_lemma] ... = F &> (𝟙X) : by rw Functor.identities ) end, begin simp, exact hfa (calc F &> (g ≫ f) = (F &> g) ≫ (F &> f) : by rw Functor.functoriality_lemma ... = 𝟙(F +> Y) : by rw [hg, is_Isomorphism.witness_2_lemma] ... = F &> (𝟙Y) : by rw Functor.identities ) end⟩ : is_Isomorphism f) end
6a784b8a31540ac5fb5d66c9143739867d71c480
54deab7025df5d2df4573383df7e1e5497b7a2c2
/order/basic.lean
c1d556faeccc89cd877f5cde5b415e0fbcdb049c
[ "Apache-2.0" ]
permissive
HGldJ1966/mathlib
f8daac93a5b4ae805cfb0ecebac21a9ce9469009
c5c5b504b918a6c5e91e372ee29ed754b0513e85
refs/heads/master
1,611,340,395,683
1,503,040,489,000
1,503,040,489,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,690
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad -/ open eq function /- TODO: automatic construction of dual definitions / theorems -/ universes u v w variables {α : Type u} {β : Type v} {γ : Type w} section monotone variables [preorder α] [preorder β] [preorder γ] def monotone (f : α → β) := ∀{{a b}}, a ≤ b → f a ≤ f b theorem monotone_id : @monotone α α _ _ id := assume x y h, h theorem monotone_const {b : β} : monotone (λ(a:α), b) := assume x y h, le_refl b theorem monotone_comp {f : α → β} {g : β → γ} (m_f : monotone f) (m_g : monotone g) : monotone (g ∘ f) := assume a b h, m_g (m_f h) end monotone section increasing variables [preorder α] definition increasing (f : α → α) := ∀a, a ≤ f a end increasing section decreasing variables [preorder α] definition decreasing (f : α → α) := ∀a, f a ≤ a end decreasing section linear_order variables [linear_order α] {a b : α} lemma not_le_iff : ¬ (a ≤ b) ↔ b < a := (lt_iff_not_ge b a).symm lemma not_lt_iff : ¬ (a < b) ↔ b ≤ a := ⟨le_of_not_gt, not_lt_of_ge⟩ end linear_order section decidable_linear_order variable [decidable_linear_order α] variables {a b c d : α} open decidable theorem le_max_left_iff_true (a b : α) : a ≤ max a b ↔ true := iff_true_intro (le_max_left a b) theorem le_max_right_iff_true (a b : α) : b ≤ max a b ↔ true := iff_true_intro (le_max_right a b) theorem min_right_comm (a b c : α) : min (min a b) c = min (min a c) b := right_comm min min_comm min_assoc a b c theorem max.left_comm (a b c : α) : max a (max b c) = max b (max a c) := left_comm max max_comm max_assoc a b c theorem max.right_comm (a b c : α) : max (max a b) c = max (max a c) b := right_comm max max_comm max_assoc a b c end decidable_linear_order /- order instances -/ def preorder_dual (o : preorder α) : preorder α := { le := λx y, y ≤ x, le_refl := le_refl, le_trans := assume a b c h₁ h₂, le_trans h₂ h₁ } instance preorder_fun {ι : Type u} {α : ι → Type v} [∀i, preorder (α i)] : preorder (Πi, α i) := { le := λx y, ∀i, x i ≤ y i, le_refl := assume a i, le_refl (a i), le_trans := assume a b c h₁ h₂ i, le_trans (h₁ i) (h₂ i) } -- instance preorder_fun [preorder β] : preorder (α → β) := by apply_instance instance partial_order_fun {ι : Type u} {α : ι → Type v} [∀i, partial_order (α i)] : partial_order (Πi, α i) := { preorder_fun with le_antisymm := λf g h1 h2, funext (λb, le_antisymm (h1 b) (h2 b)) } def partial_order_dual (wo : partial_order α) : partial_order α := { le := λx y, y ≤ x, le_refl := le_refl, le_trans := assume a b c h₁ h₂, le_trans h₂ h₁, le_antisymm := assume a b h₁ h₂, le_antisymm h₂ h₁ } theorem le_dual_eq_le {α : Type} (wo : partial_order α) (a b : α) : @has_le.le _ (@preorder.to_has_le _ (@partial_order.to_preorder _ (partial_order_dual wo))) a b = @has_le.le _ (@preorder.to_has_le _ (@partial_order.to_preorder _ wo)) b a := rfl theorem comp_le_comp_left_of_monotone [preorder α] [preorder β] [preorder γ] {f : β → α} {g h : γ → β} (m_f : monotone f) (le_gh : g ≤ h) : has_le.le.{max w u} (f ∘ g) (f ∘ h) := assume x, m_f (le_gh x) section monotone variables [preorder α] [preorder γ] theorem monotone_lam {f : α → β → γ} (m : ∀b, monotone (λa, f a b)) : monotone f := assume a a' h b, m b h theorem monotone_app (f : β → α → γ) (b : β) (m : monotone (λa b, f b a)) : monotone (f b) := assume a a' h, m h b end monotone
187be31cc608e67fdde497b4b061c1221a050d44
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/part.lean
0784718a69bccae9b676f395f58188bdd4a2bfce
[ "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
16,883
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Jeremy Avigad, Simon Hudon -/ import data.equiv.basic import data.set.basic /-! # Partial values of a type This file defines `part α`, the partial values of a type. `o : part α` carries a proposition `o.dom`, its domain, along with a function `get : o.dom → α`, its value. The rule is then that every partial value has a value but, to access it, you need to provide a proof of the domain. `part α` behaves the same as `option α` except that `o : option α` is decidably `none` or `some a` for some `a : α`, while the domain of `o : part α` doesn't have to be decidable. That means you can translate back and forth between a partial value with a decidable domain and an option, and `option α` and `part α` are classically equivalent. In general, `part α` is bigger than `option α`. In current mathlib, `part ℕ`, aka `enat`, is used to move decidability of the order to decidability of `enat.find` (which is the smallest natural satisfying a predicate, or `∞` if there's none). ## Main declarations `option`-like declarations: * `part.none`: The partial value whose domain is `false`. * `part.some a`: The partial value whose domain is `true` and whose value is `a`. * `part.of_option`: Converts an `option α` to a `part α` by sending `none` to `none` and `some a` to `some a`. * `part.to_option`: Converts a `part α` with a decidable domain to an `option α`. * `part.equiv_option`: Classical equivalence between `part α` and `option α`. Monadic structure: * `part.bind`: `o.bind f` has value `(f (o.get _)).get _` (`f o` morally) and is defined when `o` and `f (o.get _)` are defined. * `part.map`: Maps the value and keeps the same domain. Other: * `part.restrict`: `part.restrict p o` replaces the domain of `o : part α` by `p : Prop` so long as `p → o.dom`. * `part.assert`: `assert p f` appends `p` to the domains of the values of a partial function. * `part.unwrap`: Gets the value of a partial value regardless of its domain. Unsound. ## Notation For `a : α`, `o : part α`, `a ∈ o` means that `o` is defined and equal to `a`. Formally, it means `o.dom` and `o.get _ = a`. -/ /-- `part α` is the type of "partial values" of type `α`. It is similar to `option α` except the domain condition can be an arbitrary proposition, not necessarily decidable. -/ structure {u} part (α : Type u) : Type u := (dom : Prop) (get : dom → α) namespace part variables {α : Type*} {β : Type*} {γ : Type*} /-- Convert a `part α` with a decidable domain to an option -/ def to_option (o : part α) [decidable o.dom] : option α := if h : dom o then some (o.get h) else none /-- `part` extensionality -/ theorem ext' : ∀ {o p : part α} (H1 : o.dom ↔ p.dom) (H2 : ∀h₁ h₂, o.get h₁ = p.get h₂), o = p | ⟨od, o⟩ ⟨pd, p⟩ H1 H2 := have t : od = pd, from propext H1, by cases t; rw [show o = p, from funext $ λp, H2 p p] /-- `part` eta expansion -/ @[simp] theorem eta : Π (o : part α), (⟨o.dom, λ h, o.get h⟩ : part α) = o | ⟨h, f⟩ := rfl /-- `a ∈ o` means that `o` is defined and equal to `a` -/ protected def mem (a : α) (o : part α) : Prop := ∃ h, o.get h = a instance : has_mem α (part α) := ⟨part.mem⟩ theorem mem_eq (a : α) (o : part α) : (a ∈ o) = (∃ h, o.get h = a) := rfl theorem dom_iff_mem : ∀ {o : part α}, o.dom ↔ ∃ y, y ∈ o | ⟨p, f⟩ := ⟨λh, ⟨f h, h, rfl⟩, λ⟨_, h, rfl⟩, h⟩ theorem get_mem {o : part α} (h) : get o h ∈ o := ⟨_, rfl⟩ /-- `part` extensionality -/ @[ext] theorem ext {o p : part α} (H : ∀ a, a ∈ o ↔ a ∈ p) : o = p := ext' ⟨λ h, ((H _).1 ⟨h, rfl⟩).fst, λ h, ((H _).2 ⟨h, rfl⟩).fst⟩ $ λ a b, ((H _).2 ⟨_, rfl⟩).snd /-- The `none` value in `part` has a `false` domain and an empty function. -/ def none : part α := ⟨false, false.rec _⟩ instance : inhabited (part α) := ⟨none⟩ @[simp] theorem not_mem_none (a : α) : a ∉ @none α := λ h, h.fst /-- The `some a` value in `part` has a `true` domain and the function returns `a`. -/ def some (a : α) : part α := ⟨true, λ_, a⟩ theorem mem_unique : ∀ {a b : α} {o : part α}, a ∈ o → b ∈ o → a = b | _ _ ⟨p, f⟩ ⟨h₁, rfl⟩ ⟨h₂, rfl⟩ := rfl theorem mem.left_unique : relator.left_unique ((∈) : α → part α → Prop) := λ a o b, mem_unique theorem get_eq_of_mem {o : part α} {a} (h : a ∈ o) (h') : get o h' = a := mem_unique ⟨_, rfl⟩ h protected theorem subsingleton (o : part α) : set.subsingleton {a | a ∈ o} := λ a ha b hb, mem_unique ha hb @[simp] theorem get_some {a : α} (ha : (some a).dom) : get (some a) ha = a := rfl theorem mem_some (a : α) : a ∈ some a := ⟨trivial, rfl⟩ @[simp] theorem mem_some_iff {a b} : b ∈ (some a : part α) ↔ b = a := ⟨λ⟨h, e⟩, e.symm, λ e, ⟨trivial, e.symm⟩⟩ theorem eq_some_iff {a : α} {o : part α} : o = some a ↔ a ∈ o := ⟨λ e, e.symm ▸ mem_some _, λ ⟨h, e⟩, e ▸ ext' (iff_true_intro h) (λ _ _, rfl)⟩ theorem eq_none_iff {o : part α} : o = none ↔ ∀ a, a ∉ o := ⟨λ e, e.symm ▸ not_mem_none, λ h, ext (by simpa)⟩ theorem eq_none_iff' {o : part α} : o = none ↔ ¬ o.dom := ⟨λ e, e.symm ▸ id, λ h, eq_none_iff.2 (λ a h', h h'.fst)⟩ @[simp] lemma some_ne_none (x : α) : some x ≠ none := by { intro h, change none.dom, rw [← h], trivial } @[simp] lemma none_ne_some (x : α) : none ≠ some x := (some_ne_none x).symm lemma ne_none_iff {o : part α} : o ≠ none ↔ ∃ x, o = some x := begin split, { rw [ne, eq_none_iff', not_not], exact λ h, ⟨o.get h, eq_some_iff.2 (get_mem h)⟩ }, { rintro ⟨x, rfl⟩, apply some_ne_none } end lemma eq_none_or_eq_some (o : part α) : o = none ∨ ∃ x, o = some x := or_iff_not_imp_left.2 ne_none_iff.1 @[simp] lemma some_inj {a b : α} : part.some a = some b ↔ a = b := function.injective.eq_iff (λ a b h, congr_fun (eq_of_heq (part.mk.inj h).2) trivial) @[simp] lemma some_get {a : part α} (ha : a.dom) : part.some (part.get a ha) = a := eq.symm (eq_some_iff.2 ⟨ha, rfl⟩) lemma get_eq_iff_eq_some {a : part α} {ha : a.dom} {b : α} : a.get ha = b ↔ a = some b := ⟨λ h, by simp [h.symm], λ h, by simp [h]⟩ lemma get_eq_get_of_eq (a : part α) (ha : a.dom) {b : part α} (h : a = b) : a.get ha = b.get (h ▸ ha) := by { congr, exact h } lemma get_eq_iff_mem {o : part α} {a : α} (h : o.dom) : o.get h = a ↔ a ∈ o := ⟨λ H, ⟨h, H⟩, λ ⟨h', H⟩, H⟩ lemma eq_get_iff_mem {o : part α} {a : α} (h : o.dom) : a = o.get h ↔ a ∈ o := eq_comm.trans (get_eq_iff_mem h) @[simp] lemma none_to_option [decidable (@none α).dom] : (none : part α).to_option = option.none := dif_neg id @[simp] lemma some_to_option (a : α) [decidable (some a).dom] : (some a).to_option = option.some a := dif_pos trivial instance none_decidable : decidable (@none α).dom := decidable.false instance some_decidable (a : α) : decidable (some a).dom := decidable.true /-- Retrieves the value of `a : part α` if it exists, and return the provided default value otherwise. -/ def get_or_else (a : part α) [decidable a.dom] (d : α) := if ha : a.dom then a.get ha else d @[simp] lemma get_or_else_none (d : α) [decidable (none : part α).dom] : get_or_else none d = d := dif_neg id @[simp] lemma get_or_else_some (a : α) (d : α) [decidable (some a).dom] : get_or_else (some a) d = a := dif_pos trivial @[simp] theorem mem_to_option {o : part α} [decidable o.dom] {a : α} : a ∈ to_option o ↔ a ∈ o := begin unfold to_option, by_cases h : o.dom; simp [h], { exact ⟨λ h, ⟨_, h⟩, λ ⟨_, h⟩, h⟩ }, { exact mt Exists.fst h } end /-- Converts an `option α` into a `part α`. -/ def of_option : option α → part α | option.none := none | (option.some a) := some a @[simp] theorem mem_of_option {a : α} : ∀ {o : option α}, a ∈ of_option o ↔ a ∈ o | option.none := ⟨λ h, h.fst.elim, λ h, option.no_confusion h⟩ | (option.some b) := ⟨λ h, congr_arg option.some h.snd, λ h, ⟨trivial, option.some.inj h⟩⟩ @[simp] theorem of_option_dom {α} : ∀ (o : option α), (of_option o).dom ↔ o.is_some | option.none := by simp [of_option, none] | (option.some a) := by simp [of_option] theorem of_option_eq_get {α} (o : option α) : of_option o = ⟨_, @option.get _ o⟩ := part.ext' (of_option_dom o) $ λ h₁ h₂, by cases o; [cases h₁, refl] instance : has_coe (option α) (part α) := ⟨of_option⟩ @[simp] theorem mem_coe {a : α} {o : option α} : a ∈ (o : part α) ↔ a ∈ o := mem_of_option @[simp] theorem coe_none : (@option.none α : part α) = none := rfl @[simp] theorem coe_some (a : α) : (option.some a : part α) = some a := rfl @[elab_as_eliminator] protected lemma induction_on {P : part α → Prop} (a : part α) (hnone : P none) (hsome : ∀ a : α, P (some a)) : P a := (classical.em a.dom).elim (λ h, part.some_get h ▸ hsome _) (λ h, (eq_none_iff'.2 h).symm ▸ hnone) instance of_option_decidable : ∀ o : option α, decidable (of_option o).dom | option.none := part.none_decidable | (option.some a) := part.some_decidable a @[simp] theorem to_of_option (o : option α) : to_option (of_option o) = o := by cases o; refl @[simp] theorem of_to_option (o : part α) [decidable o.dom] : of_option (to_option o) = o := ext $ λ a, mem_of_option.trans mem_to_option /-- `part α` is (classically) equivalent to `option α`. -/ noncomputable def equiv_option : part α ≃ option α := by haveI := classical.dec; exact ⟨λ o, to_option o, of_option, λ o, of_to_option o, λ o, eq.trans (by dsimp; congr) (to_of_option o)⟩ /-- We give `part α` the order where everything is greater than `none`. -/ instance : partial_order (part α) := { le := λ x y, ∀ i, i ∈ x → i ∈ y, le_refl := λ x y, id, le_trans := λ x y z f g i, g _ ∘ f _, le_antisymm := λ x y f g, part.ext $ λ z, ⟨f _, g _⟩ } instance : order_bot (part α) := { bot := none, bot_le := by { introv x, rintro ⟨⟨_⟩,_⟩, } } instance : preorder (part α) := by apply_instance lemma le_total_of_le_of_le {x y : part α} (z : part α) (hx : x ≤ z) (hy : y ≤ z) : x ≤ y ∨ y ≤ x := begin rcases part.eq_none_or_eq_some x with h | ⟨b, h₀⟩, { rw h, left, apply order_bot.bot_le _ }, right, intros b' h₁, rw part.eq_some_iff at h₀, replace hx := hx _ h₀, replace hy := hy _ h₁, replace hx := part.mem_unique hx hy, subst hx, exact h₀ end /-- `assert p f` is a bind-like operation which appends an additional condition `p` to the domain and uses `f` to produce the value. -/ def assert (p : Prop) (f : p → part α) : part α := ⟨∃ h : p, (f h).dom, λha, (f ha.fst).get ha.snd⟩ /-- The bind operation has value `g (f.get)`, and is defined when all the parts are defined. -/ protected def bind (f : part α) (g : α → part β) : part β := assert (dom f) (λb, g (f.get b)) /-- The map operation for `part` just maps the value and maintains the same domain. -/ @[simps] def map (f : α → β) (o : part α) : part β := ⟨o.dom, f ∘ o.get⟩ theorem mem_map (f : α → β) {o : part α} : ∀ {a}, a ∈ o → f a ∈ map f o | _ ⟨h, rfl⟩ := ⟨_, rfl⟩ @[simp] theorem mem_map_iff (f : α → β) {o : part α} {b} : b ∈ map f o ↔ ∃ a ∈ o, f a = b := ⟨match b with _, ⟨h, rfl⟩ := ⟨_, ⟨_, rfl⟩, rfl⟩ end, λ ⟨a, h₁, h₂⟩, h₂ ▸ mem_map f h₁⟩ @[simp] theorem map_none (f : α → β) : map f none = none := eq_none_iff.2 $ λ a, by simp @[simp] theorem map_some (f : α → β) (a : α) : map f (some a) = some (f a) := eq_some_iff.2 $ mem_map f $ mem_some _ theorem mem_assert {p : Prop} {f : p → part α} : ∀ {a} (h : p), a ∈ f h → a ∈ assert p f | _ x ⟨h, rfl⟩ := ⟨⟨x, h⟩, rfl⟩ @[simp] theorem mem_assert_iff {p : Prop} {f : p → part α} {a} : a ∈ assert p f ↔ ∃ h : p, a ∈ f h := ⟨match a with _, ⟨h, rfl⟩ := ⟨_, ⟨_, rfl⟩⟩ end, λ ⟨a, h⟩, mem_assert _ h⟩ lemma assert_pos {p : Prop} {f : p → part α} (h : p) : assert p f = f h := begin dsimp [assert], cases h' : f h, simp only [h', h, true_and, iff_self, exists_prop_of_true, eq_iff_iff], apply function.hfunext, { simp only [h,h',exists_prop_of_true] }, { cc } end lemma assert_neg {p : Prop} {f : p → part α} (h : ¬ p) : assert p f = none := begin dsimp [assert,none], congr, { simp only [h, not_false_iff, exists_prop_of_false] }, { apply function.hfunext, { simp only [h, not_false_iff, exists_prop_of_false] }, cc }, end theorem mem_bind {f : part α} {g : α → part β} : ∀ {a b}, a ∈ f → b ∈ g a → b ∈ f.bind g | _ _ ⟨h, rfl⟩ ⟨h₂, rfl⟩ := ⟨⟨h, h₂⟩, rfl⟩ @[simp] theorem mem_bind_iff {f : part α} {g : α → part β} {b} : b ∈ f.bind g ↔ ∃ a ∈ f, b ∈ g a := ⟨match b with _, ⟨⟨h₁, h₂⟩, rfl⟩ := ⟨_, ⟨_, rfl⟩, ⟨_, rfl⟩⟩ end, λ ⟨a, h₁, h₂⟩, mem_bind h₁ h₂⟩ @[simp] theorem bind_none (f : α → part β) : none.bind f = none := eq_none_iff.2 $ λ a, by simp @[simp] theorem bind_some (a : α) (f : α → part β) : (some a).bind f = f a := ext $ by simp theorem bind_of_mem {o : part α} {a : α} (h : a ∈ o) (f : α → part β) : o.bind f = f a := by rw [eq_some_iff.2 h, bind_some] theorem bind_some_eq_map (f : α → β) (x : part α) : x.bind (some ∘ f) = map f x := ext $ by simp [eq_comm] theorem bind_assoc {γ} (f : part α) (g : α → part β) (k : β → part γ) : (f.bind g).bind k = f.bind (λ x, (g x).bind k) := ext $ λ a, by simp; exact ⟨λ ⟨_, ⟨_, h₁, h₂⟩, h₃⟩, ⟨_, h₁, _, h₂, h₃⟩, λ ⟨_, h₁, _, h₂, h₃⟩, ⟨_, ⟨_, h₁, h₂⟩, h₃⟩⟩ @[simp] theorem bind_map {γ} (f : α → β) (x) (g : β → part γ) : (map f x).bind g = x.bind (λ y, g (f y)) := by rw [← bind_some_eq_map, bind_assoc]; simp @[simp] theorem map_bind {γ} (f : α → part β) (x : part α) (g : β → γ) : map g (x.bind f) = x.bind (λ y, map g (f y)) := by rw [← bind_some_eq_map, bind_assoc]; simp [bind_some_eq_map] theorem map_map (g : β → γ) (f : α → β) (o : part α) : map g (map f o) = map (g ∘ f) o := by rw [← bind_some_eq_map, bind_map, bind_some_eq_map] instance : monad part := { pure := @some, map := @map, bind := @part.bind } instance : is_lawful_monad part := { bind_pure_comp_eq_map := @bind_some_eq_map, id_map := λ β f, by cases f; refl, pure_bind := @bind_some, bind_assoc := @bind_assoc } theorem map_id' {f : α → α} (H : ∀ (x : α), f x = x) (o) : map f o = o := by rw [show f = id, from funext H]; exact id_map o @[simp] theorem bind_some_right (x : part α) : x.bind some = x := by rw [bind_some_eq_map]; simp [map_id'] @[simp] theorem pure_eq_some (a : α) : pure a = some a := rfl @[simp] theorem ret_eq_some (a : α) : return a = some a := rfl @[simp] theorem map_eq_map {α β} (f : α → β) (o : part α) : f <$> o = map f o := rfl @[simp] theorem bind_eq_bind {α β} (f : part α) (g : α → part β) : f >>= g = f.bind g := rfl lemma bind_le {α} (x : part α) (f : α → part β) (y : part β) : x >>= f ≤ y ↔ (∀ a, a ∈ x → f a ≤ y) := begin split; intro h, { intros a h' b, replace h := h b, simp only [and_imp, exists_prop, bind_eq_bind, mem_bind_iff, exists_imp_distrib] at h, apply h _ h' }, { intros b h', simp only [exists_prop, bind_eq_bind, mem_bind_iff] at h', rcases h' with ⟨a,h₀,h₁⟩, apply h _ h₀ _ h₁ }, end instance : monad_fail part := { fail := λ_ _, none, ..part.monad } /-- `restrict p o h` replaces the domain of `o` with `p`, and is well defined when `p` implies `o` is defined. -/ def restrict (p : Prop) (o : part α) (H : p → o.dom) : part α := ⟨p, λh, o.get (H h)⟩ @[simp] theorem mem_restrict (p : Prop) (o : part α) (h : p → o.dom) (a : α) : a ∈ restrict p o h ↔ p ∧ a ∈ o := begin dsimp [restrict, mem_eq], split, { rintro ⟨h₀, h₁⟩, exact ⟨h₀, ⟨_, h₁⟩⟩ }, rintro ⟨h₀, h₁, h₂⟩, exact ⟨h₀, h₂⟩ end /-- `unwrap o` gets the value at `o`, ignoring the condition. This function is unsound. -/ meta def unwrap (o : part α) : α := o.get undefined theorem assert_defined {p : Prop} {f : p → part α} : ∀ (h : p), (f h).dom → (assert p f).dom := exists.intro theorem bind_defined {f : part α} {g : α → part β} : ∀ (h : f.dom), (g (f.get h)).dom → (f.bind g).dom := assert_defined @[simp] theorem bind_dom {f : part α} {g : α → part β} : (f.bind g).dom ↔ ∃ h : f.dom, (g (f.get h)).dom := iff.rfl end part
9fdf5c8bc468a4925fc865c30e04e6a92022dad2
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/string/ops.lean
63e3f3c55f2c91ac5726d371b70b38ec1c23a7f4
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,098
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.bool.lemmas import Mathlib.Lean3Lib.init.data.string.basic import Mathlib.Lean3Lib.init.meta.well_founded_tactics namespace Mathlib namespace string namespace iterator @[simp] theorem next_to_string_mk_iterator (s : string) : next_to_string (mk_iterator s) = s := string_imp.rec (fun (s : List char) => Eq.refl (next_to_string (mk_iterator (string_imp.mk s)))) s @[simp] theorem length_next_to_string_next (it : iterator) : length (next_to_string (next it)) = length (next_to_string it) - 1 := sorry theorem zero_lt_length_next_to_string_of_has_next {it : iterator} : ↥(has_next it) → 0 < length (next_to_string it) := sorry end iterator -- TODO(Sebastian): generalize to something like https://doc.rust-lang.org/std/primitive.str.html#method.split def split (p : char → Bool) (s : string) : List string := split_core p (mk_iterator s) (mk_iterator s)
bf88c5b732b337be9f2a7f28466b5ef3144be8e4
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/sizeof1.lean
380661c32678e012b737af1700079ef00f97edce
[ "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
431
lean
mutual inductive TreePos (α : Type u) (β : Type v) where | leaf (a : α) | node (children : List (List (TreeNeg α β))) inductive TreeNeg (α : Type u) (β : Type v) where | leaf (a : β) | node (children : List (List (TreePos α β))) end #print TreePos.leaf.sizeOf_spec #print TreePos.node.sizeOf_spec #print TreePos._sizeOf_3_eq #print TreePos._sizeOf_4_eq #print TreePos._sizeOf_5_eq #print TreePos._sizeOf_6_eq
df51ddf80ee8c90d3978c00007ad624f6087e399
a4673261e60b025e2c8c825dfa4ab9108246c32e
/src/Lean/Elab/BuiltinNotation.lean
f960dc1bb71f3e62363d401c254817d34fd2f7ca
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,614
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Init.Data.ToString import Lean.Compiler.BorrowedAnnotation import Lean.Meta.KAbstract import Lean.Elab.Term import Lean.Elab.Quotation import Lean.Elab.SyntheticMVars namespace Lean.Elab.Term open Meta @[builtinTermElab anonymousCtor] def elabAnonymousCtor : TermElab := fun stx expectedType? => match_syntax stx with | `(⟨$args*⟩) => do tryPostponeIfNoneOrMVar expectedType? match expectedType? with | some expectedType => let expectedType ← whnf expectedType matchConstInduct expectedType.getAppFn (fun _ => throwError! "invalid constructor ⟨...⟩, expected type must be an inductive type {indentExpr expectedType}") (fun ival us => do match ival.ctors with | [ctor] => let newStx ← `($(mkCIdentFrom stx ctor) $(args.getSepElems)*) withMacroExpansion stx newStx $ elabTerm newStx expectedType? | _ => throwError! "invalid constructor ⟨...⟩, expected type must be an inductive type with only one constructor {indentExpr expectedType}") | none => throwError "invalid constructor ⟨...⟩, expected type must be known" | _ => throwUnsupportedSyntax @[builtinTermElab borrowed] def elabBorrowed : TermElab := fun stx expectedType? => match_syntax stx with | `(@& $e) => return markBorrowed (← elabTerm e expectedType?) | _ => throwUnsupportedSyntax @[builtinMacro Lean.Parser.Term.show] def expandShow : Macro := fun stx => match_syntax stx with | `(show $type from $val) => let thisId := mkIdentFrom stx `this; `(let! $thisId : $type := $val; $thisId) | `(show $type by $tac:tacticSeq) => `(show $type from by $tac:tacticSeq) | _ => Macro.throwUnsupported @[builtinMacro Lean.Parser.Term.have] def expandHave : Macro := fun stx => let stx := stx.setArg 4 (mkNullNode #[mkAtomFrom stx ";"]) -- HACK match_syntax stx with | `(have $type from $val; $body) => let thisId := mkIdentFrom stx `this; `(let! $thisId : $type := $val; $body) | `(have $type by $tac:tacticSeq; $body) => `(have $type from by $tac:tacticSeq; $body) | `(have $type := $val; $body) => let thisId := mkIdentFrom stx `this; `(let! $thisId : $type := $val; $body) | `(have $x : $type from $val; $body) => `(let! $x:ident : $type := $val; $body) | `(have $x : $type by $tac:tacticSeq; $body) => `(have $x : $type from by $tac:tacticSeq; $body) | `(have $x : $type := $val; $body) => `(let! $x:ident : $type := $val; $body) | _ => Macro.throwUnsupported @[builtinMacro Lean.Parser.Term.suffices] def expandSuffices : Macro := fun stx => let stx := stx.setArg 4 (mkNullNode #[mkAtomFrom stx ";"]) -- HACK match_syntax stx with | `(suffices $type from $val; $body) => `(have $type from $body; $val) | `(suffices $type by $tac:tacticSeq; $body) => `(have $type from $body; by $tac:tacticSeq) | `(suffices $x : $type from $val; $body) => `(have $x:ident : $type from $body; $val) | `(suffices $x : $type by $tac:tacticSeq; $body) => `(have $x:ident : $type from $body; by $tac:tacticSeq) | _ => Macro.throwUnsupported private def elabParserMacroAux (prec : Syntax) (e : Syntax) : TermElabM Syntax := do let (some declName) ← getDeclName? | throwError "invalid `parser!` macro, it must be used in definitions" match extractMacroScopes declName with | { name := Name.str _ s _, scopes := scps, .. } => let kind := quote declName let s := quote s let p ← `(Lean.Parser.leadingNode $kind $prec $e) if scps == [] then -- TODO simplify the following quotation as soon as we have coercions `(OrElse.orElse (Lean.Parser.mkAntiquot $s (some $kind)) $p) else -- if the parser decl is hidden by hygiene, it doesn't make sense to provide an antiquotation kind `(OrElse.orElse (Lean.Parser.mkAntiquot $s none) $p) | _ => throwError "invalid `parser!` macro, unexpected declaration name" @[builtinTermElab «parser!»] def elabParserMacro : TermElab := adaptExpander fun stx => match_syntax stx with | `(parser! $e) => elabParserMacroAux (quote Parser.maxPrec) e | `(parser! : $prec $e) => elabParserMacroAux prec e | _ => throwUnsupportedSyntax private def elabTParserMacroAux (prec : Syntax) (e : Syntax) : TermElabM Syntax := do let declName? ← getDeclName? match declName? with | some declName => let kind := quote declName; `(Lean.Parser.trailingNode $kind $prec $e) | none => throwError "invalid `tparser!` macro, it must be used in definitions" @[builtinTermElab «tparser!»] def elabTParserMacro : TermElab := adaptExpander fun stx => match_syntax stx with | `(tparser! $e) => elabTParserMacroAux (quote Parser.maxPrec) e | `(tparser! : $prec $e) => elabTParserMacroAux prec e | _ => throwUnsupportedSyntax private def mkNativeReflAuxDecl (type val : Expr) : TermElabM Name := do let auxName ← mkAuxName `_nativeRefl let decl := Declaration.defnDecl { name := auxName, lparams := [], type := type, value := val, hints := ReducibilityHints.abbrev, isUnsafe := false } addDecl decl compileDecl decl pure auxName private def elabClosedTerm (stx : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do let e ← elabTermAndSynthesize stx expectedType? if e.hasMVar then throwError! "invalid macro application, term contains metavariables{indentExpr e}" if e.hasFVar then throwError! "invalid macro application, term contains free variables{indentExpr e}" pure e @[builtinTermElab «nativeRefl»] def elabNativeRefl : TermElab := fun stx _ => do let arg := stx[1] let e ← elabClosedTerm arg none let type ← inferType e let type ← whnf type unless type.isConstOf `Bool || type.isConstOf `Nat do throwError! "invalid `nativeRefl!` macro application, term must have type `Nat` or `Bool`{indentExpr type}" let auxDeclName ← mkNativeReflAuxDecl type e let isBool := type.isConstOf `Bool let reduceValFn := if isBool then `Lean.reduceBool else `Lean.reduceNat let reduceThm := if isBool then `Lean.ofReduceBool else `Lean.ofReduceNat let aux := Lean.mkConst auxDeclName let reduceVal := mkApp (Lean.mkConst reduceValFn) aux let val? ← liftMetaM $ Meta.reduceNative? reduceVal match val? with | none => throwError! "failed to reduce term at `nativeRefl!` macro application{e}" | some val => let rflPrf ← mkEqRefl val let r := mkApp3 (Lean.mkConst reduceThm) aux val rflPrf let eq ← mkEq e val mkExpectedTypeHint r eq private def getPropToDecide (expectedType? : Option Expr) : TermElabM Expr := do tryPostponeIfNoneOrMVar expectedType? match expectedType? with | none => throwError "invalid macro, expected type is not available" | some expectedType => synthesizeSyntheticMVars let expectedType ← instantiateMVars expectedType if expectedType.hasFVar || expectedType.hasMVar then throwError! "expected type must not contain free or meta variables{indentExpr expectedType}" pure expectedType @[builtinTermElab «nativeDecide»] def elabNativeDecide : TermElab := fun stx expectedType? => do let p ← getPropToDecide expectedType? let d ← mkDecide p let auxDeclName ← mkNativeReflAuxDecl (Lean.mkConst `Bool) d let rflPrf ← mkEqRefl (toExpr true) let r := mkApp3 (Lean.mkConst `Lean.ofReduceBool) (Lean.mkConst auxDeclName) (toExpr true) rflPrf mkExpectedTypeHint r p @[builtinTermElab Lean.Parser.Term.decide] def elabDecide : TermElab := fun stx expectedType? => do let p ← getPropToDecide expectedType? let d ← mkDecide p let d ← instantiateMVars d let s := d.appArg! -- get instance from `d` let rflPrf ← mkEqRefl (toExpr true) pure $ mkApp3 (Lean.mkConst `ofDecideEqTrue) p s rflPrf def expandInfix (f : Syntax) : Macro := fun stx => do -- term `op` term let a := stx[0] let b := stx[2] pure (Syntax.mkApp f #[a, b]) def expandInfixOp (op : Name) : Macro := fun stx => expandInfix (mkCIdentFrom stx[1] op) stx def expandPrefixOp (op : Name) : Macro := fun stx => do -- `op` term let a := stx[1] pure (Syntax.mkApp (mkCIdentFrom stx[0] op) #[a]) @[builtinMacro Lean.Parser.Term.not] def expandNot : Macro := expandPrefixOp `Not @[builtinMacro Lean.Parser.Term.bnot] def expandBNot : Macro := expandPrefixOp `not @[builtinMacro Lean.Parser.Term.uminus] def expandUMinus : Macro := expandPrefixOp `Neg.neg @[builtinTermElab panic] def elabPanic : TermElab := fun stx expectedType? => do let arg := stx[1] let pos ← getRefPosition let env ← getEnv let stxNew ← match (← getDeclName?) with | some declName => `(panicWithPosWithDecl $(quote (toString env.mainModule)) $(quote (toString declName)) $(quote pos.line) $(quote pos.column) $arg) | none => `(panicWithPos $(quote (toString env.mainModule)) $(quote pos.line) $(quote pos.column) $arg) withMacroExpansion stx stxNew $ elabTerm stxNew expectedType? @[builtinMacro Lean.Parser.Term.unreachable] def expandUnreachable : Macro := fun stx => `(panic! "unreachable code has been reached") @[builtinMacro Lean.Parser.Term.assert] def expandAssert : Macro := fun stx => -- TODO: support for disabling runtime assertions let cond := stx[1] let body := stx[3] match cond.reprint with | some code => `(if $cond then $body else panic! ("assertion violation: " ++ $(quote code))) | none => `(if $cond then $body else panic! ("assertion violation")) @[builtinMacro Lean.Parser.Term.dbgTrace] def expandDbgTrace : Macro := fun stx => let arg := stx[1] let body := stx[3] if arg.getKind == interpolatedStrKind then `(dbgTrace (s! $arg) fun _ => $body) else `(dbgTrace (toString $arg) fun _ => $body) @[builtinMacro Lean.Parser.Term.«sorry»] def expandSorry : Macro := fun _ => `(sorryAx _ false) @[builtinTermElab emptyC] def expandEmptyC : TermElab := fun stx expectedType? => do let stxNew ← `(EmptyCollection.emptyCollection) withMacroExpansion stx stxNew $ elabTerm stxNew expectedType? /-- Return syntax `Prod.mk elems[0] (Prod.mk elems[1] ... (Prod.mk elems[elems.size - 2] elems[elems.size - 1])))` -/ partial def mkPairs (elems : Array Syntax) : MacroM Syntax := let rec loop (i : Nat) (acc : Syntax) := do if i > 0 then let i := i - 1 let elem := elems[i] let acc ← `(Prod.mk $elem $acc) loop i acc else pure acc loop (elems.size - 1) elems.back private partial def hasCDot : Syntax → Bool | Syntax.node k args => if k == `Lean.Parser.Term.paren then false else if k == `Lean.Parser.Term.cdot then true else args.any hasCDot | _ => false /-- Auxiliary function for expandind the `·` notation. The extra state `Array Syntax` contains the new binder names. If `stx` is a `·`, we create a fresh identifier, store in the extra state, and return it. Otherwise, we just return `stx`. -/ private partial def expandCDot : Syntax → StateT (Array Syntax) MacroM Syntax | stx@(Syntax.node k args) => if k == `Lean.Parser.Term.paren then pure stx else if k == `Lean.Parser.Term.cdot then withFreshMacroScope do let id ← `(a) modify fun s => s.push id; pure id else do let args ← args.mapM expandCDot pure $ Syntax.node k args | stx => pure stx /-- Return `some` if succeeded expanding `·` notation occurring in the given syntax. Otherwise, return `none`. Examples: - `· + 1` => `fun _a_1 => _a_1 + 1` - `f · · b` => `fun _a_1 _a_2 => f _a_1 _a_2 b` -/ def expandCDot? (stx : Syntax) : MacroM (Option Syntax) := do if hasCDot stx then let (newStx, binders) ← (expandCDot stx).run #[]; `(fun $binders* => $newStx) else pure none /-- Try to expand `·` notation, and if successful elaborate result. This method is used to elaborate the Lean parentheses notation. Recall that in Lean the `·` notation must be surrounded by parentheses. We may change this is the future, but right now, here are valid examples - `(· + 1)` - `(f ⟨·, 1⟩ ·)` - `(· + ·)` - `(f · a b)` -/ private def elabCDot (stx : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do match (← liftMacroM $ expandCDot? stx) with | some stx' => withMacroExpansion stx stx' (elabTerm stx' expectedType?) | none => elabTerm stx expectedType? @[builtinTermElab paren] def elabParen : TermElab := fun stx expectedType? => match_syntax stx with | `(()) => pure $ Lean.mkConst `Unit.unit | `(($e : $type)) => do let type ← withSynthesize (mayPostpone := true) $ elabType type let e ← elabCDot e type ensureHasType type e | `(($e)) => elabCDot e expectedType? | `(($e, $es*)) => do let pairs ← liftMacroM $ mkPairs (#[e] ++ es.getEvenElems) withMacroExpansion stx pairs (elabTerm pairs expectedType?) | _ => throwError "unexpected parentheses notation" @[builtinTermElab subst] def elabSubst : TermElab := fun stx expectedType? => do let expectedType ← tryPostponeIfHasMVars expectedType? "invalid `▸` notation" match_syntax stx with | `($heq ▸ $h) => do let mut heq ← elabTerm heq none let heqType ← inferType heq let heqType ← instantiateMVars heqType match (← Meta.matchEq? heqType) with | none => throwError! "invalid `▸` notation, argument{indentExpr heq}\nhas type{indentExpr heqType}\nequality expected" | some (α, lhs, rhs) => let mut lhs := lhs let mut rhs := rhs let mkMotive (typeWithLooseBVar : Expr) := withLocalDeclD (← mkFreshUserName `x) α fun x => do mkLambdaFVars #[x] $ typeWithLooseBVar.instantiate1 x let mut expectedAbst ← kabstract expectedType rhs unless expectedAbst.hasLooseBVars do expectedAbst ← kabstract expectedType lhs unless expectedAbst.hasLooseBVars do throwError! "invalid `▸` notation, expected type{indentExpr expectedType}\ndoes contain equation left-hand-side nor right-hand-side{indentExpr heqType}" heq ← mkEqSymm heq (lhs, rhs) := (rhs, lhs) let hExpectedType := expectedAbst.instantiate1 lhs let h ← withRef h do let h ← elabTerm h hExpectedType try ensureHasType hExpectedType h catch ex => -- if `rhs` occurs in `hType`, we try to apply `heq` to `h` too let hType ← inferType h let hTypeAbst ← kabstract hType rhs unless hTypeAbst.hasLooseBVars do throw ex let hTypeNew := hTypeAbst.instantiate1 lhs unless (← isDefEq hExpectedType hTypeNew) do throw ex mkEqNDRec (← mkMotive hTypeAbst) h (← mkEqSymm heq) mkEqNDRec (← mkMotive expectedAbst) h heq | _ => throwUnsupportedSyntax @[builtinTermElab stateRefT] def elabStateRefT : TermElab := fun stx _ => do let σ ← elabType stx[1] let mut m := stx[2] if m.getKind == `Lean.Parser.Term.macroDollarArg then m := m[1] let m ← elabTerm m (← mkArrow (mkSort levelOne) (mkSort levelOne)) let ω ← mkFreshExprMVar (mkSort levelOne) let stWorld ← mkAppM `STWorld #[ω, m] mkInstMVar stWorld mkAppM `StateRefT' #[ω, σ, m] end Lean.Elab.Term
f8b8565c001e6c311dc822fc044d7c17edc16490
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/cases_bug.lean
c50af39a00d31b3d52af0139e73ce5846a7e9b9a
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
123
lean
theorem cast_heq₂ : ∀ {A B : Type} (H : A = B) (a : A), cast H a == a | A .A (eq.refl .A) a := heq_of_eq $ cast_eq _ _
b54bd150c4c01dc757c24e6aa495ab0c8d2a61a7
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/ring_theory/power_basis.lean
349cf5064473d411ce011da31f382cdbc1091149
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
20,907
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.minpoly /-! # 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`. For example, if `x` is algebraic over a ring/field, adjoining `x` gives a `power_basis` structure generated by `x`. ## 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). * `finrank (hf : f ≠ 0) : finite_dimensional.finrank 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 `comm_ring` with `is_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 open_locale 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*} [comm_ring A] [comm_ring B] [is_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_nonempty_instance] structure power_basis (R S : Type*) [comm_ring R] [ring S] [algebra R S] := (gen : S) (dim : ℕ) (basis : basis (fin dim) R S) (basis_eq_pow : ∀ i, basis i = gen ^ (i : ℕ)) -- this is usually not needed because of `basis_eq_pow` but can be needed in some cases; -- in such circumstances, add it manually using `@[simps dim gen basis]`. initialize_simps_projections power_basis (-basis) namespace power_basis @[simp] lemma coe_basis (pb : power_basis R S) : ⇑pb.basis = λ (i : fin pb.dim), pb.gen ^ (i : ℕ) := funext pb.basis_eq_pow /-- 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.basis lemma finrank [algebra K S] (pb : power_basis K S) : finite_dimensional.finrank K S = pb.dim := by rw [finite_dimensional.finrank_eq_card_basis pb.basis, fintype.card_fin] lemma mem_span_pow' {x y : S} {d : ℕ} : y ∈ submodule.span R (set.range (λ (i : fin d), x ^ (i : ℕ))) ↔ ∃ f : R[X], 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⟩⟩ }, simp only [this, finsupp.mem_span_image_iff_total, degree_lt_iff_coeff_zero, exists_iff_exists_finsupp, coeff, aeval, eval₂_ring_hom', eval₂_eq_sum, polynomial.sum, support, finsupp.mem_supported', finsupp.total, finsupp.sum, algebra.smul_def, eval₂_zero, exists_prop, linear_map.id_coe, eval₂_one, id.def, not_lt, finsupp.coe_lsum, linear_map.coe_smul_right, finset.mem_range, alg_hom.coe_mk, finset.mem_coe], simp_rw [@eq_comm _ y], exact iff.rfl end lemma mem_span_pow {x y : S} {d : ℕ} (hd : d ≠ 0) : y ∈ submodule.span R (set.range (λ (i : fin d), x ^ (i : ℕ))) ↔ ∃ f : R[X], 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_coe d }, simpa only [degree_eq_nat_degree hf, with_bot.coe_lt_coe] using h }, end lemma dim_ne_zero [h : nontrivial S] (pb : power_basis R S) : pb.dim ≠ 0 := λ h, not_nonempty_iff.mpr (h.symm ▸ fin.is_empty : is_empty (fin pb.dim)) pb.basis.index_nonempty lemma dim_pos [nontrivial S] (pb : power_basis R S) : 0 < pb.dim := nat.pos_of_ne_zero pb.dim_ne_zero lemma exists_eq_aeval [nontrivial S] (pb : power_basis R S) (y : S) : ∃ f : R[X], f.nat_degree < pb.dim ∧ y = aeval pb.gen f := (mem_span_pow pb.dim_ne_zero).mp (by simpa using pb.basis.mem_span y) lemma exists_eq_aeval' (pb : power_basis R S) (y : S) : ∃ f : R[X], y = aeval pb.gen f := begin nontriviality S, obtain ⟨f, _, hf⟩ := exists_eq_aeval pb y, exact ⟨f, hf⟩ end lemma alg_hom_ext {S' : Type*} [semiring S'] [algebra R S'] (pb : power_basis R S) ⦃f g : S →ₐ[R] S'⦄ (h : f pb.gen = g pb.gen) : f = g := begin ext x, obtain ⟨f, rfl⟩ := pb.exists_eq_aeval' x, rw [← polynomial.aeval_alg_hom_apply, ← polynomial.aeval_alg_hom_apply, h] end 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_minpoly` shows its degree is indeed minimal. -/ noncomputable def minpoly_gen (pb : power_basis A S) : A[X] := X ^ pb.dim - ∑ (i : fin pb.dim), C (pb.basis.repr (pb.gen ^ pb.dim) i) * X ^ (i : ℕ) @[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.basis.total_repr (pb.gen ^ pb.dim)).symm.trans _), rw [finsupp.total_apply, finsupp.sum_fintype]; simp only [pb.coe_basis, zero_smul, eq_self_iff_true, implies_true_iff] end lemma dim_le_nat_degree_of_root (h : power_basis A S) {p : A[X]} (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.basis.linear_independent _ _ _ 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, { simp_rw [coe_basis, p_coeff, fin.coe_mk] }, { rw [coeff_eq_zero_of_nat_degree_lt (lt_of_lt_of_le hlt (le_of_not_gt hi)), zero_smul] } end lemma dim_le_degree_of_root (h : power_basis A S) {p : A[X]} (ne_zero : p ≠ 0) (root : aeval h.gen p = 0) : ↑h.dim ≤ p.degree := by { rw [degree_eq_nat_degree ne_zero, with_bot.coe_le_coe], exact h.dim_le_nat_degree_of_root ne_zero root } variables [is_domain A] @[simp] lemma degree_minpoly_gen (pb : power_basis A S) : degree (minpoly_gen pb) = pb.dim := begin unfold minpoly_gen, rw degree_sub_eq_left_of_degree_lt; rw degree_X_pow, apply degree_sum_fin_lt end @[simp] lemma nat_degree_minpoly_gen (pb : power_basis A S) : nat_degree (minpoly_gen pb) = pb.dim := nat_degree_eq_of_degree_eq_some pb.degree_minpoly_gen lemma minpoly_gen_monic (pb : power_basis A S) : monic (minpoly_gen pb) := begin apply (monic_X_pow _).sub_of_left _, rw degree_X_pow, exact degree_sum_fin_lt _ 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⟩ @[simp] lemma nat_degree_minpoly (pb : power_basis A S) : (minpoly A pb.gen).nat_degree = pb.dim := begin refine le_antisymm _ (dim_le_nat_degree_of_root pb (minpoly.ne_zero pb.is_integral_gen) (minpoly.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 minpoly.min _ _ (minpoly_gen_monic pb) (aeval_minpoly_gen pb) end @[simp] lemma minpoly_gen_eq [algebra K S] (pb : power_basis K S) : pb.minpoly_gen = minpoly K pb.gen := minpoly.unique K pb.gen pb.minpoly_gen_monic pb.aeval_minpoly_gen (λ p p_monic p_root, pb.degree_minpoly_gen.symm ▸ pb.dim_le_degree_of_root p_monic.ne_zero p_root) end minpoly section equiv variables [algebra A S] {S' : Type*} [comm_ring S'] [algebra A S'] lemma nat_degree_lt_nat_degree {p q : R[X]} (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 variables [is_domain A] lemma constr_pow_aeval (pb : power_basis A S) {y : S'} (hy : aeval y (minpoly A pb.gen) = 0) (f : A[X]) : pb.basis.constr A (λ i, y ^ (i : ℕ)) (aeval pb.gen f) = aeval y f := begin rw [← aeval_mod_by_monic_eq_self_of_root (minpoly.monic pb.is_integral_gen) (minpoly.aeval _ _), ← @aeval_mod_by_monic_eq_self_of_root _ _ _ _ _ f _ (minpoly.monic pb.is_integral_gen) y hy], by_cases hf : f %ₘ minpoly A pb.gen = 0, { simp only [hf, alg_hom.map_zero, linear_map.map_zero] }, have : (f %ₘ minpoly A pb.gen).nat_degree < pb.dim, { rw ← pb.nat_degree_minpoly, apply nat_degree_lt_nat_degree hf, exact degree_mod_by_monic_lt _ (minpoly.monic pb.is_integral_gen) }, 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, rw [← fin.coe_mk hi, ← pb.basis_eq_pow ⟨i, hi⟩, basis.constr_basis] end lemma constr_pow_gen (pb : power_basis A S) {y : S'} (hy : aeval y (minpoly A pb.gen) = 0) : pb.basis.constr A (λ 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 (minpoly A pb.gen) = 0) (x : A) : pb.basis.constr A (λ 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 (pb : power_basis A S) {y : S'} (hy : aeval y (minpoly A pb.gen) = 0) (x x' : S) : pb.basis.constr A (λ i, y ^ (i : ℕ)) (x * x') = pb.basis.constr A (λ i, y ^ (i : ℕ)) x * pb.basis.constr A (λ i, y ^ (i : ℕ)) x' := begin obtain ⟨f, rfl⟩ := pb.exists_eq_aeval' x, obtain ⟨g, 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`. See `power_basis.lift_equiv` for a bundled equiv sending `⟨y, hy⟩` to the algebra map. -/ noncomputable def lift (pb : power_basis A S) (y : S') (hy : aeval y (minpoly A pb.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.basis.constr A (λ i, y ^ (i : ℕ)) } @[simp] lemma lift_gen (pb : power_basis A S) (y : S') (hy : aeval y (minpoly A pb.gen) = 0) : pb.lift y hy pb.gen = y := pb.constr_pow_gen hy @[simp] lemma lift_aeval (pb : power_basis A S) (y : S') (hy : aeval y (minpoly A pb.gen) = 0) (f : A[X]) : pb.lift y hy (aeval pb.gen f) = aeval y f := pb.constr_pow_aeval hy f /-- `pb.lift_equiv` states that roots of the minimal polynomial of `pb.gen` correspond to maps sending `pb.gen` to that root. This is the bundled equiv version of `power_basis.lift`. If the codomain of the `alg_hom`s is an integral domain, then the roots form a multiset, see `lift_equiv'` for the corresponding statement. -/ @[simps] noncomputable def lift_equiv (pb : power_basis A S) : (S →ₐ[A] S') ≃ {y : S' // aeval y (minpoly A pb.gen) = 0} := { to_fun := λ f, ⟨f pb.gen, by rw [aeval_alg_hom_apply, minpoly.aeval, f.map_zero]⟩, inv_fun := λ y, pb.lift y y.2, left_inv := λ f, pb.alg_hom_ext $ lift_gen _ _ _, right_inv := λ y, subtype.ext $ lift_gen _ _ y.prop } /-- `pb.lift_equiv'` states that elements of the root set of the minimal polynomial of `pb.gen` correspond to maps sending `pb.gen` to that root. -/ @[simps {fully_applied := ff}] noncomputable def lift_equiv' (pb : power_basis A S) : (S →ₐ[A] B) ≃ {y : B // y ∈ ((minpoly A pb.gen).map (algebra_map A B)).roots} := pb.lift_equiv.trans ((equiv.refl _).subtype_equiv (λ x, begin rw [mem_roots, is_root.def, equiv.refl_apply, ← eval₂_eq_eval_map, ← aeval_def], exact map_monic_ne_zero (minpoly.monic pb.is_integral_gen) end)) /-- There are finitely many algebra homomorphisms `S →ₐ[A] B` if `S` is of the form `A[x]` and `B` is an integral domain. -/ noncomputable def alg_hom.fintype (pb : power_basis A S) : fintype (S →ₐ[A] B) := by letI := classical.dec_eq B; exact fintype.of_equiv _ pb.lift_equiv'.symm local attribute [irreducible] power_basis.lift /-- `pb.equiv_of_root pb' h₁ h₂` is an equivalence of algebras with the same power basis, where "the same" means that `pb` is a root of `pb'`s minimal polynomial and vice versa. See also `power_basis.equiv_of_minpoly` which takes the hypothesis that the minimal polynomials are identical. -/ @[simps apply {attrs := []}] noncomputable def equiv_of_root (pb : power_basis A S) (pb' : power_basis A S') (h₁ : aeval pb.gen (minpoly A pb'.gen) = 0) (h₂ : aeval pb'.gen (minpoly A pb.gen) = 0) : S ≃ₐ[A] S' := alg_equiv.of_alg_hom (pb.lift pb'.gen h₂) (pb'.lift pb.gen h₁) (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_of_root_aeval (pb : power_basis A S) (pb' : power_basis A S') (h₁ : aeval pb.gen (minpoly A pb'.gen) = 0) (h₂ : aeval pb'.gen (minpoly A pb.gen) = 0) (f : A[X]) : pb.equiv_of_root pb' h₁ h₂ (aeval pb.gen f) = aeval pb'.gen f := pb.lift_aeval _ h₂ _ @[simp] lemma equiv_of_root_gen (pb : power_basis A S) (pb' : power_basis A S') (h₁ : aeval pb.gen (minpoly A pb'.gen) = 0) (h₂ : aeval pb'.gen (minpoly A pb.gen) = 0) : pb.equiv_of_root pb' h₁ h₂ pb.gen = pb'.gen := pb.lift_gen _ h₂ @[simp] lemma equiv_of_root_symm (pb : power_basis A S) (pb' : power_basis A S') (h₁ : aeval pb.gen (minpoly A pb'.gen) = 0) (h₂ : aeval pb'.gen (minpoly A pb.gen) = 0) : (pb.equiv_of_root pb' h₁ h₂).symm = pb'.equiv_of_root pb h₂ h₁ := rfl /-- `pb.equiv_of_minpoly pb' h` is an equivalence of algebras with the same power basis, where "the same" means that they have identical minimal polynomials. See also `power_basis.equiv_of_root` which takes the hypothesis that each generator is a root of the other basis' minimal polynomial; `power_basis.equiv_root` is more general if `A` is not a field. -/ @[simps apply {attrs := []}] noncomputable def equiv_of_minpoly (pb : power_basis A S) (pb' : power_basis A S') (h : minpoly A pb.gen = minpoly A pb'.gen) : S ≃ₐ[A] S' := pb.equiv_of_root pb' (h ▸ minpoly.aeval _ _) (h.symm ▸ minpoly.aeval _ _) @[simp] lemma equiv_of_minpoly_aeval (pb : power_basis A S) (pb' : power_basis A S') (h : minpoly A pb.gen = minpoly A pb'.gen) (f : A[X]) : pb.equiv_of_minpoly pb' h (aeval pb.gen f) = aeval pb'.gen f := pb.equiv_of_root_aeval pb' _ _ _ @[simp] lemma equiv_of_minpoly_gen (pb : power_basis A S) (pb' : power_basis A S') (h : minpoly A pb.gen = minpoly A pb'.gen) : pb.equiv_of_minpoly pb' h pb.gen = pb'.gen := pb.equiv_of_root_gen pb' _ _ @[simp] lemma equiv_of_minpoly_symm (pb : power_basis A S) (pb' : power_basis A S') (h : minpoly A pb.gen = minpoly A pb'.gen) : (pb.equiv_of_minpoly pb' h).symm = pb'.equiv_of_minpoly pb h.symm := rfl end equiv end power_basis open power_basis /-- Useful lemma to show `x` generates a power basis: the powers of `x` less than the degree of `x`'s minimal polynomial are linearly independent. -/ lemma is_integral.linear_independent_pow [algebra K S] {x : S} (hx : is_integral K x) : linear_independent K (λ (i : fin (minpoly K x).nat_degree), x ^ (i : ℕ)) := begin rw linear_independent_iff, intros p hp, set f : K[X] := p.sum (λ i, monomial i) with hf0, have f_def : ∀ (i : fin _), f.coeff i = p i, { intro i, simp only [f, finsupp.sum, coeff_monomial, finset_sum_coeff], rw [finset.sum_eq_single, if_pos rfl], { intros b _ hb, rw if_neg (mt (λ h, _) hb), exact fin.coe_injective h }, { intro hi, rw if_pos rfl, 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⟩ }, simp only [f, finsupp.sum, coeff_monomial, finset_sum_coeff], 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 : (minpoly K x).degree ≤ f.degree, { apply minpoly.degree_le_of_ne_zero K x hf, convert h, simp_rw [finsupp.total_apply, aeval_def, hf0, finsupp.sum, eval₂_finset_sum], apply finset.sum_congr rfl, rintro i -, simp only [algebra.smul_def, eval₂_monomial] }, have : ¬ (minpoly K x).degree ≤ f.degree, { apply not_le_of_lt, rw [degree_eq_nat_degree (minpoly.ne_zero hx), degree_lt_iff_coeff_zero], intros i hi, rw [f_def' i, dif_neg], exact hi.not_lt }, contradiction end lemma is_integral.mem_span_pow [nontrivial R] {x y : S} (hx : is_integral R x) (hy : ∃ f : R[X], y = aeval x f) : y ∈ submodule.span R (set.range (λ (i : fin (minpoly R x).nat_degree), x ^ (i : ℕ))) := begin obtain ⟨f, rfl⟩ := hy, apply mem_span_pow'.mpr _, have := minpoly.monic hx, refine ⟨f.mod_by_monic (minpoly R x), lt_of_lt_of_le (degree_mod_by_monic_lt _ this) degree_le_nat_degree, _⟩, conv_lhs { rw ← mod_by_monic_add_div f this }, simp only [add_zero, zero_mul, minpoly.aeval, aeval_add, alg_hom.map_mul] end namespace power_basis section map variables {S' : Type*} [comm_ring S'] [algebra R S'] /-- `power_basis.map pb (e : S ≃ₐ[R] S')` is the power basis for `S'` generated by `e pb.gen`. -/ @[simps dim gen basis] noncomputable def map (pb : power_basis R S) (e : S ≃ₐ[R] S') : power_basis R S' := { dim := pb.dim, basis := pb.basis.map e.to_linear_equiv, gen := e pb.gen, basis_eq_pow := λ i, by rw [basis.map_apply, pb.basis_eq_pow, e.to_linear_equiv_apply, e.map_pow] } variables [algebra A S] [algebra A S'] @[simp] lemma minpoly_gen_map (pb : power_basis A S) (e : S ≃ₐ[A] S') : (pb.map e).minpoly_gen = pb.minpoly_gen := by { dsimp only [minpoly_gen, map_dim], -- Turn `fin (pb.map e).dim` into `fin pb.dim` simp only [linear_equiv.trans_apply, map_basis, basis.map_repr, map_gen, alg_equiv.to_linear_equiv_apply, e.to_linear_equiv_symm, alg_equiv.map_pow, alg_equiv.symm_apply_apply, sub_right_inj] } variables [is_domain A] @[simp] lemma equiv_of_root_map (pb : power_basis A S) (e : S ≃ₐ[A] S') (h₁ h₂) : pb.equiv_of_root (pb.map e) h₁ h₂ = e := by { ext x, obtain ⟨f, rfl⟩ := pb.exists_eq_aeval' x, simp [aeval_alg_equiv] } @[simp] lemma equiv_of_minpoly_map (pb : power_basis A S) (e : S ≃ₐ[A] S') (h : minpoly A pb.gen = minpoly A (pb.map e).gen) : pb.equiv_of_minpoly (pb.map e) h = e := pb.equiv_of_root_map _ _ _ end map section adjoin open algebra lemma adjoin_gen_eq_top (B : power_basis R S) : adjoin R ({B.gen} : set S) = ⊤ := begin rw [← to_submodule_eq_top, _root_.eq_top_iff, ← B.basis.span_eq, submodule.span_le], rintros x ⟨i, rfl⟩, rw [B.basis_eq_pow i], exact subalgebra.pow_mem _ (subset_adjoin (set.mem_singleton _)) _, end lemma adjoin_eq_top_of_gen_mem_adjoin {B : power_basis R S} {x : S} (hx : B.gen ∈ adjoin R ({x} : set S)) : adjoin R ({x} : set S) = ⊤ := begin rw [_root_.eq_top_iff, ← B.adjoin_gen_eq_top], refine adjoin_le _, simp [hx], end end adjoin end power_basis
cdc0c51874e5c2826a658cd2f918a14a11e20b27
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/normed_space/complemented.lean
331fcda1459c8f6476b56e0792c1293299440164
[ "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
5,821
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.normed_space.banach import analysis.normed_space.finite_dimension /-! # Complemented subspaces of normed vector spaces A submodule `p` of a topological module `E` over `R` is called *complemented* if there exists a continuous linear projection `f : E →ₗ[R] p`, `∀ x : p, f x = x`. We prove that for a closed subspace of a normed space this condition is equivalent to existence of a closed subspace `q` such that `p ⊓ q = ⊥`, `p ⊔ q = ⊤`. We also prove that a subspace of finite codimension is always a complemented subspace. ## Tags complemented subspace, normed vector space -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] {G : Type*} [normed_group G] [normed_space 𝕜 G] noncomputable theory namespace continuous_linear_map section variables [complete_space 𝕜] lemma ker_closed_complemented_of_finite_dimensional_range (f : E →L[𝕜] F) [finite_dimensional 𝕜 f.range] : f.ker.closed_complemented := begin set f' : E →L[𝕜] f.range := f.cod_restrict _ (f : E →ₗ[𝕜] F).mem_range_self, rcases f'.exists_right_inverse_of_surjective (f : E →ₗ[𝕜] F).range_range_restrict with ⟨g, hg⟩, simpa only [ker_cod_restrict] using f'.closed_complemented_ker_of_right_inverse g (ext_iff.1 hg) end end variables [complete_space E] [complete_space (F × G)] /-- If `f : E →L[R] F` and `g : E →L[R] G` are two surjective linear maps and their kernels are complement of each other, then `x ↦ (f x, g x)` defines a linear equivalence `E ≃L[R] F × G`. -/ def equiv_prod_of_surjective_of_is_compl (f : E →L[𝕜] F) (g : E →L[𝕜] G) (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) : E ≃L[𝕜] F × G := ((f : E →ₗ[𝕜] F).equiv_prod_of_surjective_of_is_compl ↑g hf hg hfg).to_continuous_linear_equiv_of_continuous (f.continuous.prod_mk g.continuous) @[simp] lemma coe_equiv_prod_of_surjective_of_is_compl {f : E →L[𝕜] F} {g : E →L[𝕜] G} (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) : (equiv_prod_of_surjective_of_is_compl f g hf hg hfg : E →ₗ[𝕜] F × G) = f.prod g := rfl @[simp] lemma equiv_prod_of_surjective_of_is_compl_to_linear_equiv {f : E →L[𝕜] F} {g : E →L[𝕜] G} (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) : (equiv_prod_of_surjective_of_is_compl f g hf hg hfg).to_linear_equiv = linear_map.equiv_prod_of_surjective_of_is_compl f g hf hg hfg := rfl @[simp] lemma equiv_prod_of_surjective_of_is_compl_apply {f : E →L[𝕜] F} {g : E →L[𝕜] G} (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) (x : E): equiv_prod_of_surjective_of_is_compl f g hf hg hfg x = (f x, g x) := rfl end continuous_linear_map namespace subspace variables [complete_space E] (p q : subspace 𝕜 E) open continuous_linear_map (subtype_val) /-- If `q` is a closed complement of a closed subspace `p`, then `p × q` is continuously isomorphic to `E`. -/ def prod_equiv_of_closed_compl (h : is_compl p q) (hp : is_closed (p : set E)) (hq : is_closed (q : set E)) : (p × q) ≃L[𝕜] E := begin haveI := hp.complete_space_coe, haveI := hq.complete_space_coe, refine (p.prod_equiv_of_is_compl q h).to_continuous_linear_equiv_of_continuous _, exact ((subtype_val p).coprod (subtype_val q)).continuous end /-- Projection to a closed submodule along a closed complement. -/ def linear_proj_of_closed_compl (h : is_compl p q) (hp : is_closed (p : set E)) (hq : is_closed (q : set E)) : E →L[𝕜] p := (continuous_linear_map.fst 𝕜 p q) ∘L ↑(prod_equiv_of_closed_compl p q h hp hq).symm variables {p q} @[simp] lemma coe_prod_equiv_of_closed_compl (h : is_compl p q) (hp : is_closed (p : set E)) (hq : is_closed (q : set E)) : ⇑(p.prod_equiv_of_closed_compl q h hp hq) = p.prod_equiv_of_is_compl q h := rfl @[simp] lemma coe_prod_equiv_of_closed_compl_symm (h : is_compl p q) (hp : is_closed (p : set E)) (hq : is_closed (q : set E)) : ⇑(p.prod_equiv_of_closed_compl q h hp hq).symm = (p.prod_equiv_of_is_compl q h).symm := rfl @[simp] lemma coe_continuous_linear_proj_of_closed_compl (h : is_compl p q) (hp : is_closed (p : set E)) (hq : is_closed (q : set E)) : (p.linear_proj_of_closed_compl q h hp hq : E →ₗ[𝕜] p) = p.linear_proj_of_is_compl q h := rfl @[simp] lemma coe_continuous_linear_proj_of_closed_compl' (h : is_compl p q) (hp : is_closed (p : set E)) (hq : is_closed (q : set E)) : ⇑(p.linear_proj_of_closed_compl q h hp hq) = p.linear_proj_of_is_compl q h := rfl lemma closed_complemented_of_closed_compl (h : is_compl p q) (hp : is_closed (p : set E)) (hq : is_closed (q : set E)) : p.closed_complemented := ⟨p.linear_proj_of_closed_compl q h hp hq, submodule.linear_proj_of_is_compl_apply_left h⟩ lemma closed_complemented_iff_has_closed_compl : p.closed_complemented ↔ is_closed (p : set E) ∧ ∃ (q : subspace 𝕜 E) (hq : is_closed (q : set E)), is_compl p q := ⟨λ h, ⟨h.is_closed, h.has_closed_complement⟩, λ ⟨hp, ⟨q, hq, hpq⟩⟩, closed_complemented_of_closed_compl hpq hp hq⟩ lemma closed_complemented_of_quotient_finite_dimensional [complete_space 𝕜] [finite_dimensional 𝕜 (E ⧸ p)] (hp : is_closed (p : set E)) : p.closed_complemented := begin obtain ⟨q, hq⟩ : ∃ q, is_compl p q := p.exists_is_compl, haveI : finite_dimensional 𝕜 q := (p.quotient_equiv_of_is_compl q hq).finite_dimensional, exact closed_complemented_of_closed_compl hq hp q.closed_of_finite_dimensional end end subspace
872c29edefcd7195247e1d2682bfe4faad5720de
3dd1b66af77106badae6edb1c4dea91a146ead30
/library/standard/if.lean
bb3f163290a3074b0a0f5a9143e104e0c3f5834f
[ "Apache-2.0" ]
permissive
silky/lean
79c20c15c93feef47bb659a2cc139b26f3614642
df8b88dca2f8da1a422cb618cd476ef5be730546
refs/heads/master
1,610,737,587,697
1,406,574,534,000
1,406,574,534,000
22,362,176
1
0
null
null
null
null
UTF-8
Lean
false
false
1,296
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Leonardo de Moura import decidable tactic using decidable tactic definition ite (c : Prop) {H : decidable c} {A : Type} (t e : A) : A := rec_on H (assume Hc, t) (assume Hnc, e) notation `if` c `then` t `else` e:45 := ite c t e theorem if_pos {c : Prop} {H : decidable c} (Hc : c) {A : Type} (t e : A) : (if c then t else e) = t := decidable_rec (assume Hc : c, refl (@ite c (inl Hc) A t e)) (assume Hnc : ¬c, absurd_elim (@ite c (inr Hnc) A t e = t) Hc Hnc) H theorem if_neg {c : Prop} {H : decidable c} (Hnc : ¬c) {A : Type} (t e : A) : (if c then t else e) = e := decidable_rec (assume Hc : c, absurd_elim (@ite c (inl Hc) A t e = e) Hc Hnc) (assume Hnc : ¬c, refl (@ite c (inr Hnc) A t e)) H theorem if_t_t (c : Prop) {H : decidable c} {A : Type} (t : A) : (if c then t else t) = t := decidable_rec (assume Hc : c, refl (@ite c (inl Hc) A t t)) (assume Hnc : ¬c, refl (@ite c (inr Hnc) A t t)) H theorem if_true {A : Type} (t e : A) : (if true then t else e) = t := if_pos trivial t e theorem if_false {A : Type} (t e : A) : (if false then t else e) = e := if_neg not_false_trivial t e
964ade250fd5e5dccd2027847b0e0804e0a1546c
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/keyAttrErase.lean
9a530e576b8517007ca693068916794523781678
[ "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
123
lean
theorem ex {i j : Fin n} (h : i = j) : i.val = j.val := h ▸ rfl attribute [-appUnexpander] unexpandEqNDRec #print ex
482a73be5503fcf713a53668eb923162dbff079a
80162757f50b09d3cad5564907e4c9b00742e045
/rb.lean
d0c662d06777fc6187099814a2bbe1505eb37ae4
[]
no_license
EdAyers/edlib
cc30d0a54fed347a85b6df6045f68e6b48bc71a3
78b8c5d91f023f939c102837d748868e2f3ed27d
refs/heads/master
1,586,459,758,216
1,571,322,179,000
1,571,322,179,000
160,538,917
2
0
null
null
null
null
UTF-8
Lean
false
false
21,486
lean
import .order .split_ifs namespace rb universes u v w inductive col |Red|Black inductive node (k : Type u) (α : Type v) : Type max u v |Leaf {}: node |Node (c:col) (l:node) (v:k×α) (r:node) : node open node col notation `Rd` := (Node Red) notation `Bk` := (Node Black) namespace node variables {k : Type u} [has_lt k] [decidable_rel ((<) : k → k → Prop)] variables {α : Type v} def empty : node k α := Leaf instance : has_emptyc (node k α) := ⟨empty⟩ def mk_black : node k α → node k α |(Leaf) := Leaf |(Node _ l a r) := Node Black l a r def mk_red : node k α → node k α |Leaf := Leaf |(Node _ l a r) := Node Red l a r @[simp] def lbal : node k α → k×α → node k α → node k α | (Rd (Rd a x b) y c) v r := Rd (Bk a x b) y (Bk c v r) | (Rd a x (Rd b y c)) v r := Rd (Bk a x b) y (Bk c v r) | l v r := Bk l v r variables (a b c r : node k α) (x y v : k×α) def rbal : node k α → k×α → node k α → node k α | l v (Rd (Rd b w c) z d) := Rd (Bk l v b) w (Bk c z d) | l v (Rd b w (Rd c z d)) := Rd (Bk l v b) w (Bk c z d) | l v r := Bk l v r /--Same as `rbal` but cases swapped.-/ def rbal' : node k α → k×α → node k α → node k α | l v (Rd b w (Rd c z d)) := Rd (Bk l v b) w (Bk c z d) | l v (Rd (Rd b w c) z d) := Rd (Bk l v b) w (Bk c z d) | l v r := Bk l v r def lbalS : node k α → k×α → node k α → node k α | (Rd a x b) v r := Rd (Bk a x b) v r | l v (Bk a w b) := rbal' l v (Rd a w b) | l v (Rd (Bk a w b) z c) := Rd (Bk l v a) w (rbal' b z (mk_red c)) | l v r := Rd l v r /- impossible -/ def rbalS : node k α → k×α → node k α → node k α | l v (Rd b w c) := Rd l v (Bk b w c) | (Bk a v b) w r := lbal (Rd a v b) w r | (Rd a v₁ (Bk b v₂ c)) v₃ r := Rd (lbal (mk_red a) v₁ b) v₂ (Bk c v₃ r) | l v r := Rd l v r /- impossible -/ def ins_aux (key : k) (a : α) : node k α → node k α |Leaf := Rd Leaf ⟨key,a⟩ Leaf |(Rd l v r) := -- l and r are both Black. if key < v.1 then Rd (ins_aux l) v r -- [FIXME] `Leaf` is `Black`, but `ins_aux Leaf` is `Red`, but (Red (Red)) is not wf. else if key > v.1 then Rd l v (ins_aux r) -- R else Rd l ⟨key,a⟩ r -- R |(Bk l v r) := if key < v.1 then lbal (ins_aux l) v r -- BR else if key > v.1 then rbal l v (ins_aux r) --BR else Bk l ⟨key,a⟩ r -- B def insert : k → α → node k α → node k α := λ key x s, mk_black (ins_aux key x s) instance : has_insert (k×α) (node k α) := ⟨λ ⟨key,a⟩ t, insert key a t⟩ /--Used to get the `append` method to be well-founded-/ instance custom_wf : has_well_founded (node k α × node k α) := has_well_founded_of_has_sizeof (node k α × node k α) def append : (node k α × node k α) → node k α |⟨Leaf, r⟩ := r |⟨ l, Leaf⟩ := l |⟨(Rd ll lx lr),(Rd rl rx rr) ⟩ := match append ⟨lr, rl⟩ with |Rd lr x rl := Rd (Rd ll lx lr) x (Rd rl rx rr) |lrl := Rd ll lx (Rd lrl rx rr) end |⟨ (Bk ll lx lr) ,(Bk rl rx rr) ⟩ := match append ⟨lr, rl⟩ with |Rd lr x rl := Rd (Bk ll lx lr) x (Bk rl rx rr) |lrl := lbalS ll lx (Bk lrl rx rr) end |⟨(Rd ll lx lr), r⟩ := Rd ll lx (append ⟨lr, r⟩) |⟨ l, (Rd rl rx rr)⟩ := Rd (append ⟨l, rl⟩) rx rr def erase_aux (key : k) : node k α → node k α |Leaf := Leaf |(Node _ l y r) := if key < y.1 then match l with | (Bk _ _ _) := lbalS (erase_aux l) y r | _ := Rd (erase_aux l) y r end else if key > y.1 then match r with | (Bk _ _ _) := rbalS l y (erase_aux r) | _ := Rd l y (erase_aux r) end else append ⟨l, r⟩ def erase (key : k) (t : node k α) : node k α := mk_black (erase_aux key t) def pop_min_aux : node k α → k×α → node k α → k × α × node k α |Leaf ⟨k,x⟩ r := ⟨k,x,r⟩ |(Node lc ll lx lr) y r := let ⟨k,x,l⟩ := pop_min_aux ll lx lr in match lc with |Black := ⟨k,x, lbalS l y r⟩ |Red := ⟨k,x, Rd l y r⟩ end /-- Remove the minimal element and key from the table. -/ def pop_min : node k α → option (k × α × node k α) |Leaf := none |(Node _ l y r) := let ⟨k,x,t⟩ := pop_min_aux l y r in some ⟨k,x, mk_black t⟩ def fold {β : Type w} (f : k → α → β → β) : β → node k α → β |b Leaf := b |b (Node _ l ⟨k,a⟩ r) := fold (f k a $ fold b l) r def mfold {T : Type u → Type u} [monad T] {β} (f : k → α → β → T β) : β → node k α → T β |b Leaf := pure b |b (Node _ l ⟨k,a⟩ r) := do b ← mfold b l, b ← f k a b, mfold b r /-- Get the number of black nodes between the root and the leaves of the trees. -/ def height : node k α → ℕ |Leaf := 0 |(Rd l _ _ ) := height l |(Bk l _ _ ) := nat.succ $ height l namespace treeify def bogus : node k α × list (k×α) := ⟨Leaf, []⟩ def treeify_t (k : Type u) (α : Type v) : Type max u v := list (k×α) → (node k α × list (k×α)) def treeify_zero : treeify_t k α := λ acc, ⟨Leaf, acc⟩ def treeify_one : treeify_t k α |(x::acc) := ⟨Rd Leaf x Leaf, acc⟩ |_ := bogus def treeify_cont (f g : treeify_t k α) : treeify_t k α := λ acc, match f acc with |⟨l, x::acc⟩ := let ⟨r, acc⟩ := g acc in ⟨Bk l x r, acc⟩ |_ := bogus end def positive := list bool def treeify_aux : bool → positive → treeify_t k α |pred [] := if pred then treeify_zero else treeify_one |pred (ff::n) := treeify_cont (treeify_aux pred n) (treeify_aux tt n) |pred (tt::n) := treeify_cont (treeify_aux ff n) (treeify_aux pred n) def succ : positive → positive |[] := [ff] |(ff::tail) := tt::tail |(tt::tail) := ff :: (succ tail) def plength_aux : list α → positive → positive |[] p := p |(_::t) p := plength_aux t $ succ p def plength (l : list α) := plength_aux l [] end treeify /--Take an __ordered__ list and convert it to a node tree.-/ def treeify (l : list (k×α)) : node k α := prod.fst $ treeify.treeify_aux tt (treeify.plength l) l def filter (p : k → α → bool) : node k α → node k α := treeify ∘ fold (λ key a l, ite (p key a) (⟨key,a⟩::l) l) [] def get (key:k) : node k α → option α |Leaf := none |(Node _ l y r) := if key < y.1 then get l else if y.1 < key then get r else some (y.2) def contains (key:k): node k α → bool := option.is_some ∘ get key instance : has_mem (k) (node k α) := ⟨λ key t, contains key t⟩ def min : node k α → option (k×α) |Leaf := none |(Node _ l x _) := (min l) <|> some x def max : node k α → option (k× α ) |Leaf := none |(Node _ _ x r) := (max r) <|> some x /-- Asssign each member of `r` to `l`, if there is a key clash then choose the entry in `r` and clobber `l`. -/ def merge : node k α → node k α → node k α := fold insert def table (k : Type u) : Type u := node k unit def intersect (t₁ t₂ : table k) : table k := if height t₁ < height t₂ then filter (λ k _, contains k t₂) t₁ else filter (λ k _, contains k t₁) t₂ instance : has_inter (table k) := ⟨intersect⟩ def union (l r : table k) : table k := if height l < height r then fold insert r l else fold insert l r instance : has_union (table k) := ⟨union⟩ /--Remove all of the keys found in the table `r` from the dictionary `l`. -/ def subtract (l : node k α) (r : table k) : node k α := fold (λ k _ l, erase k l) l r --instance : has_sub (table k) := ⟨subtract⟩ def map {β : Type w} (f : α → β) : node k α → node k β |Leaf := Leaf |(Node c l ⟨k,a⟩ r) := Node c (map l) ⟨k, f a⟩ (map r) instance : functor (node k) := { map := λ _ _ f t, map f t } def keys_of : (node k α) → table k := map (λ a, ⟨⟩) end node namespace proofs open nat node variables {k : Type u} {α : Type v} variables [decidable_linear_order k] inductive is_rb : node k α → col → nat → Prop |leaf_rb {} : is_rb Leaf Black 0 |red_rb {l v r n} (rb_l : is_rb l Black n) (rb_r : is_rb r Black n) : is_rb (Rd l v r) Red n |black_rb {l c₁ v r c₂ n} (rb_l : is_rb l c₁ n) (rb_r : is_rb r c₂ n) : is_rb(Bk l v r) Black (succ n) inductive mem (key:k) : node k α → Prop |left {c l v r} : mem l → mem (Node c l v r) |mid {c l v r} : (v:k×α).1 = key → mem (Node c l v r) |right {c l v r} : mem r → mem (Node c l v r) instance : has_mem (k) (node k α) := ⟨mem⟩ lemma leaf_empty {key : k} : key ∉ (@Leaf k α) := λ h, by cases h def dominates (k₁ : k) (t : node k α) : Prop := ∀ k₂ ∈ t, k₁ > k₂ def dominated_by (k₁ : k) (t : node k α) : Prop := ∀ k₂ ∈ t, k₁ < k₂ infix ` ⋗ `: 50 := dominates infix ` ⋖ `: 50 := dominated_by inductive ordered : node k α → Prop |o_leaf {} : ordered (Leaf) |o_node {c l} {v:k×α} {r} (ol:ordered l) (vdl : v.1 ⋗ l) (rdv : v.1 ⋖ r) (or : ordered r) : ordered (Node c l v r) lemma ordered.ol {c l v r} : ordered (Node c l v r : node k α) → ordered l := begin intros, cases a, assumption end lemma ordered.or {c l v r} : ordered (Node c l v r : node k α) → ordered r := begin intros, cases a, assumption end open ordered @[simp] def is_wf (t: node k α) : Prop := (∃ n, is_rb t Black n) ∧ ordered t variables {key k₁ k₂ :k} {a:α} {v v₁ v₂ : k × α} {l r t : node k α} {c : col} @[trans] lemma dominates.trans : k₁ > k₂ → k₂ ⋗ t → k₁ ⋗ t := λ p q k₃ kt, lt.trans (q _ kt) p lemma dominates.leaf : k₁ ⋗ (@Leaf k α) := λ k₂ kt, false.rec_on _ $ leaf_empty kt lemma dominates.node (hl : k₁ ⋗ l) (hv : k₁ > v.1) (hr : k₁ ⋗ r) : k₁ ⋗ (Node c l v r) |k₂ (mem.left xl) := hl _ xl |k₂ (mem.mid xm) := xm ▸ hv |k₂ (mem.right xr) := hr _ xr lemma dominates.l : k₁ ⋗ (Node c l v r) → k₁ ⋗ l := λ h k₂ hl, h k₂ (mem.left hl) lemma dominates.r : k₁ ⋗ (Node c l v r) → k₁ ⋗ r := λ h k₂ hr, h k₂ (mem.right hr) lemma dominates.v : k₁ ⋗ (Node c l v r) → k₁ > v.1 := λ h, h v.1 $ mem.mid rfl @[trans] lemma dominated_by.trans : k₁ < k₂ → k₂ ⋖ t → k₁ ⋖ t := λ p q k₃ kt, lt.trans p (q _ kt) lemma dominated_by.leaf : k₁ ⋖ (@Leaf k α) := λ k₂ kt, false.rec_on _ $ leaf_empty kt lemma dominated_by.node (hl : k₁ ⋖ l) (hv : k₁ < v.1) (hr : k₁ ⋖ r) : k₁ ⋖ (Node c l v r) |k₂ (mem.left xl) := hl _ xl |k₂ (mem.mid xm) := xm ▸ hv |k₂ (mem.right xr) := hr _ xr lemma dominated_by.l : k₁ ⋖ (Node c l v r) → k₁ ⋖ l := λ h k₂ hl, h k₂ (mem.left hl) lemma dominated_by.r : k₁ ⋖ (Node c l v r) → k₁ ⋖ r := λ h k₂ hr, h k₂ (mem.right hr) lemma dominated_by.v : k₁ ⋖ (Node c l v r) → k₁ < v.1 := λ h, h v.1 $ mem.mid rfl def all_below := λ (t₁ t₂ : node k α), ∀ (k₁ ∈ t₁) (k₂ ∈ t₂), k₁ < k₂ infix ` ⊏ `: 100 := all_below open tactic /- Look at the target, find all occurences of the name, -/ meta def expand (n : name) : tactic unit := do delta_target [n] meta def get_cases_candidate_single (e : expr) : tactic expr := do --e ← tactic.to_expr pe, s_l ← get_simp_lemmas_or_default none, e ← simp_lemmas.dsimplify s_l [`rec_on, `cases_on] e {fail_if_unchanged := ff}, -- rewrite alternative definitions of recursion. --e ← whnf e, (fn,args) ← pure $ expr.get_app_fn_args e, fn_name ← pure $ expr.const_name fn, env ← get_env, is_rec ← pure $ environment.is_recursor env fn_name, -- hopefully, the last argument of the recursor is always the thing being recursed on. rec_arg ← pure $ expr.app_arg e, is_local ← pure $ expr.is_local_constant rec_arg, -- trace fn_name, -- trace args, -- trace is_rec, -- trace rec_arg, -- trace is_local, -- trace "\n", guard is_rec, guard is_local, pure rec_arg meta def get_cases_candidate : expr → tactic expr := λ e, get_cases_candidate_single e <|> list.any_of (expr.get_app_args e) get_cases_candidate meta def recursion_cases : tactic unit := do t ← target >>= instantiate_mvars, cand ← get_cases_candidate t, --trace cand, tactic.cases_core cand, --dsimp_target none [] {fail_if_unchanged := ff}, all_goals $ try $ dsimp_target, --(dsimp_target none []) <|> pure ⟨⟩, -- args ← pure $ expr.get_app_args e, -- list.any_of args cases_on_variable --tactic.dsimplify (λ e, pure ⟨e,tt⟩) (dsimp_post) t, pure ⟨⟩ #check node.cases_on #check node.rec_on meta def cases_all : expr → tactic (list expr) := λ h, do --h ← get_local h_name, --trace h, ty ← infer_type h >>= instantiate_mvars >>= whnf, --trace ty, --[c] ← get_constructors_for ty | pure [], -- count the number of non-named arguments [(case_name,new_hyps,new_subs)] ← cases_core h | fail "more than one constructor", list.mfoldl (λ l h, list.append l <$> ((cases_all h) <|> (pure [h]))) [] new_hyps meta def one_of : list (tactic unit) → tactic unit |(h::t) := h <|> one_of t |[] := skip meta def apply_pexpr : pexpr → tactic unit := λ p, ((to_expr p) >>= apply) $> ⟨⟩ inductive growth : node k α → nat → Prop |stay {c n t} : is_rb t c n → growth t n |sprout_l {n l v r} : is_rb l Red n → is_rb r Black n → growth (Rd l v r) n |sprout_r {n l v r} : is_rb l Black n → is_rb r Red n → growth (Rd l v r) n /- We have to consider way more cases than the written definition of `rbal` because the equation compiler has to transform the match expression into a series of `cases_on` calls which is less flexible than match. As far as I can tell these are the solutions to avoiding having to do all of the cases - Write a `rbal_ind` helper lemma: like below. - Write some really clever automation that reduces the problem to a few cases for you. - Write your proofs inline with the function - this tends to obfuscate what your underlying data transformation is and makes everything quite bloaty. It is very hard to pull off. -/ lemma rbal_ind {P Q : node k α → Prop} {q : Q r} (c₁ : Π {b c d w z}, Q(Rd (Rd b w c) z d) → P(Rd (Bk l v b) w (Bk c z d))) (c₂ : Π {b c d w z}, Q(Rd b w (Rd c z d)) → P(Rd (Bk l v b) w (Bk c z d))) (c₃ : P(Bk l v r)) : P(rbal l v r) := begin expand ``rbal, dsimp_target none [`id_rhs], repeat{one_of [ recursion_cases, apply_pexpr ```(c₁ q), apply_pexpr ```(c₂ q), apply_pexpr ```(c₃) ]}, end -- lemma ins_aux_ind {P Q : node k α → Prop} (q : Q t) -- (c₁ : P(Rd Leaf ⟨key,a⟩ Leaf)) -- (c₂ : Π {l v r}, Q(Bk l v r) → key < v.1 → P(ins_aux key a l) → P(lbal (ins_aux key a l) v r)) -- (c₃ : Π {l v r}, Q(Bk l v r) → key > v.1 → P(ins_aux key a r) → P(rbal l v (ins_aux key a r))) -- (c₄ : Π {l v r}, Q(Bk l v r) → key = v.1 → P(Bk l ⟨key,a⟩ r)) -- (c₅ : Π {l v r}, Q(Rd l v r) → key < v.1 → P(ins_aux key a l) → P(Rd (ins_aux key a l) v r)) -- (c₆ : Π {l v r}, Q(Rd l v r) → key > v.1 → P(ins_aux key a r) → P(Rd l v (ins_aux key a r))) -- (c₇ : Π {l v r}, Q(Rd l v r) → key = v.1 → P(Rd l ⟨key,a⟩ r)) -- : P(ins_aux key a t) := -- begin -- induction t with c l v r, apply c₁, cases c, -- simp [ins_aux], -- --expand ``ins_aux, simp, dsimp_target none [`id_rhs], -- focus { -- split_ifs, apply c₅ _ _ (t_ih_l _), repeat {assumption}, -- } -- end lemma rbal.ordered (ol : ordered l) (vdl : v.1 ⋗ l) (rdv : v.1 ⋖ r) (or : ordered r) : ordered (rbal l v r) := begin apply rbal_ind, apply and.intro or rdv, focus { intros _ _ _ _ _ a, (get_local `a >>= cases_all), apply o_node (o_node _ _ _ _) _ _ (o_node _ _ _ _), repeat {assumption}, apply a_right.l.l, apply dominates.node, apply dominates.trans, apply a_right.l.v, assumption, apply a_right.l.v, assumption, apply dominated_by.node, assumption, apply a_left_vdl.v, apply dominated_by.trans, apply a_left_vdl.v, assumption, apply a_left_vdl.r }, focus { intros _ _ _ _ _ a, (get_local `a >>= cases_all), apply o_node (o_node _ _ _ _) _ _ (o_node _ _ _ _), repeat {assumption}, apply a_right.l, apply dominates.node, apply dominates.trans, apply a_right.v, assumption, apply a_right.v, assumption, apply dominated_by.node, apply a_left_rdv.l, apply a_left_rdv.v, apply dominated_by.trans, apply a_left_rdv.v, assumption }, focus { apply o_node ol vdl rdv or, } end lemma rbal_mem : (key ∈ r) → (key ∈ rbal l v r) := begin intros, apply rbal_ind, apply a, focus {intros _ _ _ _ _ h, cases h, cases h_a, apply (mem.left $ mem.right _),assumption, apply (mem.mid _), assumption, apply (mem.right $ mem.left _), assumption, apply (mem.right $ mem.mid _), assumption, apply (mem.right $ mem.right _), assumption, }, focus {intros, cases a_1, apply (mem.left $ mem.right _), assumption, apply (mem.mid _), assumption, cases a_1_a, apply (mem.right $ mem.left _), assumption, apply (mem.right $ mem.mid _), assumption, apply (mem.right $ mem.right _), assumption, }, focus { apply mem.right, assumption, } end lemma rbal_rb {cl n} : is_rb l cl n → growth r n → ∃ c', is_rb (rbal l v r) c' (succ n) := begin intros lrb rrg, apply @rbal_ind k _ _ _ _ _ (λ t, ∃ c', is_rb (t) c' (succ n)) (λ t, growth t n), apply rrg, focus {intros, cases a, cases a_a, cases a_a_rb_l, split, apply is_rb.red_rb, apply is_rb.black_rb, assumption, cases a_a, assumption, apply is_rb.black_rb, }, focus {intros, cases a, cases a_rb_r, }, focus {existsi Black, apply is_rb.black_rb, assumption, assumption} end -- [TODO] repeat for lbal. lemma eq_of_nlt {a b : k} : (¬ a < b) → ¬(b < a) → a = b := λ p q, match lt_trichotomy a b with |(or.inl h) := absurd h p |(or.inr (or.inl h)) := h |(or.inr (or.inr h)) := absurd h q end lemma lbal.ordered : ordered l → v.1 ⋗ l → v.1 ⋖ r → ordered r → ordered (lbal l v r) := sorry lemma lbal.mem : (key ∈ l) → (key ∈ lbal l v r) := sorry lemma lbal.rb {cl cr n} : is_rb l cl n → is_rb r cr n → ∃ c', is_rb (lbal l v r) c' (succ n) := sorry lemma ins_aux.mem : (k₁ ∈ ins_aux key a t) ↔ (k₁ ∈ t ∨ k₁ = key) := sorry lemma ins_aux.is_rb {n} : is_rb t c n → ∃ c', is_rb (ins_aux key a t) c' n := begin intro rb, induction rb with l v r n hl hr il ir l c₁ v r c₂ n hl hr il ir, case rb.proofs.is_rb.leaf_rb { simp [ins_aux], split, apply is_rb.red_rb, all_goals {apply is_rb.leaf_rb}, }, case rb.proofs.is_rb.red_rb { simp [ins_aux], split_ifs, split, apply is_rb.red_rb, } end lemma ins_aux.ordered : ordered t → ordered (ins_aux key a t) := begin intro o, induction t with c l v r lq rq, case rb.node.Leaf { simp [ins_aux], apply o_node, assumption, apply dominates.leaf, apply dominated_by.leaf, assumption, }, case rb.node.Node { cases o, have hl := lq o_ol, clear lq, have hr := rq o_or, clear rq, cases c, all_goals {simp[ins_aux] }, case rb.col.Red { split_ifs, apply o_node, any_goals {assumption}, focus { intros k₁ ki, have h₂ := ins_aux.mem.1 ki, cases h₂, apply o_vdl, assumption, cases h₂, assumption, }, split_ifs, focus { apply o_node, any_goals {assumption}, intros k₁ ki, cases (ins_aux.mem.1 ki), apply o_rdv, assumption, cases h_2, assumption }, split_ifs, have e := eq_of_nlt h h_1, subst e, apply o_node, any_goals {assumption} }, case rb.col.Black { split_ifs, apply lbal.ordered, any_goals {assumption}, intros k₁ ki, cases (ins_aux.mem.1 ki), apply o_vdl _ h_1, subst h_1, assumption, split_ifs, apply rbal.ordered, any_goals {assumption}, intros k₁ ki, cases (ins_aux.mem.1 ki), apply o_rdv _ h_2, subst h_2, assumption, split_ifs, have e := eq_of_nlt h h_1, subst e, apply o_node, any_goals {assumption} } } end /- Now that I have worked this out, I am 100% sure that I can write some automation for this, probably in the same vain as auto2 so I don't have to redesign anything. It will be some non-trivial amount of work. -/ lemma empty_is_wf : is_wf (@node.empty k _ _ α) := sorry lemma insert_is_wf :is_wf t → is_wf (insert key a t) := sorry lemma insert_works : is_wf t → get key (insert key a t) = some a := sorry lemma erase_is_wf : is_wf t → is_wf (erase key t) := sorry lemma erase_works : is_wf t → get key (erase key t) = none := sorry /- [TODO] from mathlib -/ inductive sorted (R : α → α → Prop) : list α → Prop |nil {} : sorted [] |cons {a} {l:list α} : (∀ b∈l, R a b) → sorted l → sorted (a::l) lemma treeify_works : ∀ {l : list (k×α)}, sorted ((<) on prod.fst) l → is_wf (treeify l) := sorry lemma filter_works {p} : is_wf t → is_wf (filter p t) := sorry end proofs end rb
0e8e150d7eeddf12f28116e835477d5ee307b4a9
3268ab3a126f0fef71459fbf170dc38efe5d0506
/colimit/pointed.hlean
9b4a3ea7a854a74684b913782b93d98dcac26e2c
[ "Apache-2.0" ]
permissive
soraismus/Spectral
f043fed1a4e02ddfeba531769b2980eb817471f4
32512bf47db3a1b932856e7ed7c7830b1fc07ef0
refs/heads/master
1,585,628,705,579
1,538,609,948,000
1,538,609,974,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,989
hlean
/- pointed sequential colimits -/ -- authors: Floris van Doorn, Egbert Rijke, Stefano Piceghello import .seq_colim types.fin homotopy.chain_complex types.pointed2 open seq_colim pointed algebra eq is_trunc nat is_equiv equiv sigma sigma.ops chain_complex fiber namespace seq_colim definition pseq_diagram [reducible] (A : ℕ → Type*) : Type := Πn, A n →* A (succ n) definition pseq_colim [constructor] {X : ℕ → Type*} (f : pseq_diagram X) : Type* := pointed.MK (seq_colim f) (@sι _ _ 0 pt) variables {A A' : ℕ → Type*} {f : pseq_diagram A} {f' : pseq_diagram A'} {τ : Πn, A n →* A' n} {H : Πn, τ (n+1) ∘* f n ~* f' n ∘* τ n} definition inclusion_pt {X : ℕ → Type*} (f : pseq_diagram X) (n : ℕ) : inclusion f (Point (X n)) = Point (pseq_colim f) := begin induction n with n p, reflexivity, exact (ap (sι f) (respect_pt _))⁻¹ᵖ ⬝ (!glue ⬝ p) end definition pinclusion [constructor] {X : ℕ → Type*} (f : pseq_diagram X) (n : ℕ) : X n →* pseq_colim f := pmap.mk (inclusion f) (inclusion_pt f n) definition pshift_equiv [constructor] {A : ℕ → Type*} (f : Πn, A n →* A (succ n)) : pseq_colim f ≃* pseq_colim (λn, f (n+1)) := begin fapply pequiv_of_equiv, { apply shift_equiv }, { exact ap (ι _) (respect_pt (f 0)) } end definition pshift_equiv_pinclusion {A : ℕ → Type*} (f : Πn, A n →* A (succ n)) (n : ℕ) : psquare (pinclusion f n) (pinclusion (λn, f (n+1)) n) (f n) (pshift_equiv f) := phomotopy.mk homotopy.rfl begin refine !idp_con ⬝ _, esimp, induction n with n IH, { esimp[inclusion_pt], esimp[shift_diag], exact !idp_con⁻¹ }, { esimp[inclusion_pt], refine !con_inv_cancel_left ⬝ _, rewrite ap_con, rewrite ap_con, refine _ ⬝ whisker_right _ !con.assoc, refine _ ⬝ (con.assoc (_ ⬝ _) _ _)⁻¹, xrewrite [-IH], esimp[shift_up], rewrite [elim_glue, ap_inv, ap_compose'], esimp, rewrite [-+con.assoc], apply whisker_right, rewrite con.assoc, apply !eq_inv_con_of_con_eq, symmetry, exact eq_of_square !natural_square } end definition pseq_colim_functor [constructor] {A A' : ℕ → Type*} {f : pseq_diagram A} {f' : pseq_diagram A'} (g : Πn, A n →* A' n) (p : Π⦃n⦄, g (n+1) ∘* f n ~ f' n ∘* g n) : pseq_colim f →* pseq_colim f' := pmap.mk (seq_colim_functor g p) (ap (ι _) (respect_pt (g _))) definition pseq_colim_pequiv' [constructor] {A A' : ℕ → Type*} {f : pseq_diagram A} {f' : pseq_diagram A'} (g : Πn, A n ≃* A' n) (p : Π⦃n⦄, g (n+1) ∘* f n ~ f' n ∘* g n) : pseq_colim @f ≃* pseq_colim @f' := pequiv_of_equiv (seq_colim_equiv g p) (ap (ι _) (respect_pt (g _))) definition pseq_colim_pequiv [constructor] {A A' : ℕ → Type*} {f : pseq_diagram A} {f' : pseq_diagram A'} (g : Πn, A n ≃* A' n) (p : Πn, g (n+1) ∘* f n ~* f' n ∘* g n) : pseq_colim @f ≃* pseq_colim @f' := pseq_colim_pequiv' g (λn, @p n) -- definition seq_colim_equiv_constant [constructor] {A : ℕ → Type*} {f f' : pseq_diagram A} -- (p : Π⦃n⦄ (a : A n), f n a = f' n a) : seq_colim f ≃ seq_colim f' := -- seq_colim_equiv (λn, erfl) p definition pseq_colim_equiv_constant' [constructor] {A : ℕ → Type*} {f f' : pseq_diagram A} (p : Π⦃n⦄, f n ~ f' n) : pseq_colim @f ≃* pseq_colim @f' := pseq_colim_pequiv' (λn, pequiv.rfl) p definition pseq_colim_equiv_constant [constructor] {A : ℕ → Type*} {f f' : pseq_diagram A} (p : Πn, f n ~* f' n) : pseq_colim @f ≃* pseq_colim @f' := pseq_colim_pequiv (λn, pequiv.rfl) (λn, !pid_pcompose ⬝* p n ⬝* !pcompose_pid⁻¹*) definition pseq_colim_pequiv_pinclusion {A A' : ℕ → Type*} {f : Πn, A n →* A (n+1)} {f' : Πn, A' n →* A' (n+1)} (g : Πn, A n ≃* A' n) (p : Π⦃n⦄, g (n+1) ∘* f n ~* f' n ∘* g n) (n : ℕ) : psquare (pinclusion f n) (pinclusion f' n) (g n) (pseq_colim_pequiv g p) := phomotopy.mk homotopy.rfl begin esimp, refine !idp_con ⬝ _, induction n with n IH, { esimp[inclusion_pt], exact !idp_con⁻¹ }, { esimp[inclusion_pt], rewrite [+ap_con, -+ap_inv, +con.assoc, +seq_colim_functor_glue], xrewrite[-IH], rewrite[+ap_compose', -+con.assoc], apply whisker_right, esimp, rewrite[(eq_con_inv_of_con_eq (to_homotopy_pt (@p _)))], rewrite[ap_con], esimp, rewrite[-+con.assoc, ap_con, ap_compose', +ap_inv], rewrite[-+con.assoc], refine _ ⬝ whisker_right _ (whisker_right _ (whisker_right _ (whisker_right _ !con.left_inv⁻¹))), rewrite[idp_con, +con.assoc], apply whisker_left, rewrite[ap_con, ap_compose', con_inv, +con.assoc], apply whisker_left, refine eq_inv_con_of_con_eq _, symmetry, exact eq_of_square !natural_square } end definition seq_colim_equiv_constant_pinclusion {A : ℕ → Type*} {f f' : pseq_diagram A} (p : Πn, f n ~* f' n) (n : ℕ) : pseq_colim_equiv_constant p ∘* pinclusion f n ~* pinclusion f' n := begin transitivity pinclusion f' n ∘* !pid, refine phomotopy_of_psquare !pseq_colim_pequiv_pinclusion, exact !pcompose_pid end definition pseq_colim.elim' [constructor] {A : ℕ → Type*} {B : Type*} {f : pseq_diagram A} (g : Πn, A n →* B) (p : Πn, g (n+1) ∘* f n ~ g n) : pseq_colim f →* B := begin fapply pmap.mk, { intro x, induction x with n a n a, { exact g n a }, { exact p n a }}, { esimp, apply respect_pt } end definition pseq_colim.elim [constructor] {A : ℕ → Type*} {B : Type*} {f : pseq_diagram A} (g : Πn, A n →* B) (p : Πn, g (n+1) ∘* f n ~* g n) : pseq_colim @f →* B := pseq_colim.elim' g p definition pseq_colim.elim_pinclusion {A : ℕ → Type*} {B : Type*} {f : pseq_diagram A} (g : Πn, A n →* B) (p : Πn, g (n+1) ∘* f n ~* g n) (n : ℕ) : pseq_colim.elim g p ∘* pinclusion f n ~* g n := begin refine phomotopy.mk phomotopy.rfl _, refine !idp_con ⬝ _, esimp, induction n with n IH, { esimp, esimp[inclusion_pt], exact !idp_con⁻¹ }, { esimp, esimp[inclusion_pt], rewrite ap_con, rewrite ap_con, rewrite elim_glue, rewrite [-ap_inv], rewrite [ap_compose'], esimp, rewrite [(eq_con_inv_of_con_eq (!to_homotopy_pt))], rewrite [IH], rewrite [con_inv], rewrite [-+con.assoc], refine _ ⬝ whisker_right _ !con.assoc⁻¹, rewrite [con.left_inv], esimp, refine _ ⬝ !con.assoc⁻¹, rewrite [con.left_inv], esimp, rewrite [ap_inv], rewrite [-con.assoc], refine !idp_con⁻¹ ⬝ whisker_right _ !con.left_inv⁻¹, } end definition prep0 [constructor] {A : ℕ → Type*} (f : pseq_diagram A) (k : ℕ) : A 0 →* A k := pmap.mk (rep0 (λn x, f n x) k) begin induction k with k p, reflexivity, exact ap (@f k) p ⬝ !respect_pt end definition respect_pt_prep0_succ {A : ℕ → Type*} (f : pseq_diagram A) (k : ℕ) : respect_pt (prep0 f (succ k)) = ap (@f k) (respect_pt (prep0 f k)) ⬝ respect_pt (f k) := by reflexivity theorem prep0_succ_lemma {A : ℕ → Type*} (f : pseq_diagram A) (n : ℕ) (p : rep0 (λn x, f n x) n pt = rep0 (λn x, f n x) n pt) (q : prep0 f n (Point (A 0)) = Point (A n)) : loop_equiv_eq_closed (ap (@f n) q ⬝ respect_pt (@f n)) (ap (@f n) p) = Ω→(@f n) (loop_equiv_eq_closed q p) := by rewrite [▸*, con_inv, ↑ap1_gen, +ap_con, ap_inv, +con.assoc] -- definition succ_add_tr_rep {A : ℕ → Type} (f : seq_diagram A) {n : ℕ} (k : ℕ) (x : A n) -- : transport A (succ_add n k) (rep f k (f x)) = rep f (succ k) x := -- begin -- induction k with k p, -- reflexivity, -- exact tr_ap A succ (succ_add n k) _ ⬝ (fn_tr_eq_tr_fn (succ_add n k) f _)⁻¹ ⬝ ap (@f _) p, -- end -- definition succ_add_tr_rep_succ {A : ℕ → Type} (f : seq_diagram A) {n : ℕ} (k : ℕ) (x : A n) -- : succ_add_tr_rep f (succ k) x = tr_ap A succ (succ_add n k) _ ⬝ -- (fn_tr_eq_tr_fn (succ_add n k) f _)⁻¹ ⬝ ap (@f _) (succ_add_tr_rep f k x) := -- by reflexivity -- definition code_glue_equiv [constructor] {A : ℕ → Type} (f : seq_diagram A) {n : ℕ} (k : ℕ) (x y : A n) -- : rep f k (f x) = rep f k (f y) ≃ rep f (succ k) x = rep f (succ k) y := -- begin -- refine eq_equiv_fn_eq_of_equiv (equiv_ap A (succ_add n k)) _ _ ⬝e _, -- apply eq_equiv_eq_closed, -- exact succ_add_tr_rep f k x, -- exact succ_add_tr_rep f k y -- end -- theorem code_glue_equiv_ap {n : ℕ} {k : ℕ} {x y : A n} (p : rep f k (f x) = rep f k (f y)) -- : code_glue_equiv f (succ k) x y (ap (@f _) p) = ap (@f _) (code_glue_equiv f k x y p) := -- begin -- rewrite [▸*, +ap_con, ap_inv, +succ_add_tr_rep_succ, con_inv, inv_con_inv_right, +con.assoc], -- apply whisker_left, -- rewrite [- +con.assoc], apply whisker_right, rewrite [- +ap_compose'], -- note s := (eq_top_of_square (natural_square_tr -- (λx, fn_tr_eq_tr_fn (succ_add n k) f x ⬝ (tr_ap A succ (succ_add n k) (f x))⁻¹) p))⁻¹ᵖ, -- rewrite [inv_con_inv_right at s, -con.assoc at s], exact s -- end definition pseq_colim_loop {X : ℕ → Type*} (f : Πn, X n →* X (n+1)) : Ω (pseq_colim f) ≃* pseq_colim (λn, Ω→ (f n)) := begin fapply pequiv_of_equiv, { refine !seq_colim_eq_equiv0 ⬝e _, fapply seq_colim_equiv, { intro n, exact loop_equiv_eq_closed (respect_pt (prep0 f n)) }, { intro n p, apply prep0_succ_lemma }}, { reflexivity } end definition pseq_colim_loop_pinclusion {X : ℕ → Type*} (f : Πn, X n →* X (n+1)) (n : ℕ) : pseq_colim_loop f ∘* Ω→ (pinclusion f n) ~* pinclusion (λn, Ω→(f n)) n := sorry definition pseq_colim_loop_natural (n : ℕ) : psquare (pseq_colim_loop f) (pseq_colim_loop f') (Ω→ (pseq_colim_functor τ H)) (pseq_colim_functor (λn, Ω→ (τ n)) (λn, ap1_psquare (H n))) := sorry definition pseq_diagram_pfiber {A A' : ℕ → Type*} {f : pseq_diagram A} {f' : pseq_diagram A'} (g : Πn, A n →* A' n) (p : Πn, g (succ n) ∘* f n ~* f' n ∘* g n) : pseq_diagram (λk, pfiber (g k)) := λk, pfiber_functor (f k) (f' k) (p k) /- Two issues when going to the pointed version of the fiber commuting with colimit: - seq_diagram_fiber τ p a for a : A n at position k lives over (A (n + k)), so for a : A 0 you get A (0 + k), but we need A k - in seq_diagram_fiber the fibers are taken in rep f ..., but in the pointed version over the basepoint of A n -/ definition pfiber_pseq_colim_functor {A A' : ℕ → Type*} {f : pseq_diagram A} {f' : pseq_diagram A'} (τ : Πn, A n →* A' n) (p : Π⦃n⦄, τ (n+1) ∘* f n ~* f' n ∘* τ n) : pfiber (pseq_colim_functor τ p) ≃* pseq_colim (pseq_diagram_pfiber τ p) := begin fapply pequiv_of_equiv, { refine fiber_seq_colim_functor0 τ p pt ⬝e _, fapply seq_colim_equiv, intro n, esimp, repeat exact sorry }, exact sorry end -- open succ_str -- definition pseq_colim_succ_str_change_index' {N : succ_str} {B : N → Type*} (n : N) (m : ℕ) -- (h : Πn, B n →* B (S n)) : -- pseq_colim (λk, h (n +' (m + succ k))) ≃* pseq_colim (λk, h (S n +' (m + k))) := -- sorry -- definition pseq_colim_succ_str_change_index {N : succ_str} {B : ℕ → N → Type*} (n : N) -- (h : Π(k : ℕ) n, B k n →* B k (S n)) : -- pseq_colim (λk, h k (n +' succ k)) ≃* pseq_colim (λk, h k (S n +' k)) := -- sorry -- definition pseq_colim_index_eq_general {N : succ_str} (B : N → Type*) (f g : ℕ → N) (p : f ~ g) -- (pf : Πn, S (f n) = f (n+1)) (pg : Πn, S (g n) = g (n+1)) (h : Πn, B n →* B (S n)) : -- @pseq_colim (λn, B (f n)) (λn, ptransport B (pf n) ∘* h (f n)) ≃* -- @pseq_colim (λn, B (g n)) (λn, ptransport B (pg n) ∘* h (g n)) := -- sorry end seq_colim
3c7c9d4fee13d72d275382971f51db4a435eec96
ac1c2a2f522b0fdf854095ba00f882ca849669e7
/tests/lean/run/check_constants.lean
2fa2316f2725ade634c808667ebf5a83eaf13156
[ "Apache-2.0" ]
permissive
abliss/lean
b8b336abc8d50dbb0726dcff9dd16793c23bfbe1
fb24cc99573c153f97a1951ee94bbbdda300b6be
refs/heads/master
1,611,536,584,520
1,497,811,981,000
1,497,811,981,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,901
lean
-- DO NOT EDIT, automatically generated file, generator scripts/gen_constants_cpp.py import smt system.io open tactic meta def script_check_id (n : name) : tactic unit := do env ← get_env, (env^.get n >> return ()) <|> (guard $ env^.is_namespace n) <|> (attribute.get_instances n >> return ()) <|> fail ("identifier '" ++ to_string n ++ "' is not a constant, namespace nor attribute") run_cmd script_check_id `abs run_cmd script_check_id `absurd run_cmd script_check_id `acc.cases_on run_cmd script_check_id `add_comm_group run_cmd script_check_id `add_comm_semigroup run_cmd script_check_id `add_group run_cmd script_check_id `add_monoid run_cmd script_check_id `and run_cmd script_check_id `and.elim_left run_cmd script_check_id `and.elim_right run_cmd script_check_id `and.intro run_cmd script_check_id `and.rec run_cmd script_check_id `and.cases_on run_cmd script_check_id `auto_param run_cmd script_check_id `bit0 run_cmd script_check_id `bit1 run_cmd script_check_id `bool run_cmd script_check_id `bool.ff run_cmd script_check_id `bool.tt run_cmd script_check_id `combinator.K run_cmd script_check_id `caching_user_attribute run_cmd script_check_id `cast run_cmd script_check_id `cast_heq run_cmd script_check_id `char run_cmd script_check_id `char.of_nat run_cmd script_check_id `char.of_nat_ne_of_ne run_cmd script_check_id `classical.prop_decidable run_cmd script_check_id `classical.type_decidable_eq run_cmd script_check_id `coe run_cmd script_check_id `coe_fn run_cmd script_check_id `coe_sort run_cmd script_check_id `coe_to_lift run_cmd script_check_id `congr run_cmd script_check_id `congr_arg run_cmd script_check_id `congr_fun run_cmd script_check_id `decidable run_cmd script_check_id `decidable.to_bool run_cmd script_check_id `distrib run_cmd script_check_id `dite run_cmd script_check_id `id run_cmd script_check_id `empty run_cmd script_check_id `Exists run_cmd script_check_id `eq run_cmd script_check_id `eq.cases_on run_cmd script_check_id `eq.drec run_cmd script_check_id `eq.mp run_cmd script_check_id `eq.mpr run_cmd script_check_id `eq.rec run_cmd script_check_id `eq.refl run_cmd script_check_id `eq.subst run_cmd script_check_id `eq.symm run_cmd script_check_id `eq.trans run_cmd script_check_id `eq_of_heq run_cmd script_check_id `eq_rec_heq run_cmd script_check_id `eq_true_intro run_cmd script_check_id `eq_false_intro run_cmd script_check_id `eq_self_iff_true run_cmd script_check_id `expr run_cmd script_check_id `expr.subst run_cmd script_check_id `format run_cmd script_check_id `false run_cmd script_check_id `false_of_true_iff_false run_cmd script_check_id `false_of_true_eq_false run_cmd script_check_id `true_eq_false_of_false run_cmd script_check_id `false.rec run_cmd script_check_id `field run_cmd script_check_id `fin.mk run_cmd script_check_id `fin.ne_of_vne run_cmd script_check_id `forall_congr run_cmd script_check_id `forall_congr_eq run_cmd script_check_id `forall_not_of_not_exists run_cmd script_check_id `funext run_cmd script_check_id `ge run_cmd script_check_id `gt run_cmd script_check_id `has_add run_cmd script_check_id `has_add.add run_cmd script_check_id `andthen run_cmd script_check_id `has_bind.and_then run_cmd script_check_id `has_bind.bind run_cmd script_check_id `has_bind.seq run_cmd script_check_id `has_div run_cmd script_check_id `has_div.div run_cmd script_check_id `has_emptyc.emptyc run_cmd script_check_id `has_mod.mod run_cmd script_check_id `has_mul run_cmd script_check_id `has_mul.mul run_cmd script_check_id `has_insert.insert run_cmd script_check_id `has_inv run_cmd script_check_id `has_inv.inv run_cmd script_check_id `has_le run_cmd script_check_id `has_le.le run_cmd script_check_id `has_lt run_cmd script_check_id `has_lt.lt run_cmd script_check_id `has_neg run_cmd script_check_id `has_neg.neg run_cmd script_check_id `has_one run_cmd script_check_id `has_one.one run_cmd script_check_id `has_orelse.orelse run_cmd script_check_id `has_sep.sep run_cmd script_check_id `has_sizeof run_cmd script_check_id `has_sizeof.mk run_cmd script_check_id `has_sub run_cmd script_check_id `has_sub.sub run_cmd script_check_id `has_to_format run_cmd script_check_id `has_to_string run_cmd script_check_id `has_well_founded run_cmd script_check_id `has_well_founded.r run_cmd script_check_id `has_well_founded.wf run_cmd script_check_id `has_zero run_cmd script_check_id `has_zero.zero run_cmd script_check_id `has_coe_t run_cmd script_check_id `heq run_cmd script_check_id `heq.refl run_cmd script_check_id `heq.symm run_cmd script_check_id `heq.trans run_cmd script_check_id `heq_of_eq run_cmd script_check_id `hole_command run_cmd script_check_id `id_locked run_cmd script_check_id `if_neg run_cmd script_check_id `if_pos run_cmd script_check_id `iff run_cmd script_check_id `iff_false_intro run_cmd script_check_id `iff.intro run_cmd script_check_id `iff.mp run_cmd script_check_id `iff.mpr run_cmd script_check_id `iff.refl run_cmd script_check_id `iff.symm run_cmd script_check_id `iff.trans run_cmd script_check_id `iff_true_intro run_cmd script_check_id `imp_congr run_cmd script_check_id `imp_congr_eq run_cmd script_check_id `imp_congr_ctx run_cmd script_check_id `imp_congr_ctx_eq run_cmd script_check_id `implies run_cmd script_check_id `implies_of_if_neg run_cmd script_check_id `implies_of_if_pos run_cmd script_check_id `int run_cmd script_check_id `int.has_add run_cmd script_check_id `int.has_mul run_cmd script_check_id `int.has_sub run_cmd script_check_id `int.has_div run_cmd script_check_id `int.has_le run_cmd script_check_id `int.has_lt run_cmd script_check_id `int.has_neg run_cmd script_check_id `int.has_mod run_cmd script_check_id `int.bit0_nonneg run_cmd script_check_id `int.bit1_nonneg run_cmd script_check_id `int.one_nonneg run_cmd script_check_id `int.zero_nonneg run_cmd script_check_id `int.bit0_pos run_cmd script_check_id `int.bit1_pos run_cmd script_check_id `int.one_pos run_cmd script_check_id `int.nat_abs_zero run_cmd script_check_id `int.nat_abs_one run_cmd script_check_id `int.nat_abs_bit0_step run_cmd script_check_id `int.nat_abs_bit1_nonneg_step run_cmd script_check_id `int.ne_of_nat_ne_nonneg_case run_cmd script_check_id `int.ne_neg_of_ne run_cmd script_check_id `int.neg_ne_of_pos run_cmd script_check_id `int.ne_neg_of_pos run_cmd script_check_id `int.neg_ne_zero_of_ne run_cmd script_check_id `int.zero_ne_neg_of_ne run_cmd script_check_id `int.decidable_linear_ordered_comm_group run_cmd script_check_id `interactive.param_desc run_cmd script_check_id `interactive.parse run_cmd script_check_id `io run_cmd script_check_id `io.interface run_cmd script_check_id `is_associative run_cmd script_check_id `is_associative.assoc run_cmd script_check_id `is_commutative run_cmd script_check_id `is_commutative.comm run_cmd script_check_id `ite run_cmd script_check_id `lean.parser run_cmd script_check_id `lean.parser.pexpr run_cmd script_check_id `lean.parser.tk run_cmd script_check_id `left_distrib run_cmd script_check_id `left_comm run_cmd script_check_id `le_refl run_cmd script_check_id `linear_ordered_ring run_cmd script_check_id `linear_ordered_semiring run_cmd script_check_id `list run_cmd script_check_id `list.nil run_cmd script_check_id `list.cons run_cmd script_check_id `match_failed run_cmd script_check_id `monad run_cmd script_check_id `monad_fail run_cmd script_check_id `monoid run_cmd script_check_id `mul_one run_cmd script_check_id `mul_zero run_cmd script_check_id `mul_zero_class run_cmd script_check_id `name.anonymous run_cmd script_check_id `name.mk_numeral run_cmd script_check_id `name.mk_string run_cmd script_check_id `nat run_cmd script_check_id `nat.succ run_cmd script_check_id `nat.zero run_cmd script_check_id `nat.has_zero run_cmd script_check_id `nat.has_one run_cmd script_check_id `nat.has_add run_cmd script_check_id `nat.add run_cmd script_check_id `nat.cases_on run_cmd script_check_id `nat.bit0_ne run_cmd script_check_id `nat.bit0_ne_bit1 run_cmd script_check_id `nat.bit0_ne_zero run_cmd script_check_id `nat.bit0_ne_one run_cmd script_check_id `nat.bit1_ne run_cmd script_check_id `nat.bit1_ne_bit0 run_cmd script_check_id `nat.bit1_ne_zero run_cmd script_check_id `nat.bit1_ne_one run_cmd script_check_id `nat.zero_ne_one run_cmd script_check_id `nat.zero_ne_bit0 run_cmd script_check_id `nat.zero_ne_bit1 run_cmd script_check_id `nat.one_ne_zero run_cmd script_check_id `nat.one_ne_bit0 run_cmd script_check_id `nat.one_ne_bit1 run_cmd script_check_id `nat.bit0_lt run_cmd script_check_id `nat.bit1_lt run_cmd script_check_id `nat.bit0_lt_bit1 run_cmd script_check_id `nat.bit1_lt_bit0 run_cmd script_check_id `nat.zero_lt_one run_cmd script_check_id `nat.zero_lt_bit1 run_cmd script_check_id `nat.zero_lt_bit0 run_cmd script_check_id `nat.one_lt_bit0 run_cmd script_check_id `nat.one_lt_bit1 run_cmd script_check_id `nat.le_of_lt run_cmd script_check_id `nat.le_refl run_cmd script_check_id `ne run_cmd script_check_id `neq_of_not_iff run_cmd script_check_id `norm_num.add1 run_cmd script_check_id `norm_num.add1_bit0 run_cmd script_check_id `norm_num.add1_bit1_helper run_cmd script_check_id `norm_num.add1_one run_cmd script_check_id `norm_num.add1_zero run_cmd script_check_id `norm_num.add_div_helper run_cmd script_check_id `norm_num.bin_add_zero run_cmd script_check_id `norm_num.bin_zero_add run_cmd script_check_id `norm_num.bit0_add_bit0_helper run_cmd script_check_id `norm_num.bit0_add_bit1_helper run_cmd script_check_id `norm_num.bit0_add_one run_cmd script_check_id `norm_num.bit1_add_bit0_helper run_cmd script_check_id `norm_num.bit1_add_bit1_helper run_cmd script_check_id `norm_num.bit1_add_one_helper run_cmd script_check_id `norm_num.div_add_helper run_cmd script_check_id `norm_num.div_eq_div_helper run_cmd script_check_id `norm_num.div_helper run_cmd script_check_id `norm_num.div_mul_helper run_cmd script_check_id `norm_num.mk_cong run_cmd script_check_id `norm_num.mul_bit0_helper run_cmd script_check_id `norm_num.mul_bit1_helper run_cmd script_check_id `norm_num.mul_div_helper run_cmd script_check_id `norm_num.neg_add_neg_helper run_cmd script_check_id `norm_num.neg_add_pos_helper1 run_cmd script_check_id `norm_num.neg_add_pos_helper2 run_cmd script_check_id `norm_num.neg_mul_neg_helper run_cmd script_check_id `norm_num.neg_mul_pos_helper run_cmd script_check_id `norm_num.neg_neg_helper run_cmd script_check_id `norm_num.neg_zero_helper run_cmd script_check_id `norm_num.nonneg_bit0_helper run_cmd script_check_id `norm_num.nonneg_bit1_helper run_cmd script_check_id `norm_num.nonzero_of_div_helper run_cmd script_check_id `norm_num.nonzero_of_neg_helper run_cmd script_check_id `norm_num.nonzero_of_pos_helper run_cmd script_check_id `norm_num.one_add_bit0 run_cmd script_check_id `norm_num.one_add_bit1_helper run_cmd script_check_id `norm_num.one_add_one run_cmd script_check_id `norm_num.pos_add_neg_helper run_cmd script_check_id `norm_num.pos_bit0_helper run_cmd script_check_id `norm_num.pos_bit1_helper run_cmd script_check_id `norm_num.pos_mul_neg_helper run_cmd script_check_id `norm_num.sub_nat_zero_helper run_cmd script_check_id `norm_num.sub_nat_pos_helper run_cmd script_check_id `norm_num.subst_into_div run_cmd script_check_id `norm_num.subst_into_prod run_cmd script_check_id `norm_num.subst_into_subtr run_cmd script_check_id `norm_num.subst_into_sum run_cmd script_check_id `not run_cmd script_check_id `not_of_iff_false run_cmd script_check_id `not_of_eq_false run_cmd script_check_id `of_eq_true run_cmd script_check_id `of_iff_true run_cmd script_check_id `opt_param run_cmd script_check_id `or run_cmd script_check_id `inout_param run_cmd script_check_id `punit run_cmd script_check_id `punit.star run_cmd script_check_id `prod.mk run_cmd script_check_id `pprod run_cmd script_check_id `pprod.mk run_cmd script_check_id `pprod.fst run_cmd script_check_id `pprod.snd run_cmd script_check_id `propext run_cmd script_check_id `to_pexpr run_cmd script_check_id `quot.mk run_cmd script_check_id `quot.lift run_cmd script_check_id `real run_cmd script_check_id `real.of_int run_cmd script_check_id `real.to_int run_cmd script_check_id `real.is_int run_cmd script_check_id `real.has_neg run_cmd script_check_id `real.has_div run_cmd script_check_id `real.has_add run_cmd script_check_id `real.has_mul run_cmd script_check_id `real.has_sub run_cmd script_check_id `real.has_lt run_cmd script_check_id `real.has_le run_cmd script_check_id `reflected run_cmd script_check_id `reflected.subst run_cmd script_check_id `rfl run_cmd script_check_id `right_distrib run_cmd script_check_id `ring run_cmd script_check_id `scope_trace run_cmd script_check_id `set_of run_cmd script_check_id `semiring run_cmd script_check_id `psigma run_cmd script_check_id `psigma.cases_on run_cmd script_check_id `psigma.mk run_cmd script_check_id `psigma.fst run_cmd script_check_id `psigma.snd run_cmd script_check_id `singleton run_cmd script_check_id `sizeof run_cmd script_check_id `smt.array run_cmd script_check_id `smt.select run_cmd script_check_id `smt.store run_cmd script_check_id `smt.prove run_cmd script_check_id `string run_cmd script_check_id `string.empty run_cmd script_check_id `string.str run_cmd script_check_id `string.empty_ne_str run_cmd script_check_id `string.str_ne_empty run_cmd script_check_id `string.str_ne_str_left run_cmd script_check_id `string.str_ne_str_right run_cmd script_check_id `subsingleton run_cmd script_check_id `subsingleton.elim run_cmd script_check_id `subsingleton.helim run_cmd script_check_id `subtype run_cmd script_check_id `subtype.mk run_cmd script_check_id `subtype.val run_cmd script_check_id `subtype.rec run_cmd script_check_id `psum run_cmd script_check_id `psum.cases_on run_cmd script_check_id `psum.inl run_cmd script_check_id `psum.inr run_cmd script_check_id `tactic run_cmd script_check_id `tactic.try run_cmd script_check_id `tactic.triv run_cmd script_check_id `tactic.add_coinductive_predicate run_cmd script_check_id `thunk run_cmd script_check_id `to_fmt run_cmd script_check_id `to_string run_cmd script_check_id `trans_rel_left run_cmd script_check_id `trans_rel_right run_cmd script_check_id `true run_cmd script_check_id `true.intro run_cmd script_check_id `unification_hint run_cmd script_check_id `unification_hint.mk run_cmd script_check_id `unit run_cmd script_check_id `unit.cases_on run_cmd script_check_id `unit.star run_cmd script_check_id `unsafe_monad_from_pure_bind run_cmd script_check_id `user_attribute run_cmd script_check_id `vm_monitor run_cmd script_check_id `weak_order run_cmd script_check_id `well_founded.fix run_cmd script_check_id `well_founded.fix_eq run_cmd script_check_id `well_founded_tactics run_cmd script_check_id `well_founded_tactics.default run_cmd script_check_id `well_founded_tactics.rel_tac run_cmd script_check_id `well_founded_tactics.dec_tac run_cmd script_check_id `xor run_cmd script_check_id `zero_le_one run_cmd script_check_id `zero_lt_one run_cmd script_check_id `zero_mul
002ffead7ca00f9e499cadb976803fe7913bb0a4
c9ba4946202cfd1e2586e71960dfed00503dcdf4
/src/main.lean
f71446043b937a3bcb5633f31659a09e0dabcd56
[]
no_license
ammkrn/learning_semantics_of_k
f55f669b369e32ef8407c16521b21ac5c106dc4d
c1487b538e1decc0f1fd389cd36bc36d2da012ab
refs/heads/master
1,588,081,593,954
1,552,449,093,000
1,552,449,093,000
175,315,800
0
0
null
null
null
null
UTF-8
Lean
false
false
12,150
lean
import meta_k.meta_sort import meta_k.meta_var import meta_k.meta_symbol import meta_k.meta_pattern import object_k.object_symbol import object_k.object_pattern import meta_k.sort_schemas import meta_k.symbol_schemas /- This file contains a definition of the 'working example' matching logic theory described in chapter 8 of the "Semantics of K" document for illustration purposes. Notation used that differs from that in the document is 1) the use of tilde (~) as a prefix for object theory elements (like # for meta)m 2) the ↑ up arrow is used to cast one type into another in a way that is explicit. Matching Logic is based on many-sorted logic, and there are implicit transofrmations happening all over the place, which is daunting for the uninitiated, so I've made them explicit lifts instead of implicit coercions. 3) The modified logical connective notation corresponds to its matching logic pattern counterpart. IE #∧ is the #and pattern, ~s~> is implication parameterized by sort s, etc. For the purposes of chapter 8, the definition of T here can just be thought of as the items in scope. The constructions and lifting used in chapters 7 and 8 are also surprisingly different from thos used in Kore; Symbols become 'head' objects, pattern constructions are all made into independent production rules, and they object pattern/meta pattern constructors are parametric across all patterns, and the lifting process between object theory elements and meta theory elements is altered. -/ -- object sort definition of Nat def Nat : object_sort := ~sort ⟨"Nat"⟩ ~nilSortList -- object sort definition of List{Nat} def List_Nat : object_sort := ~sort ⟨"List"⟩ ↑[~sort ⟨"Nat"⟩ ~nilSortList] -- specification of lift for sorts as defined in 8.3 mutual def lift_object_sort, lift_object_sort_list with lift_object_sort : object_sort → meta_sort | (object_sort.mk ident object_sort_list.nil) := #sort (repr ident) #nilSortList | (object_sort.mk ident L) := #sort (repr ident) (lift_object_sort_list L) with lift_object_sort_list : object_sort_list → meta_sort_list | (object_sort_list.nil) := #nilSortList | (object_sort_list.cons hd tl) := #consSortList (lift_object_sort hd) (lift_object_sort_list tl) instance : has_lift object_sort meta_sort := ⟨ lift_object_sort ⟩ instance : has_lift object_sort_list meta_sort_list := ⟨ lift_object_sort_list ⟩ def meta_Nat : meta_sort := ↑Nat def meta_List_Nat : meta_sort := ↑List_Nat -- evaluation of lifting process from 8.3 on Nat and List_Nat; VM evaluation -- will show that they come out to -- #sort "Nat" ε -- #sort "List" [#sort "Nat" ε] -- respectively #eval meta_Nat #eval meta_List_Nat -- The literal definition of the sort helper functions for #'Nat and #'List -- given in 8.3 would be these two functions. def nat_meta_rep : #Sort := #sort "Nat" #nilSortList notation `#'Nat` := nat_meta_rep def list_meta_rep : #Sort → #Sort | s := #sort "List" ↑[s] notation `#'List` := list_meta_rep -- The axioms which define them intensionally in matching logic would be as follows, -- so we add these patterns to our theory T. def nat_constructor_axiom : #Pattern := let s1 := #'Nat, s2 := #sort "Nat" #nilSortList in (↑s1 #= (s1, s2); ↑s2) def list_constructor_axiom {carrier s : #Sort} : #Pattern := let s1 := #'List s, s2 := #sort "List" ↑[s] in #∀carrier, "s" : %Sort . (↑s1) #= (s1, s2); (↑s2) -- axiom schema about definedness of list parameters; for any sort s, -- if s is defined in the current theory, that implies that the sort List s -- is therefore defined. def list_axiom_schema {carrier s : #Sort} {L : #SortList}: #Pattern := #∀carrier, "s" : %Sort . (#apply (#symbol "sortDeclared" [%Sort] [%Sort, %SortList] %Sort) . [↑s, ↑L]) ~carrier~> (#apply (#symbol "sortDeclared" [%Sort] [%Sort, %SortList] %Sort) . [↑(#'List s), ↑L]) -- Generic function to lift object symbol declarations into meta theory -- based on specification in chapter 8. Uses already defined lifts for sort. def object_symbol_lift_to_meta : object_symbol → meta_symbol | (~symbol ident params args ret) := #symbol (name_fn ident) ↑params ↑args ↑ret instance : has_lift object_symbol meta_symbol := ⟨ object_symbol_lift_to_meta ⟩ def object_nat_zero : ~Symbol := ~symbol "O" [] [] Nat def meta_nat_zero : #Symbol := #symbol "zero" [] [] #'Nat notation `~O` := object_nat_zero -- definition of object theory's 'zero' symbol in meta theory -- is as follows; #eval shows that the version produced by the -- lifting function is identical. def zeros_eq : bool := ↑object_nat_zero = meta_nat_zero #eval zeros_eq def object_nat_succ : ~Symbol := ~symbol "succ" [] [Nat] Nat def meta_nat_succ : #Symbol := #symbol "succ" [] [#'Nat] #'Nat def succs_eq : bool := ↑object_nat_succ = meta_nat_succ #eval succs_eq def object_nat_plus : ~Symbol := ~symbol "+" [] [Nat, Nat] Nat def meta_nat_plus : #Symbol := #symbol "plus" [] [#'Nat, #'Nat] #'Nat notation `~+` := object_nat_plus def pluses_eq : bool := ↑object_nat_plus = meta_nat_plus #eval pluses_eq def object_list_nat_nil : ~Symbol := ~symbol "ε" [Nat] [] (List_Nat) def meta_list_nat_nil : #Symbol := #symbol "nil" [#'Nat] [] (#'List(#'Nat)) def nils_eq : bool := ↑object_list_nat_nil = meta_list_nat_nil #eval nils_eq def object_list_nat_cons : ~Symbol := ~symbol "::" [Nat] [Nat, List_Nat] (List_Nat) def meta_list_nat_cons : #Symbol := #symbol "cons" [#'Nat] [#'Nat, #'List(#'Nat)] (#'List(#'Nat)) -- parametric list symbols def meta_list_nil : #Sort → #Symbol | s := #symbol "nil" [%Sort] [] (#'List s) notation `#'nil` := meta_list_nil def meta_list_cons : #Sort → #Symbol | s := #symbol "cons" [%Sort] [s, #'List s] (#'List s) notation `#'cons` := meta_list_cons def meta_list_append : #Sort → #Symbol | s := #symbol "append" [%Sort] [#'List s, #'List s] (#'List s) notation `#'append` := meta_list_append def cons_eq : bool := ↑object_list_nat_cons = meta_list_nat_cons #eval cons_eq def object_list_nat_append : ~Symbol := ~symbol "@" [Nat] [List_Nat, List_Nat] (List_Nat) def meta_list_nat_append : #Symbol := #symbol "append" [#'Nat] [#'List(#'Nat), #'List(#'Nat)] (#'List(#'Nat)) notation `#'append` := meta_list_nat_append def append_eq : bool := ↑object_list_nat_append = meta_list_nat_append #eval append_eq def symbol_declared_pred {s : #Sort} := #symbol "symbolDeclared" [%Sort] [%Symbol, %SymbolList] %Pattern def sort_declared_pred {s : #Sort} := #symbol "sortDeclared" [%Sort] [%Sort, %SortList] %Pattern def sorts_declared_pred {s : #Sort} := #symbol "sortsDeclared" [%Sort] [%SortList, %SortList] %Pattern def axiom_declared_pred {s : #Sort} := #symbol "axiomDeclared" [%Sort] [%Pattern, %PatternList] %Pattern -- for any #s : Sort in T, the meta representation of the nat zero symbol -- is declared, but not for any other sorts. -- object_nat_zero gets lifted to meta_symbol, then to meta_pattern to -- satisfy the specification. def non_parametric_axiom1 {«#s» : #Sort} {L : #SymbolList }: #Pattern := #apply (@symbol_declared_pred «#s») . [↑object_nat_zero, ↑L] -- for any #s : Sort in T, the meta representation of succ is declared, -- but not for any others. def non_parametric_axiom2 {«#s» : #Sort} {L : #SymbolList} : #Pattern := #apply (@symbol_declared_pred «#s») . [↑object_nat_succ, ↑L] def non_parametric_axiom3 {«#s» : #Sort} {L : #SymbolList} : #Pattern := #apply (@symbol_declared_pred «#s») . [↑object_nat_plus, ↑L] def parametric_axiom1 {«#s» s : #Sort} {LS : #SortList} {LSy : #SymbolList} : #Pattern := (#apply (@sort_declared_pred «#s») . [↑s, ↑LS]) ~s~> (#apply (@symbol_declared_pred «#s») . [↑object_list_nat_nil, ↑LSy]) def parametric_axiom2 {«#s» s : #Sort} {LS : #SortList} {LSy : #SymbolList} : #Pattern := (#apply (@sort_declared_pred «#s») . [↑s, ↑LS]) ~s~> (#apply (@symbol_declared_pred «#s») . [↑object_list_nat_cons, ↑LSy]) def parametric_axiom3 {«#s» s : #Sort} {LS : #SortList} {LSy : #SymbolList} : #Pattern := (#apply (@sort_declared_pred «#s») . [↑s, ↑LS]) ~s~> (#apply (@symbol_declared_pred «#s») . [↑object_list_nat_append, ↑LSy]) def object_variable_lift_meta : ~Variable → #Variable | (~variable ident s) := #variable (name_fn ident) (↑s) instance : has_lift object_variable meta_variable := ⟨ object_variable_lift_meta ⟩ open object_pattern open meta_pattern mutual def object_pattern_lift_meta, object_pattern_list_lift_meta with object_pattern_lift_meta : ~Pattern → #Pattern | (~variableAsPattern v) := #variableAsPattern ↑v | (object_application σ L) := #apply ↑σ . (object_pattern_list_lift_meta L) | (object_and s φ1 φ2) := meta_and ↑s (object_pattern_lift_meta φ1) (object_pattern_lift_meta φ2) | (object_not s φ) := meta_not ↑s (object_pattern_lift_meta φ) | (object_exsts s v φ) := meta_exsts ↑s ↑v (object_pattern_lift_meta φ) with object_pattern_list_lift_meta : ~PatternList → #PatternList | [] := [] | (hd :: tl) := object_pattern_lift_meta hd :: (object_pattern_list_lift_meta tl) instance object_pattern_to_meta_pattern_lift : has_lift object_pattern meta_pattern := ⟨ object_pattern_lift_meta ⟩ def object_nat_var : ~Variable := ⟨ "x", Nat ⟩ def meta_nat_var : #Variable := ⟨ "x", #'Nat ⟩ #eval ((((↑object_nat_var) : #Pattern) = (↑meta_nat_var)) : bool) -- for any x : Nat, x + O is equal to x -- extremely verbose version to clarify what's going on. def object_aux_axiom1_verbose {c s' : ~Sort} : ~Pattern := ~∀c, "x" : Nat . ((~apply (~symbol "+" [] [Nat, Nat] Nat) . [(~variableAsPattern ⟨"x", Nat⟩), ↑(~symbol "O" [] [] Nat)]) ~= (Nat, s'); (~variableAsPattern ⟨"x", Nat⟩)) -- more comfortable version def object_aux_axiom1 {c s' : ~Sort} : ~Pattern := let x : ~Variable := ⟨ "x", Nat ⟩ in ~∀c, x . ((~apply ~+ . [↑x, ↑~O]) ~= (Nat, s'); (↑x)) def meta_aux_axiom1 {c s' : #Sort} : #Pattern := let x : #Variable := #variable "x" #'Nat in #∀c, x . ((#apply meta_nat_plus . [↑x, ↑meta_nat_zero]) #= (#'Nat, s'); (↑x)) def a_lift : #Pattern := ↑(@object_aux_axiom1 object_sort.inhabited.default object_sort.inhabited.default) #eval a_lift -- example of naming helper functions given on p. 49-50, allowing -- #'zero to be used in place of the longer 'raw' representation def «#'zero» : #Symbol := #symbol "zero" [] [] #'Nat def zero_axiom {«#s» : #Sort} : #Pattern := ↑«#'zero» #= (#'Nat, «#s»); #apply (#symbol "zero" [] [] #'Nat) . [] def «#'nil» : #Sort → #Symbol | s := #symbol "nil" [%Sort] [] s def nil_axiom {«#s» : #Sort} : #Sort → #Pattern | s' := #∀«#s», "s'" : s' . ↑(«#'nil» s') #= (s', «#s»); #apply (#symbol "nil" [%Sort] [] s') . [] def Ψ1 {s' : #Sort} : #Pattern := (#apply (#symbol "#'plus" [] [#'Nat, #'Nat] #'Nat) . [#variableAsPattern ⟨ "x", #'Nat⟩, ↑«#'zero»]) #= (#'Nat, s'); (#variableAsPattern ⟨ "x", #'Nat ⟩) -- append(s) (x :: L0) (L) == x :: (L0 ++ L) def Ψ2 {s s' : #Sort} : #Pattern := (#apply (#'append s) . [(#apply (#'cons s) . [#variableAsPattern ⟨"L0", (#'List s)⟩]), (#variableAsPattern ⟨"L", (#'List s)⟩) ]) #= (#'List s, s'); (#apply (#'cons s) . [(#apply (#'append s) . [#variableAsPattern ⟨"L0", (#'List s)⟩, #variableAsPattern ⟨"L", (#'List s)⟩]) ]) def axiom_Ψ1 {«#s» s s' : #Sort} {Ss : #SortList} {φs : #PatternList} : #Pattern := #∀«#s», "s'" : %Sort . (#apply (@sort_declared_pred «#s») . [↑s']) ~«#s»~> (#apply (@axiom_declared_pred «#s») . [@Ψ1 s']) def axiom_Ψ2 {«#s» s s' : #Sort} {Ss : #SortList} {φs : #PatternList} : #Pattern := #∀«#s», "s'" : %Sort . ((#apply (@sort_declared_pred «#s») . [↑s]) #∧(s'); (#apply (@sort_declared_pred «#s») . [↑s'])) ~«#s»~> (#apply (@axiom_declared_pred «#s») . [@Ψ2 s s'])
0cd30623a0a8e0bdc22e98efcf84e75587c4a7d0
08a8ee10652ba4f8592710ceb654b37e951d9082
/types/fiber.lean
dd7b2a6f6169d833396cb5d36b37c11ba9886821
[ "Apache-2.0" ]
permissive
felixwellen/hott3
e9f299c84d30a782a741c40d38741ec024d391fb
8ac87a2699ab94c23ea7984b4a5fbd5a7052575c
refs/heads/master
1,619,972,899,098
1,509,047,351,000
1,518,040,986,000
120,676,559
0
0
null
1,518,040,503,000
1,518,040,503,000
null
UTF-8
Lean
false
false
16,528
lean
/- 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, Mike Shulman Ported from Coq HoTT Theorems about fibers -/ import .sigma .eq .pi ..cubical.squareover .pointed .eq universes u v w hott_theory namespace hott open hott.equiv hott.sigma hott.eq hott.pi hott.pointed hott.is_equiv structure fiber {A B : Type _} (f : A → B) (b : B) := (point : A) (point_eq : f point = b) namespace fiber variables {A : Type _} {B : Type _} {f : A → B} {b : B} @[hott] protected def sigma_char (f : A → B) (b : B) : fiber f b ≃ (Σ(a : A), f a = b) := begin fapply equiv.MK, {intro x, exact ⟨point x, point_eq x⟩}, {intro x, exact (fiber.mk x.1 x.2)}, {intro x, cases x, apply idp }, {intro x, cases x, apply idp }, end @[hott] def fiber_eq_equiv (x y : fiber f b) : (x = y) ≃ (Σ(p : point x = point y), point_eq x = ap f p ⬝ point_eq y) := begin apply equiv.trans, apply eq_equiv_fn_eq_of_equiv, apply fiber.sigma_char, apply equiv.trans, apply sigma_eq_equiv, apply sigma_equiv_sigma_right, intro p, apply eq_pathover_equiv_Fl, end @[hott] def fiber_eq {x y : fiber f b} (p : point x = point y) (q : point_eq x = ap f p ⬝ point_eq y) : x = y := to_inv (fiber_eq_equiv _ _) ⟨p, q⟩ @[hott] def fiber_pathover {X : Type _} {A B : X → Type _} {x₁ x₂ : X} {p : x₁ = x₂} {f : Πx, A x → B x} {b : Πx, B x} {v₁ : fiber (f x₁) (b x₁)} {v₂ : fiber (f x₂) (b x₂)} (q : point v₁ =[p] point v₂) (r : squareover B hrfl (pathover_idp_of_eq _ (point_eq v₁)) (pathover_idp_of_eq _ (point_eq v₂)) (apo f q) (apd b p)) : v₁ =[p; λ x, fiber (f x) (b x)] v₂ := begin apply (pathover_of_fn_pathover_fn (λ (x : X), @fiber.sigma_char (A x) (B x) (f x) (b x))), dsimp, fapply sigma_pathover; dsimp, { exact q}, { induction v₁ with a₁ p₁, induction v₂ with a₂ p₂, dsimp at *, induction q, apply pathover_idp_of_eq, apply eq_of_vdeg_square, dsimp[hrfl] at r, dsimp[apo, apd] at r, exact (square_of_squareover_ids B r)} end open is_trunc @[hott] def π₁ : (B : A → Type _) → (Σa, B a) → A := sigma.fst @[hott] def fiber_pr1 (B : A → Type _) (a : A) : fiber (sigma.fst : (Σa, B a) → A) a ≃ B a := calc fiber sigma.fst a ≃ Σ(u : Σ a, B a), (sigma.fst u) = a : fiber.sigma_char ... ≃ Σa' (b : B a'), a' = a : sigma_assoc_equiv ... ≃ Σa' (p : a' = a), B a' : sigma_equiv_sigma_right (λa', !comm_equiv_nondep) ... ≃ Σu, B u.1 : sigma_assoc_equiv ... ≃ B a : !sigma_equiv_of_is_contr_left @[hott] def sigma_fiber_equiv (f : A → B) : (Σb, fiber f b) ≃ A := calc (Σb, fiber f b) ≃ Σb a, f a = b : sigma_equiv_sigma_right (λb, fiber.sigma_char _ b) ... ≃ Σa b, f a = b : sigma_comm_equiv ... ≃ A : sigma_equiv_of_is_contr_right @[hott] def is_pointed_fiber [instance] (f : A → B) (a : A) : pointed (fiber f (f a)) := pointed.mk (fiber.mk a idp) @[hott] def pointed_fiber (f : A → B) (a : A) : Type* := pointed.Mk (fiber.mk a (idpath (f a))) @[hott] def is_trunc_fun [reducible] (n : ℕ₋₂) (f : A → B) := Π(b : B), is_trunc n (fiber f b) @[hott] def is_contr_fun [reducible] (f : A → B) := is_trunc_fun -2 f -- pre and post composition with equivalences open function variable (f) @[hott] protected def equiv_postcompose {B' : Type _} (g : B ≃ B') --[H : is_equiv g] (b : B) : fiber (g ∘ f) (g b) ≃ fiber f b := calc fiber (g ∘ f) (g b) ≃ Σa : A, g (f a) = g b : fiber.sigma_char ... ≃ Σa : A, f a = b : begin apply sigma_equiv_sigma_right, intro a, apply equiv.symm, apply eq_equiv_fn_eq end ... ≃ fiber f b : fiber.sigma_char @[hott] protected def equiv_precompose {A' : Type _} (g : A' ≃ A) --[H : is_equiv g] (b : B) : fiber (f ∘ g) b ≃ fiber f b := calc fiber (f ∘ g) b ≃ Σa' : A', f (g a') = b : fiber.sigma_char ... ≃ Σa : A, f a = b : begin apply sigma_equiv_sigma g, intro a', apply erfl end ... ≃ fiber f b : fiber.sigma_char end fiber open unit is_trunc pointed namespace fiber @[hott] def fiber_star_equiv (A : Type _) : fiber (λx : A, star) star ≃ A := begin fapply equiv.MK, { intro f, cases f with a H, exact a }, { intro a, apply fiber.mk a, reflexivity }, { intro a, reflexivity }, { intro f, cases f with a H, change fiber.mk a (refl star) = fiber.mk a H, rwr [is_set.elim H (refl star)] } end @[hott] def fiber_const_equiv (A : Type _) (a₀ : A) (a : A) : fiber (λz : unit, a₀) a ≃ a₀ = a := calc fiber (λz : unit, a₀) a ≃ Σz : unit, a₀ = a : fiber.sigma_char ... ≃ a₀ = a : sigma_unit_left -- the pointed fiber of a pointed map, which is the fiber over the basepoint open pointed @[hott] def pfiber {X Y : Type*} (f : X →* Y) : Type* := pointed.MK (fiber f pt) (fiber.mk pt (respect_pt _)) @[hott] def ppoint {X Y : Type*} (f : X →* Y) : pfiber f →* X := pmap.mk point idp @[hott] def pfiber.sigma_char {A B : Type*} (f : A →* B) : pfiber f ≃* pointed.MK (Σa, f a = pt) ⟨pt, respect_pt f⟩ := pequiv_of_equiv (fiber.sigma_char f pt) idp @[hott] def ppoint_sigma_char {A B : Type*} (f : A →* B) : ppoint f ~* pmap.mk pr1 idp ∘* pfiber.sigma_char f := phomotopy.refl _ @[hott] def pfiber_pequiv_of_phomotopy {A B : Type*} {f g : A →* B} (h : f ~* g) : pfiber f ≃* pfiber g := begin fapply pequiv_of_equiv, { refine (fiber.sigma_char f pt ⬝e _ ⬝e (fiber.sigma_char g pt)⁻¹ᵉ), apply sigma_equiv_sigma_right, intros a, apply equiv_eq_closed_left, apply (to_homotopy h) }, { refine (fiber_eq rfl _), change (h pt)⁻¹ ⬝ respect_pt f = idp ⬝ respect_pt g, rwr idp_con, apply inv_con_eq_of_eq_con, symmetry, exact (to_homotopy_pt h) } end @[hott] def transport_fiber_equiv {A B : Type _} (f : A → B) {b1 b2 : B} (p : b1 = b2) : fiber f b1 ≃ fiber f b2 := calc fiber f b1 ≃ Σa, f a = b1 : fiber.sigma_char ... ≃ Σa, f a = b2 : sigma_equiv_sigma_right (λa, equiv_eq_closed_right (f a) p) ... ≃ fiber f b2 : fiber.sigma_char @[hott] def pequiv_postcompose {A B B' : Type*} (f : A →* B) (g : B ≃* B') : pfiber (g ∘* f) ≃* pfiber f := begin fapply pequiv_of_equiv, esimp, refine transport_fiber_equiv (g ∘* f) (respect_pt g)⁻¹ ⬝e fiber.equiv_postcompose f g (Point B), esimp, apply (ap (fiber.mk (Point A))), refine !con.assoc ⬝ _, apply inv_con_eq_of_eq_con, rewrite [▸*, con.assoc, con.right_inv, con_idp, -ap_compose'], exact ap_con_eq_con (λ x, ap g⁻¹ᵉ* (ap g (pleft_inv' g x)⁻¹) ⬝ ap g⁻¹ᵉ* (pright_inv g (g x)) ⬝ pleft_inv' g x) (respect_pt f) end @[hott] def pequiv_precompose {A A' B : Type*} (f : A →* B) (g : A' ≃* A) : pfiber (f ∘* g) ≃* pfiber f := begin fapply pequiv_of_equiv, esimp, refine fiber.equiv_precompose f g (Point B), esimp, apply (eq_of_fn_eq_fn (fiber.sigma_char _ _)), fapply sigma_eq: esimp, { apply respect_pt g }, { apply eq_pathover_Fl' } end @[hott] def pfiber_pequiv_of_square {A B C D : Type*} {f : A →* B} {g : C →* D} (h : A ≃* C) (k : B ≃* D) (s : k ∘* f ~* g ∘* h) : pfiber f ≃* pfiber g := calc pfiber f ≃* pfiber (k ∘* f) : pequiv_postcompose ... ≃* pfiber (g ∘* h) : pfiber_pequiv_of_phomotopy s ... ≃* pfiber g : pequiv_precompose @[hott] def pcompose_ppoint {A B : Type*} (f : A →* B) : f ∘* ppoint f ~* pconst (pfiber f) B := begin fapply phomotopy.mk, { exact point_eq }, { exact !idp_con⁻¹ } end @[hott] def point_fiber_eq {A B : Type _} {f : A → B} {b : B} {x y : fiber f b} (p : point x = point y) (q : point_eq x = ap f p ⬝ point_eq y) : ap point (fiber_eq p q) = p := begin induction x with a r, induction y with a' s, esimp at *, induction p, induction q using eq.rec_symm, induction s, reflexivity end @[hott] def fiber_eq_equiv_fiber {A B : Type _} {f : A → B} {b : B} (x y : fiber f b) : x = y ≃ fiber (ap1_gen f (point_eq x) (point_eq y)) (idpath b) := calc x = y ≃ fiber.sigma_char f b x = fiber.sigma_char f b y : eq_equiv_fn_eq_of_equiv (fiber.sigma_char f b) x y ... ≃ Σ(p : point x = point y), point_eq x =[p] point_eq y : sigma_eq_equiv ... ≃ Σ(p : point x = point y), (point_eq x)⁻¹ ⬝ ap f p ⬝ point_eq y = idp : sigma_equiv_sigma_right (λp, calc point_eq x =[p] point_eq y ≃ point_eq x = ap f p ⬝ point_eq y : eq_pathover_equiv_Fl ... ≃ ap f p ⬝ point_eq y = point_eq x : eq_equiv_eq_symm ... ≃ (point_eq x)⁻¹ ⬝ (ap f p ⬝ point_eq y) = idp : eq_equiv_inv_con_eq_idp ... ≃ (point_eq x)⁻¹ ⬝ ap f p ⬝ point_eq y = idp : equiv_eq_closed_left _ !con.assoc⁻¹) ... ≃ fiber (ap1_gen f (point_eq x) (point_eq y)) (idpath b) : fiber.sigma_char @[hott] def loop_pfiber {A B : Type*} (f : A →* B) : Ω (pfiber f) ≃* pfiber (Ω→ f) := pequiv_of_equiv (fiber_eq_equiv_fiber pt pt) begin induction f with f f₀, induction B with B b₀, esimp at (f,f₀), induction f₀, reflexivity end @[hott] def pfiber_loop_space {A B : Type*} (f : A →* B) : pfiber (Ω→ f) ≃* Ω (pfiber f) := (loop_pfiber f)⁻¹ᵉ* @[hott] def point_fiber_eq_equiv_fiber {A B : Type _} {f : A → B} {b : B} {x y : fiber f b} (p : x = y) : point (fiber_eq_equiv_fiber x y p) = ap1_gen point idp idp p := by induction p; reflexivity @[hott] lemma ppoint_loop_pfiber {A B : Type*} (f : A →* B) : ppoint (Ω→ f) ∘* loop_pfiber f ~* Ω→ (ppoint f) := phomotopy.mk (point_fiber_eq_equiv_fiber) begin induction f with f f₀, induction B with B b₀, esimp at (f,f₀), induction f₀, reflexivity end @[hott] lemma ppoint_loop_pfiber_inv {A B : Type*} (f : A →* B) : Ω→ (ppoint f) ∘* (loop_pfiber f)⁻¹ᵉ* ~* ppoint (Ω→ f) := (phomotopy_pinv_right_of_phomotopy (ppoint_loop_pfiber f))⁻¹* @[hott] lemma pfiber_pequiv_of_phomotopy_ppoint {A B : Type*} {f g : A →* B} (h : f ~* g) : ppoint g ∘* pfiber_pequiv_of_phomotopy h ~* ppoint f := begin induction f with f f₀, induction g with g g₀, induction h with h h₀, induction B with B b₀, esimp at *, induction h₀, induction g₀, fapply phomotopy.mk, { reflexivity }, { symmetry, rexact point_fiber_eq (idpath pt) (inv_con_eq_of_eq_con (idpath (h pt ⬝ (idp ⬝ point_eq (fiber.mk pt idp))))) } end @[hott] lemma pequiv_postcompose_ppoint {A B B' : Type*} (f : A →* B) (g : B ≃* B') : ppoint f ∘* fiber.pequiv_postcompose f g ~* ppoint (g ∘* f) := begin induction f with f f₀, induction g with g hg g₀, induction B with B b₀, induction B' with B' b₀', esimp at * ⊢, induction g₀, induction f₀, fapply phomotopy.mk, { reflexivity }, { symmetry, refine !ap_compose⁻¹ ⬝ _, apply ap_constant } end @[hott] lemma pequiv_precompose_ppoint {A A' B : Type*} (f : A →* B) (g : A' ≃* A) : ppoint f ∘* fiber.pequiv_precompose f g ~* g ∘* ppoint (f ∘* g) := begin induction f with f f₀, induction g with g h₁ h₂ p₁ p₂, induction B with B b₀, induction g with g g₀, induction A with A a₀', esimp at *, induction g₀, induction f₀, reflexivity end @[hott] def pfiber_pequiv_of_square_ppoint {A B C D : Type*} {f : A →* B} {g : C →* D} (h : A ≃* C) (k : B ≃* D) (s : k ∘* f ~* g ∘* h) : ppoint g ∘* pfiber_pequiv_of_square h k s ~* h ∘* ppoint f := begin refine !passoc⁻¹* ⬝* _, refine pwhisker_right _ !pequiv_precompose_ppoint ⬝* _, refine !passoc ⬝* _, apply pwhisker_left, refine !passoc⁻¹* ⬝* _, refine pwhisker_right _ !pfiber_pequiv_of_phomotopy_ppoint ⬝* _, apply pinv_right_phomotopy_of_phomotopy, refine !pequiv_postcompose_ppoint⁻¹*, end -- this breaks certain proofs if it is an instance @[hott] def is_trunc_fiber (n : ℕ₋₂) {A B : Type _} (f : A → B) (b : B) [is_trunc n A] [is_trunc (n.+1) B] : is_trunc n (fiber f b) := is_trunc_equiv_closed_rev n !fiber.sigma_char @[hott] def is_trunc_pfiber (n : ℕ₋₂) {A B : Type*} (f : A →* B) [is_trunc n A] [is_trunc (n.+1) B] : is_trunc n (pfiber f) := is_trunc_fiber n f pt @[hott] def fiber_equiv_of_is_contr {A B : Type _} (f : A → B) (b : B) [is_contr B] : fiber f b ≃ A := !fiber.sigma_char ⬝e !sigma_equiv_of_is_contr_right @[hott] def pfiber_pequiv_of_is_contr {A B : Type*} (f : A →* B) [is_contr B] : pfiber f ≃* A := pequiv_of_equiv (fiber_equiv_of_is_contr f pt) idp @[hott] def pfiber_ppoint_equiv {A B : Type*} (f : A →* B) : pfiber (ppoint f) ≃ Ω B := calc pfiber (ppoint f) ≃ Σ(x : pfiber f), ppoint f x = pt : fiber.sigma_char ... ≃ Σ(x : Σa, f a = pt), x.1 = pt : by exact sigma_equiv_sigma !fiber.sigma_char (λa, erfl) ... ≃ Σ(x : Σa, a = pt), f x.1 = pt : by exact !sigma_assoc_comm_equiv ... ≃ f pt = pt : by exact !sigma_equiv_of_is_contr_left ... ≃ Ω B : by exact !equiv_eq_closed_left !respect_pt @[hott] def pfiber_ppoint_pequiv {A B : Type*} (f : A →* B) : pfiber (ppoint f) ≃* Ω B := pequiv_of_equiv (pfiber_ppoint_equiv f) !con.left_inv @[hott] def fiber_ppoint_equiv_eq {A B : Type*} {f : A →* B} {a : A} (p : f a = pt) (q : ppoint f (fiber.mk a p) = pt) : pfiber_ppoint_equiv f (fiber.mk (fiber.mk a p) q) = (respect_pt f)⁻¹ ⬝ ap f q⁻¹ ⬝ p := begin refine _ ⬝ !con.assoc⁻¹, apply whisker_left, refine eq_transport_Fl _ _ ⬝ _, apply whisker_right, refine inverse2 !ap_inv ⬝ !inv_inv ⬝ _, refine ap_compose f pr₁ _ ⬝ ap02 f !ap_pr1_center_eq_sigma_eq', end @[hott] def fiber_ppoint_equiv_inv_eq {A B : Type*} (f : A →* B) (p : Ω B) : (pfiber_ppoint_equiv f)⁻¹ᵉ p = fiber.mk (fiber.mk pt (respect_pt f ⬝ p)) idp := begin apply inv_eq_of_eq, refine _ ⬝ !fiber_ppoint_equiv_eq⁻¹, exact !inv_con_cancel_left⁻¹ end end fiber open function is_equiv namespace fiber /- @[hott] theorem 4.7.6 -/ variables {A : Type _} {P Q : A → Type _} variable (f : Πa, P a → Q a) @[hott] def fiber_total_equiv {a : A} (q : Q a) : fiber (total f) ⟨a , q⟩ ≃ fiber (f a) q := calc fiber (total f) ⟨a , q⟩ ≃ Σ(w : Σx, P x), ⟨w.1 , f w.1 w.2 ⟩ = ⟨a , q⟩ : fiber.sigma_char ... ≃ Σ(x : A), Σ(p : P x), ⟨x , f x p⟩ = ⟨a , q⟩ : sigma_assoc_equiv ... ≃ Σ(x : A), Σ(p : P x), Σ(H : x = a), f x p =[H] q : begin apply sigma_equiv_sigma_right, intro x, apply sigma_equiv_sigma_right, intro p, apply sigma_eq_equiv end ... ≃ Σ(x : A), Σ(H : x = a), Σ(p : P x), f x p =[H] q : begin apply sigma_equiv_sigma_right, intro x, apply sigma_comm_equiv end ... ≃ Σ(w : Σx, x = a), Σ(p : P w.1), f w.1 p =[w.2] q : sigma_assoc_equiv ... ≃ Σ(p : P (center (Σx, x=a)).1), f (center (Σx, x=a)).1 p =[(center (Σx, x=a)).2] q : sigma_equiv_of_is_contr_left ... ≃ Σ(p : P a), f a p =[idpath a] q : equiv_of_eq idp ... ≃ Σ(p : P a), f a p = q : begin apply sigma_equiv_sigma_right, intro p, apply pathover_idp end ... ≃ fiber (f a) q : fiber.sigma_char end fiber end hott
925ec5b1019699f76d7cef54a4cefd912abdce1f
7cef822f3b952965621309e88eadf618da0c8ae9
/src/tactic/suggest.lean
07ed541563ef7ecda4027212fb1b6211406dfefc
[ "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
13,064
lean
/- Copyright (c) 2019 Lucas Allen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Lucas Allen and Scott Morrison -/ import data.mllist import tactic.solve_by_elim /-! `suggest` and `library_search` are a pair of tactics for applying lemmas from the library to the current goal. * `suggest` prints a list of `exact ...` or `refine ...` statements, which may produce new goals * `library_search` prints a single `exact ...` which closes the goal, or fails -/ namespace tactic open native namespace suggest /-- compute the head symbol of an expression, but normalise `>` to `<` and `≥` to `≤` -/ -- We may want to tweak this further? meta def head_symbol : expr → name | (expr.pi _ _ _ t) := head_symbol t | (expr.app f _) := head_symbol f | (expr.const n _) := -- TODO this is a hack; if you suspect more cases here would help, please report them match n with | `gt := `has_lt.lt | `ge := `has_le.le | _ := n end | _ := `_ /-- A declaration can match the head symbol of the current goal in four possible ways: * `ex` : an exact match * `mp` : the declaration returns an `iff`, and the right hand side matches the goal * `mpr` : the declaration returns an `iff`, and the left hand side matches the goal * `both`: the declaration returns an `iff`, and the both sides match the goal -/ inductive head_symbol_match | ex | mp | mpr | both open head_symbol_match /-- a textual representation of a `head_symbol_match`, for trace debugging. -/ def head_symbol_match.to_string : head_symbol_match → string | ex := "exact" | mp := "iff.mp" | mpr := "iff.mpr" | both := "iff.mp and iff.mpr" /-- When we are determining if a given declaration is potentially relevant for the current goal, we compute `unfold_head_symbol` on the head symbol of the declaration, producing a list of names. We consider the declaration potentially relevant if the head symbol of the goal appears in this list. -/ -- This is a hack. meta def unfold_head_symbol : name → list name | `false := [`not, `false] | n := [n] /-- Determine if, and in which way, a given expression matches the specified head symbol. -/ meta def match_head_symbol (hs : name) : expr → option head_symbol_match | (expr.pi _ _ _ t) := match_head_symbol t | `(%%a ↔ %%b) := if `iff = hs then some ex else match (match_head_symbol a, match_head_symbol b) with | (some ex, some ex) := some both | (some ex, _) := some mpr | (_, some ex) := some mp | _ := none end | (expr.app f _) := match_head_symbol f | (expr.const n _) := if list.mem hs (unfold_head_symbol n) then some ex else none | _ := none meta structure decl_data := (d : declaration) (n : name) (m : head_symbol_match) (l : ℕ) -- cached length of name /-- Generate a `decl_data` from the given declaration if it matches the head symbol `hs` for the current goal. -/ -- We used to check here for private declarations, or declarations with certain suffixes. -- It turns out `apply` is so fast, it's better to just try them all. meta def process_declaration (hs : name) (d : declaration) : option decl_data := let n := d.to_name in if ¬ d.is_trusted ∨ n.is_internal then none else (λ m, ⟨d, n, m, n.length⟩) <$> match_head_symbol hs d.type /-- Retrieve all library definitions with a given head symbol. -/ meta def library_defs (hs : name) : tactic (list decl_data) := do env ← get_env, return $ env.decl_filter_map (process_declaration hs) /-- Apply the lemma `e`, then attempt to close all goals using `solve_by_elim { discharger := discharger }`, failing if `close_goals = tt` and there are any goals remaining. -/ -- Implementation note: as this is used by both `library_search` and `suggest`, -- we first run `solve_by_elim` separately on a subset of the goals, whether or not `close_goals` is set, -- and then if `close_goals = tt`, require that `solve_by_elim { all_goals := tt }` succeeds -- on the remaining goals. meta def apply_and_solve (close_goals : bool) (discharger : tactic unit) (e : expr) : tactic unit := apply e >> -- Phase 1 -- Run `solve_by_elim` on each "safe" goal separately, not worrying about failures. -- (We only attempt the "safe" goals in this way in Phase 1. In Phase 2 we will do -- backtracking search across all goals, allowing us to guess solutions that involve data, or -- unify metavariables, but only as long as we can finish all goals.) try (any_goals (independent_goal >> solve_by_elim { discharger := discharger })) >> -- Phase 2 (done <|> -- If there were any goals that we did not attempt solving in the first phase -- (because they weren't propositional, or contained a metavariable) -- as a second phase we attempt to solve all remaining goals at once (with backtracking across goals). any_goals (success_if_fail independent_goal) >> solve_by_elim { backtrack_all_goals := tt, discharger := discharger } <|> -- and fail unless `close_goals = ff` guard ¬ close_goals) /-- Apply the declaration `d` (or the forward and backward implications separately, if it is an `iff`), and then attempt to solve the goal using `apply_and_solve`. -/ meta def apply_declaration (close_goals : bool) (discharger : tactic unit) (d : decl_data) : tactic unit := let tac := apply_and_solve close_goals discharger in do (e, t) ← decl_mk_const d.d, match d.m with | ex := tac e | mp := do l ← iff_mp_core e t, tac l | mpr := do l ← iff_mpr_core e t, tac l | both := (do l ← iff_mp_core e t, tac l) <|> (do l ← iff_mpr_core e t, tac l) end /-- Replace any metavariables in the expression with underscores, in preparation for printing `refine ...` statements. -/ meta def replace_mvars (e : expr) : expr := e.replace (λ e' _, if e'.is_mvar then some (unchecked_cast pexpr.mk_placeholder) else none) /-- Construct a `refine ...` or `exact ...` string which would construct `g`. -/ meta def tactic_statement (g : expr) : tactic string := do g ← instantiate_mvars g, g ← head_beta g, r ← pp (replace_mvars g), if g.has_meta_var then return (sformat!"refine {r}") else return (sformat!"exact {r}") /-- Assuming that a goal `g` has been (partially) solved in the tactic_state `l`, this function prepares a string of the form `exact ...` or `refine ...` (possibly with underscores) that will reproduce that result. -/ meta def message (g : expr) (l : tactic_state) : tactic string := do s ← read, write l, r ← tactic_statement g, write s, return r /-- An `application` records the result of a successful application of a library lemma. -/ meta structure application := (state : tactic_state) (script : string) (decl : option declaration) (num_goals : ℕ) (hyps_used : ℕ) end suggest open suggest declare_trace suggest -- Trace a list of all relevant lemmas -- implementation note: we produce a `tactic (mllist tactic application)` first, -- because it's easier to work in the tactic monad, but in a moment we squash this -- down to an `mllist tactic application`. private meta def suggest_core' (discharger : tactic unit := done) : tactic (mllist tactic application) := do g :: _ ← get_goals, hyps ← local_context, -- Make sure that `solve_by_elim` doesn't just solve the goal immediately: (lock_tactic_state (do focus1 $ solve_by_elim { discharger := discharger }, s ← read, m ← tactic_statement g, g ← instantiate_mvars g, return $ mllist.of_list [⟨s, m, none, 0, hyps.countp(λ h, h.occurs g)⟩])) <|> -- Otherwise, let's actually try applying library lemmas. (do -- Collect all definitions with the correct head symbol t ← infer_type g, defs ← library_defs (head_symbol t), -- Sort by length; people like short proofs let defs := defs.qsort(λ d₁ d₂, d₁.l ≤ d₂.l), when (is_trace_enabled_for `suggest) $ (do trace format!"Found {defs.length} relevant lemmas:", trace $ defs.map (λ ⟨d, n, m, l⟩, (n, m.to_string))), -- Try applying each lemma against the goal, -- then record the number of remaining goals, and number of local hypotheses used. return $ (mllist.of_list defs).mfilter_map -- (This tactic block is only executed when we evaluate the mllist, -- so we need to do the `focus1` here.) (λ d, lock_tactic_state $ focus1 $ do apply_declaration ff discharger d, ng ← num_goals, g ← instantiate_mvars g, state ← read, m ← message g state, return { application . state := state, decl := d.d, script := m, num_goals := ng, hyps_used := hyps.countp(λ h, h.occurs g) })) /-- The core `suggest` tactic. It attempts to apply a declaration from the library, then solve new goals using `solve_by_elim`. It returns a list of `application`s consisting of fields: * `state`, a tactic state resulting from the successful application of a declaration from the library, * `script`, a string of the form `refine ...` or `exact ...` which will reproduce that tactic state, * `decl`, an `option declaration` indicating the declaration that was applied (or none, if `solve_by_elim` succeeded), * `num_goals`, the number of remaining goals, and * `hyps_used`, the number of local hypotheses used in the solution. -/ meta def suggest_core (discharger : tactic unit := done) : mllist tactic application := (mllist.monad_lift (suggest_core' discharger)).join /-- See `suggest_core`. Returns a list of at most `limit` `application`s, sorted by number of goals, and then (reverse) number of hypotheses used. -/ meta def suggest (limit : option ℕ := none) (discharger : tactic unit := done) : tactic (list application) := do let results := suggest_core discharger, -- Get the first n elements of the successful lemmas L ← if h : limit.is_some then results.take (option.get h) else results.force, -- Sort by number of remaining goals, then by number of hypotheses used. return $ L.qsort(λ d₁ d₂, d₁.num_goals < d₂.num_goals ∨ (d₁.num_goals = d₂.num_goals ∧ d₁.hyps_used ≥ d₂.hyps_used)) /-- Returns a list of at most `limit` strings, of the form `exact ...` or `refine ...`, which make progress on the current goal using a declaration from the library. -/ meta def suggest_scripts (limit : option ℕ := none) (discharger : tactic unit := done) : tactic (list string) := do L ← suggest limit discharger, return $ L.map application.script /-- Returns a string of the form `exact ...`, which closes the current goal. -/ meta def library_search (discharger : tactic unit := done) : tactic string := (suggest_core discharger).mfirst (λ a, do guard (a.num_goals = 0), write a.state, return a.script) namespace interactive open tactic open interactive open lean.parser local postfix `?`:9001 := optional declare_trace silence_suggest -- Turn off `exact/refine ...` trace messages for `suggest` /-- `suggest` tries to apply suitable theorems/defs from the library, and generates a list of `exact ...` or `refine ...` scripts that could be used at this step. It leaves the tactic state unchanged. It is intended as a complement of the search function in your editor, the `#find` tactic, and `library_search`. `suggest` takes an optional natural number `num` as input and returns the first `num` (or less, if all possibilities are exhausted) possibilities ordered by length of lemma names. The default for `num` is `50`. For performance reasons `suggest` uses monadic lazy lists (`mllist`). This means that `suggest` might miss some results if `num` is not large enough. However, because `suggest` uses monadic lazy lists, smaller values of `num` run faster than larger values. -/ meta def suggest (n : parse (with_desc "n" small_nat)?) : tactic unit := do L ← tactic.suggest_scripts (n.get_or_else 50), if is_trace_enabled_for `silence_suggest then skip else if L.length = 0 then fail "There are no applicable declarations" else L.mmap trace >> skip declare_trace silence_library_search -- Turn off `exact ...` trace message for `library_search /-- `library_search` attempts to apply every definition in the library whose head symbol matches the goal, and then discharge any new goals using `solve_by_elim`. If it succeeds, it prints a trace message `exact ...` which can replace the invocation of `library_search`. -/ meta def library_search : tactic unit := tactic.library_search tactic.done >>= if is_trace_enabled_for `silence_library_search then (λ _, skip) else trace end interactive @[hole_command] meta def library_search_hole_cmd : hole_command := { name := "library_search", descr := "Use `library_search` to complete the goal.", action := λ _, do script ← library_search, -- Is there a better API for dropping the 'exact ' prefix on this string? return [((script.mk_iterator.remove 6).to_string, "by library_search")] } end tactic
fe906ad140bac241e568b400a0a979618ad5f3f6
d406927ab5617694ec9ea7001f101b7c9e3d9702
/archive/imo/imo1994_q1.lean
bab8915e6af63427a12f620a23df74fef6e1e446
[ "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
4,121
lean
/- Copyright (c) 2021 Antoine Labelle. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Antoine Labelle -/ import algebra.big_operators.basic import algebra.big_operators.order import data.fintype.big_operators import data.finset.sort import data.fin.interval import tactic.linarith import tactic.by_contra /-! # IMO 1994 Q1 Let `m` and `n` be two positive integers. Let `a₁, a₂, ..., aₘ` be `m` different numbers from the set `{1, 2, ..., n}` such that for any two indices `i` and `j` with `1 ≤ i ≤ j ≤ m` and `aᵢ + aⱼ ≤ n`, there exists an index `k` such that `aᵢ + aⱼ = aₖ`. Show that `(a₁+a₂+...+aₘ)/m ≥ (n+1)/2` # Sketch of solution We can order the numbers so that `a₁ ≤ a₂ ≤ ... ≤ aₘ`. The key idea is to pair the numbers in the sum and show that `aᵢ + aₘ₊₁₋ᵢ ≥ n+1`. Indeed, if we had `aᵢ + aₘ₊₁₋ᵢ ≤ n`, then `a₁ + aₘ₊₁₋ᵢ, a₂ + aₘ₊₁₋ᵢ, ..., aᵢ + aₘ₊₁₋ᵢ` would be `m` elements of the set of `aᵢ`'s all larger than `aₘ₊₁₋ᵢ`, which is impossible. -/ open_locale big_operators open finset lemma tedious (m : ℕ) (k : fin (m+1)) : m - (m + (m + 1 - ↑k)) % (m + 1) = ↑k := begin cases k with k hk, rw [nat.lt_succ_iff, le_iff_exists_add] at hk, rcases hk with ⟨c, rfl⟩, have : k + c + (k + c + 1 - k) = c + (k + c + 1), { simp only [add_assoc, add_tsub_cancel_left, add_left_comm] }, rw [fin.coe_mk, this, nat.add_mod_right, nat.mod_eq_of_lt, nat.add_sub_cancel], linarith end theorem imo1994_q1 (n : ℕ) (m : ℕ) (A : finset ℕ) (hm : A.card = m + 1) (hrange : ∀ a ∈ A, 0 < a ∧ a ≤ n) (hadd : ∀ (a b ∈ A), a + b ≤ n → a + b ∈ A) : (m + 1) * (n + 1) ≤ 2 * ∑ x in A, x := begin set a := order_emb_of_fin A hm, -- We sort the elements of `A` have ha : ∀ i, a i ∈ A := λ i, order_emb_of_fin_mem A hm i, set rev := equiv.sub_left (fin.last m), -- `i ↦ m-i` -- We reindex the sum by fin (m+1) have : ∑ x in A, x = ∑ i : fin (m+1), a i, { convert sum_image (λ x hx y hy, (order_embedding.eq_iff_eq a).1), rw ←coe_inj, simp }, rw this, clear this, -- The main proof is a simple calculation by rearranging one of the two sums suffices hpair : ∀ k ∈ univ, a k + a (rev k) ≥ n+1, calc 2 * ∑ i : fin (m+1), a i = ∑ i : fin (m+1), a i + ∑ i : fin (m+1), a i : two_mul _ ... = ∑ i : fin (m+1), a i + ∑ i : fin (m+1), a (rev i) : by rw equiv.sum_comp rev ... = ∑ i : fin (m+1), (a i + a (rev i)) : sum_add_distrib.symm ... ≥ ∑ i : fin (m+1), (n+1) : sum_le_sum hpair ... = (m+1) * (n+1) : by rw [sum_const, card_fin, nat.nsmul_eq_mul], -- It remains to prove the key inequality, by contradiction rintros k -, by_contra' h : a k + a (rev k) < n + 1, -- We exhibit `k+1` elements of `A` greater than `a (rev k)` set f : fin (m+1) ↪ ℕ := ⟨λ i, a i + a (rev k), begin apply injective_of_le_imp_le, intros i j hij, rwa [add_le_add_iff_right, a.map_rel_iff] at hij, end⟩, -- Proof that the `f i` are greater than `a (rev k)` for `i ≤ k` have hf : map f (Icc 0 k) ⊆ map a.to_embedding (Ioc (rev k) (fin.last m)), { intros x hx, simp only [equiv.sub_left_apply] at h, simp only [mem_map, f, mem_Icc, mem_Ioc, fin.zero_le, true_and, equiv.sub_left_apply, function.embedding.coe_fn_mk, exists_prop, rel_embedding.coe_fn_to_embedding] at hx ⊢, rcases hx with ⟨i, ⟨hi, rfl⟩⟩, have h1 : a i + a (fin.last m - k) ≤ n, { linarith only [h, a.monotone hi] }, have h2 : a i + a (fin.last m - k) ∈ A := hadd _ (ha _) _ (ha _) h1, rw [←mem_coe, ←range_order_emb_of_fin A hm, set.mem_range] at h2, cases h2 with j hj, refine ⟨j, ⟨_, fin.le_last j⟩, hj⟩, rw [← a.strict_mono.lt_iff_lt, hj], simpa using (hrange (a i) (ha i)).1 }, -- A set of size `k+1` embed in one of size `k`, which yields a contradiction simpa [fin.coe_sub, tedious] using card_le_of_subset hf, end
f59ae5191f7ebcee524c6d98399b82555c8348e8
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/alexandroff.lean
d818326b6323da52a857a266d8d9c15bc6214548
[ "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
18,693
lean
/- Copyright (c) 2021 Yourong Zang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yourong Zang, Yury Kudryashov -/ import data.fintype.option import topology.separation import topology.sets.opens /-! # The Alexandroff Compactification We construct the Alexandroff compactification (the one-point compactification) of an arbitrary topological space `X` and prove some properties inherited from `X`. ## Main definitions * `alexandroff`: the Alexandroff compactification, we use coercion for the canonical embedding `X → alexandroff X`; when `X` is already compact, the compactification adds an isolated point to the space. * `alexandroff.infty`: the extra point ## Main results * The topological structure of `alexandroff X` * The connectedness of `alexandroff X` for a noncompact, preconnected `X` * `alexandroff X` is `T₀` for a T₀ space `X` * `alexandroff X` is `T₁` for a T₁ space `X` * `alexandroff X` is normal if `X` is a locally compact Hausdorff space ## Tags one-point compactification, compactness -/ open set filter open_locale classical topological_space filter /-! ### Definition and basic properties In this section we define `alexandroff X` to be the disjoint union of `X` and `∞`, implemented as `option X`. Then we restate some lemmas about `option X` for `alexandroff X`. -/ variables {X : Type*} /-- The Alexandroff extension of an arbitrary topological space `X` -/ def alexandroff (X : Type*) := option X /-- The repr uses the notation from the `alexandroff` locale. -/ instance [has_repr X] : has_repr (alexandroff X) := ⟨λ o, match o with | none := "∞" | (some a) := "↑" ++ repr a end⟩ namespace alexandroff /-- The point at infinity -/ def infty : alexandroff X := none localized "notation (name := alexandroff.infty) `∞` := alexandroff.infty" in alexandroff instance : has_coe_t X (alexandroff X) := ⟨option.some⟩ instance : inhabited (alexandroff X) := ⟨∞⟩ instance [fintype X] : fintype (alexandroff X) := option.fintype instance infinite [infinite X] : infinite (alexandroff X) := option.infinite lemma coe_injective : function.injective (coe : X → alexandroff X) := option.some_injective X @[norm_cast] lemma coe_eq_coe {x y : X} : (x : alexandroff X) = y ↔ x = y := coe_injective.eq_iff @[simp] lemma coe_ne_infty (x : X) : (x : alexandroff X) ≠ ∞ . @[simp] lemma infty_ne_coe (x : X) : ∞ ≠ (x : alexandroff X) . /-- Recursor for `alexandroff` using the preferred forms `∞` and `↑x`. -/ @[elab_as_eliminator] protected def rec (C : alexandroff X → Sort*) (h₁ : C ∞) (h₂ : Π x : X, C x) : Π (z : alexandroff X), C z := option.rec h₁ h₂ lemma is_compl_range_coe_infty : is_compl (range (coe : X → alexandroff X)) {∞} := is_compl_range_some_none X @[simp] lemma range_coe_union_infty : (range (coe : X → alexandroff X) ∪ {∞}) = univ := range_some_union_none X @[simp] lemma range_coe_inter_infty : (range (coe : X → alexandroff X) ∩ {∞}) = ∅ := range_some_inter_none X @[simp] lemma compl_range_coe : (range (coe : X → alexandroff X))ᶜ = {∞} := compl_range_some X lemma compl_infty : ({∞}ᶜ : set (alexandroff X)) = range (coe : X → alexandroff X) := (@is_compl_range_coe_infty X).symm.compl_eq lemma compl_image_coe (s : set X) : (coe '' s : set (alexandroff X))ᶜ = coe '' sᶜ ∪ {∞} := by rw [coe_injective.compl_image_eq, compl_range_coe] lemma ne_infty_iff_exists {x : alexandroff X} : x ≠ ∞ ↔ ∃ (y : X), (y : alexandroff X) = x := by induction x using alexandroff.rec; simp instance can_lift : can_lift (alexandroff X) X coe (λ x, x ≠ ∞) := with_top.can_lift lemma not_mem_range_coe_iff {x : alexandroff X} : x ∉ range (coe : X → alexandroff X) ↔ x = ∞ := by rw [← mem_compl_iff, compl_range_coe, mem_singleton_iff] lemma infty_not_mem_range_coe : ∞ ∉ range (coe : X → alexandroff X) := not_mem_range_coe_iff.2 rfl lemma infty_not_mem_image_coe {s : set X} : ∞ ∉ (coe : X → alexandroff X) '' s := not_mem_subset (image_subset_range _ _) infty_not_mem_range_coe @[simp] lemma coe_preimage_infty : (coe : X → alexandroff X) ⁻¹' {∞} = ∅ := by { ext, simp } /-! ### Topological space structure on `alexandroff X` We define a topological space structure on `alexandroff X` so that `s` is open if and only if * `coe ⁻¹' s` is open in `X`; * if `∞ ∈ s`, then `(coe ⁻¹' s)ᶜ` is compact. Then we reformulate this definition in a few different ways, and prove that `coe : X → alexandroff X` is an open embedding. If `X` is not a compact space, then we also prove that `coe` has dense range, so it is a dense embedding. -/ variables [topological_space X] instance : topological_space (alexandroff X) := { is_open := λ s, (∞ ∈ s → is_compact ((coe : X → alexandroff X) ⁻¹' s)ᶜ) ∧ is_open ((coe : X → alexandroff X) ⁻¹' s), is_open_univ := by simp, is_open_inter := λ s t, begin rintros ⟨hms, hs⟩ ⟨hmt, ht⟩, refine ⟨_, hs.inter ht⟩, rintros ⟨hms', hmt'⟩, simpa [compl_inter] using (hms hms').union (hmt hmt') end, is_open_sUnion := λ S ho, begin suffices : is_open (coe ⁻¹' ⋃₀ S : set X), { refine ⟨_, this⟩, rintro ⟨s, hsS : s ∈ S, hs : ∞ ∈ s⟩, refine is_compact_of_is_closed_subset ((ho s hsS).1 hs) this.is_closed_compl _, exact compl_subset_compl.mpr (preimage_mono $ subset_sUnion_of_mem hsS) }, rw [preimage_sUnion], exact is_open_bUnion (λ s hs, (ho s hs).2) end } variables {s : set (alexandroff X)} {t : set X} lemma is_open_def : is_open s ↔ (∞ ∈ s → is_compact (coe ⁻¹' s : set X)ᶜ) ∧ is_open (coe ⁻¹' s : set X) := iff.rfl lemma is_open_iff_of_mem' (h : ∞ ∈ s) : is_open s ↔ is_compact (coe ⁻¹' s : set X)ᶜ ∧ is_open (coe ⁻¹' s : set X) := by simp [is_open_def, h] lemma is_open_iff_of_mem (h : ∞ ∈ s) : is_open s ↔ is_closed (coe ⁻¹' s : set X)ᶜ ∧ is_compact (coe ⁻¹' s : set X)ᶜ := by simp only [is_open_iff_of_mem' h, is_closed_compl_iff, and.comm] lemma is_open_iff_of_not_mem (h : ∞ ∉ s) : is_open s ↔ is_open (coe ⁻¹' s : set X) := by simp [is_open_def, h] lemma is_closed_iff_of_mem (h : ∞ ∈ s) : is_closed s ↔ is_closed (coe ⁻¹' s : set X) := have ∞ ∉ sᶜ, from λ H, H h, by rw [← is_open_compl_iff, is_open_iff_of_not_mem this, ← is_open_compl_iff, preimage_compl] lemma is_closed_iff_of_not_mem (h : ∞ ∉ s) : is_closed s ↔ is_closed (coe ⁻¹' s : set X) ∧ is_compact (coe ⁻¹' s : set X) := by rw [← is_open_compl_iff, is_open_iff_of_mem (mem_compl h), ← preimage_compl, compl_compl] @[simp] lemma is_open_image_coe {s : set X} : is_open (coe '' s : set (alexandroff X)) ↔ is_open s := by rw [is_open_iff_of_not_mem infty_not_mem_image_coe, preimage_image_eq _ coe_injective] lemma is_open_compl_image_coe {s : set X} : is_open (coe '' s : set (alexandroff X))ᶜ ↔ is_closed s ∧ is_compact s := begin rw [is_open_iff_of_mem, ← preimage_compl, compl_compl, preimage_image_eq _ coe_injective], exact infty_not_mem_image_coe end @[simp] lemma is_closed_image_coe {s : set X} : is_closed (coe '' s : set (alexandroff X)) ↔ is_closed s ∧ is_compact s := by rw [← is_open_compl_iff, is_open_compl_image_coe] /-- An open set in `alexandroff X` constructed from a closed compact set in `X` -/ def opens_of_compl (s : set X) (h₁ : is_closed s) (h₂ : is_compact s) : topological_space.opens (alexandroff X) := ⟨(coe '' s)ᶜ, is_open_compl_image_coe.2 ⟨h₁, h₂⟩⟩ lemma infty_mem_opens_of_compl {s : set X} (h₁ : is_closed s) (h₂ : is_compact s) : ∞ ∈ opens_of_compl s h₁ h₂ := mem_compl infty_not_mem_image_coe @[continuity] lemma continuous_coe : continuous (coe : X → alexandroff X) := continuous_def.mpr (λ s hs, hs.right) lemma is_open_map_coe : is_open_map (coe : X → alexandroff X) := λ s, is_open_image_coe.2 lemma open_embedding_coe : open_embedding (coe : X → alexandroff X) := open_embedding_of_continuous_injective_open continuous_coe coe_injective is_open_map_coe lemma is_open_range_coe : is_open (range (coe : X → alexandroff X)) := open_embedding_coe.open_range lemma is_closed_infty : is_closed ({∞} : set (alexandroff X)) := by { rw [← compl_range_coe, is_closed_compl_iff], exact is_open_range_coe } lemma nhds_coe_eq (x : X) : 𝓝 ↑x = map (coe : X → alexandroff X) (𝓝 x) := (open_embedding_coe.map_nhds_eq x).symm lemma nhds_within_coe_image (s : set X) (x : X) : 𝓝[coe '' s] (x : alexandroff X) = map coe (𝓝[s] x) := (open_embedding_coe.to_embedding.map_nhds_within_eq _ _).symm lemma nhds_within_coe (s : set (alexandroff X)) (x : X) : 𝓝[s] ↑x = map coe (𝓝[coe ⁻¹' s] x) := (open_embedding_coe.map_nhds_within_preimage_eq _ _).symm lemma comap_coe_nhds (x : X) : comap (coe : X → alexandroff X) (𝓝 x) = 𝓝 x := (open_embedding_coe.to_inducing.nhds_eq_comap x).symm /-- If `x` is not an isolated point of `X`, then `x : alexandroff X` is not an isolated point of `alexandroff X`. -/ instance nhds_within_compl_coe_ne_bot (x : X) [h : ne_bot (𝓝[≠] x)] : ne_bot (𝓝[≠] (x : alexandroff X)) := by simpa [nhds_within_coe, preimage, coe_eq_coe] using h.map coe lemma nhds_within_compl_infty_eq : 𝓝[≠] (∞ : alexandroff X) = map coe (coclosed_compact X) := begin refine (nhds_within_basis_open ∞ _).ext (has_basis_coclosed_compact.map _) _ _, { rintro s ⟨hs, hso⟩, refine ⟨_, (is_open_iff_of_mem hs).mp hso, _⟩, simp }, { rintro s ⟨h₁, h₂⟩, refine ⟨_, ⟨mem_compl infty_not_mem_image_coe, is_open_compl_image_coe.2 ⟨h₁, h₂⟩⟩, _⟩, simp [compl_image_coe, ← diff_eq, subset_preimage_image] } end /-- If `X` is a non-compact space, then `∞` is not an isolated point of `alexandroff X`. -/ instance nhds_within_compl_infty_ne_bot [noncompact_space X] : ne_bot (𝓝[≠] (∞ : alexandroff X)) := by { rw nhds_within_compl_infty_eq, apply_instance } @[priority 900] instance nhds_within_compl_ne_bot [∀ x : X, ne_bot (𝓝[≠] x)] [noncompact_space X] (x : alexandroff X) : ne_bot (𝓝[≠] x) := alexandroff.rec _ alexandroff.nhds_within_compl_infty_ne_bot (λ y, alexandroff.nhds_within_compl_coe_ne_bot y) x lemma nhds_infty_eq : 𝓝 (∞ : alexandroff X) = map coe (coclosed_compact X) ⊔ pure ∞ := by rw [← nhds_within_compl_infty_eq, nhds_within_compl_singleton_sup_pure] lemma has_basis_nhds_infty : (𝓝 (∞ : alexandroff X)).has_basis (λ s : set X, is_closed s ∧ is_compact s) (λ s, coe '' sᶜ ∪ {∞}) := begin rw nhds_infty_eq, exact (has_basis_coclosed_compact.map _).sup_pure _ end @[simp] lemma comap_coe_nhds_infty : comap (coe : X → alexandroff X) (𝓝 ∞) = coclosed_compact X := by simp [nhds_infty_eq, comap_sup, comap_map coe_injective] lemma le_nhds_infty {f : filter (alexandroff X)} : f ≤ 𝓝 ∞ ↔ ∀ s : set X, is_closed s → is_compact s → coe '' sᶜ ∪ {∞} ∈ f := by simp only [has_basis_nhds_infty.ge_iff, and_imp] lemma ultrafilter_le_nhds_infty {f : ultrafilter (alexandroff X)} : (f : filter (alexandroff X)) ≤ 𝓝 ∞ ↔ ∀ s : set X, is_closed s → is_compact s → coe '' s ∉ f := by simp only [le_nhds_infty, ← compl_image_coe, ultrafilter.mem_coe, ultrafilter.compl_mem_iff_not_mem] lemma tendsto_nhds_infty' {α : Type*} {f : alexandroff X → α} {l : filter α} : tendsto f (𝓝 ∞) l ↔ tendsto f (pure ∞) l ∧ tendsto (f ∘ coe) (coclosed_compact X) l := by simp [nhds_infty_eq, and_comm] lemma tendsto_nhds_infty {α : Type*} {f : alexandroff X → α} {l : filter α} : tendsto f (𝓝 ∞) l ↔ ∀ s ∈ l, f ∞ ∈ s ∧ ∃ t : set X, is_closed t ∧ is_compact t ∧ maps_to (f ∘ coe) tᶜ s := tendsto_nhds_infty'.trans $ by simp only [tendsto_pure_left, has_basis_coclosed_compact.tendsto_left_iff, forall_and_distrib, and_assoc, exists_prop] lemma continuous_at_infty' {Y : Type*} [topological_space Y] {f : alexandroff X → Y} : continuous_at f ∞ ↔ tendsto (f ∘ coe) (coclosed_compact X) (𝓝 (f ∞)) := tendsto_nhds_infty'.trans $ and_iff_right (tendsto_pure_nhds _ _) lemma continuous_at_infty {Y : Type*} [topological_space Y] {f : alexandroff X → Y} : continuous_at f ∞ ↔ ∀ s ∈ 𝓝 (f ∞), ∃ t : set X, is_closed t ∧ is_compact t ∧ maps_to (f ∘ coe) tᶜ s := continuous_at_infty'.trans $ by simp only [has_basis_coclosed_compact.tendsto_left_iff, exists_prop, and_assoc] lemma continuous_at_coe {Y : Type*} [topological_space Y] {f : alexandroff X → Y} {x : X} : continuous_at f x ↔ continuous_at (f ∘ coe) x := by rw [continuous_at, nhds_coe_eq, tendsto_map'_iff, continuous_at] /-- If `X` is not a compact space, then the natural embedding `X → alexandroff X` has dense range. -/ lemma dense_range_coe [noncompact_space X] : dense_range (coe : X → alexandroff X) := begin rw [dense_range, ← compl_infty], exact dense_compl_singleton _ end lemma dense_embedding_coe [noncompact_space X] : dense_embedding (coe : X → alexandroff X) := { dense := dense_range_coe, .. open_embedding_coe } @[simp] lemma specializes_coe {x y : X} : (x : alexandroff X) ⤳ y ↔ x ⤳ y := open_embedding_coe.to_inducing.specializes_iff @[simp] lemma inseparable_coe {x y : X} : inseparable (x : alexandroff X) y ↔ inseparable x y := open_embedding_coe.to_inducing.inseparable_iff lemma not_specializes_infty_coe {x : X} : ¬specializes ∞ (x : alexandroff X) := is_closed_infty.not_specializes rfl (coe_ne_infty x) lemma not_inseparable_infty_coe {x : X} : ¬inseparable ∞ (x : alexandroff X) := λ h, not_specializes_infty_coe h.specializes lemma not_inseparable_coe_infty {x : X} : ¬inseparable (x : alexandroff X) ∞ := λ h, not_specializes_infty_coe h.specializes' lemma inseparable_iff {x y : alexandroff X} : inseparable x y ↔ x = ∞ ∧ y = ∞ ∨ ∃ x' : X, x = x' ∧ ∃ y' : X, y = y' ∧ inseparable x' y' := by induction x using alexandroff.rec; induction y using alexandroff.rec; simp [not_inseparable_infty_coe, not_inseparable_coe_infty, coe_eq_coe] /-! ### Compactness and separation properties In this section we prove that `alexandroff X` is a compact space; it is a T₀ (resp., T₁) space if the original space satisfies the same separation axiom. If the original space is a locally compact Hausdorff space, then `alexandroff X` is a normal (hence, T₃ and Hausdorff) space. Finally, if the original space `X` is *not* compact and is a preconnected space, then `alexandroff X` is a connected space. -/ /-- For any topological space `X`, its one point compactification is a compact space. -/ instance : compact_space (alexandroff X) := { is_compact_univ := begin have : tendsto (coe : X → alexandroff X) (cocompact X) (𝓝 ∞), { rw [nhds_infty_eq], exact (tendsto_map.mono_left cocompact_le_coclosed_compact).mono_right le_sup_left }, convert ← this.is_compact_insert_range_of_cocompact continuous_coe, exact insert_none_range_some X end } /-- The one point compactification of a `t0_space` space is a `t0_space`. -/ instance [t0_space X] : t0_space (alexandroff X) := begin refine ⟨λ x y hxy, _⟩, rcases inseparable_iff.1 hxy with ⟨rfl, rfl⟩|⟨x, rfl, y, rfl, h⟩, exacts [rfl, congr_arg coe h.eq] end /-- The one point compactification of a `t1_space` space is a `t1_space`. -/ instance [t1_space X] : t1_space (alexandroff X) := { t1 := λ z, begin induction z using alexandroff.rec, { exact is_closed_infty }, { rw [← image_singleton, is_closed_image_coe], exact ⟨is_closed_singleton, is_compact_singleton⟩ } end } /-- The one point compactification of a locally compact Hausdorff space is a normal (hence, Hausdorff and regular) topological space. -/ instance [locally_compact_space X] [t2_space X] : normal_space (alexandroff X) := begin have key : ∀ z : X, ∃ u v : set (alexandroff X), is_open u ∧ is_open v ∧ ↑z ∈ u ∧ ∞ ∈ v ∧ disjoint u v, { intro z, rcases exists_open_with_compact_closure z with ⟨u, hu, huy', Hu⟩, exact ⟨coe '' u, (coe '' closure u)ᶜ, is_open_image_coe.2 hu, is_open_compl_image_coe.2 ⟨is_closed_closure, Hu⟩, mem_image_of_mem _ huy', mem_compl infty_not_mem_image_coe, (image_subset _ subset_closure).disjoint_compl_right⟩ }, refine @normal_of_compact_t2 _ _ _ ⟨λ x y hxy, _⟩, induction x using alexandroff.rec; induction y using alexandroff.rec, { exact (hxy rfl).elim }, { rcases key y with ⟨u, v, hu, hv, hxu, hyv, huv⟩, exact ⟨v, u, hv, hu, hyv, hxu, huv.symm⟩ }, { exact key x }, { exact separated_by_open_embedding open_embedding_coe (mt coe_eq_coe.mpr hxy) } end /-- If `X` is not a compact space, then `alexandroff X` is a connected space. -/ instance [preconnected_space X] [noncompact_space X] : connected_space (alexandroff X) := { to_preconnected_space := dense_embedding_coe.to_dense_inducing.preconnected_space, to_nonempty := infer_instance } /-- If `X` is an infinite type with discrete topology (e.g., `ℕ`), then the identity map from `cofinite_topology (alexandroff X)` to `alexandroff X` is not continuous. -/ lemma not_continuous_cofinite_topology_of_symm [infinite X] [discrete_topology X] : ¬(continuous (@cofinite_topology.of (alexandroff X)).symm) := begin inhabit X, simp only [continuous_iff_continuous_at, continuous_at, not_forall], use [cofinite_topology.of ↑(default : X)], simpa [nhds_coe_eq, nhds_discrete, cofinite_topology.nhds_eq] using (finite_singleton ((default : X) : alexandroff X)).infinite_compl end end alexandroff /-- A concrete counterexample shows that `continuous.homeo_of_equiv_compact_to_t2` cannot be generalized from `t2_space` to `t1_space`. Let `α = alexandroff ℕ` be the one-point compactification of `ℕ`, and let `β` be the same space `alexandroff ℕ` with the cofinite topology. Then `α` is compact, `β` is T1, and the identity map `id : α → β` is a continuous equivalence that is not a homeomorphism. -/ lemma continuous.homeo_of_equiv_compact_to_t2.t1_counterexample : ∃ (α β : Type) (Iα : topological_space α) (Iβ : topological_space β), by exactI compact_space α ∧ t1_space β ∧ ∃ f : α ≃ β, continuous f ∧ ¬ continuous f.symm := ⟨alexandroff ℕ, cofinite_topology (alexandroff ℕ), infer_instance, infer_instance, infer_instance, infer_instance, cofinite_topology.of, cofinite_topology.continuous_of, alexandroff.not_continuous_cofinite_topology_of_symm⟩
e3db1716cf797a5e635250046ac10d5bc393ed04
9dc8cecdf3c4634764a18254e94d43da07142918
/src/combinatorics/catalan.lean
9d8ea674b350d563186f457a0d20f11e89dabd20
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
5,245
lean
/- Copyright (c) 2022 Julian Kuelshammer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Julian Kuelshammer -/ import data.nat.choose.central import algebra.big_operators.fin import tactic.field_simp import tactic.linear_combination /-! # Catalan numbers The Catalan numbers (http://oeis.org/A000108) are probably the most ubiquitous sequence of integers in mathematics. They enumerate several important objects like binary trees, Dyck paths, and triangulations of convex polygons. ## Main definitions * `catalan n`: the `n`th Catalan number, defined recursively as `catalan (n + 1) = ∑ i : fin n.succ, catalan i * catalan (n - i)`. ## Main results * `catalan_eq_central_binom_div `: The explicit formula for the Catalan number using the central binomial coefficient, `catalan n = nat.central_binom n / (n + 1)`. ## Implementation details The proof of `catalan_eq_central_binom_div` follows https://math.stackexchange.com/questions/3304415/catalan-numbers-algebraic-proof-of-the-recurrence-relation ## TODO * Prove that the Catalan numbers enumerate many interesting objects. * Provide the many variants of Catalan numbers, e.g. associated to complex reflection groups, Fuss-Catalan, etc. -/ open_locale big_operators open finset /-- The recursive definition of the sequence of Catalan numbers: `catalan (n + 1) = ∑ i : fin n.succ, catalan i * catalan (n - i)` -/ def catalan : ℕ → ℕ | 0 := 1 | (n + 1) := ∑ i : fin n.succ, have _ := i.2, have _ := nat.lt_succ_iff.mpr (n.sub_le i), catalan i * catalan (n - i) @[simp] lemma catalan_zero : catalan 0 = 1 := by rw catalan lemma catalan_succ (n : ℕ) : catalan (n + 1) = ∑ i : fin n.succ, catalan i * catalan (n - i) := by rw catalan @[simp] lemma catalan_one : catalan 1 = 1 := by simp [catalan_succ] /-- A helper sequence that can be used to prove the equality of the recursive and the explicit definition using a telescoping sum argument. -/ private def gosper_catalan (n j : ℕ) : ℚ := nat.central_binom j * nat.central_binom (n - j) * (2 * j - n) / (2 * n * (n + 1)) private lemma gosper_trick {n i : ℕ} (h : i ≤ n) : gosper_catalan (n+1) (i+1) - gosper_catalan (n+1) i = nat.central_binom i / (i + 1) * nat.central_binom (n - i) / (n - i + 1) := begin have : (n:ℚ) + 1 ≠ 0 := by exact_mod_cast n.succ_ne_zero, have : (n:ℚ) + 1 + 1 ≠ 0 := by exact_mod_cast (n + 1).succ_ne_zero, have : (i:ℚ) + 1 ≠ 0 := by exact_mod_cast i.succ_ne_zero, have : (n:ℚ) - i + 1 ≠ 0 := by exact_mod_cast (n - i).succ_ne_zero, have h₁ : ((i:ℚ) + 1) * (i + 1).central_binom = 2 * (2 * i + 1) * i.central_binom, { exact_mod_cast nat.succ_mul_central_binom_succ i }, have h₂ : ((n:ℚ) - i + 1) * (n - i + 1).central_binom = 2 * (2 * (n - i) + 1) * (n - i).central_binom, { exact_mod_cast nat.succ_mul_central_binom_succ (n - i) }, simp only [gosper_catalan], push_cast, field_simp, rw (nat.succ_sub h), linear_combination (2:ℚ) * (n - i).central_binom * (i + 1 - (n - i)) * (n + 1) * (n + 2) * ((n - i) + 1) * h₁ - 2 * i.central_binom * (n + 1) * (n + 2) * (i - (n - i) - 1) * (i + 1) * h₂, end private lemma gosper_catalan_sub_eq_central_binom_div (n : ℕ) : gosper_catalan (n + 1) (n + 1) - gosper_catalan (n + 1) 0 = nat.central_binom (n + 1) / (n + 2) := begin have : (n:ℚ) + 1 ≠ 0 := by exact_mod_cast n.succ_ne_zero, have : (n:ℚ) + 1 + 1 ≠ 0 := by exact_mod_cast (n + 1).succ_ne_zero, have h : (n:ℚ) + 2 ≠ 0 := by exact_mod_cast (n + 1).succ_ne_zero, simp only [gosper_catalan, nat.sub_zero, nat.central_binom_zero, nat.sub_self], field_simp, ring, end theorem catalan_eq_central_binom_div (n : ℕ) : catalan n = n.central_binom / (n + 1) := begin suffices : (catalan n : ℚ) = nat.central_binom n / (n + 1), { have h := nat.succ_dvd_central_binom n, exact_mod_cast this }, induction n using nat.case_strong_induction_on with d hd, { simp }, { simp_rw [catalan_succ, nat.cast_sum, nat.cast_mul], transitivity (∑ i : fin d.succ, (nat.central_binom i / (i + 1)) * (nat.central_binom (d - i) / (d - i + 1)) : ℚ), { refine sum_congr rfl (λ i _, _), congr, { exact_mod_cast hd i i.is_le }, { rw_mod_cast hd (d - i), push_cast, rw nat.cast_sub i.is_le, exact tsub_le_self }, }, { transitivity ∑ i : fin d.succ, (gosper_catalan (d + 1) (i + 1) - gosper_catalan (d + 1) i), { refine sum_congr rfl (λ i _, _), rw_mod_cast [gosper_trick i.is_le, mul_div] }, { rw [← sum_range (λi, gosper_catalan (d + 1) (i + 1) - gosper_catalan (d + 1) i), sum_range_sub, nat.succ_eq_add_one], exact_mod_cast gosper_catalan_sub_eq_central_binom_div d } } } end theorem succ_mul_catalan_eq_central_binom (n : ℕ) : (n+1) * catalan n = n.central_binom := (nat.eq_mul_of_div_eq_right n.succ_dvd_central_binom (catalan_eq_central_binom_div n).symm).symm lemma catalan_two : catalan 2 = 2 := by norm_num [catalan_eq_central_binom_div, nat.central_binom, nat.choose] lemma catalan_three : catalan 3 = 5 := by norm_num [catalan_eq_central_binom_div, nat.central_binom, nat.choose]
582d823ff21a0020885ebc25ae4433a8fd2abaef
ca1ad81c8733787aba30f7a8d63f418508e12812
/clfrags/src/hilbert/wr/proofs/ad.lean
9ee94dd2b0eda7f7d01bc269dd5958da0c93d5d1
[]
no_license
greati/hilbert-classical-fragments
5cdbe07851e979c8a03c621a5efd4d24bbfa333a
18a21ac6b2e890060eb4ae65752fc0245394d226
refs/heads/master
1,591,973,117,184
1,573,822,710,000
1,573,822,710,000
194,334,439
2
0
null
null
null
null
UTF-8
Lean
false
false
14,144
lean
import hilbert.wr.ad namespace clfrags namespace hilbert namespace wr namespace ad theorem ad₂₆ {a b c : Prop} (h₁ : a) (h₂ : b) : ad a b c := have h₃ : ad (ad a b c) b a, from ad₂ h₂, show ad a b c, from ad₁ h₁ h₃ theorem ad₄' {a b c : Prop} (h₁ : a) : ad c a (ad c a (ad b a c)) := have h₂ : ad a a a, from ad₂₆ h₁ h₁, have h₃ : ad (ad c a (ad c a (ad b a c))) a a, from ad₄ h₂, show ad c a (ad c a (ad b a c)), from ad₁ h₁ h₃ theorem ad₂₇ {a b c d e : Prop} (h₁ : ad (ad a b c) d e) : ad (ad a d e) b c := have h₂ : (ad a d e) or (ad a b c), from ad₁₀ h₁, have h₃ : (ad a b c) or (ad a d e), from ad₁₃ h₂, show ad (ad a d e) b c, from ad₁₁ h₃ theorem ad₆' {a b : Prop} (h₁ : a) : a or b := ad₆ h₁ theorem ad₁_ad {a b c d e : Prop} (h₁ : ad c d e) (h₂ : ad (ad a b c) d e) : ad a d e := have h₃ : (ad (ad a b c) d e) → ((ad a d e) or (ad a b c)), from ad₁₀, have h₄ : ad a d e → ad a d e, from R, have h₅ : ad c d e → ad a b c → ad a d e, from ad₉, have h₆ : ad c d e → ad a d e → ad a d e, from M₁ h₄, have h₇ : ad c d e → ((ad a d e) or (ad a b c)) → ad a d e, from δ_or₂ h₆ h₅, have h₈ : ad c d e → (ad (ad a b c) d e) → ((ad a d e) or (ad a b c)), from M₁ h₃, show ad a d e, from (T₂ h₈ h₇) h₁ h₂ theorem ad₂_ad {a b c d e : Prop} (h₁ : ad a d e) : ad (ad (ad c a b) a c) d e := let b' := ad c a b in have h₂ : (ad a d e) → (((ad c a c) or b') or ((ad c d e) or b')), from (assume h, ad₂₀ $ ad₁₃ $ ad₂₀ $ ad₁₀ $ ad₈ $ ad₁₃ $ ad₆' h), have h₃ : ((ad c a c) or b') → ((ad b' d e) or (ad b' a c)), from (assume h, ad₁₃ $ ad₆' $ ad₁₁ h), have h₄ : ((ad c d e) or b') → ((ad b' d e) or (ad b' a c)), from (assume h, ad₆' $ ad₁₁ h), have h₅ : (((ad c a c) or b') or ((ad c d e) or b')) → ((ad b' d e) or (ad b' a c)), from δ_or₁ h₃ h₄, have h₆ : ad a d e → ((ad b' d e) or (ad b' a c)), from T₁ h₂ h₅, show ad (ad b' a c) d e, from ad₁₁ (h₆ h₁) theorem ad₃_ad {a b c d e f g h : Prop} (h₁ : ad (ad a b c) g h) : ad (ad (ad (ad (ad f a d) a (ad e a d)) a (ad (ad f a e) a d)) b c) g h := let j := ad (ad f a d) a (ad e a d), k := ad (ad f a e) a d, i := ad j a k in have h₂ : (ad (ad a b c) g h) → ((ad a g h) or (ad a b c)), from ad₁₀, have h₃ : ad a g h → ad (ad i b c) g h, from (assume h₃₁, ad₁₁ $ ad₆' $ ad₃ h₃₁), have h₄ : ad a b c → ad (ad i b c) g h, from (assume h₄₁, ad₆ $ ad₃ h₄₁), have h₅ : ((ad a g h) or (ad a b c)) → ad (ad i b c) g h, from (δ_or₁ h₃ h₄), (T₁ h₂ h₅) h₁ theorem ad₄_ad {a b c d e f g : Prop} (h₁ : ad (ad a b c) f g) : ad (ad (ad d a (ad d a (ad e a d))) b c) f g := let h := ad d a (ad d a (ad e a d)) in have h₂ : (ad (ad a b c) f g) → ((ad a f g) or (ad a b c)), from ad₁₀, have h₃ : ad a f g → ad (ad h b c) f g, from (assume h₃₁, ad₁₁ $ ad₆' $ ad₄ h₃₁), have h₄ : ad a b c → ad (ad h b c) f g, from (assume h₄₁, ad₆ $ ad₄ h₄₁), have h₅ : ((ad a f g) or (ad a b c)) → ad (ad h b c) f g, from δ_or₁ h₃ h₄, (T₁ h₂ h₅) h₁ theorem ad₅_ad {a b c d e : Prop} (h₁ : ad (ad a b c) d e) : ad (b or a) d e := have h₂ : ad (ad a b c) d e → ((ad a d e) or (ad a b c)), from ad₁₀, have h₃ : (ad a d e) → (ad (b or a) d e), from (assume h, ad₈ $ ad₁₃ $ ad₆' h), have h₄ : (ad a b c) → (ad (b or a) d e), from (assume h, ad₆ $ ad₅ h), have h₅ : ((ad a d e) or (ad a b c)) → (ad (b or a) d e), from δ_or₁ h₃ h₄, (T₁ h₂ h₅) h₁ theorem ad₆_ad {a b c d e : Prop} (h₁ : ad a d e) : ad (ad a b c) d e := ad₁₁ $ ad₆' h₁ theorem ad₇_ad {a b c d e f : Prop} (h₁ : ad (ad (c or d) a b) e f) : ad ((ad c a b) or (ad d a b)) e f := have h₂ : ad (ad (c or d) a b) e f → ((ad (c or d) e f) or (ad (c or d) a b)), from ad₁₀, have h₃ : ad (c or d) e f → ((ad c e f) or (ad d e f)), from ad₇, have h₄ : ad c e f → ad ((ad c a b) or (ad d a b)) e f, from (assume h, ad₈ $ ad₆' $ ad₁₁ $ ad₆' h), have h₅ : ad d e f → ad ((ad c a b) or (ad d a b)) e f, from (assume h, ad₈ $ ad₁₃ $ ad₆' $ ad₁₁ $ ad₆' h), have h₆ : ((ad c e f) or (ad d e f)) → ad ((ad c a b) or (ad d a b)) e f, from δ_or₁ h₄ h₅, have h₇ : ad (c or d) e f → ad ((ad c a b) or (ad d a b)) e f, from T₁ h₃ h₆, have h₈ : ad (c or d) a b → ad ((ad c a b) or (ad d a b)) e f, from (assume h, ad₆ $ ad₇ h), have h₉ : ((ad (c or d) e f) or (ad (c or d) a b)) → ad ((ad c a b) or (ad d a b)) e f, from δ_or₁ h₇ h₈, (T₁ h₂ h₉) h₁ theorem ad₁₂_ad {a b c: Prop} (h₁ : ad (a or a) b c) : ad a b c := ad₁₂ $ ad₇ h₁ theorem ad₁₃_ad {a b c d : Prop} (h₁ : ad (a or b) c d) : ad (b or a) c d := ad₈ $ ad₁₃ $ ad₇ h₁ theorem ad₁₄_ad {a b c d e : Prop} (h₁ : ad (a or (b or c)) d e) : ad ((a or b) or c) d e := ad₈ $ ad₁₃ $ ad₂₂ $ ad₁₃ $ ad₁₄ $ ad₂₁ $ ad₇ h₁ theorem ad₈' {a b c d e f : Prop} (h₁ : ad (ad c a b) e f) : ad (ad (c or d) a b) e f := have h₂ : (ad (ad c a b) e f) → ((ad c e f) or (ad c a b)), from ad₁₀, have h₃ : (ad c e f) → (ad (ad (c or d) a b) e f), from (assume h, ad₂₇ $ ad₆ $ ad₆_ad h), have h₄ : (ad c a b) → (ad (ad (c or d) a b) e f), from (assume h, ad₆ $ ad₆_ad h), have h₅ : ((ad c e f) or (ad c a b)) → (ad (ad (c or d) a b) e f), from δ_or₁ h₃ h₄, show ad (ad (c or d) a b) e f, from (T₁ h₂ h₅) h₁ theorem ad₈'' {a b c d e f : Prop} (h₁ : ad (ad (c or d) a b) e f) : ad (ad (d or c) a b) e f := have h₃₁ : (ad (ad (c or d) a b) e f) → ((ad (c or d) e f) or (ad (c or d) a b)), from ad₁₀, have h₃₂ : ad (c or d) e f → ad (ad (d or c) a b) e f, from (assume h, ad₂₇ $ ad₆ $ ad₁₃_ad h), have h₃₃ : ad (c or d) a b → ad (ad (d or c) a b) e f, from (assume h, ad₆ $ ad₁₃_ad h), have h₃₄ : ((ad (c or d) e f) or (ad (c or d) a b)) → ad (ad (d or c) a b) e f, from δ_or₁ h₃₂ h₃₃, (T₁ h₃₁ h₃₄) h₁ theorem ad₈_ad {a b c d e f : Prop} (h₁ : ad ((ad c a b) or (ad d a b)) e f) : ad (ad (c or d) a b) e f := have h₂ : ad ((ad c a b) or (ad d a b)) e f → ((ad (ad c a b) e f) or (ad (ad d a b) e f)), from ad₇, have h₃ : ad (ad c a b) e f → ad (ad (c or d) a b) e f, from ad₈', have h₄ : ad (ad d a b) e f → ad (ad (c or d) a b) e f, from (assume h, ad₈'' $ ad₈' h), have h₅ : ((ad (ad c a b) e f) or (ad (ad d a b) e f)) → ad (ad (c or d) a b) e f, from δ_or₁ h₃ h₄, (T₁ h₂ h₅) h₁ theorem ad₉_ad {a b c d e f g : Prop} (h₁ : ad (ad a b c) f g) (h₂ : ad (ad d e a) f g) : ad (ad d b c) f g := let g' := ad d f g, c' := ad d b c in have h₃ : ad (ad a b c) f g → ((ad a f g) or (ad a b c)), from ad₁₀, have h₄ : ad (ad d e a) f g → (g' or (ad d e a)), from ad₁₀, have h₅ : ad a b c → ad d f g → (g' or c'), from M₁ ad₆', have h₆ : ad a b c → ad d e a → (g' or c'), from (assume h, assume i, ad₁₃ $ ad₆' $ ad₉ h i), have h₇ : ad a f g → ad d f g → (g' or c'), from M₁ ad₆', have h₈ : ad a f g → ad d e a → (g' or c'), from (assume h, assume i, ad₆' $ ad₉ h i), have h₉ : (g' or (ad d e a)) → ad a b c → (g' or c'), from flip (δ_or₂ h₅ h₆), have h₁₀ : (g' or (ad d e a)) → ad a f g → (g' or c'), from flip (δ_or₂ h₇ h₈), have h₁₁ : (g' or (ad d e a)) → ((ad a f g) or (ad a b c)) → (g' or c'), from δ_or₂ h₁₀ h₉, have h₁₂ : (ad (ad a b c) f g) → (g' or (ad d e a)) → (g' or c'), from flip (T₂ (M₁ h₃) h₁₁), have h₁₃ : (ad (ad a b c) f g) → (ad (ad d e a) f g) → (g' or c'), from T₂ (M₁ h₄) h₁₂, ad₁₁ (h₁₃ h₁ h₂) theorem ad₁₀_ad {a b c d e f g : Prop} (h₁ : ad (ad (ad e d c) a b) f g) : ad ((ad e a b) or (ad e d c)) f g := have h₂ : ad (ad (ad e d c) a b) f g → ((ad (ad e d c) f g) or (ad (ad e d c) a b)), from ad₁₀, have h₃ : ad (ad e d c) f g → ad ((ad e a b) or (ad e d c)) f g, from (assume h, ad₈ $ ad₁₃ $ ad₆' h), have h₄ : ad (ad e d c) a b → ad ((ad e a b) or (ad e d c)) f g, from (assume h, ad₆ $ ad₁₀ h), have h₅ : ((ad (ad e d c) f g) or (ad (ad e d c) a b)) → ad ((ad e a b) or (ad e d c)) f g, from δ_or₁ h₃ h₄, (T₁ h₂ h₅) h₁ theorem ad₁₁_ad {a b c d e f g : Prop} (h₁ : ad ((ad e a b) or (ad e d c)) f g) : ad (ad (ad e d c) a b) f g := have h₂ : ad ((ad e a b) or (ad e d c)) f g → ((ad (ad e a b) f g) or (ad (ad e d c) f g)), from ad₇, have h₃ : ad (ad e a b) f g → ((ad e f g) or (ad e a b)), from ad₁₀, have h₄ : ad e f g → ((ad e f g) or (ad e d c)), from ad₆', have h₅ : ((ad e f g) or (ad e d c)) → ad (ad (ad e d c) a b) f g, from (assume h, ad₁₁ $ ad₁₃ $ ad₁₀ $ ad₆ $ ad₁₁ h), have h₆ : ad e f g → ad (ad (ad e d c) a b) f g, from T₁ h₄ h₅, have h₇ : ((ad e a b) or (ad e d c)) → ad (ad (ad e d c) a b) f g, from (assume h, ad₆ $ ad₁₁ h), have h₈ : ad e a b → ((ad e a b) or (ad e d c)), from ad₆', have h₉ : ad e a b → ad (ad (ad e d c) a b) f g, from T₁ h₈ h₇, have h₁₀ : ((ad e f g) or (ad e a b)) → ad (ad (ad e d c) a b) f g, from δ_or₁ h₆ h₉, have h₁₁ : ad (ad e a b) f g → ad (ad (ad e d c) a b) f g, from T₁ h₃ h₁₀, have h₁₂ : ad (ad e d c) f g → ad (ad (ad e d c) a b) f g, from (assume h, ad₂₇ $ ad₆ $ ad₁₁ $ ad₁₀ h), have h₁₃ : ((ad (ad e a b) f g) or (ad (ad e d c) f g)) → ad (ad (ad e d c) a b) f g, from δ_or₁ h₁₁ h₁₂, (T₁ h₂ h₁₃) h₁ end ad end wr end hilbert end clfrags
61a2abb9d7515b070ab7d4b07dbe5acb70dffab0
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/simpJpCasesDepBug.lean
cbff64f44338c081d01caa86698b2bd2149efb14
[ "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,507
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.Command import Lean.Meta.Closure import Lean.Meta.SizeOf import Lean.Meta.Injective import Lean.Meta.Structure import Lean.Meta.AppBuilder import Lean.Elab.Command import Lean.Elab.DeclModifiers import Lean.Elab.DeclUtil import Lean.Elab.Inductive import Lean.Elab.DeclarationRange import Lean.Elab.Binders namespace Lean.Elab.Command open Meta inductive StructFieldKind where | newField | copiedField | fromParent | subobject deriving Inhabited, DecidableEq, Repr structure StructFieldInfo where name : Name declName : Name -- Remark: for `fromParent` fields, `declName` is only relevant in the generation of auxiliary "default value" functions. fvar : Expr kind : StructFieldKind value? : Option Expr := none deriving Inhabited, Repr private def addCtorFields (fieldInfos : Array StructFieldInfo) : Nat → Expr → TermElabM Expr | 0, type => pure type | i+1, type => do let info := fieldInfos[i]! let decl ← Term.getFVarLocalDecl! info.fvar let type ← instantiateMVars type let type := type.abstract #[info.fvar] match info.kind with | StructFieldKind.fromParent => let val := decl.value addCtorFields fieldInfos i (type.instantiate1 val) | _ => addCtorFields fieldInfos i (mkForall decl.userName decl.binderInfo decl.type type)
8cd1dd6cd0a775f98b179c9d655a77bf13f037b6
ff5230333a701471f46c57e8c115a073ebaaa448
/library/init/data/rbtree/basic.lean
ab488a8f30adf85d2a1a61691e67b2bd5ccc9722
[ "Apache-2.0" ]
permissive
stanford-cs242/lean
f81721d2b5d00bc175f2e58c57b710d465e6c858
7bd861261f4a37326dcf8d7a17f1f1f330e4548c
refs/heads/master
1,600,957,431,849
1,576,465,093,000
1,576,465,093,000
225,779,423
0
3
Apache-2.0
1,575,433,936,000
1,575,433,935,000
null
UTF-8
Lean
false
false
6,875
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.data.ordering universes u v inductive rbnode (α : Type u) | leaf {} : rbnode | red_node (lchild : rbnode) (val : α) (rchild : rbnode) : rbnode | black_node (lchild : rbnode) (val : α) (rchild : rbnode) : rbnode namespace rbnode variables {α : Type u} {β : Type v} inductive color | red | black open color nat instance color.decidable_eq : decidable_eq color := λ a b, color.cases_on a (color.cases_on b (is_true rfl) (is_false (λ h, color.no_confusion h))) (color.cases_on b (is_false (λ h, color.no_confusion h)) (is_true rfl)) def depth (f : nat → nat → nat) : rbnode α → nat | leaf := 0 | (red_node l _ r) := succ (f (depth l) (depth r)) | (black_node l _ r) := succ (f (depth l) (depth r)) protected def min : rbnode α → option α | leaf := none | (red_node leaf v _) := some v | (black_node leaf v _) := some v | (red_node l v _) := min l | (black_node l v _) := min l protected def max : rbnode α → option α | leaf := none | (red_node _ v leaf) := some v | (black_node _ v leaf) := some v | (red_node _ v r) := max r | (black_node _ v r) := max r def fold (f : α → β → β) : rbnode α → β → β | leaf b := b | (red_node l v r) b := fold r (f v (fold l b)) | (black_node l v r) b := fold r (f v (fold l b)) def rev_fold (f : α → β → β) : rbnode α → β → β | leaf b := b | (red_node l v r) b := rev_fold l (f v (rev_fold r b)) | (black_node l v r) b := rev_fold l (f v (rev_fold r b)) def balance1 : rbnode α → α → rbnode α → α → rbnode α → rbnode α | (red_node l x r₁) y r₂ v t := red_node (black_node l x r₁) y (black_node r₂ v t) | l₁ y (red_node l₂ x r) v t := red_node (black_node l₁ y l₂) x (black_node r v t) | l y r v t := black_node (red_node l y r) v t def balance1_node : rbnode α → α → rbnode α → rbnode α | (red_node l x r) v t := balance1 l x r v t | (black_node l x r) v t := balance1 l x r v t | leaf v t := t /- dummy value -/ def balance2 : rbnode α → α → rbnode α → α → rbnode α → rbnode α | (red_node l x₁ r₁) y r₂ v t := red_node (black_node t v l) x₁ (black_node r₁ y r₂) | l₁ y (red_node l₂ x₂ r₂) v t := red_node (black_node t v l₁) y (black_node l₂ x₂ r₂) | l y r v t := black_node t v (red_node l y r) def balance2_node : rbnode α → α → rbnode α → rbnode α | (red_node l x r) v t := balance2 l x r v t | (black_node l x r) v t := balance2 l x r v t | leaf v t := t /- dummy -/ def get_color : rbnode α → color | (red_node _ _ _) := red | _ := black section insert variables (lt : α → α → Prop) [decidable_rel lt] def ins : rbnode α → α → rbnode α | leaf x := red_node leaf x leaf | (red_node a y b) x := match cmp_using lt x y with | ordering.lt := red_node (ins a x) y b | ordering.eq := red_node a x b | ordering.gt := red_node a y (ins b x) end | (black_node a y b) x := match cmp_using lt x y with | ordering.lt := if a.get_color = red then balance1_node (ins a x) y b else black_node (ins a x) y b | ordering.eq := black_node a x b | ordering.gt := if b.get_color = red then balance2_node (ins b x) y a else black_node a y (ins b x) end def mk_insert_result : color → rbnode α → rbnode α | red (red_node l v r) := black_node l v r | _ t := t def insert (t : rbnode α) (x : α) : rbnode α := mk_insert_result (get_color t) (ins lt t x) end insert section membership variable (lt : α → α → Prop) def mem : α → rbnode α → Prop | a leaf := false | a (red_node l v r) := mem a l ∨ (¬ lt a v ∧ ¬ lt v a) ∨ mem a r | a (black_node l v r) := mem a l ∨ (¬ lt a v ∧ ¬ lt v a) ∨ mem a r def mem_exact : α → rbnode α → Prop | a leaf := false | a (red_node l v r) := mem_exact a l ∨ a = v ∨ mem_exact a r | a (black_node l v r) := mem_exact a l ∨ a = v ∨ mem_exact a r variable [decidable_rel lt] def find : rbnode α → α → option α | leaf x := none | (red_node a y b) x := match cmp_using lt x y with | ordering.lt := find a x | ordering.eq := some y | ordering.gt := find b x end | (black_node a y b) x := match cmp_using lt x y with | ordering.lt := find a x | ordering.eq := some y | ordering.gt := find b x end end membership inductive well_formed (lt : α → α → Prop) : rbnode α → Prop | leaf_wff : well_formed leaf | insert_wff {n n' : rbnode α} {x : α} [decidable_rel lt] : well_formed n → n' = insert lt n x → well_formed n' end rbnode open rbnode set_option auto_param.check_exists false def rbtree (α : Type u) (lt : α → α → Prop . rbtree.default_lt) : Type u := {t : rbnode α // t.well_formed lt } def mk_rbtree (α : Type u) (lt : α → α → Prop . rbtree.default_lt) : rbtree α lt := ⟨leaf, well_formed.leaf_wff lt⟩ namespace rbtree variables {α : Type u} {β : Type v} {lt : α → α → Prop} protected def mem (a : α) (t : rbtree α lt) : Prop := rbnode.mem lt a t.val instance : has_mem α (rbtree α lt) := ⟨rbtree.mem⟩ def mem_exact (a : α) (t : rbtree α lt) : Prop := rbnode.mem_exact a t.val def depth (f : nat → nat → nat) (t : rbtree α lt) : nat := t.val.depth f def fold (f : α → β → β) : rbtree α lt → β → β | ⟨t, _⟩ b := t.fold f b def rev_fold (f : α → β → β) : rbtree α lt → β → β | ⟨t, _⟩ b := t.rev_fold f b def empty : rbtree α lt → bool | ⟨leaf, _⟩ := tt | _ := ff def to_list : rbtree α lt → list α | ⟨t, _⟩ := t.rev_fold (::) [] protected def min : rbtree α lt → option α | ⟨t, _⟩ := t.min protected def max : rbtree α lt → option α | ⟨t, _⟩ := t.max instance [has_repr α] : has_repr (rbtree α lt) := ⟨λ t, "rbtree_of " ++ repr t.to_list⟩ variables [decidable_rel lt] def insert : rbtree α lt → α → rbtree α lt | ⟨t, w⟩ x := ⟨t.insert lt x, well_formed.insert_wff w rfl⟩ def find : rbtree α lt → α → option α | ⟨t, _⟩ x := t.find lt x def contains (t : rbtree α lt) (a : α) : bool := (t.find a).is_some def from_list (l : list α) (lt : α → α → Prop . rbtree.default_lt) [decidable_rel lt] : rbtree α lt := l.foldl insert (mk_rbtree α lt) end rbtree def rbtree_of {α : Type u} (l : list α) (lt : α → α → Prop . rbtree.default_lt) [decidable_rel lt] : rbtree α lt := rbtree.from_list l lt
f51a6bc42dc8f50cc30bba28a9a1942a5ae9b471
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/order/ring/cone.lean
ee0647139a646aeb66ff7182ee3483fd0fcd6909
[ "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
2,657
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro -/ import algebra.order.ring.defs /-! # Constructing an ordered ring from a ring with a specified positive cone. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ /-! ### Positive cones -/ set_option old_structure_cmd true variables {α : Type*} [ring α] [nontrivial α] namespace ring /-- A positive cone in a ring consists of a positive cone in underlying `add_comm_group`, which contains `1` and such that the positive elements are closed under multiplication. -/ @[nolint has_nonempty_instance] structure positive_cone (α : Type*) [ring α] extends add_comm_group.positive_cone α := (one_nonneg : nonneg 1) (mul_pos : ∀ (a b), pos a → pos b → pos (a * b)) /-- Forget that a positive cone in a ring respects the multiplicative structure. -/ add_decl_doc positive_cone.to_positive_cone /-- A total positive cone in a nontrivial ring induces a linear order. -/ @[nolint has_nonempty_instance] structure total_positive_cone (α : Type*) [ring α] extends positive_cone α, add_comm_group.total_positive_cone α /-- Forget that a `total_positive_cone` in a ring is total. -/ add_decl_doc total_positive_cone.to_positive_cone /-- Forget that a `total_positive_cone` in a ring respects the multiplicative structure. -/ add_decl_doc total_positive_cone.to_total_positive_cone lemma positive_cone.one_pos (C : positive_cone α) : C.pos 1 := (C.pos_iff _).2 ⟨C.one_nonneg, λ h, one_ne_zero $ C.nonneg_antisymm C.one_nonneg h⟩ end ring open ring /-- Construct a `strict_ordered_ring` by designating a positive cone in an existing `ring`. -/ def strict_ordered_ring.mk_of_positive_cone (C : positive_cone α) : strict_ordered_ring α := { exists_pair_ne := ⟨0, 1, λ h, by simpa [←h, C.pos_iff] using C.one_pos⟩, zero_le_one := by { change C.nonneg (1 - 0), convert C.one_nonneg, simp, }, mul_pos := λ x y xp yp, begin change C.pos (x*y - 0), convert C.mul_pos x y (by { convert xp, simp, }) (by { convert yp, simp, }), simp, end, ..‹ring α›, ..ordered_add_comm_group.mk_of_positive_cone C.to_positive_cone } /-- Construct a `linear_ordered_ring` by designating a positive cone in an existing `ring`. -/ def linear_ordered_ring.mk_of_positive_cone (C : total_positive_cone α) : linear_ordered_ring α := { ..strict_ordered_ring.mk_of_positive_cone C.to_positive_cone, ..linear_ordered_add_comm_group.mk_of_positive_cone C.to_total_positive_cone, }
823ec0a496a9c52c299021155c6c6cbef5be9b63
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/topology/instances/ennreal.lean
6e77656d9e8a00407e7ca6c7444838a2e7299daf
[ "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
55,106
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import topology.instances.nnreal import topology.algebra.ordered.liminf_limsup /-! # Extended non-negative reals -/ noncomputable theory open classical set filter metric open_locale classical topological_space ennreal nnreal big_operators filter variables {α : Type*} {β : Type*} {γ : Type*} namespace ennreal variables {a b c d : ℝ≥0∞} {r p q : ℝ≥0} variables {x y z : ℝ≥0∞} {ε ε₁ ε₂ : ℝ≥0∞} {s : set ℝ≥0∞} section topological_space open topological_space /-- Topology on `ℝ≥0∞`. Note: this is different from the `emetric_space` topology. The `emetric_space` topology has `is_open {⊤}`, while this topology doesn't have singleton elements. -/ instance : topological_space ℝ≥0∞ := preorder.topology ℝ≥0∞ instance : order_topology ℝ≥0∞ := ⟨rfl⟩ instance : t2_space ℝ≥0∞ := by apply_instance -- short-circuit type class inference instance : second_countable_topology ℝ≥0∞ := ⟨⟨⋃q ≥ (0:ℚ), {{a : ℝ≥0∞ | a < real.to_nnreal q}, {a : ℝ≥0∞ | ↑(real.to_nnreal q) < a}}, (countable_encodable _).bUnion $ assume a ha, (countable_singleton _).insert _, le_antisymm (le_generate_from $ by simp [or_imp_distrib, is_open_lt', is_open_gt'] {contextual := tt}) (le_generate_from $ λ s h, begin rcases h with ⟨a, hs | hs⟩; [ rw show s = ⋃q∈{q:ℚ | 0 ≤ q ∧ a < real.to_nnreal q}, {b | ↑(real.to_nnreal q) < b}, from set.ext (assume b, by simp [hs, @ennreal.lt_iff_exists_rat_btwn a b, and_assoc]), rw show s = ⋃q∈{q:ℚ | 0 ≤ q ∧ ↑(real.to_nnreal q) < a}, {b | b < ↑(real.to_nnreal q)}, from set.ext (assume b, by simp [hs, @ennreal.lt_iff_exists_rat_btwn b a, and_comm, and_assoc])]; { apply is_open_Union, intro q, apply is_open_Union, intro hq, exact generate_open.basic _ (mem_bUnion hq.1 $ by simp) } end)⟩⟩ lemma embedding_coe : embedding (coe : ℝ≥0 → ℝ≥0∞) := ⟨⟨begin refine le_antisymm _ _, { rw [@order_topology.topology_eq_generate_intervals ℝ≥0∞ _, ← coinduced_le_iff_le_induced], refine le_generate_from (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, show is_open {b : ℝ≥0 | a < ↑b}, { cases a; simp [none_eq_top, some_eq_coe, is_open_lt'] }, show is_open {b : ℝ≥0 | ↑b < a}, { cases a; simp [none_eq_top, some_eq_coe, is_open_gt', is_open_const] } }, { rw [@order_topology.topology_eq_generate_intervals ℝ≥0 _], refine le_generate_from (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, exact ⟨Ioi a, is_open_Ioi, by simp [Ioi]⟩, exact ⟨Iio a, is_open_Iio, by simp [Iio]⟩ } end⟩, assume a b, coe_eq_coe.1⟩ lemma is_open_ne_top : is_open {a : ℝ≥0∞ | a ≠ ⊤} := is_open_ne lemma is_open_Ico_zero : is_open (Ico 0 b) := by { rw ennreal.Ico_eq_Iio, exact is_open_Iio} lemma open_embedding_coe : open_embedding (coe : ℝ≥0 → ℝ≥0∞) := ⟨embedding_coe, by { convert is_open_ne_top, ext (x|_); simp [none_eq_top, some_eq_coe] }⟩ lemma coe_range_mem_nhds : range (coe : ℝ≥0 → ℝ≥0∞) ∈ 𝓝 (r : ℝ≥0∞) := is_open.mem_nhds open_embedding_coe.open_range $ mem_range_self _ @[norm_cast] lemma tendsto_coe {f : filter α} {m : α → ℝ≥0} {a : ℝ≥0} : tendsto (λa, (m a : ℝ≥0∞)) f (𝓝 ↑a) ↔ tendsto m f (𝓝 a) := embedding_coe.tendsto_nhds_iff.symm lemma continuous_coe : continuous (coe : ℝ≥0 → ℝ≥0∞) := embedding_coe.continuous lemma continuous_coe_iff {α} [topological_space α] {f : α → ℝ≥0} : continuous (λa, (f a : ℝ≥0∞)) ↔ continuous f := embedding_coe.continuous_iff.symm lemma nhds_coe {r : ℝ≥0} : 𝓝 (r : ℝ≥0∞) = (𝓝 r).map coe := (open_embedding_coe.map_nhds_eq r).symm lemma tendsto_nhds_coe_iff {α : Type*} {l : filter α} {x : ℝ≥0} {f : ℝ≥0∞ → α} : tendsto f (𝓝 ↑x) l ↔ tendsto (f ∘ coe : ℝ≥0 → α) (𝓝 x) l := show _ ≤ _ ↔ _ ≤ _, by rw [nhds_coe, filter.map_map] lemma continuous_at_coe_iff {α : Type*} [topological_space α] {x : ℝ≥0} {f : ℝ≥0∞ → α} : continuous_at f (↑x) ↔ continuous_at (f ∘ coe : ℝ≥0 → α) x := tendsto_nhds_coe_iff lemma nhds_coe_coe {r p : ℝ≥0} : 𝓝 ((r : ℝ≥0∞), (p : ℝ≥0∞)) = (𝓝 (r, p)).map (λp:ℝ≥0×ℝ≥0, (p.1, p.2)) := ((open_embedding_coe.prod open_embedding_coe).map_nhds_eq (r, p)).symm lemma continuous_of_real : continuous ennreal.of_real := (continuous_coe_iff.2 continuous_id).comp nnreal.continuous_of_real lemma tendsto_of_real {f : filter α} {m : α → ℝ} {a : ℝ} (h : tendsto m f (𝓝 a)) : tendsto (λa, ennreal.of_real (m a)) f (𝓝 (ennreal.of_real a)) := tendsto.comp (continuous.tendsto continuous_of_real _) h lemma tendsto_to_nnreal {a : ℝ≥0∞} (ha : a ≠ ⊤) : tendsto ennreal.to_nnreal (𝓝 a) (𝓝 a.to_nnreal) := begin lift a to ℝ≥0 using ha, rw [nhds_coe, tendsto_map'_iff], exact tendsto_id end lemma continuous_on_to_nnreal : continuous_on ennreal.to_nnreal {a | a ≠ ∞} := λ a ha, continuous_at.continuous_within_at (tendsto_to_nnreal ha) lemma tendsto_to_real {a : ℝ≥0∞} (ha : a ≠ ⊤) : tendsto ennreal.to_real (𝓝 a) (𝓝 a.to_real) := nnreal.tendsto_coe.2 $ tendsto_to_nnreal ha /-- The set of finite `ℝ≥0∞` numbers is homeomorphic to `ℝ≥0`. -/ def ne_top_homeomorph_nnreal : {a | a ≠ ∞} ≃ₜ ℝ≥0 := { continuous_to_fun := continuous_on_iff_continuous_restrict.1 continuous_on_to_nnreal, continuous_inv_fun := continuous_subtype_mk _ continuous_coe, .. ne_top_equiv_nnreal } /-- The set of finite `ℝ≥0∞` numbers is homeomorphic to `ℝ≥0`. -/ def lt_top_homeomorph_nnreal : {a | a < ∞} ≃ₜ ℝ≥0 := by refine (homeomorph.set_congr $ set.ext $ λ x, _).trans ne_top_homeomorph_nnreal; simp only [mem_set_of_eq, lt_top_iff_ne_top] lemma nhds_top : 𝓝 ∞ = ⨅ a ≠ ∞, 𝓟 (Ioi a) := nhds_top_order.trans $ by simp [lt_top_iff_ne_top, Ioi] lemma nhds_top' : 𝓝 ∞ = ⨅ r : ℝ≥0, 𝓟 (Ioi r) := nhds_top.trans $ infi_ne_top _ lemma nhds_top_basis : (𝓝 ∞).has_basis (λ a, a < ∞) (λ a, Ioi a) := nhds_top_basis lemma tendsto_nhds_top_iff_nnreal {m : α → ℝ≥0∞} {f : filter α} : tendsto m f (𝓝 ⊤) ↔ ∀ x : ℝ≥0, ∀ᶠ a in f, ↑x < m a := by simp only [nhds_top', tendsto_infi, tendsto_principal, mem_Ioi] lemma tendsto_nhds_top_iff_nat {m : α → ℝ≥0∞} {f : filter α} : tendsto m f (𝓝 ⊤) ↔ ∀ n : ℕ, ∀ᶠ a in f, ↑n < m a := tendsto_nhds_top_iff_nnreal.trans ⟨λ h n, by simpa only [ennreal.coe_nat] using h n, λ h x, let ⟨n, hn⟩ := exists_nat_gt x in (h n).mono (λ y, lt_trans $ by rwa [← ennreal.coe_nat, coe_lt_coe])⟩ lemma tendsto_nhds_top {m : α → ℝ≥0∞} {f : filter α} (h : ∀ n : ℕ, ∀ᶠ a in f, ↑n < m a) : tendsto m f (𝓝 ⊤) := tendsto_nhds_top_iff_nat.2 h lemma tendsto_nat_nhds_top : tendsto (λ n : ℕ, ↑n) at_top (𝓝 ∞) := tendsto_nhds_top $ λ n, mem_at_top_sets.2 ⟨n+1, λ m hm, ennreal.coe_nat_lt_coe_nat.2 $ nat.lt_of_succ_le hm⟩ @[simp, norm_cast] lemma tendsto_coe_nhds_top {f : α → ℝ≥0} {l : filter α} : tendsto (λ x, (f x : ℝ≥0∞)) l (𝓝 ∞) ↔ tendsto f l at_top := by rw [tendsto_nhds_top_iff_nnreal, at_top_basis_Ioi.tendsto_right_iff]; [simp, apply_instance, apply_instance] lemma nhds_zero : 𝓝 (0 : ℝ≥0∞) = ⨅a ≠ 0, 𝓟 (Iio a) := nhds_bot_order.trans $ by simp [bot_lt_iff_ne_bot, Iio] lemma nhds_zero_basis : (𝓝 (0 : ℝ≥0∞)).has_basis (λ a : ℝ≥0∞, 0 < a) (λ a, Iio a) := nhds_bot_basis lemma nhds_zero_basis_Iic : (𝓝 (0 : ℝ≥0∞)).has_basis (λ a : ℝ≥0∞, 0 < a) Iic := nhds_bot_basis_Iic @[instance] lemma nhds_within_Ioi_coe_ne_bot {r : ℝ≥0} : (𝓝[Ioi r] (r : ℝ≥0∞)).ne_bot := nhds_within_Ioi_self_ne_bot' ennreal.coe_lt_top @[instance] lemma nhds_within_Ioi_zero_ne_bot : (𝓝[Ioi 0] (0 : ℝ≥0∞)).ne_bot := nhds_within_Ioi_coe_ne_bot -- using Icc because -- • don't have 'Ioo (x - ε) (x + ε) ∈ 𝓝 x' unless x > 0 -- • (x - y ≤ ε ↔ x ≤ ε + y) is true, while (x - y < ε ↔ x < ε + y) is not lemma Icc_mem_nhds : x ≠ ⊤ → 0 < ε → Icc (x - ε) (x + ε) ∈ 𝓝 x := begin assume xt ε0, rw _root_.mem_nhds_iff, by_cases x0 : x = 0, { use Iio (x + ε), have : Iio (x + ε) ⊆ Icc (x - ε) (x + ε), assume a, rw x0, simpa using le_of_lt, use this, exact ⟨is_open_Iio, mem_Iio_self_add xt ε0⟩ }, { use Ioo (x - ε) (x + ε), use Ioo_subset_Icc_self, exact ⟨is_open_Ioo, mem_Ioo_self_sub_add xt x0 ε0 ε0 ⟩ } end lemma nhds_of_ne_top : x ≠ ⊤ → 𝓝 x = ⨅ε > 0, 𝓟 (Icc (x - ε) (x + ε)) := begin assume xt, refine le_antisymm _ _, -- first direction simp only [le_infi_iff, le_principal_iff], assume ε ε0, exact Icc_mem_nhds xt ε0, -- second direction rw nhds_generate_from, refine le_infi (assume s, le_infi $ assume hs, _), simp only [mem_set_of_eq] at hs, rcases hs with ⟨xs, ⟨a, ha⟩⟩, cases ha, { rw ha at *, rcases exists_between xs with ⟨b, ⟨ab, bx⟩⟩, have xb_pos : x - b > 0 := zero_lt_sub_iff_lt.2 bx, have xxb : x - (x - b) = b := sub_sub_cancel (by rwa lt_top_iff_ne_top) (le_of_lt bx), refine infi_le_of_le (x - b) (infi_le_of_le xb_pos _), simp only [mem_principal, le_principal_iff], assume y, rintros ⟨h₁, h₂⟩, rw xxb at h₁, calc a < b : ab ... ≤ y : h₁ }, { rw ha at *, rcases exists_between xs with ⟨b, ⟨xb, ba⟩⟩, have bx_pos : b - x > 0 := zero_lt_sub_iff_lt.2 xb, have xbx : x + (b - x) = b := add_sub_cancel_of_le (le_of_lt xb), refine infi_le_of_le (b - x) (infi_le_of_le bx_pos _), simp only [mem_principal, le_principal_iff], assume y, rintros ⟨h₁, h₂⟩, rw xbx at h₂, calc y ≤ b : h₂ ... < a : ba }, end /-- Characterization of neighborhoods for `ℝ≥0∞` numbers. See also `tendsto_order` for a version with strict inequalities. -/ protected theorem tendsto_nhds {f : filter α} {u : α → ℝ≥0∞} {a : ℝ≥0∞} (ha : a ≠ ⊤) : tendsto u f (𝓝 a) ↔ ∀ ε > 0, ∀ᶠ x in f, (u x) ∈ Icc (a - ε) (a + ε) := by simp only [nhds_of_ne_top ha, tendsto_infi, tendsto_principal, mem_Icc] protected lemma tendsto_at_top [nonempty β] [semilattice_sup β] {f : β → ℝ≥0∞} {a : ℝ≥0∞} (ha : a ≠ ⊤) : tendsto f at_top (𝓝 a) ↔ ∀ε>0, ∃N, ∀n≥N, (f n) ∈ Icc (a - ε) (a + ε) := by simp only [ennreal.tendsto_nhds ha, mem_at_top_sets, mem_set_of_eq, filter.eventually] instance : has_continuous_add ℝ≥0∞ := begin refine ⟨continuous_iff_continuous_at.2 _⟩, rintro ⟨(_|a), b⟩, { exact tendsto_nhds_top_mono' continuous_at_fst (λ p, le_add_right le_rfl) }, rcases b with (_|b), { exact tendsto_nhds_top_mono' continuous_at_snd (λ p, le_add_left le_rfl) }, simp only [continuous_at, some_eq_coe, nhds_coe_coe, ← coe_add, tendsto_map'_iff, (∘), tendsto_coe, tendsto_add] end protected lemma tendsto_at_top_zero [hβ : nonempty β] [semilattice_sup β] {f : β → ℝ≥0∞} : filter.at_top.tendsto f (𝓝 0) ↔ ∀ ε > 0, ∃ N, ∀ n ≥ N, f n ≤ ε := begin rw ennreal.tendsto_at_top zero_ne_top, { simp_rw [set.mem_Icc, zero_add, zero_sub, zero_le _, true_and], }, { exact hβ, }, end protected lemma tendsto_mul (ha : a ≠ 0 ∨ b ≠ ⊤) (hb : b ≠ 0 ∨ a ≠ ⊤) : tendsto (λp:ℝ≥0∞×ℝ≥0∞, p.1 * p.2) (𝓝 (a, b)) (𝓝 (a * b)) := have ht : ∀b:ℝ≥0∞, b ≠ 0 → tendsto (λp:ℝ≥0∞×ℝ≥0∞, p.1 * p.2) (𝓝 ((⊤:ℝ≥0∞), b)) (𝓝 ⊤), begin refine assume b hb, tendsto_nhds_top_iff_nnreal.2 $ assume n, _, rcases lt_iff_exists_nnreal_btwn.1 (pos_iff_ne_zero.2 hb) with ⟨ε, hε, hεb⟩, replace hε : 0 < ε, from coe_pos.1 hε, filter_upwards [prod_is_open.mem_nhds (lt_mem_nhds $ @coe_lt_top (n / ε)) (lt_mem_nhds hεb)], rintros ⟨a₁, a₂⟩ ⟨h₁, h₂⟩, dsimp at h₁ h₂ ⊢, rw [← div_mul_cancel n hε.ne', coe_mul], exact mul_lt_mul h₁ h₂ end, begin cases a, {simp [none_eq_top] at hb, simp [none_eq_top, ht b hb, top_mul, hb] }, cases b, { simp [none_eq_top] at ha, simp [*, nhds_swap (a : ℝ≥0∞) ⊤, none_eq_top, some_eq_coe, top_mul, tendsto_map'_iff, (∘), mul_comm] }, simp [some_eq_coe, nhds_coe_coe, tendsto_map'_iff, (∘)], simp only [coe_mul.symm, tendsto_coe, tendsto_mul] end protected lemma tendsto.mul {f : filter α} {ma : α → ℝ≥0∞} {mb : α → ℝ≥0∞} {a b : ℝ≥0∞} (hma : tendsto ma f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ ⊤) (hmb : tendsto mb f (𝓝 b)) (hb : b ≠ 0 ∨ a ≠ ⊤) : tendsto (λa, ma a * mb a) f (𝓝 (a * b)) := show tendsto ((λp:ℝ≥0∞×ℝ≥0∞, p.1 * p.2) ∘ (λa, (ma a, mb a))) f (𝓝 (a * b)), from tendsto.comp (ennreal.tendsto_mul ha hb) (hma.prod_mk_nhds hmb) protected lemma tendsto.const_mul {f : filter α} {m : α → ℝ≥0∞} {a b : ℝ≥0∞} (hm : tendsto m f (𝓝 b)) (hb : b ≠ 0 ∨ a ≠ ⊤) : tendsto (λb, a * m b) f (𝓝 (a * b)) := by_cases (assume : a = 0, by simp [this, tendsto_const_nhds]) (assume ha : a ≠ 0, ennreal.tendsto.mul tendsto_const_nhds (or.inl ha) hm hb) protected lemma tendsto.mul_const {f : filter α} {m : α → ℝ≥0∞} {a b : ℝ≥0∞} (hm : tendsto m f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ ⊤) : tendsto (λx, m x * b) f (𝓝 (a * b)) := by simpa only [mul_comm] using ennreal.tendsto.const_mul hm ha lemma tendsto_finset_prod_of_ne_top {ι : Type*} {f : ι → α → ℝ≥0∞} {x : filter α} {a : ι → ℝ≥0∞} (s : finset ι) (h : ∀ i ∈ s, tendsto (f i) x (𝓝 (a i))) (h' : ∀ i ∈ s, a i ≠ ∞): tendsto (λ b, ∏ c in s, f c b) x (𝓝 (∏ c in s, a c)) := begin induction s using finset.induction with a s has IH, { simp [tendsto_const_nhds] }, simp only [finset.prod_insert has], apply tendsto.mul (h _ (finset.mem_insert_self _ _)), { right, exact (prod_lt_top (λ i hi, lt_top_iff_ne_top.2 (h' _ (finset.mem_insert_of_mem hi)))).ne }, { exact IH (λ i hi, h _ (finset.mem_insert_of_mem hi)) (λ i hi, h' _ (finset.mem_insert_of_mem hi)) }, { exact or.inr (h' _ (finset.mem_insert_self _ _)) } end protected lemma continuous_at_const_mul {a b : ℝ≥0∞} (h : a ≠ ⊤ ∨ b ≠ 0) : continuous_at ((*) a) b := tendsto.const_mul tendsto_id h.symm protected lemma continuous_at_mul_const {a b : ℝ≥0∞} (h : a ≠ ⊤ ∨ b ≠ 0) : continuous_at (λ x, x * a) b := tendsto.mul_const tendsto_id h.symm protected lemma continuous_const_mul {a : ℝ≥0∞} (ha : a ≠ ⊤) : continuous ((*) a) := continuous_iff_continuous_at.2 $ λ x, ennreal.continuous_at_const_mul (or.inl ha) protected lemma continuous_mul_const {a : ℝ≥0∞} (ha : a ≠ ⊤) : continuous (λ x, x * a) := continuous_iff_continuous_at.2 $ λ x, ennreal.continuous_at_mul_const (or.inl ha) lemma le_of_forall_lt_one_mul_le {x y : ℝ≥0∞} (h : ∀ a < 1, a * x ≤ y) : x ≤ y := begin have : tendsto (* x) (𝓝[Iio 1] 1) (𝓝 (1 * x)) := (ennreal.continuous_at_mul_const (or.inr one_ne_zero)).mono_left inf_le_left, rw one_mul at this, haveI : (𝓝[Iio 1] (1 : ℝ≥0∞)).ne_bot := nhds_within_Iio_self_ne_bot' ennreal.zero_lt_one, exact le_of_tendsto this (eventually_nhds_within_iff.2 $ eventually_of_forall h) end lemma infi_mul_left' {ι} {f : ι → ℝ≥0∞} {a : ℝ≥0∞} (h : a = ⊤ → (⨅ i, f i) = 0 → ∃ i, f i = 0) (h0 : a = 0 → nonempty ι) : (⨅ i, a * f i) = a * ⨅ i, f i := begin by_cases H : a = ⊤ ∧ (⨅ i, f i) = 0, { rcases h H.1 H.2 with ⟨i, hi⟩, rw [H.2, mul_zero, ← bot_eq_zero, infi_eq_bot], exact λ b hb, ⟨i, by rwa [hi, mul_zero, ← bot_eq_zero]⟩ }, { rw not_and_distrib at H, by_cases hι : nonempty ι, { resetI, exact (map_infi_of_continuous_at_of_monotone' (ennreal.continuous_at_const_mul H) ennreal.mul_left_mono).symm }, { rw [infi_of_empty hι, infi_of_empty hι, mul_top, if_neg], exact mt h0 hι } } end lemma infi_mul_left {ι} [nonempty ι] {f : ι → ℝ≥0∞} {a : ℝ≥0∞} (h : a = ⊤ → (⨅ i, f i) = 0 → ∃ i, f i = 0) : (⨅ i, a * f i) = a * ⨅ i, f i := infi_mul_left' h (λ _, ‹nonempty ι›) lemma infi_mul_right' {ι} {f : ι → ℝ≥0∞} {a : ℝ≥0∞} (h : a = ⊤ → (⨅ i, f i) = 0 → ∃ i, f i = 0) (h0 : a = 0 → nonempty ι) : (⨅ i, f i * a) = (⨅ i, f i) * a := by simpa only [mul_comm a] using infi_mul_left' h h0 lemma infi_mul_right {ι} [nonempty ι] {f : ι → ℝ≥0∞} {a : ℝ≥0∞} (h : a = ⊤ → (⨅ i, f i) = 0 → ∃ i, f i = 0) : (⨅ i, f i * a) = (⨅ i, f i) * a := infi_mul_right' h (λ _, ‹nonempty ι›) protected lemma continuous_inv : continuous (has_inv.inv : ℝ≥0∞ → ℝ≥0∞) := continuous_iff_continuous_at.2 $ λ a, tendsto_order.2 ⟨begin assume b hb, simp only [@ennreal.lt_inv_iff_lt_inv b], exact gt_mem_nhds (ennreal.lt_inv_iff_lt_inv.1 hb), end, begin assume b hb, simp only [gt_iff_lt, @ennreal.inv_lt_iff_inv_lt _ b], exact lt_mem_nhds (ennreal.inv_lt_iff_inv_lt.1 hb) end⟩ @[simp] protected lemma tendsto_inv_iff {f : filter α} {m : α → ℝ≥0∞} {a : ℝ≥0∞} : tendsto (λ x, (m x)⁻¹) f (𝓝 a⁻¹) ↔ tendsto m f (𝓝 a) := ⟨λ h, by simpa only [function.comp, ennreal.inv_inv] using (ennreal.continuous_inv.tendsto a⁻¹).comp h, (ennreal.continuous_inv.tendsto a).comp⟩ protected lemma tendsto.div {f : filter α} {ma : α → ℝ≥0∞} {mb : α → ℝ≥0∞} {a b : ℝ≥0∞} (hma : tendsto ma f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ 0) (hmb : tendsto mb f (𝓝 b)) (hb : b ≠ ⊤ ∨ a ≠ ⊤) : tendsto (λa, ma a / mb a) f (𝓝 (a / b)) := by { apply tendsto.mul hma _ (ennreal.tendsto_inv_iff.2 hmb) _; simp [ha, hb] } protected lemma tendsto.const_div {f : filter α} {m : α → ℝ≥0∞} {a b : ℝ≥0∞} (hm : tendsto m f (𝓝 b)) (hb : b ≠ ⊤ ∨ a ≠ ⊤) : tendsto (λb, a / m b) f (𝓝 (a / b)) := by { apply tendsto.const_mul (ennreal.tendsto_inv_iff.2 hm), simp [hb] } protected lemma tendsto.div_const {f : filter α} {m : α → ℝ≥0∞} {a b : ℝ≥0∞} (hm : tendsto m f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ 0) : tendsto (λx, m x / b) f (𝓝 (a / b)) := by { apply tendsto.mul_const hm, simp [ha] } protected lemma tendsto_inv_nat_nhds_zero : tendsto (λ n : ℕ, (n : ℝ≥0∞)⁻¹) at_top (𝓝 0) := ennreal.inv_top ▸ ennreal.tendsto_inv_iff.2 tendsto_nat_nhds_top lemma bsupr_add {ι} {s : set ι} (hs : s.nonempty) {f : ι → ℝ≥0∞} : (⨆ i ∈ s, f i) + a = ⨆ i ∈ s, f i + a := begin simp only [← Sup_image], symmetry, rw [image_comp (+ a)], refine is_lub.Sup_eq ((is_lub_Sup $ f '' s).is_lub_of_tendsto _ (hs.image _) _), exacts [λ x _ y _ hxy, add_le_add hxy le_rfl, tendsto.add (tendsto_id' inf_le_left) tendsto_const_nhds] end lemma Sup_add {s : set ℝ≥0∞} (hs : s.nonempty) : Sup s + a = ⨆b∈s, b + a := by rw [Sup_eq_supr, bsupr_add hs] lemma supr_add {ι : Sort*} {s : ι → ℝ≥0∞} [h : nonempty ι] : supr s + a = ⨆b, s b + a := let ⟨x⟩ := h in calc supr s + a = Sup (range s) + a : by rw Sup_range ... = (⨆b∈range s, b + a) : Sup_add ⟨s x, x, rfl⟩ ... = _ : supr_range lemma add_supr {ι : Sort*} {s : ι → ℝ≥0∞} [h : nonempty ι] : a + supr s = ⨆b, a + s b := by rw [add_comm, supr_add]; simp [add_comm] lemma supr_add_supr {ι : Sort*} {f g : ι → ℝ≥0∞} (h : ∀i j, ∃k, f i + g j ≤ f k + g k) : supr f + supr g = (⨆ a, f a + g a) := begin by_cases hι : nonempty ι, { letI := hι, refine le_antisymm _ (supr_le $ λ a, add_le_add (le_supr _ _) (le_supr _ _)), simpa [add_supr, supr_add] using λ i j:ι, show f i + g j ≤ ⨆ a, f a + g a, from let ⟨k, hk⟩ := h i j in le_supr_of_le k hk }, { have : ∀f:ι → ℝ≥0∞, (⨆i, f i) = 0 := λ f, supr_eq_zero.mpr (λ i, (hι ⟨i⟩).elim), rw [this, this, this, zero_add] } end lemma supr_add_supr_of_monotone {ι : Sort*} [semilattice_sup ι] {f g : ι → ℝ≥0∞} (hf : monotone f) (hg : monotone g) : supr f + supr g = (⨆ a, f a + g a) := supr_add_supr $ assume i j, ⟨i ⊔ j, add_le_add (hf $ le_sup_left) (hg $ le_sup_right)⟩ lemma finset_sum_supr_nat {α} {ι} [semilattice_sup ι] {s : finset α} {f : α → ι → ℝ≥0∞} (hf : ∀a, monotone (f a)) : ∑ a in s, supr (f a) = (⨆ n, ∑ a in s, f a n) := begin refine finset.induction_on s _ _, { simp, }, { assume a s has ih, simp only [finset.sum_insert has], rw [ih, supr_add_supr_of_monotone (hf a)], assume i j h, exact (finset.sum_le_sum $ assume a ha, hf a h) } end lemma mul_Sup {s : set ℝ≥0∞} {a : ℝ≥0∞} : a * Sup s = ⨆i∈s, a * i := begin by_cases hs : ∀x∈s, x = (0:ℝ≥0∞), { have h₁ : Sup s = 0 := (bot_unique $ Sup_le $ assume a ha, (hs a ha).symm ▸ le_refl 0), have h₂ : (⨆i ∈ s, a * i) = 0 := (bot_unique $ supr_le $ assume a, supr_le $ assume ha, by simp [hs a ha]), rw [h₁, h₂, mul_zero] }, { simp only [not_forall] at hs, rcases hs with ⟨x, hx, hx0⟩, have s₁ : Sup s ≠ 0 := pos_iff_ne_zero.1 (lt_of_lt_of_le (pos_iff_ne_zero.2 hx0) (le_Sup hx)), have : Sup ((λb, a * b) '' s) = a * Sup s := is_lub.Sup_eq ((is_lub_Sup s).is_lub_of_tendsto (assume x _ y _ h, mul_le_mul_left' h _) ⟨x, hx⟩ (ennreal.tendsto.const_mul (tendsto_id' inf_le_left) (or.inl s₁))), rw [this.symm, Sup_image] } end lemma mul_supr {ι : Sort*} {f : ι → ℝ≥0∞} {a : ℝ≥0∞} : a * supr f = ⨆i, a * f i := by rw [← Sup_range, mul_Sup, supr_range] lemma supr_mul {ι : Sort*} {f : ι → ℝ≥0∞} {a : ℝ≥0∞} : supr f * a = ⨆i, f i * a := by rw [mul_comm, mul_supr]; congr; funext; rw [mul_comm] lemma supr_div {ι : Sort*} {f : ι → ℝ≥0∞} {a : ℝ≥0∞} : supr f / a = ⨆i, f i / a := supr_mul protected lemma tendsto_coe_sub : ∀{b:ℝ≥0∞}, tendsto (λb:ℝ≥0∞, ↑r - b) (𝓝 b) (𝓝 (↑r - b)) := begin refine (forall_ennreal.2 $ and.intro (assume a, _) _), { simp [@nhds_coe a, tendsto_map'_iff, (∘), tendsto_coe, coe_sub.symm], exact tendsto_const_nhds.sub tendsto_id }, simp, exact (tendsto.congr' (mem_of_superset (lt_mem_nhds $ @coe_lt_top r) $ by simp [le_of_lt] {contextual := tt})) tendsto_const_nhds end lemma sub_supr {ι : Sort*} [nonempty ι] {b : ι → ℝ≥0∞} (hr : a < ⊤) : a - (⨆i, b i) = (⨅i, a - b i) := let ⟨r, eq, _⟩ := lt_iff_exists_coe.mp hr in have Inf ((λb, ↑r - b) '' range b) = ↑r - (⨆i, b i), from is_glb.Inf_eq $ is_lub_supr.is_glb_of_tendsto (assume x _ y _, sub_le_sub (le_refl _)) (range_nonempty _) (ennreal.tendsto_coe_sub.comp (tendsto_id' inf_le_left)), by rw [eq, ←this]; simp [Inf_image, infi_range, -mem_range]; exact le_refl _ end topological_space section tsum variables {f g : α → ℝ≥0∞} @[norm_cast] protected lemma has_sum_coe {f : α → ℝ≥0} {r : ℝ≥0} : has_sum (λa, (f a : ℝ≥0∞)) ↑r ↔ has_sum f r := have (λs:finset α, ∑ a in s, ↑(f a)) = (coe : ℝ≥0 → ℝ≥0∞) ∘ (λs:finset α, ∑ a in s, f a), from funext $ assume s, ennreal.coe_finset_sum.symm, by unfold has_sum; rw [this, tendsto_coe] protected lemma tsum_coe_eq {f : α → ℝ≥0} (h : has_sum f r) : ∑'a, (f a : ℝ≥0∞) = r := (ennreal.has_sum_coe.2 h).tsum_eq protected lemma coe_tsum {f : α → ℝ≥0} : summable f → ↑(tsum f) = ∑'a, (f a : ℝ≥0∞) | ⟨r, hr⟩ := by rw [hr.tsum_eq, ennreal.tsum_coe_eq hr] protected lemma has_sum : has_sum f (⨆s:finset α, ∑ a in s, f a) := tendsto_at_top_supr $ λ s t, finset.sum_le_sum_of_subset @[simp] protected lemma summable : summable f := ⟨_, ennreal.has_sum⟩ lemma tsum_coe_ne_top_iff_summable {f : β → ℝ≥0} : ∑' b, (f b:ℝ≥0∞) ≠ ∞ ↔ summable f := begin refine ⟨λ h, _, λ h, ennreal.coe_tsum h ▸ ennreal.coe_ne_top⟩, lift (∑' b, (f b:ℝ≥0∞)) to ℝ≥0 using h with a ha, refine ⟨a, ennreal.has_sum_coe.1 _⟩, rw ha, exact ennreal.summable.has_sum end protected lemma tsum_eq_supr_sum : ∑'a, f a = (⨆s:finset α, ∑ a in s, f a) := ennreal.has_sum.tsum_eq protected lemma tsum_eq_supr_sum' {ι : Type*} (s : ι → finset α) (hs : ∀ t, ∃ i, t ⊆ s i) : ∑' a, f a = ⨆ i, ∑ a in s i, f a := begin rw [ennreal.tsum_eq_supr_sum], symmetry, change (⨆i:ι, (λ t : finset α, ∑ a in t, f a) (s i)) = ⨆s:finset α, ∑ a in s, f a, exact (finset.sum_mono_set f).supr_comp_eq hs end protected lemma tsum_sigma {β : α → Type*} (f : Πa, β a → ℝ≥0∞) : ∑'p:Σa, β a, f p.1 p.2 = ∑'a b, f a b := tsum_sigma' (assume b, ennreal.summable) ennreal.summable protected lemma tsum_sigma' {β : α → Type*} (f : (Σ a, β a) → ℝ≥0∞) : ∑'p:(Σa, β a), f p = ∑'a b, f ⟨a, b⟩ := tsum_sigma' (assume b, ennreal.summable) ennreal.summable protected lemma tsum_prod {f : α → β → ℝ≥0∞} : ∑'p:α×β, f p.1 p.2 = ∑'a, ∑'b, f a b := tsum_prod' ennreal.summable $ λ _, ennreal.summable protected lemma tsum_comm {f : α → β → ℝ≥0∞} : ∑'a, ∑'b, f a b = ∑'b, ∑'a, f a b := tsum_comm' ennreal.summable (λ _, ennreal.summable) (λ _, ennreal.summable) protected lemma tsum_add : ∑'a, (f a + g a) = (∑'a, f a) + (∑'a, g a) := tsum_add ennreal.summable ennreal.summable protected lemma tsum_le_tsum (h : ∀a, f a ≤ g a) : ∑'a, f a ≤ ∑'a, g a := tsum_le_tsum h ennreal.summable ennreal.summable protected lemma sum_le_tsum {f : α → ℝ≥0∞} (s : finset α) : ∑ x in s, f x ≤ ∑' x, f x := sum_le_tsum s (λ x hx, zero_le _) ennreal.summable protected lemma tsum_eq_supr_nat' {f : ℕ → ℝ≥0∞} {N : ℕ → ℕ} (hN : tendsto N at_top at_top) : ∑'i:ℕ, f i = (⨆i:ℕ, ∑ a in finset.range (N i), f a) := ennreal.tsum_eq_supr_sum' _ $ λ t, let ⟨n, hn⟩ := t.exists_nat_subset_range, ⟨k, _, hk⟩ := exists_le_of_tendsto_at_top hN 0 n in ⟨k, finset.subset.trans hn (finset.range_mono hk)⟩ protected lemma tsum_eq_supr_nat {f : ℕ → ℝ≥0∞} : ∑'i:ℕ, f i = (⨆i:ℕ, ∑ a in finset.range i, f a) := ennreal.tsum_eq_supr_sum' _ finset.exists_nat_subset_range protected lemma tsum_eq_liminf_sum_nat {f : ℕ → ℝ≥0∞} : ∑' i, f i = filter.at_top.liminf (λ n, ∑ i in finset.range n, f i) := begin rw [ennreal.tsum_eq_supr_nat, filter.liminf_eq_supr_infi_of_nat], congr, refine funext (λ n, le_antisymm _ _), { refine le_binfi (λ i hi, finset.sum_le_sum_of_subset_of_nonneg _ (λ _ _ _, zero_le _)), simpa only [finset.range_subset, add_le_add_iff_right] using hi, }, { refine le_trans (infi_le _ n) _, simp [le_refl n, le_refl ((finset.range n).sum f)], }, end protected lemma le_tsum (a : α) : f a ≤ ∑'a, f a := le_tsum' ennreal.summable a protected lemma tsum_eq_top_of_eq_top : (∃ a, f a = ∞) → ∑' a, f a = ∞ | ⟨a, ha⟩ := top_unique $ ha ▸ ennreal.le_tsum a @[simp] protected lemma tsum_top [nonempty α] : ∑' a : α, ∞ = ∞ := let ⟨a⟩ := ‹nonempty α› in ennreal.tsum_eq_top_of_eq_top ⟨a, rfl⟩ protected lemma ne_top_of_tsum_ne_top (h : ∑' a, f a ≠ ∞) (a : α) : f a ≠ ∞ := λ ha, h $ ennreal.tsum_eq_top_of_eq_top ⟨a, ha⟩ protected lemma tsum_mul_left : ∑'i, a * f i = a * ∑'i, f i := if h : ∀i, f i = 0 then by simp [h] else let ⟨i, (hi : f i ≠ 0)⟩ := not_forall.mp h in have sum_ne_0 : ∑'i, f i ≠ 0, from ne_of_gt $ calc 0 < f i : lt_of_le_of_ne (zero_le _) hi.symm ... ≤ ∑'i, f i : ennreal.le_tsum _, have tendsto (λs:finset α, ∑ j in s, a * f j) at_top (𝓝 (a * ∑'i, f i)), by rw [← show (*) a ∘ (λs:finset α, ∑ j in s, f j) = λs, ∑ j in s, a * f j, from funext $ λ s, finset.mul_sum]; exact ennreal.tendsto.const_mul ennreal.summable.has_sum (or.inl sum_ne_0), has_sum.tsum_eq this protected lemma tsum_mul_right : (∑'i, f i * a) = (∑'i, f i) * a := by simp [mul_comm, ennreal.tsum_mul_left] @[simp] lemma tsum_supr_eq {α : Type*} (a : α) {f : α → ℝ≥0∞} : ∑'b:α, (⨆ (h : a = b), f b) = f a := le_antisymm (by rw [ennreal.tsum_eq_supr_sum]; exact supr_le (assume s, calc (∑ b in s, ⨆ (h : a = b), f b) ≤ ∑ b in {a}, ⨆ (h : a = b), f b : finset.sum_le_sum_of_ne_zero $ assume b _ hb, suffices a = b, by simpa using this.symm, classical.by_contradiction $ assume h, by simpa [h] using hb ... = f a : by simp)) (calc f a ≤ (⨆ (h : a = a), f a) : le_supr (λh:a=a, f a) rfl ... ≤ (∑'b:α, ⨆ (h : a = b), f b) : ennreal.le_tsum _) lemma has_sum_iff_tendsto_nat {f : ℕ → ℝ≥0∞} (r : ℝ≥0∞) : has_sum f r ↔ tendsto (λn:ℕ, ∑ i in finset.range n, f i) at_top (𝓝 r) := begin refine ⟨has_sum.tendsto_sum_nat, assume h, _⟩, rw [← supr_eq_of_tendsto _ h, ← ennreal.tsum_eq_supr_nat], { exact ennreal.summable.has_sum }, { exact assume s t hst, finset.sum_le_sum_of_subset (finset.range_subset.2 hst) } end lemma tendsto_nat_tsum (f : ℕ → ℝ≥0∞) : tendsto (λn:ℕ, ∑ i in finset.range n, f i) at_top (𝓝 (∑' n, f n)) := by { rw ← has_sum_iff_tendsto_nat, exact ennreal.summable.has_sum } lemma to_nnreal_apply_of_tsum_ne_top {α : Type*} {f : α → ℝ≥0∞} (hf : ∑' i, f i ≠ ∞) (x : α) : (((ennreal.to_nnreal ∘ f) x : ℝ≥0) : ℝ≥0∞) = f x := coe_to_nnreal $ ennreal.ne_top_of_tsum_ne_top hf _ lemma summable_to_nnreal_of_tsum_ne_top {α : Type*} {f : α → ℝ≥0∞} (hf : ∑' i, f i ≠ ∞) : summable (ennreal.to_nnreal ∘ f) := by simpa only [←tsum_coe_ne_top_iff_summable, to_nnreal_apply_of_tsum_ne_top hf] using hf lemma tendsto_cofinite_zero_of_tsum_lt_top {α} {f : α → ℝ≥0∞} (hf : ∑' x, f x < ∞) : tendsto f cofinite (𝓝 0) := begin have f_ne_top : ∀ n, f n ≠ ∞, from ennreal.ne_top_of_tsum_ne_top hf.ne, have h_f_coe : f = λ n, ((f n).to_nnreal : ennreal), from funext (λ n, (coe_to_nnreal (f_ne_top n)).symm), rw [h_f_coe, ←@coe_zero, tendsto_coe], exact nnreal.tendsto_cofinite_zero_of_summable (summable_to_nnreal_of_tsum_ne_top hf.ne), end lemma tendsto_at_top_zero_of_tsum_lt_top {f : ℕ → ℝ≥0∞} (hf : ∑' x, f x < ∞) : tendsto f at_top (𝓝 0) := by { rw ←nat.cofinite_eq_at_top, exact tendsto_cofinite_zero_of_tsum_lt_top hf } protected lemma tsum_apply {ι α : Type*} {f : ι → α → ℝ≥0∞} {x : α} : (∑' i, f i) x = ∑' i, f i x := tsum_apply $ pi.summable.mpr $ λ _, ennreal.summable lemma tsum_sub {f : ℕ → ℝ≥0∞} {g : ℕ → ℝ≥0∞} (h₁ : ∑' i, g i < ∞) (h₂ : g ≤ f) : ∑' i, (f i - g i) = (∑' i, f i) - (∑' i, g i) := begin have h₃: ∑' i, (f i - g i) = ∑' i, (f i - g i + g i) - ∑' i, g i, { rw [ennreal.tsum_add, add_sub_self h₁]}, have h₄:(λ i, (f i - g i) + (g i)) = f, { ext n, rw ennreal.sub_add_cancel_of_le (h₂ n)}, rw h₄ at h₃, apply h₃, end end tsum lemma tendsto_to_real_iff {ι} {fi : filter ι} {f : ι → ℝ≥0∞} (hf : ∀ i, f i ≠ ∞) {x : ℝ≥0∞} (hx : x ≠ ∞) : fi.tendsto (λ n, (f n).to_real) (𝓝 x.to_real) ↔ fi.tendsto f (𝓝 x) := begin refine ⟨λ h, _, λ h, tendsto.comp (ennreal.tendsto_to_real hx) h⟩, have h_eq : f = (λ n, ennreal.of_real (f n).to_real), by { ext1 n, rw ennreal.of_real_to_real (hf n), }, rw [h_eq, ← ennreal.of_real_to_real hx], exact ennreal.tendsto_of_real h, end lemma tsum_coe_ne_top_iff_summable_coe {f : α → ℝ≥0} : ∑' a, (f a : ℝ≥0∞) ≠ ∞ ↔ summable (λ a, (f a : ℝ)) := begin rw nnreal.summable_coe, exact tsum_coe_ne_top_iff_summable, end lemma tsum_coe_eq_top_iff_not_summable_coe {f : α → ℝ≥0} : ∑' a, (f a : ℝ≥0∞) = ∞ ↔ ¬ summable (λ a, (f a : ℝ)) := begin rw [← @not_not (∑' a, ↑(f a) = ⊤)], exact not_congr tsum_coe_ne_top_iff_summable_coe end lemma summable_to_real {f : α → ℝ≥0∞} (hsum : ∑' x, f x ≠ ∞) : summable (λ x, (f x).to_real) := begin lift f to α → ℝ≥0 using ennreal.ne_top_of_tsum_ne_top hsum, rwa ennreal.tsum_coe_ne_top_iff_summable_coe at hsum, end end ennreal namespace nnreal open_locale nnreal lemma tsum_eq_to_nnreal_tsum {f : β → ℝ≥0} : (∑' b, f b) = (∑' b, (f b : ℝ≥0∞)).to_nnreal := begin by_cases h : summable f, { rw [← ennreal.coe_tsum h, ennreal.to_nnreal_coe] }, { have A := tsum_eq_zero_of_not_summable h, simp only [← ennreal.tsum_coe_ne_top_iff_summable, not_not] at h, simp only [h, ennreal.top_to_nnreal, A] } end /-- Comparison test of convergence of `ℝ≥0`-valued series. -/ lemma exists_le_has_sum_of_le {f g : β → ℝ≥0} {r : ℝ≥0} (hgf : ∀b, g b ≤ f b) (hfr : has_sum f r) : ∃p≤r, has_sum g p := have ∑'b, (g b : ℝ≥0∞) ≤ r, begin refine has_sum_le (assume b, _) ennreal.summable.has_sum (ennreal.has_sum_coe.2 hfr), exact ennreal.coe_le_coe.2 (hgf _) end, let ⟨p, eq, hpr⟩ := ennreal.le_coe_iff.1 this in ⟨p, hpr, ennreal.has_sum_coe.1 $ eq ▸ ennreal.summable.has_sum⟩ /-- Comparison test of convergence of `ℝ≥0`-valued series. -/ lemma summable_of_le {f g : β → ℝ≥0} (hgf : ∀b, g b ≤ f b) : summable f → summable g | ⟨r, hfr⟩ := let ⟨p, _, hp⟩ := exists_le_has_sum_of_le hgf hfr in hp.summable /-- A series of non-negative real numbers converges to `r` in the sense of `has_sum` if and only if the sequence of partial sum converges to `r`. -/ lemma has_sum_iff_tendsto_nat {f : ℕ → ℝ≥0} {r : ℝ≥0} : has_sum f r ↔ tendsto (λn:ℕ, ∑ i in finset.range n, f i) at_top (𝓝 r) := begin rw [← ennreal.has_sum_coe, ennreal.has_sum_iff_tendsto_nat], simp only [ennreal.coe_finset_sum.symm], exact ennreal.tendsto_coe end lemma not_summable_iff_tendsto_nat_at_top {f : ℕ → ℝ≥0} : ¬ summable f ↔ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top at_top := begin split, { intros h, refine ((tendsto_of_monotone _).resolve_right h).comp _, exacts [finset.sum_mono_set _, tendsto_finset_range] }, { rintros hnat ⟨r, hr⟩, exact not_tendsto_nhds_of_tendsto_at_top hnat _ (has_sum_iff_tendsto_nat.1 hr) } end lemma summable_iff_not_tendsto_nat_at_top {f : ℕ → ℝ≥0} : summable f ↔ ¬ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top at_top := by rw [← not_iff_not, not_not, not_summable_iff_tendsto_nat_at_top] lemma summable_of_sum_range_le {f : ℕ → ℝ≥0} {c : ℝ≥0} (h : ∀ n, ∑ i in finset.range n, f i ≤ c) : summable f := begin apply summable_iff_not_tendsto_nat_at_top.2 (λ H, _), rcases exists_lt_of_tendsto_at_top H 0 c with ⟨n, -, hn⟩, exact lt_irrefl _ (hn.trans_le (h n)), end lemma tsum_le_of_sum_range_le {f : ℕ → ℝ≥0} {c : ℝ≥0} (h : ∀ n, ∑ i in finset.range n, f i ≤ c) : ∑' n, f n ≤ c := le_of_tendsto' (has_sum_iff_tendsto_nat.1 (summable_of_sum_range_le h).has_sum) h lemma tsum_comp_le_tsum_of_inj {β : Type*} {f : α → ℝ≥0} (hf : summable f) {i : β → α} (hi : function.injective i) : ∑' x, f (i x) ≤ ∑' x, f x := tsum_le_tsum_of_inj i hi (λ c hc, zero_le _) (λ b, le_refl _) (summable_comp_injective hf hi) hf lemma summable_sigma {β : Π x : α, Type*} {f : (Σ x, β x) → ℝ≥0} : summable f ↔ (∀ x, summable (λ y, f ⟨x, y⟩)) ∧ summable (λ x, ∑' y, f ⟨x, y⟩) := begin split, { simp only [← nnreal.summable_coe, nnreal.coe_tsum], exact λ h, ⟨h.sigma_factor, h.sigma⟩ }, { rintro ⟨h₁, h₂⟩, simpa only [← ennreal.tsum_coe_ne_top_iff_summable, ennreal.tsum_sigma', ennreal.coe_tsum, h₁] using h₂ } end lemma indicator_summable {f : α → ℝ≥0} (hf : summable f) (s : set α) : summable (s.indicator f) := begin refine nnreal.summable_of_le (λ a, le_trans (le_of_eq (s.indicator_apply f a)) _) hf, split_ifs, exact le_refl (f a), exact zero_le_coe, end lemma tsum_indicator_ne_zero {f : α → ℝ≥0} (hf : summable f) {s : set α} (h : ∃ a ∈ s, f a ≠ 0) : ∑' x, (s.indicator f) x ≠ 0 := λ h', let ⟨a, ha, hap⟩ := h in hap (trans (set.indicator_apply_eq_self.mpr (absurd ha)).symm (((tsum_eq_zero_iff (indicator_summable hf s)).1 h') a)) open finset /-- For `f : ℕ → ℝ≥0`, then `∑' k, f (k + i)` tends to zero. This does not require a summability assumption on `f`, as otherwise all sums are zero. -/ lemma tendsto_sum_nat_add (f : ℕ → ℝ≥0) : tendsto (λ i, ∑' k, f (k + i)) at_top (𝓝 0) := begin rw ← tendsto_coe, convert tendsto_sum_nat_add (λ i, (f i : ℝ)), norm_cast, end lemma has_sum_lt {f g : α → ℝ≥0} {sf sg : ℝ≥0} {i : α} (h : ∀ (a : α), f a ≤ g a) (hi : f i < g i) (hf : has_sum f sf) (hg : has_sum g sg) : sf < sg := begin have A : ∀ (a : α), (f a : ℝ) ≤ g a := λ a, nnreal.coe_le_coe.2 (h a), have : (sf : ℝ) < sg := has_sum_lt A (nnreal.coe_lt_coe.2 hi) (has_sum_coe.2 hf) (has_sum_coe.2 hg), exact nnreal.coe_lt_coe.1 this end @[mono] lemma has_sum_strict_mono {f g : α → ℝ≥0} {sf sg : ℝ≥0} (hf : has_sum f sf) (hg : has_sum g sg) (h : f < g) : sf < sg := let ⟨hle, i, hi⟩ := pi.lt_def.mp h in has_sum_lt hle hi hf hg lemma tsum_lt_tsum {f g : α → ℝ≥0} {i : α} (h : ∀ (a : α), f a ≤ g a) (hi : f i < g i) (hg : summable g) : ∑' n, f n < ∑' n, g n := has_sum_lt h hi (summable_of_le h hg).has_sum hg.has_sum @[mono] lemma tsum_strict_mono {f g : α → ℝ≥0} (hg : summable g) (h : f < g) : ∑' n, f n < ∑' n, g n := let ⟨hle, i, hi⟩ := pi.lt_def.mp h in tsum_lt_tsum hle hi hg lemma tsum_pos {g : α → ℝ≥0} (hg : summable g) (i : α) (hi : 0 < g i) : 0 < ∑' b, g b := by { rw ← tsum_zero, exact tsum_lt_tsum (λ a, zero_le _) hi hg } end nnreal namespace ennreal lemma tsum_to_real_eq {f : α → ℝ≥0∞} (hf : ∀ a, f a ≠ ∞) : (∑' a, f a).to_real = ∑' a, (f a).to_real := begin lift f to α → ℝ≥0 using hf, have : (∑' (a : α), (f a : ℝ≥0∞)).to_real = ((∑' (a : α), (f a : ℝ≥0∞)).to_nnreal : ℝ≥0∞).to_real, { rw [ennreal.coe_to_real], refl }, rw [this, ← nnreal.tsum_eq_to_nnreal_tsum, ennreal.coe_to_real], exact nnreal.coe_tsum end lemma tendsto_sum_nat_add (f : ℕ → ℝ≥0∞) (hf : ∑' i, f i ≠ ∞) : tendsto (λ i, ∑' k, f (k + i)) at_top (𝓝 0) := begin lift f to ℕ → ℝ≥0 using ennreal.ne_top_of_tsum_ne_top hf, replace hf : summable f := tsum_coe_ne_top_iff_summable.1 hf, simp only [← ennreal.coe_tsum, nnreal.summable_nat_add _ hf, ← ennreal.coe_zero], exact_mod_cast nnreal.tendsto_sum_nat_add f end end ennreal lemma tsum_comp_le_tsum_of_inj {β : Type*} {f : α → ℝ} (hf : summable f) (hn : ∀ a, 0 ≤ f a) {i : β → α} (hi : function.injective i) : tsum (f ∘ i) ≤ tsum f := begin let g : α → ℝ≥0 := λ a, ⟨f a, hn a⟩, have hg : summable g, by rwa ← nnreal.summable_coe, convert nnreal.coe_le_coe.2 (nnreal.tsum_comp_le_tsum_of_inj hg hi); { rw nnreal.coe_tsum, congr } end /-- Comparison test of convergence of series of non-negative real numbers. -/ lemma summable_of_nonneg_of_le {f g : β → ℝ} (hg : ∀b, 0 ≤ g b) (hgf : ∀b, g b ≤ f b) (hf : summable f) : summable g := let f' (b : β) : ℝ≥0 := ⟨f b, le_trans (hg b) (hgf b)⟩ in let g' (b : β) : ℝ≥0 := ⟨g b, hg b⟩ in have summable f', from nnreal.summable_coe.1 hf, have summable g', from nnreal.summable_of_le (assume b, (@nnreal.coe_le_coe (g' b) (f' b)).2 $ hgf b) this, show summable (λb, g' b : β → ℝ), from nnreal.summable_coe.2 this /-- A series of non-negative real numbers converges to `r` in the sense of `has_sum` if and only if the sequence of partial sum converges to `r`. -/ lemma has_sum_iff_tendsto_nat_of_nonneg {f : ℕ → ℝ} (hf : ∀i, 0 ≤ f i) (r : ℝ) : has_sum f r ↔ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top (𝓝 r) := begin lift f to ℕ → ℝ≥0 using hf, simp only [has_sum, ← nnreal.coe_sum, nnreal.tendsto_coe'], exact exists_congr (λ hr, nnreal.has_sum_iff_tendsto_nat) end lemma ennreal.of_real_tsum_of_nonneg {f : α → ℝ} (hf_nonneg : ∀ n, 0 ≤ f n) (hf : summable f) : ennreal.of_real (∑' n, f n) = ∑' n, ennreal.of_real (f n) := by simp_rw [ennreal.of_real, ennreal.tsum_coe_eq (nnreal.has_sum_of_real_of_nonneg hf_nonneg hf)] lemma not_summable_iff_tendsto_nat_at_top_of_nonneg {f : ℕ → ℝ} (hf : ∀ n, 0 ≤ f n) : ¬ summable f ↔ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top at_top := begin lift f to ℕ → ℝ≥0 using hf, exact_mod_cast nnreal.not_summable_iff_tendsto_nat_at_top end lemma summable_iff_not_tendsto_nat_at_top_of_nonneg {f : ℕ → ℝ} (hf : ∀ n, 0 ≤ f n) : summable f ↔ ¬ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top at_top := by rw [← not_iff_not, not_not, not_summable_iff_tendsto_nat_at_top_of_nonneg hf] lemma summable_sigma_of_nonneg {β : Π x : α, Type*} {f : (Σ x, β x) → ℝ} (hf : ∀ x, 0 ≤ f x) : summable f ↔ (∀ x, summable (λ y, f ⟨x, y⟩)) ∧ summable (λ x, ∑' y, f ⟨x, y⟩) := by { lift f to (Σ x, β x) → ℝ≥0 using hf, exact_mod_cast nnreal.summable_sigma } lemma summable_of_sum_le {ι : Type*} {f : ι → ℝ} {c : ℝ} (hf : 0 ≤ f) (h : ∀ u : finset ι, ∑ x in u, f x ≤ c) : summable f := ⟨ ⨆ u : finset ι, ∑ x in u, f x, tendsto_at_top_csupr (finset.sum_mono_set_of_nonneg hf) ⟨c, λ y ⟨u, hu⟩, hu ▸ h u⟩ ⟩ lemma summable_of_sum_range_le {f : ℕ → ℝ} {c : ℝ} (hf : ∀ n, 0 ≤ f n) (h : ∀ n, ∑ i in finset.range n, f i ≤ c) : summable f := begin apply (summable_iff_not_tendsto_nat_at_top_of_nonneg hf).2 (λ H, _), rcases exists_lt_of_tendsto_at_top H 0 c with ⟨n, -, hn⟩, exact lt_irrefl _ (hn.trans_le (h n)), end lemma tsum_le_of_sum_range_le {f : ℕ → ℝ} {c : ℝ} (hf : ∀ n, 0 ≤ f n) (h : ∀ n, ∑ i in finset.range n, f i ≤ c) : ∑' n, f n ≤ c := le_of_tendsto' ((has_sum_iff_tendsto_nat_of_nonneg hf _).1 (summable_of_sum_range_le hf h).has_sum) h /-- If a sequence `f` with non-negative terms is dominated by a sequence `g` with summable series and at least one term of `f` is strictly smaller than the corresponding term in `g`, then the series of `f` is strictly smaller than the series of `g`. -/ lemma tsum_lt_tsum_of_nonneg {i : ℕ} {f g : ℕ → ℝ} (h0 : ∀ (b : ℕ), 0 ≤ f b) (h : ∀ (b : ℕ), f b ≤ g b) (hi : f i < g i) (hg : summable g) : ∑' n, f n < ∑' n, g n := tsum_lt_tsum h hi (summable_of_nonneg_of_le h0 h hg) hg section variables [emetric_space β] open ennreal filter emetric /-- In an emetric ball, the distance between points is everywhere finite -/ lemma edist_ne_top_of_mem_ball {a : β} {r : ℝ≥0∞} (x y : ball a r) : edist x.1 y.1 ≠ ⊤ := lt_top_iff_ne_top.1 $ calc edist x y ≤ edist a x + edist a y : edist_triangle_left x.1 y.1 a ... < r + r : by rw [edist_comm a x, edist_comm a y]; exact add_lt_add x.2 y.2 ... ≤ ⊤ : le_top /-- Each ball in an extended metric space gives us a metric space, as the edist is everywhere finite. -/ def metric_space_emetric_ball (a : β) (r : ℝ≥0∞) : metric_space (ball a r) := emetric_space.to_metric_space edist_ne_top_of_mem_ball local attribute [instance] metric_space_emetric_ball lemma nhds_eq_nhds_emetric_ball (a x : β) (r : ℝ≥0∞) (h : x ∈ ball a r) : 𝓝 x = map (coe : ball a r → β) (𝓝 ⟨x, h⟩) := (map_nhds_subtype_coe_eq _ $ is_open.mem_nhds emetric.is_open_ball h).symm end section variable [pseudo_emetric_space α] open emetric lemma tendsto_iff_edist_tendsto_0 {l : filter β} {f : β → α} {y : α} : tendsto f l (𝓝 y) ↔ tendsto (λ x, edist (f x) y) l (𝓝 0) := by simp only [emetric.nhds_basis_eball.tendsto_right_iff, emetric.mem_ball, @tendsto_order ℝ≥0∞ β _ _, forall_prop_of_false ennreal.not_lt_zero, forall_const, true_and] /-- Yet another metric characterization of Cauchy sequences on integers. This one is often the most efficient. -/ lemma emetric.cauchy_seq_iff_le_tendsto_0 [nonempty β] [semilattice_sup β] {s : β → α} : cauchy_seq s ↔ (∃ (b: β → ℝ≥0∞), (∀ n m N : β, N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N) ∧ (tendsto b at_top (𝓝 0))) := ⟨begin assume hs, rw emetric.cauchy_seq_iff at hs, /- `s` is Cauchy sequence. The sequence `b` will be constructed by taking the supremum of the distances between `s n` and `s m` for `n m ≥ N`-/ let b := λN, Sup ((λ(p : β × β), edist (s p.1) (s p.2))''{p | p.1 ≥ N ∧ p.2 ≥ N}), --Prove that it bounds the distances of points in the Cauchy sequence have C : ∀ n m N, N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N, { refine λm n N hm hn, le_Sup _, use (prod.mk m n), simp only [and_true, eq_self_iff_true, set.mem_set_of_eq], exact ⟨hm, hn⟩ }, --Prove that it tends to `0`, by using the Cauchy property of `s` have D : tendsto b at_top (𝓝 0), { refine tendsto_order.2 ⟨λa ha, absurd ha (ennreal.not_lt_zero), λε εpos, _⟩, rcases exists_between εpos with ⟨δ, δpos, δlt⟩, rcases hs δ δpos with ⟨N, hN⟩, refine filter.mem_at_top_sets.2 ⟨N, λn hn, _⟩, have : b n ≤ δ := Sup_le begin simp only [and_imp, set.mem_image, set.mem_set_of_eq, exists_imp_distrib, prod.exists], intros d p q hp hq hd, rw ← hd, exact le_of_lt (hN p q (le_trans hn hp) (le_trans hn hq)) end, simpa using lt_of_le_of_lt this δlt }, -- Conclude exact ⟨b, ⟨C, D⟩⟩ end, begin rintros ⟨b, ⟨b_bound, b_lim⟩⟩, /-b : ℕ → ℝ, b_bound : ∀ (n m N : ℕ), N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N, b_lim : tendsto b at_top (𝓝 0)-/ refine emetric.cauchy_seq_iff.2 (λε εpos, _), have : ∀ᶠ n in at_top, b n < ε := (tendsto_order.1 b_lim ).2 _ εpos, rcases filter.mem_at_top_sets.1 this with ⟨N, hN⟩, exact ⟨N, λm n hm hn, calc edist (s m) (s n) ≤ b N : b_bound m n N hm hn ... < ε : (hN _ (le_refl N)) ⟩ end⟩ lemma continuous_of_le_add_edist {f : α → ℝ≥0∞} (C : ℝ≥0∞) (hC : C ≠ ⊤) (h : ∀x y, f x ≤ f y + C * edist x y) : continuous f := begin refine continuous_iff_continuous_at.2 (λx, tendsto_order.2 ⟨_, _⟩), show ∀e, e < f x → ∀ᶠ y in 𝓝 x, e < f y, { assume e he, let ε := min (f x - e) 1, have : ε < ⊤ := lt_of_le_of_lt (min_le_right _ _) (by simp [lt_top_iff_ne_top]), have : 0 < ε := by simp [ε, hC, he, ennreal.zero_lt_one], have : 0 < C⁻¹ * (ε/2) := bot_lt_iff_ne_bot.2 (by simp [hC, (ne_of_lt this).symm, mul_eq_zero]), have I : C * (C⁻¹ * (ε/2)) < ε, { by_cases C_zero : C = 0, { simp [C_zero, ‹0 < ε›] }, { calc C * (C⁻¹ * (ε/2)) = (C * C⁻¹) * (ε/2) : by simp [mul_assoc] ... = ε/2 : by simp [ennreal.mul_inv_cancel C_zero hC] ... < ε : ennreal.half_lt_self (‹0 < ε›.ne') (‹ε < ⊤›.ne) }}, have : ball x (C⁻¹ * (ε/2)) ⊆ {y : α | e < f y}, { rintros y hy, by_cases htop : f y = ⊤, { simp [htop, lt_top_iff_ne_top, ne_top_of_lt he] }, { simp at hy, have : e + ε < f y + ε := calc e + ε ≤ e + (f x - e) : add_le_add_left (min_le_left _ _) _ ... = f x : by simp [le_of_lt he] ... ≤ f y + C * edist x y : h x y ... = f y + C * edist y x : by simp [edist_comm] ... ≤ f y + C * (C⁻¹ * (ε/2)) : add_le_add_left (mul_le_mul_left' (le_of_lt hy) _) _ ... < f y + ε : (ennreal.add_lt_add_iff_left (lt_top_iff_ne_top.2 htop)).2 I, show e < f y, from (ennreal.add_lt_add_iff_right ‹ε < ⊤›).1 this }}, apply filter.mem_of_superset (ball_mem_nhds _ (‹0 < C⁻¹ * (ε/2)›)) this }, show ∀e, f x < e → ∀ᶠ y in 𝓝 x, f y < e, { assume e he, let ε := min (e - f x) 1, have : ε < ⊤ := lt_of_le_of_lt (min_le_right _ _) (by simp [lt_top_iff_ne_top]), have : 0 < ε := by simp [ε, he, ennreal.zero_lt_one], have : 0 < C⁻¹ * (ε/2) := bot_lt_iff_ne_bot.2 (by simp [hC, (ne_of_lt this).symm, mul_eq_zero]), have I : C * (C⁻¹ * (ε/2)) < ε, { by_cases C_zero : C = 0, simp [C_zero, ‹0 < ε›], calc C * (C⁻¹ * (ε/2)) = (C * C⁻¹) * (ε/2) : by simp [mul_assoc] ... = ε/2 : by simp [ennreal.mul_inv_cancel C_zero hC] ... < ε : ennreal.half_lt_self (‹0 < ε›.ne') (‹ε < ⊤›.ne) }, have : ball x (C⁻¹ * (ε/2)) ⊆ {y : α | f y < e}, { rintros y hy, have htop : f x ≠ ⊤ := ne_top_of_lt he, show f y < e, from calc f y ≤ f x + C * edist y x : h y x ... ≤ f x + C * (C⁻¹ * (ε/2)) : add_le_add_left (mul_le_mul_left' (le_of_lt hy) _) _ ... < f x + ε : (ennreal.add_lt_add_iff_left (lt_top_iff_ne_top.2 htop)).2 I ... ≤ f x + (e - f x) : add_le_add_left (min_le_left _ _) _ ... = e : by simp [le_of_lt he] }, apply filter.mem_of_superset (ball_mem_nhds _ (‹0 < C⁻¹ * (ε/2)›)) this }, end theorem continuous_edist : continuous (λp:α×α, edist p.1 p.2) := begin apply continuous_of_le_add_edist 2 (by norm_num), rintros ⟨x, y⟩ ⟨x', y'⟩, calc edist x y ≤ edist x x' + edist x' y' + edist y' y : edist_triangle4 _ _ _ _ ... = edist x' y' + (edist x x' + edist y y') : by simp [edist_comm]; cc ... ≤ edist x' y' + (edist (x, y) (x', y') + edist (x, y) (x', y')) : add_le_add_left (add_le_add (le_max_left _ _) (le_max_right _ _)) _ ... = edist x' y' + 2 * edist (x, y) (x', y') : by rw [← mul_two, mul_comm] end theorem continuous.edist [topological_space β] {f g : β → α} (hf : continuous f) (hg : continuous g) : continuous (λb, edist (f b) (g b)) := continuous_edist.comp (hf.prod_mk hg : _) theorem filter.tendsto.edist {f g : β → α} {x : filter β} {a b : α} (hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) : tendsto (λx, edist (f x) (g x)) x (𝓝 (edist a b)) := (continuous_edist.tendsto (a, b)).comp (hf.prod_mk_nhds hg) lemma cauchy_seq_of_edist_le_of_tsum_ne_top {f : ℕ → α} (d : ℕ → ℝ≥0∞) (hf : ∀ n, edist (f n) (f n.succ) ≤ d n) (hd : tsum d ≠ ∞) : cauchy_seq f := begin lift d to (ℕ → nnreal) using (λ i, ennreal.ne_top_of_tsum_ne_top hd i), rw ennreal.tsum_coe_ne_top_iff_summable at hd, exact cauchy_seq_of_edist_le_of_summable d hf hd end lemma emetric.is_closed_ball {a : α} {r : ℝ≥0∞} : is_closed (closed_ball a r) := is_closed_le (continuous_id.edist continuous_const) continuous_const @[simp] lemma emetric.diam_closure (s : set α) : diam (closure s) = diam s := begin refine le_antisymm (diam_le $ λ x hx y hy, _) (diam_mono subset_closure), have : edist x y ∈ closure (Iic (diam s)), from map_mem_closure2 (@continuous_edist α _) hx hy (λ _ _, edist_le_diam_of_mem), rwa closure_Iic at this end @[simp] lemma metric.diam_closure {α : Type*} [pseudo_metric_space α] (s : set α) : metric.diam (closure s) = diam s := by simp only [metric.diam, emetric.diam_closure] namespace real /-- For a bounded set `s : set ℝ`, its `emetric.diam` is equal to `Sup s - Inf s` reinterpreted as `ℝ≥0∞`. -/ lemma ediam_eq {s : set ℝ} (h : bounded s) : emetric.diam s = ennreal.of_real (Sup s - Inf s) := begin rcases eq_empty_or_nonempty s with rfl|hne, { simp }, refine le_antisymm (metric.ediam_le_of_forall_dist_le $ λ x hx y hy, _) _, { have := real.subset_Icc_Inf_Sup_of_bounded h, exact real.dist_le_of_mem_Icc (this hx) (this hy) }, { apply ennreal.of_real_le_of_le_to_real, rw [← metric.diam, ← metric.diam_closure], have h' := real.bounded_iff_bdd_below_bdd_above.1 h, calc Sup s - Inf s ≤ dist (Sup s) (Inf s) : le_abs_self _ ... ≤ diam (closure s) : dist_le_diam_of_mem h.closure (cSup_mem_closure hne h'.2) (cInf_mem_closure hne h'.1) } end /-- For a bounded set `s : set ℝ`, its `metric.diam` is equal to `Sup s - Inf s`. -/ lemma diam_eq {s : set ℝ} (h : bounded s) : metric.diam s = Sup s - Inf s := begin rw [metric.diam, real.ediam_eq h, ennreal.to_real_of_real], rw real.bounded_iff_bdd_below_bdd_above at h, exact sub_nonneg.2 (real.Inf_le_Sup s h.1 h.2) end @[simp] lemma ediam_Ioo (a b : ℝ) : emetric.diam (Ioo a b) = ennreal.of_real (b - a) := begin rcases le_or_lt b a with h|h, { simp [h] }, { rw [real.ediam_eq (bounded_Ioo _ _), cSup_Ioo h, cInf_Ioo h] }, end @[simp] lemma ediam_Icc (a b : ℝ) : emetric.diam (Icc a b) = ennreal.of_real (b - a) := begin rcases le_or_lt a b with h|h, { rw [real.ediam_eq (bounded_Icc _ _), cSup_Icc h, cInf_Icc h] }, { simp [h, h.le] } end @[simp] lemma ediam_Ico (a b : ℝ) : emetric.diam (Ico a b) = ennreal.of_real (b - a) := le_antisymm (ediam_Icc a b ▸ diam_mono Ico_subset_Icc_self) (ediam_Ioo a b ▸ diam_mono Ioo_subset_Ico_self) @[simp] lemma ediam_Ioc (a b : ℝ) : emetric.diam (Ioc a b) = ennreal.of_real (b - a) := le_antisymm (ediam_Icc a b ▸ diam_mono Ioc_subset_Icc_self) (ediam_Ioo a b ▸ diam_mono Ioo_subset_Ioc_self) end real /-- If `edist (f n) (f (n+1))` is bounded above by a function `d : ℕ → ℝ≥0∞`, then the distance from `f n` to the limit is bounded by `∑'_{k=n}^∞ d k`. -/ lemma edist_le_tsum_of_edist_le_of_tendsto {f : ℕ → α} (d : ℕ → ℝ≥0∞) (hf : ∀ n, edist (f n) (f n.succ) ≤ d n) {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : edist (f n) a ≤ ∑' m, d (n + m) := begin refine le_of_tendsto (tendsto_const_nhds.edist ha) (mem_at_top_sets.2 ⟨n, λ m hnm, _⟩), refine le_trans (edist_le_Ico_sum_of_edist_le hnm (λ k _ _, hf k)) _, rw [finset.sum_Ico_eq_sum_range], exact sum_le_tsum _ (λ _ _, zero_le _) ennreal.summable end /-- If `edist (f n) (f (n+1))` is bounded above by a function `d : ℕ → ℝ≥0∞`, then the distance from `f 0` to the limit is bounded by `∑'_{k=0}^∞ d k`. -/ lemma edist_le_tsum_of_edist_le_of_tendsto₀ {f : ℕ → α} (d : ℕ → ℝ≥0∞) (hf : ∀ n, edist (f n) (f n.succ) ≤ d n) {a : α} (ha : tendsto f at_top (𝓝 a)) : edist (f 0) a ≤ ∑' m, d m := by simpa using edist_le_tsum_of_edist_le_of_tendsto d hf ha 0 end --section
fe79b1d890960f20cce38840450d38863c9c0c29
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/bad3.lean
f47cdcdfc9c8bcbc27ca8d450555753790ab65d2
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
325
lean
import Int. variable list : Type -> Type variable nil {A : Type} : list A variable cons {A : Type} (head : A) (tail : list A) : list A variables a b : Int variables n m : Nat definition l1 : list Int := cons a (cons b (cons n nil)) definition l2 : list Int := cons a (cons n (cons b nil)) check cons a (cons b (cons n nil))
2137b1f2983d3a849981e8d95b961965e186703f
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1951.lean
8e1089fcde25044e1a34e6c803f31a6a78a67fa9
[ "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
845
lean
class FunLike (F : Type _) (A : outParam (Type _)) (B : outParam (A → Type _)) where toFun : F → ∀ a, B a instance [FunLike F A B] : CoeFun F fun _ => ∀ a, B a where coe := FunLike.toFun class One (M) where one : M instance [One M] : OfNat M (nat_lit 1) where ofNat := One.one class OneHomClass (F) (A B : outParam _) [One A] [One B] extends FunLike F A fun _ => B where map_one (f : F) : f 1 = 1 class MulHomClass (F) (A B : outParam _) [Mul A] [Mul B] extends FunLike F A fun _ => B class Monoid (M) extends Mul M, One M class MonoidHomClass (F) (A B : outParam _) [Monoid A] [Monoid B] extends MulHomClass F A B, OneHomClass F A B theorem map_one [Monoid A] [Monoid B] [OneHomClass F A B] (f : F) : f 1 = 1 := OneHomClass.map_one f example [Monoid M] [Monoid N] [MonoidHomClass F M N] (f : F) : f 1 = 1 := by simp only [map_one]
dc75817784f01dc48f184791292a2884f094632c
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/tests/lean/run/fieldDefaultValueWithoutType.lean
238de1dde2ff925b77f40359ed4328983038c835
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
114
lean
#lang lean4 structure S := (x := true) def f (s : S) : Bool := s.x #eval f {} theorem ex : f {} = true := rfl
76ec88e9aad70cf24429ec380d2fb640a84106ad
9ff7f5b3a42ac34566057527feaf6d6eac02c9ab
/src/category_theory/instances/rings.lean
078bac57a8973c40f358251ffb5b79e66d030052
[ "Apache-2.0" ]
permissive
spl/mathlib
40ec047255466cdabf38082bccdcaf9d7e870e32
50a03f76bb89ee5cf9d9703f78e1eb2139d9b609
refs/heads/master
1,625,718,728,368
1,548,880,101,000
1,548,890,441,000
109,381,255
0
0
null
1,509,703,801,000
1,509,703,801,000
null
UTF-8
Lean
false
false
5,259
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johannes Hölzl Introduce CommRing -- the category of commutative rings. Currently only the basic setup. -/ import category_theory.concrete_category import category_theory.instances.monoids import category_theory.fully_faithful import category_theory.adjunction import linear_algebra.multivariate_polynomial import algebra.ring universes u v open category_theory namespace category_theory.instances /-- The category of rings. -/ @[reducible] def Ring : Type (u+1) := bundled ring instance (x : Ring) : ring x := x.str instance concrete_is_ring_hom : concrete_category @is_ring_hom := ⟨by introsI α ia; apply_instance, by introsI α β γ ia ib ic f g hf hg; apply_instance⟩ instance Ring_hom_is_ring_hom {R S : Ring} (f : R ⟶ S) : is_ring_hom (f : R → S) := f.2 /-- The category of commutative rings. -/ @[reducible] def CommRing : Type (u+1) := bundled comm_ring instance (x : CommRing) : comm_ring x := x.str -- Here we don't use the `concrete` machinery, -- because it would require introducing a useless synonym for `is_ring_hom`. instance : category CommRing := { hom := λ R S, { f : R → S // is_ring_hom f }, id := λ R, ⟨ id, by resetI; apply_instance ⟩, comp := λ R S T g h, ⟨ h.1 ∘ g.1, begin haveI := g.2, haveI := h.2, apply_instance end ⟩ } namespace CommRing variables {R S T : CommRing.{u}} @[simp] lemma id_val : subtype.val (𝟙 R) = id := rfl @[simp] lemma comp_val (f : R ⟶ S) (g : S ⟶ T) : (f ≫ g).val = g.val ∘ f.val := rfl instance hom_coe : has_coe_to_fun (R ⟶ S) := { F := λ f, R → S, coe := λ f, f.1 } @[simp] lemma hom_coe_app (f : R ⟶ S) (r : R) : f r = f.val r := rfl instance hom_is_ring_hom (f : R ⟶ S) : is_ring_hom (f : R → S) := f.2 def Int : CommRing := ⟨ℤ, infer_instance⟩ def Int.cast {R : CommRing} : Int ⟶ R := { val := int.cast, property := by apply_instance } def int.eq_cast' {R : Type u} [ring R] (f : int → R) [is_ring_hom f] : f = int.cast := funext $ int.eq_cast f (is_ring_hom.map_one f) (λ _ _, is_ring_hom.map_add f) def Int.hom_unique {R : CommRing} : unique (Int ⟶ R) := { default := Int.cast, uniq := λ f, subtype.ext.mpr $ funext $ int.eq_cast f f.2.map_one f.2.map_add } /-- The forgetful functor commutative rings to Type. -/ def forget : CommRing.{u} ⥤ Type u := { obj := λ R, R, map := λ _ _ f, f } instance forget.faithful : faithful (forget) := {} /-- The functor from commutative rings to rings. -/ def to_Ring : CommRing.{u} ⥤ Ring.{u} := { obj := λ X, { α := X.1, str := by apply_instance }, map := λ X Y f, ⟨ f, by apply_instance ⟩ } instance to_Ring.faithful : faithful (to_Ring) := {} /-- The forgetful functor from commutative rings to (multiplicative) commutative monoids. -/ def forget_to_CommMon : CommRing.{u} ⥤ CommMon.{u} := { obj := λ X, { α := X.1, str := by apply_instance }, map := λ X Y f, ⟨ f, by apply_instance ⟩ } instance forget_to_CommMon.faithful : faithful (forget_to_CommMon) := {} example : faithful (forget_to_CommMon ⋙ CommMon.forget_to_Mon) := by apply_instance section open mv_polynomial local attribute [instance, priority 0] subtype.fintype set_fintype classical.prop_decidable noncomputable def polynomial : Type u ⥤ CommRing.{u} := { obj := λ α, ⟨mv_polynomial α ℤ, by apply_instance⟩, map := λ α β f, ⟨eval₂ C (X ∘ f), by apply_instance⟩, map_id' := λ α, subtype.ext.mpr $ funext $ eval₂_eta, map_comp' := λ α β γ f g, subtype.ext.mpr $ funext $ λ p, by apply mv_polynomial.induction_on p; intros; simp only [*, eval₂_add, eval₂_mul, eval₂_C, eval₂_X, comp_val, eq_self_iff_true, function.comp_app, types_comp] at * } @[simp] lemma polynomial_obj_α {α : Type u} : (polynomial.obj α).α = mv_polynomial α ℤ := rfl @[simp] lemma polynomial_map_val {α β : Type u} {f : α → β} : (polynomial.map f).val = eval₂ C (X ∘ f) := rfl noncomputable def adj : adjunction polynomial (forget : CommRing ⥤ Type u) := adjunction.mk_of_hom_equiv _ _ { hom_equiv := λ α R, { to_fun := λ f, f ∘ X, inv_fun := λ f, ⟨eval₂ int.cast f, by apply_instance⟩, left_inv := λ f, subtype.ext.mpr $ funext $ λ p, begin have H0 := λ n, (congr (int.eq_cast' (f.val ∘ C)) (rfl : n = n)).symm, have H1 := λ p₁ p₂, (@is_ring_hom.map_add _ _ _ _ f.val f.2 p₁ p₂).symm, have H2 := λ p₁ p₂, (@is_ring_hom.map_mul _ _ _ _ f.val f.2 p₁ p₂).symm, apply mv_polynomial.induction_on p; intros; simp only [*, eval₂_add, eval₂_mul, eval₂_C, eval₂_X, eq_self_iff_true, function.comp_app, hom_coe_app] at * end, right_inv := by tidy }, hom_equiv_naturality_left_symm' := λ X' X Y f g, subtype.ext.mpr $ funext $ λ p, begin apply mv_polynomial.induction_on p; intros; simp only [*, eval₂_mul, eval₂_add, eval₂_C, eval₂_X, comp_val, equiv.coe_fn_symm_mk, hom_coe_app, polynomial_map_val, eq_self_iff_true, function.comp_app, add_right_inj, types_comp] at * end } end end CommRing end category_theory.instances
84fb56335cb48a89b6f7f2ffb998b4294153e198
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/category_theory/endomorphism.lean
36cb6dccd6dab4ef58fcdae3a8ee3053a193069f
[ "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
2,744
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Scott Morrison, Simon Hudon Definition and basic properties of endomorphisms and automorphisms of an object in a category. -/ import category_theory.groupoid import data.equiv.mul_add universes v v' u u' namespace category_theory /-- Endomorphisms of an object in a category. Arguments order in multiplication agrees with `function.comp`, not with `category.comp`. -/ def End {C : Type u} [category_struct.{v} C] (X : C) := X ⟶ X namespace End section struct variables {C : Type u} [category_struct.{v} C] (X : C) instance has_one : has_one (End X) := ⟨𝟙 X⟩ /-- Multiplication of endomorphisms agrees with `function.comp`, not `category_struct.comp`. -/ instance has_mul : has_mul (End X) := ⟨λ x y, y ≫ x⟩ variable {X} @[simp] lemma one_def : (1 : End X) = 𝟙 X := rfl @[simp] lemma mul_def (xs ys : End X) : xs * ys = ys ≫ xs := rfl end struct /-- Endomorphisms of an object form a monoid -/ instance monoid {C : Type u} [category.{v} C] {X : C} : monoid (End X) := { mul_one := category.id_comp, one_mul := category.comp_id, mul_assoc := λ x y z, (category.assoc z y x).symm, ..End.has_mul X, ..End.has_one X } /-- In a groupoid, endomorphisms form a group -/ instance group {C : Type u} [groupoid.{v} C] (X : C) : group (End X) := { mul_left_inv := groupoid.comp_inv, inv := groupoid.inv, ..End.monoid } end End variables {C : Type u} [category.{v} C] (X : C) def Aut (X : C) := X ≅ X attribute [ext Aut] iso.ext namespace Aut instance : group (Aut X) := by refine { one := iso.refl X, inv := iso.symm, mul := flip iso.trans, .. } ; simp [flip, (*), has_one.one] /-- Units in the monoid of endomorphisms of an object are (multiplicatively) equivalent to automorphisms of that object. -/ def units_End_equiv_Aut : units (End X) ≃* Aut X := { to_fun := λ f, ⟨f.1, f.2, f.4, f.3⟩, inv_fun := λ f, ⟨f.1, f.2, f.4, f.3⟩, left_inv := λ ⟨f₁, f₂, f₃, f₄⟩, rfl, right_inv := λ ⟨f₁, f₂, f₃, f₄⟩, rfl, map_mul' := λ f g, by rcases f; rcases g; refl } end Aut namespace functor variables {D : Type u'} [category.{v'} D] (f : C ⥤ D) (X) /-- `f.map` as a monoid hom between endomorphism monoids. -/ def map_End : End X →* End (f.obj X) := { to_fun := functor.map f, map_mul' := λ x y, f.map_comp y x, map_one' := f.map_id X } /-- `f.map_iso` as a group hom between automorphism groups. -/ def map_Aut : Aut X →* Aut (f.obj X) := { to_fun := f.map_iso, map_mul' := λ x y, f.map_iso_trans y x, map_one' := f.map_iso_refl X } end functor end category_theory
84f9164b98e7c60b9f3ee3a4c69d085523e72047
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/geometry/manifold/instances/sphere.lean
94926f32702314dc22f2f185430232ef6c619d8c
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
20,176
lean
/- Copyright (c) 2021 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import geometry.manifold.instances.real import analysis.complex.circle import analysis.inner_product_space.calculus /-! # Manifold structure on the sphere This file defines stereographic projection from the sphere in an inner product space `E`, and uses it to put a smooth manifold structure on the sphere. ## Main results For a unit vector `v` in `E`, the definition `stereographic` gives the stereographic projection centred at `v`, a local homeomorphism from the sphere to `(ℝ ∙ v)ᗮ` (the orthogonal complement of `v`). For finite-dimensional `E`, we then construct a smooth manifold instance on the sphere; the charts here are obtained by composing the local homeomorphisms `stereographic` with arbitrary isometries from `(ℝ ∙ v)ᗮ` to Euclidean space. We prove two lemmas about smooth maps: * `times_cont_mdiff_coe_sphere` states that the coercion map from the sphere into `E` is smooth; this is a useful tool for constructing smooth maps *from* the sphere. * `times_cont_mdiff.cod_restrict_sphere` states that a map from a manifold into the sphere is smooth if its lift to a map to `E` is smooth; this is a useful tool for constructing smooth maps *to* the sphere. As an application we prove `times_cont_mdiff_neg_sphere`, that the antipodal map is smooth. Finally, we equip the `circle` (defined in `analysis.complex.circle` to be the sphere in `ℂ` centred at `0` of radius `1`) with the following structure: * a charted space with model space `euclidean_space ℝ (fin 1)` (inherited from `metric.sphere`) * a Lie group with model with corners `𝓡 1` We furthermore show that `exp_map_circle` (defined in `analysis.complex.circle` to be the natural map `λ t, exp (t * I)` from `ℝ` to `circle`) is smooth. ## Implementation notes The model space for the charted space instance is `euclidean_space ℝ (fin n)`, where `n` is a natural number satisfying the typeclass assumption `[fact (finrank ℝ E = n + 1)]`. This may seem a little awkward, but it is designed to circumvent the problem that the literal expression for the dimension of the model space (up to definitional equality) determines the type. If one used the naive expression `euclidean_space ℝ (fin (finrank ℝ E - 1))` for the model space, then the sphere in `ℂ` would be a manifold with model space `euclidean_space ℝ (fin (2 - 1))` but not with model space `euclidean_space ℝ (fin 1)`. -/ variables {E : Type*} [inner_product_space ℝ E] noncomputable theory open metric finite_dimensional open_locale manifold local attribute [instance] fact_finite_dimensional_of_finrank_eq_succ section stereographic_projection variables (v : E) /-! ### Construction of the stereographic projection -/ /-- Stereographic projection, forward direction. This is a map from an inner product space `E` to the orthogonal complement of an element `v` of `E`. It is smooth away from the affine hyperplane through `v` parallel to the orthogonal complement. It restricts on the sphere to the stereographic projection. -/ def stereo_to_fun [complete_space E] (x : E) : (ℝ ∙ v)ᗮ := (2 / ((1:ℝ) - inner_right v x)) • orthogonal_projection (ℝ ∙ v)ᗮ x variables {v} @[simp] lemma stereo_to_fun_apply [complete_space E] (x : E) : stereo_to_fun v x = (2 / ((1:ℝ) - inner_right v x)) • orthogonal_projection (ℝ ∙ v)ᗮ x := rfl lemma times_cont_diff_on_stereo_to_fun [complete_space E] : times_cont_diff_on ℝ ⊤ (stereo_to_fun v) {x : E | inner_right v x ≠ (1:ℝ)} := begin refine times_cont_diff_on.smul _ (orthogonal_projection ((ℝ ∙ v)ᗮ)).times_cont_diff.times_cont_diff_on, refine times_cont_diff_const.times_cont_diff_on.div _ _, { exact (times_cont_diff_const.sub (inner_right v).times_cont_diff).times_cont_diff_on }, { intros x h h', exact h (sub_eq_zero.mp h').symm } end lemma continuous_on_stereo_to_fun [complete_space E] : continuous_on (stereo_to_fun v) {x : E | inner_right v x ≠ (1:ℝ)} := times_cont_diff_on_stereo_to_fun.continuous_on variables (v) /-- Auxiliary function for the construction of the reverse direction of the stereographic projection. This is a map from the orthogonal complement of a unit vector `v` in an inner product space `E` to `E`; we will later prove that it takes values in the unit sphere. For most purposes, use `stereo_inv_fun`, not `stereo_inv_fun_aux`. -/ def stereo_inv_fun_aux (w : E) : E := (∥w∥ ^ 2 + 4)⁻¹ • ((4:ℝ) • w + (∥w∥ ^ 2 - 4) • v) variables {v} @[simp] lemma stereo_inv_fun_aux_apply (w : E) : stereo_inv_fun_aux v w = (∥w∥ ^ 2 + 4)⁻¹ • ((4:ℝ) • w + (∥w∥ ^ 2 - 4) • v) := rfl lemma stereo_inv_fun_aux_mem (hv : ∥v∥ = 1) {w : E} (hw : w ∈ (ℝ ∙ v)ᗮ) : stereo_inv_fun_aux v w ∈ (sphere (0:E) 1) := begin have h₁ : 0 ≤ ∥w∥ ^ 2 + 4 := by nlinarith, suffices : ∥(4:ℝ) • w + (∥w∥ ^ 2 - 4) • v∥ = ∥w∥ ^ 2 + 4, { have h₂ : ∥w∥ ^ 2 + 4 ≠ 0 := by nlinarith, simp only [mem_sphere_zero_iff_norm, norm_smul, real.norm_eq_abs, abs_inv, this, abs_of_nonneg h₁, stereo_inv_fun_aux_apply], field_simp }, suffices : ∥(4:ℝ) • w + (∥w∥ ^ 2 - 4) • v∥ ^ 2 = (∥w∥ ^ 2 + 4) ^ 2, { have h₃ : 0 ≤ ∥stereo_inv_fun_aux v w∥ := norm_nonneg _, simpa [h₁, h₃, -one_pow] using this }, simp [norm_add_sq_real, norm_smul, inner_smul_left, inner_smul_right, inner_left_of_mem_orthogonal_singleton _ hw, mul_pow, real.norm_eq_abs, hv], ring end lemma times_cont_diff_stereo_inv_fun_aux : times_cont_diff ℝ ⊤ (stereo_inv_fun_aux v) := begin have h₀ : times_cont_diff ℝ ⊤ (λ w : E, ∥w∥ ^ 2) := times_cont_diff_norm_sq, have h₁ : times_cont_diff ℝ ⊤ (λ w : E, (∥w∥ ^ 2 + 4)⁻¹), { refine (h₀.add times_cont_diff_const).inv _, intros x, nlinarith }, have h₂ : times_cont_diff ℝ ⊤ (λ w, (4:ℝ) • w + (∥w∥ ^ 2 - 4) • v), { refine (times_cont_diff_const.smul times_cont_diff_id).add _, refine (h₀.sub times_cont_diff_const).smul times_cont_diff_const }, exact h₁.smul h₂ end /-- Stereographic projection, reverse direction. This is a map from the orthogonal complement of a unit vector `v` in an inner product space `E` to the unit sphere in `E`. -/ def stereo_inv_fun (hv : ∥v∥ = 1) (w : (ℝ ∙ v)ᗮ) : sphere (0:E) 1 := ⟨stereo_inv_fun_aux v (w:E), stereo_inv_fun_aux_mem hv w.2⟩ @[simp] lemma stereo_inv_fun_apply (hv : ∥v∥ = 1) (w : (ℝ ∙ v)ᗮ) : (stereo_inv_fun hv w : E) = (∥w∥ ^ 2 + 4)⁻¹ • ((4:ℝ) • w + (∥w∥ ^ 2 - 4) • v) := rfl lemma stereo_inv_fun_ne_north_pole (hv : ∥v∥ = 1) (w : (ℝ ∙ v)ᗮ) : stereo_inv_fun hv w ≠ (⟨v, by simp [hv]⟩ : sphere (0:E) 1) := begin refine subtype.ne_of_val_ne _, rw ← inner_lt_one_iff_real_of_norm_one _ hv, { have hw : ⟪v, w⟫_ℝ = 0 := inner_right_of_mem_orthogonal_singleton v w.2, have hw' : (∥(w:E)∥ ^ 2 + 4)⁻¹ * (∥(w:E)∥ ^ 2 - 4) < 1, { refine (inv_mul_lt_iff' _).mpr _, { nlinarith }, linarith }, simpa [real_inner_comm, inner_add_right, inner_smul_right, real_inner_self_eq_norm_sq, hw, hv] using hw' }, { simpa using stereo_inv_fun_aux_mem hv w.2 } end lemma continuous_stereo_inv_fun (hv : ∥v∥ = 1) : continuous (stereo_inv_fun hv) := continuous_induced_rng (times_cont_diff_stereo_inv_fun_aux.continuous.comp continuous_subtype_coe) variables [complete_space E] lemma stereo_left_inv (hv : ∥v∥ = 1) {x : sphere (0:E) 1} (hx : (x:E) ≠ v) : stereo_inv_fun hv (stereo_to_fun v x) = x := begin ext, simp only [stereo_to_fun_apply, stereo_inv_fun_apply, smul_add], -- name two frequently-occuring quantities and write down their basic properties set a : ℝ := inner_right v x, set y := orthogonal_projection (ℝ ∙ v)ᗮ x, have split : ↑x = a • v + ↑y, { convert eq_sum_orthogonal_projection_self_orthogonal_complement (ℝ ∙ v) x, exact (orthogonal_projection_unit_singleton ℝ hv x).symm }, have hvy : ⟪v, y⟫_ℝ = 0 := inner_right_of_mem_orthogonal_singleton v y.2, have pythag : 1 = a ^ 2 + ∥y∥ ^ 2, { have hvy' : ⟪a • v, y⟫_ℝ = 0 := by simp [inner_smul_left, hvy], convert norm_add_sq_eq_norm_sq_add_norm_sq_of_inner_eq_zero _ _ hvy' using 2, { simp [← split] }, { simp [norm_smul, hv, real.norm_eq_abs, ← sq, sq_abs] }, { exact sq _ } }, -- two facts which will be helpful for clearing denominators in the main calculation have ha : 1 - a ≠ 0, { have : a < 1 := (inner_lt_one_iff_real_of_norm_one hv (by simp)).mpr hx.symm, linarith }, have : 2 ^ 2 * ∥y∥ ^ 2 + 4 * (1 - a) ^ 2 ≠ 0, { refine ne_of_gt _, have := norm_nonneg (y:E), have : 0 < (1 - a) ^ 2 := sq_pos_of_ne_zero (1 - a) ha, nlinarith }, -- the core of the problem is these two algebraic identities: have h₁ : (2 ^ 2 / (1 - a) ^ 2 * ∥y∥ ^ 2 + 4)⁻¹ * 4 * (2 / (1 - a)) = 1, { field_simp, nlinarith }, have h₂ : (2 ^ 2 / (1 - a) ^ 2 * ∥y∥ ^ 2 + 4)⁻¹ * (2 ^ 2 / (1 - a) ^ 2 * ∥y∥ ^ 2 - 4) = a, { field_simp, transitivity (1 - a) ^ 2 * (a * (2 ^ 2 * ∥y∥ ^ 2 + 4 * (1 - a) ^ 2)), { congr, nlinarith }, ring_nf, ring }, -- deduce the result convert congr_arg2 has_add.add (congr_arg (λ t, t • (y:E)) h₁) (congr_arg (λ t, t • v) h₂) using 1, { simp [inner_add_right, inner_smul_right, hvy, real_inner_self_eq_norm_sq, hv, mul_smul, mul_pow, real.norm_eq_abs, sq_abs, norm_smul] }, { simp [split, add_comm] } end lemma stereo_right_inv (hv : ∥v∥ = 1) (w : (ℝ ∙ v)ᗮ) : stereo_to_fun v (stereo_inv_fun hv w) = w := begin have : 2 / (1 - (∥(w:E)∥ ^ 2 + 4)⁻¹ * (∥(w:E)∥ ^ 2 - 4)) * (∥(w:E)∥ ^ 2 + 4)⁻¹ * 4 = 1, { have : ∥(w:E)∥ ^ 2 + 4 ≠ 0 := by nlinarith, have : (4:ℝ) + 4 ≠ 0 := by nlinarith, field_simp, ring }, convert congr_arg (λ c, c • w) this, { have h₁ : orthogonal_projection (ℝ ∙ v)ᗮ v = 0 := orthogonal_projection_orthogonal_complement_singleton_eq_zero v, have h₂ : orthogonal_projection (ℝ ∙ v)ᗮ w = w := orthogonal_projection_mem_subspace_eq_self w, have h₃ : inner_right v w = (0:ℝ) := inner_right_of_mem_orthogonal_singleton v w.2, have h₄ : inner_right v v = (1:ℝ) := by simp [real_inner_self_eq_norm_sq, hv], simp [h₁, h₂, h₃, h₄, continuous_linear_map.map_add, continuous_linear_map.map_smul, mul_smul] }, { simp } end /-- Stereographic projection from the unit sphere in `E`, centred at a unit vector `v` in `E`; this is the version as a local homeomorphism. -/ def stereographic (hv : ∥v∥ = 1) : local_homeomorph (sphere (0:E) 1) (ℝ ∙ v)ᗮ := { to_fun := (stereo_to_fun v) ∘ coe, inv_fun := stereo_inv_fun hv, source := {⟨v, by simp [hv]⟩}ᶜ, target := set.univ, map_source' := by simp, map_target' := λ w _, stereo_inv_fun_ne_north_pole hv w, left_inv' := λ _ hx, stereo_left_inv hv (λ h, hx (subtype.ext h)), right_inv' := λ w _, stereo_right_inv hv w, open_source := is_open_compl_singleton, open_target := is_open_univ, continuous_to_fun := continuous_on_stereo_to_fun.comp continuous_subtype_coe.continuous_on (λ w h, h ∘ subtype.ext ∘ eq.symm ∘ (inner_eq_norm_mul_iff_of_norm_one hv (by simp)).mp), continuous_inv_fun := (continuous_stereo_inv_fun hv).continuous_on } @[simp] lemma stereographic_source (hv : ∥v∥ = 1) : (stereographic hv).source = {⟨v, by simp [hv]⟩}ᶜ := rfl @[simp] lemma stereographic_target (hv : ∥v∥ = 1) : (stereographic hv).target = set.univ := rfl end stereographic_projection section charted_space /-! ### Charted space structure on the sphere In this section we construct a charted space structure on the unit sphere in a finite-dimensional real inner product space `E`; that is, we show that it is locally homeomorphic to the Euclidean space of dimension one less than `E`. The restriction to finite dimension is for convenience. The most natural `charted_space` structure for the sphere uses the stereographic projection from the antipodes of a point as the canonical chart at this point. However, the codomain of the stereographic projection constructed in the previous section is `(ℝ ∙ v)ᗮ`, the orthogonal complement of the vector `v` in `E` which is the "north pole" of the projection, so a priori these charts all have different codomains. So it is necessary to prove that these codomains are all continuously linearly equivalent to a fixed normed space. This could be proved in general by a simple case of Gram-Schmidt orthogonalization, but in the finite-dimensional case it follows more easily by dimension-counting. -/ /-- Variant of the stereographic projection, for the sphere in an `n + 1`-dimensional inner product space `E`. This version has codomain the Euclidean space of dimension `n`, and is obtained by composing the original sterographic projection (`stereographic`) with an arbitrary linear isometry from `(ℝ ∙ v)ᗮ` to the Euclidean space. -/ def stereographic' (n : ℕ) [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) : local_homeomorph (sphere (0:E) 1) (euclidean_space ℝ (fin n)) := (stereographic (norm_eq_of_mem_sphere v)) ≫ₕ (linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere v)).to_homeomorph.to_local_homeomorph @[simp] lemma stereographic'_source {n : ℕ} [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) : (stereographic' n v).source = {v}ᶜ := by simp [stereographic'] @[simp] lemma stereographic'_target {n : ℕ} [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) : (stereographic' n v).target = set.univ := by simp [stereographic'] /-- The unit sphere in an `n + 1`-dimensional inner product space `E` is a charted space modelled on the Euclidean space of dimension `n`. -/ instance {n : ℕ} [fact (finrank ℝ E = n + 1)] : charted_space (euclidean_space ℝ (fin n)) (sphere (0:E) 1) := { atlas := {f | ∃ v : (sphere (0:E) 1), f = stereographic' n v}, chart_at := λ v, stereographic' n (-v), mem_chart_source := λ v, by simpa using ne_neg_of_mem_unit_sphere ℝ v, chart_mem_atlas := λ v, ⟨-v, rfl⟩ } end charted_space section smooth_manifold /-! ### Smooth manifold structure on the sphere -/ /-- The unit sphere in an `n + 1`-dimensional inner product space `E` is a smooth manifold, modelled on the Euclidean space of dimension `n`. -/ instance {n : ℕ} [fact (finrank ℝ E = n + 1)] : smooth_manifold_with_corners (𝓡 n) (sphere (0:E) 1) := smooth_manifold_with_corners_of_times_cont_diff_on (𝓡 n) (sphere (0:E) 1) begin rintros _ _ ⟨v, rfl⟩ ⟨v', rfl⟩, let U : (ℝ ∙ (v:E))ᗮ ≃ₗᵢ[ℝ] euclidean_space ℝ (fin n) := linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere v), let U' : (ℝ ∙ (v':E))ᗮ ≃ₗᵢ[ℝ] euclidean_space ℝ (fin n) := linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere v'), have hUv : stereographic' n v = (stereographic (norm_eq_of_mem_sphere v)) ≫ₕ U.to_homeomorph.to_local_homeomorph := rfl, have hU'v' : stereographic' n v' = (stereographic (norm_eq_of_mem_sphere v')).trans U'.to_homeomorph.to_local_homeomorph := rfl, have H₁ := U'.times_cont_diff.comp_times_cont_diff_on times_cont_diff_on_stereo_to_fun, have H₂ := (times_cont_diff_stereo_inv_fun_aux.comp (ℝ ∙ (v:E))ᗮ.subtypeL.times_cont_diff).comp U.symm.times_cont_diff, convert H₁.comp' (H₂.times_cont_diff_on : times_cont_diff_on ℝ ⊤ _ set.univ) using 1, have h_set : ∀ p : sphere (0:E) 1, p = v' ↔ ⟪(p:E), v'⟫_ℝ = 1, { simp [subtype.ext_iff, inner_eq_norm_mul_iff_of_norm_one] }, ext, simp [h_set, hUv, hU'v', stereographic, real_inner_comm] end /-- The inclusion map (i.e., `coe`) from the sphere in `E` to `E` is smooth. -/ lemma times_cont_mdiff_coe_sphere {n : ℕ} [fact (finrank ℝ E = n + 1)] : times_cont_mdiff (𝓡 n) 𝓘(ℝ, E) ∞ (coe : (sphere (0:E) 1) → E) := begin rw times_cont_mdiff_iff, split, { exact continuous_subtype_coe }, { intros v _, let U : (ℝ ∙ ((-v):E))ᗮ ≃ₗᵢ[ℝ] euclidean_space ℝ (fin n) := linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere (-v)), exact ((times_cont_diff_stereo_inv_fun_aux.comp (ℝ ∙ ((-v):E))ᗮ.subtypeL.times_cont_diff).comp U.symm.times_cont_diff).times_cont_diff_on } end variables {F : Type*} [normed_group F] [normed_space ℝ F] variables {H : Type*} [topological_space H] {I : model_with_corners ℝ F H} variables {M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] /-- If a `times_cont_mdiff` function `f : M → E`, where `M` is some manifold, takes values in the sphere, then it restricts to a `times_cont_mdiff` function from `M` to the sphere. -/ lemma times_cont_mdiff.cod_restrict_sphere {n : ℕ} [fact (finrank ℝ E = n + 1)] {m : with_top ℕ} {f : M → E} (hf : times_cont_mdiff I 𝓘(ℝ, E) m f) (hf' : ∀ x, f x ∈ sphere (0:E) 1) : times_cont_mdiff I (𝓡 n) m (set.cod_restrict _ _ hf' : M → (sphere (0:E) 1)) := begin rw times_cont_mdiff_iff_target, refine ⟨continuous_induced_rng hf.continuous, _⟩, intros v, let U : (ℝ ∙ ((-v):E))ᗮ ≃ₗᵢ[ℝ] euclidean_space ℝ (fin n) := (linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere (-v))), have h : times_cont_diff_on ℝ ⊤ U set.univ := U.times_cont_diff.times_cont_diff_on, have H₁ := (h.comp' times_cont_diff_on_stereo_to_fun).times_cont_mdiff_on, have H₂ : times_cont_mdiff_on _ _ _ _ set.univ := hf.times_cont_mdiff_on, convert (H₁.of_le le_top).comp' H₂ using 1, ext x, have hfxv : f x = -↑v ↔ ⟪f x, -↑v⟫_ℝ = 1, { have hfx : ∥f x∥ = 1 := by simpa using hf' x, rw inner_eq_norm_mul_iff_of_norm_one hfx, exact norm_eq_of_mem_sphere (-v) }, dsimp [chart_at], simp [not_iff_not, subtype.ext_iff, hfxv, real_inner_comm] end /-- The antipodal map is smooth. -/ lemma times_cont_mdiff_neg_sphere {n : ℕ} [fact (finrank ℝ E = n + 1)] : times_cont_mdiff (𝓡 n) (𝓡 n) ∞ (λ x : sphere (0:E) 1, -x) := (times_cont_diff_neg.times_cont_mdiff.comp times_cont_mdiff_coe_sphere).cod_restrict_sphere _ end smooth_manifold section circle open complex local attribute [instance] finrank_real_complex_fact /-- The unit circle in `ℂ` is a charted space modelled on `euclidean_space ℝ (fin 1)`. This follows by definition from the corresponding result for `metric.sphere`. -/ instance : charted_space (euclidean_space ℝ (fin 1)) circle := metric.sphere.charted_space instance : smooth_manifold_with_corners (𝓡 1) circle := metric.sphere.smooth_manifold_with_corners /-- The unit circle in `ℂ` is a Lie group. -/ instance : lie_group (𝓡 1) circle := { smooth_mul := begin let c : circle → ℂ := coe, have h₁ : times_cont_mdiff _ _ _ (prod.map c c) := times_cont_mdiff_coe_sphere.prod_map times_cont_mdiff_coe_sphere, have h₂ : times_cont_mdiff (𝓘(ℝ, ℂ).prod 𝓘(ℝ, ℂ)) 𝓘(ℝ, ℂ) ∞ (λ (z : ℂ × ℂ), z.fst * z.snd), { rw times_cont_mdiff_iff, exact ⟨continuous_mul, λ x y, (times_cont_diff_mul.restrict_scalars ℝ).times_cont_diff_on⟩ }, exact (h₂.comp h₁).cod_restrict_sphere _, end, smooth_inv := (complex.conj_cle.times_cont_diff.times_cont_mdiff.comp times_cont_mdiff_coe_sphere).cod_restrict_sphere _, .. metric.sphere.smooth_manifold_with_corners } /-- The map `λ t, exp (t * I)` from `ℝ` to the unit circle in `ℂ` is smooth. -/ lemma times_cont_mdiff_exp_map_circle : times_cont_mdiff 𝓘(ℝ, ℝ) (𝓡 1) ∞ exp_map_circle := (((times_cont_diff_exp.restrict_scalars ℝ).comp (times_cont_diff_id.smul times_cont_diff_const)).times_cont_mdiff).cod_restrict_sphere _ end circle
396f2b8df90f7174acf89b3551be28892390e158
4727251e0cd73359b15b664c3170e5d754078599
/src/ring_theory/ideal/quotient.lean
f2e732ba00571101626a9d1fd147feaec47fae68
[ "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
15,919
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes, Mario Carneiro, Anne Baanen -/ import linear_algebra.quotient import ring_theory.ideal.basic /-! # Ideal quotients This file defines ideal quotients as a special case of submodule quotients and proves some basic results about these quotients. See `algebra.ring_quot` for quotients of non-commutative rings. ## Main definitions - `ideal.quotient`: the quotient of a commutative ring `R` by an ideal `I : ideal R` ## Main results - `ideal.quotient_inf_ring_equiv_pi_quotient`: the **Chinese Remainder Theorem** -/ universes u v w namespace ideal open set open_locale big_operators variables {R : Type u} [comm_ring R] (I : ideal R) {a b : R} variables {S : Type v} /-- The quotient `R/I` of a ring `R` by an ideal `I`. The ideal quotient of `I` is defined to equal the quotient of `I` as an `R`-submodule of `R`. This definition is marked `reducible` so that typeclass instances can be shared between `ideal.quotient I` and `submodule.quotient I`. -/ -- Note that at present `ideal` means a left-ideal, -- so this quotient is only useful in a commutative ring. -- We should develop quotients by two-sided ideals as well. @[reducible] instance : has_quotient R (ideal R) := submodule.has_quotient namespace quotient variables {I} {x y : R} instance has_one (I : ideal R) : has_one (R ⧸ I) := ⟨submodule.quotient.mk 1⟩ instance has_mul (I : ideal R) : has_mul (R ⧸ I) := ⟨λ a b, quotient.lift_on₂' a b (λ a b, submodule.quotient.mk (a * b)) $ λ a₁ a₂ b₁ b₂ h₁ h₂, quot.sound $ begin rw submodule.quotient_rel_r_def at h₁ h₂ ⊢, have F := I.add_mem (I.mul_mem_left a₂ h₁) (I.mul_mem_right b₁ h₂), have : a₁ * a₂ - b₁ * b₂ = a₂ * (a₁ - b₁) + (a₂ - b₂) * b₁, { rw [mul_sub, sub_mul, sub_add_sub_cancel, mul_comm, mul_comm b₁] }, rw ← this at F, change _ ∈ _, convert F, end⟩ instance comm_ring (I : ideal R) : comm_ring (R ⧸ I) := { mul := (*), one := 1, mul_assoc := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (mul_assoc a b c), mul_comm := λ a b, quotient.induction_on₂' a b $ λ a b, congr_arg submodule.quotient.mk (mul_comm a b), one_mul := λ a, quotient.induction_on' a $ λ a, congr_arg submodule.quotient.mk (one_mul a), mul_one := λ a, quotient.induction_on' a $ λ a, congr_arg submodule.quotient.mk (mul_one a), left_distrib := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (left_distrib a b c), right_distrib := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (right_distrib a b c), ..submodule.quotient.add_comm_group I } /-- The ring homomorphism from a ring `R` to a quotient ring `R/I`. -/ def mk (I : ideal R) : R →+* (R ⧸ I) := ⟨λ a, submodule.quotient.mk a, rfl, λ _ _, rfl, rfl, λ _ _, rfl⟩ /- Two `ring_homs`s from the quotient by an ideal are equal if their compositions with `ideal.quotient.mk'` are equal. See note [partially-applied ext lemmas]. -/ @[ext] lemma ring_hom_ext [non_assoc_semiring S] ⦃f g : R ⧸ I →+* S⦄ (h : f.comp (mk I) = g.comp (mk I)) : f = g := ring_hom.ext $ λ x, quotient.induction_on' x $ (ring_hom.congr_fun h : _) instance inhabited : inhabited (R ⧸ I) := ⟨mk I 37⟩ protected theorem eq : mk I x = mk I y ↔ x - y ∈ I := submodule.quotient.eq I @[simp] theorem mk_eq_mk (x : R) : (submodule.quotient.mk x : R ⧸ I) = mk I x := rfl lemma eq_zero_iff_mem {I : ideal R} : mk I a = 0 ↔ a ∈ I := submodule.quotient.mk_eq_zero _ theorem zero_eq_one_iff {I : ideal R} : (0 : R ⧸ I) = 1 ↔ I = ⊤ := eq_comm.trans $ eq_zero_iff_mem.trans (eq_top_iff_one _).symm theorem zero_ne_one_iff {I : ideal R} : (0 : R ⧸ I) ≠ 1 ↔ I ≠ ⊤ := not_congr zero_eq_one_iff protected theorem nontrivial {I : ideal R} (hI : I ≠ ⊤) : nontrivial (R ⧸ I) := ⟨⟨0, 1, zero_ne_one_iff.2 hI⟩⟩ lemma subsingleton_iff {I : ideal R} : subsingleton (R ⧸ I) ↔ I = ⊤ := by rw [eq_top_iff_one, ← subsingleton_iff_zero_eq_one, eq_comm, ← I^.quotient.mk^.map_one, quotient.eq_zero_iff_mem] instance : unique (R ⧸ (⊤ : ideal R)) := ⟨⟨0⟩, by rintro ⟨x⟩; exact quotient.eq_zero_iff_mem.mpr submodule.mem_top⟩ lemma mk_surjective : function.surjective (mk I) := λ y, quotient.induction_on' y (λ x, exists.intro x rfl) /-- If `I` is an ideal of a commutative ring `R`, if `q : R → R/I` is the quotient map, and if `s ⊆ R` is a subset, then `q⁻¹(q(s)) = ⋃ᵢ(i + s)`, the union running over all `i ∈ I`. -/ lemma quotient_ring_saturate (I : ideal R) (s : set R) : mk I ⁻¹' (mk I '' s) = (⋃ x : I, (λ y, x.1 + y) '' s) := begin ext x, simp only [mem_preimage, mem_image, mem_Union, ideal.quotient.eq], exact ⟨λ ⟨a, a_in, h⟩, ⟨⟨_, I.neg_mem h⟩, a, a_in, by simp⟩, λ ⟨⟨i, hi⟩, a, ha, eq⟩, ⟨a, ha, by rw [← eq, sub_add_eq_sub_sub_swap, sub_self, zero_sub]; exact I.neg_mem hi⟩⟩ end instance is_domain (I : ideal R) [hI : I.is_prime] : is_domain (R ⧸ I) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b, quotient.induction_on₂' a b $ λ a b hab, (hI.mem_or_mem (eq_zero_iff_mem.1 hab)).elim (or.inl ∘ eq_zero_iff_mem.2) (or.inr ∘ eq_zero_iff_mem.2), .. quotient.nontrivial hI.1 } lemma is_domain_iff_prime (I : ideal R) : is_domain (R ⧸ I) ↔ I.is_prime := ⟨ λ ⟨h1, h2⟩, ⟨zero_ne_one_iff.1 $ @zero_ne_one _ _ ⟨h2⟩, λ x y h, by { simp only [←eq_zero_iff_mem, (mk I).map_mul] at ⊢ h, exact h1 h}⟩, λ h, by { resetI, apply_instance }⟩ lemma exists_inv {I : ideal R} [hI : I.is_maximal] : ∀ {a : (R ⧸ I)}, a ≠ 0 → ∃ b : (R ⧸ I), a * b = 1 := begin rintro ⟨a⟩ h, rcases hI.exists_inv (mt eq_zero_iff_mem.2 h) with ⟨b, c, hc, abc⟩, rw [mul_comm] at abc, refine ⟨mk _ b, quot.sound _⟩, --quot.sound hb rw ← eq_sub_iff_add_eq' at abc, rw [abc, ← neg_mem_iff, neg_sub] at hc, rw submodule.quotient_rel_r_def, convert hc, end open_locale classical /-- quotient by maximal ideal is a field. def rather than instance, since users will have computable inverses in some applications. See note [reducible non-instances]. -/ @[reducible] protected noncomputable def field (I : ideal R) [hI : I.is_maximal] : field (R ⧸ I) := { inv := λ a, if ha : a = 0 then 0 else classical.some (exists_inv ha), mul_inv_cancel := λ a (ha : a ≠ 0), show a * dite _ _ _ = _, by rw dif_neg ha; exact classical.some_spec (exists_inv ha), inv_zero := dif_pos rfl, ..quotient.comm_ring I, ..quotient.is_domain I } /-- If the quotient by an ideal is a field, then the ideal is maximal. -/ theorem maximal_of_is_field (I : ideal R) (hqf : is_field (R ⧸ I)) : I.is_maximal := begin apply ideal.is_maximal_iff.2, split, { intro h, rcases hqf.exists_pair_ne with ⟨⟨x⟩, ⟨y⟩, hxy⟩, exact hxy (ideal.quotient.eq.2 (mul_one (x - y) ▸ I.mul_mem_left _ h)) }, { intros J x hIJ hxnI hxJ, rcases hqf.mul_inv_cancel (mt ideal.quotient.eq_zero_iff_mem.1 hxnI) with ⟨⟨y⟩, hy⟩, rw [← zero_add (1 : R), ← sub_self (x * y), sub_add], refine J.sub_mem (J.mul_mem_right _ hxJ) (hIJ (ideal.quotient.eq.1 hy)) } end /-- The quotient of a ring by an ideal is a field iff the ideal is maximal. -/ theorem maximal_ideal_iff_is_field_quotient (I : ideal R) : I.is_maximal ↔ is_field (R ⧸ I) := ⟨λ h, @field.to_is_field (R ⧸ I) (@ideal.quotient.field _ _ I h), λ h, maximal_of_is_field I h⟩ variable [comm_ring S] /-- Given a ring homomorphism `f : R →+* S` sending all elements of an ideal to zero, lift it to the quotient by this ideal. -/ def lift (I : ideal R) (f : R →+* S) (H : ∀ (a : R), a ∈ I → f a = 0) : R ⧸ I →+* S := { map_one' := f.map_one, map_zero' := f.map_zero, map_add' := λ a₁ a₂, quotient.induction_on₂' a₁ a₂ f.map_add, map_mul' := λ a₁ a₂, quotient.induction_on₂' a₁ a₂ f.map_mul, .. quotient_add_group.lift I.to_add_subgroup f.to_add_monoid_hom H } @[simp] lemma lift_mk (I : ideal R) (f : R →+* S) (H : ∀ (a : R), a ∈ I → f a = 0) : lift I f H (mk I a) = f a := rfl /-- The ring homomorphism from the quotient by a smaller ideal to the quotient by a larger ideal. This is the `ideal.quotient` version of `quot.factor` -/ def factor (S T : ideal R) (H : S ≤ T) : R ⧸ S →+* R ⧸ T := ideal.quotient.lift S (T^.quotient.mk) (λ x hx, eq_zero_iff_mem.2 (H hx)) @[simp] lemma factor_mk (S T : ideal R) (H : S ≤ T) (x : R) : factor S T H (mk S x) = mk T x := rfl @[simp] lemma factor_comp_mk (S T : ideal R) (H : S ≤ T) : (factor S T H).comp (mk S) = mk T := by { ext x, rw [ring_hom.comp_apply, factor_mk] } end quotient /-- Quotienting by equal ideals gives equivalent rings. See also `submodule.quot_equiv_of_eq`. -/ def quot_equiv_of_eq {R : Type*} [comm_ring R] {I J : ideal R} (h : I = J) : (R ⧸ I) ≃+* R ⧸ J := { map_mul' := by { rintro ⟨x⟩ ⟨y⟩, refl }, .. submodule.quot_equiv_of_eq I J h } @[simp] lemma quot_equiv_of_eq_mk {R : Type*} [comm_ring R] {I J : ideal R} (h : I = J) (x : R) : quot_equiv_of_eq h (ideal.quotient.mk I x) = ideal.quotient.mk J x := rfl section pi variables (ι : Type v) /-- `R^n/I^n` is a `R/I`-module. -/ instance module_pi : module (R ⧸ I) ((ι → R) ⧸ I.pi ι) := { smul := λ c m, quotient.lift_on₂' c m (λ r m, submodule.quotient.mk $ r • m) begin intros c₁ m₁ c₂ m₂ hc hm, apply ideal.quotient.eq.2, rw submodule.quotient_rel_r_def at hc hm, intro i, exact I.mul_sub_mul_mem hc (hm i), end, one_smul := begin rintro ⟨a⟩, change ideal.quotient.mk _ _ = ideal.quotient.mk _ _, congr' with i, exact one_mul (a i), end, mul_smul := begin rintro ⟨a⟩ ⟨b⟩ ⟨c⟩, change ideal.quotient.mk _ _ = ideal.quotient.mk _ _, simp only [(•)], congr' with i, exact mul_assoc a b (c i), end, smul_add := begin rintro ⟨a⟩ ⟨b⟩ ⟨c⟩, change ideal.quotient.mk _ _ = ideal.quotient.mk _ _, congr' with i, exact mul_add a (b i) (c i), end, smul_zero := begin rintro ⟨a⟩, change ideal.quotient.mk _ _ = ideal.quotient.mk _ _, congr' with i, exact mul_zero a, end, add_smul := begin rintro ⟨a⟩ ⟨b⟩ ⟨c⟩, change ideal.quotient.mk _ _ = ideal.quotient.mk _ _, congr' with i, exact add_mul a b (c i), end, zero_smul := begin rintro ⟨a⟩, change ideal.quotient.mk _ _ = ideal.quotient.mk _ _, congr' with i, exact zero_mul (a i), end, } /-- `R^n/I^n` is isomorphic to `(R/I)^n` as an `R/I`-module. -/ noncomputable def pi_quot_equiv : ((ι → R) ⧸ I.pi ι) ≃ₗ[(R ⧸ I)] (ι → (R ⧸ I)) := { to_fun := λ x, quotient.lift_on' x (λ f i, ideal.quotient.mk I (f i)) $ λ a b hab, funext (λ i, (submodule.quotient.eq' _).2 (hab i)), map_add' := by { rintros ⟨_⟩ ⟨_⟩, refl }, map_smul' := by { rintros ⟨_⟩ ⟨_⟩, refl }, inv_fun := λ x, ideal.quotient.mk (I.pi ι) $ λ i, quotient.out' (x i), left_inv := begin rintro ⟨x⟩, exact ideal.quotient.eq.2 (λ i, ideal.quotient.eq.1 (quotient.out_eq' _)) end, right_inv := begin intro x, ext i, obtain ⟨r, hr⟩ := @quot.exists_rep _ _ (x i), simp_rw ←hr, convert quotient.out_eq' _ end } /-- If `f : R^n → R^m` is an `R`-linear map and `I ⊆ R` is an ideal, then the image of `I^n` is contained in `I^m`. -/ lemma map_pi {ι} [fintype ι] {ι' : Type w} (x : ι → R) (hi : ∀ i, x i ∈ I) (f : (ι → R) →ₗ[R] (ι' → R)) (i : ι') : f x i ∈ I := begin rw pi_eq_sum_univ x, simp only [finset.sum_apply, smul_eq_mul, linear_map.map_sum, pi.smul_apply, linear_map.map_smul], exact I.sum_mem (λ j hj, I.mul_mem_right _ (hi j)) end end pi section chinese_remainder variables {ι : Type v} theorem exists_sub_one_mem_and_mem (s : finset ι) {f : ι → ideal R} (hf : ∀ i ∈ s, ∀ j ∈ s, i ≠ j → f i ⊔ f j = ⊤) (i : ι) (his : i ∈ s) : ∃ r : R, r - 1 ∈ f i ∧ ∀ j ∈ s, j ≠ i → r ∈ f j := begin have : ∀ j ∈ s, j ≠ i → ∃ r : R, ∃ H : r - 1 ∈ f i, r ∈ f j, { intros j hjs hji, specialize hf i his j hjs hji.symm, rw [eq_top_iff_one, submodule.mem_sup] at hf, rcases hf with ⟨r, hri, s, hsj, hrs⟩, refine ⟨1 - r, _, _⟩, { rw [sub_right_comm, sub_self, zero_sub], exact (f i).neg_mem hri }, { rw [← hrs, add_sub_cancel'], exact hsj } }, classical, have : ∃ g : ι → R, (∀ j, g j - 1 ∈ f i) ∧ ∀ j ∈ s, j ≠ i → g j ∈ f j, { choose g hg1 hg2, refine ⟨λ j, if H : j ∈ s ∧ j ≠ i then g j H.1 H.2 else 1, λ j, _, λ j, _⟩, { split_ifs with h, { apply hg1 }, rw sub_self, exact (f i).zero_mem }, { intros hjs hji, rw dif_pos, { apply hg2 }, exact ⟨hjs, hji⟩ } }, rcases this with ⟨g, hgi, hgj⟩, use (∏ x in s.erase i, g x), split, { rw [← quotient.eq, ring_hom.map_one, ring_hom.map_prod], apply finset.prod_eq_one, intros, rw [← ring_hom.map_one, quotient.eq], apply hgi }, intros j hjs hji, rw [← quotient.eq_zero_iff_mem, ring_hom.map_prod], refine finset.prod_eq_zero (finset.mem_erase_of_ne_of_mem hji hjs) _, rw quotient.eq_zero_iff_mem, exact hgj j hjs hji end theorem exists_sub_mem [fintype ι] {f : ι → ideal R} (hf : ∀ i j, i ≠ j → f i ⊔ f j = ⊤) (g : ι → R) : ∃ r : R, ∀ i, r - g i ∈ f i := begin have : ∃ φ : ι → R, (∀ i, φ i - 1 ∈ f i) ∧ (∀ i j, i ≠ j → φ i ∈ f j), { have := exists_sub_one_mem_and_mem (finset.univ : finset ι) (λ i _ j _ hij, hf i j hij), choose φ hφ, existsi λ i, φ i (finset.mem_univ i), exact ⟨λ i, (hφ i _).1, λ i j hij, (hφ i _).2 j (finset.mem_univ j) hij.symm⟩ }, rcases this with ⟨φ, hφ1, hφ2⟩, use ∑ i, g i * φ i, intros i, rw [← quotient.eq, ring_hom.map_sum], refine eq.trans (finset.sum_eq_single i _ _) _, { intros j _ hji, rw quotient.eq_zero_iff_mem, exact (f i).mul_mem_left _ (hφ2 j i hji) }, { intros hi, exact (hi $ finset.mem_univ i).elim }, specialize hφ1 i, rw [← quotient.eq, ring_hom.map_one] at hφ1, rw [ring_hom.map_mul, hφ1, mul_one] end /-- The homomorphism from `R/(⋂ i, f i)` to `∏ i, (R / f i)` featured in the Chinese Remainder Theorem. It is bijective if the ideals `f i` are comaximal. -/ def quotient_inf_to_pi_quotient (f : ι → ideal R) : R ⧸ (⨅ i, f i) →+* Π i, R ⧸ f i := quotient.lift (⨅ i, f i) (pi.ring_hom (λ i : ι, (quotient.mk (f i) : _))) $ λ r hr, begin rw submodule.mem_infi at hr, ext i, exact quotient.eq_zero_iff_mem.2 (hr i) end theorem quotient_inf_to_pi_quotient_bijective [fintype ι] {f : ι → ideal R} (hf : ∀ i j, i ≠ j → f i ⊔ f j = ⊤) : function.bijective (quotient_inf_to_pi_quotient f) := ⟨λ x y, quotient.induction_on₂' x y $ λ r s hrs, quotient.eq.2 $ (submodule.mem_infi _).2 $ λ i, quotient.eq.1 $ show quotient_inf_to_pi_quotient f (quotient.mk' r) i = _, by rw hrs; refl, λ g, let ⟨r, hr⟩ := exists_sub_mem hf (λ i, quotient.out' (g i)) in ⟨quotient.mk _ r, funext $ λ i, quotient.out_eq' (g i) ▸ quotient.eq.2 (hr i)⟩⟩ /-- Chinese Remainder Theorem. Eisenbud Ex.2.6. Similar to Atiyah-Macdonald 1.10 and Stacks 00DT -/ noncomputable def quotient_inf_ring_equiv_pi_quotient [fintype ι] (f : ι → ideal R) (hf : ∀ i j, i ≠ j → f i ⊔ f j = ⊤) : R ⧸ (⨅ i, f i) ≃+* Π i, R ⧸ f i := { .. equiv.of_bijective _ (quotient_inf_to_pi_quotient_bijective hf), .. quotient_inf_to_pi_quotient f } end chinese_remainder end ideal
cb73b82fc71b73d6825107a936c7b9816dd5d13f
e0b0b1648286e442507eb62344760d5cd8d13f2d
/tests/lean/run/500_lean3.lean
6e469ac78fb8b8e003725007a572a7b3543f7967
[ "Apache-2.0" ]
permissive
MULXCODE/lean4
743ed389e05e26e09c6a11d24607ad5a697db39b
4675817a9e89824eca37192364cd47a4027c6437
refs/heads/master
1,682,231,879,857
1,620,423,501,000
1,620,423,501,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
126
lean
example (foo bar : OptionM Nat) : False := by have do { let x ← bar; foo } = bar >>= fun x => foo from rfl admit done
137eafaf9b253372e8a056302fce70aaf51ed508
9c1ad797ec8a5eddb37d34806c543602d9a6bf70
/examples/semigroups/semigroups.lean
9886276110e4d30ce97b622a0e2d8bb974de4b17
[]
no_license
timjb/lean-category-theory
816eefc3a0582c22c05f4ee1c57ed04e57c0982f
12916cce261d08bb8740bc85e0175b75fb2a60f4
refs/heads/master
1,611,078,926,765
1,492,080,000,000
1,492,080,000,000
88,348,246
0
0
null
1,492,262,499,000
1,492,262,498,000
null
UTF-8
Lean
false
false
1,987
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import ...monoidal_categories.braided_monoidal_category namespace tqft.categories.examples.semigroups universe variables u open tqft.categories set_option pp.universes true structure semigroup_morphism { α β : Type u } ( s : semigroup α ) ( t: semigroup β ) := (map: α → β) (multiplicative : ∀ x y : α, map(x * y) = map(x) * map(y)) attribute [simp] semigroup_morphism.multiplicative instance monoid_semigroup_to_map { α β : Type u } { s : semigroup α } { t: semigroup β } : has_coe_to_fun (semigroup_morphism s t) := { F := λ f, Π x : α, β, coe := semigroup_morphism.map } @[reducible] definition semigroup_identity { α : Type u } ( s: semigroup α ) : semigroup_morphism s s := ⟨ id, ♮ ⟩ @[reducible] definition semigroup_morphism_composition { α β γ : Type u } { s: semigroup α } { t: semigroup β } { u: semigroup γ} ( f: semigroup_morphism s t ) ( g: semigroup_morphism t u ) : semigroup_morphism s u := { map := λ x, g (f x), multiplicative := ♮ } @[pointwise] lemma semigroup_morphism_pointwise_equality { α β : Type u } { s : semigroup α } { t: semigroup β } ( f g : semigroup_morphism s t ) ( w : ∀ x : α, f x = g x) : f = g := begin induction f with fc, induction g with gc, have hc : fc = gc, from funext w, by subst hc end @[reducible] definition CategoryOfSemigroups : Category := { Obj := Σ α : Type u, semigroup α, Hom := λ s t, semigroup_morphism s.2 t.2, identity := λ s, semigroup_identity s.2, compose := λ _ _ _ f g, semigroup_morphism_composition f g, left_identity := ♯, right_identity := ♯, associativity := ♮ } definition trivial_semigroup: semigroup punit := { mul := λ _ _, punit.star, mul_assoc := ♮ } end tqft.categories.examples.semigroups
88d022843e03a491134145d719db30918ee4ca7e
137c667471a40116a7afd7261f030b30180468c2
/src/linear_algebra/matrix/nonsingular_inverse.lean
a1f495e7059ce9f983f9f66123142c1b5420764c
[ "Apache-2.0" ]
permissive
bragadeesh153/mathlib
46bf814cfb1eecb34b5d1549b9117dc60f657792
b577bb2cd1f96eb47031878256856020b76f73cd
refs/heads/master
1,687,435,188,334
1,626,384,207,000
1,626,384,207,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
20,673
lean
/- Copyright (c) 2019 Tim Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tim Baanen, Lu-Ming Zhang -/ import algebra.associated import linear_algebra.matrix.determinant import tactic.linarith import tactic.ring_exp /-! # Nonsingular inverses In this file, we define an inverse for square matrices of invertible determinant. For matrices that are not square or not of full rank, there is a more general notion of pseudoinverses which we do not consider here. The definition of inverse used in this file is the adjugate divided by the determinant. The adjugate is calculated with Cramer's rule, which we introduce first. The vectors returned by Cramer's rule are given by the linear map `cramer`, which sends a matrix `A` and vector `b` to the vector consisting of the determinant of replacing the `i`th column of `A` with `b` at index `i` (written as `(A.update_column i b).det`). Using Cramer's rule, we can compute for each matrix `A` the matrix `adjugate A`. The entries of the adjugate are the determinants of each minor of `A`. Instead of defining a minor to be `A` with row `i` and column `j` deleted, we replace the `i`th row of `A` with the `j`th basis vector; this has the same determinant as the minor but more importantly equals Cramer's rule applied to `A` and the `j`th basis vector, simplifying the subsequent proofs. We prove the adjugate behaves like `det A • A⁻¹`. Finally, we show that dividing the adjugate by `det A` (if possible), giving a matrix `nonsing_inv A`, will result in a multiplicative inverse to `A`. ## References * https://en.wikipedia.org/wiki/Cramer's_rule#Finding_inverse_matrix ## Tags matrix inverse, cramer, cramer's rule, adjugate -/ namespace matrix universes u v variables {n : Type u} [decidable_eq n] [fintype n] {α : Type v} [comm_ring α] open_locale matrix big_operators open equiv equiv.perm finset section cramer /-! ### `cramer` section Introduce the linear map `cramer` with values defined by `cramer_map`. After defining `cramer_map` and showing it is linear, we will restrict our proofs to using `cramer`. -/ variables (A : matrix n n α) (b : n → α) /-- `cramer_map A b i` is the determinant of the matrix `A` with column `i` replaced with `b`, and thus `cramer_map A b` is the vector output by Cramer's rule on `A` and `b`. If `A ⬝ x = b` has a unique solution in `x`, `cramer_map A` sends the vector `b` to `A.det • x`. Otherwise, the outcome of `cramer_map` is well-defined but not necessarily useful. -/ def cramer_map (i : n) : α := (A.update_column i b).det lemma cramer_map_is_linear (i : n) : is_linear_map α (λ b, cramer_map A b i) := { map_add := det_update_column_add _ _, map_smul := det_update_column_smul _ _ } lemma cramer_is_linear : is_linear_map α (cramer_map A) := begin split; intros; ext i, { apply (cramer_map_is_linear A i).1 }, { apply (cramer_map_is_linear A i).2 } end /-- `cramer A b i` is the determinant of the matrix `A` with column `i` replaced with `b`, and thus `cramer A b` is the vector output by Cramer's rule on `A` and `b`. If `A ⬝ x = b` has a unique solution in `x`, `cramer A` sends the vector `b` to `A.det • x`. Otherwise, the outcome of `cramer` is well-defined but not necessarily useful. -/ def cramer (A : matrix n n α) : (n → α) →ₗ[α] (n → α) := is_linear_map.mk' (cramer_map A) (cramer_is_linear A) lemma cramer_apply (i : n) : cramer A b i = (A.update_column i b).det := rfl lemma cramer_transpose_row_self (i : n) : Aᵀ.cramer (A i) = λ j, ite (i = j) A.det 0 := begin ext j, rw cramer_apply, by_cases h : i = j, { -- i = j: this entry should be `A.det` rw [update_column_transpose, det_transpose], simp [update_row, h], }, { -- i ≠ j: this entry should be 0 rw [if_neg h, update_column_transpose, det_transpose], apply det_zero_of_row_eq h, rw [update_row_self, update_row_ne], apply h } end /-- Use linearity of `cramer` to take it out of a summation. -/ lemma sum_cramer {β} (s : finset β) (f : β → n → α) : ∑ x in s, cramer A (f x) = cramer A (∑ x in s, f x) := (linear_map.map_sum (cramer A)).symm /-- Use linearity of `cramer` and vector evaluation to take `cramer A _ i` out of a summation. -/ lemma sum_cramer_apply {β} (s : finset β) (f : n → β → α) (i : n) : ∑ x in s, cramer A (λ j, f j x) i = cramer A (λ (j : n), ∑ x in s, f j x) i := calc ∑ x in s, cramer A (λ j, f j x) i = (∑ x in s, cramer A (λ j, f j x)) i : (finset.sum_apply i s _).symm ... = cramer A (λ (j : n), ∑ x in s, f j x) i : by { rw [sum_cramer, cramer_apply], congr' with j, apply finset.sum_apply } end cramer section adjugate /-! ### `adjugate` section Define the `adjugate` matrix and a few equations. These will hold for any matrix over a commutative ring, while the `inv` section is specifically for invertible matrices. -/ /-- The adjugate matrix is the transpose of the cofactor matrix. Typically, the cofactor matrix is defined by taking the determinant of minors, i.e. the matrix with a row and column removed. However, the proof of `mul_adjugate` becomes a lot easier if we define the minor as replacing a column with a basis vector, since it allows us to use facts about the `cramer` map. -/ def adjugate (A : matrix n n α) : matrix n n α := λ i, cramer Aᵀ (λ j, if i = j then 1 else 0) lemma adjugate_def (A : matrix n n α) : adjugate A = λ i, cramer Aᵀ (λ j, if i = j then 1 else 0) := rfl lemma adjugate_apply (A : matrix n n α) (i j : n) : adjugate A i j = (A.update_row j (λ j, if i = j then 1 else 0)).det := by { rw adjugate_def, simp only, rw [cramer_apply, update_column_transpose, det_transpose], } lemma adjugate_transpose (A : matrix n n α) : (adjugate A)ᵀ = adjugate (Aᵀ) := begin ext i j, rw [transpose_apply, adjugate_apply, adjugate_apply, update_row_transpose, det_transpose], rw [det_apply', det_apply'], apply finset.sum_congr rfl, intros σ _, congr' 1, by_cases i = σ j, { -- Everything except `(i , j)` (= `(σ j , j)`) is given by A, and the rest is a single `1`. congr; ext j', have := (@equiv.injective _ _ σ j j' : σ j = σ j' → j = j'), rw [update_row_apply, update_column_apply], finish }, { -- Otherwise, we need to show that there is a `0` somewhere in the product. have : (∏ j' : n, update_column A j (λ (i' : n), ite (i = i') 1 0) (σ j') j') = 0, { apply prod_eq_zero (mem_univ j), rw [update_column_self], exact if_neg h }, rw this, apply prod_eq_zero (mem_univ (σ⁻¹ i)), erw [apply_symm_apply σ i, update_row_self], apply if_neg, intro h', exact h ((symm_apply_eq σ).mp h'.symm) } end /-- Since the map `b ↦ cramer A b` is linear in `b`, it must be multiplication by some matrix. This matrix is `A.adjugate`. -/ lemma cramer_eq_adjugate_mul_vec (A : matrix n n α) (b : n → α) : cramer A b = A.adjugate.mul_vec b := begin nth_rewrite 1 ← A.transpose_transpose, rw [← adjugate_transpose, adjugate_def], have : b = ∑ i, (b i) • (λ j, if i = j then 1 else 0), { ext i, simp, }, rw this, ext k, simp [mul_vec, dot_product, mul_comm], end lemma mul_adjugate_apply (A : matrix n n α) (i j k) : A i k * adjugate A k j = cramer Aᵀ (λ j, if k = j then A i k else 0) j := begin erw [←smul_eq_mul, ←pi.smul_apply, ←linear_map.map_smul], congr' with l, rw [pi.smul_apply, smul_eq_mul, mul_boole], end lemma mul_adjugate (A : matrix n n α) : A ⬝ adjugate A = A.det • 1 := begin ext i j, rw [mul_apply, pi.smul_apply, pi.smul_apply, one_apply, smul_eq_mul, mul_boole], simp [mul_adjugate_apply, sum_cramer_apply, cramer_transpose_row_self], end lemma adjugate_mul (A : matrix n n α) : adjugate A ⬝ A = A.det • 1 := calc adjugate A ⬝ A = (Aᵀ ⬝ (adjugate Aᵀ))ᵀ : by rw [←adjugate_transpose, ←transpose_mul, transpose_transpose] ... = A.det • 1 : by rw [mul_adjugate (Aᵀ), det_transpose, transpose_smul, transpose_one] /-- `det_adjugate_of_cancel` is an auxiliary lemma for computing `(adjugate A).det`, used in `det_adjugate_eq_one` and `det_adjugate_of_is_unit`. The formula for the determinant of the adjugate of an `n` by `n` matrix `A` is in general `(adjugate A).det = A.det ^ (n - 1)`, but the proof differs in several cases. This lemma `det_adjugate_of_cancel` covers the case that `det A` cancels on the left of the equation `A.det * b = A.det ^ n`. -/ lemma det_adjugate_of_cancel {A : matrix n n α} (h : ∀ b, A.det * b = A.det ^ fintype.card n → b = A.det ^ (fintype.card n - 1)) : (adjugate A).det = A.det ^ (fintype.card n - 1) := h (adjugate A).det (calc A.det * (adjugate A).det = (A ⬝ adjugate A).det : (det_mul _ _).symm ... = A.det ^ fintype.card n : by simp [mul_adjugate]) lemma adjugate_eq_one_of_card_eq_one {A : matrix n n α} (h : fintype.card n = 1) : adjugate A = 1 := begin haveI : subsingleton n := fintype.card_le_one_iff_subsingleton.mp h.le, ext i j, simp [subsingleton.elim i j, adjugate_apply, det_eq_elem_of_card_eq_one h j], end @[simp] lemma adjugate_zero (h : 1 < fintype.card n) : adjugate (0 : matrix n n α) = 0 := begin ext i j, obtain ⟨j', hj'⟩ : ∃ j', j' ≠ j := fintype.exists_ne_of_one_lt_card h j, apply det_eq_zero_of_column_eq_zero j', intro j'', simp [update_column_ne hj'], end lemma det_adjugate_eq_one {A : matrix n n α} (h : A.det = 1) : (adjugate A).det = 1 := calc (adjugate A).det = A.det ^ (fintype.card n - 1) : det_adjugate_of_cancel (λ b hb, by simpa [h] using hb) ... = 1 : by rw [h, one_pow] /-- `det_adjugate_of_is_unit` gives the formula for `(adjugate A).det` if `A.det` has an inverse. The formula for the determinant of the adjugate of an `n` by `n` matrix `A` is in general `(adjugate A).det = A.det ^ (n - 1)`, but the proof differs in several cases. This lemma `det_adjugate_of_is_unit` covers the case that `det A` has an inverse. -/ lemma det_adjugate_of_is_unit {A : matrix n n α} (h : is_unit A.det) : (adjugate A).det = A.det ^ (fintype.card n - 1) := begin rcases is_unit_iff_exists_inv'.mp h with ⟨a, ha⟩, by_cases card_lt_zero : fintype.card n ≤ 0, { have h : fintype.card n = 0 := by linarith, simp [det_eq_one_of_card_eq_zero h] }, have zero_lt_card : 0 < fintype.card n := by linarith, have n_nonempty : nonempty n := fintype.card_pos_iff.mp zero_lt_card, by_cases card_lt_one : fintype.card n ≤ 1, { have h : fintype.card n = 1 := by linarith, simp [h, adjugate_eq_one_of_card_eq_one h] }, have one_lt_card : 1 < fintype.card n := by linarith, have zero_lt_card_sub_one : 0 < fintype.card n - 1 := (nat.sub_lt_sub_right_iff (refl 1)).mpr one_lt_card, apply det_adjugate_of_cancel, intros b hb, calc b = a * (det A ^ (fintype.card n - 1 + 1)) : by rw [←one_mul b, ←ha, mul_assoc, hb, nat.sub_add_cancel zero_lt_card] ... = a * det A * det A ^ (fintype.card n - 1) : by ring_exp ... = det A ^ (fintype.card n - 1) : by rw [ha, one_mul] end end adjugate section inv /-! ### `inv` section Defines the matrix `nonsing_inv A` and proves it is the inverse matrix of a square matrix `A` as long as `det A` has a multiplicative inverse. -/ variables (A : matrix n n α) (B : matrix n n α) open_locale classical lemma is_unit_det_transpose (h : is_unit A.det) : is_unit Aᵀ.det := by { rw det_transpose, exact h, } /-- The inverse of a square matrix, when it is invertible (and zero otherwise).-/ noncomputable def nonsing_inv : matrix n n α := if h : is_unit A.det then h.unit⁻¹ • A.adjugate else 0 noncomputable instance : has_inv (matrix n n α) := ⟨matrix.nonsing_inv⟩ lemma nonsing_inv_apply (h : is_unit A.det) : A⁻¹ = h.unit⁻¹ • A.adjugate := by { change A.nonsing_inv = _, dunfold nonsing_inv, simp only [dif_pos, h], } lemma transpose_nonsing_inv (h : is_unit A.det) : (A⁻¹)ᵀ = (Aᵀ)⁻¹ := begin have h' := A.is_unit_det_transpose h, have dets_eq : h.unit = h'.unit := units.ext (by rw [h.unit_spec, h'.unit_spec, det_transpose]), rw [A.nonsing_inv_apply h, Aᵀ.nonsing_inv_apply h', dets_eq, A.adjugate_transpose.symm], refl, end /-- The `nonsing_inv` of `A` is a right inverse. -/ @[simp] lemma mul_nonsing_inv (h : is_unit A.det) : A ⬝ A⁻¹ = 1 := by rw [A.nonsing_inv_apply h, units.smul_def, mul_smul, mul_adjugate, smul_smul, units.inv_mul_of_eq h.unit_spec, one_smul] /-- The `nonsing_inv` of `A` is a left inverse. -/ @[simp] lemma nonsing_inv_mul (h : is_unit A.det) : A⁻¹ ⬝ A = 1 := calc A⁻¹ ⬝ A = (Aᵀ ⬝ (Aᵀ)⁻¹)ᵀ : by { rw [transpose_mul, Aᵀ.transpose_nonsing_inv (A.is_unit_det_transpose h), transpose_transpose], } ... = 1ᵀ : by { rw Aᵀ.mul_nonsing_inv, exact A.is_unit_det_transpose h, } ... = 1 : transpose_one @[simp] lemma nonsing_inv_det (h : is_unit A.det) : A⁻¹.det * A.det = 1 := by rw [←det_mul, A.nonsing_inv_mul h, det_one] lemma is_unit_nonsing_inv_det (h : is_unit A.det) : is_unit A⁻¹.det := is_unit_of_mul_eq_one _ _ (A.nonsing_inv_det h) @[simp] lemma nonsing_inv_nonsing_inv (h : is_unit A.det) : (A⁻¹)⁻¹ = A := calc (A⁻¹)⁻¹ = 1 ⬝ (A⁻¹)⁻¹ : by rw matrix.one_mul ... = A ⬝ A⁻¹ ⬝ (A⁻¹)⁻¹ : by rw A.mul_nonsing_inv h ... = A : by { rw [matrix.mul_assoc, (A⁻¹).mul_nonsing_inv (A.is_unit_nonsing_inv_det h), matrix.mul_one], } /-- If `A.det` has a constructive inverse, produce one for `A`. -/ def invertible_of_det_invertible [invertible A.det] : invertible A := { inv_of := ⅟A.det • A.adjugate, mul_inv_of_self := by rw [mul_smul_comm, matrix.mul_eq_mul, mul_adjugate, smul_smul, inv_of_mul_self, one_smul], inv_of_mul_self := by rw [smul_mul_assoc, matrix.mul_eq_mul, adjugate_mul, smul_smul, inv_of_mul_self, one_smul] } /-- `A.det` is invertible if `A` has a left inverse. -/ def det_invertible_of_left_inverse (h : B ⬝ A = 1) : invertible A.det := { inv_of := B.det, mul_inv_of_self := by rw [mul_comm, ← det_mul, h, det_one], inv_of_mul_self := by rw [← det_mul, h, det_one] } /-- `A.det` is invertible if `A` has a right inverse. -/ def det_invertible_of_right_inverse (h : A ⬝ B = 1) : invertible A.det := { inv_of := B.det, mul_inv_of_self := by rw [← det_mul, h, det_one], inv_of_mul_self := by rw [mul_comm, ← det_mul, h, det_one] } /-- If `A` has a constructive inverse, produce one for `A.det`. -/ def det_invertible_of_invertible [invertible A] : invertible A.det := det_invertible_of_left_inverse A (⅟A) (inv_of_mul_self _) /-- Given a proof that `A.det` has a constructive inverse, lift `A` to `units (matrix n n α)`-/ def unit_of_det_invertible [invertible A.det] : units (matrix n n α) := @unit_of_invertible _ _ A (invertible_of_det_invertible A) /-- A matrix whose determinant is a unit is itself a unit. This is a noncomputable version of `matrix.units_of_det_invertible`, with the inverse defeq to `matrix.nonsing_inv`. -/ noncomputable def nonsing_inv_unit (h : is_unit A.det) : units (matrix n n α) := { val := A, inv := A⁻¹, val_inv := by { rw matrix.mul_eq_mul, apply A.mul_nonsing_inv h, }, inv_val := by { rw matrix.mul_eq_mul, apply A.nonsing_inv_mul h, } } lemma unit_of_det_invertible_eq_nonsing_inv_unit [invertible A.det] : unit_of_det_invertible A = nonsing_inv_unit A (is_unit_of_invertible _) := by { ext, refl } /-- When lowered to a prop, `matrix.det_invertible_of_invertible` and `matrix.invertible_of_det_invertible` form an `iff`. -/ lemma is_unit_iff_is_unit_det : is_unit A ↔ is_unit A.det := begin split; rintros ⟨x, hx⟩; refine @is_unit_of_invertible _ _ _ (id _), { haveI : invertible A := hx.rec x.invertible, apply det_invertible_of_invertible, }, { haveI : invertible A.det := hx.rec x.invertible, apply invertible_of_det_invertible, }, end /- `is_unit_of_invertible A` converts the "stronger" condition `invertible A` to proposition `is_unit A`. -/ /-- `matrix.is_unit_det_of_invertible` converts `invertible A` to `is_unit A.det`. -/ lemma is_unit_det_of_invertible [invertible A] : is_unit A.det := @is_unit_of_invertible _ _ _(det_invertible_of_invertible A) @[simp] lemma inv_eq_nonsing_inv_of_invertible [invertible A] : ⅟ A = A⁻¹ := begin suffices : is_unit A, { rw [←this.mul_left_inj, inv_of_mul_self, matrix.mul_eq_mul, nonsing_inv_mul], rwa ←is_unit_iff_is_unit_det }, exact is_unit_of_invertible _ end variables {A} {B} /- `is_unit.invertible` lifts the proposition `is_unit A` to a constructive inverse of `A`. -/ /-- "Lift" the proposition `is_unit A.det` to a constructive inverse of `A`. -/ noncomputable def invertible_of_is_unit_det (h : is_unit A.det) : invertible A := ⟨A⁻¹, nonsing_inv_mul A h, mul_nonsing_inv A h⟩ lemma is_unit_det_of_left_inverse (h : B ⬝ A = 1) : is_unit A.det := @is_unit_of_invertible _ _ _ (det_invertible_of_left_inverse _ _ h) lemma is_unit_det_of_right_inverse (h : A ⬝ B = 1) : is_unit A.det := @is_unit_of_invertible _ _ _ (det_invertible_of_right_inverse _ _ h) lemma nonsing_inv_left_right (h : A ⬝ B = 1) : B ⬝ A = 1 := begin have h' : is_unit B.det := is_unit_det_of_left_inverse h, calc B ⬝ A = (B ⬝ A) ⬝ (B ⬝ B⁻¹) : by simp only [h', matrix.mul_one, mul_nonsing_inv] ... = B ⬝ ((A ⬝ B) ⬝ B⁻¹) : by simp only [matrix.mul_assoc] ... = B ⬝ B⁻¹ : by simp only [h, matrix.one_mul] ... = 1 : mul_nonsing_inv B h', end lemma nonsing_inv_right_left (h : B ⬝ A = 1) : A ⬝ B = 1 := nonsing_inv_left_right h /-- If matrix A is left invertible, then its inverse equals its left inverse. -/ lemma inv_eq_left_inv (h : B ⬝ A = 1) : A⁻¹ = B := begin have h1 := (is_unit_det_of_left_inverse h), have h2 := matrix.invertible_of_is_unit_det h1, have := @inv_of_eq_left_inv (matrix n n α) (infer_instance) A B h2 h, simp* at *, end /-- If matrix A is right invertible, then its inverse equals its right inverse. -/ lemma inv_eq_right_inv (h : A ⬝ B = 1) : A⁻¹ = B := begin have h1 := (is_unit_det_of_right_inverse h), have h2 := matrix.invertible_of_is_unit_det h1, have := @inv_of_eq_right_inv (matrix n n α) (infer_instance) A B h2 h, simp* at *, end /-- We can construct an instance of invertible A if A has a left inverse. -/ def invertible_of_left_inverse (h: B ⬝ A = 1) : invertible A := ⟨B, h, nonsing_inv_right_left h⟩ /-- We can construct an instance of invertible A if A has a right inverse. -/ def invertible_of_right_inverse (h: A ⬝ B = 1) : invertible A := ⟨B, nonsing_inv_left_right h, h⟩ variables {C: matrix n n α} /-- The left inverse of matrix A is unique when existing. -/ lemma left_inv_eq_left_inv (h: B ⬝ A = 1) (g: C ⬝ A = 1) : B = C := by rw [←(inv_eq_left_inv h), ←(inv_eq_left_inv g)] /-- The right inverse of matrix A is unique when existing. -/ lemma right_inv_eq_right_inv (h: A ⬝ B = 1) (g: A ⬝ C = 1) : B = C := by rw [←(inv_eq_right_inv h), ←(inv_eq_right_inv g)] /-- The right inverse of matrix A equals the left inverse of A when they exist. -/ lemma right_inv_eq_left_inv (h: A ⬝ B = 1) (g: C ⬝ A = 1) : B = C := by rw [←(inv_eq_right_inv h), ←(inv_eq_left_inv g)] variable (A) @[simp] lemma mul_inv_of_invertible [invertible A] : A ⬝ A⁻¹ = 1 := mul_nonsing_inv A (is_unit_det_of_invertible A) @[simp] lemma inv_mul_of_invertible [invertible A] : A⁻¹ ⬝ A = 1 := nonsing_inv_mul A (is_unit_det_of_invertible A) end inv /-- One form of Cramer's rule -/ @[simp] lemma det_smul_inv_mul_vec_eq_cramer (A : matrix n n α) (b : n → α) (h : is_unit A.det) : A.det • A⁻¹.mul_vec b = cramer A b := begin rw [cramer_eq_adjugate_mul_vec, A.nonsing_inv_apply h, ← smul_mul_vec_assoc], conv_lhs { congr, congr, rw ← h.unit_spec, }, rw [←units.smul_def, smul_inv_smul], end /-- A stronger form of **Cramer's rule** that allows us to solve some instances of `A ⬝ x = b` even if the determinant is not a unit. A sufficient (but still not necessary) condition is that `A.det` divides `b`. -/ @[simp] lemma mul_vec_cramer (A : matrix n n α) (b : n → α) : A.mul_vec (cramer A b) = A.det • b := by rw [cramer_eq_adjugate_mul_vec, mul_vec_mul_vec, mul_adjugate, smul_mul_vec_assoc, mul_vec_one] end matrix
7df390dd9c56f8a2a2309a752ad308317ee6765f
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/analysis/normed_space/units.lean
dffbb8acf7043a121f3b2831fe478f0c5ed95f0c
[ "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
12,428
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.specific_limits import analysis.asymptotics.asymptotics /-! # The group of units of a complete normed ring This file contains the basic theory for the group of units (invertible elements) of a complete normed ring (Banach algebras being a notable special case). ## Main results The constructions `one_sub`, `add` and `unit_of_nearby` state, in varying forms, that perturbations of a unit are units. The latter two are not stated in their optimal form; more precise versions would use the spectral radius. The first main result is `is_open`: the group of units of a complete normed ring is an open subset of the ring. The function `inverse` (defined in `algebra.ring`), for a ring `R`, sends `a : R` to `a⁻¹` if `a` is a unit and 0 if not. The other major results of this file (notably `inverse_add`, `inverse_add_norm` and `inverse_add_norm_diff_nth_order`) cover the asymptotic properties of `inverse (x + t)` as `t → 0`. -/ noncomputable theory open_locale topological_space variables {R : Type*} [normed_ring R] [complete_space R] namespace units /-- In a complete normed ring, a perturbation of `1` by an element `t` of distance less than `1` from `1` is a unit. Here we construct its `units` structure. -/ def one_sub (t : R) (h : ∥t∥ < 1) : units R := { val := 1 - t, inv := ∑' n : ℕ, t ^ n, val_inv := mul_neg_geom_series t h, inv_val := geom_series_mul_neg t h } @[simp] lemma one_sub_coe (t : R) (h : ∥t∥ < 1) : ↑(one_sub t h) = 1 - t := rfl /-- In a complete normed ring, a perturbation of a unit `x` by an element `t` of distance less than `∥x⁻¹∥⁻¹` from `x` is a unit. Here we construct its `units` structure. -/ def add (x : units R) (t : R) (h : ∥t∥ < ∥(↑x⁻¹ : R)∥⁻¹) : units R := x * (units.one_sub (-(↑x⁻¹ * t)) begin nontriviality R using [zero_lt_one], have hpos : 0 < ∥(↑x⁻¹ : R)∥ := units.norm_pos x⁻¹, calc ∥-(↑x⁻¹ * t)∥ = ∥↑x⁻¹ * t∥ : by { rw norm_neg } ... ≤ ∥(↑x⁻¹ : R)∥ * ∥t∥ : norm_mul_le ↑x⁻¹ _ ... < ∥(↑x⁻¹ : R)∥ * ∥(↑x⁻¹ : R)∥⁻¹ : by nlinarith only [h, hpos] ... = 1 : mul_inv_cancel (ne_of_gt hpos) end) @[simp] lemma add_coe (x : units R) (t : R) (h : ∥t∥ < ∥(↑x⁻¹ : R)∥⁻¹) : ((x.add t h) : R) = x + t := by { unfold units.add, simp [mul_add] } /-- In a complete normed ring, an element `y` of distance less than `∥x⁻¹∥⁻¹` from `x` is a unit. Here we construct its `units` structure. -/ def unit_of_nearby (x : units R) (y : R) (h : ∥y - x∥ < ∥(↑x⁻¹ : R)∥⁻¹) : units R := x.add ((y : R) - x) h @[simp] lemma unit_of_nearby_coe (x : units R) (y : R) (h : ∥y - x∥ < ∥(↑x⁻¹ : R)∥⁻¹) : ↑(x.unit_of_nearby y h) = y := by { unfold units.unit_of_nearby, simp } /-- The group of units of a complete normed ring is an open subset of the ring. -/ protected lemma is_open : is_open {x : R | is_unit x} := begin nontriviality R, apply metric.is_open_iff.mpr, rintros x' ⟨x, rfl⟩, refine ⟨∥(↑x⁻¹ : R)∥⁻¹, inv_pos.mpr (units.norm_pos x⁻¹), _⟩, intros y hy, rw [metric.mem_ball, dist_eq_norm] at hy, exact ⟨x.unit_of_nearby y hy, unit_of_nearby_coe _ _ _⟩ end protected lemma nhds (x : units R) : {x : R | is_unit x} ∈ 𝓝 (x : R) := is_open.mem_nhds units.is_open x.is_unit end units namespace normed_ring open_locale classical big_operators open asymptotics filter metric finset ring lemma inverse_one_sub (t : R) (h : ∥t∥ < 1) : inverse (1 - t) = ↑(units.one_sub t h)⁻¹ := by rw [← inverse_unit (units.one_sub t h), units.one_sub_coe] /-- The formula `inverse (x + t) = inverse (1 + x⁻¹ * t) * x⁻¹` holds for `t` sufficiently small. -/ lemma inverse_add (x : units R) : ∀ᶠ t in (𝓝 0), inverse ((x : R) + t) = inverse (1 + ↑x⁻¹ * t) * ↑x⁻¹ := begin nontriviality R, rw [eventually_iff, metric.mem_nhds_iff], have hinv : 0 < ∥(↑x⁻¹ : R)∥⁻¹, by cancel_denoms, use [∥(↑x⁻¹ : R)∥⁻¹, hinv], intros t ht, simp only [mem_ball, dist_zero_right] at ht, have ht' : ∥-↑x⁻¹ * t∥ < 1, { refine lt_of_le_of_lt (norm_mul_le _ _) _, rw norm_neg, refine lt_of_lt_of_le (mul_lt_mul_of_pos_left ht x⁻¹.norm_pos) _, cancel_denoms }, have hright := inverse_one_sub (-↑x⁻¹ * t) ht', have hleft := inverse_unit (x.add t ht), simp only [← neg_mul_eq_neg_mul, sub_neg_eq_add] at hright, simp only [units.add_coe] at hleft, simp [hleft, hright, units.add] end lemma inverse_one_sub_nth_order (n : ℕ) : ∀ᶠ t in (𝓝 0), inverse ((1:R) - t) = (∑ i in range n, t ^ i) + (t ^ n) * inverse (1 - t) := begin simp only [eventually_iff, metric.mem_nhds_iff], use [1, by norm_num], intros t ht, simp only [mem_ball, dist_zero_right] at ht, simp only [inverse_one_sub t ht, set.mem_set_of_eq], have h : 1 = ((range n).sum (λ i, t ^ i)) * (units.one_sub t ht) + t ^ n, { simp only [units.one_sub_coe], rw [← geom_sum, geom_sum_mul_neg], simp }, rw [← one_mul ↑(units.one_sub t ht)⁻¹, h, add_mul], congr, { rw [mul_assoc, (units.one_sub t ht).mul_inv], simp }, { simp only [units.one_sub_coe], rw [← add_mul, ← geom_sum, geom_sum_mul_neg], simp } end /-- The formula `inverse (x + t) = (∑ i in range n, (- x⁻¹ * t) ^ i) * x⁻¹ + (- x⁻¹ * t) ^ n * inverse (x + t)` holds for `t` sufficiently small. -/ lemma inverse_add_nth_order (x : units R) (n : ℕ) : ∀ᶠ t in (𝓝 0), inverse ((x : R) + t) = (∑ i in range n, (- ↑x⁻¹ * t) ^ i) * ↑x⁻¹ + (- ↑x⁻¹ * t) ^ n * inverse (x + t) := begin refine (inverse_add x).mp _, have hzero : tendsto (λ (t : R), - ↑x⁻¹ * t) (𝓝 0) (𝓝 0), { convert ((mul_left_continuous (- (↑x⁻¹ : R))).tendsto 0).comp tendsto_id, simp }, refine (hzero.eventually (inverse_one_sub_nth_order n)).mp (eventually_of_forall _), simp only [neg_mul_eq_neg_mul_symm, sub_neg_eq_add], intros t h1 h2, have h := congr_arg (λ (a : R), a * ↑x⁻¹) h1, dsimp at h, convert h, rw [add_mul, mul_assoc], simp [h2.symm] end lemma inverse_one_sub_norm : is_O (λ t, inverse ((1:R) - t)) (λ t, (1:ℝ)) (𝓝 (0:R)) := begin simp only [is_O, is_O_with, eventually_iff, metric.mem_nhds_iff], refine ⟨∥(1:R)∥ + 1, (2:ℝ)⁻¹, by norm_num, _⟩, intros t ht, simp only [ball, dist_zero_right, set.mem_set_of_eq] at ht, have ht' : ∥t∥ < 1, { have : (2:ℝ)⁻¹ < 1 := by cancel_denoms, linarith }, simp only [inverse_one_sub t ht', norm_one, mul_one, set.mem_set_of_eq], change ∥∑' n : ℕ, t ^ n∥ ≤ _, have := normed_ring.tsum_geometric_of_norm_lt_1 t ht', have : (1 - ∥t∥)⁻¹ ≤ 2, { rw ← inv_inv' (2:ℝ), refine inv_le_inv_of_le (by norm_num) _, have : (2:ℝ)⁻¹ + (2:ℝ)⁻¹ = 1 := by ring, linarith }, linarith end /-- The function `λ t, inverse (x + t)` is O(1) as `t → 0`. -/ lemma inverse_add_norm (x : units R) : is_O (λ t, inverse (↑x + t)) (λ t, (1:ℝ)) (𝓝 (0:R)) := begin nontriviality R, simp only [is_O_iff, norm_one, mul_one], cases is_O_iff.mp (@inverse_one_sub_norm R _ _) with C hC, use C * ∥((x⁻¹:units R):R)∥, have hzero : tendsto (λ t, - (↑x⁻¹ : R) * t) (𝓝 0) (𝓝 0), { convert ((mul_left_continuous (-↑x⁻¹ : R)).tendsto 0).comp tendsto_id, simp }, refine (inverse_add x).mp ((hzero.eventually hC).mp (eventually_of_forall _)), intros t bound iden, rw iden, simp at bound, have hmul := norm_mul_le (inverse (1 + ↑x⁻¹ * t)) ↑x⁻¹, nlinarith [norm_nonneg (↑x⁻¹ : R)] end /-- The function `λ t, inverse (x + t) - (∑ i in range n, (- x⁻¹ * t) ^ i) * x⁻¹` is `O(t ^ n)` as `t → 0`. -/ lemma inverse_add_norm_diff_nth_order (x : units R) (n : ℕ) : is_O (λ (t : R), inverse (↑x + t) - (∑ i in range n, (- ↑x⁻¹ * t) ^ i) * ↑x⁻¹) (λ t, ∥t∥ ^ n) (𝓝 (0:R)) := begin by_cases h : n = 0, { simpa [h] using inverse_add_norm x }, have hn : 0 < n := nat.pos_of_ne_zero h, simp [is_O_iff], cases (is_O_iff.mp (inverse_add_norm x)) with C hC, use C * ∥(1:ℝ)∥ * ∥(↑x⁻¹ : R)∥ ^ n, have h : eventually_eq (𝓝 (0:R)) (λ t, inverse (↑x + t) - (∑ i in range n, (- ↑x⁻¹ * t) ^ i) * ↑x⁻¹) (λ t, ((- ↑x⁻¹ * t) ^ n) * inverse (x + t)), { refine (inverse_add_nth_order x n).mp (eventually_of_forall _), intros t ht, convert congr_arg (λ a, a - (range n).sum (pow (-↑x⁻¹ * t)) * ↑x⁻¹) ht, simp }, refine h.mp (hC.mp (eventually_of_forall _)), intros t _ hLHS, simp only [neg_mul_eq_neg_mul_symm] at hLHS, rw hLHS, refine le_trans (norm_mul_le _ _ ) _, have h' : ∥(-(↑x⁻¹ * t)) ^ n∥ ≤ ∥(↑x⁻¹ : R)∥ ^ n * ∥t∥ ^ n, { calc ∥(-(↑x⁻¹ * t)) ^ n∥ ≤ ∥(-(↑x⁻¹ * t))∥ ^ n : norm_pow_le' _ hn ... = ∥↑x⁻¹ * t∥ ^ n : by rw norm_neg ... ≤ (∥(↑x⁻¹ : R)∥ * ∥t∥) ^ n : _ ... = ∥(↑x⁻¹ : R)∥ ^ n * ∥t∥ ^ n : mul_pow _ _ n, exact pow_le_pow_of_le_left (norm_nonneg _) (norm_mul_le ↑x⁻¹ t) n }, have h'' : 0 ≤ ∥(↑x⁻¹ : R)∥ ^ n * ∥t∥ ^ n, { refine mul_nonneg _ _; exact pow_nonneg (norm_nonneg _) n }, nlinarith [norm_nonneg (inverse (↑x + t))], end /-- The function `λ t, inverse (x + t) - x⁻¹` is `O(t)` as `t → 0`. -/ lemma inverse_add_norm_diff_first_order (x : units R) : is_O (λ t, inverse (↑x + t) - ↑x⁻¹) (λ t, ∥t∥) (𝓝 (0:R)) := by { convert inverse_add_norm_diff_nth_order x 1; simp } /-- The function `λ t, inverse (x + t) - x⁻¹ + x⁻¹ * t * x⁻¹` is `O(t ^ 2)` as `t → 0`. -/ lemma inverse_add_norm_diff_second_order (x : units R) : is_O (λ t, inverse (↑x + t) - ↑x⁻¹ + ↑x⁻¹ * t * ↑x⁻¹) (λ t, ∥t∥ ^ 2) (𝓝 (0:R)) := begin convert inverse_add_norm_diff_nth_order x 2, ext t, simp only [range_succ, range_one, sum_insert, mem_singleton, sum_singleton, not_false_iff, one_ne_zero, pow_zero, add_mul], abel, simp end /-- The function `inverse` is continuous at each unit of `R`. -/ lemma inverse_continuous_at (x : units R) : continuous_at inverse (x : R) := begin have h_is_o : is_o (λ (t : R), ∥inverse (↑x + t) - ↑x⁻¹∥) (λ (t : R), (1:ℝ)) (𝓝 0), { refine is_o_norm_left.mpr ((inverse_add_norm_diff_first_order x).trans_is_o _), exact is_o_norm_left.mpr (is_o_id_const one_ne_zero) }, have h_lim : tendsto (λ (y:R), y - x) (𝓝 x) (𝓝 0), { refine tendsto_zero_iff_norm_tendsto_zero.mpr _, exact tendsto_iff_norm_tendsto_zero.mp tendsto_id }, simp only [continuous_at], rw [tendsto_iff_norm_tendsto_zero, inverse_unit], convert h_is_o.tendsto_0.comp h_lim, ext, simp end end normed_ring namespace units open opposite filter normed_ring /-- In a normed ring, the coercion from `units R` (equipped with the induced topology from the embedding in `R × R`) to `R` is an open map. -/ lemma is_open_map_coe : is_open_map (coe : units R → R) := begin rw is_open_map_iff_nhds_le, intros x s, rw [mem_map, mem_nhds_induced], rintros ⟨t, ht, hts⟩, obtain ⟨u, hu, v, hv, huvt⟩ : ∃ (u : set R), u ∈ 𝓝 ↑x ∧ ∃ (v : set Rᵒᵖ), v ∈ 𝓝 (opposite.op ↑x⁻¹) ∧ u.prod v ⊆ t, { simpa [embed_product, mem_nhds_prod_iff] using ht }, have : u ∩ (op ∘ ring.inverse) ⁻¹' v ∩ (set.range (coe : units R → R)) ∈ 𝓝 ↑x, { refine inter_mem_sets (inter_mem_sets hu _) (units.nhds x), refine (continuous_op.continuous_at.comp (inverse_continuous_at x)).preimage_mem_nhds _, simpa using hv }, refine mem_sets_of_superset this _, rintros _ ⟨⟨huy, hvy⟩, ⟨y, rfl⟩⟩, have : embed_product R y ∈ u.prod v := ⟨huy, by simpa using hvy⟩, simpa using hts (huvt this) end /-- In a normed ring, the coercion from `units R` (equipped with the induced topology from the embedding in `R × R`) to `R` is an open embedding. -/ lemma open_embedding_coe : open_embedding (coe : units R → R) := open_embedding_of_continuous_injective_open continuous_coe ext is_open_map_coe end units
850c98db6728b7b8c7753bdfbf472e69a65db6af
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/topology/algebra/ring.lean
108b13ca44a97a6c69318656f89565207e28b109
[ "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
7,420
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl Theory of topological rings. -/ import topology.algebra.group import ring_theory.ideal.basic import ring_theory.subring import algebra.ring.prod /-! # Topological (semi)rings A topological (semi)ring is a (semi)ring equipped with a topology such that all operations are continuous. Besides this definition, this file proves that the topological closure of a subring (resp. an ideal) is a subring (resp. an ideal) and defines products and quotients of topological (semi)rings. ## Main Results - `subring.topological_closure`/`subsemiring.topological_closure`: the topological closure of a `subring`/`subsemiring` is itself a `sub(semi)ring`. - `prod_ring`/`prod_semiring`: The product of two topological (semi)rings. - `ideal.closure`: The closure of an ideal is an ideal. - `topological_ring_quotient`: The quotient of a topological ring by an ideal is a topological ring. -/ open classical set filter topological_space open_locale classical section topological_ring variables (α : Type*) /-- A topological semiring is a semiring where addition and multiplication are continuous. -/ class topological_semiring [topological_space α] [semiring α] extends has_continuous_add α, has_continuous_mul α : Prop section variables {α} [topological_space α] [semiring α] [topological_semiring α] /-- The (topological-space) closure of a subsemiring of a topological semiring is itself a subsemiring. -/ def subsemiring.topological_closure (s : subsemiring α) : subsemiring α := { carrier := closure (s : set α), ..(s.to_submonoid.topological_closure), ..(s.to_add_submonoid.topological_closure ) } @[simp] lemma subsemiring.topological_closure_coe (s : subsemiring α) : (s.topological_closure : set α) = closure (s : set α) := rfl instance subsemiring.topological_closure_topological_semiring (s : subsemiring α) : topological_semiring (s.topological_closure) := { ..s.to_add_submonoid.topological_closure_has_continuous_add, ..s.to_submonoid.topological_closure_has_continuous_mul } lemma subsemiring.subring_topological_closure (s : subsemiring α) : s ≤ s.topological_closure := subset_closure lemma subsemiring.is_closed_topological_closure (s : subsemiring α) : is_closed (s.topological_closure : set α) := by convert is_closed_closure lemma subsemiring.topological_closure_minimal (s : subsemiring α) {t : subsemiring α} (h : s ≤ t) (ht : is_closed (t : set α)) : s.topological_closure ≤ t := closure_minimal h ht /-- The product topology on the cartesian product of two topological semirings makes the product into a topological semiring. -/ instance prod_semiring {β : Type*} [semiring β] [topological_space β] [topological_semiring β] : topological_semiring (α × β) := {} end /-- A topological ring is a ring where the ring operations are continuous. -/ class topological_ring [topological_space α] [ring α] extends has_continuous_add α, has_continuous_mul α : Prop := (continuous_neg : continuous (λa:α, -a)) variables {α} [ring α] [topological_space α] section variables [t : topological_ring α] @[priority 100] -- see Note [lower instance priority] instance topological_ring.to_topological_semiring : topological_semiring α := {..t} @[priority 100] -- see Note [lower instance priority] instance topological_ring.to_topological_add_group : topological_add_group α := {..t} end variables [topological_ring α] /-- The product topology on the cartesian product of two topological rings makes the product into a topological ring. -/ instance prod_ring {β : Type*} [ring β] [topological_space β] [topological_ring β] : topological_ring (α × β) := { continuous_neg := continuous_neg } /-- In a topological ring, the left-multiplication `add_monoid_hom` is continuous. -/ lemma mul_left_continuous (x : α) : continuous (add_monoid_hom.mul_left x) := continuous_const.mul continuous_id /-- In a topological ring, the right-multiplication `add_monoid_hom` is continuous. -/ lemma mul_right_continuous (x : α) : continuous (add_monoid_hom.mul_right x) := continuous_id.mul continuous_const /-- The (topological-space) closure of a subring of a topological semiring is itself a subring. -/ def subring.topological_closure (S : subring α) : subring α := { carrier := closure (S : set α), ..(S.to_submonoid.topological_closure), ..(S.to_add_subgroup.topological_closure) } instance subring.topological_closure_topological_ring (s : subring α) : topological_ring (s.topological_closure) := { ..s.to_add_subgroup.topological_closure_topological_add_group, ..s.to_submonoid.topological_closure_has_continuous_mul } lemma subring.subring_topological_closure (s : subring α) : s ≤ s.topological_closure := subset_closure lemma subring.is_closed_topological_closure (s : subring α) : is_closed (s.topological_closure : set α) := by convert is_closed_closure lemma subring.topological_closure_minimal (s : subring α) {t : subring α} (h : s ≤ t) (ht : is_closed (t : set α)) : s.topological_closure ≤ t := closure_minimal h ht end topological_ring section topological_comm_ring variables {α : Type*} [topological_space α] [comm_ring α] [topological_ring α] /-- The closure of an ideal in a topological ring as an ideal. -/ def ideal.closure (S : ideal α) : ideal α := { carrier := closure S, smul_mem' := λ c x hx, map_mem_closure (mul_left_continuous _) hx $ λ a, S.mul_mem_left c, ..(add_submonoid.topological_closure S.to_add_submonoid) } @[simp] lemma ideal.coe_closure (S : ideal α) : (S.closure : set α) = closure S := rfl end topological_comm_ring section topological_ring variables {α : Type*} [topological_space α] [comm_ring α] (N : ideal α) open ideal.quotient instance topological_ring_quotient_topology : topological_space N.quotient := by dunfold ideal.quotient submodule.quotient; apply_instance -- note for the reader: in the following, `mk` is `ideal.quotient.mk`, the canonical map `R → R/I`. variable [topological_ring α] lemma quotient_ring.is_open_map_coe : is_open_map (mk N) := begin intros s s_op, change is_open (mk N ⁻¹' (mk N '' s)), rw quotient_ring_saturate, exact is_open_Union (λ ⟨n, _⟩, is_open_map_add_left n s s_op) end lemma quotient_ring.quotient_map_coe_coe : quotient_map (λ p : α × α, (mk N p.1, mk N p.2)) := is_open_map.to_quotient_map ((quotient_ring.is_open_map_coe N).prod (quotient_ring.is_open_map_coe N)) ((continuous_quot_mk.comp continuous_fst).prod_mk (continuous_quot_mk.comp continuous_snd)) (by rintro ⟨⟨x⟩, ⟨y⟩⟩; exact ⟨(x, y), rfl⟩) instance topological_ring_quotient : topological_ring N.quotient := { continuous_add := have cont : continuous (mk N ∘ (λ (p : α × α), p.fst + p.snd)) := continuous_quot_mk.comp continuous_add, (quotient_map.continuous_iff (quotient_ring.quotient_map_coe_coe N)).mpr cont, continuous_neg := by convert continuous_quotient_lift _ (continuous_quot_mk.comp continuous_neg); apply_instance, continuous_mul := have cont : continuous (mk N ∘ (λ (p : α × α), p.fst * p.snd)) := continuous_quot_mk.comp continuous_mul, (quotient_map.continuous_iff (quotient_ring.quotient_map_coe_coe N)).mpr cont } end topological_ring
053b428a9176d3d045126d12285bd0f15cef7f59
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/library/data/list/basic.lean
a8a8867ee6cf9c4645dc2fb4e6f0344543256435
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
26,372
lean
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn Basic properties of lists. -/ import logic tools.helper_tactics data.nat.order data.nat.sub open eq.ops nat prod function option inductive list (T : Type) : Type := | nil {} : list T | cons : T → list T → list T protected definition list.is_inhabited [instance] (A : Type) : inhabited (list A) := inhabited.mk list.nil namespace list notation h :: t := cons h t notation `[` l:(foldr `, ` (h t, cons h t) nil `]`) := l variable {T : Type} lemma cons_ne_nil [simp] (a : T) (l : list T) : a::l ≠ [] := by contradiction lemma head_eq_of_cons_eq {A : Type} {h₁ h₂ : A} {t₁ t₂ : list A} : (h₁::t₁) = (h₂::t₂) → h₁ = h₂ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pheq) lemma tail_eq_of_cons_eq {A : Type} {h₁ h₂ : A} {t₁ t₂ : list A} : (h₁::t₁) = (h₂::t₂) → t₁ = t₂ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pteq) lemma cons_inj {A : Type} {a : A} : injective (cons a) := take l₁ l₂, assume Pe, tail_eq_of_cons_eq Pe /- append -/ definition append : list T → list T → list T | [] l := l | (h :: s) t := h :: (append s t) notation l₁ ++ l₂ := append l₁ l₂ theorem append_nil_left [simp] (t : list T) : [] ++ t = t := rfl theorem append_cons [simp] (x : T) (s t : list T) : (x::s) ++ t = x::(s ++ t) := rfl theorem append_nil_right [simp] : ∀ (t : list T), t ++ [] = t := by rec_inst_simp theorem append.assoc [simp] : ∀ (s t u : list T), s ++ t ++ u = s ++ (t ++ u) := by rec_inst_simp /- length -/ definition length : list T → nat | [] := 0 | (a :: l) := length l + 1 theorem length_nil [simp] : length (@nil T) = 0 := rfl theorem length_cons [simp] (x : T) (t : list T) : length (x::t) = length t + 1 := rfl theorem length_append [simp] : ∀ (s t : list T), length (s ++ t) = length s + length t := by rec_inst_simp theorem eq_nil_of_length_eq_zero : ∀ {l : list T}, length l = 0 → l = [] | [] H := rfl | (a::s) H := by contradiction theorem ne_nil_of_length_eq_succ : ∀ {l : list T} {n : nat}, length l = succ n → l ≠ [] | [] n h := by contradiction | (a::l) n h := by contradiction /- concat -/ definition concat : Π (x : T), list T → list T | a [] := [a] | a (b :: l) := b :: concat a l theorem concat_nil [simp] (x : T) : concat x [] = [x] := rfl theorem concat_cons [simp] (x y : T) (l : list T) : concat x (y::l) = y::(concat x l) := rfl theorem concat_eq_append [simp] (a : T) : ∀ (l : list T), concat a l = l ++ [a] := by rec_inst_simp theorem concat_ne_nil [simp] (a : T) : ∀ (l : list T), concat a l ≠ [] := by intro l; induction l; repeat contradiction theorem length_concat [simp] (a : T) : ∀ (l : list T), length (concat a l) = length l + 1 := by rec_inst_simp theorem concat_append [simp] (a : T) : ∀ (l₁ l₂ : list T), concat a l₁ ++ l₂ = l₁ ++ a :: l₂ := by rec_inst_simp theorem append_concat (a : T) : ∀(l₁ l₂ : list T), l₁ ++ concat a l₂ = concat a (l₁ ++ l₂) := by rec_inst_simp /- last -/ definition last : Π l : list T, l ≠ [] → T | [] h := absurd rfl h | [a] h := a | (a₁::a₂::l) h := last (a₂::l) !cons_ne_nil lemma last_singleton [simp] (a : T) (h : [a] ≠ []) : last [a] h = a := rfl lemma last_cons_cons [simp] (a₁ a₂ : T) (l : list T) (h : a₁::a₂::l ≠ []) : last (a₁::a₂::l) h = last (a₂::l) !cons_ne_nil := rfl theorem last_congr {l₁ l₂ : list T} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) : last l₁ h₁ = last l₂ h₂ := by subst l₁ theorem last_concat [simp] {x : T} : ∀ {l : list T} (h : concat x l ≠ []), last (concat x l) h = x := by rec_simp -- add_rewrite append_nil append_cons /- reverse -/ definition reverse : list T → list T | [] := [] | (a :: l) := concat a (reverse l) theorem reverse_nil [simp] : reverse (@nil T) = [] := rfl theorem reverse_cons [simp] (x : T) (l : list T) : reverse (x::l) = concat x (reverse l) := rfl theorem reverse_singleton [simp] (x : T) : reverse [x] = [x] := rfl theorem reverse_append [simp] : ∀ (s t : list T), reverse (s ++ t) = (reverse t) ++ (reverse s) := by rec_inst_simp theorem reverse_reverse [simp] : ∀ (l : list T), reverse (reverse l) = l := by rec_inst_simp theorem concat_eq_reverse_cons (x : T) (l : list T) : concat x l = reverse (x :: reverse l) := by inst_simp theorem length_reverse : ∀ (l : list T), length (reverse l) = length l := by rec_inst_simp /- head and tail -/ definition head [h : inhabited T] : list T → T | [] := arbitrary T | (a :: l) := a theorem head_cons [simp] [h : inhabited T] (a : T) (l : list T) : head (a::l) = a := rfl theorem head_append [simp] [h : inhabited T] (t : list T) : ∀ {s : list T}, s ≠ [] → head (s ++ t) = head s := by rec_inst_simp definition tail : list T → list T | [] := [] | (a :: l) := l theorem tail_nil [simp] : tail (@nil T) = [] := rfl theorem tail_cons [simp] (a : T) (l : list T) : tail (a::l) = l := rfl theorem cons_head_tail [h : inhabited T] {l : list T} : l ≠ [] → (head l)::(tail l) = l := by rec_inst_simp /- list membership -/ definition mem : T → list T → Prop | a [] := false | a (b :: l) := a = b ∨ mem a l notation e ∈ s := mem e s notation e ∉ s := ¬ e ∈ s theorem mem_nil_iff (x : T) : x ∈ [] ↔ false := iff.rfl theorem not_mem_nil (x : T) : x ∉ [] := iff.mp !mem_nil_iff theorem mem_cons [simp] (x : T) (l : list T) : x ∈ x :: l := or.inl rfl theorem mem_cons_of_mem (y : T) {x : T} {l : list T} : x ∈ l → x ∈ y :: l := assume H, or.inr H theorem mem_cons_iff (x y : T) (l : list T) : x ∈ y::l ↔ (x = y ∨ x ∈ l) := iff.rfl theorem eq_or_mem_of_mem_cons {x y : T} {l : list T} : x ∈ y::l → x = y ∨ x ∈ l := assume h, h theorem mem_singleton {x a : T} : x ∈ [a] → x = a := suppose x ∈ [a], or.elim (eq_or_mem_of_mem_cons this) (suppose x = a, this) (suppose x ∈ [], absurd this !not_mem_nil) theorem mem_of_mem_cons_of_mem {a b : T} {l : list T} : a ∈ b::l → b ∈ l → a ∈ l := assume ainbl binl, or.elim (eq_or_mem_of_mem_cons ainbl) (suppose a = b, by substvars; exact binl) (suppose a ∈ l, this) theorem mem_or_mem_of_mem_append {x : T} {s t : list T} : x ∈ s ++ t → x ∈ s ∨ x ∈ t := list.induction_on s or.inr (take y s, assume IH : x ∈ s ++ t → x ∈ s ∨ x ∈ t, suppose x ∈ y::s ++ t, have x = y ∨ x ∈ s ++ t, from this, have x = y ∨ x ∈ s ∨ x ∈ t, from or_of_or_of_imp_right this IH, iff.elim_right or.assoc this) theorem mem_append_of_mem_or_mem {x : T} {s t : list T} : x ∈ s ∨ x ∈ t → x ∈ s ++ t := list.induction_on s (take H, or.elim H false.elim (assume H, H)) (take y s, assume IH : x ∈ s ∨ x ∈ t → x ∈ s ++ t, suppose x ∈ y::s ∨ x ∈ t, or.elim this (suppose x ∈ y::s, or.elim (eq_or_mem_of_mem_cons this) (suppose x = y, or.inl this) (suppose x ∈ s, or.inr (IH (or.inl this)))) (suppose x ∈ t, or.inr (IH (or.inr this)))) theorem mem_append_iff (x : T) (s t : list T) : x ∈ s ++ t ↔ x ∈ s ∨ x ∈ t := iff.intro mem_or_mem_of_mem_append mem_append_of_mem_or_mem theorem not_mem_of_not_mem_append_left {x : T} {s t : list T} : x ∉ s++t → x ∉ s := λ nxinst xins, absurd (mem_append_of_mem_or_mem (or.inl xins)) nxinst theorem not_mem_of_not_mem_append_right {x : T} {s t : list T} : x ∉ s++t → x ∉ t := λ nxinst xint, absurd (mem_append_of_mem_or_mem (or.inr xint)) nxinst theorem not_mem_append {x : T} {s t : list T} : x ∉ s → x ∉ t → x ∉ s++t := λ nxins nxint xinst, or.elim (mem_or_mem_of_mem_append xinst) (λ xins, by contradiction) (λ xint, by contradiction) lemma length_pos_of_mem {a : T} : ∀ {l : list T}, a ∈ l → 0 < length l | [] := assume Pinnil, by contradiction | (b::l) := assume Pin, !zero_lt_succ section local attribute mem [reducible] local attribute append [reducible] theorem mem_split {x : T} {l : list T} : x ∈ l → ∃s t : list T, l = s ++ (x::t) := list.induction_on l (suppose x ∈ [], false.elim (iff.elim_left !mem_nil_iff this)) (take y l, assume IH : x ∈ l → ∃s t : list T, l = s ++ (x::t), suppose x ∈ y::l, or.elim (eq_or_mem_of_mem_cons this) (suppose x = y, exists.intro [] (!exists.intro (this ▸ rfl))) (suppose x ∈ l, obtain s (H2 : ∃t : list T, l = s ++ (x::t)), from IH this, obtain t (H3 : l = s ++ (x::t)), from H2, have y :: l = (y::s) ++ (x::t), from H3 ▸ rfl, !exists.intro (!exists.intro this))) end theorem mem_append_left {a : T} {l₁ : list T} (l₂ : list T) : a ∈ l₁ → a ∈ l₁ ++ l₂ := assume ainl₁, mem_append_of_mem_or_mem (or.inl ainl₁) theorem mem_append_right {a : T} (l₁ : list T) {l₂ : list T} : a ∈ l₂ → a ∈ l₁ ++ l₂ := assume ainl₂, mem_append_of_mem_or_mem (or.inr ainl₂) definition decidable_mem [instance] [H : decidable_eq T] (x : T) (l : list T) : decidable (x ∈ l) := list.rec_on l (decidable.inr (not_of_iff_false !mem_nil_iff)) (take (h : T) (l : list T) (iH : decidable (x ∈ l)), show decidable (x ∈ h::l), from decidable.rec_on iH (assume Hp : x ∈ l, decidable.rec_on (H x h) (suppose x = h, decidable.inl (or.inl this)) (suppose x ≠ h, decidable.inl (or.inr Hp))) (suppose ¬x ∈ l, decidable.rec_on (H x h) (suppose x = h, decidable.inl (or.inl this)) (suppose x ≠ h, have ¬(x = h ∨ x ∈ l), from suppose x = h ∨ x ∈ l, or.elim this (suppose x = h, by contradiction) (suppose x ∈ l, by contradiction), have ¬x ∈ h::l, from iff.elim_right (not_iff_not_of_iff !mem_cons_iff) this, decidable.inr this))) theorem mem_of_ne_of_mem {x y : T} {l : list T} (H₁ : x ≠ y) (H₂ : x ∈ y :: l) : x ∈ l := or.elim (eq_or_mem_of_mem_cons H₂) (λe, absurd e H₁) (λr, r) theorem ne_of_not_mem_cons {a b : T} {l : list T} : a ∉ b::l → a ≠ b := assume nin aeqb, absurd (or.inl aeqb) nin theorem not_mem_of_not_mem_cons {a b : T} {l : list T} : a ∉ b::l → a ∉ l := assume nin nainl, absurd (or.inr nainl) nin lemma not_mem_cons_of_ne_of_not_mem {x y : T} {l : list T} : x ≠ y → x ∉ l → x ∉ y::l := assume P1 P2, not.intro (assume Pxin, absurd (eq_or_mem_of_mem_cons Pxin) (not_or P1 P2)) lemma ne_and_not_mem_of_not_mem_cons {x y : T} {l : list T} : x ∉ y::l → x ≠ y ∧ x ∉ l := assume P, and.intro (ne_of_not_mem_cons P) (not_mem_of_not_mem_cons P) definition sublist (l₁ l₂ : list T) := ∀ ⦃a : T⦄, a ∈ l₁ → a ∈ l₂ infix ⊆ := sublist theorem nil_sub [simp] (l : list T) : [] ⊆ l := λ b i, false.elim (iff.mp (mem_nil_iff b) i) theorem sub.refl [simp] (l : list T) : l ⊆ l := λ b i, i theorem sub.trans {l₁ l₂ l₃ : list T} (H₁ : l₁ ⊆ l₂) (H₂ : l₂ ⊆ l₃) : l₁ ⊆ l₃ := λ b i, H₂ (H₁ i) theorem sub_cons [simp] (a : T) (l : list T) : l ⊆ a::l := λ b i, or.inr i theorem sub_of_cons_sub {a : T} {l₁ l₂ : list T} : a::l₁ ⊆ l₂ → l₁ ⊆ l₂ := λ s b i, s b (mem_cons_of_mem _ i) theorem cons_sub_cons {l₁ l₂ : list T} (a : T) (s : l₁ ⊆ l₂) : (a::l₁) ⊆ (a::l₂) := λ b Hin, or.elim (eq_or_mem_of_mem_cons Hin) (λ e : b = a, or.inl e) (λ i : b ∈ l₁, or.inr (s i)) theorem sub_append_left [simp] (l₁ l₂ : list T) : l₁ ⊆ l₁++l₂ := λ b i, iff.mpr (mem_append_iff b l₁ l₂) (or.inl i) theorem sub_append_right [simp] (l₁ l₂ : list T) : l₂ ⊆ l₁++l₂ := λ b i, iff.mpr (mem_append_iff b l₁ l₂) (or.inr i) theorem sub_cons_of_sub (a : T) {l₁ l₂ : list T} : l₁ ⊆ l₂ → l₁ ⊆ (a::l₂) := λ (s : l₁ ⊆ l₂) (x : T) (i : x ∈ l₁), or.inr (s i) theorem sub_app_of_sub_left (l l₁ l₂ : list T) : l ⊆ l₁ → l ⊆ l₁++l₂ := λ (s : l ⊆ l₁) (x : T) (xinl : x ∈ l), have x ∈ l₁, from s xinl, mem_append_of_mem_or_mem (or.inl this) theorem sub_app_of_sub_right (l l₁ l₂ : list T) : l ⊆ l₂ → l ⊆ l₁++l₂ := λ (s : l ⊆ l₂) (x : T) (xinl : x ∈ l), have x ∈ l₂, from s xinl, mem_append_of_mem_or_mem (or.inr this) theorem cons_sub_of_sub_of_mem {a : T} {l m : list T} : a ∈ m → l ⊆ m → a::l ⊆ m := λ (ainm : a ∈ m) (lsubm : l ⊆ m) (x : T) (xinal : x ∈ a::l), or.elim (eq_or_mem_of_mem_cons xinal) (suppose x = a, by substvars; exact ainm) (suppose x ∈ l, lsubm this) theorem app_sub_of_sub_of_sub {l₁ l₂ l : list T} : l₁ ⊆ l → l₂ ⊆ l → l₁++l₂ ⊆ l := λ (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) (x : T) (xinl₁l₂ : x ∈ l₁++l₂), or.elim (mem_or_mem_of_mem_append xinl₁l₂) (suppose x ∈ l₁, l₁subl this) (suppose x ∈ l₂, l₂subl this) /- find -/ section variable [H : decidable_eq T] include H definition find : T → list T → nat | a [] := 0 | a (b :: l) := if a = b then 0 else succ (find a l) theorem find_nil [simp] (x : T) : find x [] = 0 := rfl theorem find_cons (x y : T) (l : list T) : find x (y::l) = if x = y then 0 else succ (find x l) := rfl theorem find_cons_of_eq {x y : T} (l : list T) : x = y → find x (y::l) = 0 := assume e, if_pos e theorem find_cons_of_ne {x y : T} (l : list T) : x ≠ y → find x (y::l) = succ (find x l) := assume n, if_neg n theorem find_of_not_mem {l : list T} {x : T} : ¬x ∈ l → find x l = length l := list.rec_on l (suppose ¬x ∈ [], rfl) (take y l, assume iH : ¬x ∈ l → find x l = length l, suppose ¬x ∈ y::l, have ¬(x = y ∨ x ∈ l), from iff.elim_right (not_iff_not_of_iff !mem_cons_iff) this, have ¬x = y ∧ ¬x ∈ l, from (iff.elim_left not_or_iff_not_and_not this), calc find x (y::l) = if x = y then 0 else succ (find x l) : !find_cons ... = succ (find x l) : if_neg (and.elim_left this) ... = succ (length l) : {iH (and.elim_right this)} ... = length (y::l) : !length_cons⁻¹) lemma find_le_length : ∀ {a} {l : list T}, find a l ≤ length l | a [] := !le.refl | a (b::l) := decidable.rec_on (H a b) (assume Peq, by rewrite [find_cons_of_eq l Peq]; exact !zero_le) (assume Pne, begin rewrite [find_cons_of_ne l Pne, length_cons], apply succ_le_succ, apply find_le_length end) lemma not_mem_of_find_eq_length : ∀ {a} {l : list T}, find a l = length l → a ∉ l | a [] := assume Peq, !not_mem_nil | a (b::l) := decidable.rec_on (H a b) (assume Peq, by rewrite [find_cons_of_eq l Peq, length_cons]; contradiction) (assume Pne, begin rewrite [find_cons_of_ne l Pne, length_cons, mem_cons_iff], intro Plen, apply (not_or Pne), exact not_mem_of_find_eq_length (succ.inj Plen) end) lemma find_lt_length {a} {l : list T} (Pin : a ∈ l) : find a l < length l := begin apply nat.lt_of_le_and_ne, apply find_le_length, apply not.intro, intro Peq, exact absurd Pin (not_mem_of_find_eq_length Peq) end end /- nth element -/ section nth definition nth : list T → nat → option T | [] n := none | (a :: l) 0 := some a | (a :: l) (n+1) := nth l n theorem nth_zero [simp] (a : T) (l : list T) : nth (a :: l) 0 = some a := rfl theorem nth_succ [simp] (a : T) (l : list T) (n : nat) : nth (a::l) (succ n) = nth l n := rfl theorem nth_eq_some : ∀ {l : list T} {n : nat}, n < length l → Σ a : T, nth l n = some a | [] n h := absurd h !not_lt_zero | (a::l) 0 h := ⟨a, rfl⟩ | (a::l) (succ n) h := have n < length l, from lt_of_succ_lt_succ h, obtain (r : T) (req : nth l n = some r), from nth_eq_some this, ⟨r, by rewrite [nth_succ, req]⟩ open decidable theorem find_nth [decidable_eq T] {a : T} : ∀ {l}, a ∈ l → nth l (find a l) = some a | [] ain := absurd ain !not_mem_nil | (b::l) ainbl := by_cases (λ aeqb : a = b, by rewrite [find_cons_of_eq _ aeqb, nth_zero, aeqb]) (λ aneb : a ≠ b, or.elim (eq_or_mem_of_mem_cons ainbl) (λ aeqb : a = b, absurd aeqb aneb) (λ ainl : a ∈ l, by rewrite [find_cons_of_ne _ aneb, nth_succ, find_nth ainl])) definition inth [h : inhabited T] (l : list T) (n : nat) : T := match nth l n with | some a := a | none := arbitrary T end theorem inth_zero [inhabited T] (a : T) (l : list T) : inth (a :: l) 0 = a := rfl theorem inth_succ [inhabited T] (a : T) (l : list T) (n : nat) : inth (a::l) (n+1) = inth l n := rfl end nth section ith definition ith : Π (l : list T) (i : nat), i < length l → T | nil i h := absurd h !not_lt_zero | (x::xs) 0 h := x | (x::xs) (succ i) h := ith xs i (lt_of_succ_lt_succ h) lemma ith_zero [simp] (a : T) (l : list T) (h : 0 < length (a::l)) : ith (a::l) 0 h = a := rfl lemma ith_succ [simp] (a : T) (l : list T) (i : nat) (h : succ i < length (a::l)) : ith (a::l) (succ i) h = ith l i (lt_of_succ_lt_succ h) := rfl end ith open decidable definition has_decidable_eq {A : Type} [H : decidable_eq A] : ∀ l₁ l₂ : list A, decidable (l₁ = l₂) | [] [] := inl rfl | [] (b::l₂) := inr (by contradiction) | (a::l₁) [] := inr (by contradiction) | (a::l₁) (b::l₂) := match H a b with | inl Hab := match has_decidable_eq l₁ l₂ with | inl He := inl (by congruence; repeat assumption) | inr Hn := inr (by intro H; injection H; contradiction) end | inr Hnab := inr (by intro H; injection H; contradiction) end /- quasiequal a l l' means that l' is exactly l, with a added once somewhere -/ section qeq variable {A : Type} inductive qeq (a : A) : list A → list A → Prop := | qhead : ∀ l, qeq a l (a::l) | qcons : ∀ (b : A) {l l' : list A}, qeq a l l' → qeq a (b::l) (b::l') open qeq notation l' `≈`:50 a `|` l:50 := qeq a l l' theorem qeq_app : ∀ (l₁ : list A) (a : A) (l₂ : list A), l₁++(a::l₂) ≈ a|l₁++l₂ | [] a l₂ := qhead a l₂ | (x::xs) a l₂ := qcons x (qeq_app xs a l₂) theorem mem_head_of_qeq {a : A} {l₁ l₂ : list A} : l₁≈a|l₂ → a ∈ l₁ := take q, qeq.induction_on q (λ l, !mem_cons) (λ b l l' q r, or.inr r) theorem mem_tail_of_qeq {a : A} {l₁ l₂ : list A} : l₁≈a|l₂ → ∀ x, x ∈ l₂ → x ∈ l₁ := take q, qeq.induction_on q (λ l x i, or.inr i) (λ b l l' q r x xinbl, or.elim (eq_or_mem_of_mem_cons xinbl) (λ xeqb : x = b, xeqb ▸ mem_cons x l') (λ xinl : x ∈ l, or.inr (r x xinl))) theorem mem_cons_of_qeq {a : A} {l₁ l₂ : list A} : l₁≈a|l₂ → ∀ x, x ∈ l₁ → x ∈ a::l₂ := take q, qeq.induction_on q (λ l x i, i) (λ b l l' q r x xinbl', or.elim (eq_or_mem_of_mem_cons xinbl') (λ xeqb : x = b, xeqb ▸ or.inr (mem_cons x l)) (λ xinl' : x ∈ l', or.elim (eq_or_mem_of_mem_cons (r x xinl')) (λ xeqa : x = a, xeqa ▸ mem_cons x (b::l)) (λ xinl : x ∈ l, or.inr (or.inr xinl)))) theorem length_eq_of_qeq {a : A} {l₁ l₂ : list A} : l₁≈a|l₂ → length l₁ = succ (length l₂) := take q, qeq.induction_on q (λ l, rfl) (λ b l l' q r, by rewrite [*length_cons, r]) theorem qeq_of_mem {a : A} {l : list A} : a ∈ l → (∃l', l≈a|l') := list.induction_on l (λ h : a ∈ nil, absurd h (not_mem_nil a)) (λ x xs r ainxxs, or.elim (eq_or_mem_of_mem_cons ainxxs) (λ aeqx : a = x, assert aux : ∃ l, x::xs≈x|l, from exists.intro xs (qhead x xs), by rewrite aeqx; exact aux) (λ ainxs : a ∈ xs, have ∃l', xs ≈ a|l', from r ainxs, obtain (l' : list A) (q : xs ≈ a|l'), from this, have x::xs ≈ a | x::l', from qcons x q, exists.intro (x::l') this)) theorem qeq_split {a : A} {l l' : list A} : l'≈a|l → ∃l₁ l₂, l = l₁++l₂ ∧ l' = l₁++(a::l₂) := take q, qeq.induction_on q (λ t, have t = []++t ∧ a::t = []++(a::t), from and.intro rfl rfl, exists.intro [] (exists.intro t this)) (λ b t t' q r, obtain (l₁ l₂ : list A) (h : t = l₁++l₂ ∧ t' = l₁++(a::l₂)), from r, have b::t = (b::l₁)++l₂ ∧ b::t' = (b::l₁)++(a::l₂), begin rewrite [and.elim_right h, and.elim_left h], constructor, repeat reflexivity end, exists.intro (b::l₁) (exists.intro l₂ this)) theorem sub_of_mem_of_sub_of_qeq {a : A} {l : list A} {u v : list A} : a ∉ l → a::l ⊆ v → v≈a|u → l ⊆ u := λ (nainl : a ∉ l) (s : a::l ⊆ v) (q : v≈a|u) (x : A) (xinl : x ∈ l), have x ∈ v, from s (or.inr xinl), have x ∈ a::u, from mem_cons_of_qeq q x this, or.elim (eq_or_mem_of_mem_cons this) (suppose x = a, by substvars; contradiction) (suppose x ∈ u, this) end qeq section firstn variable {A : Type} definition firstn : nat → list A → list A | 0 l := [] | (n+1) [] := [] | (n+1) (a::l) := a :: firstn n l lemma firstn_zero [simp] : ∀ (l : list A), firstn 0 l = [] := by intros; reflexivity lemma firstn_nil [simp] : ∀ n, firstn n [] = ([] : list A) | 0 := rfl | (n+1) := rfl lemma firstn_cons : ∀ n (a : A) (l : list A), firstn (succ n) (a::l) = a :: firstn n l := by intros; reflexivity lemma firstn_all : ∀ (l : list A), firstn (length l) l = l | [] := rfl | (a::l) := begin unfold [length, firstn], rewrite firstn_all end lemma firstn_all_of_ge : ∀ {n} {l : list A}, n ≥ length l → firstn n l = l | 0 [] h := rfl | 0 (a::l) h := absurd h (not_le_of_gt !succ_pos) | (n+1) [] h := rfl | (n+1) (a::l) h := begin unfold firstn, rewrite [firstn_all_of_ge (le_of_succ_le_succ h)] end lemma firstn_firstn : ∀ (n m) (l : list A), firstn n (firstn m l) = firstn (min n m) l | n 0 l := by rewrite [min_zero, firstn_zero, firstn_nil] | 0 m l := by rewrite [zero_min] | (succ n) (succ m) nil := by rewrite [*firstn_nil] | (succ n) (succ m) (a::l) := by rewrite [*firstn_cons, firstn_firstn, min_succ_succ] lemma length_firstn_le : ∀ (n) (l : list A), length (firstn n l) ≤ n | 0 l := by rewrite [firstn_zero] | (succ n) (a::l) := by rewrite [firstn_cons, length_cons, add_one]; apply succ_le_succ; apply length_firstn_le | (succ n) [] := by rewrite [firstn_nil, length_nil]; apply zero_le lemma length_firstn_eq : ∀ (n) (l : list A), length (firstn n l) = min n (length l) | 0 l := by rewrite [firstn_zero, zero_min] | (succ n) (a::l) := by rewrite [firstn_cons, *length_cons, *add_one, min_succ_succ, length_firstn_eq] | (succ n) [] := by rewrite [firstn_nil] end firstn section dropn variables {A : Type} -- 'dropn n l' drops the first 'n' elements of 'l' definition dropn : ℕ → list A → list A | 0 a := a | (succ n) [] := [] | (succ n) (x::r) := dropn n r theorem length_dropn : ∀ (i : ℕ) (l : list A), length (dropn i l) = length l - i | 0 l := rfl | (succ i) [] := calc length (dropn (succ i) []) = 0 - succ i : nat.zero_sub (succ i) | (succ i) (x::l) := calc length (dropn (succ i) (x::l)) = length (dropn i l) : rfl ... = length l - i : length_dropn i l ... = succ (length l) - succ i : succ_sub_succ (length l) i end dropn section count variable {A : Type} variable [decA : decidable_eq A] include decA definition count (a : A) : list A → nat | [] := 0 | (x::xs) := if a = x then succ (count xs) else count xs lemma count_nil (a : A) : count a [] = 0 := rfl lemma count_cons (a b : A) (l : list A) : count a (b::l) = if a = b then succ (count a l) else count a l := rfl lemma count_cons_eq (a : A) (l : list A) : count a (a::l) = succ (count a l) := if_pos rfl lemma count_cons_of_ne {a b : A} (h : a ≠ b) (l : list A) : count a (b::l) = count a l := if_neg h lemma count_cons_ge_count (a b : A) (l : list A) : count a (b::l) ≥ count a l := by_cases (suppose a = b, begin subst b, rewrite count_cons_eq, apply le_succ end) (suppose a ≠ b, begin rewrite (count_cons_of_ne this), apply le.refl end) lemma count_singleton (a : A) : count a [a] = 1 := by rewrite count_cons_eq lemma count_append (a : A) : ∀ l₁ l₂, count a (l₁++l₂) = count a l₁ + count a l₂ | [] l₂ := by rewrite [append_nil_left, count_nil, zero_add] | (b::l₁) l₂ := by_cases (suppose a = b, by rewrite [-this, append_cons, *count_cons_eq, succ_add, count_append]) (suppose a ≠ b, by rewrite [append_cons, *count_cons_of_ne this, count_append]) lemma count_concat (a : A) (l : list A) : count a (concat a l) = succ (count a l) := by rewrite [concat_eq_append, count_append, count_singleton] lemma mem_of_count_gt_zero : ∀ {a : A} {l : list A}, count a l > 0 → a ∈ l | a [] h := absurd h !lt.irrefl | a (b::l) h := by_cases (suppose a = b, begin subst b, apply mem_cons end) (suppose a ≠ b, have count a l > 0, by rewrite [count_cons_of_ne this at h]; exact h, have a ∈ l, from mem_of_count_gt_zero this, show a ∈ b::l, from mem_cons_of_mem _ this) lemma count_gt_zero_of_mem : ∀ {a : A} {l : list A}, a ∈ l → count a l > 0 | a [] h := absurd h !not_mem_nil | a (b::l) h := or.elim h (suppose a = b, begin subst b, rewrite count_cons_eq, apply zero_lt_succ end) (suppose a ∈ l, calc count a (b::l) ≥ count a l : count_cons_ge_count ... > 0 : count_gt_zero_of_mem this) lemma count_eq_zero_of_not_mem {a : A} {l : list A} (h : a ∉ l) : count a l = 0 := match count a l with | zero := suppose count a l = zero, this | (succ n) := suppose count a l = succ n, absurd (mem_of_count_gt_zero (begin rewrite this, exact dec_trivial end)) h end rfl end count end list attribute list.has_decidable_eq [instance] attribute list.decidable_mem [instance]
d0e6890d2ff5743967de39146d50f081fd12584f
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/tactic4.lean
1962296fd3272dec1d8a508b395485165acb9e03
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
163
lean
(* simple_tac = Repeat(conj_tac()) .. assumption_tac() *) theorem T4 (a b : Bool) : a → b → a /\ b := _. simple_tac done print environment 1.