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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
cfa46f6c8ca23f4a9e122bab21b21a6aa2d87e24 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/combinatorics/additive/behrend.lean | 1257399320c114769c87e6375754a8b4ded6e385 | [
"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 | 22,529 | lean | /-
Copyright (c) 2022 Yaël Dillies, Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies, Bhavik Mehta
-/
import analysis.inner_product_space.pi_L2
import combinatorics.additive.salem_spencer
import combinatorics.pigeonhole
import data.complex.exponential_bounds
/-!
# Behrend's bound on Roth numbers
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file proves Behrend's lower bound on Roth numbers. This says that we can find a subset of
`{1, ..., n}` of size `n / exp (O (sqrt (log n)))` which does not contain arithmetic progressions of
length `3`.
The idea is that the sphere (in the `n` dimensional Euclidean space) doesn't contain arithmetic
progressions (literally) because the corresponding ball is strictly convex. Thus we can take
integer points on that sphere and map them onto `ℕ` in a way that preserves arithmetic progressions
(`behrend.map`).
## Main declarations
* `behrend.sphere`: The intersection of the Euclidean sphere with the positive integer quadrant.
This is the set that we will map on `ℕ`.
* `behrend.map`: Given a natural number `d`, `behrend.map d : ℕⁿ → ℕ` reads off the coordinates as
digits in base `d`.
* `behrend.card_sphere_le_roth_number_nat`: Implicit lower bound on Roth numbers in terms of
`behrend.sphere`.
* `behrend.roth_lower_bound`: Behrend's explicit lower bound on Roth numbers.
## References
* [Bryan Gillespie, *Behrend’s Construction*]
(http://www.epsilonsmall.com/resources/behrends-construction/behrend.pdf)
* Behrend, F. A., "On sets of integers which contain no three terms in arithmetical progression"
* [Wikipedia, *Salem-Spencer set*](https://en.wikipedia.org/wiki/Salem–Spencer_set)
## Tags
Salem-Spencer, Behrend construction, arithmetic progression, sphere, strictly convex
-/
open finset nat (hiding log) real
open_locale big_operators pointwise
namespace behrend
variables {α β : Type*} {n d k N : ℕ} {x : fin n → ℕ}
/-!
### Turning the sphere into a Salem-Spencer set
We define `behrend.sphere`, the intersection of the $$L^2$$ sphere with the positive quadrant of
integer points. Because the $$L^2$$ closed ball is strictly convex, the $$L^2$$ sphere and
`behrend.sphere` are Salem-Spencer (`add_salem_spencer_sphere`). Then we can turn this set in
`fin n → ℕ` into a set in `ℕ` using `behrend.map`, which preserves `add_salem_spencer` because it is
an additive monoid homomorphism.
-/
/-- The box `{0, ..., d - 1}^n` as a finset. -/
def box (n d : ℕ) : finset (fin n → ℕ) := fintype.pi_finset $ λ _, range d
lemma mem_box : x ∈ box n d ↔ ∀ i, x i < d := by simp only [box, fintype.mem_pi_finset, mem_range]
@[simp] lemma card_box : (box n d).card = d ^ n := by simp [box]
@[simp] lemma box_zero : box (n + 1) 0 = ∅ := by simp [box]
/-- The intersection of the sphere of radius `sqrt k` with the integer points in the positive
quadrant. -/
def sphere (n d k : ℕ) : finset (fin n → ℕ) := (box n d).filter $ λ x, ∑ i, x i^2 = k
lemma sphere_zero_subset : sphere n d 0 ⊆ 0 :=
λ x, by simp [sphere, function.funext_iff] {contextual := tt}
@[simp] lemma sphere_zero_right (n k : ℕ) : sphere (n + 1) 0 k = ∅ := by simp [sphere]
lemma sphere_subset_box : sphere n d k ⊆ box n d := filter_subset _ _
lemma norm_of_mem_sphere {x : fin n → ℕ} (hx : x ∈ sphere n d k) :
‖(pi_Lp.equiv 2 _).symm (coe ∘ x : fin n → ℝ)‖ = sqrt k :=
begin
rw euclidean_space.norm_eq,
dsimp,
simp_rw [abs_cast, ←cast_pow, ←cast_sum, (mem_filter.1 hx).2],
end
lemma sphere_subset_preimage_metric_sphere :
(sphere n d k : set (fin n → ℕ)) ⊆
(λ x : fin n → ℕ, (pi_Lp.equiv 2 _).symm (coe ∘ x : fin n → ℝ)) ⁻¹'
metric.sphere (0 : pi_Lp 2 (λ _ : fin n, ℝ)) (sqrt k) :=
λ x hx, by rw [set.mem_preimage, mem_sphere_zero_iff_norm, norm_of_mem_sphere hx]
/-- The map that appears in Behrend's bound on Roth numbers. -/
@[simps] def map (d : ℕ) : (fin n → ℕ) →+ ℕ :=
{ to_fun := λ a, ∑ i, a i * d ^ (i : ℕ),
map_zero' := by simp_rw [pi.zero_apply, zero_mul, sum_const_zero],
map_add' := λ a b, by simp_rw [pi.add_apply, add_mul, sum_add_distrib] }
@[simp] lemma map_zero (d : ℕ) (a : fin 0 → ℕ) : map d a = 0 := by simp [map]
lemma map_succ (a : fin (n + 1) → ℕ) : map d a = a 0 + (∑ x : fin n, a x.succ * d ^ (x : ℕ)) * d :=
by simp [map, fin.sum_univ_succ, pow_succ', ←mul_assoc, ←sum_mul]
lemma map_succ' (a : fin (n + 1) → ℕ) : map d a = a 0 + map d (a ∘ fin.succ) * d := map_succ _
lemma map_monotone (d : ℕ) : monotone (map d : (fin n → ℕ) → ℕ) :=
λ x y h, by { dsimp, exact sum_le_sum (λ i _, nat.mul_le_mul_right _ $ h i) }
lemma map_mod (a : fin n.succ → ℕ) : map d a % d = a 0 % d :=
by rw [map_succ, nat.add_mul_mod_self_right]
lemma map_eq_iff {x₁ x₂ : fin n.succ → ℕ} (hx₁ : ∀ i, x₁ i < d) (hx₂ : ∀ i, x₂ i < d) :
map d x₁ = map d x₂ ↔ x₁ 0 = x₂ 0 ∧ map d (x₁ ∘ fin.succ) = map d (x₂ ∘ fin.succ) :=
begin
refine ⟨λ h, _, λ h, by rw [map_succ', map_succ', h.1, h.2]⟩,
have : x₁ 0 = x₂ 0,
{ rw [←mod_eq_of_lt (hx₁ _), ←map_mod, ←mod_eq_of_lt (hx₂ _), ←map_mod, h] },
rw [map_succ, map_succ, this, add_right_inj, mul_eq_mul_right_iff] at h,
exact ⟨this, h.resolve_right (pos_of_gt (hx₁ 0)).ne'⟩,
end
lemma map_inj_on : {x : fin n → ℕ | ∀ i, x i < d}.inj_on (map d) :=
begin
intros x₁ hx₁ x₂ hx₂ h,
induction n with n ih,
{ simp },
ext i,
have x := (map_eq_iff hx₁ hx₂).1 h,
refine fin.cases x.1 (congr_fun $ ih (λ _, _) (λ _, _) x.2) i,
{ exact hx₁ _ },
{ exact hx₂ _ }
end
lemma map_le_of_mem_box (hx : x ∈ box n d) :
map (2 * d - 1) x ≤ ∑ i : fin n, (d - 1) * (2 * d - 1) ^ (i : ℕ) :=
map_monotone (2 * d - 1) $ λ _, nat.le_pred_of_lt $ mem_box.1 hx _
lemma add_salem_spencer_sphere : add_salem_spencer (sphere n d k : set (fin n → ℕ)) :=
begin
set f : (fin n → ℕ) →+ euclidean_space ℝ (fin n) :=
{ to_fun := λ f, (coe : ℕ → ℝ) ∘ f,
map_zero' := funext $ λ _, cast_zero,
map_add' := λ _ _, funext $ λ _, cast_add _ _ },
refine add_salem_spencer.of_image (f.to_add_freiman_hom (sphere n d k) 2) _ _,
{ exact cast_injective.comp_left.inj_on _ },
refine (add_salem_spencer_sphere 0 $ sqrt k).mono (set.image_subset_iff.2 $ λ x, _),
rw [set.mem_preimage, mem_sphere_zero_iff_norm],
exact norm_of_mem_sphere,
end
lemma add_salem_spencer_image_sphere :
add_salem_spencer ((sphere n d k).image (map (2 * d - 1)) : set ℕ) :=
begin
rw coe_image,
refine @add_salem_spencer.image _ (fin n → ℕ) ℕ _ _ (sphere n d k) _ (map (2 * d - 1))
(map_inj_on.mono _) add_salem_spencer_sphere,
rw set.add_subset_iff,
rintro a ha b hb i,
have hai := mem_box.1 (sphere_subset_box ha) i,
have hbi := mem_box.1 (sphere_subset_box hb) i,
rw [lt_tsub_iff_right, ←succ_le_iff, two_mul],
exact (add_add_add_comm _ _ 1 1).trans_le (add_le_add hai hbi),
end
lemma sum_sq_le_of_mem_box (hx : x ∈ box n d) : ∑ i : fin n, (x i)^2 ≤ n * (d - 1)^2 :=
begin
rw mem_box at hx,
have : ∀ i, x i ^ 2 ≤ (d - 1) ^ 2 := λ i, nat.pow_le_pow_of_le_left (nat.le_pred_of_lt (hx i)) _,
exact (sum_le_card_nsmul univ _ _ $ λ i _, this i).trans (by rw [card_fin, smul_eq_mul]),
end
lemma sum_eq : ∑ i : fin n, d * (2 * d + 1) ^ (i : ℕ) = ((2 * d + 1) ^ n - 1) / 2 :=
begin
refine (nat.div_eq_of_eq_mul_left zero_lt_two _).symm,
rw [←sum_range (λ i, d * (2 * d + 1) ^ (i : ℕ)), ←mul_sum, mul_right_comm, mul_comm d,
←geom_sum_mul_add, add_tsub_cancel_right, mul_comm],
end
lemma sum_lt : ∑ i : fin n, d * (2 * d + 1) ^ (i : ℕ) < (2 * d + 1) ^ n :=
sum_eq.trans_lt $ (nat.div_le_self _ 2).trans_lt $ pred_lt (pow_pos (succ_pos _) _).ne'
lemma card_sphere_le_roth_number_nat (n d k : ℕ) :
(sphere n d k).card ≤ roth_number_nat ((2 * d - 1) ^ n) :=
begin
cases n,
{ refine (card_le_univ _).trans_eq _,
rw pow_zero,
exact fintype.card_unique },
cases d,
{ simp },
refine add_salem_spencer_image_sphere.le_roth_number_nat _ _ (card_image_of_inj_on _),
{ simp only [subset_iff, mem_image, and_imp, forall_exists_index, mem_range,
forall_apply_eq_imp_iff₂, sphere, mem_filter],
rintro _ x hx _ rfl,
exact (map_le_of_mem_box hx).trans_lt sum_lt },
refine map_inj_on.mono (λ x, _),
simp only [mem_coe, sphere, mem_filter, mem_box, and_imp, two_mul],
exact λ h _ i, (h i).trans_le le_self_add,
end
/-!
### Optimization
Now that we know how to turn the integer points of any sphere into a Salem-Spencer set, we find a
sphere containing many integer points by the pigeonhole principle. This gives us an implicit bound
that we then optimize by tweaking the parameters. The (almost) optimal parameters are
`behrend.n_value` and `behrend.d_value`.
-/
lemma exists_large_sphere_aux (n d : ℕ) :
∃ k ∈ range (n * (d - 1)^2 + 1), (↑(d ^ n) / (↑(n * (d - 1)^2) + 1) : ℝ) ≤ (sphere n d k).card :=
begin
refine exists_le_card_fiber_of_nsmul_le_card_of_maps_to (λ x hx, _) nonempty_range_succ _,
{ rw [mem_range, lt_succ_iff],
exact sum_sq_le_of_mem_box hx },
{ rw [card_range, _root_.nsmul_eq_mul, mul_div_assoc', cast_add_one, mul_div_cancel_left,
card_box],
exact (cast_add_one_pos _).ne' }
end
lemma exists_large_sphere (n d : ℕ) : ∃ k, (d ^ n / ↑(n * d^2) : ℝ) ≤ (sphere n d k).card :=
begin
obtain ⟨k, -, hk⟩ := exists_large_sphere_aux n d,
refine ⟨k, _⟩,
obtain rfl | hn := n.eq_zero_or_pos,
{ simp },
obtain rfl | hd := d.eq_zero_or_pos,
{ simp },
rw ←cast_pow,
refine (div_le_div_of_le_left _ _ _).trans hk,
{ exact cast_nonneg _ },
{ exact cast_add_one_pos _ },
simp only [←le_sub_iff_add_le', cast_mul, ←mul_sub, cast_pow, cast_sub hd, sub_sq,
one_pow, cast_one, mul_one, sub_add, sub_sub_self],
apply one_le_mul_of_one_le_of_one_le,
{ rwa one_le_cast },
rw le_sub_iff_add_le,
norm_num,
exact one_le_cast.2 hd,
end
lemma bound_aux' (n d : ℕ) : (d ^ n / ↑(n * d^2) : ℝ) ≤ roth_number_nat ((2 * d - 1)^n) :=
let ⟨k, h⟩ := exists_large_sphere n d in h.trans $ cast_le.2 $ card_sphere_le_roth_number_nat _ _ _
lemma bound_aux (hd : d ≠ 0) (hn : 2 ≤ n) :
(d ^ (n - 2) / n : ℝ) ≤ roth_number_nat ((2 * d - 1)^n) :=
begin
convert bound_aux' n d using 1,
rw [cast_mul, cast_pow, mul_comm, ←div_div, pow_sub₀ _ _ hn, ←div_eq_mul_inv],
rwa cast_ne_zero,
end
open_locale filter topology
open real
section numerical_bounds
lemma log_two_mul_two_le_sqrt_log_eight : log 2 * 2 ≤ sqrt (log 8) :=
begin
rw [show (8 : ℝ) = 2 ^ ((3 : ℕ) : ℝ), by norm_num1, log_rpow zero_lt_two (3:ℕ)],
apply le_sqrt_of_sq_le,
rw [mul_pow, sq (log 2), mul_assoc, mul_comm],
refine mul_le_mul_of_nonneg_right _ (log_nonneg one_le_two),
rw ←le_div_iff,
apply log_two_lt_d9.le.trans,
all_goals { norm_num1 }
end
lemma two_div_one_sub_two_div_e_le_eight : 2 / (1 - 2 / exp 1) ≤ 8 :=
begin
rw [div_le_iff, mul_sub, mul_one, mul_div_assoc', le_sub_comm, div_le_iff (exp_pos _)],
{ linarith [exp_one_gt_d9] },
rw [sub_pos, div_lt_one];
exact exp_one_gt_d9.trans' (by norm_num),
end
lemma le_sqrt_log (hN : 4096 ≤ N) : log (2 / (1 - 2 / exp 1)) * (69 / 50) ≤ sqrt (log ↑N) :=
begin
have : ((12 : ℕ) : ℝ) * log 2 ≤ log N,
{ rw [←log_rpow zero_lt_two, log_le_log, rpow_nat_cast],
{ norm_num1,
exact_mod_cast hN },
{ exact rpow_pos_of_pos zero_lt_two _ },
rw cast_pos,
exact hN.trans_lt' (by norm_num1) },
refine (mul_le_mul_of_nonneg_right ((log_le_log _ $ by norm_num1).2
two_div_one_sub_two_div_e_le_eight) $ by norm_num1).trans (_),
{ refine div_pos zero_lt_two _,
rw [sub_pos, div_lt_one (exp_pos _)],
exact exp_one_gt_d9.trans_le' (by norm_num1) },
have l8 : log 8 = (3 : ℕ) * log 2,
{ rw [←log_rpow zero_lt_two, rpow_nat_cast],
norm_num },
rw [l8, cast_bit1, cast_one],
apply le_sqrt_of_sq_le (le_trans _ this),
simp only [cast_bit0, cast_bit1, cast_one],
rw [mul_right_comm, mul_pow, sq (log 2), ←mul_assoc],
apply mul_le_mul_of_nonneg_right _ (log_nonneg one_le_two),
rw ←le_div_iff' ,
{ exact log_two_lt_d9.le.trans (by norm_num1) },
exact sq_pos_of_ne_zero _ (by norm_num1),
end
lemma exp_neg_two_mul_le {x : ℝ} (hx : 0 < x) : exp (-2 * x) < exp (2 - ⌈x⌉₊) / ⌈x⌉₊ :=
begin
have h₁ := ceil_lt_add_one hx.le,
have h₂ : 1 - x ≤ 2 - ⌈x⌉₊,
{ rw le_sub_iff_add_le,
apply (add_le_add_left h₁.le _).trans_eq,
rw [←add_assoc, sub_add_cancel],
refl },
have h₃ : exp (-(x+1)) ≤ 1 / (x + 1),
{ rw [exp_neg, inv_eq_one_div],
refine one_div_le_one_div_of_le (add_pos hx zero_lt_one) _,
apply le_trans _ (add_one_le_exp_of_nonneg $ add_nonneg hx.le zero_le_one),
exact le_add_of_nonneg_right zero_le_one },
refine lt_of_le_of_lt _ (div_lt_div_of_lt_left (exp_pos _) (cast_pos.2 $ ceil_pos.2 hx) h₁),
refine le_trans _ (div_le_div_of_le_of_nonneg (exp_le_exp.2 h₂) $ add_nonneg hx.le zero_le_one),
rw [le_div_iff (add_pos hx zero_lt_one), ←le_div_iff' (exp_pos _), ←exp_sub, neg_mul,
sub_neg_eq_add, two_mul, sub_add_add_cancel, add_comm _ x],
refine le_trans _ (add_one_le_exp_of_nonneg $ add_nonneg hx.le zero_le_one),
exact le_add_of_nonneg_right zero_le_one,
end
lemma div_lt_floor {x : ℝ} (hx : 2 / (1 - 2 / exp 1) ≤ x) : x / exp 1 < (⌊x/2⌋₊ : ℝ) :=
begin
apply lt_of_le_of_lt _ (sub_one_lt_floor _),
have : 0 < 1 - 2 / exp 1,
{ rw [sub_pos, div_lt_one (exp_pos _)],
exact lt_of_le_of_lt (by norm_num) exp_one_gt_d9 },
rwa [le_sub_comm, div_eq_mul_one_div x, div_eq_mul_one_div x, ←mul_sub, div_sub',
←div_eq_mul_one_div, mul_div_assoc', one_le_div, ←div_le_iff this],
{ exact zero_lt_two },
{ exact two_ne_zero }
end
lemma ceil_lt_mul {x : ℝ} (hx : 50/19 ≤ x) : (⌈x⌉₊ : ℝ) < 1.38 * x :=
begin
refine (ceil_lt_add_one $ hx.trans' $ by norm_num).trans_le _,
rwa [←le_sub_iff_add_le', ←sub_one_mul, show (69/50 - 1 : ℝ) = (50/19)⁻¹, by norm_num1,
←div_eq_inv_mul, one_le_div],
norm_num1,
end
end numerical_bounds
/-- The (almost) optimal value of `n` in `behrend.bound_aux`. -/
noncomputable def n_value (N : ℕ) : ℕ := ⌈sqrt (log N)⌉₊
/-- The (almost) optimal value of `d` in `behrend.bound_aux`. -/
noncomputable def d_value (N : ℕ) : ℕ := ⌊(N : ℝ)^(1 / n_value N : ℝ)/2⌋₊
lemma n_value_pos (hN : 2 ≤ N) : 0 < n_value N :=
ceil_pos.2 $ real.sqrt_pos.2 $ log_pos $ one_lt_cast.2 $ hN
lemma two_le_n_value (hN : 3 ≤ N) : 2 ≤ n_value N :=
begin
refine succ_le_of_lt (lt_ceil.2 $ lt_sqrt_of_sq_lt _),
rw [cast_one, one_pow, lt_log_iff_exp_lt],
refine lt_of_lt_of_le _ (cast_le.2 hN),
{ exact exp_one_lt_d9.trans_le (by norm_num) },
rw cast_pos,
exact (zero_lt_succ _).trans_le hN,
end
lemma three_le_n_value (hN : 64 ≤ N) : 3 ≤ n_value N :=
begin
rw [n_value, ←lt_iff_add_one_le, lt_ceil, cast_two],
apply lt_sqrt_of_sq_lt,
have : (2 : ℝ)^((6 : ℕ) : ℝ) ≤ N,
{ rw rpow_nat_cast,
exact (cast_le.2 hN).trans' (by norm_num1) },
apply lt_of_lt_of_le _ ((log_le_log (rpow_pos_of_pos zero_lt_two _) _).2 this),
rw [log_rpow zero_lt_two, cast_bit0, cast_bit1, cast_one, ←div_lt_iff'],
{ exact log_two_gt_d9.trans_le' (by norm_num1) },
{ norm_num1 },
rw cast_pos,
exact hN.trans_lt' (by norm_num1),
end
lemma d_value_pos (hN₃ : 8 ≤ N) : 0 < d_value N :=
begin
have hN₀ : 0 < (N : ℝ) := cast_pos.2 (succ_pos'.trans_le hN₃),
rw [d_value, floor_pos, ←log_le_log zero_lt_one, log_one, log_div _ two_ne_zero, log_rpow hN₀,
div_mul_eq_mul_div, one_mul, sub_nonneg, le_div_iff],
{ have : (n_value N : ℝ) ≤ 2 * sqrt (log N),
{ apply (ceil_lt_add_one $ sqrt_nonneg _).le.trans,
rw [two_mul, add_le_add_iff_left],
apply le_sqrt_of_sq_le,
rw [one_pow, le_log_iff_exp_le hN₀],
exact (exp_one_lt_d9.le.trans $ by norm_num).trans (cast_le.2 hN₃) },
apply (mul_le_mul_of_nonneg_left this $ log_nonneg one_le_two).trans _,
rw [←mul_assoc, ←le_div_iff (real.sqrt_pos.2 $ log_pos $ one_lt_cast.2 _), div_sqrt],
{ apply log_two_mul_two_le_sqrt_log_eight.trans,
apply real.sqrt_le_sqrt,
rw log_le_log _ hN₀,
{ exact_mod_cast hN₃ },
{ norm_num } },
exact hN₃.trans_lt' (by norm_num) },
{ exact cast_pos.2 (n_value_pos $ hN₃.trans' $ by norm_num) },
{ exact (rpow_pos_of_pos hN₀ _).ne' },
{ exact div_pos (rpow_pos_of_pos hN₀ _) zero_lt_two },
end
lemma le_N (hN : 2 ≤ N) : (2 * (d_value N) - 1)^(n_value N) ≤ N :=
begin
have : (2 * d_value N - 1)^(n_value N) ≤ (2 * d_value N)^(n_value N) :=
nat.pow_le_pow_of_le_left (nat.sub_le _ _) _,
apply this.trans,
suffices : ((2 * d_value N)^n_value N : ℝ) ≤ N, by exact_mod_cast this,
rw ←rpow_nat_cast,
suffices i : (2 * d_value N : ℝ) ≤ (N : ℝ)^(1/n_value N : ℝ),
{ apply (rpow_le_rpow (mul_nonneg zero_le_two (cast_nonneg _)) i (cast_nonneg _)).trans,
rw [←rpow_mul (cast_nonneg _), one_div_mul_cancel, rpow_one],
rw cast_ne_zero,
apply (n_value_pos hN).ne', },
rw ←le_div_iff',
{ exact floor_le (div_nonneg (rpow_nonneg_of_nonneg (cast_nonneg _) _) zero_le_two) },
apply zero_lt_two
end
lemma bound (hN : 4096 ≤ N) : (N : ℝ)^(1/n_value N : ℝ) / exp 1 < d_value N :=
begin
apply div_lt_floor _,
rw [←log_le_log, log_rpow, mul_comm, ←div_eq_mul_one_div],
{ apply le_trans _ (div_le_div_of_le_left _ _ (ceil_lt_mul _).le),
rw [mul_comm, ←div_div, div_sqrt, le_div_iff],
{ exact le_sqrt_log hN },
{ norm_num1 },
{ apply log_nonneg,
rw one_le_cast,
exact hN.trans' (by norm_num1) },
{ rw [cast_pos, lt_ceil, cast_zero, real.sqrt_pos],
refine log_pos _,
rw one_lt_cast,
exact hN.trans_lt' (by norm_num1) },
apply le_sqrt_of_sq_le,
have : ((12 : ℕ) : ℝ) * log 2 ≤ log N,
{ rw [←log_rpow zero_lt_two, log_le_log, rpow_nat_cast],
{ norm_num1,
exact_mod_cast hN },
{ exact rpow_pos_of_pos zero_lt_two _ },
rw cast_pos,
exact hN.trans_lt' (by norm_num1) },
refine le_trans _ this,
simp only [cast_bit0, cast_bit1, cast_one],
rw ←div_le_iff',
{ exact log_two_gt_d9.le.trans' (by norm_num1) },
{ norm_num1 } },
{ rw cast_pos,
exact hN.trans_lt' (by norm_num1) },
{ refine div_pos zero_lt_two _,
rw [sub_pos, div_lt_one (exp_pos _)],
exact lt_of_le_of_lt (by norm_num1) exp_one_gt_d9 },
apply rpow_pos_of_pos,
rw cast_pos,
exact hN.trans_lt' (by norm_num1),
end
lemma roth_lower_bound_explicit (hN : 4096 ≤ N) :
(N : ℝ) * exp (-4 * sqrt (log N)) < roth_number_nat N :=
begin
let n := n_value N,
have hn : 0 < (n : ℝ) := cast_pos.2 (n_value_pos $ hN.trans' $ by norm_num1),
have hd : 0 < d_value N := d_value_pos (hN.trans' $ by norm_num1),
have hN₀ : 0 < (N : ℝ) := cast_pos.2 (hN.trans' $ by norm_num1),
have hn₂ : 2 ≤ n := two_le_n_value (hN.trans' $ by norm_num1),
have : (2 * d_value N - 1)^n ≤ N := le_N (hN.trans' $ by norm_num1),
refine ((bound_aux hd.ne' hn₂).trans $ cast_le.2 $ roth_number_nat.mono this).trans_lt' _,
refine (div_lt_div_of_lt hn $ pow_lt_pow_of_lt_left (bound hN) _ _).trans_le' _,
{ exact div_nonneg (rpow_nonneg_of_nonneg (cast_nonneg _) _) (exp_pos _).le },
{ exact tsub_pos_of_lt (three_le_n_value $ hN.trans' $ by norm_num1) },
rw [←rpow_nat_cast, div_rpow (rpow_nonneg_of_nonneg hN₀.le _) (exp_pos _).le, ←rpow_mul hN₀.le,
mul_comm (_ / _), mul_one_div, cast_sub hn₂, cast_two, same_sub_div hn.ne', exp_one_rpow,
div_div, rpow_sub hN₀, rpow_one, div_div, div_eq_mul_inv],
refine mul_le_mul_of_nonneg_left _ (cast_nonneg _),
rw [mul_inv, mul_inv, ←exp_neg, ←rpow_neg (cast_nonneg _), neg_sub, ←div_eq_mul_inv],
have : exp ((-4) * sqrt (log N)) = exp (-2 * sqrt (log N)) * exp (-2 * sqrt (log N)),
{ rw [←exp_add, ←add_mul],
norm_num },
rw this,
refine (mul_le_mul _ (exp_neg_two_mul_le $ real.sqrt_pos.2 $ log_pos _).le (exp_pos _).le $
rpow_nonneg_of_nonneg (cast_nonneg _) _),
{ rw [←le_log_iff_exp_le (rpow_pos_of_pos hN₀ _), log_rpow hN₀, ←le_div_iff, mul_div_assoc,
div_sqrt, neg_mul, neg_le_neg_iff, div_mul_eq_mul_div, div_le_iff hn],
{ exact mul_le_mul_of_nonneg_left (le_ceil _) zero_le_two },
refine real.sqrt_pos.2 (log_pos _),
rw one_lt_cast,
exact hN.trans_lt' (by norm_num1) },
{ rw one_lt_cast,
exact hN.trans_lt' (by norm_num1) }
end
lemma exp_four_lt : exp 4 < 64 :=
begin
rw [show (64 : ℝ) = 2 ^ ((6 : ℕ) : ℝ), by norm_num1,
←lt_log_iff_exp_lt (rpow_pos_of_pos zero_lt_two _), log_rpow zero_lt_two, ←div_lt_iff'],
exact log_two_gt_d9.trans_le' (by norm_num1),
norm_num
end
lemma four_zero_nine_six_lt_exp_sixteen : 4096 < exp 16 :=
begin
rw [←log_lt_iff_lt_exp (show (0 : ℝ) < 4096, by norm_num), show (4096 : ℝ) = 2 ^ 12, by norm_num,
←rpow_nat_cast, log_rpow zero_lt_two, cast_bit0, cast_bit0, cast_bit1, cast_one],
linarith [log_two_lt_d9],
end
lemma lower_bound_le_one' (hN : 2 ≤ N) (hN' : N ≤ 4096) : (N : ℝ) * exp (-4 * sqrt (log N)) ≤ 1 :=
begin
rw [←log_le_log (mul_pos (cast_pos.2 (zero_lt_two.trans_le hN)) (exp_pos _)) zero_lt_one,
log_one, log_mul (cast_pos.2 (zero_lt_two.trans_le hN)).ne' (exp_pos _).ne', log_exp,
neg_mul, ←sub_eq_add_neg, sub_nonpos, ←div_le_iff (real.sqrt_pos.2 $ log_pos $
one_lt_cast.2 $ one_lt_two.trans_le hN), div_sqrt, sqrt_le_left
(zero_le_bit0.2 zero_le_two), log_le_iff_le_exp (cast_pos.2 (zero_lt_two.trans_le hN))],
norm_num1,
apply le_trans _ four_zero_nine_six_lt_exp_sixteen.le,
exact_mod_cast hN',
end
lemma lower_bound_le_one (hN : 1 ≤ N) (hN' : N ≤ 4096) : (N : ℝ) * exp (-4 * sqrt (log N)) ≤ 1 :=
begin
obtain rfl | hN := hN.eq_or_lt,
{ norm_num },
{ exact lower_bound_le_one' hN hN' }
end
lemma roth_lower_bound : (N : ℝ) * exp (-4 * sqrt (log N)) ≤ roth_number_nat N :=
begin
obtain rfl | hN := nat.eq_zero_or_pos N,
{ norm_num },
obtain h₁ | h₁ := le_or_lt 4096 N,
{ exact (roth_lower_bound_explicit h₁).le },
{ apply (lower_bound_le_one hN h₁.le).trans,
simpa using roth_number_nat.monotone hN }
end
end behrend
|
fed8a268faecf5ffb10bb00be65a1daed47ae562 | 7cef822f3b952965621309e88eadf618da0c8ae9 | /test/fin_cases.lean | 88da7e0b6235921a56c28c37a452f84a0026d3b1 | [
"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 | 2,000 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import tactic.fin_cases
import data.nat.prime
import group_theory.perm.sign
import tactic.norm_num
example (f : ℕ → Prop) (p : fin 3) (h0 : f 0) (h1 : f 1) (h2 : f 2) : f p.val :=
begin
fin_cases *,
simp, assumption,
simp, assumption,
simp, assumption,
end
example (x2 : fin 2) (x3 : fin 3) (n : nat) (y : fin n) : x2.val * x3.val = x3.val * x2.val :=
begin
fin_cases x2;
fin_cases x3,
success_if_fail { fin_cases * },
success_if_fail { fin_cases y },
all_goals { simp },
end
open finset
example (x : ℕ) (h : x ∈ Ico 2 5) : x = 2 ∨ x = 3 ∨ x = 4 :=
begin
fin_cases h,
all_goals { simp }
end
open nat
example (x : ℕ) (h : x ∈ [2,3,5,7]) : x = 2 ∨ x = 3 ∨ x = 5 ∨ x = 7 :=
begin
fin_cases h,
all_goals { simp }
end
example (x : ℕ) (h : x ∈ [2,3,5,7]) : true :=
begin
success_if_fail { fin_cases h with [3,3,5,7] },
trivial
end
example (x : list ℕ) (h : x ∈ [[1],[2]]) : x.length = 1 :=
begin
fin_cases h with [[1],[1+1]],
simp,
guard_target (list.length [1 + 1] = 1),
simp
end
-- testing that `with` arguments are elaborated with respect to the expected type:
example (x : ℤ) (h : x ∈ ([2,3] : list ℤ)) : x = 2 ∨ x = 3:=
begin
fin_cases h with [2,3],
all_goals { simp }
end
instance (n : ℕ) : decidable (prime n) := decidable_prime_1 n
example (x : ℕ) (h : x ∈ (range 10).filter prime) : x = 2 ∨ x = 3 ∨ x = 5 ∨ x = 7 :=
begin
fin_cases h; exact dec_trivial
end
open equiv.perm
example (x : (Σ (a : fin 4), fin 4)) (h : x ∈ fin_pairs_lt 4) : x.1.val < 4 :=
begin
fin_cases h; simp,
any_goals { exact dec_trivial },
end
example (x : fin 3) : x.val < 5 :=
begin
fin_cases x; exact dec_trivial
end
example (f : ℕ → Prop) (p : fin 3) (h0 : f 0) (h1 : f 1) (h2 : f 2) : f p.val :=
begin
fin_cases *,
all_goals { assumption }
end
|
0f753c63040a510c3238b526bc91749e660fa87f | 947b78d97130d56365ae2ec264df196ce769371a | /tests/lean/run/def6.lean | dc7d9dd1a4a365e202182bc729f81e143e8ce747 | [
"Apache-2.0"
] | permissive | shyamalschandra/lean4 | 27044812be8698f0c79147615b1d5090b9f4b037 | 6e7a883b21eaf62831e8111b251dc9b18f40e604 | refs/heads/master | 1,671,417,126,371 | 1,601,859,995,000 | 1,601,860,020,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 851 | lean | new_frontend
open Nat
inductive BV : Nat → Type
| nil : BV 0
| cons : ∀ (n) (hd : Bool) (tl : BV n), BV (succ n)
open BV
variable (f : Bool → Bool → Bool)
def map2 : {n : Nat} → BV n → BV n → BV n
| .(0), nil, nil => nil
| .(n+1), cons n b1 v1, cons .(n) b2 v2 => cons n (f b1 b2) (map2 v1 v2)
theorem ex1 : map2 f nil nil = nil :=
rfl
theorem ex2 (n : Nat) (b1 b2 : Bool) (v1 v2 : BV n) : map2 f (cons n b1 v1) (cons n b2 v2) = cons n (f b1 b2) (map2 f v1 v2) :=
rfl
#print map2
def map2' : {n : Nat} → BV n → BV n → BV n
| _, nil, nil => nil
| _, cons _ b1 v1, cons _ b2 v2 => cons _ (f b1 b2) (map2' v1 v2)
theorem ex3 : map2' f nil nil = nil :=
rfl
theorem ex4 (n : Nat) (b1 b2 : Bool) (v1 v2 : BV n) : map2' f (cons n b1 v1) (cons n b2 v2) = cons n (f b1 b2) (map2' f v1 v2) :=
rfl
|
0076c310a641eed74b0d698bf08f4382ad5f7cbd | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/set/intervals/monotone.lean | 58f95ed94491a90406f8728095f54b212fd68caa | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 6,678 | lean | /-
Copyright (c) 2021 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import data.set.intervals.disjoint
import tactic.field_simp
/-!
# Monotonicity on intervals
In this file we prove that a function is (strictly) monotone (or antitone) on a linear order `α`
provided that it is (strictly) monotone on `(-∞, a]` and on `[a, +∞)`. We also provide an order
isomorphism `order_iso_Ioo_neg_one_one` between the open interval `(-1, 1)` in a linear ordered
field and the whole field.
-/
open set
section
variables {α β : Type*} [linear_order α] [preorder β] {a : α} {f : α → β}
/-- If `f` is strictly monotone both on `(-∞, a]` and `[a, ∞)`, then it is strictly monotone on the
whole line. -/
protected lemma strict_mono_on.Iic_union_Ici (h₁ : strict_mono_on f (Iic a))
(h₂ : strict_mono_on f (Ici a)) : strict_mono f :=
begin
intros x y hxy,
cases lt_or_le a x with hax hxa; [skip, cases le_or_lt y a with hya hay],
exacts [h₂ hax.le (hax.trans hxy).le hxy, h₁ hxa hya hxy,
(h₁.monotone_on hxa le_rfl hxa).trans_lt (h₂ le_rfl hay.le hay)]
end
/-- If `f` is strictly antitone both on `(-∞, a]` and `[a, ∞)`, then it is strictly antitone on the
whole line. -/
protected lemma strict_anti_on.Iic_union_Ici (h₁ : strict_anti_on f (Iic a))
(h₂ : strict_anti_on f (Ici a)) : strict_anti f :=
(h₁.dual_right.Iic_union_Ici h₂.dual_right).dual_right
protected lemma monotone_on.Iic_union_Ici (h₁ : monotone_on f (Iic a))
(h₂ : monotone_on f (Ici a)) : monotone f :=
begin
intros x y hxy,
cases le_total x a with hxa hax; [cases le_total y a with hya hay, skip],
exacts [h₁ hxa hya hxy, (h₁ hxa le_rfl hxa).trans (h₂ le_rfl hay hay), h₂ hax (hax.trans hxy) hxy]
end
protected lemma antitone_on.Iic_union_Ici (h₁ : antitone_on f (Iic a))
(h₂ : antitone_on f (Ici a)) : antitone f :=
(h₁.dual_right.Iic_union_Ici h₂.dual_right).dual_right
end
section ordered_group
variables {G H : Type*} [linear_ordered_add_comm_group G] [ordered_add_comm_group H]
lemma strict_mono_of_odd_strict_mono_on_nonneg {f : G → H} (h₁ : ∀ x, f (-x) = -f x)
(h₂ : strict_mono_on f (Ici 0)) :
strict_mono f :=
begin
refine strict_mono_on.Iic_union_Ici (λ x hx y hy hxy, neg_lt_neg_iff.1 _) h₂,
rw [← h₁, ← h₁],
exact h₂ (neg_nonneg.2 hy) (neg_nonneg.2 hx) (neg_lt_neg hxy)
end
lemma monotone_of_odd_of_monotone_on_nonneg {f : G → H} (h₁ : ∀ x, f (-x) = -f x)
(h₂ : monotone_on f (Ici 0)) : monotone f :=
begin
refine monotone_on.Iic_union_Ici (λ x hx y hy hxy, neg_le_neg_iff.1 _) h₂,
rw [← h₁, ← h₁],
exact h₂ (neg_nonneg.2 hy) (neg_nonneg.2 hx) (neg_le_neg hxy)
end
end ordered_group
/-- In a linear ordered field, the whole field is order isomorphic to the open interval `(-1, 1)`.
We consider the actual implementation to be a "black box", so it is irreducible.
-/
@[irreducible] def order_iso_Ioo_neg_one_one (k : Type*) [linear_ordered_field k] :
k ≃o Ioo (-1 : k) 1 :=
begin
refine strict_mono.order_iso_of_right_inverse _ _ (λ x, x / (1 - |x|)) _,
{ refine cod_restrict (λ x, x / (1 + |x|)) _ (λ x, abs_lt.1 _),
have H : 0 < 1 + |x|, from (abs_nonneg x).trans_lt (lt_one_add _),
calc |x / (1 + |x|)| = |x| / (1 + |x|) : by rw [abs_div, abs_of_pos H]
... < 1 : (div_lt_one H).2 (lt_one_add _) },
{ refine (strict_mono_of_odd_strict_mono_on_nonneg _ _).cod_restrict _,
{ intro x, simp only [abs_neg, neg_div] },
{ rintros x (hx : 0 ≤ x) y (hy : 0 ≤ y) hxy,
simp [abs_of_nonneg, mul_add, mul_comm x y, div_lt_div_iff,
hx.trans_lt (lt_one_add _), hy.trans_lt (lt_one_add _), *] } },
{ refine λ x, subtype.ext _,
have : 0 < 1 - |(x : k)|, from sub_pos.2 (abs_lt.2 x.2),
field_simp [abs_div, this.ne', abs_of_pos this] }
end
section Ixx
variables {α β : Type*} [preorder α] [preorder β] {f g : α → β}
lemma antitone_Ici : antitone (Ici : α → set α) := λ _ _, Ici_subset_Ici.2
lemma monotone_Iic : monotone (Iic : α → set α) := λ _ _, Iic_subset_Iic.2
lemma antitone_Ioi : antitone (Ioi : α → set α) := λ _ _, Ioi_subset_Ioi
lemma monotone_Iio : monotone (Iio : α → set α) := λ _ _, Iio_subset_Iio
protected lemma monotone.Ici (hf : monotone f) : antitone (λ x, Ici (f x)) :=
antitone_Ici.comp_monotone hf
protected lemma antitone.Ici (hf : antitone f) : monotone (λ x, Ici (f x)) :=
antitone_Ici.comp hf
protected lemma monotone.Iic (hf : monotone f) : monotone (λ x, Iic (f x)) :=
monotone_Iic.comp hf
protected lemma antitone.Iic (hf : antitone f) : antitone (λ x, Iic (f x)) :=
monotone_Iic.comp_antitone hf
protected lemma monotone.Ioi (hf : monotone f) : antitone (λ x, Ioi (f x)) :=
antitone_Ioi.comp_monotone hf
protected lemma antitone.Ioi (hf : antitone f) : monotone (λ x, Ioi (f x)) :=
antitone_Ioi.comp hf
protected lemma monotone.Iio (hf : monotone f) : monotone (λ x, Iio (f x)) :=
monotone_Iio.comp hf
protected lemma antitone.Iio (hf : antitone f) : antitone (λ x, Iio (f x)) :=
monotone_Iio.comp_antitone hf
protected lemma monotone.Icc (hf : monotone f) (hg : antitone g) :
antitone (λ x, Icc (f x) (g x)) :=
hf.Ici.inter hg.Iic
protected lemma antitone.Icc (hf : antitone f) (hg : monotone g) :
monotone (λ x, Icc (f x) (g x)) :=
hf.Ici.inter hg.Iic
protected lemma monotone.Ico (hf : monotone f) (hg : antitone g) :
antitone (λ x, Ico (f x) (g x)) :=
hf.Ici.inter hg.Iio
protected lemma antitone.Ico (hf : antitone f) (hg : monotone g) :
monotone (λ x, Ico (f x) (g x)) :=
hf.Ici.inter hg.Iio
protected lemma monotone.Ioc (hf : monotone f) (hg : antitone g) :
antitone (λ x, Ioc (f x) (g x)) :=
hf.Ioi.inter hg.Iic
protected lemma antitone.Ioc (hf : antitone f) (hg : monotone g) :
monotone (λ x, Ioc (f x) (g x)) :=
hf.Ioi.inter hg.Iic
protected lemma monotone.Ioo (hf : monotone f) (hg : antitone g) :
antitone (λ x, Ioo (f x) (g x)) :=
hf.Ioi.inter hg.Iio
protected lemma antitone.Ioo (hf : antitone f) (hg : monotone g) :
monotone (λ x, Ioo (f x) (g x)) :=
hf.Ioi.inter hg.Iio
end Ixx
section Union
variables {α β : Type*} [semilattice_sup α] [linear_order β] {f g : α → β} {a b : β}
lemma Union_Ioo_of_mono_of_is_glb_of_is_lub (hf : antitone f) (hg : monotone g)
(ha : is_glb (range f) a) (hb : is_lub (range g) b) :
(⋃ x, Ioo (f x) (g x)) = Ioo a b :=
calc (⋃ x, Ioo (f x) (g x)) = (⋃ x, Ioi (f x)) ∩ ⋃ x, Iio (g x) :
Union_inter_of_monotone hf.Ioi hg.Iio
... = Ioi a ∩ Iio b : congr_arg2 (∩) ha.Union_Ioi_eq hb.Union_Iio_eq
end Union
|
a79e4cd7808db1c3805dede9b3c3a16616a5724f | 94e33a31faa76775069b071adea97e86e218a8ee | /src/logic/equiv/set.lean | 2ae28c23b6f059b9e00a120970e9ad604dae8c2d | [
"Apache-2.0"
] | permissive | urkud/mathlib | eab80095e1b9f1513bfb7f25b4fa82fa4fd02989 | 6379d39e6b5b279df9715f8011369a301b634e41 | refs/heads/master | 1,658,425,342,662 | 1,658,078,703,000 | 1,658,078,703,000 | 186,910,338 | 0 | 0 | Apache-2.0 | 1,568,512,083,000 | 1,557,958,709,000 | Lean | UTF-8 | Lean | false | false | 25,601 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Mario Carneiro
-/
import data.set.function
import logic.equiv.basic
/-!
# Equivalences and sets
In this file we provide lemmas linking equivalences to sets.
Some notable definitions are:
* `equiv.of_injective`: an injective function is (noncomputably) equivalent to its range.
* `equiv.set_congr`: two equal sets are equivalent as types.
* `equiv.set.union`: a disjoint union of sets is equivalent to their `sum`.
This file is separate from `equiv/basic` such that we do not require the full lattice structure
on sets before defining what an equivalence is.
-/
open function set
universes u v w z
variables {α : Sort u} {β : Sort v} {γ : Sort w}
namespace equiv
@[simp] lemma range_eq_univ {α : Type*} {β : Type*} (e : α ≃ β) : range e = univ :=
eq_univ_of_forall e.surjective
protected lemma image_eq_preimage {α β} (e : α ≃ β) (s : set α) : e '' s = e.symm ⁻¹' s :=
set.ext $ λ x, mem_image_iff_of_inverse e.left_inv e.right_inv
lemma _root_.set.mem_image_equiv {α β} {S : set α} {f : α ≃ β} {x : β} :
x ∈ f '' S ↔ f.symm x ∈ S :=
set.ext_iff.mp (f.image_eq_preimage S) x
/-- Alias for `equiv.image_eq_preimage` -/
lemma _root_.set.image_equiv_eq_preimage_symm {α β} (S : set α) (f : α ≃ β) :
f '' S = f.symm ⁻¹' S :=
f.image_eq_preimage S
/-- Alias for `equiv.image_eq_preimage` -/
lemma _root_.set.preimage_equiv_eq_image_symm {α β} (S : set α) (f : β ≃ α) :
f ⁻¹' S = f.symm '' S :=
(f.symm.image_eq_preimage S).symm
@[simp] protected lemma subset_image {α β} (e : α ≃ β) (s : set α) (t : set β) :
e.symm '' t ⊆ s ↔ t ⊆ e '' s :=
by rw [image_subset_iff, e.image_eq_preimage]
@[simp] protected lemma subset_image' {α β} (e : α ≃ β) (s : set α) (t : set β) :
s ⊆ e.symm '' t ↔ e '' s ⊆ t :=
calc s ⊆ e.symm '' t ↔ e.symm.symm '' s ⊆ t : by rw e.symm.subset_image
... ↔ e '' s ⊆ t : by rw e.symm_symm
@[simp] lemma symm_image_image {α β} (e : α ≃ β) (s : set α) : e.symm '' (e '' s) = s :=
e.left_inverse_symm.image_image s
lemma eq_image_iff_symm_image_eq {α β} (e : α ≃ β) (s : set α) (t : set β) :
t = e '' s ↔ e.symm '' t = s :=
(e.symm.injective.image_injective.eq_iff' (e.symm_image_image s)).symm
@[simp] lemma image_symm_image {α β} (e : α ≃ β) (s : set β) : e '' (e.symm '' s) = s :=
e.symm.symm_image_image s
@[simp] lemma image_preimage {α β} (e : α ≃ β) (s : set β) : e '' (e ⁻¹' s) = s :=
e.surjective.image_preimage s
@[simp] lemma preimage_image {α β} (e : α ≃ β) (s : set α) : e ⁻¹' (e '' s) = s :=
e.injective.preimage_image s
protected lemma image_compl {α β} (f : equiv α β) (s : set α) :
f '' sᶜ = (f '' s)ᶜ :=
image_compl_eq f.bijective
@[simp] lemma symm_preimage_preimage {α β} (e : α ≃ β) (s : set β) :
e.symm ⁻¹' (e ⁻¹' s) = s :=
e.right_inverse_symm.preimage_preimage s
@[simp] lemma preimage_symm_preimage {α β} (e : α ≃ β) (s : set α) :
e ⁻¹' (e.symm ⁻¹' s) = s :=
e.left_inverse_symm.preimage_preimage s
@[simp] lemma preimage_subset {α β} (e : α ≃ β) (s t : set β) : e ⁻¹' s ⊆ e ⁻¹' t ↔ s ⊆ t :=
e.surjective.preimage_subset_preimage_iff
@[simp] lemma image_subset {α β} (e : α ≃ β) (s t : set α) : e '' s ⊆ e '' t ↔ s ⊆ t :=
image_subset_image_iff e.injective
@[simp] lemma image_eq_iff_eq {α β} (e : α ≃ β) (s t : set α) : e '' s = e '' t ↔ s = t :=
image_eq_image e.injective
lemma preimage_eq_iff_eq_image {α β} (e : α ≃ β) (s t) : e ⁻¹' s = t ↔ s = e '' t :=
preimage_eq_iff_eq_image e.bijective
lemma eq_preimage_iff_image_eq {α β} (e : α ≃ β) (s t) : s = e ⁻¹' t ↔ e '' s = t :=
eq_preimage_iff_image_eq e.bijective
@[simp] lemma prod_comm_preimage {α β} {s : set α} {t : set β} :
equiv.prod_comm α β ⁻¹' t ×ˢ s = s ×ˢ t :=
preimage_swap_prod
lemma prod_comm_image {α β} {s : set α} {t : set β} : equiv.prod_comm α β '' s ×ˢ t = t ×ˢ s :=
image_swap_prod
@[simp]
lemma prod_assoc_preimage {α β γ} {s : set α} {t : set β} {u : set γ} :
equiv.prod_assoc α β γ ⁻¹' s ×ˢ (t ×ˢ u) = (s ×ˢ t) ×ˢ u :=
by { ext, simp [and_assoc] }
@[simp]
lemma prod_assoc_symm_preimage {α β γ} {s : set α} {t : set β} {u : set γ} :
(equiv.prod_assoc α β γ).symm ⁻¹' (s ×ˢ t) ×ˢ u = s ×ˢ (t ×ˢ u) :=
by { ext, simp [and_assoc] }
-- `@[simp]` doesn't like these lemmas, as it uses `set.image_congr'` to turn `equiv.prod_assoc`
-- into a lambda expression and then unfold it.
lemma prod_assoc_image {α β γ} {s : set α} {t : set β} {u : set γ} :
equiv.prod_assoc α β γ '' (s ×ˢ t) ×ˢ u = s ×ˢ (t ×ˢ u) :=
by simpa only [equiv.image_eq_preimage] using prod_assoc_symm_preimage
lemma prod_assoc_symm_image {α β γ} {s : set α} {t : set β} {u : set γ} :
(equiv.prod_assoc α β γ).symm '' s ×ˢ (t ×ˢ u) = (s ×ˢ t) ×ˢ u :=
by simpa only [equiv.image_eq_preimage] using prod_assoc_preimage
/-- A set `s` in `α × β` is equivalent to the sigma-type `Σ x, {y | (x, y) ∈ s}`. -/
def set_prod_equiv_sigma {α β : Type*} (s : set (α × β)) :
s ≃ Σ x : α, {y | (x, y) ∈ s} :=
{ to_fun := λ x, ⟨x.1.1, x.1.2, by simp⟩,
inv_fun := λ x, ⟨(x.1, x.2.1), x.2.2⟩,
left_inv := λ ⟨⟨x, y⟩, h⟩, rfl,
right_inv := λ ⟨x, y, h⟩, rfl }
/-- The subtypes corresponding to equal sets are equivalent. -/
@[simps apply]
def set_congr {α : Type*} {s t : set α} (h : s = t) : s ≃ t :=
subtype_equiv_prop h
/--
A set is equivalent to its image under an equivalence.
-/
-- We could construct this using `equiv.set.image e s e.injective`,
-- but this definition provides an explicit inverse.
@[simps]
def image {α β : Type*} (e : α ≃ β) (s : set α) : s ≃ e '' s :=
{ to_fun := λ x, ⟨e x.1, by simp⟩,
inv_fun := λ y, ⟨e.symm y.1, by { rcases y with ⟨-, ⟨a, ⟨m, rfl⟩⟩⟩, simpa using m, }⟩,
left_inv := λ x, by simp,
right_inv := λ y, by simp, }.
namespace set
/-- `univ α` is equivalent to `α`. -/
@[simps apply symm_apply]
protected def univ (α) : @univ α ≃ α :=
⟨coe, λ a, ⟨a, trivial⟩, λ ⟨a, _⟩, rfl, λ a, rfl⟩
/-- An empty set is equivalent to the `empty` type. -/
protected def empty (α) : (∅ : set α) ≃ empty :=
equiv_empty _
/-- An empty set is equivalent to a `pempty` type. -/
protected def pempty (α) : (∅ : set α) ≃ pempty :=
equiv_pempty _
/-- If sets `s` and `t` are separated by a decidable predicate, then `s ∪ t` is equivalent to
`s ⊕ t`. -/
protected def union' {α} {s t : set α}
(p : α → Prop) [decidable_pred p]
(hs : ∀ x ∈ s, p x)
(ht : ∀ x ∈ t, ¬ p x) : (s ∪ t : set α) ≃ s ⊕ t :=
{ to_fun := λ x, if hp : p x
then sum.inl ⟨_, x.2.resolve_right (λ xt, ht _ xt hp)⟩
else sum.inr ⟨_, x.2.resolve_left (λ xs, hp (hs _ xs))⟩,
inv_fun := λ o, match o with
| (sum.inl x) := ⟨x, or.inl x.2⟩
| (sum.inr x) := ⟨x, or.inr x.2⟩
end,
left_inv := λ ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, h]; congr,
right_inv := λ o, begin
rcases o with ⟨x, h⟩ | ⟨x, h⟩;
dsimp [union'._match_1];
[simp [hs _ h], simp [ht _ h]]
end }
/-- If sets `s` and `t` are disjoint, then `s ∪ t` is equivalent to `s ⊕ t`. -/
protected def union {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) :
(s ∪ t : set α) ≃ s ⊕ t :=
set.union' (λ x, x ∈ s) (λ _, id) (λ x xt xs, H ⟨xs, xt⟩)
lemma union_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅)
{a : (s ∪ t : set α)} (ha : ↑a ∈ s) : equiv.set.union H a = sum.inl ⟨a, ha⟩ :=
dif_pos ha
lemma union_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅)
{a : (s ∪ t : set α)} (ha : ↑a ∈ t) : equiv.set.union H a = sum.inr ⟨a, ha⟩ :=
dif_neg $ λ h, H ⟨h, ha⟩
@[simp] lemma union_symm_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅)
(a : s) : (equiv.set.union H).symm (sum.inl a) = ⟨a, subset_union_left _ _ a.2⟩ :=
rfl
@[simp] lemma union_symm_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅)
(a : t) : (equiv.set.union H).symm (sum.inr a) = ⟨a, subset_union_right _ _ a.2⟩ :=
rfl
/-- A singleton set is equivalent to a `punit` type. -/
protected def singleton {α} (a : α) : ({a} : set α) ≃ punit.{u} :=
⟨λ _, punit.star, λ _, ⟨a, mem_singleton _⟩,
λ ⟨x, h⟩, by { simp at h, subst x },
λ ⟨⟩, rfl⟩
/-- Equal sets are equivalent.
TODO: this is the same as `equiv.set_congr`! -/
@[simps apply symm_apply]
protected def of_eq {α : Type u} {s t : set α} (h : s = t) : s ≃ t :=
equiv.set_congr h
/-- If `a ∉ s`, then `insert a s` is equivalent to `s ⊕ punit`. -/
protected def insert {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) :
(insert a s : set α) ≃ s ⊕ punit.{u+1} :=
calc (insert a s : set α) ≃ ↥(s ∪ {a}) : equiv.set.of_eq (by simp)
... ≃ s ⊕ ({a} : set α) : equiv.set.union (λ x ⟨hx, hx'⟩, by simp [*] at *)
... ≃ s ⊕ punit.{u+1} : sum_congr (equiv.refl _) (equiv.set.singleton _)
@[simp] lemma insert_symm_apply_inl {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s)
(b : s) : (equiv.set.insert H).symm (sum.inl b) = ⟨b, or.inr b.2⟩ :=
rfl
@[simp] lemma insert_symm_apply_inr {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s)
(b : punit.{u+1}) : (equiv.set.insert H).symm (sum.inr b) = ⟨a, or.inl rfl⟩ :=
rfl
@[simp] lemma insert_apply_left {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) :
equiv.set.insert H ⟨a, or.inl rfl⟩ = sum.inr punit.star :=
(equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl
@[simp] lemma insert_apply_right {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s)
(b : s) : equiv.set.insert H ⟨b, or.inr b.2⟩ = sum.inl b :=
(equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl
/-- If `s : set α` is a set with decidable membership, then `s ⊕ sᶜ` is equivalent to `α`. -/
protected def sum_compl {α} (s : set α) [decidable_pred (∈ s)] : s ⊕ (sᶜ : set α) ≃ α :=
calc s ⊕ (sᶜ : set α) ≃ ↥(s ∪ sᶜ) : (equiv.set.union (by simp [set.ext_iff])).symm
... ≃ @univ α : equiv.set.of_eq (by simp)
... ≃ α : equiv.set.univ _
@[simp] lemma sum_compl_apply_inl {α : Type u} (s : set α) [decidable_pred (∈ s)] (x : s) :
equiv.set.sum_compl s (sum.inl x) = x := rfl
@[simp] lemma sum_compl_apply_inr {α : Type u} (s : set α) [decidable_pred (∈ s)] (x : sᶜ) :
equiv.set.sum_compl s (sum.inr x) = x := rfl
lemma sum_compl_symm_apply_of_mem {α : Type u} {s : set α} [decidable_pred (∈ s)] {x : α}
(hx : x ∈ s) : (equiv.set.sum_compl s).symm x = sum.inl ⟨x, hx⟩ :=
have ↑(⟨x, or.inl hx⟩ : (s ∪ sᶜ : set α)) ∈ s, from hx,
by { rw [equiv.set.sum_compl], simpa using set.union_apply_left _ this }
lemma sum_compl_symm_apply_of_not_mem {α : Type u} {s : set α} [decidable_pred (∈ s)] {x : α}
(hx : x ∉ s) : (equiv.set.sum_compl s).symm x = sum.inr ⟨x, hx⟩ :=
have ↑(⟨x, or.inr hx⟩ : (s ∪ sᶜ : set α)) ∈ sᶜ, from hx,
by { rw [equiv.set.sum_compl], simpa using set.union_apply_right _ this }
@[simp] lemma sum_compl_symm_apply {α : Type*} {s : set α} [decidable_pred (∈ s)] {x : s} :
(equiv.set.sum_compl s).symm x = sum.inl x :=
by cases x with x hx; exact set.sum_compl_symm_apply_of_mem hx
@[simp] lemma sum_compl_symm_apply_compl {α : Type*} {s : set α}
[decidable_pred (∈ s)] {x : sᶜ} : (equiv.set.sum_compl s).symm x = sum.inr x :=
by cases x with x hx; exact set.sum_compl_symm_apply_of_not_mem hx
/-- `sum_diff_subset s t` is the natural equivalence between
`s ⊕ (t \ s)` and `t`, where `s` and `t` are two sets. -/
protected def sum_diff_subset {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] :
s ⊕ (t \ s : set α) ≃ t :=
calc s ⊕ (t \ s : set α) ≃ (s ∪ (t \ s) : set α) :
(equiv.set.union (by simp [inter_diff_self])).symm
... ≃ t : equiv.set.of_eq (by { simp [union_diff_self, union_eq_self_of_subset_left h] })
@[simp] lemma sum_diff_subset_apply_inl
{α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] (x : s) :
equiv.set.sum_diff_subset h (sum.inl x) = inclusion h x := rfl
@[simp] lemma sum_diff_subset_apply_inr
{α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] (x : t \ s) :
equiv.set.sum_diff_subset h (sum.inr x) = inclusion (diff_subset t s) x := rfl
lemma sum_diff_subset_symm_apply_of_mem
{α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] {x : t} (hx : x.1 ∈ s) :
(equiv.set.sum_diff_subset h).symm x = sum.inl ⟨x, hx⟩ :=
begin
apply (equiv.set.sum_diff_subset h).injective,
simp only [apply_symm_apply, sum_diff_subset_apply_inl],
exact subtype.eq rfl,
end
lemma sum_diff_subset_symm_apply_of_not_mem
{α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] {x : t} (hx : x.1 ∉ s) :
(equiv.set.sum_diff_subset h).symm x = sum.inr ⟨x, ⟨x.2, hx⟩⟩ :=
begin
apply (equiv.set.sum_diff_subset h).injective,
simp only [apply_symm_apply, sum_diff_subset_apply_inr],
exact subtype.eq rfl,
end
/-- If `s` is a set with decidable membership, then the sum of `s ∪ t` and `s ∩ t` is equivalent
to `s ⊕ t`. -/
protected def union_sum_inter {α : Type u} (s t : set α) [decidable_pred (∈ s)] :
(s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ s ⊕ t :=
calc (s ∪ t : set α) ⊕ (s ∩ t : set α)
≃ (s ∪ t \ s : set α) ⊕ (s ∩ t : set α) : by rw [union_diff_self]
... ≃ (s ⊕ (t \ s : set α)) ⊕ (s ∩ t : set α) :
sum_congr (set.union $ subset_empty_iff.2 (inter_diff_self _ _)) (equiv.refl _)
... ≃ s ⊕ (t \ s : set α) ⊕ (s ∩ t : set α) : sum_assoc _ _ _
... ≃ s ⊕ (t \ s ∪ s ∩ t : set α) : sum_congr (equiv.refl _) begin
refine (set.union' (∉ s) _ _).symm,
exacts [λ x hx, hx.2, λ x hx, not_not_intro hx.1]
end
... ≃ s ⊕ t : by { rw (_ : t \ s ∪ s ∩ t = t), rw [union_comm, inter_comm, inter_union_diff] }
/-- Given an equivalence `e₀` between sets `s : set α` and `t : set β`, the set of equivalences
`e : α ≃ β` such that `e ↑x = ↑(e₀ x)` for each `x : s` is equivalent to the set of equivalences
between `sᶜ` and `tᶜ`. -/
protected def compl {α : Type u} {β : Type v} {s : set α} {t : set β} [decidable_pred (∈ s)]
[decidable_pred (∈ t)] (e₀ : s ≃ t) :
{e : α ≃ β // ∀ x : s, e x = e₀ x} ≃ ((sᶜ : set α) ≃ (tᶜ : set β)) :=
{ to_fun := λ e, subtype_equiv e
(λ a, not_congr $ iff.symm $ maps_to.mem_iff
(maps_to_iff_exists_map_subtype.2 ⟨e₀, e.2⟩)
(surj_on.maps_to_compl (surj_on_iff_exists_map_subtype.2
⟨t, e₀, subset.refl t, e₀.surjective, e.2⟩) e.1.injective)),
inv_fun := λ e₁,
subtype.mk
(calc α ≃ s ⊕ (sᶜ : set α) : (set.sum_compl s).symm
... ≃ t ⊕ (tᶜ : set β) : e₀.sum_congr e₁
... ≃ β : set.sum_compl t)
(λ x, by simp only [sum.map_inl, trans_apply, sum_congr_apply,
set.sum_compl_apply_inl, set.sum_compl_symm_apply]),
left_inv := λ e,
begin
ext x,
by_cases hx : x ∈ s,
{ simp only [set.sum_compl_symm_apply_of_mem hx, ←e.prop ⟨x, hx⟩,
sum.map_inl, sum_congr_apply, trans_apply,
subtype.coe_mk, set.sum_compl_apply_inl] },
{ simp only [set.sum_compl_symm_apply_of_not_mem hx, sum.map_inr,
subtype_equiv_apply, set.sum_compl_apply_inr, trans_apply,
sum_congr_apply, subtype.coe_mk] },
end,
right_inv := λ e, equiv.ext $ λ x, by simp only [sum.map_inr, subtype_equiv_apply,
set.sum_compl_apply_inr, function.comp_app, sum_congr_apply, equiv.coe_trans,
subtype.coe_eta, subtype.coe_mk, set.sum_compl_symm_apply_compl] }
/-- The set product of two sets is equivalent to the type product of their coercions to types. -/
protected def prod {α β} (s : set α) (t : set β) :
↥(s ×ˢ t) ≃ s × t :=
@subtype_prod_equiv_prod α β s t
/-- The set `set.pi set.univ s` is equivalent to `Π a, s a`. -/
@[simps] protected def univ_pi {α : Type*} {β : α → Type*} (s : Π a, set (β a)) :
pi univ s ≃ Π a, s a :=
{ to_fun := λ f a, ⟨(f : Π a, β a) a, f.2 a (mem_univ a)⟩,
inv_fun := λ f, ⟨λ a, f a, λ a ha, (f a).2⟩,
left_inv := λ ⟨f, hf⟩, by { ext a, refl },
right_inv := λ f, by { ext a, refl } }
/-- If a function `f` is injective on a set `s`, then `s` is equivalent to `f '' s`. -/
protected noncomputable def image_of_inj_on {α β} (f : α → β) (s : set α) (H : inj_on f s) :
s ≃ (f '' s) :=
⟨λ p, ⟨f p, mem_image_of_mem f p.2⟩,
λ p, ⟨classical.some p.2, (classical.some_spec p.2).1⟩,
λ ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).1 h
(classical.some_spec (mem_image_of_mem f h)).2),
λ ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩
/-- If `f` is an injective function, then `s` is equivalent to `f '' s`. -/
@[simps apply]
protected noncomputable def image {α β} (f : α → β) (s : set α) (H : injective f) : s ≃ (f '' s) :=
equiv.set.image_of_inj_on f s (H.inj_on s)
@[simp] protected lemma image_symm_apply {α β} (f : α → β) (s : set α) (H : injective f)
(x : α) (h : x ∈ s) :
(set.image f s H).symm ⟨f x, ⟨x, ⟨h, rfl⟩⟩⟩ = ⟨x, h⟩ :=
begin
apply (set.image f s H).injective,
simp [(set.image f s H).apply_symm_apply],
end
lemma image_symm_preimage {α β} {f : α → β} (hf : injective f) (u s : set α) :
(λ x, (set.image f s hf).symm x : f '' s → α) ⁻¹' u = coe ⁻¹' (f '' u) :=
begin
ext ⟨b, a, has, rfl⟩,
have : ∀(h : ∃a', a' ∈ s ∧ a' = a), classical.some h = a := λ h, (classical.some_spec h).2,
simp [equiv.set.image, equiv.set.image_of_inj_on, hf.eq_iff, this],
end
/-- If `α` is equivalent to `β`, then `set α` is equivalent to `set β`. -/
@[simps]
protected def congr {α β : Type*} (e : α ≃ β) : set α ≃ set β :=
⟨λ s, e '' s, λ t, e.symm '' t, symm_image_image e, symm_image_image e.symm⟩
/-- The set `{x ∈ s | t x}` is equivalent to the set of `x : s` such that `t x`. -/
protected def sep {α : Type u} (s : set α) (t : α → Prop) :
({ x ∈ s | t x } : set α) ≃ { x : s | t x } :=
(equiv.subtype_subtype_equiv_subtype_inter s t).symm
/-- The set `𝒫 S := {x | x ⊆ S}` is equivalent to the type `set S`. -/
protected def powerset {α} (S : set α) : 𝒫 S ≃ set S :=
{ to_fun := λ x : 𝒫 S, coe ⁻¹' (x : set α),
inv_fun := λ x : set S, ⟨coe '' x, by rintro _ ⟨a : S, _, rfl⟩; exact a.2⟩,
left_inv := λ x, by ext y; exact ⟨λ ⟨⟨_, _⟩, h, rfl⟩, h, λ h, ⟨⟨_, x.2 h⟩, h, rfl⟩⟩,
right_inv := λ x, by ext; simp }
/--
If `s` is a set in `range f`,
then its image under `range_splitting f` is in bijection (via `f`) with `s`.
-/
@[simps]
noncomputable def range_splitting_image_equiv {α β : Type*} (f : α → β) (s : set (range f)) :
range_splitting f '' s ≃ s :=
{ to_fun := λ x, ⟨⟨f x, by simp⟩,
(by { rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩, simpa [apply_range_splitting f] using m, })⟩,
inv_fun := λ x, ⟨range_splitting f x, ⟨x, ⟨x.2, rfl⟩⟩⟩,
left_inv := λ x, by { rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩, simp [apply_range_splitting f] },
right_inv := λ x, by simp [apply_range_splitting f], }
end set
/-- If `f : α → β` has a left-inverse when `α` is nonempty, then `α` is computably equivalent to the
range of `f`.
While awkward, the `nonempty α` hypothesis on `f_inv` and `hf` allows this to be used when `α` is
empty too. This hypothesis is absent on analogous definitions on stronger `equiv`s like
`linear_equiv.of_left_inverse` and `ring_equiv.of_left_inverse` as their typeclass assumptions
are already sufficient to ensure non-emptiness. -/
@[simps]
def of_left_inverse {α β : Sort*}
(f : α → β) (f_inv : nonempty α → β → α) (hf : Π h : nonempty α, left_inverse (f_inv h) f) :
α ≃ range f :=
{ to_fun := λ a, ⟨f a, a, rfl⟩,
inv_fun := λ b, f_inv (nonempty_of_exists b.2) b,
left_inv := λ a, hf ⟨a⟩ a,
right_inv := λ ⟨b, a, ha⟩, subtype.eq $ show f (f_inv ⟨a⟩ b) = b,
from eq.trans (congr_arg f $ by exact ha ▸ (hf _ a)) ha }
/-- If `f : α → β` has a left-inverse, then `α` is computably equivalent to the range of `f`.
Note that if `α` is empty, no such `f_inv` exists and so this definition can't be used, unlike
the stronger but less convenient `of_left_inverse`. -/
abbreviation of_left_inverse' {α β : Sort*}
(f : α → β) (f_inv : β → α) (hf : left_inverse f_inv f) :
α ≃ range f :=
of_left_inverse f (λ _, f_inv) (λ _, hf)
/-- If `f : α → β` is an injective function, then domain `α` is equivalent to the range of `f`. -/
@[simps apply]
noncomputable def of_injective {α β} (f : α → β) (hf : injective f) : α ≃ range f :=
equiv.of_left_inverse f
(λ h, by exactI function.inv_fun f) (λ h, by exactI function.left_inverse_inv_fun hf)
theorem apply_of_injective_symm {α β} {f : α → β} (hf : injective f) (b : range f) :
f ((of_injective f hf).symm b) = b :=
subtype.ext_iff.1 $ (of_injective f hf).apply_symm_apply b
@[simp] theorem of_injective_symm_apply {α β} {f : α → β} (hf : injective f) (a : α) :
(of_injective f hf).symm ⟨f a, ⟨a, rfl⟩⟩ = a :=
begin
apply (of_injective f hf).injective,
simp [apply_of_injective_symm hf],
end
lemma coe_of_injective_symm {α β} {f : α → β} (hf : injective f) :
((of_injective f hf).symm : range f → α) = range_splitting f :=
by { ext ⟨y, x, rfl⟩, apply hf, simp [apply_range_splitting f] }
@[simp] lemma self_comp_of_injective_symm {α β} {f : α → β} (hf : injective f) :
f ∘ ((of_injective f hf).symm) = coe :=
funext (λ x, apply_of_injective_symm hf x)
lemma of_left_inverse_eq_of_injective {α β : Type*}
(f : α → β) (f_inv : nonempty α → β → α) (hf : Π h : nonempty α, left_inverse (f_inv h) f) :
of_left_inverse f f_inv hf = of_injective f
((em (nonempty α)).elim (λ h, (hf h).injective) (λ h _ _ _, by
{ haveI : subsingleton α := subsingleton_of_not_nonempty h, simp })) :=
by { ext, simp }
lemma of_left_inverse'_eq_of_injective {α β : Type*}
(f : α → β) (f_inv : β → α) (hf : left_inverse f_inv f) :
of_left_inverse' f f_inv hf = of_injective f hf.injective :=
by { ext, simp }
protected lemma set_forall_iff {α β} (e : α ≃ β) {p : set α → Prop} :
(∀ a, p a) ↔ (∀ a, p (e ⁻¹' a)) :=
e.injective.preimage_surjective.forall
lemma preimage_pi_equiv_pi_subtype_prod_symm_pi {α : Type*} {β : α → Type*}
(p : α → Prop) [decidable_pred p] (s : Π i, set (β i)) :
(pi_equiv_pi_subtype_prod p β).symm ⁻¹' pi univ s =
(pi univ (λ i : {i // p i}, s i)) ×ˢ (pi univ (λ i : {i // ¬p i}, s i)) :=
begin
ext ⟨f, g⟩,
simp only [mem_preimage, mem_univ_pi, prod_mk_mem_set_prod_eq, subtype.forall,
← forall_and_distrib],
refine forall_congr (λ i, _),
dsimp only [subtype.coe_mk],
by_cases hi : p i; simp [hi]
end
/-- `sigma_fiber_equiv f` for `f : α → β` is the natural equivalence between
the type of all preimages of points under `f` and the total space `α`. -/
-- See also `equiv.sigma_fiber_equiv`.
@[simps] def sigma_preimage_equiv {α β} (f : α → β) : (Σ b, f ⁻¹' {b}) ≃ α :=
sigma_fiber_equiv f
/-- A family of equivalences between preimages of points gives an equivalence between domains. -/
-- See also `equiv.of_fiber_equiv`.
@[simps]
def of_preimage_equiv {α β γ} {f : α → γ} {g : β → γ} (e : Π c, (f ⁻¹' {c}) ≃ (g ⁻¹' {c})) :
α ≃ β :=
equiv.of_fiber_equiv e
lemma of_preimage_equiv_map {α β γ} {f : α → γ} {g : β → γ}
(e : Π c, (f ⁻¹' {c}) ≃ (g ⁻¹' {c})) (a : α) : g (of_preimage_equiv e a) = f a :=
equiv.of_fiber_equiv_map e a
end equiv
/-- If a function is a bijection between two sets `s` and `t`, then it induces an
equivalence between the types `↥s` and `↥t`. -/
noncomputable def set.bij_on.equiv {α : Type*} {β : Type*} {s : set α} {t : set β} (f : α → β)
(h : bij_on f s t) : s ≃ t :=
equiv.of_bijective _ h.bijective
/-- The composition of an updated function with an equiv on a subset can be expressed as an
updated function. -/
lemma dite_comp_equiv_update {α : Type*} {β : Sort*} {γ : Sort*} {s : set α} (e : β ≃ s)
(v : β → γ) (w : α → γ) (j : β) (x : γ) [decidable_eq β] [decidable_eq α]
[∀ j, decidable (j ∈ s)] :
(λ (i : α), if h : i ∈ s then (function.update v j x) (e.symm ⟨i, h⟩) else w i) =
function.update (λ (i : α), if h : i ∈ s then v (e.symm ⟨i, h⟩) else w i) (e j) x :=
begin
ext i,
by_cases h : i ∈ s,
{ rw [dif_pos h,
function.update_apply_equiv_apply, equiv.symm_symm, function.comp,
function.update_apply, function.update_apply,
dif_pos h],
have h_coe : (⟨i, h⟩ : s) = e j ↔ i = e j := subtype.ext_iff.trans (by rw subtype.coe_mk),
simp_rw h_coe },
{ have : i ≠ e j,
by { contrapose! h, have : (e j : α) ∈ s := (e j).2, rwa ← h at this },
simp [h, this] }
end
|
3ed364ed0e5585f8de48eccadb95a1b20669a1bf | 0845ae2ca02071debcfd4ac24be871236c01784f | /tests/lean/trust0/t1.lean | d083d257d5bf867c70937aa964456edffe69eafb | [
"Apache-2.0"
] | permissive | GaloisInc/lean4 | 74c267eb0e900bfaa23df8de86039483ecbd60b7 | 228ddd5fdcd98dd4e9c009f425284e86917938aa | refs/heads/master | 1,643,131,356,301 | 1,562,715,572,000 | 1,562,715,572,000 | 192,390,898 | 0 | 0 | null | 1,560,792,750,000 | 1,560,792,749,000 | null | UTF-8 | Lean | false | false | 28 | lean | import init.io
#print trust
|
cdff7a6c196bde94e1cb52c941dab609632c916b | 77c5b91fae1b966ddd1db969ba37b6f0e4901e88 | /src/topology/bases.lean | 48a87409ca5b98f0f4a363a942592f4ea89a6a3e | [
"Apache-2.0"
] | permissive | dexmagic/mathlib | ff48eefc56e2412429b31d4fddd41a976eb287ce | 7a5d15a955a92a90e1d398b2281916b9c41270b2 | refs/heads/master | 1,693,481,322,046 | 1,633,360,193,000 | 1,633,360,193,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 26,835 | 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 topology.continuous_on
import topology.constructions
/-!
# Bases of topologies. Countability axioms.
A topological basis on a topological space `t` is a collection of sets,
such that all open sets can be generated as unions of these sets, without the need to take
finite intersections of them. This file introduces a framework for dealing with these collections,
and also what more we can say under certain countability conditions on bases,
which are referred to as first- and second-countable.
We also briefly cover the theory of separable spaces, which are those with a countable, dense
subset. If a space is second-countable, and also has a countably generated uniformity filter
(for example, if `t` is a metric space), it will automatically be separable (and indeed, these
conditions are equivalent in this case).
## Main definitions
* `is_topological_basis s`: The topological space `t` has basis `s`.
* `separable_space α`: The topological space `t` has a countable, dense subset.
* `first_countable_topology α`: A topology in which `𝓝 x` is countably generated for every `x`.
* `second_countable_topology α`: A topology which has a topological basis which is countable.
## Main results
* `first_countable_topology.tendsto_subseq`: In a first-countable space,
cluster points are limits of subsequences.
* `second_countable_topology.is_open_Union_countable`: In a second-countable space, the union of
arbitrarily-many open sets is equal to a sub-union of only countably many of these sets.
* `second_countable_topology.countable_cover_nhds`: Consider `f : α → set α` with the property that
`f x ∈ 𝓝 x` for all `x`. Then there is some countable set `s` whose image covers the space.
## Implementation Notes
For our applications we are interested that there exists a countable basis, but we do not need the
concrete basis itself. This allows us to declare these type classes as `Prop` to use them as mixins.
### TODO:
More fine grained instances for `first_countable_topology`, `separable_space`, `t2_space`, and more
(see the comment below `subtype.second_countable_topology`.)
-/
open set filter classical
open_locale topological_space filter
noncomputable theory
namespace topological_space
universe u
variables {α : Type u} [t : topological_space α]
include t
/-- A topological basis is one that satisfies the necessary conditions so that
it suffices to take unions of the basis sets to get a topology (without taking
finite intersections as well). -/
structure is_topological_basis (s : set (set α)) : Prop :=
(exists_subset_inter : ∀t₁∈s, ∀t₂∈s, ∀ x ∈ t₁ ∩ t₂, ∃ t₃∈s, x ∈ t₃ ∧ t₃ ⊆ t₁ ∩ t₂)
(sUnion_eq : (⋃₀ s) = univ)
(eq_generate_from : t = generate_from s)
/-- If a family of sets `s` generates the topology, then nonempty intersections of finite
subcollections of `s` form a topological basis. -/
lemma is_topological_basis_of_subbasis {s : set (set α)} (hs : t = generate_from s) :
is_topological_basis ((λ f, ⋂₀ f) '' {f : set (set α) | finite f ∧ f ⊆ s ∧ (⋂₀ f).nonempty}) :=
begin
refine ⟨_, _, _⟩,
{ rintro _ ⟨t₁, ⟨hft₁, ht₁b, ht₁⟩, rfl⟩ _ ⟨t₂, ⟨hft₂, ht₂b, ht₂⟩, rfl⟩ x h,
have : ⋂₀ (t₁ ∪ t₂) = ⋂₀ t₁ ∩ ⋂₀ t₂ := sInter_union t₁ t₂,
exact ⟨_, ⟨t₁ ∪ t₂, ⟨hft₁.union hft₂, union_subset ht₁b ht₂b, this.symm ▸ ⟨x, h⟩⟩, this⟩, h,
subset.rfl⟩ },
{ rw [sUnion_image, bUnion_eq_univ_iff],
intro x, have : x ∈ ⋂₀ ∅, { rw sInter_empty, exact mem_univ x },
exact ⟨∅, ⟨finite_empty, empty_subset _, x, this⟩, this⟩ },
{ rw hs,
apply le_antisymm; apply le_generate_from,
{ rintro _ ⟨t, ⟨hft, htb, ht⟩, rfl⟩,
exact @is_open_sInter _ (generate_from s) _ hft (λ s hs, generate_open.basic _ $ htb hs) },
{ intros t ht,
rcases t.eq_empty_or_nonempty with rfl|hne, { apply @is_open_empty _ _ },
rw ← sInter_singleton t at hne ⊢,
exact generate_open.basic _ ⟨{t}, ⟨finite_singleton t, singleton_subset_iff.2 ht, hne⟩,
rfl⟩ } }
end
/-- If a family of open sets `s` is such that every open neighbourhood contains some
member of `s`, then `s` is a topological basis. -/
lemma is_topological_basis_of_open_of_nhds {s : set (set α)}
(h_open : ∀ u ∈ s, is_open u)
(h_nhds : ∀(a:α) (u : set α), a ∈ u → is_open u → ∃v ∈ s, a ∈ v ∧ v ⊆ u) :
is_topological_basis s :=
begin
refine ⟨λ t₁ ht₁ t₂ ht₂ x hx, h_nhds _ _ hx (is_open.inter (h_open _ ht₁) (h_open _ ht₂)), _, _⟩,
{ refine sUnion_eq_univ_iff.2 (λ a, _),
rcases h_nhds a univ trivial is_open_univ with ⟨u, h₁, h₂, -⟩,
exact ⟨u, h₁, h₂⟩ },
{ refine (le_generate_from h_open).antisymm (λ u hu, _),
refine (@is_open_iff_nhds α (generate_from s) u).mpr (λ a ha, _),
rcases h_nhds a u ha hu with ⟨v, hvs, hav, hvu⟩,
rw nhds_generate_from,
exact binfi_le_of_le v ⟨hav, hvs⟩ (le_principal_iff.2 hvu) }
end
/-- A set `s` is in the neighbourhood of `a` iff there is some basis set `t`, which
contains `a` and is itself contained in `s`. -/
lemma is_topological_basis.mem_nhds_iff {a : α} {s : set α} {b : set (set α)}
(hb : is_topological_basis b) : s ∈ 𝓝 a ↔ ∃t∈b, a ∈ t ∧ t ⊆ s :=
begin
change s ∈ (𝓝 a).sets ↔ ∃t∈b, a ∈ t ∧ t ⊆ s,
rw [hb.eq_generate_from, nhds_generate_from, binfi_sets_eq],
{ simp only [mem_bUnion_iff, exists_prop, mem_set_of_eq, and_assoc, and.left_comm], refl },
{ exact assume s ⟨hs₁, hs₂⟩ t ⟨ht₁, ht₂⟩,
have a ∈ s ∩ t, from ⟨hs₁, ht₁⟩,
let ⟨u, hu₁, hu₂, hu₃⟩ := hb.1 _ hs₂ _ ht₂ _ this in
⟨u, ⟨hu₂, hu₁⟩, le_principal_iff.2 (subset.trans hu₃ (inter_subset_left _ _)),
le_principal_iff.2 (subset.trans hu₃ (inter_subset_right _ _))⟩ },
{ rcases eq_univ_iff_forall.1 hb.sUnion_eq a with ⟨i, h1, h2⟩,
exact ⟨i, h2, h1⟩ }
end
lemma is_topological_basis.nhds_has_basis {b : set (set α)} (hb : is_topological_basis b) {a : α} :
(𝓝 a).has_basis (λ t : set α, t ∈ b ∧ a ∈ t) (λ t, t) :=
⟨λ s, hb.mem_nhds_iff.trans $ by simp only [exists_prop, and_assoc]⟩
protected lemma is_topological_basis.is_open {s : set α} {b : set (set α)}
(hb : is_topological_basis b) (hs : s ∈ b) : is_open s :=
by { rw hb.eq_generate_from, exact generate_open.basic s hs }
lemma is_topological_basis.exists_subset_of_mem_open {b : set (set α)}
(hb : is_topological_basis b) {a:α} {u : set α} (au : a ∈ u)
(ou : is_open u) : ∃v ∈ b, a ∈ v ∧ v ⊆ u :=
hb.mem_nhds_iff.1 $ is_open.mem_nhds ou au
/-- Any open set is the union of the basis sets contained in it. -/
lemma is_topological_basis.open_eq_sUnion' {B : set (set α)}
(hB : is_topological_basis B) {u : set α} (ou : is_open u) :
u = ⋃₀ {s ∈ B | s ⊆ u} :=
ext $ λ a,
⟨λ ha, let ⟨b, hb, ab, bu⟩ := hB.exists_subset_of_mem_open ha ou in ⟨b, ⟨hb, bu⟩, ab⟩,
λ ⟨b, ⟨hb, bu⟩, ab⟩, bu ab⟩
lemma is_topological_basis.open_eq_sUnion {B : set (set α)}
(hB : is_topological_basis B) {u : set α} (ou : is_open u) :
∃ S ⊆ B, u = ⋃₀ S :=
⟨{s ∈ B | s ⊆ u}, λ s h, h.1, hB.open_eq_sUnion' ou⟩
lemma is_topological_basis.open_eq_Union {B : set (set α)}
(hB : is_topological_basis B) {u : set α} (ou : is_open u) :
∃ (β : Type u) (f : β → set α), u = (⋃ i, f i) ∧ ∀ i, f i ∈ B :=
⟨↥{s ∈ B | s ⊆ u}, coe, by { rw ← sUnion_eq_Union, apply hB.open_eq_sUnion' ou }, λ s, and.left s.2⟩
/-- A point `a` is in the closure of `s` iff all basis sets containing `a` intersect `s`. -/
lemma is_topological_basis.mem_closure_iff {b : set (set α)} (hb : is_topological_basis b)
{s : set α} {a : α} :
a ∈ closure s ↔ ∀ o ∈ b, a ∈ o → (o ∩ s).nonempty :=
(mem_closure_iff_nhds_basis' hb.nhds_has_basis).trans $ by simp only [and_imp]
/-- A set is dense iff it has non-trivial intersection with all basis sets. -/
lemma is_topological_basis.dense_iff {b : set (set α)} (hb : is_topological_basis b) {s : set α} :
dense s ↔ ∀ o ∈ b, set.nonempty o → (o ∩ s).nonempty :=
begin
simp only [dense, hb.mem_closure_iff],
exact ⟨λ h o hb ⟨a, ha⟩, h a o hb ha, λ h a o hb ha, h o hb ⟨a, ha⟩⟩
end
lemma is_topological_basis_opens : is_topological_basis { U : set α | is_open U } :=
is_topological_basis_of_open_of_nhds (by tauto) (by tauto)
protected lemma is_topological_basis.prod {β} [topological_space β] {B₁ : set (set α)}
{B₂ : set (set β)} (h₁ : is_topological_basis B₁) (h₂ : is_topological_basis B₂) :
is_topological_basis (image2 set.prod B₁ B₂) :=
begin
refine is_topological_basis_of_open_of_nhds _ _,
{ rintro _ ⟨u₁, u₂, hu₁, hu₂, rfl⟩,
exact (h₁.is_open hu₁).prod (h₂.is_open hu₂) },
{ rintro ⟨a, b⟩ u hu uo,
rcases (h₁.nhds_has_basis.prod_nhds h₂.nhds_has_basis).mem_iff.1 (is_open.mem_nhds uo hu)
with ⟨⟨s, t⟩, ⟨⟨hs, ha⟩, ht, hb⟩, hu⟩,
exact ⟨s.prod t, mem_image2_of_mem hs ht, ⟨ha, hb⟩, hu⟩ }
end
protected lemma is_topological_basis.inducing {β} [topological_space β]
{f : α → β} {T : set (set β)} (hf : inducing f) (h : is_topological_basis T) :
is_topological_basis (image (preimage f) T) :=
begin
refine is_topological_basis_of_open_of_nhds _ _,
{ rintros _ ⟨V, hV, rfl⟩,
rwa hf.is_open_iff,
refine ⟨V, h.is_open hV, rfl⟩ },
{ intros a U ha hU,
rw hf.is_open_iff at hU,
obtain ⟨V, hV, rfl⟩ := hU,
obtain ⟨S, hS, rfl⟩ := h.open_eq_sUnion hV,
obtain ⟨W, hW, ha⟩ := ha,
refine ⟨f ⁻¹' W, ⟨_, hS hW, rfl⟩, ha, set.preimage_mono $ set.subset_sUnion_of_mem hW⟩ }
end
lemma is_topological_basis_of_cover {ι} {U : ι → set α} (Uo : ∀ i, is_open (U i))
(Uc : (⋃ i, U i) = univ) {b : Π i, set (set (U i))} (hb : ∀ i, is_topological_basis (b i)) :
is_topological_basis (⋃ i : ι, image (coe : U i → α) '' (b i)) :=
begin
refine is_topological_basis_of_open_of_nhds (λ u hu, _) _,
{ simp only [mem_Union, mem_image] at hu,
rcases hu with ⟨i, s, sb, rfl⟩,
exact (Uo i).is_open_map_subtype_coe _ ((hb i).is_open sb) },
{ intros a u ha uo,
rcases Union_eq_univ_iff.1 Uc a with ⟨i, hi⟩,
lift a to ↥(U i) using hi,
rcases (hb i).exists_subset_of_mem_open (by exact ha) (uo.preimage continuous_subtype_coe)
with ⟨v, hvb, hav, hvu⟩,
exact ⟨coe '' v, mem_Union.2 ⟨i, mem_image_of_mem _ hvb⟩, mem_image_of_mem _ hav,
image_subset_iff.2 hvu⟩ }
end
protected lemma is_topological_basis.continuous {β : Type*} [topological_space β]
{B : set (set β)} (hB : is_topological_basis B) (f : α → β) (hf : ∀ s ∈ B, is_open (f ⁻¹' s)) :
continuous f :=
begin rw hB.eq_generate_from, exact continuous_generated_from hf end
variables (α)
/-- A separable space is one with a countable dense subset, available through
`topological_space.exists_countable_dense`. If `α` is also known to be nonempty, then
`topological_space.dense_seq` provides a sequence `ℕ → α` with dense range, see
`topological_space.dense_range_dense_seq`.
If `α` is a uniform space with countably generated uniformity filter (e.g., an `emetric_space`),
then this condition is equivalent to `topological_space.second_countable_topology α`. In this case
the latter should be used as a typeclass argument in theorems because Lean can automatically deduce
`separable_space` from `second_countable_topology` but it can't deduce `second_countable_topology`
and `emetric_space`. -/
class separable_space : Prop :=
(exists_countable_dense : ∃s:set α, countable s ∧ dense s)
lemma exists_countable_dense [separable_space α] :
∃ s : set α, countable s ∧ dense s :=
separable_space.exists_countable_dense
/-- A nonempty separable space admits a sequence with dense range. Instead of running `cases` on the
conclusion of this lemma, you might want to use `topological_space.dense_seq` and
`topological_space.dense_range_dense_seq`.
If `α` might be empty, then `exists_countable_dense` is the main way to use separability of `α`. -/
lemma exists_dense_seq [separable_space α] [nonempty α] : ∃ u : ℕ → α, dense_range u :=
begin
obtain ⟨s : set α, hs, s_dense⟩ := exists_countable_dense α,
cases countable_iff_exists_surjective.mp hs with u hu,
exact ⟨u, s_dense.mono hu⟩,
end
/-- A dense sequence in a non-empty separable topological space.
If `α` might be empty, then `exists_countable_dense` is the main way to use separability of `α`. -/
def dense_seq [separable_space α] [nonempty α] : ℕ → α := classical.some (exists_dense_seq α)
/-- The sequence `dense_seq α` has dense range. -/
@[simp] lemma dense_range_dense_seq [separable_space α] [nonempty α] :
dense_range (dense_seq α) := classical.some_spec (exists_dense_seq α)
end topological_space
open topological_space
lemma is_topological_basis_pi {ι : Type*} {X : ι → Type*}
[∀ i, topological_space (X i)] {T : Π i, set (set (X i))}
(cond : ∀ i, is_topological_basis (T i)) :
is_topological_basis {S : set (Π i, X i) | ∃ (U : Π i, set (X i)) (F : finset ι),
(∀ i, i ∈ F → (U i) ∈ T i) ∧ S = (F : set ι).pi U } :=
begin
classical,
refine is_topological_basis_of_open_of_nhds _ _,
{ rintro _ ⟨U, F, h1, rfl⟩,
apply is_open_set_pi F.finite_to_set,
intros i hi,
exact is_topological_basis.is_open (cond i) (h1 i hi) },
{ intros a U ha hU,
have : U ∈ nhds a := is_open.mem_nhds hU ha,
rw [nhds_pi, filter.mem_infi] at this,
obtain ⟨F, hF, V, hV1, rfl⟩ := this,
choose U' hU' using hV1,
obtain ⟨hU1, hU2⟩ := ⟨λ i, (hU' i).1, λ i, (hU' i).2⟩,
have : ∀ j : F, ∃ (T' : set (X j)) (hT : T' ∈ T j), a j ∈ T' ∧ T' ⊆ U' j,
{ intros i,
specialize hU1 i,
rwa (cond i).mem_nhds_iff at hU1 },
choose U'' hU'' using this,
let U : Π (i : ι), set (X i) := λ i,
if hi : i ∈ F then U'' ⟨i, hi⟩ else set.univ,
refine ⟨F.pi U, ⟨U, hF.to_finset, λ i hi, _, by simp⟩, _, _⟩,
{ dsimp only [U],
rw [dif_pos],
swap, { simpa using hi },
exact (hU'' _).1 },
{ rw set.mem_pi,
intros i hi,
dsimp only [U],
rw dif_pos hi,
exact (hU'' _).2.1 },
{ intros x hx,
rintros - ⟨i, rfl⟩,
refine hU2 i ((hU'' i).2.2 _),
convert hx i i.2,
rcases i with ⟨i, p⟩,
dsimp [U],
rw dif_pos p, } },
end
lemma is_topological_basis_infi {β : Type*} {ι : Type*} {X : ι → Type*}
[t : ∀ i, topological_space (X i)] {T : Π i, set (set (X i))}
(cond : ∀ i, is_topological_basis (T i)) (f : Π i, β → X i) :
@is_topological_basis β (⨅ i, induced (f i) (t i))
{ S | ∃ (U : Π i, set (X i)) (F : finset ι),
(∀ i, i ∈ F → U i ∈ T i) ∧ S = ⋂ i (hi : i ∈ F), (f i) ⁻¹' (U i) } :=
begin
convert (is_topological_basis_pi cond).inducing (inducing_infi_to_pi _),
ext V,
split,
{ rintros ⟨U, F, h1, h2⟩,
have : (F : set ι).pi U = (⋂ (i : ι) (hi : i ∈ F),
(λ (z : Π j, X j), z i) ⁻¹' (U i)), by { ext, simp },
refine ⟨(F : set ι).pi U, ⟨U, F, h1, rfl⟩, _⟩,
rw [this, h2, set.preimage_Inter],
congr' 1,
ext1,
rw set.preimage_Inter,
refl },
{ rintros ⟨U, ⟨U, F, h1, rfl⟩, h⟩,
refine ⟨U, F, h1, _⟩,
have : (F : set ι).pi U = (⋂ (i : ι) (hi : i ∈ F),
(λ (z : Π j, X j), z i) ⁻¹' (U i)), by { ext, simp },
rw [← h, this, set.preimage_Inter],
congr' 1,
ext1,
rw set.preimage_Inter,
refl }
end
/-- If `α` is a separable space and `f : α → β` is a continuous map with dense range, then `β` is
a separable space as well. E.g., the completion of a separable uniform space is separable. -/
protected lemma dense_range.separable_space {α β : Type*} [topological_space α] [separable_space α]
[topological_space β] {f : α → β} (h : dense_range f) (h' : continuous f) :
separable_space β :=
let ⟨s, s_cnt, s_dense⟩ := exists_countable_dense α in
⟨⟨f '' s, countable.image s_cnt f, h.dense_image h' s_dense⟩⟩
lemma dense.exists_countable_dense_subset {α : Type*} [topological_space α]
{s : set α} [separable_space s] (hs : dense s) :
∃ t ⊆ s, countable t ∧ dense t :=
let ⟨t, htc, htd⟩ := exists_countable_dense s
in ⟨coe '' t, image_subset_iff.2 $ λ x _, mem_preimage.2 $ subtype.coe_prop _, htc.image coe,
hs.dense_range_coe.dense_image continuous_subtype_val htd⟩
namespace topological_space
universe u
variables (α : Type u) [t : topological_space α]
include t
/-- A first-countable space is one in which every point has a
countable neighborhood basis. -/
class first_countable_topology : Prop :=
(nhds_generated_countable : ∀a:α, (𝓝 a).is_countably_generated)
namespace first_countable_topology
variable {α}
/-- In a first-countable space, a cluster point `x` of a sequence
is the limit of some subsequence. -/
lemma tendsto_subseq [first_countable_topology α] {u : ℕ → α} {x : α}
(hx : map_cluster_pt x at_top u) :
∃ (ψ : ℕ → ℕ), (strict_mono ψ) ∧ (tendsto (u ∘ ψ) at_top (𝓝 x)) :=
(nhds_generated_countable x).subseq_tendsto hx
end first_countable_topology
variables {α}
lemma is_countably_generated_nhds [first_countable_topology α] (x : α) :
is_countably_generated (𝓝 x) :=
first_countable_topology.nhds_generated_countable x
lemma is_countably_generated_nhds_within [first_countable_topology α] (x : α) (s : set α) :
is_countably_generated (𝓝[s] x) :=
(is_countably_generated_nhds x).inf_principal s
variable (α)
/-- A second-countable space is one with a countable basis. -/
class second_countable_topology : Prop :=
(is_open_generated_countable [] :
∃ b : set (set α), countable b ∧ t = topological_space.generate_from b)
variable {α}
protected lemma is_topological_basis.second_countable_topology
{b : set (set α)} (hb : is_topological_basis b) (hc : countable b) :
second_countable_topology α :=
⟨⟨b, hc, hb.eq_generate_from⟩⟩
variable (α)
lemma exists_countable_basis [second_countable_topology α] :
∃b:set (set α), countable b ∧ ∅ ∉ b ∧ is_topological_basis b :=
let ⟨b, hb₁, hb₂⟩ := second_countable_topology.is_open_generated_countable α in
let b' := (λs, ⋂₀ s) '' {s:set (set α) | finite s ∧ s ⊆ b ∧ (⋂₀ s).nonempty} in
⟨b',
((countable_set_of_finite_subset hb₁).mono
(by { simp only [← and_assoc], apply inter_subset_left })).image _,
assume ⟨s, ⟨_, _, hn⟩, hp⟩, absurd hn (not_nonempty_iff_eq_empty.2 hp),
is_topological_basis_of_subbasis hb₂⟩
/-- A countable topological basis of `α`. -/
def countable_basis [second_countable_topology α] : set (set α) :=
(exists_countable_basis α).some
lemma countable_countable_basis [second_countable_topology α] : countable (countable_basis α) :=
(exists_countable_basis α).some_spec.1
instance encodable_countable_basis [second_countable_topology α] :
encodable (countable_basis α) :=
(countable_countable_basis α).to_encodable
lemma empty_nmem_countable_basis [second_countable_topology α] : ∅ ∉ countable_basis α :=
(exists_countable_basis α).some_spec.2.1
lemma is_basis_countable_basis [second_countable_topology α] :
is_topological_basis (countable_basis α) :=
(exists_countable_basis α).some_spec.2.2
lemma eq_generate_from_countable_basis [second_countable_topology α] :
‹topological_space α› = generate_from (countable_basis α) :=
(is_basis_countable_basis α).eq_generate_from
variable {α}
lemma is_open_of_mem_countable_basis [second_countable_topology α] {s : set α}
(hs : s ∈ countable_basis α) : is_open s :=
(is_basis_countable_basis α).is_open hs
lemma nonempty_of_mem_countable_basis [second_countable_topology α] {s : set α}
(hs : s ∈ countable_basis α) : s.nonempty :=
ne_empty_iff_nonempty.1 $ ne_of_mem_of_not_mem hs $ empty_nmem_countable_basis α
variable (α)
@[priority 100] -- see Note [lower instance priority]
instance second_countable_topology.to_first_countable_topology
[second_countable_topology α] : first_countable_topology α :=
⟨λ x, has_countable_basis.is_countably_generated $
⟨(is_basis_countable_basis α).nhds_has_basis, (countable_countable_basis α).mono $
inter_subset_left _ _⟩⟩
/-- If `β` is a second-countable space, then its induced topology
via `f` on `α` is also second-countable. -/
lemma second_countable_topology_induced (β)
[t : topological_space β] [second_countable_topology β] (f : α → β) :
@second_countable_topology α (t.induced f) :=
begin
rcases second_countable_topology.is_open_generated_countable β with ⟨b, hb, eq⟩,
refine { is_open_generated_countable := ⟨preimage f '' b, hb.image _, _⟩ },
rw [eq, induced_generate_from_eq]
end
instance subtype.second_countable_topology (s : set α) [second_countable_topology α] :
second_countable_topology s :=
second_countable_topology_induced s α coe
/- TODO: more fine grained instances for first_countable_topology, separable_space, t2_space, ... -/
instance {β : Type*} [topological_space β]
[second_countable_topology α] [second_countable_topology β] : second_countable_topology (α × β) :=
((is_basis_countable_basis α).prod (is_basis_countable_basis β)).second_countable_topology $
(countable_countable_basis α).image2 (countable_countable_basis β) _
instance second_countable_topology_fintype {ι : Type*} {π : ι → Type*}
[fintype ι] [t : ∀a, topological_space (π a)] [sc : ∀a, second_countable_topology (π a)] :
second_countable_topology (∀a, π a) :=
begin
have : t = (λa, generate_from (countable_basis (π a))),
from funext (assume a, (is_basis_countable_basis (π a)).eq_generate_from),
rw this,
constructor,
refine ⟨pi univ '' pi univ (λ a, countable_basis (π a)), countable.image _ _, _⟩,
{ suffices : countable {f : Πa, set (π a) | ∀a, f a ∈ countable_basis (π a)}, { simpa [pi] },
exact countable_pi (assume i, (countable_countable_basis _)), },
rw [pi_generate_from_eq_fintype],
{ congr' 1 with f, simp [pi, eq_comm] },
exact assume a, (is_basis_countable_basis (π a)).sUnion_eq
end
@[priority 100] -- see Note [lower instance priority]
instance second_countable_topology.to_separable_space
[second_countable_topology α] : separable_space α :=
begin
choose p hp using λ s : countable_basis α, nonempty_of_mem_countable_basis s.2,
exact ⟨⟨range p, countable_range _,
(is_basis_countable_basis α).dense_iff.2 $ λ o ho _, ⟨p ⟨o, ho⟩, hp _, mem_range_self _⟩⟩⟩
end
variables {α}
/-- A countable open cover induces a second-countable topology if all open covers
are themselves second countable. -/
lemma second_countable_topology_of_countable_cover {ι} [encodable ι] {U : ι → set α}
[∀ i, second_countable_topology (U i)] (Uo : ∀ i, is_open (U i)) (hc : (⋃ i, U i) = univ) :
second_countable_topology α :=
begin
have : is_topological_basis (⋃ i, image (coe : U i → α) '' (countable_basis (U i))),
from is_topological_basis_of_cover Uo hc (λ i, is_basis_countable_basis (U i)),
exact this.second_countable_topology
(countable_Union $ λ i, (countable_countable_basis _).image _)
end
/-- In a second-countable space, an open set, given as a union of open sets,
is equal to the union of countably many of those sets. -/
lemma is_open_Union_countable [second_countable_topology α]
{ι} (s : ι → set α) (H : ∀ i, is_open (s i)) :
∃ T : set ι, countable T ∧ (⋃ i ∈ T, s i) = ⋃ i, s i :=
begin
let B := {b ∈ countable_basis α | ∃ i, b ⊆ s i},
choose f hf using λ b : B, b.2.2,
haveI : encodable B := ((countable_countable_basis α).mono (sep_subset _ _)).to_encodable,
refine ⟨_, countable_range f,
subset.antisymm (bUnion_subset_Union _ _) (sUnion_subset _)⟩,
rintro _ ⟨i, rfl⟩ x xs,
rcases (is_basis_countable_basis α).exists_subset_of_mem_open xs (H _) with ⟨b, hb, xb, bs⟩,
exact ⟨_, ⟨_, rfl⟩, _, ⟨⟨⟨_, hb, _, bs⟩, rfl⟩, rfl⟩, hf _ (by exact xb)⟩
end
lemma is_open_sUnion_countable [second_countable_topology α]
(S : set (set α)) (H : ∀ s ∈ S, is_open s) :
∃ T : set (set α), countable T ∧ T ⊆ S ∧ ⋃₀ T = ⋃₀ S :=
let ⟨T, cT, hT⟩ := is_open_Union_countable (λ s:S, s.1) (λ s, H s.1 s.2) in
⟨subtype.val '' T, cT.image _,
image_subset_iff.2 $ λ ⟨x, xs⟩ xt, xs,
by rwa [sUnion_image, sUnion_eq_Union]⟩
/-- In a topological space with second countable topology, if `f` is a function that sends each
point `x` to a neighborhood of `x`, then for some countable set `s`, the neighborhoods `f x`,
`x ∈ s`, cover the whole space. -/
lemma countable_cover_nhds [second_countable_topology α] {f : α → set α}
(hf : ∀ x, f x ∈ 𝓝 x) : ∃ s : set α, countable s ∧ (⋃ x ∈ s, f x) = univ :=
begin
rcases is_open_Union_countable (λ x, interior (f x)) (λ x, is_open_interior) with ⟨s, hsc, hsU⟩,
suffices : (⋃ x ∈ s, interior (f x)) = univ,
from ⟨s, hsc, flip eq_univ_of_subset this (bUnion_mono $ λ _ _, interior_subset)⟩,
simp only [hsU, eq_univ_iff_forall, mem_Union],
exact λ x, ⟨x, mem_interior_iff_mem_nhds.2 (hf x)⟩
end
lemma countable_cover_nhds_within [second_countable_topology α] {f : α → set α} {s : set α}
(hf : ∀ x ∈ s, f x ∈ 𝓝[s] x) : ∃ t ⊆ s, countable t ∧ s ⊆ (⋃ x ∈ t, f x) :=
begin
have : ∀ x : s, coe ⁻¹' (f x) ∈ 𝓝 x, from λ x, preimage_coe_mem_nhds_subtype.2 (hf x x.2),
rcases countable_cover_nhds this with ⟨t, htc, htU⟩,
refine ⟨coe '' t, subtype.coe_image_subset _ _, htc.image _, λ x hx, _⟩,
simp only [bUnion_image, eq_univ_iff_forall, ← preimage_Union, mem_preimage] at htU ⊢,
exact htU ⟨x, hx⟩
end
end topological_space
open topological_space
variables {α β : Type*} [topological_space α] [topological_space β] {f : α → β}
protected lemma inducing.second_countable_topology [second_countable_topology β]
(hf : inducing f) : second_countable_topology α :=
by { rw hf.1, exact second_countable_topology_induced α β f }
protected lemma embedding.second_countable_topology [second_countable_topology β]
(hf : embedding f) : second_countable_topology α :=
hf.1.second_countable_topology
|
6b4931f7766f4b724d747a424ef0d5462eb4d35c | 4e3bf8e2b29061457a887ac8889e88fa5aa0e34c | /lean/love06_monads_demo.lean | 55ee204f44b76aa11329695e0b4fbcb14c9254d2 | [] | no_license | mukeshtiwari/logical_verification_2019 | 9f964c067a71f65eb8884743273fbeef99e6503d | 16f62717f55ed5b7b87e03ae0134791a9bef9b9a | refs/heads/master | 1,619,158,844,208 | 1,585,139,500,000 | 1,585,139,500,000 | 249,906,380 | 0 | 0 | null | 1,585,118,728,000 | 1,585,118,727,000 | null | UTF-8 | Lean | false | false | 4,524 | lean | /- LoVe Demo 6: Monads -/
import .lovelib
namespace LoVe
/- Motivating Example -/
def sum_2_5_7₁ (ns : list ℕ) : option ℕ :=
match list.nth ns 1 with
| none := none
| some n2 :=
match list.nth ns 4 with
| none := none
| some n5 :=
match list.nth ns 6 with
| none := none
| some n7 := some (n2 + n5 + n7)
end
end
end
def bind_option {α : Type} {β : Type} :
option α → (α → option β) → option β
| none f := none
| (some a) f := f a
def sum_2_5_7₂ (ns : list ℕ) : option ℕ :=
bind_option (list.nth ns 1)
(λn2, bind_option (list.nth ns 4)
(λn5, bind_option (list.nth ns 6)
(λn7, some (n2 + n5 + n7))))
#check bind
def sum_2_5_7₃ (ns : list ℕ) : option ℕ :=
bind (list.nth ns 1)
(λn2, bind (list.nth ns 4)
(λn5, bind (list.nth ns 6)
(λn7, some (n2 + n5 + n7))))
#check (>>=)
def sum_2_5_7₄ (ns : list ℕ) : option ℕ :=
list.nth ns 1 >>=
λn2, list.nth ns 4 >>=
λn5, list.nth ns 6 >>=
λn7, some (n2 + n5 + n7)
def sum_2_5_7₅ (ns : list ℕ) : option ℕ :=
do n2 ← list.nth ns 1,
do n5 ← list.nth ns 4,
do n7 ← list.nth ns 6,
some (n2 + n5 + n7)
def sum_2_5_7₆ (ns : list ℕ) : option ℕ :=
do
n2 ← list.nth ns 1,
n5 ← list.nth ns 4,
n7 ← list.nth ns 6,
some (n2 + n5 + n7)
/- A Type Class of Monads -/
class lawful_monad (m : Type → Type)
extends has_bind m, has_pure m : Type 1 :=
(pure_bind {α β : Type} (a : α) (f : α → m β) :
(pure a >>= f) = f a)
(bind_pure {α : Type} (ma : m α) :
ma >>= pure = ma)
(bind_assoc {α β γ : Type} (f : α → m β) (g : β → m γ)
(ma : m α) :
((ma >>= f) >>= g) = (ma >>= (λa, f a >>= g)))
attribute [simp]
LoVe.lawful_monad.bind_pure
LoVe.lawful_monad.bind_assoc
LoVe.lawful_monad.pure_bind
open LoVe.lawful_monad
#print monad
#print is_lawful_monad
/- The Option Monad -/
namespace option
def pure {α : Type} : α → option α :=
option.some
def bind {α β : Type} : option α → (α → option β) → option β
| none f := none
| (some a) f := f a
instance lawful_monad_option : lawful_monad option :=
{ pure := @option.pure,
bind := @option.bind,
pure_bind :=
begin
intros α β a f,
refl
end,
bind_pure :=
begin
intros α m,
cases m; refl
end,
bind_assoc :=
begin
intros α β γ f g m,
cases m; refl
end }
end option
/- The State Monad -/
namespace state
def state (σ : Type) (α : Type) :=
σ → α × σ
end state
namespace state
def read {σ : Type} : state σ σ
| s := (s, s)
def write {σ : Type} (s : σ) : state σ unit
| _ := ((), s)
def pure {σ α : Type} (a : α) : state σ α
| s := (a, s)
def bind {σ : Type} {α β : Type} (ma : state σ α)
(f : α → state σ β) : state σ β
| s :=
match ma s with
| (a, s') := f a s'
end
instance {σ : Type} : lawful_monad (state σ) :=
{ pure := @state.pure σ,
bind := @state.bind σ,
pure_bind :=
begin
intros α β a f,
apply funext,
intro s,
refl
end,
bind_pure :=
begin
intros α m,
apply funext,
intro s,
simp [bind],
cases m s,
refl
end,
bind_assoc :=
begin
intros α β γ f g m,
apply funext,
intro s,
simp [bind],
cases m s,
refl
end }
end state
namespace state
def diff_list : list ℕ → state ℕ (list ℕ)
| [] := pure []
| (n :: ns) :=
do
prev ← read,
if n < prev then
diff_list ns
else
do
write n,
ns' ← diff_list ns,
pure (n :: ns')
#eval diff_list [1, 2, 3, 2] 0
#eval diff_list [1, 2, 3, 2, 4, 5, 2] 0
end state
/- Example: Generic Iteration over a List -/
def mmap {m : Type → Type} [lawful_monad m] {α β : Type}
(f : α → m β) : list α → m (list β)
| [] := pure []
| (a :: as) :=
do
b ← f a,
bs ← mmap as,
pure (b :: bs)
lemma mmap_append {m : Type → Type} [lawful_monad m]
{α β : Type} (f : α → m β) :
∀as as' : list α, mmap f (as ++ as') =
do
bs ← mmap f as,
bs' ← mmap f as',
pure (bs ++ bs')
| [] _ := by simp [mmap]
| (a :: as) as' := by simp [mmap, mmap_append as as']
def nths {α : Type} (xss : list (list α)) (n : ℕ) :
option (list α) :=
mmap (λxs, list.nth xs n) xss
#eval nths
[[11, 12, 13, 14],
[21, 22, 23],
[31, 32, 33]] 2
end LoVe
|
26a69dda044ec043aded0a30dbc4e5e2305cb287 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/computability/ackermann.lean | 8556260af63650b576dda4201ad2a2048390ceb3 | [
"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,390 | lean | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import computability.primrec
import tactic.linarith
/-!
# Ackermann function
In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive
definition, we show that this isn't a primitive recursive function.
## Main results
- `exists_lt_ack_of_primrec`: any primitive recursive function is pointwise bounded above by `ack m`
for some `m`.
- `not_primrec₂_ack`: the two-argument Ackermann function is not primitive recursive.
## Proof approach
We very broadly adapt the proof idea from
https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any
primitive recursive `f : ℕ → ℕ`, there exists `m` such that `f n < ack m n` for all `n`. This then
implies that `λ n, ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able
to use the same bounds as in that proof though, since our approach of using pairing functions
differs from their approach of using multivariate functions.
The important bounds we show during the main inductive proof (`exists_lt_ack_of_primrec`) are the
following. Assuming `∀ n, f n < ack a n` and `∀ n, g n < ack b n`, we have:
- `∀ n, nat.mkpair (f n) (g n) < ack (max a b + 3) n`.
- `∀ n, g (f n) < ack (max a b + 2) n`.
- `∀ n, nat.elim (f n.unpair.1) (λ (y IH : ℕ), g (nat.mkpair n.unpair.1 (nat.mkpair y IH)))
n.unpair.2 < ack (max a b + 9) n`.
The last one is evidently the hardest. Using `nat.unpair_add_le`, we reduce it to the more
manageable
- `∀ m n, elim (f m) (λ (y IH : ℕ), g (nat.mkpair m (nat.mkpair y IH))) n <
ack (max a b + 9) (m + n)`.
We then prove this by induction on `n`. Our proof crucially depends on `ack_mkpair_lt`, which is
applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds
which bump up our constant to `9`.
-/
open nat
/-- The two-argument Ackermann function, defined so that
- `ack 0 n = n + 1`
- `ack (m + 1) 0 = ack m 1`
- `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`.
This is of interest as both a fast-growing function, and as an example of a recursive function that
isn't primitive recursive. -/
def ack : ℕ → ℕ → ℕ
| 0 n := n + 1
| (m + 1) 0 := ack m 1
| (m + 1) (n + 1) := ack m (ack (m + 1) n)
@[simp] theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw ack
@[simp] theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw ack
@[simp] theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw ack
@[simp] theorem ack_one (n : ℕ) : ack 1 n = n + 2 :=
begin
induction n with n IH,
{ simp },
{ simp [IH] }
end
@[simp] theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 :=
begin
induction n with n IH,
{ simp },
{ simp [IH, mul_succ] }
end
private theorem ack_three_aux (n : ℕ) : (ack 3 n : ℤ) = 2 ^ (n + 3) - 3 :=
begin
induction n with n IH,
{ simp, norm_num },
{ simp [IH, pow_succ],
rw [mul_sub, sub_add],
norm_num }
end
@[simp] theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 :=
begin
zify,
rw cast_sub,
{ exact_mod_cast ack_three_aux n },
{ have H : 3 ≤ 2 ^ 3 := by norm_num,
exact H.trans (pow_mono one_le_two $ le_add_left le_rfl) }
end
theorem ack_pos : ∀ m n, 0 < ack m n
| 0 n := by simp
| (m + 1) 0 := by { rw ack_succ_zero, apply ack_pos }
| (m + 1) (n + 1) := by { rw ack_succ_succ, apply ack_pos }
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0 n := by simp
| (m + 1) 0 := by { rw ack_succ_zero, apply one_lt_ack_succ_left }
| (m + 1) (n + 1) := by { rw ack_succ_succ, apply one_lt_ack_succ_left }
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0 n := by simp
| (m + 1) n := begin
rw ack_succ_succ,
cases exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne',
rw h,
apply one_lt_ack_succ_right
end
theorem ack_strict_mono_right : ∀ m, strict_mono (ack m)
| 0 n₁ n₂ h := by simpa using h
| (m + 1) 0 (n + 1) h := begin
rw [ack_succ_zero, ack_succ_succ],
exact ack_strict_mono_right _ (one_lt_ack_succ_left m n)
end
| (m + 1) (n₁ + 1) (n₂ + 1) h := begin
rw [ack_succ_succ, ack_succ_succ],
apply ack_strict_mono_right _ (ack_strict_mono_right _ _),
rwa add_lt_add_iff_right at h
end
theorem ack_mono_right (m : ℕ) : monotone (ack m) := (ack_strict_mono_right m).monotone
theorem ack_injective_right (m : ℕ) : function.injective (ack m) :=
(ack_strict_mono_right m).injective
@[simp] theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strict_mono_right m).lt_iff_lt
@[simp] theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strict_mono_right m).le_iff_le
@[simp] theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0 n := by simp
| (m + 1) 0 := by simpa using add_lt_ack m 1
| (m + 1) (n + 1) :=
calc (m + 1) + n + 1
≤ m + (m + n + 2) : by linarith
... < ack m (m + n + 2) : add_lt_ack _ _
... ≤ ack m (ack (m + 1) n) : ack_mono_right m $
le_of_eq_of_le (by ring_nf) $ succ_le_of_lt $ add_lt_ack (m + 1) n
... = ack (m + 1) (n + 1) : (ack_succ_succ m n).symm
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n := succ_le_of_lt (add_lt_ack m n)
theorem lt_ack_left (m n : ℕ) : m < ack m n := (self_le_add_right m n).trans_lt $ add_lt_ack m n
theorem lt_ack_right (m n : ℕ) : n < ack m n := (self_le_add_left n m).trans_lt $ add_lt_ack m n
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} n, m₁ < m₂ → ack m₁ n < ack m₂ n
| m 0 n := λ h, (nat.not_lt_zero m h).elim
| 0 (m + 1) 0 := λ h, by simpa using one_lt_ack_succ_right m 0
| 0 (m + 1) (n + 1) := λ h, begin
rw [ack_zero, ack_succ_succ],
apply lt_of_le_of_lt (le_trans _ $ add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _),
linarith
end
| (m₁ + 1) (m₂ + 1) 0 := λ h, by simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| (m₁ + 1) (m₂ + 1) (n + 1) := λ h, begin
rw [ack_succ_succ, ack_succ_succ],
exact (ack_strict_mono_left' _ $ (add_lt_add_iff_right 1).1 h).trans
(ack_strict_mono_right _ $ ack_strict_mono_left' n h)
end
theorem ack_strict_mono_left (n : ℕ) : strict_mono (λ m, ack m n) :=
λ m₁ m₂, ack_strict_mono_left' n
theorem ack_mono_left (n : ℕ) : monotone (λ m, ack m n) := (ack_strict_mono_left n).monotone
theorem ack_injective_left (n : ℕ) : function.injective (λ m, ack m n) :=
(ack_strict_mono_left n).injective
@[simp] theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strict_mono_left n).lt_iff_lt
@[simp] theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strict_mono_left n).le_iff_le
@[simp] theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans $ ack_mono_right m₂ hn
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n :=
begin
cases n,
{ simp },
{ rw [ack_succ_succ, succ_eq_add_one],
apply ack_mono_right m (le_trans _ $ add_add_one_le_ack _ n),
linarith }
end
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 :=
begin
induction n with k hk,
{ norm_num },
{ cases k,
{ norm_num },
{ rw [succ_eq_add_one, add_sq, pow_succ 2, two_mul (2 ^ _), add_tsub_assoc_of_le,
add_comm (2 ^ _), add_assoc],
{ apply add_le_add hk,
norm_num,
apply succ_le_of_lt,
rw [pow_succ, mul_lt_mul_left (zero_lt_two' ℕ)],
apply lt_two_pow },
{ rw [pow_succ, pow_succ],
linarith [one_le_pow k 2 zero_lt_two] } } }
end
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0 n := by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| (m + 1) 0 := by { rw [ack_succ_zero, ack_succ_zero], apply ack_add_one_sq_lt_ack_add_three }
| (m + 1) (n + 1) := begin
rw [ack_succ_succ, ack_succ_succ],
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ $ ack_mono_left _ _),
linarith
end
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc ack m (ack n k)
≤ ack (max m n) (ack n k) : ack_mono_left _ (le_max_left _ _)
... < ack (max m n) (ack (max m n + 1) k) : ack_strict_mono_right _ $ ack_strict_mono_left k $
lt_succ_of_le $ le_max_right m n
... = ack (max m n + 1) (k + 1) : (ack_succ_succ _ _).symm
... ≤ ack (max m n + 2) k : ack_succ_right_le_ack_succ_left _ _
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc ack m ((n + 1) ^ 2)
< ack m ((ack m n + 1) ^ 2) : ack_strict_mono_right m $
pow_lt_pow_of_lt_left (succ_lt_succ $ lt_ack_right m n) zero_lt_two
... ≤ ack m (ack (m + 3) n) : ack_mono_right m $ ack_add_one_sq_lt_ack_add_three m n
... ≤ ack (m + 2) (ack (m + 3) n) : ack_mono_left _ $ by linarith
... = ack (m + 3) (n + 1) : (ack_succ_succ _ n).symm
... ≤ ack (m + 4) n : ack_succ_right_le_ack_succ_left _ n
theorem ack_mkpair_lt (m n k : ℕ) : ack m (mkpair n k) < ack (m + 4) (max n k) :=
(ack_strict_mono_right m $ mkpair_lt_max_add_one_sq n k).trans $ ack_add_one_sq_lt_ack_add_four _ _
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : nat.primrec f) : ∃ m, ∀ n, f n < ack m n :=
begin
induction hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg,
-- Zero function:
{ exact ⟨0, ack_pos 0⟩ },
-- Successor function:
{ refine ⟨1, λ n, _⟩,
rw succ_eq_one_add,
apply add_lt_ack },
-- Left projection:
{ refine ⟨0, λ n, _⟩,
rw [ack_zero, lt_succ_iff],
exact unpair_left_le n },
-- Right projection:
{ refine ⟨0, λ n, _⟩,
rw [ack_zero, lt_succ_iff],
exact unpair_right_le n },
all_goals { cases IHf with a ha, cases IHg with b hb },
-- Pairing:
{ refine ⟨max a b + 3, λ n, (mkpair_lt_max_add_one_sq _ _).trans_le $
(nat.pow_le_pow_of_le_left (add_le_add_right _ _) 2).trans $
ack_add_one_sq_lt_ack_add_three _ _⟩,
rw max_ack_left,
exact max_le_max (ha n).le (hb n).le },
-- Composition:
{ exact ⟨max a b + 2, λ n,
(ha _).trans $ (ack_strict_mono_right a $ hb n).trans $ ack_ack_lt_ack_max_add_two a b n⟩ },
-- Primitive recursion operator:
{ -- We prove this simpler inequality first.
have : ∀ {m n}, elim (f m) (λ y IH, g $ mkpair m $ mkpair y IH) n < ack (max a b + 9) (m + n),
{ intros m n,
-- We induct on n.
induction n with n IH,
-- The base case is easy.
{ apply (ha m).trans (ack_strict_mono_left m $ (le_max_left a b).trans_lt _),
linarith },
{ -- We get rid of the first `mkpair`.
rw elim_succ,
apply (hb _).trans ((ack_mkpair_lt _ _ _).trans_le _),
-- If m is the maximum, we get a very weak inequality.
cases lt_or_le _ m with h₁ h₁,
{ rw max_eq_left h₁.le,
exact ack_le_ack (add_le_add (le_max_right a b) $ by norm_num) (self_le_add_right m _) },
rw max_eq_right h₁,
-- We get rid of the second `mkpair`.
apply (ack_mkpair_lt _ _ _).le.trans,
-- If n is the maximum, we get a very weak inequality.
cases lt_or_le _ n with h₂ h₂,
{ rw [max_eq_left h₂.le, add_assoc],
exact ack_le_ack (add_le_add (le_max_right a b) $ by norm_num)
((le_succ n).trans $ self_le_add_left _ _) },
rw max_eq_right h₂,
-- We now use the inductive hypothesis, and some simple algebraic manipulation.
apply (ack_strict_mono_right _ IH).le.trans,
rw [add_succ m, add_succ _ 8, ack_succ_succ (_ + 8), add_assoc],
exact ack_mono_left _ (add_le_add (le_max_right a b) le_rfl) } },
-- The proof is now simple.
exact ⟨max a b + 9, λ n, this.trans_le $ ack_mono_right _ $ unpair_add_le n⟩ }
end
theorem not_nat_primrec_ack_self : ¬ nat.primrec (λ n, ack n n) :=
λ h, by { cases exists_lt_ack_of_nat_primrec h with m hm, exact (hm m).false }
theorem not_primrec_ack_self : ¬ _root_.primrec (λ n, ack n n) :=
by { rw primrec.nat_iff, exact not_nat_primrec_ack_self }
/-- The Ackermann function is not primitive recursive. -/
theorem not_primrec₂_ack : ¬ primrec₂ ack :=
λ h, not_primrec_ack_self $ h.comp primrec.id primrec.id
|
376c1142499228cc3e0c5b3890fbd17bd8ee1c15 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/group_theory/free_abelian_group.lean | 97e5237b6e7f9e126b707e34bb7d1d1276b70c39 | [] | 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 | 11,224 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
Free abelian groups as abelianization of free groups.
-- TODO: rewrite in terms of finsupp
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.algebra.group.pi
import Mathlib.group_theory.free_group
import Mathlib.group_theory.abelianization
import Mathlib.PostPort
universes u v u_1 u_2 u_3
namespace Mathlib
def free_abelian_group (α : Type u) :=
additive (abelianization (free_group α))
protected instance free_abelian_group.add_comm_group (α : Type u) : add_comm_group (free_abelian_group α) :=
additive.add_comm_group
protected instance free_abelian_group.inhabited (α : Type u) : Inhabited (free_abelian_group α) :=
{ default := 0 }
namespace free_abelian_group
def of {α : Type u} (x : α) : free_abelian_group α :=
coe_fn abelianization.of (free_group.of x)
def lift {α : Type u} {β : Type v} [add_comm_group β] (f : α → β) : free_abelian_group α →+ β :=
coe_fn monoid_hom.to_additive (abelianization.lift (monoid_hom.of ⇑(free_group.to_group f)))
namespace lift
@[simp] protected theorem add {α : Type u} {β : Type v} [add_comm_group β] (f : α → β) (x : free_abelian_group α) (y : free_abelian_group α) : coe_fn (lift f) (x + y) = coe_fn (lift f) x + coe_fn (lift f) y :=
is_add_hom.map_add (⇑(lift f)) x y
@[simp] protected theorem neg {α : Type u} {β : Type v} [add_comm_group β] (f : α → β) (x : free_abelian_group α) : coe_fn (lift f) (-x) = -coe_fn (lift f) x :=
is_add_group_hom.map_neg (⇑(lift f)) x
@[simp] protected theorem sub {α : Type u} {β : Type v} [add_comm_group β] (f : α → β) (x : free_abelian_group α) (y : free_abelian_group α) : coe_fn (lift f) (x - y) = coe_fn (lift f) x - coe_fn (lift f) y := sorry
@[simp] protected theorem zero {α : Type u} {β : Type v} [add_comm_group β] (f : α → β) : coe_fn (lift f) 0 = 0 :=
is_add_group_hom.map_zero ⇑(lift f)
@[simp] protected theorem of {α : Type u} {β : Type v} [add_comm_group β] (f : α → β) (x : α) : coe_fn (lift f) (of x) = f x := sorry
protected theorem unique {α : Type u} {β : Type v} [add_comm_group β] (f : α → β) (g : free_abelian_group α →+ β) (hg : ∀ (x : α), coe_fn g (of x) = f x) {x : free_abelian_group α} : coe_fn g x = coe_fn (lift f) x :=
abelianization.lift.unique (monoid_hom.of ⇑(free_group.to_group f)) (coe_fn add_monoid_hom.to_multiplicative g)
fun (x : free_group α) =>
free_group.to_group.unique (monoid_hom.comp (coe_fn add_monoid_hom.to_multiplicative' g) abelianization.of) hg
/-- See note [partially-applied ext lemmas]. -/
protected theorem ext {α : Type u} {β : Type v} [add_comm_group β] (g : free_abelian_group α →+ β) (h : free_abelian_group α →+ β) (H : ∀ (x : α), coe_fn g (of x) = coe_fn h (of x)) : g = h := sorry
theorem map_hom {α : Type u_1} {β : Type u_2} {γ : Type u_3} [add_comm_group β] [add_comm_group γ] (a : free_abelian_group α) (f : α → β) (g : β →+ γ) : coe_fn g (coe_fn (lift f) a) = coe_fn (lift (⇑g ∘ f)) a := sorry
end lift
theorem of_injective {α : Type u} : function.injective of := sorry
/-- The bijection underlying the free-forgetful adjunction for abelian groups.-/
def hom_equiv (X : Type u_1) (G : Type u_2) [add_comm_group G] : (free_abelian_group X →+ G) ≃ (X → G) :=
equiv.mk (fun (f : free_abelian_group X →+ G) => add_monoid_hom.to_fun f ∘ of)
(fun (f : X → G) => add_monoid_hom.of ⇑(lift f)) sorry sorry
@[simp] theorem hom_equiv_apply (X : Type u_1) (G : Type u_2) [add_comm_group G] (f : free_abelian_group X →+ G) (x : X) : coe_fn (hom_equiv X G) f x = coe_fn f (of x) :=
rfl
@[simp] theorem hom_equiv_symm_apply (X : Type u_1) (G : Type u_2) [add_comm_group G] (f : X → G) (x : free_abelian_group X) : coe_fn (coe_fn (equiv.symm (hom_equiv X G)) f) x = coe_fn (lift f) x :=
rfl
protected theorem induction_on {α : Type u} {C : free_abelian_group α → Prop} (z : free_abelian_group α) (C0 : C 0) (C1 : ∀ (x : α), C (of x)) (Cn : ∀ (x : α), C (of x) → C (-of x)) (Cp : ∀ (x y : free_abelian_group α), C x → C y → C (x + y)) : C z := sorry
theorem lift.add' {α : Type u_1} {β : Type u_2} [add_comm_group β] (a : free_abelian_group α) (f : α → β) (g : α → β) : coe_fn (lift (f + g)) a = coe_fn (lift f) a + coe_fn (lift g) a := sorry
protected instance is_add_group_hom_lift' {α : Type u_1} (β : Type u_2) [add_comm_group β] (a : free_abelian_group α) : is_add_group_hom fun (f : α → β) => coe_fn (lift f) a :=
is_add_group_hom.mk
protected instance monad : Monad free_abelian_group := sorry
protected theorem induction_on' {α : Type u} {C : free_abelian_group α → Prop} (z : free_abelian_group α) (C0 : C 0) (C1 : ∀ (x : α), C (pure x)) (Cn : ∀ (x : α), C (pure x) → C (-pure x)) (Cp : ∀ (x y : free_abelian_group α), C x → C y → C (x + y)) : C z :=
free_abelian_group.induction_on z C0 C1 Cn Cp
@[simp] theorem map_pure {α : Type u} {β : Type u} (f : α → β) (x : α) : f <$> pure x = pure (f x) :=
lift.of (of ∘ f) x
@[simp] theorem map_zero {α : Type u} {β : Type u} (f : α → β) : f <$> 0 = 0 :=
lift.zero (of ∘ f)
@[simp] theorem map_add {α : Type u} {β : Type u} (f : α → β) (x : free_abelian_group α) (y : free_abelian_group α) : f <$> (x + y) = f <$> x + f <$> y :=
lift.add (of ∘ f) x y
@[simp] theorem map_neg {α : Type u} {β : Type u} (f : α → β) (x : free_abelian_group α) : f <$> (-x) = -f <$> x :=
lift.neg (of ∘ f) x
@[simp] theorem map_sub {α : Type u} {β : Type u} (f : α → β) (x : free_abelian_group α) (y : free_abelian_group α) : f <$> (x - y) = f <$> x - f <$> y :=
lift.sub (of ∘ f) x y
@[simp] theorem map_of {α : Type u} {β : Type u} (f : α → β) (y : α) : f <$> of y = of (f y) :=
rfl
/-- The additive group homomorphism `free_abelian_group α →+ free_abelian_group β` induced from a
map `α → β` -/
def map {α : Type u} {β : Type u} (f : α → β) : free_abelian_group α →+ free_abelian_group β :=
add_monoid_hom.mk' (fun (x : free_abelian_group α) => f <$> x) (map_add f)
theorem lift_comp {α : Type u_1} {β : Type u_1} {γ : Type u_2} [add_comm_group γ] (f : α → β) (g : β → γ) (x : free_abelian_group α) : coe_fn (lift (g ∘ f)) x = coe_fn (lift g) (f <$> x) := sorry
@[simp] theorem pure_bind {α : Type u} {β : Type u} (f : α → free_abelian_group β) (x : α) : pure x >>= f = f x :=
lift.of f x
@[simp] theorem zero_bind {α : Type u} {β : Type u} (f : α → free_abelian_group β) : 0 >>= f = 0 :=
lift.zero f
@[simp] theorem add_bind {α : Type u} {β : Type u} (f : α → free_abelian_group β) (x : free_abelian_group α) (y : free_abelian_group α) : x + y >>= f = (x >>= f) + (y >>= f) :=
lift.add f x y
@[simp] theorem neg_bind {α : Type u} {β : Type u} (f : α → free_abelian_group β) (x : free_abelian_group α) : -x >>= f = -(x >>= f) :=
lift.neg f x
@[simp] theorem sub_bind {α : Type u} {β : Type u} (f : α → free_abelian_group β) (x : free_abelian_group α) (y : free_abelian_group α) : x - y >>= f = (x >>= f) - (y >>= f) :=
lift.sub f x y
@[simp] theorem pure_seq {α : Type u} {β : Type u} (f : α → β) (x : free_abelian_group α) : pure f <*> x = f <$> x :=
pure_bind
(fun (_x : α → β) => (fun (α β : Type u) (f : α → β) (x : free_abelian_group α) => coe_fn (lift (of ∘ f)) x) α β _x x)
f
@[simp] theorem zero_seq {α : Type u} {β : Type u} (x : free_abelian_group α) : 0 <*> x = 0 :=
zero_bind
fun (_x : α → β) => (fun (α β : Type u) (f : α → β) (x : free_abelian_group α) => coe_fn (lift (of ∘ f)) x) α β _x x
@[simp] theorem add_seq {α : Type u} {β : Type u} (f : free_abelian_group (α → β)) (g : free_abelian_group (α → β)) (x : free_abelian_group α) : f + g <*> x = (f <*> x) + (g <*> x) :=
add_bind
(fun (_x : α → β) => (fun (α β : Type u) (f : α → β) (x : free_abelian_group α) => coe_fn (lift (of ∘ f)) x) α β _x x)
f g
@[simp] theorem neg_seq {α : Type u} {β : Type u} (f : free_abelian_group (α → β)) (x : free_abelian_group α) : -f <*> x = -(f <*> x) :=
neg_bind
(fun (_x : α → β) => (fun (α β : Type u) (f : α → β) (x : free_abelian_group α) => coe_fn (lift (of ∘ f)) x) α β _x x)
f
@[simp] theorem sub_seq {α : Type u} {β : Type u} (f : free_abelian_group (α → β)) (g : free_abelian_group (α → β)) (x : free_abelian_group α) : f - g <*> x = (f <*> x) - (g <*> x) :=
sub_bind
(fun (_x : α → β) => (fun (α β : Type u) (f : α → β) (x : free_abelian_group α) => coe_fn (lift (of ∘ f)) x) α β _x x)
f g
protected instance is_add_group_hom_seq {α : Type u} {β : Type u} (f : free_abelian_group (α → β)) : is_add_group_hom (Seq.seq f) :=
is_add_group_hom.mk
@[simp] theorem seq_zero {α : Type u} {β : Type u} (f : free_abelian_group (α → β)) : f <*> 0 = 0 :=
is_add_group_hom.map_zero (Seq.seq f)
@[simp] theorem seq_add {α : Type u} {β : Type u} (f : free_abelian_group (α → β)) (x : free_abelian_group α) (y : free_abelian_group α) : f <*> x + y = (f <*> x) + (f <*> y) :=
is_add_hom.map_add (Seq.seq f) x y
@[simp] theorem seq_neg {α : Type u} {β : Type u} (f : free_abelian_group (α → β)) (x : free_abelian_group α) : f <*> -x = -(f <*> x) :=
is_add_group_hom.map_neg (Seq.seq f) x
@[simp] theorem seq_sub {α : Type u} {β : Type u} (f : free_abelian_group (α → β)) (x : free_abelian_group α) (y : free_abelian_group α) : f <*> x - y = (f <*> x) - (f <*> y) :=
is_add_group_hom.map_sub (Seq.seq f) x y
protected instance is_lawful_monad : is_lawful_monad free_abelian_group := sorry
protected instance is_comm_applicative : is_comm_applicative free_abelian_group := sorry
protected instance semigroup (α : Type u) [monoid α] : semigroup (free_abelian_group α) :=
semigroup.mk (fun (x : free_abelian_group α) => ⇑(lift fun (x₂ : α) => coe_fn (lift fun (x₁ : α) => of (x₁ * x₂)) x))
sorry
theorem mul_def (α : Type u) [monoid α] (x : free_abelian_group α) (y : free_abelian_group α) : x * y = coe_fn (lift fun (x₂ : α) => coe_fn (lift fun (x₁ : α) => of (x₁ * x₂)) x) y :=
rfl
theorem of_mul_of (α : Type u) [monoid α] (x : α) (y : α) : of x * of y = of (x * y) :=
rfl
theorem of_mul (α : Type u) [monoid α] (x : α) (y : α) : of (x * y) = of x * of y :=
rfl
protected instance ring (α : Type u) [monoid α] : ring (free_abelian_group α) :=
ring.mk add_comm_group.add sorry add_comm_group.zero sorry sorry add_comm_group.neg add_comm_group.sub sorry sorry
semigroup.mul sorry (of 1) sorry sorry sorry sorry
theorem one_def (α : Type u) [monoid α] : 1 = of 1 :=
rfl
theorem of_one (α : Type u) [monoid α] : of 1 = 1 :=
rfl
protected instance comm_ring (α : Type u) [comm_monoid α] : comm_ring (free_abelian_group α) :=
comm_ring.mk ring.add sorry ring.zero sorry sorry ring.neg ring.sub sorry sorry ring.mul sorry ring.one sorry sorry
sorry sorry sorry
|
793a369b7e4d49628bb74e4edc046eb94d2220d0 | 969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb | /src/measure_theory/set_integral.lean | 410d4bc12845bd9c6912759ea8e4d253b7e1d294 | [
"Apache-2.0"
] | permissive | SAAluthwela/mathlib | 62044349d72dd63983a8500214736aa7779634d3 | 83a4b8b990907291421de54a78988c024dc8a552 | refs/heads/master | 1,679,433,873,417 | 1,615,998,031,000 | 1,615,998,031,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 44,940 | lean | /-
Copyright (c) 2020 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou, Yury Kudryashov
-/
import measure_theory.bochner_integration
import analysis.normed_space.indicator_function
/-!
# Set integral
In this file we prove some properties of `∫ x in s, f x ∂μ`. Recall that this notation
is defined as `∫ x, f x ∂(μ.restrict s)`. In `integral_indicator` we prove that for a measurable
function `f` and a measurable set `s` this definition coincides with another natural definition:
`∫ x, indicator s f x ∂μ = ∫ x in s, f x ∂μ`, where `indicator s f x` is equal to `f x` for `x ∈ s`
and is zero otherwise.
Since `∫ x in s, f x ∂μ` is a notation, one can rewrite or apply any theorem about `∫ x, f x ∂μ`
directly. In this file we prove some theorems about dependence of `∫ x in s, f x ∂μ` on `s`, e.g.
`integral_union`, `integral_empty`, `integral_univ`.
We also define `integrable_on f s μ := integrable f (μ.restrict s)` and prove theorems like
`integrable_on_union : integrable_on f (s ∪ t) μ ↔ integrable_on f s μ ∧ integrable_on f t μ`.
Next we define a predicate `integrable_at_filter (f : α → E) (l : filter α) (μ : measure α)`
saying that `f` is integrable at some set `s ∈ l` and prove that a measurable function is integrable
at `l` with respect to `μ` provided that `f` is bounded above at `l ⊓ μ.ae` and `μ` is finite
at `l`.
Finally, we prove a version of the
[Fundamental theorem of calculus](https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus)
for set integral, see `filter.tendsto.integral_sub_linear_is_o_ae` and its corollaries.
Namely, consider a measurably generated filter `l`, a measure `μ` finite at this filter, and
a function `f` that has a finite limit `c` at `l ⊓ μ.ae`. Then `∫ x in s, f x ∂μ = μ s • c + o(μ s)`
as `s` tends to `l.lift' powerset`, i.e. for any `ε>0` there exists `t ∈ l` such that
`∥∫ x in s, f x ∂μ - μ s • c∥ ≤ ε * μ s` whenever `s ⊆ t`. We also formulate a version of this
theorem for a locally finite measure `μ` and a function `f` continuous at a point `a`.
## Notation
We provide the following notations for expressing the integral of a function on a set :
* `∫ a in s, f a ∂μ` is `measure_theory.integral (μ.restrict s) f`
* `∫ a in s, f a` is `∫ a in s, f a ∂volume`
Note that the set notations are defined in the file `measure_theory/bochner_integration`,
but we reference them here because all theorems about set integrals are in this file.
## TODO
The file ends with over a hundred lines of commented out code. This is the old contents of this file
using the `indicator` approach to the definition of `∫ x in s, f x ∂μ`. This code should be
migrated to the new definition.
-/
noncomputable theory
open set filter topological_space measure_theory function
open_locale classical topological_space interval big_operators filter ennreal
variables {α β E F : Type*} [measurable_space α]
section piecewise
variables {μ : measure α} {s : set α} {f g : α → β}
lemma piecewise_ae_eq_restrict (hs : measurable_set s) : piecewise s f g =ᵐ[μ.restrict s] f :=
begin
rw [ae_restrict_eq hs],
exact (piecewise_eq_on s f g).eventually_eq.filter_mono inf_le_right
end
lemma piecewise_ae_eq_restrict_compl (hs : measurable_set s) :
piecewise s f g =ᵐ[μ.restrict sᶜ] g :=
begin
rw [ae_restrict_eq hs.compl],
exact (piecewise_eq_on_compl s f g).eventually_eq.filter_mono inf_le_right
end
end piecewise
section indicator_function
variables [has_zero β] {μ : measure α} {s : set α} {f : α → β}
lemma indicator_ae_eq_restrict (hs : measurable_set s) : indicator s f =ᵐ[μ.restrict s] f :=
piecewise_ae_eq_restrict hs
lemma indicator_ae_eq_restrict_compl (hs : measurable_set s) : indicator s f =ᵐ[μ.restrict sᶜ] 0 :=
piecewise_ae_eq_restrict_compl hs
end indicator_function
section
variables [measurable_space β] {l l' : filter α} {f g : α → β} {μ ν : measure α}
/-- A function `f` is measurable at filter `l` w.r.t. a measure `μ` if it is ae-measurable
w.r.t. `μ.restrict s` for some `s ∈ l`. -/
def measurable_at_filter (f : α → β) (l : filter α) (μ : measure α . volume_tac) :=
∃ s ∈ l, ae_measurable f (μ.restrict s)
@[simp] lemma measurable_at_bot {f : α → β} : measurable_at_filter f ⊥ μ :=
⟨∅, mem_bot_sets, by simp⟩
protected lemma measurable_at_filter.eventually (h : measurable_at_filter f l μ) :
∀ᶠ s in l.lift' powerset, ae_measurable f (μ.restrict s) :=
(eventually_lift'_powerset' $ λ s t, ae_measurable.mono_set).2 h
protected lemma measurable_at_filter.filter_mono (h : measurable_at_filter f l μ) (h' : l' ≤ l) :
measurable_at_filter f l' μ :=
let ⟨s, hsl, hs⟩ := h in ⟨s, h' hsl, hs⟩
protected lemma ae_measurable.measurable_at_filter (h : ae_measurable f μ) :
measurable_at_filter f l μ :=
⟨univ, univ_mem_sets, by rwa measure.restrict_univ⟩
lemma ae_measurable.measurable_at_filter_of_mem {s} (h : ae_measurable f (μ.restrict s))
(hl : s ∈ l):
measurable_at_filter f l μ :=
⟨s, hl, h⟩
protected lemma measurable.measurable_at_filter (h : measurable f) :
measurable_at_filter f l μ :=
h.ae_measurable.measurable_at_filter
end
namespace measure_theory
section normed_group
lemma has_finite_integral_restrict_of_bounded [normed_group E] {f : α → E} {s : set α}
{μ : measure α} {C} (hs : μ s < ∞) (hf : ∀ᵐ x ∂(μ.restrict s), ∥f x∥ ≤ C) :
has_finite_integral f (μ.restrict s) :=
by haveI : finite_measure (μ.restrict s) := ⟨by rwa [measure.restrict_apply_univ]⟩;
exact has_finite_integral_of_bounded hf
variables [normed_group E] [measurable_space E] {f g : α → E} {s t : set α} {μ ν : measure α}
/-- A function is `integrable_on` a set `s` if it is a measurable function and if the integral of
its pointwise norm over `s` is less than infinity. -/
def integrable_on (f : α → E) (s : set α) (μ : measure α . volume_tac) : Prop :=
integrable f (μ.restrict s)
lemma integrable_on.integrable (h : integrable_on f s μ) :
integrable f (μ.restrict s) :=
h
@[simp] lemma integrable_on_empty : integrable_on f ∅ μ :=
by simp [integrable_on, integrable_zero_measure]
@[simp] lemma integrable_on_univ : integrable_on f univ μ ↔ integrable f μ :=
by rw [integrable_on, measure.restrict_univ]
lemma integrable_on_zero : integrable_on (λ _, (0:E)) s μ := integrable_zero _ _ _
lemma integrable_on_const {C : E} : integrable_on (λ _, C) s μ ↔ C = 0 ∨ μ s < ∞ :=
integrable_const_iff.trans $ by rw [measure.restrict_apply_univ]
lemma integrable_on.mono (h : integrable_on f t ν) (hs : s ⊆ t) (hμ : μ ≤ ν) :
integrable_on f s μ :=
h.mono_measure $ measure.restrict_mono hs hμ
lemma integrable_on.mono_set (h : integrable_on f t μ) (hst : s ⊆ t) :
integrable_on f s μ :=
h.mono hst (le_refl _)
lemma integrable_on.mono_measure (h : integrable_on f s ν) (hμ : μ ≤ ν) :
integrable_on f s μ :=
h.mono (subset.refl _) hμ
lemma integrable_on.mono_set_ae (h : integrable_on f t μ) (hst : s ≤ᵐ[μ] t) :
integrable_on f s μ :=
h.integrable.mono_measure $ restrict_mono_ae hst
lemma integrable.integrable_on (h : integrable f μ) : integrable_on f s μ :=
h.mono_measure $ measure.restrict_le_self
lemma integrable.integrable_on' (h : integrable f (μ.restrict s)) : integrable_on f s μ :=
h
lemma integrable_on.left_of_union (h : integrable_on f (s ∪ t) μ) : integrable_on f s μ :=
h.mono_set $ subset_union_left _ _
lemma integrable_on.right_of_union (h : integrable_on f (s ∪ t) μ) : integrable_on f t μ :=
h.mono_set $ subset_union_right _ _
lemma integrable_on.union (hs : integrable_on f s μ) (ht : integrable_on f t μ) :
integrable_on f (s ∪ t) μ :=
(hs.add_measure ht).mono_measure $ measure.restrict_union_le _ _
@[simp] lemma integrable_on_union :
integrable_on f (s ∪ t) μ ↔ integrable_on f s μ ∧ integrable_on f t μ :=
⟨λ h, ⟨h.left_of_union, h.right_of_union⟩, λ h, h.1.union h.2⟩
@[simp] lemma integrable_on_finite_union {s : set β} (hs : finite s)
{t : β → set α} : integrable_on f (⋃ i ∈ s, t i) μ ↔ ∀ i ∈ s, integrable_on f (t i) μ :=
begin
apply hs.induction_on,
{ simp },
{ intros a s ha hs hf, simp [hf, or_imp_distrib, forall_and_distrib] }
end
@[simp] lemma integrable_on_finset_union {s : finset β} {t : β → set α} :
integrable_on f (⋃ i ∈ s, t i) μ ↔ ∀ i ∈ s, integrable_on f (t i) μ :=
integrable_on_finite_union s.finite_to_set
lemma integrable_on.add_measure (hμ : integrable_on f s μ) (hν : integrable_on f s ν) :
integrable_on f s (μ + ν) :=
by { delta integrable_on, rw measure.restrict_add, exact hμ.integrable.add_measure hν }
@[simp] lemma integrable_on_add_measure :
integrable_on f s (μ + ν) ↔ integrable_on f s μ ∧ integrable_on f s ν :=
⟨λ h, ⟨h.mono_measure (measure.le_add_right (le_refl _)),
h.mono_measure (measure.le_add_left (le_refl _))⟩,
λ h, h.1.add_measure h.2⟩
lemma ae_measurable_indicator_iff (hs : measurable_set s) :
ae_measurable f (μ.restrict s) ↔ ae_measurable (indicator s f) μ :=
begin
split,
{ assume h,
refine ⟨indicator s (h.mk f), h.measurable_mk.indicator hs, _⟩,
have A : s.indicator f =ᵐ[μ.restrict s] s.indicator (ae_measurable.mk f h) :=
(indicator_ae_eq_restrict hs).trans (h.ae_eq_mk.trans $ (indicator_ae_eq_restrict hs).symm),
have B : s.indicator f =ᵐ[μ.restrict sᶜ] s.indicator (ae_measurable.mk f h) :=
(indicator_ae_eq_restrict_compl hs).trans (indicator_ae_eq_restrict_compl hs).symm,
have : s.indicator f =ᵐ[μ.restrict s + μ.restrict sᶜ] s.indicator (ae_measurable.mk f h) :=
ae_add_measure_iff.2 ⟨A, B⟩,
simpa only [hs, measure.restrict_add_restrict_compl] using this },
{ assume h,
exact (h.mono_measure measure.restrict_le_self).congr (indicator_ae_eq_restrict hs) }
end
lemma integrable_indicator_iff (hs : measurable_set s) :
integrable (indicator s f) μ ↔ integrable_on f s μ :=
by simp [integrable_on, integrable, has_finite_integral, nnnorm_indicator_eq_indicator_nnnorm,
ennreal.coe_indicator, lintegral_indicator _ hs, ae_measurable_indicator_iff hs]
lemma integrable_on.indicator (h : integrable_on f s μ) (hs : measurable_set s) :
integrable (indicator s f) μ :=
(integrable_indicator_iff hs).2 h
/-- We say that a function `f` is *integrable at filter* `l` if it is integrable on some
set `s ∈ l`. Equivalently, it is eventually integrable on `s` in `l.lift' powerset`. -/
def integrable_at_filter (f : α → E) (l : filter α) (μ : measure α . volume_tac) :=
∃ s ∈ l, integrable_on f s μ
variables {l l' : filter α}
protected lemma integrable_at_filter.eventually (h : integrable_at_filter f l μ) :
∀ᶠ s in l.lift' powerset, integrable_on f s μ :=
by { refine (eventually_lift'_powerset' $ λ s t hst ht, _).2 h, exact ht.mono_set hst }
lemma integrable_at_filter.filter_mono (hl : l ≤ l') (hl' : integrable_at_filter f l' μ) :
integrable_at_filter f l μ :=
let ⟨s, hs, hsf⟩ := hl' in ⟨s, hl hs, hsf⟩
lemma integrable_at_filter.inf_of_left (hl : integrable_at_filter f l μ) :
integrable_at_filter f (l ⊓ l') μ :=
hl.filter_mono inf_le_left
lemma integrable_at_filter.inf_of_right (hl : integrable_at_filter f l μ) :
integrable_at_filter f (l' ⊓ l) μ :=
hl.filter_mono inf_le_right
@[simp] lemma integrable_at_filter.inf_ae_iff {l : filter α} :
integrable_at_filter f (l ⊓ μ.ae) μ ↔ integrable_at_filter f l μ :=
begin
refine ⟨_, λ h, h.filter_mono inf_le_left⟩,
rintros ⟨s, ⟨t, ht, u, hu, hs⟩, hf⟩,
refine ⟨t, ht, _⟩,
refine hf.integrable.mono_measure (λ v hv, _),
simp only [measure.restrict_apply hv],
refine measure_mono_ae (mem_sets_of_superset hu $ λ x hx, _),
exact λ ⟨hv, ht⟩, ⟨hv, hs ⟨ht, hx⟩⟩
end
alias integrable_at_filter.inf_ae_iff ↔ measure_theory.integrable_at_filter.of_inf_ae _
/-- If `μ` is a measure finite at filter `l` and `f` is a function such that its norm is bounded
above at `l`, then `f` is integrable at `l`. -/
lemma measure.finite_at_filter.integrable_at_filter {l : filter α} [is_measurably_generated l]
(hfm : measurable_at_filter f l μ) (hμ : μ.finite_at_filter l)
(hf : l.is_bounded_under (≤) (norm ∘ f)) :
integrable_at_filter f l μ :=
begin
obtain ⟨C, hC⟩ : ∃ C, ∀ᶠ s in (l.lift' powerset), ∀ x ∈ s, ∥f x∥ ≤ C,
from hf.imp (λ C hC, eventually_lift'_powerset.2 ⟨_, hC, λ t, id⟩),
rcases (hfm.eventually.and (hμ.eventually.and hC)).exists_measurable_mem_of_lift'
with ⟨s, hsl, hsm, hfm, hμ, hC⟩,
refine ⟨s, hsl, ⟨hfm, has_finite_integral_restrict_of_bounded hμ _⟩⟩,
exact C,
rw [ae_restrict_eq hsm, eventually_inf_principal],
exact eventually_of_forall hC
end
lemma measure.finite_at_filter.integrable_at_filter_of_tendsto_ae
{l : filter α} [is_measurably_generated l] (hfm : measurable_at_filter f l μ)
(hμ : μ.finite_at_filter l) {b} (hf : tendsto f (l ⊓ μ.ae) (𝓝 b)) :
integrable_at_filter f l μ :=
(hμ.inf_of_left.integrable_at_filter (hfm.filter_mono inf_le_left)
hf.norm.is_bounded_under_le).of_inf_ae
alias measure.finite_at_filter.integrable_at_filter_of_tendsto_ae ←
filter.tendsto.integrable_at_filter_ae
lemma measure.finite_at_filter.integrable_at_filter_of_tendsto {l : filter α}
[is_measurably_generated l] (hfm : measurable_at_filter f l μ) (hμ : μ.finite_at_filter l)
{b} (hf : tendsto f l (𝓝 b)) :
integrable_at_filter f l μ :=
hμ.integrable_at_filter hfm hf.norm.is_bounded_under_le
alias measure.finite_at_filter.integrable_at_filter_of_tendsto ← filter.tendsto.integrable_at_filter
variables [borel_space E] [second_countable_topology E]
lemma integrable_add [opens_measurable_space E] {f g : α → E}
(h : univ ⊆ f ⁻¹' {0} ∪ g ⁻¹' {0}) (hf : measurable f) (hg : measurable g) :
integrable (f + g) μ ↔ integrable f μ ∧ integrable g μ :=
begin
refine ⟨λ hfg, _, λ h, h.1.add h.2⟩,
rw [← indicator_add_eq_left h],
conv { congr, skip, rw [← indicator_add_eq_right h] },
rw [integrable_indicator_iff (hf (measurable_set_singleton 0)).compl],
rw [integrable_indicator_iff (hg (measurable_set_singleton 0)).compl],
exact ⟨hfg.integrable_on, hfg.integrable_on⟩
end
/-- To prove something for an arbitrary integrable function in a second countable
Borel normed group, it suffices to show that
* the property holds for (multiples of) characteristic functions;
* is closed under addition;
* the set of functions in the `L¹` space for which the property holds is closed.
* the property is closed under the almost-everywhere equal relation.
It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions
can be added once we need them (for example in `h_sum` it is only necessary to consider the sum of
a simple function with a multiple of a characteristic function and that the intersection
of their images is a subset of `{0}`).
-/
@[elab_as_eliminator]
lemma integrable.induction (P : (α → E) → Prop)
(h_ind : ∀ (c : E) ⦃s⦄, measurable_set s → μ s < ∞ → P (s.indicator (λ _, c)))
(h_sum : ∀ ⦃f g : α → E⦄, set.univ ⊆ f ⁻¹' {0} ∪ g ⁻¹' {0} → integrable f μ → integrable g μ →
P f → P g → P (f + g))
(h_closed : is_closed {f : α →₁[μ] E | P f} )
(h_ae : ∀ ⦃f g⦄, f =ᵐ[μ] g → integrable f μ → P f → P g) :
∀ ⦃f : α → E⦄ (hf : integrable f μ), P f :=
begin
have : ∀ (f : simple_func α E), integrable f μ → P f,
{ refine simple_func.induction _ _,
{ intros c s hs h, dsimp only [simple_func.coe_const, simple_func.const_zero,
piecewise_eq_indicator, simple_func.coe_zero, simple_func.coe_piecewise] at h ⊢,
by_cases hc : c = 0,
{ subst hc, convert h_ind 0 measurable_set.empty (by simp) using 1, simp [const] },
apply h_ind c hs,
have : (nnnorm c : ℝ≥0∞) * μ s < ∞,
{ have := @comp_indicator _ _ _ _ (λ x : E, (nnnorm x : ℝ≥0∞)) (const α c) s,
dsimp only at this,
have h' := h.has_finite_integral,
simpa [has_finite_integral, this, lintegral_indicator, hs] using h' },
exact ennreal.lt_top_of_mul_lt_top_right this (by simp [hc]) },
{ intros f g hfg hf hg int_fg,
rw [simple_func.coe_add, integrable_add hfg f.measurable g.measurable] at int_fg,
refine h_sum hfg int_fg.1 int_fg.2 (hf int_fg.1) (hg int_fg.2) } },
have : ∀ (f : α →₁ₛ[μ] E), P f,
{ intro f,
exact h_ae (L1.simple_func.to_simple_func_eq_to_fun f) (L1.simple_func.integrable f)
(this (L1.simple_func.to_simple_func f) (L1.simple_func.integrable f)) },
have : ∀ (f : α →₁[μ] E), P f :=
λ f, L1.simple_func.dense_range.induction_on f h_closed this,
exact λ f hf, h_ae hf.coe_fn_to_L1 (L1.integrable_coe_fn _) (this (hf.to_L1 f)),
end
variables [complete_space E] [normed_space ℝ E]
lemma set_integral_congr_ae (hs : measurable_set s) (h : ∀ᵐ x ∂μ, x ∈ s → f x = g x) :
∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ :=
integral_congr_ae ((ae_restrict_iff' hs).2 h)
lemma set_integral_congr (hs : measurable_set s) (h : eq_on f g s) :
∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ :=
set_integral_congr_ae hs $ eventually_of_forall h
lemma integral_union (hst : disjoint s t) (hs : measurable_set s) (ht : measurable_set t)
(hfs : integrable_on f s μ) (hft : integrable_on f t μ) :
∫ x in s ∪ t, f x ∂μ = ∫ x in s, f x ∂μ + ∫ x in t, f x ∂μ :=
by simp only [integrable_on, measure.restrict_union hst hs ht, integral_add_measure hfs hft]
lemma integral_empty : ∫ x in ∅, f x ∂μ = 0 := by rw [measure.restrict_empty, integral_zero_measure]
lemma integral_univ : ∫ x in univ, f x ∂μ = ∫ x, f x ∂μ := by rw [measure.restrict_univ]
lemma integral_add_compl (hs : measurable_set s) (hfi : integrable f μ) :
∫ x in s, f x ∂μ + ∫ x in sᶜ, f x ∂μ = ∫ x, f x ∂μ :=
by rw [← integral_union disjoint_compl_right hs hs.compl hfi.integrable_on hfi.integrable_on,
union_compl_self, integral_univ]
/-- For a function `f` and a measurable set `s`, the integral of `indicator s f`
over the whole space is equal to `∫ x in s, f x ∂μ` defined as `∫ x, f x ∂(μ.restrict s)`. -/
lemma integral_indicator (hs : measurable_set s) :
∫ x, indicator s f x ∂μ = ∫ x in s, f x ∂μ :=
begin
by_cases hf : ae_measurable f (μ.restrict s), swap,
{ rw integral_non_ae_measurable hf,
rw [ae_measurable_indicator_iff hs] at hf,
exact integral_non_ae_measurable hf },
by_cases hfi : integrable_on f s μ, swap,
{ rwa [integral_undef, integral_undef],
rwa integrable_indicator_iff hs },
calc ∫ x, indicator s f x ∂μ = ∫ x in s, indicator s f x ∂μ + ∫ x in sᶜ, indicator s f x ∂μ :
(integral_add_compl hs (hfi.indicator hs)).symm
... = ∫ x in s, f x ∂μ + ∫ x in sᶜ, 0 ∂μ :
congr_arg2 (+) (integral_congr_ae (indicator_ae_eq_restrict hs))
(integral_congr_ae (indicator_ae_eq_restrict_compl hs))
... = ∫ x in s, f x ∂μ : by simp
end
lemma set_integral_const (c : E) : ∫ x in s, c ∂μ = (μ s).to_real • c :=
by rw [integral_const, measure.restrict_apply_univ]
@[simp]
lemma integral_indicator_const (e : E) ⦃s : set α⦄ (s_meas : measurable_set s) :
∫ (a : α), s.indicator (λ (x : α), e) a ∂μ = (μ s).to_real • e :=
by rw [integral_indicator s_meas, ← set_integral_const]
lemma set_integral_map {β} [measurable_space β] {g : α → β} {f : β → E} {s : set β}
(hs : measurable_set s) (hf : ae_measurable f (measure.map g μ)) (hg : measurable g) :
∫ y in s, f y ∂(measure.map g μ) = ∫ x in g ⁻¹' s, f (g x) ∂μ :=
begin
rw [measure.restrict_map hg hs, integral_map hg (hf.mono_measure _)],
exact measure.map_mono g measure.restrict_le_self
end
lemma norm_set_integral_le_of_norm_le_const_ae {C : ℝ} (hs : μ s < ∞)
(hC : ∀ᵐ x ∂μ.restrict s, ∥f x∥ ≤ C) :
∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real :=
begin
rw ← measure.restrict_apply_univ at *,
haveI : finite_measure (μ.restrict s) := ⟨‹_›⟩,
exact norm_integral_le_of_norm_le_const hC
end
lemma norm_set_integral_le_of_norm_le_const_ae' {C : ℝ} (hs : μ s < ∞)
(hC : ∀ᵐ x ∂μ, x ∈ s → ∥f x∥ ≤ C) (hfm : ae_measurable f (μ.restrict s)) :
∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real :=
begin
apply norm_set_integral_le_of_norm_le_const_ae hs,
have A : ∀ᵐ (x : α) ∂μ, x ∈ s → ∥ae_measurable.mk f hfm x∥ ≤ C,
{ filter_upwards [hC, hfm.ae_mem_imp_eq_mk],
assume a h1 h2 h3,
rw [← h2 h3],
exact h1 h3 },
have B : measurable_set {x | ∥(hfm.mk f) x∥ ≤ C} := hfm.measurable_mk.norm measurable_set_Iic,
filter_upwards [hfm.ae_eq_mk, (ae_restrict_iff B).2 A],
assume a h1 h2,
rwa h1
end
lemma norm_set_integral_le_of_norm_le_const_ae'' {C : ℝ} (hs : μ s < ∞) (hsm : measurable_set s)
(hC : ∀ᵐ x ∂μ, x ∈ s → ∥f x∥ ≤ C) :
∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real :=
norm_set_integral_le_of_norm_le_const_ae hs $ by rwa [ae_restrict_eq hsm, eventually_inf_principal]
lemma norm_set_integral_le_of_norm_le_const {C : ℝ} (hs : μ s < ∞)
(hC : ∀ x ∈ s, ∥f x∥ ≤ C) (hfm : ae_measurable f (μ.restrict s)) :
∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real :=
norm_set_integral_le_of_norm_le_const_ae' hs (eventually_of_forall hC) hfm
lemma norm_set_integral_le_of_norm_le_const' {C : ℝ} (hs : μ s < ∞) (hsm : measurable_set s)
(hC : ∀ x ∈ s, ∥f x∥ ≤ C) :
∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real :=
norm_set_integral_le_of_norm_le_const_ae'' hs hsm $ eventually_of_forall hC
lemma set_integral_eq_zero_iff_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ.restrict s] f)
(hfi : integrable_on f s μ) :
∫ x in s, f x ∂μ = 0 ↔ f =ᵐ[μ.restrict s] 0 :=
integral_eq_zero_iff_of_nonneg_ae hf hfi
lemma set_integral_pos_iff_support_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ.restrict s] f)
(hfi : integrable_on f s μ) :
0 < ∫ x in s, f x ∂μ ↔ 0 < μ (support f ∩ s) :=
begin
rw [integral_pos_iff_support_of_nonneg_ae hf hfi, restrict_apply_of_null_measurable_set],
exact hfi.ae_measurable.null_measurable_set (measurable_set_singleton 0).compl
end
end normed_group
section mono
variables {μ : measure α} {f g : α → ℝ} {s : set α}
(hf : integrable_on f s μ) (hg : integrable_on g s μ)
lemma set_integral_mono_ae_restrict (h : f ≤ᵐ[μ.restrict s] g) :
∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ :=
integral_mono_ae hf hg h
lemma set_integral_mono_ae (h : f ≤ᵐ[μ] g) :
∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ :=
set_integral_mono_ae_restrict hf hg (ae_restrict_of_ae h)
lemma set_integral_mono_on (hs : measurable_set s) (h : ∀ x ∈ s, f x ≤ g x) :
∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ :=
set_integral_mono_ae_restrict hf hg
(by simp [hs, eventually_le, eventually_inf_principal, ae_of_all _ h])
lemma set_integral_mono (h : f ≤ g) :
∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ :=
integral_mono hf hg h
end mono
section nonneg
variables {μ : measure α} {f : α → ℝ} {s : set α}
lemma set_integral_nonneg_of_ae_restrict (hf : 0 ≤ᵐ[μ.restrict s] f) :
(0:ℝ) ≤ (∫ a in s, f a ∂μ) :=
integral_nonneg_of_ae hf
lemma set_integral_nonneg_of_ae (hf : 0 ≤ᵐ[μ] f) : (0:ℝ) ≤ (∫ a in s, f a ∂μ) :=
set_integral_nonneg_of_ae_restrict (ae_restrict_of_ae hf)
lemma set_integral_nonneg (hs : measurable_set s) (hf : ∀ a, a ∈ s → 0 ≤ f a) :
(0:ℝ) ≤ (∫ a in s, f a ∂μ) :=
set_integral_nonneg_of_ae_restrict ((ae_restrict_iff' hs).mpr (ae_of_all μ hf))
end nonneg
end measure_theory
open measure_theory asymptotics metric
variables {ι : Type*} [measurable_space E] [normed_group E]
/-- Fundamental theorem of calculus for set integrals: if `μ` is a measure that is finite at a
filter `l` and `f` is a measurable function that has a finite limit `b` at `l ⊓ μ.ae`, then `∫ x in
s i, f x ∂μ = μ (s i) • b + o(μ (s i))` at a filter `li` provided that `s i` tends to `l.lift'
powerset` along `li`. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the
actual statement.
Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional
argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these
arguments, `m i = (μ (s i)).to_real` is used in the output. -/
lemma filter.tendsto.integral_sub_linear_is_o_ae
[normed_space ℝ E] [second_countable_topology E] [complete_space E] [borel_space E]
{μ : measure α} {l : filter α} [l.is_measurably_generated]
{f : α → E} {b : E} (h : tendsto f (l ⊓ μ.ae) (𝓝 b))
(hfm : measurable_at_filter f l μ) (hμ : μ.finite_at_filter l)
{s : ι → set α} {li : filter ι} (hs : tendsto s li (l.lift' powerset))
(m : ι → ℝ := λ i, (μ (s i)).to_real)
(hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) :
is_o (λ i, ∫ x in s i, f x ∂μ - m i • b) m li :=
begin
suffices : is_o (λ s, ∫ x in s, f x ∂μ - (μ s).to_real • b) (λ s, (μ s).to_real)
(l.lift' powerset),
from (this.comp_tendsto hs).congr' (hsμ.mono $ λ a ha, ha ▸ rfl) hsμ,
refine is_o_iff.2 (λ ε ε₀, _),
have : ∀ᶠ s in l.lift' powerset, ∀ᶠ x in μ.ae, x ∈ s → f x ∈ closed_ball b ε :=
eventually_lift'_powerset_eventually.2 (h.eventually $ closed_ball_mem_nhds _ ε₀),
filter_upwards [hμ.eventually, (hμ.integrable_at_filter_of_tendsto_ae hfm h).eventually,
hfm.eventually, this],
simp only [mem_closed_ball, dist_eq_norm],
intros s hμs h_integrable hfm h_norm,
rw [← set_integral_const, ← integral_sub h_integrable (integrable_on_const.2 $ or.inr hμs),
real.norm_eq_abs, abs_of_nonneg ennreal.to_real_nonneg],
exact norm_set_integral_le_of_norm_le_const_ae' hμs h_norm (hfm.sub ae_measurable_const)
end
/-- Fundamental theorem of calculus for set integrals, `nhds_within` version: if `μ` is a locally
finite measure and `f` is an almost everywhere measurable function that is continuous at a point `a`
within a measurable set `t`, then `∫ x in s i, f x ∂μ = μ (s i) • f a + o(μ (s i))` at a filter `li`
provided that `s i` tends to `(𝓝[t] a).lift' powerset` along `li`. Since `μ (s i)` is an `ℝ≥0∞`
number, we use `(μ (s i)).to_real` in the actual statement.
Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional
argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these
arguments, `m i = (μ (s i)).to_real` is used in the output. -/
lemma continuous_within_at.integral_sub_linear_is_o_ae
[topological_space α] [opens_measurable_space α]
[normed_space ℝ E] [second_countable_topology E] [complete_space E] [borel_space E]
{μ : measure α} [locally_finite_measure μ] {a : α} {t : set α}
{f : α → E} (ha : continuous_within_at f t a) (ht : measurable_set t)
(hfm : measurable_at_filter f (𝓝[t] a) μ)
{s : ι → set α} {li : filter ι} (hs : tendsto s li ((𝓝[t] a).lift' powerset))
(m : ι → ℝ := λ i, (μ (s i)).to_real)
(hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) :
is_o (λ i, ∫ x in s i, f x ∂μ - m i • f a) m li :=
by haveI : (𝓝[t] a).is_measurably_generated := ht.nhds_within_is_measurably_generated _;
exact (ha.mono_left inf_le_left).integral_sub_linear_is_o_ae
hfm (μ.finite_at_nhds_within a t) hs m hsμ
/-- Fundamental theorem of calculus for set integrals, `nhds` version: if `μ` is a locally finite
measure and `f` is an almost everywhere measurable function that is continuous at a point `a`, then
`∫ x in s i, f x ∂μ = μ (s i) • f a + o(μ (s i))` at `li` provided that `s` tends to `(𝓝 a).lift'
powerset` along `li. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the
actual statement.
Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional
argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these
arguments, `m i = (μ (s i)).to_real` is used in the output. -/
lemma continuous_at.integral_sub_linear_is_o_ae
[topological_space α] [opens_measurable_space α]
[normed_space ℝ E] [second_countable_topology E] [complete_space E] [borel_space E]
{μ : measure α} [locally_finite_measure μ] {a : α}
{f : α → E} (ha : continuous_at f a) (hfm : measurable_at_filter f (𝓝 a) μ)
{s : ι → set α} {li : filter ι} (hs : tendsto s li ((𝓝 a).lift' powerset))
(m : ι → ℝ := λ i, (μ (s i)).to_real)
(hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) :
is_o (λ i, ∫ x in s i, f x ∂μ - m i • f a) m li :=
(ha.mono_left inf_le_left).integral_sub_linear_is_o_ae hfm (μ.finite_at_nhds a) hs m hsμ
/-- If a function is integrable at `𝓝[s] x` for each point `x` of a compact set `s`, then it is
integrable on `s`. -/
lemma is_compact.integrable_on_of_nhds_within [topological_space α] {μ : measure α} {s : set α}
(hs : is_compact s) {f : α → E} (hf : ∀ x ∈ s, integrable_at_filter f (𝓝[s] x) μ) :
integrable_on f s μ :=
is_compact.induction_on hs integrable_on_empty (λ s t hst ht, ht.mono_set hst)
(λ s t hs ht, hs.union ht) hf
/-- A function which is continuous on a set `s` is almost everywhere measurable with respect to
`μ.restrict s`. -/
lemma continuous_on.ae_measurable [topological_space α] [opens_measurable_space α] [borel_space E]
{f : α → E} {s : set α} {μ : measure α} (hf : continuous_on f s) (hs : measurable_set s) :
ae_measurable f (μ.restrict s) :=
begin
refine ⟨indicator s f, _, (indicator_ae_eq_restrict hs).symm⟩,
apply measurable_of_is_open,
assume t ht,
obtain ⟨u, u_open, hu⟩ : ∃ (u : set α), is_open u ∧ f ⁻¹' t ∩ s = u ∩ s :=
_root_.continuous_on_iff'.1 hf t ht,
rw [indicator_preimage, inter_comm, hu],
exact (u_open.measurable_set.inter hs).union (hs.compl.inter (measurable_const ht.measurable_set))
end
lemma continuous_on.integrable_at_nhds_within
[topological_space α] [opens_measurable_space α] [borel_space E]
{μ : measure α} [locally_finite_measure μ] {a : α} {t : set α} {f : α → E}
(hft : continuous_on f t) (ht : measurable_set t) (ha : a ∈ t) :
integrable_at_filter f (𝓝[t] a) μ :=
by haveI : (𝓝[t] a).is_measurably_generated := ht.nhds_within_is_measurably_generated _;
exact (hft a ha).integrable_at_filter ⟨_, self_mem_nhds_within, hft.ae_measurable ht⟩
(μ.finite_at_nhds_within _ _)
/-- Fundamental theorem of calculus for set integrals, `nhds_within` version: if `μ` is a locally
finite measure, `f` is continuous on a measurable set `t`, and `a ∈ t`, then `∫ x in (s i), f x ∂μ =
μ (s i) • f a + o(μ (s i))` at `li` provided that `s i` tends to `(𝓝[t] a).lift' powerset` along
`li`. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the actual statement.
Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional
argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these
arguments, `m i = (μ (s i)).to_real` is used in the output. -/
lemma continuous_on.integral_sub_linear_is_o_ae
[topological_space α] [opens_measurable_space α]
[normed_space ℝ E] [second_countable_topology E] [complete_space E] [borel_space E]
{μ : measure α} [locally_finite_measure μ] {a : α} {t : set α}
{f : α → E} (hft : continuous_on f t) (ha : a ∈ t) (ht : measurable_set t)
{s : ι → set α} {li : filter ι} (hs : tendsto s li ((𝓝[t] a).lift' powerset))
(m : ι → ℝ := λ i, (μ (s i)).to_real)
(hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) :
is_o (λ i, ∫ x in s i, f x ∂μ - m i • f a) m li :=
(hft a ha).integral_sub_linear_is_o_ae ht ⟨t, self_mem_nhds_within, hft.ae_measurable ht⟩ hs m hsμ
/-- A function `f` continuous on a compact set `s` is integrable on this set with respect to any
locally finite measure. -/
lemma continuous_on.integrable_on_compact
[topological_space α] [opens_measurable_space α] [borel_space E]
[t2_space α] {μ : measure α} [locally_finite_measure μ]
{s : set α} (hs : is_compact s) {f : α → E} (hf : continuous_on f s) :
integrable_on f s μ :=
hs.integrable_on_of_nhds_within $ λ x hx, hf.integrable_at_nhds_within hs.measurable_set hx
/-- A continuous function `f` is integrable on any compact set with respect to any locally finite
measure. -/
lemma continuous.integrable_on_compact
[topological_space α] [opens_measurable_space α] [t2_space α]
[borel_space E] {μ : measure α} [locally_finite_measure μ] {s : set α}
(hs : is_compact s) {f : α → E} (hf : continuous f) :
integrable_on f s μ :=
hf.continuous_on.integrable_on_compact hs
/-- A continuous function with compact closure of the support is integrable on the whole space. -/
lemma continuous.integrable_of_compact_closure_support
[topological_space α] [opens_measurable_space α] [t2_space α] [borel_space E]
{μ : measure α} [locally_finite_measure μ] {f : α → E} (hf : continuous f)
(hfc : is_compact (closure $ support f)) :
integrable f μ :=
begin
rw [← indicator_eq_self.2 (@subset_closure _ _ (support f)),
integrable_indicator_iff is_closed_closure.measurable_set],
{ exact hf.integrable_on_compact hfc },
{ apply_instance }
end
section
/-! ### Continuous linear maps composed with integration
The goal of this section is to prove that integration commutes with continuous linear maps.
This holds for simple functions. The general result follows from the continuity of all involved
operations on the space `L¹`. Note that composition by a continuous linear map on `L¹` is not just
the composition, as we are dealing with classes of functions, but it has already been defined
as `continuous_linear_map.comp_Lp`. We take advantage of this construction here.
-/
variables {μ : measure α} [normed_space ℝ E]
variables [normed_group F] [normed_space ℝ F]
variables {p : ennreal}
local attribute [instance] fact_one_le_one_ennreal
namespace continuous_linear_map
variables [measurable_space F] [borel_space F]
lemma integrable_comp [opens_measurable_space E] {φ : α → E} (L : E →L[ℝ] F)
(φ_int : integrable φ μ) : integrable (λ (a : α), L (φ a)) μ :=
((integrable.norm φ_int).const_mul ∥L∥).mono' (L.measurable.comp_ae_measurable φ_int.ae_measurable)
(eventually_of_forall $ λ a, L.le_op_norm (φ a))
variables [second_countable_topology F] [complete_space F]
[borel_space E] [second_countable_topology E]
lemma integral_comp_Lp (L : E →L[ℝ] F) (φ : Lp E p μ) :
∫ a, (L.comp_Lp φ) a ∂μ = ∫ a, L (φ a) ∂μ :=
integral_congr_ae $ coe_fn_comp_Lp _ _
lemma continuous_integral_comp_L1 (L : E →L[ℝ] F) :
continuous (λ (φ : α →₁[μ] E), ∫ (a : α), L (φ a) ∂μ) :=
begin
rw ← funext L.integral_comp_Lp,
exact continuous_integral.comp (L.comp_LpL 1 μ).continuous
end
variables [complete_space E]
lemma integral_comp_comm (L : E →L[ℝ] F) {φ : α → E} (φ_int : integrable φ μ) :
∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) :=
begin
apply integrable.induction (λ φ, ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ)),
{ intros e s s_meas s_finite,
rw [integral_indicator_const e s_meas, continuous_linear_map.map_smul,
← integral_indicator_const (L e) s_meas],
congr' 1 with a,
rw set.indicator_comp_of_zero L.map_zero },
{ intros f g H f_int g_int hf hg,
simp [L.map_add, integral_add f_int g_int,
integral_add (L.integrable_comp f_int) (L.integrable_comp g_int), hf, hg] },
{ exact is_closed_eq L.continuous_integral_comp_L1 (L.continuous.comp continuous_integral) },
{ intros f g hfg f_int hf,
convert hf using 1 ; clear hf,
{ exact integral_congr_ae (hfg.fun_comp L).symm },
{ rw integral_congr_ae hfg.symm } },
all_goals { assumption }
end
lemma integral_comp_L1_comm (L : E →L[ℝ] F) (φ : α →₁[μ] E) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) :=
L.integral_comp_comm (L1.integrable_coe_fn φ)
end continuous_linear_map
variables [borel_space E] [second_countable_topology E] [complete_space E]
[measurable_space F] [borel_space F] [second_countable_topology F] [complete_space F]
lemma fst_integral {f : α → E × F} (hf : integrable f μ) :
(∫ x, f x ∂μ).1 = ∫ x, (f x).1 ∂μ :=
((continuous_linear_map.fst ℝ E F).integral_comp_comm hf).symm
lemma snd_integral {f : α → E × F} (hf : integrable f μ) :
(∫ x, f x ∂μ).2 = ∫ x, (f x).2 ∂μ :=
((continuous_linear_map.snd ℝ E F).integral_comp_comm hf).symm
lemma integral_pair {f : α → E} {g : α → F} (hf : integrable f μ) (hg : integrable g μ) :
∫ x, (f x, g x) ∂μ = (∫ x, f x ∂μ, ∫ x, g x ∂μ) :=
have _ := hf.prod_mk hg, prod.ext (fst_integral this) (snd_integral this)
lemma integral_smul_const (f : α → ℝ) (c : E) :
∫ x, f x • c ∂μ = (∫ x, f x ∂μ) • c :=
begin
by_cases hf : integrable f μ,
{ exact ((continuous_linear_map.id ℝ ℝ).smul_right c).integral_comp_comm hf },
{ by_cases hc : c = 0,
{ simp only [hc, integral_zero, smul_zero] },
rw [integral_undef hf, integral_undef, zero_smul],
simp_rw [integrable_smul_const hc, hf, not_false_iff] }
end
end
/-
namespace integrable
variables [measurable_space α] [measurable_space β] [normed_group E]
protected lemma measure_mono
end integrable
end measure_theory
section integral_on
variables [measurable_space α]
[normed_group β] [second_countable_topology β] [normed_space ℝ β] [complete_space β]
[measurable_space β] [borel_space β]
{s t : set α} {f g : α → β} {μ : measure α}
open set
lemma integral_on_congr (hf : measurable f) (hg : measurable g) (hs : measurable_set s)
(h : ∀ᵐ a ∂μ, a ∈ s → f a = g a) : ∫ a in s, f a ∂μ = ∫ a in s, g a ∂μ :=
integral_congr_ae hf hg $ _
lemma integral_on_congr_of_set (hsm : measurable_on s f) (htm : measurable_on t f)
(h : ∀ᵐ a, a ∈ s ↔ a ∈ t) : (∫ a in s, f a) = (∫ a in t, f a) :=
integral_congr_ae hsm htm $ indicator_congr_of_set h
lemma integral_on_add {s : set α} (hfm : measurable_on s f) (hfi : integrable_on s f)
(hgm : measurable_on s g) (hgi : integrable_on s g) :
(∫ a in s, f a + g a) = (∫ a in s, f a) + (∫ a in s, g a) :=
by { simp only [indicator_add], exact integral_add hfm hfi hgm hgi }
lemma integral_on_sub (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g)
(hgi : integrable_on s g) : (∫ a in s, f a - g a) = (∫ a in s, f a) - (∫ a in s, g a) :=
by { simp only [indicator_sub], exact integral_sub hfm hfi hgm hgi }
lemma integral_on_le_integral_on_ae {f g : α → ℝ} (hfm : measurable_on s f)
(hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g)
(h : ∀ᵐ a, a ∈ s → f a ≤ g a) :
(∫ a in s, f a) ≤ (∫ a in s, g a) :=
begin
apply integral_le_integral_ae hfm hfi hgm hgi,
apply indicator_le_indicator_ae,
exact h
end
lemma integral_on_le_integral_on {f g : α → ℝ} (hfm : measurable_on s f) (hfi : integrable_on s f)
(hgm : measurable_on s g) (hgi : integrable_on s g) (h : ∀ a, a ∈ s → f a ≤ g a) :
(∫ a in s, f a) ≤ (∫ a in s, g a) :=
integral_on_le_integral_on_ae hfm hfi hgm hgi $ by filter_upwards [] h
lemma integral_on_union (hsm : measurable_on s f) (hsi : integrable_on s f)
(htm : measurable_on t f) (hti : integrable_on t f) (h : disjoint s t) :
(∫ a in (s ∪ t), f a) = (∫ a in s, f a) + (∫ a in t, f a) :=
by { rw [indicator_union_of_disjoint h, integral_add hsm hsi htm hti] }
lemma integral_on_union_ae (hs : measurable_set s) (ht : measurable_set t) (hsm : measurable_on s f)
(hsi : integrable_on s f) (htm : measurable_on t f) (hti : integrable_on t f)
(h : ∀ᵐ a, a ∉ s ∩ t) :
(∫ a in (s ∪ t), f a) = (∫ a in s, f a) + (∫ a in t, f a) :=
begin
have := integral_congr_ae _ _ (indicator_union_ae h f),
rw [this, integral_add hsm hsi htm hti],
{ exact hsm.union hs ht htm },
{ exact measurable.add hsm htm }
end
lemma integral_on_nonneg_of_ae {f : α → ℝ} (hf : ∀ᵐ a, a ∈ s → 0 ≤ f a) : (0:ℝ) ≤ (∫ a in s, f a) :=
integral_nonneg_of_ae $ by { filter_upwards [hf] λ a h, indicator_nonneg' h }
lemma integral_on_nonneg {f : α → ℝ} (hf : ∀ a, a ∈ s → 0 ≤ f a) : (0:ℝ) ≤ (∫ a in s, f a) :=
integral_on_nonneg_of_ae $ univ_mem_sets' hf
lemma integral_on_nonpos_of_ae {f : α → ℝ} (hf : ∀ᵐ a, a ∈ s → f a ≤ 0) : (∫ a in s, f a) ≤ 0 :=
integral_nonpos_of_nonpos_ae $ by { filter_upwards [hf] λ a h, indicator_nonpos' h }
lemma integral_on_nonpos {f : α → ℝ} (hf : ∀ a, a ∈ s → f a ≤ 0) : (∫ a in s, f a) ≤ 0 :=
integral_on_nonpos_of_ae $ univ_mem_sets' hf
lemma tendsto_integral_on_of_monotone {s : ℕ → set α} {f : α → β} (hsm : ∀i, measurable_set (s i))
(h_mono : monotone s) (hfm : measurable_on (Union s) f) (hfi : integrable_on (Union s) f) :
tendsto (λi, ∫ a in (s i), f a) at_top (nhds (∫ a in (Union s), f a)) :=
let bound : α → ℝ := indicator (Union s) (λa, ∥f a∥) in
begin
apply tendsto_integral_of_dominated_convergence,
{ assume i, exact hfm.subset (hsm i) (subset_Union _ _) },
{ assumption },
{ show integrable_on (Union s) (λa, ∥f a∥), rwa integrable_on_norm_iff },
{ assume i, apply ae_of_all,
assume a,
rw [norm_indicator_eq_indicator_norm],
exact indicator_le_indicator_of_subset (subset_Union _ _) (λa, norm_nonneg _) _ },
{ filter_upwards [] λa, le_trans (tendsto_indicator_of_monotone _ h_mono _ _) (pure_le_nhds _) }
end
lemma tendsto_integral_on_of_antimono (s : ℕ → set α) (f : α → β) (hsm : ∀i, measurable_set (s i))
(h_mono : ∀i j, i ≤ j → s j ⊆ s i) (hfm : measurable_on (s 0) f) (hfi : integrable_on (s 0) f) :
tendsto (λi, ∫ a in (s i), f a) at_top (nhds (∫ a in (Inter s), f a)) :=
let bound : α → ℝ := indicator (s 0) (λa, ∥f a∥) in
begin
apply tendsto_integral_of_dominated_convergence,
{ assume i, refine hfm.subset (hsm i) (h_mono _ _ (zero_le _)) },
{ exact hfm.subset (measurable_set.Inter hsm) (Inter_subset _ _) },
{ show integrable_on (s 0) (λa, ∥f a∥), rwa integrable_on_norm_iff },
{ assume i, apply ae_of_all,
assume a,
rw [norm_indicator_eq_indicator_norm],
refine indicator_le_indicator_of_subset (h_mono _ _ (zero_le _)) (λa, norm_nonneg _) _ },
{ filter_upwards [] λa, le_trans (tendsto_indicator_of_antimono _ h_mono _ _) (pure_le_nhds _) }
end
-- TODO : prove this for an encodable type
-- by proving an encodable version of `filter.is_countably_generated_at_top_finset_nat `
lemma integral_on_Union (s : ℕ → set α) (f : α → β) (hm : ∀i, measurable_set (s i))
(hd : ∀ i j, i ≠ j → s i ∩ s j = ∅) (hfm : measurable_on (Union s) f)
(hfi : integrable_on (Union s) f) :
(∫ a in (Union s), f a) = ∑'i, ∫ a in s i, f a :=
suffices h : tendsto (λn:finset ℕ, ∑ i in n, ∫ a in s i, f a) at_top (𝓝 $ (∫ a in (Union s), f a)),
by { rwa has_sum.tsum_eq },
begin
have : (λn:finset ℕ, ∑ i in n, ∫ a in s i, f a) = λn:finset ℕ, ∫ a in (⋃i∈n, s i), f a,
{ funext,
rw [← integral_finset_sum, indicator_finset_bUnion],
{ assume i hi j hj hij, exact hd i j hij },
{ assume i, refine hfm.subset (hm _) (subset_Union _ _) },
{ assume i, refine hfi.subset (subset_Union _ _) } },
rw this,
refine tendsto_integral_filter_of_dominated_convergence _ _ _ _ _ _ _,
{ exact indicator (Union s) (λ a, ∥f a∥) },
{ exact is_countably_generated_at_top_finset_nat },
{ refine univ_mem_sets' (λ n, _),
simp only [mem_set_of_eq],
refine hfm.subset (measurable_set.Union (λ i, measurable_set.Union_Prop (λh, hm _)))
(bUnion_subset_Union _ _), },
{ assumption },
{ refine univ_mem_sets' (λ n, univ_mem_sets' $ _),
simp only [mem_set_of_eq],
assume a,
rw ← norm_indicator_eq_indicator_norm,
refine norm_indicator_le_of_subset (bUnion_subset_Union _ _) _ _ },
{ rw [← integrable_on, integrable_on_norm_iff], assumption },
{ filter_upwards [] λa, le_trans (tendsto_indicator_bUnion_finset _ _ _) (pure_le_nhds _) }
end
end integral_on
-/
|
774fe34a8b31ee1ac2a92cc3849dfc5134323662 | bb31430994044506fa42fd667e2d556327e18dfe | /src/data/real/basic.lean | 76aa36a70ac09d129cf3243d876aeee5e313b644 | [
"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 | 27,891 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Floris van Doorn
-/
import algebra.bounds
import algebra.order.archimedean
import algebra.star.basic
import data.real.cau_seq_completion
/-!
# Real numbers from Cauchy sequences
This file defines `ℝ` as the type of equivalence classes of Cauchy sequences of rational numbers.
This choice is motivated by how easy it is to prove that `ℝ` is a commutative ring, by simply
lifting everything to `ℚ`.
-/
assert_not_exists finset
assert_not_exists module
assert_not_exists submonoid
open_locale pointwise
/-- The type `ℝ` of real numbers constructed as equivalence classes of Cauchy sequences of rational
numbers. -/
structure real := of_cauchy ::
(cauchy : cau_seq.completion.Cauchy (abs : ℚ → ℚ))
notation `ℝ` := real
attribute [pp_using_anonymous_constructor] real
namespace cau_seq.completion
-- this can't go in `data.real.cau_seq_completion` as the structure on `rat` isn't available
@[simp] theorem of_rat_rat {abv : ℚ → ℚ} [is_absolute_value abv] (q : ℚ) :
of_rat (q : ℚ) = (q : @Cauchy _ _ _ _ abv _) := rfl
end cau_seq.completion
namespace real
open cau_seq cau_seq.completion
variables {x y : ℝ}
lemma ext_cauchy_iff : ∀ {x y : real}, x = y ↔ x.cauchy = y.cauchy
| ⟨a⟩ ⟨b⟩ := by split; cc
lemma ext_cauchy {x y : real} : x.cauchy = y.cauchy → x = y :=
ext_cauchy_iff.2
/-- The real numbers are isomorphic to the quotient of Cauchy sequences on the rationals. -/
def equiv_Cauchy : ℝ ≃ cau_seq.completion.Cauchy abs :=
⟨real.cauchy, real.of_cauchy, λ ⟨_⟩, rfl, λ _, rfl⟩
-- irreducible doesn't work for instances: https://github.com/leanprover-community/lean/issues/511
@[irreducible] private def zero : ℝ := ⟨0⟩
@[irreducible] private def one : ℝ := ⟨1⟩
@[irreducible] private def add : ℝ → ℝ → ℝ | ⟨a⟩ ⟨b⟩ := ⟨a + b⟩
@[irreducible] private def neg : ℝ → ℝ | ⟨a⟩ := ⟨-a⟩
@[irreducible] private def mul : ℝ → ℝ → ℝ | ⟨a⟩ ⟨b⟩ := ⟨a * b⟩
@[irreducible] private noncomputable def inv' : ℝ → ℝ | ⟨a⟩ := ⟨a⁻¹⟩
instance : has_zero ℝ := ⟨zero⟩
instance : has_one ℝ := ⟨one⟩
instance : has_add ℝ := ⟨add⟩
instance : has_neg ℝ := ⟨neg⟩
instance : has_mul ℝ := ⟨mul⟩
instance : has_sub ℝ := ⟨λ a b, a + (-b)⟩
noncomputable instance : has_inv ℝ := ⟨inv'⟩
lemma of_cauchy_zero : (⟨0⟩ : ℝ) = 0 := show _ = zero, by rw zero
lemma of_cauchy_one : (⟨1⟩ : ℝ) = 1 := show _ = one, by rw one
lemma of_cauchy_add (a b) : (⟨a + b⟩ : ℝ) = ⟨a⟩ + ⟨b⟩ := show _ = add _ _, by rw add
lemma of_cauchy_neg (a) : (⟨-a⟩ : ℝ) = -⟨a⟩ := show _ = neg _, by rw neg
lemma of_cauchy_sub (a b) : (⟨a - b⟩ : ℝ) = ⟨a⟩ - ⟨b⟩ :=
by { rw [sub_eq_add_neg, of_cauchy_add, of_cauchy_neg], refl }
lemma of_cauchy_mul (a b) : (⟨a * b⟩ : ℝ) = ⟨a⟩ * ⟨b⟩ := show _ = mul _ _, by rw mul
lemma of_cauchy_inv {f} : (⟨f⁻¹⟩ : ℝ) = ⟨f⟩⁻¹ := show _ = inv' _, by rw inv'
lemma cauchy_zero : (0 : ℝ).cauchy = 0 := show zero.cauchy = 0, by rw zero
lemma cauchy_one : (1 : ℝ).cauchy = 1 := show one.cauchy = 1, by rw one
lemma cauchy_add : ∀ a b, (a + b : ℝ).cauchy = a.cauchy + b.cauchy
| ⟨a⟩ ⟨b⟩ := show (add _ _).cauchy = _, by rw add
lemma cauchy_neg : ∀ a, (-a : ℝ).cauchy = -a.cauchy
| ⟨a⟩ := show (neg _).cauchy = _, by rw neg
lemma cauchy_mul : ∀ a b, (a * b : ℝ).cauchy = a.cauchy * b.cauchy
| ⟨a⟩ ⟨b⟩ := show (mul _ _).cauchy = _, by rw mul
lemma cauchy_sub : ∀ a b, (a - b : ℝ).cauchy = a.cauchy - b.cauchy
| ⟨a⟩ ⟨b⟩ := by { rw [sub_eq_add_neg, ←cauchy_neg, ←cauchy_add], refl }
lemma cauchy_inv : ∀ f, (f⁻¹ : ℝ).cauchy = f.cauchy⁻¹
| ⟨f⟩ := show (inv' _).cauchy = _, by rw inv'
instance : has_nat_cast ℝ := { nat_cast := λ n, ⟨n⟩ }
instance : has_int_cast ℝ := { int_cast := λ z, ⟨z⟩ }
instance : has_rat_cast ℝ := { rat_cast := λ q, ⟨q⟩ }
lemma of_cauchy_nat_cast (n : ℕ) : (⟨n⟩ : ℝ) = n := rfl
lemma of_cauchy_int_cast (z : ℤ) : (⟨z⟩ : ℝ) = z := rfl
lemma of_cauchy_rat_cast (q : ℚ) : (⟨q⟩ : ℝ) = q := rfl
lemma cauchy_nat_cast (n : ℕ) : (n : ℝ).cauchy = n := rfl
lemma cauchy_int_cast (z : ℤ) : (z : ℝ).cauchy = z := rfl
lemma cauchy_rat_cast (q : ℚ) : (q : ℝ).cauchy = q := rfl
instance : comm_ring ℝ :=
begin
refine_struct { zero := (0 : ℝ),
one := (1 : ℝ),
mul := (*),
add := (+),
neg := @has_neg.neg ℝ _,
sub := @has_sub.sub ℝ _,
npow := @npow_rec ℝ ⟨1⟩ ⟨(*)⟩,
nsmul := @nsmul_rec ℝ ⟨0⟩ ⟨(+)⟩,
zsmul := @zsmul_rec ℝ ⟨0⟩ ⟨(+)⟩ ⟨@has_neg.neg ℝ _⟩,
..real.has_nat_cast,
..real.has_int_cast, };
repeat { rintro ⟨_⟩, };
try { refl };
simp [← of_cauchy_zero, ← of_cauchy_one, ←of_cauchy_add, ←of_cauchy_neg, ←of_cauchy_mul,
λ n, show @coe ℕ ℝ ⟨_⟩ n = ⟨n⟩, from rfl, has_nat_cast.nat_cast, has_int_cast.int_cast];
apply add_assoc <|> apply add_comm <|> apply mul_assoc <|> apply mul_comm <|>
apply left_distrib <|> apply right_distrib <|> apply sub_eq_add_neg <|> skip,
end
/-- `real.equiv_Cauchy` as a ring equivalence. -/
@[simps]
def ring_equiv_Cauchy : ℝ ≃+* cau_seq.completion.Cauchy abs :=
{ to_fun := cauchy,
inv_fun := of_cauchy,
map_add' := cauchy_add,
map_mul' := cauchy_mul,
..equiv_Cauchy }
/-! Extra instances to short-circuit type class resolution.
These short-circuits have an additional property of ensuring that a computable path is found; if
`field ℝ` is found first, then decaying it to these typeclasses would result in a `noncomputable`
version of them. -/
instance : ring ℝ := by apply_instance
instance : comm_semiring ℝ := by apply_instance
instance : semiring ℝ := by apply_instance
instance : comm_monoid_with_zero ℝ := by apply_instance
instance : monoid_with_zero ℝ := by apply_instance
instance : add_comm_group ℝ := by apply_instance
instance : add_group ℝ := by apply_instance
instance : add_comm_monoid ℝ := by apply_instance
instance : add_monoid ℝ := by apply_instance
instance : add_left_cancel_semigroup ℝ := by apply_instance
instance : add_right_cancel_semigroup ℝ := by apply_instance
instance : add_comm_semigroup ℝ := by apply_instance
instance : add_semigroup ℝ := by apply_instance
instance : comm_monoid ℝ := by apply_instance
instance : monoid ℝ := by apply_instance
instance : comm_semigroup ℝ := by apply_instance
instance : semigroup ℝ := by apply_instance
instance : inhabited ℝ := ⟨0⟩
/-- The real numbers are a `*`-ring, with the trivial `*`-structure. -/
instance : star_ring ℝ := star_ring_of_comm
instance : has_trivial_star ℝ := ⟨λ _, rfl⟩
/-- Make a real number from a Cauchy sequence of rationals (by taking the equivalence class). -/
def mk (x : cau_seq ℚ abs) : ℝ := ⟨cau_seq.completion.mk x⟩
theorem mk_eq {f g : cau_seq ℚ abs} : mk f = mk g ↔ f ≈ g :=
ext_cauchy_iff.trans mk_eq
@[irreducible]
private def lt : ℝ → ℝ → Prop | ⟨x⟩ ⟨y⟩ :=
quotient.lift_on₂ x y (<) $
λ f₁ g₁ f₂ g₂ hf hg, propext $
⟨λ h, lt_of_eq_of_lt (setoid.symm hf) (lt_of_lt_of_eq h hg),
λ h, lt_of_eq_of_lt hf (lt_of_lt_of_eq h (setoid.symm hg))⟩
instance : has_lt ℝ := ⟨lt⟩
lemma lt_cauchy {f g} : (⟨⟦f⟧⟩ : ℝ) < ⟨⟦g⟧⟩ ↔ f < g := show lt _ _ ↔ _, by rw lt; refl
@[simp] theorem mk_lt {f g : cau_seq ℚ abs} : mk f < mk g ↔ f < g :=
lt_cauchy
lemma mk_zero : mk 0 = 0 := by rw ← of_cauchy_zero; refl
lemma mk_one : mk 1 = 1 := by rw ← of_cauchy_one; refl
lemma mk_add {f g : cau_seq ℚ abs} : mk (f + g) = mk f + mk g := by simp [mk, ←of_cauchy_add]
lemma mk_mul {f g : cau_seq ℚ abs} : mk (f * g) = mk f * mk g := by simp [mk, ←of_cauchy_mul]
lemma mk_neg {f : cau_seq ℚ abs} : mk (-f) = -mk f := by simp [mk, ←of_cauchy_neg]
@[simp] theorem mk_pos {f : cau_seq ℚ abs} : 0 < mk f ↔ pos f :=
by rw [← mk_zero, mk_lt]; exact iff_of_eq (congr_arg pos (sub_zero f))
@[irreducible] private def le (x y : ℝ) : Prop := x < y ∨ x = y
instance : has_le ℝ := ⟨le⟩
private lemma le_def {x y : ℝ} : x ≤ y ↔ x < y ∨ x = y := show le _ _ ↔ _, by rw le
@[simp] theorem mk_le {f g : cau_seq ℚ abs} : mk f ≤ mk g ↔ f ≤ g :=
by simp [le_def, mk_eq]; refl
@[elab_as_eliminator]
protected lemma ind_mk {C : real → Prop} (x : real) (h : ∀ y, C (mk y)) : C x :=
begin
cases x with x,
induction x using quot.induction_on with x,
exact h x
end
theorem add_lt_add_iff_left {a b : ℝ} (c : ℝ) : c + a < c + b ↔ a < b :=
begin
induction a using real.ind_mk,
induction b using real.ind_mk,
induction c using real.ind_mk,
simp only [mk_lt, ← mk_add],
show pos _ ↔ pos _, rw add_sub_add_left_eq_sub
end
instance : partial_order ℝ :=
{ le := (≤), lt := (<),
lt_iff_le_not_le := λ a b, real.ind_mk a $ λ a, real.ind_mk b $ λ b,
by simpa using lt_iff_le_not_le,
le_refl := λ a, a.ind_mk (by intro a; rw mk_le),
le_trans := λ a b c, real.ind_mk a $ λ a, real.ind_mk b $ λ b, real.ind_mk c $ λ c,
by simpa using le_trans,
lt_iff_le_not_le := λ a b, real.ind_mk a $ λ a, real.ind_mk b $ λ b,
by simpa using lt_iff_le_not_le,
le_antisymm := λ a b, real.ind_mk a $ λ a, real.ind_mk b $ λ b,
by simpa [mk_eq] using @cau_seq.le_antisymm _ _ a b }
instance : preorder ℝ := by apply_instance
theorem rat_cast_lt {x y : ℚ} : (x : ℝ) < (y : ℝ) ↔ x < y :=
begin
rw [mk_lt] {md := tactic.transparency.semireducible},
exact const_lt
end
protected theorem zero_lt_one : (0 : ℝ) < 1 :=
by convert rat_cast_lt.2 zero_lt_one; simp [←of_cauchy_rat_cast, of_cauchy_one, of_cauchy_zero]
protected lemma fact_zero_lt_one : fact ((0 : ℝ) < 1) := ⟨real.zero_lt_one⟩
protected theorem mul_pos {a b : ℝ} : 0 < a → 0 < b → 0 < a * b :=
begin
induction a using real.ind_mk with a,
induction b using real.ind_mk with b,
simpa only [mk_lt, mk_pos, ← mk_mul] using cau_seq.mul_pos
end
instance : strict_ordered_comm_ring ℝ :=
{ exists_pair_ne := ⟨0, 1, real.zero_lt_one.ne⟩,
add_le_add_left :=
begin
simp only [le_iff_eq_or_lt],
rintros a b ⟨rfl, h⟩,
{ simp },
{ exact λ c, or.inr ((add_lt_add_iff_left c).2 ‹_›) }
end,
zero_le_one := le_of_lt real.zero_lt_one,
mul_pos := @real.mul_pos,
.. real.comm_ring, .. real.partial_order, .. real.semiring }
instance : strict_ordered_ring ℝ := infer_instance
instance : strict_ordered_comm_semiring ℝ := infer_instance
instance : strict_ordered_semiring ℝ := infer_instance
instance : ordered_ring ℝ := infer_instance
instance : ordered_semiring ℝ := infer_instance
instance : ordered_add_comm_group ℝ := infer_instance
instance : ordered_cancel_add_comm_monoid ℝ := infer_instance
instance : ordered_add_comm_monoid ℝ := infer_instance
instance : nontrivial ℝ := infer_instance
@[irreducible]
private def sup : ℝ → ℝ → ℝ | ⟨x⟩ ⟨y⟩ :=
⟨quotient.map₂ (⊔) (λ x₁ x₂ hx y₁ y₂ hy, sup_equiv_sup hx hy) x y⟩
instance : has_sup ℝ := ⟨sup⟩
lemma of_cauchy_sup (a b) : (⟨⟦a ⊔ b⟧⟩ : ℝ) = ⟨⟦a⟧⟩ ⊔ ⟨⟦b⟧⟩ := show _ = sup _ _, by { rw sup, refl }
@[simp] lemma mk_sup (a b) : (mk (a ⊔ b) : ℝ) = mk a ⊔ mk b := of_cauchy_sup _ _
@[irreducible]
private def inf : ℝ → ℝ → ℝ | ⟨x⟩ ⟨y⟩ :=
⟨quotient.map₂ (⊓) (λ x₁ x₂ hx y₁ y₂ hy, inf_equiv_inf hx hy) x y⟩
instance : has_inf ℝ := ⟨inf⟩
lemma of_cauchy_inf (a b) : (⟨⟦a ⊓ b⟧⟩ : ℝ) = ⟨⟦a⟧⟩ ⊓ ⟨⟦b⟧⟩ := show _ = inf _ _, by { rw inf, refl }
@[simp] lemma mk_inf (a b) : (mk (a ⊓ b) : ℝ) = mk a ⊓ mk b := of_cauchy_inf _ _
instance : distrib_lattice ℝ :=
{ sup := (⊔),
le := (≤),
le_sup_left := λ a, real.ind_mk a $ λ a b, real.ind_mk b $ λ b, begin
rw [←mk_sup, mk_le],
exact cau_seq.le_sup_left,
end,
le_sup_right := λ a, real.ind_mk a $ λ a b, real.ind_mk b $ λ b, begin
rw [←mk_sup, mk_le],
exact cau_seq.le_sup_right,
end,
sup_le := λ a, real.ind_mk a $ λ a b, real.ind_mk b $ λ b c, real.ind_mk c $ λ c, begin
simp_rw [←mk_sup, mk_le],
exact cau_seq.sup_le,
end,
inf := (⊓),
inf_le_left := λ a, real.ind_mk a $ λ a b, real.ind_mk b $ λ b, begin
rw [←mk_inf, mk_le],
exact cau_seq.inf_le_left,
end,
inf_le_right := λ a, real.ind_mk a $ λ a b, real.ind_mk b $ λ b, begin
rw [←mk_inf, mk_le],
exact cau_seq.inf_le_right,
end,
le_inf := λ a, real.ind_mk a $ λ a b, real.ind_mk b $ λ b c, real.ind_mk c $ λ c, begin
simp_rw [←mk_inf, mk_le],
exact cau_seq.le_inf,
end,
le_sup_inf := λ a, real.ind_mk a $ λ a b, real.ind_mk b $ λ b c, real.ind_mk c $ λ c, eq.le begin
simp only [←mk_sup, ←mk_inf],
exact congr_arg mk (cau_seq.sup_inf_distrib_left _ _ _).symm
end,
.. real.partial_order }
/- Extra instances to short-circuit type class resolution -/
instance : lattice ℝ := infer_instance
instance : semilattice_inf ℝ := infer_instance
instance : semilattice_sup ℝ := infer_instance
open_locale classical
instance : is_total ℝ (≤) :=
⟨λ a, real.ind_mk a $ λ a b, real.ind_mk b $ λ b, by simpa using le_total a b⟩
noncomputable instance : linear_order ℝ :=
lattice.to_linear_order _
noncomputable instance : linear_ordered_comm_ring ℝ :=
{ .. real.nontrivial, .. real.strict_ordered_ring, .. real.comm_ring, .. real.linear_order }
/- Extra instances to short-circuit type class resolution -/
noncomputable instance : linear_ordered_ring ℝ := by apply_instance
noncomputable instance : linear_ordered_semiring ℝ := by apply_instance
instance : is_domain ℝ :=
{ .. real.nontrivial, .. real.comm_ring, .. linear_ordered_ring.is_domain }
noncomputable instance : linear_ordered_field ℝ :=
{ inv := has_inv.inv,
mul_inv_cancel := begin
rintros ⟨a⟩ h,
rw mul_comm,
simp only [←of_cauchy_inv, ←of_cauchy_mul, ← of_cauchy_one, ← of_cauchy_zero, ne.def] at *,
exact cau_seq.completion.inv_mul_cancel h,
end,
inv_zero := by simp [← of_cauchy_zero, ←of_cauchy_inv],
rat_cast := coe,
rat_cast_mk := λ n d hd h2,
by rw [←of_cauchy_rat_cast, rat.cast_mk', of_cauchy_mul, of_cauchy_inv, of_cauchy_nat_cast,
of_cauchy_int_cast],
..real.linear_ordered_comm_ring }
/- Extra instances to short-circuit type class resolution -/
noncomputable instance : linear_ordered_add_comm_group ℝ := by apply_instance
noncomputable instance field : field ℝ := by apply_instance
noncomputable instance : division_ring ℝ := by apply_instance
noncomputable instance decidable_lt (a b : ℝ) : decidable (a < b) := by apply_instance
noncomputable instance decidable_le (a b : ℝ) : decidable (a ≤ b) := by apply_instance
noncomputable instance decidable_eq (a b : ℝ) : decidable (a = b) := by apply_instance
/-- Show an underlying cauchy sequence for real numbers.
The representative chosen is the one passed in the VM to `quot.mk`, so two cauchy sequences
converging to the same number may be printed differently.
-/
meta instance : has_repr ℝ :=
{ repr := λ r, "real.of_cauchy " ++ repr r.cauchy }
theorem le_mk_of_forall_le {f : cau_seq ℚ abs} :
(∃ i, ∀ j ≥ i, x ≤ f j) → x ≤ mk f :=
begin
intro h,
induction x using real.ind_mk with x,
apply le_of_not_lt,
rw mk_lt,
rintro ⟨K, K0, hK⟩,
obtain ⟨i, H⟩ := exists_forall_ge_and h
(exists_forall_ge_and hK (f.cauchy₃ $ half_pos K0)),
apply not_lt_of_le (H _ le_rfl).1,
rw [mk_lt] {md := tactic.transparency.semireducible},
refine ⟨_, half_pos K0, i, λ j ij, _⟩,
have := add_le_add (H _ ij).2.1
(le_of_lt (abs_lt.1 $ (H _ le_rfl).2.2 _ ij).1),
rwa [← sub_eq_add_neg, sub_self_div_two, sub_apply, sub_add_sub_cancel] at this
end
theorem mk_le_of_forall_le {f : cau_seq ℚ abs} {x : ℝ}
(h : ∃ i, ∀ j ≥ i, (f j : ℝ) ≤ x) : mk f ≤ x :=
begin
cases h with i H,
rw [← neg_le_neg_iff, ← mk_neg],
exact le_mk_of_forall_le ⟨i, λ j ij, by simp [H _ ij]⟩
end
theorem mk_near_of_forall_near {f : cau_seq ℚ abs} {x : ℝ} {ε : ℝ}
(H : ∃ i, ∀ j ≥ i, |(f j : ℝ) - x| ≤ ε) : |mk f - x| ≤ ε :=
abs_sub_le_iff.2
⟨sub_le_iff_le_add'.2 $ mk_le_of_forall_le $
H.imp $ λ i h j ij, sub_le_iff_le_add'.1 (abs_sub_le_iff.1 $ h j ij).1,
sub_le_comm.1 $ le_mk_of_forall_le $
H.imp $ λ i h j ij, sub_le_comm.1 (abs_sub_le_iff.1 $ h j ij).2⟩
instance : archimedean ℝ :=
archimedean_iff_rat_le.2 $ λ x, real.ind_mk x $ λ f,
let ⟨M, M0, H⟩ := f.bounded' 0 in
⟨M, mk_le_of_forall_le ⟨0, λ i _,
rat.cast_le.2 $ le_of_lt (abs_lt.1 (H i)).2⟩⟩
noncomputable instance : floor_ring ℝ := archimedean.floor_ring _
theorem is_cau_seq_iff_lift {f : ℕ → ℚ} : is_cau_seq abs f ↔ is_cau_seq
abs (λ i, (f i : ℝ)) :=
⟨λ H ε ε0,
let ⟨δ, δ0, δε⟩ := exists_pos_rat_lt ε0 in
(H _ δ0).imp $ λ i hi j ij, lt_trans
(by simpa using (@rat.cast_lt ℝ _ _ _).2 (hi _ ij)) δε,
λ H ε ε0, (H _ (rat.cast_pos.2 ε0)).imp $
λ i hi j ij, (@rat.cast_lt ℝ _ _ _).1 $ by simpa using hi _ ij⟩
theorem of_near (f : ℕ → ℚ) (x : ℝ)
(h : ∀ ε > 0, ∃ i, ∀ j ≥ i, |(f j : ℝ) - x| < ε) :
∃ h', real.mk ⟨f, h'⟩ = x :=
⟨is_cau_seq_iff_lift.2 (of_near _ (const abs x) h),
sub_eq_zero.1 $ abs_eq_zero.1 $
eq_of_le_of_forall_le_of_dense (abs_nonneg _) $ λ ε ε0,
mk_near_of_forall_near $
(h _ ε0).imp (λ i h j ij, le_of_lt (h j ij))⟩
theorem exists_floor (x : ℝ) : ∃ (ub : ℤ), (ub:ℝ) ≤ x ∧
∀ (z : ℤ), (z:ℝ) ≤ x → z ≤ ub :=
int.exists_greatest_of_bdd
(let ⟨n, hn⟩ := exists_int_gt x in ⟨n, λ z h',
int.cast_le.1 $ le_trans h' $ le_of_lt hn⟩)
(let ⟨n, hn⟩ := exists_int_lt x in ⟨n, le_of_lt hn⟩)
theorem exists_is_lub (S : set ℝ) (hne : S.nonempty) (hbdd : bdd_above S) :
∃ x, is_lub S x :=
begin
rcases ⟨hne, hbdd⟩ with ⟨⟨L, hL⟩, ⟨U, hU⟩⟩,
have : ∀ d : ℕ, bdd_above {m : ℤ | ∃ y ∈ S, (m : ℝ) ≤ y * d},
{ cases exists_int_gt U with k hk,
refine λ d, ⟨k * d, λ z h, _⟩,
rcases h with ⟨y, yS, hy⟩,
refine int.cast_le.1 (hy.trans _),
push_cast,
exact mul_le_mul_of_nonneg_right ((hU yS).trans hk.le) d.cast_nonneg },
choose f hf using λ d : ℕ, int.exists_greatest_of_bdd (this d) ⟨⌊L * d⌋, L, hL, int.floor_le _⟩,
have hf₁ : ∀ n > 0, ∃ y ∈ S, ((f n / n:ℚ):ℝ) ≤ y := λ n n0,
let ⟨y, yS, hy⟩ := (hf n).1 in
⟨y, yS, by simpa using (div_le_iff ((nat.cast_pos.2 n0):((_:ℝ) < _))).2 hy⟩,
have hf₂ : ∀ (n > 0) (y ∈ S), (y - (n:ℕ)⁻¹ : ℝ) < (f n / n:ℚ),
{ intros n n0 y yS,
have := (int.sub_one_lt_floor _).trans_le (int.cast_le.2 $ (hf n).2 _ ⟨y, yS, int.floor_le _⟩),
simp [-sub_eq_add_neg],
rwa [lt_div_iff ((nat.cast_pos.2 n0):((_:ℝ) < _)), sub_mul, _root_.inv_mul_cancel],
exact ne_of_gt (nat.cast_pos.2 n0) },
have hg : is_cau_seq abs (λ n, f n / n : ℕ → ℚ),
{ intros ε ε0,
suffices : ∀ j k ≥ ⌈ε⁻¹⌉₊, (f j / j - f k / k : ℚ) < ε,
{ refine ⟨_, λ j ij, abs_lt.2 ⟨_, this _ ij _ le_rfl⟩⟩,
rw [neg_lt, neg_sub], exact this _ le_rfl _ ij },
intros j ij k ik,
replace ij := le_trans (nat.le_ceil _) (nat.cast_le.2 ij),
replace ik := le_trans (nat.le_ceil _) (nat.cast_le.2 ik),
have j0 := nat.cast_pos.1 ((inv_pos.2 ε0).trans_le ij),
have k0 := nat.cast_pos.1 ((inv_pos.2 ε0).trans_le ik),
rcases hf₁ _ j0 with ⟨y, yS, hy⟩,
refine lt_of_lt_of_le ((@rat.cast_lt ℝ _ _ _).1 _)
((inv_le ε0 (nat.cast_pos.2 k0)).1 ik),
simpa using sub_lt_iff_lt_add'.2
(lt_of_le_of_lt hy $ sub_lt_iff_lt_add.1 $ hf₂ _ k0 _ yS) },
let g : cau_seq ℚ abs := ⟨λ n, f n / n, hg⟩,
refine ⟨mk g, ⟨λ x xS, _, λ y h, _⟩⟩,
{ refine le_of_forall_ge_of_dense (λ z xz, _),
cases exists_nat_gt (x - z)⁻¹ with K hK,
refine le_mk_of_forall_le ⟨K, λ n nK, _⟩,
replace xz := sub_pos.2 xz,
replace hK := hK.le.trans (nat.cast_le.2 nK),
have n0 : 0 < n := nat.cast_pos.1 ((inv_pos.2 xz).trans_le hK),
refine le_trans _ (hf₂ _ n0 _ xS).le,
rwa [le_sub_comm, inv_le ((nat.cast_pos.2 n0):((_:ℝ) < _)) xz] },
{ exact mk_le_of_forall_le ⟨1, λ n n1,
let ⟨x, xS, hx⟩ := hf₁ _ n1 in le_trans hx (h xS)⟩ }
end
noncomputable instance : has_Sup ℝ :=
⟨λ S, if h : S.nonempty ∧ bdd_above S then classical.some (exists_is_lub S h.1 h.2) else 0⟩
lemma Sup_def (S : set ℝ) :
Sup S = if h : S.nonempty ∧ bdd_above S
then classical.some (exists_is_lub S h.1 h.2) else 0 := rfl
protected theorem is_lub_Sup (S : set ℝ) (h₁ : S.nonempty) (h₂ : bdd_above S) : is_lub S (Sup S) :=
by { simp only [Sup_def, dif_pos (and.intro h₁ h₂)], apply classical.some_spec }
noncomputable instance : has_Inf ℝ := ⟨λ S, -Sup (-S)⟩
lemma Inf_def (S : set ℝ) : Inf S = -Sup (-S) := rfl
protected theorem is_glb_Inf (S : set ℝ) (h₁ : S.nonempty) (h₂ : bdd_below S) :
is_glb S (Inf S) :=
begin
rw [Inf_def, ← is_lub_neg', neg_neg],
exact real.is_lub_Sup _ h₁.neg h₂.neg
end
noncomputable instance : conditionally_complete_linear_order ℝ :=
{ Sup := has_Sup.Sup,
Inf := has_Inf.Inf,
le_cSup := λ s a hs ha, (real.is_lub_Sup s ⟨a, ha⟩ hs).1 ha,
cSup_le := λ s a hs ha, (real.is_lub_Sup s hs ⟨a, ha⟩).2 ha,
cInf_le := λ s a hs ha, (real.is_glb_Inf s ⟨a, ha⟩ hs).1 ha,
le_cInf := λ s a hs ha, (real.is_glb_Inf s hs ⟨a, ha⟩).2 ha,
..real.linear_order, ..real.lattice}
lemma lt_Inf_add_pos {s : set ℝ} (h : s.nonempty) {ε : ℝ} (hε : 0 < ε) :
∃ a ∈ s, a < Inf s + ε :=
exists_lt_of_cInf_lt h $ lt_add_of_pos_right _ hε
lemma add_neg_lt_Sup {s : set ℝ} (h : s.nonempty) {ε : ℝ} (hε : ε < 0) :
∃ a ∈ s, Sup s + ε < a :=
exists_lt_of_lt_cSup h $ add_lt_iff_neg_left.2 hε
lemma Inf_le_iff {s : set ℝ} (h : bdd_below s) (h' : s.nonempty) {a : ℝ} :
Inf s ≤ a ↔ ∀ ε, 0 < ε → ∃ x ∈ s, x < a + ε :=
begin
rw le_iff_forall_pos_lt_add,
split; intros H ε ε_pos,
{ exact exists_lt_of_cInf_lt h' (H ε ε_pos) },
{ rcases H ε ε_pos with ⟨x, x_in, hx⟩,
exact cInf_lt_of_lt h x_in hx }
end
lemma le_Sup_iff {s : set ℝ} (h : bdd_above s) (h' : s.nonempty) {a : ℝ} :
a ≤ Sup s ↔ ∀ ε, ε < 0 → ∃ x ∈ s, a + ε < x :=
begin
rw le_iff_forall_pos_lt_add,
refine ⟨λ H ε ε_neg, _, λ H ε ε_pos, _⟩,
{ exact exists_lt_of_lt_cSup h' (lt_sub_iff_add_lt.mp (H _ (neg_pos.mpr ε_neg))) },
{ rcases H _ (neg_lt_zero.mpr ε_pos) with ⟨x, x_in, hx⟩,
exact sub_lt_iff_lt_add.mp (lt_cSup_of_lt h x_in hx) }
end
@[simp] theorem Sup_empty : Sup (∅ : set ℝ) = 0 := dif_neg $ by simp
lemma csupr_empty {α : Sort*} [is_empty α] (f : α → ℝ) : (⨆ i, f i) = 0 :=
begin
dsimp [supr],
convert real.Sup_empty,
rw set.range_eq_empty_iff,
apply_instance
end
@[simp] lemma csupr_const_zero {α : Sort*} : (⨆ i : α, (0:ℝ)) = 0 :=
begin
casesI is_empty_or_nonempty α,
{ exact real.csupr_empty _ },
{ exact csupr_const },
end
theorem Sup_of_not_bdd_above {s : set ℝ} (hs : ¬ bdd_above s) : Sup s = 0 :=
dif_neg $ assume h, hs h.2
lemma supr_of_not_bdd_above {α : Sort*} {f : α → ℝ} (hf : ¬ bdd_above (set.range f)) :
(⨆ i, f i) = 0 := Sup_of_not_bdd_above hf
theorem Sup_univ : Sup (@set.univ ℝ) = 0 :=
real.Sup_of_not_bdd_above $ λ ⟨x, h⟩, not_le_of_lt (lt_add_one _) $ h (set.mem_univ _)
@[simp] theorem Inf_empty : Inf (∅ : set ℝ) = 0 :=
by simp [Inf_def, Sup_empty]
lemma cinfi_empty {α : Sort*} [is_empty α] (f : α → ℝ) : (⨅ i, f i) = 0 :=
by rw [infi_of_empty', Inf_empty]
@[simp] lemma cinfi_const_zero {α : Sort*} : (⨅ i : α, (0:ℝ)) = 0 :=
begin
casesI is_empty_or_nonempty α,
{ exact real.cinfi_empty _ },
{ exact cinfi_const },
end
theorem Inf_of_not_bdd_below {s : set ℝ} (hs : ¬ bdd_below s) : Inf s = 0 :=
neg_eq_zero.2 $ Sup_of_not_bdd_above $ mt bdd_above_neg.1 hs
lemma infi_of_not_bdd_below {α : Sort*} {f : α → ℝ} (hf : ¬ bdd_below (set.range f)) :
(⨅ i, f i) = 0 := Inf_of_not_bdd_below hf
/--
As `0` is the default value for `real.Sup` of the empty set or sets which are not bounded above, it
suffices to show that `S` is bounded below by `0` to show that `0 ≤ Inf S`.
-/
lemma Sup_nonneg (S : set ℝ) (hS : ∀ x ∈ S, (0:ℝ) ≤ x) : 0 ≤ Sup S :=
begin
rcases S.eq_empty_or_nonempty with rfl | ⟨y, hy⟩,
{ exact Sup_empty.ge },
{ apply dite _ (λ h, le_cSup_of_le h hy $ hS y hy) (λ h, (Sup_of_not_bdd_above h).ge) }
end
/--
As `0` is the default value for `real.Sup` of the empty set, it suffices to show that `S` is
bounded above by `0` to show that `Sup S ≤ 0`.
-/
lemma Sup_nonpos (S : set ℝ) (hS : ∀ x ∈ S, x ≤ (0:ℝ)) : Sup S ≤ 0 :=
begin
rcases S.eq_empty_or_nonempty with rfl | hS₂,
exacts [Sup_empty.le, cSup_le hS₂ hS],
end
/--
As `0` is the default value for `real.Inf` of the empty set, it suffices to show that `S` is
bounded below by `0` to show that `0 ≤ Inf S`.
-/
lemma Inf_nonneg (S : set ℝ) (hS : ∀ x ∈ S, (0:ℝ) ≤ x) : 0 ≤ Inf S :=
begin
rcases S.eq_empty_or_nonempty with rfl | hS₂,
exacts [Inf_empty.ge, le_cInf hS₂ hS]
end
/--
As `0` is the default value for `real.Inf` of the empty set or sets which are not bounded below, it
suffices to show that `S` is bounded above by `0` to show that `Inf S ≤ 0`.
-/
lemma Inf_nonpos (S : set ℝ) (hS : ∀ x ∈ S, x ≤ (0:ℝ)) : Inf S ≤ 0 :=
begin
rcases S.eq_empty_or_nonempty with rfl | ⟨y, hy⟩,
{ exact Inf_empty.le },
{ apply dite _ (λ h, cInf_le_of_le h hy $ hS y hy) (λ h, (Inf_of_not_bdd_below h).le) }
end
lemma Inf_le_Sup (s : set ℝ) (h₁ : bdd_below s) (h₂ : bdd_above s) : Inf s ≤ Sup s :=
begin
rcases s.eq_empty_or_nonempty with rfl | hne,
{ rw [Inf_empty, Sup_empty] },
{ exact cInf_le_cSup h₁ h₂ hne }
end
theorem cau_seq_converges (f : cau_seq ℝ abs) : ∃ x, f ≈ const abs x :=
begin
let S := {x : ℝ | const abs x < f},
have lb : ∃ x, x ∈ S := exists_lt f,
have ub' : ∀ x, f < const abs x → ∀ y ∈ S, y ≤ x :=
λ x h y yS, le_of_lt $ const_lt.1 $ cau_seq.lt_trans yS h,
have ub : ∃ x, ∀ y ∈ S, y ≤ x := (exists_gt f).imp ub',
refine ⟨Sup S,
((lt_total _ _).resolve_left (λ h, _)).resolve_right (λ h, _)⟩,
{ rcases h with ⟨ε, ε0, i, ih⟩,
refine (cSup_le lb (ub' _ _)).not_lt (sub_lt_self _ (half_pos ε0)),
refine ⟨_, half_pos ε0, i, λ j ij, _⟩,
rw [sub_apply, const_apply, sub_right_comm,
le_sub_iff_add_le, add_halves],
exact ih _ ij },
{ rcases h with ⟨ε, ε0, i, ih⟩,
refine (le_cSup ub _).not_lt ((lt_add_iff_pos_left _).2 (half_pos ε0)),
refine ⟨_, half_pos ε0, i, λ j ij, _⟩,
rw [sub_apply, const_apply, add_comm, ← sub_sub,
le_sub_iff_add_le, add_halves],
exact ih _ ij }
end
instance : cau_seq.is_complete ℝ abs := ⟨cau_seq_converges⟩
end real
|
42494d869d1eb27605d7a812aedb7102d3757361 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/analysis/convex/gauge.lean | 2304cd00b55fe115f4465fa52330ff8464809304 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 18,991 | lean | /-
Copyright (c) 2021 Yaël Dillies, Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies, Bhavik Mehta
-/
import analysis.convex.basic
import analysis.normed_space.pointwise
import analysis.seminorm
import data.is_R_or_C.basic
import tactic.congrm
/-!
# The Minkowksi functional
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines the Minkowski functional, aka gauge.
The Minkowski functional of a set `s` is the function which associates each point to how much you
need to scale `s` for `x` to be inside it. When `s` is symmetric, convex and absorbent, its gauge is
a seminorm. Reciprocally, any seminorm arises as the gauge of some set, namely its unit ball. This
induces the equivalence of seminorms and locally convex topological vector spaces.
## Main declarations
For a real vector space,
* `gauge`: Aka Minkowksi functional. `gauge s x` is the least (actually, an infimum) `r` such
that `x ∈ r • s`.
* `gauge_seminorm`: The Minkowski functional as a seminorm, when `s` is symmetric, convex and
absorbent.
## References
* [H. H. Schaefer, *Topological Vector Spaces*][schaefer1966]
## Tags
Minkowski functional, gauge
-/
open normed_field set
open_locale pointwise topology nnreal
noncomputable theory
variables {𝕜 E F : Type*}
section add_comm_group
variables [add_comm_group E] [module ℝ E]
/--The Minkowski functional. Given a set `s` in a real vector space, `gauge s` is the functional
which sends `x : E` to the smallest `r : ℝ` such that `x` is in `s` scaled by `r`. -/
def gauge (s : set E) (x : E) : ℝ := Inf {r : ℝ | 0 < r ∧ x ∈ r • s}
variables {s t : set E} {a : ℝ} {x : E}
lemma gauge_def : gauge s x = Inf {r ∈ set.Ioi 0 | x ∈ r • s} := rfl
/-- An alternative definition of the gauge using scalar multiplication on the element rather than on
the set. -/
lemma gauge_def' : gauge s x = Inf {r ∈ set.Ioi 0 | r⁻¹ • x ∈ s} :=
begin
congrm Inf (λ r, _),
exact and_congr_right (λ hr, mem_smul_set_iff_inv_smul_mem₀ hr.ne' _ _),
end
private lemma gauge_set_bdd_below : bdd_below {r : ℝ | 0 < r ∧ x ∈ r • s} := ⟨0, λ r hr, hr.1.le⟩
/-- If the given subset is `absorbent` then the set we take an infimum over in `gauge` is nonempty,
which is useful for proving many properties about the gauge. -/
lemma absorbent.gauge_set_nonempty (absorbs : absorbent ℝ s) :
{r : ℝ | 0 < r ∧ x ∈ r • s}.nonempty :=
let ⟨r, hr₁, hr₂⟩ := absorbs x in ⟨r, hr₁, hr₂ r (real.norm_of_nonneg hr₁.le).ge⟩
lemma gauge_mono (hs : absorbent ℝ s) (h : s ⊆ t) : gauge t ≤ gauge s :=
λ x, cInf_le_cInf gauge_set_bdd_below hs.gauge_set_nonempty $ λ r hr, ⟨hr.1, smul_set_mono h hr.2⟩
lemma exists_lt_of_gauge_lt (absorbs : absorbent ℝ s) (h : gauge s x < a) :
∃ b, 0 < b ∧ b < a ∧ x ∈ b • s :=
begin
obtain ⟨b, ⟨hb, hx⟩, hba⟩ := exists_lt_of_cInf_lt absorbs.gauge_set_nonempty h,
exact ⟨b, hb, hba, hx⟩,
end
/-- The gauge evaluated at `0` is always zero (mathematically this requires `0` to be in the set `s`
but, the real infimum of the empty set in Lean being defined as `0`, it holds unconditionally). -/
@[simp] lemma gauge_zero : gauge s 0 = 0 :=
begin
rw gauge_def',
by_cases (0 : E) ∈ s,
{ simp only [smul_zero, sep_true, h, cInf_Ioi] },
{ simp only [smul_zero, sep_false, h, real.Inf_empty] }
end
@[simp] lemma gauge_zero' : gauge (0 : set E) = 0 :=
begin
ext,
rw gauge_def',
obtain rfl | hx := eq_or_ne x 0,
{ simp only [cInf_Ioi, mem_zero, pi.zero_apply, eq_self_iff_true, sep_true, smul_zero] },
{ simp only [mem_zero, pi.zero_apply, inv_eq_zero, smul_eq_zero],
convert real.Inf_empty,
exact eq_empty_iff_forall_not_mem.2 (λ r hr, hr.2.elim (ne_of_gt hr.1) hx) }
end
@[simp] lemma gauge_empty : gauge (∅ : set E) = 0 :=
by { ext, simp only [gauge_def', real.Inf_empty, mem_empty_iff_false, pi.zero_apply, sep_false] }
lemma gauge_of_subset_zero (h : s ⊆ 0) : gauge s = 0 :=
by { obtain rfl | rfl := subset_singleton_iff_eq.1 h, exacts [gauge_empty, gauge_zero'] }
/-- The gauge is always nonnegative. -/
lemma gauge_nonneg (x : E) : 0 ≤ gauge s x := real.Inf_nonneg _ $ λ x hx, hx.1.le
lemma gauge_neg (symmetric : ∀ x ∈ s, -x ∈ s) (x : E) : gauge s (-x) = gauge s x :=
begin
have : ∀ x, -x ∈ s ↔ x ∈ s := λ x, ⟨λ h, by simpa using symmetric _ h, symmetric x⟩,
simp_rw [gauge_def', smul_neg, this]
end
lemma gauge_neg_set_neg (x : E) : gauge (-s) (-x) = gauge s x :=
by simp_rw [gauge_def', smul_neg, neg_mem_neg]
lemma gauge_neg_set_eq_gauge_neg (x : E) : gauge (-s) x = gauge s (-x) :=
by rw [← gauge_neg_set_neg, neg_neg]
lemma gauge_le_of_mem (ha : 0 ≤ a) (hx : x ∈ a • s) : gauge s x ≤ a :=
begin
obtain rfl | ha' := ha.eq_or_lt,
{ rw [mem_singleton_iff.1 (zero_smul_set_subset _ hx), gauge_zero] },
{ exact cInf_le gauge_set_bdd_below ⟨ha', hx⟩ }
end
lemma gauge_le_eq (hs₁ : convex ℝ s) (hs₀ : (0 : E) ∈ s) (hs₂ : absorbent ℝ s) (ha : 0 ≤ a) :
{x | gauge s x ≤ a} = ⋂ (r : ℝ) (H : a < r), r • s :=
begin
ext,
simp_rw [set.mem_Inter, set.mem_set_of_eq],
refine ⟨λ h r hr, _, λ h, le_of_forall_pos_lt_add (λ ε hε, _)⟩,
{ have hr' := ha.trans_lt hr,
rw mem_smul_set_iff_inv_smul_mem₀ hr'.ne',
obtain ⟨δ, δ_pos, hδr, hδ⟩ := exists_lt_of_gauge_lt hs₂ (h.trans_lt hr),
suffices : (r⁻¹ * δ) • δ⁻¹ • x ∈ s,
{ rwa [smul_smul, mul_inv_cancel_right₀ δ_pos.ne'] at this },
rw mem_smul_set_iff_inv_smul_mem₀ δ_pos.ne' at hδ,
refine hs₁.smul_mem_of_zero_mem hs₀ hδ ⟨by positivity, _⟩,
rw [inv_mul_le_iff hr', mul_one],
exact hδr.le },
{ have hε' := (lt_add_iff_pos_right a).2 (half_pos hε),
exact (gauge_le_of_mem (ha.trans hε'.le) $ h _ hε').trans_lt
(add_lt_add_left (half_lt_self hε) _) }
end
lemma gauge_lt_eq' (absorbs : absorbent ℝ s) (a : ℝ) :
{x | gauge s x < a} = ⋃ (r : ℝ) (H : 0 < r) (H : r < a), r • s :=
begin
ext,
simp_rw [mem_set_of_eq, mem_Union, exists_prop],
exact ⟨exists_lt_of_gauge_lt absorbs,
λ ⟨r, hr₀, hr₁, hx⟩, (gauge_le_of_mem hr₀.le hx).trans_lt hr₁⟩,
end
lemma gauge_lt_eq (absorbs : absorbent ℝ s) (a : ℝ) :
{x | gauge s x < a} = ⋃ (r ∈ set.Ioo 0 (a : ℝ)), r • s :=
begin
ext,
simp_rw [mem_set_of_eq, mem_Union, exists_prop, mem_Ioo, and_assoc],
exact ⟨exists_lt_of_gauge_lt absorbs,
λ ⟨r, hr₀, hr₁, hx⟩, (gauge_le_of_mem hr₀.le hx).trans_lt hr₁⟩,
end
lemma gauge_lt_one_subset_self (hs : convex ℝ s) (h₀ : (0 : E) ∈ s) (absorbs : absorbent ℝ s) :
{x | gauge s x < 1} ⊆ s :=
begin
rw gauge_lt_eq absorbs,
refine set.Union₂_subset (λ r hr _, _),
rintro ⟨y, hy, rfl⟩,
exact hs.smul_mem_of_zero_mem h₀ hy (Ioo_subset_Icc_self hr),
end
lemma gauge_le_one_of_mem {x : E} (hx : x ∈ s) : gauge s x ≤ 1 :=
gauge_le_of_mem zero_le_one $ by rwa one_smul
lemma self_subset_gauge_le_one : s ⊆ {x | gauge s x ≤ 1} := λ x, gauge_le_one_of_mem
lemma convex.gauge_le (hs : convex ℝ s) (h₀ : (0 : E) ∈ s) (absorbs : absorbent ℝ s) (a : ℝ) :
convex ℝ {x | gauge s x ≤ a} :=
begin
by_cases ha : 0 ≤ a,
{ rw gauge_le_eq hs h₀ absorbs ha,
exact convex_Inter (λ i, convex_Inter (λ hi, hs.smul _)) },
{ convert convex_empty,
exact eq_empty_iff_forall_not_mem.2 (λ x hx, ha $ (gauge_nonneg _).trans hx) }
end
lemma balanced.star_convex (hs : balanced ℝ s) : star_convex ℝ 0 s :=
star_convex_zero_iff.2 $ λ x hx a ha₀ ha₁,
hs _ (by rwa real.norm_of_nonneg ha₀) (smul_mem_smul_set hx)
lemma le_gauge_of_not_mem (hs₀ : star_convex ℝ 0 s) (hs₂ : absorbs ℝ s {x}) (hx : x ∉ a • s) :
a ≤ gauge s x :=
begin
rw star_convex_zero_iff at hs₀,
obtain ⟨r, hr, h⟩ := hs₂,
refine le_cInf ⟨r, hr, singleton_subset_iff.1 $ h _ (real.norm_of_nonneg hr.le).ge⟩ _,
rintro b ⟨hb, x, hx', rfl⟩,
refine not_lt.1 (λ hba, hx _),
have ha := hb.trans hba,
refine ⟨(a⁻¹ * b) • x, hs₀ hx' (by positivity) _, _⟩,
{ rw ←div_eq_inv_mul,
exact div_le_one_of_le hba.le ha.le },
{ rw [←mul_smul, mul_inv_cancel_left₀ ha.ne'] }
end
lemma one_le_gauge_of_not_mem (hs₁ : star_convex ℝ 0 s) (hs₂ : absorbs ℝ s {x}) (hx : x ∉ s) :
1 ≤ gauge s x :=
le_gauge_of_not_mem hs₁ hs₂ $ by rwa one_smul
section linear_ordered_field
variables {α : Type*} [linear_ordered_field α] [mul_action_with_zero α ℝ] [ordered_smul α ℝ]
lemma gauge_smul_of_nonneg [mul_action_with_zero α E] [is_scalar_tower α ℝ (set E)] {s : set E}
{a : α} (ha : 0 ≤ a) (x : E) :
gauge s (a • x) = a • gauge s x :=
begin
obtain rfl | ha' := ha.eq_or_lt,
{ rw [zero_smul, gauge_zero, zero_smul] },
rw [gauge_def', gauge_def', ←real.Inf_smul_of_nonneg ha],
congr' 1,
ext r,
simp_rw [set.mem_smul_set, set.mem_sep_iff],
split,
{ rintro ⟨hr, hx⟩,
simp_rw mem_Ioi at ⊢ hr,
rw ←mem_smul_set_iff_inv_smul_mem₀ hr.ne' at hx,
have := smul_pos (inv_pos.2 ha') hr,
refine ⟨a⁻¹ • r, ⟨this, _⟩, smul_inv_smul₀ ha'.ne' _⟩,
rwa [←mem_smul_set_iff_inv_smul_mem₀ this.ne', smul_assoc,
mem_smul_set_iff_inv_smul_mem₀ (inv_ne_zero ha'.ne'), inv_inv] },
{ rintro ⟨r, ⟨hr, hx⟩, rfl⟩,
rw mem_Ioi at ⊢ hr,
rw ←mem_smul_set_iff_inv_smul_mem₀ hr.ne' at hx,
have := smul_pos ha' hr,
refine ⟨this, _⟩,
rw [←mem_smul_set_iff_inv_smul_mem₀ this.ne', smul_assoc],
exact smul_mem_smul_set hx }
end
lemma gauge_smul_left_of_nonneg [mul_action_with_zero α E] [smul_comm_class α ℝ ℝ]
[is_scalar_tower α ℝ ℝ] [is_scalar_tower α ℝ E] {s : set E} {a : α} (ha : 0 ≤ a) :
gauge (a • s) = a⁻¹ • gauge s :=
begin
obtain rfl | ha' := ha.eq_or_lt,
{ rw [inv_zero, zero_smul, gauge_of_subset_zero (zero_smul_set_subset _)] },
ext,
rw [gauge_def', pi.smul_apply, gauge_def', ←real.Inf_smul_of_nonneg (inv_nonneg.2 ha)],
congr' 1,
ext r,
simp_rw [set.mem_smul_set, set.mem_sep_iff],
split,
{ rintro ⟨hr, y, hy, h⟩,
simp_rw [mem_Ioi] at ⊢ hr,
refine ⟨a • r, ⟨smul_pos ha' hr, _⟩, inv_smul_smul₀ ha'.ne' _⟩,
rwa [smul_inv₀, smul_assoc, ←h, inv_smul_smul₀ ha'.ne'] },
{ rintro ⟨r, ⟨hr, hx⟩, rfl⟩,
rw mem_Ioi at ⊢ hr,
have := smul_pos ha' hr,
refine ⟨smul_pos (inv_pos.2 ha') hr, r⁻¹ • x, hx, _⟩,
rw [smul_inv₀, smul_assoc, inv_inv] }
end
lemma gauge_smul_left [module α E] [smul_comm_class α ℝ ℝ] [is_scalar_tower α ℝ ℝ]
[is_scalar_tower α ℝ E] {s : set E} (symmetric : ∀ x ∈ s, -x ∈ s) (a : α) :
gauge (a • s) = |a|⁻¹ • gauge s :=
begin
rw ←gauge_smul_left_of_nonneg (abs_nonneg a),
obtain h | h := abs_choice a,
{ rw h },
{ rw [h, set.neg_smul_set, ←set.smul_set_neg],
congr,
ext y,
refine ⟨symmetric _, λ hy, _⟩,
rw ←neg_neg y,
exact symmetric _ hy },
{ apply_instance }
end
end linear_ordered_field
section is_R_or_C
variables [is_R_or_C 𝕜] [module 𝕜 E] [is_scalar_tower ℝ 𝕜 E]
lemma gauge_norm_smul (hs : balanced 𝕜 s) (r : 𝕜) (x : E) : gauge s (‖r‖ • x) = gauge s (r • x) :=
begin
unfold gauge,
congr' with θ,
rw @is_R_or_C.real_smul_eq_coe_smul 𝕜,
refine and_congr_right (λ hθ, (hs.smul _).mem_smul_iff _),
rw [is_R_or_C.norm_of_real, abs_norm],
end
/-- If `s` is balanced, then the Minkowski functional is ℂ-homogeneous. -/
lemma gauge_smul (hs : balanced 𝕜 s) (r : 𝕜) (x : E) : gauge s (r • x) = ‖r‖ * gauge s x :=
by { rw [←smul_eq_mul, ←gauge_smul_of_nonneg (norm_nonneg r), gauge_norm_smul hs], apply_instance }
end is_R_or_C
section topological_space
variables [topological_space E] [has_continuous_smul ℝ E]
lemma interior_subset_gauge_lt_one (s : set E) : interior s ⊆ {x | gauge s x < 1} :=
begin
intros x hx,
let f : ℝ → E := λ t, t • x,
have hf : continuous f,
{ continuity },
let s' := f ⁻¹' (interior s),
have hs' : is_open s' := hf.is_open_preimage _ is_open_interior,
have one_mem : (1 : ℝ) ∈ s',
{ simpa only [s', f, set.mem_preimage, one_smul] },
obtain ⟨ε, hε₀, hε⟩ := (metric.nhds_basis_closed_ball.1 _).1
(is_open_iff_mem_nhds.1 hs' 1 one_mem),
rw real.closed_ball_eq_Icc at hε,
have hε₁ : 0 < 1 + ε := hε₀.trans (lt_one_add ε),
have : (1 + ε)⁻¹ < 1,
{ rw inv_lt_one_iff,
right,
linarith },
refine (gauge_le_of_mem (inv_nonneg.2 hε₁.le) _).trans_lt this,
rw mem_inv_smul_set_iff₀ hε₁.ne',
exact interior_subset
(hε ⟨(sub_le_self _ hε₀.le).trans ((le_add_iff_nonneg_right _).2 hε₀.le), le_rfl⟩),
end
lemma gauge_lt_one_eq_self_of_open (hs₁ : convex ℝ s) (hs₀ : (0 : E) ∈ s) (hs₂ : is_open s) :
{x | gauge s x < 1} = s :=
begin
refine (gauge_lt_one_subset_self hs₁ ‹_› $ absorbent_nhds_zero $ hs₂.mem_nhds hs₀).antisymm _,
convert interior_subset_gauge_lt_one s,
exact hs₂.interior_eq.symm,
end
lemma gauge_lt_one_of_mem_of_open (hs₁ : convex ℝ s) (hs₀ : (0 : E) ∈ s) (hs₂ : is_open s)
{x : E} (hx : x ∈ s) :
gauge s x < 1 :=
by rwa ←gauge_lt_one_eq_self_of_open hs₁ hs₀ hs₂ at hx
lemma gauge_lt_of_mem_smul (x : E) (ε : ℝ) (hε : 0 < ε) (hs₀ : (0 : E) ∈ s)
(hs₁ : convex ℝ s) (hs₂ : is_open s) (hx : x ∈ ε • s) :
gauge s x < ε :=
begin
have : ε⁻¹ • x ∈ s,
{ rwa ←mem_smul_set_iff_inv_smul_mem₀ hε.ne' },
have h_gauge_lt := gauge_lt_one_of_mem_of_open hs₁ hs₀ hs₂ this,
rwa [gauge_smul_of_nonneg (inv_nonneg.2 hε.le), smul_eq_mul, inv_mul_lt_iff hε, mul_one]
at h_gauge_lt,
apply_instance
end
end topological_space
lemma gauge_add_le (hs : convex ℝ s) (absorbs : absorbent ℝ s) (x y : E) :
gauge s (x + y) ≤ gauge s x + gauge s y :=
begin
refine le_of_forall_pos_lt_add (λ ε hε, _),
obtain ⟨a, ha, ha', hx⟩ := exists_lt_of_gauge_lt absorbs
(lt_add_of_pos_right (gauge s x) (half_pos hε)),
obtain ⟨b, hb, hb', hy⟩ := exists_lt_of_gauge_lt absorbs
(lt_add_of_pos_right (gauge s y) (half_pos hε)),
rw mem_smul_set_iff_inv_smul_mem₀ ha.ne' at hx,
rw mem_smul_set_iff_inv_smul_mem₀ hb.ne' at hy,
suffices : gauge s (x + y) ≤ a + b,
{ linarith },
have hab : 0 < a + b := add_pos ha hb,
apply gauge_le_of_mem hab.le,
have := convex_iff_div.1 hs hx hy ha.le hb.le hab,
rwa [smul_smul, smul_smul, ←mul_div_right_comm, ←mul_div_right_comm, mul_inv_cancel ha.ne',
mul_inv_cancel hb.ne', ←smul_add, one_div, ←mem_smul_set_iff_inv_smul_mem₀ hab.ne'] at this,
end
section is_R_or_C
variables [is_R_or_C 𝕜] [module 𝕜 E] [is_scalar_tower ℝ 𝕜 E]
/-- `gauge s` as a seminorm when `s` is balanced, convex and absorbent. -/
@[simps] def gauge_seminorm (hs₀ : balanced 𝕜 s) (hs₁ : convex ℝ s) (hs₂ : absorbent ℝ s) :
seminorm 𝕜 E :=
seminorm.of (gauge s) (gauge_add_le hs₁ hs₂) (gauge_smul hs₀)
variables {hs₀ : balanced 𝕜 s} {hs₁ : convex ℝ s} {hs₂ : absorbent ℝ s} [topological_space E]
[has_continuous_smul ℝ E]
lemma gauge_seminorm_lt_one_of_open (hs : is_open s) {x : E} (hx : x ∈ s) :
gauge_seminorm hs₀ hs₁ hs₂ x < 1 :=
gauge_lt_one_of_mem_of_open hs₁ hs₂.zero_mem hs hx
lemma gauge_seminorm_ball_one (hs : is_open s) :
(gauge_seminorm hs₀ hs₁ hs₂).ball 0 1 = s :=
begin
rw seminorm.ball_zero_eq,
exact gauge_lt_one_eq_self_of_open hs₁ hs₂.zero_mem hs
end
end is_R_or_C
/-- Any seminorm arises as the gauge of its unit ball. -/
@[simp] protected lemma seminorm.gauge_ball (p : seminorm ℝ E) : gauge (p.ball 0 1) = p :=
begin
ext,
obtain hp | hp := {r : ℝ | 0 < r ∧ x ∈ r • p.ball 0 1}.eq_empty_or_nonempty,
{ rw [gauge, hp, real.Inf_empty],
by_contra,
have hpx : 0 < p x := (map_nonneg _ _).lt_of_ne h,
have hpx₂ : 0 < 2 * p x := mul_pos zero_lt_two hpx,
refine hp.subset ⟨hpx₂, (2 * p x)⁻¹ • x, _, smul_inv_smul₀ hpx₂.ne' _⟩,
rw [p.mem_ball_zero, map_smul_eq_mul, real.norm_eq_abs, abs_of_pos (inv_pos.2 hpx₂),
inv_mul_lt_iff hpx₂, mul_one],
exact lt_mul_of_one_lt_left hpx one_lt_two },
refine is_glb.cInf_eq ⟨λ r, _, λ r hr, le_of_forall_pos_le_add $ λ ε hε, _⟩ hp,
{ rintro ⟨hr, y, hy, rfl⟩,
rw p.mem_ball_zero at hy,
rw [map_smul_eq_mul, real.norm_eq_abs, abs_of_pos hr],
exact mul_le_of_le_one_right hr.le hy.le },
{ have hpε : 0 < p x + ε := by positivity,
refine hr ⟨hpε, (p x + ε)⁻¹ • x, _, smul_inv_smul₀ hpε.ne' _⟩,
rw [p.mem_ball_zero, map_smul_eq_mul, real.norm_eq_abs, abs_of_pos (inv_pos.2 hpε),
inv_mul_lt_iff hpε, mul_one],
exact lt_add_of_pos_right _ hε }
end
lemma seminorm.gauge_seminorm_ball (p : seminorm ℝ E) :
gauge_seminorm (p.balanced_ball_zero 1) (p.convex_ball 0 1)
(p.absorbent_ball_zero zero_lt_one) = p := fun_like.coe_injective p.gauge_ball
end add_comm_group
section norm
variables [seminormed_add_comm_group E] [normed_space ℝ E] {s : set E} {r : ℝ} {x : E}
lemma gauge_unit_ball (x : E) : gauge (metric.ball (0 : E) 1) x = ‖x‖ :=
by rw [← ball_norm_seminorm ℝ, seminorm.gauge_ball, coe_norm_seminorm]
lemma gauge_ball (hr : 0 < r) (x : E) : gauge (metric.ball (0 : E) r) x = ‖x‖ / r :=
begin
rw [←smul_unit_ball_of_pos hr, gauge_smul_left, pi.smul_apply, gauge_unit_ball, smul_eq_mul,
abs_of_nonneg hr.le, div_eq_inv_mul],
simp_rw [mem_ball_zero_iff, norm_neg],
exact λ _, id,
end
lemma mul_gauge_le_norm (hs : metric.ball (0 : E) r ⊆ s) : r * gauge s x ≤ ‖x‖ :=
begin
obtain hr | hr := le_or_lt r 0,
{ exact (mul_nonpos_of_nonpos_of_nonneg hr $ gauge_nonneg _).trans (norm_nonneg _) },
rw [mul_comm, ←le_div_iff hr, ←gauge_ball hr],
exact gauge_mono (absorbent_ball_zero hr) hs x,
end
lemma convex.lipschitz_with_gauge {r : ℝ≥0} (hc : convex ℝ s) (hr : 0 < r)
(hs : metric.ball (0 : E) r ⊆ s) :
lipschitz_with r⁻¹ (gauge s) :=
have absorbent ℝ (metric.ball (0 : E) r) := absorbent_ball_zero hr,
lipschitz_with.of_le_add_mul _ $ λ x y,
calc gauge s x = gauge s (y + (x - y)) : by simp
... ≤ gauge s y + gauge s (x - y) : gauge_add_le hc (this.subset hs) _ _
... ≤ gauge s y + ‖x - y‖ / r :
add_le_add_left ((gauge_mono this hs (x - y)).trans_eq (gauge_ball hr _)) _
... = gauge s y + r⁻¹ * dist x y : by rw [dist_eq_norm, div_eq_inv_mul]
lemma convex.uniform_continuous_gauge (hc : convex ℝ s) (h₀ : s ∈ 𝓝 (0 : E)) :
uniform_continuous (gauge s) :=
begin
obtain ⟨r, hr₀, hr⟩ := metric.mem_nhds_iff.1 h₀,
lift r to ℝ≥0 using le_of_lt hr₀,
exact (hc.lipschitz_with_gauge hr₀ hr).uniform_continuous
end
end norm
|
71aa4c835a2f442744d3e4cdfa32fba7fb50dbd2 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/category_theory/comma.lean | 476c1c7b87219b655777a71f024408edf69608f0 | [
"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,435 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Johan Commelin, Bhavik Mehta
-/
import category_theory.natural_isomorphism
/-!
# Comma categories
A comma category is a construction in category theory, which builds a category out of two functors
with a common codomain. Specifically, for functors `L : A ⥤ T` and `R : B ⥤ T`, an object in
`comma L R` is a morphism `hom : L.obj left ⟶ R.obj right` for some objects `left : A` and
`right : B`, and a morphism in `comma L R` between `hom : L.obj left ⟶ R.obj right` and
`hom' : L.obj left' ⟶ R.obj right'` is a commutative square
```
L.obj left ⟶ L.obj left'
| |
hom | | hom'
↓ ↓
R.obj right ⟶ R.obj right',
```
where the top and bottom morphism come from morphisms `left ⟶ left'` and `right ⟶ right'`,
respectively.
## Main definitions
* `comma L R`: the comma category of the functors `L` and `R`.
* `over X`: the over category of the object `X` (developed in `over.lean`).
* `under X`: the under category of the object `X` (also developed in `over.lean`).
* `arrow T`: the arrow category of the category `T` (developed in `arrow.lean`).
## References
* <https://ncatlab.org/nlab/show/comma+category>
## Tags
comma, slice, coslice, over, under, arrow
-/
namespace category_theory
-- declare the `v`'s first; see `category_theory.category` for an explanation
universes v₁ v₂ v₃ u₁ u₂ u₃
variables {A : Type u₁} [category.{v₁} A]
variables {B : Type u₂} [category.{v₂} B]
variables {T : Type u₃} [category.{v₃} T]
/-- The objects of the comma category are triples of an object `left : A`, an object
`right : B` and a morphism `hom : L.obj left ⟶ R.obj right`. -/
structure comma (L : A ⥤ T) (R : B ⥤ T) : Type (max u₁ u₂ v₃) :=
(left : A . obviously)
(right : B . obviously)
(hom : L.obj left ⟶ R.obj right)
-- Satisfying the inhabited linter
instance comma.inhabited [inhabited T] : inhabited (comma (𝟭 T) (𝟭 T)) :=
{ default :=
{ left := default T,
right := default T,
hom := 𝟙 (default T) } }
variables {L : A ⥤ T} {R : B ⥤ T}
/-- A morphism between two objects in the comma category is a commutative square connecting the
morphisms coming from the two objects using morphisms in the image of the functors `L` and `R`.
-/
@[ext] structure comma_morphism (X Y : comma L R) :=
(left : X.left ⟶ Y.left . obviously)
(right : X.right ⟶ Y.right . obviously)
(w' : L.map left ≫ Y.hom = X.hom ≫ R.map right . obviously)
-- Satisfying the inhabited linter
instance comma_morphism.inhabited [inhabited (comma L R)] :
inhabited (comma_morphism (default (comma L R)) (default (comma L R))) :=
{ default :=
{ left := 𝟙 _,
right := 𝟙 _ } }
restate_axiom comma_morphism.w'
attribute [simp, reassoc] comma_morphism.w
instance comma_category : category (comma L R) :=
{ hom := comma_morphism,
id := λ X,
{ left := 𝟙 X.left,
right := 𝟙 X.right },
comp := λ X Y Z f g,
{ left := f.left ≫ g.left,
right := f.right ≫ g.right } }
namespace comma
section
variables {X Y Z : comma L R} {f : X ⟶ Y} {g : Y ⟶ Z}
@[simp] lemma id_left : ((𝟙 X) : comma_morphism X X).left = 𝟙 X.left := rfl
@[simp] lemma id_right : ((𝟙 X) : comma_morphism X X).right = 𝟙 X.right := rfl
@[simp] lemma comp_left : (f ≫ g).left = f.left ≫ g.left := rfl
@[simp] lemma comp_right : (f ≫ g).right = f.right ≫ g.right := rfl
end
variables (L) (R)
/-- The functor sending an object `X` in the comma category to `X.left`. -/
@[simps]
def fst : comma L R ⥤ A :=
{ obj := λ X, X.left,
map := λ _ _ f, f.left }
/-- The functor sending an object `X` in the comma category to `X.right`. -/
@[simps]
def snd : comma L R ⥤ B :=
{ obj := λ X, X.right,
map := λ _ _ f, f.right }
/-- We can interpret the commutative square constituting a morphism in the comma category as a
natural transformation between the functors `fst ⋙ L` and `snd ⋙ R` from the comma category
to `T`, where the components are given by the morphism that constitutes an object of the comma
category. -/
@[simps]
def nat_trans : fst L R ⋙ L ⟶ snd L R ⋙ R :=
{ app := λ X, X.hom }
section
variables {L₁ L₂ L₃ : A ⥤ T} {R₁ R₂ R₃ : B ⥤ T}
/--
Construct an isomorphism in the comma category given isomorphisms of the objects whose forward
directions give a commutative square.
-/
@[simps]
def iso_mk {X Y : comma L₁ R₁} (l : X.left ≅ Y.left) (r : X.right ≅ Y.right)
(h : L₁.map l.hom ≫ Y.hom = X.hom ≫ R₁.map r.hom) : X ≅ Y :=
{ hom := { left := l.hom, right := r.hom },
inv :=
{ left := l.inv,
right := r.inv,
w' := begin
rw [←L₁.map_iso_inv l, iso.inv_comp_eq, L₁.map_iso_hom, reassoc_of h, ← R₁.map_comp],
simp
end, } }
/-- A natural transformation `L₁ ⟶ L₂` induces a functor `comma L₂ R ⥤ comma L₁ R`. -/
@[simps]
def map_left (l : L₁ ⟶ L₂) : comma L₂ R ⥤ comma L₁ R :=
{ obj := λ X,
{ left := X.left,
right := X.right,
hom := l.app X.left ≫ X.hom },
map := λ X Y f,
{ left := f.left,
right := f.right } }
/-- The functor `comma L R ⥤ comma L R` induced by the identity natural transformation on `L` is
naturally isomorphic to the identity functor. -/
@[simps]
def map_left_id : map_left R (𝟙 L) ≅ 𝟭 _ :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
/-- The functor `comma L₁ R ⥤ comma L₃ R` induced by the composition of two natural transformations
`l : L₁ ⟶ L₂` and `l' : L₂ ⟶ L₃` is naturally isomorphic to the composition of the two functors
induced by these natural transformations. -/
@[simps]
def map_left_comp (l : L₁ ⟶ L₂) (l' : L₂ ⟶ L₃) :
(map_left R (l ≫ l')) ≅ (map_left R l') ⋙ (map_left R l) :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
/-- A natural transformation `R₁ ⟶ R₂` induces a functor `comma L R₁ ⥤ comma L R₂`. -/
@[simps]
def map_right (r : R₁ ⟶ R₂) : comma L R₁ ⥤ comma L R₂ :=
{ obj := λ X,
{ left := X.left,
right := X.right,
hom := X.hom ≫ r.app X.right },
map := λ X Y f,
{ left := f.left,
right := f.right } }
/-- The functor `comma L R ⥤ comma L R` induced by the identity natural transformation on `R` is
naturally isomorphic to the identity functor. -/
@[simps]
def map_right_id : map_right L (𝟙 R) ≅ 𝟭 _ :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
/-- The functor `comma L R₁ ⥤ comma L R₃` induced by the composition of the natural transformations
`r : R₁ ⟶ R₂` and `r' : R₂ ⟶ R₃` is naturally isomorphic to the composition of the functors
induced by these natural transformations. -/
@[simps]
def map_right_comp (r : R₁ ⟶ R₂) (r' : R₂ ⟶ R₃) :
(map_right L (r ≫ r')) ≅ (map_right L r) ⋙ (map_right L r') :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
end
end comma
end category_theory
|
07e304b95335064aac892b97f8bc48fe5a2bf7e2 | 35960c5b117752aca7e3e7767c0b393e4dbd72a7 | /src/sch/subst.lean | 04fddf950f0735342422f2944c54ea8f70b2b351 | [
"Apache-2.0"
] | permissive | spl/tts | 461dc76b83df8db47e4660d0941dc97e6d4fd7d1 | b65298fea68ce47c8ed3ba3dbce71c1a20dd3481 | refs/heads/master | 1,541,049,198,347 | 1,537,967,023,000 | 1,537,967,029,000 | 119,653,145 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,121 | lean | import .core
namespace tts ------------------------------------------------------------------
namespace sch ------------------------------------------------------------------
variables {V : Type} [_root_.decidable_eq V] -- Type of variable names
variables {vs : list V} -- List of variable names
variables {nd : vs.nodup} -- No duplicate variables names
variables {x x₁ x₂ : tagged V} -- Variables
variables {xs : list (tagged V)} -- List of variable names
variables {t tx t₁ t₂ : typ V} -- Types
variables {ts txs : list (typ V)} -- Lists of types
variables {s : sch V} -- Type schemes
open list
/-- Substitute a free variable for a type in a scheme -/
def subst (x : tagged V) (t : typ V) (s : sch V) : sch V :=
⟨s.vars, typ.subst x t s.type, s.vars_nodup⟩
@[simp] theorem subst_mk : subst x tx (mk vs t nd) = mk vs (typ.subst x tx t) nd :=
rfl
/-- Substitute a list of free variables for a list of types in a scheme -/
def subst_list (xs : list (tagged V)) (ts : list (typ V)) (s : sch V) : sch V :=
⟨s.vars, typ.subst_list xs ts s.type, s.vars_nodup⟩
@[simp] theorem subst_list_mk :
subst_list xs txs (mk vs t nd) = mk vs (typ.subst_list xs txs t) nd :=
rfl
-- Substitution with a fresh name is the identity
@[simp] theorem subst_fresh (h : x ∉ fv s) : subst x t s = s :=
eq_of_veq rfl $ typ.subst_fresh h
-- Fold typ.subst into sch.subst
theorem subst_fold : mk vs (typ.subst x t₂ t₁) nd = subst x t₂ (mk vs t₁ nd) :=
rfl
-- Substitution distributes over open
theorem subst_open_typs (l : typ.lc t) :
typ.subst x t (open_typs ts s) = open_typs (map (typ.subst x t) ts) (subst x t s) :=
by cases s; simp [typ.subst_open_typs l]
-- Substitution distributes over open_vars
theorem subst_open_vars (h : x ∉ xs) (l : typ.lc t) :
open_vars xs (subst x t s) = typ.subst x t (open_vars xs s) :=
by cases s; simp [typ.subst_open_vars h l]
@[simp] theorem subst_arity : (subst x t s).arity = s.arity :=
by unfold subst arity
@[simp] theorem subst_type : (subst x t s).type = typ.subst x t s.type :=
by unfold subst
-- A scheme substituted with a type is well-formed if the scheme is well-formed
-- and the type is locally-closed.
theorem subst_well_formed (lt : typ.lc t) (ls : lc s) : lc (subst x t s) :=
begin
cases ls with L ls,
existsi insert x L,
intros xs d ln_eq F,
simp [not_or_distrib] at F,
have h₁ : x ∉ xs := λ h, absurd (eq.refl x) (F h).1,
have h₂ : ∀ x ∈ xs, x ∉ L := λ _ h, (F h).2,
simp [typ.subst_open_vars h₁ lt, typ.subst_lc lt (ls d ln_eq h₂)],
end
-- Opening up a scheme `s` with `ts` is the same as opening up `s` with fresh
-- names `xs` and then substituting `xs` for `ts`.
theorem subst_list_intro
(d : xs.nodup)
(ln_eq : xs.length = ts.length)
(F : ∀ x ∈ xs, x ∉ fv s ∪ typ.fv_list ts)
(l : ∀ t ∈ ts, typ.lc t) :
open_typs ts s = typ.subst_list xs ts (open_vars xs s) :=
by rw [open_typs, open_vars, typ.subst_list_intro d ln_eq F l]
end /- namespace -/ sch --------------------------------------------------------
end /- namespace -/ tts --------------------------------------------------------
|
9ae6e653a13a7259905c91187339a70cf527fcb0 | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/algebra/euclidean_domain.lean | a446c48b45e64ac52c83c3205b0d29112297a8a4 | [
"Apache-2.0"
] | permissive | fpvandoorn/mathlib | b21ab4068db079cbb8590b58fda9cc4bc1f35df4 | b3433a51ea8bc07c4159c1073838fc0ee9b8f227 | refs/heads/master | 1,624,791,089,608 | 1,556,715,231,000 | 1,556,715,231,000 | 165,722,980 | 5 | 0 | Apache-2.0 | 1,552,657,455,000 | 1,547,494,646,000 | Lean | UTF-8 | Lean | false | false | 13,472 | lean | /-
Copyright (c) 2018 Louis Carlin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Louis Carlin, Mario Carneiro
Euclidean domains and Euclidean algorithm (extended to come)
A lot is based on pre-existing code in mathlib for natural number gcds
-/
import data.int.basic
universe u
class euclidean_domain (α : Type u) extends nonzero_comm_ring α :=
(quotient : α → α → α)
(quotient_zero : ∀ a, quotient a 0 = 0)
(remainder : α → α → α)
-- This could be changed to the same order as int.mod_add_div.
-- We normally write qb+r rather than r + qb though.
(quotient_mul_add_remainder_eq : ∀ a b, b * quotient a b + remainder a b = a)
(r : α → α → Prop)
(r_well_founded : well_founded r)
(remainder_lt : ∀ a {b}, b ≠ 0 → r (remainder a b) b)
/- `val_le_mul_left` is often not a required in definitions of a euclidean
domain since given the other properties we can show there is a
(noncomputable) euclidean domain α with the property `val_le_mul_left`.
So potentially this definition could be split into two different ones
(euclidean_domain_weak and euclidean_domain_strong) with a noncomputable
function from weak to strong. I've currently divided the lemmas into
strong and weak depending on whether they require `val_le_mul_left` or not. -/
(mul_left_not_lt : ∀ a {b}, b ≠ 0 → ¬r (a * b) a)
namespace euclidean_domain
variable {α : Type u}
variables [euclidean_domain α]
local infix ` ≺ `:50 := euclidean_domain.r
instance : has_div α := ⟨quotient⟩
instance : has_mod α := ⟨remainder⟩
theorem div_add_mod (a b : α) : b * (a / b) + a % b = a :=
quotient_mul_add_remainder_eq _ _
lemma mod_eq_sub_mul_div {α : Type*} [euclidean_domain α] (a b : α) :
a % b = a - b * (a / b) :=
calc a % b = b * (a / b) + a % b - b * (a / b) : (add_sub_cancel' _ _).symm
... = a - b * (a / b) : by rw div_add_mod
theorem mod_lt : ∀ a {b : α}, b ≠ 0 → (a % b) ≺ b :=
remainder_lt
theorem mul_right_not_lt {a : α} (b) (h : a ≠ 0) : ¬(a * b) ≺ b :=
by rw mul_comm; exact mul_left_not_lt b h
lemma mul_div_cancel_left {a : α} (b) (a0 : a ≠ 0) : a * b / a = b :=
eq.symm $ eq_of_sub_eq_zero $ classical.by_contradiction $ λ h,
begin
have := mul_left_not_lt a h,
rw [mul_sub, sub_eq_iff_eq_add'.2 (div_add_mod (a*b) a).symm] at this,
exact this (mod_lt _ a0)
end
lemma mul_div_cancel (a) {b : α} (b0 : b ≠ 0) : a * b / b = a :=
by rw mul_comm; exact mul_div_cancel_left a b0
@[simp] lemma mod_zero (a : α) : a % 0 = a :=
by simpa only [zero_mul, zero_add] using div_add_mod a 0
@[simp] lemma mod_eq_zero {a b : α} : a % b = 0 ↔ b ∣ a :=
⟨λ h, by rw [← div_add_mod a b, h, add_zero]; exact dvd_mul_right _ _,
λ ⟨c, e⟩, begin
rw [e, ← add_left_cancel_iff, div_add_mod, add_zero],
haveI := classical.dec,
by_cases b0 : b = 0,
{ simp only [b0, zero_mul] },
{ rw [mul_div_cancel_left _ b0] }
end⟩
@[simp] lemma mod_self (a : α) : a % a = 0 :=
mod_eq_zero.2 (dvd_refl _)
lemma dvd_mod_iff {a b c : α} (h : c ∣ b) : c ∣ a % b ↔ c ∣ a :=
by rw [dvd_add_iff_right (dvd_mul_of_dvd_left h _), div_add_mod]
lemma lt_one (a : α) : a ≺ (1:α) → a = 0 :=
by haveI := classical.dec; exact
not_imp_not.1 (λ h, by simpa only [one_mul] using mul_left_not_lt 1 h)
lemma val_dvd_le : ∀ a b : α, b ∣ a → a ≠ 0 → ¬a ≺ b
| _ b ⟨d, rfl⟩ ha := mul_left_not_lt b (mt (by rintro rfl; exact mul_zero _) ha)
@[simp] lemma mod_one (a : α) : a % 1 = 0 :=
mod_eq_zero.2 (one_dvd _)
@[simp] lemma zero_mod (b : α) : 0 % b = 0 :=
mod_eq_zero.2 (dvd_zero _)
@[simp] lemma div_zero (a : α) : a / 0 = 0 :=
quotient_zero a
@[simp] lemma zero_div {a : α} : 0 / a = 0 :=
classical.by_cases
(λ a0 : a = 0, a0.symm ▸ div_zero 0)
(λ a0, by simpa only [zero_mul] using mul_div_cancel 0 a0)
@[simp] lemma div_self {a : α} (a0 : a ≠ 0) : a / a = 1 :=
by simpa only [one_mul] using mul_div_cancel 1 a0
lemma eq_div_of_mul_eq_left {a b c : α} (hb : b ≠ 0) (h : a * b = c) : a = c / b :=
by rw [← h, mul_div_cancel _ hb]
lemma eq_div_of_mul_eq_right {a b c : α} (ha : a ≠ 0) (h : a * b = c) : b = c / a :=
by rw [← h, mul_div_cancel_left _ ha]
theorem mul_div_assoc (x : α) {y z : α} (h : z ∣ y) : x * y / z = x * (y / z) :=
begin
classical, by_cases hz : z = 0,
{ subst hz, rw [div_zero, div_zero, mul_zero] },
rcases h with ⟨p, rfl⟩,
rw [mul_div_cancel_left _ hz, mul_left_comm, mul_div_cancel_left _ hz]
end
section gcd
variable [decidable_eq α]
def gcd : α → α → α
| a := λ b, if a0 : a = 0 then b else
have h:_ := mod_lt b a0,
gcd (b%a) a
using_well_founded {dec_tac := tactic.assumption,
rel_tac := λ _ _, `[exact ⟨_, r_well_founded α⟩]}
@[simp] theorem gcd_zero_left (a : α) : gcd 0 a = a :=
by rw gcd; exact if_pos rfl
@[simp] theorem gcd_zero_right (a : α) : gcd a 0 = a :=
by rw gcd; split_ifs; simp only [h, zero_mod, gcd_zero_left]
theorem gcd_val (a b : α) : gcd a b = gcd (b % a) a :=
by rw gcd; split_ifs; [simp only [h, mod_zero, gcd_zero_right], refl]
@[elab_as_eliminator]
theorem gcd.induction {P : α → α → Prop} : ∀ a b : α,
(∀ x, P 0 x) →
(∀ a b, a ≠ 0 → P (b % a) a → P a b) →
P a b
| a := λ b H0 H1, if a0 : a = 0 then by rw [a0]; apply H0 else
have h:_ := mod_lt b a0,
H1 _ _ a0 (gcd.induction (b%a) a H0 H1)
using_well_founded {dec_tac := tactic.assumption,
rel_tac := λ _ _, `[exact ⟨_, r_well_founded α⟩]}
theorem gcd_dvd (a b : α) : gcd a b ∣ a ∧ gcd a b ∣ b :=
gcd.induction a b
(λ b, by rw [gcd_zero_left]; exact ⟨dvd_zero _, dvd_refl _⟩)
(λ a b aneq ⟨IH₁, IH₂⟩, by rw gcd_val;
exact ⟨IH₂, (dvd_mod_iff IH₂).1 IH₁⟩)
theorem gcd_dvd_left (a b : α) : gcd a b ∣ a := (gcd_dvd a b).left
theorem gcd_dvd_right (a b : α) : gcd a b ∣ b := (gcd_dvd a b).right
protected theorem gcd_eq_zero_iff {a b : α} :
gcd a b = 0 ↔ a = 0 ∧ b = 0 :=
⟨λ h, by simpa [h] using gcd_dvd a b,
by rintro ⟨rfl, rfl⟩; simp⟩
theorem dvd_gcd {a b c : α} : c ∣ a → c ∣ b → c ∣ gcd a b :=
gcd.induction a b
(λ _ _ H, by simpa only [gcd_zero_left] using H)
(λ a b a0 IH ca cb, by rw gcd_val;
exact IH ((dvd_mod_iff ca).2 cb) ca)
theorem gcd_eq_left {a b : α} : gcd a b = a ↔ a ∣ b :=
⟨λ h, by rw ← h; apply gcd_dvd_right,
λ h, by rw [gcd_val, mod_eq_zero.2 h, gcd_zero_left]⟩
@[simp] theorem gcd_one_left (a : α) : gcd 1 a = 1 :=
gcd_eq_left.2 (one_dvd _)
@[simp] theorem gcd_self (a : α) : gcd a a = a :=
gcd_eq_left.2 (dvd_refl _)
def xgcd_aux : α → α → α → α → α → α → α × α × α
| r := λ s t r' s' t',
if hr : r = 0 then (r', s', t')
else
have r' % r ≺ r, from mod_lt _ hr,
let q := r' / r in xgcd_aux (r' % r) (s' - q * s) (t' - q * t) r s t
using_well_founded {dec_tac := tactic.assumption,
rel_tac := λ _ _, `[exact ⟨_, r_well_founded α⟩]}
@[simp] theorem xgcd_zero_left {s t r' s' t' : α} : xgcd_aux 0 s t r' s' t' = (r', s', t') :=
by unfold xgcd_aux; exact if_pos rfl
@[simp] theorem xgcd_aux_rec {r s t r' s' t' : α} (h : r ≠ 0) :
xgcd_aux r s t r' s' t' = xgcd_aux (r' % r) (s' - (r' / r) * s) (t' - (r' / r) * t) r s t :=
by conv {to_lhs, rw [xgcd_aux]}; exact if_neg h
/-- Use the extended GCD algorithm to generate the `a` and `b` values
satisfying `gcd x y = x * a + y * b`. -/
def xgcd (x y : α) : α × α := (xgcd_aux x 1 0 y 0 1).2
/-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/
def gcd_a (x y : α) : α := (xgcd x y).1
/-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/
def gcd_b (x y : α) : α := (xgcd x y).2
@[simp] theorem xgcd_aux_fst (x y : α) : ∀ s t s' t',
(xgcd_aux x s t y s' t').1 = gcd x y :=
gcd.induction x y (by intros; rw [xgcd_zero_left, gcd_zero_left])
(λ x y h IH s t s' t', by simp only [xgcd_aux_rec h, if_neg h, IH]; rw ← gcd_val)
theorem xgcd_aux_val (x y : α) : xgcd_aux x 1 0 y 0 1 = (gcd x y, xgcd x y) :=
by rw [xgcd, ← xgcd_aux_fst x y 1 0 0 1, prod.mk.eta]
theorem xgcd_val (x y : α) : xgcd x y = (gcd_a x y, gcd_b x y) :=
prod.mk.eta.symm
private def P (a b : α) : α × α × α → Prop | (r, s, t) := (r : α) = a * s + b * t
theorem xgcd_aux_P (a b : α) {r r' : α} : ∀ {s t s' t'}, P a b (r, s, t) →
P a b (r', s', t') → P a b (xgcd_aux r s t r' s' t') :=
gcd.induction r r' (by intros; simpa only [xgcd_zero_left]) $ λ x y h IH s t s' t' p p', begin
rw [xgcd_aux_rec h], refine IH _ p, unfold P at p p' ⊢,
rw [mul_sub, mul_sub, add_sub, sub_add_eq_add_sub, ← p', sub_sub,
mul_comm _ s, ← mul_assoc, mul_comm _ t, ← mul_assoc, ← add_mul, ← p,
mod_eq_sub_mul_div]
end
theorem gcd_eq_gcd_ab (a b : α) : (gcd a b : α) = a * gcd_a a b + b * gcd_b a b :=
by have := @xgcd_aux_P _ _ _ a b a b 1 0 0 1
(by rw [P, mul_one, mul_zero, add_zero]) (by rw [P, mul_one, mul_zero, zero_add]);
rwa [xgcd_aux_val, xgcd_val] at this
instance (α : Type*) [e : euclidean_domain α] : integral_domain α :=
by haveI := classical.dec_eq α; exact
{ eq_zero_or_eq_zero_of_mul_eq_zero :=
λ a b (h : a * b = 0), or_iff_not_and_not.2 $ λ h0 : a ≠ 0 ∧ b ≠ 0,
h0.1 $ by rw [← mul_div_cancel a h0.2, h, zero_div],
..e }
end gcd
section lcm
variables [decidable_eq α]
def lcm (x y : α) : α :=
x * y / gcd x y
theorem dvd_lcm_left (x y : α) : x ∣ lcm x y :=
classical.by_cases
(assume hxy : gcd x y = 0, by rw [lcm, hxy, div_zero]; exact dvd_zero _)
(λ hxy, let ⟨z, hz⟩ := (gcd_dvd x y).2 in ⟨z, eq.symm $ eq_div_of_mul_eq_left hxy $
by rw [mul_right_comm, mul_assoc, ← hz]⟩)
theorem dvd_lcm_right (x y : α) : y ∣ lcm x y :=
classical.by_cases
(assume hxy : gcd x y = 0, by rw [lcm, hxy, div_zero]; exact dvd_zero _)
(λ hxy, let ⟨z, hz⟩ := (gcd_dvd x y).1 in ⟨z, eq.symm $ eq_div_of_mul_eq_right hxy $
by rw [← mul_assoc, mul_right_comm, ← hz]⟩)
theorem lcm_dvd {x y z : α} (hxz : x ∣ z) (hyz : y ∣ z) : lcm x y ∣ z :=
begin
rw lcm, by_cases hxy : gcd x y = 0,
{ rw [hxy, div_zero], rw euclidean_domain.gcd_eq_zero_iff at hxy, rwa hxy.1 at hxz },
rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩,
suffices : x * y ∣ z * gcd x y,
{ cases this with p hp, use p,
generalize_hyp : gcd x y = g at hxy hs hp ⊢, subst hs,
rw [mul_left_comm, mul_div_cancel_left _ hxy, ← domain.mul_right_inj hxy, hp],
rw [← mul_assoc], simp only [mul_right_comm] },
rw [gcd_eq_gcd_ab, mul_add], apply dvd_add,
{ rw mul_left_comm, exact mul_dvd_mul_left _ (dvd_mul_of_dvd_left hyz _) },
{ rw [mul_left_comm, mul_comm], exact mul_dvd_mul_left _ (dvd_mul_of_dvd_left hxz _) }
end
@[simp] lemma lcm_dvd_iff {x y z : α} : lcm x y ∣ z ↔ x ∣ z ∧ y ∣ z :=
⟨λ hz, ⟨dvd_trans (dvd_lcm_left _ _) hz, dvd_trans (dvd_lcm_right _ _) hz⟩,
λ ⟨hxz, hyz⟩, lcm_dvd hxz hyz⟩
@[simp] lemma lcm_zero_left (x : α) : lcm 0 x = 0 :=
by rw [lcm, zero_mul, zero_div]
@[simp] lemma lcm_zero_right (x : α) : lcm x 0 = 0 :=
by rw [lcm, mul_zero, zero_div]
@[simp] lemma lcm_eq_zero_iff {x y : α} : lcm x y = 0 ↔ x = 0 ∨ y = 0 :=
begin
split,
{ intro hxy, rw [lcm, mul_div_assoc _ (gcd_dvd_right _ _), mul_eq_zero] at hxy,
apply or_of_or_of_imp_right hxy, intro hy,
by_cases hgxy : gcd x y = 0,
{ rw euclidean_domain.gcd_eq_zero_iff at hgxy, exact hgxy.2 },
{ rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩,
generalize_hyp : gcd x y = g at hr hs hy hgxy ⊢, subst hs,
rw [mul_div_cancel_left _ hgxy] at hy, rw [hy, mul_zero] } },
rintro (hx | hy),
{ rw [hx, lcm_zero_left] },
{ rw [hy, lcm_zero_right] }
end
@[simp] lemma gcd_mul_lcm (x y : α) : gcd x y * lcm x y = x * y :=
begin
rw lcm, by_cases h : gcd x y = 0,
{ rw [h, zero_mul], rw euclidean_domain.gcd_eq_zero_iff at h, rw [h.1, zero_mul] },
rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩,
generalize_hyp : gcd x y = g at h hr ⊢, subst hr,
rw [mul_assoc, mul_div_cancel_left _ h]
end
end lcm
end euclidean_domain
open euclidean_domain
instance int.euclidean_domain : euclidean_domain ℤ :=
{ quotient := (/),
quotient_zero := int.div_zero,
remainder := (%),
quotient_mul_add_remainder_eq := λ a b, by rw add_comm; exact int.mod_add_div _ _,
r := λ a b, a.nat_abs < b.nat_abs,
r_well_founded := measure_wf (λ a, int.nat_abs a),
remainder_lt := λ a b b0, int.coe_nat_lt.1 $
by rw [int.nat_abs_of_nonneg (int.mod_nonneg _ b0), ← int.abs_eq_nat_abs];
exact int.mod_lt _ b0,
mul_left_not_lt := λ a b b0, not_lt_of_ge $
by rw [← mul_one a.nat_abs, int.nat_abs_mul];
exact mul_le_mul_of_nonneg_left (int.nat_abs_pos_of_ne_zero b0) (nat.zero_le _) }
instance discrete_field.to_euclidean_domain {K : Type u} [discrete_field K] : euclidean_domain K :=
{ quotient := (/),
remainder := λ a b, if b = 0 then a else 0,
quotient_zero := div_zero,
quotient_mul_add_remainder_eq := λ a b,
if H : b = 0 then by rw [if_pos H, H, zero_mul, zero_add] else
by rw [if_neg H, add_zero, mul_div_cancel' _ H],
r := λ a b, a = 0 ∧ b ≠ 0,
r_well_founded := well_founded.intro $ λ a, acc.intro _ $ λ b ⟨hb, hna⟩,
acc.intro _ $ λ c ⟨hc, hnb⟩, false.elim $ hnb hb,
remainder_lt := λ a b hnb, ⟨if_neg hnb, hnb⟩,
mul_left_not_lt := λ a b hnb ⟨hab, hna⟩, or.cases_on (mul_eq_zero.1 hab) hna hnb }
|
3041801b6e0679ce15b1ead25ab271a0db0dba88 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/geometry/manifold/algebra/monoid.lean | 64c5c72065b9beada8edcda0a99646c5b1284644 | [
"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 | 15,181 | lean | /-
Copyright © 2020 Nicolò Cavalleri. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Nicolò Cavalleri
-/
import geometry.manifold.cont_mdiff_map
/-!
# Smooth monoid
A smooth monoid is a monoid that is also a smooth manifold, in which multiplication is a smooth map
of the product manifold `G` × `G` into `G`.
In this file we define the basic structures to talk about smooth monoids: `has_smooth_mul` and its
additive counterpart `has_smooth_add`. These structures are general enough to also talk about smooth
semigroups.
-/
open_locale manifold
/--
1. All smooth algebraic structures on `G` are `Prop`-valued classes that extend
`smooth_manifold_with_corners I G`. This way we save users from adding both
`[smooth_manifold_with_corners I G]` and `[has_smooth_mul I G]` to the assumptions. While many API
lemmas hold true without the `smooth_manifold_with_corners I G` assumption, we're not aware of a
mathematically interesting monoid on a topological manifold such that (a) the space is not a
`smooth_manifold_with_corners`; (b) the multiplication is smooth at `(a, b)` in the charts
`ext_chart_at I a`, `ext_chart_at I b`, `ext_chart_at I (a * b)`.
2. Because of `model_prod` we can't assume, e.g., that a `lie_group` is modelled on `𝓘(𝕜, E)`. So,
we formulate the definitions and lemmas for any model.
3. While smoothness of an operation implies its continuity, lemmas like
`has_continuous_mul_of_smooth` can't be instances becausen otherwise Lean would have to search for
`has_smooth_mul I G` with unknown `𝕜`, `E`, `H`, and `I : model_with_corners 𝕜 E H`. If users needs
`[has_continuous_mul G]` in a proof about a smooth monoid, then they need to either add
`[has_continuous_mul G]` as an assumption (worse) or use `haveI` in the proof (better). -/
library_note "Design choices about smooth algebraic structures"
/-- Basic hypothesis to talk about a smooth (Lie) additive monoid or a smooth additive
semigroup. A smooth additive monoid over `α`, for example, is obtained by requiring both the
instances `add_monoid α` and `has_smooth_add α`. -/
-- See note [Design choices about smooth algebraic structures]
@[ancestor smooth_manifold_with_corners]
class has_smooth_add {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{H : Type*} [topological_space H]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] (I : model_with_corners 𝕜 E H)
(G : Type*) [has_add G] [topological_space G] [charted_space H G]
extends smooth_manifold_with_corners I G : Prop :=
(smooth_add : smooth (I.prod I) I (λ p : G×G, p.1 + p.2))
/-- Basic hypothesis to talk about a smooth (Lie) monoid or a smooth semigroup.
A smooth monoid over `G`, for example, is obtained by requiring both the instances `monoid G`
and `has_smooth_mul I G`. -/
-- See note [Design choices about smooth algebraic structures]
@[ancestor smooth_manifold_with_corners, to_additive]
class has_smooth_mul {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{H : Type*} [topological_space H]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] (I : model_with_corners 𝕜 E H)
(G : Type*) [has_mul G] [topological_space G] [charted_space H G]
extends smooth_manifold_with_corners I G : Prop :=
(smooth_mul : smooth (I.prod I) I (λ p : G×G, p.1 * p.2))
section has_smooth_mul
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{H : Type*} [topological_space H]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {I : model_with_corners 𝕜 E H}
{G : Type*} [has_mul G] [topological_space G] [charted_space H G] [has_smooth_mul I G]
{E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'}
{M : Type*} [topological_space M] [charted_space H' M]
section
variables (I)
@[to_additive]
lemma smooth_mul : smooth (I.prod I) I (λ p : G×G, p.1 * p.2) :=
has_smooth_mul.smooth_mul
/-- If the multiplication is smooth, then it is continuous. This is not an instance for technical
reasons, see note [Design choices about smooth algebraic structures]. -/
@[to_additive
"If the addition is smooth, then it is continuous. This is not an instance for technical reasons,
see note [Design choices about smooth algebraic structures]."]
lemma has_continuous_mul_of_smooth : has_continuous_mul G :=
⟨(smooth_mul I).continuous⟩
end
section
variables {f g : M → G} {s : set M} {x : M} {n : ℕ∞}
@[to_additive]
lemma cont_mdiff_within_at.mul (hf : cont_mdiff_within_at I' I n f s x)
(hg : cont_mdiff_within_at I' I n g s x) : cont_mdiff_within_at I' I n (f * g) s x :=
((smooth_mul I).smooth_at.of_le le_top).comp_cont_mdiff_within_at x (hf.prod_mk hg)
@[to_additive]
lemma cont_mdiff_at.mul (hf : cont_mdiff_at I' I n f x) (hg : cont_mdiff_at I' I n g x) :
cont_mdiff_at I' I n (f * g) x :=
hf.mul hg
@[to_additive]
lemma cont_mdiff_on.mul (hf : cont_mdiff_on I' I n f s) (hg : cont_mdiff_on I' I n g s) :
cont_mdiff_on I' I n (f * g) s :=
λ x hx, (hf x hx).mul (hg x hx)
@[to_additive]
lemma cont_mdiff.mul (hf : cont_mdiff I' I n f) (hg : cont_mdiff I' I n g) :
cont_mdiff I' I n (f * g) :=
λ x, (hf x).mul (hg x)
@[to_additive]
lemma smooth_within_at.mul (hf : smooth_within_at I' I f s x)
(hg : smooth_within_at I' I g s x) : smooth_within_at I' I (f * g) s x :=
hf.mul hg
@[to_additive]
lemma smooth_at.mul (hf : smooth_at I' I f x) (hg : smooth_at I' I g x) :
smooth_at I' I (f * g) x :=
hf.mul hg
@[to_additive]
lemma smooth_on.mul (hf : smooth_on I' I f s) (hg : smooth_on I' I g s) :
smooth_on I' I (f * g) s :=
hf.mul hg
@[to_additive]
lemma smooth.mul (hf : smooth I' I f) (hg : smooth I' I g) :
smooth I' I (f * g) :=
hf.mul hg
@[to_additive]
lemma smooth_mul_left {a : G} : smooth I I (λ b : G, a * b) :=
smooth_const.mul smooth_id
@[to_additive]
lemma smooth_mul_right {a : G} : smooth I I (λ b : G, b * a) :=
smooth_id.mul smooth_const
end
variables (I) (g h : G)
/-- Left multiplication by `g`. It is meant to mimic the usual notation in Lie groups.
Lemmas involving `smooth_left_mul` with the notation `𝑳` usually use `L` instead of `𝑳` in the
names. -/
def smooth_left_mul : C^∞⟮I, G; I, G⟯ := ⟨(left_mul g), smooth_mul_left⟩
/-- Right multiplication by `g`. It is meant to mimic the usual notation in Lie groups.
Lemmas involving `smooth_right_mul` with the notation `𝑹` usually use `R` instead of `𝑹` in the
names. -/
def smooth_right_mul : C^∞⟮I, G; I, G⟯ := ⟨(right_mul g), smooth_mul_right⟩
/- Left multiplication. The abbreviation is `MIL`. -/
localized "notation (name := smooth_left_mul) `𝑳` := smooth_left_mul" in lie_group
/- Right multiplication. The abbreviation is `MIR`. -/
localized "notation (name := smooth_right_mul) `𝑹` := smooth_right_mul" in lie_group
open_locale lie_group
@[simp] lemma L_apply : (𝑳 I g) h = g * h := rfl
@[simp] lemma R_apply : (𝑹 I g) h = h * g := rfl
@[simp] lemma L_mul {G : Type*} [semigroup G] [topological_space G] [charted_space H G]
[has_smooth_mul I G] (g h : G) : 𝑳 I (g * h) = (𝑳 I g).comp (𝑳 I h) :=
by { ext, simp only [cont_mdiff_map.comp_apply, L_apply, mul_assoc] }
@[simp] lemma R_mul {G : Type*} [semigroup G] [topological_space G] [charted_space H G]
[has_smooth_mul I G] (g h : G) : 𝑹 I (g * h) = (𝑹 I h).comp (𝑹 I g) :=
by { ext, simp only [cont_mdiff_map.comp_apply, R_apply, mul_assoc] }
section
variables {G' : Type*} [monoid G'] [topological_space G'] [charted_space H G']
[has_smooth_mul I G'] (g' : G')
lemma smooth_left_mul_one : (𝑳 I g') 1 = g' := mul_one g'
lemma smooth_right_mul_one : (𝑹 I g') 1 = g' := one_mul g'
end
/- Instance of product -/
@[to_additive]
instance has_smooth_mul.prod {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
(G : Type*) [topological_space G] [charted_space H G]
[has_mul G] [has_smooth_mul I G]
{E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H')
(G' : Type*) [topological_space G'] [charted_space H' G']
[has_mul G'] [has_smooth_mul I' G'] :
has_smooth_mul (I.prod I') (G×G') :=
{ smooth_mul := ((smooth_fst.comp smooth_fst).smooth.mul (smooth_fst.comp smooth_snd)).prod_mk
((smooth_snd.comp smooth_fst).smooth.mul (smooth_snd.comp smooth_snd)),
.. smooth_manifold_with_corners.prod G G' }
end has_smooth_mul
section monoid
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{H : Type*} [topological_space H]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {I : model_with_corners 𝕜 E H}
{G : Type*} [monoid G] [topological_space G] [charted_space H G] [has_smooth_mul I G]
{H' : Type*} [topological_space H']
{E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E'] {I' : model_with_corners 𝕜 E' H'}
{G' : Type*} [monoid G'] [topological_space G'] [charted_space H' G'] [has_smooth_mul I' G']
lemma smooth_pow : ∀ n : ℕ, smooth I I (λ a : G, a ^ n)
| 0 := by { simp only [pow_zero], exact smooth_const }
| (k+1) := by simpa [pow_succ] using smooth_id.mul (smooth_pow _)
/-- Morphism of additive smooth monoids. -/
structure smooth_add_monoid_morphism
(I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H')
(G : Type*) [topological_space G] [charted_space H G] [add_monoid G] [has_smooth_add I G]
(G' : Type*) [topological_space G'] [charted_space H' G'] [add_monoid G'] [has_smooth_add I' G']
extends G →+ G' :=
(smooth_to_fun : smooth I I' to_fun)
/-- Morphism of smooth monoids. -/
@[to_additive] structure smooth_monoid_morphism
(I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H')
(G : Type*) [topological_space G] [charted_space H G] [monoid G] [has_smooth_mul I G]
(G' : Type*) [topological_space G'] [charted_space H' G'] [monoid G'] [has_smooth_mul I' G']
extends G →* G' :=
(smooth_to_fun : smooth I I' to_fun)
@[to_additive]
instance : has_one (smooth_monoid_morphism I I' G G') :=
⟨{ smooth_to_fun := smooth_const, to_monoid_hom := 1 }⟩
@[to_additive]
instance : inhabited (smooth_monoid_morphism I I' G G') := ⟨1⟩
@[to_additive]
instance : has_coe_to_fun (smooth_monoid_morphism I I' G G') (λ _, G → G') := ⟨λ a, a.to_fun⟩
end monoid
section comm_monoid
open_locale big_operators
variables {ι 𝕜 : Type*} [nontrivially_normed_field 𝕜]
{H : Type*} [topological_space H]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {I : model_with_corners 𝕜 E H}
{G : Type*} [comm_monoid G] [topological_space G] [charted_space H G] [has_smooth_mul I G]
{E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E']
{H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'}
{M : Type*} [topological_space M] [charted_space H' M] {s : set M} {x : M}
{t : finset ι} {f : ι → M → G} {n : ℕ∞} {p : ι → Prop}
@[to_additive]
lemma cont_mdiff_within_at_finset_prod' (h : ∀ i ∈ t, cont_mdiff_within_at I' I n (f i) s x) :
cont_mdiff_within_at I' I n (∏ i in t, f i) s x :=
finset.prod_induction f (λ f, cont_mdiff_within_at I' I n f s x)
(λ f g hf hg, hf.mul hg) cont_mdiff_within_at_const h
@[to_additive]
lemma cont_mdiff_at_finset_prod' (h : ∀ i ∈ t, cont_mdiff_at I' I n (f i) x) :
cont_mdiff_at I' I n (∏ i in t, f i) x :=
cont_mdiff_within_at_finset_prod' h
@[to_additive]
lemma cont_mdiff_on_finset_prod' (h : ∀ i ∈ t, cont_mdiff_on I' I n (f i) s) :
cont_mdiff_on I' I n (∏ i in t, f i) s :=
λ x hx, cont_mdiff_within_at_finset_prod' $ λ i hi, h i hi x hx
@[to_additive]
lemma cont_mdiff_finset_prod' (h : ∀ i ∈ t, cont_mdiff I' I n (f i)) :
cont_mdiff I' I n (∏ i in t, f i) :=
λ x, cont_mdiff_at_finset_prod' $ λ i hi, h i hi x
@[to_additive]
lemma cont_mdiff_within_at_finset_prod (h : ∀ i ∈ t, cont_mdiff_within_at I' I n (f i) s x) :
cont_mdiff_within_at I' I n (λ x, ∏ i in t, f i x) s x :=
by { simp only [← finset.prod_apply], exact cont_mdiff_within_at_finset_prod' h }
@[to_additive]
lemma cont_mdiff_at_finset_prod (h : ∀ i ∈ t, cont_mdiff_at I' I n (f i) x) :
cont_mdiff_at I' I n (λ x, ∏ i in t, f i x) x :=
cont_mdiff_within_at_finset_prod h
@[to_additive]
lemma cont_mdiff_on_finset_prod (h : ∀ i ∈ t, cont_mdiff_on I' I n (f i) s) :
cont_mdiff_on I' I n (λ x, ∏ i in t, f i x) s :=
λ x hx, cont_mdiff_within_at_finset_prod $ λ i hi, h i hi x hx
@[to_additive]
lemma cont_mdiff_finset_prod (h : ∀ i ∈ t, cont_mdiff I' I n (f i)) :
cont_mdiff I' I n (λ x, ∏ i in t, f i x) :=
λ x, cont_mdiff_at_finset_prod $ λ i hi, h i hi x
@[to_additive]
lemma smooth_within_at_finset_prod' (h : ∀ i ∈ t, smooth_within_at I' I (f i) s x) :
smooth_within_at I' I (∏ i in t, f i) s x :=
cont_mdiff_within_at_finset_prod' h
@[to_additive]
lemma smooth_at_finset_prod' (h : ∀ i ∈ t, smooth_at I' I (f i) x) :
smooth_at I' I (∏ i in t, f i) x :=
cont_mdiff_at_finset_prod' h
@[to_additive]
lemma smooth_on_finset_prod' (h : ∀ i ∈ t, smooth_on I' I (f i) s) :
smooth_on I' I (∏ i in t, f i) s :=
cont_mdiff_on_finset_prod' h
@[to_additive]
lemma smooth_finset_prod' (h : ∀ i ∈ t, smooth I' I (f i)) : smooth I' I (∏ i in t, f i) :=
cont_mdiff_finset_prod' h
@[to_additive]
lemma smooth_within_at_finset_prod (h : ∀ i ∈ t, smooth_within_at I' I (f i) s x) :
smooth_within_at I' I (λ x, ∏ i in t, f i x) s x :=
cont_mdiff_within_at_finset_prod h
@[to_additive]
lemma smooth_at_finset_prod (h : ∀ i ∈ t, smooth_at I' I (f i) x) :
smooth_at I' I (λ x, ∏ i in t, f i x) x :=
cont_mdiff_at_finset_prod h
@[to_additive]
lemma smooth_on_finset_prod (h : ∀ i ∈ t, smooth_on I' I (f i) s) :
smooth_on I' I (λ x, ∏ i in t, f i x) s :=
cont_mdiff_on_finset_prod h
@[to_additive]
lemma smooth_finset_prod (h : ∀ i ∈ t, smooth I' I (f i)) :
smooth I' I (λ x, ∏ i in t, f i x) :=
cont_mdiff_finset_prod h
open function filter
@[to_additive]
lemma cont_mdiff_finprod (h : ∀ i, cont_mdiff I' I n (f i))
(hfin : locally_finite (λ i, mul_support (f i))) :
cont_mdiff I' I n (λ x, ∏ᶠ i, f i x) :=
begin
intro x,
rcases finprod_eventually_eq_prod hfin x with ⟨s, hs⟩,
exact (cont_mdiff_finset_prod (λ i hi, h i) x).congr_of_eventually_eq hs,
end
@[to_additive]
lemma cont_mdiff_finprod_cond (hc : ∀ i, p i → cont_mdiff I' I n (f i))
(hf : locally_finite (λ i, mul_support (f i))) :
cont_mdiff I' I n (λ x, ∏ᶠ i (hi : p i), f i x) :=
begin
simp only [← finprod_subtype_eq_finprod_cond],
exact cont_mdiff_finprod (λ i, hc i i.2) (hf.comp_injective subtype.coe_injective)
end
@[to_additive]
lemma smooth_finprod (h : ∀ i, smooth I' I (f i)) (hfin : locally_finite (λ i, mul_support (f i))) :
smooth I' I (λ x, ∏ᶠ i, f i x) :=
cont_mdiff_finprod h hfin
@[to_additive]
lemma smooth_finprod_cond (hc : ∀ i, p i → smooth I' I (f i))
(hf : locally_finite (λ i, mul_support (f i))) :
smooth I' I (λ x, ∏ᶠ i (hi : p i), f i x) :=
cont_mdiff_finprod_cond hc hf
end comm_monoid
|
cbc2e16d20b6a8416832e14161b8749c31bac326 | 64a7884f2a9a760de30168e86a897e1ed06616d2 | /TPPmark/hagiwara/LongestCommon.lean | 73083d34ccefc19f2b3226e3f0cdc84e4dacc538 | [] | no_license | aigarashi/TPP2017 | b87b44649856bd092aa44e9d30dd2b99fecae8ac | 3d27301214cebccad6262315b822f81ab273940a | refs/heads/master | 1,630,127,109,936 | 1,512,981,388,000 | 1,512,981,388,000 | 104,860,417 | 1 | 2 | null | 1,512,542,842,000 | 1,506,415,863,000 | Coq | UTF-8 | Lean | false | false | 6,278 | lean | prelude
import init.data.list.lemmas
import init.data.nat.lemmas
import .Subsequences
variables {α : Type} {β : Type}
open list nat prod
namespace list
definition longest_com' (X Y : list ℕ) := length (com' X Y)
theorem com'_is_longest_com (Z : list ℕ × list ℕ) : longest_com (com' (fst Z) (snd Z)) (fst Z) (snd Z) :=
begin
assert H0 : ∀ Z : list ℕ × list ℕ, pair_length Z = 0 → longest_com (com' (fst Z) (snd Z)) (fst Z) (snd Z),
{intro Z1, cases Z1 with X Y,
unfold pair_length, intro h,
assert h1 : X = [], apply eq_nil_of_length_eq_zero, apply eq_zero_of_add_eq_zero_right h,
rw h1, rw com'_nil_left, apply nil_longest_com_left},
assert Hn : ∀ n : ℕ, (∀ Z : list ℕ × list ℕ, pair_length Z ≤ n → longest_com (com' (fst Z) (snd Z)) (fst Z) (snd Z))
→ ∀ Z : list ℕ × list ℕ, pair_length Z = n+1 → longest_com (com' (fst Z) (snd Z)) (fst Z) (snd Z),
{intros n hind Z hlZ,
cases Z with X Y,
change longest_com (com' X Y) X Y,
cases X with x X',
{rw com'_nil_left, apply nil_longest_com_left},
{cases Y with y Y',
{rw com'_nil_right, apply nil_longest_com_right},
{cases decidable.em (x=y) with hxy hnxy,
{rw -hxy,
assert hlXY'' : pair_length (X', Y') ≤ n,
{revert hlZ, unfold pair_length, unfold length, intro h1,
assert h2 : length X' + 1 + length Y' = n, revert h1, rw -add_assoc, apply nat.add_right_cancel,
rw -h2, rw add_assoc, rw (add_comm 1 (length Y')), rw -add_assoc,
change length X' + length Y' ≤ length X' + length Y' + 1, apply le_succ},
assert hcom' : com' (x::X') (x::Y') = x::(com' X' Y'), unfold com', unfold com, rw if_pos (eq.refl x),
rw hcom', apply iff.elim_right (longest_com_tail x (com' X' Y') X' Y'),
apply hind (X', Y'), apply hlXY''},
{unfold longest_com, apply and.intro,
{apply com'_implies_com_subseq},
{intros W h,
cases W with w W',
{unfold length, apply zero_le},
{unfold com', unfold com, rw if_neg hnxy,
change length (w::W') ≤ length (longer (com' (x::X') Y') (com' X' (y::Y'))), unfold longer,
cases decidable.em (length (com' (x::X') Y') ≤ length (com' X' (y::Y'))) with hle hg,
{rw if_pos hle,
cases decidable.em (x=w) with hxw hnxw,
{assert hlongX'Y : longest_com (com' (x::X') Y') (x::X') Y',
apply hind ((x::X'), Y'), apply le_of_eq, revert hlZ, unfold pair_length, unfold length, rw -add_assoc, apply add_right_cancel,
assert hW : com_subseq (w::W') (x::X') Y', apply and.intro, apply and.left h, apply or.elim (and.right h),
intro h2, apply absurd, apply (eq.trans hxw (and.left h2)), apply hnxy,
intro h2, apply h2,
assert hWle : length (w::W') ≤ length (com' (x::X') Y'), note h2 := and.right hlongX'Y, apply h2, apply hW,
apply le_trans, apply hWle, apply hle},
{assert hlongXY' : longest_com (com' X' (y::Y')) X' (y::Y'),
apply hind (X', (y::Y')), apply le_of_eq, revert hlZ, unfold pair_length, unfold length, simp, rw add_comm, intro hlZ, apply add_right_cancel, apply hlZ,
assert hW : com_subseq (w::W') X' (y::Y'), apply and.intro,
note h1 := and.left h, revert h1, unfold subseq, intro h1, apply or.elim h1,
intro h2, apply absurd, apply (eq.symm (and.left h2)), apply hnxw,
intro h2, apply h2,
apply and.right h,
note h2 := and.right hlongXY', apply h2, apply hW},},
{rw if_neg hg,
cases decidable.em (x=w) with hxw hnxw,
{assert hlongX'Y : longest_com (com' (x::X') Y') (x::X') Y',
apply hind ((x::X'), Y'), apply le_of_eq, revert hlZ, unfold pair_length, unfold length, rw -add_assoc, apply add_right_cancel,
assert hW : com_subseq (w::W') (x::X') Y', apply and.intro, apply and.left h, apply or.elim (and.right h),
intro h2, apply absurd, apply (eq.trans hxw (and.left h2)), apply hnxy,
intro h2, apply h2,
assert hWle : length (w::W') ≤ length (com' (x::X') Y'), note h2 := and.right hlongX'Y, apply h2, apply hW,
apply hWle},
{assert hlongXY' : longest_com (com' X' (y::Y')) X' (y::Y'),
apply hind (X', (y::Y')), apply le_of_eq, revert hlZ, unfold pair_length, unfold length, simp, rw add_comm, intro hlZ, apply add_right_cancel, apply hlZ,
assert hW : com_subseq (w::W') X' (y::Y'), apply and.intro,
note h1 := and.left h, revert h1, unfold subseq, intro h1, apply or.elim h1,
intro h2, apply absurd, apply (eq.symm (and.left h2)), apply hnxw,
intro h2, apply h2,
apply and.right h,
assert hl : length (com' X' (y::Y')) ≤ length (com' (x::X') Y'), apply le_of_not_le, apply hg,
note h2 := and.right hlongXY',
apply le_trans, apply h2, apply hW, apply hl
}}}}}}}},
apply funcToNat_strong_ind H0 Hn
end
end list |
18bb6366b8bc23e5b7c2750d2a1da2730be7ad49 | 690889011852559ee5ac4dfea77092de8c832e7e | /test/lint.lean | 37cee14063e2a2fb5117075cd93121bcf0e1eeb8 | [
"Apache-2.0"
] | permissive | williamdemeo/mathlib | f6df180148f8acc91de9ba5e558976ab40a872c7 | 1fa03c29f9f273203bbffb79d10d31f696b3d317 | refs/heads/master | 1,584,785,260,929 | 1,572,195,914,000 | 1,572,195,913,000 | 138,435,193 | 0 | 0 | Apache-2.0 | 1,529,789,739,000 | 1,529,789,739,000 | null | UTF-8 | Lean | false | false | 2,208 | lean | import tactic.lint
def foo1 (n m : ℕ) : ℕ := n + 1
def foo2 (n m : ℕ) : m = m := by refl
lemma foo3 (n m : ℕ) : ℕ := n - m
lemma foo.foo (n m : ℕ) : n ≥ n := le_refl n
instance bar.bar : has_add ℕ := by apply_instance -- we don't check the name of instances
-- section
-- local attribute [instance, priority 1001] classical.prop_decidable
-- lemma foo4 : (if 3 = 3 then 1 else 2) = 1 := if_pos (by refl)
-- end
open tactic
run_cmd do
let t := name × list ℕ,
e ← get_env,
l ← e.mfilter (λ d, return $
e.in_current_file' d.to_name && ¬ d.to_name.is_internal && ¬ d.is_auto_generated e),
l2 ← fold_over_with_cond l (return ∘ check_unused_arguments),
guard $ l2.length = 3,
let l2 : list t := l2.map $ λ x, ⟨x.1.to_name, x.2⟩,
guard $ (⟨`foo1, [2]⟩ : t) ∈ l2,
guard $ (⟨`foo2, [1]⟩ : t) ∈ l2,
guard $ (⟨`foo.foo, [2]⟩ : t) ∈ l2,
l2 ← fold_over_with_cond l incorrect_def_lemma,
guard $ l2.length = 2,
let l2 : list (name × _) := l2.map $ λ x, ⟨x.1.to_name, x.2⟩,
guard $ ∃(x ∈ l2), (x : name × _).1 = `foo2,
guard $ ∃(x ∈ l2), (x : name × _).1 = `foo3,
l3 ← fold_over_with_cond l dup_namespace,
guard $ l3.length = 1,
guard $ ∃(x ∈ l3), (x : declaration × _).1.to_name = `foo.foo,
l4 ← fold_over_with_cond l illegal_constants_in_statement,
guard $ l4.length = 1,
guard $ ∃(x ∈ l4), (x : declaration × _).1.to_name = `foo.foo,
-- guard $ ∃(x ∈ l4), (x : declaration × _).1.to_name = `foo4,
s ← lint ff,
guard $ "/- (slow tests skipped) -/\n".is_suffix_of s.to_string,
s2 ← lint tt,
guard $ s.to_string ≠ s2.to_string,
skip
/- check customizability and nolint -/
@[nolint] def bar.foo : (if 3 = 3 then 1 else 2) = 1 := if_pos (by refl)
meta def dummy_check (d : declaration) : tactic (option string) :=
return $ if d.to_name.last = "foo" then some "gotcha!" else none
meta def linter.dummy_linter : linter :=
{ test := dummy_check,
no_errors_found := "found nothing",
errors_found := "found something" }
run_cmd do
s ← lint tt [`linter.dummy_linter] tt,
guard $ "/- found something: -/\n#print foo.foo /- gotcha! -/\n\n".is_suffix_of s.to_string
|
495106de34c9066805a686ff8f85e47bb70a0782 | f3a5af2927397cf346ec0e24312bfff077f00425 | /src/mynat/add.lean | d5e7e595f29660abf27d6d3e589e09187585d8e4 | [
"Apache-2.0"
] | permissive | ImperialCollegeLondon/natural_number_game | 05c39e1586408cfb563d1a12e1085a90726ab655 | f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd | refs/heads/master | 1,688,570,964,990 | 1,636,908,242,000 | 1,636,908,242,000 | 195,403,790 | 277 | 84 | Apache-2.0 | 1,694,547,955,000 | 1,562,328,792,000 | Lean | UTF-8 | Lean | false | false | 469 | lean | import mynat.definition
/-
mynat/add.lean
-/
namespace mynat
-- definition of "addition on the natural numbers"
def add : mynat → mynat → mynat
| m 0 := m
| m (succ n) := succ (add m n)
instance : has_add mynat := ⟨mynat.add⟩
-- numerals now work
example : mynat := 37
lemma add_zero (m : mynat) : m + 0 = m := rfl
lemma add_succ (m n : mynat) : m + succ n = succ (m + n) := rfl
-- end of definition of "addition on the natural numbers"
end mynat
|
591d583f91b2369c300d77478c22f4d2dd49690e | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/data/nat/prime.lean | 803eadc223a25faef606d3fc7d0d4052ae3d0b26 | [
"Apache-2.0"
] | permissive | gebner/mathlib | eab0150cc4f79ec45d2016a8c21750244a2e7ff0 | cc6a6edc397c55118df62831e23bfbd6e6c6b4ab | refs/heads/master | 1,625,574,853,976 | 1,586,712,827,000 | 1,586,712,827,000 | 99,101,412 | 1 | 0 | Apache-2.0 | 1,586,716,389,000 | 1,501,667,958,000 | Lean | UTF-8 | Lean | false | false | 19,838 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad, Mario Carneiro
-/
import data.nat.sqrt data.nat.gcd data.list.defs data.list.perm
import algebra.group_power
import tactic.wlog
/-!
# Prime numbers
This file deals with prime numbers: natural numbers `p ≥ 2` whose only divisors are `p` and `1`.
## Important declarations
All the following declarations exist in the namespace `nat`.
- `prime`: the predicate that expresses that a natural number `p` is prime
- `primes`: the subtype of natural numbers that are prime
- `min_fac n`: the minimal prime factor of a natural number `n ≠ 1`
- `exists_infinite_primes`: Euclid's theorem that there exist infinitely many prime numbers
- `factors n`: the prime factorization of `n`
- `factors_unique`: uniqueness of the prime factorisation
-/
open bool subtype
namespace nat
open decidable
/-- `prime p` means that `p` is a prime number, that is, a natural number
at least 2 whose only divisors are `p` and `1`. -/
def prime (p : ℕ) := 2 ≤ p ∧ ∀ m ∣ p, m = 1 ∨ m = p
theorem prime.two_le {p : ℕ} : prime p → 2 ≤ p := and.left
theorem prime.one_lt {p : ℕ} : prime p → 1 < p := prime.two_le
lemma prime.ne_one {p : ℕ} (hp : p.prime) : p ≠ 1 :=
ne.symm $ (ne_of_lt hp.one_lt)
theorem prime_def_lt {p : ℕ} : prime p ↔ 2 ≤ p ∧ ∀ m < p, m ∣ p → m = 1 :=
and_congr_right $ λ p2, forall_congr $ λ m,
⟨λ h l d, (h d).resolve_right (ne_of_lt l),
λ h d, (decidable.lt_or_eq_of_le $
le_of_dvd (le_of_succ_le p2) d).imp_left (λ l, h l d)⟩
theorem prime_def_lt' {p : ℕ} : prime p ↔ 2 ≤ p ∧ ∀ m, 2 ≤ m → m < p → ¬ m ∣ p :=
prime_def_lt.trans $ and_congr_right $ λ p2, forall_congr $ λ m,
⟨λ h m2 l d, not_lt_of_ge m2 ((h l d).symm ▸ dec_trivial),
λ h l d, begin
rcases m with _|_|m,
{ rw eq_zero_of_zero_dvd d at p2, revert p2, exact dec_trivial },
{ refl },
{ exact (h dec_trivial l).elim d }
end⟩
theorem prime_def_le_sqrt {p : ℕ} : prime p ↔ 2 ≤ p ∧
∀ m, 2 ≤ m → m ≤ sqrt p → ¬ m ∣ p :=
prime_def_lt'.trans $ and_congr_right $ λ p2,
⟨λ a m m2 l, a m m2 $ lt_of_le_of_lt l $ sqrt_lt_self p2,
λ a, have ∀ {m k}, m ≤ k → 1 < m → p ≠ m * k, from
λ m k mk m1 e, a m m1
(le_sqrt.2 (e.symm ▸ mul_le_mul_left m mk)) ⟨k, e⟩,
λ m m2 l ⟨k, e⟩, begin
cases (le_total m k) with mk km,
{ exact this mk m2 e },
{ rw [mul_comm] at e,
refine this km (lt_of_mul_lt_mul_right _ (zero_le m)) e,
rwa [one_mul, ← e] }
end⟩
/--
This instance is slower than the instance `decidable_prime` defined below,
but has the advantage that it works in the kernel.
If you need to prove that a particular number is prime, in any case
you should not use `dec_trivial`, but rather `by norm_num`, which is
much faster.
-/
def decidable_prime_1 (p : ℕ) : decidable (prime p) :=
decidable_of_iff' _ prime_def_lt'
local attribute [instance] decidable_prime_1
lemma prime.ne_zero {n : ℕ} (h : prime n) : n ≠ 0 :=
assume hn : n = 0,
have h2 : ¬ prime 0, from dec_trivial,
h2 (hn ▸ h)
theorem prime.pos {p : ℕ} (pp : prime p) : 0 < p :=
lt_of_succ_lt pp.one_lt
theorem not_prime_zero : ¬ prime 0 := dec_trivial
theorem not_prime_one : ¬ prime 1 := dec_trivial
theorem prime_two : prime 2 := dec_trivial
theorem prime_three : prime 3 := dec_trivial
theorem prime.pred_pos {p : ℕ} (pp : prime p) : 0 < pred p :=
lt_pred_iff.2 pp.one_lt
theorem succ_pred_prime {p : ℕ} (pp : prime p) : succ (pred p) = p :=
succ_pred_eq_of_pos pp.pos
theorem dvd_prime {p m : ℕ} (pp : prime p) : m ∣ p ↔ m = 1 ∨ m = p :=
⟨λ d, pp.2 m d, λ h, h.elim (λ e, e.symm ▸ one_dvd _) (λ e, e.symm ▸ dvd_refl _)⟩
theorem dvd_prime_two_le {p m : ℕ} (pp : prime p) (H : 2 ≤ m) : m ∣ p ↔ m = p :=
(dvd_prime pp).trans $ or_iff_right_of_imp $ not.elim $ ne_of_gt H
theorem prime.not_dvd_one {p : ℕ} (pp : prime p) : ¬ p ∣ 1
| d := (not_le_of_gt pp.one_lt) $ le_of_dvd dec_trivial d
theorem not_prime_mul {a b : ℕ} (a1 : 1 < a) (b1 : 1 < b) : ¬ prime (a * b) :=
λ h, ne_of_lt (nat.mul_lt_mul_of_pos_left b1 (lt_of_succ_lt a1)) $
by simpa using (dvd_prime_two_le h a1).1 (dvd_mul_right _ _)
section min_fac
private lemma min_fac_lemma (n k : ℕ) (h : ¬ n < k * k) :
sqrt n - k < sqrt n + 2 - k :=
(nat.sub_lt_sub_right_iff $ le_sqrt.2 $ le_of_not_gt h).2 $
nat.lt_add_of_pos_right dec_trivial
def min_fac_aux (n : ℕ) : ℕ → ℕ | k :=
if h : n < k * k then n else
if k ∣ n then k else
have _, from min_fac_lemma n k h,
min_fac_aux (k + 2)
using_well_founded {rel_tac :=
λ _ _, `[exact ⟨_, measure_wf (λ k, sqrt n + 2 - k)⟩]}
/-- Returns the smallest prime factor of `n ≠ 1`. -/
def min_fac : ℕ → ℕ
| 0 := 2
| 1 := 1
| (n+2) := if 2 ∣ n then 2 else min_fac_aux (n + 2) 3
@[simp] theorem min_fac_zero : min_fac 0 = 2 := rfl
@[simp] theorem min_fac_one : min_fac 1 = 1 := rfl
theorem min_fac_eq : ∀ n, min_fac n = if 2 ∣ n then 2 else min_fac_aux n 3
| 0 := rfl
| 1 := by simp [show 2≠1, from dec_trivial]; rw min_fac_aux; refl
| (n+2) :=
have 2 ∣ n + 2 ↔ 2 ∣ n, from
(nat.dvd_add_iff_left (by refl)).symm,
by simp [min_fac, this]; congr
private def min_fac_prop (n k : ℕ) :=
2 ≤ k ∧ k ∣ n ∧ ∀ m, 2 ≤ m → m ∣ n → k ≤ m
theorem min_fac_aux_has_prop {n : ℕ} (n2 : 2 ≤ n) (nd2 : ¬ 2 ∣ n) :
∀ k i, k = 2*i+3 → (∀ m, 2 ≤ m → m ∣ n → k ≤ m) → min_fac_prop n (min_fac_aux n k)
| k := λ i e a, begin
rw min_fac_aux,
by_cases h : n < k*k; simp [h],
{ have pp : prime n :=
prime_def_le_sqrt.2 ⟨n2, λ m m2 l d,
not_lt_of_ge l $ lt_of_lt_of_le (sqrt_lt.2 h) (a m m2 d)⟩,
from ⟨n2, dvd_refl _, λ m m2 d, le_of_eq
((dvd_prime_two_le pp m2).1 d).symm⟩ },
have k2 : 2 ≤ k, { subst e, exact dec_trivial },
by_cases dk : k ∣ n; simp [dk],
{ exact ⟨k2, dk, a⟩ },
{ refine have _, from min_fac_lemma n k h,
min_fac_aux_has_prop (k+2) (i+1)
(by simp [e, left_distrib]) (λ m m2 d, _),
cases nat.eq_or_lt_of_le (a m m2 d) with me ml,
{ subst me, contradiction },
apply (nat.eq_or_lt_of_le ml).resolve_left, intro me,
rw [← me, e] at d, change 2 * (i + 2) ∣ n at d,
have := dvd_of_mul_right_dvd d, contradiction }
end
using_well_founded {rel_tac :=
λ _ _, `[exact ⟨_, measure_wf (λ k, sqrt n + 2 - k)⟩]}
theorem min_fac_has_prop {n : ℕ} (n1 : n ≠ 1) :
min_fac_prop n (min_fac n) :=
begin
by_cases n0 : n = 0, {simp [n0, min_fac_prop, ge]},
have n2 : 2 ≤ n, { revert n0 n1, rcases n with _|_|_; exact dec_trivial },
simp [min_fac_eq],
by_cases d2 : 2 ∣ n; simp [d2],
{ exact ⟨le_refl _, d2, λ k k2 d, k2⟩ },
{ refine min_fac_aux_has_prop n2 d2 3 0 rfl
(λ m m2 d, (nat.eq_or_lt_of_le m2).resolve_left (mt _ d2)),
exact λ e, e.symm ▸ d }
end
theorem min_fac_dvd (n : ℕ) : min_fac n ∣ n :=
by by_cases n1 : n = 1;
[exact n1.symm ▸ dec_trivial, exact (min_fac_has_prop n1).2.1]
theorem min_fac_prime {n : ℕ} (n1 : n ≠ 1) : prime (min_fac n) :=
let ⟨f2, fd, a⟩ := min_fac_has_prop n1 in
prime_def_lt'.2 ⟨f2, λ m m2 l d, not_le_of_gt l (a m m2 (dvd_trans d fd))⟩
theorem min_fac_le_of_dvd {n : ℕ} : ∀ {m : ℕ}, 2 ≤ m → m ∣ n → min_fac n ≤ m :=
by by_cases n1 : n = 1;
[exact λ m m2 d, n1.symm ▸ le_trans dec_trivial m2,
exact (min_fac_has_prop n1).2.2]
theorem min_fac_pos (n : ℕ) : 0 < min_fac n :=
by by_cases n1 : n = 1;
[exact n1.symm ▸ dec_trivial, exact (min_fac_prime n1).pos]
theorem min_fac_le {n : ℕ} (H : 0 < n) : min_fac n ≤ n :=
le_of_dvd H (min_fac_dvd n)
theorem prime_def_min_fac {p : ℕ} : prime p ↔ 2 ≤ p ∧ min_fac p = p :=
⟨λ pp, ⟨pp.two_le,
let ⟨f2, fd, a⟩ := min_fac_has_prop $ ne_of_gt pp.one_lt in
((dvd_prime pp).1 fd).resolve_left (ne_of_gt f2)⟩,
λ ⟨p2, e⟩, e ▸ min_fac_prime (ne_of_gt p2)⟩
/--
This instance is faster in the virtual machine than `decidable_prime_1`,
but slower in the kernel.
If you need to prove that a particular number is prime, in any case
you should not use `dec_trivial`, but rather `by norm_num`, which is
much faster.
-/
instance decidable_prime (p : ℕ) : decidable (prime p) :=
decidable_of_iff' _ prime_def_min_fac
theorem not_prime_iff_min_fac_lt {n : ℕ} (n2 : 2 ≤ n) : ¬ prime n ↔ min_fac n < n :=
(not_congr $ prime_def_min_fac.trans $ and_iff_right n2).trans $
(lt_iff_le_and_ne.trans $ and_iff_right $ min_fac_le $ le_of_succ_le n2).symm
lemma min_fac_le_div {n : ℕ} (pos : 0 < n) (np : ¬ prime n) : min_fac n ≤ n / min_fac n :=
match min_fac_dvd n with
| ⟨0, h0⟩ := absurd pos $ by rw [h0, mul_zero]; exact dec_trivial
| ⟨1, h1⟩ :=
begin
rw mul_one at h1,
rw [prime_def_min_fac, not_and_distrib, ← h1, eq_self_iff_true, not_true, or_false, not_le] at np,
rw [le_antisymm (le_of_lt_succ np) (succ_le_of_lt pos), min_fac_one, nat.div_one]
end
| ⟨(x+2), hx⟩ :=
begin
conv_rhs { congr, rw hx },
rw [nat.mul_div_cancel_left _ (min_fac_pos _)],
exact min_fac_le_of_dvd dec_trivial ⟨min_fac n, by rwa mul_comm⟩
end
end
end min_fac
theorem exists_dvd_of_not_prime {n : ℕ} (n2 : 2 ≤ n) (np : ¬ prime n) :
∃ m, m ∣ n ∧ m ≠ 1 ∧ m ≠ n :=
⟨min_fac n, min_fac_dvd _, ne_of_gt (min_fac_prime (ne_of_gt n2)).one_lt,
ne_of_lt $ (not_prime_iff_min_fac_lt n2).1 np⟩
theorem exists_dvd_of_not_prime2 {n : ℕ} (n2 : 2 ≤ n) (np : ¬ prime n) :
∃ m, m ∣ n ∧ 2 ≤ m ∧ m < n :=
⟨min_fac n, min_fac_dvd _, (min_fac_prime (ne_of_gt n2)).two_le,
(not_prime_iff_min_fac_lt n2).1 np⟩
theorem exists_prime_and_dvd {n : ℕ} (n2 : 2 ≤ n) : ∃ p, prime p ∧ p ∣ n :=
⟨min_fac n, min_fac_prime (ne_of_gt n2), min_fac_dvd _⟩
/-- Euclid's theorem. There exist infinitely many prime numbers.
Here given in the form: for every `n`, there exists a prime number `p ≥ n`. -/
theorem exists_infinite_primes (n : ℕ) : ∃ p, n ≤ p ∧ prime p :=
let p := min_fac (fact n + 1) in
have f1 : fact n + 1 ≠ 1, from ne_of_gt $ succ_lt_succ $ fact_pos _,
have pp : prime p, from min_fac_prime f1,
have np : n ≤ p, from le_of_not_ge $ λ h,
have h₁ : p ∣ fact n, from dvd_fact (min_fac_pos _) h,
have h₂ : p ∣ 1, from (nat.dvd_add_iff_right h₁).2 (min_fac_dvd _),
pp.not_dvd_one h₂,
⟨p, np, pp⟩
lemma prime.eq_two_or_odd {p : ℕ} (hp : prime p) : p = 2 ∨ p % 2 = 1 :=
(nat.mod_two_eq_zero_or_one p).elim
(λ h, or.inl ((hp.2 2 (dvd_of_mod_eq_zero h)).resolve_left dec_trivial).symm)
or.inr
theorem factors_lemma {k} : (k+2) / min_fac (k+2) < k+2 :=
div_lt_self dec_trivial (min_fac_prime dec_trivial).one_lt
/-- `factors n` is the prime factorization of `n`, listed in increasing order. -/
def factors : ℕ → list ℕ
| 0 := []
| 1 := []
| n@(k+2) :=
let m := min_fac n in have n / m < n := factors_lemma,
m :: factors (n / m)
lemma mem_factors : ∀ {n p}, p ∈ factors n → prime p
| 0 := λ p, false.elim
| 1 := λ p, false.elim
| n@(k+2) := λ p h,
let m := min_fac n in have n / m < n := factors_lemma,
have h₁ : p = m ∨ p ∈ (factors (n / m)) :=
(list.mem_cons_iff _ _ _).1 h,
or.cases_on h₁ (λ h₂, h₂.symm ▸ min_fac_prime dec_trivial)
mem_factors
lemma prod_factors : ∀ {n}, 0 < n → list.prod (factors n) = n
| 0 := (lt_irrefl _).elim
| 1 := λ h, rfl
| n@(k+2) := λ h,
let m := min_fac n in have n / m < n := factors_lemma,
show list.prod (m :: factors (n / m)) = n, from
have h₁ : 0 < n / m :=
nat.pos_of_ne_zero $ λ h,
have n = 0 * m := (nat.div_eq_iff_eq_mul_left (min_fac_pos _) (min_fac_dvd _)).1 h,
by rw zero_mul at this; exact (show k + 2 ≠ 0, from dec_trivial) this,
by rw [list.prod_cons, prod_factors h₁, nat.mul_div_cancel' (min_fac_dvd _)]
theorem prime.coprime_iff_not_dvd {p n : ℕ} (pp : prime p) : coprime p n ↔ ¬ p ∣ n :=
⟨λ co d, pp.not_dvd_one $ co.dvd_of_dvd_mul_left (by simp [d]),
λ nd, coprime_of_dvd $ λ m m2 mp, ((dvd_prime_two_le pp m2).1 mp).symm ▸ nd⟩
theorem prime.dvd_iff_not_coprime {p n : ℕ} (pp : prime p) : p ∣ n ↔ ¬ coprime p n :=
iff_not_comm.2 pp.coprime_iff_not_dvd
theorem prime.dvd_mul {p m n : ℕ} (pp : prime p) : p ∣ m * n ↔ p ∣ m ∨ p ∣ n :=
⟨λ H, or_iff_not_imp_left.2 $ λ h,
(pp.coprime_iff_not_dvd.2 h).dvd_of_dvd_mul_left H,
or.rec (λ h, dvd_mul_of_dvd_left h _) (λ h, dvd_mul_of_dvd_right h _)⟩
theorem prime.not_dvd_mul {p m n : ℕ} (pp : prime p)
(Hm : ¬ p ∣ m) (Hn : ¬ p ∣ n) : ¬ p ∣ m * n :=
mt pp.dvd_mul.1 $ by simp [Hm, Hn]
theorem prime.dvd_of_dvd_pow {p m n : ℕ} (pp : prime p) (h : p ∣ m^n) : p ∣ m :=
by induction n with n IH;
[exact pp.not_dvd_one.elim h,
exact (pp.dvd_mul.1 h).elim IH id]
lemma prime.mul_eq_prime_pow_two_iff {x y p : ℕ} (hp : p.prime) (hx : x ≠ 1) (hy : y ≠ 1) :
x * y = p ^ 2 ↔ x = p ∧ y = p :=
⟨λ h, have pdvdxy : p ∣ x * y, by rw h; simp [nat.pow_two],
begin
wlog := hp.dvd_mul.1 pdvdxy using x y,
cases case with a ha,
have hap : a ∣ p, from ⟨y, by rwa [ha, nat.pow_two,
mul_assoc, nat.mul_left_inj hp.pos, eq_comm] at h⟩,
exact ((nat.dvd_prime hp).1 hap).elim
(λ _, by clear_aux_decl; simp [*, nat.pow_two, nat.mul_left_inj hp.pos] at *
{contextual := tt})
(λ _, by clear_aux_decl; simp [*, nat.pow_two, mul_comm, mul_assoc,
nat.mul_left_inj hp.pos, nat.mul_right_eq_self_iff hp.pos] at *
{contextual := tt})
end,
λ ⟨h₁, h₂⟩, h₁.symm ▸ h₂.symm ▸ (nat.pow_two _).symm⟩
lemma prime.dvd_fact : ∀ {n p : ℕ} (hp : prime p), p ∣ n.fact ↔ p ≤ n
| 0 p hp := iff_of_false hp.not_dvd_one (not_le_of_lt hp.pos)
| (n+1) p hp := begin
rw [fact_succ, hp.dvd_mul, prime.dvd_fact hp],
exact ⟨λ h, h.elim (le_of_dvd (succ_pos _)) le_succ_of_le,
λ h, (_root_.lt_or_eq_of_le h).elim (or.inr ∘ le_of_lt_succ)
(λ h, or.inl $ by rw h)⟩
end
theorem prime.coprime_pow_of_not_dvd {p m a : ℕ} (pp : prime p) (h : ¬ p ∣ a) : coprime a (p^m) :=
(pp.coprime_iff_not_dvd.2 h).symm.pow_right _
theorem coprime_primes {p q : ℕ} (pp : prime p) (pq : prime q) : coprime p q ↔ p ≠ q :=
pp.coprime_iff_not_dvd.trans $ not_congr $ dvd_prime_two_le pq pp.two_le
theorem coprime_pow_primes {p q : ℕ} (n m : ℕ) (pp : prime p) (pq : prime q) (h : p ≠ q) :
coprime (p^n) (q^m) :=
((coprime_primes pp pq).2 h).pow _ _
theorem coprime_or_dvd_of_prime {p} (pp : prime p) (i : ℕ) : coprime p i ∨ p ∣ i :=
by rw [pp.dvd_iff_not_coprime]; apply em
theorem dvd_prime_pow {p : ℕ} (pp : prime p) {m i : ℕ} : i ∣ (p^m) ↔ ∃ k ≤ m, i = p^k :=
begin
induction m with m IH generalizing i, {simp [pow_succ, le_zero_iff] at *},
by_cases p ∣ i,
{ cases h with a e, subst e,
rw [pow_succ, mul_comm (p^m) p, nat.mul_dvd_mul_iff_left pp.pos, IH],
split; intro h; rcases h with ⟨k, h, e⟩,
{ exact ⟨succ k, succ_le_succ h, by rw [mul_comm, e]; refl⟩ },
cases k with k,
{ apply pp.not_dvd_one.elim,
simp at e, rw ← e, apply dvd_mul_right },
{ refine ⟨k, le_of_succ_le_succ h, _⟩,
rwa [mul_comm, pow_succ, nat.mul_right_inj pp.pos] at e } },
{ split; intro d,
{ rw (pp.coprime_pow_of_not_dvd h).eq_one_of_dvd d,
exact ⟨0, zero_le _, rfl⟩ },
{ rcases d with ⟨k, l, e⟩,
rw e, exact pow_dvd_pow _ l } }
end
section
open list
lemma mem_list_primes_of_dvd_prod {p : ℕ} (hp : prime p) :
∀ {l : list ℕ}, (∀ p ∈ l, prime p) → p ∣ prod l → p ∈ l
| [] := λ h₁ h₂, absurd h₂ (prime.not_dvd_one hp)
| (q :: l) := λ h₁ h₂,
have h₃ : p ∣ q * prod l := @prod_cons _ _ l q ▸ h₂,
have hq : prime q := h₁ q (mem_cons_self _ _),
or.cases_on ((prime.dvd_mul hp).1 h₃)
(λ h, by rw [prime.dvd_iff_not_coprime hp, coprime_primes hp hq, ne.def, not_not] at h;
exact h ▸ mem_cons_self _ _)
(λ h, have hl : ∀ p ∈ l, prime p := λ p hlp, h₁ p ((mem_cons_iff _ _ _).2 (or.inr hlp)),
(mem_cons_iff _ _ _).2 (or.inr (mem_list_primes_of_dvd_prod hl h)))
lemma mem_factors_iff_dvd {n p : ℕ} (hn : 0 < n) (hp : prime p) : p ∈ factors n ↔ p ∣ n :=
⟨λ h, prod_factors hn ▸ list.dvd_prod h,
λ h, mem_list_primes_of_dvd_prod hp (@mem_factors n) ((prod_factors hn).symm ▸ h)⟩
lemma perm_of_prod_eq_prod : ∀ {l₁ l₂ : list ℕ}, prod l₁ = prod l₂ →
(∀ p ∈ l₁, prime p) → (∀ p ∈ l₂, prime p) → l₁ ~ l₂
| [] [] _ _ _ := perm.nil
| [] (a :: l) h₁ h₂ h₃ :=
have ha : a ∣ 1 := @prod_nil ℕ _ ▸ h₁.symm ▸ (@prod_cons _ _ l a).symm ▸ dvd_mul_right _ _,
absurd ha (prime.not_dvd_one (h₃ a (mem_cons_self _ _)))
| (a :: l) [] h₁ h₂ h₃ :=
have ha : a ∣ 1 := @prod_nil ℕ _ ▸ h₁ ▸ (@prod_cons _ _ l a).symm ▸ dvd_mul_right _ _,
absurd ha (prime.not_dvd_one (h₂ a (mem_cons_self _ _)))
| (a :: l₁) (b :: l₂) h hl₁ hl₂ :=
have hl₁' : ∀ p ∈ l₁, prime p := λ p hp, hl₁ p (mem_cons_of_mem _ hp),
have hl₂' : ∀ p ∈ (b :: l₂).erase a, prime p := λ p hp, hl₂ p (mem_of_mem_erase hp),
have ha : a ∈ (b :: l₂) := mem_list_primes_of_dvd_prod (hl₁ a (mem_cons_self _ _)) hl₂
(h ▸ by rw prod_cons; exact dvd_mul_right _ _),
have hb : b :: l₂ ~ a :: (b :: l₂).erase a := perm_erase ha,
have hl : prod l₁ = prod ((b :: l₂).erase a) :=
(nat.mul_left_inj (prime.pos (hl₁ a (mem_cons_self _ _)))).1 $
by rwa [← prod_cons, ← prod_cons, ← prod_eq_of_perm hb],
perm.trans (perm.skip _ (perm_of_prod_eq_prod hl hl₁' hl₂')) hb.symm
lemma factors_unique {n : ℕ} {l : list ℕ} (h₁ : prod l = n) (h₂ : ∀ p ∈ l, prime p) : l ~ factors n :=
have hn : 0 < n := nat.pos_of_ne_zero $ λ h, begin
rw h at *, clear h,
induction l with a l hi,
{ exact absurd h₁ dec_trivial },
{ rw prod_cons at h₁,
exact nat.mul_ne_zero (ne_of_lt (prime.pos (h₂ a (mem_cons_self _ _)))).symm
(hi (λ p hp, h₂ p (mem_cons_of_mem _ hp))) h₁ }
end,
perm_of_prod_eq_prod (by rwa prod_factors hn) h₂ (@mem_factors _)
end
lemma succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul {p : ℕ} (p_prime : prime p) {m n k l : ℕ}
(hpm : p ^ k ∣ m) (hpn : p ^ l ∣ n) (hpmn : p ^ (k+l+1) ∣ m*n) :
p ^ (k+1) ∣ m ∨ p ^ (l+1) ∣ n :=
have hpd : p^(k+l) * p ∣ m*n, from hpmn,
have hpd2 : p ∣ (m*n) / p ^ (k+l), from dvd_div_of_mul_dvd hpd,
have hpd3 : p ∣ (m*n) / (p^k * p^l), by simpa [nat.pow_add] using hpd2,
have hpd4 : p ∣ (m / p^k) * (n / p^l), by simpa [nat.div_mul_div hpm hpn] using hpd3,
have hpd5 : p ∣ (m / p^k) ∨ p ∣ (n / p^l), from (prime.dvd_mul p_prime).1 hpd4,
show p^k*p ∣ m ∨ p^l*p ∣ n, from
hpd5.elim
(assume : p ∣ m / p ^ k, or.inl $ mul_dvd_of_dvd_div hpm this)
(assume : p ∣ n / p ^ l, or.inr $ mul_dvd_of_dvd_div hpn this)
/-- The type of prime numbers -/
def primes := {p : ℕ // p.prime}
namespace primes
instance : has_repr nat.primes := ⟨λ p, repr p.val⟩
instance : inhabited primes := ⟨⟨2, prime_two⟩⟩
instance coe_nat : has_coe nat.primes ℕ := ⟨subtype.val⟩
theorem coe_nat_inj (p q : nat.primes) : (p : ℕ) = (q : ℕ) → p = q :=
λ h, subtype.eq h
end primes
instance monoid.prime_pow {α : Type*} [monoid α] : has_pow α primes := ⟨λ x p, x^p.val⟩
end nat
|
1b7c0e1c8eb9bad4fd9731c9c8cac1b50ab7591b | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /stage0/src/Lean/Meta/AbstractMVars.lean | 48d050b2b7fd4c4910aa01ebb10323ae019fa98a | [
"Apache-2.0"
] | permissive | WojciechKarpiel/lean4 | 7f89706b8e3c1f942b83a2c91a3a00b05da0e65b | f6e1314fa08293dea66a329e05b6c196a0189163 | refs/heads/master | 1,686,633,402,214 | 1,625,821,189,000 | 1,625,821,258,000 | 384,640,886 | 0 | 0 | Apache-2.0 | 1,625,903,617,000 | 1,625,903,026,000 | null | UTF-8 | Lean | false | false | 5,533 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Basic
namespace Lean.Meta
structure AbstractMVarsResult where
paramNames : Array Name
numMVars : Nat
expr : Expr
deriving Inhabited, BEq
namespace AbstractMVars
open Std (HashMap)
structure State where
ngen : NameGenerator
lctx : LocalContext
mctx : MetavarContext
nextParamIdx : Nat := 0
paramNames : Array Name := #[]
fvars : Array Expr := #[]
lmap : HashMap Name Level := {}
emap : HashMap Name Expr := {}
abbrev M := StateM State
def mkFreshId : M Name := do
let s ← get
let fresh := s.ngen.curr
modify fun s => { s with ngen := s.ngen.next }
pure fresh
private partial def abstractLevelMVars (u : Level) : M Level := do
if !u.hasMVar then
return u
else
match u with
| Level.zero _ => return u
| Level.param _ _ => return u
| Level.succ v _ => return u.updateSucc! (← abstractLevelMVars v)
| Level.max v w _ => return u.updateMax! (← abstractLevelMVars v) (← abstractLevelMVars w)
| Level.imax v w _ => return u.updateIMax! (← abstractLevelMVars v) (← abstractLevelMVars w)
| Level.mvar mvarId _ =>
let s ← get
let depth := s.mctx.getLevelDepth mvarId;
if depth != s.mctx.depth then
return u -- metavariables from lower depths are treated as constants
else
match s.lmap.find? mvarId with
| some u => pure u
| none =>
let paramId := Name.mkNum `_abstMVar s.nextParamIdx
let u := mkLevelParam paramId
modify fun s => { s with nextParamIdx := s.nextParamIdx + 1, lmap := s.lmap.insert mvarId u, paramNames := s.paramNames.push paramId }
return u
partial def abstractExprMVars (e : Expr) : M Expr := do
if !e.hasMVar then
return e
else
match e with
| e@(Expr.lit _ _) => return e
| e@(Expr.bvar _ _) => return e
| e@(Expr.fvar _ _) => return e
| e@(Expr.sort u _) => return e.updateSort! (← abstractLevelMVars u)
| e@(Expr.const _ us _) => return e.updateConst! (← us.mapM abstractLevelMVars)
| e@(Expr.proj _ _ s _) => return e.updateProj! (← abstractExprMVars s)
| e@(Expr.app f a _) => return e.updateApp! (← abstractExprMVars f) (← abstractExprMVars a)
| e@(Expr.mdata _ b _) => return e.updateMData! (← abstractExprMVars b)
| e@(Expr.lam _ d b _) => return e.updateLambdaE! (← abstractExprMVars d) (← abstractExprMVars b)
| e@(Expr.forallE _ d b _) => return e.updateForallE! (← abstractExprMVars d) (← abstractExprMVars b)
| e@(Expr.letE _ t v b _) => return e.updateLet! (← abstractExprMVars t) (← abstractExprMVars v) (← abstractExprMVars b)
| e@(Expr.mvar mvarId _) =>
let s ← get
let decl := s.mctx.getDecl mvarId
if decl.depth != s.mctx.depth then
return e
else
let (eNew, mctxNew) ← s.mctx.instantiateMVars e
if e != eNew then
modify fun s => { s with mctx := mctxNew }
abstractExprMVars eNew
else
match s.emap.find? mvarId with
| some e =>
return e
| none =>
let type ← abstractExprMVars decl.type
let fvarId ← mkFreshId
let fvar := mkFVar fvarId;
let userName := if decl.userName.isAnonymous then (`x).appendIndexAfter s.fvars.size else decl.userName
modify fun s => {
s with
emap := s.emap.insert mvarId fvar,
fvars := s.fvars.push fvar,
lctx := s.lctx.mkLocalDecl fvarId userName type }
return fvar
end AbstractMVars
/--
Abstract (current depth) metavariables occurring in `e`.
The result contains
- An array of universe level parameters that replaced universe metavariables occurring in `e`.
- The number of (expr) metavariables abstracted.
- And an expression of the form `fun (m_1 : A_1) ... (m_k : A_k) => e'`, where
`k` equal to the number of (expr) metavariables abstracted, and `e'` is `e` after we
replace the metavariables.
Example: given `f.{?u} ?m1` where `?m1 : ?m2 Nat`, `?m2 : Type -> Type`. This function returns
`{ levels := #[u], size := 2, expr := (fun (m2 : Type -> Type) (m1 : m2 Nat) => f.{u} m1) }`
This API can be used to "transport" to a different metavariable context.
Given a new metavariable context, we replace the `AbstractMVarsResult.levels` with
new fresh universe metavariables, and instantiate the `(m_i : A_i)` in the lambda-expression
with new fresh metavariables.
Application: we use this method to cache the results of type class resolution. -/
def abstractMVars (e : Expr) : MetaM AbstractMVarsResult := do
let e ← instantiateMVars e
let (e, s) := AbstractMVars.abstractExprMVars e { mctx := (← getMCtx), lctx := (← getLCtx), ngen := (← getNGen) }
setNGen s.ngen
setMCtx s.mctx
let e := s.lctx.mkLambda s.fvars e
pure { paramNames := s.paramNames, numMVars := s.fvars.size, expr := e }
def openAbstractMVarsResult (a : AbstractMVarsResult) : MetaM (Array Expr × Array BinderInfo × Expr) := do
let us ← a.paramNames.mapM fun _ => mkFreshLevelMVar
let e := a.expr.instantiateLevelParamsArray a.paramNames us
lambdaMetaTelescope e (some a.numMVars)
end Lean.Meta
|
82d96aea2042503b64f17ee4bb5b3bd8469c0673 | 471bedbd023d35c9d078c2f936dd577ace7f5813 | /library/init/meta/has_reflect.lean | 38a92feb84982c4bd50c4c678118ce3e3fe08a76 | [
"Apache-2.0"
] | permissive | lambdaxymox/lean | e06f0fa503666df827edd9867d7f49ca017aae64 | fc13c8c72a15dab71a2c2b31410c2cadc3526bd7 | refs/heads/master | 1,666,785,407,985 | 1,666,153,673,000 | 1,666,153,673,000 | 310,165,986 | 0 | 0 | Apache-2.0 | 1,604,542,096,000 | 1,604,542,095,000 | null | UTF-8 | Lean | false | false | 1,796 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
-/
prelude
import init.meta.expr init.util
universes u v
/-- `has_reflect α` lets you produce an `expr` from an instance of α. That is, it is a function from α to expr such that the expr has type α. -/
@[reducible] meta def has_reflect (α : Sort u) := Π a : α, reflected _ a
meta structure reflected_value (α : Type u) :=
(val : α)
[reflect : reflected _ val]
namespace reflected_value
meta def expr {α : Type u} (v : reflected_value α) : expr := v.reflect
meta def subst {α : Type u} {β : Type v} (f : α → β) [rf : reflected _ f]
(a : reflected_value α) : reflected_value β :=
@mk _ (f a.val) (rf.subst a.reflect)
end reflected_value
section
local attribute [semireducible] reflected
meta instance nat.reflect : has_reflect ℕ
| n := if n = 0 then `(0 : ℕ)
else if n = 1 then `(1 : ℕ)
else if n % 2 = 0 then `(bit0 %%(nat.reflect (n / 2)) : ℕ)
else `(bit1 %%(nat.reflect (n / 2)) : ℕ)
meta instance unsigned.reflect : has_reflect unsigned
| ⟨n, pr⟩ := `(unsigned.of_nat' n)
end
/- Instances that [derive] depends on. All other basic instances are defined at the end of
derive.lean. -/
meta instance name.reflect : has_reflect name
| name.anonymous := `(name.anonymous)
| (name.mk_string s n) := `(λ n, name.mk_string s n).subst (name.reflect n)
| (name.mk_numeral i n) := `(λ n, name.mk_numeral i n).subst (name.reflect n)
meta instance list.reflect {α : Type} [has_reflect α] [reflected _ α] : has_reflect (list α)
| [] := `([])
| (h::t) := `(λ t, h :: t).subst (list.reflect t)
meta instance punit.reflect : has_reflect punit
| () := `(_)
|
af62722bfbd7c375a08903caca04371ef0b2a904 | 9c1ad797ec8a5eddb37d34806c543602d9a6bf70 | /examples/naturals/naturals.lean | 081c1fe30b5e958619ba5fc51a8cc604901e31b3 | [] | 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,631 | 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 ...natural_transformation
namespace tqft.categories.examples.naturals
open tqft.categories
open tqft.categories.functor
@[simp]
lemma nat_add_left_cancel_iff (a b : ℕ) : a + b = a ↔ b = 0 :=
@add_left_cancel_iff _ _ a b 0
@[simp]
lemma nat_add_right_cancel_iff (a b : ℕ) : a + b = b ↔ a = 0 :=
begin
note h := @add_right_cancel_iff _ _ b a 0,
simp at h,
exact h
end
-- @[ematch]
-- lemma nat_add_associativity (a b c : ℕ) : nat.add (nat.add a b) c = nat.add a (nat.add b c) :=
-- begin
-- exact nat.add_assoc a b c
-- end
-- @[ematch]
-- lemma nat_add_commutativity (a b : ℕ) : nat.add a b = nat.add a b := ♮
-- FIXME This reducible is gross
-- @[reducible] definition ℕCategory : Category :=
-- begin
-- refine {
-- Obj := unit,
-- Hom := λ _ _, ℕ,
-- identity := _, -- Notice we don't specify the identity here: `refine` works it out for us.
-- compose := λ _ _ _ n m, n + m,
-- left_identity := _,
-- right_identity := _,
-- associativity := _
-- },
-- all_goals { blast }
-- end
-- -- @[simp] lemma ℕCategory.hom { X Y : ℕCategory.Obj } : ℕCategory.Hom X Y = ℕ := ♮
-- definition DoublingAsFunctor : Functor ℕCategory ℕCategory :=
-- { onObjects := id,
-- onMorphisms := λ _ _ n, n + n, -- TODO this is ugly.
-- identities := ♯,
-- functoriality := ♯
-- }
end tqft.categories.examples.naturals
|
9afaa86471019ce494d7d4c3b5440ae8b6dc7463 | b00eb947a9c4141624aa8919e94ce6dcd249ed70 | /src/Lean/Elab/SyntheticMVars.lean | 4c71234d2634cec65d3bbbc41057849fa01558b0 | [
"Apache-2.0"
] | permissive | gebner/lean4-old | a4129a041af2d4d12afb3a8d4deedabde727719b | ee51cdfaf63ee313c914d83264f91f414a0e3b6e | refs/heads/master | 1,683,628,606,745 | 1,622,651,300,000 | 1,622,654,405,000 | 142,608,821 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 17,201 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Sebastian Ullrich
-/
import Lean.Util.ForEachExpr
import Lean.Elab.Term
import Lean.Elab.Tactic.Basic
namespace Lean.Elab.Term
open Tactic (TacticM evalTactic getUnsolvedGoals withTacticInfoContext)
open Meta
/-- Auxiliary function used to implement `synthesizeSyntheticMVars`. -/
private def resumeElabTerm (stx : Syntax) (expectedType? : Option Expr) (errToSorry := true) : TermElabM Expr :=
-- Remark: if `ctx.errToSorry` is already false, then we don't enable it. Recall tactics disable `errToSorry`
withReader (fun ctx => { ctx with errToSorry := ctx.errToSorry && errToSorry }) do
elabTerm stx expectedType? false
/--
Try to elaborate `stx` that was postponed by an elaboration method using `Expection.postpone`.
It returns `true` if it succeeded, and `false` otherwise.
It is used to implement `synthesizeSyntheticMVars`. -/
private def resumePostponed (savedContext : SavedContext) (stx : Syntax) (mvarId : MVarId) (postponeOnError : Bool) : TermElabM Bool :=
withRef stx <| withMVarContext mvarId do
let s ← get
try
withSavedContext savedContext do
let mvarDecl ← getMVarDecl mvarId
let expectedType ← instantiateMVars mvarDecl.type
withInfoHole mvarId do
let result ← resumeElabTerm stx expectedType (!postponeOnError)
/- We must ensure `result` has the expected type because it is the one expected by the method that postponed stx.
That is, the method does not have an opportunity to check whether `result` has the expected type or not. -/
let result ← withRef stx <| ensureHasType expectedType result
/- We must perform `occursCheck` here since `result` may contain `mvarId` when it has synthetic `sorry`s. -/
if (← occursCheck mvarId result) then
assignExprMVar mvarId result
return true
else
return false
catch
| ex@(Exception.internal id _) =>
if id == postponeExceptionId then
set s
return false
else
throw ex
| ex@(Exception.error _ _) =>
if postponeOnError then
set s
return false
else
logException ex
return true
/--
Similar to `synthesizeInstMVarCore`, but makes sure that `instMVar` local context and instances
are used. It also logs any error message produced. -/
private def synthesizePendingInstMVar (instMVar : MVarId) : TermElabM Bool :=
withMVarContext instMVar do
try
synthesizeInstMVarCore instMVar
catch
| ex@(Exception.error _ _) => logException ex; return true
| _ => unreachable!
/--
Similar to `synthesizePendingInstMVar`, but generates type mismatch error message.
Remark: `eNew` is of the form `@coe ... mvar`, where `mvar` is the metavariable for the `CoeT ...` instance.
If `mvar` can be synthesized, then assign `auxMVarId := (expandCoe eNew)`.
-/
private def synthesizePendingCoeInstMVar
(auxMVarId : MVarId) (errorMsgHeader? : Option String) (eNew : Expr) (expectedType : Expr) (eType : Expr) (e : Expr) (f? : Option Expr) : TermElabM Bool := do
let instMVarId := eNew.appArg!.mvarId!
withMVarContext instMVarId do
if (← isDefEq expectedType eType) then
/- This case may seem counterintuitive since we created the coercion
because the `isDefEq expectedType eType` test failed before.
However, it may succeed here because we have more information, for example, metavariables
occurring at `expectedType` and `eType` may have been assigned. -/
if (← occursCheck auxMVarId e) then
assignExprMVar auxMVarId e
return true
else
return false
try
if (← synthesizeCoeInstMVarCore instMVarId) then
let eNew ← expandCoe eNew
if (← occursCheck auxMVarId eNew) then
assignExprMVar auxMVarId eNew
return true
return false
catch
| Exception.error _ msg => throwTypeMismatchError errorMsgHeader? expectedType eType e f? msg
| _ => unreachable!
/--
Try to synthesize a value for `mvarId` using the given default instance.
Return `some (val, mvarDecls)` if successful, where `val` is the value assigned to `mvarId`, and `mvarDecls` is a list of new type class instances that need to be synthesized.
-/
private def tryToSynthesizeUsingDefaultInstance (mvarId : MVarId) (defaultInstance : Name) : TermElabM (Option (Expr × List SyntheticMVarDecl)) :=
commitWhenSome? do
let candidate ← mkConstWithFreshMVarLevels defaultInstance
let (mvars, bis, _) ← forallMetaTelescopeReducing (← inferType candidate)
let candidate := mkAppN candidate mvars
trace[Elab.resume] "trying default instance for {mkMVar mvarId} := {candidate}"
if (← isDefEqGuarded (mkMVar mvarId) candidate) then
-- Succeeded. Collect new TC problems
let mut result := []
for i in [:bis.size] do
if bis[i] == BinderInfo.instImplicit then
result := { mvarId := mvars[i].mvarId!, stx := (← getRef), kind := SyntheticMVarKind.typeClass } :: result
trace[Elab.resume] "worked"
return some (candidate, result)
else
return none
private def tryToSynthesizeUsingDefaultInstances (mvarId : MVarId) (prio : Nat) : TermElabM (Option (Expr × List SyntheticMVarDecl)) :=
withMVarContext mvarId do
let mvarType := (← Meta.getMVarDecl mvarId).type
match (← isClass? mvarType) with
| none => return none
| some className =>
match (← getDefaultInstances className) with
| [] => return none
| defaultInstances =>
for (defaultInstance, instPrio) in defaultInstances do
if instPrio == prio then
match (← tryToSynthesizeUsingDefaultInstance mvarId defaultInstance) with
| some result => return some result
| none => continue
return none
/- Used to implement `synthesizeUsingDefault`. This method only consider default instances with the given priority. -/
private def synthesizeUsingDefaultPrio (prio : Nat) : TermElabM Bool := do
let rec visit (syntheticMVars : List SyntheticMVarDecl) (syntheticMVarsNew : List SyntheticMVarDecl) : TermElabM Bool := do
match syntheticMVars with
| [] => return false
| mvarDecl :: mvarDecls =>
match mvarDecl.kind with
| SyntheticMVarKind.typeClass =>
match (← withRef mvarDecl.stx <| tryToSynthesizeUsingDefaultInstances mvarDecl.mvarId prio) with
| none => visit mvarDecls (mvarDecl :: syntheticMVarsNew)
| some (val, newMVarDecls) =>
for newMVarDecl in newMVarDecls do
-- Register that `newMVarDecl.mvarId`s are implicit arguments of the value assigned to `mvarDecl.mvarId`
registerMVarErrorImplicitArgInfo newMVarDecl.mvarId (← getRef) val
let syntheticMVarsNew := newMVarDecls ++ syntheticMVarsNew
let syntheticMVarsNew := mvarDecls.reverse ++ syntheticMVarsNew
modify fun s => { s with syntheticMVars := syntheticMVarsNew }
return true
| _ => visit mvarDecls (mvarDecl :: syntheticMVarsNew)
/- Recall that s.syntheticMVars is essentially a stack. The first metavariable was the last one created.
We want to apply the default instance in reverse creation order. Otherwise,
`toString 0` will produce a `OfNat String _` cannot be synthesized error. -/
visit (← get).syntheticMVars.reverse []
/--
Apply default value to any pending synthetic metavariable of kind `SyntheticMVarKind.withDefault`
Return true if something was synthesized. -/
private def synthesizeUsingDefault : TermElabM Bool := do
let prioSet ← getDefaultInstancesPriorities
/- Recall that `prioSet` is stored in descending order -/
for prio in prioSet do
if (← synthesizeUsingDefaultPrio prio) then
return true
return false
/-- Report an error for each synthetic metavariable that could not be resolved. -/
private def reportStuckSyntheticMVars : TermElabM Unit := do
let syntheticMVars ← modifyGet fun s => (s.syntheticMVars, { s with syntheticMVars := [] })
for mvarSyntheticDecl in syntheticMVars do
withRef mvarSyntheticDecl.stx do
match mvarSyntheticDecl.kind with
| SyntheticMVarKind.typeClass =>
withMVarContext mvarSyntheticDecl.mvarId do
let mvarDecl ← getMVarDecl mvarSyntheticDecl.mvarId
unless (← get).messages.hasErrors do
throwError "typeclass instance problem is stuck, it is often due to metavariables{indentExpr mvarDecl.type}"
| SyntheticMVarKind.coe header eNew expectedType eType e f? =>
let mvarId := eNew.appArg!.mvarId!
withMVarContext mvarId do
let mvarDecl ← getMVarDecl mvarId
throwTypeMismatchError header expectedType eType e f? (some ("failed to create type class instance for " ++ indentExpr mvarDecl.type))
| _ => unreachable! -- TODO handle other cases.
private def getSomeSynthethicMVarsRef : TermElabM Syntax := do
let s ← get
match s.syntheticMVars.find? fun (mvarDecl : SyntheticMVarDecl) => !mvarDecl.stx.getPos?.isNone with
| some mvarDecl => return mvarDecl.stx
| none => return Syntax.missing
mutual
partial def runTactic (mvarId : MVarId) (tacticCode : Syntax) : TermElabM Unit := do
/- Recall, `tacticCode` is the whole `by ...` expression. -/
let byTk := tacticCode[0]
let code := tacticCode[1]
modifyThe Meta.State fun s => { s with mctx := s.mctx.instantiateMVarDeclMVars mvarId }
let remainingGoals ← withInfoHole mvarId <| Tactic.run mvarId do
withTacticInfoContext tacticCode (evalTactic code)
synthesizeSyntheticMVars (mayPostpone := false)
unless remainingGoals.isEmpty do
reportUnsolvedGoals remainingGoals
/-- Try to synthesize the given pending synthetic metavariable. -/
private partial def synthesizeSyntheticMVar (mvarSyntheticDecl : SyntheticMVarDecl) (postponeOnError : Bool) (runTactics : Bool) : TermElabM Bool :=
withRef mvarSyntheticDecl.stx do
match mvarSyntheticDecl.kind with
| SyntheticMVarKind.typeClass => synthesizePendingInstMVar mvarSyntheticDecl.mvarId
| SyntheticMVarKind.coe header? eNew expectedType eType e f? => synthesizePendingCoeInstMVar mvarSyntheticDecl.mvarId header? eNew expectedType eType e f?
-- NOTE: actual processing at `synthesizeSyntheticMVarsAux`
| SyntheticMVarKind.postponed savedContext => resumePostponed savedContext mvarSyntheticDecl.stx mvarSyntheticDecl.mvarId postponeOnError
| SyntheticMVarKind.tactic tacticCode savedContext =>
withSavedContext savedContext do
if runTactics then
runTactic mvarSyntheticDecl.mvarId tacticCode
return true
else
return false
/--
Try to synthesize the current list of pending synthetic metavariables.
Return `true` if at least one of them was synthesized. -/
private partial def synthesizeSyntheticMVarsStep (postponeOnError : Bool) (runTactics : Bool) : TermElabM Bool := do
let ctx ← read
traceAtCmdPos `Elab.resuming fun _ =>
m!"resuming synthetic metavariables, mayPostpone: {ctx.mayPostpone}, postponeOnError: {postponeOnError}"
let syntheticMVars := (← get).syntheticMVars
let numSyntheticMVars := syntheticMVars.length
-- We reset `syntheticMVars` because new synthetic metavariables may be created by `synthesizeSyntheticMVar`.
modify fun s => { s with syntheticMVars := [] }
-- Recall that `syntheticMVars` is a list where head is the most recent pending synthetic metavariable.
-- We use `filterRevM` instead of `filterM` to make sure we process the synthetic metavariables using the order they were created.
-- It would not be incorrect to use `filterM`.
let remainingSyntheticMVars ← syntheticMVars.filterRevM fun mvarDecl => do
-- We use `traceM` because we want to make sure the metavar local context is used to trace the message
traceM `Elab.postpone (withMVarContext mvarDecl.mvarId do addMessageContext m!"resuming {mkMVar mvarDecl.mvarId}")
let succeeded ← synthesizeSyntheticMVar mvarDecl postponeOnError runTactics
trace[Elab.postpone] if succeeded then fmt "succeeded" else fmt "not ready yet"
pure !succeeded
-- Merge new synthetic metavariables with `remainingSyntheticMVars`, i.e., metavariables that still couldn't be synthesized
modify fun s => { s with syntheticMVars := s.syntheticMVars ++ remainingSyntheticMVars }
return numSyntheticMVars != remainingSyntheticMVars.length
/--
Try to process pending synthetic metavariables. If `mayPostpone == false`,
then `syntheticMVars` is `[]` after executing this method.
It keeps executing `synthesizeSyntheticMVarsStep` while progress is being made.
If `mayPostpone == false`, then it applies default instances to `SyntheticMVarKind.typeClass` (if available)
metavariables that are still unresolved, and then tries to resolve metavariables
with `mayPostpone == false`. That is, we force them to produce error messages and/or commit to
a "best option". If, after that, we still haven't made progress, we report "stuck" errors. -/
partial def synthesizeSyntheticMVars (mayPostpone := true) : TermElabM Unit :=
let rec loop (u : Unit) : TermElabM Unit := do
withRef (← getSomeSynthethicMVarsRef) <| withIncRecDepth do
unless (← get).syntheticMVars.isEmpty do
if ← synthesizeSyntheticMVarsStep (postponeOnError := false) (runTactics := false) then
loop ()
else if !mayPostpone then
/- Resume pending metavariables with "elaboration postponement" disabled.
We postpone elaboration errors in this step by setting `postponeOnError := true`.
Example:
```
#check let x := ⟨1, 2⟩; Prod.fst x
```
The term `⟨1, 2⟩` can't be elaborated because the expected type is not know.
The `x` at `Prod.fst x` is not elaborated because the type of `x` is not known.
When we execute the following step with "elaboration postponement" disabled,
the elaborator fails at `⟨1, 2⟩` and postpones it, and succeeds at `x` and learns
that its type must be of the form `Prod ?α ?β`.
Recall that we postponed `x` at `Prod.fst x` because its type it is not known.
We the type of `x` may learn later its type and it may contain implicit and/or auto arguments.
By disabling postponement, we are essentially giving up the opportunity of learning `x`s type
and assume it does not have implict and/or auto arguments. -/
if ← withoutPostponing <| synthesizeSyntheticMVarsStep (postponeOnError := true) (runTactics := false) then
loop ()
else if ← synthesizeUsingDefault then
loop ()
else if ← withoutPostponing <| synthesizeSyntheticMVarsStep (postponeOnError := false) (runTactics := false) then
loop ()
else if ← synthesizeSyntheticMVarsStep (postponeOnError := false) (runTactics := true) then
loop ()
else
reportStuckSyntheticMVars
loop ()
end
def synthesizeSyntheticMVarsNoPostponing : TermElabM Unit :=
synthesizeSyntheticMVars (mayPostpone := false)
/- Keep invoking `synthesizeUsingDefault` until it returns false. -/
private partial def synthesizeUsingDefaultLoop : TermElabM Unit := do
if (← synthesizeUsingDefault) then
synthesizeSyntheticMVars (mayPostpone := true)
synthesizeUsingDefaultLoop
def synthesizeSyntheticMVarsUsingDefault : TermElabM Unit := do
synthesizeSyntheticMVars (mayPostpone := true)
synthesizeUsingDefaultLoop
private partial def withSynthesizeImp {α} (k : TermElabM α) (mayPostpone : Bool) : TermElabM α := do
let syntheticMVarsSaved := (← get).syntheticMVars
modify fun s => { s with syntheticMVars := [] }
try
let a ← k
synthesizeSyntheticMVars mayPostpone
if mayPostpone then
synthesizeUsingDefaultLoop
return a
finally
modify fun s => { s with syntheticMVars := s.syntheticMVars ++ syntheticMVarsSaved }
/--
Execute `k`, and synthesize pending synthetic metavariables created while executing `k` are solved.
If `mayPostpone == false`, then all of them must be synthesized.
Remark: even if `mayPostpone == true`, the method still uses `synthesizeUsingDefault` -/
@[inline] def withSynthesize [MonadFunctorT TermElabM m] [Monad m] (k : m α) (mayPostpone := false) : m α :=
monadMap (m := TermElabM) (withSynthesizeImp . mayPostpone) k
/-- Elaborate `stx`, and make sure all pending synthetic metavariables created while elaborating `stx` are solved. -/
def elabTermAndSynthesize (stx : Syntax) (expectedType? : Option Expr) : TermElabM Expr :=
withRef stx do
instantiateMVars (← withSynthesize <| elabTerm stx expectedType?)
end Lean.Elab.Term
|
45e6999873620d63ae4882419a2f21e6ae6ee35d | 4727251e0cd73359b15b664c3170e5d754078599 | /archive/imo/imo1959_q1.lean | 750f25078a71353bfcaa328811c5ef70792d8f2d | [
"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 | 902 | lean | /-
Copyright (c) 2020 Kevin Lacker. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Lacker
-/
import tactic.ring
import data.nat.prime
/-!
# IMO 1959 Q1
Prove that the fraction `(21n+4)/(14n+3)` is irreducible for every natural number `n`.
Since Lean doesn't have a concept of "irreducible fractions" per se, we just formalize this
as saying the numerator and denominator are relatively prime.
-/
open nat
lemma calculation (n k : ℕ) (h1 : k ∣ 21 * n + 4) (h2 : k ∣ 14 * n + 3) : k ∣ 1 :=
have h3 : k ∣ 2 * (21 * n + 4), from h1.mul_left 2,
have h4 : k ∣ 3 * (14 * n + 3), from h2.mul_left 3,
have h5 : 3 * (14 * n + 3) = 2 * (21 * n + 4) + 1, by ring,
(nat.dvd_add_right h3).mp (h5 ▸ h4)
theorem imo1959_q1 : ∀ n : ℕ, coprime (21 * n + 4) (14 * n + 3) :=
assume n, coprime_of_dvd' $ λ k hp h1 h2, calculation n k h1 h2
|
03d720b8066cda4168d693fe24440d1f82ceb44b | ec5a7ae10c533e1b1f4b0bc7713e91ecf829a3eb | /ijcar16/congr.lean | 2d08535e4bcfa1bb7638903ce445c13e577eeda2 | [
"MIT"
] | permissive | leanprover/leanprover.github.io | cf248934af7c7e9aeff17cf8df3c12c5e7e73f1a | 071a20d2e059a2c3733e004c681d3949cac3c07a | refs/heads/master | 1,692,621,047,417 | 1,691,396,994,000 | 1,691,396,994,000 | 19,366,263 | 18 | 27 | MIT | 1,693,989,071,000 | 1,399,006,345,000 | Lean | UTF-8 | Lean | false | false | 8,259 | lean | namespace paper
/-
In Lean {x : A} is used to declare implicit parameters that must be inferred
by solving typing constraints.
In Lean, the introduction rule and elimination rule for eq are called `eq.refl`
and `eq.rec` respectively.
So, we define refl and erec using them to match the notation used in the paper.
-/
definition refl {A : Type} (a : A) : a = a :=
eq.refl a
/- Remark: in eq.rec, the motive C is also an implicit argument -/
definition erec {A : Type} {a : A} (C : A → Type) (h₁ : C a) {b : A} (h₂ : a = b) : C b :=
eq.rec h₁ h₂
definition cast {A B : Type} (e : A = B) (a : A) : B :=
erec (λ x, x) a e
local infixr ` ▸ ` := cast
/- eq is symmetric -/
lemma symm {A : Type} {a b : A} : a = b → b = a :=
λ e, erec (λ x, x = a) (refl a) e
/- eq is transitivte -/
lemma trans {A : Type} {a b c : A} : a = b → b = c → a = c :=
λ e₁ e₂, erec (λ x, a = x) e₁ e₂
/- Simple congruence lemma for non-dependent functions. -/
lemma congr {A B : Type} {f g : A → B} {a b : A} : f = g → a = b → f a = g b :=
λ e₁ e₂,
let p : f a = g a := erec (λ x, f a = x a) (refl (f a)) e₁ in
erec (λ x, f a = g x) p e₂
/-
Heterogeneous equality
----------------------
There are many ways to define heterogeneous equality.
In the following one, given (a : A) (b : B), we say `a == b` iff
there is a proof (e : A = B) that can be used to cast `a` into B.
Two equivalent ways of defining heterogeneous equality can be found
at the end of this file.
-/
definition heq {A B : Type} (a : A) (b : B) :=
∃ e : A = B, e ▸ a = b
local infix ` == `:50 := heq
/- heq is reflexive -/
lemma hrefl {A : Type} (a : A) : a == a :=
Exists.intro (refl A) (refl a)
lemma flip {A B : Type} {e : A = B} : ∀ {a : A} {b : B}, e ▸ a = b → a = (symm e) ▸ b :=
eq.drec_on e (λ (a b : A) h, h)
lemma merge {A B C : Type} {e₁ : A = B} :
∀ {e₂ : B = C} {a : A} {b : B} {c : C}, e₁ ▸ a = b → e₂ ▸ b = c → (trans e₁ e₂) ▸ a = c :=
eq.drec_on e₁ (λ e₂, eq.drec_on e₂ (λ a b c e₃ e₄, trans e₃ e₄))
/- heq is symmetric -/
lemma hsymm {A B : Type} {a : A} {b : B} : a == b → b == a :=
begin
intro e, induction e with [e₁, e₂],
constructor, exact symm (flip e₂)
end
/- heq is transitivte -/
lemma htrans {A B C : Type} {a : A} {b : B} {c : C} : a == b → b == c → a == c :=
begin
intro e₁ e₂,
induction e₁ with [e₁₁, e₁₂],
induction e₂ with [e₂₁, e₂₂],
constructor, exact merge e₁₂ e₂₂
end
/- Converting homogeneous equality to heterogeneous equality -/
lemma ofeq {A : Type} {a b : A} : a = b → a == b :=
λ e, erec (λ x, a == x) (hrefl a) e
/- For the reverse direction, we need to use UIP or proof irrelevance -/
lemma ofheq {A : Type} {a b : A} : a == b → a = b :=
begin
intro e,
induction e with [e₁, e₂],
-- e₁ : A = A,
-- e₂ : e₁ ▸ a = b
-- |- a = b
have aux : e₁ = (refl A), from proof_irrel e₁ (refl A),
rewrite aux at e₂,
-- e₁ : A = A,
-- e₂ : (refl A) ▸ a = b
-- |- a = b
-- Now ((refl A) ▸ a = b) is definitionally equal to (a = b)
-- even in systems that do not have K-axiom in the kernel.
exact e₂
end
/-
Congruence lemmas for heterogeneous equality
--------------------------------------------
We can prove any instance of hcongr_n using the following
tactic script
begin
intro _ _ e, induction (ofheq e),
repeat (intro _ _ e; induction (ofheq e)),
apply hrefl
end
Now, we apply it to a few instances of hcongr_n
-/
lemma hcongr₁ {A₁ : Type}
{A₂ : A₁ → Type} :
Π {f f' : Π a₁, A₂ a₁}, f == f' →
Π {a₁ a₁' : A₁}, a₁ == a₁' →
f a₁ == f' a₁' :=
begin
intro _ _ e, induction (ofheq e),
repeat (intro _ _ e; induction (ofheq e)),
apply hrefl
end
lemma hcongr₂ {A₁ : Type}
{A₂ : A₁ → Type}
{A₃ : Π a₁, A₂ a₁ → Type} :
Π {f f' : Π a₁ a₂, A₃ a₁ a₂}, f = f' →
Π {a₁ a₁' : A₁}, a₁ == a₁' →
Π {a₂ : A₂ a₁} {a₂' : A₂ a₁'}, a₂ == a₂' →
f a₁ a₂ == f' a₁' a₂' :=
begin
intro _ _ e, induction e,
repeat (intro _ _ e; induction (ofheq e)),
apply hrefl
end
lemma hcongr₃ {A₁ : Type}
{A₂ : A₁ → Type}
{A₃ : Π a₁, A₂ a₁ → Type}
{A₄ : Π a₁ a₂, A₃ a₁ a₂ → Type} :
Π {f f' : Π a₁ a₂ a₃, A₄ a₁ a₂ a₃}, f = f' →
Π {a₁ a₁' : A₁}, a₁ == a₁' →
Π {a₂ : A₂ a₁} {a₂' : A₂ a₁'}, a₂ == a₂' →
Π {a₃ : A₃ a₁ a₂} {a₃' : A₃ a₁' a₂'}, a₃ == a₃' →
f a₁ a₂ a₃ == f' a₁' a₂' a₃' :=
begin
intro _ _ e, induction e,
repeat (intro _ _ e; induction (ofheq e)),
apply hrefl
end
lemma hcongr₄ {A₁ : Type}
{A₂ : A₁ → Type}
{A₃ : Π a₁, A₂ a₁ → Type}
{A₄ : Π a₁ a₂, A₃ a₁ a₂ → Type}
{A₅ : Π a₁ a₂ a₃, A₄ a₁ a₂ a₃ → Type} :
Π {f f' : Π a₁ a₂ a₃ a₄, A₅ a₁ a₂ a₃ a₄}, f = f' →
Π {a₁ a₁' : A₁}, a₁ == a₁' →
Π {a₂ : A₂ a₁} {a₂' : A₂ a₁'}, a₂ == a₂' →
Π {a₃ : A₃ a₁ a₂} {a₃' : A₃ a₁' a₂'}, a₃ == a₃' →
Π {a₄ : A₄ a₁ a₂ a₃} {a₄' : A₄ a₁' a₂' a₃'}, a₄ == a₄' →
f a₁ a₂ a₃ a₄ == f' a₁' a₂' a₃' a₄' :=
begin
intro _ _ e, induction e,
repeat (intro _ _ e; induction (ofheq e)),
apply hrefl
end
lemma hcongr₅ {A₁ : Type}
{A₂ : A₁ → Type}
{A₃ : Π a₁, A₂ a₁ → Type}
{A₄ : Π a₁ a₂, A₃ a₁ a₂ → Type}
{A₅ : Π a₁ a₂ a₃, A₄ a₁ a₂ a₃ → Type}
{A₆ : Π a₁ a₂ a₃ a₄, A₅ a₁ a₂ a₃ a₄ → Type} :
Π {f f' : Π a₁ a₂ a₃ a₄ a₅, A₆ a₁ a₂ a₃ a₄ a₅}, f = f' →
Π {a₁ a₁' : A₁}, a₁ == a₁' →
Π {a₂ : A₂ a₁} {a₂' : A₂ a₁'}, a₂ == a₂' →
Π {a₃ : A₃ a₁ a₂} {a₃' : A₃ a₁' a₂'}, a₃ == a₃' →
Π {a₄ : A₄ a₁ a₂ a₃} {a₄' : A₄ a₁' a₂' a₃'}, a₄ == a₄' →
Π {a₅ : A₅ a₁ a₂ a₃ a₄} {a₅' : A₅ a₁' a₂' a₃' a₄'}, a₅ == a₅' →
f a₁ a₂ a₃ a₄ a₅ == f' a₁' a₂' a₃' a₄' a₅' :=
begin
intro _ _ e, induction e,
repeat (intro _ _ e; induction (ofheq e)),
apply hrefl
end
/-
Equivalent ways of defining heterogeneous equality
--------------------------------------------------
In Coq, heterogeneous equality is called JMeq,
and is defined as the following inductive datatype.
-/
inductive JMeq {A : Type} (a : A) : ∀ {B : Type}, B → Prop :=
refl : JMeq a a
lemma heq_iff_JMeq {A B : Type} {a : A} {b : B} : a == b ↔ JMeq a b :=
begin
apply iff.intro,
{intro e, induction e with [e₁, e₂], subst e₁, subst e₂, apply JMeq.refl},
{assume e, JMeq.rec (hrefl a) e}
end
/-
Another way to define heterogeneous equality is as homogeneous equality
of pointed types.
-/
inductive ptdtype := mk : ∀ {A : Type}, A → ptdtype
definition ptdeq {A B : Type} (a : A) (b : B) : Prop := ptdtype.mk a = ptdtype.mk b
definition unptd (p : ptdtype) : Type := ptdtype.rec_on p (λ A a, A)
definition basept (p : ptdtype) : unptd p := ptdtype.rec_on p (λ A a, a)
lemma JMeq_of_ptdeq_helper : ∀ (p₁ p₂ : ptdtype), p₁ = p₂ -> JMeq (basept p₁) (basept p₂) :=
begin
intros p₁ p₂ eq_p₁_p₂,
induction eq_p₁_p₂,
apply JMeq.refl
end
lemma JMeq_iff_ptdeq {A B : Type} (a : A) (b : B) : JMeq a b ↔ ptdeq a b :=
begin
apply iff.intro,
{intro JMeq_a_b, induction JMeq_a_b, apply refl},
{intro ptdeq_a_b, apply JMeq_of_ptdeq_helper, exact ptdeq_a_b}
end
end paper
|
0f22db6301207d80379986c1f1f735a8698234f1 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/interactive/foldingRange.lean | 68967269e408c0babe43807ed97fd1dc258f0d7d | [
"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 | 419 | lean | --^ textDocument/foldingRange
import Lean
import Lean.Data
open Lean
namespace Foo
open Std
open Lean
section Bar
/-!
A module-level doc comment
-/
/--
Some documentation comment
-/
@[inline]
def add (x y : Nat) :=
x + y
inductive InductiveTy
| a
/--
Another doc comment. This one is not folded.
-/
| b
mutual
def a :=
1
def b :=
a
end
end Bar
end Foo
#check #[
1,
2,
3
]
|
827609fcaa54eb1b36951a3bb9cd2d713820c3fe | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /library/logic/connectives.lean | b604a9496e64f5fc4b5bf3c23bc8057f55ef4885 | [
"Apache-2.0"
] | permissive | codyroux/lean | 7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3 | 0cca265db19f7296531e339192e9b9bae4a31f8b | refs/heads/master | 1,610,909,964,159 | 1,407,084,399,000 | 1,416,857,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,390 | lean | -- Copyright (c) 2014 Microsoft Corporation. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Leonardo de Moura, Jeremy Avigad
import general_notation .eq
-- and
-- ---
inductive and (a b : Prop) : Prop :=
intro : a → b → and a b
notation a /\ b := and a b
notation a ∧ b := and a b
variables {a b c d : Prop}
namespace and
theorem elim (H₁ : a ∧ b) (H₂ : a → b → c) : c :=
rec H₂ H₁
definition elim_left (H : a ∧ b) : a :=
rec (λa b, a) H
definition elim_right (H : a ∧ b) : b :=
rec (λa b, b) H
theorem swap (H : a ∧ b) : b ∧ a :=
intro (elim_right H) (elim_left H)
definition not_left (b : Prop) (Hna : ¬a) : ¬(a ∧ b) :=
assume H : a ∧ b, absurd (elim_left H) Hna
definition not_right (a : Prop) {b : Prop} (Hnb : ¬b) : ¬(a ∧ b) :=
assume H : a ∧ b, absurd (elim_right H) Hnb
theorem imp_and (H₁ : a ∧ b) (H₂ : a → c) (H₃ : b → d) : c ∧ d :=
elim H₁ (assume Ha : a, assume Hb : b, intro (H₂ Ha) (H₃ Hb))
theorem imp_left (H₁ : a ∧ c) (H : a → b) : b ∧ c :=
elim H₁ (assume Ha : a, assume Hc : c, intro (H Ha) Hc)
theorem imp_right (H₁ : c ∧ a) (H : a → b) : c ∧ b :=
elim H₁ (assume Hc : c, assume Ha : a, intro Hc (H Ha))
end and
-- or
-- --
inductive or (a b : Prop) : Prop :=
intro_left : a → or a b,
intro_right : b → or a b
notation a `\/` b := or a b
notation a ∨ b := or a b
namespace or
definition inl (Ha : a) : a ∨ b :=
intro_left b Ha
definition inr (Hb : b) : a ∨ b :=
intro_right a Hb
theorem elim (H₁ : a ∨ b) (H₂ : a → c) (H₃ : b → c) : c :=
rec H₂ H₃ H₁
theorem elim3 (H : a ∨ b ∨ c) (Ha : a → d) (Hb : b → d) (Hc : c → d) : d :=
elim H Ha (assume H₂, elim H₂ Hb Hc)
theorem resolve_right (H₁ : a ∨ b) (H₂ : ¬a) : b :=
elim H₁ (assume Ha, absurd Ha H₂) (assume Hb, Hb)
theorem resolve_left (H₁ : a ∨ b) (H₂ : ¬b) : a :=
elim H₁ (assume Ha, Ha) (assume Hb, absurd Hb H₂)
theorem swap (H : a ∨ b) : b ∨ a :=
elim H (assume Ha, inr Ha) (assume Hb, inl Hb)
definition not_intro (Hna : ¬a) (Hnb : ¬b) : ¬(a ∨ b) :=
assume H : a ∨ b, or.rec_on H
(assume Ha, absurd Ha Hna)
(assume Hb, absurd Hb Hnb)
theorem imp_or (H₁ : a ∨ b) (H₂ : a → c) (H₃ : b → d) : c ∨ d :=
elim H₁
(assume Ha : a, inl (H₂ Ha))
(assume Hb : b, inr (H₃ Hb))
theorem imp_or_left (H₁ : a ∨ c) (H : a → b) : b ∨ c :=
elim H₁
(assume H₂ : a, inl (H H₂))
(assume H₂ : c, inr H₂)
theorem imp_or_right (H₁ : c ∨ a) (H : a → b) : c ∨ b :=
elim H₁
(assume H₂ : c, inl H₂)
(assume H₂ : a, inr (H H₂))
end or
theorem not_not_em {p : Prop} : ¬¬(p ∨ ¬p) :=
assume not_em : ¬(p ∨ ¬p),
have Hnp : ¬p, from
assume Hp : p, absurd (or.inl Hp) not_em,
absurd (or.inr Hnp) not_em
-- iff
-- ---
definition iff (a b : Prop) := (a → b) ∧ (b → a)
notation a <-> b := iff a b
notation a ↔ b := iff a b
namespace iff
definition def : (a ↔ b) = ((a → b) ∧ (b → a)) :=
rfl
definition intro (H₁ : a → b) (H₂ : b → a) : a ↔ b :=
and.intro H₁ H₂
definition elim (H₁ : (a → b) → (b → a) → c) (H₂ : a ↔ b) : c :=
and.rec H₁ H₂
definition elim_left (H : a ↔ b) : a → b :=
elim (assume H₁ H₂, H₁) H
definition mp := @elim_left
definition elim_right (H : a ↔ b) : b → a :=
elim (assume H₁ H₂, H₂) H
definition flip_sign (H₁ : a ↔ b) : ¬a ↔ ¬b :=
intro
(assume Hna, mt (elim_right H₁) Hna)
(assume Hnb, mt (elim_left H₁) Hnb)
definition refl (a : Prop) : a ↔ a :=
intro (assume H, H) (assume H, H)
definition rfl {a : Prop} : a ↔ a :=
refl a
theorem trans (H₁ : a ↔ b) (H₂ : b ↔ c) : a ↔ c :=
intro
(assume Ha, elim_left H₂ (elim_left H₁ Ha))
(assume Hc, elim_right H₁ (elim_right H₂ Hc))
theorem symm (H : a ↔ b) : b ↔ a :=
intro
(assume Hb, elim_right H Hb)
(assume Ha, elim_left H Ha)
theorem true_elim (H : a ↔ true) : a :=
mp (symm H) trivial
theorem false_elim (H : a ↔ false) : ¬a :=
assume Ha : a, mp H Ha
end iff
calc_refl iff.refl
calc_trans iff.trans
open eq.ops
theorem eq_to_iff {a b : Prop} (H : a = b) : a ↔ b :=
iff.intro (λ Ha, H ▸ Ha) (λ Hb, H⁻¹ ▸ Hb)
-- comm and assoc for and / or
-- ---------------------------
namespace and
theorem comm : a ∧ b ↔ b ∧ a :=
iff.intro (λH, swap H) (λH, swap H)
theorem assoc : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) :=
iff.intro
(assume H, intro
(elim_left (elim_left H))
(intro (elim_right (elim_left H)) (elim_right H)))
(assume H, intro
(intro (elim_left H) (elim_left (elim_right H)))
(elim_right (elim_right H)))
end and
namespace or
theorem comm : a ∨ b ↔ b ∨ a :=
iff.intro (λH, swap H) (λH, swap H)
theorem assoc : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) :=
iff.intro
(assume H, elim H
(assume H₁, elim H₁
(assume Ha, inl Ha)
(assume Hb, inr (inl Hb)))
(assume Hc, inr (inr Hc)))
(assume H, elim H
(assume Ha, (inl (inl Ha)))
(assume H₁, elim H₁
(assume Hb, inl (inr Hb))
(assume Hc, inr Hc)))
end or
|
27e6ffd87b65510cebb04c4decf56aa13de91cee | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/topology/metric_space/hausdorff_distance.lean | 9077225247886cfa619157bece5d9e3a223c8929 | [
"Apache-2.0"
] | permissive | hikari0108/mathlib | b7ea2b7350497ab1a0b87a09d093ecc025a50dfa | a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901 | refs/heads/master | 1,690,483,608,260 | 1,631,541,580,000 | 1,631,541,580,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 32,623 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import topology.metric_space.isometry
import topology.instances.ennreal
import analysis.specific_limits
/-!
# Hausdorff distance
The Hausdorff distance on subsets of a metric (or emetric) space.
Given two subsets `s` and `t` of a metric space, their Hausdorff distance is the smallest `d`
such that any point `s` is within `d` of a point in `t`, and conversely. This quantity
is often infinite (think of `s` bounded and `t` unbounded), and therefore better
expressed in the setting of emetric spaces.
## Main definitions
This files introduces:
* `inf_edist x s`, the infimum edistance of a point `x` to a set `s` in an emetric space
* `Hausdorff_edist s t`, the Hausdorff edistance of two sets in an emetric space
* Versions of these notions on metric spaces, called respectively `inf_dist` and
`Hausdorff_dist`.
-/
noncomputable theory
open_locale classical nnreal ennreal topological_space
universes u v w
open classical set function topological_space filter
namespace emetric
section inf_edist
variables {α : Type u} {β : Type v} [pseudo_emetric_space α] [pseudo_emetric_space β] {x y : α}
{s t : set α} {Φ : α → β}
/-! ### Distance of a point to a set as a function into `ℝ≥0∞`. -/
/-- The minimal edistance of a point to a set -/
def inf_edist (x : α) (s : set α) : ℝ≥0∞ := ⨅ y ∈ s, edist x y
@[simp] lemma inf_edist_empty : inf_edist x ∅ = ∞ := infi_emptyset
lemma le_inf_edist {d} : d ≤ inf_edist x s ↔ ∀ y ∈ s, d ≤ edist x y :=
by simp only [inf_edist, le_infi_iff]
/-- The edist to a union is the minimum of the edists -/
@[simp] lemma inf_edist_union : inf_edist x (s ∪ t) = inf_edist x s ⊓ inf_edist x t :=
infi_union
/-- The edist to a singleton is the edistance to the single point of this singleton -/
@[simp] lemma inf_edist_singleton : inf_edist x {y} = edist x y :=
infi_singleton
/-- The edist to a set is bounded above by the edist to any of its points -/
lemma inf_edist_le_edist_of_mem (h : y ∈ s) : inf_edist x s ≤ edist x y :=
binfi_le _ h
/-- If a point `x` belongs to `s`, then its edist to `s` vanishes -/
lemma inf_edist_zero_of_mem (h : x ∈ s) : inf_edist x s = 0 :=
nonpos_iff_eq_zero.1 $ @edist_self _ _ x ▸ inf_edist_le_edist_of_mem h
/-- The edist is monotonous with respect to inclusion -/
lemma inf_edist_le_inf_edist_of_subset (h : s ⊆ t) : inf_edist x t ≤ inf_edist x s :=
infi_le_infi_of_subset h
/-- If the edist to a set is `< r`, there exists a point in the set at edistance `< r` -/
lemma exists_edist_lt_of_inf_edist_lt {r : ℝ≥0∞} (h : inf_edist x s < r) :
∃y∈s, edist x y < r :=
by simpa only [inf_edist, infi_lt_iff] using h
/-- The edist of `x` to `s` is bounded by the sum of the edist of `y` to `s` and
the edist from `x` to `y` -/
lemma inf_edist_le_inf_edist_add_edist : inf_edist x s ≤ inf_edist y s + edist x y :=
calc (⨅ z ∈ s, edist x z) ≤ ⨅ z ∈ s, edist y z + edist x y :
binfi_le_binfi $ λ z hz, (edist_triangle _ _ _).trans_eq (add_comm _ _)
... = (⨅ z ∈ s, edist y z) + edist x y : by simp only [ennreal.infi_add]
/-- The edist to a set depends continuously on the point -/
@[continuity]
lemma continuous_inf_edist : continuous (λx, inf_edist x s) :=
continuous_of_le_add_edist 1 (by simp) $
by simp only [one_mul, inf_edist_le_inf_edist_add_edist, forall_2_true_iff]
/-- The edist to a set and to its closure coincide -/
lemma inf_edist_closure : inf_edist x (closure s) = inf_edist x s :=
begin
refine le_antisymm (inf_edist_le_inf_edist_of_subset subset_closure) _,
refine ennreal.le_of_forall_pos_le_add (λε εpos h, _),
have εpos' : (0 : ℝ≥0∞) < ε := by simpa,
have : inf_edist x (closure s) < inf_edist x (closure s) + ε/2 :=
ennreal.lt_add_right h.ne (ennreal.half_pos εpos'),
rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ycs, hy⟩,
-- y : α, ycs : y ∈ closure s, hy : edist x y < inf_edist x (closure s) + ↑ε / 2
rcases emetric.mem_closure_iff.1 ycs (ε/2) (ennreal.half_pos εpos') with ⟨z, zs, dyz⟩,
-- z : α, zs : z ∈ s, dyz : edist y z < ↑ε / 2
calc inf_edist x s ≤ edist x z : inf_edist_le_edist_of_mem zs
... ≤ edist x y + edist y z : edist_triangle _ _ _
... ≤ (inf_edist x (closure s) + ε / 2) + (ε/2) : add_le_add (le_of_lt hy) (le_of_lt dyz)
... = inf_edist x (closure s) + ↑ε : by rw [add_assoc, ennreal.add_halves]
end
/-- A point belongs to the closure of `s` iff its infimum edistance to this set vanishes -/
lemma mem_closure_iff_inf_edist_zero : x ∈ closure s ↔ inf_edist x s = 0 :=
⟨λh, by rw ← inf_edist_closure; exact inf_edist_zero_of_mem h,
λh, emetric.mem_closure_iff.2 $ λε εpos, exists_edist_lt_of_inf_edist_lt (by rwa h)⟩
/-- Given a closed set `s`, a point belongs to `s` iff its infimum edistance to this set vanishes -/
lemma mem_iff_inf_edist_zero_of_closed (h : is_closed s) : x ∈ s ↔ inf_edist x s = 0 :=
begin
convert ← mem_closure_iff_inf_edist_zero,
exact h.closure_eq
end
/-- The infimum edistance is invariant under isometries -/
lemma inf_edist_image (hΦ : isometry Φ) :
inf_edist (Φ x) (Φ '' t) = inf_edist x t :=
by simp only [inf_edist, infi_image, hΦ.edist_eq]
lemma _root_.is_open.exists_Union_is_closed {U : set α} (hU : is_open U) :
∃ F : ℕ → set α, (∀ n, is_closed (F n)) ∧ (∀ n, F n ⊆ U) ∧ ((⋃ n, F n) = U) ∧ (monotone F) :=
begin
obtain ⟨a, a_pos, a_lt_one⟩ : ∃ (a : ℝ≥0∞), 0 < a ∧ a < 1 := exists_between (ennreal.zero_lt_one),
let F := λ (n : ℕ), (λ x, inf_edist x Uᶜ) ⁻¹' (Ici (a^n)),
have F_subset : ∀ n, F n ⊆ U,
{ assume n x hx,
by_contra h,
rw [← mem_compl_iff,
mem_iff_inf_edist_zero_of_closed (is_open.is_closed_compl hU)] at h,
have : 0 < inf_edist x Uᶜ := lt_of_lt_of_le (ennreal.pow_pos a_pos _) hx,
rw h at this,
exact lt_irrefl _ this },
refine ⟨F, λ n, is_closed.preimage continuous_inf_edist is_closed_Ici, F_subset, _, _⟩,
show monotone F,
{ assume m n hmn x hx,
simp only [mem_Ici, mem_preimage] at hx ⊢,
apply le_trans (ennreal.pow_le_pow_of_le_one a_lt_one.le hmn) hx },
show (⋃ n, F n) = U,
{ refine subset.antisymm (by simp only [Union_subset_iff, F_subset, forall_const]) (λ x hx, _),
have : ¬(x ∈ Uᶜ), by simpa using hx,
rw mem_iff_inf_edist_zero_of_closed (is_open.is_closed_compl hU) at this,
have B : 0 < inf_edist x Uᶜ, by simpa [pos_iff_ne_zero] using this,
have : filter.tendsto (λ n, a^n) at_top (𝓝 0) :=
ennreal.tendsto_pow_at_top_nhds_0_of_lt_1 a_lt_one,
rcases ((tendsto_order.1 this).2 _ B).exists with ⟨n, hn⟩,
simp only [mem_Union, mem_Ici, mem_preimage],
exact ⟨n, hn.le⟩ },
end
end inf_edist --section
/-! ### The Hausdorff distance as a function into `ℝ≥0∞`. -/
/-- The Hausdorff edistance between two sets is the smallest `r` such that each set
is contained in the `r`-neighborhood of the other one -/
@[irreducible] def Hausdorff_edist {α : Type u} [pseudo_emetric_space α] (s t : set α) : ℝ≥0∞ :=
(⨆ x ∈ s, inf_edist x t) ⊔ (⨆ y ∈ t, inf_edist y s)
lemma Hausdorff_edist_def {α : Type u} [pseudo_emetric_space α] (s t : set α) :
Hausdorff_edist s t = (⨆ x ∈ s, inf_edist x t) ⊔ (⨆ y ∈ t, inf_edist y s) :=
by rw Hausdorff_edist
section Hausdorff_edist
variables {α : Type u} {β : Type v} [pseudo_emetric_space α] [pseudo_emetric_space β]
{x y : α} {s t u : set α} {Φ : α → β}
/-- The Hausdorff edistance of a set to itself vanishes -/
@[simp] lemma Hausdorff_edist_self : Hausdorff_edist s s = 0 :=
begin
simp only [Hausdorff_edist_def, sup_idem, ennreal.supr_eq_zero],
exact λ x hx, inf_edist_zero_of_mem hx
end
/-- The Haudorff edistances of `s` to `t` and of `t` to `s` coincide -/
lemma Hausdorff_edist_comm : Hausdorff_edist s t = Hausdorff_edist t s :=
by unfold Hausdorff_edist; apply sup_comm
/-- Bounding the Hausdorff edistance by bounding the edistance of any point
in each set to the other set -/
lemma Hausdorff_edist_le_of_inf_edist {r : ℝ≥0∞}
(H1 : ∀x ∈ s, inf_edist x t ≤ r) (H2 : ∀x ∈ t, inf_edist x s ≤ r) :
Hausdorff_edist s t ≤ r :=
begin
simp only [Hausdorff_edist, sup_le_iff, supr_le_iff],
exact ⟨H1, H2⟩
end
/-- Bounding the Hausdorff edistance by exhibiting, for any point in each set,
another point in the other set at controlled distance -/
lemma Hausdorff_edist_le_of_mem_edist {r : ℝ≥0∞}
(H1 : ∀x ∈ s, ∃y ∈ t, edist x y ≤ r) (H2 : ∀x ∈ t, ∃y ∈ s, edist x y ≤ r) :
Hausdorff_edist s t ≤ r :=
begin
refine Hausdorff_edist_le_of_inf_edist _ _,
{ assume x xs,
rcases H1 x xs with ⟨y, yt, hy⟩,
exact le_trans (inf_edist_le_edist_of_mem yt) hy },
{ assume x xt,
rcases H2 x xt with ⟨y, ys, hy⟩,
exact le_trans (inf_edist_le_edist_of_mem ys) hy }
end
/-- The distance to a set is controlled by the Hausdorff distance -/
lemma inf_edist_le_Hausdorff_edist_of_mem (h : x ∈ s) : inf_edist x t ≤ Hausdorff_edist s t :=
begin
rw Hausdorff_edist_def,
refine le_trans _ le_sup_left,
exact le_bsupr x h
end
/-- If the Hausdorff distance is `<r`, then any point in one of the sets has
a corresponding point at distance `<r` in the other set -/
lemma exists_edist_lt_of_Hausdorff_edist_lt {r : ℝ≥0∞} (h : x ∈ s)
(H : Hausdorff_edist s t < r) :
∃y∈t, edist x y < r :=
exists_edist_lt_of_inf_edist_lt $ calc
inf_edist x t ≤ Hausdorff_edist s t : inf_edist_le_Hausdorff_edist_of_mem h
... < r : H
/-- The distance from `x` to `s` or `t` is controlled in terms of the Hausdorff distance
between `s` and `t` -/
lemma inf_edist_le_inf_edist_add_Hausdorff_edist :
inf_edist x t ≤ inf_edist x s + Hausdorff_edist s t :=
ennreal.le_of_forall_pos_le_add $ λε εpos h, begin
have εpos' : (0 : ℝ≥0∞) < ε := by simpa,
have : inf_edist x s < inf_edist x s + ε/2 :=
ennreal.lt_add_right (ennreal.add_lt_top.1 h).1.ne (ennreal.half_pos εpos'),
rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ys, dxy⟩,
-- y : α, ys : y ∈ s, dxy : edist x y < inf_edist x s + ↑ε / 2
have : Hausdorff_edist s t < Hausdorff_edist s t + ε/2 :=
ennreal.lt_add_right (ennreal.add_lt_top.1 h).2.ne (ennreal.half_pos εpos'),
rcases exists_edist_lt_of_Hausdorff_edist_lt ys this with ⟨z, zt, dyz⟩,
-- z : α, zt : z ∈ t, dyz : edist y z < Hausdorff_edist s t + ↑ε / 2
calc inf_edist x t ≤ edist x z : inf_edist_le_edist_of_mem zt
... ≤ edist x y + edist y z : edist_triangle _ _ _
... ≤ (inf_edist x s + ε/2) + (Hausdorff_edist s t + ε/2) : add_le_add dxy.le dyz.le
... = inf_edist x s + Hausdorff_edist s t + ε :
by simp [ennreal.add_halves, add_comm, add_left_comm]
end
/-- The Hausdorff edistance is invariant under eisometries -/
lemma Hausdorff_edist_image (h : isometry Φ) :
Hausdorff_edist (Φ '' s) (Φ '' t) = Hausdorff_edist s t :=
by simp only [Hausdorff_edist_def, supr_image, inf_edist_image h]
/-- The Hausdorff distance is controlled by the diameter of the union -/
lemma Hausdorff_edist_le_ediam (hs : s.nonempty) (ht : t.nonempty) :
Hausdorff_edist s t ≤ diam (s ∪ t) :=
begin
rcases hs with ⟨x, xs⟩,
rcases ht with ⟨y, yt⟩,
refine Hausdorff_edist_le_of_mem_edist _ _,
{ intros z hz,
exact ⟨y, yt, edist_le_diam_of_mem (subset_union_left _ _ hz) (subset_union_right _ _ yt)⟩ },
{ intros z hz,
exact ⟨x, xs, edist_le_diam_of_mem (subset_union_right _ _ hz) (subset_union_left _ _ xs)⟩ }
end
/-- The Hausdorff distance satisfies the triangular inequality -/
lemma Hausdorff_edist_triangle : Hausdorff_edist s u ≤ Hausdorff_edist s t + Hausdorff_edist t u :=
begin
rw Hausdorff_edist_def,
simp only [sup_le_iff, supr_le_iff],
split,
show ∀x ∈ s, inf_edist x u ≤ Hausdorff_edist s t + Hausdorff_edist t u, from λx xs, calc
inf_edist x u ≤ inf_edist x t + Hausdorff_edist t u : inf_edist_le_inf_edist_add_Hausdorff_edist
... ≤ Hausdorff_edist s t + Hausdorff_edist t u :
add_le_add_right (inf_edist_le_Hausdorff_edist_of_mem xs) _,
show ∀x ∈ u, inf_edist x s ≤ Hausdorff_edist s t + Hausdorff_edist t u, from λx xu, calc
inf_edist x s ≤ inf_edist x t + Hausdorff_edist t s : inf_edist_le_inf_edist_add_Hausdorff_edist
... ≤ Hausdorff_edist u t + Hausdorff_edist t s :
add_le_add_right (inf_edist_le_Hausdorff_edist_of_mem xu) _
... = Hausdorff_edist s t + Hausdorff_edist t u : by simp [Hausdorff_edist_comm, add_comm]
end
/-- Two sets are at zero Hausdorff edistance if and only if they have the same closure -/
lemma Hausdorff_edist_zero_iff_closure_eq_closure :
Hausdorff_edist s t = 0 ↔ closure s = closure t :=
calc Hausdorff_edist s t = 0 ↔ s ⊆ closure t ∧ t ⊆ closure s :
by simp only [Hausdorff_edist_def, ennreal.sup_eq_zero, ennreal.supr_eq_zero,
← mem_closure_iff_inf_edist_zero, subset_def]
... ↔ closure s = closure t :
⟨λ h, subset.antisymm (closure_minimal h.1 is_closed_closure)
(closure_minimal h.2 is_closed_closure),
λ h, ⟨h ▸ subset_closure, h.symm ▸ subset_closure⟩⟩
/-- The Hausdorff edistance between a set and its closure vanishes -/
@[simp, priority 1100]
lemma Hausdorff_edist_self_closure : Hausdorff_edist s (closure s) = 0 :=
by rw [Hausdorff_edist_zero_iff_closure_eq_closure, closure_closure]
/-- Replacing a set by its closure does not change the Hausdorff edistance. -/
@[simp] lemma Hausdorff_edist_closure₁ : Hausdorff_edist (closure s) t = Hausdorff_edist s t :=
begin
refine le_antisymm _ _,
{ calc _ ≤ Hausdorff_edist (closure s) s + Hausdorff_edist s t : Hausdorff_edist_triangle
... = Hausdorff_edist s t : by simp [Hausdorff_edist_comm] },
{ calc _ ≤ Hausdorff_edist s (closure s) + Hausdorff_edist (closure s) t :
Hausdorff_edist_triangle
... = Hausdorff_edist (closure s) t : by simp }
end
/-- Replacing a set by its closure does not change the Hausdorff edistance. -/
@[simp] lemma Hausdorff_edist_closure₂ : Hausdorff_edist s (closure t) = Hausdorff_edist s t :=
by simp [@Hausdorff_edist_comm _ _ s _]
/-- The Hausdorff edistance between sets or their closures is the same -/
@[simp] lemma Hausdorff_edist_closure :
Hausdorff_edist (closure s) (closure t) = Hausdorff_edist s t :=
by simp
/-- Two closed sets are at zero Hausdorff edistance if and only if they coincide -/
lemma Hausdorff_edist_zero_iff_eq_of_closed (hs : is_closed s) (ht : is_closed t) :
Hausdorff_edist s t = 0 ↔ s = t :=
by rw [Hausdorff_edist_zero_iff_closure_eq_closure, hs.closure_eq, ht.closure_eq]
/-- The Haudorff edistance to the empty set is infinite -/
lemma Hausdorff_edist_empty (ne : s.nonempty) : Hausdorff_edist s ∅ = ∞ :=
begin
rcases ne with ⟨x, xs⟩,
have : inf_edist x ∅ ≤ Hausdorff_edist s ∅ := inf_edist_le_Hausdorff_edist_of_mem xs,
simpa using this,
end
/-- If a set is at finite Hausdorff edistance of a nonempty set, it is nonempty -/
lemma nonempty_of_Hausdorff_edist_ne_top (hs : s.nonempty) (fin : Hausdorff_edist s t ≠ ⊤) :
t.nonempty :=
t.eq_empty_or_nonempty.elim (λ ht, (fin $ ht.symm ▸ Hausdorff_edist_empty hs).elim) id
lemma empty_or_nonempty_of_Hausdorff_edist_ne_top (fin : Hausdorff_edist s t ≠ ⊤) :
s = ∅ ∧ t = ∅ ∨ s.nonempty ∧ t.nonempty :=
begin
cases s.eq_empty_or_nonempty with hs hs,
{ cases t.eq_empty_or_nonempty with ht ht,
{ exact or.inl ⟨hs, ht⟩ },
{ rw Hausdorff_edist_comm at fin,
exact or.inr ⟨nonempty_of_Hausdorff_edist_ne_top ht fin, ht⟩ } },
{ exact or.inr ⟨hs, nonempty_of_Hausdorff_edist_ne_top hs fin⟩ }
end
end Hausdorff_edist -- section
end emetric --namespace
/-! Now, we turn to the same notions in metric spaces. To avoid the difficulties related to
`Inf` and `Sup` on `ℝ` (which is only conditionally complete), we use the notions in `ℝ≥0∞`
formulated in terms of the edistance, and coerce them to `ℝ`.
Then their properties follow readily from the corresponding properties in `ℝ≥0∞`,
modulo some tedious rewriting of inequalities from one to the other. -/
namespace metric
section
variables {α : Type u} {β : Type v} [pseudo_metric_space α] [pseudo_metric_space β]
{s t u : set α} {x y : α} {Φ : α → β}
open emetric
/-! ### Distance of a point to a set as a function into `ℝ`. -/
/-- The minimal distance of a point to a set -/
def inf_dist (x : α) (s : set α) : ℝ := ennreal.to_real (inf_edist x s)
/-- the minimal distance is always nonnegative -/
lemma inf_dist_nonneg : 0 ≤ inf_dist x s := by simp [inf_dist]
/-- the minimal distance to the empty set is 0 (if you want to have the more reasonable
value ∞ instead, use `inf_edist`, which takes values in ℝ≥0∞) -/
@[simp] lemma inf_dist_empty : inf_dist x ∅ = 0 :=
by simp [inf_dist]
/-- In a metric space, the minimal edistance to a nonempty set is finite -/
lemma inf_edist_ne_top (h : s.nonempty) : inf_edist x s ≠ ⊤ :=
begin
rcases h with ⟨y, hy⟩,
apply lt_top_iff_ne_top.1,
calc inf_edist x s ≤ edist x y : inf_edist_le_edist_of_mem hy
... < ⊤ : lt_top_iff_ne_top.2 (edist_ne_top _ _)
end
/-- The minimal distance of a point to a set containing it vanishes -/
lemma inf_dist_zero_of_mem (h : x ∈ s) : inf_dist x s = 0 :=
by simp [inf_edist_zero_of_mem h, inf_dist]
/-- The minimal distance to a singleton is the distance to the unique point in this singleton -/
@[simp] lemma inf_dist_singleton : inf_dist x {y} = dist x y :=
by simp [inf_dist, inf_edist, dist_edist]
/-- The minimal distance to a set is bounded by the distance to any point in this set -/
lemma inf_dist_le_dist_of_mem (h : y ∈ s) : inf_dist x s ≤ dist x y :=
begin
rw [dist_edist, inf_dist,
ennreal.to_real_le_to_real (inf_edist_ne_top ⟨_, h⟩) (edist_ne_top _ _)],
exact inf_edist_le_edist_of_mem h
end
/-- The minimal distance is monotonous with respect to inclusion -/
lemma inf_dist_le_inf_dist_of_subset (h : s ⊆ t) (hs : s.nonempty) :
inf_dist x t ≤ inf_dist x s :=
begin
have ht : t.nonempty := hs.mono h,
rw [inf_dist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ht) (inf_edist_ne_top hs)],
exact inf_edist_le_inf_edist_of_subset h
end
/-- If the minimal distance to a set is `<r`, there exists a point in this set at distance `<r` -/
lemma exists_dist_lt_of_inf_dist_lt {r : real} (h : inf_dist x s < r) (hs : s.nonempty) :
∃y∈s, dist x y < r :=
begin
have rpos : 0 < r := lt_of_le_of_lt inf_dist_nonneg h,
have : inf_edist x s < ennreal.of_real r,
{ rwa [inf_dist, ← ennreal.to_real_of_real (le_of_lt rpos),
ennreal.to_real_lt_to_real (inf_edist_ne_top hs)] at h,
simp },
rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ys, hy⟩,
rw [edist_dist, ennreal.of_real_lt_of_real_iff rpos] at hy,
exact ⟨y, ys, hy⟩,
end
/-- The minimal distance from `x` to `s` is bounded by the distance from `y` to `s`, modulo
the distance between `x` and `y` -/
lemma inf_dist_le_inf_dist_add_dist : inf_dist x s ≤ inf_dist y s + dist x y :=
begin
cases s.eq_empty_or_nonempty with hs hs,
{ by simp [hs, dist_nonneg] },
{ rw [inf_dist, inf_dist, dist_edist,
← ennreal.to_real_add (inf_edist_ne_top hs) (edist_ne_top _ _),
ennreal.to_real_le_to_real (inf_edist_ne_top hs)],
{ apply inf_edist_le_inf_edist_add_edist },
{ simp [ennreal.add_eq_top, inf_edist_ne_top hs, edist_ne_top] }}
end
variable (s)
/-- The minimal distance to a set is Lipschitz in point with constant 1 -/
lemma lipschitz_inf_dist_pt : lipschitz_with 1 (λx, inf_dist x s) :=
lipschitz_with.of_le_add $ λ x y, inf_dist_le_inf_dist_add_dist
/-- The minimal distance to a set is uniformly continuous in point -/
lemma uniform_continuous_inf_dist_pt :
uniform_continuous (λx, inf_dist x s) :=
(lipschitz_inf_dist_pt s).uniform_continuous
/-- The minimal distance to a set is continuous in point -/
@[continuity]
lemma continuous_inf_dist_pt : continuous (λx, inf_dist x s) :=
(uniform_continuous_inf_dist_pt s).continuous
variable {s}
/-- The minimal distance to a set and its closure coincide -/
lemma inf_dist_eq_closure : inf_dist x (closure s) = inf_dist x s :=
by simp [inf_dist, inf_edist_closure]
/-- A point belongs to the closure of `s` iff its infimum distance to this set vanishes -/
lemma mem_closure_iff_inf_dist_zero (h : s.nonempty) : x ∈ closure s ↔ inf_dist x s = 0 :=
by simp [mem_closure_iff_inf_edist_zero, inf_dist, ennreal.to_real_eq_zero_iff, inf_edist_ne_top h]
/-- Given a closed set `s`, a point belongs to `s` iff its infimum distance to this set vanishes -/
lemma mem_iff_inf_dist_zero_of_closed (h : is_closed s) (hs : s.nonempty) :
x ∈ s ↔ inf_dist x s = 0 :=
begin
have := @mem_closure_iff_inf_dist_zero _ _ s x hs,
rwa h.closure_eq at this
end
/-- The infimum distance is invariant under isometries -/
lemma inf_dist_image (hΦ : isometry Φ) :
inf_dist (Φ x) (Φ '' t) = inf_dist x t :=
by simp [inf_dist, inf_edist_image hΦ]
/-! ### Distance of a point to a set as a function into `ℝ≥0`. -/
/-- The minimal distance of a point to a set as a `ℝ≥0` -/
def inf_nndist (x : α) (s : set α) : ℝ≥0 := ennreal.to_nnreal (inf_edist x s)
@[simp] lemma coe_inf_nndist : (inf_nndist x s : ℝ) = inf_dist x s := rfl
/-- The minimal distance to a set (as `ℝ≥0`) is Lipschitz in point with constant 1 -/
lemma lipschitz_inf_nndist_pt (s : set α) : lipschitz_with 1 (λx, inf_nndist x s) :=
lipschitz_with.of_le_add $ λ x y, inf_dist_le_inf_dist_add_dist
/-- The minimal distance to a set (as `ℝ≥0`) is uniformly continuous in point -/
lemma uniform_continuous_inf_nndist_pt (s : set α) :
uniform_continuous (λx, inf_nndist x s) :=
(lipschitz_inf_nndist_pt s).uniform_continuous
/-- The minimal distance to a set (as `ℝ≥0`) is continuous in point -/
lemma continuous_inf_nndist_pt (s : set α) : continuous (λx, inf_nndist x s) :=
(uniform_continuous_inf_nndist_pt s).continuous
/-! ### The Hausdorff distance as a function into `ℝ`. -/
/-- The Hausdorff distance between two sets is the smallest nonnegative `r` such that each set is
included in the `r`-neighborhood of the other. If there is no such `r`, it is defined to
be `0`, arbitrarily -/
def Hausdorff_dist (s t : set α) : ℝ := ennreal.to_real (Hausdorff_edist s t)
/-- The Hausdorff distance is nonnegative -/
lemma Hausdorff_dist_nonneg : 0 ≤ Hausdorff_dist s t :=
by simp [Hausdorff_dist]
/-- If two sets are nonempty and bounded in a metric space, they are at finite Hausdorff
edistance. -/
lemma Hausdorff_edist_ne_top_of_nonempty_of_bounded (hs : s.nonempty) (ht : t.nonempty)
(bs : bounded s) (bt : bounded t) : Hausdorff_edist s t ≠ ⊤ :=
begin
rcases hs with ⟨cs, hcs⟩,
rcases ht with ⟨ct, hct⟩,
rcases (bounded_iff_subset_ball ct).1 bs with ⟨rs, hrs⟩,
rcases (bounded_iff_subset_ball cs).1 bt with ⟨rt, hrt⟩,
have : Hausdorff_edist s t ≤ ennreal.of_real (max rs rt),
{ apply Hausdorff_edist_le_of_mem_edist,
{ assume x xs,
existsi [ct, hct],
have : dist x ct ≤ max rs rt := le_trans (hrs xs) (le_max_left _ _),
rwa [edist_dist, ennreal.of_real_le_of_real_iff],
exact le_trans dist_nonneg this },
{ assume x xt,
existsi [cs, hcs],
have : dist x cs ≤ max rs rt := le_trans (hrt xt) (le_max_right _ _),
rwa [edist_dist, ennreal.of_real_le_of_real_iff],
exact le_trans dist_nonneg this }},
exact ennreal.lt_top_iff_ne_top.1 (lt_of_le_of_lt this (by simp [lt_top_iff_ne_top]))
end
/-- The Hausdorff distance between a set and itself is zero -/
@[simp] lemma Hausdorff_dist_self_zero : Hausdorff_dist s s = 0 :=
by simp [Hausdorff_dist]
/-- The Hausdorff distance from `s` to `t` and from `t` to `s` coincide -/
lemma Hausdorff_dist_comm : Hausdorff_dist s t = Hausdorff_dist t s :=
by simp [Hausdorff_dist, Hausdorff_edist_comm]
/-- The Hausdorff distance to the empty set vanishes (if you want to have the more reasonable
value ∞ instead, use `Hausdorff_edist`, which takes values in ℝ≥0∞) -/
@[simp] lemma Hausdorff_dist_empty : Hausdorff_dist s ∅ = 0 :=
begin
cases s.eq_empty_or_nonempty with h h,
{ simp [h] },
{ simp [Hausdorff_dist, Hausdorff_edist_empty h] }
end
/-- The Hausdorff distance to the empty set vanishes (if you want to have the more reasonable
value ∞ instead, use `Hausdorff_edist`, which takes values in ℝ≥0∞) -/
@[simp] lemma Hausdorff_dist_empty' : Hausdorff_dist ∅ s = 0 :=
by simp [Hausdorff_dist_comm]
/-- Bounding the Hausdorff distance by bounding the distance of any point
in each set to the other set -/
lemma Hausdorff_dist_le_of_inf_dist {r : ℝ} (hr : 0 ≤ r)
(H1 : ∀x ∈ s, inf_dist x t ≤ r) (H2 : ∀x ∈ t, inf_dist x s ≤ r) :
Hausdorff_dist s t ≤ r :=
begin
by_cases h1 : Hausdorff_edist s t = ⊤,
by rwa [Hausdorff_dist, h1, ennreal.top_to_real],
cases s.eq_empty_or_nonempty with hs hs,
by rwa [hs, Hausdorff_dist_empty'],
cases t.eq_empty_or_nonempty with ht ht,
by rwa [ht, Hausdorff_dist_empty],
have : Hausdorff_edist s t ≤ ennreal.of_real r,
{ apply Hausdorff_edist_le_of_inf_edist _ _,
{ assume x hx,
have I := H1 x hx,
rwa [inf_dist, ← ennreal.to_real_of_real hr,
ennreal.to_real_le_to_real (inf_edist_ne_top ht) ennreal.of_real_ne_top] at I },
{ assume x hx,
have I := H2 x hx,
rwa [inf_dist, ← ennreal.to_real_of_real hr,
ennreal.to_real_le_to_real (inf_edist_ne_top hs) ennreal.of_real_ne_top] at I }},
rwa [Hausdorff_dist, ← ennreal.to_real_of_real hr,
ennreal.to_real_le_to_real h1 ennreal.of_real_ne_top]
end
/-- Bounding the Hausdorff distance by exhibiting, for any point in each set,
another point in the other set at controlled distance -/
lemma Hausdorff_dist_le_of_mem_dist {r : ℝ} (hr : 0 ≤ r)
(H1 : ∀x ∈ s, ∃y ∈ t, dist x y ≤ r) (H2 : ∀x ∈ t, ∃y ∈ s, dist x y ≤ r) :
Hausdorff_dist s t ≤ r :=
begin
apply Hausdorff_dist_le_of_inf_dist hr,
{ assume x xs,
rcases H1 x xs with ⟨y, yt, hy⟩,
exact le_trans (inf_dist_le_dist_of_mem yt) hy },
{ assume x xt,
rcases H2 x xt with ⟨y, ys, hy⟩,
exact le_trans (inf_dist_le_dist_of_mem ys) hy }
end
/-- The Hausdorff distance is controlled by the diameter of the union -/
lemma Hausdorff_dist_le_diam (hs : s.nonempty) (bs : bounded s) (ht : t.nonempty) (bt : bounded t) :
Hausdorff_dist s t ≤ diam (s ∪ t) :=
begin
rcases hs with ⟨x, xs⟩,
rcases ht with ⟨y, yt⟩,
refine Hausdorff_dist_le_of_mem_dist diam_nonneg _ _,
{ exact λz hz, ⟨y, yt, dist_le_diam_of_mem (bounded_union.2 ⟨bs, bt⟩)
(subset_union_left _ _ hz) (subset_union_right _ _ yt)⟩ },
{ exact λz hz, ⟨x, xs, dist_le_diam_of_mem (bounded_union.2 ⟨bs, bt⟩)
(subset_union_right _ _ hz) (subset_union_left _ _ xs)⟩ }
end
/-- The distance to a set is controlled by the Hausdorff distance -/
lemma inf_dist_le_Hausdorff_dist_of_mem (hx : x ∈ s) (fin : Hausdorff_edist s t ≠ ⊤) :
inf_dist x t ≤ Hausdorff_dist s t :=
begin
have ht : t.nonempty := nonempty_of_Hausdorff_edist_ne_top ⟨x, hx⟩ fin,
rw [Hausdorff_dist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ht) fin],
exact inf_edist_le_Hausdorff_edist_of_mem hx
end
/-- If the Hausdorff distance is `<r`, then any point in one of the sets is at distance
`<r` of a point in the other set -/
lemma exists_dist_lt_of_Hausdorff_dist_lt {r : ℝ} (h : x ∈ s) (H : Hausdorff_dist s t < r)
(fin : Hausdorff_edist s t ≠ ⊤) : ∃y∈t, dist x y < r :=
begin
have r0 : 0 < r := lt_of_le_of_lt (Hausdorff_dist_nonneg) H,
have : Hausdorff_edist s t < ennreal.of_real r,
by rwa [Hausdorff_dist, ← ennreal.to_real_of_real (le_of_lt r0),
ennreal.to_real_lt_to_real fin (ennreal.of_real_ne_top)] at H,
rcases exists_edist_lt_of_Hausdorff_edist_lt h this with ⟨y, hy, yr⟩,
rw [edist_dist, ennreal.of_real_lt_of_real_iff r0] at yr,
exact ⟨y, hy, yr⟩
end
/-- If the Hausdorff distance is `<r`, then any point in one of the sets is at distance
`<r` of a point in the other set -/
lemma exists_dist_lt_of_Hausdorff_dist_lt' {r : ℝ} (h : y ∈ t) (H : Hausdorff_dist s t < r)
(fin : Hausdorff_edist s t ≠ ⊤) : ∃x∈s, dist x y < r :=
begin
rw Hausdorff_dist_comm at H,
rw Hausdorff_edist_comm at fin,
simpa [dist_comm] using exists_dist_lt_of_Hausdorff_dist_lt h H fin
end
/-- The infimum distance to `s` and `t` are the same, up to the Hausdorff distance
between `s` and `t` -/
lemma inf_dist_le_inf_dist_add_Hausdorff_dist (fin : Hausdorff_edist s t ≠ ⊤) :
inf_dist x t ≤ inf_dist x s + Hausdorff_dist s t :=
begin
rcases empty_or_nonempty_of_Hausdorff_edist_ne_top fin with ⟨hs,ht⟩|⟨hs,ht⟩,
{ simp only [hs, ht, Hausdorff_dist_empty, inf_dist_empty, zero_add] },
rw [inf_dist, inf_dist, Hausdorff_dist, ← ennreal.to_real_add (inf_edist_ne_top hs) fin,
ennreal.to_real_le_to_real (inf_edist_ne_top ht)],
{ exact inf_edist_le_inf_edist_add_Hausdorff_edist },
{ exact ennreal.add_ne_top.2 ⟨inf_edist_ne_top hs, fin⟩ }
end
/-- The Hausdorff distance is invariant under isometries -/
lemma Hausdorff_dist_image (h : isometry Φ) :
Hausdorff_dist (Φ '' s) (Φ '' t) = Hausdorff_dist s t :=
by simp [Hausdorff_dist, Hausdorff_edist_image h]
/-- The Hausdorff distance satisfies the triangular inequality -/
lemma Hausdorff_dist_triangle (fin : Hausdorff_edist s t ≠ ⊤) :
Hausdorff_dist s u ≤ Hausdorff_dist s t + Hausdorff_dist t u :=
begin
by_cases Hausdorff_edist s u = ⊤,
{ calc Hausdorff_dist s u = 0 + 0 : by simp [Hausdorff_dist, h]
... ≤ Hausdorff_dist s t + Hausdorff_dist t u :
add_le_add (Hausdorff_dist_nonneg) (Hausdorff_dist_nonneg) },
{ have Dtu : Hausdorff_edist t u < ⊤ := calc
Hausdorff_edist t u ≤ Hausdorff_edist t s + Hausdorff_edist s u : Hausdorff_edist_triangle
... = Hausdorff_edist s t + Hausdorff_edist s u : by simp [Hausdorff_edist_comm]
... < ⊤ : by simp [ennreal.add_lt_top]; simp [ennreal.lt_top_iff_ne_top, h, fin],
rw [Hausdorff_dist, Hausdorff_dist, Hausdorff_dist,
← ennreal.to_real_add fin (lt_top_iff_ne_top.1 Dtu), ennreal.to_real_le_to_real h],
{ exact Hausdorff_edist_triangle },
{ simp [ennreal.add_eq_top, lt_top_iff_ne_top.1 Dtu, fin] }}
end
/-- The Hausdorff distance satisfies the triangular inequality -/
lemma Hausdorff_dist_triangle' (fin : Hausdorff_edist t u ≠ ⊤) :
Hausdorff_dist s u ≤ Hausdorff_dist s t + Hausdorff_dist t u :=
begin
rw Hausdorff_edist_comm at fin,
have I : Hausdorff_dist u s ≤ Hausdorff_dist u t + Hausdorff_dist t s :=
Hausdorff_dist_triangle fin,
simpa [add_comm, Hausdorff_dist_comm] using I
end
/-- The Hausdorff distance between a set and its closure vanish -/
@[simp, priority 1100]
lemma Hausdorff_dist_self_closure : Hausdorff_dist s (closure s) = 0 :=
by simp [Hausdorff_dist]
/-- Replacing a set by its closure does not change the Hausdorff distance. -/
@[simp] lemma Hausdorff_dist_closure₁ : Hausdorff_dist (closure s) t = Hausdorff_dist s t :=
by simp [Hausdorff_dist]
/-- Replacing a set by its closure does not change the Hausdorff distance. -/
@[simp] lemma Hausdorff_dist_closure₂ : Hausdorff_dist s (closure t) = Hausdorff_dist s t :=
by simp [Hausdorff_dist]
/-- The Hausdorff distance between two sets and their closures coincide -/
@[simp] lemma Hausdorff_dist_closure :
Hausdorff_dist (closure s) (closure t) = Hausdorff_dist s t :=
by simp [Hausdorff_dist]
/-- Two sets are at zero Hausdorff distance if and only if they have the same closures -/
lemma Hausdorff_dist_zero_iff_closure_eq_closure (fin : Hausdorff_edist s t ≠ ⊤) :
Hausdorff_dist s t = 0 ↔ closure s = closure t :=
by simp [Hausdorff_edist_zero_iff_closure_eq_closure.symm, Hausdorff_dist,
ennreal.to_real_eq_zero_iff, fin]
/-- Two closed sets are at zero Hausdorff distance if and only if they coincide -/
lemma Hausdorff_dist_zero_iff_eq_of_closed (hs : is_closed s) (ht : is_closed t)
(fin : Hausdorff_edist s t ≠ ⊤) : Hausdorff_dist s t = 0 ↔ s = t :=
by simp [(Hausdorff_edist_zero_iff_eq_of_closed hs ht).symm, Hausdorff_dist,
ennreal.to_real_eq_zero_iff, fin]
end --section
end metric --namespace
|
cde1090e29c6d9b3ad7220fb2f279246e35b243e | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/1011.lean | 3893b13f70619ed314eecef4419090b27ee8119c | [
"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 | 159 | lean | def f (x : A) := x
set_option relaxedAutoImplicit false
def g (x : A) := x
def h (x : AA) := x
def i (x : A1) := x
def j (x : A₁) := x
def k (x : A') := x
|
faf2a9150529903a28763c5bb67683bac447b334 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/fin/vec_notation.lean | e55841bcc108ea128fffbdd45313524f34ea85c4 | [
"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 | 13,033 | 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 data.fin.tuple
import data.list.range
import group_theory.group_action.pi
/-!
# Matrix and vector notation
This file defines notation for vectors and matrices. Given `a b c d : α`,
the notation allows us to write `![a, b, c, d] : fin 4 → α`.
Nesting vectors gives a matrix, so `![![a, b], ![c, d]] : fin 2 → fin 2 → α`.
Later we will define `matrix m n α` to be `m → n → α`, so the type of `![![a, b], ![c, d]]`
can be written as `matrix (fin 2) (fin 2) α`.
## Main definitions
* `vec_empty` is the empty vector (or `0` by `n` matrix) `![]`
* `vec_cons` prepends an entry to a vector, so `![a, b]` is `vec_cons a (vec_cons b vec_empty)`
## Implementation notes
The `simp` lemmas require that one of the arguments is of the form `vec_cons _ _`.
This ensures `simp` works with entries only when (some) entries are already given.
In other words, this notation will only appear in the output of `simp` if it
already appears in the input.
## Notations
The main new notation is `![a, b]`, which gets expanded to `vec_cons a (vec_cons b vec_empty)`.
## Examples
Examples of usage can be found in the `test/matrix.lean` file.
-/
namespace matrix
universe u
variables {α : Type u}
section matrix_notation
/-- `![]` is the vector with no entries. -/
def vec_empty : fin 0 → α :=
fin_zero_elim
/-- `vec_cons h t` prepends an entry `h` to a vector `t`.
The inverse functions are `vec_head` and `vec_tail`.
The notation `![a, b, ...]` expands to `vec_cons a (vec_cons b ...)`.
-/
def vec_cons {n : ℕ} (h : α) (t : fin n → α) : fin n.succ → α :=
fin.cons h t
notation `![` l:(foldr `, ` (h t, vec_cons h t) vec_empty `]`) := l
/-- `vec_head v` gives the first entry of the vector `v` -/
def vec_head {n : ℕ} (v : fin n.succ → α) : α :=
v 0
/-- `vec_tail v` gives a vector consisting of all entries of `v` except the first -/
def vec_tail {n : ℕ} (v : fin n.succ → α) : fin n → α :=
v ∘ fin.succ
variables {m n : ℕ}
/-- Use `![...]` notation for displaying a vector `fin n → α`, for example:
```
#eval ![1, 2] + ![3, 4] -- ![4, 6]
```
-/
instance pi_fin.has_repr [has_repr α] : has_repr (fin n → α) :=
{ repr := λ f, "![" ++ (string.intercalate ", " ((list.fin_range n).map (λ n, repr (f n)))) ++ "]" }
end matrix_notation
variables {m n o : ℕ} {m' n' o' : Type*}
lemma empty_eq (v : fin 0 → α) : v = ![] := subsingleton.elim _ _
section val
@[simp] lemma head_fin_const (a : α) : vec_head (λ (i : fin (n + 1)), a) = a := rfl
@[simp] lemma cons_val_zero (x : α) (u : fin m → α) : vec_cons x u 0 = x := rfl
lemma cons_val_zero' (h : 0 < m.succ) (x : α) (u : fin m → α) :
vec_cons x u ⟨0, h⟩ = x :=
rfl
@[simp] lemma cons_val_succ (x : α) (u : fin m → α) (i : fin m) :
vec_cons x u i.succ = u i :=
by simp [vec_cons]
@[simp] lemma cons_val_succ' {i : ℕ} (h : i.succ < m.succ) (x : α) (u : fin m → α) :
vec_cons x u ⟨i.succ, h⟩ = u ⟨i, nat.lt_of_succ_lt_succ h⟩ :=
by simp only [vec_cons, fin.cons, fin.cases_succ']
@[simp] lemma head_cons (x : α) (u : fin m → α) :
vec_head (vec_cons x u) = x :=
rfl
@[simp] lemma tail_cons (x : α) (u : fin m → α) :
vec_tail (vec_cons x u) = u :=
by { ext, simp [vec_tail] }
@[simp] lemma empty_val' {n' : Type*} (j : n') :
(λ i, (![] : fin 0 → n' → α) i j) = ![] :=
empty_eq _
@[simp] lemma cons_head_tail (u : fin m.succ → α) :
vec_cons (vec_head u) (vec_tail u) = u :=
fin.cons_self_tail _
@[simp] lemma range_cons (x : α) (u : fin n → α) :
set.range (vec_cons x u) = {x} ∪ set.range u :=
set.ext $ λ y, by simp [fin.exists_fin_succ, eq_comm]
@[simp] lemma range_empty (u : fin 0 → α) : set.range u = ∅ :=
set.range_eq_empty _
@[simp] lemma vec_cons_const (a : α) : vec_cons a (λ k : fin n, a) = λ _, a :=
funext $ fin.forall_fin_succ.2 ⟨rfl, cons_val_succ _ _⟩
lemma vec_single_eq_const (a : α) : ![a] = λ _, a :=
funext $ unique.forall_iff.2 rfl
/-- `![a, b, ...] 1` is equal to `b`.
The simplifier needs a special lemma for length `≥ 2`, in addition to
`cons_val_succ`, because `1 : fin 1 = 0 : fin 1`.
-/
@[simp] lemma cons_val_one (x : α) (u : fin m.succ → α) :
vec_cons x u 1 = vec_head u :=
by { rw [← fin.succ_zero_eq_one, cons_val_succ], refl }
@[simp] lemma cons_val_fin_one (x : α) (u : fin 0 → α) (i : fin 1) :
vec_cons x u i = x :=
by { refine fin.forall_fin_one.2 _ i, refl }
lemma cons_fin_one (x : α) (u : fin 0 → α) : vec_cons x u = (λ _, x) :=
funext (cons_val_fin_one x u)
/-! ### Numeral (`bit0` and `bit1`) indices
The following definitions and `simp` lemmas are to allow any
numeral-indexed element of a vector given with matrix notation to
be extracted by `simp` (even when the numeral is larger than the
number of elements in the vector, which is taken modulo that number
of elements by virtue of the semantics of `bit0` and `bit1` and of
addition on `fin n`).
-/
@[simp] lemma empty_append (v : fin n → α) : fin.append (zero_add _).symm ![] v = v :=
by { ext, simp [fin.append] }
@[simp] lemma cons_append (ho : o + 1 = m + 1 + n) (x : α) (u : fin m → α) (v : fin n → α) :
fin.append ho (vec_cons x u) v =
vec_cons x (fin.append (by rwa [add_assoc, add_comm 1, ←add_assoc,
add_right_cancel_iff] at ho) u v) :=
begin
ext i,
simp_rw [fin.append],
split_ifs with h,
{ rcases i with ⟨⟨⟩ | i, hi⟩,
{ simp },
{ simp only [nat.succ_eq_add_one, add_lt_add_iff_right, fin.coe_mk] at h,
simp [h] } },
{ rcases i with ⟨⟨⟩ | i, hi⟩,
{ simpa using h },
{ rw [not_lt, fin.coe_mk, nat.succ_eq_add_one, add_le_add_iff_right] at h,
simp [h] } }
end
/-- `vec_alt0 v` gives a vector with half the length of `v`, with
only alternate elements (even-numbered). -/
def vec_alt0 (hm : m = n + n) (v : fin m → α) (k : fin n) : α :=
v ⟨(k : ℕ) + k, hm.symm ▸ add_lt_add k.property k.property⟩
/-- `vec_alt1 v` gives a vector with half the length of `v`, with
only alternate elements (odd-numbered). -/
def vec_alt1 (hm : m = n + n) (v : fin m → α) (k : fin n) : α :=
v ⟨(k : ℕ) + k + 1, hm.symm ▸ nat.add_succ_lt_add k.property k.property⟩
lemma vec_alt0_append (v : fin n → α) : vec_alt0 rfl (fin.append rfl v v) = v ∘ bit0 :=
begin
ext i,
simp_rw [function.comp, bit0, vec_alt0, fin.append],
split_ifs with h; congr,
{ rw fin.coe_mk at h,
simp only [fin.ext_iff, fin.coe_add, fin.coe_mk],
exact (nat.mod_eq_of_lt h).symm },
{ rw [fin.coe_mk, not_lt] at h,
simp only [fin.ext_iff, fin.coe_add, fin.coe_mk, nat.mod_eq_sub_mod h],
refine (nat.mod_eq_of_lt _).symm,
rw tsub_lt_iff_left h,
exact add_lt_add i.property i.property }
end
lemma vec_alt1_append (v : fin (n + 1) → α) : vec_alt1 rfl (fin.append rfl v v) = v ∘ bit1 :=
begin
ext i,
simp_rw [function.comp, vec_alt1, fin.append],
cases n,
{ simp, congr },
{ split_ifs with h; simp_rw [bit1, bit0]; congr,
{ simp only [fin.ext_iff, fin.coe_add, fin.coe_mk],
rw fin.coe_mk at h,
rw fin.coe_one,
rw nat.mod_eq_of_lt (nat.lt_of_succ_lt h),
rw nat.mod_eq_of_lt h },
{ rw [fin.coe_mk, not_lt] at h,
simp only [fin.ext_iff, fin.coe_add, fin.coe_mk, nat.mod_add_mod, fin.coe_one,
nat.mod_eq_sub_mod h],
refine (nat.mod_eq_of_lt _).symm,
rw tsub_lt_iff_left h,
exact nat.add_succ_lt_add i.property i.property } }
end
@[simp] lemma vec_head_vec_alt0 (hm : (m + 2) = (n + 1) + (n + 1)) (v : fin (m + 2) → α) :
vec_head (vec_alt0 hm v) = v 0 := rfl
@[simp] lemma vec_head_vec_alt1 (hm : (m + 2) = (n + 1) + (n + 1)) (v : fin (m + 2) → α) :
vec_head (vec_alt1 hm v) = v 1 :=
by simp [vec_head, vec_alt1]
@[simp] lemma cons_vec_bit0_eq_alt0 (x : α) (u : fin n → α) (i : fin (n + 1)) :
vec_cons x u (bit0 i) = vec_alt0 rfl (fin.append rfl (vec_cons x u) (vec_cons x u)) i :=
by rw vec_alt0_append
@[simp] lemma cons_vec_bit1_eq_alt1 (x : α) (u : fin n → α) (i : fin (n + 1)) :
vec_cons x u (bit1 i) = vec_alt1 rfl (fin.append rfl (vec_cons x u) (vec_cons x u)) i :=
by rw vec_alt1_append
@[simp] lemma cons_vec_alt0 (h : m + 1 + 1 = (n + 1) + (n + 1)) (x y : α) (u : fin m → α) :
vec_alt0 h (vec_cons x (vec_cons y u)) = vec_cons x (vec_alt0
(by rwa [add_assoc n, add_comm 1, ←add_assoc, ←add_assoc, add_right_cancel_iff,
add_right_cancel_iff] at h) u) :=
begin
ext i,
simp_rw [vec_alt0],
rcases i with ⟨⟨⟩ | i, hi⟩,
{ refl },
{ simp [vec_alt0, nat.add_succ, nat.succ_add] }
end
-- Although proved by simp, extracting element 8 of a five-element
-- vector does not work by simp unless this lemma is present.
@[simp] lemma empty_vec_alt0 (α) {h} : vec_alt0 h (![] : fin 0 → α) = ![] :=
by simp
@[simp] lemma cons_vec_alt1 (h : m + 1 + 1 = (n + 1) + (n + 1)) (x y : α) (u : fin m → α) :
vec_alt1 h (vec_cons x (vec_cons y u)) = vec_cons y (vec_alt1
(by rwa [add_assoc n, add_comm 1, ←add_assoc, ←add_assoc, add_right_cancel_iff,
add_right_cancel_iff] at h) u) :=
begin
ext i,
simp_rw [vec_alt1],
rcases i with ⟨⟨⟩ | i, hi⟩,
{ refl },
{ simp [vec_alt1, nat.add_succ, nat.succ_add] }
end
-- Although proved by simp, extracting element 9 of a five-element
-- vector does not work by simp unless this lemma is present.
@[simp] lemma empty_vec_alt1 (α) {h} : vec_alt1 h (![] : fin 0 → α) = ![] :=
by simp
end val
section smul
variables {M : Type*} [has_scalar M α]
@[simp] lemma smul_empty (x : M) (v : fin 0 → α) : x • v = ![] := empty_eq _
@[simp] lemma smul_cons (x : M) (y : α) (v : fin n → α) :
x • vec_cons y v = vec_cons (x • y) (x • v) :=
by { ext i, refine fin.cases _ _ i; simp }
end smul
section add
variables [has_add α]
@[simp] lemma empty_add_empty (v w : fin 0 → α) : v + w = ![] := empty_eq _
@[simp] lemma cons_add (x : α) (v : fin n → α) (w : fin n.succ → α) :
vec_cons x v + w = vec_cons (x + vec_head w) (v + vec_tail w) :=
by { ext i, refine fin.cases _ _ i; simp [vec_head, vec_tail] }
@[simp] lemma add_cons (v : fin n.succ → α) (y : α) (w : fin n → α) :
v + vec_cons y w = vec_cons (vec_head v + y) (vec_tail v + w) :=
by { ext i, refine fin.cases _ _ i; simp [vec_head, vec_tail] }
@[simp] lemma cons_add_cons (x : α) (v : fin n → α) (y : α) (w : fin n → α) :
vec_cons x v + vec_cons y w = vec_cons (x + y) (v + w) :=
by simp
@[simp] lemma head_add (a b : fin n.succ → α) : vec_head (a + b) = vec_head a + vec_head b := rfl
@[simp] lemma tail_add (a b : fin n.succ → α) : vec_tail (a + b) = vec_tail a + vec_tail b := rfl
end add
section sub
variables [has_sub α]
@[simp] lemma empty_sub_empty (v w : fin 0 → α) : v - w = ![] := empty_eq _
@[simp] lemma cons_sub (x : α) (v : fin n → α) (w : fin n.succ → α) :
vec_cons x v - w = vec_cons (x - vec_head w) (v - vec_tail w) :=
by { ext i, refine fin.cases _ _ i; simp [vec_head, vec_tail] }
@[simp] lemma sub_cons (v : fin n.succ → α) (y : α) (w : fin n → α) :
v - vec_cons y w = vec_cons (vec_head v - y) (vec_tail v - w) :=
by { ext i, refine fin.cases _ _ i; simp [vec_head, vec_tail] }
@[simp] lemma cons_sub_cons (x : α) (v : fin n → α) (y : α) (w : fin n → α) :
vec_cons x v - vec_cons y w = vec_cons (x - y) (v - w) :=
by simp
@[simp] lemma head_sub (a b : fin n.succ → α) : vec_head (a - b) = vec_head a - vec_head b := rfl
@[simp] lemma tail_sub (a b : fin n.succ → α) : vec_tail (a - b) = vec_tail a - vec_tail b := rfl
end sub
section zero
variables [has_zero α]
@[simp] lemma zero_empty : (0 : fin 0 → α) = ![] :=
empty_eq _
@[simp] lemma cons_zero_zero : vec_cons (0 : α) (0 : fin n → α) = 0 :=
by { ext i j, refine fin.cases _ _ i, { refl }, simp }
@[simp] lemma head_zero : vec_head (0 : fin n.succ → α) = 0 := rfl
@[simp] lemma tail_zero : vec_tail (0 : fin n.succ → α) = 0 := rfl
@[simp] lemma cons_eq_zero_iff {v : fin n → α} {x : α} :
vec_cons x v = 0 ↔ x = 0 ∧ v = 0 :=
⟨ λ h, ⟨ congr_fun h 0, by { convert congr_arg vec_tail h, simp } ⟩,
λ ⟨hx, hv⟩, by simp [hx, hv] ⟩
open_locale classical
lemma cons_nonzero_iff {v : fin n → α} {x : α} :
vec_cons x v ≠ 0 ↔ (x ≠ 0 ∨ v ≠ 0) :=
⟨ λ h, not_and_distrib.mp (h ∘ cons_eq_zero_iff.mpr),
λ h, mt cons_eq_zero_iff.mp (not_and_distrib.mpr h) ⟩
end zero
section neg
variables [has_neg α]
@[simp] lemma neg_empty (v : fin 0 → α) : -v = ![] := empty_eq _
@[simp] lemma neg_cons (x : α) (v : fin n → α) :
-(vec_cons x v) = vec_cons (-x) (-v) :=
by { ext i, refine fin.cases _ _ i; simp }
@[simp] lemma head_neg (a : fin n.succ → α) : vec_head (-a) = -vec_head a := rfl
@[simp] lemma tail_neg (a : fin n.succ → α) : vec_tail (-a) = -vec_tail a := rfl
end neg
end matrix
|
cbe02ca62200ead5350324665060b125b2ad430e | 4fa161becb8ce7378a709f5992a594764699e268 | /src/data/pnat/basic.lean | 1e2ddd870309cf199df3ab2afdf7a0b2b4386ef9 | [
"Apache-2.0"
] | permissive | laughinggas/mathlib | e4aa4565ae34e46e834434284cb26bd9d67bc373 | 86dcd5cda7a5017c8b3c8876c89a510a19d49aad | refs/heads/master | 1,669,496,232,688 | 1,592,831,995,000 | 1,592,831,995,000 | 274,155,979 | 0 | 0 | Apache-2.0 | 1,592,835,190,000 | 1,592,835,189,000 | null | UTF-8 | Lean | false | false | 13,473 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro, Neil Strickland
-/
import data.nat.prime
/-- `ℕ+` is the type of positive natural numbers. It is defined as a subtype,
and the VM representation of `ℕ+` is the same as `ℕ` because the proof
is not stored. -/
def pnat := {n : ℕ // 0 < n}
notation `ℕ+` := pnat
instance coe_pnat_nat : has_coe ℕ+ ℕ := ⟨subtype.val⟩
instance : has_repr ℕ+ := ⟨λ n, repr n.1⟩
namespace nat
/-- Convert a natural number to a positive natural number. The
positivity assumption is inferred by `dec_trivial`. -/
def to_pnat (n : ℕ) (h : 0 < n . tactic.exact_dec_trivial) : ℕ+ := ⟨n, h⟩
/-- Write a successor as an element of `ℕ+`. -/
def succ_pnat (n : ℕ) : ℕ+ := ⟨succ n, succ_pos n⟩
@[simp] theorem succ_pnat_coe (n : ℕ) : (succ_pnat n : ℕ) = succ n := rfl
theorem succ_pnat_inj {n m : ℕ} : succ_pnat n = succ_pnat m → n = m :=
λ h, by { let h' := congr_arg (coe : ℕ+ → ℕ) h, exact nat.succ.inj h' }
/-- Convert a natural number to a pnat. `n+1` is mapped to itself,
and `0` becomes `1`. -/
def to_pnat' (n : ℕ) : ℕ+ := succ_pnat (pred n)
@[simp] theorem to_pnat'_coe : ∀ (n : ℕ),
((to_pnat' n) : ℕ) = ite (0 < n) n 1
| 0 := rfl
| (m + 1) := by {rw [if_pos (succ_pos m)], refl}
namespace primes
instance coe_pnat : has_coe nat.primes ℕ+ := ⟨λ p, ⟨(p : ℕ), p.property.pos⟩⟩
theorem coe_pnat_nat (p : nat.primes) : ((p : ℕ+) : ℕ) = p := rfl
theorem coe_pnat_inj (p q : nat.primes) : (p : ℕ+) = (q : ℕ+) → p = q := λ h,
begin
replace h : ((p : ℕ+) : ℕ) = ((q : ℕ+) : ℕ) := congr_arg subtype.val h,
rw [coe_pnat_nat, coe_pnat_nat] at h,
exact subtype.eq h,
end
end primes
end nat
namespace pnat
open nat
/-- We now define a long list of structures on ℕ+ induced by
similar structures on ℕ. Most of these behave in a completely
obvious way, but there are a few things to be said about
subtraction, division and powers.
-/
instance : decidable_eq ℕ+ := λ (a b : ℕ+), by apply_instance
instance : decidable_linear_order ℕ+ :=
subtype.decidable_linear_order _
@[simp] lemma mk_le_mk (n k : ℕ) (hn : 0 < n) (hk : 0 < k) :
(⟨n, hn⟩ : ℕ+) ≤ ⟨k, hk⟩ ↔ n ≤ k := iff.rfl
@[simp] lemma mk_lt_mk (n k : ℕ) (hn : 0 < n) (hk : 0 < k) :
(⟨n, hn⟩ : ℕ+) < ⟨k, hk⟩ ↔ n < k := iff.rfl
@[simp, norm_cast] lemma coe_le_coe (n k : ℕ+) : (n:ℕ) ≤ k ↔ n ≤ k := iff.rfl
@[simp, norm_cast] lemma coe_lt_coe (n k : ℕ+) : (n:ℕ) < k ↔ n < k := iff.rfl
@[simp] theorem pos (n : ℕ+) : 0 < (n : ℕ) := n.2
theorem eq {m n : ℕ+} : (m : ℕ) = n → m = n := subtype.eq
@[simp] theorem mk_coe (n h) : ((⟨n, h⟩ : ℕ+) : ℕ) = n := rfl
instance : add_comm_semigroup ℕ+ :=
{ add := λ a b, ⟨(a + b : ℕ), add_pos a.pos b.pos⟩,
add_comm := λ a b, subtype.eq (add_comm a b),
add_assoc := λ a b c, subtype.eq (add_assoc a b c) }
@[simp] theorem add_coe (m n : ℕ+) : ((m + n : ℕ+) : ℕ) = m + n := rfl
instance coe_add_hom : is_add_hom (coe : ℕ+ → ℕ) := ⟨add_coe⟩
instance : add_left_cancel_semigroup ℕ+ :=
{ add_left_cancel := λ a b c h, by {
replace h := congr_arg (coe : ℕ+ → ℕ) h,
rw [add_coe, add_coe] at h,
exact eq ((add_right_inj (a : ℕ)).mp h)},
.. (pnat.add_comm_semigroup) }
instance : add_right_cancel_semigroup ℕ+ :=
{ add_right_cancel := λ a b c h, by {
replace h := congr_arg (coe : ℕ+ → ℕ) h,
rw [add_coe, add_coe] at h,
exact eq ((add_left_inj (b : ℕ)).mp h)},
.. (pnat.add_comm_semigroup) }
@[simp] theorem ne_zero (n : ℕ+) : (n : ℕ) ≠ 0 := ne_of_gt n.2
theorem to_pnat'_coe {n : ℕ} : 0 < n → (n.to_pnat' : ℕ) = n := succ_pred_eq_of_pos
@[simp] theorem coe_to_pnat' (n : ℕ+) : (n : ℕ).to_pnat' = n := eq (to_pnat'_coe n.pos)
instance : comm_monoid ℕ+ :=
{ mul := λ m n, ⟨m.1 * n.1, mul_pos m.2 n.2⟩,
mul_assoc := λ a b c, subtype.eq (mul_assoc _ _ _),
one := succ_pnat 0,
one_mul := λ a, subtype.eq (one_mul _),
mul_one := λ a, subtype.eq (mul_one _),
mul_comm := λ a b, subtype.eq (mul_comm _ _) }
theorem lt_add_one_iff : ∀ {a b : ℕ+}, a < b + 1 ↔ a ≤ b :=
λ a b, nat.lt_add_one_iff
theorem add_one_le_iff : ∀ {a b : ℕ+}, a + 1 ≤ b ↔ a < b :=
λ a b, nat.add_one_le_iff
@[simp] lemma one_le (n : ℕ+) : (1 : ℕ+) ≤ n := n.2
instance : order_bot ℕ+ :=
{ bot := 1,
bot_le := λ a, a.property,
..(by apply_instance : partial_order ℕ+) }
@[simp] lemma bot_eq_zero : (⊥ : ℕ+) = 1 := rfl
instance : inhabited ℕ+ := ⟨1⟩
-- Some lemmas that rewrite `pnat.mk n h`, for `n` an explicit numeral, into explicit numerals.
@[simp] lemma mk_one {h} : (⟨1, h⟩ : ℕ+) = (1 : ℕ+) := rfl
@[simp] lemma mk_bit0 (n) {h} : (⟨bit0 n, h⟩ : ℕ+) = (bit0 ⟨n, pos_of_bit0_pos h⟩ : ℕ+) := rfl
@[simp] lemma mk_bit1 (n) {h} {k} : (⟨bit1 n, h⟩ : ℕ+) = (bit1 ⟨n, k⟩ : ℕ+) := rfl
-- Some lemmas that rewrite inequalities between explicit numerals in `pnat`
-- into the corresponding inequalities in `nat`.
-- TODO: perhaps this should not be attempted by `simp`,
-- and instead we should expect `norm_num` to take care of these directly?
-- TODO: these lemmas are perhaps incomplete:
-- * 1 is not represented as a bit0 or bit1
-- * strict inequalities?
@[simp] lemma bit0_le_bit0 (n m : ℕ+) : (bit0 n) ≤ (bit0 m) ↔ (bit0 (n : ℕ)) ≤ (bit0 (m : ℕ)) :=
iff.rfl
@[simp] lemma bit0_le_bit1 (n m : ℕ+) : (bit0 n) ≤ (bit1 m) ↔ (bit0 (n : ℕ)) ≤ (bit1 (m : ℕ)) :=
iff.rfl
@[simp] lemma bit1_le_bit0 (n m : ℕ+) : (bit1 n) ≤ (bit0 m) ↔ (bit1 (n : ℕ)) ≤ (bit0 (m : ℕ)) :=
iff.rfl
@[simp] lemma bit1_le_bit1 (n m : ℕ+) : (bit1 n) ≤ (bit1 m) ↔ (bit1 (n : ℕ)) ≤ (bit1 (m : ℕ)) :=
iff.rfl
@[simp] theorem one_coe : ((1 : ℕ+) : ℕ) = 1 := rfl
@[simp] theorem mul_coe (m n : ℕ+) : ((m * n : ℕ+) : ℕ) = m * n := rfl
instance coe_mul_hom : is_monoid_hom (coe : ℕ+ → ℕ) :=
{map_one := one_coe, map_mul := mul_coe}
@[simp] lemma coe_bit0 (a : ℕ+) : ((bit0 a : ℕ+) : ℕ) = bit0 (a : ℕ) := rfl
@[simp] lemma coe_bit1 (a : ℕ+) : ((bit1 a : ℕ+) : ℕ) = bit1 (a : ℕ) := rfl
@[simp] theorem pow_coe (m : ℕ+) (n : ℕ) : ((m ^ n : ℕ+) : ℕ) = (m : ℕ) ^ n :=
by induction n with n ih;
[refl, rw [nat.pow_succ, pow_succ, mul_coe, mul_comm, ih]]
instance : left_cancel_semigroup ℕ+ :=
{ mul_left_cancel := λ a b c h, by {
replace h := congr_arg (coe : ℕ+ → ℕ) h,
exact eq ((nat.mul_right_inj a.pos).mp h)},
.. (pnat.comm_monoid) }
instance : right_cancel_semigroup ℕ+ :=
{ mul_right_cancel := λ a b c h, by {
replace h := congr_arg (coe : ℕ+ → ℕ) h,
exact eq ((nat.mul_left_inj b.pos).mp h)},
.. (pnat.comm_monoid) }
instance : distrib ℕ+ :=
{ left_distrib := λ a b c, eq (mul_add a b c),
right_distrib := λ a b c, eq (add_mul a b c),
..(pnat.add_comm_semigroup), ..(pnat.comm_monoid) }
/-- Subtraction a - b is defined in the obvious way when
a > b, and by a - b = 1 if a ≤ b.
-/
instance : has_sub ℕ+ := ⟨λ a b, to_pnat' (a - b : ℕ)⟩
theorem sub_coe (a b : ℕ+) : ((a - b : ℕ+) : ℕ) = ite (b < a) (a - b : ℕ) 1 :=
begin
change ((to_pnat' ((a : ℕ) - (b : ℕ)) : ℕ)) =
ite ((a : ℕ) > (b : ℕ)) ((a : ℕ) - (b : ℕ)) 1,
split_ifs with h,
{ exact to_pnat'_coe (nat.sub_pos_of_lt h) },
{ rw [nat.sub_eq_zero_iff_le.mpr (le_of_not_gt h)], refl }
end
theorem add_sub_of_lt {a b : ℕ+} : a < b → a + (b - a) = b :=
λ h, eq $ by { rw [add_coe, sub_coe, if_pos h],
exact nat.add_sub_of_le (le_of_lt h) }
/-- We define m % k and m / k in the same way as for nat
except that when m = n * k we take m % k = k and
m / k = n - 1. This ensures that m % k is always positive
and m = (m % k) + k * (m / k) in all cases. Later we
define a function div_exact which gives the usual m / k
in the case where k divides m.
-/
def mod_div_aux : ℕ+ → ℕ → ℕ → ℕ+ × ℕ
| k 0 q := ⟨k, q.pred⟩
| k (r + 1) q := ⟨⟨r + 1, nat.succ_pos r⟩, q⟩
lemma mod_div_aux_spec : ∀ (k : ℕ+) (r q : ℕ) (h : ¬ (r = 0 ∧ q = 0)),
(((mod_div_aux k r q).1 : ℕ) + k * (mod_div_aux k r q).2 = (r + k * q))
| k 0 0 h := (h ⟨rfl, rfl⟩).elim
| k 0 (q + 1) h := by {
change (k : ℕ) + (k : ℕ) * (q + 1).pred = 0 + (k : ℕ) * (q + 1),
rw [nat.pred_succ, nat.mul_succ, zero_add, add_comm]}
| k (r + 1) q h := rfl
def mod_div (m k : ℕ+) : ℕ+ × ℕ := mod_div_aux k ((m : ℕ) % (k : ℕ)) ((m : ℕ) / (k : ℕ))
def mod (m k : ℕ+) : ℕ+ := (mod_div m k).1
def div (m k : ℕ+) : ℕ := (mod_div m k).2
theorem mod_add_div (m k : ℕ+) : (m : ℕ) = (mod m k) + k * (div m k) :=
begin
let h₀ := nat.mod_add_div (m : ℕ) (k : ℕ),
have : ¬ ((m : ℕ) % (k : ℕ) = 0 ∧ (m : ℕ) / (k : ℕ) = 0),
by { rintro ⟨hr, hq⟩, rw [hr, hq, mul_zero, zero_add] at h₀,
exact (m.ne_zero h₀.symm).elim },
have := mod_div_aux_spec k ((m : ℕ) % (k : ℕ)) ((m : ℕ) / (k : ℕ)) this,
exact (this.trans h₀).symm,
end
theorem mod_coe (m k : ℕ+) :
((mod m k) : ℕ) = ite ((m : ℕ) % (k : ℕ) = 0) (k : ℕ) ((m : ℕ) % (k : ℕ)) :=
begin
dsimp [mod, mod_div],
cases (m : ℕ) % (k : ℕ),
{ rw [if_pos rfl], refl },
{ rw [if_neg n.succ_ne_zero], refl }
end
theorem div_coe (m k : ℕ+) :
((div m k) : ℕ) = ite ((m : ℕ) % (k : ℕ) = 0) ((m : ℕ) / (k : ℕ)).pred ((m : ℕ) / (k : ℕ)) :=
begin
dsimp [div, mod_div],
cases (m : ℕ) % (k : ℕ),
{ rw [if_pos rfl], refl },
{ rw [if_neg n.succ_ne_zero], refl }
end
theorem mod_le (m k : ℕ+) : mod m k ≤ m ∧ mod m k ≤ k :=
begin
change ((mod m k) : ℕ) ≤ (m : ℕ) ∧ ((mod m k) : ℕ) ≤ (k : ℕ),
rw [mod_coe], split_ifs,
{ have hm : (m : ℕ) > 0 := m.pos,
rw [← nat.mod_add_div (m : ℕ) (k : ℕ), h, zero_add] at hm ⊢,
by_cases h' : ((m : ℕ) / (k : ℕ)) = 0,
{ rw [h', mul_zero] at hm, exact (lt_irrefl _ hm).elim},
{ let h' := nat.mul_le_mul_left (k : ℕ)
(nat.succ_le_of_lt (nat.pos_of_ne_zero h')),
rw [mul_one] at h', exact ⟨h', le_refl (k : ℕ)⟩ } },
{ exact ⟨nat.mod_le (m : ℕ) (k : ℕ), le_of_lt (nat.mod_lt (m : ℕ) k.pos)⟩ }
end
instance : has_dvd ℕ+ := ⟨λ k m, (k : ℕ) ∣ (m : ℕ)⟩
theorem dvd_iff {k m : ℕ+} : k ∣ m ↔ (k : ℕ) ∣ (m : ℕ) := by {refl}
theorem dvd_iff' {k m : ℕ+} : k ∣ m ↔ mod m k = k :=
begin
change (k : ℕ) ∣ (m : ℕ) ↔ mod m k = k,
rw [nat.dvd_iff_mod_eq_zero], split,
{ intro h, apply eq, rw [mod_coe, if_pos h] },
{ intro h, by_cases h' : (m : ℕ) % (k : ℕ) = 0,
{ exact h'},
{ replace h : ((mod m k) : ℕ) = (k : ℕ) := congr_arg _ h,
rw [mod_coe, if_neg h'] at h,
exact (ne_of_lt (nat.mod_lt (m : ℕ) k.pos) h).elim } }
end
def div_exact {m k : ℕ+} (h : k ∣ m) : ℕ+ :=
⟨(div m k).succ, nat.succ_pos _⟩
theorem mul_div_exact {m k : ℕ+} (h : k ∣ m) : k * (div_exact h) = m :=
begin
apply eq, rw [mul_coe],
change (k : ℕ) * (div m k).succ = m,
rw [mod_add_div m k, dvd_iff'.mp h, nat.mul_succ, add_comm],
end
theorem dvd_iff'' {k n : ℕ+} : k ∣ n ↔ ∃ m, k * m = n :=
⟨λ h, ⟨div_exact h, mul_div_exact h⟩,
λ ⟨m, h⟩, dvd.intro (m : ℕ)
((mul_coe k m).symm.trans (congr_arg subtype.val h))⟩
theorem dvd_intro {k n : ℕ+} (m : ℕ+) (h : k * m = n) : k ∣ n :=
dvd_iff''.mpr ⟨m, h⟩
theorem dvd_refl (m : ℕ+) : m ∣ m := dvd_intro 1 (mul_one m)
theorem dvd_antisymm {m n : ℕ+} : m ∣ n → n ∣ m → m = n :=
λ hmn hnm, subtype.eq (nat.dvd_antisymm hmn hnm)
protected theorem dvd_trans {k m n : ℕ+} : k ∣ m → m ∣ n → k ∣ n :=
@dvd_trans ℕ _ (k : ℕ) (m : ℕ) (n : ℕ)
theorem one_dvd (n : ℕ+) : 1 ∣ n := dvd_intro n (one_mul n)
theorem dvd_one_iff (n : ℕ+) : n ∣ 1 ↔ n = 1 :=
⟨λ h, dvd_antisymm h (one_dvd n), λ h, h.symm ▸ (dvd_refl 1)⟩
def gcd (n m : ℕ+) : ℕ+ :=
⟨nat.gcd (n : ℕ) (m : ℕ), nat.gcd_pos_of_pos_left (m : ℕ) n.pos⟩
def lcm (n m : ℕ+) : ℕ+ :=
⟨nat.lcm (n : ℕ) (m : ℕ),
by { let h := mul_pos n.pos m.pos,
rw [← gcd_mul_lcm (n : ℕ) (m : ℕ), mul_comm] at h,
exact pos_of_dvd_of_pos (dvd.intro (nat.gcd (n : ℕ) (m : ℕ)) rfl) h }⟩
@[simp] theorem gcd_coe (n m : ℕ+) : ((gcd n m) : ℕ) = nat.gcd n m := rfl
@[simp] theorem lcm_coe (n m : ℕ+) : ((lcm n m) : ℕ) = nat.lcm n m := rfl
theorem gcd_dvd_left (n m : ℕ+) : (gcd n m) ∣ n := nat.gcd_dvd_left (n : ℕ) (m : ℕ)
theorem gcd_dvd_right (n m : ℕ+) : (gcd n m) ∣ m := nat.gcd_dvd_right (n : ℕ) (m : ℕ)
theorem dvd_gcd {m n k : ℕ+} (hm : k ∣ m) (hn : k ∣ n) : k ∣ gcd m n :=
@nat.dvd_gcd (m : ℕ) (n : ℕ) (k : ℕ) hm hn
theorem dvd_lcm_left (n m : ℕ+) : n ∣ lcm n m := nat.dvd_lcm_left (n : ℕ) (m : ℕ)
theorem dvd_lcm_right (n m : ℕ+) : m ∣ lcm n m := nat.dvd_lcm_right (n : ℕ) (m : ℕ)
theorem lcm_dvd {m n k : ℕ+} (hm : m ∣ k) (hn : n ∣ k) : lcm m n ∣ k :=
@nat.lcm_dvd (m : ℕ) (n : ℕ) (k : ℕ) hm hn
theorem gcd_mul_lcm (n m : ℕ+) : (gcd n m) * (lcm n m) = n * m :=
subtype.eq (nat.gcd_mul_lcm (n : ℕ) (m : ℕ))
def prime (p : ℕ+) : Prop := (p : ℕ).prime
end pnat
|
40579c91407e3707183ac253cae5024affadff6e | 037dba89703a79cd4a4aec5e959818147f97635d | /src/2022/sets/sheet4.lean | ab569653d976ad6f9fdba5602f69b87ad610565b | [] | no_license | ImperialCollegeLondon/M40001_lean | 3a6a09298da395ab51bc220a535035d45bbe919b | 62a76fa92654c855af2b2fc2bef8e60acd16ccec | refs/heads/master | 1,666,750,403,259 | 1,665,771,117,000 | 1,665,771,117,000 | 209,141,835 | 115 | 12 | null | 1,640,270,596,000 | 1,568,749,174,000 | Lean | UTF-8 | Lean | false | false | 1,688 | lean | /-
Copyright (c) 2022 Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author : Kevin Buzzard
-/
import tactic -- imports all the Lean tactics
/-!
# Sets in Lean, example sheet 4 : exists (`∃`)
In this sheet we learn how to manipulate `∃` in Lean. We will see
statements of the form `∃ x, x ∈ A`. We will need to learn two new
tactics -- one for making progress when the goal is `⊢ ∃ x, x ∈ A`,
and one for making progress when we have a hypothesis `h : ∃ x, x ∈ A`.
Now we have `∃` and `∀`, and `∈` and `∉`, we can finally get going
with some harder levels.
## New tactics you will need to know
`cases` or `rcases` -- to get the `x` from a hypothesis `h : ∃ x, ...`
`use` -- to make progress on goals of the form `⊢ ∃ x, ...`
### The `cases` tactic
We've seen this tactic before to take apart `h : P ∧ Q`. We can also
use it to take apart `h : ∃ t : X, F t`: if `h` is such a hypothesis
then `cases h with x hx,` will give us `x : X` and `hx : F x`
### The `use` tactic
If we have a goal `⊢ ∃ x : X, F x` and a term `a : X` which we know
will work, then `use a` will change the goal to `F a`. By the way,
`use` tries `refl` so it might magically close goals early
-/
open set
variables
(X : Type) -- Everything will be a subset of `X`
(A B C D E : set X) -- A,B,C,D,E are subsets of `X`
(x y z : X) -- x,y,z are elements of `X` or, more precisely, terms of type `X`
example : x ∈ A → ∃ t, t ∈ A :=
begin
sorry
end
example : (∀ x, x ∈ A) ↔ ¬ (∃ x, x ∉ A) :=
begin
sorry
end
example : (∃ x, x ∈ A) ↔ ¬ (∀ x, x ∉ A) :=
begin
sorry
end
|
c2aae7573dc51606ff7e6cc576ef38ceddbc1d2b | 94e33a31faa76775069b071adea97e86e218a8ee | /src/data/set/intervals/monotone.lean | fc9b40cb4cc54f09276659ae3166603334f0e014 | [
"Apache-2.0"
] | permissive | urkud/mathlib | eab80095e1b9f1513bfb7f25b4fa82fa4fd02989 | 6379d39e6b5b279df9715f8011369a301b634e41 | refs/heads/master | 1,658,425,342,662 | 1,658,078,703,000 | 1,658,078,703,000 | 186,910,338 | 0 | 0 | Apache-2.0 | 1,568,512,083,000 | 1,557,958,709,000 | Lean | UTF-8 | Lean | false | false | 8,851 | lean | /-
Copyright (c) 2021 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import data.set.intervals.disjoint
import tactic.field_simp
/-!
# Monotonicity on intervals
In this file we prove that a function is (strictly) monotone (or antitone) on a linear order `α`
provided that it is (strictly) monotone on `(-∞, a]` and on `[a, +∞)`. We also provide an order
isomorphism `order_iso_Ioo_neg_one_one` between the open interval `(-1, 1)` in a linear ordered
field and the whole field.
-/
open set
section
variables {α β : Type*} [linear_order α] [preorder β] {a : α} {f : α → β}
/-- If `f` is strictly monotone both on `(-∞, a]` and `[a, ∞)`, then it is strictly monotone on the
whole line. -/
protected lemma strict_mono_on.Iic_union_Ici (h₁ : strict_mono_on f (Iic a))
(h₂ : strict_mono_on f (Ici a)) : strict_mono f :=
begin
intros x y hxy,
cases lt_or_le a x with hax hxa; [skip, cases le_or_lt y a with hya hay],
exacts [h₂ hax.le (hax.trans hxy).le hxy, h₁ hxa hya hxy,
(h₁.monotone_on hxa le_rfl hxa).trans_lt (h₂ le_rfl hay.le hay)]
end
/-- If `f` is strictly antitone both on `(-∞, a]` and `[a, ∞)`, then it is strictly antitone on the
whole line. -/
protected lemma strict_anti_on.Iic_union_Ici (h₁ : strict_anti_on f (Iic a))
(h₂ : strict_anti_on f (Ici a)) : strict_anti f :=
(h₁.dual_right.Iic_union_Ici h₂.dual_right).dual_right
protected lemma monotone_on.Iic_union_Ici (h₁ : monotone_on f (Iic a))
(h₂ : monotone_on f (Ici a)) : monotone f :=
begin
intros x y hxy,
cases le_total x a with hxa hax; [cases le_total y a with hya hay, skip],
exacts [h₁ hxa hya hxy, (h₁ hxa le_rfl hxa).trans (h₂ le_rfl hay hay), h₂ hax (hax.trans hxy) hxy]
end
protected lemma antitone_on.Iic_union_Ici (h₁ : antitone_on f (Iic a))
(h₂ : antitone_on f (Ici a)) : antitone f :=
(h₁.dual_right.Iic_union_Ici h₂.dual_right).dual_right
end
section ordered_group
variables {G H : Type*} [linear_ordered_add_comm_group G] [ordered_add_comm_group H]
lemma strict_mono_of_odd_strict_mono_on_nonneg {f : G → H} (h₁ : ∀ x, f (-x) = -f x)
(h₂ : strict_mono_on f (Ici 0)) :
strict_mono f :=
begin
refine strict_mono_on.Iic_union_Ici (λ x hx y hy hxy, neg_lt_neg_iff.1 _) h₂,
rw [← h₁, ← h₁],
exact h₂ (neg_nonneg.2 hy) (neg_nonneg.2 hx) (neg_lt_neg hxy)
end
lemma monotone_of_odd_of_monotone_on_nonneg {f : G → H} (h₁ : ∀ x, f (-x) = -f x)
(h₂ : monotone_on f (Ici 0)) : monotone f :=
begin
refine monotone_on.Iic_union_Ici (λ x hx y hy hxy, neg_le_neg_iff.1 _) h₂,
rw [← h₁, ← h₁],
exact h₂ (neg_nonneg.2 hy) (neg_nonneg.2 hx) (neg_le_neg hxy)
end
end ordered_group
/-- In a linear ordered field, the whole field is order isomorphic to the open interval `(-1, 1)`.
We consider the actual implementation to be a "black box", so it is irreducible.
-/
@[irreducible] def order_iso_Ioo_neg_one_one (k : Type*) [linear_ordered_field k] :
k ≃o Ioo (-1 : k) 1 :=
begin
refine strict_mono.order_iso_of_right_inverse _ _ (λ x, x / (1 - |x|)) _,
{ refine cod_restrict (λ x, x / (1 + |x|)) _ (λ x, abs_lt.1 _),
have H : 0 < 1 + |x|, from (abs_nonneg x).trans_lt (lt_one_add _),
calc |x / (1 + |x|)| = |x| / (1 + |x|) : by rw [abs_div, abs_of_pos H]
... < 1 : (div_lt_one H).2 (lt_one_add _) },
{ refine (strict_mono_of_odd_strict_mono_on_nonneg _ _).cod_restrict _,
{ intro x, simp only [abs_neg, neg_div] },
{ rintros x (hx : 0 ≤ x) y (hy : 0 ≤ y) hxy,
simp [abs_of_nonneg, mul_add, mul_comm x y, div_lt_div_iff,
hx.trans_lt (lt_one_add _), hy.trans_lt (lt_one_add _), *] } },
{ refine λ x, subtype.ext _,
have : 0 < 1 - |(x : k)|, from sub_pos.2 (abs_lt.2 x.2),
field_simp [abs_div, this.ne', abs_of_pos this] }
end
section Ixx
variables {α β : Type*} [preorder α] [preorder β] {f g : α → β} {s : set α}
lemma antitone_Ici : antitone (Ici : α → set α) := λ _ _, Ici_subset_Ici.2
lemma monotone_Iic : monotone (Iic : α → set α) := λ _ _, Iic_subset_Iic.2
lemma antitone_Ioi : antitone (Ioi : α → set α) := λ _ _, Ioi_subset_Ioi
lemma monotone_Iio : monotone (Iio : α → set α) := λ _ _, Iio_subset_Iio
protected lemma monotone.Ici (hf : monotone f) : antitone (λ x, Ici (f x)) :=
antitone_Ici.comp_monotone hf
protected lemma monotone_on.Ici (hf : monotone_on f s) : antitone_on (λ x, Ici (f x)) s :=
antitone_Ici.comp_monotone_on hf
protected lemma antitone.Ici (hf : antitone f) : monotone (λ x, Ici (f x)) :=
antitone_Ici.comp hf
protected lemma antitone_on.Ici (hf : antitone_on f s) : monotone_on (λ x, Ici (f x)) s :=
antitone_Ici.comp_antitone_on hf
protected lemma monotone.Iic (hf : monotone f) : monotone (λ x, Iic (f x)) :=
monotone_Iic.comp hf
protected lemma monotone_on.Iic (hf : monotone_on f s) : monotone_on (λ x, Iic (f x)) s :=
monotone_Iic.comp_monotone_on hf
protected lemma antitone.Iic (hf : antitone f) : antitone (λ x, Iic (f x)) :=
monotone_Iic.comp_antitone hf
protected lemma antitone_on.Iic (hf : antitone_on f s) : antitone_on (λ x, Iic (f x)) s :=
monotone_Iic.comp_antitone_on hf
protected lemma monotone.Ioi (hf : monotone f) : antitone (λ x, Ioi (f x)) :=
antitone_Ioi.comp_monotone hf
protected lemma monotone_on.Ioi (hf : monotone_on f s) : antitone_on (λ x, Ioi (f x)) s :=
antitone_Ioi.comp_monotone_on hf
protected lemma antitone.Ioi (hf : antitone f) : monotone (λ x, Ioi (f x)) :=
antitone_Ioi.comp hf
protected lemma antitone_on.Ioi (hf : antitone_on f s) : monotone_on (λ x, Ioi (f x)) s :=
antitone_Ioi.comp_antitone_on hf
protected lemma monotone.Iio (hf : monotone f) : monotone (λ x, Iio (f x)) :=
monotone_Iio.comp hf
protected lemma monotone_on.Iio (hf : monotone_on f s) : monotone_on (λ x, Iio (f x)) s :=
monotone_Iio.comp_monotone_on hf
protected lemma antitone.Iio (hf : antitone f) : antitone (λ x, Iio (f x)) :=
monotone_Iio.comp_antitone hf
protected lemma antitone_on.Iio (hf : antitone_on f s) : antitone_on (λ x, Iio (f x)) s :=
monotone_Iio.comp_antitone_on hf
protected lemma monotone.Icc (hf : monotone f) (hg : antitone g) :
antitone (λ x, Icc (f x) (g x)) :=
hf.Ici.inter hg.Iic
protected lemma monotone_on.Icc (hf : monotone_on f s) (hg : antitone_on g s) :
antitone_on (λ x, Icc (f x) (g x)) s :=
hf.Ici.inter hg.Iic
protected lemma antitone.Icc (hf : antitone f) (hg : monotone g) :
monotone (λ x, Icc (f x) (g x)) :=
hf.Ici.inter hg.Iic
protected lemma antitone_on.Icc (hf : antitone_on f s) (hg : monotone_on g s) :
monotone_on (λ x, Icc (f x) (g x)) s :=
hf.Ici.inter hg.Iic
protected lemma monotone.Ico (hf : monotone f) (hg : antitone g) :
antitone (λ x, Ico (f x) (g x)) :=
hf.Ici.inter hg.Iio
protected lemma monotone_on.Ico (hf : monotone_on f s) (hg : antitone_on g s) :
antitone_on (λ x, Ico (f x) (g x)) s :=
hf.Ici.inter hg.Iio
protected lemma antitone.Ico (hf : antitone f) (hg : monotone g) :
monotone (λ x, Ico (f x) (g x)) :=
hf.Ici.inter hg.Iio
protected lemma antitone_on.Ico (hf : antitone_on f s) (hg : monotone_on g s) :
monotone_on (λ x, Ico (f x) (g x)) s :=
hf.Ici.inter hg.Iio
protected lemma monotone.Ioc (hf : monotone f) (hg : antitone g) :
antitone (λ x, Ioc (f x) (g x)) :=
hf.Ioi.inter hg.Iic
protected lemma monotone_on.Ioc (hf : monotone_on f s) (hg : antitone_on g s) :
antitone_on (λ x, Ioc (f x) (g x)) s :=
hf.Ioi.inter hg.Iic
protected lemma antitone.Ioc (hf : antitone f) (hg : monotone g) :
monotone (λ x, Ioc (f x) (g x)) :=
hf.Ioi.inter hg.Iic
protected lemma antitone_on.Ioc (hf : antitone_on f s) (hg : monotone_on g s) :
monotone_on (λ x, Ioc (f x) (g x)) s :=
hf.Ioi.inter hg.Iic
protected lemma monotone.Ioo (hf : monotone f) (hg : antitone g) :
antitone (λ x, Ioo (f x) (g x)) :=
hf.Ioi.inter hg.Iio
protected lemma monotone_on.Ioo (hf : monotone_on f s) (hg : antitone_on g s) :
antitone_on (λ x, Ioo (f x) (g x)) s :=
hf.Ioi.inter hg.Iio
protected lemma antitone.Ioo (hf : antitone f) (hg : monotone g) :
monotone (λ x, Ioo (f x) (g x)) :=
hf.Ioi.inter hg.Iio
protected lemma antitone_on.Ioo (hf : antitone_on f s) (hg : monotone_on g s) :
monotone_on (λ x, Ioo (f x) (g x)) s :=
hf.Ioi.inter hg.Iio
end Ixx
section Union
variables {α β : Type*} [semilattice_sup α] [linear_order β] {f g : α → β} {a b : β}
lemma Union_Ioo_of_mono_of_is_glb_of_is_lub (hf : antitone f) (hg : monotone g)
(ha : is_glb (range f) a) (hb : is_lub (range g) b) :
(⋃ x, Ioo (f x) (g x)) = Ioo a b :=
calc (⋃ x, Ioo (f x) (g x)) = (⋃ x, Ioi (f x)) ∩ ⋃ x, Iio (g x) :
Union_inter_of_monotone hf.Ioi hg.Iio
... = Ioi a ∩ Iio b : congr_arg2 (∩) ha.Union_Ioi_eq hb.Union_Iio_eq
end Union
|
ffe757608876d82c50a133e1e97584afa6742175 | bb31430994044506fa42fd667e2d556327e18dfe | /src/algebra/star/pi.lean | 3aafeb402df3490d11b440507bbfc1a3d9833dc7 | [
"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 | 2,379 | lean | /-
Copyright (c) 2021 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import algebra.star.basic
import algebra.ring.pi
/-!
# `star` on pi types
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
We put a `has_star` structure on pi types that operates elementwise, such that it describes the
complex conjugation of vectors.
-/
universes u v w
variable {I : Type u} -- The indexing type
variable {f : I → Type v} -- The family of types already equipped with instances
namespace pi
instance [Π i, has_star (f i)] : has_star (Π i, f i) :=
{ star := λ x i, star (x i) }
@[simp] lemma star_apply [Π i, has_star (f i)] (x : Π i, f i) (i : I) : star x i = star (x i) := rfl
lemma star_def [Π i, has_star (f i)] (x : Π i, f i) : star x = λ i, star (x i) := rfl
instance [Π i, has_involutive_star (f i)] : has_involutive_star (Π i, f i) :=
{ star_involutive := λ _, funext $ λ _, star_star _ }
instance [Π i, semigroup (f i)] [Π i, star_semigroup (f i)] : star_semigroup (Π i, f i) :=
{ star_mul := λ _ _, funext $ λ _, star_mul _ _ }
instance [Π i, add_monoid (f i)] [Π i, star_add_monoid (f i)] : star_add_monoid (Π i, f i) :=
{ star_add := λ _ _, funext $ λ _, star_add _ _ }
instance [Π i, non_unital_semiring (f i)] [Π i, star_ring (f i)] : star_ring (Π i, f i) :=
{ ..pi.star_add_monoid, ..(pi.star_semigroup : star_semigroup (Π i, f i)) }
instance {R : Type w}
[Π i, has_smul R (f i)] [has_star R] [Π i, has_star (f i)] [Π i, star_module R (f i)] :
star_module R (Π i, f i) :=
{ star_smul := λ r x, funext $ λ i, star_smul r (x i) }
lemma single_star [Π i, add_monoid (f i)] [Π i, star_add_monoid (f i)] [decidable_eq I]
(i : I) (a : f i) :
pi.single i (star a) = star (pi.single i a) :=
single_op (λ i, @star (f i) _) (λ i, star_zero _) i a
end pi
namespace function
lemma update_star [Π i, has_star (f i)] [decidable_eq I]
(h : Π (i : I), f i) (i : I) (a : f i) :
function.update (star h) i (star a) = star (function.update h i a) :=
funext $ λ j, (apply_update (λ i, star) h i a j).symm
lemma star_sum_elim {I J α : Type*} (x : I → α) (y : J → α) [has_star α] :
star (sum.elim x y) = sum.elim (star x) (star y) :=
by { ext x, cases x; simp }
end function
|
f683df01f0670bf904e4252d45f4d2c1578555d6 | 3af272061d36e7f3f0521cceaa3a847ed4e03af9 | /src/computable_matrix.lean | 274e9c79b261ffb2dc6e2e6cb16ca53701cb6385 | [] | no_license | semorrison/kbb | fdab0929d21dca880d835081814225a95f946187 | 229bd06e840bc7a7438b8fee6802a4f8024419e3 | refs/heads/master | 1,585,351,834,355 | 1,539,848,241,000 | 1,539,848,241,000 | 147,323,315 | 2 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 12,749 | lean | /-
A faster, computable version of matrices implemented entirely in terms of d_array,
trying to rely on fast pre-existing array and d_array functions as much as possible.
A slightly more "serious" version of fast_matrix (?)
Still a WIP.
The second half of the document is a bit of a mess.
-/
import data.vector
import data.list.basic
import .matrices
universes u v
def computable_d_matrix (m n : ℕ) (α : fin m → fin n → Type u) := d_array m (λ j, d_array n (α j))
namespace computable_d_matrix
variables {m n : ℕ} {α : fin m → fin n → Type u} {β : Type v}
def nil {α} : computable_d_matrix 0 0 α :=
⟨λ i, absurd (fin.is_lt i) (nat.not_lt_zero (fin.val i))⟩
def read (a : computable_d_matrix m n α) (i : fin m) (j : fin n) : α i j :=
d_array.read (d_array.read a i) j
def read_row (a : computable_d_matrix m n α) (i : fin m) : d_array n (α i) :=
d_array.read a i
def read_col (a : computable_d_matrix m n α) (j : fin n) : d_array m (λ i, α i j) :=
⟨λ i, d_array.read (d_array.read a i) j⟩
def transpose (a : computable_d_matrix m n α) : computable_d_matrix n m (λ (j : fin n) (i : fin m), α i j) :=
⟨λ j, ⟨λ i, a.read i j⟩⟩ -- TODO: This can probably be optimised
def write (a : computable_d_matrix m n α) (i : fin m) (j : fin n) (v : α i j) : computable_d_matrix m n α :=
d_array.write a i (d_array.write (d_array.read a i) j v)
def write_row (a : computable_d_matrix m n α) (i : fin m) (v : d_array n (α i)) : computable_d_matrix m n α :=
d_array.write a i v
def write_col (a : computable_d_matrix m n α) (j : fin n) (v : d_array m (λ i, α i j)) : computable_d_matrix m n α :=
transpose (d_array.write (transpose a) j v) -- TODO: This can probably be optimised, by not relying on transpose
def iterate (a : computable_d_matrix m n α) (b : β) (f : Π (i : fin m) (j : fin n), α i j → β → β) : β :=
d_array.iterate a b (λ i arr state, d_array.iterate arr state (λ j, f i j))
def iterate_row (a : computable_d_matrix m n α) (b : β) (f : Π (i : fin m), (d_array n (α i)) → β → β) : β :=
d_array.iterate a b f
def iterate_col (a : computable_d_matrix m n α) (b : β) (f : Π (j : fin n), d_array m (λ i, α i j) → β → β) : β :=
d_array.iterate (transpose a) b f -- TODO: this can probably be optimised, by not relying on transpose
def foreach (a : computable_d_matrix m n α) (f : Π (i : fin m) (j : fin n), α i j → α i j) : computable_d_matrix m n α :=
iterate a a $ λ i j v a', a'.write i j (f i j v)
def map (f : Π (i : fin m) (j : fin n), α i j → α i j) (a : computable_d_matrix m n α) : computable_d_matrix m n α :=
foreach a f
def map₂ (f : Π (i : fin m) (j : fin n), α i j → α i j → α i j) (a b : computable_d_matrix m n α) : computable_d_matrix m n α :=
foreach b (λ i j, f i j (a.read i j))
def foldl (a : computable_d_matrix m n α) (b : β) (f : Π (i : fin m) (j : fin n), α i j → β → β) : β :=
iterate a b f
@[simp] lemma read_write (a : computable_d_matrix m n α) (i : fin m) (j : fin n) (v : α i j) : read (write a i j v) i j = v :=
by simp [read, write]
@[simp] lemma read_write_row (a : computable_d_matrix m n α) (i : fin m) (v : d_array n (α i)) : read_row (write_row a i v) i = v :=
by simp [read_row, write_row]
-- TODO: This one
-- @[simp] lemma read_write_col (a : computable_d_matrix m n α) (j : fin n) (v : d_array m (λ i, α i j)) : read_col (write_col a j v) j = v :=
-- by simp [read_col, write_col, transpose, d_array.write, d_array.read, read, write, read_row, write_row]
@[simp] lemma read_write_of_ne (a : computable_d_matrix m n α) {i j : fin m} {k l : fin n} (v : α i k) : i ≠ j → k ≠ l → (read (write a i k v) j l) = (read a j l):=
by intros h₁ h₂; simp [read, write, h₁, h₂]
protected lemma ext {a b : computable_d_matrix m n α} (h : ∀ i j, read a i j = read b i j) : a = b :=
by cases a; cases b; congr; funext i; exact d_array.ext (h i)
protected lemma ext' {a b : computable_d_matrix m n α} (h : ∀ (i j : nat) (h₁ : i < m) (h₂ : j < n), read a ⟨i, h₁⟩ ⟨j, h₂⟩ = read b ⟨i, h₁⟩ ⟨j, h₂⟩) : a = b :=
begin -- TODO: Clean this up
cases a,
cases b,
congr,
funext i,
apply d_array.ext,
intros j,
have hi : i = ⟨i.val, (fin.is_lt i)⟩, by simp,
have hj : j = ⟨j.val, (fin.is_lt j)⟩, by simp,
rw hi,
rw hj,
from (h (fin.val i) (fin.val j) (fin.is_lt i) (fin.is_lt j)),
end
protected def beq_aux [∀ i j, decidable_eq (α i j)] (a b : computable_d_matrix m n α) : Π (i : nat), i ≤ m → bool :=
@d_array.beq_aux _ _ _ a b
protected def beq [∀ i j, decidable_eq (α i j)] (a b : computable_d_matrix m n α) : bool :=
@d_array.beq_aux _ _ _ a b _ (le_refl m)
instance [∀ i j, decidable_eq (α i j)] : decidable_eq (computable_d_matrix m n α) :=
d_array.decidable_eq
end computable_d_matrix
def computable_matrix (m n : ℕ) (α : Type u) := computable_d_matrix m n (λ i j, α)
namespace computable_matrix
variables {m n : nat} {α : Type u} {β : Type v}
def nil : computable_matrix 0 0 α :=
computable_d_matrix.nil
def read (a : computable_matrix m n α) (i : fin m) (j : fin n) : α :=
computable_d_matrix.read a i j
def read_row (a : computable_matrix m n α) (i : fin m) : array n α :=
computable_d_matrix.read_row a i
def read_col (a : computable_matrix m n α) (j : fin n) : array m α :=
computable_d_matrix.read_col a j
def transpose (a : computable_matrix m n α) : computable_matrix n m α :=
computable_d_matrix.transpose a
def write (a : computable_matrix m n α) (i : fin m) (j : fin n) (v : α) : computable_matrix m n α :=
computable_d_matrix.write a i j v
def write_row (a : computable_matrix m n α) (i : fin m) (v : array n α) : computable_matrix m n α :=
computable_d_matrix.write_row a i v
def write_col (a : computable_matrix m n α) (j : fin n) (v : array m α) : computable_matrix m n α :=
computable_d_matrix.write_col a j v
def iterate_row (a : computable_matrix m n α) (b : β) (f : fin m → array n α → β → β) : β :=
computable_d_matrix.iterate_row a b (λ i V₁, f i ⟨V₁.data⟩)
def iterate_col (a : computable_matrix m n α) (b : β) (f : fin n → array m α → β → β) : β :=
computable_d_matrix.iterate_col a b (λ i V₁, f i ⟨V₁.data⟩)
def iterate (a : computable_matrix m n α) (b : β) (f : fin m → fin n → α → β → β) : β :=
computable_d_matrix.iterate a b (λ i j v, f i j v)
def foreach (a : computable_matrix m n α) (f : fin m → fin n → α → α) : computable_matrix m n α :=
iterate a a (λ i j v a', a'.write i j (f i j v))
def map (f : α → α) (a : computable_matrix m n α) : computable_matrix m n α :=
foreach a (λ _ _, f)
def map₂ (f : α → α → α) (a b : computable_matrix m n α) : computable_matrix m n α :=
foreach b (λ i j, f (a.read i j))
def foldl_row (a : computable_matrix m n α) (b : β) (f : (array n α) → β → β) : β :=
iterate_row a b (λ _, f)
def foldl_col (a : computable_matrix m n α) (b : β) (f : (array m α) → β → β) : β :=
iterate_col a b (λ _, f)
def rev_list_row (a : computable_matrix m n α) : list (array n α) :=
a.foldl_row [] (::)
def rev_list_col (a : computable_matrix m n α) : list (array m α) :=
a.foldl_col [] (::)
@[simp] lemma read_write (a : computable_matrix m n α) (i : fin m) (j : fin n) (v : α) : read (write a i j v) i j = v :=
by simp [read, write]
@[simp] lemma read_write_of_ne (a : computable_matrix m n α) {i j : fin m} {k l : fin n} (v : α) : i ≠ j → k ≠ l → (read (write a i k v) j l) = (read a j l):=
by intros h₁ h₂; simp [read, write, h₁, h₂]
protected lemma ext {a b : computable_matrix m n α} (h : ∀ i j, read a i j = read b i j) : a = b :=
by cases a; cases b; congr; funext i; exact array.ext (h i)
protected lemma ext' {a b : computable_matrix m n α} (h : ∀ (i j : nat) (h₁ : i < m) (h₂ : j < n), read a ⟨i, h₁⟩ ⟨j, h₂⟩ = read b ⟨i, h₁⟩ ⟨j, h₂⟩) : a = b :=
begin -- TODO: Golf this one
cases a,
cases b,
congr,
funext i,
apply array.ext,
intros j,
have hi : i = ⟨i.val, (fin.is_lt i)⟩, by simp,
have hj : j = ⟨j.val, (fin.is_lt j)⟩, by simp,
rw hi,
rw hj,
from (h (fin.val i) (fin.val j) (fin.is_lt i) (fin.is_lt j)),
end
protected def beq [decidable_eq α] (a b : computable_matrix m n α) : bool :=
@computable_d_matrix.beq m n (λ i j, α) _ a b
instance [decidable_eq α] : decidable_eq (computable_matrix m n α) :=
computable_d_matrix.decidable_eq
def to_list (a : computable_matrix m n α) : list (list (α)) :=
iterate_row a [] (λ i v b, list.concat b (array.to_list v))
instance repr [has_repr α] : has_repr (computable_matrix m n α) :=
⟨λ a, list.repr (to_list a)⟩
-- Matrix row operations. Not sure if it's actually worth having these implemented like this here.
def exchange_rows (a : computable_matrix m n α) (i₁ i₂ : fin m) : computable_matrix m n α :=
⟨λ i, if h₁ : i₁ = i then eq.rec_on h₁ (a.read_row i₂) else if h₂ : i₂ = i then eq.rec_on h₂ (a.read_row i₁) else a.read_row i⟩
def exchange_cols (a : computable_matrix m n α) (j₁ j₂ : fin n) : computable_matrix m n α :=
(computable_matrix.exchange_rows (a.transpose) j₁ j₂).transpose -- TODO: Can probably be optimised without transpose
def add_multiple_of_row_to_row (a : computable_matrix m n α) [has_add α] [has_mul α] (i₁ : fin m) (s : α) (i₂ : fin m) : computable_matrix m n α :=
computable_matrix.write_row a i₁ (array.map₂ (has_add.add) (a.read_row i₁) (array.map (has_mul.mul s) (a.read_row i₂)))
def add_multiple_of_col_to_col (a : computable_matrix m n α) [has_add α] [has_mul α] (j₁ : fin n) (s : α) (j₂ : fin n) : computable_matrix m n α :=
(add_multiple_of_row_to_row (a.transpose) j₁ s j₂).transpose -- TODO: Can probably be optimised without transpose
end computable_matrix
-- A number of these coercions are probably completely unncessary.
-- I think I probably went more than a bit overboard. Will probably remove some later.
def array_of_vector {α : Type u} {n : ℕ} : vector α n → array n α :=
λ v, ⟨λ i, vector.nth v i⟩
instance coe_array_of_vector {α : Type u} {n : ℕ} : has_coe (vector α n) (array n α) :=
⟨array_of_vector⟩
def vector_of_array {α : Type u} {n : ℕ} : array n α → vector α n :=
λ a, ⟨list.reverse (array.to_list a), by simp ⟩
instance coe_vector_of_array {α : Type u} {n : ℕ} : has_coe (array n α) (vector α n) :=
⟨vector_of_array⟩
-- TODO: These coes are unrelated and probably shouldn't be here.
instance coe_matrix_of_array_of_array {α : Type u} {m n : ℕ} : has_coe (array m (array n α)) (computable_matrix m n α) :=
⟨λ M, M⟩
instance coe_distributes_over_list {α β : Type u} [has_coe α β] : has_coe (list α) (list β) :=
⟨λ l, l.map coe⟩
instance coe_distributes_over_array {α β : Type u} {n : ℕ} [has_coe α β] : has_coe (array n α) (array n β) :=
⟨λ a, ⟨λ i, a.read i⟩⟩
instance coe_distributes_over_vector {α β : Type u} {n : ℕ} [has_coe α β] : has_coe (vector α n) (vector β n) :=
⟨λv, v.map coe⟩
-----------------------------------------------------------------
instance coe_matrix_of_vector_of_vector {α : Type u} {m n : ℕ} : has_coe (vector (vector α n) m) (computable_matrix m n α) :=
begin
constructor,
intros M,
unfold computable_matrix,
unfold computable_d_matrix,
apply coe_array_of_vector.coe,
apply coe_distributes_over_vector.coe,
from M,
from coe_array_of_vector,
end -- Todo: Golf this
instance coe_distributes_over_computable_matrix {m n : ℕ} {α β : Type u} [has_coe α β] : has_coe (computable_matrix m n α) (computable_matrix m n β) :=
⟨λ M, ⟨λ i, ⟨λ j, M.read i j⟩⟩⟩
-- Notation
def array.mk {α : Type*} {n : ℕ} (l : list α) (pr : l.length = n) : array n α :=
array_of_vector ⟨l, pr⟩
notation `![` l:(foldr `, ` (h t, list.cons h t) list.nil `]`) :=
array.mk l (refl _)
def test1 : computable_matrix 2 3 ℤ := -- Just like our old friend fast_matrix!
![![ 1 , 1, 5 ],
![ 0 , 1, 2 ]]
#eval test1 -- Reads the right value!
|
4f95374a23d0daa2b93003427d6016ecd93cb254 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/analysis/calculus/times_cont_diff_auto.lean | e35a4f5178b01e07ee3cd11585d9a6b395c90abf | [] | 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 | 143,642 | 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.analysis.calculus.mean_value
import Mathlib.analysis.calculus.formal_multilinear_series
import Mathlib.PostPort
universes u_1 u_2 u_3 l u_4 u u_5 u_6 u_7
namespace Mathlib
/-!
# Higher differentiability
A function is `C^1` on a domain if it is differentiable there, and its derivative is continuous.
By induction, it is `C^n` if it is `C^{n-1}` and its (n-1)-th derivative is `C^1` there or,
equivalently, if it is `C^1` and its derivative is `C^{n-1}`.
Finally, it is `C^∞` if it is `C^n` for all n.
We formalize these notions by defining iteratively the `n+1`-th derivative of a function as the
derivative of the `n`-th derivative. It is called `iterated_fderiv 𝕜 n f x` where `𝕜` is the
field, `n` is the number of iterations, `f` is the function and `x` is the point, and it is given
as an `n`-multilinear map. We also define a version `iterated_fderiv_within` relative to a domain,
as well as predicates `times_cont_diff_within_at`, `times_cont_diff_at`, `times_cont_diff_on` and
`times_cont_diff` saying that the function is `C^n` within a set at a point, at a point, on a set
and on the whole space respectively.
To avoid the issue of choice when choosing a derivative in sets where the derivative is not
necessarily unique, `times_cont_diff_on` is not defined directly in terms of the
regularity of the specific choice `iterated_fderiv_within 𝕜 n f s` inside `s`, but in terms of the
existence of a nice sequence of derivatives, expressed with a predicate
`has_ftaylor_series_up_to_on`.
We prove basic properties of these notions.
## Main definitions and results
Let `f : E → F` be a map between normed vector spaces over a nondiscrete normed field `𝕜`.
* `has_ftaylor_series_up_to n f p`: expresses that the formal multilinear series `p` is a sequence
of iterated derivatives of `f`, up to the `n`-th term (where `n` is a natural number or `∞`).
* `has_ftaylor_series_up_to_on n f p s`: same thing, but inside a set `s`. The notion of derivative
is now taken inside `s`. In particular, derivatives don't have to be unique.
* `times_cont_diff 𝕜 n f`: expresses that `f` is `C^n`, i.e., it admits a Taylor series up to
rank `n`.
* `times_cont_diff_on 𝕜 n f s`: expresses that `f` is `C^n` in `s`.
* `times_cont_diff_at 𝕜 n f x`: expresses that `f` is `C^n` around `x`.
* `times_cont_diff_within_at 𝕜 n f s x`: expresses that `f` is `C^n` around `x` within the set `s`.
* `iterated_fderiv_within 𝕜 n f s x` is an `n`-th derivative of `f` over the field `𝕜` on the
set `s` at the point `x`. It is a continuous multilinear map from `E^n` to `F`, defined as a
derivative within `s` of `iterated_fderiv_within 𝕜 (n-1) f s` if one exists, and `0` otherwise.
* `iterated_fderiv 𝕜 n f x` is the `n`-th derivative of `f` over the field `𝕜` at the point `x`.
It is a continuous multilinear map from `E^n` to `F`, defined as a derivative of
`iterated_fderiv 𝕜 (n-1) f` if one exists, and `0` otherwise.
In sets of unique differentiability, `times_cont_diff_on 𝕜 n f s` can be expressed in terms of the
properties of `iterated_fderiv_within 𝕜 m f s` for `m ≤ n`. In the whole space,
`times_cont_diff 𝕜 n f` can be expressed in terms of the properties of `iterated_fderiv 𝕜 m f`
for `m ≤ n`.
We also prove that the usual operations (addition, multiplication, difference, composition, and
so on) preserve `C^n` functions.
## Implementation notes
The definitions in this file are designed to work on any field `𝕜`. They are sometimes slightly more
complicated than the naive definitions one would guess from the intuition over the real or complex
numbers, but they are designed to circumvent the lack of gluing properties and partitions of unity
in general. In the usual situations, they coincide with the usual definitions.
### Definition of `C^n` functions in domains
One could define `C^n` functions in a domain `s` by fixing an arbitrary choice of derivatives (this
is what we do with `iterated_fderiv_within`) and requiring that all these derivatives up to `n` are
continuous. If the derivative is not unique, this could lead to strange behavior like two `C^n`
functions `f` and `g` on `s` whose sum is not `C^n`. A better definition is thus to say that a
function is `C^n` inside `s` if it admits a sequence of derivatives up to `n` inside `s`.
This definition still has the problem that a function which is locally `C^n` would not need to
be `C^n`, as different choices of sequences of derivatives around different points might possibly
not be glued together to give a globally defined sequence of derivatives. (Note that this issue
can not happen over reals, thanks to partition of unity, but the behavior over a general field is
not so clear, and we want a definition for general fields). Also, there are locality
problems for the order parameter: one could image a function which, for each `n`, has a nice
sequence of derivatives up to order `n`, but they do not coincide for varying `n` and can therefore
not be glued to give rise to an infinite sequence of derivatives. This would give a function
which is `C^n` for all `n`, but not `C^∞`. We solve this issue by putting locality conditions
in space and order in our definition of `times_cont_diff_within_at` and `times_cont_diff_on`.
The resulting definition is slightly more complicated to work with (in fact not so much), but it
gives rise to completely satisfactory theorems.
For instance, with this definition, a real function which is `C^m` (but not better) on `(-1/m, 1/m)`
for each natural `m` is by definition `C^∞` at `0`.
There is another issue with the definition of `times_cont_diff_within_at 𝕜 n f s x`. We can
require the existence and good behavior of derivatives up to order `n` on a neighborhood of `x`
within `s`. However, this does not imply continuity or differentiability within `s` of the function
at `x` when `x` does not belong to `s`. Therefore, we require such existence and good behavior on
a neighborhood of `x` within `s ∪ {x}` (which appears as `insert x s` in this file).
### Side of the composition, and universe issues
With a naïve direct definition, the `n`-th derivative of a function belongs to the space
`E →L[𝕜] (E →L[𝕜] (E ... F)...)))` where there are n iterations of `E →L[𝕜]`. This space
may also be seen as the space of continuous multilinear functions on `n` copies of `E` with
values in `F`, by uncurrying. This is the point of view that is usually adopted in textbooks,
and that we also use. This means that the definition and the first proofs are slightly involved,
as one has to keep track of the uncurrying operation. The uncurrying can be done from the
left or from the right, amounting to defining the `n+1`-th derivative either as the derivative of
the `n`-th derivative, or as the `n`-th derivative of the derivative.
For proofs, it would be more convenient to use the latter approach (from the right),
as it means to prove things at the `n+1`-th step we only need to understand well enough the
derivative in `E →L[𝕜] F` (contrary to the approach from the left, where one would need to know
enough on the `n`-th derivative to deduce things on the `n+1`-th derivative).
However, the definition from the right leads to a universe polymorphism problem: if we define
`iterated_fderiv 𝕜 (n + 1) f x = iterated_fderiv 𝕜 n (fderiv 𝕜 f) x` by induction, we need to
generalize over all spaces (as `f` and `fderiv 𝕜 f` don't take values in the same space). It is
only possible to generalize over all spaces in some fixed universe in an inductive definition.
For `f : E → F`, then `fderiv 𝕜 f` is a map `E → (E →L[𝕜] F)`. Therefore, the definition will only
work if `F` and `E →L[𝕜] F` are in the same universe.
This issue does not appear with the definition from the left, where one does not need to generalize
over all spaces. Therefore, we use the definition from the left. This means some proofs later on
become a little bit more complicated: to prove that a function is `C^n`, the most efficient approach
is to exhibit a formula for its `n`-th derivative and prove it is continuous (contrary to the
inductive approach where one would prove smoothness statements without giving a formula for the
derivative). In the end, this approach is still satisfactory as it is good to have formulas for the
iterated derivatives in various constructions.
One point where we depart from this explicit approach is in the proof of smoothness of a
composition: there is a formula for the `n`-th derivative of a composition (Faà di Bruno's formula),
but it is very complicated and barely usable, while the inductive proof is very simple. Thus, we
give the inductive proof. As explained above, it works by generalizing over the target space, hence
it only works well if all spaces belong to the same universe. To get the general version, we lift
things to a common universe using a trick.
### Variables management
The textbook definitions and proofs use various identifications and abuse of notations, for instance
when saying that the natural space in which the derivative lives, i.e.,
`E →L[𝕜] (E →L[𝕜] ( ... →L[𝕜] F))`, is the same as a space of multilinear maps. When doing things
formally, we need to provide explicit maps for these identifications, and chase some diagrams to see
everything is compatible with the identifications. In particular, one needs to check that taking the
derivative and then doing the identification, or first doing the identification and then taking the
derivative, gives the same result. The key point for this is that taking the derivative commutes
with continuous linear equivalences. Therefore, we need to implement all our identifications with
continuous linear equivs.
## Notations
We use the notation `E [×n]→L[𝕜] F` for the space of continuous multilinear maps on `E^n` with
values in `F`. This is the space in which the `n`-th derivative of a function from `E` to `F` lives.
In this file, we denote `⊤ : with_top ℕ` with `∞`.
## Tags
derivative, differentiability, higher derivative, `C^n`, multilinear, Taylor series, formal series
-/
/-! ### Functions with a Taylor series on a domain -/
/-- `has_ftaylor_series_up_to_on n f p s` registers the fact that `p 0 = f` and `p (m+1)` is a
derivative of `p m` for `m < n`, and is continuous for `m ≤ n`. This is a predicate analogous to
`has_fderiv_within_at` but for higher order derivatives. -/
structure has_ftaylor_series_up_to_on {𝕜 : 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]
(n : with_top ℕ) (f : E → F) (p : E → formal_multilinear_series 𝕜 E F) (s : set E)
where
zero_eq : ∀ (x : E), x ∈ s → continuous_multilinear_map.uncurry0 (p x 0) = f x
fderiv_within :
∀ (m : ℕ),
↑m < n →
∀ (x : E),
x ∈ s →
has_fderiv_within_at (fun (y : E) => p y m)
(continuous_multilinear_map.curry_left (p x (Nat.succ m))) s x
cont : ∀ (m : ℕ), ↑m ≤ n → continuous_on (fun (x : E) => p x m) s
theorem has_ftaylor_series_up_to_on.zero_eq' {𝕜 : 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] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F}
{n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) {x : E} (hx : x ∈ s) :
p x 0 = coe_fn (continuous_linear_equiv.symm (continuous_multilinear_curry_fin0 𝕜 E F)) (f x) :=
sorry
/-- If two functions coincide on a set `s`, then a Taylor series for the first one is as well a
Taylor series for the second one. -/
theorem has_ftaylor_series_up_to_on.congr {𝕜 : 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]
{s : set E} {f : E → F} {f₁ : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ}
(h : has_ftaylor_series_up_to_on n f p s) (h₁ : ∀ (x : E), x ∈ s → f₁ x = f x) :
has_ftaylor_series_up_to_on n f₁ p s :=
sorry
theorem has_ftaylor_series_up_to_on.mono {𝕜 : 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]
{s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ}
(h : has_ftaylor_series_up_to_on n f p s) {t : set E} (hst : t ⊆ s) :
has_ftaylor_series_up_to_on n f p t :=
sorry
theorem has_ftaylor_series_up_to_on.of_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]
{s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {m : with_top ℕ}
{n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hmn : m ≤ n) :
has_ftaylor_series_up_to_on m f p s :=
sorry
theorem has_ftaylor_series_up_to_on.continuous_on {𝕜 : 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] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F}
{n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) : continuous_on f s :=
sorry
theorem has_ftaylor_series_up_to_on_zero_iff {𝕜 : 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] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} :
has_ftaylor_series_up_to_on 0 f p s ↔
continuous_on f s ∧ ∀ (x : E), x ∈ s → continuous_multilinear_map.uncurry0 (p x 0) = f x :=
sorry
theorem has_ftaylor_series_up_to_on_top_iff {𝕜 : 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] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} :
has_ftaylor_series_up_to_on ⊤ f p s ↔ ∀ (n : ℕ), has_ftaylor_series_up_to_on (↑n) f p s :=
sorry
/-- If a function has a Taylor series at order at least `1`, then the term of order `1` of this
series is a derivative of `f`. -/
theorem has_ftaylor_series_up_to_on.has_fderiv_within_at {𝕜 : 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] {s : set E} {f : E → F} {x : E} {p : E → formal_multilinear_series 𝕜 E F}
{n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hx : x ∈ s) :
has_fderiv_within_at f (coe_fn (continuous_multilinear_curry_fin1 𝕜 E F) (p x 1)) s x :=
sorry
theorem has_ftaylor_series_up_to_on.differentiable_on {𝕜 : 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] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F}
{n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) :
differentiable_on 𝕜 f s :=
fun (x : E) (hx : x ∈ s) =>
has_fderiv_within_at.differentiable_within_at
(has_ftaylor_series_up_to_on.has_fderiv_within_at h hn hx)
/-- `p` is a Taylor series of `f` up to `n+1` if and only if `p` is a Taylor series up to `n`, and
`p (n + 1)` is a derivative of `p n`. -/
theorem has_ftaylor_series_up_to_on_succ_iff_left {𝕜 : 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] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : ℕ} :
has_ftaylor_series_up_to_on (↑n + 1) f p s ↔
has_ftaylor_series_up_to_on (↑n) f p s ∧
(∀ (x : E),
x ∈ s →
has_fderiv_within_at (fun (y : E) => p y n)
(continuous_multilinear_map.curry_left (p x (Nat.succ n))) s x) ∧
continuous_on (fun (x : E) => p x (n + 1)) s :=
sorry
/-- `p` is a Taylor series of `f` up to `n+1` if and only if `p.shift` is a Taylor series up to `n`
for `p 1`, which is a derivative of `f`. -/
theorem has_ftaylor_series_up_to_on_succ_iff_right {𝕜 : 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] {s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : ℕ} :
has_ftaylor_series_up_to_on (↑(n + 1)) f p s ↔
(∀ (x : E), x ∈ s → continuous_multilinear_map.uncurry0 (p x 0) = f x) ∧
(∀ (x : E),
x ∈ s →
has_fderiv_within_at (fun (y : E) => p y 0)
(continuous_multilinear_map.curry_left (p x 1)) s x) ∧
has_ftaylor_series_up_to_on (↑n)
(fun (x : E) => coe_fn (continuous_multilinear_curry_fin1 𝕜 E F) (p x 1))
(fun (x : E) => formal_multilinear_series.shift (p x)) s :=
sorry
/-! ### Smooth functions within a set around a point -/
/-- A function is continuously differentiable up to order `n` within a set `s` at a point `x` if
it admits continuous derivatives up to order `n` in a neighborhood of `x` in `s ∪ {x}`.
For `n = ∞`, we only require that this holds up to any finite order (where the neighborhood may
depend on the finite order we consider).
For instance, a real function which is `C^m` on `(-1/m, 1/m)` for each natural `m`, but not
better, is `C^∞` at `0` within `univ`.
-/
def times_cont_diff_within_at (𝕜 : 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]
(n : with_top ℕ) (f : E → F) (s : set E) (x : E) :=
∀ (m : ℕ),
↑m ≤ n →
∃ (u : set E),
∃ (H : u ∈ nhds_within x (insert x s)),
∃ (p : E → formal_multilinear_series 𝕜 E F), has_ftaylor_series_up_to_on (↑m) f p u
theorem times_cont_diff_within_at_nat {𝕜 : 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]
{s : set E} {f : E → F} {x : E} {n : ℕ} :
times_cont_diff_within_at 𝕜 (↑n) f s x ↔
∃ (u : set E),
∃ (H : u ∈ nhds_within x (insert x s)),
∃ (p : E → formal_multilinear_series 𝕜 E F), has_ftaylor_series_up_to_on (↑n) f p u :=
sorry
theorem times_cont_diff_within_at.of_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]
{s : set E} {f : E → F} {x : E} {m : with_top ℕ} {n : with_top ℕ}
(h : times_cont_diff_within_at 𝕜 n f s x) (hmn : m ≤ n) : times_cont_diff_within_at 𝕜 m f s x :=
fun (k : ℕ) (hk : ↑k ≤ m) => h k (le_trans hk hmn)
theorem times_cont_diff_within_at_iff_forall_nat_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] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} :
times_cont_diff_within_at 𝕜 n f s x ↔
∀ (m : ℕ), ↑m ≤ n → times_cont_diff_within_at 𝕜 (↑m) f s x :=
{ mp :=
fun (H : times_cont_diff_within_at 𝕜 n f s x) (m : ℕ) (hm : ↑m ≤ n) =>
times_cont_diff_within_at.of_le H hm,
mpr :=
fun (H : ∀ (m : ℕ), ↑m ≤ n → times_cont_diff_within_at 𝕜 (↑m) f s x) (m : ℕ) (hm : ↑m ≤ n) =>
H m hm m le_rfl }
theorem times_cont_diff_within_at_top {𝕜 : 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]
{s : set E} {f : E → F} {x : E} :
times_cont_diff_within_at 𝕜 ⊤ f s x ↔ ∀ (n : ℕ), times_cont_diff_within_at 𝕜 (↑n) f s x :=
sorry
theorem times_cont_diff_within_at.continuous_within_at {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : with_top ℕ}
(h : times_cont_diff_within_at 𝕜 n f s x) : continuous_within_at f s x :=
sorry
theorem times_cont_diff_within_at.congr_of_eventually_eq {𝕜 : 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] {s : set E} {f : E → F} {f₁ : E → F} {x : E} {n : with_top ℕ}
(h : times_cont_diff_within_at 𝕜 n f s x) (h₁ : filter.eventually_eq (nhds_within x s) f₁ f)
(hx : f₁ x = f x) : times_cont_diff_within_at 𝕜 n f₁ s x :=
sorry
theorem times_cont_diff_within_at.congr_of_eventually_eq' {𝕜 : 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] {s : set E} {f : E → F} {f₁ : E → F} {x : E}
{n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x)
(h₁ : filter.eventually_eq (nhds_within x s) f₁ f) (hx : x ∈ s) :
times_cont_diff_within_at 𝕜 n f₁ s x :=
times_cont_diff_within_at.congr_of_eventually_eq h h₁
(filter.eventually.self_of_nhds_within h₁ hx)
theorem filter.eventually_eq.times_cont_diff_within_at_iff {𝕜 : 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] {s : set E} {f : E → F} {f₁ : E → F} {x : E}
{n : with_top ℕ} (h₁ : filter.eventually_eq (nhds_within x s) f₁ f) (hx : f₁ x = f x) :
times_cont_diff_within_at 𝕜 n f₁ s x ↔ times_cont_diff_within_at 𝕜 n f s x :=
sorry
theorem times_cont_diff_within_at.congr {𝕜 : 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]
{s : set E} {f : E → F} {f₁ : E → F} {x : E} {n : with_top ℕ}
(h : times_cont_diff_within_at 𝕜 n f s x) (h₁ : ∀ (y : E), y ∈ s → f₁ y = f y)
(hx : f₁ x = f x) : times_cont_diff_within_at 𝕜 n f₁ s x :=
times_cont_diff_within_at.congr_of_eventually_eq h
(filter.eventually_eq_of_mem self_mem_nhds_within h₁) hx
theorem times_cont_diff_within_at.mono_of_mem {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : with_top ℕ}
(h : times_cont_diff_within_at 𝕜 n f s x) {t : set E} (hst : s ∈ nhds_within x t) :
times_cont_diff_within_at 𝕜 n f t x :=
sorry
theorem times_cont_diff_within_at.mono {𝕜 : 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]
{s : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜 n f s x)
{t : set E} (hst : t ⊆ s) : times_cont_diff_within_at 𝕜 n f t x :=
times_cont_diff_within_at.mono_of_mem h (filter.mem_sets_of_superset self_mem_nhds_within hst)
theorem times_cont_diff_within_at.congr_nhds {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : with_top ℕ}
(h : times_cont_diff_within_at 𝕜 n f s x) {t : set E}
(hst : nhds_within x s = nhds_within x t) : times_cont_diff_within_at 𝕜 n f t x :=
times_cont_diff_within_at.mono_of_mem h (hst ▸ self_mem_nhds_within)
theorem times_cont_diff_within_at_congr_nhds {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} {t : set E}
(hst : nhds_within x s = nhds_within x t) :
times_cont_diff_within_at 𝕜 n f s x ↔ times_cont_diff_within_at 𝕜 n f t x :=
{ mp :=
fun (h : times_cont_diff_within_at 𝕜 n f s x) => times_cont_diff_within_at.congr_nhds h hst,
mpr :=
fun (h : times_cont_diff_within_at 𝕜 n f t x) =>
times_cont_diff_within_at.congr_nhds h (Eq.symm hst) }
theorem times_cont_diff_within_at_inter' {𝕜 : 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]
{s : set E} {t : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : t ∈ nhds_within x s) :
times_cont_diff_within_at 𝕜 n f (s ∩ t) x ↔ times_cont_diff_within_at 𝕜 n f s x :=
times_cont_diff_within_at_congr_nhds (Eq.symm (nhds_within_restrict'' s h))
theorem times_cont_diff_within_at_inter {𝕜 : 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]
{s : set E} {t : set E} {f : E → F} {x : E} {n : with_top ℕ} (h : t ∈ nhds x) :
times_cont_diff_within_at 𝕜 n f (s ∩ t) x ↔ times_cont_diff_within_at 𝕜 n f s x :=
times_cont_diff_within_at_inter' (mem_nhds_within_of_mem_nhds h)
/-- If a function is `C^n` within a set at a point, with `n ≥ 1`, then it is differentiable
within this set at this point. -/
theorem times_cont_diff_within_at.differentiable_within_at' {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : with_top ℕ}
(h : times_cont_diff_within_at 𝕜 n f s x) (hn : 1 ≤ n) :
differentiable_within_at 𝕜 f (insert x s) x :=
sorry
theorem times_cont_diff_within_at.differentiable_within_at {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : with_top ℕ}
(h : times_cont_diff_within_at 𝕜 n f s x) (hn : 1 ≤ n) : differentiable_within_at 𝕜 f s x :=
differentiable_within_at.mono (times_cont_diff_within_at.differentiable_within_at' h hn)
(set.subset_insert x s)
/-- A function is `C^(n + 1)` on a domain iff locally, it has a derivative which is `C^n`. -/
theorem times_cont_diff_within_at_succ_iff_has_fderiv_within_at {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : ℕ} :
times_cont_diff_within_at 𝕜 (↑(n + 1)) f s x ↔
∃ (u : set E),
∃ (H : u ∈ nhds_within x (insert x s)),
∃ (f' : E → continuous_linear_map 𝕜 E F),
(∀ (x : E), x ∈ u → has_fderiv_within_at f (f' x) u x) ∧
times_cont_diff_within_at 𝕜 (↑n) f' u x :=
sorry
/-! ### Smooth functions within a set -/
/-- A function is continuously differentiable up to `n` on `s` if, for any point `x` in `s`, it
admits continuous derivatives up to order `n` on a neighborhood of `x` in `s`.
For `n = ∞`, we only require that this holds up to any finite order (where the neighborhood may
depend on the finite order we consider).
-/
def times_cont_diff_on (𝕜 : 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] (n : with_top ℕ)
(f : E → F) (s : set E) :=
∀ (x : E), x ∈ s → times_cont_diff_within_at 𝕜 n f s x
theorem times_cont_diff_on.times_cont_diff_within_at {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : with_top ℕ}
(h : times_cont_diff_on 𝕜 n f s) (hx : x ∈ s) : times_cont_diff_within_at 𝕜 n f s x :=
h x hx
theorem times_cont_diff_within_at.times_cont_diff_on {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : with_top ℕ} {m : ℕ} (hm : ↑m ≤ n)
(h : times_cont_diff_within_at 𝕜 n f s x) :
∃ (u : set E),
∃ (H : u ∈ nhds_within x (insert x s)), u ⊆ insert x s ∧ times_cont_diff_on 𝕜 (↑m) f u :=
sorry
theorem times_cont_diff_on.of_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]
{s : set E} {f : E → F} {m : with_top ℕ} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s)
(hmn : m ≤ n) : times_cont_diff_on 𝕜 m f s :=
fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.of_le (h x hx) hmn
theorem times_cont_diff_on_iff_forall_nat_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] {s : set E} {f : E → F} {n : with_top ℕ} :
times_cont_diff_on 𝕜 n f s ↔ ∀ (m : ℕ), ↑m ≤ n → times_cont_diff_on 𝕜 (↑m) f s :=
sorry
theorem times_cont_diff_on_top {𝕜 : 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]
{s : set E} {f : E → F} :
times_cont_diff_on 𝕜 ⊤ f s ↔ ∀ (n : ℕ), times_cont_diff_on 𝕜 (↑n) f s :=
sorry
theorem times_cont_diff_on_all_iff_nat {𝕜 : 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]
{s : set E} {f : E → F} :
(∀ (n : with_top ℕ), times_cont_diff_on 𝕜 n f s) ↔ ∀ (n : ℕ), times_cont_diff_on 𝕜 (↑n) f s :=
sorry
theorem times_cont_diff_on.continuous_on {𝕜 : 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]
{s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) : continuous_on f s :=
fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.continuous_within_at (h x hx)
theorem times_cont_diff_on.congr {𝕜 : 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]
{s : set E} {f : E → F} {f₁ : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s)
(h₁ : ∀ (x : E), x ∈ s → f₁ x = f x) : times_cont_diff_on 𝕜 n f₁ s :=
fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.congr (h x hx) h₁ (h₁ x hx)
theorem times_cont_diff_on_congr {𝕜 : 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]
{s : set E} {f : E → F} {f₁ : E → F} {n : with_top ℕ} (h₁ : ∀ (x : E), x ∈ s → f₁ x = f x) :
times_cont_diff_on 𝕜 n f₁ s ↔ times_cont_diff_on 𝕜 n f s :=
{ mp :=
fun (H : times_cont_diff_on 𝕜 n f₁ s) =>
times_cont_diff_on.congr H fun (x : E) (hx : x ∈ s) => Eq.symm (h₁ x hx),
mpr := fun (H : times_cont_diff_on 𝕜 n f s) => times_cont_diff_on.congr H h₁ }
theorem times_cont_diff_on.mono {𝕜 : 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]
{s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) {t : set E}
(hst : t ⊆ s) : times_cont_diff_on 𝕜 n f t :=
fun (x : E) (hx : x ∈ t) => times_cont_diff_within_at.mono (h x (hst hx)) hst
theorem times_cont_diff_on.congr_mono {𝕜 : 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]
{s : set E} {s₁ : set E} {f : E → F} {f₁ : E → F} {n : with_top ℕ}
(hf : times_cont_diff_on 𝕜 n f s) (h₁ : ∀ (x : E), x ∈ s₁ → f₁ x = f x) (hs : s₁ ⊆ s) :
times_cont_diff_on 𝕜 n f₁ s₁ :=
times_cont_diff_on.congr (times_cont_diff_on.mono hf hs) h₁
/-- If a function is `C^n` on a set with `n ≥ 1`, then it is differentiable there. -/
theorem times_cont_diff_on.differentiable_on {𝕜 : 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] {s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s)
(hn : 1 ≤ n) : differentiable_on 𝕜 f s :=
fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.differentiable_within_at (h x hx) hn
/-- If a function is `C^n` around each point in a set, then it is `C^n` on the set. -/
theorem times_cont_diff_on_of_locally_times_cont_diff_on {𝕜 : 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] {s : set E} {f : E → F} {n : with_top ℕ}
(h : ∀ (x : E), x ∈ s → ∃ (u : set E), is_open u ∧ x ∈ u ∧ times_cont_diff_on 𝕜 n f (s ∩ u)) :
times_cont_diff_on 𝕜 n f s :=
sorry
/-- A function is `C^(n + 1)` on a domain iff locally, it has a derivative which is `C^n`. -/
theorem times_cont_diff_on_succ_iff_has_fderiv_within_at {𝕜 : 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] {s : set E} {f : E → F} {n : ℕ} :
times_cont_diff_on 𝕜 (↑(n + 1)) f s ↔
∀ (x : E) (H : x ∈ s),
∃ (u : set E),
∃ (H : u ∈ nhds_within x (insert x s)),
∃ (f' : E → continuous_linear_map 𝕜 E F),
(∀ (x : E), x ∈ u → has_fderiv_within_at f (f' x) u x) ∧
times_cont_diff_on 𝕜 (↑n) f' u :=
sorry
/-! ### Iterated derivative within a set -/
/--
The `n`-th derivative of a function along a set, defined inductively by saying that the `n+1`-th
derivative of `f` is the derivative of the `n`-th derivative of `f` along this set, together with
an uncurrying step to see it as a multilinear map in `n+1` variables..
-/
def iterated_fderiv_within (𝕜 : 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] (n : ℕ)
(f : E → F) (s : set E) : E → continuous_multilinear_map 𝕜 (fun (i : fin n) => E) F :=
nat.rec_on n (fun (x : E) => continuous_multilinear_map.curry0 𝕜 E (f x))
fun (n : ℕ) (rec : E → continuous_multilinear_map 𝕜 (fun (i : fin n) => E) F) (x : E) =>
continuous_linear_map.uncurry_left (fderiv_within 𝕜 rec s x)
/-- Formal Taylor series associated to a function within a set. -/
def ftaylor_series_within (𝕜 : 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 : E → F) (s : set E) (x : E) : formal_multilinear_series 𝕜 E F :=
fun (n : ℕ) => iterated_fderiv_within 𝕜 n f s x
@[simp] theorem iterated_fderiv_within_zero_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] {s : set E} {f : E → F} {x : E} (m : fin 0 → E) :
coe_fn (iterated_fderiv_within 𝕜 0 f s x) m = f x :=
rfl
theorem iterated_fderiv_within_zero_eq_comp {𝕜 : 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] {s : set E} {f : E → F} :
iterated_fderiv_within 𝕜 0 f s =
⇑(continuous_linear_equiv.symm (continuous_multilinear_curry_fin0 𝕜 E F)) ∘ f :=
rfl
theorem iterated_fderiv_within_succ_apply_left {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : ℕ} (m : fin (n + 1) → E) :
coe_fn (iterated_fderiv_within 𝕜 (n + 1) f s x) m =
coe_fn (coe_fn (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n f s) s x) (m 0)) (fin.tail m) :=
rfl
/-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv,
and the derivative of the `n`-th derivative. -/
theorem iterated_fderiv_within_succ_eq_comp_left {𝕜 : 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] {s : set E} {f : E → F} {n : ℕ} :
iterated_fderiv_within 𝕜 (n + 1) f s =
⇑(continuous_multilinear_curry_left_equiv 𝕜 (fun (i : fin (n + 1)) => E) F) ∘
fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n f s) s :=
rfl
theorem iterated_fderiv_within_succ_apply_right {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : ℕ} (hs : unique_diff_on 𝕜 s)
(hx : x ∈ s) (m : fin (n + 1) → E) :
coe_fn (iterated_fderiv_within 𝕜 (n + 1) f s x) m =
coe_fn
(coe_fn (iterated_fderiv_within 𝕜 n (fun (y : E) => fderiv_within 𝕜 f s y) s x)
(fin.init m))
(m (fin.last n)) :=
sorry
/-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv,
and the `n`-th derivative of the derivative. -/
theorem iterated_fderiv_within_succ_eq_comp_right {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : ℕ} (hs : unique_diff_on 𝕜 s)
(hx : x ∈ s) :
iterated_fderiv_within 𝕜 (n + 1) f s x =
function.comp (⇑(continuous_multilinear_curry_right_equiv' 𝕜 n E F))
(iterated_fderiv_within 𝕜 n (fun (y : E) => fderiv_within 𝕜 f s y) s) x :=
sorry
@[simp] theorem iterated_fderiv_within_one_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] {s : set E} {f : E → F} {x : E} (hs : unique_diff_on 𝕜 s) (hx : x ∈ s)
(m : fin 1 → E) :
coe_fn (iterated_fderiv_within 𝕜 1 f s x) m = coe_fn (fderiv_within 𝕜 f s x) (m 0) :=
sorry
/-- If two functions coincide on a set `s` of unique differentiability, then their iterated
differentials within this set coincide. -/
theorem iterated_fderiv_within_congr {𝕜 : 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]
{s : set E} {f : E → F} {f₁ : E → F} {x : E} {n : ℕ} (hs : unique_diff_on 𝕜 s)
(hL : ∀ (y : E), y ∈ s → f₁ y = f y) (hx : x ∈ s) :
iterated_fderiv_within 𝕜 n f₁ s x = iterated_fderiv_within 𝕜 n f s x :=
sorry
/-- The iterated differential within a set `s` at a point `x` is not modified if one intersects
`s` with an open set containing `x`. -/
theorem iterated_fderiv_within_inter_open {𝕜 : 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]
{s : set E} {u : set E} {f : E → F} {x : E} {n : ℕ} (hu : is_open u)
(hs : unique_diff_on 𝕜 (s ∩ u)) (hx : x ∈ s ∩ u) :
iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x :=
sorry
/-- The iterated differential within a set `s` at a point `x` is not modified if one intersects
`s` with a neighborhood of `x` within `s`. -/
theorem iterated_fderiv_within_inter' {𝕜 : 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]
{s : set E} {u : set E} {f : E → F} {x : E} {n : ℕ} (hu : u ∈ nhds_within x s)
(hs : unique_diff_on 𝕜 s) (xs : x ∈ s) :
iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x :=
sorry
/-- The iterated differential within a set `s` at a point `x` is not modified if one intersects
`s` with a neighborhood of `x`. -/
theorem iterated_fderiv_within_inter {𝕜 : 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]
{s : set E} {u : set E} {f : E → F} {x : E} {n : ℕ} (hu : u ∈ nhds x) (hs : unique_diff_on 𝕜 s)
(xs : x ∈ s) : iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x :=
iterated_fderiv_within_inter' (mem_nhds_within_of_mem_nhds hu) hs xs
@[simp] theorem times_cont_diff_on_zero {𝕜 : 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]
{s : set E} {f : E → F} : times_cont_diff_on 𝕜 0 f s ↔ continuous_on f s :=
sorry
theorem times_cont_diff_within_at_zero {𝕜 : 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]
{s : set E} {f : E → F} {x : E} (hx : x ∈ s) :
times_cont_diff_within_at 𝕜 0 f s x ↔
∃ (u : set E), ∃ (H : u ∈ nhds_within x s), continuous_on f (s ∩ u) :=
sorry
/-- On a set with unique differentiability, any choice of iterated differential has to coincide
with the one we have chosen in `iterated_fderiv_within 𝕜 m f s`. -/
theorem has_ftaylor_series_up_to_on.eq_ftaylor_series_of_unique_diff_on {𝕜 : 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] {s : set E} {f : E → F} {x : E}
{p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ}
(h : has_ftaylor_series_up_to_on n f p s) {m : ℕ} (hmn : ↑m ≤ n) (hs : unique_diff_on 𝕜 s)
(hx : x ∈ s) : p x m = iterated_fderiv_within 𝕜 m f s x :=
sorry
/-- When a function is `C^n` in a set `s` of unique differentiability, it admits
`ftaylor_series_within 𝕜 f s` as a Taylor series up to order `n` in `s`. -/
theorem times_cont_diff_on.ftaylor_series_within {𝕜 : 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] {s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s)
(hs : unique_diff_on 𝕜 s) : has_ftaylor_series_up_to_on n f (ftaylor_series_within 𝕜 f s) s :=
sorry
theorem times_cont_diff_on_of_continuous_on_differentiable_on {𝕜 : 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] {s : set E} {f : E → F} {n : with_top ℕ}
(Hcont : ∀ (m : ℕ), ↑m ≤ n → continuous_on (fun (x : E) => iterated_fderiv_within 𝕜 m f s x) s)
(Hdiff :
∀ (m : ℕ), ↑m < n → differentiable_on 𝕜 (fun (x : E) => iterated_fderiv_within 𝕜 m f s x) s) :
times_cont_diff_on 𝕜 n f s :=
sorry
theorem times_cont_diff_on_of_differentiable_on {𝕜 : 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] {s : set E} {f : E → F} {n : with_top ℕ}
(h : ∀ (m : ℕ), ↑m ≤ n → differentiable_on 𝕜 (iterated_fderiv_within 𝕜 m f s) s) :
times_cont_diff_on 𝕜 n f s :=
times_cont_diff_on_of_continuous_on_differentiable_on
(fun (m : ℕ) (hm : ↑m ≤ n) => differentiable_on.continuous_on (h m hm))
fun (m : ℕ) (hm : ↑m < n) => h m (le_of_lt hm)
theorem times_cont_diff_on.continuous_on_iterated_fderiv_within {𝕜 : 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] {s : set E} {f : E → F} {n : with_top ℕ} {m : ℕ}
(h : times_cont_diff_on 𝕜 n f s) (hmn : ↑m ≤ n) (hs : unique_diff_on 𝕜 s) :
continuous_on (iterated_fderiv_within 𝕜 m f s) s :=
has_ftaylor_series_up_to_on.cont (times_cont_diff_on.ftaylor_series_within h hs) m hmn
theorem times_cont_diff_on.differentiable_on_iterated_fderiv_within {𝕜 : 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] {s : set E} {f : E → F} {n : with_top ℕ} {m : ℕ}
(h : times_cont_diff_on 𝕜 n f s) (hmn : ↑m < n) (hs : unique_diff_on 𝕜 s) :
differentiable_on 𝕜 (iterated_fderiv_within 𝕜 m f s) s :=
fun (x : E) (hx : x ∈ s) =>
has_fderiv_within_at.differentiable_within_at
(has_ftaylor_series_up_to_on.fderiv_within (times_cont_diff_on.ftaylor_series_within h hs) m
hmn x hx)
theorem times_cont_diff_on_iff_continuous_on_differentiable_on {𝕜 : 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] {s : set E} {f : E → F} {n : with_top ℕ}
(hs : unique_diff_on 𝕜 s) :
times_cont_diff_on 𝕜 n f s ↔
(∀ (m : ℕ), ↑m ≤ n → continuous_on (fun (x : E) => iterated_fderiv_within 𝕜 m f s x) s) ∧
∀ (m : ℕ),
↑m < n → differentiable_on 𝕜 (fun (x : E) => iterated_fderiv_within 𝕜 m f s x) s :=
sorry
/-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is
differentiable there, and its derivative (expressed with `fderiv_within`) is `C^n`. -/
theorem times_cont_diff_on_succ_iff_fderiv_within {𝕜 : 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] {s : set E} {f : E → F} {n : ℕ} (hs : unique_diff_on 𝕜 s) :
times_cont_diff_on 𝕜 (↑(n + 1)) f s ↔
differentiable_on 𝕜 f s ∧
times_cont_diff_on 𝕜 (↑n) (fun (y : E) => fderiv_within 𝕜 f s y) s :=
sorry
/-- A function is `C^(n + 1)` on an open domain if and only if it is
differentiable there, and its derivative (expressed with `fderiv`) is `C^n`. -/
theorem times_cont_diff_on_succ_iff_fderiv_of_open {𝕜 : 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] {s : set E} {f : E → F} {n : ℕ} (hs : is_open s) :
times_cont_diff_on 𝕜 (↑(n + 1)) f s ↔
differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 (↑n) (fun (y : E) => fderiv 𝕜 f y) s :=
sorry
/-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable
there, and its derivative (expressed with `fderiv_within`) is `C^∞`. -/
theorem times_cont_diff_on_top_iff_fderiv_within {𝕜 : 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] {s : set E} {f : E → F} (hs : unique_diff_on 𝕜 s) :
times_cont_diff_on 𝕜 ⊤ f s ↔
differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 ⊤ (fun (y : E) => fderiv_within 𝕜 f s y) s :=
sorry
/-- A function is `C^∞` on an open domain if and only if it is differentiable there, and its
derivative (expressed with `fderiv`) is `C^∞`. -/
theorem times_cont_diff_on_top_iff_fderiv_of_open {𝕜 : 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] {s : set E} {f : E → F} (hs : is_open s) :
times_cont_diff_on 𝕜 ⊤ f s ↔
differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 ⊤ (fun (y : E) => fderiv 𝕜 f y) s :=
sorry
theorem times_cont_diff_on.fderiv_within {𝕜 : 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]
{s : set E} {f : E → F} {m : with_top ℕ} {n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s)
(hs : unique_diff_on 𝕜 s) (hmn : m + 1 ≤ n) :
times_cont_diff_on 𝕜 m (fun (y : E) => fderiv_within 𝕜 f s y) s :=
sorry
theorem times_cont_diff_on.fderiv_of_open {𝕜 : 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]
{s : set E} {f : E → F} {m : with_top ℕ} {n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s)
(hs : is_open s) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (fun (y : E) => fderiv 𝕜 f y) s :=
times_cont_diff_on.congr (times_cont_diff_on.fderiv_within hf (is_open.unique_diff_on hs) hmn)
fun (x : E) (hx : x ∈ s) => Eq.symm (fderiv_within_of_open hs hx)
theorem times_cont_diff_on.continuous_on_fderiv_within {𝕜 : 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] {s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s)
(hs : unique_diff_on 𝕜 s) (hn : 1 ≤ n) :
continuous_on (fun (x : E) => fderiv_within 𝕜 f s x) s :=
times_cont_diff_on.continuous_on
(and.right
(iff.mp (times_cont_diff_on_succ_iff_fderiv_within hs) (times_cont_diff_on.of_le h hn)))
theorem times_cont_diff_on.continuous_on_fderiv_of_open {𝕜 : 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] {s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s)
(hs : is_open s) (hn : 1 ≤ n) : continuous_on (fun (x : E) => fderiv 𝕜 f x) s :=
times_cont_diff_on.continuous_on
(and.right
(iff.mp (times_cont_diff_on_succ_iff_fderiv_of_open hs) (times_cont_diff_on.of_le h hn)))
/-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is
continuous. -/
theorem times_cont_diff_on.continuous_on_fderiv_within_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] {s : set E} {f : E → F} {n : with_top ℕ}
(h : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hn : 1 ≤ n) :
continuous_on (fun (p : E × E) => coe_fn (fderiv_within 𝕜 f s (prod.fst p)) (prod.snd p))
(set.prod s set.univ) :=
sorry
/-! ### Functions with a Taylor series on the whole space -/
/-- `has_ftaylor_series_up_to n f p` registers the fact that `p 0 = f` and `p (m+1)` is a
derivative of `p m` for `m < n`, and is continuous for `m ≤ n`. This is a predicate analogous to
`has_fderiv_at` but for higher order derivatives. -/
structure has_ftaylor_series_up_to {𝕜 : 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]
(n : with_top ℕ) (f : E → F) (p : E → formal_multilinear_series 𝕜 E F)
where
zero_eq : ∀ (x : E), continuous_multilinear_map.uncurry0 (p x 0) = f x
fderiv :
∀ (m : ℕ),
↑m < n →
∀ (x : E),
has_fderiv_at (fun (y : E) => p y m)
(continuous_multilinear_map.curry_left (p x (Nat.succ m))) x
cont : ∀ (m : ℕ), ↑m ≤ n → continuous fun (x : E) => p x m
theorem has_ftaylor_series_up_to.zero_eq' {𝕜 : 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 : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ}
(h : has_ftaylor_series_up_to n f p) (x : E) :
p x 0 = coe_fn (continuous_linear_equiv.symm (continuous_multilinear_curry_fin0 𝕜 E F)) (f x) :=
sorry
theorem has_ftaylor_series_up_to_on_univ_iff {𝕜 : 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 : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ} :
has_ftaylor_series_up_to_on n f p set.univ ↔ has_ftaylor_series_up_to n f p :=
sorry
theorem has_ftaylor_series_up_to.has_ftaylor_series_up_to_on {𝕜 : 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 : E → F} {p : E → formal_multilinear_series 𝕜 E F}
{n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (s : set E) :
has_ftaylor_series_up_to_on n f p s :=
has_ftaylor_series_up_to_on.mono (iff.mpr has_ftaylor_series_up_to_on_univ_iff h)
(set.subset_univ s)
theorem has_ftaylor_series_up_to.of_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 : E → F} {p : E → formal_multilinear_series 𝕜 E F} {m : with_top ℕ} {n : with_top ℕ}
(h : has_ftaylor_series_up_to n f p) (hmn : m ≤ n) : has_ftaylor_series_up_to m f p :=
sorry
theorem has_ftaylor_series_up_to.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] {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ}
(h : has_ftaylor_series_up_to n f p) : continuous f :=
sorry
theorem has_ftaylor_series_up_to_zero_iff {𝕜 : 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 : E → F} {p : E → formal_multilinear_series 𝕜 E F} :
has_ftaylor_series_up_to 0 f p ↔
continuous f ∧ ∀ (x : E), continuous_multilinear_map.uncurry0 (p x 0) = f x :=
sorry
/-- If a function has a Taylor series at order at least `1`, then the term of order `1` of this
series is a derivative of `f`. -/
theorem has_ftaylor_series_up_to.has_fderiv_at {𝕜 : 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 : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ}
(h : has_ftaylor_series_up_to n f p) (hn : 1 ≤ n) (x : E) :
has_fderiv_at f (coe_fn (continuous_multilinear_curry_fin1 𝕜 E F) (p x 1)) x :=
sorry
theorem has_ftaylor_series_up_to.differentiable {𝕜 : 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 : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ}
(h : has_ftaylor_series_up_to n f p) (hn : 1 ≤ n) : differentiable 𝕜 f :=
fun (x : E) => has_fderiv_at.differentiable_at (has_ftaylor_series_up_to.has_fderiv_at h hn x)
/-- `p` is a Taylor series of `f` up to `n+1` if and only if `p.shift` is a Taylor series up to `n`
for `p 1`, which is a derivative of `f`. -/
theorem has_ftaylor_series_up_to_succ_iff_right {𝕜 : 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 : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : ℕ} :
has_ftaylor_series_up_to (↑(n + 1)) f p ↔
(∀ (x : E), continuous_multilinear_map.uncurry0 (p x 0) = f x) ∧
(∀ (x : E),
has_fderiv_at (fun (y : E) => p y 0) (continuous_multilinear_map.curry_left (p x 1))
x) ∧
has_ftaylor_series_up_to (↑n)
(fun (x : E) => coe_fn (continuous_multilinear_curry_fin1 𝕜 E F) (p x 1))
fun (x : E) => formal_multilinear_series.shift (p x) :=
sorry
/-! ### Smooth functions at a point -/
/-- A function is continuously differentiable up to `n` at a point `x` if, for any integer `k ≤ n`,
there is a neighborhood of `x` where `f` admits derivatives up to order `n`, which are continuous.
-/
def times_cont_diff_at (𝕜 : 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] (n : with_top ℕ)
(f : E → F) (x : E) :=
times_cont_diff_within_at 𝕜 n f set.univ x
theorem times_cont_diff_within_at_univ {𝕜 : 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 : E → F} {x : E} {n : with_top ℕ} :
times_cont_diff_within_at 𝕜 n f set.univ x ↔ times_cont_diff_at 𝕜 n f x :=
iff.rfl
theorem times_cont_diff_at_top {𝕜 : 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 : E → F} {x : E} : times_cont_diff_at 𝕜 ⊤ f x ↔ ∀ (n : ℕ), times_cont_diff_at 𝕜 (↑n) f x :=
sorry
theorem times_cont_diff_at.times_cont_diff_within_at {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : with_top ℕ}
(h : times_cont_diff_at 𝕜 n f x) : times_cont_diff_within_at 𝕜 n f s x :=
times_cont_diff_within_at.mono h (set.subset_univ s)
theorem times_cont_diff_within_at.times_cont_diff_at {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : with_top ℕ}
(h : times_cont_diff_within_at 𝕜 n f s x) (hx : s ∈ nhds x) : times_cont_diff_at 𝕜 n f x :=
sorry
theorem times_cont_diff_at.congr_of_eventually_eq {𝕜 : 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 : E → F} {f₁ : E → F} {x : E} {n : with_top ℕ}
(h : times_cont_diff_at 𝕜 n f x) (hg : filter.eventually_eq (nhds x) f₁ f) :
times_cont_diff_at 𝕜 n f₁ x :=
times_cont_diff_within_at.congr_of_eventually_eq' h
(eq.mpr
(id
(Eq._oldrec (Eq.refl (filter.eventually_eq (nhds_within x set.univ) f₁ f))
(nhds_within_univ x)))
hg)
(set.mem_univ x)
theorem times_cont_diff_at.of_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 : E → F} {x : E} {m : with_top ℕ} {n : with_top ℕ} (h : times_cont_diff_at 𝕜 n f x)
(hmn : m ≤ n) : times_cont_diff_at 𝕜 m f x :=
times_cont_diff_within_at.of_le h hmn
theorem times_cont_diff_at.continuous_at {𝕜 : 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 : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_at 𝕜 n f x) : continuous_at f x :=
eq.mpr (id (Eq.refl (continuous_at f x)))
(eq.mp (propext (continuous_within_at_univ f x))
(times_cont_diff_within_at.continuous_within_at h))
/-- If a function is `C^n` with `n ≥ 1` at a point, then it is differentiable there. -/
theorem times_cont_diff_at.differentiable_at {𝕜 : 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 : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff_at 𝕜 n f x)
(hn : 1 ≤ n) : differentiable_at 𝕜 f x :=
sorry
/-- A function is `C^(n + 1)` at a point iff locally, it has a derivative which is `C^n`. -/
theorem times_cont_diff_at_succ_iff_has_fderiv_at {𝕜 : 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 : E → F} {x : E} {n : ℕ} :
times_cont_diff_at 𝕜 (↑(n + 1)) f x ↔
∃ (f' : E → continuous_linear_map 𝕜 E F),
(∃ (u : set E), ∃ (H : u ∈ nhds x), ∀ (x : E), x ∈ u → has_fderiv_at f (f' x) x) ∧
times_cont_diff_at 𝕜 (↑n) f' x :=
sorry
/-! ### Smooth functions -/
/-- A function is continuously differentiable up to `n` if it admits derivatives up to
order `n`, which are continuous. Contrary to the case of definitions in domains (where derivatives
might not be unique) we do not need to localize the definition in space or time.
-/
def times_cont_diff (𝕜 : 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] (n : with_top ℕ)
(f : E → F) :=
∃ (p : E → formal_multilinear_series 𝕜 E F), has_ftaylor_series_up_to n f p
theorem times_cont_diff_on_univ {𝕜 : 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 : E → F} {n : with_top ℕ} : times_cont_diff_on 𝕜 n f set.univ ↔ times_cont_diff 𝕜 n f :=
sorry
theorem times_cont_diff_iff_times_cont_diff_at {𝕜 : 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 : E → F} {n : with_top ℕ} :
times_cont_diff 𝕜 n f ↔ ∀ (x : E), times_cont_diff_at 𝕜 n f x :=
sorry
theorem times_cont_diff.times_cont_diff_at {𝕜 : 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 : E → F} {x : E} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) :
times_cont_diff_at 𝕜 n f x :=
iff.mp times_cont_diff_iff_times_cont_diff_at h x
theorem times_cont_diff.times_cont_diff_within_at {𝕜 : 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] {s : set E} {f : E → F} {x : E} {n : with_top ℕ}
(h : times_cont_diff 𝕜 n f) : times_cont_diff_within_at 𝕜 n f s x :=
times_cont_diff_at.times_cont_diff_within_at (times_cont_diff.times_cont_diff_at h)
theorem times_cont_diff_top {𝕜 : 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 : E → F} : times_cont_diff 𝕜 ⊤ f ↔ ∀ (n : ℕ), times_cont_diff 𝕜 (↑n) f :=
sorry
theorem times_cont_diff_all_iff_nat {𝕜 : 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 : E → F} :
(∀ (n : with_top ℕ), times_cont_diff 𝕜 n f) ↔ ∀ (n : ℕ), times_cont_diff 𝕜 (↑n) f :=
sorry
theorem times_cont_diff.times_cont_diff_on {𝕜 : 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] {s : set E} {f : E → F} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) :
times_cont_diff_on 𝕜 n f s :=
times_cont_diff_on.mono (iff.mpr times_cont_diff_on_univ h) (set.subset_univ s)
@[simp] theorem times_cont_diff_zero {𝕜 : 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 : E → F} : times_cont_diff 𝕜 0 f ↔ continuous f :=
sorry
theorem times_cont_diff_at_zero {𝕜 : 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 : E → F} {x : E} :
times_cont_diff_at 𝕜 0 f x ↔ ∃ (u : set E), ∃ (H : u ∈ nhds x), continuous_on f u :=
sorry
theorem times_cont_diff.of_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 : E → F} {m : with_top ℕ} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hmn : m ≤ n) :
times_cont_diff 𝕜 m f :=
iff.mp times_cont_diff_on_univ (times_cont_diff_on.of_le (iff.mpr times_cont_diff_on_univ h) hmn)
theorem times_cont_diff.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]
{f : E → F} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) : continuous f :=
iff.mp times_cont_diff_zero (times_cont_diff.of_le h bot_le)
/-- If a function is `C^n` with `n ≥ 1`, then it is differentiable. -/
theorem times_cont_diff.differentiable {𝕜 : 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 : E → F} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) : differentiable 𝕜 f :=
iff.mp differentiable_on_univ
(times_cont_diff_on.differentiable_on (iff.mpr times_cont_diff_on_univ h) hn)
/-! ### Iterated derivative -/
/-- The `n`-th derivative of a function, as a multilinear map, defined inductively. -/
def iterated_fderiv (𝕜 : 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] (n : ℕ) (f : E → F) :
E → continuous_multilinear_map 𝕜 (fun (i : fin n) => E) F :=
nat.rec_on n (fun (x : E) => continuous_multilinear_map.curry0 𝕜 E (f x))
fun (n : ℕ) (rec : E → continuous_multilinear_map 𝕜 (fun (i : fin n) => E) F) (x : E) =>
continuous_linear_map.uncurry_left (fderiv 𝕜 rec x)
/-- Formal Taylor series associated to a function within a set. -/
def ftaylor_series (𝕜 : 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 : E → F) (x : E) :
formal_multilinear_series 𝕜 E F :=
fun (n : ℕ) => iterated_fderiv 𝕜 n f x
@[simp] theorem iterated_fderiv_zero_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] {f : E → F} {x : E} (m : fin 0 → E) :
coe_fn (iterated_fderiv 𝕜 0 f x) m = f x :=
rfl
theorem iterated_fderiv_zero_eq_comp {𝕜 : 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 : E → F} :
iterated_fderiv 𝕜 0 f =
⇑(continuous_linear_equiv.symm (continuous_multilinear_curry_fin0 𝕜 E F)) ∘ f :=
rfl
theorem iterated_fderiv_succ_apply_left {𝕜 : 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 : E → F} {x : E} {n : ℕ} (m : fin (n + 1) → E) :
coe_fn (iterated_fderiv 𝕜 (n + 1) f x) m =
coe_fn (coe_fn (fderiv 𝕜 (iterated_fderiv 𝕜 n f) x) (m 0)) (fin.tail m) :=
rfl
/-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv,
and the derivative of the `n`-th derivative. -/
theorem iterated_fderiv_succ_eq_comp_left {𝕜 : 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 : E → F} {n : ℕ} :
iterated_fderiv 𝕜 (n + 1) f =
⇑(continuous_multilinear_curry_left_equiv 𝕜 (fun (i : fin (n + 1)) => E) F) ∘
fderiv 𝕜 (iterated_fderiv 𝕜 n f) :=
rfl
theorem iterated_fderiv_within_univ {𝕜 : 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 : E → F} {n : ℕ} : iterated_fderiv_within 𝕜 n f set.univ = iterated_fderiv 𝕜 n f :=
sorry
theorem ftaylor_series_within_univ {𝕜 : 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 : E → F} : ftaylor_series_within 𝕜 f set.univ = ftaylor_series 𝕜 f :=
sorry
theorem iterated_fderiv_succ_apply_right {𝕜 : 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 : E → F} {x : E} {n : ℕ} (m : fin (n + 1) → E) :
coe_fn (iterated_fderiv 𝕜 (n + 1) f x) m =
coe_fn (coe_fn (iterated_fderiv 𝕜 n (fun (y : E) => fderiv 𝕜 f y) x) (fin.init m))
(m (fin.last n)) :=
sorry
/-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv,
and the `n`-th derivative of the derivative. -/
theorem iterated_fderiv_succ_eq_comp_right {𝕜 : 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 : E → F} {x : E} {n : ℕ} :
iterated_fderiv 𝕜 (n + 1) f x =
function.comp (⇑(continuous_multilinear_curry_right_equiv' 𝕜 n E F))
(iterated_fderiv 𝕜 n fun (y : E) => fderiv 𝕜 f y) x :=
sorry
@[simp] theorem iterated_fderiv_one_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]
{f : E → F} {x : E} (m : fin 1 → E) :
coe_fn (iterated_fderiv 𝕜 1 f x) m = coe_fn (fderiv 𝕜 f x) (m 0) :=
sorry
/-- When a function is `C^n` in a set `s` of unique differentiability, it admits
`ftaylor_series_within 𝕜 f s` as a Taylor series up to order `n` in `s`. -/
theorem times_cont_diff_on_iff_ftaylor_series {𝕜 : 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 : E → F} {n : with_top ℕ} :
times_cont_diff 𝕜 n f ↔ has_ftaylor_series_up_to n f (ftaylor_series 𝕜 f) :=
sorry
theorem times_cont_diff_iff_continuous_differentiable {𝕜 : 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 : E → F} {n : with_top ℕ} :
times_cont_diff 𝕜 n f ↔
(∀ (m : ℕ), ↑m ≤ n → continuous fun (x : E) => iterated_fderiv 𝕜 m f x) ∧
∀ (m : ℕ), ↑m < n → differentiable 𝕜 fun (x : E) => iterated_fderiv 𝕜 m f x :=
sorry
theorem times_cont_diff_of_differentiable_iterated_fderiv {𝕜 : 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 : E → F} {n : with_top ℕ}
(h : ∀ (m : ℕ), ↑m ≤ n → differentiable 𝕜 (iterated_fderiv 𝕜 m f)) : times_cont_diff 𝕜 n f :=
iff.mpr times_cont_diff_iff_continuous_differentiable
{ left := fun (m : ℕ) (hm : ↑m ≤ n) => differentiable.continuous (h m hm),
right := fun (m : ℕ) (hm : ↑m < n) => h m (le_of_lt hm) }
/-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable
there, and its derivative is `C^n`. -/
theorem times_cont_diff_succ_iff_fderiv {𝕜 : 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 : E → F} {n : ℕ} :
times_cont_diff 𝕜 (↑(n + 1)) f ↔
differentiable 𝕜 f ∧ times_cont_diff 𝕜 ↑n fun (y : E) => fderiv 𝕜 f y :=
sorry
/-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable
there, and its derivative is `C^∞`. -/
theorem times_cont_diff_top_iff_fderiv {𝕜 : 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 : E → F} :
times_cont_diff 𝕜 ⊤ f ↔ differentiable 𝕜 f ∧ times_cont_diff 𝕜 ⊤ fun (y : E) => fderiv 𝕜 f y :=
sorry
theorem times_cont_diff.continuous_fderiv {𝕜 : 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 : E → F} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) :
continuous fun (x : E) => fderiv 𝕜 f x :=
times_cont_diff.continuous
(and.right (iff.mp times_cont_diff_succ_iff_fderiv (times_cont_diff.of_le h hn)))
/-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is
continuous. -/
theorem times_cont_diff.continuous_fderiv_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] {f : E → F} {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) :
continuous fun (p : E × E) => coe_fn (fderiv 𝕜 f (prod.fst p)) (prod.snd p) :=
continuous.comp (is_bounded_bilinear_map.continuous is_bounded_bilinear_map_apply)
(continuous.prod_mk (continuous.comp (times_cont_diff.continuous_fderiv h hn) continuous_fst)
continuous_snd)
/-! ### Constants -/
theorem iterated_fderiv_within_zero_fun {𝕜 : 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] {n : ℕ} :
(iterated_fderiv 𝕜 n fun (x : E) => 0) = 0 :=
sorry
theorem times_cont_diff_zero_fun {𝕜 : 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]
{n : with_top ℕ} : times_cont_diff 𝕜 n fun (x : E) => 0 :=
sorry
/--
Constants are `C^∞`.
-/
theorem times_cont_diff_const {𝕜 : 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]
{n : with_top ℕ} {c : F} : times_cont_diff 𝕜 n fun (x : E) => c :=
sorry
theorem times_cont_diff_on_const {𝕜 : 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]
{n : with_top ℕ} {c : F} {s : set E} : times_cont_diff_on 𝕜 n (fun (x : E) => c) s :=
times_cont_diff.times_cont_diff_on times_cont_diff_const
theorem times_cont_diff_at_const {𝕜 : 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] {x : E}
{n : with_top ℕ} {c : F} : times_cont_diff_at 𝕜 n (fun (x : E) => c) x :=
times_cont_diff.times_cont_diff_at times_cont_diff_const
theorem times_cont_diff_within_at_const {𝕜 : 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]
{s : set E} {x : E} {n : with_top ℕ} {c : F} :
times_cont_diff_within_at 𝕜 n (fun (x : E) => c) s x :=
times_cont_diff_at.times_cont_diff_within_at times_cont_diff_at_const
theorem times_cont_diff_of_subsingleton {𝕜 : 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 : E → F} [subsingleton F] {n : with_top ℕ} : times_cont_diff 𝕜 n f :=
eq.mpr (id (Eq._oldrec (Eq.refl (times_cont_diff 𝕜 n f)) (subsingleton.elim f fun (_x : E) => 0)))
times_cont_diff_const
theorem times_cont_diff_at_of_subsingleton {𝕜 : 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 : E → F} {x : E} [subsingleton F] {n : with_top ℕ} :
times_cont_diff_at 𝕜 n f x :=
eq.mpr
(id (Eq._oldrec (Eq.refl (times_cont_diff_at 𝕜 n f x)) (subsingleton.elim f fun (_x : E) => 0)))
times_cont_diff_at_const
theorem times_cont_diff_within_at_of_subsingleton {𝕜 : 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] {s : set E} {f : E → F} {x : E} [subsingleton F] {n : with_top ℕ} :
times_cont_diff_within_at 𝕜 n f s x :=
eq.mpr
(id
(Eq._oldrec (Eq.refl (times_cont_diff_within_at 𝕜 n f s x))
(subsingleton.elim f fun (_x : E) => 0)))
times_cont_diff_within_at_const
theorem times_cont_diff_on_of_subsingleton {𝕜 : 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] {s : set E} {f : E → F} [subsingleton F] {n : with_top ℕ} :
times_cont_diff_on 𝕜 n f s :=
eq.mpr
(id (Eq._oldrec (Eq.refl (times_cont_diff_on 𝕜 n f s)) (subsingleton.elim f fun (_x : E) => 0)))
times_cont_diff_on_const
/-! ### Linear functions -/
/--
Unbundled bounded linear functions are `C^∞`.
-/
theorem is_bounded_linear_map.times_cont_diff {𝕜 : 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 : E → F} {n : with_top ℕ} (hf : is_bounded_linear_map 𝕜 f) :
times_cont_diff 𝕜 n f :=
sorry
theorem continuous_linear_map.times_cont_diff {𝕜 : 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] {n : with_top ℕ} (f : continuous_linear_map 𝕜 E F) :
times_cont_diff 𝕜 n ⇑f :=
is_bounded_linear_map.times_cont_diff (continuous_linear_map.is_bounded_linear_map f)
/--
The first projection in a product is `C^∞`.
-/
theorem times_cont_diff_fst {𝕜 : 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]
{n : with_top ℕ} : times_cont_diff 𝕜 n prod.fst :=
is_bounded_linear_map.times_cont_diff is_bounded_linear_map.fst
/--
The first projection on a domain in a product is `C^∞`.
-/
theorem times_cont_diff_on_fst {𝕜 : 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]
{s : set (E × F)} {n : with_top ℕ} : times_cont_diff_on 𝕜 n prod.fst s :=
times_cont_diff.times_cont_diff_on times_cont_diff_fst
/--
The first projection at a point in a product is `C^∞`.
-/
theorem times_cont_diff_at_fst {𝕜 : 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]
{p : E × F} {n : with_top ℕ} : times_cont_diff_at 𝕜 n prod.fst p :=
times_cont_diff.times_cont_diff_at times_cont_diff_fst
/--
The first projection within a domain at a point in a product is `C^∞`.
-/
theorem times_cont_diff_within_at_fst {𝕜 : 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]
{s : set (E × F)} {p : E × F} {n : with_top ℕ} : times_cont_diff_within_at 𝕜 n prod.fst s p :=
times_cont_diff.times_cont_diff_within_at times_cont_diff_fst
/--
The second projection in a product is `C^∞`.
-/
theorem times_cont_diff_snd {𝕜 : 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]
{n : with_top ℕ} : times_cont_diff 𝕜 n prod.snd :=
is_bounded_linear_map.times_cont_diff is_bounded_linear_map.snd
/--
The second projection on a domain in a product is `C^∞`.
-/
theorem times_cont_diff_on_snd {𝕜 : 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]
{s : set (E × F)} {n : with_top ℕ} : times_cont_diff_on 𝕜 n prod.snd s :=
times_cont_diff.times_cont_diff_on times_cont_diff_snd
/--
The second projection at a point in a product is `C^∞`.
-/
theorem times_cont_diff_at_snd {𝕜 : 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]
{p : E × F} {n : with_top ℕ} : times_cont_diff_at 𝕜 n prod.snd p :=
times_cont_diff.times_cont_diff_at times_cont_diff_snd
/--
The second projection within a domain at a point in a product is `C^∞`.
-/
theorem times_cont_diff_within_at_snd {𝕜 : 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]
{s : set (E × F)} {p : E × F} {n : with_top ℕ} : times_cont_diff_within_at 𝕜 n prod.snd s p :=
times_cont_diff.times_cont_diff_within_at times_cont_diff_snd
/--
The identity is `C^∞`.
-/
theorem times_cont_diff_id {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {n : with_top ℕ} : times_cont_diff 𝕜 n id :=
is_bounded_linear_map.times_cont_diff is_bounded_linear_map.id
theorem times_cont_diff_within_at_id {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {n : with_top ℕ} {s : set E} {x : E} :
times_cont_diff_within_at 𝕜 n id s x :=
times_cont_diff.times_cont_diff_within_at times_cont_diff_id
theorem times_cont_diff_at_id {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {n : with_top ℕ} {x : E} : times_cont_diff_at 𝕜 n id x :=
times_cont_diff.times_cont_diff_at times_cont_diff_id
theorem times_cont_diff_on_id {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {n : with_top ℕ} {s : set E} :
times_cont_diff_on 𝕜 n id s :=
times_cont_diff.times_cont_diff_on times_cont_diff_id
/--
Bilinear functions are `C^∞`.
-/
theorem is_bounded_bilinear_map.times_cont_diff {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {b : E × F → G}
{n : with_top ℕ} (hb : is_bounded_bilinear_map 𝕜 b) : times_cont_diff 𝕜 n b :=
sorry
/-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `g ∘ f` admits a Taylor
series whose `k`-th term is given by `g ∘ (p k)`. -/
theorem has_ftaylor_series_up_to_on.continuous_linear_map_comp {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G]
{s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ}
(g : continuous_linear_map 𝕜 F G) (hf : has_ftaylor_series_up_to_on n f p s) :
has_ftaylor_series_up_to_on n (⇑g ∘ f)
(fun (x : E) (k : ℕ) => continuous_linear_map.comp_continuous_multilinear_map g (p x k))
s :=
sorry
/-- Composition by continuous linear maps on the left preserves `C^n` functions in a domain
at a point. -/
theorem times_cont_diff_within_at.continuous_linear_map_comp {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G]
{s : set E} {f : E → F} {x : E} {n : with_top ℕ} (g : continuous_linear_map 𝕜 F G)
(hf : times_cont_diff_within_at 𝕜 n f s x) : times_cont_diff_within_at 𝕜 n (⇑g ∘ f) s x :=
sorry
/-- Composition by continuous linear maps on the left preserves `C^n` functions in a domain
at a point. -/
theorem times_cont_diff_at.continuous_linear_map_comp {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {f : E → F} {x : E}
{n : with_top ℕ} (g : continuous_linear_map 𝕜 F G) (hf : times_cont_diff_at 𝕜 n f x) :
times_cont_diff_at 𝕜 n (⇑g ∘ f) x :=
times_cont_diff_within_at.continuous_linear_map_comp g hf
/-- Composition by continuous linear maps on the left preserves `C^n` functions on domains. -/
theorem times_cont_diff_on.continuous_linear_map_comp {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F}
{n : with_top ℕ} (g : continuous_linear_map 𝕜 F G) (hf : times_cont_diff_on 𝕜 n f s) :
times_cont_diff_on 𝕜 n (⇑g ∘ f) s :=
fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.continuous_linear_map_comp g (hf x hx)
/-- Composition by continuous linear maps on the left preserves `C^n` functions. -/
theorem times_cont_diff.continuous_linear_map_comp {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ}
{f : E → F} (g : continuous_linear_map 𝕜 F G) (hf : times_cont_diff 𝕜 n f) :
times_cont_diff 𝕜 n fun (x : E) => coe_fn g (f x) :=
iff.mp times_cont_diff_on_univ
(times_cont_diff_on.continuous_linear_map_comp g (iff.mpr times_cont_diff_on_univ hf))
/-- Composition by continuous linear equivs on the left respects higher differentiability on
domains. -/
theorem continuous_linear_equiv.comp_times_cont_diff_within_at_iff {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G]
{s : set E} {f : E → F} {x : E} {n : with_top ℕ} (e : continuous_linear_equiv 𝕜 F G) :
times_cont_diff_within_at 𝕜 n (⇑e ∘ f) s x ↔ times_cont_diff_within_at 𝕜 n f s x :=
sorry
/-- Composition by continuous linear equivs on the left respects higher differentiability on
domains. -/
theorem continuous_linear_equiv.comp_times_cont_diff_on_iff {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G]
{s : set E} {f : E → F} {n : with_top ℕ} (e : continuous_linear_equiv 𝕜 F G) :
times_cont_diff_on 𝕜 n (⇑e ∘ f) s ↔ times_cont_diff_on 𝕜 n f s :=
sorry
/-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `f ∘ g` admits a Taylor
series in `g ⁻¹' s`, whose `k`-th term is given by `p k (g v₁, ..., g vₖ)` . -/
theorem has_ftaylor_series_up_to_on.comp_continuous_linear_map {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G]
{s : set E} {f : E → F} {p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ}
(hf : has_ftaylor_series_up_to_on n f p s) (g : continuous_linear_map 𝕜 G E) :
has_ftaylor_series_up_to_on n (f ∘ ⇑g)
(fun (x : G) (k : ℕ) =>
continuous_multilinear_map.comp_continuous_linear_map (p (coe_fn g x) k)
fun (_x : fin k) => g)
(⇑g ⁻¹' s) :=
sorry
/-- Composition by continuous linear maps on the right preserves `C^n` functions at a point on
a domain. -/
theorem times_cont_diff_within_at.comp_continuous_linear_map {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G]
{s : set E} {f : E → F} {n : with_top ℕ} {x : G} (g : continuous_linear_map 𝕜 G E)
(hf : times_cont_diff_within_at 𝕜 n f s (coe_fn g x)) :
times_cont_diff_within_at 𝕜 n (f ∘ ⇑g) (⇑g ⁻¹' s) x :=
sorry
/-- Composition by continuous linear maps on the right preserves `C^n` functions on domains. -/
theorem times_cont_diff_on.comp_continuous_linear_map {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F}
{n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (g : continuous_linear_map 𝕜 G E) :
times_cont_diff_on 𝕜 n (f ∘ ⇑g) (⇑g ⁻¹' s) :=
fun (x : G) (hx : x ∈ ⇑g ⁻¹' s) =>
times_cont_diff_within_at.comp_continuous_linear_map g (hf (coe_fn g x) hx)
/-- Composition by continuous linear maps on the right preserves `C^n` functions. -/
theorem times_cont_diff.comp_continuous_linear_map {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ}
{f : E → F} {g : continuous_linear_map 𝕜 G E} (hf : times_cont_diff 𝕜 n f) :
times_cont_diff 𝕜 n (f ∘ ⇑g) :=
iff.mp times_cont_diff_on_univ
(times_cont_diff_on.comp_continuous_linear_map (iff.mpr times_cont_diff_on_univ hf) g)
/-- Composition by continuous linear equivs on the right respects higher differentiability at a
point in a domain. -/
theorem continuous_linear_equiv.times_cont_diff_within_at_comp_iff {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G]
{s : set E} {f : E → F} {x : E} {n : with_top ℕ} (e : continuous_linear_equiv 𝕜 G E) :
times_cont_diff_within_at 𝕜 n (f ∘ ⇑e) (⇑e ⁻¹' s) (coe_fn (continuous_linear_equiv.symm e) x) ↔
times_cont_diff_within_at 𝕜 n f s x :=
sorry
/-- Composition by continuous linear equivs on the right respects higher differentiability on
domains. -/
theorem continuous_linear_equiv.times_cont_diff_on_comp_iff {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G]
{s : set E} {f : E → F} {n : with_top ℕ} (e : continuous_linear_equiv 𝕜 G E) :
times_cont_diff_on 𝕜 n (f ∘ ⇑e) (⇑e ⁻¹' s) ↔ times_cont_diff_on 𝕜 n f s :=
sorry
/-- If two functions `f` and `g` admit Taylor series `p` and `q` in a set `s`, then the cartesian
product of `f` and `g` admits the cartesian product of `p` and `q` as a Taylor series. -/
theorem has_ftaylor_series_up_to_on.prod {𝕜 : 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]
{G : Type u_4} [normed_group G] [normed_space 𝕜 G] {s : set E} {f : E → F}
{p : E → formal_multilinear_series 𝕜 E F} {n : with_top ℕ}
(hf : has_ftaylor_series_up_to_on n f p s) {g : E → G} {q : E → formal_multilinear_series 𝕜 E G}
(hg : has_ftaylor_series_up_to_on n g q s) :
has_ftaylor_series_up_to_on n (fun (y : E) => (f y, g y))
(fun (y : E) (k : ℕ) => continuous_multilinear_map.prod (p y k) (q y k)) s :=
sorry
/-- The cartesian product of `C^n` functions at a point in a domain is `C^n`. -/
theorem times_cont_diff_within_at.prod {𝕜 : 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]
{G : Type u_4} [normed_group G] [normed_space 𝕜 G] {x : E} {n : with_top ℕ} {s : set E}
{f : E → F} {g : E → G} (hf : times_cont_diff_within_at 𝕜 n f s x)
(hg : times_cont_diff_within_at 𝕜 n g s x) :
times_cont_diff_within_at 𝕜 n (fun (x : E) => (f x, g x)) s x :=
sorry
/-- The cartesian product of `C^n` functions on domains is `C^n`. -/
theorem times_cont_diff_on.prod {𝕜 : 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]
{G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {s : set E} {f : E → F}
{g : E → G} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) :
times_cont_diff_on 𝕜 n (fun (x : E) => (f x, g x)) s :=
fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.prod (hf x hx) (hg x hx)
/-- The cartesian product of `C^n` functions at a point is `C^n`. -/
theorem times_cont_diff_at.prod {𝕜 : 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]
{G : Type u_4} [normed_group G] [normed_space 𝕜 G] {x : E} {n : with_top ℕ} {f : E → F}
{g : E → G} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x) :
times_cont_diff_at 𝕜 n (fun (x : E) => (f x, g x)) x :=
iff.mp times_cont_diff_within_at_univ
(times_cont_diff_within_at.prod (iff.mpr times_cont_diff_within_at_univ hf)
(iff.mpr times_cont_diff_within_at_univ hg))
/--
The cartesian product of `C^n` functions is `C^n`.
-/
theorem times_cont_diff.prod {𝕜 : 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]
{G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {f : E → F} {g : E → G}
(hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) :
times_cont_diff 𝕜 n fun (x : E) => (f x, g x) :=
iff.mp times_cont_diff_on_univ
(times_cont_diff_on.prod (iff.mpr times_cont_diff_on_univ hf)
(iff.mpr times_cont_diff_on_univ hg))
/-!
### Composition of `C^n` functions
We show that the composition of `C^n` functions is `C^n`. One way to prove it would be to write
the `n`-th derivative of the composition (this is Faà di Bruno's formula) and check its continuity,
but this is very painful. Instead, we go for a simple inductive proof. Assume it is done for `n`.
Then, to check it for `n+1`, one needs to check that the derivative of `g ∘ f` is `C^n`, i.e.,
that `Dg(f x) ⬝ Df(x)` is `C^n`. The term `Dg (f x)` is the composition of two `C^n` functions, so
it is `C^n` by the inductive assumption. The term `Df(x)` is also `C^n`. Then, the matrix
multiplication is the application of a bilinear map (which is `C^∞`, and therefore `C^n`) to
`x ↦ (Dg(f x), Df x)`. As the composition of two `C^n` maps, it is again `C^n`, and we are done.
There is a subtlety in this argument: we apply the inductive assumption to functions on other Banach
spaces. In maths, one would say: prove by induction over `n` that, for all `C^n` maps between all
pairs of Banach spaces, their composition is `C^n`. In Lean, this is fine as long as the spaces
stay in the same universe. This is not the case in the above argument: if `E` lives in universe `u`
and `F` lives in universe `v`, then linear maps from `E` to `F` (to which the derivative of `f`
belongs) is in universe `max u v`. If one could quantify over finitely many universes, the above
proof would work fine, but this is not the case. One could still write the proof considering spaces
in any universe in `u, v, w, max u v, max v w, max u v w`, but it would be extremely tedious and
lead to a lot of duplication. Instead, we formulate the above proof when all spaces live in the same
universe (where everything is fine), and then we deduce the general result by lifting all our spaces
to a common universe. We use the trick that any space `H` is isomorphic through a continuous linear
equiv to `continuous_multilinear_map (λ (i : fin 0), E × F × G) H` to change the universe level,
and then argue that composing with such a linear equiv does not change the fact of being `C^n`,
which we have already proved previously.
-/
/-- Auxiliary lemma proving that the composition of `C^n` functions on domains is `C^n` when all
spaces live in the same universe. Use instead `times_cont_diff_on.comp` which removes the universe
assumption (but is deduced from this one). -/
/-- The composition of `C^n` functions on domains is `C^n`. -/
theorem times_cont_diff_on.comp {𝕜 : 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]
{G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {s : set E} {t : set F}
{g : F → G} {f : E → F} (hg : times_cont_diff_on 𝕜 n g t) (hf : times_cont_diff_on 𝕜 n f s)
(st : s ⊆ f ⁻¹' t) : times_cont_diff_on 𝕜 n (g ∘ f) s :=
sorry
/-- The composition of `C^n` functions on domains is `C^n`. -/
theorem times_cont_diff_on.comp' {𝕜 : 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]
{G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {s : set E} {t : set F}
{g : F → G} {f : E → F} (hg : times_cont_diff_on 𝕜 n g t) (hf : times_cont_diff_on 𝕜 n f s) :
times_cont_diff_on 𝕜 n (g ∘ f) (s ∩ f ⁻¹' t) :=
times_cont_diff_on.comp hg (times_cont_diff_on.mono hf (set.inter_subset_left s (f ⁻¹' t)))
(set.inter_subset_right s (f ⁻¹' t))
/-- The composition of a `C^n` function on a domain with a `C^n` function is `C^n`. -/
theorem times_cont_diff.comp_times_cont_diff_on {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ}
{s : set E} {g : F → G} {f : E → F} (hg : times_cont_diff 𝕜 n g)
(hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (g ∘ f) s :=
times_cont_diff_on.comp (iff.mpr times_cont_diff_on_univ hg) hf set.subset_preimage_univ
/-- The composition of `C^n` functions is `C^n`. -/
theorem times_cont_diff.comp {𝕜 : 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]
{G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {g : F → G} {f : E → F}
(hg : times_cont_diff 𝕜 n g) (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (g ∘ f) :=
iff.mp times_cont_diff_on_univ
(times_cont_diff_on.comp (iff.mpr times_cont_diff_on_univ hg)
(iff.mpr times_cont_diff_on_univ hf) (set.subset_univ set.univ))
/-- The composition of `C^n` functions at points in domains is `C^n`. -/
theorem times_cont_diff_within_at.comp {𝕜 : 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]
{G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {s : set E} {t : set F}
{g : F → G} {f : E → F} (x : E) (hg : times_cont_diff_within_at 𝕜 n g t (f x))
(hf : times_cont_diff_within_at 𝕜 n f s x) (st : s ⊆ f ⁻¹' t) :
times_cont_diff_within_at 𝕜 n (g ∘ f) s x :=
sorry
/-- The composition of `C^n` functions at points in domains is `C^n`. -/
theorem times_cont_diff_within_at.comp' {𝕜 : 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]
{G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ} {s : set E} {t : set F}
{g : F → G} {f : E → F} (x : E) (hg : times_cont_diff_within_at 𝕜 n g t (f x))
(hf : times_cont_diff_within_at 𝕜 n f s x) :
times_cont_diff_within_at 𝕜 n (g ∘ f) (s ∩ f ⁻¹' t) x :=
times_cont_diff_within_at.comp x hg
(times_cont_diff_within_at.mono hf (set.inter_subset_left s (f ⁻¹' t)))
(set.inter_subset_right s (f ⁻¹' t))
theorem times_cont_diff_at.comp_times_cont_diff_within_at {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G]
{s : set E} {f : E → F} {g : F → G} {n : with_top ℕ} (x : E)
(hg : times_cont_diff_at 𝕜 n g (f x)) (hf : times_cont_diff_within_at 𝕜 n f s x) :
times_cont_diff_within_at 𝕜 n (g ∘ f) s x :=
times_cont_diff_within_at.comp x hg hf (set.maps_to_univ (fun (a : E) => a) s)
/-- The composition of `C^n` functions at points is `C^n`. -/
theorem times_cont_diff_at.comp {𝕜 : 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]
{G : Type u_4} [normed_group G] [normed_space 𝕜 G] {f : E → F} {g : F → G} {n : with_top ℕ}
(x : E) (hg : times_cont_diff_at 𝕜 n g (f x)) (hf : times_cont_diff_at 𝕜 n f x) :
times_cont_diff_at 𝕜 n (g ∘ f) x :=
times_cont_diff_within_at.comp x hg hf set.subset_preimage_univ
theorem times_cont_diff.comp_times_cont_diff_within_at {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {t : set E} {x : E}
{n : with_top ℕ} {g : F → G} {f : E → F} (h : times_cont_diff 𝕜 n g)
(hf : times_cont_diff_within_at 𝕜 n f t x) : times_cont_diff_within_at 𝕜 n (g ∘ f) t x :=
times_cont_diff_within_at.comp x
(times_cont_diff_at.times_cont_diff_within_at (times_cont_diff.times_cont_diff_at h)) hf
(set.subset_univ t)
theorem times_cont_diff.comp_times_cont_diff_at {𝕜 : 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] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {n : with_top ℕ}
{g : F → G} {f : E → F} (x : E) (hg : times_cont_diff 𝕜 n g) (hf : times_cont_diff_at 𝕜 n f x) :
times_cont_diff_at 𝕜 n (g ∘ f) x :=
times_cont_diff.comp_times_cont_diff_within_at hg hf
/-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/
theorem times_cont_diff_on_fderiv_within_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] {m : with_top ℕ} {n : with_top ℕ} {s : set E} {f : E → F}
(hf : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hmn : m + 1 ≤ n) :
times_cont_diff_on 𝕜 m
(fun (p : E × E) => coe_fn (fderiv_within 𝕜 f s (prod.fst p)) (prod.snd p))
(set.prod s set.univ) :=
sorry
/-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/
theorem times_cont_diff.times_cont_diff_fderiv_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] {n : with_top ℕ} {m : with_top ℕ} {f : E → F} (hf : times_cont_diff 𝕜 n f)
(hmn : m + 1 ≤ n) :
times_cont_diff 𝕜 m fun (p : E × E) => coe_fn (fderiv 𝕜 f (prod.fst p)) (prod.snd p) :=
sorry
/-! ### Sum of two functions -/
/- The sum is smooth. -/
theorem times_cont_diff_add {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3}
[normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} :
times_cont_diff 𝕜 n fun (p : F × F) => prod.fst p + prod.snd p :=
is_bounded_linear_map.times_cont_diff
(is_bounded_linear_map.add is_bounded_linear_map.fst is_bounded_linear_map.snd)
/-- The sum of two `C^n` functions within a set at a point is `C^n` within this set
at this point. -/
theorem times_cont_diff_within_at.add {𝕜 : 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] {x : E}
{n : with_top ℕ} {s : set E} {f : E → F} {g : E → F} (hf : times_cont_diff_within_at 𝕜 n f s x)
(hg : times_cont_diff_within_at 𝕜 n g s x) :
times_cont_diff_within_at 𝕜 n (fun (x : E) => f x + g x) s x :=
times_cont_diff_within_at.comp x (times_cont_diff.times_cont_diff_within_at times_cont_diff_add)
(times_cont_diff_within_at.prod hf hg) set.subset_preimage_univ
/-- The sum of two `C^n` functions at a point is `C^n` at this point. -/
theorem times_cont_diff_at.add {𝕜 : 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] {x : E}
{n : with_top ℕ} {f : E → F} {g : E → F} (hf : times_cont_diff_at 𝕜 n f x)
(hg : times_cont_diff_at 𝕜 n g x) : times_cont_diff_at 𝕜 n (fun (x : E) => f x + g x) x :=
sorry
/-- The sum of two `C^n`functions is `C^n`. -/
theorem times_cont_diff.add {𝕜 : 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]
{n : with_top ℕ} {f : E → F} {g : E → F} (hf : times_cont_diff 𝕜 n f)
(hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n fun (x : E) => f x + g x :=
times_cont_diff.comp times_cont_diff_add (times_cont_diff.prod hf hg)
/-- The sum of two `C^n` functions on a domain is `C^n`. -/
theorem times_cont_diff_on.add {𝕜 : 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]
{n : with_top ℕ} {s : set E} {f : E → F} {g : E → F} (hf : times_cont_diff_on 𝕜 n f s)
(hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (fun (x : E) => f x + g x) s :=
fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.add (hf x hx) (hg x hx)
/-! ### Negative -/
/- The negative is smooth. -/
theorem times_cont_diff_neg {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3}
[normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} : times_cont_diff 𝕜 n fun (p : F) => -p :=
is_bounded_linear_map.times_cont_diff (is_bounded_linear_map.neg is_bounded_linear_map.id)
/-- The negative of a `C^n` function within a domain at a point is `C^n` within this domain at
this point. -/
theorem times_cont_diff_within_at.neg {𝕜 : 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] {x : E}
{n : with_top ℕ} {s : set E} {f : E → F} (hf : times_cont_diff_within_at 𝕜 n f s x) :
times_cont_diff_within_at 𝕜 n (fun (x : E) => -f x) s x :=
times_cont_diff_within_at.comp x (times_cont_diff.times_cont_diff_within_at times_cont_diff_neg)
hf set.subset_preimage_univ
/-- The negative of a `C^n` function at a point is `C^n` at this point. -/
theorem times_cont_diff_at.neg {𝕜 : 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] {x : E}
{n : with_top ℕ} {f : E → F} (hf : times_cont_diff_at 𝕜 n f x) :
times_cont_diff_at 𝕜 n (fun (x : E) => -f x) x :=
sorry
/-- The negative of a `C^n`function is `C^n`. -/
theorem times_cont_diff.neg {𝕜 : 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]
{n : with_top ℕ} {f : E → F} (hf : times_cont_diff 𝕜 n f) :
times_cont_diff 𝕜 n fun (x : E) => -f x :=
times_cont_diff.comp times_cont_diff_neg hf
/-- The negative of a `C^n` function on a domain is `C^n`. -/
theorem times_cont_diff_on.neg {𝕜 : 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]
{n : with_top ℕ} {s : set E} {f : E → F} (hf : times_cont_diff_on 𝕜 n f s) :
times_cont_diff_on 𝕜 n (fun (x : E) => -f x) s :=
fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.neg (hf x hx)
/-! ### Subtraction -/
/-- The difference of two `C^n` functions within a set at a point is `C^n` within this set
at this point. -/
theorem times_cont_diff_within_at.sub {𝕜 : 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] {x : E}
{n : with_top ℕ} {s : set E} {f : E → F} {g : E → F} (hf : times_cont_diff_within_at 𝕜 n f s x)
(hg : times_cont_diff_within_at 𝕜 n g s x) :
times_cont_diff_within_at 𝕜 n (fun (x : E) => f x - g x) s x :=
sorry
/-- The difference of two `C^n` functions at a point is `C^n` at this point. -/
theorem times_cont_diff_at.sub {𝕜 : 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] {x : E}
{n : with_top ℕ} {f : E → F} {g : E → F} (hf : times_cont_diff_at 𝕜 n f x)
(hg : times_cont_diff_at 𝕜 n g x) : times_cont_diff_at 𝕜 n (fun (x : E) => f x - g x) x :=
sorry
/-- The difference of two `C^n` functions on a domain is `C^n`. -/
theorem times_cont_diff_on.sub {𝕜 : 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]
{n : with_top ℕ} {s : set E} {f : E → F} {g : E → F} (hf : times_cont_diff_on 𝕜 n f s)
(hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (fun (x : E) => f x - g x) s :=
sorry
/-- The difference of two `C^n` functions is `C^n`. -/
theorem times_cont_diff.sub {𝕜 : 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]
{n : with_top ℕ} {f : E → F} {g : E → F} (hf : times_cont_diff 𝕜 n f)
(hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n fun (x : E) => f x - g x :=
sorry
/-! ### Sum of finitely many functions -/
theorem times_cont_diff_within_at.sum {𝕜 : 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]
{ι : Type u_4} {f : ι → E → F} {s : finset ι} {n : with_top ℕ} {t : set E} {x : E}
(h : ∀ (i : ι), i ∈ s → times_cont_diff_within_at 𝕜 n (fun (x : E) => f i x) t x) :
times_cont_diff_within_at 𝕜 n (fun (x : E) => finset.sum s fun (i : ι) => f i x) t x :=
sorry
theorem times_cont_diff_at.sum {𝕜 : 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]
{ι : Type u_4} {f : ι → E → F} {s : finset ι} {n : with_top ℕ} {x : E}
(h : ∀ (i : ι), i ∈ s → times_cont_diff_at 𝕜 n (fun (x : E) => f i x) x) :
times_cont_diff_at 𝕜 n (fun (x : E) => finset.sum s fun (i : ι) => f i x) x :=
sorry
theorem times_cont_diff_on.sum {𝕜 : 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]
{ι : Type u_4} {f : ι → E → F} {s : finset ι} {n : with_top ℕ} {t : set E}
(h : ∀ (i : ι), i ∈ s → times_cont_diff_on 𝕜 n (fun (x : E) => f i x) t) :
times_cont_diff_on 𝕜 n (fun (x : E) => finset.sum s fun (i : ι) => f i x) t :=
fun (x : E) (hx : x ∈ t) => times_cont_diff_within_at.sum fun (i : ι) (hi : i ∈ s) => h i hi x hx
theorem times_cont_diff.sum {𝕜 : 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]
{ι : Type u_4} {f : ι → E → F} {s : finset ι} {n : with_top ℕ}
(h : ∀ (i : ι), i ∈ s → times_cont_diff 𝕜 n fun (x : E) => f i x) :
times_cont_diff 𝕜 n fun (x : E) => finset.sum s fun (i : ι) => f i x :=
sorry
/-! ### Product of two functions -/
/- The product is smooth. -/
theorem times_cont_diff_mul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {n : with_top ℕ} :
times_cont_diff 𝕜 n fun (p : 𝕜 × 𝕜) => prod.fst p * prod.snd p :=
is_bounded_bilinear_map.times_cont_diff is_bounded_bilinear_map_mul
/-- The product of two `C^n` functions within a set at a point is `C^n` within this set
at this point. -/
theorem times_cont_diff_within_at.mul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {x : E} {n : with_top ℕ} {s : set E} {f : E → 𝕜} {g : E → 𝕜}
(hf : times_cont_diff_within_at 𝕜 n f s x) (hg : times_cont_diff_within_at 𝕜 n g s x) :
times_cont_diff_within_at 𝕜 n (fun (x : E) => f x * g x) s x :=
times_cont_diff_within_at.comp x (times_cont_diff.times_cont_diff_within_at times_cont_diff_mul)
(times_cont_diff_within_at.prod hf hg) set.subset_preimage_univ
/-- The product of two `C^n` functions at a point is `C^n` at this point. -/
theorem times_cont_diff_at.mul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {x : E} {n : with_top ℕ} {f : E → 𝕜} {g : E → 𝕜}
(hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x) :
times_cont_diff_at 𝕜 n (fun (x : E) => f x * g x) x :=
sorry
/-- The product of two `C^n` functions on a domain is `C^n`. -/
theorem times_cont_diff_on.mul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {n : with_top ℕ} {s : set E} {f : E → 𝕜} {g : E → 𝕜}
(hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) :
times_cont_diff_on 𝕜 n (fun (x : E) => f x * g x) s :=
fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.mul (hf x hx) (hg x hx)
/-- The product of two `C^n`functions is `C^n`. -/
theorem times_cont_diff.mul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {n : with_top ℕ} {f : E → 𝕜} {g : E → 𝕜}
(hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) :
times_cont_diff 𝕜 n fun (x : E) => f x * g x :=
times_cont_diff.comp times_cont_diff_mul (times_cont_diff.prod hf hg)
theorem times_cont_diff_within_at.div_const {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜]
{E : Type u_2} [normed_group E] [normed_space 𝕜 E] {s : set E} {x : E} {f : E → 𝕜}
{n : with_top ℕ} {c : 𝕜} (hf : times_cont_diff_within_at 𝕜 n f s x) :
times_cont_diff_within_at 𝕜 n (fun (x : E) => f x / c) s x :=
times_cont_diff_within_at.mul hf times_cont_diff_within_at_const
theorem times_cont_diff_at.div_const {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {x : E} {f : E → 𝕜} {n : with_top ℕ} {c : 𝕜}
(hf : times_cont_diff_at 𝕜 n f x) : times_cont_diff_at 𝕜 n (fun (x : E) => f x / c) x :=
times_cont_diff_at.mul hf times_cont_diff_at_const
theorem times_cont_diff_on.div_const {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {s : set E} {f : E → 𝕜} {n : with_top ℕ} {c : 𝕜}
(hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (fun (x : E) => f x / c) s :=
times_cont_diff_on.mul hf times_cont_diff_on_const
theorem times_cont_diff.div_const {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {f : E → 𝕜} {n : with_top ℕ} {c : 𝕜}
(hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n fun (x : E) => f x / c :=
times_cont_diff.mul hf times_cont_diff_const
theorem times_cont_diff.pow {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {n : with_top ℕ} {f : E → 𝕜} (hf : times_cont_diff 𝕜 n f)
(m : ℕ) : times_cont_diff 𝕜 n fun (x : E) => f x ^ m :=
sorry
/-! ### Scalar multiplication -/
/- The scalar multiplication is smooth. -/
theorem times_cont_diff_smul {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3}
[normed_group F] [normed_space 𝕜 F] {n : with_top ℕ} :
times_cont_diff 𝕜 n fun (p : 𝕜 × F) => prod.fst p • prod.snd p :=
is_bounded_bilinear_map.times_cont_diff is_bounded_bilinear_map_smul
/-- The scalar multiplication of two `C^n` functions within a set at a point is `C^n` within this
set at this point. -/
theorem times_cont_diff_within_at.smul {𝕜 : 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] {x : E}
{n : with_top ℕ} {s : set E} {f : E → 𝕜} {g : E → F} (hf : times_cont_diff_within_at 𝕜 n f s x)
(hg : times_cont_diff_within_at 𝕜 n g s x) :
times_cont_diff_within_at 𝕜 n (fun (x : E) => f x • g x) s x :=
times_cont_diff_within_at.comp x (times_cont_diff.times_cont_diff_within_at times_cont_diff_smul)
(times_cont_diff_within_at.prod hf hg) set.subset_preimage_univ
/-- The scalar multiplication of two `C^n` functions at a point is `C^n` at this point. -/
theorem times_cont_diff_at.smul {𝕜 : 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] {x : E}
{n : with_top ℕ} {f : E → 𝕜} {g : E → F} (hf : times_cont_diff_at 𝕜 n f x)
(hg : times_cont_diff_at 𝕜 n g x) : times_cont_diff_at 𝕜 n (fun (x : E) => f x • g x) x :=
sorry
/-- The scalar multiplication of two `C^n` functions is `C^n`. -/
theorem times_cont_diff.smul {𝕜 : 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]
{n : with_top ℕ} {f : E → 𝕜} {g : E → F} (hf : times_cont_diff 𝕜 n f)
(hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n fun (x : E) => f x • g x :=
times_cont_diff.comp times_cont_diff_smul (times_cont_diff.prod hf hg)
/-- The scalar multiplication of two `C^n` functions on a domain is `C^n`. -/
theorem times_cont_diff_on.smul {𝕜 : 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]
{n : with_top ℕ} {s : set E} {f : E → 𝕜} {g : E → F} (hf : times_cont_diff_on 𝕜 n f s)
(hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (fun (x : E) => f x • g x) s :=
fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.smul (hf x hx) (hg x hx)
/-! ### Cartesian product of two functions-/
/-- The product map of two `C^n` functions within a set at a point is `C^n`
within the product set at the product point. -/
theorem times_cont_diff_within_at.prod_map' {𝕜 : 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] {E' : Type u_5} [normed_group E'] [normed_space 𝕜 E'] {F' : Type u_6}
[normed_group F'] [normed_space 𝕜 F'] {n : with_top ℕ} {s : set E} {t : set E'} {f : E → F}
{g : E' → F'} {p : E × E'} (hf : times_cont_diff_within_at 𝕜 n f s (prod.fst p))
(hg : times_cont_diff_within_at 𝕜 n g t (prod.snd p)) :
times_cont_diff_within_at 𝕜 n (prod.map f g) (set.prod s t) p :=
times_cont_diff_within_at.prod
(times_cont_diff_within_at.comp p hf times_cont_diff_within_at_fst
(set.prod_subset_preimage_fst s t))
(times_cont_diff_within_at.comp p hg times_cont_diff_within_at_snd
(set.prod_subset_preimage_snd s t))
theorem times_cont_diff_within_at.prod_map {𝕜 : 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] {E' : Type u_5} [normed_group E'] [normed_space 𝕜 E'] {F' : Type u_6}
[normed_group F'] [normed_space 𝕜 F'] {n : with_top ℕ} {s : set E} {t : set E'} {f : E → F}
{g : E' → F'} {x : E} {y : E'} (hf : times_cont_diff_within_at 𝕜 n f s x)
(hg : times_cont_diff_within_at 𝕜 n g t y) :
times_cont_diff_within_at 𝕜 n (prod.map f g) (set.prod s t) (x, y) :=
times_cont_diff_within_at.prod_map' hf hg
/-- The product map of two `C^n` functions on a set is `C^n` on the product set. -/
theorem times_cont_diff_on.prod_map {𝕜 : 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]
{E' : Type u_4} [normed_group E'] [normed_space 𝕜 E'] {F' : Type u_5} [normed_group F']
[normed_space 𝕜 F'] {s : set E} {t : set E'} {n : with_top ℕ} {f : E → F} {g : E' → F'}
(hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g t) :
times_cont_diff_on 𝕜 n (prod.map f g) (set.prod s t) :=
times_cont_diff_on.prod
(times_cont_diff_on.comp hf times_cont_diff_on_fst (set.prod_subset_preimage_fst s t))
(times_cont_diff_on.comp hg times_cont_diff_on_snd (set.prod_subset_preimage_snd s t))
/-- The product map of two `C^n` functions within a set at a point is `C^n`
within the product set at the product point. -/
theorem times_cont_diff_at.prod_map {𝕜 : 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]
{E' : Type u_5} [normed_group E'] [normed_space 𝕜 E'] {F' : Type u_6} [normed_group F']
[normed_space 𝕜 F'] {n : with_top ℕ} {f : E → F} {g : E' → F'} {x : E} {y : E'}
(hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g y) :
times_cont_diff_at 𝕜 n (prod.map f g) (x, y) :=
sorry
/-- The product map of two `C^n` functions within a set at a point is `C^n`
within the product set at the product point. -/
theorem times_cont_diff_at.prod_map' {𝕜 : 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]
{E' : Type u_5} [normed_group E'] [normed_space 𝕜 E'] {F' : Type u_6} [normed_group F']
[normed_space 𝕜 F'] {n : with_top ℕ} {f : E → F} {g : E' → F'} {p : E × E'}
(hf : times_cont_diff_at 𝕜 n f (prod.fst p)) (hg : times_cont_diff_at 𝕜 n g (prod.snd p)) :
times_cont_diff_at 𝕜 n (prod.map f g) p :=
sorry
/-- The product map of two `C^n` functions is `C^n`. -/
theorem times_cont_diff.prod_map {𝕜 : 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]
{E' : Type u_5} [normed_group E'] [normed_space 𝕜 E'] {F' : Type u_6} [normed_group F']
[normed_space 𝕜 F'] {n : with_top ℕ} {f : E → F} {g : E' → F'} (hf : times_cont_diff 𝕜 n f)
(hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n (prod.map f g) :=
sorry
/-! ### Inversion in a complete normed algebra -/
/-- In a complete normed algebra, the operation of inversion is `C^n`, for all `n`, at each
invertible element. The proof is by induction, bootstrapping using an identity expressing the
derivative of inversion as a bilinear map of inversion itself. -/
theorem times_cont_diff_at_ring_inverse (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {R : Type u_5}
[normed_ring R] [normed_algebra 𝕜 R] [complete_space R] {n : with_top ℕ} (x : units R) :
times_cont_diff_at 𝕜 n ring.inverse ↑x :=
sorry
theorem times_cont_diff_at_inv (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {𝕜' : Type u_6}
[normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [complete_space 𝕜'] {x : 𝕜'} (hx : x ≠ 0)
{n : with_top ℕ} : times_cont_diff_at 𝕜 n has_inv.inv x :=
sorry
theorem times_cont_diff_on_inv (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] {𝕜' : Type u_6}
[normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [complete_space 𝕜'] {n : with_top ℕ} :
times_cont_diff_on 𝕜 n has_inv.inv (singleton 0ᶜ) :=
fun (x : 𝕜') (hx : x ∈ (singleton 0ᶜ)) =>
times_cont_diff_at.times_cont_diff_within_at (times_cont_diff_at_inv 𝕜 hx)
-- TODO: the next few lemmas don't need `𝕜` or `𝕜'` to be complete
-- A good way to show this is to generalize `times_cont_diff_at_ring_inverse` to the setting
-- of a function `f` such that `∀ᶠ x in 𝓝 a, x * f x = 1`.
theorem times_cont_diff_within_at.inv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {s : set E} {x : E} {𝕜' : Type u_6} [normed_field 𝕜']
[normed_algebra 𝕜 𝕜'] [complete_space 𝕜'] {f : E → 𝕜'} {n : with_top ℕ}
(hf : times_cont_diff_within_at 𝕜 n f s x) (hx : f x ≠ 0) :
times_cont_diff_within_at 𝕜 n (fun (x : E) => f x⁻¹) s x :=
times_cont_diff_at.comp_times_cont_diff_within_at x (times_cont_diff_at_inv 𝕜 hx) hf
theorem times_cont_diff_at.inv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {x : E} {𝕜' : Type u_6} [normed_field 𝕜']
[normed_algebra 𝕜 𝕜'] [complete_space 𝕜'] {f : E → 𝕜'} {n : with_top ℕ}
(hf : times_cont_diff_at 𝕜 n f x) (hx : f x ≠ 0) :
times_cont_diff_at 𝕜 n (fun (x : E) => f x⁻¹) x :=
times_cont_diff_within_at.inv hf hx
-- TODO: generalize to `f g : E → 𝕜'`
theorem times_cont_diff_within_at.div {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {s : set E} {x : E} [complete_space 𝕜] {f : E → 𝕜}
{g : E → 𝕜} {n : with_top ℕ} (hf : times_cont_diff_within_at 𝕜 n f s x)
(hg : times_cont_diff_within_at 𝕜 n g s x) (hx : g x ≠ 0) :
times_cont_diff_within_at 𝕜 n (fun (x : E) => f x / g x) s x :=
times_cont_diff_within_at.mul hf (times_cont_diff_within_at.inv hg hx)
theorem times_cont_diff_at.div {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] {x : E} [complete_space 𝕜] {f : E → 𝕜} {g : E → 𝕜}
{n : with_top ℕ} (hf : times_cont_diff_at 𝕜 n f x) (hg : times_cont_diff_at 𝕜 n g x)
(hx : g x ≠ 0) : times_cont_diff_at 𝕜 n (fun (x : E) => f x / g x) x :=
times_cont_diff_within_at.div hf hg hx
theorem times_cont_diff.div {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2}
[normed_group E] [normed_space 𝕜 E] [complete_space 𝕜] {f : E → 𝕜} {g : E → 𝕜} {n : with_top ℕ}
(hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) (h0 : ∀ (x : E), g x ≠ 0) :
times_cont_diff 𝕜 n fun (x : E) => f x / g x :=
sorry
/-! ### Inversion of continuous linear maps between Banach spaces -/
/-- At a continuous linear equivalence `e : E ≃L[𝕜] F` between Banach spaces, the operation of
inversion is `C^n`, for all `n`. -/
theorem times_cont_diff_at_map_inverse {𝕜 : 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 E] {n : with_top ℕ} (e : continuous_linear_equiv 𝕜 E F) :
times_cont_diff_at 𝕜 n continuous_linear_map.inverse ↑e :=
sorry
/-- If `f` is a local homeomorphism and the point `a` is in its target, and if `f` is `n` times
continuously differentiable at `f.symm a`, and if the derivative at `f.symm a` is a continuous linear
equivalence, then `f.symm` is `n` times continuously differentiable at the point `a`.
This is one of the easy parts of the inverse function theorem: it assumes that we already have
an inverse function. -/
theorem local_homeomorph.times_cont_diff_at_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 E] {n : with_top ℕ} (f : local_homeomorph E F)
{f₀' : continuous_linear_equiv 𝕜 E F} {a : F}
(ha : a ∈ local_equiv.target (local_homeomorph.to_local_equiv f))
(hf₀' : has_fderiv_at (⇑f) (↑f₀') (coe_fn (local_homeomorph.symm f) a))
(hf : times_cont_diff_at 𝕜 n (⇑f) (coe_fn (local_homeomorph.symm f) a)) :
times_cont_diff_at 𝕜 n (⇑(local_homeomorph.symm f)) a :=
sorry
/-- Let `f` be a local homeomorphism of a nondiscrete normed field, let `a` be a point in its
target. if `f` is `n` times continuously differentiable at `f.symm a`, and if the derivative at
`f.symm a` is nonzero, then `f.symm` is `n` times continuously differentiable at the point `a`.
This is one of the easy parts of the inverse function theorem: it assumes that we already have
an inverse function. -/
theorem local_homeomorph.times_cont_diff_at_symm_deriv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜]
[complete_space 𝕜] {n : with_top ℕ} (f : local_homeomorph 𝕜 𝕜) {f₀' : 𝕜} {a : 𝕜} (h₀ : f₀' ≠ 0)
(ha : a ∈ local_equiv.target (local_homeomorph.to_local_equiv f))
(hf₀' : has_deriv_at (⇑f) f₀' (coe_fn (local_homeomorph.symm f) a))
(hf : times_cont_diff_at 𝕜 n (⇑f) (coe_fn (local_homeomorph.symm f) a)) :
times_cont_diff_at 𝕜 n (⇑(local_homeomorph.symm f)) a :=
local_homeomorph.times_cont_diff_at_symm f ha (has_deriv_at.has_fderiv_at_equiv hf₀' h₀) hf
/-!
### Results over `ℝ` or `ℂ`
The results in this section rely on the Mean Value Theorem, and therefore hold only over `ℝ` (and
its extension fields such as `ℂ`).
-/
/-- If a function has a Taylor series at order at least 1, then at points in the interior of the
domain of definition, the term of order 1 of this series is a strict derivative of `f`. -/
theorem has_ftaylor_series_up_to_on.has_strict_fderiv_at {𝕂 : Type u_5} [is_R_or_C 𝕂]
{E' : Type u_6} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_7} [normed_group F']
[normed_space 𝕂 F'] {s : set E'} {f : E' → F'} {x : E'}
{p : E' → formal_multilinear_series 𝕂 E' F'} {n : with_top ℕ}
(hf : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hs : s ∈ nhds x) :
has_strict_fderiv_at f (coe_fn (continuous_multilinear_curry_fin1 𝕂 E' F') (p x 1)) x :=
sorry
/-- If a function is `C^n` with `1 ≤ n` around a point, then the derivative of `f` at this point
is also a strict derivative. -/
theorem times_cont_diff_at.has_strict_fderiv_at {𝕂 : Type u_5} [is_R_or_C 𝕂] {E' : Type u_6}
[normed_group E'] [normed_space 𝕂 E'] {F' : Type u_7} [normed_group F'] [normed_space 𝕂 F']
{f : E' → F'} {x : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f x) (hn : 1 ≤ n) :
has_strict_fderiv_at f (fderiv 𝕂 f x) x :=
sorry
/-- If a function is `C^n` with `1 ≤ n` around a point, and its derivative at that point is given to
us as `f'`, then `f'` is also a strict derivative. -/
theorem times_cont_diff_at.has_strict_fderiv_at' {𝕂 : Type u_5} [is_R_or_C 𝕂] {E' : Type u_6}
[normed_group E'] [normed_space 𝕂 E'] {F' : Type u_7} [normed_group F'] [normed_space 𝕂 F']
{f : E' → F'} {f' : continuous_linear_map 𝕂 E' F'} {x : E'} {n : with_top ℕ}
(hf : times_cont_diff_at 𝕂 n f x) (hf' : has_fderiv_at f f' x) (hn : 1 ≤ n) :
has_strict_fderiv_at f f' x :=
sorry
/-- If a function is `C^n` with `1 ≤ n`, then the derivative of `f` is also a strict derivative. -/
theorem times_cont_diff.has_strict_fderiv_at {𝕂 : Type u_5} [is_R_or_C 𝕂] {E' : Type u_6}
[normed_group E'] [normed_space 𝕂 E'] {F' : Type u_7} [normed_group F'] [normed_space 𝕂 F']
{f : E' → F'} {x : E'} {n : with_top ℕ} (hf : times_cont_diff 𝕂 n f) (hn : 1 ≤ n) :
has_strict_fderiv_at f (fderiv 𝕂 f x) x :=
times_cont_diff_at.has_strict_fderiv_at (times_cont_diff.times_cont_diff_at hf) hn
/-!
### One dimension
All results up to now have been expressed in terms of the general Fréchet derivative `fderiv`. For
maps defined on the field, the one-dimensional derivative `deriv` is often easier to use. In this
paragraph, we reformulate some higher smoothness results in terms of `deriv`.
-/
/-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is
differentiable there, and its derivative (formulated with `deriv_within`) is `C^n`. -/
theorem times_cont_diff_on_succ_iff_deriv_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜]
{F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} {n : ℕ}
(hs : unique_diff_on 𝕜 s₂) :
times_cont_diff_on 𝕜 (↑(n + 1)) f₂ s₂ ↔
differentiable_on 𝕜 f₂ s₂ ∧ times_cont_diff_on 𝕜 (↑n) (deriv_within f₂ s₂) s₂ :=
sorry
/-- A function is `C^(n + 1)` on an open domain if and only if it is
differentiable there, and its derivative (formulated with `deriv`) is `C^n`. -/
theorem times_cont_diff_on_succ_iff_deriv_of_open {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜]
{F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} {n : ℕ}
(hs : is_open s₂) :
times_cont_diff_on 𝕜 (↑(n + 1)) f₂ s₂ ↔
differentiable_on 𝕜 f₂ s₂ ∧ times_cont_diff_on 𝕜 (↑n) (deriv f₂) s₂ :=
sorry
/-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable
there, and its derivative (formulated with `deriv_within`) is `C^∞`. -/
theorem times_cont_diff_on_top_iff_deriv_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜]
{F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜}
(hs : unique_diff_on 𝕜 s₂) :
times_cont_diff_on 𝕜 ⊤ f₂ s₂ ↔
differentiable_on 𝕜 f₂ s₂ ∧ times_cont_diff_on 𝕜 ⊤ (deriv_within f₂ s₂) s₂ :=
sorry
/-- A function is `C^∞` on an open domain if and only if it is differentiable
there, and its derivative (formulated with `deriv`) is `C^∞`. -/
theorem times_cont_diff_on_top_iff_deriv_of_open {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜]
{F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} (hs : is_open s₂) :
times_cont_diff_on 𝕜 ⊤ f₂ s₂ ↔
differentiable_on 𝕜 f₂ s₂ ∧ times_cont_diff_on 𝕜 ⊤ (deriv f₂) s₂ :=
sorry
theorem times_cont_diff_on.deriv_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3}
[normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} {m : with_top ℕ} {n : with_top ℕ}
(hf : times_cont_diff_on 𝕜 n f₂ s₂) (hs : unique_diff_on 𝕜 s₂) (hmn : m + 1 ≤ n) :
times_cont_diff_on 𝕜 m (deriv_within f₂ s₂) s₂ :=
sorry
theorem times_cont_diff_on.deriv_of_open {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3}
[normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} {m : with_top ℕ} {n : with_top ℕ}
(hf : times_cont_diff_on 𝕜 n f₂ s₂) (hs : is_open s₂) (hmn : m + 1 ≤ n) :
times_cont_diff_on 𝕜 m (deriv f₂) s₂ :=
times_cont_diff_on.congr (times_cont_diff_on.deriv_within hf (is_open.unique_diff_on hs) hmn)
fun (x : 𝕜) (hx : x ∈ s₂) => Eq.symm (deriv_within_of_open hs hx)
theorem times_cont_diff_on.continuous_on_deriv_within {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜]
{F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} {n : with_top ℕ}
(h : times_cont_diff_on 𝕜 n f₂ s₂) (hs : unique_diff_on 𝕜 s₂) (hn : 1 ≤ n) :
continuous_on (deriv_within f₂ s₂) s₂ :=
times_cont_diff_on.continuous_on
(and.right
(iff.mp (times_cont_diff_on_succ_iff_deriv_within hs) (times_cont_diff_on.of_le h hn)))
theorem times_cont_diff_on.continuous_on_deriv_of_open {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜]
{F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {s₂ : set 𝕜} {n : with_top ℕ}
(h : times_cont_diff_on 𝕜 n f₂ s₂) (hs : is_open s₂) (hn : 1 ≤ n) :
continuous_on (deriv f₂) s₂ :=
times_cont_diff_on.continuous_on
(and.right
(iff.mp (times_cont_diff_on_succ_iff_deriv_of_open hs) (times_cont_diff_on.of_le h hn)))
/-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is
differentiable there, and its derivative is `C^n`. -/
theorem times_cont_diff_succ_iff_deriv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {F : Type u_3}
[normed_group F] [normed_space 𝕜 F] {f₂ : 𝕜 → F} {n : ℕ} :
times_cont_diff 𝕜 (↑(n + 1)) f₂ ↔ differentiable 𝕜 f₂ ∧ times_cont_diff 𝕜 (↑n) (deriv f₂) :=
sorry
/-!
### Restricting from `ℂ` to `ℝ`, or generally from `𝕜'` to `𝕜`
If a function is `n` times continuously differentiable over `ℂ`, then it is `n` times continuously
differentiable over `ℝ`. In this paragraph, we give variants of this statement, in the general
situation where `ℂ` and `ℝ` are replaced respectively by `𝕜'` and `𝕜` where `𝕜'` is a normed algebra
over `𝕜`.
-/
theorem has_ftaylor_series_up_to_on.restrict_scalars (𝕜 : 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] {s : set E} {f : E → F} {𝕜' : Type u_5} [nondiscrete_normed_field 𝕜']
[normed_algebra 𝕜 𝕜'] [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] [normed_space 𝕜' F]
[is_scalar_tower 𝕜 𝕜' F] {p' : E → formal_multilinear_series 𝕜' E F} {n : with_top ℕ}
(h : has_ftaylor_series_up_to_on n f p' s) :
has_ftaylor_series_up_to_on n f
(fun (x : E) => formal_multilinear_series.restrict_scalars 𝕜 (p' x)) s :=
sorry
theorem times_cont_diff_within_at.restrict_scalars (𝕜 : 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] {s : set E} {f : E → F} {x : E} {𝕜' : Type u_5} [nondiscrete_normed_field 𝕜']
[normed_algebra 𝕜 𝕜'] [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] [normed_space 𝕜' F]
[is_scalar_tower 𝕜 𝕜' F] {n : with_top ℕ} (h : times_cont_diff_within_at 𝕜' n f s x) :
times_cont_diff_within_at 𝕜 n f s x :=
sorry
theorem times_cont_diff_on.restrict_scalars (𝕜 : 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] {s : set E} {f : E → F} {𝕜' : Type u_5} [nondiscrete_normed_field 𝕜']
[normed_algebra 𝕜 𝕜'] [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] [normed_space 𝕜' F]
[is_scalar_tower 𝕜 𝕜' F] {n : with_top ℕ} (h : times_cont_diff_on 𝕜' n f s) :
times_cont_diff_on 𝕜 n f s :=
fun (x : E) (hx : x ∈ s) => times_cont_diff_within_at.restrict_scalars 𝕜 (h x hx)
theorem times_cont_diff_at.restrict_scalars (𝕜 : 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 : E → F} {x : E} {𝕜' : Type u_5} [nondiscrete_normed_field 𝕜']
[normed_algebra 𝕜 𝕜'] [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] [normed_space 𝕜' F]
[is_scalar_tower 𝕜 𝕜' F] {n : with_top ℕ} (h : times_cont_diff_at 𝕜' n f x) :
times_cont_diff_at 𝕜 n f x :=
iff.mp times_cont_diff_within_at_univ
(times_cont_diff_within_at.restrict_scalars 𝕜 (times_cont_diff_at.times_cont_diff_within_at h))
theorem times_cont_diff.restrict_scalars (𝕜 : 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 : E → F} {𝕜' : Type u_5} [nondiscrete_normed_field 𝕜'] [normed_algebra 𝕜 𝕜']
[normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] [normed_space 𝕜' F] [is_scalar_tower 𝕜 𝕜' F]
{n : with_top ℕ} (h : times_cont_diff 𝕜' n f) : times_cont_diff 𝕜 n f :=
iff.mpr times_cont_diff_iff_times_cont_diff_at
fun (x : E) => times_cont_diff_at.restrict_scalars 𝕜 (times_cont_diff.times_cont_diff_at h)
end Mathlib |
d6c3edcde2240e2d7640b638ca555c819de3044b | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /08_Building_Theories_and_Proofs.org.33.lean | 65a71c02f34725b1fc4d70b57d595c7087338384 | [] | no_license | cjmazey/lean-tutorial | ba559a49f82aa6c5848b9bf17b7389bf7f4ba645 | 381f61c9fcac56d01d959ae0fa6e376f2c4e3b34 | refs/heads/master | 1,610,286,098,832 | 1,447,124,923,000 | 1,447,124,923,000 | 43,082,433 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 108 | lean | import standard
import data.nat
open nat (add add.assoc add.comm)
check add
check add.assoc
check add.comm
|
4aff56bcf80633609cf022d4ed98fdee1f83a7f7 | da3a76c514d38801bae19e8a9e496dc31f8e5866 | /tests/lean/interactive/rb_map_ts.lean | 886afae221c2490ff1a836e096090e76771919d1 | [
"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 | 2,139 | lean | meta def mytac :=
state_t (name_map nat) tactic
meta instance : monad mytac :=
state_t.monad _ _
meta instance : monad.has_monad_lift tactic mytac :=
monad.monad_transformer_lift (state_t (name_map nat)) tactic
meta instance (α : Type) : has_coe (tactic α) (mytac α) :=
⟨monad.monad_lift⟩
namespace mytac
meta def step {α : Type} (t : mytac α) : mytac unit :=
t >> return ()
meta def rstep {α : Type} (line : nat) (col : nat) (t : mytac α) : mytac unit :=
λ v s, result.cases_on (@scope_trace _ line col (t v s))
(λ ⟨a, v⟩ new_s, result.success ((), v) new_s)
(λ opt_msg_thunk e new_s, match opt_msg_thunk with
| some msg_thunk := let msg := msg_thunk () ++ format.line ++ to_fmt "value: " ++ to_fmt v ++ format.line ++ to_fmt "state:" ++ format.line ++ new_s^.to_format in
(tactic.report_error line col msg >> interaction_monad.silent_fail) new_s | none := interaction_monad.silent_fail new_s end)
meta def execute (tac : mytac unit) : tactic unit :=
tac (name_map.mk nat) >> return ()
meta def save_info (line col : nat) : mytac unit :=
do v ← state_t.read,
s ← tactic.read,
tactic.save_info_thunk line col
(λ _, to_fmt "Custom state: " ++ to_fmt v ++ format.line ++
tactic_state.to_format s)
namespace interactive
meta def intros : mytac unit :=
tactic.intros >> return ()
meta def constructor : mytac unit :=
tactic.constructor
meta def trace (s : string) : mytac unit :=
tactic.trace s
meta def assumption : mytac unit :=
tactic.assumption
open lean.parser
open interactive
open interactive.types
meta def add (n : parse ident) (v : nat) : mytac unit :=
do m ← state_t.read, state_t.write (m^.insert n v)
end interactive
end mytac
lemma ex₁ (p q : Prop) : p → q → p ∧ q :=
begin [mytac]
intros,
add x 10,
trace "test",
--^ "command": "info"
constructor,
add y 20,
assumption,
--^ "command": "info"
assumption
end
print ex₁
lemma ex₂ (p q : Prop) : p → q → p ∧ q :=
begin [mytac]
intros,
add x 10,
trace "test",
constructor,
add y 20,
assumption,
--^ "command": "info"
assumption
end
print ex₂
|
d13882f733df5dd2fe24f88315f52013717ad864 | 9028d228ac200bbefe3a711342514dd4e4458bff | /src/linear_algebra/tensor_product.lean | 48617bb71ffd8b256ceb931cd076c1d111a90a59 | [
"Apache-2.0"
] | permissive | mcncm/mathlib | 8d25099344d9d2bee62822cb9ed43aa3e09fa05e | fde3d78cadeec5ef827b16ae55664ef115e66f57 | refs/heads/master | 1,672,743,316,277 | 1,602,618,514,000 | 1,602,618,514,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 23,004 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Mario Carneiro
-/
import group_theory.congruence
import linear_algebra.basic
/-!
# Tensor product of semimodules over commutative semirings.
This file constructs the tensor product of semimodules over commutative semirings. Given a semiring
`R` and semimodules over it `M` and `N`, the standard construction of the tensor product is
`tensor_product R M N`. It is also a semimodule over `R`.
It comes with a canonical bilinear map `M → N → tensor_product R M N`.
Given any bilinear map `M → N → P`, there is a unique linear map `tensor_product R M N → P` whose
composition with the canonical bilinear map `M → N → tensor_product R M N` is the given bilinear
map `M → N → P`.
We start by proving basic lemmas about bilinear maps.
## Notations
This file uses the localized notation `M ⊗ N` and `M ⊗[R] N` for `tensor_product R M N`, as well
as `m ⊗ₜ n` and `m ⊗ₜ[R] n` for `tensor_product.tmul R m n`.
## Tags
bilinear, tensor, tensor product
-/
namespace linear_map
variables {R : Type*} [comm_semiring R]
variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*}
variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] [add_comm_monoid Q] [add_comm_monoid S]
variables [semimodule R M] [semimodule R N] [semimodule R P] [semimodule R Q] [semimodule R S]
include R
variables (R)
/-- Create a bilinear map from a function that is linear in each component. -/
def mk₂ (f : M → N → P)
(H1 : ∀ m₁ m₂ n, f (m₁ + m₂) n = f m₁ n + f m₂ n)
(H2 : ∀ (c:R) m n, f (c • m) n = c • f m n)
(H3 : ∀ m n₁ n₂, f m (n₁ + n₂) = f m n₁ + f m n₂)
(H4 : ∀ (c:R) m n, f m (c • n) = c • f m n) : M →ₗ N →ₗ P :=
⟨λ m, ⟨f m, H3 m, λ c, H4 c m⟩,
λ m₁ m₂, linear_map.ext $ H1 m₁ m₂,
λ c m, linear_map.ext $ H2 c m⟩
variables {R}
@[simp] theorem mk₂_apply
(f : M → N → P) {H1 H2 H3 H4} (m : M) (n : N) :
(mk₂ R f H1 H2 H3 H4 : M →ₗ[R] N →ₗ P) m n = f m n := rfl
theorem ext₂ {f g : M →ₗ[R] N →ₗ[R] P}
(H : ∀ m n, f m n = g m n) : f = g :=
linear_map.ext (λ m, linear_map.ext $ λ n, H m n)
/-- Given a linear map from `M` to linear maps from `N` to `P`, i.e., a bilinear map from `M × N` to
`P`, change the order of variables and get a linear map from `N` to linear maps from `M` to `P`. -/
def flip (f : M →ₗ[R] N →ₗ[R] P) : N →ₗ M →ₗ P :=
mk₂ R (λ n m, f m n)
(λ n₁ n₂ m, (f m).map_add _ _)
(λ c n m, (f m).map_smul _ _)
(λ n m₁ m₂, by rw f.map_add; refl)
(λ c n m, by rw f.map_smul; refl)
variable (f : M →ₗ[R] N →ₗ[R] P)
@[simp] theorem flip_apply (m : M) (n : N) : flip f n m = f m n := rfl
variables {R}
theorem flip_inj {f g : M →ₗ[R] N →ₗ P} (H : flip f = flip g) : f = g :=
ext₂ $ λ m n, show flip f n m = flip g n m, by rw H
variables (R M N P)
/-- Given a linear map from `M` to linear maps from `N` to `P`, i.e., a bilinear map `M → N → P`,
change the order of variables and get a linear map from `N` to linear maps from `M` to `P`. -/
def lflip : (M →ₗ[R] N →ₗ P) →ₗ[R] N →ₗ M →ₗ P :=
⟨flip, λ _ _, rfl, λ _ _, rfl⟩
variables {R M N P}
@[simp] theorem lflip_apply (m : M) (n : N) : lflip R M N P f n m = f m n := rfl
theorem map_zero₂ (y) : f 0 y = 0 := (flip f y).map_zero
theorem map_neg₂ {R : Type*} [comm_ring R] {M N P : Type*}
[add_comm_group M] [add_comm_group N] [add_comm_group P]
[module R M] [module R N] [module R P] (f : M →ₗ[R] N →ₗ[R] P) (x y) :
f (-x) y = -f x y :=
(flip f y).map_neg _
theorem map_add₂ (x₁ x₂ y) : f (x₁ + x₂) y = f x₁ y + f x₂ y := (flip f y).map_add _ _
theorem map_smul₂ (r:R) (x y) : f (r • x) y = r • f x y := (flip f y).map_smul _ _
variables (R P)
/-- Composing a linear map `M → N` and a linear map `N → P` to form a linear map `M → P`. -/
def lcomp (f : M →ₗ[R] N) : (N →ₗ[R] P) →ₗ[R] M →ₗ[R] P :=
flip $ linear_map.comp (flip id) f
variables {R P}
@[simp] theorem lcomp_apply (f : M →ₗ[R] N) (g : N →ₗ P) (x : M) :
lcomp R P f g x = g (f x) := rfl
variables (R M N P)
/-- Composing a linear map `M → N` and a linear map `N → P` to form a linear map `M → P`. -/
def llcomp : (N →ₗ[R] P) →ₗ[R] (M →ₗ[R] N) →ₗ M →ₗ P :=
flip ⟨lcomp R P,
λ f f', ext₂ $ λ g x, g.map_add _ _,
λ c f, ext₂ $ λ g x, g.map_smul _ _⟩
variables {R M N P}
section
@[simp] theorem llcomp_apply (f : N →ₗ[R] P) (g : M →ₗ[R] N) (x : M) :
llcomp R M N P f g x = f (g x) := rfl
end
/-- Composing a linear map `Q → N` and a bilinear map `M → N → P` to
form a bilinear map `M → Q → P`. -/
def compl₂ (g : Q →ₗ N) : M →ₗ Q →ₗ P := (lcomp R _ g).comp f
@[simp] theorem compl₂_apply (g : Q →ₗ[R] N) (m : M) (q : Q) :
f.compl₂ g m q = f m (g q) := rfl
/-- Composing a linear map `P → Q` and a bilinear map `M × N → P` to
form a bilinear map `M → N → Q`. -/
def compr₂ (g : P →ₗ Q) : M →ₗ N →ₗ Q :=
linear_map.comp (llcomp R N P Q g) f
@[simp] theorem compr₂_apply (g : P →ₗ[R] Q) (m : M) (n : N) :
f.compr₂ g m n = g (f m n) := rfl
variables (R M)
/-- Scalar multiplication as a bilinear map `R → M → M`. -/
def lsmul : R →ₗ M →ₗ M :=
mk₂ R (•) add_smul (λ _ _ _, mul_smul _ _ _) smul_add
(λ r s m, by simp only [smul_smul, smul_eq_mul, mul_comm])
variables {R M}
@[simp] theorem lsmul_apply (r : R) (m : M) : lsmul R M r m = r • m := rfl
end linear_map
section semiring
variables {R : Type*} [comm_semiring R]
variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*}
variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] [add_comm_monoid Q] [add_comm_monoid S]
variables [semimodule R M] [semimodule R N] [semimodule R P] [semimodule R Q] [semimodule R S]
include R
variables (M N)
namespace tensor_product
section
-- open free_add_monoid
variables (R)
/-- The relation on `free_add_monoid (M × N)` that generates a congruence whose quotient is
the tensor product. -/
inductive eqv : free_add_monoid (M × N) → free_add_monoid (M × N) → Prop
| of_zero_left : ∀ n : N, eqv (free_add_monoid.of (0, n)) 0
| of_zero_right : ∀ m : M, eqv (free_add_monoid.of (m, 0)) 0
| of_add_left : ∀ (m₁ m₂ : M) (n : N), eqv
(free_add_monoid.of (m₁, n) + free_add_monoid.of (m₂, n)) (free_add_monoid.of (m₁ + m₂, n))
| of_add_right : ∀ (m : M) (n₁ n₂ : N), eqv
(free_add_monoid.of (m, n₁) + free_add_monoid.of (m, n₂)) (free_add_monoid.of (m, n₁ + n₂))
| of_smul : ∀ (r : R) (m : M) (n : N), eqv
(free_add_monoid.of (r • m, n)) (free_add_monoid.of (m, r • n))
| add_comm : ∀ x y, eqv (x + y) (y + x)
end
end tensor_product
variables (R)
/-- The tensor product of two semimodules `M` and `N` over the same commutative semiring `R`.
The localized notations are `M ⊗ N` and `M ⊗[R] N`, accessed by `open_locale tensor_product`. -/
def tensor_product : Type* :=
(add_con_gen (tensor_product.eqv R M N)).quotient
variables {R}
localized "infix ` ⊗ `:100 := tensor_product _" in tensor_product
localized "notation M ` ⊗[`:100 R `] ` N:100 := tensor_product R M N" in tensor_product
namespace tensor_product
section module
instance : add_comm_monoid (M ⊗[R] N) :=
{ add_comm := λ x y, add_con.induction_on₂ x y $ λ x y, quotient.sound' $
add_con_gen.rel.of _ _ $ eqv.add_comm _ _,
.. (add_con_gen (tensor_product.eqv R M N)).add_monoid }
instance : inhabited (M ⊗[R] N) := ⟨0⟩
variables (R) {M N}
/-- The canonical function `M → N → M ⊗ N`. The localized notations are `m ⊗ₜ n` and `m ⊗ₜ[R] n`,
accessed by `open_locale tensor_product`. -/
def tmul (m : M) (n : N) : M ⊗[R] N := add_con.mk' _ $ free_add_monoid.of (m, n)
variables {R}
infix ` ⊗ₜ `:100 := tmul _
notation x ` ⊗ₜ[`:100 R `] ` y := tmul R x y
@[elab_as_eliminator]
protected theorem induction_on
{C : (M ⊗[R] N) → Prop}
(z : M ⊗[R] N)
(C0 : C 0)
(C1 : ∀ {x y}, C $ x ⊗ₜ[R] y)
(Cp : ∀ {x y}, C x → C y → C (x + y)) : C z :=
add_con.induction_on z $ λ x, free_add_monoid.rec_on x C0 $ λ ⟨m, n⟩ y ih,
by { rw add_con.coe_add, exact Cp C1 ih }
variables (M)
@[simp] lemma zero_tmul (n : N) : (0 ⊗ₜ n : M ⊗[R] N) = 0 :=
quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_left _
variables {M}
lemma add_tmul (m₁ m₂ : M) (n : N) : (m₁ + m₂) ⊗ₜ n = m₁ ⊗ₜ n + m₂ ⊗ₜ[R] n :=
eq.symm $ quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_add_left _ _ _
variables (N)
@[simp] lemma tmul_zero (m : M) : (m ⊗ₜ 0 : M ⊗[R] N) = 0 :=
quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_right _
variables {N}
lemma tmul_add (m : M) (n₁ n₂ : N) : m ⊗ₜ (n₁ + n₂) = m ⊗ₜ n₁ + m ⊗ₜ[R] n₂ :=
eq.symm $ quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_add_right _ _ _
lemma smul_tmul (r : R) (m : M) (n : N) : (r • m) ⊗ₜ n = m ⊗ₜ[R] (r • n) :=
quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_smul _ _ _
/-- Auxiliary function to defining scalar multiplication on tensor product. -/
def smul.aux (r : R) : free_add_monoid (M × N) →+ M ⊗[R] N :=
free_add_monoid.lift $ λ p : M × N, (r • p.1) ⊗ₜ p.2
theorem smul.aux_of (r : R) (m : M) (n : N) :
smul.aux r (free_add_monoid.of (m, n)) = (r • m) ⊗ₜ n :=
rfl
instance : has_scalar R (M ⊗[R] N) :=
⟨λ r, (add_con_gen (tensor_product.eqv R M N)).lift (smul.aux r) $ add_con.add_con_gen_le $
λ x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, smul.aux_of, smul_zero, zero_tmul]
| _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, smul.aux_of, tmul_zero]
| _, _, (eqv.of_add_left m₁ m₂ n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, smul.aux_of, smul_add, add_tmul]
| _, _, (eqv.of_add_right m n₁ n₂) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, smul.aux_of, tmul_add]
| _, _, (eqv.of_smul s m n) := (add_con.ker_rel _).2 $
by simp_rw [smul.aux_of, smul_tmul, smul_smul, mul_comm]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
end⟩
protected theorem smul_zero (r : R) : (r • 0 : M ⊗[R] N) = 0 :=
add_monoid_hom.map_zero _
protected theorem smul_add (r : R) (x y : M ⊗[R] N) :
r • (x + y) = r • x + r • y :=
add_monoid_hom.map_add _ _ _
theorem smul_tmul' (r : R) (m : M) (n : N) :
r • (m ⊗ₜ n : M ⊗[R] N) = (r • m) ⊗ₜ n :=
rfl
instance : semimodule R (M ⊗[R] N) :=
{ smul := (•),
smul_add := λ r x y, tensor_product.smul_add r x y,
mul_smul := λ r s x, tensor_product.induction_on x
(by simp_rw tensor_product.smul_zero)
(λ m n, by simp_rw [smul_tmul', mul_smul])
(λ x y ihx ihy, by { simp_rw tensor_product.smul_add, rw [ihx, ihy] }),
one_smul := λ x, tensor_product.induction_on x
(by rw tensor_product.smul_zero)
(λ m n, by rw [smul_tmul', one_smul])
(λ x y ihx ihy, by rw [tensor_product.smul_add, ihx, ihy]),
add_smul := λ r s x, tensor_product.induction_on x
(by simp_rw [tensor_product.smul_zero, add_zero])
(λ m n, by simp_rw [smul_tmul', add_smul, add_tmul])
(λ x y ihx ihy, by { simp_rw tensor_product.smul_add, rw [ihx, ihy, add_add_add_comm] }),
smul_zero := λ r, tensor_product.smul_zero r,
zero_smul := λ x, tensor_product.induction_on x
(by rw tensor_product.smul_zero)
(λ m n, by rw [smul_tmul', zero_smul, zero_tmul])
(λ x y ihx ihy, by rw [tensor_product.smul_add, ihx, ihy, add_zero]) }
@[simp] lemma tmul_smul (r : R) (x : M) (y : N) : x ⊗ₜ (r • y) = r • (x ⊗ₜ[R] y) :=
(smul_tmul _ _ _).symm
variables (R M N)
/-- The canonical bilinear map `M → N → M ⊗[R] N`. -/
def mk : M →ₗ N →ₗ M ⊗[R] N :=
linear_map.mk₂ R (⊗ₜ) add_tmul (λ c m n, by rw [smul_tmul, tmul_smul]) tmul_add tmul_smul
variables {R M N}
@[simp] lemma mk_apply (m : M) (n : N) : mk R M N m n = m ⊗ₜ n := rfl
lemma ite_tmul (x₁ : M) (x₂ : N) (P : Prop) [decidable P] :
((if P then x₁ else 0) ⊗ₜ[R] x₂) = if P then (x₁ ⊗ₜ x₂) else 0 :=
by { split_ifs; simp }
lemma tmul_ite (x₁ : M) (x₂ : N) (P : Prop) [decidable P] :
(x₁ ⊗ₜ[R] (if P then x₂ else 0)) = if P then (x₁ ⊗ₜ x₂) else 0 :=
by { split_ifs; simp }
section
open_locale big_operators
lemma sum_tmul {α : Type*} (s : finset α) (m : α → M) (n : N) :
((∑ a in s, m a) ⊗ₜ[R] n) = ∑ a in s, m a ⊗ₜ[R] n :=
begin
classical,
induction s using finset.induction with a s has ih h,
{ simp, },
{ simp [finset.sum_insert has, add_tmul, ih], },
end
lemma tmul_sum (m : M) {α : Type*} (s : finset α) (n : α → N) :
(m ⊗ₜ[R] (∑ a in s, n a)) = ∑ a in s, m ⊗ₜ[R] n a :=
begin
classical,
induction s using finset.induction with a s has ih h,
{ simp, },
{ simp [finset.sum_insert has, tmul_add, ih], },
end
end
end module
section UMP
variables {M N P Q}
variables (f : M →ₗ[R] N →ₗ[R] P)
/-- Auxiliary function to constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P`
with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def lift_aux : (M ⊗[R] N) →+ P :=
(add_con_gen (tensor_product.eqv R M N)).lift (free_add_monoid.lift $ λ p : M × N, f p.1 p.2) $
add_con.add_con_gen_le $ λ x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, free_add_monoid.lift_eval_of, f.map_zero₂]
| _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, free_add_monoid.lift_eval_of, (f m).map_zero]
| _, _, (eqv.of_add_left m₁ m₂ n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, free_add_monoid.lift_eval_of, f.map_add₂]
| _, _, (eqv.of_add_right m n₁ n₂) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, free_add_monoid.lift_eval_of, (f m).map_add]
| _, _, (eqv.of_smul r m n) := (add_con.ker_rel _).2 $
by simp_rw [free_add_monoid.lift_eval_of, f.map_smul₂, (f m).map_smul]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
end
lemma lift_aux_tmul (m n) : lift_aux f (m ⊗ₜ n) = f m n :=
zero_add _
variable {f}
@[simp] lemma lift_aux.smul (r : R) (x) : lift_aux f (r • x) = r • lift_aux f x :=
tensor_product.induction_on x (smul_zero _).symm
(λ p q, by rw [← tmul_smul, lift_aux_tmul, lift_aux_tmul, (f p).map_smul])
(λ p q ih1 ih2, by rw [smul_add, (lift_aux f).map_add, ih1, ih2, (lift_aux f).map_add, smul_add])
variable (f)
/-- Constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P` with the property that
its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def lift : M ⊗ N →ₗ P :=
{ map_smul' := lift_aux.smul,
.. lift_aux f }
variable {f}
@[simp] lemma lift.tmul (x y) : lift f (x ⊗ₜ y) = f x y :=
zero_add _
@[simp] lemma lift.tmul' (x y) : (lift f).1 (x ⊗ₜ y) = f x y :=
lift.tmul _ _
@[ext]
theorem ext {g h : (M ⊗[R] N) →ₗ[R] P}
(H : ∀ x y, g (x ⊗ₜ y) = h (x ⊗ₜ y)) : g = h :=
linear_map.ext $ λ z, tensor_product.induction_on z (by simp_rw linear_map.map_zero) H $
λ x y ihx ihy, by rw [g.map_add, h.map_add, ihx, ihy]
theorem lift.unique {g : (M ⊗[R] N) →ₗ[R] P} (H : ∀ x y, g (x ⊗ₜ y) = f x y) :
g = lift f :=
ext $ λ m n, by rw [H, lift.tmul]
theorem lift_mk : lift (mk R M N) = linear_map.id :=
eq.symm $ lift.unique $ λ x y, rfl
theorem lift_compr₂ (g : P →ₗ Q) : lift (f.compr₂ g) = g.comp (lift f) :=
eq.symm $ lift.unique $ λ x y, by simp
theorem lift_mk_compr₂ (f : M ⊗ N →ₗ P) : lift ((mk R M N).compr₂ f) = f :=
by rw [lift_compr₂ f, lift_mk, linear_map.comp_id]
theorem mk_compr₂_inj {g h : M ⊗ N →ₗ P}
(H : (mk R M N).compr₂ g = (mk R M N).compr₂ h) : g = h :=
by rw [← lift_mk_compr₂ g, H, lift_mk_compr₂]
example : M → N → (M → N → P) → P :=
λ m, flip $ λ f, f m
variables (R M N P)
/-- Linearly constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P`
with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def uncurry : (M →ₗ[R] N →ₗ[R] P) →ₗ[R] M ⊗[R] N →ₗ[R] P :=
linear_map.flip $ lift $ (linear_map.lflip _ _ _ _).comp (linear_map.flip linear_map.id)
variables {R M N P}
@[simp] theorem uncurry_apply (f : M →ₗ[R] N →ₗ[R] P) (m : M) (n : N) :
uncurry R M N P f (m ⊗ₜ n) = f m n :=
by rw [uncurry, linear_map.flip_apply, lift.tmul]; refl
variables (R M N P)
/-- A linear equivalence constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P`
with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def lift.equiv : (M →ₗ N →ₗ P) ≃ₗ (M ⊗ N →ₗ P) :=
{ inv_fun := λ f, (mk R M N).compr₂ f,
left_inv := λ f, linear_map.ext₂ $ λ m n, lift.tmul _ _,
right_inv := λ f, ext $ λ m n, lift.tmul _ _,
.. uncurry R M N P }
/-- Given a linear map `M ⊗ N → P`, compose it with the canonical bilinear map `M → N → M ⊗ N` to
form a bilinear map `M → N → P`. -/
def lcurry : (M ⊗[R] N →ₗ[R] P) →ₗ[R] M →ₗ[R] N →ₗ[R] P :=
(lift.equiv R M N P).symm
variables {R M N P}
@[simp] theorem lcurry_apply (f : M ⊗[R] N →ₗ[R] P) (m : M) (n : N) :
lcurry R M N P f m n = f (m ⊗ₜ n) := rfl
/-- Given a linear map `M ⊗ N → P`, compose it with the canonical bilinear map `M → N → M ⊗ N` to
form a bilinear map `M → N → P`. -/
def curry (f : M ⊗ N →ₗ P) : M →ₗ N →ₗ P := lcurry R M N P f
@[simp] theorem curry_apply (f : M ⊗ N →ₗ[R] P) (m : M) (n : N) :
curry f m n = f (m ⊗ₜ n) := rfl
theorem ext_threefold {g h : (M ⊗[R] N) ⊗[R] P →ₗ[R] Q}
(H : ∀ x y z, g ((x ⊗ₜ y) ⊗ₜ z) = h ((x ⊗ₜ y) ⊗ₜ z)) : g = h :=
begin
let e := linear_equiv.to_equiv (lift.equiv R (M ⊗[R] N) P Q),
apply e.symm.injective,
refine ext _,
intros x y,
ext z,
exact H x y z
end
-- We'll need this one for checking the pentagon identity!
theorem ext_fourfold {g h : ((M ⊗[R] N) ⊗[R] P) ⊗[R] Q →ₗ[R] S}
(H : ∀ w x y z, g (((w ⊗ₜ x) ⊗ₜ y) ⊗ₜ z) = h (((w ⊗ₜ x) ⊗ₜ y) ⊗ₜ z)) : g = h :=
begin
let e := linear_equiv.to_equiv (lift.equiv R ((M ⊗[R] N) ⊗[R] P) Q S),
apply e.symm.injective,
refine ext_threefold _,
intros x y z,
ext w,
exact H x y z w,
end
end UMP
variables {M N}
section
variables (R M)
/--
The base ring is a left identity for the tensor product of modules, up to linear equivalence.
-/
protected def lid : R ⊗ M ≃ₗ M :=
linear_equiv.of_linear (lift $ linear_map.lsmul R M) (mk R R M 1)
(linear_map.ext $ λ _, by simp)
(ext $ λ r m, by simp; rw [← tmul_smul, ← smul_tmul, smul_eq_mul, mul_one])
end
@[simp] theorem lid_tmul (m : M) (r : R) :
((tensor_product.lid R M) : (R ⊗ M → M)) (r ⊗ₜ m) = r • m :=
begin
dsimp [tensor_product.lid],
simp,
end
section
variables (R M N)
/--
The tensor product of modules is commutative, up to linear equivalence.
-/
protected def comm : M ⊗ N ≃ₗ N ⊗ M :=
linear_equiv.of_linear (lift (mk R N M).flip) (lift (mk R M N).flip)
(ext $ λ m n, rfl)
(ext $ λ m n, rfl)
@[simp] theorem comm_tmul (m : M) (n : N) :
((tensor_product.comm R M N) : (M ⊗ N → N ⊗ M)) (m ⊗ₜ n) = n ⊗ₜ m :=
begin
dsimp [tensor_product.comm],
simp,
end
end
section
variables (R M)
/--
The base ring is a right identity for the tensor product of modules, up to linear equivalence.
-/
protected def rid : M ⊗[R] R ≃ₗ M := linear_equiv.trans (tensor_product.comm R M R) (tensor_product.lid R M)
end
@[simp] theorem rid_tmul (m : M) (r : R) :
((tensor_product.rid R M) : (M ⊗ R → M)) (m ⊗ₜ r) = r • m :=
begin
dsimp [tensor_product.rid, tensor_product.comm, tensor_product.lid],
simp,
end
open linear_map
section
variables (R M N P)
/-- The associator for tensor product of R-modules, as a linear equivalence. -/
protected def assoc : (M ⊗[R] N) ⊗[R] P ≃ₗ[R] M ⊗[R] (N ⊗[R] P) :=
begin
refine linear_equiv.of_linear
(lift $ lift $ comp (lcurry R _ _ _) $ mk _ _ _)
(lift $ comp (uncurry R _ _ _) $ curry $ mk _ _ _)
(mk_compr₂_inj $ linear_map.ext $ λ m, ext $ λ n p, _)
(mk_compr₂_inj $ flip_inj $ linear_map.ext $ λ p, ext $ λ m n, _);
repeat { rw lift.tmul <|> rw compr₂_apply <|> rw comp_apply <|>
rw mk_apply <|> rw flip_apply <|> rw lcurry_apply <|>
rw uncurry_apply <|> rw curry_apply <|> rw id_apply }
end
end
@[simp] theorem assoc_tmul (m : M) (n : N) (p : P) :
((tensor_product.assoc R M N P) : (M ⊗[R] N) ⊗[R] P → M ⊗[R] (N ⊗[R] P)) ((m ⊗ₜ n) ⊗ₜ p) = m ⊗ₜ (n ⊗ₜ p) :=
rfl
/-- The tensor product of a pair of linear maps between modules. -/
def map (f : M →ₗ[R] P) (g : N →ₗ Q) : M ⊗ N →ₗ[R] P ⊗ Q :=
lift $ comp (compl₂ (mk _ _ _) g) f
@[simp] theorem map_tmul (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (m : M) (n : N) :
map f g (m ⊗ₜ n) = f m ⊗ₜ g n :=
rfl
/-- If M and P are linearly equivalent and N and Q are linearly equivalent
then M ⊗ N and P ⊗ Q are linearly equivalent. -/
def congr (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) : M ⊗ N ≃ₗ[R] P ⊗ Q :=
linear_equiv.of_linear (map f g) (map f.symm g.symm)
(ext $ λ m n, by simp; simp only [linear_equiv.apply_symm_apply])
(ext $ λ m n, by simp; simp only [linear_equiv.symm_apply_apply])
@[simp] theorem congr_tmul (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) (m : M) (n : N) :
congr f g (m ⊗ₜ n) = f m ⊗ₜ g n :=
rfl
end tensor_product
end semiring
section ring
namespace tensor_product
variables {R : Type*} [comm_ring R]
variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*}
variables [add_comm_group M] [add_comm_group N] [add_comm_group P] [add_comm_group Q] [add_comm_group S]
variables [module R M] [module R N] [module R P] [module R Q] [module R S]
open_locale tensor_product
open linear_map
instance : add_comm_group (M ⊗[R] N) := semimodule.add_comm_monoid_to_add_comm_group R
lemma neg_tmul (m : M) (n : N) : (-m) ⊗ₜ n = -(m ⊗ₜ[R] n) := (mk R M N).map_neg₂ _ _
lemma tmul_neg (m : M) (n : N) : m ⊗ₜ (-n) = -(m ⊗ₜ[R] n) := (mk R M N _).map_neg _
end tensor_product
end ring
|
e097e6048ce8b6d0182f516796cf0670fcacef2f | 853df553b1d6ca524e3f0a79aedd32dde5d27ec3 | /src/ring_theory/integral_domain.lean | 5dba8c6efd26d910e0fdb2e8ffbf0b961d173e0c | [
"Apache-2.0"
] | permissive | DanielFabian/mathlib | efc3a50b5dde303c59eeb6353ef4c35a345d7112 | f520d07eba0c852e96fe26da71d85bf6d40fcc2a | refs/heads/master | 1,668,739,922,971 | 1,595,201,756,000 | 1,595,201,756,000 | 279,469,476 | 0 | 0 | null | 1,594,696,604,000 | 1,594,696,604,000 | null | UTF-8 | Lean | false | false | 6,765 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Chris Hughes
-/
import data.fintype.card
import data.polynomial
import group_theory.order_of_element
import algebra.geom_sum
/-!
# Integral domains
-/
section
open finset polynomial function
open_locale big_operators nat
variables {R : Type*} {G : Type*} [integral_domain R] [group G] [fintype G]
lemma card_nth_roots_subgroup_units (f : G →* R) (hf : injective f) {n : ℕ} (hn : 0 < n) (g₀ : G) :
({g ∈ univ | g ^ n = g₀} : finset G).card ≤ (nth_roots n (f g₀)).card :=
begin
apply card_le_card_of_inj_on f,
{ intros g hg, rw [sep_def, mem_filter] at hg, rw [mem_nth_roots hn, ← f.map_pow, hg.2] },
{ intros, apply hf, assumption }
end
/-- A finite subgroup of the unit group of an integral domain is cyclic. -/
lemma is_cyclic_of_subgroup_integral_domain (f : G →* R) (hf : injective f) : is_cyclic G :=
begin
haveI := classical.dec_eq G,
apply is_cyclic_of_card_pow_eq_one_le,
intros n hn,
convert (le_trans (card_nth_roots_subgroup_units f hf hn 1) (card_nth_roots n (f 1)))
end
/-- The unit group of a finite integral domain is cyclic. -/
instance [fintype R] : is_cyclic (units R) :=
is_cyclic_of_subgroup_integral_domain (units.coe_hom R) $ units.ext
/-- Every finite integral domain is a field. -/
def field_of_integral_domain [fintype R] [decidable_eq R] : field R :=
{ inv := λ a, if h : a = 0 then 0
else fintype.bij_inv (show function.bijective (* a),
from fintype.injective_iff_bijective.1 $ λ _ _, mul_right_cancel' h) 1,
mul_inv_cancel := λ a ha, show a * dite _ _ _ = _, by rw [dif_neg ha, mul_comm];
exact fintype.right_inverse_bij_inv (show function.bijective (* a), from _) 1,
inv_zero := dif_pos rfl,
..show integral_domain R, by apply_instance }
section
variables (S : set (units R)) [is_subgroup S] [fintype S]
/-- A finite subgroup of the units of an integral domain is cyclic. -/
instance subgroup_units_cyclic : is_cyclic S :=
begin
refine is_cyclic_of_subgroup_integral_domain ⟨(coe : S → R), _, _⟩
(units.ext.comp subtype.val_injective),
{ simp only [is_submonoid.coe_one, units.coe_one, coe_coe] },
{ intros, simp only [is_submonoid.coe_mul, units.coe_mul, coe_coe] },
end
end
lemma card_fiber_eq_of_mem_range {H : Type*} [group H] [decidable_eq H]
(f : G →* H) {x y : H} (hx : x ∈ set.range f) (hy : y ∈ set.range f) :
(univ.filter $ λ g, f g = x).card = (univ.filter $ λ g, f g = y).card :=
begin
rcases hx with ⟨x, rfl⟩,
rcases hy with ⟨y, rfl⟩,
refine card_congr (λ g _, g * x⁻¹ * y) _ _ (λ g hg, ⟨g * y⁻¹ * x, _⟩),
{ simp only [mem_filter, one_mul, monoid_hom.map_mul, mem_univ, mul_right_inv,
eq_self_iff_true, monoid_hom.map_mul_inv, and_self, forall_true_iff] {contextual := tt} },
{ simp only [mul_left_inj, imp_self, forall_2_true_iff], },
{ simp only [true_and, mem_filter, mem_univ] at hg,
simp only [hg, mem_filter, one_mul, monoid_hom.map_mul, mem_univ, mul_right_inv,
eq_self_iff_true, exists_prop_of_true, monoid_hom.map_mul_inv, and_self,
mul_inv_cancel_right, inv_mul_cancel_right], }
end
/-- In an integral domain, a sum indexed by a nontrivial homomorphism from a finite group is zero. -/
lemma sum_hom_units_eq_zero (f : G →* R) (hf : f ≠ 1) : ∑ g : G, f g = 0 :=
begin
classical,
obtain ⟨x, hx⟩ : ∃ x : set.range f.to_hom_units, ∀ y : set.range f.to_hom_units, y ∈ powers x,
from is_cyclic.exists_monoid_generator (set.range (f.to_hom_units)),
have hx1 : x ≠ 1,
{ rintro rfl,
apply hf,
ext g,
rw [monoid_hom.one_apply],
cases hx ⟨f.to_hom_units g, g, rfl⟩ with n hn,
rwa [subtype.ext_iff, units.ext_iff, subtype.coe_mk, monoid_hom.coe_to_hom_units,
is_submonoid.coe_pow, units.coe_pow, is_submonoid.coe_one, units.coe_one,
_root_.one_pow, eq_comm] at hn, },
replace hx1 : (x : R) - 1 ≠ 0,
from λ h, hx1 (subtype.eq (units.ext (sub_eq_zero.1 h))),
let c := (univ.filter (λ g, f.to_hom_units g = 1)).card,
calc ∑ g : G, f g
= ∑ g : G, f.to_hom_units g : rfl
... = ∑ u : units R in univ.image f.to_hom_units, (univ.filter (λ g, f.to_hom_units g = u)).card • u :
sum_comp (coe : units R → R) f.to_hom_units
... = ∑ u : units R in univ.image f.to_hom_units, c • u :
sum_congr rfl (λ u hu, congr_arg2 _ _ rfl) -- remaining goal 1, proven below
... = ∑ b : set.range f.to_hom_units, c • ↑b : finset.sum_subtype
(by simp only [mem_image, set.mem_range, forall_const, iff_self, mem_univ, exists_prop_of_true]) _
... = c • ∑ b : set.range f.to_hom_units, (b : R) : smul_sum.symm
... = c • 0 : congr_arg2 _ rfl _ -- remaining goal 2, proven below
... = 0 : smul_zero _,
{ -- remaining goal 1
show (univ.filter (λ (g : G), f.to_hom_units g = u)).card = c,
apply card_fiber_eq_of_mem_range f.to_hom_units,
{ simpa only [mem_image, mem_univ, exists_prop_of_true, set.mem_range] using hu, },
{ exact ⟨1, f.to_hom_units.map_one⟩ } },
-- remaining goal 2
show ∑ b : set.range f.to_hom_units, (b : R) = 0,
calc ∑ b : set.range f.to_hom_units, (b : R)
= ∑ n in range (order_of x), x ^ n :
eq.symm $ sum_bij (λ n _, x ^ n)
(by simp only [mem_univ, forall_true_iff])
(by simp only [is_submonoid.coe_pow, eq_self_iff_true, units.coe_pow, coe_coe, forall_true_iff])
(λ m n hm hn, pow_injective_of_lt_order_of _
(by simpa only [mem_range] using hm)
(by simpa only [mem_range] using hn))
(λ b hb, let ⟨n, hn⟩ := hx b in ⟨n % order_of x, mem_range.2 (nat.mod_lt _ (order_of_pos _)),
by rw [← pow_eq_mod_order_of, hn]⟩)
... = 0 : _,
rw [← mul_left_inj' hx1, zero_mul, ← geom_series, geom_sum_mul, coe_coe],
norm_cast,
rw [pow_order_of_eq_one, is_submonoid.coe_one, units.coe_one, sub_self],
end
/-- In an integral domain, a sum indexed by a homomorphism from a finite group is zero,
unless the homomorphism is trivial, in which case the sum is equal to the cardinality of the group. -/
lemma sum_hom_units (f : G →* R) [decidable (f = 1)] :
∑ g : G, f g = if f = 1 then fintype.card G else 0 :=
begin
split_ifs with h h,
{ simp [h, card_univ] },
{ exact sum_hom_units_eq_zero f h }
end
lemma left_dvd_or_dvd_right_of_dvd_prime_mul {a : R} :
∀ {b p : R}, prime p → a ∣ p * b → p ∣ a ∨ a ∣ b :=
begin
rintros b p hp ⟨c, hc⟩,
rcases hp.2.2 a c (hc ▸ dvd_mul_right _ _) with h | ⟨x, rfl⟩,
{ exact or.inl h },
{ rw [mul_left_comm, mul_right_inj' hp.ne_zero] at hc,
exact or.inr (hc.symm ▸ dvd_mul_right _ _) }
end
end
|
97693cc3973b7105fc1925c5329901a3ff79931d | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/algebra/indicator_function.lean | e338cc654fd357080cbb4329285beee5dae77345 | [
"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 | 19,972 | lean | /-
Copyright (c) 2020 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou
-/
import algebra.group.pi
import group_theory.group_action
import algebra.support
import data.finset.lattice
/-!
# Indicator function
- `indicator (s : set α) (f : α → β) (a : α)` is `f a` if `a ∈ s` and is `0` otherwise.
- `mul_indicator (s : set α) (f : α → β) (a : α)` is `f a` if `a ∈ s` and is `1` otherwise.
## Implementation note
In mathematics, an indicator function or a characteristic function is a function
used to indicate membership of an element in a set `s`,
having the value `1` for all elements of `s` and the value `0` otherwise.
But since it is usually used to restrict a function to a certain set `s`,
we let the indicator function take the value `f x` for some function `f`, instead of `1`.
If the usual indicator function is needed, just set `f` to be the constant function `λx, 1`.
## Tags
indicator, characteristic
-/
noncomputable theory
open_locale classical big_operators
open function
variables {α β ι M N : Type*}
namespace set
section has_one
variables [has_one M] [has_one N] {s t : set α} {f g : α → M} {a : α}
/-- `indicator s f a` is `f a` if `a ∈ s`, `0` otherwise. -/
def indicator {M} [has_zero M] (s : set α) (f : α → M) : α → M := λ x, if x ∈ s then f x else 0
/-- `mul_indicator s f a` is `f a` if `a ∈ s`, `1` otherwise. -/
@[to_additive]
def mul_indicator (s : set α) (f : α → M) : α → M := λ x, if x ∈ s then f x else 1
@[simp, to_additive] lemma piecewise_eq_mul_indicator : s.piecewise f 1 = s.mul_indicator f := rfl
@[to_additive] lemma mul_indicator_apply (s : set α) (f : α → M) (a : α) :
mul_indicator s f a = if a ∈ s then f a else 1 := rfl
@[simp, to_additive] lemma mul_indicator_of_mem (h : a ∈ s) (f : α → M) :
mul_indicator s f a = f a := if_pos h
@[simp, to_additive] lemma mul_indicator_of_not_mem (h : a ∉ s) (f : α → M) :
mul_indicator s f a = 1 := if_neg h
@[to_additive] lemma mul_indicator_eq_one_or_self (s : set α) (f : α → M) (a : α) :
mul_indicator s f a = 1 ∨ mul_indicator s f a = f a :=
if h : a ∈ s then or.inr (mul_indicator_of_mem h f) else or.inl (mul_indicator_of_not_mem h f)
@[simp, to_additive] lemma mul_indicator_apply_eq_self :
s.mul_indicator f a = f a ↔ (a ∉ s → f a = 1) :=
ite_eq_left_iff.trans $ by rw [@eq_comm _ (f a)]
@[simp, to_additive] lemma mul_indicator_eq_self : s.mul_indicator f = f ↔ mul_support f ⊆ s :=
by simp only [funext_iff, subset_def, mem_mul_support, mul_indicator_apply_eq_self, not_imp_comm]
@[to_additive] lemma mul_indicator_eq_self_of_superset (h1 : s.mul_indicator f = f) (h2 : s ⊆ t) :
t.mul_indicator f = f :=
by { rw mul_indicator_eq_self at h1 ⊢, exact subset.trans h1 h2 }
@[simp, to_additive] lemma mul_indicator_apply_eq_one :
mul_indicator s f a = 1 ↔ (a ∈ s → f a = 1) :=
ite_eq_right_iff
@[simp, to_additive] lemma mul_indicator_eq_one :
mul_indicator s f = (λ x, 1) ↔ disjoint (mul_support f) s :=
by simp only [funext_iff, mul_indicator_apply_eq_one, set.disjoint_left, mem_mul_support,
not_imp_not]
@[simp, to_additive] lemma mul_indicator_eq_one' :
mul_indicator s f = 1 ↔ disjoint (mul_support f) s :=
mul_indicator_eq_one
@[simp, to_additive] lemma mul_support_mul_indicator :
function.mul_support (s.mul_indicator f) = s ∩ function.mul_support f :=
ext $ λ x, by simp [function.mem_mul_support, mul_indicator_apply_eq_one]
/-- If a multiplicative indicator function is not equal to one at a point, then that
point is in the set. -/
@[to_additive] lemma mem_of_mul_indicator_ne_one (h : mul_indicator s f a ≠ 1) : a ∈ s :=
not_imp_comm.1 (λ hn, mul_indicator_of_not_mem hn f) h
@[to_additive] lemma eq_on_mul_indicator : eq_on (mul_indicator s f) f s :=
λ x hx, mul_indicator_of_mem hx f
@[to_additive] lemma mul_support_mul_indicator_subset : mul_support (s.mul_indicator f) ⊆ s :=
λ x hx, hx.imp_symm (λ h, mul_indicator_of_not_mem h f)
@[simp, to_additive] lemma mul_indicator_mul_support : mul_indicator (mul_support f) f = f :=
mul_indicator_eq_self.2 subset.rfl
@[simp, to_additive] lemma mul_indicator_range_comp {ι : Sort*} (f : ι → α) (g : α → M) :
mul_indicator (range f) g ∘ f = g ∘ f :=
piecewise_range_comp _ _ _
@[to_additive] lemma mul_indicator_congr (h : eq_on f g s) :
mul_indicator s f = mul_indicator s g :=
funext $ λx, by { simp only [mul_indicator], split_ifs, { exact h h_1 }, refl }
@[simp, to_additive] lemma mul_indicator_univ (f : α → M) : mul_indicator (univ : set α) f = f :=
mul_indicator_eq_self.2 $ subset_univ _
@[simp, to_additive] lemma mul_indicator_empty (f : α → M) : mul_indicator (∅ : set α) f = λa, 1 :=
mul_indicator_eq_one.2 $ disjoint_empty _
variable (M)
@[simp, to_additive] lemma mul_indicator_one (s : set α) :
mul_indicator s (λx, (1:M)) = λx, (1:M) :=
mul_indicator_eq_one.2 $ by simp only [mul_support_one, empty_disjoint]
@[simp, to_additive] lemma mul_indicator_one' {s : set α} : s.mul_indicator (1 : α → M) = 1 :=
mul_indicator_one M s
variable {M}
@[to_additive] lemma mul_indicator_mul_indicator (s t : set α) (f : α → M) :
mul_indicator s (mul_indicator t f) = mul_indicator (s ∩ t) f :=
funext $ λx, by { simp only [mul_indicator], split_ifs, repeat {simp * at * {contextual := tt}} }
@[simp, to_additive] lemma mul_indicator_inter_mul_support (s : set α) (f : α → M) :
mul_indicator (s ∩ mul_support f) f = mul_indicator s f :=
by rw [← mul_indicator_mul_indicator, mul_indicator_mul_support]
@[to_additive] lemma comp_mul_indicator (h : M → β) (f : α → M) {s : set α} {x : α} :
h (s.mul_indicator f x) = s.piecewise (h ∘ f) (const α (h 1)) x :=
s.apply_piecewise _ _ (λ _, h)
@[to_additive] lemma mul_indicator_comp_right {s : set α} (f : β → α) {g : α → M} {x : β} :
mul_indicator (f ⁻¹' s) (g ∘ f) x = mul_indicator s g (f x) :=
by { simp only [mul_indicator], split_ifs; refl }
@[to_additive] lemma mul_indicator_comp_of_one {g : M → N} (hg : g 1 = 1) :
mul_indicator s (g ∘ f) = g ∘ (mul_indicator s f) :=
begin
funext,
simp only [mul_indicator],
split_ifs; simp [*]
end
@[to_additive] lemma mul_indicator_preimage (s : set α) (f : α → M) (B : set M) :
(mul_indicator s f)⁻¹' B = s.ite (f ⁻¹' B) (1 ⁻¹' B) :=
piecewise_preimage s f 1 B
@[to_additive] lemma mul_indicator_preimage_of_not_mem (s : set α) (f : α → M)
{t : set M} (ht : (1:M) ∉ t) :
(mul_indicator s f)⁻¹' t = f ⁻¹' t ∩ s :=
by simp [mul_indicator_preimage, pi.one_def, set.preimage_const_of_not_mem ht]
@[to_additive] lemma mem_range_mul_indicator {r : M} {s : set α} {f : α → M} :
r ∈ range (mul_indicator s f) ↔ (r = 1 ∧ s ≠ univ) ∨ (r ∈ f '' s) :=
by simp [mul_indicator, ite_eq_iff, exists_or_distrib, eq_univ_iff_forall, and_comm, or_comm,
@eq_comm _ r 1]
@[to_additive] lemma mul_indicator_rel_mul_indicator {r : M → M → Prop} (h1 : r 1 1)
(ha : a ∈ s → r (f a) (g a)) :
r (mul_indicator s f a) (mul_indicator s g a) :=
by { simp only [mul_indicator], split_ifs with has has, exacts [ha has, h1] }
end has_one
section monoid
variables [mul_one_class M] {s t : set α} {f g : α → M} {a : α}
@[to_additive] lemma mul_indicator_union_mul_inter_apply (f : α → M) (s t : set α) (a : α) :
mul_indicator (s ∪ t) f a * mul_indicator (s ∩ t) f a =
mul_indicator s f a * mul_indicator t f a :=
by by_cases hs : a ∈ s; by_cases ht : a ∈ t; simp *
@[to_additive] lemma mul_indicator_union_mul_inter (f : α → M) (s t : set α) :
mul_indicator (s ∪ t) f * mul_indicator (s ∩ t) f = mul_indicator s f * mul_indicator t f :=
funext $ mul_indicator_union_mul_inter_apply f s t
@[to_additive] lemma mul_indicator_union_of_not_mem_inter (h : a ∉ s ∩ t) (f : α → M) :
mul_indicator (s ∪ t) f a = mul_indicator s f a * mul_indicator t f a :=
by rw [← mul_indicator_union_mul_inter_apply f s t, mul_indicator_of_not_mem h, mul_one]
@[to_additive] lemma mul_indicator_union_of_disjoint (h : disjoint s t) (f : α → M) :
mul_indicator (s ∪ t) f = λa, mul_indicator s f a * mul_indicator t f a :=
funext $ λa, mul_indicator_union_of_not_mem_inter (λ ha, h ha) _
@[to_additive] lemma mul_indicator_mul (s : set α) (f g : α → M) :
mul_indicator s (λa, f a * g a) = λa, mul_indicator s f a * mul_indicator s g a :=
by { funext, simp only [mul_indicator], split_ifs, { refl }, rw mul_one }
@[simp, to_additive] lemma mul_indicator_compl_mul_self_apply (s : set α) (f : α → M) (a : α) :
mul_indicator sᶜ f a * mul_indicator s f a = f a :=
classical.by_cases (λ ha : a ∈ s, by simp [ha]) (λ ha, by simp [ha])
@[simp, to_additive] lemma mul_indicator_compl_mul_self (s : set α) (f : α → M) :
mul_indicator sᶜ f * mul_indicator s f = f :=
funext $ mul_indicator_compl_mul_self_apply s f
@[simp, to_additive] lemma mul_indicator_self_mul_compl_apply (s : set α) (f : α → M) (a : α) :
mul_indicator s f a * mul_indicator sᶜ f a = f a :=
classical.by_cases (λ ha : a ∈ s, by simp [ha]) (λ ha, by simp [ha])
@[simp, to_additive] lemma mul_indicator_self_mul_compl (s : set α) (f : α → M) :
mul_indicator s f * mul_indicator sᶜ f = f :=
funext $ mul_indicator_self_mul_compl_apply s f
@[to_additive] lemma mul_indicator_mul_eq_left {f g : α → M}
(h : disjoint (mul_support f) (mul_support g)) :
(mul_support f).mul_indicator (f * g) = f :=
begin
refine (mul_indicator_congr $ λ x hx, _).trans mul_indicator_mul_support,
have : g x = 1, from nmem_mul_support.1 (disjoint_left.1 h hx),
rw [pi.mul_apply, this, mul_one]
end
@[to_additive] lemma mul_indicator_mul_eq_right {f g : α → M}
(h : disjoint (mul_support f) (mul_support g)) :
(mul_support g).mul_indicator (f * g) = g :=
begin
refine (mul_indicator_congr $ λ x hx, _).trans mul_indicator_mul_support,
have : f x = 1, from nmem_mul_support.1 (disjoint_right.1 h hx),
rw [pi.mul_apply, this, one_mul]
end
/-- `set.mul_indicator` as a `monoid_hom`. -/
@[to_additive "`set.indicator` as an `add_monoid_hom`."]
def mul_indicator_hom {α} (M) [mul_one_class M] (s : set α) : (α → M) →* (α → M) :=
{ to_fun := mul_indicator s,
map_one' := mul_indicator_one M s,
map_mul' := mul_indicator_mul s }
end monoid
section distrib_mul_action
variables {A : Type*} [add_monoid A] [monoid M] [distrib_mul_action M A]
lemma indicator_smul (s : set α) (r : M) (f : α → A) :
indicator s (λ (x : α), r • f x) = λ (x : α), r • indicator s f x :=
by { simp only [indicator], funext, split_ifs, refl, exact (smul_zero r).symm }
end distrib_mul_action
section group
variables {G : Type*} [group G] {s t : set α} {f g : α → G} {a : α}
@[to_additive] lemma mul_indicator_inv' (s : set α) (f : α → G) :
mul_indicator s (f⁻¹) = (mul_indicator s f)⁻¹ :=
(mul_indicator_hom G s).map_inv f
@[to_additive] lemma mul_indicator_inv (s : set α) (f : α → G) :
mul_indicator s (λa, (f a)⁻¹) = λa, (mul_indicator s f a)⁻¹ :=
mul_indicator_inv' s f
lemma indicator_sub {G} [add_group G] (s : set α) (f g : α → G) :
indicator s (λa, f a - g a) = λa, indicator s f a - indicator s g a :=
(indicator_hom G s).map_sub f g
@[to_additive indicator_compl'] lemma mul_indicator_compl (s : set α) (f : α → G) :
mul_indicator sᶜ f = f * (mul_indicator s f)⁻¹ :=
eq_mul_inv_of_mul_eq $ s.mul_indicator_compl_mul_self f
lemma indicator_compl {G} [add_group G] (s : set α) (f : α → G) :
indicator sᶜ f = f - indicator s f :=
by rw [sub_eq_add_neg, indicator_compl']
@[to_additive indicator_diff'] lemma mul_indicator_diff (h : s ⊆ t) (f : α → G) :
mul_indicator (t \ s) f = mul_indicator t f * (mul_indicator s f)⁻¹ :=
eq_mul_inv_of_mul_eq $ by rw [pi.mul_def, ← mul_indicator_union_of_disjoint disjoint_diff.symm f,
diff_union_self, union_eq_self_of_subset_right h]
lemma indicator_diff {G : Type*} [add_group G] {s t : set α} (h : s ⊆ t) (f : α → G) :
indicator (t \ s) f = indicator t f - indicator s f :=
by rw [indicator_diff' h, sub_eq_add_neg]
end group
section comm_monoid
variables [comm_monoid M]
/-- Consider a product of `g i (f i)` over a `finset`. Suppose `g` is a
function such as `pow`, which maps a second argument of `1` to
`1`. Then if `f` is replaced by the corresponding multiplicative indicator
function, the `finset` may be replaced by a possibly larger `finset`
without changing the value of the sum. -/
@[to_additive] lemma prod_mul_indicator_subset_of_eq_one [has_one N] (f : α → N)
(g : α → N → M) {s t : finset α} (h : s ⊆ t) (hg : ∀ a, g a 1 = 1) :
∏ i in s, g i (f i) = ∏ i in t, g i (mul_indicator ↑s f i) :=
begin
rw ← finset.prod_subset h _,
{ apply finset.prod_congr rfl,
intros i hi,
congr,
symmetry,
exact mul_indicator_of_mem hi _ },
{ refine λ i hi hn, _,
convert hg i,
exact mul_indicator_of_not_mem hn _ }
end
/-- Consider a sum of `g i (f i)` over a `finset`. Suppose `g` is a
function such as multiplication, which maps a second argument of 0 to
0. (A typical use case would be a weighted sum of `f i * h i` or `f i
• h i`, where `f` gives the weights that are multiplied by some other
function `h`.) Then if `f` is replaced by the corresponding indicator
function, the `finset` may be replaced by a possibly larger `finset`
without changing the value of the sum. -/
add_decl_doc set.sum_indicator_subset_of_eq_zero
@[to_additive] lemma prod_mul_indicator_subset (f : α → M) {s t : finset α} (h : s ⊆ t) :
∏ i in s, f i = ∏ i in t, mul_indicator ↑s f i :=
prod_mul_indicator_subset_of_eq_one _ (λ a b, b) h (λ _, rfl)
/-- Summing an indicator function over a possibly larger `finset` is
the same as summing the original function over the original
`finset`. -/
add_decl_doc sum_indicator_subset
@[to_additive] lemma mul_indicator_finset_prod (I : finset ι) (s : set α) (f : ι → α → M) :
mul_indicator s (∏ i in I, f i) = ∏ i in I, mul_indicator s (f i) :=
(mul_indicator_hom M s).map_prod _ _
@[to_additive] lemma mul_indicator_finset_bUnion {ι} (I : finset ι)
(s : ι → set α) {f : α → M} : (∀ (i ∈ I) (j ∈ I), i ≠ j → disjoint (s i) (s j)) →
mul_indicator (⋃ i ∈ I, s i) f = λ a, ∏ i in I, mul_indicator (s i) f a :=
begin
refine finset.induction_on I _ _,
{ intro h, funext, simp },
assume a I haI ih hI,
funext,
rw [finset.prod_insert haI, finset.set_bUnion_insert, mul_indicator_union_of_not_mem_inter, ih _],
{ assume i hi j hj hij,
exact hI i (finset.mem_insert_of_mem hi) j (finset.mem_insert_of_mem hj) hij },
simp only [not_exists, exists_prop, mem_Union, mem_inter_eq, not_and],
assume hx a' ha',
refine disjoint_left.1 (hI a (finset.mem_insert_self _ _) a' (finset.mem_insert_of_mem ha') _) hx,
exact (ne_of_mem_of_not_mem ha' haI).symm
end
end comm_monoid
section mul_zero_class
variables [mul_zero_class M] {s t : set α} {f g : α → M} {a : α}
lemma indicator_mul (s : set α) (f g : α → M) :
indicator s (λa, f a * g a) = λa, indicator s f a * indicator s g a :=
by { funext, simp only [indicator], split_ifs, { refl }, rw mul_zero }
lemma indicator_mul_left (s : set α) (f g : α → M) :
indicator s (λa, f a * g a) a = indicator s f a * g a :=
by { simp only [indicator], split_ifs, { refl }, rw [zero_mul] }
lemma indicator_mul_right (s : set α) (f g : α → M) :
indicator s (λa, f a * g a) a = f a * indicator s g a :=
by { simp only [indicator], split_ifs, { refl }, rw [mul_zero] }
lemma inter_indicator_mul {t1 t2 : set α} (f g : α → M) (x : α) :
(t1 ∩ t2).indicator (λ x, f x * g x) x = t1.indicator f x * t2.indicator g x :=
by { rw [← set.indicator_indicator], simp [indicator] }
end mul_zero_class
section monoid_with_zero
variables [monoid_with_zero M]
lemma indicator_prod_one {s : set α} {t : set β} {x : α} {y : β} :
(s.prod t).indicator (1 : _ → M) (x, y) = s.indicator 1 x * t.indicator 1 y :=
by simp [indicator, ← ite_and]
end monoid_with_zero
section order
variables [has_one M] [preorder M] {s t : set α} {f g : α → M} {a : α} {y : M}
@[to_additive] lemma mul_indicator_apply_le' (hfg : a ∈ s → f a ≤ y) (hg : a ∉ s → 1 ≤ y) :
mul_indicator s f a ≤ y :=
if ha : a ∈ s then by simpa [ha] using hfg ha else by simpa [ha] using hg ha
@[to_additive] lemma mul_indicator_le' (hfg : ∀ a ∈ s, f a ≤ g a) (hg : ∀ a ∉ s, 1 ≤ g a) :
mul_indicator s f ≤ g :=
λ a, mul_indicator_apply_le' (hfg _) (hg _)
@[to_additive] lemma le_mul_indicator_apply {y} (hfg : a ∈ s → y ≤ g a) (hf : a ∉ s → y ≤ 1) :
y ≤ mul_indicator s g a :=
@mul_indicator_apply_le' α (order_dual M) ‹_› _ _ _ _ _ hfg hf
@[to_additive] lemma le_mul_indicator (hfg : ∀ a ∈ s, f a ≤ g a) (hf : ∀ a ∉ s, f a ≤ 1) :
f ≤ mul_indicator s g :=
λ a, le_mul_indicator_apply (hfg _) (hf _)
@[to_additive indicator_apply_nonneg]
lemma one_le_mul_indicator_apply (h : a ∈ s → 1 ≤ f a) : 1 ≤ mul_indicator s f a :=
le_mul_indicator_apply h (λ _, le_rfl)
@[to_additive indicator_nonneg]
lemma one_le_mul_indicator (h : ∀ a ∈ s, 1 ≤ f a) (a : α) : 1 ≤ mul_indicator s f a :=
one_le_mul_indicator_apply (h a)
@[to_additive] lemma mul_indicator_apply_le_one (h : a ∈ s → f a ≤ 1) : mul_indicator s f a ≤ 1 :=
mul_indicator_apply_le' h (λ _, le_rfl)
@[to_additive] lemma mul_indicator_le_one (h : ∀ a ∈ s, f a ≤ 1) (a : α) :
mul_indicator s f a ≤ 1 :=
mul_indicator_apply_le_one (h a)
@[to_additive] lemma mul_indicator_le_mul_indicator (h : f a ≤ g a) :
mul_indicator s f a ≤ mul_indicator s g a :=
mul_indicator_rel_mul_indicator (le_refl _) (λ _, h)
attribute [mono] mul_indicator_le_mul_indicator indicator_le_indicator
@[to_additive] lemma mul_indicator_le_mul_indicator_of_subset (h : s ⊆ t) (hf : ∀ a, 1 ≤ f a)
(a : α) :
mul_indicator s f a ≤ mul_indicator t f a :=
mul_indicator_apply_le' (λ ha, le_mul_indicator_apply (λ _, le_rfl) (λ hat, (hat $ h ha).elim))
(λ ha, one_le_mul_indicator_apply (λ _, hf _))
@[to_additive] lemma mul_indicator_le_self' (hf : ∀ x ∉ s, 1 ≤ f x) : mul_indicator s f ≤ f :=
mul_indicator_le' (λ _ _, le_refl _) hf
@[to_additive] lemma mul_indicator_Union_apply {ι M} [complete_lattice M] [has_one M]
(h1 : (⊥:M) = 1) (s : ι → set α) (f : α → M) (x : α) :
mul_indicator (⋃ i, s i) f x = ⨆ i, mul_indicator (s i) f x :=
begin
by_cases hx : x ∈ ⋃ i, s i,
{ rw [mul_indicator_of_mem hx],
rw [mem_Union] at hx,
refine le_antisymm _ (supr_le $ λ i, mul_indicator_le_self' (λ x hx, h1 ▸ bot_le) x),
rcases hx with ⟨i, hi⟩,
exact le_supr_of_le i (ge_of_eq $ mul_indicator_of_mem hi _) },
{ rw [mul_indicator_of_not_mem hx],
simp only [mem_Union, not_exists] at hx,
simp [hx, ← h1] }
end
end order
section canonically_ordered_monoid
variables [canonically_ordered_monoid M]
@[to_additive] lemma mul_indicator_le_self (s : set α) (f : α → M) :
mul_indicator s f ≤ f :=
mul_indicator_le_self' $ λ _ _, one_le _
@[to_additive] lemma mul_indicator_apply_le {a : α} {s : set α} {f g : α → M}
(hfg : a ∈ s → f a ≤ g a) :
mul_indicator s f a ≤ g a :=
mul_indicator_apply_le' hfg $ λ _, one_le _
@[to_additive] lemma mul_indicator_le {s : set α} {f g : α → M} (hfg : ∀ a ∈ s, f a ≤ g a) :
mul_indicator s f ≤ g :=
mul_indicator_le' hfg $ λ _ _, one_le _
end canonically_ordered_monoid
end set
@[to_additive] lemma monoid_hom.map_mul_indicator {M N : Type*} [monoid M] [monoid N] (f : M →* N)
(s : set α) (g : α → M) (x : α) :
f (s.mul_indicator g x) = s.mul_indicator (f ∘ g) x :=
congr_fun (set.mul_indicator_comp_of_one f.map_one).symm x
|
b6e0566928854d608aeac9b18cce2e05a484f1f5 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/simpArith1.lean | 31c2a1dc4f5efe0e76edc9e64908203ab4a6142c | [
"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 | 558 | lean | theorem ex1 : a + b < b + 1 + a + c := by
simp (config := { arith := true })
theorem ex2 : a + b < b + 1 + a + c := by
simp_arith
theorem ex3 : a + (fun x => x) b < b + 1 + a + c := by
simp_arith
theorem ex4 : a + (fun x => x) b < b + 1 + a + c := by
simp_arith (config := { beta := false })
trace_state
simp_arith
theorem ex5 (h : a + d + b > b + 1 + (a + (c + c) + d)) : False := by
simp_arith at h
#print ex5
theorem ex6 (p : Nat → Prop) (h : p (a + 1 + a + 2 + b)) : p (2*a + b + 3) := by
simp_arith at h
assumption
#print ex6
|
31c305813fb0f2142f4b4c1789442310e9f72856 | c09f5945267fd905e23a77be83d9a78580e04a4a | /src/data/finset.lean | 47f9e5dfc58a28ebdefa402c7a22140272654ef9 | [
"Apache-2.0"
] | permissive | OHIHIYA20/mathlib | 023a6df35355b5b6eb931c404f7dd7535dccfa89 | 1ec0a1f49db97d45e8666a3bf33217ff79ca1d87 | refs/heads/master | 1,587,964,529,965 | 1,551,819,319,000 | 1,551,819,319,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 79,951 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro
Finite sets.
-/
import logic.embedding order.boolean_algebra algebra.order_functions
data.multiset data.sigma.basic data.set.lattice
open multiset subtype nat lattice
variables {α : Type*} {β : Type*} {γ : Type*}
/-- `finset α` is the type of finite sets of elements of `α`. It is implemented
as a multiset (a list up to permutation) which has no duplicate elements. -/
structure finset (α : Type*) :=
(val : multiset α)
(nodup : nodup val)
namespace finset
theorem eq_of_veq : ∀ {s t : finset α}, s.1 = t.1 → s = t
| ⟨s, _⟩ ⟨t, _⟩ rfl := rfl
@[simp] theorem val_inj {s t : finset α} : s.1 = t.1 ↔ s = t :=
⟨eq_of_veq, congr_arg _⟩
@[simp] theorem erase_dup_eq_self [decidable_eq α] (s : finset α) : erase_dup s.1 = s.1 :=
erase_dup_eq_self.2 s.2
instance has_decidable_eq [decidable_eq α] : decidable_eq (finset α)
| s₁ s₂ := decidable_of_iff _ val_inj
/- membership -/
instance : has_mem α (finset α) := ⟨λ a s, a ∈ s.1⟩
theorem mem_def {a : α} {s : finset α} : a ∈ s ↔ a ∈ s.1 := iff.rfl
@[simp] theorem mem_mk {a : α} {s nd} : a ∈ @finset.mk α s nd ↔ a ∈ s := iff.rfl
instance decidable_mem [h : decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ s) :=
multiset.decidable_mem _ _
/- set coercion -/
/-- Convert a finset to a set in the natural way. -/
def to_set (s : finset α) : set α := {x | x ∈ s}
instance : has_lift (finset α) (set α) := ⟨to_set⟩
@[simp] lemma mem_coe {a : α} {s : finset α} : a ∈ (↑s : set α) ↔ a ∈ s := iff.rfl
@[simp] lemma set_of_mem {α} {s : finset α} : {a | a ∈ s} = ↑s := rfl
/- extensionality -/
theorem ext {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ :=
val_inj.symm.trans $ nodup_ext s₁.2 s₂.2
@[extensionality]
theorem ext' {s₁ s₂ : finset α} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ :=
ext.2
@[simp] theorem coe_inj {s₁ s₂ : finset α} : (↑s₁ : set α) = ↑s₂ ↔ s₁ = s₂ :=
(set.ext_iff _ _).trans ext.symm
/- subset -/
instance : has_subset (finset α) := ⟨λ s₁ s₂, ∀ ⦃a⦄, a ∈ s₁ → a ∈ s₂⟩
theorem subset_def {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ s₁.1 ⊆ s₂.1 := iff.rfl
@[simp] theorem subset.refl (s : finset α) : s ⊆ s := subset.refl _
theorem subset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := subset.trans
theorem mem_of_subset {s₁ s₂ : finset α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := mem_of_subset
theorem subset.antisymm {s₁ s₂ : finset α} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ :=
ext.2 $ λ a, ⟨@H₁ a, @H₂ a⟩
theorem subset_iff {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ ∀ ⦃x⦄, x ∈ s₁ → x ∈ s₂ := iff.rfl
@[simp] theorem coe_subset {s₁ s₂ : finset α} :
(↑s₁ : set α) ⊆ ↑s₂ ↔ s₁ ⊆ s₂ := iff.rfl
@[simp] theorem val_le_iff {s₁ s₂ : finset α} : s₁.1 ≤ s₂.1 ↔ s₁ ⊆ s₂ := le_iff_subset s₁.2
instance : has_ssubset (finset α) := ⟨λa b, a ⊆ b ∧ ¬ b ⊆ a⟩
instance : partial_order (finset α) :=
{ le := (⊆),
lt := (⊂),
le_refl := subset.refl,
le_trans := @subset.trans _,
le_antisymm := @subset.antisymm _ }
theorem subset.antisymm_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ s₁ ⊆ s₂ ∧ s₂ ⊆ s₁ :=
le_antisymm_iff
@[simp] theorem le_iff_subset {s₁ s₂ : finset α} : s₁ ≤ s₂ ↔ s₁ ⊆ s₂ := iff.rfl
@[simp] theorem lt_iff_ssubset {s₁ s₂ : finset α} : s₁ < s₂ ↔ s₁ ⊂ s₂ := iff.rfl
@[simp] lemma coe_ssubset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊂ s₂ :=
show (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊆ s₂ ∧ ¬s₂ ⊆ s₁,
by simp only [set.ssubset_iff_subset_not_subset, finset.coe_subset]
@[simp] theorem val_lt_iff {s₁ s₂ : finset α} : s₁.1 < s₂.1 ↔ s₁ ⊂ s₂ :=
and_congr val_le_iff $ not_congr val_le_iff
/- empty -/
protected def empty : finset α := ⟨0, nodup_zero⟩
instance : has_emptyc (finset α) := ⟨finset.empty⟩
instance : inhabited (finset α) := ⟨∅⟩
@[simp] theorem empty_val : (∅ : finset α).1 = 0 := rfl
@[simp] theorem not_mem_empty (a : α) : a ∉ (∅ : finset α) := id
@[simp] theorem ne_empty_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ≠ ∅
| e := not_mem_empty a $ e ▸ h
@[simp] theorem empty_subset (s : finset α) : ∅ ⊆ s := zero_subset _
theorem eq_empty_of_forall_not_mem {s : finset α} (H : ∀x, x ∉ s) : s = ∅ :=
eq_of_veq (eq_zero_of_forall_not_mem H)
lemma eq_empty_iff_forall_not_mem {s : finset α} : s = ∅ ↔ ∀ x, x ∉ s :=
⟨by rintro rfl x; exact id, λ h, eq_empty_of_forall_not_mem h⟩
@[simp] theorem val_eq_zero {s : finset α} : s.1 = 0 ↔ s = ∅ := @val_inj _ s ∅
theorem subset_empty {s : finset α} : s ⊆ ∅ ↔ s = ∅ := subset_zero.trans val_eq_zero
theorem exists_mem_of_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a : α, a ∈ s :=
exists_mem_of_ne_zero (mt val_eq_zero.1 h)
@[simp] lemma coe_empty : ↑(∅ : finset α) = (∅ : set α) := rfl
/-- `singleton a` is the set `{a}` containing `a` and nothing else. -/
def singleton (a : α) : finset α := ⟨_, nodup_singleton a⟩
local prefix `ι`:90 := singleton
@[simp] theorem singleton_val (a : α) : (ι a).1 = a :: 0 := rfl
@[simp] theorem mem_singleton {a b : α} : b ∈ ι a ↔ b = a := mem_singleton
theorem not_mem_singleton {a b : α} : a ∉ ι b ↔ a ≠ b := not_iff_not_of_iff mem_singleton
theorem mem_singleton_self (a : α) : a ∈ ι a := or.inl rfl
theorem singleton_inj {a b : α} : ι a = ι b ↔ a = b :=
⟨λ h, mem_singleton.1 (h ▸ mem_singleton_self _), congr_arg _⟩
@[simp] theorem singleton_ne_empty (a : α) : ι a ≠ ∅ := ne_empty_of_mem (mem_singleton_self _)
@[simp] lemma coe_singleton (a : α) : ↑(ι a) = ({a} : set α) := rfl
/- insert -/
section decidable_eq
variables [decidable_eq α]
/-- `insert a s` is the set `{a} ∪ s` containing `a` and the elements of `s`. -/
instance : has_insert α (finset α) := ⟨λ a s, ⟨_, nodup_ndinsert a s.2⟩⟩
@[simp] theorem has_insert_eq_insert (a : α) (s : finset α) : has_insert.insert a s = insert a s := rfl
theorem insert_def (a : α) (s : finset α) : insert a s = ⟨_, nodup_ndinsert a s.2⟩ := rfl
@[simp] theorem insert_val (a : α) (s : finset α) : (insert a s).1 = ndinsert a s.1 := rfl
theorem insert_val' (a : α) (s : finset α) : (insert a s).1 = erase_dup (a :: s.1) :=
by rw [erase_dup_cons, erase_dup_eq_self]; refl
theorem insert_val_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : (insert a s).1 = a :: s.1 :=
by rw [insert_val, ndinsert_of_not_mem h]
@[simp] theorem mem_insert {a b : α} {s : finset α} : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert
theorem mem_insert_self (a : α) (s : finset α) : a ∈ insert a s := mem_ndinsert_self a s.1
theorem mem_insert_of_mem {a b : α} {s : finset α} (h : a ∈ s) : a ∈ insert b s := mem_ndinsert_of_mem h
theorem mem_of_mem_insert_of_ne {a b : α} {s : finset α} (h : b ∈ insert a s) : b ≠ a → b ∈ s :=
(mem_insert.1 h).resolve_left
@[simp] lemma coe_insert (a : α) (s : finset α) : ↑(insert a s) = (insert a ↑s : set α) :=
set.ext $ λ x, by simp only [mem_coe, mem_insert, set.mem_insert_iff]
@[simp] theorem insert_eq_of_mem {a : α} {s : finset α} (h : a ∈ s) : insert a s = s :=
eq_of_veq $ ndinsert_of_mem h
theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) :=
ext.2 $ λ x, by simp only [finset.mem_insert, or.left_comm]
@[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s :=
ext.2 $ λ x, by simp only [finset.mem_insert, or.assoc.symm, or_self]
@[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ :=
ne_empty_of_mem (mem_insert_self a s)
theorem insert_subset {a : α} {s t : finset α} : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t :=
by simp only [subset_iff, mem_insert, forall_eq, or_imp_distrib, forall_and_distrib]
theorem subset_insert [h : decidable_eq α] (a : α) (s : finset α) : s ⊆ insert a s :=
λ b, mem_insert_of_mem
theorem insert_subset_insert (a : α) {s t : finset α} (h : s ⊆ t) : insert a s ⊆ insert a t :=
insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩
lemma ssubset_iff {s t : finset α} : s ⊂ t ↔ (∃a, a ∉ s ∧ insert a s ⊆ t) :=
iff.intro
(assume ⟨h₁, h₂⟩,
have ∃a ∈ t, a ∉ s, by simpa only [finset.subset_iff, classical.not_forall] using h₂,
let ⟨a, hat, has⟩ := this in ⟨a, has, insert_subset.mpr ⟨hat, h₁⟩⟩)
(assume ⟨a, hat, has⟩,
let ⟨h₁, h₂⟩ := insert_subset.mp has in
⟨h₂, assume h, hat $ h h₁⟩)
lemma ssubset_insert {s : finset α} {a : α} (h : a ∉ s) : s ⊂ insert a s :=
ssubset_iff.mpr ⟨a, h, subset.refl _⟩
@[recursor 6] protected theorem induction {α : Type*} {p : finset α → Prop} [decidable_eq α]
(h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : ∀ s, p s
| ⟨s, nd⟩ := multiset.induction_on s (λ _, h₁) (λ a s IH nd, begin
cases nodup_cons.1 nd with m nd',
rw [← (eq_of_veq _ : insert a (finset.mk s _) = ⟨a::s, nd⟩)],
{ exact h₂ (by exact m) (IH nd') },
{ rw [insert_val, ndinsert_of_not_mem m] }
end) nd
@[elab_as_eliminator] protected theorem induction_on {α : Type*} {p : finset α → Prop} [decidable_eq α]
(s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : p s :=
finset.induction h₁ h₂ s
@[simp] theorem singleton_eq_singleton (a : α) : _root_.singleton a = ι a := rfl
@[simp] theorem insert_empty_eq_singleton (a : α) : {a} = ι a := rfl
@[simp] theorem insert_singleton_self_eq (a : α) : ({a, a} : finset α) = ι a :=
insert_eq_of_mem $ mem_singleton_self _
/- union -/
/-- `s ∪ t` is the set such that `a ∈ s ∪ t` iff `a ∈ s` or `a ∈ t`. -/
instance : has_union (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndunion s₁.1 s₂.2⟩⟩
theorem union_val_nd (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = ndunion s₁.1 s₂.1 := rfl
@[simp] theorem union_val (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = s₁.1 ∪ s₂.1 :=
ndunion_eq_union s₁.2
@[simp] theorem mem_union {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := mem_ndunion
theorem mem_union_left {a : α} {s₁ : finset α} (s₂ : finset α) (h : a ∈ s₁) : a ∈ s₁ ∪ s₂ := mem_union.2 $ or.inl h
theorem mem_union_right {a : α} {s₂ : finset α} (s₁ : finset α) (h : a ∈ s₂) : a ∈ s₁ ∪ s₂ := mem_union.2 $ or.inr h
theorem not_mem_union {a : α} {s₁ s₂ : finset α} : a ∉ s₁ ∪ s₂ ↔ a ∉ s₁ ∧ a ∉ s₂ :=
by rw [mem_union, not_or_distrib]
@[simp] lemma coe_union (s₁ s₂ : finset α) : ↑(s₁ ∪ s₂) = (↑s₁ ∪ ↑s₂ : set α) := set.ext $ λ x, mem_union
theorem union_subset {s₁ s₂ s₃ : finset α} (h₁ : s₁ ⊆ s₃) (h₂ : s₂ ⊆ s₃) : s₁ ∪ s₂ ⊆ s₃ :=
val_le_iff.1 (ndunion_le.2 ⟨h₁, val_le_iff.2 h₂⟩)
theorem subset_union_left (s₁ s₂ : finset α) : s₁ ⊆ s₁ ∪ s₂ := λ x, mem_union_left _
theorem subset_union_right (s₁ s₂ : finset α) : s₂ ⊆ s₁ ∪ s₂ := λ x, mem_union_right _
@[simp] theorem union_comm (s₁ s₂ : finset α) : s₁ ∪ s₂ = s₂ ∪ s₁ :=
ext.2 $ λ x, by simp only [mem_union, or_comm]
instance : is_commutative (finset α) (∪) := ⟨union_comm⟩
@[simp] theorem union_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) :=
ext.2 $ λ x, by simp only [mem_union, or_assoc]
instance : is_associative (finset α) (∪) := ⟨union_assoc⟩
@[simp] theorem union_idempotent (s : finset α) : s ∪ s = s :=
ext.2 $ λ _, mem_union.trans $ or_self _
instance : is_idempotent (finset α) (∪) := ⟨union_idempotent⟩
theorem union_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) :=
ext.2 $ λ _, by simp only [mem_union, or.left_comm]
theorem union_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ :=
ext.2 $ λ x, by simp only [mem_union, or_assoc, or_comm (x ∈ s₂)]
@[simp] theorem union_self (s : finset α) : s ∪ s = s := union_idempotent s
@[simp] theorem union_empty (s : finset α) : s ∪ ∅ = s :=
ext.2 $ λ x, mem_union.trans $ or_false _
@[simp] theorem empty_union (s : finset α) : ∅ ∪ s = s :=
ext.2 $ λ x, mem_union.trans $ false_or _
theorem insert_eq (a : α) (s : finset α) : insert a s = {a} ∪ s := rfl
@[simp] theorem insert_union (a : α) (s t : finset α) : insert a s ∪ t = insert a (s ∪ t) :=
by simp only [insert_eq, union_assoc]
@[simp] theorem union_insert (a : α) (s t : finset α) : s ∪ insert a t = insert a (s ∪ t) :=
by simp only [insert_eq, union_left_comm]
theorem insert_union_distrib (a : α) (s t : finset α) : insert a (s ∪ t) = insert a s ∪ insert a t :=
by simp only [insert_union, union_insert, insert_idem]
/- inter -/
/-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/
instance : has_inter (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndinter s₂.1 s₁.2⟩⟩
theorem inter_val_nd (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = ndinter s₁.1 s₂.1 := rfl
@[simp] theorem inter_val (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = s₁.1 ∩ s₂.1 :=
ndinter_eq_inter s₁.2
@[simp] theorem mem_inter {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := mem_ndinter
theorem mem_of_mem_inter_left {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₁ := (mem_inter.1 h).1
theorem mem_of_mem_inter_right {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₂ := (mem_inter.1 h).2
theorem mem_inter_of_mem {a : α} {s₁ s₂ : finset α} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ :=
and_imp.1 mem_inter.2
theorem inter_subset_left (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₁ := λ a, mem_of_mem_inter_left
theorem inter_subset_right (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₂ := λ a, mem_of_mem_inter_right
theorem subset_inter {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₁ ⊆ s₃ → s₁ ⊆ s₂ ∩ s₃ :=
by simp only [subset_iff, mem_inter] {contextual:=tt}; intros; split; trivial
@[simp] lemma coe_inter (s₁ s₂ : finset α) : ↑(s₁ ∩ s₂) = (↑s₁ ∩ ↑s₂ : set α) := set.ext $ λ _, mem_inter
@[simp] theorem inter_comm (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₂ ∩ s₁ :=
ext.2 $ λ _, by simp only [mem_inter, and_comm]
@[simp] theorem inter_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) :=
ext.2 $ λ _, by simp only [mem_inter, and_assoc]
@[simp] theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) :=
ext.2 $ λ _, by simp only [mem_inter, and.left_comm]
@[simp] theorem inter_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ :=
ext.2 $ λ _, by simp only [mem_inter, and.right_comm]
@[simp] theorem inter_self (s : finset α) : s ∩ s = s :=
ext.2 $ λ _, mem_inter.trans $ and_self _
@[simp] theorem inter_empty (s : finset α) : s ∩ ∅ = ∅ :=
ext.2 $ λ _, mem_inter.trans $ and_false _
@[simp] theorem empty_inter (s : finset α) : ∅ ∩ s = ∅ :=
ext.2 $ λ _, mem_inter.trans $ false_and _
@[simp] theorem insert_inter_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₂) :
insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) :=
ext.2 $ λ x, have x = a ∨ x ∈ s₂ ↔ x ∈ s₂, from or_iff_right_of_imp $ by rintro rfl; exact h,
by simp only [mem_inter, mem_insert, or_and_distrib_left, this]
@[simp] theorem inter_insert_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₁) :
s₁ ∩ insert a s₂ = insert a (s₁ ∩ s₂) :=
by rw [inter_comm, insert_inter_of_mem h, inter_comm]
@[simp] theorem insert_inter_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₂) :
insert a s₁ ∩ s₂ = s₁ ∩ s₂ :=
ext.2 $ λ x, have ¬ (x = a ∧ x ∈ s₂), by rintro ⟨rfl, H⟩; exact h H,
by simp only [mem_inter, mem_insert, or_and_distrib_right, this, false_or]
@[simp] theorem inter_insert_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₁) :
s₁ ∩ insert a s₂ = s₁ ∩ s₂ :=
by rw [inter_comm, insert_inter_of_not_mem h, inter_comm]
@[simp] theorem singleton_inter_of_mem {a : α} {s : finset α} (H : a ∈ s) : ι a ∩ s = ι a :=
show insert a ∅ ∩ s = insert a ∅, by rw [insert_inter_of_mem H, empty_inter]
@[simp] theorem singleton_inter_of_not_mem {a : α} {s : finset α} (H : a ∉ s) : ι a ∩ s = ∅ :=
eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_singleton]; rintro x ⟨rfl, h⟩; exact H h
@[simp] theorem inter_singleton_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ∩ ι a = ι a :=
by rw [inter_comm, singleton_inter_of_mem h]
@[simp] theorem inter_singleton_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : s ∩ ι a = ∅ :=
by rw [inter_comm, singleton_inter_of_not_mem h]
/- lattice laws -/
instance : lattice (finset α) :=
{ sup := (∪),
sup_le := assume a b c, union_subset,
le_sup_left := subset_union_left,
le_sup_right := subset_union_right,
inf := (∩),
le_inf := assume a b c, subset_inter,
inf_le_left := inter_subset_left,
inf_le_right := inter_subset_right,
..finset.partial_order }
@[simp] theorem sup_eq_union (s t : finset α) : s ⊔ t = s ∪ t := rfl
@[simp] theorem inf_eq_inter (s t : finset α) : s ⊓ t = s ∩ t := rfl
instance : semilattice_inf_bot (finset α) :=
{ bot := ∅, bot_le := empty_subset, ..finset.lattice.lattice }
instance {α : Type*} [decidable_eq α] : semilattice_sup_bot (finset α) :=
{ ..finset.lattice.semilattice_inf_bot, ..finset.lattice.lattice }
instance : distrib_lattice (finset α) :=
{ le_sup_inf := assume a b c, show (a ∪ b) ∩ (a ∪ c) ⊆ a ∪ b ∩ c,
by simp only [subset_iff, mem_inter, mem_union, and_imp, or_imp_distrib] {contextual:=tt};
simp only [true_or, imp_true_iff, true_and, or_true],
..finset.lattice.lattice }
theorem inter_distrib_left (s t u : finset α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := inf_sup_left
theorem inter_distrib_right (s t u : finset α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := inf_sup_right
theorem union_distrib_left (s t u : finset α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := sup_inf_left
theorem union_distrib_right (s t u : finset α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := sup_inf_right
/- erase -/
/-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are
not equal to `a`. -/
def erase (s : finset α) (a : α) : finset α := ⟨_, nodup_erase_of_nodup a s.2⟩
@[simp] theorem erase_val (s : finset α) (a : α) : (erase s a).1 = s.1.erase a := rfl
@[simp] theorem mem_erase {a b : α} {s : finset α} : a ∈ erase s b ↔ a ≠ b ∧ a ∈ s :=
mem_erase_iff_of_nodup s.2
theorem not_mem_erase (a : α) (s : finset α) : a ∉ erase s a := mem_erase_of_nodup s.2
@[simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl
theorem ne_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ≠ a :=
by simp only [mem_erase]; exact and.left
theorem mem_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ∈ s := mem_of_mem_erase
theorem mem_erase_of_ne_of_mem {a b : α} {s : finset α} : a ≠ b → a ∈ s → a ∈ erase s b :=
by simp only [mem_erase]; exact and.intro
theorem erase_insert {a : α} {s : finset α} (h : a ∉ s) : erase (insert a s) a = s :=
ext.2 $ assume x, by simp only [mem_erase, mem_insert, and_or_distrib_left, not_and_self, false_or];
apply and_iff_right_of_imp; rintro H rfl; exact h H
theorem insert_erase {a : α} {s : finset α} (h : a ∈ s) : insert a (erase s a) = s :=
ext.2 $ assume x, by simp only [mem_insert, mem_erase, or_and_distrib_left, dec_em, true_and];
apply or_iff_right_of_imp; rintro rfl; exact h
theorem erase_subset_erase (a : α) {s t : finset α} (h : s ⊆ t) : erase s a ⊆ erase t a :=
val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h
theorem erase_subset (a : α) (s : finset α) : erase s a ⊆ s := erase_subset _ _
@[simp] lemma coe_erase (a : α) (s : finset α) : ↑(erase s a) = (↑s \ {a} : set α) :=
set.ext $ λ _, mem_erase.trans $ by rw [and_comm, set.mem_diff, set.mem_singleton_iff]; refl
lemma erase_ssubset {a : α} {s : finset α} (h : a ∈ s) : s.erase a ⊂ s :=
calc s.erase a ⊂ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _
... = _ : insert_erase h
theorem erase_eq_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : erase s a = s :=
eq_of_veq $ erase_of_not_mem h
theorem subset_insert_iff {a : α} {s t : finset α} : s ⊆ insert a t ↔ erase s a ⊆ t :=
by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp];
exact forall_congr (λ x, forall_swap)
theorem erase_insert_subset (a : α) (s : finset α) : erase (insert a s) a ⊆ s :=
subset_insert_iff.1 $ subset.refl _
theorem insert_erase_subset (a : α) (s : finset α) : s ⊆ insert a (erase s a) :=
subset_insert_iff.2 $ subset.refl _
/- sdiff -/
/-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/
instance : has_sdiff (finset α) := ⟨λs₁ s₂, ⟨s₁.1 - s₂.1, nodup_of_le (sub_le_self _ _) s₁.2⟩⟩
@[simp] theorem mem_sdiff {a : α} {s₁ s₂ : finset α} :
a ∈ s₁ \ s₂ ↔ a ∈ s₁ ∧ a ∉ s₂ := mem_sub_of_nodup s₁.2
@[simp] theorem sdiff_union_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ :=
ext.2 $ λ a, by simpa only [mem_sdiff, mem_union, or_comm,
or_and_distrib_left, dec_em, and_true] using or_iff_right_of_imp (@h a)
@[simp] theorem union_sdiff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ∪ (s₂ \ s₁) = s₂ :=
(union_comm _ _).trans (sdiff_union_of_subset h)
@[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ :=
eq_empty_of_forall_not_mem $
by simp only [mem_inter, mem_sdiff]; rintro x ⟨h, _, hn⟩; exact hn h
@[simp] theorem sdiff_inter_self (s₁ s₂ : finset α) : (s₂ \ s₁) ∩ s₁ = ∅ :=
(inter_comm _ _).trans (inter_sdiff_self _ _)
theorem sdiff_subset_sdiff {s₁ s₂ t₁ t₂ : finset α} (h₁ : t₁ ⊆ t₂) (h₂ : s₂ ⊆ s₁) : t₁ \ s₁ ⊆ t₂ \ s₂ :=
by simpa only [subset_iff, mem_sdiff, and_imp] using λ a m₁ m₂, and.intro (h₁ m₁) (mt (@h₂ _) m₂)
@[simp] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (↑s₁ \ ↑s₂ : set α) :=
set.ext $ λ _, mem_sdiff
end decidable_eq
/- attach -/
/-- `attach s` takes the elements of `s` and forms a new set of elements of the
subtype `{x // x ∈ s}`. -/
def attach (s : finset α) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩
@[simp] theorem attach_val (s : finset α) : s.attach.1 = s.1.attach := rfl
@[simp] theorem mem_attach (s : finset α) : ∀ x, x ∈ s.attach := mem_attach _
@[simp] theorem attach_empty : attach (∅ : finset α) = ∅ := rfl
section decidable_pi_exists
variables {s : finset α}
instance decidable_dforall_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] :
decidable (∀a (h : a ∈ s), p a h) :=
multiset.decidable_dforall_multiset
/-- decidable equality for functions whose domain is bounded by finsets -/
instance decidable_eq_pi_finset {β : α → Type*} [h : ∀a, decidable_eq (β a)] :
decidable_eq (Πa∈s, β a) :=
multiset.decidable_eq_pi_multiset
instance decidable_dexists_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] :
decidable (∃a (h : a ∈ s), p a h) :=
multiset.decidable_dexists_multiset
end decidable_pi_exists
/- filter -/
section filter
variables {p q : α → Prop} [decidable_pred p] [decidable_pred q]
/-- `filter p s` is the set of elements of `s` that satisfy `p`. -/
def filter (p : α → Prop) [decidable_pred p] (s : finset α) : finset α :=
⟨_, nodup_filter p s.2⟩
@[simp] theorem filter_val (s : finset α) : (filter p s).1 = s.1.filter p := rfl
@[simp] theorem mem_filter {s : finset α} {a : α} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter
@[simp] theorem filter_subset (s : finset α) : s.filter p ⊆ s := filter_subset _
theorem filter_filter (s : finset α) :
(s.filter p).filter q = s.filter (λa, p a ∧ q a) :=
ext.2 $ assume a, by simp only [mem_filter, and_comm, and.left_comm]
@[simp] lemma filter_true {s : finset α} [h : decidable_pred (λ _, true)] :
@finset.filter α (λ _, true) h s = s :=
by ext; simp
@[simp] theorem filter_false {h} (s : finset α) : @filter α (λa, false) h s = ∅ :=
ext.2 $ assume a, by simp only [mem_filter, and_false]; refl
lemma filter_congr {s : finset α} (H : ∀ x ∈ s, p x ↔ q x) : filter p s = filter q s :=
eq_of_veq $ filter_congr H
lemma filter_empty : filter p ∅ = ∅ :=
subset_empty.1 $ filter_subset _
lemma filter_subset_filter {s t : finset α} (h : s ⊆ t) : s.filter p ⊆ t.filter p :=
assume a ha, mem_filter.2 ⟨h (mem_filter.1 ha).1, (mem_filter.1 ha).2⟩
variable [decidable_eq α]
theorem filter_union (s₁ s₂ : finset α) :
(s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p :=
ext.2 $ λ _, by simp only [mem_filter, mem_union, or_and_distrib_right]
theorem filter_or (s : finset α) : s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q :=
ext.2 $ λ _, by simp only [mem_filter, mem_union, and_or_distrib_left]
theorem filter_and (s : finset α) : s.filter (λ a, p a ∧ q a) = s.filter p ∩ s.filter q :=
ext.2 $ λ _, by simp only [mem_filter, mem_inter, and_comm, and.left_comm, and_self]
theorem filter_not (s : finset α) : s.filter (λ a, ¬ p a) = s \ s.filter p :=
ext.2 $ by simpa only [mem_filter, mem_sdiff, and_comm, not_and] using λ a, and_congr_right $
λ h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm
theorem sdiff_eq_filter (s₁ s₂ : finset α) :
s₁ \ s₂ = filter (∉ s₂) s₁ := ext.2 $ λ _, by simp only [mem_sdiff, mem_filter]
theorem filter_union_filter_neg_eq (s : finset α) : s.filter p ∪ s.filter (λa, ¬ p a) = s :=
by simp only [filter_not, union_sdiff_of_subset (filter_subset s)]
theorem filter_inter_filter_neg_eq (s : finset α) : s.filter p ∩ s.filter (λa, ¬ p a) = ∅ :=
by simp only [filter_not, inter_sdiff_self]
@[simp] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) :=
set.ext $ λ _, mem_filter
end filter
/- range -/
section range
variables {n m l : ℕ}
/-- `range n` is the set of natural numbers less than `n`. -/
def range (n : ℕ) : finset ℕ := ⟨_, nodup_range n⟩
@[simp] theorem range_val (n : ℕ) : (range n).1 = multiset.range n := rfl
@[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range
@[simp] theorem range_zero : range 0 = ∅ := rfl
@[simp] theorem range_one : range 1 = {0} := rfl
theorem range_succ : range (succ n) = insert n (range n) :=
eq_of_veq $ (range_succ n).trans $ (ndinsert_of_not_mem not_mem_range_self).symm
theorem range_add_one : range (n + 1) = insert n (range n) :=
range_succ
@[simp] theorem not_mem_range_self : n ∉ range n := not_mem_range_self
@[simp] theorem range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset
theorem exists_nat_subset_range (s : finset ℕ) : ∃n : ℕ, s ⊆ range n :=
finset.induction_on s ⟨0, empty_subset _⟩ $ λ a s ha ⟨n, hn⟩,
⟨max (a + 1) n, insert_subset.2
⟨by simpa only [mem_range] using le_max_left (a+1) n,
subset.trans hn (by simpa only [range_subset] using le_max_right (a+1) n)⟩⟩
end range
/- useful rules for calculations with quantifiers -/
theorem exists_mem_empty_iff (p : α → Prop) : (∃ x, x ∈ (∅ : finset α) ∧ p x) ↔ false :=
by simp only [not_mem_empty, false_and, exists_false]
theorem exists_mem_insert [d : decidable_eq α]
(a : α) (s : finset α) (p : α → Prop) :
(∃ x, x ∈ insert a s ∧ p x) ↔ p a ∨ (∃ x, x ∈ s ∧ p x) :=
by simp only [mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left]
theorem forall_mem_empty_iff (p : α → Prop) : (∀ x, x ∈ (∅ : finset α) → p x) ↔ true :=
iff_true_intro $ λ _, false.elim
theorem forall_mem_insert [d : decidable_eq α]
(a : α) (s : finset α) (p : α → Prop) :
(∀ x, x ∈ insert a s → p x) ↔ p a ∧ (∀ x, x ∈ s → p x) :=
by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq]
end finset
namespace option
/-- Construct an empty or singleton finset from an `option` -/
def to_finset (o : option α) : finset α :=
match o with
| none := ∅
| some a := finset.singleton a
end
@[simp] theorem to_finset_none : none.to_finset = (∅ : finset α) := rfl
@[simp] theorem to_finset_some {a : α} : (some a).to_finset = finset.singleton a := rfl
@[simp] theorem mem_to_finset {a : α} {o : option α} : a ∈ o.to_finset ↔ a ∈ o :=
by cases o; simp only [to_finset, finset.mem_singleton, option.mem_def, eq_comm]; refl
end option
/- erase_dup on list and multiset -/
namespace multiset
variable [decidable_eq α]
/-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/
def to_finset (s : multiset α) : finset α := ⟨_, nodup_erase_dup s⟩
@[simp] theorem to_finset_val (s : multiset α) : s.to_finset.1 = s.erase_dup := rfl
theorem to_finset_eq {s : multiset α} (n : nodup s) : finset.mk s n = s.to_finset :=
finset.val_inj.1 (erase_dup_eq_self.2 n).symm
@[simp] theorem mem_to_finset {a : α} {s : multiset α} : a ∈ s.to_finset ↔ a ∈ s :=
mem_erase_dup
@[simp] lemma to_finset_zero :
to_finset (0 : multiset α) = ∅ :=
rfl
@[simp] lemma to_finset_cons (a : α) (s : multiset α) :
to_finset (a :: s) = insert a (to_finset s) :=
finset.eq_of_veq erase_dup_cons
@[simp] lemma to_finset_add (s t : multiset α) :
to_finset (s + t) = to_finset s ∪ to_finset t :=
finset.ext' $ by simp
@[simp] lemma to_finset_smul (s : multiset α) :
∀(n : ℕ) (hn : n ≠ 0), (add_monoid.smul n s).to_finset = s.to_finset
| 0 h := by contradiction
| (n+1) h :=
begin
by_cases n = 0,
{ rw [h, zero_add, add_monoid.one_smul] },
{ rw [add_monoid.add_smul, to_finset_add, add_monoid.one_smul, to_finset_smul n h,
finset.union_idempotent] }
end
end multiset
namespace list
variable [decidable_eq α]
/-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/
def to_finset (l : list α) : finset α := multiset.to_finset l
@[simp] theorem to_finset_val (l : list α) : l.to_finset.1 = (l.erase_dup : multiset α) := rfl
theorem to_finset_eq {l : list α} (n : nodup l) : @finset.mk α l n = l.to_finset :=
multiset.to_finset_eq n
@[simp] theorem mem_to_finset {a : α} {l : list α} : a ∈ l.to_finset ↔ a ∈ l :=
mem_erase_dup
@[simp] theorem to_finset_nil : to_finset (@nil α) = ∅ :=
rfl
@[simp] theorem to_finset_cons {a : α} {l : list α} : to_finset (a :: l) = insert a (to_finset l) :=
finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.erase_dup_cons, h]
end list
namespace finset
section map
open function
def map (f : α ↪ β) (s : finset α) : finset β :=
⟨s.1.map f, nodup_map f.2 s.2⟩
@[simp] theorem map_val (f : α ↪ β) (s : finset α) : (map f s).1 = s.1.map f := rfl
@[simp] theorem map_empty (f : α ↪ β) (s : finset α) : (∅ : finset α).map f = ∅ := rfl
variables {f : α ↪ β} {s : finset α}
@[simp] theorem mem_map {b : β} : b ∈ s.map f ↔ ∃ a ∈ s, f a = b :=
mem_map.trans $ by simp only [exists_prop]; refl
theorem mem_map' (f : α ↪ β) {a} {s : finset α} : f a ∈ s.map f ↔ a ∈ s :=
mem_map_of_inj f.2
@[simp] theorem mem_map_of_mem (f : α ↪ β) {a} {s : finset α} : a ∈ s → f a ∈ s.map f :=
(mem_map' _).2
theorem map_to_finset [decidable_eq α] [decidable_eq β] {s : multiset α} :
s.to_finset.map f = (s.map f).to_finset :=
ext.2 $ λ _, by simp only [mem_map, multiset.mem_map, exists_prop, multiset.mem_to_finset]
theorem map_refl : s.map (embedding.refl _) = s :=
ext.2 $ λ _, by simpa only [mem_map, exists_prop] using exists_eq_right
theorem map_map {g : β ↪ γ} : (s.map f).map g = s.map (f.trans g) :=
eq_of_veq $ by simp only [map_val, multiset.map_map]; refl
theorem map_subset_map {s₁ s₂ : finset α} : s₁.map f ⊆ s₂.map f ↔ s₁ ⊆ s₂ :=
⟨λ h x xs, (mem_map' _).1 $ h $ (mem_map' f).2 xs,
λ h, by simp [subset_def, map_subset_map h]⟩
theorem map_inj {s₁ s₂ : finset α} : s₁.map f = s₂.map f ↔ s₁ = s₂ :=
by simp only [subset.antisymm_iff, map_subset_map]
def map_embedding (f : α ↪ β) : finset α ↪ finset β := ⟨map f, λ s₁ s₂, map_inj.1⟩
@[simp] theorem map_embedding_apply : map_embedding f s = map f s := rfl
theorem map_filter {p : β → Prop} [decidable_pred p] :
(s.map f).filter p = (s.filter (p ∘ f)).map f :=
ext.2 $ λ b, by simp only [mem_filter, mem_map, exists_prop, and_assoc]; exact
⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, h1, h2, rfl⟩,
by rintro ⟨x, h1, h2, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩
theorem map_union [decidable_eq α] [decidable_eq β]
{f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).map f = s₁.map f ∪ s₂.map f :=
ext.2 $ λ _, by simp only [mem_map, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib]
theorem map_inter [decidable_eq α] [decidable_eq β]
{f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∩ s₂).map f = s₁.map f ∩ s₂.map f :=
ext.2 $ λ b, by simp only [mem_map, mem_inter, exists_prop]; exact
⟨by rintro ⟨a, ⟨m₁, m₂⟩, rfl⟩; exact ⟨⟨a, m₁, rfl⟩, ⟨a, m₂, rfl⟩⟩,
by rintro ⟨⟨a, m₁, e⟩, ⟨a', m₂, rfl⟩⟩; cases f.2 e; exact ⟨_, ⟨m₁, m₂⟩, rfl⟩⟩
@[simp] theorem map_singleton (f : α ↪ β) (a : α) : (singleton a).map f = singleton (f a) :=
ext.2 $ λ _, by simp only [mem_map, mem_singleton, exists_prop, exists_eq_left]; exact eq_comm
@[simp] theorem map_insert [decidable_eq α] [decidable_eq β]
(f : α ↪ β) (a : α) (s : finset α) :
(insert a s).map f = insert (f a) (s.map f) :=
by simp only [insert_eq, insert_empty_eq_singleton, map_union, map_singleton]
@[simp] theorem map_eq_empty : s.map f = ∅ ↔ s = ∅ :=
⟨λ h, eq_empty_of_forall_not_mem $
λ a m, ne_empty_of_mem (mem_map_of_mem _ m) h, λ e, e.symm ▸ rfl⟩
lemma attach_map_val {s : finset α} : s.attach.map (embedding.subtype _) = s :=
eq_of_veq $ by rw [map_val, attach_val]; exact attach_map_val _
end map
section image
variables [decidable_eq β]
/-- `image f s` is the forward image of `s` under `f`. -/
def image (f : α → β) (s : finset α) : finset β := (s.1.map f).to_finset
@[simp] theorem image_val (f : α → β) (s : finset α) : (image f s).1 = (s.1.map f).erase_dup := rfl
@[simp] theorem image_empty (f : α → β) : (∅ : finset α).image f = ∅ := rfl
variables {f : α → β} {s : finset α}
@[simp] theorem mem_image {b : β} : b ∈ s.image f ↔ ∃ a ∈ s, f a = b :=
by simp only [mem_def, image_val, mem_erase_dup, multiset.mem_map, exists_prop]
@[simp] theorem mem_image_of_mem (f : α → β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.image f :=
mem_image.2 ⟨_, h, rfl⟩
@[simp] lemma coe_image {f : α → β} : ↑(s.image f) = f '' ↑s :=
set.ext $ λ _, mem_image.trans $ by simp only [exists_prop]; refl
theorem image_to_finset [decidable_eq α] {s : multiset α} : s.to_finset.image f = (s.map f).to_finset :=
ext.2 $ λ _, by simp only [mem_image, multiset.mem_to_finset, exists_prop, multiset.mem_map]
@[simp] theorem image_val_of_inj_on (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : (image f s).1 = s.1.map f :=
multiset.erase_dup_eq_self.2 (nodup_map_on H s.2)
theorem image_id [decidable_eq α] : s.image id = s :=
ext.2 $ λ _, by simp only [mem_image, exists_prop, id, exists_eq_right]
theorem image_image [decidable_eq γ] {g : β → γ} : (s.image f).image g = s.image (g ∘ f) :=
eq_of_veq $ by simp only [image_val, erase_dup_map_erase_dup_eq, multiset.map_map]
theorem image_subset_image {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.image f ⊆ s₂.image f :=
by simp only [subset_def, image_val, subset_erase_dup', erase_dup_subset', multiset.map_subset_map h]
theorem image_filter {p : β → Prop} [decidable_pred p] :
(s.image f).filter p = (s.filter (p ∘ f)).image f :=
ext.2 $ λ b, by simp only [mem_filter, mem_image, exists_prop]; exact
⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, ⟨h1, h2⟩, rfl⟩,
by rintro ⟨x, ⟨h1, h2⟩, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩
theorem image_union [decidable_eq α] {f : α → β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).image f = s₁.image f ∪ s₂.image f :=
ext.2 $ λ _, by simp only [mem_image, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib]
theorem image_inter [decidable_eq α] (s₁ s₂ : finset α) (hf : ∀x y, f x = f y → x = y) : (s₁ ∩ s₂).image f = s₁.image f ∩ s₂.image f :=
ext.2 $ by simp only [mem_image, exists_prop, mem_inter]; exact λ b,
⟨λ ⟨a, ⟨m₁, m₂⟩, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, m₂, e⟩⟩,
λ ⟨⟨a, m₁, e₁⟩, ⟨a', m₂, e₂⟩⟩, ⟨a, ⟨m₁, hf _ _ (e₂.trans e₁.symm) ▸ m₂⟩, e₁⟩⟩.
@[simp] theorem image_singleton [decidable_eq α] (f : α → β) (a : α) : (singleton a).image f = singleton (f a) :=
ext.2 $ λ x, by simpa only [mem_image, exists_prop, mem_singleton, exists_eq_left] using eq_comm
@[simp] theorem image_insert [decidable_eq α] (f : α → β) (a : α) (s : finset α) :
(insert a s).image f = insert (f a) (s.image f) :=
by simp only [insert_eq, insert_empty_eq_singleton, image_singleton, image_union]
@[simp] theorem image_eq_empty : s.image f = ∅ ↔ s = ∅ :=
⟨λ h, eq_empty_of_forall_not_mem $
λ a m, ne_empty_of_mem (mem_image_of_mem _ m) h, λ e, e.symm ▸ rfl⟩
lemma attach_image_val [decidable_eq α] {s : finset α} : s.attach.image subtype.val = s :=
eq_of_veq $ by rw [image_val, attach_val, multiset.attach_map_val, erase_dup_eq_self]
@[simp] lemma attach_insert [decidable_eq α] {a : α} {s : finset α} :
attach (insert a s) = insert (⟨a, mem_insert_self a s⟩ : {x // x ∈ insert a s})
((attach s).image (λx, ⟨x.1, mem_insert_of_mem x.2⟩)) :=
ext.2 $ λ ⟨x, hx⟩, ⟨or.cases_on (mem_insert.1 hx)
(assume h : x = a, λ _, mem_insert.2 $ or.inl $ subtype.eq h)
(assume h : x ∈ s, λ _, mem_insert_of_mem $ mem_image.2 $ ⟨⟨x, h⟩, mem_attach _ _, subtype.eq rfl⟩),
λ _, finset.mem_attach _ _⟩
theorem map_eq_image (f : α ↪ β) (s : finset α) : s.map f = s.image f :=
eq_of_veq $ (multiset.erase_dup_eq_self.2 (s.map f).2).symm
lemma image_const [decidable_eq β] {s : finset α} (h : s ≠ ∅) (b : β) : s.image (λa, b) = singleton b :=
ext.2 $ assume b', by simp only [mem_image, exists_prop, exists_and_distrib_right,
exists_mem_of_ne_empty h, true_and, mem_singleton, eq_comm]
protected def subtype {α} (p : α → Prop) [decidable_pred p] (s : finset α) : finset (subtype p) :=
(s.filter p).attach.map ⟨λ x, ⟨x.1, (finset.mem_filter.1 x.2).2⟩,
λ x y H, subtype.eq $ subtype.mk.inj H⟩
@[simp] lemma mem_subtype {p : α → Prop} [decidable_pred p] {s : finset α} :
∀{a : subtype p}, a ∈ s.subtype p ↔ a.val ∈ s
| ⟨a, ha⟩ := by simp [finset.subtype, ha]
end image
/- card -/
section card
/-- `card s` is the cardinality (number of elements) of `s`. -/
def card (s : finset α) : nat := s.1.card
theorem card_def (s : finset α) : s.card = s.1.card := rfl
@[simp] theorem card_empty : card (∅ : finset α) = 0 := rfl
@[simp] theorem card_eq_zero {s : finset α} : card s = 0 ↔ s = ∅ :=
card_eq_zero.trans val_eq_zero
theorem card_pos {s : finset α} : 0 < card s ↔ s ≠ ∅ :=
pos_iff_ne_zero.trans $ not_congr card_eq_zero
theorem card_eq_one {s : finset α} : s.card = 1 ↔ ∃ a, s = finset.singleton a :=
by cases s; simp [multiset.card_eq_one, finset.singleton, finset.card]
@[simp] theorem card_insert_of_not_mem [decidable_eq α]
{a : α} {s : finset α} (h : a ∉ s) : card (insert a s) = card s + 1 :=
by simpa only [card_cons, card, insert_val] using
congr_arg multiset.card (ndinsert_of_not_mem h)
theorem card_insert_le [decidable_eq α] (a : α) (s : finset α) : card (insert a s) ≤ card s + 1 :=
by by_cases a ∈ s; [{rw [insert_eq_of_mem h], apply nat.le_add_right},
rw [card_insert_of_not_mem h]]
@[simp] theorem card_singleton (a : α) : card (singleton a) = 1 := card_singleton _
theorem card_erase_of_mem [decidable_eq α] {a : α} {s : finset α} : a ∈ s → card (erase s a) = pred (card s) := card_erase_of_mem
@[simp] theorem card_range (n : ℕ) : card (range n) = n := card_range n
@[simp] theorem card_attach {s : finset α} : card (attach s) = card s := multiset.card_attach
theorem card_image_of_inj_on [decidable_eq β] {f : α → β} {s : finset α}
(H : ∀x∈s, ∀y∈s, f x = f y → x = y) : card (image f s) = card s :=
by simp only [card, image_val_of_inj_on H, card_map]
theorem card_image_of_injective [decidable_eq β] {f : α → β} (s : finset α)
(H : function.injective f) : card (image f s) = card s :=
card_image_of_inj_on $ λ x _ y _ h, H h
lemma card_eq_of_bijective [decidable_eq α] {s : finset α} {n : ℕ}
(f : ∀i, i < n → α)
(hf : ∀a∈s, ∃i, ∃h:i<n, f i h = a) (hf' : ∀i (h : i < n), f i h ∈ s)
(f_inj : ∀i j (hi : i < n) (hj : j < n), f i hi = f j hj → i = j) :
card s = n :=
have ∀ (a : α), a ∈ s ↔ ∃i (hi : i ∈ range n), f i (mem_range.1 hi) = a,
from assume a, ⟨assume ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩,
assume ⟨i, hi, eq⟩, eq ▸ hf' i (mem_range.1 hi)⟩,
have s = ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)),
by simpa only [ext, mem_image, exists_prop, subtype.exists, mem_attach, true_and],
calc card s = card ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)) :
by rw [this]
... = card ((range n).attach) :
card_image_of_injective _ $ assume ⟨i, hi⟩ ⟨j, hj⟩ eq,
subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq
... = card (range n) : card_attach
... = n : card_range n
lemma card_eq_succ [decidable_eq α] {s : finset α} {n : ℕ} :
s.card = n + 1 ↔ (∃a t, a ∉ t ∧ insert a t = s ∧ card t = n) :=
iff.intro
(assume eq,
have card s > 0, from eq.symm ▸ nat.zero_lt_succ _,
let ⟨a, has⟩ := finset.exists_mem_of_ne_empty $ card_pos.mp this in
⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by simp only [eq, card_erase_of_mem has, pred_succ]⟩)
(assume ⟨a, t, hat, s_eq, n_eq⟩, s_eq ▸ n_eq ▸ card_insert_of_not_mem hat)
theorem card_le_of_subset {s t : finset α} : s ⊆ t → card s ≤ card t :=
multiset.card_le_of_le ∘ val_le_iff.mpr
theorem eq_of_subset_of_card_le {s t : finset α} (h : s ⊆ t) (h₂ : card t ≤ card s) : s = t :=
eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) h₂
lemma card_lt_card [decidable_eq α] {s t : finset α} (h : s ⊂ t) : s.card < t.card :=
card_lt_of_lt (val_lt_iff.2 h)
lemma card_le_card_of_inj_on [decidable_eq α] [decidable_eq β] {s : finset α} {t : finset β}
(f : α → β) (hf : ∀a∈s, f a ∈ t) (f_inj : ∀a₁∈s, ∀a₂∈s, f a₁ = f a₂ → a₁ = a₂) :
card s ≤ card t :=
calc card s = card (s.image f) : by rw [card_image_of_inj_on f_inj]
... ≤ card t : card_le_of_subset $
assume x hx, match x, finset.mem_image.1 hx with _, ⟨a, ha, rfl⟩ := hf a ha end
lemma card_le_of_inj_on [decidable_eq α] {n} {s : finset α}
(f : ℕ → α) (hf : ∀i<n, f i ∈ s) (f_inj : ∀i j, i<n → j<n → f i = f j → i = j) : n ≤ card s :=
calc n = card (range n) : (card_range n).symm
... ≤ card s : card_le_card_of_inj_on f
(by simpa only [mem_range])
(by simp only [mem_range]; exact assume a₁ h₁ a₂ h₂, f_inj a₁ a₂ h₁ h₂)
@[elab_as_eliminator] lemma strong_induction_on {p : finset α → Sort*} :
∀ (s : finset α), (∀s, (∀t ⊂ s, p t) → p s) → p s
| ⟨s, nd⟩ ih := multiset.strong_induction_on s
(λ s IH nd, ih ⟨s, nd⟩ (λ ⟨t, nd'⟩ ss, IH t (val_lt_iff.2 ss) nd')) nd
@[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq α] {p : finset α → Prop}
(s : finset α) (h₀ : p ∅) (h₁ : ∀ a s, a ∉ s → (∀t ⊆ s, p t) → p (insert a s)) : p s :=
finset.strong_induction_on s $ λ s,
finset.induction_on s (λ _, h₀) $ λ a s n _ ih, h₁ a s n $
λ t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _)
lemma card_congr {s : finset α} {t : finset β} (f : Π a ∈ s, β)
(h₁ : ∀ a ha, f a ha ∈ t) (h₂ : ∀ a b ha hb, f a ha = f b hb → a = b)
(h₃ : ∀ b ∈ t, ∃ a ha, f a ha = b) : s.card = t.card :=
by haveI := classical.prop_decidable; exact
calc s.card = s.attach.card : card_attach.symm
... = (s.attach.image (λ (a : {a // a ∈ s}), f a.1 a.2)).card :
eq.symm (card_image_of_injective _ (λ a b h, subtype.eq (h₂ _ _ _ _ h)))
... = t.card : congr_arg card (finset.ext.2 $ λ b,
⟨λ h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ h₁ _ _,
λ h, let ⟨a, ha₁, ha₂⟩ := h₃ b h in mem_image.2 ⟨⟨a, ha₁⟩, by simp [ha₂]⟩⟩)
lemma card_union_add_card_inter [decidable_eq α] (s t : finset α) :
(s ∪ t).card + (s ∩ t).card = s.card + t.card :=
finset.induction_on t (by simp) (λ a, by by_cases a ∈ s; simp * {contextual := tt})
lemma card_union_le [decidable_eq α] (s t : finset α) :
(s ∪ t).card ≤ s.card + t.card :=
card_union_add_card_inter s t ▸ le_add_right _ _
lemma surj_on_of_inj_on_of_card_le {s : finset α} {t : finset β}
(f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t)
(hinj : ∀ a₁ a₂ ha₁ ha₂, f a₁ ha₁ = f a₂ ha₂ → a₁ = a₂)
(hst : card t ≤ card s) :
(∀ b ∈ t, ∃ a ha, b = f a ha) :=
by haveI := classical.dec_eq β; exact
λ b hb,
have h : card (image (λ (a : {a // a ∈ s}), f (a.val) a.2) (attach s)) = card s,
from @card_attach _ s ▸ card_image_of_injective _
(λ ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ h, subtype.eq $ hinj _ _ _ _ h),
have h₁ : image (λ a : {a // a ∈ s}, f a.1 a.2) s.attach = t :=
eq_of_subset_of_card_le (λ b h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in
ha₂ ▸ hf _ _) (by simp [hst, h]),
begin
rw ← h₁ at hb,
rcases mem_image.1 hb with ⟨a, ha₁, ha₂⟩,
exact ⟨a, a.2, ha₂.symm⟩,
end
end card
section bind
variables [decidable_eq β] {s : finset α} {t : α → finset β}
/-- `bind s t` is the union of `t x` over `x ∈ s` -/
protected def bind (s : finset α) (t : α → finset β) : finset β := (s.1.bind (λ a, (t a).1)).to_finset
@[simp] theorem bind_val (s : finset α) (t : α → finset β) :
(s.bind t).1 = (s.1.bind (λ a, (t a).1)).erase_dup := rfl
@[simp] theorem bind_empty : finset.bind ∅ t = ∅ := rfl
@[simp] theorem mem_bind {b : β} : b ∈ s.bind t ↔ ∃a∈s, b ∈ t a :=
by simp only [mem_def, bind_val, mem_erase_dup, mem_bind, exists_prop]
@[simp] theorem bind_insert [decidable_eq α] {a : α} : (insert a s).bind t = t a ∪ s.bind t :=
ext.2 $ λ x, by simp only [mem_bind, exists_prop, mem_union, mem_insert,
or_and_distrib_right, exists_or_distrib, exists_eq_left]
-- ext.2 $ λ x, by simp [or_and_distrib_right, exists_or_distrib]
@[simp] lemma singleton_bind [decidable_eq α] {a : α} : (singleton a).bind t = t a :=
show (insert a ∅ : finset α).bind t = t a, from bind_insert.trans $ union_empty _
theorem image_bind [decidable_eq γ] {f : α → β} {s : finset α} {t : β → finset γ} :
(s.image f).bind t = s.bind (λa, t (f a)) :=
by haveI := classical.dec_eq α; exact
finset.induction_on s rfl (λ a s has ih,
by simp only [image_insert, bind_insert, ih])
theorem bind_image [decidable_eq γ] {s : finset α} {t : α → finset β} {f : β → γ} :
(s.bind t).image f = s.bind (λa, (t a).image f) :=
by haveI := classical.dec_eq α; exact
finset.induction_on s rfl (λ a s has ih,
by simp only [bind_insert, image_union, ih])
theorem bind_to_finset [decidable_eq α] (s : multiset α) (t : α → multiset β) :
(s.bind t).to_finset = s.to_finset.bind (λa, (t a).to_finset) :=
ext.2 $ λ x, by simp only [multiset.mem_to_finset, mem_bind, multiset.mem_bind, exists_prop]
lemma bind_mono {t₁ t₂ : α → finset β} (h : ∀a∈s, t₁ a ⊆ t₂ a) : s.bind t₁ ⊆ s.bind t₂ :=
have ∀b a, a ∈ s → b ∈ t₁ a → (∃ (a : α), a ∈ s ∧ b ∈ t₂ a),
from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩,
by simpa only [subset_iff, mem_bind, exists_imp_distrib, and_imp, exists_prop]
lemma bind_singleton {f : α → β} : s.bind (λa, {f a}) = s.image f :=
ext.2 $ λ x, by simp only [mem_bind, mem_image, insert_empty_eq_singleton, mem_singleton, eq_comm]
lemma image_bind_filter_eq [decidable_eq α] (s : finset β) (g : β → α) :
(s.image g).bind (λa, s.filter $ (λc, g c = a)) = s :=
begin
ext b,
simp,
split,
{ rintros ⟨a, ⟨b', _, _⟩, hb, _⟩, exact hb },
{ rintros hb, exact ⟨g b, ⟨b, hb, rfl⟩, hb, rfl⟩ }
end
end bind
section prod
variables {s : finset α} {t : finset β}
/-- `product s t` is the set of pairs `(a, b)` such that `a ∈ s` and `b ∈ t`. -/
protected def product (s : finset α) (t : finset β) : finset (α × β) := ⟨_, nodup_product s.2 t.2⟩
@[simp] theorem product_val : (s.product t).1 = s.1.product t.1 := rfl
@[simp] theorem mem_product {p : α × β} : p ∈ s.product t ↔ p.1 ∈ s ∧ p.2 ∈ t := mem_product
theorem product_eq_bind [decidable_eq α] [decidable_eq β] (s : finset α) (t : finset β) :
s.product t = s.bind (λa, t.image $ λb, (a, b)) :=
ext.2 $ λ ⟨x, y⟩, by simp only [mem_product, mem_bind, mem_image, exists_prop, prod.mk.inj_iff,
and.left_comm, exists_and_distrib_left, exists_eq_right, exists_eq_left]
@[simp] theorem card_product (s : finset α) (t : finset β) : card (s.product t) = card s * card t :=
multiset.card_product _ _
end prod
section sigma
variables {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)}
/-- `sigma s t` is the set of dependent pairs `⟨a, b⟩` such that `a ∈ s` and `b ∈ t a`. -/
protected def sigma (s : finset α) (t : Πa, finset (σ a)) : finset (Σa, σ a) :=
⟨_, nodup_sigma s.2 (λ a, (t a).2)⟩
@[simp] theorem mem_sigma {p : sigma σ} : p ∈ s.sigma t ↔ p.1 ∈ s ∧ p.2 ∈ t (p.1) := mem_sigma
theorem sigma_mono {s₁ s₂ : finset α} {t₁ t₂ : Πa, finset (σ a)}
(H1 : s₁ ⊆ s₂) (H2 : ∀a, t₁ a ⊆ t₂ a) : s₁.sigma t₁ ⊆ s₂.sigma t₂ :=
λ ⟨x, sx⟩ H, let ⟨H3, H4⟩ := mem_sigma.1 H in mem_sigma.2 ⟨H1 H3, H2 x H4⟩
theorem sigma_eq_bind [decidable_eq α] [∀a, decidable_eq (σ a)] (s : finset α) (t : Πa, finset (σ a)) :
s.sigma t = s.bind (λa, (t a).image $ λb, ⟨a, b⟩) :=
ext.2 $ λ ⟨x, y⟩, by simp only [mem_sigma, mem_bind, mem_image, exists_prop,
and.left_comm, exists_and_distrib_left, exists_eq_left, heq_iff_eq, exists_eq_right]
end sigma
section pi
variables {δ : α → Type*} [decidable_eq α]
def pi (s : finset α) (t : Πa, finset (δ a)) : finset (Πa∈s, δ a) :=
⟨s.1.pi (λ a, (t a).1), nodup_pi s.2 (λ a _, (t a).2)⟩
@[simp] lemma pi_val (s : finset α) (t : Πa, finset (δ a)) :
(s.pi t).1 = s.1.pi (λ a, (t a).1) := rfl
@[simp] lemma mem_pi {s : finset α} {t : Πa, finset (δ a)} {f : Πa∈s, δ a} :
f ∈ s.pi t ↔ (∀a (h : a ∈ s), f a h ∈ t a) :=
mem_pi _ _ _
def pi.empty (β : α → Sort*) [decidable_eq α] (a : α) (h : a ∈ (∅ : finset α)) : β a :=
multiset.pi.empty β a h
def pi.cons (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (a' : α) (h : a' ∈ insert a s) : δ a' :=
multiset.pi.cons s.1 a b f _ (multiset.mem_cons.2 $ mem_insert.symm.2 h)
@[simp] lemma pi.cons_same (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (h : a ∈ insert a s) :
pi.cons s a b f a h = b :=
multiset.pi.cons_same _
lemma pi.cons_ne {s : finset α} {a a' : α} {b : δ a} {f : Πa, a ∈ s → δ a} {h : a' ∈ insert a s} (ha : a ≠ a') :
pi.cons s a b f a' h = f a' ((mem_insert.1 h).resolve_left ha.symm) :=
multiset.pi.cons_ne _ _
lemma injective_pi_cons {a : α} {b : δ a} {s : finset α} (hs : a ∉ s) :
function.injective (pi.cons s a b) :=
assume e₁ e₂ eq,
@multiset.injective_pi_cons α _ δ a b s.1 hs _ _ $
funext $ assume e, funext $ assume h,
have pi.cons s a b e₁ e (by simpa only [mem_cons, mem_insert] using h) = pi.cons s a b e₂ e (by simpa only [mem_cons, mem_insert] using h),
by rw [eq],
this
@[simp] lemma pi_empty {t : Πa:α, finset (δ a)} :
pi (∅ : finset α) t = singleton (pi.empty δ) := rfl
@[simp] lemma pi_insert [∀a, decidable_eq (δ a)]
{s : finset α} {t : Πa:α, finset (δ a)} {a : α} (ha : a ∉ s) :
pi (insert a s) t = (t a).bind (λb, (pi s t).image (pi.cons s a b)) :=
begin
apply eq_of_veq,
rw ← multiset.erase_dup_eq_self.2 (pi (insert a s) t).2,
refine (λ s' (h : s' = a :: s.1), (_ : erase_dup (multiset.pi s' (λ a, (t a).1)) =
erase_dup ((t a).1.bind $ λ b,
erase_dup $ (multiset.pi s.1 (λ (a : α), (t a).val)).map $
λ f a' h', multiset.pi.cons s.1 a b f a' (h ▸ h')))) _ (insert_val_of_not_mem ha),
subst s', rw pi_cons,
congr, funext b,
rw multiset.erase_dup_eq_self.2,
exact multiset.nodup_map (multiset.injective_pi_cons ha) (pi s t).2,
end
end pi
section powerset
def powerset (s : finset α) : finset (finset α) :=
⟨s.1.powerset.pmap finset.mk
(λ t h, nodup_of_le (mem_powerset.1 h) s.2),
nodup_pmap (λ a ha b hb, congr_arg finset.val)
(nodup_powerset.2 s.2)⟩
@[simp] theorem mem_powerset {s t : finset α} : s ∈ powerset t ↔ s ⊆ t :=
by cases s; simp only [powerset, mem_mk, mem_pmap, mem_powerset, exists_prop, exists_eq_right]; rw ← val_le_iff
@[simp] theorem empty_mem_powerset (s : finset α) : ∅ ∈ powerset s :=
mem_powerset.2 (empty_subset _)
@[simp] theorem mem_powerset_self (s : finset α) : s ∈ powerset s :=
mem_powerset.2 (subset.refl _)
@[simp] theorem powerset_mono {s t : finset α} : powerset s ⊆ powerset t ↔ s ⊆ t :=
⟨λ h, (mem_powerset.1 $ h $ mem_powerset_self _),
λ st u h, mem_powerset.2 $ subset.trans (mem_powerset.1 h) st⟩
@[simp] theorem card_powerset (s : finset α) :
card (powerset s) = 2 ^ card s :=
(card_pmap _ _ _).trans (card_powerset s.1)
end powerset
section fold
variables (op : β → β → β) [hc : is_commutative β op] [ha : is_associative β op]
local notation a * b := op a b
include hc ha
/-- `fold op b f s` folds the commutative associative operation `op` over the
`f`-image of `s`, i.e. `fold (+) b f {1,2,3} = `f 1 + f 2 + f 3 + b`. -/
def fold (b : β) (f : α → β) (s : finset α) : β := (s.1.map f).fold op b
variables {op} {f : α → β} {b : β} {s : finset α} {a : α}
@[simp] theorem fold_empty : (∅ : finset α).fold op b f = b := rfl
@[simp] theorem fold_insert [decidable_eq α] (h : a ∉ s) : (insert a s).fold op b f = f a * s.fold op b f :=
by unfold fold; rw [insert_val, ndinsert_of_not_mem h, map_cons, fold_cons_left]
@[simp] theorem fold_singleton : (singleton a).fold op b f = f a * b := rfl
@[simp] theorem fold_image [decidable_eq α] {g : γ → α} {s : finset γ}
(H : ∀ (x ∈ s) (y ∈ s), g x = g y → x = y) : (s.image g).fold op b f = s.fold op b (f ∘ g) :=
by simp only [fold, image_val_of_inj_on H, multiset.map_map]
@[congr] theorem fold_congr {g : α → β} (H : ∀ x ∈ s, f x = g x) : s.fold op b f = s.fold op b g :=
by rw [fold, fold, map_congr H]
theorem fold_op_distrib {f g : α → β} {b₁ b₂ : β} :
s.fold op (b₁ * b₂) (λx, f x * g x) = s.fold op b₁ f * s.fold op b₂ g :=
by simp only [fold, fold_distrib]
theorem fold_hom {op' : γ → γ → γ} [is_commutative γ op'] [is_associative γ op']
{m : β → γ} (hm : ∀x y, m (op x y) = op' (m x) (m y)) :
s.fold op' (m b) (λx, m (f x)) = m (s.fold op b f) :=
by rw [fold, fold, ← fold_hom op hm, multiset.map_map]
theorem fold_union_inter [decidable_eq α] {s₁ s₂ : finset α} {b₁ b₂ : β} :
(s₁ ∪ s₂).fold op b₁ f * (s₁ ∩ s₂).fold op b₂ f = s₁.fold op b₂ f * s₂.fold op b₁ f :=
by unfold fold; rw [← fold_add op, ← map_add, union_val,
inter_val, union_add_inter, map_add, hc.comm, fold_add]
@[simp] theorem fold_insert_idem [decidable_eq α] [hi : is_idempotent β op] :
(insert a s).fold op b f = f a * s.fold op b f :=
by haveI := classical.prop_decidable;
rw [fold, insert_val', ← fold_erase_dup_idem op, erase_dup_map_erase_dup_eq,
fold_erase_dup_idem op]; simp only [map_cons, fold_cons_left, fold]
end fold
section sup
variables [semilattice_sup_bot α]
/-- Supremum of a finite set: `sup {a, b, c} f = f a ⊔ f b ⊔ f c` -/
def sup (s : finset β) (f : β → α) : α := s.fold (⊔) ⊥ f
variables {s s₁ s₂ : finset β} {f : β → α}
lemma sup_val : s.sup f = (s.1.map f).sup := rfl
@[simp] lemma sup_empty : (∅ : finset β).sup f = ⊥ :=
fold_empty
@[simp] lemma sup_insert [decidable_eq β] {b : β} : (insert b s : finset β).sup f = f b ⊔ s.sup f :=
fold_insert_idem
@[simp] lemma sup_singleton [decidable_eq β] {b : β} : ({b} : finset β).sup f = f b :=
calc _ = f b ⊔ (∅:finset β).sup f : sup_insert
... = f b : sup_bot_eq
lemma sup_union [decidable_eq β] : (s₁ ∪ s₂).sup f = s₁.sup f ⊔ s₂.sup f :=
finset.induction_on s₁ (by rw [empty_union, sup_empty, bot_sup_eq]) $ λ a s has ih,
by rw [insert_union, sup_insert, sup_insert, ih, sup_assoc]
theorem sup_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.sup f = s₂.sup g :=
by subst hs; exact finset.fold_congr hfg
lemma sup_mono_fun {g : β → α} : (∀b∈s, f b ≤ g b) → s.sup f ≤ s.sup g :=
by letI := classical.dec_eq β; from
finset.induction_on s (λ _, le_refl _) (λ a s has ih H,
by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H;
simp only [sup_insert]; exact sup_le_sup H.1 (ih H.2))
lemma le_sup {b : β} (hb : b ∈ s) : f b ≤ s.sup f :=
by letI := classical.dec_eq β; from
calc f b ≤ f b ⊔ s.sup f : le_sup_left
... = (insert b s).sup f : sup_insert.symm
... = s.sup f : by rw [insert_eq_of_mem hb]
lemma sup_le {a : α} : (∀b ∈ s, f b ≤ a) → s.sup f ≤ a :=
by letI := classical.dec_eq β; from
finset.induction_on s (λ _, bot_le) (λ n s hns ih H,
by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H;
simp only [sup_insert]; exact sup_le H.1 (ih H.2))
lemma sup_le_iff {a : α} : s.sup f ≤ a ↔ (∀b ∈ s, f b ≤ a) :=
iff.intro (assume h b hb, le_trans (le_sup hb) h) sup_le
lemma sup_mono (h : s₁ ⊆ s₂) : s₁.sup f ≤ s₂.sup f :=
sup_le $ assume b hb, le_sup (h hb)
lemma sup_lt [is_total α (≤)] {a : α} : (⊥ < a) → (∀b ∈ s, f b < a) → s.sup f < a :=
by letI := classical.dec_eq β; from
finset.induction_on s (by simp) (by simp {contextual := tt})
lemma comp_sup_eq_sup_comp [is_total α (≤)] {γ : Type} [semilattice_sup_bot γ]
(g : α → γ) (mono_g : monotone g) (bot : g ⊥ = ⊥) : g (s.sup f) = s.sup (g ∘ f) :=
have A : ∀x y, g (x ⊔ y) = g x ⊔ g y :=
begin
assume x y,
cases (is_total.total (≤) x y) with h,
{ simp [sup_of_le_right h, sup_of_le_right (mono_g h)] },
{ simp [sup_of_le_left h, sup_of_le_left (mono_g h)] }
end,
by letI := classical.dec_eq β; from
finset.induction_on s (by simp [bot]) (by simp [A] {contextual := tt})
end sup
lemma sup_eq_supr [complete_lattice β] (s : finset α) (f : α → β) : s.sup f = (⨆a∈s, f a) :=
le_antisymm
(finset.sup_le $ assume a ha, le_supr_of_le a $ le_supr _ ha)
(supr_le $ assume a, supr_le $ assume ha, le_sup ha)
section inf
variables [semilattice_inf_top α]
/-- Infimum of a finite set: `inf {a, b, c} f = f a ⊓ f b ⊓ f c` -/
def inf (s : finset β) (f : β → α) : α := s.fold (⊓) ⊤ f
variables {s s₁ s₂ : finset β} {f : β → α}
lemma inf_val : s.inf f = (s.1.map f).inf := rfl
@[simp] lemma inf_empty : (∅ : finset β).inf f = ⊤ :=
fold_empty
@[simp] lemma inf_insert [decidable_eq β] {b : β} : (insert b s : finset β).inf f = f b ⊓ s.inf f :=
fold_insert_idem
@[simp] lemma inf_singleton [decidable_eq β] {b : β} : ({b} : finset β).inf f = f b :=
calc _ = f b ⊓ (∅:finset β).inf f : inf_insert
... = f b : inf_top_eq
lemma inf_union [decidable_eq β] : (s₁ ∪ s₂).inf f = s₁.inf f ⊓ s₂.inf f :=
finset.induction_on s₁ (by rw [empty_union, inf_empty, top_inf_eq]) $ λ a s has ih,
by rw [insert_union, inf_insert, inf_insert, ih, inf_assoc]
theorem inf_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.inf f = s₂.inf g :=
by subst hs; exact finset.fold_congr hfg
lemma inf_mono_fun {g : β → α} : (∀b∈s, f b ≤ g b) → s.inf f ≤ s.inf g :=
by letI := classical.dec_eq β; from
finset.induction_on s (λ _, le_refl _) (λ a s has ih H,
by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H;
simp only [inf_insert]; exact inf_le_inf H.1 (ih H.2))
lemma inf_le {b : β} (hb : b ∈ s) : s.inf f ≤ f b :=
by letI := classical.dec_eq β; from
calc f b ≥ f b ⊓ s.inf f : inf_le_left
... = (insert b s).inf f : inf_insert.symm
... = s.inf f : by rw [insert_eq_of_mem hb]
lemma le_inf {a : α} : (∀b ∈ s, a ≤ f b) → a ≤ s.inf f :=
by letI := classical.dec_eq β; from
finset.induction_on s (λ _, le_top) (λ n s hns ih H,
by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H;
simp only [inf_insert]; exact le_inf H.1 (ih H.2))
lemma le_inf_iff {a : α} : a ≤ s.inf f ↔ (∀b ∈ s, a ≤ f b) :=
iff.intro (assume h b hb, le_trans h (inf_le hb)) le_inf
lemma inf_mono (h : s₁ ⊆ s₂) : s₂.inf f ≤ s₁.inf f :=
le_inf $ assume b hb, inf_le (h hb)
lemma lt_inf [is_total α (≤)] {a : α} : (a < ⊤) → (∀b ∈ s, a < f b) → a < s.inf f :=
by letI := classical.dec_eq β; from
finset.induction_on s (by simp) (by simp {contextual := tt})
lemma comp_inf_eq_inf_comp [is_total α (≤)] {γ : Type} [semilattice_inf_top γ]
(g : α → γ) (mono_g : monotone g) (top : g ⊤ = ⊤) : g (s.inf f) = s.inf (g ∘ f) :=
have A : ∀x y, g (x ⊓ y) = g x ⊓ g y :=
begin
assume x y,
cases (is_total.total (≤) x y) with h,
{ simp [inf_of_le_left h, inf_of_le_left (mono_g h)] },
{ simp [inf_of_le_right h, inf_of_le_right (mono_g h)] }
end,
by letI := classical.dec_eq β; from
finset.induction_on s (by simp [top]) (by simp [A] {contextual := tt})
end inf
lemma inf_eq_infi [complete_lattice β] (s : finset α) (f : α → β) : s.inf f = (⨅a∈s, f a) :=
le_antisymm
(le_infi $ assume a, le_infi $ assume ha, inf_le ha)
(finset.le_inf $ assume a ha, infi_le_of_le a $ infi_le _ ha)
/- max and min of finite sets -/
section max_min
variables [decidable_linear_order α]
protected def max : finset α → option α :=
fold (option.lift_or_get max) none some
theorem max_eq_sup_with_bot (s : finset α) :
s.max = @sup (with_bot α) α _ s some := rfl
@[simp] theorem max_empty : (∅ : finset α).max = none := rfl
@[simp] theorem max_insert {a : α} {s : finset α} :
(insert a s).max = option.lift_or_get max (some a) s.max := fold_insert_idem
@[simp] theorem max_singleton {a : α} : finset.max {a} = some a := max_insert
@[simp] theorem max_singleton' {a : α} : finset.max (singleton a) = some a := max_singleton
theorem max_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.max :=
(@le_sup (with_bot α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst
theorem max_of_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a, a ∈ s.max :=
let ⟨a, ha⟩ := exists_mem_of_ne_empty h in max_of_mem ha
theorem max_eq_none {s : finset α} : s.max = none ↔ s = ∅ :=
⟨λ h, by_contradiction $
λ hs, let ⟨a, ha⟩ := max_of_ne_empty hs in by rw [h] at ha; cases ha,
λ h, h.symm ▸ max_empty⟩
theorem mem_of_max {s : finset α} : ∀ {a : α}, a ∈ s.max → a ∈ s :=
finset.induction_on s (λ _ H, by cases H)
(λ b s _ (ih : ∀ {a}, a ∈ s.max → a ∈ s) a (h : a ∈ (insert b s).max),
begin
by_cases p : b = a,
{ induction p, exact mem_insert_self b s },
{ cases option.lift_or_get_choice max_choice (some b) s.max with q q;
rw [max_insert, q] at h,
{ cases h, cases p rfl },
{ exact mem_insert_of_mem (ih h) } }
end)
theorem le_max_of_mem {s : finset α} {a b : α} (h₁ : a ∈ s) (h₂ : b ∈ s.max) : a ≤ b :=
by rcases @le_sup (with_bot α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩;
cases h₂.symm.trans hb; assumption
protected def min : finset α → option α :=
fold (option.lift_or_get min) none some
theorem min_eq_inf_with_top (s : finset α) :
s.min = @inf (with_top α) α _ s some := rfl
@[simp] theorem min_empty : (∅ : finset α).min = none := rfl
@[simp] theorem min_insert {a : α} {s : finset α} :
(insert a s).min = option.lift_or_get min (some a) s.min :=
fold_insert_idem
@[simp] theorem min_singleton {a : α} : finset.min {a} = some a := min_insert
theorem min_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.min :=
(@inf_le (with_top α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst
theorem min_of_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a, a ∈ s.min :=
let ⟨a, ha⟩ := exists_mem_of_ne_empty h in min_of_mem ha
theorem min_eq_none {s : finset α} : s.min = none ↔ s = ∅ :=
⟨λ h, by_contradiction $
λ hs, let ⟨a, ha⟩ := min_of_ne_empty hs in by rw [h] at ha; cases ha,
λ h, h.symm ▸ min_empty⟩
theorem mem_of_min {s : finset α} : ∀ {a : α}, a ∈ s.min → a ∈ s :=
finset.induction_on s (λ _ H, by cases H) $
λ b s _ (ih : ∀ {a}, a ∈ s.min → a ∈ s) a (h : a ∈ (insert b s).min),
begin
by_cases p : b = a,
{ induction p, exact mem_insert_self b s },
{ cases option.lift_or_get_choice min_choice (some b) s.min with q q;
rw [min_insert, q] at h,
{ cases h, cases p rfl },
{ exact mem_insert_of_mem (ih h) } }
end
theorem le_min_of_mem {s : finset α} {a b : α} (h₁ : b ∈ s) (h₂ : a ∈ s.min) : a ≤ b :=
by rcases @inf_le (with_top α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩;
cases h₂.symm.trans hb; assumption
end max_min
section sort
variables (r : α → α → Prop) [decidable_rel r]
[is_trans α r] [is_antisymm α r] [is_total α r]
/-- `sort s` constructs a sorted list from the unordered set `s`.
(Uses merge sort algorithm.) -/
def sort (s : finset α) : list α := sort r s.1
@[simp] theorem sort_sorted (s : finset α) : list.sorted r (sort r s) :=
sort_sorted _ _
@[simp] theorem sort_eq (s : finset α) : ↑(sort r s) = s.1 :=
sort_eq _ _
@[simp] theorem sort_nodup (s : finset α) : (sort r s).nodup :=
(by rw sort_eq; exact s.2 : @multiset.nodup α (sort r s))
@[simp] theorem sort_to_finset [decidable_eq α] (s : finset α) : (sort r s).to_finset = s :=
list.to_finset_eq (sort_nodup r s) ▸ eq_of_veq (sort_eq r s)
@[simp] theorem mem_sort {s : finset α} {a : α} : a ∈ sort r s ↔ a ∈ s :=
multiset.mem_sort _
end sort
section disjoint
variable [decidable_eq α]
theorem disjoint_left {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t :=
by simp only [_root_.disjoint, inf_eq_inter, le_iff_subset, subset_iff, mem_inter, not_and, and_imp]; refl
theorem disjoint_val {s t : finset α} : disjoint s t ↔ s.1.disjoint t.1 :=
disjoint_left
theorem disjoint_iff_inter_eq_empty {s t : finset α} : disjoint s t ↔ s ∩ t = ∅ :=
disjoint_iff
theorem disjoint_right {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s :=
by rw [disjoint.comm, disjoint_left]
theorem disjoint_iff_ne {s t : finset α} : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b :=
by simp only [disjoint_left, imp_not_comm, forall_eq']
theorem disjoint_of_subset_left {s t u : finset α} (h : s ⊆ u) (d : disjoint u t) : disjoint s t :=
disjoint_left.2 (λ x m₁, (disjoint_left.1 d) (h m₁))
theorem disjoint_of_subset_right {s t u : finset α} (h : t ⊆ u) (d : disjoint s u) : disjoint s t :=
disjoint_right.2 (λ x m₁, (disjoint_right.1 d) (h m₁))
@[simp] theorem disjoint_empty_left (s : finset α) : disjoint ∅ s := disjoint_bot_left
@[simp] theorem disjoint_empty_right (s : finset α) : disjoint s ∅ := disjoint_bot_right
@[simp] theorem singleton_disjoint {s : finset α} {a : α} : disjoint (singleton a) s ↔ a ∉ s :=
by simp only [disjoint_left, mem_singleton, forall_eq]
@[simp] theorem disjoint_singleton {s : finset α} {a : α} : disjoint s (singleton a) ↔ a ∉ s :=
disjoint.comm.trans singleton_disjoint
@[simp] theorem disjoint_insert_left {a : α} {s t : finset α} :
disjoint (insert a s) t ↔ a ∉ t ∧ disjoint s t :=
by simp only [disjoint_left, mem_insert, or_imp_distrib, forall_and_distrib, forall_eq]
@[simp] theorem disjoint_insert_right {a : α} {s t : finset α} :
disjoint s (insert a t) ↔ a ∉ s ∧ disjoint s t :=
disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm]
@[simp] theorem disjoint_union_left {s t u : finset α} :
disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u :=
by simp only [disjoint_left, mem_union, or_imp_distrib, forall_and_distrib]
@[simp] theorem disjoint_union_right {s t u : finset α} :
disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u :=
by simp only [disjoint_right, mem_union, or_imp_distrib, forall_and_distrib]
lemma sdiff_disjoint {s t : finset α} : disjoint (t \ s) s :=
disjoint_left.2 $ assume a ha, (mem_sdiff.1 ha).2
lemma disjoint_sdiff {s t : finset α} : disjoint s (t \ s) :=
sdiff_disjoint.symm
lemma disjoint_bind_left {ι : Type*} [decidable_eq ι]
(s : finset ι) (f : ι → finset α) (t : finset α) :
disjoint (s.bind f) t ↔ (∀i∈s, disjoint (f i) t) :=
begin
refine s.induction _ _,
{ simp only [forall_mem_empty_iff, bind_empty, disjoint_empty_left] },
{ assume i s his ih,
simp only [disjoint_union_left, bind_insert, his, forall_mem_insert, ih] }
end
lemma disjoint_bind_right {ι : Type*} [decidable_eq ι]
(s : finset α) (t : finset ι) (f : ι → finset α) :
disjoint s (t.bind f) ↔ (∀i∈t, disjoint s (f i)) :=
by simpa only [disjoint.comm] using disjoint_bind_left t f s
@[simp] theorem card_disjoint_union {s t : finset α} (h : disjoint s t) :
card (s ∪ t) = card s + card t :=
by rw [← card_union_add_card_inter, disjoint_iff_inter_eq_empty.1 h, card_empty, add_zero]
theorem card_sdiff {s t : finset α} (h : s ⊆ t) : card (t \ s) = card t - card s :=
suffices card (t \ s) = card ((t \ s) ∪ s) - card s, by rwa sdiff_union_of_subset h at this,
by rw [card_disjoint_union sdiff_disjoint, nat.add_sub_cancel]
end disjoint
theorem sort_sorted_lt [decidable_linear_order α] (s : finset α) :
list.sorted (<) (sort (≤) s) :=
(sort_sorted _ _).imp₂ (@lt_of_le_of_ne _ _) (sort_nodup _ _)
instance [has_repr α] : has_repr (finset α) := ⟨λ s, repr s.1⟩
def attach_fin (s : finset ℕ) {n : ℕ} (h : ∀ m ∈ s, m < n) : finset (fin n) :=
⟨s.1.pmap (λ a ha, ⟨a, ha⟩) h, multiset.nodup_pmap (λ _ _ _ _, fin.mk.inj) s.2⟩
@[simp] lemma mem_attach_fin {n : ℕ} {s : finset ℕ} (h : ∀ m ∈ s, m < n) {a : fin n} :
a ∈ s.attach_fin h ↔ a.1 ∈ s :=
⟨λ h, let ⟨b, hb₁, hb₂⟩ := multiset.mem_pmap.1 h in hb₂ ▸ hb₁,
λ h, multiset.mem_pmap.2 ⟨a.1, h, fin.eta _ _⟩⟩
@[simp] lemma card_attach_fin {n : ℕ} (s : finset ℕ) (h : ∀ m ∈ s, m < n) :
(s.attach_fin h).card = s.card := multiset.card_pmap _ _ _
section choose
variables (p : α → Prop) [decidable_pred p] (l : finset α)
def choose_x (hp : (∃! a, a ∈ l ∧ p a)) : { a // a ∈ l ∧ p a } :=
multiset.choose_x p l.val hp
def choose (hp : ∃! a, a ∈ l ∧ p a) : α := choose_x p l hp
lemma choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) :=
(choose_x p l hp).property
lemma choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1
lemma choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2
end choose
theorem lt_wf {α} [decidable_eq α] : well_founded (@has_lt.lt (finset α) _) :=
have H : subrelation (@has_lt.lt (finset α) _)
(inv_image (<) card),
from λ x y hxy, card_lt_card hxy,
subrelation.wf H $ inv_image.wf _ $ nat.lt_wf
section decidable_linear_order
variables {α} [decidable_linear_order α]
def min' (S : finset α) (H : S ≠ ∅) : α :=
@option.get _ S.min $
let ⟨k, hk⟩ := exists_mem_of_ne_empty H in
let ⟨b, hb⟩ := min_of_mem hk in by simp at hb; simp [hb]
def max' (S : finset α) (H : S ≠ ∅) : α :=
@option.get _ S.max $
let ⟨k, hk⟩ := exists_mem_of_ne_empty H in
let ⟨b, hb⟩ := max_of_mem hk in by simp at hb; simp [hb]
variables (S : finset α) (H : S ≠ ∅)
theorem min'_mem : S.min' H ∈ S := mem_of_min $ by simp [min']
theorem min'_le (x) (H2 : x ∈ S) : S.min' H ≤ x := le_min_of_mem H2 $ option.get_mem _
theorem le_min' (x) (H2 : ∀ y ∈ S, x ≤ y) : x ≤ S.min' H := H2 _ $ min'_mem _ _
theorem max'_mem : S.max' H ∈ S := mem_of_max $ by simp [max']
theorem le_max' (x) (H2 : x ∈ S) : x ≤ S.max' H := le_max_of_mem H2 $ option.get_mem _
theorem max'_le (x) (H2 : ∀ y ∈ S, y ≤ x) : S.max' H ≤ x := H2 _ $ max'_mem _ _
theorem min'_lt_max' {i j} (H1 : i ∈ S) (H2 : j ∈ S) (H3 : i ≠ j) : S.min' H < S.max' H :=
begin
rcases lt_trichotomy i j with H4 | H4 | H4,
{ have H5 := min'_le S H i H1,
have H6 := le_max' S H j H2,
apply lt_of_le_of_lt H5,
apply lt_of_lt_of_le H4 H6 },
{ cc },
{ have H5 := min'_le S H j H2,
have H6 := le_max' S H i H1,
apply lt_of_le_of_lt H5,
apply lt_of_lt_of_le H4 H6 }
end
end decidable_linear_order
/- Ico (a closed openinterval) -/
variables {n m l : ℕ}
/-- `Ico n m` is the set of natural numbers `n ≤ k < m`. -/
def Ico (n m : ℕ) : finset ℕ := ⟨_, Ico.nodup n m⟩
namespace Ico
@[simp] theorem val (n m : ℕ) : (Ico n m).1 = multiset.Ico n m := rfl
@[simp] theorem to_finset (n m : ℕ) : (multiset.Ico n m).to_finset = Ico n m :=
(multiset.to_finset_eq _).symm
theorem map_add (n m k : ℕ) : (Ico n m).image ((+) k) = Ico (n + k) (m + k) :=
by simp [image, multiset.Ico.map_add]
theorem zero_bot (n : ℕ) : Ico 0 n = range n :=
eq_of_veq $ multiset.Ico.zero_bot _
@[simp] theorem card (n m : ℕ) : (Ico n m).card = m - n :=
multiset.Ico.card _ _
@[simp] theorem mem {n m l : ℕ} : l ∈ Ico n m ↔ n ≤ l ∧ l < m :=
multiset.Ico.mem
theorem eq_empty_of_le {n m : ℕ} (h : m ≤ n) : Ico n m = ∅ :=
eq_of_veq $ multiset.Ico.eq_zero_of_le h
@[simp] theorem self_eq_empty {n : ℕ} : Ico n n = ∅ :=
eq_empty_of_le $ le_refl n
@[simp] theorem eq_empty_iff {n m : ℕ} : Ico n m = ∅ ↔ m ≤ n :=
iff.trans val_eq_zero.symm multiset.Ico.eq_zero_iff
lemma union_consecutive {n m l : ℕ} (hnm : n ≤ m) (hml : m ≤ l) :
Ico n m ∪ Ico m l = Ico n l :=
by rw [← to_finset, ← to_finset, ← multiset.to_finset_add,
multiset.Ico.add_consecutive hnm hml, to_finset]
@[simp] theorem succ_singleton {n : ℕ} : Ico n (n+1) = {n} :=
eq_of_veq $ multiset.Ico.succ_singleton
theorem succ_top {n m : ℕ} (h : n ≤ m) : Ico n (m + 1) = insert m (Ico n m) :=
by rw [← to_finset, multiset.Ico.succ_top h, multiset.to_finset_cons, to_finset]
theorem eq_cons {n m : ℕ} (h : n < m) : Ico n m = insert n (Ico (n + 1) m) :=
by rw [← to_finset, multiset.Ico.eq_cons h, multiset.to_finset_cons, to_finset]
theorem pred_singleton {m : ℕ} (h : m > 0) : Ico (m - 1) m = {m - 1} :=
eq_of_veq $ multiset.Ico.pred_singleton h
@[simp] theorem not_mem_top {n m : ℕ} : m ∉ Ico n m :=
multiset.Ico.not_mem_top
lemma filter_lt_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, x < l) = Ico n m :=
eq_of_veq $ multiset.Ico.filter_lt_of_top_le hml
lemma filter_lt_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, x < l) = ∅ :=
eq_of_veq $ multiset.Ico.filter_lt_of_le_bot hln
lemma filter_lt_of_ge {n m l : ℕ} (hlm : l ≤ m) : (Ico n m).filter (λ x, x < l) = Ico n l :=
eq_of_veq $ multiset.Ico.filter_lt_of_ge hlm
@[simp] lemma filter_lt (n m l : ℕ) : (Ico n m).filter (λ x, x < l) = Ico n (min m l) :=
eq_of_veq $ multiset.Ico.filter_lt n m l
lemma filter_ge_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, x ≥ l) = Ico n m :=
eq_of_veq $ multiset.Ico.filter_ge_of_le_bot hln
lemma filter_ge_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, x ≥ l) = ∅ :=
eq_of_veq $ multiset.Ico.filter_ge_of_top_le hml
lemma filter_ge_of_ge {n m l : ℕ} (hnl : n ≤ l) : (Ico n m).filter (λ x, x ≥ l) = Ico l m :=
eq_of_veq $ multiset.Ico.filter_ge_of_ge hnl
@[simp] lemma filter_ge (n m l : ℕ) : (Ico n m).filter (λ x, x ≥ l) = Ico (max n l) m :=
eq_of_veq $ multiset.Ico.filter_ge n m l
@[simp] lemma diff_left (l n m : ℕ) : (Ico n m) \ (Ico n l) = Ico (max n l) m :=
by ext k; by_cases n ≤ k; simp [h, and_comm]
@[simp] lemma diff_right (l n m : ℕ) : (Ico n m) \ (Ico l m) = Ico n (min m l) :=
have ∀k, (k < m ∧ (l ≤ k → m ≤ k)) ↔ (k < m ∧ k < l) :=
assume k, and_congr_right $ assume hk, by rw [← not_imp_not]; simp [hk],
by ext k; by_cases n ≤ k; simp [h, this]
end Ico
end finset
namespace multiset
lemma count_sup [decidable_eq β] (s : finset α) (f : α → multiset β) (b : β) :
count b (s.sup f) = s.sup (λa, count b (f a)) :=
begin
letI := classical.dec_eq α,
refine s.induction _ _,
{ exact count_zero _ },
{ assume i s his ih,
rw [finset.sup_insert, sup_eq_union, count_union, finset.sup_insert, ih],
refl }
end
end multiset
namespace list
variable [decidable_eq α]
theorem to_finset_card_of_nodup {l : list α} (h : l.nodup) : l.to_finset.card = l.length :=
congr_arg card $ (@multiset.erase_dup_eq_self α _ l).2 h
end list
namespace lattice
variables {ι : Sort*} [complete_lattice α] [decidable_eq ι]
lemma supr_eq_supr_finset (s : ι → α) : (⨆i, s i) = (⨆t:finset (plift ι), ⨆i∈t, s (plift.down i)) :=
le_antisymm
(supr_le $ assume b, le_supr_of_le {plift.up b} $ le_supr_of_le (plift.up b) $ le_supr_of_le
(by simp) $ le_refl _)
(supr_le $ assume t, supr_le $ assume b, supr_le $ assume hb, le_supr _ _)
lemma infi_eq_infi_finset (s : ι → α) : (⨅i, s i) = (⨅t:finset (plift ι), ⨅i∈t, s (plift.down i)) :=
le_antisymm
(le_infi $ assume t, le_infi $ assume b, le_infi $ assume hb, infi_le _ _)
(le_infi $ assume b, infi_le_of_le {plift.up b} $ infi_le_of_le (plift.up b) $ infi_le_of_le
(by simp) $ le_refl _)
end lattice
namespace set
variables {ι : Sort*} [decidable_eq ι]
lemma Union_eq_Union_finset (s : ι → set α) :
(⋃i, s i) = (⋃t:finset (plift ι), ⋃i∈t, s (plift.down i)) :=
lattice.supr_eq_supr_finset s
lemma Inter_eq_Inter_finset (s : ι → set α) :
(⋂i, s i) = (⋂t:finset (plift ι), ⋂i∈t, s (plift.down i)) :=
lattice.infi_eq_infi_finset s
end set
|
8afffb3f316fff3dbaee3fa4adda6079895d5ffe | 4727251e0cd73359b15b664c3170e5d754078599 | /src/field_theory/finite/basic.lean | d1494e70e2aef3fd3c939a0d70b3f41f1914bfb4 | [
"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 | 16,155 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Joey van Langen, Casper Putz
-/
import tactic.apply_fun
import algebra.ring.equiv
import data.zmod.algebra
import linear_algebra.finite_dimensional
import ring_theory.integral_domain
import field_theory.separable
/-!
# Finite fields
This file contains basic results about finite fields.
Throughout most of this file, `K` denotes a finite field
and `q` is notation for the cardinality of `K`.
See `ring_theory.integral_domain` for the fact that the unit group of a finite field is a
cyclic group, as well as the fact that every finite integral domain is a field
(`fintype.field_of_domain`).
## Main results
1. `fintype.card_units`: The unit group of a finite field is has cardinality `q - 1`.
2. `sum_pow_units`: The sum of `x^i`, where `x` ranges over the units of `K`, is
- `q-1` if `q-1 ∣ i`
- `0` otherwise
3. `finite_field.card`: The cardinality `q` is a power of the characteristic of `K`.
See `card'` for a variant.
## Notation
Throughout most of this file, `K` denotes a finite field
and `q` is notation for the cardinality of `K`.
## Implementation notes
While `fintype Kˣ` can be inferred from `fintype K` in the presence of `decidable_eq K`,
in this file we take the `fintype Kˣ` argument directly to reduce the chance of typeclass
diamonds, as `fintype` carries data.
-/
variables {K : Type*} {R : Type*}
local notation `q` := fintype.card K
open_locale big_operators polynomial
namespace finite_field
open finset function
section polynomial
variables [comm_ring R] [is_domain R]
open polynomial
/-- The cardinality of a field is at most `n` times the cardinality of the image of a degree `n`
polynomial -/
lemma card_image_polynomial_eval [decidable_eq R] [fintype R] {p : R[X]}
(hp : 0 < p.degree) : fintype.card R ≤ nat_degree p * (univ.image (λ x, eval x p)).card :=
finset.card_le_mul_card_image _ _
(λ a _, calc _ = (p - C a).roots.to_finset.card : congr_arg card
(by simp [finset.ext_iff, mem_roots_sub_C hp])
... ≤ (p - C a).roots.card : multiset.to_finset_card_le _
... ≤ _ : card_roots_sub_C' hp)
/-- If `f` and `g` are quadratic polynomials, then the `f.eval a + g.eval b = 0` has a solution. -/
lemma exists_root_sum_quadratic [fintype R] {f g : R[X]} (hf2 : degree f = 2)
(hg2 : degree g = 2) (hR : fintype.card R % 2 = 1) : ∃ a b, f.eval a + g.eval b = 0 :=
by letI := classical.dec_eq R; exact
suffices ¬ disjoint (univ.image (λ x : R, eval x f)) (univ.image (λ x : R, eval x (-g))),
begin
simp only [disjoint_left, mem_image] at this,
push_neg at this,
rcases this with ⟨x, ⟨a, _, ha⟩, ⟨b, _, hb⟩⟩,
exact ⟨a, b, by rw [ha, ← hb, eval_neg, neg_add_self]⟩
end,
assume hd : disjoint _ _,
lt_irrefl (2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card) $
calc 2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card
≤ 2 * fintype.card R : nat.mul_le_mul_left _ (finset.card_le_univ _)
... = fintype.card R + fintype.card R : two_mul _
... < nat_degree f * (univ.image (λ x : R, eval x f)).card +
nat_degree (-g) * (univ.image (λ x : R, eval x (-g))).card :
add_lt_add_of_lt_of_le
(lt_of_le_of_ne
(card_image_polynomial_eval (by rw hf2; exact dec_trivial))
(mt (congr_arg (%2)) (by simp [nat_degree_eq_of_degree_eq_some hf2, hR])))
(card_image_polynomial_eval (by rw [degree_neg, hg2]; exact dec_trivial))
... = 2 * (univ.image (λ x : R, eval x f) ∪ univ.image (λ x : R, eval x (-g))).card :
by rw [card_disjoint_union hd]; simp [nat_degree_eq_of_degree_eq_some hf2,
nat_degree_eq_of_degree_eq_some hg2, bit0, mul_add]
end polynomial
lemma prod_univ_units_id_eq_neg_one [comm_ring K] [is_domain K] [fintype Kˣ] :
(∏ x : Kˣ, x) = (-1 : Kˣ) :=
begin
classical,
have : (∏ x in (@univ Kˣ _).erase (-1), x) = 1,
from prod_involution (λ x _, x⁻¹) (by simp)
(λ a, by simp [units.inv_eq_self_iff] {contextual := tt})
(λ a, by simp [@inv_eq_iff_inv_eq _ _ a, eq_comm])
(by simp),
rw [← insert_erase (mem_univ (-1 : Kˣ)), prod_insert (not_mem_erase _ _),
this, mul_one]
end
section
variables [group_with_zero K] [fintype K]
lemma pow_card_sub_one_eq_one (a : K) (ha : a ≠ 0) : a ^ (q - 1) = 1 :=
calc a ^ (fintype.card K - 1) = (units.mk0 a ha ^ (fintype.card K - 1) : Kˣ) :
by rw [units.coe_pow, units.coe_mk0]
... = 1 : by { classical, rw [← fintype.card_units, pow_card_eq_one], refl }
lemma pow_card (a : K) : a ^ q = a :=
begin
have hp : 0 < fintype.card K := lt_trans zero_lt_one fintype.one_lt_card,
by_cases h : a = 0, { rw h, apply zero_pow hp },
rw [← nat.succ_pred_eq_of_pos hp, pow_succ, nat.pred_eq_sub_one,
pow_card_sub_one_eq_one a h, mul_one],
end
lemma pow_card_pow (n : ℕ) (a : K) : a ^ q ^ n = a :=
begin
induction n with n ih,
{ simp, },
{ simp [pow_succ, pow_mul, ih, pow_card], },
end
end
variables (K) [field K] [fintype K]
theorem card (p : ℕ) [char_p K p] : ∃ (n : ℕ+), nat.prime p ∧ q = p^(n : ℕ) :=
begin
haveI hp : fact p.prime := ⟨char_p.char_is_prime K p⟩,
letI : module (zmod p) K := { .. (zmod.cast_hom dvd_rfl K : zmod p →+* _).to_module },
obtain ⟨n, h⟩ := vector_space.card_fintype (zmod p) K,
rw zmod.card at h,
refine ⟨⟨n, _⟩, hp.1, h⟩,
apply or.resolve_left (nat.eq_zero_or_pos n),
rintro rfl,
rw pow_zero at h,
have : (0 : K) = 1, { apply fintype.card_le_one_iff.mp (le_of_eq h) },
exact absurd this zero_ne_one,
end
-- this statement doesn't use `q` because we want `K` to be an explicit parameter
theorem card' : ∃ (p : ℕ) (n : ℕ+), nat.prime p ∧ fintype.card K = p^(n : ℕ) :=
let ⟨p, hc⟩ := char_p.exists K in ⟨p, @finite_field.card K _ _ p hc⟩
@[simp] lemma cast_card_eq_zero : (q : K) = 0 :=
begin
rcases char_p.exists K with ⟨p, _char_p⟩, resetI,
rcases card K p with ⟨n, hp, hn⟩,
simp only [char_p.cast_eq_zero_iff K p, hn],
conv { congr, rw [← pow_one p] },
exact pow_dvd_pow _ n.2,
end
lemma forall_pow_eq_one_iff (i : ℕ) :
(∀ x : Kˣ, x ^ i = 1) ↔ q - 1 ∣ i :=
begin
classical,
obtain ⟨x, hx⟩ := is_cyclic.exists_generator Kˣ,
rw [←fintype.card_units, ←order_of_eq_card_of_forall_mem_zpowers hx, order_of_dvd_iff_pow_eq_one],
split,
{ intro h, apply h },
{ intros h y,
simp_rw ← mem_powers_iff_mem_zpowers at hx,
rcases hx y with ⟨j, rfl⟩,
rw [← pow_mul, mul_comm, pow_mul, h, one_pow], }
end
/-- The sum of `x ^ i` as `x` ranges over the units of a finite field of cardinality `q`
is equal to `0` unless `(q - 1) ∣ i`, in which case the sum is `q - 1`. -/
lemma sum_pow_units [fintype Kˣ] (i : ℕ) :
∑ x : Kˣ, (x ^ i : K) = if (q - 1) ∣ i then -1 else 0 :=
begin
let φ : Kˣ →* K :=
{ to_fun := λ x, x ^ i,
map_one' := by rw [units.coe_one, one_pow],
map_mul' := by { intros, rw [units.coe_mul, mul_pow] } },
haveI : decidable (φ = 1), { classical, apply_instance },
calc ∑ x : Kˣ, φ x = if φ = 1 then fintype.card Kˣ else 0 : sum_hom_units φ
... = if (q - 1) ∣ i then -1 else 0 : _,
suffices : (q - 1) ∣ i ↔ φ = 1,
{ simp only [this],
split_ifs with h h, swap, refl,
rw [fintype.card_units, nat.cast_sub, cast_card_eq_zero, nat.cast_one, zero_sub],
show 1 ≤ q, from fintype.card_pos_iff.mpr ⟨0⟩ },
rw [← forall_pow_eq_one_iff, monoid_hom.ext_iff],
apply forall_congr, intro x,
rw [units.ext_iff, units.coe_pow, units.coe_one, monoid_hom.one_apply],
refl,
end
/-- The sum of `x ^ i` as `x` ranges over a finite field of cardinality `q`
is equal to `0` if `i < q - 1`. -/
lemma sum_pow_lt_card_sub_one (i : ℕ) (h : i < q - 1) :
∑ x : K, x ^ i = 0 :=
begin
by_cases hi : i = 0,
{ simp only [hi, nsmul_one, sum_const, pow_zero, card_univ, cast_card_eq_zero], },
classical,
have hiq : ¬ (q - 1) ∣ i, { contrapose! h, exact nat.le_of_dvd (nat.pos_of_ne_zero hi) h },
let φ : Kˣ ↪ K := ⟨coe, units.ext⟩,
have : univ.map φ = univ \ {0},
{ ext x,
simp only [true_and, embedding.coe_fn_mk, mem_sdiff, units.exists_iff_ne_zero,
mem_univ, mem_map, exists_prop_of_true, mem_singleton] },
calc ∑ x : K, x ^ i = ∑ x in univ \ {(0 : K)}, x ^ i :
by rw [← sum_sdiff ({0} : finset K).subset_univ, sum_singleton,
zero_pow (nat.pos_of_ne_zero hi), add_zero]
... = ∑ x : Kˣ, x ^ i : by { rw [← this, univ.sum_map φ], refl }
... = 0 : by { rw [sum_pow_units K i, if_neg], exact hiq, }
end
section is_splitting_field
open polynomial
section
variables (K' : Type*) [field K'] {p n : ℕ}
lemma X_pow_card_sub_X_nat_degree_eq (hp : 1 < p) :
(X ^ p - X : K'[X]).nat_degree = p :=
begin
have h1 : (X : K'[X]).degree < (X ^ p : K'[X]).degree,
{ rw [degree_X_pow, degree_X],
exact_mod_cast hp },
rw [nat_degree_eq_of_degree_eq (degree_sub_eq_left_of_degree_lt h1), nat_degree_X_pow],
end
lemma X_pow_card_pow_sub_X_nat_degree_eq (hn : n ≠ 0) (hp : 1 < p) :
(X ^ p ^ n - X : K'[X]).nat_degree = p ^ n :=
X_pow_card_sub_X_nat_degree_eq K' $ nat.one_lt_pow _ _ (nat.pos_of_ne_zero hn) hp
lemma X_pow_card_sub_X_ne_zero (hp : 1 < p) : (X ^ p - X : K'[X]) ≠ 0 :=
ne_zero_of_nat_degree_gt $
calc 1 < _ : hp
... = _ : (X_pow_card_sub_X_nat_degree_eq K' hp).symm
lemma X_pow_card_pow_sub_X_ne_zero (hn : n ≠ 0) (hp : 1 < p) :
(X ^ p ^ n - X : K'[X]) ≠ 0 :=
X_pow_card_sub_X_ne_zero K' $ nat.one_lt_pow _ _ (nat.pos_of_ne_zero hn) hp
end
variables (p : ℕ) [fact p.prime] [char_p K p]
lemma roots_X_pow_card_sub_X : roots (X^q - X : K[X]) = finset.univ.val :=
begin
classical,
have aux : (X^q - X : K[X]) ≠ 0 := X_pow_card_sub_X_ne_zero K fintype.one_lt_card,
have : (roots (X^q - X : K[X])).to_finset = finset.univ,
{ rw eq_univ_iff_forall,
intro x,
rw [multiset.mem_to_finset, mem_roots aux, is_root.def, eval_sub, eval_pow, eval_X, sub_eq_zero,
pow_card] },
rw [←this, multiset.to_finset_val, eq_comm, multiset.dedup_eq_self],
apply nodup_roots,
rw separable_def,
convert is_coprime_one_right.neg_right using 1,
{ rw [derivative_sub, derivative_X, derivative_X_pow, ←C_eq_nat_cast,
C_eq_zero.mpr (char_p.cast_card_eq_zero K), zero_mul, zero_sub], },
end
instance : is_splitting_field (zmod p) K (X^q - X) :=
{ splits :=
begin
have h : (X^q - X : K[X]).nat_degree = q :=
X_pow_card_sub_X_nat_degree_eq K fintype.one_lt_card,
rw [←splits_id_iff_splits, splits_iff_card_roots, polynomial.map_sub, polynomial.map_pow,
map_X, h, roots_X_pow_card_sub_X K, ←finset.card_def, finset.card_univ],
end,
adjoin_roots :=
begin
classical,
transitivity algebra.adjoin (zmod p) ((roots (X^q - X : K[X])).to_finset : set K),
{ simp only [polynomial.map_pow, map_X, polynomial.map_sub], convert rfl },
{ rw [roots_X_pow_card_sub_X, val_to_finset, coe_univ, algebra.adjoin_univ], }
end }
end is_splitting_field
variables {K}
theorem frobenius_pow {p : ℕ} [fact p.prime] [char_p K p] {n : ℕ} (hcard : q = p^n) :
(frobenius K p) ^ n = 1 :=
begin
ext, conv_rhs { rw [ring_hom.one_def, ring_hom.id_apply, ← pow_card x, hcard], }, clear hcard,
induction n, {simp},
rw [pow_succ, pow_succ', pow_mul, ring_hom.mul_def, ring_hom.comp_apply, frobenius_def, n_ih]
end
open polynomial
lemma expand_card (f : K[X]) :
expand K q f = f ^ q :=
begin
cases char_p.exists K with p hp,
letI := hp,
rcases finite_field.card K p with ⟨⟨n, npos⟩, ⟨hp, hn⟩⟩,
haveI : fact p.prime := ⟨hp⟩,
dsimp at hn,
rw [hn, ← map_expand_pow_char, frobenius_pow hn, ring_hom.one_def, map_id]
end
end finite_field
namespace zmod
open finite_field polynomial
lemma sq_add_sq (p : ℕ) [hp : fact p.prime] (x : zmod p) :
∃ a b : zmod p, a^2 + b^2 = x :=
begin
cases hp.1.eq_two_or_odd with hp2 hp_odd,
{ substI p, change fin 2 at x, fin_cases x, { use 0, simp }, { use [0, 1], simp } },
let f : (zmod p)[X] := X^2,
let g : (zmod p)[X] := X^2 - C x,
obtain ⟨a, b, hab⟩ : ∃ a b, f.eval a + g.eval b = 0 :=
@exists_root_sum_quadratic _ _ _ _ f g
(degree_X_pow 2) (degree_X_pow_sub_C dec_trivial _) (by rw [zmod.card, hp_odd]),
refine ⟨a, b, _⟩,
rw ← sub_eq_zero,
simpa only [eval_C, eval_X, eval_pow, eval_sub, ← add_sub_assoc] using hab,
end
end zmod
namespace char_p
lemma sq_add_sq (R : Type*) [comm_ring R] [is_domain R]
(p : ℕ) [fact (0 < p)] [char_p R p] (x : ℤ) :
∃ a b : ℕ, (a^2 + b^2 : R) = x :=
begin
haveI := char_is_prime_of_pos R p,
obtain ⟨a, b, hab⟩ := zmod.sq_add_sq p x,
refine ⟨a.val, b.val, _⟩,
simpa using congr_arg (zmod.cast_hom dvd_rfl R) hab
end
end char_p
open_locale nat
open zmod
/-- The **Fermat-Euler totient theorem**. `nat.modeq.pow_totient` is an alternative statement
of the same theorem. -/
@[simp] lemma zmod.pow_totient {n : ℕ} [fact (0 < n)] (x : (zmod n)ˣ) : x ^ φ n = 1 :=
by rw [← card_units_eq_totient, pow_card_eq_one]
/-- The **Fermat-Euler totient theorem**. `zmod.pow_totient` is an alternative statement
of the same theorem. -/
lemma nat.modeq.pow_totient {x n : ℕ} (h : nat.coprime x n) : x ^ φ n ≡ 1 [MOD n] :=
begin
cases n, {simp},
rw ← zmod.eq_iff_modeq_nat,
let x' : units (zmod (n+1)) := zmod.unit_of_coprime _ h,
have := zmod.pow_totient x',
apply_fun (coe : units (zmod (n+1)) → zmod (n+1)) at this,
simpa only [-zmod.pow_totient, nat.succ_eq_add_one, nat.cast_pow, units.coe_one,
nat.cast_one, coe_unit_of_coprime, units.coe_pow],
end
section
variables {V : Type*} [fintype K] [division_ring K] [add_comm_group V] [module K V]
-- should this go in a namespace?
-- finite_dimensional would be natural,
-- but we don't assume it...
lemma card_eq_pow_finrank [fintype V] :
fintype.card V = q ^ (finite_dimensional.finrank K V) :=
begin
let b := is_noetherian.finset_basis K V,
rw [module.card_fintype b, ← finite_dimensional.finrank_eq_card_basis b],
end
end
open finite_field
namespace zmod
/-- A variation on Fermat's little theorem. See `zmod.pow_card_sub_one_eq_one` -/
@[simp] lemma pow_card {p : ℕ} [fact p.prime] (x : zmod p) : x ^ p = x :=
by { have h := finite_field.pow_card x, rwa zmod.card p at h }
@[simp] lemma pow_card_pow {n p : ℕ} [fact p.prime] (x : zmod p) : x ^ p ^ n = x :=
begin
induction n with n ih,
{ simp, },
{ simp [pow_succ, pow_mul, ih, pow_card], },
end
@[simp] lemma frobenius_zmod (p : ℕ) [fact p.prime] :
frobenius (zmod p) p = ring_hom.id _ :=
by { ext a, rw [frobenius_def, zmod.pow_card, ring_hom.id_apply] }
@[simp] lemma card_units (p : ℕ) [fact p.prime] : fintype.card ((zmod p)ˣ) = p - 1 :=
by rw [fintype.card_units, card]
/-- **Fermat's Little Theorem**: for every unit `a` of `zmod p`, we have `a ^ (p - 1) = 1`. -/
theorem units_pow_card_sub_one_eq_one (p : ℕ) [fact p.prime] (a : (zmod p)ˣ) :
a ^ (p - 1) = 1 :=
by rw [← card_units p, pow_card_eq_one]
/-- **Fermat's Little Theorem**: for all nonzero `a : zmod p`, we have `a ^ (p - 1) = 1`. -/
theorem pow_card_sub_one_eq_one {p : ℕ} [fact p.prime] {a : zmod p} (ha : a ≠ 0) :
a ^ (p - 1) = 1 :=
by { have h := pow_card_sub_one_eq_one a ha, rwa zmod.card p at h }
open polynomial
lemma expand_card {p : ℕ} [fact p.prime] (f : polynomial (zmod p)) :
expand (zmod p) p f = f ^ p :=
by { have h := finite_field.expand_card f, rwa zmod.card p at h }
end zmod
/-- **Fermat's Little Theorem**: for all `a : ℤ` coprime to `p`, we have
`a ^ (p - 1) ≡ 1 [ZMOD p]`. -/
lemma int.modeq.pow_card_sub_one_eq_one {p : ℕ} (hp : nat.prime p) {n : ℤ} (hpn : is_coprime n p) :
n ^ (p - 1) ≡ 1 [ZMOD p] :=
begin
haveI : fact p.prime := ⟨hp⟩,
have : ¬ (n : zmod p) = 0,
{ rw [char_p.int_cast_eq_zero_iff _ p, ← (nat.prime_iff_prime_int.mp hp).coprime_iff_not_dvd],
{ exact hpn.symm },
exact zmod.char_p p },
simpa [← zmod.int_coe_eq_int_coe_iff] using zmod.pow_card_sub_one_eq_one this
end
|
85a68927d63567d425baa1c316105d2a2d258d90 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/matrix.lean | 793b1a197fca79b3ba3394d071228ef6da216487 | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 1,089 | lean | constant {u} matrix : Type u → Type u
constant same_dim {A : Type} : matrix A → matrix A → Prop
constant add1 {A : Type} (m1 m2 : matrix A) {H : same_dim m1 m2} : matrix A
theorem same_dim_irrel {A : Type} {m1 m2 : matrix A} {H1 H2 : same_dim m1 m2} : @add1 A m1 m2 H1 = @add1 A m1 m2 H2 :=
rfl
open eq
theorem same_dim_eq_args {A : Type} {m1 m2 m1' m2' : matrix A} (H1 : m1 = m1') (H2 : m2 = m2') (H : same_dim m1 m2) : same_dim m1' m2' :=
subst H1 (subst H2 H)
theorem add1_congr {A : Type} (m1 m2 m1' m2' : matrix A) (H1 : m1 = m1') (H2 : m2 = m2') (H : same_dim m1 m2) : @add1 A m1 m2 H = @add1 A m1' m2' (same_dim_eq_args H1 H2 H) :=
have base : ∀ (H1 : m1 = m1) (H2 : m2 = m2), @add1 A m1 m2 H = @add1 A m1 m2 (eq.rec (eq.rec H H1) H2), from
assume H1 H2, rfl,
have general : ∀ (H1 : m1 = m1') (H2 : m2 = m2'), @add1 A m1 m2 H = @add1 A m1' m2' (eq.rec (eq.rec H H1) H2), from
subst H1 (subst H2 base),
calc @add1 A m1 m2 H = @add1 A m1' m2' (eq.rec (eq.rec H H1) H2) : general H1 H2
... = @add1 A m1' m2' (same_dim_eq_args H1 H2 H) : same_dim_irrel
|
dd6a9d23e2ed199f6765c6833241bb994613af3d | a1179fa077c09acc49e4fbc8d67084ba89ac4f4c | /tutorials/src/solutions/07_first_negations.lean | 995201c117b5445b44bd09a374352499225e5e6f | [] | no_license | Seeram/Lean-proof-assistant | 11ca0ca0e0446bacdd1773c4c481a3653b2f1074 | e672d46e0e5f39d8de2933ad4f4cac095ca6094f | refs/heads/master | 1,682,754,224,366 | 1,620,959,431,000 | 1,620,959,431,000 | 299,000,950 | 0 | 1 | null | 1,620,680,462,000 | 1,601,200,258,000 | Lean | UTF-8 | Lean | false | false | 8,388 | lean | import tuto_lib
import data.int.parity
/-
Negations, proof by contradiction and contraposition.
This file introduces the logical rules and tactics related to negation:
exfalso, by_contradiction, contrapose, by_cases and push_neg.
There is a special statement denoted by `false` which, by definition,
has no proof.
So `false` implies everything. Indeed `false → P` means any proof of
`false` could be turned into a proof of P.
This fact is known by its latin name
"ex falso quod libet" (from false follows whatever you want).
Hence Lean's tactic to invoke this is called `exfalso`.
-/
example : false → 0 = 1 :=
begin
intro h,
exfalso,
exact h,
end
/-
The preceding example suggests that this definition of `false` isn't very useful.
But actually it allows us to define the negation of a statement P as
"P implies false" that we can read as "if P were true, we would get
a contradiction". Lean denotes this by `¬ P`.
One can prove that (¬ P) ↔ (P ↔ false). But in practice we directly
use the definition of `¬ P`.
-/
example {x : ℝ} : ¬ x < x :=
begin
intro hyp,
rw lt_iff_le_and_ne at hyp,
cases hyp with hyp_inf hyp_non,
clear hyp_inf, -- we won't use that one, so let's discard it
change x = x → false at hyp_non, -- Lean doesn't need this psychological line
apply hyp_non,
refl,
end
open int
-- 0045
example (n : ℤ) (h_pair : even n) (h_non_pair : ¬ even n) : 0 = 1 :=
begin
-- sorry
exfalso,
exact h_non_pair h_pair,
-- sorry
end
-- 0046
example (P Q : Prop) (h₁ : P ∨ Q) (h₂ : ¬ (P ∧ Q)) : ¬ P ↔ Q :=
begin
-- sorry
split,
{ intro hnP,
cases h₁ with hP hQ,
{ exfalso,
exact hnP hP, },
{ exact hQ }, },
{ intros hQ hP,
exact h₂ ⟨hP, hQ⟩ },
-- sorry
end
/-
The definition of negation easily implies that, for every statement P,
P → ¬ ¬ P
The excluded middle axiom, which asserts P ∨ ¬ P allows us to
prove the converse implication.
Together those two implications form the principle of double negation elimination.
not_not {P : Prop} : (¬ ¬ P) ↔ P
The implication `¬ ¬ P → P` is the basis for proofs by contradiction:
in order to prove P, it suffices to prove ¬¬ P, ie `¬ P → false`.
Of course there is no need to keep explaining all this. The tactic
`by_contradiction Hyp` will transform any goal P into `false` and
add Hyp : ¬ P to the local context.
Let's return to a proof from the 5th file: uniqueness of limits for a sequence.
This cannot be proved without using some version of the excluded middle
axiom. We used it secretely in
eq_of_abs_sub_le_all (x y : ℝ) : (∀ ε > 0, |x - y| ≤ ε) → x = y
(we'll prove a variation on this lemma below).
-/
example (u : ℕ → ℝ) (l l' : ℝ) : seq_limit u l → seq_limit u l' → l = l' :=
begin
intros hl hl',
by_contradiction H,
change l ≠ l' at H, -- Lean does not need this line
have ineg : |l-l'| > 0,
exact abs_pos.mpr (sub_ne_zero_of_ne H),
cases hl ( |l-l'|/4 ) (by linarith) with N hN,
cases hl' ( |l-l'|/4 ) (by linarith) with N' hN',
let N₀ := max N N', -- this is a new tactic, whose effect should be clear
specialize hN N₀ (le_max_left _ _),
specialize hN' N₀ (le_max_right _ _),
have clef : |l-l'| < |l-l'|,
calc
|l - l'| = |(l-u N₀) + (u N₀ -l')| : by ring
... ≤ |l - u N₀| + |u N₀ - l'| : by apply abs_add
... = |u N₀ - l| + |u N₀ - l'| : by rw abs_sub
... < |l-l'| : by linarith,
linarith, -- linarith can also find simple numerical contradictions
end
/-
Another incarnation of the excluded middle axiom is the principle of
contraposition: in order to prove P ⇒ Q, it suffices to prove
non Q ⇒ non P.
-/
-- Using a proof by contradiction, let's prove the contraposition principle
-- 0047
example (P Q : Prop) (h : ¬ Q → ¬ P) : P → Q :=
begin
-- sorry
intro hP,
by_contradiction hnQ,
exact h hnQ hP,
-- sorry
end
/-
Again Lean doesn't need to be explain this principle. We can use the
`contrapose` tactic.
-/
example (P Q : Prop) (h : ¬ Q → ¬ P) : P → Q :=
begin
contrapose,
exact h,
end
/-
In the next exercise, we'll use
odd n : ∃ k, n = 2*k + 1
int.odd_iff_not_even {n : ℤ} : odd n ↔ ¬ even n
-/
-- 0048
example (n : ℤ) : even (n^2) ↔ even n :=
begin
-- sorry
split,
{ contrapose,
rw ← int.odd_iff_not_even,
rw ← int.odd_iff_not_even,
rintro ⟨k, rfl⟩,
use 2*k*(k+1),
ring },
{ rintro ⟨k, rfl⟩,
use 2*k^2,
ring },
-- sorry
end
/-
As a last step on our law of the excluded middle tour, let's notice that, especially
in pure logic exercises, it can sometimes be useful to use the
excluded middle axiom in its original form:
classical.em : ∀ P, P ∨ ¬ P
Instead of applying this lemma and then using the `cases` tactic, we
have the shortcut
by_cases h : P,
combining both steps to create two proof branches: one assuming
h : P, and the other assuming h : ¬ P
For instance, let's prove a reformulation of this implication relation,
which is sometimes used as a definition in other logical foundations,
especially those based on truth tables (hence very strongly using
excluded middle from the very beginning).
-/
variables (P Q : Prop)
example : (P → Q) ↔ (¬ P ∨ Q) :=
begin
split,
{ intro h,
by_cases hP : P,
{ right,
exact h hP },
{ left,
exact hP } },
{ intros h hP,
cases h with hnP hQ,
{ exfalso,
exact hnP hP },
{ exact hQ } },
end
-- 0049
example : ¬ (P ∧ Q) ↔ ¬ P ∨ ¬ Q :=
begin
-- sorry
split,
{ intro h,
by_cases hP : P,
{ right,
intro hQ,
exact h ⟨hP, hQ⟩ },
{ left,
exact hP } },
{ rintros h ⟨hP, hQ⟩,
cases h with hnP hnQ,
{ exact hnP hP },
{ exact hnQ hQ } },
-- sorry
end
/-
It is crucial to understand negation of quantifiers.
Let's do it by hand for a little while.
In the first exercise, only the definition of negation is needed.
-/
-- 0050
example (n : ℤ) : ¬ (∃ k, n = 2*k) ↔ ∀ k, n ≠ 2*k :=
begin
-- sorry
split,
{ intros hyp k hk,
exact hyp ⟨k, hk⟩ },
{ rintros hyp ⟨k, rfl⟩,
exact hyp k rfl },
-- sorry
end
/-
Contrary to negation of the existential quantifier, negation of the
universal quantifier requires excluded middle for the first implication.
In order to prove this, we can use either
* a double proof by contradiction
* a contraposition, not_not : (¬ ¬ P) ↔ P) and a proof by contradiction.
-/
def even_fun (f : ℝ → ℝ) := ∀ x, f (-x) = f x
-- 0051
example (f : ℝ → ℝ) : ¬ even_fun f ↔ ∃ x, f (-x) ≠ f x :=
begin
-- sorry
split,
{ contrapose,
intro h,
rw not_not,
intro x,
by_contradiction H,
apply h,
use x,
/- Alternative version
intro h,
by_contradiction H,
apply h,
intro x,
by_contradiction H',
apply H,
use x, -/ },
{ rintros ⟨x, hx⟩ h',
exact hx (h' x) },
-- sorry
end
/-
Of course we can't keep repeating the above proofs, especially the second one.
So we use the `push_neg` tactic.
-/
example : ¬ even_fun (λ x, 2*x) :=
begin
unfold even_fun, -- Here unfolding is important because push_neg won't do it.
push_neg,
use 42,
linarith,
end
-- 0052
example (f : ℝ → ℝ) : ¬ even_fun f ↔ ∃ x, f (-x) ≠ f x :=
begin
-- sorry
unfold even_fun,
push_neg,
-- sorry
end
def bounded_above (f : ℝ → ℝ) := ∃ M, ∀ x, f x ≤ M
example : ¬ bounded_above (λ x, x) :=
begin
unfold bounded_above,
push_neg,
intro M,
use M + 1,
linarith,
end
-- Let's contrapose
-- 0053
example (x : ℝ) : (∀ ε > 0, x ≤ ε) → x ≤ 0 :=
begin
-- sorry
contrapose,
push_neg,
intro h,
use x/2,
split ; linarith,
-- sorry
end
/-
The "contrapose, push_neg" combo is so common that we can abreviate it to
`contrapose!`
Let's use this trick, together with:
eq_or_lt_of_le : a ≤ b → a = b ∨ a < b
-/
-- 0054
example (f : ℝ → ℝ) : (∀ x y, x < y → f x < f y) ↔ (∀ x y, (x ≤ y ↔ f x ≤ f y)) :=
begin
-- sorry
split,
{ intros hf x y,
split,
{ intros hxy,
cases eq_or_lt_of_le hxy with hxy hxy,
{ rw hxy },
{ linarith [hf x y hxy]} },
{ contrapose!,
apply hf } },
{ intros hf x y,
contrapose!,
intro h,
rwa hf, }
-- sorry
end
|
822a1bbc65e06736c3f1a501624c58ea155f2790 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /test/revert_target_deps.lean | 34f94ea5537b90c169926bf7541358bdf11f0d79 | [
"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 | 700 | lean | import tactic.interactive
example (b : bool) (h : b = tt) : true :=
begin
let b₁ : bool := b,
/-
This test shows that `tactic.revert_target_deps`
will revert `b₁` because it occurse in the `have` statement below,
but recursively also reverts `b` (and hence `h`),
because `b` occurs in the body of the `let` statement that introduces `b₁`,
even though `b` doesn't occur directly in the `have` statement below.
-/
have : ∀ b₂ : bool, b₂ ≠ b₁ → b₂ = ff,
{ revert_target_deps,
tactic.interactive.guard_target
``(∀ (b : bool), b = tt → (let b₁ : bool := b in ∀ (b₂ : bool), b₂ ≠ b₁ → b₂ = ff)),
exact dec_trivial },
trivial
end
|
87d37dc6022bd50fc2f8d48e15f5eb6403da4371 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/category_theory/adjunction/fully_faithful.lean | 5491d3ba61a0f5bf41da31994eec10d4ce5e7e17 | [
"Apache-2.0"
] | permissive | Lix0120/mathlib | 0020745240315ed0e517cbf32e738d8f9811dd80 | e14c37827456fc6707f31b4d1d16f1f3a3205e91 | refs/heads/master | 1,673,102,855,024 | 1,604,151,044,000 | 1,604,151,044,000 | 308,930,245 | 0 | 0 | Apache-2.0 | 1,604,164,710,000 | 1,604,163,547,000 | null | UTF-8 | Lean | false | false | 4,770 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.adjunction.basic
import category_theory.conj
import category_theory.yoneda
open category_theory
namespace category_theory
universes v₁ v₂ u₁ u₂
open category
open opposite
variables {C : Type u₁} [category.{v₁} C]
variables {D : Type u₂} [category.{v₂} D]
variables {L : C ⥤ D} {R : D ⥤ C} (h : L ⊣ R)
/--
If the left adjoint is fully faithful, then the unit is an isomorphism.
See
* Lemma 4.5.13 from [Riehl][riehl2017]
* https://math.stackexchange.com/a/2727177
* https://stacks.math.columbia.edu/tag/07RB (we only prove the forward direction!)
-/
instance unit_is_iso_of_L_fully_faithful [full L] [faithful L] : is_iso (adjunction.unit h) :=
@nat_iso.is_iso_of_is_iso_app _ _ _ _ _ _ (adjunction.unit h) $ λ X,
@yoneda.is_iso _ _ _ _ ((adjunction.unit h).app X)
{ inv := { app := λ Y f, L.preimage ((h.hom_equiv (unop Y) (L.obj X)).symm f) },
inv_hom_id' :=
begin
ext, dsimp,
simp only [adjunction.hom_equiv_counit, preimage_comp, preimage_map, category.assoc],
rw ←h.unit_naturality,
simp,
end,
hom_inv_id' :=
begin
ext, dsimp,
apply L.map_injective,
simp,
end }.
/--
If the right adjoint is fully faithful, then the counit is an isomorphism.
See https://stacks.math.columbia.edu/tag/07RB (we only prove the forward direction!)
-/
instance counit_is_iso_of_R_fully_faithful [full R] [faithful R] : is_iso (adjunction.counit h) :=
@nat_iso.is_iso_of_is_iso_app _ _ _ _ _ _ (adjunction.counit h) $ λ X,
@is_iso_of_op _ _ _ _ _ $
@coyoneda.is_iso _ _ _ _ ((adjunction.counit h).app X).op
{ inv := { app := λ Y f, R.preimage ((h.hom_equiv (R.obj X) Y) f) },
inv_hom_id' :=
begin
ext, dsimp,
simp only [adjunction.hom_equiv_unit, preimage_comp, preimage_map],
rw ←h.counit_naturality,
simp,
end,
hom_inv_id' :=
begin
ext, dsimp,
apply R.map_injective,
simp,
end }
-- TODO also prove the converses?
-- def L_full_of_unit_is_iso [is_iso (adjunction.unit h)] : full L := sorry
-- def L_faithful_of_unit_is_iso [is_iso (adjunction.unit h)] : faithful L := sorry
-- def R_full_of_counit_is_iso [is_iso (adjunction.counit h)] : full R := sorry
-- def R_faithful_of_counit_is_iso [is_iso (adjunction.counit h)] : faithful R := sorry
-- TODO also do the statements from Riehl 4.5.13 for full and faithful separately?
universes v₃ v₄ u₃ u₄
variables {C' : Type u₃} [category.{v₃} C']
variables {D' : Type u₄} [category.{v₄} D']
-- TODO: This needs some lemmas describing the produced adjunction, probably in terms of `adj`,
-- `iC` and `iD`.
/--
If `C` is a full subcategory of `C'` and `D` is a full subcategory of `D'`, then we can restrict
an adjunction `L' ⊣ R'` where `L' : C' ⥤ D'` and `R' : D' ⥤ C'` to `C` and `D`.
The construction here is slightly more general, in that `C` is required only to have a full and
faithful "inclusion" functor `iC : C ⥤ C'` (and similarly `iD : D ⥤ D'`) which commute (up to
natural isomorphism) with the proposed restrictions.
-/
def adjunction.restrict_fully_faithful (iC : C ⥤ C') (iD : D ⥤ D') {L' : C' ⥤ D'} {R' : D' ⥤ C'}
(adj : L' ⊣ R') {L : C ⥤ D} {R : D ⥤ C} (comm1 : iC ⋙ L' ≅ L ⋙ iD) (comm2 : iD ⋙ R' ≅ R ⋙ iC)
[full iC] [faithful iC] [full iD] [faithful iD] :
L ⊣ R :=
adjunction.mk_of_hom_equiv
{ hom_equiv := λ X Y,
calc (L.obj X ⟶ Y) ≃ (iD.obj (L.obj X) ⟶ iD.obj Y) : equiv_of_fully_faithful iD
... ≃ (L'.obj (iC.obj X) ⟶ iD.obj Y) : iso.hom_congr (comm1.symm.app X) (iso.refl _)
... ≃ (iC.obj X ⟶ R'.obj (iD.obj Y)) : adj.hom_equiv _ _
... ≃ (iC.obj X ⟶ iC.obj (R.obj Y)) : iso.hom_congr (iso.refl _) (comm2.app Y)
... ≃ (X ⟶ R.obj Y) : (equiv_of_fully_faithful iC).symm,
hom_equiv_naturality_left_symm' := λ X' X Y f g,
begin
apply iD.map_injective,
simp only [functor.image_preimage, adjunction.hom_equiv_counit, assoc, id_comp, comp_id,
functor.map_comp, iso.refl_hom, iso.refl_symm, iso.refl_inv, iso.symm_mk,
equiv_of_fully_faithful_symm_apply, iso.hom_congr_apply, iso.hom_congr_symm,
equiv_of_fully_faithful_apply, equiv.symm_symm, equiv.symm_trans_apply, iso.symm_hom],
erw [comm1.inv.naturality_assoc f],
refl,
end,
hom_equiv_naturality_right' := λ X Y' Y f g,
begin
apply iC.map_injective,
dsimp [equiv.trans, iso.hom_congr_apply, iso.hom_congr_symm],
simp only [adjunction.hom_equiv_unit, functor.image_preimage, assoc, id_comp, comp_id,
functor.map_comp],
erw comm2.hom.naturality g,
refl,
end }
end category_theory
|
89487d13bd929df7b6b955904db9117122381e4a | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/structured_arrow.lean | 5158fe858211c891c0eed19e3fae7d0043cffa1f | [
"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 | 13,254 | lean | /-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Adam Topaz, Scott Morrison
-/
import category_theory.punit
import category_theory.comma
import category_theory.limits.shapes.terminal
/-!
# The category of "structured arrows"
For `T : C ⥤ D`, a `T`-structured arrow with source `S : D`
is just a morphism `S ⟶ T.obj Y`, for some `Y : C`.
These form a category with morphisms `g : Y ⟶ Y'` making the obvious diagram commute.
We prove that `𝟙 (T.obj Y)` is the initial object in `T`-structured objects with source `T.obj Y`.
-/
namespace category_theory
-- morphism levels before object levels. See note [category_theory universes].
universes v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄
variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D]
/--
The category of `T`-structured arrows with domain `S : D` (here `T : C ⥤ D`),
has as its objects `D`-morphisms of the form `S ⟶ T Y`, for some `Y : C`,
and morphisms `C`-morphisms `Y ⟶ Y'` making the obvious triangle commute.
-/
@[derive category, nolint has_inhabited_instance]
def structured_arrow (S : D) (T : C ⥤ D) := comma (functor.from_punit S) T
namespace structured_arrow
/-- The obvious projection functor from structured arrows. -/
@[simps]
def proj (S : D) (T : C ⥤ D) : structured_arrow S T ⥤ C := comma.snd _ _
variables {S S' S'' : D} {Y Y' : C} {T : C ⥤ D}
/-- Construct a structured arrow from a morphism. -/
def mk (f : S ⟶ T.obj Y) : structured_arrow S T := ⟨⟨⟩, Y, f⟩
@[simp] lemma mk_left (f : S ⟶ T.obj Y) : (mk f).left = punit.star := rfl
@[simp] lemma mk_right (f : S ⟶ T.obj Y) : (mk f).right = Y := rfl
@[simp] lemma mk_hom_eq_self (f : S ⟶ T.obj Y) : (mk f).hom = f := rfl
@[simp, reassoc] lemma w {A B : structured_arrow S T} (f : A ⟶ B) : A.hom ≫ T.map f.right = B.hom :=
by { have := f.w; tidy }
lemma eq_mk (f : structured_arrow S T) : f = mk f.hom :=
by { cases f, congr, ext, }
/--
To construct a morphism of structured arrows,
we need a morphism of the objects underlying the target,
and to check that the triangle commutes.
-/
@[simps]
def hom_mk {f f' : structured_arrow S T} (g : f.right ⟶ f'.right) (w : f.hom ≫ T.map g = f'.hom) :
f ⟶ f' :=
{ left := eq_to_hom (by ext),
right := g,
w' := by { dsimp, simpa using w.symm, }, }
/--
Given a structured arrow `X ⟶ F(U)`, and an arrow `U ⟶ Y`, we can construct a morphism of
structured arrow given by `(X ⟶ F(U)) ⟶ (X ⟶ F(U) ⟶ F(Y))`.
-/
def hom_mk' {F : C ⥤ D} {X : D} {Y : C}
(U : structured_arrow X F) (f : U.right ⟶ Y) :
U ⟶ mk (U.hom ≫ F.map f) := { right := f }
/--
To construct an isomorphism of structured arrows,
we need an isomorphism of the objects underlying the target,
and to check that the triangle commutes.
-/
@[simps]
def iso_mk {f f' : structured_arrow S T} (g : f.right ≅ f'.right)
(w : f.hom ≫ T.map g.hom = f'.hom) : f ≅ f' :=
comma.iso_mk (eq_to_iso (by ext)) g (by simpa using w.symm)
/--
A morphism between source objects `S ⟶ S'`
contravariantly induces a functor between structured arrows,
`structured_arrow S' T ⥤ structured_arrow S T`.
Ideally this would be described as a 2-functor from `D`
(promoted to a 2-category with equations as 2-morphisms)
to `Cat`.
-/
@[simps]
def map (f : S ⟶ S') : structured_arrow S' T ⥤ structured_arrow S T :=
comma.map_left _ ((functor.const _).map f)
@[simp] lemma map_mk {f : S' ⟶ T.obj Y} (g : S ⟶ S') :
(map g).obj (mk f) = mk (g ≫ f) := rfl
@[simp] lemma map_id {f : structured_arrow S T} : (map (𝟙 S)).obj f = f :=
by { rw eq_mk f, simp, }
@[simp] lemma map_comp {f : S ⟶ S'} {f' : S' ⟶ S''} {h : structured_arrow S'' T} :
(map (f ≫ f')).obj h = (map f).obj ((map f').obj h) :=
by { rw eq_mk h, simp, }
instance proj_reflects_iso : reflects_isomorphisms (proj S T) :=
{ reflects := λ Y Z f t, by exactI
⟨⟨structured_arrow.hom_mk (inv ((proj S T).map f)) (by simp), by tidy⟩⟩ }
open category_theory.limits
/-- The identity structured arrow is initial. -/
def mk_id_initial [full T] [faithful T] : is_initial (mk (𝟙 (T.obj Y))) :=
{ desc := λ c, hom_mk (T.preimage c.X.hom) (by { dsimp, simp, }),
uniq' := λ c m _, begin
ext,
apply T.map_injective,
simpa only [hom_mk_right, T.image_preimage, ←w m] using (category.id_comp _).symm,
end }
variables {A : Type u₃} [category.{v₃} A] {B : Type u₄} [category.{v₄} B]
/-- The functor `(S, F ⋙ G) ⥤ (S, G)`. -/
@[simps]
def pre (S : D) (F : B ⥤ C) (G : C ⥤ D) : structured_arrow S (F ⋙ G) ⥤ structured_arrow S G :=
comma.pre_right _ F G
/-- The functor `(S, F) ⥤ (G(S), F ⋙ G)`. -/
@[simps] def post (S : C) (F : B ⥤ C) (G : C ⥤ D) :
structured_arrow S F ⥤ structured_arrow (G.obj S) (F ⋙ G) :=
{ obj := λ X, { right := X.right, hom := G.map X.hom },
map := λ X Y f, { right := f.right, w' :=
by { simp [functor.comp_map, ←G.map_comp, ← f.w] } } }
end structured_arrow
/--
The category of `S`-costructured arrows with target `T : D` (here `S : C ⥤ D`),
has as its objects `D`-morphisms of the form `S Y ⟶ T`, for some `Y : C`,
and morphisms `C`-morphisms `Y ⟶ Y'` making the obvious triangle commute.
-/
@[derive category, nolint has_inhabited_instance]
def costructured_arrow (S : C ⥤ D) (T : D) := comma S (functor.from_punit T)
namespace costructured_arrow
/-- The obvious projection functor from costructured arrows. -/
@[simps]
def proj (S : C ⥤ D) (T : D) : costructured_arrow S T ⥤ C := comma.fst _ _
variables {T T' T'' : D} {Y Y' : C} {S : C ⥤ D}
/-- Construct a costructured arrow from a morphism. -/
def mk (f : S.obj Y ⟶ T) : costructured_arrow S T := ⟨Y, ⟨⟩, f⟩
@[simp] lemma mk_left (f : S.obj Y ⟶ T) : (mk f).left = Y := rfl
@[simp] lemma mk_right (f : S.obj Y ⟶ T) : (mk f).right = punit.star := rfl
@[simp] lemma mk_hom_eq_self (f : S.obj Y ⟶ T) : (mk f).hom = f := rfl
@[simp, reassoc] lemma w {A B : costructured_arrow S T} (f : A ⟶ B) :
S.map f.left ≫ B.hom = A.hom :=
by tidy
lemma eq_mk (f : costructured_arrow S T) : f = mk f.hom :=
by { cases f, congr, ext, }
/--
To construct a morphism of costructured arrows,
we need a morphism of the objects underlying the source,
and to check that the triangle commutes.
-/
@[simps]
def hom_mk {f f' : costructured_arrow S T} (g : f.left ⟶ f'.left) (w : S.map g ≫ f'.hom = f.hom) :
f ⟶ f' :=
{ left := g,
right := eq_to_hom (by ext),
w' := by simpa using w, }
/--
To construct an isomorphism of costructured arrows,
we need an isomorphism of the objects underlying the source,
and to check that the triangle commutes.
-/
@[simps]
def iso_mk {f f' : costructured_arrow S T} (g : f.left ≅ f'.left)
(w : S.map g.hom ≫ f'.hom = f.hom) : f ≅ f' :=
comma.iso_mk g (eq_to_iso (by ext)) (by simpa using w)
/--
A morphism between target objects `T ⟶ T'`
covariantly induces a functor between costructured arrows,
`costructured_arrow S T ⥤ costructured_arrow S T'`.
Ideally this would be described as a 2-functor from `D`
(promoted to a 2-category with equations as 2-morphisms)
to `Cat`.
-/
@[simps]
def map (f : T ⟶ T') : costructured_arrow S T ⥤ costructured_arrow S T' :=
comma.map_right _ ((functor.const _).map f)
@[simp] lemma map_mk {f : S.obj Y ⟶ T} (g : T ⟶ T') :
(map g).obj (mk f) = mk (f ≫ g) := rfl
@[simp] lemma map_id {f : costructured_arrow S T} : (map (𝟙 T)).obj f = f :=
by { rw eq_mk f, simp, }
@[simp] lemma map_comp {f : T ⟶ T'} {f' : T' ⟶ T''} {h : costructured_arrow S T} :
(map (f ≫ f')).obj h = (map f').obj ((map f).obj h) :=
by { rw eq_mk h, simp, }
instance proj_reflects_iso : reflects_isomorphisms (proj S T) :=
{ reflects := λ Y Z f t, by exactI
⟨⟨costructured_arrow.hom_mk (inv ((proj S T).map f)) (by simp), by tidy⟩⟩ }
open category_theory.limits
/-- The identity costructured arrow is terminal. -/
def mk_id_terminal [full S] [faithful S] : is_terminal (mk (𝟙 (S.obj Y))) :=
{ lift := λ c, hom_mk (S.preimage c.X.hom) (by { dsimp, simp, }),
uniq' := begin
rintros c m -,
ext,
apply S.map_injective,
simpa only [hom_mk_left, S.image_preimage, ←w m] using (category.comp_id _).symm,
end }
variables {A : Type u₃} [category.{v₃} A] {B : Type u₄} [category.{v₄} B]
/-- The functor `(F ⋙ G, S) ⥤ (G, S)`. -/
@[simps]
def pre (F : B ⥤ C) (G : C ⥤ D) (S : D) : costructured_arrow (F ⋙ G) S ⥤ costructured_arrow G S :=
comma.pre_left F G _
/-- The functor `(F, S) ⥤ (F ⋙ G, G(S))`. -/
@[simps] def post (F : B ⥤ C) (G : C ⥤ D) (S : C) :
costructured_arrow F S ⥤ costructured_arrow (F ⋙ G) (G.obj S) :=
{ obj := λ X, { left := X.left, hom := G.map X.hom },
map := λ X Y f, { left := f.left, w' :=
by { simp [functor.comp_map, ←G.map_comp, ← f.w] } } }
end costructured_arrow
open opposite
namespace structured_arrow
/--
For a functor `F : C ⥤ D` and an object `d : D`, we obtain a contravariant functor from the
category of structured arrows `d ⟶ F.obj c` to the category of costructured arrows
`F.op.obj c ⟶ (op d)`.
-/
@[simps]
def to_costructured_arrow (F : C ⥤ D) (d : D) :
(structured_arrow d F)ᵒᵖ ⥤ costructured_arrow F.op (op d) :=
{ obj := λ X, @costructured_arrow.mk _ _ _ _ _ (op X.unop.right) F.op X.unop.hom.op,
map := λ X Y f, costructured_arrow.hom_mk (f.unop.right.op)
begin
dsimp,
rw [← op_comp, ← f.unop.w, functor.const.obj_map],
erw category.id_comp,
end }
/--
For a functor `F : C ⥤ D` and an object `d : D`, we obtain a contravariant functor from the
category of structured arrows `op d ⟶ F.op.obj c` to the category of costructured arrows
`F.obj c ⟶ d`.
-/
@[simps]
def to_costructured_arrow' (F : C ⥤ D) (d : D) :
(structured_arrow (op d) F.op)ᵒᵖ ⥤ costructured_arrow F d :=
{ obj := λ X, @costructured_arrow.mk _ _ _ _ _ (unop X.unop.right) F X.unop.hom.unop,
map := λ X Y f, costructured_arrow.hom_mk f.unop.right.unop
begin
dsimp,
rw [← quiver.hom.unop_op (F.map (quiver.hom.unop f.unop.right)), ← unop_comp, ← F.op_map,
← f.unop.w, functor.const.obj_map],
erw category.id_comp,
end }
end structured_arrow
namespace costructured_arrow
/--
For a functor `F : C ⥤ D` and an object `d : D`, we obtain a contravariant functor from the
category of costructured arrows `F.obj c ⟶ d` to the category of structured arrows
`op d ⟶ F.op.obj c`.
-/
@[simps]
def to_structured_arrow (F : C ⥤ D) (d : D) :
(costructured_arrow F d)ᵒᵖ ⥤ structured_arrow (op d) F.op :=
{ obj := λ X, @structured_arrow.mk _ _ _ _ _ (op X.unop.left) F.op X.unop.hom.op,
map := λ X Y f, structured_arrow.hom_mk f.unop.left.op
begin
dsimp,
rw [← op_comp, f.unop.w, functor.const.obj_map],
erw category.comp_id,
end }
/--
For a functor `F : C ⥤ D` and an object `d : D`, we obtain a contravariant functor from the
category of costructured arrows `F.op.obj c ⟶ op d` to the category of structured arrows
`d ⟶ F.obj c`.
-/
@[simps]
def to_structured_arrow' (F : C ⥤ D) (d : D) :
(costructured_arrow F.op (op d))ᵒᵖ ⥤ structured_arrow d F :=
{ obj := λ X, @structured_arrow.mk _ _ _ _ _ (unop X.unop.left) F X.unop.hom.unop,
map := λ X Y f, structured_arrow.hom_mk (f.unop.left.unop)
begin
dsimp,
rw [← quiver.hom.unop_op (F.map f.unop.left.unop), ← unop_comp, ← F.op_map,
f.unop.w, functor.const.obj_map],
erw category.comp_id,
end }
end costructured_arrow
/--
For a functor `F : C ⥤ D` and an object `d : D`, the category of structured arrows `d ⟶ F.obj c`
is contravariantly equivalent to the category of costructured arrows `F.op.obj c ⟶ op d`.
-/
def structured_arrow_op_equivalence (F : C ⥤ D) (d : D) :
(structured_arrow d F)ᵒᵖ ≌ costructured_arrow F.op (op d) :=
equivalence.mk (structured_arrow.to_costructured_arrow F d)
(costructured_arrow.to_structured_arrow' F d).right_op
(nat_iso.of_components (λ X, (@structured_arrow.iso_mk _ _ _ _ _ _
(structured_arrow.mk (unop X).hom) (unop X) (iso.refl _) (by tidy)).op)
(λ X Y f, quiver.hom.unop_inj $ begin ext, dsimp, simp end))
(nat_iso.of_components (λ X, @costructured_arrow.iso_mk _ _ _ _ _ _
(costructured_arrow.mk X.hom) X (iso.refl _) (by tidy))
(λ X Y f, begin ext, dsimp, simp end))
/--
For a functor `F : C ⥤ D` and an object `d : D`, the category of costructured arrows
`F.obj c ⟶ d` is contravariantly equivalent to the category of structured arrows
`op d ⟶ F.op.obj c`.
-/
def costructured_arrow_op_equivalence (F : C ⥤ D) (d : D) :
(costructured_arrow F d)ᵒᵖ ≌ structured_arrow (op d) F.op :=
equivalence.mk (costructured_arrow.to_structured_arrow F d)
(structured_arrow.to_costructured_arrow' F d).right_op
(nat_iso.of_components (λ X, (@costructured_arrow.iso_mk _ _ _ _ _ _
(costructured_arrow.mk (unop X).hom) (unop X) (iso.refl _) (by tidy)).op)
(λ X Y f, quiver.hom.unop_inj $ begin ext, dsimp, simp end))
(nat_iso.of_components (λ X, @structured_arrow.iso_mk _ _ _ _ _ _
(structured_arrow.mk X.hom) X (iso.refl _) (by tidy))
(λ X Y f, begin ext, dsimp, simp end))
end category_theory
|
386f3703688181a092f8724addf20a7e108b74c3 | 90edd5cdcf93124fe15627f7304069fdce3442dd | /stage0/src/Lean/PrettyPrinter/Parenthesizer.lean | 6d6e76cf78a744a124e20185b488b5c2a3cea041 | [
"Apache-2.0"
] | permissive | JLimperg/lean4-aesop | 8a9d9cd3ee484a8e67fda2dd9822d76708098712 | 5c4b9a3e05c32f69a4357c3047c274f4b94f9c71 | refs/heads/master | 1,689,415,944,104 | 1,627,383,284,000 | 1,627,383,284,000 | 377,536,770 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 29,875 | lean | /-
Copyright (c) 2020 Sebastian Ullrich. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
-/
/-!
The parenthesizer inserts parentheses into a `Syntax` object where syntactically necessary, usually as an intermediary
step between the delaborator and the formatter. While the delaborator outputs structurally well-formed syntax trees that
can be re-elaborated without post-processing, this tree structure is lost in the formatter and thus needs to be
preserved by proper insertion of parentheses.
# The abstract problem & solution
The Lean 4 grammar is unstructured and extensible with arbitrary new parsers, so in general it is undecidable whether
parentheses are necessary or even allowed at any point in the syntax tree. Parentheses for different categories, e.g.
terms and levels, might not even have the same structure. In this module, we focus on the correct parenthesization of
parsers defined via `Lean.Parser.prattParser`, which includes both aforementioned built-in categories. Custom
parenthesizers can be added for new node kinds, but the data collected in the implementation below might not be
appropriate for other parenthesization strategies.
Usages of a parser defined via `prattParser` in general have the form `p prec`, where `prec` is the minimum precedence
or binding power. Recall that a Pratt parser greedily runs a leading parser with precedence at least `prec` (otherwise
it fails) followed by zero or more trailing parsers with precedence at least `prec`; the precedence of a parser is
encoded in the call to `leadingNode/trailingNode`, respectively. Thus we should parenthesize a syntax node `stx`
supposedly produced by `p prec` if
1. the leading/any trailing parser involved in `stx` has precedence < `prec` (because without parentheses, `p prec`
would not produce all of `stx`), or
2. the trailing parser parsing the input to *the right of* `stx`, if any, has precedence >= `prec` (because without
parentheses, `p prec` would have parsed it as well and made it a part of `stx`). We also check that the two parsers
are from the same syntax category.
Note that in case 2, it is also sufficient to parenthesize a *parent* node as long as the offending parser is still to
the right of that node. For example, imagine the tree structure of `(f $ fun x => x) y` without parentheses. We need to
insert *some* parentheses between `x` and `y` since the lambda body is parsed with precedence 0, while the identifier
parser for `y` has precedence `maxPrec`. But we need to parenthesize the `$` node anyway since the precedence of its
RHS (0) again is smaller than that of `y`. So it's better to only parenthesize the outer node than ending up with
`(f $ (fun x => x)) y`.
# Implementation
We transform the syntax tree and collect the necessary precedence information for that in a single traversal. The
traversal is right-to-left to cover case 2. More specifically, for every Pratt parser call, we store as monadic state
the precedence of the left-most trailing parser and the minimum precedence of any parser (`contPrec`/`minPrec`) in this
call, if any, and the precedence of the nested trailing Pratt parser call (`trailPrec`), if any. If `stP` is the state
resulting from the traversal of a Pratt parser call `p prec`, and `st` is the state of the surrounding call, we
parenthesize if `prec > stP.minPrec` (case 1) or if `stP.trailPrec <= st.contPrec` (case 2).
The traversal can be customized for each `[*Parser]` parser declaration `c` (more specifically, each `SyntaxNodeKind`
`c`) using the `[parenthesizer c]` attribute. Otherwise, a default parenthesizer will be synthesized from the used
parser combinators by recursively replacing them with declarations tagged as `[combinatorParenthesizer]` for the
respective combinator. If a called function does not have a registered combinator parenthesizer and is not reducible,
the synthesizer fails. This happens mostly at the `Parser.mk` decl, which is irreducible, when some parser primitive has
not been handled yet.
The traversal over the `Syntax` object is complicated by the fact that a parser does not produce exactly one syntax
node, but an arbitrary (but constant, for each parser) amount that it pushes on top of the parser stack. This amount can
even be zero for parsers such as `checkWsBefore`. Thus we cannot simply pass and return a `Syntax` object to and from
`visit`. Instead, we use a `Syntax.Traverser` that allows arbitrary movement and modification inside the syntax tree.
Our traversal invariant is that a parser interpreter should stop at the syntax object to the *left* of all syntax
objects its parser produced, except when it is already at the left-most child. This special case is not an issue in
practice since if there is another parser to the left that produced zero nodes in this case, it should always do so, so
there is no danger of the left-most child being processed multiple times.
Ultimately, most parenthesizers are implemented via three primitives that do all the actual syntax traversal:
`maybeParenthesize mkParen prec x` runs `x` and afterwards transforms it with `mkParen` if the above
condition for `p prec` is fulfilled. `visitToken` advances to the preceding sibling and is used on atoms. `visitArgs x`
executes `x` on the last child of the current node and then advances to the preceding sibling (of the original current
node).
-/
import Lean.CoreM
import Lean.KeyedDeclsAttribute
import Lean.Parser.Extension
import Lean.ParserCompiler.Attribute
import Lean.PrettyPrinter.Basic
namespace Lean
namespace PrettyPrinter
namespace Parenthesizer
structure Context where
-- We need to store this `categoryParser` argument to deal with the implicit Pratt parser call in `trailingNode.parenthesizer`.
cat : Name := Name.anonymous
structure State where
stxTrav : Syntax.Traverser
--- precedence and category of the current left-most trailing parser, if any; see module doc for details
contPrec : Option Nat := none
contCat : Name := Name.anonymous
-- current minimum precedence in this Pratt parser call, if any; see module doc for details
minPrec : Option Nat := none
-- precedence and category of the trailing Pratt parser call if any; see module doc for details
trailPrec : Option Nat := none
trailCat : Name := Name.anonymous
-- true iff we have already visited a token on this parser level; used for detecting trailing parsers
visitedToken : Bool := false
end Parenthesizer
abbrev ParenthesizerM := ReaderT Parenthesizer.Context $ StateRefT Parenthesizer.State CoreM
abbrev Parenthesizer := ParenthesizerM Unit
@[inline] def ParenthesizerM.orelse {α} (p₁ p₂ : ParenthesizerM α) : ParenthesizerM α := do
let s ← get
catchInternalId backtrackExceptionId
p₁
(fun _ => do set s; p₂)
instance {α} : OrElse (ParenthesizerM α) := ⟨ParenthesizerM.orelse⟩
unsafe def mkParenthesizerAttribute : IO (KeyedDeclsAttribute Parenthesizer) :=
KeyedDeclsAttribute.init {
builtinName := `builtinParenthesizer,
name := `parenthesizer,
descr := "Register a parenthesizer for a parser.
[parenthesizer k] registers a declaration of type `Lean.PrettyPrinter.Parenthesizer` for the `SyntaxNodeKind` `k`.",
valueTypeName := `Lean.PrettyPrinter.Parenthesizer,
evalKey := fun builtin stx => do
let env ← getEnv
let id ← Attribute.Builtin.getId stx
-- `isValidSyntaxNodeKind` is updated only in the next stage for new `[builtin*Parser]`s, but we try to
-- synthesize a parenthesizer for it immediately, so we just check for a declaration in this case
if (builtin && (env.find? id).isSome) || Parser.isValidSyntaxNodeKind env id then pure id
else throwError "invalid [parenthesizer] argument, unknown syntax kind '{id}'"
} `Lean.PrettyPrinter.parenthesizerAttribute
@[builtinInit mkParenthesizerAttribute] constant parenthesizerAttribute : KeyedDeclsAttribute Parenthesizer
abbrev CategoryParenthesizer := forall (prec : Nat), Parenthesizer
unsafe def mkCategoryParenthesizerAttribute : IO (KeyedDeclsAttribute CategoryParenthesizer) :=
KeyedDeclsAttribute.init {
builtinName := `builtinCategoryParenthesizer,
name := `categoryParenthesizer,
descr := "Register a parenthesizer for a syntax category.
[categoryParenthesizer cat] registers a declaration of type `Lean.PrettyPrinter.CategoryParenthesizer` for the category `cat`,
which is used when parenthesizing calls of `categoryParser cat prec`. Implementations should call `maybeParenthesize`
with the precedence and `cat`. If no category parenthesizer is registered, the category will never be parenthesized,
but still be traversed for parenthesizing nested categories.",
valueTypeName := `Lean.PrettyPrinter.CategoryParenthesizer,
evalKey := fun _ stx => do
let env ← getEnv
let id ← Attribute.Builtin.getId stx
if Parser.isParserCategory env id then pure id
else throwError "invalid [categoryParenthesizer] argument, unknown parser category '{toString id}'"
} `Lean.PrettyPrinter.categoryParenthesizerAttribute
@[builtinInit mkCategoryParenthesizerAttribute] constant categoryParenthesizerAttribute : KeyedDeclsAttribute CategoryParenthesizer
unsafe def mkCombinatorParenthesizerAttribute : IO ParserCompiler.CombinatorAttribute :=
ParserCompiler.registerCombinatorAttribute
`combinatorParenthesizer
"Register a parenthesizer for a parser combinator.
[combinatorParenthesizer c] registers a declaration of type `Lean.PrettyPrinter.Parenthesizer` for the `Parser` declaration `c`.
Note that, unlike with [parenthesizer], this is not a node kind since combinators usually do not introduce their own node kinds.
The tagged declaration may optionally accept parameters corresponding to (a prefix of) those of `c`, where `Parser` is replaced
with `Parenthesizer` in the parameter types."
@[builtinInit mkCombinatorParenthesizerAttribute] constant combinatorParenthesizerAttribute : ParserCompiler.CombinatorAttribute
namespace Parenthesizer
open Lean.Core
open Std.Format
def throwBacktrack {α} : ParenthesizerM α :=
throw $ Exception.internal backtrackExceptionId
instance : Syntax.MonadTraverser ParenthesizerM := ⟨{
get := State.stxTrav <$> get,
set := fun t => modify (fun st => { st with stxTrav := t }),
modifyGet := fun f => modifyGet (fun st => let (a, t) := f st.stxTrav; (a, { st with stxTrav := t }))
}⟩
open Syntax.MonadTraverser
def addPrecCheck (prec : Nat) : ParenthesizerM Unit :=
modify fun st => { st with contPrec := Nat.min (st.contPrec.getD prec) prec, minPrec := Nat.min (st.minPrec.getD prec) prec }
/-- Execute `x` at the right-most child of the current node, if any, then advance to the left. -/
def visitArgs (x : ParenthesizerM Unit) : ParenthesizerM Unit := do
let stx ← getCur
if stx.getArgs.size > 0 then
goDown (stx.getArgs.size - 1) *> x <* goUp
goLeft
-- Macro scopes in the parenthesizer output are ultimately ignored by the pretty printer,
-- so give a trivial implementation.
instance : MonadQuotation ParenthesizerM := {
getCurrMacroScope := pure arbitrary,
getMainModule := pure arbitrary,
withFreshMacroScope := fun x => x,
}
/--
Run `x` and parenthesize the result using `mkParen` if necessary.
If `canJuxtapose` is false, we assume the category does not have a token-less juxtaposition syntax a la function application and deactivate rule 2. -/
def maybeParenthesize (cat : Name) (canJuxtapose : Bool) (mkParen : Syntax → Syntax) (prec : Nat) (x : ParenthesizerM Unit) : ParenthesizerM Unit := do
let stx ← getCur
let idx ← getIdx
let st ← get
-- reset precs for the recursive call
set { stxTrav := st.stxTrav : State }
trace[PrettyPrinter.parenthesize] "parenthesizing (cont := {(st.contPrec, st.contCat)}){indentD (fmt stx)}"
x
let { minPrec := some minPrec, trailPrec := trailPrec, trailCat := trailCat, .. } ← get
| trace[PrettyPrinter.parenthesize] "visited a syntax tree without precedences?!{line ++ fmt stx}"
trace[PrettyPrinter.parenthesize] (m!"...precedences are {prec} >? {minPrec}" ++ if canJuxtapose then m!", {(trailPrec, trailCat)} <=? {(st.contPrec, st.contCat)}" else "")
-- Should we parenthesize?
if (prec > minPrec || canJuxtapose && match trailPrec, st.contPrec with | some trailPrec, some contPrec => trailCat == st.contCat && trailPrec <= contPrec | _, _ => false) then
-- The recursive `visit` call, by the invariant, has moved to the preceding node. In order to parenthesize
-- the original node, we must first move to the right, except if we already were at the left-most child in the first
-- place.
if idx > 0 then goRight
let mut stx ← getCur
-- Move leading/trailing whitespace of `stx` outside of parentheses
if let SourceInfo.original _ pos trail endPos := stx.getHeadInfo then
stx := stx.setHeadInfo (SourceInfo.original "".toSubstring pos trail endPos)
if let SourceInfo.original lead pos _ endPos := stx.getTailInfo then
stx := stx.setTailInfo (SourceInfo.original lead pos "".toSubstring endPos)
let mut stx' := mkParen stx
if let SourceInfo.original lead pos _ endPos := stx.getHeadInfo then
stx' := stx'.setHeadInfo (SourceInfo.original lead pos "".toSubstring endPos)
if let SourceInfo.original _ pos trail endPos := stx.getTailInfo then
stx' := stx'.setTailInfo (SourceInfo.original "".toSubstring pos trail endPos)
trace[PrettyPrinter.parenthesize] "parenthesized: {stx'.formatStx none}"
setCur stx'
goLeft
-- after parenthesization, there is no more trailing parser
modify (fun st => { st with contPrec := Parser.maxPrec, contCat := cat, trailPrec := none })
let { trailPrec := trailPrec, .. } ← get
-- If we already had a token at this level, keep the trailing parser. Otherwise, use the minimum of
-- `prec` and `trailPrec`.
if st.visitedToken then
modify fun stP => { stP with trailPrec := st.trailPrec, trailCat := st.trailCat }
else
let trailPrec := match trailPrec with
| some trailPrec => Nat.min trailPrec prec
| _ => prec
modify fun stP => { stP with trailPrec := trailPrec, trailCat := cat }
modify fun stP => { stP with minPrec := st.minPrec }
/-- Adjust state and advance. -/
def visitToken : Parenthesizer := do
modify fun st => { st with contPrec := none, contCat := Name.anonymous, visitedToken := true }
goLeft
@[combinatorParenthesizer Lean.Parser.orelse] def orelse.parenthesizer (p1 p2 : Parenthesizer) : Parenthesizer := do
let st ← get
-- HACK: We have no (immediate) information on which side of the orelse could have produced the current node, so try
-- them in turn. Uses the syntax traverser non-linearly!
p1 <|> p2
-- `mkAntiquot` is quite complex, so we'd rather have its parenthesizer synthesized below the actual parser definition.
-- Note that there is a mutual recursion
-- `categoryParser -> mkAntiquot -> termParser -> categoryParser`, so we need to introduce an indirection somewhere
-- anyway.
@[extern 8 "lean_mk_antiquot_parenthesizer"]
constant mkAntiquot.parenthesizer' (name : String) (kind : Option SyntaxNodeKind) (anonymous := true) : Parenthesizer
@[inline] def liftCoreM {α} (x : CoreM α) : ParenthesizerM α :=
liftM x
-- break up big mutual recursion
@[extern "lean_pretty_printer_parenthesizer_interpret_parser_descr"]
constant interpretParserDescr' : ParserDescr → CoreM Parenthesizer
unsafe def parenthesizerForKindUnsafe (k : SyntaxNodeKind) : Parenthesizer := do
if k == `missing then
pure ()
else
let p ← runForNodeKind parenthesizerAttribute k interpretParserDescr'
p
@[implementedBy parenthesizerForKindUnsafe]
constant parenthesizerForKind (k : SyntaxNodeKind) : Parenthesizer
@[combinatorParenthesizer Lean.Parser.withAntiquot]
def withAntiquot.parenthesizer (antiP p : Parenthesizer) : Parenthesizer :=
-- TODO: could be optimized using `isAntiquot` (which would have to be moved), but I'd rather
-- fix the backtracking hack outright.
orelse.parenthesizer antiP p
@[combinatorParenthesizer Lean.Parser.withAntiquotSuffixSplice]
def withAntiquotSuffixSplice.parenthesizer (k : SyntaxNodeKind) (p suffix : Parenthesizer) : Parenthesizer := do
if (← getCur).isAntiquotSuffixSplice then
visitArgs <| suffix *> p
else
p
@[combinatorParenthesizer Lean.Parser.tokenWithAntiquot]
def tokenWithAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do
if (← getCur).isTokenAntiquot then
visitArgs p
else
p
def parenthesizeCategoryCore (cat : Name) (prec : Nat) : Parenthesizer :=
withReader (fun ctx => { ctx with cat := cat }) do
let stx ← getCur
if stx.getKind == `choice then
visitArgs $ stx.getArgs.size.forM fun _ => do
let stx ← getCur
parenthesizerForKind stx.getKind
else
withAntiquot.parenthesizer (mkAntiquot.parenthesizer' cat.toString none) (parenthesizerForKind stx.getKind)
modify fun st => { st with contCat := cat }
@[combinatorParenthesizer Lean.Parser.categoryParser]
def categoryParser.parenthesizer (cat : Name) (prec : Nat) : Parenthesizer := do
let env ← getEnv
match categoryParenthesizerAttribute.getValues env cat with
| p::_ => p prec
-- Fall back to the generic parenthesizer.
-- In this case this node will never be parenthesized since we don't know which parentheses to use.
| _ => parenthesizeCategoryCore cat prec
@[combinatorParenthesizer Lean.Parser.categoryParserOfStack]
def categoryParserOfStack.parenthesizer (offset : Nat) (prec : Nat) : Parenthesizer := do
let st ← get
let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset)
categoryParser.parenthesizer stx.getId prec
@[combinatorParenthesizer Lean.Parser.parserOfStack]
def parserOfStack.parenthesizer (offset : Nat) (prec : Nat := 0) : Parenthesizer := do
let st ← get
let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset)
parenthesizerForKind stx.getKind
@[builtinCategoryParenthesizer term]
def term.parenthesizer : CategoryParenthesizer | prec => do
let stx ← getCur
-- this can happen at `termParser <|> many1 commandParser` in `Term.stxQuot`
if stx.getKind == nullKind then
throwBacktrack
else do
maybeParenthesize `term true (fun stx => Unhygienic.run `(($stx))) prec $
parenthesizeCategoryCore `term prec
@[builtinCategoryParenthesizer tactic]
def tactic.parenthesizer : CategoryParenthesizer | prec => do
maybeParenthesize `tactic false (fun stx => Unhygienic.run `(tactic|($stx))) prec $
parenthesizeCategoryCore `tactic prec
@[builtinCategoryParenthesizer level]
def level.parenthesizer : CategoryParenthesizer | prec => do
maybeParenthesize `level false (fun stx => Unhygienic.run `(level|($stx))) prec $
parenthesizeCategoryCore `level prec
@[combinatorParenthesizer Lean.Parser.error]
def error.parenthesizer (msg : String) : Parenthesizer :=
pure ()
@[combinatorParenthesizer Lean.Parser.errorAtSavedPos]
def errorAtSavedPos.parenthesizer (msg : String) (delta : Bool) : Parenthesizer :=
pure ()
@[combinatorParenthesizer Lean.Parser.atomic]
def atomic.parenthesizer (p : Parenthesizer) : Parenthesizer :=
p
@[combinatorParenthesizer Lean.Parser.lookahead]
def lookahead.parenthesizer (p : Parenthesizer) : Parenthesizer :=
pure ()
@[combinatorParenthesizer Lean.Parser.notFollowedBy]
def notFollowedBy.parenthesizer (p : Parenthesizer) : Parenthesizer :=
pure ()
@[combinatorParenthesizer Lean.Parser.andthen]
def andthen.parenthesizer (p1 p2 : Parenthesizer) : Parenthesizer :=
p2 *> p1
@[combinatorParenthesizer Lean.Parser.node]
def node.parenthesizer (k : SyntaxNodeKind) (p : Parenthesizer) : Parenthesizer := do
let stx ← getCur
if k != stx.getKind then
trace[PrettyPrinter.parenthesize.backtrack] "unexpected node kind '{stx.getKind}', expected '{k}'"
-- HACK; see `orelse.parenthesizer`
throwBacktrack
visitArgs p
@[combinatorParenthesizer Lean.Parser.checkPrec]
def checkPrec.parenthesizer (prec : Nat) : Parenthesizer :=
addPrecCheck prec
@[combinatorParenthesizer Lean.Parser.leadingNode]
def leadingNode.parenthesizer (k : SyntaxNodeKind) (prec : Nat) (p : Parenthesizer) : Parenthesizer := do
node.parenthesizer k p
addPrecCheck prec
-- Limit `cont` precedence to `maxPrec-1`.
-- This is because `maxPrec-1` is the precedence of function application, which is the only way to turn a leading parser
-- into a trailing one.
modify fun st => { st with contPrec := Nat.min (Parser.maxPrec-1) prec }
@[combinatorParenthesizer Lean.Parser.trailingNode]
def trailingNode.parenthesizer (k : SyntaxNodeKind) (prec _ : Nat) (p : Parenthesizer) : Parenthesizer := do
let stx ← getCur
if k != stx.getKind then
trace[PrettyPrinter.parenthesize.backtrack] "unexpected node kind '{stx.getKind}', expected '{k}'"
-- HACK; see `orelse.parenthesizer`
throwBacktrack
visitArgs do
p
addPrecCheck prec
let ctx ← read
modify fun st => { st with contCat := ctx.cat }
-- After visiting the nodes actually produced by the parser passed to `trailingNode`, we are positioned on the
-- left-most child, which is the term injected by `trailingNode` in place of the recursion. Left recursion is not an
-- issue for the parenthesizer, so we can think of this child being produced by `termParser 0`, or whichever Pratt
-- parser is calling us.
categoryParser.parenthesizer ctx.cat 0
@[combinatorParenthesizer Lean.Parser.symbolNoAntiquot] def symbolNoAntiquot.parenthesizer (sym : String) := visitToken
@[combinatorParenthesizer Lean.Parser.unicodeSymbolNoAntiquot] def unicodeSymbolNoAntiquot.parenthesizer (sym asciiSym : String) := visitToken
@[combinatorParenthesizer Lean.Parser.identNoAntiquot] def identNoAntiquot.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.rawIdentNoAntiquot] def rawIdentNoAntiquot.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.identEq] def identEq.parenthesizer (id : Name) := visitToken
@[combinatorParenthesizer Lean.Parser.nonReservedSymbolNoAntiquot] def nonReservedSymbolNoAntiquot.parenthesizer (sym : String) (includeIdent : Bool) := visitToken
@[combinatorParenthesizer Lean.Parser.charLitNoAntiquot] def charLitNoAntiquot.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.strLitNoAntiquot] def strLitNoAntiquot.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.nameLitNoAntiquot] def nameLitNoAntiquot.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.numLitNoAntiquot] def numLitNoAntiquot.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.scientificLitNoAntiquot] def scientificLitNoAntiquot.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.fieldIdx] def fieldIdx.parenthesizer := visitToken
@[combinatorParenthesizer Lean.Parser.manyNoAntiquot]
def manyNoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do
let stx ← getCur
visitArgs $ stx.getArgs.size.forM fun _ => p
@[combinatorParenthesizer Lean.Parser.many1NoAntiquot]
def many1NoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do
manyNoAntiquot.parenthesizer p
@[combinatorParenthesizer Lean.Parser.many1Unbox]
def many1Unbox.parenthesizer (p : Parenthesizer) : Parenthesizer := do
let stx ← getCur
if stx.getKind == nullKind then
manyNoAntiquot.parenthesizer p
else
p
@[combinatorParenthesizer Lean.Parser.optionalNoAntiquot]
def optionalNoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do
visitArgs p
@[combinatorParenthesizer Lean.Parser.sepByNoAntiquot]
def sepByNoAntiquot.parenthesizer (p pSep : Parenthesizer) : Parenthesizer := do
let stx ← getCur
visitArgs $ (List.range stx.getArgs.size).reverse.forM $ fun i => if i % 2 == 0 then p else pSep
@[combinatorParenthesizer Lean.Parser.sepBy1NoAntiquot] def sepBy1NoAntiquot.parenthesizer := sepByNoAntiquot.parenthesizer
@[combinatorParenthesizer Lean.Parser.withPosition] def withPosition.parenthesizer (p : Parenthesizer) : Parenthesizer := do
-- We assume the formatter will indent syntax sufficiently such that parenthesizing a `withPosition` node is never necessary
modify fun st => { st with contPrec := none }
p
@[combinatorParenthesizer Lean.Parser.withoutPosition] def withoutPosition.parenthesizer (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.withForbidden] def withForbidden.parenthesizer (tk : Parser.Token) (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.withoutForbidden] def withoutForbidden.parenthesizer (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.withoutInfo] def withoutInfo.parenthesizer (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.setExpected]
def setExpected.parenthesizer (expected : List String) (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.incQuotDepth] def incQuotDepth.parenthesizer (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.decQuotDepth] def decQuotDepth.parenthesizer (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.suppressInsideQuot] def suppressInsideQuot.parenthesizer (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.evalInsideQuot] def evalInsideQuot.parenthesizer (declName : Name) (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer Lean.Parser.checkStackTop] def checkStackTop.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkWsBefore] def checkWsBefore.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkNoWsBefore] def checkNoWsBefore.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkLinebreakBefore] def checkLinebreakBefore.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkTailWs] def checkTailWs.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkColGe] def checkColGe.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkColGt] def checkColGt.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkLineEq] def checkLineEq.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.eoi] def eoi.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.notFollowedByCategoryToken] def notFollowedByCategoryToken.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkNoImmediateColon] def checkNoImmediateColon.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkInsideQuot] def checkInsideQuot.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.checkOutsideQuot] def checkOutsideQuot.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.skip] def skip.parenthesizer : Parenthesizer := pure ()
@[combinatorParenthesizer Lean.Parser.pushNone] def pushNone.parenthesizer : Parenthesizer := goLeft
@[combinatorParenthesizer Lean.Parser.interpolatedStr]
def interpolatedStr.parenthesizer (p : Parenthesizer) : Parenthesizer := do
visitArgs $ (← getCur).getArgs.reverse.forM fun chunk =>
if chunk.isOfKind interpolatedStrLitKind then
goLeft
else
p
@[combinatorParenthesizer Lean.Parser.dbgTraceState] def dbgTraceState.parenthesizer (label : String) (p : Parenthesizer) : Parenthesizer := p
@[combinatorParenthesizer ite, macroInline] def ite {α : Type} (c : Prop) [h : Decidable c] (t e : Parenthesizer) : Parenthesizer :=
if c then t else e
open Parser
abbrev ParenthesizerAliasValue := AliasValue Parenthesizer
builtin_initialize parenthesizerAliasesRef : IO.Ref (NameMap ParenthesizerAliasValue) ← IO.mkRef {}
def registerAlias (aliasName : Name) (v : ParenthesizerAliasValue) : IO Unit := do
Parser.registerAliasCore parenthesizerAliasesRef aliasName v
instance : Coe Parenthesizer ParenthesizerAliasValue := { coe := AliasValue.const }
instance : Coe (Parenthesizer → Parenthesizer) ParenthesizerAliasValue := { coe := AliasValue.unary }
instance : Coe (Parenthesizer → Parenthesizer → Parenthesizer) ParenthesizerAliasValue := { coe := AliasValue.binary }
builtin_initialize
registerAlias "ws" checkWsBefore.parenthesizer
registerAlias "noWs" checkNoWsBefore.parenthesizer
registerAlias "linebreak" checkLinebreakBefore.parenthesizer
registerAlias "colGt" checkColGt.parenthesizer
registerAlias "colGe" checkColGe.parenthesizer
registerAlias "lookahead" lookahead.parenthesizer
registerAlias "atomic" atomic.parenthesizer
registerAlias "notFollowedBy" notFollowedBy.parenthesizer
registerAlias "withPosition" withPosition.parenthesizer
registerAlias "interpolatedStr" interpolatedStr.parenthesizer
registerAlias "orelse" orelse.parenthesizer
registerAlias "andthen" andthen.parenthesizer
end Parenthesizer
open Parenthesizer
/-- Add necessary parentheses in `stx` parsed by `parser`. -/
def parenthesize (parenthesizer : Parenthesizer) (stx : Syntax) : CoreM Syntax := do
trace[PrettyPrinter.parenthesize.input] "{fmt stx}"
catchInternalId backtrackExceptionId
(do
let (_, st) ← (parenthesizer {}).run { stxTrav := Syntax.Traverser.fromSyntax stx }
pure st.stxTrav.cur)
(fun _ => throwError "parenthesize: uncaught backtrack exception")
def parenthesizeTerm := parenthesize $ categoryParser.parenthesizer `term 0
def parenthesizeCommand := parenthesize $ categoryParser.parenthesizer `command 0
builtin_initialize registerTraceClass `PrettyPrinter.parenthesize
end PrettyPrinter
end Lean
|
c041066e8422b9fb13da17c6cc2e608af1839a13 | ad0c7d243dc1bd563419e2767ed42fb323d7beea | /analysis/metric_space.lean | 0c3ce28e4d58af966999c9050150181ad306dc9e | [
"Apache-2.0"
] | permissive | sebzim4500/mathlib | e0b5a63b1655f910dee30badf09bd7e191d3cf30 | 6997cafbd3a7325af5cb318561768c316ceb7757 | refs/heads/master | 1,585,549,958,618 | 1,538,221,723,000 | 1,538,221,723,000 | 150,869,076 | 0 | 0 | Apache-2.0 | 1,538,229,323,000 | 1,538,229,323,000 | null | UTF-8 | Lean | false | false | 21,873 | lean | /-
Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Metric spaces.
Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro
Many definitions and theorems expected on metric spaces are already introduced on uniform spaces and
topological spaces. For example:
open and closed sets, compactness, completeness, continuity and uniform continuity
-/
import data.real.nnreal analysis.topology.topological_structures
open lattice set filter classical
noncomputable theory
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
/-- Construct a metric space from a distance function and metric space axioms -/
def metric_space.uniform_space_of_dist
(dist : α → α → ℝ)
(dist_self : ∀ x : α, dist x x = 0)
(dist_comm : ∀ x y : α, dist x y = dist y x)
(dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) : uniform_space α :=
uniform_space.of_core {
uniformity := (⨅ ε>0, principal {p:α×α | dist p.1 p.2 < ε}),
refl := le_infi $ assume ε, le_infi $
by simp [set.subset_def, id_rel, dist_self, (>)] {contextual := tt},
comp := le_infi $ assume ε, le_infi $ assume h, lift'_le
(mem_infi_sets (ε / 2) $ mem_infi_sets (div_pos_of_pos_of_pos h two_pos) (subset.refl _)) $
have ∀ (a b c : α), dist a c < ε / 2 → dist c b < ε / 2 → dist a b < ε,
from assume a b c hac hcb,
calc dist a b ≤ dist a c + dist c b : dist_triangle _ _ _
... < ε / 2 + ε / 2 : add_lt_add hac hcb
... = ε : by rw [div_add_div_same, add_self_div_two],
by simpa [comp_rel],
symm := tendsto_infi.2 $ assume ε, tendsto_infi.2 $ assume h,
tendsto_infi' ε $ tendsto_infi' h $ tendsto_principal_principal.2 $ by simp [dist_comm] }
/-- The distance function (given an ambient metric space on `α`), which returns
a nonnegative real number `dist x y` given `x y : α`. -/
class has_dist (α : Type*) := (dist : α → α → ℝ)
export has_dist (dist)
/-- Metric space
Each metric space induces a canonical `uniform_space` and hence a canonical `topological_space`.
This is enforced in the type class definition, by extending the `uniform_space` structure. When
instantiating a `metric_space` structure, the uniformity fields are not necessary, they will be
filled in by default. -/
class metric_space (α : Type u) extends has_dist α : Type u :=
(dist_self : ∀ x : α, dist x x = 0)
(eq_of_dist_eq_zero : ∀ {x y : α}, dist x y = 0 → x = y)
(dist_comm : ∀ x y : α, dist x y = dist y x)
(dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z)
(to_uniform_space : uniform_space α := metric_space.uniform_space_of_dist dist dist_self dist_comm dist_triangle)
(uniformity_dist : uniformity = ⨅ ε>0, principal {p:α×α | dist p.1 p.2 < ε} . control_laws_tac)
theorem uniformity_dist_of_mem_uniformity {U : filter (α × α)} (D : α → α → ℝ)
(H : ∀ s, s ∈ U.sets ↔ ∃ε>0, ∀{a b:α}, D a b < ε → (a, b) ∈ s) :
U = ⨅ ε>0, principal {p:α×α | D p.1 p.2 < ε} :=
le_antisymm
(le_infi $ λ ε, le_infi $ λ ε0, le_principal_iff.2 $ (H _).2 ⟨ε, ε0, λ a b, id⟩)
(λ r ur, let ⟨ε, ε0, h⟩ := (H _).1 ur in
mem_infi_sets ε $ mem_infi_sets ε0 $ mem_principal_sets.2 $ λ ⟨a, b⟩, h)
variables [metric_space α]
instance metric_space.to_uniform_space' : uniform_space α :=
metric_space.to_uniform_space α
@[simp] theorem dist_self (x : α) : dist x x = 0 := metric_space.dist_self x
theorem eq_of_dist_eq_zero {x y : α} : dist x y = 0 → x = y :=
metric_space.eq_of_dist_eq_zero
theorem dist_comm (x y : α) : dist x y = dist y x := metric_space.dist_comm x y
@[simp] theorem dist_eq_zero {x y : α} : dist x y = 0 ↔ x = y :=
iff.intro eq_of_dist_eq_zero (assume : x = y, this ▸ dist_self _)
@[simp] theorem zero_eq_dist {x y : α} : 0 = dist x y ↔ x = y :=
by rw [eq_comm, dist_eq_zero]
theorem dist_triangle (x y z : α) : dist x z ≤ dist x y + dist y z :=
metric_space.dist_triangle x y z
theorem dist_triangle_left (x y z : α) : dist x y ≤ dist z x + dist z y :=
by rw dist_comm z; apply dist_triangle
theorem dist_triangle_right (x y z : α) : dist x y ≤ dist x z + dist y z :=
by rw dist_comm y; apply dist_triangle
theorem swap_dist : function.swap (@dist α _) = dist :=
by funext x y; exact dist_comm _ _
theorem abs_dist_sub_le (x y z : α) : abs (dist x z - dist y z) ≤ dist x y :=
abs_sub_le_iff.2
⟨sub_le_iff_le_add.2 (dist_triangle _ _ _),
sub_le_iff_le_add.2 (dist_triangle_left _ _ _)⟩
theorem dist_nonneg {x y : α} : 0 ≤ dist x y :=
have 2 * dist x y ≥ 0,
from calc 2 * dist x y = dist x y + dist y x : by rw [dist_comm x y, two_mul]
... ≥ 0 : by rw ← dist_self x; apply dist_triangle,
nonneg_of_mul_nonneg_left this two_pos
@[simp] theorem dist_le_zero {x y : α} : dist x y ≤ 0 ↔ x = y :=
by simpa [le_antisymm_iff, dist_nonneg] using @dist_eq_zero _ _ x y
@[simp] theorem dist_pos {x y : α} : 0 < dist x y ↔ x ≠ y :=
by simpa [-dist_le_zero] using not_congr (@dist_le_zero _ _ x y)
section
variables [metric_space α]
def nndist (a b : α) : nnreal := ⟨dist a b, dist_nonneg⟩
@[simp] lemma nndist_self (a : α) : nndist a a = 0 := (nnreal.coe_eq_zero _).1 (dist_self a)
@[simp] lemma coe_dist (a b : α) : (nndist a b : ℝ) = dist a b := rfl
theorem eq_of_nndist_eq_zero {x y : α} : nndist x y = 0 → x = y :=
by simp [nnreal.eq_iff.symm]
theorem nndist_comm (x y : α) : nndist x y = nndist y x :=
by simpa [nnreal.eq_iff.symm] using dist_comm x y
@[simp] theorem nndist_eq_zero {x y : α} : nndist x y = 0 ↔ x = y :=
by simp [nnreal.eq_iff.symm]
@[simp] theorem zero_eq_nndist {x y : α} : 0 = nndist x y ↔ x = y :=
by simp [nnreal.eq_iff.symm]
theorem nndist_triangle (x y z : α) : nndist x z ≤ nndist x y + nndist y z :=
by simpa [nnreal.coe_le] using dist_triangle x y z
theorem nndist_triangle_left (x y z : α) : nndist x y ≤ nndist z x + nndist z y :=
by simpa [nnreal.coe_le] using dist_triangle_left x y z
theorem nndist_triangle_right (x y z : α) : nndist x y ≤ nndist x z + nndist y z :=
by simpa [nnreal.coe_le] using dist_triangle_right x y z
end
/- instantiate metric space as a topology -/
variables {x y z : α} {ε ε₁ ε₂ : ℝ} {s : set α}
/-- `ball x ε` is the set of all points `y` with `dist y x < ε` -/
def ball (x : α) (ε : ℝ) : set α := {y | dist y x < ε}
@[simp] theorem mem_ball : y ∈ ball x ε ↔ dist y x < ε := iff.rfl
theorem mem_ball' : y ∈ ball x ε ↔ dist x y < ε := by rw dist_comm; refl
/-- `closed_ball x ε` is the set of all points `y` with `dist y x ≤ ε` -/
def closed_ball (x : α) (ε : ℝ) := {y | dist y x ≤ ε}
@[simp] theorem mem_closed_ball : y ∈ closed_ball x ε ↔ dist y x ≤ ε := iff.rfl
theorem pos_of_mem_ball (hy : y ∈ ball x ε) : ε > 0 :=
lt_of_le_of_lt dist_nonneg hy
theorem mem_ball_self (h : ε > 0) : x ∈ ball x ε :=
show dist x x < ε, by rw dist_self; assumption
theorem mem_ball_comm : x ∈ ball y ε ↔ y ∈ ball x ε :=
by simp [dist_comm]
theorem ball_subset_ball (h : ε₁ ≤ ε₂) : ball x ε₁ ⊆ ball x ε₂ :=
λ y (yx : _ < ε₁), lt_of_lt_of_le yx h
theorem ball_disjoint (h : ε₁ + ε₂ ≤ dist x y) : ball x ε₁ ∩ ball y ε₂ = ∅ :=
eq_empty_iff_forall_not_mem.2 $ λ z ⟨h₁, h₂⟩,
not_lt_of_le (dist_triangle_left x y z)
(lt_of_lt_of_le (add_lt_add h₁ h₂) h)
theorem ball_disjoint_same (h : ε ≤ dist x y / 2) : ball x ε ∩ ball y ε = ∅ :=
ball_disjoint $ by rwa [← two_mul, ← le_div_iff' two_pos]
theorem ball_subset (h : dist x y ≤ ε₂ - ε₁) : ball x ε₁ ⊆ ball y ε₂ :=
λ z zx, by rw ← add_sub_cancel'_right ε₁ ε₂; exact
lt_of_le_of_lt (dist_triangle z x y) (add_lt_add_of_lt_of_le zx h)
theorem ball_half_subset (y) (h : y ∈ ball x (ε / 2)) : ball y (ε / 2) ⊆ ball x ε :=
ball_subset $ by rw sub_self_div_two; exact le_of_lt h
theorem exists_ball_subset_ball (h : y ∈ ball x ε) : ∃ ε' > 0, ball y ε' ⊆ ball x ε :=
⟨_, sub_pos.2 h, ball_subset $ by rw sub_sub_self⟩
theorem ball_eq_empty_iff_nonpos : ε ≤ 0 ↔ ball x ε = ∅ :=
(eq_empty_iff_forall_not_mem.trans
⟨λ h, le_of_not_gt $ λ ε0, h _ $ mem_ball_self ε0,
λ ε0 y h, not_lt_of_le ε0 $ pos_of_mem_ball h⟩).symm
theorem uniformity_dist : uniformity = (⨅ ε>0, principal {p:α×α | dist p.1 p.2 < ε}) :=
metric_space.uniformity_dist _
theorem uniformity_dist' : uniformity = (⨅ε:{ε:ℝ // ε>0}, principal {p:α×α | dist p.1 p.2 < ε.val}) :=
by simp [infi_subtype]; exact uniformity_dist
theorem mem_uniformity_dist {s : set (α×α)} :
s ∈ (@uniformity α _).sets ↔ (∃ε>0, ∀{a b:α}, dist a b < ε → (a, b) ∈ s) :=
begin
rw [uniformity_dist', infi_sets_eq],
simp [subset_def],
exact assume ⟨r, hr⟩ ⟨p, hp⟩, ⟨⟨min r p, lt_min hr hp⟩, by simp [lt_min_iff, (≥)] {contextual := tt}⟩,
exact ⟨⟨1, zero_lt_one⟩⟩
end
theorem dist_mem_uniformity {ε:ℝ} (ε0 : 0 < ε) :
{p:α×α | dist p.1 p.2 < ε} ∈ (@uniformity α _).sets :=
mem_uniformity_dist.2 ⟨ε, ε0, λ a b, id⟩
theorem uniform_continuous_of_metric [metric_space β] {f : α → β} :
uniform_continuous f ↔ ∀ ε > 0, ∃ δ > 0,
∀{a b:α}, dist a b < δ → dist (f a) (f b) < ε :=
uniform_continuous_def.trans
⟨λ H ε ε0, mem_uniformity_dist.1 $ H _ $ dist_mem_uniformity ε0,
λ H r ru,
let ⟨ε, ε0, hε⟩ := mem_uniformity_dist.1 ru, ⟨δ, δ0, hδ⟩ := H _ ε0 in
mem_uniformity_dist.2 ⟨δ, δ0, λ a b h, hε (hδ h)⟩⟩
theorem uniform_embedding_of_metric [metric_space β] {f : α → β} :
uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧
∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, dist (f a) (f b) < ε → dist a b < δ :=
uniform_embedding_def'.trans $ and_congr iff.rfl $ and_congr iff.rfl
⟨λ H δ δ0, let ⟨t, tu, ht⟩ := H _ (dist_mem_uniformity δ0),
⟨ε, ε0, hε⟩ := mem_uniformity_dist.1 tu in
⟨ε, ε0, λ a b h, ht _ _ (hε h)⟩,
λ H s su, let ⟨δ, δ0, hδ⟩ := mem_uniformity_dist.1 su, ⟨ε, ε0, hε⟩ := H _ δ0 in
⟨_, dist_mem_uniformity ε0, λ a b h, hδ (hε h)⟩⟩
theorem totally_bounded_of_metric {s : set α} :
totally_bounded s ↔ ∀ ε > 0, ∃t : set α, finite t ∧ s ⊆ ⋃y∈t, ball y ε :=
⟨λ H ε ε0, H _ (dist_mem_uniformity ε0),
λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_dist.1 ru,
⟨t, ft, h⟩ := H ε ε0 in
⟨t, ft, subset.trans h $ Union_subset_Union $ λ y, Union_subset_Union $ λ yt z, hε⟩⟩
lemma cauchy_of_metric {f : filter α} :
cauchy f ↔ f ≠ ⊥ ∧ ∀ ε > 0, ∃ t ∈ f.sets, ∀ x y ∈ t, dist x y < ε :=
cauchy_iff.trans $ and_congr iff.rfl
⟨λ H ε ε0, let ⟨t, tf, ts⟩ := H _ (dist_mem_uniformity ε0) in
⟨t, tf, λ x y xt yt, @ts (x, y) ⟨xt, yt⟩⟩,
λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_dist.1 ru,
⟨t, tf, h⟩ := H ε ε0 in
⟨t, tf, λ ⟨x, y⟩ ⟨hx, hy⟩, hε (h x y hx hy)⟩⟩
theorem nhds_eq_metric : nhds x = (⨅ε:{ε:ℝ // ε>0}, principal (ball x ε.val)) :=
begin
rw [nhds_eq_uniformity, uniformity_dist', lift'_infi],
{ apply congr_arg, funext ε,
rw [lift'_principal],
{ simp [ball, dist_comm] },
{ exact monotone_preimage } },
{ exact ⟨⟨1, zero_lt_one⟩⟩ },
{ intros, refl }
end
theorem mem_nhds_iff_metric : s ∈ (nhds x).sets ↔ ∃ε>0, ball x ε ⊆ s :=
begin
rw [nhds_eq_metric, infi_sets_eq],
{ simp },
{ intros y z, cases y with y hy, cases z with z hz,
refine ⟨⟨min y z, lt_min hy hz⟩, _⟩,
simp [ball_subset_ball, min_le_left, min_le_right, (≥)] },
{ exact ⟨⟨1, zero_lt_one⟩⟩ }
end
theorem is_open_metric : is_open s ↔ ∀x∈s, ∃ε>0, ball x ε ⊆ s :=
by simp [is_open_iff_nhds, mem_nhds_iff_metric]
theorem is_open_ball : is_open (ball x ε) :=
is_open_metric.2 $ λ y, exists_ball_subset_ball
theorem ball_mem_nhds (x : α) {ε : ℝ} (ε0 : 0 < ε) : ball x ε ∈ (nhds x).sets :=
mem_nhds_sets is_open_ball (mem_ball_self ε0)
theorem tendsto_nhds_of_metric [metric_space β] {f : α → β} {a b} :
tendsto f (nhds a) (nhds b) ↔ ∀ ε > 0,
∃ δ > 0, ∀{x:α}, dist x a < δ → dist (f x) b < ε :=
⟨λ H ε ε0, mem_nhds_iff_metric.1 (H (ball_mem_nhds _ ε0)),
λ H s hs,
let ⟨ε, ε0, hε⟩ := mem_nhds_iff_metric.1 hs, ⟨δ, δ0, hδ⟩ := H _ ε0 in
mem_nhds_iff_metric.2 ⟨δ, δ0, λ x h, hε (hδ h)⟩⟩
theorem continuous_of_metric [metric_space β] {f : α → β} :
continuous f ↔ ∀b (ε > 0), ∃ δ > 0, ∀a,
dist a b < δ → dist (f a) (f b) < ε :=
continuous_iff_tendsto.trans $ forall_congr $ λ b, tendsto_nhds_of_metric
theorem exists_delta_of_continuous [metric_space β] {f : α → β} {ε:ℝ}
(hf : continuous f) (hε : ε > 0) (b : α) :
∃ δ > 0, ∀a, dist a b ≤ δ → dist (f a) (f b) < ε :=
let ⟨δ, δ_pos, hδ⟩ := continuous_of_metric.1 hf b ε hε in
⟨δ / 2, half_pos δ_pos, assume a ha, hδ a $ lt_of_le_of_lt ha $ div_two_lt_of_pos δ_pos⟩
theorem eq_of_forall_dist_le {x y : α} (h : ∀ε, ε > 0 → dist x y ≤ ε) : x = y :=
eq_of_dist_eq_zero (eq_of_le_of_forall_le_of_dense dist_nonneg h)
instance metric_space.to_separated : separated α :=
separated_def.2 $ λ x y h, eq_of_forall_dist_le $
λ ε ε0, le_of_lt (h _ (dist_mem_uniformity ε0))
/-- Instantiate the reals as a metric space. -/
instance : metric_space ℝ :=
{ dist := λx y, abs (x - y),
dist_self := by simp [abs_zero],
eq_of_dist_eq_zero := by simp [add_neg_eq_zero],
dist_comm := assume x y, abs_sub _ _,
dist_triangle := assume x y z, abs_sub_le _ _ _ }
theorem real.dist_eq (x y : ℝ) : dist x y = abs (x - y) := rfl
theorem real.dist_0_eq_abs (x : ℝ) : dist x 0 = abs x :=
by simp [real.dist_eq]
@[simp] theorem abs_dist {a b : α} : abs (dist a b) = dist a b :=
abs_of_nonneg dist_nonneg
instance : orderable_topology ℝ :=
orderable_topology_of_nhds_abs $ λ x, begin
simp only [show ∀ r, {b : ℝ | abs (x - b) < r} = ball x r,
by simp [-sub_eq_add_neg, abs_sub, ball, real.dist_eq]],
apply le_antisymm,
{ simp [le_infi_iff],
exact λ ε ε0, mem_nhds_sets (is_open_ball) (mem_ball_self ε0) },
{ intros s h,
rcases mem_nhds_iff_metric.1 h with ⟨ε, ε0, ss⟩,
exact mem_infi_sets _ (mem_infi_sets ε0 (mem_principal_sets.2 ss)) },
end
def metric_space.replace_uniformity {α} [U : uniform_space α] (m : metric_space α)
(H : @uniformity _ U = @uniformity _ (metric_space.to_uniform_space α)) :
metric_space α :=
{ dist := @dist _ m.to_has_dist,
dist_self := dist_self,
eq_of_dist_eq_zero := @eq_of_dist_eq_zero _ _,
dist_comm := dist_comm,
dist_triangle := dist_triangle,
to_uniform_space := U,
uniformity_dist := H.trans (@uniformity_dist α _) }
def metric_space.induced {α β} (f : α → β) (hf : function.injective f)
(m : metric_space β) : metric_space α :=
{ dist := λ x y, dist (f x) (f y),
dist_self := λ x, dist_self _,
eq_of_dist_eq_zero := λ x y h, hf (dist_eq_zero.1 h),
dist_comm := λ x y, dist_comm _ _,
dist_triangle := λ x y z, dist_triangle _ _ _,
to_uniform_space := uniform_space.comap f m.to_uniform_space,
uniformity_dist := begin
apply @uniformity_dist_of_mem_uniformity _ _ (λ x y, dist (f x) (f y)),
refine λ s, mem_comap_sets.trans _,
split; intro H,
{ rcases H with ⟨r, ru, rs⟩,
rcases mem_uniformity_dist.1 ru with ⟨ε, ε0, hε⟩,
refine ⟨ε, ε0, λ a b h, rs (hε _)⟩, exact h },
{ rcases H with ⟨ε, ε0, hε⟩,
exact ⟨_, dist_mem_uniformity ε0, λ ⟨a, b⟩, hε⟩ }
end }
theorem metric_space.induced_uniform_embedding {α β} (f : α → β) (hf : function.injective f)
(m : metric_space β) :
by haveI := metric_space.induced f hf m;
exact uniform_embedding f :=
by let := metric_space.induced f hf m; exactI
uniform_embedding_of_metric.2 ⟨hf, uniform_continuous_comap, λ ε ε0, ⟨ε, ε0, λ a b, id⟩⟩
instance {p : α → Prop} [t : metric_space α] : metric_space (subtype p) :=
metric_space.induced subtype.val (λ x y, subtype.eq) t
theorem subtype.dist_eq {p : α → Prop} [t : metric_space α] (x y : subtype p) :
dist x y = dist x.1 y.1 := rfl
instance prod.metric_space_max [metric_space β] : metric_space (α × β) :=
{ dist := λ x y, max (dist x.1 y.1) (dist x.2 y.2),
dist_self := λ x, by simp,
eq_of_dist_eq_zero := λ x y h, begin
cases max_le_iff.1 (le_of_eq h) with h₁ h₂,
exact prod.ext_iff.2 ⟨dist_le_zero.1 h₁, dist_le_zero.1 h₂⟩
end,
dist_comm := λ x y, by simp [dist_comm],
dist_triangle := λ x y z, max_le
(le_trans (dist_triangle _ _ _) (add_le_add (le_max_left _ _) (le_max_left _ _)))
(le_trans (dist_triangle _ _ _) (add_le_add (le_max_right _ _) (le_max_right _ _))),
uniformity_dist := begin
refine uniformity_prod.trans _,
simp [uniformity_dist, comap_infi],
rw ← infi_inf_eq, congr, funext,
rw ← infi_inf_eq, congr, funext,
simp [inf_principal, ext_iff, max_lt_iff]
end,
to_uniform_space := prod.uniform_space }
theorem uniform_continuous_dist' : uniform_continuous (λp:α×α, dist p.1 p.2) :=
uniform_continuous_of_metric.2 (λ ε ε0, ⟨ε/2, half_pos ε0,
begin
suffices,
{ intros p q h, cases p with p₁ p₂, cases q with q₁ q₂,
cases max_lt_iff.1 h with h₁ h₂, clear h,
dsimp at h₁ h₂ ⊢,
rw real.dist_eq,
refine abs_sub_lt_iff.2 ⟨_, _⟩,
{ revert p₁ p₂ q₁ q₂ h₁ h₂, exact this },
{ apply this; rwa dist_comm } },
intros p₁ p₂ q₁ q₂ h₁ h₂,
have := add_lt_add
(abs_sub_lt_iff.1 (lt_of_le_of_lt (abs_dist_sub_le p₁ q₁ p₂) h₁)).1
(abs_sub_lt_iff.1 (lt_of_le_of_lt (abs_dist_sub_le p₂ q₂ q₁) h₂)).1,
rwa [add_halves, dist_comm p₂, sub_add_sub_cancel, dist_comm q₂] at this
end⟩)
theorem uniform_continuous_dist [uniform_space β] {f g : β → α}
(hf : uniform_continuous f) (hg : uniform_continuous g) :
uniform_continuous (λb, dist (f b) (g b)) :=
(hf.prod_mk hg).comp uniform_continuous_dist'
theorem continuous_dist' : continuous (λp:α×α, dist p.1 p.2) :=
uniform_continuous_dist'.continuous
theorem continuous_dist [topological_space β] {f g : β → α}
(hf : continuous f) (hg : continuous g) : continuous (λb, dist (f b) (g b)) :=
(hf.prod_mk hg).comp continuous_dist'
theorem tendsto_dist {f g : β → α} {x : filter β} {a b : α}
(hf : tendsto f x (nhds a)) (hg : tendsto g x (nhds b)) :
tendsto (λx, dist (f x) (g x)) x (nhds (dist a b)) :=
have tendsto (λp:α×α, dist p.1 p.2) (nhds (a, b)) (nhds (dist a b)),
from continuous_iff_tendsto.mp continuous_dist' (a, b),
(hf.prod_mk hg).comp (by rw [nhds_prod_eq] at this; exact this)
lemma nhds_comap_dist (a : α) : (nhds (0 : ℝ)).comap (λa', dist a' a) = nhds a :=
have h₁ : ∀ε, (λa', dist a' a) ⁻¹' ball 0 ε ⊆ ball a ε,
by simp [subset_def, real.dist_0_eq_abs],
have h₂ : tendsto (λa', dist a' a) (nhds a) (nhds (dist a a)),
from tendsto_dist tendsto_id tendsto_const_nhds,
le_antisymm
(by simp [h₁, nhds_eq_metric, infi_le_infi, principal_mono,
-le_principal_iff, -le_infi_iff])
(by simpa [map_le_iff_le_comap.symm, tendsto] using h₂)
lemma tendsto_iff_dist_tendsto_zero {f : β → α} {x : filter β} {a : α} :
(tendsto f x (nhds a)) ↔ (tendsto (λb, dist (f b) a) x (nhds 0)) :=
by rw [← nhds_comap_dist a, tendsto_comap_iff]
theorem is_closed_ball : is_closed (closed_ball x ε) :=
is_closed_le (continuous_dist continuous_id continuous_const) continuous_const
section pi
open finset lattice
variables {π : β → Type*} [fintype β] [∀b, metric_space (π b)]
instance has_dist_pi : has_dist (Πb, π b) :=
⟨λf g, ((finset.sup univ (λb, nndist (f b) (g b)) : nnreal) : ℝ)⟩
lemma dist_pi_def (f g : Πb, π b) :
dist f g = (finset.sup univ (λb, nndist (f b) (g b)) : nnreal) := rfl
instance metric_space_pi : metric_space (Πb, π b) :=
{ dist := dist,
dist_self := assume f, (nnreal.coe_eq_zero _).2 $ bot_unique $ finset.sup_le $ by simp,
dist_comm := assume f g, nnreal.eq_iff.2 $ by congr; ext a; exact nndist_comm _ _,
dist_triangle := assume f g h, show dist f h ≤ (dist f g) + (dist g h), from
begin
simp only [dist_pi_def, (nnreal.coe_add _ _).symm, (nnreal.coe_le _ _).symm,
finset.sup_le_iff],
assume b hb,
exact le_trans (nndist_triangle _ (g b) _) (add_le_add (le_sup hb) (le_sup hb))
end,
eq_of_dist_eq_zero := assume f g eq0,
begin
simp only [dist_pi_def, nnreal.coe_eq_zero, nnreal.bot_eq_zero.symm, eq_bot_iff,
finset.sup_le_iff] at eq0,
exact (funext $ assume b, eq_of_nndist_eq_zero $ bot_unique $ eq0 b $ mem_univ b),
end }
end pi
lemma lebesgue_number_lemma_of_metric
{s : set α} {ι} {c : ι → set α} (hs : compact s)
(hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) :
∃ δ > 0, ∀ x ∈ s, ∃ i, ball x δ ⊆ c i :=
let ⟨n, en, hn⟩ := lebesgue_number_lemma hs hc₁ hc₂,
⟨δ, δ0, hδ⟩ := mem_uniformity_dist.1 en in
⟨δ, δ0, assume x hx, let ⟨i, hi⟩ := hn x hx in
⟨i, assume y hy, hi (hδ (mem_ball'.mp hy))⟩⟩
lemma lebesgue_number_lemma_of_metric_sUnion
{s : set α} {c : set (set α)} (hs : compact s)
(hc₁ : ∀ t ∈ c, is_open t) (hc₂ : s ⊆ ⋃₀ c) :
∃ δ > 0, ∀ x ∈ s, ∃ t ∈ c, ball x δ ⊆ t :=
by rw sUnion_eq_Union at hc₂;
simpa using lebesgue_number_lemma_of_metric hs (by simpa) hc₂
|
64cf89cb320a202d55b80f0a1d867ab6f727f826 | 1dd482be3f611941db7801003235dc84147ec60a | /src/data/subtype.lean | ecec6f922d3a957b2da930a086b6ab4a6523728f | [
"Apache-2.0"
] | permissive | sanderdahmen/mathlib | 479039302bd66434bb5672c2a4cecf8d69981458 | 8f0eae75cd2d8b7a083cf935666fcce4565df076 | refs/heads/master | 1,587,491,322,775 | 1,549,672,060,000 | 1,549,672,060,000 | 169,748,224 | 0 | 0 | Apache-2.0 | 1,549,636,694,000 | 1,549,636,694,000 | null | UTF-8 | Lean | false | false | 2,977 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Johannes Hölzl
-/
section subtype
variables {α : Sort*} {β : α → Prop}
protected lemma subtype.eq' : ∀ {a1 a2 : {x // β x}}, a1.val = a2.val → a1 = a2
| ⟨x, h1⟩ ⟨.(x), h2⟩ rfl := rfl
lemma subtype.ext {a1 a2 : {x // β x}} : a1 = a2 ↔ a1.val = a2.val :=
⟨congr_arg _, subtype.eq'⟩
lemma subtype.coe_ext {a1 a2 : {x // β x}} : a1 = a2 ↔ (a1 : α) = a2 :=
subtype.ext
theorem subtype.val_injective : function.injective (@subtype.val _ β) :=
λ a b, subtype.eq'
@[simp] theorem subtype.coe_eta {α : Type*} {β : α → Prop}
(a : {a // β a}) (h : β a) : subtype.mk ↑a h = a := subtype.eta _ _
@[simp] theorem subtype.coe_mk {α : Type*} {β : α → Prop}
(a h) : (@subtype.mk α β a h : α) = a := rfl
@[simp] theorem subtype.mk_eq_mk {α : Type*} {β : α → Prop}
{a h a' h'} : @subtype.mk α β a h = @subtype.mk α β a' h' ↔ a = a' :=
⟨λ H, by injection H, λ H, by congr; assumption⟩
@[simp] theorem subtype.forall {p : {a // β a} → Prop} :
(∀ x, p x) ↔ (∀ a b, p ⟨a, b⟩) :=
⟨assume h a b, h ⟨a, b⟩, assume h ⟨a, b⟩, h a b⟩
@[simp] theorem subtype.exists {p : {a // β a} → Prop} :
(∃ x, p x) ↔ (∃ a b, p ⟨a, b⟩) :=
⟨assume ⟨⟨a, b⟩, h⟩, ⟨a, b, h⟩, assume ⟨a, b, h⟩, ⟨⟨a, b⟩, h⟩⟩
end subtype
namespace subtype
variables {α : Sort*} {β : Sort*} {γ : Sort*}
/-- Restriction of a function to a function on subtypes. -/
def map {p : α → Prop} {q : β → Prop} (f : α → β) (h : ∀a, p a → q (f a)) :
subtype p → subtype q
| ⟨v, hv⟩ := ⟨f v, h v hv⟩
theorem map_comp {p : α → Prop} {q : β → Prop} {r : γ → Prop} {x : subtype p}
(f : α → β) (h : ∀a, p a → q (f a)) (g : β → γ) (l : ∀a, q a → r (g a)) :
map g l (map f h x) = map (g ∘ f) (assume a ha, l (f a) $ h a ha) x :=
by cases x with v h; refl
theorem map_id {p : α → Prop} {h : ∀a, p a → p (id a)} : map (@id α) h = id :=
funext $ assume ⟨v, h⟩, rfl
end subtype
namespace subtype
variables {α : Sort*}
instance [has_equiv α] (p : α → Prop) : has_equiv (subtype p) :=
⟨λ s t, s.val ≈ t.val⟩
theorem equiv_iff [has_equiv α] {p : α → Prop} {s t : subtype p} :
s ≈ t ↔ s.val ≈ t.val :=
iff.rfl
variables [setoid α] {p : α → Prop}
protected theorem refl (s : subtype p) : s ≈ s :=
setoid.refl s.val
protected theorem symm {s t : subtype p} (h : s ≈ t) : t ≈ s :=
setoid.symm h
protected theorem trans {s t u : subtype p} (h₁ : s ≈ t) (h₂ : t ≈ u) : s ≈ u :=
setoid.trans h₁ h₂
theorem equivalence (p : α → Prop) : equivalence (@has_equiv.equiv (subtype p) _) :=
mk_equivalence _ subtype.refl (@subtype.symm _ _ p) (@subtype.trans _ _ p)
instance (p : α → Prop) : setoid (subtype p) :=
setoid.mk (≈) (equivalence p)
end subtype
|
a2bf81372e13d3506e8ab30c433e451b120745af | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/analysis/convex/join.lean | 4461311991b0ba19e882dda4c3b0231da0655655 | [
"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 | 10,211 | lean | /-
Copyright (c) 2022 Yaël Dillies. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies
-/
import analysis.convex.combination
/-!
# Convex join
This file defines the convex join of two sets. The convex join of `s` and `t` is the union of the
segments with one end in `s` and the other in `t`. This is notably a useful gadget to deal with
convex hulls of finite sets.
-/
open set
open_locale big_operators
variables {ι : Sort*} {𝕜 E : Type*}
section ordered_semiring
variables (𝕜) [ordered_semiring 𝕜] [add_comm_monoid E] [module 𝕜 E] {s t s₁ s₂ t₁ t₂ u : set E}
{x y : E}
/-- The join of two sets is the union of the segments joining them. This can be interpreted as the
topological join, but within the original space. -/
def convex_join (s t : set E) : set E := ⋃ (x ∈ s) (y ∈ t), segment 𝕜 x y
variables {𝕜}
lemma mem_convex_join : x ∈ convex_join 𝕜 s t ↔ ∃ (a ∈ s) (b ∈ t), x ∈ segment 𝕜 a b :=
by simp [convex_join]
lemma convex_join_comm (s t : set E) : convex_join 𝕜 s t = convex_join 𝕜 t s :=
(Union₂_comm _).trans $ by simp_rw [convex_join, segment_symm]
lemma convex_join_mono (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : convex_join 𝕜 s₁ t₁ ⊆ convex_join 𝕜 s₂ t₂ :=
bUnion_mono hs $ λ x hx, bUnion_mono ht $ λ y hy, subset.rfl
lemma convex_join_mono_left (hs : s₁ ⊆ s₂) : convex_join 𝕜 s₁ t ⊆ convex_join 𝕜 s₂ t :=
convex_join_mono hs subset.rfl
lemma convex_join_mono_right (ht : t₁ ⊆ t₂) : convex_join 𝕜 s t₁ ⊆ convex_join 𝕜 s t₂ :=
convex_join_mono subset.rfl ht
@[simp] lemma convex_join_empty_left (t : set E) : convex_join 𝕜 ∅ t = ∅ := by simp [convex_join]
@[simp] lemma convex_join_empty_right (s : set E) : convex_join 𝕜 s ∅ = ∅ := by simp [convex_join]
@[simp] lemma convex_join_singleton_left (t : set E) (x : E) :
convex_join 𝕜 {x} t = ⋃ (y ∈ t), segment 𝕜 x y := by simp [convex_join]
@[simp] lemma convex_join_singleton_right (s : set E) (y : E) :
convex_join 𝕜 s {y} = ⋃ (x ∈ s), segment 𝕜 x y := by simp [convex_join]
@[simp] lemma convex_join_singletons (x : E) : convex_join 𝕜 {x} {y} = segment 𝕜 x y :=
by simp [convex_join]
@[simp] lemma convex_join_union_left (s₁ s₂ t : set E) :
convex_join 𝕜 (s₁ ∪ s₂) t = convex_join 𝕜 s₁ t ∪ convex_join 𝕜 s₂ t :=
by simp_rw [convex_join, mem_union_eq, Union_or, Union_union_distrib]
@[simp] lemma convex_join_union_right (s t₁ t₂ : set E) :
convex_join 𝕜 s (t₁ ∪ t₂) = convex_join 𝕜 s t₁ ∪ convex_join 𝕜 s t₂ :=
by simp_rw [convex_join, mem_union_eq, Union_or, Union_union_distrib]
@[simp] lemma convex_join_Union_left (s : ι → set E) (t : set E) :
convex_join 𝕜 (⋃ i, s i) t = ⋃ i, convex_join 𝕜 (s i) t :=
by { simp_rw [convex_join, mem_Union, Union_exists], exact Union_comm _ }
@[simp] lemma convex_join_Union_right (s : set E) (t : ι → set E) :
convex_join 𝕜 s (⋃ i, t i) = ⋃ i, convex_join 𝕜 s (t i) :=
by simp_rw [convex_join_comm s, convex_join_Union_left]
lemma segment_subset_convex_join (hx : x ∈ s) (hy : y ∈ t) : segment 𝕜 x y ⊆ convex_join 𝕜 s t :=
(subset_Union₂ y hy).trans (subset_Union₂ x hx)
lemma subset_convex_join_left (h : t.nonempty) : s ⊆ convex_join 𝕜 s t :=
λ x hx, let ⟨y, hy⟩ := h in segment_subset_convex_join hx hy $ left_mem_segment _ _ _
lemma subset_convex_join_right (h : s.nonempty) : t ⊆ convex_join 𝕜 s t :=
λ y hy, let ⟨x, hx⟩ := h in segment_subset_convex_join hx hy $ right_mem_segment _ _ _
lemma convex_join_subset (hs : s ⊆ u) (ht : t ⊆ u) (hu : convex 𝕜 u) : convex_join 𝕜 s t ⊆ u :=
Union₂_subset $ λ x hx, Union₂_subset $ λ y hy, hu.segment_subset (hs hx) (ht hy)
lemma convex_join_subset_convex_hull (s t : set E) : convex_join 𝕜 s t ⊆ convex_hull 𝕜 (s ∪ t) :=
convex_join_subset ((subset_union_left _ _).trans $ subset_convex_hull _ _)
((subset_union_right _ _).trans $ subset_convex_hull _ _) $ convex_convex_hull _ _
end ordered_semiring
section linear_ordered_field
variables [linear_ordered_field 𝕜] [add_comm_group E] [module 𝕜 E] {s t u : set E} {x y : E}
lemma convex_join_assoc_aux (s t u : set E) :
convex_join 𝕜 (convex_join 𝕜 s t) u ⊆ convex_join 𝕜 s (convex_join 𝕜 t u) :=
begin
simp_rw [subset_def, mem_convex_join],
rintro _ ⟨z, ⟨x, hx, y, hy, a₁, b₁, ha₁, hb₁, hab₁, rfl⟩, z, hz, a₂, b₂, ha₂, hb₂, hab₂, rfl⟩,
obtain rfl | hb₂ := hb₂.eq_or_lt,
{ refine ⟨x, hx, y, ⟨y, hy, z, hz, left_mem_segment _ _ _⟩, a₁, b₁, ha₁, hb₁, hab₁, _⟩,
rw add_zero at hab₂,
rw [hab₂, one_smul, zero_smul, add_zero] },
have ha₂b₁ : 0 ≤ a₂ * b₁ := mul_nonneg ha₂ hb₁,
have hab : 0 < a₂ * b₁ + b₂ := add_pos_of_nonneg_of_pos ha₂b₁ hb₂,
refine ⟨x, hx, ((a₂ * b₁) / (a₂ * b₁ + b₂)) • y + (b₂ / (a₂ * b₁ + b₂)) • z,
⟨y, hy, z, hz, _, _, _, _, _, rfl⟩, a₂ * a₁, a₂ * b₁ + b₂, mul_nonneg ha₂ ha₁, hab.le, _, _⟩,
{ exact div_nonneg ha₂b₁ hab.le },
{ exact div_nonneg hb₂.le hab.le },
{ rw [←add_div, div_self hab.ne'] },
{ rw [←add_assoc, ←mul_add, hab₁, mul_one, hab₂] },
{ simp_rw [smul_add, ←mul_smul, mul_div_cancel' _ hab.ne', add_assoc] }
end
lemma convex_join_assoc (s t u : set E) :
convex_join 𝕜 (convex_join 𝕜 s t) u = convex_join 𝕜 s (convex_join 𝕜 t u) :=
begin
refine (convex_join_assoc_aux _ _ _).antisymm _,
simp_rw [convex_join_comm s, convex_join_comm _ u],
exact convex_join_assoc_aux _ _ _,
end
lemma convex_join_left_comm (s t u : set E) :
convex_join 𝕜 s (convex_join 𝕜 t u) = convex_join 𝕜 t (convex_join 𝕜 s u) :=
by simp_rw [←convex_join_assoc, convex_join_comm]
lemma convex_join_right_comm (s t u : set E) :
convex_join 𝕜 (convex_join 𝕜 s t) u = convex_join 𝕜 (convex_join 𝕜 s u) t :=
by simp_rw [convex_join_assoc, convex_join_comm]
lemma convex_join_convex_join_convex_join_comm (s t u v : set E) :
convex_join 𝕜 (convex_join 𝕜 s t) (convex_join 𝕜 u v) =
convex_join 𝕜 (convex_join 𝕜 s u) (convex_join 𝕜 t v) :=
by simp_rw [←convex_join_assoc, convex_join_right_comm]
lemma convex_hull_insert (hs : s.nonempty) :
convex_hull 𝕜 (insert x s) = convex_join 𝕜 {x} (convex_hull 𝕜 s) :=
begin
classical,
refine (convex_join_subset ((singleton_subset_iff.2 $ mem_insert _ _).trans $ subset_convex_hull
_ _) (convex_hull_mono $ subset_insert _ _) $ convex_convex_hull _ _).antisymm' (λ x hx, _),
rw convex_hull_eq at hx,
obtain ⟨ι, t, w, z, hw₀, hw₁, hz, rfl⟩ := hx,
have : (∑ i in t.filter (λ i, z i = x), w i) • x + ∑ i in t.filter (λ i, z i ≠ x), w i • z i =
t.center_mass w z,
{ rw [finset.center_mass_eq_of_sum_1 _ _ hw₁, finset.sum_smul],
convert finset.sum_filter_add_sum_filter_not _ _ (w • z) using 2,
refine finset.sum_congr rfl (λ i hi, _),
rw [pi.smul_apply', (finset.mem_filter.1 hi).2] },
rw ←this,
have hw₀' : ∀ i ∈ t.filter (λ i, z i ≠ x), 0 ≤ w i := λ i hi, hw₀ _ $ finset.filter_subset _ _ hi,
obtain hw | hw := (finset.sum_nonneg hw₀').eq_or_gt,
{ rw [←finset.sum_filter_add_sum_filter_not _ (λ i, z i = x), hw, add_zero] at hw₁,
rw [hw₁, one_smul, finset.sum_eq_zero, add_zero],
{ exact subset_convex_join_left hs.convex_hull (mem_singleton _) },
simp_rw finset.sum_eq_zero_iff_of_nonneg hw₀' at hw,
rintro i hi,
rw [hw _ hi, zero_smul] },
refine mem_convex_join.2 ⟨x, mem_singleton _, (t.filter $ λ i, z i ≠ x).center_mass w z,
finset.center_mass_mem_convex_hull _ hw₀' hw (λ i hi, _),
∑ i in t.filter (λ i, z i = x), w i, ∑ i in t.filter (λ i, z i ≠ x), w i,
finset.sum_nonneg (λ i hi, hw₀ _ $ finset.filter_subset _ _ hi), finset.sum_nonneg hw₀', _, _⟩,
{ rw finset.mem_filter at hi,
exact mem_of_mem_insert_of_ne (hz _ hi.1) hi.2 },
{ rw [finset.sum_filter_add_sum_filter_not, hw₁] },
{ rw [finset.center_mass, smul_inv_smul₀ hw.ne', finset.sum_smul] }
end
lemma convex_join_segments (a b c d : E) :
convex_join 𝕜 (segment 𝕜 a b) (segment 𝕜 c d) = convex_hull 𝕜 {a, b, c, d} :=
by simp only [convex_hull_insert, insert_nonempty, singleton_nonempty, convex_hull_pair,
←convex_join_assoc, convex_join_singletons]
lemma convex_join_segment_singleton (a b c : E) :
convex_join 𝕜 (segment 𝕜 a b) {c} = convex_hull 𝕜 {a, b, c} :=
by rw [←pair_eq_singleton, ←convex_join_segments, segment_same, pair_eq_singleton]
lemma convex_join_singleton_segment (a b c : E) :
convex_join 𝕜 {a} (segment 𝕜 b c) = convex_hull 𝕜 {a, b, c} :=
by rw [←segment_same 𝕜, convex_join_segments, insert_idem]
protected lemma convex.convex_join (hs : convex 𝕜 s) (ht : convex 𝕜 t) :
convex 𝕜 (convex_join 𝕜 s t) :=
begin
rw convex_iff_segment_subset at ⊢ ht hs,
simp_rw mem_convex_join,
rintro x ⟨xa, hxa, xb, hxb, hx⟩ y ⟨ya, hya, yb, hyb, hy⟩,
refine (segment_subset_convex_join hx hy).trans _,
have triv : ({xa, xb, ya, yb} : set E) = {xa, ya, xb, yb} := by simp only [set.insert_comm],
rw [convex_join_segments, triv, ←convex_join_segments],
exact convex_join_mono (hs hxa hya) (ht hxb hyb),
end
protected lemma convex.convex_hull_union (hs : convex 𝕜 s) (ht : convex 𝕜 t) (hs₀ : s.nonempty)
(ht₀ : t.nonempty) :
convex_hull 𝕜 (s ∪ t) = convex_join 𝕜 s t :=
(convex_hull_min (union_subset (subset_convex_join_left ht₀) $ subset_convex_join_right hs₀) $
hs.convex_join ht).antisymm $ convex_join_subset_convex_hull _ _
lemma convex_hull_union (hs : s.nonempty) (ht : t.nonempty) :
convex_hull 𝕜 (s ∪ t) = convex_join 𝕜 (convex_hull 𝕜 s) (convex_hull 𝕜 t) :=
begin
rw [←convex_hull_convex_hull_union_left, ←convex_hull_convex_hull_union_right],
exact (convex_convex_hull 𝕜 s).convex_hull_union (convex_convex_hull 𝕜 t)
hs.convex_hull ht.convex_hull,
end
end linear_ordered_field
|
022196fb34d044b76ebdde1f3ef33b0f56af9c4e | bb31430994044506fa42fd667e2d556327e18dfe | /src/data/finset/powerset.lean | 0dd929a6a0e9737a1fea41c3b08816b3be432b8c | [
"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 | 12,419 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import data.finset.lattice
import data.multiset.powerset
/-!
# The powerset of a finset
-/
namespace finset
open function multiset
variables {α : Type*} {s t : finset α}
/-! ### powerset -/
section powerset
/-- When `s` is a finset, `s.powerset` is the finset of all subsets of `s` (seen as finsets). -/
def powerset (s : finset α) : finset (finset α) :=
⟨s.1.powerset.pmap finset.mk $ λ t h, nodup_of_le (mem_powerset.1 h) s.nodup,
s.nodup.powerset.pmap $ λ a ha b hb, congr_arg finset.val⟩
@[simp] theorem mem_powerset {s t : finset α} : s ∈ powerset t ↔ s ⊆ t :=
by cases s; simp only [powerset, mem_mk, mem_pmap, mem_powerset, exists_prop, exists_eq_right];
rw ← val_le_iff
@[simp, norm_cast] lemma coe_powerset (s : finset α) :
(s.powerset : set (finset α)) = coe ⁻¹' (s : set α).powerset :=
by { ext, simp }
@[simp] theorem empty_mem_powerset (s : finset α) : ∅ ∈ powerset s :=
mem_powerset.2 (empty_subset _)
@[simp] lemma mem_powerset_self (s : finset α) : s ∈ powerset s := mem_powerset.2 subset.rfl
lemma powerset_nonempty (s : finset α) : s.powerset.nonempty := ⟨∅, empty_mem_powerset _⟩
@[simp] theorem powerset_mono {s t : finset α} : powerset s ⊆ powerset t ↔ s ⊆ t :=
⟨λ h, (mem_powerset.1 $ h $ mem_powerset_self _),
λ st u h, mem_powerset.2 $ subset.trans (mem_powerset.1 h) st⟩
lemma powerset_injective : injective (powerset : finset α → finset (finset α)) :=
injective_of_le_imp_le _ $ λ s t, powerset_mono.1
@[simp] lemma powerset_inj : powerset s = powerset t ↔ s = t := powerset_injective.eq_iff
@[simp] lemma powerset_empty : (∅ : finset α).powerset = {∅} := rfl
@[simp] lemma powerset_eq_singleton_empty : s.powerset = {∅} ↔ s = ∅ :=
by rw [←powerset_empty, powerset_inj]
/-- **Number of Subsets of a Set** -/
@[simp] theorem card_powerset (s : finset α) :
card (powerset s) = 2 ^ card s :=
(card_pmap _ _ _).trans (card_powerset s.1)
lemma not_mem_of_mem_powerset_of_not_mem {s t : finset α} {a : α}
(ht : t ∈ s.powerset) (h : a ∉ s) : a ∉ t :=
by { apply mt _ h, apply mem_powerset.1 ht }
lemma powerset_insert [decidable_eq α] (s : finset α) (a : α) :
powerset (insert a s) = s.powerset ∪ s.powerset.image (insert a) :=
begin
ext t,
simp only [exists_prop, mem_powerset, mem_image, mem_union, subset_insert_iff],
by_cases h : a ∈ t,
{ split,
{ exact λH, or.inr ⟨_, H, insert_erase h⟩ },
{ intros H,
cases H,
{ exact subset.trans (erase_subset a t) H },
{ rcases H with ⟨u, hu⟩,
rw ← hu.2,
exact subset.trans (erase_insert_subset a u) hu.1 } } },
{ have : ¬ ∃ (u : finset α), u ⊆ s ∧ insert a u = t,
by simp [ne.symm (ne_insert_of_not_mem _ _ h)],
simp [finset.erase_eq_of_not_mem h, this] }
end
/-- For predicate `p` decidable on subsets, it is decidable whether `p` holds for any subset. -/
instance decidable_exists_of_decidable_subsets {s : finset α} {p : Π t ⊆ s, Prop}
[Π t (h : t ⊆ s), decidable (p t h)] : decidable (∃ t (h : t ⊆ s), p t h) :=
decidable_of_iff (∃ t (hs : t ∈ s.powerset), p t (mem_powerset.1 hs))
⟨(λ ⟨t, _, hp⟩, ⟨t, _, hp⟩), (λ ⟨t, hs, hp⟩, ⟨t, mem_powerset.2 hs, hp⟩)⟩
/-- For predicate `p` decidable on subsets, it is decidable whether `p` holds for every subset. -/
instance decidable_forall_of_decidable_subsets {s : finset α} {p : Π t ⊆ s, Prop}
[Π t (h : t ⊆ s), decidable (p t h)] : decidable (∀ t (h : t ⊆ s), p t h) :=
decidable_of_iff (∀ t (h : t ∈ s.powerset), p t (mem_powerset.1 h))
⟨(λ h t hs, h t (mem_powerset.2 hs)), (λ h _ _, h _ _)⟩
/-- A version of `finset.decidable_exists_of_decidable_subsets` with a non-dependent `p`.
Typeclass inference cannot find `hu` here, so this is not an instance. -/
def decidable_exists_of_decidable_subsets' {s : finset α} {p : finset α → Prop}
(hu : Π t (h : t ⊆ s), decidable (p t)) : decidable (∃ t (h : t ⊆ s), p t) :=
@finset.decidable_exists_of_decidable_subsets _ _ _ hu
/-- A version of `finset.decidable_forall_of_decidable_subsets` with a non-dependent `p`.
Typeclass inference cannot find `hu` here, so this is not an instance. -/
def decidable_forall_of_decidable_subsets' {s : finset α} {p : finset α → Prop}
(hu : Π t (h : t ⊆ s), decidable (p t)) : decidable (∀ t (h : t ⊆ s), p t) :=
@finset.decidable_forall_of_decidable_subsets _ _ _ hu
end powerset
section ssubsets
variables [decidable_eq α]
/-- For `s` a finset, `s.ssubsets` is the finset comprising strict subsets of `s`. -/
def ssubsets (s : finset α) : finset (finset α) :=
erase (powerset s) s
@[simp] lemma mem_ssubsets {s t : finset α} : t ∈ s.ssubsets ↔ t ⊂ s :=
by rw [ssubsets, mem_erase, mem_powerset, ssubset_iff_subset_ne, and.comm]
lemma empty_mem_ssubsets {s : finset α} (h : s.nonempty) : ∅ ∈ s.ssubsets :=
by { rw [mem_ssubsets, ssubset_iff_subset_ne], exact ⟨empty_subset s, h.ne_empty.symm⟩, }
/-- For predicate `p` decidable on ssubsets, it is decidable whether `p` holds for any ssubset. -/
instance decidable_exists_of_decidable_ssubsets {s : finset α} {p : Π t ⊂ s, Prop}
[Π t (h : t ⊂ s), decidable (p t h)] : decidable (∃ t h, p t h) :=
decidable_of_iff (∃ t (hs : t ∈ s.ssubsets), p t (mem_ssubsets.1 hs))
⟨(λ ⟨t, _, hp⟩, ⟨t, _, hp⟩), (λ ⟨t, hs, hp⟩, ⟨t, mem_ssubsets.2 hs, hp⟩)⟩
/-- For predicate `p` decidable on ssubsets, it is decidable whether `p` holds for every ssubset. -/
instance decidable_forall_of_decidable_ssubsets {s : finset α} {p : Π t ⊂ s, Prop}
[Π t (h : t ⊂ s), decidable (p t h)] : decidable (∀ t h, p t h) :=
decidable_of_iff (∀ t (h : t ∈ s.ssubsets), p t (mem_ssubsets.1 h))
⟨(λ h t hs, h t (mem_ssubsets.2 hs)), (λ h _ _, h _ _)⟩
/-- A version of `finset.decidable_exists_of_decidable_ssubsets` with a non-dependent `p`.
Typeclass inference cannot find `hu` here, so this is not an instance. -/
def decidable_exists_of_decidable_ssubsets' {s : finset α} {p : finset α → Prop}
(hu : Π t (h : t ⊂ s), decidable (p t)) : decidable (∃ t (h : t ⊂ s), p t) :=
@finset.decidable_exists_of_decidable_ssubsets _ _ _ _ hu
/-- A version of `finset.decidable_forall_of_decidable_ssubsets` with a non-dependent `p`.
Typeclass inference cannot find `hu` here, so this is not an instance. -/
def decidable_forall_of_decidable_ssubsets' {s : finset α} {p : finset α → Prop}
(hu : Π t (h : t ⊂ s), decidable (p t)) : decidable (∀ t (h : t ⊂ s), p t) :=
@finset.decidable_forall_of_decidable_ssubsets _ _ _ _ hu
end ssubsets
section powerset_len
/-- Given an integer `n` and a finset `s`, then `powerset_len n s` is the finset of subsets of `s`
of cardinality `n`. -/
def powerset_len (n : ℕ) (s : finset α) : finset (finset α) :=
⟨(s.1.powerset_len n).pmap finset.mk $ λ t h, nodup_of_le (mem_powerset_len.1 h).1 s.2,
s.2.powerset_len.pmap $ λ a ha b hb, congr_arg finset.val⟩
/-- **Formula for the Number of Combinations** -/
theorem mem_powerset_len {n} {s t : finset α} :
s ∈ powerset_len n t ↔ s ⊆ t ∧ card s = n :=
by cases s; simp [powerset_len, val_le_iff.symm]; refl
@[simp] theorem powerset_len_mono {n} {s t : finset α} (h : s ⊆ t) :
powerset_len n s ⊆ powerset_len n t :=
λ u h', mem_powerset_len.2 $
and.imp (λ h₂, subset.trans h₂ h) id (mem_powerset_len.1 h')
/-- **Formula for the Number of Combinations** -/
@[simp] theorem card_powerset_len (n : ℕ) (s : finset α) :
card (powerset_len n s) = nat.choose (card s) n :=
(card_pmap _ _ _).trans (card_powerset_len n s.1)
@[simp] lemma powerset_len_zero (s : finset α) : finset.powerset_len 0 s = {∅} :=
begin
ext, rw [mem_powerset_len, mem_singleton, card_eq_zero],
refine ⟨λ h, h.2, λ h, by { rw h, exact ⟨empty_subset s, rfl⟩ }⟩,
end
@[simp] theorem powerset_len_empty (n : ℕ) {s : finset α} (h : s.card < n) :
powerset_len n s = ∅ :=
finset.card_eq_zero.mp (by rw [card_powerset_len, nat.choose_eq_zero_of_lt h])
theorem powerset_len_eq_filter {n} {s : finset α} :
powerset_len n s = (powerset s).filter (λ x, x.card = n) :=
by { ext, simp [mem_powerset_len] }
lemma powerset_len_succ_insert [decidable_eq α] {x : α} {s : finset α} (h : x ∉ s) (n : ℕ) :
powerset_len n.succ (insert x s) = powerset_len n.succ s ∪ (powerset_len n s).image (insert x) :=
begin
rw [powerset_len_eq_filter, powerset_insert, filter_union, ←powerset_len_eq_filter],
congr,
rw [powerset_len_eq_filter, image_filter],
congr' 1,
ext t,
simp only [mem_powerset, mem_filter, function.comp_app, and.congr_right_iff],
intro ht,
have : x ∉ t := λ H, h (ht H),
simp [card_insert_of_not_mem this, nat.succ_inj']
end
lemma powerset_len_nonempty {n : ℕ} {s : finset α} (h : n ≤ s.card) :
(powerset_len n s).nonempty :=
begin
classical,
induction s using finset.induction_on with x s hx IH generalizing n,
{ rw [card_empty, le_zero_iff] at h,
rw [h, powerset_len_zero],
exact finset.singleton_nonempty _, },
{ cases n,
{ simp },
{ rw [card_insert_of_not_mem hx, nat.succ_le_succ_iff] at h,
rw powerset_len_succ_insert hx,
refine nonempty.mono _ ((IH h).image (insert x)),
convert (subset_union_right _ _) } }
end
@[simp] lemma powerset_len_self (s : finset α) :
powerset_len s.card s = {s} :=
begin
ext,
rw [mem_powerset_len, mem_singleton],
split,
{ exact λ ⟨hs, hc⟩, eq_of_subset_of_card_le hs hc.ge },
{ rintro rfl,
simp }
end
lemma pairwise_disjoint_powerset_len (s : finset α) :
_root_.pairwise (λ i j, disjoint (s.powerset_len i) (s.powerset_len j)) :=
λ i j hij, finset.disjoint_left.mpr $ λ x hi hj, hij $
(mem_powerset_len.mp hi).2.symm.trans (mem_powerset_len.mp hj).2
lemma powerset_card_disj_Union (s : finset α) :
finset.powerset s =
(range (s.card + 1)).disj_Union (λ i, powerset_len i s)
(s.pairwise_disjoint_powerset_len.set_pairwise _) :=
begin
refine ext (λ a, ⟨λ ha, _, λ ha, _ ⟩),
{ rw mem_disj_Union,
exact ⟨a.card, mem_range.mpr (nat.lt_succ_of_le (card_le_of_subset (mem_powerset.mp ha))),
mem_powerset_len.mpr ⟨mem_powerset.mp ha, rfl⟩⟩ },
{ rcases mem_disj_Union.mp ha with ⟨i, hi, ha⟩,
exact mem_powerset.mpr (mem_powerset_len.mp ha).1, }
end
lemma powerset_card_bUnion [decidable_eq (finset α)] (s : finset α) :
finset.powerset s = (range (s.card + 1)).bUnion (λ i, powerset_len i s) :=
by simpa only [disj_Union_eq_bUnion] using powerset_card_disj_Union s
lemma powerset_len_sup [decidable_eq α] (u : finset α) (n : ℕ) (hn : n < u.card) :
(powerset_len n.succ u).sup id = u :=
begin
apply le_antisymm,
{ simp_rw [finset.sup_le_iff, mem_powerset_len],
rintros x ⟨h, -⟩,
exact h },
{ rw [sup_eq_bUnion, le_iff_subset, subset_iff],
cases (nat.succ_le_of_lt hn).eq_or_lt with h' h',
{ simp [h'] },
{ intros x hx,
simp only [mem_bUnion, exists_prop, id.def],
obtain ⟨t, ht⟩ : ∃ t, t ∈ powerset_len n (u.erase x) := powerset_len_nonempty _,
{ refine ⟨insert x t, _, mem_insert_self _ _⟩,
rw [←insert_erase hx, powerset_len_succ_insert (not_mem_erase _ _)],
exact mem_union_right _ (mem_image_of_mem _ ht) },
{ rw [card_erase_of_mem hx],
exact nat.le_pred_of_lt hn, } } }
end
@[simp]
lemma powerset_len_card_add (s : finset α) {i : ℕ} (hi : 0 < i) :
s.powerset_len (s.card + i) = ∅ :=
finset.powerset_len_empty _ (lt_add_of_pos_right (finset.card s) hi)
@[simp] theorem map_val_val_powerset_len (s : finset α) (i : ℕ) :
(s.powerset_len i).val.map finset.val = s.1.powerset_len i :=
by simp [finset.powerset_len, map_pmap, pmap_eq_map, map_id']
theorem powerset_len_map {β : Type*} (f : α ↪ β) (n : ℕ) (s : finset α) :
powerset_len n (s.map f) = (powerset_len n s).map (map_embedding f).to_embedding :=
eq_of_veq $ multiset.map_injective (@eq_of_veq _) $
by simp_rw [map_val_val_powerset_len, map_val, multiset.map_map, function.comp,
rel_embedding.coe_fn_to_embedding, map_embedding_apply, map_val, ←multiset.map_map _ val,
map_val_val_powerset_len, multiset.powerset_len_map]
end powerset_len
end finset
|
7f870b4e023bead9936018f3dfea2c9de7a192d7 | 367134ba5a65885e863bdc4507601606690974c1 | /src/deprecated/subgroup.lean | 5eadaedd65835aa2269b231926ea51c532d3c01a | [
"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 | 28,842 | 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, Mitchell Rowett, Scott Morrison, Johan Commelin, Mario Carneiro,
Michael Howes
-/
import group_theory.subgroup
import deprecated.submonoid
open set function
variables {G : Type*} {H : Type*} {A : Type*} {a a₁ a₂ b c: G}
section group
variables [group G] [add_group A]
/-- `s` is an additive subgroup: a set containing 0 and closed under addition and negation. -/
class is_add_subgroup (s : set A) extends is_add_submonoid s : Prop :=
(neg_mem {a} : a ∈ s → -a ∈ s)
/-- `s` is a subgroup: a set containing 1 and closed under multiplication and inverse. -/
@[to_additive]
class is_subgroup (s : set G) extends is_submonoid s : Prop :=
(inv_mem {a} : a ∈ s → a⁻¹ ∈ s)
@[to_additive]
lemma is_subgroup.div_mem {s : set G} [is_subgroup s] {x y : G} (hx : x ∈ s) (hy : y ∈ s) :
x / y ∈ s :=
by simpa only [div_eq_mul_inv] using is_submonoid.mul_mem hx (is_subgroup.inv_mem hy)
lemma additive.is_add_subgroup
(s : set G) [is_subgroup s] : @is_add_subgroup (additive G) _ s :=
@is_add_subgroup.mk (additive G) _ _ (additive.is_add_submonoid _)
(@is_subgroup.inv_mem _ _ _ _)
theorem additive.is_add_subgroup_iff
{s : set G} : @is_add_subgroup (additive G) _ s ↔ is_subgroup s :=
⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_subgroup.mk G _ _ ⟨h₁, @h₂⟩ @h₃,
λ h, by exactI additive.is_add_subgroup _⟩
lemma multiplicative.is_subgroup
(s : set A) [is_add_subgroup s] : @is_subgroup (multiplicative A) _ s :=
@is_subgroup.mk (multiplicative A) _ _ (multiplicative.is_submonoid _)
(@is_add_subgroup.neg_mem _ _ _ _)
theorem multiplicative.is_subgroup_iff
{s : set A} : @is_subgroup (multiplicative A) _ s ↔ is_add_subgroup s :=
⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_add_subgroup.mk A _ _ ⟨h₁, @h₂⟩ @h₃,
λ h, by exactI multiplicative.is_subgroup _⟩
/-- The group structure on a subgroup coerced to a type. -/
@[to_additive "The additive group structure on an additive subgroup coerced to a type."]
def subtype.group {s : set G} [is_subgroup s] : group s :=
{ inv := λ x, ⟨(x:G)⁻¹, is_subgroup.inv_mem x.2⟩,
mul_left_inv := λ x, subtype.eq $ mul_left_inv x.1,
div := λ x y, ⟨(x / y : G), is_subgroup.div_mem x.2 y.2⟩,
div_eq_mul_inv := λ x y, subtype.ext $ div_eq_mul_inv x.1 y.1,
.. subtype.monoid }
/-- The commutative group structure on a commutative subgroup coerced to a type. -/
@[to_additive "The additive commutative group structure
on a additive commutative subgroup coerced to a type."]
def subtype.comm_group {G : Type*} [comm_group G] {s : set G} [is_subgroup s] : comm_group s :=
{ .. subtype.group, .. subtype.comm_monoid }
section
local attribute [instance] subtype.group subtype.add_group
@[simp, norm_cast, to_additive]
lemma is_subgroup.coe_inv {s : set G} [is_subgroup s] (a : s) : ((a⁻¹ : s) : G) = a⁻¹ := rfl
attribute [norm_cast] is_add_subgroup.coe_neg
@[simp, norm_cast] lemma is_subgroup.coe_gpow {s : set G} [is_subgroup s] (a : s) (n : ℤ) :
((a ^ n : s) : G) = a ^ n :=
by induction n; simp [is_submonoid.coe_pow a]
@[simp, norm_cast] lemma is_add_subgroup.gsmul_coe {s : set A} [is_add_subgroup s] (a : s) (n : ℤ) :
((gsmul n a : s) : A) = gsmul n a :=
by induction n; simp [is_add_submonoid.smul_coe a]
attribute [to_additive gsmul_coe] is_subgroup.coe_gpow
end
@[to_additive of_add_neg]
theorem is_subgroup.of_div (s : set G)
(one_mem : (1:G) ∈ s) (div_mem : ∀{a b:G}, a ∈ s → b ∈ s → a * b⁻¹ ∈ s) :
is_subgroup s :=
have inv_mem : ∀a, a ∈ s → a⁻¹ ∈ s, from
assume a ha,
have 1 * a⁻¹ ∈ s, from div_mem one_mem ha,
by simpa,
{ inv_mem := inv_mem,
mul_mem := assume a b ha hb,
have a * b⁻¹⁻¹ ∈ s, from div_mem ha (inv_mem b hb),
by simpa,
one_mem := one_mem }
theorem is_add_subgroup.of_sub (s : set A)
(zero_mem : (0:A) ∈ s) (sub_mem : ∀{a b:A}, a ∈ s → b ∈ s → a - b ∈ s) :
is_add_subgroup s :=
is_add_subgroup.of_add_neg s zero_mem
(λ x y hx hy, by simpa only [sub_eq_add_neg] using sub_mem hx hy)
@[to_additive]
instance is_subgroup.inter (s₁ s₂ : set G) [is_subgroup s₁] [is_subgroup s₂] :
is_subgroup (s₁ ∩ s₂) :=
{ inv_mem := λ x hx, ⟨is_subgroup.inv_mem hx.1, is_subgroup.inv_mem hx.2⟩ }
@[to_additive]
instance is_subgroup.Inter {ι : Sort*} (s : ι → set G) [h : ∀ y : ι, is_subgroup (s y)] :
is_subgroup (set.Inter s) :=
{ inv_mem := λ x h, set.mem_Inter.2 $ λ y, is_subgroup.inv_mem (set.mem_Inter.1 h y) }
@[to_additive]
lemma is_subgroup_Union_of_directed {ι : Type*} [hι : nonempty ι]
(s : ι → set G) [∀ i, is_subgroup (s i)]
(directed : ∀ i j, ∃ k, s i ⊆ s k ∧ s j ⊆ s k) :
is_subgroup (⋃i, s i) :=
{ inv_mem := λ a ha,
let ⟨i, hi⟩ := set.mem_Union.1 ha in
set.mem_Union.2 ⟨i, is_subgroup.inv_mem hi⟩,
to_is_submonoid := is_submonoid_Union_of_directed s directed }
def gpowers (x : G) : set G := set.range ((^) x : ℤ → G)
def gmultiples (x : A) : set A := set.range (λ i, gsmul i x)
attribute [to_additive gmultiples] gpowers
instance gpowers.is_subgroup (x : G) : is_subgroup (gpowers x) :=
{ one_mem := ⟨(0:ℤ), by simp⟩,
mul_mem := assume x₁ x₂ ⟨i₁, h₁⟩ ⟨i₂, h₂⟩, ⟨i₁ + i₂, by simp [gpow_add, *]⟩,
inv_mem := assume x₀ ⟨i, h⟩, ⟨-i, by simp [h.symm]⟩ }
instance gmultiples.is_add_subgroup (x : A) : is_add_subgroup (gmultiples x) :=
multiplicative.is_subgroup_iff.1 $ gpowers.is_subgroup _
attribute [to_additive] gpowers.is_subgroup
lemma is_subgroup.gpow_mem {a : G} {s : set G} [is_subgroup s] (h : a ∈ s) : ∀{i:ℤ}, a ^ i ∈ s
| (n : ℕ) := is_submonoid.pow_mem h
| -[1+ n] := is_subgroup.inv_mem (is_submonoid.pow_mem h)
lemma is_add_subgroup.gsmul_mem {a : A} {s : set A} [is_add_subgroup s] :
a ∈ s → ∀{i:ℤ}, gsmul i a ∈ s :=
@is_subgroup.gpow_mem (multiplicative A) _ _ _ (multiplicative.is_subgroup _)
lemma gpowers_subset {a : G} {s : set G} [is_subgroup s] (h : a ∈ s) : gpowers a ⊆ s :=
λ x hx, match x, hx with _, ⟨i, rfl⟩ := is_subgroup.gpow_mem h end
lemma gmultiples_subset {a : A} {s : set A} [is_add_subgroup s] (h : a ∈ s) : gmultiples a ⊆ s :=
@gpowers_subset (multiplicative A) _ _ _ (multiplicative.is_subgroup _) h
attribute [to_additive gmultiples_subset] gpowers_subset
lemma mem_gpowers {a : G} : a ∈ gpowers a := ⟨1, by simp⟩
lemma mem_gmultiples {a : A} : a ∈ gmultiples a := ⟨1, by simp⟩
attribute [to_additive mem_gmultiples] mem_gpowers
end group
namespace is_subgroup
open is_submonoid
variables [group G] (s : set G) [is_subgroup s]
@[to_additive]
lemma inv_mem_iff : a⁻¹ ∈ s ↔ a ∈ s :=
⟨λ h, by simpa using inv_mem h, inv_mem⟩
@[to_additive]
lemma mul_mem_cancel_right (h : a ∈ s) : b * a ∈ s ↔ b ∈ s :=
⟨λ hba, by simpa using mul_mem hba (inv_mem h), λ hb, mul_mem hb h⟩
@[to_additive]
lemma mul_mem_cancel_left (h : a ∈ s) : a * b ∈ s ↔ b ∈ s :=
⟨λ hab, by simpa using mul_mem (inv_mem h) hab, mul_mem h⟩
end is_subgroup
class normal_add_subgroup [add_group A] (s : set A) extends is_add_subgroup s : Prop :=
(normal : ∀ n ∈ s, ∀ g : A, g + n + -g ∈ s)
@[to_additive]
class normal_subgroup [group G] (s : set G) extends is_subgroup s : Prop :=
(normal : ∀ n ∈ s, ∀ g : G, g * n * g⁻¹ ∈ s)
@[to_additive]
lemma normal_subgroup_of_comm_group [comm_group G] (s : set G) [hs : is_subgroup s] :
normal_subgroup s :=
{ normal := λ n hn g, by rwa [mul_right_comm, mul_right_inv, one_mul],
..hs }
lemma additive.normal_add_subgroup [group G]
(s : set G) [normal_subgroup s] : @normal_add_subgroup (additive G) _ s :=
@normal_add_subgroup.mk (additive G) _ _
(@additive.is_add_subgroup G _ _ _)
(@normal_subgroup.normal _ _ _ _)
theorem additive.normal_add_subgroup_iff [group G]
{s : set G} : @normal_add_subgroup (additive G) _ s ↔ normal_subgroup s :=
⟨by rintro ⟨h₁, h₂⟩; exact
@normal_subgroup.mk G _ _ (additive.is_add_subgroup_iff.1 h₁) @h₂,
λ h, by exactI additive.normal_add_subgroup _⟩
lemma multiplicative.normal_subgroup [add_group A]
(s : set A) [normal_add_subgroup s] : @normal_subgroup (multiplicative A) _ s :=
@normal_subgroup.mk (multiplicative A) _ _
(@multiplicative.is_subgroup A _ _ _)
(@normal_add_subgroup.normal _ _ _ _)
theorem multiplicative.normal_subgroup_iff [add_group A]
{s : set A} : @normal_subgroup (multiplicative A) _ s ↔ normal_add_subgroup s :=
⟨by rintro ⟨h₁, h₂⟩; exact
@normal_add_subgroup.mk A _ _ (multiplicative.is_subgroup_iff.1 h₁) @h₂,
λ h, by exactI multiplicative.normal_subgroup _⟩
namespace is_subgroup
variable [group G]
-- Normal subgroup properties
@[to_additive]
lemma mem_norm_comm {s : set G} [normal_subgroup s] {a b : G} (hab : a * b ∈ s) : b * a ∈ s :=
have h : a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ s, from normal_subgroup.normal (a * b) hab a⁻¹,
by simp at h; exact h
@[to_additive]
lemma mem_norm_comm_iff {s : set G} [normal_subgroup s] {a b : G} : a * b ∈ s ↔ b * a ∈ s :=
⟨mem_norm_comm, mem_norm_comm⟩
/-- The trivial subgroup -/
@[to_additive]
def trivial (G : Type*) [group G] : set G := {1}
@[simp, to_additive]
lemma mem_trivial {g : G} : g ∈ trivial G ↔ g = 1 :=
mem_singleton_iff
@[to_additive]
instance trivial_normal : normal_subgroup (trivial G) :=
by refine {..}; simp [trivial] {contextual := tt}
@[to_additive]
lemma eq_trivial_iff {s : set G} [is_subgroup s] :
s = trivial G ↔ (∀ x ∈ s, x = (1 : G)) :=
by simp only [set.ext_iff, is_subgroup.mem_trivial];
exact ⟨λ h x, (h x).1, λ h x, ⟨h x, λ hx, hx.symm ▸ is_submonoid.one_mem⟩⟩
@[to_additive]
instance univ_subgroup : normal_subgroup (@univ G) :=
by refine {..}; simp
@[to_additive add_center]
def center (G : Type*) [group G] : set G := {z | ∀ g, g * z = z * g}
@[to_additive mem_add_center]
lemma mem_center {a : G} : a ∈ center G ↔ ∀g, g * a = a * g := iff.rfl
@[to_additive add_center_normal]
instance center_normal : normal_subgroup (center G) :=
{ one_mem := by simp [center],
mul_mem := assume a b ha hb g,
by rw [←mul_assoc, mem_center.2 ha g, mul_assoc, mem_center.2 hb g, ←mul_assoc],
inv_mem := assume a ha g,
calc
g * a⁻¹ = a⁻¹ * (g * a) * a⁻¹ : by simp [ha g]
... = a⁻¹ * g : by rw [←mul_assoc, mul_assoc]; simp,
normal := assume n ha g h,
calc
h * (g * n * g⁻¹) = h * n : by simp [ha g, mul_assoc]
... = g * g⁻¹ * n * h : by rw ha h; simp
... = g * n * g⁻¹ * h : by rw [mul_assoc g, ha g⁻¹, ←mul_assoc] }
@[to_additive add_normalizer]
def normalizer (s : set G) : set G :=
{g : G | ∀ n, n ∈ s ↔ g * n * g⁻¹ ∈ s}
@[to_additive]
instance normalizer_is_subgroup (s : set G) : is_subgroup (normalizer s) :=
{ one_mem := by simp [normalizer],
mul_mem := λ a b (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s)
(hb : ∀ n, n ∈ s ↔ b * n * b⁻¹ ∈ s) n,
by rw [mul_inv_rev, ← mul_assoc, mul_assoc a, mul_assoc a, ← ha, ← hb],
inv_mem := λ a (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s) n,
by rw [ha (a⁻¹ * n * a⁻¹⁻¹)];
simp [mul_assoc] }
@[to_additive subset_add_normalizer]
lemma subset_normalizer (s : set G) [is_subgroup s] : s ⊆ normalizer s :=
λ g hg n, by rw [is_subgroup.mul_mem_cancel_right _ ((is_subgroup.inv_mem_iff _).2 hg),
is_subgroup.mul_mem_cancel_left _ hg]
local attribute [instance] subtype.group
/-- Every subgroup is a normal subgroup of its normalizer -/
@[to_additive add_normal_in_add_normalizer]
instance normal_in_normalizer (s : set G) [is_subgroup s] :
normal_subgroup (subtype.val ⁻¹' s : set (normalizer s)) :=
{ one_mem := show (1 : G) ∈ s, from is_submonoid.one_mem,
mul_mem := λ a b ha hb, show (a * b : G) ∈ s, from is_submonoid.mul_mem ha hb,
inv_mem := λ a ha, show (a⁻¹ : G) ∈ s, from is_subgroup.inv_mem ha,
normal := λ a ha ⟨m, hm⟩, (hm a).1 ha }
end is_subgroup
-- Homomorphism subgroups
namespace is_group_hom
open is_submonoid is_subgroup
open is_mul_hom (map_mul)
@[to_additive]
def ker [group H] (f : G → H) : set G := preimage f (trivial H)
@[to_additive]
lemma mem_ker [group H] (f : G → H) {x : G} : x ∈ ker f ↔ f x = 1 :=
mem_trivial
variables [group G] [group H]
@[to_additive]
lemma one_ker_inv (f : G → H) [is_group_hom f] {a b : G} (h : f (a * b⁻¹) = 1) : f a = f b :=
begin
rw [map_mul f, map_inv f] at h,
rw [←inv_inv (f b), eq_inv_of_mul_eq_one h]
end
@[to_additive]
lemma one_ker_inv' (f : G → H) [is_group_hom f] {a b : G} (h : f (a⁻¹ * b) = 1) : f a = f b :=
begin
rw [map_mul f, map_inv f] at h,
apply inv_injective,
rw eq_inv_of_mul_eq_one h
end
@[to_additive]
lemma inv_ker_one (f : G → H) [is_group_hom f] {a b : G} (h : f a = f b) : f (a * b⁻¹) = 1 :=
have f a * (f b)⁻¹ = 1, by rw [h, mul_right_inv],
by rwa [←map_inv f, ←map_mul f] at this
@[to_additive]
lemma inv_ker_one' (f : G → H) [is_group_hom f] {a b : G} (h : f a = f b) : f (a⁻¹ * b) = 1 :=
have (f a)⁻¹ * f b = 1, by rw [h, mul_left_inv],
by rwa [←map_inv f, ←map_mul f] at this
@[to_additive]
lemma one_iff_ker_inv (f : G → H) [is_group_hom f] (a b : G) : f a = f b ↔ f (a * b⁻¹) = 1 :=
⟨inv_ker_one f, one_ker_inv f⟩
@[to_additive]
lemma one_iff_ker_inv' (f : G → H) [is_group_hom f] (a b : G) : f a = f b ↔ f (a⁻¹ * b) = 1 :=
⟨inv_ker_one' f, one_ker_inv' f⟩
@[to_additive]
lemma inv_iff_ker (f : G → H) [w : is_group_hom f] (a b : G) : f a = f b ↔ a * b⁻¹ ∈ ker f :=
by rw [mem_ker]; exact one_iff_ker_inv _ _ _
@[to_additive]
lemma inv_iff_ker' (f : G → H) [w : is_group_hom f] (a b : G) : f a = f b ↔ a⁻¹ * b ∈ ker f :=
by rw [mem_ker]; exact one_iff_ker_inv' _ _ _
@[to_additive]
instance image_subgroup (f : G → H) [is_group_hom f] (s : set G) [is_subgroup s] :
is_subgroup (f '' s) :=
{ mul_mem := assume a₁ a₂ ⟨b₁, hb₁, eq₁⟩ ⟨b₂, hb₂, eq₂⟩,
⟨b₁ * b₂, mul_mem hb₁ hb₂, by simp [eq₁, eq₂, map_mul f]⟩,
one_mem := ⟨1, one_mem, map_one f⟩,
inv_mem := assume a ⟨b, hb, eq⟩, ⟨b⁻¹, inv_mem hb, by rw map_inv f; simp *⟩ }
@[to_additive]
instance range_subgroup (f : G → H) [is_group_hom f] : is_subgroup (set.range f) :=
@set.image_univ _ _ f ▸ is_group_hom.image_subgroup f set.univ
local attribute [simp] one_mem inv_mem mul_mem normal_subgroup.normal
@[to_additive]
instance preimage (f : G → H) [is_group_hom f] (s : set H) [is_subgroup s] :
is_subgroup (f ⁻¹' s) :=
by refine {..}; simp [map_mul f, map_one f, map_inv f, @inv_mem H _ s] {contextual:=tt}
@[to_additive]
instance preimage_normal (f : G → H) [is_group_hom f] (s : set H) [normal_subgroup s] :
normal_subgroup (f ⁻¹' s) :=
⟨by simp [map_mul f, map_inv f] {contextual:=tt}⟩
@[to_additive]
instance normal_subgroup_ker (f : G → H) [is_group_hom f] : normal_subgroup (ker f) :=
is_group_hom.preimage_normal f (trivial H)
@[to_additive]
lemma injective_of_trivial_ker (f : G → H) [is_group_hom f] (h : ker f = trivial G) :
function.injective f :=
begin
intros a₁ a₂ hfa,
simp [ext_iff, ker, is_subgroup.trivial] at h,
have ha : a₁ * a₂⁻¹ = 1, by rw ←h; exact inv_ker_one f hfa,
rw [eq_inv_of_mul_eq_one ha, inv_inv a₂]
end
@[to_additive]
lemma trivial_ker_of_injective (f : G → H) [is_group_hom f] (h : function.injective f) :
ker f = trivial G :=
set.ext $ assume x, iff.intro
(assume hx,
suffices f x = f 1, by simpa using h this,
by simp [map_one f]; rwa [mem_ker] at hx)
(by simp [mem_ker, is_group_hom.map_one f] {contextual := tt})
@[to_additive]
lemma injective_iff_trivial_ker (f : G → H) [is_group_hom f] :
function.injective f ↔ ker f = trivial G :=
⟨trivial_ker_of_injective f, injective_of_trivial_ker f⟩
@[to_additive]
lemma trivial_ker_iff_eq_one (f : G → H) [is_group_hom f] :
ker f = trivial G ↔ ∀ x, f x = 1 → x = 1 :=
by rw set.ext_iff; simp [ker]; exact
⟨λ h x hx, (h x).1 hx, λ h x, ⟨h x, λ hx, by rw [hx, map_one f]⟩⟩
end is_group_hom
section
local attribute [instance] subtype.group
@[to_additive]
instance subtype_val.is_group_hom [group G] {s : set G} [is_subgroup s] :
is_group_hom (subtype.val : s → G) := { ..subtype_val.is_monoid_hom }
@[to_additive]
instance coe.is_group_hom [group G] {s : set G} [is_subgroup s] :
is_group_hom (coe : s → G) := { ..subtype_val.is_monoid_hom }
@[to_additive]
instance subtype_mk.is_group_hom [group G] [group H] {s : set G}
[is_subgroup s] (f : H → G) [is_group_hom f] (h : ∀ x, f x ∈ s) :
is_group_hom (λ x, (⟨f x, h x⟩ : s)) := { ..subtype_mk.is_monoid_hom f h }
@[to_additive]
instance set_inclusion.is_group_hom [group G] {s t : set G}
[is_subgroup s] [is_subgroup t] (h : s ⊆ t) : is_group_hom (set.inclusion h) :=
subtype_mk.is_group_hom _ _
end
section
local attribute [instance] subtype.monoid
/-- `subtype.val : set.range f → H` as a monoid homomorphism, when `f` is a monoid homomorphism. -/
@[to_additive "`subtype.val : set.range f → H` as an additive monoid homomorphism, when `f` is
an additive monoid homomorphism."]
def monoid_hom.range_subtype_val [monoid G] [monoid H] (f : G →* H) : (set.range f) →* H :=
monoid_hom.of subtype.val
/-- `set.range_factorization f : G → set.range f` as a monoid homomorphism, when `f` is a monoid
homomorphism. -/
@[to_additive "`set.range_factorization f : G → set.range f` as an additive monoid homomorphism,
when `f` is an additive monoid homomorphism."]
def monoid_hom.range_factorization [monoid G] [monoid H] (f : G →* H) : G →* (set.range f) :=
{ to_fun := set.range_factorization f,
map_one' := by { dsimp [set.range_factorization], simp, refl, },
map_mul' := by { intros, dsimp [set.range_factorization], simp, refl, } }
end
namespace add_group
variables [add_group A]
inductive in_closure (s : set A) : A → Prop
| basic {a : A} : a ∈ s → in_closure a
| zero : in_closure 0
| neg {a : A} : in_closure a → in_closure (-a)
| add {a b : A} : in_closure a → in_closure b → in_closure (a + b)
end add_group
namespace group
open is_submonoid is_subgroup
variables [group G] {s : set G}
@[to_additive]
inductive in_closure (s : set G) : G → Prop
| basic {a : G} : a ∈ s → in_closure a
| one : in_closure 1
| inv {a : G} : in_closure a → in_closure a⁻¹
| mul {a b : G} : in_closure a → in_closure b → in_closure (a * b)
/-- `group.closure s` is the subgroup closed over `s`, i.e. the smallest subgroup containg s. -/
@[to_additive]
def closure (s : set G) : set G := {a | in_closure s a }
@[to_additive]
lemma mem_closure {a : G} : a ∈ s → a ∈ closure s := in_closure.basic
@[to_additive]
instance closure.is_subgroup (s : set G) : is_subgroup (closure s) :=
{ one_mem := in_closure.one,
mul_mem := assume a b, in_closure.mul,
inv_mem := assume a, in_closure.inv }
@[to_additive]
theorem subset_closure {s : set G} : s ⊆ closure s := λ a, mem_closure
@[to_additive]
theorem closure_subset {s t : set G} [is_subgroup t] (h : s ⊆ t) : closure s ⊆ t :=
assume a ha, by induction ha; simp [h _, *, one_mem, mul_mem, inv_mem_iff]
@[to_additive]
lemma closure_subset_iff (s t : set G) [is_subgroup t] : closure s ⊆ t ↔ s ⊆ t :=
⟨assume h b ha, h (mem_closure ha), assume h b ha, closure_subset h ha⟩
@[to_additive]
theorem closure_mono {s t : set G} (h : s ⊆ t) : closure s ⊆ closure t :=
closure_subset $ set.subset.trans h subset_closure
@[simp, to_additive]
lemma closure_subgroup (s : set G) [is_subgroup s] : closure s = s :=
set.subset.antisymm (closure_subset $ set.subset.refl s) subset_closure
@[to_additive]
theorem exists_list_of_mem_closure {s : set G} {a : G} (h : a ∈ closure s) :
(∃l:list G, (∀x∈l, x ∈ s ∨ x⁻¹ ∈ s) ∧ l.prod = a) :=
in_closure.rec_on h
(λ x hxs, ⟨[x], list.forall_mem_singleton.2 $ or.inl hxs, one_mul _⟩)
⟨[], list.forall_mem_nil _, rfl⟩
(λ x _ ⟨L, HL1, HL2⟩, ⟨L.reverse.map has_inv.inv,
λ x hx, let ⟨y, hy1, hy2⟩ := list.exists_of_mem_map hx in
hy2 ▸ or.imp id (by rw [inv_inv]; exact id) (HL1 _ $ list.mem_reverse.1 hy1).symm,
HL2 ▸ list.rec_on L one_inv.symm (λ hd tl ih,
by rw [list.reverse_cons, list.map_append, list.prod_append, ih, list.map_singleton,
list.prod_cons, list.prod_nil, mul_one, list.prod_cons, mul_inv_rev])⟩)
(λ x y hx hy ⟨L1, HL1, HL2⟩ ⟨L2, HL3, HL4⟩, ⟨L1 ++ L2, list.forall_mem_append.2 ⟨HL1, HL3⟩,
by rw [list.prod_append, HL2, HL4]⟩)
@[to_additive]
lemma image_closure [group H] (f : G → H) [is_group_hom f] (s : set G) :
f '' closure s = closure (f '' s) :=
le_antisymm
begin
rintros _ ⟨x, hx, rfl⟩,
apply in_closure.rec_on hx; intros,
{ solve_by_elim [subset_closure, set.mem_image_of_mem] },
{ rw [is_monoid_hom.map_one f], apply is_submonoid.one_mem },
{ rw [is_group_hom.map_inv f], apply is_subgroup.inv_mem, assumption },
{ rw [is_monoid_hom.map_mul f], solve_by_elim [is_submonoid.mul_mem] }
end
(closure_subset $ set.image_subset _ subset_closure)
@[to_additive]
theorem mclosure_subset {s : set G} : monoid.closure s ⊆ closure s :=
monoid.closure_subset $ subset_closure
@[to_additive]
theorem mclosure_inv_subset {s : set G} : monoid.closure (has_inv.inv ⁻¹' s) ⊆ closure s :=
monoid.closure_subset $ λ x hx, inv_inv x ▸ (is_subgroup.inv_mem $ subset_closure hx)
@[to_additive]
theorem closure_eq_mclosure {s : set G} : closure s = monoid.closure (s ∪ has_inv.inv ⁻¹' s) :=
set.subset.antisymm
(@closure_subset _ _ _ (monoid.closure (s ∪ has_inv.inv ⁻¹' s))
{ inv_mem := λ x hx, monoid.in_closure.rec_on hx
(λ x hx, or.cases_on hx (λ hx, monoid.subset_closure $ or.inr $
show x⁻¹⁻¹ ∈ s, from (inv_inv x).symm ▸ hx)
(λ hx, monoid.subset_closure $ or.inl hx))
((@one_inv G _).symm ▸ is_submonoid.one_mem)
(λ x y hx hy ihx ihy, (mul_inv_rev x y).symm ▸ is_submonoid.mul_mem ihy ihx) }
(set.subset.trans (set.subset_union_left _ _) monoid.subset_closure))
(monoid.closure_subset $ set.union_subset subset_closure $
λ x hx, inv_inv x ▸ (is_subgroup.inv_mem $ subset_closure hx))
@[to_additive]
theorem mem_closure_union_iff {G : Type*} [comm_group G] {s t : set G} {x : G} :
x ∈ closure (s ∪ t) ↔ ∃ y ∈ closure s, ∃ z ∈ closure t, y * z = x :=
begin
simp only [closure_eq_mclosure, monoid.mem_closure_union_iff, exists_prop, preimage_union], split,
{ rintro ⟨_, ⟨ys, hys, yt, hyt, rfl⟩, _, ⟨zs, hzs, zt, hzt, rfl⟩, rfl⟩,
refine ⟨_, ⟨_, hys, _, hzs, rfl⟩, _, ⟨_, hyt, _, hzt, rfl⟩, _⟩,
rw [mul_assoc, mul_assoc, mul_left_comm zs] },
{ rintro ⟨_, ⟨ys, hys, zs, hzs, rfl⟩, _, ⟨yt, hyt, zt, hzt, rfl⟩, rfl⟩,
refine ⟨_, ⟨ys, hys, yt, hyt, rfl⟩, _, ⟨zs, hzs, zt, hzt, rfl⟩, _⟩,
rw [mul_assoc, mul_assoc, mul_left_comm yt] }
end
@[to_additive gmultiples_eq_closure]
theorem gpowers_eq_closure {a : G} : gpowers a = closure {a} :=
subset.antisymm
(gpowers_subset $ mem_closure $ by simp)
(closure_subset $ by simp [mem_gpowers])
end group
namespace is_subgroup
variable [group G]
@[to_additive]
lemma trivial_eq_closure : trivial G = group.closure ∅ :=
subset.antisymm
(by simp [set.subset_def, is_submonoid.one_mem])
(group.closure_subset $ by simp)
end is_subgroup
/-The normal closure of a set s is the subgroup closure of all the conjugates of
elements of s. It is the smallest normal subgroup containing s. -/
namespace group
variables {s : set G} [group G]
lemma conjugates_subset {t : set G} [normal_subgroup t] {a : G} (h : a ∈ t) : conjugates a ⊆ t :=
λ x ⟨c,w⟩,
begin
have H := normal_subgroup.normal a h c,
rwa ←w,
end
theorem conjugates_of_set_subset' {s t : set G} [normal_subgroup t] (h : s ⊆ t) :
conjugates_of_set s ⊆ t :=
set.bUnion_subset (λ x H, conjugates_subset (h H))
/-- The normal closure of a set s is the subgroup closure of all the conjugates of
elements of s. It is the smallest normal subgroup containing s. -/
def normal_closure (s : set G) : set G := closure (conjugates_of_set s)
theorem conjugates_of_set_subset_normal_closure : conjugates_of_set s ⊆ normal_closure s :=
subset_closure
theorem subset_normal_closure : s ⊆ normal_closure s :=
set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure
/-- The normal closure of a set is a subgroup. -/
instance normal_closure.is_subgroup (s : set G) : is_subgroup (normal_closure s) :=
closure.is_subgroup (conjugates_of_set s)
/-- The normal closure of s is a normal subgroup. -/
instance normal_closure.is_normal : normal_subgroup (normal_closure s) :=
⟨ λ n h g,
begin
induction h with x hx x hx ihx x y hx hy ihx ihy,
{exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx))},
{simpa using (normal_closure.is_subgroup s).one_mem},
{rw ←conj_inv,
exact (is_subgroup.inv_mem ihx)},
{rw ←conj_mul,
exact (is_submonoid.mul_mem ihx ihy)},
end ⟩
/-- The normal closure of s is the smallest normal subgroup containing s. -/
theorem normal_closure_subset {s t : set G} [normal_subgroup t] (h : s ⊆ t) :
normal_closure s ⊆ t :=
λ a w,
begin
induction w with x hx x hx ihx x y hx hy ihx ihy,
{exact (conjugates_of_set_subset' h $ hx)},
{exact is_submonoid.one_mem},
{exact is_subgroup.inv_mem ihx},
{exact is_submonoid.mul_mem ihx ihy}
end
lemma normal_closure_subset_iff {s t : set G} [normal_subgroup t] : s ⊆ t ↔ normal_closure s ⊆ t :=
⟨normal_closure_subset, set.subset.trans (subset_normal_closure)⟩
theorem normal_closure_mono {s t : set G} : s ⊆ t → normal_closure s ⊆ normal_closure t :=
λ h, normal_closure_subset (set.subset.trans h (subset_normal_closure))
end group
section simple_group
class simple_group (G : Type*) [group G] : Prop :=
(simple : ∀ (N : set G) [normal_subgroup N], N = is_subgroup.trivial G ∨ N = set.univ)
class simple_add_group (A : Type*) [add_group A] : Prop :=
(simple : ∀ (N : set A) [normal_add_subgroup N], N = is_add_subgroup.trivial A ∨ N = set.univ)
attribute [to_additive] simple_group
theorem additive.simple_add_group_iff [group G] :
simple_add_group (additive G) ↔ simple_group G :=
⟨λ hs, ⟨λ N h, @simple_add_group.simple _ _ hs _ (by exactI additive.normal_add_subgroup_iff.2 h)⟩,
λ hs, ⟨λ N h, @simple_group.simple _ _ hs _ (by exactI additive.normal_add_subgroup_iff.1 h)⟩⟩
instance additive.simple_add_group [group G] [simple_group G] :
simple_add_group (additive G) := additive.simple_add_group_iff.2 (by apply_instance)
theorem multiplicative.simple_group_iff [add_group A] :
simple_group (multiplicative A) ↔ simple_add_group A :=
⟨λ hs, ⟨λ N h, @simple_group.simple _ _ hs _ (by exactI multiplicative.normal_subgroup_iff.2 h)⟩,
λ hs, ⟨λ N h,
@simple_add_group.simple _ _ hs _ (by exactI multiplicative.normal_subgroup_iff.1 h)⟩⟩
instance multiplicative.simple_group [add_group A] [simple_add_group A] :
simple_group (multiplicative A) := multiplicative.simple_group_iff.2 (by apply_instance)
@[to_additive]
lemma simple_group_of_surjective [group G] [group H] [simple_group G] (f : G → H)
[is_group_hom f] (hf : function.surjective f) : simple_group H :=
⟨λ H iH, have normal_subgroup (f ⁻¹' H), by resetI; apply_instance,
begin
resetI,
cases simple_group.simple (f ⁻¹' H) with h h,
{ refine or.inl (is_subgroup.eq_trivial_iff.2 (λ x hx, _)),
cases hf x with y hy,
rw ← hy at hx,
rw [← hy, is_subgroup.eq_trivial_iff.1 h y hx, is_group_hom.map_one f] },
{ refine or.inr (set.eq_univ_of_forall (λ x, _)),
cases hf x with y hy,
rw set.eq_univ_iff_forall at h,
rw ← hy,
exact h y }
end⟩
end simple_group
/-- Create a bundled subgroup from a set `s` and `[is_subroup s]`. -/
@[to_additive "Create a bundled additive subgroup from a set `s` and `[is_add_subgroup s]`."]
def subgroup.of [group G] (s : set G) [h : is_subgroup s] : subgroup G :=
{ carrier := s,
one_mem' := h.1.1,
mul_mem' := h.1.2,
inv_mem' := h.2 }
@[to_additive]
instance subgroup.is_subgroup [group G] (K : subgroup G) : is_subgroup (K : set G) :=
{ one_mem := K.one_mem',
mul_mem := K.mul_mem',
inv_mem := K.inv_mem' }
@[to_additive]
instance subgroup.of_normal [group G] (s : set G) [h : is_subgroup s] [n : normal_subgroup s] :
subgroup.normal (subgroup.of s) :=
{ conj_mem := n.normal, }
|
eb296e49b00970968d671923db0bfb34434046af | 367134ba5a65885e863bdc4507601606690974c1 | /src/data/equiv/local_equiv.lean | e643ee5268f4ebc927a5d43ef775f819e666fcc1 | [
"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 | 25,690 | 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 data.equiv.basic
/-!
# Local equivalences
This files defines equivalences between subsets of given types.
An element `e` of `local_equiv α β` is made of two maps `e.to_fun` and `e.inv_fun` respectively
from α to β and from β to α (just like equivs), which are inverse to each other on the subsets
`e.source` and `e.target` of respectively α and β.
They are designed in particular to define charts on manifolds.
The main functionality is `e.trans f`, which composes the two local equivalences by restricting
the source and target to the maximal set where the composition makes sense.
As for equivs, we register a coercion to functions and use it in our simp normal form: we write
`e x` and `e.symm y` instead of `e.to_fun x` and `e.inv_fun y`.
## Main definitions
`equiv.to_local_equiv`: associating a local equiv to an equiv, with source = target = univ
`local_equiv.symm` : the inverse of a local equiv
`local_equiv.trans` : the composition of two local equivs
`local_equiv.refl` : the identity local equiv
`local_equiv.of_set` : the identity on a set `s`
`eq_on_source` : equivalence relation describing the "right" notion of equality for local
equivs (see below in implementation notes)
## Implementation notes
There are at least three possible implementations of local equivalences:
* equivs on subtypes
* pairs of functions taking values in `option α` and `option β`, equal to none where the local
equivalence is not defined
* pairs of functions defined everywhere, keeping the source and target as additional data
Each of these implementations has pros and cons.
* When dealing with subtypes, one still need to define additional API for composition and
restriction of domains. Checking that one always belongs to the right subtype makes things very
tedious, and leads quickly to DTT hell (as the subtype `u ∩ v` is not the "same" as `v ∩ u`, for
instance).
* With option-valued functions, the composition is very neat (it is just the usual composition, and
the domain is restricted automatically). These are implemented in `pequiv.lean`. For manifolds,
where one wants to discuss thoroughly the smoothness of the maps, this creates however a lot of
overhead as one would need to extend all classes of smoothness to option-valued maps.
* The local_equiv version as explained above is easier to use for manifolds. The drawback is that
there is extra useless data (the values of `to_fun` and `inv_fun` outside of `source` and `target`).
In particular, the equality notion between local equivs is not "the right one", i.e., coinciding
source and target and equality there. Moreover, there are no local equivs in this sense between
an empty type and a nonempty type. Since empty types are not that useful, and since one almost never
needs to talk about equal local equivs, this is not an issue in practice.
Still, we introduce an equivalence relation `eq_on_source` that captures this right notion of
equality, and show that many properties are invariant under this equivalence relation.
### Local coding conventions
If a lemma deals with the intersection of a set with either source or target of a `local_equiv`,
then it should use `e.source ∩ s` or `e.target ∩ t`, not `s ∩ e.source` or `t ∩ e.target`.
-/
mk_simp_attribute mfld_simps "The simpset `mfld_simps` records several simp lemmas that are
especially useful in manifolds. It is a subset of the whole set of simp lemmas, but it makes it
possible to have quicker proofs (when used with `squeeze_simp` or `simp only`) while retaining
readability.
The typical use case is the following, in a file on manifolds:
If `simp [foo, bar]` is slow, replace it with `squeeze_simp [foo, bar] with mfld_simps` and paste
its output. The list of lemmas should be reasonable (contrary to the output of
`squeeze_simp [foo, bar]` which might contain tens of lemmas), and the outcome should be quick
enough.
"
-- register in the simpset `mfld_simps` several lemmas that are often useful when dealing
-- with manifolds
attribute [mfld_simps] id.def function.comp.left_id set.mem_set_of_eq set.image_eq_empty
set.univ_inter set.preimage_univ set.prod_mk_mem_set_prod_eq and_true set.mem_univ
set.mem_image_of_mem true_and set.mem_inter_eq set.mem_preimage function.comp_app
set.inter_subset_left set.mem_prod set.range_id and_self set.mem_range_self
eq_self_iff_true forall_const forall_true_iff set.inter_univ set.preimage_id function.comp.right_id
not_false_iff and_imp set.prod_inter_prod set.univ_prod_univ true_or or_true prod.map_mk
set.preimage_inter heq_iff_eq equiv.sigma_equiv_prod_apply equiv.sigma_equiv_prod_symm_apply
subtype.coe_mk equiv.to_fun_as_coe equiv.inv_fun_as_coe
namespace tactic.interactive
/-- A very basic tactic to show that sets showing up in manifolds coincide or are included in
one another. -/
meta def mfld_set_tac : tactic unit := do
goal ← tactic.target,
match goal with
| `(%%e₁ = %%e₂) :=
`[ext my_y,
split;
{ assume h_my_y,
try { simp only [*, -h_my_y] with mfld_simps at h_my_y },
simp only [*] with mfld_simps }]
| `(%%e₁ ⊆ %%e₂) :=
`[assume my_y h_my_y,
try { simp only [*, -h_my_y] with mfld_simps at h_my_y },
simp only [*] with mfld_simps]
| _ := tactic.fail "goal should be an equality or an inclusion"
end
end tactic.interactive
open function set
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
/-- Local equivalence between subsets `source` and `target` of α and β respectively. The (global)
maps `to_fun : α → β` and `inv_fun : β → α` map `source` to `target` and conversely, and are inverse
to each other there. The values of `to_fun` outside of `source` and of `inv_fun` outside of `target`
are irrelevant. -/
@[nolint has_inhabited_instance]
structure local_equiv (α : Type*) (β : Type*) :=
(to_fun : α → β)
(inv_fun : β → α)
(source : set α)
(target : set β)
(map_source' : ∀{x}, x ∈ source → to_fun x ∈ target)
(map_target' : ∀{x}, x ∈ target → inv_fun x ∈ source)
(left_inv' : ∀{x}, x ∈ source → inv_fun (to_fun x) = x)
(right_inv' : ∀{x}, x ∈ target → to_fun (inv_fun x) = x)
/-- Associating a local_equiv to an equiv-/
def equiv.to_local_equiv (e : equiv α β) : local_equiv α β :=
{ to_fun := e.to_fun,
inv_fun := e.inv_fun,
source := univ,
target := univ,
map_source' := λx hx, mem_univ _,
map_target' := λy hy, mem_univ _,
left_inv' := λx hx, e.left_inv x,
right_inv' := λx hx, e.right_inv x }
namespace local_equiv
variables (e : local_equiv α β) (e' : local_equiv β γ)
/-- The inverse of a local equiv -/
protected def symm : local_equiv β α :=
{ to_fun := e.inv_fun,
inv_fun := e.to_fun,
source := e.target,
target := e.source,
map_source' := e.map_target',
map_target' := e.map_source',
left_inv' := e.right_inv',
right_inv' := e.left_inv' }
instance : has_coe_to_fun (local_equiv α β) := ⟨_, local_equiv.to_fun⟩
@[simp, mfld_simps] theorem coe_mk (f : α → β) (g s t ml mr il ir) :
(local_equiv.mk f g s t ml mr il ir : α → β) = f := rfl
@[simp, mfld_simps] theorem coe_symm_mk (f : α → β) (g s t ml mr il ir) :
((local_equiv.mk f g s t ml mr il ir).symm : β → α) = g := rfl
@[simp, mfld_simps] lemma to_fun_as_coe : e.to_fun = e := rfl
@[simp, mfld_simps] lemma inv_fun_as_coe : e.inv_fun = e.symm := rfl
@[simp, mfld_simps] lemma map_source {x : α} (h : x ∈ e.source) : e x ∈ e.target :=
e.map_source' h
@[simp, mfld_simps] lemma map_target {x : β} (h : x ∈ e.target) : e.symm x ∈ e.source :=
e.map_target' h
@[simp, mfld_simps] lemma left_inv {x : α} (h : x ∈ e.source) : e.symm (e x) = x :=
e.left_inv' h
@[simp, mfld_simps] lemma right_inv {x : β} (h : x ∈ e.target) : e (e.symm x) = x :=
e.right_inv' h
protected lemma maps_to : maps_to e e.source e.target := λ x, e.map_source
lemma symm_maps_to : maps_to e.symm e.target e.source := e.symm.maps_to
protected lemma left_inv_on : left_inv_on e.symm e e.source := λ x, e.left_inv
protected lemma right_inv_on : right_inv_on e.symm e e.target := λ x, e.right_inv
protected lemma inv_on : inv_on e.symm e e.source e.target := ⟨e.left_inv_on, e.right_inv_on⟩
protected lemma inj_on : inj_on e e.source := e.left_inv_on.inj_on
protected lemma bij_on : bij_on e e.source e.target := e.inv_on.bij_on e.maps_to e.symm_maps_to
protected lemma surj_on : surj_on e e.source e.target := e.bij_on.surj_on
/-- Associating to a local_equiv an equiv between the source and the target -/
protected def to_equiv : equiv (e.source) (e.target) :=
{ to_fun := λ x, ⟨e x, e.map_source x.mem⟩,
inv_fun := λ y, ⟨e.symm y, e.map_target y.mem⟩,
left_inv := λ⟨x, hx⟩, subtype.eq $ e.left_inv hx,
right_inv := λ⟨y, hy⟩, subtype.eq $ e.right_inv hy }
@[simp, mfld_simps] lemma symm_source : e.symm.source = e.target := rfl
@[simp, mfld_simps] lemma symm_target : e.symm.target = e.source := rfl
@[simp, mfld_simps] lemma symm_symm : e.symm.symm = e := by { cases e, refl }
lemma image_source_eq_target : e '' e.source = e.target := e.bij_on.image_eq
lemma image_source_inter_eq' (s : set α) :
e '' (e.source ∩ s) = e.target ∩ e.symm ⁻¹' s :=
by rw [inter_comm, e.left_inv_on.image_inter', image_source_eq_target, inter_comm]
lemma image_source_inter_eq (s : set α) :
e '' (e.source ∩ s) = e.target ∩ e.symm ⁻¹' (e.source ∩ s) :=
by rw [inter_comm, e.left_inv_on.image_inter, image_source_eq_target, inter_comm]
lemma image_eq_target_inter_inv_preimage {s : set α} (h : s ⊆ e.source) :
e '' s = e.target ∩ e.symm ⁻¹' s :=
by rw [← e.image_source_inter_eq', inter_eq_self_of_subset_right h]
lemma symm_image_eq_source_inter_preimage {s : set β} (h : s ⊆ e.target) :
e.symm '' s = e.source ∩ e ⁻¹' s :=
e.symm.image_eq_target_inter_inv_preimage h
lemma symm_image_target_inter_eq (s : set β) :
e.symm '' (e.target ∩ s) = e.source ∩ e ⁻¹' (e.target ∩ s) :=
e.symm.image_source_inter_eq _
lemma symm_image_target_inter_eq' (s : set β) :
e.symm '' (e.target ∩ s) = e.source ∩ e ⁻¹' s :=
e.symm.image_source_inter_eq' _
lemma source_inter_preimage_inv_preimage (s : set α) :
e.source ∩ e ⁻¹' (e.symm ⁻¹' s) = e.source ∩ s :=
set.ext $ λ x, and.congr_right_iff.2 $ λ hx, by simp only [mem_preimage, e.left_inv hx]
lemma target_inter_inv_preimage_preimage (s : set β) :
e.target ∩ e.symm ⁻¹' (e ⁻¹' s) = e.target ∩ s :=
e.symm.source_inter_preimage_inv_preimage _
lemma source_subset_preimage_target : e.source ⊆ e ⁻¹' e.target :=
e.maps_to
lemma symm_image_target_eq_source : e.symm '' e.target = e.source :=
e.symm.image_source_eq_target
lemma target_subset_preimage_source : e.target ⊆ e.symm ⁻¹' e.source :=
e.symm_maps_to
/-- Two local equivs that have the same `source`, same `to_fun` and same `inv_fun`, coincide. -/
@[ext]
protected lemma ext {e e' : local_equiv α β} (h : ∀x, e x = e' x)
(hsymm : ∀x, e.symm x = e'.symm x) (hs : e.source = e'.source) : e = e' :=
begin
have A : (e : α → β) = e', by { ext x, exact h x },
have B : (e.symm : β → α) = e'.symm, by { ext x, exact hsymm x },
have I : e '' e.source = e.target := e.image_source_eq_target,
have I' : e' '' e'.source = e'.target := e'.image_source_eq_target,
rw [A, hs, I'] at I,
cases e; cases e',
simp * at *
end
/-- Restricting a local equivalence to e.source ∩ s -/
protected def restr (s : set α) : local_equiv α β :=
{ to_fun := e,
inv_fun := e.symm,
source := e.source ∩ s,
target := e.target ∩ e.symm⁻¹' s,
map_source' := λx hx, begin
simp only with mfld_simps at hx,
simp only [hx] with mfld_simps,
end,
map_target' := λy hy, begin
simp only with mfld_simps at hy,
simp only [hy] with mfld_simps,
end,
left_inv' := λx hx, e.left_inv hx.1,
right_inv' := λy hy, e.right_inv hy.1 }
@[simp, mfld_simps] lemma restr_coe (s : set α) : (e.restr s : α → β) = e := rfl
@[simp, mfld_simps] lemma restr_coe_symm (s : set α) : ((e.restr s).symm : β → α) = e.symm := rfl
@[simp, mfld_simps] lemma restr_source (s : set α) : (e.restr s).source = e.source ∩ s := rfl
@[simp, mfld_simps] lemma restr_target (s : set α) :
(e.restr s).target = e.target ∩ e.symm ⁻¹' s := rfl
lemma restr_eq_of_source_subset {e : local_equiv α β} {s : set α} (h : e.source ⊆ s) :
e.restr s = e :=
local_equiv.ext (λ_, rfl) (λ_, rfl) (by simp [inter_eq_self_of_subset_left h])
@[simp, mfld_simps] lemma restr_univ {e : local_equiv α β} : e.restr univ = e :=
restr_eq_of_source_subset (subset_univ _)
/-- The identity local equiv -/
protected def refl (α : Type*) : local_equiv α α := (equiv.refl α).to_local_equiv
@[simp, mfld_simps] lemma refl_source : (local_equiv.refl α).source = univ := rfl
@[simp, mfld_simps] lemma refl_target : (local_equiv.refl α).target = univ := rfl
@[simp, mfld_simps] lemma refl_coe : (local_equiv.refl α : α → α) = id := rfl
@[simp, mfld_simps] lemma refl_symm : (local_equiv.refl α).symm = local_equiv.refl α := rfl
@[simp, mfld_simps] lemma refl_restr_source (s : set α) :
((local_equiv.refl α).restr s).source = s :=
by simp
@[simp, mfld_simps] lemma refl_restr_target (s : set α) :
((local_equiv.refl α).restr s).target = s :=
by { change univ ∩ id⁻¹' s = s, simp }
/-- The identity local equiv on a set `s` -/
def of_set (s : set α) : local_equiv α α :=
{ to_fun := id,
inv_fun := id,
source := s,
target := s,
map_source' := λx hx, hx,
map_target' := λx hx, hx,
left_inv' := λx hx, rfl,
right_inv' := λx hx, rfl }
@[simp, mfld_simps] lemma of_set_source (s : set α) : (local_equiv.of_set s).source = s := rfl
@[simp, mfld_simps] lemma of_set_target (s : set α) : (local_equiv.of_set s).target = s := rfl
@[simp, mfld_simps] lemma of_set_coe (s : set α) : (local_equiv.of_set s : α → α) = id := rfl
@[simp, mfld_simps] lemma of_set_symm (s : set α) :
(local_equiv.of_set s).symm = local_equiv.of_set s := rfl
/-- Composing two local equivs if the target of the first coincides with the source of the
second. -/
protected def trans' (e' : local_equiv β γ) (h : e.target = e'.source) :
local_equiv α γ :=
{ to_fun := e' ∘ e,
inv_fun := e.symm ∘ e'.symm,
source := e.source,
target := e'.target,
map_source' := λx hx, by simp [h.symm, hx],
map_target' := λy hy, by simp [h, hy],
left_inv' := λx hx, by simp [hx, h.symm],
right_inv' := λy hy, by simp [hy, h] }
/-- Composing two local equivs, by restricting to the maximal domain where their composition
is well defined. -/
protected def trans : local_equiv α γ :=
local_equiv.trans' (e.symm.restr (e'.source)).symm (e'.restr (e.target)) (inter_comm _ _)
@[simp, mfld_simps] lemma coe_trans : (e.trans e' : α → γ) = e' ∘ e := rfl
@[simp, mfld_simps] lemma coe_trans_symm : ((e.trans e').symm : γ → α) = e.symm ∘ e'.symm := rfl
lemma trans_symm_eq_symm_trans_symm : (e.trans e').symm = e'.symm.trans e.symm :=
by cases e; cases e'; refl
@[simp, mfld_simps] lemma trans_source : (e.trans e').source = e.source ∩ e ⁻¹' e'.source := rfl
lemma trans_source' : (e.trans e').source = e.source ∩ e ⁻¹' (e.target ∩ e'.source) :=
by mfld_set_tac
lemma trans_source'' : (e.trans e').source = e.symm '' (e.target ∩ e'.source) :=
by rw [e.trans_source', e.symm_image_target_inter_eq]
lemma image_trans_source : e '' (e.trans e').source = e.target ∩ e'.source :=
(e.symm.restr e'.source).symm.image_source_eq_target
@[simp, mfld_simps] lemma trans_target :
(e.trans e').target = e'.target ∩ e'.symm ⁻¹' e.target := rfl
lemma trans_target' : (e.trans e').target = e'.target ∩ e'.symm ⁻¹' (e'.source ∩ e.target) :=
trans_source' e'.symm e.symm
lemma trans_target'' : (e.trans e').target = e' '' (e'.source ∩ e.target) :=
trans_source'' e'.symm e.symm
lemma inv_image_trans_target : e'.symm '' (e.trans e').target = e'.source ∩ e.target :=
image_trans_source e'.symm e.symm
lemma trans_assoc (e'' : local_equiv γ δ) : (e.trans e').trans e'' = e.trans (e'.trans e'') :=
local_equiv.ext (λx, rfl) (λx, rfl) (by simp [trans_source, @preimage_comp α β γ, inter_assoc])
@[simp, mfld_simps] lemma trans_refl : e.trans (local_equiv.refl β) = e :=
local_equiv.ext (λx, rfl) (λx, rfl) (by simp [trans_source])
@[simp, mfld_simps] lemma refl_trans : (local_equiv.refl α).trans e = e :=
local_equiv.ext (λx, rfl) (λx, rfl) (by simp [trans_source, preimage_id])
lemma trans_refl_restr (s : set β) :
e.trans ((local_equiv.refl β).restr s) = e.restr (e ⁻¹' s) :=
local_equiv.ext (λx, rfl) (λx, rfl) (by simp [trans_source])
lemma trans_refl_restr' (s : set β) :
e.trans ((local_equiv.refl β).restr s) = e.restr (e.source ∩ e ⁻¹' s) :=
local_equiv.ext (λx, rfl) (λx, rfl) $ by { simp [trans_source], rw [← inter_assoc, inter_self] }
lemma restr_trans (s : set α) :
(e.restr s).trans e' = (e.trans e').restr s :=
local_equiv.ext (λx, rfl) (λx, rfl) $ by { simp [trans_source, inter_comm], rwa inter_assoc }
/-- `eq_on_source e e'` means that `e` and `e'` have the same source, and coincide there. Then `e`
and `e'` should really be considered the same local equiv. -/
def eq_on_source (e e' : local_equiv α β) : Prop :=
e.source = e'.source ∧ (e.source.eq_on e e')
/-- `eq_on_source` is an equivalence relation -/
instance eq_on_source_setoid : setoid (local_equiv α β) :=
{ r := eq_on_source,
iseqv := ⟨
λe, by simp [eq_on_source],
λe e' h, by { simp [eq_on_source, h.1.symm], exact λx hx, (h.2 hx).symm },
λe e' e'' h h', ⟨by rwa [← h'.1, ← h.1], λx hx, by { rw [← h'.2, h.2 hx], rwa ← h.1 }⟩⟩ }
lemma eq_on_source_refl : e ≈ e := setoid.refl _
/-- Two equivalent local equivs have the same source -/
lemma eq_on_source.source_eq {e e' : local_equiv α β} (h : e ≈ e') : e.source = e'.source :=
h.1
/-- Two equivalent local equivs coincide on the source -/
lemma eq_on_source.eq_on {e e' : local_equiv α β} (h : e ≈ e') : e.source.eq_on e e' :=
h.2
/-- Two equivalent local equivs have the same target -/
lemma eq_on_source.target_eq {e e' : local_equiv α β} (h : e ≈ e') : e.target = e'.target :=
by simp only [← image_source_eq_target, ← h.source_eq, h.2.image_eq]
/-- If two local equivs are equivalent, so are their inverses. -/
lemma eq_on_source.symm' {e e' : local_equiv α β} (h : e ≈ e') : e.symm ≈ e'.symm :=
begin
refine ⟨h.target_eq, eq_on_of_left_inv_on_of_right_inv_on e.left_inv_on _ _⟩;
simp only [symm_source, h.target_eq, h.source_eq, e'.symm_maps_to],
exact e'.right_inv_on.congr_right e'.symm_maps_to (h.source_eq ▸ h.eq_on.symm),
end
/-- Two equivalent local equivs have coinciding inverses on the target -/
lemma eq_on_source.symm_eq_on {e e' : local_equiv α β} (h : e ≈ e') :
eq_on e.symm e'.symm e.target :=
h.symm'.eq_on
/-- Composition of local equivs respects equivalence -/
lemma eq_on_source.trans' {e e' : local_equiv α β} {f f' : local_equiv β γ}
(he : e ≈ e') (hf : f ≈ f') : e.trans f ≈ e'.trans f' :=
begin
split,
{ rw [trans_source'', trans_source'', ← he.target_eq, ← hf.1],
exact (he.symm'.eq_on.mono $ inter_subset_left _ _).image_eq },
{ assume x hx,
rw trans_source at hx,
simp [(he.2 hx.1).symm, hf.2 hx.2] }
end
/-- Restriction of local equivs respects equivalence -/
lemma eq_on_source.restr {e e' : local_equiv α β} (he : e ≈ e') (s : set α) :
e.restr s ≈ e'.restr s :=
begin
split,
{ simp [he.1] },
{ assume x hx,
simp only [mem_inter_eq, restr_source] at hx,
exact he.2 hx.1 }
end
/-- Preimages are respected by equivalence -/
lemma eq_on_source.source_inter_preimage_eq {e e' : local_equiv α β} (he : e ≈ e') (s : set β) :
e.source ∩ e ⁻¹' s = e'.source ∩ e' ⁻¹' s :=
by rw [he.eq_on.inter_preimage_eq, he.source_eq]
/-- Composition of a local equiv and its inverse is equivalent to the restriction of the identity
to the source -/
lemma trans_self_symm :
e.trans e.symm ≈ local_equiv.of_set e.source :=
begin
have A : (e.trans e.symm).source = e.source, by mfld_set_tac,
refine ⟨by simp [A], λx hx, _⟩,
rw A at hx,
simp only [hx] with mfld_simps
end
/-- Composition of the inverse of a local equiv and this local equiv is equivalent to the
restriction of the identity to the target -/
lemma trans_symm_self :
e.symm.trans e ≈ local_equiv.of_set e.target :=
trans_self_symm (e.symm)
/-- Two equivalent local equivs are equal when the source and target are univ -/
lemma eq_of_eq_on_source_univ (e e' : local_equiv α β) (h : e ≈ e')
(s : e.source = univ) (t : e.target = univ) : e = e' :=
begin
apply local_equiv.ext (λx, _) (λx, _) h.1,
{ apply h.2,
rw s,
exact mem_univ _ },
{ apply h.symm'.2,
rw [symm_source, t],
exact mem_univ _ }
end
section prod
/-- The product of two local equivs, as a local equiv on the product. -/
def prod (e : local_equiv α β) (e' : local_equiv γ δ) : local_equiv (α × γ) (β × δ) :=
{ source := set.prod e.source e'.source,
target := set.prod e.target e'.target,
to_fun := λp, (e p.1, e' p.2),
inv_fun := λp, (e.symm p.1, e'.symm p.2),
map_source' := λp hp, by { simp at hp, simp [hp] },
map_target' := λp hp, by { simp at hp, simp [map_target, hp] },
left_inv' := λp hp, by { simp at hp, simp [hp] },
right_inv' := λp hp, by { simp at hp, simp [hp] } }
@[simp, mfld_simps] lemma prod_source (e : local_equiv α β) (e' : local_equiv γ δ) :
(e.prod e').source = set.prod e.source e'.source := rfl
@[simp, mfld_simps] lemma prod_target (e : local_equiv α β) (e' : local_equiv γ δ) :
(e.prod e').target = set.prod e.target e'.target := rfl
@[simp, mfld_simps] lemma prod_coe (e : local_equiv α β) (e' : local_equiv γ δ) :
((e.prod e') : α × γ → β × δ) = (λp, (e p.1, e' p.2)) := rfl
lemma prod_coe_symm (e : local_equiv α β) (e' : local_equiv γ δ) :
((e.prod e').symm : β × δ → α × γ) = (λp, (e.symm p.1, e'.symm p.2)) := rfl
@[simp, mfld_simps] lemma prod_symm (e : local_equiv α β) (e' : local_equiv γ δ) :
(e.prod e').symm = (e.symm.prod e'.symm) :=
by ext x; simp [prod_coe_symm]
@[simp, mfld_simps] lemma prod_trans {η : Type*} {ε : Type*}
(e : local_equiv α β) (f : local_equiv β γ) (e' : local_equiv δ η) (f' : local_equiv η ε) :
(e.prod e').trans (f.prod f') = (e.trans f).prod (e'.trans f') :=
by ext x; simp [ext_iff]; tauto
end prod
section pi
variables {ι : Type*} {αi βi : ι → Type*} (ei : Π i, local_equiv (αi i) (βi i))
/-- The product of a family of local equivs, as a local equiv on the pi type. -/
@[simps source target] protected def pi : local_equiv (Π i, αi i) (Π i, βi i) :=
{ to_fun := λ f i, ei i (f i),
inv_fun := λ f i, (ei i).symm (f i),
source := pi univ (λ i, (ei i).source),
target := pi univ (λ i, (ei i).target),
map_source' := λ f hf i hi, (ei i).map_source (hf i hi),
map_target' := λ f hf i hi, (ei i).map_target (hf i hi),
left_inv' := λ f hf, funext $ λ i, (ei i).left_inv (hf i trivial),
right_inv' := λ f hf, funext $ λ i, (ei i).right_inv (hf i trivial) }
attribute [mfld_simps] pi_source pi_target
@[simp, mfld_simps] lemma pi_coe : ⇑(local_equiv.pi ei) = λ (f : Π i, αi i) i, ei i (f i) := rfl
@[simp, mfld_simps] lemma pi_symm :
(local_equiv.pi ei).symm = local_equiv.pi (λ i, (ei i).symm) := rfl
end pi
end local_equiv
namespace set
-- All arguments are explicit to avoid missing information in the pretty printer output
/-- A bijection between two sets `s : set α` and `t : set β` provides a local equivalence
between `α` and `β`. -/
@[simps] noncomputable def bij_on.to_local_equiv [nonempty α] (f : α → β) (s : set α) (t : set β)
(hf : bij_on f s t) :
local_equiv α β :=
{ to_fun := f,
inv_fun := inv_fun_on f s,
source := s,
target := t,
map_source' := hf.maps_to,
map_target' := hf.surj_on.maps_to_inv_fun_on,
left_inv' := hf.inv_on_inv_fun_on.1,
right_inv' := hf.inv_on_inv_fun_on.2 }
/-- A map injective on a subset of its domain provides a local equivalence. -/
@[simp, mfld_simps] noncomputable def inj_on.to_local_equiv [nonempty α] (f : α → β) (s : set α)
(hf : inj_on f s) :
local_equiv α β :=
hf.bij_on_image.to_local_equiv f s (f '' s)
end set
namespace equiv
/- equivs give rise to local_equiv. We set up simp lemmas to reduce most properties of the local
equiv to that of the equiv. -/
variables (e : equiv α β) (e' : equiv β γ)
@[simp, mfld_simps] lemma to_local_equiv_coe : (e.to_local_equiv : α → β) = e := rfl
@[simp, mfld_simps] lemma to_local_equiv_symm_coe : (e.to_local_equiv.symm : β → α) = e.symm := rfl
@[simp, mfld_simps] lemma to_local_equiv_source : e.to_local_equiv.source = univ := rfl
@[simp, mfld_simps] lemma to_local_equiv_target : e.to_local_equiv.target = univ := rfl
@[simp, mfld_simps] lemma refl_to_local_equiv :
(equiv.refl α).to_local_equiv = local_equiv.refl α := rfl
@[simp, mfld_simps] lemma symm_to_local_equiv : e.symm.to_local_equiv = e.to_local_equiv.symm := rfl
@[simp, mfld_simps] lemma trans_to_local_equiv :
(e.trans e').to_local_equiv = e.to_local_equiv.trans e'.to_local_equiv :=
local_equiv.ext (λx, rfl) (λx, rfl) (by simp [local_equiv.trans_source, equiv.to_local_equiv])
end equiv
|
416aa1a8e62e8ca1c3acc18c50aae30c4f1b43cb | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/category_theory/const.lean | 860a46738eba0c532f2b0769f54f42c16781cd01 | [
"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 | 2,672 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.functor_category
import category_theory.opposites
universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation
open category_theory
namespace category_theory.functor
variables (J : Type u₁) [𝒥 : category.{v₁} J]
variables {C : Type u₂} [𝒞 : category.{v₂} C]
include 𝒥 𝒞
def const : C ⥤ (J ⥤ C) :=
{ obj := λ X,
{ obj := λ j, X,
map := λ j j' f, 𝟙 X },
map := λ X Y f, { app := λ j, f } }
namespace const
open opposite
variables {J}
@[simp] lemma obj_obj (X : C) (j : J) : ((const J).obj X).obj j = X := rfl
@[simp] lemma obj_map (X : C) {j j' : J} (f : j ⟶ j') : ((const J).obj X).map f = 𝟙 X := rfl
@[simp] lemma map_app {X Y : C} (f : X ⟶ Y) (j : J) : ((const J).map f).app j = f := rfl
def op_obj_op (X : C) :
(const Jᵒᵖ).obj (op X) ≅ ((const J).obj X).op :=
{ hom := { app := λ j, 𝟙 _ },
inv := { app := λ j, 𝟙 _ } }
@[simp] lemma op_obj_op_hom_app (X : C) (j : Jᵒᵖ) : (op_obj_op X).hom.app j = 𝟙 _ := rfl
@[simp] lemma op_obj_op_inv_app (X : C) (j : Jᵒᵖ) : (op_obj_op X).inv.app j = 𝟙 _ := rfl
def op_obj_unop (X : Cᵒᵖ) :
(const Jᵒᵖ).obj (unop X) ≅ ((const J).obj X).left_op :=
{ hom := { app := λ j, 𝟙 _ },
inv := { app := λ j, 𝟙 _ } }
-- Lean needs some help with universes here.
@[simp] lemma op_obj_unop_hom_app (X : Cᵒᵖ) (j : Jᵒᵖ) : (op_obj_unop.{v₁ v₂} X).hom.app j = 𝟙 _ := rfl
@[simp] lemma op_obj_unop_inv_app (X : Cᵒᵖ) (j : Jᵒᵖ) : (op_obj_unop.{v₁ v₂} X).inv.app j = 𝟙 _ := rfl
@[simp] lemma unop_functor_op_obj_map (X : Cᵒᵖ) {j₁ j₂ : J} (f : j₁ ⟶ j₂) :
(unop ((functor.op (const J)).obj X)).map f = 𝟙 (unop X) := rfl
end const
section
variables {D : Type u₃} [𝒟 : category.{v₃} D]
include 𝒟
/-- These are actually equal, of course, but not definitionally equal
(the equality requires F.map (𝟙 _) = 𝟙 _). A natural isomorphism is
more convenient than an equality between functors (compare id_to_iso). -/
@[simp] def const_comp (X : C) (F : C ⥤ D) :
(const J).obj X ⋙ F ≅ (const J).obj (F.obj X) :=
{ hom := { app := λ _, 𝟙 _ },
inv := { app := λ _, 𝟙 _ } }
@[simp] lemma const_comp_hom_app (X : C) (F : C ⥤ D) (j : J) :
(const_comp J X F).hom.app j = 𝟙 _ := rfl
@[simp] lemma const_comp_inv_app (X : C) (F : C ⥤ D) (j : J) :
(const_comp J X F).inv.app j = 𝟙 _ := rfl
end
end category_theory.functor
|
7559c45cb0a4f1c3c3f549e72512d1acf4f4423c | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/tc_cache2.lean | eb94ff4ce781fab2701bf0c51ea9fd7e8fde4310 | [
"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 | 837 | lean | class succeeds_w_cache (α : Type) := (a : α)
class fails_quickly_w_cache (α : Type) extends succeeds_w_cache α
class loops_wo_cache (α : Type) := (a : α)
class has_no_inst (α : Type)
instance loops_wo_cache.loop {α} [loops_wo_cache α] [inhabited α] :
loops_wo_cache α :=
‹loops_wo_cache α›
instance inhabited.to_loops_wo_cache {α} [inhabited α] : loops_wo_cache α :=
{a := default}
instance loops_wo_cache.to_fails_quickly_w_cache {α} [has_no_inst α] [loops_wo_cache α] :
fails_quickly_w_cache α :=
{a := loops_wo_cache.a}
@[priority 1] instance inhabited.to_succeeds_w_cache {α} [inhabited α] :
succeeds_w_cache α :=
{a := default}
#check (by apply_instance : succeeds_w_cache ℕ)
open tactic
#eval do
x ← to_expr ``(succeeds_w_cache.a),
infer_type x >>= unify `(nat),
unify `(nat.zero) x
|
ac9f49cc51e821b2951710d9e3d4b6ccc9a9e00c | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/set/intervals/default_auto.lean | 10e8536bbe528d7df8bb12cec135c1b0755b8d96 | [] | 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 | 211 | lean | import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.set.intervals.disjoint
import Mathlib.data.set.intervals.unordered_interval
import Mathlib.PostPort
namespace Mathlib
end Mathlib |
5c38865b2af78f44dc84484af839c6898321cb1b | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/local_notation_bug2.lean | e356619108cb17cad4cbe655417bc22390df5bf5 | [
"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 | 149 | lean | open nat
section
parameters (b : ℕ)
definition add_b (n : ℕ) := n + b
local postfix `%%`:max := add_b
end
#reduce 5%% -- Error, unexpected token
|
1bcf7b62efa2c101934252bbabafab643de67ae6 | be5348f86d661459153802318209304b793c0e2a | /src/simple.lean | 0fff9982385f24784b70674fa3d9f9318ea30a8f | [] | no_license | reglayass/lean-avl | 6b758c7708bdb3316b1b97ada3e3f259b49da58a | c7bffa75d7548e5ff8cdd7d69f5a58499f883df1 | refs/heads/master | 1,692,297,536,477 | 1,633,946,864,000 | 1,633,946,864,000 | 340,881,572 | 4 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,833 | lean | import definitions tactic.linarith tactic.omega
set_option pp.generalized_field_notation false
universe u
open btree
variables {α : Type u}
/- # Simple reflexive lemmas # -/
/- If we lookup empty btree then return none -/
lemma lookup_empty (k : nat) :
lookup k (@empty_tree α) = none := by refl
/- If you check if a key is bound on an empty tree, bound will return false -/
lemma bound_empty (k : nat) :
bound k (@empty_tree α) = ff := by refl
/- If you insert into an empty tree, then you just have one node -/
lemma insert_empty (k : nat) (v : α) :
insert k v (@empty_tree α) = btree.node btree.empty k v btree.empty := by refl
/- # Bound and lookup correctness # -/
/- If bound returns false, then the key is not in the tree therefore
The lookup will return none -/
lemma bound_false (k : nat) (t : btree α) :
bound k t = ff → lookup k t = none :=
begin
intro h₁,
induction t,
case empty {
apply refl,
},
case node : tl tk ta tr ihl ihr {
simp [lookup],
simp [bound] at h₁,
by_cases c₁ : (k < tk),
{ simp only [if_pos c₁],
apply ihl,
apply and.left (and.right h₁),
},
{ simp only [if_neg c₁],
by_cases c₂ : (tk < k),
{ simp only [if_pos c₂],
apply ihr,
apply and.right (and.right h₁),
},
{ simp only [if_neg c₂],
cases_matching* (_ ∧ _),
have h : k = tk := by linarith,
have h₂ : k ≠ tk := by omega,
contradiction,
},
},
},
end
/- If a key is bound to a tree, then lookup will in result in some result -/
lemma bound_lookup (t : btree α) (k : nat) :
ordered t → bound k t = tt → ∃ (v : α), lookup k t = some v :=
begin
intros h₁ h₂,
induction t,
case empty {
simp [bound] at h₂,
contradiction,
},
case node : tl tk ta tr ihl ihr {
rw ordered at h₁,
cases_matching* (_ ∧ _),
simp only [lookup],
simp [bound] at h₂,
by_cases c₁ : (k < tk),
{ simp only [if_pos c₁],
apply ihl,
{ assumption, },
{ cases_matching* (_ ∨ _),
{ exfalso, linarith, },
{ assumption, },
{ unfold forall_keys at h₁_right_right_right,
specialize h₁_right_right_right k h₂,
exfalso, linarith,
},
},
},
{ simp only [if_neg c₁],
by_cases c₂ : (k > tk),
{ simp only [if_pos c₂],
apply ihr,
{ assumption, },
{ cases_matching* (_ ∨ _),
{ exfalso, linarith, },
{ unfold forall_keys at h₁_right_right_left,
specialize h₁_right_right_left k h₂,
exfalso, linarith,
},
{ assumption, },
},
},
{ simp only [if_neg c₂],
existsi ta,
refl,
},
},
},
end |
0a24e3cfda6c4f4bf44a5043e7c7c409226889e5 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/rel_auto.lean | c504b6d7eda9a0d1130b5940894aeaffa91b3c54 | [] | 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 | 8,488 | lean | /-
Copyright (c) 2018 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
Operations on set-valued functions, aka partial multifunctions, aka relations.
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.set.lattice
import Mathlib.PostPort
universes u_1 u_2 u_3 u_4
namespace Mathlib
/-- A relation on `α` and `β`, aka a set-valued function, aka a partial multifunction --/
def rel (α : Type u_1) (β : Type u_2) := α → β → Prop
namespace rel
/-- The inverse relation : `r.inv x y ↔ r y x`. Note that this is *not* a groupoid inverse. -/
def inv {α : Type u_1} {β : Type u_2} (r : rel α β) : rel β α := flip r
theorem inv_def {α : Type u_1} {β : Type u_2} (r : rel α β) (x : α) (y : β) : inv r y x ↔ r x y :=
iff.rfl
theorem inv_inv {α : Type u_1} {β : Type u_2} (r : rel α β) : inv (inv r) = r :=
funext fun (x : α) => funext fun (y : β) => propext (iff.refl (inv (inv r) x y))
/-- Domain of a relation -/
def dom {α : Type u_1} {β : Type u_2} (r : rel α β) : set α :=
set_of fun (x : α) => ∃ (y : β), r x y
/-- Codomain aka range of a relation-/
def codom {α : Type u_1} {β : Type u_2} (r : rel α β) : set β :=
set_of fun (y : β) => ∃ (x : α), r x y
theorem codom_inv {α : Type u_1} {β : Type u_2} (r : rel α β) : codom (inv r) = dom r :=
set.ext fun (x : α) => iff.refl (x ∈ codom (inv r))
theorem dom_inv {α : Type u_1} {β : Type u_2} (r : rel α β) : dom (inv r) = codom r :=
set.ext fun (x : β) => iff.refl (x ∈ dom (inv r))
/-- Composition of relation; note that it follows the `category_theory/` order of arguments. -/
def comp {α : Type u_1} {β : Type u_2} {γ : Type u_3} (r : rel α β) (s : rel β γ) : rel α γ :=
fun (x : α) (z : γ) => ∃ (y : β), r x y ∧ s y z
theorem comp_assoc {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} (r : rel α β)
(s : rel β γ) (t : rel γ δ) : comp (comp r s) t = comp r (comp s t) :=
sorry
@[simp] theorem comp_right_id {α : Type u_1} {β : Type u_2} (r : rel α β) : comp r Eq = r := sorry
@[simp] theorem comp_left_id {α : Type u_1} {β : Type u_2} (r : rel α β) : comp Eq r = r := sorry
theorem inv_id {α : Type u_1} : inv Eq = Eq :=
funext fun (x : α) => funext fun (y : α) => propext { mp := Eq.symm, mpr := Eq.symm }
theorem inv_comp {α : Type u_1} {β : Type u_2} {γ : Type u_3} (r : rel α β) (s : rel β γ) :
inv (comp r s) = comp (inv s) (inv r) :=
sorry
/-- Image of a set under a relation -/
def image {α : Type u_1} {β : Type u_2} (r : rel α β) (s : set α) : set β :=
set_of fun (y : β) => ∃ (x : α), ∃ (H : x ∈ s), r x y
theorem mem_image {α : Type u_1} {β : Type u_2} (r : rel α β) (y : β) (s : set α) :
y ∈ image r s ↔ ∃ (x : α), ∃ (H : x ∈ s), r x y :=
iff.rfl
theorem image_subset {α : Type u_1} {β : Type u_2} (r : rel α β) :
relator.lift_fun has_subset.subset has_subset.subset (image r) (image r) :=
sorry
theorem image_mono {α : Type u_1} {β : Type u_2} (r : rel α β) : monotone (image r) :=
image_subset r
theorem image_inter {α : Type u_1} {β : Type u_2} (r : rel α β) (s : set α) (t : set α) :
image r (s ∩ t) ⊆ image r s ∩ image r t :=
monotone.map_inf_le (image_mono r) s t
theorem image_union {α : Type u_1} {β : Type u_2} (r : rel α β) (s : set α) (t : set α) :
image r (s ∪ t) = image r s ∪ image r t :=
sorry
@[simp] theorem image_id {α : Type u_1} (s : set α) : image Eq s = s := sorry
theorem image_comp {α : Type u_1} {β : Type u_2} {γ : Type u_3} (r : rel α β) (s : rel β γ)
(t : set α) : image (comp r s) t = image s (image r t) :=
sorry
theorem image_univ {α : Type u_1} {β : Type u_2} (r : rel α β) : image r set.univ = codom r := sorry
/-- Preimage of a set under a relation `r`. Same as the image of `s` under `r.inv` -/
def preimage {α : Type u_1} {β : Type u_2} (r : rel α β) (s : set β) : set α := image (inv r) s
theorem mem_preimage {α : Type u_1} {β : Type u_2} (r : rel α β) (x : α) (s : set β) :
x ∈ preimage r s ↔ ∃ (y : β), ∃ (H : y ∈ s), r x y :=
iff.rfl
theorem preimage_def {α : Type u_1} {β : Type u_2} (r : rel α β) (s : set β) :
preimage r s = set_of fun (x : α) => ∃ (y : β), ∃ (H : y ∈ s), r x y :=
set.ext fun (x : α) => mem_preimage r x s
theorem preimage_mono {α : Type u_1} {β : Type u_2} (r : rel α β) {s : set β} {t : set β}
(h : s ⊆ t) : preimage r s ⊆ preimage r t :=
image_mono (inv r) h
theorem preimage_inter {α : Type u_1} {β : Type u_2} (r : rel α β) (s : set β) (t : set β) :
preimage r (s ∩ t) ⊆ preimage r s ∩ preimage r t :=
image_inter (inv r) s t
theorem preimage_union {α : Type u_1} {β : Type u_2} (r : rel α β) (s : set β) (t : set β) :
preimage r (s ∪ t) = preimage r s ∪ preimage r t :=
image_union (inv r) s t
theorem preimage_id {α : Type u_1} (s : set α) : preimage Eq s = s := sorry
theorem preimage_comp {α : Type u_1} {β : Type u_2} {γ : Type u_3} (r : rel α β) (s : rel β γ)
(t : set γ) : preimage (comp r s) t = preimage r (preimage s t) :=
sorry
theorem preimage_univ {α : Type u_1} {β : Type u_2} (r : rel α β) : preimage r set.univ = dom r :=
eq.mpr
(id (Eq._oldrec (Eq.refl (preimage r set.univ = dom r)) (preimage.equations._eqn_1 r set.univ)))
(eq.mpr (id (Eq._oldrec (Eq.refl (image (inv r) set.univ = dom r)) (image_univ (inv r))))
(eq.mpr (id (Eq._oldrec (Eq.refl (codom (inv r) = dom r)) (codom_inv r))) (Eq.refl (dom r))))
/-- Core of a set `s : set β` w.r.t `r : rel α β` is the set of `x : α` that are related *only*
to elements of `s`. -/
def core {α : Type u_1} {β : Type u_2} (r : rel α β) (s : set β) : set α :=
set_of fun (x : α) => ∀ (y : β), r x y → y ∈ s
theorem mem_core {α : Type u_1} {β : Type u_2} (r : rel α β) (x : α) (s : set β) :
x ∈ core r s ↔ ∀ (y : β), r x y → y ∈ s :=
iff.rfl
theorem core_subset {α : Type u_1} {β : Type u_2} (r : rel α β) :
relator.lift_fun has_subset.subset has_subset.subset (core r) (core r) :=
fun (s t : set β) (h : s ⊆ t) (x : α) (h' : x ∈ core r s) (y : β) (rxy : r x y) => h (h' y rxy)
theorem core_mono {α : Type u_1} {β : Type u_2} (r : rel α β) : monotone (core r) := core_subset r
theorem core_inter {α : Type u_1} {β : Type u_2} (r : rel α β) (s : set β) (t : set β) :
core r (s ∩ t) = core r s ∩ core r t :=
sorry
theorem core_union {α : Type u_1} {β : Type u_2} (r : rel α β) (s : set β) (t : set β) :
core r s ∪ core r t ⊆ core r (s ∪ t) :=
monotone.le_map_sup (core_mono r) s t
@[simp] theorem core_univ {α : Type u_1} {β : Type u_2} (r : rel α β) :
core r set.univ = set.univ :=
sorry
theorem core_id {α : Type u_1} (s : set α) : core Eq s = s := sorry
theorem core_comp {α : Type u_1} {β : Type u_2} {γ : Type u_3} (r : rel α β) (s : rel β γ)
(t : set γ) : core (comp r s) t = core r (core s t) :=
sorry
/-- Restrict the domain of a relation to a subtype. -/
def restrict_domain {α : Type u_1} {β : Type u_2} (r : rel α β) (s : set α) :
rel (Subtype fun (x : α) => x ∈ s) β :=
fun (x : Subtype fun (x : α) => x ∈ s) (y : β) => r (subtype.val x) y
theorem image_subset_iff {α : Type u_1} {β : Type u_2} (r : rel α β) (s : set α) (t : set β) :
image r s ⊆ t ↔ s ⊆ core r t :=
sorry
theorem core_preimage_gc {α : Type u_1} {β : Type u_2} (r : rel α β) :
galois_connection (image r) (core r) :=
image_subset_iff r
end rel
namespace function
/-- The graph of a function as a relation. -/
def graph {α : Type u_1} {β : Type u_2} (f : α → β) : rel α β := fun (x : α) (y : β) => f x = y
end function
namespace set
-- TODO: if image were defined with bounded quantification in corelib, the next two would
-- be definitional
theorem image_eq {α : Type u_1} {β : Type u_2} (f : α → β) (s : set α) :
f '' s = rel.image (function.graph f) s :=
sorry
theorem preimage_eq {α : Type u_1} {β : Type u_2} (f : α → β) (s : set β) :
f ⁻¹' s = rel.preimage (function.graph f) s :=
sorry
theorem preimage_eq_core {α : Type u_1} {β : Type u_2} (f : α → β) (s : set β) :
f ⁻¹' s = rel.core (function.graph f) s :=
sorry
end Mathlib |
3d0e635b6ae1e3dbc7522111656135674dbb28a5 | fe84e287c662151bb313504482b218a503b972f3 | /src/exercises/hello_world.lean | c740a17934ccd2e3167fdab5c7bbcdc5adb548d3 | [] | no_license | NeilStrickland/lean_lib | 91e163f514b829c42fe75636407138b5c75cba83 | 6a9563de93748ace509d9db4302db6cd77d8f92c | refs/heads/master | 1,653,408,198,261 | 1,652,996,419,000 | 1,652,996,419,000 | 181,006,067 | 4 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 1,519 | lean | theorem one_plus_one : 1 + 1 ≤ 2 :=
/-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This line states a theorem. Note that we need a colon between the
name of the theorem, and a := between the statement and the proof.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/
begin
refl,
end
/-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is the body of the proof.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/
theorem one_plus_one_alt : 1 + 1 = 2 := rfl
/-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The above proof was written in tactic mode, as indicated by
the keywords `begin` and `end`. This mode is most appropriate
for long proofs with complex logical flow and many local
definitions, which is of course not the case here. It would
be more efficient to use term mode, as shown here.
Note that the previous proof uses the word `refl`, which is
the name of a proof tactic. The current proof uses the word
`rfl`, which is the name of the fact that `x = x` for all `x`.
The word `rfl` counts as a proof here, because we can just
unwind the definitions of `1`, `2` and `+` and then the two
sides of our equation become identical. The proof tactic
called `refl` just applies the fact called `rfl` together with
some similar facts (such as the fact that `n ≤ n` for all
natural numbers `n`).
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-/
|
514c195a2ad8fde0c9584953deacdf3c48d56f2e | d1bbf1801b3dcb214451d48214589f511061da63 | /src/field_theory/normal.lean | 799e05c173f671f2d0e39e36d6817fd03211c43c | [
"Apache-2.0"
] | permissive | cheraghchi/mathlib | 5c366f8c4f8e66973b60c37881889da8390cab86 | f29d1c3038422168fbbdb2526abf7c0ff13e86db | refs/heads/master | 1,676,577,831,283 | 1,610,894,638,000 | 1,610,894,638,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,574 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import field_theory.minpoly
import field_theory.splitting_field
import linear_algebra.finite_dimensional
/-!
# Normal field extensions
In this file we define normal field extensions and prove that for a finite extension, being normal
is the same as being a splitting field (TODO).
## Main Definitions
- `normal F K` where `K` is a field extension of `F`.
-/
noncomputable theory
open_locale classical
open polynomial
universes u v
variables (F : Type u) (K : Type v) [field F] [field K] [algebra F K]
/-- Typeclass for normal field extension: `K` is a normal extension of `F` iff the minimal
polynomial of every element `x` in `K` splits in `K`, i.e. every conjugate of `x` is in `K`. -/
@[class] def normal : Prop :=
∀ x : K, ∃ H : is_integral F x, splits (algebra_map F K) (minpoly H)
instance normal_self : normal F F :=
λ x, ⟨is_integral_algebra_map, by { rw minpoly.eq_X_sub_C, exact splits_X_sub_C _ }⟩
theorem normal.is_integral [h : normal F K] (x : K) : is_integral F x := (h x).fst
theorem normal.splits [h : normal F K] (x : K) :
splits (algebra_map F K) (minpoly $ normal.is_integral F K x) := (h x).snd
theorem normal.exists_is_splitting_field [normal F K] [finite_dimensional F K] :
∃ p : polynomial F, is_splitting_field F K p :=
begin
obtain ⟨s, hs⟩ := finite_dimensional.exists_is_basis_finset F K,
refine ⟨s.prod $ λ x, minpoly $ normal.is_integral F K x,
splits_prod _ $ λ x hx, normal.splits F K x,
subalgebra.to_submodule_injective _⟩,
rw [algebra.coe_top, eq_top_iff, ← hs.2, submodule.span_le, set.range_subset_iff],
refine λ x, algebra.subset_adjoin (multiset.mem_to_finset.mpr $
(mem_roots $ mt (map_eq_zero $ algebra_map F K).1 $
finset.prod_ne_zero_iff.2 $ λ x hx, _).2 _),
{ exact minpoly.ne_zero _ },
rw [is_root.def, eval_map, ← aeval_def, alg_hom.map_prod],
exact finset.prod_eq_zero x.2 (minpoly.aeval _)
end
section normal_tower
variables (E : Type*) [field E] [algebra F E] [algebra K E] [is_scalar_tower F K E]
lemma normal.tower_top_of_normal (h : normal F E) : normal K E :=
begin
intros x,
cases h x with hx hhx,
rw is_scalar_tower.algebra_map_eq F K E at hhx,
exact ⟨is_integral_of_is_scalar_tower x hx, polynomial.splits_of_splits_of_dvd (algebra_map K E)
(polynomial.map_ne_zero (minpoly.ne_zero hx))
((polynomial.splits_map_iff (algebra_map F K) (algebra_map K E)).mpr hhx)
(minpoly.dvd_map_of_is_scalar_tower K hx)⟩,
end
variables {F} {E} {E' : Type*} [field E'] [algebra F E']
lemma normal.of_alg_equiv [h : normal F E] (f : E ≃ₐ[F] E') : normal F E' :=
begin
intro x,
cases h (f.symm x) with hx hhx,
have H := is_integral_alg_hom f.to_alg_hom hx,
rw [alg_equiv.to_alg_hom_eq_coe, alg_equiv.coe_alg_hom, alg_equiv.apply_symm_apply] at H,
use H,
apply polynomial.splits_of_splits_of_dvd (algebra_map F E') (minpoly.ne_zero hx),
{ rw ← alg_hom.comp_algebra_map f.to_alg_hom,
exact polynomial.splits_comp_of_splits (algebra_map F E) f.to_alg_hom.to_ring_hom hhx },
{ apply minpoly.dvd H,
rw ← add_equiv.map_eq_zero_iff f.symm.to_add_equiv,
exact eq.trans (polynomial.aeval_alg_hom_apply f.symm.to_alg_hom x
(minpoly hx)).symm (minpoly.aeval hx) },
end
lemma alg_equiv.transfer_normal (f : E ≃ₐ[F] E') : normal F E ↔ normal F E' :=
⟨λ h, by exactI normal.of_alg_equiv f, λ h, by exactI normal.of_alg_equiv f.symm⟩
end normal_tower
|
401827dec0212b5ad08c1348c8ad459a959bdf70 | f57749ca63d6416f807b770f67559503fdb21001 | /library/data/real/basic.lean | f438912ec5115cf2f97865113f2cb2190b399e92 | [
"Apache-2.0"
] | permissive | aliassaf/lean | bd54e85bed07b1ff6f01396551867b2677cbc6ac | f9b069b6a50756588b309b3d716c447004203152 | refs/heads/master | 1,610,982,152,948 | 1,438,916,029,000 | 1,438,916,029,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 38,324 | lean | /-
Copyright (c) 2015 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Robert Y. Lewis
The real numbers, constructed as equivalence classes of Cauchy sequences of rationals.
This construction follows Bishop and Bridges (1985).
To do:
o Rename things and possibly make theorems private
-/
import data.nat data.rat.order data.pnat
open nat eq eq.ops pnat
open -[coercions] rat
local notation 0 := rat.of_num 0
local notation 1 := rat.of_num 1
----------------------------------------------------------------------------------------------------
-- small helper lemmas
theorem s_mul_assoc_lemma_3 (a b n : ℕ+) (p : ℚ) :
p * ((a * n)⁻¹ + (b * n)⁻¹) = p * (a⁻¹ + b⁻¹) * n⁻¹ :=
by rewrite [rat.mul.assoc, rat.right_distrib, *inv_mul_eq_mul_inv]
theorem s_mul_assoc_lemma_4 {n : ℕ+} {ε q : ℚ} (Hε : ε > 0) (Hq : q > 0) (H : n ≥ pceil (q / ε)) :
q * n⁻¹ ≤ ε :=
begin
let H2 := pceil_helper H (pos_div_of_pos_of_pos Hq Hε),
let H3 := mul_le_of_le_div (pos_div_of_pos_of_pos Hq Hε) H2,
rewrite -(one_mul ε),
apply mul_le_mul_of_mul_div_le,
repeat assumption
end
theorem find_thirds (a b : ℚ) (H : b > 0) : ∃ n : ℕ+, a + n⁻¹ + n⁻¹ + n⁻¹ < a + b :=
let n := pceil (of_nat 4 / b) in
have of_nat 3 * n⁻¹ < b, from calc
of_nat 3 * n⁻¹ < of_nat 4 * n⁻¹
: rat.mul_lt_mul_of_pos_right dec_trivial !inv_pos
... ≤ of_nat 4 * (b / of_nat 4)
: rat.mul_le_mul_of_nonneg_left (!inv_pceil_div dec_trivial H) !of_nat_nonneg
... = b / of_nat 4 * of_nat 4 : rat.mul.comm
... = b : rat.div_mul_cancel dec_trivial,
exists.intro n (calc
a + n⁻¹ + n⁻¹ + n⁻¹ = a + (1 + 1 + 1) * n⁻¹ : by rewrite[*rat.right_distrib,*rat.one_mul,-*rat.add.assoc]
... = a + of_nat 3 * n⁻¹ : {show 1+1+1=of_nat 3, from dec_trivial}
... < a + b : rat.add_lt_add_left this a)
theorem squeeze {a b : ℚ} (H : ∀ j : ℕ+, a ≤ b + j⁻¹ + j⁻¹ + j⁻¹) : a ≤ b :=
begin
apply rat.le_of_not_gt,
intro Hb,
apply (exists.elim (find_midpoint Hb)),
intro c Hc,
apply (exists.elim (find_thirds b c (and.right Hc))),
intro j Hbj,
have Ha : a > b + j⁻¹ + j⁻¹ + j⁻¹, from lt.trans Hbj (and.left Hc),
exact absurd !H (not_le_of_gt Ha)
end
theorem squeeze_2 {a b : ℚ} (H : ∀ ε : ℚ, ε > 0 → a ≥ b - ε) : a ≥ b :=
begin
apply rat.le_of_not_gt,
intro Hb,
apply (exists.elim (find_midpoint Hb)),
intro c Hc,
let Hc' := H c (and.right Hc),
apply (rat.not_le_of_gt (and.left Hc)) (iff.mpr !le_add_iff_sub_right_le Hc')
end
theorem rewrite_helper (a b c d : ℚ) : a * b - c * d = a * (b - d) + (a - c) * d :=
by rewrite[rat.mul_sub_left_distrib, rat.mul_sub_right_distrib, add_sub, rat.sub_add_cancel]
theorem rewrite_helper3 (a b c d e f g: ℚ) : a * (b + c) - (d * e + f * g) =
(a * b - d * e) + (a * c - f * g) :=
by rewrite[rat.mul.left_distrib, add_sub_comm]
theorem rewrite_helper4 (a b c d : ℚ) : a * b - c * d = (a * b - a * d) + (a * d - c * d) :=
by rewrite[add_sub, rat.sub_add_cancel]
theorem rewrite_helper5 (a b x y : ℚ) : a - b = (a - x) + (x - y) + (y - b) :=
by rewrite[*add_sub, *rat.sub_add_cancel]
theorem rewrite_helper7 (a b c d x : ℚ) :
a * b * c - d = (b * c) * (a - x) + (x * b * c - d) :=
by rewrite[rat.mul_sub_left_distrib, add_sub]; exact (calc
a * b * c - d = a * b * c - x * b * c + x * b * c - d : rat.sub_add_cancel
... = b * c * a - b * c * x + x * b * c - d :
have ∀ {a b c : ℚ}, a * b * c = b * c * a, from
λa b c, !rat.mul.comm ▸ !rat.mul.right_comm,
this ▸ this ▸ rfl)
theorem ineq_helper (a b : ℚ) (k m n : ℕ+) (H : a ≤ (k * 2 * m)⁻¹ + (k * 2 * n)⁻¹)
(H2 : b ≤ (k * 2 * m)⁻¹ + (k * 2 * n)⁻¹) :
(rat_of_pnat k) * a + b * (rat_of_pnat k) ≤ m⁻¹ + n⁻¹ :=
have (k * 2 * m)⁻¹ + (k * 2 * n)⁻¹ = (2 * k)⁻¹ * (m⁻¹ + n⁻¹),
by rewrite[rat.mul.left_distrib,*inv_mul_eq_mul_inv]; exact !rat.mul.comm ▸ rfl,
have a + b ≤ k⁻¹ * (m⁻¹ + n⁻¹), from calc
a + b ≤ (2 * k)⁻¹ * (m⁻¹ + n⁻¹) + (2 * k)⁻¹ * (m⁻¹ + n⁻¹) : rat.add_le_add (this ▸ H) (this ▸ H2)
... = ((2 * k)⁻¹ + (2 * k)⁻¹) * (m⁻¹ + n⁻¹) : rat.mul.right_distrib
... = k⁻¹ * (m⁻¹ + n⁻¹) : (pnat.add_halves k) ▸ rfl,
calc (rat_of_pnat k) * a + b * (rat_of_pnat k)
= (rat_of_pnat k) * a + (rat_of_pnat k) * b : rat.mul.comm
... = (rat_of_pnat k) * (a + b) : rat.left_distrib
... ≤ (rat_of_pnat k) * (k⁻¹ * (m⁻¹ + n⁻¹)) :
iff.mp (!rat.le_iff_mul_le_mul_left !rat_of_pnat_is_pos) this
... = m⁻¹ + n⁻¹ : by rewrite[-rat.mul.assoc, inv_cancel_left, rat.one_mul]
theorem factor_lemma (a b c d e : ℚ) : abs (a + b + c - (d + (b + e))) = abs ((a - d) + (c - e)) :=
!congr_arg (calc
a + b + c - (d + (b + e)) = a + b + c - (d + b + e) : rat.add.assoc
... = a + b - (d + b) + (c - e) : add_sub_comm
... = a + b - b - d + (c - e) : sub_add_eq_sub_sub_swap
... = a - d + (c - e) : rat.add_sub_cancel)
theorem factor_lemma_2 (a b c d : ℚ) : (a + b) + (c + d) = (a + c) + (d + b) :=
!rat.add.comm ▸ (binary.comm4 rat.add.comm rat.add.assoc a b c d)
--------------------------------------
-- define cauchy sequences and equivalence. show equivalence actually is one
namespace s
notation `seq` := ℕ+ → ℚ
definition regular (s : seq) := ∀ m n : ℕ+, abs (s m - s n) ≤ m⁻¹ + n⁻¹
definition equiv (s t : seq) := ∀ n : ℕ+, abs (s n - t n) ≤ n⁻¹ + n⁻¹
infix `≡` := equiv
theorem equiv.refl (s : seq) : s ≡ s :=
begin
intros,
rewrite [rat.sub_self, abs_zero],
apply add_invs_nonneg
end
theorem equiv.symm (s t : seq) (H : s ≡ t) : t ≡ s :=
begin
intros,
rewrite [-abs_neg, neg_sub],
exact H n
end
theorem bdd_of_eq {s t : seq} (H : s ≡ t) :
∀ j : ℕ+, ∀ n : ℕ+, n ≥ 2 * j → abs (s n - t n) ≤ j⁻¹ :=
begin
intros [j, n, Hn],
apply rat.le.trans,
apply H n,
rewrite -(add_halves j),
apply rat.add_le_add,
apply inv_ge_of_le Hn,
apply inv_ge_of_le Hn
end
theorem eq_of_bdd {s t : seq} (Hs : regular s) (Ht : regular t)
(H : ∀ j : ℕ+, ∃ Nj : ℕ+, ∀ n : ℕ+, Nj ≤ n → abs (s n - t n) ≤ j⁻¹) : s ≡ t :=
begin
intros,
have Hj : (∀ j : ℕ+, abs (s n - t n) ≤ n⁻¹ + n⁻¹ + j⁻¹ + j⁻¹ + j⁻¹), begin
intros,
cases H j with [Nj, HNj],
rewrite [-(rat.sub_add_cancel (s n) (s (max j Nj))), rat.add.assoc (s n + -s (max j Nj)),
↑regular at *],
apply rat.le.trans,
apply abs_add_le_abs_add_abs,
apply rat.le.trans,
apply rat.add_le_add,
apply Hs,
rewrite [-(rat.sub_add_cancel (s (max j Nj)) (t (max j Nj))), rat.add.assoc],
apply abs_add_le_abs_add_abs,
apply rat.le.trans,
apply rat.add_le_add_left,
apply rat.add_le_add,
apply HNj (max j Nj) (max_right j Nj),
apply Ht,
have hsimp : ∀ m : ℕ+, n⁻¹ + m⁻¹ + (j⁻¹ + (m⁻¹ + n⁻¹)) = n⁻¹ + n⁻¹ + j⁻¹ + (m⁻¹ + m⁻¹),
from λm, calc
n⁻¹ + m⁻¹ + (j⁻¹ + (m⁻¹ + n⁻¹)) = n⁻¹ + (j⁻¹ + (m⁻¹ + n⁻¹)) + m⁻¹ : rat.add.right_comm
... = n⁻¹ + (j⁻¹ + m⁻¹ + n⁻¹) + m⁻¹ : rat.add.assoc
... = n⁻¹ + (n⁻¹ + (j⁻¹ + m⁻¹)) + m⁻¹ : rat.add.comm
... = n⁻¹ + n⁻¹ + j⁻¹ + (m⁻¹ + m⁻¹) : by rewrite[-*rat.add.assoc],
rewrite hsimp,
have Hms : (max j Nj)⁻¹ + (max j Nj)⁻¹ ≤ j⁻¹ + j⁻¹, begin
apply rat.add_le_add,
apply inv_ge_of_le (max_left j Nj),
apply inv_ge_of_le (max_left j Nj),
end,
apply (calc
n⁻¹ + n⁻¹ + j⁻¹ + ((max j Nj)⁻¹ + (max j Nj)⁻¹) ≤ n⁻¹ + n⁻¹ + j⁻¹ + (j⁻¹ + j⁻¹) :
rat.add_le_add_left Hms
... = n⁻¹ + n⁻¹ + j⁻¹ + j⁻¹ + j⁻¹ : by rewrite *rat.add.assoc)
end,
apply squeeze Hj
end
theorem eq_of_bdd_var {s t : seq} (Hs : regular s) (Ht : regular t)
(H : ∀ ε : ℚ, ε > 0 → ∃ Nj : ℕ+, ∀ n : ℕ+, Nj ≤ n → abs (s n - t n) ≤ ε) : s ≡ t :=
begin
apply eq_of_bdd,
repeat assumption,
intros,
apply H j⁻¹,
apply inv_pos
end
theorem pnat_bound {ε : ℚ} (Hε : ε > 0) : ∃ p : ℕ+, p⁻¹ ≤ ε :=
begin
existsi (pceil (1 / ε)),
rewrite -(rat.div_div (rat.ne_of_gt Hε)) at {2},
apply pceil_helper,
apply le.refl,
apply div_pos_of_pos Hε
end
theorem bdd_of_eq_var {s t : seq} (Hs : regular s) (Ht : regular t) (Heq : s ≡ t) :
∀ ε : ℚ, ε > 0 → ∃ Nj : ℕ+, ∀ n : ℕ+, Nj ≤ n → abs (s n - t n) ≤ ε :=
begin
intro ε Hε,
cases pnat_bound Hε with [N, HN],
existsi 2 * N,
intro n Hn,
apply rat.le.trans,
apply bdd_of_eq Heq N n Hn,
assumption
end
theorem equiv.trans (s t u : seq) (Hs : regular s) (Ht : regular t) (Hu : regular u)
(H : s ≡ t) (H2 : t ≡ u) : s ≡ u :=
begin
apply eq_of_bdd Hs Hu,
intros,
existsi 2 * (2 * j),
intro n Hn,
rewrite [-rat.sub_add_cancel (s n) (t n), rat.add.assoc],
apply rat.le.trans,
apply abs_add_le_abs_add_abs,
have Hst : abs (s n - t n) ≤ (2 * j)⁻¹, from bdd_of_eq H _ _ Hn,
have Htu : abs (t n - u n) ≤ (2 * j)⁻¹, from bdd_of_eq H2 _ _ Hn,
rewrite -(add_halves j),
apply rat.add_le_add,
repeat assumption
end
-----------------------------------
-- define operations on cauchy sequences. show operations preserve regularity
definition K (s : seq) : ℕ+ := pnat.pos (ubound (abs (s pone)) + 1 + 1) dec_trivial
theorem canon_bound {s : seq} (Hs : regular s) (n : ℕ+) : abs (s n) ≤ rat_of_pnat (K s) :=
calc
abs (s n) = abs (s n - s pone + s pone) : by rewrite rat.sub_add_cancel
... ≤ abs (s n - s pone) + abs (s pone) : abs_add_le_abs_add_abs
... ≤ n⁻¹ + pone⁻¹ + abs (s pone) : rat.add_le_add_right !Hs
... = n⁻¹ + (1 + abs (s pone)) : by rewrite [pone_inv, rat.add.assoc]
... ≤ 1 + (1 + abs (s pone)) : rat.add_le_add_right (inv_le_one n)
... = abs (s pone) + (1 + 1) :
by rewrite [add.comm 1 (abs (s pone)), rat.add.comm 1, rat.add.assoc]
... ≤ of_nat (ubound (abs (s pone))) + (1 + 1) : rat.add_le_add_right (!ubound_ge)
... = of_nat (ubound (abs (s pone)) + (1 + 1)) : of_nat_add
... = of_nat (ubound (abs (s pone)) + 1 + 1) : nat.add.assoc
theorem bdd_of_regular {s : seq} (H : regular s) : ∃ b : ℚ, ∀ n : ℕ+, s n ≤ b :=
begin
existsi rat_of_pnat (K s),
intros,
apply rat.le.trans,
apply le_abs_self,
apply canon_bound H
end
theorem bdd_of_regular_strict {s : seq} (H : regular s) : ∃ b : ℚ, ∀ n : ℕ+, s n < b :=
begin
cases bdd_of_regular H with [b, Hb],
existsi b + 1,
intro n,
apply rat.lt_of_le_of_lt,
apply Hb,
apply rat.lt_add_of_pos_right,
apply rat.zero_lt_one
end
definition K₂ (s t : seq) := max (K s) (K t)
theorem K₂_symm (s t : seq) : K₂ s t = K₂ t s :=
if H : K s < K t then
(assert H1 : K₂ s t = K t, from max_eq_right H,
assert H2 : K₂ t s = K t, from max_eq_left (not_lt_of_ge (le_of_lt H)),
by rewrite [H1, -H2])
else
(assert H1 : K₂ s t = K s, from max_eq_left H,
if J : K t < K s then
(assert H2 : K₂ t s = K s, from max_eq_right J, by rewrite [H1, -H2])
else
(assert Heq : K t = K s, from
eq_of_le_of_ge (le_of_not_gt H) (le_of_not_gt J),
by rewrite [↑K₂, Heq]))
theorem canon_2_bound_left (s t : seq) (Hs : regular s) (n : ℕ+) :
abs (s n) ≤ rat_of_pnat (K₂ s t) :=
calc
abs (s n) ≤ rat_of_pnat (K s) : canon_bound Hs n
... ≤ rat_of_pnat (K₂ s t) : rat_of_pnat_le_of_pnat_le (!max_left)
theorem canon_2_bound_right (s t : seq) (Ht : regular t) (n : ℕ+) :
abs (t n) ≤ rat_of_pnat (K₂ s t) :=
calc
abs (t n) ≤ rat_of_pnat (K t) : canon_bound Ht n
... ≤ rat_of_pnat (K₂ s t) : rat_of_pnat_le_of_pnat_le (!max_right)
definition sadd (s t : seq) : seq := λ n, (s (2 * n)) + (t (2 * n))
theorem reg_add_reg {s t : seq} (Hs : regular s) (Ht : regular t) : regular (sadd s t) :=
begin
rewrite [↑regular at *, ↑sadd],
intros,
rewrite add_sub_comm,
apply rat.le.trans,
apply abs_add_le_abs_add_abs,
rewrite add_halves_double,
apply rat.add_le_add,
apply Hs,
apply Ht
end
definition smul (s t : seq) : seq := λ n : ℕ+, (s ((K₂ s t) * 2 * n)) * (t ((K₂ s t) * 2 * n))
theorem reg_mul_reg {s t : seq} (Hs : regular s) (Ht : regular t) : regular (smul s t) :=
begin
rewrite [↑regular at *, ↑smul],
intros,
rewrite rewrite_helper,
apply rat.le.trans,
apply abs_add_le_abs_add_abs,
apply rat.le.trans,
apply rat.add_le_add,
rewrite abs_mul,
apply rat.mul_le_mul_of_nonneg_right,
apply canon_2_bound_left s t Hs,
apply abs_nonneg,
rewrite abs_mul,
apply rat.mul_le_mul_of_nonneg_left,
apply canon_2_bound_right s t Ht,
apply abs_nonneg,
apply ineq_helper,
apply Ht,
apply Hs
end
definition sneg (s : seq) : seq := λ n : ℕ+, - (s n)
theorem reg_neg_reg {s : seq} (Hs : regular s) : regular (sneg s) :=
begin
rewrite [↑regular at *, ↑sneg],
intros,
rewrite [-abs_neg, neg_sub, sub_neg_eq_add, rat.add.comm],
apply Hs
end
-----------------------------------
-- show properties of +, *, -
definition zero : seq := λ n, 0
definition one : seq := λ n, 1
theorem s_add_comm (s t : seq) : sadd s t ≡ sadd t s :=
begin
esimp [sadd],
intro n,
rewrite [sub_add_eq_sub_sub, rat.add_sub_cancel, rat.sub_self, abs_zero],
apply add_invs_nonneg
end
theorem s_add_assoc (s t u : seq) (Hs : regular s) (Hu : regular u) :
sadd (sadd s t) u ≡ sadd s (sadd t u) :=
begin
rewrite [↑sadd, ↑equiv, ↑regular at *],
intros,
rewrite factor_lemma,
apply rat.le.trans,
apply abs_add_le_abs_add_abs,
apply rat.le.trans,
rotate 1,
apply rat.add_le_add_right,
apply inv_two_mul_le_inv,
rewrite [-(add_halves (2 * n)), -(add_halves n), factor_lemma_2],
apply rat.add_le_add,
apply Hs,
apply Hu
end
theorem s_mul_comm (s t : seq) : smul s t ≡ smul t s :=
begin
rewrite ↑smul,
intros n,
rewrite [*(K₂_symm s t), rat.mul.comm, rat.sub_self, abs_zero],
apply add_invs_nonneg
end
definition DK (s t : seq) := (K₂ s t) * 2
theorem DK_rewrite (s t : seq) : (K₂ s t) * 2 = DK s t := rfl
definition TK (s t u : seq) := (DK (λ (n : ℕ+), s (mul (DK s t) n) * t (mul (DK s t) n)) u)
theorem TK_rewrite (s t u : seq) :
(DK (λ (n : ℕ+), s (mul (DK s t) n) * t (mul (DK s t) n)) u) = TK s t u := rfl
theorem s_mul_assoc_lemma (s t u : seq) (a b c d : ℕ+) :
abs (s a * t a * u b - s c * t d * u d) ≤ abs (t a) * abs (u b) * abs (s a - s c) +
abs (s c) * abs (t a) * abs (u b - u d) + abs (s c) * abs (u d) * abs (t a - t d) :=
begin
rewrite (rewrite_helper7 _ _ _ _ (s c)),
apply rat.le.trans,
apply abs_add_le_abs_add_abs,
rewrite rat.add.assoc,
apply rat.add_le_add,
rewrite 2 abs_mul,
apply rat.le.refl,
rewrite [*rat.mul.assoc, -rat.mul_sub_left_distrib, -rat.left_distrib, abs_mul],
apply rat.mul_le_mul_of_nonneg_left,
rewrite rewrite_helper,
apply rat.le.trans,
apply abs_add_le_abs_add_abs,
apply rat.add_le_add,
rewrite abs_mul, apply rat.le.refl,
rewrite [abs_mul, rat.mul.comm], apply rat.le.refl,
apply abs_nonneg
end
definition Kq (s : seq) := rat_of_pnat (K s) + 1
theorem Kq_bound {s : seq} (H : regular s) : ∀ n, abs (s n) ≤ Kq s :=
begin
intros,
apply rat.le_of_lt,
apply rat.lt_of_le_of_lt,
apply canon_bound H,
apply rat.lt_add_of_pos_right,
apply rat.zero_lt_one
end
theorem Kq_bound_nonneg {s : seq} (H : regular s) : 0 ≤ Kq s :=
rat.le.trans !abs_nonneg (Kq_bound H 2)
theorem Kq_bound_pos {s : seq} (H : regular s) : 0 < Kq s :=
have H1 : 0 ≤ rat_of_pnat (K s), from rat.le.trans (!abs_nonneg) (canon_bound H 2),
add_pos_of_nonneg_of_pos H1 rat.zero_lt_one
theorem s_mul_assoc_lemma_5 {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u)
(a b c : ℕ+) : abs (t a) * abs (u b) * abs (s a - s c) ≤ (Kq t) * (Kq u) * (a⁻¹ + c⁻¹) :=
begin
repeat apply rat.mul_le_mul,
apply Kq_bound Ht,
apply Kq_bound Hu,
apply abs_nonneg,
apply Kq_bound_nonneg Ht,
apply Hs,
apply abs_nonneg,
apply rat.mul_nonneg,
apply Kq_bound_nonneg Ht,
apply Kq_bound_nonneg Hu,
end
theorem s_mul_assoc_lemma_2 {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u)
(a b c d : ℕ+) :
abs (t a) * abs (u b) * abs (s a - s c) + abs (s c) * abs (t a) * abs (u b - u d)
+ abs (s c) * abs (u d) * abs (t a - t d) ≤
(Kq t) * (Kq u) * (a⁻¹ + c⁻¹) + (Kq s) * (Kq t) * (b⁻¹ + d⁻¹) + (Kq s) * (Kq u) * (a⁻¹ + d⁻¹) :=
begin
apply add_le_add_three,
repeat (assumption | apply rat.mul_le_mul | apply Kq_bound | apply Kq_bound_nonneg |
apply abs_nonneg),
apply Hs,
apply abs_nonneg,
apply rat.mul_nonneg,
repeat (apply Kq_bound_nonneg | assumption),
repeat apply rat.mul_le_mul,
repeat (assumption | apply rat.mul_le_mul | apply Kq_bound | apply Kq_bound_nonneg |
apply abs_nonneg),
apply Hu,
apply abs_nonneg,
apply rat.mul_nonneg,
repeat (assumption | apply rat.mul_le_mul | apply Kq_bound | apply Kq_bound_nonneg |
apply abs_nonneg),
apply Ht,
apply abs_nonneg,
apply rat.mul_nonneg,
repeat (apply Kq_bound_nonneg; assumption)
end
theorem s_mul_assoc {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) :
smul (smul s t) u ≡ smul s (smul t u) :=
begin
apply eq_of_bdd_var,
repeat apply reg_mul_reg,
apply Hs,
apply Ht,
apply Hu,
apply reg_mul_reg Hs,
apply reg_mul_reg Ht Hu,
intros,
apply exists.intro,
intros,
rewrite [↑smul, *DK_rewrite, *TK_rewrite, -*pnat.mul.assoc, -*rat.mul.assoc],
apply rat.le.trans,
apply s_mul_assoc_lemma,
apply rat.le.trans,
apply s_mul_assoc_lemma_2,
apply Hs,
apply Ht,
apply Hu,
rewrite [*s_mul_assoc_lemma_3, -rat.distrib_three_right],
apply s_mul_assoc_lemma_4,
apply a,
repeat apply rat.add_pos,
repeat apply rat.mul_pos,
apply Kq_bound_pos Ht,
apply Kq_bound_pos Hu,
apply rat.add_pos,
repeat apply inv_pos,
repeat apply rat.mul_pos,
apply Kq_bound_pos Hs,
apply Kq_bound_pos Ht,
apply rat.add_pos,
repeat apply inv_pos,
repeat apply rat.mul_pos,
apply Kq_bound_pos Hs,
apply Kq_bound_pos Hu,
apply rat.add_pos,
repeat apply inv_pos,
apply a_1
end
theorem zero_is_reg : regular zero :=
begin
rewrite [↑regular, ↑zero],
intros,
rewrite [rat.sub_zero, abs_zero],
apply add_invs_nonneg
end
theorem s_zero_add (s : seq) (H : regular s) : sadd zero s ≡ s :=
begin
rewrite [↑sadd, ↑zero, ↑equiv, ↑regular at H],
intros,
rewrite [rat.zero_add],
apply rat.le.trans,
apply H,
apply rat.add_le_add,
apply inv_two_mul_le_inv,
apply rat.le.refl
end
theorem s_add_zero (s : seq) (H : regular s) : sadd s zero ≡ s :=
begin
rewrite [↑sadd, ↑zero, ↑equiv, ↑regular at H],
intros,
rewrite [rat.add_zero],
apply rat.le.trans,
apply H,
apply rat.add_le_add,
apply inv_two_mul_le_inv,
apply rat.le.refl
end
theorem s_neg_cancel (s : seq) (H : regular s) : sadd (sneg s) s ≡ zero :=
begin
rewrite [↑sadd, ↑sneg, ↑regular at H, ↑zero, ↑equiv],
intros,
rewrite [neg_add_eq_sub, rat.sub_self, rat.sub_zero, abs_zero],
apply add_invs_nonneg
end
theorem neg_s_cancel (s : seq) (H : regular s) : sadd s (sneg s) ≡ zero :=
begin
apply equiv.trans,
rotate 3,
apply s_add_comm,
apply s_neg_cancel s H,
repeat (apply reg_add_reg | apply reg_neg_reg | assumption),
apply zero_is_reg
end
theorem add_well_defined {s t u v : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u)
(Hv : regular v) (Esu : s ≡ u) (Etv : t ≡ v) : sadd s t ≡ sadd u v :=
begin
rewrite [↑sadd, ↑equiv at *],
intros,
rewrite [add_sub_comm, add_halves_double],
apply rat.le.trans,
apply abs_add_le_abs_add_abs,
apply rat.add_le_add,
apply Esu,
apply Etv
end
set_option tactic.goal_names false
theorem mul_bound_helper {s t : seq} (Hs : regular s) (Ht : regular t) (a b c : ℕ+) (j : ℕ+) :
∃ N : ℕ+, ∀ n : ℕ+, n ≥ N → abs (s (a * n) * t (b * n) - s (c * n) * t (c * n)) ≤ j⁻¹ :=
begin
existsi pceil (((rat_of_pnat (K s)) * (b⁻¹ + c⁻¹) + (a⁻¹ + c⁻¹) *
(rat_of_pnat (K t))) * (rat_of_pnat j)),
intros n Hn,
rewrite rewrite_helper4,
apply rat.le.trans,
apply abs_add_le_abs_add_abs,
apply rat.le.trans,
rotate 1,
show n⁻¹ * ((rat_of_pnat (K s)) * (b⁻¹ + c⁻¹)) +
n⁻¹ * ((a⁻¹ + c⁻¹) * (rat_of_pnat (K t))) ≤ j⁻¹, begin
rewrite -rat.left_distrib,
apply rat.le.trans,
apply rat.mul_le_mul_of_nonneg_right,
apply pceil_helper Hn,
repeat (apply rat.mul_pos | apply rat.add_pos | apply inv_pos | apply rat_of_pnat_is_pos),
apply rat.le_of_lt,
apply rat.add_pos,
apply rat.mul_pos,
apply rat_of_pnat_is_pos,
apply rat.add_pos,
repeat apply inv_pos,
apply rat.mul_pos,
apply rat.add_pos,
repeat apply inv_pos,
apply rat_of_pnat_is_pos,
have H : (rat_of_pnat (K s) * (b⁻¹ + c⁻¹) + (a⁻¹ + c⁻¹) * rat_of_pnat (K t)) ≠ 0, begin
apply rat.ne_of_gt,
repeat (apply rat.mul_pos | apply rat.add_pos | apply rat_of_pnat_is_pos | apply inv_pos),
end,
rewrite (rat.div_helper H),
apply rat.le.refl
end,
apply rat.add_le_add,
rewrite [-rat.mul_sub_left_distrib, abs_mul],
apply rat.le.trans,
apply rat.mul_le_mul,
apply canon_bound,
apply Hs,
apply Ht,
apply abs_nonneg,
apply rat.le_of_lt,
apply rat_of_pnat_is_pos,
rewrite [*inv_mul_eq_mul_inv, -rat.right_distrib, -rat.mul.assoc, rat.mul.comm],
apply rat.mul_le_mul_of_nonneg_left,
apply rat.le.refl,
apply rat.le_of_lt,
apply inv_pos,
rewrite [-rat.mul_sub_right_distrib, abs_mul],
apply rat.le.trans,
apply rat.mul_le_mul,
apply Hs,
apply canon_bound,
apply Ht,
apply abs_nonneg,
apply add_invs_nonneg,
rewrite [*inv_mul_eq_mul_inv, -rat.right_distrib, mul.comm _ n⁻¹, rat.mul.assoc],
apply rat.mul_le_mul,
repeat apply rat.le.refl,
apply rat.le_of_lt,
apply rat.mul_pos,
apply rat.add_pos,
repeat apply inv_pos,
apply rat_of_pnat_is_pos,
apply rat.le_of_lt,
apply inv_pos
end
theorem s_distrib {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) :
smul s (sadd t u) ≡ sadd (smul s t) (smul s u) :=
begin
apply eq_of_bdd,
repeat (assumption | apply reg_add_reg | apply reg_mul_reg),
intros,
let exh1 := λ a b c, mul_bound_helper Hs Ht a b c (2 * j),
apply exists.elim,
apply exh1,
rotate 3,
intros N1 HN1,
let exh2 := λ d e f, mul_bound_helper Hs Hu d e f (2 * j),
apply exists.elim,
apply exh2,
rotate 3,
intros N2 HN2,
existsi max N1 N2,
intros n Hn,
rewrite [↑sadd at *, ↑smul, rewrite_helper3, -add_halves j, -*pnat.mul.assoc at *],
apply rat.le.trans,
apply abs_add_le_abs_add_abs,
apply rat.add_le_add,
apply HN1,
apply le.trans,
apply max_left N1 N2,
apply Hn,
apply HN2,
apply le.trans,
apply max_right N1 N2,
apply Hn
end
theorem mul_zero_equiv_zero {s t : seq} (Hs : regular s) (Ht : regular t) (Htz : t ≡ zero) :
smul s t ≡ zero :=
begin
apply eq_of_bdd_var,
apply reg_mul_reg Hs Ht,
apply zero_is_reg,
intro ε Hε,
let Bd := bdd_of_eq_var Ht zero_is_reg Htz (ε / (Kq s))
(pos_div_of_pos_of_pos Hε (Kq_bound_pos Hs)),
cases Bd with [N, HN],
existsi N,
intro n Hn,
rewrite [↑equiv at Htz, ↑zero at *, rat.sub_zero, ↑smul, abs_mul],
apply rat.le.trans,
apply rat.mul_le_mul,
apply Kq_bound Hs,
let HN' := λ n, (!rat.sub_zero ▸ HN n),
apply HN',
apply le.trans Hn,
apply pnat.mul_le_mul_left,
apply abs_nonneg,
apply rat.le_of_lt (Kq_bound_pos Hs),
rewrite (rat.mul_div_cancel' (ne.symm (rat.ne_of_lt (Kq_bound_pos Hs)))),
apply rat.le.refl
end
theorem neg_bound_eq_bound (s : seq) : K (sneg s) = K s :=
by rewrite [↑K, ↑sneg, abs_neg]
theorem neg_bound2_eq_bound2 (s t : seq) : K₂ s (sneg t) = K₂ s t :=
by rewrite [↑K₂, neg_bound_eq_bound]
theorem sneg_def (s : seq) : (λ (n : ℕ+), -(s n)) = sneg s := rfl
theorem mul_neg_equiv_neg_mul {s t : seq} : smul s (sneg t) ≡ sneg (smul s t) :=
begin
rewrite [↑equiv, ↑smul],
intros,
rewrite [↑sneg, *sub_neg_eq_add, -neg_mul_eq_mul_neg, rat.add.comm, *sneg_def,
*neg_bound2_eq_bound2, rat.sub_self, abs_zero],
apply add_invs_nonneg
end
theorem equiv_of_diff_equiv_zero {s t : seq} (Hs : regular s) (Ht : regular t)
(H : sadd s (sneg t) ≡ zero) : s ≡ t :=
begin
have hsimp : ∀ a b c d e : ℚ, a + b + c + (d + e) = b + d + a + e + c, from
λ a b c d e, calc
a + b + c + (d + e) = a + b + (d + e) + c : rat.add.right_comm
... = a + (b + d) + e + c : by rewrite[-*rat.add.assoc]
... = b + d + a + e + c : rat.add.comm,
apply eq_of_bdd Hs Ht,
intros,
let He := bdd_of_eq H,
existsi 2 * (2 * (2 * j)),
intros n Hn,
rewrite (rewrite_helper5 _ _ (s (2 * n)) (t (2 * n))),
apply rat.le.trans,
apply abs_add_three,
apply rat.le.trans,
apply add_le_add_three,
apply Hs,
rewrite [↑sadd at He, ↑sneg at He, ↑zero at He],
let He' := λ a b c, !rat.sub_zero ▸ (He a b c),
apply (He' _ _ Hn),
apply Ht,
rewrite [hsimp, add_halves, -(add_halves j), -(add_halves (2 * j)), -*rat.add.assoc],
apply rat.add_le_add_right,
apply add_le_add_three,
repeat (apply rat.le.trans; apply inv_ge_of_le Hn; apply inv_two_mul_le_inv)
end
theorem s_sub_cancel (s : seq) : sadd s (sneg s) ≡ zero :=
begin
rewrite [↑equiv, ↑sadd, ↑sneg, ↑zero],
intros,
rewrite [rat.sub_zero, rat.sub_self, abs_zero],
apply add_invs_nonneg
end
theorem diff_equiv_zero_of_equiv {s t : seq} (Hs : regular s) (Ht : regular t) (H : s ≡ t) :
sadd s (sneg t) ≡ zero :=
begin
apply equiv.trans,
rotate 4,
apply s_sub_cancel t,
rotate 2,
apply zero_is_reg,
apply add_well_defined,
repeat (assumption | apply reg_neg_reg),
apply equiv.refl,
repeat (assumption | apply reg_add_reg | apply reg_neg_reg)
end
theorem mul_well_defined_half1 {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u)
(Etu : t ≡ u) : smul s t ≡ smul s u :=
begin
apply equiv_of_diff_equiv_zero,
rotate 2,
apply equiv.trans,
rotate 3,
apply equiv.symm,
apply add_well_defined,
rotate 4,
apply equiv.refl,
apply mul_neg_equiv_neg_mul,
apply equiv.trans,
rotate 3,
apply equiv.symm,
apply s_distrib,
rotate 3,
apply mul_zero_equiv_zero,
rotate 2,
apply diff_equiv_zero_of_equiv,
repeat (assumption | apply reg_mul_reg | apply reg_neg_reg | apply reg_add_reg |
apply zero_is_reg)
end
theorem mul_well_defined_half2 {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u)
(Est : s ≡ t) : smul s u ≡ smul t u :=
begin
apply equiv.trans,
rotate 3,
apply s_mul_comm,
apply equiv.trans,
rotate 3,
apply mul_well_defined_half1,
rotate 2,
apply Ht,
rotate 1,
apply s_mul_comm,
repeat (assumption | apply reg_mul_reg)
end
theorem mul_well_defined {s t u v : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u)
(Hv : regular v) (Esu : s ≡ u) (Etv : t ≡ v) : smul s t ≡ smul u v :=
begin
apply equiv.trans,
exact reg_mul_reg Hs Ht,
exact reg_mul_reg Hs Hv,
exact reg_mul_reg Hu Hv,
apply mul_well_defined_half1,
repeat assumption,
apply mul_well_defined_half2,
repeat assumption
end
theorem neg_well_defined {s t : seq} (Est : s ≡ t) : sneg s ≡ sneg t :=
begin
rewrite [↑sneg, ↑equiv at *],
intros,
rewrite [-abs_neg, neg_sub, sub_neg_eq_add, rat.add.comm],
apply Est
end
theorem one_is_reg : regular one :=
begin
rewrite [↑regular, ↑one],
intros,
rewrite [rat.sub_self, abs_zero],
apply add_invs_nonneg
end
theorem s_one_mul {s : seq} (H : regular s) : smul one s ≡ s :=
begin
intros,
rewrite [↑smul, ↑one, rat.one_mul],
apply rat.le.trans,
apply H,
apply rat.add_le_add_right,
apply inv_mul_le_inv
end
theorem s_mul_one {s : seq} (H : regular s) : smul s one ≡ s :=
begin
apply equiv.trans,
apply reg_mul_reg H one_is_reg,
rotate 2,
apply s_mul_comm,
apply s_one_mul H,
apply reg_mul_reg one_is_reg H,
apply H
end
theorem zero_nequiv_one : ¬ zero ≡ one :=
begin
intro Hz,
rewrite [↑equiv at Hz, ↑zero at Hz, ↑one at Hz],
let H := Hz (2 * 2),
rewrite [rat.zero_sub at H, abs_neg at H, add_halves at H],
have H' : pone⁻¹ ≤ 2⁻¹, from calc
pone⁻¹ = 1 : by rewrite -pone_inv
... = abs 1 : abs_of_pos zero_lt_one
... ≤ 2⁻¹ : H,
let H'' := ge_of_inv_le H',
apply absurd (one_lt_two) (not_lt_of_ge H'')
end
---------------------------------------------
-- constant sequences
definition const (a : ℚ) : seq := λ n, a
theorem const_reg (a : ℚ) : regular (const a) :=
begin
intros,
rewrite [↑const, rat.sub_self, abs_zero],
apply add_invs_nonneg
end
theorem add_consts (a b : ℚ) : sadd (const a) (const b) ≡ const (a + b) :=
by apply equiv.refl
theorem mul_consts (a b : ℚ) : smul (const a) (const b) ≡ const (a * b) :=
by apply equiv.refl
theorem neg_const (a : ℚ) : sneg (const a) ≡ const (-a) :=
by apply equiv.refl
---------------------------------------------
-- create the type of regular sequences and lift theorems
record reg_seq : Type :=
(sq : seq) (is_reg : regular sq)
definition requiv (s t : reg_seq) := (reg_seq.sq s) ≡ (reg_seq.sq t)
definition requiv.refl (s : reg_seq) : requiv s s := equiv.refl (reg_seq.sq s)
definition requiv.symm (s t : reg_seq) (H : requiv s t) : requiv t s :=
equiv.symm (reg_seq.sq s) (reg_seq.sq t) H
definition requiv.trans (s t u : reg_seq) (H : requiv s t) (H2 : requiv t u) : requiv s u :=
equiv.trans _ _ _ (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) H H2
definition radd (s t : reg_seq) : reg_seq :=
reg_seq.mk (sadd (reg_seq.sq s) (reg_seq.sq t))
(reg_add_reg (reg_seq.is_reg s) (reg_seq.is_reg t))
infix `+` := radd
definition rmul (s t : reg_seq) : reg_seq :=
reg_seq.mk (smul (reg_seq.sq s) (reg_seq.sq t))
(reg_mul_reg (reg_seq.is_reg s) (reg_seq.is_reg t))
infix `*` := rmul
definition rneg (s : reg_seq) : reg_seq :=
reg_seq.mk (sneg (reg_seq.sq s)) (reg_neg_reg (reg_seq.is_reg s))
prefix `-` := rneg
definition radd_well_defined {s t u v : reg_seq} (H : requiv s u) (H2 : requiv t v) :
requiv (s + t) (u + v) :=
add_well_defined (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) (reg_seq.is_reg v) H H2
definition rmul_well_defined {s t u v : reg_seq} (H : requiv s u) (H2 : requiv t v) :
requiv (s * t) (u * v) :=
mul_well_defined (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) (reg_seq.is_reg v) H H2
definition rneg_well_defined {s t : reg_seq} (H : requiv s t) : requiv (-s) (-t) :=
neg_well_defined H
theorem requiv_is_equiv : equivalence requiv :=
mk_equivalence requiv requiv.refl requiv.symm requiv.trans
definition reg_seq.to_setoid [instance] : setoid reg_seq :=
⦃setoid, r := requiv, iseqv := requiv_is_equiv⦄
definition r_zero : reg_seq :=
reg_seq.mk (zero) (zero_is_reg)
definition r_one : reg_seq :=
reg_seq.mk (one) (one_is_reg)
theorem r_add_comm (s t : reg_seq) : requiv (s + t) (t + s) :=
s_add_comm (reg_seq.sq s) (reg_seq.sq t)
theorem r_add_assoc (s t u : reg_seq) : requiv (s + t + u) (s + (t + u)) :=
s_add_assoc (reg_seq.sq s) (reg_seq.sq t) (reg_seq.sq u) (reg_seq.is_reg s) (reg_seq.is_reg u)
theorem r_zero_add (s : reg_seq) : requiv (r_zero + s) s :=
s_zero_add (reg_seq.sq s) (reg_seq.is_reg s)
theorem r_add_zero (s : reg_seq) : requiv (s + r_zero) s :=
s_add_zero (reg_seq.sq s) (reg_seq.is_reg s)
theorem r_neg_cancel (s : reg_seq) : requiv (-s + s) r_zero :=
s_neg_cancel (reg_seq.sq s) (reg_seq.is_reg s)
theorem r_mul_comm (s t : reg_seq) : requiv (s * t) (t * s) :=
s_mul_comm (reg_seq.sq s) (reg_seq.sq t)
theorem r_mul_assoc (s t u : reg_seq) : requiv (s * t * u) (s * (t * u)) :=
s_mul_assoc (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u)
theorem r_mul_one (s : reg_seq) : requiv (s * r_one) s :=
s_mul_one (reg_seq.is_reg s)
theorem r_one_mul (s : reg_seq) : requiv (r_one * s) s :=
s_one_mul (reg_seq.is_reg s)
theorem r_distrib (s t u : reg_seq) : requiv (s * (t + u)) (s * t + s * u) :=
s_distrib (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u)
theorem r_zero_nequiv_one : ¬ requiv r_zero r_one :=
zero_nequiv_one
definition r_const (a : ℚ) : reg_seq := reg_seq.mk (const a) (const_reg a)
theorem r_add_consts (a b : ℚ) : requiv (r_const a + r_const b) (r_const (a + b)) := add_consts a b
theorem r_mul_consts (a b : ℚ) : requiv (r_const a * r_const b) (r_const (a * b)) := mul_consts a b
theorem r_neg_const (a : ℚ) : requiv (-r_const a) (r_const (-a)) := neg_const a
end s
----------------------------------------------
-- take quotients to get ℝ and show it's a comm ring
open s
definition real := quot reg_seq.to_setoid
namespace real
notation `ℝ` := real
definition add (x y : ℝ) : ℝ :=
(quot.lift_on₂ x y (λ a b, quot.mk (a + b))
(take a b c d : reg_seq, take Hab : requiv a c, take Hcd : requiv b d,
quot.sound (radd_well_defined Hab Hcd)))
protected definition prio := num.pred rat.prio
infix [priority real.prio] `+` := add
definition mul (x y : ℝ) : ℝ :=
(quot.lift_on₂ x y (λ a b, quot.mk (a * b))
(take a b c d : reg_seq, take Hab : requiv a c, take Hcd : requiv b d,
quot.sound (rmul_well_defined Hab Hcd)))
infix [priority real.prio] `*` := mul
definition neg (x : ℝ) : ℝ :=
(quot.lift_on x (λ a, quot.mk (-a)) (take a b : reg_seq, take Hab : requiv a b,
quot.sound (rneg_well_defined Hab)))
prefix [priority real.prio] `-` := neg
definition zero : ℝ := quot.mk r_zero
--notation 0 := zero
definition one : ℝ := quot.mk r_one
theorem add_comm (x y : ℝ) : x + y = y + x :=
quot.induction_on₂ x y (λ s t, quot.sound (r_add_comm s t))
theorem add_assoc (x y z : ℝ) : x + y + z = x + (y + z) :=
quot.induction_on₃ x y z (λ s t u, quot.sound (r_add_assoc s t u))
theorem zero_add (x : ℝ) : zero + x = x :=
quot.induction_on x (λ s, quot.sound (r_zero_add s))
theorem add_zero (x : ℝ) : x + zero = x :=
quot.induction_on x (λ s, quot.sound (r_add_zero s))
theorem neg_cancel (x : ℝ) : -x + x = zero :=
quot.induction_on x (λ s, quot.sound (r_neg_cancel s))
theorem mul_assoc (x y z : ℝ) : x * y * z = x * (y * z) :=
quot.induction_on₃ x y z (λ s t u, quot.sound (r_mul_assoc s t u))
theorem mul_comm (x y : ℝ) : x * y = y * x :=
quot.induction_on₂ x y (λ s t, quot.sound (r_mul_comm s t))
theorem one_mul (x : ℝ) : one * x = x :=
quot.induction_on x (λ s, quot.sound (r_one_mul s))
theorem mul_one (x : ℝ) : x * one = x :=
quot.induction_on x (λ s, quot.sound (r_mul_one s))
theorem distrib (x y z : ℝ) : x * (y + z) = x * y + x * z :=
quot.induction_on₃ x y z (λ s t u, quot.sound (r_distrib s t u))
theorem distrib_l (x y z : ℝ) : (x + y) * z = x * z + y * z :=
by rewrite [mul_comm, distrib, {x * _}mul_comm, {y * _}mul_comm] -- this shouldn't be necessary
theorem zero_ne_one : ¬ zero = one :=
take H : zero = one,
absurd (quot.exact H) (r_zero_nequiv_one)
protected definition comm_ring [reducible] : algebra.comm_ring ℝ :=
begin
fapply algebra.comm_ring.mk,
exact add,
exact add_assoc,
exact zero,
exact zero_add,
exact add_zero,
exact neg,
exact neg_cancel,
exact add_comm,
exact mul,
exact mul_assoc,
apply one,
apply one_mul,
apply mul_one,
apply distrib,
apply distrib_l,
apply mul_comm
end
open rat -- no coercions before
definition of_rat [coercion] (a : ℚ) : ℝ := quot.mk (s.r_const a)
theorem of_rat_add (a b : ℚ) : of_rat a + of_rat b = of_rat (a + b) :=
quot.sound (s.r_add_consts a b)
theorem of_rat_neg (a : ℚ) : of_rat (-a) = -of_rat a := eq.symm (quot.sound (s.r_neg_const a))
theorem of_rat_mul (a b : ℚ) : of_rat a * of_rat b = of_rat (a * b) :=
quot.sound (s.r_mul_consts a b)
theorem add_half_of_rat (n : ℕ+) : of_rat (2 * n)⁻¹ + of_rat (2 * n)⁻¹ = of_rat (n⁻¹) :=
by rewrite [of_rat_add, pnat.add_halves]
end real
|
cc9cea13998a05c9a449527b967d60c2918b0853 | 1dd482be3f611941db7801003235dc84147ec60a | /src/logic/basic.lean | 1ef17f24100f46acf53f4b274f1fddc3572944ba | [
"Apache-2.0"
] | permissive | sanderdahmen/mathlib | 479039302bd66434bb5672c2a4cecf8d69981458 | 8f0eae75cd2d8b7a083cf935666fcce4565df076 | refs/heads/master | 1,587,491,322,775 | 1,549,672,060,000 | 1,549,672,060,000 | 169,748,224 | 0 | 0 | Apache-2.0 | 1,549,636,694,000 | 1,549,636,694,000 | null | UTF-8 | Lean | false | false | 25,860 | 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
Theorems that require decidability hypotheses are in the namespace "decidable".
Classical versions are in the namespace "classical".
Note: in the presence of automation, this whole file may be unnecessary. On the other hand,
maybe it is useful for writing automation.
-/
import data.prod tactic.cache
/-
miscellany
TODO: move elsewhere
-/
section miscellany
variables {α : Type*} {β : Type*}
@[reducible] def hidden {a : α} := a
def empty.elim {C : Sort*} : empty → C.
instance : subsingleton empty := ⟨λa, a.elim⟩
instance : decidable_eq empty := λa, a.elim
@[priority 0] instance decidable_eq_of_subsingleton
{α} [subsingleton α] : decidable_eq α
| a b := is_true (subsingleton.elim a b)
/- Add an instance to "undo" coercion transitivity into a chain of coercions, because
most simp lemmas are stated with respect to simple coercions and will not match when
part of a chain. -/
@[simp] theorem coe_coe {α β γ} [has_coe α β] [has_coe_t β γ]
(a : α) : (a : γ) = (a : β) := rfl
@[simp] theorem coe_fn_coe_trans
{α β γ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_fun γ]
(x : α) : @coe_fn α _ x = @coe_fn β _ x := rfl
@[simp] theorem coe_fn_coe_base
{α β} [has_coe α β] [has_coe_to_fun β]
(x : α) : @coe_fn α _ x = @coe_fn β _ x := rfl
@[simp] theorem coe_sort_coe_trans
{α β γ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_sort γ]
(x : α) : @coe_sort α _ x = @coe_sort β _ x := rfl
@[simp] theorem coe_sort_coe_base
{α β} [has_coe α β] [has_coe_to_sort β]
(x : α) : @coe_sort α _ x = @coe_sort β _ x := rfl
/-- `pempty` is the universe-polymorphic analogue of `empty`. -/
@[derive decidable_eq]
inductive {u} pempty : Sort u
def pempty.elim {C : Sort*} : pempty → C.
instance subsingleton_pempty : subsingleton pempty := ⟨λa, a.elim⟩
lemma congr_arg_heq {α} {β : α → Sort*} (f : ∀ a, β a) : ∀ {a₁ a₂ : α}, a₁ = a₂ → f a₁ == f a₂
| a _ rfl := heq.rfl
end miscellany
/-
propositional connectives
-/
@[simp] theorem false_ne_true : false ≠ true
| h := h.symm ▸ trivial
section propositional
variables {a b c d : Prop}
/- implies -/
theorem iff_of_eq (e : a = b) : a ↔ b := e ▸ iff.rfl
theorem iff_iff_eq : (a ↔ b) ↔ a = b := ⟨propext, iff_of_eq⟩
@[simp] theorem imp_self : (a → a) ↔ true := iff_true_intro id
theorem imp_intro {α β} (h : α) (h₂ : β) : α := h
theorem imp_false : (a → false) ↔ ¬ a := iff.rfl
theorem imp_and_distrib {α} : (α → b ∧ c) ↔ (α → b) ∧ (α → c) :=
⟨λ h, ⟨λ ha, (h ha).left, λ ha, (h ha).right⟩,
λ h ha, ⟨h.left ha, h.right ha⟩⟩
@[simp] theorem and_imp : (a ∧ b → c) ↔ (a → b → c) :=
iff.intro (λ h ha hb, h ⟨ha, hb⟩) (λ h ⟨ha, hb⟩, h ha hb)
theorem iff_def : (a ↔ b) ↔ (a → b) ∧ (b → a) :=
iff_iff_implies_and_implies _ _
theorem iff_def' : (a ↔ b) ↔ (b → a) ∧ (a → b) :=
iff_def.trans and.comm
@[simp] theorem imp_true_iff {α : Sort*} : (α → true) ↔ true :=
iff_true_intro $ λ_, trivial
@[simp] theorem imp_iff_right (ha : a) : (a → b) ↔ b :=
⟨λf, f ha, imp_intro⟩
/- not -/
theorem not.elim {α : Sort*} (H1 : ¬a) (H2 : a) : α := absurd H2 H1
@[reducible] theorem not.imp {a b : Prop} (H2 : ¬b) (H1 : a → b) : ¬a := mt H1 H2
theorem not_not_of_not_imp : ¬(a → b) → ¬¬a :=
mt not.elim
theorem not_of_not_imp {α} : ¬(α → b) → ¬b :=
mt imp_intro
theorem dec_em (p : Prop) [decidable p] : p ∨ ¬p := decidable.em p
theorem by_contradiction {p} [decidable p] : (¬p → false) → p :=
decidable.by_contradiction
@[simp] theorem not_not [decidable a] : ¬¬a ↔ a :=
iff.intro by_contradiction not_not_intro
theorem of_not_not [decidable a] : ¬¬a → a :=
by_contradiction
theorem of_not_imp [decidable a] (h : ¬ (a → b)) : a :=
by_contradiction (not_not_of_not_imp h)
theorem not.imp_symm [decidable a] (h : ¬a → b) (hb : ¬b) : a :=
by_contradiction $ hb ∘ h
theorem not_imp_comm [decidable a] [decidable b] : (¬a → b) ↔ (¬b → a) :=
⟨not.imp_symm, not.imp_symm⟩
theorem imp.swap : (a → b → c) ↔ (b → a → c) :=
⟨function.swap, function.swap⟩
theorem imp_not_comm : (a → ¬b) ↔ (b → ¬a) :=
imp.swap
/- and -/
theorem not_and_of_not_left (b : Prop) : ¬a → ¬(a ∧ b) :=
mt and.left
theorem not_and_of_not_right (a : Prop) {b : Prop} : ¬b → ¬(a ∧ b) :=
mt and.right
theorem and.imp_left (h : a → b) : a ∧ c → b ∧ c :=
and.imp h id
theorem and.imp_right (h : a → b) : c ∧ a → c ∧ b :=
and.imp id h
lemma and.right_comm : (a ∧ b) ∧ c ↔ (a ∧ c) ∧ b :=
by simp [and.left_comm, and.comm]
lemma and.rotate : a ∧ b ∧ c ↔ b ∧ c ∧ a :=
by simp [and.left_comm, and.comm]
theorem and_not_self_iff (a : Prop) : a ∧ ¬ a ↔ false :=
iff.intro (assume h, (h.right) (h.left)) (assume h, h.elim)
theorem not_and_self_iff (a : Prop) : ¬ a ∧ a ↔ false :=
iff.intro (assume ⟨hna, ha⟩, hna ha) false.elim
theorem and_iff_left_of_imp {a b : Prop} (h : a → b) : (a ∧ b) ↔ a :=
iff.intro and.left (λ ha, ⟨ha, h ha⟩)
theorem and_iff_right_of_imp {a b : Prop} (h : b → a) : (a ∧ b) ↔ b :=
iff.intro and.right (λ hb, ⟨h hb, hb⟩)
lemma and.congr_right_iff : (a ∧ b ↔ a ∧ c) ↔ (a → (b ↔ c)) :=
⟨λ h ha, by simp [ha] at h; exact h, and_congr_right⟩
/- or -/
theorem or_of_or_of_imp_of_imp (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → d) : c ∨ d :=
or.imp h₂ h₃ h₁
theorem or_of_or_of_imp_left (h₁ : a ∨ c) (h : a → b) : b ∨ c :=
or.imp_left h h₁
theorem or_of_or_of_imp_right (h₁ : c ∨ a) (h : a → b) : c ∨ b :=
or.imp_right h h₁
theorem or.elim3 (h : a ∨ b ∨ c) (ha : a → d) (hb : b → d) (hc : c → d) : d :=
or.elim h ha (assume h₂, or.elim h₂ hb hc)
theorem or_imp_distrib : (a ∨ b → c) ↔ (a → c) ∧ (b → c) :=
⟨assume h, ⟨assume ha, h (or.inl ha), assume hb, h (or.inr hb)⟩,
assume ⟨ha, hb⟩, or.rec ha hb⟩
theorem or_iff_not_imp_left [decidable a] : a ∨ b ↔ (¬ a → b) :=
⟨or.resolve_left, λ h, dite _ or.inl (or.inr ∘ h)⟩
theorem or_iff_not_imp_right [decidable b] : a ∨ b ↔ (¬ b → a) :=
or.comm.trans or_iff_not_imp_left
theorem not_imp_not [decidable a] : (¬ a → ¬ b) ↔ (b → a) :=
⟨assume h hb, by_contradiction $ assume na, h na hb, mt⟩
/- distributivity -/
theorem and_or_distrib_left : a ∧ (b ∨ c) ↔ (a ∧ b) ∨ (a ∧ c) :=
⟨λ ⟨ha, hbc⟩, hbc.imp (and.intro ha) (and.intro ha),
or.rec (and.imp_right or.inl) (and.imp_right or.inr)⟩
theorem or_and_distrib_right : (a ∨ b) ∧ c ↔ (a ∧ c) ∨ (b ∧ c) :=
(and.comm.trans and_or_distrib_left).trans (or_congr and.comm and.comm)
theorem or_and_distrib_left : a ∨ (b ∧ c) ↔ (a ∨ b) ∧ (a ∨ c) :=
⟨or.rec (λha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr),
and.rec $ or.rec (imp_intro ∘ or.inl) (or.imp_right ∘ and.intro)⟩
theorem and_or_distrib_right : (a ∧ b) ∨ c ↔ (a ∨ c) ∧ (b ∨ c) :=
(or.comm.trans or_and_distrib_left).trans (and_congr or.comm or.comm)
/- iff -/
theorem iff_of_true (ha : a) (hb : b) : a ↔ b :=
⟨λ_, hb, λ _, ha⟩
theorem iff_of_false (ha : ¬a) (hb : ¬b) : a ↔ b :=
⟨ha.elim, hb.elim⟩
theorem iff_true_left (ha : a) : (a ↔ b) ↔ b :=
⟨λ h, h.1 ha, iff_of_true ha⟩
theorem iff_true_right (ha : a) : (b ↔ a) ↔ b :=
iff.comm.trans (iff_true_left ha)
theorem iff_false_left (ha : ¬a) : (a ↔ b) ↔ ¬b :=
⟨λ h, mt h.2 ha, iff_of_false ha⟩
theorem iff_false_right (ha : ¬a) : (b ↔ a) ↔ ¬b :=
iff.comm.trans (iff_false_left ha)
theorem not_or_of_imp [decidable a] (h : a → b) : ¬ a ∨ b :=
if ha : a then or.inr (h ha) else or.inl ha
theorem imp_iff_not_or [decidable a] : (a → b) ↔ (¬ a ∨ b) :=
⟨not_or_of_imp, or.neg_resolve_left⟩
theorem imp_or_distrib [decidable a] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) :=
by simp [imp_iff_not_or, or.comm, or.left_comm]
theorem imp_or_distrib' [decidable b] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) :=
by by_cases b; simp [h, or_iff_right_of_imp ((∘) false.elim)]
theorem not_imp_of_and_not : a ∧ ¬ b → ¬ (a → b)
| ⟨ha, hb⟩ h := hb $ h ha
@[simp] theorem not_imp [decidable a] : ¬(a → b) ↔ a ∧ ¬b :=
⟨λ h, ⟨of_not_imp h, not_of_not_imp h⟩, not_imp_of_and_not⟩
-- for monotonicity
lemma imp_imp_imp
(h₀ : c → a) (h₁ : b → d) :
(a → b) → (c → d) :=
assume (h₂ : a → b),
h₁ ∘ h₂ ∘ h₀
theorem peirce (a b : Prop) [decidable a] : ((a → b) → a) → a :=
if ha : a then λ h, ha else λ h, h ha.elim
theorem peirce' {a : Prop} (H : ∀ b : Prop, (a → b) → a) : a := H _ id
theorem not_iff_not [decidable a] [decidable b] : (¬ a ↔ ¬ b) ↔ (a ↔ b) :=
by rw [@iff_def (¬ a), @iff_def' a]; exact and_congr not_imp_not not_imp_not
theorem not_iff_comm [decidable a] [decidable b] : (¬ a ↔ b) ↔ (¬ b ↔ a) :=
by rw [@iff_def (¬ a), @iff_def (¬ b)]; exact and_congr not_imp_comm imp_not_comm
theorem not_iff [decidable a] [decidable b] : ¬ (a ↔ b) ↔ (¬ a ↔ b) :=
by split; intro h; [split, skip]; intro h'; [by_contradiction,intro,skip];
try { refine h _; simp [*] }; rw [h',not_iff_self] at h; exact h
theorem iff_not_comm [decidable a] [decidable b] : (a ↔ ¬ b) ↔ (b ↔ ¬ a) :=
by rw [@iff_def a, @iff_def b]; exact and_congr imp_not_comm not_imp_comm
theorem iff_iff_and_or_not_and_not [decidable b] : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) :=
by { split; intro h,
{ rw h; by_cases b; [left,right]; split; assumption },
{ cases h with h h; cases h; split; intro; { contradiction <|> assumption } } }
@[simp] theorem not_and_not_right [decidable b] : ¬(a ∧ ¬b) ↔ (a → b) :=
⟨λ h ha, h.imp_symm $ and.intro ha, λ h ⟨ha, hb⟩, hb $ h ha⟩
@[inline] def decidable_of_iff (a : Prop) (h : a ↔ b) [D : decidable a] : decidable b :=
decidable_of_decidable_of_iff D h
@[inline] def decidable_of_iff' (b : Prop) (h : a ↔ b) [D : decidable b] : decidable a :=
decidable_of_decidable_of_iff D h.symm
def decidable_of_bool : ∀ (b : bool) (h : b ↔ a), decidable a
| tt h := is_true (h.1 rfl)
| ff h := is_false (mt h.2 bool.ff_ne_tt)
/- de morgan's laws -/
theorem not_and_of_not_or_not (h : ¬ a ∨ ¬ b) : ¬ (a ∧ b)
| ⟨ha, hb⟩ := or.elim h (absurd ha) (absurd hb)
theorem not_and_distrib [decidable a] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b :=
⟨λ h, if ha : a then or.inr (λ hb, h ⟨ha, hb⟩) else or.inl ha, not_and_of_not_or_not⟩
theorem not_and_distrib' [decidable b] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b :=
⟨λ h, if hb : b then or.inl (λ ha, h ⟨ha, hb⟩) else or.inr hb, not_and_of_not_or_not⟩
@[simp] theorem not_and : ¬ (a ∧ b) ↔ (a → ¬ b) := and_imp
theorem not_and' : ¬ (a ∧ b) ↔ b → ¬a :=
not_and.trans imp_not_comm
theorem not_or_distrib : ¬ (a ∨ b) ↔ ¬ a ∧ ¬ b :=
⟨λ h, ⟨λ ha, h (or.inl ha), λ hb, h (or.inr hb)⟩,
λ ⟨h₁, h₂⟩ h, or.elim h h₁ h₂⟩
theorem or_iff_not_and_not [decidable a] [decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) :=
by rw [← not_or_distrib, not_not]
theorem and_iff_not_or_not [decidable a] [decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) :=
by rw [← not_and_distrib, not_not]
end propositional
/- equality -/
section equality
variables {α : Sort*} {a b : α}
@[simp] theorem heq_iff_eq : a == b ↔ a = b :=
⟨eq_of_heq, heq_of_eq⟩
theorem proof_irrel_heq {p q : Prop} (hp : p) (hq : q) : hp == hq :=
have p = q, from propext ⟨λ _, hq, λ _, hp⟩,
by subst q; refl
theorem ne_of_mem_of_not_mem {α β} [has_mem α β] {s : β} {a b : α}
(h : a ∈ s) : b ∉ s → a ≠ b :=
mt $ λ e, e ▸ h
theorem eq_equivalence : equivalence (@eq α) :=
⟨eq.refl, @eq.symm _, @eq.trans _⟩
lemma heq_of_eq_mp :
∀ {α β : Sort*} {a : α} {a' : β} (e : α = β) (h₂ : (eq.mp e a) = a'), a == a'
| α ._ a a' rfl h := eq.rec_on h (heq.refl _)
lemma rec_heq_of_heq {β} {C : α → Sort*} {x : C a} {y : β} (eq : a = b) (h : x == y) :
@eq.rec α a C x b eq == y :=
by subst eq; exact h
end equality
/-
quantifiers
-/
section quantifiers
variables {α : Sort*} {p q : α → Prop} {b : Prop}
def Exists.imp := @exists_imp_exists
theorem forall_swap {α β} {p : α → β → Prop} : (∀ x y, p x y) ↔ ∀ y x, p x y :=
⟨function.swap, function.swap⟩
theorem exists_swap {α β} {p : α → β → Prop} : (∃ x y, p x y) ↔ ∃ y x, p x y :=
⟨λ ⟨x, y, h⟩, ⟨y, x, h⟩, λ ⟨y, x, h⟩, ⟨x, y, h⟩⟩
@[simp] theorem exists_imp_distrib : ((∃ x, p x) → b) ↔ ∀ x, p x → b :=
⟨λ h x hpx, h ⟨x, hpx⟩, λ h ⟨x, hpx⟩, h x hpx⟩
--theorem forall_not_of_not_exists (h : ¬ ∃ x, p x) : ∀ x, ¬ p x :=
--forall_imp_of_exists_imp h
theorem not_exists_of_forall_not (h : ∀ x, ¬ p x) : ¬ ∃ x, p x :=
exists_imp_distrib.2 h
@[simp] theorem not_exists : (¬ ∃ x, p x) ↔ ∀ x, ¬ p x :=
exists_imp_distrib
theorem not_forall_of_exists_not : (∃ x, ¬ p x) → ¬ ∀ x, p x
| ⟨x, hn⟩ h := hn (h x)
theorem not_forall {p : α → Prop}
[decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] :
(¬ ∀ x, p x) ↔ ∃ x, ¬ p x :=
⟨not.imp_symm $ λ nx x, nx.imp_symm $ λ h, ⟨x, h⟩,
not_forall_of_exists_not⟩
@[simp] theorem not_forall_not [decidable (∃ x, p x)] :
(¬ ∀ x, ¬ p x) ↔ ∃ x, p x :=
by haveI := decidable_of_iff (¬ ∃ x, p x) not_exists;
exact not_iff_comm.1 not_exists
@[simp] theorem not_exists_not [∀ x, decidable (p x)] :
(¬ ∃ x, ¬ p x) ↔ ∀ x, p x :=
by simp
@[simp] theorem forall_true_iff : (α → true) ↔ true :=
iff_true_intro (λ _, trivial)
-- Unfortunately this causes simp to loop sometimes, so we
-- add the 2 and 3 cases as simp lemmas instead
theorem forall_true_iff' (h : ∀ a, p a ↔ true) : (∀ a, p a) ↔ true :=
iff_true_intro (λ _, of_iff_true (h _))
@[simp] theorem forall_2_true_iff {β : α → Sort*} : (∀ a, β a → true) ↔ true :=
forall_true_iff' $ λ _, forall_true_iff
@[simp] theorem forall_3_true_iff {β : α → Sort*} {γ : Π a, β a → Sort*} :
(∀ a (b : β a), γ a b → true) ↔ true :=
forall_true_iff' $ λ _, forall_2_true_iff
@[simp] theorem forall_const (α : Sort*) [inhabited α] : (α → b) ↔ b :=
⟨λ h, h (arbitrary α), λ hb x, hb⟩
@[simp] theorem exists_const (α : Sort*) [inhabited α] : (∃ x : α, b) ↔ b :=
⟨λ ⟨x, h⟩, h, λ h, ⟨arbitrary α, h⟩⟩
theorem forall_and_distrib : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) :=
⟨λ h, ⟨λ x, (h x).left, λ x, (h x).right⟩, λ ⟨h₁, h₂⟩ x, ⟨h₁ x, h₂ x⟩⟩
theorem exists_or_distrib : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) :=
⟨λ ⟨x, hpq⟩, hpq.elim (λ hpx, or.inl ⟨x, hpx⟩) (λ hqx, or.inr ⟨x, hqx⟩),
λ hepq, hepq.elim (λ ⟨x, hpx⟩, ⟨x, or.inl hpx⟩) (λ ⟨x, hqx⟩, ⟨x, or.inr hqx⟩)⟩
@[simp] theorem exists_and_distrib_left {q : Prop} {p : α → Prop} :
(∃x, q ∧ p x) ↔ q ∧ (∃x, p x) :=
⟨λ ⟨x, hq, hp⟩, ⟨hq, x, hp⟩, λ ⟨hq, x, hp⟩, ⟨x, hq, hp⟩⟩
@[simp] theorem exists_and_distrib_right {q : Prop} {p : α → Prop} :
(∃x, p x ∧ q) ↔ (∃x, p x) ∧ q :=
by simp [and_comm]
@[simp] theorem forall_eq {a' : α} : (∀a, a = a' → p a) ↔ p a' :=
⟨λ h, h a' rfl, λ h a e, e.symm ▸ h⟩
@[simp] theorem exists_eq {a' : α} : ∃ a, a = a' := ⟨_, rfl⟩
@[simp] theorem exists_eq_left {a' : α} : (∃ a, a = a' ∧ p a) ↔ p a' :=
⟨λ ⟨a, e, h⟩, e ▸ h, λ h, ⟨_, rfl, h⟩⟩
@[simp] theorem exists_eq_right {a' : α} : (∃ a, p a ∧ a = a') ↔ p a' :=
(exists_congr $ by exact λ a, and.comm).trans exists_eq_left
@[simp] theorem forall_eq' {a' : α} : (∀a, a' = a → p a) ↔ p a' :=
by simp [@eq_comm _ a']
@[simp] theorem exists_eq_left' {a' : α} : (∃ a, a' = a ∧ p a) ↔ p a' :=
by simp [@eq_comm _ a']
@[simp] theorem exists_eq_right' {a' : α} : (∃ a, p a ∧ a' = a) ↔ p a' :=
by simp [@eq_comm _ a']
theorem forall_or_of_or_forall (h : b ∨ ∀x, p x) (x) : b ∨ p x :=
h.imp_right $ λ h₂, h₂ x
theorem forall_or_distrib_left {q : Prop} {p : α → Prop} [decidable q] :
(∀x, q ∨ p x) ↔ q ∨ (∀x, p x) :=
⟨λ h, if hq : q then or.inl hq else or.inr $ λ x, (h x).resolve_left hq,
forall_or_of_or_forall⟩
@[simp] theorem exists_prop {p q : Prop} : (∃ h : p, q) ↔ p ∧ q :=
⟨λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩⟩
@[simp] theorem exists_false : ¬ (∃a:α, false) := assume ⟨a, h⟩, h
theorem Exists.fst {p : b → Prop} : Exists p → b
| ⟨h, _⟩ := h
theorem Exists.snd {p : b → Prop} : ∀ h : Exists p, p h.fst
| ⟨_, h⟩ := h
@[simp] theorem forall_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∀ h' : p, q h') ↔ q h :=
@forall_const (q h) p ⟨h⟩
@[simp] theorem exists_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∃ h' : p, q h') ↔ q h :=
@exists_const (q h) p ⟨h⟩
@[simp] theorem forall_prop_of_false {p : Prop} {q : p → Prop} (hn : ¬ p) : (∀ h' : p, q h') ↔ true :=
iff_true_intro $ λ h, hn.elim h
@[simp] theorem exists_prop_of_false {p : Prop} {q : p → Prop} : ¬ p → ¬ (∃ h' : p, q h') :=
mt Exists.fst
end quantifiers
/- classical versions -/
namespace classical
variables {α : Sort*} {p : α → Prop}
local attribute [instance] prop_decidable
protected theorem not_forall : (¬ ∀ x, p x) ↔ (∃ x, ¬ p x) := not_forall
protected theorem forall_or_distrib_left {q : Prop} {p : α → Prop} :
(∀x, q ∨ p x) ↔ q ∨ (∀x, p x) :=
forall_or_distrib_left
theorem cases {p : Prop → Prop} (h1 : p true) (h2 : p false) : ∀a, p a :=
assume a, cases_on a h1 h2
theorem or_not {p : Prop} : p ∨ ¬ p :=
by_cases or.inl or.inr
protected theorem or_iff_not_imp_left {p q : Prop} : p ∨ q ↔ (¬ p → q) :=
or_iff_not_imp_left
protected theorem or_iff_not_imp_right {p q : Prop} : q ∨ p ↔ (¬ p → q) :=
or_iff_not_imp_right
protected lemma not_not {p : Prop} : ¬¬p ↔ p := not_not
/- use shortened names to avoid conflict when classical namespace is open -/
noncomputable theorem dec (p : Prop) : decidable p := by apply_instance
noncomputable theorem dec_pred (p : α → Prop) : decidable_pred p := by apply_instance
noncomputable theorem dec_rel (p : α → α → Prop) : decidable_rel p := by apply_instance
noncomputable theorem dec_eq (α : Sort*) : decidable_eq α := by apply_instance
@[elab_as_eliminator]
noncomputable def {u} exists_cases {C : Sort u} (H0 : C) (H : ∀ a, p a → C) : C :=
if h : ∃ a, p a then H (classical.some h) (classical.some_spec h) else H0
lemma some_spec2 {α : Type*} {p : α → Prop} {h : ∃a, p a}
(q : α → Prop) (hpq : ∀a, p a → q a) : q (some h) :=
hpq _ $ some_spec _
end classical
@[elab_as_eliminator]
noncomputable def {u} exists.classical_rec_on
{α} {p : α → Prop} (h : ∃ a, p a) {C : Sort u} (H : ∀ a, p a → C) : C :=
H (classical.some h) (classical.some_spec h)
/-
bounded quantifiers
-/
section bounded_quantifiers
variables {α : Sort*} {r p q : α → Prop} {P Q : ∀ x, p x → Prop} {b : Prop}
theorem bex_def : (∃ x (h : p x), q x) ↔ ∃ x, p x ∧ q x :=
⟨λ ⟨x, px, qx⟩, ⟨x, px, qx⟩, λ ⟨x, px, qx⟩, ⟨x, px, qx⟩⟩
theorem bex.elim {b : Prop} : (∃ x h, P x h) → (∀ a h, P a h → b) → b
| ⟨a, h₁, h₂⟩ h' := h' a h₁ h₂
theorem bex.intro (a : α) (h₁ : p a) (h₂ : P a h₁) : ∃ x (h : p x), P x h :=
⟨a, h₁, h₂⟩
theorem ball_congr (H : ∀ x h, P x h ↔ Q x h) :
(∀ x h, P x h) ↔ (∀ x h, Q x h) :=
forall_congr $ λ x, forall_congr (H x)
theorem bex_congr (H : ∀ x h, P x h ↔ Q x h) :
(∃ x h, P x h) ↔ (∃ x h, Q x h) :=
exists_congr $ λ x, exists_congr (H x)
theorem ball.imp_right (H : ∀ x h, (P x h → Q x h))
(h₁ : ∀ x h, P x h) (x h) : Q x h :=
H _ _ $ h₁ _ _
theorem bex.imp_right (H : ∀ x h, (P x h → Q x h)) :
(∃ x h, P x h) → ∃ x h, Q x h
| ⟨x, h, h'⟩ := ⟨_, _, H _ _ h'⟩
theorem ball.imp_left (H : ∀ x, p x → q x)
(h₁ : ∀ x, q x → r x) (x) (h : p x) : r x :=
h₁ _ $ H _ h
theorem bex.imp_left (H : ∀ x, p x → q x) :
(∃ x (_ : p x), r x) → ∃ x (_ : q x), r x
| ⟨x, hp, hr⟩ := ⟨x, H _ hp, hr⟩
theorem ball_of_forall (h : ∀ x, p x) (x) (_ : q x) : p x :=
h x
theorem forall_of_ball (H : ∀ x, p x) (h : ∀ x, p x → q x) (x) : q x :=
h x $ H x
theorem bex_of_exists (H : ∀ x, p x) : (∃ x, q x) → ∃ x (_ : p x), q x
| ⟨x, hq⟩ := ⟨x, H x, hq⟩
theorem exists_of_bex : (∃ x (_ : p x), q x) → ∃ x, q x
| ⟨x, _, hq⟩ := ⟨x, hq⟩
@[simp] theorem bex_imp_distrib : ((∃ x h, P x h) → b) ↔ (∀ x h, P x h → b) :=
by simp
theorem not_bex : (¬ ∃ x h, P x h) ↔ ∀ x h, ¬ P x h :=
bex_imp_distrib
theorem not_ball_of_bex_not : (∃ x h, ¬ P x h) → ¬ ∀ x h, P x h
| ⟨x, h, hp⟩ al := hp $ al x h
theorem not_ball [decidable (∃ x h, ¬ P x h)] [∀ x h, decidable (P x h)] :
(¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) :=
⟨not.imp_symm $ λ nx x h, nx.imp_symm $ λ h', ⟨x, h, h'⟩,
not_ball_of_bex_not⟩
theorem ball_true_iff (p : α → Prop) : (∀ x, p x → true) ↔ true :=
iff_true_intro (λ h hrx, trivial)
theorem ball_and_distrib : (∀ x h, P x h ∧ Q x h) ↔ (∀ x h, P x h) ∧ (∀ x h, Q x h) :=
iff.trans (forall_congr $ λ x, forall_and_distrib) forall_and_distrib
theorem bex_or_distrib : (∃ x h, P x h ∨ Q x h) ↔ (∃ x h, P x h) ∨ (∃ x h, Q x h) :=
iff.trans (exists_congr $ λ x, exists_or_distrib) exists_or_distrib
end bounded_quantifiers
namespace classical
local attribute [instance] prop_decidable
theorem not_ball {α : Sort*} {p : α → Prop} {P : Π (x : α), p x → Prop} :
(¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := _root_.not_ball
end classical
section nonempty
universes u v w
variables {α : Type u} {β : Type v} {γ : α → Type w}
attribute [simp] nonempty_of_inhabited
lemma exists_true_iff_nonempty {α : Sort*} : (∃a:α, true) ↔ nonempty α :=
iff.intro (λ⟨a, _⟩, ⟨a⟩) (λ⟨a⟩, ⟨a, trivial⟩)
@[simp] lemma nonempty_Prop {p : Prop} : nonempty p ↔ p :=
iff.intro (assume ⟨h⟩, h) (assume h, ⟨h⟩)
lemma not_nonempty_iff_imp_false {p : Prop} : ¬ nonempty α ↔ α → false :=
⟨λ h a, h ⟨a⟩, λ h ⟨a⟩, h a⟩
@[simp] lemma nonempty_sigma : nonempty (Σa:α, γ a) ↔ (∃a:α, nonempty (γ a)) :=
iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩)
@[simp] lemma nonempty_subtype {α : Sort u} {p : α → Prop} : nonempty (subtype p) ↔ (∃a:α, p a) :=
iff.intro (assume ⟨⟨a, h⟩⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a, h⟩⟩)
@[simp] lemma nonempty_prod : nonempty (α × β) ↔ (nonempty α ∧ nonempty β) :=
iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩)
@[simp] lemma nonempty_pprod {α : Sort u} {β : Sort v} :
nonempty (pprod α β) ↔ (nonempty α ∧ nonempty β) :=
iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩)
@[simp] lemma nonempty_sum : nonempty (α ⊕ β) ↔ (nonempty α ∨ nonempty β) :=
iff.intro
(assume ⟨h⟩, match h with sum.inl a := or.inl ⟨a⟩ | sum.inr b := or.inr ⟨b⟩ end)
(assume h, match h with or.inl ⟨a⟩ := ⟨sum.inl a⟩ | or.inr ⟨b⟩ := ⟨sum.inr b⟩ end)
@[simp] lemma nonempty_psum {α : Sort u} {β : Sort v} :
nonempty (psum α β) ↔ (nonempty α ∨ nonempty β) :=
iff.intro
(assume ⟨h⟩, match h with psum.inl a := or.inl ⟨a⟩ | psum.inr b := or.inr ⟨b⟩ end)
(assume h, match h with or.inl ⟨a⟩ := ⟨psum.inl a⟩ | or.inr ⟨b⟩ := ⟨psum.inr b⟩ end)
@[simp] lemma nonempty_psigma {α : Sort u} {β : α → Sort v} :
nonempty (psigma β) ↔ (∃a:α, nonempty (β a)) :=
iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩)
@[simp] lemma nonempty_empty : ¬ nonempty empty :=
assume ⟨h⟩, h.elim
@[simp] lemma nonempty_ulift : nonempty (ulift α) ↔ nonempty α :=
iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩)
@[simp] lemma nonempty_plift {α : Sort u} : nonempty (plift α) ↔ nonempty α :=
iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩)
@[simp] lemma nonempty.forall {α : Sort u} {p : nonempty α → Prop} :
(∀h:nonempty α, p h) ↔ (∀a, p ⟨a⟩) :=
iff.intro (assume h a, h _) (assume h ⟨a⟩, h _)
@[simp] lemma nonempty.exists {α : Sort u} {p : nonempty α → Prop} :
(∃h:nonempty α, p h) ↔ (∃a, p ⟨a⟩) :=
iff.intro (assume ⟨⟨a⟩, h⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a⟩, h⟩)
lemma classical.nonempty_pi {α : Sort u} {β : α → Sort v} :
nonempty (Πa:α, β a) ↔ (∀a:α, nonempty (β a)) :=
iff.intro (assume ⟨f⟩ a, ⟨f a⟩) (assume f, ⟨assume a, classical.choice $ f a⟩)
-- inhabited_of_nonempty already exists, in core/init/classical.lean, but the
-- assumption is not [...], which makes it unsuitable for some applications
noncomputable def classical.inhabited_of_nonempty' {α : Sort u} [h : nonempty α] : inhabited α :=
⟨classical.choice h⟩
-- `nonempty` cannot be a `functor`, because `functor` is restricted to Types.
lemma nonempty.map {α : Sort u} {β : Sort v} (f : α → β) : nonempty α → nonempty β
| ⟨h⟩ := ⟨f h⟩
end nonempty
|
19f57f5ca712208fee34941315cb182870dab613 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/topology/uniform_space/uniform_embedding.lean | 7764ede6e6918bfac6feca815f98796b64ac817d | [
"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 | 25,427 | 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, Sébastien Gouëzel, Patrick Massot
-/
import topology.uniform_space.cauchy
import topology.uniform_space.separation
import topology.dense_embedding
/-!
# Uniform embeddings of uniform spaces.
Extension of uniform continuous functions.
-/
open filter topological_space set classical
open_locale classical uniformity topological_space filter
section
variables {α : Type*} {β : Type*} {γ : Type*}
[uniform_space α] [uniform_space β] [uniform_space γ]
universe u
/-- A map `f : α → β` between uniform spaces is called *uniform inducing* if the uniformity filter
on `α` is the pullback of the uniformity filter on `β` under `prod.map f f`. If `α` is a separated
space, then this implies that `f` is injective, hence it is a `uniform_embedding`. -/
structure uniform_inducing (f : α → β) : Prop :=
(comap_uniformity : comap (λx:α×α, (f x.1, f x.2)) (𝓤 β) = 𝓤 α)
lemma uniform_inducing.mk' {f : α → β} (h : ∀ s, s ∈ 𝓤 α ↔
∃ t ∈ 𝓤 β, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s) : uniform_inducing f :=
⟨by simp [eq_comm, filter.ext_iff, subset_def, h]⟩
lemma uniform_inducing.comp {g : β → γ} (hg : uniform_inducing g)
{f : α → β} (hf : uniform_inducing f) : uniform_inducing (g ∘ f) :=
⟨ by rw [show (λ (x : α × α), ((g ∘ f) x.1, (g ∘ f) x.2)) =
(λ y : β × β, (g y.1, g y.2)) ∘ (λ x : α × α, (f x.1, f x.2)), by ext ; simp,
← filter.comap_comap, hg.1, hf.1]⟩
lemma uniform_inducing.basis_uniformity {f : α → β} (hf : uniform_inducing f)
{ι : Sort*} {p : ι → Prop} {s : ι → set (β × β)} (H : (𝓤 β).has_basis p s) :
(𝓤 α).has_basis p (λ i, prod.map f f ⁻¹' s i) :=
hf.1 ▸ H.comap _
/-- A map `f : α → β` between uniform spaces is a *uniform embedding* if it is uniform inducing and
injective. If `α` is a separated space, then the latter assumption follows from the former. -/
structure uniform_embedding (f : α → β) extends uniform_inducing f : Prop :=
(inj : function.injective f)
lemma uniform_embedding_subtype_val {p : α → Prop} :
uniform_embedding (subtype.val : subtype p → α) :=
{ comap_uniformity := rfl,
inj := subtype.val_injective }
lemma uniform_embedding_subtype_coe {p : α → Prop} :
uniform_embedding (coe : subtype p → α) :=
uniform_embedding_subtype_val
lemma uniform_embedding_set_inclusion {s t : set α} (hst : s ⊆ t) :
uniform_embedding (inclusion hst) :=
{ comap_uniformity :=
by { erw [uniformity_subtype, uniformity_subtype, comap_comap], congr },
inj := inclusion_injective hst }
lemma uniform_embedding.comp {g : β → γ} (hg : uniform_embedding g)
{f : α → β} (hf : uniform_embedding f) : uniform_embedding (g ∘ f) :=
{ inj := hg.inj.comp hf.inj,
..hg.to_uniform_inducing.comp hf.to_uniform_inducing }
theorem uniform_embedding_def {f : α → β} :
uniform_embedding f ↔ function.injective f ∧ ∀ s, s ∈ 𝓤 α ↔
∃ t ∈ 𝓤 β, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s :=
begin
split,
{ rintro ⟨⟨h⟩, h'⟩,
rw [eq_comm, filter.ext_iff] at h,
simp [*, subset_def] },
{ rintro ⟨h, h'⟩,
refine uniform_embedding.mk ⟨_⟩ h,
rw [eq_comm, filter.ext_iff],
simp [*, subset_def] }
end
theorem uniform_embedding_def' {f : α → β} :
uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧
∀ s, s ∈ 𝓤 α →
∃ t ∈ 𝓤 β, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s :=
by simp only [uniform_embedding_def, uniform_continuous_def]; exact
⟨λ ⟨I, H⟩, ⟨I, λ s su, (H _).2 ⟨s, su, λ x y, id⟩, λ s, (H s).1⟩,
λ ⟨I, H₁, H₂⟩, ⟨I, λ s, ⟨H₂ s,
λ ⟨t, tu, h⟩, mem_of_superset (H₁ t tu) (λ ⟨a, b⟩, h a b)⟩⟩⟩
lemma equiv.uniform_embedding {α β : Type*} [uniform_space α] [uniform_space β] (f : α ≃ β)
(h₁ : uniform_continuous f) (h₂ : uniform_continuous f.symm) : uniform_embedding f :=
{ comap_uniformity :=
begin
refine le_antisymm _ _,
{ change comap (f.prod_congr f) _ ≤ _,
rw ← map_equiv_symm (f.prod_congr f),
exact h₂ },
{ rw ← map_le_iff_le_comap,
exact h₁ }
end,
inj := f.injective }
theorem uniform_embedding_inl : uniform_embedding (sum.inl : α → α ⊕ β) :=
begin
apply uniform_embedding_def.2 ⟨sum.inl_injective, λ s, ⟨_, _⟩⟩,
{ assume hs,
refine ⟨(λ p : α × α, (sum.inl p.1, sum.inl p.2)) '' s ∪
(λ p : β × β, (sum.inr p.1, sum.inr p.2)) '' univ, _, _⟩,
{ exact union_mem_uniformity_sum hs univ_mem },
{ simp } },
{ rintros ⟨t, ht, h't⟩,
simp only [sum.uniformity, mem_sup, mem_map] at ht,
apply filter.mem_of_superset ht.1,
rintros ⟨x, y⟩ hx,
exact h't _ _ hx }
end
theorem uniform_embedding_inr : uniform_embedding (sum.inr : β → α ⊕ β) :=
begin
apply uniform_embedding_def.2 ⟨sum.inr_injective, λ s, ⟨_, _⟩⟩,
{ assume hs,
refine ⟨(λ p : α × α, (sum.inl p.1, sum.inl p.2)) '' univ ∪
(λ p : β × β, (sum.inr p.1, sum.inr p.2)) '' s, _, _⟩,
{ exact union_mem_uniformity_sum univ_mem hs },
{ simp } },
{ rintros ⟨t, ht, h't⟩,
simp only [sum.uniformity, mem_sup, mem_map] at ht,
apply filter.mem_of_superset ht.2,
rintros ⟨x, y⟩ hx,
exact h't _ _ hx }
end
/-- If the domain of a `uniform_inducing` map `f` is a `separated_space`, then `f` is injective,
hence it is a `uniform_embedding`. -/
protected theorem uniform_inducing.uniform_embedding [separated_space α] {f : α → β}
(hf : uniform_inducing f) :
uniform_embedding f :=
⟨hf, λ x y h, eq_of_uniformity_basis (hf.basis_uniformity (𝓤 β).basis_sets) $
λ s hs, mem_preimage.2 $ mem_uniformity_of_eq hs h⟩
/-- If a map `f : α → β` sends any two distinct points to point that are **not** related by a fixed
`s ∈ 𝓤 β`, then `f` is uniform inducing with respect to the discrete uniformity on `α`:
the preimage of `𝓤 β` under `prod.map f f` is the principal filter generated by the diagonal in
`α × α`. -/
lemma comap_uniformity_of_spaced_out {α} {f : α → β} {s : set (β × β)} (hs : s ∈ 𝓤 β)
(hf : pairwise (λ x y, (f x, f y) ∉ s)) :
comap (prod.map f f) (𝓤 β) = 𝓟 id_rel :=
begin
refine le_antisymm _ (@refl_le_uniformity α (uniform_space.comap f ‹_›)),
calc comap (prod.map f f) (𝓤 β) ≤ comap (prod.map f f) (𝓟 s) : comap_mono (le_principal_iff.2 hs)
... = 𝓟 (prod.map f f ⁻¹' s) : comap_principal
... ≤ 𝓟 id_rel : principal_mono.2 _,
rintro ⟨x, y⟩, simpa [not_imp_not] using hf x y
end
/-- If a map `f : α → β` sends any two distinct points to point that are **not** related by a fixed
`s ∈ 𝓤 β`, then `f` is a uniform embedding with respect to the discrete uniformity on `α`. -/
lemma uniform_embedding_of_spaced_out {α} {f : α → β} {s : set (β × β)} (hs : s ∈ 𝓤 β)
(hf : pairwise (λ x y, (f x, f y) ∉ s)) :
@uniform_embedding α β ⊥ ‹_› f :=
begin
letI : uniform_space α := ⊥, haveI : separated_space α := separated_iff_t2.2 infer_instance,
exact uniform_inducing.uniform_embedding ⟨comap_uniformity_of_spaced_out hs hf⟩
end
lemma uniform_inducing.uniform_continuous {f : α → β}
(hf : uniform_inducing f) : uniform_continuous f :=
by simp [uniform_continuous, hf.comap_uniformity.symm, tendsto_comap]
lemma uniform_inducing.uniform_continuous_iff {f : α → β} {g : β → γ} (hg : uniform_inducing g) :
uniform_continuous f ↔ uniform_continuous (g ∘ f) :=
by { dsimp only [uniform_continuous, tendsto],
rw [← hg.comap_uniformity, ← map_le_iff_le_comap, filter.map_map] }
lemma uniform_inducing.inducing {f : α → β} (h : uniform_inducing f) : inducing f :=
begin
refine ⟨eq_of_nhds_eq_nhds $ assume a, _ ⟩,
rw [nhds_induced, nhds_eq_uniformity, nhds_eq_uniformity, ← h.comap_uniformity,
comap_lift'_eq, comap_lift'_eq2];
{ refl <|> exact monotone_preimage }
end
lemma uniform_inducing.prod {α' : Type*} {β' : Type*} [uniform_space α'] [uniform_space β']
{e₁ : α → α'} {e₂ : β → β'} (h₁ : uniform_inducing e₁) (h₂ : uniform_inducing e₂) :
uniform_inducing (λp:α×β, (e₁ p.1, e₂ p.2)) :=
⟨by simp [(∘), uniformity_prod, h₁.comap_uniformity.symm, h₂.comap_uniformity.symm,
comap_inf, comap_comap]⟩
lemma uniform_inducing.dense_inducing {f : α → β} (h : uniform_inducing f) (hd : dense_range f) :
dense_inducing f :=
{ dense := hd,
induced := h.inducing.induced }
lemma uniform_embedding.embedding {f : α → β} (h : uniform_embedding f) : embedding f :=
{ induced := h.to_uniform_inducing.inducing.induced,
inj := h.inj }
lemma uniform_embedding.dense_embedding {f : α → β} (h : uniform_embedding f) (hd : dense_range f) :
dense_embedding f :=
{ dense := hd,
inj := h.inj,
induced := h.embedding.induced }
lemma closed_embedding_of_spaced_out {α} [topological_space α] [discrete_topology α]
[separated_space β] {f : α → β} {s : set (β × β)} (hs : s ∈ 𝓤 β)
(hf : pairwise (λ x y, (f x, f y) ∉ s)) :
closed_embedding f :=
begin
unfreezingI { rcases (discrete_topology.eq_bot α) with rfl }, letI : uniform_space α := ⊥,
exact { closed_range := is_closed_range_of_spaced_out hs hf,
.. (uniform_embedding_of_spaced_out hs hf).embedding }
end
lemma closure_image_mem_nhds_of_uniform_inducing
{s : set (α×α)} {e : α → β} (b : β)
(he₁ : uniform_inducing e) (he₂ : dense_inducing e) (hs : s ∈ 𝓤 α) :
∃a, closure (e '' {a' | (a, a') ∈ s}) ∈ 𝓝 b :=
have s ∈ comap (λp:α×α, (e p.1, e p.2)) (𝓤 β),
from he₁.comap_uniformity.symm ▸ hs,
let ⟨t₁, ht₁u, ht₁⟩ := this in
have ht₁ : ∀p:α×α, (e p.1, e p.2) ∈ t₁ → p ∈ s, from ht₁,
let ⟨t₂, ht₂u, ht₂s, ht₂c⟩ := comp_symm_of_uniformity ht₁u in
let ⟨t, htu, hts, htc⟩ := comp_symm_of_uniformity ht₂u in
have preimage e {b' | (b, b') ∈ t₂} ∈ comap e (𝓝 b),
from preimage_mem_comap $ mem_nhds_left b ht₂u,
let ⟨a, (ha : (b, e a) ∈ t₂)⟩ := (he₂.comap_nhds_ne_bot _).nonempty_of_mem this in
have ∀b' (s' : set (β × β)), (b, b') ∈ t → s' ∈ 𝓤 β →
({y : β | (b', y) ∈ s'} ∩ e '' {a' : α | (a, a') ∈ s}).nonempty,
from assume b' s' hb' hs',
have preimage e {b'' | (b', b'') ∈ s' ∩ t} ∈ comap e (𝓝 b'),
from preimage_mem_comap $ mem_nhds_left b' $ inter_mem hs' htu,
let ⟨a₂, ha₂s', ha₂t⟩ := (he₂.comap_nhds_ne_bot _).nonempty_of_mem this in
have (e a, e a₂) ∈ t₁,
from ht₂c $ prod_mk_mem_comp_rel (ht₂s ha) $ htc $ prod_mk_mem_comp_rel hb' ha₂t,
have e a₂ ∈ {b'':β | (b', b'') ∈ s'} ∩ e '' {a' | (a, a') ∈ s},
from ⟨ha₂s', mem_image_of_mem _ $ ht₁ (a, a₂) this⟩,
⟨_, this⟩,
have ∀b', (b, b') ∈ t → ne_bot (𝓝 b' ⊓ 𝓟 (e '' {a' | (a, a') ∈ s})),
begin
intros b' hb',
rw [nhds_eq_uniformity, lift'_inf_principal_eq, lift'_ne_bot_iff],
exact assume s, this b' s hb',
exact monotone_preimage.inter monotone_const
end,
have ∀b', (b, b') ∈ t → b' ∈ closure (e '' {a' | (a, a') ∈ s}),
from assume b' hb', by rw [closure_eq_cluster_pts]; exact this b' hb',
⟨a, (𝓝 b).sets_of_superset (mem_nhds_left b htu) this⟩
lemma uniform_embedding_subtype_emb (p : α → Prop) {e : α → β} (ue : uniform_embedding e)
(de : dense_embedding e) : uniform_embedding (dense_embedding.subtype_emb p e) :=
{ comap_uniformity := by simp [comap_comap, (∘), dense_embedding.subtype_emb,
uniformity_subtype, ue.comap_uniformity.symm],
inj := (de.subtype p).inj }
lemma uniform_embedding.prod {α' : Type*} {β' : Type*} [uniform_space α'] [uniform_space β']
{e₁ : α → α'} {e₂ : β → β'} (h₁ : uniform_embedding e₁) (h₂ : uniform_embedding e₂) :
uniform_embedding (λp:α×β, (e₁ p.1, e₂ p.2)) :=
{ inj := h₁.inj.prod_map h₂.inj,
..h₁.to_uniform_inducing.prod h₂.to_uniform_inducing }
lemma is_complete_of_complete_image {m : α → β} {s : set α} (hm : uniform_inducing m)
(hs : is_complete (m '' s)) : is_complete s :=
begin
intros f hf hfs,
rw le_principal_iff at hfs,
obtain ⟨_, ⟨x, hx, rfl⟩, hyf⟩ : ∃ y ∈ m '' s, map m f ≤ 𝓝 y,
from hs (f.map m) (hf.map hm.uniform_continuous)
(le_principal_iff.2 (image_mem_map hfs)),
rw [map_le_iff_le_comap, ← nhds_induced, ← hm.inducing.induced] at hyf,
exact ⟨x, hx, hyf⟩
end
lemma is_complete.complete_space_coe {s : set α} (hs : is_complete s) :
complete_space s :=
complete_space_iff_is_complete_univ.2 $
is_complete_of_complete_image uniform_embedding_subtype_coe.to_uniform_inducing $ by simp [hs]
/-- A set is complete iff its image under a uniform inducing map is complete. -/
lemma is_complete_image_iff {m : α → β} {s : set α} (hm : uniform_inducing m) :
is_complete (m '' s) ↔ is_complete s :=
begin
refine ⟨is_complete_of_complete_image hm, λ c, _⟩,
haveI : complete_space s := c.complete_space_coe,
set m' : s → β := m ∘ coe,
suffices : is_complete (range m'), by rwa [range_comp, subtype.range_coe] at this,
have hm' : uniform_inducing m' := hm.comp uniform_embedding_subtype_coe.to_uniform_inducing,
intros f hf hfm,
rw filter.le_principal_iff at hfm,
have cf' : cauchy (comap m' f) :=
hf.comap' hm'.comap_uniformity.le (ne_bot.comap_of_range_mem hf.1 hfm),
rcases complete_space.complete cf' with ⟨x, hx⟩,
rw [hm'.inducing.nhds_eq_comap, comap_le_comap_iff hfm] at hx,
use [m' x, mem_range_self _, hx]
end
lemma complete_space_iff_is_complete_range {f : α → β} (hf : uniform_inducing f) :
complete_space α ↔ is_complete (range f) :=
by rw [complete_space_iff_is_complete_univ, ← is_complete_image_iff hf, image_univ]
lemma uniform_inducing.is_complete_range [complete_space α] {f : α → β}
(hf : uniform_inducing f) :
is_complete (range f) :=
(complete_space_iff_is_complete_range hf).1 ‹_›
lemma complete_space_congr {e : α ≃ β} (he : uniform_embedding e) :
complete_space α ↔ complete_space β :=
by rw [complete_space_iff_is_complete_range he.to_uniform_inducing, e.range_eq_univ,
complete_space_iff_is_complete_univ]
lemma complete_space_coe_iff_is_complete {s : set α} :
complete_space s ↔ is_complete s :=
(complete_space_iff_is_complete_range uniform_embedding_subtype_coe.to_uniform_inducing).trans $
by rw [subtype.range_coe]
lemma is_closed.complete_space_coe [complete_space α] {s : set α} (hs : is_closed s) :
complete_space s :=
hs.is_complete.complete_space_coe
lemma complete_space_extension {m : β → α} (hm : uniform_inducing m) (dense : dense_range m)
(h : ∀f:filter β, cauchy f → ∃x:α, map m f ≤ 𝓝 x) : complete_space α :=
⟨assume (f : filter α), assume hf : cauchy f,
let
p : set (α × α) → set α → set α := λs t, {y : α| ∃x:α, x ∈ t ∧ (x, y) ∈ s},
g := (𝓤 α).lift (λs, f.lift' (p s))
in
have mp₀ : monotone p,
from assume a b h t s ⟨x, xs, xa⟩, ⟨x, xs, h xa⟩,
have mp₁ : ∀{s}, monotone (p s),
from assume s a b h x ⟨y, ya, yxs⟩, ⟨y, h ya, yxs⟩,
have f ≤ g, from
le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht,
le_principal_iff.mpr $
mem_of_superset ht $ assume x hx, ⟨x, hx, refl_mem_uniformity hs⟩,
have ne_bot g, from hf.left.mono this,
have ne_bot (comap m g), from comap_ne_bot $ assume t ht,
let ⟨t', ht', ht_mem⟩ := (mem_lift_sets $ monotone_lift' monotone_const mp₀).mp ht in
let ⟨t'', ht'', ht'_sub⟩ := (mem_lift'_sets mp₁).mp ht_mem in
let ⟨x, (hx : x ∈ t'')⟩ := hf.left.nonempty_of_mem ht'' in
have h₀ : ne_bot (𝓝[range m] x),
from dense.nhds_within_ne_bot x,
have h₁ : {y | (x, y) ∈ t'} ∈ 𝓝[range m] x,
from @mem_inf_of_left α (𝓝 x) (𝓟 (range m)) _ $ mem_nhds_left x ht',
have h₂ : range m ∈ 𝓝[range m] x,
from @mem_inf_of_right α (𝓝 x) (𝓟 (range m)) _ $ subset.refl _,
have {y | (x, y) ∈ t'} ∩ range m ∈ 𝓝[range m] x,
from @inter_mem α (𝓝[range m] x) _ _ h₁ h₂,
let ⟨y, xyt', b, b_eq⟩ := h₀.nonempty_of_mem this in
⟨b, b_eq.symm ▸ ht'_sub ⟨x, hx, xyt'⟩⟩,
have cauchy g, from
⟨‹ne_bot g›, assume s hs,
let
⟨s₁, hs₁, (comp_s₁ : comp_rel s₁ s₁ ⊆ s)⟩ := comp_mem_uniformity_sets hs,
⟨s₂, hs₂, (comp_s₂ : comp_rel s₂ s₂ ⊆ s₁)⟩ := comp_mem_uniformity_sets hs₁,
⟨t, ht, (prod_t : t ×ˢ t ⊆ s₂)⟩ := mem_prod_same_iff.mp (hf.right hs₂)
in
have hg₁ : p (preimage prod.swap s₁) t ∈ g,
from mem_lift (symm_le_uniformity hs₁) $ @mem_lift' α α f _ t ht,
have hg₂ : p s₂ t ∈ g,
from mem_lift hs₂ $ @mem_lift' α α f _ t ht,
have hg : p (prod.swap ⁻¹' s₁) t ×ˢ p s₂ t ∈ g ×ᶠ g,
from @prod_mem_prod α α _ _ g g hg₁ hg₂,
(g ×ᶠ g).sets_of_superset hg
(assume ⟨a, b⟩ ⟨⟨c₁, c₁t, hc₁⟩, ⟨c₂, c₂t, hc₂⟩⟩,
have (c₁, c₂) ∈ t ×ˢ t, from ⟨c₁t, c₂t⟩,
comp_s₁ $ prod_mk_mem_comp_rel hc₁ $
comp_s₂ $ prod_mk_mem_comp_rel (prod_t this) hc₂)⟩,
have cauchy (filter.comap m g),
from ‹cauchy g›.comap' (le_of_eq hm.comap_uniformity) ‹_›,
let ⟨x, (hx : map m (filter.comap m g) ≤ 𝓝 x)⟩ := h _ this in
have cluster_pt x (map m (filter.comap m g)),
from (le_nhds_iff_adhp_of_cauchy (this.map hm.uniform_continuous)).mp hx,
have cluster_pt x g,
from this.mono map_comap_le,
⟨x, calc f ≤ g : by assumption
... ≤ 𝓝 x : le_nhds_of_cauchy_adhp ‹cauchy g› this⟩⟩
lemma totally_bounded_preimage {f : α → β} {s : set β} (hf : uniform_embedding f)
(hs : totally_bounded s) : totally_bounded (f ⁻¹' s) :=
λ t ht, begin
rw ← hf.comap_uniformity at ht,
rcases mem_comap.2 ht with ⟨t', ht', ts⟩,
rcases totally_bounded_iff_subset.1
(totally_bounded_subset (image_preimage_subset f s) hs) _ ht' with ⟨c, cs, hfc, hct⟩,
refine ⟨f ⁻¹' c, hfc.preimage (hf.inj.inj_on _), λ x h, _⟩,
have := hct (mem_image_of_mem f h), simp at this ⊢,
rcases this with ⟨z, zc, zt⟩,
rcases cs zc with ⟨y, yc, rfl⟩,
exact ⟨y, zc, ts (by exact zt)⟩
end
instance complete_space.sum [complete_space α] [complete_space β] :
complete_space (α ⊕ β) :=
begin
rw complete_space_iff_is_complete_univ,
have A : is_complete (range (sum.inl : α → α ⊕ β)) :=
uniform_embedding_inl.to_uniform_inducing.is_complete_range,
have B : is_complete (range (sum.inr : β → α ⊕ β)) :=
uniform_embedding_inr.to_uniform_inducing.is_complete_range,
convert A.union B,
apply (eq_univ_of_forall (λ x, _)).symm,
cases x,
{ left, exact mem_range_self _ },
{ right, exact mem_range_self _ }
end
end
lemma uniform_embedding_comap {α : Type*} {β : Type*} {f : α → β} [u : uniform_space β]
(hf : function.injective f) : @uniform_embedding α β (uniform_space.comap f u) u f :=
@uniform_embedding.mk _ _ (uniform_space.comap f u) _ _
(@uniform_inducing.mk _ _ (uniform_space.comap f u) _ _ rfl) hf
/-- Pull back a uniform space structure by an embedding, adjusting the new uniform structure to
make sure that its topology is defeq to the original one. -/
def embedding.comap_uniform_space {α β} [topological_space α] [u : uniform_space β] (f : α → β)
(h : embedding f) : uniform_space α :=
(u.comap f).replace_topology h.induced
lemma embedding.to_uniform_embedding {α β} [topological_space α] [u : uniform_space β] (f : α → β)
(h : embedding f) :
@uniform_embedding α β (h.comap_uniform_space f) u f :=
{ comap_uniformity := rfl,
inj := h.inj }
section uniform_extension
variables {α : Type*} {β : Type*} {γ : Type*}
[uniform_space α] [uniform_space β] [uniform_space γ]
{e : β → α}
(h_e : uniform_inducing e)
(h_dense : dense_range e)
{f : β → γ}
(h_f : uniform_continuous f)
local notation `ψ` := (h_e.dense_inducing h_dense).extend f
lemma uniformly_extend_exists [complete_space γ] (a : α) :
∃c, tendsto f (comap e (𝓝 a)) (𝓝 c) :=
let de := (h_e.dense_inducing h_dense) in
have cauchy (𝓝 a), from cauchy_nhds,
have cauchy (comap e (𝓝 a)), from
this.comap' (le_of_eq h_e.comap_uniformity) (de.comap_nhds_ne_bot _),
have cauchy (map f (comap e (𝓝 a))), from this.map h_f,
complete_space.complete this
lemma uniform_extend_subtype [complete_space γ]
{p : α → Prop} {e : α → β} {f : α → γ} {b : β} {s : set α}
(hf : uniform_continuous (λx:subtype p, f x.val))
(he : uniform_embedding e) (hd : ∀x:β, x ∈ closure (range e))
(hb : closure (e '' s) ∈ 𝓝 b) (hs : is_closed s) (hp : ∀x∈s, p x) :
∃c, tendsto f (comap e (𝓝 b)) (𝓝 c) :=
have de : dense_embedding e,
from he.dense_embedding hd,
have de' : dense_embedding (dense_embedding.subtype_emb p e),
by exact de.subtype p,
have ue' : uniform_embedding (dense_embedding.subtype_emb p e),
from uniform_embedding_subtype_emb _ he de,
have b ∈ closure (e '' {x | p x}),
from (closure_mono $ monotone_image $ hp) (mem_of_mem_nhds hb),
let ⟨c, (hc : tendsto (f ∘ subtype.val)
(comap (dense_embedding.subtype_emb p e) (𝓝 ⟨b, this⟩)) (𝓝 c))⟩ :=
uniformly_extend_exists ue'.to_uniform_inducing de'.dense hf _ in
begin
rw [nhds_subtype_eq_comap] at hc,
simp [comap_comap] at hc,
change (tendsto (f ∘ @subtype.val α p) (comap (e ∘ @subtype.val α p) (𝓝 b)) (𝓝 c)) at hc,
rw [←comap_comap, tendsto_comap'_iff] at hc,
exact ⟨c, hc⟩,
exact ⟨_, hb, assume x,
begin
change e x ∈ (closure (e '' s)) → x ∈ range subtype.val,
rw [← closure_induced, mem_closure_iff_cluster_pt, cluster_pt, ne_bot_iff,
nhds_induced, ← de.to_dense_inducing.nhds_eq_comap,
← mem_closure_iff_nhds_ne_bot, hs.closure_eq],
exact assume hxs, ⟨⟨x, hp x hxs⟩, rfl⟩,
end⟩
end
include h_f
lemma uniformly_extend_spec [complete_space γ] (a : α) :
tendsto f (comap e (𝓝 a)) (𝓝 (ψ a)) :=
by simpa only [dense_inducing.extend] using tendsto_nhds_lim (uniformly_extend_exists h_e ‹_› h_f _)
lemma uniform_continuous_uniformly_extend [cγ : complete_space γ] : uniform_continuous ψ :=
assume d hd,
let ⟨s, hs, hs_comp⟩ := (mem_lift'_sets $
monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp
(comp_le_uniformity3 hd) in
have h_pnt : ∀{a m}, m ∈ 𝓝 a → ∃c, c ∈ f '' preimage e m ∧ (c, ψ a) ∈ s ∧ (ψ a, c) ∈ s,
from assume a m hm,
have nb : ne_bot (map f (comap e (𝓝 a))),
from ((h_e.dense_inducing h_dense).comap_nhds_ne_bot _).map _,
have (f '' preimage e m) ∩ ({c | (c, ψ a) ∈ s } ∩ {c | (ψ a, c) ∈ s }) ∈ map f (comap e (𝓝 a)),
from inter_mem (image_mem_map $ preimage_mem_comap $ hm)
(uniformly_extend_spec h_e h_dense h_f _
(inter_mem (mem_nhds_right _ hs) (mem_nhds_left _ hs))),
nb.nonempty_of_mem this,
have preimage (λp:β×β, (f p.1, f p.2)) s ∈ 𝓤 β,
from h_f hs,
have preimage (λp:β×β, (f p.1, f p.2)) s ∈ comap (λx:β×β, (e x.1, e x.2)) (𝓤 α),
by rwa [h_e.comap_uniformity.symm] at this,
let ⟨t, ht, ts⟩ := this in
show preimage (λp:(α×α), (ψ p.1, ψ p.2)) d ∈ 𝓤 α,
from (𝓤 α).sets_of_superset (interior_mem_uniformity ht) $
assume ⟨x₁, x₂⟩ hx_t,
have 𝓝 (x₁, x₂) ≤ 𝓟 (interior t),
from is_open_iff_nhds.mp is_open_interior (x₁, x₂) hx_t,
have interior t ∈ 𝓝 x₁ ×ᶠ 𝓝 x₂,
by rwa [nhds_prod_eq, le_principal_iff] at this,
let ⟨m₁, hm₁, m₂, hm₂, (hm : m₁ ×ˢ m₂ ⊆ interior t)⟩ := mem_prod_iff.mp this in
let ⟨a, ha₁, _, ha₂⟩ := h_pnt hm₁ in
let ⟨b, hb₁, hb₂, _⟩ := h_pnt hm₂ in
have (e ⁻¹' m₁) ×ˢ (e ⁻¹' m₂) ⊆ (λ p : β × β, (f p.1, f p.2)) ⁻¹' s,
from calc _ ⊆ preimage (λp:(β×β), (e p.1, e p.2)) (interior t) : preimage_mono hm
... ⊆ preimage (λp:(β×β), (e p.1, e p.2)) t : preimage_mono interior_subset
... ⊆ preimage (λp:(β×β), (f p.1, f p.2)) s : ts,
have (f '' (e ⁻¹' m₁)) ×ˢ (f '' (e ⁻¹' m₂)) ⊆ s,
from calc (f '' (e ⁻¹' m₁)) ×ˢ (f '' (e ⁻¹' m₂)) =
(λp:(β×β), (f p.1, f p.2)) '' ((e ⁻¹' m₁) ×ˢ (e ⁻¹' m₂)) : prod_image_image_eq
... ⊆ (λp:(β×β), (f p.1, f p.2)) '' ((λp:(β×β), (f p.1, f p.2)) ⁻¹' s) : monotone_image this
... ⊆ s : image_preimage_subset _ _,
have (a, b) ∈ s, from @this (a, b) ⟨ha₁, hb₁⟩,
hs_comp $ show (ψ x₁, ψ x₂) ∈ comp_rel s (comp_rel s s),
from ⟨a, ha₂, ⟨b, this, hb₂⟩⟩
omit h_f
variables [separated_space γ]
lemma uniformly_extend_of_ind (b : β) : ψ (e b) = f b :=
dense_inducing.extend_eq_at _ h_f.continuous.continuous_at
lemma uniformly_extend_unique {g : α → γ} (hg : ∀ b, g (e b) = f b)
(hc : continuous g) :
ψ = g :=
dense_inducing.extend_unique _ hg hc
end uniform_extension
|
85b746cd0269acd3468e56c4aa56e2ccfd0dbc74 | 453dcd7c0d1ef170b0843a81d7d8caedc9741dce | /data/fintype.lean | 47400ead3e10ad726a28b10b54a22ada3219b4c7 | [
"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 | 18,915 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
Finite types.
-/
import data.finset algebra.big_operators data.array.lemmas data.vector2
universes u v
variables {α : Type*} {β : Type*} {γ : Type*}
/-- `fintype α` means that `α` is finite, i.e. there are only
finitely many distinct elements of type `α`. The evidence of this
is a finset `elems` (a list up to permutation without duplicates),
together with a proof that everything of type `α` is in the list. -/
class fintype (α : Type*) :=
(elems : finset α)
(complete : ∀ x : α, x ∈ elems)
namespace finset
variable [fintype α]
/-- `univ` is the universal finite set of type `finset α` implied from
the assumption `fintype α`. -/
def univ : finset α := fintype.elems α
@[simp] theorem mem_univ (x : α) : x ∈ (univ : finset α) :=
fintype.complete x
@[simp] theorem mem_univ_val : ∀ x, x ∈ (univ : finset α).1 := mem_univ
theorem subset_univ (s : finset α) : s ⊆ univ := λ a _, mem_univ a
theorem eq_univ_iff_forall {s : finset α} : s = univ ↔ ∀ x, x ∈ s :=
by simp [ext]
end finset
open finset function
namespace fintype
instance decidable_pi_fintype {α} {β : α → Type*} [fintype α] [∀a, decidable_eq (β a)] :
decidable_eq (Πa, β a) :=
assume f g, decidable_of_iff (∀ a ∈ fintype.elems α, f a = g a)
(by simp [function.funext_iff, fintype.complete])
instance decidable_forall_fintype [fintype α] {p : α → Prop} [decidable_pred p] :
decidable (∀ a, p a) :=
decidable_of_iff (∀ a ∈ @univ α _, p a) (by simp)
instance decidable_exists_fintype [fintype α] {p : α → Prop} [decidable_pred p] :
decidable (∃ a, p a) :=
decidable_of_iff (∃ a ∈ @univ α _, p a) (by simp)
instance decidable_eq_equiv_fintype [fintype α] [decidable_eq β] :
decidable_eq (α ≃ β) :=
λ a b, decidable_of_iff (a.1 = b.1) ⟨λ h, equiv.ext _ _ (congr_fun h), congr_arg _⟩
/-- Construct a proof of `fintype α` from a universal multiset -/
def of_multiset [decidable_eq α] (s : multiset α)
(H : ∀ x : α, x ∈ s) : fintype α :=
⟨s.to_finset, by simpa using H⟩
/-- Construct a proof of `fintype α` from a universal list -/
def of_list [decidable_eq α] (l : list α)
(H : ∀ x : α, x ∈ l) : fintype α :=
⟨l.to_finset, by simpa using H⟩
theorem exists_univ_list (α) [fintype α] :
∃ l : list α, l.nodup ∧ ∀ x : α, x ∈ l :=
let ⟨l, e⟩ := quotient.exists_rep (@univ α _).1 in
by have := and.intro univ.2 mem_univ_val;
exact ⟨_, by rwa ← e at this⟩
/-- `card α` is the number of elements in `α`, defined when `α` is a fintype. -/
def card (α) [fintype α] : ℕ := (@univ α _).card
/-- There is (computably) a bijection between `α` and `fin n` where
`n = card α`. Since it is not unique, and depends on which permutation
of the universe list is used, the bijection is wrapped in `trunc` to
preserve computability. -/
def equiv_fin (α) [fintype α] [decidable_eq α] : trunc (α ≃ fin (card α)) :=
by unfold card finset.card; exact
quot.rec_on_subsingleton (@univ α _).1
(λ l (h : ∀ x:α, x ∈ l) (nd : l.nodup), trunc.mk
⟨λ a, ⟨_, list.index_of_lt_length.2 (h a)⟩,
λ i, l.nth_le i.1 i.2,
λ a, by simp,
λ ⟨i, h⟩, fin.eq_of_veq $ list.nodup_iff_nth_le_inj.1 nd _ _
(list.index_of_lt_length.2 (list.nth_le_mem _ _ _)) h $ by simp⟩)
mem_univ_val univ.2
theorem exists_equiv_fin (α) [fintype α] : ∃ n, nonempty (α ≃ fin n) :=
by haveI := classical.dec_eq α; exact ⟨card α, nonempty_of_trunc (equiv_fin α)⟩
instance (α : Type*) : subsingleton (fintype α) :=
⟨λ ⟨s₁, h₁⟩ ⟨s₂, h₂⟩, by congr; simp [finset.ext, h₁, h₂]⟩
protected def subtype {p : α → Prop} (s : finset α)
(H : ∀ x : α, x ∈ s ↔ p x) : fintype {x // p x} :=
⟨⟨multiset.pmap subtype.mk s.1 (λ x, (H x).1),
multiset.nodup_pmap (λ a _ b _, congr_arg subtype.val) s.2⟩,
λ ⟨x, px⟩, multiset.mem_pmap.2 ⟨x, (H x).2 px, rfl⟩⟩
theorem subtype_card {p : α → Prop} (s : finset α)
(H : ∀ x : α, x ∈ s ↔ p x) :
@card {x // p x} (fintype.subtype s H) = s.card :=
multiset.card_pmap _ _ _
theorem card_of_subtype {p : α → Prop} (s : finset α)
(H : ∀ x : α, x ∈ s ↔ p x) [fintype {x // p x}] :
card {x // p x} = s.card :=
by rw ← subtype_card s H; congr
/-- If `f : α → β` is a bijection and `α` is a fintype, then `β` is also a fintype. -/
def of_bijective [fintype α] (f : α → β) (H : function.bijective f) : fintype β :=
⟨univ.map ⟨f, H.1⟩,
λ b, let ⟨a, e⟩ := H.2 b in e ▸ mem_map_of_mem _ (mem_univ _)⟩
/-- If `f : α → β` is a surjection and `α` is a fintype, then `β` is also a fintype. -/
def of_surjective [fintype α] [decidable_eq β] (f : α → β) (H : function.surjective f) : fintype β :=
⟨univ.image f, λ b, let ⟨a, e⟩ := H b in e ▸ mem_image_of_mem _ (mem_univ _)⟩
/-- If `f : α ≃ β` and `α` is a fintype, then `β` is also a fintype. -/
def of_equiv (α : Type*) [fintype α] (f : α ≃ β) : fintype β := of_bijective _ f.bijective
theorem of_equiv_card [fintype α] (f : α ≃ β) :
@card β (of_equiv α f) = card α :=
multiset.card_map _ _
theorem card_congr {α β} [fintype α] [fintype β] (f : α ≃ β) : card α = card β :=
by rw ← of_equiv_card f; congr
theorem card_eq {α β} [F : fintype α] [G : fintype β] : card α = card β ↔ nonempty (α ≃ β) :=
⟨λ e, match F, G, e with ⟨⟨s, nd⟩, h⟩, ⟨⟨s', nd'⟩, h'⟩, e' := begin
change multiset.card s = multiset.card s' at e',
revert nd nd' h h' e',
refine quotient.induction_on₂ s s' (λ l₁ l₂
(nd₁ : l₁.nodup) (nd₂ : l₂.nodup)
(h₁ : ∀ x, x ∈ l₁) (h₂ : ∀ x, x ∈ l₂)
(e' : l₁.length = l₂.length), _),
haveI := classical.dec_eq α,
refine ⟨equiv.of_bijective ⟨_, _⟩⟩,
{ refine λ a, l₂.nth_le (l₁.index_of a) _,
rw ← e', exact list.index_of_lt_length.2 (h₁ a) },
{ intros a b h, simpa [h₁] using congr_arg l₁.nth
(list.nodup_iff_nth_le_inj.1 nd₂ _ _ _ _ h) },
{ have := classical.dec_eq β,
refine λ b, ⟨l₁.nth_le (l₂.index_of b) _, _⟩,
{ rw e', exact list.index_of_lt_length.2 (h₂ b) },
{ simp [nd₁] } }
end end, λ ⟨f⟩, card_congr f⟩
end fintype
instance (n : ℕ) : fintype (fin n) :=
⟨⟨list.pmap fin.mk (list.range n) (λ a, list.mem_range.1),
list.nodup_pmap (λ a _ b _, congr_arg fin.val) (list.nodup_range _)⟩,
λ ⟨m, h⟩, list.mem_pmap.2 ⟨m, list.mem_range.2 h, rfl⟩⟩
@[simp] theorem fintype.card_fin (n : ℕ) : fintype.card (fin n) = n :=
by rw [fin.fintype]; simp [fintype.card, card, univ]
instance : fintype empty := ⟨∅, empty.rec _⟩
@[simp] theorem fintype.univ_empty : @univ empty _ = ∅ := rfl
@[simp] theorem fintype.card_empty : fintype.card empty = 0 := rfl
instance : fintype unit := ⟨⟨()::0, by simp⟩, λ ⟨⟩, by simp⟩
@[simp] theorem fintype.univ_unit : @univ unit _ = {()} := rfl
@[simp] theorem fintype.card_unit : fintype.card unit = 1 := rfl
instance : fintype bool := ⟨⟨tt::ff::0, by simp⟩, λ x, by cases x; simp⟩
@[simp] theorem fintype.univ_bool : @univ bool _ = {ff, tt} := rfl
instance units_int.fintype : fintype (units ℤ) :=
⟨{1, -1}, λ x, by cases int.units_eq_one_or x; simp *⟩
@[simp] theorem fintype.card_units_int : fintype.card (units ℤ) = 2 := rfl
@[simp] theorem fintype.card_bool : fintype.card bool = 2 := rfl
def finset.insert_none (s : finset α) : finset (option α) :=
⟨none :: s.1.map some, multiset.nodup_cons.2
⟨by simp, multiset.nodup_map (λ a b, option.some.inj) s.2⟩⟩
@[simp] theorem finset.mem_insert_none {s : finset α} : ∀ {o : option α},
o ∈ s.insert_none ↔ ∀ a ∈ o, a ∈ s
| none := iff_of_true (multiset.mem_cons_self _ _) (λ a h, by cases h)
| (some a) := multiset.mem_cons.trans $ by simp; refl
theorem finset.some_mem_insert_none {s : finset α} {a : α} :
some a ∈ s.insert_none ↔ a ∈ s := by simp
instance {α : Type*} [fintype α] : fintype (option α) :=
⟨univ.insert_none, λ a, by simp⟩
@[simp] theorem fintype.card_option {α : Type*} [fintype α] :
fintype.card (option α) = fintype.card α + 1 :=
(multiset.card_cons _ _).trans (by rw multiset.card_map; refl)
instance {α : Type*} (β : α → Type*)
[fintype α] [∀ a, fintype (β a)] : fintype (sigma β) :=
⟨univ.sigma (λ _, univ), λ ⟨a, b⟩, by simp⟩
@[simp] theorem fintype.card_sigma {α : Type*} (β : α → Type*)
[fintype α] [∀ a, fintype (β a)] :
fintype.card (sigma β) = univ.sum (λ a, fintype.card (β a)) :=
card_sigma _ _
instance (α β : Type*) [fintype α] [fintype β] : fintype (α × β) :=
⟨univ.product univ, λ ⟨a, b⟩, by simp⟩
@[simp] theorem fintype.card_prod (α β : Type*) [fintype α] [fintype β] :
fintype.card (α × β) = fintype.card α * fintype.card β :=
card_product _ _
def fintype.fintype_prod_left {α β} [decidable_eq α] [fintype (α × β)] [nonempty β] : fintype α :=
⟨(fintype.elems (α × β)).image prod.fst,
assume a, let ⟨b⟩ := ‹nonempty β› in by simp; exact ⟨b, fintype.complete _⟩⟩
def fintype.fintype_prod_right {α β} [decidable_eq β] [fintype (α × β)] [nonempty α] : fintype β :=
⟨(fintype.elems (α × β)).image prod.snd,
assume b, let ⟨a⟩ := ‹nonempty α› in by simp; exact ⟨a, fintype.complete _⟩⟩
instance (α : Type*) [fintype α] : fintype (ulift α) :=
fintype.of_equiv _ equiv.ulift.symm
@[simp] theorem fintype.card_ulift (α : Type*) [fintype α] :
fintype.card (ulift α) = fintype.card α :=
fintype.of_equiv_card _
instance (α : Type u) (β : Type v) [fintype α] [fintype β] : fintype (α ⊕ β) :=
@fintype.of_equiv _ _ (@sigma.fintype _
(λ b, cond b (ulift α) (ulift.{(max u v) v} β)) _
(λ b, by cases b; apply ulift.fintype))
((equiv.sum_equiv_sigma_bool _ _).symm.trans
(equiv.sum_congr equiv.ulift equiv.ulift))
@[simp] theorem fintype.card_sum (α β : Type*) [fintype α] [fintype β] :
fintype.card (α ⊕ β) = fintype.card α + fintype.card β :=
by rw [sum.fintype, fintype.of_equiv_card]; simp
lemma fintype.card_le_of_injective [fintype α] [fintype β] (f : α → β)
(hf : function.injective f) : fintype.card α ≤ fintype.card β :=
by haveI := classical.prop_decidable; exact
finset.card_le_card_of_inj_on f (λ _ _, finset.mem_univ _) (λ _ _ _ _ h, hf h)
lemma fintype.card_eq_one_iff [fintype α] : fintype.card α = 1 ↔ (∃ x : α, ∀ y, y = x) :=
by rw [← fintype.card_unit, fintype.card_eq]; exact
⟨λ ⟨a⟩, ⟨a.symm (), λ y, a.bijective.1 (subsingleton.elim _ _)⟩,
λ ⟨x, hx⟩, ⟨⟨λ _, (), λ _, x, λ _, (hx _).trans (hx _).symm,
λ _, subsingleton.elim _ _⟩⟩⟩
lemma fintype.card_eq_zero_iff [fintype α] : fintype.card α = 0 ↔ (α → false) :=
⟨λ h a, have e : α ≃ empty := classical.choice (fintype.card_eq.1 (by simp [h])), (e a).elim,
λ h, have e : α ≃ empty := ⟨λ a, (h a).elim, λ a, a.elim, λ a, (h a).elim, λ a, a.elim⟩,
by simp [fintype.card_congr e]⟩
lemma fintype.card_pos_iff [fintype α] : 0 < fintype.card α ↔ nonempty α :=
⟨λ h, classical.by_contradiction (λ h₁,
have fintype.card α = 0 := fintype.card_eq_zero_iff.2 (λ a, h₁ ⟨a⟩),
lt_irrefl 0 $ by rwa this at h),
λ ⟨a⟩, nat.pos_of_ne_zero (mt fintype.card_eq_zero_iff.1 (λ h, h a))⟩
lemma fintype.card_le_one_iff [fintype α] : fintype.card α ≤ 1 ↔ (∀ a b : α, a = b) :=
let n := fintype.card α in
have hn : n = fintype.card α := rfl,
match n, hn with
| 0 := λ ha, ⟨λ h, λ a, (fintype.card_eq_zero_iff.1 ha.symm a).elim, λ _, ha ▸ nat.le_succ _⟩
| 1 := λ ha, ⟨λ h, λ a b, let ⟨x, hx⟩ := fintype.card_eq_one_iff.1 ha.symm in
by rw [hx a, hx b],
λ _, ha ▸ le_refl _⟩
| (n+2) := λ ha, ⟨λ h, by rw ← ha at h; exact absurd h dec_trivial,
(λ h, fintype.card_unit ▸ fintype.card_le_of_injective (λ _, ())
(λ _ _ _, h _ _))⟩
end
lemma fintype.injective_iff_surjective [fintype α] {f : α → α} : injective f ↔ surjective f :=
by haveI := classical.prop_decidable; exact
have ∀ {f : α → α}, injective f → surjective f,
from λ f hinj x,
have h₁ : image f univ = univ := eq_of_subset_of_card_le (subset_univ _)
((card_image_of_injective univ hinj).symm ▸ le_refl _),
have h₂ : x ∈ image f univ := h₁.symm ▸ mem_univ _,
exists_of_bex (mem_image.1 h₂),
⟨this,
λ hsurj, injective_of_has_left_inverse
⟨surj_inv hsurj, left_inverse_of_surjective_of_right_inverse
(this (injective_surj_inv _)) (right_inverse_surj_inv _)⟩⟩
lemma fintype.injective_iff_surjective_of_equiv [fintype α] {f : α → β} (e : α ≃ β) :
injective f ↔ surjective f :=
have injective (e.symm ∘ f) ↔ surjective (e.symm ∘ f), from fintype.injective_iff_surjective,
⟨λ hinj, by simpa [function.comp] using
surjective_comp e.bijective.2 (this.1 (injective_comp e.symm.bijective.1 hinj)),
λ hsurj, by simpa [function.comp] using
injective_comp e.bijective.1 (this.2 (surjective_comp e.symm.bijective.2 hsurj))⟩
instance list.subtype.fintype [decidable_eq α] (l : list α) : fintype {x // x ∈ l} :=
fintype.of_list l.attach l.mem_attach
instance multiset.subtype.fintype [decidable_eq α] (s : multiset α) : fintype {x // x ∈ s} :=
fintype.of_multiset s.attach s.mem_attach
instance finset.subtype.fintype (s : finset α) : fintype {x // x ∈ s} :=
⟨s.attach, s.mem_attach⟩
instance finset_coe.fintype (s : finset α) : fintype (↑s : set α) :=
finset.subtype.fintype s
instance plift.fintype (p : Prop) [decidable p] : fintype (plift p) :=
⟨if h : p then finset.singleton ⟨h⟩ else ∅, λ ⟨h⟩, by simp [h]⟩
instance Prop.fintype : fintype Prop :=
⟨⟨true::false::0, by simp [true_ne_false]⟩,
classical.cases (by simp) (by simp)⟩
def set_fintype {α} [fintype α] (s : set α) [decidable_pred s] : fintype s :=
fintype.subtype (univ.filter (∈ s)) (by simp)
instance pi.fintype {α : Type*} {β : α → Type*}
[fintype α] [decidable_eq α] [∀a, fintype (β a)] : fintype (Πa, β a) :=
@fintype.of_equiv _ _
⟨univ.pi $ λa:α, @univ (β a) _,
λ f, finset.mem_pi.2 $ λ a ha, mem_univ _⟩
⟨λ f a, f a (mem_univ _), λ f a _, f a, λ f, rfl, λ f, rfl⟩
@[simp] lemma fintype.card_pi {β : α → Type*} [fintype α] [decidable_eq α]
[f : Π a, fintype (β a)] : fintype.card (Π a, β a) = univ.prod (λ a, fintype.card (β a)) :=
by letI f' : fintype (Πa∈univ, β a) :=
⟨(univ.pi $ λa, univ), assume f, finset.mem_pi.2 $ assume a ha, mem_univ _⟩;
exact calc fintype.card (Π a, β a) = fintype.card (Π a ∈ univ, β a) : fintype.card_congr
⟨λ f a ha, f a, λ f a, f a (mem_univ a), λ _, rfl, λ _, rfl⟩
... = univ.prod (λ a, fintype.card (β a)) : finset.card_pi _ _
@[simp] lemma fintype.card_fun [fintype α] [decidable_eq α] [fintype β] :
fintype.card (α → β) = fintype.card β ^ fintype.card α :=
by rw [fintype.card_pi, finset.prod_const, nat.pow_eq_pow]; refl
instance d_array.fintype {n : ℕ} {α : fin n → Type*}
[∀n, fintype (α n)] : fintype (d_array n α) :=
fintype.of_equiv _ (equiv.d_array_equiv_fin _).symm
instance array.fintype {n : ℕ} {α : Type*} [fintype α] : fintype (array n α) :=
d_array.fintype
instance vector.fintype {α : Type*} [fintype α] {n : ℕ} : fintype (vector α n) :=
fintype.of_equiv _ (equiv.vector_equiv_fin _ _).symm
instance quotient.fintype [fintype α] (s : setoid α)
[decidable_rel ((≈) : α → α → Prop)] : fintype (quotient s) :=
fintype.of_surjective quotient.mk (λ x, quotient.induction_on x (λ x, ⟨x, rfl⟩))
instance finset.fintype [fintype α] : fintype (finset α) :=
⟨univ.powerset, λ x, finset.mem_powerset.2 (finset.subset_univ _)⟩
instance subtype.fintype [fintype α] (p : α → Prop) [decidable_pred p] : fintype {x // p x} :=
set_fintype _
instance set.fintype [fintype α] [decidable_eq α] : fintype (set α) :=
pi.fintype
instance pfun_fintype (p : Prop) [decidable p] (α : p → Type*)
[Π hp, fintype (α hp)] : fintype (Π hp : p, α hp) :=
if hp : p then fintype.of_equiv (α hp) ⟨λ a _, a, λ f, f hp, λ _, rfl, λ _, rfl⟩
else ⟨singleton (λ h, (hp h).elim), by simp [hp, function.funext_iff]⟩
def quotient.fin_choice_aux {ι : Type*} [decidable_eq ι]
{α : ι → Type*} [S : ∀ i, setoid (α i)] :
∀ (l : list ι), (∀ i ∈ l, quotient (S i)) → @quotient (Π i ∈ l, α i) (by apply_instance)
| [] f := ⟦λ i, false.elim⟧
| (i::l) f := begin
refine quotient.lift_on₂ (f i (list.mem_cons_self _ _))
(quotient.fin_choice_aux l (λ j h, f j (list.mem_cons_of_mem _ h)))
_ _,
exact λ a l, ⟦λ j h,
if e : j = i then by rw e; exact a else
l _ (h.resolve_left e)⟧,
refine λ a₁ l₁ a₂ l₂ h₁ h₂, quotient.sound (λ j h, _),
by_cases e : j = i; simp [e],
{ subst j, exact h₁ },
{ exact h₂ _ _ }
end
theorem quotient.fin_choice_aux_eq {ι : Type*} [decidable_eq ι]
{α : ι → Type*} [S : ∀ i, setoid (α i)] :
∀ (l : list ι) (f : ∀ i ∈ l, α i), quotient.fin_choice_aux l (λ i h, ⟦f i h⟧) = ⟦f⟧
| [] f := quotient.sound (λ i h, h.elim)
| (i::l) f := begin
simp [quotient.fin_choice_aux, quotient.fin_choice_aux_eq l],
refine quotient.sound (λ j h, _),
by_cases e : j = i; simp [e],
subst j, refl
end
def quotient.fin_choice {ι : Type*} [fintype ι] [decidable_eq ι]
{α : ι → Type*} [S : ∀ i, setoid (α i)]
(f : ∀ i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) :=
quotient.lift_on (@quotient.rec_on _ _ (λ l : multiset ι,
@quotient (Π i ∈ l, α i) (by apply_instance))
finset.univ.1
(λ l, quotient.fin_choice_aux l (λ i _, f i))
(λ a b h, begin
have := λ a, quotient.fin_choice_aux_eq a (λ i h, quotient.out (f i)),
simp [quotient.out_eq] at this,
simp [this],
let g := λ a:multiset ι, ⟦λ (i : ι) (h : i ∈ a), quotient.out (f i)⟧,
refine eq_of_heq ((eq_rec_heq _ _).trans (_ : g a == g b)),
congr' 1, exact quotient.sound h,
end))
(λ f, ⟦λ i, f i (finset.mem_univ _)⟧)
(λ a b h, quotient.sound $ λ i, h _ _)
theorem quotient.fin_choice_eq {ι : Type*} [fintype ι] [decidable_eq ι]
{α : ι → Type*} [∀ i, setoid (α i)]
(f : ∀ i, α i) : quotient.fin_choice (λ i, ⟦f i⟧) = ⟦f⟧ :=
begin
let q, swap, change quotient.lift_on q _ _ = _,
have : q = ⟦λ i h, f i⟧,
{ dsimp [q],
exact quotient.induction_on
(@finset.univ ι _).1 (λ l, quotient.fin_choice_aux_eq _ _) },
simp [this], exact setoid.refl _
end |
ba4cbfb5cd0f26b2a0e2359d5857f9a65c9b9891 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/sigma/order.lean | f85e907856eb07e2f14935478aacc7c2c0c1dca2 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 6,605 | lean | /-
Copyright (c) 2021 Yaël Dillies. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies
-/
import data.sigma.lex
import order.bounded_order
/-!
# Orders on a sigma type
This file defines two orders on a sigma type:
* The disjoint sum of orders. `a` is less `b` iff `a` and `b` are in the same summand and `a` is
less than `b` there.
* The lexicographical order. `a` is less than `b` if its summand is strictly less than the summand
of `b` or they are in the same summand and `a` is less than `b` there.
We make the disjoint sum of orders the default set of instances. The lexicographic order goes on a
type synonym.
## Notation
* `Σₗ i, α i`: Sigma type equipped with the lexicographic order. Type synonym of `Σ i, α i`.
## See also
Related files are:
* `data.finset.colex`: Colexicographic order on finite sets.
* `data.list.lex`: Lexicographic order on lists.
* `data.psigma.order`: Lexicographic order on `Σₗ' i, α i`. Basically a twin of this file.
* `order.lexicographic`: Lexicographic order on `α × β`.
## TODO
Prove that a sigma type is a `no_max_order`, `no_min_order`, `densely_ordered` when its summands
are.
Upgrade `equiv.sigma_congr_left`, `equiv.sigma_congr`, `equiv.sigma_assoc`,
`equiv.sigma_prod_of_equiv`, `equiv.sigma_equiv_prod`, ... to order isomorphisms.
-/
namespace sigma
variables {ι : Type*} {α : ι → Type*}
/-! ### Disjoint sum of orders on `sigma` -/
/-- Disjoint sum of orders. `⟨i, a⟩ ≤ ⟨j, b⟩` iff `i = j` and `a ≤ b`. -/
inductive le [Π i, has_le (α i)] : Π a b : Σ i, α i, Prop
| fiber (i : ι) (a b : α i) : a ≤ b → le ⟨i, a⟩ ⟨i, b⟩
/-- Disjoint sum of orders. `⟨i, a⟩ < ⟨j, b⟩` iff `i = j` and `a < b`. -/
inductive lt [Π i, has_lt (α i)] : Π a b : Σ i, α i, Prop
| fiber (i : ι) (a b : α i) : a < b → lt ⟨i, a⟩ ⟨i, b⟩
instance [Π i, has_le (α i)] : has_le (Σ i, α i) := ⟨le⟩
instance [Π i, has_lt (α i)] : has_lt (Σ i, α i) := ⟨lt⟩
@[simp] lemma mk_le_mk_iff [Π i, has_le (α i)] {i : ι} {a b : α i} :
(⟨i, a⟩ : sigma α) ≤ ⟨i, b⟩ ↔ a ≤ b :=
⟨λ ⟨_, _, _, h⟩, h, le.fiber _ _ _⟩
@[simp] lemma mk_lt_mk_iff [Π i, has_lt (α i)] {i : ι} {a b : α i} :
(⟨i, a⟩ : sigma α) < ⟨i, b⟩ ↔ a < b :=
⟨λ ⟨_, _, _, h⟩, h, lt.fiber _ _ _⟩
lemma le_def [Π i, has_le (α i)] {a b : Σ i, α i} : a ≤ b ↔ ∃ h : a.1 = b.1, h.rec a.2 ≤ b.2 :=
begin
split,
{ rintro ⟨i, a, b, h⟩,
exact ⟨rfl, h⟩ },
{ obtain ⟨i, a⟩ := a,
obtain ⟨j, b⟩ := b,
rintro ⟨(rfl : i = j), h⟩,
exact le.fiber _ _ _ h }
end
lemma lt_def [Π i, has_lt (α i)] {a b : Σ i, α i} : a < b ↔ ∃ h : a.1 = b.1, h.rec a.2 < b.2 :=
begin
split,
{ rintro ⟨i, a, b, h⟩,
exact ⟨rfl, h⟩ },
{ obtain ⟨i, a⟩ := a,
obtain ⟨j, b⟩ := b,
rintro ⟨(rfl : i = j), h⟩,
exact lt.fiber _ _ _ h }
end
instance [Π i, preorder (α i)] : preorder (Σ i, α i) :=
{ le_refl := λ ⟨i, a⟩, le.fiber i a a le_rfl,
le_trans := begin
rintro _ _ _ ⟨i, a, b, hab⟩ ⟨_, _, c, hbc⟩,
exact le.fiber i a c (hab.trans hbc),
end,
lt_iff_le_not_le := λ _ _, begin
split,
{ rintro ⟨i, a, b, hab⟩,
rwa [mk_le_mk_iff, mk_le_mk_iff, ←lt_iff_le_not_le] },
{ rintro ⟨⟨i, a, b, hab⟩, h⟩,
rw mk_le_mk_iff at h,
exact mk_lt_mk_iff.2 (hab.lt_of_not_le h) }
end,
.. sigma.has_le,
.. sigma.has_lt }
instance [Π i, partial_order (α i)] : partial_order (Σ i, α i) :=
{ le_antisymm := begin
rintro _ _ ⟨i, a, b, hab⟩ ⟨_, _, _, hba⟩,
exact ext rfl (heq_of_eq $ hab.antisymm hba),
end,
.. sigma.preorder }
/-! ### Lexicographical order on `sigma` -/
namespace lex
notation `Σₗ` binders `, ` r:(scoped p, _root_.lex (sigma p)) := r
/-- The lexicographical `≤` on a sigma type. -/
instance has_le [has_lt ι] [Π i, has_le (α i)] : has_le (Σₗ i, α i) := ⟨lex (<) (λ i, (≤))⟩
/-- The lexicographical `<` on a sigma type. -/
instance has_lt [has_lt ι] [Π i, has_lt (α i)] : has_lt (Σₗ i, α i) := ⟨lex (<) (λ i, (<))⟩
/-- The lexicographical preorder on a sigma type. -/
instance preorder [preorder ι] [Π i, preorder (α i)] : preorder (Σₗ i, α i) :=
{ le_refl := λ ⟨i, a⟩, lex.right a a le_rfl,
le_trans := λ _ _ _, trans_of (lex (<) $ λ _, (≤)),
lt_iff_le_not_le := begin
refine λ a b, ⟨λ hab, ⟨hab.mono_right (λ i a b, le_of_lt), _⟩, _⟩,
{ rintro (⟨j, i, b, a, hji⟩ | ⟨i, b, a, hba⟩);
obtain (⟨_, _, _, _, hij⟩ | ⟨_, _, _, hab⟩) := hab,
{ exact hij.not_lt hji },
{ exact lt_irrefl _ hji },
{ exact lt_irrefl _ hij },
{ exact hab.not_le hba } },
{ rintro ⟨⟨i, j, a, b, hij⟩ |⟨i, a, b, hab⟩, hba⟩,
{ exact lex.left _ _ hij },
{ exact lex.right _ _ (hab.lt_of_not_le $ λ h, hba $ lex.right _ _ h) } }
end,
.. lex.has_le,
.. lex.has_lt }
/-- The lexicographical partial order on a sigma type. -/
instance partial_order [preorder ι] [Π i, partial_order (α i)] :
partial_order (Σₗ i, α i) :=
{ le_antisymm := λ _ _, antisymm_of (lex (<) $ λ _, (≤)),
.. lex.preorder }
/-- The lexicographical linear order on a sigma type. -/
instance linear_order [linear_order ι] [Π i, linear_order (α i)] :
linear_order (Σₗ i, α i) :=
{ le_total := total_of (lex (<) $ λ _, (≤)),
decidable_eq := sigma.decidable_eq,
decidable_le := lex.decidable _ _,
.. lex.partial_order }
/-- The lexicographical linear order on a sigma type. -/
instance order_bot [partial_order ι] [order_bot ι] [Π i, preorder (α i)] [order_bot (α ⊥)] :
order_bot (Σₗ i, α i) :=
{ bot := ⟨⊥, ⊥⟩,
bot_le := λ ⟨a, b⟩, begin
obtain rfl | ha := eq_bot_or_bot_lt a,
{ exact lex.right _ _ bot_le },
{ exact lex.left _ _ ha }
end }
/-- The lexicographical linear order on a sigma type. -/
instance order_top [partial_order ι] [order_top ι] [Π i, preorder (α i)] [order_top (α ⊤)] :
order_top (Σₗ i, α i) :=
{ top := ⟨⊤, ⊤⟩,
le_top := λ ⟨a, b⟩, begin
obtain rfl | ha := eq_top_or_lt_top a,
{ exact lex.right _ _ le_top },
{ exact lex.left _ _ ha }
end }
/-- The lexicographical linear order on a sigma type. -/
instance bounded_order [partial_order ι] [bounded_order ι] [Π i, preorder (α i)]
[order_bot (α ⊥)] [order_top (α ⊤)] :
bounded_order (Σₗ i, α i) :=
{ .. lex.order_bot, .. lex.order_top }
end lex
end sigma
|
0d2b410c21bd03ac3674d71fe217a2da0dacb367 | 367134ba5a65885e863bdc4507601606690974c1 | /src/data/set/disjointed.lean | 2934139b7a90c962958e784eaa609d94737a2eb4 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 4,646 | 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
Disjointed sets
-/
import data.set.lattice
import tactic.wlog
open set classical
open_locale classical
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x}
{s t u : set α}
/-- A relation `p` holds pairwise if `p i j` for all `i ≠ j`. -/
def pairwise {α : Type*} (p : α → α → Prop) := ∀i j, i ≠ j → p i j
theorem set.pairwise_on_univ {r : α → α → Prop} :
(univ : set α).pairwise_on r ↔ pairwise r :=
by simp only [pairwise_on, pairwise, mem_univ, forall_const]
theorem set.pairwise_on.on_injective {s : set α} {r : α → α → Prop} (hs : pairwise_on s r)
{f : β → α} (hf : function.injective f) (hfs : ∀ x, f x ∈ s) :
pairwise (r on f) :=
λ i j hij, hs _ (hfs i) _ (hfs j) (hf.ne hij)
theorem pairwise.mono {p q : α → α → Prop} (h : ∀ ⦃i j⦄, p i j → q i j) (hp : pairwise p) :
pairwise q :=
λ i j hij, h (hp i j hij)
theorem pairwise_on_bool {r} (hr : symmetric r) {a b : α} :
pairwise (r on (λ c, cond c a b)) ↔ r a b :=
by simpa [pairwise, function.on_fun] using @hr a b
theorem pairwise_disjoint_on_bool [semilattice_inf_bot α] {a b : α} :
pairwise (disjoint on (λ c, cond c a b)) ↔ disjoint a b :=
pairwise_on_bool disjoint.symm
theorem pairwise.pairwise_on {p : α → α → Prop} (h : pairwise p) (s : set α) : s.pairwise_on p :=
λ x hx y hy, h x y
theorem pairwise_disjoint_fiber (f : α → β) : pairwise (disjoint on (λ y : β, f ⁻¹' {y})) :=
set.pairwise_on_univ.1 $ pairwise_on_disjoint_fiber f univ
namespace set
/-- If `f : ℕ → set α` is a sequence of sets, then `disjointed f` is
the sequence formed with each set subtracted from the later ones
in the sequence, to form a disjoint sequence. -/
def disjointed (f : ℕ → set α) (n : ℕ) : set α := f n ∩ (⋂i<n, (f i)ᶜ)
lemma disjoint_disjointed {f : ℕ → set α} : pairwise (disjoint on disjointed f) :=
λ i j h, begin
wlog h' : i ≤ j; [skip, {revert a, exact (this h.symm).symm}],
rintro a ⟨⟨h₁, _⟩, h₂, h₃⟩, simp at h₃,
exact h₃ _ (lt_of_le_of_ne h' h) h₁
end
lemma disjoint_disjointed' {f : ℕ → set α} :
∀ i j, i ≠ j → (disjointed f i) ∩ (disjointed f j) = ∅ :=
λ i j hij, disjoint_iff.1 $ disjoint_disjointed i j hij
lemma disjointed_subset {f : ℕ → set α} {n : ℕ} : disjointed f n ⊆ f n := inter_subset_left _ _
lemma Union_lt_succ {f : ℕ → set α} {n} : (⋃i < nat.succ n, f i) = f n ∪ (⋃i < n, f i) :=
ext $ λ a, by simp [nat.lt_succ_iff_lt_or_eq, or_and_distrib_right, exists_or_distrib, or_comm]
lemma Inter_lt_succ {f : ℕ → set α} {n} : (⋂i < nat.succ n, f i) = f n ∩ (⋂i < n, f i) :=
ext $ λ a, by simp [nat.lt_succ_iff_lt_or_eq, or_imp_distrib, forall_and_distrib, and_comm]
lemma subset_Union_disjointed {f : ℕ → set α} {n} : f n ⊆ ⋃ i < n.succ, disjointed f i :=
λ x hx,
have ∃ k, x ∈ f k, from ⟨n, hx⟩,
have hn : ∀ (i : ℕ), i < nat.find this → x ∉ f i,
from assume i, nat.find_min this,
have hlt : nat.find this < n.succ,
from (nat.find_min' this hx).trans_lt n.lt_succ_self,
mem_bUnion hlt ⟨nat.find_spec this, mem_bInter hn⟩
lemma Union_disjointed {f : ℕ → set α} : (⋃n, disjointed f n) = (⋃n, f n) :=
subset.antisymm (Union_subset_Union $ assume i, inter_subset_left _ _)
(Union_subset $ λ n, subset.trans subset_Union_disjointed (bUnion_subset_Union _ _))
lemma disjointed_induct {f : ℕ → set α} {n : ℕ} {p : set α → Prop}
(h₁ : p (f n)) (h₂ : ∀t i, p t → p (t \ f i)) :
p (disjointed f n) :=
begin
rw disjointed,
generalize_hyp : f n = t at h₁ ⊢,
induction n,
case nat.zero { simp [nat.not_lt_zero, h₁] },
case nat.succ : n ih {
rw [Inter_lt_succ, inter_comm ((f n)ᶜ), ← inter_assoc],
exact h₂ _ n ih }
end
lemma disjointed_of_mono {f : ℕ → set α} {n : ℕ} (hf : monotone f) :
disjointed f (n + 1) = f (n + 1) \ f n :=
have (⋂i (h : i < n + 1), (f i)ᶜ) = (f n)ᶜ,
from le_antisymm
(infi_le_of_le n $ infi_le_of_le (nat.lt_succ_self _) $ subset.refl _)
(le_infi $ assume i, le_infi $ assume hi, compl_le_compl $ hf $ nat.le_of_succ_le_succ hi),
by simp [disjointed, this, diff_eq]
lemma Union_disjointed_of_mono {f : ℕ → set α} (hf : monotone f) (n : ℕ) :
(⋃i<n.succ, disjointed f i) = f n :=
subset.antisymm (bUnion_subset $ λ k hk, subset.trans disjointed_subset $ hf $ nat.lt_succ_iff.1 hk)
subset_Union_disjointed
end set
|
94b97ef75606d072f1148081490820477ef79048 | 4e3bf8e2b29061457a887ac8889e88fa5aa0e34c | /lean/love10_denotational_semantics_homework_sheet.lean | adb2502fcb6aaa9a242b5e8333e7c1a59fa120e7 | [] | no_license | mukeshtiwari/logical_verification_2019 | 9f964c067a71f65eb8884743273fbeef99e6503d | 16f62717f55ed5b7b87e03ae0134791a9bef9b9a | refs/heads/master | 1,619,158,844,208 | 1,585,139,500,000 | 1,585,139,500,000 | 249,906,380 | 0 | 0 | null | 1,585,118,728,000 | 1,585,118,727,000 | null | UTF-8 | Lean | false | false | 5,019 | lean | /- LoVe Homework 10: Denotational Semantics -/
import .love10_denotational_semantics_demo
namespace LoVe
/- Denotational semantics are well suited to functional programming. In this
exercise, we will study some representations of functional programs in Lean and
their denotational semantics. -/
/- The `nondet` type represents functional programs that can perform
nondeterministic computations: A program can choose between many different
computation paths / return values. Returning no results at all is represented by
`fail`, and nondeterministic choice between two alternatives (identified by the
`bool` values `tt` and `ff`) is represented by `choice`. -/
inductive nondet (α : Type) : Type
| pure : α → nondet
| fail {} : nondet
| choice : (bool → nondet) → nondet
namespace nondet
/- Question 1: The `nondet` Monad -/
/- The `nondet` inductive type forms a monad. The `pure` operator is
`nondet.pure`; `bind` is as follows: -/
def bind {α β : Type} : nondet α → (α → nondet β) → nondet β
| (pure x) f := f x
| fail f := fail
| (choice k) f := choice (λb, bind (k b) f)
instance : has_pure nondet := { pure := @pure }
instance : has_bind nondet := { bind := @bind }
/- 1.1. Prove the monadic laws (lecture 6) for `nondet`.
Hint: To prove `f = g` from `∀x, f x = g x`, use the theorem `funext`. -/
lemma pure_bind {α β : Type} (x : α) (f : α → nondet β) :
pure x >>= f = f x :=
sorry
lemma bind_pure {α : Type} :
∀mx : nondet α, mx >>= pure = mx
:= sorry
lemma bind_assoc {α β γ : Type} :
∀(mx : nondet α) (f : α → nondet β) (g : β → nondet γ),
((mx >>= f) >>= g) = (mx >>= (λa, f a >>= g))
:= sorry
/- The function `portmanteau` computes a portmanteau of two lists: A portmanteau
of `xs` and `ys` has `xs` as a prefix and `ys` as a suffix, and they overlap. We
use `starts_with xs ys` to test that `ys` has `xs` as a prefix. -/
def starts_with : list ℕ → list ℕ → bool
| (x :: xs) [] := ff
| [] ys := tt
| (x :: xs) (y :: ys) := (x = y) && starts_with xs ys
#eval starts_with [1, 2] [1, 2, 3]
#eval starts_with [1, 2, 3] [1, 2]
def portmanteau : list ℕ → list ℕ → list (list ℕ)
| [] ys := []
| (x :: xs) ys :=
list.map (list.cons x) (portmanteau xs ys) ++
(if starts_with (x :: xs) ys then [ys] else [])
/- Here are some examples of portmanteaux: -/
#reduce portmanteau [0, 1, 2, 3] [2, 3, 4]
#reduce portmanteau [0, 1] [2, 3, 4]
#reduce portmanteau [0, 1, 2, 1, 2] [1, 2, 1, 2, 3, 4]
/- 1.2 (**optional**). Translate the `portmanteau` program from the `list` monad
to the `nondet` monad. -/
def nondet_portmanteau : list ℕ → list ℕ → nondet (list ℕ)
:= sorry
/- Question 2: Nondeterminism, Denotationally -/
/- 2.1. Give a denotational semantics for `nondet`, mapping it into a `list` of
all results. `pure` returns one result, `fail` returns zero, and `choice`
combines the results of either alternative. -/
def list_sem {α : Type} : nondet α → list α
:= sorry
/- Check that the following lines give the same output as for `portmanteau`: -/
#reduce list_sem (nondet_portmanteau [0, 1, 2, 3] [2, 3, 4])
#reduce list_sem (nondet_portmanteau [0, 1] [2, 3, 4])
#reduce list_sem (nondet_portmanteau [0, 1, 2, 1, 2] [1, 2, 1, 2, 3, 4])
/- 2.2. Often, we are not interested in getting all outcomes, just the first
successful one. Give a semantics for `nondet` that produces the first successful
result, if any. -/
def option_sem {α : Type} : nondet α → option α
:= sorry
/- 2.3. Prove the theorem `list_option_compat` below, showing that the two
semantics you defined are compatible. -/
lemma head'_orelse_eq_head'_append {α : Type} (xs ys : list α) :
(list.head' xs <|> list.head' ys) = list.head' (xs ++ ys) :=
by induction xs; simp
theorem list_option_compat {α : Type} :
∀mx : nondet α, option_sem mx = list.head' (list_sem mx)
:= sorry
/- Question 3 (**optional**). Nondeterminism, Operationally -/
/- We can define the following big-step operational semantics for `nondet`: -/
inductive big_step {α : Type} : nondet α → α → Prop
| pure {x : α} :
big_step (pure x) x
| choice_l {k : bool → nondet α} {x : α} :
big_step (k ff) x → big_step (choice k) x
| choice_r {k : bool → nondet α} {x : α} :
big_step (k tt) x → big_step (choice k) x
-- there is no case for `fail`
notation mx `⟹` x := big_step mx x
/- 3.1 (**optional**). Prove the following lemma.
The lemma states that `choice` has the semantics of "angelic nondeterminism": If
there is a computational path that leads to some `x`, the `choice` operator will
produce this `x`. -/
lemma choice_existential {α : Type} (x : α) (k : bool → nondet α) :
nondet.choice k ⟹ x ↔ ∃b, k b ⟹ x :=
sorry
/- 3.2 (**optional**). Prove the compatibility between denotational and
operational semantics. -/
theorem den_op_compat {α : Type} :
∀(x : α) (mx : nondet α), x ∈ list_sem mx ↔ mx ⟹ x
:= sorry
end nondet
end LoVe
|
d5213872d7d051be1692f31924662c3c06c9df21 | bdb33f8b7ea65f7705fc342a178508e2722eb851 | /logic/schroeder_bernstein.lean | ba43717647c3b689f8b7dcdd020a5f83f7d133b2 | [
"Apache-2.0"
] | permissive | rwbarton/mathlib | 939ae09bf8d6eb1331fc2f7e067d39567e10e33d | c13c5ea701bb1eec057e0a242d9f480a079105e9 | refs/heads/master | 1,584,015,335,862 | 1,524,142,167,000 | 1,524,142,167,000 | 130,614,171 | 0 | 0 | Apache-2.0 | 1,548,902,667,000 | 1,524,437,371,000 | Lean | UTF-8 | Lean | false | false | 5,471 | 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
The Schröder-Bernstein theorem, and well ordering of cardinals.
-/
import order.fixed_points data.set.lattice logic.function logic.embedding order.zorn
open lattice set classical
local attribute [instance] prop_decidable
universes u v
namespace function
namespace embedding
section antisymm
variables {α : Type u} {β : Type v}
theorem schroeder_bernstein {f : α → β} {g : β → α}
(hf : injective f) (hg : injective g) : ∃h:α→β, bijective h :=
let s : set α := lfp $ λs, - (g '' - (f '' s)) in
have hs : s = - (g '' - (f '' s)),
from lfp_eq $ assume s t h,
compl_subset_compl_iff_subset.mpr $ image_subset _ $
compl_subset_compl_iff_subset.mpr $ image_subset _ h,
have hns : - s = g '' - (f '' s),
from lattice.neg_eq_neg_of_eq $ by simp [hs.symm],
let g' := λa, @inv_fun β ⟨f a⟩ α g a in
have g'g : g' ∘ g = id,
from funext $ assume b, @left_inverse_inv_fun _ ⟨f (g b)⟩ _ _ hg b,
have hg'ns : g' '' (-s) = - (f '' s),
by rw [hns, ←image_comp, g'g, image_id],
let h := λa, if a ∈ s then f a else g' a in
have h '' univ = univ,
from calc h '' univ = h '' s ∪ h '' (- s) : by rw [←image_union, union_compl_self]
... = f '' s ∪ g' '' (-s) :
congr (congr_arg (∪)
(image_congr $ by simp [h, if_pos] {contextual := tt}))
(image_congr $ by simp [h, if_neg] {contextual := tt})
... = univ : by rw [hg'ns, union_compl_self],
have surjective h,
from assume b,
have b ∈ h '' univ, by rw [this]; trivial,
let ⟨a, _, eq⟩ := this in
⟨a, eq⟩,
have split : ∀x∈s, ∀y∉s, h x = h y → false,
from assume x hx y hy eq,
have y ∈ g '' - (f '' s), by rwa [←hns],
let ⟨y', hy', eq_y'⟩ := this in
have f x = y',
from calc f x = g' y : by simp [h, hx, hy, if_pos, if_neg] at eq; assumption
... = (g' ∘ g) y' : by simp [(∘), eq_y']
... = _ : by simp [g'g],
have y' ∈ f '' s, from this ▸ mem_image_of_mem _ hx,
hy' this,
have injective h,
from assume x y eq,
by_cases
(assume hx : x ∈ s, by_cases
(assume hy : y ∈ s, by simp [h, hx, hy, if_pos, if_neg] at eq; exact hf eq)
(assume hy : y ∉ s, (split x hx y hy eq).elim))
(assume hx : x ∉ s, by_cases
(assume hy : y ∈ s, (split y hy x hx eq.symm).elim)
(assume hy : y ∉ s,
have x ∈ g '' - (f '' s), by rwa [←hns],
let ⟨x', hx', eqx⟩ := this in
have y ∈ g '' - (f '' s), by rwa [←hns],
let ⟨y', hy', eqy⟩ := this in
have g' x = g' y, by simp [h, hx, hy, if_pos, if_neg] at eq; assumption,
have (g' ∘ g) x' = (g' ∘ g) y', by simp [(∘), eqx, eqy, this],
have x' = y', by rwa [g'g] at this,
calc x = g x' : eqx.symm
... = g y' : by rw [this]
... = y : eqy)),
⟨h, ‹injective h›, ‹surjective h›⟩
theorem antisymm : (α ↪ β) → (β ↪ α) → nonempty (α ≃ β)
| ⟨e₁, h₁⟩ ⟨e₂, h₂⟩ :=
let ⟨f, hf⟩ := schroeder_bernstein h₁ h₂ in
⟨equiv.of_bijective hf⟩
end antisymm
section wo
parameters {ι : Type u} {β : ι → Type v}
private def sets := {s : set (∀ i, β i) //
∀ (x ∈ s) (y ∈ s) i, (x : ∀ i, β i) i = y i → x = y}
private def sets.partial_order : partial_order sets :=
{ le := λ s t, s.1 ⊆ t.1,
le_refl := λ s, subset.refl _,
le_trans := λ s t u, subset.trans,
le_antisymm := λ s t h₁ h₂, subtype.eq (subset.antisymm h₁ h₂) }
local attribute [instance] sets.partial_order
theorem injective_min (I : nonempty ι) : ∃ i, nonempty (∀ j, β i ↪ β j) :=
let ⟨⟨s, hs⟩, ms⟩ := show ∃s:sets, ∀a, s ≤ a → a = s, from
zorn.zorn_partial_order $ λ c hc,
⟨⟨⋃₀ (subtype.val '' c),
λ x ⟨_, ⟨⟨s, hs⟩, sc, rfl⟩, xs⟩ y ⟨_, ⟨⟨t, ht⟩, tc, rfl⟩, yt⟩,
(hc.total sc tc).elim (λ h, ht _ (h xs) _ yt) (λ h, hs _ xs _ (h yt))⟩,
λ ⟨s, hs⟩ sc x h, ⟨s, ⟨⟨s, hs⟩, sc, rfl⟩, h⟩⟩ in
let ⟨i, e⟩ := show ∃ i, ∀ y, ∃ x ∈ s, (x : ∀ i, β i) i = y, from
classical.by_contradiction $ λ h,
have h : ∀ i, ∃ y, ∀ x ∈ s, (x : ∀ i, β i) i ≠ y,
by simpa [classical.not_forall] using h,
let ⟨f, hf⟩ := axiom_of_choice h in
have f ∈ (⟨s, hs⟩:sets).1, from
let s' : sets := ⟨insert f s, λ x hx y hy, begin
cases hx; cases hy, {simp [hx, hy]},
{ subst x, exact λ i e, (hf i y hy e.symm).elim },
{ subst y, exact λ i e, (hf i x hx e).elim },
{ exact hs x hx y hy }
end⟩ in ms s' (subset_insert f s) ▸ mem_insert _ _,
let ⟨i⟩ := I in hf i f this rfl in
let ⟨f, hf⟩ := axiom_of_choice e in
⟨i, ⟨λ j, ⟨λ a, f a j, λ a b e',
let ⟨sa, ea⟩ := hf a, ⟨sb, eb⟩ := hf b in
by rw [← ea, ← eb, hs _ sa _ sb _ e']⟩⟩⟩
end wo
theorem total {α : Type u} {β : Type v} : nonempty (α ↪ β) ∨ nonempty (β ↪ α) :=
match @injective_min bool (λ b, cond b (ulift α) (ulift.{(max u v) v} β)) ⟨tt⟩ with
| ⟨tt, ⟨h⟩⟩ := let ⟨f, hf⟩ := h ff in or.inl ⟨embedding.congr equiv.ulift equiv.ulift ⟨f, hf⟩⟩
| ⟨ff, ⟨h⟩⟩ := let ⟨f, hf⟩ := h tt in or.inr ⟨embedding.congr equiv.ulift equiv.ulift ⟨f, hf⟩⟩
end
end embedding
end function
|
08dbf5d33841a4ba4b50ad93876ca2e330e3e2f9 | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/geometry/manifold/smooth_manifold_with_corners.lean | 50ad9e83cebbb095f0a306e25d8b3aa01aa51ec5 | [
"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 | 39,411 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import analysis.calculus.times_cont_diff
import geometry.manifold.charted_space
/-!
# Smooth manifolds (possibly with boundary or corners)
A smooth manifold is a manifold modelled on a normed vector space, or a subset like a
half-space (to get manifolds with boundaries) for which the changes of coordinates are smooth maps.
We define a model with corners as a map `I : H → E` embedding nicely the topological space `H` in
the vector space `E` (or more precisely as a structure containing all the relevant properties).
Given such a model with corners `I` on `(E, H)`, we define the groupoid of local
homeomorphisms of `H` which are smooth when read in `E` (for any regularity `n : with_top ℕ`).
With this groupoid at hand and the general machinery of charted spaces, we thus get the notion
of `C^n` manifold with respect to any model with corners `I` on `(E, H)`. We also introduce a
specific type class for `C^∞` manifolds as these are the most commonly used.
## Main definitions
* `model_with_corners 𝕜 E H` :
a structure containing informations on the way a space `H` embeds in a
model vector space E over the field `𝕜`. This is all that is needed to
define a smooth manifold with model space `H`, and model vector space `E`.
* `model_with_corners_self 𝕜 E` :
trivial model with corners structure on the space `E` embedded in itself by the identity.
* `times_cont_diff_groupoid n I` :
when `I` is a model with corners on `(𝕜, E, H)`, this is the groupoid of local homeos of `H`
which are of class `C^n` over the normed field `𝕜`, when read in `E`.
* `smooth_manifold_with_corners I M` :
a type class saying that the charted space `M`, modelled on the space `H`, has `C^∞` changes of
coordinates with respect to the model with corners `I` on `(𝕜, E, H)`. This type class is just
a shortcut for `has_groupoid M (times_cont_diff_groupoid ∞ I)`.
* `ext_chart_at I x`:
in a smooth manifold with corners with the model `I` on `(E, H)`, the charts take values in `H`,
but often we may want to use their `E`-valued version, obtained by composing the charts with `I`.
Since the target is in general not open, we can not register them as local homeomorphisms, but
we register them as local equivs. `ext_chart_at I x` is the canonical such local equiv around `x`.
As specific examples of models with corners, we define (in the file `real_instances.lean`)
* `model_with_corners_self ℝ (euclidean_space (fin n))` for the model space used to define
`n`-dimensional real manifolds without boundary (with notation `𝓡 n` in the locale `manifold`)
* `model_with_corners ℝ (euclidean_space (fin n)) (euclidean_half_space n)` for the model space
used to define `n`-dimensional real manifolds with boundary (with notation `𝓡∂ n` in the locale
`manifold`)
* `model_with_corners ℝ (euclidean_space (fin n)) (euclidean_quadrant n)` for the model space used
to define `n`-dimensional real manifolds with corners
With these definitions at hand, to invoke an `n`-dimensional real manifold without boundary,
one could use
`variables {n : ℕ} {M : Type*} [topological_space M] [charted_space (euclidean_space (fin n)) M]
[smooth_manifold_with_corners (𝓡 n) M]`.
However, this is not the recommended way: a theorem proved using this assumption would not apply
for instance to the tangent space of such a manifold, which is modelled on
`(euclidean_space (fin n)) × (euclidean_space (fin n))` and not on `euclidean_space (fin (2 * n))`!
In the same way, it would not apply to product manifolds, modelled on
`(euclidean_space (fin n)) × (euclidean_space (fin m))`.
The right invocation does not focus on one specific construction, but on all constructions sharing
the right properties, like
`variables {E : Type*} [normed_group E] [normed_space ℝ E] [finite_dimensional ℝ E]
{I : model_with_corners ℝ E E} [I.boundaryless]
{M : Type*} [topological_space M] [charted_space E M] [smooth_manifold_with_corners I M]`
Here, `I.boundaryless` is a typeclass property ensuring that there is no boundary (this is for
instance the case for `model_with_corners_self`, or products of these). Note that one could consider
as a natural assumption to only use the trivial model with corners `model_with_corners_self ℝ E`,
but again in product manifolds the natural model with corners will not be this one but the product
one (and they are not defeq as `(λp : E × F, (p.1, p.2))` is not defeq to the identity). So, it is
important to use the above incantation to maximize the applicability of theorems.
## Implementation notes
We want to talk about manifolds modelled on a vector space, but also on manifolds with
boundary, modelled on a half space (or even manifolds with corners). For the latter examples,
we still want to define smooth functions, tangent bundles, and so on. As smooth functions are
well defined on vector spaces or subsets of these, one could take for model space a subtype of a
vector space. With the drawback that the whole vector space itself (which is the most basic
example) is not directly a subtype of itself: the inclusion of `univ : set E` in `set E` would
show up in the definition, instead of `id`.
A good abstraction covering both cases it to have a vector
space `E` (with basic example the Euclidean space), a model space `H` (with basic example the upper
half space), and an embedding of `H` into `E` (which can be the identity for `H = E`, or
`subtype.val` for manifolds with corners). We say that the pair `(E, H)` with their embedding is a
model with corners, and we encompass all the relevant properties (in particular the fact that the
image of `H` in `E` should have unique differentials) in the definition of `model_with_corners`.
We concentrate on `C^∞` manifolds: all the definitions work equally well for `C^n` manifolds, but
later on it is a pain to carry all over the smoothness parameter, especially when one wants to deal
with `C^k` functions as there would be additional conditions `k ≤ n` everywhere. Since one deals
almost all the time with `C^∞` (or analytic) manifolds, this seems to be a reasonable choice that
one could revisit later if needed. `C^k` manifolds are still available, but they should be called
using `has_groupoid M (times_cont_diff_groupoid k I)` where `I` is the model with corners.
I have considered using the model with corners `I` as a typeclass argument, possibly `out_param`, to
get lighter notations later on, but it did not turn out right, as on `E × F` there are two natural
model with corners, the trivial (identity) one, and the product one, and they are not defeq and one
needs to indicate to Lean which one we want to use.
This means that when talking on objects on manifolds one will most often need to specify the model
with corners one is using. For instance, the tangent bundle will be `tangent_bundle I M` and the
derivative will be `mfderiv I I' f`, instead of the more natural notations `tangent_bundle 𝕜 M` and
`mfderiv 𝕜 f` (the field has to be explicit anyway, as some manifolds could be considered both as
real and complex manifolds).
-/
noncomputable theory
universes u v w u' v' w'
open set filter
open_locale manifold filter topological_space
localized "notation `∞` := (⊤ : with_top ℕ)" in manifold
section model_with_corners
/-! ### Models with corners. -/
/-- A structure containing informations on the way a space `H` embeds in a
model vector space `E` over the field `𝕜`. This is all what is needed to
define a smooth manifold with model space `H`, and model vector space `E`.
-/
@[nolint has_inhabited_instance]
structure model_with_corners (𝕜 : Type*) [nondiscrete_normed_field 𝕜]
(E : Type*) [normed_group E] [normed_space 𝕜 E] (H : Type*) [topological_space H]
extends local_equiv H E :=
(source_eq : source = univ)
(unique_diff' : unique_diff_on 𝕜 (range to_fun))
(continuous_to_fun : continuous to_fun . tactic.interactive.continuity')
(continuous_inv_fun : continuous inv_fun . tactic.interactive.continuity')
attribute [simp, mfld_simps] model_with_corners.source_eq
/-- A vector space is a model with corners. -/
def model_with_corners_self (𝕜 : Type*) [nondiscrete_normed_field 𝕜]
(E : Type*) [normed_group E] [normed_space 𝕜 E] : model_with_corners 𝕜 E E :=
{ to_fun := id,
inv_fun := id,
source := univ,
target := univ,
source_eq := rfl,
map_source' := λ_ _, mem_univ _,
map_target' := λ_ _, mem_univ _,
left_inv' := λ_ _, rfl,
right_inv' := λ_ _, rfl,
unique_diff' := by { rw range_id, exact unique_diff_on_univ },
continuous_to_fun := continuous_id,
continuous_inv_fun := continuous_id }
localized "notation `𝓘(` 𝕜 `, ` E `)` := model_with_corners_self 𝕜 E" in manifold
localized "notation `𝓘(` 𝕜 `)` := model_with_corners_self 𝕜 𝕜" in manifold
section
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H]
(I : model_with_corners 𝕜 E H)
namespace model_with_corners
instance : has_coe_to_fun (model_with_corners 𝕜 E H) := ⟨_, λ e, e.to_fun⟩
/-- The inverse to a model with corners, only registered as a local equiv. -/
protected def symm : local_equiv E H := I.to_local_equiv.symm
/- Register a few lemmas to make sure that `simp` puts expressions in normal form -/
@[simp, mfld_simps] lemma to_local_equiv_coe : (I.to_local_equiv : H → E) = I :=
rfl
@[simp, mfld_simps] lemma mk_coe (e : local_equiv H E) (a b c d) :
((model_with_corners.mk e a b c d : model_with_corners 𝕜 E H) : H → E) = (e : H → E) := rfl
@[simp, mfld_simps] lemma to_local_equiv_coe_symm : (I.to_local_equiv.symm : E → H) = I.symm := rfl
@[simp, mfld_simps] lemma mk_symm (e : local_equiv H E) (a b c d) :
(model_with_corners.mk e a b c d : model_with_corners 𝕜 E H).symm = e.symm :=
rfl
protected lemma unique_diff : unique_diff_on 𝕜 (range I) := I.unique_diff'
@[continuity] protected lemma continuous : continuous I := I.continuous_to_fun
protected lemma continuous_at {x} : continuous_at I x := I.continuous.continuous_at
protected lemma continuous_within_at {s x} : continuous_within_at I s x :=
I.continuous_at.continuous_within_at
@[continuity] lemma continuous_symm : continuous I.symm := I.continuous_inv_fun
lemma continuous_at_symm {x} : continuous_at I.symm x := I.continuous_symm.continuous_at
lemma continuous_within_at_symm {s x} : continuous_within_at I.symm s x :=
I.continuous_symm.continuous_within_at
@[simp, mfld_simps] lemma target_eq : I.target = range (I : H → E) :=
by { rw [← image_univ, ← I.source_eq], exact (I.to_local_equiv.image_source_eq_target).symm }
@[simp, mfld_simps] protected lemma left_inv (x : H) : I.symm (I x) = x :=
by { refine I.left_inv' _, simp }
protected lemma left_inverse : function.left_inverse I.symm I := I.left_inv
@[simp, mfld_simps] lemma symm_comp_self : I.symm ∘ I = id :=
I.left_inverse.comp_eq_id
protected lemma right_inv_on : right_inv_on I.symm I (range I) :=
I.left_inverse.right_inv_on_range
@[simp, mfld_simps] protected lemma right_inv {x : E} (hx : x ∈ range I) : I (I.symm x) = x :=
I.right_inv_on hx
protected lemma image_eq (s : set H) : I '' s = I.symm ⁻¹' s ∩ range I :=
begin
refine (I.to_local_equiv.image_eq_target_inter_inv_preimage _).trans _,
{ rw I.source_eq, exact subset_univ _ },
{ rw [inter_comm, I.target_eq, I.to_local_equiv_coe_symm] }
end
protected lemma closed_embedding : closed_embedding I :=
I.left_inverse.closed_embedding I.continuous_symm I.continuous
lemma closed_range : is_closed (range I) :=
I.closed_embedding.closed_range
lemma map_nhds_eq (x : H) : map I (𝓝 x) = 𝓝[range I] (I x) :=
I.closed_embedding.to_embedding.map_nhds_eq x
lemma image_mem_nhds_within {x : H} {s : set H} (hs : s ∈ 𝓝 x) :
I '' s ∈ 𝓝[range I] (I x) :=
I.map_nhds_eq x ▸ image_mem_map hs
lemma symm_map_nhds_within_range (x : H) :
map I.symm (𝓝[range I] (I x)) = 𝓝 x :=
by rw [← I.map_nhds_eq, map_map, I.symm_comp_self, map_id]
lemma unique_diff_preimage {s : set H} (hs : is_open s) :
unique_diff_on 𝕜 (I.symm ⁻¹' s ∩ range I) :=
by { rw inter_comm, exact I.unique_diff.inter (hs.preimage I.continuous_inv_fun) }
lemma unique_diff_preimage_source {β : Type*} [topological_space β]
{e : local_homeomorph H β} : unique_diff_on 𝕜 (I.symm ⁻¹' (e.source) ∩ range I) :=
I.unique_diff_preimage e.open_source
lemma unique_diff_at_image {x : H} : unique_diff_within_at 𝕜 (range I) (I x) :=
I.unique_diff _ (mem_range_self _)
protected lemma locally_compact [locally_compact_space E] (I : model_with_corners 𝕜 E H) :
locally_compact_space H :=
begin
have : ∀ (x : H), (𝓝 x).has_basis (λ s, s ∈ 𝓝 (I x) ∧ is_compact s)
(λ s, I.symm '' (s ∩ range ⇑I)),
{ intro x,
rw ← I.symm_map_nhds_within_range,
exact ((compact_basis_nhds (I x)).inf_principal _).map _ },
refine locally_compact_space_of_has_basis this _,
rintro x s ⟨-, hsc⟩,
exact (hsc.inter_right I.closed_range).image I.continuous_symm
end
end model_with_corners
section
variables (𝕜 E)
/-- In the trivial model with corners, the associated local equiv is the identity. -/
@[simp, mfld_simps] lemma model_with_corners_self_local_equiv :
(𝓘(𝕜, E)).to_local_equiv = local_equiv.refl E := rfl
@[simp, mfld_simps] lemma model_with_corners_self_coe :
(𝓘(𝕜, E) : E → E) = id := rfl
@[simp, mfld_simps] lemma model_with_corners_self_coe_symm :
(𝓘(𝕜, E).symm : E → E) = id := rfl
end
end
section model_with_corners_prod
/-- Given two model_with_corners `I` on `(E, H)` and `I'` on `(E', H')`, we define the model with
corners `I.prod I'` on `(E × E', H × H')`. This appears in particular for the manifold structure on
the tangent bundle to a manifold modelled on `(E, H)`: it will be modelled on `(E × E, H × E)`. -/
def model_with_corners.prod
{𝕜 : Type u} [nondiscrete_normed_field 𝕜]
{E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H]
(I : model_with_corners 𝕜 E H)
{E' : Type v'} [normed_group E'] [normed_space 𝕜 E'] {H' : Type w'} [topological_space H']
(I' : model_with_corners 𝕜 E' H') : model_with_corners 𝕜 (E × E') (model_prod H H') :=
{ to_fun := λ p, (I p.1, I' p.2),
inv_fun := λ p, (I.symm p.1, I'.symm p.2),
source := (univ : set (H × H')),
target := set.prod (range I) (range I'),
map_source' := λ ⟨x, x'⟩ _, by simp [-mem_range, mem_range_self],
map_target' := λ ⟨x, x'⟩ _, mem_univ _,
left_inv' := λ ⟨x, x'⟩ _, by simp,
right_inv' := λ ⟨x, x'⟩ ⟨hx, hx'⟩, by simp [hx, hx'],
source_eq := rfl,
unique_diff' := begin
have : range (λ(p : model_prod H H'), (I p.1, I' p.2)) = set.prod (range I) (range I'),
by { dsimp [model_prod], rw ← prod_range_range_eq },
rw this,
exact unique_diff_on.prod I.unique_diff I'.unique_diff,
end,
continuous_to_fun := (continuous.comp I.continuous_to_fun continuous_fst).prod_mk
(continuous.comp I'.continuous_to_fun continuous_snd),
continuous_inv_fun := (continuous.comp I.continuous_inv_fun continuous_fst).prod_mk
(continuous.comp I'.continuous_inv_fun continuous_snd) }
/-- Special case of product model with corners, which is trivial on the second factor. This shows up
as the model to tangent bundles. -/
@[reducible] def model_with_corners.tangent
{𝕜 : Type u} [nondiscrete_normed_field 𝕜]
{E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H]
(I : model_with_corners 𝕜 E H) : model_with_corners 𝕜 (E × E) (model_prod H E) :=
I.prod (𝓘(𝕜, E))
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{F : Type*} [normed_group F] [normed_space 𝕜 F] {F' : Type*} [normed_group F'] [normed_space 𝕜 F']
{H : Type*} [topological_space H] {H' : Type*} [topological_space H']
{G : Type*} [topological_space G] {G' : Type*} [topological_space G']
{I : model_with_corners 𝕜 E H} {J : model_with_corners 𝕜 F G}
@[simp, mfld_simps] lemma model_with_corners_prod_to_local_equiv :
(I.prod J).to_local_equiv = (I.to_local_equiv).prod (J.to_local_equiv) :=
begin
ext1 x,
{ refl, },
{ intro x, refl, },
{ simp only [set.univ_prod_univ, model_with_corners.source_eq, local_equiv.prod_source], }
end
@[simp, mfld_simps] lemma model_with_corners_prod_coe
(I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') :
(I.prod I' : _ × _ → _ × _) = prod.map I I' := rfl
@[simp, mfld_simps] lemma model_with_corners_prod_coe_symm
(I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') :
((I.prod I').symm : _ × _ → _ × _) = prod.map I.symm I'.symm := rfl
end model_with_corners_prod
section boundaryless
/-- Property ensuring that the model with corners `I` defines manifolds without boundary. -/
class model_with_corners.boundaryless {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H]
(I : model_with_corners 𝕜 E H) : Prop :=
(range_eq_univ : range I = univ)
/-- The trivial model with corners has no boundary -/
instance model_with_corners_self_boundaryless (𝕜 : Type*) [nondiscrete_normed_field 𝕜]
(E : Type*) [normed_group E] [normed_space 𝕜 E] : (model_with_corners_self 𝕜 E).boundaryless :=
⟨by simp⟩
/-- If two model with corners are boundaryless, their product also is -/
instance model_with_corners.range_eq_univ_prod {𝕜 : Type u} [nondiscrete_normed_field 𝕜]
{E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H]
(I : model_with_corners 𝕜 E H) [I.boundaryless]
{E' : Type v'} [normed_group E'] [normed_space 𝕜 E'] {H' : Type w'} [topological_space H']
(I' : model_with_corners 𝕜 E' H') [I'.boundaryless] :
(I.prod I').boundaryless :=
begin
split,
dsimp [model_with_corners.prod, model_prod],
rw [← prod_range_range_eq, model_with_corners.boundaryless.range_eq_univ,
model_with_corners.boundaryless.range_eq_univ, univ_prod_univ]
end
end boundaryless
section times_cont_diff_groupoid
/-! ### Smooth functions on models with corners -/
variables {m n : with_top ℕ} {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H]
(I : model_with_corners 𝕜 E H)
{M : Type*} [topological_space M]
variable (n)
/-- Given a model with corners `(E, H)`, we define the groupoid of `C^n` transformations of `H` as
the maps that are `C^n` when read in `E` through `I`. -/
def times_cont_diff_groupoid : structure_groupoid H :=
pregroupoid.groupoid
{ property := λf s, times_cont_diff_on 𝕜 n (I ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I),
comp := λf g u v hf hg hu hv huv, begin
have : I ∘ (g ∘ f) ∘ I.symm = (I ∘ g ∘ I.symm) ∘ (I ∘ f ∘ I.symm),
by { ext x, simp },
rw this,
apply times_cont_diff_on.comp hg _,
{ rintros x ⟨hx1, hx2⟩,
simp only with mfld_simps at ⊢ hx1,
exact hx1.2 },
{ refine hf.mono _,
rintros x ⟨hx1, hx2⟩,
exact ⟨hx1.1, hx2⟩ }
end,
id_mem := begin
apply times_cont_diff_on.congr (times_cont_diff_id.times_cont_diff_on),
rintros x ⟨hx1, hx2⟩,
rcases mem_range.1 hx2 with ⟨y, hy⟩,
rw ← hy,
simp only with mfld_simps,
end,
locality := λf u hu H, begin
apply times_cont_diff_on_of_locally_times_cont_diff_on,
rintros y ⟨hy1, hy2⟩,
rcases mem_range.1 hy2 with ⟨x, hx⟩,
rw ← hx at ⊢ hy1,
simp only with mfld_simps at ⊢ hy1,
rcases H x hy1 with ⟨v, v_open, xv, hv⟩,
have : ((I.symm ⁻¹' (u ∩ v)) ∩ (range I))
= ((I.symm ⁻¹' u) ∩ (range I) ∩ I.symm ⁻¹' v),
{ rw [preimage_inter, inter_assoc, inter_assoc],
congr' 1,
rw inter_comm },
rw this at hv,
exact ⟨I.symm ⁻¹' v, v_open.preimage I.continuous_symm, by simpa, hv⟩
end,
congr := λf g u hu fg hf, begin
apply hf.congr,
rintros y ⟨hy1, hy2⟩,
rcases mem_range.1 hy2 with ⟨x, hx⟩,
rw ← hx at ⊢ hy1,
simp only with mfld_simps at ⊢ hy1,
rw fg _ hy1
end }
variable {n}
/-- Inclusion of the groupoid of `C^n` local diffeos in the groupoid of `C^m` local diffeos when
`m ≤ n` -/
lemma times_cont_diff_groupoid_le (h : m ≤ n) :
times_cont_diff_groupoid n I ≤ times_cont_diff_groupoid m I :=
begin
rw [times_cont_diff_groupoid, times_cont_diff_groupoid],
apply groupoid_of_pregroupoid_le,
assume f s hfs,
exact times_cont_diff_on.of_le hfs h
end
/-- The groupoid of `0`-times continuously differentiable maps is just the groupoid of all
local homeomorphisms -/
lemma times_cont_diff_groupoid_zero_eq :
times_cont_diff_groupoid 0 I = continuous_groupoid H :=
begin
apply le_antisymm le_top,
assume u hu,
-- we have to check that every local homeomorphism belongs to `times_cont_diff_groupoid 0 I`,
-- by unfolding its definition
change u ∈ times_cont_diff_groupoid 0 I,
rw [times_cont_diff_groupoid, mem_groupoid_of_pregroupoid],
simp only [times_cont_diff_on_zero],
split,
{ apply continuous_on.comp (@continuous.continuous_on _ _ _ _ _ univ I.continuous)
_ (subset_univ _),
apply continuous_on.comp u.continuous_to_fun I.continuous_symm.continuous_on
(inter_subset_left _ _) },
{ apply continuous_on.comp (@continuous.continuous_on _ _ _ _ _ univ I.continuous)
_ (subset_univ _),
apply continuous_on.comp u.continuous_inv_fun I.continuous_inv_fun.continuous_on
(inter_subset_left _ _) },
end
variable (n)
/-- An identity local homeomorphism belongs to the `C^n` groupoid. -/
lemma of_set_mem_times_cont_diff_groupoid {s : set H} (hs : is_open s) :
local_homeomorph.of_set s hs ∈ times_cont_diff_groupoid n I :=
begin
rw [times_cont_diff_groupoid, mem_groupoid_of_pregroupoid],
suffices h : times_cont_diff_on 𝕜 n (I ∘ I.symm) (I.symm ⁻¹' s ∩ range I),
by simp [h],
have : times_cont_diff_on 𝕜 n id (univ : set E) :=
times_cont_diff_id.times_cont_diff_on,
exact this.congr_mono (λ x hx, by simp [hx.2]) (subset_univ _)
end
/-- The composition of a local homeomorphism from `H` to `M` and its inverse belongs to
the `C^n` groupoid. -/
lemma symm_trans_mem_times_cont_diff_groupoid (e : local_homeomorph M H) :
e.symm.trans e ∈ times_cont_diff_groupoid n I :=
begin
have : e.symm.trans e ≈ local_homeomorph.of_set e.target e.open_target :=
local_homeomorph.trans_symm_self _,
exact structure_groupoid.eq_on_source _
(of_set_mem_times_cont_diff_groupoid n I e.open_target) this
end
variables {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H']
/-- The product of two smooth local homeomorphisms is smooth. -/
lemma times_cont_diff_groupoid_prod
{I : model_with_corners 𝕜 E H} {I' : model_with_corners 𝕜 E' H'}
{e : local_homeomorph H H} {e' : local_homeomorph H' H'}
(he : e ∈ times_cont_diff_groupoid ⊤ I) (he' : e' ∈ times_cont_diff_groupoid ⊤ I') :
e.prod e' ∈ times_cont_diff_groupoid ⊤ (I.prod I') :=
begin
cases he with he he_symm,
cases he' with he' he'_symm,
simp only at he he_symm he' he'_symm,
split;
simp only [local_equiv.prod_source, local_homeomorph.prod_to_local_equiv],
{ have h3 := times_cont_diff_on.prod_map he he',
rw [← I.image_eq, ← I'.image_eq, set.prod_image_image_eq] at h3,
rw ← (I.prod I').image_eq,
exact h3, },
{ have h3 := times_cont_diff_on.prod_map he_symm he'_symm,
rw [← I.image_eq, ← I'.image_eq, set.prod_image_image_eq] at h3,
rw ← (I.prod I').image_eq,
exact h3, }
end
/-- The `C^n` groupoid is closed under restriction. -/
instance : closed_under_restriction (times_cont_diff_groupoid n I) :=
(closed_under_restriction_iff_id_le _).mpr
begin
apply structure_groupoid.le_iff.mpr,
rintros e ⟨s, hs, hes⟩,
apply (times_cont_diff_groupoid n I).eq_on_source' _ _ _ hes,
exact of_set_mem_times_cont_diff_groupoid n I hs,
end
end times_cont_diff_groupoid
end model_with_corners
section smooth_manifold_with_corners
/-! ### Smooth manifolds with corners -/
set_option old_structure_cmd true
/-- Typeclass defining smooth manifolds with corners with respect to a model with corners, over a
field `𝕜` and with infinite smoothness to simplify typeclass search and statements later on. -/
@[ancestor has_groupoid]
class smooth_manifold_with_corners {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
(M : Type*) [topological_space M] [charted_space H M] extends
has_groupoid M (times_cont_diff_groupoid ∞ I) : Prop
lemma smooth_manifold_with_corners_of_times_cont_diff_on
{𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
(M : Type*) [topological_space M] [charted_space H M]
(h : ∀ (e e' : local_homeomorph M H), e ∈ atlas H M → e' ∈ atlas H M →
times_cont_diff_on 𝕜 ⊤ (I ∘ (e.symm ≫ₕ e') ∘ I.symm)
(I.symm ⁻¹' (e.symm ≫ₕ e').source ∩ range I)) :
smooth_manifold_with_corners I M :=
{ compatible :=
begin
haveI : has_groupoid M (times_cont_diff_groupoid ∞ I) := has_groupoid_of_pregroupoid _ h,
apply structure_groupoid.compatible,
end }
/-- For any model with corners, the model space is a smooth manifold -/
instance model_space_smooth {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H]
{I : model_with_corners 𝕜 E H} :
smooth_manifold_with_corners I H := { .. has_groupoid_model_space _ _ }
end smooth_manifold_with_corners
namespace smooth_manifold_with_corners
/- We restate in the namespace `smooth_manifolds_with_corners` some lemmas that hold for general
charted space with a structure groupoid, avoiding the need to specify the groupoid
`times_cont_diff_groupoid ∞ I` explicitly. -/
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
(M : Type*) [topological_space M] [charted_space H M]
/-- The maximal atlas of `M` for the smooth manifold with corners structure corresponding to the
model with corners `I`. -/
def maximal_atlas := (times_cont_diff_groupoid ∞ I).maximal_atlas M
variable {M}
lemma mem_maximal_atlas_of_mem_atlas [smooth_manifold_with_corners I M]
{e : local_homeomorph M H} (he : e ∈ atlas H M) : e ∈ maximal_atlas I M :=
structure_groupoid.mem_maximal_atlas_of_mem_atlas _ he
lemma chart_mem_maximal_atlas [smooth_manifold_with_corners I M] (x : M) :
chart_at H x ∈ maximal_atlas I M :=
structure_groupoid.chart_mem_maximal_atlas _ x
variable {I}
lemma compatible_of_mem_maximal_atlas
{e e' : local_homeomorph M H} (he : e ∈ maximal_atlas I M) (he' : e' ∈ maximal_atlas I M) :
e.symm.trans e' ∈ times_cont_diff_groupoid ∞ I :=
structure_groupoid.compatible_of_mem_maximal_atlas he he'
/-- The product of two smooth manifolds with corners is naturally a smooth manifold with corners. -/
instance prod {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{E' : Type*} [normed_group E'] [normed_space 𝕜 E']
{H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H}
{H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'}
(M : Type*) [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M]
(M' : Type*) [topological_space M'] [charted_space H' M'] [smooth_manifold_with_corners I' M'] :
smooth_manifold_with_corners (I.prod I') (M×M') :=
{ compatible :=
begin
rintros f g ⟨f1, hf1, f2, hf2, hf⟩ ⟨g1, hg1, g2, hg2, hg⟩,
rw [hf, hg, local_homeomorph.prod_symm, local_homeomorph.prod_trans],
have h1 := has_groupoid.compatible (times_cont_diff_groupoid ⊤ I) hf1 hg1,
have h2 := has_groupoid.compatible (times_cont_diff_groupoid ⊤ I') hf2 hg2,
exact times_cont_diff_groupoid_prod h1 h2,
end }
end smooth_manifold_with_corners
section extended_charts
open_locale topological_space
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H)
{M : Type*} [topological_space M] [charted_space H M]
(x : M) {s t : set M}
/-!
### Extended charts
In a smooth manifold with corners, the model space is the space `H`. However, we will also
need to use extended charts taking values in the model vector space `E`. These extended charts are
not `local_homeomorph` as the target is not open in `E` in general, but we can still register them
as `local_equiv`.
-/
/-- The preferred extended chart on a manifold with corners around a point `x`, from a neighborhood
of `x` to the model vector space. -/
@[simp, mfld_simps] def ext_chart_at (x : M) : local_equiv M E :=
(chart_at H x).to_local_equiv.trans I.to_local_equiv
lemma ext_chart_at_coe : ⇑(ext_chart_at I x) = I ∘ chart_at H x := rfl
lemma ext_chart_at_coe_symm :
⇑(ext_chart_at I x).symm = (chart_at H x).symm ∘ I.symm := rfl
lemma ext_chart_at_source : (ext_chart_at I x).source = (chart_at H x).source :=
by rw [ext_chart_at, local_equiv.trans_source, I.source_eq, preimage_univ, inter_univ]
lemma ext_chart_at_open_source : is_open (ext_chart_at I x).source :=
by { rw ext_chart_at_source, exact (chart_at H x).open_source }
lemma mem_ext_chart_source : x ∈ (ext_chart_at I x).source :=
by simp only [ext_chart_at_source, mem_chart_source]
lemma ext_chart_at_to_inv :
(ext_chart_at I x).symm ((ext_chart_at I x) x) = x :=
(ext_chart_at I x).left_inv (mem_ext_chart_source I x)
lemma ext_chart_at_source_mem_nhds' {x' : M} (h : x' ∈ (ext_chart_at I x).source) :
(ext_chart_at I x).source ∈ 𝓝 x' :=
mem_nhds_sets (ext_chart_at_open_source I x) h
lemma ext_chart_at_source_mem_nhds : (ext_chart_at I x).source ∈ 𝓝 x :=
ext_chart_at_source_mem_nhds' I x (mem_ext_chart_source I x)
lemma ext_chart_at_source_mem_nhds_within' {x' : M} (h : x' ∈ (ext_chart_at I x).source) :
(ext_chart_at I x).source ∈ 𝓝[s] x' :=
mem_nhds_within_of_mem_nhds (ext_chart_at_source_mem_nhds' I x h)
lemma ext_chart_at_source_mem_nhds_within :
(ext_chart_at I x).source ∈ 𝓝[s] x :=
mem_nhds_within_of_mem_nhds (ext_chart_at_source_mem_nhds I x)
lemma ext_chart_at_continuous_on :
continuous_on (ext_chart_at I x) (ext_chart_at I x).source :=
begin
refine I.continuous.comp_continuous_on _,
rw ext_chart_at_source,
exact (chart_at H x).continuous_on
end
lemma ext_chart_at_continuous_at' {x' : M} (h : x' ∈ (ext_chart_at I x).source) :
continuous_at (ext_chart_at I x) x' :=
(ext_chart_at_continuous_on I x).continuous_at $ ext_chart_at_source_mem_nhds' I x h
lemma ext_chart_at_continuous_at : continuous_at (ext_chart_at I x) x :=
ext_chart_at_continuous_at' _ _ (mem_ext_chart_source I x)
lemma ext_chart_at_continuous_on_symm :
continuous_on (ext_chart_at I x).symm (ext_chart_at I x).target :=
begin
apply continuous_on.comp (chart_at H x).continuous_on_symm I.continuous_symm.continuous_on,
simp [ext_chart_at, local_equiv.trans_target]
end
lemma ext_chart_at_map_nhds' {x y : M} (hy : y ∈ (ext_chart_at I x).source) :
map (ext_chart_at I x) (𝓝 y) = 𝓝[range I] (ext_chart_at I x y) :=
begin
rw [ext_chart_at_coe, (∘), ← I.map_nhds_eq, ← (chart_at H x).map_nhds_eq, map_map],
rwa ext_chart_at_source at hy
end
lemma ext_chart_at_map_nhds :
map (ext_chart_at I x) (𝓝 x) = 𝓝[range I] (ext_chart_at I x x) :=
ext_chart_at_map_nhds' I $ mem_ext_chart_source I x
lemma ext_chart_at_target_mem_nhds_within' {y : M} (hy : y ∈ (ext_chart_at I x).source) :
(ext_chart_at I x).target ∈ 𝓝[range I] (ext_chart_at I x y) :=
begin
rw [← local_equiv.image_source_eq_target, ← ext_chart_at_map_nhds' I hy],
exact image_mem_map (ext_chart_at_source_mem_nhds' _ _ hy)
end
lemma ext_chart_at_target_mem_nhds_within :
(ext_chart_at I x).target ∈ 𝓝[range I] (ext_chart_at I x x) :=
ext_chart_at_target_mem_nhds_within' I x (mem_ext_chart_source I x)
lemma ext_chart_at_target_subset_range : (ext_chart_at I x).target ⊆ range I :=
by simp only with mfld_simps
lemma nhds_within_ext_chart_target_eq' {y : M} (hy : y ∈ (ext_chart_at I x).source) :
𝓝[(ext_chart_at I x).target] (ext_chart_at I x y) =
𝓝[range I] (ext_chart_at I x y) :=
(nhds_within_mono _ (ext_chart_at_target_subset_range _ _)).antisymm $
nhds_within_le_of_mem (ext_chart_at_target_mem_nhds_within' _ _ hy)
lemma nhds_within_ext_chart_target_eq :
𝓝[(ext_chart_at I x).target] ((ext_chart_at I x) x) =
𝓝[range I] ((ext_chart_at I x) x) :=
nhds_within_ext_chart_target_eq' I x (mem_ext_chart_source I x)
lemma ext_chart_continuous_at_symm'' {y : E} (h : y ∈ (ext_chart_at I x).target) :
continuous_at (ext_chart_at I x).symm y :=
continuous_at.comp ((chart_at H x).continuous_at_symm h.2) (I.continuous_symm.continuous_at)
lemma ext_chart_continuous_at_symm' {x' : M} (h : x' ∈ (ext_chart_at I x).source) :
continuous_at (ext_chart_at I x).symm (ext_chart_at I x x') :=
ext_chart_continuous_at_symm'' I _ $ (ext_chart_at I x).map_source h
lemma ext_chart_continuous_at_symm :
continuous_at (ext_chart_at I x).symm ((ext_chart_at I x) x) :=
ext_chart_continuous_at_symm' I x (mem_ext_chart_source I x)
lemma ext_chart_continuous_on_symm :
continuous_on (ext_chart_at I x).symm (ext_chart_at I x).target :=
λ y hy, (ext_chart_continuous_at_symm'' _ _ hy).continuous_within_at
lemma ext_chart_preimage_open_of_open' {s : set E} (hs : is_open s) :
is_open ((ext_chart_at I x).source ∩ ext_chart_at I x ⁻¹' s) :=
(ext_chart_at_continuous_on I x).preimage_open_of_open (ext_chart_at_open_source _ _) hs
lemma ext_chart_preimage_open_of_open {s : set E} (hs : is_open s) :
is_open ((chart_at H x).source ∩ ext_chart_at I x ⁻¹' s) :=
by { rw ← ext_chart_at_source I, exact ext_chart_preimage_open_of_open' I x hs }
lemma ext_chart_at_map_nhds_within_eq_image' {y : M} (hy : y ∈ (ext_chart_at I x).source) :
map (ext_chart_at I x) (𝓝[s] y) =
𝓝[ext_chart_at I x '' ((ext_chart_at I x).source ∩ s)] (ext_chart_at I x y) :=
by set e := ext_chart_at I x;
calc map e (𝓝[s] y) = map e (𝓝[e.source ∩ s] y) :
congr_arg (map e) (nhds_within_inter_of_mem (ext_chart_at_source_mem_nhds_within' I x hy)).symm
... = 𝓝[e '' (e.source ∩ s)] (e y) :
((ext_chart_at I x).left_inv_on.mono $ inter_subset_left _ _).map_nhds_within_eq
((ext_chart_at I x).left_inv hy)
(ext_chart_continuous_at_symm' I x hy).continuous_within_at
(ext_chart_at_continuous_at' I x hy).continuous_within_at
lemma ext_chart_at_map_nhds_within_eq_image :
map (ext_chart_at I x) (𝓝[s] x) =
𝓝[ext_chart_at I x '' ((ext_chart_at I x).source ∩ s)] (ext_chart_at I x x) :=
ext_chart_at_map_nhds_within_eq_image' I x (mem_ext_chart_source I x)
lemma ext_chart_at_map_nhds_within' {y : M} (hy : y ∈ (ext_chart_at I x).source) :
map (ext_chart_at I x) (𝓝[s] y) =
𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] (ext_chart_at I x y) :=
by rw [ext_chart_at_map_nhds_within_eq_image' I x hy, nhds_within_inter,
← nhds_within_ext_chart_target_eq' _ _ hy, ← nhds_within_inter,
(ext_chart_at I x).image_source_inter_eq', inter_comm]
lemma ext_chart_at_map_nhds_within :
map (ext_chart_at I x) (𝓝[s] x) =
𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] (ext_chart_at I x x) :=
ext_chart_at_map_nhds_within' I x (mem_ext_chart_source I x)
lemma ext_chart_at_symm_map_nhds_within' {y : M} (hy : y ∈ (ext_chart_at I x).source) :
map (ext_chart_at I x).symm
(𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] (ext_chart_at I x y)) = 𝓝[s] y :=
begin
rw [← ext_chart_at_map_nhds_within' I x hy, map_map, map_congr, map_id],
exact (ext_chart_at I x).left_inv_on.eq_on.eventually_eq_of_mem
(ext_chart_at_source_mem_nhds_within' _ _ hy)
end
lemma ext_chart_at_symm_map_nhds_within_range' {y : M} (hy : y ∈ (ext_chart_at I x).source) :
map (ext_chart_at I x).symm (𝓝[range I] (ext_chart_at I x y)) = 𝓝 y :=
by rw [← nhds_within_univ, ← ext_chart_at_symm_map_nhds_within' I x hy, preimage_univ, univ_inter]
lemma ext_chart_at_symm_map_nhds_within :
map (ext_chart_at I x).symm
(𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] (ext_chart_at I x x)) = 𝓝[s] x :=
ext_chart_at_symm_map_nhds_within' I x (mem_ext_chart_source I x)
lemma ext_chart_at_symm_map_nhds_within_range :
map (ext_chart_at I x).symm (𝓝[range I] (ext_chart_at I x x)) = 𝓝 x :=
ext_chart_at_symm_map_nhds_within_range' I x (mem_ext_chart_source I x)
/-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point
in the source is a neighborhood of the preimage, within a set. -/
lemma ext_chart_preimage_mem_nhds_within' {x' : M} (h : x' ∈ (ext_chart_at I x).source)
(ht : t ∈ 𝓝[s] x') :
(ext_chart_at I x).symm ⁻¹' t ∈
𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x') :=
by rwa [← ext_chart_at_symm_map_nhds_within' I x h, mem_map] at ht
/-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of the
base point is a neighborhood of the preimage, within a set. -/
lemma ext_chart_preimage_mem_nhds_within (ht : t ∈ 𝓝[s] x) :
(ext_chart_at I x).symm ⁻¹' t ∈
𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) :=
ext_chart_preimage_mem_nhds_within' I x (mem_ext_chart_source I x) ht
/-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point
is a neighborhood of the preimage. -/
lemma ext_chart_preimage_mem_nhds (ht : t ∈ 𝓝 x) :
(ext_chart_at I x).symm ⁻¹' t ∈ 𝓝 ((ext_chart_at I x) x) :=
begin
apply (ext_chart_continuous_at_symm I x).preimage_mem_nhds,
rwa (ext_chart_at I x).left_inv (mem_ext_chart_source _ _)
end
/-- Technical lemma to rewrite suitably the preimage of an intersection under an extended chart, to
bring it into a convenient form to apply derivative lemmas. -/
lemma ext_chart_preimage_inter_eq :
((ext_chart_at I x).symm ⁻¹' (s ∩ t) ∩ range I)
= ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ∩ ((ext_chart_at I x).symm ⁻¹' t) :=
by mfld_set_tac
end extended_charts
/-- In the case of the manifold structure on a vector space, the extended charts are just the
identity.-/
lemma ext_chart_model_space_eq_id (𝕜 : Type*) [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E] (x : E) :
ext_chart_at (model_with_corners_self 𝕜 E) x = local_equiv.refl E :=
by simp only with mfld_simps
|
c5cb29382141820aed6f5eea47244916e03cda42 | 5719a16e23dfc08cdea7a5bf035b81690f307965 | /stage0/src/Init/Lean/Compiler/IR/Basic.lean | df47ec6e9bcc3f2124ceec7f51615e4bc3be9636 | [
"Apache-2.0"
] | permissive | postmasters/lean4 | 488b03969a371e1507e1e8a4df9ebf63c7cbe7ac | f3976fc53a883ac7606fc59357d43f4b51016ca7 | refs/heads/master | 1,655,582,707,480 | 1,588,682,595,000 | 1,588,682,595,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 25,719 | 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.Data.Array
import Init.Lean.Data.KVMap
import Init.Lean.Data.Name
import Init.Lean.Data.Format
import Init.Lean.Compiler.ExternAttr
/-
Implements (extended) λPure and λRc proposed in the article
"Counting Immutable Beans", Sebastian Ullrich and Leonardo de Moura.
The Lean to IR transformation produces λPure code, and
this part is implemented in C++. The procedures described in the paper
above are implemented in Lean.
-/
namespace Lean
namespace IR
/- Function identifier -/
abbrev FunId := Name
abbrev Index := Nat
/- Variable identifier -/
structure VarId :=
(idx : Index)
/- Join point identifier -/
structure JoinPointId :=
(idx : Index)
abbrev Index.lt (a b : Index) : Bool := a < b
namespace VarId
instance : HasBeq VarId := ⟨fun a b => a.idx == b.idx⟩
instance : HasToString VarId := ⟨fun a => "x_" ++ toString a.idx⟩
instance : HasFormat VarId := ⟨fun a => toString a⟩
instance : Hashable VarId := ⟨fun a => hash a.idx⟩
end VarId
namespace JoinPointId
instance : HasBeq JoinPointId := ⟨fun a b => a.idx == b.idx⟩
instance : HasToString JoinPointId := ⟨fun a => "block_" ++ toString a.idx⟩
instance : HasFormat JoinPointId := ⟨fun a => toString a⟩
instance : Hashable JoinPointId := ⟨fun a => hash a.idx⟩
end JoinPointId
abbrev MData := KVMap
namespace MData
abbrev empty : MData := {KVMap .}
instance : HasEmptyc MData := ⟨empty⟩
end MData
/- Low Level IR types. Most are self explanatory.
- `usize` represents the C++ `size_t` Type. We have it here
because it is 32-bit in 32-bit machines, and 64-bit in 64-bit machines,
and we want the C++ backend for our Compiler to generate platform independent code.
- `irrelevant` for Lean types, propositions and proofs.
- `object` a pointer to a value in the heap.
- `tobject` a pointer to a value in the heap or tagged pointer
(i.e., the least significant bit is 1) storing a scalar value.
- `struct` and `union` are used to return small values (e.g., `Option`, `Prod`, `Except`)
on the stack.
Remark: the RC operations for `tobject` are slightly more expensive because we
first need to test whether the `tobject` is really a pointer or not.
Remark: the Lean runtime assumes that sizeof(void*) == sizeof(sizeT).
Lean cannot be compiled on old platforms where this is not True.
Since values of type `struct` and `union` are only used to return values,
We assume they must be used/consumed "linearly". We use the term "linear" here
to mean "exactly once" in each execution. That is, given `x : S`, where `S` is a struct,
then one of the following must hold in each (execution) branch.
1- `x` occurs only at a single `ret x` instruction. That is, it is being consumed by being returned.
2- `x` occurs only at a single `ctor`. That is, it is being "consumed" by being stored into another `struct/union`.
3- We extract (aka project) every single field of `x` exactly once. That is, we are consuming `x` by consuming each
of one of its components. Minor refinement: we don't need to consume scalar fields or struct/union
fields that do not contain object fields.
-/
inductive IRType
| float | uint8 | uint16 | uint32 | uint64 | usize
| irrelevant | object | tobject
| struct (leanTypeName : Option Name) (types : Array IRType) : IRType
| union (leanTypeName : Name) (types : Array IRType) : IRType
namespace IRType
partial def beq : IRType → IRType → Bool
| float, float => true
| uint8, uint8 => true
| uint16, uint16 => true
| uint32, uint32 => true
| uint64, uint64 => true
| usize, usize => true
| irrelevant, irrelevant => true
| object, object => true
| tobject, tobject => true
| struct n₁ tys₁, struct n₂ tys₂ => n₁ == n₂ && Array.isEqv tys₁ tys₂ beq
| union n₁ tys₁, union n₂ tys₂ => n₁ == n₂ && Array.isEqv tys₁ tys₂ beq
| _, _ => false
instance HasBeq : HasBeq IRType := ⟨beq⟩
def isScalar : IRType → Bool
| float => true
| uint8 => true
| uint16 => true
| uint32 => true
| uint64 => true
| usize => true
| _ => false
def isObj : IRType → Bool
| object => true
| tobject => true
| _ => false
def isIrrelevant : IRType → Bool
| irrelevant => true
| _ => false
def isStruct : IRType → Bool
| struct _ _ => true
| _ => false
def isUnion : IRType → Bool
| union _ _ => true
| _ => false
end IRType
/- Arguments to applications, constructors, etc.
We use `irrelevant` for Lean types, propositions and proofs that have been erased.
Recall that for a Function `f`, we also generate `f._rarg` which does not take
`irrelevant` arguments. However, `f._rarg` is only safe to be used in full applications. -/
inductive Arg
| var (id : VarId)
| irrelevant
namespace Arg
protected def beq : Arg → Arg → Bool
| var x, var y => x == y
| irrelevant, irrelevant => true
| _, _ => false
instance : HasBeq Arg := ⟨Arg.beq⟩
instance : Inhabited Arg := ⟨irrelevant⟩
end Arg
@[export lean_ir_mk_var_arg] def mkVarArg (id : VarId) : Arg := Arg.var id
inductive LitVal
| num (v : Nat)
| str (v : String)
def LitVal.beq : LitVal → LitVal → Bool
| LitVal.num v₁, LitVal.num v₂ => v₁ == v₂
| LitVal.str v₁, LitVal.str v₂ => v₁ == v₂
| _, _ => false
instance LitVal.HasBeq : HasBeq LitVal := ⟨LitVal.beq⟩
/- Constructor information.
- `name` is the Name of the Constructor in Lean.
- `cidx` is the Constructor index (aka tag).
- `size` is the number of arguments of type `object/tobject`.
- `usize` is the number of arguments of type `usize`.
- `ssize` is the number of bytes used to store scalar values.
Recall that a Constructor object contains a header, then a sequence of
pointers to other Lean objects, a sequence of `USize` (i.e., `size_t`)
scalar values, and a sequence of other scalar values. -/
structure CtorInfo :=
(name : Name) (cidx : Nat) (size : Nat) (usize : Nat) (ssize : Nat)
def CtorInfo.beq : CtorInfo → CtorInfo → Bool
| ⟨n₁, cidx₁, size₁, usize₁, ssize₁⟩, ⟨n₂, cidx₂, size₂, usize₂, ssize₂⟩ =>
n₁ == n₂ && cidx₁ == cidx₂ && size₁ == size₂ && usize₁ == usize₂ && ssize₁ == ssize₂
instance CtorInfo.HasBeq : HasBeq CtorInfo := ⟨CtorInfo.beq⟩
def CtorInfo.isRef (info : CtorInfo) : Bool :=
info.size > 0 || info.usize > 0 || info.ssize > 0
def CtorInfo.isScalar (info : CtorInfo) : Bool :=
!info.isRef
inductive Expr
/- We use `ctor` mainly for constructing Lean object/tobject values `lean_ctor_object` in the runtime.
This instruction is also used to creat `struct` and `union` return values.
For `union`, only `i.cidx` is relevant. For `struct`, `i` is irrelevant. -/
| ctor (i : CtorInfo) (ys : Array Arg)
| reset (n : Nat) (x : VarId)
/- `reuse x in ctor_i ys` instruction in the paper. -/
| reuse (x : VarId) (i : CtorInfo) (updtHeader : Bool) (ys : Array Arg)
/- Extract the `tobject` value at Position `sizeof(void*)*i` from `x`.
We also use `proj` for extracting fields from `struct` return values, and casting `union` return values. -/
| proj (i : Nat) (x : VarId)
/- Extract the `Usize` value at Position `sizeof(void*)*i` from `x`. -/
| uproj (i : Nat) (x : VarId)
/- Extract the scalar value at Position `sizeof(void*)*n + offset` from `x`. -/
| sproj (n : Nat) (offset : Nat) (x : VarId)
/- Full application. -/
| fap (c : FunId) (ys : Array Arg)
/- Partial application that creates a `pap` value (aka closure in our nonstandard terminology). -/
| pap (c : FunId) (ys : Array Arg)
/- Application. `x` must be a `pap` value. -/
| ap (x : VarId) (ys : Array Arg)
/- Given `x : ty` where `ty` is a scalar type, this operation returns a value of Type `tobject`.
For small scalar values, the Result is a tagged pointer, and no memory allocation is performed. -/
| box (ty : IRType) (x : VarId)
/- Given `x : [t]object`, obtain the scalar value. -/
| unbox (x : VarId)
| lit (v : LitVal)
/- Return `1 : uint8` Iff `RC(x) > 1` -/
| isShared (x : VarId)
/- Return `1 : uint8` Iff `x : tobject` is a tagged pointer (storing a scalar value). -/
| isTaggedPtr (x : VarId)
@[export lean_ir_mk_ctor_expr] def mkCtorExpr (n : Name) (cidx : Nat) (size : Nat) (usize : Nat) (ssize : Nat) (ys : Array Arg) : Expr := Expr.ctor ⟨n, cidx, size, usize, ssize⟩ ys
@[export lean_ir_mk_proj_expr] def mkProjExpr (i : Nat) (x : VarId) : Expr := Expr.proj i x
@[export lean_ir_mk_uproj_expr] def mkUProjExpr (i : Nat) (x : VarId) : Expr := Expr.uproj i x
@[export lean_ir_mk_sproj_expr] def mkSProjExpr (n : Nat) (offset : Nat) (x : VarId) : Expr := Expr.sproj n offset x
@[export lean_ir_mk_fapp_expr] def mkFAppExpr (c : FunId) (ys : Array Arg) : Expr := Expr.fap c ys
@[export lean_ir_mk_papp_expr] def mkPAppExpr (c : FunId) (ys : Array Arg) : Expr := Expr.pap c ys
@[export lean_ir_mk_app_expr] def mkAppExpr (x : VarId) (ys : Array Arg) : Expr := Expr.ap x ys
@[export lean_ir_mk_num_expr] def mkNumExpr (v : Nat) : Expr := Expr.lit (LitVal.num v)
@[export lean_ir_mk_str_expr] def mkStrExpr (v : String) : Expr := Expr.lit (LitVal.str v)
structure Param :=
(x : VarId) (borrow : Bool) (ty : IRType)
instance paramInh : Inhabited Param := ⟨{ x := { idx := 0 }, borrow := false, ty := IRType.object }⟩
@[export lean_ir_mk_param] def mkParam (x : VarId) (borrow : Bool) (ty : IRType) : Param := ⟨x, borrow, ty⟩
inductive AltCore (FnBody : Type) : Type
| ctor (info : CtorInfo) (b : FnBody) : AltCore
| default (b : FnBody) : AltCore
inductive FnBody
/- `let x : ty := e; b` -/
| vdecl (x : VarId) (ty : IRType) (e : Expr) (b : FnBody)
/- Join point Declaration `block_j (xs) := e; b` -/
| jdecl (j : JoinPointId) (xs : Array Param) (v : FnBody) (b : FnBody)
/- Store `y` at Position `sizeof(void*)*i` in `x`. `x` must be a Constructor object and `RC(x)` must be 1.
This operation is not part of λPure is only used during optimization. -/
| set (x : VarId) (i : Nat) (y : Arg) (b : FnBody)
| setTag (x : VarId) (cidx : Nat) (b : FnBody)
/- Store `y : Usize` at Position `sizeof(void*)*i` in `x`. `x` must be a Constructor object and `RC(x)` must be 1. -/
| uset (x : VarId) (i : Nat) (y : VarId) (b : FnBody)
/- Store `y : ty` at Position `sizeof(void*)*i + offset` in `x`. `x` must be a Constructor object and `RC(x)` must be 1.
`ty` must not be `object`, `tobject`, `irrelevant` nor `Usize`. -/
| sset (x : VarId) (i : Nat) (offset : Nat) (y : VarId) (ty : IRType) (b : FnBody)
/- RC increment for `object`. If c == `true`, then `inc` must check whether `x` is a tagged pointer or not.
If `persistent == true` then `x` is statically known to be a persistent object. -/
| inc (x : VarId) (n : Nat) (c : Bool) (persistent : Bool) (b : FnBody)
/- RC decrement for `object`. If c == `true`, then `inc` must check whether `x` is a tagged pointer or not.
If `persistent == true` then `x` is statically known to be a persistent object. -/
| dec (x : VarId) (n : Nat) (c : Bool) (persistent : Bool) (b : FnBody)
| del (x : VarId) (b : FnBody)
| mdata (d : MData) (b : FnBody)
| case (tid : Name) (x : VarId) (xType : IRType) (cs : Array (AltCore FnBody))
| ret (x : Arg)
/- Jump to join point `j` -/
| jmp (j : JoinPointId) (ys : Array Arg)
| unreachable
instance : Inhabited FnBody := ⟨FnBody.unreachable⟩
abbrev FnBody.nil := FnBody.unreachable
@[export lean_ir_mk_vdecl] def mkVDecl (x : VarId) (ty : IRType) (e : Expr) (b : FnBody) : FnBody := FnBody.vdecl x ty e b
@[export lean_ir_mk_jdecl] def mkJDecl (j : JoinPointId) (xs : Array Param) (v : FnBody) (b : FnBody) : FnBody := FnBody.jdecl j xs v b
@[export lean_ir_mk_uset] def mkUSet (x : VarId) (i : Nat) (y : VarId) (b : FnBody) : FnBody := FnBody.uset x i y b
@[export lean_ir_mk_sset] def mkSSet (x : VarId) (i : Nat) (offset : Nat) (y : VarId) (ty : IRType) (b : FnBody) : FnBody := FnBody.sset x i offset y ty b
@[export lean_ir_mk_case] def mkCase (tid : Name) (x : VarId) (cs : Array (AltCore FnBody)) : FnBody :=
-- Tyhe field `xType` is set by `explicitBoxing` compiler pass.
FnBody.case tid x IRType.object cs
@[export lean_ir_mk_ret] def mkRet (x : Arg) : FnBody := FnBody.ret x
@[export lean_ir_mk_jmp] def mkJmp (j : JoinPointId) (ys : Array Arg) : FnBody := FnBody.jmp j ys
@[export lean_ir_mk_unreachable] def mkUnreachable : Unit → FnBody := fun _ => FnBody.unreachable
abbrev Alt := AltCore FnBody
@[matchPattern] abbrev Alt.ctor := @AltCore.ctor FnBody
@[matchPattern] abbrev Alt.default := @AltCore.default FnBody
instance altInh : Inhabited Alt :=
⟨Alt.default (arbitrary _)⟩
def FnBody.isTerminal : FnBody → Bool
| FnBody.case _ _ _ _ => true
| FnBody.ret _ => true
| FnBody.jmp _ _ => true
| FnBody.unreachable => true
| _ => false
def FnBody.body : FnBody → FnBody
| FnBody.vdecl _ _ _ b => b
| FnBody.jdecl _ _ _ b => b
| FnBody.set _ _ _ b => b
| FnBody.uset _ _ _ b => b
| FnBody.sset _ _ _ _ _ b => b
| FnBody.setTag _ _ b => b
| FnBody.inc _ _ _ _ b => b
| FnBody.dec _ _ _ _ b => b
| FnBody.del _ b => b
| FnBody.mdata _ b => b
| other => other
def FnBody.setBody : FnBody → FnBody → FnBody
| FnBody.vdecl x t v _, b => FnBody.vdecl x t v b
| FnBody.jdecl j xs v _, b => FnBody.jdecl j xs v b
| FnBody.set x i y _, b => FnBody.set x i y b
| FnBody.uset x i y _, b => FnBody.uset x i y b
| FnBody.sset x i o y t _, b => FnBody.sset x i o y t b
| FnBody.setTag x i _, b => FnBody.setTag x i b
| FnBody.inc x n c p _, b => FnBody.inc x n c p b
| FnBody.dec x n c p _, b => FnBody.dec x n c p b
| FnBody.del x _, b => FnBody.del x b
| FnBody.mdata d _, b => FnBody.mdata d b
| other, b => other
@[inline] def FnBody.resetBody (b : FnBody) : FnBody :=
b.setBody FnBody.nil
/- If b is a non terminal, then return a pair `(c, b')` s.t. `b == c <;> b'`,
and c.body == FnBody.nil -/
@[inline] def FnBody.split (b : FnBody) : FnBody × FnBody :=
let b' := b.body;
let c := b.resetBody;
(c, b')
def AltCore.body : Alt → FnBody
| Alt.ctor _ b => b
| Alt.default b => b
def AltCore.setBody : Alt → FnBody → Alt
| Alt.ctor c _, b => Alt.ctor c b
| Alt.default _, b => Alt.default b
@[inline] def AltCore.modifyBody (f : FnBody → FnBody) : AltCore FnBody → Alt
| Alt.ctor c b => Alt.ctor c (f b)
| Alt.default b => Alt.default (f b)
@[inline] def AltCore.mmodifyBody {m : Type → Type} [Monad m] (f : FnBody → m FnBody) : AltCore FnBody → m Alt
| Alt.ctor c b => Alt.ctor c <$> f b
| Alt.default b => Alt.default <$> f b
def Alt.isDefault : Alt → Bool
| Alt.ctor _ _ => false
| Alt.default _ => true
def push (bs : Array FnBody) (b : FnBody) : Array FnBody :=
let b := b.resetBody;
bs.push b
partial def flattenAux : FnBody → Array FnBody → (Array FnBody) × FnBody
| b, r =>
if b.isTerminal then (r, b)
else flattenAux b.body (push r b)
def FnBody.flatten (b : FnBody) : (Array FnBody) × FnBody :=
flattenAux b #[]
partial def reshapeAux : Array FnBody → Nat → FnBody → FnBody
| a, i, b =>
if i == 0 then b
else
let i := i - 1;
let (curr, a) := a.swapAt! i (arbitrary _);
let b := curr.setBody b;
reshapeAux a i b
def reshape (bs : Array FnBody) (term : FnBody) : FnBody :=
reshapeAux bs bs.size term
@[inline] def modifyJPs (bs : Array FnBody) (f : FnBody → FnBody) : Array FnBody :=
bs.map $ fun b => match b with
| FnBody.jdecl j xs v k => FnBody.jdecl j xs (f v) k
| other => other
@[inline] def mmodifyJPs {m : Type → Type} [Monad m] (bs : Array FnBody) (f : FnBody → m FnBody) : m (Array FnBody) :=
bs.mapM $ fun b => match b with
| FnBody.jdecl j xs v k => do v ← f v; pure $ FnBody.jdecl j xs v k
| other => pure other
@[export lean_ir_mk_alt] def mkAlt (n : Name) (cidx : Nat) (size : Nat) (usize : Nat) (ssize : Nat) (b : FnBody) : Alt := Alt.ctor ⟨n, cidx, size, usize, ssize⟩ b
inductive Decl
| fdecl (f : FunId) (xs : Array Param) (ty : IRType) (b : FnBody)
| extern (f : FunId) (xs : Array Param) (ty : IRType) (ext : ExternAttrData)
namespace Decl
instance : Inhabited Decl :=
⟨fdecl (arbitrary _) (arbitrary _) IRType.irrelevant (arbitrary _)⟩
def name : Decl → FunId
| Decl.fdecl f _ _ _ => f
| Decl.extern f _ _ _ => f
def params : Decl → Array Param
| Decl.fdecl _ xs _ _ => xs
| Decl.extern _ xs _ _ => xs
def resultType : Decl → IRType
| Decl.fdecl _ _ t _ => t
| Decl.extern _ _ t _ => t
def isExtern : Decl → Bool
| Decl.extern _ _ _ _ => true
| _ => false
end Decl
@[export lean_ir_mk_decl] def mkDecl (f : FunId) (xs : Array Param) (ty : IRType) (b : FnBody) : Decl := Decl.fdecl f xs ty b
@[export lean_ir_mk_extern_decl] def mkExternDecl (f : FunId) (xs : Array Param) (ty : IRType) (e : ExternAttrData) : Decl :=
Decl.extern f xs ty e
/-- Set of variable and join point names -/
abbrev IndexSet := RBTree Index Index.lt
instance vsetInh : Inhabited IndexSet := ⟨{}⟩
def mkIndexSet (idx : Index) : IndexSet :=
RBTree.empty.insert idx
inductive LocalContextEntry
| param : IRType → LocalContextEntry
| localVar : IRType → Expr → LocalContextEntry
| joinPoint : Array Param → FnBody → LocalContextEntry
abbrev LocalContext := RBMap Index LocalContextEntry Index.lt
def LocalContext.addLocal (ctx : LocalContext) (x : VarId) (t : IRType) (v : Expr) : LocalContext :=
ctx.insert x.idx (LocalContextEntry.localVar t v)
def LocalContext.addJP (ctx : LocalContext) (j : JoinPointId) (xs : Array Param) (b : FnBody) : LocalContext :=
ctx.insert j.idx (LocalContextEntry.joinPoint xs b)
def LocalContext.addParam (ctx : LocalContext) (p : Param) : LocalContext :=
ctx.insert p.x.idx (LocalContextEntry.param p.ty)
def LocalContext.addParams (ctx : LocalContext) (ps : Array Param) : LocalContext :=
ps.foldl LocalContext.addParam ctx
def LocalContext.isJP (ctx : LocalContext) (idx : Index) : Bool :=
match ctx.find? idx with
| some (LocalContextEntry.joinPoint _ _) => true
| other => false
def LocalContext.getJPBody (ctx : LocalContext) (j : JoinPointId) : Option FnBody :=
match ctx.find? j.idx with
| some (LocalContextEntry.joinPoint _ b) => some b
| other => none
def LocalContext.getJPParams (ctx : LocalContext) (j : JoinPointId) : Option (Array Param) :=
match ctx.find? j.idx with
| some (LocalContextEntry.joinPoint ys _) => some ys
| other => none
def LocalContext.isParam (ctx : LocalContext) (idx : Index) : Bool :=
match ctx.find? idx with
| some (LocalContextEntry.param _) => true
| other => false
def LocalContext.isLocalVar (ctx : LocalContext) (idx : Index) : Bool :=
match ctx.find? idx with
| some (LocalContextEntry.localVar _ _) => true
| other => false
def LocalContext.contains (ctx : LocalContext) (idx : Index) : Bool :=
ctx.contains idx
def LocalContext.eraseJoinPointDecl (ctx : LocalContext) (j : JoinPointId) : LocalContext :=
ctx.erase j.idx
def LocalContext.getType (ctx : LocalContext) (x : VarId) : Option IRType :=
match ctx.find? x.idx with
| some (LocalContextEntry.param t) => some t
| some (LocalContextEntry.localVar t _) => some t
| other => none
def LocalContext.getValue (ctx : LocalContext) (x : VarId) : Option Expr :=
match ctx.find? x.idx with
| some (LocalContextEntry.localVar _ v) => some v
| other => none
abbrev IndexRenaming := RBMap Index Index Index.lt
class HasAlphaEqv (α : Type) :=
(aeqv : IndexRenaming → α → α → Bool)
export HasAlphaEqv (aeqv)
def VarId.alphaEqv (ρ : IndexRenaming) (v₁ v₂ : VarId) : Bool :=
match ρ.find? v₁.idx with
| some v => v == v₂.idx
| none => v₁ == v₂
instance VarId.hasAeqv : HasAlphaEqv VarId := ⟨VarId.alphaEqv⟩
def Arg.alphaEqv (ρ : IndexRenaming) : Arg → Arg → Bool
| Arg.var v₁, Arg.var v₂ => aeqv ρ v₁ v₂
| Arg.irrelevant, Arg.irrelevant => true
| _, _ => false
instance Arg.hasAeqv : HasAlphaEqv Arg := ⟨Arg.alphaEqv⟩
def args.alphaEqv (ρ : IndexRenaming) (args₁ args₂ : Array Arg) : Bool :=
Array.isEqv args₁ args₂ (fun a b => aeqv ρ a b)
instance args.hasAeqv : HasAlphaEqv (Array Arg) := ⟨args.alphaEqv⟩
def Expr.alphaEqv (ρ : IndexRenaming) : Expr → Expr → Bool
| Expr.ctor i₁ ys₁, Expr.ctor i₂ ys₂ => i₁ == i₂ && aeqv ρ ys₁ ys₂
| Expr.reset n₁ x₁, Expr.reset n₂ x₂ => n₁ == n₂ && aeqv ρ x₁ x₂
| Expr.reuse x₁ i₁ u₁ ys₁, Expr.reuse x₂ i₂ u₂ ys₂ => aeqv ρ x₁ x₂ && i₁ == i₂ && u₁ == u₂ && aeqv ρ ys₁ ys₂
| Expr.proj i₁ x₁, Expr.proj i₂ x₂ => i₁ == i₂ && aeqv ρ x₁ x₂
| Expr.uproj i₁ x₁, Expr.uproj i₂ x₂ => i₁ == i₂ && aeqv ρ x₁ x₂
| Expr.sproj n₁ o₁ x₁, Expr.sproj n₂ o₂ x₂ => n₁ == n₂ && o₁ == o₂ && aeqv ρ x₁ x₂
| Expr.fap c₁ ys₁, Expr.fap c₂ ys₂ => c₁ == c₂ && aeqv ρ ys₁ ys₂
| Expr.pap c₁ ys₁, Expr.pap c₂ ys₂ => c₁ == c₂ && aeqv ρ ys₁ ys₂
| Expr.ap x₁ ys₁, Expr.ap x₂ ys₂ => aeqv ρ x₁ x₂ && aeqv ρ ys₁ ys₂
| Expr.box ty₁ x₁, Expr.box ty₂ x₂ => ty₁ == ty₂ && aeqv ρ x₁ x₂
| Expr.unbox x₁, Expr.unbox x₂ => aeqv ρ x₁ x₂
| Expr.lit v₁, Expr.lit v₂ => v₁ == v₂
| Expr.isShared x₁, Expr.isShared x₂ => aeqv ρ x₁ x₂
| Expr.isTaggedPtr x₁, Expr.isTaggedPtr x₂ => aeqv ρ x₁ x₂
| _, _ => false
instance Expr.hasAeqv : HasAlphaEqv Expr:= ⟨Expr.alphaEqv⟩
def addVarRename (ρ : IndexRenaming) (x₁ x₂ : Nat) :=
if x₁ == x₂ then ρ else ρ.insert x₁ x₂
def addParamRename (ρ : IndexRenaming) (p₁ p₂ : Param) : Option IndexRenaming :=
if p₁.ty == p₂.ty && p₁.borrow = p₂.borrow then some (addVarRename ρ p₁.x.idx p₂.x.idx)
else none
def addParamsRename (ρ : IndexRenaming) (ps₁ ps₂ : Array Param) : Option IndexRenaming :=
if ps₁.size != ps₂.size then none
else Array.foldl₂ (fun ρ p₁ p₂ => do ρ ← ρ; addParamRename ρ p₁ p₂) (some ρ) ps₁ ps₂
partial def FnBody.alphaEqv : IndexRenaming → FnBody → FnBody → Bool
| ρ, FnBody.vdecl x₁ t₁ v₁ b₁, FnBody.vdecl x₂ t₂ v₂ b₂ => t₁ == t₂ && aeqv ρ v₁ v₂ && FnBody.alphaEqv (addVarRename ρ x₁.idx x₂.idx) b₁ b₂
| ρ, FnBody.jdecl j₁ ys₁ v₁ b₁, FnBody.jdecl j₂ ys₂ v₂ b₂ => match addParamsRename ρ ys₁ ys₂ with
| some ρ' => FnBody.alphaEqv ρ' v₁ v₂ && FnBody.alphaEqv (addVarRename ρ j₁.idx j₂.idx) b₁ b₂
| none => false
| ρ, FnBody.set x₁ i₁ y₁ b₁, FnBody.set x₂ i₂ y₂ b₂ => aeqv ρ x₁ x₂ && i₁ == i₂ && aeqv ρ y₁ y₂ && FnBody.alphaEqv ρ b₁ b₂
| ρ, FnBody.uset x₁ i₁ y₁ b₁, FnBody.uset x₂ i₂ y₂ b₂ => aeqv ρ x₁ x₂ && i₁ == i₂ && aeqv ρ y₁ y₂ && FnBody.alphaEqv ρ b₁ b₂
| ρ, FnBody.sset x₁ i₁ o₁ y₁ t₁ b₁, FnBody.sset x₂ i₂ o₂ y₂ t₂ b₂ =>
aeqv ρ x₁ x₂ && i₁ = i₂ && o₁ = o₂ && aeqv ρ y₁ y₂ && t₁ == t₂ && FnBody.alphaEqv ρ b₁ b₂
| ρ, FnBody.setTag x₁ i₁ b₁, FnBody.setTag x₂ i₂ b₂ => aeqv ρ x₁ x₂ && i₁ == i₂ && FnBody.alphaEqv ρ b₁ b₂
| ρ, FnBody.inc x₁ n₁ c₁ p₁ b₁, FnBody.inc x₂ n₂ c₂ p₂ b₂ => aeqv ρ x₁ x₂ && n₁ == n₂ && c₁ == c₂ && p₁ == p₂ && FnBody.alphaEqv ρ b₁ b₂
| ρ, FnBody.dec x₁ n₁ c₁ p₁ b₁, FnBody.dec x₂ n₂ c₂ p₂ b₂ => aeqv ρ x₁ x₂ && n₁ == n₂ && c₁ == c₂ && p₁ == p₂ && FnBody.alphaEqv ρ b₁ b₂
| ρ, FnBody.del x₁ b₁, FnBody.del x₂ b₂ => aeqv ρ x₁ x₂ && FnBody.alphaEqv ρ b₁ b₂
| ρ, FnBody.mdata m₁ b₁, FnBody.mdata m₂ b₂ => m₁ == m₂ && FnBody.alphaEqv ρ b₁ b₂
| ρ, FnBody.case n₁ x₁ _ alts₁, FnBody.case n₂ x₂ _ alts₂ => n₁ == n₂ && aeqv ρ x₁ x₂ && Array.isEqv alts₁ alts₂ (fun alt₁ alt₂ =>
match alt₁, alt₂ with
| Alt.ctor i₁ b₁, Alt.ctor i₂ b₂ => i₁ == i₂ && FnBody.alphaEqv ρ b₁ b₂
| Alt.default b₁, Alt.default b₂ => FnBody.alphaEqv ρ b₁ b₂
| _, _ => false)
| ρ, FnBody.jmp j₁ ys₁, FnBody.jmp j₂ ys₂ => j₁ == j₂ && aeqv ρ ys₁ ys₂
| ρ, FnBody.ret x₁, FnBody.ret x₂ => aeqv ρ x₁ x₂
| _, FnBody.unreachable, FnBody.unreachable => true
| _, _, _ => false
def FnBody.beq (b₁ b₂ : FnBody) : Bool :=
FnBody.alphaEqv ∅ b₁ b₂
instance FnBody.HasBeq : HasBeq FnBody := ⟨FnBody.beq⟩
abbrev VarIdSet := RBTree VarId (fun x y => x.idx < y.idx)
namespace VarIdSet
instance : Inhabited VarIdSet := ⟨{}⟩
end VarIdSet
def mkIf (x : VarId) (t e : FnBody) : FnBody :=
FnBody.case `Bool x IRType.uint8 #[
Alt.ctor {name := `Bool.false, cidx := 0, size := 0, usize := 0, ssize := 0} e,
Alt.ctor {name := `Bool.true, cidx := 1, size := 0, usize := 0, ssize := 0} t
]
end IR
end Lean
|
04cfe043d2f9362fedea03ab463755d359143910 | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /src/Lean/Compiler/CSimpAttr.lean | 9af13161c8927350a062852611d42e01c8ad7adf | [
"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 | 2,363 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.ScopedEnvExtension
import Lean.Util.Recognizers
import Lean.Util.ReplaceExpr
namespace Lean.Compiler
namespace CSimp
structure Entry where
fromDeclName : Name
toDeclName : Name
thmName : Name
deriving Inhabited
structure State where
map : SMap Name Name := {}
thmNames : SSet Name := {}
deriving Inhabited
def State.switch : State → State
| { map, thmNames } => { map := map.switch, thmNames := thmNames.switch }
builtin_initialize ext : SimpleScopedEnvExtension Entry State ←
registerSimpleScopedEnvExtension {
name := `csimp
initial := {}
addEntry := fun { map, thmNames } { fromDeclName, toDeclName, thmName } => { map := map.insert fromDeclName toDeclName, thmNames := thmNames.insert thmName }
finalizeImport := fun s => s.switch
}
private def isConstantReplacement? (declName : Name) : CoreM (Option Entry) := do
let info ← getConstInfo declName
match info.type.eq? with
| some (_, Expr.const fromDeclName us .., Expr.const toDeclName vs ..) =>
if us == vs then
return some { fromDeclName, toDeclName, thmName := declName }
else
return none
| _ => return none
def add (declName : Name) (kind : AttributeKind) : CoreM Unit := do
if let some entry ← isConstantReplacement? declName then
ext.add entry kind
else
throwError "invalid 'csimp' theorem, only constant replacement theorems (e.g., `@f = @g`) are currently supported."
builtin_initialize
registerBuiltinAttribute {
name := `csimp
descr := "simplification theorem for the compiler"
add := fun declName stx attrKind => do
Attribute.Builtin.ensureNoArgs stx
discard <| add declName attrKind
}
@[export lean_csimp_replace_constants]
def replaceConstants (env : Environment) (e : Expr) : Expr :=
let s := ext.getState env
e.replace fun e =>
if e.isConst then
match s.map.find? e.constName! with
| some declNameNew => some (mkConst declNameNew e.constLevels!)
| none => none
else
none
end CSimp
def hasCSimpAttribute (env : Environment) (declName : Name) : Bool :=
CSimp.ext.getState env |>.thmNames.contains declName
end Lean.Compiler
|
083c8e6e4e6f32f0d8fa13880212831af2b4e92a | 306b89478da7f3c210fecd6f66472a644e896eff | /src/exercises/07_first_negations.lean | a5150669d4a43af041d6dc2720e4f46e3fa6cc7e | [
"Apache-2.0"
] | permissive | sinhp/tutorials | a671990270a3bcb3f2c7a8869bbd425e7085672f | 2cd093d5901b848e708f07b8fe450f108eb928c6 | refs/heads/master | 1,684,060,380,201 | 1,623,025,767,000 | 1,623,025,767,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,137 | lean | import tuto_lib
import data.int.parity
/-
Negations, proof by contradiction and contraposition.
This file introduces the logical rules and tactics related to negation:
exfalso, by_contradiction, contrapose, by_cases and push_neg.
There is a special statement denoted by `false` which, by definition,
has no proof.
So `false` implies everything. Indeed `false → P` means any proof of
`false` could be turned into a proof of P.
This fact is known by its latin name
"ex falso quod libet" (from false follows whatever you want).
Hence Lean's tactic to invoke this is called `exfalso`.
-/
example : false → 0 = 1 :=
begin
intro h,
exfalso,
exact h,
end
/-
The preceding example suggests that this definition of `false` isn't very useful.
But actually it allows us to define the negation of a statement P as
"P implies false" that we can read as "if P were true, we would get
a contradiction". Lean denotes this by `¬ P`.
One can prove that (¬ P) ↔ (P ↔ false). But in practice we directly
use the definition of `¬ P`.
-/
example {x : ℝ} : ¬ x < x :=
begin
intro hyp,
rw lt_iff_le_and_ne at hyp,
cases hyp with hyp_inf hyp_non,
clear hyp_inf, -- we won't use that one, so let's discard it
change x = x → false at hyp_non, -- Lean doesn't need this psychological line
apply hyp_non,
refl,
end
open int
-- 0045
example (n : ℤ) (h_even : even n) (h_not_even : ¬ even n) : 0 = 1 :=
begin
sorry
end
-- 0046
example (P Q : Prop) (h₁ : P ∨ Q) (h₂ : ¬ (P ∧ Q)) : ¬ P ↔ Q :=
begin
sorry
end
/-
The definition of negation easily implies that, for every statement P,
P → ¬ ¬ P
The excluded middle axiom, which asserts P ∨ ¬ P allows us to
prove the converse implication.
Together those two implications form the principle of double negation elimination.
not_not {P : Prop} : (¬ ¬ P) ↔ P
The implication `¬ ¬ P → P` is the basis for proofs by contradiction:
in order to prove P, it suffices to prove ¬¬ P, ie `¬ P → false`.
Of course there is no need to keep explaining all this. The tactic
`by_contradiction Hyp` will transform any goal P into `false` and
add Hyp : ¬ P to the local context.
Let's return to a proof from the 5th file: uniqueness of limits for a sequence.
This cannot be proved without using some version of the excluded middle
axiom. We used it secretely in
eq_of_abs_sub_le_all (x y : ℝ) : (∀ ε > 0, |x - y| ≤ ε) → x = y
(we'll prove a variation on this lemma below).
In the proof below, we also take the opportunity to introduce the `let` tactic
which creates a local definition. If needed, it can be unfolded using `dsimp` which
takes a list of definitions to unfold. For instance after using `let N₀ := max N N'`,
you could write `dsimp [N₀] at h` to replace `N₀` by its definition in some
local assumption `h`.
-/
example (u : ℕ → ℝ) (l l' : ℝ) : seq_limit u l → seq_limit u l' → l = l' :=
begin
intros hl hl',
by_contradiction H,
change l ≠ l' at H, -- Lean does not need this line
have ineg : |l-l'| > 0,
exact abs_pos.mpr (sub_ne_zero_of_ne H), -- details about that line are not important
cases hl ( |l-l'|/4 ) (by linarith) with N hN,
cases hl' ( |l-l'|/4 ) (by linarith) with N' hN',
let N₀ := max N N',
specialize hN N₀ (le_max_left _ _),
specialize hN' N₀ (le_max_right _ _),
have clef : |l-l'| < |l-l'|,
calc
|l - l'| = |(l-u N₀) + (u N₀ -l')| : by ring
... ≤ |l - u N₀| + |u N₀ - l'| : by apply abs_add
... = |u N₀ - l| + |u N₀ - l'| : by rw abs_sub
... < |l-l'| : by linarith,
linarith, -- linarith can also find simple numerical contradictions
end
/-
Another incarnation of the excluded middle axiom is the principle of
contraposition: in order to prove P ⇒ Q, it suffices to prove
non Q ⇒ non P.
-/
-- Using a proof by contradiction, let's prove the contraposition principle
-- 0047
example (P Q : Prop) (h : ¬ Q → ¬ P) : P → Q :=
begin
sorry
end
/-
Again Lean doesn't need this principle explained to it. We can use the
`contrapose` tactic.
-/
example (P Q : Prop) (h : ¬ Q → ¬ P) : P → Q :=
begin
contrapose,
exact h,
end
/-
In the next exercise, we'll use
odd n : ∃ k, n = 2*k + 1
int.odd_iff_not_even {n : ℤ} : odd n ↔ ¬ even n
-/
-- 0048
example (n : ℤ) : even (n^2) ↔ even n :=
begin
sorry
end
/-
As a last step on our law of the excluded middle tour, let's notice that, especially
in pure logic exercises, it can sometimes be useful to use the
excluded middle axiom in its original form:
classical.em : ∀ P, P ∨ ¬ P
Instead of applying this lemma and then using the `cases` tactic, we
have the shortcut
by_cases h : P,
combining both steps to create two proof branches: one assuming
h : P, and the other assuming h : ¬ P
For instance, let's prove a reformulation of this implication relation,
which is sometimes used as a definition in other logical foundations,
especially those based on truth tables (hence very strongly using
excluded middle from the very beginning).
-/
variables (P Q : Prop)
example : (P → Q) ↔ (¬ P ∨ Q) :=
begin
split,
{ intro h,
by_cases hP : P,
{ right,
exact h hP },
{ left,
exact hP } },
{ intros h hP,
cases h with hnP hQ,
{ exfalso,
exact hnP hP },
{ exact hQ } },
end
-- 0049
example : ¬ (P ∧ Q) ↔ ¬ P ∨ ¬ Q :=
begin
sorry
end
/-
It is crucial to understand negation of quantifiers.
Let's do it by hand for a little while.
In the first exercise, only the definition of negation is needed.
-/
-- 0050
example (n : ℤ) : ¬ (∃ k, n = 2*k) ↔ ∀ k, n ≠ 2*k :=
begin
sorry
end
/-
Contrary to negation of the existential quantifier, negation of the
universal quantifier requires excluded middle for the first implication.
In order to prove this, we can use either
* a double proof by contradiction
* a contraposition, not_not : (¬ ¬ P) ↔ P) and a proof by contradiction.
-/
def even_fun (f : ℝ → ℝ) := ∀ x, f (-x) = f x
-- 0051
example (f : ℝ → ℝ) : ¬ even_fun f ↔ ∃ x, f (-x) ≠ f x :=
begin
sorry
end
/-
Of course we can't keep repeating the above proofs, especially the second one.
So we use the `push_neg` tactic.
-/
example : ¬ even_fun (λ x, 2*x) :=
begin
unfold even_fun, -- Here unfolding is important because push_neg won't do it.
push_neg,
use 42,
linarith,
end
-- 0052
example (f : ℝ → ℝ) : ¬ even_fun f ↔ ∃ x, f (-x) ≠ f x :=
begin
sorry
end
def bounded_above (f : ℝ → ℝ) := ∃ M, ∀ x, f x ≤ M
example : ¬ bounded_above (λ x, x) :=
begin
unfold bounded_above,
push_neg,
intro M,
use M + 1,
linarith,
end
-- Let's contrapose
-- 0053
example (x : ℝ) : (∀ ε > 0, x ≤ ε) → x ≤ 0 :=
begin
sorry
end
/-
The "contrapose, push_neg" combo is so common that we can abreviate it to
`contrapose!`
Let's use this trick, together with:
eq_or_lt_of_le : a ≤ b → a = b ∨ a < b
-/
-- 0054
example (f : ℝ → ℝ) : (∀ x y, x < y → f x < f y) ↔ (∀ x y, (x ≤ y ↔ f x ≤ f y)) :=
begin
sorry
end
|
a9d99c67e2a8e1b12180099591b9d466dd753400 | c6da0300d417abe3464e750ab51a63502b93acfa | /src/struct_tact/default.lean | aa9d1b6c5c5d5468fd10a33d81aff75ca4c1a13f | [
"Apache-2.0"
] | permissive | uwplse/struct_tact | 55bc1d260fac498cff83a4d71461041f8ed74bd6 | 22188ea2e97705d1185f75dde24e6bab88054ab0 | refs/heads/master | 1,630,670,592,496 | 1,515,453,299,000 | 1,515,453,299,000 | 104,603,771 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,641 | lean | import .induction_on
import .auto
import .simp_option
import .traversals
import .break_tactics
open tactic
open interactive
meta def simp_coe :=
`[unfold coe lift_t has_lift_t.lift coe_t has_coe_t.coe coe_b has_coe.coe,
try { dsimp * at * }]
-- (* https://github.com/uwplse/StructTact *)
-- Ltac subst_max :=
-- repeat match goal with
-- | [ H : ?X = _ |- _ ] => subst X
-- | [H : _ = ?X |- _] => subst X
-- end.
-- Ltac inv H := inversion H; subst_max.
-- Ltac invc H := inv H; clear H.
-- Ltac invcs H := invc H; simpl in *.
-- Ltac solve_by_inversion' tac :=
-- match goal with
-- | [H : _ |- _] => solve [inv H; tac]
-- end.
-- Ltac solve_by_inversion := solve_by_inversion' auto.
-- Ltac apply_fun f H:=
-- match type of H with
-- | ?X = ?Y => assert (f X = f Y)
-- end.
-- Ltac conclude H tac :=
-- (let H' := fresh in
-- match type of H with
-- | ?P -> _ => assert P as H' by (tac)
-- end; specialize (H H'); clear H').
-- Ltac concludes :=
-- match goal with
-- | [ H : ?P -> _ |- _ ] => conclude H auto
-- end.
-- Ltac forward H :=
-- let H' := fresh in
-- match type of H with
-- | ?P -> _ => assert P as H'
-- end.
-- Ltac forwards :=
-- match goal with
-- | [ H : ?P -> _ |- _ ] => forward H
-- end.
-- Ltac find_contradiction :=
-- match goal with
-- | [ H : ?X = _, H' : ?X = _ |- _ ] => rewrite H in H'; solve_by_inversion
-- end.
-- Ltac find_rewrite :=
-- match goal with
-- | [ H : ?X _ _ _ _ = _, H' : ?X _ _ _ _ = _ |- _ ] => rewrite H in H'
-- | [ H : ?X = _, H' : ?X = _ |- _ ] => rewrite H in H'
-- | [ H : ?X = _, H' : context [ ?X ] |- _ ] => rewrite H in H'
-- | [ H : ?X = _ |- context [ ?X ] ] => rewrite H
-- end.
-- Ltac find_erewrite :=
-- match goal with
-- | [ H : ?X _ _ _ _ = _, H' : ?X _ _ _ _ = _ |- _ ] => erewrite H in H'
-- | [ H : ?X = _, H' : ?X = _ |- _ ] => erewrite H in H'
-- | [ H : ?X = _, H' : context [ ?X ] |- _ ] => erewrite H in H'
-- | [ H : ?X = _ |- context [ ?X ] ] => erewrite H
-- end.
-- Ltac find_rewrite_lem lem :=
-- match goal with
-- | [ H : _ |- _ ] =>
-- rewrite lem in H; [idtac]
-- end.
-- Ltac find_rewrite_lem_by lem t :=
-- match goal with
-- | [ H : _ |- _ ] =>
-- rewrite lem in H by t
-- end.
-- Ltac find_erewrite_lem lem :=
-- match goal with
-- | [ H : _ |- _] => erewrite lem in H by eauto
-- end.
-- Ltac find_reverse_rewrite :=
-- match goal with
-- | [ H : _ = ?X _ _ _ _, H' : ?X _ _ _ _ = _ |- _ ] => rewrite <- H in H'
-- | [ H : _ = ?X, H' : context [ ?X ] |- _ ] => rewrite <- H in H'
-- | [ H : _ = ?X |- context [ ?X ] ] => rewrite <- H
-- end.
-- Ltac find_inversion :=
-- match goal with
-- | [ H : ?X _ _ _ _ _ _ = ?X _ _ _ _ _ _ |- _ ] => invc H
-- | [ H : ?X _ _ _ _ _ = ?X _ _ _ _ _ |- _ ] => invc H
-- | [ H : ?X _ _ _ _ = ?X _ _ _ _ |- _ ] => invc H
-- | [ H : ?X _ _ _ = ?X _ _ _ |- _ ] => invc H
-- | [ H : ?X _ _ = ?X _ _ |- _ ] => invc H
-- | [ H : ?X _ = ?X _ |- _ ] => invc H
-- end.
-- Ltac prove_eq :=
-- match goal with
-- | [ H : ?X ?x1 ?x2 ?x3 = ?X ?y1 ?y2 ?y3 |- _ ] =>
-- assert (x1 = y1) by congruence;
-- assert (x2 = y2) by congruence;
-- assert (x3 = y3) by congruence;
-- clear H
-- | [ H : ?X ?x1 ?x2 = ?X ?y1 ?y2 |- _ ] =>
-- assert (x1 = y1) by congruence;
-- assert (x2 = y2) by congruence;
-- clear H
-- | [ H : ?X ?x1 = ?X ?y1 |- _ ] =>
-- assert (x1 = y1) by congruence;
-- clear H
-- end.
-- Ltac tuple_inversion :=
-- match goal with
-- | [ H : (_, _, _, _) = (_, _, _, _) |- _ ] => invc H
-- | [ H : (_, _, _) = (_, _, _) |- _ ] => invc H
-- | [ H : (_, _) = (_, _) |- _ ] => invc H
-- end.
-- Ltac f_apply H f :=
-- match type of H with
-- | ?X = ?Y =>
-- assert (f X = f Y) by (rewrite H; auto)
-- end.
-- Ltac break_let :=
-- match goal with
-- | [ H : context [ (let (_,_) := ?X in _) ] |- _ ] => destruct X eqn:?
-- | [ |- context [ (let (_,_) := ?X in _) ] ] => destruct X eqn:?
-- end.
-- Ltac break_or_hyp :=
-- match goal with
-- | [ H : _ \/ _ |- _ ] => invc H
-- end.
-- Ltac copy_apply lem H :=
-- let x := fresh in
-- pose proof H as x;
-- apply lem in x.
-- Ltac copy_eapply lem H :=
-- let x := fresh in
-- pose proof H as x;
-- eapply lem in x.
-- Ltac conclude_using tac :=
-- match goal with
-- | [ H : ?P -> _ |- _ ] => conclude H tac
-- end.
-- Ltac find_higher_order_rewrite :=
-- match goal with
-- | [ H : _ = _ |- _ ] => rewrite H in *
-- | [ H : forall _, _ = _ |- _ ] => rewrite H in *
-- | [ H : forall _ _, _ = _ |- _ ] => rewrite H in *
-- end.
-- Ltac find_reverse_higher_order_rewrite :=
-- match goal with
-- | [ H : _ = _ |- _ ] => rewrite <- H in *
-- | [ H : forall _, _ = _ |- _ ] => rewrite <- H in *
-- | [ H : forall _ _, _ = _ |- _ ] => rewrite <- H in *
-- end.
-- Ltac clean :=
-- match goal with
-- | [ H : ?X = ?X |- _ ] => clear H
-- end.
-- Ltac find_apply_hyp_goal :=
-- match goal with
-- | [ H : _ |- _ ] => solve [apply H]
-- end.
-- Ltac find_copy_apply_lem_hyp lem :=
-- match goal with
-- | [ H : _ |- _ ] => copy_apply lem H
-- end.
-- Ltac find_apply_hyp_hyp :=
-- match goal with
-- | [ H : forall _, _ -> _,
-- H' : _ |- _ ] =>
-- apply H in H'; [idtac]
-- | [ H : _ -> _ , H' : _ |- _ ] =>
-- apply H in H'; auto; [idtac]
-- end.
-- Ltac find_copy_apply_hyp_hyp :=
-- match goal with
-- | [ H : forall _, _ -> _,
-- H' : _ |- _ ] =>
-- copy_apply H H'; [idtac]
-- | [ H : _ -> _ , H' : _ |- _ ] =>
-- copy_apply H H'; auto; [idtac]
-- end.
-- Ltac find_apply_lem_hyp lem :=
-- match goal with
-- | [ H : _ |- _ ] => apply lem in H
-- end.
-- Ltac find_eapply_lem_hyp lem :=
-- match goal with
-- | [ H : _ |- _ ] => eapply lem in H
-- end.
-- Ltac insterU H :=
-- match type of H with
-- | forall _ : ?T, _ =>
-- let x := fresh "x" in
-- evar (x : T);
-- let x' := (eval unfold x in x) in
-- clear x; specialize (H x')
-- end.
-- Ltac find_insterU :=
-- match goal with
-- | [ H : forall _, _ |- _ ] => insterU H
-- end.
-- Ltac eapply_prop P :=
-- match goal with
-- | H : P _ |- _ =>
-- eapply H
-- end.
-- Ltac isVar t :=
-- match goal with
-- | v : _ |- _ =>
-- match t with
-- | v => idtac
-- end
-- end.
-- Ltac remGen t :=
-- let x := fresh in
-- let H := fresh in
-- remember t as x eqn:H;
-- generalize dependent H.
-- Ltac remGenIfNotVar t := first [isVar t| remGen t].
-- Ltac rememberNonVars H :=
-- match type of H with
-- | _ ?a ?b ?c ?d ?e ?f ?g ?h =>
-- remGenIfNotVar a;
-- remGenIfNotVar b;
-- remGenIfNotVar c;
-- remGenIfNotVar d;
-- remGenIfNotVar e;
-- remGenIfNotVar f;
-- remGenIfNotVar g;
-- remGenIfNotVar h
-- | _ ?a ?b ?c ?d ?e ?f ?g =>
-- remGenIfNotVar a;
-- remGenIfNotVar b;
-- remGenIfNotVar c;
-- remGenIfNotVar d;
-- remGenIfNotVar e;
-- remGenIfNotVar f;
-- remGenIfNotVar g
-- | _ ?a ?b ?c ?d ?e ?f =>
-- remGenIfNotVar a;
-- remGenIfNotVar b;
-- remGenIfNotVar c;
-- remGenIfNotVar d;
-- remGenIfNotVar e;
-- remGenIfNotVar f
-- | _ ?a ?b ?c ?d ?e =>
-- remGenIfNotVar a;
-- remGenIfNotVar b;
-- remGenIfNotVar c;
-- remGenIfNotVar d;
-- remGenIfNotVar e
-- | _ ?a ?b ?c ?d =>
-- remGenIfNotVar a;
-- remGenIfNotVar b;
-- remGenIfNotVar c;
-- remGenIfNotVar d
-- | _ ?a ?b ?c =>
-- remGenIfNotVar a;
-- remGenIfNotVar b;
-- remGenIfNotVar c
-- | _ ?a ?b =>
-- remGenIfNotVar a;
-- remGenIfNotVar b
-- | _ ?a =>
-- remGenIfNotVar a
-- end.
-- Ltac generalizeEverythingElse H :=
-- repeat match goal with
-- | [ x : ?T |- _ ] =>
-- first [
-- match H with
-- | x => fail 2
-- end |
-- match type of H with
-- | context [x] => fail 2
-- end |
-- revert x]
-- end.
-- Ltac prep_induction H :=
-- rememberNonVars H;
-- generalizeEverythingElse H.
-- Ltac econcludes :=
-- match goal with
-- | [ H : ?P -> _ |- _ ] => conclude H eauto
-- end.
-- Ltac find_copy_eapply_lem_hyp lem :=
-- match goal with
-- | [ H : _ |- _ ] => copy_eapply lem H
-- end.
-- Ltac apply_prop_hyp P Q :=
-- match goal with
-- | [ H : context [ P ], H' : context [ Q ] |- _ ] =>
-- apply H in H'
-- end.
-- Ltac eapply_prop_hyp P Q :=
-- match goal with
-- | [ H : context [ P ], H' : context [ Q ] |- _ ] =>
-- eapply H in H'
-- end.
-- Ltac copy_eapply_prop_hyp P Q :=
-- match goal with
-- | [ H : context [ P ], H' : context [ Q ] |- _ ] =>
-- copy_eapply H H'
-- end.
-- Ltac find_false :=
-- match goal with
-- | H : _ -> False |- _ => exfalso; apply H
-- end.
-- Ltac injc H :=
-- injection H; clear H; intros; subst_max.
-- Ltac find_injection :=
-- match goal with
-- | [ H : ?X _ _ _ _ _ _ = ?X _ _ _ _ _ _ |- _ ] => injc H
-- | [ H : ?X _ _ _ _ _ = ?X _ _ _ _ _ |- _ ] => injc H
-- | [ H : ?X _ _ _ _ = ?X _ _ _ _ |- _ ] => injc H
-- | [ H : ?X _ _ _ = ?X _ _ _ |- _ ] => injc H
-- | [ H : ?X _ _ = ?X _ _ |- _ ] => injc H
-- | [ H : ?X _ = ?X _ |- _ ] => injc H
-- end.
-- Ltac aggressive_rewrite_goal :=
-- match goal with H : _ |- _ => rewrite H end.
-- Ltac break_exists_name x :=
-- match goal with
-- | [ H : exists _, _ |- _ ] => destruct H as [x H]
-- end.
-- Tactic Notation "unify" uconstr(x) "with" uconstr(y) :=
-- let Htmp := fresh "Htmp" in
-- refine (let Htmp : False -> x := fun false : False =>
-- match false return y with end
-- in _);
-- clear Htmp.
-- (* generic forward reasoning *)
-- Tactic Notation "fwd" tactic3(tac) "as" ident(H) :=
-- simple refine (let H : _ := _ in _);
-- [ shelve
-- | tac
-- | clearbody H ].
-- Tactic Notation "fwd" tactic3(tac) :=
-- let H := fresh "H" in
-- fwd tac as H.
-- Ltac ee :=
-- econstructor; eauto.
run_cmd add_interactive [
`induction_on,
`simp_option,
`break_match,
`break_if,
`break_conj
-- `break_irrefutable
]
|
2083b38fcb9cf209410cfc05ceff92c981238445 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/linear_algebra/direct_sum/finsupp.lean | 5a369c076c87c02bf49924f91cc3f211799c85aa | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 3,983 | lean | /-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import algebra.direct_sum.finsupp
import linear_algebra.finsupp
import linear_algebra.direct_sum.tensor_product
/-!
# Results on finitely supported functions.
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
The tensor product of ι →₀ M and κ →₀ N is linearly equivalent to (ι × κ) →₀ (M ⊗ N).
-/
universes u v w
noncomputable theory
open_locale direct_sum
open set linear_map submodule
variables {R : Type u} {M : Type v} {N : Type w} [ring R] [add_comm_group M] [module R M]
[add_comm_group N] [module R N]
section tensor_product
open tensor_product
open_locale tensor_product classical
/-- The tensor product of ι →₀ M and κ →₀ N is linearly equivalent to (ι × κ) →₀ (M ⊗ N). -/
def finsupp_tensor_finsupp (R M N ι κ : Sort*) [comm_ring R]
[add_comm_group M] [module R M] [add_comm_group N] [module R N] :
(ι →₀ M) ⊗[R] (κ →₀ N) ≃ₗ[R] (ι × κ) →₀ (M ⊗[R] N) :=
(tensor_product.congr (finsupp_lequiv_direct_sum R M ι) (finsupp_lequiv_direct_sum R N κ))
≪≫ₗ ((tensor_product.direct_sum R (λ _ : ι, M) (λ _ : κ, N))
≪≫ₗ (finsupp_lequiv_direct_sum R (M ⊗[R] N) (ι × κ)).symm)
@[simp] theorem finsupp_tensor_finsupp_single (R M N ι κ : Sort*) [comm_ring R]
[add_comm_group M] [module R M] [add_comm_group N] [module R N]
(i : ι) (m : M) (k : κ) (n : N) :
finsupp_tensor_finsupp R M N ι κ (finsupp.single i m ⊗ₜ finsupp.single k n) =
finsupp.single (i, k) (m ⊗ₜ n) :=
by simp [finsupp_tensor_finsupp]
@[simp] theorem finsupp_tensor_finsupp_apply (R M N ι κ : Sort*) [comm_ring R]
[add_comm_group M] [module R M] [add_comm_group N] [module R N]
(f : ι →₀ M) (g : κ →₀ N) (i : ι) (k : κ) :
finsupp_tensor_finsupp R M N ι κ (f ⊗ₜ g) (i, k) = f i ⊗ₜ g k :=
begin
apply finsupp.induction_linear f,
{ simp, },
{ intros f₁ f₂ hf₁ hf₂, simp [add_tmul, hf₁, hf₂], },
{ intros i' m,
apply finsupp.induction_linear g,
{ simp, },
{ intros g₁ g₂ hg₁ hg₂, simp [tmul_add, hg₁, hg₂], },
{ intros k' n,
simp only [finsupp_tensor_finsupp_single],
simp only [finsupp.single_apply],
-- split_ifs; finish can close the goal from here
by_cases h1 : (i', k') = (i, k),
{ simp only [prod.mk.inj_iff] at h1, simp [h1] },
{ simp only [h1, if_false],
simp only [prod.mk.inj_iff, not_and_distrib] at h1,
cases h1; simp [h1] } } }
end
@[simp] theorem finsupp_tensor_finsupp_symm_single (R M N ι κ : Sort*) [comm_ring R]
[add_comm_group M] [module R M] [add_comm_group N] [module R N]
(i : ι × κ) (m : M) (n : N) :
(finsupp_tensor_finsupp R M N ι κ).symm (finsupp.single i (m ⊗ₜ n)) =
(finsupp.single i.1 m ⊗ₜ finsupp.single i.2 n) :=
prod.cases_on i $ λ i k, (linear_equiv.symm_apply_eq _).2
(finsupp_tensor_finsupp_single _ _ _ _ _ _ _ _ _).symm
variables (S : Type*) [comm_ring S] (α β : Type*)
/--
A variant of `finsupp_tensor_finsupp` where both modules are the ground ring.
-/
def finsupp_tensor_finsupp' : ((α →₀ S) ⊗[S] (β →₀ S)) ≃ₗ[S] (α × β →₀ S) :=
(finsupp_tensor_finsupp S S S α β).trans (finsupp.lcongr (equiv.refl _) (tensor_product.lid S S))
@[simp] lemma finsupp_tensor_finsupp'_apply_apply (f : α →₀ S) (g : β →₀ S) (a : α) (b : β) :
finsupp_tensor_finsupp' S α β (f ⊗ₜ[S] g) (a, b) = f a * g b :=
by simp [finsupp_tensor_finsupp']
@[simp] lemma finsupp_tensor_finsupp'_single_tmul_single (a : α) (b : β) (r₁ r₂ : S) :
finsupp_tensor_finsupp' S α β (finsupp.single a r₁ ⊗ₜ[S] finsupp.single b r₂) =
finsupp.single (a, b) (r₁ * r₂) :=
by { ext ⟨a', b'⟩, simp [finsupp.single_apply, ite_and] }
end tensor_product
|
ddaec23869bbc58dd434d837bfbca477804b5ab9 | 78630e908e9624a892e24ebdd21260720d29cf55 | /src/logic_propositional/prop_03.lean | b5fec6a38e0db90304ecd5e2163385d79f8e4ac1 | [
"CC0-1.0"
] | permissive | tomasz-lisowski/lean-logic-examples | 84e612466776be0a16c23a0439ff8ef6114ddbe1 | 2b2ccd467b49c3989bf6c92ec0358a8d6ee68c5d | refs/heads/master | 1,683,334,199,431 | 1,621,938,305,000 | 1,621,938,305,000 | 365,041,573 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 236 | lean | namespace prop_03
variables A B : Prop
theorem prop_3 : ¬ (A ∧ B) → (A → ¬ B) :=
assume h1: ¬ (A ∧ B),
assume h2: A,
assume h3: B,
have h4: A ∧ B, from and.intro h2 h3,
show false, from h1 h4
-- end namespace
end prop_03 |
a41d56c476eafff05e3346f74c5516ec15bddd6b | 82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7 | /src/Lean/Meta/Tactic/Assert.lean | d57ec1133be17ae37644db059cc20075e4cd278c | [
"Apache-2.0"
] | permissive | banksonian/lean4 | 3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc | 78da6b3aa2840693eea354a41e89fc5b212a5011 | refs/heads/master | 1,673,703,624,165 | 1,605,123,551,000 | 1,605,123,551,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,059 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Tactic.Util
import Lean.Meta.Tactic.FVarSubst
import Lean.Meta.Tactic.Intro
import Lean.Meta.Tactic.Revert
namespace Lean.Meta
/--
Convert the given goal `Ctx |- target` into `Ctx |- type -> target`.
It assumes `val` has type `type` -/
def assert (mvarId : MVarId) (name : Name) (type : Expr) (val : Expr) : MetaM MVarId :=
withMVarContext mvarId do
checkNotAssigned mvarId `assert
let tag ← getMVarTag mvarId
let target ← getMVarType mvarId
let newType := Lean.mkForall name BinderInfo.default type target
let newMVar ← mkFreshExprSyntheticOpaqueMVar newType tag
assignExprMVar mvarId (mkApp newMVar val)
pure newMVar.mvarId!
/--
Convert the given goal `Ctx |- target` into `Ctx |- let name : type := val; target`.
It assumes `val` has type `type` -/
def define (mvarId : MVarId) (name : Name) (type : Expr) (val : Expr) : MetaM MVarId := do
withMVarContext mvarId $ do
checkNotAssigned mvarId `define
let tag ← getMVarTag mvarId
let target ← getMVarType mvarId
let newType := Lean.mkLet name type val target
let newMVar ← mkFreshExprSyntheticOpaqueMVar newType tag
assignExprMVar mvarId newMVar
pure newMVar.mvarId!
/--
Convert the given goal `Ctx |- target` into `Ctx |- forall (name : type) -> name = val -> target`.
It assumes `val` has type `type` -/
def assertExt (mvarId : MVarId) (name : Name) (type : Expr) (val : Expr) (hName : Name := `h) : MetaM MVarId := do
withMVarContext mvarId $ do
checkNotAssigned mvarId `assert
let tag ← getMVarTag mvarId
let target ← getMVarType mvarId
let u ← getLevel type
let hType := mkApp3 (mkConst `Eq [u]) type (mkBVar 0) val
let newType := Lean.mkForall name BinderInfo.default type $ Lean.mkForall hName BinderInfo.default hType target
let newMVar ← mkFreshExprSyntheticOpaqueMVar newType tag
let rflPrf ← mkEqRefl val
assignExprMVar mvarId (mkApp2 newMVar val rflPrf)
pure newMVar.mvarId!
structure AssertAfterResult :=
(fvarId : FVarId)
(mvarId : MVarId)
(subst : FVarSubst)
/--
Convert the given goal `Ctx |- target` into a goal containing `(userName : type)` after the local declaration with if `fvarId`.
It assumes `val` has type `type`, and that `type` is well-formed after `fvarId`.
Note that `val` does not need to be well-formed after `fvarId`. That is, it may contain variables that are defined after `fvarId`. -/
def assertAfter (mvarId : MVarId) (fvarId : FVarId) (userName : Name) (type : Expr) (val : Expr) : MetaM AssertAfterResult := do
withMVarContext mvarId $ do
checkNotAssigned mvarId `assertAfter
let tag ← getMVarTag mvarId
let target ← getMVarType mvarId
let localDecl ← getLocalDecl fvarId
let lctx ← getLCtx
let localInsts ← getLocalInstances
let fvarIds := lctx.foldl (init := #[]) (start := localDecl.index+1) fun fvarIds decl => fvarIds.push decl.fvarId
let xs := fvarIds.map mkFVar
let targetNew ← mkForallFVars xs target
let targetNew := Lean.mkForall userName BinderInfo.default type targetNew
let lctxNew := fvarIds.foldl (init := lctx) fun lctxNew fvarId => lctxNew.erase fvarId
let localInstsNew := localInsts.filter fun inst => fvarIds.contains inst.fvar.fvarId!
let mvarNew ← mkFreshExprMVarAt lctxNew localInstsNew targetNew MetavarKind.syntheticOpaque tag
let args := (fvarIds.filter fun fvarId => !(lctx.get! fvarId).isLet).map mkFVar
let args := #[val] ++ args
assignExprMVar mvarId (mkAppN mvarNew args)
let (fvarIdNew, mvarIdNew) ← intro1P mvarNew.mvarId!
let (fvarIdsNew, mvarIdNew) ← introNP mvarIdNew fvarIds.size
let subst := fvarIds.size.fold (init := {}) fun i subst => subst.insert fvarIds[i] (mkFVar fvarIdsNew[i])
pure { fvarId := fvarIdNew, mvarId := mvarIdNew, subst := subst }
end Lean.Meta
|
72ef9b3c5c00a7e6242bc5cdf502573765433545 | dac4e6671410f506c880986cbb2212dd7f5b3dfd | /hanoi_project/Lean8_6.lean | 0c745216969b9476b01964ede909b8ef7c7caf00 | [
"CC-BY-4.0"
] | permissive | thalesant/formalabstracts-2018 | e6ddfe8b3ce5c6f055ddcccf345bf55c41f850c1 | d206dfa32a6b4a84aacc3a5500a144757e6d3454 | refs/heads/master | 1,642,678,879,231 | 1,561,648,713,000 | 1,561,648,713,000 | 97,608,420 | 1 | 0 | null | 1,564,063,995,000 | 1,500,388,250,000 | Lean | UTF-8 | Lean | false | false | 1,495 | lean | #print nat
#print list
#print or
#print and
#print prod
namespace hanoi
inductive unit'
| zero : unit'
open unit'
theorem everything_is_zero :
∀ u : unit', u = zero :=
λ u, @unit'.rec
(λ u, u = zero) rfl u
set_option pp.beta true
#check @unit'.rec
(λ u, u = zero)
#print unit'.rec
#print or.rec
inductive and'(a b : Prop):Prop
|intro : a → b → and'
#print and'.rec
theorem and'.left {a b: Prop}
(h : and' a b) : a :=
@and'.rec a b a
(λ (ha : a)(hb : b), ha) h
theorem and'.right{a b: Prop}
(h : and' a b) : b :=
@and'.rec a b b
(λ (ha : a)(hb : b), hb) h
inductive weird: Type
|succ (n:weird):weird
#print weird.rec
#print false.rec
#print not
#reduce @nat.rec (λ n, nat) 0
(λ prev IH, nat.succ IH) 0
#check @congr_arg
theorem no_weird(n:weird):false :=
@weird.rec(λ n:weird, false)
(λ a b, b) n
#print nat.rec
def nat_identity : nat → nat :=
@nat.rec(λ n, nat) 0
(λ prev IH, nat.succ IH)
theorem nat_identity_is_id
(n: nat) :
nat_identity n = n :=
@nat.rec
(λ n, nat_identity n = n)
rfl
(λ n IH,
show nat_identity (nat.succ n)
= nat.succ n, from
show nat.succ (nat_identity n)
= nat.succ n, from
congr_arg nat.succ IH)
n
inductive my_ind :Type
|one : my_ind
|two : my_ind
|three (x:nat): my_ind
|four : ∀ (x:nat), my_ind
|five : ∀ (x:nat)
(y:nat), my_ind
|six : ∀ (x:nat)
(y:my_ind), my_ind
#print my_ind.six
#print my_ind.rec
end hanoi
|
19856716e7e5523d9c8232c0342b532e1cda74e7 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/ring_theory/roots_of_unity.lean | 453fd90d7e9b9f3e7134397d2f296b0c9666a3b0 | [
"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 | 47,107 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import algebra.char_p.two
import algebra.ne_zero
import data.polynomial.ring_division
import field_theory.finite.basic
import field_theory.separable
import group_theory.specific_groups.cyclic
import number_theory.divisors
import ring_theory.integral_domain
import tactic.zify
/-!
# Roots of unity and primitive roots of unity
We define roots of unity in the context of an arbitrary commutative monoid,
as a subgroup of the group of units. We also define a predicate `is_primitive_root` on commutative
monoids, expressing that an element is a primitive root of unity.
## Main definitions
* `roots_of_unity n M`, for `n : ℕ+` is the subgroup of the units of a commutative monoid `M`
consisting of elements `x` that satisfy `x ^ n = 1`.
* `is_primitive_root ζ k`: an element `ζ` is a primitive `k`-th root of unity if `ζ ^ k = 1`,
and if `l` satisfies `ζ ^ l = 1` then `k ∣ l`.
* `primitive_roots k R`: the finset of primitive `k`-th roots of unity in an integral domain `R`.
* `is_primitive_root.aut_to_pow`: the monoid hom that takes an automorphism of a ring to the power
it sends that specific primitive root, as a member of `(zmod n)ˣ`.
## Main results
* `roots_of_unity.is_cyclic`: the roots of unity in an integral domain form a cyclic group.
* `is_primitive_root.zmod_equiv_zpowers`: `zmod k` is equivalent to
the subgroup generated by a primitive `k`-th root of unity.
* `is_primitive_root.zpowers_eq`: in an integral domain, the subgroup generated by
a primitive `k`-th root of unity is equal to the `k`-th roots of unity.
* `is_primitive_root.card_primitive_roots`: if an integral domain
has a primitive `k`-th root of unity, then it has `φ k` of them.
## Implementation details
It is desirable that `roots_of_unity` is a subgroup,
and it will mainly be applied to rings (e.g. the ring of integers in a number field) and fields.
We therefore implement it as a subgroup of the units of a commutative monoid.
We have chosen to define `roots_of_unity n` for `n : ℕ+`, instead of `n : ℕ`,
because almost all lemmas need the positivity assumption,
and in particular the type class instances for `fintype` and `is_cyclic`.
On the other hand, for primitive roots of unity, it is desirable to have a predicate
not just on units, but directly on elements of the ring/field.
For example, we want to say that `exp (2 * pi * I / n)` is a primitive `n`-th root of unity
in the complex numbers, without having to turn that number into a unit first.
This creates a little bit of friction, but lemmas like `is_primitive_root.is_unit` and
`is_primitive_root.coe_units_iff` should provide the necessary glue.
-/
open_locale classical big_operators polynomial
noncomputable theory
open polynomial
open finset
variables {M N G R S F : Type*}
variables [comm_monoid M] [comm_monoid N] [division_comm_monoid G]
section roots_of_unity
variables {k l : ℕ+}
/-- `roots_of_unity k M` is the subgroup of elements `m : Mˣ` that satisfy `m ^ k = 1` -/
def roots_of_unity (k : ℕ+) (M : Type*) [comm_monoid M] : subgroup Mˣ :=
{ carrier := { ζ | ζ ^ (k : ℕ) = 1 },
one_mem' := one_pow _,
mul_mem' := λ ζ ξ hζ hξ, by simp only [*, set.mem_set_of_eq, mul_pow, one_mul] at *,
inv_mem' := λ ζ hζ, by simp only [*, set.mem_set_of_eq, inv_pow, inv_one] at * }
@[simp] lemma mem_roots_of_unity (k : ℕ+) (ζ : Mˣ) :
ζ ∈ roots_of_unity k M ↔ ζ ^ (k : ℕ) = 1 := iff.rfl
lemma mem_roots_of_unity' (k : ℕ+) (ζ : Mˣ) :
ζ ∈ roots_of_unity k M ↔ (ζ : M) ^ (k : ℕ) = 1 :=
by { rw [mem_roots_of_unity], norm_cast }
lemma roots_of_unity.coe_injective {n : ℕ+} : function.injective (coe : (roots_of_unity n M) → M) :=
units.ext.comp (λ x y, subtype.ext)
/-- Make an element of `roots_of_unity` from a member of the base ring, and a proof that it has
a positive power equal to one. -/
@[simps coe_coe] def roots_of_unity.mk_of_pow_eq (ζ : M) {n : ℕ+} (h : ζ ^ (n : ℕ) = 1) :
roots_of_unity n M :=
⟨units.mk_of_mul_eq_one ζ (ζ ^ n.nat_pred) $
by rwa [←pow_one ζ, ←pow_mul, ←pow_add, one_mul, pnat.one_add_nat_pred],
units.ext $ by simpa⟩
@[simp] lemma roots_of_unity.coe_mk_of_pow_eq {ζ : M} {n : ℕ+}
(h : ζ ^ (n : ℕ) = 1) : (roots_of_unity.mk_of_pow_eq _ h : M) = ζ := rfl
lemma roots_of_unity_le_of_dvd (h : k ∣ l) : roots_of_unity k M ≤ roots_of_unity l M :=
begin
obtain ⟨d, rfl⟩ := h,
intros ζ h,
simp only [mem_roots_of_unity, pnat.mul_coe, pow_mul, one_pow, *] at *,
end
lemma map_roots_of_unity (f : Mˣ →* Nˣ) (k : ℕ+) :
(roots_of_unity k M).map f ≤ roots_of_unity k N :=
begin
rintros _ ⟨ζ, h, rfl⟩,
simp only [←map_pow, *, mem_roots_of_unity, set_like.mem_coe, monoid_hom.map_one] at *
end
@[norm_cast] lemma roots_of_unity.coe_pow [comm_monoid R] (ζ : roots_of_unity k R) (m : ℕ) :
↑(ζ ^ m) = (ζ ^ m : R) :=
begin
change ↑(↑(ζ ^ m) : Rˣ) = ↑(ζ : Rˣ) ^ m,
rw [subgroup.coe_pow, units.coe_pow],
end
section comm_semiring
variables [comm_semiring R] [comm_semiring S]
/-- Restrict a ring homomorphism to the nth roots of unity -/
def restrict_roots_of_unity [ring_hom_class F R S] (σ : F) (n : ℕ+) :
roots_of_unity n R →* roots_of_unity n S :=
let h : ∀ ξ : roots_of_unity n R, (σ ξ) ^ (n : ℕ) = 1 := λ ξ, by
{ change (σ (ξ : Rˣ)) ^ (n : ℕ) = 1,
rw [←map_pow, ←units.coe_pow, show ((ξ : Rˣ) ^ (n : ℕ) = 1), from ξ.2,
units.coe_one, map_one σ] } in
{ to_fun := λ ξ, ⟨@unit_of_invertible _ _ _ (invertible_of_pow_eq_one _ _ (h ξ) n.2),
by { ext, rw units.coe_pow, exact h ξ }⟩,
map_one' := by { ext, exact map_one σ },
map_mul' := λ ξ₁ ξ₂, by { ext, rw [subgroup.coe_mul, units.coe_mul], exact map_mul σ _ _ } }
@[simp] lemma restrict_roots_of_unity_coe_apply [ring_hom_class F R S] (σ : F)
(ζ : roots_of_unity k R) : ↑(restrict_roots_of_unity σ k ζ) = σ ↑ζ :=
rfl
/-- Restrict a ring isomorphism to the nth roots of unity -/
def ring_equiv.restrict_roots_of_unity (σ : R ≃+* S) (n : ℕ+) :
roots_of_unity n R ≃* roots_of_unity n S :=
{ to_fun := restrict_roots_of_unity σ.to_ring_hom n,
inv_fun :=restrict_roots_of_unity σ.symm.to_ring_hom n,
left_inv := λ ξ, by { ext, exact σ.symm_apply_apply ξ },
right_inv := λ ξ, by { ext, exact σ.apply_symm_apply ξ },
map_mul' := (restrict_roots_of_unity _ n).map_mul }
@[simp] lemma ring_equiv.restrict_roots_of_unity_coe_apply (σ : R ≃+* S) (ζ : roots_of_unity k R) :
↑(σ.restrict_roots_of_unity k ζ) = σ ↑ζ :=
rfl
@[simp] lemma ring_equiv.restrict_roots_of_unity_symm (σ : R ≃+* S) :
(σ.restrict_roots_of_unity k).symm = σ.symm.restrict_roots_of_unity k :=
rfl
end comm_semiring
section is_domain
variables [comm_ring R] [is_domain R]
lemma mem_roots_of_unity_iff_mem_nth_roots {ζ : Rˣ} :
ζ ∈ roots_of_unity k R ↔ (ζ : R) ∈ nth_roots k (1 : R) :=
by simp only [mem_roots_of_unity, mem_nth_roots k.pos, units.ext_iff, units.coe_one, units.coe_pow]
variables (k R)
/-- Equivalence between the `k`-th roots of unity in `R` and the `k`-th roots of `1`.
This is implemented as equivalence of subtypes,
because `roots_of_unity` is a subgroup of the group of units,
whereas `nth_roots` is a multiset. -/
def roots_of_unity_equiv_nth_roots :
roots_of_unity k R ≃ {x // x ∈ nth_roots k (1 : R)} :=
begin
refine
{ to_fun := λ x, ⟨x, mem_roots_of_unity_iff_mem_nth_roots.mp x.2⟩,
inv_fun := λ x, ⟨⟨x, x ^ (k - 1 : ℕ), _, _⟩, _⟩,
left_inv := _,
right_inv := _ },
swap 4, { rintro ⟨x, hx⟩, ext, refl },
swap 4, { rintro ⟨x, hx⟩, ext, refl },
all_goals
{ rcases x with ⟨x, hx⟩, rw [mem_nth_roots k.pos] at hx,
simp only [subtype.coe_mk, ← pow_succ, ← pow_succ', hx,
tsub_add_cancel_of_le (show 1 ≤ (k : ℕ), from k.one_le)] },
{ show (_ : Rˣ) ^ (k : ℕ) = 1,
simp only [units.ext_iff, hx, units.coe_mk, units.coe_one, subtype.coe_mk, units.coe_pow] }
end
variables {k R}
@[simp] lemma roots_of_unity_equiv_nth_roots_apply (x : roots_of_unity k R) :
(roots_of_unity_equiv_nth_roots R k x : R) = x :=
rfl
@[simp] lemma roots_of_unity_equiv_nth_roots_symm_apply (x : {x // x ∈ nth_roots k (1 : R)}) :
((roots_of_unity_equiv_nth_roots R k).symm x : R) = x :=
rfl
variables (k R)
instance roots_of_unity.fintype : fintype (roots_of_unity k R) :=
fintype.of_equiv {x // x ∈ nth_roots k (1 : R)} $ (roots_of_unity_equiv_nth_roots R k).symm
instance roots_of_unity.is_cyclic : is_cyclic (roots_of_unity k R) :=
is_cyclic_of_subgroup_is_domain ((units.coe_hom R).comp (roots_of_unity k R).subtype)
(units.ext.comp subtype.val_injective)
lemma card_roots_of_unity : fintype.card (roots_of_unity k R) ≤ k :=
calc fintype.card (roots_of_unity k R)
= fintype.card {x // x ∈ nth_roots k (1 : R)} :
fintype.card_congr (roots_of_unity_equiv_nth_roots R k)
... ≤ (nth_roots k (1 : R)).attach.card : multiset.card_le_of_le (multiset.dedup_le _)
... = (nth_roots k (1 : R)).card : multiset.card_attach
... ≤ k : card_nth_roots k 1
variables {k R}
lemma map_root_of_unity_eq_pow_self [ring_hom_class F R R] (σ : F) (ζ : roots_of_unity k R) :
∃ m : ℕ, σ ζ = ζ ^ m :=
begin
obtain ⟨m, hm⟩ := monoid_hom.map_cyclic (restrict_roots_of_unity σ k),
rw [←restrict_roots_of_unity_coe_apply, hm, zpow_eq_mod_order_of, ←int.to_nat_of_nonneg
(m.mod_nonneg (int.coe_nat_ne_zero.mpr (pos_iff_ne_zero.mp (order_of_pos ζ)))),
zpow_coe_nat, roots_of_unity.coe_pow],
exact ⟨(m % (order_of ζ)).to_nat, rfl⟩,
end
end is_domain
section reduced
variables (R) [comm_ring R] [is_reduced R]
@[simp] lemma mem_roots_of_unity_prime_pow_mul_iff (p k : ℕ) (m : ℕ+) [hp : fact p.prime]
[char_p R p] {ζ : Rˣ} :
ζ ∈ roots_of_unity (⟨p, hp.1.pos⟩ ^ k * m) R ↔ ζ ∈ roots_of_unity m R :=
by simp [mem_roots_of_unity']
end reduced
end roots_of_unity
/-- An element `ζ` is a primitive `k`-th root of unity if `ζ ^ k = 1`,
and if `l` satisfies `ζ ^ l = 1` then `k ∣ l`. -/
structure is_primitive_root (ζ : M) (k : ℕ) : Prop :=
(pow_eq_one : ζ ^ (k : ℕ) = 1)
(dvd_of_pow_eq_one : ∀ l : ℕ, ζ ^ l = 1 → k ∣ l)
/-- Turn a primitive root μ into a member of the `roots_of_unity` subgroup. -/
@[simps] def is_primitive_root.to_roots_of_unity {μ : M} {n : ℕ+} (h : is_primitive_root μ n) :
roots_of_unity n M := roots_of_unity.mk_of_pow_eq μ h.pow_eq_one
section primitive_roots
variables {k : ℕ}
/-- `primitive_roots k R` is the finset of primitive `k`-th roots of unity
in the integral domain `R`. -/
def primitive_roots (k : ℕ) (R : Type*) [comm_ring R] [is_domain R] : finset R :=
(nth_roots k (1 : R)).to_finset.filter (λ ζ, is_primitive_root ζ k)
variables [comm_ring R] [is_domain R]
@[simp] lemma mem_primitive_roots {ζ : R} (h0 : 0 < k) :
ζ ∈ primitive_roots k R ↔ is_primitive_root ζ k :=
begin
rw [primitive_roots, mem_filter, multiset.mem_to_finset, mem_nth_roots h0, and_iff_right_iff_imp],
exact is_primitive_root.pow_eq_one
end
end primitive_roots
namespace is_primitive_root
variables {k l : ℕ}
lemma iff_def (ζ : M) (k : ℕ) :
is_primitive_root ζ k ↔ (ζ ^ k = 1) ∧ (∀ l : ℕ, ζ ^ l = 1 → k ∣ l) :=
⟨λ ⟨h1, h2⟩, ⟨h1, h2⟩, λ ⟨h1, h2⟩, ⟨h1, h2⟩⟩
lemma mk_of_lt (ζ : M) (hk : 0 < k) (h1 : ζ ^ k = 1) (h : ∀ l : ℕ, 0 < l → l < k → ζ ^ l ≠ 1) :
is_primitive_root ζ k :=
begin
refine ⟨h1, _⟩,
intros l hl,
apply dvd_trans _ (k.gcd_dvd_right l),
suffices : k.gcd l = k, { rw this },
rw eq_iff_le_not_lt,
refine ⟨nat.le_of_dvd hk (k.gcd_dvd_left l), _⟩,
intro h', apply h _ (nat.gcd_pos_of_pos_left _ hk) h',
exact pow_gcd_eq_one _ h1 hl
end
section comm_monoid
variables {ζ : M} {f : F} (h : is_primitive_root ζ k)
@[nontriviality] lemma of_subsingleton [subsingleton M] (x : M) : is_primitive_root x 1 :=
⟨subsingleton.elim _ _, λ _ _, one_dvd _⟩
lemma pow_eq_one_iff_dvd (l : ℕ) : ζ ^ l = 1 ↔ k ∣ l :=
⟨h.dvd_of_pow_eq_one l,
by { rintro ⟨i, rfl⟩, simp only [pow_mul, h.pow_eq_one, one_pow, pnat.mul_coe] }⟩
lemma is_unit (h : is_primitive_root ζ k) (h0 : 0 < k) : is_unit ζ :=
begin
apply is_unit_of_mul_eq_one ζ (ζ ^ (k - 1)),
rw [← pow_succ, tsub_add_cancel_of_le h0.nat_succ_le, h.pow_eq_one]
end
lemma pow_ne_one_of_pos_of_lt (h0 : 0 < l) (hl : l < k) : ζ ^ l ≠ 1 :=
mt (nat.le_of_dvd h0 ∘ h.dvd_of_pow_eq_one _) $ not_le_of_lt hl
lemma pow_inj (h : is_primitive_root ζ k) ⦃i j : ℕ⦄ (hi : i < k) (hj : j < k) (H : ζ ^ i = ζ ^ j) :
i = j :=
begin
wlog hij : i ≤ j,
apply le_antisymm hij,
rw ← tsub_eq_zero_iff_le,
apply nat.eq_zero_of_dvd_of_lt _ (lt_of_le_of_lt tsub_le_self hj),
apply h.dvd_of_pow_eq_one,
rw [← ((h.is_unit (lt_of_le_of_lt (nat.zero_le _) hi)).pow i).mul_left_inj,
← pow_add, tsub_add_cancel_of_le hij, H, one_mul]
end
lemma one : is_primitive_root (1 : M) 1 :=
{ pow_eq_one := pow_one _,
dvd_of_pow_eq_one := λ l hl, one_dvd _ }
@[simp] lemma one_right_iff : is_primitive_root ζ 1 ↔ ζ = 1 :=
begin
split,
{ intro h, rw [← pow_one ζ, h.pow_eq_one] },
{ rintro rfl, exact one }
end
@[simp] lemma coe_submonoid_class_iff {M B : Type*} [comm_monoid M] [set_like B M]
[submonoid_class B M] {N : B} {ζ : N} : is_primitive_root (ζ : M) k ↔ is_primitive_root ζ k :=
by simp [iff_def, ← submonoid_class.coe_pow]
@[simp] lemma coe_units_iff {ζ : Mˣ} :
is_primitive_root (ζ : M) k ↔ is_primitive_root ζ k :=
by simp only [iff_def, units.ext_iff, units.coe_pow, units.coe_one]
lemma pow_of_coprime (h : is_primitive_root ζ k) (i : ℕ) (hi : i.coprime k) :
is_primitive_root (ζ ^ i) k :=
begin
by_cases h0 : k = 0,
{ subst k, simp only [*, pow_one, nat.coprime_zero_right] at * },
rcases h.is_unit (nat.pos_of_ne_zero h0) with ⟨ζ, rfl⟩,
rw [← units.coe_pow],
rw coe_units_iff at h ⊢,
refine
{ pow_eq_one := by rw [← pow_mul', pow_mul, h.pow_eq_one, one_pow],
dvd_of_pow_eq_one := _ },
intros l hl,
apply h.dvd_of_pow_eq_one,
rw [← pow_one ζ, ← zpow_coe_nat ζ, ← hi.gcd_eq_one, nat.gcd_eq_gcd_ab, zpow_add,
mul_pow, ← zpow_coe_nat, ← zpow_mul, mul_right_comm],
simp only [zpow_mul, hl, h.pow_eq_one, one_zpow, one_pow, one_mul, zpow_coe_nat]
end
lemma pow_of_prime (h : is_primitive_root ζ k) {p : ℕ} (hprime : nat.prime p) (hdiv : ¬ p ∣ k) :
is_primitive_root (ζ ^ p) k :=
h.pow_of_coprime p (hprime.coprime_iff_not_dvd.2 hdiv)
lemma pow_iff_coprime (h : is_primitive_root ζ k) (h0 : 0 < k) (i : ℕ) :
is_primitive_root (ζ ^ i) k ↔ i.coprime k :=
begin
refine ⟨_, h.pow_of_coprime i⟩,
intro hi,
obtain ⟨a, ha⟩ := i.gcd_dvd_left k,
obtain ⟨b, hb⟩ := i.gcd_dvd_right k,
suffices : b = k,
{ rwa [this, ← one_mul k, nat.mul_left_inj h0, eq_comm] at hb { occs := occurrences.pos [1] } },
rw [ha] at hi,
rw [mul_comm] at hb,
apply nat.dvd_antisymm ⟨i.gcd k, hb⟩ (hi.dvd_of_pow_eq_one b _),
rw [← pow_mul', ← mul_assoc, ← hb, pow_mul, h.pow_eq_one, one_pow]
end
protected lemma order_of (ζ : M) : is_primitive_root ζ (order_of ζ) :=
⟨pow_order_of_eq_one ζ, λ l, order_of_dvd_of_pow_eq_one⟩
lemma unique {ζ : M} (hk : is_primitive_root ζ k) (hl : is_primitive_root ζ l) : k = l :=
begin
wlog hkl : k ≤ l,
rcases hkl.eq_or_lt with rfl | hkl,
{ refl },
rcases k.eq_zero_or_pos with rfl | hk',
{ exact (zero_dvd_iff.mp $ hk.dvd_of_pow_eq_one l hl.pow_eq_one).symm },
exact absurd hk.pow_eq_one (hl.pow_ne_one_of_pos_of_lt hk' hkl)
end
lemma eq_order_of : k = order_of ζ := h.unique (is_primitive_root.order_of ζ)
protected lemma iff (hk : 0 < k) :
is_primitive_root ζ k ↔ ζ ^ k = 1 ∧ ∀ l : ℕ, 0 < l → l < k → ζ ^ l ≠ 1 :=
begin
refine ⟨λ h, ⟨h.pow_eq_one, λ l hl' hl, _⟩, λ ⟨hζ, hl⟩, is_primitive_root.mk_of_lt ζ hk hζ hl⟩,
rw h.eq_order_of at hl,
exact pow_ne_one_of_lt_order_of' hl'.ne' hl,
end
protected lemma not_iff : ¬ is_primitive_root ζ k ↔ order_of ζ ≠ k :=
⟨λ h hk, h $ hk ▸ is_primitive_root.order_of ζ,
λ h hk, h.symm $ hk.unique $ is_primitive_root.order_of ζ⟩
lemma pow_of_dvd (h : is_primitive_root ζ k) {p : ℕ} (hp : p ≠ 0) (hdiv : p ∣ k) :
is_primitive_root (ζ ^ p) (k / p) :=
begin
suffices : order_of (ζ ^ p) = k / p,
{ exact this ▸ is_primitive_root.order_of (ζ ^ p) },
rw [order_of_pow' _ hp, ← eq_order_of h, nat.gcd_eq_right hdiv]
end
protected
lemma mem_roots_of_unity {ζ : Mˣ} {n : ℕ+} (h : is_primitive_root ζ n) : ζ ∈ roots_of_unity n M :=
h.pow_eq_one
/-- If there is a `n`-th primitive root of unity in `R` and `b` divides `n`,
then there is a `b`-th primitive root of unity in `R`. -/
lemma pow {n : ℕ} {a b : ℕ} (hn : 0 < n) (h : is_primitive_root ζ n) (hprod : n = a * b) :
is_primitive_root (ζ ^ a) b :=
begin
subst n,
simp only [iff_def, ← pow_mul, h.pow_eq_one, eq_self_iff_true, true_and],
intros l hl,
have ha0 : a ≠ 0, { rintro rfl, simpa only [nat.not_lt_zero, zero_mul] using hn },
rwa ← mul_dvd_mul_iff_left ha0,
exact h.dvd_of_pow_eq_one _ hl
end
section maps
open function
lemma map_of_injective [monoid_hom_class F M N] (h : is_primitive_root ζ k) (hf : injective f) :
is_primitive_root (f ζ) k :=
{ pow_eq_one := by rw [←map_pow, h.pow_eq_one, _root_.map_one],
dvd_of_pow_eq_one := begin
rw h.eq_order_of,
intros l hl,
rw [←map_pow, ←map_one f] at hl,
exact order_of_dvd_of_pow_eq_one (hf hl)
end }
lemma of_map_of_injective [monoid_hom_class F M N] (h : is_primitive_root (f ζ) k)
(hf : injective f) : is_primitive_root ζ k :=
{ pow_eq_one := by { apply_fun f, rw [map_pow, _root_.map_one, h.pow_eq_one] },
dvd_of_pow_eq_one := begin
rw h.eq_order_of,
intros l hl,
apply_fun f at hl,
rw [map_pow, _root_.map_one] at hl,
exact order_of_dvd_of_pow_eq_one hl
end }
lemma map_iff_of_injective [monoid_hom_class F M N] (hf : injective f) :
is_primitive_root (f ζ) k ↔ is_primitive_root ζ k :=
⟨λ h, h.of_map_of_injective hf, λ h, h.map_of_injective hf⟩
end maps
end comm_monoid
section comm_monoid_with_zero
variables {M₀ : Type*} [comm_monoid_with_zero M₀]
lemma zero [nontrivial M₀] : is_primitive_root (0 : M₀) 0 :=
⟨pow_zero 0, λ l hl, by simpa [zero_pow_eq, show ∀ p, ¬p → false ↔ p, from @not_not] using hl⟩
protected lemma ne_zero [nontrivial M₀] {ζ : M₀} (h : is_primitive_root ζ k) : k ≠ 0 → ζ ≠ 0 :=
mt $ λ hn, h.unique (hn.symm ▸ is_primitive_root.zero)
end comm_monoid_with_zero
section division_comm_monoid
variables {ζ : G}
lemma zpow_eq_one (h : is_primitive_root ζ k) : ζ ^ (k : ℤ) = 1 :=
by { rw zpow_coe_nat, exact h.pow_eq_one }
lemma zpow_eq_one_iff_dvd (h : is_primitive_root ζ k) (l : ℤ) :
ζ ^ l = 1 ↔ (k : ℤ) ∣ l :=
begin
by_cases h0 : 0 ≤ l,
{ lift l to ℕ using h0, rw [zpow_coe_nat], norm_cast, exact h.pow_eq_one_iff_dvd l },
{ have : 0 ≤ -l, { simp only [not_le, neg_nonneg] at h0 ⊢, exact le_of_lt h0 },
lift -l to ℕ using this with l' hl',
rw [← dvd_neg, ← hl'],
norm_cast,
rw [← h.pow_eq_one_iff_dvd, ← inv_inj, ← zpow_neg, ← hl', zpow_coe_nat, inv_one] }
end
lemma inv (h : is_primitive_root ζ k) : is_primitive_root ζ⁻¹ k :=
{ pow_eq_one := by simp only [h.pow_eq_one, inv_one, eq_self_iff_true, inv_pow],
dvd_of_pow_eq_one :=
begin
intros l hl,
apply h.dvd_of_pow_eq_one l,
rw [← inv_inj, ← inv_pow, hl, inv_one]
end }
@[simp] lemma inv_iff : is_primitive_root ζ⁻¹ k ↔ is_primitive_root ζ k :=
by { refine ⟨_, λ h, inv h⟩, intro h, rw [← inv_inv ζ], exact inv h }
lemma zpow_of_gcd_eq_one (h : is_primitive_root ζ k) (i : ℤ) (hi : i.gcd k = 1) :
is_primitive_root (ζ ^ i) k :=
begin
by_cases h0 : 0 ≤ i,
{ lift i to ℕ using h0,
rw zpow_coe_nat,
exact h.pow_of_coprime i hi },
have : 0 ≤ -i, { simp only [not_le, neg_nonneg] at h0 ⊢, exact le_of_lt h0 },
lift -i to ℕ using this with i' hi',
rw [← inv_iff, ← zpow_neg, ← hi', zpow_coe_nat],
apply h.pow_of_coprime,
rw [int.gcd, ← int.nat_abs_neg, ← hi'] at hi,
exact hi
end
end division_comm_monoid
section is_domain
variables {ζ : R}
variables [comm_ring R] [is_domain R]
@[simp] lemma primitive_roots_zero : primitive_roots 0 R = ∅ :=
begin
rw [← finset.val_eq_zero, ← multiset.subset_zero, ← nth_roots_zero (1 : R), primitive_roots],
simp only [finset.not_mem_empty, forall_const, forall_prop_of_false, multiset.to_finset_zero,
finset.filter_true_of_mem, finset.empty_val, not_false_iff,
multiset.zero_subset, nth_roots_zero]
end
@[simp] lemma primitive_roots_one : primitive_roots 1 R = {(1 : R)} :=
begin
apply finset.eq_singleton_iff_unique_mem.2,
split,
{ simp only [is_primitive_root.one_right_iff, mem_primitive_roots zero_lt_one] },
{ intros x hx,
rw [mem_primitive_roots zero_lt_one, is_primitive_root.one_right_iff] at hx,
exact hx }
end
lemma ne_zero' {n : ℕ+} (hζ : is_primitive_root ζ n) : ne_zero ((n : ℕ) : R) :=
begin
let p := ring_char R,
have hfin := (multiplicity.finite_nat_iff.2 ⟨char_p.char_ne_one R p, n.pos⟩),
obtain ⟨m, hm⟩ := multiplicity.exists_eq_pow_mul_and_not_dvd hfin,
by_cases hp : p ∣ n,
{ obtain ⟨k, hk⟩ := nat.exists_eq_succ_of_ne_zero (multiplicity.pos_of_dvd hfin hp).ne',
haveI : ne_zero p := ne_zero.of_pos (nat.pos_of_dvd_of_pos hp n.pos),
haveI hpri : fact p.prime := char_p.char_is_prime_of_pos R p,
have := hζ.pow_eq_one,
rw [hm.1, hk, pow_succ, mul_assoc, pow_mul', ← frobenius_def, ← frobenius_one p] at this,
exfalso,
have hpos : 0 < p ^ k * m,
{ refine (mul_pos (pow_pos hpri.1.pos _) (nat.pos_of_ne_zero (λ h, _))),
have H := hm.1,
rw [h] at H,
simpa using H },
refine hζ.pow_ne_one_of_pos_of_lt hpos _ (frobenius_inj R p this),
{ rw [hm.1, hk, pow_succ, mul_assoc, mul_comm p],
exact lt_mul_of_one_lt_right hpos hpri.1.one_lt } },
{ exact ne_zero.of_not_dvd R hp }
end
end is_domain
section is_domain
variables [comm_ring R]
variables {ζ : Rˣ} (h : is_primitive_root ζ k)
lemma eq_neg_one_of_two_right [no_zero_divisors R] {ζ : R} (h : is_primitive_root ζ 2) : ζ = -1 :=
begin
apply (eq_or_eq_neg_of_sq_eq_sq ζ 1 _).resolve_left,
{ rw [← pow_one ζ], apply h.pow_ne_one_of_pos_of_lt; dec_trivial },
{ simp only [h.pow_eq_one, one_pow] }
end
lemma neg_one (p : ℕ) [nontrivial R] [h : char_p R p] (hp : p ≠ 2) : is_primitive_root (-1 : R) 2 :=
begin
convert is_primitive_root.order_of (-1 : R),
rw [order_of_neg_one, if_neg],
rwa ring_char.eq_iff.mpr h
end
/-- The (additive) monoid equivalence between `zmod k`
and the powers of a primitive root of unity `ζ`. -/
def zmod_equiv_zpowers (h : is_primitive_root ζ k) : zmod k ≃+ additive (subgroup.zpowers ζ) :=
add_equiv.of_bijective
(add_monoid_hom.lift_of_right_inverse (int.cast_add_hom $ zmod k) _ zmod.int_cast_right_inverse
⟨{ to_fun := λ i, additive.of_mul (⟨_, i, rfl⟩ : subgroup.zpowers ζ),
map_zero' := by { simp only [zpow_zero], refl },
map_add' := by { intros i j, simp only [zpow_add], refl } },
(λ i hi,
begin
simp only [add_monoid_hom.mem_ker, char_p.int_cast_eq_zero_iff (zmod k) k,
add_monoid_hom.coe_mk, int.coe_cast_add_hom] at hi ⊢,
obtain ⟨i, rfl⟩ := hi,
simp only [zpow_mul, h.pow_eq_one, one_zpow, zpow_coe_nat],
refl
end)⟩)
begin
split,
{ rw injective_iff_map_eq_zero,
intros i hi,
rw subtype.ext_iff at hi,
have := (h.zpow_eq_one_iff_dvd _).mp hi,
rw [← (char_p.int_cast_eq_zero_iff (zmod k) k _).mpr this, eq_comm],
exact zmod.int_cast_right_inverse i },
{ rintro ⟨ξ, i, rfl⟩,
refine ⟨int.cast_add_hom _ i, _⟩,
rw [add_monoid_hom.lift_of_right_inverse_comp_apply],
refl }
end
@[simp] lemma zmod_equiv_zpowers_apply_coe_int (i : ℤ) :
h.zmod_equiv_zpowers i = additive.of_mul (⟨ζ ^ i, i, rfl⟩ : subgroup.zpowers ζ) :=
add_monoid_hom.lift_of_right_inverse_comp_apply _ _ zmod.int_cast_right_inverse _ _
@[simp] lemma zmod_equiv_zpowers_apply_coe_nat (i : ℕ) :
h.zmod_equiv_zpowers i = additive.of_mul (⟨ζ ^ i, i, rfl⟩ : subgroup.zpowers ζ) :=
begin
have : (i : zmod k) = (i : ℤ), by norm_cast,
simp only [this, zmod_equiv_zpowers_apply_coe_int, zpow_coe_nat],
refl
end
@[simp] lemma zmod_equiv_zpowers_symm_apply_zpow (i : ℤ) :
h.zmod_equiv_zpowers.symm (additive.of_mul (⟨ζ ^ i, i, rfl⟩ : subgroup.zpowers ζ)) = i :=
by rw [← h.zmod_equiv_zpowers.symm_apply_apply i, zmod_equiv_zpowers_apply_coe_int]
@[simp] lemma zmod_equiv_zpowers_symm_apply_zpow' (i : ℤ) :
h.zmod_equiv_zpowers.symm ⟨ζ ^ i, i, rfl⟩ = i :=
h.zmod_equiv_zpowers_symm_apply_zpow i
@[simp] lemma zmod_equiv_zpowers_symm_apply_pow (i : ℕ) :
h.zmod_equiv_zpowers.symm (additive.of_mul (⟨ζ ^ i, i, rfl⟩ : subgroup.zpowers ζ)) = i :=
by rw [← h.zmod_equiv_zpowers.symm_apply_apply i, zmod_equiv_zpowers_apply_coe_nat]
@[simp] lemma zmod_equiv_zpowers_symm_apply_pow' (i : ℕ) :
h.zmod_equiv_zpowers.symm ⟨ζ ^ i, i, rfl⟩ = i :=
h.zmod_equiv_zpowers_symm_apply_pow i
variables [is_domain R]
lemma zpowers_eq {k : ℕ+} {ζ : Rˣ} (h : is_primitive_root ζ k) :
subgroup.zpowers ζ = roots_of_unity k R :=
begin
apply set_like.coe_injective,
haveI F : fintype (subgroup.zpowers ζ) := fintype.of_equiv _ (h.zmod_equiv_zpowers).to_equiv,
refine @set.eq_of_subset_of_card_le Rˣ (subgroup.zpowers ζ) (roots_of_unity k R)
F (roots_of_unity.fintype R k)
(subgroup.zpowers_subset $ show ζ ∈ roots_of_unity k R, from h.pow_eq_one) _,
calc fintype.card (roots_of_unity k R)
≤ k : card_roots_of_unity R k
... = fintype.card (zmod k) : (zmod.card k).symm
... = fintype.card (subgroup.zpowers ζ) : fintype.card_congr (h.zmod_equiv_zpowers).to_equiv
end
lemma eq_pow_of_mem_roots_of_unity {k : ℕ+} {ζ ξ : Rˣ}
(h : is_primitive_root ζ k) (hξ : ξ ∈ roots_of_unity k R) :
∃ (i : ℕ) (hi : i < k), ζ ^ i = ξ :=
begin
obtain ⟨n, rfl⟩ : ∃ n : ℤ, ζ ^ n = ξ, by rwa [← h.zpowers_eq] at hξ,
have hk0 : (0 : ℤ) < k := by exact_mod_cast k.pos,
let i := n % k,
have hi0 : 0 ≤ i := int.mod_nonneg _ (ne_of_gt hk0),
lift i to ℕ using hi0 with i₀ hi₀,
refine ⟨i₀, _, _⟩,
{ zify, rw [hi₀], exact int.mod_lt_of_pos _ hk0 },
{ have aux := h.zpow_eq_one, rw [← coe_coe] at aux,
rw [← zpow_coe_nat, hi₀, ← int.mod_add_div n k, zpow_add, zpow_mul,
aux, one_zpow, mul_one] }
end
lemma eq_pow_of_pow_eq_one {k : ℕ} {ζ ξ : R}
(h : is_primitive_root ζ k) (hξ : ξ ^ k = 1) (h0 : 0 < k) :
∃ i < k, ζ ^ i = ξ :=
begin
obtain ⟨ζ, rfl⟩ := h.is_unit h0,
obtain ⟨ξ, rfl⟩ := is_unit_of_pow_eq_one ξ k hξ h0,
obtain ⟨k, rfl⟩ : ∃ k' : ℕ+, k = k' := ⟨⟨k, h0⟩, rfl⟩,
simp only [← units.coe_pow, ← units.ext_iff],
rw coe_units_iff at h,
apply h.eq_pow_of_mem_roots_of_unity,
rw [mem_roots_of_unity, units.ext_iff, units.coe_pow, hξ, units.coe_one]
end
lemma is_primitive_root_iff' {k : ℕ+} {ζ ξ : Rˣ} (h : is_primitive_root ζ k) :
is_primitive_root ξ k ↔ ∃ (i < (k : ℕ)) (hi : i.coprime k), ζ ^ i = ξ :=
begin
split,
{ intro hξ,
obtain ⟨i, hik, rfl⟩ := h.eq_pow_of_mem_roots_of_unity hξ.pow_eq_one,
rw h.pow_iff_coprime k.pos at hξ,
exact ⟨i, hik, hξ, rfl⟩ },
{ rintro ⟨i, -, hi, rfl⟩, exact h.pow_of_coprime i hi }
end
lemma is_primitive_root_iff {k : ℕ} {ζ ξ : R} (h : is_primitive_root ζ k) (h0 : 0 < k) :
is_primitive_root ξ k ↔ ∃ (i < k) (hi : i.coprime k), ζ ^ i = ξ :=
begin
split,
{ intro hξ,
obtain ⟨i, hik, rfl⟩ := h.eq_pow_of_pow_eq_one hξ.pow_eq_one h0,
rw h.pow_iff_coprime h0 at hξ,
exact ⟨i, hik, hξ, rfl⟩ },
{ rintro ⟨i, -, hi, rfl⟩, exact h.pow_of_coprime i hi }
end
lemma card_roots_of_unity' {n : ℕ+} (h : is_primitive_root ζ n) :
fintype.card (roots_of_unity n R) = n :=
begin
let e := h.zmod_equiv_zpowers,
haveI F : fintype (subgroup.zpowers ζ) := fintype.of_equiv _ e.to_equiv,
calc fintype.card (roots_of_unity n R)
= fintype.card (subgroup.zpowers ζ) : fintype.card_congr $ by rw h.zpowers_eq
... = fintype.card (zmod n) : fintype.card_congr e.to_equiv.symm
... = n : zmod.card n
end
lemma card_roots_of_unity {ζ : R} {n : ℕ+} (h : is_primitive_root ζ n) :
fintype.card (roots_of_unity n R) = n :=
begin
obtain ⟨ζ, hζ⟩ := h.is_unit n.pos,
rw [← hζ, is_primitive_root.coe_units_iff] at h,
exact h.card_roots_of_unity'
end
/-- The cardinality of the multiset `nth_roots ↑n (1 : R)` is `n`
if there is a primitive root of unity in `R`. -/
lemma card_nth_roots {ζ : R} {n : ℕ} (h : is_primitive_root ζ n) :
(nth_roots n (1 : R)).card = n :=
begin
cases nat.eq_zero_or_pos n with hzero hpos,
{ simp only [hzero, multiset.card_zero, nth_roots_zero] },
rw eq_iff_le_not_lt,
use card_nth_roots n 1,
{ rw [not_lt],
have hcard : fintype.card {x // x ∈ nth_roots n (1 : R)}
≤ (nth_roots n (1 : R)).attach.card := multiset.card_le_of_le (multiset.dedup_le _),
rw multiset.card_attach at hcard,
rw ← pnat.to_pnat'_coe hpos at hcard h ⊢,
set m := nat.to_pnat' n,
rw [← fintype.card_congr (roots_of_unity_equiv_nth_roots R m), card_roots_of_unity h] at hcard,
exact hcard }
end
/-- The multiset `nth_roots ↑n (1 : R)` has no repeated elements
if there is a primitive root of unity in `R`. -/
lemma nth_roots_nodup {ζ : R} {n : ℕ} (h : is_primitive_root ζ n) : (nth_roots n (1 : R)).nodup :=
begin
cases nat.eq_zero_or_pos n with hzero hpos,
{ simp only [hzero, multiset.nodup_zero, nth_roots_zero] },
apply (@multiset.dedup_eq_self R _ _).1,
rw eq_iff_le_not_lt,
split,
{ exact multiset.dedup_le (nth_roots n (1 : R)) },
{ by_contra ha,
replace ha := multiset.card_lt_of_lt ha,
rw card_nth_roots h at ha,
have hrw : (nth_roots n (1 : R)).dedup.card =
fintype.card {x // x ∈ (nth_roots n (1 : R))},
{ set fs := (⟨(nth_roots n (1 : R)).dedup, multiset.nodup_dedup _⟩ : finset R),
rw [← finset.card_mk, ← fintype.card_of_subtype fs _],
intro x,
simp only [multiset.mem_dedup, finset.mem_mk] },
rw ← pnat.to_pnat'_coe hpos at h hrw ha,
set m := nat.to_pnat' n,
rw [hrw, ← fintype.card_congr (roots_of_unity_equiv_nth_roots R m),
card_roots_of_unity h] at ha,
exact nat.lt_asymm ha ha }
end
@[simp] lemma card_nth_roots_finset {ζ : R} {n : ℕ} (h : is_primitive_root ζ n) :
(nth_roots_finset n R).card = n :=
by rw [nth_roots_finset, ← multiset.to_finset_eq (nth_roots_nodup h), card_mk, h.card_nth_roots]
open_locale nat
/-- If an integral domain has a primitive `k`-th root of unity, then it has `φ k` of them. -/
lemma card_primitive_roots {ζ : R} {k : ℕ} (h : is_primitive_root ζ k) :
(primitive_roots k R).card = φ k :=
begin
by_cases h0 : k = 0,
{ simp [h0], },
symmetry,
refine finset.card_congr (λ i _, ζ ^ i) _ _ _,
{ simp only [true_and, and_imp, mem_filter, mem_range, mem_univ],
rintro i - hi,
rw mem_primitive_roots (nat.pos_of_ne_zero h0),
exact h.pow_of_coprime i hi.symm },
{ simp only [true_and, and_imp, mem_filter, mem_range, mem_univ],
rintro i j hi - hj - H,
exact h.pow_inj hi hj H },
{ simp only [exists_prop, true_and, mem_filter, mem_range, mem_univ],
intros ξ hξ,
rw [mem_primitive_roots (nat.pos_of_ne_zero h0),
h.is_primitive_root_iff (nat.pos_of_ne_zero h0)] at hξ,
rcases hξ with ⟨i, hin, hi, H⟩,
exact ⟨i, ⟨hin, hi.symm⟩, H⟩ }
end
/-- The sets `primitive_roots k R` are pairwise disjoint. -/
lemma disjoint {k l : ℕ} (h : k ≠ l) :
disjoint (primitive_roots k R) (primitive_roots l R) :=
begin
by_cases hk : k = 0, { simp [hk], },
by_cases hl : l = 0, { simp [hl], },
intro z,
simp only [finset.inf_eq_inter, finset.mem_inter, mem_primitive_roots,
nat.pos_of_ne_zero hk, nat.pos_of_ne_zero hl, iff_def],
rintro ⟨⟨hzk, Hzk⟩, ⟨hzl, Hzl⟩⟩,
apply_rules [h, nat.dvd_antisymm, Hzk, Hzl, hzk, hzl]
end
/-- `nth_roots n` as a `finset` is equal to the union of `primitive_roots i R` for `i ∣ n`
if there is a primitive root of unity in `R`.
This holds for any `nat`, not just `pnat`, see `nth_roots_one_eq_bUnion_primitive_roots`. -/
lemma nth_roots_one_eq_bUnion_primitive_roots' {ζ : R} {n : ℕ+} (h : is_primitive_root ζ n) :
nth_roots_finset n R = (nat.divisors ↑n).bUnion (λ i, (primitive_roots i R)) :=
begin
symmetry,
apply finset.eq_of_subset_of_card_le,
{ intros x,
simp only [nth_roots_finset, ← multiset.to_finset_eq (nth_roots_nodup h),
exists_prop, finset.mem_bUnion, finset.mem_filter, finset.mem_range, mem_nth_roots,
finset.mem_mk, nat.mem_divisors, and_true, ne.def, pnat.ne_zero, pnat.pos, not_false_iff],
rintro ⟨a, ⟨d, hd⟩, ha⟩,
have hazero : 0 < a,
{ contrapose! hd with ha0,
simp only [nonpos_iff_eq_zero, zero_mul, *] at *,
exact n.ne_zero },
rw mem_primitive_roots hazero at ha,
rw [hd, pow_mul, ha.pow_eq_one, one_pow] },
{ apply le_of_eq,
rw [h.card_nth_roots_finset, finset.card_bUnion],
{ nth_rewrite_lhs 0 ← nat.sum_totient n,
refine sum_congr rfl _,
simp only [nat.mem_divisors],
rintro k ⟨⟨d, hd⟩, -⟩,
rw mul_comm at hd,
rw (h.pow n.pos hd).card_primitive_roots },
{ intros i hi j hj hdiff,
exact disjoint hdiff } }
end
/-- `nth_roots n` as a `finset` is equal to the union of `primitive_roots i R` for `i ∣ n`
if there is a primitive root of unity in `R`. -/
lemma nth_roots_one_eq_bUnion_primitive_roots {ζ : R} {n : ℕ}
(h : is_primitive_root ζ n) :
nth_roots_finset n R = (nat.divisors n).bUnion (λ i, (primitive_roots i R)) :=
begin
by_cases hn : n = 0,
{ simp [hn], },
exact @nth_roots_one_eq_bUnion_primitive_roots' _ _ _ _ ⟨n, nat.pos_of_ne_zero hn⟩ h
end
end is_domain
section minpoly
open minpoly
section comm_ring
variables {n : ℕ} {K : Type*} [comm_ring K] {μ : K} (h : is_primitive_root μ n) (hpos : 0 < n)
include n μ h hpos
/--`μ` is integral over `ℤ`. -/
lemma is_integral : is_integral ℤ μ :=
begin
use (X ^ n - 1),
split,
{ exact (monic_X_pow_sub_C 1 (ne_of_lt hpos).symm) },
{ simp only [((is_primitive_root.iff_def μ n).mp h).left, eval₂_one, eval₂_X_pow, eval₂_sub,
sub_self] }
end
section is_domain
variables [is_domain K] [char_zero K]
omit hpos
/--The minimal polynomial of a root of unity `μ` divides `X ^ n - 1`. -/
lemma minpoly_dvd_X_pow_sub_one : minpoly ℤ μ ∣ X ^ n - 1 :=
begin
rcases n.eq_zero_or_pos with rfl | hpos,
{ simp },
apply minpoly.gcd_domain_dvd (is_integral h hpos) (monic_X_pow_sub_C 1 hpos.ne').ne_zero,
simp only [((is_primitive_root.iff_def μ n).mp h).left, aeval_X_pow, eq_int_cast,
int.cast_one, aeval_one, alg_hom.map_sub, sub_self]
end
/-- The reduction modulo `p` of the minimal polynomial of a root of unity `μ` is separable. -/
lemma separable_minpoly_mod {p : ℕ} [fact p.prime] (hdiv : ¬p ∣ n) :
separable (map (int.cast_ring_hom (zmod p)) (minpoly ℤ μ)) :=
begin
have hdvd : (map (int.cast_ring_hom (zmod p))
(minpoly ℤ μ)) ∣ X ^ n - 1,
{ simpa [polynomial.map_pow, map_X, polynomial.map_one, polynomial.map_sub] using
ring_hom.map_dvd (map_ring_hom (int.cast_ring_hom (zmod p)))
(minpoly_dvd_X_pow_sub_one h) },
refine separable.of_dvd (separable_X_pow_sub_C 1 _ one_ne_zero) hdvd,
by_contra hzero,
exact hdiv ((zmod.nat_coe_zmod_eq_zero_iff_dvd n p).1 hzero)
end
/-- The reduction modulo `p` of the minimal polynomial of a root of unity `μ` is squarefree. -/
lemma squarefree_minpoly_mod {p : ℕ} [fact p.prime] (hdiv : ¬ p ∣ n) :
squarefree (map (int.cast_ring_hom (zmod p)) (minpoly ℤ μ)) :=
(separable_minpoly_mod h hdiv).squarefree
/- Let `P` be the minimal polynomial of a root of unity `μ` and `Q` be the minimal polynomial of
`μ ^ p`, where `p` is a prime that does not divide `n`. Then `P` divides `expand ℤ p Q`. -/
lemma minpoly_dvd_expand {p : ℕ} (hprime : nat.prime p) (hdiv : ¬ p ∣ n) :
minpoly ℤ μ ∣ expand ℤ p (minpoly ℤ (μ ^ p)) :=
begin
rcases n.eq_zero_or_pos with rfl | hpos,
{ simp * at *, },
refine minpoly.gcd_domain_dvd (h.is_integral hpos) _ _,
{ apply monic.ne_zero,
rw [polynomial.monic, leading_coeff, nat_degree_expand, mul_comm, coeff_expand_mul'
(nat.prime.pos hprime), ← leading_coeff, ← polynomial.monic],
exact minpoly.monic (is_integral (pow_of_prime h hprime hdiv) hpos) },
{ rw [aeval_def, coe_expand, ← comp, eval₂_eq_eval_map, map_comp, polynomial.map_pow, map_X,
eval_comp, eval_pow, eval_X, ← eval₂_eq_eval_map, ← aeval_def],
exact minpoly.aeval _ _ }
end
/- Let `P` be the minimal polynomial of a root of unity `μ` and `Q` be the minimal polynomial of
`μ ^ p`, where `p` is a prime that does not divide `n`. Then `P` divides `Q ^ p` modulo `p`. -/
lemma minpoly_dvd_pow_mod {p : ℕ} [hprime : fact p.prime] (hdiv : ¬ p ∣ n) :
map (int.cast_ring_hom (zmod p)) (minpoly ℤ μ) ∣
map (int.cast_ring_hom (zmod p)) (minpoly ℤ (μ ^ p)) ^ p :=
begin
set Q := minpoly ℤ (μ ^ p),
have hfrob : map (int.cast_ring_hom (zmod p)) Q ^ p =
map (int.cast_ring_hom (zmod p)) (expand ℤ p Q),
by rw [← zmod.expand_card, map_expand],
rw [hfrob],
apply ring_hom.map_dvd (map_ring_hom (int.cast_ring_hom (zmod p))),
exact minpoly_dvd_expand h hprime.1 hdiv
end
/- Let `P` be the minimal polynomial of a root of unity `μ` and `Q` be the minimal polynomial of
`μ ^ p`, where `p` is a prime that does not divide `n`. Then `P` divides `Q` modulo `p`. -/
lemma minpoly_dvd_mod_p {p : ℕ} [hprime : fact p.prime] (hdiv : ¬ p ∣ n) :
map (int.cast_ring_hom (zmod p)) (minpoly ℤ μ) ∣
map (int.cast_ring_hom (zmod p)) (minpoly ℤ (μ ^ p)) :=
(unique_factorization_monoid.dvd_pow_iff_dvd_of_squarefree (squarefree_minpoly_mod h
hdiv) hprime.1.ne_zero).1 (minpoly_dvd_pow_mod h hdiv)
/-- If `p` is a prime that does not divide `n`,
then the minimal polynomials of a primitive `n`-th root of unity `μ`
and of `μ ^ p` are the same. -/
lemma minpoly_eq_pow {p : ℕ} [hprime : fact p.prime] (hdiv : ¬ p ∣ n) :
minpoly ℤ μ = minpoly ℤ (μ ^ p) :=
begin
by_cases hn : n = 0, { simp * at *, },
have hpos := nat.pos_of_ne_zero hn,
by_contra hdiff,
set P := minpoly ℤ μ,
set Q := minpoly ℤ (μ ^ p),
have Pmonic : P.monic := minpoly.monic (h.is_integral hpos),
have Qmonic : Q.monic := minpoly.monic ((h.pow_of_prime hprime.1 hdiv).is_integral hpos),
have Pirr : irreducible P := minpoly.irreducible (h.is_integral hpos),
have Qirr : irreducible Q :=
minpoly.irreducible ((h.pow_of_prime hprime.1 hdiv).is_integral hpos),
have PQprim : is_primitive (P * Q) := Pmonic.is_primitive.mul Qmonic.is_primitive,
have prod : P * Q ∣ X ^ n - 1,
{ rw [(is_primitive.int.dvd_iff_map_cast_dvd_map_cast (P * Q) (X ^ n - 1) PQprim
(monic_X_pow_sub_C (1 : ℤ) (ne_of_gt hpos)).is_primitive), polynomial.map_mul],
refine is_coprime.mul_dvd _ _ _,
{ have aux := is_primitive.int.irreducible_iff_irreducible_map_cast Pmonic.is_primitive,
refine (dvd_or_coprime _ _ (aux.1 Pirr)).resolve_left _,
rw map_dvd_map (int.cast_ring_hom ℚ) int.cast_injective Pmonic,
intro hdiv,
refine hdiff (eq_of_monic_of_associated Pmonic Qmonic _),
exact associated_of_dvd_dvd hdiv (Pirr.dvd_symm Qirr hdiv) },
{ apply (map_dvd_map (int.cast_ring_hom ℚ) int.cast_injective Pmonic).2,
exact minpoly_dvd_X_pow_sub_one h },
{ apply (map_dvd_map (int.cast_ring_hom ℚ) int.cast_injective Qmonic).2,
exact minpoly_dvd_X_pow_sub_one (pow_of_prime h hprime.1 hdiv) } },
replace prod := ring_hom.map_dvd ((map_ring_hom (int.cast_ring_hom (zmod p)))) prod,
rw [coe_map_ring_hom, polynomial.map_mul, polynomial.map_sub,
polynomial.map_one, polynomial.map_pow, map_X] at prod,
obtain ⟨R, hR⟩ := minpoly_dvd_mod_p h hdiv,
rw [hR, ← mul_assoc, ← polynomial.map_mul, ← sq, polynomial.map_pow] at prod,
have habs : map (int.cast_ring_hom (zmod p)) P ^ 2 ∣ map (int.cast_ring_hom (zmod p)) P ^ 2 * R,
{ use R },
replace habs := lt_of_lt_of_le (part_enat.coe_lt_coe.2 one_lt_two)
(multiplicity.le_multiplicity_of_pow_dvd (dvd_trans habs prod)),
have hfree : squarefree (X ^ n - 1 : (zmod p)[X]),
{ exact (separable_X_pow_sub_C 1
(λ h, hdiv $ (zmod.nat_coe_zmod_eq_zero_iff_dvd n p).1 h) one_ne_zero).squarefree },
cases (multiplicity.squarefree_iff_multiplicity_le_one (X ^ n - 1)).1 hfree
(map (int.cast_ring_hom (zmod p)) P) with hle hunit,
{ rw nat.cast_one at habs, exact hle.not_lt habs },
{ replace hunit := degree_eq_zero_of_is_unit hunit,
rw degree_map_eq_of_leading_coeff_ne_zero (int.cast_ring_hom (zmod p)) _ at hunit,
{ exact (minpoly.degree_pos (is_integral h hpos)).ne' hunit },
simp only [Pmonic, eq_int_cast, monic.leading_coeff, int.cast_one, ne.def,
not_false_iff, one_ne_zero] }
end
/-- If `m : ℕ` is coprime with `n`,
then the minimal polynomials of a primitive `n`-th root of unity `μ`
and of `μ ^ m` are the same. -/
lemma minpoly_eq_pow_coprime {m : ℕ} (hcop : nat.coprime m n) :
minpoly ℤ μ = minpoly ℤ (μ ^ m) :=
begin
revert n hcop,
refine unique_factorization_monoid.induction_on_prime m _ _ _,
{ intros n hn h,
congr,
simpa [(nat.coprime_zero_left n).mp hn] using h },
{ intros u hunit n hcop h,
congr,
simp [nat.is_unit_iff.mp hunit] },
{ intros a p ha hprime hind n hcop h,
rw hind (nat.coprime.coprime_mul_left hcop) h, clear hind,
replace hprime := nat.prime_iff.2 hprime,
have hdiv := (nat.prime.coprime_iff_not_dvd hprime).1 (nat.coprime.coprime_mul_right hcop),
haveI := fact.mk hprime,
rw [minpoly_eq_pow (h.pow_of_coprime a (nat.coprime.coprime_mul_left hcop)) hdiv],
congr' 1,
ring_exp }
end
/-- If `m : ℕ` is coprime with `n`,
then the minimal polynomial of a primitive `n`-th root of unity `μ`
has `μ ^ m` as root. -/
lemma pow_is_root_minpoly {m : ℕ} (hcop : nat.coprime m n) :
is_root (map (int.cast_ring_hom K) (minpoly ℤ μ)) (μ ^ m) :=
by simpa [minpoly_eq_pow_coprime h hcop, eval_map, aeval_def (μ ^ m) _]
using minpoly.aeval ℤ (μ ^ m)
/-- `primitive_roots n K` is a subset of the roots of the minimal polynomial of a primitive
`n`-th root of unity `μ`. -/
lemma is_roots_of_minpoly : primitive_roots n K ⊆ (map (int.cast_ring_hom K)
(minpoly ℤ μ)).roots.to_finset :=
begin
by_cases hn : n = 0, { simp * at *, },
have hpos := nat.pos_of_ne_zero hn,
intros x hx,
obtain ⟨m, hle, hcop, rfl⟩ := (is_primitive_root_iff h hpos).1 ((mem_primitive_roots hpos).1 hx),
simpa [multiset.mem_to_finset,
mem_roots (map_monic_ne_zero $ minpoly.monic $ is_integral h hpos)]
using pow_is_root_minpoly h hcop
end
/-- The degree of the minimal polynomial of `μ` is at least `totient n`. -/
lemma totient_le_degree_minpoly : nat.totient n ≤ (minpoly ℤ μ).nat_degree :=
let P : ℤ[X] := minpoly ℤ μ,-- minimal polynomial of `μ`
P_K : K[X] := map (int.cast_ring_hom K) P -- minimal polynomial of `μ` sent to `K[X]`
in calc
n.totient = (primitive_roots n K).card : h.card_primitive_roots.symm
... ≤ P_K.roots.to_finset.card : finset.card_le_of_subset (is_roots_of_minpoly h)
... ≤ P_K.roots.card : multiset.to_finset_card_le _
... ≤ P_K.nat_degree : card_roots' _
... ≤ P.nat_degree : nat_degree_map_le _ _
end is_domain
end comm_ring
end minpoly
section automorphisms
variables {S} [comm_ring S] [is_domain S] {μ : S} {n : ℕ+} (hμ : is_primitive_root μ n)
(R) [comm_ring R] [algebra R S]
/-- The `monoid_hom` that takes an automorphism to the power of μ that μ gets mapped to under it. -/
noncomputable def aut_to_pow : (S ≃ₐ[R] S) →* (zmod n)ˣ :=
let μ' := hμ.to_roots_of_unity in
have ho : order_of μ' = n :=
by rw [hμ.eq_order_of, ←hμ.coe_to_roots_of_unity_coe, order_of_units, order_of_subgroup],
monoid_hom.to_hom_units
{ to_fun := λ σ, (map_root_of_unity_eq_pow_self σ.to_alg_hom μ').some,
map_one' := begin
generalize_proofs h1,
have h := h1.some_spec,
dsimp only [alg_equiv.one_apply, alg_equiv.to_ring_equiv_eq_coe, ring_equiv.to_ring_hom_eq_coe,
ring_equiv.coe_to_ring_hom, alg_equiv.coe_ring_equiv] at *,
replace h : μ' = μ' ^ h1.some := roots_of_unity.coe_injective
(by simpa only [roots_of_unity.coe_pow] using h),
rw ←pow_one μ' at h {occs := occurrences.pos [1]},
rw [←@nat.cast_one $ zmod n, zmod.nat_coe_eq_nat_coe_iff, ←ho, ←pow_eq_pow_iff_modeq μ', h]
end,
map_mul' := begin
generalize_proofs hxy' hx' hy',
have hxy := hxy'.some_spec,
have hx := hx'.some_spec,
have hy := hy'.some_spec,
dsimp only [alg_equiv.to_ring_equiv_eq_coe, ring_equiv.to_ring_hom_eq_coe,
ring_equiv.coe_to_ring_hom, alg_equiv.coe_ring_equiv, alg_equiv.mul_apply] at *,
replace hxy : x (↑μ' ^ hy'.some) = ↑μ' ^ hxy'.some := hy ▸ hxy,
rw x.map_pow at hxy,
replace hxy : ((μ' : S) ^ hx'.some) ^ hy'.some = μ' ^ hxy'.some := hx ▸ hxy,
rw ←pow_mul at hxy,
replace hxy : μ' ^ (hx'.some * hy'.some) = μ' ^ hxy'.some := roots_of_unity.coe_injective
(by simpa only [roots_of_unity.coe_pow] using hxy),
rw [←nat.cast_mul, zmod.nat_coe_eq_nat_coe_iff, ←ho, ←pow_eq_pow_iff_modeq μ', hxy]
end }
-- We are not using @[simps] in aut_to_pow to avoid a timeout.
lemma coe_aut_to_pow_apply (f : S ≃ₐ[R] S) : (aut_to_pow R hμ f : zmod n) =
((map_root_of_unity_eq_pow_self f hμ.to_roots_of_unity).some : zmod n) := rfl
@[simp] lemma aut_to_pow_spec (f : S ≃ₐ[R] S) :
μ ^ (hμ.aut_to_pow R f : zmod n).val = f μ :=
begin
rw is_primitive_root.coe_aut_to_pow_apply,
generalize_proofs h,
have := h.some_spec,
dsimp only [alg_equiv.to_alg_hom_eq_coe, alg_equiv.coe_alg_hom] at this,
refine (_ : ↑hμ.to_roots_of_unity ^ _ = _).trans this.symm,
rw [←roots_of_unity.coe_pow, ←roots_of_unity.coe_pow],
congr' 1,
rw [pow_eq_pow_iff_modeq, ←order_of_subgroup, ←order_of_units, hμ.coe_to_roots_of_unity_coe,
←hμ.eq_order_of, zmod.val_nat_cast],
exact nat.mod_modeq _ _
end
end automorphisms
end is_primitive_root
|
d2e052419df8c9cc2c35059cae9a662e3dbb90a3 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/geometry/manifold/whitney_embedding.lean | 02240f02a1f1a90600c2f09190a325c7139d0ab6 | [
"Apache-2.0"
] | permissive | kbuzzard/mathlib | 2ff9e85dfe2a46f4b291927f983afec17e946eb8 | 58537299e922f9c77df76cb613910914a479c1f7 | refs/heads/master | 1,685,313,702,744 | 1,683,974,212,000 | 1,683,974,212,000 | 128,185,277 | 1 | 0 | null | 1,522,920,600,000 | 1,522,920,600,000 | null | UTF-8 | Lean | false | false | 6,409 | 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 geometry.manifold.diffeomorph
import geometry.manifold.instances.real
import geometry.manifold.partition_of_unity
/-!
# Whitney embedding theorem
In this file we prove a version of the Whitney embedding theorem: for any compact real manifold `M`,
for sufficiently large `n` there exists a smooth embedding `M → ℝ^n`.
## TODO
* Prove the weak Whitney embedding theorem: any `σ`-compact smooth `m`-dimensional manifold can be
embedded into `ℝ^(2m+1)`. This requires a version of Sard's theorem: for a locally Lipschitz
continuous map `f : ℝ^m → ℝ^n`, `m < n`, the range has Hausdorff dimension at most `m`, hence it
has measure zero.
## Tags
partition of unity, smooth bump function, whitney theorem
-/
universes uι uE uH uM
variables {ι : Type uι}
{E : Type uE} [normed_add_comm_group E] [normed_space ℝ E] [finite_dimensional ℝ E]
{H : Type uH} [topological_space H] {I : model_with_corners ℝ E H}
{M : Type uM} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M]
open function filter finite_dimensional set
open_locale topology manifold classical filter big_operators
noncomputable theory
namespace smooth_bump_covering
/-!
### Whitney embedding theorem
In this section we prove a version of the Whitney embedding theorem: for any compact real manifold
`M`, for sufficiently large `n` there exists a smooth embedding `M → ℝ^n`.
-/
variables [t2_space M] [hi : fintype ι] {s : set M} (f : smooth_bump_covering ι I M s)
include hi
/-- Smooth embedding of `M` into `(E × ℝ) ^ ι`. -/
def embedding_pi_tangent : C^∞⟮I, M; 𝓘(ℝ, ι → (E × ℝ)), ι → (E × ℝ)⟯ :=
{ to_fun := λ x i, (f i x • ext_chart_at I (f.c i) x, f i x),
cont_mdiff_to_fun := cont_mdiff_pi_space.2 $ λ i,
((f i).smooth_smul cont_mdiff_on_ext_chart_at).prod_mk_space ((f i).smooth) }
local attribute [simp] lemma embedding_pi_tangent_coe :
⇑f.embedding_pi_tangent = λ x i, (f i x • ext_chart_at I (f.c i) x, f i x) :=
rfl
lemma embedding_pi_tangent_inj_on : inj_on f.embedding_pi_tangent s :=
begin
intros x hx y hy h,
simp only [embedding_pi_tangent_coe, funext_iff] at h,
obtain ⟨h₁, h₂⟩ := prod.mk.inj_iff.1 (h (f.ind x hx)),
rw [f.apply_ind x hx] at h₂,
rw [← h₂, f.apply_ind x hx, one_smul, one_smul] at h₁,
have := f.mem_ext_chart_at_source_of_eq_one h₂.symm,
exact (ext_chart_at I (f.c _)).inj_on (f.mem_ext_chart_at_ind_source x hx) this h₁
end
lemma embedding_pi_tangent_injective (f : smooth_bump_covering ι I M) :
injective f.embedding_pi_tangent :=
injective_iff_inj_on_univ.2 f.embedding_pi_tangent_inj_on
lemma comp_embedding_pi_tangent_mfderiv (x : M) (hx : x ∈ s) :
((continuous_linear_map.fst ℝ E ℝ).comp
(@continuous_linear_map.proj ℝ _ ι (λ _, E × ℝ) _ _
(λ _, infer_instance) (f.ind x hx))).comp
(mfderiv I 𝓘(ℝ, ι → (E × ℝ)) f.embedding_pi_tangent x) =
mfderiv I I (chart_at H (f.c (f.ind x hx))) x :=
begin
set L := ((continuous_linear_map.fst ℝ E ℝ).comp
(@continuous_linear_map.proj ℝ _ ι (λ _, E × ℝ) _ _ (λ _, infer_instance) (f.ind x hx))),
have := L.has_mfderiv_at.comp x f.embedding_pi_tangent.mdifferentiable_at.has_mfderiv_at,
convert has_mfderiv_at_unique this _,
refine (has_mfderiv_at_ext_chart_at I (f.mem_chart_at_ind_source x hx)).congr_of_eventually_eq _,
refine (f.eventually_eq_one x hx).mono (λ y hy, _),
simp only [embedding_pi_tangent_coe, continuous_linear_map.coe_comp', (∘),
continuous_linear_map.coe_fst', continuous_linear_map.proj_apply],
rw [hy, pi.one_apply, one_smul]
end
lemma embedding_pi_tangent_ker_mfderiv (x : M) (hx : x ∈ s) :
linear_map.ker (mfderiv I 𝓘(ℝ, ι → (E × ℝ)) f.embedding_pi_tangent x) = ⊥ :=
begin
apply bot_unique,
rw [← (mdifferentiable_chart I (f.c (f.ind x hx))).ker_mfderiv_eq_bot
(f.mem_chart_at_ind_source x hx), ← comp_embedding_pi_tangent_mfderiv],
exact linear_map.ker_le_ker_comp _ _
end
lemma embedding_pi_tangent_injective_mfderiv (x : M) (hx : x ∈ s) :
injective (mfderiv I 𝓘(ℝ, ι → (E × ℝ)) f.embedding_pi_tangent x) :=
linear_map.ker_eq_bot.1 (f.embedding_pi_tangent_ker_mfderiv x hx)
omit hi
/-- Baby version of the **Whitney weak embedding theorem**: if `M` admits a finite covering by
supports of bump functions, then for some `n` it can be immersed into the `n`-dimensional
Euclidean space. -/
lemma exists_immersion_euclidean [finite ι] (f : smooth_bump_covering ι I M) :
∃ (n : ℕ) (e : M → euclidean_space ℝ (fin n)), smooth I (𝓡 n) e ∧
injective e ∧ ∀ x : M, injective (mfderiv I (𝓡 n) e x) :=
begin
casesI nonempty_fintype ι,
set F := euclidean_space ℝ (fin $ finrank ℝ (ι → (E × ℝ))),
letI : is_noetherian ℝ (E × ℝ) := is_noetherian.iff_fg.2 infer_instance,
letI : finite_dimensional ℝ (ι → E × ℝ) := is_noetherian.iff_fg.1 infer_instance,
set eEF : (ι → (E × ℝ)) ≃L[ℝ] F :=
continuous_linear_equiv.of_finrank_eq finrank_euclidean_space_fin.symm,
refine ⟨_, eEF ∘ f.embedding_pi_tangent,
eEF.to_diffeomorph.smooth.comp f.embedding_pi_tangent.smooth,
eEF.injective.comp f.embedding_pi_tangent_injective, λ x, _⟩,
rw [mfderiv_comp _ eEF.differentiable_at.mdifferentiable_at
f.embedding_pi_tangent.mdifferentiable_at, eEF.mfderiv_eq],
exact eEF.injective.comp (f.embedding_pi_tangent_injective_mfderiv _ trivial)
end
end smooth_bump_covering
/-- Baby version of the Whitney weak embedding theorem: if `M` admits a finite covering by
supports of bump functions, then for some `n` it can be embedded into the `n`-dimensional
Euclidean space. -/
lemma exists_embedding_euclidean_of_compact [t2_space M] [compact_space M] :
∃ (n : ℕ) (e : M → euclidean_space ℝ (fin n)), smooth I (𝓡 n) e ∧
closed_embedding e ∧ ∀ x : M, injective (mfderiv I (𝓡 n) e x) :=
begin
rcases smooth_bump_covering.exists_is_subordinate I is_closed_univ (λ (x : M) _, univ_mem)
with ⟨ι, f, -⟩,
haveI := f.fintype,
rcases f.exists_immersion_euclidean with ⟨n, e, hsmooth, hinj, hinj_mfderiv⟩,
exact ⟨n, e, hsmooth, hsmooth.continuous.closed_embedding hinj, hinj_mfderiv⟩
end
|
f15386964546deb3630d5a250a0cb518f999f4c6 | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/data/polynomial/coeff.lean | 606319803c46c4558fe175627ec44e679089f82c | [
"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 | 5,567 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker
-/
import data.polynomial.monomial
import data.finset.nat_antidiagonal
/-!
# Theory of univariate polynomials
The theorems include formulas for computing coefficients, such as
`coeff_add`, `coeff_sum`, `coeff_mul`
-/
noncomputable theory
open finsupp finset add_monoid_algebra
open_locale big_operators
namespace polynomial
universes u v
variables {R : Type u} {S : Type v} {a b : R} {n m : ℕ}
variables [semiring R] {p q r : polynomial R}
section coeff
lemma coeff_one (n : ℕ) : coeff (1 : polynomial R) n = if 0 = n then 1 else 0 :=
coeff_monomial
@[simp]
lemma coeff_add (p q : polynomial R) (n : ℕ) : coeff (p + q) n = coeff p n + coeff q n := rfl
lemma coeff_sum [semiring S] (n : ℕ) (f : ℕ → R → polynomial S) :
coeff (p.sum f) n = p.sum (λ a b, coeff (f a b) n) := finsupp.sum_apply
@[simp] lemma coeff_smul (p : polynomial R) (r : R) (n : ℕ) :
coeff (r • p) n = r * coeff p n := finsupp.smul_apply
variable (R)
/-- The nth coefficient, as a linear map. -/
def lcoeff (n : ℕ) : polynomial R →ₗ[R] R :=
{ to_fun := λ f, coeff f n,
map_add' := λ f g, coeff_add f g n,
map_smul' := λ r p, coeff_smul p r n }
variable {R}
@[simp] lemma lcoeff_apply (n : ℕ) (f : polynomial R) : lcoeff R n f = coeff f n := rfl
@[simp] lemma finset_sum_coeff {ι : Type*} (s : finset ι) (f : ι → polynomial R) (n : ℕ) :
coeff (∑ b in s, f b) n = ∑ b in s, coeff (f b) n :=
(s.sum_hom (λ q : polynomial R, lcoeff R n q)).symm
lemma coeff_mul (p q : polynomial R) (n : ℕ) :
coeff (p * q) n = ∑ x in nat.antidiagonal n, coeff p x.1 * coeff q x.2 :=
have hite : ∀ a : ℕ × ℕ, ite (a.1 + a.2 = n) (coeff p (a.fst) * coeff q (a.snd)) 0 ≠ 0
→ a.1 + a.2 = n, from λ a ha, by_contradiction
(λ h, absurd (eq.refl (0 : R)) (by rwa if_neg h at ha)),
calc coeff (p * q) n = ∑ a in p.support, ∑ b in q.support,
ite (a + b = n) (coeff p a * coeff q b) 0 :
by { simp only [mul_def, coeff_sum, coeff_single], refl }
... = ∑ v in p.support.product q.support, ite (v.1 + v.2 = n) (coeff p v.1 * coeff q v.2) 0 :
by rw sum_product
... = ∑ x in nat.antidiagonal n, coeff p x.1 * coeff q x.2 :
begin
refine sum_bij_ne_zero (λ x _ _, x)
(λ x _ hx, nat.mem_antidiagonal.2 (hite x hx)) (λ _ _ _ _ _ _ h, h)
(λ x h₁ h₂, ⟨x, _, _, rfl⟩) _,
{ rw [mem_product, mem_support_iff, mem_support_iff],
exact ne_zero_and_ne_zero_of_mul h₂ },
{ rw nat.mem_antidiagonal at h₁, rwa [if_pos h₁] },
{ intros x h hx, rw [if_pos (hite x hx)] }
end
@[simp] lemma mul_coeff_zero (p q : polynomial R) : coeff (p * q) 0 = coeff p 0 * coeff q 0 :=
by simp [coeff_mul]
lemma coeff_mul_X_zero (p : polynomial R) : coeff (p * X) 0 = 0 :=
by simp
lemma coeff_X_mul_zero (p : polynomial R) : coeff (X * p) 0 = 0 :=
by simp
lemma coeff_C_mul_X (x : R) (k n : ℕ) :
coeff (C x * X^k : polynomial R) n = if n = k then x else 0 :=
by rw [← single_eq_C_mul_X]; simp [monomial, single, eq_comm, coeff]; congr
@[simp] lemma coeff_C_mul (p : polynomial R) : coeff (C a * p) n = a * coeff p n :=
begin
conv in (a * _) { rw [← @sum_single _ _ _ p, coeff_sum] },
rw [mul_def, ←monomial_zero_left, monomial, sum_single_index],
{ simp only [coeff_single, finsupp.mul_sum, coeff_sum],
apply sum_congr rfl,
assume i hi, by_cases i = n; simp [h] },
{ simp [finsupp.sum] }
end
lemma C_mul' (a : R) (f : polynomial R) : C a * f = a • f :=
ext $ λ n, coeff_C_mul f
@[simp] lemma coeff_mul_C (p : polynomial R) (n : ℕ) (a : R) :
coeff (p * C a) n = coeff p n * a :=
begin
conv_rhs { rw [← @finsupp.sum_single _ _ _ p, coeff_sum] },
rw [mul_def, ←monomial_zero_left], simp_rw [sum_single_index],
{ simp only [coeff_single, finsupp.sum_mul, coeff_sum],
apply sum_congr rfl,
assume i hi, by_cases i = n; simp [h], },
end
lemma monomial_one_eq_X_pow : ∀{n}, monomial n (1 : R) = X^n
| 0 := rfl
| (n+1) :=
calc monomial (n + 1) (1 : R) = monomial n 1 * X : by rw [X, monomial_mul_monomial, mul_one]
... = X^n * X : by rw [monomial_one_eq_X_pow]
... = X^(n+1) : by simp only [pow_add, pow_one]
lemma monomial_eq_smul_X {n} : monomial n (a : R) = a • X^n :=
begin
calc monomial n a = monomial n (a * 1) : by simp
... = a • monomial n 1 : (smul_single' _ _ _).symm
... = a • X^n : by rw monomial_one_eq_X_pow
end
lemma coeff_X_pow (k n : ℕ) :
coeff (X^k : polynomial R) n = if n = k then 1 else 0 :=
by rw [← monomial_one_eq_X_pow]; simp [monomial, single, eq_comm, coeff]; congr
@[simp]
lemma coeff_X_pow_self (n : ℕ) :
coeff (X^n : polynomial R) n = 1 :=
by simp [coeff_X_pow]
theorem coeff_mul_X_pow (p : polynomial R) (n d : ℕ) :
coeff (p * polynomial.X ^ n) (d + n) = coeff p d :=
begin
rw [coeff_mul, sum_eq_single (d,n), coeff_X_pow, if_pos rfl, mul_one],
{ rintros ⟨i,j⟩ h1 h2, rw [coeff_X_pow, if_neg, mul_zero], rintro rfl, apply h2,
rw [nat.mem_antidiagonal, add_right_cancel_iff] at h1, subst h1 },
{ exact λ h1, (h1 (nat.mem_antidiagonal.2 rfl)).elim }
end
@[simp] theorem coeff_mul_X (p : polynomial R) (n : ℕ) :
coeff (p * X) (n + 1) = coeff p n :=
by simpa only [pow_one] using coeff_mul_X_pow p 1 n
theorem mul_X_pow_eq_zero {p : polynomial R} {n : ℕ}
(H : p * X ^ n = 0) : p = 0 :=
ext $ λ k, (coeff_mul_X_pow p n k).symm.trans $ ext_iff.1 H (k+n)
end coeff
end polynomial
|
066d8233d8e8717b4f6f806f703fabfa2cad0fca | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /doc/examples/Certora2022/ex17.lean | c64cc9bb9767e7afbe708aea8abea60eb50bb152 | [
"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 | 356 | lean | /- More tactics -/
example (p q : Nat → Prop) : (∃ x, p x ∧ q x) → ∃ x, q x ∧ p x := by
intro h
cases h with
| intro x hpq =>
cases hpq with
| intro hp hq =>
exists x
example : p ∧ q → q ∧ p := by
intro p
cases p
constructor <;> assumption
example : p ∧ ¬ p → q := by
intro h
cases h
contradiction
|
1246dcb62abbf0ea7ca6e12dce14053a28cb3f94 | 5719a16e23dfc08cdea7a5bf035b81690f307965 | /src/Init/Lean/Elab/DeclModifiers.lean | 6f2c8a021c1837e73cd17449b8aff342e96d1d71 | [
"Apache-2.0"
] | permissive | postmasters/lean4 | 488b03969a371e1507e1e8a4df9ebf63c7cbe7ac | f3976fc53a883ac7606fc59357d43f4b51016ca7 | refs/heads/master | 1,655,582,707,480 | 1,588,682,595,000 | 1,588,682,595,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,284 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Sebastian Ullrich
-/
prelude
import Init.Lean.Elab.Command
namespace Lean
namespace Elab
namespace Command
structure Attribute :=
(name : Name) (args : Syntax := Syntax.missing)
instance Attribute.hasFormat : HasFormat Attribute :=
⟨fun attr => Format.bracket "@[" (toString attr.name ++ (if attr.args.isMissing then "" else toString attr.args)) "]"⟩
inductive Visibility
| regular | «protected» | «private»
instance Visibility.hasToString : HasToString Visibility :=
⟨fun v => match v with
| Visibility.regular => "regular"
| Visibility.private => "private"
| Visibility.protected => "protected"⟩
structure Modifiers :=
(docString : Option String := none)
(visibility : Visibility := Visibility.regular)
(isNoncomputable : Bool := false)
(isPartial : Bool := false)
(isUnsafe : Bool := false)
(attrs : Array Attribute := #[])
def Modifiers.addAttribute (modifiers : Modifiers) (attr : Attribute) : Modifiers :=
{ attrs := modifiers.attrs.push attr, .. modifiers }
instance Modifiers.hasFormat : HasFormat Modifiers :=
⟨fun m =>
let components : List Format :=
(match m.docString with
| some str => ["/--" ++ str ++ "-/"]
| none => [])
++ (match m.visibility with
| Visibility.regular => []
| Visibility.protected => ["protected"]
| Visibility.private => ["private"])
++ (if m.isNoncomputable then ["noncomputable"] else [])
++ (if m.isPartial then ["partial"] else [])
++ (if m.isUnsafe then ["unsafe"] else [])
++ m.attrs.toList.map (fun attr => fmt attr);
Format.bracket "{" (Format.joinSep components ("," ++ Format.line)) "}"⟩
instance Modifiers.hasToString : HasToString Modifiers := ⟨toString ∘ format⟩
def elabAttr (stx : Syntax) : CommandElabM Attribute := do
-- rawIdent >> many attrArg
let nameStx := stx.getArg 0;
attrName ← match nameStx.isIdOrAtom? with
| none => throwError nameStx "identifier expected"
| some str => pure $ mkNameSimple str;
env ← getEnv;
unless (isAttribute env attrName) $
throwError stx ("unknown attribute [" ++ attrName ++ "]");
let args := stx.getArg 1;
pure { name := attrName, args := args }
def elabAttrs (stx : Syntax) : CommandElabM (Array Attribute) :=
(stx.getArg 1).foldSepArgsM
(fun stx attrs => do
attr ← elabAttr stx;
pure $ attrs.push attr)
#[]
def elabModifiers (stx : Syntax) : CommandElabM Modifiers := do
let docCommentStx := stx.getArg 0;
let attrsStx := stx.getArg 1;
let visibilityStx := stx.getArg 2;
let noncompStx := stx.getArg 3;
let unsafeStx := stx.getArg 4;
let partialStx := stx.getArg 5;
docString ← match docCommentStx.getOptional? with
| none => pure none
| some s => match s.getArg 1 with
| Syntax.atom _ val => pure (some (val.extract 0 (val.bsize - 2)))
| _ => throwError s ("unexpected doc string " ++ toString (s.getArg 1));
visibility ← match visibilityStx.getOptional? with
| none => pure Visibility.regular
| some v =>
let kind := v.getKind;
if kind == `Lean.Parser.Command.private then pure Visibility.private
else if kind == `Lean.Parser.Command.protected then pure Visibility.protected
else throwError v "unexpected visibility modifier";
attrs ← match attrsStx.getOptional? with
| none => pure #[]
| some attrs => elabAttrs attrs;
pure {
docString := docString,
visibility := visibility,
isPartial := !partialStx.isNone,
isUnsafe := !unsafeStx.isNone,
isNoncomputable := !noncompStx.isNone,
attrs := attrs
}
def mkDeclName (modifiers : Modifiers) (atomicName : Name) : CommandElabM Name := do
currNamespace ← getCurrNamespace;
let declName := currNamespace ++ atomicName;
match modifiers.visibility with
| Visibility.private => do
env ← getEnv;
pure $ mkPrivateName env declName
| _ => pure declName
def checkNotAlreadyDeclared (ref : Syntax) (declName : Name) : CommandElabM Unit := do
env ← getEnv;
when (env.contains declName) $
match privateToUserName? declName with
| none => throwError ref ("'" ++ declName ++ "' has already been declared")
| some declName => throwError ref ("private declaration '" ++ declName ++ "' has already been declared");
when (env.contains (mkPrivateName env declName)) $
throwError ref ("a private declaration '" ++ declName ++ "' has already been declared");
match privateToUserName? declName with
| none => pure ()
| some declName =>
when (env.contains declName) $
throwError ref ("a non-private declaration '" ++ declName ++ "' has already been declared")
def applyAttributes (ref : Syntax) (declName : Name) (attrs : Array Attribute) (applicationTime : AttributeApplicationTime) : CommandElabM Unit :=
attrs.forM $ fun attr => do
env ← getEnv;
match getAttributeImpl env attr.name with
| Except.error errMsg => throwError ref errMsg
| Except.ok attrImpl =>
when (attrImpl.applicationTime == applicationTime) $ do
env ← getEnv;
env ← liftIO ref $ attrImpl.add env declName attr.args true;
setEnv env
end Command
end Elab
end Lean
|
14c7ee3812fcf8b04e2b2788859a2524b9bc0cac | 5d62e434e81e3303af5bef665e46bef75f10b45e | /current_mathlib/src/poly_eval.lean | 96db8b5b5631cb4a550beba6d41d4dda709bd307 | [] | no_license | ChrisHughes24/type_class | 4b49764e9b96b2c7d2b6e4e7827730ca7a64a3b2 | b582b60bdc84b7cd17b4243600088143afddc65c | refs/heads/master | 1,595,404,449,845 | 1,567,951,656,000 | 1,567,951,656,000 | 207,121,926 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 437 | lean | import data.polynomial
universe u
variables {K : Type u} {L : Type u} [discrete_field K] [discrete_field L]
(i : K ≃r L)
open polynomial
-- polynomial.map is very hard to use
example (f : polynomial L) (x : K) (hfx : f.eval₂ i.symm.to_equiv x = 0) :
∃ y : L, f.eval y = 0 :=
⟨i.to_equiv x, begin
rw [← i.symm.to_equiv.injective.eq_iff],
-- cannot find the pattern, but it is there
rw [← eval₂_hom],
end⟩
|
4ff8af3518fa27437ea2c789f8169495f45f17ad | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/algebra/direct_sum/basic.lean | da4963a7dee91bea6433e9cf3eeabbeddd9fe411 | [
"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 | 9,060 | lean | /-
Copyright (c) 2019 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import data.dfinsupp
import group_theory.submonoid.operations
import group_theory.subgroup.basic
/-!
# Direct sum
This file defines the direct sum of abelian groups, indexed by a discrete type.
## Notation
`⨁ i, β i` is the n-ary direct sum `direct_sum`.
This notation is in the `direct_sum` locale, accessible after `open_locale direct_sum`.
## References
* https://en.wikipedia.org/wiki/Direct_sum
-/
open_locale big_operators
universes u v w u₁
variables (ι : Type v) [dec_ι : decidable_eq ι] (β : ι → Type w)
/-- `direct_sum β` is the direct sum of a family of additive commutative monoids `β i`.
Note: `open_locale direct_sum` will enable the notation `⨁ i, β i` for `direct_sum β`. -/
@[derive [add_comm_monoid, inhabited]]
def direct_sum [Π i, add_comm_monoid (β i)] : Type* := Π₀ i, β i
instance [Π i, add_comm_monoid (β i)] : has_coe_to_fun (direct_sum ι β) (λ _, Π i : ι, β i) :=
dfinsupp.has_coe_to_fun
localized "notation `⨁` binders `, ` r:(scoped f, direct_sum _ f) := r" in direct_sum
namespace direct_sum
variables {ι}
section add_comm_group
variables [Π i, add_comm_group (β i)]
instance : add_comm_group (direct_sum ι β) := dfinsupp.add_comm_group
variables {β}
@[simp] lemma sub_apply (g₁ g₂ : ⨁ i, β i) (i : ι) : (g₁ - g₂) i = g₁ i - g₂ i :=
dfinsupp.sub_apply _ _ _
end add_comm_group
variables [Π i, add_comm_monoid (β i)]
@[simp] lemma zero_apply (i : ι) : (0 : ⨁ i, β i) i = 0 := rfl
variables {β}
@[simp] lemma add_apply (g₁ g₂ : ⨁ i, β i) (i : ι) : (g₁ + g₂) i = g₁ i + g₂ i :=
dfinsupp.add_apply _ _ _
variables (β)
include dec_ι
/-- `mk β s x` is the element of `⨁ i, β i` that is zero outside `s`
and has coefficient `x i` for `i` in `s`. -/
def mk (s : finset ι) : (Π i : (↑s : set ι), β i.1) →+ ⨁ i, β i :=
{ to_fun := dfinsupp.mk s,
map_add' := λ _ _, dfinsupp.mk_add,
map_zero' := dfinsupp.mk_zero, }
/-- `of i` is the natural inclusion map from `β i` to `⨁ i, β i`. -/
def of (i : ι) : β i →+ ⨁ i, β i :=
dfinsupp.single_add_hom β i
@[simp] lemma of_eq_same (i : ι) (x : β i) : (of _ i x) i = x :=
dfinsupp.single_eq_same
lemma of_eq_of_ne (i j : ι) (x : β i) (h : i ≠ j) : (of _ i x) j = 0 :=
dfinsupp.single_eq_of_ne h
@[simp] lemma support_zero [Π (i : ι) (x : β i), decidable (x ≠ 0)] :
(0 : ⨁ i, β i).support = ∅ := dfinsupp.support_zero
@[simp] lemma support_of [Π (i : ι) (x : β i), decidable (x ≠ 0)]
(i : ι) (x : β i) (h : x ≠ 0) :
(of _ i x).support = {i} := dfinsupp.support_single_ne_zero h
lemma support_of_subset [Π (i : ι) (x : β i), decidable (x ≠ 0)] {i : ι} {b : β i} :
(of _ i b).support ⊆ {i} := dfinsupp.support_single_subset
lemma sum_support_of [Π (i : ι) (x : β i), decidable (x ≠ 0)] (x : ⨁ i, β i) :
∑ i in x.support, of β i (x i) = x := dfinsupp.sum_single
variables {β}
theorem mk_injective (s : finset ι) : function.injective (mk β s) :=
dfinsupp.mk_injective s
theorem of_injective (i : ι) : function.injective (of β i) :=
dfinsupp.single_injective
@[elab_as_eliminator]
protected theorem induction_on {C : (⨁ i, β i) → Prop}
(x : ⨁ i, β i) (H_zero : C 0)
(H_basic : ∀ (i : ι) (x : β i), C (of β i x))
(H_plus : ∀ x y, C x → C y → C (x + y)) : C x :=
begin
apply dfinsupp.induction x H_zero,
intros i b f h1 h2 ih,
solve_by_elim
end
/-- If two additive homomorphisms from `⨁ i, β i` are equal on each `of β i y`,
then they are equal. -/
lemma add_hom_ext {γ : Type*} [add_monoid γ] ⦃f g : (⨁ i, β i) →+ γ⦄
(H : ∀ (i : ι) (y : β i), f (of _ i y) = g (of _ i y)) : f = g :=
dfinsupp.add_hom_ext H
/-- If two additive homomorphisms from `⨁ i, β i` are equal on each `of β i y`,
then they are equal.
See note [partially-applied ext lemmas]. -/
@[ext] lemma add_hom_ext' {γ : Type*} [add_monoid γ] ⦃f g : (⨁ i, β i) →+ γ⦄
(H : ∀ (i : ι), f.comp (of _ i) = g.comp (of _ i)) : f = g :=
add_hom_ext $ λ i, add_monoid_hom.congr_fun $ H i
variables {γ : Type u₁} [add_comm_monoid γ]
section to_add_monoid
variables (φ : Π i, β i →+ γ) (ψ : (⨁ i, β i) →+ γ)
/-- `to_add_monoid φ` is the natural homomorphism from `⨁ i, β i` to `γ`
induced by a family `φ` of homomorphisms `β i → γ`. -/
def to_add_monoid : (⨁ i, β i) →+ γ :=
(dfinsupp.lift_add_hom φ)
@[simp] lemma to_add_monoid_of (i) (x : β i) : to_add_monoid φ (of β i x) = φ i x :=
dfinsupp.lift_add_hom_apply_single φ i x
theorem to_add_monoid.unique (f : ⨁ i, β i) :
ψ f = to_add_monoid (λ i, ψ.comp (of β i)) f :=
by {congr, ext, simp [to_add_monoid, of]}
end to_add_monoid
section from_add_monoid
/-- `from_add_monoid φ` is the natural homomorphism from `γ` to `⨁ i, β i`
induced by a family `φ` of homomorphisms `γ → β i`.
Note that this is not an isomorphism. Not every homomorphism `γ →+ ⨁ i, β i` arises in this way. -/
def from_add_monoid : (⨁ i, γ →+ β i) →+ (γ →+ ⨁ i, β i) :=
to_add_monoid $ λ i, add_monoid_hom.comp_hom (of β i)
@[simp] lemma from_add_monoid_of (i : ι) (f : γ →+ β i) :
from_add_monoid (of _ i f) = (of _ i).comp f :=
by { rw [from_add_monoid, to_add_monoid_of], refl }
lemma from_add_monoid_of_apply (i : ι) (f : γ →+ β i) (x : γ) :
from_add_monoid (of _ i f) x = of _ i (f x) :=
by rw [from_add_monoid_of, add_monoid_hom.coe_comp]
end from_add_monoid
variables (β)
/-- `set_to_set β S T h` is the natural homomorphism `⨁ (i : S), β i → ⨁ (i : T), β i`,
where `h : S ⊆ T`. -/
-- TODO: generalize this to remove the assumption `S ⊆ T`.
def set_to_set (S T : set ι) (H : S ⊆ T) :
(⨁ (i : S), β i) →+ (⨁ (i : T), β i) :=
to_add_monoid $ λ i, of (λ (i : subtype T), β i) ⟨↑i, H i.prop⟩
variables {β}
omit dec_ι
/-- The natural equivalence between `⨁ _ : ι, M` and `M` when `unique ι`. -/
protected def id (M : Type v) (ι : Type* := punit) [add_comm_monoid M] [unique ι] :
(⨁ (_ : ι), M) ≃+ M :=
{ to_fun := direct_sum.to_add_monoid (λ _, add_monoid_hom.id M),
inv_fun := of (λ _, M) (default ι),
left_inv := λ x, direct_sum.induction_on x
(by rw [add_monoid_hom.map_zero, add_monoid_hom.map_zero])
(λ p x, by rw [unique.default_eq p, to_add_monoid_of]; refl)
(λ x y ihx ihy, by rw [add_monoid_hom.map_add, add_monoid_hom.map_add, ihx, ihy]),
right_inv := λ x, to_add_monoid_of _ _ _,
..direct_sum.to_add_monoid (λ _, add_monoid_hom.id M) }
/-- The canonical embedding from `⨁ i, A i` to `M` where `A` is a collection of `add_submonoid M`
indexed by `ι`-/
def add_submonoid_coe {M : Type*} [decidable_eq ι] [add_comm_monoid M]
(A : ι → add_submonoid M) : (⨁ i, A i) →+ M :=
to_add_monoid (λ i, (A i).subtype)
@[simp] lemma add_submonoid_coe_of {M : Type*} [decidable_eq ι] [add_comm_monoid M]
(A : ι → add_submonoid M) (i : ι) (x : A i) :
add_submonoid_coe A (of (λ i, A i) i x) = x :=
to_add_monoid_of _ _ _
/-- The `direct_sum` formed by a collection of `add_submonoid`s of `M` is said to be internal if the
canonical map `(⨁ i, A i) →+ M` is bijective.
See `direct_sum.add_subgroup_is_internal` for the same statement about `add_subgroup`s. -/
def add_submonoid_is_internal {M : Type*} [decidable_eq ι] [add_comm_monoid M]
(A : ι → add_submonoid M) : Prop :=
function.bijective (add_submonoid_coe A)
lemma add_submonoid_is_internal.supr_eq_top {M : Type*} [decidable_eq ι] [add_comm_monoid M]
(A : ι → add_submonoid M)
(h : add_submonoid_is_internal A) : supr A = ⊤ :=
begin
rw [add_submonoid.supr_eq_mrange_dfinsupp_sum_add_hom, add_monoid_hom.mrange_top_iff_surjective],
exact function.bijective.surjective h,
end
/-- The canonical embedding from `⨁ i, A i` to `M` where `A` is a collection of `add_subgroup M`
indexed by `ι`-/
def add_subgroup_coe {M : Type*} [decidable_eq ι] [add_comm_group M]
(A : ι → add_subgroup M) : (⨁ i, A i) →+ M :=
to_add_monoid (λ i, (A i).subtype)
@[simp] lemma add_subgroup_coe_of {M : Type*} [decidable_eq ι] [add_comm_group M]
(A : ι → add_subgroup M) (i : ι) (x : A i) :
add_subgroup_coe A (of (λ i, A i) i x) = x :=
to_add_monoid_of _ _ _
/-- The `direct_sum` formed by a collection of `add_subgroup`s of `M` is said to be internal if the
canonical map `(⨁ i, A i) →+ M` is bijective.
See `direct_sum.submodule_is_internal` for the same statement about `submodules`s. -/
def add_subgroup_is_internal {M : Type*} [decidable_eq ι] [add_comm_group M]
(A : ι → add_subgroup M) : Prop :=
function.bijective (add_subgroup_coe A)
lemma add_subgroup_is_internal.to_add_submonoid
{M : Type*} [decidable_eq ι] [add_comm_group M] (A : ι → add_subgroup M) :
add_subgroup_is_internal A ↔
add_submonoid_is_internal (λ i, (A i).to_add_submonoid) :=
iff.rfl
end direct_sum
|
61e6bca7d7e549599d6121d443477f74284aed3a | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/number_theory/fermat4.lean | 470b015987202b6e71f7bf959042b1a37d1c309d | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,109 | lean | /-
Copyright (c) 2020 Paul van Wamelen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Paul van Wamelen.
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.number_theory.pythagorean_triples
import Mathlib.ring_theory.coprime
import Mathlib.PostPort
namespace Mathlib
/-!
# Fermat's Last Theorem for the case n = 4
There are no non-zero integers `a`, `b` and `c` such that `a ^ 4 + b ^ 4 = c ^ 4`.
-/
/-- Shorthand for three non-zero integers `a`, `b`, and `c` satisfying `a ^ 4 + b ^ 4 = c ^ 2`.
We will show that no integers satisfy this equation. Clearly Fermat's Last theorem for n = 4
follows. -/
def fermat_42 (a : ℤ) (b : ℤ) (c : ℤ) :=
a ≠ 0 ∧ b ≠ 0 ∧ a ^ bit0 (bit0 1) + b ^ bit0 (bit0 1) = c ^ bit0 1
namespace fermat_42
theorem comm {a : ℤ} {b : ℤ} {c : ℤ} : fermat_42 a b c ↔ fermat_42 b a c := sorry
theorem mul {a : ℤ} {b : ℤ} {c : ℤ} {k : ℤ} (hk0 : k ≠ 0) : fermat_42 a b c ↔ fermat_42 (k * a) (k * b) (k ^ bit0 1 * c) := sorry
theorem ne_zero {a : ℤ} {b : ℤ} {c : ℤ} (h : fermat_42 a b c) : c ≠ 0 := sorry
/-- We say a solution to `a ^ 4 + b ^ 4 = c ^ 2` is minimal if there is no other solution with
a smaller `c` (in absolute value). -/
def minimal (a : ℤ) (b : ℤ) (c : ℤ) :=
fermat_42 a b c ∧ ∀ (a1 b1 c1 : ℤ), fermat_42 a1 b1 c1 → int.nat_abs c ≤ int.nat_abs c1
/-- if we have a solution to `a ^ 4 + b ^ 4 = c ^ 2` then there must be a minimal one. -/
theorem exists_minimal {a : ℤ} {b : ℤ} {c : ℤ} (h : fermat_42 a b c) : ∃ (a0 : ℤ), ∃ (b0 : ℤ), ∃ (c0 : ℤ), minimal a0 b0 c0 := sorry
/-- a minimal solution to `a ^ 4 + b ^ 4 = c ^ 2` must have `a` and `b` coprime. -/
theorem coprime_of_minimal {a : ℤ} {b : ℤ} {c : ℤ} (h : minimal a b c) : is_coprime a b := sorry
/-- We can swap `a` and `b` in a minimal solution to `a ^ 4 + b ^ 4 = c ^ 2`. -/
theorem minimal_comm {a : ℤ} {b : ℤ} {c : ℤ} : minimal a b c → minimal b a c := sorry
/-- We can assume that a minimal solution to `a ^ 4 + b ^ 4 = c ^ 2` has positive `c`. -/
theorem neg_of_minimal {a : ℤ} {b : ℤ} {c : ℤ} : minimal a b c → minimal a b (-c) := sorry
/-- We can assume that a minimal solution to `a ^ 4 + b ^ 4 = c ^ 2` has `a` odd. -/
theorem exists_odd_minimal {a : ℤ} {b : ℤ} {c : ℤ} (h : fermat_42 a b c) : ∃ (a0 : ℤ), ∃ (b0 : ℤ), ∃ (c0 : ℤ), minimal a0 b0 c0 ∧ a0 % bit0 1 = 1 := sorry
/-- We can assume that a minimal solution to `a ^ 4 + b ^ 4 = c ^ 2` has
`a` odd and `c` positive. -/
theorem exists_pos_odd_minimal {a : ℤ} {b : ℤ} {c : ℤ} (h : fermat_42 a b c) : ∃ (a0 : ℤ), ∃ (b0 : ℤ), ∃ (c0 : ℤ), minimal a0 b0 c0 ∧ a0 % bit0 1 = 1 ∧ 0 < c0 := sorry
end fermat_42
theorem int.coprime_of_sqr_sum {r : ℤ} {s : ℤ} (h2 : is_coprime s r) : is_coprime (r ^ bit0 1 + s ^ bit0 1) r :=
eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime (r ^ bit0 1 + s ^ bit0 1) r)) (pow_two r)))
(eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime (r * r + s ^ bit0 1) r)) (pow_two s)))
(is_coprime.mul_add_left_left (is_coprime.mul_left h2 h2) r))
theorem int.coprime_of_sqr_sum' {r : ℤ} {s : ℤ} (h : is_coprime r s) : is_coprime (r ^ bit0 1 + s ^ bit0 1) (r * s) :=
is_coprime.mul_right (int.coprime_of_sqr_sum (iff.mp is_coprime_comm h))
(eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime (r ^ bit0 1 + s ^ bit0 1) s)) (add_comm (r ^ bit0 1) (s ^ bit0 1))))
(int.coprime_of_sqr_sum h))
namespace fermat_42
-- If we have a solution to a ^ 4 + b ^ 4 = c ^ 2, we can construct a smaller one. This
-- implies there can't be a smallest solution.
theorem not_minimal {a : ℤ} {b : ℤ} {c : ℤ} (h : minimal a b c) (ha2 : a % bit0 1 = 1) (hc : 0 < c) : False := sorry
end fermat_42
theorem not_fermat_42 {a : ℤ} {b : ℤ} {c : ℤ} (ha : a ≠ 0) (hb : b ≠ 0) : a ^ bit0 (bit0 1) + b ^ bit0 (bit0 1) ≠ c ^ bit0 1 := sorry
theorem not_fermat_4 {a : ℤ} {b : ℤ} {c : ℤ} (ha : a ≠ 0) (hb : b ≠ 0) : a ^ bit0 (bit0 1) + b ^ bit0 (bit0 1) ≠ c ^ bit0 (bit0 1) := sorry
|
931944228419b881b8439eda92a749a7823285fa | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/algebra/category/Group/basic.lean | 5c0c6a654fccc9f917e81a3da03607dc26ed9403 | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 9,383 | lean | /-
Copyright (c) 2018 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import algebra.category.Mon.basic
import category_theory.endomorphism
/-!
# Category instances for group, add_group, comm_group, and add_comm_group.
We introduce the bundled categories:
* `Group`
* `AddGroup`
* `CommGroup`
* `AddCommGroup`
along with the relevant forgetful functors between them, and to the bundled monoid categories.
-/
universes u v
open category_theory
/-- The category of groups and group morphisms. -/
@[to_additive AddGroup]
def Group : Type (u+1) := bundled group
/-- The category of additive groups and group morphisms -/
add_decl_doc AddGroup
namespace Group
@[to_additive]
instance : bundled_hom.parent_projection group.to_monoid := ⟨⟩
attribute [derive [has_coe_to_sort, large_category, concrete_category]] Group
attribute [to_additive] Group.has_coe_to_sort Group.large_category Group.concrete_category
/-- Construct a bundled `Group` from the underlying type and typeclass. -/
@[to_additive] def of (X : Type u) [group X] : Group := bundled.of X
/-- Construct a bundled `AddGroup` from the underlying type and typeclass. -/
add_decl_doc AddGroup.of
@[to_additive]
instance (G : Group) : group G := G.str
@[simp, to_additive] lemma coe_of (R : Type u) [group R] : (Group.of R : Type u) = R := rfl
@[to_additive]
instance : has_one Group := ⟨Group.of punit⟩
@[to_additive]
instance : inhabited Group := ⟨1⟩
@[to_additive]
instance one.unique : unique (1 : Group) :=
{ default := 1,
uniq := λ a, begin cases a, refl, end }
@[simp, to_additive]
lemma one_apply (G H : Group) (g : G) : (1 : G ⟶ H) g = 1 := rfl
@[ext, to_additive]
lemma ext (G H : Group) (f₁ f₂ : G ⟶ H) (w : ∀ x, f₁ x = f₂ x) : f₁ = f₂ :=
by { ext1, apply w }
-- should to_additive do this automatically?
attribute [ext] AddGroup.ext
@[to_additive has_forget_to_AddMon]
instance has_forget_to_Mon : has_forget₂ Group Mon := bundled_hom.forget₂ _ _
end Group
/-- The category of commutative groups and group morphisms. -/
@[to_additive AddCommGroup]
def CommGroup : Type (u+1) := bundled comm_group
/-- The category of additive commutative groups and group morphisms. -/
add_decl_doc AddCommGroup
/-- `Ab` is an abbreviation for `AddCommGroup`, for the sake of mathematicians' sanity. -/
abbreviation Ab := AddCommGroup
namespace CommGroup
@[to_additive]
instance : bundled_hom.parent_projection comm_group.to_group := ⟨⟩
attribute [derive [has_coe_to_sort, large_category, concrete_category]] CommGroup
attribute [to_additive] CommGroup.has_coe_to_sort CommGroup.large_category
CommGroup.concrete_category
/-- Construct a bundled `CommGroup` from the underlying type and typeclass. -/
@[to_additive] def of (G : Type u) [comm_group G] : CommGroup := bundled.of G
/-- Construct a bundled `AddCommGroup` from the underlying type and typeclass. -/
add_decl_doc AddCommGroup.of
@[to_additive]
instance comm_group_instance (G : CommGroup) : comm_group G := G.str
@[simp, to_additive] lemma coe_of (R : Type u) [comm_group R] : (CommGroup.of R : Type u) = R := rfl
@[to_additive] instance : has_one CommGroup := ⟨CommGroup.of punit⟩
@[to_additive] instance : inhabited CommGroup := ⟨1⟩
@[to_additive]
instance one.unique : unique (1 : CommGroup) :=
{ default := 1,
uniq := λ a, begin cases a, refl, end }
@[simp, to_additive]
lemma one_apply (G H : CommGroup) (g : G) : (1 : G ⟶ H) g = 1 := rfl
@[to_additive,ext]
lemma ext (G H : CommGroup) (f₁ f₂ : G ⟶ H) (w : ∀ x, f₁ x = f₂ x) : f₁ = f₂ :=
by { ext1, apply w }
attribute [ext] AddCommGroup.ext
@[to_additive has_forget_to_AddGroup]
instance has_forget_to_Group : has_forget₂ CommGroup Group := bundled_hom.forget₂ _ _
@[to_additive has_forget_to_AddCommMon]
instance has_forget_to_CommMon : has_forget₂ CommGroup CommMon :=
induced_category.has_forget₂ (λ G : CommGroup, CommMon.of G)
end CommGroup
-- This example verifies an improvement possible in Lean 3.8.
-- Before that, to have `monoid_hom.map_map` usable by `simp` here,
-- we had to mark all the concrete category `has_coe_to_sort` instances reducible.
-- Now, it just works.
@[to_additive]
example {R S : CommGroup} (i : R ⟶ S) (r : R) (h : r = 1) : i r = 1 :=
by simp [h]
namespace AddCommGroup
/-- Any element of an abelian group gives a unique morphism from `ℤ` sending
`1` to that element. -/
-- Note that because `ℤ : Type 0`, this forces `G : AddCommGroup.{0}`,
-- so we write this explicitly to be clear.
-- TODO generalize this, requiring a `ulift_instances.lean` file
def as_hom {G : AddCommGroup.{0}} (g : G) : (AddCommGroup.of ℤ) ⟶ G :=
gmultiples_hom G g
@[simp]
lemma as_hom_apply {G : AddCommGroup.{0}} (g : G) (i : ℤ) : (as_hom g) i = i • g := rfl
lemma as_hom_injective {G : AddCommGroup.{0}} : function.injective (@as_hom G) :=
λ h k w, by convert congr_arg (λ k : (AddCommGroup.of ℤ) ⟶ G, (k : ℤ → G) (1 : ℤ)) w; simp
@[ext]
lemma int_hom_ext
{G : AddCommGroup.{0}} (f g : (AddCommGroup.of ℤ) ⟶ G) (w : f (1 : ℤ) = g (1 : ℤ)) : f = g :=
add_monoid_hom.ext_int w
-- TODO: this argument should be generalised to the situation where
-- the forgetful functor is representable.
lemma injective_of_mono {G H : AddCommGroup.{0}} (f : G ⟶ H) [mono f] : function.injective f :=
λ g₁ g₂ h,
begin
have t0 : as_hom g₁ ≫ f = as_hom g₂ ≫ f :=
begin
ext,
simpa [as_hom_apply] using h,
end,
have t1 : as_hom g₁ = as_hom g₂ := (cancel_mono _).1 t0,
apply as_hom_injective t1,
end
end AddCommGroup
variables {X Y : Type u}
/-- Build an isomorphism in the category `Group` from a `mul_equiv` between `group`s. -/
@[to_additive add_equiv.to_AddGroup_iso, simps]
def mul_equiv.to_Group_iso [group X] [group Y] (e : X ≃* Y) : Group.of X ≅ Group.of Y :=
{ hom := e.to_monoid_hom,
inv := e.symm.to_monoid_hom }
/-- Build an isomorphism in the category `AddGroup` from an `add_equiv` between `add_group`s. -/
add_decl_doc add_equiv.to_AddGroup_iso
/-- Build an isomorphism in the category `CommGroup` from a `mul_equiv` between `comm_group`s. -/
@[to_additive add_equiv.to_AddCommGroup_iso, simps]
def mul_equiv.to_CommGroup_iso [comm_group X] [comm_group Y] (e : X ≃* Y) :
CommGroup.of X ≅ CommGroup.of Y :=
{ hom := e.to_monoid_hom,
inv := e.symm.to_monoid_hom }
/-- Build an isomorphism in the category `AddCommGroup` from a `add_equiv` between
`add_comm_group`s. -/
add_decl_doc add_equiv.to_AddCommGroup_iso
namespace category_theory.iso
/-- Build a `mul_equiv` from an isomorphism in the category `Group`. -/
@[to_additive AddGroup_iso_to_add_equiv "Build an `add_equiv` from an isomorphism in the category
`AddGroup`.", simps]
def Group_iso_to_mul_equiv {X Y : Group} (i : X ≅ Y) : X ≃* Y :=
i.hom.to_mul_equiv i.inv i.hom_inv_id i.inv_hom_id
/-- Build a `mul_equiv` from an isomorphism in the category `CommGroup`. -/
@[to_additive AddCommGroup_iso_to_add_equiv "Build an `add_equiv` from an isomorphism
in the category `AddCommGroup`.", simps]
def CommGroup_iso_to_mul_equiv {X Y : CommGroup} (i : X ≅ Y) : X ≃* Y :=
i.hom.to_mul_equiv i.inv i.hom_inv_id i.inv_hom_id
end category_theory.iso
/-- multiplicative equivalences between `group`s are the same as (isomorphic to) isomorphisms
in `Group` -/
@[to_additive add_equiv_iso_AddGroup_iso "additive equivalences between `add_group`s are the same
as (isomorphic to) isomorphisms in `AddGroup`"]
def mul_equiv_iso_Group_iso {X Y : Type u} [group X] [group Y] :
(X ≃* Y) ≅ (Group.of X ≅ Group.of Y) :=
{ hom := λ e, e.to_Group_iso,
inv := λ i, i.Group_iso_to_mul_equiv, }
/-- multiplicative equivalences between `comm_group`s are the same as (isomorphic to) isomorphisms
in `CommGroup` -/
@[to_additive add_equiv_iso_AddCommGroup_iso "additive equivalences between `add_comm_group`s are
the same as (isomorphic to) isomorphisms in `AddCommGroup`"]
def mul_equiv_iso_CommGroup_iso {X Y : Type u} [comm_group X] [comm_group Y] :
(X ≃* Y) ≅ (CommGroup.of X ≅ CommGroup.of Y) :=
{ hom := λ e, e.to_CommGroup_iso,
inv := λ i, i.CommGroup_iso_to_mul_equiv, }
namespace category_theory.Aut
/-- The (bundled) group of automorphisms of a type is isomorphic to the (bundled) group
of permutations. -/
def iso_perm {α : Type u} : Group.of (Aut α) ≅ Group.of (equiv.perm α) :=
{ hom := ⟨λ g, g.to_equiv, (by tidy), (by tidy)⟩,
inv := ⟨λ g, g.to_iso, (by tidy), (by tidy)⟩ }
/-- The (unbundled) group of automorphisms of a type is `mul_equiv` to the (unbundled) group
of permutations. -/
def mul_equiv_perm {α : Type u} : Aut α ≃* equiv.perm α :=
iso_perm.Group_iso_to_mul_equiv
end category_theory.Aut
@[to_additive]
instance Group.forget_reflects_isos : reflects_isomorphisms (forget Group.{u}) :=
{ reflects := λ X Y f _,
begin
resetI,
let i := as_iso ((forget Group).map f),
let e : X ≃* Y := { ..f, ..i.to_equiv },
exact ⟨(is_iso.of_iso e.to_Group_iso).1⟩,
end }
@[to_additive]
instance CommGroup.forget_reflects_isos : reflects_isomorphisms (forget CommGroup.{u}) :=
{ reflects := λ X Y f _,
begin
resetI,
let i := as_iso ((forget CommGroup).map f),
let e : X ≃* Y := { ..f, ..i.to_equiv },
exact ⟨(is_iso.of_iso e.to_CommGroup_iso).1⟩,
end }
|
d1a45667af6f7800170fdc7cddb20a3c4ccb26d6 | d1a52c3f208fa42c41df8278c3d280f075eb020c | /src/Lean/Runtime.lean | 30a82e85989611edb9f3d7fb804aa7e93dab2613 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | cipher1024/lean4 | 6e1f98bb58e7a92b28f5364eb38a14c8d0aae393 | 69114d3b50806264ef35b57394391c3e738a9822 | refs/heads/master | 1,642,227,983,603 | 1,642,011,696,000 | 1,642,011,696,000 | 228,607,691 | 0 | 0 | Apache-2.0 | 1,576,584,269,000 | 1,576,584,268,000 | null | UTF-8 | Lean | false | false | 432 | 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
-/
namespace Lean
@[extern "lean_closure_max_args"]
constant closureMaxArgsFn : Unit → Nat
@[extern "lean_max_small_nat"]
constant maxSmallNatFn : Unit → Nat
def closureMaxArgs : Nat :=
closureMaxArgsFn ()
def maxSmallNat : Nat :=
maxSmallNatFn ()
end Lean
|
062ad3ce401a80e8ab0b886a3e088c7d67497e1b | 94e33a31faa76775069b071adea97e86e218a8ee | /src/data/set/finite.lean | 83920f652699df98271eea169be6228625b188ba | [
"Apache-2.0"
] | permissive | urkud/mathlib | eab80095e1b9f1513bfb7f25b4fa82fa4fd02989 | 6379d39e6b5b279df9715f8011369a301b634e41 | refs/heads/master | 1,658,425,342,662 | 1,658,078,703,000 | 1,658,078,703,000 | 186,910,338 | 0 | 0 | Apache-2.0 | 1,568,512,083,000 | 1,557,958,709,000 | Lean | UTF-8 | Lean | false | false | 49,325 | 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, Kyle Miller
-/
import data.finset.sort
import data.set.functor
import data.finite.basic
/-!
# Finite sets
This file defines predicates for finite and infinite sets and provides
`fintype` instances for many set constructions. It also proves basic facts
about finite sets and gives ways to manipulate `set.finite` expressions.
## Main definitions
* `set.finite : set α → Prop`
* `set.infinite : set α → Prop`
* `set.to_finite` to prove `set.finite` for a `set` from a `finite` instance.
* `set.finite.to_finset` to noncomputably produce a `finset` from a `set.finite` proof.
(See `set.to_finset` for a computable version.)
## Implementation
A finite set is defined to be a set whose coercion to a type has a `fintype` instance.
Since `set.finite` is `Prop`-valued, this is the mere fact that the `fintype` instance
exists.
There are two components to finiteness constructions. The first is `fintype` instances for each
construction. This gives a way to actually compute a `finset` that represents the set, and these
may be accessed using `set.to_finset`. This gets the `finset` in the correct form, since otherwise
`finset.univ : finset s` is a `finset` for the subtype for `s`. The second component is
"constructors" for `set.finite` that give proofs that `fintype` instances exist classically given
other `set.finite` proofs. Unlike the `fintype` instances, these *do not* use any decidability
instances since they do not compute anything.
## Tags
finite sets
-/
open set function
universes u v w x
variables {α : Type u} {β : Type v} {ι : Sort w} {γ : Type x}
namespace set
/-- A set is finite if there is a `finset` with the same elements.
This is represented as there being a `fintype` instance for the set
coerced to a type.
Note: this is a custom inductive type rather than `nonempty (fintype s)`
so that it won't be frozen as a local instance. -/
@[protected] inductive finite (s : set α) : Prop
| intro : fintype s → finite
-- The `protected` attribute does not take effect within the same namespace block.
end set
namespace set
lemma finite_def {s : set α} : s.finite ↔ nonempty (fintype s) := ⟨λ ⟨h⟩, ⟨h⟩, λ ⟨h⟩, ⟨h⟩⟩
alias finite_def ↔ finite.nonempty_fintype _
lemma finite_coe_iff {s : set α} : finite s ↔ s.finite :=
by rw [finite_iff_nonempty_fintype, finite_def]
/-- Constructor for `set.finite` using a `finite` instance. -/
theorem to_finite (s : set α) [finite s] : s.finite :=
finite_coe_iff.mp ‹_›
/-- Construct a `finite` instance for a `set` from a `finset` with the same elements. -/
protected lemma finite.of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : p.finite :=
⟨fintype.of_finset s H⟩
/-- Projection of `set.finite` to its `finite` instance.
This is intended to be used with dot notation.
See also `set.finite.fintype` and `set.finite.nonempty_fintype`. -/
protected lemma finite.to_subtype {s : set α} (h : s.finite) : finite s :=
finite_coe_iff.mpr h
/-- A finite set coerced to a type is a `fintype`.
This is the `fintype` projection for a `set.finite`.
Note that because `finite` isn't a typeclass, this definition will not fire if it
is made into an instance -/
noncomputable def finite.fintype {s : set α} (h : s.finite) : fintype s :=
h.nonempty_fintype.some
/-- Using choice, get the `finset` that represents this `set.` -/
noncomputable def finite.to_finset {s : set α} (h : s.finite) : finset α :=
@set.to_finset _ _ h.fintype
theorem finite.exists_finset {s : set α} (h : s.finite) :
∃ s' : finset α, ∀ a : α, a ∈ s' ↔ a ∈ s :=
by { casesI h, exact ⟨s.to_finset, λ _, mem_to_finset⟩ }
theorem finite.exists_finset_coe {s : set α} (h : s.finite) :
∃ s' : finset α, ↑s' = s :=
by { casesI h, exact ⟨s.to_finset, s.coe_to_finset⟩ }
/-- Finite sets can be lifted to finsets. -/
instance : can_lift (set α) (finset α) :=
{ coe := coe,
cond := set.finite,
prf := λ s hs, hs.exists_finset_coe }
/-- A set is infinite if it is not finite.
This is protected so that it does not conflict with global `infinite`. -/
protected def infinite (s : set α) : Prop := ¬ s.finite
@[simp] lemma not_infinite {s : set α} : ¬ s.infinite ↔ s.finite := not_not
/-- See also `fintype_or_infinite`. -/
lemma finite_or_infinite {s : set α} : s.finite ∨ s.infinite := em _
/-! ### Basic properties of `set.finite.to_finset` -/
section finite_to_finset
@[simp] lemma finite.coe_to_finset {s : set α} (h : s.finite) : (h.to_finset : set α) = s :=
@set.coe_to_finset _ s h.fintype
@[simp] theorem finite.mem_to_finset {s : set α} (h : s.finite) {a : α} : a ∈ h.to_finset ↔ a ∈ s :=
@mem_to_finset _ _ h.fintype _
@[simp] theorem finite.nonempty_to_finset {s : set α} (h : s.finite) :
h.to_finset.nonempty ↔ s.nonempty :=
by rw [← finset.coe_nonempty, finite.coe_to_finset]
@[simp] lemma finite.coe_sort_to_finset {s : set α} (h : s.finite) :
(h.to_finset : Type*) = s :=
by rw [← finset.coe_sort_coe _, h.coe_to_finset]
@[simp] lemma finite_empty_to_finset (h : (∅ : set α).finite) : h.to_finset = ∅ :=
by rw [← finset.coe_inj, h.coe_to_finset, finset.coe_empty]
@[simp] lemma finite.to_finset_inj {s t : set α} {hs : s.finite} {ht : t.finite} :
hs.to_finset = ht.to_finset ↔ s = t :=
by simp only [←finset.coe_inj, finite.coe_to_finset]
lemma subset_to_finset_iff {s : finset α} {t : set α} (ht : t.finite) :
s ⊆ ht.to_finset ↔ ↑s ⊆ t :=
by rw [← finset.coe_subset, ht.coe_to_finset]
@[simp] lemma finite_to_finset_eq_empty_iff {s : set α} {h : s.finite} :
h.to_finset = ∅ ↔ s = ∅ :=
by simp only [←finset.coe_inj, finite.coe_to_finset, finset.coe_empty]
@[simp, mono] lemma finite.to_finset_mono {s t : set α} {hs : s.finite} {ht : t.finite} :
hs.to_finset ⊆ ht.to_finset ↔ s ⊆ t :=
by simp only [← finset.coe_subset, finite.coe_to_finset]
@[simp, mono] lemma finite.to_finset_strict_mono {s t : set α} {hs : s.finite} {ht : t.finite} :
hs.to_finset ⊂ ht.to_finset ↔ s ⊂ t :=
by simp only [← finset.coe_ssubset, finite.coe_to_finset]
end finite_to_finset
/-! ### Fintype instances
Every instance here should have a corresponding `set.finite` constructor in the next section.
-/
section fintype_instances
instance fintype_univ [fintype α] : fintype (@univ α) :=
fintype.of_equiv α (equiv.set.univ α).symm
/-- If `(set.univ : set α)` is finite then `α` is a finite type. -/
noncomputable def fintype_of_finite_univ (H : (univ : set α).finite) : fintype α :=
@fintype.of_equiv _ (univ : set α) H.fintype (equiv.set.univ _)
instance fintype_union [decidable_eq α] (s t : set α) [fintype s] [fintype t] :
fintype (s ∪ t : set α) := fintype.of_finset (s.to_finset ∪ t.to_finset) $ by simp
instance fintype_sep (s : set α) (p : α → Prop) [fintype s] [decidable_pred p] :
fintype ({a ∈ s | p a} : set α) := fintype.of_finset (s.to_finset.filter p) $ by simp
instance fintype_inter (s t : set α) [decidable_eq α] [fintype s] [fintype t] :
fintype (s ∩ t : set α) := fintype.of_finset (s.to_finset ∩ t.to_finset) $ by simp
/-- A `fintype` instance for set intersection where the left set has a `fintype` instance. -/
instance fintype_inter_of_left (s t : set α) [fintype s] [decidable_pred (∈ t)] :
fintype (s ∩ t : set α) := fintype.of_finset (s.to_finset.filter (∈ t)) $ by simp
/-- A `fintype` instance for set intersection where the right set has a `fintype` instance. -/
instance fintype_inter_of_right (s t : set α) [fintype t] [decidable_pred (∈ s)] :
fintype (s ∩ t : set α) := fintype.of_finset (t.to_finset.filter (∈ s)) $ by simp [and_comm]
/-- A `fintype` structure on a set defines a `fintype` structure on its subset. -/
def fintype_subset (s : set α) {t : set α} [fintype s] [decidable_pred (∈ t)] (h : t ⊆ s) :
fintype t :=
by { rw ← inter_eq_self_of_subset_right h, apply set.fintype_inter_of_left }
instance fintype_diff [decidable_eq α] (s t : set α) [fintype s] [fintype t] :
fintype (s \ t : set α) := fintype.of_finset (s.to_finset \ t.to_finset) $ by simp
instance fintype_diff_left (s t : set α) [fintype s] [decidable_pred (∈ t)] :
fintype (s \ t : set α) := set.fintype_sep s (∈ tᶜ)
instance fintype_Union [decidable_eq α] [fintype (plift ι)]
(f : ι → set α) [∀ i, fintype (f i)] : fintype (⋃ i, f i) :=
fintype.of_finset (finset.univ.bUnion (λ i : plift ι, (f i.down).to_finset)) $ by simp
instance fintype_sUnion [decidable_eq α] {s : set (set α)}
[fintype s] [H : ∀ (t : s), fintype (t : set α)] : fintype (⋃₀ s) :=
by { rw sUnion_eq_Union, exact @set.fintype_Union _ _ _ _ _ H }
/-- A union of sets with `fintype` structure over a set with `fintype` structure has a `fintype`
structure. -/
def fintype_bUnion [decidable_eq α] {ι : Type*} (s : set ι) [fintype s]
(t : ι → set α) (H : ∀ i ∈ s, fintype (t i)) : fintype (⋃(x ∈ s), t x) :=
fintype.of_finset
(s.to_finset.attach.bUnion
(λ x, by { haveI := H x (by simpa using x.property), exact (t x).to_finset })) $ by simp
instance fintype_bUnion' [decidable_eq α] {ι : Type*} (s : set ι) [fintype s]
(t : ι → set α) [∀ i, fintype (t i)] : fintype (⋃(x ∈ s), t x) :=
fintype.of_finset (s.to_finset.bUnion (λ x, (t x).to_finset)) $ by simp
/-- If `s : set α` is a set with `fintype` instance and `f : α → set β` is a function such that
each `f a`, `a ∈ s`, has a `fintype` structure, then `s >>= f` has a `fintype` structure. -/
def fintype_bind {α β} [decidable_eq β] (s : set α) [fintype s]
(f : α → set β) (H : ∀ a ∈ s, fintype (f a)) : fintype (s >>= f) :=
set.fintype_bUnion s f H
instance fintype_bind' {α β} [decidable_eq β] (s : set α) [fintype s]
(f : α → set β) [H : ∀ a, fintype (f a)] : fintype (s >>= f) :=
set.fintype_bUnion' s f
instance fintype_empty : fintype (∅ : set α) := fintype.of_finset ∅ $ by simp
instance fintype_singleton (a : α) : fintype ({a} : set α) := fintype.of_finset {a} $ by simp
instance fintype_pure : ∀ a : α, fintype (pure a : set α) :=
set.fintype_singleton
/-- A `fintype` instance for inserting an element into a `set` using the
corresponding `insert` function on `finset`. This requires `decidable_eq α`.
There is also `set.fintype_insert'` when `a ∈ s` is decidable. -/
instance fintype_insert (a : α) (s : set α) [decidable_eq α] [fintype s] :
fintype (insert a s : set α) :=
fintype.of_finset (insert a s.to_finset) $ by simp
/-- A `fintype` structure on `insert a s` when inserting a new element. -/
def fintype_insert_of_not_mem {a : α} (s : set α) [fintype s] (h : a ∉ s) :
fintype (insert a s : set α) :=
fintype.of_finset ⟨a ::ₘ s.to_finset.1, s.to_finset.nodup.cons (by simp [h]) ⟩ $ by simp
/-- A `fintype` structure on `insert a s` when inserting a pre-existing element. -/
def fintype_insert_of_mem {a : α} (s : set α) [fintype s] (h : a ∈ s) :
fintype (insert a s : set α) :=
fintype.of_finset s.to_finset $ by simp [h]
/-- The `set.fintype_insert` instance requires decidable equality, but when `a ∈ s`
is decidable for this particular `a` we can still get a `fintype` instance by using
`set.fintype_insert_of_not_mem` or `set.fintype_insert_of_mem`.
This instance pre-dates `set.fintype_insert`, and it is less efficient.
When `decidable_mem_of_fintype` is made a local instance, then this instance would
override `set.fintype_insert` if not for the fact that its priority has been
adjusted. See Note [lower instance priority]. -/
@[priority 100]
instance fintype_insert' (a : α) (s : set α) [decidable $ a ∈ s] [fintype s] :
fintype (insert a s : set α) :=
if h : a ∈ s then fintype_insert_of_mem s h else fintype_insert_of_not_mem s h
instance fintype_image [decidable_eq β] (s : set α) (f : α → β) [fintype s] : fintype (f '' s) :=
fintype.of_finset (s.to_finset.image f) $ by simp
/-- If a function `f` has a partial inverse and sends a set `s` to a set with `[fintype]` instance,
then `s` has a `fintype` structure as well. -/
def fintype_of_fintype_image (s : set α)
{f : α → β} {g} (I : is_partial_inv f g) [fintype (f '' s)] : fintype s :=
fintype.of_finset ⟨_, (f '' s).to_finset.2.filter_map g $ injective_of_partial_inv_right I⟩ $ λ a,
begin
suffices : (∃ b x, f x = b ∧ g b = some a ∧ x ∈ s) ↔ a ∈ s,
by simpa [exists_and_distrib_left.symm, and.comm, and.left_comm, and.assoc],
rw exists_swap,
suffices : (∃ x, x ∈ s ∧ g (f x) = some a) ↔ a ∈ s, {simpa [and.comm, and.left_comm, and.assoc]},
simp [I _, (injective_of_partial_inv I).eq_iff]
end
instance fintype_range [decidable_eq α] (f : ι → α) [fintype (plift ι)] :
fintype (range f) :=
fintype.of_finset (finset.univ.image $ f ∘ plift.down) $ by simp [equiv.plift.exists_congr_left]
instance fintype_map {α β} [decidable_eq β] :
∀ (s : set α) (f : α → β) [fintype s], fintype (f <$> s) := set.fintype_image
instance fintype_lt_nat (n : ℕ) : fintype {i | i < n} :=
fintype.of_finset (finset.range n) $ by simp
instance fintype_le_nat (n : ℕ) : fintype {i | i ≤ n} :=
by simpa [nat.lt_succ_iff] using set.fintype_lt_nat (n+1)
/-- This is not an instance so that it does not conflict with the one
in src/order/locally_finite. -/
def nat.fintype_Iio (n : ℕ) : fintype (Iio n) :=
set.fintype_lt_nat n
instance fintype_prod (s : set α) (t : set β) [fintype s] [fintype t] :
fintype (s ×ˢ t : set (α × β)) :=
fintype.of_finset (s.to_finset.product t.to_finset) $ by simp
/-- `image2 f s t` is `fintype` if `s` and `t` are. -/
instance fintype_image2 [decidable_eq γ] (f : α → β → γ) (s : set α) (t : set β)
[hs : fintype s] [ht : fintype t] : fintype (image2 f s t : set γ) :=
by { rw ← image_prod, apply set.fintype_image }
instance fintype_seq [decidable_eq β] (f : set (α → β)) (s : set α) [fintype f] [fintype s] :
fintype (f.seq s) :=
by { rw seq_def, apply set.fintype_bUnion' }
instance fintype_seq' {α β : Type u} [decidable_eq β]
(f : set (α → β)) (s : set α) [fintype f] [fintype s] : fintype (f <*> s) :=
set.fintype_seq f s
instance fintype_mem_finset (s : finset α) : fintype {a | a ∈ s} :=
finset.fintype_coe_sort s
end fintype_instances
end set
/-! ### Finset -/
namespace finset
/-- Gives a `set.finite` for the `finset` coerced to a `set`.
This is a wrapper around `set.to_finite`. -/
lemma finite_to_set (s : finset α) : (s : set α).finite := set.to_finite _
@[simp] lemma finite_to_set_to_finset (s : finset α) : s.finite_to_set.to_finset = s :=
by { ext, rw [set.finite.mem_to_finset, mem_coe] }
end finset
namespace multiset
lemma finite_to_set (s : multiset α) : {x | x ∈ s}.finite :=
by { classical, simpa only [← multiset.mem_to_finset] using s.to_finset.finite_to_set }
@[simp] lemma finite_to_set_to_finset [decidable_eq α] (s : multiset α) :
s.finite_to_set.to_finset = s.to_finset :=
by { ext x, simp }
end multiset
lemma list.finite_to_set (l : list α) : {x | x ∈ l}.finite :=
(show multiset α, from ⟦l⟧).finite_to_set
/-! ### Finite instances
There is seemingly some overlap between the following instances and the `fintype` instances
in `data.set.finite`. While every `fintype` instance gives a `finite` instance, those
instances that depend on `fintype` or `decidable` instances need an additional `finite` instance
to be able to generally apply.
Some set instances do not appear here since they are consequences of others, for example
`subtype.finite` for subsets of a finite type.
-/
namespace finite.set
open_locale classical
example {s : set α} [finite α] : finite s := infer_instance
example : finite (∅ : set α) := infer_instance
example (a : α) : finite ({a} : set α) := infer_instance
instance finite_union (s t : set α) [finite s] [finite t] :
finite (s ∪ t : set α) :=
by { casesI nonempty_fintype s, casesI nonempty_fintype t, apply_instance }
instance finite_sep (s : set α) (p : α → Prop) [finite s] :
finite ({a ∈ s | p a} : set α) :=
by { casesI nonempty_fintype s, apply_instance }
protected lemma subset (s : set α) {t : set α} [finite s] (h : t ⊆ s) : finite t :=
by { rw eq_sep_of_subset h, apply_instance }
instance finite_inter_of_right (s t : set α) [finite t] :
finite (s ∩ t : set α) := finite.set.subset t (inter_subset_right s t)
instance finite_inter_of_left (s t : set α) [finite s] :
finite (s ∩ t : set α) := finite.set.subset s (inter_subset_left s t)
instance finite_diff (s t : set α) [finite s] :
finite (s \ t : set α) := finite.set.subset s (diff_subset s t)
instance finite_range (f : ι → α) [finite ι] : finite (range f) :=
by { haveI := fintype.of_finite (plift ι), apply_instance }
instance finite_Union [finite ι] (f : ι → set α) [∀ i, finite (f i)] : finite (⋃ i, f i) :=
begin
rw [Union_eq_range_psigma],
apply set.finite_range
end
instance finite_sUnion {s : set (set α)} [finite s] [H : ∀ (t : s), finite (t : set α)] :
finite (⋃₀ s) :=
by { rw sUnion_eq_Union, exact @finite.set.finite_Union _ _ _ _ H }
lemma finite_bUnion {ι : Type*} (s : set ι) [finite s] (t : ι → set α) (H : ∀ i ∈ s, finite (t i)) :
finite (⋃(x ∈ s), t x) :=
begin
rw [bUnion_eq_Union],
haveI : ∀ (i : s), finite (t i) := λ i, H i i.property,
apply_instance,
end
instance finite_bUnion' {ι : Type*} (s : set ι) [finite s] (t : ι → set α) [∀ i, finite (t i)] :
finite (⋃(x ∈ s), t x) :=
finite_bUnion s t (λ i h, infer_instance)
/--
Example: `finite (⋃ (i < n), f i)` where `f : ℕ → set α` and `[∀ i, finite (f i)]`
(when given instances from `data.nat.interval`).
-/
instance finite_bUnion'' {ι : Type*} (p : ι → Prop) [h : finite {x | p x}]
(t : ι → set α) [∀ i, finite (t i)] :
finite (⋃ x (h : p x), t x) :=
@finite.set.finite_bUnion' _ _ (set_of p) h t _
instance finite_Inter {ι : Sort*} [nonempty ι] (t : ι → set α) [∀ i, finite (t i)] :
finite (⋂ i, t i) :=
finite.set.subset (t $ classical.arbitrary ι) (Inter_subset _ _)
instance finite_insert (a : α) (s : set α) [finite s] : finite (insert a s : set α) :=
finite.set.finite_union {a} s
instance finite_image (s : set α) (f : α → β) [finite s] : finite (f '' s) :=
by { casesI nonempty_fintype s, apply_instance }
instance finite_replacement [finite α] (f : α → β) : finite {(f x) | (x : α)} :=
finite.set.finite_range f
instance finite_prod (s : set α) (t : set β) [finite s] [finite t] :
finite (s ×ˢ t : set (α × β)) :=
finite.of_equiv _ (equiv.set.prod s t).symm
instance finite_image2 (f : α → β → γ) (s : set α) (t : set β) [finite s] [finite t] :
finite (image2 f s t : set γ) :=
by { rw ← image_prod, apply_instance }
instance finite_seq (f : set (α → β)) (s : set α) [finite f] [finite s] : finite (f.seq s) :=
by { rw seq_def, apply_instance }
end finite.set
namespace set
/-! ### Constructors for `set.finite`
Every constructor here should have a corresponding `fintype` instance in the previous section
(or in the `fintype` module).
The implementation of these constructors ideally should be no more than `set.to_finite`,
after possibly setting up some `fintype` and classical `decidable` instances.
-/
section set_finite_constructors
@[nontriviality] lemma finite.of_subsingleton [subsingleton α] (s : set α) : s.finite := s.to_finite
theorem finite_univ [finite α] : (@univ α).finite := set.to_finite _
theorem finite_univ_iff : (@univ α).finite ↔ finite α :=
finite_coe_iff.symm.trans (equiv.set.univ α).finite_iff
alias finite_univ_iff ↔ _root_.finite.of_finite_univ _
theorem finite.union {s t : set α} (hs : s.finite) (ht : t.finite) : (s ∪ t).finite :=
by { casesI hs, casesI ht, apply to_finite }
lemma finite.sup {s t : set α} : s.finite → t.finite → (s ⊔ t).finite := finite.union
theorem finite.sep {s : set α} (hs : s.finite) (p : α → Prop) : {a ∈ s | p a}.finite :=
by { casesI hs, apply to_finite }
theorem finite.inter_of_left {s : set α} (hs : s.finite) (t : set α) : (s ∩ t).finite :=
by { casesI hs, apply to_finite }
theorem finite.inter_of_right {s : set α} (hs : s.finite) (t : set α) : (t ∩ s).finite :=
by { casesI hs, apply to_finite }
theorem finite.inf_of_left {s : set α} (h : s.finite) (t : set α) : (s ⊓ t).finite :=
h.inter_of_left t
theorem finite.inf_of_right {s : set α} (h : s.finite) (t : set α) : (t ⊓ s).finite :=
h.inter_of_right t
theorem finite.subset {s : set α} (hs : s.finite) {t : set α} (ht : t ⊆ s) : t.finite :=
by { casesI hs, haveI := finite.set.subset _ ht, apply to_finite }
theorem finite.diff {s : set α} (hs : s.finite) (t : set α) : (s \ t).finite :=
by { casesI hs, apply to_finite }
theorem finite.of_diff {s t : set α} (hd : (s \ t).finite) (ht : t.finite) : s.finite :=
(hd.union ht).subset $ subset_diff_union _ _
theorem finite_Union [finite ι] {f : ι → set α} (H : ∀ i, (f i).finite) :
(⋃ i, f i).finite :=
by { haveI := λ i, (H i).fintype, apply to_finite }
theorem finite.sUnion {s : set (set α)} (hs : s.finite) (H : ∀ t ∈ s, set.finite t) :
(⋃₀ s).finite :=
by { casesI hs, haveI := λ (i : s), (H i i.2).to_subtype, apply to_finite }
theorem finite.bUnion {ι} {s : set ι} (hs : s.finite)
{t : ι → set α} (ht : ∀ i ∈ s, (t i).finite) : (⋃(i ∈ s), t i).finite :=
by { classical, casesI hs,
haveI := fintype_bUnion s t (λ i hi, (ht i hi).fintype), apply to_finite }
/-- Dependent version of `finite.bUnion`. -/
theorem finite.bUnion' {ι} {s : set ι} (hs : s.finite)
{t : Π (i ∈ s), set α} (ht : ∀ i ∈ s, (t i ‹_›).finite) : (⋃(i ∈ s), t i ‹_›).finite :=
by { casesI hs, rw [bUnion_eq_Union], apply finite_Union (λ (i : s), ht i.1 i.2), }
theorem finite.sInter {α : Type*} {s : set (set α)} {t : set α} (ht : t ∈ s)
(hf : t.finite) : (⋂₀ s).finite :=
hf.subset (sInter_subset_of_mem ht)
theorem finite.bind {α β} {s : set α} {f : α → set β} (h : s.finite) (hf : ∀ a ∈ s, (f a).finite) :
(s >>= f).finite :=
h.bUnion hf
@[simp] theorem finite_empty : (∅ : set α).finite := to_finite _
@[simp] theorem finite_singleton (a : α) : ({a} : set α).finite := to_finite _
theorem finite_pure (a : α) : (pure a : set α).finite := to_finite _
@[simp] theorem finite.insert (a : α) {s : set α} (hs : s.finite) : (insert a s).finite :=
by { casesI hs, apply to_finite }
theorem finite.image {s : set α} (f : α → β) (hs : s.finite) : (f '' s).finite :=
by { casesI hs, apply to_finite }
theorem finite_range (f : ι → α) [finite ι] : (range f).finite :=
to_finite _
lemma finite.dependent_image {s : set α} (hs : s.finite) (F : Π i ∈ s, β) :
{y : β | ∃ x (hx : x ∈ s), y = F x hx}.finite :=
by { casesI hs, simpa [range, eq_comm] using finite_range (λ x : s, F x x.2) }
theorem finite.map {α β} {s : set α} : ∀ (f : α → β), s.finite → (f <$> s).finite :=
finite.image
theorem finite.of_finite_image {s : set α} {f : α → β} (h : (f '' s).finite) (hi : set.inj_on f s) :
s.finite :=
by { casesI h, exact ⟨fintype.of_injective (λ a, (⟨f a.1, mem_image_of_mem f a.2⟩ : f '' s))
(λ a b eq, subtype.eq $ hi a.2 b.2 $ subtype.ext_iff_val.1 eq)⟩ }
theorem finite.of_preimage {f : α → β} {s : set β} (h : (f ⁻¹' s).finite) (hf : surjective f) :
s.finite :=
hf.image_preimage s ▸ h.image _
theorem finite.preimage {s : set β} {f : α → β}
(I : set.inj_on f (f⁻¹' s)) (h : s.finite) : (f ⁻¹' s).finite :=
(h.subset (image_preimage_subset f s)).of_finite_image I
theorem finite.preimage_embedding {s : set β} (f : α ↪ β) (h : s.finite) : (f ⁻¹' s).finite :=
h.preimage (λ _ _ _ _ h', f.injective h')
lemma finite_lt_nat (n : ℕ) : set.finite {i | i < n} := to_finite _
lemma finite_le_nat (n : ℕ) : set.finite {i | i ≤ n} := to_finite _
lemma finite.prod {s : set α} {t : set β} (hs : s.finite) (ht : t.finite) :
(s ×ˢ t : set (α × β)).finite :=
by { casesI hs, casesI ht, apply to_finite }
lemma finite.image2 (f : α → β → γ) {s : set α} {t : set β} (hs : s.finite) (ht : t.finite) :
(image2 f s t).finite :=
by { casesI hs, casesI ht, apply to_finite }
theorem finite.seq {f : set (α → β)} {s : set α} (hf : f.finite) (hs : s.finite) :
(f.seq s).finite :=
by { classical, casesI hf, casesI hs, apply to_finite }
theorem finite.seq' {α β : Type u} {f : set (α → β)} {s : set α} (hf : f.finite) (hs : s.finite) :
(f <*> s).finite :=
hf.seq hs
theorem finite_mem_finset (s : finset α) : {a | a ∈ s}.finite := to_finite _
lemma subsingleton.finite {s : set α} (h : s.subsingleton) : s.finite :=
h.induction_on finite_empty finite_singleton
theorem exists_finite_iff_finset {p : set α → Prop} :
(∃ s : set α, s.finite ∧ p s) ↔ ∃ s : finset α, p ↑s :=
⟨λ ⟨s, hs, hps⟩, ⟨hs.to_finset, hs.coe_to_finset.symm ▸ hps⟩,
λ ⟨s, hs⟩, ⟨s, s.finite_to_set, hs⟩⟩
/-- There are finitely many subsets of a given finite set -/
lemma finite.finite_subsets {α : Type u} {a : set α} (h : a.finite) : {b | b ⊆ a}.finite :=
⟨fintype.of_finset ((finset.powerset h.to_finset).map finset.coe_emb.1) $ λ s,
by simpa [← @exists_finite_iff_finset α (λ t, t ⊆ a ∧ t = s), subset_to_finset_iff,
← and.assoc] using h.subset⟩
/-- Finite product of finite sets is finite -/
lemma finite.pi {δ : Type*} [fintype δ] {κ : δ → Type*} {t : Π d, set (κ d)}
(ht : ∀ d, (t d).finite) :
(pi univ t).finite :=
begin
lift t to Π d, finset (κ d) using ht,
classical,
rw ← fintype.coe_pi_finset,
apply finset.finite_to_set
end
/-- A finite union of finsets is finite. -/
lemma union_finset_finite_of_range_finite (f : α → finset β) (h : (range f).finite) :
(⋃ a, (f a : set β)).finite :=
by { rw ← bUnion_range, exact h.bUnion (λ y hy, y.finite_to_set) }
lemma finite_range_ite {p : α → Prop} [decidable_pred p] {f g : α → β} (hf : (range f).finite)
(hg : (range g).finite) : (range (λ x, if p x then f x else g x)).finite :=
(hf.union hg).subset range_ite_subset
lemma finite_range_const {c : β} : (range (λ x : α, c)).finite :=
(finite_singleton c).subset range_const_subset
end set_finite_constructors
/-! ### Properties -/
instance finite.inhabited : inhabited {s : set α // s.finite} := ⟨⟨∅, finite_empty⟩⟩
@[simp] lemma finite_union {s t : set α} : (s ∪ t).finite ↔ s.finite ∧ t.finite :=
⟨λ h, ⟨h.subset (subset_union_left _ _), h.subset (subset_union_right _ _)⟩,
λ ⟨hs, ht⟩, hs.union ht⟩
theorem finite_image_iff {s : set α} {f : α → β} (hi : inj_on f s) :
(f '' s).finite ↔ s.finite :=
⟨λ h, h.of_finite_image hi, finite.image _⟩
lemma univ_finite_iff_nonempty_fintype :
(univ : set α).finite ↔ nonempty (fintype α) :=
⟨λ h, ⟨fintype_of_finite_univ h⟩, λ ⟨_i⟩, by exactI finite_univ⟩
@[simp] lemma finite.to_finset_singleton {a : α} (ha : ({a} : set α).finite := finite_singleton _) :
ha.to_finset = {a} :=
finset.ext $ by simp
@[simp] lemma finite.to_finset_insert [decidable_eq α] {s : set α} {a : α}
(hs : (insert a s).finite) :
hs.to_finset = insert a (hs.subset $ subset_insert _ _).to_finset :=
finset.ext $ by simp
lemma finite.to_finset_insert' [decidable_eq α] {a : α} {s : set α} (hs : s.finite) :
(hs.insert a).to_finset = insert a hs.to_finset :=
finite.to_finset_insert _
lemma finite.fin_embedding {s : set α} (h : s.finite) : ∃ (n : ℕ) (f : fin n ↪ α), range f = s :=
⟨_, (fintype.equiv_fin (h.to_finset : set α)).symm.as_embedding, by simp⟩
lemma finite.fin_param {s : set α} (h : s.finite) :
∃ (n : ℕ) (f : fin n → α), injective f ∧ range f = s :=
let ⟨n, f, hf⟩ := h.fin_embedding in ⟨n, f, f.injective, hf⟩
lemma finite_option {s : set (option α)} : s.finite ↔ {x : α | some x ∈ s}.finite :=
⟨λ h, h.preimage_embedding embedding.some,
λ h, ((h.image some).insert none).subset $
λ x, option.cases_on x (λ _, or.inl rfl) (λ x hx, or.inr $ mem_image_of_mem _ hx)⟩
lemma finite_image_fst_and_snd_iff {s : set (α × β)} :
(prod.fst '' s).finite ∧ (prod.snd '' s).finite ↔ s.finite :=
⟨λ h, (h.1.prod h.2).subset $ λ x h, ⟨mem_image_of_mem _ h, mem_image_of_mem _ h⟩,
λ h, ⟨h.image _, h.image _⟩⟩
lemma forall_finite_image_eval_iff {δ : Type*} [fintype δ] {κ : δ → Type*} {s : set (Π d, κ d)} :
(∀ d, (eval d '' s).finite) ↔ s.finite :=
⟨λ h, (finite.pi h).subset $ subset_pi_eval_image _ _, λ h d, h.image _⟩
lemma finite_subset_Union {s : set α} (hs : s.finite)
{ι} {t : ι → set α} (h : s ⊆ ⋃ i, t i) : ∃ I : set ι, I.finite ∧ s ⊆ ⋃ i ∈ I, t i :=
begin
casesI hs,
choose f hf using show ∀ x : s, ∃ i, x.1 ∈ t i, {simpa [subset_def] using h},
refine ⟨range f, finite_range f, λ x hx, _⟩,
rw [bUnion_range, mem_Union],
exact ⟨⟨x, hx⟩, hf _⟩
end
lemma eq_finite_Union_of_finite_subset_Union {ι} {s : ι → set α} {t : set α} (tfin : t.finite)
(h : t ⊆ ⋃ i, s i) :
∃ I : set ι, I.finite ∧ ∃ σ : {i | i ∈ I} → set α,
(∀ i, (σ i).finite) ∧ (∀ i, σ i ⊆ s i) ∧ t = ⋃ i, σ i :=
let ⟨I, Ifin, hI⟩ := finite_subset_Union tfin h in
⟨I, Ifin, λ x, s x ∩ t,
λ i, tfin.subset (inter_subset_right _ _),
λ i, inter_subset_left _ _,
begin
ext x,
rw mem_Union,
split,
{ intro x_in,
rcases mem_Union.mp (hI x_in) with ⟨i, _, ⟨hi, rfl⟩, H⟩,
use [i, hi, H, x_in] },
{ rintros ⟨i, hi, H⟩,
exact H }
end⟩
@[elab_as_eliminator]
theorem finite.induction_on {C : set α → Prop} {s : set α} (h : s.finite)
(H0 : C ∅) (H1 : ∀ {a s}, a ∉ s → set.finite s → C s → C (insert a s)) : C s :=
begin
lift s to finset α using h,
induction s using finset.cons_induction_on with a s ha hs,
{ rwa [finset.coe_empty] },
{ rw [finset.coe_cons],
exact @H1 a s ha (set.to_finite _) hs }
end
@[elab_as_eliminator]
theorem finite.dinduction_on {C : ∀ (s : set α), s.finite → Prop} {s : set α} (h : s.finite)
(H0 : C ∅ finite_empty)
(H1 : ∀ {a s}, a ∉ s → ∀ h : set.finite s, C s h → C (insert a s) (h.insert a)) :
C s h :=
have ∀ h : s.finite, C s h,
from finite.induction_on h (λ h, H0) (λ a s has hs ih h, H1 has hs (ih _)),
this h
section
local attribute [instance] nat.fintype_Iio
/--
If `P` is some relation between terms of `γ` and sets in `γ`,
such that every finite set `t : set γ` has some `c : γ` related to it,
then there is a recursively defined sequence `u` in `γ`
so `u n` is related to the image of `{0, 1, ..., n-1}` under `u`.
(We use this later to show sequentially compact sets
are totally bounded.)
-/
lemma seq_of_forall_finite_exists {γ : Type*}
{P : γ → set γ → Prop} (h : ∀ t : set γ, t.finite → ∃ c, P c t) :
∃ u : ℕ → γ, ∀ n, P (u n) (u '' Iio n) :=
⟨λ n, @nat.strong_rec_on' (λ _, γ) n $ λ n ih, classical.some $ h
(range $ λ m : Iio n, ih m.1 m.2)
(finite_range _),
λ n, begin
classical,
refine nat.strong_rec_on' n (λ n ih, _),
rw nat.strong_rec_on_beta', convert classical.some_spec (h _ _),
ext x, split,
{ rintros ⟨m, hmn, rfl⟩, exact ⟨⟨m, hmn⟩, rfl⟩ },
{ rintros ⟨⟨m, hmn⟩, rfl⟩, exact ⟨m, hmn, rfl⟩ }
end⟩
end
/-! ### Cardinality -/
theorem empty_card : fintype.card (∅ : set α) = 0 := rfl
@[simp] theorem empty_card' {h : fintype.{u} (∅ : set α)} :
@fintype.card (∅ : set α) h = 0 :=
eq.trans (by congr) empty_card
theorem card_fintype_insert_of_not_mem {a : α} (s : set α) [fintype s] (h : a ∉ s) :
@fintype.card _ (fintype_insert_of_not_mem s h) = fintype.card s + 1 :=
by rw [fintype_insert_of_not_mem, fintype.card_of_finset];
simp [finset.card, to_finset]; refl
@[simp] theorem card_insert {a : α} (s : set α)
[fintype s] (h : a ∉ s) {d : fintype.{u} (insert a s : set α)} :
@fintype.card _ d = fintype.card s + 1 :=
by rw ← card_fintype_insert_of_not_mem s h; congr
lemma card_image_of_inj_on {s : set α} [fintype s]
{f : α → β} [fintype (f '' s)] (H : ∀x∈s, ∀y∈s, f x = f y → x = y) :
fintype.card (f '' s) = fintype.card s :=
by haveI := classical.prop_decidable; exact
calc fintype.card (f '' s) = (s.to_finset.image f).card : fintype.card_of_finset' _ (by simp)
... = s.to_finset.card : finset.card_image_of_inj_on
(λ x hx y hy hxy, H x (mem_to_finset.1 hx) y (mem_to_finset.1 hy) hxy)
... = fintype.card s : (fintype.card_of_finset' _ (λ a, mem_to_finset)).symm
lemma card_image_of_injective (s : set α) [fintype s]
{f : α → β} [fintype (f '' s)] (H : function.injective f) :
fintype.card (f '' s) = fintype.card s :=
card_image_of_inj_on $ λ _ _ _ _ h, H h
@[simp] theorem card_singleton (a : α) :
fintype.card ({a} : set α) = 1 :=
fintype.card_of_subsingleton _
lemma card_lt_card {s t : set α} [fintype s] [fintype t] (h : s ⊂ t) :
fintype.card s < fintype.card t :=
fintype.card_lt_of_injective_not_surjective (set.inclusion h.1) (set.inclusion_injective h.1) $
λ hst, (ssubset_iff_subset_ne.1 h).2 (eq_of_inclusion_surjective hst)
lemma card_le_of_subset {s t : set α} [fintype s] [fintype t] (hsub : s ⊆ t) :
fintype.card s ≤ fintype.card t :=
fintype.card_le_of_injective (set.inclusion hsub) (set.inclusion_injective hsub)
lemma eq_of_subset_of_card_le {s t : set α} [fintype s] [fintype t]
(hsub : s ⊆ t) (hcard : fintype.card t ≤ fintype.card s) : s = t :=
(eq_or_ssubset_of_subset hsub).elim id
(λ h, absurd hcard $ not_le_of_lt $ card_lt_card h)
lemma card_range_of_injective [fintype α] {f : α → β} (hf : injective f)
[fintype (range f)] : fintype.card (range f) = fintype.card α :=
eq.symm $ fintype.card_congr $ equiv.of_injective f hf
lemma finite.card_to_finset {s : set α} [fintype s] (h : s.finite) :
h.to_finset.card = fintype.card s :=
begin
rw [← finset.card_attach, finset.attach_eq_univ, ← fintype.card],
refine fintype.card_congr (equiv.set_congr _),
ext x,
show x ∈ h.to_finset ↔ x ∈ s,
simp,
end
lemma card_ne_eq [fintype α] (a : α) [fintype {x : α | x ≠ a}] :
fintype.card {x : α | x ≠ a} = fintype.card α - 1 :=
begin
haveI := classical.dec_eq α,
rw [←to_finset_card, to_finset_ne_eq_erase, finset.card_erase_of_mem (finset.mem_univ _),
finset.card_univ],
end
/-! ### Infinite sets -/
theorem infinite_univ_iff : (@univ α).infinite ↔ infinite α :=
by rw [set.infinite, finite_univ_iff, not_finite_iff_infinite]
theorem infinite_univ [h : infinite α] : (@univ α).infinite :=
infinite_univ_iff.2 h
theorem infinite_coe_iff {s : set α} : infinite s ↔ s.infinite :=
⟨λ ⟨h₁⟩ h₂, h₁ h₂.fintype, λ h₁, ⟨λ h₂, h₁ ⟨h₂⟩⟩⟩
theorem infinite.to_subtype {s : set α} (h : s.infinite) : infinite s :=
infinite_coe_iff.2 h
/-- Embedding of `ℕ` into an infinite set. -/
noncomputable def infinite.nat_embedding (s : set α) (h : s.infinite) : ℕ ↪ s :=
by { haveI := h.to_subtype, exact infinite.nat_embedding s }
lemma infinite.exists_subset_card_eq {s : set α} (hs : s.infinite) (n : ℕ) :
∃ t : finset α, ↑t ⊆ s ∧ t.card = n :=
⟨((finset.range n).map (hs.nat_embedding _)).map (embedding.subtype _), by simp⟩
lemma infinite.nonempty {s : set α} (h : s.infinite) : s.nonempty :=
let a := infinite.nat_embedding s h 37 in ⟨a.1, a.2⟩
lemma infinite_of_finite_compl [infinite α] {s : set α} (hs : sᶜ.finite) : s.infinite :=
λ h, set.infinite_univ (by simpa using hs.union h)
lemma finite.infinite_compl [infinite α] {s : set α} (hs : s.finite) : sᶜ.infinite :=
λ h, set.infinite_univ (by simpa using hs.union h)
protected theorem infinite.mono {s t : set α} (h : s ⊆ t) : s.infinite → t.infinite :=
mt (λ ht, ht.subset h)
lemma infinite.diff {s t : set α} (hs : s.infinite) (ht : t.finite) : (s \ t).infinite :=
λ h, hs $ h.of_diff ht
@[simp] lemma infinite_union {s t : set α} : (s ∪ t).infinite ↔ s.infinite ∨ t.infinite :=
by simp only [set.infinite, finite_union, not_and_distrib]
theorem infinite_of_infinite_image (f : α → β) {s : set α} (hs : (f '' s).infinite) :
s.infinite :=
mt (finite.image f) hs
theorem infinite_image_iff {s : set α} {f : α → β} (hi : inj_on f s) :
(f '' s).infinite ↔ s.infinite :=
not_congr $ finite_image_iff hi
theorem infinite_of_inj_on_maps_to {s : set α} {t : set β} {f : α → β}
(hi : inj_on f s) (hm : maps_to f s t) (hs : s.infinite) : t.infinite :=
((infinite_image_iff hi).2 hs).mono (maps_to'.mp hm)
theorem infinite.exists_ne_map_eq_of_maps_to {s : set α} {t : set β} {f : α → β}
(hs : s.infinite) (hf : maps_to f s t) (ht : t.finite) :
∃ (x ∈ s) (y ∈ s), x ≠ y ∧ f x = f y :=
begin
contrapose! ht,
exact infinite_of_inj_on_maps_to (λ x hx y hy, not_imp_not.1 (ht x hx y hy)) hf hs
end
theorem infinite_range_of_injective [infinite α] {f : α → β} (hi : injective f) :
(range f).infinite :=
by { rw [←image_univ, infinite_image_iff (inj_on_of_injective hi _)], exact infinite_univ }
theorem infinite_of_injective_forall_mem [infinite α] {s : set β} {f : α → β}
(hi : injective f) (hf : ∀ x : α, f x ∈ s) : s.infinite :=
by { rw ←range_subset_iff at hf, exact (infinite_range_of_injective hi).mono hf }
lemma infinite.exists_nat_lt {s : set ℕ} (hs : s.infinite) (n : ℕ) : ∃ m ∈ s, n < m :=
let ⟨m, hm⟩ := (hs.diff $ set.finite_le_nat n).nonempty in ⟨m, by simpa using hm⟩
lemma infinite.exists_not_mem_finset {s : set α} (hs : s.infinite) (f : finset α) :
∃ a ∈ s, a ∉ f :=
let ⟨a, has, haf⟩ := (hs.diff (to_finite f)).nonempty
in ⟨a, has, λ h, haf $ finset.mem_coe.1 h⟩
/-! ### Order properties -/
lemma finite_is_top (α : Type*) [partial_order α] : {x : α | is_top x}.finite :=
(subsingleton_is_top α).finite
lemma finite_is_bot (α : Type*) [partial_order α] : {x : α | is_bot x}.finite :=
(subsingleton_is_bot α).finite
theorem infinite.exists_lt_map_eq_of_maps_to [linear_order α] {s : set α} {t : set β} {f : α → β}
(hs : s.infinite) (hf : maps_to f s t) (ht : t.finite) :
∃ (x ∈ s) (y ∈ s), x < y ∧ f x = f y :=
let ⟨x, hx, y, hy, hxy, hf⟩ := hs.exists_ne_map_eq_of_maps_to hf ht
in hxy.lt_or_lt.elim (λ hxy, ⟨x, hx, y, hy, hxy, hf⟩) (λ hyx, ⟨y, hy, x, hx, hyx, hf.symm⟩)
lemma finite.exists_lt_map_eq_of_range_subset [linear_order α] [infinite α] {t : set β}
{f : α → β} (hf : range f ⊆ t) (ht : t.finite) :
∃ a b, a < b ∧ f a = f b :=
begin
rw [range_subset_iff, ←maps_univ_to] at hf,
obtain ⟨a, -, b, -, h⟩ := (@infinite_univ α _).exists_lt_map_eq_of_maps_to hf ht,
exact ⟨a, b, h⟩,
end
lemma exists_min_image [linear_order β] (s : set α) (f : α → β) (h1 : s.finite) :
s.nonempty → ∃ a ∈ s, ∀ b ∈ s, f a ≤ f b
| ⟨x, hx⟩ := by simpa only [exists_prop, finite.mem_to_finset]
using h1.to_finset.exists_min_image f ⟨x, h1.mem_to_finset.2 hx⟩
lemma exists_max_image [linear_order β] (s : set α) (f : α → β) (h1 : s.finite) :
s.nonempty → ∃ a ∈ s, ∀ b ∈ s, f b ≤ f a
| ⟨x, hx⟩ := by simpa only [exists_prop, finite.mem_to_finset]
using h1.to_finset.exists_max_image f ⟨x, h1.mem_to_finset.2 hx⟩
theorem exists_lower_bound_image [hα : nonempty α] [linear_order β] (s : set α) (f : α → β)
(h : s.finite) : ∃ (a : α), ∀ b ∈ s, f a ≤ f b :=
begin
by_cases hs : set.nonempty s,
{ exact let ⟨x₀, H, hx₀⟩ := set.exists_min_image s f h hs in ⟨x₀, λ x hx, hx₀ x hx⟩ },
{ exact nonempty.elim hα (λ a, ⟨a, λ x hx, absurd (set.nonempty_of_mem hx) hs⟩) }
end
theorem exists_upper_bound_image [hα : nonempty α] [linear_order β] (s : set α) (f : α → β)
(h : s.finite) : ∃ (a : α), ∀ b ∈ s, f b ≤ f a :=
begin
by_cases hs : set.nonempty s,
{ exact let ⟨x₀, H, hx₀⟩ := set.exists_max_image s f h hs in ⟨x₀, λ x hx, hx₀ x hx⟩ },
{ exact nonempty.elim hα (λ a, ⟨a, λ x hx, absurd (set.nonempty_of_mem hx) hs⟩) }
end
lemma finite.supr_binfi_of_monotone {ι ι' α : Type*} [preorder ι'] [nonempty ι']
[is_directed ι' (≤)] [order.frame α] {s : set ι} (hs : s.finite) {f : ι → ι' → α}
(hf : ∀ i ∈ s, monotone (f i)) :
(⨆ j, ⨅ i ∈ s, f i j) = ⨅ i ∈ s, ⨆ j, f i j :=
begin
revert hf,
refine hs.induction_on _ _,
{ intro hf, simp [supr_const] },
{ intros a s has hs ihs hf,
rw [ball_insert_iff] at hf,
simp only [infi_insert, ← ihs hf.2],
exact supr_inf_of_monotone hf.1 (λ j₁ j₂ hj, infi₂_mono $ λ i hi, hf.2 i hi hj) }
end
lemma finite.supr_binfi_of_antitone {ι ι' α : Type*} [preorder ι'] [nonempty ι']
[is_directed ι' (swap (≤))] [order.frame α] {s : set ι} (hs : s.finite) {f : ι → ι' → α}
(hf : ∀ i ∈ s, antitone (f i)) :
(⨆ j, ⨅ i ∈ s, f i j) = ⨅ i ∈ s, ⨆ j, f i j :=
@finite.supr_binfi_of_monotone ι ι'ᵒᵈ α _ _ _ _ _ hs _ (λ i hi, (hf i hi).dual_left)
lemma finite.infi_bsupr_of_monotone {ι ι' α : Type*} [preorder ι'] [nonempty ι']
[is_directed ι' (swap (≤))] [order.coframe α] {s : set ι} (hs : s.finite) {f : ι → ι' → α}
(hf : ∀ i ∈ s, monotone (f i)) :
(⨅ j, ⨆ i ∈ s, f i j) = ⨆ i ∈ s, ⨅ j, f i j :=
hs.supr_binfi_of_antitone (λ i hi, (hf i hi).dual_right)
lemma finite.infi_bsupr_of_antitone {ι ι' α : Type*} [preorder ι'] [nonempty ι']
[is_directed ι' (≤)] [order.coframe α] {s : set ι} (hs : s.finite) {f : ι → ι' → α}
(hf : ∀ i ∈ s, antitone (f i)) :
(⨅ j, ⨆ i ∈ s, f i j) = ⨆ i ∈ s, ⨅ j, f i j :=
hs.supr_binfi_of_monotone (λ i hi, (hf i hi).dual_right)
lemma _root_.supr_infi_of_monotone {ι ι' α : Type*} [fintype ι] [preorder ι'] [nonempty ι']
[is_directed ι' (≤)] [order.frame α] {f : ι → ι' → α} (hf : ∀ i, monotone (f i)) :
(⨆ j, ⨅ i, f i j) = ⨅ i, ⨆ j, f i j :=
by simpa only [infi_univ] using finite_univ.supr_binfi_of_monotone (λ i hi, hf i)
lemma _root_.supr_infi_of_antitone {ι ι' α : Type*} [fintype ι] [preorder ι'] [nonempty ι']
[is_directed ι' (swap (≤))] [order.frame α] {f : ι → ι' → α} (hf : ∀ i, antitone (f i)) :
(⨆ j, ⨅ i, f i j) = ⨅ i, ⨆ j, f i j :=
@supr_infi_of_monotone ι ι'ᵒᵈ α _ _ _ _ _ _ (λ i, (hf i).dual_left)
lemma _root_.infi_supr_of_monotone {ι ι' α : Type*} [fintype ι] [preorder ι'] [nonempty ι']
[is_directed ι' (swap (≤))] [order.coframe α] {f : ι → ι' → α} (hf : ∀ i, monotone (f i)) :
(⨅ j, ⨆ i, f i j) = ⨆ i, ⨅ j, f i j :=
supr_infi_of_antitone (λ i, (hf i).dual_right)
lemma _root_.infi_supr_of_antitone {ι ι' α : Type*} [fintype ι] [preorder ι'] [nonempty ι']
[is_directed ι' (≤)] [order.coframe α] {f : ι → ι' → α} (hf : ∀ i, antitone (f i)) :
(⨅ j, ⨆ i, f i j) = ⨆ i, ⨅ j, f i j :=
supr_infi_of_monotone (λ i, (hf i).dual_right)
/-- An increasing union distributes over finite intersection. -/
lemma Union_Inter_of_monotone {ι ι' α : Type*} [fintype ι] [preorder ι'] [is_directed ι' (≤)]
[nonempty ι'] {s : ι → ι' → set α} (hs : ∀ i, monotone (s i)) :
(⋃ j : ι', ⋂ i : ι, s i j) = ⋂ i : ι, ⋃ j : ι', s i j :=
supr_infi_of_monotone hs
/-- A decreasing union distributes over finite intersection. -/
lemma Union_Inter_of_antitone {ι ι' α : Type*} [fintype ι] [preorder ι'] [is_directed ι' (swap (≤))]
[nonempty ι'] {s : ι → ι' → set α} (hs : ∀ i, antitone (s i)) :
(⋃ j : ι', ⋂ i : ι, s i j) = ⋂ i : ι, ⋃ j : ι', s i j :=
supr_infi_of_antitone hs
/-- An increasing intersection distributes over finite union. -/
lemma Inter_Union_of_monotone {ι ι' α : Type*} [fintype ι] [preorder ι'] [is_directed ι' (swap (≤))]
[nonempty ι'] {s : ι → ι' → set α} (hs : ∀ i, monotone (s i)) :
(⋂ j : ι', ⋃ i : ι, s i j) = ⋃ i : ι, ⋂ j : ι', s i j :=
infi_supr_of_monotone hs
/-- A decreasing intersection distributes over finite union. -/
lemma Inter_Union_of_antitone {ι ι' α : Type*} [fintype ι] [preorder ι'] [is_directed ι' (≤)]
[nonempty ι'] {s : ι → ι' → set α} (hs : ∀ i, antitone (s i)) :
(⋂ j : ι', ⋃ i : ι, s i j) = ⋃ i : ι, ⋂ j : ι', s i j :=
infi_supr_of_antitone hs
lemma Union_pi_of_monotone {ι ι' : Type*} [linear_order ι'] [nonempty ι'] {α : ι → Type*}
{I : set ι} {s : Π i, ι' → set (α i)} (hI : I.finite) (hs : ∀ i ∈ I, monotone (s i)) :
(⋃ j : ι', I.pi (λ i, s i j)) = I.pi (λ i, ⋃ j, s i j) :=
begin
simp only [pi_def, bInter_eq_Inter, preimage_Union],
haveI := hI.fintype,
exact Union_Inter_of_monotone (λ i j₁ j₂ h, preimage_mono $ hs i i.2 h)
end
lemma Union_univ_pi_of_monotone {ι ι' : Type*} [linear_order ι'] [nonempty ι'] [fintype ι]
{α : ι → Type*} {s : Π i, ι' → set (α i)} (hs : ∀ i, monotone (s i)) :
(⋃ j : ι', pi univ (λ i, s i j)) = pi univ (λ i, ⋃ j, s i j) :=
Union_pi_of_monotone finite_univ (λ i _, hs i)
lemma finite_range_find_greatest {P : α → ℕ → Prop} [∀ x, decidable_pred (P x)] {b : ℕ} :
(range (λ x, nat.find_greatest (P x) b)).finite :=
(finite_le_nat b).subset $ range_subset_iff.2 $ λ x, nat.find_greatest_le _
lemma finite.exists_maximal_wrt [partial_order β] (f : α → β) (s : set α) (h : set.finite s) :
s.nonempty → ∃ a ∈ s, ∀ a' ∈ s, f a ≤ f a' → f a = f a' :=
begin
refine h.induction_on _ _,
{ exact λ h, absurd h empty_not_nonempty },
intros a s his _ ih _,
cases s.eq_empty_or_nonempty with h h,
{ use a, simp [h] },
rcases ih h with ⟨b, hb, ih⟩,
by_cases f b ≤ f a,
{ refine ⟨a, set.mem_insert _ _, λ c hc hac, le_antisymm hac _⟩,
rcases set.mem_insert_iff.1 hc with rfl | hcs,
{ refl },
{ rwa [← ih c hcs (le_trans h hac)] } },
{ refine ⟨b, set.mem_insert_of_mem _ hb, λ c hc hbc, _⟩,
rcases set.mem_insert_iff.1 hc with rfl | hcs,
{ exact (h hbc).elim },
{ exact ih c hcs hbc } }
end
section
variables [semilattice_sup α] [nonempty α] {s : set α}
/--A finite set is bounded above.-/
protected lemma finite.bdd_above (hs : s.finite) : bdd_above s :=
finite.induction_on hs bdd_above_empty $ λ a s _ _ h, h.insert a
/--A finite union of sets which are all bounded above is still bounded above.-/
lemma finite.bdd_above_bUnion {I : set β} {S : β → set α} (H : I.finite) :
(bdd_above (⋃i∈I, S i)) ↔ (∀i ∈ I, bdd_above (S i)) :=
finite.induction_on H
(by simp only [bUnion_empty, bdd_above_empty, ball_empty_iff])
(λ a s ha _ hs, by simp only [bUnion_insert, ball_insert_iff, bdd_above_union, hs])
lemma infinite_of_not_bdd_above : ¬ bdd_above s → s.infinite :=
begin
contrapose!,
rw not_infinite,
apply finite.bdd_above,
end
end
section
variables [semilattice_inf α] [nonempty α] {s : set α}
/--A finite set is bounded below.-/
protected lemma finite.bdd_below (hs : s.finite) : bdd_below s := @finite.bdd_above αᵒᵈ _ _ _ hs
/--A finite union of sets which are all bounded below is still bounded below.-/
lemma finite.bdd_below_bUnion {I : set β} {S : β → set α} (H : I.finite) :
bdd_below (⋃ i ∈ I, S i) ↔ ∀ i ∈ I, bdd_below (S i) :=
@finite.bdd_above_bUnion αᵒᵈ _ _ _ _ _ H
lemma infinite_of_not_bdd_below : ¬ bdd_below s → s.infinite :=
begin
contrapose!,
rw not_infinite,
apply finite.bdd_below,
end
end
end set
namespace finset
/-- A finset is bounded above. -/
protected lemma bdd_above [semilattice_sup α] [nonempty α] (s : finset α) :
bdd_above (↑s : set α) :=
s.finite_to_set.bdd_above
/-- A finset is bounded below. -/
protected lemma bdd_below [semilattice_inf α] [nonempty α] (s : finset α) :
bdd_below (↑s : set α) :=
s.finite_to_set.bdd_below
end finset
/--
If a set `s` does not contain any elements between any pair of elements `x, z ∈ s` with `x ≤ z`
(i.e if given `x, y, z ∈ s` such that `x ≤ y ≤ z`, then `y` is either `x` or `z`), then `s` is
finite.
-/
lemma set.finite_of_forall_between_eq_endpoints {α : Type*} [linear_order α] (s : set α)
(h : ∀ (x ∈ s) (y ∈ s) (z ∈ s), x ≤ y → y ≤ z → x = y ∨ y = z) :
set.finite s :=
begin
by_contra hinf,
change s.infinite at hinf,
rcases hinf.exists_subset_card_eq 3 with ⟨t, hts, ht⟩,
let f := t.order_iso_of_fin ht,
let x := f 0,
let y := f 1,
let z := f 2,
have := h x (hts x.2) y (hts y.2) z (hts z.2)
(f.monotone $ by dec_trivial) (f.monotone $ by dec_trivial),
have key₁ : (0 : fin 3) ≠ 1 := by dec_trivial,
have key₂ : (1 : fin 3) ≠ 2 := by dec_trivial,
cases this,
{ dsimp only [x, y] at this, exact key₁ (f.injective $ subtype.coe_injective this) },
{ dsimp only [y, z] at this, exact key₂ (f.injective $ subtype.coe_injective this) }
end
|
e554030988d525ea0073550a517ecec9c3e36a2c | 46125763b4dbf50619e8846a1371029346f4c3db | /src/topology/metric_space/isometry.lean | d710a2372a32ecd5b5ed25855c9b4367680617db | [
"Apache-2.0"
] | permissive | thjread/mathlib | a9d97612cedc2c3101060737233df15abcdb9eb1 | 7cffe2520a5518bba19227a107078d83fa725ddc | refs/heads/master | 1,615,637,696,376 | 1,583,953,063,000 | 1,583,953,063,000 | 246,680,271 | 0 | 0 | Apache-2.0 | 1,583,960,875,000 | 1,583,960,875,000 | null | UTF-8 | Lean | false | false | 13,312 | lean | /-
Copyright (c) 2018 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Isometries of emetric and metric spaces
Authors: Sébastien Gouëzel
-/
import topology.metric_space.basic
topology.bounded_continuous_function analysis.normed_space.basic topology.opens
/-!
# Isometries
We define isometries, i.e., maps between emetric spaces that preserve
the edistance (on metric spaces, these are exactly the maps that preserve distances),
and prove their basic properties. We also introduce isometric bijections.
-/
noncomputable theory
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
open function set
/-- An isometry (also known as isometric embedding) is a map preserving the edistance
between emetric spaces, or equivalently the distance between metric space. -/
def isometry [emetric_space α] [emetric_space β] (f : α → β) : Prop :=
∀x1 x2 : α, edist (f x1) (f x2) = edist x1 x2
/-- On metric spaces, a map is an isometry if and only if it preserves distances. -/
lemma isometry_emetric_iff_metric [metric_space α] [metric_space β] {f : α → β} :
isometry f ↔ (∀x y, dist (f x) (f y) = dist x y) :=
⟨assume H x y, by simp [dist_edist, H x y],
assume H x y, by simp [edist_dist, H x y]⟩
/-- An isometry preserves edistances. -/
theorem isometry.edist_eq [emetric_space α] [emetric_space β] {f : α → β} {x y : α} (hf : isometry f) :
edist (f x) (f y) = edist x y :=
hf x y
/-- An isometry preserves distances. -/
theorem isometry.dist_eq [metric_space α] [metric_space β] {f : α → β} {x y : α} (hf : isometry f) :
dist (f x) (f y) = dist x y :=
by rw [dist_edist, dist_edist, hf]
section emetric_isometry
variables [emetric_space α] [emetric_space β] [emetric_space γ]
variables {f : α → β} {x y z : α} {s : set α}
lemma isometry.lipschitz (h : isometry f) : lipschitz_with 1 f :=
lipschitz_with.of_edist_le $ λ x y, le_of_eq (h x y)
lemma isometry.antilipschitz (h : isometry f) : antilipschitz_with 1 f :=
λ x y, by simp only [h x y, ennreal.coe_one, one_mul, le_refl]
/-- An isometry is injective -/
lemma isometry.injective (h : isometry f) : injective f := h.antilipschitz.injective
/-- Any map on a subsingleton is an isometry -/
theorem isometry_subsingleton [subsingleton α] : isometry f :=
λx y, by rw subsingleton.elim x y; simp
/-- The identity is an isometry -/
lemma isometry_id : isometry (id : α → α) :=
λx y, rfl
/-- The composition of isometries is an isometry -/
theorem isometry.comp {g : β → γ} {f : α → β} (hg : isometry g) (hf : isometry f) : isometry (g ∘ f) :=
assume x y, calc
edist ((g ∘ f) x) ((g ∘ f) y) = edist (f x) (f y) : hg _ _
... = edist x y : hf _ _
/-- An isometry is an embedding -/
theorem isometry.uniform_embedding (hf : isometry f) : uniform_embedding f :=
hf.antilipschitz.uniform_embedding hf.lipschitz.uniform_continuous
/-- An isometry is continuous. -/
lemma isometry.continuous (hf : isometry f) : continuous f :=
hf.lipschitz.continuous
/-- The inverse of an isometry is an isometry. -/
lemma isometry.inv (e : α ≃ β) (h : isometry e.to_fun) : isometry e.inv_fun :=
λx y, by rw [← h, e.right_inv _, e.right_inv _]
/-- Isometries preserve the diameter in emetric spaces. -/
lemma isometry.ediam_image (hf : isometry f) (s : set α) :
emetric.diam (f '' s) = emetric.diam s :=
eq_of_forall_ge_iff $ λ d,
by simp only [emetric.diam_le_iff_forall_edist_le, ball_image_iff, hf.edist_eq]
lemma isometry.ediam_range (hf : isometry f) :
emetric.diam (range f) = emetric.diam (univ : set α) :=
by { rw ← image_univ, exact hf.ediam_image univ }
/-- The injection from a subtype is an isometry -/
lemma isometry_subtype_val {s : set α} : isometry (subtype.val : s → α) :=
λx y, rfl
end emetric_isometry --section
/-- An isometry preserves the diameter in metric spaces. -/
lemma isometry.diam_image [metric_space α] [metric_space β]
{f : α → β} (hf : isometry f) (s : set α) : metric.diam (f '' s) = metric.diam s :=
by rw [metric.diam, metric.diam, hf.ediam_image]
lemma isometry.diam_range [metric_space α] [metric_space β] {f : α → β} (hf : isometry f) :
metric.diam (range f) = metric.diam (univ : set α) :=
by { rw ← image_univ, exact hf.diam_image univ }
/-- `α` and `β` are isometric if there is an isometric bijection between them. -/
structure isometric (α : Type*) (β : Type*) [emetric_space α] [emetric_space β]
extends α ≃ β :=
(isometry_to_fun : isometry to_fun)
infix ` ≃ᵢ `:25 := isometric
namespace isometric
variables [emetric_space α] [emetric_space β] [emetric_space γ]
instance : has_coe_to_fun (α ≃ᵢ β) := ⟨λ_, α → β, λe, e.to_equiv⟩
lemma coe_eq_to_equiv (h : α ≃ᵢ β) (a : α) : h a = h.to_equiv a := rfl
lemma isometry_inv_fun (h : α ≃ᵢ β) : isometry h.to_equiv.symm :=
h.isometry_to_fun.inv h.to_equiv
/-- Alternative constructor for isometric bijections,
taking as input an isometry, and a right inverse. -/
def mk' (f : α → β) (g : β → α) (hfg : ∀ x, f (g x) = x) (hf : isometry f) : α ≃ᵢ β :=
{ to_fun := f,
inv_fun := g,
left_inv := λ x, hf.injective $ hfg _,
right_inv := hfg,
isometry_to_fun := hf }
/-- The (bundled) homeomorphism associated to an isometric isomorphism. -/
protected def to_homeomorph (h : α ≃ᵢ β) : α ≃ₜ β :=
{ continuous_to_fun := (isometry_to_fun h).continuous,
continuous_inv_fun := (isometry_inv_fun h).continuous,
.. h.to_equiv }
lemma coe_eq_to_homeomorph (h : α ≃ᵢ β) (a : α) :
h a = h.to_homeomorph a := rfl
lemma to_homeomorph_to_equiv (h : α ≃ᵢ β) :
h.to_homeomorph.to_equiv = h.to_equiv :=
by ext; refl
/-- The identity isometry of a space. -/
protected def refl (α : Type*) [emetric_space α] : α ≃ᵢ α :=
{ isometry_to_fun := isometry_id, .. equiv.refl α }
/-- The composition of two isometric isomorphisms, as an isometric isomorphism. -/
protected def trans (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) : α ≃ᵢ γ :=
{ isometry_to_fun := h₂.isometry_to_fun.comp h₁.isometry_to_fun,
.. equiv.trans h₁.to_equiv h₂.to_equiv }
/-- The inverse of an isometric isomorphism, as an isometric isomorphism. -/
protected def symm (h : α ≃ᵢ β) : β ≃ᵢ α :=
{ isometry_to_fun := h.isometry_inv_fun,
.. h.to_equiv.symm }
protected lemma isometry (h : α ≃ᵢ β) : isometry h := h.isometry_to_fun
lemma symm_comp_self (h : α ≃ᵢ β) : ⇑h.symm ∘ ⇑h = id :=
funext $ assume a, h.to_equiv.left_inv a
lemma self_comp_symm (h : α ≃ᵢ β) : ⇑h ∘ ⇑h.symm = id :=
funext $ assume a, h.to_equiv.right_inv a
lemma range_coe (h : α ≃ᵢ β) : range h = univ :=
eq_univ_of_forall $ assume b, ⟨h.symm b, congr_fun h.self_comp_symm b⟩
lemma image_symm (h : α ≃ᵢ β) : image h.symm = preimage h :=
image_eq_preimage_of_inverse h.symm.to_equiv.left_inv h.symm.to_equiv.right_inv
lemma preimage_symm (h : α ≃ᵢ β) : preimage h.symm = image h :=
(image_eq_preimage_of_inverse h.to_equiv.left_inv h.to_equiv.right_inv).symm
end isometric
/-- An isometry induces an isometric isomorphism between the source space and the
range of the isometry. -/
def isometry.isometric_on_range [emetric_space α] [emetric_space β] {f : α → β} (h : isometry f) :
α ≃ᵢ range f :=
{ isometry_to_fun := λx y,
begin
change edist ((equiv.set.range f _) x) ((equiv.set.range f _) y) = edist x y,
rw [equiv.set.range_apply f h.injective, equiv.set.range_apply f h.injective],
exact h x y
end,
.. equiv.set.range f h.injective }
lemma isometry.isometric_on_range_apply [emetric_space α] [emetric_space β]
{f : α → β} (h : isometry f) (x : α) : h.isometric_on_range x = ⟨f x, mem_range_self _⟩ :=
begin
dunfold isometry.isometric_on_range,
rw ← equiv.set.range_apply f h.injective x,
refl
end
/-- In a normed algebra, the inclusion of the base field in the extended field is an isometry. -/
lemma algebra_map_isometry (𝕜 : Type*) (𝕜' : Type*) [normed_field 𝕜] [normed_ring 𝕜']
[h : normed_algebra 𝕜 𝕜'] : isometry (@algebra_map 𝕜 𝕜' _ _ _) :=
begin
refine isometry_emetric_iff_metric.2 (λx y, _),
rw [dist_eq_norm, dist_eq_norm, ← algebra.map_sub, norm_algebra_map_eq],
end
/-- The space of bounded sequences, with its sup norm -/
@[reducible] def ℓ_infty_ℝ : Type := bounded_continuous_function ℕ ℝ
open bounded_continuous_function metric topological_space
namespace Kuratowski_embedding
/-! ### In this section, we show that any separable metric space can be embedded isometrically in ℓ^∞(ℝ) -/
variables {f g : ℓ_infty_ℝ} {n : ℕ} {C : ℝ} [metric_space α] (x : ℕ → α) (a b : α)
/-- A metric space can be embedded in `l^∞(ℝ)` via the distances to points in
a fixed countable set, if this set is dense. This map is given in the next definition,
without density assumptions. -/
def embedding_of_subset : ℓ_infty_ℝ :=
of_normed_group_discrete (λn, dist a (x n) - dist (x 0) (x n)) (dist a (x 0)) (λ_, abs_dist_sub_le _ _ _)
lemma embedding_of_subset_coe : embedding_of_subset x a n = dist a (x n) - dist (x 0) (x n) := rfl
/-- The embedding map is always a semi-contraction. -/
lemma embedding_of_subset_dist_le (a b : α) :
dist (embedding_of_subset x a) (embedding_of_subset x b) ≤ dist a b :=
begin
refine (dist_le dist_nonneg).2 (λn, _),
have A : dist a (x n) + (dist (x 0) (x n) + (-dist b (x n) + -dist (x 0) (x n)))
= dist a (x n) - dist b (x n), by ring,
simp only [embedding_of_subset_coe, real.dist_eq, A, add_comm, neg_add_rev, _root_.neg_neg,
sub_eq_add_neg, add_left_comm],
exact abs_dist_sub_le _ _ _
end
/-- When the reference set is dense, the embedding map is an isometry on its image. -/
lemma embedding_of_subset_isometry (H : closure (range x) = univ) : isometry (embedding_of_subset x) :=
begin
refine isometry_emetric_iff_metric.2 (λa b, _),
refine le_antisymm (embedding_of_subset_dist_le x a b) (real.le_of_forall_epsilon_le (λe epos, _)),
/- First step: find n with dist a (x n) < e -/
have A : a ∈ closure (range x), by { have B := mem_univ a, rwa [← H] at B },
rcases metric.mem_closure_range_iff.1 A (e/2) (half_pos epos) with ⟨n, hn⟩,
/- Second step: use the norm control at index n to conclude -/
have C : dist b (x n) - dist a (x n) = embedding_of_subset x b n - embedding_of_subset x a n :=
by { simp [embedding_of_subset_coe, sub_eq_add_neg] },
have := calc
dist a b ≤ dist a (x n) + dist (x n) b : dist_triangle _ _ _
... = 2 * dist a (x n) + (dist b (x n) - dist a (x n)) : by { simp [dist_comm], ring }
... ≤ 2 * dist a (x n) + abs (dist b (x n) - dist a (x n)) :
by apply_rules [add_le_add_left, le_abs_self]
... ≤ 2 * (e/2) + abs (embedding_of_subset x b n - embedding_of_subset x a n) :
begin rw [C], apply_rules [add_le_add, mul_le_mul_of_nonneg_left, le_of_lt hn, le_refl], norm_num end
... ≤ 2 * (e/2) + dist (embedding_of_subset x b) (embedding_of_subset x a) :
begin rw [← coe_diff], apply add_le_add_left, rw [coe_diff, ←real.dist_eq], apply dist_coe_le_dist end
... = dist (embedding_of_subset x b) (embedding_of_subset x a) + e : by ring,
simpa [dist_comm] using this
end
/-- Every separable metric space embeds isometrically in ℓ_infty_ℝ. -/
theorem exists_isometric_embedding (α : Type u) [metric_space α] [separable_space α] :
∃(f : α → ℓ_infty_ℝ), isometry f :=
begin
cases (univ : set α).eq_empty_or_nonempty with h h,
{ use (λ_, 0), assume x, exact absurd h (nonempty.ne_empty ⟨x, mem_univ x⟩) },
{ /- We construct a map x : ℕ → α with dense image -/
rcases h with basepoint,
haveI : inhabited α := ⟨basepoint⟩,
have : ∃s:set α, countable s ∧ closure s = univ := separable_space.exists_countable_closure_eq_univ _,
rcases this with ⟨S, ⟨S_countable, S_dense⟩⟩,
rcases countable_iff_exists_surjective.1 S_countable with ⟨x, x_range⟩,
have : closure (range x) = univ :=
univ_subset_iff.1 (by { rw [← S_dense], apply closure_mono, assumption }),
/- Use embedding_of_subset to construct the desired isometry -/
exact ⟨embedding_of_subset x, embedding_of_subset_isometry x this⟩ }
end
end Kuratowski_embedding
open topological_space Kuratowski_embedding
/-- The Kuratowski embedding is an isometric embedding of a separable metric space in ℓ^∞(ℝ) -/
def Kuratowski_embedding (α : Type u) [metric_space α] [separable_space α] : α → ℓ_infty_ℝ :=
classical.some (Kuratowski_embedding.exists_isometric_embedding α)
/-- The Kuratowski embedding is an isometry -/
protected lemma Kuratowski_embedding.isometry (α : Type u) [metric_space α] [separable_space α] :
isometry (Kuratowski_embedding α) :=
classical.some_spec (exists_isometric_embedding α)
/-- Version of the Kuratowski embedding for nonempty compacts -/
def nonempty_compacts.Kuratowski_embedding (α : Type u) [metric_space α] [compact_space α] [nonempty α] :
nonempty_compacts ℓ_infty_ℝ :=
⟨range (Kuratowski_embedding α), range_nonempty _,
compact_range (Kuratowski_embedding.isometry α).continuous⟩
|
4c7a5cfc176b51f896b995655d0fed4b01176274 | 130c49f47783503e462c16b2eff31933442be6ff | /src/Lean/Server/Requests.lean | 32b19be7e697e7ea39941d5ed9541d05f3061a63 | [
"Apache-2.0"
] | permissive | Hazel-Brown/lean4 | 8aa5860e282435ffc30dcdfccd34006c59d1d39c | 79e6732fc6bbf5af831b76f310f9c488d44e7a16 | refs/heads/master | 1,689,218,208,951 | 1,629,736,869,000 | 1,629,736,896,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,991 | lean | /-
Copyright (c) 2021 Wojciech Nawrocki. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Wojciech Nawrocki, Marc Huisinga
-/
import Lean.DeclarationRange
import Lean.Data.Json
import Lean.Data.Lsp
import Lean.Server.FileSource
import Lean.Server.FileWorker.Utils
import Lean.Server.Rpc.Basic
/-! We maintain a global map of LSP request handlers. This allows user code such as plugins
to register its own handlers, for example to support ITP functionality such as goal state
visualization.
For details of how to register one, see `registerLspRequestHandler`. -/
namespace Lean.Server
structure RequestError where
code : JsonRpc.ErrorCode
message : String
namespace RequestError
open JsonRpc
def fileChanged : RequestError :=
{ code := ErrorCode.contentModified
message := "File changed." }
def methodNotFound (method : String) : RequestError :=
{ code := ErrorCode.methodNotFound
message := s!"No request handler found for '{method}'" }
instance : Coe IO.Error RequestError where
coe e := { code := ErrorCode.internalError
message := toString e }
def toLspResponseError (id : RequestID) (e : RequestError) : ResponseError Unit :=
{ id := id
code := e.code
message := e.message }
end RequestError
def parseRequestParams (paramType : Type) [FromJson paramType] (params : Json)
: Except RequestError paramType :=
fromJson? params |>.mapError fun inner =>
{ code := JsonRpc.ErrorCode.parseError
message := s!"Cannot parse request params: {params.compress}\n{inner}" }
structure RequestContext where
rpcSesh : FileWorker.RpcSession
srcSearchPath : SearchPath
doc : FileWorker.EditableDocument
hLog : IO.FS.Stream
abbrev RequestTask α := Task (Except RequestError α)
/-- Workers execute request handlers in this monad. -/
abbrev RequestM := ReaderT RequestContext <| ExceptT RequestError IO
instance : Inhabited (RequestM α) :=
⟨throwThe IO.Error "executing Inhabited instance?!"⟩
instance : MonadRpcSession RequestM where
rpcSessionId := do
(←read).rpcSesh.sessionId
rpcStoreRef typeName obj := do
(←read).rpcSesh.state.modifyGet fun st => st.store typeName obj
rpcGetRef r := do
let rs ← (←read).rpcSesh.state.get
rs.aliveRefs.find? r
rpcReleaseRef r := do
(←read).rpcSesh.state.modifyGet fun st => st.release r
namespace RequestM
open FileWorker
open Snapshots
def readDoc : RequestM EditableDocument := fun rc =>
rc.doc
def asTask (t : RequestM α) : RequestM (RequestTask α) := fun rc => do
let t ← IO.asTask <| t rc
return t.map fun
| Except.error e => throwThe RequestError e
| Except.ok v => v
def mapTask (t : Task α) (f : α → RequestM β) : RequestM (RequestTask β) := fun rc => do
let t ← (IO.mapTask · t) fun a => f a rc
return t.map fun
| Except.error e => throwThe RequestError e
| Except.ok v => v
def bindTask (t : Task α) (f : α → RequestM (RequestTask β)) : RequestM (RequestTask β) := fun rc => do
let t ← IO.bindTask t fun a => do
match (← f a rc) with
| Except.error e => return Task.pure <| Except.ok <| Except.error e
| Except.ok t => return t.map Except.ok
return t.map fun
| Except.error e => throwThe RequestError e
| Except.ok v => v
/-- Create a task which waits for a snapshot matching `p`, handles various errors,
and if a matching snapshot was found executes `x` with it. If not found, the task
executes `notFoundX`. -/
def withWaitFindSnap (doc : EditableDocument) (p : Snapshot → Bool)
(notFoundX : RequestM β)
(x : Snapshot → RequestM β)
: RequestM (RequestTask β) := do
let findTask ← doc.cmdSnaps.waitFind? p
mapTask findTask fun
/- The elaboration task that we're waiting for may be aborted if the file contents change.
In that case, we reply with the `fileChanged` error. Thanks to this, the server doesn't
get bogged down in requests for an old state of the document. -/
| Except.error FileWorker.ElabTaskError.aborted =>
throwThe RequestError RequestError.fileChanged
| Except.error (FileWorker.ElabTaskError.ioError e) =>
throwThe IO.Error e
| Except.error FileWorker.ElabTaskError.eof => notFoundX
| Except.ok none => notFoundX
| Except.ok (some snap) => x snap
end RequestM
/- The global request handlers table. -/
section HandlerTable
open Lsp
private structure RequestHandler where
fileSource : Json → Except RequestError Lsp.DocumentUri
handle : Json → RequestM (RequestTask Json)
builtin_initialize requestHandlers : IO.Ref (Std.PersistentHashMap String RequestHandler) ←
IO.mkRef {}
/-- NB: This method may only be called in `initialize`/`builtin_initialize` blocks.
A registration consists of:
- a type of JSON-parsable request data `paramType`
- a `FileSource` instance for it so the system knows where to route requests
- a type of JSON-serializable response data `respType`
- an actual `handler` which runs in the `RequestM` monad and is expected
to produce an asynchronous `RequestTask` which does any waiting/computation
A handler task may be cancelled at any time, so it should check the cancellation token when possible
to handle this cooperatively. Any exceptions thrown in a request handler will be reported to the client
as LSP error responses. -/
def registerLspRequestHandler (method : String)
paramType [FromJson paramType] [FileSource paramType]
respType [ToJson respType]
(handler : paramType → RequestM (RequestTask respType)) : IO Unit := do
if !(← IO.initializing) then
throw <| IO.userError s!"Failed to register LSP request handler for '{method}': only possible during initialization"
if (←requestHandlers.get).contains method then
throw <| IO.userError s!"Failed to register LSP request handler for '{method}': already registered"
let fileSource := fun j =>
parseRequestParams paramType j |>.map Lsp.fileSource
let handle := fun j => do
let params ← parseRequestParams paramType j
let t ← handler params
t.map <| Except.map ToJson.toJson
requestHandlers.modify fun rhs => rhs.insert method { fileSource, handle }
private def lookupLspRequestHandler (method : String) : IO (Option RequestHandler) := do
(← requestHandlers.get).find? method
def routeLspRequest (method : String) (params : Json) : IO (Except RequestError DocumentUri) := do
match (← lookupLspRequestHandler method) with
| none => return Except.error <| RequestError.methodNotFound method
| some rh => return rh.fileSource params
def handleLspRequest (method : String) (params : Json) : RequestM (RequestTask Json) := do
match (← lookupLspRequestHandler method) with
| none => throwThe IO.Error <| IO.userError s!"internal server error: request '{method}' routed through watchdog but unknown in worker; are both using the same plugins?"
| some rh => rh.handle params
end HandlerTable
end Lean.Server
|
06214e95ab66e6eb3db7522e7602d69c6e37a88c | 853df553b1d6ca524e3f0a79aedd32dde5d27ec3 | /src/measure_theory/indicator_function.lean | 3675e26b53b1fecc61033cf800efa8264b368ce5 | [
"Apache-2.0"
] | permissive | DanielFabian/mathlib | efc3a50b5dde303c59eeb6353ef4c35a345d7112 | f520d07eba0c852e96fe26da71d85bf6d40fcc2a | refs/heads/master | 1,668,739,922,971 | 1,595,201,756,000 | 1,595,201,756,000 | 279,469,476 | 0 | 0 | null | 1,594,696,604,000 | 1,594,696,604,000 | null | UTF-8 | Lean | false | false | 5,627 | lean | /-
Copyright (c) 2020 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou
-/
import data.indicator_function
import measure_theory.measure_space
/-!
# Indicator function
Properties of indicator functions.
## Tags
indicator, characteristic
-/
noncomputable theory
open_locale classical
open set measure_theory filter
universes u v
variables {α : Type u} {β : Type v}
section has_zero
variables [has_zero β] {s t : set α} {f g : α → β} {a : α}
lemma indicator_congr_ae [measure_space α] (h : ∀ₘ a, a ∈ s → f a = g a) :
∀ₘ a, indicator s f a = indicator s g a :=
begin
filter_upwards [h],
simp only [mem_set_of_eq, indicator],
assume a ha,
split_ifs with h₁,
{ exact ha h₁ },
refl
end
lemma indicator_congr_of_set [measure_space α] (h : ∀ₘ a, a ∈ s ↔ a ∈ t) :
∀ₘ a, indicator s f a = indicator t f a :=
begin
filter_upwards [h],
simp only [mem_set_of_eq, indicator],
assume a ha,
split_ifs with h₁ h₂ h₂,
{ refl },
{ have := ha.1 h₁, contradiction },
{ have := ha.2 h₂, contradiction },
refl
end
end has_zero
section has_add
variables [add_monoid β] {s t : set α} {f g : α → β} {a : α}
lemma indicator_union_ae [measure_space α] {β : Type*} [add_monoid β]
(h : ∀ₘ a, a ∉ s ∩ t) (f : α → β) :
∀ₘ a, indicator (s ∪ t) f a = indicator s f a + indicator t f a :=
begin
filter_upwards [h],
simp only [mem_set_of_eq],
assume a ha,
exact indicator_union_of_not_mem_inter ha _
end
end has_add
section norm
variables [normed_group β] {s t : set α} {f g : α → β} {a : α}
lemma norm_indicator_le_of_subset (h : s ⊆ t) (f : α → β) (a : α) :
∥indicator s f a∥ ≤ ∥indicator t f a∥ :=
begin
simp only [indicator],
split_ifs with h₁ h₂,
{ refl },
{ exact absurd (h h₁) h₂ },
{ simp only [norm_zero, norm_nonneg] },
refl
end
lemma norm_indicator_le_norm_self (f : α → β) (a : α) : ∥indicator s f a∥ ≤ ∥f a∥ :=
by { convert norm_indicator_le_of_subset (subset_univ s) f a, rw indicator_univ }
lemma norm_indicator_eq_indicator_norm (f : α → β) (a : α) : ∥indicator s f a∥ = indicator s (λa, ∥f a∥) a :=
by { simp only [indicator], split_ifs, { refl }, rw norm_zero }
lemma indicator_norm_le_norm_self (f : α → β) (a : α) : indicator s (λa, ∥f a∥) a ≤ ∥f a∥ :=
by { rw ← norm_indicator_eq_indicator_norm, exact norm_indicator_le_norm_self _ _ }
end norm
section order
variables [has_zero β] [preorder β] {s t : set α} {f g : α → β} {a : α}
lemma indicator_le_indicator_ae [measure_space α] (h : ∀ₘ a, a ∈ s → f a ≤ g a) :
∀ₘ a, indicator s f a ≤ indicator s g a :=
begin
refine h.mono (λ a h, _),
simp only [indicator],
split_ifs with ha,
{ exact h ha },
refl
end
end order
lemma tendsto_indicator_of_monotone {ι} [semilattice_sup ι] [has_zero β]
(s : ι → set α) (hs : monotone s) (f : α → β) (a : α) :
tendsto (λi, indicator (s i) f a) at_top (pure $ indicator (Union s) f a) :=
begin
by_cases h : ∃i, a ∈ s i,
{ rcases h with ⟨i, hi⟩,
haveI : inhabited ι := ⟨i⟩,
simp only [tendsto_pure, eventually_at_top],
use i, assume n hn,
rw [indicator_of_mem (hs hn hi) _, indicator_of_mem ((subset_Union _ _) hi) _] },
{ rw [not_exists] at h,
have : (λi, indicator (s i) f a) = λi, 0 := funext (λi, indicator_of_not_mem (h i) _),
rw this,
have : indicator (Union s) f a = 0,
{ apply indicator_of_not_mem, simpa only [not_exists, mem_Union] },
rw this,
exact tendsto_const_pure }
end
lemma tendsto_indicator_of_antimono {ι} [semilattice_sup ι] [has_zero β]
(s : ι → set α) (hs : ∀i j, i ≤ j → s j ⊆ s i) (f : α → β) (a : α) :
tendsto (λi, indicator (s i) f a) at_top (pure $ indicator (Inter s) f a) :=
begin
by_cases h : ∃i, a ∉ s i,
{ rcases h with ⟨i, hi⟩,
haveI : inhabited ι := ⟨i⟩,
simp only [tendsto_pure, eventually_at_top],
use i, assume n hn,
rw [indicator_of_not_mem _ _, indicator_of_not_mem _ _],
{ simp only [mem_Inter, not_forall], exact ⟨i, hi⟩ },
{ assume h, have := hs i _ hn h, contradiction } },
{ simp only [not_exists, not_not_mem] at h,
have : (λi, indicator (s i) f a) = λi, f a := funext (λi, indicator_of_mem (h i) _),
rw this,
have : indicator (Inter s) f a = f a,
{ apply indicator_of_mem, simpa only [mem_Inter] },
rw this,
exact tendsto_const_pure }
end
lemma tendsto_indicator_bUnion_finset {ι} [has_zero β] (s : ι → set α) (f : α → β) (a : α) :
tendsto (λ (n : finset ι), indicator (⋃i∈n, s i) f a) at_top (pure $ indicator (Union s) f a) :=
begin
by_cases h : ∃i, a ∈ s i,
{ simp only [tendsto_pure, eventually_at_top, ge_iff_le, finset.le_iff_subset],
rcases h with ⟨i, hi⟩,
use {i}, assume n hn,
replace hn : i ∈ n := hn (finset.mem_singleton_self _),
rw [indicator_of_mem, indicator_of_mem],
{ rw [mem_Union], use i, assumption },
{ rw [mem_Union], use i, rw [mem_Union], use hn, assumption } },
{ rw [not_exists] at h,
have : (λ (n : finset ι), indicator (⋃ (i : ι) (H : i ∈ n), s i) f a) = λi, 0,
{ funext,
rw indicator_of_not_mem,
simp only [not_exists, exists_prop, mem_Union, not_and],
intros, apply h },
rw this,
have : indicator (Union s) f a = 0,
{ apply indicator_of_not_mem, simpa only [not_exists, mem_Union] },
rw this,
exact tendsto_const_pure }
end
|
2f73e2069f24d7d00f40a522e71428b320f6b204 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/sites/compatible_sheafification.lean | d6774e097c3e19db95bad0d2000fc6f67d0c26e3 | [
"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,789 | lean | /-
Copyright (c) 2021 Adam Topaz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Adam Topaz
-/
import category_theory.sites.compatible_plus
/-!
In this file, we prove that sheafification is compatible with functors which
preserve the correct limits and colimits.
-/
namespace category_theory.grothendieck_topology
open category_theory
open category_theory.limits
open opposite
universes w₁ w₂ v u
variables {C : Type u} [category.{v} C] (J : grothendieck_topology C)
variables {D : Type w₁} [category.{max v u} D]
variables {E : Type w₂} [category.{max v u} E]
variables (F : D ⥤ E)
noncomputable theory
variables [∀ (α β : Type (max v u)) (fst snd : β → α),
has_limits_of_shape (walking_multicospan fst snd) D]
variables [∀ (α β : Type (max v u)) (fst snd : β → α),
has_limits_of_shape (walking_multicospan fst snd) E]
variables [∀ (X : C), has_colimits_of_shape (J.cover X)ᵒᵖ D]
variables [∀ (X : C), has_colimits_of_shape (J.cover X)ᵒᵖ E]
variables [∀ (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ F]
variables [∀ (X : C) (W : J.cover X) (P : Cᵒᵖ ⥤ D), preserves_limit (W.index P).multicospan F]
variables (P : Cᵒᵖ ⥤ D)
/-- The isomorphism between the sheafification of `P` composed with `F` and
the sheafification of `P ⋙ F`.
Use the lemmas `whisker_right_to_sheafify_sheafify_comp_iso_hom`,
`to_sheafify_comp_sheafify_comp_iso_inv` and `sheafify_comp_iso_inv_eq_sheafify_lift` to reduce
the components of this isomorphisms to a state that can be handled using the universal property
of sheafification. -/
def sheafify_comp_iso : J.sheafify P ⋙ F ≅ J.sheafify (P ⋙ F) :=
J.plus_comp_iso _ _ ≪≫ (J.plus_functor _).map_iso (J.plus_comp_iso _ _)
/-- The isomorphism between the sheafification of `P` composed with `F` and
the sheafification of `P ⋙ F`, functorially in `F`. -/
def sheafification_whisker_left_iso (P : Cᵒᵖ ⥤ D)
[∀ (F : D ⥤ E) (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ F]
[∀ (F : D ⥤ E) (X : C) (W : J.cover X) (P : Cᵒᵖ ⥤ D),
preserves_limit (W.index P).multicospan F] :
(whiskering_left _ _ E).obj (J.sheafify P) ≅
(whiskering_left _ _ _).obj P ⋙ J.sheafification E :=
begin
refine J.plus_functor_whisker_left_iso _ ≪≫ _ ≪≫ functor.associator _ _ _,
refine iso_whisker_right _ _,
refine J.plus_functor_whisker_left_iso _,
end
@[simp]
lemma sheafification_whisker_left_iso_hom_app (P : Cᵒᵖ ⥤ D) (F : D ⥤ E)
[∀ (F : D ⥤ E) (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ F]
[∀ (F : D ⥤ E) (X : C) (W : J.cover X) (P : Cᵒᵖ ⥤ D),
preserves_limit (W.index P).multicospan F] :
(sheafification_whisker_left_iso J P).hom.app F = (J.sheafify_comp_iso F P).hom :=
begin
dsimp [sheafification_whisker_left_iso, sheafify_comp_iso],
rw category.comp_id,
end
@[simp]
lemma sheafification_whisker_left_iso_inv_app (P : Cᵒᵖ ⥤ D) (F : D ⥤ E)
[∀ (F : D ⥤ E) (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ F]
[∀ (F : D ⥤ E) (X : C) (W : J.cover X) (P : Cᵒᵖ ⥤ D),
preserves_limit (W.index P).multicospan F] :
(sheafification_whisker_left_iso J P).inv.app F = (J.sheafify_comp_iso F P).inv :=
begin
dsimp [sheafification_whisker_left_iso, sheafify_comp_iso],
erw category.id_comp,
end
/-- The isomorphism between the sheafification of `P` composed with `F` and
the sheafification of `P ⋙ F`, functorially in `P`. -/
def sheafification_whisker_right_iso :
J.sheafification D ⋙ (whiskering_right _ _ _).obj F ≅
(whiskering_right _ _ _).obj F ⋙ J.sheafification E :=
begin
refine functor.associator _ _ _ ≪≫ _,
refine iso_whisker_left (J.plus_functor D) (J.plus_functor_whisker_right_iso _) ≪≫ _,
refine _ ≪≫ functor.associator _ _ _,
refine (functor.associator _ _ _).symm ≪≫ _,
exact iso_whisker_right (J.plus_functor_whisker_right_iso _) (J.plus_functor E),
end
@[simp]
lemma sheafification_whisker_right_iso_hom_app :
(J.sheafification_whisker_right_iso F).hom.app P = (J.sheafify_comp_iso F P).hom :=
begin
dsimp [sheafification_whisker_right_iso, sheafify_comp_iso],
simp only [category.id_comp, category.comp_id],
erw category.id_comp,
end
@[simp]
lemma sheafification_whisker_right_iso_inv_app :
(J.sheafification_whisker_right_iso F).inv.app P = (J.sheafify_comp_iso F P).inv :=
begin
dsimp [sheafification_whisker_right_iso, sheafify_comp_iso],
simp only [category.id_comp, category.comp_id],
erw category.id_comp,
end
@[simp, reassoc]
lemma whisker_right_to_sheafify_sheafify_comp_iso_hom :
whisker_right (J.to_sheafify _) _ ≫ (J.sheafify_comp_iso F P).hom = J.to_sheafify _ :=
begin
dsimp [sheafify_comp_iso],
erw [whisker_right_comp, category.assoc],
slice_lhs 2 3 { rw plus_comp_iso_whisker_right },
rw [category.assoc, ← J.plus_map_comp,
whisker_right_to_plus_comp_plus_comp_iso_hom, ← category.assoc,
whisker_right_to_plus_comp_plus_comp_iso_hom],
refl,
end
@[simp, reassoc]
lemma to_sheafify_comp_sheafify_comp_iso_inv :
J.to_sheafify _ ≫ (J.sheafify_comp_iso F P).inv = whisker_right (J.to_sheafify _) _ :=
by { rw iso.comp_inv_eq, simp }
section
-- We will sheafify `D`-valued presheaves in this section.
variables
[concrete_category.{max v u} D]
[preserves_limits (forget D)]
[∀ (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ (forget D)]
[reflects_isomorphisms (forget D)]
@[simp]
lemma sheafify_comp_iso_inv_eq_sheafify_lift : (J.sheafify_comp_iso F P).inv =
J.sheafify_lift (whisker_right (J.to_sheafify _) _) ((J.sheafify_is_sheaf _).comp _) :=
begin
apply J.sheafify_lift_unique,
rw iso.comp_inv_eq,
simp,
end
end
end category_theory.grothendieck_topology
|
e37f931c1d50c54f40937d7790bec51a77ce8630 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/linear_algebra/multilinear/basic.lean | e3d22caa099e20d79c92978fd37dd0a8d048b35b | [
"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 | 58,920 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import linear_algebra.basic
import algebra.algebra.basic
import algebra.big_operators.order
import algebra.big_operators.ring
import data.fintype.big_operators
import data.fintype.sort
/-!
# Multilinear maps
We define multilinear maps as maps from `Π(i : ι), M₁ i` to `M₂` which are linear in each
coordinate. Here, `M₁ i` and `M₂` are modules over a ring `R`, and `ι` is an arbitrary type
(although some statements will require it to be a fintype). This space, denoted by
`multilinear_map R M₁ M₂`, inherits a module structure by pointwise addition and multiplication.
## Main definitions
* `multilinear_map R M₁ M₂` is the space of multilinear maps from `Π(i : ι), M₁ i` to `M₂`.
* `f.map_smul` is the multiplicativity of the multilinear map `f` along each coordinate.
* `f.map_add` is the additivity of the multilinear map `f` along each coordinate.
* `f.map_smul_univ` expresses the multiplicativity of `f` over all coordinates at the same time,
writing `f (λi, c i • m i)` as `(∏ i, c i) • f m`.
* `f.map_add_univ` expresses the additivity of `f` over all coordinates at the same time, writing
`f (m + m')` as the sum over all subsets `s` of `ι` of `f (s.piecewise m m')`.
* `f.map_sum` expresses `f (Σ_{j₁} g₁ j₁, ..., Σ_{jₙ} gₙ jₙ)` as the sum of
`f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all possible functions.
We also register isomorphisms corresponding to currying or uncurrying variables, transforming a
multilinear function `f` on `n+1` variables into a linear function taking values in multilinear
functions in `n` variables, and into a multilinear function in `n` variables taking values in linear
functions. These operations are called `f.curry_left` and `f.curry_right` respectively
(with inverses `f.uncurry_left` and `f.uncurry_right`). These operations induce linear equivalences
between spaces of multilinear functions in `n+1` variables and spaces of linear functions into
multilinear functions in `n` variables (resp. multilinear functions in `n` variables taking values
in linear functions), called respectively `multilinear_curry_left_equiv` and
`multilinear_curry_right_equiv`.
## Implementation notes
Expressing that a map is linear along the `i`-th coordinate when all other coordinates are fixed
can be done in two (equivalent) different ways:
* fixing a vector `m : Π(j : ι - i), M₁ j.val`, and then choosing separately the `i`-th coordinate
* fixing a vector `m : Πj, M₁ j`, and then modifying its `i`-th coordinate
The second way is more artificial as the value of `m` at `i` is not relevant, but it has the
advantage of avoiding subtype inclusion issues. This is the definition we use, based on
`function.update` that allows to change the value of `m` at `i`.
-/
open function fin set
open_locale big_operators
universes u v v' v₁ v₂ v₃ w u'
variables {R : Type u} {ι : Type u'} {n : ℕ}
{M : fin n.succ → Type v} {M₁ : ι → Type v₁} {M₂ : Type v₂} {M₃ : Type v₃} {M' : Type v'}
[decidable_eq ι]
/-- Multilinear maps over the ring `R`, from `Πi, M₁ i` to `M₂` where `M₁ i` and `M₂` are modules
over `R`. -/
structure multilinear_map (R : Type u) {ι : Type u'} (M₁ : ι → Type v) (M₂ : Type w)
[decidable_eq ι] [semiring R] [∀i, add_comm_monoid (M₁ i)] [add_comm_monoid M₂]
[∀i, module R (M₁ i)] [module R M₂] :=
(to_fun : (Πi, M₁ i) → M₂)
(map_add' : ∀(m : Πi, M₁ i) (i : ι) (x y : M₁ i),
to_fun (update m i (x + y)) = to_fun (update m i x) + to_fun (update m i y))
(map_smul' : ∀(m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i),
to_fun (update m i (c • x)) = c • to_fun (update m i x))
namespace multilinear_map
section semiring
variables [semiring R]
[∀i, add_comm_monoid (M i)] [∀i, add_comm_monoid (M₁ i)] [add_comm_monoid M₂] [add_comm_monoid M₃]
[add_comm_monoid M']
[∀i, module R (M i)] [∀i, module R (M₁ i)] [module R M₂] [module R M₃]
[module R M']
(f f' : multilinear_map R M₁ M₂)
instance : has_coe_to_fun (multilinear_map R M₁ M₂) (λ f, (Πi, M₁ i) → M₂) := ⟨to_fun⟩
initialize_simps_projections multilinear_map (to_fun → apply)
@[simp] lemma to_fun_eq_coe : f.to_fun = f := rfl
@[simp] lemma coe_mk (f : (Π i, M₁ i) → M₂) (h₁ h₂ ) :
⇑(⟨f, h₁, h₂⟩ : multilinear_map R M₁ M₂) = f := rfl
theorem congr_fun {f g : multilinear_map R M₁ M₂} (h : f = g) (x : Π i, M₁ i) : f x = g x :=
congr_arg (λ h : multilinear_map R M₁ M₂, h x) h
theorem congr_arg (f : multilinear_map R M₁ M₂) {x y : Π i, M₁ i} (h : x = y) : f x = f y :=
congr_arg (λ x : Π i, M₁ i, f x) h
theorem coe_injective : injective (coe_fn : multilinear_map R M₁ M₂ → ((Π i, M₁ i) → M₂)) :=
by { intros f g h, cases f, cases g, cases h, refl }
@[simp, norm_cast] theorem coe_inj {f g : multilinear_map R M₁ M₂} :
(f : (Π i, M₁ i) → M₂) = g ↔ f = g :=
coe_injective.eq_iff
@[ext] theorem ext {f f' : multilinear_map R M₁ M₂} (H : ∀ x, f x = f' x) : f = f' :=
coe_injective (funext H)
theorem ext_iff {f g : multilinear_map R M₁ M₂} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, ext h⟩
@[simp] lemma mk_coe (f : multilinear_map R M₁ M₂) (h₁ h₂) :
(⟨f, h₁, h₂⟩ : multilinear_map R M₁ M₂) = f :=
by { ext, refl, }
@[simp] protected lemma map_add (m : Πi, M₁ i) (i : ι) (x y : M₁ i) :
f (update m i (x + y)) = f (update m i x) + f (update m i y) :=
f.map_add' m i x y
@[simp] protected lemma map_smul (m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i) :
f (update m i (c • x)) = c • f (update m i x) :=
f.map_smul' m i c x
lemma map_coord_zero {m : Πi, M₁ i} (i : ι) (h : m i = 0) : f m = 0 :=
begin
have : (0 : R) • (0 : M₁ i) = 0, by simp,
rw [← update_eq_self i m, h, ← this, f.map_smul, zero_smul]
end
@[simp] lemma map_update_zero (m : Πi, M₁ i) (i : ι) : f (update m i 0) = 0 :=
f.map_coord_zero i (update_same i 0 m)
@[simp] lemma map_zero [nonempty ι] : f 0 = 0 :=
begin
obtain ⟨i, _⟩ : ∃i:ι, i ∈ set.univ := set.exists_mem_of_nonempty ι,
exact map_coord_zero f i rfl
end
instance : has_add (multilinear_map R M₁ M₂) :=
⟨λf f', ⟨λx, f x + f' x, λm i x y, by simp [add_left_comm, add_assoc],
λm i c x, by simp [smul_add]⟩⟩
@[simp] lemma add_apply (m : Πi, M₁ i) : (f + f') m = f m + f' m := rfl
instance : has_zero (multilinear_map R M₁ M₂) :=
⟨⟨λ _, 0, λm i x y, by simp, λm i c x, by simp⟩⟩
instance : inhabited (multilinear_map R M₁ M₂) := ⟨0⟩
@[simp] lemma zero_apply (m : Πi, M₁ i) : (0 : multilinear_map R M₁ M₂) m = 0 := rfl
section has_smul
variables {R' A : Type*} [monoid R'] [semiring A]
[Π i, module A (M₁ i)] [distrib_mul_action R' M₂] [module A M₂] [smul_comm_class A R' M₂]
instance : has_smul R' (multilinear_map A M₁ M₂) := ⟨λ c f,
⟨λ m, c • f m, λm i x y, by simp [smul_add], λl i x d, by simp [←smul_comm x c] ⟩⟩
@[simp] lemma smul_apply (f : multilinear_map A M₁ M₂) (c : R') (m : Πi, M₁ i) :
(c • f) m = c • f m := rfl
lemma coe_smul (c : R') (f : multilinear_map A M₁ M₂) : ⇑(c • f) = c • f :=
rfl
end has_smul
instance : add_comm_monoid (multilinear_map R M₁ M₂) :=
coe_injective.add_comm_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl)
@[simp] lemma sum_apply {α : Type*} (f : α → multilinear_map R M₁ M₂)
(m : Πi, M₁ i) : ∀ {s : finset α}, (∑ a in s, f a) m = ∑ a in s, f a m :=
begin
classical,
apply finset.induction,
{ rw finset.sum_empty, simp },
{ assume a s has H, rw finset.sum_insert has, simp [H, has] }
end
/-- If `f` is a multilinear map, then `f.to_linear_map m i` is the linear map obtained by fixing all
coordinates but `i` equal to those of `m`, and varying the `i`-th coordinate. -/
@[simps] def to_linear_map (m : Πi, M₁ i) (i : ι) : M₁ i →ₗ[R] M₂ :=
{ to_fun := λx, f (update m i x),
map_add' := λx y, by simp,
map_smul' := λc x, by simp }
/-- The cartesian product of two multilinear maps, as a multilinear map. -/
def prod (f : multilinear_map R M₁ M₂) (g : multilinear_map R M₁ M₃) :
multilinear_map R M₁ (M₂ × M₃) :=
{ to_fun := λ m, (f m, g m),
map_add' := λ m i x y, by simp,
map_smul' := λ m i c x, by simp }
/-- Combine a family of multilinear maps with the same domain and codomains `M' i` into a
multilinear map taking values in the space of functions `Π i, M' i`. -/
@[simps] def pi {ι' : Type*} {M' : ι' → Type*} [Π i, add_comm_monoid (M' i)]
[Π i, module R (M' i)] (f : Π i, multilinear_map R M₁ (M' i)) :
multilinear_map R M₁ (Π i, M' i) :=
{ to_fun := λ m i, f i m,
map_add' := λ m i x y, funext $ λ j, (f j).map_add _ _ _ _,
map_smul' := λ m i c x, funext $ λ j, (f j).map_smul _ _ _ _ }
section
variables (R M₂)
/-- The evaluation map from `ι → M₂` to `M₂` is multilinear at a given `i` when `ι` is subsingleton.
-/
@[simps]
def of_subsingleton [subsingleton ι] (i' : ι) : multilinear_map R (λ _ : ι, M₂) M₂ :=
{ to_fun := function.eval i',
map_add' := λ m i x y, by
{ rw subsingleton.elim i i', simp only [function.eval, function.update_same], },
map_smul' := λ m i r x, by
{ rw subsingleton.elim i i', simp only [function.eval, function.update_same], } }
variables {M₂}
/-- The constant map is multilinear when `ι` is empty. -/
@[simps {fully_applied := ff}]
def const_of_is_empty [is_empty ι] (m : M₂) : multilinear_map R M₁ M₂ :=
{ to_fun := function.const _ m,
map_add' := λ m, is_empty_elim,
map_smul' := λ m, is_empty_elim }
end
/-- Given a multilinear map `f` on `n` variables (parameterized by `fin n`) and a subset `s` of `k`
of these variables, one gets a new multilinear map on `fin k` by varying these variables, and fixing
the other ones equal to a given value `z`. It is denoted by `f.restr s hk z`, where `hk` is a
proof that the cardinality of `s` is `k`. The implicit identification between `fin k` and `s` that
we use is the canonical (increasing) bijection. -/
def restr {k n : ℕ} (f : multilinear_map R (λ i : fin n, M') M₂) (s : finset (fin n))
(hk : s.card = k) (z : M') :
multilinear_map R (λ i : fin k, M') M₂ :=
{ to_fun := λ v, f (λ j, if h : j ∈ s then v ((s.order_iso_of_fin hk).symm ⟨j, h⟩) else z),
map_add' := λ v i x y,
by { erw [dite_comp_equiv_update, dite_comp_equiv_update, dite_comp_equiv_update], simp },
map_smul' := λ v i c x, by { erw [dite_comp_equiv_update, dite_comp_equiv_update], simp } }
variable {R}
/-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build
an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the additivity of a
multilinear map along the first variable. -/
lemma cons_add (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (x y : M 0) :
f (cons (x+y) m) = f (cons x m) + f (cons y m) :=
by rw [← update_cons_zero x m (x+y), f.map_add, update_cons_zero, update_cons_zero]
/-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build
an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the multiplicativity
of a multilinear map along the first variable. -/
lemma cons_smul (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (c : R) (x : M 0) :
f (cons (c • x) m) = c • f (cons x m) :=
by rw [← update_cons_zero x m (c • x), f.map_smul, update_cons_zero]
/-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build
an element of `Π(i : fin (n+1)), M i` using `snoc`, one can express directly the additivity of a
multilinear map along the first variable. -/
lemma snoc_add (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.cast_succ) (x y : M (last n)) :
f (snoc m (x+y)) = f (snoc m x) + f (snoc m y) :=
by rw [← update_snoc_last x m (x+y), f.map_add, update_snoc_last, update_snoc_last]
/-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build
an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the multiplicativity
of a multilinear map along the first variable. -/
lemma snoc_smul (f : multilinear_map R M M₂)
(m : Π(i : fin n), M i.cast_succ) (c : R) (x : M (last n)) :
f (snoc m (c • x)) = c • f (snoc m x) :=
by rw [← update_snoc_last x m (c • x), f.map_smul, update_snoc_last]
section
variables {M₁' : ι → Type*} [Π i, add_comm_monoid (M₁' i)] [Π i, module R (M₁' i)]
variables {M₁'' : ι → Type*} [Π i, add_comm_monoid (M₁'' i)] [Π i, module R (M₁'' i)]
/-- If `g` is a multilinear map and `f` is a collection of linear maps,
then `g (f₁ m₁, ..., fₙ mₙ)` is again a multilinear map, that we call
`g.comp_linear_map f`. -/
def comp_linear_map (g : multilinear_map R M₁' M₂) (f : Π i, M₁ i →ₗ[R] M₁' i) :
multilinear_map R M₁ M₂ :=
{ to_fun := λ m, g $ λ i, f i (m i),
map_add' := λ m i x y,
have ∀ j z, f j (update m i z j) = update (λ k, f k (m k)) i (f i z) j :=
λ j z, function.apply_update (λ k, f k) _ _ _ _,
by simp [this],
map_smul' := λ m i c x,
have ∀ j z, f j (update m i z j) = update (λ k, f k (m k)) i (f i z) j :=
λ j z, function.apply_update (λ k, f k) _ _ _ _,
by simp [this] }
@[simp] lemma comp_linear_map_apply (g : multilinear_map R M₁' M₂) (f : Π i, M₁ i →ₗ[R] M₁' i)
(m : Π i, M₁ i) :
g.comp_linear_map f m = g (λ i, f i (m i)) :=
rfl
/-- Composing a multilinear map twice with a linear map in each argument is
the same as composing with their composition. -/
lemma comp_linear_map_assoc (g : multilinear_map R M₁'' M₂) (f₁ : Π i, M₁' i →ₗ[R] M₁'' i)
(f₂ : Π i, M₁ i →ₗ[R] M₁' i) :
(g.comp_linear_map f₁).comp_linear_map f₂ = g.comp_linear_map (λ i, f₁ i ∘ₗ f₂ i) :=
rfl
/-- Composing the zero multilinear map with a linear map in each argument. -/
@[simp] lemma zero_comp_linear_map (f : Π i, M₁ i →ₗ[R] M₁' i) :
(0 : multilinear_map R M₁' M₂).comp_linear_map f = 0 :=
ext $ λ _, rfl
/-- Composing a multilinear map with the identity linear map in each argument. -/
@[simp] lemma comp_linear_map_id (g : multilinear_map R M₁' M₂) :
g.comp_linear_map (λ i, linear_map.id) = g :=
ext $ λ _, rfl
/-- Composing with a family of surjective linear maps is injective. -/
lemma comp_linear_map_injective (f : Π i, M₁ i →ₗ[R] M₁' i) (hf : ∀ i, surjective (f i)) :
injective (λ g : multilinear_map R M₁' M₂, g.comp_linear_map f) :=
λ g₁ g₂ h, ext $ λ x,
by simpa [λ i, surj_inv_eq (hf i)] using ext_iff.mp h (λ i, surj_inv (hf i) (x i))
lemma comp_linear_map_inj (f : Π i, M₁ i →ₗ[R] M₁' i) (hf : ∀ i, surjective (f i))
(g₁ g₂ : multilinear_map R M₁' M₂) : g₁.comp_linear_map f = g₂.comp_linear_map f ↔ g₁ = g₂ :=
(comp_linear_map_injective _ hf).eq_iff
/-- Composing a multilinear map with a linear equiv on each argument gives the zero map
if and only if the multilinear map is the zero map. -/
@[simp] lemma comp_linear_equiv_eq_zero_iff (g : multilinear_map R M₁' M₂)
(f : Π i, M₁ i ≃ₗ[R] M₁' i) : g.comp_linear_map (λ i, (f i : M₁ i →ₗ[R] M₁' i)) = 0 ↔ g = 0 :=
begin
set f' := (λ i, (f i : M₁ i →ₗ[R] M₁' i)),
rw [←zero_comp_linear_map f', comp_linear_map_inj f' (λ i, (f i).surjective)],
end
end
/-- If one adds to a vector `m'` another vector `m`, but only for coordinates in a finset `t`, then
the image under a multilinear map `f` is the sum of `f (s.piecewise m m')` along all subsets `s` of
`t`. This is mainly an auxiliary statement to prove the result when `t = univ`, given in
`map_add_univ`, although it can be useful in its own right as it does not require the index set `ι`
to be finite.-/
lemma map_piecewise_add (m m' : Πi, M₁ i) (t : finset ι) :
f (t.piecewise (m + m') m') = ∑ s in t.powerset, f (s.piecewise m m') :=
begin
revert m',
refine finset.induction_on t (by simp) _,
assume i t hit Hrec m',
have A : (insert i t).piecewise (m + m') m' = update (t.piecewise (m + m') m') i (m i + m' i) :=
t.piecewise_insert _ _ _,
have B : update (t.piecewise (m + m') m') i (m' i) = t.piecewise (m + m') m',
{ ext j,
by_cases h : j = i,
{ rw h, simp [hit] },
{ simp [h] } },
let m'' := update m' i (m i),
have C : update (t.piecewise (m + m') m') i (m i) = t.piecewise (m + m'') m'',
{ ext j,
by_cases h : j = i,
{ rw h, simp [m'', hit] },
{ by_cases h' : j ∈ t; simp [h, hit, m'', h'] } },
rw [A, f.map_add, B, C, finset.sum_powerset_insert hit, Hrec, Hrec, add_comm],
congr' 1,
apply finset.sum_congr rfl (λs hs, _),
have : (insert i s).piecewise m m' = s.piecewise m m'',
{ ext j,
by_cases h : j = i,
{ rw h, simp [m'', finset.not_mem_of_mem_powerset_of_not_mem hs hit] },
{ by_cases h' : j ∈ s; simp [h, m'', h'] } },
rw this
end
/-- Additivity of a multilinear map along all coordinates at the same time,
writing `f (m + m')` as the sum of `f (s.piecewise m m')` over all sets `s`. -/
lemma map_add_univ [fintype ι] (m m' : Πi, M₁ i) :
f (m + m') = ∑ s : finset ι, f (s.piecewise m m') :=
by simpa using f.map_piecewise_add m m' finset.univ
section apply_sum
variables {α : ι → Type*} (g : Π i, α i → M₁ i) (A : Π i, finset (α i))
open_locale classical
open fintype finset
/-- If `f` is multilinear, then `f (Σ_{j₁ ∈ A₁} g₁ j₁, ..., Σ_{jₙ ∈ Aₙ} gₙ jₙ)` is the sum of
`f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions with `r 1 ∈ A₁`, ...,
`r n ∈ Aₙ`. This follows from multilinearity by expanding successively with respect to each
coordinate. Here, we give an auxiliary statement tailored for an inductive proof. Use instead
`map_sum_finset`. -/
lemma map_sum_finset_aux [fintype ι] {n : ℕ} (h : ∑ i, (A i).card = n) :
f (λ i, ∑ j in A i, g i j) = ∑ r in pi_finset A, f (λ i, g i (r i)) :=
begin
induction n using nat.strong_induction_on with n IH generalizing A,
-- If one of the sets is empty, then all the sums are zero
by_cases Ai_empty : ∃ i, A i = ∅,
{ rcases Ai_empty with ⟨i, hi⟩,
have : ∑ j in A i, g i j = 0, by rw [hi, finset.sum_empty],
rw f.map_coord_zero i this,
have : pi_finset A = ∅,
{ apply finset.eq_empty_of_forall_not_mem (λ r hr, _),
have : r i ∈ A i := mem_pi_finset.mp hr i,
rwa hi at this },
rw [this, finset.sum_empty] },
push_neg at Ai_empty,
-- Otherwise, if all sets are at most singletons, then they are exactly singletons and the result
-- is again straightforward
by_cases Ai_singleton : ∀ i, (A i).card ≤ 1,
{ have Ai_card : ∀ i, (A i).card = 1,
{ assume i,
have pos : finset.card (A i) ≠ 0, by simp [finset.card_eq_zero, Ai_empty i],
have : finset.card (A i) ≤ 1 := Ai_singleton i,
exact le_antisymm this (nat.succ_le_of_lt (_root_.pos_iff_ne_zero.mpr pos)) },
have : ∀ (r : Π i, α i), r ∈ pi_finset A → f (λ i, g i (r i)) = f (λ i, ∑ j in A i, g i j),
{ assume r hr,
unfold_coes,
congr' with i,
have : ∀ j ∈ A i, g i j = g i (r i),
{ assume j hj,
congr,
apply finset.card_le_one_iff.1 (Ai_singleton i) hj,
exact mem_pi_finset.mp hr i },
simp only [finset.sum_congr rfl this, finset.mem_univ, finset.sum_const, Ai_card i,
one_nsmul] },
simp only [sum_congr rfl this, Ai_card, card_pi_finset, prod_const_one, one_nsmul,
finset.sum_const] },
-- Remains the interesting case where one of the `A i`, say `A i₀`, has cardinality at least 2.
-- We will split into two parts `B i₀` and `C i₀` of smaller cardinality, let `B i = C i = A i`
-- for `i ≠ i₀`, apply the inductive assumption to `B` and `C`, and add up the corresponding
-- parts to get the sum for `A`.
push_neg at Ai_singleton,
obtain ⟨i₀, hi₀⟩ : ∃ i, 1 < (A i).card := Ai_singleton,
obtain ⟨j₁, j₂, hj₁, hj₂, j₁_ne_j₂⟩ : ∃ j₁ j₂, (j₁ ∈ A i₀) ∧ (j₂ ∈ A i₀) ∧ j₁ ≠ j₂ :=
finset.one_lt_card_iff.1 hi₀,
let B := function.update A i₀ (A i₀ \ {j₂}),
let C := function.update A i₀ {j₂},
have B_subset_A : ∀ i, B i ⊆ A i,
{ assume i,
by_cases hi : i = i₀,
{ rw hi, simp only [B, sdiff_subset, update_same]},
{ simp only [hi, B, update_noteq, ne.def, not_false_iff, finset.subset.refl] } },
have C_subset_A : ∀ i, C i ⊆ A i,
{ assume i,
by_cases hi : i = i₀,
{ rw hi, simp only [C, hj₂, finset.singleton_subset_iff, update_same] },
{ simp only [hi, C, update_noteq, ne.def, not_false_iff, finset.subset.refl] } },
-- split the sum at `i₀` as the sum over `B i₀` plus the sum over `C i₀`, to use additivity.
have A_eq_BC : (λ i, ∑ j in A i, g i j) =
function.update (λ i, ∑ j in A i, g i j) i₀ (∑ j in B i₀, g i₀ j + ∑ j in C i₀, g i₀ j),
{ ext i,
by_cases hi : i = i₀,
{ rw [hi],
simp only [function.update_same],
have : A i₀ = B i₀ ∪ C i₀,
{ simp only [B, C, function.update_same, finset.sdiff_union_self_eq_union],
symmetry,
simp only [hj₂, finset.singleton_subset_iff, finset.union_eq_left_iff_subset] },
rw this,
apply finset.sum_union,
apply finset.disjoint_right.2 (λ j hj, _),
have : j = j₂, by { dsimp [C] at hj, simpa using hj },
rw this,
dsimp [B],
simp only [mem_sdiff, eq_self_iff_true, not_true, not_false_iff, finset.mem_singleton,
update_same, and_false] },
{ simp [hi] } },
have Beq : function.update (λ i, ∑ j in A i, g i j) i₀ (∑ j in B i₀, g i₀ j) =
(λ i, ∑ j in B i, g i j),
{ ext i,
by_cases hi : i = i₀,
{ rw hi, simp only [update_same] },
{ simp only [hi, B, update_noteq, ne.def, not_false_iff] } },
have Ceq : function.update (λ i, ∑ j in A i, g i j) i₀ (∑ j in C i₀, g i₀ j) =
(λ i, ∑ j in C i, g i j),
{ ext i,
by_cases hi : i = i₀,
{ rw hi, simp only [update_same] },
{ simp only [hi, C, update_noteq, ne.def, not_false_iff] } },
-- Express the inductive assumption for `B`
have Brec : f (λ i, ∑ j in B i, g i j) = ∑ r in pi_finset B, f (λ i, g i (r i)),
{ have : ∑ i, finset.card (B i) < ∑ i, finset.card (A i),
{ refine finset.sum_lt_sum (λ i hi, finset.card_le_of_subset (B_subset_A i))
⟨i₀, finset.mem_univ _, _⟩,
have : {j₂} ⊆ A i₀, by simp [hj₂],
simp only [B, finset.card_sdiff this, function.update_same, finset.card_singleton],
exact nat.pred_lt (ne_of_gt (lt_trans nat.zero_lt_one hi₀)) },
rw h at this,
exact IH _ this B rfl },
-- Express the inductive assumption for `C`
have Crec : f (λ i, ∑ j in C i, g i j) = ∑ r in pi_finset C, f (λ i, g i (r i)),
{ have : ∑ i, finset.card (C i) < ∑ i, finset.card (A i) :=
finset.sum_lt_sum (λ i hi, finset.card_le_of_subset (C_subset_A i))
⟨i₀, finset.mem_univ _, by simp [C, hi₀]⟩,
rw h at this,
exact IH _ this C rfl },
have D : disjoint (pi_finset B) (pi_finset C),
{ have : disjoint (B i₀) (C i₀), by simp [B, C],
exact pi_finset_disjoint_of_disjoint B C this },
have pi_BC : pi_finset A = pi_finset B ∪ pi_finset C,
{ apply finset.subset.antisymm,
{ assume r hr,
by_cases hri₀ : r i₀ = j₂,
{ apply finset.mem_union_right,
apply mem_pi_finset.2 (λ i, _),
by_cases hi : i = i₀,
{ have : r i₀ ∈ C i₀, by simp [C, hri₀],
convert this },
{ simp [C, hi, mem_pi_finset.1 hr i] } },
{ apply finset.mem_union_left,
apply mem_pi_finset.2 (λ i, _),
by_cases hi : i = i₀,
{ have : r i₀ ∈ B i₀,
by simp [B, hri₀, mem_pi_finset.1 hr i₀],
convert this },
{ simp [B, hi, mem_pi_finset.1 hr i] } } },
{ exact finset.union_subset (pi_finset_subset _ _ (λ i, B_subset_A i))
(pi_finset_subset _ _ (λ i, C_subset_A i)) } },
rw A_eq_BC,
simp only [multilinear_map.map_add, Beq, Ceq, Brec, Crec, pi_BC],
rw ← finset.sum_union D,
end
/-- If `f` is multilinear, then `f (Σ_{j₁ ∈ A₁} g₁ j₁, ..., Σ_{jₙ ∈ Aₙ} gₙ jₙ)` is the sum of
`f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions with `r 1 ∈ A₁`, ...,
`r n ∈ Aₙ`. This follows from multilinearity by expanding successively with respect to each
coordinate. -/
lemma map_sum_finset [fintype ι] :
f (λ i, ∑ j in A i, g i j) = ∑ r in pi_finset A, f (λ i, g i (r i)) :=
f.map_sum_finset_aux _ _ rfl
/-- If `f` is multilinear, then `f (Σ_{j₁} g₁ j₁, ..., Σ_{jₙ} gₙ jₙ)` is the sum of
`f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions `r`. This follows from
multilinearity by expanding successively with respect to each coordinate. -/
lemma map_sum [fintype ι] [∀ i, fintype (α i)] :
f (λ i, ∑ j, g i j) = ∑ r : Π i, α i, f (λ i, g i (r i)) :=
f.map_sum_finset g (λ i, finset.univ)
lemma map_update_sum {α : Type*} (t : finset α) (i : ι) (g : α → M₁ i) (m : Π i, M₁ i):
f (update m i (∑ a in t, g a)) = ∑ a in t, f (update m i (g a)) :=
begin
induction t using finset.induction with a t has ih h,
{ simp },
{ simp [finset.sum_insert has, ih] }
end
end apply_sum
/-- Restrict the codomain of a multilinear map to a submodule.
This is the multilinear version of `linear_map.cod_restrict`. -/
@[simps]
def cod_restrict (f : multilinear_map R M₁ M₂) (p : submodule R M₂) (h : ∀ v, f v ∈ p) :
multilinear_map R M₁ p :=
{ to_fun := λ v, ⟨f v, h v⟩,
map_add' := λ v i x y, subtype.ext $ multilinear_map.map_add _ _ _ _ _,
map_smul' := λ v i c x, subtype.ext $ multilinear_map.map_smul _ _ _ _ _ }
section restrict_scalar
variables (R) {A : Type*} [semiring A] [has_smul R A] [Π (i : ι), module A (M₁ i)]
[module A M₂] [∀ i, is_scalar_tower R A (M₁ i)] [is_scalar_tower R A M₂]
/-- Reinterpret an `A`-multilinear map as an `R`-multilinear map, if `A` is an algebra over `R`
and their actions on all involved modules agree with the action of `R` on `A`. -/
def restrict_scalars (f : multilinear_map A M₁ M₂) : multilinear_map R M₁ M₂ :=
{ to_fun := f,
map_add' := f.map_add,
map_smul' := λ m i, (f.to_linear_map m i).map_smul_of_tower }
@[simp] lemma coe_restrict_scalars (f : multilinear_map A M₁ M₂) :
⇑(f.restrict_scalars R) = f := rfl
end restrict_scalar
section
variables {ι₁ ι₂ ι₃ : Type*} [decidable_eq ι₁] [decidable_eq ι₂] [decidable_eq ι₃]
/-- Transfer the arguments to a map along an equivalence between argument indices.
The naming is derived from `finsupp.dom_congr`, noting that here the permutation applies to the
domain of the domain. -/
@[simps apply]
def dom_dom_congr (σ : ι₁ ≃ ι₂) (m : multilinear_map R (λ i : ι₁, M₂) M₃) :
multilinear_map R (λ i : ι₂, M₂) M₃ :=
{ to_fun := λ v, m (λ i, v (σ i)),
map_add' := λ v i a b, by { simp_rw function.update_apply_equiv_apply v, rw m.map_add, },
map_smul' := λ v i a b, by { simp_rw function.update_apply_equiv_apply v, rw m.map_smul, }, }
lemma dom_dom_congr_trans (σ₁ : ι₁ ≃ ι₂) (σ₂ : ι₂ ≃ ι₃) (m : multilinear_map R (λ i : ι₁, M₂) M₃) :
m.dom_dom_congr (σ₁.trans σ₂) = (m.dom_dom_congr σ₁).dom_dom_congr σ₂ := rfl
lemma dom_dom_congr_mul (σ₁ : equiv.perm ι₁) (σ₂ : equiv.perm ι₁)
(m : multilinear_map R (λ i : ι₁, M₂) M₃) :
m.dom_dom_congr (σ₂ * σ₁) = (m.dom_dom_congr σ₁).dom_dom_congr σ₂ := rfl
/-- `multilinear_map.dom_dom_congr` as an equivalence.
This is declared separately because it does not work with dot notation. -/
@[simps apply symm_apply]
def dom_dom_congr_equiv (σ : ι₁ ≃ ι₂) :
multilinear_map R (λ i : ι₁, M₂) M₃ ≃+ multilinear_map R (λ i : ι₂, M₂) M₃ :=
{ to_fun := dom_dom_congr σ,
inv_fun := dom_dom_congr σ.symm,
left_inv := λ m, by {ext, simp},
right_inv := λ m, by {ext, simp},
map_add' := λ a b, by {ext, simp} }
/-- The results of applying `dom_dom_congr` to two maps are equal if
and only if those maps are. -/
@[simp] lemma dom_dom_congr_eq_iff (σ : ι₁ ≃ ι₂) (f g : multilinear_map R (λ i : ι₁, M₂) M₃) :
f.dom_dom_congr σ = g.dom_dom_congr σ ↔ f = g :=
(dom_dom_congr_equiv σ : _ ≃+ multilinear_map R (λ i, M₂) M₃).apply_eq_iff_eq
end
end semiring
end multilinear_map
namespace linear_map
variables [semiring R]
[Πi, add_comm_monoid (M₁ i)] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M']
[∀i, module R (M₁ i)] [module R M₂] [module R M₃] [module R M']
/-- Composing a multilinear map with a linear map gives again a multilinear map. -/
def comp_multilinear_map (g : M₂ →ₗ[R] M₃) (f : multilinear_map R M₁ M₂) :
multilinear_map R M₁ M₃ :=
{ to_fun := g ∘ f,
map_add' := λ m i x y, by simp,
map_smul' := λ m i c x, by simp }
@[simp] lemma coe_comp_multilinear_map (g : M₂ →ₗ[R] M₃) (f : multilinear_map R M₁ M₂) :
⇑(g.comp_multilinear_map f) = g ∘ f := rfl
@[simp]
lemma comp_multilinear_map_apply (g : M₂ →ₗ[R] M₃) (f : multilinear_map R M₁ M₂) (m : Π i, M₁ i) :
g.comp_multilinear_map f m = g (f m) := rfl
/-- The multilinear version of `linear_map.subtype_comp_cod_restrict` -/
@[simp]
lemma subtype_comp_multilinear_map_cod_restrict (f : multilinear_map R M₁ M₂) (p : submodule R M₂)
(h) : p.subtype.comp_multilinear_map (f.cod_restrict p h) = f :=
multilinear_map.ext $ λ v, rfl
/-- The multilinear version of `linear_map.comp_cod_restrict` -/
@[simp]
lemma comp_multilinear_map_cod_restrict (g : M₂ →ₗ[R] M₃) (f : multilinear_map R M₁ M₂)
(p : submodule R M₃) (h) :
(g.cod_restrict p h).comp_multilinear_map f =
(g.comp_multilinear_map f).cod_restrict p (λ v, h (f v)):=
multilinear_map.ext $ λ v, rfl
variables {ι₁ ι₂ : Type*} [decidable_eq ι₁] [decidable_eq ι₂]
@[simp] lemma comp_multilinear_map_dom_dom_congr (σ : ι₁ ≃ ι₂) (g : M₂ →ₗ[R] M₃)
(f : multilinear_map R (λ i : ι₁, M') M₂) :
(g.comp_multilinear_map f).dom_dom_congr σ = g.comp_multilinear_map (f.dom_dom_congr σ) :=
by { ext, simp }
end linear_map
namespace multilinear_map
section comm_semiring
variables [comm_semiring R] [∀i, add_comm_monoid (M₁ i)] [∀i, add_comm_monoid (M i)]
[add_comm_monoid M₂] [∀i, module R (M i)] [∀i, module R (M₁ i)] [module R M₂]
(f f' : multilinear_map R M₁ M₂)
/-- If one multiplies by `c i` the coordinates in a finset `s`, then the image under a multilinear
map is multiplied by `∏ i in s, c i`. This is mainly an auxiliary statement to prove the result when
`s = univ`, given in `map_smul_univ`, although it can be useful in its own right as it does not
require the index set `ι` to be finite. -/
lemma map_piecewise_smul (c : ι → R) (m : Πi, M₁ i) (s : finset ι) :
f (s.piecewise (λi, c i • m i) m) = (∏ i in s, c i) • f m :=
begin
refine s.induction_on (by simp) _,
assume j s j_not_mem_s Hrec,
have A : function.update (s.piecewise (λi, c i • m i) m) j (m j) =
s.piecewise (λi, c i • m i) m,
{ ext i,
by_cases h : i = j,
{ rw h, simp [j_not_mem_s] },
{ simp [h] } },
rw [s.piecewise_insert, f.map_smul, A, Hrec],
simp [j_not_mem_s, mul_smul]
end
/-- Multiplicativity of a multilinear map along all coordinates at the same time,
writing `f (λi, c i • m i)` as `(∏ i, c i) • f m`. -/
lemma map_smul_univ [fintype ι] (c : ι → R) (m : Πi, M₁ i) :
f (λi, c i • m i) = (∏ i, c i) • f m :=
by simpa using map_piecewise_smul f c m finset.univ
@[simp] lemma map_update_smul [fintype ι] (m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i) :
f (update (c • m) i x) = c^(fintype.card ι - 1) • f (update m i x) :=
begin
have : f ((finset.univ.erase i).piecewise (c • update m i x) (update m i x))
= (∏ i in finset.univ.erase i, c) • f (update m i x) :=
map_piecewise_smul f _ _ _,
simpa [←function.update_smul c m] using this,
end
section distrib_mul_action
variables {R' A : Type*} [monoid R'] [semiring A]
[Π i, module A (M₁ i)] [distrib_mul_action R' M₂] [module A M₂] [smul_comm_class A R' M₂]
instance : distrib_mul_action R' (multilinear_map A M₁ M₂) :=
{ one_smul := λ f, ext $ λ x, one_smul _ _,
mul_smul := λ c₁ c₂ f, ext $ λ x, mul_smul _ _ _,
smul_zero := λ r, ext $ λ x, smul_zero _,
smul_add := λ r f₁ f₂, ext $ λ x, smul_add _ _ _ }
end distrib_mul_action
section module
variables {R' A : Type*} [semiring R'] [semiring A]
[Π i, module A (M₁ i)] [module A M₂]
[add_comm_monoid M₃] [module R' M₃] [module A M₃] [smul_comm_class A R' M₃]
/-- The space of multilinear maps over an algebra over `R` is a module over `R`, for the pointwise
addition and scalar multiplication. -/
instance [module R' M₂] [smul_comm_class A R' M₂] : module R' (multilinear_map A M₁ M₂) :=
{ add_smul := λ r₁ r₂ f, ext $ λ x, add_smul _ _ _,
zero_smul := λ f, ext $ λ x, zero_smul _ _ }
instance [no_zero_smul_divisors R' M₃] : no_zero_smul_divisors R' (multilinear_map A M₁ M₃) :=
coe_injective.no_zero_smul_divisors _ rfl coe_smul
variables (M₂ M₃ R' A)
/-- `multilinear_map.dom_dom_congr` as a `linear_equiv`. -/
@[simps apply symm_apply]
def dom_dom_congr_linear_equiv {ι₁ ι₂} [decidable_eq ι₁] [decidable_eq ι₂] (σ : ι₁ ≃ ι₂) :
multilinear_map A (λ i : ι₁, M₂) M₃ ≃ₗ[R'] multilinear_map A (λ i : ι₂, M₂) M₃ :=
{ map_smul' := λ c f, by { ext, simp },
.. (dom_dom_congr_equiv σ : multilinear_map A (λ i : ι₁, M₂) M₃ ≃+
multilinear_map A (λ i : ι₂, M₂) M₃) }
variables (R M₁)
/-- The dependent version of `multilinear_map.dom_dom_congr_linear_equiv`. -/
@[simps apply symm_apply]
def dom_dom_congr_linear_equiv' {ι' : Type*} [decidable_eq ι'] (σ : ι ≃ ι') :
multilinear_map R M₁ M₂ ≃ₗ[R] multilinear_map R (λ i, M₁ (σ.symm i)) M₂ :=
{ to_fun := λ f,
{ to_fun := f ∘ (σ.Pi_congr_left' M₁).symm,
map_add' := λ m i,
begin
rw ← σ.apply_symm_apply i,
intros x y,
simp only [comp_app, Pi_congr_left'_symm_update, f.map_add],
end,
map_smul' := λ m i c,
begin
rw ← σ.apply_symm_apply i,
intros x,
simp only [comp_app, Pi_congr_left'_symm_update, f.map_smul],
end, },
inv_fun := λ f,
{ to_fun := f ∘ (σ.Pi_congr_left' M₁),
map_add' := λ m i,
begin
rw ← σ.symm_apply_apply i,
intros x y,
simp only [comp_app, Pi_congr_left'_update, f.map_add],
end,
map_smul' := λ m i c,
begin
rw ← σ.symm_apply_apply i,
intros x,
simp only [comp_app, Pi_congr_left'_update, f.map_smul],
end, },
map_add' := λ f₁ f₂, by { ext, simp only [comp_app, coe_mk, add_apply], },
map_smul' := λ c f, by { ext, simp only [comp_app, coe_mk, smul_apply, ring_hom.id_apply], },
left_inv := λ f, by { ext, simp only [comp_app, coe_mk, equiv.symm_apply_apply], },
right_inv := λ f, by { ext, simp only [comp_app, coe_mk, equiv.apply_symm_apply], }, }
/-- The space of constant maps is equivalent to the space of maps that are multilinear with respect
to an empty family. -/
@[simps] def const_linear_equiv_of_is_empty [is_empty ι] : M₂ ≃ₗ[R] multilinear_map R M₁ M₂ :=
{ to_fun := multilinear_map.const_of_is_empty R,
map_add' := λ x y, rfl,
map_smul' := λ t x, rfl,
inv_fun := λ f, f 0,
left_inv := λ _, rfl,
right_inv := λ f, ext $ λ x, multilinear_map.congr_arg f $ subsingleton.elim _ _ }
end module
section
variables (R ι) (A : Type*) [comm_semiring A] [algebra R A] [fintype ι]
/-- Given an `R`-algebra `A`, `mk_pi_algebra` is the multilinear map on `A^ι` associating
to `m` the product of all the `m i`.
See also `multilinear_map.mk_pi_algebra_fin` for a version that works with a non-commutative
algebra `A` but requires `ι = fin n`. -/
protected def mk_pi_algebra : multilinear_map R (λ i : ι, A) A :=
{ to_fun := λ m, ∏ i, m i,
map_add' := λ m i x y, by simp [finset.prod_update_of_mem, add_mul],
map_smul' := λ m i c x, by simp [finset.prod_update_of_mem] }
variables {R A ι}
@[simp] lemma mk_pi_algebra_apply (m : ι → A) :
multilinear_map.mk_pi_algebra R ι A m = ∏ i, m i :=
rfl
end
section
variables (R n) (A : Type*) [semiring A] [algebra R A]
/-- Given an `R`-algebra `A`, `mk_pi_algebra_fin` is the multilinear map on `A^n` associating
to `m` the product of all the `m i`.
See also `multilinear_map.mk_pi_algebra` for a version that assumes `[comm_semiring A]` but works
for `A^ι` with any finite type `ι`. -/
protected def mk_pi_algebra_fin : multilinear_map R (λ i : fin n, A) A :=
{ to_fun := λ m, (list.of_fn m).prod,
map_add' :=
begin
intros m i x y,
have : (list.fin_range n).index_of i < n,
by simpa using list.index_of_lt_length.2 (list.mem_fin_range i),
simp [list.of_fn_eq_map, (list.nodup_fin_range n).map_update, list.prod_update_nth, add_mul,
this, mul_add, add_mul]
end,
map_smul' :=
begin
intros m i c x,
have : (list.fin_range n).index_of i < n,
by simpa using list.index_of_lt_length.2 (list.mem_fin_range i),
simp [list.of_fn_eq_map, (list.nodup_fin_range n).map_update, list.prod_update_nth, this]
end }
variables {R A n}
@[simp] lemma mk_pi_algebra_fin_apply (m : fin n → A) :
multilinear_map.mk_pi_algebra_fin R n A m = (list.of_fn m).prod :=
rfl
lemma mk_pi_algebra_fin_apply_const (a : A) :
multilinear_map.mk_pi_algebra_fin R n A (λ _, a) = a ^ n :=
by simp
end
/-- Given an `R`-multilinear map `f` taking values in `R`, `f.smul_right z` is the map
sending `m` to `f m • z`. -/
def smul_right (f : multilinear_map R M₁ R) (z : M₂) : multilinear_map R M₁ M₂ :=
(linear_map.smul_right linear_map.id z).comp_multilinear_map f
@[simp] lemma smul_right_apply (f : multilinear_map R M₁ R) (z : M₂) (m : Π i, M₁ i) :
f.smul_right z m = f m • z :=
rfl
variables (R ι)
/-- The canonical multilinear map on `R^ι` when `ι` is finite, associating to `m` the product of
all the `m i` (multiplied by a fixed reference element `z` in the target module). See also
`mk_pi_algebra` for a more general version. -/
protected def mk_pi_ring [fintype ι] (z : M₂) : multilinear_map R (λ(i : ι), R) M₂ :=
(multilinear_map.mk_pi_algebra R ι R).smul_right z
variables {R ι}
@[simp] lemma mk_pi_ring_apply [fintype ι] (z : M₂) (m : ι → R) :
(multilinear_map.mk_pi_ring R ι z : (ι → R) → M₂) m = (∏ i, m i) • z := rfl
lemma mk_pi_ring_apply_one_eq_self [fintype ι] (f : multilinear_map R (λ(i : ι), R) M₂) :
multilinear_map.mk_pi_ring R ι (f (λi, 1)) = f :=
begin
ext m,
have : m = (λi, m i • 1), by { ext j, simp },
conv_rhs { rw [this, f.map_smul_univ] },
refl
end
lemma mk_pi_ring_eq_iff [fintype ι] {z₁ z₂ : M₂} :
multilinear_map.mk_pi_ring R ι z₁ = multilinear_map.mk_pi_ring R ι z₂ ↔ z₁ = z₂ :=
begin
simp_rw [multilinear_map.ext_iff, mk_pi_ring_apply],
split; intro h,
{ simpa using h (λ _, 1) },
{ intro x, simp [h] }
end
lemma mk_pi_ring_zero [fintype ι] :
multilinear_map.mk_pi_ring R ι (0 : M₂) = 0 :=
by ext; rw [mk_pi_ring_apply, smul_zero, multilinear_map.zero_apply]
lemma mk_pi_ring_eq_zero_iff [fintype ι] (z : M₂) :
multilinear_map.mk_pi_ring R ι z = 0 ↔ z = 0 :=
by rw [← mk_pi_ring_zero, mk_pi_ring_eq_iff]
end comm_semiring
section range_add_comm_group
variables [semiring R] [∀i, add_comm_monoid (M₁ i)] [add_comm_group M₂]
[∀i, module R (M₁ i)] [module R M₂]
(f g : multilinear_map R M₁ M₂)
instance : has_neg (multilinear_map R M₁ M₂) :=
⟨λ f, ⟨λ m, - f m, λm i x y, by simp [add_comm], λm i c x, by simp⟩⟩
@[simp] lemma neg_apply (m : Πi, M₁ i) : (-f) m = - (f m) := rfl
instance : has_sub (multilinear_map R M₁ M₂) :=
⟨λ f g,
⟨λ m, f m - g m,
λ m i x y, by { simp only [multilinear_map.map_add, sub_eq_add_neg, neg_add], cc },
λ m i c x, by { simp only [multilinear_map.map_smul, smul_sub] }⟩⟩
@[simp] lemma sub_apply (m : Πi, M₁ i) : (f - g) m = f m - g m := rfl
instance : add_comm_group (multilinear_map R M₁ M₂) :=
by refine
{ zero := (0 : multilinear_map R M₁ M₂),
add := (+),
neg := has_neg.neg,
sub := has_sub.sub,
sub_eq_add_neg := _,
nsmul := λ n f, ⟨λ m, n • f m, λm i x y, by simp [smul_add], λl i x d, by simp [←smul_comm x n] ⟩,
zsmul := λ n f, ⟨λ m, n • f m, λm i x y, by simp [smul_add], λl i x d, by simp [←smul_comm x n] ⟩,
zsmul_zero' := _,
zsmul_succ' := _,
zsmul_neg' := _,
.. multilinear_map.add_comm_monoid, .. };
intros; ext; simp [add_comm, add_left_comm, sub_eq_add_neg, add_smul, nat.succ_eq_add_one]
end range_add_comm_group
section add_comm_group
variables [semiring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂]
[∀i, module R (M₁ i)] [module R M₂]
(f : multilinear_map R M₁ M₂)
@[simp] lemma map_neg (m : Πi, M₁ i) (i : ι) (x : M₁ i) :
f (update m i (-x)) = -f (update m i x) :=
eq_neg_of_add_eq_zero_left $ by rw [←multilinear_map.map_add, add_left_neg,
f.map_coord_zero i (update_same i 0 m)]
@[simp] lemma map_sub (m : Πi, M₁ i) (i : ι) (x y : M₁ i) :
f (update m i (x - y)) = f (update m i x) - f (update m i y) :=
by rw [sub_eq_add_neg, sub_eq_add_neg, multilinear_map.map_add, map_neg]
end add_comm_group
section comm_semiring
variables [comm_semiring R] [∀i, add_comm_monoid (M₁ i)] [add_comm_monoid M₂]
[∀i, module R (M₁ i)] [module R M₂]
/-- When `ι` is finite, multilinear maps on `R^ι` with values in `M₂` are in bijection with `M₂`,
as such a multilinear map is completely determined by its value on the constant vector made of ones.
We register this bijection as a linear equivalence in `multilinear_map.pi_ring_equiv`. -/
protected def pi_ring_equiv [fintype ι] : M₂ ≃ₗ[R] (multilinear_map R (λ(i : ι), R) M₂) :=
{ to_fun := λ z, multilinear_map.mk_pi_ring R ι z,
inv_fun := λ f, f (λi, 1),
map_add' := λ z z', by { ext m, simp [smul_add] },
map_smul' := λ c z, by { ext m, simp [smul_smul, mul_comm] },
left_inv := λ z, by simp,
right_inv := λ f, f.mk_pi_ring_apply_one_eq_self }
end comm_semiring
end multilinear_map
section currying
/-!
### Currying
We associate to a multilinear map in `n+1` variables (i.e., based on `fin n.succ`) two
curried functions, named `f.curry_left` (which is a linear map on `E 0` taking values
in multilinear maps in `n` variables) and `f.curry_right` (wich is a multilinear map in `n`
variables taking values in linear maps on `E 0`). In both constructions, the variable that is
singled out is `0`, to take advantage of the operations `cons` and `tail` on `fin n`.
The inverse operations are called `uncurry_left` and `uncurry_right`.
We also register linear equiv versions of these correspondences, in
`multilinear_curry_left_equiv` and `multilinear_curry_right_equiv`.
-/
open multilinear_map
variables {R M M₂}
[comm_semiring R] [∀i, add_comm_monoid (M i)] [add_comm_monoid M'] [add_comm_monoid M₂]
[∀i, module R (M i)] [module R M'] [module R M₂]
/-! #### Left currying -/
/-- Given a linear map `f` from `M 0` to multilinear maps on `n` variables,
construct the corresponding multilinear map on `n+1` variables obtained by concatenating
the variables, given by `m ↦ f (m 0) (tail m)`-/
def linear_map.uncurry_left
(f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) :
multilinear_map R M M₂ :=
{ to_fun := λm, f (m 0) (tail m),
map_add' := λm i x y, begin
by_cases h : i = 0,
{ subst i,
rw [update_same, update_same, update_same, f.map_add, add_apply,
tail_update_zero, tail_update_zero, tail_update_zero] },
{ rw [update_noteq (ne.symm h), update_noteq (ne.symm h), update_noteq (ne.symm h)],
revert x y,
rw ← succ_pred i h,
assume x y,
rw [tail_update_succ, multilinear_map.map_add, tail_update_succ, tail_update_succ] }
end,
map_smul' := λm i c x, begin
by_cases h : i = 0,
{ subst i,
rw [update_same, update_same, tail_update_zero, tail_update_zero,
← smul_apply, f.map_smul] },
{ rw [update_noteq (ne.symm h), update_noteq (ne.symm h)],
revert x,
rw ← succ_pred i h,
assume x,
rw [tail_update_succ, tail_update_succ, multilinear_map.map_smul] }
end }
@[simp] lemma linear_map.uncurry_left_apply
(f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) (m : Πi, M i) :
f.uncurry_left m = f (m 0) (tail m) := rfl
/-- Given a multilinear map `f` in `n+1` variables, split the first variable to obtain
a linear map into multilinear maps in `n` variables, given by `x ↦ (m ↦ f (cons x m))`. -/
def multilinear_map.curry_left
(f : multilinear_map R M M₂) :
M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂) :=
{ to_fun := λx,
{ to_fun := λm, f (cons x m),
map_add' := λm i y y', by simp,
map_smul' := λm i y c, by simp },
map_add' := λx y, by { ext m, exact cons_add f m x y },
map_smul' := λc x, by { ext m, exact cons_smul f m c x } }
@[simp] lemma multilinear_map.curry_left_apply
(f : multilinear_map R M M₂) (x : M 0) (m : Π(i : fin n), M i.succ) :
f.curry_left x m = f (cons x m) := rfl
@[simp] lemma linear_map.curry_uncurry_left
(f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) :
f.uncurry_left.curry_left = f :=
begin
ext m x,
simp only [tail_cons, linear_map.uncurry_left_apply, multilinear_map.curry_left_apply],
rw cons_zero
end
@[simp] lemma multilinear_map.uncurry_curry_left
(f : multilinear_map R M M₂) :
f.curry_left.uncurry_left = f :=
by { ext m, simp, }
variables (R M M₂)
/-- The space of multilinear maps on `Π(i : fin (n+1)), M i` is canonically isomorphic to
the space of linear maps from `M 0` to the space of multilinear maps on
`Π(i : fin n), M i.succ `, by separating the first variable. We register this isomorphism as a
linear isomorphism in `multilinear_curry_left_equiv R M M₂`.
The direct and inverse maps are given by `f.uncurry_left` and `f.curry_left`. Use these
unless you need the full framework of linear equivs. -/
def multilinear_curry_left_equiv :
(M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) ≃ₗ[R] (multilinear_map R M M₂) :=
{ to_fun := linear_map.uncurry_left,
map_add' := λf₁ f₂, by { ext m, refl },
map_smul' := λc f, by { ext m, refl },
inv_fun := multilinear_map.curry_left,
left_inv := linear_map.curry_uncurry_left,
right_inv := multilinear_map.uncurry_curry_left }
variables {R M M₂}
/-! #### Right currying -/
/-- Given a multilinear map `f` in `n` variables to the space of linear maps from `M (last n)` to
`M₂`, construct the corresponding multilinear map on `n+1` variables obtained by concatenating
the variables, given by `m ↦ f (init m) (m (last n))`-/
def multilinear_map.uncurry_right
(f : (multilinear_map R (λ(i : fin n), M i.cast_succ) (M (last n) →ₗ[R] M₂))) :
multilinear_map R M M₂ :=
{ to_fun := λm, f (init m) (m (last n)),
map_add' := λm i x y, begin
by_cases h : i.val < n,
{ have : last n ≠ i := ne.symm (ne_of_lt h),
rw [update_noteq this, update_noteq this, update_noteq this],
revert x y,
rw [(cast_succ_cast_lt i h).symm],
assume x y,
rw [init_update_cast_succ, multilinear_map.map_add, init_update_cast_succ,
init_update_cast_succ, linear_map.add_apply] },
{ revert x y,
rw eq_last_of_not_lt h,
assume x y,
rw [init_update_last, init_update_last, init_update_last,
update_same, update_same, update_same, linear_map.map_add] }
end,
map_smul' := λm i c x, begin
by_cases h : i.val < n,
{ have : last n ≠ i := ne.symm (ne_of_lt h),
rw [update_noteq this, update_noteq this],
revert x,
rw [(cast_succ_cast_lt i h).symm],
assume x,
rw [init_update_cast_succ, init_update_cast_succ, multilinear_map.map_smul,
linear_map.smul_apply] },
{ revert x,
rw eq_last_of_not_lt h,
assume x,
rw [update_same, update_same, init_update_last, init_update_last, map_smul] }
end }
@[simp] lemma multilinear_map.uncurry_right_apply
(f : (multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂))) (m : Πi, M i) :
f.uncurry_right m = f (init m) (m (last n)) := rfl
/-- Given a multilinear map `f` in `n+1` variables, split the last variable to obtain
a multilinear map in `n` variables taking values in linear maps from `M (last n)` to `M₂`, given by
`m ↦ (x ↦ f (snoc m x))`. -/
def multilinear_map.curry_right (f : multilinear_map R M M₂) :
multilinear_map R (λ(i : fin n), M (fin.cast_succ i)) ((M (last n)) →ₗ[R] M₂) :=
{ to_fun := λm,
{ to_fun := λx, f (snoc m x),
map_add' := λx y, by rw f.snoc_add,
map_smul' := λc x, by simp only [f.snoc_smul, ring_hom.id_apply] },
map_add' := λm i x y, begin
ext z,
change f (snoc (update m i (x + y)) z)
= f (snoc (update m i x) z) + f (snoc (update m i y) z),
rw [snoc_update, snoc_update, snoc_update, f.map_add]
end,
map_smul' := λm i c x, begin
ext z,
change f (snoc (update m i (c • x)) z) = c • f (snoc (update m i x) z),
rw [snoc_update, snoc_update, f.map_smul]
end }
@[simp] lemma multilinear_map.curry_right_apply
(f : multilinear_map R M M₂) (m : Π(i : fin n), M i.cast_succ) (x : M (last n)) :
f.curry_right m x = f (snoc m x) := rfl
@[simp] lemma multilinear_map.curry_uncurry_right
(f : (multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂))) :
f.uncurry_right.curry_right = f :=
begin
ext m x,
simp only [snoc_last, multilinear_map.curry_right_apply, multilinear_map.uncurry_right_apply],
rw init_snoc
end
@[simp] lemma multilinear_map.uncurry_curry_right
(f : multilinear_map R M M₂) : f.curry_right.uncurry_right = f :=
by { ext m, simp }
variables (R M M₂)
/-- The space of multilinear maps on `Π(i : fin (n+1)), M i` is canonically isomorphic to
the space of linear maps from the space of multilinear maps on `Π(i : fin n), M i.cast_succ` to the
space of linear maps on `M (last n)`, by separating the last variable. We register this isomorphism
as a linear isomorphism in `multilinear_curry_right_equiv R M M₂`.
The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these
unless you need the full framework of linear equivs. -/
def multilinear_curry_right_equiv :
(multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂))
≃ₗ[R] (multilinear_map R M M₂) :=
{ to_fun := multilinear_map.uncurry_right,
map_add' := λf₁ f₂, by { ext m, refl },
map_smul' := λc f, by { ext m, rw [smul_apply], refl },
inv_fun := multilinear_map.curry_right,
left_inv := multilinear_map.curry_uncurry_right,
right_inv := multilinear_map.uncurry_curry_right }
namespace multilinear_map
variables {ι' : Type*} [decidable_eq ι'] [decidable_eq (ι ⊕ ι')] {R M₂}
/-- A multilinear map on `Π i : ι ⊕ ι', M'` defines a multilinear map on `Π i : ι, M'`
taking values in the space of multilinear maps on `Π i : ι', M'`. -/
def curry_sum (f : multilinear_map R (λ x : ι ⊕ ι', M') M₂) :
multilinear_map R (λ x : ι, M') (multilinear_map R (λ x : ι', M') M₂) :=
{ to_fun := λ u,
{ to_fun := λ v, f (sum.elim u v),
map_add' := λ v i x y, by simp only [← sum.update_elim_inr, f.map_add],
map_smul' := λ v i c x, by simp only [← sum.update_elim_inr, f.map_smul] },
map_add' := λ u i x y, ext $ λ v,
by simp only [multilinear_map.coe_mk, add_apply, ← sum.update_elim_inl, f.map_add],
map_smul' := λ u i c x, ext $ λ v,
by simp only [multilinear_map.coe_mk, smul_apply, ← sum.update_elim_inl, f.map_smul] }
@[simp] lemma curry_sum_apply (f : multilinear_map R (λ x : ι ⊕ ι', M') M₂)
(u : ι → M') (v : ι' → M') :
f.curry_sum u v = f (sum.elim u v) :=
rfl
/-- A multilinear map on `Π i : ι, M'` taking values in the space of multilinear maps
on `Π i : ι', M'` defines a multilinear map on `Π i : ι ⊕ ι', M'`. -/
def uncurry_sum (f : multilinear_map R (λ x : ι, M') (multilinear_map R (λ x : ι', M') M₂)) :
multilinear_map R (λ x : ι ⊕ ι', M') M₂ :=
{ to_fun := λ u, f (u ∘ sum.inl) (u ∘ sum.inr),
map_add' := λ u i x y, by cases i;
simp only [multilinear_map.map_add, add_apply, sum.update_inl_comp_inl, sum.update_inl_comp_inr,
sum.update_inr_comp_inl, sum.update_inr_comp_inr],
map_smul' := λ u i c x, by cases i;
simp only [multilinear_map.map_smul, smul_apply, sum.update_inl_comp_inl,
sum.update_inl_comp_inr, sum.update_inr_comp_inl, sum.update_inr_comp_inr] }
@[simp] lemma uncurry_sum_aux_apply
(f : multilinear_map R (λ x : ι, M') (multilinear_map R (λ x : ι', M') M₂)) (u : ι ⊕ ι' → M') :
f.uncurry_sum u = f (u ∘ sum.inl) (u ∘ sum.inr) :=
rfl
variables (ι ι' R M₂ M')
/-- Linear equivalence between the space of multilinear maps on `Π i : ι ⊕ ι', M'` and the space
of multilinear maps on `Π i : ι, M'` taking values in the space of multilinear maps
on `Π i : ι', M'`. -/
def curry_sum_equiv : multilinear_map R (λ x : ι ⊕ ι', M') M₂ ≃ₗ[R]
multilinear_map R (λ x : ι, M') (multilinear_map R (λ x : ι', M') M₂) :=
{ to_fun := curry_sum,
inv_fun := uncurry_sum,
left_inv := λ f, ext $ λ u, by simp,
right_inv := λ f, by { ext, simp },
map_add' := λ f g, by { ext, refl },
map_smul' := λ c f, by { ext, refl } }
variables {ι ι' R M₂ M'}
@[simp] lemma coe_curry_sum_equiv : ⇑(curry_sum_equiv R ι M₂ M' ι') = curry_sum := rfl
@[simp] lemma coe_curr_sum_equiv_symm : ⇑(curry_sum_equiv R ι M₂ M' ι').symm = uncurry_sum := rfl
variables (R M₂ M')
/-- If `s : finset (fin n)` is a finite set of cardinality `k` and its complement has cardinality
`l`, then the space of multilinear maps on `λ i : fin n, M'` is isomorphic to the space of
multilinear maps on `λ i : fin k, M'` taking values in the space of multilinear maps
on `λ i : fin l, M'`. -/
def curry_fin_finset {k l n : ℕ} {s : finset (fin n)}
(hk : s.card = k) (hl : sᶜ.card = l) :
multilinear_map R (λ x : fin n, M') M₂ ≃ₗ[R]
multilinear_map R (λ x : fin k, M') (multilinear_map R (λ x : fin l, M') M₂) :=
(dom_dom_congr_linear_equiv M' M₂ R R (fin_sum_equiv_of_finset hk hl).symm).trans
(curry_sum_equiv R (fin k) M₂ M' (fin l))
variables {R M₂ M'}
@[simp]
lemma curry_fin_finset_apply {k l n : ℕ} {s : finset (fin n)}
(hk : s.card = k) (hl : sᶜ.card = l) (f : multilinear_map R (λ x : fin n, M') M₂)
(mk : fin k → M') (ml : fin l → M') :
curry_fin_finset R M₂ M' hk hl f mk ml =
f (λ i, sum.elim mk ml ((fin_sum_equiv_of_finset hk hl).symm i)) :=
rfl
@[simp] lemma curry_fin_finset_symm_apply {k l n : ℕ} {s : finset (fin n)}
(hk : s.card = k) (hl : sᶜ.card = l)
(f : multilinear_map R (λ x : fin k, M') (multilinear_map R (λ x : fin l, M') M₂))
(m : fin n → M') :
(curry_fin_finset R M₂ M' hk hl).symm f m =
f (λ i, m $ fin_sum_equiv_of_finset hk hl (sum.inl i))
(λ i, m $ fin_sum_equiv_of_finset hk hl (sum.inr i)) :=
rfl
@[simp] lemma curry_fin_finset_symm_apply_piecewise_const {k l n : ℕ} {s : finset (fin n)}
(hk : s.card = k) (hl : sᶜ.card = l)
(f : multilinear_map R (λ x : fin k, M') (multilinear_map R (λ x : fin l, M') M₂)) (x y : M') :
(curry_fin_finset R M₂ M' hk hl).symm f (s.piecewise (λ _, x) (λ _, y)) = f (λ _, x) (λ _, y) :=
begin
rw curry_fin_finset_symm_apply, congr,
{ ext i, rw [fin_sum_equiv_of_finset_inl, finset.piecewise_eq_of_mem],
apply finset.order_emb_of_fin_mem },
{ ext i, rw [fin_sum_equiv_of_finset_inr, finset.piecewise_eq_of_not_mem],
exact finset.mem_compl.1 (finset.order_emb_of_fin_mem _ _ _) }
end
@[simp] lemma curry_fin_finset_symm_apply_const {k l n : ℕ} {s : finset (fin n)}
(hk : s.card = k) (hl : sᶜ.card = l)
(f : multilinear_map R (λ x : fin k, M') (multilinear_map R (λ x : fin l, M') M₂)) (x : M') :
(curry_fin_finset R M₂ M' hk hl).symm f (λ _, x) = f (λ _, x) (λ _, x) :=
rfl
@[simp] lemma curry_fin_finset_apply_const {k l n : ℕ} {s : finset (fin n)}
(hk : s.card = k) (hl : sᶜ.card = l) (f : multilinear_map R (λ x : fin n, M') M₂) (x y : M') :
curry_fin_finset R M₂ M' hk hl f (λ _, x) (λ _, y) = f (s.piecewise (λ _, x) (λ _, y)) :=
begin
refine (curry_fin_finset_symm_apply_piecewise_const hk hl _ _ _).symm.trans _, -- `rw` fails
rw linear_equiv.symm_apply_apply
end
end multilinear_map
end currying
namespace multilinear_map
section submodule
variables {R M M₂}
[ring R] [∀i, add_comm_monoid (M₁ i)] [add_comm_monoid M'] [add_comm_monoid M₂]
[∀i, module R (M₁ i)] [module R M'] [module R M₂]
/-- The pushforward of an indexed collection of submodule `p i ⊆ M₁ i` by `f : M₁ → M₂`.
Note that this is not a submodule - it is not closed under addition. -/
def map [nonempty ι] (f : multilinear_map R M₁ M₂) (p : Π i, submodule R (M₁ i)) :
sub_mul_action R M₂ :=
{ carrier := f '' { v | ∀ i, v i ∈ p i},
smul_mem' := λ c _ ⟨x, hx, hf⟩, let ⟨i⟩ := ‹nonempty ι› in by
{ refine ⟨update x i (c • x i), λ j, if hij : j = i then _ else _, hf ▸ _⟩,
{ rw [hij, update_same], exact (p i).smul_mem _ (hx i) },
{ rw [update_noteq hij], exact hx j },
{ rw [f.map_smul, update_eq_self] } } }
/-- The map is always nonempty. This lemma is needed to apply `sub_mul_action.zero_mem`. -/
lemma map_nonempty [nonempty ι] (f : multilinear_map R M₁ M₂) (p : Π i, submodule R (M₁ i)) :
(map f p : set M₂).nonempty :=
⟨f 0, 0, λ i, (p i).zero_mem, rfl⟩
/-- The range of a multilinear map, closed under scalar multiplication. -/
def range [nonempty ι] (f : multilinear_map R M₁ M₂) : sub_mul_action R M₂ :=
f.map (λ i, ⊤)
end submodule
end multilinear_map
|
55400a01037747e3d4e7d06d47d19939ef97f5ec | 740993c2939128e433a4feddfe605721331a0c75 | /incidence.lean | c2c90ceccaa580a7d2c5cb403397e81745342405 | [] | no_license | gabrielmoise/Lean-work | 436bb7bf1e8144eb1aa90cc161b386a6ee6892bf | 52b2a2c840f8477e293cbb897335a19ebf482cf7 | refs/heads/master | 1,681,251,086,221 | 1,620,036,203,000 | 1,620,036,203,000 | 360,084,138 | 0 | 2 | null | null | null | null | UTF-8 | Lean | false | false | 16,998 | lean | /-
Copyright (c) 2021 Gabriel Moise. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Gabriel Moise.
-/
import algebra.big_operators.basic
import combinatorics.simple_graph.basic
import data.fintype.basic
import data.sym2
import linear_algebra.matrix
/-!
# Incidence matrices
This module defines the incidence matrix `inc_matrix` of an undirected graph `simple_graph`, and provides
theorems and lemmas connecting graph properties to computational properties of the matrix. It also
defines the notion of `orientation` for a `simple_graph`, picking a direction for each undirected
edge in the graph.
## Main definitions
* `inc_matrix` is the incidence matrix `M` of a `simple_graph` with coefficients in a given ring R.
* `orientation` is a structure that defines a choice of direction on the edges of a `simple_graph`.
* `dir_inc_matrix` is the directed incidence matrix `N(o)` of a `simple_graph` with
respect to a given `orientation`.
## Main statements
1. ∑ e : E, M i e * M j e = 1, for any two adjacent vertices i and j.
2. M i e * M j e = 0, for any two distinct non-adjacent vertices i, j and edge e.
3. Every element from M is idempotent.
4. For any vertex i, the sum on the ith row of M is equal to the degree of i.
5. (N(o) i e) ^ 2 = M i e, for any orientation o, vertex i and edge e.
6. For any adjacent vertices i j and edge e, N(o) i e * N(o) j e = if e = (i,j) then -1 else 0.
7. For any non-adjacent distinct vertices i j and edge e, N(o) i e * N(o) j e = 0.
8. (xᵀ ⬝ N) e = x head(e) - x tail(e).
-/
open_locale big_operators matrix
open finset matrix simple_graph sym2
universe u
variables {R : Type u} [ring R] [nontrivial R] [decidable_eq R]
@[simp]
lemma ite_prod_one_zero {P Q : Prop} [decidable P] [decidable Q] :
(ite P 1 0) * (ite Q 1 0) = ite (P ∧ Q) (1 : R) 0 :=
by { by_cases h : P; simp [h] }
lemma fintype.card_coe_filter {α : Sort*} {s t : set α} [fintype s] [fintype t]
[decidable_pred (λ (x : t), (x : α) ∈ s)] (h : s ⊆ t) :
fintype.card s = finset.card (finset.filter (λ (x : t), (x : α) ∈ s) finset.univ) :=
begin
refine finset.card_congr _ _ _ _,
{ rintros ⟨e, he⟩ he',
exact ⟨e, h he⟩ },
{ rintros ⟨e, he⟩ he',
simpa only [true_and, finset.mem_univ, finset.mem_filter] using he},
{ rintros ⟨e1, he1⟩ ⟨e2, he2⟩ he1' he2' hr,
ext,
simp only [subtype.mk_eq_mk] at hr,
simp only [hr] },
{ rintros ⟨e, he⟩ he',
use [e],
{ simpa only [true_and, finset.mem_univ, finset.mem_filter] using he'},
{ simp only [finset.mem_univ, exists_prop_of_true] } }
end
namespace simple_graph
universe v
variables {V : Type v} [fintype V] (G : simple_graph V) (R) [decidable_rel G.adj] [decidable_eq V]
-- ## Incidence matrix M
/-- `inc_matrix G R` is the matrix `M` such that `M i e = 1` if vertex `i` is an
endpoint of the edge `e` in the simple graph `G`, otherwise `M i j = 0`. -/
def inc_matrix : matrix V G.edge_set R
| i e := if (e : sym2 V) ∈ G.incidence_set i then 1 else 0
@[simp]
lemma inc_matrix_apply {i : V} {e : G.edge_set} :
G.inc_matrix R i e = if (e : sym2 V) ∈ G.incidence_set i then 1 else 0 := rfl
lemma inc_matrix_def : G.inc_matrix R = λ i e, ite ((e : sym2 V) ∈ G.incidence_set i) 1 0 :=
by { ext, simp only [inc_matrix_apply] }
-- ## Relation between inc_matrix elements and incidence_set property
@[simp]
lemma inc_matrix_zero {i : V} {e : G.edge_set} : G.inc_matrix R i e = 0 ↔ e.val ∉ G.incidence_set i :=
by simp only [inc_matrix, ite_eq_right_iff, subtype.val_eq_coe, ← decidable.not_imp_not,
forall_true_left, not_false_iff, one_ne_zero]
@[simp]
lemma inc_matrix_one {i : V} {e : G.edge_set} : G.inc_matrix R i e = 1 ↔ e.val ∈ G.incidence_set i :=
by simp only [inc_matrix, ite_eq_left_iff, subtype.val_eq_coe, ← decidable.not_imp_not,
set.not_not_mem, forall_true_left, not_false_iff, zero_ne_one]
-- ## One - zero properties
@[simp]
lemma inc_matrix_not_zero {i : V} {e : G.edge_set} : ¬ G.inc_matrix R i e = 0 ↔ G.inc_matrix R i e = 1 :=
by simp only [inc_matrix_zero, inc_matrix_one, set.not_not_mem]
@[simp]
lemma inc_matrix_not_one {i : V} {e : G.edge_set} : ¬ G.inc_matrix R i e = 1 ↔ G.inc_matrix R i e = 0 :=
by simp only [inc_matrix_zero, inc_matrix_one]
lemma inc_matrix_zero_or_one {i : V} {e : G.edge_set} :
G.inc_matrix R i e = 0 ∨ G.inc_matrix R i e = 1 :=
by { rw [inc_matrix_zero, inc_matrix_one], exact (em (e.val ∈ G.incidence_set i)).symm }
@[simp]
lemma inc_matrix_elements_product_one {i j : V} {e : G.edge_set} :
G.inc_matrix R i e * G.inc_matrix R j e = 1 ↔ G.inc_matrix R i e = 1 ∧ G.inc_matrix R j e = 1 :=
begin
cases G.inc_matrix_zero_or_one R with H₀ H₁,
{ rw H₀, simp only [if_t_t, mul_boole, inc_matrix_apply, zero_ne_one, false_and] },
{ rw H₁, simp only [true_and, mul_boole, inc_matrix_apply, eq_self_iff_true] }
end
-- ## Helping lemmas for edges
@[simp]
lemma edge_val_equiv {e₁ e₂ : G.edge_set} : e₁.val = e₂.val ↔ e₁ = e₂ :=
begin
split,
{ exact subtype.eq },
{ intro hyp,
rw hyp }
end
lemma edge_val_in_set {e : G.edge_set} : e.val ∈ G.edge_set :=
by simp only [subtype.coe_prop, subtype.val_eq_coe]
lemma edge_set_ne {u v : V} {e: G.edge_set} (h : e.val = ⟦(u, v)⟧) : u ≠ v :=
begin
apply G.ne_of_adj,
simp only [← G.mem_edge_set, ← h, edge_val_in_set],
end
lemma incidence_equiv {i : V} {e : G.edge_set} : e.val ∈ G.incidence_set i ↔ i ∈ e.val :=
by simp only [incidence_set, true_and, set.mem_sep_eq, edge_val_in_set]
lemma incidence_set_iff_any_vertex {i u v : V} (h : ⟦(u, v)⟧ ∈ G.edge_set) :
⟦(u, v)⟧ ∈ G.incidence_set i ↔ i = u ∨ i = v :=
by simp only [← mem_iff, h, incidence_set, true_and, set.mem_sep_eq]
lemma edge_in_two_incidence_sets {i j : V} {e : sym2 V} (H_ne : i ≠ j) :
e ∈ G.incidence_set i ∧ e ∈ G.incidence_set j → e = ⟦(i, j)⟧ :=
begin
refine quotient.rec_on_subsingleton e (λ p, _),
rcases p with ⟨v, w⟩,
rw eq_iff,
rintros ⟨⟨_, H_i⟩, ⟨_, H_j⟩⟩,
cases (mem_iff.mp H_i) with H_i₁ H_i₂;
cases (mem_iff.mp H_j) with H_j₁ H_j₂,
{ exfalso, apply H_ne, rw [H_i₁, H_j₁] }, -- i = v, j = v
{ left, use [H_i₁.symm, H_j₂.symm] }, -- i = v, j = w
{ right, use [H_j₁.symm, H_i₂.symm] }, -- i = w, j = v
{ exfalso, apply H_ne, rw [H_i₂, H_j₂] } -- i = w, j = w
end
lemma mem_incidence_sets_iff_eq_of_adj {i j : V} {e : sym2 V} (h : G.adj i j) :
e ∈ G.incidence_set i ∧ e ∈ G.incidence_set j ↔ e = ⟦(i, j)⟧ :=
begin
refine quotient.rec_on_subsingleton e (λ p, _),
rcases p with ⟨v, w⟩,
rw eq_iff,
simp only [incidence_set],
tidy,
end
lemma adj_iff_exists_edge_val {i j : V} : G.adj i j ↔ ∃ (e : G.edge_set), e.val = ⟦(i, j)⟧ :=
by simp only [mem_edge_set, exists_prop, set_coe.exists, exists_eq_right, subtype.coe_mk]
-- 1. ∑ e : E, M i e * M j e = 1, where i and j are adjacent.
lemma adj_sum_of_prod_inc_one {i j : V} (H_adj : G.adj i j) :
∑ (e : G.edge_set), G.inc_matrix R i e * G.inc_matrix R j e = (1 : R) :=
begin
simp only [inc_matrix_apply, ite_prod_one_zero, G.mem_incidence_sets_iff_eq_of_adj H_adj,
sum_boole, ← subtype.val_eq_coe],
rw adj_iff_exists_edge_val at H_adj,
rcases H_adj with ⟨e, H_e⟩,
simp only [← H_e, edge_val_equiv],
have H : filter (λ (x : G.edge_set), x = e) univ = {e},
{ ext, simp only [true_and, mem_filter, mem_univ, mem_singleton] },
simp only [H, filter_congr_decidable, nat.cast_one, card_singleton]
end
-- 2. M i e * M j e = 0, where i, j distinct non-adjacent vertices, e an edge.
lemma inc_matrix_prod_non_adj {i j : V} {e : G.edge_set} (Hne : i ≠ j) (H_non_adj : ¬ G.adj i j) :
G.inc_matrix R i e * G.inc_matrix R j e = 0 :=
begin
by_cases H₁ : G.inc_matrix R i e = 0,
{ rw [H₁, zero_mul] },
{ rw [inc_matrix_not_zero, inc_matrix_one] at H₁,
by_cases H₂ : G.inc_matrix R j e = 0,
{ rw [H₂, mul_zero] },
{ rw [inc_matrix_not_zero, inc_matrix_one] at H₂,
exfalso,
apply H_non_adj,
rw [← mem_edge_set, ← G.edge_in_two_incidence_sets Hne ⟨H₁, H₂⟩],
exact G.edge_val_in_set } }
end
-- 3. (M i e) ^ 2 = M i e; with i a vertex, e an edge.
@[simp]
lemma inc_matrix_element_power_id {i : V} {e : G.edge_set} :
(G.inc_matrix R i e) * (G.inc_matrix R i e) = G.inc_matrix R i e :=
by simp [inc_matrix_apply]
-- 4. degree(i) = ∑ e : E, M i e; where i is a vertex.
lemma degree_equals_sum_of_incidence_row {i : V} : (G.degree i : R) = ∑ (e : G.edge_set), G.inc_matrix R i e :=
begin
rw [inc_matrix_def, ←card_incidence_set_eq_degree],
simp only [sum_boole, nat.cast_inj, fintype.card_coe_filter (G.incidence_set_subset i)],
end
-- ## Orientations
/-- Define an `orientation` on the undirected graph G as a structure that defines (consistently)
for each edge a `head` and a `tail`. -/
@[ext]
structure orientation (G : simple_graph V) :=
(head : G.edge_set → V)
(tail : G.edge_set → V)
(consistent : ∀ e : G.edge_set, e.val = ⟦(head(e),tail(e))⟧)
-- ## Directed Incidence Matrix N(o)
/-- A `directed incidence matrix` N(o) is defined with respect to the orientation of the edges and is defined to be
`1` for entries (`i`,`e`) where `i` is the head of `e`, `-1` where `i` is the tail of `e`, and `0` otherwise. -/
def dir_inc_matrix (o : orientation G) : matrix V G.edge_set R :=
λ i e, if i = o.head e then 1 else (if i = o.tail e then (-1 : R) else 0)
variables {o : orientation G}
@[simp]
lemma dir_inc_matrix_apply {i : V} {e : G.edge_set} :
G.dir_inc_matrix R o i e = if i = o.head e then 1 else (if i = o.tail e then (-1 : R) else 0) := rfl
lemma head_neq_tail {e : G.edge_set} : o.head(e) ≠ o.tail(e) :=
by exact G.edge_set_ne (o.consistent e)
@[simp]
lemma dir_inc_matrix_head {i : V} {e : G.edge_set} (H_head : i = o.head e) :
G.dir_inc_matrix R o i e = 1 :=
by simp only [H_head, if_true, eq_self_iff_true, dir_inc_matrix_apply]
@[simp]
lemma dir_inc_matrix_tail {i : V} {e : G.edge_set} (H_tail : i = o.tail e) :
G.dir_inc_matrix R o i e = -1 :=
by simp only [H_tail, dir_inc_matrix, (G.head_neq_tail).symm, if_false, if_true, eq_self_iff_true]
@[simp]
lemma dir_inc_matrix_zero {i : V} {e : G.edge_set} :
G.dir_inc_matrix R o i e = 0 ↔ i ≠ o.head e ∧ i ≠ o.tail e :=
begin
by_cases H₁ : i = o.head e,
{ simp only [dir_inc_matrix, H₁, if_true, eq_self_iff_true, not_true,
ne.def, one_ne_zero, false_and] },
{ by_cases H₂ : i = o.tail e,
{ simp only [H₂, dir_inc_matrix_tail, eq_self_iff_true, not_true,
ne.def, neg_eq_zero, one_ne_zero, and_false] },
{ simp only [H₁, H₂, eq_self_iff_true, if_false, ne.def,
not_false_iff, and_self, dir_inc_matrix_apply] } }
end
@[simp]
lemma dir_inc_matrix_non_zero {i : V} {e : G.edge_set} :
¬ G.dir_inc_matrix R o i e = 0 ↔ i = o.head e ∨ i = o.tail e :=
begin
by_cases H₁ : i = o.head e,
{ simp only [H₁, if_true, true_or, eq_self_iff_true, ne.def,
not_false_iff, one_ne_zero, dir_inc_matrix_apply] },
{ by_cases H₂ : i = o.tail e,
{ simp only [H₂, dir_inc_matrix_tail, eq_self_iff_true, ne.def, or_true,
not_false_iff, neg_eq_zero, one_ne_zero] },
{ simp only [H₁, H₂, eq_self_iff_true, not_true, if_false,
ne.def, dir_inc_matrix_apply, or_self] } }
end
lemma incidence_set_orientation_head {e : G.edge_set} : e.val ∈ G.incidence_set (o.head e) :=
by { rw [incidence_equiv, o.consistent e], simp only [mem_iff, true_or, eq_self_iff_true] }
lemma incidence_set_orientation_tail {e : G.edge_set} : e.val ∈ G.incidence_set (o.tail e) :=
by { rw [incidence_equiv, o.consistent e], simp only [mem_iff, eq_self_iff_true, or_true] }
lemma incidence_set_orientation {i : V} {e : G.edge_set} :
e.val ∈ G.incidence_set i ↔ i = o.head e ∨ i = o.tail e :=
begin
rw o.consistent e,
have key : ⟦(o.head e, o.tail e)⟧ ∈ G.edge_set, {rw ← o.consistent e, exact G.edge_val_in_set},
exact G.incidence_set_iff_any_vertex key,
end
lemma not_incidence_set_orientation {i : V} {e : G.edge_set}
(H_head : i ≠ o.head e) (H_tail : i ≠ o.tail e) : e.val ∉ G.incidence_set i :=
begin
intro h,
rw G.incidence_set_orientation at h,
tauto,
end
-- 5. (N(o) i e) ^ 2 = M i e, for any orientation o, vertex i and edge e.
@[simp]
lemma dir_inc_matrix_elem_squared {i : V} {e : G.edge_set} :
G.dir_inc_matrix R o i e * G.dir_inc_matrix R o i e = G.inc_matrix R i e :=
begin
by_cases H_head : i = o.head e,
{ rw [G.dir_inc_matrix_head R H_head, H_head, mul_one, eq_comm, inc_matrix_one],
exact G.incidence_set_orientation_head },
{ by_cases H_tail : i = o.tail e,
{ rw [G.dir_inc_matrix_tail R H_tail, H_tail, mul_neg_eq_neg_mul_symm, mul_one,
neg_neg, eq_comm, inc_matrix_one],
exact G.incidence_set_orientation_tail },
{ rw [(G.dir_inc_matrix_zero R).mpr ⟨H_head, H_tail⟩, mul_zero, eq_comm, inc_matrix_zero],
exact G.not_incidence_set_orientation H_head H_tail } }
end
-- 6. For any adjacent vertices i j and edge e, N(o) i e * N(o) j e = if e = (i,j) then -1 else 0.
lemma dir_inc_matrix_prod_of_adj {i j : V} {e : G.edge_set} (H_adj : G.adj i j) :
G.dir_inc_matrix R o i e * G.dir_inc_matrix R o j e = ite (e.val = ⟦(i, j)⟧) (-1) 0 :=
begin
by_cases H_e : e.val = ⟦(i, j)⟧,
{ rw [H_e, if_pos rfl],
rw [o.consistent e, eq_iff] at H_e,
rcases H_e with (⟨H_head_i, H_tail_j⟩ | ⟨H_head_j, H_tail_i⟩),
{ rw [G.dir_inc_matrix_head R H_head_i.symm, G.dir_inc_matrix_tail R H_tail_j.symm,
mul_neg_eq_neg_mul_symm, mul_one] },
{ rw [G.dir_inc_matrix_head R H_head_j.symm, G.dir_inc_matrix_tail R H_tail_i.symm, mul_one] } },
{ simp only [H_e, if_false],
rw [o.consistent e, eq_iff, decidable.not_or_iff_and_not] at H_e,
repeat { rw decidable.not_and_iff_or_not at H_e },
rcases H_e with ⟨(H_head_i | H_tail_j), (H_head_j | H_tail_i)⟩,
{ have H_tail : o.tail e ≠ i ∨ o.tail e ≠ j,
{ by_contradiction h,
rw [decidable.not_or_iff_and_not, not_not, not_not] at h,
rcases h with ⟨h_i, h_j⟩, rw h_i at h_j,
exact G.ne_of_adj H_adj h_j },
cases H_tail with H_tail_i H_tail_j,
{ rw [(G.dir_inc_matrix_zero R).mpr ⟨ne.symm H_head_i, ne.symm H_tail_i⟩, zero_mul] },
{ rw [(G.dir_inc_matrix_zero R).mpr ⟨ne.symm H_head_j, ne.symm H_tail_j⟩, mul_zero] } },
{ rw [(G.dir_inc_matrix_zero R).mpr ⟨ne.symm H_head_i, ne.symm H_tail_i⟩, zero_mul] },
{ rw [(G.dir_inc_matrix_zero R).mpr ⟨ne.symm H_head_j, ne.symm H_tail_j⟩, mul_zero] },
{ have H_head : o.head e ≠ i ∨ o.head e ≠ j,
{ by_contradiction h,
rw [decidable.not_or_iff_and_not, not_not, not_not] at h,
rcases h with ⟨h_i, h_j⟩, rw h_i at h_j,
exact G.ne_of_adj H_adj h_j },
cases H_head with H_head_i H_head_j,
{ rw [(G.dir_inc_matrix_zero R).mpr ⟨ne.symm H_head_i, ne.symm H_tail_i⟩, zero_mul] },
{ rw [(G.dir_inc_matrix_zero R).mpr ⟨ne.symm H_head_j, ne.symm H_tail_j⟩, mul_zero] } } }
end
-- 7. For any non-adjacent distinct vertices i j and edge e, N(o) i e * N(o) j e = 0.
lemma dir_inc_matrix_prod_non_adj {i j : V} {e : G.edge_set} (H_ij : i ≠ j) (H_not_adj : ¬ G.adj i j) :
G.dir_inc_matrix R o i e * G.dir_inc_matrix R o j e = 0 :=
begin
by_cases H₁ : G.dir_inc_matrix R o i e = 0,
{ rw [H₁, zero_mul] },
{ by_cases H₂ : G.dir_inc_matrix R o j e = 0,
{ rw [H₂, mul_zero] },
{
rcases ((G.dir_inc_matrix_non_zero R).mp H₁) with (H_head_i | H_tail_i) ;
rcases ((G.dir_inc_matrix_non_zero R).mp H₂) with (H_head_j | H_tail_j),
{ rw [H_head_i, H_head_j] at H_ij, tauto },
{ exfalso, apply H_not_adj, rw [H_head_i, H_tail_j, ← mem_edge_set, ← o.consistent e],
simp only [subtype.coe_prop, subtype.val_eq_coe] },
{ exfalso, apply H_not_adj, rw [edge_symm, H_tail_i, H_head_j, ← mem_edge_set, ← o.consistent e],
simp only [subtype.coe_prop, subtype.val_eq_coe] },
{ rw [H_tail_i, H_tail_j] at H_ij, tauto } } }
end
-- 8. (xᵀ ⬝ N) e = x head(e) - x tail(e).
lemma vec_mul_dir_inc_matrix {o : orientation G} (x : V → R) (e : G.edge_set) :
vec_mul x (G.dir_inc_matrix R o) e = x (o.head e) - x (o.tail e) :=
begin
simp only [vec_mul, dot_product, dir_inc_matrix, mul_ite, mul_one, mul_neg_eq_neg_mul_symm, mul_zero],
rw [sum_ite, sum_ite, sum_filter, sum_ite_eq', sum_const_zero, add_zero, filter_filter],
simp only [mem_univ, if_true],
have key : filter (λ (a : V), ¬a = o.head e ∧ a = o.tail e) univ = {o.tail e},
{ ext,
simp only [mem_filter, mem_singleton, true_and, and_iff_right_iff_imp, mem_univ],
intro hyp,
rw hyp,
exact ne.symm (G.head_neq_tail) },
rw [key, sum_singleton],
ring_nf
end
end simple_graph
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.