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
5b8044ca493121e51bce984b03c8cfc895c9eab9
57fdc8de88f5ea3bfde4325e6ecd13f93a274ab5
/data/real/cau_seq.lean
f39d51d814eb4da03b2b59883c7bec6768c46d98
[ "Apache-2.0" ]
permissive
louisanu/mathlib
11f56f2d40dc792bc05ee2f78ea37d73e98ecbfe
2bd5e2159d20a8f20d04fc4d382e65eea775ed39
refs/heads/master
1,617,706,993,439
1,523,163,654,000
1,523,163,654,000
124,519,997
0
0
Apache-2.0
1,520,588,283,000
1,520,588,283,000
null
UTF-8
Lean
false
false
19,971
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro A basic theory of Cauchy sequences, used in the construction of the reals. Where applicable, lemmas that will be reused in other contexts have been stated in extra generality. -/ import data.rat algebra.ordered_field class is_absolute_value {α} [discrete_linear_ordered_field α] {β} [ring β] (f : β → α) : Prop := (abv_nonneg : ∀ x, 0 ≤ f x) (abv_eq_zero : ∀ {x}, f x = 0 ↔ x = 0) (abv_add : ∀ x y, f (x + y) ≤ f x + f y) (abv_mul : ∀ x y, f (x * y) = f x * f y) namespace is_absolute_value variables {α : Type*} [discrete_linear_ordered_field α] {β : Type*} [ring β] (abv : β → α) [is_absolute_value abv] theorem abv_zero : abv 0 = 0 := (abv_eq_zero abv).2 rfl theorem abv_one' (h : (1:β) ≠ 0) : abv 1 = 1 := (domain.mul_left_inj $ mt (abv_eq_zero abv).1 h).1 $ by rw [← abv_mul abv, mul_one, mul_one] theorem abv_one {β : Type*} [domain β] (abv : β → α) [is_absolute_value abv] : abv 1 = 1 := abv_one' abv one_ne_zero theorem abv_pos {a : β} : 0 < abv a ↔ a ≠ 0 := by rw [lt_iff_le_and_ne, ne, eq_comm]; simp [abv_eq_zero abv, abv_nonneg abv] theorem abv_neg (a : β) : abv (-a) = abv a := by rw [← mul_self_inj_of_nonneg (abv_nonneg abv _) (abv_nonneg abv _), ← abv_mul abv, ← abv_mul abv]; simp theorem abv_sub (a b : β) : abv (a - b) = abv (b - a) := by rw [← neg_sub, abv_neg abv] theorem abv_inv {β : Type*} [discrete_field β] (abv : β → α) [is_absolute_value abv] (a : β) : abv a⁻¹ = (abv a)⁻¹ := classical.by_cases (λ h : a = 0, by simp [h, abv_zero abv]) (λ h, (domain.mul_left_inj (mt (abv_eq_zero abv).1 h)).1 $ by rw [← abv_mul abv]; simp [h, mt (abv_eq_zero abv).1 h, abv_one abv]) theorem abv_div {β : Type*} [discrete_field β] (abv : β → α) [is_absolute_value abv] (a b : β) : abv (a / b) = abv a / abv b := by rw [division_def, abv_mul abv, abv_inv abv]; refl lemma abv_sub_le (a b c : β) : abv (a - c) ≤ abv (a - b) + abv (b - c) := by simpa using abv_add abv (a - b) (b - c) lemma sub_abv_le_abv_sub (a b : β) : abv a - abv b ≤ abv (a - b) := sub_le_iff_le_add.2 $ by simpa using abv_add abv (a - b) b lemma abs_abv_sub_le_abv_sub (a b : β) : abs (abv a - abv b) ≤ abv (a - b) := abs_sub_le_iff.2 ⟨sub_abv_le_abv_sub abv _ _, by rw abv_sub abv; apply sub_abv_le_abv_sub abv⟩ end is_absolute_value instance abs_is_absolute_value {α} [discrete_linear_ordered_field α] : is_absolute_value (abs : α → α) := { abv_nonneg := abs_nonneg, abv_eq_zero := λ _, abs_eq_zero, abv_add := abs_add, abv_mul := abs_mul } open is_absolute_value theorem exists_forall_ge_and {α} [linear_order α] {P Q : α → Prop} : (∃ i, ∀ j ≥ i, P j) → (∃ i, ∀ j ≥ i, Q j) → ∃ i, ∀ j ≥ i, P j ∧ Q j | ⟨a, h₁⟩ ⟨b, h₂⟩ := let ⟨c, ac, bc⟩ := exists_ge_of_linear a b in ⟨c, λ j hj, ⟨h₁ _ (le_trans ac hj), h₂ _ (le_trans bc hj)⟩⟩ section variables {α : Type*} [discrete_linear_ordered_field α] {β : Type*} [ring β] (abv : β → α) [is_absolute_value abv] theorem rat_add_continuous_lemma {ε : α} (ε0 : 0 < ε) : ∃ δ > 0, ∀ {a₁ a₂ b₁ b₂ : β}, abv (a₁ - b₁) < δ → abv (a₂ - b₂) < δ → abv (a₁ + a₂ - (b₁ + b₂)) < ε := ⟨ε / 2, half_pos ε0, λ a₁ a₂ b₁ b₂ h₁ h₂, by simpa [add_halves] using lt_of_le_of_lt (abv_add abv _ _) (add_lt_add h₁ h₂)⟩ theorem rat_mul_continuous_lemma {ε K₁ K₂ : α} (ε0 : 0 < ε) (K₁0 : 0 < K₁) (K₂0 : 0 < K₂) : ∃ δ > 0, ∀ {a₁ a₂ b₁ b₂ : β}, abv a₁ < K₁ → abv b₂ < K₂ → abv (a₁ - b₁) < δ → abv (a₂ - b₂) < δ → abv (a₁ * a₂ - b₁ * b₂) < ε := begin have K0 := lt_of_lt_of_le K₁0 (le_max_left _ K₂), have εK := div_pos (half_pos ε0) K0, refine ⟨_, εK, λ a₁ a₂ b₁ b₂ ha₁ hb₂ h₁ h₂, _⟩, replace ha₁ := lt_of_lt_of_le ha₁ (le_max_left _ K₂), replace hb₂ := lt_of_lt_of_le hb₂ (le_max_right K₁ _), have := add_lt_add (mul_lt_mul' (le_of_lt h₁) hb₂ (abv_nonneg abv _) εK) (mul_lt_mul' (le_of_lt h₂) ha₁ (abv_nonneg abv _) εK), rw [← abv_mul abv, mul_comm, div_mul_cancel _ (ne_of_gt K0), ← abv_mul abv, add_halves] at this, simpa [mul_add, add_mul] using lt_of_le_of_lt (abv_add abv _ _) this end theorem rat_inv_continuous_lemma {β : Type*} [discrete_field β] (abv : β → α) [is_absolute_value abv] {ε K : α} (ε0 : 0 < ε) (K0 : 0 < K) : ∃ δ > 0, ∀ {a b : β}, K ≤ abv a → K ≤ abv b → abv (a - b) < δ → abv (a⁻¹ - b⁻¹) < ε := begin have KK := mul_pos K0 K0, have εK := mul_pos ε0 KK, refine ⟨_, εK, λ a b ha hb h, _⟩, have a0 := lt_of_lt_of_le K0 ha, have b0 := lt_of_lt_of_le K0 hb, rw [inv_sub_inv ((abv_pos abv).1 a0) ((abv_pos abv).1 b0), abv_div abv, abv_mul abv, mul_comm, abv_sub abv, ← mul_div_cancel ε (ne_of_gt KK)], exact div_lt_div h (mul_le_mul hb ha (le_of_lt K0) (abv_nonneg abv _)) (le_of_lt $ mul_pos ε0 KK) KK end end def is_cau_seq {α : Type*} [discrete_linear_ordered_field α] {β : Type*} [ring β] (abv : β → α) [is_absolute_value abv] (f : ℕ → β) := ∀ ε > 0, ∃ i, ∀ j ≥ i, abv (f j - f i) < ε namespace is_cau_seq variables {α : Type*} [discrete_linear_ordered_field α] {β : Type*} [ring β] {abv : β → α} [is_absolute_value abv] {f : ℕ → β} theorem cauchy₂ (hf : is_cau_seq abv f) {ε:α} (ε0 : ε > 0) : ∃ i, ∀ j k ≥ i, abv (f j - f k) < ε := begin refine (hf _ (half_pos ε0)).imp (λ i hi j k ij ik, _), rw ← add_halves ε, refine lt_of_le_of_lt (abv_sub_le abv _ _ _) (add_lt_add (hi _ ij) _), rw abv_sub abv, exact hi _ ik end theorem cauchy₃ (hf : is_cau_seq abv f) {ε:α} (ε0 : ε > 0) : ∃ i, ∀ j ≥ i, ∀ k ≥ j, abv (f k - f j) < ε := let ⟨i, H⟩ := hf.cauchy₂ ε0 in ⟨i, λ j ij k jk, H _ _ (le_trans ij jk) ij⟩ end is_cau_seq def cau_seq {α : Type*} [discrete_linear_ordered_field α] (β : Type*) [ring β] (abv : β → α) [is_absolute_value abv] := {f : ℕ → β // is_cau_seq abv f} namespace cau_seq variables {α : Type*} [discrete_linear_ordered_field α] section ring variables {β : Type*} [ring β] {abv : β → α} [is_absolute_value abv] instance : has_coe_to_fun (cau_seq β abv) := ⟨_, subtype.val⟩ @[simp] theorem mk_to_fun (f) (hf : is_cau_seq abv f) : @coe_fn (cau_seq β abv) _ ⟨f, hf⟩ = f := rfl theorem ext {f g : cau_seq β abv} (h : ∀ i, f i = g i) : f = g := subtype.eq (funext h) theorem is_cau (f : cau_seq β abv) : is_cau_seq abv f := f.2 theorem cauchy (f : cau_seq β abv) : ∀ {ε}, ε > 0 → ∃ i, ∀ j ≥ i, abv (f j - f i) < ε := f.2 theorem cauchy₂ (f : cau_seq β abv) {ε:α} : ε > 0 → ∃ i, ∀ j k ≥ i, abv (f j - f k) < ε := f.2.cauchy₂ theorem cauchy₃ (f : cau_seq β abv) {ε:α} : ε > 0 → ∃ i, ∀ j ≥ i, ∀ k ≥ j, abv (f k - f j) < ε := f.2.cauchy₃ theorem bounded (f : cau_seq β abv) : ∃ r, ∀ i, abv (f i) < r := begin cases f.cauchy zero_lt_one with i h, let R := (finset.range (i+1)).sum (λ j, abv (f j)), have : ∀ j ≤ i, abv (f j) ≤ R, { intros j ij, change (λ j, abv (f j)) j ≤ R, apply finset.single_le_sum, { intros, apply abv_nonneg abv }, { rwa [finset.mem_range, nat.lt_succ_iff] } }, refine ⟨R + 1, λ j, _⟩, cases lt_or_le j i with ij ij, { exact lt_of_le_of_lt (this _ (le_of_lt ij)) (lt_add_one _) }, { have := lt_of_le_of_lt (abv_add abv _ _) (add_lt_add_of_le_of_lt (this _ (le_refl _)) (h _ ij)), rw [add_sub, add_comm] at this, simpa } end theorem bounded' (f : cau_seq β abv) (x : α) : ∃ r > x, ∀ i, abv (f i) < r := let ⟨r, h⟩ := f.bounded in ⟨max r (x+1), lt_of_lt_of_le (lt_add_one _) (le_max_right _ _), λ i, lt_of_lt_of_le (h i) (le_max_left _ _)⟩ def of_eq (f : cau_seq β abv) (g : ℕ → β) (e : ∀ i, f i = g i) : cau_seq β abv := ⟨g, λ ε, by rw [show g = f, from (funext e).symm]; exact f.cauchy⟩ instance : has_add (cau_seq β abv) := ⟨λ f g, ⟨λ i, (f i + g i : β), λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_add_continuous_lemma abv ε0, ⟨i, H⟩ := exists_forall_ge_and (f.cauchy₃ δ0) (g.cauchy₃ δ0) in ⟨i, λ j ij, let ⟨H₁, H₂⟩ := H _ (le_refl _) in Hδ (H₁ _ ij) (H₂ _ ij)⟩⟩⟩ @[simp] theorem add_apply (f g : cau_seq β abv) (i : ℕ) : (f + g) i = f i + g i := rfl variable (abv) def const (x : β) : cau_seq β abv := ⟨λ i, x, λ ε ε0, ⟨0, λ j ij, by simpa [abv_zero abv] using ε0⟩⟩ variable {abv} local notation `const` := const abv @[simp] theorem const_apply (x : β) (i : ℕ) : (const x : ℕ → β) i = x := rfl theorem const_inj {x y : β} : (const x : cau_seq β abv) = const y ↔ x = y := ⟨λ h, congr_arg (λ f:cau_seq β abv, (f:ℕ→β) 0) h, congr_arg _⟩ instance : has_zero (cau_seq β abv) := ⟨const 0⟩ instance : has_one (cau_seq β abv) := ⟨const 1⟩ @[simp] theorem zero_apply (i) : (0 : cau_seq β abv) i = 0 := rfl @[simp] theorem one_apply (i) : (1 : cau_seq β abv) i = 1 := rfl theorem const_add (x y : β) : const (x + y) = const x + const y := ext $ λ i, rfl instance : has_mul (cau_seq β abv) := ⟨λ f g, ⟨λ i, (f i * g i : β), λ ε ε0, let ⟨F, F0, hF⟩ := f.bounded' 0, ⟨G, G0, hG⟩ := g.bounded' 0, ⟨δ, δ0, Hδ⟩ := rat_mul_continuous_lemma abv ε0 F0 G0, ⟨i, H⟩ := exists_forall_ge_and (f.cauchy₃ δ0) (g.cauchy₃ δ0) in ⟨i, λ j ij, let ⟨H₁, H₂⟩ := H _ (le_refl _) in Hδ (hF j) (hG i) (H₁ _ ij) (H₂ _ ij)⟩⟩⟩ @[simp] theorem mul_apply (f g : cau_seq β abv) (i : ℕ) : (f * g) i = f i * g i := rfl theorem const_mul (x y : β) : const (x * y) = const x * const y := ext $ λ i, rfl instance : has_neg (cau_seq β abv) := ⟨λ f, of_eq (const (-1) * f) (λ x, -f x) (λ i, by simp)⟩ @[simp] theorem neg_apply (f : cau_seq β abv) (i) : (-f) i = -f i := rfl theorem const_neg (x : β) : const (-x) = -const x := ext $ λ i, rfl instance : ring (cau_seq β abv) := by refine {neg := has_neg.neg, add := (+), zero := 0, mul := (*), one := 1, ..}; { intros, apply ext, simp [mul_add, mul_assoc, add_mul] } instance {β : Type*} [comm_ring β] {abv : β → α} [is_absolute_value abv] : comm_ring (cau_seq β abv) := { mul_comm := by intros; apply ext; simp [mul_left_comm, mul_comm], ..cau_seq.ring } theorem const_sub (x y : β) : const (x - y) = const x - const y := by rw [sub_eq_add_neg, const_add, const_neg, sub_eq_add_neg] @[simp] theorem sub_apply (f g : cau_seq β abv) (i : ℕ) : (f - g) i = f i - g i := rfl def lim_zero (f : cau_seq β abv) := ∀ ε > 0, ∃ i, ∀ j ≥ i, abv (f j) < ε theorem add_lim_zero {f g : cau_seq β abv} (hf : lim_zero f) (hg : lim_zero g) : lim_zero (f + g) | ε ε0 := (exists_forall_ge_and (hf _ $ half_pos ε0) (hg _ $ half_pos ε0)).imp $ λ i H j ij, let ⟨H₁, H₂⟩ := H _ ij in by simpa [add_halves ε] using lt_of_le_of_lt (abv_add abv _ _) (add_lt_add H₁ H₂) theorem mul_lim_zero (f : cau_seq β abv) {g} (hg : lim_zero g) : lim_zero (f * g) | ε ε0 := let ⟨F, F0, hF⟩ := f.bounded' 0 in (hg _ $ div_pos ε0 F0).imp $ λ i H j ij, by have := mul_lt_mul' (le_of_lt $ hF j) (H _ ij) (abv_nonneg abv _) F0; rwa [mul_comm F, div_mul_cancel _ (ne_of_gt F0), ← abv_mul abv] at this theorem neg_lim_zero {f : cau_seq β abv} (hf : lim_zero f) : lim_zero (-f) := by rw ← neg_one_mul; exact mul_lim_zero _ hf theorem sub_lim_zero {f g : cau_seq β abv} (hf : lim_zero f) (hg : lim_zero g) : lim_zero (f - g) := add_lim_zero hf (neg_lim_zero hg) theorem zero_lim_zero : lim_zero (0 : cau_seq β abv) | ε ε0 := ⟨0, λ j ij, by simpa [abv_zero abv] using ε0⟩ theorem const_lim_zero {x : β} : lim_zero (const x) ↔ x = 0 := ⟨λ H, (abv_eq_zero abv).1 $ eq_of_le_of_forall_le_of_dense (abv_nonneg abv _) $ λ ε ε0, let ⟨i, hi⟩ := H _ ε0 in le_of_lt $ hi _ (le_refl _), λ e, e.symm ▸ zero_lim_zero⟩ instance equiv : setoid (cau_seq β abv) := ⟨λ f g, lim_zero (f - g), ⟨λ f, by simp [zero_lim_zero], λ f g h, by simpa using neg_lim_zero h, λ f g h fg gh, by simpa using add_lim_zero fg gh⟩⟩ theorem equiv_def₃ {f g : cau_seq β abv} (h : f ≈ g) {ε:α} (ε0 : 0 < ε) : ∃ i, ∀ j ≥ i, ∀ k ≥ j, abv (f k - g j) < ε := (exists_forall_ge_and (h _ $ half_pos ε0) (f.cauchy₃ $ half_pos ε0)).imp $ λ i H j ij k jk, let ⟨h₁, h₂⟩ := H _ ij in by have := lt_of_le_of_lt (abv_add abv (f j - g j) _) (add_lt_add h₁ (h₂ _ jk)); rwa [sub_add_sub_cancel', add_halves] at this theorem lim_zero_congr {f g : cau_seq β abv} (h : f ≈ g) : lim_zero f ↔ lim_zero g := ⟨λ l, by simpa using add_lim_zero (setoid.symm h) l, λ l, by simpa using add_lim_zero h l⟩ theorem abv_pos_of_not_lim_zero {f : cau_seq β abv} (hf : ¬ lim_zero f) : ∃ K > 0, ∃ i, ∀ j ≥ i, K ≤ abv (f j) := begin haveI := classical.prop_decidable, by_contra nk, refine hf (λ ε ε0, _), simp [not_forall] at nk, cases f.cauchy₃ (half_pos ε0) with i hi, rcases nk _ (half_pos ε0) i with ⟨j, ij, hj⟩, refine ⟨j, λ k jk, _⟩, have := lt_of_le_of_lt (abv_add abv _ _) (add_lt_add (hi j ij k jk) hj), rwa [sub_add_cancel, add_halves] at this end theorem of_near (f : ℕ → β) (g : cau_seq β abv) (h : ∀ ε > 0, ∃ i, ∀ j ≥ i, abv (f j - g j) < ε) : is_cau_seq abv f | ε ε0 := let ⟨i, hi⟩ := exists_forall_ge_and (h _ (half_pos $ half_pos ε0)) (g.cauchy₃ $ half_pos ε0) in ⟨i, λ j ij, begin cases hi _ (le_refl _) with h₁ h₂, rw abv_sub abv at h₁, have := lt_of_le_of_lt (abv_add abv _ _) (add_lt_add (hi _ ij).1 h₁), have := lt_of_le_of_lt (abv_add abv _ _) (add_lt_add this (h₂ _ ij)), rwa [add_halves, add_halves, add_right_comm, sub_add_sub_cancel, sub_add_sub_cancel] at this end⟩ end ring section discrete_field variables {β : Type*} [discrete_field β] {abv : β → α} [is_absolute_value abv] theorem inv_aux {f : cau_seq β abv} (hf : ¬ lim_zero f) : ∀ ε > 0, ∃ i, ∀ j ≥ i, abv ((f j)⁻¹ - (f i)⁻¹) < ε | ε ε0 := let ⟨K, K0, HK⟩ := abv_pos_of_not_lim_zero hf, ⟨δ, δ0, Hδ⟩ := rat_inv_continuous_lemma abv ε0 K0, ⟨i, H⟩ := exists_forall_ge_and HK (f.cauchy₃ δ0) in ⟨i, λ j ij, let ⟨iK, H'⟩ := H _ (le_refl _) in Hδ (H _ ij).1 iK (H' _ ij)⟩ def inv (f) (hf : ¬ lim_zero f) : cau_seq β abv := ⟨_, inv_aux hf⟩ @[simp] theorem inv_apply {f : cau_seq β abv} (hf i) : inv f hf i = (f i)⁻¹ := rfl theorem inv_mul_cancel {f : cau_seq β abv} (hf) : inv f hf * f ≈ 1 := λ ε ε0, let ⟨K, K0, i, H⟩ := abv_pos_of_not_lim_zero hf in ⟨i, λ j ij, by simpa [(abv_pos abv).1 (lt_of_lt_of_le K0 (H _ ij)), abv_zero abv] using ε0⟩ end discrete_field section abs local notation `const` := const abs def pos (f : cau_seq α abs) : Prop := ∃ K > 0, ∃ i, ∀ j ≥ i, K ≤ f j theorem not_lim_zero_of_pos {f : cau_seq α abs} : pos f → ¬ lim_zero f | ⟨F, F0, hF⟩ H := let ⟨i, h⟩ := exists_forall_ge_and hF (H _ F0), ⟨h₁, h₂⟩ := h _ (le_refl _) in not_lt_of_le h₁ (abs_lt.1 h₂).2 theorem const_pos {x : α} : pos (const x) ↔ 0 < x := ⟨λ ⟨K, K0, i, h⟩, lt_of_lt_of_le K0 (h _ (le_refl _)), λ h, ⟨x, h, 0, λ j _, le_refl _⟩⟩ theorem add_pos {f g : cau_seq α abs} : pos f → pos g → pos (f + g) | ⟨F, F0, hF⟩ ⟨G, G0, hG⟩ := let ⟨i, h⟩ := exists_forall_ge_and hF hG in ⟨_, _root_.add_pos F0 G0, i, λ j ij, let ⟨h₁, h₂⟩ := h _ ij in add_le_add h₁ h₂⟩ theorem pos_add_lim_zero {f g : cau_seq α abs} : pos f → lim_zero g → pos (f + g) | ⟨F, F0, hF⟩ H := let ⟨i, h⟩ := exists_forall_ge_and hF (H _ (half_pos F0)) in ⟨_, half_pos F0, i, λ j ij, begin cases h j ij with h₁ h₂, have := add_le_add h₁ (le_of_lt (abs_lt.1 h₂).1), rwa [← sub_eq_add_neg, sub_self_div_two] at this end⟩ theorem mul_pos {f g : cau_seq α abs} : pos f → pos g → pos (f * g) | ⟨F, F0, hF⟩ ⟨G, G0, hG⟩ := let ⟨i, h⟩ := exists_forall_ge_and hF hG in ⟨_, _root_.mul_pos F0 G0, i, λ j ij, let ⟨h₁, h₂⟩ := h _ ij in mul_le_mul h₁ h₂ (le_of_lt G0) (le_trans (le_of_lt F0) h₁)⟩ theorem trichotomy (f : cau_seq α abs) : pos f ∨ lim_zero f ∨ pos (-f) := begin cases classical.em (lim_zero f); simp *, rcases abv_pos_of_not_lim_zero h with ⟨K, K0, hK⟩, rcases exists_forall_ge_and hK (f.cauchy₃ K0) with ⟨i, hi⟩, refine (le_total 0 (f i)).imp _ _; refine (λ h, ⟨K, K0, i, λ j ij, _⟩); have := (hi _ ij).1; cases hi _ (le_refl _) with h₁ h₂, { rwa abs_of_nonneg at this, rw abs_of_nonneg h at h₁, exact (le_add_iff_nonneg_right _).1 (le_trans h₁ $ neg_le_sub_iff_le_add'.1 $ le_of_lt (abs_lt.1 $ h₂ _ ij).1) }, { rwa abs_of_nonpos at this, rw abs_of_nonpos h at h₁, rw [← sub_le_sub_iff_right, zero_sub], exact le_trans (le_of_lt (abs_lt.1 $ h₂ _ ij).2) h₁ } end instance : has_lt (cau_seq α abs) := ⟨λ f g, pos (g - f)⟩ instance : has_le (cau_seq α abs) := ⟨λ f g, f < g ∨ f ≈ g⟩ theorem lt_of_lt_of_eq {f g h : cau_seq α abs} (fg : f < g) (gh : g ≈ h) : f < h := by simpa using pos_add_lim_zero fg (neg_lim_zero gh) theorem lt_of_eq_of_lt {f g h : cau_seq α abs} (fg : f ≈ g) (gh : g < h) : f < h := by have := pos_add_lim_zero gh (neg_lim_zero fg); rwa [← sub_eq_add_neg, sub_sub_sub_cancel_right] at this theorem lt_trans {f g h : cau_seq α abs} (fg : f < g) (gh : g < h) : f < h := by simpa using add_pos fg gh theorem lt_irrefl {f : cau_seq α abs} : ¬ f < f | h := not_lim_zero_of_pos h (by simp [zero_lim_zero]) instance : preorder (cau_seq α abs) := { lt := (<), le := λ f g, f < g ∨ f ≈ g, le_refl := λ f, or.inr (setoid.refl _), le_trans := λ f g h fg, match fg with | or.inl fg, or.inl gh := or.inl $ lt_trans fg gh | or.inl fg, or.inr gh := or.inl $ lt_of_lt_of_eq fg gh | or.inr fg, or.inl gh := or.inl $ lt_of_eq_of_lt fg gh | or.inr fg, or.inr gh := or.inr $ setoid.trans fg gh end, lt_iff_le_not_le := λ f g, ⟨λ h, ⟨or.inl h, not_or (mt (lt_trans h) lt_irrefl) (not_lim_zero_of_pos h)⟩, λ ⟨h₁, h₂⟩, h₁.resolve_right (mt (λ h, or.inr (setoid.symm h)) h₂)⟩ } theorem le_antisymm {f g : cau_seq α abs} (fg : f ≤ g) (gf : g ≤ f) : f ≈ g := fg.resolve_left (not_lt_of_le gf) theorem lt_total (f g : cau_seq α abs) : f < g ∨ f ≈ g ∨ g < f := (trichotomy (g - f)).imp_right (λ h, h.imp (λ h, setoid.symm h) (λ h, by rwa neg_sub at h)) theorem le_total (f g : cau_seq α abs) : f ≤ g ∨ g ≤ f := (or.assoc.2 (lt_total f g)).imp_right or.inl theorem const_lt {x y : α} : const x < const y ↔ x < y := show pos _ ↔ _, by rw [← const_sub, const_pos, sub_pos] theorem const_equiv {x y : α} : const x ≈ const y ↔ x = y := show lim_zero _ ↔ _, by rw [← const_sub, const_lim_zero, sub_eq_zero] theorem const_le {x y : α} : const x ≤ const y ↔ x ≤ y := by rw le_iff_lt_or_eq; exact or_congr const_lt const_equiv theorem exists_gt (f : cau_seq α abs) : ∃ a : α, f < const a := let ⟨K, H⟩ := f.bounded in ⟨K + 1, 1, zero_lt_one, 0, λ i _, begin rw [sub_apply, const_apply, le_sub_iff_add_le', add_le_add_iff_right], exact le_of_lt (abs_lt.1 (H _)).2 end⟩ theorem exists_lt (f : cau_seq α abs) : ∃ a : α, const a < f := let ⟨a, h⟩ := (-f).exists_gt in ⟨-a, show pos _, by rwa [const_neg, sub_neg_eq_add, add_comm, ← sub_neg_eq_add]⟩ end abs end cau_seq
1fda3dc56f2b539575a6953852aa16222b80af81
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/linear_algebra/basis.lean
188e73be1e37f049b68f496eb27c3e680c1873a8
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
20,659
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, Alexander Bentkamp -/ import linear_algebra.linear_independent import linear_algebra.projection import linear_algebra.linear_pmap import data.fintype.card /-! # Bases This file defines bases in a module or vector space. It is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light. ## Main definitions All definitions are given for families of vectors, i.e. `v : ι → M` where `M` is the module or vector space and `ι : Type*` is an arbitrary indexing type. * `is_basis R v` states that the vector family `v` is a basis, i.e. it is linearly independent and spans the entire space. * `is_basis.repr hv x` is the basis version of `linear_independent.repr hv x`. It returns the linear combination representing `x : M` on a basis `v` of `M` (using classical choice). The argument `hv` must be a proof that `is_basis R v`. `is_basis.repr hv` is given as a linear map as well. * `is_basis.constr hv f` constructs a linear map `M₁ →ₗ[R] M₂` given the values `f : ι → M₂` at the basis `v : ι → M₁`, given `hv : is_basis R v`. ## Main statements * `is_basis.ext` states that two linear maps are equal if they coincide on a basis. * `exists_is_basis` states that every vector space has a basis. ## Implementation notes We use families instead of sets because it allows us to say that two identical vectors are linearly dependent. For bases, this is useful as well because we can easily derive ordered bases by using an ordered index type `ι`. ## Tags basis, bases -/ noncomputable theory universe u open function set submodule open_locale classical big_operators variables {ι : Type*} {ι' : Type*} {R : Type*} {K : Type*} variables {M : Type*} {M' M'' : Type*} {V : Type u} {V' : Type*} section module open linear_map variables {v : ι → M} variables [ring R] [add_comm_group M] [add_comm_group M'] [add_comm_group M''] variables [module R M] [module R M'] [module R M''] variables {a b : R} {x y : M} variables (R) (v) /-- A family of vectors is a basis if it is linearly independent and all vectors are in the span. -/ def is_basis := linear_independent R v ∧ span R (range v) = ⊤ variables {R} {v} section is_basis variables {s t : set M} (hv : is_basis R v) lemma is_basis.mem_span (hv : is_basis R v) : ∀ x, x ∈ span R (range v) := eq_top_iff'.1 hv.2 lemma is_basis.comp (hv : is_basis R v) (f : ι' → ι) (hf : bijective f) : is_basis R (v ∘ f) := begin split, { apply hv.1.comp f hf.1 }, { rw[set.range_comp, range_iff_surjective.2 hf.2, image_univ, hv.2] } end lemma is_basis.injective [nontrivial R] (hv : is_basis R v) : injective v := λ x y h, linear_independent.injective hv.1 h lemma is_basis.range (hv : is_basis R v) : is_basis R (λ x, x : range v → M) := ⟨hv.1.to_subtype_range, by { convert hv.2, ext i, exact ⟨λ ⟨p, hp⟩, hp ▸ p.2, λ hi, ⟨⟨i, hi⟩, rfl⟩⟩ }⟩ /-- Given a basis, any vector can be written as a linear combination of the basis vectors. They are given by this linear map. This is one direction of `module_equiv_finsupp`. -/ def is_basis.repr : M →ₗ (ι →₀ R) := (hv.1.repr).comp (linear_map.id.cod_restrict _ hv.mem_span) lemma is_basis.total_repr (x) : finsupp.total ι M R v (hv.repr x) = x := hv.1.total_repr ⟨x, _⟩ lemma is_basis.total_comp_repr : (finsupp.total ι M R v).comp hv.repr = linear_map.id := linear_map.ext hv.total_repr lemma is_basis.ext {f g : M →ₗ[R] M'} (hv : is_basis R v) (h : ∀i, f (v i) = g (v i)) : f = g := linear_map.ext_on_range hv.2 h lemma is_basis.repr_ker : hv.repr.ker = ⊥ := linear_map.ker_eq_bot.2 $ left_inverse.injective hv.total_repr lemma is_basis.repr_range : hv.repr.range = finsupp.supported R R univ := by rw [is_basis.repr, linear_map.range, submodule.map_comp, linear_map.map_cod_restrict, submodule.map_id, comap_top, map_top, hv.1.repr_range, finsupp.supported_univ] lemma is_basis.repr_total (x : ι →₀ R) (hx : x ∈ finsupp.supported R R (univ : set ι)) : hv.repr (finsupp.total ι M R v x) = x := begin rw [← hv.repr_range, linear_map.mem_range] at hx, cases hx with w hw, rw [← hw, hv.total_repr], end lemma is_basis.repr_eq_single {i} : hv.repr (v i) = finsupp.single i 1 := by apply hv.1.repr_eq_single; simp @[simp] lemma is_basis.repr_self_apply (i j : ι) : hv.repr (v i) j = if i = j then 1 else 0 := by rw [hv.repr_eq_single, finsupp.single_apply] lemma is_basis.repr_eq_iff {f : M →ₗ[R] (ι →₀ R)} : hv.repr = f ↔ ∀ i, f (v i) = finsupp.single i 1 := begin split, { rintros rfl i, exact hv.repr_eq_single }, intro h, refine hv.ext (λ _, _), rw [h, hv.repr_eq_single] end lemma is_basis.repr_apply_eq {f : M → ι → R} (hadd : ∀ x y, f (x + y) = f x + f y) (hsmul : ∀ (c : R) (x : M), f (c • x) = c • f x) (f_eq : ∀ i, f (v i) = finsupp.single i 1) (x : M) (i : ι) : hv.repr x i = f x i := begin let f_i : M →ₗ[R] R := { to_fun := λ x, f x i, map_add' := λ _ _, by rw [hadd, pi.add_apply], map_smul' := λ _ _, by rw [hsmul, pi.smul_apply] }, show (finsupp.lapply i).comp hv.repr x = f_i x, congr' 1, refine hv.ext (λ j, _), show hv.repr (v j) i = f (v j) i, rw [hv.repr_eq_single, f_eq] end lemma is_basis.range_repr_self (i : ι) : hv.range.repr (v i) = finsupp.single ⟨v i, mem_range_self i⟩ 1 := hv.1.to_subtype_range.repr_eq_single _ _ rfl @[simp] lemma is_basis.range_repr (i : ι) : hv.range.repr x ⟨v i, mem_range_self i⟩ = hv.repr x i := begin by_cases H : (0 : R) = 1, { exact eq_of_zero_eq_one H _ _ }, refine (hv.repr_apply_eq _ _ _ x i).symm, { intros x y, ext j, rw [linear_map.map_add, finsupp.add_apply], refl }, { intros c x, ext j, rw [linear_map.map_smul, finsupp.smul_apply], refl }, { intro i, ext j, haveI : nontrivial R := ⟨⟨0, 1, H⟩⟩, simp [hv.range_repr_self, finsupp.single_apply, hv.injective.eq_iff] } end /-- Construct a linear map given the value at the basis. -/ def is_basis.constr (f : ι → M') : M →ₗ[R] M' := (finsupp.total M' M' R id).comp $ (finsupp.lmap_domain R R f).comp hv.repr theorem is_basis.constr_apply (f : ι → M') (x : M) : (hv.constr f : M → M') x = (hv.repr x).sum (λb a, a • f b) := by dsimp [is_basis.constr] ; rw [finsupp.total_apply, finsupp.sum_map_domain_index]; simp [add_smul] @[simp] lemma constr_basis {f : ι → M'} {i : ι} (hv : is_basis R v) : (hv.constr f : M → M') (v i) = f i := by simp [is_basis.constr_apply, hv.repr_eq_single, finsupp.sum_single_index] lemma constr_eq {g : ι → M'} {f : M →ₗ[R] M'} (hv : is_basis R v) (h : ∀i, g i = f (v i)) : hv.constr g = f := hv.ext $ λ i, (constr_basis hv).trans (h i) lemma constr_self (f : M →ₗ[R] M') : hv.constr (λ i, f (v i)) = f := constr_eq hv $ λ x, rfl lemma constr_zero (hv : is_basis R v) : hv.constr (λi, (0 : M')) = 0 := constr_eq hv $ λ x, rfl lemma constr_add {g f : ι → M'} (hv : is_basis R v) : hv.constr (λi, f i + g i) = hv.constr f + hv.constr g := constr_eq hv $ λ b, by simp lemma constr_neg {f : ι → M'} (hv : is_basis R v) : hv.constr (λi, - f i) = - hv.constr f := constr_eq hv $ λ b, by simp lemma constr_sub {g f : ι → M'} (hs : is_basis R v) : hv.constr (λi, f i - g i) = hs.constr f - hs.constr g := by simp [sub_eq_add_neg, constr_add, constr_neg] -- this only works on functions if `R` is a commutative ring lemma constr_smul {ι R M} [comm_ring R] [add_comm_group M] [module R M] {v : ι → R} {f : ι → M} {a : R} (hv : is_basis R v) : hv.constr (λb, a • f b) = a • hv.constr f := constr_eq hv $ by simp [constr_basis hv] {contextual := tt} lemma constr_range [nonempty ι] (hv : is_basis R v) {f : ι → M'} : (hv.constr f).range = span R (range f) := by rw [is_basis.constr, linear_map.range_comp, linear_map.range_comp, is_basis.repr_range, finsupp.lmap_domain_supported, ←set.image_univ, ←finsupp.span_eq_map_total, image_id] /-- Canonical equivalence between a module and the linear combinations of basis vectors. -/ def module_equiv_finsupp (hv : is_basis R v) : M ≃ₗ[R] ι →₀ R := (hv.1.total_equiv.trans (linear_equiv.of_top _ hv.2)).symm @[simp] theorem module_equiv_finsupp_apply_basis (hv : is_basis R v) (i : ι) : module_equiv_finsupp hv (v i) = finsupp.single i 1 := (linear_equiv.symm_apply_eq _).2 $ by simp [linear_independent.total_equiv] /-- Isomorphism between the two modules, given two modules `M` and `M'` with respective bases `v` and `v'` and a bijection between the indexing sets of the two bases. -/ def linear_equiv_of_is_basis {v : ι → M} {v' : ι' → M'} (hv : is_basis R v) (hv' : is_basis R v') (e : ι ≃ ι') : M ≃ₗ[R] M' := { inv_fun := hv'.constr (v ∘ e.symm), left_inv := have (hv'.constr (v ∘ e.symm)).comp (hv.constr (v' ∘ e)) = linear_map.id, from hv.ext $ by simp, λ x, congr_arg (λ h : M →ₗ[R] M, h x) this, right_inv := have (hv.constr (v' ∘ e)).comp (hv'.constr (v ∘ e.symm)) = linear_map.id, from hv'.ext $ by simp, λ y, congr_arg (λ h : M' →ₗ[R] M', h y) this, ..hv.constr (v' ∘ e) } /-- Isomorphism between the two modules, given two modules `M` and `M'` with respective bases `v` and `v'` and a bijection between the two bases. -/ def linear_equiv_of_is_basis' {v : ι → M} {v' : ι' → M'} (f : M → M') (g : M' → M) (hv : is_basis R v) (hv' : is_basis R v') (hf : ∀i, f (v i) ∈ range v') (hg : ∀i, g (v' i) ∈ range v) (hgf : ∀i, g (f (v i)) = v i) (hfg : ∀i, f (g (v' i)) = v' i) : M ≃ₗ M' := { inv_fun := hv'.constr (g ∘ v'), left_inv := have (hv'.constr (g ∘ v')).comp (hv.constr (f ∘ v)) = linear_map.id, from hv.ext $ λ i, exists.elim (hf i) (λ i' hi', by simp [constr_basis, hi'.symm]; rw [hi', hgf]), λ x, congr_arg (λ h:M →ₗ[R] M, h x) this, right_inv := have (hv.constr (f ∘ v)).comp (hv'.constr (g ∘ v')) = linear_map.id, from hv'.ext $ λ i', exists.elim (hg i') (λ i hi, by simp [constr_basis, hi.symm]; rw [hi, hfg]), λ y, congr_arg (λ h:M' →ₗ[R] M', h y) this, ..hv.constr (f ∘ v) } @[simp] lemma linear_equiv_of_is_basis_comp {ι'' : Type*} {v : ι → M} {v' : ι' → M'} {v'' : ι'' → M''} (hv : is_basis R v) (hv' : is_basis R v') (hv'' : is_basis R v'') (e : ι ≃ ι') (f : ι' ≃ ι'' ) : (linear_equiv_of_is_basis hv hv' e).trans (linear_equiv_of_is_basis hv' hv'' f) = linear_equiv_of_is_basis hv hv'' (e.trans f) := begin apply linear_equiv.injective_to_linear_map, apply hv.ext, intros i, simp [linear_equiv_of_is_basis] end @[simp] lemma linear_equiv_of_is_basis_refl : linear_equiv_of_is_basis hv hv (equiv.refl ι) = linear_equiv.refl R M := begin apply linear_equiv.injective_to_linear_map, apply hv.ext, intros i, simp [linear_equiv_of_is_basis] end lemma linear_equiv_of_is_basis_trans_symm (e : ι ≃ ι') {v' : ι' → M'} (hv' : is_basis R v') : (linear_equiv_of_is_basis hv hv' e).trans (linear_equiv_of_is_basis hv' hv e.symm) = linear_equiv.refl R M := by simp lemma linear_equiv_of_is_basis_symm_trans (e : ι ≃ ι') {v' : ι' → M'} (hv' : is_basis R v') : (linear_equiv_of_is_basis hv' hv e.symm).trans (linear_equiv_of_is_basis hv hv' e) = linear_equiv.refl R M' := by simp lemma is_basis_inl_union_inr {v : ι → M} {v' : ι' → M'} (hv : is_basis R v) (hv' : is_basis R v') : is_basis R (sum.elim (inl R M M' ∘ v) (inr R M M' ∘ v')) := begin split, apply linear_independent_inl_union_inr' hv.1 hv'.1, rw [sum.elim_range, span_union, set.range_comp, span_image (inl R M M'), hv.2, map_top, set.range_comp, span_image (inr R M M'), hv'.2, map_top], exact linear_map.sup_range_inl_inr end end is_basis lemma is_basis_singleton_one (R : Type*) [unique ι] [ring R] : is_basis R (λ (_ : ι), (1 : R)) := begin split, { refine linear_independent_iff.2 (λ l hl, _), rw [finsupp.total_unique, smul_eq_mul, mul_one] at hl, exact finsupp.unique_ext hl }, { refine top_unique (λ _ _, _), simp only [mem_span_singleton, range_const, mul_one, exists_eq, smul_eq_mul] } end protected lemma linear_equiv.is_basis (hs : is_basis R v) (f : M ≃ₗ[R] M') : is_basis R (f ∘ v) := begin split, { simpa only using hs.1.map' (f : M →ₗ[R] M') f.ker }, { rw [set.range_comp, ← linear_equiv.coe_coe, span_image, hs.2, map_top, f.range] } end lemma is_basis_span (hs : linear_independent R v) : @is_basis ι R (span R (range v)) (λ i : ι, ⟨v i, subset_span (mem_range_self _)⟩) _ _ _ := begin split, { apply linear_independent_span hs }, { rw eq_top_iff', intro x, have h₁ : subtype.val '' set.range (λ i, subtype.mk (v i) _) = range v, by rw ←set.range_comp, have h₂ : map (submodule.subtype _) (span R (set.range (λ i, subtype.mk (v i) _))) = span R (range v), by rw [←span_image, submodule.subtype_eq_val, h₁], have h₃ : (x : M) ∈ map (submodule.subtype _) (span R (set.range (λ i, subtype.mk (v i) _))), by rw h₂; apply subtype.mem x, rcases mem_map.1 h₃ with ⟨y, hy₁, hy₂⟩, have h_x_eq_y : x = y, by rw [subtype.ext_iff, ← hy₂]; simp, rw h_x_eq_y, exact hy₁ } end variables (M) lemma is_basis_empty [subsingleton M] (h_empty : ¬ nonempty ι) : is_basis R (λ x : ι, (0 : M)) := ⟨ linear_independent_empty_type h_empty, subsingleton.elim _ _ ⟩ variables {M} open fintype variables [fintype ι] (h : is_basis R v) /-- A module over `R` with a finite basis is linearly equivalent to functions from its basis to `R`. -/ def is_basis.equiv_fun : M ≃ₗ[R] (ι → R) := linear_equiv.trans (module_equiv_finsupp h) { to_fun := coe_fn, map_add' := finsupp.coe_add, map_smul' := finsupp.coe_smul, ..finsupp.equiv_fun_on_fintype } /-- A module over a finite ring that admits a finite basis is finite. -/ def module.fintype_of_fintype [fintype R] : fintype M := fintype.of_equiv _ h.equiv_fun.to_equiv.symm theorem module.card_fintype [fintype R] [fintype M] : card M = (card R) ^ (card ι) := calc card M = card (ι → R) : card_congr h.equiv_fun.to_equiv ... = card R ^ card ι : card_fun /-- Given a basis `v` indexed by `ι`, the canonical linear equivalence between `ι → R` and `M` maps a function `x : ι → R` to the linear combination `∑_i x i • v i`. -/ @[simp] lemma is_basis.equiv_fun_symm_apply (x : ι → R) : h.equiv_fun.symm x = ∑ i, x i • v i := begin change finsupp.sum ((finsupp.equiv_fun_on_fintype.symm : (ι → R) ≃ (ι →₀ R)) x) (λ (i : ι) (a : R), a • v i) = ∑ i, x i • v i, dsimp [finsupp.equiv_fun_on_fintype, finsupp.sum], rw finset.sum_filter, refine finset.sum_congr rfl (λi hi, _), by_cases H : x i = 0; simp [H] end lemma is_basis.equiv_fun_apply (u : M) : h.equiv_fun u = h.repr u := rfl lemma is_basis.equiv_fun_total (u : M) : ∑ i, h.equiv_fun u i • v i = u:= begin conv_rhs { rw ← h.total_repr u }, simp [finsupp.total_apply, finsupp.sum_fintype, h.equiv_fun_apply] end @[simp] lemma is_basis.equiv_fun_self (i j : ι) : h.equiv_fun (v i) j = if i = j then 1 else 0 := by { rw [h.equiv_fun_apply, h.repr_self_apply] } @[simp] theorem is_basis.constr_apply_fintype (f : ι → M') (x : M) : (h.constr f : M → M') x = ∑ i, (h.equiv_fun x i) • f i := by simp [h.constr_apply, h.equiv_fun_apply, finsupp.sum_fintype] end module section vector_space variables [field K] [add_comm_group V] [add_comm_group V'] [vector_space K V] [vector_space K V'] variables {v : ι → V} {s t : set V} {x y z : V} include K open submodule lemma exists_subset_is_basis (hs : linear_independent K (λ x, x : s → V)) : ∃b, s ⊆ b ∧ is_basis K (coe : b → V) := let ⟨b, hb₀, hx, hb₂, hb₃⟩ := exists_linear_independent hs (@subset_univ _ _) in ⟨ b, hx, @linear_independent.restrict_of_comp_subtype _ _ _ id _ _ _ _ hb₃, by simp; exact eq_top_iff.2 hb₂⟩ lemma exists_sum_is_basis (hs : linear_independent K v) : ∃ (ι' : Type u) (v' : ι' → V), is_basis K (sum.elim v v') := begin -- This is a hack: we jump through hoops to reuse `exists_subset_is_basis`. let s := set.range v, let e : ι ≃ s := equiv.set.range v hs.injective, have : (λ x, x : s → V) = v ∘ e.symm := by { funext, dsimp, rw [equiv.set.apply_range_symm v], }, have : linear_independent K (λ x, x : s → V), { rw this, exact linear_independent.comp hs _ (e.symm.injective), }, obtain ⟨b, ss, is⟩ := exists_subset_is_basis this, let e' : ι ⊕ (b \ s : set V) ≃ b := calc ι ⊕ (b \ s : set V) ≃ s ⊕ (b \ s : set V) : equiv.sum_congr e (equiv.refl _) ... ≃ b : equiv.set.sum_diff_subset ss, refine ⟨(b \ s : set V), λ x, x.1, _⟩, convert is_basis.comp is e' _, { funext x, cases x; simp; refl, }, { exact e'.bijective, }, end variables (K V) lemma exists_is_basis : ∃b : set V, is_basis K (λ i, i : b → V) := let ⟨b, _, hb⟩ := exists_subset_is_basis (linear_independent_empty K V : _) in ⟨b, hb⟩ variables {K V} lemma linear_map.exists_left_inverse_of_injective (f : V →ₗ[K] V') (hf_inj : f.ker = ⊥) : ∃g:V' →ₗ V, g.comp f = linear_map.id := begin rcases exists_is_basis K V with ⟨B, hB⟩, have hB₀ : _ := hB.1.to_subtype_range, have : linear_independent K (λ x, x : f '' B → V'), { have h₁ := hB₀.image_subtype (show disjoint (span K (range (λ i : B, i.val))) (linear_map.ker f), by simp [hf_inj]), rwa subtype.range_coe at h₁ }, rcases exists_subset_is_basis this with ⟨C, BC, hC⟩, haveI : inhabited V := ⟨0⟩, use hC.constr (C.restrict (inv_fun f)), refine hB.ext (λ b, _), rw image_subset_iff at BC, have : f b = (⟨f b, BC b.2⟩ : C) := rfl, dsimp, rw [this, constr_basis hC], exact left_inverse_inv_fun (linear_map.ker_eq_bot.1 hf_inj) _ end lemma submodule.exists_is_compl (p : submodule K V) : ∃ q : submodule K V, is_compl p q := let ⟨f, hf⟩ := p.subtype.exists_left_inverse_of_injective p.ker_subtype in ⟨f.ker, linear_map.is_compl_of_proj $ linear_map.ext_iff.1 hf⟩ instance vector_space.submodule.is_complemented : is_complemented (submodule K V) := ⟨submodule.exists_is_compl⟩ lemma linear_map.exists_right_inverse_of_surjective (f : V →ₗ[K] V') (hf_surj : f.range = ⊤) : ∃g:V' →ₗ V, f.comp g = linear_map.id := begin rcases exists_is_basis K V' with ⟨C, hC⟩, haveI : inhabited V := ⟨0⟩, use hC.constr (C.restrict (inv_fun f)), refine hC.ext (λ c, _), simp [constr_basis hC, right_inverse_inv_fun (linear_map.range_eq_top.1 hf_surj) c] end /-- Any linear map `f : p →ₗ[K] V'` defined on a subspace `p` can be extended to the whole space. -/ lemma linear_map.exists_extend {p : submodule K V} (f : p →ₗ[K] V') : ∃ g : V →ₗ[K] V', g.comp p.subtype = f := let ⟨g, hg⟩ := p.subtype.exists_left_inverse_of_injective p.ker_subtype in ⟨f.comp g, by rw [linear_map.comp_assoc, hg, f.comp_id]⟩ open submodule linear_map /-- If `p < ⊤` is a subspace of a vector space `V`, then there exists a nonzero linear map `f : V →ₗ[K] K` such that `p ≤ ker f`. -/ lemma submodule.exists_le_ker_of_lt_top (p : submodule K V) (hp : p < ⊤) : ∃ f ≠ (0 : V →ₗ[K] K), p ≤ ker f := begin rcases submodule.exists_of_lt hp with ⟨v, -, hpv⟩, clear hp, rcases (linear_pmap.sup_span_singleton ⟨p, 0⟩ v (1 : K) hpv).to_fun.exists_extend with ⟨f, hf⟩, refine ⟨f, _, _⟩, { rintro rfl, rw [linear_map.zero_comp] at hf, have := linear_pmap.sup_span_singleton_apply_mk ⟨p, 0⟩ v (1 : K) hpv 0 p.zero_mem 1, simpa using (linear_map.congr_fun hf _).trans this }, { refine λ x hx, mem_ker.2 _, have := linear_pmap.sup_span_singleton_apply_mk ⟨p, 0⟩ v (1 : K) hpv x hx 0, simpa using (linear_map.congr_fun hf _).trans this } end theorem quotient_prod_linear_equiv (p : submodule K V) : nonempty ((p.quotient × p) ≃ₗ[K] V) := let ⟨q, hq⟩ := p.exists_is_compl in nonempty.intro $ ((quotient_equiv_of_is_compl p q hq).prod (linear_equiv.refl _ _)).trans (prod_equiv_of_is_compl q p hq.symm) open fintype variables (K) (V) theorem vector_space.card_fintype [fintype K] [fintype V] : ∃ n : ℕ, card V = (card K) ^ n := exists.elim (exists_is_basis K V) $ λ b hb, ⟨card b, module.card_fintype hb⟩ end vector_space
7d71d2c1d530fcfa057a4bdb212fea2183156099
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/1681.lean
70db944cf01ec5479463713456664abd5700798f
[ "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
326
lean
def prod.foo (s : bool × bool) (fst : bool) : bool × bool := match s with (a, b) := (fst, b) end variable s : bool × bool #check prod.foo s #check prod.foo s tt #check s.foo #check s.foo tt def prod.foo2 {α β} (s : α × β) (fst : α) : α × β := match s with (a, b) := (fst, b) end #check s.foo2 #check s.foo2 tt
b512bf4c44b778a5434db800c711e27e52c62c66
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/sites/pretopology.lean
8a3af8eeec0c79d5c3925b2040406547f90f9d11
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
6,643
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.sites.grothendieck import Mathlib.PostPort universes v u l namespace Mathlib /-! # Grothendieck pretopologies Definition and lemmas about Grothendieck pretopologies. A Grothendieck pretopology for a category `C` is a set of families of morphisms with fixed codomain, satisfying certain closure conditions. We show that a pretopology generates a genuine Grothendieck topology, and every topology has a maximal pretopology which generates it. The pretopology associated to a topological space is defined in `spaces.lean`. ## Tags coverage, pretopology, site ## References * [https://ncatlab.org/nlab/show/Grothendieck+pretopology][nlab] * [S. MacLane, I. Moerdijk, *Sheaves in Geometry and Logic*][MM92] * [https://stacks.math.columbia.edu/tag/00VG][Stacks] -/ namespace category_theory /-- Pullback a set of arrows with given codomain along a fixed map, by taking the pullback in the category. This is not the same as the arrow set of `sieve.pullback`, but there is a relation between them in `pullback_arrows_comm`. -/ inductive pullback_arrows {C : Type u} [category C] [limits.has_pullbacks C] {X : C} {Y : C} (f : Y ⟶ X) (S : presieve X) : presieve Y where | mk : ∀ (Z : C) (h : Z ⟶ X), S h → pullback_arrows f S limits.pullback.snd theorem pullback_arrows_comm {C : Type u} [category C] [limits.has_pullbacks C] {X : C} {Y : C} (f : Y ⟶ X) (R : presieve X) : sieve.generate (pullback_arrows f R) = sieve.pullback f (sieve.generate R) := sorry theorem pullback_singleton {C : Type u} [category C] [limits.has_pullbacks C] {X : C} {Y : C} {Z : C} (f : Y ⟶ X) (g : Z ⟶ X) : pullback_arrows f (presieve.singleton g) = presieve.singleton limits.pullback.snd := sorry /-- A (Grothendieck) pretopology on `C` consists of a collection of families of morphisms with a fixed target `X` for every object `X` in `C`, called "coverings" of `X`, which satisfies the following three axioms: 1. Every family consisting of a single isomorphism is a covering family. 2. The collection of covering families is stable under pullback. 3. Given a covering family, and a covering family on each domain of the former, the composition is a covering family. In some sense, a pretopology can be seen as Grothendieck topology with weaker saturation conditions, in that each covering is not necessarily downward closed. See: https://ncatlab.org/nlab/show/Grothendieck+pretopology, or https://stacks.math.columbia.edu/tag/00VH, or [MM92] Chapter III, Section 2, Definition 2. Note that Stacks calls a category together with a pretopology a site, and [MM92] calls this a basis for a topology. -/ structure pretopology (C : Type u) [category C] [limits.has_pullbacks C] where coverings : (X : C) → set (presieve X) has_isos : ∀ {X Y : C} (f : Y ⟶ X) [_inst_3 : is_iso f], presieve.singleton f ∈ coverings X pullbacks : ∀ {X Y : C} (f : Y ⟶ X) (S : presieve X), S ∈ coverings X → pullback_arrows f S ∈ coverings Y transitive : ∀ {X : C} (S : presieve X) (Ti : {Y : C} → (f : Y ⟶ X) → S f → presieve Y), S ∈ coverings X → (∀ {Y : C} (f : Y ⟶ X) (H : S f), Ti f H ∈ coverings Y) → presieve.bind S Ti ∈ coverings X namespace pretopology protected instance has_coe_to_fun (C : Type u) [category C] [limits.has_pullbacks C] : has_coe_to_fun (pretopology C) := has_coe_to_fun.mk (fun (J : pretopology C) => (X : C) → set (presieve X)) fun (J : pretopology C) => coverings J protected instance partial_order (C : Type u) [category C] [limits.has_pullbacks C] : partial_order (pretopology C) := partial_order.mk (fun (K₁ K₂ : pretopology C) => ⇑K₁ ≤ ⇑K₂) (preorder.lt._default fun (K₁ K₂ : pretopology C) => ⇑K₁ ≤ ⇑K₂) sorry sorry sorry protected instance order_top (C : Type u) [category C] [limits.has_pullbacks C] : order_top (pretopology C) := order_top.mk (mk (fun (_x : C) => set.univ) sorry sorry sorry) partial_order.le partial_order.lt sorry sorry sorry sorry protected instance inhabited (C : Type u) [category C] [limits.has_pullbacks C] : Inhabited (pretopology C) := { default := ⊤ } /-- A pretopology `K` can be completed to a Grothendieck topology `J` by declaring a sieve to be `J`-covering if it contains a family in `K`. See https://stacks.math.columbia.edu/tag/00ZC, or [MM92] Chapter III, Section 2, Equation (2). -/ def to_grothendieck (C : Type u) [category C] [limits.has_pullbacks C] (K : pretopology C) : grothendieck_topology C := grothendieck_topology.mk (fun (X : C) (S : sieve X) => ∃ (R : presieve X), ∃ (H : R ∈ coe_fn K X), R ≤ ⇑S) sorry sorry sorry theorem mem_to_grothendieck (C : Type u) [category C] [limits.has_pullbacks C] (K : pretopology C) (X : C) (S : sieve X) : S ∈ coe_fn (to_grothendieck C K) X ↔ ∃ (R : presieve X), ∃ (H : R ∈ coe_fn K X), R ≤ ⇑S := iff.rfl /-- The largest pretopology generating the given Grothendieck topology. See [MM92] Chapter III, Section 2, Equations (3,4). -/ def of_grothendieck (C : Type u) [category C] [limits.has_pullbacks C] (J : grothendieck_topology C) : pretopology C := mk (fun (X : C) (R : presieve X) => sieve.generate R ∈ coe_fn J X) sorry sorry sorry /-- We have a galois insertion from pretopologies to Grothendieck topologies. -/ def gi (C : Type u) [category C] [limits.has_pullbacks C] : galois_insertion (to_grothendieck C) (of_grothendieck C) := galois_insertion.mk (fun (x : pretopology C) (hx : of_grothendieck C (to_grothendieck C x) ≤ x) => to_grothendieck C x) sorry sorry sorry /-- The trivial pretopology, in which the coverings are exactly singleton isomorphisms. This topology is also known as the indiscrete, coarse, or chaotic topology. See https://stacks.math.columbia.edu/tag/07GE -/ def trivial (C : Type u) [category C] [limits.has_pullbacks C] : pretopology C := mk (fun (X : C) (S : presieve X) => ∃ (Y : C), ∃ (f : Y ⟶ X), ∃ (h : is_iso f), S = presieve.singleton f) sorry sorry sorry protected instance order_bot (C : Type u) [category C] [limits.has_pullbacks C] : order_bot (pretopology C) := order_bot.mk (trivial C) partial_order.le partial_order.lt sorry sorry sorry sorry /-- The trivial pretopology induces the trivial grothendieck topology. -/ theorem to_grothendieck_bot (C : Type u) [category C] [limits.has_pullbacks C] : to_grothendieck C ⊥ = ⊥ := galois_connection.l_bot (galois_insertion.gc (gi C))
4980570071b9e96b112ca66968ce85d0fb28bff9
f083c4ed5d443659f3ed9b43b1ca5bb037ddeb58
/tactic/basic.lean
e34e5e7f3c9c95d642fe42243f20fccdeea91770
[ "Apache-2.0" ]
permissive
semorrison/mathlib
1be6f11086e0d24180fec4b9696d3ec58b439d10
20b4143976dad48e664c4847b75a85237dca0a89
refs/heads/master
1,583,799,212,170
1,535,634,130,000
1,535,730,505,000
129,076,205
0
0
Apache-2.0
1,551,697,998,000
1,523,442,265,000
Lean
UTF-8
Lean
false
false
18,585
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Simon Hudon, Scott Morrison -/ import data.dlist.basic category.basic namespace name meta def deinternalize_field : name → name | (name.mk_string s name.anonymous) := let i := s.mk_iterator in if i.curr = '_' then i.next.next_to_string else s | n := n end name namespace expr open tactic protected meta def to_pos_nat : expr → option ℕ | `(has_one.one _) := some 1 | `(bit0 %%e) := bit0 <$> e.to_pos_nat | `(bit1 %%e) := bit1 <$> e.to_pos_nat | _ := none protected meta def to_nat : expr → option ℕ | `(has_zero.zero _) := some 0 | e := e.to_pos_nat protected meta def to_int : expr → option ℤ | `(has_neg.neg %%e) := do n ← e.to_nat, some (-n) | e := coe <$> e.to_nat protected meta def of_nat (α : expr) : ℕ → tactic expr := nat.binary_rec (tactic.mk_mapp ``has_zero.zero [some α, none]) (λ b n tac, if n = 0 then mk_mapp ``has_one.one [some α, none] else do e ← tac, tactic.mk_app (cond b ``bit1 ``bit0) [e]) protected meta def of_int (α : expr) : ℤ → tactic expr | (n : ℕ) := expr.of_nat α n | -[1+ n] := do e ← expr.of_nat α (n+1), tactic.mk_app ``has_neg.neg [e] meta def is_meta_var : expr → bool | (mvar _ _ _) := tt | e := ff meta def list_local_consts (e : expr) : list expr := e.fold [] (λ e' _ es, if e'.is_local_constant then insert e' es else es) meta def list_meta_vars (e : expr) : list expr := e.fold [] (λ e' _ es, if e'.is_meta_var then insert e' es else es) meta def list_names_with_prefix (pre : name) (e : expr) : name_set := e.fold mk_name_set $ λ e' _ l, match e' with | expr.const n _ := if n.get_prefix = pre then l.insert n else l | _ := l end /- only traverses the direct descendents -/ meta def {u} traverse {m : Type → Type u} [applicative m] {elab elab' : bool} (f : expr elab → m (expr elab')) : expr elab → m (expr elab') | (var v) := pure $ var v | (sort l) := pure $ sort l | (const n ls) := pure $ const n ls | (mvar n n' e) := mvar n n' <$> f e | (local_const n n' bi e) := local_const n n' bi <$> f e | (app e₀ e₁) := app <$> f e₀ <*> f e₁ | (lam n bi e₀ e₁) := lam n bi <$> f e₀ <*> f e₁ | (pi n bi e₀ e₁) := pi n bi <$> f e₀ <*> f e₁ | (elet n e₀ e₁ e₂) := elet n <$> f e₀ <*> f e₁ <*> f e₂ | (macro mac es) := macro mac <$> list.traverse f es meta def mfoldl {α : Type} {m} [monad m] (f : α → expr → m α) : α → expr → m α | x e := prod.snd <$> (state_t.run (e.traverse $ λ e', (get >>= monad_lift ∘ flip f e' >>= put) $> e') x : m _) end expr namespace environment meta def in_current_file' (env : environment) (n : name) : bool := env.in_current_file n && (n ∉ [``quot, ``quot.mk, ``quot.lift, ``quot.ind]) meta def is_structure_like (env : environment) (n : name) : option (nat × name) := do guardb (env.is_inductive n), d ← (env.get n).to_option, [intro] ← pure (env.constructors_of n) | none, guard (env.inductive_num_indices n = 0), some (env.inductive_num_params n, intro) meta def is_structure (env : environment) (n : name) : bool := option.is_some $ do (nparams, intro) ← env.is_structure_like n, di ← (env.get intro).to_option, expr.pi x _ _ _ ← nparams.iterate (λ e : option expr, do expr.pi _ _ _ body ← e | none, some body) (some di.type) | none, env.is_projection (n ++ x.deinternalize_field) end environment namespace interaction_monad open result meta def get_result {σ α} (tac : interaction_monad σ α) : interaction_monad σ (interaction_monad.result σ α) | s := match tac s with | r@(success _ s') := success r s' | r@(exception _ _ s') := success r s' end end interaction_monad namespace lean.parser open lean interaction_monad.result meta def of_tactic' {α} (tac : tactic α) : parser α := do r ← of_tactic (interaction_monad.get_result tac), match r with | (success a _) := return a | (exception f pos _) := exception f pos end end lean.parser namespace tactic meta def mk_local (n : name) : expr := expr.local_const n n binder_info.default (expr.const n []) meta def local_def_value (e : expr) : tactic expr := do do (v,_) ← solve_aux `(true) (do (expr.elet n t v _) ← (revert e >> target) | fail format!"{e} is not a local definition", return v), return v meta def check_defn (n : name) (e : pexpr) : tactic unit := do (declaration.defn _ _ _ d _ _) ← get_decl n, e' ← to_expr e, guard (d =ₐ e') <|> trace d >> failed -- meta def compile_eqn (n : name) (univ : list name) (args : list expr) (val : expr) (num : ℕ) : tactic unit := -- do let lhs := (expr.const n $ univ.map level.param).mk_app args, -- stmt ← mk_app `eq [lhs,val], -- let vs := stmt.list_local_const, -- let stmt := stmt.pis vs, -- (_,pr) ← solve_aux stmt (tactic.intros >> reflexivity), -- add_decl $ declaration.thm (n <.> "equations" <.> to_string (format!"_eqn_{num}")) univ stmt (pure pr) meta def to_implicit : expr → expr | (expr.local_const uniq n bi t) := expr.local_const uniq n binder_info.implicit t | e := e meta def pis : list expr → expr → tactic expr | (e@(expr.local_const uniq pp info _) :: es) f := do t ← infer_type e, f' ← pis es f, pure $ expr.pi pp info t (expr.abstract_local f' uniq) | _ f := pure f meta def lambdas : list expr → expr → tactic expr | (e@(expr.local_const uniq pp info _) :: es) f := do t ← infer_type e, f' ← lambdas es f, pure $ expr.lam pp info t (expr.abstract_local f' uniq) | _ f := pure f meta def extract_def (n : name) (trusted : bool) (elab_def : tactic unit) : tactic unit := do cxt ← list.map to_implicit <$> local_context, t ← target, (eqns,d) ← solve_aux t elab_def, d ← instantiate_mvars d, t' ← pis cxt t, d' ← lambdas cxt d, let univ := t'.collect_univ_params, add_decl $ declaration.defn n univ t' d' (reducibility_hints.regular 1 tt) trusted, applyc n meta def exact_dec_trivial : tactic unit := `[exact dec_trivial] /-- Runs a tactic for a result, reverting the state after completion -/ meta def retrieve {α} (tac : tactic α) : tactic α := λ s, result.cases_on (tac s) (λ a s', result.success a s) result.exception /-- Repeat a tactic at least once, calling it recursively on all subgoals, until it fails. This tactic fails if the first invocation fails. -/ meta def repeat1 (t : tactic unit) : tactic unit := t; repeat t /-- `iterate_range m n t`: Repeat the given tactic at least `m` times and at most `n` times or until `t` fails. Fails if `t` does not run at least m times. -/ meta def iterate_range : ℕ → ℕ → tactic unit → tactic unit | 0 0 t := skip | 0 (n+1) t := try (t >> iterate_range 0 n t) | (m+1) n t := t >> iterate_range m (n-1) t meta def replace_at (tac : expr → tactic (expr × expr)) (hs : list expr) (tgt : bool) : tactic bool := do to_remove ← hs.mfilter $ λ h, do { h_type ← infer_type h, succeeds $ do (new_h_type, pr) ← tac h_type, assert h.local_pp_name new_h_type, mk_eq_mp pr h >>= tactic.exact }, goal_simplified ← succeeds $ do { guard tgt, (new_t, pr) ← target >>= tac, replace_target new_t pr }, to_remove.mmap' (λ h, try (clear h)), return (¬ to_remove.empty ∨ goal_simplified) meta def simp_bottom_up' (post : expr → tactic (expr × expr)) (e : expr) (cfg : simp_config := {}) : tactic (expr × expr) := prod.snd <$> simplify_bottom_up () (λ _, (<$>) (prod.mk ()) ∘ post) e cfg meta structure instance_cache := (α : expr) (univ : level) (inst : name_map expr) meta def mk_instance_cache (α : expr) : tactic instance_cache := do u ← mk_meta_univ, infer_type α >>= unify (expr.sort (level.succ u)), u ← get_univ_assignment u, return ⟨α, u, mk_name_map⟩ namespace instance_cache meta def get (c : instance_cache) (n : name) : tactic (instance_cache × expr) := match c.inst.find n with | some i := return (c, i) | none := do e ← mk_app n [c.α] >>= mk_instance, return (⟨c.α, c.univ, c.inst.insert n e⟩, e) end open expr meta def append_typeclasses : expr → instance_cache → list expr → tactic (instance_cache × list expr) | (pi _ binder_info.inst_implicit (app (const n _) (var _)) body) c l := do (c, p) ← c.get n, return (c, p :: l) | _ c l := return (c, l) meta def mk_app (c : instance_cache) (n : name) (l : list expr) : tactic (instance_cache × expr) := do d ← get_decl n, (c, l) ← append_typeclasses d.type.binding_body c l, return (c, (expr.const n [c.univ]).mk_app (c.α :: l)) end instance_cache /-- Reset the instance cache for the main goal. -/ meta def reset_instance_cache : tactic unit := unfreeze_local_instances meta def match_head (e : expr) : expr → tactic unit | e' := unify e e' <|> do `(_ → %%e') ← whnf e', v ← mk_mvar, match_head (e'.instantiate_var v) meta def find_matching_head : expr → list expr → tactic (list expr) | e [] := return [] | e (H :: Hs) := do t ← infer_type H, ((::) H <$ match_head e t <|> pure id) <*> find_matching_head e Hs meta def subst_locals (s : list (expr × expr)) (e : expr) : expr := (e.abstract_locals (s.map (expr.local_uniq_name ∘ prod.fst)).reverse).instantiate_vars (s.map prod.snd) meta def set_binder : expr → list binder_info → expr | e [] := e | (expr.pi v _ d b) (bi :: bs) := expr.pi v bi d (set_binder b bs) | e _ := e meta def last_explicit_arg : expr → tactic expr | (expr.app f e) := do t ← infer_type f >>= whnf, if t.binding_info = binder_info.default then pure e else last_explicit_arg f | e := pure e private meta def get_expl_pi_arity_aux : expr → tactic nat | (expr.pi n bi d b) := do m ← mk_fresh_name, let l := expr.local_const m n bi d, new_b ← whnf (expr.instantiate_var b l), r ← get_expl_pi_arity_aux new_b, if bi = binder_info.default then return (r + 1) else return r | e := return 0 /-- Compute the arity of explicit arguments of the given (Pi-)type -/ meta def get_expl_pi_arity (type : expr) : tactic nat := whnf type >>= get_expl_pi_arity_aux /-- Compute the arity of explicit arguments of the given function -/ meta def get_expl_arity (fn : expr) : tactic nat := infer_type fn >>= get_expl_pi_arity /-- variation on `assert` where a (possibly incomplete) proof of the assertion is provided as a parameter. ``(h,gs) ← local_proof `h p tac`` creates a local `h : p` and use `tac` to (partially) construct a proof for it. `gs` is the list of remaining goals in the proof of `h`. The benefits over assert are: - unlike with ``h ← assert `h p, tac`` , `h` cannot be used by `tac`; - when `tac` does not complete the proof of `h`, returning the list of goals allows one to write a tactic using `h` and with the confidence that a proof will not boil over to goals left over from the proof of `h`, unlike what would be the case when using `tactic.swap`. -/ meta def local_proof (h : name) (p : expr) (tac₀ : tactic unit) : tactic (expr × list expr) := focus1 $ do h' ← assert h p, [g₀,g₁] ← get_goals, set_goals [g₀], tac₀, gs ← get_goals, set_goals [g₁], return (h', gs) meta def try_intros : list name → tactic (list name) | [] := try intros $> [] | (x::xs) := (intro x >> try_intros xs) <|> pure (x :: xs) meta def ext1 (xs : list name) : tactic (list name) := do ls ← attribute.get_instances `extensionality, ls.any_of (λ l, applyc l) <|> fail "no applicable extensionality rule found", try_intros xs meta def ext : list name → option ℕ → tactic unit | _ (some 0) := skip | xs n := focus1 $ do ys ← ext1 xs, try (ext ys (nat.pred <$> n)) meta def var_names : expr → list name | (expr.pi n _ _ b) := n :: var_names b | _ := [] meta def drop_binders : expr → tactic expr | (expr.pi n bi t b) := b.instantiate_var <$> mk_local' n bi t >>= drop_binders | e := pure e meta def subobject_names (struct_n : name) : tactic (list name × list name) := do env ← get_env, [c] ← pure $ env.constructors_of struct_n | fail "too many constructors", vs ← var_names <$> (mk_const c >>= infer_type), fields ← env.structure_fields struct_n, return $ fields.partition (λ fn, ↑("_" ++ fn.to_string) ∈ vs) meta def expanded_field_list' : name → tactic (dlist $ name × name) | struct_n := do (so,fs) ← subobject_names struct_n, ts ← so.mmap (λ n, do e ← mk_const (n.update_prefix struct_n) >>= infer_type >>= drop_binders, expanded_field_list' $ e.get_app_fn.const_name), return $ dlist.join ts ++ dlist.of_list (fs.map $ prod.mk struct_n) open functor function meta def expanded_field_list (struct_n : name) : tactic (list $ name × name) := dlist.to_list <$> expanded_field_list' struct_n meta def get_classes (e : expr) : tactic (list name) := attribute.get_instances `class >>= list.mfilter (λ n, succeeds $ mk_app n [e] >>= mk_instance) open nat meta def mk_mvar_list : ℕ → tactic (list expr) | 0 := pure [] | (succ n) := (::) <$> mk_mvar <*> mk_mvar_list n /--`iterate_at_most_on_all_goals n t`: repeat the given tactic at most `n` times on all goals, or until it fails. Always succeeds. -/ meta def iterate_at_most_on_all_goals : nat → tactic unit → tactic unit | 0 tac := trace "maximal iterations reached" | (succ n) tac := tactic.all_goals $ (do tac, iterate_at_most_on_all_goals n tac) <|> skip /--`iterate_at_most_on_subgoals n t`: repeat the tactic `t` at most `n` times on the first goal and on all subgoals thus produced, or until it fails. Fails iff `t` fails on current goal. -/ meta def iterate_at_most_on_subgoals : nat → tactic unit → tactic unit | 0 tac := trace "maximal iterations reached" | (succ n) tac := focus1 (do tac, iterate_at_most_on_all_goals n tac) /--`apply_list l`: try to apply the tactics in the list `l` on the first goal, and fail if none succeeds -/ meta def apply_list_expr : list expr → tactic unit | [] := fail "no matching rule" | (h::t) := do interactive.concat_tags (apply h) <|> apply_list_expr t /-- constructs a list of expressions given a list of p-expressions, as follows: - if the p-expression is the name of a theorem, use `i_to_expr_for_apply` on it - if the p-expression is a user attribute, add all the theorems with this attribute to the list.-/ meta def build_list_expr_for_apply : list pexpr → tactic (list expr) | [] := return [] | (h::t) := do tail ← build_list_expr_for_apply t, a ← i_to_expr_for_apply h, (do l ← attribute.get_instances (expr.const_name a), m ← list.mmap mk_const l, return (m.append tail)) <|> return (a::tail) /--`apply_rules hs n`: apply the list of rules `hs` (given as pexpr) and `assumption` on the first goal and the resulting subgoals, iteratively, at most `n` times -/ meta def apply_rules (hs : list pexpr) (n : nat) : tactic unit := do l ← build_list_expr_for_apply hs, iterate_at_most_on_subgoals n (assumption <|> apply_list_expr l) meta def replace (h : name) (p : pexpr) : tactic unit := do h' ← get_local h, p ← to_expr p, note h none p, clear h' meta def symm_apply (e : expr) (cfg : apply_cfg := {}) : tactic (list (name × expr)) := tactic.apply e cfg <|> (symmetry >> tactic.apply e cfg) meta def apply_assumption (asms : option (list expr) := none) (tac : tactic unit := skip) : tactic unit := do { ctx ← asms.to_monad <|> local_context, ctx.any_of (λ H, () <$ symm_apply H; tac) } <|> do { exfalso, ctx ← asms.to_monad <|> local_context, ctx.any_of (λ H, () <$ symm_apply H; tac) } <|> fail "assumption tactic failed" open nat meta def solve_by_elim_aux (discharger : tactic unit) (asms : option (list expr)) : ℕ → tactic unit | 0 := done | (succ n) := discharger <|> (apply_assumption asms $ solve_by_elim_aux n) meta structure by_elim_opt := (discharger : tactic unit := done) (restr_hyp_set : option (list expr) := none) (max_rep : ℕ := 3) meta def solve_by_elim (opt : by_elim_opt := { }) : tactic unit := do tactic.fail_if_no_goals, solve_by_elim_aux opt.discharger opt.restr_hyp_set opt.max_rep meta def metavariables : tactic (list expr) := do r ← result, pure (r.list_meta_vars) /-- Succeeds only if the current goal is a proposition. -/ meta def propositional_goal : tactic unit := do goals ← get_goals, let current_goal := goals.head, current_goal_type ← infer_type current_goal, p ← is_prop current_goal_type, guard p variable {α : Type} private meta def iterate_aux (t : tactic α) : list α → tactic (list α) | L := (do r ← t, iterate_aux (r :: L)) <|> return L meta def iterate' (t : tactic α) : tactic (list α) := list.reverse <$> iterate_aux t [] meta def iterate1 (t : tactic α) : tactic (α × list α) := do r ← t | fail "iterate1 failed: tactic did not succeed", L ← iterate' t, return (r, L) meta def intros1 : tactic (list expr) := iterate1 intro1 >>= λ p, return (p.1 :: p.2) /-- `successes` invokes each tactic in turn, returning the list of successful results. -/ meta def successes {α} (tactics : list (tactic α)) : tactic (list α) := list.filter_map id <$> monad.sequence (tactics.map (λ t, try_core t)) /-- Return target after instantiating metavars and whnf -/ private meta def target' : tactic expr := target >>= instantiate_mvars >>= whnf /-- Just like `split`, `fsplit` applies the constructor when the type of the target is an inductive data type with one constructor. However it does not reorder goals or invoke `auto_param` tactics. -/ -- FIXME check if we can remove `auto_param := ff` meta def fsplit : tactic unit := do [c] ← target' >>= get_constructors_for | tactic.fail "fsplit tactic failed, target is not an inductive datatype with only one constructor", mk_const c >>= λ e, apply e {new_goals := new_goals.all, auto_param := ff} >> skip run_cmd add_interactive [`fsplit] /-- Calls `injection` on each hypothesis, and then, for each hypothesis on which `injection` succeeds, clears the old hypothesis. -/ meta def injections_and_clear : tactic unit := do l ← local_context, results ← successes $ l.map $ λ e, injection e >> clear e, when (results.empty) (fail "could not use `injection` then `clear` on any hypothesis") run_cmd add_interactive [`injections_and_clear] end tactic
ef766e2bcccc2adffda2a24388073abfce206146
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/eval_unboxed_const.lean
2229dacb8255a9fd7fcf9c1936043557d5f5fb57
[ "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
480
lean
import Lean new_frontend open Lean def c1 : Bool := true unsafe def tst1 : CoreM Unit := do let env ← getEnv; let v := env.evalConst Bool `c1; IO.println v #eval tst1 -- outputs incorrect value (ok false). Reason: the unboxed value `true` is `1`, but the boxed `false` value is also `1`. def c2 : Bool := false unsafe def tst2 : CoreM Unit := do let env ← getEnv; let v := env.evalConst Bool `c2; IO.println v #eval tst2 -- crashes since `0` is not a valid boxed value
6d9e7cd775f3862c6f74412ff779139152e267a6
9dc8cecdf3c4634764a18254e94d43da07142918
/src/set_theory/cardinal/finite.lean
7e1708e1a028e65684a8c57323e1969529c91c59
[ "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
3,342
lean
/- Copyright (c) 2021 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import data.zmod.defs import set_theory.cardinal.basic /-! # Finite Cardinality Functions ## Main Definitions * `nat.card α` is the cardinality of `α` as a natural number. If `α` is infinite, `nat.card α = 0`. * `part_enat.card α` is the cardinality of `α` as an extended natural number (`part ℕ` implementation). If `α` is infinite, `part_enat.card α = ⊤`. -/ open cardinal noncomputable theory open_locale big_operators variables {α β : Type*} namespace nat /-- `nat.card α` is the cardinality of `α` as a natural number. If `α` is infinite, `nat.card α = 0`. -/ protected def card (α : Type*) : ℕ := (mk α).to_nat @[simp] lemma card_eq_fintype_card [fintype α] : nat.card α = fintype.card α := mk_to_nat_eq_card @[simp] lemma card_eq_zero_of_infinite [infinite α] : nat.card α = 0 := mk_to_nat_of_infinite lemma card_congr (f : α ≃ β) : nat.card α = nat.card β := cardinal.to_nat_congr f lemma card_eq_of_bijective (f : α → β) (hf : function.bijective f) : nat.card α = nat.card β := card_congr (equiv.of_bijective f hf) lemma card_eq_of_equiv_fin {α : Type*} {n : ℕ} (f : α ≃ fin n) : nat.card α = n := by simpa using card_congr f /-- If the cardinality is positive, that means it is a finite type, so there is an equivalence between `α` and `fin (nat.card α)`. See also `finite.equiv_fin`. -/ def equiv_fin_of_card_pos {α : Type*} (h : nat.card α ≠ 0) : α ≃ fin (nat.card α) := begin casesI fintype_or_infinite α, { simpa using fintype.equiv_fin α }, { simpa using h }, end lemma card_of_subsingleton (a : α) [subsingleton α] : nat.card α = 1 := begin letI := fintype.of_subsingleton a, rw [card_eq_fintype_card, fintype.card_of_subsingleton a] end @[simp] lemma card_unique [unique α] : nat.card α = 1 := card_of_subsingleton default lemma card_eq_one_iff_unique : nat.card α = 1 ↔ subsingleton α ∧ nonempty α := cardinal.to_nat_eq_one_iff_unique theorem card_of_is_empty [is_empty α] : nat.card α = 0 := by simp @[simp] lemma card_prod (α β : Type*) : nat.card (α × β) = nat.card α * nat.card β := by simp only [nat.card, mk_prod, to_nat_mul, to_nat_lift] @[simp] lemma card_ulift (α : Type*) : nat.card (ulift α) = nat.card α := card_congr equiv.ulift @[simp] lemma card_plift (α : Type*) : nat.card (plift α) = nat.card α := card_congr equiv.plift lemma card_pi {β : α → Type*} [fintype α] : nat.card (Π a, β a) = ∏ a, nat.card (β a) := by simp_rw [nat.card, mk_pi, prod_eq_of_fintype, to_nat_lift, to_nat_finset_prod] @[simp] lemma card_zmod (n : ℕ) : nat.card (zmod n) = n := begin cases n, { exact nat.card_eq_zero_of_infinite }, { rw [nat.card_eq_fintype_card, zmod.card] }, end end nat namespace part_enat /-- `part_enat.card α` is the cardinality of `α` as an extended natural number. If `α` is infinite, `part_enat.card α = ⊤`. -/ def card (α : Type*) : part_enat := (mk α).to_part_enat @[simp] lemma card_eq_coe_fintype_card [fintype α] : card α = fintype.card α := mk_to_part_enat_eq_coe_card @[simp] lemma card_eq_top_of_infinite [infinite α] : card α = ⊤ := mk_to_part_enat_of_infinite end part_enat
1c88715723a6864d99a1c1812de81235de9b42dc
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/set/intervals/unordered_interval.lean
9461ef2701ef57d1a39dce79b3dc33e57d20836a
[ "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,354
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 order.bounds import data.set.intervals.image_preimage /-! # Intervals without endpoints ordering In any decidable linear order `α`, we define the set of elements lying between two elements `a` and `b` as `Icc (min a b) (max a b)`. `Icc a b` requires the assumption `a ≤ b` to be meaningful, which is sometimes inconvenient. The interval as defined in this file is always the set of things lying between `a` and `b`, regardless of the relative order of `a` and `b`. For real numbers, `Icc (min a b) (max a b)` is the same as `segment ℝ a b`. ## Notation We use the localized notation `[a, b]` for `interval a b`. One can open the locale `interval` to make the notation available. -/ universe u open_locale pointwise open order_dual (to_dual of_dual) namespace set section linear_order variables {α : Type u} [linear_order α] {a a₁ a₂ b b₁ b₂ c x : α} /-- `interval a b` is the set of elements lying between `a` and `b`, with `a` and `b` included. -/ def interval (a b : α) := Icc (min a b) (max a b) localized "notation (name := set.interval) `[`a `, ` b `]` := set.interval a b" in interval @[simp] lemma dual_interval (a b : α) : [to_dual a, to_dual b] = of_dual ⁻¹' [a, b] := dual_Icc @[simp] lemma interval_of_le (h : a ≤ b) : [a, b] = Icc a b := by rw [interval, min_eq_left h, max_eq_right h] @[simp] lemma interval_of_ge (h : b ≤ a) : [a, b] = Icc b a := by { rw [interval, min_eq_right h, max_eq_left h] } lemma interval_swap (a b : α) : [a, b] = [b, a] := by rw [interval, interval, min_comm, max_comm] lemma interval_of_lt (h : a < b) : [a, b] = Icc a b := interval_of_le (le_of_lt h) lemma interval_of_gt (h : b < a) : [a, b] = Icc b a := interval_of_ge (le_of_lt h) lemma interval_of_not_le (h : ¬ a ≤ b) : [a, b] = Icc b a := interval_of_gt (lt_of_not_ge h) lemma interval_of_not_ge (h : ¬ b ≤ a) : [a, b] = Icc a b := interval_of_lt (lt_of_not_ge h) @[simp] lemma interval_self : [a, a] = {a} := set.ext $ by simp [le_antisymm_iff, and_comm] @[simp] lemma nonempty_interval : set.nonempty [a, b] := by { simp only [interval, min_le_iff, le_max_iff, nonempty_Icc], left, left, refl } @[simp] lemma left_mem_interval : a ∈ [a, b] := by { rw [interval, mem_Icc], exact ⟨min_le_left _ _, le_max_left _ _⟩ } @[simp] lemma right_mem_interval : b ∈ [a, b] := by { rw interval_swap, exact left_mem_interval } lemma Icc_subset_interval : Icc a b ⊆ [a, b] := Icc_subset_Icc (min_le_left _ _) (le_max_right _ _) lemma Icc_subset_interval' : Icc b a ⊆ [a, b] := by { rw interval_swap, apply Icc_subset_interval } lemma mem_interval_of_le (ha : a ≤ x) (hb : x ≤ b) : x ∈ [a, b] := Icc_subset_interval ⟨ha, hb⟩ lemma mem_interval_of_ge (hb : b ≤ x) (ha : x ≤ a) : x ∈ [a, b] := Icc_subset_interval' ⟨hb, ha⟩ lemma not_mem_interval_of_lt (ha : c < a) (hb : c < b) : c ∉ interval a b := not_mem_Icc_of_lt $ lt_min_iff.mpr ⟨ha, hb⟩ lemma not_mem_interval_of_gt (ha : a < c) (hb : b < c) : c ∉ interval a b := not_mem_Icc_of_gt $ max_lt_iff.mpr ⟨ha, hb⟩ lemma interval_subset_interval (h₁ : a₁ ∈ [a₂, b₂]) (h₂ : b₁ ∈ [a₂, b₂]) : [a₁, b₁] ⊆ [a₂, b₂] := Icc_subset_Icc (le_min h₁.1 h₂.1) (max_le h₁.2 h₂.2) lemma interval_subset_Icc (ha : a₁ ∈ Icc a₂ b₂) (hb : b₁ ∈ Icc a₂ b₂) : [a₁, b₁] ⊆ Icc a₂ b₂ := Icc_subset_Icc (le_min ha.1 hb.1) (max_le ha.2 hb.2) lemma interval_subset_interval_iff_mem : [a₁, b₁] ⊆ [a₂, b₂] ↔ a₁ ∈ [a₂, b₂] ∧ b₁ ∈ [a₂, b₂] := iff.intro (λh, ⟨h left_mem_interval, h right_mem_interval⟩) (λ h, interval_subset_interval h.1 h.2) lemma interval_subset_interval_iff_le : [a₁, b₁] ⊆ [a₂, b₂] ↔ min a₂ b₂ ≤ min a₁ b₁ ∧ max a₁ b₁ ≤ max a₂ b₂ := by { rw [interval, interval, Icc_subset_Icc_iff], exact min_le_max } lemma interval_subset_interval_right (h : x ∈ [a, b]) : [x, b] ⊆ [a, b] := interval_subset_interval h right_mem_interval lemma interval_subset_interval_left (h : x ∈ [a, b]) : [a, x] ⊆ [a, b] := interval_subset_interval left_mem_interval h /-- A sort of triangle inequality. -/ lemma interval_subset_interval_union_interval : [a, c] ⊆ [a, b] ∪ [b, c] := begin rintro x hx, obtain hac | hac := le_total a c, { rw interval_of_le hac at hx, obtain hb | hb := le_total x b, { exact or.inl (mem_interval_of_le hx.1 hb) }, { exact or.inr (mem_interval_of_le hb hx.2) } }, { rw interval_of_ge hac at hx, obtain hb | hb := le_total x b, { exact or.inr (mem_interval_of_ge hx.1 hb) }, { exact or.inl (mem_interval_of_ge hb hx.2) } } end lemma bdd_below_bdd_above_iff_subset_interval (s : set α) : bdd_below s ∧ bdd_above s ↔ ∃ a b, s ⊆ [a, b] := begin rw [bdd_below_bdd_above_iff_subset_Icc], split, { rintro ⟨a, b, h⟩, exact ⟨a, b, λ x hx, Icc_subset_interval (h hx)⟩ }, { rintro ⟨a, b, h⟩, exact ⟨min a b, max a b, h⟩ } end /-- The open-closed interval with unordered bounds. -/ def interval_oc : α → α → set α := λ a b, Ioc (min a b) (max a b) -- Below is a capital iota localized "notation `Ι` := set.interval_oc" in interval lemma interval_oc_of_le (h : a ≤ b) : Ι a b = Ioc a b := by simp [interval_oc, h] lemma interval_oc_of_lt (h : b < a) : Ι a b = Ioc b a := by simp [interval_oc, le_of_lt h] lemma interval_oc_eq_union : Ι a b = Ioc a b ∪ Ioc b a := by cases le_total a b; simp [interval_oc, *] lemma forall_interval_oc_iff {P : α → Prop} : (∀ x ∈ Ι a b, P x) ↔ (∀ x ∈ Ioc a b, P x) ∧ (∀ x ∈ Ioc b a, P x) := by simp only [interval_oc_eq_union, mem_union_eq, or_imp_distrib, forall_and_distrib] lemma interval_oc_subset_interval_oc_of_interval_subset_interval {a b c d : α} (h : [a, b] ⊆ [c, d]) : Ι a b ⊆ Ι c d := Ioc_subset_Ioc (interval_subset_interval_iff_le.1 h).1 (interval_subset_interval_iff_le.1 h).2 lemma interval_oc_swap (a b : α) : Ι a b = Ι b a := by simp only [interval_oc, min_comm a b, max_comm a b] lemma Ioc_subset_interval_oc : Ioc a b ⊆ Ι a b := Ioc_subset_Ioc (min_le_left _ _) (le_max_right _ _) lemma Ioc_subset_interval_oc' : Ioc a b ⊆ Ι b a := Ioc_subset_Ioc (min_le_right _ _) (le_max_left _ _) end linear_order open_locale interval section ordered_add_comm_group variables {α : Type u} [linear_ordered_add_comm_group α] (a b c x y : α) @[simp] lemma preimage_const_add_interval : (λ x, a + x) ⁻¹' [b, c] = [b - a, c - a] := by simp only [interval, preimage_const_add_Icc, min_sub_sub_right, max_sub_sub_right] @[simp] lemma preimage_add_const_interval : (λ x, x + a) ⁻¹' [b, c] = [b - a, c - a] := by simpa only [add_comm] using preimage_const_add_interval a b c @[simp] lemma preimage_neg_interval : - [a, b] = [-a, -b] := by simp only [interval, preimage_neg_Icc, min_neg_neg, max_neg_neg] @[simp] lemma preimage_sub_const_interval : (λ x, x - a) ⁻¹' [b, c] = [b + a, c + a] := by simp [sub_eq_add_neg] @[simp] lemma preimage_const_sub_interval : (λ x, a - x) ⁻¹' [b, c] = [a - b, a - c] := by { rw [interval, interval, preimage_const_sub_Icc], simp only [sub_eq_add_neg, min_add_add_left, max_add_add_left, min_neg_neg, max_neg_neg], } @[simp] lemma image_const_add_interval : (λ x, a + x) '' [b, c] = [a + b, a + c] := by simp [add_comm] @[simp] lemma image_add_const_interval : (λ x, x + a) '' [b, c] = [b + a, c + a] := by simp @[simp] lemma image_const_sub_interval : (λ x, a - x) '' [b, c] = [a - b, a - c] := by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)] @[simp] lemma image_sub_const_interval : (λ x, x - a) '' [b, c] = [b - a, c - a] := by simp [sub_eq_add_neg, add_comm] lemma image_neg_interval : has_neg.neg '' [a, b] = [-a, -b] := by simp variables {a b c x y} /-- If `[x, y]` is a subinterval of `[a, b]`, then the distance between `x` and `y` is less than or equal to that of `a` and `b` -/ lemma abs_sub_le_of_subinterval (h : [x, y] ⊆ [a, b]) : |y - x| ≤ |b - a| := begin rw [← max_sub_min_eq_abs, ← max_sub_min_eq_abs], rw [interval_subset_interval_iff_le] at h, exact sub_le_sub h.2 h.1, end /-- If `x ∈ [a, b]`, then the distance between `a` and `x` is less than or equal to that of `a` and `b` -/ lemma abs_sub_left_of_mem_interval (h : x ∈ [a, b]) : |x - a| ≤ |b - a| := abs_sub_le_of_subinterval (interval_subset_interval_left h) /-- If `x ∈ [a, b]`, then the distance between `x` and `b` is less than or equal to that of `a` and `b` -/ lemma abs_sub_right_of_mem_interval (h : x ∈ [a, b]) : |b - x| ≤ |b - a| := abs_sub_le_of_subinterval (interval_subset_interval_right h) end ordered_add_comm_group section linear_ordered_field variables {k : Type u} [linear_ordered_field k] {a : k} @[simp] lemma preimage_mul_const_interval (ha : a ≠ 0) (b c : k) : (λ x, x * a) ⁻¹' [b, c] = [b / a, c / a] := (lt_or_gt_of_ne ha).elim (λ ha, by simp [interval, ha, ha.le, min_div_div_right_of_nonpos, max_div_div_right_of_nonpos]) (λ (ha : 0 < a), by simp [interval, ha, ha.le, min_div_div_right, max_div_div_right]) @[simp] lemma preimage_const_mul_interval (ha : a ≠ 0) (b c : k) : (λ x, a * x) ⁻¹' [b, c] = [b / a, c / a] := by simp only [← preimage_mul_const_interval ha, mul_comm] @[simp] lemma preimage_div_const_interval (ha : a ≠ 0) (b c : k) : (λ x, x / a) ⁻¹' [b, c] = [b * a, c * a] := by simp only [div_eq_mul_inv, preimage_mul_const_interval (inv_ne_zero ha), inv_inv] @[simp] lemma image_mul_const_interval (a b c : k) : (λ x, x * a) '' [b, c] = [b * a, c * a] := if ha : a = 0 then by simp [ha] else calc (λ x, x * a) '' [b, c] = (λ x, x * a⁻¹) ⁻¹' [b, c] : (units.mk0 a ha).mul_right.image_eq_preimage _ ... = (λ x, x / a) ⁻¹' [b, c] : by simp only [div_eq_mul_inv] ... = [b * a, c * a] : preimage_div_const_interval ha _ _ @[simp] lemma image_const_mul_interval (a b c : k) : (λ x, a * x) '' [b, c] = [a * b, a * c] := by simpa only [mul_comm] using image_mul_const_interval a b c @[simp] lemma image_div_const_interval (a b c : k) : (λ x, x / a) '' [b, c] = [b / a, c / a] := by simp only [div_eq_mul_inv, image_mul_const_interval] end linear_ordered_field end set
0b325fd82ce537a692fc165275d2255b6e3237bc
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/run/getline_crash.lean
a84b5db01ba151f3cb861b3be2289624b5312e26
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,265
lean
def tstGetLine (str : String) : IO Unit := do let path := "tmp_file"; IO.FS.withFile path IO.FS.Mode.write $ λ (h : IO.FS.Handle) => h.putStrLn str; IO.FS.withFile path IO.FS.Mode.read $ λ (h : IO.FS.Handle) => do let str' ← h.getLine; IO.println str.length; IO.println str'.length; IO.print str'; «unless» (str'.length == str.length + 1) $ throw (IO.userError ("unexpected length: " ++ toString str'.trim.length)); «unless» (str'.trim == str) $ throw (IO.userError ("unexpected result: " ++ str')) def tstGetLine2 (str1 str2 : String) : IO Unit := do let path := "tmp_file"; IO.FS.withFile path IO.FS.Mode.write $ λ (h : IO.FS.Handle) => do { h.putStrLn str1; h.putStr str2 }; IO.FS.withFile path IO.FS.Mode.read $ λ (h : IO.FS.Handle) => do let str1' ← h.getLine; let str2' ← h.getLine; «unless» (str1'.length == str1.length + 1) $ throw (IO.userError ("unexpected length: " ++ toString str1'.trim.length)); «unless» (str1'.trim == str1) $ throw (IO.userError ("unexpected result: " ++ str1')); «unless» (str2'.length == str2.length) $ throw (IO.userError ("unexpected length: " ++ toString str2'.trim.length)); «unless» (str2'.trim == str2) $ throw (IO.userError ("unexpected result: " ++ str2')) def tstGetLine3 (str : String) : IO Unit := do let path := "tmp_file"; IO.FS.withFile path IO.FS.Mode.write $ λ (h : IO.FS.Handle) => do { h.putStrLn str }; IO.FS.withFile path IO.FS.Mode.read $ λ (h : IO.FS.Handle) => do (h.getLine >>= IO.println); (h.getLine >>= IO.println); (h.getLine >>= IO.println); IO.print "done"; pure () #eval tstGetLine3 "abc" #eval tstGetLine ("".pushn 'α' 40) #eval tstGetLine "a" #eval tstGetLine "" #eval tstGetLine ("".pushn 'α' 20) #eval tstGetLine ("".pushn 'a' 61) #eval tstGetLine ("".pushn 'α' 61) #eval tstGetLine ("".pushn 'a' 62) #eval tstGetLine ("".pushn 'a' 63) #eval tstGetLine ("".pushn 'a' 64) #eval tstGetLine ("".pushn 'a' 65) #eval tstGetLine ("".pushn 'a' 66) #eval tstGetLine ("".pushn 'a' 128) #eval tstGetLine2 ("".pushn 'α' 20) ("".pushn 'β' 20) #eval tstGetLine2 ("".pushn 'α' 40) ("".pushn 'β' 40) #eval tstGetLine2 ("".pushn 'a' 61) ("".pushn 'b' 61) #eval tstGetLine2 ("".pushn 'a' 61) ("".pushn 'b' 62)
06447a00efba11a4167ee76db71c28b861296929
037dba89703a79cd4a4aec5e959818147f97635d
/src/2019/questions/sheet3.lean
f4c34597cc445e97f070986e6de7a26ecbd87729
[]
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
6,117
lean
/- Math40001 : Introduction to university mathematics. Problem Sheet 3, October 2019. This is a Lean file. It can be read with the Lean theorem prover. You can work on this file online at https://tinyurl.com/Lean-M40001-Example-Sheet-3 or you can install Lean and its maths library following the instructions at https://github.com/leanprover-community/mathlib#installation There are advantages to installing Lean on your own computer (for example it's faster), but it's more hassle than just using it online. In the below, delete "sorry" and replace it with some tactics which prove the result. -/ import data.real.basic -- the real numbers /- Question 1. Say $X$, $Y$ and $Z$ are sets, and $f:X\to Y$ and $g:Y\to Z$ are functions. In lectures we proved that if $f$ and $g$ are injective, then $g\circ f$ is also injective, and we will prove on Monday that if $f$ and $g $ are surjective, then $g\circ f$ is surjective. But what about the other way? \begin{enumerate} \item If $g\circ f$ is injective, then is $f$ injective? Give a proof or a counterexample. \item If $g\circ f$ is injective, then is $g$ injective? Give a proof or a counterexample. \item If $g\circ f$ is surjective, then is $f$ surjective? Give a proof or a counterexample. \item If $g\circ f$ is surjective, then is $g$ surjective? Give a proof or a counterexample. \end{enumerate} -/ open function -- in Q1 you would be best off defining the counterexample explicitly before you embark upon the -- disproofs of the false statements -- put ¬ in front of the ∀ and put everything in brackets if you want to disprove it lemma question_one_a_true : ∀ (X Y Z : Type) (f : X → Y) (g : Y → Z), injective (g ∘ f) → injective f := begin sorry end -- put ¬ in front of the ∀ and put everything in brackets if you want to disprove it lemma question_one_b_true : ∀ (X Y Z : Type) (f : X → Y) (g : Y → Z), injective (g ∘ f) → injective g := begin sorry end -- put ¬ in front of the ∀ and put everything in brackets if you want to disprove it lemma question_one_c_true : ∀ (X Y Z : Type) (f : X → Y) (g : Y → Z), surjective (g ∘ f) → surjective f := begin sorry end -- put ¬ in front of the ∀ and put everything in brackets if you want to disprove it lemma question_one_d_true : ∀ (X Y Z : Type) (f : X → Y) (g : Y → Z), surjective (g ∘ f) → surjective g := begin sorry end /- % Q2 \item For each of the following functions, decide whether or not thprop_decidableey are injective, surjective, bijective. Proofs required! \begin{enumerate} \item $f:\R\to\R$, $f(x)=1/x$ if $x\not=0$ and $f(0)=0$. \item $f : \R\to\R$, $f(x)=2x+1$. \item $f:\Z\to\Z$, $f(x)=2x+1$. \item $f:\R\to\R$ defined by $f(x)=3-x$ if the Riemann hypothesis is true, and $f(x)=2+x$ if not. [NB the \ href{https://en.wikipedia.org/wiki/Riemann_hypothesis}{Riemann Hypothesis} is a hard unsolved problem in mat hematics; nobody currently knows if it is true or false.] \item $f:\Z\to\Z$, $f(n)=n^3-2n^2+2n-1$. \end{enumerate} -/ -- this line just says "we're mathematicians so every proposition is either true or false" local attribute [instance, priority 10] classical.prop_decidable -- this line says "a function might not be defined by an algorithm" noncomputable theory -- definition of the functions in Q2."λ x," is the way computer scientists say "x ↦" def fa : ℝ → ℝ := λ x, 1 / x -- Lean defines 1/0 to be 0 def fb : ℝ → ℝ := λ x, 2 * x + 1 def fc : ℤ → ℤ := λ x, 2 * x + 1 constant Riemann_Hypothesis : Prop -- doesn't matter what it says def fd : ℝ → ℝ := λ x, if Riemann_Hypothesis then 3 - x else 2 + x def fe : ℤ → ℤ := λ n, n ^ 3 - 2 * n ^ 2 + 2 * n - 1 -- now write your own questions, below are some examples (that may or may not be possible to prove) lemma Q2a1 : injective fa := sorry lemma Q2a2 : ¬ (surjective fa) := sorry lemma Q2c3 : bijective fc := sorry /- Question 3 is "why does this not make sense" so it can't be formalised. -/ /- % Q4 \item Prove the claim I will make in lecture on Monday, saying that if $f:X\to Y$ is a function, and $g:Y\t o X$ is a two-sided inverse of~$f$, then~$f$ is a two-sided inverse for~$g$. Deduce that if~$X$ and~$Y$ are sets, and there exists a bijection from~$X$ to~$Y$, then there exists a bijection from~$Y$ to~$X$. -/ lemma Q4a (X Y : Type) (f : X → Y) (g : Y → X) (h2sided : (∀ x : X, g (f x) = x) ∧ (∀ y : Y, f (g y) = y)) : (∀ y : Y, f (g y) = y) ∧ (∀ x : X, g (f x) = x) := sorry -- you will need this result to do the second part. Ignore the proof, I'm using term mode just to -- make it quicker. Note that this crazy-looking proof is an indication that there are other -- ways of using Lean apart from tactic mode. lemma exists_bijection_iff_has_two_sided_inverse (X Y : Type) : (∃ f : X → Y, bijective f) ↔ (∃ (f : X → Y), ∃ (g : Y → X), (∀ x : X, g (f x) = x) ∧ (∀ y : Y, f (g y) = y)) := ⟨λ ⟨f, hf⟩, ⟨f, bijective_iff_has_inverse.1 hf⟩, λ ⟨f, g, hgf, hfg⟩, ⟨f, bijective_iff_has_inverse.2 ⟨g, hgf, hfg⟩⟩⟩ lemma Q4b (X Y : Type) : (∃ f : X → Y, bijective f) ↔ (∃ g : Y → X, bijective g) := sorry /- % Q5 \item Let~$Z$ be a set. If $f:X\to Z$ and $g:Y\to Z$ are injective functions, let's say that $f$ \emph{is friends with} $g$ if there is a bijection $h:X\to Y$ such that $f=g\circ h$. Prove that $f$ is friends with $g$ if and only if the image of~$f$ equals the image of~$g$. NB: by the \emph{image} of $f:X\to Z$ I mean th e subset of~$Z$ consisting of things ``hit'' by $f$, in other words the set $\{z\in Z\,:\,\exists x\in X, f( x)=z\}$. Some people call this the ``range'' of $f$, although other people use ``range'' to mean the same th ing as ``codomain'' :-| -/ def friends {X Y Z : Type} (f : X → Z) (g : Y → Z) (hf : injective f) (hg : injective g) := ∃ h : X → Y, bijective h ∧ f = g ∘ h lemma Q5 (X Y Z : Type) (f : X → Z) (g : Y → Z) (hf : injective f) (hg : injective g) : friends f g hf hg ↔ set.range f = set.range g := sorry
79eb1057b70a22bbb26d28fdc75ce410a52cc0ec
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/nat/parity.lean
3d1295b945eb9b8cabe26214e253e7d07061ecfb
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
4,069
lean
/- Copyright (c) 2019 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad The `even` predicate on the natural numbers. -/ import data.nat.modeq namespace nat @[simp] theorem mod_two_ne_one {n : nat} : ¬ n % 2 = 1 ↔ n % 2 = 0 := by cases mod_two_eq_zero_or_one n with h h; simp [h] @[simp] theorem mod_two_ne_zero {n : nat} : ¬ n % 2 = 0 ↔ n % 2 = 1 := by cases mod_two_eq_zero_or_one n with h h; simp [h] /-- A natural number `n` is `even` if `2 | n`. -/ def even (n : nat) : Prop := 2 ∣ n theorem even_iff {n : nat} : even n ↔ n % 2 = 0 := ⟨λ ⟨m, hm⟩, by simp [hm], λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by simp [h])⟩⟩ lemma not_even_iff {n : ℕ} : ¬ even n ↔ n % 2 = 1 := by rw [even_iff, mod_two_ne_zero] instance : decidable_pred even := λ n, decidable_of_decidable_of_iff (by apply_instance) even_iff.symm mk_simp_attribute parity_simps "Simp attribute for lemmas about `even`" @[simp] theorem even_zero : even 0 := ⟨0, dec_trivial⟩ @[simp] theorem not_even_one : ¬ even 1 := by rw even_iff; apply one_ne_zero @[simp] theorem even_bit0 (n : nat) : even (bit0 n) := ⟨n, by rw [bit0, two_mul]⟩ @[parity_simps] theorem even_add {m n : nat} : even (m + n) ↔ (even m ↔ even n) := begin cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂; simp [even_iff, h₁, h₂], { exact @modeq.modeq_add _ _ 0 _ 0 h₁ h₂ }, { exact @modeq.modeq_add _ _ 0 _ 1 h₁ h₂ }, { exact @modeq.modeq_add _ _ 1 _ 0 h₁ h₂ }, exact @modeq.modeq_add _ _ 1 _ 1 h₁ h₂ end theorem even.add {m n : ℕ} (hm : m.even) (hn : n.even) : (m + n).even := even_add.2 $ by simp only [*] @[simp] theorem not_even_bit1 (n : nat) : ¬ even (bit1 n) := by simp [bit1] with parity_simps lemma two_not_dvd_two_mul_add_one (a : ℕ) : ¬(2 ∣ 2 * a + 1) := begin convert not_even_bit1 a, exact two_mul a, end lemma two_not_dvd_two_mul_sub_one : Π {a : ℕ} (w : 0 < a), ¬(2 ∣ 2 * a - 1) | (a+1) _ := two_not_dvd_two_mul_add_one a @[parity_simps] theorem even_sub {m n : nat} (h : n ≤ m) : even (m - n) ↔ (even m ↔ even n) := begin conv { to_rhs, rw [←nat.sub_add_cancel h, even_add] }, by_cases h : even n; simp [h] end theorem even.sub {m n : ℕ} (hm : m.even) (hn : n.even) : (m - n).even := (le_total n m).elim (λ h, by simp only [even_sub h, *]) (λ h, by simp only [sub_eq_zero_of_le h, even_zero]) @[parity_simps] theorem even_succ {n : nat} : even (succ n) ↔ ¬ even n := by rw [succ_eq_add_one, even_add]; simp [not_even_one] @[parity_simps] theorem even_mul {m n : nat} : even (m * n) ↔ even m ∨ even n := begin cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂; simp [even_iff, h₁, h₂], { exact @modeq.modeq_mul _ _ 0 _ 0 h₁ h₂ }, { exact @modeq.modeq_mul _ _ 0 _ 1 h₁ h₂ }, { exact @modeq.modeq_mul _ _ 1 _ 0 h₁ h₂ }, exact @modeq.modeq_mul _ _ 1 _ 1 h₁ h₂ end /-- If `m` and `n` are natural numbers, then the natural number `m^n` is even if and only if `m` is even and `n` is positive. -/ @[parity_simps] theorem even_pow {m n : nat} : even (m^n) ↔ even m ∧ n ≠ 0 := by { induction n with n ih; simp [*, pow_succ', even_mul], tauto } lemma even_div {a b : ℕ} : even (a / b) ↔ a % (2 * b) / b = 0 := by rw [even, dvd_iff_mod_eq_zero, nat.div_mod_eq_mod_mul_div, mul_comm] theorem neg_one_pow_eq_one_iff_even {α : Type*} [ring α] {n : ℕ} (h1 : (-1 : α) ≠ 1): (-1 : α) ^ n = 1 ↔ even n := ⟨λ h, n.mod_two_eq_zero_or_one.elim (dvd_iff_mod_eq_zero _ _).2 (λ hn, by rw [neg_one_pow_eq_pow_mod_two, hn, pow_one] at h; exact (h1 h).elim), λ ⟨m, hm⟩, by rw [neg_one_pow_eq_pow_mod_two, hm]; simp⟩ -- Here are examples of how `parity_simps` can be used with `nat`. example (m n : nat) (h : even m) : ¬ even (n + 3) ↔ even (m^2 + m + n) := by simp [*, (dec_trivial : ¬ 2 = 0)] with parity_simps example : ¬ even 25394535 := by simp end nat
b0324e7677e5cf09e579c3ff6b9ff924eae32b36
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/data/buffer/parser/basic.lean
3db3e23db83f2c2e24abc0b8577dd8b996bdecd9
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
29,220
lean
/- Copyright (c) 2020 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import data.string.basic /-! # Parsers `parser α` is the type that describes a computation that can ingest a `char_buffer` and output, if successful, a term of type `α`. This file expands on the definitions in the core library, proving that all the core library parsers are `valid`. There are also lemmas on the composability of parsers. ## Main definitions * `parse_result.pos` : The position of a `char_buffer` at which a `parser α` has finished. * `parser.valid` : The property that a parser only moves forward within a buffer, in both cases of success or failure. ## Implementation details Lemmas about how parsers are valid are in the `valid` namespace. That allows using projection notation for shorter term proofs that are parallel to the definitions of the parsers in structure. -/ open parser parse_result /-- For some `parse_result α`, give the position at which the result was provided, in either the `done` or the `fail` case. -/ @[simp] def parse_result.pos {α} : parse_result α → ℕ | (done n _) := n | (fail n _) := n namespace parser section defn_lemmas variables {α β : Type} (msgs : thunk (list string)) (msg : thunk string) variables (p q : parser α) (cb : char_buffer) (n n' : ℕ) {err : dlist string} variables {a : α} {b : β} /-- A `parser α` is defined to be `valid` if the result `p cb n` it gives, for some `cb : char_buffer` and `n : ℕ`, (whether `done` or `fail`), is always at a `parse_result.pos` that is at least `n`. Additionally, if the position of the result of the parser was within the size of the `cb`, then the input to the parser must have been within `cb.size` too. -/ def valid : Prop := ∀ (cb : char_buffer) (n : ℕ), n ≤ (p cb n).pos ∧ ((p cb n).pos ≤ cb.size → n ≤ cb.size) lemma fail_iff : (∀ pos' result, p cb n ≠ done pos' result) ↔ ∃ (pos' : ℕ) (err : dlist string), p cb n = fail pos' err := by cases p cb n; simp lemma success_iff : (∀ pos' err, p cb n ≠ fail pos' err) ↔ ∃ (pos' : ℕ) (result : α), p cb n = done pos' result := by cases p cb n; simp variables {p q cb n n' msgs msg} lemma decorate_errors_fail (h : p cb n = fail n' err) : @decorate_errors α msgs p cb n = fail n ((dlist.lazy_of_list (msgs ()))) := by simp [decorate_errors, h] lemma decorate_errors_success (h : p cb n = done n' a) : @decorate_errors α msgs p cb n = done n' a := by simp [decorate_errors, h] lemma decorate_error_fail (h : p cb n = fail n' err) : @decorate_error α msg p cb n = fail n ((dlist.lazy_of_list ([msg ()]))) := decorate_errors_fail h lemma decorate_error_success (h : p cb n = done n' a) : @decorate_error α msg p cb n = done n' a := decorate_errors_success h @[simp] lemma decorate_errors_eq_done : @decorate_errors α msgs p cb n = done n' a ↔ p cb n = done n' a := by cases h : p cb n; simp [decorate_errors, h] @[simp] lemma decorate_error_eq_done : @decorate_error α msg p cb n = done n' a ↔ p cb n = done n' a := decorate_errors_eq_done @[simp] lemma decorate_errors_eq_fail : @decorate_errors α msgs p cb n = fail n err ↔ err = dlist.lazy_of_list (msgs ()) ∧ ∃ np err', p cb n = fail np err' := by cases h : p cb n; simp [decorate_errors, h, eq_comm] @[simp] lemma decorate_error_eq_fail : @decorate_error α msg p cb n = fail n err ↔ err = dlist.lazy_of_list ([msg ()]) ∧ ∃ np err', p cb n = fail np err' := decorate_errors_eq_fail @[simp] lemma return_eq_pure : (@return parser _ _ a) = pure a := rfl lemma pure_eq_done : (@pure parser _ _ a) = λ _ n, done n a := rfl @[simp] lemma pure_ne_fail : (pure a : parser α) cb n ≠ fail n' err := by simp [pure_eq_done] section bind variable (f : α → parser β) @[simp] lemma bind_eq_bind : p.bind f = p >>= f := rfl variable {f} @[simp] lemma bind_eq_done : (p >>= f) cb n = done n' b ↔ ∃ (np : ℕ) (a : α), p cb n = done np a ∧ f a cb np = done n' b := by cases hp : p cb n; simp [hp, ←bind_eq_bind, parser.bind, and_assoc] @[simp] lemma bind_eq_fail : (p >>= f) cb n = fail n' err ↔ (p cb n = fail n' err) ∨ (∃ (np : ℕ) (a : α), p cb n = done np a ∧ f a cb np = fail n' err) := by cases hp : p cb n; simp [hp, ←bind_eq_bind, parser.bind, and_assoc] @[simp] lemma and_then_eq_bind {α β : Type} {m : Type → Type} [monad m] (a : m α) (b : m β) : a >> b = a >>= (λ _, b) := rfl lemma and_then_fail : (p >> return ()) cb n = parse_result.fail n' err ↔ p cb n = fail n' err := by simp [pure_eq_done] lemma and_then_success : (p >> return ()) cb n = parse_result.done n' () ↔ ∃ a, p cb n = done n' a:= by simp [pure_eq_done] end bind section map variable {f : α → β} @[simp] lemma map_eq_done : (f <$> p) cb n = done n' b ↔ ∃ (a : α), p cb n = done n' a ∧ f a = b := by cases hp : p cb n; simp [←is_lawful_monad.bind_pure_comp_eq_map, hp, and_assoc, pure_eq_done] @[simp] lemma map_eq_fail : (f <$> p) cb n = fail n' err ↔ p cb n = fail n' err := by simp [←bind_pure_comp_eq_map, pure_eq_done] @[simp] lemma map_const_eq_done {b'} : (b <$ p) cb n = done n' b' ↔ ∃ (a : α), p cb n = done n' a ∧ b = b' := by simp [map_const_eq] @[simp] lemma map_const_eq_fail : (b <$ p) cb n = fail n' err ↔ p cb n = fail n' err := by simp only [map_const_eq, map_eq_fail] lemma map_const_rev_eq_done {b'} : (p $> b) cb n = done n' b' ↔ ∃ (a : α), p cb n = done n' a ∧ b = b' := map_const_eq_done lemma map_rev_const_eq_fail : (p $> b) cb n = fail n' err ↔ p cb n = fail n' err := map_const_eq_fail end map @[simp] lemma orelse_eq_orelse : p.orelse q = (p <|> q) := rfl @[simp] lemma orelse_eq_done : (p <|> q) cb n = done n' a ↔ (p cb n = done n' a ∨ (q cb n = done n' a ∧ ∃ err, p cb n = fail n err)) := begin cases hp : p cb n with np resp np errp, { simp [hp, ←orelse_eq_orelse, parser.orelse] }, { by_cases hn : np = n, { cases hq : q cb n with nq resq nq errq, { simp [hp, hn, hq, ←orelse_eq_orelse, parser.orelse] }, { rcases lt_trichotomy nq n with H|rfl|H; simp [hp, hn, hq, H, not_lt_of_lt H, lt_irrefl, ←orelse_eq_orelse, parser.orelse] <|> simp [hp, hn, hq, lt_irrefl, ←orelse_eq_orelse, parser.orelse] } }, { simp [hp, hn, ←orelse_eq_orelse, parser.orelse] } } end @[simp] lemma orelse_eq_fail_eq : (p <|> q) cb n = fail n err ↔ (p cb n = fail n err ∧ ∃ (nq errq), n < nq ∧ q cb n = fail nq errq) ∨ (∃ (errp errq), p cb n = fail n errp ∧ q cb n = fail n errq ∧ errp ++ errq = err) := begin cases hp : p cb n with np resp np errp, { simp [hp, ←orelse_eq_orelse, parser.orelse] }, { by_cases hn : np = n, { cases hq : q cb n with nq resq nq errq, { simp [hp, hn, hq, ←orelse_eq_orelse, parser.orelse] }, { rcases lt_trichotomy nq n with H|rfl|H; simp [hp, hq, hn, ←orelse_eq_orelse, parser.orelse, H, ne_of_gt H, ne_of_lt H, not_lt_of_lt H] <|> simp [hp, hq, hn, ←orelse_eq_orelse, parser.orelse, lt_irrefl] } }, { simp [hp, hn, ←orelse_eq_orelse, parser.orelse] } } end lemma orelse_eq_fail_invalid_lt (hn : n' < n) : (p <|> q) cb n = fail n' err ↔ (p cb n = fail n' err) ∨ (q cb n = fail n' err ∧ (∃ (errp), p cb n = fail n errp)) := begin cases hp : p cb n with np resp np errp, { simp [hp, ←orelse_eq_orelse, parser.orelse] }, { by_cases h : np = n, { cases hq : q cb n with nq resq nq errq, { simp [hp, h, hn, hq, ne_of_gt hn, ←orelse_eq_orelse, parser.orelse] }, { rcases lt_trichotomy nq n with H|H|H, { simp [hp, hq, h, H, ne_of_gt hn, not_lt_of_lt H, ←orelse_eq_orelse, parser.orelse] }, { simp [hp, hq, h, H, ne_of_gt hn, lt_irrefl, ←orelse_eq_orelse, parser.orelse] }, { simp [hp, hq, h, H, ne_of_gt (hn.trans H), ←orelse_eq_orelse, parser.orelse] } } }, { simp [hp, h, ←orelse_eq_orelse, parser.orelse] } } end lemma orelse_eq_fail_of_valid_ne (hv : q.valid) (hn : n ≠ n') : (p <|> q) cb n = fail n' err ↔ p cb n = fail n' err := begin cases hp : p cb n with np resp np errp, { simp [hp, ←orelse_eq_orelse, parser.orelse] }, { by_cases h : np = n, { cases hq : q cb n with nq resq nq errq, { simp [hp, h, hn, hq, hn, ←orelse_eq_orelse, parser.orelse] }, { have : n ≤ nq := by { convert (hv cb n).left, simp [hq] }, rcases eq_or_lt_of_le this with rfl|H, { simp [hp, hq, h, hn, lt_irrefl, ←orelse_eq_orelse, parser.orelse] }, { simp [hp, hq, h, hn, H, ←orelse_eq_orelse, parser.orelse] } } }, { simp [hp, h, ←orelse_eq_orelse, parser.orelse] } }, end @[simp] lemma failure_eq_failure : @parser.failure α = failure := rfl @[simp] lemma failure_def : (failure : parser α) cb n = fail n dlist.empty := rfl lemma not_failure_eq_done : ¬ (failure : parser α) cb n = done n' a := by simp lemma failure_eq_fail : (failure : parser α) cb n = fail n' err ↔ n = n' ∧ err = dlist.empty := by simp [eq_comm] lemma seq_eq_done {f : parser (α → β)} {p : parser α} : (f <*> p) cb n = done n' b ↔ ∃ (nf : ℕ) (f' : α → β) (a : α), f cb n = done nf f' ∧ p cb nf = done n' a ∧ f' a = b := by simp [seq_eq_bind_map] lemma seq_eq_fail {f : parser (α → β)} {p : parser α} : (f <*> p) cb n = fail n' err ↔ (f cb n = fail n' err) ∨ (∃ (nf : ℕ) (f' : α → β), f cb n = done nf f' ∧ p cb nf = fail n' err) := by simp [seq_eq_bind_map] lemma seq_left_eq_done {p : parser α} {q : parser β} : (p <* q) cb n = done n' a ↔ ∃ (np : ℕ) (b : β), p cb n = done np a ∧ q cb np = done n' b := begin have : ∀ (p q : ℕ → α → Prop), (∃ (np : ℕ) (x : α), p np x ∧ q np x ∧ x = a) ↔ ∃ (np : ℕ), p np a ∧ q np a := λ _ _, ⟨λ ⟨np, x, hp, hq, rfl⟩, ⟨np, hp, hq⟩, λ ⟨np, hp, hq⟩, ⟨np, a, hp, hq, rfl⟩⟩, simp [seq_left_eq, seq_eq_done, map_eq_done, this] end lemma seq_left_eq_fail {p : parser α} {q : parser β} : (p <* q) cb n = fail n' err ↔ (p cb n = fail n' err) ∨ (∃ (np : ℕ) (a : α), p cb n = done np a ∧ q cb np = fail n' err) := by simp [seq_left_eq, seq_eq_fail] lemma seq_right_eq_done {p : parser α} {q : parser β} : (p *> q) cb n = done n' b ↔ ∃ (np : ℕ) (a : α), p cb n = done np a ∧ q cb np = done n' b := by simp [seq_right_eq, seq_eq_done, map_eq_done, and.comm, and.assoc] lemma seq_right_eq_fail {p : parser α} {q : parser β} : (p *> q) cb n = fail n' err ↔ (p cb n = fail n' err) ∨ (∃ (np : ℕ) (a : α), p cb n = done np a ∧ q cb np = fail n' err) := by simp [seq_right_eq, seq_eq_fail] lemma mmap_eq_done {f : α → parser β} {a : α} {l : list α} {b : β} {l' : list β} : (a :: l).mmap f cb n = done n' (b :: l') ↔ ∃ (np : ℕ), f a cb n = done np b ∧ l.mmap f cb np = done n' l' := by simp [mmap, and.comm, and.assoc, and.left_comm, pure_eq_done] lemma mmap'_eq_done {f : α → parser β} {a : α} {l : list α} : (a :: l).mmap' f cb n = done n' () ↔ ∃ (np : ℕ) (b : β), f a cb n = done np b ∧ l.mmap' f cb np = done n' () := by simp [mmap'] lemma guard_eq_done {p : Prop} [decidable p] : @guard parser _ p _ cb n = done n' () ↔ p ∧ n = n' := by { by_cases hp : p; simp [guard, hp, pure_eq_done] } lemma guard_eq_fail {p : Prop} [decidable p] : @guard parser _ p _ cb n = fail n' err ↔ (¬ p) ∧ n = n' ∧ err = dlist.empty := by { by_cases hp : p; simp [guard, hp, eq_comm, pure_eq_done] } section valid variables {sep : parser unit} namespace valid lemma mono_done (hp : p.valid) (h : p cb n = done n' a) : n ≤ n' := by simpa [h] using (hp cb n).left lemma mono_fail (hp : p.valid) (h : p cb n = fail n' err) : n ≤ n' := by simpa [h] using (hp cb n).left lemma pure : valid (pure a) := λ _ _, by simp [pure_eq_done] @[simp] lemma bind {f : α → parser β} (hp : p.valid) (hf : ∀ a, (f a).valid) : (p >>= f).valid := begin intros cb n, cases hx : (p >>= f) cb n, { obtain ⟨n', a, h, h'⟩ := bind_eq_done.mp hx, refine ⟨le_trans (hp cb n).left (h' ▸ _), λ hn, (hp cb n).right _⟩, { simp [h, hf a cb n'] }, { simp [h, (hf a cb n').right (h'.symm ▸ hn)] } }, { obtain h | ⟨n', a, h, h'⟩ := bind_eq_fail.mp hx, { simpa only [h] using hp cb n }, { refine ⟨le_trans (hp cb n).left (h' ▸ _), λ hn, (hp cb n).right _⟩, { simp [h, hf a cb n'] }, { simp [h, (hf a cb n').right (h'.symm ▸ hn)] } } } end lemma and_then {q : parser β} (hp : p.valid) (hq : q.valid) : (p >> q).valid := hp.bind (λ _, hq) @[simp] lemma map (hp : p.valid) {f : α → β} : (f <$> p).valid := hp.bind (λ _, pure) @[simp] lemma seq {f : parser (α → β)} (hf : f.valid) (hp : p.valid) : (f <*> p).valid := hf.bind (λ _, hp.map) @[simp] lemma mmap {l : list α} {f : α → parser β} (h : ∀ a ∈ l, (f a).valid) : (l.mmap f).valid := begin induction l with hd tl hl generalizing h, { exact pure }, { exact bind (h _ (list.mem_cons_self _ _)) (λ b, map (hl (λ _ ha, h _ (list.mem_cons_of_mem _ ha)))) } end @[simp] lemma mmap' {l : list α} {f : α → parser β} (h : ∀ a ∈ l, (f a).valid) : (l.mmap' f).valid := begin induction l with hd tl hl generalizing h, { exact pure }, { exact and_then (h _ (list.mem_cons_self _ _)) (hl (λ _ ha, h _ (list.mem_cons_of_mem _ ha))) } end @[simp] lemma failure : @parser.valid α failure := by simp [valid, le_refl] @[simp] lemma guard {p : Prop} [decidable p] : valid (guard p) := by simp only [guard, apply_ite valid, pure, failure, or_true, if_true_left_eq_or] @[simp] lemma orelse (hp : p.valid) (hq : q.valid) : (p <|> q).valid := begin intros cb n, cases hx : (p <|> q) cb n with posx resx posx errx, { obtain h | ⟨h, -, -⟩ := orelse_eq_done.mp hx, { simpa [h] using hp cb n }, { simpa [h] using hq cb n } }, { by_cases h : n = posx, { simp [hx, h] }, { simpa [(orelse_eq_fail_of_valid_ne hq h).mp hx] using hp cb n } } end @[simp] lemma decorate_errors (hp : p.valid) : (@decorate_errors α msgs p).valid := begin intros cb n, cases h : p cb n, { simpa [decorate_errors, h] using hp cb n }, { simp [decorate_errors, h] } end @[simp] lemma decorate_error (hp : p.valid) : (@decorate_error α msg p).valid := decorate_errors hp @[simp] lemma any_char : valid any_char := begin intros cb n, by_cases h : n < cb.size, { simpa [any_char, h] using nat.le_of_succ_le }, { simp [any_char, h] } end @[simp] lemma sat {p : char → Prop} [decidable_pred p] : valid (sat p) := begin intros cb n, simp only [sat], split_ifs, { simpa using nat.le_of_succ_le }, { simp }, { simp } end @[simp] lemma eps : valid eps := pure lemma ch {c : char} : valid (ch c) := decorate_error (sat.and_then eps) lemma char_buf {s : char_buffer} : valid (char_buf s) := decorate_error (mmap' (λ _ _, ch)) lemma one_of {cs : list char} : (one_of cs).valid := decorate_errors sat lemma one_of' {cs : list char} : (one_of' cs).valid := one_of.and_then eps lemma str {s : string} : (str s).valid := decorate_error (mmap' (λ _ _, ch)) lemma remaining : remaining.valid := λ _ _, ⟨le_refl _, λ h, h⟩ lemma eof : eof.valid := decorate_error (remaining.bind (λ _, guard)) lemma foldr_core_zero {f : α → β → β} {b : β} : (foldr_core f p b 0).valid := failure lemma foldr_core {f : α → β → β} {b : β} (hp : p.valid) : ∀ {reps : ℕ}, (foldr_core f p b reps).valid | 0 := failure | (reps + 1) := orelse (hp.bind (λ _, foldr_core.bind (λ _, pure))) pure lemma foldr {f : α → β → β} (hp : p.valid) : valid (foldr f p b) := λ _ _, foldr_core hp _ _ lemma foldl_core_zero {f : β → α → β} {b : β} : (foldl_core f b p 0).valid := failure lemma foldl_core {f : α → β → α} {p : parser β} (hp : p.valid) : ∀ {a : α} {reps : ℕ}, (foldl_core f a p reps).valid | _ 0 := failure | _ (reps + 1) := orelse (hp.bind (λ _, foldl_core)) pure lemma foldl {f : α → β → α} {p : parser β} (hp : p.valid) : valid (foldl f a p) := λ _ _, foldl_core hp _ _ lemma many (hp : p.valid) : p.many.valid := hp.foldr lemma many_char {p : parser char} (hp : p.valid) : p.many_char.valid := hp.many.map lemma many' (hp : p.valid) : p.many'.valid := hp.many.and_then eps lemma many1 (hp : p.valid) : p.many1.valid := hp.map.seq hp.many lemma many_char1 {p : parser char} (hp : p.valid) : p.many_char1.valid := hp.many1.map lemma sep_by1 {sep : parser unit} (hp : p.valid) (hs : sep.valid) : valid (sep_by1 sep p) := hp.map.seq (hs.and_then hp).many lemma sep_by {sep : parser unit} (hp : p.valid) (hs : sep.valid) : valid (sep_by sep p) := (hp.sep_by1 hs).orelse pure lemma fix_core {F : parser α → parser α} (hF : ∀ (p : parser α), p.valid → (F p).valid) : ∀ (max_depth : ℕ), valid (fix_core F max_depth) | 0 := failure | (max_depth + 1) := hF _ (fix_core _) lemma digit : digit.valid := decorate_error (sat.bind (λ _, pure)) lemma nat : nat.valid := decorate_error (digit.many1.bind (λ _, pure)) lemma fix {F : parser α → parser α} (hF : ∀ (p : parser α), p.valid → (F p).valid) : valid (fix F) := λ _ _, fix_core hF _ _ _ end valid end valid @[simp] lemma orelse_pure_eq_fail : (p <|> pure a) cb n = fail n' err ↔ p cb n = fail n' err ∧ n ≠ n' := begin by_cases hn : n = n', { simp [hn, pure_eq_done] }, { rw [orelse_eq_fail_of_valid_ne valid.pure hn], simp [hn] } end end defn_lemmas section done variables {α β : Type} {cb : char_buffer} {n n' : ℕ} (hn : n < cb.size) {a a' : α} {b : β} {c : char} include hn lemma any_char_eq_done : any_char cb n = done n' c ↔ n' = n + 1 ∧ cb.read ⟨n, hn⟩ = c := by simp [any_char, hn, eq_comm] lemma sat_eq_done {p : char → Prop} [decidable_pred p] : sat p cb n = done n' c ↔ p c ∧ n' = n + 1 ∧ cb.read ⟨n, hn⟩ = c := begin by_cases hp : p (cb.read ⟨n, hn⟩), { simp only [sat, hp, hn, dif_pos, if_true], split, { rintro ⟨rfl, rfl⟩, simp [hp] }, { rintro ⟨-, rfl, rfl⟩, simp } }, { simp [sat, hp, hn, dif_pos, false_iff, not_and, if_false], rintro hc rfl rfl, exact hp hc } end omit hn lemma eps_eq_done : eps cb n = done n' () ↔ n = n' := by simp [eps, pure_eq_done] include hn lemma ch_eq_done : ch c cb n = done n' () ↔ n' = n + 1 ∧ cb.read ⟨n, hn⟩ = c := by simp [ch, hn, eps_eq_done, sat_eq_done, and.comm, @eq_comm _ n'] -- TODO: add char_buf_eq_done, needs lemmas about matching buffers lemma one_of_eq_done {cs : list char} : one_of cs cb n = done n' c ↔ c ∈ cs ∧ n' = n + 1 ∧ cb.read ⟨n, hn⟩ = c := by simp [one_of, hn, sat_eq_done] lemma one_of'_eq_done {cs : list char} : one_of' cs cb n = done n' () ↔ cb.read ⟨n, hn⟩ ∈ cs ∧ n' = n + 1 := begin have : ∀ {x : char} {p : Prop}, (∃ (z : char), z ∈ cs ∧ p ∧ x = z) ↔ x ∈ cs ∧ p, { intros x p, exact ⟨λ ⟨z, hz, hp, rfl⟩, ⟨hz, hp⟩, λ ⟨hx, hp⟩, ⟨x, hx, hp, rfl⟩⟩ }, simp [one_of', hn, one_of_eq_done, sat_eq_done, eps_eq_done, this] end omit hn lemma remaining_eq_done {r : ℕ} : remaining cb n = done n' r ↔ n = n' ∧ cb.size - n = r := by simp [remaining] lemma eof_eq_done : eof cb n = done n' () ↔ n = n' ∧ cb.size ≤ n := by simp [eof, guard_eq_done, remaining_eq_done, nat.sub_eq_zero_iff_le, and_comm, and_assoc] @[simp] lemma foldr_core_zero_eq_done {f : α → β → β} {p : parser α} {b' : β} : foldr_core f p b 0 cb n ≠ done n' b' := by simp [foldr_core] lemma foldr_core_eq_done {f : α → β → β} {p : parser α} {reps : ℕ} {b' : β} : foldr_core f p b (reps + 1) cb n = done n' b' ↔ (∃ (np : ℕ) (a : α) (xs : β), p cb n = done np a ∧ foldr_core f p b reps cb np = done n' xs ∧ f a xs = b') ∨ (n = n' ∧ b = b' ∧ ∃ (err), (p cb n = fail n err) ∨ (∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldr_core f p b reps cb np = fail n err)) := by simp [foldr_core, and.comm, and.assoc, pure_eq_done] @[simp] lemma foldr_core_zero_eq_fail {f : α → β → β} {p : parser α} {err : dlist string} : foldr_core f p b 0 cb n = fail n' err ↔ n = n' ∧ err = dlist.empty := by simp [foldr_core, eq_comm] lemma foldr_core_succ_eq_fail {f : α → β → β} {p : parser α} {reps : ℕ} {err : dlist string} : foldr_core f p b (reps + 1) cb n = fail n' err ↔ n ≠ n' ∧ (p cb n = fail n' err ∨ ∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldr_core f p b reps cb np = fail n' err) := by simp [foldr_core, and_comm] lemma foldr_eq_done {f : α → β → β} {p : parser α} {b' : β} : foldr f p b cb n = done n' b' ↔ ((∃ (np : ℕ) (a : α) (x : β), p cb n = done np a ∧ foldr_core f p b (cb.size - n) cb np = done n' x ∧ f a x = b') ∨ (n = n' ∧ b = b' ∧ (∃ (err), p cb n = parse_result.fail n err ∨ ∃ (np : ℕ) (x : α), p cb n = done np x ∧ foldr_core f p b (cb.size - n) cb np = fail n err))) := by simp [foldr, foldr_core_eq_done] lemma foldr_eq_fail_of_valid_at_end {f : α → β → β} {p : parser α} {err : dlist string} (hp : p.valid) (hc : cb.size ≤ n) : foldr f p b cb n = fail n' err ↔ n < n' ∧ (p cb n = fail n' err ∨ ∃ (a : α), p cb n = done n' a ∧ err = dlist.empty) := begin have : cb.size - n = 0 := nat.sub_eq_zero_of_le hc, simp only [foldr, foldr_core_succ_eq_fail, this, and.left_comm, foldr_core_zero_eq_fail, ne_iff_lt_iff_le, exists_and_distrib_right, exists_eq_left, and.congr_left_iff, exists_and_distrib_left], rintro (h | ⟨⟨a, h⟩, rfl⟩), { exact hp.mono_fail h }, { exact hp.mono_done h } end lemma foldr_eq_fail {f : α → β → β} {p : parser α} {err : dlist string} : foldr f p b cb n = fail n' err ↔ n ≠ n' ∧ (p cb n = fail n' err ∨ ∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldr_core f p b (cb.size - n) cb np = fail n' err) := by simp [foldr, foldr_core_succ_eq_fail] @[simp] lemma foldl_core_zero_eq_done {f : β → α → β} {p : parser α} {b' : β} : foldl_core f b p 0 cb n = done n' b' ↔ false := by simp [foldl_core] lemma foldl_core_eq_done {f : β → α → β} {p : parser α} {reps : ℕ} {b' : β} : foldl_core f b p (reps + 1) cb n = done n' b' ↔ (∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldl_core f (f b a) p reps cb np = done n' b') ∨ (n = n' ∧ b = b' ∧ ∃ (err), (p cb n = fail n err) ∨ (∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldl_core f (f b a) p reps cb np = fail n err)) := by simp [foldl_core, and.assoc, pure_eq_done] @[simp] lemma foldl_core_zero_eq_fail {f : β → α → β} {p : parser α} {err : dlist string} : foldl_core f b p 0 cb n = fail n' err ↔ n = n' ∧ err = dlist.empty := by simp [foldl_core, eq_comm] lemma foldl_core_succ_eq_fail {f : β → α → β} {p : parser α} {reps : ℕ} {err : dlist string} : foldl_core f b p (reps + 1) cb n = fail n' err ↔ n ≠ n' ∧ (p cb n = fail n' err ∨ ∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldl_core f (f b a) p reps cb np = fail n' err) := by simp [foldl_core, and_comm] lemma foldl_eq_done {f : β → α → β} {p : parser α} {b' : β} : foldl f b p cb n = done n' b' ↔ (∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldl_core f (f b a) p (cb.size - n) cb np = done n' b') ∨ (n = n' ∧ b = b' ∧ ∃ (err), (p cb n = fail n err) ∨ (∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldl_core f (f b a) p (cb.size - n) cb np = fail n err)) := by simp [foldl, foldl_core_eq_done] lemma foldl_eq_fail {f : β → α → β} {p : parser α} {err : dlist string} : foldl f b p cb n = fail n' err ↔ n ≠ n' ∧ (p cb n = fail n' err ∨ ∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldl_core f (f b a) p (cb.size - n) cb np = fail n' err) := by simp [foldl, foldl_core_succ_eq_fail] lemma many_eq_done_nil {p : parser α} : many p cb n = done n' (@list.nil α) ↔ n = n' ∧ ∃ (err), p cb n = fail n err ∨ ∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldr_core list.cons p [] (cb.size - n) cb np = fail n err := by simp [many, foldr_eq_done] lemma many_eq_done {p : parser α} {x : α} {xs : list α} : many p cb n = done n' (x :: xs) ↔ ∃ (np : ℕ), p cb n = done np x ∧ foldr_core list.cons p [] (cb.size - n) cb np = done n' xs := by simp [many, foldr_eq_done, and.comm, and.assoc, and.left_comm] lemma many_eq_fail {p : parser α} {err : dlist string} : many p cb n = fail n' err ↔ n ≠ n' ∧ (p cb n = fail n' err ∨ ∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldr_core list.cons p [] (cb.size - n) cb np = fail n' err) := by simp [many, foldr_eq_fail] lemma many_char_eq_done_empty {p : parser char} : many_char p cb n = done n' string.empty ↔ n = n' ∧ ∃ (err), p cb n = fail n err ∨ ∃ (np : ℕ) (c : char), p cb n = done np c ∧ foldr_core list.cons p [] (cb.size - n) cb np = fail n err := by simp [many_char, many_eq_done_nil, map_eq_done, list.as_string_eq] lemma many_char_eq_done_not_empty {p : parser char} {s : string} (h : s ≠ "") : many_char p cb n = done n' s ↔ ∃ (np : ℕ), p cb n = done np s.head ∧ foldr_core list.cons p list.nil (buffer.size cb - n) cb np = done n' (s.popn 1).to_list := by simp [many_char, list.as_string_eq, string.to_list_nonempty h, many_eq_done] lemma many_char_eq_many_of_to_list {p : parser char} {s : string} : many_char p cb n = done n' s ↔ many p cb n = done n' s.to_list := by simp [many_char, list.as_string_eq] lemma many'_eq_done {p : parser α} : many' p cb n = done n' () ↔ many p cb n = done n' [] ∨ ∃ (np : ℕ) (a : α) (l : list α), many p cb n = done n' (a :: l) ∧ p cb n = done np a ∧ foldr_core list.cons p [] (buffer.size cb - n) cb np = done n' l := begin simp only [many', eps_eq_done, many, foldr, and_then_eq_bind, exists_and_distrib_right, bind_eq_done, exists_eq_right], split, { rintro ⟨_ | ⟨hd, tl⟩, hl⟩, { exact or.inl hl }, { have hl2 := hl, simp only [foldr_core_eq_done, or_false, exists_and_distrib_left, and_false, false_and, exists_eq_right_right] at hl, obtain ⟨np, hp, h⟩ := hl, refine or.inr ⟨np, _, _, hl2, hp, h⟩ } }, { rintro (h | ⟨np, a, l, hp, h⟩), { exact ⟨[], h⟩ }, { refine ⟨a :: l, hp⟩ } } end @[simp] lemma many1_ne_done_nil {p : parser α} : many1 p cb n ≠ done n' [] := by simp [many1, seq_eq_done] lemma many1_eq_done {p : parser α} {l : list α} : many1 p cb n = done n' (a :: l) ↔ ∃ (np : ℕ), p cb n = done np a ∧ many p cb np = done n' l := by simp [many1, seq_eq_done, map_eq_done] lemma many1_eq_fail {p : parser α} {err : dlist string} : many1 p cb n = fail n' err ↔ p cb n = fail n' err ∨ (∃ (np : ℕ) (a : α), p cb n = done np a ∧ many p cb np = fail n' err) := by simp [many1, seq_eq_fail] @[simp] lemma many_char1_ne_empty {p : parser char} : many_char1 p cb n ≠ done n' "" := by simp [many_char1, ←string.nil_as_string_eq_empty] lemma many_char1_eq_done {p : parser char} {s : string} (h : s ≠ "") : many_char1 p cb n = done n' s ↔ ∃ (np : ℕ), p cb n = done np s.head ∧ many_char p cb np = done n' (s.popn 1) := by simp [many_char1, list.as_string_eq, string.to_list_nonempty h, many1_eq_done, many_char_eq_many_of_to_list] @[simp] lemma sep_by1_ne_done_nil {sep : parser unit} {p : parser α} : sep_by1 sep p cb n ≠ done n' [] := by simp [sep_by1, seq_eq_done] lemma sep_by1_eq_done {sep : parser unit} {p : parser α} {l : list α} : sep_by1 sep p cb n = done n' (a :: l) ↔ ∃ (np : ℕ), p cb n = done np a ∧ (sep >> p).many cb np = done n' l := by simp [sep_by1, seq_eq_done] lemma sep_by_eq_done_nil {sep : parser unit} {p : parser α} : sep_by sep p cb n = done n' [] ↔ n = n' ∧ ∃ (err), sep_by1 sep p cb n = fail n err := by simp [sep_by, pure_eq_done] @[simp] lemma fix_core_ne_done_zero {F : parser α → parser α} : fix_core F 0 cb n ≠ done n' a := by simp [fix_core] lemma fix_core_eq_done {F : parser α → parser α} {max_depth : ℕ} : fix_core F (max_depth + 1) cb n = done n' a ↔ F (fix_core F max_depth) cb n = done n' a := by simp [fix_core] include hn lemma digit_eq_done {k : ℕ} : digit cb n = done n' k ↔ n' = n + 1 ∧ k ≤ 9 ∧ (cb.read ⟨n, hn⟩).to_nat - '0'.to_nat = k ∧ '0' ≤ cb.read ⟨n, hn⟩ ∧ cb.read ⟨n, hn⟩ ≤ '9' := begin have c9 : '9'.to_nat - '0'.to_nat = 9 := rfl, have l09 : '0'.to_nat ≤ '9'.to_nat := dec_trivial, have le_iff_le : ∀ {c c' : char}, c ≤ c' ↔ c.to_nat ≤ c'.to_nat := λ _ _, iff.rfl, simp only [digit, pure_eq_done, sat_eq_done, hn, and.left_comm, exists_eq_left, bind_eq_done, and.congr_right_iff, @eq_comm _ n', decorate_error_eq_done, and.comm, exists_and_distrib_left], simp only [←and.assoc, exists_eq_right', le_iff_le, and.congr_left_iff, ←c9], rintro hn rfl hl, simp [l09, nat.sub_le_sub_right_iff, hl], end end done end parser
7240444c8c9e787cf61bc51c1342f10f15feb522
f3849be5d845a1cb97680f0bbbe03b85518312f0
/library/init/data/quot.lean
013553b3dd109df2730b8a90ed1a31ee103ea7c2
[ "Apache-2.0" ]
permissive
bjoeris/lean
0ed95125d762b17bfcb54dad1f9721f953f92eeb
4e496b78d5e73545fa4f9a807155113d8e6b0561
refs/heads/master
1,611,251,218,281
1,495,337,658,000
1,495,337,658,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,374
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 Quotient types. -/ prelude /- We import propext here, otherwise we would need a quot.lift for propositions. -/ import init.data.sigma.basic init.logic init.propext init.data.setoid universes u v -- iff can now be used to do substitutions in a calculation attribute [subst] lemma iff_subst {a b : Prop} {p : Prop → Prop} (h₁ : a ↔ b) (h₂ : p a) : p b := eq.subst (propext h₁) h₂ namespace quot constant sound : Π {α : Sort u} {r : α → α → Prop} {a b : α}, r a b → quot.mk r a = quot.mk r b attribute [elab_as_eliminator] lift ind protected lemma lift_beta {α : Sort u} {r : α → α → Prop} {β : Sort v} (f : α → β) (c : ∀ a b, r a b → f a = f b) (a : α) : lift f c (quot.mk r a) = f a := rfl protected lemma ind_beta {α : Sort u} {r : α → α → Prop} {β : quot r → Prop} (p : ∀ a, β (quot.mk r a)) (a : α) : (ind p (quot.mk r a) : β (quot.mk r a)) = p a := rfl attribute [reducible, elab_as_eliminator] protected def lift_on {α : Sort u} {β : Sort v} {r : α → α → Prop} (q : quot r) (f : α → β) (c : ∀ a b, r a b → f a = f b) : β := lift f c q attribute [elab_as_eliminator] protected lemma induction_on {α : Sort u} {r : α → α → Prop} {β : quot r → Prop} (q : quot r) (h : ∀ a, β (quot.mk r a)) : β q := ind h q lemma exists_rep {α : Sort u} {r : α → α → Prop} (q : quot r) : ∃ a : α, (quot.mk r a) = q := quot.induction_on q (λ a, ⟨a, rfl⟩) section variable {α : Sort u} variable {r : α → α → Prop} variable {β : quot r → Sort v} local notation `⟦`:max a `⟧` := quot.mk r a attribute [reducible] protected def indep (f : Π a, β ⟦a⟧) (a : α) : psigma β := ⟨⟦a⟧, f a⟩ protected lemma indep_coherent (f : Π a, β ⟦a⟧) (h : ∀ (a b : α) (p : r a b), (eq.rec (f a) (sound p) : β ⟦b⟧) = f b) : ∀ a b, r a b → quot.indep f a = quot.indep f b := λ a b e, psigma.eq (sound e) (h a b e) protected lemma lift_indep_pr1 (f : Π a, β ⟦a⟧) (h : ∀ (a b : α) (p : r a b), (eq.rec (f a) (sound p) : β ⟦b⟧) = f b) (q : quot r) : (lift (quot.indep f) (quot.indep_coherent f h) q).1 = q := quot.ind (λ (a : α), eq.refl (quot.indep f a).1) q attribute [reducible, elab_as_eliminator] protected def rec (f : Π a, β ⟦a⟧) (h : ∀ (a b : α) (p : r a b), (eq.rec (f a) (sound p) : β ⟦b⟧) = f b) (q : quot r) : β q := eq.rec_on (quot.lift_indep_pr1 f h q) ((lift (quot.indep f) (quot.indep_coherent f h) q).2) attribute [reducible, elab_as_eliminator] protected def rec_on (q : quot r) (f : Π a, β ⟦a⟧) (h : ∀ (a b : α) (p : r a b), (eq.rec (f a) (sound p) : β ⟦b⟧) = f b) : β q := quot.rec f h q attribute [reducible, elab_as_eliminator] protected def rec_on_subsingleton [h : ∀ a, subsingleton (β ⟦a⟧)] (q : quot r) (f : Π a, β ⟦a⟧) : β q := quot.rec f (λ a b h, subsingleton.elim _ (f b)) q attribute [reducible, elab_as_eliminator] protected def hrec_on (q : quot r) (f : Π a, β ⟦a⟧) (c : ∀ (a b : α) (p : r a b), f a == f b) : β q := quot.rec_on q f (λ a b p, eq_of_heq (calc (eq.rec (f a) (sound p) : β ⟦b⟧) == f a : eq_rec_heq (sound p) (f a) ... == f b : c a b p)) end end quot def quotient {α : Sort u} (s : setoid α) := @quot α setoid.r namespace quotient protected def mk {α : Sort u} [s : setoid α] (a : α) : quotient s := quot.mk setoid.r a notation `⟦`:max a `⟧`:0 := quotient.mk a def sound {α : Sort u} [s : setoid α] {a b : α} : a ≈ b → ⟦a⟧ = ⟦b⟧ := quot.sound attribute [reducible, elab_as_eliminator] protected def lift {α : Sort u} {β : Sort v} [s : setoid α] (f : α → β) : (∀ a b, a ≈ b → f a = f b) → quotient s → β := quot.lift f attribute [elab_as_eliminator] protected lemma ind {α : Sort u} [s : setoid α] {β : quotient s → Prop} : (∀ a, β ⟦a⟧) → ∀ q, β q := quot.ind attribute [reducible, elab_as_eliminator] protected def lift_on {α : Sort u} {β : Sort v} [s : setoid α] (q : quotient s) (f : α → β) (c : ∀ a b, a ≈ b → f a = f b) : β := quot.lift_on q f c attribute [elab_as_eliminator] protected lemma induction_on {α : Sort u} [s : setoid α] {β : quotient s → Prop} (q : quotient s) (h : ∀ a, β ⟦a⟧) : β q := quot.induction_on q h lemma exists_rep {α : Sort u} [s : setoid α] (q : quotient s) : ∃ a : α, ⟦a⟧ = q := quot.exists_rep q section variable {α : Sort u} variable [s : setoid α] variable {β : quotient s → Sort v} protected def rec (f : Π a, β ⟦a⟧) (h : ∀ (a b : α) (p : a ≈ b), (eq.rec (f a) (quotient.sound p) : β ⟦b⟧) = f b) (q : quotient s) : β q := quot.rec f h q attribute [reducible, elab_as_eliminator] protected def rec_on (q : quotient s) (f : Π a, β ⟦a⟧) (h : ∀ (a b : α) (p : a ≈ b), (eq.rec (f a) (quotient.sound p) : β ⟦b⟧) = f b) : β q := quot.rec_on q f h attribute [reducible, elab_as_eliminator] protected def rec_on_subsingleton [h : ∀ a, subsingleton (β ⟦a⟧)] (q : quotient s) (f : Π a, β ⟦a⟧) : β q := @quot.rec_on_subsingleton _ _ _ h q f attribute [reducible, elab_as_eliminator] protected def hrec_on (q : quotient s) (f : Π a, β ⟦a⟧) (c : ∀ (a b : α) (p : a ≈ b), f a == f b) : β q := quot.hrec_on q f c end section universes u_a u_b u_c variables {α : Sort u_a} {β : Sort u_b} {φ : Sort u_c} variables [s₁ : setoid α] [s₂ : setoid β] include s₁ s₂ attribute [reducible, elab_as_eliminator] protected def lift₂ (f : α → β → φ)(c : ∀ a₁ a₂ b₁ b₂, a₁ ≈ b₁ → a₂ ≈ b₂ → f a₁ a₂ = f b₁ b₂) (q₁ : quotient s₁) (q₂ : quotient s₂) : φ := quotient.lift (λ (a₁ : α), quot.lift (f a₁) (λ (a b : β), c a₁ a a₁ b (setoid.refl a₁)) q₂) (λ (a b : α) (h : a ≈ b), @quotient.ind β s₂ (λ (a_1 : quotient s₂), (quotient.lift (f a) (λ (a_1 b : β), c a a_1 a b (setoid.refl a)) a_1) = (quotient.lift (f b) (λ (a b_1 : β), c b a b b_1 (setoid.refl b)) a_1)) (λ (a' : β), c a a' b a' h (setoid.refl a')) q₂) q₁ attribute [reducible, elab_as_eliminator] protected def lift_on₂ (q₁ : quotient s₁) (q₂ : quotient s₂) (f : α → β → φ) (c : ∀ a₁ a₂ b₁ b₂, a₁ ≈ b₁ → a₂ ≈ b₂ → f a₁ a₂ = f b₁ b₂) : φ := quotient.lift₂ f c q₁ q₂ attribute [elab_as_eliminator] protected lemma ind₂ {φ : quotient s₁ → quotient s₂ → Prop} (h : ∀ a b, φ ⟦a⟧ ⟦b⟧) (q₁ : quotient s₁) (q₂ : quotient s₂) : φ q₁ q₂ := quotient.ind (λ a₁, quotient.ind (λ a₂, h a₁ a₂) q₂) q₁ attribute [elab_as_eliminator] protected lemma induction_on₂ {φ : quotient s₁ → quotient s₂ → Prop} (q₁ : quotient s₁) (q₂ : quotient s₂) (h : ∀ a b, φ ⟦a⟧ ⟦b⟧) : φ q₁ q₂ := quotient.ind (λ a₁, quotient.ind (λ a₂, h a₁ a₂) q₂) q₁ attribute [elab_as_eliminator] protected lemma induction_on₃ [s₃ : setoid φ] {δ : quotient s₁ → quotient s₂ → quotient s₃ → Prop} (q₁ : quotient s₁) (q₂ : quotient s₂) (q₃ : quotient s₃) (h : ∀ a b c, δ ⟦a⟧ ⟦b⟧ ⟦c⟧) : δ q₁ q₂ q₃ := quot.ind (λ a₁, quot.ind (λ a₂, quot.ind (λ a₃, h a₁ a₂ a₃) q₃) q₂) q₁ end section exact variable {α : Sort u} variable [s : setoid α] include s private def rel (q₁ q₂ : quotient s) : Prop := quotient.lift_on₂ q₁ q₂ (λ a₁ a₂, a₁ ≈ a₂) (λ a₁ a₂ b₁ b₂ a₁b₁ a₂b₂, propext (iff.intro (λ a₁a₂, setoid.trans (setoid.symm a₁b₁) (setoid.trans a₁a₂ a₂b₂)) (λ b₁b₂, setoid.trans a₁b₁ (setoid.trans b₁b₂ (setoid.symm a₂b₂))))) local infix `~` := rel private lemma rel.refl : ∀ q : quotient s, q ~ q := λ q, quot.induction_on q (λ a, setoid.refl a) private lemma eq_imp_rel {q₁ q₂ : quotient s} : q₁ = q₂ → q₁ ~ q₂ := assume h, eq.rec_on h (rel.refl q₁) lemma exact {a b : α} : ⟦a⟧ = ⟦b⟧ → a ≈ b := assume h, eq_imp_rel h end exact section universes u_a u_b u_c variables {α : Sort u_a} {β : Sort u_b} variables [s₁ : setoid α] [s₂ : setoid β] include s₁ s₂ attribute [reducible, elab_as_eliminator] protected def rec_on_subsingleton₂ {φ : quotient s₁ → quotient s₂ → Sort u_c} [h : ∀ a b, subsingleton (φ ⟦a⟧ ⟦b⟧)] (q₁ : quotient s₁) (q₂ : quotient s₂) (f : Π a b, φ ⟦a⟧ ⟦b⟧) : φ q₁ q₂:= @quotient.rec_on_subsingleton _ s₁ (λ q, φ q q₂) (λ a, quotient.ind (λ b, h a b) q₂) q₁ (λ a, quotient.rec_on_subsingleton q₂ (λ b, f a b)) end end quotient open decidable instance {α : Sort u} {s : setoid α} [d : ∀ a b : α, decidable (a ≈ b)] : decidable_eq (quotient s) := λ q₁ q₂ : quotient s, quotient.rec_on_subsingleton₂ q₁ q₂ (λ a₁ a₂, match (d a₁ a₂) with | (is_true h₁) := is_true (quotient.sound h₁) | (is_false h₂) := is_false (λ h, absurd (quotient.exact h) h₂) end)
58361c6b6b1f3c09de454bf42cdbb242baac850d
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/1334a.lean
847680c9f8d2d57a227c08f79ec3aa84c0e4a803
[ "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
199
lean
inductive nlist : Type | atom : nlist | mk : list nlist → nlist open nlist list def fn : nlist → nlist | (mk l) := mk [] | _ := atom #check fn.equations._eqn_1 #check fn.equations._eqn_2
89cc482f313ad829398941a1a732ba4d64b2c929
4727251e0cd73359b15b664c3170e5d754078599
/src/measure_theory/decomposition/jordan.lean
6ac5fb5ee48ac4151cf1f1c0b9bd02da6b9f404a
[ "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,315
lean
/- Copyright (c) 2021 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import measure_theory.decomposition.signed_hahn import measure_theory.measure.mutually_singular /-! # Jordan decomposition This file proves the existence and uniqueness of the Jordan decomposition for signed measures. The Jordan decomposition theorem states that, given a signed measure `s`, there exists a unique pair of mutually singular measures `μ` and `ν`, such that `s = μ - ν`. The Jordan decomposition theorem for measures is a corollary of the Hahn decomposition theorem and is useful for the Lebesgue decomposition theorem. ## Main definitions * `measure_theory.jordan_decomposition`: a Jordan decomposition of a measurable space is a pair of mutually singular finite measures. We say `j` is a Jordan decomposition of a signed measure `s` if `s = j.pos_part - j.neg_part`. * `measure_theory.signed_measure.to_jordan_decomposition`: the Jordan decomposition of a signed measure. * `measure_theory.signed_measure.to_jordan_decomposition_equiv`: is the `equiv` between `measure_theory.signed_measure` and `measure_theory.jordan_decomposition` formed by `measure_theory.signed_measure.to_jordan_decomposition`. ## Main results * `measure_theory.signed_measure.to_signed_measure_to_jordan_decomposition` : the Jordan decomposition theorem. * `measure_theory.jordan_decomposition.to_signed_measure_injective` : the Jordan decomposition of a signed measure is unique. ## Tags Jordan decomposition theorem -/ noncomputable theory open_locale classical measure_theory ennreal nnreal variables {α β : Type*} [measurable_space α] namespace measure_theory /-- A Jordan decomposition of a measurable space is a pair of mutually singular, finite measures. -/ @[ext] structure jordan_decomposition (α : Type*) [measurable_space α] := (pos_part neg_part : measure α) [pos_part_finite : is_finite_measure pos_part] [neg_part_finite : is_finite_measure neg_part] (mutually_singular : pos_part ⊥ₘ neg_part) attribute [instance] jordan_decomposition.pos_part_finite attribute [instance] jordan_decomposition.neg_part_finite namespace jordan_decomposition open measure vector_measure variable (j : jordan_decomposition α) instance : has_zero (jordan_decomposition α) := { zero := ⟨0, 0, mutually_singular.zero_right⟩ } instance : inhabited (jordan_decomposition α) := { default := 0 } instance : has_involutive_neg (jordan_decomposition α) := { neg := λ j, ⟨j.neg_part, j.pos_part, j.mutually_singular.symm⟩, neg_neg := λ j, jordan_decomposition.ext _ _ rfl rfl } instance : has_scalar ℝ≥0 (jordan_decomposition α) := { smul := λ r j, ⟨r • j.pos_part, r • j.neg_part, mutually_singular.smul _ (mutually_singular.smul _ j.mutually_singular.symm).symm⟩ } instance has_scalar_real : has_scalar ℝ (jordan_decomposition α) := { smul := λ r j, if hr : 0 ≤ r then r.to_nnreal • j else - ((-r).to_nnreal • j) } @[simp] lemma zero_pos_part : (0 : jordan_decomposition α).pos_part = 0 := rfl @[simp] lemma zero_neg_part : (0 : jordan_decomposition α).neg_part = 0 := rfl @[simp] lemma neg_pos_part : (-j).pos_part = j.neg_part := rfl @[simp] lemma neg_neg_part : (-j).neg_part = j.pos_part := rfl @[simp] lemma smul_pos_part (r : ℝ≥0) : (r • j).pos_part = r • j.pos_part := rfl @[simp] lemma smul_neg_part (r : ℝ≥0) : (r • j).neg_part = r • j.neg_part := rfl lemma real_smul_def (r : ℝ) (j : jordan_decomposition α) : r • j = if hr : 0 ≤ r then r.to_nnreal • j else - ((-r).to_nnreal • j) := rfl @[simp] lemma coe_smul (r : ℝ≥0) : (r : ℝ) • j = r • j := show dite _ _ _ = _, by rw [dif_pos (nnreal.coe_nonneg r), real.to_nnreal_coe] lemma real_smul_nonneg (r : ℝ) (hr : 0 ≤ r) : r • j = r.to_nnreal • j := dif_pos hr lemma real_smul_neg (r : ℝ) (hr : r < 0) : r • j = - ((-r).to_nnreal • j) := dif_neg (not_le.2 hr) lemma real_smul_pos_part_nonneg (r : ℝ) (hr : 0 ≤ r) : (r • j).pos_part = r.to_nnreal • j.pos_part := by { rw [real_smul_def, ← smul_pos_part, dif_pos hr] } lemma real_smul_neg_part_nonneg (r : ℝ) (hr : 0 ≤ r) : (r • j).neg_part = r.to_nnreal • j.neg_part := by { rw [real_smul_def, ← smul_neg_part, dif_pos hr] } lemma real_smul_pos_part_neg (r : ℝ) (hr : r < 0) : (r • j).pos_part = (-r).to_nnreal • j.neg_part := by { rw [real_smul_def, ← smul_neg_part, dif_neg (not_le.2 hr), neg_pos_part] } lemma real_smul_neg_part_neg (r : ℝ) (hr : r < 0) : (r • j).neg_part = (-r).to_nnreal • j.pos_part := by { rw [real_smul_def, ← smul_pos_part, dif_neg (not_le.2 hr), neg_neg_part] } /-- The signed measure associated with a Jordan decomposition. -/ def to_signed_measure : signed_measure α := j.pos_part.to_signed_measure - j.neg_part.to_signed_measure lemma to_signed_measure_zero : (0 : jordan_decomposition α).to_signed_measure = 0 := begin ext1 i hi, erw [to_signed_measure, to_signed_measure_sub_apply hi, sub_self, zero_apply], end lemma to_signed_measure_neg : (-j).to_signed_measure = -j.to_signed_measure := begin ext1 i hi, rw [neg_apply, to_signed_measure, to_signed_measure, to_signed_measure_sub_apply hi, to_signed_measure_sub_apply hi, neg_sub], refl, end lemma to_signed_measure_smul (r : ℝ≥0) : (r • j).to_signed_measure = r • j.to_signed_measure := begin ext1 i hi, rw [vector_measure.smul_apply, to_signed_measure, to_signed_measure, to_signed_measure_sub_apply hi, to_signed_measure_sub_apply hi, smul_sub, smul_pos_part, smul_neg_part, ← ennreal.to_real_smul, ← ennreal.to_real_smul], refl end /-- A Jordan decomposition provides a Hahn decomposition. -/ lemma exists_compl_positive_negative : ∃ S : set α, measurable_set S ∧ j.to_signed_measure ≤[S] 0 ∧ 0 ≤[Sᶜ] j.to_signed_measure ∧ j.pos_part S = 0 ∧ j.neg_part Sᶜ = 0 := begin obtain ⟨S, hS₁, hS₂, hS₃⟩ := j.mutually_singular, refine ⟨S, hS₁, _, _, hS₂, hS₃⟩, { refine restrict_le_restrict_of_subset_le _ _ (λ A hA hA₁, _), rw [to_signed_measure, to_signed_measure_sub_apply hA, show j.pos_part A = 0, by exact nonpos_iff_eq_zero.1 (hS₂ ▸ measure_mono hA₁), ennreal.zero_to_real, zero_sub, neg_le, zero_apply, neg_zero], exact ennreal.to_real_nonneg }, { refine restrict_le_restrict_of_subset_le _ _ (λ A hA hA₁, _), rw [to_signed_measure, to_signed_measure_sub_apply hA, show j.neg_part A = 0, by exact nonpos_iff_eq_zero.1 (hS₃ ▸ measure_mono hA₁), ennreal.zero_to_real, sub_zero], exact ennreal.to_real_nonneg }, end end jordan_decomposition namespace signed_measure open measure vector_measure jordan_decomposition classical variables {s : signed_measure α} {μ ν : measure α} [is_finite_measure μ] [is_finite_measure ν] /-- Given a signed measure `s`, `s.to_jordan_decomposition` is the Jordan decomposition `j`, such that `s = j.to_signed_measure`. This property is known as the Jordan decomposition theorem, and is shown by `measure_theory.signed_measure.to_signed_measure_to_jordan_decomposition`. -/ def to_jordan_decomposition (s : signed_measure α) : jordan_decomposition α := let i := some s.exists_compl_positive_negative in let hi := some_spec s.exists_compl_positive_negative in { pos_part := s.to_measure_of_zero_le i hi.1 hi.2.1, neg_part := s.to_measure_of_le_zero iᶜ hi.1.compl hi.2.2, pos_part_finite := infer_instance, neg_part_finite := infer_instance, mutually_singular := begin refine ⟨iᶜ, hi.1.compl, _, _⟩, { rw [to_measure_of_zero_le_apply _ _ hi.1 hi.1.compl], simp }, { rw [to_measure_of_le_zero_apply _ _ hi.1.compl hi.1.compl.compl], simp } end } lemma to_jordan_decomposition_spec (s : signed_measure α) : ∃ (i : set α) (hi₁ : measurable_set i) (hi₂ : 0 ≤[i] s) (hi₃ : s ≤[iᶜ] 0), s.to_jordan_decomposition.pos_part = s.to_measure_of_zero_le i hi₁ hi₂ ∧ s.to_jordan_decomposition.neg_part = s.to_measure_of_le_zero iᶜ hi₁.compl hi₃ := begin set i := some s.exists_compl_positive_negative, obtain ⟨hi₁, hi₂, hi₃⟩ := some_spec s.exists_compl_positive_negative, exact ⟨i, hi₁, hi₂, hi₃, rfl, rfl⟩, end /-- **The Jordan decomposition theorem**: Given a signed measure `s`, there exists a pair of mutually singular measures `μ` and `ν` such that `s = μ - ν`. In this case, the measures `μ` and `ν` are given by `s.to_jordan_decomposition.pos_part` and `s.to_jordan_decomposition.neg_part` respectively. Note that we use `measure_theory.jordan_decomposition.to_signed_measure` to represent the signed measure corresponding to `s.to_jordan_decomposition.pos_part - s.to_jordan_decomposition.neg_part`. -/ @[simp] lemma to_signed_measure_to_jordan_decomposition (s : signed_measure α) : s.to_jordan_decomposition.to_signed_measure = s := begin obtain ⟨i, hi₁, hi₂, hi₃, hμ, hν⟩ := s.to_jordan_decomposition_spec, simp only [jordan_decomposition.to_signed_measure, hμ, hν], ext k hk, rw [to_signed_measure_sub_apply hk, to_measure_of_zero_le_apply _ hi₂ hi₁ hk, to_measure_of_le_zero_apply _ hi₃ hi₁.compl hk], simp only [ennreal.coe_to_real, subtype.coe_mk, ennreal.some_eq_coe, sub_neg_eq_add], rw [← of_union _ (measurable_set.inter hi₁ hk) (measurable_set.inter hi₁.compl hk), set.inter_comm i, set.inter_comm iᶜ, set.inter_union_compl _ _], { apply_instance }, { rintro x ⟨⟨hx₁, _⟩, hx₂, _⟩, exact false.elim (hx₂ hx₁) } end section variables {u v w : set α} /-- A subset `v` of a null-set `w` has zero measure if `w` is a subset of a positive set `u`. -/ lemma subset_positive_null_set (hu : measurable_set u) (hv : measurable_set v) (hw : measurable_set w) (hsu : 0 ≤[u] s) (hw₁ : s w = 0) (hw₂ : w ⊆ u) (hwt : v ⊆ w) : s v = 0 := begin have : s v + s (w \ v) = 0, { rw [← hw₁, ← of_union set.disjoint_diff hv (hw.diff hv), set.union_diff_self, set.union_eq_self_of_subset_left hwt], apply_instance }, have h₁ := nonneg_of_zero_le_restrict _ (restrict_le_restrict_subset _ _ hu hsu (hwt.trans hw₂)), have h₂ := nonneg_of_zero_le_restrict _ (restrict_le_restrict_subset _ _ hu hsu ((w.diff_subset v).trans hw₂)), linarith, end /-- A subset `v` of a null-set `w` has zero measure if `w` is a subset of a negative set `u`. -/ lemma subset_negative_null_set (hu : measurable_set u) (hv : measurable_set v) (hw : measurable_set w) (hsu : s ≤[u] 0) (hw₁ : s w = 0) (hw₂ : w ⊆ u) (hwt : v ⊆ w) : s v = 0 := begin rw [← s.neg_le_neg_iff _ hu, neg_zero] at hsu, have := subset_positive_null_set hu hv hw hsu, simp only [pi.neg_apply, neg_eq_zero, coe_neg] at this, exact this hw₁ hw₂ hwt, end /-- If the symmetric difference of two positive sets is a null-set, then so are the differences between the two sets. -/ lemma of_diff_eq_zero_of_symm_diff_eq_zero_positive (hu : measurable_set u) (hv : measurable_set v) (hsu : 0 ≤[u] s) (hsv : 0 ≤[v] s) (hs : s (u ∆ v) = 0) : s (u \ v) = 0 ∧ s (v \ u) = 0 := begin rw restrict_le_restrict_iff at hsu hsv, have a := hsu (hu.diff hv) (u.diff_subset v), have b := hsv (hv.diff hu) (v.diff_subset u), erw [of_union (set.disjoint_of_subset_left (u.diff_subset v) set.disjoint_diff) (hu.diff hv) (hv.diff hu)] at hs, rw zero_apply at a b, split, all_goals { linarith <|> apply_instance <|> assumption }, end /-- If the symmetric difference of two negative sets is a null-set, then so are the differences between the two sets. -/ lemma of_diff_eq_zero_of_symm_diff_eq_zero_negative (hu : measurable_set u) (hv : measurable_set v) (hsu : s ≤[u] 0) (hsv : s ≤[v] 0) (hs : s (u ∆ v) = 0) : s (u \ v) = 0 ∧ s (v \ u) = 0 := begin rw [← s.neg_le_neg_iff _ hu, neg_zero] at hsu, rw [← s.neg_le_neg_iff _ hv, neg_zero] at hsv, have := of_diff_eq_zero_of_symm_diff_eq_zero_positive hu hv hsu hsv, simp only [pi.neg_apply, neg_eq_zero, coe_neg] at this, exact this hs, end lemma of_inter_eq_of_symm_diff_eq_zero_positive (hu : measurable_set u) (hv : measurable_set v) (hw : measurable_set w) (hsu : 0 ≤[u] s) (hsv : 0 ≤[v] s) (hs : s (u ∆ v) = 0) : s (w ∩ u) = s (w ∩ v) := begin have hwuv : s ((w ∩ u) ∆ (w ∩ v)) = 0, { refine subset_positive_null_set (hu.union hv) ((hw.inter hu).symm_diff (hw.inter hv)) (hu.symm_diff hv) (restrict_le_restrict_union _ _ hu hsu hv hsv) hs _ _, { exact symm_diff_le_sup u v }, { rintro x (⟨⟨hxw, hxu⟩, hx⟩ | ⟨⟨hxw, hxv⟩, hx⟩); rw [set.mem_inter_eq, not_and] at hx, { exact or.inl ⟨hxu, hx hxw⟩ }, { exact or.inr ⟨hxv, hx hxw⟩ } } }, obtain ⟨huv, hvu⟩ := of_diff_eq_zero_of_symm_diff_eq_zero_positive (hw.inter hu) (hw.inter hv) (restrict_le_restrict_subset _ _ hu hsu (w.inter_subset_right u)) (restrict_le_restrict_subset _ _ hv hsv (w.inter_subset_right v)) hwuv, rw [← of_diff_of_diff_eq_zero (hw.inter hu) (hw.inter hv) hvu, huv, zero_add] end lemma of_inter_eq_of_symm_diff_eq_zero_negative (hu : measurable_set u) (hv : measurable_set v) (hw : measurable_set w) (hsu : s ≤[u] 0) (hsv : s ≤[v] 0) (hs : s (u ∆ v) = 0) : s (w ∩ u) = s (w ∩ v) := begin rw [← s.neg_le_neg_iff _ hu, neg_zero] at hsu, rw [← s.neg_le_neg_iff _ hv, neg_zero] at hsv, have := of_inter_eq_of_symm_diff_eq_zero_positive hu hv hw hsu hsv, simp only [pi.neg_apply, neg_inj, neg_eq_zero, coe_neg] at this, exact this hs, end end end signed_measure namespace jordan_decomposition open measure vector_measure signed_measure function private lemma eq_of_pos_part_eq_pos_part {j₁ j₂ : jordan_decomposition α} (hj : j₁.pos_part = j₂.pos_part) (hj' : j₁.to_signed_measure = j₂.to_signed_measure) : j₁ = j₂ := begin ext1, { exact hj }, { rw ← to_signed_measure_eq_to_signed_measure_iff, suffices : j₁.pos_part.to_signed_measure - j₁.neg_part.to_signed_measure = j₁.pos_part.to_signed_measure - j₂.neg_part.to_signed_measure, { exact sub_right_inj.mp this }, convert hj' } end /-- The Jordan decomposition of a signed measure is unique. -/ theorem to_signed_measure_injective : injective $ @jordan_decomposition.to_signed_measure α _ := begin /- The main idea is that two Jordan decompositions of a signed measure provide two Hahn decompositions for that measure. Then, from `of_symm_diff_compl_positive_negative`, the symmetric difference of the two Hahn decompositions has measure zero, thus, allowing us to show the equality of the underlying measures of the Jordan decompositions. -/ intros j₁ j₂ hj, -- obtain the two Hahn decompositions from the Jordan decompositions obtain ⟨S, hS₁, hS₂, hS₃, hS₄, hS₅⟩ := j₁.exists_compl_positive_negative, obtain ⟨T, hT₁, hT₂, hT₃, hT₄, hT₅⟩ := j₂.exists_compl_positive_negative, rw ← hj at hT₂ hT₃, -- the symmetric differences of the two Hahn decompositions have measure zero obtain ⟨hST₁, -⟩ := of_symm_diff_compl_positive_negative hS₁.compl hT₁.compl ⟨hS₃, (compl_compl S).symm ▸ hS₂⟩ ⟨hT₃, (compl_compl T).symm ▸ hT₂⟩, -- it suffices to show the Jordan decompositions have the same positive parts refine eq_of_pos_part_eq_pos_part _ hj, ext1 i hi, -- we see that the positive parts of the two Jordan decompositions are equal to their -- associated signed measures restricted on their associated Hahn decompositions have hμ₁ : (j₁.pos_part i).to_real = j₁.to_signed_measure (i ∩ Sᶜ), { rw [to_signed_measure, to_signed_measure_sub_apply (hi.inter hS₁.compl), show j₁.neg_part (i ∩ Sᶜ) = 0, by exact nonpos_iff_eq_zero.1 (hS₅ ▸ measure_mono (set.inter_subset_right _ _)), ennreal.zero_to_real, sub_zero], conv_lhs { rw ← set.inter_union_compl i S }, rw [measure_union, show j₁.pos_part (i ∩ S) = 0, by exact nonpos_iff_eq_zero.1 (hS₄ ▸ measure_mono (set.inter_subset_right _ _)), zero_add], { refine set.disjoint_of_subset_left (set.inter_subset_right _ _) (set.disjoint_of_subset_right (set.inter_subset_right _ _) disjoint_compl_right) }, { exact hi.inter hS₁.compl } }, have hμ₂ : (j₂.pos_part i).to_real = j₂.to_signed_measure (i ∩ Tᶜ), { rw [to_signed_measure, to_signed_measure_sub_apply (hi.inter hT₁.compl), show j₂.neg_part (i ∩ Tᶜ) = 0, by exact nonpos_iff_eq_zero.1 (hT₅ ▸ measure_mono (set.inter_subset_right _ _)), ennreal.zero_to_real, sub_zero], conv_lhs { rw ← set.inter_union_compl i T }, rw [measure_union, show j₂.pos_part (i ∩ T) = 0, by exact nonpos_iff_eq_zero.1 (hT₄ ▸ measure_mono (set.inter_subset_right _ _)), zero_add], { exact set.disjoint_of_subset_left (set.inter_subset_right _ _) (set.disjoint_of_subset_right (set.inter_subset_right _ _) disjoint_compl_right) }, { exact hi.inter hT₁.compl } }, -- since the two signed measures associated with the Jordan decompositions are the same, -- and the symmetric difference of the Hahn decompositions have measure zero, the result follows rw [← ennreal.to_real_eq_to_real (measure_ne_top _ _) (measure_ne_top _ _), hμ₁, hμ₂, ← hj], exact of_inter_eq_of_symm_diff_eq_zero_positive hS₁.compl hT₁.compl hi hS₃ hT₃ hST₁, all_goals { apply_instance }, end @[simp] lemma to_jordan_decomposition_to_signed_measure (j : jordan_decomposition α) : (j.to_signed_measure).to_jordan_decomposition = j := (@to_signed_measure_injective _ _ j (j.to_signed_measure).to_jordan_decomposition (by simp)).symm end jordan_decomposition namespace signed_measure open jordan_decomposition /-- `measure_theory.signed_measure.to_jordan_decomposition` and `measure_theory.jordan_decomposition.to_signed_measure` form a `equiv`. -/ @[simps apply symm_apply] def to_jordan_decomposition_equiv (α : Type*) [measurable_space α] : signed_measure α ≃ jordan_decomposition α := { to_fun := to_jordan_decomposition, inv_fun := to_signed_measure, left_inv := to_signed_measure_to_jordan_decomposition, right_inv := to_jordan_decomposition_to_signed_measure } lemma to_jordan_decomposition_zero : (0 : signed_measure α).to_jordan_decomposition = 0 := begin apply to_signed_measure_injective, simp [to_signed_measure_zero], end lemma to_jordan_decomposition_neg (s : signed_measure α) : (-s).to_jordan_decomposition = -s.to_jordan_decomposition := begin apply to_signed_measure_injective, simp [to_signed_measure_neg], end lemma to_jordan_decomposition_smul (s : signed_measure α) (r : ℝ≥0) : (r • s).to_jordan_decomposition = r • s.to_jordan_decomposition := begin apply to_signed_measure_injective, simp [to_signed_measure_smul], end private lemma to_jordan_decomposition_smul_real_nonneg (s : signed_measure α) (r : ℝ) (hr : 0 ≤ r): (r • s).to_jordan_decomposition = r • s.to_jordan_decomposition := begin lift r to ℝ≥0 using hr, rw [jordan_decomposition.coe_smul, ← to_jordan_decomposition_smul], refl end lemma to_jordan_decomposition_smul_real (s : signed_measure α) (r : ℝ) : (r • s).to_jordan_decomposition = r • s.to_jordan_decomposition := begin by_cases hr : 0 ≤ r, { exact to_jordan_decomposition_smul_real_nonneg s r hr }, { ext1, { rw [real_smul_pos_part_neg _ _ (not_le.1 hr), show r • s = -(-r • s), by rw [neg_smul, neg_neg], to_jordan_decomposition_neg, neg_pos_part, to_jordan_decomposition_smul_real_nonneg, ← smul_neg_part, real_smul_nonneg], all_goals { exact left.nonneg_neg_iff.2 (le_of_lt (not_le.1 hr)) } }, { rw [real_smul_neg_part_neg _ _ (not_le.1 hr), show r • s = -(-r • s), by rw [neg_smul, neg_neg], to_jordan_decomposition_neg, neg_neg_part, to_jordan_decomposition_smul_real_nonneg, ← smul_pos_part, real_smul_nonneg], all_goals { exact left.nonneg_neg_iff.2 (le_of_lt (not_le.1 hr)) } } } end lemma to_jordan_decomposition_eq {s : signed_measure α} {j : jordan_decomposition α} (h : s = j.to_signed_measure) : s.to_jordan_decomposition = j := by rw [h, to_jordan_decomposition_to_signed_measure] /-- The total variation of a signed measure. -/ def total_variation (s : signed_measure α) : measure α := s.to_jordan_decomposition.pos_part + s.to_jordan_decomposition.neg_part lemma total_variation_zero : (0 : signed_measure α).total_variation = 0 := by simp [total_variation, to_jordan_decomposition_zero] lemma total_variation_neg (s : signed_measure α) : (-s).total_variation = s.total_variation := by simp [total_variation, to_jordan_decomposition_neg, add_comm] lemma null_of_total_variation_zero (s : signed_measure α) {i : set α} (hs : s.total_variation i = 0) : s i = 0 := begin rw [total_variation, measure.coe_add, pi.add_apply, add_eq_zero_iff] at hs, rw [← to_signed_measure_to_jordan_decomposition s, to_signed_measure, vector_measure.coe_sub, pi.sub_apply, measure.to_signed_measure_apply, measure.to_signed_measure_apply], by_cases hi : measurable_set i, { rw [if_pos hi, if_pos hi], simp [hs.1, hs.2] }, { simp [if_neg hi] } end lemma absolutely_continuous_ennreal_iff (s : signed_measure α) (μ : vector_measure α ℝ≥0∞) : s ≪ᵥ μ ↔ s.total_variation ≪ μ.ennreal_to_measure := begin split; intro h, { refine measure.absolutely_continuous.mk (λ S hS₁ hS₂, _), obtain ⟨i, hi₁, hi₂, hi₃, hpos, hneg⟩ := s.to_jordan_decomposition_spec, rw [total_variation, measure.add_apply, hpos, hneg, to_measure_of_zero_le_apply _ _ _ hS₁, to_measure_of_le_zero_apply _ _ _ hS₁], rw ← vector_measure.absolutely_continuous.ennreal_to_measure at h, simp [h (measure_mono_null (i.inter_subset_right S) hS₂), h (measure_mono_null (iᶜ.inter_subset_right S) hS₂)] }, { refine vector_measure.absolutely_continuous.mk (λ S hS₁ hS₂, _), rw ← vector_measure.ennreal_to_measure_apply hS₁ at hS₂, exact null_of_total_variation_zero s (h hS₂) } end lemma total_variation_absolutely_continuous_iff (s : signed_measure α) (μ : measure α) : s.total_variation ≪ μ ↔ s.to_jordan_decomposition.pos_part ≪ μ ∧ s.to_jordan_decomposition.neg_part ≪ μ := begin split; intro h, { split, all_goals { refine measure.absolutely_continuous.mk (λ S hS₁ hS₂, _), have := h hS₂, rw [total_variation, measure.add_apply, add_eq_zero_iff] at this }, exacts [this.1, this.2] }, { refine measure.absolutely_continuous.mk (λ S hS₁ hS₂, _), rw [total_variation, measure.add_apply, h.1 hS₂, h.2 hS₂, add_zero] } end -- TODO: Generalize to vector measures once total variation on vector measures is defined lemma mutually_singular_iff (s t : signed_measure α) : s ⊥ᵥ t ↔ s.total_variation ⊥ₘ t.total_variation := begin split, { rintro ⟨u, hmeas, hu₁, hu₂⟩, obtain ⟨i, hi₁, hi₂, hi₃, hipos, hineg⟩ := s.to_jordan_decomposition_spec, obtain ⟨j, hj₁, hj₂, hj₃, hjpos, hjneg⟩ := t.to_jordan_decomposition_spec, refine ⟨u, hmeas, _, _⟩, { rw [total_variation, measure.add_apply, hipos, hineg, to_measure_of_zero_le_apply _ _ _ hmeas, to_measure_of_le_zero_apply _ _ _ hmeas], simp [hu₁ _ (set.inter_subset_right _ _)] }, { rw [total_variation, measure.add_apply, hjpos, hjneg, to_measure_of_zero_le_apply _ _ _ hmeas.compl, to_measure_of_le_zero_apply _ _ _ hmeas.compl], simp [hu₂ _ (set.inter_subset_right _ _)] } }, { rintro ⟨u, hmeas, hu₁, hu₂⟩, exact ⟨u, hmeas, (λ t htu, null_of_total_variation_zero _ (measure_mono_null htu hu₁)), (λ t htv, null_of_total_variation_zero _ (measure_mono_null htv hu₂))⟩ } end lemma mutually_singular_ennreal_iff (s : signed_measure α) (μ : vector_measure α ℝ≥0∞) : s ⊥ᵥ μ ↔ s.total_variation ⊥ₘ μ.ennreal_to_measure := begin split, { rintro ⟨u, hmeas, hu₁, hu₂⟩, obtain ⟨i, hi₁, hi₂, hi₃, hpos, hneg⟩ := s.to_jordan_decomposition_spec, refine ⟨u, hmeas, _, _⟩, { rw [total_variation, measure.add_apply, hpos, hneg, to_measure_of_zero_le_apply _ _ _ hmeas, to_measure_of_le_zero_apply _ _ _ hmeas], simp [hu₁ _ (set.inter_subset_right _ _)] }, { rw vector_measure.ennreal_to_measure_apply hmeas.compl, exact hu₂ _ (set.subset.refl _) } }, { rintro ⟨u, hmeas, hu₁, hu₂⟩, refine vector_measure.mutually_singular.mk u hmeas (λ t htu _, null_of_total_variation_zero _ (measure_mono_null htu hu₁)) (λ t htv hmt, _), rw ← vector_measure.ennreal_to_measure_apply hmt, exact measure_mono_null htv hu₂ } end lemma total_variation_mutually_singular_iff (s : signed_measure α) (μ : measure α) : s.total_variation ⊥ₘ μ ↔ s.to_jordan_decomposition.pos_part ⊥ₘ μ ∧ s.to_jordan_decomposition.neg_part ⊥ₘ μ := measure.mutually_singular.add_left_iff end signed_measure end measure_theory
8ee32773e008dee643e047f2dd0d6f3eab575ec4
618003631150032a5676f229d13a079ac875ff77
/src/analysis/calculus/specific_functions.lean
f6f6a196e183bff6b585d1cb9fa988176083f5cd
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
7,687
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 analysis.calculus.extend_deriv import analysis.calculus.iterated_deriv import analysis.special_functions.exp_log /-! # Smoothness of specific functions The real function `exp_neg_inv_glue` given by `x ↦ exp (-1/x)` for `x > 0` and `0` for `x ≤ 0` is a basic building block to construct smooth partitions of unity. We prove that it is `C^∞` in `exp_neg_inv_glue.smooth`. -/ noncomputable theory open_locale classical topological_space open polynomial real filter set /-- `exp_neg_inv_glue` is the real function given by `x ↦ exp (-1/x)` for `x > 0` and `0` for `x ≤ 0`. is a basic building block to construct smooth partitions of unity. Its main property is that it vanishes for `x ≤ 0`, it is positive for `x > 0`, and the junction between the two behaviors is flat enough to retain smoothness. The fact that this function is `C^∞` is proved in `exp_neg_inv_glue.smooth`. -/ def exp_neg_inv_glue (x : ℝ) : ℝ := if x ≤ 0 then 0 else exp (-x⁻¹) namespace exp_neg_inv_glue /-- Our goal is to prove that `exp_neg_inv_glue` is `C^∞`. For this, we compute its successive derivatives for `x > 0`. The `n`-th derivative is of the form `P_aux n (x) exp(-1/x) / x^(2 n)`, where `P_aux n` is computed inductively. -/ noncomputable def P_aux : ℕ → polynomial ℝ | 0 := 1 | (n+1) := X^2 * (P_aux n).derivative + (1 - C (2 * n) * X) * (P_aux n) /-- Formula for the `n`-th derivative of `exp_neg_inv_glue`, as an auxiliary function `f_aux`. -/ def f_aux (n : ℕ) (x : ℝ) : ℝ := if x ≤ 0 then 0 else (P_aux n).eval x * exp (-x⁻¹) / x^(2 * n) /-- The `0`-th auxiliary function `f_aux 0` coincides with `exp_neg_inv_glue`, by definition. -/ lemma f_aux_zero_eq : f_aux 0 = exp_neg_inv_glue := begin ext x, by_cases h : x ≤ 0, { simp [exp_neg_inv_glue, f_aux, h] }, { simp [h, exp_neg_inv_glue, f_aux, ne_of_gt (not_le.1 h), P_aux] } end /-- For positive values, the derivative of the `n`-th auxiliary function `f_aux n` (given in this statement in unfolded form) is the `n+1`-th auxiliary function, since the polynomial `P_aux (n+1)` was chosen precisely to ensure this. -/ lemma f_aux_deriv (n : ℕ) (x : ℝ) (hx : x ≠ 0) : has_deriv_at (λx, (P_aux n).eval x * exp (-x⁻¹) / x^(2 * n)) ((P_aux (n+1)).eval x * exp (-x⁻¹) / x^(2 * (n + 1))) x := begin have A : ∀k:ℕ, 2 * (k + 1) - 1 = 2 * k + 1, by omega, convert (((P_aux n).has_deriv_at x).mul (((has_deriv_at_exp _).comp x (has_deriv_at_inv hx).neg))).div (has_deriv_at_pow (2 * n) x) (pow_ne_zero _ hx) using 1, field_simp [hx, P_aux], cases n; simp [nat.succ_eq_add_one, A]; ring_exp end /-- For positive values, the derivative of the `n`-th auxiliary function `f_aux n` is the `n+1`-th auxiliary function. -/ lemma f_aux_deriv_pos (n : ℕ) (x : ℝ) (hx : 0 < x) : has_deriv_at (f_aux n) ((P_aux (n+1)).eval x * exp (-x⁻¹) / x^(2 * (n + 1))) x := begin apply (f_aux_deriv n x (ne_of_gt hx)).congr_of_mem_nhds, have : Ioi (0 : ℝ) ∈ 𝓝 x := lt_mem_nhds hx, filter_upwards [this], assume y hy, have : ¬(y ≤ 0), by simpa using hy, simp [f_aux, this] end /-- To get differentiability at `0` of the auxiliary functions, we need to know that their limit is `0`, to be able to apply general differentiability extension theorems. This limit is checked in this lemma. -/ lemma f_aux_limit (n : ℕ) : tendsto (λx, (P_aux n).eval x * exp (-x⁻¹) / x^(2 * n)) (nhds_within 0 (Ioi 0)) (𝓝 0) := begin have A : tendsto (λx, (P_aux n).eval x) (nhds_within 0 (Ioi 0)) (𝓝 ((P_aux n).eval 0)) := (P_aux n).continuous_within_at, have B : tendsto (λx, exp (-x⁻¹) / x^(2 * n)) (nhds_within 0 (Ioi 0)) (𝓝 0), { convert (tendsto_pow_mul_exp_neg_at_top_nhds_0 (2 * n)).comp tendsto_inv_zero_at_top, ext x, field_simp }, convert A.mul B; simp [mul_div_assoc] end /-- Deduce from the limiting behavior at `0` of its derivative and general differentiability extension theorems that the auxiliary function `f_aux n` is differentiable at `0`, with derivative `0`. -/ lemma f_aux_deriv_zero (n : ℕ) : has_deriv_at (f_aux n) 0 0 := begin -- we check separately differentiability on the left and on the right have A : has_deriv_within_at (f_aux n) (0 : ℝ) (Iic 0) 0, { apply (has_deriv_at_const (0 : ℝ) (0 : ℝ)).has_deriv_within_at.congr, { assume y hy, simp at hy, simp [f_aux, hy] }, { simp [f_aux, le_refl] } }, have B : has_deriv_within_at (f_aux n) (0 : ℝ) (Ici 0) 0, { have diff : differentiable_on ℝ (f_aux n) (Ioi 0) := λx hx, (f_aux_deriv_pos n x hx).differentiable_at.differentiable_within_at, -- next line is the nontrivial bit of this proof, appealing to differentiability -- extension results. apply has_deriv_at_interval_left_endpoint_of_tendsto_deriv diff _ self_mem_nhds_within, { refine (f_aux_limit (n+1)).congr' _, apply mem_sets_of_superset self_mem_nhds_within (λx hx, _), simp [(f_aux_deriv_pos n x hx).deriv] }, { have : f_aux n 0 = 0, by simp [f_aux, le_refl], simp only [continuous_within_at, this], refine (f_aux_limit n).congr' _, apply mem_sets_of_superset self_mem_nhds_within (λx hx, _), have : ¬(x ≤ 0), by simpa using hx, simp [f_aux, this] } }, simpa using A.union B, end /-- At every point, the auxiliary function `f_aux n` has a derivative which is equal to `f_aux (n+1)`. -/ lemma f_aux_has_deriv_at (n : ℕ) (x : ℝ) : has_deriv_at (f_aux n) (f_aux (n+1) x) x := begin -- check separately the result for `x < 0`, where it is trivial, for `x > 0`, where it is done -- in `f_aux_deriv_pos`, and for `x = 0`, done in -- `f_aux_deriv_zero`. rcases lt_trichotomy x 0 with hx|hx|hx, { have : f_aux (n+1) x = 0, by simp [f_aux, le_of_lt hx], rw this, apply (has_deriv_at_const x (0 : ℝ)).congr_of_mem_nhds, have : Iio (0 : ℝ) ∈ 𝓝 x := gt_mem_nhds hx, filter_upwards [this], assume y hy, simp [f_aux, le_of_lt hy] }, { have : f_aux (n + 1) 0 = 0, by simp [f_aux, le_refl], rw [hx, this], exact f_aux_deriv_zero n }, { have : f_aux (n+1) x = (P_aux (n+1)).eval x * exp (-x⁻¹) / x^(2 * (n+1)), by simp [f_aux, not_le_of_gt hx], rw this, exact f_aux_deriv_pos n x hx }, end /-- The successive derivatives of the auxiliary function `f_aux 0` are the functions `f_aux n`, by induction. -/ lemma f_aux_iterated_deriv (n : ℕ) : iterated_deriv n (f_aux 0) = f_aux n := begin induction n with n IH, { simp }, { simp [iterated_deriv_succ, IH], ext x, exact (f_aux_has_deriv_at n x).deriv } end /-- The function `exp_neg_inv_glue` is smooth. -/ theorem smooth : times_cont_diff ℝ ⊤ (exp_neg_inv_glue) := begin rw ← f_aux_zero_eq, apply times_cont_diff_of_differentiable_iterated_deriv (λ m hm, _), rw f_aux_iterated_deriv m, exact λ x, (f_aux_has_deriv_at m x).differentiable_at end /-- The function `exp_neg_inv_glue` vanishes on `(-∞, 0]`. -/ lemma zero_of_nonpos {x : ℝ} (hx : x ≤ 0) : exp_neg_inv_glue x = 0 := by simp [exp_neg_inv_glue, hx] /-- The function `exp_neg_inv_glue` is positive on `(0, +∞)`. -/ lemma pos_of_pos {x : ℝ} (hx : 0 < x) : 0 < exp_neg_inv_glue x := by simp [exp_neg_inv_glue, not_le.2 hx, exp_pos] /-- The function exp_neg_inv_glue` is nonnegative. -/ lemma nonneg (x : ℝ) : 0 ≤ exp_neg_inv_glue x := begin cases le_or_gt x 0, { exact ge_of_eq (zero_of_nonpos h) }, { exact le_of_lt (pos_of_pos h) } end end exp_neg_inv_glue
bc3f45506318a4bb2693ed8428f590fd011c80b2
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/stage0/src/Lean/Meta/Tactic/Generalize.lean
462fe2faea66d449c9f9542bb567967e3ddb448a
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
4,438
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.KAbstract import Lean.Meta.Tactic.Util import Lean.Meta.Tactic.Intro import Lean.Meta.Tactic.FVarSubst import Lean.Meta.Tactic.Revert namespace Lean.Meta /-- The `generalize` tactic takes arguments of the form `h : e = x` -/ structure GeneralizeArg where expr : Expr xName? : Option Name := none hName? : Option Name := none deriving Inhabited /-- Telescopic `generalize` tactic. It can simultaneously generalize many terms. It uses `kabstract` to occurrences of the terms that need to be generalized. -/ private partial def generalizeCore (mvarId : MVarId) (args : Array GeneralizeArg) : MetaM (Array FVarId × MVarId) := mvarId.withContext do mvarId.checkNotAssigned `generalize let tag ← mvarId.getTag let target ← instantiateMVars (← mvarId.getType) let rec go (i : Nat) : MetaM Expr := do if _h : i < args.size then let arg := args[i] let e ← instantiateMVars arg.expr let eType ← instantiateMVars (← inferType e) let type ← go (i+1) let xName ← if let some xName := arg.xName? then pure xName else mkFreshUserName `x return Lean.mkForall xName BinderInfo.default eType (← kabstract type e) else return target let targetNew ← go 0 unless (← isTypeCorrect targetNew) do throwTacticEx `generalize mvarId m!"result is not type correct{indentExpr targetNew}" let es := args.map (·.expr) if !args.any fun arg => arg.hName?.isSome then let mvarNew ← mkFreshExprSyntheticOpaqueMVar targetNew tag mvarId.assign (mkAppN mvarNew es) mvarNew.mvarId!.introNP args.size else let (rfls, targetNew) ← forallBoundedTelescope targetNew args.size fun xs type => do let rec go' (i : Nat) : MetaM (List Expr × Expr) := do if _h : i < xs.size then let arg := args[i]! if let some hName := arg.hName? then let xType ← inferType xs[i] let e ← instantiateMVars arg.expr let eType ← instantiateMVars (← inferType e) let (hType, r) ← if (← isDefEq xType eType) then pure (← mkEq e xs[i], ← mkEqRefl e) else pure (← mkHEq e xs[i], ← mkHEqRefl e) let (rs, type) ← go' (i+1) return (r :: rs, mkForall hName BinderInfo.default hType type) else go' (i+1) else return ([], type) let (rfls, type) ← go' 0 return (rfls, ← mkForallFVars xs type) let mvarNew ← mkFreshExprSyntheticOpaqueMVar targetNew tag mvarId.assign (mkAppN (mkAppN mvarNew es) rfls.toArray) mvarNew.mvarId!.introNP (args.size + rfls.length) @[inheritDoc generalizeCore] def _root_.Lean.MVarId.generalize (mvarId : MVarId) (args : Array GeneralizeArg) : MetaM (Array FVarId × MVarId) := generalizeCore mvarId args @[inheritDoc generalizeCore, deprecated MVarId.generalize] def generalize (mvarId : MVarId) (args : Array GeneralizeArg) : MetaM (Array FVarId × MVarId) := generalizeCore mvarId args /-- Extension of `generalize` to support generalizing within specified hypotheses. The `hyps` array contains the list of hypotheses within which to look for occurrences of the generalizing expressions. -/ def _root_.Lean.MVarId.generalizeHyp (mvarId : MVarId) (args : Array GeneralizeArg) (hyps : Array FVarId := #[]) (fvarSubst : FVarSubst := {}) : MetaM (FVarSubst × Array FVarId × MVarId) := do if hyps.isEmpty then -- trivial case return (fvarSubst, ← mvarId.generalize args) let args ← args.mapM fun arg => return { arg with expr := ← instantiateMVars arg.expr } let hyps ← hyps.filterM fun h => do let type ← instantiateMVars (← h.getType) args.anyM fun arg => return (← kabstract type arg.expr).hasLooseBVars let (reverted, mvarId) ← mvarId.revert hyps true let (newVars, mvarId) ← mvarId.generalize args let (reintros, mvarId) ← mvarId.introNP reverted.size let fvarSubst := Id.run do let mut subst : FVarSubst := fvarSubst for h in reverted, reintro in reintros do subst := subst.insert h (mkFVar reintro) pure subst return (fvarSubst, newVars, mvarId) end Lean.Meta
62f1d6938443399a0bcc73384ff3242d18d5d7e0
94637389e03c919023691dcd05bd4411b1034aa5
/src/inClassNotes/higherOrderFunctions/option_mapTest.lean
8374a6fcadfedc89bf8713bfe2c982255b3f78f6
[]
no_license
kevinsullivan/complogic-s21
7c4eef2105abad899e46502270d9829d913e8afc
99039501b770248c8ceb39890be5dfe129dc1082
refs/heads/master
1,682,985,669,944
1,621,126,241,000
1,621,126,241,000
335,706,272
0
38
null
1,618,325,669,000
1,612,374,118,000
Lean
UTF-8
Lean
false
false
629
lean
import ..type_library.option namespace hidden -- concrete example def map_option_nat_nat : (nat → nat) → (option nat) → option nat | f option.none := option.none <<<<<<< HEAD | f (option.some v) := option.some (f v) ======= | f (option.some v) := option.some (_) -- by case analysis on the option argument >>>>>>> 02a3f4d24c3926279b3d61af458d69c2e979b8c8 -- API, general case universes u₁ u₂ #check option def map_option {α : Type u₁} {β : Type u₂} : (α → β) → (option α) → option β | f option.none := option.none | f (option.some v) := option.some (f v) end hidden
5da0d853ce7a0439c9eb936f7516db3b690d8223
4727251e0cd73359b15b664c3170e5d754078599
/src/data/multiset/basic.lean
7b15c442460d7a2dc8b462b47247a6b438e2cadd
[ "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
90,278
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.bool.all_any import data.list.perm /-! # Multisets These are implemented as the quotient of a list by permutations. ## Notation We define the global infix notation `::ₘ` for `multiset.cons`. -/ open list subtype nat variables {α : Type*} {β : Type*} {γ : Type*} /-- `multiset α` is the quotient of `list α` by list permutation. The result is a type of finite sets with duplicates allowed. -/ def {u} multiset (α : Type u) : Type u := quotient (list.is_setoid α) namespace multiset instance : has_coe (list α) (multiset α) := ⟨quot.mk _⟩ @[simp] theorem quot_mk_to_coe (l : list α) : @eq (multiset α) ⟦l⟧ l := rfl @[simp] theorem quot_mk_to_coe' (l : list α) : @eq (multiset α) (quot.mk (≈) l) l := rfl @[simp] theorem quot_mk_to_coe'' (l : list α) : @eq (multiset α) (quot.mk setoid.r l) l := rfl @[simp] theorem coe_eq_coe {l₁ l₂ : list α} : (l₁ : multiset α) = l₂ ↔ l₁ ~ l₂ := quotient.eq instance has_decidable_eq [decidable_eq α] : decidable_eq (multiset α) | s₁ s₂ := quotient.rec_on_subsingleton₂ s₁ s₂ $ λ l₁ l₂, decidable_of_iff' _ quotient.eq /-- defines a size for a multiset by referring to the size of the underlying list -/ protected def sizeof [has_sizeof α] (s : multiset α) : ℕ := quot.lift_on s sizeof $ λ l₁ l₂, perm.sizeof_eq_sizeof instance has_sizeof [has_sizeof α] : has_sizeof (multiset α) := ⟨multiset.sizeof⟩ /-! ### Empty multiset -/ /-- `0 : multiset α` is the empty set -/ protected def zero : multiset α := @nil α instance : has_zero (multiset α) := ⟨multiset.zero⟩ instance : has_emptyc (multiset α) := ⟨0⟩ instance inhabited_multiset : inhabited (multiset α) := ⟨0⟩ @[simp] theorem coe_nil_eq_zero : (@nil α : multiset α) = 0 := rfl @[simp] theorem empty_eq_zero : (∅ : multiset α) = 0 := rfl @[simp] theorem coe_eq_zero (l : list α) : (l : multiset α) = 0 ↔ l = [] := iff.trans coe_eq_coe perm_nil /-! ### `multiset.cons` -/ /-- `cons a s` is the multiset which contains `s` plus one more instance of `a`. -/ def cons (a : α) (s : multiset α) : multiset α := quot.lift_on s (λ l, (a :: l : multiset α)) (λ l₁ l₂ p, quot.sound (p.cons a)) infixr ` ::ₘ `:67 := multiset.cons instance : has_insert α (multiset α) := ⟨cons⟩ @[simp] theorem insert_eq_cons (a : α) (s : multiset α) : insert a s = a ::ₘ s := rfl @[simp] theorem cons_coe (a : α) (l : list α) : (a ::ₘ l : multiset α) = (a::l : list α) := rfl theorem singleton_coe (a : α) : (a ::ₘ 0 : multiset α) = ([a] : list α) := rfl @[simp] theorem cons_inj_left {a b : α} (s : multiset α) : a ::ₘ s = b ::ₘ s ↔ a = b := ⟨quot.induction_on s $ λ l e, have [a] ++ l ~ [b] ++ l, from quotient.exact e, singleton_perm_singleton.1 $ (perm_append_right_iff _).1 this, congr_arg _⟩ @[simp] theorem cons_inj_right (a : α) : ∀{s t : multiset α}, a ::ₘ s = a ::ₘ t ↔ s = t := by rintros ⟨l₁⟩ ⟨l₂⟩; simp @[recursor 5] protected theorem induction {p : multiset α → Prop} (h₁ : p 0) (h₂ : ∀ ⦃a : α⦄ {s : multiset α}, p s → p (a ::ₘ s)) : ∀s, p s := by rintros ⟨l⟩; induction l with _ _ ih; [exact h₁, exact h₂ ih] @[elab_as_eliminator] protected theorem induction_on {p : multiset α → Prop} (s : multiset α) (h₁ : p 0) (h₂ : ∀ ⦃a : α⦄ {s : multiset α}, p s → p (a ::ₘ s)) : p s := multiset.induction h₁ h₂ s theorem cons_swap (a b : α) (s : multiset α) : a ::ₘ b ::ₘ s = b ::ₘ a ::ₘ s := quot.induction_on s $ λ l, quotient.sound $ perm.swap _ _ _ section rec variables {C : multiset α → Sort*} /-- Dependent recursor on multisets. TODO: should be @[recursor 6], but then the definition of `multiset.pi` fails with a stack overflow in `whnf`. -/ protected def rec (C_0 : C 0) (C_cons : Πa m, C m → C (a ::ₘ m)) (C_cons_heq : ∀ a a' m b, C_cons a (a' ::ₘ m) (C_cons a' m b) == C_cons a' (a ::ₘ m) (C_cons a m b)) (m : multiset α) : C m := quotient.hrec_on m (@list.rec α (λl, C ⟦l⟧) C_0 (λa l b, C_cons a ⟦l⟧ b)) $ assume l l' h, h.rec_heq (assume a l l' b b' hl, have ⟦l⟧ = ⟦l'⟧, from quot.sound hl, by cc) (assume a a' l, C_cons_heq a a' ⟦l⟧) /-- Companion to `multiset.rec` with more convenient argument order. -/ @[elab_as_eliminator] protected def rec_on (m : multiset α) (C_0 : C 0) (C_cons : Πa m, C m → C (a ::ₘ m)) (C_cons_heq : ∀a a' m b, C_cons a (a' ::ₘ m) (C_cons a' m b) == C_cons a' (a ::ₘ m) (C_cons a m b)) : C m := multiset.rec C_0 C_cons C_cons_heq m variables {C_0 : C 0} {C_cons : Πa m, C m → C (a ::ₘ m)} {C_cons_heq : ∀a a' m b, C_cons a (a' ::ₘ m) (C_cons a' m b) == C_cons a' (a ::ₘ m) (C_cons a m b)} @[simp] lemma rec_on_0 : @multiset.rec_on α C (0:multiset α) C_0 C_cons C_cons_heq = C_0 := rfl @[simp] lemma rec_on_cons (a : α) (m : multiset α) : (a ::ₘ m).rec_on C_0 C_cons C_cons_heq = C_cons a m (m.rec_on C_0 C_cons C_cons_heq) := quotient.induction_on m $ assume l, rfl end rec section mem /-- `a ∈ s` means that `a` has nonzero multiplicity in `s`. -/ def mem (a : α) (s : multiset α) : Prop := quot.lift_on s (λ l, a ∈ l) (λ l₁ l₂ (e : l₁ ~ l₂), propext $ e.mem_iff) instance : has_mem α (multiset α) := ⟨mem⟩ @[simp] lemma mem_coe {a : α} {l : list α} : a ∈ (l : multiset α) ↔ a ∈ l := iff.rfl instance decidable_mem [decidable_eq α] (a : α) (s : multiset α) : decidable (a ∈ s) := quot.rec_on_subsingleton s $ list.decidable_mem a @[simp] theorem mem_cons {a b : α} {s : multiset α} : a ∈ b ::ₘ s ↔ a = b ∨ a ∈ s := quot.induction_on s $ λ l, iff.rfl lemma mem_cons_of_mem {a b : α} {s : multiset α} (h : a ∈ s) : a ∈ b ::ₘ s := mem_cons.2 $ or.inr h @[simp] theorem mem_cons_self (a : α) (s : multiset α) : a ∈ a ::ₘ s := mem_cons.2 (or.inl rfl) theorem forall_mem_cons {p : α → Prop} {a : α} {s : multiset α} : (∀ x ∈ (a ::ₘ s), p x) ↔ p a ∧ ∀ x ∈ s, p x := quotient.induction_on' s $ λ L, list.forall_mem_cons theorem exists_cons_of_mem {s : multiset α} {a : α} : a ∈ s → ∃ t, s = a ::ₘ t := quot.induction_on s $ λ l (h : a ∈ l), let ⟨l₁, l₂, e⟩ := mem_split h in e.symm ▸ ⟨(l₁++l₂ : list α), quot.sound perm_middle⟩ @[simp] theorem not_mem_zero (a : α) : a ∉ (0 : multiset α) := id theorem eq_zero_of_forall_not_mem {s : multiset α} : (∀x, x ∉ s) → s = 0 := quot.induction_on s $ λ l H, by rw eq_nil_iff_forall_not_mem.mpr H; refl theorem eq_zero_iff_forall_not_mem {s : multiset α} : s = 0 ↔ ∀ a, a ∉ s := ⟨λ h, h.symm ▸ λ _, not_false, eq_zero_of_forall_not_mem⟩ theorem exists_mem_of_ne_zero {s : multiset α} : s ≠ 0 → ∃ a : α, a ∈ s := quot.induction_on s $ assume l hl, match l, hl with | [] := assume h, false.elim $ h rfl | (a :: l) := assume _, ⟨a, by simp⟩ end lemma empty_or_exists_mem (s : multiset α) : s = 0 ∨ ∃ a, a ∈ s := or_iff_not_imp_left.mpr multiset.exists_mem_of_ne_zero @[simp] lemma zero_ne_cons {a : α} {m : multiset α} : 0 ≠ a ::ₘ m := assume h, have a ∈ (0:multiset α), from h.symm ▸ mem_cons_self _ _, not_mem_zero _ this @[simp] lemma cons_ne_zero {a : α} {m : multiset α} : a ::ₘ m ≠ 0 := zero_ne_cons.symm lemma cons_eq_cons {a b : α} {as bs : multiset α} : a ::ₘ as = b ::ₘ bs ↔ ((a = b ∧ as = bs) ∨ (a ≠ b ∧ ∃cs, as = b ::ₘ cs ∧ bs = a ::ₘ cs)) := begin haveI : decidable_eq α := classical.dec_eq α, split, { assume eq, by_cases a = b, { subst h, simp * at * }, { have : a ∈ b ::ₘ bs, from eq ▸ mem_cons_self _ _, have : a ∈ bs, by simpa [h], rcases exists_cons_of_mem this with ⟨cs, hcs⟩, simp [h, hcs], have : a ::ₘ as = b ::ₘ a ::ₘ cs, by simp [eq, hcs], have : a ::ₘ as = a ::ₘ b ::ₘ cs, by rwa [cons_swap], simpa using this } }, { assume h, rcases h with ⟨eq₁, eq₂⟩ | ⟨h, cs, eq₁, eq₂⟩, { simp * }, { simp [*, cons_swap a b] } } end end mem /-! ### `multiset.subset` -/ section subset /-- `s ⊆ t` is the lift of the list subset relation. It means that any element with nonzero multiplicity in `s` has nonzero multiplicity in `t`, but it does not imply that the multiplicity of `a` in `s` is less or equal than in `t`; see `s ≤ t` for this relation. -/ protected def subset (s t : multiset α) : Prop := ∀ ⦃a : α⦄, a ∈ s → a ∈ t instance : has_subset (multiset α) := ⟨multiset.subset⟩ instance : has_ssubset (multiset α) := ⟨λ s t, s ⊆ t ∧ ¬ t ⊆ s⟩ @[simp] theorem coe_subset {l₁ l₂ : list α} : (l₁ : multiset α) ⊆ l₂ ↔ l₁ ⊆ l₂ := iff.rfl @[simp] theorem subset.refl (s : multiset α) : s ⊆ s := λ a h, h theorem subset.trans {s t u : multiset α} : s ⊆ t → t ⊆ u → s ⊆ u := λ h₁ h₂ a m, h₂ (h₁ m) theorem subset_iff {s t : multiset α} : s ⊆ t ↔ (∀⦃x⦄, x ∈ s → x ∈ t) := iff.rfl theorem mem_of_subset {s t : multiset α} {a : α} (h : s ⊆ t) : a ∈ s → a ∈ t := @h _ @[simp] theorem zero_subset (s : multiset α) : 0 ⊆ s := λ a, (not_mem_nil a).elim lemma subset_cons (s : multiset α) (a : α) : s ⊆ a ::ₘ s := λ _, mem_cons_of_mem lemma ssubset_cons {s : multiset α} {a : α} (ha : a ∉ s) : s ⊂ a ::ₘ s := ⟨subset_cons _ _, λ h, ha $ h $ mem_cons_self _ _⟩ @[simp] theorem cons_subset {a : α} {s t : multiset α} : (a ::ₘ s) ⊆ t ↔ a ∈ t ∧ s ⊆ t := by simp [subset_iff, or_imp_distrib, forall_and_distrib] lemma cons_subset_cons {a : α} {s t : multiset α} : s ⊆ t → a ::ₘ s ⊆ a ::ₘ t := quotient.induction_on₂ s t $ λ _ _, cons_subset_cons _ theorem eq_zero_of_subset_zero {s : multiset α} (h : s ⊆ 0) : s = 0 := eq_zero_of_forall_not_mem h theorem subset_zero {s : multiset α} : s ⊆ 0 ↔ s = 0 := ⟨eq_zero_of_subset_zero, λ xeq, xeq.symm ▸ subset.refl 0⟩ lemma induction_on' {p : multiset α → Prop} (S : multiset α) (h₁ : p ∅) (h₂ : ∀ {a s}, a ∈ S → s ⊆ S → p s → p (insert a s)) : p S := @multiset.induction_on α (λ T, T ⊆ S → p T) S (λ _, h₁) (λ a s hps hs, let ⟨hS, sS⟩ := cons_subset.1 hs in h₂ hS sS (hps sS)) (subset.refl S) end subset section to_list /-- Produces a list of the elements in the multiset using choice. -/ @[reducible] noncomputable def to_list {α : Type*} (s : multiset α) := classical.some (quotient.exists_rep s) @[simp] lemma to_list_zero {α : Type*} : (multiset.to_list 0 : list α) = [] := (multiset.coe_eq_zero _).1 (classical.some_spec (quotient.exists_rep multiset.zero)) @[simp, norm_cast] lemma coe_to_list {α : Type*} (s : multiset α) : (s.to_list : multiset α) = s := classical.some_spec (quotient.exists_rep _) @[simp] lemma mem_to_list {α : Type*} (a : α) (s : multiset α) : a ∈ s.to_list ↔ a ∈ s := by rw [←multiset.mem_coe, multiset.coe_to_list] end to_list /-! ### Partial order on `multiset`s -/ /-- `s ≤ t` means that `s` is a sublist of `t` (up to permutation). Equivalently, `s ≤ t` means that `count a s ≤ count a t` for all `a`. -/ protected def le (s t : multiset α) : Prop := quotient.lift_on₂ s t (<+~) $ λ v₁ v₂ w₁ w₂ p₁ p₂, propext (p₂.subperm_left.trans p₁.subperm_right) instance : partial_order (multiset α) := { le := multiset.le, le_refl := by rintros ⟨l⟩; exact subperm.refl _, le_trans := by rintros ⟨l₁⟩ ⟨l₂⟩ ⟨l₃⟩; exact @subperm.trans _ _ _ _, le_antisymm := by rintros ⟨l₁⟩ ⟨l₂⟩ h₁ h₂; exact quot.sound (subperm.antisymm h₁ h₂) } section variables {s t : multiset α} {a : α} lemma subset_of_le : s ≤ t → s ⊆ t := quotient.induction_on₂ s t $ λ l₁ l₂, subperm.subset alias subset_of_le ← multiset.le.subset lemma mem_of_le (h : s ≤ t) : a ∈ s → a ∈ t := mem_of_subset (subset_of_le h) lemma not_mem_mono (h : s ⊆ t) : a ∉ t → a ∉ s := mt $ @h _ @[simp] theorem coe_le {l₁ l₂ : list α} : (l₁ : multiset α) ≤ l₂ ↔ l₁ <+~ l₂ := iff.rfl @[elab_as_eliminator] theorem le_induction_on {C : multiset α → multiset α → Prop} {s t : multiset α} (h : s ≤ t) (H : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → C l₁ l₂) : C s t := quotient.induction_on₂ s t (λ l₁ l₂ ⟨l, p, s⟩, (show ⟦l⟧ = ⟦l₁⟧, from quot.sound p) ▸ H s) h theorem zero_le (s : multiset α) : 0 ≤ s := quot.induction_on s $ λ l, (nil_sublist l).subperm lemma le_zero : s ≤ 0 ↔ s = 0 := ⟨λ h, le_antisymm h (zero_le _), le_of_eq⟩ theorem lt_cons_self (s : multiset α) (a : α) : s < a ::ₘ s := quot.induction_on s $ λ l, suffices l <+~ a :: l ∧ (¬l ~ a :: l), by simpa [lt_iff_le_and_ne], ⟨(sublist_cons _ _).subperm, λ p, ne_of_lt (lt_succ_self (length l)) p.length_eq⟩ theorem le_cons_self (s : multiset α) (a : α) : s ≤ a ::ₘ s := le_of_lt $ lt_cons_self _ _ lemma cons_le_cons_iff (a : α) : a ::ₘ s ≤ a ::ₘ t ↔ s ≤ t := quotient.induction_on₂ s t $ λ l₁ l₂, subperm_cons a lemma cons_le_cons (a : α) : s ≤ t → a ::ₘ s ≤ a ::ₘ t := (cons_le_cons_iff a).2 lemma le_cons_of_not_mem (m : a ∉ s) : s ≤ a ::ₘ t ↔ s ≤ t := begin refine ⟨_, λ h, le_trans h $ le_cons_self _ _⟩, suffices : ∀ {t'} (_ : s ≤ t') (_ : a ∈ t'), a ::ₘ s ≤ t', { exact λ h, (cons_le_cons_iff a).1 (this h (mem_cons_self _ _)) }, introv h, revert m, refine le_induction_on h _, introv s m₁ m₂, rcases mem_split m₂ with ⟨r₁, r₂, rfl⟩, exact perm_middle.subperm_left.2 ((subperm_cons _).2 $ ((sublist_or_mem_of_sublist s).resolve_right m₁).subperm) end end /-! ### Singleton -/ instance : has_singleton α (multiset α) := ⟨λ a, a ::ₘ 0⟩ instance : is_lawful_singleton α (multiset α) := ⟨λ a, rfl⟩ theorem singleton_eq_cons (a : α) : singleton a = a ::ₘ 0 := rfl @[simp] theorem mem_singleton {a b : α} : b ∈ ({a} : multiset α) ↔ b = a := by simp only [singleton_eq_cons, mem_cons, iff_self, or_false, not_mem_zero] theorem mem_singleton_self (a : α) : a ∈ ({a} : multiset α) := by { rw singleton_eq_cons, exact mem_cons_self _ _ } theorem singleton_inj {a b : α} : ({a} : multiset α) = {b} ↔ a = b := by { simp_rw [singleton_eq_cons], exact cons_inj_left _ } @[simp] theorem singleton_ne_zero (a : α) : ({a} : multiset α) ≠ 0 := ne_of_gt (lt_cons_self _ _) @[simp] theorem singleton_le {a : α} {s : multiset α} : {a} ≤ s ↔ a ∈ s := ⟨λ h, mem_of_le h (mem_singleton_self _), λ h, let ⟨t, e⟩ := exists_cons_of_mem h in e.symm ▸ cons_le_cons _ (zero_le _)⟩ /-! ### Additive monoid -/ /-- The sum of two multisets is the lift of the list append operation. This adds the multiplicities of each element, i.e. `count a (s + t) = count a s + count a t`. -/ protected def add (s₁ s₂ : multiset α) : multiset α := quotient.lift_on₂ s₁ s₂ (λ l₁ l₂, ((l₁ ++ l₂ : list α) : multiset α)) $ λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound $ p₁.append p₂ instance : has_add (multiset α) := ⟨multiset.add⟩ @[simp] theorem coe_add (s t : list α) : (s + t : multiset α) = (s ++ t : list α) := rfl protected theorem add_comm (s t : multiset α) : s + t = t + s := quotient.induction_on₂ s t $ λ l₁ l₂, quot.sound perm_append_comm protected theorem zero_add (s : multiset α) : 0 + s = s := quot.induction_on s $ λ l, rfl theorem singleton_add (a : α) (s : multiset α) : {a} + s = a ::ₘ s := rfl protected theorem add_le_add_left (s) {t u : multiset α} : s + t ≤ s + u ↔ t ≤ u := quotient.induction_on₃ s t u $ λ l₁ l₂ l₃, subperm_append_left _ protected theorem add_left_cancel (s) {t u : multiset α} (h : s + t = s + u) : t = u := le_antisymm ((multiset.add_le_add_left _).1 (le_of_eq h)) ((multiset.add_le_add_left _).1 (le_of_eq h.symm)) instance : ordered_cancel_add_comm_monoid (multiset α) := { zero := 0, add := (+), add_comm := multiset.add_comm, add_assoc := λ s₁ s₂ s₃, quotient.induction_on₃ s₁ s₂ s₃ $ λ l₁ l₂ l₃, congr_arg coe $ append_assoc l₁ l₂ l₃, zero_add := multiset.zero_add, add_zero := λ s, by rw [multiset.add_comm, multiset.zero_add], add_left_cancel := multiset.add_left_cancel, add_le_add_left := λ s₁ s₂ h s₃, (multiset.add_le_add_left _).2 h, le_of_add_le_add_left := λ s₁ s₂ s₃, (multiset.add_le_add_left _).1, ..@multiset.partial_order α } theorem le_add_right (s t : multiset α) : s ≤ s + t := by simpa using add_le_add_left (zero_le t) s theorem le_add_left (s t : multiset α) : s ≤ t + s := by simpa using add_le_add_right (zero_le t) s theorem le_iff_exists_add {s t : multiset α} : s ≤ t ↔ ∃ u, t = s + u := ⟨λ h, le_induction_on h $ λ l₁ l₂ s, let ⟨l, p⟩ := s.exists_perm_append in ⟨l, quot.sound p⟩, λ ⟨u, e⟩, e.symm ▸ le_add_right _ _⟩ instance : order_bot (multiset α) := { bot := 0, bot_le := multiset.zero_le } instance : canonically_ordered_add_monoid (multiset α) := { le_iff_exists_add := @le_iff_exists_add _, ..multiset.order_bot, ..multiset.ordered_cancel_add_comm_monoid } @[simp] theorem cons_add (a : α) (s t : multiset α) : a ::ₘ s + t = a ::ₘ (s + t) := by rw [← singleton_add, ← singleton_add, add_assoc] @[simp] theorem add_cons (a : α) (s t : multiset α) : s + a ::ₘ t = a ::ₘ (s + t) := by rw [add_comm, cons_add, add_comm] @[simp] theorem mem_add {a : α} {s t : multiset α} : a ∈ s + t ↔ a ∈ s ∨ a ∈ t := quotient.induction_on₂ s t $ λ l₁ l₂, mem_append lemma mem_of_mem_nsmul {a : α} {s : multiset α} {n : ℕ} (h : a ∈ n • s) : a ∈ s := begin induction n with n ih, { rw zero_nsmul at h, exact absurd h (not_mem_zero _) }, { rw [succ_nsmul, mem_add] at h, exact h.elim id ih }, end @[simp] lemma mem_nsmul {a : α} {s : multiset α} {n : ℕ} (h0 : n ≠ 0) : a ∈ n • s ↔ a ∈ s := begin refine ⟨mem_of_mem_nsmul, λ h, _⟩, obtain ⟨n, rfl⟩ := exists_eq_succ_of_ne_zero h0, rw [succ_nsmul, mem_add], exact or.inl h end lemma nsmul_cons {s : multiset α} (n : ℕ) (a : α) : n • (a ::ₘ s) = n • {a} + n • s := by rw [←singleton_add, nsmul_add] /-! ### Cardinality -/ /-- The cardinality of a multiset is the sum of the multiplicities of all its elements, or simply the length of the underlying list. -/ def card : multiset α →+ ℕ := { to_fun := λ s, quot.lift_on s length $ λ l₁ l₂, perm.length_eq, map_zero' := rfl, map_add' := λ s t, quotient.induction_on₂ s t length_append } @[simp] theorem coe_card (l : list α) : card (l : multiset α) = length l := rfl @[simp] theorem card_zero : @card α 0 = 0 := rfl theorem card_add (s t : multiset α) : card (s + t) = card s + card t := card.map_add s t lemma card_nsmul (s : multiset α) (n : ℕ) : (n • s).card = n * s.card := by rw [card.map_nsmul s n, nat.nsmul_eq_mul] @[simp] theorem card_cons (a : α) (s : multiset α) : card (a ::ₘ s) = card s + 1 := quot.induction_on s $ λ l, rfl @[simp] theorem card_singleton (a : α) : card ({a} : multiset α) = 1 := by simp only [singleton_eq_cons, card_zero, eq_self_iff_true, zero_add, card_cons] lemma card_pair (a b : α) : ({a, b} : multiset α).card = 2 := by rw [insert_eq_cons, card_cons, card_singleton] theorem card_eq_one {s : multiset α} : card s = 1 ↔ ∃ a, s = {a} := ⟨quot.induction_on s $ λ l h, (list.length_eq_one.1 h).imp $ λ a, congr_arg coe, λ ⟨a, e⟩, e.symm ▸ rfl⟩ theorem card_le_of_le {s t : multiset α} (h : s ≤ t) : card s ≤ card t := le_induction_on h $ λ l₁ l₂, length_le_of_sublist @[mono] theorem card_mono : monotone (@card α) := λ a b, card_le_of_le theorem eq_of_le_of_card_le {s t : multiset α} (h : s ≤ t) : card t ≤ card s → s = t := le_induction_on h $ λ l₁ l₂ s h₂, congr_arg coe $ eq_of_sublist_of_length_le s h₂ theorem card_lt_of_lt {s t : multiset α} (h : s < t) : card s < card t := lt_of_not_ge $ λ h₂, ne_of_lt h $ eq_of_le_of_card_le (le_of_lt h) h₂ theorem lt_iff_cons_le {s t : multiset α} : s < t ↔ ∃ a, a ::ₘ s ≤ t := ⟨quotient.induction_on₂ s t $ λ l₁ l₂ h, subperm.exists_of_length_lt (le_of_lt h) (card_lt_of_lt h), λ ⟨a, h⟩, lt_of_lt_of_le (lt_cons_self _ _) h⟩ @[simp] theorem card_eq_zero {s : multiset α} : card s = 0 ↔ s = 0 := ⟨λ h, (eq_of_le_of_card_le (zero_le _) (le_of_eq h)).symm, λ e, by simp [e]⟩ theorem card_pos {s : multiset α} : 0 < card s ↔ s ≠ 0 := pos_iff_ne_zero.trans $ not_congr card_eq_zero theorem card_pos_iff_exists_mem {s : multiset α} : 0 < card s ↔ ∃ a, a ∈ s := quot.induction_on s $ λ l, length_pos_iff_exists_mem lemma card_eq_two {s : multiset α} : s.card = 2 ↔ ∃ x y, s = {x, y} := ⟨quot.induction_on s (λ l h, (list.length_eq_two.mp h).imp (λ a, Exists.imp (λ b, congr_arg coe))), λ ⟨a, b, e⟩, e.symm ▸ rfl⟩ lemma card_eq_three {s : multiset α} : s.card = 3 ↔ ∃ x y z, s = {x, y, z} := ⟨quot.induction_on s (λ l h, (list.length_eq_three.mp h).imp (λ a, Exists.imp (λ b, Exists.imp (λ c, congr_arg coe)))), λ ⟨a, b, c, e⟩, e.symm ▸ rfl⟩ /-! ### Induction principles -/ /-- A strong induction principle for multisets: If you construct a value for a particular multiset given values for all strictly smaller multisets, you can construct a value for any multiset. -/ @[elab_as_eliminator] def strong_induction_on {p : multiset α → Sort*} : ∀ (s : multiset α), (∀ s, (∀t < s, p t) → p s) → p s | s := λ ih, ih s $ λ t h, have card t < card s, from card_lt_of_lt h, strong_induction_on t ih using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf card⟩]} theorem strong_induction_eq {p : multiset α → Sort*} (s : multiset α) (H) : @strong_induction_on _ p s H = H s (λ t h, @strong_induction_on _ p t H) := by rw [strong_induction_on] @[elab_as_eliminator] lemma case_strong_induction_on {p : multiset α → Prop} (s : multiset α) (h₀ : p 0) (h₁ : ∀ a s, (∀t ≤ s, p t) → p (a ::ₘ s)) : p s := multiset.strong_induction_on s $ assume s, multiset.induction_on s (λ _, h₀) $ λ a s _ ih, h₁ _ _ $ λ t h, ih _ $ lt_of_le_of_lt h $ lt_cons_self _ _ /-- Suppose that, given that `p t` can be defined on all supersets of `s` of cardinality less than `n`, one knows how to define `p s`. Then one can inductively define `p s` for all multisets `s` of cardinality less than `n`, starting from multisets of card `n` and iterating. This can be used either to define data, or to prove properties. -/ def strong_downward_induction {p : multiset α → Sort*} {n : ℕ} (H : ∀ t₁, (∀ {t₂ : multiset α}, t₂.card ≤ n → t₁ < t₂ → p t₂) → t₁.card ≤ n → p t₁) : ∀ (s : multiset α), s.card ≤ n → p s | s := H s (λ t ht h, have n - card t < n - card s, from (tsub_lt_tsub_iff_left_of_le ht).2 (card_lt_of_lt h), strong_downward_induction t ht) using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ (t : multiset α), n - t.card)⟩]} lemma strong_downward_induction_eq {p : multiset α → Sort*} {n : ℕ} (H : ∀ t₁, (∀ {t₂ : multiset α}, t₂.card ≤ n → t₁ < t₂ → p t₂) → t₁.card ≤ n → p t₁) (s : multiset α) : strong_downward_induction H s = H s (λ t ht hst, strong_downward_induction H t ht) := by rw strong_downward_induction /-- Analogue of `strong_downward_induction` with order of arguments swapped. -/ @[elab_as_eliminator] def strong_downward_induction_on {p : multiset α → Sort*} {n : ℕ} : ∀ (s : multiset α), (∀ t₁, (∀ {t₂ : multiset α}, t₂.card ≤ n → t₁ < t₂ → p t₂) → t₁.card ≤ n → p t₁) → s.card ≤ n → p s := λ s H, strong_downward_induction H s lemma strong_downward_induction_on_eq {p : multiset α → Sort*} (s : multiset α) {n : ℕ} (H : ∀ t₁, (∀ {t₂ : multiset α}, t₂.card ≤ n → t₁ < t₂ → p t₂) → t₁.card ≤ n → p t₁) : s.strong_downward_induction_on H = H s (λ t ht h, t.strong_downward_induction_on H ht) := by { dunfold strong_downward_induction_on, rw strong_downward_induction } /-- Another way of expressing `strong_induction_on`: the `(<)` relation is well-founded. -/ lemma well_founded_lt : well_founded ((<) : multiset α → multiset α → Prop) := subrelation.wf (λ _ _, multiset.card_lt_of_lt) (measure_wf multiset.card) /-! ### `multiset.repeat` -/ /-- `repeat a n` is the multiset containing only `a` with multiplicity `n`. -/ def repeat (a : α) (n : ℕ) : multiset α := repeat a n @[simp] lemma repeat_zero (a : α) : repeat a 0 = 0 := rfl @[simp] lemma repeat_succ (a : α) (n) : repeat a (n+1) = a ::ₘ repeat a n := by simp [repeat] @[simp] lemma repeat_one (a : α) : repeat a 1 = {a} := by simp only [repeat_succ, singleton_eq_cons, eq_self_iff_true, repeat_zero, cons_inj_right] @[simp] lemma card_repeat : ∀ (a : α) n, card (repeat a n) = n := length_repeat lemma mem_repeat {a b : α} {n : ℕ} : b ∈ repeat a n ↔ n ≠ 0 ∧ b = a := mem_repeat theorem eq_of_mem_repeat {a b : α} {n} : b ∈ repeat a n → b = a := eq_of_mem_repeat theorem eq_repeat' {a : α} {s : multiset α} : s = repeat a s.card ↔ ∀ b ∈ s, b = a := quot.induction_on s $ λ l, iff.trans ⟨λ h, (perm_repeat.1 $ (quotient.exact h)), congr_arg coe⟩ eq_repeat' theorem eq_repeat_of_mem {a : α} {s : multiset α} : (∀ b ∈ s, b = a) → s = repeat a s.card := eq_repeat'.2 theorem eq_repeat {a : α} {n} {s : multiset α} : s = repeat a n ↔ card s = n ∧ ∀ b ∈ s, b = a := ⟨λ h, h.symm ▸ ⟨card_repeat _ _, λ b, eq_of_mem_repeat⟩, λ ⟨e, al⟩, e ▸ eq_repeat_of_mem al⟩ lemma repeat_left_injective {n : ℕ} (hn : n ≠ 0) : function.injective (λ a : α, repeat a n) := λ a b h, (eq_repeat.1 h).2 _ $ mem_repeat.2 ⟨hn, rfl⟩ @[simp] lemma repeat_left_inj {a b : α} {n : ℕ} (h : n ≠ 0) : repeat a n = repeat b n ↔ a = b := (repeat_left_injective h).eq_iff theorem repeat_injective (a : α) : function.injective (repeat a) := λ m n h, by rw [← (eq_repeat.1 h).1, card_repeat] theorem repeat_subset_singleton : ∀ (a : α) n, repeat a n ⊆ {a} := repeat_subset_singleton theorem repeat_le_coe {a : α} {n} {l : list α} : repeat a n ≤ l ↔ list.repeat a n <+ l := ⟨λ ⟨l', p, s⟩, (perm_repeat.1 p) ▸ s, sublist.subperm⟩ theorem nsmul_singleton (a : α) (n) : n • ({a} : multiset α) = repeat a n := begin refine eq_repeat.mpr ⟨_, λ b hb, mem_singleton.mp (mem_of_mem_nsmul hb)⟩, rw [card_nsmul, card_singleton, mul_one] end lemma nsmul_repeat {a : α} (n m : ℕ) : n • (repeat a m) = repeat a (n * m) := begin rw eq_repeat, split, { rw [card_nsmul, card_repeat] }, { exact λ b hb, eq_of_mem_repeat (mem_of_mem_nsmul hb) }, end /-! ### Erasing one copy of an element -/ section erase variables [decidable_eq α] {s t : multiset α} {a b : α} /-- `erase s a` is the multiset that subtracts 1 from the multiplicity of `a`. -/ def erase (s : multiset α) (a : α) : multiset α := quot.lift_on s (λ l, (l.erase a : multiset α)) (λ l₁ l₂ p, quot.sound (p.erase a)) @[simp] theorem coe_erase (l : list α) (a : α) : erase (l : multiset α) a = l.erase a := rfl @[simp] theorem erase_zero (a : α) : (0 : multiset α).erase a = 0 := rfl @[simp] theorem erase_cons_head (a : α) (s : multiset α) : (a ::ₘ s).erase a = s := quot.induction_on s $ λ l, congr_arg coe $ erase_cons_head a l @[simp, priority 990] theorem erase_cons_tail {a b : α} (s : multiset α) (h : b ≠ a) : (b ::ₘ s).erase a = b ::ₘ s.erase a := quot.induction_on s $ λ l, congr_arg coe $ erase_cons_tail l h @[simp, priority 980] theorem erase_of_not_mem {a : α} {s : multiset α} : a ∉ s → s.erase a = s := quot.induction_on s $ λ l h, congr_arg coe $ erase_of_not_mem h @[simp, priority 980] theorem cons_erase {s : multiset α} {a : α} : a ∈ s → a ::ₘ s.erase a = s := quot.induction_on s $ λ l h, quot.sound (perm_cons_erase h).symm theorem le_cons_erase (s : multiset α) (a : α) : s ≤ a ::ₘ s.erase a := if h : a ∈ s then le_of_eq (cons_erase h).symm else by rw erase_of_not_mem h; apply le_cons_self theorem erase_add_left_pos {a : α} {s : multiset α} (t) : a ∈ s → (s + t).erase a = s.erase a + t := quotient.induction_on₂ s t $ λ l₁ l₂ h, congr_arg coe $ erase_append_left l₂ h theorem erase_add_right_pos {a : α} (s) {t : multiset α} (h : a ∈ t) : (s + t).erase a = s + t.erase a := by rw [add_comm, erase_add_left_pos s h, add_comm] theorem erase_add_right_neg {a : α} {s : multiset α} (t) : a ∉ s → (s + t).erase a = s + t.erase a := quotient.induction_on₂ s t $ λ l₁ l₂ h, congr_arg coe $ erase_append_right l₂ h theorem erase_add_left_neg {a : α} (s) {t : multiset α} (h : a ∉ t) : (s + t).erase a = s.erase a + t := by rw [add_comm, erase_add_right_neg s h, add_comm] theorem erase_le (a : α) (s : multiset α) : s.erase a ≤ s := quot.induction_on s $ λ l, (erase_sublist a l).subperm @[simp] theorem erase_lt {a : α} {s : multiset α} : s.erase a < s ↔ a ∈ s := ⟨λ h, not_imp_comm.1 erase_of_not_mem (ne_of_lt h), λ h, by simpa [h] using lt_cons_self (s.erase a) a⟩ theorem erase_subset (a : α) (s : multiset α) : s.erase a ⊆ s := subset_of_le (erase_le a s) theorem mem_erase_of_ne {a b : α} {s : multiset α} (ab : a ≠ b) : a ∈ s.erase b ↔ a ∈ s := quot.induction_on s $ λ l, list.mem_erase_of_ne ab theorem mem_of_mem_erase {a b : α} {s : multiset α} : a ∈ s.erase b → a ∈ s := mem_of_subset (erase_subset _ _) theorem erase_comm (s : multiset α) (a b : α) : (s.erase a).erase b = (s.erase b).erase a := quot.induction_on s $ λ l, congr_arg coe $ l.erase_comm a b theorem erase_le_erase {s t : multiset α} (a : α) (h : s ≤ t) : s.erase a ≤ t.erase a := le_induction_on h $ λ l₁ l₂ h, (h.erase _).subperm theorem erase_le_iff_le_cons {s t : multiset α} {a : α} : s.erase a ≤ t ↔ s ≤ a ::ₘ t := ⟨λ h, le_trans (le_cons_erase _ _) (cons_le_cons _ h), λ h, if m : a ∈ s then by rw ← cons_erase m at h; exact (cons_le_cons_iff _).1 h else le_trans (erase_le _ _) ((le_cons_of_not_mem m).1 h)⟩ @[simp] theorem card_erase_of_mem {a : α} {s : multiset α} : a ∈ s → card (s.erase a) = pred (card s) := quot.induction_on s $ λ l, length_erase_of_mem @[simp] lemma card_erase_add_one {a : α} {s : multiset α} : a ∈ s → (s.erase a).card + 1 = s.card := quot.induction_on s $ λ l, length_erase_add_one theorem card_erase_lt_of_mem {a : α} {s : multiset α} : a ∈ s → card (s.erase a) < card s := λ h, card_lt_of_lt (erase_lt.mpr h) theorem card_erase_le {a : α} {s : multiset α} : card (s.erase a) ≤ card s := card_le_of_le (erase_le a s) theorem card_erase_eq_ite {a : α} {s : multiset α} : card (s.erase a) = if a ∈ s then pred (card s) else card s := begin by_cases h : a ∈ s, { rwa [card_erase_of_mem h, if_pos] }, { rwa [erase_of_not_mem h, if_neg] } end end erase @[simp] theorem coe_reverse (l : list α) : (reverse l : multiset α) = l := quot.sound $ reverse_perm _ /-! ### `multiset.map` -/ /-- `map f s` is the lift of the list `map` operation. The multiplicity of `b` in `map f s` is the number of `a ∈ s` (counting multiplicity) such that `f a = b`. -/ def map (f : α → β) (s : multiset α) : multiset β := quot.lift_on s (λ l : list α, (l.map f : multiset β)) (λ l₁ l₂ p, quot.sound (p.map f)) @[congr] theorem map_congr {f g : α → β} {s t : multiset α} : s = t → (∀ x ∈ t, f x = g x) → map f s = map g t := begin rintros rfl h, induction s using quot.induction_on, exact congr_arg coe (map_congr h) end lemma map_hcongr {β' : Type*} {m : multiset α} {f : α → β} {f' : α → β'} (h : β = β') (hf : ∀a∈m, f a == f' a) : map f m == map f' m := begin subst h, simp at hf, simp [map_congr rfl hf] end theorem forall_mem_map_iff {f : α → β} {p : β → Prop} {s : multiset α} : (∀ y ∈ s.map f, p y) ↔ (∀ x ∈ s, p (f x)) := quotient.induction_on' s $ λ L, list.forall_mem_map_iff @[simp] theorem coe_map (f : α → β) (l : list α) : map f ↑l = l.map f := rfl @[simp] theorem map_zero (f : α → β) : map f 0 = 0 := rfl @[simp] theorem map_cons (f : α → β) (a s) : map f (a ::ₘ s) = f a ::ₘ map f s := quot.induction_on s $ λ l, rfl theorem map_comp_cons (f : α → β) (t) : map f ∘ cons t = cons (f t) ∘ map f := by { ext, simp } @[simp] theorem map_singleton (f : α → β) (a : α) : ({a} : multiset α).map f = {f a} := rfl theorem map_repeat (f : α → β) (a : α) (k : ℕ) : (repeat a k).map f = repeat (f a) k := by { induction k, simp, simpa } @[simp] theorem map_add (f : α → β) (s t) : map f (s + t) = map f s + map f t := quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ map_append _ _ _ /-- If each element of `s : multiset α` can be lifted to `β`, then `s` can be lifted to `multiset β`. -/ instance [can_lift α β] : can_lift (multiset α) (multiset β) := { cond := λ s, ∀ x ∈ s, can_lift.cond β x, coe := map can_lift.coe, prf := by { rintro ⟨l⟩ hl, lift l to list β using hl, exact ⟨l, coe_map _ _⟩ } } /-- `multiset.map` as an `add_monoid_hom`. -/ def map_add_monoid_hom (f : α → β) : multiset α →+ multiset β := { to_fun := map f, map_zero' := map_zero _, map_add' := map_add _ } @[simp] lemma coe_map_add_monoid_hom (f : α → β) : (map_add_monoid_hom f : multiset α → multiset β) = map f := rfl theorem map_nsmul (f : α → β) (n : ℕ) (s) : map f (n • s) = n • (map f s) := (map_add_monoid_hom f).map_nsmul _ _ @[simp] theorem mem_map {f : α → β} {b : β} {s : multiset α} : b ∈ map f s ↔ ∃ a, a ∈ s ∧ f a = b := quot.induction_on s $ λ l, mem_map @[simp] theorem card_map (f : α → β) (s) : card (map f s) = card s := quot.induction_on s $ λ l, length_map _ _ @[simp] theorem map_eq_zero {s : multiset α} {f : α → β} : s.map f = 0 ↔ s = 0 := by rw [← multiset.card_eq_zero, multiset.card_map, multiset.card_eq_zero] theorem mem_map_of_mem (f : α → β) {a : α} {s : multiset α} (h : a ∈ s) : f a ∈ map f s := mem_map.2 ⟨_, h, rfl⟩ lemma map_eq_singleton {f : α → β} {s : multiset α} {b : β} : map f s = {b} ↔ ∃ a : α, s = {a} ∧ f a = b := begin split, { intro h, obtain ⟨a, ha⟩ : ∃ a, s = {a}, { rw [←card_eq_one, ←card_map, h, card_singleton] }, refine ⟨a, ha, _⟩, rw [←mem_singleton, ←h, ha, map_singleton, mem_singleton] }, { rintro ⟨a, rfl, rfl⟩, simp } end theorem mem_map_of_injective {f : α → β} (H : function.injective f) {a : α} {s : multiset α} : f a ∈ map f s ↔ a ∈ s := quot.induction_on s $ λ l, mem_map_of_injective H @[simp] theorem map_map (g : β → γ) (f : α → β) (s : multiset α) : map g (map f s) = map (g ∘ f) s := quot.induction_on s $ λ l, congr_arg coe $ list.map_map _ _ _ theorem map_id (s : multiset α) : map id s = s := quot.induction_on s $ λ l, congr_arg coe $ map_id _ @[simp] lemma map_id' (s : multiset α) : map (λx, x) s = s := map_id s @[simp] theorem map_const (s : multiset α) (b : β) : map (function.const α b) s = repeat b s.card := quot.induction_on s $ λ l, congr_arg coe $ map_const _ _ theorem eq_of_mem_map_const {b₁ b₂ : β} {l : list α} (h : b₁ ∈ map (function.const α b₂) l) : b₁ = b₂ := eq_of_mem_repeat $ by rwa map_const at h @[simp] theorem map_le_map {f : α → β} {s t : multiset α} (h : s ≤ t) : map f s ≤ map f t := le_induction_on h $ λ l₁ l₂ h, (h.map f).subperm @[simp] lemma map_lt_map {f : α → β} {s t : multiset α} (h : s < t) : s.map f < t.map f := begin refine (map_le_map h.le).lt_of_not_le (λ H, h.ne $ eq_of_le_of_card_le h.le _), rw [←s.card_map f, ←t.card_map f], exact card_le_of_le H, end lemma map_mono (f : α → β) : monotone (map f) := λ _ _, map_le_map lemma map_strict_mono (f : α → β) : strict_mono (map f) := λ _ _, map_lt_map @[simp] theorem map_subset_map {f : α → β} {s t : multiset α} (H : s ⊆ t) : map f s ⊆ map f t := λ b m, let ⟨a, h, e⟩ := mem_map.1 m in mem_map.2 ⟨a, H h, e⟩ lemma map_erase [decidable_eq α] [decidable_eq β] (f : α → β) (hf : function.injective f) (x : α) (s : multiset α) : (s.erase x).map f = (s.map f).erase (f x) := begin induction s using multiset.induction_on with y s ih, { simp }, by_cases hxy : y = x, { cases hxy, simp }, { rw [s.erase_cons_tail hxy, map_cons, map_cons, (s.map f).erase_cons_tail (hf.ne hxy), ih] } end /-! ### `multiset.fold` -/ /-- `foldl f H b s` is the lift of the list operation `foldl f b l`, which folds `f` over the multiset. It is well defined when `f` is right-commutative, that is, `f (f b a₁) a₂ = f (f b a₂) a₁`. -/ def foldl (f : β → α → β) (H : right_commutative f) (b : β) (s : multiset α) : β := quot.lift_on s (λ l, foldl f b l) (λ l₁ l₂ p, p.foldl_eq H b) @[simp] theorem foldl_zero (f : β → α → β) (H b) : foldl f H b 0 = b := rfl @[simp] theorem foldl_cons (f : β → α → β) (H b a s) : foldl f H b (a ::ₘ s) = foldl f H (f b a) s := quot.induction_on s $ λ l, rfl @[simp] theorem foldl_add (f : β → α → β) (H b s t) : foldl f H b (s + t) = foldl f H (foldl f H b s) t := quotient.induction_on₂ s t $ λ l₁ l₂, foldl_append _ _ _ _ /-- `foldr f H b s` is the lift of the list operation `foldr f b l`, which folds `f` over the multiset. It is well defined when `f` is left-commutative, that is, `f a₁ (f a₂ b) = f a₂ (f a₁ b)`. -/ def foldr (f : α → β → β) (H : left_commutative f) (b : β) (s : multiset α) : β := quot.lift_on s (λ l, foldr f b l) (λ l₁ l₂ p, p.foldr_eq H b) @[simp] theorem foldr_zero (f : α → β → β) (H b) : foldr f H b 0 = b := rfl @[simp] theorem foldr_cons (f : α → β → β) (H b a s) : foldr f H b (a ::ₘ s) = f a (foldr f H b s) := quot.induction_on s $ λ l, rfl @[simp] theorem foldr_singleton (f : α → β → β) (H b a) : foldr f H b ({a} : multiset α) = f a b := rfl @[simp] theorem foldr_add (f : α → β → β) (H b s t) : foldr f H b (s + t) = foldr f H (foldr f H b t) s := quotient.induction_on₂ s t $ λ l₁ l₂, foldr_append _ _ _ _ @[simp] theorem coe_foldr (f : α → β → β) (H : left_commutative f) (b : β) (l : list α) : foldr f H b l = l.foldr f b := rfl @[simp] theorem coe_foldl (f : β → α → β) (H : right_commutative f) (b : β) (l : list α) : foldl f H b l = l.foldl f b := rfl theorem coe_foldr_swap (f : α → β → β) (H : left_commutative f) (b : β) (l : list α) : foldr f H b l = l.foldl (λ x y, f y x) b := (congr_arg (foldr f H b) (coe_reverse l)).symm.trans $ foldr_reverse _ _ _ theorem foldr_swap (f : α → β → β) (H : left_commutative f) (b : β) (s : multiset α) : foldr f H b s = foldl (λ x y, f y x) (λ x y z, (H _ _ _).symm) b s := quot.induction_on s $ λ l, coe_foldr_swap _ _ _ _ theorem foldl_swap (f : β → α → β) (H : right_commutative f) (b : β) (s : multiset α) : foldl f H b s = foldr (λ x y, f y x) (λ x y z, (H _ _ _).symm) b s := (foldr_swap _ _ _ _).symm lemma foldr_induction' (f : α → β → β) (H : left_commutative f) (x : β) (q : α → Prop) (p : β → Prop) (s : multiset α) (hpqf : ∀ a b, q a → p b → p (f a b)) (px : p x) (q_s : ∀ a ∈ s, q a) : p (foldr f H x s) := begin revert s, refine multiset.induction (by simp [px]) _, intros a s hs hsa, rw foldr_cons, have hps : ∀ (x : α), x ∈ s → q x, from λ x hxs, hsa x (mem_cons_of_mem hxs), exact hpqf a (foldr f H x s) (hsa a (mem_cons_self a s)) (hs hps), end lemma foldr_induction (f : α → α → α) (H : left_commutative f) (x : α) (p : α → Prop) (s : multiset α) (p_f : ∀ a b, p a → p b → p (f a b)) (px : p x) (p_s : ∀ a ∈ s, p a) : p (foldr f H x s) := foldr_induction' f H x p p s p_f px p_s lemma foldl_induction' (f : β → α → β) (H : right_commutative f) (x : β) (q : α → Prop) (p : β → Prop) (s : multiset α) (hpqf : ∀ a b, q a → p b → p (f b a)) (px : p x) (q_s : ∀ a ∈ s, q a) : p (foldl f H x s) := begin rw foldl_swap, exact foldr_induction' (λ x y, f y x) (λ x y z, (H _ _ _).symm) x q p s hpqf px q_s, end lemma foldl_induction (f : α → α → α) (H : right_commutative f) (x : α) (p : α → Prop) (s : multiset α) (p_f : ∀ a b, p a → p b → p (f b a)) (px : p x) (p_s : ∀ a ∈ s, p a) : p (foldl f H x s) := foldl_induction' f H x p p s p_f px p_s /-! ### Map for partial functions -/ /-- Lift of the list `pmap` operation. Map a partial function `f` over a multiset `s` whose elements are all in the domain of `f`. -/ def pmap {p : α → Prop} (f : Π a, p a → β) (s : multiset α) : (∀ a ∈ s, p a) → multiset β := quot.rec_on s (λ l H, ↑(pmap f l H)) $ λ l₁ l₂ (pp : l₁ ~ l₂), funext $ λ (H₂ : ∀ a ∈ l₂, p a), have H₁ : ∀ a ∈ l₁, p a, from λ a h, H₂ a (pp.subset h), have ∀ {s₂ e H}, @eq.rec (multiset α) l₁ (λ s, (∀ a ∈ s, p a) → multiset β) (λ _, ↑(pmap f l₁ H₁)) s₂ e H = ↑(pmap f l₁ H₁), by intros s₂ e _; subst e, this.trans $ quot.sound $ pp.pmap f @[simp] theorem coe_pmap {p : α → Prop} (f : Π a, p a → β) (l : list α) (H : ∀ a ∈ l, p a) : pmap f l H = l.pmap f H := rfl @[simp] lemma pmap_zero {p : α → Prop} (f : Π a, p a → β) (h : ∀a∈(0:multiset α), p a) : pmap f 0 h = 0 := rfl @[simp] lemma pmap_cons {p : α → Prop} (f : Π a, p a → β) (a : α) (m : multiset α) : ∀(h : ∀b∈a ::ₘ m, p b), pmap f (a ::ₘ m) h = f a (h a (mem_cons_self a m)) ::ₘ pmap f m (λa ha, h a $ mem_cons_of_mem ha) := quotient.induction_on m $ assume l h, rfl /-- "Attach" a proof that `a ∈ s` to each element `a` in `s` to produce a multiset on `{x // x ∈ s}`. -/ def attach (s : multiset α) : multiset {x // x ∈ s} := pmap subtype.mk s (λ a, id) @[simp] theorem coe_attach (l : list α) : @eq (multiset {x // x ∈ l}) (@attach α l) l.attach := rfl theorem sizeof_lt_sizeof_of_mem [has_sizeof α] {x : α} {s : multiset α} (hx : x ∈ s) : sizeof x < sizeof s := by { induction s with l a b, exact list.sizeof_lt_sizeof_of_mem hx, refl } theorem pmap_eq_map (p : α → Prop) (f : α → β) (s : multiset α) : ∀ H, @pmap _ _ p (λ a _, f a) s H = map f s := quot.induction_on s $ λ l H, congr_arg coe $ pmap_eq_map p f l H theorem pmap_congr {p q : α → Prop} {f : Π a, p a → β} {g : Π a, q a → β} (s : multiset α) {H₁ H₂} (h : ∀ a h₁ h₂, f a h₁ = g a h₂) : pmap f s H₁ = pmap g s H₂ := quot.induction_on s (λ l H₁ H₂, congr_arg coe $ pmap_congr l h) H₁ H₂ theorem map_pmap {p : α → Prop} (g : β → γ) (f : Π a, p a → β) (s) : ∀ H, map g (pmap f s H) = pmap (λ a h, g (f a h)) s H := quot.induction_on s $ λ l H, congr_arg coe $ map_pmap g f l H theorem pmap_eq_map_attach {p : α → Prop} (f : Π a, p a → β) (s) : ∀ H, pmap f s H = s.attach.map (λ x, f x.1 (H _ x.2)) := quot.induction_on s $ λ l H, congr_arg coe $ pmap_eq_map_attach f l H theorem attach_map_val (s : multiset α) : s.attach.map subtype.val = s := quot.induction_on s $ λ l, congr_arg coe $ attach_map_val l @[simp] theorem mem_attach (s : multiset α) : ∀ x, x ∈ s.attach := quot.induction_on s $ λ l, mem_attach _ @[simp] theorem mem_pmap {p : α → Prop} {f : Π a, p a → β} {s H b} : b ∈ pmap f s H ↔ ∃ a (h : a ∈ s), f a (H a h) = b := quot.induction_on s (λ l H, mem_pmap) H @[simp] theorem card_pmap {p : α → Prop} (f : Π a, p a → β) (s H) : card (pmap f s H) = card s := quot.induction_on s (λ l H, length_pmap) H @[simp] theorem card_attach {m : multiset α} : card (attach m) = card m := card_pmap _ _ _ @[simp] lemma attach_zero : (0 : multiset α).attach = 0 := rfl lemma attach_cons (a : α) (m : multiset α) : (a ::ₘ m).attach = ⟨a, mem_cons_self a m⟩ ::ₘ (m.attach.map $ λp, ⟨p.1, mem_cons_of_mem p.2⟩) := quotient.induction_on m $ assume l, congr_arg coe $ congr_arg (list.cons _) $ by rw [list.map_pmap]; exact list.pmap_congr _ (assume a' h₁ h₂, subtype.eq rfl) section decidable_pi_exists variables {m : multiset α} /-- If `p` is a decidable predicate, so is the predicate that all elements of a multiset satisfy `p`. -/ protected def decidable_forall_multiset {p : α → Prop} [hp : ∀a, decidable (p a)] : decidable (∀a∈m, p a) := quotient.rec_on_subsingleton m (λl, decidable_of_iff (∀a∈l, p a) $ by simp) instance decidable_dforall_multiset {p : Πa∈m, Prop} [hp : ∀a (h : a ∈ m), decidable (p a h)] : decidable (∀a (h : a ∈ m), p a h) := decidable_of_decidable_of_iff (@multiset.decidable_forall_multiset {a // a ∈ m} m.attach (λa, p a.1 a.2) _) (iff.intro (assume h a ha, h ⟨a, ha⟩ (mem_attach _ _)) (assume h ⟨a, ha⟩ _, h _ _)) /-- decidable equality for functions whose domain is bounded by multisets -/ instance decidable_eq_pi_multiset {β : α → Type*} [h : ∀a, decidable_eq (β a)] : decidable_eq (Πa∈m, β a) := assume f g, decidable_of_iff (∀a (h : a ∈ m), f a h = g a h) (by simp [function.funext_iff]) /-- If `p` is a decidable predicate, so is the existence of an element in a multiset satisfying `p`. -/ def decidable_exists_multiset {p : α → Prop} [decidable_pred p] : decidable (∃ x ∈ m, p x) := quotient.rec_on_subsingleton m list.decidable_exists_mem instance decidable_dexists_multiset {p : Πa∈m, Prop} [hp : ∀a (h : a ∈ m), decidable (p a h)] : decidable (∃a (h : a ∈ m), p a h) := decidable_of_decidable_of_iff (@multiset.decidable_exists_multiset {a // a ∈ m} m.attach (λa, p a.1 a.2) _) (iff.intro (λ ⟨⟨a, ha₁⟩, _, ha₂⟩, ⟨a, ha₁, ha₂⟩) (λ ⟨a, ha₁, ha₂⟩, ⟨⟨a, ha₁⟩, mem_attach _ _, ha₂⟩)) end decidable_pi_exists /-! ### Subtraction -/ section variables [decidable_eq α] {s t u : multiset α} {a b : α} /-- `s - t` is the multiset such that `count a (s - t) = count a s - count a t` for all `a` (note that it is truncated subtraction, so it is `0` if `count a t ≥ count a s`). -/ protected def sub (s t : multiset α) : multiset α := quotient.lift_on₂ s t (λ l₁ l₂, (l₁.diff l₂ : multiset α)) $ λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound $ p₁.diff p₂ instance : has_sub (multiset α) := ⟨multiset.sub⟩ @[simp] theorem coe_sub (s t : list α) : (s - t : multiset α) = (s.diff t : list α) := rfl /-- This is a special case of `tsub_zero`, which should be used instead of this. This is needed to prove `has_ordered_sub (multiset α)`. -/ protected theorem sub_zero (s : multiset α) : s - 0 = s := quot.induction_on s $ λ l, rfl @[simp] theorem sub_cons (a : α) (s t : multiset α) : s - a ::ₘ t = s.erase a - t := quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ diff_cons _ _ _ /-- This is a special case of `tsub_le_iff_right`, which should be used instead of this. This is needed to prove `has_ordered_sub (multiset α)`. -/ protected theorem sub_le_iff_le_add : s - t ≤ u ↔ s ≤ u + t := by revert s; exact multiset.induction_on t (by simp [multiset.sub_zero]) (λ a t IH s, by simp [IH, erase_le_iff_le_cons]) instance : has_ordered_sub (multiset α) := ⟨λ n m k, multiset.sub_le_iff_le_add⟩ theorem sub_eq_fold_erase (s t : multiset α) : s - t = foldl erase erase_comm s t := quotient.induction_on₂ s t $ λ l₁ l₂, show ↑(l₁.diff l₂) = foldl erase erase_comm ↑l₁ ↑l₂, by { rw diff_eq_foldl l₁ l₂, symmetry, exact foldl_hom _ _ _ _ _ (λ x y, rfl) } @[simp] theorem card_sub {s t : multiset α} (h : t ≤ s) : card (s - t) = card s - card t := (tsub_eq_of_eq_add_rev $ by rw [add_comm, ← card_add, tsub_add_cancel_of_le h]).symm /-! ### Union -/ /-- `s ∪ t` is the lattice join operation with respect to the multiset `≤`. The multiplicity of `a` in `s ∪ t` is the maximum of the multiplicities in `s` and `t`. -/ def union (s t : multiset α) : multiset α := s - t + t instance : has_union (multiset α) := ⟨union⟩ theorem union_def (s t : multiset α) : s ∪ t = s - t + t := rfl theorem le_union_left (s t : multiset α) : s ≤ s ∪ t := le_tsub_add theorem le_union_right (s t : multiset α) : t ≤ s ∪ t := le_add_left _ _ theorem eq_union_left : t ≤ s → s ∪ t = s := tsub_add_cancel_of_le theorem union_le_union_right (h : s ≤ t) (u) : s ∪ u ≤ t ∪ u := add_le_add_right (tsub_le_tsub_right h _) u theorem union_le (h₁ : s ≤ u) (h₂ : t ≤ u) : s ∪ t ≤ u := by rw ← eq_union_left h₂; exact union_le_union_right h₁ t @[simp] theorem mem_union : a ∈ s ∪ t ↔ a ∈ s ∨ a ∈ t := ⟨λ h, (mem_add.1 h).imp_left (mem_of_le tsub_le_self), or.rec (mem_of_le $ le_union_left _ _) (mem_of_le $ le_union_right _ _)⟩ @[simp] theorem map_union [decidable_eq β] {f : α → β} (finj : function.injective f) {s t : multiset α} : map f (s ∪ t) = map f s ∪ map f t := quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe (by rw [list.map_append f, list.map_diff finj]) /-! ### Intersection -/ /-- `s ∩ t` is the lattice meet operation with respect to the multiset `≤`. The multiplicity of `a` in `s ∩ t` is the minimum of the multiplicities in `s` and `t`. -/ def inter (s t : multiset α) : multiset α := quotient.lift_on₂ s t (λ l₁ l₂, (l₁.bag_inter l₂ : multiset α)) $ λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound $ p₁.bag_inter p₂ instance : has_inter (multiset α) := ⟨inter⟩ @[simp] theorem inter_zero (s : multiset α) : s ∩ 0 = 0 := quot.induction_on s $ λ l, congr_arg coe l.bag_inter_nil @[simp] theorem zero_inter (s : multiset α) : 0 ∩ s = 0 := quot.induction_on s $ λ l, congr_arg coe l.nil_bag_inter @[simp] theorem cons_inter_of_pos {a} (s : multiset α) {t} : a ∈ t → (a ::ₘ s) ∩ t = a ::ₘ s ∩ t.erase a := quotient.induction_on₂ s t $ λ l₁ l₂ h, congr_arg coe $ cons_bag_inter_of_pos _ h @[simp] theorem cons_inter_of_neg {a} (s : multiset α) {t} : a ∉ t → (a ::ₘ s) ∩ t = s ∩ t := quotient.induction_on₂ s t $ λ l₁ l₂ h, congr_arg coe $ cons_bag_inter_of_neg _ h theorem inter_le_left (s t : multiset α) : s ∩ t ≤ s := quotient.induction_on₂ s t $ λ l₁ l₂, (bag_inter_sublist_left _ _).subperm theorem inter_le_right (s : multiset α) : ∀ t, s ∩ t ≤ t := multiset.induction_on s (λ t, (zero_inter t).symm ▸ zero_le _) $ λ a s IH t, if h : a ∈ t then by simpa [h] using cons_le_cons a (IH (t.erase a)) else by simp [h, IH] theorem le_inter (h₁ : s ≤ t) (h₂ : s ≤ u) : s ≤ t ∩ u := begin revert s u, refine multiset.induction_on t _ (λ a t IH, _); intros, { simp [h₁] }, by_cases a ∈ u, { rw [cons_inter_of_pos _ h, ← erase_le_iff_le_cons], exact IH (erase_le_iff_le_cons.2 h₁) (erase_le_erase _ h₂) }, { rw cons_inter_of_neg _ h, exact IH ((le_cons_of_not_mem $ mt (mem_of_le h₂) h).1 h₁) h₂ } end @[simp] theorem mem_inter : a ∈ s ∩ t ↔ a ∈ s ∧ a ∈ t := ⟨λ h, ⟨mem_of_le (inter_le_left _ _) h, mem_of_le (inter_le_right _ _) h⟩, λ ⟨h₁, h₂⟩, by rw [← cons_erase h₁, cons_inter_of_pos _ h₂]; apply mem_cons_self⟩ instance : lattice (multiset α) := { sup := (∪), sup_le := @union_le _ _, le_sup_left := le_union_left, le_sup_right := le_union_right, inf := (∩), le_inf := @le_inter _ _, inf_le_left := inter_le_left, inf_le_right := inter_le_right, ..@multiset.partial_order α } @[simp] theorem sup_eq_union (s t : multiset α) : s ⊔ t = s ∪ t := rfl @[simp] theorem inf_eq_inter (s t : multiset α) : s ⊓ t = s ∩ t := rfl @[simp] theorem le_inter_iff : s ≤ t ∩ u ↔ s ≤ t ∧ s ≤ u := le_inf_iff @[simp] theorem union_le_iff : s ∪ t ≤ u ↔ s ≤ u ∧ t ≤ u := sup_le_iff theorem union_comm (s t : multiset α) : s ∪ t = t ∪ s := sup_comm theorem inter_comm (s t : multiset α) : s ∩ t = t ∩ s := inf_comm theorem eq_union_right (h : s ≤ t) : s ∪ t = t := by rw [union_comm, eq_union_left h] theorem union_le_union_left (h : s ≤ t) (u) : u ∪ s ≤ u ∪ t := sup_le_sup_left h _ theorem union_le_add (s t : multiset α) : s ∪ t ≤ s + t := union_le (le_add_right _ _) (le_add_left _ _) theorem union_add_distrib (s t u : multiset α) : (s ∪ t) + u = (s + u) ∪ (t + u) := by simpa [(∪), union, eq_comm, add_assoc] using show s + u - (t + u) = s - t, by rw [add_comm t, tsub_add_eq_tsub_tsub, add_tsub_cancel_right] theorem add_union_distrib (s t u : multiset α) : s + (t ∪ u) = (s + t) ∪ (s + u) := by rw [add_comm, union_add_distrib, add_comm s, add_comm s] theorem cons_union_distrib (a : α) (s t : multiset α) : a ::ₘ (s ∪ t) = (a ::ₘ s) ∪ (a ::ₘ t) := by simpa using add_union_distrib (a ::ₘ 0) s t theorem inter_add_distrib (s t u : multiset α) : (s ∩ t) + u = (s + u) ∩ (t + u) := begin by_contra h, cases lt_iff_cons_le.1 (lt_of_le_of_ne (le_inter (add_le_add_right (inter_le_left s t) u) (add_le_add_right (inter_le_right s t) u)) h) with a hl, rw ← cons_add at hl, exact not_le_of_lt (lt_cons_self (s ∩ t) a) (le_inter (le_of_add_le_add_right (le_trans hl (inter_le_left _ _))) (le_of_add_le_add_right (le_trans hl (inter_le_right _ _)))) end theorem add_inter_distrib (s t u : multiset α) : s + (t ∩ u) = (s + t) ∩ (s + u) := by rw [add_comm, inter_add_distrib, add_comm s, add_comm s] theorem cons_inter_distrib (a : α) (s t : multiset α) : a ::ₘ (s ∩ t) = (a ::ₘ s) ∩ (a ::ₘ t) := by simp theorem union_add_inter (s t : multiset α) : s ∪ t + s ∩ t = s + t := begin apply le_antisymm, { rw union_add_distrib, refine union_le (add_le_add_left (inter_le_right _ _) _) _, rw add_comm, exact add_le_add_right (inter_le_left _ _) _ }, { rw [add_comm, add_inter_distrib], refine le_inter (add_le_add_right (le_union_right _ _) _) _, rw add_comm, exact add_le_add_right (le_union_left _ _) _ } end theorem sub_add_inter (s t : multiset α) : s - t + s ∩ t = s := begin rw [inter_comm], revert s, refine multiset.induction_on t (by simp) (λ a t IH s, _), by_cases a ∈ s, { rw [cons_inter_of_pos _ h, sub_cons, add_cons, IH, cons_erase h] }, { rw [cons_inter_of_neg _ h, sub_cons, erase_of_not_mem h, IH] } end theorem sub_inter (s t : multiset α) : s - (s ∩ t) = s - t := add_right_cancel $ by rw [sub_add_inter s t, tsub_add_cancel_of_le (inter_le_left s t)] end /-! ### `multiset.filter` -/ section variables (p : α → Prop) [decidable_pred p] /-- `filter p s` returns the elements in `s` (with the same multiplicities) which satisfy `p`, and removes the rest. -/ def filter (s : multiset α) : multiset α := quot.lift_on s (λ l, (filter p l : multiset α)) (λ l₁ l₂ h, quot.sound $ h.filter p) @[simp] theorem coe_filter (l : list α) : filter p (↑l) = l.filter p := rfl @[simp] theorem filter_zero : filter p 0 = 0 := rfl lemma filter_congr {p q : α → Prop} [decidable_pred p] [decidable_pred q] {s : multiset α} : (∀ x ∈ s, p x ↔ q x) → filter p s = filter q s := quot.induction_on s $ λ l h, congr_arg coe $ filter_congr' h @[simp] theorem filter_add (s t : multiset α) : filter p (s + t) = filter p s + filter p t := quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ filter_append _ _ @[simp] theorem filter_le (s : multiset α) : filter p s ≤ s := quot.induction_on s $ λ l, (filter_sublist _).subperm @[simp] theorem filter_subset (s : multiset α) : filter p s ⊆ s := subset_of_le $ filter_le _ _ theorem filter_le_filter {s t} (h : s ≤ t) : filter p s ≤ filter p t := le_induction_on h $ λ l₁ l₂ h, (h.filter p).subperm lemma monotone_filter_left : monotone (filter p) := λ s t, filter_le_filter p lemma monotone_filter_right (s : multiset α) ⦃p q : α → Prop⦄ [decidable_pred p] [decidable_pred q] (h : p ≤ q) : s.filter p ≤ s.filter q := quotient.induction_on s (λ l, (l.monotone_filter_right h).subperm) variable {p} @[simp] theorem filter_cons_of_pos {a : α} (s) : p a → filter p (a ::ₘ s) = a ::ₘ filter p s := quot.induction_on s $ λ l h, congr_arg coe $ filter_cons_of_pos l h @[simp] theorem filter_cons_of_neg {a : α} (s) : ¬ p a → filter p (a ::ₘ s) = filter p s := quot.induction_on s $ λ l h, @congr_arg _ _ _ _ coe $ filter_cons_of_neg l h @[simp] theorem mem_filter {a : α} {s} : a ∈ filter p s ↔ a ∈ s ∧ p a := quot.induction_on s $ λ l, mem_filter theorem of_mem_filter {a : α} {s} (h : a ∈ filter p s) : p a := (mem_filter.1 h).2 theorem mem_of_mem_filter {a : α} {s} (h : a ∈ filter p s) : a ∈ s := (mem_filter.1 h).1 theorem mem_filter_of_mem {a : α} {l} (m : a ∈ l) (h : p a) : a ∈ filter p l := mem_filter.2 ⟨m, h⟩ theorem filter_eq_self {s} : filter p s = s ↔ ∀ a ∈ s, p a := quot.induction_on s $ λ l, iff.trans ⟨λ h, eq_of_sublist_of_length_eq (filter_sublist _) (@congr_arg _ _ _ _ card h), congr_arg coe⟩ filter_eq_self theorem filter_eq_nil {s} : filter p s = 0 ↔ ∀ a ∈ s, ¬p a := quot.induction_on s $ λ l, iff.trans ⟨λ h, eq_nil_of_length_eq_zero (@congr_arg _ _ _ _ card h), congr_arg coe⟩ filter_eq_nil theorem le_filter {s t} : s ≤ filter p t ↔ s ≤ t ∧ ∀ a ∈ s, p a := ⟨λ h, ⟨le_trans h (filter_le _ _), λ a m, of_mem_filter (mem_of_le h m)⟩, λ ⟨h, al⟩, filter_eq_self.2 al ▸ filter_le_filter p h⟩ theorem filter_cons {a : α} (s : multiset α) : filter p (a ::ₘ s) = (if p a then {a} else 0) + filter p s := begin split_ifs with h, { rw [filter_cons_of_pos _ h, singleton_add] }, { rw [filter_cons_of_neg _ h, zero_add] }, end lemma filter_nsmul (s : multiset α) (n : ℕ) : filter p (n • s) = n • filter p s := begin refine s.induction_on _ _, { simp only [filter_zero, nsmul_zero] }, { intros a ha ih, rw [nsmul_cons, filter_add, ih, filter_cons, nsmul_add], congr, split_ifs with hp; { simp only [filter_eq_self, nsmul_zero, filter_eq_nil], intros b hb, rwa (mem_singleton.mp (mem_of_mem_nsmul hb)) } } end variable (p) @[simp] theorem filter_sub [decidable_eq α] (s t : multiset α) : filter p (s - t) = filter p s - filter p t := begin revert s, refine multiset.induction_on t (by simp) (λ a t IH s, _), rw [sub_cons, IH], by_cases p a, { rw [filter_cons_of_pos _ h, sub_cons], congr, by_cases m : a ∈ s, { rw [← cons_inj_right a, ← filter_cons_of_pos _ h, cons_erase (mem_filter_of_mem m h), cons_erase m] }, { rw [erase_of_not_mem m, erase_of_not_mem (mt mem_of_mem_filter m)] } }, { rw [filter_cons_of_neg _ h], by_cases m : a ∈ s, { rw [(by rw filter_cons_of_neg _ h : filter p (erase s a) = filter p (a ::ₘ erase s a)), cons_erase m] }, { rw [erase_of_not_mem m] } } end @[simp] theorem filter_union [decidable_eq α] (s t : multiset α) : filter p (s ∪ t) = filter p s ∪ filter p t := by simp [(∪), union] @[simp] theorem filter_inter [decidable_eq α] (s t : multiset α) : filter p (s ∩ t) = filter p s ∩ filter p t := le_antisymm (le_inter (filter_le_filter _ $ inter_le_left _ _) (filter_le_filter _ $ inter_le_right _ _)) $ le_filter.2 ⟨inf_le_inf (filter_le _ _) (filter_le _ _), λ a h, of_mem_filter (mem_of_le (inter_le_left _ _) h)⟩ @[simp] theorem filter_filter (q) [decidable_pred q] (s : multiset α) : filter p (filter q s) = filter (λ a, p a ∧ q a) s := quot.induction_on s $ λ l, congr_arg coe $ filter_filter p q l theorem filter_add_filter (q) [decidable_pred q] (s : multiset α) : filter p s + filter q s = filter (λ a, p a ∨ q a) s + filter (λ a, p a ∧ q a) s := multiset.induction_on s rfl $ λ a s IH, by by_cases p a; by_cases q a; simp * theorem filter_add_not (s : multiset α) : filter p s + filter (λ a, ¬ p a) s = s := by rw [filter_add_filter, filter_eq_self.2, filter_eq_nil.2]; simp [decidable.em] theorem map_filter (f : β → α) (s : multiset β) : filter p (map f s) = map f (filter (p ∘ f) s) := quot.induction_on s (λ l, by simp [map_filter]) /-! ### Simultaneously filter and map elements of a multiset -/ /-- `filter_map f s` is a combination filter/map operation on `s`. The function `f : α → option β` is applied to each element of `s`; if `f a` is `some b` then `b` is added to the result, otherwise `a` is removed from the resulting multiset. -/ def filter_map (f : α → option β) (s : multiset α) : multiset β := quot.lift_on s (λ l, (filter_map f l : multiset β)) (λ l₁ l₂ h, quot.sound $ h.filter_map f) @[simp] theorem coe_filter_map (f : α → option β) (l : list α) : filter_map f l = l.filter_map f := rfl @[simp] theorem filter_map_zero (f : α → option β) : filter_map f 0 = 0 := rfl @[simp] theorem filter_map_cons_none {f : α → option β} (a : α) (s : multiset α) (h : f a = none) : filter_map f (a ::ₘ s) = filter_map f s := quot.induction_on s $ λ l, @congr_arg _ _ _ _ coe $ filter_map_cons_none a l h @[simp] theorem filter_map_cons_some (f : α → option β) (a : α) (s : multiset α) {b : β} (h : f a = some b) : filter_map f (a ::ₘ s) = b ::ₘ filter_map f s := quot.induction_on s $ λ l, @congr_arg _ _ _ _ coe $ filter_map_cons_some f a l h theorem filter_map_eq_map (f : α → β) : filter_map (some ∘ f) = map f := funext $ λ s, quot.induction_on s $ λ l, @congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_map f) l theorem filter_map_eq_filter : filter_map (option.guard p) = filter p := funext $ λ s, quot.induction_on s $ λ l, @congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_filter p) l theorem filter_map_filter_map (f : α → option β) (g : β → option γ) (s : multiset α) : filter_map g (filter_map f s) = filter_map (λ x, (f x).bind g) s := quot.induction_on s $ λ l, congr_arg coe $ filter_map_filter_map f g l theorem map_filter_map (f : α → option β) (g : β → γ) (s : multiset α) : map g (filter_map f s) = filter_map (λ x, (f x).map g) s := quot.induction_on s $ λ l, congr_arg coe $ map_filter_map f g l theorem filter_map_map (f : α → β) (g : β → option γ) (s : multiset α) : filter_map g (map f s) = filter_map (g ∘ f) s := quot.induction_on s $ λ l, congr_arg coe $ filter_map_map f g l theorem filter_filter_map (f : α → option β) (p : β → Prop) [decidable_pred p] (s : multiset α) : filter p (filter_map f s) = filter_map (λ x, (f x).filter p) s := quot.induction_on s $ λ l, congr_arg coe $ filter_filter_map f p l theorem filter_map_filter (f : α → option β) (s : multiset α) : filter_map f (filter p s) = filter_map (λ x, if p x then f x else none) s := quot.induction_on s $ λ l, congr_arg coe $ filter_map_filter p f l @[simp] theorem filter_map_some (s : multiset α) : filter_map some s = s := quot.induction_on s $ λ l, congr_arg coe $ filter_map_some l @[simp] theorem mem_filter_map (f : α → option β) (s : multiset α) {b : β} : b ∈ filter_map f s ↔ ∃ a, a ∈ s ∧ f a = some b := quot.induction_on s $ λ l, mem_filter_map f l theorem map_filter_map_of_inv (f : α → option β) (g : β → α) (H : ∀ x : α, (f x).map g = some x) (s : multiset α) : map g (filter_map f s) = s := quot.induction_on s $ λ l, congr_arg coe $ map_filter_map_of_inv f g H l theorem filter_map_le_filter_map (f : α → option β) {s t : multiset α} (h : s ≤ t) : filter_map f s ≤ filter_map f t := le_induction_on h $ λ l₁ l₂ h, (h.filter_map _).subperm /-! ### countp -/ /-- `countp p s` counts the number of elements of `s` (with multiplicity) that satisfy `p`. -/ def countp (s : multiset α) : ℕ := quot.lift_on s (countp p) (λ l₁ l₂, perm.countp_eq p) @[simp] theorem coe_countp (l : list α) : countp p l = l.countp p := rfl @[simp] theorem countp_zero : countp p 0 = 0 := rfl variable {p} @[simp] theorem countp_cons_of_pos {a : α} (s) : p a → countp p (a ::ₘ s) = countp p s + 1 := quot.induction_on s $ countp_cons_of_pos p @[simp] theorem countp_cons_of_neg {a : α} (s) : ¬ p a → countp p (a ::ₘ s) = countp p s := quot.induction_on s $ countp_cons_of_neg p variable (p) theorem countp_cons (b : α) (s) : countp p (b ::ₘ s) = countp p s + (if p b then 1 else 0) := begin split_ifs with h; simp only [h, multiset.countp_cons_of_pos, add_zero, multiset.countp_cons_of_neg, not_false_iff], end theorem countp_eq_card_filter (s) : countp p s = card (filter p s) := quot.induction_on s $ λ l, countp_eq_length_filter _ _ @[simp] theorem countp_add (s t) : countp p (s + t) = countp p s + countp p t := by simp [countp_eq_card_filter] /-- `countp p`, the number of elements of a multiset satisfying `p`, promoted to an `add_monoid_hom`. -/ def countp_add_monoid_hom : multiset α →+ ℕ := { to_fun := countp p, map_zero' := countp_zero _, map_add' := countp_add _ } @[simp] lemma coe_countp_add_monoid_hom : (countp_add_monoid_hom p : multiset α → ℕ) = countp p := rfl @[simp] theorem countp_sub [decidable_eq α] {s t : multiset α} (h : t ≤ s) : countp p (s - t) = countp p s - countp p t := by simp [countp_eq_card_filter, h, filter_le_filter] theorem countp_le_of_le {s t} (h : s ≤ t) : countp p s ≤ countp p t := by simpa [countp_eq_card_filter] using card_le_of_le (filter_le_filter p h) @[simp] theorem countp_filter (q) [decidable_pred q] (s : multiset α) : countp p (filter q s) = countp (λ a, p a ∧ q a) s := by simp [countp_eq_card_filter] theorem countp_map (f : α → β) (s : multiset α) (p : β → Prop) [decidable_pred p] : countp p (map f s) = (s.filter (λ a, p (f a))).card := begin refine multiset.induction_on s _ (λ a t IH, _), { rw [map_zero, countp_zero, filter_zero, card_zero] }, { rw [map_cons, countp_cons, IH, filter_cons, card_add, apply_ite card, card_zero, card_singleton, add_comm] }, end variable {p} theorem countp_pos {s} : 0 < countp p s ↔ ∃ a ∈ s, p a := by simp [countp_eq_card_filter, card_pos_iff_exists_mem] theorem countp_pos_of_mem {s a} (h : a ∈ s) (pa : p a) : 0 < countp p s := countp_pos.2 ⟨_, h, pa⟩ end /-! ### Multiplicity of an element -/ section variable [decidable_eq α] /-- `count a s` is the multiplicity of `a` in `s`. -/ def count (a : α) : multiset α → ℕ := countp (eq a) @[simp] theorem coe_count (a : α) (l : list α) : count a (↑l) = l.count a := coe_countp _ _ @[simp] theorem count_zero (a : α) : count a 0 = 0 := rfl @[simp] theorem count_cons_self (a : α) (s : multiset α) : count a (a ::ₘ s) = succ (count a s) := countp_cons_of_pos _ rfl @[simp, priority 990] theorem count_cons_of_ne {a b : α} (h : a ≠ b) (s : multiset α) : count a (b ::ₘ s) = count a s := countp_cons_of_neg _ h theorem count_le_of_le (a : α) {s t} : s ≤ t → count a s ≤ count a t := countp_le_of_le _ theorem count_le_count_cons (a b : α) (s : multiset α) : count a s ≤ count a (b ::ₘ s) := count_le_of_le _ (le_cons_self _ _) theorem count_cons (a b : α) (s : multiset α) : count a (b ::ₘ s) = count a s + (if a = b then 1 else 0) := by by_cases h : a = b; simp [h] @[simp] theorem count_singleton_self (a : α) : count a ({a} : multiset α) = 1 := by simp only [count_cons_self, singleton_eq_cons, eq_self_iff_true, count_zero] theorem count_singleton (a b : α) : count a ({b} : multiset α) = if a = b then 1 else 0 := by simp only [count_cons, singleton_eq_cons, count_zero, zero_add] @[simp] theorem count_add (a : α) : ∀ s t, count a (s + t) = count a s + count a t := countp_add _ /-- `count a`, the multiplicity of `a` in a multiset, promoted to an `add_monoid_hom`. -/ def count_add_monoid_hom (a : α) : multiset α →+ ℕ := countp_add_monoid_hom (eq a) @[simp] lemma coe_count_add_monoid_hom {a : α} : (count_add_monoid_hom a : multiset α → ℕ) = count a := rfl @[simp] theorem count_nsmul (a : α) (n s) : count a (n • s) = n * count a s := by induction n; simp [*, succ_nsmul', succ_mul, zero_nsmul] theorem count_pos {a : α} {s : multiset α} : 0 < count a s ↔ a ∈ s := by simp [count, countp_pos] theorem one_le_count_iff_mem {a : α} {s : multiset α} : 1 ≤ count a s ↔ a ∈ s := by rw [succ_le_iff, count_pos] @[simp, priority 980] theorem count_eq_zero_of_not_mem {a : α} {s : multiset α} (h : a ∉ s) : count a s = 0 := by_contradiction $ λ h', h $ count_pos.1 (nat.pos_of_ne_zero h') @[simp] theorem count_eq_zero {a : α} {s : multiset α} : count a s = 0 ↔ a ∉ s := iff_not_comm.1 $ count_pos.symm.trans pos_iff_ne_zero theorem count_ne_zero {a : α} {s : multiset α} : count a s ≠ 0 ↔ a ∈ s := by simp [ne.def, count_eq_zero] @[simp] theorem count_repeat_self (a : α) (n : ℕ) : count a (repeat a n) = n := by simp [repeat] theorem count_repeat (a b : α) (n : ℕ) : count a (repeat b n) = if (a = b) then n else 0 := begin split_ifs with h₁, { rw [h₁, count_repeat_self] }, { rw [count_eq_zero], apply mt eq_of_mem_repeat h₁ }, end @[simp] theorem count_erase_self (a : α) (s : multiset α) : count a (erase s a) = pred (count a s) := begin by_cases a ∈ s, { rw [(by rw cons_erase h : count a s = count a (a ::ₘ erase s a)), count_cons_self]; refl }, { rw [erase_of_not_mem h, count_eq_zero.2 h]; refl } end @[simp, priority 980] theorem count_erase_of_ne {a b : α} (ab : a ≠ b) (s : multiset α) : count a (erase s b) = count a s := begin by_cases b ∈ s, { rw [← count_cons_of_ne ab, cons_erase h] }, { rw [erase_of_not_mem h] } end @[simp] theorem count_sub (a : α) (s t : multiset α) : count a (s - t) = count a s - count a t := begin revert s, refine multiset.induction_on t (by simp) (λ b t IH s, _), rw [sub_cons, IH], by_cases ab : a = b, { subst b, rw [count_erase_self, count_cons_self, sub_succ, pred_sub] }, { rw [count_erase_of_ne ab, count_cons_of_ne ab] } end @[simp] theorem count_union (a : α) (s t : multiset α) : count a (s ∪ t) = max (count a s) (count a t) := by simp [(∪), union, tsub_add_eq_max, -add_comm] @[simp] theorem count_inter (a : α) (s t : multiset α) : count a (s ∩ t) = min (count a s) (count a t) := begin apply @nat.add_left_cancel (count a (s - t)), rw [← count_add, sub_add_inter, count_sub, tsub_add_min], end theorem le_count_iff_repeat_le {a : α} {s : multiset α} {n : ℕ} : n ≤ count a s ↔ repeat a n ≤ s := quot.induction_on s $ λ l, le_count_iff_repeat_sublist.trans repeat_le_coe.symm @[simp] theorem count_filter_of_pos {p} [decidable_pred p] {a} {s : multiset α} (h : p a) : count a (filter p s) = count a s := quot.induction_on s $ λ l, count_filter h @[simp] theorem count_filter_of_neg {p} [decidable_pred p] {a} {s : multiset α} (h : ¬ p a) : count a (filter p s) = 0 := multiset.count_eq_zero_of_not_mem (λ t, h (of_mem_filter t)) theorem count_filter {p} [decidable_pred p] {a} {s : multiset α} : count a (filter p s) = if p a then count a s else 0 := begin split_ifs with h, { exact count_filter_of_pos h }, { exact count_filter_of_neg h }, end theorem ext {s t : multiset α} : s = t ↔ ∀ a, count a s = count a t := quotient.induction_on₂ s t $ λ l₁ l₂, quotient.eq.trans perm_iff_count @[ext] theorem ext' {s t : multiset α} : (∀ a, count a s = count a t) → s = t := ext.2 @[simp] theorem coe_inter (s t : list α) : (s ∩ t : multiset α) = (s.bag_inter t : list α) := by ext; simp theorem le_iff_count {s t : multiset α} : s ≤ t ↔ ∀ a, count a s ≤ count a t := ⟨λ h a, count_le_of_le a h, λ al, by rw ← (ext.2 (λ a, by simp [max_eq_right (al a)]) : s ∪ t = t); apply le_union_left⟩ instance : distrib_lattice (multiset α) := { le_sup_inf := λ s t u, le_of_eq $ eq.symm $ ext.2 $ λ a, by simp only [max_min_distrib_left, multiset.count_inter, multiset.sup_eq_union, multiset.count_union, multiset.inf_eq_inter], ..multiset.lattice } theorem repeat_inf (s : multiset α) (a : α) (n : ℕ) : (repeat a n) ⊓ s = repeat a (min (s.count a) n) := begin ext x, rw [inf_eq_inter, count_inter, count_repeat, count_repeat], by_cases x = a, simp only [min_comm, h, if_true, eq_self_iff_true], simp only [h, if_false, zero_min], end theorem count_map {α β : Type*} (f : α → β) (s : multiset α) [decidable_eq β] (b : β) : count b (map f s) = (s.filter (λ a, b = f a)).card := countp_map _ _ _ /-- `multiset.map f` preserves `count` if `f` is injective on the set of elements contained in the multiset -/ theorem count_map_eq_count [decidable_eq β] (f : α → β) (s : multiset α) (hf : set.inj_on f {x : α | x ∈ s}) (x ∈ s) : (s.map f).count (f x) = s.count x := begin suffices : (filter (λ (a : α), f x = f a) s).count x = card (filter (λ (a : α), f x = f a) s), { rw [count, countp_map, ← this], exact count_filter_of_pos rfl }, { rw eq_repeat.2 ⟨rfl, λ b hb, eq_comm.1 ((hf H (mem_filter.1 hb).left) (mem_filter.1 hb).right)⟩, simp only [count_repeat, eq_self_iff_true, if_true, card_repeat]}, end /-- `multiset.map f` preserves `count` if `f` is injective -/ theorem count_map_eq_count' [decidable_eq β] (f : α → β) (s : multiset α) (hf : function.injective f) (x : α) : (s.map f).count (f x) = s.count x := begin by_cases H : x ∈ s, { exact count_map_eq_count f _ (set.inj_on_of_injective hf _) _ H, }, { rw [count_eq_zero_of_not_mem H, count_eq_zero, mem_map], rintro ⟨k, hks, hkx⟩, rw hf hkx at *, contradiction } end lemma filter_eq' (s : multiset α) (b : α) : s.filter (= b) = repeat b (count b s) := begin ext a, rw [count_repeat, count_filter], exact if_ctx_congr iff.rfl (λ h, congr_arg _ h) (λ h, rfl), end lemma filter_eq (s : multiset α) (b : α) : s.filter (eq b) = repeat b (count b s) := by simp_rw [←filter_eq', eq_comm] @[simp] lemma repeat_inter (x : α) (n : ℕ) (s : multiset α) : repeat x n ∩ s = repeat x (min n (s.count x)) := begin refine le_antisymm _ _, { simp only [le_iff_count, count_inter, count_repeat], intro a, split_ifs with h, { rw h }, { rw [nat.zero_min] } }, simp only [le_inter_iff, ← le_count_iff_repeat_le, count_inter, count_repeat_self], end @[simp] lemma inter_repeat (s : multiset α) (x : α) (n : ℕ) : s ∩ repeat x n = repeat x (min (s.count x) n) := by rw [inter_comm, repeat_inter, min_comm] end section embedding @[simp] lemma map_le_map_iff {f : α → β} (hf : function.injective f) {s t : multiset α} : s.map f ≤ t.map f ↔ s ≤ t := begin classical, refine ⟨λ h, le_iff_count.mpr (λ a, _), map_le_map⟩, simpa [count_map_eq_count' f _ hf] using le_iff_count.mp h (f a), end /-- Associate to an embedding `f` from `α` to `β` the order embedding that maps a multiset to its image under `f`. -/ @[simps] def map_embedding (f : α ↪ β) : multiset α ↪o multiset β := order_embedding.of_map_le_iff (map f) (λ _ _, map_le_map_iff f.inj') end embedding lemma count_eq_card_filter_eq [decidable_eq α] (s : multiset α) (a : α) : s.count a = (s.filter (eq a)).card := by rw [count, countp_eq_card_filter] /-- Mapping a multiset through a predicate and counting the `true`s yields the cardinality of the set filtered by the predicate. Note that this uses the notion of a multiset of `Prop`s - due to the decidability requirements of `count`, the decidability instance on the LHS is different from the RHS. In particular, the decidability instance on the left leaks `classical.dec_eq`. See [here](https://github.com/leanprover-community/mathlib/pull/11306#discussion_r782286812) for more discussion. -/ @[simp] lemma map_count_true_eq_filter_card (s : multiset α) (p : α → Prop) [decidable_pred p] : (s.map p).count true = (s.filter p).card := by simp only [count_eq_card_filter_eq, map_filter, card_map, function.comp.left_id, eq_true_eq_id] /-! ### Lift a relation to `multiset`s -/ section rel /-- `rel r s t` -- lift the relation `r` between two elements to a relation between `s` and `t`, s.t. there is a one-to-one mapping betweem elements in `s` and `t` following `r`. -/ @[mk_iff] inductive rel (r : α → β → Prop) : multiset α → multiset β → Prop | zero : rel 0 0 | cons {a b as bs} : r a b → rel as bs → rel (a ::ₘ as) (b ::ₘ bs) variables {δ : Type*} {r : α → β → Prop} {p : γ → δ → Prop} private lemma rel_flip_aux {s t} (h : rel r s t) : rel (flip r) t s := rel.rec_on h rel.zero (assume _ _ _ _ h₀ h₁ ih, rel.cons h₀ ih) lemma rel_flip {s t} : rel (flip r) s t ↔ rel r t s := ⟨rel_flip_aux, rel_flip_aux⟩ lemma rel_refl_of_refl_on {m : multiset α} {r : α → α → Prop} : (∀ x ∈ m, r x x) → rel r m m := begin apply m.induction_on, { intros, apply rel.zero }, { intros a m ih h, exact rel.cons (h _ (mem_cons_self _ _)) (ih (λ _ ha, h _ (mem_cons_of_mem ha))) } end lemma rel_eq_refl {s : multiset α} : rel (=) s s := rel_refl_of_refl_on (λ x hx, rfl) lemma rel_eq {s t : multiset α} : rel (=) s t ↔ s = t := begin split, { assume h, induction h; simp * }, { assume h, subst h, exact rel_eq_refl } end lemma rel.mono {r p : α → β → Prop} {s t} (hst : rel r s t) (h : ∀(a ∈ s) (b ∈ t), r a b → p a b) : rel p s t := begin induction hst, case rel.zero { exact rel.zero }, case rel.cons : a b s t hab hst ih { apply rel.cons (h a (mem_cons_self _ _) b (mem_cons_self _ _) hab), exact ih (λ a' ha' b' hb' h', h a' (mem_cons_of_mem ha') b' (mem_cons_of_mem hb') h') } end lemma rel.add {s t u v} (hst : rel r s t) (huv : rel r u v) : rel r (s + u) (t + v) := begin induction hst, case rel.zero { simpa using huv }, case rel.cons : a b s t hab hst ih { simpa using ih.cons hab } end lemma rel_flip_eq {s t : multiset α} : rel (λa b, b = a) s t ↔ s = t := show rel (flip (=)) s t ↔ s = t, by rw [rel_flip, rel_eq, eq_comm] @[simp] lemma rel_zero_left {b : multiset β} : rel r 0 b ↔ b = 0 := by rw [rel_iff]; simp @[simp] lemma rel_zero_right {a : multiset α} : rel r a 0 ↔ a = 0 := by rw [rel_iff]; simp lemma rel_cons_left {a as bs} : rel r (a ::ₘ as) bs ↔ (∃b bs', r a b ∧ rel r as bs' ∧ bs = b ::ₘ bs') := begin split, { generalize hm : a ::ₘ as = m, assume h, induction h generalizing as, case rel.zero { simp at hm, contradiction }, case rel.cons : a' b as' bs ha'b h ih { rcases cons_eq_cons.1 hm with ⟨eq₁, eq₂⟩ | ⟨h, cs, eq₁, eq₂⟩, { subst eq₁, subst eq₂, exact ⟨b, bs, ha'b, h, rfl⟩ }, { rcases ih eq₂.symm with ⟨b', bs', h₁, h₂, eq⟩, exact ⟨b', b ::ₘ bs', h₁, eq₁.symm ▸ rel.cons ha'b h₂, eq.symm ▸ cons_swap _ _ _⟩ } } }, { exact assume ⟨b, bs', hab, h, eq⟩, eq.symm ▸ rel.cons hab h } end lemma rel_cons_right {as b bs} : rel r as (b ::ₘ bs) ↔ (∃a as', r a b ∧ rel r as' bs ∧ as = a ::ₘ as') := begin rw [← rel_flip, rel_cons_left], refine exists₂_congr (λ a as', _), rw [rel_flip, flip] end lemma rel_add_left {as₀ as₁} : ∀{bs}, rel r (as₀ + as₁) bs ↔ (∃bs₀ bs₁, rel r as₀ bs₀ ∧ rel r as₁ bs₁ ∧ bs = bs₀ + bs₁) := multiset.induction_on as₀ (by simp) begin assume a s ih bs, simp only [ih, cons_add, rel_cons_left], split, { assume h, rcases h with ⟨b, bs', hab, h, rfl⟩, rcases h with ⟨bs₀, bs₁, h₀, h₁, rfl⟩, exact ⟨b ::ₘ bs₀, bs₁, ⟨b, bs₀, hab, h₀, rfl⟩, h₁, by simp⟩ }, { assume h, rcases h with ⟨bs₀, bs₁, h, h₁, rfl⟩, rcases h with ⟨b, bs, hab, h₀, rfl⟩, exact ⟨b, bs + bs₁, hab, ⟨bs, bs₁, h₀, h₁, rfl⟩, by simp⟩ } end lemma rel_add_right {as bs₀ bs₁} : rel r as (bs₀ + bs₁) ↔ (∃as₀ as₁, rel r as₀ bs₀ ∧ rel r as₁ bs₁ ∧ as = as₀ + as₁) := by rw [← rel_flip, rel_add_left]; simp [rel_flip] lemma rel_map_left {s : multiset γ} {f : γ → α} : ∀{t}, rel r (s.map f) t ↔ rel (λa b, r (f a) b) s t := multiset.induction_on s (by simp) (by simp [rel_cons_left] {contextual := tt}) lemma rel_map_right {s : multiset α} {t : multiset γ} {f : γ → β} : rel r s (t.map f) ↔ rel (λa b, r a (f b)) s t := by rw [← rel_flip, rel_map_left, ← rel_flip]; refl lemma rel_map {s : multiset α} {t : multiset β} {f : α → γ} {g : β → δ} : rel p (s.map f) (t.map g) ↔ rel (λa b, p (f a) (g b)) s t := rel_map_left.trans rel_map_right lemma card_eq_card_of_rel {r : α → β → Prop} {s : multiset α} {t : multiset β} (h : rel r s t) : card s = card t := by induction h; simp [*] lemma exists_mem_of_rel_of_mem {r : α → β → Prop} {s : multiset α} {t : multiset β} (h : rel r s t) : ∀ {a : α} (ha : a ∈ s), ∃ b ∈ t, r a b := begin induction h with x y s t hxy hst ih, { simp }, { assume a ha, cases mem_cons.1 ha with ha ha, { exact ⟨y, mem_cons_self _ _, ha.symm ▸ hxy⟩ }, { rcases ih ha with ⟨b, hbt, hab⟩, exact ⟨b, mem_cons.2 (or.inr hbt), hab⟩ } } end lemma rel_of_forall {m1 m2 : multiset α} {r : α → α → Prop} (h : ∀ a b, a ∈ m1 → b ∈ m2 → r a b) (hc : card m1 = card m2) : m1.rel r m2 := begin revert m1, apply m2.induction_on, { intros m h hc, rw [rel_zero_right, ← card_eq_zero, hc, card_zero] }, { intros a t ih m h hc, rw card_cons at hc, obtain ⟨b, hb⟩ := card_pos_iff_exists_mem.1 (show 0 < card m, from hc.symm ▸ (nat.succ_pos _)), obtain ⟨m', rfl⟩ := exists_cons_of_mem hb, refine rel_cons_right.mpr ⟨b, m', h _ _ hb (mem_cons_self _ _), ih _ _, rfl⟩, { exact λ _ _ ha hb, h _ _ (mem_cons_of_mem ha) (mem_cons_of_mem hb) }, { simpa using hc } } end lemma rel_repeat_left {m : multiset α} {a : α} {r : α → α → Prop} {n : ℕ} : (repeat a n).rel r m ↔ m.card = n ∧ ∀ x, x ∈ m → r a x := ⟨λ h, ⟨(card_eq_card_of_rel h).symm.trans (card_repeat _ _), λ x hx, begin obtain ⟨b, hb1, hb2⟩ := exists_mem_of_rel_of_mem (rel_flip.2 h) hx, rwa eq_of_mem_repeat hb1 at hb2, end⟩, λ h, rel_of_forall (λ x y hx hy, (eq_of_mem_repeat hx).symm ▸ (h.2 _ hy)) (eq.trans (card_repeat _ _) h.1.symm)⟩ lemma rel_repeat_right {m : multiset α} {a : α} {r : α → α → Prop} {n : ℕ} : m.rel r (repeat a n) ↔ m.card = n ∧ ∀ x, x ∈ m → r x a := by { rw [← rel_flip], exact rel_repeat_left } end rel section map theorem map_eq_map {f : α → β} (hf : function.injective f) {s t : multiset α} : s.map f = t.map f ↔ s = t := by { rw [← rel_eq, ← rel_eq, rel_map], simp only [hf.eq_iff] } theorem map_injective {f : α → β} (hf : function.injective f) : function.injective (multiset.map f) := assume x y, (map_eq_map hf).1 end map section quot theorem map_mk_eq_map_mk_of_rel {r : α → α → Prop} {s t : multiset α} (hst : s.rel r t) : s.map (quot.mk r) = t.map (quot.mk r) := rel.rec_on hst rfl $ assume a b s t hab hst ih, by simp [ih, quot.sound hab] theorem exists_multiset_eq_map_quot_mk {r : α → α → Prop} (s : multiset (quot r)) : ∃t:multiset α, s = t.map (quot.mk r) := multiset.induction_on s ⟨0, rfl⟩ $ assume a s ⟨t, ht⟩, quot.induction_on a $ assume a, ht.symm ▸ ⟨a ::ₘ t, (map_cons _ _ _).symm⟩ theorem induction_on_multiset_quot {r : α → α → Prop} {p : multiset (quot r) → Prop} (s : multiset (quot r)) : (∀s:multiset α, p (s.map (quot.mk r))) → p s := match s, exists_multiset_eq_map_quot_mk s with _, ⟨t, rfl⟩ := assume h, h _ end end quot /-! ### Disjoint multisets -/ /-- `disjoint s t` means that `s` and `t` have no elements in common. -/ def disjoint (s t : multiset α) : Prop := ∀ ⦃a⦄, a ∈ s → a ∈ t → false @[simp] theorem coe_disjoint (l₁ l₂ : list α) : @disjoint α l₁ l₂ ↔ l₁.disjoint l₂ := iff.rfl theorem disjoint.symm {s t : multiset α} (d : disjoint s t) : disjoint t s | a i₂ i₁ := d i₁ i₂ theorem disjoint_comm {s t : multiset α} : disjoint s t ↔ disjoint t s := ⟨disjoint.symm, disjoint.symm⟩ theorem disjoint_left {s t : multiset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := iff.rfl theorem disjoint_right {s t : multiset α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s := disjoint_comm theorem disjoint_iff_ne {s t : multiset α} : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b := by simp [disjoint_left, imp_not_comm] theorem disjoint_of_subset_left {s t u : multiset α} (h : s ⊆ u) (d : disjoint u t) : disjoint s t | x m₁ := d (h m₁) theorem disjoint_of_subset_right {s t u : multiset α} (h : t ⊆ u) (d : disjoint s u) : disjoint s t | x m m₁ := d m (h m₁) theorem disjoint_of_le_left {s t u : multiset α} (h : s ≤ u) : disjoint u t → disjoint s t := disjoint_of_subset_left (subset_of_le h) theorem disjoint_of_le_right {s t u : multiset α} (h : t ≤ u) : disjoint s u → disjoint s t := disjoint_of_subset_right (subset_of_le h) @[simp] theorem zero_disjoint (l : multiset α) : disjoint 0 l | a := (not_mem_nil a).elim @[simp, priority 1100] theorem singleton_disjoint {l : multiset α} {a : α} : disjoint {a} l ↔ a ∉ l := by simp [disjoint]; refl @[simp, priority 1100] theorem disjoint_singleton {l : multiset α} {a : α} : disjoint l {a} ↔ a ∉ l := by rw [disjoint_comm, singleton_disjoint] @[simp] theorem disjoint_add_left {s t u : multiset α} : disjoint (s + t) u ↔ disjoint s u ∧ disjoint t u := by simp [disjoint, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_add_right {s t u : multiset α} : disjoint s (t + u) ↔ disjoint s t ∧ disjoint s u := by rw [disjoint_comm, disjoint_add_left]; tauto @[simp] theorem disjoint_cons_left {a : α} {s t : multiset α} : disjoint (a ::ₘ s) t ↔ a ∉ t ∧ disjoint s t := (@disjoint_add_left _ {a} s t).trans $ by rw singleton_disjoint @[simp] theorem disjoint_cons_right {a : α} {s t : multiset α} : disjoint s (a ::ₘ t) ↔ a ∉ s ∧ disjoint s t := by rw [disjoint_comm, disjoint_cons_left]; tauto theorem inter_eq_zero_iff_disjoint [decidable_eq α] {s t : multiset α} : s ∩ t = 0 ↔ disjoint s t := by rw ← subset_zero; simp [subset_iff, disjoint] @[simp] theorem disjoint_union_left [decidable_eq α] {s t u : multiset α} : disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u := by simp [disjoint, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_union_right [decidable_eq α] {s t u : multiset α} : disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u := by simp [disjoint, or_imp_distrib, forall_and_distrib] lemma add_eq_union_iff_disjoint [decidable_eq α] {s t : multiset α} : s + t = s ∪ t ↔ disjoint s t := by simp_rw [←inter_eq_zero_iff_disjoint, ext, count_add, count_union, count_inter, count_zero, nat.min_eq_zero_iff, nat.add_eq_max_iff] lemma disjoint_map_map {f : α → γ} {g : β → γ} {s : multiset α} {t : multiset β} : disjoint (s.map f) (t.map g) ↔ (∀a∈s, ∀b∈t, f a ≠ g b) := by { simp [disjoint, @eq_comm _ (f _) (g _)], refl } /-- `pairwise r m` states that there exists a list of the elements s.t. `r` holds pairwise on this list. -/ def pairwise (r : α → α → Prop) (m : multiset α) : Prop := ∃l:list α, m = l ∧ l.pairwise r lemma pairwise_coe_iff_pairwise {r : α → α → Prop} (hr : symmetric r) {l : list α} : multiset.pairwise r l ↔ l.pairwise r := iff.intro (assume ⟨l', eq, h⟩, ((quotient.exact eq).pairwise_iff hr).2 h) (assume h, ⟨l, rfl, h⟩) end multiset namespace multiset section choose variables (p : α → Prop) [decidable_pred p] (l : multiset α) /-- Given a proof `hp` that there exists a unique `a ∈ l` such that `p a`, `choose_x p l hp` returns that `a` together with proofs of `a ∈ l` and `p a`. -/ def choose_x : Π hp : (∃! a, a ∈ l ∧ p a), { a // a ∈ l ∧ p a } := quotient.rec_on l (λ l' ex_unique, list.choose_x p l' (exists_of_exists_unique ex_unique)) begin intros, funext hp, suffices all_equal : ∀ x y : { t // t ∈ b ∧ p t }, x = y, { apply all_equal }, { rintros ⟨x, px⟩ ⟨y, py⟩, rcases hp with ⟨z, ⟨z_mem_l, pz⟩, z_unique⟩, congr, calc x = z : z_unique x px ... = y : (z_unique y py).symm } end /-- Given a proof `hp` that there exists a unique `a ∈ l` such that `p a`, `choose p l hp` returns that `a`. -/ 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 variable (α) /-- The equivalence between lists and multisets of a subsingleton type. -/ def subsingleton_equiv [subsingleton α] : list α ≃ multiset α := { to_fun := coe, inv_fun := quot.lift id $ λ (a b : list α) (h : a ~ b), list.ext_le h.length_eq $ λ n h₁ h₂, subsingleton.elim _ _, left_inv := λ l, rfl, right_inv := λ m, quot.induction_on m $ λ l, rfl } variable {α} @[simp] lemma coe_subsingleton_equiv [subsingleton α] : (subsingleton_equiv α : list α → multiset α) = coe := rfl end multiset
efc4172832ea23bcecdb1581e08f37f10ecef2bf
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/analysis/special_functions/complex/log.lean
7f31c378c7dd4be9af0c01b3d2d17b9c0f1dfd1f
[ "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
8,369
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Benjamin Davidson -/ import analysis.special_functions.complex.arg import analysis.special_functions.log /-! # The complex `log` function Basic properties, relationship with `exp`. -/ noncomputable theory namespace complex open set filter open_locale real topological_space /-- Inverse of the `exp` function. Returns values such that `(log x).im > - π` and `(log x).im ≤ π`. `log 0 = 0`-/ @[pp_nodot] noncomputable def log (x : ℂ) : ℂ := x.abs.log + arg x * I lemma log_re (x : ℂ) : x.log.re = x.abs.log := by simp [log] lemma log_im (x : ℂ) : x.log.im = x.arg := by simp [log] lemma neg_pi_lt_log_im (x : ℂ) : -π < (log x).im := by simp only [log_im, neg_pi_lt_arg] lemma log_im_le_pi (x : ℂ) : (log x).im ≤ π := by simp only [log_im, arg_le_pi] lemma exp_log {x : ℂ} (hx : x ≠ 0) : exp (log x) = x := by rw [log, exp_add_mul_I, ← of_real_sin, sin_arg, ← of_real_cos, cos_arg hx, ← of_real_exp, real.exp_log (abs_pos.2 hx), mul_add, of_real_div, of_real_div, mul_div_cancel' _ (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), ← mul_assoc, mul_div_cancel' _ (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), re_add_im] @[simp] lemma range_exp : range exp = {0}ᶜ := set.ext $ λ x, ⟨by { rintro ⟨x, rfl⟩, exact exp_ne_zero x }, λ hx, ⟨log x, exp_log hx⟩⟩ lemma exp_inj_of_neg_pi_lt_of_le_pi {x y : ℂ} (hx₁ : -π < x.im) (hx₂ : x.im ≤ π) (hy₁ : - π < y.im) (hy₂ : y.im ≤ π) (hxy : exp x = exp y) : x = y := by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y] at hxy; exact complex.ext (real.exp_injective $ by simpa [abs_mul, abs_cos_add_sin_mul_I] using congr_arg complex.abs hxy) (by simpa [(of_real_exp _).symm, - of_real_exp, arg_real_mul _ (real.exp_pos _), arg_cos_add_sin_mul_I hx₁ hx₂, arg_cos_add_sin_mul_I hy₁ hy₂] using congr_arg arg hxy) lemma log_exp {x : ℂ} (hx₁ : -π < x.im) (hx₂: x.im ≤ π) : log (exp x) = x := exp_inj_of_neg_pi_lt_of_le_pi (by rw log_im; exact neg_pi_lt_arg _) (by rw log_im; exact arg_le_pi _) hx₁ hx₂ (by rw [exp_log (exp_ne_zero _)]) lemma of_real_log {x : ℝ} (hx : 0 ≤ x) : (x.log : ℂ) = log x := complex.ext (by rw [log_re, of_real_re, abs_of_nonneg hx]) (by rw [of_real_im, log_im, arg_of_real_of_nonneg hx]) lemma log_of_real_re (x : ℝ) : (log (x : ℂ)).re = real.log x := by simp [log_re] @[simp] lemma log_zero : log 0 = 0 := by simp [log] @[simp] lemma log_one : log 1 = 0 := by simp [log] lemma log_neg_one : log (-1) = π * I := by simp [log] lemma log_I : log I = π / 2 * I := by simp [log] lemma log_neg_I : log (-I) = -(π / 2) * I := by simp [log] lemma two_pi_I_ne_zero : (2 * π * I : ℂ) ≠ 0 := by norm_num [real.pi_ne_zero, I_ne_zero] lemma exp_eq_one_iff {x : ℂ} : exp x = 1 ↔ ∃ n : ℤ, x = n * ((2 * π) * I) := have real.exp (x.re) * real.cos (x.im) = 1 → real.cos x.im ≠ -1, from λ h₁ h₂, begin rw [h₂, mul_neg_eq_neg_mul_symm, mul_one, neg_eq_iff_neg_eq] at h₁, have := real.exp_pos x.re, rw ← h₁ at this, exact absurd this (by norm_num) end, calc exp x = 1 ↔ (exp x).re = 1 ∧ (exp x).im = 0 : by simp [complex.ext_iff] ... ↔ real.cos x.im = 1 ∧ real.sin x.im = 0 ∧ x.re = 0 : begin rw exp_eq_exp_re_mul_sin_add_cos, simp [complex.ext_iff, cos_of_real_re, sin_of_real_re, exp_of_real_re, real.exp_ne_zero], split; finish [real.sin_eq_zero_iff_cos_eq] end ... ↔ (∃ n : ℤ, ↑n * (2 * π) = x.im) ∧ (∃ n : ℤ, ↑n * π = x.im) ∧ x.re = 0 : by rw [real.sin_eq_zero_iff, real.cos_eq_one_iff] ... ↔ ∃ n : ℤ, x = n * ((2 * π) * I) : ⟨λ ⟨⟨n, hn⟩, ⟨m, hm⟩, h⟩, ⟨n, by simp [complex.ext_iff, hn.symm, h]⟩, λ ⟨n, hn⟩, ⟨⟨n, by simp [hn]⟩, ⟨2 * n, by simp [hn, mul_comm, mul_assoc, mul_left_comm]⟩, by simp [hn]⟩⟩ lemma exp_eq_exp_iff_exp_sub_eq_one {x y : ℂ} : exp x = exp y ↔ exp (x - y) = 1 := by rw [exp_sub, div_eq_one_iff_eq (exp_ne_zero _)] lemma exp_eq_exp_iff_exists_int {x y : ℂ} : exp x = exp y ↔ ∃ n : ℤ, x = y + n * ((2 * π) * I) := by simp only [exp_eq_exp_iff_exp_sub_eq_one, exp_eq_one_iff, sub_eq_iff_eq_add'] @[simp] lemma countable_preimage_exp {s : set ℂ} : countable (exp ⁻¹' s) ↔ countable s := begin refine ⟨λ hs, _, λ hs, _⟩, { refine ((hs.image exp).insert 0).mono _, rw [image_preimage_eq_inter_range, range_exp, ← diff_eq, ← union_singleton, diff_union_self], exact subset_union_left _ _ }, { rw ← bUnion_preimage_singleton, refine hs.bUnion (λ z hz, _), rcases em (∃ w, exp w = z) with ⟨w, rfl⟩|hne, { simp only [preimage, mem_singleton_iff, exp_eq_exp_iff_exists_int, set_of_exists], exact countable_Union (λ m, countable_singleton _) }, { push_neg at hne, simp [preimage, hne] } } end alias countable_preimage_exp ↔ _ set.countable.preimage_cexp lemma tendsto_log_nhds_within_im_neg_of_re_neg_of_im_zero {z : ℂ} (hre : z.re < 0) (him : z.im = 0) : tendsto log (𝓝[{z : ℂ | z.im < 0}] z) (𝓝 $ real.log (abs z) - π * I) := begin have := (continuous_of_real.continuous_at.comp_continuous_within_at (continuous_abs.continuous_within_at.log _)).tendsto.add (((continuous_of_real.tendsto _).comp $ tendsto_arg_nhds_within_im_neg_of_re_neg_of_im_zero hre him).mul tendsto_const_nhds), convert this, { simp [sub_eq_add_neg] }, { lift z to ℝ using him, simpa using hre.ne } end lemma continuous_within_at_log_of_re_neg_of_im_zero {z : ℂ} (hre : z.re < 0) (him : z.im = 0) : continuous_within_at log {z : ℂ | 0 ≤ z.im} z := begin have := (continuous_of_real.continuous_at.comp_continuous_within_at (continuous_abs.continuous_within_at.log _)).tendsto.add ((continuous_of_real.continuous_at.comp_continuous_within_at $ continuous_within_at_arg_of_re_neg_of_im_zero hre him).mul tendsto_const_nhds), convert this, { lift z to ℝ using him, simpa using hre.ne } end lemma tendsto_log_nhds_within_im_nonneg_of_re_neg_of_im_zero {z : ℂ} (hre : z.re < 0) (him : z.im = 0) : tendsto log (𝓝[{z : ℂ | 0 ≤ z.im}] z) (𝓝 $ real.log (abs z) + π * I) := by simpa only [log, arg_eq_pi_iff.2 ⟨hre, him⟩] using (continuous_within_at_log_of_re_neg_of_im_zero hre him).tendsto end complex section log_deriv open complex filter open_locale topological_space variables {α : Type*} lemma continuous_at_clog {x : ℂ} (h : 0 < x.re ∨ x.im ≠ 0) : continuous_at log x := begin refine continuous_at.add _ _, { refine continuous_of_real.continuous_at.comp _, refine (real.continuous_at_log _).comp complex.continuous_abs.continuous_at, rw abs_ne_zero, intro hx, cases h, { refine h.ne.symm _, rw hx, exact zero_re, }, { refine h _, rw hx, exact zero_im, }, }, { have h_cont_mul : continuous (λ x : ℂ, x * I), from continuous_id'.mul continuous_const, refine h_cont_mul.continuous_at.comp (continuous_of_real.continuous_at.comp _), exact continuous_at_arg h, }, end lemma filter.tendsto.clog {l : filter α} {f : α → ℂ} {x : ℂ} (h : tendsto f l (𝓝 x)) (hx : 0 < x.re ∨ x.im ≠ 0) : tendsto (λ t, log (f t)) l (𝓝 $ log x) := (continuous_at_clog hx).tendsto.comp h variables [topological_space α] lemma continuous_at.clog {f : α → ℂ} {x : α} (h₁ : continuous_at f x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : continuous_at (λ t, log (f t)) x := h₁.clog h₂ lemma continuous_within_at.clog {f : α → ℂ} {s : set α} {x : α} (h₁ : continuous_within_at f s x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : continuous_within_at (λ t, log (f t)) s x := h₁.clog h₂ lemma continuous_on.clog {f : α → ℂ} {s : set α} (h₁ : continuous_on f s) (h₂ : ∀ x ∈ s, 0 < (f x).re ∨ (f x).im ≠ 0) : continuous_on (λ t, log (f t)) s := λ x hx, (h₁ x hx).clog (h₂ x hx) lemma continuous.clog {f : α → ℂ} (h₁ : continuous f) (h₂ : ∀ x, 0 < (f x).re ∨ (f x).im ≠ 0) : continuous (λ t, log (f t)) := continuous_iff_continuous_at.2 $ λ x, h₁.continuous_at.clog (h₂ x) end log_deriv
c85e2c11965e5e0e3ca1fd85bc1b1c521c1f40ee
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/field_theory/splitting_field.lean
eea0b8e29a9b7d3add3fc8aa8deccf4563586d7d
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
44,797
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import field_theory.minpoly import ring_theory.adjoin_root import linear_algebra.finite_dimensional import algebra.polynomial.big_operators import ring_theory.algebraic import ring_theory.algebra_tower import tactic.field_simp /-! # Splitting fields This file introduces the notion of a splitting field of a polynomial and provides an embedding from a splitting field to any field that splits the polynomial. A polynomial `f : polynomial K` splits over a field extension `L` of `K` if it is zero or all of its irreducible factors over `L` have degree `1`. A field extension of `K` of a polynomial `f : polynomial K` is called a splitting field if it is the smallest field extension of `K` such that `f` splits. ## Main definitions * `polynomial.splits i f`: A predicate on a field homomorphism `i : K → L` and a polynomial `f` saying that `f` is zero or all of its irreducible factors over `L` have degree `1`. * `polynomial.splitting_field f`: A fixed splitting field of the polynomial `f`. * `polynomial.is_splitting_field`: A predicate on a field to be a splitting field of a polynomial `f`. ## Main statements * `polynomial.C_leading_coeff_mul_prod_multiset_X_sub_C`: If a polynomial has as many roots as its degree, it can be written as the product of its leading coefficient with `∏ (X - a)` where `a` ranges through its roots. * `lift_of_splits`: If `K` and `L` are field extensions of a field `F` and for some finite subset `S` of `K`, the minimal polynomial of every `x ∈ K` splits as a polynomial with coefficients in `L`, then `algebra.adjoin F S` embeds into `L`. * `polynomial.is_splitting_field.lift`: An embedding of a splitting field of the polynomial `f` into another field such that `f` splits. * `polynomial.is_splitting_field.alg_equiv`: Every splitting field of a polynomial `f` is isomorphic to `splitting_field f` and thus, being a splitting field is unique up to isomorphism. -/ noncomputable theory open_locale classical big_operators universes u v w variables {F : Type u} {K : Type v} {L : Type w} namespace polynomial variables [field K] [field L] [field F] open polynomial section splits variables (i : K →+* L) /-- A polynomial `splits` iff it is zero or all of its irreducible factors have `degree` 1. -/ def splits (f : polynomial K) : Prop := f = 0 ∨ ∀ {g : polynomial L}, irreducible g → g ∣ f.map i → degree g = 1 @[simp] lemma splits_zero : splits i (0 : polynomial K) := or.inl rfl @[simp] lemma splits_C (a : K) : splits i (C a) := if ha : a = 0 then ha.symm ▸ (@C_0 K _).symm ▸ splits_zero i else have hia : i a ≠ 0, from mt ((i.injective_iff).1 i.injective _) ha, or.inr $ λ g hg ⟨p, hp⟩, absurd hg.1 (not_not.2 (is_unit_iff_degree_eq_zero.2 $ by have := congr_arg degree hp; simp [degree_C hia, @eq_comm (with_bot ℕ) 0, nat.with_bot.add_eq_zero_iff] at this; clear _fun_match; tauto)) lemma splits_of_degree_eq_one {f : polynomial K} (hf : degree f = 1) : splits i f := or.inr $ λ g hg ⟨p, hp⟩, by have := congr_arg degree hp; simp [nat.with_bot.add_eq_one_iff, hf, @eq_comm (with_bot ℕ) 1, mt is_unit_iff_degree_eq_zero.2 hg.1] at this; clear _fun_match; tauto lemma splits_of_degree_le_one {f : polynomial K} (hf : degree f ≤ 1) : splits i f := begin cases h : degree f with n, { rw [degree_eq_bot.1 h]; exact splits_zero i }, { cases n with n, { rw [eq_C_of_degree_le_zero (trans_rel_right (≤) h le_rfl)]; exact splits_C _ _ }, { have hn : n = 0, { rw h at hf, cases n, { refl }, { exact absurd hf dec_trivial } }, exact splits_of_degree_eq_one _ (by rw [h, hn]; refl) } } end lemma splits_of_nat_degree_le_one {f : polynomial K} (hf : nat_degree f ≤ 1) : splits i f := splits_of_degree_le_one i (degree_le_of_nat_degree_le hf) lemma splits_of_nat_degree_eq_one {f : polynomial K} (hf : nat_degree f = 1) : splits i f := splits_of_nat_degree_le_one i (le_of_eq hf) lemma splits_mul {f g : polynomial K} (hf : splits i f) (hg : splits i g) : splits i (f * g) := if h : f * g = 0 then by simp [h] else or.inr $ λ p hp hpf, ((principal_ideal_ring.irreducible_iff_prime.1 hp).2.2 _ _ (show p ∣ map i f * map i g, by convert hpf; rw polynomial.map_mul)).elim (hf.resolve_left (λ hf, by simpa [hf] using h) hp) (hg.resolve_left (λ hg, by simpa [hg] using h) hp) lemma splits_of_splits_mul {f g : polynomial K} (hfg : f * g ≠ 0) (h : splits i (f * g)) : splits i f ∧ splits i g := ⟨or.inr $ λ g hgi hg, or.resolve_left h hfg hgi (by rw map_mul; exact hg.trans (dvd_mul_right _ _)), or.inr $ λ g hgi hg, or.resolve_left h hfg hgi (by rw map_mul; exact hg.trans (dvd_mul_left _ _))⟩ lemma splits_of_splits_of_dvd {f g : polynomial K} (hf0 : f ≠ 0) (hf : splits i f) (hgf : g ∣ f) : splits i g := by { obtain ⟨f, rfl⟩ := hgf, exact (splits_of_splits_mul i hf0 hf).1 } lemma splits_of_splits_gcd_left {f g : polynomial K} (hf0 : f ≠ 0) (hf : splits i f) : splits i (euclidean_domain.gcd f g) := polynomial.splits_of_splits_of_dvd i hf0 hf (euclidean_domain.gcd_dvd_left f g) lemma splits_of_splits_gcd_right {f g : polynomial K} (hg0 : g ≠ 0) (hg : splits i g) : splits i (euclidean_domain.gcd f g) := polynomial.splits_of_splits_of_dvd i hg0 hg (euclidean_domain.gcd_dvd_right f g) lemma splits_map_iff (j : L →+* F) {f : polynomial K} : splits j (f.map i) ↔ splits (j.comp i) f := by simp [splits, polynomial.map_map] theorem splits_one : splits i 1 := splits_C i 1 theorem splits_of_is_unit {u : polynomial K} (hu : is_unit u) : u.splits i := splits_of_splits_of_dvd i one_ne_zero (splits_one _) $ is_unit_iff_dvd_one.1 hu theorem splits_X_sub_C {x : K} : (X - C x).splits i := splits_of_degree_eq_one _ $ degree_X_sub_C x theorem splits_X : X.splits i := splits_of_degree_eq_one _ $ degree_X theorem splits_id_iff_splits {f : polynomial K} : (f.map i).splits (ring_hom.id L) ↔ f.splits i := by rw [splits_map_iff, ring_hom.id_comp] theorem splits_mul_iff {f g : polynomial K} (hf : f ≠ 0) (hg : g ≠ 0) : (f * g).splits i ↔ f.splits i ∧ g.splits i := ⟨splits_of_splits_mul i (mul_ne_zero hf hg), λ ⟨hfs, hgs⟩, splits_mul i hfs hgs⟩ theorem splits_prod {ι : Type u} {s : ι → polynomial K} {t : finset ι} : (∀ j ∈ t, (s j).splits i) → (∏ x in t, s x).splits i := begin refine finset.induction_on t (λ _, splits_one i) (λ a t hat ih ht, _), rw finset.forall_mem_insert at ht, rw finset.prod_insert hat, exact splits_mul i ht.1 (ih ht.2) end lemma splits_pow {f : polynomial K} (hf : f.splits i) (n : ℕ) : (f ^ n).splits i := begin rw [←finset.card_range n, ←finset.prod_const], exact splits_prod i (λ j hj, hf), end lemma splits_X_pow (n : ℕ) : (X ^ n).splits i := splits_pow i (splits_X i) n theorem splits_prod_iff {ι : Type u} {s : ι → polynomial K} {t : finset ι} : (∀ j ∈ t, s j ≠ 0) → ((∏ x in t, s x).splits i ↔ ∀ j ∈ t, (s j).splits i) := begin refine finset.induction_on t (λ _, ⟨λ _ _ h, h.elim, λ _, splits_one i⟩) (λ a t hat ih ht, _), rw finset.forall_mem_insert at ht ⊢, rw [finset.prod_insert hat, splits_mul_iff i ht.1 (finset.prod_ne_zero_iff.2 ht.2), ih ht.2] end lemma degree_eq_one_of_irreducible_of_splits {p : polynomial L} (hp : irreducible p) (hp_splits : splits (ring_hom.id L) p) : p.degree = 1 := begin by_cases h_nz : p = 0, { exfalso, simp [*] at *, }, rcases hp_splits, { contradiction }, { apply hp_splits hp, simp } end lemma exists_root_of_splits {f : polynomial K} (hs : splits i f) (hf0 : degree f ≠ 0) : ∃ x, eval₂ i x f = 0 := if hf0 : f = 0 then ⟨37, by simp [hf0]⟩ else let ⟨g, hg⟩ := wf_dvd_monoid.exists_irreducible_factor (show ¬ is_unit (f.map i), from mt is_unit_iff_degree_eq_zero.1 (by rwa degree_map)) (map_ne_zero hf0) in let ⟨x, hx⟩ := exists_root_of_degree_eq_one (hs.resolve_left hf0 hg.1 hg.2) in let ⟨i, hi⟩ := hg.2 in ⟨x, by rw [← eval_map, hi, eval_mul, show _ = _, from hx, zero_mul]⟩ lemma exists_multiset_of_splits {f : polynomial K} : splits i f → ∃ (s : multiset L), f.map i = C (i f.leading_coeff) * (s.map (λ a : L, (X : polynomial L) - C a)).prod := suffices splits (ring_hom.id _) (f.map i) → ∃ s : multiset L, f.map i = (C (f.map i).leading_coeff) * (s.map (λ a : L, (X : polynomial L) - C a)).prod, by rwa [splits_map_iff, leading_coeff_map i] at this, wf_dvd_monoid.induction_on_irreducible (f.map i) (λ _, ⟨{37}, by simp [i.map_zero]⟩) (λ u hu _, ⟨0, by conv_lhs { rw eq_C_of_degree_eq_zero (is_unit_iff_degree_eq_zero.1 hu) }; simp [leading_coeff, nat_degree_eq_of_degree_eq_some (is_unit_iff_degree_eq_zero.1 hu)]⟩) (λ f p hf0 hp ih hfs, have hpf0 : p * f ≠ 0, from mul_ne_zero hp.ne_zero hf0, let ⟨s, hs⟩ := ih (splits_of_splits_mul _ hpf0 hfs).2 in ⟨-(p * norm_unit p).coeff 0 ::ₘ s, have hp1 : degree p = 1, from hfs.resolve_left hpf0 hp (by simp), begin rw [multiset.map_cons, multiset.prod_cons, leading_coeff_mul, C_mul, mul_assoc, mul_left_comm (C f.leading_coeff), ← hs, ← mul_assoc, mul_left_inj' hf0], conv_lhs {rw eq_X_add_C_of_degree_eq_one hp1}, simp only [mul_add, coe_norm_unit_of_ne_zero hp.ne_zero, mul_comm p, coeff_neg, C_neg, sub_eq_add_neg, neg_neg, coeff_C_mul, (mul_assoc _ _ _).symm, C_mul.symm, mul_inv_cancel (show p.leading_coeff ≠ 0, from mt leading_coeff_eq_zero.1 hp.ne_zero), one_mul], end⟩) /-- Pick a root of a polynomial that splits. -/ def root_of_splits {f : polynomial K} (hf : f.splits i) (hfd : f.degree ≠ 0) : L := classical.some $ exists_root_of_splits i hf hfd theorem map_root_of_splits {f : polynomial K} (hf : f.splits i) (hfd) : f.eval₂ i (root_of_splits i hf hfd) = 0 := classical.some_spec $ exists_root_of_splits i hf hfd theorem roots_map {f : polynomial K} (hf : f.splits $ ring_hom.id K) : (f.map i).roots = (f.roots).map i := if hf0 : f = 0 then by rw [hf0, map_zero, roots_zero, roots_zero, multiset.map_zero] else have hmf0 : f.map i ≠ 0 := map_ne_zero hf0, let ⟨m, hm⟩ := exists_multiset_of_splits _ hf in have h1 : (0 : polynomial K) ∉ m.map (λ r, X - C r), from zero_nmem_multiset_map_X_sub_C _ _, have h2 : (0 : polynomial L) ∉ m.map (λ r, X - C (i r)), from zero_nmem_multiset_map_X_sub_C _ _, begin rw map_id at hm, rw hm at hf0 hmf0 ⊢, rw map_mul at hmf0 ⊢, rw [roots_mul hf0, roots_mul hmf0, map_C, roots_C, zero_add, roots_C, zero_add, map_multiset_prod, multiset.map_map], simp_rw [(∘), map_sub, map_X, map_C], rw [roots_multiset_prod _ h2, multiset.bind_map, roots_multiset_prod _ h1, multiset.bind_map], simp_rw roots_X_sub_C, rw [multiset.bind_singleton, multiset.bind_singleton, multiset.map_id'] end lemma eq_prod_roots_of_splits {p : polynomial K} {i : K →+* L} (hsplit : splits i p) : p.map i = C (i p.leading_coeff) * ((p.map i).roots.map (λ a, X - C a)).prod := begin by_cases p_eq_zero : p = 0, { rw [p_eq_zero, map_zero, leading_coeff_zero, i.map_zero, C.map_zero, zero_mul] }, obtain ⟨s, hs⟩ := exists_multiset_of_splits i hsplit, have map_ne_zero : p.map i ≠ 0 := map_ne_zero (p_eq_zero), have prod_ne_zero : C (i p.leading_coeff) * (multiset.map (λ a, X - C a) s).prod ≠ 0 := by rwa hs at map_ne_zero, have zero_nmem : (0 : polynomial L) ∉ s.map (λ a, X - C a), from zero_nmem_multiset_map_X_sub_C _ _, have map_bind_roots_eq : (s.map (λ a, X - C a)).bind (λ a, a.roots) = s, { refine multiset.induction_on s (by rw [multiset.map_zero, multiset.zero_bind]) _, intros a s ih, rw [multiset.map_cons, multiset.cons_bind, ih, roots_X_sub_C, multiset.singleton_add] }, rw [hs, roots_mul prod_ne_zero, roots_C, zero_add, roots_multiset_prod _ zero_nmem, map_bind_roots_eq] end lemma eq_prod_roots_of_splits_id {p : polynomial K} (hsplit : splits (ring_hom.id K) p) : p = C (p.leading_coeff) * (p.roots.map (λ a, X - C a)).prod := by simpa using eq_prod_roots_of_splits hsplit lemma eq_prod_roots_of_monic_of_splits_id {p : polynomial K} (m : monic p) (hsplit : splits (ring_hom.id K) p) : p = (p.roots.map (λ a, X - C a)).prod := begin convert eq_prod_roots_of_splits_id hsplit, simp [m], end lemma eq_X_sub_C_of_splits_of_single_root {x : K} {h : polynomial K} (h_splits : splits i h) (h_roots : (h.map i).roots = {i x}) : h = (C (leading_coeff h)) * (X - C x) := begin apply polynomial.map_injective _ i.injective, rw [eq_prod_roots_of_splits h_splits, h_roots], simp, end lemma nat_degree_eq_card_roots {p : polynomial K} {i : K →+* L} (hsplit : splits i p) : p.nat_degree = (p.map i).roots.card := begin by_cases p_eq_zero : p = 0, { rw [p_eq_zero, nat_degree_zero, map_zero, roots_zero, multiset.card_zero] }, have map_ne_zero : p.map i ≠ 0 := map_ne_zero (p_eq_zero), rw eq_prod_roots_of_splits hsplit at map_ne_zero, conv_lhs { rw [← nat_degree_map i, eq_prod_roots_of_splits hsplit] }, have : (0 : polynomial L) ∉ (map i p).roots.map (λ a, X - C a), from zero_nmem_multiset_map_X_sub_C _ _, simp [nat_degree_mul (left_ne_zero_of_mul map_ne_zero) (right_ne_zero_of_mul map_ne_zero), nat_degree_multiset_prod _ this] end lemma degree_eq_card_roots {p : polynomial K} {i : K →+* L} (p_ne_zero : p ≠ 0) (hsplit : splits i p) : p.degree = (p.map i).roots.card := by rw [degree_eq_nat_degree p_ne_zero, nat_degree_eq_card_roots hsplit] section UFD local attribute [instance, priority 10] principal_ideal_ring.to_unique_factorization_monoid local infix ` ~ᵤ ` : 50 := associated open unique_factorization_monoid associates lemma splits_of_exists_multiset {f : polynomial K} {s : multiset L} (hs : f.map i = C (i f.leading_coeff) * (s.map (λ a : L, (X : polynomial L) - C a)).prod) : splits i f := if hf0 : f = 0 then or.inl hf0 else or.inr $ λ p hp hdp, have ht : multiset.rel associated (normalized_factors (f.map i)) (s.map (λ a : L, (X : polynomial L) - C a)) := factors_unique (λ p hp, irreducible_of_normalized_factor _ hp) (λ p' m, begin obtain ⟨a,m,rfl⟩ := multiset.mem_map.1 m, exact irreducible_of_degree_eq_one (degree_X_sub_C _), end) (associated.symm $ calc _ ~ᵤ f.map i : ⟨(units.map C.to_monoid_hom : Lˣ →* (polynomial L)ˣ) (units.mk0 (f.map i).leading_coeff (mt leading_coeff_eq_zero.1 (map_ne_zero hf0))), by conv_rhs { rw [hs, ← leading_coeff_map i, mul_comm] }; refl⟩ ... ~ᵤ _ : (unique_factorization_monoid.normalized_factors_prod (by simpa using hf0)).symm), let ⟨q, hq, hpq⟩ := exists_mem_normalized_factors_of_dvd (by simpa) hp hdp in let ⟨q', hq', hqq'⟩ := multiset.exists_mem_of_rel_of_mem ht hq in let ⟨a, ha⟩ := multiset.mem_map.1 hq' in by rw [← degree_X_sub_C a, ha.2]; exact degree_eq_degree_of_associated (hpq.trans hqq') lemma splits_of_splits_id {f : polynomial K} : splits (ring_hom.id _) f → splits i f := unique_factorization_monoid.induction_on_prime f (λ _, splits_zero _) (λ _ hu _, splits_of_degree_le_one _ ((is_unit_iff_degree_eq_zero.1 hu).symm ▸ dec_trivial)) (λ a p ha0 hp ih hfi, splits_mul _ (splits_of_degree_eq_one _ ((splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).1.resolve_left hp.1 hp.irreducible (by rw map_id))) (ih (splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).2)) end UFD lemma splits_iff_exists_multiset {f : polynomial K} : splits i f ↔ ∃ (s : multiset L), f.map i = C (i f.leading_coeff) * (s.map (λ a : L, (X : polynomial L) - C a)).prod := ⟨exists_multiset_of_splits i, λ ⟨s, hs⟩, splits_of_exists_multiset i hs⟩ lemma splits_comp_of_splits (j : L →+* F) {f : polynomial K} (h : splits i f) : splits (j.comp i) f := begin change i with ((ring_hom.id _).comp i) at h, rw [← splits_map_iff], rw [← splits_map_iff i] at h, exact splits_of_splits_id _ h end /-- A monic polynomial `p` that has as many roots as its degree can be written `p = ∏(X - a)`, for `a` in `p.roots`. -/ private lemma prod_multiset_X_sub_C_of_monic_of_roots_card_eq_of_field {p : polynomial K} (hmonic : p.monic) (hroots : p.roots.card = p.nat_degree) : (multiset.map (λ (a : K), X - C a) p.roots).prod = p := begin have hprodmonic : (multiset.map (λ (a : K), X - C a) p.roots).prod.monic, { simp only [prod_multiset_root_eq_finset_root, monic_prod_of_monic, monic_X_sub_C, monic_pow, forall_true_iff] }, have hdegree : (multiset.map (λ (a : K), X - C a) p.roots).prod.nat_degree = p.nat_degree, { rw [← hroots, nat_degree_multiset_prod _ (zero_nmem_multiset_map_X_sub_C _ (λ a : K, a))], simp only [eq_self_iff_true, mul_one, nat.cast_id, nsmul_eq_mul, multiset.sum_repeat, multiset.map_const,nat_degree_X_sub_C, function.comp, multiset.map_map] }, obtain ⟨q, hq⟩ := prod_multiset_X_sub_C_dvd p, have qzero : q ≠ 0, { rintro rfl, apply hmonic.ne_zero, simpa only [mul_zero] using hq }, have degp : p.nat_degree = (multiset.map (λ (a : K), X - C a) p.roots).prod.nat_degree + q.nat_degree, { nth_rewrite 0 [hq], simp only [nat_degree_mul (ne_zero_of_monic hprodmonic) qzero] }, have degq : q.nat_degree = 0, { rw hdegree at degp, rw [← add_right_inj p.nat_degree, ← degp, add_zero], }, obtain ⟨u, hu⟩ := is_unit_iff_degree_eq_zero.2 ((degree_eq_iff_nat_degree_eq qzero).2 degq), have hassoc : associated (multiset.map (λ (a : K), X - C a) p.roots).prod p, { rw associated, use u, rw [hu, ← hq] }, exact eq_of_monic_of_associated hprodmonic hmonic hassoc end lemma prod_multiset_X_sub_C_of_monic_of_roots_card_eq {K : Type*} [comm_ring K] [is_domain K] {p : polynomial K} (hmonic : p.monic) (hroots : p.roots.card = p.nat_degree) : (multiset.map (λ (a : K), X - C a) p.roots).prod = p := begin apply map_injective _ (is_fraction_ring.injective K (fraction_ring K)), rw map_multiset_prod, simp only [map_C, function.comp_app, map_X, multiset.map_map, map_sub], have : p.roots.map (algebra_map K (fraction_ring K)) = (map (algebra_map K (fraction_ring K)) p).roots := roots_map_of_injective_card_eq_total_degree (is_fraction_ring.injective K (fraction_ring K)) hroots, rw ← prod_multiset_X_sub_C_of_monic_of_roots_card_eq_of_field (monic_map (algebra_map K (fraction_ring K)) hmonic), { simp only [map_C, function.comp_app, map_X, map_sub], congr' 1, rw ← this, simp, }, { rw [nat_degree_map_eq_of_injective (is_fraction_ring.injective K (fraction_ring K)), ← this], simp only [←hroots, multiset.card_map], }, end /-- A polynomial `p` that has as many roots as its degree can be written `p = p.leading_coeff * ∏(X - a)`, for `a` in `p.roots`. Used to prove the more general `C_leading_coeff_mul_prod_multiset_X_sub_C` below. -/ private lemma C_leading_coeff_mul_prod_multiset_X_sub_C_of_field {p : polynomial K} (hroots : p.roots.card = p.nat_degree) : C p.leading_coeff * (multiset.map (λ (a : K), X - C a) p.roots).prod = p := begin by_cases hzero : p = 0, { rw [hzero, leading_coeff_zero, ring_hom.map_zero, zero_mul], }, { have hcoeff : p.leading_coeff ≠ 0, { intro h, exact hzero (leading_coeff_eq_zero.1 h) }, have hrootsnorm : (normalize p).roots.card = (normalize p).nat_degree, { rw [roots_normalize, normalize_apply, nat_degree_mul hzero (units.ne_zero _), hroots, coe_norm_unit, nat_degree_C, add_zero], }, have hprod := prod_multiset_X_sub_C_of_monic_of_roots_card_eq (monic_normalize hzero) hrootsnorm, rw [roots_normalize, normalize_apply, coe_norm_unit_of_ne_zero hzero] at hprod, calc (C p.leading_coeff) * (multiset.map (λ (a : K), X - C a) p.roots).prod = p * C ((p.leading_coeff)⁻¹ * p.leading_coeff) : by rw [hprod, mul_comm, mul_assoc, ← C_mul] ... = p * C 1 : by field_simp ... = p : by simp only [mul_one, ring_hom.map_one], }, end /-- A polynomial `p` that has as many roots as its degree can be written `p = p.leading_coeff * ∏(X - a)`, for `a` in `p.roots`. -/ lemma C_leading_coeff_mul_prod_multiset_X_sub_C {K : Type*} [comm_ring K] [is_domain K] {p : polynomial K} (hroots : p.roots.card = p.nat_degree) : C p.leading_coeff * (multiset.map (λ (a : K), X - C a) p.roots).prod = p := begin by_cases hzero : p = 0, { rw [hzero, leading_coeff_zero, ring_hom.map_zero, zero_mul], }, have hcoeff : p.leading_coeff ≠ 0, { intro h, exact hzero (leading_coeff_eq_zero.1 h) }, apply map_injective _ (is_fraction_ring.injective K (fraction_ring K)), rw [map_mul, map_multiset_prod], simp only [map_C, function.comp_app, map_X, multiset.map_map, map_sub], have h : p.roots.map (algebra_map K (fraction_ring K)) = (map (algebra_map K (fraction_ring K)) p).roots := roots_map_of_injective_card_eq_total_degree (is_fraction_ring.injective K (fraction_ring K)) hroots, have : multiset.card (map (algebra_map K (fraction_ring K)) p).roots = (map (algebra_map K (fraction_ring K)) p).nat_degree, { rw [nat_degree_map_eq_of_injective (is_fraction_ring.injective K (fraction_ring K)), ← h], simp only [←hroots, multiset.card_map], }, rw [← C_leading_coeff_mul_prod_multiset_X_sub_C_of_field this], simp only [map_C, function.comp_app, map_X, map_sub], congr' 2, { rw leading_coeff_map_of_leading_coeff_ne_zero, intro hn, apply hcoeff, apply is_fraction_ring.injective K (fraction_ring K), simp [hn], }, rw ← h, simp, end /-- A polynomial splits if and only if it has as many roots as its degree. -/ lemma splits_iff_card_roots {p : polynomial K} : splits (ring_hom.id K) p ↔ p.roots.card = p.nat_degree := begin split, { intro H, rw [nat_degree_eq_card_roots H, map_id] }, { intro hroots, apply (splits_iff_exists_multiset (ring_hom.id K)).2, use p.roots, simp only [ring_hom.id_apply, map_id], exact (C_leading_coeff_mul_prod_multiset_X_sub_C hroots).symm }, end lemma aeval_root_derivative_of_splits [algebra K L] {P : polynomial K} (hmo : P.monic) (hP : P.splits (algebra_map K L)) {r : L} (hr : r ∈ (P.map (algebra_map K L)).roots) : aeval r P.derivative = (multiset.map (λ a, r - a) ((P.map (algebra_map K L)).roots.erase r)).prod := begin replace hmo := monic_map (algebra_map K L) hmo, replace hP := (splits_id_iff_splits (algebra_map K L)).2 hP, rw [aeval_def, ← eval_map, ← derivative_map], nth_rewrite 0 [eq_prod_roots_of_monic_of_splits_id hmo hP], rw [eval_multiset_prod_X_sub_C_derivative hr] end /-- If `P` is a monic polynomial that splits, then `coeff P 0` equals the product of the roots. -/ lemma prod_roots_eq_coeff_zero_of_monic_of_split {P : polynomial K} (hmo : P.monic) (hP : P.splits (ring_hom.id K)) : coeff P 0 = (-1) ^ P.nat_degree * P.roots.prod := begin nth_rewrite 0 [eq_prod_roots_of_monic_of_splits_id hmo hP], rw [coeff_zero_eq_eval_zero, eval_multiset_prod, multiset.map_map], simp_rw [function.comp_app, eval_sub, eval_X, zero_sub, eval_C], conv_lhs { congr, congr, funext, rw [neg_eq_neg_one_mul] }, rw [multiset.prod_map_mul, multiset.map_const, multiset.prod_repeat, multiset.map_id', splits_iff_card_roots.1 hP] end /-- If `P` is a monic polynomial that splits, then `P.next_coeff` equals the sum of the roots. -/ lemma sum_roots_eq_next_coeff_of_monic_of_split {P : polynomial K} (hmo : P.monic) (hP : P.splits (ring_hom.id K)) : P.next_coeff = - P.roots.sum := begin nth_rewrite 0 [eq_prod_roots_of_monic_of_splits_id hmo hP], rw [monic.next_coeff_multiset_prod _ _ (λ a ha, _)], { simp_rw [next_coeff_X_sub_C, multiset.sum_map_neg] }, { exact monic_X_sub_C a } end end splits end polynomial section embeddings variables (F) [field F] /-- If `p` is the minimal polynomial of `a` over `F` then `F[a] ≃ₐ[F] F[x]/(p)` -/ def alg_equiv.adjoin_singleton_equiv_adjoin_root_minpoly {R : Type*} [comm_ring R] [algebra F R] (x : R) : algebra.adjoin F ({x} : set R) ≃ₐ[F] adjoin_root (minpoly F x) := alg_equiv.symm $ alg_equiv.of_bijective (alg_hom.cod_restrict (adjoin_root.lift_hom _ x $ minpoly.aeval F x) _ (λ p, adjoin_root.induction_on _ p $ λ p, (algebra.adjoin_singleton_eq_range_aeval F x).symm ▸ (polynomial.aeval _).mem_range.mpr ⟨p, rfl⟩)) ⟨(alg_hom.injective_cod_restrict _ _ _).2 $ (alg_hom.injective_iff _).2 $ λ p, adjoin_root.induction_on _ p $ λ p hp, ideal.quotient.eq_zero_iff_mem.2 $ ideal.mem_span_singleton.2 $ minpoly.dvd F x hp, λ y, let ⟨p, hp⟩ := (set_like.ext_iff.1 (algebra.adjoin_singleton_eq_range_aeval F x) (y : R)).1 y.2 in ⟨adjoin_root.mk _ p, subtype.eq hp⟩⟩ open finset /-- If a `subalgebra` is finite_dimensional as a submodule then it is `finite_dimensional`. -/ lemma finite_dimensional.of_subalgebra_to_submodule {K V : Type*} [field K] [ring V] [algebra K V] {s : subalgebra K V} (h : finite_dimensional K s.to_submodule) : finite_dimensional K s := h /-- If `K` and `L` are field extensions of `F` and we have `s : finset K` such that the minimal polynomial of each `x ∈ s` splits in `L` then `algebra.adjoin F s` embeds in `L`. -/ theorem lift_of_splits {F K L : Type*} [field F] [field K] [field L] [algebra F K] [algebra F L] (s : finset K) : (∀ x ∈ s, is_integral F x ∧ polynomial.splits (algebra_map F L) (minpoly F x)) → nonempty (algebra.adjoin F (↑s : set K) →ₐ[F] L) := begin refine finset.induction_on s (λ H, _) (λ a s has ih H, _), { rw [coe_empty, algebra.adjoin_empty], exact ⟨(algebra.of_id F L).comp (algebra.bot_equiv F K)⟩ }, rw forall_mem_insert at H, rcases H with ⟨⟨H1, H2⟩, H3⟩, cases ih H3 with f, choose H3 H4 using H3, rw [coe_insert, set.insert_eq, set.union_comm, algebra.adjoin_union_eq_adjoin_adjoin], letI := (f : algebra.adjoin F (↑s : set K) →+* L).to_algebra, haveI : finite_dimensional F (algebra.adjoin F (↑s : set K)) := ( (submodule.fg_iff_finite_dimensional _).1 (fg_adjoin_of_finite (set.finite_mem_finset s) H3)).of_subalgebra_to_submodule, letI := field_of_finite_dimensional F (algebra.adjoin F (↑s : set K)), have H5 : is_integral (algebra.adjoin F (↑s : set K)) a := is_integral_of_is_scalar_tower a H1, have H6 : (minpoly (algebra.adjoin F (↑s : set K)) a).splits (algebra_map (algebra.adjoin F (↑s : set K)) L), { refine polynomial.splits_of_splits_of_dvd _ (polynomial.map_ne_zero $ minpoly.ne_zero H1 : polynomial.map (algebra_map _ _) _ ≠ 0) ((polynomial.splits_map_iff _ _).2 _) (minpoly.dvd _ _ _), { rw ← is_scalar_tower.algebra_map_eq, exact H2 }, { rw [← is_scalar_tower.aeval_apply, minpoly.aeval] } }, obtain ⟨y, hy⟩ := polynomial.exists_root_of_splits _ H6 (ne_of_lt (minpoly.degree_pos H5)).symm, refine ⟨subalgebra.of_restrict_scalars _ _ _⟩, refine (adjoin_root.lift_hom (minpoly (algebra.adjoin F (↑s : set K)) a) y hy).comp _, exact alg_equiv.adjoin_singleton_equiv_adjoin_root_minpoly (algebra.adjoin F (↑s : set K)) a end end embeddings namespace polynomial variables [field K] [field L] [field F] open polynomial section splitting_field /-- Non-computably choose an irreducible factor from a polynomial. -/ def factor (f : polynomial K) : polynomial K := if H : ∃ g, irreducible g ∧ g ∣ f then classical.some H else X instance irreducible_factor (f : polynomial K) : irreducible (factor f) := begin rw factor, split_ifs with H, { exact (classical.some_spec H).1 }, { exact irreducible_X } end theorem factor_dvd_of_not_is_unit {f : polynomial K} (hf1 : ¬is_unit f) : factor f ∣ f := begin by_cases hf2 : f = 0, { rw hf2, exact dvd_zero _ }, rw [factor, dif_pos (wf_dvd_monoid.exists_irreducible_factor hf1 hf2)], exact (classical.some_spec $ wf_dvd_monoid.exists_irreducible_factor hf1 hf2).2 end theorem factor_dvd_of_degree_ne_zero {f : polynomial K} (hf : f.degree ≠ 0) : factor f ∣ f := factor_dvd_of_not_is_unit (mt degree_eq_zero_of_is_unit hf) theorem factor_dvd_of_nat_degree_ne_zero {f : polynomial K} (hf : f.nat_degree ≠ 0) : factor f ∣ f := factor_dvd_of_degree_ne_zero (mt nat_degree_eq_of_degree_eq_some hf) /-- Divide a polynomial f by X - C r where r is a root of f in a bigger field extension. -/ def remove_factor (f : polynomial K) : polynomial (adjoin_root $ factor f) := map (adjoin_root.of f.factor) f /ₘ (X - C (adjoin_root.root f.factor)) theorem X_sub_C_mul_remove_factor (f : polynomial K) (hf : f.nat_degree ≠ 0) : (X - C (adjoin_root.root f.factor)) * f.remove_factor = map (adjoin_root.of f.factor) f := let ⟨g, hg⟩ := factor_dvd_of_nat_degree_ne_zero hf in mul_div_by_monic_eq_iff_is_root.2 $ by rw [is_root.def, eval_map, hg, eval₂_mul, ← hg, adjoin_root.eval₂_root, zero_mul] theorem nat_degree_remove_factor (f : polynomial K) : f.remove_factor.nat_degree = f.nat_degree - 1 := by rw [remove_factor, nat_degree_div_by_monic _ (monic_X_sub_C _), nat_degree_map, nat_degree_X_sub_C] theorem nat_degree_remove_factor' {f : polynomial K} {n : ℕ} (hfn : f.nat_degree = n+1) : f.remove_factor.nat_degree = n := by rw [nat_degree_remove_factor, hfn, n.add_sub_cancel] /-- Auxiliary construction to a splitting field of a polynomial. Uses induction on the degree. -/ def splitting_field_aux (n : ℕ) : Π {K : Type u} [field K], by exactI Π (f : polynomial K), f.nat_degree = n → Type u := nat.rec_on n (λ K _ _ _, K) $ λ n ih K _ f hf, by exactI ih f.remove_factor (nat_degree_remove_factor' hf) namespace splitting_field_aux theorem succ (n : ℕ) (f : polynomial K) (hfn : f.nat_degree = n + 1) : splitting_field_aux (n+1) f hfn = splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn) := rfl instance field (n : ℕ) : Π {K : Type u} [field K], by exactI Π {f : polynomial K} (hfn : f.nat_degree = n), field (splitting_field_aux n f hfn) := nat.rec_on n (λ K _ _ _, ‹field K›) $ λ n ih K _ f hf, ih _ instance inhabited {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n) : inhabited (splitting_field_aux n f hfn) := ⟨37⟩ /- Note that the recursive nature of this definition and `splitting_field_aux.field` creates non-definitionally-equal diamonds in the `ℕ`- and `ℤ`- actions. ```lean example (n : ℕ) {K : Type u} [field K] {f : polynomial K} (hfn : f.nat_degree = n) : (add_comm_monoid.nat_module : module ℕ (splitting_field_aux n f hfn)) = @algebra.to_module _ _ _ _ (splitting_field_aux.algebra n _ hfn) := rfl -- fails ``` It's not immediately clear whether this _can_ be fixed; the failure is much the same as the reason that the following fails: ```lean def cases_twice {α} (a₀ aₙ : α) : ℕ → α × α | 0 := (a₀, a₀) | (n + 1) := (aₙ, aₙ) example (x : ℕ) {α} (a₀ aₙ : α) : (cases_twice a₀ aₙ x).1 = (cases_twice a₀ aₙ x).2 := rfl -- fails ``` We don't really care at this point because this is an implementation detail (which is why this is not a docstring), but we do in `splitting_field.algebra'` below. -/ instance algebra (n : ℕ) : Π (R : Type*) {K : Type u} [comm_semiring R] [field K], by exactI Π [algebra R K] {f : polynomial K} (hfn : f.nat_degree = n), algebra R (splitting_field_aux n f hfn) := nat.rec_on n (λ R K _ _ _ _ _, by exactI ‹algebra R K›) $ λ n ih R K _ _ _ f hfn, by exactI ih R (nat_degree_remove_factor' hfn) instance is_scalar_tower (n : ℕ) : Π (R₁ R₂ : Type*) {K : Type u} [comm_semiring R₁] [comm_semiring R₂] [has_scalar R₁ R₂] [field K], by exactI Π [algebra R₁ K] [algebra R₂ K], by exactI Π [is_scalar_tower R₁ R₂ K] {f : polynomial K} (hfn : f.nat_degree = n), is_scalar_tower R₁ R₂ (splitting_field_aux n f hfn) := nat.rec_on n (λ R₁ R₂ K _ _ _ _ _ _ _ _ _, by exactI ‹is_scalar_tower R₁ R₂ K›) $ λ n ih R₁ R₂ K _ _ _ _ _ _ _ f hfn, by exactI ih R₁ R₂ (nat_degree_remove_factor' hfn) instance algebra''' {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) : algebra (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)) := splitting_field_aux.algebra n _ _ instance algebra' {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) : algebra (adjoin_root f.factor) (splitting_field_aux n.succ f hfn) := splitting_field_aux.algebra''' _ instance algebra'' {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) : algebra K (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)) := splitting_field_aux.algebra n K _ instance scalar_tower' {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) : is_scalar_tower K (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)) := begin -- finding this instance ourselves makes things faster haveI : is_scalar_tower K (adjoin_root f.factor) (adjoin_root f.factor) := is_scalar_tower.right, exact splitting_field_aux.is_scalar_tower n K (adjoin_root f.factor) (nat_degree_remove_factor' hfn), end instance scalar_tower {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) : is_scalar_tower K (adjoin_root f.factor) (splitting_field_aux _ f hfn) := splitting_field_aux.scalar_tower' _ theorem algebra_map_succ (n : ℕ) (f : polynomial K) (hfn : f.nat_degree = n + 1) : by exact algebra_map K (splitting_field_aux _ _ hfn) = (algebra_map (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn))).comp (adjoin_root.of f.factor) := is_scalar_tower.algebra_map_eq _ _ _ protected theorem splits (n : ℕ) : ∀ {K : Type u} [field K], by exactI ∀ (f : polynomial K) (hfn : f.nat_degree = n), splits (algebra_map K $ splitting_field_aux n f hfn) f := nat.rec_on n (λ K _ _ hf, by exactI splits_of_degree_le_one _ (le_trans degree_le_nat_degree $ hf.symm ▸ with_bot.coe_le_coe.2 zero_le_one)) $ λ n ih K _ f hf, by { resetI, rw [← splits_id_iff_splits, algebra_map_succ, ← map_map, splits_id_iff_splits, ← X_sub_C_mul_remove_factor f (λ h, by { rw h at hf, cases hf })], exact splits_mul _ (splits_X_sub_C _) (ih _ _) } theorem exists_lift (n : ℕ) : ∀ {K : Type u} [field K], by exactI ∀ (f : polynomial K) (hfn : f.nat_degree = n) {L : Type*} [field L], by exactI ∀ (j : K →+* L) (hf : splits j f), ∃ k : splitting_field_aux n f hfn →+* L, k.comp (algebra_map _ _) = j := nat.rec_on n (λ K _ _ _ L _ j _, by exactI ⟨j, j.comp_id⟩) $ λ n ih K _ f hf L _ j hj, by exactI have hndf : f.nat_degree ≠ 0, by { intro h, rw h at hf, cases hf }, have hfn0 : f ≠ 0, by { intro h, rw h at hndf, exact hndf rfl }, let ⟨r, hr⟩ := exists_root_of_splits _ (splits_of_splits_of_dvd j hfn0 hj (factor_dvd_of_nat_degree_ne_zero hndf)) (mt is_unit_iff_degree_eq_zero.2 f.irreducible_factor.1) in have hmf0 : map (adjoin_root.of f.factor) f ≠ 0, from map_ne_zero hfn0, have hsf : splits (adjoin_root.lift j r hr) f.remove_factor, by { rw ← X_sub_C_mul_remove_factor _ hndf at hmf0, refine (splits_of_splits_mul _ hmf0 _).2, rwa [X_sub_C_mul_remove_factor _ hndf, ← splits_id_iff_splits, map_map, adjoin_root.lift_comp_of, splits_id_iff_splits] }, let ⟨k, hk⟩ := ih f.remove_factor (nat_degree_remove_factor' hf) (adjoin_root.lift j r hr) hsf in ⟨k, by rw [algebra_map_succ, ← ring_hom.comp_assoc, hk, adjoin_root.lift_comp_of]⟩ theorem adjoin_roots (n : ℕ) : ∀ {K : Type u} [field K], by exactI ∀ (f : polynomial K) (hfn : f.nat_degree = n), algebra.adjoin K (↑(f.map $ algebra_map K $ splitting_field_aux n f hfn).roots.to_finset : set (splitting_field_aux n f hfn)) = ⊤ := nat.rec_on n (λ K _ f hf, by exactI algebra.eq_top_iff.2 (λ x, subalgebra.range_le _ ⟨x, rfl⟩)) $ λ n ih K _ f hfn, by exactI have hndf : f.nat_degree ≠ 0, by { intro h, rw h at hfn, cases hfn }, have hfn0 : f ≠ 0, by { intro h, rw h at hndf, exact hndf rfl }, have hmf0 : map (algebra_map K (splitting_field_aux n.succ f hfn)) f ≠ 0 := map_ne_zero hfn0, by { rw [algebra_map_succ, ← map_map, ← X_sub_C_mul_remove_factor _ hndf, map_mul] at hmf0 ⊢, rw [roots_mul hmf0, map_sub, map_X, map_C, roots_X_sub_C, multiset.to_finset_add, finset.coe_union, multiset.to_finset_singleton, finset.coe_singleton, algebra.adjoin_union_eq_adjoin_adjoin, ← set.image_singleton, algebra.adjoin_algebra_map K (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)), adjoin_root.adjoin_root_eq_top, algebra.map_top, is_scalar_tower.adjoin_range_to_alg_hom K (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)), ih, subalgebra.restrict_scalars_top] } end splitting_field_aux /-- A splitting field of a polynomial. -/ def splitting_field (f : polynomial K) := splitting_field_aux _ f rfl namespace splitting_field variables (f : polynomial K) instance : field (splitting_field f) := splitting_field_aux.field _ _ instance inhabited : inhabited (splitting_field f) := ⟨37⟩ /-- This should be an instance globally, but it creates diamonds with the `ℕ` and `ℤ` actions: ```lean example : (add_comm_monoid.nat_module : module ℕ (splitting_field f)) = @algebra.to_module _ _ _ _ (splitting_field.algebra' f) := rfl -- fails example : (add_comm_group.int_module _ : module ℤ (splitting_field f)) = @algebra.to_module _ _ _ _ (splitting_field.algebra' f) := rfl -- fails ``` Until we resolve these diamonds, it's more convenient to only turn this instance on with `local attribute [instance]` in places where the benefit of having the instance outweighs the cost. In the meantime, the `splitting_field.algebra` instance below is immune to these particular diamonds since `K = ℕ` and `K = ℤ` are not possible due to the `field K` assumption. Diamonds in `algebra ℚ (splitting_field f)` instances are still possible, but this is a problem throughout the library and not unique to this `algebra` instance. -/ instance algebra' {R} [comm_semiring R] [algebra R K] : algebra R (splitting_field f) := splitting_field_aux.algebra _ _ _ instance : algebra K (splitting_field f) := splitting_field_aux.algebra _ _ _ protected theorem splits : splits (algebra_map K (splitting_field f)) f := splitting_field_aux.splits _ _ _ variables [algebra K L] (hb : splits (algebra_map K L) f) /-- Embeds the splitting field into any other field that splits the polynomial. -/ def lift : splitting_field f →ₐ[K] L := { commutes' := λ r, by { have := classical.some_spec (splitting_field_aux.exists_lift _ _ _ _ hb), exact ring_hom.ext_iff.1 this r }, .. classical.some (splitting_field_aux.exists_lift _ _ _ _ hb) } theorem adjoin_roots : algebra.adjoin K (↑(f.map (algebra_map K $ splitting_field f)).roots.to_finset : set (splitting_field f)) = ⊤ := splitting_field_aux.adjoin_roots _ _ _ theorem adjoin_root_set : algebra.adjoin K (f.root_set f.splitting_field) = ⊤ := adjoin_roots f end splitting_field variables (K L) [algebra K L] /-- Typeclass characterising splitting fields. -/ class is_splitting_field (f : polynomial K) : Prop := (splits [] : splits (algebra_map K L) f) (adjoin_roots [] : algebra.adjoin K (↑(f.map (algebra_map K L)).roots.to_finset : set L) = ⊤) namespace is_splitting_field variables {K} instance splitting_field (f : polynomial K) : is_splitting_field K (splitting_field f) f := ⟨splitting_field.splits f, splitting_field.adjoin_roots f⟩ section scalar_tower variables {K L F} [algebra F K] [algebra F L] [is_scalar_tower F K L] variables {K} instance map (f : polynomial F) [is_splitting_field F L f] : is_splitting_field K L (f.map $ algebra_map F K) := ⟨by { rw [splits_map_iff, ← is_scalar_tower.algebra_map_eq], exact splits L f }, subalgebra.restrict_scalars_injective F $ by { rw [map_map, ← is_scalar_tower.algebra_map_eq, subalgebra.restrict_scalars_top, eq_top_iff, ← adjoin_roots L f, algebra.adjoin_le_iff], exact λ x hx, @algebra.subset_adjoin K _ _ _ _ _ _ hx }⟩ variables {K} (L) theorem splits_iff (f : polynomial K) [is_splitting_field K L f] : polynomial.splits (ring_hom.id K) f ↔ (⊤ : subalgebra K L) = ⊥ := ⟨λ h, eq_bot_iff.2 $ adjoin_roots L f ▸ (roots_map (algebra_map K L) h).symm ▸ algebra.adjoin_le_iff.2 (λ y hy, let ⟨x, hxs, hxy⟩ := finset.mem_image.1 (by rwa multiset.to_finset_map at hy) in hxy ▸ set_like.mem_coe.2 $ subalgebra.algebra_map_mem _ _), λ h, @ring_equiv.to_ring_hom_refl K _ ▸ ring_equiv.self_trans_symm (ring_equiv.of_bijective _ $ algebra.bijective_algebra_map_iff.2 h) ▸ by { rw ring_equiv.to_ring_hom_trans, exact splits_comp_of_splits _ _ (splits L f) }⟩ theorem mul (f g : polynomial F) (hf : f ≠ 0) (hg : g ≠ 0) [is_splitting_field F K f] [is_splitting_field K L (g.map $ algebra_map F K)] : is_splitting_field F L (f * g) := ⟨(is_scalar_tower.algebra_map_eq F K L).symm ▸ splits_mul _ (splits_comp_of_splits _ _ (splits K f)) ((splits_map_iff _ _).1 (splits L $ g.map $ algebra_map F K)), by rw [map_mul, roots_mul (mul_ne_zero (map_ne_zero hf : f.map (algebra_map F L) ≠ 0) (map_ne_zero hg)), multiset.to_finset_add, finset.coe_union, algebra.adjoin_union_eq_adjoin_adjoin, is_scalar_tower.algebra_map_eq F K L, ← map_map, roots_map (algebra_map K L) ((splits_id_iff_splits $ algebra_map F K).2 $ splits K f), multiset.to_finset_map, finset.coe_image, algebra.adjoin_algebra_map, adjoin_roots, algebra.map_top, is_scalar_tower.adjoin_range_to_alg_hom, ← map_map, adjoin_roots, subalgebra.restrict_scalars_top]⟩ end scalar_tower /-- Splitting field of `f` embeds into any field that splits `f`. -/ def lift [algebra K F] (f : polynomial K) [is_splitting_field K L f] (hf : polynomial.splits (algebra_map K F) f) : L →ₐ[K] F := if hf0 : f = 0 then (algebra.of_id K F).comp $ (algebra.bot_equiv K L : (⊥ : subalgebra K L) →ₐ[K] K).comp $ by { rw ← (splits_iff L f).1 (show f.splits (ring_hom.id K), from hf0.symm ▸ splits_zero _), exact algebra.to_top } else alg_hom.comp (by { rw ← adjoin_roots L f, exact classical.choice (lift_of_splits _ $ λ y hy, have aeval y f = 0, from (eval₂_eq_eval_map _).trans $ (mem_roots $ by exact map_ne_zero hf0).1 (multiset.mem_to_finset.mp hy), ⟨is_algebraic_iff_is_integral.1 ⟨f, hf0, this⟩, splits_of_splits_of_dvd _ hf0 hf $ minpoly.dvd _ _ this⟩) }) algebra.to_top theorem finite_dimensional (f : polynomial K) [is_splitting_field K L f] : finite_dimensional K L := ⟨@algebra.top_to_submodule K L _ _ _ ▸ adjoin_roots L f ▸ fg_adjoin_of_finite (set.finite_mem_finset _) (λ y hy, if hf : f = 0 then by { rw [hf, map_zero, roots_zero] at hy, cases hy } else is_algebraic_iff_is_integral.1 ⟨f, hf, (eval₂_eq_eval_map _).trans $ (mem_roots $ by exact map_ne_zero hf).1 (multiset.mem_to_finset.mp hy)⟩)⟩ instance (f : polynomial K) : _root_.finite_dimensional K f.splitting_field := finite_dimensional f.splitting_field f /-- Any splitting field is isomorphic to `splitting_field f`. -/ def alg_equiv (f : polynomial K) [is_splitting_field K L f] : L ≃ₐ[K] splitting_field f := begin refine alg_equiv.of_bijective (lift L f $ splits (splitting_field f) f) ⟨ring_hom.injective (lift L f $ splits (splitting_field f) f).to_ring_hom, _⟩, haveI := finite_dimensional (splitting_field f) f, haveI := finite_dimensional L f, have : finite_dimensional.finrank K L = finite_dimensional.finrank K (splitting_field f) := le_antisymm (linear_map.finrank_le_finrank_of_injective (show function.injective (lift L f $ splits (splitting_field f) f).to_linear_map, from ring_hom.injective (lift L f $ splits (splitting_field f) f : L →+* f.splitting_field))) (linear_map.finrank_le_finrank_of_injective (show function.injective (lift (splitting_field f) f $ splits L f).to_linear_map, from ring_hom.injective (lift (splitting_field f) f $ splits L f : f.splitting_field →+* L))), change function.surjective (lift L f $ splits (splitting_field f) f).to_linear_map, refine (linear_map.injective_iff_surjective_of_finrank_eq_finrank this).1 _, exact ring_hom.injective (lift L f $ splits (splitting_field f) f : L →+* f.splitting_field) end end is_splitting_field end splitting_field end polynomial
00faf6d04911228264556feabd32828f3d047f03
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/measure_theory/measure/vector_measure.lean
8e58f333844117ecedfff302f3407c44f20df326
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
36,721
lean
/- Copyright (c) 2021 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import measure_theory.integral.lebesgue /-! # Vector valued measures This file defines vector valued measures, which are σ-additive functions from a set to a add monoid `M` such that it maps the empty set and non-measurable sets to zero. In the case that `M = ℝ`, we called the vector measure a signed measure and write `signed_measure α`. Similarly, when `M = ℂ`, we call the measure a complex measure and write `complex_measure α`. ## Main definitions * `measure_theory.vector_measure` is a vector valued, σ-additive function that maps the empty and non-measurable set to zero. * `measure_theory.vector_measure.map` is the pushforward of a vector measure along a function. * `measure_theory.vector_measure.restrict` is the restriction of a vector measure on some set. ## Notation * `v ≤[i] w` means that the vector measure `v` restricted on the set `i` is less than or equal to the vector measure `w` restricted on `i`, i.e. `v.restrict i ≤ w.restrict i`. ## Implementation notes We require all non-measurable sets to be mapped to zero in order for the extensionality lemma to only compare the underlying functions for measurable sets. We use `has_sum` instead of `tsum` in the definition of vector measures in comparison to `measure` since this provides summablity. ## Tags vector measure, signed measure, complex measure -/ noncomputable theory open_locale classical big_operators nnreal ennreal namespace measure_theory variables {α β : Type*} [measurable_space α] /-- A vector measure on a measurable space `α` is a σ-additive `M`-valued function (for some `M` an add monoid) such that the empty set and non-measurable sets are mapped to zero. -/ structure vector_measure (α : Type*) [measurable_space α] (M : Type*) [add_comm_monoid M] [topological_space M] := (measure_of' : set α → M) (empty' : measure_of' ∅ = 0) (not_measurable' ⦃i : set α⦄ : ¬ measurable_set i → measure_of' i = 0) (m_Union' ⦃f : ℕ → set α⦄ : (∀ i, measurable_set (f i)) → pairwise (disjoint on f) → has_sum (λ i, measure_of' (f i)) (measure_of' (⋃ i, f i))) /-- A `signed_measure` is a `ℝ`-vector measure. -/ abbreviation signed_measure (α : Type*) [measurable_space α] := vector_measure α ℝ /-- A `complex_measure` is a `ℂ`-vector_measure. -/ abbreviation complex_measure (α : Type*) [measurable_space α] := vector_measure α ℂ open set measure_theory namespace vector_measure section variables {M : Type*} [add_comm_monoid M] [topological_space M] instance : has_coe_to_fun (vector_measure α M) := ⟨λ _, set α → M, vector_measure.measure_of'⟩ initialize_simps_projections vector_measure (measure_of' → apply) @[simp] lemma measure_of_eq_coe (v : vector_measure α M) : v.measure_of' = v := rfl @[simp] lemma empty (v : vector_measure α M) : v ∅ = 0 := v.empty' lemma not_measurable (v : vector_measure α M) {i : set α} (hi : ¬ measurable_set i) : v i = 0 := v.not_measurable' hi lemma m_Union (v : vector_measure α M) {f : ℕ → set α} (hf₁ : ∀ i, measurable_set (f i)) (hf₂ : pairwise (disjoint on f)) : has_sum (λ i, v (f i)) (v (⋃ i, f i)) := v.m_Union' hf₁ hf₂ lemma of_disjoint_Union_nat [t2_space M] (v : vector_measure α M) {f : ℕ → set α} (hf₁ : ∀ i, measurable_set (f i)) (hf₂ : pairwise (disjoint on f)) : v (⋃ i, f i) = ∑' i, v (f i) := (v.m_Union hf₁ hf₂).tsum_eq.symm lemma coe_injective : @function.injective (vector_measure α M) (set α → M) coe_fn := λ v w h, by { cases v, cases w, congr' } lemma ext_iff' (v w : vector_measure α M) : v = w ↔ ∀ i : set α, v i = w i := by rw [← coe_injective.eq_iff, function.funext_iff] lemma ext_iff (v w : vector_measure α M) : v = w ↔ ∀ i : set α, measurable_set i → v i = w i := begin split, { rintro rfl _ _, refl }, { rw ext_iff', intros h i, by_cases hi : measurable_set i, { exact h i hi }, { simp_rw [not_measurable _ hi] } } end @[ext] lemma ext {s t : vector_measure α M} (h : ∀ i : set α, measurable_set i → s i = t i) : s = t := (ext_iff s t).2 h variables [t2_space M] {v : vector_measure α M} {f : ℕ → set α} lemma has_sum_of_disjoint_Union [encodable β] {f : β → set α} (hf₁ : ∀ i, measurable_set (f i)) (hf₂ : pairwise (disjoint on f)) : has_sum (λ i, v (f i)) (v (⋃ i, f i)) := begin set g := λ i : ℕ, ⋃ (b : β) (H : b ∈ encodable.decode₂ β i), f b with hg, have hg₁ : ∀ i, measurable_set (g i), { exact λ _, measurable_set.Union (λ b, measurable_set.Union_Prop $ λ _, hf₁ b) }, have hg₂ : pairwise (disjoint on g), { exact encodable.Union_decode₂_disjoint_on hf₂ }, have := v.of_disjoint_Union_nat hg₁ hg₂, rw [hg, encodable.Union_decode₂] at this, have hg₃ : (λ (i : β), v (f i)) = (λ i, v (g (encodable.encode i))), { ext, rw hg, simp only, congr, ext y, simp only [exists_prop, mem_Union, option.mem_def], split, { intro hy, refine ⟨x, (encodable.decode₂_is_partial_inv _ _).2 rfl, hy⟩ }, { rintro ⟨b, hb₁, hb₂⟩, rw (encodable.decode₂_is_partial_inv _ _) at hb₁, rwa ← encodable.encode_injective hb₁ } }, rw [summable.has_sum_iff, this, ← tsum_Union_decode₂], { exact v.empty }, { rw hg₃, change summable ((λ i, v (g i)) ∘ encodable.encode), rw function.injective.summable_iff encodable.encode_injective, { exact (v.m_Union hg₁ hg₂).summable }, { intros x hx, convert v.empty, simp only [Union_eq_empty, option.mem_def, not_exists, mem_range] at ⊢ hx, intros i hi, exact false.elim ((hx i) ((encodable.decode₂_is_partial_inv _ _).1 hi)) } } end lemma of_disjoint_Union [encodable β] {f : β → set α} (hf₁ : ∀ i, measurable_set (f i)) (hf₂ : pairwise (disjoint on f)) : v (⋃ i, f i) = ∑' i, v (f i) := (has_sum_of_disjoint_Union hf₁ hf₂).tsum_eq.symm lemma of_union {A B : set α} (h : disjoint A B) (hA : measurable_set A) (hB : measurable_set B) : v (A ∪ B) = v A + v B := begin rw [union_eq_Union, of_disjoint_Union, tsum_fintype, fintype.sum_bool, cond, cond], exacts [λ b, bool.cases_on b hB hA, pairwise_disjoint_on_bool.2 h] end lemma of_add_of_diff {A B : set α} (hA : measurable_set A) (hB : measurable_set B) (h : A ⊆ B) : v A + v (B \ A) = v B := begin rw [← of_union disjoint_diff hA (hB.diff hA), union_diff_cancel h], apply_instance, end lemma of_diff {M : Type*} [add_comm_group M] [topological_space M] [t2_space M] {v : vector_measure α M} {A B : set α} (hA : measurable_set A) (hB : measurable_set B) (h : A ⊆ B) : v (B \ A) = v B - (v A) := begin rw [← of_add_of_diff hA hB h, add_sub_cancel'], apply_instance, end lemma of_diff_of_diff_eq_zero {A B : set α} (hA : measurable_set A) (hB : measurable_set B) (h' : v (B \ A) = 0) : v (A \ B) + v B = v A := begin symmetry, calc v A = v (A \ B ∪ A ∩ B) : by simp only [set.diff_union_inter] ... = v (A \ B) + v (A ∩ B) : by { rw of_union, { rw disjoint.comm, exact set.disjoint_of_subset_left (A.inter_subset_right B) set.disjoint_diff }, { exact hA.diff hB }, { exact hA.inter hB } } ... = v (A \ B) + v (A ∩ B ∪ B \ A) : by { rw [of_union, h', add_zero], { exact set.disjoint_of_subset_left (A.inter_subset_left B) set.disjoint_diff }, { exact hA.inter hB }, { exact hB.diff hA } } ... = v (A \ B) + v B : by { rw [set.union_comm, set.inter_comm, set.diff_union_inter] } end lemma of_Union_nonneg {M : Type*} [topological_space M] [ordered_add_comm_monoid M] [order_closed_topology M] {v : vector_measure α M} (hf₁ : ∀ i, measurable_set (f i)) (hf₂ : pairwise (disjoint on f)) (hf₃ : ∀ i, 0 ≤ v (f i)) : 0 ≤ v (⋃ i, f i) := (v.of_disjoint_Union_nat hf₁ hf₂).symm ▸ tsum_nonneg hf₃ lemma of_Union_nonpos {M : Type*} [topological_space M] [ordered_add_comm_monoid M] [order_closed_topology M] {v : vector_measure α M} (hf₁ : ∀ i, measurable_set (f i)) (hf₂ : pairwise (disjoint on f)) (hf₃ : ∀ i, v (f i) ≤ 0) : v (⋃ i, f i) ≤ 0 := (v.of_disjoint_Union_nat hf₁ hf₂).symm ▸ tsum_nonpos hf₃ lemma of_nonneg_disjoint_union_eq_zero {s : signed_measure α} {A B : set α} (h : disjoint A B) (hA₁ : measurable_set A) (hB₁ : measurable_set B) (hA₂ : 0 ≤ s A) (hB₂ : 0 ≤ s B) (hAB : s (A ∪ B) = 0) : s A = 0 := begin rw of_union h hA₁ hB₁ at hAB, linarith, apply_instance, end lemma of_nonpos_disjoint_union_eq_zero {s : signed_measure α} {A B : set α} (h : disjoint A B) (hA₁ : measurable_set A) (hB₁ : measurable_set B) (hA₂ : s A ≤ 0) (hB₂ : s B ≤ 0) (hAB : s (A ∪ B) = 0) : s A = 0 := begin rw of_union h hA₁ hB₁ at hAB, linarith, apply_instance, end end section add_comm_monoid variables {M : Type*} [add_comm_monoid M] [topological_space M] instance : has_zero (vector_measure α M) := ⟨⟨0, rfl, λ _ _, rfl, λ _ _ _, has_sum_zero⟩⟩ instance : inhabited (vector_measure α M) := ⟨0⟩ @[simp] lemma coe_zero : ⇑(0 : vector_measure α M) = 0 := rfl lemma zero_apply (i : set α) : (0 : vector_measure α M) i = 0 := rfl variables [has_continuous_add M] /-- The sum of two vector measure is a vector measure. -/ def add (v w : vector_measure α M) : vector_measure α M := { measure_of' := v + w, empty' := by simp, not_measurable' := λ _ hi, by simp [v.not_measurable hi, w.not_measurable hi], m_Union' := λ f hf₁ hf₂, has_sum.add (v.m_Union hf₁ hf₂) (w.m_Union hf₁ hf₂) } instance : has_add (vector_measure α M) := ⟨add⟩ @[simp] lemma coe_add (v w : vector_measure α M) : ⇑(v + w) = v + w := rfl lemma add_apply (v w : vector_measure α M) (i : set α) :(v + w) i = v i + w i := rfl instance : add_comm_monoid (vector_measure α M) := function.injective.add_comm_monoid _ coe_injective coe_zero coe_add /-- `coe_fn` is an `add_monoid_hom`. -/ @[simps] def coe_fn_add_monoid_hom : vector_measure α M →+ (set α → M) := { to_fun := coe_fn, map_zero' := coe_zero, map_add' := coe_add } end add_comm_monoid section add_comm_group variables {M : Type*} [add_comm_group M] [topological_space M] variables [topological_add_group M] /-- The negative of a vector measure is a vector measure. -/ def neg (v : vector_measure α M) : vector_measure α M := { measure_of' := -v, empty' := by simp, not_measurable' := λ _ hi, by simp [v.not_measurable hi], m_Union' := λ f hf₁ hf₂, has_sum.neg $ v.m_Union hf₁ hf₂ } instance : has_neg (vector_measure α M) := ⟨neg⟩ @[simp] lemma coe_neg (v : vector_measure α M) : ⇑(-v) = - v := rfl lemma neg_apply (v : vector_measure α M) (i : set α) :(-v) i = - v i := rfl /-- The difference of two vector measure is a vector measure. -/ def sub (v w : vector_measure α M) : vector_measure α M := { measure_of' := v - w, empty' := by simp, not_measurable' := λ _ hi, by simp [v.not_measurable hi, w.not_measurable hi], m_Union' := λ f hf₁ hf₂, has_sum.sub (v.m_Union hf₁ hf₂) (w.m_Union hf₁ hf₂) } instance : has_sub (vector_measure α M) := ⟨sub⟩ @[simp] lemma coe_sub (v w : vector_measure α M) : ⇑(v - w) = v - w := rfl lemma sub_apply (v w : vector_measure α M) (i : set α) : (v - w) i = v i - w i := rfl instance : add_comm_group (vector_measure α M) := function.injective.add_comm_group _ coe_injective coe_zero coe_add coe_neg coe_sub end add_comm_group section distrib_mul_action variables {M : Type*} [add_comm_monoid M] [topological_space M] variables {R : Type*} [semiring R] [distrib_mul_action R M] variables [topological_space R] [has_continuous_smul R M] /-- Given a real number `r` and a signed measure `s`, `smul r s` is the signed measure corresponding to the function `r • s`. -/ def smul (r : R) (v : vector_measure α M) : vector_measure α M := { measure_of' := r • v, empty' := by rw [pi.smul_apply, empty, smul_zero], not_measurable' := λ _ hi, by rw [pi.smul_apply, v.not_measurable hi, smul_zero], m_Union' := λ _ hf₁ hf₂, has_sum.smul (v.m_Union hf₁ hf₂) } instance : has_scalar R (vector_measure α M) := ⟨smul⟩ @[simp] lemma coe_smul (r : R) (v : vector_measure α M) : ⇑(r • v) = r • v := rfl lemma smul_apply (r : R) (v : vector_measure α M) (i : set α) : (r • v) i = r • v i := rfl instance [has_continuous_add M] : distrib_mul_action R (vector_measure α M) := function.injective.distrib_mul_action coe_fn_add_monoid_hom coe_injective coe_smul end distrib_mul_action section module variables {M : Type*} [add_comm_monoid M] [topological_space M] variables {R : Type*} [semiring R] [module R M] variables [topological_space R] [has_continuous_smul R M] instance [has_continuous_add M] : module R (vector_measure α M) := function.injective.module R coe_fn_add_monoid_hom coe_injective coe_smul end module end vector_measure namespace measure /-- A finite measure coerced into a real function is a signed measure. -/ @[simps] def to_signed_measure (μ : measure α) [hμ : finite_measure μ] : signed_measure α := { measure_of' := λ i : set α, if measurable_set i then (μ.measure_of i).to_real else 0, empty' := by simp [μ.empty], not_measurable' := λ _ hi, if_neg hi, m_Union' := begin intros _ hf₁ hf₂, rw [μ.m_Union hf₁ hf₂, ennreal.tsum_to_real_eq, if_pos (measurable_set.Union hf₁), summable.has_sum_iff], { congr, ext n, rw if_pos (hf₁ n) }, { refine @summable_of_nonneg_of_le _ (ennreal.to_real ∘ μ ∘ f) _ _ _ _, { intro, split_ifs, exacts [ennreal.to_real_nonneg, le_refl _] }, { intro, split_ifs, exacts [le_refl _, ennreal.to_real_nonneg] }, exact summable_measure_to_real hf₁ hf₂ }, { intros a ha, apply ne_of_lt hμ.measure_univ_lt_top, rw [eq_top_iff, ← ha, outer_measure.measure_of_eq_coe, coe_to_outer_measure], exact measure_mono (set.subset_univ _) } end } lemma to_signed_measure_apply_measurable {μ : measure α} [finite_measure μ] {i : set α} (hi : measurable_set i) : μ.to_signed_measure i = (μ i).to_real := if_pos hi lemma to_signed_measure_eq_to_signed_measure_iff {μ ν : measure α} [finite_measure μ] [finite_measure ν] : μ.to_signed_measure = ν.to_signed_measure ↔ μ = ν := begin refine ⟨λ h, _, λ h, _⟩, { ext1 i hi, have : μ.to_signed_measure i = ν.to_signed_measure i, { rw h }, rwa [to_signed_measure_apply_measurable hi, to_signed_measure_apply_measurable hi, ennreal.to_real_eq_to_real] at this; { exact measure_lt_top _ _ } }, { congr, assumption } end @[simp] lemma to_signed_measure_zero : (0 : measure α).to_signed_measure = 0 := by { ext i hi, simp } @[simp] lemma to_signed_measure_add (μ ν : measure α) [finite_measure μ] [finite_measure ν] : (μ + ν).to_signed_measure = μ.to_signed_measure + ν.to_signed_measure := begin ext i hi, rw [to_signed_measure_apply_measurable hi, add_apply, ennreal.to_real_add (ne_of_lt (measure_lt_top _ _ )) (ne_of_lt (measure_lt_top _ _)), vector_measure.add_apply, to_signed_measure_apply_measurable hi, to_signed_measure_apply_measurable hi], all_goals { apply_instance } end @[simp] lemma to_signed_measure_smul (μ : measure α) [finite_measure μ] (r : ℝ≥0) : (r • μ).to_signed_measure = r • μ.to_signed_measure := begin ext i hi, rw [to_signed_measure_apply_measurable hi, vector_measure.smul_apply, to_signed_measure_apply_measurable hi, coe_nnreal_smul, pi.smul_apply, ennreal.to_real_smul], end /-- A measure is a vector measure over `ℝ≥0∞`. -/ @[simps] def to_ennreal_vector_measure (μ : measure α) : vector_measure α ℝ≥0∞ := { measure_of' := λ i : set α, if measurable_set i then μ i else 0, empty' := by simp [μ.empty], not_measurable' := λ _ hi, if_neg hi, m_Union' := λ _ hf₁ hf₂, begin rw summable.has_sum_iff ennreal.summable, { rw [if_pos (measurable_set.Union hf₁), measure_theory.measure_Union hf₂ hf₁], exact tsum_congr (λ n, if_pos (hf₁ n)) }, end } lemma to_ennreal_vector_measure_apply_measurable {μ : measure α} {i : set α} (hi : measurable_set i) : μ.to_ennreal_vector_measure i = μ i := if_pos hi @[simp] lemma to_ennreal_vector_measure_zero : (0 : measure α).to_ennreal_vector_measure = 0 := by { ext i hi, simp } @[simp] lemma to_ennreal_vector_measure_add (μ ν : measure α) : (μ + ν).to_ennreal_vector_measure = μ.to_ennreal_vector_measure + ν.to_ennreal_vector_measure := begin refine measure_theory.vector_measure.ext (λ i hi, _), rw [to_ennreal_vector_measure_apply_measurable hi, add_apply, vector_measure.add_apply, to_ennreal_vector_measure_apply_measurable hi, to_ennreal_vector_measure_apply_measurable hi] end lemma to_signed_measure_sub_apply {μ ν : measure α} [finite_measure μ] [finite_measure ν] {i : set α} (hi : measurable_set i) : (μ.to_signed_measure - ν.to_signed_measure) i = (μ i).to_real - (ν i).to_real := begin rw [vector_measure.sub_apply, to_signed_measure_apply_measurable hi, measure.to_signed_measure_apply_measurable hi, sub_eq_add_neg] end end measure namespace vector_measure section variables [measurable_space β] variables {M : Type*} [add_comm_monoid M] [topological_space M] variables (v : vector_measure α M) /-- The pushforward of a vector measure along a function. -/ def map (v : vector_measure α M) (f : α → β) : vector_measure β M := if hf : measurable f then { measure_of' := λ s, if measurable_set s then v (f ⁻¹' s) else 0, empty' := by simp, not_measurable' := λ i hi, if_neg hi, m_Union' := begin intros g hg₁ hg₂, convert v.m_Union (λ i, hf (hg₁ i)) (λ i j hij x hx, hg₂ i j hij hx), { ext i, rw if_pos (hg₁ i) }, { rw [preimage_Union, if_pos (measurable_set.Union hg₁)] } end } else 0 lemma map_apply {f : α → β} (hf : measurable f) {s : set β} (hs : measurable_set s) : v.map f s = v (f ⁻¹' s) := by { rw [map, dif_pos hf], exact if_pos hs } @[simp] lemma map_id : v.map id = v := ext (λ i hi, by rw [map_apply v measurable_id hi, preimage_id]) @[simp] lemma map_zero (f : α → β) : (0 : vector_measure α M).map f = 0 := begin by_cases hf : measurable f, { ext i hi, rw [map_apply _ hf hi, zero_apply, zero_apply] }, { exact dif_neg hf } end /-- The restriction of a vector measure on some set. -/ def restrict (v : vector_measure α M) (i : set α) : vector_measure α M := if hi : measurable_set i then { measure_of' := λ s, if measurable_set s then v (s ∩ i) else 0, empty' := by simp, not_measurable' := λ i hi, if_neg hi, m_Union' := begin intros f hf₁ hf₂, convert v.m_Union (λ n, (hf₁ n).inter hi) (hf₂.mono $ λ i j, disjoint.mono inf_le_left inf_le_left), { ext n, rw if_pos (hf₁ n) }, { rw [Union_inter, if_pos (measurable_set.Union hf₁)] } end } else 0 lemma restrict_not_measurable {i : set α} (hi : ¬ measurable_set i) : v.restrict i = 0 := dif_neg hi lemma restrict_apply {i : set α} (hi : measurable_set i) {j : set α} (hj : measurable_set j) : v.restrict i j = v (j ∩ i) := by { rw [restrict, dif_pos hi], exact if_pos hj } lemma restrict_eq_self {i : set α} (hi : measurable_set i) {j : set α} (hj : measurable_set j) (hij : j ⊆ i) : v.restrict i j = v j := by rw [restrict_apply v hi hj, inter_eq_left_iff_subset.2 hij] @[simp] lemma restrict_empty : v.restrict ∅ = 0 := ext (λ i hi, by rw [restrict_apply v measurable_set.empty hi, inter_empty, v.empty, zero_apply]) @[simp] lemma restrict_univ : v.restrict univ = v := ext (λ i hi, by rw [restrict_apply v measurable_set.univ hi, inter_univ]) @[simp] lemma restrict_zero {i : set α} : (0 : vector_measure α M).restrict i = 0 := begin by_cases hi : measurable_set i, { ext j hj, rw [restrict_apply 0 hi hj], refl }, { exact dif_neg hi } end section has_continuous_add variables [has_continuous_add M] lemma map_add (v w : vector_measure α M) (f : α → β) : (v + w).map f = v.map f + w.map f := begin by_cases hf : measurable f, { ext i hi, simp [map_apply _ hf hi] }, { simp [map, dif_neg hf] } end /-- `vector_measure.map` as an additive monoid homomorphism. -/ @[simps] def map_gm (f : α → β) : vector_measure α M →+ vector_measure β M := { to_fun := λ v, v.map f, map_zero' := map_zero f, map_add' := λ _ _, map_add _ _ f } lemma restrict_add (v w : vector_measure α M) (i : set α) : (v + w).restrict i = v.restrict i + w.restrict i := begin by_cases hi : measurable_set i, { ext j hj, simp [restrict_apply _ hi hj] }, { simp [restrict_not_measurable _ hi] } end /--`vector_measure.restrict` as an additive monoid homomorphism. -/ @[simps] def restrict_gm (i : set α) : vector_measure α M →+ vector_measure α M := { to_fun := λ v, v.restrict i, map_zero' := restrict_zero, map_add' := λ _ _, restrict_add _ _ i } end has_continuous_add end section variables [measurable_space β] variables {M : Type*} [add_comm_monoid M] [topological_space M] variables {R : Type*} [semiring R] [distrib_mul_action R M] variables [topological_space R] [has_continuous_smul R M] @[simp] lemma map_smul {v : vector_measure α M} {f : α → β} (c : R) : (c • v).map f = c • v.map f := begin by_cases hf : measurable f, { ext i hi, simp [map_apply _ hf hi] }, { simp only [map, dif_neg hf], -- `smul_zero` does not work since we do not require `has_continuous_add` ext i hi, simp } end @[simp] lemma restrict_smul {v :vector_measure α M} {i : set α} (c : R) : (c • v).restrict i = c • v.restrict i := begin by_cases hi : measurable_set i, { ext j hj, simp [restrict_apply _ hi hj] }, { simp only [restrict_not_measurable _ hi], -- `smul_zero` does not work since we do not require `has_continuous_add` ext j hj, simp } end end section variables [measurable_space β] variables {M : Type*} [add_comm_monoid M] [topological_space M] variables {R : Type*} [semiring R] [module R M] variables [topological_space R] [has_continuous_smul R M] [has_continuous_add M] /-- `vector_measure.map` as a linear map. -/ @[simps] def mapₗ (f : α → β) : vector_measure α M →ₗ[R] vector_measure β M := { to_fun := λ v, v.map f, map_add' := λ _ _, map_add _ _ f, map_smul' := λ _ _, map_smul _ } /-- `vector_measure.restrict` as an additive monoid homomorphism. -/ @[simps] def restrictₗ (i : set α) : vector_measure α M →ₗ[R] vector_measure α M := { to_fun := λ v, v.restrict i, map_add' := λ _ _, restrict_add _ _ i, map_smul' := λ _ _, restrict_smul _ } end section variables {M : Type*} [topological_space M] [add_comm_monoid M] [partial_order M] /-- Vector measures over a partially ordered monoid is partially ordered. This definition is consistent with `measure.partial_order`. -/ instance : partial_order (vector_measure α M) := { le := λ v w, ∀ i, measurable_set i → v i ≤ w i, le_refl := λ v i hi, le_refl _, le_trans := λ u v w h₁ h₂ i hi, le_trans (h₁ i hi) (h₂ i hi), le_antisymm := λ v w h₁ h₂, ext (λ i hi, le_antisymm (h₁ i hi) (h₂ i hi)) } variables {u v w : vector_measure α M} lemma le_iff : v ≤ w ↔ ∀ i, measurable_set i → v i ≤ w i := iff.rfl lemma le_iff' : v ≤ w ↔ ∀ i, v i ≤ w i := begin refine ⟨λ h i, _, λ h i hi, h i⟩, by_cases hi : measurable_set i, { exact h i hi }, { rw [v.not_measurable hi, w.not_measurable hi] } end end localized "notation v ` ≤[`:50 i:50 `] `:0 w:50 := measure_theory.vector_measure.restrict v i ≤ measure_theory.vector_measure.restrict w i" in measure_theory section variables {M : Type*} [topological_space M] [add_comm_monoid M] [partial_order M] variables (v w : vector_measure α M) lemma restrict_le_restrict_iff {i : set α} (hi : measurable_set i) : v ≤[i] w ↔ ∀ ⦃j⦄, measurable_set j → j ⊆ i → v j ≤ w j := ⟨λ h j hj₁ hj₂, (restrict_eq_self v hi hj₁ hj₂) ▸ (restrict_eq_self w hi hj₁ hj₂) ▸ h j hj₁, λ h, le_iff.1 (λ j hj, (restrict_apply v hi hj).symm ▸ (restrict_apply w hi hj).symm ▸ h (hj.inter hi) (set.inter_subset_right j i))⟩ lemma subset_le_of_restrict_le_restrict {i : set α} (hi : measurable_set i) (hi₂ : v ≤[i] w) {j : set α} (hj : j ⊆ i) : v j ≤ w j := begin by_cases hj₁ : measurable_set j, { exact (restrict_le_restrict_iff _ _ hi).1 hi₂ hj₁ hj }, { rw [v.not_measurable hj₁, w.not_measurable hj₁] }, end lemma restrict_le_restrict_of_subset_le {i : set α} (h : ∀ ⦃j⦄, measurable_set j → j ⊆ i → v j ≤ w j) : v ≤[i] w := begin by_cases hi : measurable_set i, { exact (restrict_le_restrict_iff _ _ hi).2 h }, { rw [restrict_not_measurable v hi, restrict_not_measurable w hi], exact le_refl _ }, end lemma restrict_le_restrict_subset {i j : set α} (hi₁ : measurable_set i) (hi₂ : v ≤[i] w) (hij : j ⊆ i) : v ≤[j] w := restrict_le_restrict_of_subset_le v w (λ k hk₁ hk₂, subset_le_of_restrict_le_restrict v w hi₁ hi₂ (set.subset.trans hk₂ hij)) lemma le_restrict_empty : v ≤[∅] w := begin intros j hj, rw [restrict_empty, restrict_empty] end lemma le_restrict_univ_iff_le : v ≤[univ] w ↔ v ≤ w := begin split, { intros h s hs, have := h s hs, rwa [restrict_apply _ measurable_set.univ hs, inter_univ, restrict_apply _ measurable_set.univ hs, inter_univ] at this }, { intros h s hs, rw [restrict_apply _ measurable_set.univ hs, inter_univ, restrict_apply _ measurable_set.univ hs, inter_univ], exact h s hs } end end section variables {M : Type*} [topological_space M] [ordered_add_comm_group M] [topological_add_group M] variables (v w : vector_measure α M) lemma neg_le_neg {i : set α} (hi : measurable_set i) (h : v ≤[i] w) : -w ≤[i] -v := begin intros j hj₁, rw [restrict_apply _ hi hj₁, restrict_apply _ hi hj₁, neg_apply, neg_apply], refine neg_le_neg _, rw [← restrict_apply _ hi hj₁, ← restrict_apply _ hi hj₁], exact h j hj₁, end @[simp] lemma neg_le_neg_iff {i : set α} (hi : measurable_set i) : -w ≤[i] -v ↔ v ≤[i] w := ⟨λ h, neg_neg v ▸ neg_neg w ▸ neg_le_neg _ _ hi h, λ h, neg_le_neg _ _ hi h⟩ end section variables {M : Type*} [topological_space M] [ordered_add_comm_monoid M] [order_closed_topology M] variables (v w : vector_measure α M) {i j : set α} lemma restrict_le_restrict_Union {f : ℕ → set α} (hf₁ : ∀ n, measurable_set (f n)) (hf₂ : ∀ n, v ≤[f n] w) : v ≤[⋃ n, f n] w := begin refine restrict_le_restrict_of_subset_le v w (λ a ha₁ ha₂, _), have ha₃ : (⋃ n, a ∩ disjointed f n) = a, { rwa [← inter_Union, Union_disjointed, inter_eq_left_iff_subset] }, have ha₄ : pairwise (disjoint on (λ n, a ∩ disjointed f n)), { exact (disjoint_disjointed _).mono (λ i j, disjoint.mono inf_le_right inf_le_right) }, rw [← ha₃, v.of_disjoint_Union_nat _ ha₄, w.of_disjoint_Union_nat _ ha₄], refine tsum_le_tsum (λ n, (restrict_le_restrict_iff v w (hf₁ n)).1 (hf₂ n) _ _) _ _, { exact (ha₁.inter (measurable_set.disjointed hf₁ n)) }, { exact set.subset.trans (set.inter_subset_right _ _) (disjointed_subset _ _) }, { refine (v.m_Union (λ n, _) _).summable, { exact ha₁.inter (measurable_set.disjointed hf₁ n) }, { exact (disjoint_disjointed _).mono (λ i j, disjoint.mono inf_le_right inf_le_right) } }, { refine (w.m_Union (λ n, _) _).summable, { exact ha₁.inter (measurable_set.disjointed hf₁ n) }, { exact (disjoint_disjointed _).mono (λ i j, disjoint.mono inf_le_right inf_le_right) } }, { intro n, exact (ha₁.inter (measurable_set.disjointed hf₁ n)) }, { exact λ n, ha₁.inter (measurable_set.disjointed hf₁ n) } end lemma restrict_le_restrict_encodable_Union [encodable β] {f : β → set α} (hf₁ : ∀ b, measurable_set (f b)) (hf₂ : ∀ b, v ≤[f b] w) : v ≤[⋃ b, f b] w := begin rw ← encodable.Union_decode₂, refine restrict_le_restrict_Union v w _ _, { intro n, measurability }, { intro n, cases encodable.decode₂ β n with b, { simp }, { simp [hf₂ b] } } end lemma restrict_le_restrict_union (hi₁ : measurable_set i) (hi₂ : v ≤[i] w) (hj₁ : measurable_set j) (hj₂ : v ≤[j] w) : v ≤[i ∪ j] w := begin rw union_eq_Union, refine restrict_le_restrict_encodable_Union v w _ _, { measurability }, { rintro (_ | _); simpa } end end section variables {M : Type*} [topological_space M] [ordered_add_comm_monoid M] variables (v w : vector_measure α M) {i j : set α} lemma nonneg_of_zero_le_restrict (hi₂ : 0 ≤[i] v) : 0 ≤ v i := begin by_cases hi₁ : measurable_set i, { exact (restrict_le_restrict_iff _ _ hi₁).1 hi₂ hi₁ set.subset.rfl }, { rw v.not_measurable hi₁ }, end lemma nonpos_of_restrict_le_zero (hi₂ : v ≤[i] 0) : v i ≤ 0 := begin by_cases hi₁ : measurable_set i, { exact (restrict_le_restrict_iff _ _ hi₁).1 hi₂ hi₁ set.subset.rfl }, { rw v.not_measurable hi₁ } end lemma zero_le_restrict_not_measurable (hi : ¬ measurable_set i) : 0 ≤[i] v := begin rw [restrict_zero, restrict_not_measurable _ hi], exact le_refl _, end lemma restrict_le_zero_of_not_measurable (hi : ¬ measurable_set i) : v ≤[i] 0 := begin rw [restrict_zero, restrict_not_measurable _ hi], exact le_refl _, end lemma measurable_of_not_zero_le_restrict (hi : ¬ 0 ≤[i] v) : measurable_set i := not.imp_symm (zero_le_restrict_not_measurable _) hi lemma measurable_of_not_restrict_le_zero (hi : ¬ v ≤[i] 0) : measurable_set i := not.imp_symm (restrict_le_zero_of_not_measurable _) hi lemma zero_le_restrict_subset (hi₁ : measurable_set i) (hij : j ⊆ i) (hi₂ : 0 ≤[i] v): 0 ≤[j] v := restrict_le_restrict_of_subset_le _ _ (λ k hk₁ hk₂, (restrict_le_restrict_iff _ _ hi₁).1 hi₂ hk₁ (set.subset.trans hk₂ hij)) lemma restrict_le_zero_subset (hi₁ : measurable_set i) (hij : j ⊆ i) (hi₂ : v ≤[i] 0): v ≤[j] 0 := restrict_le_restrict_of_subset_le _ _ (λ k hk₁ hk₂, (restrict_le_restrict_iff _ _ hi₁).1 hi₂ hk₁ (set.subset.trans hk₂ hij)) end section variables {M : Type*} [topological_space M] [linear_ordered_add_comm_monoid M] variables (v w : vector_measure α M) {i j : set α} lemma exists_pos_measure_of_not_restrict_le_zero (hi : ¬ v ≤[i] 0) : ∃ j : set α, measurable_set j ∧ j ⊆ i ∧ 0 < v j := begin have hi₁ : measurable_set i := measurable_of_not_restrict_le_zero _ hi, rw [restrict_le_restrict_iff _ _ hi₁] at hi, push_neg at hi, obtain ⟨j, hj₁, hj₂, hj⟩ := hi, exact ⟨j, hj₁, hj₂, hj⟩, end end section variables {M : Type*} [topological_space M] [add_comm_monoid M] [partial_order M] [covariant_class M M (+) (≤)] [has_continuous_add M] instance covariant_add_le : covariant_class (vector_measure α M) (vector_measure α M) (+) (≤) := ⟨λ u v w h i hi, add_le_add_left (h i hi) _⟩ end end vector_measure namespace signed_measure open vector_measure open_locale measure_theory /-- The underlying function for `signed_measure.to_measure_of_zero_le`. -/ def to_measure_of_zero_le' (s : signed_measure α) (i : set α) (hi : 0 ≤[i] s) (j : set α) (hj : measurable_set j) : ℝ≥0∞ := @coe ℝ≥0 ℝ≥0∞ _ ⟨s.restrict i j, le_trans (by simp) (hi j hj)⟩ /-- Given a signed measure `s` and a positive measurable set `i`, `to_measure_of_zero_le` provides the measure, mapping measurable sets `j` to `s (i ∩ j)`. -/ def to_measure_of_zero_le (s : signed_measure α) (i : set α) (hi₁ : measurable_set i) (hi₂ : 0 ≤[i] s) : measure α := measure.of_measurable (s.to_measure_of_zero_le' i hi₂) (by { simp_rw [to_measure_of_zero_le', s.restrict_apply hi₁ measurable_set.empty, set.empty_inter i, s.empty], refl }) begin intros f hf₁ hf₂, have h₁ : ∀ n, measurable_set (i ∩ f n) := λ n, hi₁.inter (hf₁ n), have h₂ : pairwise (disjoint on λ (n : ℕ), i ∩ f n), { rintro n m hnm x ⟨⟨_, hx₁⟩, _, hx₂⟩, exact hf₂ n m hnm ⟨hx₁, hx₂⟩ }, simp only [to_measure_of_zero_le', s.restrict_apply hi₁ (measurable_set.Union hf₁), set.inter_comm, set.inter_Union, s.of_disjoint_Union_nat h₁ h₂, ennreal.some_eq_coe, id.def], have h : ∀ n, 0 ≤ s (i ∩ f n) := λ n, s.nonneg_of_zero_le_restrict (s.zero_le_restrict_subset hi₁ (inter_subset_left _ _) hi₂), rw [nnreal.coe_tsum_of_nonneg h, ennreal.coe_tsum], { refine tsum_congr (λ n, _), simp_rw [s.restrict_apply hi₁ (hf₁ n), set.inter_comm] }, { exact (nnreal.summable_coe_of_nonneg h).2 (s.m_Union h₁ h₂).summable } end variables (s : signed_measure α) {i j : set α} lemma to_measure_of_zero_le_apply (hi : 0 ≤[i] s) (hi₁ : measurable_set i) (hj₁ : measurable_set j) : s.to_measure_of_zero_le i hi₁ hi j = @coe ℝ≥0 ℝ≥0∞ _ ⟨s (i ∩ j), nonneg_of_zero_le_restrict s (zero_le_restrict_subset s hi₁ (set.inter_subset_left _ _) hi)⟩ := by simp_rw [to_measure_of_zero_le, measure.of_measurable_apply _ hj₁, to_measure_of_zero_le', s.restrict_apply hi₁ hj₁, set.inter_comm] /-- Given a signed measure `s` and a negative measurable set `i`, `to_measure_of_le_zero` provides the measure, mapping measurable sets `j` to `-s (i ∩ j)`. -/ def to_measure_of_le_zero (s : signed_measure α) (i : set α) (hi₁ : measurable_set i) (hi₂ : s ≤[i] 0) : measure α := to_measure_of_zero_le (-s) i hi₁ $ (@neg_zero (vector_measure α ℝ) _) ▸ neg_le_neg _ _ hi₁ hi₂ lemma to_measure_of_le_zero_apply (hi : s ≤[i] 0) (hi₁ : measurable_set i) (hj₁ : measurable_set j) : s.to_measure_of_le_zero i hi₁ hi j = @coe ℝ≥0 ℝ≥0∞ _ ⟨-s (i ∩ j), neg_apply s (i ∩ j) ▸ nonneg_of_zero_le_restrict _ (zero_le_restrict_subset _ hi₁ (set.inter_subset_left _ _) ((@neg_zero (vector_measure α ℝ) _) ▸ neg_le_neg _ _ hi₁ hi))⟩ := begin erw [to_measure_of_zero_le_apply], { simp }, { assumption }, end /-- `signed_measure.to_measure_of_zero_le` is a finite measure. -/ instance to_measure_of_zero_le_finite (hi : 0 ≤[i] s) (hi₁ : measurable_set i) : finite_measure (s.to_measure_of_zero_le i hi₁ hi) := { measure_univ_lt_top := begin rw [to_measure_of_zero_le_apply s hi hi₁ measurable_set.univ], exact ennreal.coe_lt_top, end } /-- `signed_measure.to_measure_of_le_zero` is a finite measure. -/ instance to_measure_of_le_zero_finite (hi : s ≤[i] 0) (hi₁ : measurable_set i) : finite_measure (s.to_measure_of_le_zero i hi₁ hi) := { measure_univ_lt_top := begin rw [to_measure_of_le_zero_apply s hi hi₁ measurable_set.univ], exact ennreal.coe_lt_top, end } lemma to_measure_of_zero_le_to_signed_measure (hs : 0 ≤[univ] s) : (s.to_measure_of_zero_le univ measurable_set.univ hs).to_signed_measure = s := begin ext i hi, simp [measure.to_signed_measure_apply_measurable hi, to_measure_of_zero_le_apply _ _ _ hi], end lemma to_measure_of_le_zero_to_signed_measure (hs : s ≤[univ] 0) : (s.to_measure_of_le_zero univ measurable_set.univ hs).to_signed_measure = -s := begin ext i hi, simp [measure.to_signed_measure_apply_measurable hi, to_measure_of_le_zero_apply _ _ _ hi], end end signed_measure namespace measure open vector_measure variables (μ : measure α) [finite_measure μ] lemma zero_le_to_signed_measure : 0 ≤ μ.to_signed_measure := begin rw ← le_restrict_univ_iff_le, refine restrict_le_restrict_of_subset_le _ _ (λ j hj₁ _, _), simp only [measure.to_signed_measure_apply_measurable hj₁, coe_zero, pi.zero_apply, ennreal.to_real_nonneg, vector_measure.coe_zero] end lemma to_signed_measure_to_measure_of_zero_le : μ.to_signed_measure.to_measure_of_zero_le univ measurable_set.univ ((le_restrict_univ_iff_le _ _).2 (zero_le_to_signed_measure μ)) = μ := begin refine measure.ext (λ i hi, _), lift μ i to ℝ≥0 using (measure_lt_top _ _).ne with m hm, simp [signed_measure.to_measure_of_zero_le_apply _ _ _ hi, measure.to_signed_measure_apply_measurable hi, ← hm], end end measure end measure_theory
5a9ac7a987c0dba17cfe431ed5dddb50c68ccbcb
d26814d9437130e14d6d016c92d8c436b6dc62f3
/my_prelude.hlean
97221815aa32c776faece44b3cb527311b460626
[]
no_license
jonas-frey/segal
92fb5a556c164d4e9e864f2da1258be5a6482af8
64b4ec62ec5f293b781d5bbfea5a6b0997558c13
refs/heads/master
1,594,256,935,764
1,566,490,351,000
1,566,490,351,000
203,831,654
0
0
null
null
null
null
UTF-8
Lean
false
false
7,105
hlean
import prop_trunc types.prod types.fin hit.pushout types.fiber open funext eq trunc is_trunc prod sum pi function is_equiv sigma sigma.ops equiv nat eq equiv universe variables u v notation `↑` := eq_of_homotopy -- type \u notation `⇑` := eq_of_homotopy2 -- type \u notation `⤊` := eq_of_homotopy3 -- what to type? -- backwards transport definition transport' [subst] [reducible] [unfold 5] {A} (P : A → Type) {x y : A} (p : x = y) (u : P y) : P x := by induction p; exact u reserve infixl ` ► `:74 reserve infixr ` ◄ `:74 infix ` ► ` := λ x p, transport _ p x infixr ` ◄ ` := transport' _ definition pa {A} {B : A → Type} {f g : Π a, B a} (p : f = g) (a : A) : f a = g a := by induction p; reflexivity definition hwhisker_right' {A B C} {g g' : B → C} (H : g ~ g') (f : A → B) : g ∘ f ~ g' ∘ f := λa, H (f a) infix ` ▻ `:76 := pa -- type \t4 infixr ` ◅ `:76 := ap -- type \t8 infixr ` ◁ `:77 := hwhisker_left -- type \Tw infix ` ▷ `:77 := hwhisker_right' -- type \Tw definition ap_const {A B} {a a' : A} {b : B} (p : a = a') : (λ a, b) ◅ p = rfl := eq.rec rfl p definition path_lift {A} {B : A → Type} {a a' : A} (p : a = a') (b : B a) : b ► p =[p] b := by induction p; constructor definition path_lift' {A} {B : A → Type} {a a' : A} (p : a = a') (b' : B a') : b' =[p] p ◄ b' := by induction p; constructor definition po_of_eq {A} {B : A → Type} {a: A} {b b' : B a} (p : b = b') : b =[refl a] b' := begin induction p, constructor end -- pathovers of paths are paths between compositions definition po_path_left {A B} {a a' : A} {b : B} {m : A → B} (p : a = a') (q : m a = b) (q' : m a' = b) : q = m ◅ p ⬝ q' → q =[p] q' := begin induction p, λ r, po_of_eq (r ⬝ !idp_con) end definition po_path_right {A B} {a a' : A} {b : B} {m : A → B} (p : a = a') (q : b = m a) (q' : b = m a') : q ⬝ m ◅ p = q' → q =[p] q' := begin induction p, λr, po_of_eq r end definition po_path {A B} {a a' : A} {m n : A → B} (p : a = a') (q : m a = n a) (q' : m a' = n a') : q ⬝ n ◅ p = m ◅ p ⬝ q' → q =[p] q' := begin induction p, intro H, apply po_of_eq, calc q = q ⬝ rfl : con_idp ... = q ⬝ n ◅ rfl : concat q ◅ !ap_idp ... = m ◅ rfl ⬝ q' : H ... = rfl ⬝ q' : (λ r, concat r q') ◅ !ap_idp ... = q' : idp_con end definition pathover_of_homotopy {A B} {C : A → B → Type} {a a' : A} (p : a = a')(f : Π b, C a b)(f' : Π b, C a' b) : (Π b, f b =[p] f' b) → f=[p] f' := begin intro H,induction p, apply po_of_eq, apply eq_of_homotopy, intro b, apply @eq_of_pathover_idp _ _ a, apply H end definition nfext0 {A : Type} {B : A → Type} {f g : Π a, B a} (p q : f ~ g) : (Π a, p a = q a) → p = q := λH, ↑H definition nfext {A : Type} {B : A → Type} {f g : Π a, B a} (p q : f = g) : (Π a, p ▻ a = q ▻ a) → p = q := begin intro H, note z := nfext0 (apd10 p) (apd10 q), assert K : apd10 p = apd10 q → p = q, intro, refine _ ⬝ is_equiv.left_inv apd10 q, symmetry, refine _ ⬝ is_equiv.left_inv apd10 p, symmetry, exact ap _ a, apply K, clear K, apply z, intro a, apply H end definition aux2 {A} {B : A → Type} {f g : Π a, B a} (H : f ~ g) (a : A) : ↑H ▻ a = H a := right_inv apd10 H ▻ a definition apap {A B : Type} (f : A → B) {x y : A} {p q : x = y} : p = q → f ◅ p = f ◅ q := begin intro, apply ap (ap f), assumption end infix ` ◅◅ `:76 := apap -- type \t8 section -- flipping equalities between compositions variables {A : Type} {a0 a1 a2 : A} {p2 : a0 = a1} {p0 : a1 = a2} {p1 : a0 = a2} definition frri : p2 = p1 ⬝ p0⁻¹ → p2 ⬝ p0 = p1 := begin induction p0, exact id end definition frli : p0 = p2⁻¹ ⬝ p1 → p2 ⬝ p0 = p1 := begin induction p2, λa, !idp_con⬝a⬝!idp_con end definition flri : p1 ⬝ p0⁻¹ = p2 → p1 = p2 ⬝ p0 := begin induction p0, exact id end definition flli : p2⁻¹ ⬝ p1 = p0 → p1 = p2 ⬝ p0 := begin induction p2, λa, !idp_con⁻¹⬝a⬝!idp_con⁻¹ end definition flr : p2 ⬝ p0 = p1 → p2 = p1 ⬝ p0⁻¹ := begin induction p0, exact id end definition fll : p2 ⬝ p0 = p1 → p0 = p2⁻¹ ⬝ p1 := begin induction p2, λ a, !idp_con⁻¹⬝a⬝!idp_con⁻¹ end definition frr : p1 = p2 ⬝ p0 → p1 ⬝ p0⁻¹ = p2 := begin induction p0, exact id end definition frl : p1 = p2 ⬝ p0 → p2⁻¹ ⬝ p1 = p0 := begin induction p2, λa, !idp_con⬝a⬝!idp_con end end definition total_equiv (A : Type) (B C : A → Type) : (∀ a, B a ≃ C a) → sigma B ≃ sigma C := begin intro ef, fapply equiv.MK, { intro s, induction s with a b, exact dpair a (ef a b)}, { intro s, induction s with a c, exact dpair a ((ef a)⁻¹ c)}, { intro p, induction p, refine dpair _ ◅ _, apply right_inv}, { intro p, induction p, refine dpair _ ◅ _, apply left_inv}, end namespace trunc definition true : Prop.{0} := Prop.mk unit (is_trunc_succ unit -2) definition and : Prop.{u} → Prop.{v} → Prop.{max u v} := λ p q, Prop.mk (p × q) !is_trunc_prod notation A ∧ B := and A B end trunc namespace fin definition emb [coercion] {n : ℕ} (i : fin n) : fin (nat.succ n) := begin cases i with i p, fconstructor , exact i, apply nat.le_trans, exact p, apply le_succ end end fin lemma contr_point {A : Type} (b : is_contr A) : A := begin induction b with b, induction b with c ce, exact c, end lemma is_equiv_of_contr_fiber {A B : Type} (f : A → B) : (∀ b, is_contr (fiber f b)) → is_equiv f := begin intro H, fapply adjointify, intro b, exact fiber.point (contr_point (H b)), intro b, exact fiber.point_eq (contr_point (H b)), intro a, note z := H (f a), assert K : is_prop (fiber f (f a)), apply is_trunc_succ, assert L : contr_point (H (f a)) = fiber.mk a rfl, apply is_prop.elim, apply eq.ap fiber.point L end definition eq_of_feq {A B : Type} (f : A → B) [H : is_equiv f] {a a' : A} : f a = f a' → a = a' := (eq_equiv_fn_eq_of_is_equiv f a a')⁻¹ᶠ definition precompose [unfold_full] {A B} C (f : A → B) : (B → C) → (A → C) := λ h, h ∘ f definition postcompose [unfold_full] A {B C} (g : B → C) : (A → B) → (A → C) := λ h, g ∘ h definition is_equiv_precompose_of_is_equiv [instance] {A B C} (f : A → B) [H : is_equiv f] : is_equiv (precompose C f) := is_equiv.rec_on H $ λ g linv rinv adj, adjointify _ (precompose C g) (λ h, ↑(h ◁ rinv)) (λ k, ↑(k ◁ linv)) definition is_equiv_postcompose_of_is_equiv [instance] {A B C} (f : B → C) [H : is_equiv f] : is_equiv (postcompose A f) := is_equiv.rec_on H $ λ g linv rinv adj, adjointify (λh, f ∘ h) (λk, g ∘ k) (λh, ↑(linv ▷ h)) (λk, ↑(rinv ▷ k)) definition equiv_functionals_of_equiv {A B C} : (A ≃ B) → (A → C) ≃ (B → C) := λ e, equiv.mk (precompose C e⁻¹ᶠ) _ definition equiv_families_of_equiv {A B C} : (B ≃ C) → (A → B) ≃ (A → C) := λ e, equiv.mk (postcompose A e) _
d2d55362a8598476c5f22eec22c98a4cc4ffdde5
82e44445c70db0f03e30d7be725775f122d72f3e
/src/category_theory/monad/limits.lean
16210718bf8b744a89f6f0482bd7133bb87182cc
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
15,010
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta -/ import category_theory.monad.adjunction import category_theory.adjunction.limits import category_theory.limits.preserves.shapes.terminal /-! # Limits and colimits in the category of algebras This file shows that the forgetful functor `forget T : algebra T ⥤ C` for a monad `T : C ⥤ C` creates limits and creates any colimits which `T` preserves. This is used to show that `algebra T` has any limits which `C` has, and any colimits which `C` has and `T` preserves. This is generalised to the case of a monadic functor `D ⥤ C`. ## TODO Dualise for the category of coalgebras and comonadic left adjoints. -/ namespace category_theory open category open category_theory.limits universes v₁ v₂ u₁ u₂ -- morphism levels before object levels. See note [category_theory universes]. namespace monad variables {C : Type u₁} [category.{v₁} C] variables {T : monad C} variables {J : Type v₁} [small_category J] namespace forget_creates_limits variables (D : J ⥤ algebra T) (c : cone (D ⋙ T.forget)) (t : is_limit c) /-- (Impl) The natural transformation used to define the new cone -/ @[simps] def γ : (D ⋙ T.forget ⋙ ↑T) ⟶ D ⋙ T.forget := { app := λ j, (D.obj j).a } /-- (Impl) This new cone is used to construct the algebra structure -/ @[simps π_app] def new_cone : cone (D ⋙ forget T) := { X := T.obj c.X, π := (functor.const_comp _ _ ↑T).inv ≫ whisker_right c.π T ≫ γ D } /-- The algebra structure which will be the apex of the new limit cone for `D`. -/ @[simps] def cone_point : algebra T := { A := c.X, a := t.lift (new_cone D c), unit' := t.hom_ext $ λ j, begin rw [category.assoc, t.fac, new_cone_π_app, ←T.η.naturality_assoc, functor.id_map, (D.obj j).unit], dsimp, simp -- See library note [dsimp, simp] end, assoc' := t.hom_ext $ λ j, begin rw [category.assoc, category.assoc, t.fac (new_cone D c), new_cone_π_app, ←functor.map_comp_assoc, t.fac (new_cone D c), new_cone_π_app, ←T.μ.naturality_assoc, (D.obj j).assoc, functor.map_comp, category.assoc], refl, end } /-- (Impl) Construct the lifted cone in `algebra T` which will be limiting. -/ @[simps] def lifted_cone : cone D := { X := cone_point D c t, π := { app := λ j, { f := c.π.app j }, naturality' := λ X Y f, by { ext1, dsimp, erw c.w f, simp } } } /-- (Impl) Prove that the lifted cone is limiting. -/ @[simps] def lifted_cone_is_limit : is_limit (lifted_cone D c t) := { lift := λ s, { f := t.lift ((forget T).map_cone s), h' := t.hom_ext $ λ j, begin dsimp, rw [category.assoc, category.assoc, t.fac, new_cone_π_app, ←functor.map_comp_assoc, t.fac, functor.map_cone_π_app], apply (s.π.app j).h, end }, uniq' := λ s m J, begin ext1, apply t.hom_ext, intro j, simpa [t.fac ((forget T).map_cone s) j] using congr_arg algebra.hom.f (J j), end } end forget_creates_limits -- Theorem 5.6.5 from [Riehl][riehl2017] /-- The forgetful functor from the Eilenberg-Moore category creates limits. -/ noncomputable instance forget_creates_limits : creates_limits (forget T) := { creates_limits_of_shape := λ J 𝒥, by exactI { creates_limit := λ D, creates_limit_of_reflects_iso (λ c t, { lifted_cone := forget_creates_limits.lifted_cone D c t, valid_lift := cones.ext (iso.refl _) (λ j, (id_comp _).symm), makes_limit := forget_creates_limits.lifted_cone_is_limit _ _ _ } ) } } /-- `D ⋙ forget T` has a limit, then `D` has a limit. -/ lemma has_limit_of_comp_forget_has_limit (D : J ⥤ algebra T) [has_limit (D ⋙ forget T)] : has_limit D := has_limit_of_created D (forget T) namespace forget_creates_colimits -- Let's hide the implementation details in a namespace variables {D : J ⥤ algebra T} (c : cocone (D ⋙ forget T)) (t : is_colimit c) -- We have a diagram D of shape J in the category of algebras, and we assume that we are given a -- colimit for its image D ⋙ forget T under the forgetful functor, say its apex is L. -- We'll construct a colimiting coalgebra for D, whose carrier will also be L. -- To do this, we must find a map TL ⟶ L. Since T preserves colimits, TL is also a colimit. -- In particular, it is a colimit for the diagram `(D ⋙ forget T) ⋙ T` -- so to construct a map TL ⟶ L it suffices to show that L is the apex of a cocone for this diagram. -- In other words, we need a natural transformation from const L to `(D ⋙ forget T) ⋙ T`. -- But we already know that L is the apex of a cocone for the diagram `D ⋙ forget T`, so it -- suffices to give a natural transformation `((D ⋙ forget T) ⋙ T) ⟶ (D ⋙ forget T)`: /-- (Impl) The natural transformation given by the algebra structure maps, used to construct a cocone `c` with apex `colimit (D ⋙ forget T)`. -/ @[simps] def γ : ((D ⋙ forget T) ⋙ ↑T) ⟶ (D ⋙ forget T) := { app := λ j, (D.obj j).a } /-- (Impl) A cocone for the diagram `(D ⋙ forget T) ⋙ T` found by composing the natural transformation `γ` with the colimiting cocone for `D ⋙ forget T`. -/ @[simps] def new_cocone : cocone ((D ⋙ forget T) ⋙ ↑T) := { X := c.X, ι := γ ≫ c.ι } variables [preserves_colimit (D ⋙ forget T) (T : C ⥤ C)] /-- (Impl) Define the map `λ : TL ⟶ L`, which will serve as the structure of the coalgebra on `L`, and we will show is the colimiting object. We use the cocone constructed by `c` and the fact that `T` preserves colimits to produce this morphism. -/ @[reducible] def lambda : ((T : C ⥤ C).map_cocone c).X ⟶ c.X := (is_colimit_of_preserves _ t).desc (new_cocone c) /-- (Impl) The key property defining the map `λ : TL ⟶ L`. -/ lemma commuting (j : J) : (T : C ⥤ C).map (c.ι.app j) ≫ lambda c t = (D.obj j).a ≫ c.ι.app j := (is_colimit_of_preserves _ t).fac (new_cocone c) j variables [preserves_colimit ((D ⋙ forget T) ⋙ ↑T) (T : C ⥤ C)] /-- (Impl) Construct the colimiting algebra from the map `λ : TL ⟶ L` given by `lambda`. We are required to show it satisfies the two algebra laws, which follow from the algebra laws for the image of `D` and our `commuting` lemma. -/ @[simps] def cocone_point : algebra T := { A := c.X, a := lambda c t, unit' := begin apply t.hom_ext, intro j, rw [(show c.ι.app j ≫ T.η.app c.X ≫ _ = T.η.app (D.obj j).A ≫ _ ≫ _, from T.η.naturality_assoc _ _), commuting, algebra.unit_assoc (D.obj j)], dsimp, simp -- See library note [dsimp, simp] end, assoc' := begin refine (is_colimit_of_preserves _ (is_colimit_of_preserves _ t)).hom_ext (λ j, _), rw [functor.map_cocone_ι_app, functor.map_cocone_ι_app, (show (T : C ⥤ C).map ((T : C ⥤ C).map _) ≫ _ ≫ _ = _, from T.μ.naturality_assoc _ _), ←functor.map_comp_assoc, commuting, functor.map_comp, category.assoc, commuting], apply (D.obj j).assoc_assoc _, end } /-- (Impl) Construct the lifted cocone in `algebra T` which will be colimiting. -/ @[simps] def lifted_cocone : cocone D := { X := cocone_point c t, ι := { app := λ j, { f := c.ι.app j, h' := commuting _ _ _ }, naturality' := λ A B f, by { ext1, dsimp, rw [comp_id], apply c.w } } } /-- (Impl) Prove that the lifted cocone is colimiting. -/ @[simps] def lifted_cocone_is_colimit : is_colimit (lifted_cocone c t) := { desc := λ s, { f := t.desc ((forget T).map_cocone s), h' := (is_colimit_of_preserves (T : C ⥤ C) t).hom_ext $ λ j, begin dsimp, rw [←functor.map_comp_assoc, ←category.assoc, t.fac, commuting, category.assoc, t.fac], apply algebra.hom.h, end }, uniq' := λ s m J, by { ext1, apply t.hom_ext, intro j, simpa using congr_arg algebra.hom.f (J j) } } end forget_creates_colimits open forget_creates_colimits -- TODO: the converse of this is true as well /-- The forgetful functor from the Eilenberg-Moore category for a monad creates any colimit which the monad itself preserves. -/ noncomputable instance forget_creates_colimit (D : J ⥤ algebra T) [preserves_colimit (D ⋙ forget T) (T : C ⥤ C)] [preserves_colimit ((D ⋙ forget T) ⋙ ↑T) (T : C ⥤ C)] : creates_colimit D (forget T) := creates_colimit_of_reflects_iso $ λ c t, { lifted_cocone := { X := cocone_point c t, ι := { app := λ j, { f := c.ι.app j, h' := commuting _ _ _ }, naturality' := λ A B f, by { ext1, dsimp, erw [comp_id, c.w] } } }, valid_lift := cocones.ext (iso.refl _) (by tidy), makes_colimit := lifted_cocone_is_colimit _ _ } noncomputable instance forget_creates_colimits_of_shape [preserves_colimits_of_shape J (T : C ⥤ C)] : creates_colimits_of_shape J (forget T) := { creates_colimit := λ K, by apply_instance } noncomputable instance forget_creates_colimits [preserves_colimits (T : C ⥤ C)] : creates_colimits (forget T) := { creates_colimits_of_shape := λ J 𝒥₁, by apply_instance } /-- For `D : J ⥤ algebra T`, `D ⋙ forget T` has a colimit, then `D` has a colimit provided colimits of shape `J` are preserved by `T`. -/ lemma forget_creates_colimits_of_monad_preserves [preserves_colimits_of_shape J (T : C ⥤ C)] (D : J ⥤ algebra T) [has_colimit (D ⋙ forget T)] : has_colimit D := has_colimit_of_created D (forget T) end monad variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₁} D] variables {J : Type v₁} [small_category J] instance comp_comparison_forget_has_limit (F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit (F ⋙ R)] : has_limit ((F ⋙ monad.comparison (adjunction.of_right_adjoint R)) ⋙ monad.forget _) := @has_limit_of_iso _ _ _ _ (F ⋙ R) _ _ (iso_whisker_left F (monad.comparison_forget (adjunction.of_right_adjoint R)).symm) instance comp_comparison_has_limit (F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit (F ⋙ R)] : has_limit (F ⋙ monad.comparison (adjunction.of_right_adjoint R)) := monad.has_limit_of_comp_forget_has_limit (F ⋙ monad.comparison (adjunction.of_right_adjoint R)) /-- Any monadic functor creates limits. -/ noncomputable def monadic_creates_limits (R : D ⥤ C) [monadic_right_adjoint R] : creates_limits R := creates_limits_of_nat_iso (monad.comparison_forget (adjunction.of_right_adjoint R)) /-- The forgetful functor from the Eilenberg-Moore category for a monad creates any colimit which the monad itself preserves. -/ noncomputable def monadic_creates_colimit_of_preserves_colimit (R : D ⥤ C) (K : J ⥤ D) [monadic_right_adjoint R] [preserves_colimit (K ⋙ R) (left_adjoint R ⋙ R)] [preserves_colimit ((K ⋙ R) ⋙ left_adjoint R ⋙ R) (left_adjoint R ⋙ R)] : creates_colimit K R := begin apply creates_colimit_of_nat_iso (monad.comparison_forget (adjunction.of_right_adjoint R)), apply category_theory.comp_creates_colimit _ _, apply_instance, let i : ((K ⋙ monad.comparison (adjunction.of_right_adjoint R)) ⋙ monad.forget _) ≅ K ⋙ R := functor.associator _ _ _ ≪≫ iso_whisker_left K (monad.comparison_forget (adjunction.of_right_adjoint R)), apply category_theory.monad.forget_creates_colimit _, { dsimp, refine preserves_colimit_of_iso_diagram _ i.symm }, { dsimp, refine preserves_colimit_of_iso_diagram _ (iso_whisker_right i (left_adjoint R ⋙ R)).symm }, end /-- A monadic functor creates any colimits of shapes it preserves. -/ noncomputable def monadic_creates_colimits_of_shape_of_preserves_colimits_of_shape (R : D ⥤ C) [monadic_right_adjoint R] [preserves_colimits_of_shape J R] : creates_colimits_of_shape J R := begin have : preserves_colimits_of_shape J (left_adjoint R ⋙ R), { apply category_theory.limits.comp_preserves_colimits_of_shape _ _, { haveI := adjunction.left_adjoint_preserves_colimits (adjunction.of_right_adjoint R), apply_instance }, apply_instance }, exactI ⟨λ K, monadic_creates_colimit_of_preserves_colimit _ _⟩, end /-- A monadic functor creates colimits if it preserves colimits. -/ noncomputable def monadic_creates_colimits_of_preserves_colimits (R : D ⥤ C) [monadic_right_adjoint R] [preserves_colimits R] : creates_colimits R := { creates_colimits_of_shape := λ J 𝒥₁, by exactI monadic_creates_colimits_of_shape_of_preserves_colimits_of_shape _ } section lemma has_limit_of_reflective (F : J ⥤ D) (R : D ⥤ C) [has_limit (F ⋙ R)] [reflective R] : has_limit F := by { haveI := monadic_creates_limits R, exact has_limit_of_created F R } /-- If `C` has limits of shape `J` then any reflective subcategory has limits of shape `J`. -/ lemma has_limits_of_shape_of_reflective [has_limits_of_shape J C] (R : D ⥤ C) [reflective R] : has_limits_of_shape J D := { has_limit := λ F, has_limit_of_reflective F R } /-- If `C` has limits then any reflective subcategory has limits. -/ lemma has_limits_of_reflective (R : D ⥤ C) [has_limits C] [reflective R] : has_limits D := { has_limits_of_shape := λ J 𝒥₁, by exactI has_limits_of_shape_of_reflective R } /-- If `C` has colimits of shape `J` then any reflective subcategory has colimits of shape `J`. -/ lemma has_colimits_of_shape_of_reflective (R : D ⥤ C) [reflective R] [has_colimits_of_shape J C] : has_colimits_of_shape J D := { has_colimit := λ F, begin let c := (left_adjoint R).map_cocone (colimit.cocone (F ⋙ R)), letI := (adjunction.of_right_adjoint R).left_adjoint_preserves_colimits, let t : is_colimit c := is_colimit_of_preserves (left_adjoint R) (colimit.is_colimit _), apply has_colimit.mk ⟨_, (is_colimit.precompose_inv_equiv _ _).symm t⟩, apply (iso_whisker_left F (as_iso (adjunction.of_right_adjoint R).counit) : _) ≪≫ F.right_unitor, end } /-- If `C` has colimits then any reflective subcategory has colimits. -/ lemma has_colimits_of_reflective (R : D ⥤ C) [reflective R] [has_colimits C] : has_colimits D := { has_colimits_of_shape := λ J 𝒥, by exactI has_colimits_of_shape_of_reflective R } /-- The reflector always preserves terminal objects. Note this in general doesn't apply to any other limit. -/ noncomputable def left_adjoint_preserves_terminal_of_reflective (R : D ⥤ C) [reflective R] [has_terminal C] : preserves_limits_of_shape (discrete pempty) (left_adjoint R) := { preserves_limit := λ K, begin letI : has_terminal D := has_limits_of_shape_of_reflective R, letI := monadic_creates_limits R, letI := category_theory.preserves_limit_of_creates_limit_and_has_limit (functor.empty _) R, letI : preserves_limit (functor.empty _) (left_adjoint R), { apply preserves_terminal_of_iso, apply _ ≪≫ as_iso ((adjunction.of_right_adjoint R).counit.app (⊤_ D)), apply (left_adjoint R).map_iso (preserves_terminal.iso R).symm }, apply preserves_limit_of_iso_diagram (left_adjoint R) (functor.unique_from_empty _).symm, end } end end category_theory
184a5a4ebe87a60a6a1952ebc6546f2f03e272fb
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/fin_category.lean
11e88b62157beb97958f0fa38d8df8b2ec9a07f6
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
4,252
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 data.fintype.card import category_theory.discrete_category import category_theory.opposites import category_theory.category.ulift /-! # Finite categories A category is finite in this sense if it has finitely many objects, and finitely many morphisms. ## Implementation Prior to #14046, `fin_category` required a `decidable_eq` instance on the object and morphism types. This does not seem to have had any practical payoff (i.e. making some definition constructive) so we have removed these requirements to avoid having to supply instances or delay with non-defeq conflicts between instances. -/ universes w v u open_locale classical noncomputable theory namespace category_theory instance discrete_fintype {α : Type*} [fintype α] : fintype (discrete α) := fintype.of_equiv α (discrete_equiv.symm) instance discrete_hom_fintype {α : Type*} (X Y : discrete α) : fintype (X ⟶ Y) := by { apply ulift.fintype } /-- A category with a `fintype` of objects, and a `fintype` for each morphism space. -/ class fin_category (J : Type v) [small_category J] := (fintype_obj : fintype J . tactic.apply_instance) (fintype_hom : Π (j j' : J), fintype (j ⟶ j') . tactic.apply_instance) attribute [instance] fin_category.fintype_obj fin_category.fintype_hom instance fin_category_discrete_of_fintype (J : Type v) [fintype J] : fin_category (discrete J) := { } namespace fin_category variables (α : Type*) [fintype α] [small_category α] [fin_category α] /-- A fin_category `α` is equivalent to a category with objects in `Type`. -/ @[nolint unused_arguments] abbreviation obj_as_type : Type := induced_category α (fintype.equiv_fin α).symm /-- The constructed category is indeed equivalent to `α`. -/ noncomputable def obj_as_type_equiv : obj_as_type α ≌ α := (induced_functor (fintype.equiv_fin α).symm).as_equivalence /-- A fin_category `α` is equivalent to a fin_category with in `Type`. -/ @[nolint unused_arguments] abbreviation as_type : Type := fin (fintype.card α) @[simps hom id comp (lemmas_only)] noncomputable instance category_as_type : small_category (as_type α) := { hom := λ i j, fin (fintype.card (@quiver.hom (obj_as_type α) _ i j)), id := λ i, fintype.equiv_fin _ (𝟙 i), comp := λ i j k f g, fintype.equiv_fin _ ((fintype.equiv_fin _).symm f ≫ (fintype.equiv_fin _).symm g) } local attribute [simp] category_as_type_hom category_as_type_id category_as_type_comp /-- The "identity" functor from `as_type α` to `obj_as_type α`. -/ @[simps] noncomputable def as_type_to_obj_as_type : as_type α ⥤ obj_as_type α := { obj := id, map := λ i j, (fintype.equiv_fin _).symm } /-- The "identity" functor from `obj_as_type α` to `as_type α`. -/ @[simps] noncomputable def obj_as_type_to_as_type : obj_as_type α ⥤ as_type α := { obj := id, map := λ i j, fintype.equiv_fin _ } /-- The constructed category (`as_type α`) is equivalent to `obj_as_type α`. -/ noncomputable def as_type_equiv_obj_as_type : as_type α ≌ obj_as_type α := equivalence.mk (as_type_to_obj_as_type α) (obj_as_type_to_as_type α) (nat_iso.of_components iso.refl $ λ _ _ _, by { dsimp, simp }) (nat_iso.of_components iso.refl $ λ _ _ _, by { dsimp, simp }) noncomputable instance as_type_fin_category : fin_category (as_type α) := {} /-- The constructed category (`as_type α`) is indeed equivalent to `α`. -/ noncomputable def equiv_as_type : as_type α ≌ α := (as_type_equiv_obj_as_type α).trans (obj_as_type_equiv α) end fin_category open opposite /-- The opposite of a finite category is finite. -/ instance fin_category_opposite {J : Type v} [small_category J] [fin_category J] : fin_category Jᵒᵖ := { fintype_obj := fintype.of_equiv _ equiv_to_opposite, fintype_hom := λ j j', fintype.of_equiv _ (op_equiv j j').symm, } /-- Applying `ulift` to morphisms and objects of a category preserves finiteness. -/ instance fin_category_ulift {J : Type v} [small_category J] [fin_category J] : fin_category.{(max w v)} (ulift_hom.{w (max w v)} (ulift.{w v} J)) := { fintype_obj := ulift.fintype J } end category_theory
32cbaac38333a93f68586c4f9865c33ecb93b458
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Data/PersistentHashSet.lean
0cbcfacb0712ea253fead62f05473c90e8088382
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,709
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import Lean.Data.PersistentHashMap namespace Lean universe u v structure PersistentHashSet (α : Type u) [BEq α] [Hashable α] where (set : PersistentHashMap α Unit) abbrev PHashSet (α : Type u) [BEq α] [Hashable α] := PersistentHashSet α namespace PersistentHashSet @[inline] def empty [BEq α] [Hashable α] : PersistentHashSet α := { set := PersistentHashMap.empty } instance [BEq α] [Hashable α] : Inhabited (PersistentHashSet α) where default := empty instance [BEq α] [Hashable α] : EmptyCollection (PersistentHashSet α) := ⟨empty⟩ variable {_ : BEq α} {_ : Hashable α} @[inline] def isEmpty (s : PersistentHashSet α) : Bool := s.set.isEmpty @[inline] def insert (s : PersistentHashSet α) (a : α) : PersistentHashSet α := { set := s.set.insert a () } @[inline] def erase (s : PersistentHashSet α) (a : α) : PersistentHashSet α := { set := s.set.erase a } @[inline] def find? (s : PersistentHashSet α) (a : α) : Option α := match s.set.findEntry? a with | some (a, _) => some a | none => none @[inline] def contains (s : PersistentHashSet α) (a : α) : Bool := s.set.contains a @[inline] def size (s : PersistentHashSet α) : Nat := s.set.size @[inline] def foldM {β : Type v} {m : Type v → Type v} [Monad m] (f : β → α → m β) (init : β) (s : PersistentHashSet α) : m β := s.set.foldlM (init := init) fun d a _ => f d a @[inline] def fold {β : Type v} (f : β → α → β) (init : β) (s : PersistentHashSet α) : β := Id.run $ s.foldM f init
6e0db9e82f5e6349f33622e70b76cf1414d2d306
6e36ebd5594a0d512dea8bc6ffe78c71b5b5032d
/src/mywork/Practice/myLecture14.lean
731617ac037730dbbe29ec47ecf525ae333bc2d0
[]
no_license
wrw2ztk/cs2120f21
cdc4b1b4043c8ae8f3c8c3c0e91cdacb2cfddb16
f55df4c723d3ce989908679f5653e4be669334ae
refs/heads/main
1,691,764,473,342
1,633,707,809,000
1,633,707,809,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
498
lean
axioms (Person : Type) (Likes : Person → Person → Prop) example : ¬ (∀ p : Person, Likes p p ) ↔ ∃ p : Person, ¬ Likes p p := begin apply iff.intro, assume h, have f := classical.em (∃ (p : Person), ¬Likes p p), cases f, exact f, have contra : ∀ (p : Person), Likes p p := _, contradiction, assume p, have g := classical.em (Likes p p), cases g, exact g, have a : ∃(p : Person), ¬Likes p p := exists.intro p g, contradiction, end
e2095c13611be2ffb8c823aa1f62c107c3f2107b
94e33a31faa76775069b071adea97e86e218a8ee
/src/number_theory/legendre_symbol/gauss_eisenstein_lemmas.lean
bd60ea818edcfb50e3ff0351d4283c5f73b7fd71
[ "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
13,769
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import field_theory.finite.basic import data.zmod.basic /-! # Lemmas of Gauss and Eisenstein This file contains code for the proof of the Lemmas of Gauss and Eisenstein on the Legendre symbol. The main results are `gauss_lemma_aux` and `eisenstein_lemma_aux`; they are used in `quadratic_reciprocity.lean` to prove `gauss_lemma` and `eisenstein_lemma`, respectively. -/ open function finset nat finite_field zmod open_locale big_operators nat namespace zmod section wilson variables (p : ℕ) [fact p.prime] -- One can probably deduce the following from `finite_field.prod_univ_units_id_eq_neg_one` /-- **Wilson's Lemma**: the product of `1`, ..., `p-1` is `-1` modulo `p`. -/ @[simp] lemma wilsons_lemma : ((p - 1)! : zmod p) = -1 := begin refine calc ((p - 1)! : zmod p) = (∏ x in Ico 1 (succ (p - 1)), x) : by rw [← finset.prod_Ico_id_eq_factorial, prod_nat_cast] ... = (∏ x : (zmod p)ˣ, x) : _ ... = -1 : by simp_rw [← units.coe_hom_apply, ← (units.coe_hom (zmod p)).map_prod, prod_univ_units_id_eq_neg_one, units.coe_hom_apply, units.coe_neg, units.coe_one], have hp : 0 < p := (fact.out p.prime).pos, symmetry, refine prod_bij (λ a _, (a : zmod p).val) _ _ _ _, { intros a ha, rw [mem_Ico, ← nat.succ_sub hp, nat.succ_sub_one], split, { apply nat.pos_of_ne_zero, rw ← @val_zero p, assume h, apply units.ne_zero a (val_injective p h) }, { exact val_lt _ } }, { intros a ha, simp only [cast_id, nat_cast_val], }, { intros _ _ _ _ h, rw units.ext_iff, exact val_injective p h }, { intros b hb, rw [mem_Ico, nat.succ_le_iff, ← succ_sub hp, succ_sub_one, pos_iff_ne_zero] at hb, refine ⟨units.mk0 b _, finset.mem_univ _, _⟩, { assume h, apply hb.1, apply_fun val at h, simpa only [val_cast_of_lt hb.right, val_zero] using h }, { simp only [val_cast_of_lt hb.right, units.coe_mk0], } } end @[simp] lemma prod_Ico_one_prime : (∏ x in Ico 1 p, (x : zmod p)) = -1 := begin conv in (Ico 1 p) { rw [← succ_sub_one p, succ_sub (fact.out p.prime).pos] }, rw [← prod_nat_cast, finset.prod_Ico_id_eq_factorial, wilsons_lemma] end end wilson end zmod section gauss_eisenstein namespace legendre_symbol /-- The image of the map sending a non zero natural number `x ≤ p / 2` to the absolute value of the element of interger in the interval `(-p/2, p/2]` congruent to `a * x` mod p is the set of non zero natural numbers `x` such that `x ≤ p / 2` -/ lemma Ico_map_val_min_abs_nat_abs_eq_Ico_map_id (p : ℕ) [hp : fact p.prime] (a : zmod p) (hap : a ≠ 0) : (Ico 1 (p / 2).succ).1.map (λ x, (a * x).val_min_abs.nat_abs) = (Ico 1 (p / 2).succ).1.map (λ a, a) := begin have he : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x ≠ 0 ∧ x ≤ p / 2, by simp [nat.lt_succ_iff, nat.succ_le_iff, pos_iff_ne_zero] {contextual := tt}, have hep : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x < p, from λ x hx, lt_of_le_of_lt (he hx).2 (nat.div_lt_self hp.1.pos dec_trivial), have hpe : ∀ {x}, x ∈ Ico 1 (p / 2).succ → ¬ p ∣ x, from λ x hx hpx, not_lt_of_ge (le_of_dvd (nat.pos_of_ne_zero (he hx).1) hpx) (hep hx), have hmem : ∀ (x : ℕ) (hx : x ∈ Ico 1 (p / 2).succ), (a * x : zmod p).val_min_abs.nat_abs ∈ Ico 1 (p / 2).succ, { assume x hx, simp [hap, char_p.cast_eq_zero_iff (zmod p) p, hpe hx, lt_succ_iff, succ_le_iff, pos_iff_ne_zero, nat_abs_val_min_abs_le _], }, have hsurj : ∀ (b : ℕ) (hb : b ∈ Ico 1 (p / 2).succ), ∃ x ∈ Ico 1 (p / 2).succ, b = (a * x : zmod p).val_min_abs.nat_abs, { assume b hb, refine ⟨(b / a : zmod p).val_min_abs.nat_abs, mem_Ico.mpr ⟨_, _⟩, _⟩, { apply nat.pos_of_ne_zero, simp only [div_eq_mul_inv, hap, char_p.cast_eq_zero_iff (zmod p) p, hpe hb, not_false_iff, val_min_abs_eq_zero, inv_eq_zero, int.nat_abs_eq_zero, ne.def, mul_eq_zero, or_self] }, { apply lt_succ_of_le, apply nat_abs_val_min_abs_le }, { rw nat_cast_nat_abs_val_min_abs, split_ifs, { erw [mul_div_cancel' _ hap, val_min_abs_def_pos, val_cast_of_lt (hep hb), if_pos (le_of_lt_succ (mem_Ico.1 hb).2), int.nat_abs_of_nat], }, { erw [mul_neg, mul_div_cancel' _ hap, nat_abs_val_min_abs_neg, val_min_abs_def_pos, val_cast_of_lt (hep hb), if_pos (le_of_lt_succ (mem_Ico.1 hb).2), int.nat_abs_of_nat] } } }, exact multiset.map_eq_map_of_bij_of_nodup _ _ (finset.nodup _) (finset.nodup _) (λ x _, (a * x : zmod p).val_min_abs.nat_abs) hmem (λ _ _, rfl) (inj_on_of_surj_on_of_card_le _ hmem hsurj le_rfl) hsurj end private lemma gauss_lemma_aux₁ (p : ℕ) [fact p.prime] [fact (p % 2 = 1)] {a : ℤ} (hap : (a : zmod p) ≠ 0) : (a^(p / 2) * (p / 2)! : zmod p) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2)! := calc (a ^ (p / 2) * (p / 2)! : zmod p) = (∏ x in Ico 1 (p / 2).succ, a * x) : by rw [prod_mul_distrib, ← prod_nat_cast, prod_Ico_id_eq_factorial, prod_const, card_Ico, succ_sub_one]; simp ... = (∏ x in Ico 1 (p / 2).succ, (a * x : zmod p).val) : by simp ... = (∏ x in Ico 1 (p / 2).succ, (if (a * x : zmod p).val ≤ p / 2 then 1 else -1) * (a * x : zmod p).val_min_abs.nat_abs) : prod_congr rfl $ λ _ _, begin simp only [nat_cast_nat_abs_val_min_abs], split_ifs; simp end ... = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (∏ x in Ico 1 (p / 2).succ, (a * x : zmod p).val_min_abs.nat_abs) : have (∏ x in Ico 1 (p / 2).succ, if (a * x : zmod p).val ≤ p / 2 then (1 : zmod p) else -1) = (∏ x in (Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2), -1), from prod_bij_ne_one (λ x _ _, x) (λ x, by split_ifs; simp * at * {contextual := tt}) (λ _ _ _ _ _ _, id) (λ b h _, ⟨b, by simp [-not_le, *] at *⟩) (by intros; split_ifs at *; simp * at *), by rw [prod_mul_distrib, this]; simp ... = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2)! : by rw [← prod_nat_cast, finset.prod_eq_multiset_prod, Ico_map_val_min_abs_nat_abs_eq_Ico_map_id p a hap, ← finset.prod_eq_multiset_prod, prod_Ico_id_eq_factorial] lemma gauss_lemma_aux (p : ℕ) [hp : fact p.prime] [fact (p % 2 = 1)] {a : ℤ} (hap : (a : zmod p) ≠ 0) : (a^(p / 2) : zmod p) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card := (mul_left_inj' (show ((p / 2)! : zmod p) ≠ 0, by rw [ne.def, char_p.cast_eq_zero_iff (zmod p) p, hp.1.dvd_factorial, not_le]; exact nat.div_lt_self hp.1.pos dec_trivial)).1 $ by simpa using gauss_lemma_aux₁ p hap private lemma eisenstein_lemma_aux₁ (p : ℕ) [fact p.prime] [hp2 : fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : ((∑ x in Ico 1 (p / 2).succ, a * x : ℕ) : zmod 2) = ((Ico 1 (p / 2).succ).filter ((λ x : ℕ, p / 2 < (a * x : zmod p).val))).card + ∑ x in Ico 1 (p / 2).succ, x + (∑ x in Ico 1 (p / 2).succ, (a * x) / p : ℕ) := have hp2 : (p : zmod 2) = (1 : ℕ), from (eq_iff_modeq_nat _).2 hp2.1, calc ((∑ x in Ico 1 (p / 2).succ, a * x : ℕ) : zmod 2) = ((∑ x in Ico 1 (p / 2).succ, ((a * x) % p + p * ((a * x) / p)) : ℕ) : zmod 2) : by simp only [mod_add_div] ... = (∑ x in Ico 1 (p / 2).succ, ((a * x : ℕ) : zmod p).val : ℕ) + (∑ x in Ico 1 (p / 2).succ, (a * x) / p : ℕ) : by simp only [val_nat_cast]; simp [sum_add_distrib, mul_sum.symm, nat.cast_add, nat.cast_mul, nat.cast_sum, hp2] ... = _ : congr_arg2 (+) (calc ((∑ x in Ico 1 (p / 2).succ, ((a * x : ℕ) : zmod p).val : ℕ) : zmod 2) = ∑ x in Ico 1 (p / 2).succ, ((((a * x : zmod p).val_min_abs + (if (a * x : zmod p).val ≤ p / 2 then 0 else p)) : ℤ) : zmod 2) : by simp only [(val_eq_ite_val_min_abs _).symm]; simp [nat.cast_sum] ... = ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card + ((∑ x in Ico 1 (p / 2).succ, (a * x : zmod p).val_min_abs.nat_abs) : ℕ) : by { simp [ite_cast, add_comm, sum_add_distrib, finset.sum_ite, hp2, nat.cast_sum], } ... = _ : by rw [finset.sum_eq_multiset_sum, Ico_map_val_min_abs_nat_abs_eq_Ico_map_id p a hap, ← finset.sum_eq_multiset_sum]; simp [nat.cast_sum]) rfl lemma eisenstein_lemma_aux (p : ℕ) [fact p.prime] [fact (p % 2 = 1)] {a : ℕ} (ha2 : a % 2 = 1) (hap : (a : zmod p) ≠ 0) : ((Ico 1 (p / 2).succ).filter ((λ x : ℕ, p / 2 < (a * x : zmod p).val))).card ≡ ∑ x in Ico 1 (p / 2).succ, (x * a) / p [MOD 2] := have ha2 : (a : zmod 2) = (1 : ℕ), from (eq_iff_modeq_nat _).2 ha2, (eq_iff_modeq_nat 2).1 $ sub_eq_zero.1 $ by simpa [add_left_comm, sub_eq_add_neg, finset.mul_sum.symm, mul_comm, ha2, nat.cast_sum, add_neg_eq_iff_eq_add.symm, neg_eq_self_mod_two, add_assoc] using eq.symm (eisenstein_lemma_aux₁ p hap) lemma div_eq_filter_card {a b c : ℕ} (hb0 : 0 < b) (hc : a / b ≤ c) : a / b = ((Ico 1 c.succ).filter (λ x, x * b ≤ a)).card := calc a / b = (Ico 1 (a / b).succ).card : by simp ... = ((Ico 1 c.succ).filter (λ x, x * b ≤ a)).card : congr_arg _ $ finset.ext $ λ x, have x * b ≤ a → x ≤ c, from λ h, le_trans (by rwa [le_div_iff_mul_le hb0]) hc, by simp [lt_succ_iff, le_div_iff_mul_le hb0]; tauto /-- The given sum is the number of integer points in the triangle formed by the diagonal of the rectangle `(0, p/2) × (0, q/2)` -/ private lemma sum_Ico_eq_card_lt {p q : ℕ} : ∑ a in Ico 1 (p / 2).succ, (a * q) / p = (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q)).card := if hp0 : p = 0 then by simp [hp0, finset.ext_iff] else calc ∑ a in Ico 1 (p / 2).succ, (a * q) / p = ∑ a in Ico 1 (p / 2).succ, ((Ico 1 (q / 2).succ).filter (λ x, x * p ≤ a * q)).card : finset.sum_congr rfl $ λ x hx, div_eq_filter_card (nat.pos_of_ne_zero hp0) (calc x * q / p ≤ (p / 2) * q / p : nat.div_le_div_right (mul_le_mul_of_nonneg_right (le_of_lt_succ $ (mem_Ico.mp hx).2) (nat.zero_le _)) ... ≤ _ : nat.div_mul_div_le_div _ _ _) ... = _ : by rw [← card_sigma]; exact card_congr (λ a _, ⟨a.1, a.2⟩) (by simp only [mem_filter, mem_sigma, and_self, forall_true_iff, mem_product] {contextual := tt}) (λ ⟨_, _⟩ ⟨_, _⟩, by simp only [prod.mk.inj_iff, eq_self_iff_true, and_self, heq_iff_eq, forall_true_iff] {contextual := tt}) (λ ⟨b₁, b₂⟩ h, ⟨⟨b₁, b₂⟩, by revert h; simp only [mem_filter, eq_self_iff_true, exists_prop_of_true, mem_sigma, and_self, forall_true_iff, mem_product] {contextual := tt}⟩) /-- Each of the sums in this lemma is the cardinality of the set integer points in each of the two triangles formed by the diagonal of the rectangle `(0, p/2) × (0, q/2)`. Adding them gives the number of points in the rectangle. -/ lemma sum_mul_div_add_sum_mul_div_eq_mul (p q : ℕ) [hp : fact p.prime] (hq0 : (q : zmod p) ≠ 0) : ∑ a in Ico 1 (p / 2).succ, (a * q) / p + ∑ a in Ico 1 (q / 2).succ, (a * p) / q = (p / 2) * (q / 2) := begin have hswap : (((Ico 1 (q / 2).succ).product (Ico 1 (p / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * q ≤ x.1 * p)).card = (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p)).card := card_congr (λ x _, prod.swap x) (λ ⟨_, _⟩, by simp only [mem_filter, and_self, prod.swap_prod_mk, forall_true_iff, mem_product] {contextual := tt}) (λ ⟨_, _⟩ ⟨_, _⟩, by simp only [prod.mk.inj_iff, eq_self_iff_true, and_self, prod.swap_prod_mk, forall_true_iff] {contextual := tt}) (λ ⟨x₁, x₂⟩ h, ⟨⟨x₂, x₁⟩, by revert h; simp only [mem_filter, eq_self_iff_true, and_self, exists_prop_of_true, prod.swap_prod_mk, forall_true_iff, mem_product] {contextual := tt}⟩), have hdisj : disjoint (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q)) (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p)), { apply disjoint_filter.2 (λ x hx hpq hqp, _), have hxp : x.1 < p, from lt_of_le_of_lt (show x.1 ≤ p / 2, by simp only [*, lt_succ_iff, mem_Ico, mem_product] at *; tauto) (nat.div_lt_self hp.1.pos dec_trivial), have : (x.1 : zmod p) = 0, { simpa [hq0] using congr_arg (coe : ℕ → zmod p) (le_antisymm hpq hqp) }, apply_fun zmod.val at this, rw [val_cast_of_lt hxp, val_zero] at this, simpa only [this, nonpos_iff_eq_zero, mem_Ico, one_ne_zero, false_and, mem_product] using hx }, have hunion : ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q) ∪ ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p) = ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)), from finset.ext (λ x, by have := le_total (x.2 * p) (x.1 * q); simp only [mem_union, mem_filter, mem_Ico, mem_product]; tauto), rw [sum_Ico_eq_card_lt, sum_Ico_eq_card_lt, hswap, ← card_disjoint_union hdisj, hunion, card_product], simp only [card_Ico, tsub_zero, succ_sub_succ_eq_sub] end end legendre_symbol end gauss_eisenstein
9f82a3f11953cf363cc254d98e5ebe2246a180bd
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/algebra/order.lean
7df05b270ff28b98a88019369ca76ee079e825f0
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
10,711
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import tactic.alias import tactic.lint /-! # Lemmas about inequalities This file contains some lemmas about `≤`/`≥`/`<`/`>`, and `cmp`. * We simplify `a ≥ b` and `a > b` to `b ≤ a` and `b < a`, respectively. This way we can formulate all lemmas using `≤`/`<` avoiding duplication. * In some cases we introduce dot syntax aliases so that, e.g., from `(hab : a ≤ b) (hbc : b ≤ c) (hbc' : b < c)` one can prove `hab.trans hbc : a ≤ c` and `hab.trans_lt hbc' : a < c`. -/ universe u variables {α : Type u} alias le_trans ← has_le.le.trans alias lt_of_le_of_lt ← has_le.le.trans_lt alias le_antisymm ← has_le.le.antisymm alias lt_of_le_of_ne ← has_le.le.lt_of_ne alias lt_of_le_not_le ← has_le.le.lt_of_not_le alias lt_or_eq_of_le ← has_le.le.lt_or_eq alias le_of_lt ← has_lt.lt.le alias lt_trans ← has_lt.lt.trans alias lt_of_lt_of_le ← has_lt.lt.trans_le alias ne_of_lt ← has_lt.lt.ne alias lt_asymm ← has_lt.lt.asymm has_lt.lt.not_lt alias le_of_eq ← eq.le /-- A version of `le_refl` where the argument is implicit -/ lemma le_rfl [preorder α] {x : α} : x ≤ x := le_refl x namespace eq /-- If `x = y` then `y ≤ x`. Note: this lemma uses `y ≤ x` instead of `x ≥ y`, because `le` is used almost exclusively in mathlib. -/ protected lemma ge [preorder α] {x y : α} (h : x = y) : y ≤ x := h.symm.le lemma trans_le [preorder α] {x y z : α} (h1 : x = y) (h2 : y ≤ z) : x ≤ z := h1.le.trans h2 end eq namespace has_le namespace le @[nolint ge_or_gt] -- see Note [nolint_ge] protected lemma ge [has_le α] {x y : α} (h : x ≤ y) : y ≥ x := h lemma trans_eq [preorder α] {x y z : α} (h1 : x ≤ y) (h2 : y = z) : x ≤ z := h1.trans h2.le end le end has_le namespace has_lt namespace lt @[nolint ge_or_gt] -- see Note [nolint_ge] protected lemma gt [has_lt α] {x y : α} (h : x < y) : y > x := h protected lemma false [preorder α] {x : α} : x < x → false := lt_irrefl x end lt end has_lt namespace ge @[nolint ge_or_gt] -- see Note [nolint_ge] protected lemma le [has_le α] {x y : α} (h : x ≥ y) : y ≤ x := h end ge namespace gt @[nolint ge_or_gt] -- see Note [nolint_ge] protected lemma lt [has_lt α] {x y : α} (h : x > y) : y < x := h end gt @[nolint ge_or_gt] -- see Note [nolint_ge] theorem ge_of_eq [preorder α] {a b : α} (h : a = b) : a ≥ b := h.ge @[simp, nolint ge_or_gt] -- see Note [nolint_ge] lemma ge_iff_le [preorder α] {a b : α} : a ≥ b ↔ b ≤ a := iff.rfl @[simp, nolint ge_or_gt] -- see Note [nolint_ge] lemma gt_iff_lt [preorder α] {a b : α} : a > b ↔ b < a := iff.rfl lemma not_le_of_lt [preorder α] {a b : α} (h : a < b) : ¬ b ≤ a := (le_not_le_of_lt h).right alias not_le_of_lt ← has_lt.lt.not_le lemma not_lt_of_le [preorder α] {a b : α} (h : a ≤ b) : ¬ b < a | hab := hab.not_le h alias not_lt_of_le ← has_le.le.not_lt lemma le_iff_eq_or_lt [partial_order α] {a b : α} : a ≤ b ↔ a = b ∨ a < b := le_iff_lt_or_eq.trans or.comm lemma lt_iff_le_and_ne [partial_order α] {a b : α} : a < b ↔ a ≤ b ∧ a ≠ b := ⟨λ h, ⟨le_of_lt h, ne_of_lt h⟩, λ ⟨h1, h2⟩, h1.lt_of_ne h2⟩ lemma eq_iff_le_not_lt [partial_order α] {a b : α} : a = b ↔ a ≤ b ∧ ¬ a < b := ⟨λ h, ⟨h.le, h ▸ lt_irrefl _⟩, λ ⟨h₁, h₂⟩, h₁.antisymm $ classical.by_contradiction $ λ h₃, h₂ (h₁.lt_of_not_le h₃)⟩ lemma eq_or_lt_of_le [partial_order α] {a b : α} (h : a ≤ b) : a = b ∨ a < b := h.lt_or_eq.symm alias eq_or_lt_of_le ← has_le.le.eq_or_lt lemma lt_of_not_ge' [linear_order α] {a b : α} (h : ¬ b ≤ a) : a < b := ((le_total _ _).resolve_right h).lt_of_not_le h lemma lt_iff_not_ge' [linear_order α] {x y : α} : x < y ↔ ¬ y ≤ x := ⟨not_le_of_gt, lt_of_not_ge'⟩ lemma le_of_not_lt [linear_order α] {a b : α} : ¬ a < b → b ≤ a := not_lt.1 lemma lt_or_le [linear_order α] : ∀ a b : α, a < b ∨ b ≤ a := lt_or_ge lemma le_or_lt [linear_order α] : ∀ a b : α, a ≤ b ∨ b < a := le_or_gt lemma has_le.le.lt_or_le [linear_order α] {a b : α} (h : a ≤ b) (c : α) : a < c ∨ c ≤ b := (lt_or_le a c).imp id $ λ hc, hc.trans h lemma has_le.le.le_or_lt [linear_order α] {a b : α} (h : a ≤ b) (c : α) : a ≤ c ∨ c < b := (le_or_lt a c).imp id $ λ hc, hc.trans_le h lemma not_lt_iff_eq_or_lt [linear_order α] {a b : α} : ¬ a < b ↔ a = b ∨ b < a := not_lt.trans $ le_iff_eq_or_lt.trans $ or_congr eq_comm iff.rfl lemma exists_ge_of_linear [linear_order α] (a b : α) : ∃ c, a ≤ c ∧ b ≤ c := match le_total a b with | or.inl h := ⟨_, h, le_rfl⟩ | or.inr h := ⟨_, le_rfl, h⟩ end lemma lt_imp_lt_of_le_imp_le {β} [linear_order α] [preorder β] {a b : α} {c d : β} (H : a ≤ b → c ≤ d) (h : d < c) : b < a := lt_of_not_ge' $ λ h', (H h').not_lt h lemma le_imp_le_of_lt_imp_lt {β} [preorder α] [linear_order β] {a b : α} {c d : β} (H : d < c → b < a) (h : a ≤ b) : c ≤ d := le_of_not_gt $ λ h', (H h').not_le h lemma le_imp_le_iff_lt_imp_lt {β} [linear_order α] [linear_order β] {a b : α} {c d : β} : (a ≤ b → c ≤ d) ↔ (d < c → b < a) := ⟨lt_imp_lt_of_le_imp_le, le_imp_le_of_lt_imp_lt⟩ lemma lt_iff_lt_of_le_iff_le' {β} [preorder α] [preorder β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) (H' : b ≤ a ↔ d ≤ c) : b < a ↔ d < c := lt_iff_le_not_le.trans $ (and_congr H' (not_congr H)).trans lt_iff_le_not_le.symm lemma lt_iff_lt_of_le_iff_le {β} [linear_order α] [linear_order β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) : b < a ↔ d < c := not_le.symm.trans $ (not_congr H).trans $ not_le lemma le_iff_le_iff_lt_iff_lt {β} [linear_order α] [linear_order β] {a b : α} {c d : β} : (a ≤ b ↔ c ≤ d) ↔ (b < a ↔ d < c) := ⟨lt_iff_lt_of_le_iff_le, λ H, not_lt.symm.trans $ (not_congr H).trans $ not_lt⟩ lemma eq_of_forall_le_iff [partial_order α] {a b : α} (H : ∀ c, c ≤ a ↔ c ≤ b) : a = b := le_antisymm ((H _).1 (le_refl _)) ((H _).2 (le_refl _)) lemma le_of_forall_le [preorder α] {a b : α} (H : ∀ c, c ≤ a → c ≤ b) : a ≤ b := H _ (le_refl _) lemma le_of_forall_le' [preorder α] {a b : α} (H : ∀ c, a ≤ c → b ≤ c) : b ≤ a := H _ (le_refl _) lemma le_of_forall_lt [linear_order α] {a b : α} (H : ∀ c, c < a → c < b) : a ≤ b := le_of_not_lt $ λ h, lt_irrefl _ (H _ h) lemma forall_lt_iff_le [linear_order α] {a b : α} : (∀ ⦃c⦄, c < a → c < b) ↔ a ≤ b := ⟨le_of_forall_lt, λ h c hca, lt_of_lt_of_le hca h⟩ lemma le_of_forall_lt' [linear_order α] {a b : α} (H : ∀ c, a < c → b < c) : b ≤ a := le_of_not_lt $ λ h, lt_irrefl _ (H _ h) lemma forall_lt_iff_le' [linear_order α] {a b : α} : (∀ ⦃c⦄, a < c → b < c) ↔ b ≤ a := ⟨le_of_forall_lt', λ h c hac, lt_of_le_of_lt h hac⟩ lemma eq_of_forall_ge_iff [partial_order α] {a b : α} (H : ∀ c, a ≤ c ↔ b ≤ c) : a = b := le_antisymm ((H _).2 (le_refl _)) ((H _).1 (le_refl _)) /-- monotonicity of `≤` with respect to `→` -/ lemma le_implies_le_of_le_of_le {a b c d : α} [preorder α] (h₀ : c ≤ a) (h₁ : b ≤ d) : a ≤ b → c ≤ d := assume h₂ : a ≤ b, calc c ≤ a : h₀ ... ≤ b : h₂ ... ≤ d : h₁ namespace decidable -- See Note [decidable namespace] lemma le_imp_le_iff_lt_imp_lt {β} [linear_order α] [decidable_linear_order β] {a b : α} {c d : β} : (a ≤ b → c ≤ d) ↔ (d < c → b < a) := ⟨lt_imp_lt_of_le_imp_le, le_imp_le_of_lt_imp_lt⟩ -- See Note [decidable namespace] lemma le_iff_le_iff_lt_iff_lt {β} [decidable_linear_order α] [decidable_linear_order β] {a b : α} {c d : β} : (a ≤ b ↔ c ≤ d) ↔ (b < a ↔ d < c) := ⟨lt_iff_lt_of_le_iff_le, λ H, not_lt.symm.trans $ (not_congr H).trans $ not_lt⟩ end decidable namespace ordering /-- `compares o a b` means that `a` and `b` have the ordering relation `o` between them, assuming that the relation `a < b` is defined -/ @[simp] def compares [has_lt α] : ordering → α → α → Prop | lt a b := a < b | eq a b := a = b | gt a b := a > b theorem compares.eq_lt [preorder α] : ∀ {o} {a b : α}, compares o a b → (o = lt ↔ a < b) | lt a b h := ⟨λ _, h, λ _, rfl⟩ | eq a b h := ⟨λ h, by injection h, λ h', (ne_of_lt h' h).elim⟩ | gt a b h := ⟨λ h, by injection h, λ h', (lt_asymm h h').elim⟩ theorem compares.eq_eq [preorder α] : ∀ {o} {a b : α}, compares o a b → (o = eq ↔ a = b) | lt a b h := ⟨λ h, by injection h, λ h', (ne_of_lt h h').elim⟩ | eq a b h := ⟨λ _, h, λ _, rfl⟩ | gt a b h := ⟨λ h, by injection h, λ h', (ne_of_gt h h').elim⟩ theorem compares.eq_gt [preorder α] : ∀ {o} {a b : α}, compares o a b → (o = gt ↔ b < a) | lt a b h := ⟨λ h, by injection h, λ h', (lt_asymm h h').elim⟩ | eq a b h := ⟨λ h, by injection h, λ h', (ne_of_gt h' h).elim⟩ | gt a b h := ⟨λ _, h, λ _, rfl⟩ theorem compares.inj [preorder α] {o₁} : ∀ {o₂} {a b : α}, compares o₁ a b → compares o₂ a b → o₁ = o₂ | lt a b h₁ h₂ := h₁.eq_lt.2 h₂ | eq a b h₁ h₂ := h₁.eq_eq.2 h₂ | gt a b h₁ h₂ := h₁.eq_gt.2 h₂ theorem compares_iff_of_compares_impl {β : Type*} [linear_order α] [preorder β] {a b : α} {a' b' : β} (h : ∀ {o}, compares o a b → compares o a' b') (o) : compares o a b ↔ compares o a' b' := begin refine ⟨h, λ ho, _⟩, cases lt_trichotomy a b with hab hab, { change compares ordering.lt a b at hab, rwa [ho.inj (h hab)] }, { cases hab with hab hab, { change compares ordering.eq a b at hab, rwa [ho.inj (h hab)] }, { change compares ordering.gt a b at hab, rwa [ho.inj (h hab)] } } end theorem swap_or_else (o₁ o₂) : (or_else o₁ o₂).swap = or_else o₁.swap o₂.swap := by cases o₁; try {refl}; cases o₂; refl theorem or_else_eq_lt (o₁ o₂) : or_else o₁ o₂ = lt ↔ o₁ = lt ∨ (o₁ = eq ∧ o₂ = lt) := by cases o₁; cases o₂; exact dec_trivial end ordering theorem cmp_compares [decidable_linear_order α] (a b : α) : (cmp a b).compares a b := begin unfold cmp cmp_using, by_cases a < b; simp [h], by_cases h₂ : b < a; simp [h₂, gt], exact (lt_or_eq_of_le (le_of_not_gt h₂)).resolve_left h end theorem cmp_swap [preorder α] [@decidable_rel α (<)] (a b : α) : (cmp a b).swap = cmp b a := begin unfold cmp cmp_using, by_cases a < b; by_cases h₂ : b < a; simp [h, h₂, gt, ordering.swap], exact lt_asymm h h₂ end
8138dba737d1a47679bae24fbe1ac70a2044eada
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/simp8.lean
6088279c7d527b9cdfb3e6cf08f858e49994d5ba
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
294
lean
variables a b c d e f : Nat rewrite_set simple add_rewrite Nat::add_assoc Nat::add_comm Nat::add_left_comm Nat::distributer Nat::distributel : simple (* local t = parse_lean("f + (c + f + d) + (e * (a + c) + (d + a))") local t2, pr = simplify(t, "simple") print(t) print("====>") print(t2) *)
990a97a05e543fd637dda422ae039c1e950bd8c7
82e44445c70db0f03e30d7be725775f122d72f3e
/src/analysis/calculus/specific_functions.lean
23d6c720645a868a5330496b7b7407071c313900
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
18,898
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 analysis.calculus.iterated_deriv import analysis.normed_space.euclidean_dist /-! # Infinitely smooth bump function In this file we construct several infinitely smooth functions with properties that an analytic function cannot have: * `exp_neg_inv_glue` is equal to zero for `x ≤ 0` and is strictly positive otherwise; it is given by `x ↦ exp (-1/x)` for `x > 0`; * `real.smooth_transition` is equal to zero for `x ≤ 0` and is equal to one for `x ≥ 1`; it is given by `exp_neg_inv_glue x / (exp_neg_inv_glue x + exp_neg_inv_glue (1 - x))`; * `f : times_cont_diff_bump_of_inner c`, where `c` is a point in an inner product space, is a bundled smooth function such that - `f` is equal to `1` in `metric.closed_ball c f.r`; - `support f = metric.ball c f.R`; - `0 ≤ f x ≤ 1` for all `x`. The structure `times_cont_diff_bump_of_inner` contains the data required to construct the function: real numbers `r`, `R`, and proofs of `0 < r < R`. The function itself is available through `coe_fn`. * `f : times_cont_diff_bump c`, where `c` is a point in a finite dimensional real vector space, is a bundled smooth function such that - `f` is equal to `1` in `euclidean.closed_ball c f.r`; - `support f = euclidean.ball c f.R`; - `0 ≤ f x ≤ 1` for all `x`. The structure `times_cont_diff_bump` contains the data required to construct the function: real numbers `r`, `R`, and proofs of `0 < r < R`. The function itself is available through `coe_fn`. -/ noncomputable theory open_locale classical topological_space open polynomial real filter set function /-- `exp_neg_inv_glue` is the real function given by `x ↦ exp (-1/x)` for `x > 0` and `0` for `x ≤ 0`. It is a basic building block to construct smooth partitions of unity. Its main property is that it vanishes for `x ≤ 0`, it is positive for `x > 0`, and the junction between the two behaviors is flat enough to retain smoothness. The fact that this function is `C^∞` is proved in `exp_neg_inv_glue.smooth`. -/ def exp_neg_inv_glue (x : ℝ) : ℝ := if x ≤ 0 then 0 else exp (-x⁻¹) namespace exp_neg_inv_glue /-- Our goal is to prove that `exp_neg_inv_glue` is `C^∞`. For this, we compute its successive derivatives for `x > 0`. The `n`-th derivative is of the form `P_aux n (x) exp(-1/x) / x^(2 n)`, where `P_aux n` is computed inductively. -/ noncomputable def P_aux : ℕ → polynomial ℝ | 0 := 1 | (n+1) := X^2 * (P_aux n).derivative + (1 - C ↑(2 * n) * X) * (P_aux n) /-- Formula for the `n`-th derivative of `exp_neg_inv_glue`, as an auxiliary function `f_aux`. -/ def f_aux (n : ℕ) (x : ℝ) : ℝ := if x ≤ 0 then 0 else (P_aux n).eval x * exp (-x⁻¹) / x^(2 * n) /-- The `0`-th auxiliary function `f_aux 0` coincides with `exp_neg_inv_glue`, by definition. -/ lemma f_aux_zero_eq : f_aux 0 = exp_neg_inv_glue := begin ext x, by_cases h : x ≤ 0, { simp [exp_neg_inv_glue, f_aux, h] }, { simp [h, exp_neg_inv_glue, f_aux, ne_of_gt (not_le.1 h), P_aux] } end /-- For positive values, the derivative of the `n`-th auxiliary function `f_aux n` (given in this statement in unfolded form) is the `n+1`-th auxiliary function, since the polynomial `P_aux (n+1)` was chosen precisely to ensure this. -/ lemma f_aux_deriv (n : ℕ) (x : ℝ) (hx : x ≠ 0) : has_deriv_at (λx, (P_aux n).eval x * exp (-x⁻¹) / x^(2 * n)) ((P_aux (n+1)).eval x * exp (-x⁻¹) / x^(2 * (n + 1))) x := begin have A : ∀k:ℕ, 2 * (k + 1) - 1 = 2 * k + 1, { assume k, rw nat.sub_eq_iff_eq_add, { ring }, { simpa [mul_add] using add_le_add (zero_le (2 * k)) one_le_two } }, convert (((P_aux n).has_deriv_at x).mul (((has_deriv_at_exp _).comp x (has_deriv_at_inv hx).neg))).div (has_deriv_at_pow (2 * n) x) (pow_ne_zero _ hx) using 1, field_simp [hx, P_aux], -- `ring_exp` can't solve `p ∨ q` goal generated by `mul_eq_mul_right_iff` cases n; simp [nat.succ_eq_add_one, A, -mul_eq_mul_right_iff]; ring_exp end /-- For positive values, the derivative of the `n`-th auxiliary function `f_aux n` is the `n+1`-th auxiliary function. -/ lemma f_aux_deriv_pos (n : ℕ) (x : ℝ) (hx : 0 < x) : has_deriv_at (f_aux n) ((P_aux (n+1)).eval x * exp (-x⁻¹) / x^(2 * (n + 1))) x := begin apply (f_aux_deriv n x (ne_of_gt hx)).congr_of_eventually_eq, filter_upwards [lt_mem_nhds hx], assume y hy, simp [f_aux, hy.not_le] end /-- To get differentiability at `0` of the auxiliary functions, we need to know that their limit is `0`, to be able to apply general differentiability extension theorems. This limit is checked in this lemma. -/ lemma f_aux_limit (n : ℕ) : tendsto (λx, (P_aux n).eval x * exp (-x⁻¹) / x^(2 * n)) (𝓝[Ioi 0] 0) (𝓝 0) := begin have A : tendsto (λx, (P_aux n).eval x) (𝓝[Ioi 0] 0) (𝓝 ((P_aux n).eval 0)) := (P_aux n).continuous_within_at, have B : tendsto (λx, exp (-x⁻¹) / x^(2 * n)) (𝓝[Ioi 0] 0) (𝓝 0), { convert (tendsto_pow_mul_exp_neg_at_top_nhds_0 (2 * n)).comp tendsto_inv_zero_at_top, ext x, field_simp }, convert A.mul B; simp [mul_div_assoc] end /-- Deduce from the limiting behavior at `0` of its derivative and general differentiability extension theorems that the auxiliary function `f_aux n` is differentiable at `0`, with derivative `0`. -/ lemma f_aux_deriv_zero (n : ℕ) : has_deriv_at (f_aux n) 0 0 := begin -- we check separately differentiability on the left and on the right have A : has_deriv_within_at (f_aux n) (0 : ℝ) (Iic 0) 0, { apply (has_deriv_at_const (0 : ℝ) (0 : ℝ)).has_deriv_within_at.congr, { assume y hy, simp at hy, simp [f_aux, hy] }, { simp [f_aux, le_refl] } }, have B : has_deriv_within_at (f_aux n) (0 : ℝ) (Ici 0) 0, { have diff : differentiable_on ℝ (f_aux n) (Ioi 0) := λx hx, (f_aux_deriv_pos n x hx).differentiable_at.differentiable_within_at, -- next line is the nontrivial bit of this proof, appealing to differentiability -- extension results. apply has_deriv_at_interval_left_endpoint_of_tendsto_deriv diff _ self_mem_nhds_within, { refine (f_aux_limit (n+1)).congr' _, apply mem_sets_of_superset self_mem_nhds_within (λx hx, _), simp [(f_aux_deriv_pos n x hx).deriv] }, { have : f_aux n 0 = 0, by simp [f_aux, le_refl], simp only [continuous_within_at, this], refine (f_aux_limit n).congr' _, apply mem_sets_of_superset self_mem_nhds_within (λx hx, _), have : ¬(x ≤ 0), by simpa using hx, simp [f_aux, this] } }, simpa using A.union B, end /-- At every point, the auxiliary function `f_aux n` has a derivative which is equal to `f_aux (n+1)`. -/ lemma f_aux_has_deriv_at (n : ℕ) (x : ℝ) : has_deriv_at (f_aux n) (f_aux (n+1) x) x := begin -- check separately the result for `x < 0`, where it is trivial, for `x > 0`, where it is done -- in `f_aux_deriv_pos`, and for `x = 0`, done in -- `f_aux_deriv_zero`. rcases lt_trichotomy x 0 with hx|hx|hx, { have : f_aux (n+1) x = 0, by simp [f_aux, le_of_lt hx], rw this, apply (has_deriv_at_const x (0 : ℝ)).congr_of_eventually_eq, filter_upwards [gt_mem_nhds hx], assume y hy, simp [f_aux, hy.le] }, { have : f_aux (n + 1) 0 = 0, by simp [f_aux, le_refl], rw [hx, this], exact f_aux_deriv_zero n }, { have : f_aux (n+1) x = (P_aux (n+1)).eval x * exp (-x⁻¹) / x^(2 * (n+1)), by simp [f_aux, not_le_of_gt hx], rw this, exact f_aux_deriv_pos n x hx }, end /-- The successive derivatives of the auxiliary function `f_aux 0` are the functions `f_aux n`, by induction. -/ lemma f_aux_iterated_deriv (n : ℕ) : iterated_deriv n (f_aux 0) = f_aux n := begin induction n with n IH, { simp }, { simp [iterated_deriv_succ, IH], ext x, exact (f_aux_has_deriv_at n x).deriv } end /-- The function `exp_neg_inv_glue` is smooth. -/ protected theorem times_cont_diff {n} : times_cont_diff ℝ n exp_neg_inv_glue := begin rw ← f_aux_zero_eq, apply times_cont_diff_of_differentiable_iterated_deriv (λ m hm, _), rw f_aux_iterated_deriv m, exact λ x, (f_aux_has_deriv_at m x).differentiable_at end /-- The function `exp_neg_inv_glue` vanishes on `(-∞, 0]`. -/ lemma zero_of_nonpos {x : ℝ} (hx : x ≤ 0) : exp_neg_inv_glue x = 0 := by simp [exp_neg_inv_glue, hx] /-- The function `exp_neg_inv_glue` is positive on `(0, +∞)`. -/ lemma pos_of_pos {x : ℝ} (hx : 0 < x) : 0 < exp_neg_inv_glue x := by simp [exp_neg_inv_glue, not_le.2 hx, exp_pos] /-- The function exp_neg_inv_glue` is nonnegative. -/ lemma nonneg (x : ℝ) : 0 ≤ exp_neg_inv_glue x := begin cases le_or_gt x 0, { exact ge_of_eq (zero_of_nonpos h) }, { exact le_of_lt (pos_of_pos h) } end end exp_neg_inv_glue /-- An infinitely smooth function `f : ℝ → ℝ` such that `f x = 0` for `x ≤ 0`, `f x = 1` for `1 ≤ x`, and `0 < f x < 1` for `0 < x < 1`. -/ def real.smooth_transition (x : ℝ) : ℝ := exp_neg_inv_glue x / (exp_neg_inv_glue x + exp_neg_inv_glue (1 - x)) namespace real namespace smooth_transition variables {x : ℝ} open exp_neg_inv_glue lemma pos_denom (x) : 0 < exp_neg_inv_glue x + exp_neg_inv_glue (1 - x) := ((@zero_lt_one ℝ _ _).lt_or_lt x).elim (λ hx, add_pos_of_pos_of_nonneg (pos_of_pos hx) (nonneg _)) (λ hx, add_pos_of_nonneg_of_pos (nonneg _) (pos_of_pos $ sub_pos.2 hx)) lemma one_of_one_le (h : 1 ≤ x) : smooth_transition x = 1 := (div_eq_one_iff_eq $ (pos_denom x).ne').2 $ by rw [zero_of_nonpos (sub_nonpos.2 h), add_zero] lemma zero_of_nonpos (h : x ≤ 0) : smooth_transition x = 0 := by rw [smooth_transition, zero_of_nonpos h, zero_div] lemma le_one (x : ℝ) : smooth_transition x ≤ 1 := (div_le_one (pos_denom x)).2 $ le_add_of_nonneg_right (nonneg _) lemma nonneg (x : ℝ) : 0 ≤ smooth_transition x := div_nonneg (exp_neg_inv_glue.nonneg _) (pos_denom x).le lemma lt_one_of_lt_one (h : x < 1) : smooth_transition x < 1 := (div_lt_one $ pos_denom x).2 $ lt_add_of_pos_right _ $ pos_of_pos $ sub_pos.2 h lemma pos_of_pos (h : 0 < x) : 0 < smooth_transition x := div_pos (exp_neg_inv_glue.pos_of_pos h) (pos_denom x) protected lemma times_cont_diff {n} : times_cont_diff ℝ n smooth_transition := exp_neg_inv_glue.times_cont_diff.div (exp_neg_inv_glue.times_cont_diff.add $ exp_neg_inv_glue.times_cont_diff.comp $ times_cont_diff_const.sub times_cont_diff_id) $ λ x, (pos_denom x).ne' protected lemma times_cont_diff_at {x n} : times_cont_diff_at ℝ n smooth_transition x := smooth_transition.times_cont_diff.times_cont_diff_at end smooth_transition end real variable {E : Type*} /-- `f : times_cont_diff_bump_of_inner c`, where `c` is a point in an inner product space, is a bundled smooth function such that - `f` is equal to `1` in `metric.closed_ball c f.r`; - `support f = metric.ball c f.R`; - `0 ≤ f x ≤ 1` for all `x`. The structure `times_cont_diff_bump_of_inner` contains the data required to construct the function: real numbers `r`, `R`, and proofs of `0 < r < R`. The function itself is available through `coe_fn`. -/ structure times_cont_diff_bump_of_inner (c : E) := (r R : ℝ) (r_pos : 0 < r) (r_lt_R : r < R) namespace times_cont_diff_bump_of_inner lemma R_pos {c : E} (f : times_cont_diff_bump_of_inner c) : 0 < f.R := f.r_pos.trans f.r_lt_R instance (c : E) : inhabited (times_cont_diff_bump_of_inner c) := ⟨⟨1, 2, zero_lt_one, one_lt_two⟩⟩ variables [inner_product_space ℝ E] {c : E} (f : times_cont_diff_bump_of_inner c) {x : E} /-- The function defined by `f : times_cont_diff_bump_of_inner c`. Use automatic coercion to function instead. -/ def to_fun (f : times_cont_diff_bump_of_inner c) : E → ℝ := λ x, real.smooth_transition ((f.R - dist x c) / (f.R - f.r)) instance : has_coe_to_fun (times_cont_diff_bump_of_inner c) := ⟨_, to_fun⟩ open real (smooth_transition) real.smooth_transition metric lemma one_of_mem_closed_ball (hx : x ∈ closed_ball c f.r) : f x = 1 := one_of_one_le $ (one_le_div (sub_pos.2 f.r_lt_R)).2 $ sub_le_sub_left hx _ lemma nonneg : 0 ≤ f x := nonneg _ lemma le_one : f x ≤ 1 := le_one _ lemma pos_of_mem_ball (hx : x ∈ ball c f.R) : 0 < f x := pos_of_pos $ div_pos (sub_pos.2 hx) (sub_pos.2 f.r_lt_R) lemma lt_one_of_lt_dist (h : f.r < dist x c) : f x < 1 := lt_one_of_lt_one $ (div_lt_one (sub_pos.2 f.r_lt_R)).2 $ sub_lt_sub_left h _ lemma zero_of_le_dist (hx : f.R ≤ dist x c) : f x = 0 := zero_of_nonpos $ div_nonpos_of_nonpos_of_nonneg (sub_nonpos.2 hx) (sub_nonneg.2 f.r_lt_R.le) lemma support_eq : support ⇑f = metric.ball c f.R := begin ext x, suffices : f x ≠ 0 ↔ dist x c < f.R, by simpa [mem_support], cases lt_or_le (dist x c) f.R with hx hx, { simp [hx, (f.pos_of_mem_ball hx).ne'] }, { simp [hx.not_lt, f.zero_of_le_dist hx] } end lemma eventually_eq_one_of_mem_ball (h : x ∈ ball c f.r) : f =ᶠ[𝓝 x] 1 := ((is_open_lt (continuous_id.dist continuous_const) continuous_const).eventually_mem h).mono $ λ z hz, f.one_of_mem_closed_ball (le_of_lt hz) lemma eventually_eq_one : f =ᶠ[𝓝 c] 1 := f.eventually_eq_one_of_mem_ball (mem_ball_self f.r_pos) protected lemma times_cont_diff_at {n} : times_cont_diff_at ℝ n f x := begin rcases em (x = c) with rfl|hx, { refine times_cont_diff_at.congr_of_eventually_eq _ f.eventually_eq_one, rw pi.one_def, exact times_cont_diff_at_const }, { exact real.smooth_transition.times_cont_diff_at.comp x (times_cont_diff_at.div_const $ times_cont_diff_at_const.sub $ times_cont_diff_at_id.dist times_cont_diff_at_const hx) } end protected lemma times_cont_diff {n} : times_cont_diff ℝ n f := times_cont_diff_iff_times_cont_diff_at.2 $ λ y, f.times_cont_diff_at protected lemma times_cont_diff_within_at {s n} : times_cont_diff_within_at ℝ n f s x := f.times_cont_diff_at.times_cont_diff_within_at end times_cont_diff_bump_of_inner /-- `f : times_cont_diff_bump c`, where `c` is a point in a finite dimensional real vector space, is a bundled smooth function such that - `f` is equal to `1` in `euclidean.closed_ball c f.r`; - `support f = euclidean.ball c f.R`; - `0 ≤ f x ≤ 1` for all `x`. The structure `times_cont_diff_bump` contains the data required to construct the function: real numbers `r`, `R`, and proofs of `0 < r < R`. The function itself is available through `coe_fn`.-/ structure times_cont_diff_bump [normed_group E] [normed_space ℝ E] [finite_dimensional ℝ E] (c : E) extends times_cont_diff_bump_of_inner (to_euclidean c) namespace times_cont_diff_bump variables [normed_group E] [normed_space ℝ E] [finite_dimensional ℝ E] {c x : E} (f : times_cont_diff_bump c) /-- The function defined by `f : times_cont_diff_bump c`. Use automatic coercion to function instead. -/ def to_fun (f : times_cont_diff_bump c) : E → ℝ := f.to_times_cont_diff_bump_of_inner ∘ to_euclidean instance : has_coe_to_fun (times_cont_diff_bump c) := ⟨λ f, E → ℝ, to_fun⟩ instance (c : E) : inhabited (times_cont_diff_bump c) := ⟨⟨default _⟩⟩ lemma R_pos : 0 < f.R := f.to_times_cont_diff_bump_of_inner.R_pos lemma coe_eq_comp : ⇑f = f.to_times_cont_diff_bump_of_inner ∘ to_euclidean := rfl lemma one_of_mem_closed_ball (hx : x ∈ euclidean.closed_ball c f.r) : f x = 1 := f.to_times_cont_diff_bump_of_inner.one_of_mem_closed_ball hx lemma nonneg : 0 ≤ f x := f.to_times_cont_diff_bump_of_inner.nonneg lemma le_one : f x ≤ 1 := f.to_times_cont_diff_bump_of_inner.le_one lemma pos_of_mem_ball (hx : x ∈ euclidean.ball c f.R) : 0 < f x := f.to_times_cont_diff_bump_of_inner.pos_of_mem_ball hx lemma lt_one_of_lt_dist (h : f.r < euclidean.dist x c) : f x < 1 := f.to_times_cont_diff_bump_of_inner.lt_one_of_lt_dist h lemma zero_of_le_dist (hx : f.R ≤ euclidean.dist x c) : f x = 0 := f.to_times_cont_diff_bump_of_inner.zero_of_le_dist hx lemma support_eq : support ⇑f = euclidean.ball c f.R := by rw [euclidean.ball_eq_preimage, ← f.to_times_cont_diff_bump_of_inner.support_eq, ← support_comp_eq_preimage, coe_eq_comp] lemma closure_support_eq : closure (support f) = euclidean.closed_ball c f.R := by rw [f.support_eq, euclidean.closure_ball _ f.R_pos] lemma compact_closure_support : is_compact (closure (support f)) := by { rw f.closure_support_eq, exact euclidean.compact_ball } lemma eventually_eq_one_of_mem_ball (h : x ∈ euclidean.ball c f.r) : f =ᶠ[𝓝 x] 1 := to_euclidean.continuous_at (f.to_times_cont_diff_bump_of_inner.eventually_eq_one_of_mem_ball h) lemma eventually_eq_one : f =ᶠ[𝓝 c] 1 := f.eventually_eq_one_of_mem_ball $ euclidean.mem_ball_self f.r_pos protected lemma times_cont_diff {n} : times_cont_diff ℝ n f := f.to_times_cont_diff_bump_of_inner.times_cont_diff.comp (to_euclidean : E ≃L[ℝ] _).times_cont_diff protected lemma times_cont_diff_at {n} : times_cont_diff_at ℝ n f x := f.times_cont_diff.times_cont_diff_at protected lemma times_cont_diff_within_at {s n} : times_cont_diff_within_at ℝ n f s x := f.times_cont_diff_at.times_cont_diff_within_at lemma exists_closure_support_subset {s : set E} (hs : s ∈ 𝓝 c) : ∃ f : times_cont_diff_bump c, closure (support f) ⊆ s := let ⟨R, h0, hR⟩ := euclidean.nhds_basis_closed_ball.mem_iff.1 hs in ⟨⟨⟨R / 2, R, half_pos h0, half_lt_self h0⟩⟩, by rwa closure_support_eq⟩ lemma exists_closure_subset {R : ℝ} (hR : 0 < R) {s : set E} (hs : is_closed s) (hsR : s ⊆ euclidean.ball c R) : ∃ f : times_cont_diff_bump c, f.R = R ∧ s ⊆ euclidean.ball c f.r := begin rcases euclidean.exists_pos_lt_subset_ball hR hs hsR with ⟨r, hr, hsr⟩, exact ⟨⟨⟨r, R, hr.1, hr.2⟩⟩, rfl, hsr⟩ end end times_cont_diff_bump open finite_dimensional metric /-- If `E` is a finite dimensional normed space over `ℝ`, then for any point `x : E` and its neighborhood `s` there exists an infinitely smooth function with the following properties: * `f y = 1` in a neighborhood of `x`; * `f y = 0` outside of `s`; * moreover, `closure (support f) ⊆ s` and `closure (support f)` is a compact set; * `f y ∈ [0, 1]` for all `y`. This lemma is a simple wrapper around lemmas about bundled smooth bump functions, see `times_cont_diff_bump`. -/ lemma exists_times_cont_diff_bump_function_of_mem_nhds [normed_group E] [normed_space ℝ E] [finite_dimensional ℝ E] {x : E} {s : set E} (hs : s ∈ 𝓝 x) : ∃ f : E → ℝ, f =ᶠ[𝓝 x] 1 ∧ (∀ y, f y ∈ Icc (0 : ℝ) 1) ∧ times_cont_diff ℝ ⊤ f ∧ is_compact (closure $ support f) ∧ closure (support f) ⊆ s := let ⟨f, hf⟩ := times_cont_diff_bump.exists_closure_support_subset hs in ⟨f, f.eventually_eq_one, λ y, ⟨f.nonneg, f.le_one⟩, f.times_cont_diff, f.compact_closure_support, hf⟩
33318a9e2152e4ccd4415695865e65f8cbc93e26
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/linear_algebra/sesquilinear_form.lean
7f97ce4f11024f7cf6aa553aff70a5d6fa6cf644
[ "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
8,673
lean
/- Copyright (c) 2018 Andreas Swerdlow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Andreas Swerdlow -/ import algebra.module ring_theory.maps /-! # Sesquilinear form This file defines a bilinear form over a module. The definition requires a ring antiautomorphism on the scalar ring, which comes from the file ring_theory.involution. Basic ideas such as orthogonality are also introduced. A sesquilinear form on an R-module M, is a function from M x M to R, that is linear in the first argument and antilinear in the second, with respect to an antiautomorphism on R (an antiisomorphism from R to R). ## Notations Given any term S of type sesq_form, due to a coercion, can use the notation S x y to refer to the function field, ie. S x y = S.bilin x y. ## References * <https://en.wikipedia.org/wiki/Sesquilinear_form#Over_arbitrary_rings> ## Tags Sesquilinear form, -/ open ring_anti_equiv universes u v /-- A sesquilinear form over a module -/ structure sesq_form (R : Type u) (M : Type v) [ring R] (I : ring_anti_equiv R R) [add_comm_group M] [module R M] := (sesq : M → M → R) (sesq_add_left : ∀ (x y z : M), sesq (x + y) z = sesq x z + sesq y z) (sesq_smul_left : ∀ (a : R) (x y : M), sesq (a • x) y = a * (sesq x y)) (sesq_add_right : ∀ (x y z : M), sesq x (y + z) = sesq x y + sesq x z) (sesq_smul_right : ∀ (a : R) (x y : M), sesq x (a • y) = (I a) * (sesq x y)) namespace sesq_form section general_ring variables {R : Type u} {M : Type v} [ring R] [add_comm_group M] [module R M] {I : ring_anti_equiv R R} {S : sesq_form R M I} instance : has_coe_to_fun (sesq_form R M I) := ⟨_, λ S, S.sesq⟩ lemma add_left (x y z : M) : S (x + y) z = S x z + S y z := sesq_add_left S x y z lemma smul_left (a : R) (x y : M) : S (a • x) y = a * (S x y) := sesq_smul_left S a x y lemma add_right (x y z : M) : S x (y + z) = S x y + S x z := sesq_add_right S x y z lemma smul_right (a : R) (x y : M) : S x (a • y) = (I a) * (S x y) := sesq_smul_right S a x y lemma zero_left (x : M) : S 0 x = 0 := by {rw [←@zero_smul R _ _ _ _ (0 : M), smul_left, zero_mul]} lemma zero_right (x : M) : S x 0 = 0 := by rw [←@zero_smul _ _ _ _ _ (0 : M), smul_right, map_zero, ring.zero_mul] lemma neg_left (x y : M) : S (-x) y = -(S x y) := by rw [←@neg_one_smul R _ _, smul_left, neg_one_mul] lemma neg_right (x y : M) : S x (-y) = -(S x y) := by rw [←@neg_one_smul R _ _, smul_right, map_neg_one, neg_one_mul] lemma sub_left (x y z : M) : S (x - y) z = S x z - S y z := by rw [sub_eq_add_neg, add_left, neg_left]; refl lemma sub_right (x y z : M) : S x (y - z) = S x y - S x z := by rw [sub_eq_add_neg, add_right, neg_right]; refl variable {D : sesq_form R M I} @[ext] lemma ext (H : ∀ (x y : M), S x y = D x y) : S = D := by {cases S, cases D, congr, funext, exact H _ _} instance : add_comm_group (sesq_form R M I) := { add := λ S D, { sesq := λ x y, S x y + D x y, sesq_add_left := λ x y z, by {rw add_left, rw add_left, ac_refl}, sesq_smul_left := λ a x y, by {rw [smul_left, smul_left, mul_add]}, sesq_add_right := λ x y z, by {rw add_right, rw add_right, ac_refl}, sesq_smul_right := λ a x y, by {rw [smul_right, smul_right, mul_add]} }, add_assoc := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq coe_fn has_coe_to_fun.coe sesq, rw add_assoc}, zero := { sesq := λ x y, 0, sesq_add_left := λ x y z, (add_zero 0).symm, sesq_smul_left := λ a x y, (mul_zero a).symm, sesq_add_right := λ x y z, (zero_add 0).symm, sesq_smul_right := λ a x y, (mul_zero (I a)).symm }, zero_add := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq, rw zero_add}, add_zero := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq, rw add_zero}, neg := λ S, { sesq := λ x y, - (S.1 x y), sesq_add_left := λ x y z, by rw [sesq_add_left, neg_add], sesq_smul_left := λ a x y, by rw [sesq_smul_left, mul_neg_eq_neg_mul_symm], sesq_add_right := λ x y z, by rw [sesq_add_right, neg_add], sesq_smul_right := λ a x y, by rw [sesq_smul_right, mul_neg_eq_neg_mul_symm] }, add_left_neg := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq, rw neg_add_self}, add_comm := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq, rw add_comm} } instance : inhabited (sesq_form R M I) := ⟨0⟩ /-- The proposition that two elements of a sesquilinear form space are orthogonal -/ def is_ortho (S : sesq_form R M I) (x y : M) : Prop := S x y = 0 lemma ortho_zero (x : M) : is_ortho S (0 : M) x := zero_left x end general_ring section comm_ring variables {R : Type*} [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {J : ring_anti_equiv R R} (F : sesq_form R M J) (f : M → M) instance to_module : module R (sesq_form R M J) := { smul := λ c S, { sesq := λ x y, c * S x y, sesq_add_left := λ x y z, by {unfold coe_fn has_coe_to_fun.coe sesq, rw [sesq_add_left, left_distrib]}, sesq_smul_left := λ a x y, by {unfold coe_fn has_coe_to_fun.coe sesq, rw [sesq_smul_left, ←mul_assoc, mul_comm c, mul_assoc]}, sesq_add_right := λ x y z, by {unfold coe_fn has_coe_to_fun.coe sesq, rw [sesq_add_right, left_distrib]}, sesq_smul_right := λ a x y, by {unfold coe_fn has_coe_to_fun.coe sesq, rw [sesq_smul_right, ←mul_assoc, mul_comm c, mul_assoc], refl} }, smul_add := λ c S D, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw left_distrib}, add_smul := λ c S D, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw right_distrib}, mul_smul := λ a c D, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw mul_assoc}, one_smul := λ S, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw one_mul}, zero_smul := λ S, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw zero_mul}, smul_zero := λ S, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw mul_zero} } end comm_ring section domain variables {R : Type*} [domain R] {M : Type v} [add_comm_group M] [module R M] {K : ring_anti_equiv R R} {G : sesq_form R M K} theorem ortho_smul_left {x y : M} {a : R} (ha : a ≠ 0) : (is_ortho G x y) ↔ (is_ortho G (a • x) y) := begin dunfold is_ortho, split; intro H, { rw [smul_left, H, ring.mul_zero] }, { rw [smul_left, mul_eq_zero] at H, cases H, { trivial }, { exact H }} end theorem ortho_smul_right {x y : M} {a : R} (ha : a ≠ 0) : (is_ortho G x y) ↔ (is_ortho G x (a • y)) := begin dunfold is_ortho, split; intro H, { rw [smul_right, H, ring.mul_zero] }, { rw [smul_right, mul_eq_zero] at H, cases H, { rw map_zero_iff at H, trivial }, { exact H }} end end domain end sesq_form namespace refl_sesq_form open refl_sesq_form sesq_form variables {R : Type*} {M : Type*} [ring R] [add_comm_group M] [module R M] {I : ring_anti_equiv R R} {S : sesq_form R M I} /-- The proposition that a sesquilinear form is reflexive -/ def is_refl (S : sesq_form R M I) : Prop := ∀ (x y : M), S x y = 0 → S y x = 0 variable (H : is_refl S) lemma eq_zero : ∀ {x y : M}, S x y = 0 → S y x = 0 := λ x y, H x y lemma ortho_sym {x y : M} : is_ortho S x y ↔ is_ortho S y x := ⟨eq_zero H, eq_zero H⟩ end refl_sesq_form namespace sym_sesq_form open sym_sesq_form sesq_form variables {R : Type*} {M : Type*} [ring R] [add_comm_group M] [module R M] {I : ring_anti_equiv R R} {S : sesq_form R M I} /-- The proposition that a sesquilinear form is symmetric -/ def is_sym (S : sesq_form R M I) : Prop := ∀ (x y : M), I (S x y) = S y x variable (H : is_sym S) include H lemma sym (x y : M) : I (S x y) = S y x := H x y lemma is_refl : refl_sesq_form.is_refl S := λ x y H1, by rw [←H, map_zero_iff, H1] lemma ortho_sym {x y : M} : is_ortho S x y ↔ is_ortho S y x := refl_sesq_form.ortho_sym (is_refl H) end sym_sesq_form namespace alt_sesq_form open alt_sesq_form sesq_form variables {R : Type*} {M : Type*} [ring R] [add_comm_group M] [module R M] {I : ring_anti_equiv R R} {S : sesq_form R M I} /-- The proposition that a sesquilinear form is alternating -/ def is_alt (S : sesq_form R M I) : Prop := ∀ (x : M), S x x = 0 variable (H : is_alt S) include H lemma self_eq_zero (x : M) : S x x = 0 := H x lemma neg (x y : M) : - S x y = S y x := begin have H1 : S (x + y) (x + y) = 0, { exact self_eq_zero H (x + y) }, rw [add_left, add_right, add_right, self_eq_zero H, self_eq_zero H, ring.zero_add, ring.add_zero, add_eq_zero_iff_neg_eq] at H1, exact H1, end end alt_sesq_form
22a9c74f2203d64d07c9da5e09bf0fe0d1332613
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/doc_string5.lean
ff36e7cc76c501d98d69d3679de36303dcceb7c4
[ "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
192
lean
/-- Doc string for constants -/ constant a : nat /-- Doc string for axiom -/ axiom ax : a = 0 /-- Doc string for meta constant -/ meta constant v : tactic unit /-- Doc string -/ def x := 1
171695caebf5d4bc603fd2c34f15f128abf65e2f
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/finset/option.lean
4f43df6ee3bfe280a2f764b42288eefbd4bf9115
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,388
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Mario Carneiro, Sean Leather -/ import data.finset.basic import order.preorder_hom /-! # Finite sets in `option α` In this file we define * `option.to_finset`: construct an empty or singleton `finset α` from an `option α`; * `finset.insert_none`: given `s : finset α`, lift it to a finset on `option α` using `option.some` and then insert `option.none`; * `finset.erase_none`: given `s : finset (option α)`, returns `t : finset α` such that `x ∈ t ↔ some x ∈ s`. Then we prove some basic lemmas about these definitions. ## Tags finset, option -/ variables {α β : Type*} open function namespace option /-- Construct an empty or singleton finset from an `option` -/ def to_finset (o : option α) : finset α := o.elim ∅ singleton @[simp] theorem to_finset_none : none.to_finset = (∅ : finset α) := rfl @[simp] theorem to_finset_some {a : α} : (some a).to_finset = {a} := rfl @[simp] theorem mem_to_finset {a : α} {o : option α} : a ∈ o.to_finset ↔ a ∈ o := by cases o; simp [eq_comm] theorem card_to_finset (o : option α) : o.to_finset.card = o.elim 0 1 := by cases o; refl end option namespace finset /-- Given a finset on `α`, lift it to being a finset on `option α` using `option.some` and then insert `option.none`. -/ def insert_none : finset α ↪o finset (option α) := order_embedding.of_map_le_iff (λ s, cons none (s.map embedding.some) $ by simp) $ λ s t, cons_subset_cons.trans map_subset_map /-⟨none ::ₘ s.1.map some, multiset.nodup_cons.2 ⟨by simp, multiset.nodup_map (λ a b, option.some.inj) s.2⟩⟩-/ @[simp] theorem 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 some_mem_insert_none {s : finset α} {a : α} : some a ∈ s.insert_none ↔ a ∈ s := by simp @[simp] theorem card_insert_none (s : finset α) : s.insert_none.card = s.card + 1 := by simp [insert_none] /-- Given `s : finset (option α)`, `s.erase_none : finset α` is the set of `x : α` such that `some x ∈ s`. -/ def erase_none : finset (option α) →ₘ finset α := (finset.map_embedding (equiv.option_is_some_equiv α).to_embedding).to_preorder_hom.comp ⟨finset.subtype _, subtype_mono⟩ @[simp] lemma mem_erase_none {s : finset (option α)} {x : α} : x ∈ s.erase_none ↔ some x ∈ s := by simp [erase_none] lemma erase_none_eq_bUnion [decidable_eq α] (s : finset (option α)) : s.erase_none = s.bUnion option.to_finset := by { ext, simp } @[simp] lemma erase_none_map_some (s : finset α) : (s.map embedding.some).erase_none = s := by { ext, simp } @[simp] lemma erase_none_image_some [decidable_eq (option α)] (s : finset α) : (s.image some).erase_none = s := by simpa only [map_eq_image] using erase_none_map_some s @[simp] lemma coe_erase_none (s : finset (option α)) : (s.erase_none : set α) = some ⁻¹' s := set.ext $ λ x, mem_erase_none @[simp] lemma erase_none_union [decidable_eq (option α)] [decidable_eq α] (s t : finset (option α)) : (s ∪ t).erase_none = s.erase_none ∪ t.erase_none := by { ext, simp } @[simp] lemma erase_none_inter [decidable_eq (option α)] [decidable_eq α] (s t : finset (option α)) : (s ∩ t).erase_none = s.erase_none ∩ t.erase_none := by { ext, simp } @[simp] lemma erase_none_empty : (∅ : finset (option α)).erase_none = ∅ := by { ext, simp } @[simp] lemma erase_none_none : ({none} : finset (option α)).erase_none = ∅ := by { ext, simp } @[simp] lemma image_some_erase_none [decidable_eq (option α)] (s : finset (option α)) : s.erase_none.image some = s.erase none := by ext (_|x); simp @[simp] lemma map_some_erase_none [decidable_eq (option α)] (s : finset (option α)) : s.erase_none.map embedding.some = s.erase none := by rw [map_eq_image, embedding.some_apply, image_some_erase_none] @[simp] lemma insert_none_erase_none [decidable_eq (option α)] (s : finset (option α)) : insert_none (erase_none s) = insert none s := by ext (_|x); simp @[simp] lemma erase_none_insert_none (s : finset α) : erase_none (insert_none s) = s := by { ext, simp } end finset
e5ca1f94b6739052869e427f912cfca010681cb7
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/constant_speed.lean
c6ac9e016baf75f195f51a382f897202e6507861
[ "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
13,463
lean
/- Copyright (c) 2023 Rémi Bottinelli. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémi Bottinelli -/ import data.set.function import analysis.bounded_variation import tactic.swap_var /-! # Constant speed This file defines the notion of constant (and unit) speed for a function `f : ℝ → E` with pseudo-emetric structure on `E` with respect to a set `s : set ℝ` and "speed" `l : ℝ≥0`, and shows that if `f` has locally bounded variation on `s`, it can be obtained (up to distance zero, on `s`), as a composite `φ ∘ (variation_on_from_to f s a)`, where `φ` has unit speed and `a ∈ s`. ## Main definitions * `has_constant_speed_on_with f s l`, stating that the speed of `f` on `s` is `l`. * `has_unit_speed_on f s`, stating that the speed of `f` on `s` is `1`. * `natural_parameterization f s a : ℝ → E`, the unit speed reparameterization of `f` on `s` relative to `a`. ## Main statements * `unique_unit_speed_on_Icc_zero` proves that if `f` and `f ∘ φ` are both naturally parameterized on closed intervals starting at `0`, then `φ` must be the identity on those intervals. * `edist_natural_parameterization_eq_zero` proves that if `f` has locally bounded variation, then precomposing `natural_parameterization f s a` with `variation_on_from_to f s a` yields a function at distance zero from `f` on `s`. * `has_unit_speed_natural_parameterization` proves that if `f` has locally bounded variation, then `natural_parameterization f s a` has unit speed on `s`. ## Tags arc-length, parameterization -/ open_locale big_operators nnreal ennreal open set measure_theory classical variables {α : Type*} [linear_order α] {E : Type*} [pseudo_emetric_space E] variables (f : ℝ → E) (s : set ℝ) (l : ℝ≥0) /-- `f` has constant speed `l` on `s` if the variation of `f` on `s ∩ Icc x y` is equal to `l * (y - x)` for any `x y` in `s`. -/ def has_constant_speed_on_with := ∀ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), evariation_on f (s ∩ Icc x y) = ennreal.of_real (l * (y - x)) variables {f} {s} {l} lemma has_constant_speed_on_with.has_locally_bounded_variation_on (h : has_constant_speed_on_with f s l) : has_locally_bounded_variation_on f s := λ x y hx hy, by simp only [has_bounded_variation_on, h hx hy, ne.def, ennreal.of_real_ne_top, not_false_iff] lemma has_constant_speed_on_with_of_subsingleton (f : ℝ → E) {s : set ℝ} (hs : s.subsingleton) (l : ℝ≥0) : has_constant_speed_on_with f s l := begin rintro x hx y hy, cases hs hx hy, rw evariation_on.subsingleton f (λ y hy z hz, hs hy.1 hz.1 : (s ∩ Icc x x).subsingleton), simp only [sub_self, mul_zero, ennreal.of_real_zero], end lemma has_constant_speed_on_with_iff_ordered : has_constant_speed_on_with f s l ↔ ∀ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), (x ≤ y) → evariation_on f (s ∩ Icc x y) = ennreal.of_real (l * (y - x)) := begin refine ⟨λ h x xs y ys xy, h xs ys, λ h x xs y ys, _⟩, rcases le_total x y with xy|yx, { exact h xs ys xy, }, { rw [evariation_on.subsingleton, ennreal.of_real_of_nonpos], { exact mul_nonpos_of_nonneg_of_nonpos l.prop (sub_nonpos_of_le yx), }, { rintro z ⟨zs, xz, zy⟩ w ⟨ws, xw, wy⟩, cases le_antisymm (zy.trans yx) xz, cases le_antisymm (wy.trans yx) xw, refl, }, }, end lemma has_constant_speed_on_with_iff_variation_on_from_to_eq : has_constant_speed_on_with f s l ↔ (has_locally_bounded_variation_on f s ∧ ∀ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), variation_on_from_to f s x y = l * (y - x)) := begin split, { rintro h, refine ⟨h.has_locally_bounded_variation_on, λ x xs y ys, _⟩, rw has_constant_speed_on_with_iff_ordered at h, rcases le_total x y with xy|yx, { rw [variation_on_from_to.eq_of_le f s xy, h xs ys xy, ennreal.to_real_of_real (mul_nonneg l.prop (sub_nonneg.mpr xy))], }, { rw [variation_on_from_to.eq_of_ge f s yx, h ys xs yx, ennreal.to_real_of_real (mul_nonneg l.prop (sub_nonneg.mpr yx)), mul_comm ↑l, mul_comm ↑l, ←neg_mul, neg_sub], }, }, { rw has_constant_speed_on_with_iff_ordered, rintro h x xs y ys xy, rw [←h.2 xs ys, variation_on_from_to.eq_of_le f s xy, ennreal.of_real_to_real (h.1 x y xs ys)], }, end lemma has_constant_speed_on_with.union {t : set ℝ} (hfs : has_constant_speed_on_with f s l) (hft : has_constant_speed_on_with f t l) {x : ℝ} (hs : is_greatest s x) (ht : is_least t x) : has_constant_speed_on_with f (s ∪ t) l := begin rw has_constant_speed_on_with_iff_ordered at hfs hft ⊢, rintro z (zs|zt) y (ys|yt) zy, { have : (s ∪ t) ∩ Icc z y = (s ∩ Icc z y), by { ext w, split, { rintro ⟨(ws|wt), zw, wy⟩, { exact ⟨ws, zw, wy⟩, }, { exact ⟨(le_antisymm (wy.trans (hs.2 ys)) (ht.2 wt)).symm ▸ hs.1, zw, wy⟩, }, }, { rintro ⟨ws, zwy⟩, exact ⟨or.inl ws, zwy⟩, }, }, rw [this, hfs zs ys zy], }, { have : (s ∪ t) ∩ Icc z y = (s ∩ Icc z x) ∪ (t ∩ Icc x y), by { ext w, split, { rintro ⟨(ws|wt), zw, wy⟩, exacts [or.inl ⟨ws, zw, hs.2 ws⟩, or.inr ⟨wt, ht.2 wt, wy⟩], }, { rintro (⟨ws, zw, wx⟩|⟨wt, xw, wy⟩), exacts [⟨or.inl ws, zw, wx.trans (ht.2 yt)⟩, ⟨or.inr wt, (hs.2 zs).trans xw, wy⟩], }, }, rw [this, @evariation_on.union _ _ _ _ f _ _ x, hfs zs hs.1 (hs.2 zs), hft ht.1 yt (ht.2 yt), ←ennreal.of_real_add (mul_nonneg l.prop (sub_nonneg.mpr (hs.2 zs))) (mul_nonneg l.prop (sub_nonneg.mpr (ht.2 yt))) ], ring_nf, exacts [⟨⟨hs.1, hs.2 zs, le_rfl⟩, λ w ⟨ws, zw, wx⟩, wx⟩, ⟨⟨ht.1, le_rfl, ht.2 yt⟩, λ w ⟨wt, xw, wy⟩, xw⟩], }, { cases le_antisymm zy ((hs.2 ys).trans (ht.2 zt)), simp only [Icc_self, sub_self, mul_zero, ennreal.of_real_zero], exact evariation_on.subsingleton _ (λ _ ⟨_, uz⟩ _ ⟨_, vz⟩, uz.trans vz.symm), }, { have : (s ∪ t) ∩ Icc z y = (t ∩ Icc z y), by { ext w, split, { rintro ⟨(ws|wt), zw, wy⟩, { exact ⟨(le_antisymm ((ht.2 zt).trans zw) (hs.2 ws)) ▸ ht.1, zw, wy⟩, }, { exact ⟨wt, zw, wy⟩, }, }, { rintro ⟨wt, zwy⟩, exact ⟨or.inr wt, zwy⟩, }, }, rw [this, hft zt yt zy], } end lemma has_constant_speed_on_with.Icc_Icc {x y z : ℝ} (hfs : has_constant_speed_on_with f (Icc x y) l) (hft : has_constant_speed_on_with f (Icc y z) l) : has_constant_speed_on_with f (Icc x z) l := begin rcases le_total x y with xy|yx, rcases le_total y z with yz|zy, { rw ←set.Icc_union_Icc_eq_Icc xy yz, exact hfs.union hft (is_greatest_Icc xy) (is_least_Icc yz), }, { rintro u ⟨xu, uz⟩ v ⟨xv, vz⟩, rw [Icc_inter_Icc, sup_of_le_right xu, inf_of_le_right vz, ←hfs ⟨xu, uz.trans zy⟩ ⟨xv, vz.trans zy⟩, Icc_inter_Icc, sup_of_le_right xu, inf_of_le_right (vz.trans zy)], }, { rintro u ⟨xu, uz⟩ v ⟨xv, vz⟩, rw [Icc_inter_Icc, sup_of_le_right xu, inf_of_le_right vz, ←hft ⟨yx.trans xu, uz⟩ ⟨yx.trans xv, vz⟩, Icc_inter_Icc, sup_of_le_right (yx.trans xu), inf_of_le_right (vz)], }, end lemma has_constant_speed_on_with_zero_iff : has_constant_speed_on_with f s 0 ↔ ∀ x y ∈ s, edist (f x) (f y) = 0 := begin dsimp [has_constant_speed_on_with], simp only [zero_mul, ennreal.of_real_zero, ←evariation_on.eq_zero_iff], split, { by_contra', obtain ⟨h, hfs⟩ := this, simp_rw evariation_on.eq_zero_iff at hfs h, push_neg at hfs, obtain ⟨x, xs, y, ys, hxy⟩ := hfs, rcases le_total x y with xy|yx, { exact hxy (h xs ys x ⟨xs, le_rfl, xy⟩ y ⟨ys, xy, le_rfl⟩), }, { rw edist_comm at hxy, exact hxy (h ys xs y ⟨ys, le_rfl, yx⟩ x ⟨xs, yx, le_rfl⟩), }, }, { rintro h x xs y ys, refine le_antisymm _ (zero_le'), rw ←h, exact evariation_on.mono f (inter_subset_left s (Icc x y)), }, end lemma has_constant_speed_on_with.ratio {l' : ℝ≥0} (hl' : l' ≠ 0) {φ : ℝ → ℝ} (φm : monotone_on φ s) (hfφ : has_constant_speed_on_with (f ∘ φ) s l) (hf : has_constant_speed_on_with f (φ '' s) l') ⦃x : ℝ⦄ (xs : x ∈ s) : eq_on φ (λ y, (l / l') * (y - x) + (φ x)) s := begin rintro y ys, rw [←sub_eq_iff_eq_add, mul_comm, ←mul_div_assoc, eq_div_iff (nnreal.coe_ne_zero.mpr hl')], rw has_constant_speed_on_with_iff_variation_on_from_to_eq at hf, rw has_constant_speed_on_with_iff_variation_on_from_to_eq at hfφ, symmetry, calc (y - x) * l = l * (y - x) : by rw mul_comm ... = variation_on_from_to (f ∘ φ) s x y : (hfφ.2 xs ys).symm ... = variation_on_from_to f (φ '' s) (φ x) (φ y) : variation_on_from_to.comp_eq_of_monotone_on f φ φm xs ys ... = l' * (φ y - φ x) : hf.2 ⟨x,xs,rfl⟩ ⟨y,ys,rfl⟩ ... = (φ y - φ x) * l' : by rw mul_comm, end /-- `f` has unit speed on `s` if it is linearly parameterized by `l = 1` on `s`. -/ def has_unit_speed_on (f : ℝ → E) (s : set ℝ) := has_constant_speed_on_with f s 1 lemma has_unit_speed_on.union {t : set ℝ} {x : ℝ} (hfs : has_unit_speed_on f s) (hft : has_unit_speed_on f t) (hs : is_greatest s x) (ht : is_least t x) : has_unit_speed_on f (s ∪ t) := has_constant_speed_on_with.union hfs hft hs ht lemma has_unit_speed_on.Icc_Icc {x y z : ℝ} (hfs : has_unit_speed_on f (Icc x y)) (hft : has_unit_speed_on f (Icc y z)) : has_unit_speed_on f (Icc x z) := has_constant_speed_on_with.Icc_Icc hfs hft /-- If both `f` and `f ∘ φ` have unit speed (on `t` and `s` respectively) and `φ` monotonically maps `s` onto `t`, then `φ` is just a translation (on `s`). -/ lemma unique_unit_speed {φ : ℝ → ℝ} (φm : monotone_on φ s) (hfφ : has_unit_speed_on (f ∘ φ) s) (hf : has_unit_speed_on f (φ '' s)) ⦃x : ℝ⦄ (xs : x ∈ s) : eq_on φ (λ y, (y - x) + (φ x)) s := begin dsimp only [has_unit_speed_on] at hf hfφ, convert has_constant_speed_on_with.ratio one_ne_zero φm hfφ hf xs, simp only [nonneg.coe_one, div_self, ne.def, one_ne_zero, not_false_iff, one_mul], end /-- If both `f` and `f ∘ φ` have unit speed (on `Icc 0 t` and `Icc 0 s` respectively) and `φ` monotonically maps `Icc 0 s` onto `Icc 0 t`, then `φ` is the identity on `Icc 0 s` -/ lemma unique_unit_speed_on_Icc_zero {s t : ℝ} (hs : 0 ≤ s) (ht : 0 ≤ t) {φ : ℝ → ℝ} (φm : monotone_on φ $ Icc 0 s) (φst : φ '' (Icc 0 s) = (Icc 0 t)) (hfφ : has_unit_speed_on (f ∘ φ) (Icc 0 s)) (hf : has_unit_speed_on f (Icc 0 t)) : eq_on φ id (Icc 0 s) := begin rw ←φst at hf, convert unique_unit_speed φm hfφ hf ⟨le_rfl, hs⟩, have : φ 0 = 0, by { obtain ⟨x,xs,hx⟩ := φst.rec_on (surj_on_image φ (Icc 0 s)) ⟨le_rfl, ht⟩, exact le_antisymm (hx.rec_on (φm ⟨le_rfl,hs⟩ xs xs.1)) (φst.rec_on (maps_to_image φ (Icc 0 s)) (⟨le_rfl, hs⟩)).1, }, simp only [tsub_zero, this, add_zero], refl, end /-- The natural parameterization of `f` on `s`, which, if `f` has locally bounded variation on `s`, * has unit speed on `s` (by `natural_parameterization_has_unit_speed`). * composed with `variation_on_from_to f s a`, is at distance zero from `f` (by `natural_parameterization_edist_zero`). -/ noncomputable def natural_parameterization (f : α → E) (s : set α) (a : α) : ℝ → E := f ∘ (@function.inv_fun_on _ _ ⟨a⟩ (variation_on_from_to f s a) s) lemma edist_natural_parameterization_eq_zero {f : α → E} {s : set α} (hf : has_locally_bounded_variation_on f s) {a : α} (as : a ∈ s) {b : α} (bs : b ∈ s) : edist (natural_parameterization f s a (variation_on_from_to f s a b)) (f b) = 0 := begin dsimp only [natural_parameterization], haveI : nonempty α := ⟨a⟩, let c := function.inv_fun_on (variation_on_from_to f s a) s (variation_on_from_to f s a b), obtain ⟨cs, hc⟩ := @function.inv_fun_on_pos _ _ _ s (variation_on_from_to f s a) (variation_on_from_to f s a b) ⟨b, bs, rfl⟩, rw [variation_on_from_to.eq_left_iff hf as cs bs] at hc, apply variation_on_from_to.edist_zero_of_eq_zero hf cs bs hc, end lemma has_unit_speed_natural_parameterization (f : α → E) {s : set α} (hf : has_locally_bounded_variation_on f s) {a : α} (as : a ∈ s) : has_unit_speed_on (natural_parameterization f s a) (variation_on_from_to f s a '' s) := begin dsimp only [has_unit_speed_on], rw has_constant_speed_on_with_iff_ordered, rintro _ ⟨b, bs, rfl⟩ _ ⟨c, cs, rfl⟩ h, rcases le_total c b with cb|bc, { rw [nnreal.coe_one, one_mul, le_antisymm h (variation_on_from_to.monotone_on hf as cs bs cb), sub_self, ennreal.of_real_zero, Icc_self, evariation_on.subsingleton], exact λ x hx y hy, hx.2.trans hy.2.symm, }, { rw [nnreal.coe_one, one_mul, sub_eq_add_neg, variation_on_from_to.eq_neg_swap, neg_neg, add_comm, variation_on_from_to.add hf bs as cs, ←variation_on_from_to.eq_neg_swap f], rw [←evariation_on.comp_inter_Icc_eq_of_monotone_on (natural_parameterization f s a) _ (variation_on_from_to.monotone_on hf as) bs cs], rw [@evariation_on.eq_of_edist_zero_on _ _ _ _ _ f], { rw [variation_on_from_to.eq_of_le _ _ bc, ennreal.of_real_to_real (hf b c bs cs)], }, { rintro x ⟨xs, bx, xc⟩, exact edist_natural_parameterization_eq_zero hf as xs, }, }, end
83c0f242137e472cd20c24e73b093d8f58612467
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/module/pi.lean
bb91f6ee2bcd9b807da3991b5410c624735c7b5f
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
2,725
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot -/ import algebra.module.basic import algebra.regular.smul import algebra.ring.pi import group_theory.group_action.pi /-! # Pi instances for modules This file defines instances for module and related structures on Pi Types -/ universes u v w variable {I : Type u} -- The indexing type variable {f : I → Type v} -- The family of types already equipped with instances variables (x y : Π i, f i) (i : I) namespace pi lemma _root_.is_smul_regular.pi {α : Type*} [Π i, has_scalar α $ f i] {k : α} (hk : Π i, is_smul_regular (f i) k) : is_smul_regular (Π i, f i) k := λ _ _ h, funext $ λ i, hk i (congr_fun h i : _) instance smul_with_zero (α) [has_zero α] [Π i, has_zero (f i)] [Π i, smul_with_zero α (f i)] : smul_with_zero α (Π i, f i) := { smul_zero := λ _, funext $ λ _, smul_zero' (f _) _, zero_smul := λ _, funext $ λ _, zero_smul _ _, ..pi.has_scalar } instance smul_with_zero' {g : I → Type*} [Π i, has_zero (g i)] [Π i, has_zero (f i)] [Π i, smul_with_zero (g i) (f i)] : smul_with_zero (Π i, g i) (Π i, f i) := { smul_zero := λ _, funext $ λ _, smul_zero' (f _) _, zero_smul := λ _, funext $ λ _, zero_smul _ _, ..pi.has_scalar' } instance mul_action_with_zero (α) [monoid_with_zero α] [Π i, has_zero (f i)] [Π i, mul_action_with_zero α (f i)] : mul_action_with_zero α (Π i, f i) := { ..pi.mul_action _, ..pi.smul_with_zero _ } instance mul_action_with_zero' {g : I → Type*} [Π i, monoid_with_zero (g i)] [Π i, has_zero (f i)] [Π i, mul_action_with_zero (g i) (f i)] : mul_action_with_zero (Π i, g i) (Π i, f i) := { ..pi.mul_action', ..pi.smul_with_zero' } variables (I f) instance module (α) {r : semiring α} {m : ∀ i, add_comm_monoid $ f i} [∀ i, module α $ f i] : @module α (Π i : I, f i) r (@pi.add_comm_monoid I f m) := { add_smul := λ c f g, funext $ λ i, add_smul _ _ _, zero_smul := λ f, funext $ λ i, zero_smul α _, ..pi.distrib_mul_action _ } variables {I f} instance module' {g : I → Type*} {r : Π i, semiring (f i)} {m : Π i, add_comm_monoid (g i)} [Π i, module (f i) (g i)] : module (Π i, f i) (Π i, g i) := { add_smul := by { intros, ext1, apply add_smul }, zero_smul := by { intros, ext1, apply zero_smul } } instance (α) {r : semiring α} {m : Π i, add_comm_monoid $ f i} [Π i, module α $ f i] [∀ i, no_zero_smul_divisors α $ f i] : no_zero_smul_divisors α (Π i : I, f i) := ⟨λ c x h, or_iff_not_imp_left.mpr (λ hc, funext (λ i, (smul_eq_zero.mp (congr_fun h i)).resolve_left hc))⟩ end pi
ff8e9727bbc255e0a345426bd140b621aa1354b8
75c54c8946bb4203e0aaf196f918424a17b0de99
/old/term-jesse.lean
e6c84710b1ef12e0385553fdc7338b45fdb4534a
[ "Apache-2.0" ]
permissive
urkud/flypitch
261e2a45f1038130178575406df8aea78255ba77
2250f5eda14b6ef9fc3e4e1f4a9ac4005634de5c
refs/heads/master
1,653,266,469,246
1,577,819,679,000
1,577,819,679,000
259,862,235
1
0
Apache-2.0
1,588,147,244,000
1,588,147,244,000
null
UTF-8
Lean
false
false
1,787
lean
/-- Authors: Andrew Tindall, Jesse Han --/ structure Language := language :: (consts : Type) (relations : Π n : nat, Type) (functions : Π n : nat, Type) variable L : Language inductive term : ℕ → Type | nil {} : term 0 | const : L.consts → term 1 | var : nat → term 1 | apply : ∀ (n : nat), ∀ (f : L.functions n), term n → term 1 | conj : ∀ (n : nat), term 1 → term n → term (n + 1) inductive formula : Type | equals : term L 1 → term L 1 → formula | apply : Π n : nat, L.relations n → term L n → formula | and : formula → formula → formula | or : formula → formula → formula | not : formula → formula | ex : formula → formula | all : formula → formula def substitution_map_on_term (v : ℕ) (t : term L 1) : Π n : ℕ, term L n → term L n | 0 t := term.nil | k (term.var _ v) := t | 1 (term.apply n f s) := (term.apply n f) (substitution_map_on_term n s) | 1 (term.const c) := term.const c | (nat.succ k) (term.conj n a s) := (term.conj n (substitution_map_on_term 1 a) (substitution_map_on_term k s)) def substitution_map_on_formula (v : ℕ) (t : term L 1) : formula L → formula L | (formula.equals t1 t2) := (formula.equals (substitution_map_on_term L v t 1 t1) (substitution_map_on_term L v t 1 t2)) | (formula.apply n R s) := formula.apply n R (substitution_map_on_term L v t n s) | (formula.and A B) := formula.and (substitution_map_on_formula A) (substitution_map_on_formula B) | (formula.or A B) := formula.or (substitution_map_on_formula A) (substitution_map_on_formula B) | (formula.ex A) := formula.ex (substitution_map_on_formula A) | (formula.all A) := formula.all (substitution_map_on_formula A) | (formula.not A) := formula.not (substitution_map_on_formula A)
80456f991d73a753950c44a043cf36e94852bb78
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/setoid/basic.lean
0ee7b3b8eadf0fbf77139574e5fe8eb7b8c47f43
[ "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
18,387
lean
/- Copyright (c) 2019 Amelia Livingston. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Amelia Livingston, Bryan Gin-ge Chen -/ import order.galois_connection /-! # Equivalence relations This file defines the complete lattice of equivalence relations on a type, results about the inductively defined equivalence closure of a binary relation, and the analogues of some isomorphism theorems for quotients of arbitrary types. ## Implementation notes The function `rel` and lemmas ending in ' make it easier to talk about different equivalence relations on the same type. The complete lattice instance for equivalence relations could have been defined by lifting the Galois insertion of equivalence relations on α into binary relations on α, and then using `complete_lattice.copy` to define a complete lattice instance with more appropriate definitional equalities (a similar example is `filter.complete_lattice` in `order/filter/basic.lean`). This does not save space, however, and is less clear. Partitions are not defined as a separate structure here; users are encouraged to reason about them using the existing `setoid` and its infrastructure. ## Tags setoid, equivalence, iseqv, relation, equivalence relation -/ variables {α : Type*} {β : Type*} /-- A version of `setoid.r` that takes the equivalence relation as an explicit argument. -/ def setoid.rel (r : setoid α) : α → α → Prop := @setoid.r _ r /-- A version of `quotient.eq'` compatible with `setoid.rel`, to make rewriting possible. -/ lemma quotient.eq_rel {r : setoid α} {x y} : (quotient.mk' x : quotient r) = quotient.mk' y ↔ r.rel x y := quotient.eq namespace setoid @[ext] lemma ext' {r s : setoid α} (H : ∀ a b, r.rel a b ↔ s.rel a b) : r = s := ext H lemma ext_iff {r s : setoid α} : r = s ↔ ∀ a b, r.rel a b ↔ s.rel a b := ⟨λ h a b, h ▸ iff.rfl, ext'⟩ /-- Two equivalence relations are equal iff their underlying binary operations are equal. -/ theorem eq_iff_rel_eq {r₁ r₂ : setoid α} : r₁ = r₂ ↔ r₁.rel = r₂.rel := ⟨λ h, h ▸ rfl, λ h, setoid.ext' $ λ x y, h ▸ iff.rfl⟩ /-- Defining `≤` for equivalence relations. -/ instance : has_le (setoid α) := ⟨λ r s, ∀ ⦃x y⦄, r.rel x y → s.rel x y⟩ theorem le_def {r s : setoid α} : r ≤ s ↔ ∀ {x y}, r.rel x y → s.rel x y := iff.rfl @[refl] lemma refl' (r : setoid α) (x) : r.rel x x := r.2.1 x @[symm] lemma symm' (r : setoid α) : ∀ {x y}, r.rel x y → r.rel y x := λ _ _ h, r.2.2.1 h @[trans] lemma trans' (r : setoid α) : ∀ {x y z}, r.rel x y → r.rel y z → r.rel x z := λ _ _ _ hx, r.2.2.2 hx lemma comm' (s : setoid α) {x y} : s.rel x y ↔ s.rel y x := ⟨s.symm', s.symm'⟩ /-- The kernel of a function is an equivalence relation. -/ def ker (f : α → β) : setoid α := ⟨(=) on f, eq_equivalence.comap f⟩ /-- The kernel of the quotient map induced by an equivalence relation r equals r. -/ @[simp] lemma ker_mk_eq (r : setoid α) : ker (@quotient.mk _ r) = r := ext' $ λ x y, quotient.eq lemma ker_apply_mk_out {f : α → β} (a : α) : f (by haveI := setoid.ker f; exact ⟦a⟧.out) = f a := @quotient.mk_out _ (setoid.ker f) a lemma ker_apply_mk_out' {f : α → β} (a : α) : f ((quotient.mk' a : quotient $ setoid.ker f).out') = f a := @quotient.mk_out' _ (setoid.ker f) a lemma ker_def {f : α → β} {x y : α} : (ker f).rel x y ↔ f x = f y := iff.rfl /-- Given types `α`, `β`, the product of two equivalence relations `r` on `α` and `s` on `β`: `(x₁, x₂), (y₁, y₂) ∈ α × β` are related by `r.prod s` iff `x₁` is related to `y₁` by `r` and `x₂` is related to `y₂` by `s`. -/ protected def prod (r : setoid α) (s : setoid β) : setoid (α × β) := { r := λ x y, r.rel x.1 y.1 ∧ s.rel x.2 y.2, iseqv := ⟨λ x, ⟨r.refl' x.1, s.refl' x.2⟩, λ _ _ h, ⟨r.symm' h.1, s.symm' h.2⟩, λ _ _ _ h1 h2, ⟨r.trans' h1.1 h2.1, s.trans' h1.2 h2.2⟩⟩ } /-- The infimum of two equivalence relations. -/ instance : has_inf (setoid α) := ⟨λ r s, ⟨λ x y, r.rel x y ∧ s.rel x y, ⟨λ x, ⟨r.refl' x, s.refl' x⟩, λ _ _ h, ⟨r.symm' h.1, s.symm' h.2⟩, λ _ _ _ h1 h2, ⟨r.trans' h1.1 h2.1, s.trans' h1.2 h2.2⟩⟩⟩⟩ /-- The infimum of 2 equivalence relations r and s is the same relation as the infimum of the underlying binary operations. -/ lemma inf_def {r s : setoid α} : (r ⊓ s).rel = r.rel ⊓ s.rel := rfl theorem inf_iff_and {r s : setoid α} {x y} : (r ⊓ s).rel x y ↔ r.rel x y ∧ s.rel x y := iff.rfl /-- The infimum of a set of equivalence relations. -/ instance : has_Inf (setoid α) := ⟨λ S, ⟨λ x y, ∀ r ∈ S, rel r x y, ⟨λ x r hr, r.refl' x, λ _ _ h r hr, r.symm' $ h r hr, λ _ _ _ h1 h2 r hr, r.trans' (h1 r hr) $ h2 r hr⟩⟩⟩ /-- The underlying binary operation of the infimum of a set of equivalence relations is the infimum of the set's image under the map to the underlying binary operation. -/ theorem Inf_def {s : set (setoid α)} : (Inf s).rel = Inf (rel '' s) := by { ext, simp only [Inf_image, infi_apply, infi_Prop_eq], refl } instance : partial_order (setoid α) := { le := (≤), lt := λ r s, r ≤ s ∧ ¬s ≤ r, le_refl := λ _ _ _, id, le_trans := λ _ _ _ hr hs _ _ h, hs $ hr h, lt_iff_le_not_le := λ _ _, iff.rfl, le_antisymm := λ r s h1 h2, setoid.ext' $ λ x y, ⟨λ h, h1 h, λ h, h2 h⟩ } /-- The complete lattice of equivalence relations on a type, with bottom element `=` and top element the trivial equivalence relation. -/ instance complete_lattice : complete_lattice (setoid α) := { inf := has_inf.inf, inf_le_left := λ _ _ _ _ h, h.1, inf_le_right := λ _ _ _ _ h, h.2, le_inf := λ _ _ _ h1 h2 _ _ h, ⟨h1 h, h2 h⟩, top := ⟨λ _ _, true, ⟨λ _, trivial, λ _ _ h, h, λ _ _ _ h1 h2, h1⟩⟩, le_top := λ _ _ _ _, trivial, bot := ⟨(=), ⟨λ _, rfl, λ _ _ h, h.symm, λ _ _ _ h1 h2, h1.trans h2⟩⟩, bot_le := λ r x y h, h ▸ r.2.1 x, .. complete_lattice_of_Inf (setoid α) $ assume s, ⟨λ r hr x y h, h _ hr, λ r hr x y h r' hr', hr hr' h⟩ } @[simp] lemma top_def : (⊤ : setoid α).rel = ⊤ := rfl @[simp] lemma bot_def : (⊥ : setoid α).rel = (=) := rfl lemma eq_top_iff {s : setoid α} : s = (⊤ : setoid α) ↔ ∀ x y : α, s.rel x y := by simp [eq_top_iff, setoid.le_def, setoid.top_def, pi.top_apply] /-- The inductively defined equivalence closure of a binary relation r is the infimum of the set of all equivalence relations containing r. -/ theorem eqv_gen_eq (r : α → α → Prop) : eqv_gen.setoid r = Inf {s : setoid α | ∀ ⦃x y⦄, r x y → s.rel x y} := le_antisymm (λ _ _ H, eqv_gen.rec (λ _ _ h _ hs, hs h) (refl' _) (λ _ _ _, symm' _) (λ _ _ _ _ _, trans' _) H) (Inf_le $ λ _ _ h, eqv_gen.rel _ _ h) /-- The supremum of two equivalence relations r and s is the equivalence closure of the binary relation `x is related to y by r or s`. -/ lemma sup_eq_eqv_gen (r s : setoid α) : r ⊔ s = eqv_gen.setoid (λ x y, r.rel x y ∨ s.rel x y) := begin rw eqv_gen_eq, apply congr_arg Inf, simp only [le_def, or_imp_distrib, ← forall_and_distrib] end /-- The supremum of 2 equivalence relations r and s is the equivalence closure of the supremum of the underlying binary operations. -/ lemma sup_def {r s : setoid α} : r ⊔ s = eqv_gen.setoid (r.rel ⊔ s.rel) := by rw sup_eq_eqv_gen; refl /-- The supremum of a set S of equivalence relations is the equivalence closure of the binary relation `there exists r ∈ S relating x and y`. -/ lemma Sup_eq_eqv_gen (S : set (setoid α)) : Sup S = eqv_gen.setoid (λ x y, ∃ r : setoid α, r ∈ S ∧ r.rel x y) := begin rw eqv_gen_eq, apply congr_arg Inf, simp only [upper_bounds, le_def, and_imp, exists_imp_distrib], ext, exact ⟨λ H x y r hr, H hr, λ H r hr x y, H r hr⟩ end /-- The supremum of a set of equivalence relations is the equivalence closure of the supremum of the set's image under the map to the underlying binary operation. -/ lemma Sup_def {s : set (setoid α)} : Sup s = eqv_gen.setoid (Sup (rel '' s)) := begin rw [Sup_eq_eqv_gen, Sup_image], congr' with x y, simp only [supr_apply, supr_Prop_eq, exists_prop] end /-- The equivalence closure of an equivalence relation r is r. -/ @[simp] lemma eqv_gen_of_setoid (r : setoid α) : eqv_gen.setoid r.r = r := le_antisymm (by rw eqv_gen_eq; exact Inf_le (λ _ _, id)) eqv_gen.rel /-- Equivalence closure is idempotent. -/ @[simp] lemma eqv_gen_idem (r : α → α → Prop) : eqv_gen.setoid (eqv_gen.setoid r).rel = eqv_gen.setoid r := eqv_gen_of_setoid _ /-- The equivalence closure of a binary relation r is contained in any equivalence relation containing r. -/ theorem eqv_gen_le {r : α → α → Prop} {s : setoid α} (h : ∀ x y, r x y → s.rel x y) : eqv_gen.setoid r ≤ s := by rw eqv_gen_eq; exact Inf_le h /-- Equivalence closure of binary relations is monotone. -/ theorem eqv_gen_mono {r s : α → α → Prop} (h : ∀ x y, r x y → s x y) : eqv_gen.setoid r ≤ eqv_gen.setoid s := eqv_gen_le $ λ _ _ hr, eqv_gen.rel _ _ $ h _ _ hr /-- There is a Galois insertion of equivalence relations on α into binary relations on α, with equivalence closure the lower adjoint. -/ def gi : @galois_insertion (α → α → Prop) (setoid α) _ _ eqv_gen.setoid rel := { choice := λ r h, eqv_gen.setoid r, gc := λ r s, ⟨λ H _ _ h, H $ eqv_gen.rel _ _ h, λ H, eqv_gen_of_setoid s ▸ eqv_gen_mono H⟩, le_l_u := λ x, (eqv_gen_of_setoid x).symm ▸ le_refl x, choice_eq := λ _ _, rfl } open function /-- A function from α to β is injective iff its kernel is the bottom element of the complete lattice of equivalence relations on α. -/ theorem injective_iff_ker_bot (f : α → β) : injective f ↔ ker f = ⊥ := (@eq_bot_iff (setoid α) _ _ (ker f)).symm /-- The elements related to x ∈ α by the kernel of f are those in the preimage of f(x) under f. -/ lemma ker_iff_mem_preimage {f : α → β} {x y} : (ker f).rel x y ↔ x ∈ f ⁻¹' {f y} := iff.rfl /-- Equivalence between functions `α → β` such that `r x y → f x = f y` and functions `quotient r → β`. -/ def lift_equiv (r : setoid α) : {f : α → β // r ≤ ker f} ≃ (quotient r → β) := { to_fun := λ f, quotient.lift (f : α → β) f.2, inv_fun := λ f, ⟨f ∘ quotient.mk, λ x y h, by simp [ker_def, quotient.sound h]⟩, left_inv := λ ⟨f, hf⟩, subtype.eq $ funext $ λ x, rfl, right_inv := λ f, funext $ λ x, quotient.induction_on' x $ λ x, rfl } /-- The uniqueness part of the universal property for quotients of an arbitrary type. -/ theorem lift_unique {r : setoid α} {f : α → β} (H : r ≤ ker f) (g : quotient r → β) (Hg : f = g ∘ quotient.mk) : quotient.lift f H = g := begin ext ⟨x⟩, erw [quotient.lift_mk f H, Hg], refl end /-- Given a map f from α to β, the natural map from the quotient of α by the kernel of f is injective. -/ lemma ker_lift_injective (f : α → β) : injective (@quotient.lift _ _ (ker f) f (λ _ _ h, h)) := λ x y, quotient.induction_on₂' x y $ λ a b h, quotient.sound' h /-- Given a map f from α to β, the kernel of f is the unique equivalence relation on α whose induced map from the quotient of α to β is injective. -/ lemma ker_eq_lift_of_injective {r : setoid α} (f : α → β) (H : ∀ x y, r.rel x y → f x = f y) (h : injective (quotient.lift f H)) : ker f = r := le_antisymm (λ x y hk, quotient.exact $ h $ show quotient.lift f H ⟦x⟧ = quotient.lift f H ⟦y⟧, from hk) H variables (r : setoid α) (f : α → β) /-- The first isomorphism theorem for sets: the quotient of α by the kernel of a function f bijects with f's image. -/ noncomputable def quotient_ker_equiv_range : quotient (ker f) ≃ set.range f := equiv.of_bijective (@quotient.lift _ (set.range f) (ker f) (λ x, ⟨f x, set.mem_range_self x⟩) $ λ _ _ h, subtype.ext_val h) ⟨λ x y h, ker_lift_injective f $ by rcases x; rcases y; injections, λ ⟨w, z, hz⟩, ⟨@quotient.mk _ (ker f) z, by rw quotient.lift_mk; exact subtype.ext_iff_val.2 hz⟩⟩ /-- If `f` has a computable right-inverse, then the quotient by its kernel is equivalent to its domain. -/ @[simps] def quotient_ker_equiv_of_right_inverse (g : β → α) (hf : function.right_inverse g f) : quotient (ker f) ≃ β := { to_fun := λ a, quotient.lift_on' a f $ λ _ _, id, inv_fun := λ b, quotient.mk' (g b), left_inv := λ a, quotient.induction_on' a $ λ a, quotient.sound' $ by exact hf (f a), right_inv := hf } /-- The quotient of α by the kernel of a surjective function f bijects with f's codomain. If a specific right-inverse of `f` is known, `setoid.quotient_ker_equiv_of_right_inverse` can be definitionally more useful. -/ noncomputable def quotient_ker_equiv_of_surjective (hf : surjective f) : quotient (ker f) ≃ β := quotient_ker_equiv_of_right_inverse _ (function.surj_inv hf) (right_inverse_surj_inv hf) variables {r f} /-- Given a function `f : α → β` and equivalence relation `r` on `α`, the equivalence closure of the relation on `f`'s image defined by '`x ≈ y` iff the elements of `f⁻¹(x)` are related to the elements of `f⁻¹(y)` by `r`.' -/ def map (r : setoid α) (f : α → β) : setoid β := eqv_gen.setoid $ λ x y, ∃ a b, f a = x ∧ f b = y ∧ r.rel a b /-- Given a surjective function f whose kernel is contained in an equivalence relation r, the equivalence relation on f's codomain defined by x ≈ y ↔ the elements of f⁻¹(x) are related to the elements of f⁻¹(y) by r. -/ def map_of_surjective (r) (f : α → β) (h : ker f ≤ r) (hf : surjective f) : setoid β := ⟨λ x y, ∃ a b, f a = x ∧ f b = y ∧ r.rel a b, ⟨λ x, let ⟨y, hy⟩ := hf x in ⟨y, y, hy, hy, r.refl' y⟩, λ _ _ ⟨x, y, hx, hy, h⟩, ⟨y, x, hy, hx, r.symm' h⟩, λ _ _ _ ⟨x, y, hx, hy, h₁⟩ ⟨y', z, hy', hz, h₂⟩, ⟨x, z, hx, hz, r.trans' h₁ $ r.trans' (h $ by rwa ←hy' at hy) h₂⟩⟩⟩ /-- A special case of the equivalence closure of an equivalence relation r equalling r. -/ lemma map_of_surjective_eq_map (h : ker f ≤ r) (hf : surjective f) : map r f = map_of_surjective r f h hf := by rw ←eqv_gen_of_setoid (map_of_surjective r f h hf); refl /-- Given a function `f : α → β`, an equivalence relation `r` on `β` induces an equivalence relation on `α` defined by '`x ≈ y` iff `f(x)` is related to `f(y)` by `r`'. -/ def comap (f : α → β) (r : setoid β) : setoid α := ⟨r.rel on f, r.iseqv.comap _⟩ lemma comap_rel (f : α → β) (r : setoid β) (x y : α) : (comap f r).rel x y ↔ r.rel (f x) (f y) := iff.rfl /-- Given a map `f : N → M` and an equivalence relation `r` on `β`, the equivalence relation induced on `α` by `f` equals the kernel of `r`'s quotient map composed with `f`. -/ lemma comap_eq {f : α → β} {r : setoid β} : comap f r = ker (@quotient.mk _ r ∘ f) := ext $ λ x y, show _ ↔ ⟦_⟧ = ⟦_⟧, by rw quotient.eq; refl /-- The second isomorphism theorem for sets. -/ noncomputable def comap_quotient_equiv (f : α → β) (r : setoid β) : quotient (comap f r) ≃ set.range (@quotient.mk _ r ∘ f) := (quotient.congr_right $ ext_iff.1 comap_eq).trans $ quotient_ker_equiv_range $ quotient.mk ∘ f variables (r f) /-- The third isomorphism theorem for sets. -/ def quotient_quotient_equiv_quotient (s : setoid α) (h : r ≤ s) : quotient (ker (quot.map_right h)) ≃ quotient s := { to_fun := λ x, quotient.lift_on' x (λ w, quotient.lift_on' w (@quotient.mk _ s) $ λ x y H, quotient.sound $ h H) $ λ x y, quotient.induction_on₂' x y $ λ w z H, show @quot.mk _ _ _ = @quot.mk _ _ _, from H, inv_fun := λ x, quotient.lift_on' x (λ w, @quotient.mk _ (ker $ quot.map_right h) $ @quotient.mk _ r w) $ λ x y H, quotient.sound' $ show @quot.mk _ _ _ = @quot.mk _ _ _, from quotient.sound H, left_inv := λ x, quotient.induction_on' x $ λ y, quotient.induction_on' y $ λ w, by show ⟦_⟧ = _; refl, right_inv := λ x, quotient.induction_on' x $ λ y, by show ⟦_⟧ = _; refl } variables {r f} open quotient /-- Given an equivalence relation `r` on `α`, the order-preserving bijection between the set of equivalence relations containing `r` and the equivalence relations on the quotient of `α` by `r`. -/ def correspondence (r : setoid α) : {s // r ≤ s} ≃o setoid (quotient r) := { to_fun := λ s, map_of_surjective s.1 quotient.mk ((ker_mk_eq r).symm ▸ s.2) exists_rep, inv_fun := λ s, ⟨comap quotient.mk' s, λ x y h, by rw [comap_rel, eq_rel.2 h]⟩, left_inv := λ s, subtype.ext_iff_val.2 $ ext' $ λ _ _, ⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in s.1.trans' (s.1.symm' $ s.2 $ eq_rel.1 hx) $ s.1.trans' H $ s.2 $ eq_rel.1 hy, λ h, ⟨_, _, rfl, rfl, h⟩⟩, right_inv := λ s, let Hm : ker quotient.mk' ≤ comap quotient.mk' s := λ x y h, by rw [comap_rel, (@eq_rel _ r x y).2 ((ker_mk_eq r) ▸ h)] in ext' $ λ x y, ⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in hx ▸ hy ▸ H, quotient.induction_on₂ x y $ λ w z h, ⟨w, z, rfl, rfl, h⟩⟩, map_rel_iff' := λ s t, ⟨λ h x y hs, let ⟨a, b, hx, hy, ht⟩ := h ⟨x, y, rfl, rfl, hs⟩ in t.1.trans' (t.1.symm' $ t.2 $ eq_rel.1 hx) $ t.1.trans' ht $ t.2 $ eq_rel.1 hy, λ h x y hs, let ⟨a, b, hx, hy, Hs⟩ := hs in ⟨a, b, hx, hy, h Hs⟩⟩ } end setoid @[simp] lemma quotient.subsingleton_iff {s : setoid α} : subsingleton (quotient s) ↔ s = ⊤ := begin simp only [subsingleton_iff, eq_top_iff, setoid.le_def, setoid.top_def, pi.top_apply, forall_const], refine (surjective_quotient_mk _).forall.trans (forall_congr $ λ a, _), refine (surjective_quotient_mk _).forall.trans (forall_congr $ λ b, _), exact quotient.eq', end lemma quot.subsingleton_iff (r : α → α → Prop) : subsingleton (quot r) ↔ eqv_gen r = ⊤ := begin simp only [subsingleton_iff, _root_.eq_top_iff, pi.le_def, pi.top_apply, forall_const], refine (surjective_quot_mk _).forall.trans (forall_congr $ λ a, _), refine (surjective_quot_mk _).forall.trans (forall_congr $ λ b, _), rw quot.eq, simp only [forall_const, le_Prop_eq], end
d2a21e174ebfe95d6f6f3a89a688fdc3b4cb5455
05b503addd423dd68145d68b8cde5cd595d74365
/src/algebra/group/prod.lean
df9c3df6b29bced5687613c118d54a36dd8ed520
[ "Apache-2.0" ]
permissive
aestriplex/mathlib
77513ff2b176d74a3bec114f33b519069788811d
e2fa8b2b1b732d7c25119229e3cdfba8370cb00f
refs/heads/master
1,621,969,960,692
1,586,279,279,000
1,586,279,279,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,842
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot, Yury Kudryashov -/ import algebra.group.hom /-! # Monoid, group etc structures on `M × N` In this file we define one-binop (`monoid`, `group` etc) structures on `M × N`. We also prove trivial `simp` lemmas, and define the following operations on `monoid_hom`s: * `fst M N : M × N →* M`, `snd M N : M × N →* N`: projections `prod.fst` and `prod.snd` as `monoid_hom`s; * `inl M N : M →* M × N`, `inr M N : N →* M × N`: inclusions of first/second monoid into the product; * `f.prod g : `M →* N × P`: sends `x` to `(f x, g x)`; * `f.coprod g : M × N →* P`: sends `(x, y)` to `f x * g y`; * `f.prod_map g : M × N → M' × N'`: `prod.map f g` as a `monoid_hom`, sends `(x, y)` to `(f x, g y)`. -/ variables {A : Type*} {B : Type*} {G : Type*} {H : Type*} {M : Type*} {N : Type*} {P : Type*} namespace prod @[to_additive] instance [has_mul M] [has_mul N] : has_mul (M × N) := ⟨λ p q, ⟨p.1 * q.1, p.2 * q.2⟩⟩ @[simp, to_additive] lemma fst_mul [has_mul M] [has_mul N] (p q : M × N) : (p * q).1 = p.1 * q.1 := rfl @[simp, to_additive] lemma snd_mul [has_mul M] [has_mul N] (p q : M × N) : (p * q).2 = p.2 * q.2 := rfl @[simp, to_additive] lemma mk_mul_mk [has_mul M] [has_mul N] (a₁ a₂ : M) (b₁ b₂ : N) : (a₁, b₁) * (a₂, b₂) = (a₁ * a₂, b₁ * b₂) := rfl @[to_additive] instance [has_one M] [has_one N] : has_one (M × N) := ⟨(1, 1)⟩ @[simp, to_additive] lemma fst_one [has_one M] [has_one N] : (1 : M × N).1 = 1 := rfl @[simp, to_additive] lemma snd_one [has_one M] [has_one N] : (1 : M × N).2 = 1 := rfl @[to_additive] lemma one_eq_mk [has_one M] [has_one N] : (1 : M × N) = (1, 1) := rfl @[to_additive] lemma fst_mul_snd [monoid M] [monoid N] (p : M × N) : (p.fst, 1) * (1, p.snd) = p := ext (mul_one p.1) (one_mul p.2) @[to_additive] instance [has_inv M] [has_inv N] : has_inv (M × N) := ⟨λp, (p.1⁻¹, p.2⁻¹)⟩ @[simp, to_additive] lemma fst_inv [has_inv G] [has_inv H] (p : G × H) : (p⁻¹).1 = (p.1)⁻¹ := rfl @[simp, to_additive] lemma snd_inv [has_inv G] [has_inv H] (p : G × H) : (p⁻¹).2 = (p.2)⁻¹ := rfl @[simp, to_additive] lemma inv_mk [has_inv G] [has_inv H] (a : G) (b : H) : (a, b)⁻¹ = (a⁻¹, b⁻¹) := rfl @[to_additive add_semigroup] instance [semigroup M] [semigroup N] : semigroup (M × N) := { mul_assoc := assume a b c, mk.inj_iff.mpr ⟨mul_assoc _ _ _, mul_assoc _ _ _⟩, .. prod.has_mul } @[to_additive add_monoid] instance [monoid M] [monoid N] : monoid (M × N) := { one_mul := assume a, prod.rec_on a $ λa b, mk.inj_iff.mpr ⟨one_mul _, one_mul _⟩, mul_one := assume a, prod.rec_on a $ λa b, mk.inj_iff.mpr ⟨mul_one _, mul_one _⟩, .. prod.semigroup, .. prod.has_one } @[to_additive add_group] instance [group G] [group H] : group (G × H) := { mul_left_inv := assume a, mk.inj_iff.mpr ⟨mul_left_inv _, mul_left_inv _⟩, .. prod.monoid, .. prod.has_inv } @[simp] lemma fst_sub [add_group A] [add_group B] (a b : A × B) : (a - b).1 = a.1 - b.1 := rfl @[simp] lemma snd_sub [add_group A] [add_group B] (a b : A × B) : (a - b).2 = a.2 - b.2 := rfl @[simp] lemma mk_sub_mk [add_group A] [add_group B] (x₁ x₂ : A) (y₁ y₂ : B) : (x₁, y₁) - (x₂, y₂) = (x₁ - x₂, y₁ - y₂) := rfl @[to_additive add_comm_semigroup] instance [comm_semigroup G] [comm_semigroup H] : comm_semigroup (G × H) := { mul_comm := assume a b, mk.inj_iff.mpr ⟨mul_comm _ _, mul_comm _ _⟩, .. prod.semigroup } @[to_additive add_comm_monoid] instance [comm_monoid M] [comm_monoid N] : comm_monoid (M × N) := { .. prod.comm_semigroup, .. prod.monoid } @[to_additive add_comm_group] instance [comm_group G] [comm_group H] : comm_group (G × H) := { .. prod.comm_semigroup, .. prod.group } end prod namespace monoid_hom variables (M N) [monoid M] [monoid N] /-- Given monoids `M`, `N`, the natural projection homomorphism from `M × N` to `M`.-/ @[to_additive "Given additive monoids `A`, `B`, the natural projection homomorphism from `A × B` to `A`"] def fst : M × N →* M := ⟨prod.fst, rfl, λ _ _, rfl⟩ /-- Given monoids `M`, `N`, the natural projection homomorphism from `M × N` to `N`.-/ @[to_additive "Given additive monoids `A`, `B`, the natural projection homomorphism from `A × B` to `B`"] def snd : M × N →* N := ⟨prod.snd, rfl, λ _ _, rfl⟩ /-- Given monoids `M`, `N`, the natural inclusion homomorphism from `M` to `M × N`. -/ @[to_additive "Given additive monoids `A`, `B`, the natural inclusion homomorphism from `A` to `A × B`."] def inl : M →* M × N := ⟨λ x, (x, 1), rfl, λ _ _, prod.ext rfl (one_mul 1).symm⟩ /-- Given monoids `M`, `N`, the natural inclusion homomorphism from `N` to `M × N`. -/ @[to_additive "Given additive monoids `A`, `B`, the natural inclusion homomorphism from `B` to `A × B`."] def inr : N →* M × N := ⟨λ y, (1, y), rfl, λ _ _, prod.ext (one_mul 1).symm rfl⟩ variables {M N} @[simp, to_additive] lemma coe_fst : ⇑(fst M N) = prod.fst := rfl @[simp, to_additive] lemma coe_snd : ⇑(snd M N) = prod.snd := rfl @[simp, to_additive] lemma inl_apply (x) : inl M N x = (x, 1) := rfl @[simp, to_additive] lemma inr_apply (y) : inr M N y = (1, y) := rfl @[simp, to_additive] lemma fst_comp_inl : (fst M N).comp (inl M N) = id M := rfl @[simp, to_additive] lemma snd_comp_inl : (snd M N).comp (inl M N) = 1 := rfl @[simp, to_additive] lemma fst_comp_inr : (fst M N).comp (inr M N) = 1 := rfl @[simp, to_additive] lemma snd_comp_inr : (snd M N).comp (inr M N) = id N := rfl section prod variable [monoid P] /-- Combine two `monoid_hom`s `f : M →* N`, `g : M →* P` into `f.prod g : M →* N × P` given by `(f.prod g) x = (f x, g x)` -/ @[to_additive prod "Combine two `add_monoid_hom`s `f : M →+ N`, `g : M →+ P` into `f.prod g : M →+ N × P` given by `(f.prod g) x = (f x, g x)`"] protected def prod (f : M →* N) (g : M →* P) : M →* N × P := { to_fun := λ x, (f x, g x), map_one' := prod.ext f.map_one g.map_one, map_mul' := λ x y, prod.ext (f.map_mul x y) (g.map_mul x y) } @[simp, to_additive prod_apply] lemma prod_apply (f : M →* N) (g : M →* P) (x) : f.prod g x = (f x, g x) := rfl @[to_additive fst_comp_prod] lemma fst_comp_prod (f : M →* N) (g : M →* P) : (fst N P).comp (f.prod g) = f := ext $ λ x, rfl @[to_additive snd_comp_prod] lemma snd_comp_prod (f : M →* N) (g : M →* P) : (snd N P).comp (f.prod g) = g := ext $ λ x, rfl @[to_additive prod_unique] lemma prod_unique (f : M →* N × P) : ((fst N P).comp f).prod ((snd N P).comp f) = f := ext $ λ x, by simp only [prod_apply, coe_fst, coe_snd, comp_apply, prod.mk.eta] end prod section prod_map variables {M' : Type*} {N' : Type*} [monoid M'] [monoid N'] [monoid P] (f : M →* M') (g : N →* N') /-- `prod.map` as a `monoid_hom`. -/ @[to_additive prod_map "`prod.map` as an `add_monoid_hom`"] def prod_map : M × N →* M' × N' := (f.comp (fst M N)).prod (g.comp (snd M N)) @[to_additive prod_map_def] lemma prod_map_def : prod_map f g = (f.comp (fst M N)).prod (g.comp (snd M N)) := rfl -- TODO : use `rfl` once we redefine `prod.map` in stdlib @[simp, to_additive coe_prod_map] lemma coe_prod_map : ⇑(prod_map f g) = prod.map f g := funext $ λ ⟨x, y⟩, rfl @[to_additive prod_comp_prod_map] lemma prod_comp_prod_map (f : P →* M) (g : P →* N) (f' : M →* M') (g' : N →* N') : (f'.prod_map g').comp (f.prod g) = (f'.comp f).prod (g'.comp g) := rfl end prod_map section coprod variables [comm_monoid P] (f : M →* P) (g : N →* P) /-- Coproduct of two `monoid_hom`s with the same codomain: `f.coprod g (p : M × N) = f p.1 * g p.2`. -/ @[to_additive "Coproduct of two `add_monoid_hom`s with the same codomain: `f.coprod g (p : M × N) = f p.1 + g p.2`."] def coprod : M × N →* P := f.comp (fst M N) * g.comp (snd M N) @[simp, to_additive] lemma coprod_apply (p : M × N) : f.coprod g p = f p.1 * g p.2 := rfl @[simp, to_additive] lemma coprod_comp_inl : (f.coprod g).comp (inl M N) = f := ext $ λ x, by simp [coprod_apply] @[simp, to_additive] lemma coprod_comp_inr : (f.coprod g).comp (inr M N) = g := ext $ λ x, by simp [coprod_apply] @[simp, to_additive] lemma coprod_unique (f : M × N →* P) : (f.comp (inl M N)).coprod (f.comp (inr M N)) = f := ext $ λ x, by simp [coprod_apply, inl_apply, inr_apply, ← map_mul] @[simp, to_additive] lemma coprod_inl_inr {M N : Type*} [comm_monoid M] [comm_monoid N] : (inl M N).coprod (inr M N) = id (M × N) := coprod_unique (id $ M × N) lemma comp_coprod {Q : Type*} [comm_monoid Q] (h : P →* Q) (f : M →* P) (g : N →* P) : h.comp (f.coprod g) = (h.comp f).coprod (h.comp g) := ext $ λ x, by simp end coprod end monoid_hom
7f729d0e2ad51389a3cc2aef05f74dc8b766e675
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebra/algebra/subalgebra/basic.lean
d5ff8a8c1a12b459bb894842413b36b25d9c7ad3
[ "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
44,718
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import algebra.algebra.basic import data.set.Union_lift /-! # Subalgebras over Commutative Semiring In this file we define `subalgebra`s and the usual operations on them (`map`, `comap`). More lemmas about `adjoin` can be found in `ring_theory.adjoin`. -/ universes u u' v w w' open_locale big_operators set_option old_structure_cmd true /-- A subalgebra is a sub(semi)ring that includes the range of `algebra_map`. -/ structure subalgebra (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] extends subsemiring A : Type v := (algebra_map_mem' : ∀ r, algebra_map R A r ∈ carrier) (zero_mem' := (algebra_map R A).map_zero ▸ algebra_map_mem' 0) (one_mem' := (algebra_map R A).map_one ▸ algebra_map_mem' 1) /-- Reinterpret a `subalgebra` as a `subsemiring`. -/ add_decl_doc subalgebra.to_subsemiring namespace subalgebra variables {R' : Type u'} {R : Type u} {A : Type v} {B : Type w} {C : Type w'} variables [comm_semiring R] variables [semiring A] [algebra R A] [semiring B] [algebra R B] [semiring C] [algebra R C] include R instance : set_like (subalgebra R A) A := { coe := subalgebra.carrier, coe_injective' := λ p q h, by cases p; cases q; congr' } instance : subsemiring_class (subalgebra R A) A := { add_mem := add_mem', mul_mem := mul_mem', one_mem := one_mem', zero_mem := zero_mem' } @[simp] lemma mem_carrier {s : subalgebra R A} {x : A} : x ∈ s.carrier ↔ x ∈ s := iff.rfl @[ext] theorem ext {S T : subalgebra R A} (h : ∀ x : A, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h @[simp] lemma mem_to_subsemiring {S : subalgebra R A} {x} : x ∈ S.to_subsemiring ↔ x ∈ S := iff.rfl @[simp] lemma coe_to_subsemiring (S : subalgebra R A) : (↑S.to_subsemiring : set A) = S := rfl theorem to_subsemiring_injective : function.injective (to_subsemiring : subalgebra R A → subsemiring A) := λ S T h, ext $ λ x, by rw [← mem_to_subsemiring, ← mem_to_subsemiring, h] theorem to_subsemiring_inj {S U : subalgebra R A} : S.to_subsemiring = U.to_subsemiring ↔ S = U := to_subsemiring_injective.eq_iff /-- Copy of a subalgebra with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (S : subalgebra R A) (s : set A) (hs : s = ↑S) : subalgebra R A := { carrier := s, add_mem' := hs.symm ▸ S.add_mem', mul_mem' := hs.symm ▸ S.mul_mem', algebra_map_mem' := hs.symm ▸ S.algebra_map_mem' } @[simp] lemma coe_copy (S : subalgebra R A) (s : set A) (hs : s = ↑S) : (S.copy s hs : set A) = s := rfl lemma copy_eq (S : subalgebra R A) (s : set A) (hs : s = ↑S) : S.copy s hs = S := set_like.coe_injective hs variables (S : subalgebra R A) theorem algebra_map_mem (r : R) : algebra_map R A r ∈ S := S.algebra_map_mem' r theorem srange_le : (algebra_map R A).srange ≤ S.to_subsemiring := λ x ⟨r, hr⟩, hr ▸ S.algebra_map_mem r theorem range_subset : set.range (algebra_map R A) ⊆ S := λ x ⟨r, hr⟩, hr ▸ S.algebra_map_mem r theorem range_le : set.range (algebra_map R A) ≤ S := S.range_subset theorem smul_mem {x : A} (hx : x ∈ S) (r : R) : r • x ∈ S := (algebra.smul_def r x).symm ▸ mul_mem (S.algebra_map_mem r) hx protected theorem one_mem : (1 : A) ∈ S := one_mem S protected theorem mul_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x * y ∈ S := mul_mem hx hy protected theorem pow_mem {x : A} (hx : x ∈ S) (n : ℕ) : x ^ n ∈ S := pow_mem hx n protected theorem zero_mem : (0 : A) ∈ S := zero_mem S protected theorem add_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x + y ∈ S := add_mem hx hy protected theorem nsmul_mem {x : A} (hx : x ∈ S) (n : ℕ) : n • x ∈ S := nsmul_mem hx n protected theorem coe_nat_mem (n : ℕ) : (n : A) ∈ S := coe_nat_mem S n protected theorem list_prod_mem {L : list A} (h : ∀ x ∈ L, x ∈ S) : L.prod ∈ S := list_prod_mem h protected theorem list_sum_mem {L : list A} (h : ∀ x ∈ L, x ∈ S) : L.sum ∈ S := list_sum_mem h protected theorem multiset_sum_mem {m : multiset A} (h : ∀ x ∈ m, x ∈ S) : m.sum ∈ S := multiset_sum_mem m h protected theorem sum_mem {ι : Type w} {t : finset ι} {f : ι → A} (h : ∀ x ∈ t, f x ∈ S) : ∑ x in t, f x ∈ S := sum_mem h protected theorem multiset_prod_mem {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) {m : multiset A} (h : ∀ x ∈ m, x ∈ S) : m.prod ∈ S := multiset_prod_mem m h protected theorem prod_mem {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) {ι : Type w} {t : finset ι} {f : ι → A} (h : ∀ x ∈ t, f x ∈ S) : ∏ x in t, f x ∈ S := prod_mem h instance {R A : Type*} [comm_ring R] [ring A] [algebra R A] : subring_class (subalgebra R A) A := { neg_mem := λ S x hx, neg_one_smul R x ▸ S.smul_mem hx _, .. subalgebra.subsemiring_class } protected theorem neg_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x : A} (hx : x ∈ S) : -x ∈ S := neg_mem hx protected theorem sub_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x - y ∈ S := sub_mem hx hy protected theorem zsmul_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x : A} (hx : x ∈ S) (n : ℤ) : n • x ∈ S := zsmul_mem hx n protected theorem coe_int_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) (n : ℤ) : (n : A) ∈ S := coe_int_mem S n /-- The projection from a subalgebra of `A` to an additive submonoid of `A`. -/ def to_add_submonoid {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : add_submonoid A := S.to_subsemiring.to_add_submonoid /-- The projection from a subalgebra of `A` to a submonoid of `A`. -/ def to_submonoid {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : submonoid A := S.to_subsemiring.to_submonoid /-- A subalgebra over a ring is also a `subring`. -/ def to_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : subring A := { neg_mem' := λ _, S.neg_mem, .. S.to_subsemiring } @[simp] lemma mem_to_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] {S : subalgebra R A} {x} : x ∈ S.to_subring ↔ x ∈ S := iff.rfl @[simp] lemma coe_to_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : (↑S.to_subring : set A) = S := rfl theorem to_subring_injective {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] : function.injective (to_subring : subalgebra R A → subring A) := λ S T h, ext $ λ x, by rw [← mem_to_subring, ← mem_to_subring, h] theorem to_subring_inj {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] {S U : subalgebra R A} : S.to_subring = U.to_subring ↔ S = U := to_subring_injective.eq_iff instance : inhabited S := ⟨(0 : S.to_subsemiring)⟩ section /-! `subalgebra`s inherit structure from their `subsemiring` / `semiring` coercions. -/ instance to_semiring {R A} [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : semiring S := S.to_subsemiring.to_semiring instance to_comm_semiring {R A} [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) : comm_semiring S := S.to_subsemiring.to_comm_semiring instance to_ring {R A} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : ring S := S.to_subring.to_ring instance to_comm_ring {R A} [comm_ring R] [comm_ring A] [algebra R A] (S : subalgebra R A) : comm_ring S := S.to_subring.to_comm_ring instance to_ordered_semiring {R A} [comm_semiring R] [ordered_semiring A] [algebra R A] (S : subalgebra R A) : ordered_semiring S := S.to_subsemiring.to_ordered_semiring instance to_ordered_comm_semiring {R A} [comm_semiring R] [ordered_comm_semiring A] [algebra R A] (S : subalgebra R A) : ordered_comm_semiring S := S.to_subsemiring.to_ordered_comm_semiring instance to_ordered_ring {R A} [comm_ring R] [ordered_ring A] [algebra R A] (S : subalgebra R A) : ordered_ring S := S.to_subring.to_ordered_ring instance to_ordered_comm_ring {R A} [comm_ring R] [ordered_comm_ring A] [algebra R A] (S : subalgebra R A) : ordered_comm_ring S := S.to_subring.to_ordered_comm_ring instance to_linear_ordered_semiring {R A} [comm_semiring R] [linear_ordered_semiring A] [algebra R A] (S : subalgebra R A) : linear_ordered_semiring S := S.to_subsemiring.to_linear_ordered_semiring /-! There is no `linear_ordered_comm_semiring`. -/ instance to_linear_ordered_ring {R A} [comm_ring R] [linear_ordered_ring A] [algebra R A] (S : subalgebra R A) : linear_ordered_ring S := S.to_subring.to_linear_ordered_ring instance to_linear_ordered_comm_ring {R A} [comm_ring R] [linear_ordered_comm_ring A] [algebra R A] (S : subalgebra R A) : linear_ordered_comm_ring S := S.to_subring.to_linear_ordered_comm_ring end /-- Convert a `subalgebra` to `submodule` -/ def to_submodule : submodule R A := { carrier := S, zero_mem' := (0:S).2, add_mem' := λ x y hx hy, (⟨x, hx⟩ + ⟨y, hy⟩ : S).2, smul_mem' := λ c x hx, (algebra.smul_def c x).symm ▸ (⟨algebra_map R A c, S.range_le ⟨c, rfl⟩⟩ * ⟨x, hx⟩:S).2 } @[simp] lemma mem_to_submodule {x} : x ∈ S.to_submodule ↔ x ∈ S := iff.rfl @[simp] lemma coe_to_submodule (S : subalgebra R A) : (↑S.to_submodule : set A) = S := rfl theorem to_submodule_injective : function.injective (to_submodule : subalgebra R A → submodule R A) := λ S T h, ext $ λ x, by rw [← mem_to_submodule, ← mem_to_submodule, h] theorem to_submodule_inj {S U : subalgebra R A} : S.to_submodule = U.to_submodule ↔ S = U := to_submodule_injective.eq_iff section /-! `subalgebra`s inherit structure from their `submodule` coercions. -/ instance module' [semiring R'] [has_smul R' R] [module R' A] [is_scalar_tower R' R A] : module R' S := S.to_submodule.module' instance : module R S := S.module' instance [semiring R'] [has_smul R' R] [module R' A] [is_scalar_tower R' R A] : is_scalar_tower R' R S := S.to_submodule.is_scalar_tower instance algebra' [comm_semiring R'] [has_smul R' R] [algebra R' A] [is_scalar_tower R' R A] : algebra R' S := { commutes' := λ c x, subtype.eq $ algebra.commutes _ _, smul_def' := λ c x, subtype.eq $ algebra.smul_def _ _, .. (algebra_map R' A).cod_restrict S $ λ x, begin rw [algebra.algebra_map_eq_smul_one, ←smul_one_smul R x (1 : A), ←algebra.algebra_map_eq_smul_one], exact algebra_map_mem S _, end } instance : algebra R S := S.algebra' end instance no_zero_smul_divisors_bot [no_zero_smul_divisors R A] : no_zero_smul_divisors R S := ⟨λ c x h, have c = 0 ∨ (x : A) = 0, from eq_zero_or_eq_zero_of_smul_eq_zero (congr_arg coe h), this.imp_right (@subtype.ext_iff _ _ x 0).mpr⟩ protected lemma coe_add (x y : S) : (↑(x + y) : A) = ↑x + ↑y := rfl protected lemma coe_mul (x y : S) : (↑(x * y) : A) = ↑x * ↑y := rfl protected lemma coe_zero : ((0 : S) : A) = 0 := rfl protected lemma coe_one : ((1 : S) : A) = 1 := rfl protected lemma coe_neg {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] {S : subalgebra R A} (x : S) : (↑(-x) : A) = -↑x := rfl protected lemma coe_sub {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] {S : subalgebra R A} (x y : S) : (↑(x - y) : A) = ↑x - ↑y := rfl @[simp, norm_cast] lemma coe_smul [semiring R'] [has_smul R' R] [module R' A] [is_scalar_tower R' R A] (r : R') (x : S) : (↑(r • x) : A) = r • ↑x := rfl @[simp, norm_cast] lemma coe_algebra_map [comm_semiring R'] [has_smul R' R] [algebra R' A] [is_scalar_tower R' R A] (r : R') : ↑(algebra_map R' S r) = algebra_map R' A r := rfl protected lemma coe_pow (x : S) (n : ℕ) : (↑(x^n) : A) = (↑x)^n := submonoid_class.coe_pow x n protected lemma coe_eq_zero {x : S} : (x : A) = 0 ↔ x = 0 := add_submonoid_class.coe_eq_zero protected lemma coe_eq_one {x : S} : (x : A) = 1 ↔ x = 1 := submonoid_class.coe_eq_one -- todo: standardize on the names these morphisms -- compare with submodule.subtype /-- Embedding of a subalgebra into the algebra. -/ def val : S →ₐ[R] A := by refine_struct { to_fun := (coe : S → A) }; intros; refl @[simp] lemma coe_val : (S.val : S → A) = coe := rfl lemma val_apply (x : S) : S.val x = (x : A) := rfl @[simp] lemma to_subsemiring_subtype : S.to_subsemiring.subtype = (S.val : S →+* A) := rfl @[simp] lemma to_subring_subtype {R A : Type*} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : S.to_subring.subtype = (S.val : S →+* A) := rfl /-- Linear equivalence between `S : submodule R A` and `S`. Though these types are equal, we define it as a `linear_equiv` to avoid type equalities. -/ def to_submodule_equiv (S : subalgebra R A) : S.to_submodule ≃ₗ[R] S := linear_equiv.of_eq _ _ rfl /-- Transport a subalgebra via an algebra homomorphism. -/ def map (S : subalgebra R A) (f : A →ₐ[R] B) : subalgebra R B := { algebra_map_mem' := λ r, f.commutes r ▸ set.mem_image_of_mem _ (S.algebra_map_mem r), .. S.to_subsemiring.map (f : A →+* B) } lemma map_mono {S₁ S₂ : subalgebra R A} {f : A →ₐ[R] B} : S₁ ≤ S₂ → S₁.map f ≤ S₂.map f := set.image_subset f lemma map_injective {S₁ S₂ : subalgebra R A} (f : A →ₐ[R] B) (hf : function.injective f) (ih : S₁.map f = S₂.map f) : S₁ = S₂ := ext $ set.ext_iff.1 $ set.image_injective.2 hf $ set.ext $ set_like.ext_iff.mp ih @[simp] lemma map_id (S : subalgebra R A) : S.map (alg_hom.id R A) = S := set_like.coe_injective $ set.image_id _ lemma map_map (S : subalgebra R A) (g : B →ₐ[R] C) (f : A →ₐ[R] B) : (S.map f).map g = S.map (g.comp f) := set_like.coe_injective $ set.image_image _ _ _ lemma mem_map {S : subalgebra R A} {f : A →ₐ[R] B} {y : B} : y ∈ map S f ↔ ∃ x ∈ S, f x = y := subsemiring.mem_map lemma map_to_submodule {S : subalgebra R A} {f : A →ₐ[R] B} : (S.map f).to_submodule = S.to_submodule.map f.to_linear_map := set_like.coe_injective rfl lemma map_to_subsemiring {S : subalgebra R A} {f : A →ₐ[R] B} : (S.map f).to_subsemiring = S.to_subsemiring.map f.to_ring_hom := set_like.coe_injective rfl @[simp] lemma coe_map (S : subalgebra R A) (f : A →ₐ[R] B) : (S.map f : set B) = f '' S := rfl /-- Preimage of a subalgebra under an algebra homomorphism. -/ def comap (S : subalgebra R B) (f : A →ₐ[R] B) : subalgebra R A := { algebra_map_mem' := λ r, show f (algebra_map R A r) ∈ S, from (f.commutes r).symm ▸ S.algebra_map_mem r, .. S.to_subsemiring.comap (f : A →+* B) } theorem map_le {S : subalgebra R A} {f : A →ₐ[R] B} {U : subalgebra R B} : map S f ≤ U ↔ S ≤ comap U f := set.image_subset_iff lemma gc_map_comap (f : A →ₐ[R] B) : galois_connection (λ S, map S f) (λ S, comap S f) := λ S U, map_le @[simp] lemma mem_comap (S : subalgebra R B) (f : A →ₐ[R] B) (x : A) : x ∈ S.comap f ↔ f x ∈ S := iff.rfl @[simp, norm_cast] lemma coe_comap (S : subalgebra R B) (f : A →ₐ[R] B) : (S.comap f : set A) = f ⁻¹' (S : set B) := rfl instance no_zero_divisors {R A : Type*} [comm_semiring R] [semiring A] [no_zero_divisors A] [algebra R A] (S : subalgebra R A) : no_zero_divisors S := S.to_subsemiring.no_zero_divisors instance is_domain {R A : Type*} [comm_ring R] [ring A] [is_domain A] [algebra R A] (S : subalgebra R A) : is_domain S := subring.is_domain S.to_subring end subalgebra namespace submodule variables {R A : Type*} [comm_semiring R] [semiring A] [algebra R A] variables (p : submodule R A) /-- A submodule containing `1` and closed under multiplication is a subalgebra. -/ def to_subalgebra (p : submodule R A) (h_one : (1 : A) ∈ p) (h_mul : ∀ x y, x ∈ p → y ∈ p → x * y ∈ p) : subalgebra R A := { mul_mem' := h_mul, algebra_map_mem' := λ r, begin rw algebra.algebra_map_eq_smul_one, exact p.smul_mem _ h_one, end, ..p} @[simp] lemma mem_to_subalgebra {p : submodule R A} {h_one h_mul} {x} : x ∈ p.to_subalgebra h_one h_mul ↔ x ∈ p := iff.rfl @[simp] lemma coe_to_subalgebra (p : submodule R A) (h_one h_mul) : (p.to_subalgebra h_one h_mul : set A) = p := rfl @[simp] lemma to_subalgebra_mk (s : set A) (h0 hadd hsmul h1 hmul) : (submodule.mk s hadd h0 hsmul : submodule R A).to_subalgebra h1 hmul = subalgebra.mk s @hmul h1 @hadd h0 (λ r, by { rw algebra.algebra_map_eq_smul_one, exact hsmul r h1 }) := rfl @[simp] lemma to_subalgebra_to_submodule (p : submodule R A) (h_one h_mul) : (p.to_subalgebra h_one h_mul).to_submodule = p := set_like.coe_injective rfl @[simp] lemma _root_.subalgebra.to_submodule_to_subalgebra (S : subalgebra R A) : S.to_submodule.to_subalgebra S.one_mem (λ _ _, S.mul_mem) = S := set_like.coe_injective rfl end submodule namespace alg_hom variables {R' : Type u'} {R : Type u} {A : Type v} {B : Type w} {C : Type w'} variables [comm_semiring R] variables [semiring A] [algebra R A] [semiring B] [algebra R B] [semiring C] [algebra R C] variables (φ : A →ₐ[R] B) /-- Range of an `alg_hom` as a subalgebra. -/ protected def range (φ : A →ₐ[R] B) : subalgebra R B := { algebra_map_mem' := λ r, ⟨algebra_map R A r, φ.commutes r⟩, .. φ.to_ring_hom.srange } @[simp] lemma mem_range (φ : A →ₐ[R] B) {y : B} : y ∈ φ.range ↔ ∃ x, φ x = y := ring_hom.mem_srange theorem mem_range_self (φ : A →ₐ[R] B) (x : A) : φ x ∈ φ.range := φ.mem_range.2 ⟨x, rfl⟩ @[simp] lemma coe_range (φ : A →ₐ[R] B) : (φ.range : set B) = set.range φ := by { ext, rw [set_like.mem_coe, mem_range], refl } theorem range_comp (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (g.comp f).range = f.range.map g := set_like.coe_injective (set.range_comp g f) theorem range_comp_le_range (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (g.comp f).range ≤ g.range := set_like.coe_mono (set.range_comp_subset_range f g) /-- Restrict the codomain of an algebra homomorphism. -/ def cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) : A →ₐ[R] S := { commutes' := λ r, subtype.eq $ f.commutes r, .. ring_hom.cod_restrict (f : A →+* B) S hf } @[simp] lemma val_comp_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) : S.val.comp (f.cod_restrict S hf) = f := alg_hom.ext $ λ _, rfl @[simp] lemma coe_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) (x : A) : ↑(f.cod_restrict S hf x) = f x := rfl theorem injective_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) : function.injective (f.cod_restrict S hf) ↔ function.injective f := ⟨λ H x y hxy, H $ subtype.eq hxy, λ H x y hxy, H (congr_arg subtype.val hxy : _)⟩ /-- Restrict the codomain of a alg_hom `f` to `f.range`. This is the bundled version of `set.range_factorization`. -/ @[reducible] def range_restrict (f : A →ₐ[R] B) : A →ₐ[R] f.range := f.cod_restrict f.range f.mem_range_self /-- The equalizer of two R-algebra homomorphisms -/ def equalizer (ϕ ψ : A →ₐ[R] B) : subalgebra R A := { carrier := {a | ϕ a = ψ a}, add_mem' := λ x y (hx : ϕ x = ψ x) (hy : ϕ y = ψ y), by rw [set.mem_set_of_eq, ϕ.map_add, ψ.map_add, hx, hy], mul_mem' := λ x y (hx : ϕ x = ψ x) (hy : ϕ y = ψ y), by rw [set.mem_set_of_eq, ϕ.map_mul, ψ.map_mul, hx, hy], algebra_map_mem' := λ x, by rw [set.mem_set_of_eq, alg_hom.commutes, alg_hom.commutes] } @[simp] lemma mem_equalizer (ϕ ψ : A →ₐ[R] B) (x : A) : x ∈ ϕ.equalizer ψ ↔ ϕ x = ψ x := iff.rfl /-- The range of a morphism of algebras is a fintype, if the domain is a fintype. Note that this instance can cause a diamond with `subtype.fintype` if `B` is also a fintype. -/ instance fintype_range [fintype A] [decidable_eq B] (φ : A →ₐ[R] B) : fintype φ.range := set.fintype_range φ end alg_hom namespace alg_equiv variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] /-- Restrict an algebra homomorphism with a left inverse to an algebra isomorphism to its range. This is a computable alternative to `alg_equiv.of_injective`. -/ def of_left_inverse {g : B → A} {f : A →ₐ[R] B} (h : function.left_inverse g f) : A ≃ₐ[R] f.range := { to_fun := f.range_restrict, inv_fun := g ∘ f.range.val, left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := f.mem_range.mp x.prop in show f (g x) = x, by rw [←hx', h x'], ..f.range_restrict } @[simp] lemma of_left_inverse_apply {g : B → A} {f : A →ₐ[R] B} (h : function.left_inverse g f) (x : A) : ↑(of_left_inverse h x) = f x := rfl @[simp] lemma of_left_inverse_symm_apply {g : B → A} {f : A →ₐ[R] B} (h : function.left_inverse g f) (x : f.range) : (of_left_inverse h).symm x = g x := rfl /-- Restrict an injective algebra homomorphism to an algebra isomorphism -/ noncomputable def of_injective (f : A →ₐ[R] B) (hf : function.injective f) : A ≃ₐ[R] f.range := of_left_inverse (classical.some_spec hf.has_left_inverse) @[simp] lemma of_injective_apply (f : A →ₐ[R] B) (hf : function.injective f) (x : A) : ↑(of_injective f hf x) = f x := rfl /-- Restrict an algebra homomorphism between fields to an algebra isomorphism -/ noncomputable def of_injective_field {E F : Type*} [division_ring E] [semiring F] [nontrivial F] [algebra R E] [algebra R F] (f : E →ₐ[R] F) : E ≃ₐ[R] f.range := of_injective f f.to_ring_hom.injective /-- Given an equivalence `e : A ≃ₐ[R] B` of `R`-algebras and a subalgebra `S` of `A`, `subalgebra_map` is the induced equivalence between `S` and `S.map e` -/ @[simps] def subalgebra_map (e : A ≃ₐ[R] B) (S : subalgebra R A) : S ≃ₐ[R] (S.map e.to_alg_hom) := { commutes' := λ r, by { ext, simp }, ..e.to_ring_equiv.subsemiring_map S.to_subsemiring } end alg_equiv namespace algebra variables (R : Type u) {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] /-- The minimal subalgebra that includes `s`. -/ def adjoin (s : set A) : subalgebra R A := { algebra_map_mem' := λ r, subsemiring.subset_closure $ or.inl ⟨r, rfl⟩, .. subsemiring.closure (set.range (algebra_map R A) ∪ s) } variables {R} protected lemma gc : galois_connection (adjoin R : set A → subalgebra R A) coe := λ s S, ⟨λ H, le_trans (le_trans (set.subset_union_right _ _) subsemiring.subset_closure) H, λ H, show subsemiring.closure (set.range (algebra_map R A) ∪ s) ≤ S.to_subsemiring, from subsemiring.closure_le.2 $ set.union_subset S.range_subset H⟩ /-- Galois insertion between `adjoin` and `coe`. -/ protected def gi : galois_insertion (adjoin R : set A → subalgebra R A) coe := { choice := λ s hs, (adjoin R s).copy s $ le_antisymm (algebra.gc.le_u_l s) hs, gc := algebra.gc, le_l_u := λ S, (algebra.gc (S : set A) (adjoin R S)).1 $ le_rfl, choice_eq := λ _ _, subalgebra.copy_eq _ _ _ } instance : complete_lattice (subalgebra R A) := galois_insertion.lift_complete_lattice algebra.gi @[simp] lemma coe_top : (↑(⊤ : subalgebra R A) : set A) = set.univ := rfl @[simp] lemma mem_top {x : A} : x ∈ (⊤ : subalgebra R A) := set.mem_univ x @[simp] lemma top_to_submodule : (⊤ : subalgebra R A).to_submodule = ⊤ := rfl @[simp] lemma top_to_subsemiring : (⊤ : subalgebra R A).to_subsemiring = ⊤ := rfl @[simp] lemma top_to_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A] : (⊤ : subalgebra R A).to_subring = ⊤ := rfl @[simp] lemma to_submodule_eq_top {S : subalgebra R A} : S.to_submodule = ⊤ ↔ S = ⊤ := subalgebra.to_submodule_injective.eq_iff' top_to_submodule @[simp] lemma to_subsemiring_eq_top {S : subalgebra R A} : S.to_subsemiring = ⊤ ↔ S = ⊤ := subalgebra.to_subsemiring_injective.eq_iff' top_to_subsemiring @[simp] lemma to_subring_eq_top {R A : Type*} [comm_ring R] [ring A] [algebra R A] {S : subalgebra R A} : S.to_subring = ⊤ ↔ S = ⊤ := subalgebra.to_subring_injective.eq_iff' top_to_subring lemma mem_sup_left {S T : subalgebra R A} : ∀ {x : A}, x ∈ S → x ∈ S ⊔ T := show S ≤ S ⊔ T, from le_sup_left lemma mem_sup_right {S T : subalgebra R A} : ∀ {x : A}, x ∈ T → x ∈ S ⊔ T := show T ≤ S ⊔ T, from le_sup_right lemma mul_mem_sup {S T : subalgebra R A} {x y : A} (hx : x ∈ S) (hy : y ∈ T) : x * y ∈ S ⊔ T := (S ⊔ T).mul_mem (mem_sup_left hx) (mem_sup_right hy) lemma map_sup (f : A →ₐ[R] B) (S T : subalgebra R A) : (S ⊔ T).map f = S.map f ⊔ T.map f := (subalgebra.gc_map_comap f).l_sup @[simp, norm_cast] lemma coe_inf (S T : subalgebra R A) : (↑(S ⊓ T) : set A) = S ∩ T := rfl @[simp] lemma mem_inf {S T : subalgebra R A} {x : A} : x ∈ S ⊓ T ↔ x ∈ S ∧ x ∈ T := iff.rfl @[simp] lemma inf_to_submodule (S T : subalgebra R A) : (S ⊓ T).to_submodule = S.to_submodule ⊓ T.to_submodule := rfl @[simp] lemma inf_to_subsemiring (S T : subalgebra R A) : (S ⊓ T).to_subsemiring = S.to_subsemiring ⊓ T.to_subsemiring := rfl @[simp, norm_cast] lemma coe_Inf (S : set (subalgebra R A)) : (↑(Inf S) : set A) = ⋂ s ∈ S, ↑s := Inf_image lemma mem_Inf {S : set (subalgebra R A)} {x : A} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := by simp only [← set_like.mem_coe, coe_Inf, set.mem_Inter₂] @[simp] lemma Inf_to_submodule (S : set (subalgebra R A)) : (Inf S).to_submodule = Inf (subalgebra.to_submodule '' S) := set_like.coe_injective $ by simp @[simp] lemma Inf_to_subsemiring (S : set (subalgebra R A)) : (Inf S).to_subsemiring = Inf (subalgebra.to_subsemiring '' S) := set_like.coe_injective $ by simp @[simp, norm_cast] lemma coe_infi {ι : Sort*} {S : ι → subalgebra R A} : (↑(⨅ i, S i) : set A) = ⋂ i, S i := by simp [infi] lemma mem_infi {ι : Sort*} {S : ι → subalgebra R A} {x : A} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp] lemma infi_to_submodule {ι : Sort*} (S : ι → subalgebra R A) : (⨅ i, S i).to_submodule = ⨅ i, (S i).to_submodule := set_like.coe_injective $ by simp instance : inhabited (subalgebra R A) := ⟨⊥⟩ theorem mem_bot {x : A} : x ∈ (⊥ : subalgebra R A) ↔ x ∈ set.range (algebra_map R A) := suffices (of_id R A).range = (⊥ : subalgebra R A), by { rw [← this, ←set_like.mem_coe, alg_hom.coe_range], refl }, le_bot_iff.mp (λ x hx, subalgebra.range_le _ ((of_id R A).coe_range ▸ hx)) theorem to_submodule_bot : (⊥ : subalgebra R A).to_submodule = R ∙ 1 := by { ext x, simp [mem_bot, -set.singleton_one, submodule.mem_span_singleton, algebra.smul_def] } @[simp] theorem coe_bot : ((⊥ : subalgebra R A) : set A) = set.range (algebra_map R A) := by simp [set.ext_iff, algebra.mem_bot] theorem eq_top_iff {S : subalgebra R A} : S = ⊤ ↔ ∀ x : A, x ∈ S := ⟨λ h x, by rw h; exact mem_top, λ h, by ext x; exact ⟨λ _, mem_top, λ _, h x⟩⟩ @[simp] theorem range_id : (alg_hom.id R A).range = ⊤ := set_like.coe_injective set.range_id @[simp] theorem map_top (f : A →ₐ[R] B) : subalgebra.map (⊤ : subalgebra R A) f = f.range := set_like.coe_injective set.image_univ @[simp] theorem map_bot (f : A →ₐ[R] B) : subalgebra.map (⊥ : subalgebra R A) f = ⊥ := set_like.coe_injective $ by simp only [← set.range_comp, (∘), algebra.coe_bot, subalgebra.coe_map, f.commutes] @[simp] theorem comap_top (f : A →ₐ[R] B) : subalgebra.comap (⊤ : subalgebra R B) f = ⊤ := eq_top_iff.2 $ λ x, mem_top /-- `alg_hom` to `⊤ : subalgebra R A`. -/ def to_top : A →ₐ[R] (⊤ : subalgebra R A) := (alg_hom.id R A).cod_restrict ⊤ (λ _, mem_top) theorem surjective_algebra_map_iff : function.surjective (algebra_map R A) ↔ (⊤ : subalgebra R A) = ⊥ := ⟨λ h, eq_bot_iff.2 $ λ y _, let ⟨x, hx⟩ := h y in hx ▸ subalgebra.algebra_map_mem _ _, λ h y, algebra.mem_bot.1 $ eq_bot_iff.1 h (algebra.mem_top : y ∈ _)⟩ theorem bijective_algebra_map_iff {R A : Type*} [field R] [semiring A] [nontrivial A] [algebra R A] : function.bijective (algebra_map R A) ↔ (⊤ : subalgebra R A) = ⊥ := ⟨λ h, surjective_algebra_map_iff.1 h.2, λ h, ⟨(algebra_map R A).injective, surjective_algebra_map_iff.2 h⟩⟩ /-- The bottom subalgebra is isomorphic to the base ring. -/ noncomputable def bot_equiv_of_injective (h : function.injective (algebra_map R A)) : (⊥ : subalgebra R A) ≃ₐ[R] R := alg_equiv.symm $ alg_equiv.of_bijective (algebra.of_id R _) ⟨λ x y hxy, h (congr_arg subtype.val hxy : _), λ ⟨y, hy⟩, let ⟨x, hx⟩ := algebra.mem_bot.1 hy in ⟨x, subtype.eq hx⟩⟩ /-- The bottom subalgebra is isomorphic to the field. -/ @[simps symm_apply] noncomputable def bot_equiv (F R : Type*) [field F] [semiring R] [nontrivial R] [algebra F R] : (⊥ : subalgebra F R) ≃ₐ[F] F := bot_equiv_of_injective (ring_hom.injective _) end algebra namespace subalgebra open algebra variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] variables (S : subalgebra R A) /-- The top subalgebra is isomorphic to the algebra. This is the algebra version of `submodule.top_equiv`. -/ @[simps] def top_equiv : (⊤ : subalgebra R A) ≃ₐ[R] A := alg_equiv.of_alg_hom (subalgebra.val ⊤) to_top rfl $ alg_hom.ext $ λ _, subtype.ext rfl -- TODO[gh-6025]: make this an instance once safe to do so lemma subsingleton_of_subsingleton [subsingleton A] : subsingleton (subalgebra R A) := ⟨λ B C, ext (λ x, by { simp only [subsingleton.elim x 0, zero_mem B, zero_mem C] })⟩ /-- For performance reasons this is not an instance. If you need this instance, add ``` local attribute [instance] alg_hom.subsingleton subalgebra.subsingleton_of_subsingleton ``` in the section that needs it. -/ -- TODO[gh-6025]: make this an instance once safe to do so lemma _root_.alg_hom.subsingleton [subsingleton (subalgebra R A)] : subsingleton (A →ₐ[R] B) := ⟨λ f g, alg_hom.ext $ λ a, have a ∈ (⊥ : subalgebra R A) := subsingleton.elim (⊤ : subalgebra R A) ⊥ ▸ mem_top, let ⟨x, hx⟩ := set.mem_range.mp (mem_bot.mp this) in hx ▸ (f.commutes _).trans (g.commutes _).symm⟩ -- TODO[gh-6025]: make this an instance once safe to do so lemma _root_.alg_equiv.subsingleton_left [subsingleton (subalgebra R A)] : subsingleton (A ≃ₐ[R] B) := begin haveI : subsingleton (A →ₐ[R] B) := alg_hom.subsingleton, exact ⟨λ f g, alg_equiv.ext (λ x, alg_hom.ext_iff.mp (subsingleton.elim f.to_alg_hom g.to_alg_hom) x)⟩, end -- TODO[gh-6025]: make this an instance once safe to do so lemma _root_.alg_equiv.subsingleton_right [subsingleton (subalgebra R B)] : subsingleton (A ≃ₐ[R] B) := begin haveI : subsingleton (B ≃ₐ[R] A) := alg_equiv.subsingleton_left, exact ⟨λ f g, eq.trans (alg_equiv.symm_symm _).symm (by rw [subsingleton.elim f.symm g.symm, alg_equiv.symm_symm])⟩ end lemma range_val : S.val.range = S := ext $ set.ext_iff.1 $ S.val.coe_range.trans subtype.range_val instance : unique (subalgebra R R) := { uniq := begin intro S, refine le_antisymm (λ r hr, _) bot_le, simp only [set.mem_range, mem_bot, id.map_eq_self, exists_apply_eq_apply, default], end .. algebra.subalgebra.inhabited } /-- The map `S → T` when `S` is a subalgebra contained in the subalgebra `T`. This is the subalgebra version of `submodule.of_le`, or `subring.inclusion` -/ def inclusion {S T : subalgebra R A} (h : S ≤ T) : S →ₐ[R] T := { to_fun := set.inclusion h, map_one' := rfl, map_add' := λ _ _, rfl, map_mul' := λ _ _, rfl, map_zero' := rfl, commutes' := λ _, rfl } lemma inclusion_injective {S T : subalgebra R A} (h : S ≤ T) : function.injective (inclusion h) := λ _ _, subtype.ext ∘ subtype.mk.inj @[simp] lemma inclusion_self {S : subalgebra R A}: inclusion (le_refl S) = alg_hom.id R S := alg_hom.ext $ λ x, subtype.ext rfl @[simp] lemma inclusion_mk {S T : subalgebra R A} (h : S ≤ T) (x : A) (hx : x ∈ S) : inclusion h ⟨x, hx⟩ = ⟨x, h hx⟩ := rfl lemma inclusion_right {S T : subalgebra R A} (h : S ≤ T) (x : T) (m : (x : A) ∈ S) : inclusion h ⟨x, m⟩ = x := subtype.ext rfl @[simp] lemma inclusion_inclusion {S T U : subalgebra R A} (hst : S ≤ T) (htu : T ≤ U) (x : S) : inclusion htu (inclusion hst x) = inclusion (le_trans hst htu) x := subtype.ext rfl @[simp] lemma coe_inclusion {S T : subalgebra R A} (h : S ≤ T) (s : S) : (inclusion h s : A) = s := rfl /-- Two subalgebras that are equal are also equivalent as algebras. This is the `subalgebra` version of `linear_equiv.of_eq` and `equiv.set.of_eq`. -/ @[simps apply] def equiv_of_eq (S T : subalgebra R A) (h : S = T) : S ≃ₐ[R] T := { to_fun := λ x, ⟨x, h ▸ x.2⟩, inv_fun := λ x, ⟨x, h.symm ▸ x.2⟩, map_mul' := λ _ _, rfl, commutes' := λ _, rfl, .. linear_equiv.of_eq _ _ (congr_arg to_submodule h) } @[simp] lemma equiv_of_eq_symm (S T : subalgebra R A) (h : S = T) : (equiv_of_eq S T h).symm = equiv_of_eq T S h.symm := rfl @[simp] lemma equiv_of_eq_rfl (S : subalgebra R A) : equiv_of_eq S S rfl = alg_equiv.refl := by { ext, refl } @[simp] lemma equiv_of_eq_trans (S T U : subalgebra R A) (hST : S = T) (hTU : T = U) : (equiv_of_eq S T hST).trans (equiv_of_eq T U hTU) = equiv_of_eq S U (trans hST hTU) := rfl section prod variables (S₁ : subalgebra R B) /-- The product of two subalgebras is a subalgebra. -/ def prod : subalgebra R (A × B) := { carrier := (S : set A) ×ˢ (S₁ : set B), algebra_map_mem' := λ r, ⟨algebra_map_mem _ _, algebra_map_mem _ _⟩, .. S.to_subsemiring.prod S₁.to_subsemiring } @[simp] lemma coe_prod : (prod S S₁ : set (A × B)) = (S : set A) ×ˢ (S₁ : set B):= rfl lemma prod_to_submodule : (S.prod S₁).to_submodule = S.to_submodule.prod S₁.to_submodule := rfl @[simp] lemma mem_prod {S : subalgebra R A} {S₁ : subalgebra R B} {x : A × B} : x ∈ prod S S₁ ↔ x.1 ∈ S ∧ x.2 ∈ S₁ := set.mem_prod @[simp] lemma prod_top : (prod ⊤ ⊤ : subalgebra R (A × B)) = ⊤ := by ext; simp lemma prod_mono {S T : subalgebra R A} {S₁ T₁ : subalgebra R B} : S ≤ T → S₁ ≤ T₁ → prod S S₁ ≤ prod T T₁ := set.prod_mono @[simp] lemma prod_inf_prod {S T : subalgebra R A} {S₁ T₁ : subalgebra R B} : S.prod S₁ ⊓ T.prod T₁ = (S ⊓ T).prod (S₁ ⊓ T₁) := set_like.coe_injective set.prod_inter_prod end prod section supr_lift variables {ι : Type*} lemma coe_supr_of_directed [nonempty ι] {S : ι → subalgebra R A} (dir : directed (≤) S) : ↑(supr S) = ⋃ i, (S i : set A) := let K : subalgebra R A := { carrier := ⋃ i, (S i), mul_mem' := λ x y hx hy, let ⟨i, hi⟩ := set.mem_Union.1 hx in let ⟨j, hj⟩ := set.mem_Union.1 hy in let ⟨k, hik, hjk⟩ := dir i j in set.mem_Union.2 ⟨k, subalgebra.mul_mem (S k) (hik hi) (hjk hj)⟩ , add_mem' := λ x y hx hy, let ⟨i, hi⟩ := set.mem_Union.1 hx in let ⟨j, hj⟩ := set.mem_Union.1 hy in let ⟨k, hik, hjk⟩ := dir i j in set.mem_Union.2 ⟨k, subalgebra.add_mem (S k) (hik hi) (hjk hj)⟩, algebra_map_mem' := λ r, let i := @nonempty.some ι infer_instance in set.mem_Union.2 ⟨i, subalgebra.algebra_map_mem _ _⟩ } in have supr S = K, from le_antisymm (supr_le (λ i, set.subset_Union (λ i, ↑(S i)) i)) (set_like.coe_subset_coe.1 (set.Union_subset (λ i, set_like.coe_subset_coe.2 (le_supr _ _)))), this.symm ▸ rfl /-- Define an algebra homomorphism on a directed supremum of subalgebras by defining it on each subalgebra, and proving that it agrees on the intersection of subalgebras. -/ noncomputable def supr_lift [nonempty ι] (K : ι → subalgebra R A) (dir : directed (≤) K) (f : Π i, K i →ₐ[R] B) (hf : ∀ (i j : ι) (h : K i ≤ K j), f i = (f j).comp (inclusion h)) (T : subalgebra R A) (hT : T = supr K) : ↥T →ₐ[R] B := by subst hT; exact { to_fun := set.Union_lift (λ i, ↑(K i)) (λ i x, f i x) (λ i j x hxi hxj, let ⟨k, hik, hjk⟩ := dir i j in begin rw [hf i k hik, hf j k hjk], refl end) ↑(supr K) (by rw coe_supr_of_directed dir; refl), map_one' := set.Union_lift_const _ (λ _, 1) (λ _, rfl) _ (by simp), map_zero' := set.Union_lift_const _ (λ _, 0) (λ _, rfl) _ (by simp), map_mul' := set.Union_lift_binary (coe_supr_of_directed dir) dir _ (λ _, (*)) (λ _ _ _, rfl) _ (by simp), map_add' := set.Union_lift_binary (coe_supr_of_directed dir) dir _ (λ _, (+)) (λ _ _ _, rfl) _ (by simp), commutes' := λ r, set.Union_lift_const _ (λ _, algebra_map _ _ r) (λ _, rfl) _ (λ i, by erw [alg_hom.commutes (f i)]) } variables [nonempty ι] {K : ι → subalgebra R A} {dir : directed (≤) K} {f : Π i, K i →ₐ[R] B} {hf : ∀ (i j : ι) (h : K i ≤ K j), f i = (f j).comp (inclusion h)} {T : subalgebra R A} {hT : T = supr K} @[simp] lemma supr_lift_inclusion {i : ι} (x : K i) (h : K i ≤ T) : supr_lift K dir f hf T hT (inclusion h x) = f i x := by subst T; exact set.Union_lift_inclusion _ _ @[simp] lemma supr_lift_comp_inclusion {i : ι} (h : K i ≤ T) : (supr_lift K dir f hf T hT).comp (inclusion h) = f i := by ext; simp @[simp] lemma supr_lift_mk {i : ι} (x : K i) (hx : (x : A) ∈ T) : supr_lift K dir f hf T hT ⟨x, hx⟩ = f i x := by subst hT; exact set.Union_lift_mk x hx lemma supr_lift_of_mem {i : ι} (x : T) (hx : (x : A) ∈ K i) : supr_lift K dir f hf T hT x = f i ⟨x, hx⟩ := by subst hT; exact set.Union_lift_of_mem x hx end supr_lift /-! ## Actions by `subalgebra`s These are just copies of the definitions about `subsemiring` starting from `subring.mul_action`. -/ section actions variables {α β : Type*} /-- The action by a subalgebra is the action by the underlying algebra. -/ instance [has_smul A α] (S : subalgebra R A) : has_smul S α := S.to_subsemiring.has_smul lemma smul_def [has_smul A α] {S : subalgebra R A} (g : S) (m : α) : g • m = (g : A) • m := rfl instance smul_comm_class_left [has_smul A β] [has_smul α β] [smul_comm_class A α β] (S : subalgebra R A) : smul_comm_class S α β := S.to_subsemiring.smul_comm_class_left instance smul_comm_class_right [has_smul α β] [has_smul A β] [smul_comm_class α A β] (S : subalgebra R A) : smul_comm_class α S β := S.to_subsemiring.smul_comm_class_right /-- Note that this provides `is_scalar_tower S R R` which is needed by `smul_mul_assoc`. -/ instance is_scalar_tower_left [has_smul α β] [has_smul A α] [has_smul A β] [is_scalar_tower A α β] (S : subalgebra R A) : is_scalar_tower S α β := S.to_subsemiring.is_scalar_tower instance is_scalar_tower_mid {R S T : Type*} [comm_semiring R] [semiring S] [add_comm_monoid T] [algebra R S] [module R T] [module S T] [is_scalar_tower R S T] (S' : subalgebra R S) : is_scalar_tower R S' T := ⟨λ x y z, (smul_assoc _ (y : S) _ : _)⟩ instance [has_smul A α] [has_faithful_smul A α] (S : subalgebra R A) : has_faithful_smul S α := S.to_subsemiring.has_faithful_smul /-- The action by a subalgebra is the action by the underlying algebra. -/ instance [mul_action A α] (S : subalgebra R A) : mul_action S α := S.to_subsemiring.mul_action /-- The action by a subalgebra is the action by the underlying algebra. -/ instance [add_monoid α] [distrib_mul_action A α] (S : subalgebra R A) : distrib_mul_action S α := S.to_subsemiring.distrib_mul_action /-- The action by a subalgebra is the action by the underlying algebra. -/ instance [has_zero α] [smul_with_zero A α] (S : subalgebra R A) : smul_with_zero S α := S.to_subsemiring.smul_with_zero /-- The action by a subalgebra is the action by the underlying algebra. -/ instance [has_zero α] [mul_action_with_zero A α] (S : subalgebra R A) : mul_action_with_zero S α := S.to_subsemiring.mul_action_with_zero /-- The action by a subalgebra is the action by the underlying algebra. -/ instance module_left [add_comm_monoid α] [module A α] (S : subalgebra R A) : module S α := S.to_subsemiring.module /-- The action by a subalgebra is the action by the underlying algebra. -/ instance to_algebra {R A : Type*} [comm_semiring R] [comm_semiring A] [semiring α] [algebra R A] [algebra A α] (S : subalgebra R A) : algebra S α := algebra.of_subsemiring S.to_subsemiring lemma algebra_map_eq {R A : Type*} [comm_semiring R] [comm_semiring A] [semiring α] [algebra R A] [algebra A α] (S : subalgebra R A) : algebra_map S α = (algebra_map A α).comp S.val := rfl @[simp] lemma srange_algebra_map {R A : Type*} [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) : (algebra_map S A).srange = S.to_subsemiring := by rw [algebra_map_eq, algebra.id.map_eq_id, ring_hom.id_comp, ← to_subsemiring_subtype, subsemiring.srange_subtype] @[simp] lemma range_algebra_map {R A : Type*} [comm_ring R] [comm_ring A] [algebra R A] (S : subalgebra R A) : (algebra_map S A).range = S.to_subring := by rw [algebra_map_eq, algebra.id.map_eq_id, ring_hom.id_comp, ← to_subring_subtype, subring.range_subtype] instance no_zero_smul_divisors_top [no_zero_divisors A] (S : subalgebra R A) : no_zero_smul_divisors S A := ⟨λ c x h, have (c : A) = 0 ∨ x = 0, from eq_zero_or_eq_zero_of_mul_eq_zero h, this.imp_left (@subtype.ext_iff _ _ c 0).mpr⟩ end actions section center lemma _root_.set.algebra_map_mem_center (r : R) : algebra_map R A r ∈ set.center A := by simp [algebra.commutes, set.mem_center_iff] variables (R A) /-- The center of an algebra is the set of elements which commute with every element. They form a subalgebra. -/ def center : subalgebra R A := { algebra_map_mem' := set.algebra_map_mem_center, .. subsemiring.center A } lemma coe_center : (center R A : set A) = set.center A := rfl @[simp] lemma center_to_subsemiring : (center R A).to_subsemiring = subsemiring.center A := rfl @[simp] lemma center_to_subring (R A : Type*) [comm_ring R] [ring A] [algebra R A] : (center R A).to_subring = subring.center A := rfl @[simp] lemma center_eq_top (A : Type*) [comm_semiring A] [algebra R A] : center R A = ⊤ := set_like.coe_injective (set.center_eq_univ A) variables {R A} instance : comm_semiring (center R A) := subsemiring.center.comm_semiring instance {A : Type*} [ring A] [algebra R A] : comm_ring (center R A) := subring.center.comm_ring lemma mem_center_iff {a : A} : a ∈ center R A ↔ ∀ (b : A), b*a = a*b := iff.rfl end center section centralizer @[simp] lemma _root_.set.algebra_map_mem_centralizer {s : set A} (r : R) : algebra_map R A r ∈ s.centralizer := λ a h, (algebra.commutes _ _).symm variables (R) /-- The centralizer of a set as a subalgebra. -/ def centralizer (s : set A) : subalgebra R A := { algebra_map_mem' := set.algebra_map_mem_centralizer, ..subsemiring.centralizer s, } @[simp, norm_cast] lemma coe_centralizer (s : set A) : (centralizer R s : set A) = s.centralizer := rfl lemma mem_centralizer_iff {s : set A} {z : A} : z ∈ centralizer R s ↔ ∀ g ∈ s, g * z = z * g := iff.rfl lemma centralizer_le (s t : set A) (h : s ⊆ t) : centralizer R t ≤ centralizer R s := set.centralizer_subset h @[simp] lemma centralizer_univ : centralizer R set.univ = center R A := set_like.ext' (set.centralizer_univ A) end centralizer end subalgebra section nat variables {R : Type*} [semiring R] /-- A subsemiring is a `ℕ`-subalgebra. -/ def subalgebra_of_subsemiring (S : subsemiring R) : subalgebra ℕ R := { algebra_map_mem' := λ i, coe_nat_mem S i, .. S } @[simp] lemma mem_subalgebra_of_subsemiring {x : R} {S : subsemiring R} : x ∈ subalgebra_of_subsemiring S ↔ x ∈ S := iff.rfl end nat section int variables {R : Type*} [ring R] /-- A subring is a `ℤ`-subalgebra. -/ def subalgebra_of_subring (S : subring R) : subalgebra ℤ R := { algebra_map_mem' := λ i, int.induction_on i (by simpa using S.zero_mem) (λ i ih, by simpa using S.add_mem ih S.one_mem) (λ i ih, show ((-i - 1 : ℤ) : R) ∈ S, by { rw [int.cast_sub, int.cast_one], exact S.sub_mem ih S.one_mem }), .. S } variables {S : Type*} [semiring S] @[simp] lemma mem_subalgebra_of_subring {x : R} {S : subring R} : x ∈ subalgebra_of_subring S ↔ x ∈ S := iff.rfl end int
d0a020e49d0ccccf9fb904404f8a34ae690a0cea
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/convex/join.lean
5255a3d07c2a6157592c388f6fc44d393ff34179
[ "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
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 y ⟨xa, hxa, xb, hxb, hx⟩ ⟨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
3ef8aa428541ff5c9c22afe3ec8dcd56456aa707
b7f22e51856f4989b970961f794f1c435f9b8f78
/hott/hit/two_quotient.hlean
0919efe0962036308d17f048037e47a5e921589f
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
31,451
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import homotopy.circle eq2 algebra.e_closure cubical.squareover cubical.cube cubical.square2 open quotient eq circle sum sigma equiv function relation e_closure /- This files defines a general class of nonrecursive 2-HITs using just quotients. We can define any HIT X which has - a single 0-constructor f : A → X (for some type A) - a single 1-constructor e : Π{a a' : A}, R a a' → a = a' (for some (type-valued) relation R on A) and furthermore has 2-constructors which are all of the form p = p' where p, p' are of the form - refl (f a), for some a : A; - e r, for some r : R a a'; - ap f q, where q : a = a' :> A; - inverses of such paths; - concatenations of such paths. so an example 2-constructor could be (as long as it typechecks): ap f q' ⬝ ((e r)⁻¹ ⬝ ap f q)⁻¹ ⬝ e r' = idp We first define "simple two quotients" which have as requirement that the right hand side is idp Then we define "two quotients" which can have an arbitrary path on the right hand side Then we define "truncated two quotients", which is a two quotient followed by n-truncation, and show that this satisfies the desired induction principle and computation rule. Caveat: for none of these constructions we show that the induction priniciple computes on 2-paths. However, with truncated two quotients, if the truncation is a 1-truncation, then this computation rule follows automatically, since the target is a 1-type. -/ namespace simple_two_quotient section parameters {A : Type} (R : A → A → Type) local abbreviation T := e_closure R -- the (type-valued) equivalence closure of R parameter (Q : Π⦃a⦄, T a a → Type) variables ⦃a a' : A⦄ {s : R a a'} {r : T a a} local abbreviation B := A ⊎ Σ(a : A) (r : T a a), Q r inductive pre_two_quotient_rel : B → B → Type := | pre_Rmk {} : Π⦃a a'⦄ (r : R a a'), pre_two_quotient_rel (inl a) (inl a') --BUG: if {} not provided, the alias for pre_Rmk is wrong definition pre_two_quotient := quotient pre_two_quotient_rel open pre_two_quotient_rel local abbreviation C := quotient pre_two_quotient_rel protected definition j [constructor] (a : A) : C := class_of pre_two_quotient_rel (inl a) protected definition pre_aux [constructor] (q : Q r) : C := class_of pre_two_quotient_rel (inr ⟨a, r, q⟩) protected definition e (s : R a a') : j a = j a' := eq_of_rel _ (pre_Rmk s) protected definition et (t : T a a') : j a = j a' := e_closure.elim e t protected definition f [unfold 7] (q : Q r) : S¹ → C := circle.elim (j a) (et r) protected definition pre_rec [unfold 8] {P : C → Type} (Pj : Πa, P (j a)) (Pa : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), P (pre_aux q)) (Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a =[e s] Pj a') (x : C) : P x := begin induction x with p, { induction p, { apply Pj}, { induction a with a1 a2, induction a2, apply Pa}}, { induction H, esimp, apply Pe}, end protected definition pre_elim [unfold 8] {P : Type} (Pj : A → P) (Pa : Π⦃a : A⦄ ⦃r : T a a⦄, Q r → P) (Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a = Pj a') (x : C) : P := pre_rec Pj Pa (λa a' s, pathover_of_eq _ (Pe s)) x protected theorem rec_e {P : C → Type} (Pj : Πa, P (j a)) (Pa : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), P (pre_aux q)) (Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a =[e s] Pj a') ⦃a a' : A⦄ (s : R a a') : apd (pre_rec Pj Pa Pe) (e s) = Pe s := !rec_eq_of_rel protected theorem elim_e {P : Type} (Pj : A → P) (Pa : Π⦃a : A⦄ ⦃r : T a a⦄, Q r → P) (Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a = Pj a') ⦃a a' : A⦄ (s : R a a') : ap (pre_elim Pj Pa Pe) (e s) = Pe s := begin apply eq_of_fn_eq_fn_inv !(pathover_constant (e s)), rewrite [▸*,-apd_eq_pathover_of_eq_ap,↑pre_elim,rec_e], end protected definition elim_et {P : Type} (Pj : A → P) (Pa : Π⦃a : A⦄ ⦃r : T a a⦄, Q r → P) (Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a = Pj a') ⦃a a' : A⦄ (t : T a a') : ap (pre_elim Pj Pa Pe) (et t) = e_closure.elim Pe t := ap_e_closure_elim_h e (elim_e Pj Pa Pe) t protected definition rec_et {P : C → Type} (Pj : Πa, P (j a)) (Pa : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), P (pre_aux q)) (Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a =[e s] Pj a') ⦃a a' : A⦄ (t : T a a') : apd (pre_rec Pj Pa Pe) (et t) = e_closure.elimo e Pe t := ap_e_closure_elimo_h e Pe (rec_e Pj Pa Pe) t inductive simple_two_quotient_rel : C → C → Type := | Rmk {} : Π{a : A} {r : T a a} (q : Q r) (x : circle), simple_two_quotient_rel (f q x) (pre_aux q) open simple_two_quotient_rel definition simple_two_quotient := quotient simple_two_quotient_rel local abbreviation D := simple_two_quotient local abbreviation i := class_of simple_two_quotient_rel definition incl0 (a : A) : D := i (j a) protected definition aux (q : Q r) : D := i (pre_aux q) definition incl1 (s : R a a') : incl0 a = incl0 a' := ap i (e s) definition inclt (t : T a a') : incl0 a = incl0 a' := e_closure.elim incl1 t -- "wrong" version inclt, which is ap i (p ⬝ q) instead of ap i p ⬝ ap i q -- it is used in the proof, because incltw is easier to work with protected definition incltw (t : T a a') : incl0 a = incl0 a' := ap i (et t) protected definition inclt_eq_incltw (t : T a a') : inclt t = incltw t := (ap_e_closure_elim i e t)⁻¹ definition incl2' (q : Q r) (x : S¹) : i (f q x) = aux q := eq_of_rel simple_two_quotient_rel (Rmk q x) protected definition incl2w (q : Q r) : incltw r = idp := (ap02 i (elim_loop (j a) (et r))⁻¹) ⬝ (ap_compose i (f q) loop)⁻¹ ⬝ ap_is_constant (incl2' q) loop ⬝ !con.right_inv definition incl2 (q : Q r) : inclt r = idp := inclt_eq_incltw r ⬝ incl2w q local attribute simple_two_quotient f i D incl0 aux incl1 incl2' inclt [reducible] local attribute i aux incl0 [constructor] parameters {R Q} protected definition rec {P : D → Type} (P0 : Π(a : A), P (incl0 a)) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a =[incl1 s] P0 a') (P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), change_path (incl2 q) (e_closure.elimo incl1 P1 r) = idpo) (x : D) : P x := begin induction x, { refine (pre_rec _ _ _ a), { exact P0}, { intro a r q, exact incl2' q base ▸ P0 a}, { intro a a' s, exact pathover_of_pathover_ap P i (P1 s)}}, { exact abstract [irreducible] begin induction H, induction x, { esimp, exact pathover_tr (incl2' q base) (P0 a)}, { apply pathover_pathover, esimp, fold [i, incl2' q], refine eq_hconcato _ _, apply _, { transitivity _, { apply ap (pathover_ap _ _), transitivity _, apply apd_compose2 (pre_rec P0 _ _) (f q) loop, apply ap (pathover_of_pathover_ap _ _), transitivity _, apply apd_change_path, exact !elim_loop⁻¹, transitivity _, apply ap (change_path _), transitivity _, apply rec_et, transitivity (pathover_of_pathover_ap P i (change_path (inclt_eq_incltw r) (e_closure.elimo incl1 (λ (a a' : A) (s : R a a'), P1 s) r))), apply e_closure_elimo_ap, exact idp, apply change_path_pathover_of_pathover_ap}, esimp, transitivity _, apply pathover_ap_pathover_of_pathover_ap P i (f q), transitivity _, apply ap (change_path _), apply to_right_inv !pathover_compose, do 2 (transitivity _; exact !change_path_con⁻¹), transitivity _, apply ap (change_path _), exact (to_left_inv (change_path_equiv _ _ (incl2 q)) _)⁻¹, esimp, rewrite P2, transitivity _; exact !change_path_con⁻¹, apply ap (λx, change_path x _), rewrite [↑incl2, con_inv], transitivity _, exact !con.assoc⁻¹, rewrite [inv_con_cancel_right, ↑incl2w, ↑ap02, +con_inv, +ap_inv, +inv_inv, -+con.assoc, +con_inv_cancel_right], reflexivity}, rewrite [change_path_con, apd_constant], apply squareover_change_path_left, apply squareover_change_path_right', apply squareover_change_path_left, refine change_square _ vrflo, symmetry, apply inv_ph_eq_of_eq_ph, rewrite [ap_is_constant_natural_square], apply whisker_bl_whisker_tl_eq} end end}, end protected definition rec_on [reducible] {P : D → Type} (x : D) (P0 : Π(a : A), P (incl0 a)) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a =[incl1 s] P0 a') (P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), change_path (incl2 q) (e_closure.elimo incl1 P1 r) = idpo) : P x := rec P0 P1 P2 x theorem rec_incl1 {P : D → Type} (P0 : Π(a : A), P (incl0 a)) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a =[incl1 s] P0 a') (P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), change_path (incl2 q) (e_closure.elimo incl1 P1 r) = idpo) ⦃a a' : A⦄ (s : R a a') : apd (rec P0 P1 P2) (incl1 s) = P1 s := begin unfold [rec, incl1], refine !apd_ap ⬝ _, esimp, rewrite rec_e, apply to_right_inv !pathover_compose end theorem rec_inclt {P : D → Type} (P0 : Π(a : A), P (incl0 a)) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a =[incl1 s] P0 a') (P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), change_path (incl2 q) (e_closure.elimo incl1 P1 r) = idpo) ⦃a a' : A⦄ (t : T a a') : apd (rec P0 P1 P2) (inclt t) = e_closure.elimo incl1 P1 t := ap_e_closure_elimo_h incl1 P1 (rec_incl1 P0 P1 P2) t protected definition elim {P : Type} (P0 : A → P) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a') (P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp) (x : D) : P := begin induction x, { refine (pre_elim _ _ _ a), { exact P0}, { intro a r q, exact P0 a}, { exact P1}}, { exact abstract begin induction H, induction x, { exact idpath (P0 a)}, { unfold f, apply eq_pathover, apply hdeg_square, exact abstract ap_compose (pre_elim P0 _ P1) (f q) loop ⬝ ap _ !elim_loop ⬝ !elim_et ⬝ P2 q ⬝ !ap_constant⁻¹ end} end end}, end local attribute elim [unfold 8] protected definition elim_on {P : Type} (x : D) (P0 : A → P) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a') (P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp) : P := elim P0 P1 P2 x definition elim_incl1 {P : Type} {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp) ⦃a a' : A⦄ (s : R a a') : ap (elim P0 P1 P2) (incl1 s) = P1 s := (ap_compose (elim P0 P1 P2) i (e s))⁻¹ ⬝ !elim_e definition elim_inclt {P : Type} {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp) ⦃a a' : A⦄ (t : T a a') : ap (elim P0 P1 P2) (inclt t) = e_closure.elim P1 t := ap_e_closure_elim_h incl1 (elim_incl1 P2) t protected definition elim_incltw {P : Type} {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp) ⦃a a' : A⦄ (t : T a a') : ap (elim P0 P1 P2) (incltw t) = e_closure.elim P1 t := (ap_compose (elim P0 P1 P2) i (et t))⁻¹ ⬝ !elim_et protected theorem elim_inclt_eq_elim_incltw {P : Type} {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp) ⦃a a' : A⦄ (t : T a a') : elim_inclt P2 t = ap (ap (elim P0 P1 P2)) (inclt_eq_incltw t) ⬝ elim_incltw P2 t := begin unfold [elim_inclt,elim_incltw,inclt_eq_incltw,et], refine !ap_e_closure_elim_h_eq ⬝ _, rewrite [ap_inv,-con.assoc], xrewrite [eq_of_square (ap_ap_e_closure_elim i (elim P0 P1 P2) e t)⁻¹ʰ], rewrite [↓incl1,con.assoc], apply whisker_left, rewrite [↑[elim_et,elim_incl1],+ap_e_closure_elim_h_eq,con_inv,↑[i,function.compose]], rewrite [-con.assoc (_ ⬝ _),con.assoc _⁻¹,con.left_inv,▸*,-ap_inv,-ap_con], apply ap (ap _), krewrite [-eq_of_homotopy3_inv,-eq_of_homotopy3_con] end definition elim_incl2' {P : Type} {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp) ⦃a : A⦄ ⦃r : T a a⦄ (q : Q r) : ap (elim P0 P1 P2) (incl2' q base) = idpath (P0 a) := !elim_eq_of_rel protected theorem elim_incl2w {P : Type} (P0 : A → P) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a') (P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp) ⦃a : A⦄ ⦃r : T a a⦄ (q : Q r) : square (ap02 (elim P0 P1 P2) (incl2w q)) (P2 q) (elim_incltw P2 r) idp := begin esimp [incl2w,ap02], rewrite [+ap_con (ap _),▸*], xrewrite [-ap_compose (ap _) (ap i)], rewrite [+ap_inv], xrewrite [eq_top_of_square ((ap_compose_natural (elim P0 P1 P2) i (elim_loop (j a) (et r)))⁻¹ʰ⁻¹ᵛ ⬝h (ap_ap_compose (elim P0 P1 P2) i (f q) loop)⁻¹ʰ⁻¹ᵛ ⬝h ap_ap_is_constant (elim P0 P1 P2) (incl2' q) loop ⬝h ap_con_right_inv_sq (elim P0 P1 P2) (incl2' q base)), ↑[elim_incltw]], apply whisker_tl, rewrite [ap_is_constant_eq], xrewrite [naturality_apd_eq (λx, !elim_eq_of_rel) loop], rewrite [↑elim_2,rec_loop,square_of_pathover_concato_eq,square_of_pathover_eq_concato, eq_of_square_vconcat_eq,eq_of_square_eq_vconcat], apply eq_vconcat, { apply ap (λx, _ ⬝ eq_con_inv_of_con_eq ((_ ⬝ x ⬝ _)⁻¹ ⬝ _) ⬝ _), transitivity _, apply ap eq_of_square, apply to_right_inv !eq_pathover_equiv_square (hdeg_square (elim_1 P A R Q P0 P1 a r q P2)), transitivity _, apply eq_of_square_hdeg_square, unfold elim_1, reflexivity}, rewrite [+con_inv,whisker_left_inv,+inv_inv,-whisker_right_inv, con.assoc (whisker_left _ _),con.assoc _ (whisker_right _ _),▸*, whisker_right_con_whisker_left _ !ap_constant], xrewrite [-con.assoc _ _ (whisker_right _ _)], rewrite [con.assoc _ _ (whisker_left _ _),idp_con_whisker_left,▸*, con.assoc _ !ap_constant⁻¹,con.left_inv], xrewrite [eq_con_inv_of_con_eq_whisker_left,▸*], rewrite [+con.assoc _ _ !con.right_inv, right_inv_eq_idp ( (λ(x : ap (elim P0 P1 P2) (incl2' q base) = idpath (elim P0 P1 P2 (class_of simple_two_quotient_rel (f q base)))), x) (elim_incl2' P2 q)), ↑[whisker_left]], xrewrite [con2_con_con2], rewrite [idp_con,↑elim_incl2',con.left_inv,whisker_right_inv,↑whisker_right], xrewrite [con.assoc _ _ (_ ◾ _)], rewrite [con.left_inv,▸*,-+con.assoc,con.assoc _⁻¹,↑[elim,function.compose],con.left_inv, ▸*,↑j,con.left_inv,idp_con], apply square_of_eq, reflexivity end theorem elim_incl2 {P : Type} (P0 : A → P) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a') (P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp) ⦃a : A⦄ ⦃r : T a a⦄ (q : Q r) : square (ap02 (elim P0 P1 P2) (incl2 q)) (P2 q) (elim_inclt P2 r) idp := begin rewrite [↑incl2,↑ap02,ap_con,elim_inclt_eq_elim_incltw], apply whisker_tl, apply elim_incl2w end end end simple_two_quotient export [unfold] simple_two_quotient attribute simple_two_quotient.j simple_two_quotient.incl0 [constructor] attribute simple_two_quotient.rec simple_two_quotient.elim [unfold 8] [recursor 8] --attribute simple_two_quotient.elim_type [unfold 9] -- TODO attribute simple_two_quotient.rec_on simple_two_quotient.elim_on [unfold 5] --attribute simple_two_quotient.elim_type_on [unfold 6] -- TODO namespace two_quotient open simple_two_quotient section parameters {A : Type} (R : A → A → Type) local abbreviation T := e_closure R -- the (type-valued) equivalence closure of R parameter (Q : Π⦃a a'⦄, T a a' → T a a' → Type) variables ⦃a a' a'' : A⦄ {s : R a a'} {t t' : T a a'} inductive two_quotient_Q : Π⦃a : A⦄, e_closure R a a → Type := | Qmk : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄, Q t t' → two_quotient_Q (t ⬝r t'⁻¹ʳ) open two_quotient_Q local abbreviation Q2 := two_quotient_Q definition two_quotient := simple_two_quotient R Q2 definition incl0 (a : A) : two_quotient := incl0 _ _ a definition incl1 (s : R a a') : incl0 a = incl0 a' := incl1 _ _ s definition inclt (t : T a a') : incl0 a = incl0 a' := e_closure.elim incl1 t definition incl2 (q : Q t t') : inclt t = inclt t' := eq_of_con_inv_eq_idp (incl2 _ _ (Qmk R q)) parameters {R Q} protected definition rec {P : two_quotient → Type} (P0 : Π(a : A), P (incl0 a)) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a =[incl1 s] P0 a') (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), change_path (incl2 q) (e_closure.elimo incl1 P1 t) = e_closure.elimo incl1 P1 t') (x : two_quotient) : P x := begin induction x, { exact P0 a}, { exact P1 s}, { exact abstract [irreducible] begin induction q with a a' t t' q, rewrite [elimo_trans (simple_two_quotient.incl1 R Q2) P1, elimo_symm (simple_two_quotient.incl1 R Q2) P1, -whisker_right_eq_of_con_inv_eq_idp (simple_two_quotient.incl2 R Q2 (Qmk R q)), change_path_con], xrewrite [change_path_cono], refine ap (λx, change_path _ (_ ⬝o x)) !change_path_invo ⬝ _, esimp, apply cono_invo_eq_idpo, apply P2 end end} end protected definition rec_on [reducible] {P : two_quotient → Type} (x : two_quotient) (P0 : Π(a : A), P (incl0 a)) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a =[incl1 s] P0 a') (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), change_path (incl2 q) (e_closure.elimo incl1 P1 t) = e_closure.elimo incl1 P1 t') : P x := rec P0 P1 P2 x theorem rec_incl1 {P : two_quotient → Type} (P0 : Π(a : A), P (incl0 a)) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a =[incl1 s] P0 a') (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), change_path (incl2 q) (e_closure.elimo incl1 P1 t) = e_closure.elimo incl1 P1 t') ⦃a a' : A⦄ (s : R a a') : apd (rec P0 P1 P2) (incl1 s) = P1 s := rec_incl1 _ _ _ s theorem rec_inclt {P : two_quotient → Type} (P0 : Π(a : A), P (incl0 a)) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a =[incl1 s] P0 a') (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), change_path (incl2 q) (e_closure.elimo incl1 P1 t) = e_closure.elimo incl1 P1 t') ⦃a a' : A⦄ (t : T a a') : apd (rec P0 P1 P2) (inclt t) = e_closure.elimo incl1 P1 t := rec_inclt _ _ _ t protected definition elim {P : Type} (P0 : A → P) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a') (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') (x : two_quotient) : P := begin induction x, { exact P0 a}, { exact P1 s}, { exact abstract [unfold 10] begin induction q with a a' t t' q, esimp [e_closure.elim], apply con_inv_eq_idp, exact P2 q end end}, end local attribute elim [unfold 8] protected definition elim_on {P : Type} (x : two_quotient) (P0 : A → P) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a') (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') : P := elim P0 P1 P2 x definition elim_incl1 {P : Type} {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') ⦃a a' : A⦄ (s : R a a') : ap (elim P0 P1 P2) (incl1 s) = P1 s := !elim_incl1 definition elim_inclt {P : Type} {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') ⦃a a' : A⦄ (t : T a a') : ap (elim P0 P1 P2) (inclt t) = e_closure.elim P1 t := ap_e_closure_elim_h incl1 (elim_incl1 P2) t theorem elim_incl2 {P : Type} (P0 : A → P) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a') (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') ⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t') : square (ap02 (elim P0 P1 P2) (incl2 q)) (P2 q) (elim_inclt P2 t) (elim_inclt P2 t') := begin rewrite [↑[incl2,elim],ap_eq_of_con_inv_eq_idp], xrewrite [eq_top_of_square (elim_incl2 P0 P1 (elim_1 A R Q P P0 P1 P2) (Qmk R q))], xrewrite [{simple_two_quotient.elim_inclt (elim_1 A R Q P P0 P1 P2) (t ⬝r t'⁻¹ʳ)} idpath (ap_con (simple_two_quotient.elim P0 P1 (elim_1 A R Q P P0 P1 P2)) (inclt t) (inclt t')⁻¹ ⬝ (simple_two_quotient.elim_inclt (elim_1 A R Q P P0 P1 P2) t ◾ (ap_inv (simple_two_quotient.elim P0 P1 (elim_1 A R Q P P0 P1 P2)) (inclt t') ⬝ inverse2 (simple_two_quotient.elim_inclt (elim_1 A R Q P P0 P1 P2) t')))),▸*], rewrite [-con.assoc _ _ (con_inv_eq_idp _),-con.assoc _ _ (_ ◾ _),con.assoc _ _ (ap_con _ _ _), con.left_inv,↑whisker_left,con2_con_con2,-con.assoc (ap_inv _ _)⁻¹, con.left_inv,+idp_con,eq_of_con_inv_eq_idp_con2], xrewrite [to_left_inv !eq_equiv_con_inv_eq_idp (P2 q)], apply top_deg_square end definition elim_inclt_rel [unfold_full] {P : Type} {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') ⦃a a' : A⦄ (r : R a a') : elim_inclt P2 [r] = elim_incl1 P2 r := idp definition elim_inclt_inv [unfold_full] {P : Type} {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') ⦃a a' : A⦄ (t : T a a') : elim_inclt P2 t⁻¹ʳ = ap_inv (elim P0 P1 P2) (inclt t) ⬝ (elim_inclt P2 t)⁻² := idp definition elim_inclt_con [unfold_full] {P : Type} {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') ⦃a a' a'' : A⦄ (t : T a a') (t': T a' a'') : elim_inclt P2 (t ⬝r t') = ap_con (elim P0 P1 P2) (inclt t) (inclt t') ⬝ (elim_inclt P2 t ◾ elim_inclt P2 t') := idp definition inclt_rel [unfold_full] (r : R a a') : inclt [r] = incl1 r := idp definition inclt_inv [unfold_full] (t : T a a') : inclt t⁻¹ʳ = (inclt t)⁻¹ := idp definition inclt_con [unfold_full] (t : T a a') (t' : T a' a'') : inclt (t ⬝r t') = inclt t ⬝ inclt t' := idp end end two_quotient attribute two_quotient.incl0 [constructor] attribute two_quotient.rec two_quotient.elim [unfold 8] [recursor 8] --attribute two_quotient.elim_type [unfold 9] attribute two_quotient.rec_on two_quotient.elim_on [unfold 5] --attribute two_quotient.elim_type_on [unfold 6] open two_quotient is_trunc trunc namespace trunc_two_quotient section parameters (n : ℕ₋₂) {A : Type} (R : A → A → Type) local abbreviation T := e_closure R -- the (type-valued) equivalence closure of R parameter (Q : Π⦃a a'⦄, T a a' → T a a' → Type) variables ⦃a a' a'' : A⦄ {s : R a a'} {t t' : T a a'} definition trunc_two_quotient := trunc n (two_quotient R Q) parameters {n R Q} definition incl0 (a : A) : trunc_two_quotient := tr (!incl0 a) definition incl1 (s : R a a') : incl0 a = incl0 a' := ap tr (!incl1 s) definition incltw (t : T a a') : incl0 a = incl0 a' := ap tr (!inclt t) definition inclt (t : T a a') : incl0 a = incl0 a' := e_closure.elim incl1 t definition incl2w (q : Q t t') : incltw t = incltw t' := ap02 tr (!incl2 q) definition incl2 (q : Q t t') : inclt t = inclt t' := !ap_e_closure_elim⁻¹ ⬝ ap02 tr (!incl2 q) ⬝ !ap_e_closure_elim local attribute trunc_two_quotient incl0 [reducible] definition is_trunc_trunc_two_quotient [instance] : is_trunc n trunc_two_quotient := _ protected definition rec {P : trunc_two_quotient → Type} [H : Πx, is_trunc n (P x)] (P0 : Π(a : A), P (incl0 a)) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a =[incl1 s] P0 a') (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), change_path (incl2 q) (e_closure.elimo incl1 P1 t) = e_closure.elimo incl1 P1 t') (x : trunc_two_quotient) : P x := begin induction x, induction a, { exact P0 a}, { exact !pathover_of_pathover_ap (P1 s)}, { exact abstract [irreducible] by rewrite [+ e_closure_elimo_ap, ↓incl1, -P2 q, change_path_pathover_of_pathover_ap, - + change_path_con, ↑incl2, con_inv_cancel_right] end} end protected definition rec_on [reducible] {P : trunc_two_quotient → Type} [H : Πx, is_trunc n (P x)] (x : trunc_two_quotient) (P0 : Π(a : A), P (incl0 a)) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a =[incl1 s] P0 a') (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), change_path (incl2 q) (e_closure.elimo incl1 P1 t) = e_closure.elimo incl1 P1 t') : P x := rec P0 P1 P2 x theorem rec_incl1 {P : trunc_two_quotient → Type} [H : Πx, is_trunc n (P x)] (P0 : Π(a : A), P (incl0 a)) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a =[incl1 s] P0 a') (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), change_path (incl2 q) (e_closure.elimo incl1 P1 t) = e_closure.elimo incl1 P1 t') ⦃a a' : A⦄ (s : R a a') : apd (rec P0 P1 P2) (incl1 s) = P1 s := !apd_ap ⬝ ap !pathover_ap !rec_incl1 ⬝ to_right_inv !pathover_compose (P1 s) theorem rec_inclt {P : trunc_two_quotient → Type} [H : Πx, is_trunc n (P x)] (P0 : Π(a : A), P (incl0 a)) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a =[incl1 s] P0 a') (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), change_path (incl2 q) (e_closure.elimo incl1 P1 t) = e_closure.elimo incl1 P1 t') ⦃a a' : A⦄ (t : T a a') : apd (rec P0 P1 P2) (inclt t) = e_closure.elimo incl1 P1 t := ap_e_closure_elimo_h incl1 P1 (rec_incl1 P0 P1 P2) t protected definition elim {P : Type} (P0 : A → P) [H : is_trunc n P] (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a') (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') (x : trunc_two_quotient) : P := begin induction x, induction a, { exact P0 a}, { exact P1 s}, { exact P2 q}, end local attribute elim [unfold 10] protected definition elim_on {P : Type} [H : is_trunc n P] (x : trunc_two_quotient) (P0 : A → P) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a') (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') : P := elim P0 P1 P2 x definition elim_incl1 {P : Type} [H : is_trunc n P] {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') ⦃a a' : A⦄ (s : R a a') : ap (elim P0 P1 P2) (incl1 s) = P1 s := !ap_compose⁻¹ ⬝ !elim_incl1 definition elim_inclt {P : Type} [H : is_trunc n P] {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') ⦃a a' : A⦄ (t : T a a') : ap (elim P0 P1 P2) (inclt t) = e_closure.elim P1 t := ap_e_closure_elim_h incl1 (elim_incl1 P2) t open function theorem elim_incl2 {P : Type} [H : is_trunc n P] (P0 : A → P) (P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a') (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') ⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t') : square (ap02 (elim P0 P1 P2) (incl2 q)) (P2 q) (elim_inclt P2 t) (elim_inclt P2 t') := begin note Ht' := ap_ap_e_closure_elim tr (elim P0 P1 P2) (two_quotient.incl1 R Q) t', note Ht := ap_ap_e_closure_elim tr (elim P0 P1 P2) (two_quotient.incl1 R Q) t, note Hn := natural_square (ap_compose (elim P0 P1 P2) tr) (two_quotient.incl2 R Q q), note H7 := eq_top_of_square (Ht⁻¹ʰ ⬝h Hn⁻¹ᵛ ⬝h Ht'), clear [Hn, Ht, Ht'], unfold [ap02,incl2], rewrite [+ap_con,ap_inv,-ap_compose (ap _)], xrewrite [H7, ↑function.compose, eq_top_of_square (elim_incl2 P0 P1 P2 q)], clear [H7], have H : Π(t : T a a'), ap_e_closure_elim (elim P0 P1 P2) (λa a' (r : R a a'), ap tr (two_quotient.incl1 R Q r)) t ⬝ (ap_e_closure_elim_h (two_quotient.incl1 R Q) (λa a' (s : R a a'), ap_compose (elim P0 P1 P2) tr (two_quotient.incl1 R Q s)) t)⁻¹ ⬝ two_quotient.elim_inclt P2 t = elim_inclt P2 t, from ap_e_closure_elim_h_zigzag (elim P0 P1 P2) (two_quotient.incl1 R Q) (two_quotient.elim_incl1 P2), rewrite [con.assoc5, con.assoc5, H t, -inv_con_inv_right, -con_inv], xrewrite [H t'], apply top_deg_square end definition elim_inclt_rel [unfold_full] {P : Type} [is_trunc n P] {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') ⦃a a' : A⦄ (r : R a a') : elim_inclt P2 [r] = elim_incl1 P2 r := idp definition elim_inclt_inv [unfold_full] {P : Type} [is_trunc n P] {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') ⦃a a' : A⦄ (t : T a a') : elim_inclt P2 t⁻¹ʳ = ap_inv (elim P0 P1 P2) (inclt t) ⬝ (elim_inclt P2 t)⁻² := idp definition elim_inclt_con [unfold_full] {P : Type} [is_trunc n P] {P0 : A → P} {P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'} (P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t') ⦃a a' a'' : A⦄ (t : T a a') (t': T a' a'') : elim_inclt P2 (t ⬝r t') = ap_con (elim P0 P1 P2) (inclt t) (inclt t') ⬝ (elim_inclt P2 t ◾ elim_inclt P2 t') := idp definition inclt_rel [unfold_full] (r : R a a') : inclt [r] = incl1 r := idp definition inclt_inv [unfold_full] (t : T a a') : inclt t⁻¹ʳ = (inclt t)⁻¹ := idp definition inclt_con [unfold_full] (t : T a a') (t' : T a' a'') : inclt (t ⬝r t') = inclt t ⬝ inclt t' := idp end end trunc_two_quotient attribute trunc_two_quotient.incl0 [constructor] attribute trunc_two_quotient.rec trunc_two_quotient.elim [unfold 10] [recursor 10] attribute trunc_two_quotient.rec_on trunc_two_quotient.elim_on [unfold 7]
f27e337280f264bd74886f3f5da21fb83e312aa6
957a80ea22c5abb4f4670b250d55534d9db99108
/library/init/meta/rewrite_tactic.lean
00c6bb22032dec88ce5b8f3af9002c1b2bff5fe8
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
1,909
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.relation_tactics init.meta.occurrences namespace tactic /-- Configuration options for the `rewrite` tactic. -/ structure rewrite_cfg extends apply_cfg := (md := reducible) (symm := ff) (occs := occurrences.all) /-- Rewrite the expression `e` using `h`. The unification is performed using the transparency mode in `cfg`. If `cfg.approx` is `tt`, then fallback to first-order unification, and approximate context during unification. `cfg.new_goals` specifies which unassigned metavariables become new goals, and their order. If `cfg.instances` is `tt`, then use type class resolution to instantiate unassigned meta-variables. The fields `cfg.auto_param` and `cfg.opt_param` are ignored by this tactic (See `tactic.rewrite`). It a triple `(new_e, prf, metas)` where `prf : e = new_e`, and `metas` is a list of all introduced meta variables, even the assigned ones. TODO(Leo): improve documentation and explain symm/occs -/ meta constant rewrite_core (h : expr) (e : expr) (cfg : rewrite_cfg := {}) : tactic (expr × expr × list expr) meta def rewrite (h : expr) (e : expr) (cfg : rewrite_cfg := {}) : tactic (expr × expr × list expr) := do (new_t, prf, metas) ← rewrite_core h e cfg, try_apply_opt_auto_param cfg.to_apply_cfg metas, return (new_t, prf, metas) meta def rewrite_target (h : expr) (cfg : rewrite_cfg := {}) : tactic unit := do t ← target, (new_t, prf, _) ← rewrite h t cfg, replace_target new_t prf meta def rewrite_hyp (h : expr) (hyp : expr) (cfg : rewrite_cfg := {}) : tactic expr := do hyp_type ← infer_type hyp, (new_hyp_type, prf, _) ← rewrite h hyp_type cfg, replace_hyp hyp new_hyp_type prf end tactic
acc8d621339f6f4f436e1fb5e1c7b17dce92283d
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Init/Control/ExceptCps.lean
7adc504f9a2d82e0506b2bf6a6d220774c311a27
[ "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
3,030
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 -/ prelude import Init.Control.Lawful /-! The Exception monad transformer using CPS style. -/ def ExceptCpsT (ε : Type u) (m : Type u → Type v) (α : Type u) := (β : Type u) → (α → m β) → (ε → m β) → m β namespace ExceptCpsT @[always_inline, inline] def run {ε α : Type u} [Monad m] (x : ExceptCpsT ε m α) : m (Except ε α) := x _ (fun a => pure (Except.ok a)) (fun e => pure (Except.error e)) @[always_inline, inline] def runK {ε α : Type u} (x : ExceptCpsT ε m α) (s : ε) (ok : α → m β) (error : ε → m β) : m β := x _ ok error @[always_inline, inline] def runCatch [Monad m] (x : ExceptCpsT α m α) : m α := x α pure pure @[always_inline] instance : Monad (ExceptCpsT ε m) where map f x := fun _ k₁ k₂ => x _ (fun a => k₁ (f a)) k₂ pure a := fun _ k _ => k a bind x f := fun _ k₁ k₂ => x _ (fun a => f a _ k₁ k₂) k₂ instance : LawfulMonad (ExceptCpsT σ m) := by refine' { .. } <;> intros <;> rfl instance : MonadExceptOf ε (ExceptCpsT ε m) where throw e := fun _ _ k => k e tryCatch x handle := fun _ k₁ k₂ => x _ k₁ (fun e => handle e _ k₁ k₂) @[always_inline, inline] def lift [Monad m] (x : m α) : ExceptCpsT ε m α := fun _ k _ => x >>= k instance [Monad m] : MonadLift m (ExceptCpsT σ m) where monadLift := ExceptCpsT.lift instance [Inhabited ε] : Inhabited (ExceptCpsT ε m α) where default := fun _ _ k₂ => k₂ default @[simp] theorem run_pure [Monad m] : run (pure x : ExceptCpsT ε m α) = pure (Except.ok x) := rfl @[simp] theorem run_lift {α ε : Type u} [Monad m] (x : m α) : run (ExceptCpsT.lift x : ExceptCpsT ε m α) = (x >>= fun a => pure (Except.ok a) : m (Except ε α)) := rfl @[simp] theorem run_throw [Monad m] : run (throw e : ExceptCpsT ε m β) = pure (Except.error e) := rfl @[simp] theorem run_bind_lift [Monad m] (x : m α) (f : α → ExceptCpsT ε m β) : run (ExceptCpsT.lift x >>= f : ExceptCpsT ε m β) = x >>= fun a => run (f a) := rfl @[simp] theorem run_bind_throw [Monad m] (e : ε) (f : α → ExceptCpsT ε m β) : run (throw e >>= f : ExceptCpsT ε m β) = run (throw e) := rfl @[simp] theorem runCatch_pure [Monad m] : runCatch (pure x : ExceptCpsT α m α) = pure x := rfl @[simp] theorem runCatch_lift {α : Type u} [Monad m] [LawfulMonad m] (x : m α) : runCatch (ExceptCpsT.lift x : ExceptCpsT α m α) = x := by simp [runCatch, lift] @[simp] theorem runCatch_throw [Monad m] : runCatch (throw a : ExceptCpsT α m α) = pure a := rfl @[simp] theorem runCatch_bind_lift [Monad m] (x : m α) (f : α → ExceptCpsT β m β) : runCatch (ExceptCpsT.lift x >>= f : ExceptCpsT β m β) = x >>= fun a => runCatch (f a) := rfl @[simp] theorem runCatch_bind_throw [Monad m] (e : β) (f : α → ExceptCpsT β m β) : runCatch (throw e >>= f : ExceptCpsT β m β) = pure e := rfl end ExceptCpsT
7b91724324a1bfedb54b1a2290fa0d9ae2761c7e
a721fe7446524f18ba361625fc01033d9c8b7a78
/elaborate/add_comm.stripped.lean
f8d79a1b69b654140bb06fd21fa8862ca5e95260
[]
no_license
Sterrs/leaning
8fd80d1f0a6117a220bb2e57ece639b9a63deadc
3901cc953694b33adda86cb88ca30ba99594db31
refs/heads/master
1,627,023,822,744
1,616,515,221,000
1,616,515,221,000
245,512,190
2
0
null
1,616,429,050,000
1,583,527,118,000
Lean
UTF-8
Lean
false
false
2,029
lean
λ (m n : mynat), mynat.rec (eq.rec true.intro (eq.rec (eq.refl (m = add zero m)) (eq.rec (eq.rec (eq.refl (m = add zero m)) (eq.rec (eq.refl (add zero m)) (mynat.rec (eq.refl zero) (λ (m_n : mynat) (m_ih : add zero m_n = m_n), eq.rec m_ih (eq.rec (eq.refl (succ (add zero m_n) = succ m_n)) (eq.rec (eq.refl (succ (add zero m_n) = succ m_n)) (propext {mp := λ (h : succ (add zero m_n) = succ m_n), eq.rec (λ (h11 : succ (add zero m_n) = succ (add zero m_n)) (a : add zero m_n = add zero m_n → add zero m_n = m_n), a (eq.refl (add zero m_n))) h h (λ (n_eq : add zero m_n = m_n), n_eq), mpr := λ (a : add zero m_n = m_n), eq.rec (eq.refl (succ (add zero m_n))) a})))) m))) (propext {mp := λ (hl : m = m), true.intro, mpr := λ (hr : true), eq.refl m})))) (λ (n_n : mynat) (n_ih : add m n_n = add n_n m), eq.rec n_ih (eq.rec (eq.refl (succ (add m n_n) = add (succ n_n) m)) (eq.rec (eq.rec (eq.refl (succ (add m n_n) = add (succ n_n) m)) (mynat.rec (eq.refl (succ n_n)) (λ (n_n_1 : mynat) (n_ih : add (succ n_n) n_n_1 = succ (add n_n n_n_1)), eq.rec n_ih (eq.rec (eq.refl (succ (add (succ n_n) n_n_1) = succ (succ (add n_n n_n_1)))) (eq.rec (eq.refl (succ (add (succ n_n) n_n_1) = succ (succ (add n_n n_n_1)))) (propext {mp := λ (h : succ (add (succ n_n) n_n_1) = succ (succ (add n_n n_n_1))), eq.rec (λ (h11 : succ (add (succ n_n) n_n_1) = succ (add (succ n_n) n_n_1)) (a : add (succ n_n) n_n_1 = add (succ n_n) n_n_1 → add (succ n_n) n_n_1 = succ (add n_n n_n_1)), a (eq.refl (add (succ n_n) n_n_1))) h h (λ (n_eq : add (succ n_n) n_n_1 = succ (add n_n n_n_1)), n_eq), mpr := λ (a : add (succ n_n) n_n_1 = succ (add n_n n_n_1)), eq.rec (eq.refl (succ (add (succ n_n) n_n_1))) a})))) m)) (propext {mp := λ (h : succ (add m n_n) = succ (add n_n m)), eq.rec (λ (h11 : succ (add m n_n) = succ (add m n_n)) (a : add m n_n = add m n_n → add m n_n = add n_n m), a (eq.refl (add m n_n))) h h (λ (n_eq : add m n_n = add n_n m), n_eq), mpr := λ (a : add m n_n = add n_n m), eq.rec (eq.refl (succ (add m n_n))) a})))) n
e038c9eb6c8941ccc7c0dc2aef89ea674c38c7c7
4b846d8dabdc64e7ea03552bad8f7fa74763fc67
/library/init/meta/format.lean
edcfaea44b130e0e13962b5c29c9bafa847f4971
[ "Apache-2.0" ]
permissive
pacchiano/lean
9324b33f3ac3b5c5647285160f9f6ea8d0d767dc
fdadada3a970377a6df8afcd629a6f2eab6e84e8
refs/heads/master
1,611,357,380,399
1,489,870,101,000
1,489,870,101,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,577
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.options universes u v inductive format.color | red | green | orange | blue | pink | cyan | grey meta constant format : Type meta constant format.line : format meta constant format.space : format meta constant format.nil : format meta constant format.compose : format → format → format meta constant format.nest : nat → format → format meta constant format.highlight : format → color → format meta constant format.group : format → format meta constant format.of_string : string → format meta constant format.of_nat : nat → format meta constant format.flatten : format → format meta constant format.to_string : format → options → string meta constant format.of_options : options → format meta constant format.is_nil : format → bool meta constant trace_fmt {α : Type u} : format → (unit → α) → α meta instance : inhabited format := ⟨format.space⟩ meta instance : has_append format := ⟨format.compose⟩ meta instance : has_to_string format := ⟨λ f, format.to_string f options.mk⟩ meta class has_to_format (α : Type u) := (to_format : α → format) meta instance : has_to_format format := ⟨id⟩ meta def to_fmt {α : Type u} [has_to_format α] : α → format := has_to_format.to_format meta instance nat_to_format : has_coe nat format := ⟨format.of_nat⟩ meta instance string_to_format : has_coe string format := ⟨format.of_string⟩ open format list meta def format.indent (f : format) (n : nat) : format := nest n (line ++ f) meta def format.when {α : Type u} [has_to_format α] : bool → α → format | tt a := to_fmt a | ff a := nil meta def format.join (xs : list format) : format := foldl compose (of_string "") xs meta instance : has_to_format options := ⟨λ o, format.of_options o⟩ meta instance : has_to_format bool := ⟨λ b, if b then of_string "tt" else of_string "ff"⟩ meta instance {p : Prop} : has_to_format (decidable p) := ⟨λ b : decidable p, @ite p b _ (of_string "tt") (of_string "ff")⟩ meta instance : has_to_format string := ⟨λ s, format.of_string s⟩ meta instance : has_to_format nat := ⟨λ n, format.of_nat n⟩ meta instance : has_to_format char := ⟨λ c : char, format.of_string [c]⟩ meta def list.to_format {α : Type u} [has_to_format α] : list α → format | [] := to_fmt "[]" | xs := to_fmt "[" ++ group (nest 1 $ format.join $ list.intersperse ("," ++ line) $ xs^.map to_fmt) ++ to_fmt "]" meta instance {α : Type u} [has_to_format α] : has_to_format (list α) := ⟨list.to_format⟩ attribute [instance] string.has_to_format meta instance : has_to_format name := ⟨λ n, to_fmt (to_string n)⟩ meta instance : has_to_format unit := ⟨λ u, to_fmt "()"⟩ meta instance {α : Type u} [has_to_format α] : has_to_format (option α) := ⟨λ o, option.cases_on o (to_fmt "none") (λ a, to_fmt "(some " ++ nest 6 (to_fmt a) ++ to_fmt ")")⟩ meta instance sum_has_to_format {α : Type u} {β : Type v} [has_to_format α] [has_to_format β] : has_to_format (sum α β) := ⟨λ s, sum.cases_on s (λ a, to_fmt "(inl " ++ nest 5 (to_fmt a) ++ to_fmt ")") (λ b, to_fmt "(inr " ++ nest 5 (to_fmt b) ++ to_fmt ")")⟩ open prod meta instance {α : Type u} {β : Type v} [has_to_format α] [has_to_format β] : has_to_format (prod α β) := ⟨λ ⟨a, b⟩, group (nest 1 (to_fmt "(" ++ to_fmt a ++ to_fmt "," ++ line ++ to_fmt b ++ to_fmt ")"))⟩ open sigma meta instance {α : Type u} {β : α → Type v} [has_to_format α] [s : ∀ x, has_to_format (β x)] : has_to_format (sigma β) := ⟨λ ⟨a, b⟩, group (nest 1 (to_fmt "⟨" ++ to_fmt a ++ to_fmt "," ++ line ++ to_fmt b ++ to_fmt "⟩"))⟩ open subtype meta instance {α : Type u} {p : α → Prop} [has_to_format α] : has_to_format (subtype p) := ⟨λ s, to_fmt (val s)⟩ meta def format.bracket : string → string → format → format | o c f := to_fmt o ++ nest (utf8_length o) f ++ to_fmt c meta def format.paren (f : format) : format := format.bracket "(" ")" f meta def format.cbrace (f : format) : format := format.bracket "{" "}" f meta def format.sbracket (f : format) : format := format.bracket "[" "]" f meta def format.dcbrace (f : format) : format := to_fmt "⦃" ++ nest 1 f ++ to_fmt "⦄"
a226625f2e4cc947388ed7a03f4a4ee32f051ab4
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/logic/basic.lean
11a8fec89c18436723e9594eb7e8910505f4df05
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
57,850
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 -/ import tactic.doc_commands import tactic.reserved_notation /-! # Basic logic properties This file is one of the earliest imports in mathlib. ## Implementation notes Theorems that require decidability hypotheses are in the namespace "decidable". Classical versions are in the namespace "classical". In the presence of automation, this whole file may be unnecessary. On the other hand, maybe it is useful for writing automation. -/ local attribute [instance, priority 10] classical.prop_decidable section miscellany /- We add the `inline` attribute to optimize VM computation using these declarations. For example, `if p ∧ q then ... else ...` will not evaluate the decidability of `q` if `p` is false. -/ attribute [inline] and.decidable or.decidable decidable.false xor.decidable iff.decidable decidable.true implies.decidable not.decidable ne.decidable bool.decidable_eq decidable.to_bool attribute [simp] cast_eq cast_heq variables {α : Type*} {β : Type*} /-- An identity function with its main argument implicit. This will be printed as `hidden` even if it is applied to a large term, so it can be used for elision, as done in the `elide` and `unelide` tactics. -/ @[reducible] def hidden {α : Sort*} {a : α} := a /-- Ex falso, the nondependent eliminator for the `empty` type. -/ def empty.elim {C : Sort*} : empty → C. instance : subsingleton empty := ⟨λa, a.elim⟩ instance subsingleton.prod {α β : Type*} [subsingleton α] [subsingleton β] : subsingleton (α × β) := ⟨by { intros a b, cases a, cases b, congr, }⟩ instance : decidable_eq empty := λa, a.elim instance sort.inhabited : inhabited (Sort*) := ⟨punit⟩ instance sort.inhabited' : inhabited (default (Sort*)) := ⟨punit.star⟩ instance psum.inhabited_left {α β} [inhabited α] : inhabited (psum α β) := ⟨psum.inl (default _)⟩ instance psum.inhabited_right {α β} [inhabited β] : inhabited (psum α β) := ⟨psum.inr (default _)⟩ @[priority 10] instance decidable_eq_of_subsingleton {α} [subsingleton α] : decidable_eq α | a b := is_true (subsingleton.elim a b) @[simp] lemma eq_iff_true_of_subsingleton {α : Sort*} [subsingleton α] (x y : α) : x = y ↔ true := by cc /-- If all points are equal to a given point `x`, then `α` is a subsingleton. -/ lemma subsingleton_of_forall_eq {α : Sort*} (x : α) (h : ∀ y, y = x) : subsingleton α := ⟨λ a b, (h a).symm ▸ (h b).symm ▸ rfl⟩ lemma subsingleton_iff_forall_eq {α : Sort*} (x : α) : subsingleton α ↔ ∀ y, y = x := ⟨λ h y, @subsingleton.elim _ h y x, subsingleton_of_forall_eq x⟩ /-- 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 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 theorem coe_sort_coe_trans {α β γ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_sort γ] (x : α) : @coe_sort α _ x = @coe_sort β _ x := rfl /-- Many structures such as bundled morphisms coerce to functions so that you can transparently apply them to arguments. For example, if `e : α ≃ β` and `a : α` then you can write `e a` and this is elaborated as `⇑e a`. This type of coercion is implemented using the `has_coe_to_fun` type class. There is one important consideration: If a type coerces to another type which in turn coerces to a function, then it **must** implement `has_coe_to_fun` directly: ```lean structure sparkling_equiv (α β) extends α ≃ β -- if we add a `has_coe` instance, instance {α β} : has_coe (sparkling_equiv α β) (α ≃ β) := ⟨sparkling_equiv.to_equiv⟩ -- then a `has_coe_to_fun` instance **must** be added as well: instance {α β} : has_coe_to_fun (sparkling_equiv α β) := ⟨λ _, α → β, λ f, f.to_equiv.to_fun⟩ ``` (Rationale: if we do not declare the direct coercion, then `⇑e a` is not in simp-normal form. The lemma `coe_fn_coe_base` will unfold it to `⇑↑e a`. This often causes loops in the simplifier.) -/ library_note "function coercion" @[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 /-- Ex falso, the nondependent eliminator for the `pempty` type. -/ def pempty.elim {C : Sort*} : pempty → C. instance subsingleton_pempty : subsingleton pempty := ⟨λa, a.elim⟩ @[simp] lemma not_nonempty_pempty : ¬ nonempty pempty := assume ⟨h⟩, h.elim @[simp] theorem forall_pempty {P : pempty → Prop} : (∀ x : pempty, P x) ↔ true := ⟨λ h, trivial, λ h x, by cases x⟩ @[simp] theorem exists_pempty {P : pempty → Prop} : (∃ x : pempty, P x) ↔ false := ⟨λ h, by { cases h with w, cases w }, false.elim⟩ lemma congr_arg_heq {α} {β : α → Sort*} (f : ∀ a, β a) : ∀ {a₁ a₂ : α}, a₁ = a₂ → f a₁ == f a₂ | a _ rfl := heq.rfl lemma plift.down_inj {α : Sort*} : ∀ (a b : plift α), a.down = b.down → a = b | ⟨a⟩ ⟨b⟩ rfl := rfl -- missing [symm] attribute for ne in core. attribute [symm] ne.symm lemma ne_comm {α} {a b : α} : a ≠ b ↔ b ≠ a := ⟨ne.symm, ne.symm⟩ @[simp] lemma eq_iff_eq_cancel_left {b c : α} : (∀ {a}, a = b ↔ a = c) ↔ (b = c) := ⟨λ h, by rw [← h], λ h a, by rw h⟩ @[simp] lemma eq_iff_eq_cancel_right {a b : α} : (∀ {c}, a = c ↔ b = c) ↔ (a = b) := ⟨λ h, by rw h, λ h a, by rw h⟩ /-- Wrapper for adding elementary propositions to the type class systems. Warning: this can easily be abused. See the rest of this docstring for details. Certain propositions should not be treated as a class globally, but sometimes it is very convenient to be able to use the type class system in specific circumstances. For example, `zmod p` is a field if and only if `p` is a prime number. In order to be able to find this field instance automatically by type class search, we have to turn `p.prime` into an instance implicit assumption. On the other hand, making `nat.prime` a class would require a major refactoring of the library, and it is questionable whether making `nat.prime` a class is desirable at all. The compromise is to add the assumption `[fact p.prime]` to `zmod.field`. In particular, this class is not intended for turning the type class system into an automated theorem prover for first order logic. -/ class fact (p : Prop) : Prop := (out [] : p) lemma fact.elim {p : Prop} (h : fact p) : p := h.1 lemma fact_iff {p : Prop} : fact p ↔ p := ⟨λ h, h.1, λ h, ⟨h⟩⟩ end miscellany /-! ### Declarations about propositional connectives -/ theorem false_ne_true : false ≠ true | h := h.symm ▸ trivial section propositional variables {a b c d : Prop} /-! ### Declarations about `implies` -/ instance : is_refl Prop iff := ⟨iff.refl⟩ instance : is_trans Prop iff := ⟨λ _ _ _, iff.trans⟩ 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] lemma eq_iff_iff {p q : Prop} : (p = q) ↔ (p ↔ q) := iff_iff_eq.symm @[simp] theorem imp_self : (a → a) ↔ true := iff_true_intro id theorem imp_intro {α β : Prop} (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 theorem imp_true_iff {α : Sort*} : (α → true) ↔ true := iff_true_intro $ λ_, trivial theorem imp_iff_right (ha : a) : (a → b) ↔ b := ⟨λf, f ha, imp_intro⟩ /-! ### Declarations about `not` -/ /-- Ex falso for negation. From `¬ a` and `a` anything follows. This is the same as `absurd` with the arguments flipped, but it is in the `not` namespace so that projection notation can be used. -/ def 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 {a : Prop} : ¬(a → b) → ¬b := mt imp_intro theorem dec_em (p : Prop) [decidable p] : p ∨ ¬p := decidable.em p theorem dec_em' (p : Prop) [decidable p] : ¬p ∨ p := (dec_em p).swap theorem em (p : Prop) : p ∨ ¬p := classical.em _ theorem em' (p : Prop) : ¬p ∨ p := (em p).swap theorem or_not {p : Prop} : p ∨ ¬p := em _ theorem decidable.eq_or_ne [decidable (a = b)] : a = b ∨ a ≠ b := dec_em $ a = b theorem decidable.ne_or_eq [decidable (a = b)] : a ≠ b ∨ a = b := dec_em' $ a = b theorem eq_or_ne : a = b ∨ a ≠ b := em $ a = b theorem ne_or_eq : a ≠ b ∨ a = b := em' $ a = b theorem by_contradiction {p} : (¬p → false) → p := decidable.by_contradiction -- alias by_contradiction ← by_contra theorem by_contra {p} : (¬p → false) → p := decidable.by_contradiction /-- In most of mathlib, we use the law of excluded middle (LEM) and the axiom of choice (AC) freely. The `decidable` namespace contains versions of lemmas from the root namespace that explicitly attempt to avoid the axiom of choice, usually by adding decidability assumptions on the inputs. You can check if a lemma uses the axiom of choice by using `#print axioms foo` and seeing if `classical.choice` appears in the list. -/ library_note "decidable namespace" /-- As mathlib is primarily classical, if the type signature of a `def` or `lemma` does not require any `decidable` instances to state, it is preferable not to introduce any `decidable` instances that are needed in the proof as arguments, but rather to use the `classical` tactic as needed. In the other direction, when `decidable` instances do appear in the type signature, it is better to use explicitly introduced ones rather than allowing Lean to automatically infer classical ones, as these may cause instance mismatch errors later. -/ library_note "decidable arguments" -- See Note [decidable namespace] protected theorem decidable.not_not [decidable a] : ¬¬a ↔ a := iff.intro decidable.by_contradiction not_not_intro /-- The Double Negation Theorem: `¬ ¬ P` is equivalent to `P`. The left-to-right direction, double negation elimination (DNE), is classically true but not constructively. -/ @[simp] theorem not_not : ¬¬a ↔ a := decidable.not_not theorem of_not_not : ¬¬a → a := by_contra -- See Note [decidable namespace] protected theorem decidable.of_not_imp [decidable a] (h : ¬ (a → b)) : a := decidable.by_contradiction (not_not_of_not_imp h) theorem of_not_imp : ¬ (a → b) → a := decidable.of_not_imp -- See Note [decidable namespace] protected theorem decidable.not_imp_symm [decidable a] (h : ¬a → b) (hb : ¬b) : a := decidable.by_contradiction $ hb ∘ h theorem not.decidable_imp_symm [decidable a] : (¬a → b) → ¬b → a := decidable.not_imp_symm theorem not.imp_symm : (¬a → b) → ¬b → a := not.decidable_imp_symm -- See Note [decidable namespace] protected theorem decidable.not_imp_comm [decidable a] [decidable b] : (¬a → b) ↔ (¬b → a) := ⟨not.decidable_imp_symm, not.decidable_imp_symm⟩ theorem not_imp_comm : (¬a → b) ↔ (¬b → a) := decidable.not_imp_comm @[simp] theorem imp_not_self : (a → ¬a) ↔ ¬a := ⟨λ h ha, h ha ha, λ h _, h⟩ theorem decidable.not_imp_self [decidable a] : (¬a → a) ↔ a := by { have := @imp_not_self (¬a), rwa decidable.not_not at this } @[simp] theorem not_imp_self : (¬a → a) ↔ a := decidable.not_imp_self theorem imp.swap : (a → b → c) ↔ (b → a → c) := ⟨function.swap, function.swap⟩ theorem imp_not_comm : (a → ¬b) ↔ (b → ¬a) := imp.swap /-! ### Declarations about `and` -/ theorem and_congr_left (h : c → (a ↔ b)) : a ∧ c ↔ b ∧ c := and.comm.trans $ (and_congr_right h).trans and.comm theorem and_congr_left' (h : a ↔ b) : a ∧ c ↔ b ∧ c := and_congr h iff.rfl theorem and_congr_right' (h : b ↔ c) : a ∧ b ↔ a ∧ c := and_congr iff.rfl h 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 only [and.left_comm, and.comm] lemma and.rotate : a ∧ b ∧ c ↔ b ∧ c ∧ a := by simp only [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⟩) @[simp] theorem and_iff_left_iff_imp {a b : Prop} : ((a ∧ b) ↔ a) ↔ (a → b) := ⟨λ h ha, (h.2 ha).2, and_iff_left_of_imp⟩ @[simp] theorem and_iff_right_iff_imp {a b : Prop} : ((a ∧ b) ↔ b) ↔ (b → a) := ⟨λ h ha, (h.2 ha).1, and_iff_right_of_imp⟩ @[simp] lemma and.congr_right_iff : (a ∧ b ↔ a ∧ c) ↔ (a → (b ↔ c)) := ⟨λ h ha, by simp [ha] at h; exact h, and_congr_right⟩ @[simp] lemma and.congr_left_iff : (a ∧ c ↔ b ∧ c) ↔ c → (a ↔ b) := by simp only [and.comm, ← and.congr_right_iff] @[simp] lemma and_self_left : a ∧ a ∧ b ↔ a ∧ b := ⟨λ h, ⟨h.1, h.2.2⟩, λ h, ⟨h.1, h.1, h.2⟩⟩ @[simp] lemma and_self_right : (a ∧ b) ∧ b ↔ a ∧ b := ⟨λ h, ⟨h.1.1, h.2⟩, λ h, ⟨⟨h.1, h.2⟩, h.2⟩⟩ /-! ### Declarations about `or` -/ theorem or_congr_left (h : a ↔ b) : a ∨ c ↔ b ∨ c := or_congr h iff.rfl theorem or_congr_right (h : b ↔ c) : a ∨ b ↔ a ∨ c := or_congr iff.rfl h theorem or.right_comm : (a ∨ b) ∨ c ↔ (a ∨ c) ∨ b := by rw [or_assoc, or_assoc, or_comm b] 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⟩ -- See Note [decidable namespace] protected theorem decidable.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_left : a ∨ b ↔ (¬ a → b) := decidable.or_iff_not_imp_left -- See Note [decidable namespace] protected theorem decidable.or_iff_not_imp_right [decidable b] : a ∨ b ↔ (¬ b → a) := or.comm.trans decidable.or_iff_not_imp_left theorem or_iff_not_imp_right : a ∨ b ↔ (¬ b → a) := decidable.or_iff_not_imp_right -- See Note [decidable namespace] protected theorem decidable.not_imp_not [decidable a] : (¬ a → ¬ b) ↔ (b → a) := ⟨assume h hb, decidable.by_contradiction $ assume na, h na hb, mt⟩ theorem not_imp_not : (¬ a → ¬ b) ↔ (b → a) := decidable.not_imp_not @[simp] theorem or_iff_left_iff_imp : (a ∨ b ↔ a) ↔ (b → a) := ⟨λ h hb, h.1 (or.inr hb), or_iff_left_of_imp⟩ @[simp] theorem or_iff_right_iff_imp : (a ∨ b ↔ b) ↔ (a → b) := by rw [or_comm, or_iff_left_iff_imp] /-! ### Declarations about distributivity -/ /-- `∧` distributes over `∨` (on the left). -/ 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)⟩ /-- `∧` distributes over `∨` (on the right). -/ 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) /-- `∨` distributes over `∧` (on the left). -/ 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)⟩ /-- `∨` distributes over `∧` (on the right). -/ 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) @[simp] lemma or_self_left : a ∨ a ∨ b ↔ a ∨ b := ⟨λ h, h.elim or.inl id, λ h, h.elim or.inl (or.inr ∘ or.inr)⟩ @[simp] lemma or_self_right : (a ∨ b) ∨ b ↔ a ∨ b := ⟨λ h, h.elim id or.inr, λ h, h.elim (or.inl ∘ or.inl) or.inr⟩ /-! Declarations about `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) @[simp] lemma iff_mpr_iff_true_intro {P : Prop} (h : P) : iff.mpr (iff_true_intro h) true.intro = h := rfl -- See Note [decidable namespace] protected theorem decidable.not_or_of_imp [decidable a] (h : a → b) : ¬ a ∨ b := if ha : a then or.inr (h ha) else or.inl ha theorem not_or_of_imp : (a → b) → ¬ a ∨ b := decidable.not_or_of_imp -- See Note [decidable namespace] protected theorem decidable.imp_iff_not_or [decidable a] : (a → b) ↔ (¬ a ∨ b) := ⟨decidable.not_or_of_imp, or.neg_resolve_left⟩ theorem imp_iff_not_or : (a → b) ↔ (¬ a ∨ b) := decidable.imp_iff_not_or -- See Note [decidable namespace] protected theorem decidable.imp_or_distrib [decidable a] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by simp [decidable.imp_iff_not_or, or.comm, or.left_comm] theorem imp_or_distrib : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := decidable.imp_or_distrib -- See Note [decidable namespace] protected theorem decidable.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 imp_or_distrib' : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := decidable.imp_or_distrib' theorem not_imp_of_and_not : a ∧ ¬ b → ¬ (a → b) | ⟨ha, hb⟩ h := hb $ h ha -- See Note [decidable namespace] protected theorem decidable.not_imp [decidable a] : ¬(a → b) ↔ a ∧ ¬b := ⟨λ h, ⟨decidable.of_not_imp h, not_of_not_imp h⟩, not_imp_of_and_not⟩ theorem not_imp : ¬(a → b) ↔ a ∧ ¬b := decidable.not_imp -- for monotonicity lemma imp_imp_imp (h₀ : c → a) (h₁ : b → d) : (a → b) → (c → d) := assume (h₂ : a → b), h₁ ∘ h₂ ∘ h₀ -- See Note [decidable namespace] protected theorem decidable.peirce (a b : Prop) [decidable a] : ((a → b) → a) → a := if ha : a then λ h, ha else λ h, h ha.elim theorem peirce (a b : Prop) : ((a → b) → a) → a := decidable.peirce _ _ theorem peirce' {a : Prop} (H : ∀ b : Prop, (a → b) → a) : a := H _ id -- See Note [decidable namespace] protected theorem decidable.not_iff_not [decidable a] [decidable b] : (¬ a ↔ ¬ b) ↔ (a ↔ b) := by rw [@iff_def (¬ a), @iff_def' a]; exact and_congr decidable.not_imp_not decidable.not_imp_not theorem not_iff_not : (¬ a ↔ ¬ b) ↔ (a ↔ b) := decidable.not_iff_not -- See Note [decidable namespace] protected theorem decidable.not_iff_comm [decidable a] [decidable b] : (¬ a ↔ b) ↔ (¬ b ↔ a) := by rw [@iff_def (¬ a), @iff_def (¬ b)]; exact and_congr decidable.not_imp_comm imp_not_comm theorem not_iff_comm : (¬ a ↔ b) ↔ (¬ b ↔ a) := decidable.not_iff_comm -- See Note [decidable namespace] protected theorem decidable.not_iff : ∀ [decidable b], ¬ (a ↔ b) ↔ (¬ a ↔ b) := by intro h; cases h; simp only [h, iff_true, iff_false] theorem not_iff : ¬ (a ↔ b) ↔ (¬ a ↔ b) := decidable.not_iff -- See Note [decidable namespace] protected theorem decidable.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 decidable.not_imp_comm theorem iff_not_comm : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := decidable.iff_not_comm -- See Note [decidable namespace] protected theorem decidable.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 } } } theorem iff_iff_and_or_not_and_not : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) := decidable.iff_iff_and_or_not_and_not lemma decidable.iff_iff_not_or_and_or_not [decidable a] [decidable b] : (a ↔ b) ↔ ((¬a ∨ b) ∧ (a ∨ ¬b)) := begin rw [iff_iff_implies_and_implies a b], simp only [decidable.imp_iff_not_or, or.comm] end lemma iff_iff_not_or_and_or_not : (a ↔ b) ↔ ((¬a ∨ b) ∧ (a ∨ ¬b)) := decidable.iff_iff_not_or_and_or_not -- See Note [decidable namespace] protected theorem decidable.not_and_not_right [decidable b] : ¬(a ∧ ¬b) ↔ (a → b) := ⟨λ h ha, h.decidable_imp_symm $ and.intro ha, λ h ⟨ha, hb⟩, hb $ h ha⟩ theorem not_and_not_right : ¬(a ∧ ¬b) ↔ (a → b) := decidable.not_and_not_right /-- Transfer decidability of `a` to decidability of `b`, if the propositions are equivalent. **Important**: this function should be used instead of `rw` on `decidable b`, because the kernel will get stuck reducing the usage of `propext` otherwise, and `dec_trivial` will not work. -/ @[inline] def decidable_of_iff (a : Prop) (h : a ↔ b) [D : decidable a] : decidable b := decidable_of_decidable_of_iff D h /-- Transfer decidability of `b` to decidability of `a`, if the propositions are equivalent. This is the same as `decidable_of_iff` but the iff is flipped. -/ @[inline] def decidable_of_iff' (b : Prop) (h : a ↔ b) [D : decidable b] : decidable a := decidable_of_decidable_of_iff D h.symm /-- Prove that `a` is decidable by constructing a boolean `b` and a proof that `b ↔ a`. (This is sometimes taken as an alternate definition of decidability.) -/ 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) -- See Note [decidable namespace] protected theorem decidable.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⟩ -- See Note [decidable namespace] protected theorem decidable.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⟩ /-- One of de Morgan's laws: the negation of a conjunction is logically equivalent to the disjunction of the negations. -/ theorem not_and_distrib : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := decidable.not_and_distrib @[simp] theorem not_and : ¬ (a ∧ b) ↔ (a → ¬ b) := and_imp theorem not_and' : ¬ (a ∧ b) ↔ b → ¬a := not_and.trans imp_not_comm /-- One of de Morgan's laws: the negation of a disjunction is logically equivalent to the conjunction of the negations. -/ 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₂⟩ -- See Note [decidable namespace] protected theorem decidable.or_iff_not_and_not [decidable a] [decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) := by rw [← not_or_distrib, decidable.not_not] theorem or_iff_not_and_not : a ∨ b ↔ ¬ (¬a ∧ ¬b) := decidable.or_iff_not_and_not -- See Note [decidable namespace] protected theorem decidable.and_iff_not_or_not [decidable a] [decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := by rw [← decidable.not_and_distrib, decidable.not_not] theorem and_iff_not_or_not : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := decidable.and_iff_not_or_not end propositional /-! ### Declarations about 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 lemma ne_of_apply_ne {α β : Sort*} (f : α → β) {x y : α} (h : f x ≠ f y) : x ≠ y := λ (w : x = y), h (congr_arg f w) theorem eq_equivalence : equivalence (@eq α) := ⟨eq.refl, @eq.symm _, @eq.trans _⟩ /-- Transport through trivial families is the identity. -/ @[simp] lemma eq_rec_constant {α : Sort*} {a a' : α} {β : Sort*} (y : β) (h : a = a') : (@eq.rec α a (λ a, β) y a' h) = y := by { cases h, refl, } @[simp] lemma eq_mp_eq_cast {α β : Sort*} (h : α = β) : eq.mp h = cast h := rfl @[simp] lemma eq_mpr_eq_cast {α β : Sort*} (h : α = β) : eq.mpr h = cast h.symm := rfl @[simp] lemma cast_cast : ∀ {α β γ : Sort*} (ha : α = β) (hb : β = γ) (a : α), cast hb (cast ha a) = cast (ha.trans hb) a | _ _ _ rfl rfl a := rfl @[simp] lemma congr_refl_left {α β : Sort*} (f : α → β) {a b : α} (h : a = b) : congr (eq.refl f) h = congr_arg f h := rfl @[simp] lemma congr_refl_right {α β : Sort*} {f g : α → β} (h : f = g) (a : α) : congr h (eq.refl a) = congr_fun h a := rfl @[simp] lemma congr_arg_refl {α β : Sort*} (f : α → β) (a : α) : congr_arg f (eq.refl a) = eq.refl (f a) := rfl @[simp] lemma congr_fun_rfl {α β : Sort*} (f : α → β) (a : α) : congr_fun (eq.refl f) a = eq.refl (f a) := rfl @[simp] lemma congr_fun_congr_arg {α β γ : Sort*} (f : α → β → γ) {a a' : α} (p : a = a') (b : β) : congr_fun (congr_arg f p) b = congr_arg (λ a, f a b) p := rfl lemma heq_of_cast_eq : ∀ {α β : Sort*} {a : α} {a' : β} (e : α = β) (h₂ : cast e a = a'), a == a' | α ._ a a' rfl h := eq.rec_on h (heq.refl _) lemma cast_eq_iff_heq {α β : Sort*} {a : α} {a' : β} {e : α = β} : cast e a = a' ↔ a == a' := ⟨heq_of_cast_eq _, λ h, by cases h; 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 protected lemma eq.congr {x₁ x₂ y₁ y₂ : α} (h₁ : x₁ = y₁) (h₂ : x₂ = y₂) : (x₁ = x₂) ↔ (y₁ = y₂) := by { subst h₁, subst h₂ } lemma eq.congr_left {x y z : α} (h : x = y) : x = z ↔ y = z := by rw [h] lemma eq.congr_right {x y z : α} (h : x = y) : z = x ↔ z = y := by rw [h] lemma congr_arg2 {α β γ : Type*} (f : α → β → γ) {x x' : α} {y y' : β} (hx : x = x') (hy : y = y') : f x y = f x' y' := by { subst hx, subst hy } end equality /-! ### Declarations about quantifiers -/ section quantifiers variables {α : Sort*} {β : Sort*} {p q : α → Prop} {b : Prop} lemma forall_imp (h : ∀ a, p a → q a) : (∀ a, p a) → ∀ a, q a := λ h' a, h a (h' a) lemma forall₂_congr {p q : α → β → Prop} (h : ∀ a b, p a b ↔ q a b) : (∀ a b, p a b) ↔ (∀ a b, q a b) := forall_congr (λ a, forall_congr (h a)) lemma forall₃_congr {γ : Sort*} {p q : α → β → γ → Prop} (h : ∀ a b c, p a b c ↔ q a b c) : (∀ a b c, p a b c) ↔ (∀ a b c, q a b c) := forall_congr (λ a, forall₂_congr (h a)) lemma forall₄_congr {γ δ : Sort*} {p q : α → β → γ → δ → Prop} (h : ∀ a b c d, p a b c d ↔ q a b c d) : (∀ a b c d, p a b c d) ↔ (∀ a b c d, q a b c d) := forall_congr (λ a, forall₃_congr (h a)) lemma Exists.imp (h : ∀ a, (p a → q a)) (p : ∃ a, p a) : ∃ a, q a := exists_imp_exists h p lemma exists_imp_exists' {p : α → Prop} {q : β → Prop} (f : α → β) (hpq : ∀ a, p a → q (f a)) (hp : ∃ a, p a) : ∃ b, q b := exists.elim hp (λ a hp', ⟨_, hpq _ hp'⟩) lemma exists₂_congr {p q : α → β → Prop} (h : ∀ a b, p a b ↔ q a b) : (∃ a b, p a b) ↔ (∃ a b, q a b) := exists_congr (λ a, exists_congr (h a)) lemma exists₃_congr {γ : Sort*} {p q : α → β → γ → Prop} (h : ∀ a b c, p a b c ↔ q a b c) : (∃ a b c, p a b c) ↔ (∃ a b c, q a b c) := exists_congr (λ a, exists₂_congr (h a)) lemma exists₄_congr {γ δ : Sort*} {p q : α → β → γ → δ → Prop} (h : ∀ a b c d, p a b c d ↔ q a b c d) : (∃ a b c d, p a b c d) ↔ (∃ a b c d, q a b c d) := exists_congr (λ a, exists₃_congr (h a)) 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⟩ /-- Extract an element from a existential statement, using `classical.some`. -/ -- This enables projection notation. @[reducible] noncomputable def Exists.some {p : α → Prop} (P : ∃ a, p a) : α := classical.some P /-- Show that an element extracted from `P : ∃ a, p a` using `P.some` satisfies `p`. -/ lemma Exists.some_spec {p : α → Prop} (P : ∃ a, p a) : p (P.some) := classical.some_spec P --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) -- See Note [decidable namespace] protected theorem decidable.not_forall {p : α → Prop} [decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := ⟨not.decidable_imp_symm $ λ nx x, nx.decidable_imp_symm $ λ h, ⟨x, h⟩, not_forall_of_exists_not⟩ @[simp] theorem not_forall {p : α → Prop} : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := decidable.not_forall -- See Note [decidable namespace] protected theorem decidable.not_forall_not [decidable (∃ x, p x)] : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := (@decidable.not_iff_comm _ _ _ (decidable_of_iff (¬ ∃ x, p x) not_exists)).1 not_exists theorem not_forall_not : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := decidable.not_forall_not -- See Note [decidable namespace] protected theorem decidable.not_exists_not [∀ x, decidable (p x)] : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := by simp [decidable.not_not] @[simp] theorem not_exists_not : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := decidable.not_exists_not -- TODO: duplicate of a lemma in core theorem forall_true_iff : (α → true) ↔ true := implies_true_iff α -- 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*) [i : nonempty α] : (α → b) ↔ b := ⟨i.elim, λ hb x, hb⟩ @[simp] theorem exists_const (α : Sort*) [i : nonempty α] : (∃ x : α, b) ↔ b := ⟨λ ⟨x, h⟩, h, i.elim exists.intro⟩ 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 forall_eq' {a' : α} : (∀a, a' = a → p a) ↔ p a' := by simp [@eq_comm _ a'] -- this lemma is needed to simplify the output of `list.mem_cons_iff` @[simp] theorem forall_eq_or_imp {a' : α} : (∀ a, a = a' ∨ q a → p a) ↔ p a' ∧ ∀ a, q a → p a := by simp only [or_imp_distrib, forall_and_distrib, forall_eq] @[simp] theorem exists_eq {a' : α} : ∃ a, a = a' := ⟨_, rfl⟩ @[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 exists_eq_right_right {a' : α} : (∃ (a : α), p a ∧ b ∧ a = a') ↔ p a' ∧ b := ⟨λ ⟨_, hp, hq, rfl⟩, ⟨hp, hq⟩, λ ⟨hp, hq⟩, ⟨a', hp, hq, rfl⟩⟩ @[simp] theorem exists_eq_right_right' {a' : α} : (∃ (a : α), p a ∧ b ∧ a' = a) ↔ p a' ∧ b := ⟨λ ⟨_, hp, hq, rfl⟩, ⟨hp, hq⟩, λ ⟨hp, hq⟩, ⟨a', hp, hq, rfl⟩⟩ @[simp] theorem exists_apply_eq_apply {α β : Type*} (f : α → β) (a' : α) : ∃ a, f a = f a' := ⟨a', rfl⟩ @[simp] theorem exists_apply_eq_apply' {α β : Type*} (f : α → β) (a' : α) : ∃ a, f a' = f a := ⟨a', rfl⟩ @[simp] theorem exists_exists_and_eq_and {f : α → β} {p : α → Prop} {q : β → Prop} : (∃ b, (∃ a, p a ∧ f a = b) ∧ q b) ↔ ∃ a, p a ∧ q (f a) := ⟨λ ⟨b, ⟨a, ha, hab⟩, hb⟩, ⟨a, ha, hab.symm ▸ hb⟩, λ ⟨a, hp, hq⟩, ⟨f a, ⟨a, hp, rfl⟩, hq⟩⟩ @[simp] theorem exists_exists_eq_and {f : α → β} {p : β → Prop} : (∃ b, (∃ a, f a = b) ∧ p b) ↔ ∃ a, p (f a) := ⟨λ ⟨b, ⟨a, ha⟩, hb⟩, ⟨a, ha.symm ▸ hb⟩, λ ⟨a, ha⟩, ⟨f a, ⟨a, rfl⟩, ha⟩⟩ @[simp] lemma exists_or_eq_left (y : α) (p : α → Prop) : ∃ (x : α), x = y ∨ p x := ⟨y, or.inl rfl⟩ @[simp] lemma exists_or_eq_right (y : α) (p : α → Prop) : ∃ (x : α), p x ∨ x = y := ⟨y, or.inr rfl⟩ @[simp] lemma exists_or_eq_left' (y : α) (p : α → Prop) : ∃ (x : α), y = x ∨ p x := ⟨y, or.inl rfl⟩ @[simp] lemma exists_or_eq_right' (y : α) (p : α → Prop) : ∃ (x : α), p x ∨ y = x := ⟨y, or.inr rfl⟩ @[simp] theorem forall_apply_eq_imp_iff {f : α → β} {p : β → Prop} : (∀ a, ∀ b, f a = b → p b) ↔ (∀ a, p (f a)) := ⟨λ h a, h a (f a) rfl, λ h a b hab, hab ▸ h a⟩ @[simp] theorem forall_apply_eq_imp_iff' {f : α → β} {p : β → Prop} : (∀ b, ∀ a, f a = b → p b) ↔ (∀ a, p (f a)) := by { rw forall_swap, simp } @[simp] theorem forall_eq_apply_imp_iff {f : α → β} {p : β → Prop} : (∀ a, ∀ b, b = f a → p b) ↔ (∀ a, p (f a)) := by simp [@eq_comm _ _ (f _)] @[simp] theorem forall_eq_apply_imp_iff' {f : α → β} {p : β → Prop} : (∀ b, ∀ a, b = f a → p b) ↔ (∀ a, p (f a)) := by { rw forall_swap, simp } @[simp] theorem forall_apply_eq_imp_iff₂ {f : α → β} {p : α → Prop} {q : β → Prop} : (∀ b, ∀ a, p a → f a = b → q b) ↔ ∀ a, p a → q (f a) := ⟨λ h a ha, h (f a) a ha rfl, λ h b a ha hb, hb ▸ h a ha⟩ @[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 exists_comm {p : α → β → Prop} : (∃ a b, p a b) ↔ ∃ b a, p a b := ⟨λ ⟨a, b, h⟩, ⟨b, a, h⟩, λ ⟨b, a, h⟩, ⟨a, b, h⟩⟩ theorem forall_or_of_or_forall (h : b ∨ ∀x, p x) (x) : b ∨ p x := h.imp_right $ λ h₂, h₂ x -- See Note [decidable namespace] protected theorem decidable.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⟩ theorem forall_or_distrib_left {q : Prop} {p : α → Prop} : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := decidable.forall_or_distrib_left -- See Note [decidable namespace] protected theorem decidable.forall_or_distrib_right {q : Prop} {p : α → Prop} [decidable q] : (∀x, p x ∨ q) ↔ (∀x, p x) ∨ q := by simp [or_comm, decidable.forall_or_distrib_left] theorem forall_or_distrib_right {q : Prop} {p : α → Prop} : (∀x, p x ∨ q) ↔ (∀x, p x) ∨ q := decidable.forall_or_distrib_right /-- A predicate holds everywhere on the image of a surjective functions iff it holds everywhere. -/ theorem forall_iff_forall_surj {α β : Type*} {f : α → β} (h : function.surjective f) {P : β → Prop} : (∀ a, P (f a)) ↔ ∀ b, P b := ⟨λ ha b, by cases h b with a hab; rw ←hab; exact ha a, λ hb a, hb $ f a⟩ @[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 @[simp] lemma exists_unique_false : ¬ (∃! (a : α), false) := assume ⟨a, h, 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 theorem forall_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∀ h' : p, q h') ↔ q h := @forall_const (q h) p ⟨h⟩ theorem exists_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∃ h' : p, q h') ↔ q h := @exists_const (q h) p ⟨h⟩ theorem forall_prop_of_false {p : Prop} {q : p → Prop} (hn : ¬ p) : (∀ h' : p, q h') ↔ true := iff_true_intro $ λ h, hn.elim h theorem exists_prop_of_false {p : Prop} {q : p → Prop} : ¬ p → ¬ (∃ h' : p, q h') := mt Exists.fst @[congr] lemma exists_prop_congr {p p' : Prop} {q q' : p → Prop} (hq : ∀ h, q h ↔ q' h) (hp : p ↔ p') : Exists q ↔ ∃ h : p', q' (hp.2 h) := ⟨λ ⟨_, _⟩, ⟨hp.1 ‹_›, (hq _).1 ‹_›⟩, λ ⟨_, _⟩, ⟨_, (hq _).2 ‹_›⟩⟩ @[congr] lemma exists_prop_congr' {p p' : Prop} {q q' : p → Prop} (hq : ∀ h, q h ↔ q' h) (hp : p ↔ p') : Exists q = ∃ h : p', q' (hp.2 h) := propext (exists_prop_congr hq _) @[simp] lemma exists_true_left (p : true → Prop) : (∃ x, p x) ↔ p true.intro := exists_prop_of_true _ @[simp] lemma exists_false_left (p : false → Prop) : ¬ ∃ x, p x := exists_prop_of_false not_false lemma exists_unique.exists {α : Sort*} {p : α → Prop} (h : ∃! x, p x) : ∃ x, p x := exists.elim h (λ x hx, ⟨x, and.left hx⟩) lemma exists_unique.unique {α : Sort*} {p : α → Prop} (h : ∃! x, p x) {y₁ y₂ : α} (py₁ : p y₁) (py₂ : p y₂) : y₁ = y₂ := unique_of_exists_unique h py₁ py₂ @[congr] lemma forall_prop_congr {p p' : Prop} {q q' : p → Prop} (hq : ∀ h, q h ↔ q' h) (hp : p ↔ p') : (∀ h, q h) ↔ ∀ h : p', q' (hp.2 h) := ⟨λ h1 h2, (hq _).1 (h1 (hp.2 _)), λ h1 h2, (hq _).2 (h1 (hp.1 h2))⟩ @[congr] lemma forall_prop_congr' {p p' : Prop} {q q' : p → Prop} (hq : ∀ h, q h ↔ q' h) (hp : p ↔ p') : (∀ h, q h) = ∀ h : p', q' (hp.2 h) := propext (forall_prop_congr hq _) @[simp] lemma forall_true_left (p : true → Prop) : (∀ x, p x) ↔ p true.intro := forall_prop_of_true _ @[simp] lemma forall_false_left (p : false → Prop) : (∀ x, p x) ↔ true := forall_prop_of_false not_false @[simp] lemma exists_unique_iff_exists {α : Sort*} [subsingleton α] {p : α → Prop} : (∃! x, p x) ↔ ∃ x, p x := ⟨λ h, h.exists, Exists.imp $ λ x hx, ⟨hx, λ y _, subsingleton.elim y x⟩⟩ lemma exists_unique.elim2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π x (h : p x), Prop} {b : Prop} (h₂ : ∃! x (h : p x), q x h) (h₁ : ∀ x (h : p x), q x h → (∀ y (hy : p y), q y hy → y = x) → b) : b := begin simp only [exists_unique_iff_exists] at h₂, apply h₂.elim, exact λ x ⟨hxp, hxq⟩ H, h₁ x hxp hxq (λ y hyp hyq, H y ⟨hyp, hyq⟩) end lemma exists_unique.intro2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π (x : α) (h : p x), Prop} (w : α) (hp : p w) (hq : q w hp) (H : ∀ y (hy : p y), q y hy → y = w) : ∃! x (hx : p x), q x hx := begin simp only [exists_unique_iff_exists], exact exists_unique.intro w ⟨hp, hq⟩ (λ y ⟨hyp, hyq⟩, H y hyp hyq) end lemma exists_unique.exists2 {α : Sort*} {p : α → Sort*} {q : Π (x : α) (h : p x), Prop} (h : ∃! x (hx : p x), q x hx) : ∃ x (hx : p x), q x hx := h.exists.imp (λ x hx, hx.exists) lemma exists_unique.unique2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π (x : α) (hx : p x), Prop} (h : ∃! x (hx : p x), q x hx) {y₁ y₂ : α} (hpy₁ : p y₁) (hqy₁ : q y₁ hpy₁) (hpy₂ : p y₂) (hqy₂ : q y₂ hpy₂) : y₁ = y₂ := begin simp only [exists_unique_iff_exists] at h, exact h.unique ⟨hpy₁, hqy₁⟩ ⟨hpy₂, hqy₂⟩ end end quantifiers /-! ### Classical lemmas -/ namespace classical variables {α : Sort*} {p : α → Prop} theorem cases {p : Prop → Prop} (h1 : p true) (h2 : p false) : ∀a, p a := assume a, cases_on a h1 h2 /- use shortened names to avoid conflict when classical namespace is open. -/ noncomputable lemma dec (p : Prop) : decidable p := -- see Note [classical lemma] by apply_instance noncomputable lemma dec_pred (p : α → Prop) : decidable_pred p := -- see Note [classical lemma] by apply_instance noncomputable lemma dec_rel (p : α → α → Prop) : decidable_rel p := -- see Note [classical lemma] by apply_instance noncomputable lemma dec_eq (α : Sort*) : decidable_eq α := -- see Note [classical lemma] by apply_instance /-- We make decidability results that depends on `classical.choice` noncomputable lemmas. * We have to mark them as noncomputable, because otherwise Lean will try to generate bytecode for them, and fail because it depends on `classical.choice`. * We make them lemmas, and not definitions, because otherwise later definitions will raise \"failed to generate bytecode\" errors when writing something like `letI := classical.dec_eq _`. Cf. <https://leanprover-community.github.io/archive/stream/113488-general/topic/noncomputable.20theorem.html> -/ library_note "classical lemma" /-- Construct a function from a default value `H0`, and a function to use if there exists a value satisfying the predicate. -/ @[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 {α : Sort*} {p : α → Prop} {h : ∃a, p a} (q : α → Prop) (hpq : ∀a, p a → q a) : q (some h) := hpq _ $ some_spec _ /-- A version of classical.indefinite_description which is definitionally equal to a pair -/ noncomputable def subtype_of_exists {α : Type*} {P : α → Prop} (h : ∃ x, P x) : {x // P x} := ⟨classical.some h, classical.some_spec h⟩ end classical /-- This function has the same type as `exists.rec_on`, and can be used to case on an equality, but `exists.rec_on` can only eliminate into Prop, while this version eliminates into any universe using the axiom of choice. -/ @[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) /-! ### Declarations about 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 bex_eq_left {a : α} : (∃ x (_ : x = a), p x) ↔ p a := by simp only [exists_prop, exists_eq_left] 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) : 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 -- See Note [decidable namespace] protected theorem decidable.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.decidable_imp_symm $ λ nx x h, nx.decidable_imp_symm $ λ h', ⟨x, h, h'⟩, not_ball_of_bex_not⟩ theorem not_ball : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := decidable.not_ball 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 theorem ball_or_left_distrib : (∀ x, p x ∨ q x → r x) ↔ (∀ x, p x → r x) ∧ (∀ x, q x → r x) := iff.trans (forall_congr $ λ x, or_imp_distrib) forall_and_distrib theorem bex_or_left_distrib : (∃ x (_ : p x ∨ q x), r x) ↔ (∃ x (_ : p x), r x) ∨ (∃ x (_ : q x), r x) := by simp only [exists_prop]; exact iff.trans (exists_congr $ λ x, or_and_distrib_right) 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 lemma ite_eq_iff {α} {p : Prop} [decidable p] {a b c : α} : (if p then a else b) = c ↔ p ∧ a = c ∨ ¬p ∧ b = c := by by_cases p; simp * @[simp] lemma ite_eq_left_iff {α} {p : Prop} [decidable p] {a b : α} : (if p then a else b) = a ↔ (¬p → b = a) := by by_cases p; simp * @[simp] lemma ite_eq_right_iff {α} {p : Prop} [decidable p] {a b : α} : (if p then a else b) = b ↔ (p → a = b) := by by_cases p; simp * /-! ### Declarations about `nonempty` -/ section nonempty universe variables u v w variables {α : Type u} {β : Type v} {γ : α → Type w} attribute [simp] nonempty_of_inhabited @[priority 20] instance has_zero.nonempty [has_zero α] : nonempty α := ⟨0⟩ @[priority 20] instance has_one.nonempty [has_one α] : nonempty α := ⟨1⟩ 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 {α : Sort*} : ¬ 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⟩) /-- Using `classical.choice`, lifts a (`Prop`-valued) `nonempty` instance to a (`Type`-valued) `inhabited` instance. `classical.inhabited_of_nonempty` already exists, in `core/init/classical.lean`, but the assumption is not a type class argument, which makes it unsuitable for some applications. -/ noncomputable def classical.inhabited_of_nonempty' {α : Sort u} [h : nonempty α] : inhabited α := ⟨classical.choice h⟩ /-- Using `classical.choice`, extracts a term from a `nonempty` type. -/ @[reducible] protected noncomputable def nonempty.some {α : Sort u} (h : nonempty α) : α := classical.choice h /-- Using `classical.choice`, extracts a term from a `nonempty` type. -/ @[reducible] protected noncomputable def classical.arbitrary (α : Sort u) [h : nonempty α] : α := classical.choice h /-- Given `f : α → β`, if `α` is nonempty then `β` is also nonempty. `nonempty` cannot be a `functor`, because `functor` is restricted to `Type`. -/ lemma nonempty.map {α : Sort u} {β : Sort v} (f : α → β) : nonempty α → nonempty β | ⟨h⟩ := ⟨f h⟩ protected lemma nonempty.map2 {α β γ : Sort*} (f : α → β → γ) : nonempty α → nonempty β → nonempty γ | ⟨x⟩ ⟨y⟩ := ⟨f x y⟩ protected lemma nonempty.congr {α : Sort u} {β : Sort v} (f : α → β) (g : β → α) : nonempty α ↔ nonempty β := ⟨nonempty.map f, nonempty.map g⟩ lemma nonempty.elim_to_inhabited {α : Sort*} [h : nonempty α] {p : Prop} (f : inhabited α → p) : p := h.elim $ f ∘ inhabited.mk instance {α β} [h : nonempty α] [h2 : nonempty β] : nonempty (α × β) := h.elim $ λ g, h2.elim $ λ g2, ⟨⟨g, g2⟩⟩ end nonempty lemma subsingleton_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : subsingleton α := ⟨λ x, false.elim $ not_nonempty_iff_imp_false.mp h x⟩ section ite /-- A `dite` whose results do not actually depend on the condition may be reduced to an `ite`. -/ @[simp] lemma dite_eq_ite (P : Prop) [decidable P] {α : Sort*} (x y : α) : dite P (λ h, x) (λ h, y) = ite P x y := rfl /-- A function applied to a `dite` is a `dite` of that function applied to each of the branches. -/ lemma apply_dite {α β : Sort*} (f : α → β) (P : Prop) [decidable P] (x : P → α) (y : ¬P → α) : f (dite P x y) = dite P (λ h, f (x h)) (λ h, f (y h)) := by { by_cases h : P; simp [h] } /-- A function applied to a `ite` is a `ite` of that function applied to each of the branches. -/ lemma apply_ite {α β : Sort*} (f : α → β) (P : Prop) [decidable P] (x y : α) : f (ite P x y) = ite P (f x) (f y) := apply_dite f P (λ _, x) (λ _, y) /-- A two-argument function applied to two `dite`s is a `dite` of that two-argument function applied to each of the branches. -/ lemma apply_dite2 {α β γ : Sort*} (f : α → β → γ) (P : Prop) [decidable P] (a : P → α) (b : ¬P → α) (c : P → β) (d : ¬P → β) : f (dite P a b) (dite P c d) = dite P (λ h, f (a h) (c h)) (λ h, f (b h) (d h)) := by { by_cases h : P; simp [h] } /-- A two-argument function applied to two `ite`s is a `ite` of that two-argument function applied to each of the branches. -/ lemma apply_ite2 {α β γ : Sort*} (f : α → β → γ) (P : Prop) [decidable P] (a b : α) (c d : β) : f (ite P a b) (ite P c d) = ite P (f a c) (f b d) := apply_dite2 f P (λ _, a) (λ _, b) (λ _, c) (λ _, d) /-- A 'dite' producing a `Pi` type `Π a, β a`, applied to a value `x : α` is a `dite` that applies either branch to `x`. -/ lemma dite_apply {α : Sort*} {β : α → Sort*} (P : Prop) [decidable P] (f : P → Π a, β a) (g : ¬ P → Π a, β a) (x : α) : (dite P f g) x = dite P (λ h, f h x) (λ h, g h x) := by { by_cases h : P; simp [h] } /-- A 'ite' producing a `Pi` type `Π a, β a`, applied to a value `x : α` is a `ite` that applies either branch to `x` -/ lemma ite_apply {α : Sort*} {β : α → Sort*} (P : Prop) [decidable P] (f g : Π a, β a) (x : α) : (ite P f g) x = ite P (f x) (g x) := dite_apply P (λ _, f) (λ _, g) x /-- Negation of the condition `P : Prop` in a `dite` is the same as swapping the branches. -/ @[simp] lemma dite_not {α : Sort*} (P : Prop) [decidable P] (x : ¬ P → α) (y : ¬¬ P → α) : dite (¬ P) x y = dite P (λ h, y (not_not_intro h)) x := by { by_cases h : P; simp [h] } /-- Negation of the condition `P : Prop` in a `ite` is the same as swapping the branches. -/ @[simp] lemma ite_not {α : Sort*} (P : Prop) [decidable P] (x y : α) : ite (¬ P) x y = ite P y x := dite_not P (λ _, x) (λ _, y) lemma ite_and {α} {p q : Prop} [decidable p] [decidable q] {x y : α} : ite (p ∧ q) x y = ite p (ite q x y) y := by { by_cases hp : p; by_cases hq : q; simp [hp, hq] } end ite
0288d2d259c940b0e9e6776dd72f75621e369f43
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/sets_functions_and_relations/unnamed_480.lean
b028db2821875c0dc1ed04c24633d5be1a626c13
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
133
lean
import data.nat.prime data.nat.parity tactic open set nat example : { n | prime n } ∩ { n | n > 2} ⊆ { n | ¬ even n } := sorry
5e56cba8d9da1da8b100bd385f403995a1d304f9
ad0c7d243dc1bd563419e2767ed42fb323d7beea
/order/filter.lean
4e3dcf6f4c1854fc41f888fd23c5d18b066d1e39
[ "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
82,935
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 Theory of filters on sets. -/ import order.galois_connection order.zorn import data.set.finite data.list import category.applicative open lattice set universes u v w x y local attribute [instance] classical.prop_decidable namespace lattice variables {α : Type u} {ι : Sort v} section variable [complete_lattice α] lemma Inf_eq_finite_sets {s : set α} : Inf s = (⨅ t ∈ { t | finite t ∧ t ⊆ s}, Inf t) := le_antisymm (le_infi $ assume t, le_infi $ assume ⟨_, h⟩, Inf_le_Inf h) (le_Inf $ assume b h, infi_le_of_le {b} $ infi_le_of_le (by simp [h]) $ Inf_le $ by simp) lemma infi_insert_finset {ι : Type v} {s : finset ι} {f : ι → α} {i : ι} : (⨅j∈insert i s, f j) = f i ⊓ (⨅j∈s, f j) := by simp [infi_or, infi_inf_eq] lemma infi_empty_finset {ι : Type v} {f : ι → α} : (⨅j∈(∅ : finset ι), f j) = ⊤ := by simp end -- TODO: move lemma inf_left_comm [semilattice_inf α] (a b c : α) : a ⊓ (b ⊓ c) = b ⊓ (a ⊓ c) := by rw [← inf_assoc, ← inf_assoc, @inf_comm α _ a] def complete_lattice.copy (c : complete_lattice α) (le : α → α → Prop) (eq_le : le = @complete_lattice.le α c) (top : α) (eq_top : top = @complete_lattice.top α c) (bot : α) (eq_bot : bot = @complete_lattice.bot α c) (sup : α → α → α) (eq_sup : sup = @complete_lattice.sup α c) (inf : α → α → α) (eq_inf : inf = @complete_lattice.inf α c) (Sup : set α → α) (eq_Sup : Sup = @complete_lattice.Sup α c) (Inf : set α → α) (eq_Inf : Inf = @complete_lattice.Inf α c) : complete_lattice α := begin refine { le := le, top := top, bot := bot, sup := sup, inf := inf, Sup := Sup, Inf := Inf, ..}; subst_vars, exact @complete_lattice.le_refl α c, exact @complete_lattice.le_trans α c, exact @complete_lattice.le_antisymm α c, exact @complete_lattice.le_sup_left α c, exact @complete_lattice.le_sup_right α c, exact @complete_lattice.sup_le α c, exact @complete_lattice.inf_le_left α c, exact @complete_lattice.inf_le_right α c, exact @complete_lattice.le_inf α c, exact @complete_lattice.le_top α c, exact @complete_lattice.bot_le α c, exact @complete_lattice.le_Sup α c, exact @complete_lattice.Sup_le α c, exact @complete_lattice.Inf_le α c, exact @complete_lattice.le_Inf α c end end lattice namespace set variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {ι : Sort y} theorem monotone_inter [preorder β] {f g : β → set α} (hf : monotone f) (hg : monotone g) : monotone (λx, (f x) ∩ (g x)) := assume a b h x ⟨h₁, h₂⟩, ⟨hf h h₁, hg h h₂⟩ theorem monotone_set_of [preorder α] {p : α → β → Prop} (hp : ∀b, monotone (λa, p a b)) : monotone (λa, {b | p a b}) := assume a a' h b, hp b h end set open set lattice section order variables {α : Type u} (r : α → α → Prop) local infix `≼` : 50 := r lemma directed_on_Union {r} {ι : Sort v} {f : ι → set α} (hd : directed (⊆) f) (h : ∀x, directed_on r (f x)) : directed_on r (⋃x, f x) := by simp [directed_on]; exact assume a₁ b₁ fb₁ a₂ b₂ fb₂, let ⟨z, zb₁, zb₂⟩ := hd b₁ b₂, ⟨x, xf, xa₁, xa₂⟩ := h z a₁ (zb₁ fb₁) a₂ (zb₂ fb₂) in ⟨x, ⟨z, xf⟩, xa₁, xa₂⟩ end order theorem directed_of_chain {α β r} [is_refl β r] {f : α → β} {c : set α} (h : zorn.chain (f ⁻¹'o r) c) : directed r (λx:{a:α // a ∈ c}, f (x.val)) := assume ⟨a, ha⟩ ⟨b, hb⟩, classical.by_cases (assume : a = b, by simp [this]; exact ⟨b, hb, refl _⟩) (assume : a ≠ b, (h a ha b hb this).elim (λ h : r (f a) (f b), ⟨⟨b, hb⟩, h, refl _⟩) (λ h : r (f b) (f a), ⟨⟨a, ha⟩, refl _, h⟩)) structure filter (α : Type*) := (sets : set (set α)) (univ_sets : set.univ ∈ sets) (sets_of_superset {x y} : x ∈ sets → x ⊆ y → y ∈ sets) (inter_sets {x y} : x ∈ sets → y ∈ sets → x ∩ y ∈ sets) namespace filter variables {α : Type u} {f g : filter α} {s t : set α} lemma filter_eq : ∀{f g : filter α}, f.sets = g.sets → f = g | ⟨a, _, _, _⟩ ⟨._, _, _, _⟩ rfl := rfl lemma filter_eq_iff : f = g ↔ f.sets = g.sets := ⟨congr_arg _, filter_eq⟩ protected lemma ext_iff : f = g ↔ ∀ s, s ∈ f.sets ↔ s ∈ g.sets := by rw [filter_eq_iff, ext_iff] @[extensionality] protected lemma ext : (∀ s, s ∈ f.sets ↔ s ∈ g.sets) → f = g := filter.ext_iff.2 lemma univ_mem_sets : univ ∈ f.sets := f.univ_sets lemma mem_sets_of_superset : ∀{x y : set α}, x ∈ f.sets → x ⊆ y → y ∈ f.sets := f.sets_of_superset lemma inter_mem_sets : ∀{s t}, s ∈ f.sets → t ∈ f.sets → s ∩ t ∈ f.sets := f.inter_sets lemma univ_mem_sets' (h : ∀ a, a ∈ s): s ∈ f.sets := mem_sets_of_superset univ_mem_sets (assume x _, h x) lemma mp_sets (hs : s ∈ f.sets) (h : {x | x ∈ s → x ∈ t} ∈ f.sets) : t ∈ f.sets := mem_sets_of_superset (inter_mem_sets hs h) $ assume x ⟨h₁, h₂⟩, h₂ h₁ lemma Inter_mem_sets {β : Type v} {s : β → set α} {is : set β} (hf : finite is) : (∀i∈is, s i ∈ f.sets) → (⋂i∈is, s i) ∈ f.sets := finite.induction_on hf (assume hs, by simp [univ_mem_sets]) (assume i is _ hf hi hs, have h₁ : s i ∈ f.sets, from hs i (by simp), have h₂ : (⋂x∈is, s x) ∈ f.sets, from hi $ assume a ha, hs _ $ by simp [ha], by simp [inter_mem_sets h₁ h₂]) lemma exists_sets_subset_iff : (∃t∈f.sets, t ⊆ s) ↔ s ∈ f.sets := ⟨assume ⟨t, ht, ts⟩, mem_sets_of_superset ht ts, assume hs, ⟨s, hs, subset.refl _⟩⟩ lemma monotone_mem_sets {f : filter α} : monotone (λs, s ∈ f.sets) := assume s t hst h, mem_sets_of_superset h hst end filter namespace tactic.interactive open tactic interactive /-- `filter [t1, ⋯, tn]` replaces a goal of the form `s ∈ f.sets` and terms `h1 : t1 ∈ f.sets, ⋯, tn ∈ f.sets` with `∀x, x ∈ t1 → ⋯ → x ∈ tn → x ∈ s`. `filter [t1, ⋯, tn] e` is a short form for `{ filter [t1, ⋯, tn], exact e }`. -/ meta def filter_upwards (s : parse types.pexpr_list) (e' : parse $ optional types.texpr) : tactic unit := do s.reverse.mmap (λ e, eapplyc `filter.mp_sets >> eapply e), eapplyc `filter.univ_mem_sets', match e' with | some e := interactive.exact e | none := skip end end tactic.interactive namespace filter variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} section principal /-- The principal filter of `s` is the collection of all supersets of `s`. -/ def principal (s : set α) : filter α := { sets := {t | s ⊆ t}, univ_sets := subset_univ s, sets_of_superset := assume x y hx hy, subset.trans hx hy, inter_sets := assume x y, subset_inter } instance : inhabited (filter α) := ⟨principal ∅⟩ @[simp] lemma mem_principal_sets {s t : set α} : s ∈ (principal t).sets ↔ t ⊆ s := iff.rfl lemma mem_principal_self (s : set α) : s ∈ (principal s).sets := subset.refl _ end principal section join /-- The join of a filter of filters is defined by the relation `s ∈ join f ↔ {t | s ∈ t} ∈ f`. -/ def join (f : filter (filter α)) : filter α := { sets := {s | {t : filter α | s ∈ t.sets} ∈ f.sets}, univ_sets := by simp [univ_mem_sets]; exact univ_mem_sets, sets_of_superset := assume x y hx xy, mem_sets_of_superset hx $ assume f h, mem_sets_of_superset h xy, inter_sets := assume x y hx hy, mem_sets_of_superset (inter_mem_sets hx hy) $ assume f ⟨h₁, h₂⟩, inter_mem_sets h₁ h₂ } @[simp] lemma mem_join_sets {s : set α} {f : filter (filter α)} : s ∈ (join f).sets ↔ {t | s ∈ filter.sets t} ∈ f.sets := iff.rfl end join section lattice instance : partial_order (filter α) := { le := λf g, g.sets ⊆ f.sets, le_antisymm := assume a b h₁ h₂, filter_eq $ subset.antisymm h₂ h₁, le_refl := assume a, subset.refl _, le_trans := assume a b c h₁ h₂, subset.trans h₂ h₁ } theorem le_def {f g : filter α} : f ≤ g ↔ ∀ x ∈ g.sets, x ∈ f.sets := iff.rfl /-- `generate_sets g s`: `s` is in the filter closure of `g`. -/ inductive generate_sets (g : set (set α)) : set α → Prop | basic {s : set α} : s ∈ g → generate_sets s | univ {} : generate_sets univ | superset {s t : set α} : generate_sets s → s ⊆ t → generate_sets t | inter {s t : set α} : generate_sets s → generate_sets t → generate_sets (s ∩ t) /-- `generate g` is the smallest filter containing the sets `g`. -/ def generate (g : set (set α)) : filter α := { sets := {s | generate_sets g s}, univ_sets := generate_sets.univ, sets_of_superset := assume x y, generate_sets.superset, inter_sets := assume s t, generate_sets.inter } lemma sets_iff_generate {s : set (set α)} {f : filter α} : f ≤ filter.generate s ↔ s ⊆ f.sets := iff.intro (assume h u hu, h $ generate_sets.basic $ hu) (assume h u hu, hu.rec_on h univ_mem_sets (assume x y _ hxy hx, mem_sets_of_superset hx hxy) (assume x y _ _ hx hy, inter_mem_sets hx hy)) protected def mk_of_closure (s : set (set α)) (hs : (generate s).sets = s) : filter α := { sets := s, univ_sets := hs ▸ univ_mem_sets, sets_of_superset := assume x y, hs ▸ mem_sets_of_superset, inter_sets := assume x y, hs ▸ inter_mem_sets } lemma mk_of_closure_sets {s : set (set α)} {hs : (generate s).sets = s} : filter.mk_of_closure s hs = generate s := filter.ext $ assume u, hs.symm ▸ iff.refl _ /- Galois insertion from sets of sets into a filters. -/ def gi_generate (α : Type*) : @galois_insertion (set (set α)) (order_dual (filter α)) _ _ filter.generate filter.sets := { gc := assume s f, sets_iff_generate, le_l_u := assume f u, generate_sets.basic, choice := λs hs, filter.mk_of_closure s (le_antisymm hs $ sets_iff_generate.1 $ le_refl _), choice_eq := assume s hs, mk_of_closure_sets } /-- The infimum of filters is the filter generated by intersections of elements of the two filters. -/ instance : has_inf (filter α) := ⟨λf g : filter α, { sets := {s | ∃ (a ∈ f.sets) (b ∈ g.sets), a ∩ b ⊆ s }, univ_sets := ⟨_, univ_mem_sets, _, univ_mem_sets, inter_subset_left _ _⟩, sets_of_superset := assume x y ⟨a, ha, b, hb, h⟩ xy, ⟨a, ha, b, hb, subset.trans h xy⟩, inter_sets := assume x y ⟨a, ha, b, hb, hx⟩ ⟨c, hc, d, hd, hy⟩, ⟨_, inter_mem_sets ha hc, _, inter_mem_sets hb hd, calc a ∩ c ∩ (b ∩ d) = (a ∩ b) ∩ (c ∩ d) : by ac_refl ... ⊆ x ∩ y : inter_subset_inter hx hy⟩ }⟩ @[simp] lemma mem_inf_sets {f g : filter α} {s : set α} : s ∈ (f ⊓ g).sets ↔ ∃t₁∈f.sets, ∃t₂∈g.sets, t₁ ∩ t₂ ⊆ s := iff.rfl lemma mem_inf_sets_of_left {f g : filter α} {s : set α} (h : s ∈ f.sets) : s ∈ (f ⊓ g).sets := ⟨s, h, univ, univ_mem_sets, inter_subset_left _ _⟩ lemma mem_inf_sets_of_right {f g : filter α} {s : set α} (h : s ∈ g.sets) : s ∈ (f ⊓ g).sets := ⟨univ, univ_mem_sets, s, h, inter_subset_right _ _⟩ lemma inter_mem_inf_sets {α : Type u} {f g : filter α} {s t : set α} (hs : s ∈ f.sets) (ht : t ∈ g.sets) : s ∩ t ∈ (f ⊓ g).sets := inter_mem_sets (mem_inf_sets_of_left hs) (mem_inf_sets_of_right ht) instance : has_top (filter α) := ⟨{ sets := {s | ∀x, x ∈ s}, univ_sets := assume x, mem_univ x, sets_of_superset := assume x y hx hxy a, hxy (hx a), inter_sets := assume x y hx hy a, mem_inter (hx _) (hy _) }⟩ lemma mem_top_sets_iff_forall {s : set α} : s ∈ (⊤ : filter α).sets ↔ (∀x, x ∈ s) := iff.refl _ @[simp] lemma mem_top_sets {s : set α} : s ∈ (⊤ : filter α).sets ↔ s = univ := by rw [mem_top_sets_iff_forall, eq_univ_iff_forall] section complete_lattice /- We lift the complete lattice along the Galois connection `generate` / `sets`. Unfortunately, we want to have different definitional equalities for the lattice operations. So we define them upfront and change the lattice operations for the complete lattice instance. -/ private def original_complete_lattice : complete_lattice (filter α) := @order_dual.lattice.complete_lattice _ (gi_generate α).lift_complete_lattice local attribute [instance] original_complete_lattice instance : complete_lattice (filter α) := original_complete_lattice.copy /- le -/ filter.partial_order.le rfl /- top -/ (filter.lattice.has_top).1 (top_unique $ assume s hs, (eq_univ_of_forall hs).symm ▸ univ_mem_sets) /- bot -/ _ rfl /- sup -/ _ rfl /- inf -/ (filter.lattice.has_inf).1 begin ext f g : 2, exact le_antisymm (le_inf (assume s, mem_inf_sets_of_left) (assume s, mem_inf_sets_of_right)) (assume s ⟨a, ha, b, hb, hs⟩, mem_sets_of_superset (inter_mem_sets (@inf_le_left (filter α) _ _ _ _ ha) (@inf_le_right (filter α) _ _ _ _ hb)) hs) end /- Sup -/ (join ∘ principal) (by ext s x; exact (@mem_bInter_iff _ _ s filter.sets x).symm) /- Inf -/ _ rfl end complete_lattice lemma bot_sets_eq : (⊥ : filter α).sets = univ := rfl lemma sup_sets_eq {f g : filter α} : (f ⊔ g).sets = f.sets ∩ g.sets := (gi_generate α).gc.u_inf lemma Sup_sets_eq {s : set (filter α)} : (Sup s).sets = (⋂f∈s, (f:filter α).sets) := (gi_generate α).gc.u_Inf lemma supr_sets_eq {f : ι → filter α} : (supr f).sets = (⋂i, (f i).sets) := (gi_generate α).gc.u_infi lemma generate_empty : filter.generate ∅ = (⊤ : filter α) := (gi_generate α).gc.l_bot lemma generate_univ : filter.generate univ = (⊥ : filter α) := mk_of_closure_sets.symm lemma generate_union {s t : set (set α)} : filter.generate (s ∪ t) = filter.generate s ⊓ filter.generate t := (gi_generate α).gc.l_sup lemma generate_Union {s : ι → set (set α)} : filter.generate (⋃ i, s i) = (⨅ i, filter.generate (s i)) := (gi_generate α).gc.l_supr @[simp] lemma mem_bot_sets {s : set α} : s ∈ (⊥ : filter α).sets := trivial @[simp] lemma mem_sup_sets {f g : filter α} {s : set α} : s ∈ (f ⊔ g).sets ↔ s ∈ f.sets ∧ s ∈ g.sets := iff.rfl @[simp] lemma mem_Sup_sets {x : set α} {s : set (filter α)} : x ∈ (Sup s).sets ↔ (∀f∈s, x ∈ (f:filter α).sets) := iff.rfl @[simp] lemma mem_supr_sets {x : set α} {f : ι → filter α} : x ∈ (supr f).sets ↔ (∀i, x ∈ (f i).sets) := by simp [supr_sets_eq] @[simp] lemma le_principal_iff {s : set α} {f : filter α} : f ≤ principal s ↔ s ∈ f.sets := show (∀{t}, s ⊆ t → t ∈ f.sets) ↔ s ∈ f.sets, from ⟨assume h, h (subset.refl s), assume hs t ht, mem_sets_of_superset hs ht⟩ lemma principal_mono {s t : set α} : principal s ≤ principal t ↔ s ⊆ t := by simp lemma monotone_principal : monotone (principal : set α → filter α) := by simp [monotone, principal_mono]; exact assume a b h, h @[simp] lemma principal_eq_iff_eq {s t : set α} : principal s = principal t ↔ s = t := by simp [le_antisymm_iff]; refl @[simp] lemma join_principal_eq_Sup {s : set (filter α)} : join (principal s) = Sup s := rfl /- lattice equations -/ lemma empty_in_sets_eq_bot {f : filter α} : ∅ ∈ f.sets ↔ f = ⊥ := ⟨assume h, bot_unique $ assume s _, mem_sets_of_superset h (empty_subset s), assume : f = ⊥, this.symm ▸ mem_bot_sets⟩ lemma inhabited_of_mem_sets {f : filter α} {s : set α} (hf : f ≠ ⊥) (hs : s ∈ f.sets) : ∃x, x ∈ s := have ∅ ∉ f.sets, from assume h, hf $ empty_in_sets_eq_bot.mp h, have s ≠ ∅, from assume h, this (h ▸ hs), exists_mem_of_ne_empty this lemma filter_eq_bot_of_not_nonempty {f : filter α} (ne : ¬ nonempty α) : f = ⊥ := empty_in_sets_eq_bot.mp $ univ_mem_sets' $ assume x, false.elim (ne ⟨x⟩) lemma forall_sets_neq_empty_iff_neq_bot {f : filter α} : (∀ (s : set α), s ∈ f.sets → s ≠ ∅) ↔ f ≠ ⊥ := by simp [(@empty_in_sets_eq_bot α f).symm]; exact ⟨assume h hs, h _ hs rfl, assume h s hs eq, h $ eq ▸ hs⟩ lemma mem_sets_of_neq_bot {f : filter α} {s : set α} (h : f ⊓ principal (-s) = ⊥) : s ∈ f.sets := have ∅ ∈ (f ⊓ principal (- s)).sets, from h.symm ▸ mem_bot_sets, let ⟨s₁, hs₁, s₂, (hs₂ : -s ⊆ s₂), (hs : s₁ ∩ s₂ ⊆ ∅)⟩ := this in by filter_upwards [hs₁] assume a ha, classical.by_contradiction $ assume ha', hs ⟨ha, hs₂ ha'⟩ lemma infi_sets_eq {f : ι → filter α} (h : directed (≥) f) (ne : nonempty ι) : (infi f).sets = (⋃ i, (f i).sets) := let ⟨i⟩ := ne, u := { filter . sets := (⋃ i, (f i).sets), univ_sets := begin simp, exact ⟨i, univ_mem_sets⟩ end, sets_of_superset := begin simp, assume x y i hx hxy, exact ⟨i, mem_sets_of_superset hx hxy⟩ end, inter_sets := begin simp, assume x y a hx b hy, rcases h a b with ⟨c, ha, hb⟩, exact ⟨c, inter_mem_sets (ha hx) (hb hy)⟩ end } in subset.antisymm (show u ≤ infi f, from le_infi $ assume i, le_supr (λi, (f i).sets) i) (Union_subset $ assume i, infi_le f i) lemma infi_sets_eq' {f : β → filter α} {s : set β} (h : directed_on (f ⁻¹'o (≥)) s) (ne : ∃i, i ∈ s) : (⨅ i∈s, f i).sets = (⋃ i ∈ s, (f i).sets) := let ⟨i, hi⟩ := ne in calc (⨅ i ∈ s, f i).sets = (⨅ t : {t // t ∈ s}, (f t.val)).sets : by rw [infi_subtype]; refl ... = (⨆ t : {t // t ∈ s}, (f t.val).sets) : infi_sets_eq (assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end) ⟨⟨i, hi⟩⟩ ... = (⨆ t ∈ {t | t ∈ s}, (f t).sets) : by rw [supr_subtype]; refl lemma Inf_sets_eq_finite {s : set (filter α)} : (Inf s).sets = (⋃ t ∈ {t | finite t ∧ t ⊆ s}, (Inf t).sets) := calc (Inf s).sets = (⨅ t ∈ { t | finite t ∧ t ⊆ s}, Inf t).sets : by rw [lattice.Inf_eq_finite_sets] ... = (⨆ t ∈ {t | finite t ∧ t ⊆ s}, (Inf t).sets) : infi_sets_eq' (assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨x ∪ y, ⟨finite_union hx₁ hy₁, union_subset hx₂ hy₂⟩, Inf_le_Inf $ subset_union_left _ _, Inf_le_Inf $ subset_union_right _ _⟩) ⟨∅, by simp⟩ @[simp] lemma sup_join {f₁ f₂ : filter (filter α)} : (join f₁ ⊔ join f₂) = join (f₁ ⊔ f₂) := filter_eq $ set.ext $ assume x, by simp [supr_sets_eq, join] @[simp] lemma supr_join {ι : Sort w} {f : ι → filter (filter α)} : (⨆x, join (f x)) = join (⨆x, f x) := filter_eq $ set.ext $ assume x, by simp [supr_sets_eq, join] instance : bounded_distrib_lattice (filter α) := { le_sup_inf := begin assume x y z s, simp only [and_assoc, mem_inf_sets, mem_sup_sets, exists_prop, exists_imp_distrib, and_imp], intros hs t₁ ht₁ t₂ ht₂ hts, exact ⟨s ∪ t₁, x.sets_of_superset hs $ subset_union_left _ _, y.sets_of_superset ht₁ $ subset_union_right _ _, s ∪ t₂, x.sets_of_superset hs $ subset_union_left _ _, z.sets_of_superset ht₂ $ subset_union_right _ _, subset.trans (@le_sup_inf (set α) _ _ _ _) (union_subset (subset.refl _) hts)⟩ end, ..filter.lattice.complete_lattice } private lemma infi_finite_distrib {s : set (filter α)} {f : filter α} (h : finite s) : (⨅ a ∈ s, f ⊔ a) = f ⊔ (Inf s) := finite.induction_on h (by simp only [mem_empty_eq, infi_false, infi_top, Inf_empty, sup_top_eq]) (by intros a s hn hs hi; rw [infi_insert, hi, ← sup_inf_left, Inf_insert]) /- the complementary version with ⨆ g∈s, f ⊓ g does not hold! -/ lemma binfi_sup_eq { f : filter α } {s : set (filter α)} : (⨅ g∈s, f ⊔ g) = f ⊔ Inf s := le_antisymm begin intros t h, cases h with h₁ h₂, rw [Inf_sets_eq_finite] at h₂, simp [and_assoc] at h₂, rcases h₂ with ⟨s', hs', hs's, ht'⟩, have ht : t ∈ (⨅ a ∈ s', f ⊔ a).sets, { rw [infi_finite_distrib], exact ⟨h₁, ht'⟩, exact hs' }, clear h₁ ht', revert ht t, change (⨅ a ∈ s, f ⊔ a) ≤ (⨅ a ∈ s', f ⊔ a), apply infi_le_infi2 _, exact assume i, ⟨i, infi_le_infi2 $ assume h, ⟨hs's h, le_refl _⟩⟩ end (le_infi $ assume g, le_infi $ assume h, sup_le_sup (le_refl f) $ Inf_le h) lemma infi_sup_eq { f : filter α } {g : ι → filter α} : (⨅ x, f ⊔ g x) = f ⊔ infi g := calc (⨅ x, f ⊔ g x) = (⨅ x (h : ∃i, g i = x), f ⊔ x) : by simp; rw [infi_comm]; simp ... = f ⊔ Inf {x | ∃i, g i = x} : binfi_sup_eq ... = f ⊔ infi g : by rw [Inf_eq_infi]; dsimp; simp; rw [infi_comm]; simp lemma mem_infi_sets_finset {s : finset α} {f : α → filter β} : ∀t, t ∈ (⨅a∈s, f a).sets ↔ (∃p:α → set β, (∀a∈s, p a ∈ (f a).sets) ∧ (⋂a∈s, p a) ⊆ t) := show ∀t, t ∈ (⨅a∈s, f a).sets ↔ (∃p:α → set β, (∀a∈s, p a ∈ (f a).sets) ∧ (⨅a∈s, p a) ≤ t), begin refine finset.induction_on s _ _, { simp only [finset.not_mem_empty, false_implies_iff, lattice.infi_empty_finset, top_le_iff, imp_true_iff, mem_top_sets, true_and, exists_const], intros; refl }, { intros a s has ih t, simp only [ih, finset.forall_mem_insert, lattice.infi_insert_finset, mem_inf_sets, exists_prop, iff_iff_implies_and_implies, exists_imp_distrib, and_imp, and_assoc] {contextual := tt}, split, { intros t₁ ht₁ t₂ p hp ht₂ ht, existsi function.update p a t₁, have : ∀a'∈s, function.update p a t₁ a' = p a', from assume a' ha', have a' ≠ a, from assume h, has $ h ▸ ha', function.update_noteq this, have eq : (⨅j ∈ s, function.update p a t₁ j) = (⨅j ∈ s, p j), begin congr, funext b, congr, funext h, apply this, assumption end, simp only [this, ht₁, hp, function.update_same, true_and, imp_true_iff, eq] {contextual := tt}, exact subset.trans (inter_subset_inter (subset.refl _) ht₂) ht }, from assume p hpa hp ht, ⟨p a, hpa, (⨅j∈s, p j), ⟨⟨p, hp, le_refl _⟩, ht⟩⟩ } end /- principal equations -/ @[simp] lemma inf_principal {s t : set α} : principal s ⊓ principal t = principal (s ∩ t) := le_antisymm (by simp; exact ⟨s, subset.refl s, t, subset.refl t, by simp⟩) (by simp [le_inf_iff, inter_subset_left, inter_subset_right]) @[simp] lemma sup_principal {s t : set α} : principal s ⊔ principal t = principal (s ∪ t) := filter_eq $ set.ext $ by simp [union_subset_iff] @[simp] lemma supr_principal {ι : Sort w} {s : ι → set α} : (⨆x, principal (s x)) = principal (⋃i, s i) := filter_eq $ set.ext $ assume x, by simp [supr_sets_eq]; exact (@supr_le_iff (set α) _ _ _ _).symm lemma principal_univ : principal (univ : set α) = ⊤ := top_unique $ by simp lemma principal_empty : principal (∅ : set α) = ⊥ := bot_unique $ assume s _, empty_subset _ @[simp] lemma principal_eq_bot_iff {s : set α} : principal s = ⊥ ↔ s = ∅ := ⟨assume h, principal_eq_iff_eq.mp $ by simp [principal_empty, h], assume h, by simp [*, principal_empty]⟩ lemma inf_principal_eq_bot {f : filter α} {s : set α} (hs : -s ∈ f.sets) : f ⊓ principal s = ⊥ := empty_in_sets_eq_bot.mp ⟨_, hs, s, mem_principal_self s, assume x ⟨h₁, h₂⟩, h₁ h₂⟩ end lattice section map /-- The forward map of a filter -/ def map (m : α → β) (f : filter α) : filter β := { sets := preimage m ⁻¹' f.sets, univ_sets := univ_mem_sets, sets_of_superset := assume s t hs st, mem_sets_of_superset hs $ preimage_mono st, inter_sets := assume s t hs ht, inter_mem_sets hs ht } @[simp] lemma map_principal {s : set α} {f : α → β} : map f (principal s) = principal (set.image f s) := filter_eq $ set.ext $ assume a, image_subset_iff.symm variables {f : filter α} {m : α → β} {m' : β → γ} {s : set α} {t : set β} @[simp] lemma mem_map : t ∈ (map m f).sets ↔ {x | m x ∈ t} ∈ f.sets := iff.rfl lemma image_mem_map (hs : s ∈ f.sets) : m '' s ∈ (map m f).sets := f.sets_of_superset hs $ subset_preimage_image m s lemma mem_map_sets_iff : t ∈ (map m f).sets ↔ (∃s∈f.sets, m '' s ⊆ t) := iff.intro (assume ht, ⟨set.preimage m t, ht, image_preimage_subset _ _⟩) (assume ⟨s, hs, ht⟩, mem_sets_of_superset (image_mem_map hs) ht) @[simp] lemma map_id : filter.map id f = f := filter_eq $ rfl @[simp] lemma map_compose : filter.map m' ∘ filter.map m = filter.map (m' ∘ m) := funext $ assume _, filter_eq $ rfl @[simp] lemma map_map : filter.map m' (filter.map m f) = filter.map (m' ∘ m) f := congr_fun (@@filter.map_compose m m') f end map section comap /-- The inverse map of a filter -/ def comap (m : α → β) (f : filter β) : filter α := { sets := { s | ∃t∈f.sets, m ⁻¹' t ⊆ s }, univ_sets := ⟨univ, univ_mem_sets, by simp⟩, sets_of_superset := assume a b ⟨a', ha', ma'a⟩ ab, ⟨a', ha', subset.trans ma'a ab⟩, inter_sets := assume a b ⟨a', ha₁, ha₂⟩ ⟨b', hb₁, hb₂⟩, ⟨a' ∩ b', inter_mem_sets ha₁ hb₁, inter_subset_inter ha₂ hb₂⟩ } end comap /-- The cofinite filter is the filter of subsets whose complements are finite. -/ def cofinite : filter α := { sets := {s | finite (- s)}, univ_sets := by simp, sets_of_superset := assume s t (hs : finite (-s)) (st: s ⊆ t), finite_subset hs $ @lattice.neg_le_neg (set α) _ _ _ st, inter_sets := assume s t (hs : finite (-s)) (ht : finite (-t)), by simp [compl_inter, finite_union, ht, hs] } /-- The monadic bind operation on filter is defined the usual way in terms of `map` and `join`. Unfortunately, this `bind` does not result in the expected applicative. See `filter.seq` for the applicative instance. -/ def bind (f : filter α) (m : α → filter β) : filter β := join (map m f) /-- The applicative sequentiation operation. This is not induced by the bind operation. -/ def seq (f : filter (α → β)) (g : filter α) : filter β := ⟨{ s | ∃u∈f.sets, ∃t∈g.sets, (∀m∈u, ∀x∈t, (m : α → β) x ∈ s) }, ⟨univ, univ_mem_sets, univ, univ_mem_sets, by simp⟩, assume s₀ s₁ ⟨t₀, t₁, h₀, h₁, h⟩ hst, ⟨t₀, t₁, h₀, h₁, assume x hx y hy, hst $ h _ hx _ hy⟩, assume s₀ s₁ ⟨t₀, ht₀, t₁, ht₁, ht⟩ ⟨u₀, hu₀, u₁, hu₁, hu⟩, ⟨t₀ ∩ u₀, inter_mem_sets ht₀ hu₀, t₁ ∩ u₁, inter_mem_sets ht₁ hu₁, assume x ⟨hx₀, hx₁⟩ x ⟨hy₀, hy₁⟩, ⟨ht _ hx₀ _ hy₀, hu _ hx₁ _ hy₁⟩⟩⟩ instance : has_pure filter := ⟨λ(α : Type u) x, principal {x}⟩ instance : has_bind filter := ⟨@filter.bind⟩ instance : has_seq filter := ⟨@filter.seq⟩ instance : functor filter := { map := @filter.map } section -- this section needs to be before applicative, otherwiese the wrong instance will be chosen protected def monad : monad filter := { map := @filter.map } local attribute [instance] filter.monad protected def is_lawful_monad : is_lawful_monad filter := { id_map := assume α f, filter_eq rfl, pure_bind := assume α β a f, by simp [bind, Sup_image], bind_assoc := assume α β γ f m₁ m₂, filter_eq rfl, bind_pure_comp_eq_map := assume α β f x, filter_eq $ by simp [bind, join, map, preimage, principal] } end instance : applicative filter := { map := @filter.map, seq := @filter.seq } instance : alternative filter := { failure := λα, ⊥, orelse := λα x y, x ⊔ y } @[simp] lemma pure_def (x : α) : pure x = principal {x} := rfl @[simp] lemma mem_pure {a : α} {s : set α} : a ∈ s → s ∈ (pure a : filter α).sets := by simp; exact id @[simp] lemma map_def {α β} (m : α → β) (f : filter α) : m <$> f = map m f := rfl @[simp] lemma bind_def {α β} (f : filter α) (m : α → filter β) : f >>= m = bind f m := rfl /- map and comap equations -/ section map variables {f f₁ f₂ : filter α} {g g₁ g₂ : filter β} {m : α → β} {m' : β → γ} {s : set α} {t : set β} @[simp] theorem mem_comap_sets : s ∈ (comap m g).sets ↔ ∃t∈g.sets, m ⁻¹' t ⊆ s := iff.rfl theorem preimage_mem_comap (ht : t ∈ g.sets) : m ⁻¹' t ∈ (comap m g).sets := ⟨t, ht, subset.refl _⟩ lemma comap_id : comap id f = f := le_antisymm (assume s, preimage_mem_comap) (assume s ⟨t, ht, hst⟩, mem_sets_of_superset ht hst) lemma comap_comap_comp {m : γ → β} {n : β → α} : comap m (comap n f) = comap (n ∘ m) f := le_antisymm (assume c ⟨b, hb, (h : preimage (n ∘ m) b ⊆ c)⟩, ⟨preimage n b, preimage_mem_comap hb, h⟩) (assume c ⟨b, ⟨a, ha, (h₁ : preimage n a ⊆ b)⟩, (h₂ : preimage m b ⊆ c)⟩, ⟨a, ha, show preimage m (preimage n a) ⊆ c, from subset.trans (preimage_mono h₁) h₂⟩) @[simp] theorem comap_principal {t : set β} : comap m (principal t) = principal (m ⁻¹' t) := filter_eq $ set.ext $ assume s, ⟨assume ⟨u, (hu : t ⊆ u), (b : preimage m u ⊆ s)⟩, subset.trans (preimage_mono hu) b, assume : preimage m t ⊆ s, ⟨t, subset.refl t, this⟩⟩ lemma map_le_iff_le_comap : map m f ≤ g ↔ f ≤ comap m g := ⟨assume h s ⟨t, ht, hts⟩, mem_sets_of_superset (h ht) hts, assume h s ht, h ⟨_, ht, subset.refl _⟩⟩ lemma gc_map_comap (m : α → β) : galois_connection (map m) (comap m) := assume f g, map_le_iff_le_comap lemma map_mono (h : f₁ ≤ f₂) : map m f₁ ≤ map m f₂ := (gc_map_comap m).monotone_l h lemma monotone_map : monotone (map m) | a b := map_mono lemma comap_mono (h : g₁ ≤ g₂) : comap m g₁ ≤ comap m g₂ := (gc_map_comap m).monotone_u h lemma monotone_comap : monotone (comap m) | a b := comap_mono @[simp] lemma map_bot : map m ⊥ = ⊥ := (gc_map_comap m).l_bot @[simp] lemma map_sup : map m (f₁ ⊔ f₂) = map m f₁ ⊔ map m f₂ := (gc_map_comap m).l_sup @[simp] lemma map_supr {f : ι → filter α} : map m (⨆i, f i) = (⨆i, map m (f i)) := (gc_map_comap m).l_supr @[simp] lemma comap_top : comap m ⊤ = ⊤ := (gc_map_comap m).u_top @[simp] lemma comap_inf : comap m (g₁ ⊓ g₂) = comap m g₁ ⊓ comap m g₂ := (gc_map_comap m).u_inf @[simp] lemma comap_infi {f : ι → filter β} : comap m (⨅i, f i) = (⨅i, comap m (f i)) := (gc_map_comap m).u_infi lemma map_comap_le : map m (comap m g) ≤ g := (gc_map_comap m).l_u_le _ lemma le_comap_map : f ≤ comap m (map m f) := (gc_map_comap m).le_u_l _ @[simp] lemma comap_bot : comap m ⊥ = ⊥ := bot_unique $ assume s _, ⟨∅, by simp, by simp⟩ lemma comap_supr {ι} {f : ι → filter β} {m : α → β} : comap m (supr f) = (⨆i, comap m (f i)) := le_antisymm (assume s hs, have ∀i, ∃t, t ∈ (f i).sets ∧ m ⁻¹' t ⊆ s, by simpa using mem_supr_sets.1 hs, let ⟨t, ht⟩ := classical.axiom_of_choice this in ⟨⋃i, t i, mem_supr_sets.2 $ assume i, (f i).sets_of_superset (ht i).1 (subset_Union _ _), begin rw [preimage_Union, Union_subset_iff], assume i, exact (ht i).2 end⟩) (supr_le $ assume i, monotone_comap $ le_supr _ _) lemma comap_Sup {s : set (filter β)} {m : α → β} : comap m (Sup s) = (⨆f∈s, comap m f) := by simp [Sup_eq_supr, comap_supr] lemma comap_sup : comap m (g₁ ⊔ g₂) = comap m g₁ ⊔ comap m g₂ := le_antisymm (assume s ⟨⟨t₁, ht₁, hs₁⟩, ⟨t₂, ht₂, hs₂⟩⟩, ⟨t₁ ∪ t₂, ⟨g₁.sets_of_superset ht₁ (subset_union_left _ _), g₂.sets_of_superset ht₂ (subset_union_right _ _)⟩, union_subset hs₁ hs₂⟩) (sup_le (comap_mono le_sup_left) (comap_mono le_sup_right)) lemma le_map_comap' {f : filter β} {m : α → β} {s : set β} (hs : s ∈ f.sets) (hm : ∀b∈s, ∃a, m a = b) : f ≤ map m (comap m f) := assume t' ⟨t, ht, (sub : m ⁻¹' t ⊆ m ⁻¹' t')⟩, by filter_upwards [ht, hs] assume x hxt hxs, let ⟨y, hy⟩ := hm x hxs in hy ▸ sub (show m y ∈ t, from hy.symm ▸ hxt) lemma le_map_comap {f : filter β} {m : α → β} (hm : ∀x, ∃y, m y = x) : f ≤ map m (comap m f) := le_map_comap' univ_mem_sets (assume b _, hm b) lemma comap_map {f : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) : comap m (map m f) = f := have ∀s, preimage m (image m s) = s, from assume s, preimage_image_eq s h, le_antisymm (assume s hs, ⟨ image m s, f.sets_of_superset hs $ by simp [this, subset.refl], by simp [this, subset.refl]⟩) le_comap_map lemma le_of_map_le_map_inj' {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f.sets) (hsg : s ∈ g.sets) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) (h : map m f ≤ map m g) : f ≤ g := assume t ht, by filter_upwards [hsf, h $ image_mem_map (inter_mem_sets hsg ht)] assume a has ⟨b, ⟨hbs, hb⟩, h⟩, have b = a, from hm _ hbs _ has h, this ▸ hb lemma le_of_map_le_map_inj_iff {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f.sets) (hsg : s ∈ g.sets) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) : map m f ≤ map m g ↔ f ≤ g := iff.intro (le_of_map_le_map_inj' hsf hsg hm) map_mono lemma eq_of_map_eq_map_inj' {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f.sets) (hsg : s ∈ g.sets) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) (h : map m f = map m g) : f = g := le_antisymm (le_of_map_le_map_inj' hsf hsg hm $ le_of_eq h) (le_of_map_le_map_inj' hsg hsf hm $ le_of_eq h.symm) lemma map_inj {f g : filter α} {m : α → β} (hm : ∀ x y, m x = m y → x = y) (h : map m f = map m g) : f = g := have comap m (map m f) = comap m (map m g), by rw h, by rwa [comap_map hm, comap_map hm] at this lemma comap_neq_bot {f : filter β} {m : α → β} (hm : ∀t∈f.sets, ∃a, m a ∈ t) : comap m f ≠ ⊥ := forall_sets_neq_empty_iff_neq_bot.mp $ assume s ⟨t, ht, t_s⟩, let ⟨a, (ha : a ∈ preimage m t)⟩ := hm t ht in neq_bot_of_le_neq_bot (ne_empty_of_mem ha) t_s lemma comap_neq_bot_of_surj {f : filter β} {m : α → β} (hf : f ≠ ⊥) (hm : ∀b, ∃a, m a = b) : comap m f ≠ ⊥ := comap_neq_bot $ assume t ht, let ⟨b, (hx : b ∈ t)⟩ := inhabited_of_mem_sets hf ht, ⟨a, (ha : m a = b)⟩ := hm b in ⟨a, ha.symm ▸ hx⟩ @[simp] lemma map_eq_bot_iff : map m f = ⊥ ↔ f = ⊥ := ⟨by rw [←empty_in_sets_eq_bot, ←empty_in_sets_eq_bot]; exact id, assume h, by simp [*]⟩ lemma map_ne_bot (hf : f ≠ ⊥) : map m f ≠ ⊥ := assume h, hf $ by rwa [map_eq_bot_iff] at h lemma sInter_comap_sets (f : α → β) (F : filter β) : ⋂₀(comap f F).sets = ⋂ U ∈ F.sets, f ⁻¹' U := begin ext x, suffices : (∀ (A : set α) (B : set β), B ∈ F.sets → f ⁻¹' B ⊆ A → x ∈ A) ↔ ∀ (B : set β), B ∈ F.sets → f x ∈ B, by simp [set.mem_sInter, set.mem_Inter, mem_comap_sets, this], split, { intros h U U_in, simpa [set.subset.refl] using h (f ⁻¹' U) U U_in }, { intros h V U U_in f_U_V, exact f_U_V (h U U_in) }, end end map lemma map_cong {m₁ m₂ : α → β} {f : filter α} (h : {x | m₁ x = m₂ x} ∈ f.sets) : map m₁ f = map m₂ f := have ∀(m₁ m₂ : α → β) (h : {x | m₁ x = m₂ x} ∈ f.sets), map m₁ f ≤ map m₂ f, begin intros m₁ m₂ h s hs, show {x | m₁ x ∈ s} ∈ f.sets, filter_upwards [h, hs], simp [subset_def] {contextual := tt} end, le_antisymm (this m₁ m₂ h) (this m₂ m₁ $ mem_sets_of_superset h $ assume x, eq.symm) -- this is a generic rule for monotone functions: lemma map_infi_le {f : ι → filter α} {m : α → β} : map m (infi f) ≤ (⨅ i, map m (f i)) := le_infi $ assume i, map_mono $ infi_le _ _ lemma map_infi_eq {f : ι → filter α} {m : α → β} (hf : directed (≥) f) (hι : nonempty ι) : map m (infi f) = (⨅ i, map m (f i)) := le_antisymm map_infi_le (assume s (hs : preimage m s ∈ (infi f).sets), have ∃i, preimage m s ∈ (f i).sets, by simp [infi_sets_eq hf hι] at hs; assumption, let ⟨i, hi⟩ := this in have (⨅ i, map m (f i)) ≤ principal s, from infi_le_of_le i $ by simp; assumption, by simp at this; assumption) lemma map_binfi_eq {ι : Type w} {f : ι → filter α} {m : α → β} {p : ι → Prop} (h : directed_on (f ⁻¹'o (≥)) {x | p x}) (ne : ∃i, p i) : map m (⨅i (h : p i), f i) = (⨅i (h: p i), map m (f i)) := let ⟨i, hi⟩ := ne in calc map m (⨅i (h : p i), f i) = map m (⨅i:subtype p, f i.val) : by simp [infi_subtype] ... = (⨅i:subtype p, map m (f i.val)) : map_infi_eq (assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end) ⟨⟨i, hi⟩⟩ ... = (⨅i (h : p i), map m (f i)) : by simp [infi_subtype] lemma map_inf' {f g : filter α} {m : α → β} {t : set α} (htf : t ∈ f.sets) (htg : t ∈ g.sets) (h : ∀x∈t, ∀y∈t, m x = m y → x = y) : map m (f ⊓ g) = map m f ⊓ map m g := begin refine le_antisymm (le_inf (map_mono inf_le_left) (map_mono inf_le_right)) (assume s hs, _), simp [map, mem_inf_sets] at hs ⊢, rcases hs with ⟨t₁, h₁, t₂, h₂, hs⟩, refine ⟨m '' (t₁ ∩ t), _, m '' (t₂ ∩ t), _, _⟩, { filter_upwards [h₁, htf] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ }, { filter_upwards [h₂, htg] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ }, { rw [image_inter_on], { refine image_subset_iff.2 _, exact λ x ⟨⟨h₁, _⟩, h₂, _⟩, hs ⟨h₁, h₂⟩ }, { exact λ x ⟨_, hx⟩ y ⟨_, hy⟩, h x hx y hy } } end lemma map_inf {f g : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) : map m (f ⊓ g) = map m f ⊓ map m g := map_inf' univ_mem_sets univ_mem_sets (assume x _ y _, h x y) lemma map_eq_comap_of_inverse {f : filter α} {m : α → β} {n : β → α} (h₁ : m ∘ n = id) (h₂ : n ∘ m = id) : map m f = comap n f := le_antisymm (assume b ⟨a, ha, (h : preimage n a ⊆ b)⟩, f.sets_of_superset ha $ calc a = preimage (n ∘ m) a : by simp [h₂, preimage_id] ... ⊆ preimage m b : preimage_mono h) (assume b (hb : preimage m b ∈ f.sets), ⟨preimage m b, hb, show preimage (m ∘ n) b ⊆ b, by simp [h₁]; apply subset.refl⟩) lemma map_swap_eq_comap_swap {f : filter (α × β)} : prod.swap <$> f = comap prod.swap f := map_eq_comap_of_inverse prod.swap_swap_eq prod.swap_swap_eq lemma le_map {f : filter α} {m : α → β} {g : filter β} (h : ∀s∈f.sets, m '' s ∈ g.sets) : g ≤ f.map m := assume s hs, mem_sets_of_superset (h _ hs) $ image_preimage_subset _ _ section applicative @[simp] lemma mem_pure_sets {a : α} {s : set α} : s ∈ (pure a : filter α).sets ↔ a ∈ s := by simp lemma singleton_mem_pure_sets {a : α} : {a} ∈ (pure a : filter α).sets := by simp @[simp] lemma pure_neq_bot {α : Type u} {a : α} : pure a ≠ (⊥ : filter α) := by simp [pure, has_pure.pure] lemma mem_seq_sets_def {f : filter (α → β)} {g : filter α} {s : set β} : s ∈ (f.seq g).sets ↔ (∃u∈f.sets, ∃t∈g.sets, ∀x∈u, ∀y∈t, (x : α → β) y ∈ s) := iff.refl _ lemma mem_seq_sets_iff {f : filter (α → β)} {g : filter α} {s : set β} : s ∈ (f.seq g).sets ↔ (∃u∈f.sets, ∃t∈g.sets, set.seq u t ⊆ s) := by simp [mem_seq_sets_def, set.seq_subset] lemma seq_mem_seq_sets {f : filter (α → β)} {g : filter α} {s : set (α → β)} {t : set α} (hs : s ∈ f.sets) (ht : t ∈ g.sets): s.seq t ∈ (f.seq g).sets := ⟨s, hs, t, ht, assume f hf a ha, ⟨f, hf, a, ha, rfl⟩⟩ lemma le_seq {f : filter (α → β)} {g : filter α} {h : filter β} (hh : ∀t∈f.sets, ∀u∈g.sets, set.seq t u ∈ h.sets) : h ≤ seq f g := assume s ⟨t, ht, u, hu, hs⟩, mem_sets_of_superset (hh _ ht _ hu) $ assume b ⟨m, hm, a, ha, eq⟩, eq ▸ hs _ hm _ ha lemma seq_mono {f₁ f₂ : filter (α → β)} {g₁ g₂ : filter α} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.seq g₁ ≤ f₂.seq g₂ := le_seq $ assume s hs t ht, seq_mem_seq_sets (hf hs) (hg ht) @[simp] lemma pure_seq_eq_map (g : α → β) (f : filter α) : seq (pure g) f = f.map g := begin refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _), { rw ← singleton_seq, apply seq_mem_seq_sets _ hs, simp }, { rw mem_pure_sets at hs, refine sets_of_superset (map g f) (image_mem_map ht) _, rintros b ⟨a, ha, rfl⟩, exact ⟨g, hs, a, ha, rfl⟩ } end @[simp] lemma map_pure (f : α → β) (a : α) : map f (pure a) = pure (f a) := le_antisymm (le_principal_iff.2 $ sets_of_superset (map f (pure a)) (image_mem_map singleton_mem_pure_sets) $ by simp) (le_map $ assume s, begin simp, exact assume has, ⟨a, has, rfl⟩ end) @[simp] lemma seq_pure (f : filter (α → β)) (a : α) : seq f (pure a) = map (λg:α → β, g a) f := begin refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _), { rw ← seq_singleton, exact seq_mem_seq_sets hs (by simp) }, { rw mem_pure_sets at ht, refine sets_of_superset (map (λg:α→β, g a) f) (image_mem_map hs) _, rintros b ⟨g, hg, rfl⟩, exact ⟨g, hg, a, ht, rfl⟩ } end @[simp] lemma seq_assoc (x : filter α) (g : filter (α → β)) (h : filter (β → γ)) : seq h (seq g x) = seq (seq (map (∘) h) g) x := begin refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _), { rcases mem_seq_sets_iff.1 hs with ⟨u, hu, v, hv, hs⟩, rcases mem_map_sets_iff.1 hu with ⟨w, hw, hu⟩, refine mem_sets_of_superset _ (set.seq_mono (subset.trans (set.seq_mono hu (subset.refl _)) hs) (subset.refl _)), rw ← set.seq_seq, exact seq_mem_seq_sets hw (seq_mem_seq_sets hv ht) }, { rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩, refine mem_sets_of_superset _ (set.seq_mono (subset.refl _) ht), rw set.seq_seq, exact seq_mem_seq_sets (seq_mem_seq_sets (image_mem_map hs) hu) hv } end lemma prod_map_seq_comm (f : filter α) (g : filter β) : (map prod.mk f).seq g = seq (map (λb a, (a, b)) g) f := begin refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _), { rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩, refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)), rw ← set.prod_image_seq_comm, exact seq_mem_seq_sets (image_mem_map ht) hu }, { rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩, refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)), rw set.prod_image_seq_comm, exact seq_mem_seq_sets (image_mem_map ht) hu } end instance : is_lawful_functor (filter : Type u → Type u) := { id_map := assume α f, map_id, comp_map := assume α β γ f g a, map_map.symm } instance : is_lawful_applicative (filter : Type u → Type u) := { pure_seq_eq_map := assume α β, pure_seq_eq_map, map_pure := assume α β, map_pure, seq_pure := assume α β, seq_pure, seq_assoc := assume α β γ, seq_assoc } instance : is_comm_applicative (filter : Type u → Type u) := ⟨assume α β f g, prod_map_seq_comm f g⟩ lemma {l} seq_eq_filter_seq {α β : Type l} (f : filter (α → β)) (g : filter α) : f <*> g = seq f g := rfl end applicative /- bind equations -/ section bind @[simp] lemma mem_bind_sets {s : set β} {f : filter α} {m : α → filter β} : s ∈ (bind f m).sets ↔ ∃t ∈ f.sets, ∀x ∈ t, s ∈ (m x).sets := calc s ∈ (bind f m).sets ↔ {a | s ∈ (m a).sets} ∈ f.sets : by simp [bind] ... ↔ (∃t ∈ f.sets, t ⊆ {a | s ∈ (m a).sets}) : exists_sets_subset_iff.symm ... ↔ (∃t ∈ f.sets, ∀x ∈ t, s ∈ (m x).sets) : iff.refl _ lemma bind_mono {f : filter α} {g h : α → filter β} (h₁ : {a | g a ≤ h a} ∈ f.sets) : bind f g ≤ bind f h := assume x h₂, show (_ ∈ f.sets), by filter_upwards [h₁, h₂] assume s gh' h', gh' h' lemma bind_sup {f g : filter α} {h : α → filter β} : bind (f ⊔ g) h = bind f h ⊔ bind g h := by simp [bind] lemma bind_mono2 {f g : filter α} {h : α → filter β} (h₁ : f ≤ g) : bind f h ≤ bind g h := assume s h', h₁ h' lemma principal_bind {s : set α} {f : α → filter β} : (bind (principal s) f) = (⨆x ∈ s, f x) := show join (map f (principal s)) = (⨆x ∈ s, f x), by simp [Sup_image] end bind lemma infi_neq_bot_of_directed {f : ι → filter α} (hn : nonempty α) (hd : directed (≥) f) (hb : ∀i, f i ≠ ⊥): (infi f) ≠ ⊥ := let ⟨x⟩ := hn in assume h, have he: ∅ ∈ (infi f).sets, from h.symm ▸ mem_bot_sets, classical.by_cases (assume : nonempty ι, have ∃i, ∅ ∈ (f i).sets, by rw [infi_sets_eq hd this] at he; simp at he; assumption, let ⟨i, hi⟩ := this in hb i $ bot_unique $ assume s _, (f i).sets_of_superset hi $ empty_subset _) (assume : ¬ nonempty ι, have univ ⊆ (∅ : set α), begin rw [←principal_mono, principal_univ, principal_empty, ←h], exact (le_infi $ assume i, false.elim $ this ⟨i⟩) end, this $ mem_univ x) lemma infi_neq_bot_iff_of_directed {f : ι → filter α} (hn : nonempty α) (hd : directed (≥) f) : (infi f) ≠ ⊥ ↔ (∀i, f i ≠ ⊥) := ⟨assume neq_bot i eq_bot, neq_bot $ bot_unique $ infi_le_of_le i $ eq_bot ▸ le_refl _, infi_neq_bot_of_directed hn hd⟩ lemma mem_infi_sets {f : ι → filter α} (i : ι) : ∀{s}, s ∈ (f i).sets → s ∈ (⨅i, f i).sets := show (⨅i, f i) ≤ f i, from infi_le _ _ @[elab_as_eliminator] lemma infi_sets_induct {f : ι → filter α} {s : set α} (hs : s ∈ (infi f).sets) {p : set α → Prop} (uni : p univ) (ins : ∀{i s₁ s₂}, s₁ ∈ (f i).sets → p s₂ → p (s₁ ∩ s₂)) (upw : ∀{s₁ s₂}, s₁ ⊆ s₂ → p s₁ → p s₂) : p s := begin have hs' : s ∈ (Inf {a : filter α | ∃ (i : ι), a = f i}).sets := hs, rw [Inf_sets_eq_finite] at hs', simp only [mem_Union] at hs', rcases hs' with ⟨is, ⟨fin_is, his⟩, hs⟩, revert his s, refine finite.induction_on fin_is _ (λ fi is fi_ne_is fin_is ih, _); intros his s hs' hs, { rw [Inf_empty, mem_top_sets] at hs, simpa [hs] }, { rw [Inf_insert] at hs, rcases hs with ⟨s₁, hs₁, s₂, hs₂, hs⟩, rcases (his (mem_insert _ _) : ∃i, fi = f i) with ⟨i, rfl⟩, have hs₂ : p s₂, from have his : is ⊆ {x | ∃i, x = f i}, from assume i hi, his $ mem_insert_of_mem _ hi, have infi f ≤ Inf is, from Inf_le_Inf his, ih his (this hs₂) hs₂, exact upw hs (ins hs₁ hs₂) } end /- tendsto -/ /-- `tendsto` is the generic "limit of a function" predicate. `tendsto f l₁ l₂` asserts that for every `l₂` neighborhood `a`, the `f`-preimage of `a` is an `l₁` neighborhood. -/ def tendsto (f : α → β) (l₁ : filter α) (l₂ : filter β) := l₁.map f ≤ l₂ lemma tendsto_def {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ ∀ s ∈ l₂.sets, f ⁻¹' s ∈ l₁.sets := iff.rfl lemma tendsto_iff_comap {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ l₁ ≤ l₂.comap f := map_le_iff_le_comap lemma tendsto_cong {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (h : tendsto f₁ l₁ l₂) (hl : {x | f₁ x = f₂ x} ∈ l₁.sets) : tendsto f₂ l₁ l₂ := by rwa [tendsto, ←map_cong hl] lemma tendsto_id' {x y : filter α} : x ≤ y → tendsto id x y := by simp [tendsto] { contextual := tt } lemma tendsto_id {x : filter α} : tendsto id x x := tendsto_id' $ le_refl x lemma tendsto.comp {f : α → β} {g : β → γ} {x : filter α} {y : filter β} {z : filter γ} (hf : tendsto f x y) (hg : tendsto g y z) : tendsto (g ∘ f) x z := calc map (g ∘ f) x = map g (map f x) : by rw [map_map] ... ≤ map g y : map_mono hf ... ≤ z : hg lemma tendsto_le_left {f : α → β} {x y : filter α} {z : filter β} (h : y ≤ x) : tendsto f x z → tendsto f y z := le_trans (map_mono h) lemma tendsto_le_right {f : α → β} {x : filter α} {y z : filter β} (h₁ : y ≤ z) (h₂ : tendsto f x y) : tendsto f x z := le_trans h₂ h₁ lemma tendsto_map {f : α → β} {x : filter α} : tendsto f x (map f x) := le_refl (map f x) lemma tendsto_map' {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} (h : tendsto (f ∘ g) x y) : tendsto f (map g x) y := by rwa [tendsto, map_map] lemma tendsto_map'_iff {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} : tendsto f (map g x) y ↔ tendsto (f ∘ g) x y := by rw [tendsto, map_map]; refl lemma tendsto_comap {f : α → β} {x : filter β} : tendsto f (comap f x) x := map_comap_le lemma tendsto_comap_iff {f : α → β} {g : β → γ} {a : filter α} {c : filter γ} : tendsto f a (c.comap g) ↔ tendsto (g ∘ f) a c := ⟨assume h, h.comp tendsto_comap, assume h, map_le_iff_le_comap.mp $ by rwa [map_map]⟩ lemma tendsto_comap'' {m : α → β} {f : filter α} {g : filter β} (s : set α) {i : γ → α} (hs : s ∈ f.sets) (hi : ∀a∈s, ∃c, i c = a) (h : tendsto (m ∘ i) (comap i f) g) : tendsto m f g := have tendsto m (map i $ comap i $ f) g, by rwa [tendsto, ←map_compose] at h, le_trans (map_mono $ le_map_comap' hs hi) this lemma comap_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} {ψ : β → α} (inv₁ : φ ∘ ψ = id) (inv₂ : ψ ∘ φ = id) (lim₁ : tendsto φ f g) (lim₂ : tendsto ψ g f) : comap φ g = f := begin have ineq₁ := calc comap φ g = map ψ g : eq.symm (map_eq_comap_of_inverse inv₂ inv₁) ... ≤ f : lim₂, have ineq₂ : f ≤ comap φ g := map_le_iff_le_comap.1 lim₁, exact le_antisymm ineq₁ ineq₂ end lemma tendsto_inf {f : α → β} {x : filter α} {y₁ y₂ : filter β} : tendsto f x (y₁ ⊓ y₂) ↔ tendsto f x y₁ ∧ tendsto f x y₂ := by simp [tendsto] lemma tendsto_inf_left {f : α → β} {x₁ x₂ : filter α} {y : filter β} (h : tendsto f x₁ y) : tendsto f (x₁ ⊓ x₂) y := le_trans (map_mono inf_le_left) h lemma tendsto_inf_right {f : α → β} {x₁ x₂ : filter α} {y : filter β} (h : tendsto f x₂ y) : tendsto f (x₁ ⊓ x₂) y := le_trans (map_mono inf_le_right) h lemma tendsto_infi {f : α → β} {x : filter α} {y : ι → filter β} : tendsto f x (⨅i, y i) ↔ ∀i, tendsto f x (y i) := by simp [tendsto] lemma tendsto_infi' {f : α → β} {x : ι → filter α} {y : filter β} (i : ι) : tendsto f (x i) y → tendsto f (⨅i, x i) y := tendsto_le_left (infi_le _ _) lemma tendsto_principal {f : α → β} {a : filter α} {s : set β} : tendsto f a (principal s) ↔ {a | f a ∈ s} ∈ a.sets := by simp [tendsto] lemma tendsto_principal_principal {f : α → β} {s : set α} {t : set β} : tendsto f (principal s) (principal t) ↔ ∀a∈s, f a ∈ t := by simp [tendsto, image_subset_iff]; refl section lift /-- A variant on `bind` using a function `g` taking a set instead of a member of `α`. -/ protected def lift (f : filter α) (g : set α → filter β) := ⨅s ∈ f.sets, g s variables {f f₁ f₂ : filter α} {g g₁ g₂ : set α → filter β} lemma lift_sets_eq (hg : monotone g) : (f.lift g).sets = (⋃t∈f.sets, (g t).sets) := infi_sets_eq' (assume s hs t ht, ⟨s ∩ t, inter_mem_sets hs ht, hg $ inter_subset_left s t, hg $ inter_subset_right s t⟩) ⟨univ, univ_mem_sets⟩ lemma mem_lift {s : set β} {t : set α} (ht : t ∈ f.sets) (hs : s ∈ (g t).sets) : s ∈ (f.lift g).sets := le_principal_iff.mp $ show f.lift g ≤ principal s, from infi_le_of_le t $ infi_le_of_le ht $ le_principal_iff.mpr hs lemma mem_lift_sets (hg : monotone g) {s : set β} : s ∈ (f.lift g).sets ↔ (∃t∈f.sets, s ∈ (g t).sets) := by rw [lift_sets_eq hg]; simp only [mem_Union] lemma lift_le {f : filter α} {g : set α → filter β} {h : filter β} {s : set α} (hs : s ∈ f.sets) (hg : g s ≤ h) : f.lift g ≤ h := infi_le_of_le s $ infi_le_of_le hs $ hg lemma le_lift {f : filter α} {g : set α → filter β} {h : filter β} (hh : ∀s∈f.sets, h ≤ g s) : h ≤ f.lift g := le_infi $ assume s, le_infi $ assume hs, hh s hs lemma lift_mono (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.lift g₁ ≤ f₂.lift g₂ := infi_le_infi $ assume s, infi_le_infi2 $ assume hs, ⟨hf hs, hg s⟩ lemma lift_mono' (hg : ∀s∈f.sets, g₁ s ≤ g₂ s) : f.lift g₁ ≤ f.lift g₂ := infi_le_infi $ assume s, infi_le_infi $ assume hs, hg s hs lemma map_lift_eq {m : β → γ} (hg : monotone g) : map m (f.lift g) = f.lift (map m ∘ g) := have monotone (map m ∘ g), from monotone_comp hg monotone_map, filter_eq $ set.ext $ by simp [mem_lift_sets, hg, @mem_lift_sets _ _ f _ this] lemma comap_lift_eq {m : γ → β} (hg : monotone g) : comap m (f.lift g) = f.lift (comap m ∘ g) := have monotone (comap m ∘ g), from monotone_comp hg monotone_comap, filter_eq $ set.ext begin simp only [hg, @mem_lift_sets _ _ f _ this, comap, mem_lift_sets, mem_set_of_eq, exists_prop, function.comp_apply], exact λ s, ⟨λ ⟨b, ⟨a, ha, hb⟩, hs⟩, ⟨a, ha, b, hb, hs⟩, λ ⟨a, ha, b, hb, hs⟩, ⟨b, ⟨a, ha, hb⟩, hs⟩⟩ end theorem comap_lift_eq2 {m : β → α} {g : set β → filter γ} (hg : monotone g) : (comap m f).lift g = f.lift (g ∘ preimage m) := le_antisymm (le_infi $ assume s, le_infi $ assume hs, infi_le_of_le (preimage m s) $ infi_le _ ⟨s, hs, subset.refl _⟩) (le_infi $ assume s, le_infi $ assume ⟨s', hs', (h_sub : preimage m s' ⊆ s)⟩, infi_le_of_le s' $ infi_le_of_le hs' $ hg h_sub) lemma map_lift_eq2 {g : set β → filter γ} {m : α → β} (hg : monotone g) : (map m f).lift g = f.lift (g ∘ image m) := le_antisymm (infi_le_infi2 $ assume s, ⟨image m s, infi_le_infi2 $ assume hs, ⟨ f.sets_of_superset hs $ assume a h, mem_image_of_mem _ h, le_refl _⟩⟩) (infi_le_infi2 $ assume t, ⟨preimage m t, infi_le_infi2 $ assume ht, ⟨ht, hg $ assume x, assume h : x ∈ m '' preimage m t, let ⟨y, hy, h_eq⟩ := h in show x ∈ t, from h_eq ▸ hy⟩⟩) lemma lift_comm {g : filter β} {h : set α → set β → filter γ} : f.lift (λs, g.lift (h s)) = g.lift (λt, f.lift (λs, h s t)) := le_antisymm (le_infi $ assume i, le_infi $ assume hi, le_infi $ assume j, le_infi $ assume hj, infi_le_of_le j $ infi_le_of_le hj $ infi_le_of_le i $ infi_le _ hi) (le_infi $ assume i, le_infi $ assume hi, le_infi $ assume j, le_infi $ assume hj, infi_le_of_le j $ infi_le_of_le hj $ infi_le_of_le i $ infi_le _ hi) lemma lift_assoc {h : set β → filter γ} (hg : monotone g) : (f.lift g).lift h = f.lift (λs, (g s).lift h) := le_antisymm (le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, infi_le_of_le t $ infi_le _ $ (mem_lift_sets hg).mpr ⟨_, hs, ht⟩) (le_infi $ assume t, le_infi $ assume ht, let ⟨s, hs, h'⟩ := (mem_lift_sets hg).mp ht in infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le t $ infi_le _ h') lemma lift_lift_same_le_lift {g : set α → set α → filter β} : f.lift (λs, f.lift (g s)) ≤ f.lift (λs, g s s) := le_infi $ assume s, le_infi $ assume hs, infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le s $ infi_le _ hs lemma lift_lift_same_eq_lift {g : set α → set α → filter β} (hg₁ : ∀s, monotone (λt, g s t)) (hg₂ : ∀t, monotone (λs, g s t)): f.lift (λs, f.lift (g s)) = f.lift (λs, g s s) := le_antisymm lift_lift_same_le_lift (le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, infi_le_of_le (s ∩ t) $ infi_le_of_le (inter_mem_sets hs ht) $ calc g (s ∩ t) (s ∩ t) ≤ g s (s ∩ t) : hg₂ (s ∩ t) (inter_subset_left _ _) ... ≤ g s t : hg₁ s (inter_subset_right _ _)) lemma lift_principal {s : set α} (hg : monotone g) : (principal s).lift g = g s := le_antisymm (infi_le_of_le s $ infi_le _ $ subset.refl _) (le_infi $ assume t, le_infi $ assume hi, hg hi) theorem monotone_lift [preorder γ] {f : γ → filter α} {g : γ → set α → filter β} (hf : monotone f) (hg : monotone g) : monotone (λc, (f c).lift (g c)) := assume a b h, lift_mono (hf h) (hg h) lemma lift_neq_bot_iff (hm : monotone g) : (f.lift g ≠ ⊥) ↔ (∀s∈f.sets, g s ≠ ⊥) := classical.by_cases (assume hn : nonempty β, calc f.lift g ≠ ⊥ ↔ (⨅s : { s // s ∈ f.sets}, g s.val) ≠ ⊥ : by simp [filter.lift, infi_subtype] ... ↔ (∀s:{ s // s ∈ f.sets}, g s.val ≠ ⊥) : infi_neq_bot_iff_of_directed hn (assume ⟨a, ha⟩ ⟨b, hb⟩, ⟨⟨a ∩ b, inter_mem_sets ha hb⟩, hm $ inter_subset_left _ _, hm $ inter_subset_right _ _⟩) ... ↔ (∀s∈f.sets, g s ≠ ⊥) : ⟨assume h s hs, h ⟨s, hs⟩, assume h ⟨s, hs⟩, h s hs⟩) (assume hn : ¬ nonempty β, have h₁ : f.lift g = ⊥, from filter_eq_bot_of_not_nonempty hn, have h₂ : ∀s, g s = ⊥, from assume s, filter_eq_bot_of_not_nonempty hn, calc (f.lift g ≠ ⊥) ↔ false : by simp [h₁] ... ↔ (∀s∈f.sets, false) : ⟨false.elim, assume h, h univ univ_mem_sets⟩ ... ↔ (∀s∈f.sets, g s ≠ ⊥) : by simp [h₂]) @[simp] lemma lift_const {f : filter α} {g : filter β} : f.lift (λx, g) = g := le_antisymm (lift_le univ_mem_sets $ le_refl g) (le_lift $ assume s hs, le_refl g) @[simp] lemma lift_inf {f : filter α} {g h : set α → filter β} : f.lift (λx, g x ⊓ h x) = f.lift g ⊓ f.lift h := by simp [filter.lift, infi_inf_eq] @[simp] lemma lift_principal2 {f : filter α} : f.lift principal = f := le_antisymm (assume s hs, mem_lift hs (mem_principal_self s)) (le_infi $ assume s, le_infi $ assume hs, by simp [hs]) lemma lift_infi {f : ι → filter α} {g : set α → filter β} (hι : nonempty ι) (hg : ∀{s t}, g s ⊓ g t = g (s ∩ t)) : (infi f).lift g = (⨅i, (f i).lift g) := le_antisymm (le_infi $ assume i, lift_mono (infi_le _ _) (le_refl _)) (assume s, have g_mono : monotone g, from assume s t h, le_of_inf_eq $ eq.trans hg $ congr_arg g $ inter_eq_self_of_subset_left h, have ∀t∈(infi f).sets, (⨅ (i : ι), filter.lift (f i) g) ≤ g t, from assume t ht, infi_sets_induct ht (let ⟨i⟩ := hι in infi_le_of_le i $ infi_le_of_le univ $ infi_le _ univ_mem_sets) (assume i s₁ s₂ hs₁ hs₂, @hg s₁ s₂ ▸ le_inf (infi_le_of_le i $ infi_le_of_le s₁ $ infi_le _ hs₁) hs₂) (assume s₁ s₂ hs₁ hs₂, le_trans hs₂ $ g_mono hs₁), begin rw [lift_sets_eq g_mono], simp only [mem_Union, exists_imp_distrib], exact assume t ht hs, this t ht hs end) end lift section lift' /-- Specialize `lift` to functions `set α → set β`. This can be viewed as a generalization of `comap`. -/ protected def lift' (f : filter α) (h : set α → set β) := f.lift (principal ∘ h) variables {f f₁ f₂ : filter α} {h h₁ h₂ : set α → set β} lemma mem_lift' {t : set α} (ht : t ∈ f.sets) : h t ∈ (f.lift' h).sets := le_principal_iff.mp $ show f.lift' h ≤ principal (h t), from infi_le_of_le t $ infi_le_of_le ht $ le_refl _ lemma mem_lift'_sets (hh : monotone h) {s : set β} : s ∈ (f.lift' h).sets ↔ (∃t∈f.sets, h t ⊆ s) := have monotone (principal ∘ h), from assume a b h, principal_mono.mpr $ hh h, by simp [filter.lift', @mem_lift_sets α β f _ this] lemma lift'_le {f : filter α} {g : set α → set β} {h : filter β} {s : set α} (hs : s ∈ f.sets) (hg : principal (g s) ≤ h) : f.lift' g ≤ h := lift_le hs hg lemma lift'_mono (hf : f₁ ≤ f₂) (hh : h₁ ≤ h₂) : f₁.lift' h₁ ≤ f₂.lift' h₂ := lift_mono hf $ assume s, principal_mono.mpr $ hh s lemma lift'_mono' (hh : ∀s∈f.sets, h₁ s ⊆ h₂ s) : f.lift' h₁ ≤ f.lift' h₂ := infi_le_infi $ assume s, infi_le_infi $ assume hs, principal_mono.mpr $ hh s hs lemma lift'_cong (hh : ∀s∈f.sets, h₁ s = h₂ s) : f.lift' h₁ = f.lift' h₂ := le_antisymm (lift'_mono' $ assume s hs, le_of_eq $ hh s hs) (lift'_mono' $ assume s hs, le_of_eq $ (hh s hs).symm) lemma map_lift'_eq {m : β → γ} (hh : monotone h) : map m (f.lift' h) = f.lift' (image m ∘ h) := calc map m (f.lift' h) = f.lift (map m ∘ principal ∘ h) : map_lift_eq $ monotone_comp hh monotone_principal ... = f.lift' (image m ∘ h) : by simp [function.comp, filter.lift'] lemma map_lift'_eq2 {g : set β → set γ} {m : α → β} (hg : monotone g) : (map m f).lift' g = f.lift' (g ∘ image m) := map_lift_eq2 $ monotone_comp hg monotone_principal theorem comap_lift'_eq {m : γ → β} (hh : monotone h) : comap m (f.lift' h) = f.lift' (preimage m ∘ h) := calc comap m (f.lift' h) = f.lift (comap m ∘ principal ∘ h) : comap_lift_eq $ monotone_comp hh monotone_principal ... = f.lift' (preimage m ∘ h) : by simp [function.comp, filter.lift'] theorem comap_lift'_eq2 {m : β → α} {g : set β → set γ} (hg : monotone g) : (comap m f).lift' g = f.lift' (g ∘ preimage m) := comap_lift_eq2 $ monotone_comp hg monotone_principal lemma lift'_principal {s : set α} (hh : monotone h) : (principal s).lift' h = principal (h s) := lift_principal $ monotone_comp hh monotone_principal lemma principal_le_lift' {t : set β} (hh : ∀s∈f.sets, t ⊆ h s) : principal t ≤ f.lift' h := le_infi $ assume s, le_infi $ assume hs, principal_mono.mpr (hh s hs) theorem monotone_lift' [preorder γ] {f : γ → filter α} {g : γ → set α → set β} (hf : monotone f) (hg : monotone g) : monotone (λc, (f c).lift' (g c)) := assume a b h, lift'_mono (hf h) (hg h) lemma lift_lift'_assoc {g : set α → set β} {h : set β → filter γ} (hg : monotone g) (hh : monotone h) : (f.lift' g).lift h = f.lift (λs, h (g s)) := calc (f.lift' g).lift h = f.lift (λs, (principal (g s)).lift h) : lift_assoc (monotone_comp hg monotone_principal) ... = f.lift (λs, h (g s)) : by simp [lift_principal, hh] lemma lift'_lift'_assoc {g : set α → set β} {h : set β → set γ} (hg : monotone g) (hh : monotone h) : (f.lift' g).lift' h = f.lift' (λs, h (g s)) := lift_lift'_assoc hg (monotone_comp hh monotone_principal) lemma lift'_lift_assoc {g : set α → filter β} {h : set β → set γ} (hg : monotone g) : (f.lift g).lift' h = f.lift (λs, (g s).lift' h) := lift_assoc hg lemma lift_lift'_same_le_lift' {g : set α → set α → set β} : f.lift (λs, f.lift' (g s)) ≤ f.lift' (λs, g s s) := lift_lift_same_le_lift lemma lift_lift'_same_eq_lift' {g : set α → set α → set β} (hg₁ : ∀s, monotone (λt, g s t)) (hg₂ : ∀t, monotone (λs, g s t)): f.lift (λs, f.lift' (g s)) = f.lift' (λs, g s s) := lift_lift_same_eq_lift (assume s, monotone_comp monotone_id $ monotone_comp (hg₁ s) monotone_principal) (assume t, monotone_comp (hg₂ t) monotone_principal) lemma lift'_inf_principal_eq {h : set α → set β} {s : set β} : f.lift' h ⊓ principal s = f.lift' (λt, h t ∩ s) := le_antisymm (le_infi $ assume t, le_infi $ assume ht, calc filter.lift' f h ⊓ principal s ≤ principal (h t) ⊓ principal s : inf_le_inf (infi_le_of_le t $ infi_le _ ht) (le_refl _) ... = _ : by simp) (le_inf (le_infi $ assume t, le_infi $ assume ht, infi_le_of_le t $ infi_le_of_le ht $ by simp; exact inter_subset_right _ _) (infi_le_of_le univ $ infi_le_of_le univ_mem_sets $ by simp; exact inter_subset_left _ _)) lemma lift'_neq_bot_iff (hh : monotone h) : (f.lift' h ≠ ⊥) ↔ (∀s∈f.sets, h s ≠ ∅) := calc (f.lift' h ≠ ⊥) ↔ (∀s∈f.sets, principal (h s) ≠ ⊥) : lift_neq_bot_iff (monotone_comp hh monotone_principal) ... ↔ (∀s∈f.sets, h s ≠ ∅) : by simp [principal_eq_bot_iff] @[simp] lemma lift'_id {f : filter α} : f.lift' id = f := lift_principal2 lemma le_lift' {f : filter α} {h : set α → set β} {g : filter β} (h_le : ∀s∈f.sets, h s ∈ g.sets) : g ≤ f.lift' h := le_infi $ assume s, le_infi $ assume hs, by simp [h_le]; exact h_le s hs lemma lift_infi' {f : ι → filter α} {g : set α → filter β} (hι : nonempty ι) (hf : directed (≥) f) (hg : monotone g) : (infi f).lift g = (⨅i, (f i).lift g) := le_antisymm (le_infi $ assume i, lift_mono (infi_le _ _) (le_refl _)) (assume s, begin rw [lift_sets_eq hg], simp only [mem_Union, exists_imp_distrib, infi_sets_eq hf hι], exact assume t i ht hs, mem_infi_sets i $ mem_lift ht hs end) lemma lift'_infi {f : ι → filter α} {g : set α → set β} (hι : nonempty ι) (hg : ∀{s t}, g s ∩ g t = g (s ∩ t)) : (infi f).lift' g = (⨅i, (f i).lift' g) := lift_infi hι $ by simp; apply assume s t, hg theorem comap_eq_lift' {f : filter β} {m : α → β} : comap m f = f.lift' (preimage m) := filter_eq $ set.ext $ by simp [mem_lift'_sets, monotone_preimage, comap] end lift' section prod variables {s : set α} {t : set β} {f : filter α} {g : filter β} /- The product filter cannot be defined using the monad structure on filters. For example: F := do {x <- seq, y <- top, return (x, y)} hence: s ∈ F <-> ∃n, [n..∞] × univ ⊆ s G := do {y <- top, x <- seq, return (x, y)} hence: s ∈ G <-> ∀i:ℕ, ∃n, [n..∞] × {i} ⊆ s Now ⋃i, [i..∞] × {i} is in G but not in F. As product filter we want to have F as result. -/ /-- Product of filters. This is the filter generated by cartesian products of elements of the component filters. -/ protected def prod (f : filter α) (g : filter β) : filter (α × β) := f.comap prod.fst ⊓ g.comap prod.snd lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β} (hs : s ∈ f.sets) (ht : t ∈ g.sets) : set.prod s t ∈ (filter.prod f g).sets := inter_mem_inf_sets (preimage_mem_comap hs) (preimage_mem_comap ht) lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} : s ∈ (filter.prod f g).sets ↔ (∃t₁∈f.sets, ∃t₂∈g.sets, set.prod t₁ t₂ ⊆ s) := begin simp [filter.prod], split, exact assume ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, h⟩, ⟨s₁, hs₁, s₂, hs₂, subset.trans (inter_subset_inter hts₁ hts₂) h⟩, exact assume ⟨t₁, ht₁, t₂, ht₂, h⟩, ⟨prod.fst ⁻¹' t₁, ⟨t₁, ht₁, subset.refl _⟩, prod.snd ⁻¹' t₂, ⟨t₂, ht₂, subset.refl _⟩, h⟩ end lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (filter.prod f g) f := tendsto_inf_left tendsto_comap lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (filter.prod f g) g := tendsto_inf_right tendsto_comap lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ} (h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λx, (m₁ x, m₂ x)) f (filter.prod g h) := tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩ lemma prod_infi_left {f : ι → filter α} {g : filter β} (i : ι) : filter.prod (⨅i, f i) g = (⨅i, filter.prod (f i) g) := by rw [filter.prod, comap_infi, infi_inf i]; simp [filter.prod] lemma prod_infi_right {f : filter α} {g : ι → filter β} (i : ι) : filter.prod f (⨅i, g i) = (⨅i, filter.prod f (g i)) := by rw [filter.prod, comap_infi, inf_infi i]; simp [filter.prod] lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : filter.prod f₁ g₁ ≤ filter.prod f₂ g₂ := inf_le_inf (comap_mono hf) (comap_mono hg) lemma prod_comap_comap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} : filter.prod (comap m₁ f₁) (comap m₂ f₂) = comap (λp:β₁×β₂, (m₁ p.1, m₂ p.2)) (filter.prod f₁ f₂) := by simp [filter.prod, comap_comap_comp] lemma prod_comm' : filter.prod f g = comap (prod.swap) (filter.prod g f) := by simp [filter.prod, comap_comap_comp, function.comp, inf_comm] lemma prod_comm : filter.prod f g = map (λp:β×α, (p.2, p.1)) (filter.prod g f) := by rw [prod_comm', ← map_swap_eq_comap_swap]; refl lemma prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} : filter.prod (map m₁ f₁) (map m₂ f₂) = map (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) (filter.prod f₁ f₂) := le_antisymm (assume s hs, let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in filter.sets_of_superset _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $ calc set.prod (m₁ '' s₁) (m₂ '' s₂) = (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) '' set.prod s₁ s₂ : set.prod_image_image_eq ... ⊆ _ : by rwa [image_subset_iff]) ((tendsto_fst.comp (le_refl _)).prod_mk (tendsto_snd.comp (le_refl _))) lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} : filter.prod f₁ g₁ ⊓ filter.prod f₂ g₂ = filter.prod (f₁ ⊓ f₂) (g₁ ⊓ g₂) := by simp only [filter.prod, comap_inf, inf_comm, inf_assoc, lattice.inf_left_comm] @[simp] lemma prod_bot1 {f : filter α} : filter.prod f (⊥ : filter β) = ⊥ := by simp [filter.prod] @[simp] lemma prod_bot2 {g : filter β} : filter.prod (⊥ : filter α) g = ⊥ := by simp [filter.prod] @[simp] lemma prod_principal_principal {s : set α} {t : set β} : filter.prod (principal s) (principal t) = principal (set.prod s t) := by simp [filter.prod, comap_principal]; refl lemma prod_def {f : filter α} {g : filter β} : f.prod g = (f.lift $ λs, g.lift' $ set.prod s) := have ∀(s:set α) (t : set β), principal (set.prod s t) = (principal s).comap prod.fst ⊓ (principal t).comap prod.snd, by simp; intros; refl, begin simp [filter.lift', function.comp, this, -comap_principal, lift_inf], rw [← comap_lift_eq monotone_principal, ← comap_lift_eq monotone_principal], simp [filter.prod] end lemma prod_same_eq : filter.prod f f = f.lift' (λt, set.prod t t) := by rw [prod_def]; from lift_lift'_same_eq_lift' (assume s, set.monotone_prod monotone_const monotone_id) (assume t, set.monotone_prod monotone_id monotone_const) lemma mem_prod_same_iff {s : set (α×α)} : s ∈ (filter.prod f f).sets ↔ (∃t∈f.sets, set.prod t t ⊆ s) := by rw [prod_same_eq, mem_lift'_sets]; exact set.monotone_prod monotone_id monotone_id lemma prod_lift_lift {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → filter β₁} {g₂ : set α₂ → filter β₂} (hg₁ : monotone g₁) (hg₂ : monotone g₂) : filter.prod (f₁.lift g₁) (f₂.lift g₂) = f₁.lift (λs, f₂.lift (λt, filter.prod (g₁ s) (g₂ t))) := begin simp only [prod_def], rw [lift_assoc], apply congr_arg, funext x, rw [lift_comm], apply congr_arg, funext y, rw [lift'_lift_assoc], exact hg₂, exact hg₁ end lemma prod_lift'_lift' {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → set β₁} {g₂ : set α₂ → set β₂} (hg₁ : monotone g₁) (hg₂ : monotone g₂) : filter.prod (f₁.lift' g₁) (f₂.lift' g₂) = f₁.lift (λs, f₂.lift' (λt, set.prod (g₁ s) (g₂ t))) := begin rw [prod_def, lift_lift'_assoc], apply congr_arg, funext x, rw [lift'_lift'_assoc], exact hg₂, exact set.monotone_prod monotone_const monotone_id, exact hg₁, exact (monotone_lift' monotone_const $ monotone_lam $ assume x, set.monotone_prod monotone_id monotone_const) end lemma prod_neq_bot {f : filter α} {g : filter β} : filter.prod f g ≠ ⊥ ↔ (f ≠ ⊥ ∧ g ≠ ⊥) := calc filter.prod f g ≠ ⊥ ↔ (∀s∈f.sets, g.lift' (set.prod s) ≠ ⊥) : begin rw [prod_def, lift_neq_bot_iff], exact (monotone_lift' monotone_const $ monotone_lam $ assume s, set.monotone_prod monotone_id monotone_const) end ... ↔ (∀s∈f.sets, ∀t∈g.sets, s ≠ ∅ ∧ t ≠ ∅) : begin apply forall_congr, intro s, apply forall_congr, intro hs, rw [lift'_neq_bot_iff], apply forall_congr, intro t, apply forall_congr, intro ht, rw [set.prod_neq_empty_iff], exact set.monotone_prod monotone_const monotone_id end ... ↔ (∀s∈f.sets, s ≠ ∅) ∧ (∀t∈g.sets, t ≠ ∅) : ⟨assume h, ⟨assume s hs, (h s hs univ univ_mem_sets).left, assume t ht, (h univ univ_mem_sets t ht).right⟩, assume ⟨h₁, h₂⟩ s hs t ht, ⟨h₁ s hs, h₂ t ht⟩⟩ ... ↔ _ : by simp only [forall_sets_neq_empty_iff_neq_bot] lemma tendsto_prod_iff {f : α × β → γ} {x : filter α} {y : filter β} {z : filter γ} : filter.tendsto f (filter.prod x y) z ↔ ∀ W ∈ z.sets, ∃ U ∈ x.sets, ∃ V ∈ y.sets, ∀ x y, x ∈ U → y ∈ V → f (x, y) ∈ W := by simp [tendsto_def, mem_prod_iff, set.prod_sub_preimage_iff] lemma tendsto_prod_self_iff {f : α × α → β} {x : filter α} {y : filter β} : filter.tendsto f (filter.prod x x) y ↔ ∀ W ∈ y.sets, ∃ U ∈ x.sets, ∀ (x x' : α), x ∈ U → x' ∈ U → f (x, x') ∈ W := by simp [tendsto_def, mem_prod_same_iff, set.prod_sub_preimage_iff] end prod /- at_top and at_bot -/ /-- `at_top` is the filter representing the limit `→ ∞` on an ordered set. It is generated by the collection of up-sets `{b | a ≤ b}`. (The preorder need not have a top element for this to be well defined, and indeed is trivial when a top element exists.) -/ def at_top [preorder α] : filter α := ⨅ a, principal {b | a ≤ b} /-- `at_bot` is the filter representing the limit `→ -∞` on an ordered set. It is generated by the collection of down-sets `{b | b ≤ a}`. (The preorder need not have a bottom element for this to be well defined, and indeed is trivial when a bottom element exists.) -/ def at_bot [preorder α] : filter α := ⨅ a, principal {b | b ≤ a} lemma mem_at_top [preorder α] (a : α) : {b : α | a ≤ b} ∈ (@at_top α _).sets := mem_infi_sets a $ subset.refl _ @[simp] lemma at_top_ne_bot [inhabited α] [semilattice_sup α] : (at_top : filter α) ≠ ⊥ := infi_neq_bot_of_directed (by apply_instance) (assume a b, ⟨a ⊔ b, by simp [ge] {contextual := tt}⟩) (assume a, by simp [principal_eq_bot_iff]; exact ne_empty_of_mem (le_refl a)) @[simp] lemma mem_at_top_sets [inhabited α] [semilattice_sup α] {s : set α} : s ∈ (at_top : filter α).sets ↔ ∃a:α, ∀b≥a, b ∈ s := iff.intro (assume h, infi_sets_induct h ⟨default α, by simp⟩ (assume a s₁ s₂ ha ⟨b, hb⟩, ⟨a ⊔ b, assume c hc, ⟨ha $ le_trans le_sup_left hc, hb _ $ le_trans le_sup_right hc⟩⟩) (assume s₁ s₂ h ⟨a, ha⟩, ⟨a, assume b hb, h $ ha _ hb⟩)) (assume ⟨a, h⟩, mem_infi_sets a $ assume x, h x) lemma map_at_top_eq [inhabited α] [semilattice_sup α] {f : α → β} : at_top.map f = (⨅a, principal $ f '' {a' | a ≤ a'}) := calc map f (⨅a, principal {a' | a ≤ a'}) = (⨅a, map f $ principal {a' | a ≤ a'}) : map_infi_eq (assume a b, ⟨a ⊔ b, by simp [ge] {contextual := tt}⟩) ⟨default α⟩ ... = (⨅a, principal $ f '' {a' | a ≤ a'}) : by simp lemma tendsto_finset_image_at_top_at_top {i : β → γ} {j : γ → β} (h : ∀x, j (i x) = x) : tendsto (λs:finset γ, s.image j) at_top at_top := tendsto_infi.2 $ assume s, tendsto_infi' (s.image i) $ tendsto_principal_principal.2 $ assume t (ht : s.image i ⊆ t), calc s = (s.image i).image j : by simp [finset.image_image, (∘), h]; exact finset.image_id.symm ... ⊆ t.image j : finset.image_subset_image ht /- ultrafilter -/ section ultrafilter open zorn variables {f g : filter α} /-- An ultrafilter is a minimal (maximal in the set order) proper filter. -/ def ultrafilter (f : filter α) := f ≠ ⊥ ∧ ∀g, g ≠ ⊥ → g ≤ f → f ≤ g lemma ultrafilter_pure {a : α} : ultrafilter (pure a) := ⟨pure_neq_bot, assume g hg ha, have {a} ∈ g.sets, begin simp at ha, assumption end, show ∀s∈g.sets, {a} ⊆ s, from classical.by_contradiction $ begin simp only [classical.not_forall, not_imp, exists_imp_distrib, singleton_subset_iff], exact assume s ⟨hs, hna⟩, have {a} ∩ s ∈ g.sets, from inter_mem_sets ‹{a} ∈ g.sets› hs, have ∅ ∈ g.sets, from mem_sets_of_superset this $ assume x ⟨hxa, hxs⟩, begin simp at hxa; simp [hxa] at hxs, exact hna hxs end, have g = ⊥, from empty_in_sets_eq_bot.mp this, hg this end⟩ lemma ultrafilter_unique (hg : ultrafilter g) (hf : f ≠ ⊥) (h : f ≤ g) : f = g := le_antisymm h (hg.right _ hf h) lemma exists_ultrafilter (h : f ≠ ⊥) : ∃u, u ≤ f ∧ ultrafilter u := let τ := {f' // f' ≠ ⊥ ∧ f' ≤ f}, r : τ → τ → Prop := λt₁ t₂, t₂.val ≤ t₁.val, ⟨a, ha⟩ := inhabited_of_mem_sets h univ_mem_sets, top : τ := ⟨f, h, le_refl f⟩, sup : Π(c:set τ), chain r c → τ := λc hc, ⟨⨅a:{a:τ // a ∈ insert top c}, a.val.val, infi_neq_bot_of_directed ⟨a⟩ (directed_of_chain $ chain_insert hc $ assume ⟨b, _, hb⟩ _ _, or.inl hb) (assume ⟨⟨a, ha, _⟩, _⟩, ha), infi_le_of_le ⟨top, mem_insert _ _⟩ (le_refl _)⟩ in have ∀c (hc: chain r c) a (ha : a ∈ c), r a (sup c hc), from assume c hc a ha, infi_le_of_le ⟨a, mem_insert_of_mem _ ha⟩ (le_refl _), have (∃ (u : τ), ∀ (a : τ), r u a → r a u), from zorn (assume c hc, ⟨sup c hc, this c hc⟩) (assume f₁ f₂ f₃ h₁ h₂, le_trans h₂ h₁), let ⟨uτ, hmin⟩ := this in ⟨uτ.val, uτ.property.right, uτ.property.left, assume g hg₁ hg₂, hmin ⟨g, hg₁, le_trans hg₂ uτ.property.right⟩ hg₂⟩ lemma le_of_ultrafilter {g : filter α} (hf : ultrafilter f) (h : f ⊓ g ≠ ⊥) : f ≤ g := le_of_inf_eq $ ultrafilter_unique hf h inf_le_left lemma mem_or_compl_mem_of_ultrafilter (hf : ultrafilter f) (s : set α) : s ∈ f.sets ∨ - s ∈ f.sets := classical.or_iff_not_imp_right.2 $ assume : - s ∉ f.sets, have f ≤ principal s, from le_of_ultrafilter hf $ assume h, this $ mem_sets_of_neq_bot $ by simp [*], by simp at this; assumption lemma mem_or_mem_of_ultrafilter {s t : set α} (hf : ultrafilter f) (h : s ∪ t ∈ f.sets) : s ∈ f.sets ∨ t ∈ f.sets := (mem_or_compl_mem_of_ultrafilter hf s).imp_right (assume : -s ∈ f.sets, by filter_upwards [this, h] assume x hnx hx, hx.resolve_left hnx) lemma mem_of_finite_sUnion_ultrafilter {s : set (set α)} (hf : ultrafilter f) (hs : finite s) : ⋃₀ s ∈ f.sets → ∃t∈s, t ∈ f.sets := finite.induction_on hs (by simp [empty_in_sets_eq_bot, hf.left]) $ λ t s' ht' hs' ih, by simp; exact assume h, (mem_or_mem_of_ultrafilter hf h).elim (assume : t ∈ f.sets, ⟨t, or.inl rfl, this⟩) (assume h, let ⟨t, hts', ht⟩ := ih h in ⟨t, or.inr hts', ht⟩) lemma mem_of_finite_Union_ultrafilter {is : set β} {s : β → set α} (hf : ultrafilter f) (his : finite is) (h : (⋃i∈is, s i) ∈ f.sets) : ∃i∈is, s i ∈ f.sets := have his : finite (image s is), from finite_image s his, have h : (⋃₀ image s is) ∈ f.sets, from by simp [sUnion_image]; assumption, let ⟨t, ⟨i, hi, h_eq⟩, (ht : t ∈ f.sets)⟩ := mem_of_finite_sUnion_ultrafilter hf his h in ⟨i, hi, h_eq.symm ▸ ht⟩ lemma ultrafilter_of_split {f : filter α} (hf : f ≠ ⊥) (h : ∀s, s ∈ f.sets ∨ - s ∈ f.sets) : ultrafilter f := ⟨hf, assume g hg g_le s hs, (h s).elim id $ assume : - s ∈ f.sets, have s ∩ -s ∈ g.sets, from inter_mem_sets hs (g_le this), by simp [empty_in_sets_eq_bot, hg] at this; contradiction⟩ lemma ultrafilter_map {f : filter α} {m : α → β} (h : ultrafilter f) : ultrafilter (map m f) := ultrafilter_of_split (by simp [map_eq_bot_iff, h.left]) $ assume s, show preimage m s ∈ f.sets ∨ - preimage m s ∈ f.sets, from mem_or_compl_mem_of_ultrafilter h (preimage m s) /-- Construct an ultrafilter extending a given filter. The ultrafilter lemma is the assertion that such a filter exists; we use the axiom of choice to pick one. -/ noncomputable def ultrafilter_of (f : filter α) : filter α := if h : f = ⊥ then ⊥ else classical.epsilon (λu, u ≤ f ∧ ultrafilter u) lemma ultrafilter_of_spec (h : f ≠ ⊥) : ultrafilter_of f ≤ f ∧ ultrafilter (ultrafilter_of f) := begin have h' := classical.epsilon_spec (exists_ultrafilter h), simp [ultrafilter_of, dif_neg, h], simp at h', assumption end lemma ultrafilter_of_le : ultrafilter_of f ≤ f := if h : f = ⊥ then by simp [ultrafilter_of, dif_pos, h]; exact le_refl _ else (ultrafilter_of_spec h).left lemma ultrafilter_ultrafilter_of (h : f ≠ ⊥) : ultrafilter (ultrafilter_of f) := (ultrafilter_of_spec h).right lemma ultrafilter_of_ultrafilter (h : ultrafilter f) : ultrafilter_of f = f := ultrafilter_unique h (ultrafilter_ultrafilter_of h.left).left ultrafilter_of_le end ultrafilter end filter namespace filter variables {α β γ : Type u} {f : β → filter α} {s : γ → set α} open list lemma mem_traverse_sets : ∀(fs : list β) (us : list γ), forall₂ (λb c, s c ∈ (f b).sets) fs us → traverse s us ∈ (traverse f fs).sets | [] [] forall₂.nil := mem_pure_sets.2 $ mem_singleton _ | (f::fs) (u::us) (forall₂.cons h hs) := seq_mem_seq_sets (image_mem_map h) (mem_traverse_sets fs us hs) lemma mem_traverse_sets_iff (fs : list β) (t : set (list α)) : t ∈ (traverse f fs).sets ↔ (∃us:list (set α), forall₂ (λb (s : set α), s ∈ (f b).sets) fs us ∧ sequence us ⊆ t) := begin split, { induction fs generalizing t, case nil { simp [sequence, set.pure_def] }, case cons : b fs ih t { assume ht, rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩, rcases mem_map_sets_iff.1 hu with ⟨w, hw, hwu⟩, rcases ih v hv with ⟨us, hus, hu⟩, exact ⟨w :: us, forall₂.cons hw hus, subset.trans (set.seq_mono hwu hu) ht⟩ } }, { rintros ⟨us, hus, hs⟩, exact mem_sets_of_superset (mem_traverse_sets _ _ hus) hs } end lemma sequence_mono : ∀(as bs : list (filter α)), forall₂ (≤) as bs → sequence as ≤ sequence bs | [] [] forall₂.nil := le_refl _ | (a::as) (b::bs) (forall₂.cons h hs) := seq_mono (map_mono h) (sequence_mono as bs hs) end filter
6fd217a84238a9b22c1a0ea96e34bb397c3c44b7
35b83be3126daae10419b573c55e1fed009d3ae8
/_target/deps/mathlib/analysis/complex.lean
0c79e75d0e7725a8b71ab42b6b1a145e3f65094d
[]
no_license
AHassan1024/Lean_Playground
ccb25b72029d199c0d23d002db2d32a9f2689ebc
a00b004c3a2eb9e3e863c361aa2b115260472414
refs/heads/master
1,586,221,905,125
1,544,951,310,000
1,544,951,310,000
157,934,290
0
0
null
null
null
null
UTF-8
Lean
false
false
5,146
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Topology of the complex numbers. -/ import data.complex.basic analysis.metric_space noncomputable theory open filter namespace complex -- TODO(Mario): these proofs are all copied from analysis/real. Generalize -- to normed fields instance : metric_space ℂ := { dist := λx y, (x - y).abs, dist_self := by simp [abs_zero], eq_of_dist_eq_zero := by simp [add_neg_eq_zero], dist_comm := assume x y, complex.abs_sub _ _, dist_triangle := assume x y z, complex.abs_sub_le _ _ _ } theorem dist_eq (x y : ℂ) : dist x y = (x - y).abs := rfl theorem uniform_continuous_add : uniform_continuous (λp : ℂ × ℂ, p.1 + p.2) := uniform_continuous_of_metric.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_add_continuous_lemma abs ε0 in ⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ h₁ h₂⟩ theorem uniform_continuous_neg : uniform_continuous (@has_neg.neg ℂ _) := uniform_continuous_of_metric.2 $ λ ε ε0, ⟨_, ε0, λ a b h, by rw dist_comm at h; simpa [dist_eq] using h⟩ instance : uniform_add_group ℂ := uniform_add_group.mk' uniform_continuous_add uniform_continuous_neg instance : topological_add_group ℂ := by apply_instance lemma uniform_continuous_inv (s : set ℂ) {r : ℝ} (r0 : 0 < r) (H : ∀ x ∈ s, r ≤ abs x) : uniform_continuous (λp:s, p.1⁻¹) := uniform_continuous_of_metric.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_inv_continuous_lemma abs ε0 r0 in ⟨δ, δ0, λ a b h, Hδ (H _ a.2) (H _ b.2) h⟩ lemma uniform_continuous_abs : uniform_continuous (abs : ℂ → ℝ) := uniform_continuous_of_metric.2 $ λ ε ε0, ⟨ε, ε0, λ a b, lt_of_le_of_lt (abs_abs_sub_le_abs_sub _ _)⟩ lemma continuous_abs : continuous (abs : ℂ → ℝ) := uniform_continuous_abs.continuous lemma tendsto_inv {r : ℂ} (r0 : r ≠ 0) : tendsto (λq, q⁻¹) (nhds r) (nhds r⁻¹) := by rw ← abs_pos at r0; exact tendsto_of_uniform_continuous_subtype (uniform_continuous_inv {x | abs r / 2 < abs x} (half_pos r0) (λ x h, le_of_lt h)) (mem_nhds_sets (continuous_abs _ $ is_open_lt' (abs r / 2)) (half_lt_self r0)) lemma continuous_inv' : continuous (λa:{r:ℂ // r ≠ 0}, a.val⁻¹) := continuous_iff_tendsto.mpr $ assume ⟨r, hr⟩, (continuous_iff_tendsto.mp continuous_subtype_val _).comp (tendsto_inv hr) lemma continuous_inv {α} [topological_space α] {f : α → ℂ} (h : ∀a, f a ≠ 0) (hf : continuous f) : continuous (λa, (f a)⁻¹) := show continuous ((has_inv.inv ∘ @subtype.val ℂ (λr, r ≠ 0)) ∘ λa, ⟨f a, h a⟩), from (continuous_subtype_mk _ hf).comp continuous_inv' lemma uniform_continuous_mul_const {x : ℂ} : uniform_continuous ((*) x) := uniform_continuous_of_metric.2 $ λ ε ε0, begin cases no_top (abs x) with y xy, have y0 := lt_of_le_of_lt (abs_nonneg _) xy, refine ⟨_, div_pos ε0 y0, λ a b h, _⟩, rw [dist_eq, ← mul_sub, abs_mul, ← mul_div_cancel' ε (ne_of_gt y0)], exact mul_lt_mul' (le_of_lt xy) h (abs_nonneg _) y0 end lemma uniform_continuous_mul (s : set (ℂ × ℂ)) {r₁ r₂ : ℝ} (r₁0 : 0 < r₁) (r₂0 : 0 < r₂) (H : ∀ x ∈ s, abs (x : ℂ × ℂ).1 < r₁ ∧ abs x.2 < r₂) : uniform_continuous (λp:s, p.1.1 * p.1.2) := uniform_continuous_of_metric.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_mul_continuous_lemma abs ε0 r₁0 r₂0 in ⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ (H _ a.2).1 (H _ b.2).2 h₁ h₂⟩ protected lemma continuous_mul : continuous (λp : ℂ × ℂ, p.1 * p.2) := continuous_iff_tendsto.2 $ λ ⟨a₁, a₂⟩, tendsto_of_uniform_continuous_subtype (uniform_continuous_mul ({x | abs x < abs a₁ + 1}.prod {x | abs x < abs a₂ + 1}) (lt_of_le_of_lt (abs_nonneg _) (lt_add_one _)) (lt_of_le_of_lt (abs_nonneg _) (lt_add_one _)) (λ x, id)) (mem_nhds_sets (is_open_prod (continuous_abs _ $ is_open_gt' _) (continuous_abs _ $ is_open_gt' _)) ⟨lt_add_one (abs a₁), lt_add_one (abs a₂)⟩) lemma uniform_continuous_re : uniform_continuous re := uniform_continuous_of_metric.2 (λ ε ε0, ⟨ε, ε0, λ _ _, lt_of_le_of_lt (abs_re_le_abs _)⟩) lemma continuous_re : continuous re := uniform_continuous_re.continuous lemma uniform_continuous_im : uniform_continuous im := uniform_continuous_of_metric.2 (λ ε ε0, ⟨ε, ε0, λ _ _, lt_of_le_of_lt (abs_im_le_abs _)⟩) lemma continuous_im : continuous im := uniform_continuous_im.continuous lemma uniform_continuous_of_real : uniform_continuous of_real := uniform_continuous_of_metric.2 (λ ε ε0, ⟨ε, ε0, λ _ _, by rw [real.dist_eq, complex.dist_eq, of_real_eq_coe, of_real_eq_coe, ← of_real_sub, abs_of_real]; exact id⟩) lemma continuous_of_real : continuous of_real := uniform_continuous_of_real.continuous instance : topological_ring ℂ := { continuous_mul := complex.continuous_mul, ..complex.topological_add_group } instance : topological_semiring ℂ := by apply_instance end complex
95ab75d7536c3a0c509e917a9a4522625bef2bdb
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/complex/removable_singularity.lean
784b50e998e6db15df55dbc73db0f2bf1f96792f
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
9,674
lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.calculus.fderiv_analytic import analysis.asymptotics.specific_asymptotics import analysis.complex.cauchy_integral /-! # Removable singularity theorem > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we prove Riemann's removable singularity theorem: if `f : ℂ → E` is complex differentiable in a punctured neighborhood of a point `c` and is bounded in a punctured neighborhood of `c` (or, more generally, $f(z) - f(c)=o((z-c)^{-1})$), then it has a limit at `c` and the function `function.update f c (lim (𝓝[≠] c) f)` is complex differentiable in a neighborhood of `c`. -/ open topological_space metric set filter asymptotics function open_locale topology filter nnreal real universe u variables {E : Type u} [normed_add_comm_group E] [normed_space ℂ E] [complete_space E] namespace complex /-- **Removable singularity** theorem, weak version. If `f : ℂ → E` is differentiable in a punctured neighborhood of a point and is continuous at this point, then it is analytic at this point. -/ lemma analytic_at_of_differentiable_on_punctured_nhds_of_continuous_at {f : ℂ → E} {c : ℂ} (hd : ∀ᶠ z in 𝓝[≠] c, differentiable_at ℂ f z) (hc : continuous_at f c) : analytic_at ℂ f c := begin rcases (nhds_within_has_basis nhds_basis_closed_ball _).mem_iff.1 hd with ⟨R, hR0, hRs⟩, lift R to ℝ≥0 using hR0.le, replace hc : continuous_on f (closed_ball c R), { refine λ z hz, continuous_at.continuous_within_at _, rcases eq_or_ne z c with rfl | hne, exacts [hc, (hRs ⟨hz, hne⟩).continuous_at] }, exact (has_fpower_series_on_ball_of_differentiable_off_countable (countable_singleton c) hc (λ z hz, hRs (diff_subset_diff_left ball_subset_closed_ball hz)) hR0).analytic_at end lemma differentiable_on_compl_singleton_and_continuous_at_iff {f : ℂ → E} {s : set ℂ} {c : ℂ} (hs : s ∈ 𝓝 c) : differentiable_on ℂ f (s \ {c}) ∧ continuous_at f c ↔ differentiable_on ℂ f s := begin refine ⟨_, λ hd, ⟨hd.mono (diff_subset _ _), (hd.differentiable_at hs).continuous_at⟩⟩, rintro ⟨hd, hc⟩ x hx, rcases eq_or_ne x c with rfl | hne, { refine (analytic_at_of_differentiable_on_punctured_nhds_of_continuous_at _ hc) .differentiable_at.differentiable_within_at, refine eventually_nhds_within_iff.2 ((eventually_mem_nhds.2 hs).mono $ λ z hz hzx, _), exact hd.differentiable_at (inter_mem hz (is_open_ne.mem_nhds hzx)) }, { simpa only [differentiable_within_at, has_fderiv_within_at, hne.nhds_within_diff_singleton] using hd x ⟨hx, hne⟩ } end lemma differentiable_on_dslope {f : ℂ → E} {s : set ℂ} {c : ℂ} (hc : s ∈ 𝓝 c) : differentiable_on ℂ (dslope f c) s ↔ differentiable_on ℂ f s := ⟨λ h, h.of_dslope, λ h, (differentiable_on_compl_singleton_and_continuous_at_iff hc).mp $ ⟨iff.mpr (differentiable_on_dslope_of_nmem $ λ h, h.2 rfl) (h.mono $ diff_subset _ _), continuous_at_dslope_same.2 $ h.differentiable_at hc⟩⟩ /-- **Removable singularity** theorem: if `s` is a neighborhood of `c : ℂ`, a function `f : ℂ → E` is complex differentiable on `s \ {c}`, and $f(z) - f(c)=o((z-c)^{-1})$, then `f` redefined to be equal to `lim (𝓝[≠] c) f` at `c` is complex differentiable on `s`. -/ lemma differentiable_on_update_lim_of_is_o {f : ℂ → E} {s : set ℂ} {c : ℂ} (hc : s ∈ 𝓝 c) (hd : differentiable_on ℂ f (s \ {c})) (ho : (λ z, f z - f c) =o[𝓝[≠] c] (λ z, (z - c)⁻¹)) : differentiable_on ℂ (update f c (lim (𝓝[≠] c) f)) s := begin set F : ℂ → E := λ z, (z - c) • f z with hF, suffices : differentiable_on ℂ F (s \ {c}) ∧ continuous_at F c, { rw [differentiable_on_compl_singleton_and_continuous_at_iff hc, ← differentiable_on_dslope hc, dslope_sub_smul] at this; try { apply_instance }, have hc : tendsto f (𝓝[≠] c) (𝓝 (deriv F c)), from continuous_at_update_same.mp (this.continuous_on.continuous_at hc), rwa hc.lim_eq }, refine ⟨(differentiable_on_id.sub_const _).smul hd, _⟩, rw ← continuous_within_at_compl_self, have H := ho.tendsto_inv_smul_nhds_zero, have H' : tendsto (λ z, (z - c) • f c) (𝓝[≠] c) (𝓝 (F c)), from (continuous_within_at_id.tendsto.sub tendsto_const_nhds).smul tendsto_const_nhds, simpa [← smul_add, continuous_within_at] using H.add H' end /-- **Removable singularity** theorem: if `s` is a punctured neighborhood of `c : ℂ`, a function `f : ℂ → E` is complex differentiable on `s`, and $f(z) - f(c)=o((z-c)^{-1})$, then `f` redefined to be equal to `lim (𝓝[≠] c) f` at `c` is complex differentiable on `{c} ∪ s`. -/ lemma differentiable_on_update_lim_insert_of_is_o {f : ℂ → E} {s : set ℂ} {c : ℂ} (hc : s ∈ 𝓝[≠] c) (hd : differentiable_on ℂ f s) (ho : (λ z, f z - f c) =o[𝓝[≠] c] (λ z, (z - c)⁻¹)) : differentiable_on ℂ (update f c (lim (𝓝[≠] c) f)) (insert c s) := differentiable_on_update_lim_of_is_o (insert_mem_nhds_iff.2 hc) (hd.mono $ λ z hz, hz.1.resolve_left hz.2) ho /-- **Removable singularity** theorem: if `s` is a neighborhood of `c : ℂ`, a function `f : ℂ → E` is complex differentiable and is bounded on `s \ {c}`, then `f` redefined to be equal to `lim (𝓝[≠] c) f` at `c` is complex differentiable on `s`. -/ lemma differentiable_on_update_lim_of_bdd_above {f : ℂ → E} {s : set ℂ} {c : ℂ} (hc : s ∈ 𝓝 c) (hd : differentiable_on ℂ f (s \ {c})) (hb : bdd_above (norm ∘ f '' (s \ {c}))) : differentiable_on ℂ (update f c (lim (𝓝[≠] c) f)) s := differentiable_on_update_lim_of_is_o hc hd $ is_bounded_under.is_o_sub_self_inv $ let ⟨C, hC⟩ := hb in ⟨C + ‖f c‖, eventually_map.2 $ mem_nhds_within_iff_exists_mem_nhds_inter.2 ⟨s, hc, λ z hz, norm_sub_le_of_le (hC $ mem_image_of_mem _ hz) le_rfl⟩⟩ /-- **Removable singularity** theorem: if a function `f : ℂ → E` is complex differentiable on a punctured neighborhood of `c` and $f(z) - f(c)=o((z-c)^{-1})$, then `f` has a limit at `c`. -/ lemma tendsto_lim_of_differentiable_on_punctured_nhds_of_is_o {f : ℂ → E} {c : ℂ} (hd : ∀ᶠ z in 𝓝[≠] c, differentiable_at ℂ f z) (ho : (λ z, f z - f c) =o[𝓝[≠] c] (λ z, (z - c)⁻¹)) : tendsto f (𝓝[≠] c) (𝓝 $ lim (𝓝[≠] c) f) := begin rw eventually_nhds_within_iff at hd, have : differentiable_on ℂ f ({z | z ≠ c → differentiable_at ℂ f z} \ {c}), from λ z hz, (hz.1 hz.2).differentiable_within_at, have H := differentiable_on_update_lim_of_is_o hd this ho, exact continuous_at_update_same.1 (H.differentiable_at hd).continuous_at end /-- **Removable singularity** theorem: if a function `f : ℂ → E` is complex differentiable and bounded on a punctured neighborhood of `c`, then `f` has a limit at `c`. -/ lemma tendsto_lim_of_differentiable_on_punctured_nhds_of_bounded_under {f : ℂ → E} {c : ℂ} (hd : ∀ᶠ z in 𝓝[≠] c, differentiable_at ℂ f z) (hb : is_bounded_under (≤) (𝓝[≠] c) (λ z, ‖f z - f c‖)) : tendsto f (𝓝[≠] c) (𝓝 $ lim (𝓝[≠] c) f) := tendsto_lim_of_differentiable_on_punctured_nhds_of_is_o hd hb.is_o_sub_self_inv /-- The Cauchy formula for the derivative of a holomorphic function. -/ lemma two_pi_I_inv_smul_circle_integral_sub_sq_inv_smul_of_differentiable {U : set ℂ} (hU : is_open U) {c w₀ : ℂ} {R : ℝ} {f : ℂ → E} (hc : closed_ball c R ⊆ U) (hf : differentiable_on ℂ f U) (hw₀ : w₀ ∈ ball c R) : (2 * π * I : ℂ)⁻¹ • ∮ z in C(c, R), ((z - w₀) ^ 2)⁻¹ • f z = deriv f w₀ := begin -- We apply the removable singularity theorem and the Cauchy formula to `dslope f w₀` have hR : 0 < R := not_le.mp (ball_eq_empty.not.mp (nonempty_of_mem hw₀).ne_empty), have hf' : differentiable_on ℂ (dslope f w₀) U, from (differentiable_on_dslope (hU.mem_nhds ((ball_subset_closed_ball.trans hc) hw₀))).mpr hf, have h0 := (hf'.diff_cont_on_cl_ball hc).two_pi_I_inv_smul_circle_integral_sub_inv_smul hw₀, rw [← dslope_same, ← h0], congr' 1, transitivity ∮ z in C(c, R), ((z - w₀) ^ 2)⁻¹ • (f z - f w₀), { have h1 : continuous_on (λ (z : ℂ), ((z - w₀) ^ 2)⁻¹) (sphere c R), { refine ((continuous_id'.sub continuous_const).pow 2).continuous_on.inv₀ (λ w hw h, _), exact sphere_disjoint_ball.ne_of_mem hw hw₀ (sub_eq_zero.mp (sq_eq_zero_iff.mp h)) }, have h2 : circle_integrable (λ (z : ℂ), ((z - w₀) ^ 2)⁻¹ • f z) c R, { refine continuous_on.circle_integrable (pos_of_mem_ball hw₀).le _, exact h1.smul (hf.continuous_on.mono (sphere_subset_closed_ball.trans hc)) }, have h3 : circle_integrable (λ (z : ℂ), ((z - w₀) ^ 2)⁻¹ • f w₀) c R, from continuous_on.circle_integrable (pos_of_mem_ball hw₀).le (h1.smul continuous_on_const), have h4 : ∮ (z : ℂ) in C(c, R), ((z - w₀) ^ 2)⁻¹ = 0, by simpa using circle_integral.integral_sub_zpow_of_ne (dec_trivial : (-2 : ℤ) ≠ -1) c w₀ R, simp only [smul_sub, circle_integral.integral_sub h2 h3, h4, circle_integral.integral_smul_const, zero_smul, sub_zero] }, { refine circle_integral.integral_congr (pos_of_mem_ball hw₀).le (λ z hz, _), simp only [dslope_of_ne, metric.sphere_disjoint_ball.ne_of_mem hz hw₀, slope, ← smul_assoc, sq, mul_inv, ne.def, not_false_iff, vsub_eq_sub, algebra.id.smul_eq_mul] } end end complex
5ff1d6418517e792125171fe7a5891371a274ee3
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/tests/lean/hott/531b.hlean
1d85fa49bbf37d46286bd5493daf056396f6dfca
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
6,287
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: init.hit Authors: Floris van Doorn Declaration of hits -/ structure diagram [class] := (Iob : Type) (Ihom : Type) (ob : Iob → Type) (dom cod : Ihom → Iob) (hom : Π(j : Ihom), ob (dom j) → ob (cod j)) open eq diagram -- structure col (D : diagram) := -- (incl : Π{i : Iob}, ob i) -- (eq_endpoint : Π{j : Ihom} (x : ob (dom j)), ob (cod j)) -- set_option pp.universes true -- check @diagram -- check @col constant colimit.{u v w} : diagram.{u v w} → Type.{max u v w} namespace colimit constant inclusion : Π [D : diagram] {i : Iob}, ob i → colimit D abbreviation ι := @inclusion constant cglue : Π [D : diagram] (j : Ihom) (x : ob (dom j)), ι (hom j x) = ι x /-protected-/ constant rec : Π [D : diagram] {P : colimit D → Type} (Pincl : Π⦃i : Iob⦄ (x : ob i), P (ι x)) (Pglue : Π(j : Ihom) (x : ob (dom j)), cglue j x ▸ Pincl (hom j x) = Pincl x) (y : colimit D), P y -- {P : my_colim f → Type} (Hinc : Π⦃n : ℕ⦄ (a : A n), P (inc f a)) -- (Heq : Π(n : ℕ) (a : A n), inc_eq f a ▸ Hinc (f a) = Hinc a) : Πaa, P aa -- init_hit definition comp_incl [D : diagram] {P : colimit D → Type} (Pincl : Π⦃i : Iob⦄ (x : ob i), P (ι x)) (Pglue : Π(j : Ihom) (x : ob (dom j)), cglue j x ▸ Pincl (hom j x) = Pincl x) {i : Iob} (x : ob i) : rec Pincl Pglue (ι x) = Pincl x := sorry --idp --set_option pp.notation false definition comp_cglue [D : diagram] {P : colimit D → Type} (Pincl : Π⦃i : Iob⦄ (x : ob i), P (ι x)) (Pglue : Π(j : Ihom) (x : ob (dom j)), cglue j x ▸ Pincl (hom j x) = Pincl x) {j : Ihom} (x : ob (dom j)) : apd (rec Pincl Pglue) (cglue j x) = sorry ⬝ Pglue j x ⬝ sorry := --the sorry's in the statement can be removed when comp_incl is definitional sorry --idp protected definition rec_on [D : diagram] {P : colimit D → Type} (y : colimit D) (Pincl : Π⦃i : Iob⦄ (x : ob i), P (ι x)) (Pglue : Π(j : Ihom) (x : ob (dom j)), cglue j x ▸ Pincl (hom j x) = Pincl x) : P y := colimit.rec Pincl Pglue y end colimit open colimit bool namespace pushout section universe u parameters {TL BL TR : Type.{u}} (f : TL → BL) (g : TL → TR) inductive pushout_ob := | tl : pushout_ob | bl : pushout_ob | tr : pushout_ob open pushout_ob definition pushout_diag [reducible] : diagram := diagram.mk pushout_ob bool (λi, pushout_ob.rec_on i TL BL TR) (λj, bool.rec_on j tl tl) (λj, bool.rec_on j bl tr) (λj, bool.rec_on j f g) local notation `D` := pushout_diag -- open bool -- definition pushout_diag : diagram := -- diagram.mk pushout_ob -- bool -- (λi, match i with | tl := TL | tr := TR | bl := BL end) -- (λj, match j with | tt := tl | ff := tl end) -- (λj, match j with | tt := bl | ff := tr end) -- (λj, match j with | tt := f | ff := g end) definition pushout := colimit pushout_diag local attribute pushout_diag [instance] definition inl (x : BL) : pushout := @ι _ _ x definition inr (x : TR) : pushout := @ι _ _ x definition coherence (x : TL) : inl (f x) = @ι _ _ x := @cglue _ _ x definition glue (x : TL) : inl (f x) = inr (g x) := @cglue _ _ x ⬝ (@cglue _ _ x)⁻¹ set_option pp.notation false set_option pp.implicit true set_option pp.beta false -- set_option pp.universes true protected theorem rec {P : pushout → Type} --make def (Pinl : Π(x : BL), P (inl x)) (Pinr : Π(x : TR), P (inr x)) (Pglue : Π(x : TL), glue x ▸ Pinl (f x) = Pinr (g x)) (y : pushout) : P y := begin fapply (@colimit.rec_on _ _ y), { intros [i, x], cases i, exact (coherence x ▸ Pinl (f x)), apply Pinl, apply Pinr}, { intros [j, x], cases j, exact idp, change (transport P (@cglue _ tt x) (Pinr (g x)) = transport P (coherence x) (Pinl (f x))), --(@cglue _ tt x ▸ (Pinr (g x)) = (coherence x ▸ Pinl (f x))), apply concat;rotate 1;apply (idpath (coherence x ▸ Pinl (f x))), apply concat;apply (ap (transport _ _));apply (idpath (Pinr (g x))), apply tr_eq_of_eq_inv_tr, -- rewrite -{(transport (λ (x : pushout), P x) (inverse (coherence x)) (transport P (@cglue _ tt x) (Pinr (g x))))}tr_con, apply concat, rotate 1, apply con_tr, rewrite -Pglue} end example {P : pushout → Type} (Pinl : Π (x : BL), P (inl x)) (Pinr : Π (x : TR), P (inr x)) (Pglue : Π (x : TL), @eq (P (inr (g x))) (@transport pushout (λ (x : pushout), P x) (inl (f x)) (inr (g x)) (glue x) (Pinl (f x))) (Pinr (g x))) (y : pushout) (x : @ob pushout_diag (@dom pushout_diag tt)) : @eq ((λ (x : colimit pushout_diag), P x) (@ι pushout_diag (@dom pushout_diag tt) x)) (@transport (colimit pushout_diag) (λ (x : colimit pushout_diag), P x) (@ι pushout_diag (@cod pushout_diag tt) (@hom pushout_diag tt x)) (@ι pushout_diag (@dom pushout_diag tt) x) (@cglue pushout_diag tt x) (@pushout_ob.cases_on (λ (n : pushout_ob), Π (x : @ob pushout_diag n), P (@ι pushout_diag n x)) (@cod pushout_diag tt) (λ (x : @ob pushout_diag tl), @transport pushout (λ (x : pushout), P x) (inl (f x)) (@ι pushout_diag (@dom pushout_diag ff) x) (coherence x) (Pinl (f x))) (λ (x : @ob pushout_diag bl), Pinl x) (λ (x : @ob pushout_diag tr), Pinr x) (@hom pushout_diag tt x))) (@pushout_ob.cases_on (λ (n : pushout_ob), Π (x : @ob pushout_diag n), P (@ι pushout_diag n x)) (@dom pushout_diag tt) (λ (x : @ob pushout_diag tl), @transport pushout (λ (x : pushout), P x) (inl (f x)) (@ι pushout_diag (@dom pushout_diag ff) x) (coherence x) (Pinl (f x))) (λ (x : @ob pushout_diag bl), Pinl x) (λ (x : @ob pushout_diag tr), Pinr x) x) := begin change (transport P (@cglue _ tt x) (Pinr (g x)) = transport P (coherence x) (Pinl (f x))), apply sorry end exit
0a8cec312e7dc17a9f814b8f81956b377bf78316
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/hott/algebra/trunc_group.hlean
969961d0a6ad736940573642c0e322c7faa3621c
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
2,485
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn truncating an ∞-group to a group -/ import hit.trunc algebra.group open eq is_trunc trunc namespace algebra section parameters (A : Type) (mul : A → A → A) (inv : A → A) (one : A) {mul_assoc : ∀a b c, mul (mul a b) c = mul a (mul b c)} {one_mul : ∀a, mul one a = a} {mul_one : ∀a, mul a one = a} {mul_left_inv : ∀a, mul (inv a) a = one} local abbreviation G := trunc 0 A include mul_assoc one_mul mul_one mul_left_inv definition trunc_mul [unfold 9 10] (g h : G) : G := begin apply trunc.rec_on g, intro p, apply trunc.rec_on h, intro q, exact tr (mul p q) end definition trunc_inv [unfold 9] (g : G) : G := begin apply trunc.rec_on g, intro p, exact tr (inv p) end definition trunc_one [constructor] : G := tr one local notation 1 := trunc_one local postfix ⁻¹ := trunc_inv local infix * := trunc_mul theorem trunc_mul_assoc (g₁ g₂ g₃ : G) : g₁ * g₂ * g₃ = g₁ * (g₂ * g₃) := begin apply trunc.rec_on g₁, intro p₁, apply trunc.rec_on g₂, intro p₂, apply trunc.rec_on g₃, intro p₃, exact ap tr !mul_assoc, end theorem trunc_one_mul (g : G) : 1 * g = g := begin apply trunc.rec_on g, intro p, exact ap tr !one_mul end theorem trunc_mul_one (g : G) : g * 1 = g := begin apply trunc.rec_on g, intro p, exact ap tr !mul_one end theorem trunc_mul_left_inv (g : G) : g⁻¹ * g = 1 := begin apply trunc.rec_on g, intro p, exact ap tr !mul_left_inv end theorem trunc_mul_comm (mul_comm : ∀a b, mul a b = mul b a) (g h : G) : g * h = h * g := begin apply trunc.rec_on g, intro p, apply trunc.rec_on h, intro q, exact ap tr !mul_comm end parameters (mul_assoc) (one_mul) (mul_one) (mul_left_inv) {A} definition trunc_group [constructor] : group G := ⦃group, mul := trunc_mul, mul_assoc := trunc_mul_assoc, one := trunc_one, one_mul := trunc_one_mul, mul_one := trunc_mul_one, inv := trunc_inv, mul_left_inv := trunc_mul_left_inv, is_hset_carrier := _⦄ definition trunc_comm_group [constructor] (mul_comm : ∀a b, mul a b = mul b a) : comm_group G := ⦃comm_group, trunc_group, mul_comm := trunc_mul_comm mul_comm⦄ end end algebra
2b910cdac2c600795c78bf6f72a3641e40125091
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/game/max/max_API_test.lean
a33acb8f15622685396c86e955a7a29b9331b840
[ "Apache-2.0" ]
permissive
adastra7470/real-number-game
776606961f52db0eb824555ed2f8e16f92216ea3
f9dcb7d9255a79b57e62038228a23346c2dc301b
refs/heads/master
1,669,221,575,893
1,594,669,800,000
1,594,669,800,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,906
lean
import tactic open_locale classical -- def of max a b is "if a <= b then b else a" noncomputable theory -- same reason -- A linear order is a reflexive, transitive, antisymmetric and total relation `≤` variables {X : Type} [linear_order X] {a b c : X} -- Note in the below that sometimes we have to supply the variable names and other times not. -- I am scared after my NNG experience to break with tradition and start -- changing mathlib conventions. They are there for a reason (you don't need -- them when there are implications involved, because you can guess the -- variables from the hypotheses) -- I think that the user would be happy to accept all of these as axioms, -- even though strictly speaking one could define a < b to be a ≤ b ∧ a ≠ b -- and then prove the last two. example : a ≤ a := le_refl a example : a ≤ b → b ≤ c → a ≤ c := le_trans example : a ≤ b → b ≤ a → a = b := le_antisymm -- I think this is more useful in practice than trichotomy example : a ≤ b ∨ b ≤ a := le_total a b example : a < b ↔ a ≤ b ∧ a ≠ b := lt_iff_le_and_ne example : a ≤ b → b < c → a < c := lt_of_le_of_lt example : a < b → b ≤ c → a < c := lt_of_lt_of_le -- max is already defined so we work in a namespace namespace test -- I think this definition might be hard to work with def max (a b : X) := if a ≤ b then b else a -- need if_pos to do this one theorem max_eq_right (hab : a ≤ b) : max a b = b := begin unfold max, rw if_pos hab, end -- need if_neg to do this one theorem max_eq_left (hba : b ≤ a) : max a b = a := begin by_cases hab : a ≤ b, { rw max_eq_right hab, exact le_antisymm hba hab, }, { unfold max, rw if_neg hab, } end -- but now things work really nicely. Proposal: tell them that -- max_eq_left and max_eq_right are axioms of max, and don't -- tell them the definition. Note that max_eq_left and max_eq_right -- together are enough to deduce that max a b is what it is, -- because of le_antisymm -- level 1 theorem max_choice (a b : X) : max a b = a ∨ max a b = b := begin cases le_total a b with hab hba, { right, exact max_eq_right hab }, { left, exact max_eq_left hba } end -- level 2 theorem max_comm (a b : X) : max a b = max b a := begin cases le_total a b with hab hba, { rw max_eq_right hab, rw max_eq_left hab, }, { rw max_eq_left hba, rw max_eq_right hba } end -- level 3 theorem le_max_left (a b : X) : a ≤ max a b := begin cases le_total a b with hab hba, { rw max_eq_right hab, assumption }, { rw max_eq_left hba, -- Lean closes a ≤ a automatically because ≤ is reflexive } end -- level 4 theorem le_max_right (a b : X) : b ≤ max a b := begin rw max_comm, apply le_max_left end -- this comes out nicely -- level 5 theorem max_le (hac : a ≤ c) (hbc : b ≤ c) : max a b ≤ c := begin cases max_choice a b with ha hb, { rw ha, assumption }, { rw hb, assumption } end -- and this, if we can teach `apply le_trans _ habc` -- level 6 theorem max_le_iff : a ≤ c ∧ b ≤ c ↔ max a b ≤ c := begin split, { intro h, cases h with hac hbc, exact max_le hac hbc }, { intro habc, split, { apply le_trans _ habc, apply le_max_left}, { apply le_trans _ habc, apply le_max_right } }, end -- so does this -- level 7 theorem max_lt (hac : a < c) (hbc : b < c) : max a b < c := begin cases max_choice a b with ha hb, { rw ha, assumption }, { rw hb, assumption } end -- level 8 theorem max_lt_iff : a < c ∧ b < c ↔ max a b < c := begin split, { intro h, cases h with hac hbc, exact max_lt hac hbc }, { intro habc, split, { apply lt_of_le_of_lt _ habc, apply le_max_left}, { apply lt_of_le_of_lt _ habc, apply le_max_right } }, end -- level 9 theorem le_max_iff : a ≤ max b c ↔ a ≤ b ∨ a ≤ c := begin split, { intro ha, cases le_total b c with hbc hcb, { rw max_eq_right hbc at ha, right, assumption, }, { rw max_eq_left hcb at ha, left, assumption } }, { intro habc, cases habc with hab hac, { apply le_trans hab, apply le_max_left}, { apply le_trans hac, apply le_max_right}, } end -- same as previous one theorem lt_max_iff : a < max b c ↔ a < b ∨ a < c := begin split, { intro ha, cases le_total b c with hbc hcb, { rw max_eq_right hbc at ha, right, assumption, }, { rw max_eq_left hcb at ha, left, assumption } }, { intro habc, cases habc with hab hac, { apply lt_of_lt_of_le hab, apply le_max_left}, { apply lt_of_lt_of_le hac, apply le_max_right}, } end -- I think that's a good API for max. Let's test this hypothesis -- by seeing how easy it is to make a good API for abs. end test
512faf8ed6807a1ffbd25247180d74872303e699
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/number_theory/sum_two_squares.lean
b4fd2731133e7aca4f6a1b655e6d3c253e3b3c66
[]
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
736
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Chris Hughes -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.zsqrtd.gaussian_int import Mathlib.PostPort namespace Mathlib /-! # Sums of two squares Proof of Fermat's theorem on the sum of two squares. Every prime congruent to 1 mod 4 is the sum of two squares -/ namespace nat namespace prime /-- Fermat's theorem on the sum of two squares. Every prime congruent to 1 mod 4 is the sum of two squares -/ theorem sum_two_squares (p : ℕ) [hp : fact (prime p)] (hp1 : p % bit0 (bit0 1) = 1) : ∃ (a : ℕ), ∃ (b : ℕ), a ^ bit0 1 + b ^ bit0 1 = p := sorry
20fc61fd19080646e629c97009522ac11705092b
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/616.lean
ff3a8407bd6305dacd828fcdcaea8f5d7502250d
[ "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
109
lean
def bug : Monad (λ α : Type _ => α → Prop) where pure x := (.=x) bind s f y := ∃ x, s x ∧ f x y
f937104febfdfff6c72b930527d63aa8420720e2
efce24474b28579aba3272fdb77177dc2b11d7aa
/src/homotopy_theory/formal/cylinder/hep.lean
9a93f3ab280a96e67a1422a66714b2ed3153ec25
[ "Apache-2.0" ]
permissive
rwbarton/lean-homotopy-theory
cff499f24268d60e1c546e7c86c33f58c62888ed
39e1b4ea1ed1b0eca2f68bc64162dde6a6396dee
refs/heads/lean-3.4.2
1,622,711,883,224
1,598,550,958,000
1,598,550,958,000
136,023,667
12
6
Apache-2.0
1,573,187,573,000
1,528,116,262,000
Lean
UTF-8
Lean
false
false
5,059
lean
import category_theory.colimits import category_theory.isomorphism import category_theory.preserves_colimits import category_theory.replete import .definitions import category_theory.functor open category_theory open category_theory.category local notation f ` ∘ `:80 g:80 := g ≫ f universes v u namespace homotopy_theory.cylinder section hep variables {C : Type u} [category.{v} C] [inst1 : has_cylinder C] [inst2 : has_cylinder_with_involution C] include inst1 -- The homotopy extension property with respect to the given cylinder -- functor, "on side ε". def hep (ε) {A X : C} (j : A ⟶ X) : Prop := ∀ Y (f : X ⟶ Y) (H : I.obj A ⟶ Y), f ∘ j = H ∘ i ε @> A → ∃ H' : I.obj X ⟶ Y, H' ∘ i ε @> X = f ∧ H' ∘ I &> j = H lemma hep_of_isomorphism (ε) {A X : C} (h : iso A X) : hep ε h.hom := assume Y f H e, ⟨H ∘ I &> h.inv, by erw [←assoc, ←(i ε).naturality, assoc, ←e, iso.inv_hom_id_assoc], by erw [←functor.map_iso_hom, iso.hom_inv_id_assoc]⟩ lemma hep_id (ε) {X : C} : hep ε (𝟙 X) := hep_of_isomorphism ε (iso.refl X) lemma hep_comp (ε) {A B X : C} {f : A ⟶ B} {g : B ⟶ X} (hf : hep ε f) (hg : hep ε g) : hep ε (g ∘ f) := assume Y k H e, let ⟨J, Je₁, Je₂⟩ := hf Y (k ∘ g) H (by convert e using 1; simp) in let ⟨K, Ke₁, Ke₂⟩ := hg Y k J Je₁.symm in ⟨K, Ke₁, by rw [I.map_comp, assoc, Ke₂, Je₂]⟩ instance hep_replete (ε) : replete_wide_subcategory.{v} C (λ a b, hep ε) := replete_wide_subcategory.mk' (λ a b, hep_of_isomorphism ε) (λ a b c f g, hep_comp ε) lemma hep_pushout (ε) {A B A' B' : C} {f : A ⟶ B} {g : A ⟶ A'} {f' : A' ⟶ B'} {g' : B ⟶ B'} (po : Is_pushout f g g' f') (po' : Is_pushout (I &> f) (I &> g) (I &> g') (I &> f')) (hf : hep ε f) : hep ε f' := assume Y h H e, have (h ∘ g') ∘ f = (H ∘ (I &> g)) ∘ i ε @> A, begin erw [←assoc, ←assoc, po.commutes, ←(i ε).naturality], simp [e] end, let ⟨J, Je₁, Je₂⟩ := hf Y (h ∘ g') (H ∘ (I &> g)) this in let K := po'.induced J H Je₂ in ⟨K, begin apply po.uniqueness; erw [←assoc, (i ε).naturality, assoc], { rw [←Je₁], simp }, { rw [e], simp } end, po'.induced_commutes₁ J H Je₂⟩ lemma hep_pushout' [preserves_pushouts (I : C ↝ C)] (ε) {A B A' B' : C} {f : A ⟶ B} {g : A ⟶ A'} {f' : A' ⟶ B'} {g' : B ⟶ B'} (po : Is_pushout f g g' f') (hf : hep ε f) : hep ε f' := hep_pushout ε po (preserves_pushouts.Is_pushout_of_Is_pushout po) hf lemma hep_iff_pushout_retract (ε) {A X : C} {j : A ⟶ X} {Z : C} {i' : X ⟶ Z} {j' : I.obj A ⟶ Z} (po : Is_pushout j (i ε @> A) i' j') : hep ε j ↔ ∃ r : I.obj X ⟶ Z, r ∘ po.induced (i ε @> X) (I &> j) ((i ε).naturality _) = 𝟙 _ := iff.intro (assume h, let ⟨r, hr₁, hr₂⟩ := h Z i' j' po.commutes in ⟨r, by apply po.uniqueness; rw ←assoc; simpa⟩) (assume ⟨r, hr⟩ Y f H e, have hr₁ : r ∘ i ε @> X = i', from eq.symm $ calc i' = 𝟙 _ ∘ i' : by simp ... = (r ∘ _) ∘ i' : by rw hr ... = _ : by rw ←assoc; simp, have hr₂ : r ∘ I &> j = j', from eq.symm $ calc j' = 𝟙 _ ∘ j' : by simp ... = (r ∘ _) ∘ j' : by rw hr ... = _ : by rw ←assoc; simp, ⟨po.induced f H e ∘ r, by rw [←assoc, hr₁]; simp, by rw [←assoc, hr₂]; simp⟩) lemma hep_initial_induced (ε) {A X : C} {j : A ⟶ X} (Ai : Is_initial_object.{v} A) (IAi : Is_initial_object.{v} (I.obj A)) : hep ε j := let po : Is_pushout j (i ε @> A) (𝟙 X) IAi.induced := begin convert Is_pushout_of_isomorphic (Is_pushout.refl j) j (i ε @> A) (iso.refl A) (iso.refl X) (initial_object.unique IAi Ai) (Ai.uniqueness _ _) (Ai.uniqueness _ _), { simp }, { apply IAi.uniqueness } end in (hep_iff_pushout_retract ε po).mpr ⟨p @> X, po.uniqueness (by rw [←assoc, po.induced_commutes₀]; simp) (IAi.uniqueness _ _)⟩ -- The two-sided homotopy extension property. @[reducible] def two_sided_hep {A X : C} (j : A ⟶ X) : Prop := ∀ ε, hep ε j omit inst1 include inst2 lemma hep_involution {ε} {A X : C} {j : A ⟶ X} (h : hep ε j) : hep ε.v j := assume Y f H e, let ⟨H₁, h₁, h₂⟩ := h Y f (H ∘ v @> A) (by convert e using 1; rw [←assoc]; simp) in ⟨H₁ ∘ v @> X, by rw ←assoc; simpa using h₁, calc H₁ ∘ v @> X ∘ I &> j = H₁ ∘ (v @> X ∘ I &> j) : by rw assoc ... = H₁ ∘ (I &> j ∘ v @> A) : by erw v.naturality; refl ... = (H₁ ∘ I &> j) ∘ v @> A : by simp ... = (H ∘ v @> A) ∘ v @> A : by rw h₂ ... = H : by rw ←assoc; simp; dsimp; simp⟩ lemma two_sided_hep_iff_hep {ε} {A X : C} {j : A ⟶ X} : two_sided_hep j ↔ hep ε j := have ∀ ε', ε' = ε ∨ ε' = ε.v, by intro ε'; cases ε; cases ε'; simp; refl, iff.intro (assume h, h ε) (assume h ε', begin cases this ε'; subst ε', { exact h }, { exact hep_involution h } end) end hep end homotopy_theory.cylinder
ff15180576a12f7c67526c587cc9173afd504354
94e33a31faa76775069b071adea97e86e218a8ee
/src/probability/hitting_time.lean
70924d70c016fce6715a076281d0904abe92ebeb
[ "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,767
lean
/- Copyright (c) 2022 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying, Rémy Degenne -/ import probability.stopping /-! # Hitting time Given a stochastic process, the hitting time provides the first time the process ``hits'' some subset of the state space. The hitting time is a stopping time in the case that the time index is discrete and the process is adapted (this is true in a far more general setting however we have only proved it for the discrete case so far). ## Main definition * `measure_theory.hitting`: the hitting time of a stochastic process ## Main results * `measure_theory.hitting_is_stopping_time`: a discrete hitting time of an adapted process is a stopping time ## Implementation notes In the definition of the hitting time, we bound the hitting time by an upper and lower bound. This is to ensure that our result is meaningful in the case we are taking the infimum of an empty set or the infimum of a set which is unbounded from below. With this, we can talk about hitting times indexed by the natural numbers or the reals. By taking the bounds to be `⊤` and `⊥`, we obtain the standard definition in the case that the index is `with_top ℕ` or `ℝ≥0∞`. -/ open filter order topological_space open_locale classical measure_theory nnreal ennreal topological_space big_operators namespace measure_theory variables {α β ι : Type*} {m : measurable_space α} /-- Hitting time: given a stochastic process `u` and a set `s`, `hitting u s n m` is the first time `u` is in `s` after time `n` and before time `m` (if `u` does not hit `s` after time `n` and before `m` then the hitting time is simply `m`). The hitting time is a stopping time if the process is adapted and discrete. -/ noncomputable def hitting [preorder ι] [has_Inf ι] (u : ι → α → β) (s : set β) (n m : ι) : α → ι := λ x, if ∃ j ∈ set.Icc n m, u j x ∈ s then Inf (set.Icc n m ∩ {i : ι | u i x ∈ s}) else m section inequalities variables [conditionally_complete_linear_order ι] {u : ι → α → β} {s : set β} {n i : ι} {x : α} lemma hitting_of_lt {m : ι} (h : m < n) : hitting u s n m x = m := begin simp_rw [hitting], have h_not : ¬∃ (j : ι) (H : j ∈ set.Icc n m), u j x ∈ s, { push_neg, intro j, rw set.Icc_eq_empty_of_lt h, simp only [set.mem_empty_eq, is_empty.forall_iff], }, simp only [h_not, if_false], end lemma hitting_le {m : ι} (x : α) : hitting u s n m x ≤ m := begin cases le_or_lt n m with h_le h_lt, { simp only [hitting], split_ifs, { obtain ⟨j, hj₁, hj₂⟩ := h, exact (cInf_le (bdd_below.inter_of_left bdd_below_Icc) (set.mem_inter hj₁ hj₂)).trans hj₁.2 }, { exact le_rfl }, }, { rw hitting_of_lt h_lt, }, end lemma le_hitting {m : ι} (hnm : n ≤ m) (x : α) : n ≤ hitting u s n m x := begin simp only [hitting], split_ifs, { refine le_cInf _ (λ b hb, _), { obtain ⟨k, hk_Icc, hk_s⟩ := h, exact ⟨k, hk_Icc, hk_s⟩, }, { rw set.mem_inter_iff at hb, exact hb.1.1, }, }, { exact hnm }, end lemma le_hitting_of_exists {m : ι} (h_exists : ∃ j ∈ set.Icc n m, u j x ∈ s) : n ≤ hitting u s n m x := begin refine le_hitting _ x, by_contra, rw set.Icc_eq_empty_of_lt (not_le.mp h) at h_exists, simpa using h_exists, end lemma hitting_mem_Icc {m : ι} (hnm : n ≤ m) (x : α) : hitting u s n m x ∈ set.Icc n m := ⟨le_hitting hnm x, hitting_le x⟩ lemma hitting_mem_set [is_well_order ι (<)] {m : ι} (h_exists : ∃ j ∈ set.Icc n m, u j x ∈ s) : u (hitting u s n m x) x ∈ s := begin simp_rw [hitting, if_pos h_exists], have h_nonempty : (set.Icc n m ∩ {i : ι | u i x ∈ s}).nonempty, { obtain ⟨k, hk₁, hk₂⟩ := h_exists, exact ⟨k, set.mem_inter hk₁ hk₂⟩, }, have h_mem := Inf_mem h_nonempty, rw [set.mem_inter_iff] at h_mem, exact h_mem.2, end lemma hitting_le_of_mem {m : ι} (hin : n ≤ i) (him : i ≤ m) (his : u i x ∈ s) : hitting u s n m x ≤ i := begin have h_exists : ∃ k ∈ set.Icc n m, u k x ∈ s := ⟨i, ⟨hin, him⟩, his⟩, simp_rw [hitting, if_pos h_exists], exact cInf_le (bdd_below.inter_of_left bdd_below_Icc) (set.mem_inter ⟨hin, him⟩ his), end lemma hitting_le_iff_of_exists [is_well_order ι (<)] {m : ι} (h_exists : ∃ j ∈ set.Icc n m, u j x ∈ s) : hitting u s n m x ≤ i ↔ ∃ j ∈ set.Icc n i, u j x ∈ s := begin split; intro h', { exact ⟨hitting u s n m x, ⟨le_hitting_of_exists h_exists, h'⟩, hitting_mem_set h_exists⟩, }, { have h'' : ∃ k ∈ set.Icc n (min m i), u k x ∈ s, { obtain ⟨k₁, hk₁_mem, hk₁_s⟩ := h_exists, obtain ⟨k₂, hk₂_mem, hk₂_s⟩ := h', refine ⟨min k₁ k₂, ⟨le_min hk₁_mem.1 hk₂_mem.1, min_le_min hk₁_mem.2 hk₂_mem.2⟩, _⟩, exact min_rec' (λ j, u j x ∈ s) hk₁_s hk₂_s, }, obtain ⟨k, hk₁, hk₂⟩ := h'', refine le_trans _ (hk₁.2.trans (min_le_right _ _)), exact hitting_le_of_mem hk₁.1 (hk₁.2.trans (min_le_left _ _)) hk₂, }, end lemma hitting_le_iff_of_lt [is_well_order ι (<)] {m : ι} (i : ι) (hi : i < m) : hitting u s n m x ≤ i ↔ ∃ j ∈ set.Icc n i, u j x ∈ s := begin by_cases h_exists : ∃ j ∈ set.Icc n m, u j x ∈ s, { rw hitting_le_iff_of_exists h_exists, }, { simp_rw [hitting, if_neg h_exists], push_neg at h_exists, simp only [not_le.mpr hi, set.mem_Icc, false_iff, not_exists, and_imp], exact λ k hkn hki, h_exists k ⟨hkn, hki.trans hi.le⟩, }, end lemma hitting_lt_iff [is_well_order ι (<)] {m : ι} (i : ι) (hi : i ≤ m) : hitting u s n m x < i ↔ ∃ j ∈ set.Ico n i, u j x ∈ s := begin split; intro h', { have h : ∃ j ∈ set.Icc n m, u j x ∈ s, { by_contra, simp_rw [hitting, if_neg h, ← not_le] at h', exact h' hi, }, exact ⟨hitting u s n m x, ⟨le_hitting_of_exists h, h'⟩, hitting_mem_set h⟩, }, { obtain ⟨k, hk₁, hk₂⟩ := h', refine lt_of_le_of_lt _ hk₁.2, exact hitting_le_of_mem hk₁.1 (hk₁.2.le.trans hi) hk₂, }, end end inequalities /-- A discrete hitting time is a stopping time. -/ lemma hitting_is_stopping_time [conditionally_complete_linear_order ι] [is_well_order ι (<)] [encodable ι] [topological_space β] [pseudo_metrizable_space β] [measurable_space β] [borel_space β] {f : filtration ι m} {u : ι → α → β} {s : set β} {n n' : ι} (hu : adapted f u) (hs : measurable_set s) : is_stopping_time f (hitting u s n n') := begin intro i, cases le_or_lt n' i with hi hi, { have h_le : ∀ x, hitting u s n n' x ≤ i := λ x, (hitting_le x).trans hi, simp [h_le], }, { have h_set_eq_Union : {x | hitting u s n n' x ≤ i} = ⋃ j ∈ set.Icc n i, u j ⁻¹' s, { ext x, rw [set.mem_set_of_eq, hitting_le_iff_of_lt _ hi], simp only [set.mem_Icc, exists_prop, set.mem_Union, set.mem_preimage], }, rw h_set_eq_Union, exact measurable_set.Union (λ j, measurable_set.Union_Prop $ λ hj, f.mono hj.2 _ ((hu j).measurable hs)) } end lemma stopped_value_hitting_mem [conditionally_complete_linear_order ι] [is_well_order ι (<)] {u : ι → α → β} {s : set β} {n m : ι} {x : α} (h : ∃ j ∈ set.Icc n m, u j x ∈ s) : stopped_value u (hitting u s n m) x ∈ s := begin simp only [stopped_value, hitting, if_pos h], obtain ⟨j, hj₁, hj₂⟩ := h, have : Inf (set.Icc n m ∩ {i | u i x ∈ s}) ∈ set.Icc n m ∩ {i | u i x ∈ s} := Inf_mem (set.nonempty_of_mem ⟨hj₁, hj₂⟩), exact this.2, end section complete_lattice variables [complete_lattice ι] {u : ι → α → β} {s : set β} {f : filtration ι m} lemma hitting_eq_Inf (x : α) : hitting u s ⊥ ⊤ x = Inf {i : ι | u i x ∈ s} := begin simp only [hitting, set.mem_Icc, bot_le, le_top, and_self, exists_true_left, set.Icc_bot, set.Iic_top, set.univ_inter, ite_eq_left_iff, not_exists], intro h_nmem_s, symmetry, rw Inf_eq_top, exact λ i hi_mem_s, absurd hi_mem_s (h_nmem_s i), end end complete_lattice section conditionally_complete_linear_order_bot variables [conditionally_complete_linear_order_bot ι] [is_well_order ι (<)] variables {u : ι → α → β} {s : set β} {f : filtration ℕ m} lemma hitting_bot_le_iff {i n : ι} {x : α} (hx : ∃ j, j ≤ n ∧ u j x ∈ s) : hitting u s ⊥ n x ≤ i ↔ ∃ j ≤ i, u j x ∈ s := begin cases lt_or_le i n with hi hi, { rw hitting_le_iff_of_lt _ hi, simp, }, { simp only [(hitting_le x).trans hi, true_iff], obtain ⟨j, hj₁, hj₂⟩ := hx, exact ⟨j, hj₁.trans hi, hj₂⟩, }, end end conditionally_complete_linear_order_bot end measure_theory
d438ecd96e5d83d0f6a2cfd26f861a8dac9c87e8
c3e8fac5ab7ca328e55bccf82a0207a97f96678c
/lean/src/byte.lean
f9ee72054506eb553022522d599dd5a7e51e437b
[ "Unlicense" ]
permissive
Rotsor/brainfuck
941bb33862ce3e9d61f0454db5ca02942f4b5775
3e6f30f298b8ba76d0bc71b8b5a47cedaf2f0b97
refs/heads/master
1,619,718,778,100
1,532,913,653,000
1,532,913,653,000
121,682,141
0
0
null
null
null
null
UTF-8
Lean
false
false
425
lean
def byte := fin 256 instance : has_repr byte := ⟨ (λ ⟨ n, _ ⟩, repr (char.of_nat n) : (fin 256) → string) ⟩ namespace byte instance : has_add(byte) := ⟨fin.add⟩ instance : has_one(byte) := ⟨(1 : fin 256)⟩ instance : has_zero(byte) := ⟨(0 : fin 256)⟩ def increment : byte -> byte := fin.add 1 def decrement : byte -> byte := fin.add 255 def zero : byte := (0 : fin 256) end byte
424724fe027b0166d222c228932f0f2dda189df9
9dc8cecdf3c4634764a18254e94d43da07142918
/src/linear_algebra/symplectic_group.lean
a9fab15586eb14f9f55bc42894cc932afaebbd2e
[ "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
6,354
lean
/- Copyright (c) 2022 Matej Penciak. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Matej Penciak, Moritz Doll, Fabien Clery -/ import data.real.basic import linear_algebra.matrix.nonsingular_inverse /-! # The Symplectic Group This file defines the symplectic group and proves elementary properties. ## Main Definitions `matrix.J`: the canonical `2n × 2n` skew-symmetric matrix `symplectic_group`: the group of symplectic matrices ## TODO * Every symplectic matrix has determinant 1. * For `n = 1` the symplectic group coincides with the special linear group. -/ open_locale matrix variables {l R : Type*} namespace matrix variables (l) [decidable_eq l] (R) [comm_ring R] section J_matrix_lemmas /-- The matrix defining the canonical skew-symmetric bilinear form. -/ def J : matrix (l ⊕ l) (l ⊕ l) R := matrix.from_blocks 0 (-1) 1 0 @[simp] lemma J_transpose : (J l R)ᵀ = - (J l R) := begin rw [J, from_blocks_transpose, ←neg_one_smul R (from_blocks _ _ _ _), from_blocks_smul, matrix.transpose_zero, matrix.transpose_one, transpose_neg], simp [from_blocks], end variables [fintype l] lemma J_squared : (J l R) ⬝ (J l R) = -1 := begin rw [J, from_blocks_multiply], simp only [matrix.zero_mul, matrix.neg_mul, zero_add, neg_zero', matrix.one_mul, add_zero], rw [← neg_zero, ← matrix.from_blocks_neg, ← from_blocks_one], end lemma J_inv : (J l R)⁻¹ = -(J l R) := begin refine matrix.inv_eq_right_inv _, rw [matrix.mul_neg, J_squared], exact neg_neg 1, end lemma J_det_mul_J_det : (det (J l R)) * (det (J l R)) = 1 := begin rw [←det_mul, J_squared], rw [←one_smul R (-1 : matrix _ _ R)], rw [smul_neg, ←neg_smul, det_smul], simp only [fintype.card_sum, det_one, mul_one], apply even.neg_one_pow, exact even_add_self _ end lemma is_unit_det_J : is_unit (det (J l R)) := is_unit_iff_exists_inv.mpr ⟨det (J l R), J_det_mul_J_det _ _⟩ end J_matrix_lemmas variable [fintype l] /-- The group of symplectic matrices over a ring `R`. -/ def symplectic_group : submonoid (matrix (l ⊕ l) (l ⊕ l) R) := { carrier := { A | A ⬝ (J l R) ⬝ Aᵀ = J l R}, mul_mem' := begin intros a b ha hb, simp only [mul_eq_mul, set.mem_set_of_eq, transpose_mul] at *, rw [←matrix.mul_assoc, a.mul_assoc, a.mul_assoc, hb], exact ha, end, one_mem' := by simp } end matrix namespace symplectic_group variables {l} {R} [decidable_eq l] [fintype l] [comm_ring R] open matrix lemma mem_iff {A : matrix (l ⊕ l) (l ⊕ l) R} : A ∈ symplectic_group l R ↔ A ⬝ (J l R) ⬝ Aᵀ = J l R := by simp [symplectic_group] instance coe_matrix : has_coe (symplectic_group l R) (matrix (l ⊕ l) (l ⊕ l) R) := by apply_instance section symplectic_J variables (l) (R) lemma J_mem : (J l R) ∈ symplectic_group l R := begin rw [mem_iff, J, from_blocks_multiply, from_blocks_transpose, from_blocks_multiply], simp, end /-- The canonical skew-symmetric matrix as an element in the symplectic group. -/ def sym_J : symplectic_group l R := ⟨J l R, J_mem l R⟩ variables {l} {R} @[simp] lemma coe_J : ↑(sym_J l R) = J l R := rfl end symplectic_J variables {R} {A : matrix (l ⊕ l) (l ⊕ l) R} lemma neg_mem (h : A ∈ symplectic_group l R) : -A ∈ symplectic_group l R := begin rw mem_iff at h ⊢, simp [h], end lemma symplectic_det (hA : A ∈ symplectic_group l R) : is_unit $ det A := begin rw is_unit_iff_exists_inv, use A.det, refine (is_unit_det_J l R).mul_left_cancel _, rw [mul_one], rw mem_iff at hA, apply_fun det at hA, simp only [det_mul, det_transpose] at hA, rw [mul_comm A.det, mul_assoc] at hA, exact hA, end lemma transpose_mem (hA : A ∈ symplectic_group l R) : Aᵀ ∈ symplectic_group l R := begin rw mem_iff at ⊢ hA, rw transpose_transpose, have huA := symplectic_det hA, have huAT : is_unit (Aᵀ).det := begin rw matrix.det_transpose, exact huA, end, calc Aᵀ ⬝ J l R ⬝ A = - Aᵀ ⬝ (J l R)⁻¹ ⬝ A : by {rw J_inv, simp} ... = - Aᵀ ⬝ (A ⬝ J l R ⬝ Aᵀ)⁻¹ ⬝ A : by rw hA ... = - (Aᵀ ⬝ (Aᵀ⁻¹ ⬝ (J l R)⁻¹)) ⬝ A⁻¹ ⬝ A : by simp only [matrix.mul_inv_rev, matrix.mul_assoc, matrix.neg_mul] ... = - (J l R)⁻¹ : by rw [mul_nonsing_inv_cancel_left _ _ huAT, nonsing_inv_mul_cancel_right _ _ huA] ... = (J l R) : by simp [J_inv] end @[simp] lemma transpose_mem_iff : Aᵀ ∈ symplectic_group l R ↔ A ∈ symplectic_group l R := ⟨λ hA, by simpa using transpose_mem hA , transpose_mem⟩ lemma mem_iff' : A ∈ symplectic_group l R ↔ Aᵀ ⬝ (J l R) ⬝ A = J l R := by rw [←transpose_mem_iff, mem_iff, transpose_transpose] instance : has_inv (symplectic_group l R) := { inv := λ A, ⟨- (J l R) ⬝ (A : matrix (l ⊕ l) (l ⊕ l) R)ᵀ ⬝ (J l R), mul_mem (mul_mem (neg_mem $ J_mem _ _) $ transpose_mem A.2) $ J_mem _ _⟩ } lemma coe_inv (A : symplectic_group l R) : (↑(A⁻¹) : matrix _ _ _) = - J l R ⬝ (↑A)ᵀ ⬝ J l R := rfl lemma inv_left_mul_aux (hA : A ∈ symplectic_group l R) : -(J l R ⬝ Aᵀ ⬝ J l R ⬝ A) = 1 := calc -(J l R ⬝ Aᵀ ⬝ J l R ⬝ A) = - J l R ⬝ (Aᵀ ⬝ J l R ⬝ A) : by simp only [matrix.mul_assoc, matrix.neg_mul] ... = - J l R ⬝ J l R : by {rw mem_iff' at hA, rw hA} ... = (-1 : R) • (J l R ⬝ J l R) : by simp only [matrix.neg_mul, neg_smul, one_smul] ... = (-1 : R) • -1 : by rw J_squared ... = 1 : by simp only [neg_smul_neg, one_smul] lemma coe_inv' (A : symplectic_group l R) : (↑(A⁻¹) : matrix (l ⊕ l) (l ⊕ l) R) = A⁻¹ := begin refine (coe_inv A).trans (inv_eq_left_inv _).symm, simp [inv_left_mul_aux, coe_inv], end lemma inv_eq_symplectic_inv (A : matrix (l ⊕ l) (l ⊕ l) R) (hA : A ∈ symplectic_group l R) : A⁻¹ = - (J l R) ⬝ Aᵀ ⬝ (J l R) := inv_eq_left_inv (by simp only [matrix.neg_mul, inv_left_mul_aux hA]) instance : group (symplectic_group l R) := { mul_left_inv := λ A, begin apply subtype.ext, simp only [submonoid.coe_one, submonoid.coe_mul, matrix.neg_mul, coe_inv], rw [matrix.mul_eq_mul, matrix.neg_mul], exact inv_left_mul_aux A.2, end, .. symplectic_group.has_inv, .. submonoid.to_monoid _ } end symplectic_group
c19e8f2683b233b0fc978ddae7c58d17ff48fb51
b82c5bb4c3b618c23ba67764bc3e93f4999a1a39
/src/formal_ml/analytic_function.lean
402149cb1bccd2628ff2a5e5ec24c6d52f33af2f
[ "Apache-2.0" ]
permissive
nouretienne/formal-ml
83c4261016955bf9bcb55bd32b4f2621b44163e0
40b6da3b6e875f47412d50c7cd97936cb5091a2b
refs/heads/master
1,671,216,448,724
1,600,472,285,000
1,600,472,285,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
136,694
lean
/- Copyright 2020 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -/ import order.filter.basic import data.real.basic import topology.basic import topology.instances.real import data.complex.exponential import topology.algebra.infinite_sum import data.nat.basic import analysis.specific_limits import analysis.calculus.deriv import analysis.asymptotics import formal_ml.sum import formal_ml.real import formal_ml.classical_deriv import formal_ml.classical_limit import formal_ml.classical lemma tsum_replace {α β:Type*} [add_comm_monoid α] [topological_space α] [t2_space α] {g:β → α} {x:α}: (has_sum g x) → tsum g = x := begin intro A1, have A2:summable g := exists.intro x A1, have A3:=has_sum_tsum A2, apply has_sum.unique A3 A1, end ---Results about sums-------------------------------------------------------------------------- /- Analytic functions are based upon conditional (as opposed to absolute) sums. -/ def has_conditional_sum (f:ℕ → ℝ) (x:ℝ):Prop := has_classical_limit (λ n:ℕ, (finset.range n).sum f) x lemma has_conditional_sum_def (f:ℕ → ℝ) (x:ℝ): has_conditional_sum f x = has_classical_limit (λ n:ℕ, (finset.range n).sum f) x := rfl def conditionally_summable (f:ℕ → ℝ):Prop := ∃ x, has_conditional_sum f x --This is equivalent to summable. def absolutely_summable (f:ℕ → ℝ):Prop := conditionally_summable (λ y, abs (f y)) def has_absolute_sum (f:ℕ → ℝ) (x:ℝ):Prop := absolutely_summable f ∧ has_conditional_sum f x section functional_def local attribute [instance] classical.prop_decidable noncomputable def conditional_sum (f:ℕ → ℝ):ℝ := if h:conditionally_summable f then classical.some h else 0 lemma conditional_sum_def (f:ℕ → ℝ): conditional_sum f = classical_limit (λ n:ℕ, (finset.range n).sum f) := rfl noncomputable def absolute_sum (f:ℕ → ℝ):ℝ := if absolutely_summable f then (conditional_sum f) else 0 end functional_def notation `∑ₚ ` binders `, ` r:(scoped f, conditional_sum f) := r lemma has_conditional_sum_unique {f:ℕ → ℝ} {x y:ℝ}: has_conditional_sum f x → has_conditional_sum f y → (x = y) := begin unfold has_conditional_sum, apply has_classical_limit_unique, end lemma conditionally_summable_elim {f:ℕ → ℝ}: conditionally_summable f → (∃ x:ℝ, has_conditional_sum f x) := begin unfold conditionally_summable, intro A1, apply A1, end lemma conditionally_summable_of_has_conditional_sum {f:ℕ → ℝ} {x:ℝ}: has_conditional_sum f x → conditionally_summable f := begin intro A1, unfold conditionally_summable, apply exists.intro x A1, end lemma has_conditional_sum_conditional_sum_of_conditionally_summable {f:ℕ → ℝ}: conditionally_summable f → has_conditional_sum f (conditional_sum f) := begin intro A1, simp [A1, conditional_sum], apply classical.some_spec A1, end lemma conditional_sum_replace {f:ℕ → ℝ} {x:ℝ}: has_conditional_sum f x → conditional_sum f = x := begin intro A1, have A2:conditionally_summable f := conditionally_summable_of_has_conditional_sum A1, have A3:has_conditional_sum f (conditional_sum f) := has_conditional_sum_conditional_sum_of_conditionally_summable A2, apply has_conditional_sum_unique A3 A1, end lemma absolutely_summable_of_nonneg_of_conditionally_summable {f:ℕ → ℝ}: 0 ≤ f → conditionally_summable f → absolutely_summable f := begin intros A1 A2, unfold absolutely_summable, have A3:(λ (y : ℕ), abs (f y)) = f, { ext y, rw abs_of_nonneg, apply A1, }, rw A3, apply A2, end lemma has_conditional_sum_neg {f:ℕ → ℝ} {x:ℝ}: has_conditional_sum f x → has_conditional_sum (-f) (-x) := begin unfold has_conditional_sum, intro A1, have A2:(λ (n:ℕ),finset.sum (finset.range n) (-f)) = -(λ (n:ℕ),finset.sum (finset.range n) (f)), { ext n, simp, }, rw A2, apply has_classical_limit_neg, apply A1, end lemma conditionally_summable_neg {f:ℕ → ℝ}: conditionally_summable f → conditionally_summable (-f) := begin unfold conditionally_summable, intro A1, cases A1 with x A2, apply exists.intro (-x), apply has_conditional_sum_neg, apply A2, end lemma conditionally_summable_neg2 {f:ℕ → ℝ}: conditionally_summable (-f) → conditionally_summable (f) := begin intro A1, have A2:-(-f)=f := neg_neg_func, rw ← A2, apply conditionally_summable_neg A1, end lemma conditional_sum_neg_of_conditionally_summable {f g:ℕ → ℝ}: conditionally_summable f → conditional_sum (-f) = -(conditional_sum f) := begin intro A1, cases (conditionally_summable_elim A1) with x A2, have A3:=has_conditional_sum_neg A2, rw conditional_sum_replace A2, rw conditional_sum_replace A3, end lemma has_conditional_sum_add {f g:ℕ → ℝ} {x y:ℝ}: has_conditional_sum f x → has_conditional_sum g y → has_conditional_sum (f + g) (x + y) := begin unfold has_conditional_sum, intros A1 A2, have A3: (λ (n : ℕ), finset.sum (finset.range n) (f + g)) = (λ (n : ℕ), finset.sum (finset.range n) (f)) + (λ (n : ℕ), finset.sum (finset.range n) (g)), { ext n, simp, rw finset.sum_add_distrib, }, rw A3, apply has_classical_limit_sum;assumption, end lemma conditionally_summable_add {f g:ℕ → ℝ}: conditionally_summable f → conditionally_summable g → conditionally_summable (f + g) := begin unfold conditionally_summable, intros A1 A2, cases A1 with x A3, cases A2 with y A4, apply exists.intro (x + y), apply has_conditional_sum_add A3 A4, end lemma conditional_sum_add_of_conditionally_summable {f g:ℕ → ℝ}: conditionally_summable f → conditionally_summable g → conditional_sum (f + g) = conditional_sum f + conditional_sum g := begin intros A1 A2, cases (conditionally_summable_elim A1) with x A3, cases (conditionally_summable_elim A2) with y A4, have A5:conditionally_summable (f + g) := conditionally_summable_add A1 A2, have A6:=has_conditional_sum_add A3 A4, rw conditional_sum_replace A6, rw conditional_sum_replace A3, rw conditional_sum_replace A4, end lemma has_conditional_sum_sub {f g:ℕ → ℝ} {x y:ℝ}: has_conditional_sum f x → has_conditional_sum g y → has_conditional_sum (f - g) (x - y) := begin unfold has_conditional_sum, intros A1 A2, have A3:(λ (n : ℕ), finset.sum (finset.range n) (f - g))= (λ (n : ℕ), ((finset.range n).sum f) - ((finset.range n).sum g)), { ext n, have A3A:(λ m:ℕ, f m - g m) = f - g, { refl, }, rw ← A3A, rw @finset.sum_sub_distrib ℕ ℝ (finset.range n) f g, }, rw A3, apply has_classical_limit_sub A1 A2, end lemma conditionally_summable_sub {f g:ℕ → ℝ}: conditionally_summable f → conditionally_summable g → conditionally_summable (f - g) := begin intros A1 A2, cases (conditionally_summable_elim A1) with x A3, cases (conditionally_summable_elim A2) with y A4, apply exists.intro (x - y), apply has_conditional_sum_sub A3 A4, end lemma conditional_sum_sub_of_conditionally_summable {f g:ℕ → ℝ}: conditionally_summable f → conditionally_summable g → conditional_sum (f - g) = conditional_sum f - conditional_sum g := begin intros A1 A2, cases (conditionally_summable_elim A1) with x A3, cases (conditionally_summable_elim A2) with y A4, have A5:conditionally_summable (f + g) := conditionally_summable_add A1 A2, have A6:=has_conditional_sum_sub A3 A4, rw conditional_sum_replace A6, rw conditional_sum_replace A3, rw conditional_sum_replace A4, end lemma has_conditional_sum_mul_const_of_has_conditional_sum {f:ℕ → ℝ} {x k:ℝ}: has_conditional_sum f x → has_conditional_sum (λ n, k * (f n)) (k * x) := begin rw has_conditional_sum_def, rw has_conditional_sum_def, intro A1, have A2:(λ n, finset.sum (finset.range n) (λ (m:ℕ), k * f m)) = (λ n, k * finset.sum (finset.range n) (λ (m:ℕ), f m)), { ext n, rw finset_sum_mul_const, }, rw A2, apply has_classical_limit_mul_const A1, end lemma conditional_summable_mul_const_of_conditionally_summable {f:ℕ → ℝ} {k:ℝ}: conditionally_summable f → conditionally_summable (λ n, k * (f n)) := begin intro A1, cases (conditionally_summable_elim A1) with x A2, apply exists.intro (k * x), apply has_conditional_sum_mul_const_of_has_conditional_sum A2, end lemma conditional_sum_mul_const_of_conditionally_summable {f:ℕ → ℝ} {k:ℝ}: conditionally_summable f → conditional_sum (λ n, k * (f n)) = k * conditional_sum f := begin intro A1, cases (conditionally_summable_elim A1) with x A2, have A3:=@has_conditional_sum_mul_const_of_has_conditional_sum f x k A2, rw conditional_sum_replace A3, rw conditional_sum_replace A2, end lemma has_conditional_sum_zero: has_conditional_sum 0 0 := begin rw has_conditional_sum_def, simp, apply has_classical_limit_const, end lemma absolutely_summable_neg (f:ℕ → ℝ): absolutely_summable f → absolutely_summable (-f) := begin unfold absolutely_summable, intro A1, have A2:(λ (y : ℕ), abs (f y)) = (λ (y : ℕ), abs ((-f) y)), { ext n, simp, }, rw ← A2, apply A1, end lemma absolutely_summable_of_nonpos_of_conditionally_summable (f:ℕ → ℝ): f ≤ 0 → conditionally_summable f → absolutely_summable f := begin intros A1 A2, have A3:conditionally_summable (-f), { apply conditionally_summable_neg, apply A2, }, unfold absolutely_summable, have A4:(λ (y : ℕ), abs (f y)) = -f, { ext, apply abs_of_nonpos, apply A1, }, rw A4, apply A3, end lemma monotone_partial_sum_of_nonneg {f:ℕ → ℝ}: 0≤ f → monotone (λ n:ℕ, (finset.range n).sum f) := begin intro A1, have A2:(λ n:ℕ, (finset.range n).sum f) = (λ S:finset ℕ, S.sum f) ∘ finset.range := rfl, rw A2, apply monotone.comp, apply sum_monotone_of_nonneg A1, apply finset.range_mono, end lemma upper_bound_finset_range {S:finset ℕ}: S ⊆ finset.range (nat.succ (finset.sup S id)) := begin rw finset.subset_iff, intros x A1, simp, apply nat.lt_succ_of_le, have A2:id x = x := rfl, rw ← A2, apply finset.le_sup A1, end lemma bdd_above_iff_bdd_above_of_nonneg {f:ℕ → ℝ}: 0≤ f → (bdd_above (set.range (λ n:ℕ, (finset.range n).sum f)) ↔ bdd_above (set.range (λ S:finset ℕ, S.sum f))) := begin intro A1, unfold bdd_above upper_bounds, split; intro A2; cases A2 with x A3; apply exists.intro x; simp; simp at A3; intros z, { intros S A4, subst z, let n := nat.succ ((finset.sup S) id), let T := finset.range n, begin have A5:finset.sum T f = finset.sum T f := rfl, have A6 := A3 n A5, have A7: S ≤ T := upper_bound_finset_range, have A8:= sum_monotone_of_nonneg A1 A7, apply le_trans, apply A8, apply A6, end }, { intros n A4, subst z, have A5:finset.sum (finset.range n) f = finset.sum (finset.range n) f := rfl, apply A3 (finset.range n) A5, } end lemma cSup_le_cSup2 {A B:set ℝ}:(A.nonempty) → (bdd_above B) → (∀ a∈ A, ∃ b∈ B, a ≤ b) → Sup A ≤ Sup B := begin intros A1 A2 A3, apply cSup_le A1, intros a A4, have A5:=A3 a A4, cases A5 with b A6, cases A6 with A7 A8, apply le_trans A8, apply le_cSup A2 A7, end lemma supr_eq_supr_of_nonneg_of_bdd_above {f:ℕ → ℝ}: 0≤ f → (bdd_above (set.range (λ n:ℕ, (finset.range n).sum f))) → (⨆ n, (finset.range n).sum f) = (⨆ (S:finset ℕ), S.sum f) := begin intros A1 A2, have A3:bdd_above (set.range (λ S:finset ℕ, S.sum f)) := (bdd_above_iff_bdd_above_of_nonneg A1).mp A2, unfold supr, apply le_antisymm, { apply cSup_le_cSup2 _ A3, { intros a A4, simp at A4, cases A4 with n A5, subst a, apply exists.intro (finset.sum (finset.range n) f), simp, }, { apply set_range_inhabited_domain, } }, { apply cSup_le_cSup2 _ A2, { intros a A4, simp at A4, cases A4 with S A5, subst a, let n := nat.succ ((finset.sup S) id), let T := finset.range n, begin have A5:finset.sum T f = finset.sum T f := rfl, apply exists.intro (finset.sum T f), split, { simp, }, { apply sum_monotone_of_nonneg A1, apply upper_bound_finset_range, } end }, { apply set_range_inhabited_domain, } } end lemma supr_eq_supr_of_nonneg_of_bdd_above2 {f:ℕ → ℝ}: 0≤ f → bdd_above (set.range (λ S:finset ℕ, S.sum f)) → (⨆ n, (finset.range n).sum f) = (⨆ (S:finset ℕ), S.sum f) := begin intros A1 A2, have A3:(bdd_above (set.range (λ n:ℕ, (finset.range n).sum f))) := (bdd_above_iff_bdd_above_of_nonneg A1).mpr A2, apply supr_eq_supr_of_nonneg_of_bdd_above A1 A3, end lemma has_conditional_sum_iff_has_sum_of_nonneg {f:ℕ → ℝ} {x:ℝ}: 0 ≤ f → (has_conditional_sum f x ↔ has_sum f x) := begin intro A1, have A1B := monotone_partial_sum_of_nonneg A1, split;intro A2, { unfold has_conditional_sum at A2, have A3:=exists_classical_limit_intro A2, have A4:=has_classical_limit_supr_of_monotone_of_exists_classical_limit A1B A3, have A5:=bdd_above_of_exists_classical_limit A3, have A6:bdd_above (set.range (λ S:finset ℕ, S.sum f)) := (bdd_above_iff_bdd_above_of_nonneg A1).mp A5, apply has_sum_of_bdd_above A1 A6, have A8:=has_classical_limit_supr_of_monotone_of_exists_classical_limit A1B A3, have A9:=supr_eq_supr_of_nonneg_of_bdd_above2 A1 A6, rw ← A9, apply has_classical_limit_unique A2 A8, }, { unfold has_conditional_sum, have A3:=bdd_above_of_has_sum A1 A2, have A4:= (bdd_above_iff_bdd_above_of_nonneg A1).mpr A3, have A5:=supr_eq_supr_of_nonneg_of_bdd_above2 A1 A3, have A6:=supr_of_has_sum A1 A2, subst x, rw ← A5, apply has_classical_limit_supr_of_monotone_of_bdd_above A1B A4, } end lemma has_conditional_sum_iff_has_sum_of_nonpos {f:ℕ → ℝ} {x:ℝ}: f ≤ 0 → (has_conditional_sum f x ↔ has_sum f x) := begin intro A1, have A3:-(-f)=f:=neg_neg_func, have A3B:-(-x)=x, { simp, }, have A1B:0 ≤ -f := nonpos_iff_neg_nonneg_func.mp A1, split;intros A2, { rw ← A3, rw ← A3B, apply has_sum.neg, have A4:has_conditional_sum (-f) (-x), { apply has_conditional_sum_neg A2, }, apply (has_conditional_sum_iff_has_sum_of_nonneg A1B).mp A4, }, { rw ← A3, rw ← A3B, have A4:has_sum (-f) (-x), { apply has_sum.neg A2, }, have A5:has_conditional_sum (-f) (-x), { apply (has_conditional_sum_iff_has_sum_of_nonneg A1B).mpr A4, }, apply has_conditional_sum_neg A5, } end lemma conditionally_summable_iff_summable_of_nonneg {f:ℕ → ℝ}: 0 ≤ f → (conditionally_summable f ↔ summable f) := begin intro A1, unfold conditionally_summable, unfold summable, split;intros A2;cases A2 with x A3;apply exists.intro x, { apply (has_conditional_sum_iff_has_sum_of_nonneg A1).mp A3, }, { apply (has_conditional_sum_iff_has_sum_of_nonneg A1).mpr A3, } end lemma conditionally_summable_iff_summable_of_nonpos {f:ℕ → ℝ}: f ≤ 0 → (conditionally_summable f ↔ summable f) := begin intro A1, have A1B:0 ≤ -f := nonpos_iff_neg_nonneg_func.mp A1, split;intros A2, { have A3:-(-f)=f:=neg_neg_func, rw ← A3, apply summable.neg, have A4:conditionally_summable (-f), { apply conditionally_summable_neg A2, }, apply (conditionally_summable_iff_summable_of_nonneg A1B).mp A4, }, { have A3:-(-f)=f:=neg_neg_func, have A4:summable (-f), { apply summable.neg A2, }, have A5:conditionally_summable (-f), { apply (conditionally_summable_iff_summable_of_nonneg A1B).mpr A4, }, apply conditionally_summable_neg2 A5, } end lemma finset_range_diff {n:ℕ} {f:ℕ → ℝ}: finset.sum (finset.range (nat.succ n)) f - (finset.sum (finset.range n) f) = f n := begin rw finset.range_succ, simp, end lemma conditionally_summable_bound (f g:ℕ → ℝ): (0 ≤ f) → (f ≤ g) → conditionally_summable g → conditionally_summable f := begin intros A1 A2 A3, have A4:0≤ g, { apply le_func_trans A1 A2, }, have A5:=(conditionally_summable_iff_summable_of_nonneg A4).mp A3, rw (conditionally_summable_iff_summable_of_nonneg A1), apply has_sum_of_le_of_le_of_has_sum A1 A2 A5, end lemma has_classical_limit_of_conditional_summable (f:ℕ → ℝ): conditionally_summable f → has_classical_limit f 0 := begin intro A1, unfold conditionally_summable at A1, unfold has_classical_limit, intros ε A2, cases A1 with x A3, unfold has_conditional_sum at A3, unfold has_classical_limit at A3, have A4:0 < (ε/2) := zero_lt_epsilon_half_of_zero_lt_epsilon A2, have A5:= A3 (ε/2) A4, cases A5 with n A6, apply exists.intro n, intros n' A7, --rw abs_lt_iff_in_Ioo, have A9:= A6 n' A7, have A10:n < nat.succ n', { apply lt_trans, apply A7, apply nat.lt_succ_self, }, have A11:= A6 (nat.succ n') A10, have A12:abs (finset.sum (finset.range (nat.succ n')) f - finset.sum (finset.range n') f) ≤ abs (finset.sum (finset.range (nat.succ n')) f - x) + abs (finset.sum (finset.range n') f - x), { rw @abs_antisymm (finset.sum (finset.range n') f) x, apply abs_triangle, }, rw finset_range_diff at A12, simp, apply lt_of_le_of_lt A12, have A13:ε/2 + ε/2 = ε, { simp, }, rw ← A13, apply lt_trans, apply add_lt_add_left A9, apply add_lt_add_right A11, end lemma has_conditional_sum_le {f g:ℕ → ℝ} {x y:ℝ}: has_conditional_sum f x → has_conditional_sum g y → f ≤ g → x ≤ y := begin intros A1 A2 A3, apply has_classical_limit_le, { rw has_conditional_sum_def at A1, apply A1, }, { rw has_conditional_sum_def at A2, apply A2, }, { rw le_func_def, intro n, apply finset_sum_le3 A3, }, end lemma has_conditional_sum_nonneg {f:ℕ → ℝ} {x:ℝ}: has_conditional_sum f x → 0 ≤ f → 0 ≤ x := begin intro A1, apply has_conditional_sum_le, apply has_conditional_sum_zero, apply A1, end lemma abs_nonneg_func {f:ℕ → ℝ}:0 ≤ abs ∘ f := begin rw le_func_def, intro n, apply abs_nonneg, end lemma absolutely_summable_def {f:ℕ → ℝ}:absolutely_summable f ↔ summable f := begin symmetry, apply iff.trans, apply summable_iff_abs_summable, unfold absolutely_summable, symmetry, apply conditionally_summable_iff_summable_of_nonneg, apply abs_nonneg_func, end lemma conditionally_summable_of_absolutely_summable {f:ℕ → ℝ}: absolutely_summable f → conditionally_summable f := begin intro A1, have A2:summable f, { apply absolutely_summable_def.mp A1, }, have A3:(summable (pos_only ∘ f) ∧ summable (neg_only ∘ f)), { apply summable_iff_pos_only_summable_neg_only_summable.mp A2, }, cases A3 with A4 A5, have A7:(conditionally_summable (pos_only ∘ f)), { apply (conditionally_summable_iff_summable_of_nonneg pos_only_nonneg).mpr A4, }, have A9:(conditionally_summable (neg_only ∘ f)), { apply (conditionally_summable_iff_summable_of_nonpos neg_only_nonpos).mpr A5, }, have A10:(conditionally_summable (pos_only ∘ f + neg_only ∘ f)), { apply conditionally_summable_add A7 A9, }, rw pos_only_add_neg_only_eq at A10, apply A10, end lemma conditionally_summable_of_summable {f:ℕ → ℝ}: summable f → conditionally_summable f := begin intro A1, apply conditionally_summable_of_absolutely_summable, rw absolutely_summable_def, apply A1, end lemma summable_of_nonneg_of_conditionally_summable {f:ℕ → ℝ}: 0 ≤ f → conditionally_summable f → summable f := begin intros A1 A2, rw ← absolutely_summable_def, apply absolutely_summable_of_nonneg_of_conditionally_summable A1 A2, end lemma has_absolute_sum_absolute_sum_of_absolutely_summable (f:ℕ → ℝ): absolutely_summable f → has_absolute_sum f (absolute_sum f) := begin intro A1, split, apply A1, unfold absolute_sum, rw if_pos, apply has_conditional_sum_conditional_sum_of_conditionally_summable, apply conditionally_summable_of_absolutely_summable A1, apply A1, end lemma conditional_sum_eq_absolute_sum_of_absolutely_summable (f:ℕ → ℝ): absolutely_summable f → absolute_sum f = conditional_sum f := begin intro A1, unfold absolute_sum, rw if_pos, apply A1, end lemma has_conditional_sum_of_has_absolute_sum (f:ℕ → ℝ) (x:ℝ): has_absolute_sum f x → has_conditional_sum f x := begin intro A1, unfold has_absolute_sum at A1, cases A1 with A2 A3, apply A3, end /-This is a crazy proof. Forward: You basically prove that pos_only ∘ f and neg_only ∘ f are summable, to prove that they are conditionally summable, such that we can separate conditionally summable into positive and negative parts, so we prove that the sum is the same (whew!). has_conditional_sum_iff_has_sum_of_nonneg : ∀ {f : ℕ → ℝ} {x : ℝ}, 0 ≤ f → (has_conditional_sum f x ↔ has_sum f x) conditionally_summable_iff_summable_of_nonneg : ∀ {f : ℕ → ℝ}, 0 ≤ f → (conditionally_summable f ↔ summable f) It is likely that the return trip will be similar. Note: it also looks like I can do better by showing x exists. -/ lemma has_absolute_sum_has_conditional_sum_has_sum_of_absolutely_summable {f:ℕ → ℝ}: absolutely_summable f → ∃ x, has_absolute_sum f x ∧ has_conditional_sum f x ∧ has_sum f x := begin intro A1, have A2:summable f := absolutely_summable_def.mp A1, have A3:summable (pos_only∘f) ∧ summable (neg_only∘f) := summable_iff_pos_only_summable_neg_only_summable.mp A2, cases A3 with A4 A5, -- We could also establish has_sum x_pos and has_sum x_neg, then translate. rw ← (conditionally_summable_iff_summable_of_nonneg pos_only_nonneg) at A4, rw ← (conditionally_summable_iff_summable_of_nonpos neg_only_nonpos) at A5, have A6:pos_only ∘ f + neg_only ∘ f = f := pos_only_add_neg_only_eq, cases A4 with x_pos A8, cases A5 with x_neg A9, apply exists.intro (x_pos + x_neg), have A10:has_conditional_sum f (x_pos + x_neg), { rw ← A6, apply has_conditional_sum_add A8 A9, }, split, { -- ⊢ has_absolute_sum f (x_pos + x_neg) split, { apply A1, }, { apply A10, } }, split, { -- ⊢ has_conditional_sum f (x_pos + x_neg) apply A10, }, { rw (has_conditional_sum_iff_has_sum_of_nonneg pos_only_nonneg) at A8, rw (has_conditional_sum_iff_has_sum_of_nonpos neg_only_nonpos) at A9, rw ← A6, apply has_sum.add A8 A9, } end lemma absolutely_summable_of_has_absolute_sum {f:ℕ → ℝ} {x:ℝ}: has_absolute_sum f x → absolutely_summable f := begin unfold has_absolute_sum, intro A1, apply A1.left, end lemma has_absolute_sum_unique {f:ℕ → ℝ} {x y:ℝ}: has_absolute_sum f x → has_absolute_sum f y → x = y := begin unfold has_absolute_sum, intros A1 A2, apply has_conditional_sum_unique, { apply A1.right, }, { apply A2.right, }, end lemma absolute_sum_replace {f:ℕ → ℝ} {x:ℝ}: has_absolute_sum f x → absolute_sum f = x := begin intro A1, have A2:absolutely_summable f := absolutely_summable_of_has_absolute_sum A1, have A3:=has_absolute_sum_absolute_sum_of_absolutely_summable f A2, apply has_absolute_sum_unique A3 A1, end lemma has_absolute_sum_has_conditional_sum_has_sum_of_summable {f:ℕ → ℝ}: summable f → ∃ x, has_absolute_sum f x ∧ has_conditional_sum f x ∧ has_sum f x := begin intro A1, have A2:absolutely_summable f := absolutely_summable_def.mpr A1, apply has_absolute_sum_has_conditional_sum_has_sum_of_absolutely_summable A2, end lemma tsum_zero_of_not_summable {f:ℕ → ℝ}: ¬ (summable f) → (∑' i, f i) = 0 := begin intro A1, unfold tsum, rw dif_neg, apply A1, end lemma absolute_sum_zero_of_not_absolutely_summable {f:ℕ → ℝ}: ¬ (absolutely_summable f) → absolute_sum f = 0 := begin intro A1, unfold absolute_sum, rw if_neg, apply A1, end lemma tsum_eq_absolute_sum {f:ℕ → ℝ}: (∑' i, f i) = (absolute_sum f) := begin have A1:summable f ∨ ¬ (summable f), { apply classical.em, }, cases A1, { have A2:=has_absolute_sum_has_conditional_sum_has_sum_of_summable A1, cases A2 with x A3, cases A3 with A4 A5, cases A5 with A6 A7, rw absolute_sum_replace A4, rw tsum_replace A7, }, { rw absolute_sum_zero_of_not_absolutely_summable, rw tsum_zero_of_not_summable, apply A1, intro A2, apply A1, rw absolutely_summable_def at A2, apply A2, } end lemma tsum_eq_conditional_sum_of_summable {f:ℕ → ℝ}: summable f → (∑' i, f i) = (∑ₚ i, f i) := begin intro A1, rw tsum_eq_absolute_sum, have A2:absolutely_summable f := absolutely_summable_def.mpr A1, apply conditional_sum_eq_absolute_sum_of_absolutely_summable, apply A2, end lemma has_absolute_sum_iff_has_sum {f:ℕ → ℝ} {x:ℝ}: has_absolute_sum f x ↔ has_sum f x := begin split;intro A1, { unfold has_absolute_sum at A1, cases A1 with A2 A3, have A4 := has_absolute_sum_has_conditional_sum_has_sum_of_absolutely_summable A2, cases A4 with x2 A5, cases A5 with A6 A7, cases A7 with A8 A9, have A10:x = x2 := has_conditional_sum_unique A3 A8, subst x2, apply A9, }, { have A2:summable f := summable_intro A1, have A4 := has_absolute_sum_has_conditional_sum_has_sum_of_summable A2, cases A4 with x2 A5, cases A5 with A6 A7, cases A7 with A8 A9, have A10:x = x2 := has_sum.unique A1 A9, subst x2, apply A6, } end def has_taylor_series (f:ℕ → ℝ) (x x':ℝ) (g:ℝ → ℝ):Prop := has_conditional_sum (λ n:ℕ, (f n) * (x' - x)^n) (g x') def analytic_with_fn_at (g:ℝ → ℝ) (x:ℝ) (f:ℕ → ℝ):Prop := ∃ (V:set ℝ), (is_open V) ∧ (∀ y∈ V, has_taylor_series f x y g) def analytic_at (g:ℝ → ℝ) (x:ℝ):Prop := ∃ (f:ℕ → ℝ), analytic_with_fn_at g x f def analytic (g:ℝ → ℝ):Prop := ∀ x:ℝ, analytic_at g x def is_maclaurin_fn (f:ℕ → ℝ) (g:ℝ → ℝ):Prop := ∀ x:ℝ, has_conditional_sum (λ n:ℕ, f n * x^n) (g x) lemma is_maclaurin_fn_def (f:ℕ → ℝ) (g:ℝ → ℝ): is_maclaurin_fn f g = (∀ x, has_conditional_sum (λ n:ℕ, f n * x^n) (g x)) := rfl def deriv_maclaurin_series (f:ℕ → ℝ) (n:ℕ):ℝ := (f (nat.succ n)) * (nat.succ n) lemma deriv_maclaurin_series_def {f:ℕ → ℝ}: deriv_maclaurin_series f = (λ n, (f (nat.succ n)) * (nat.succ n)) := rfl /- If we are concerned with functions that are everywhere analytic, then we need this guarantee to hold. Specifically, this guarantee is necessary for the series to be bounded at x, and such a guarantee is sufficient for it to be bounded anywhere "less than" x. -/ def bounded_maclaurin (f:ℕ → ℝ):Prop := ∀ x:ℝ, has_classical_limit (λ n:ℕ, x^n * f n) 0 noncomputable def maclaurin (f:ℕ → ℝ) (x:ℝ):ℝ := conditional_sum (λ n:ℕ, f n * x^n) noncomputable def deriv_maclaurin (f:ℕ → ℝ):ℝ → ℝ := maclaurin (deriv_maclaurin_series f) --This could be expanded to an arbitrary commutative group. /- This is the key observation of analytic functions. If you go slightly further out, then you can prove that the series is converging exponentially fast. Note: prefer bounded_maclaurin_exponential. -/ lemma bounded_maclaurin_exponential {f:ℕ → ℝ} {x:ℝ}: bounded_maclaurin f → (∃ n:ℕ, ∀ n', n < n' → (abs (x^n' * (f n')) < ((2:ℝ)⁻¹)^(n'))) := begin intro A1, unfold bounded_maclaurin at A1, have A2:has_classical_limit (λ (n : ℕ), (2 * x) ^ n * f n) 0 := A1 (2 * x), unfold has_classical_limit at A2, have A3:(∃ (n : ℕ), ∀ (n' : ℕ), n < n' → abs ((2 * x) ^ n' * f n' - 0) < 1) := A2 (1:ℝ) (zero_lt_one), cases A3 with n A4, apply exists.intro n, intros n' A5, have A6:abs ((2 * x) ^ n' * f n' - 0) < 1 := A4 n' A5, simp at A6, rw pow_distrib at A6, rw mul_assoc at A6, rw abs_mul_eq_mul_abs at A6, --rw mul_assoc at A7, rw mul_comm at A6, rw ← lt_div_iff at A6, rw div_inv at A6, rw inv_exp at A6, apply A6, apply pow_two_pos, apply le_of_lt, apply pow_two_pos, end /- This is a more useful variant. -/ lemma bounded_maclaurin_exponential2 {f:ℕ → ℝ} {x:ℝ}: bounded_maclaurin f → (∃ n:ℕ, ∀ n', n < n' → (abs ((f n') * x^n') < ((2:ℝ)⁻¹)^(n'))) := begin intro A1, have A2: (∃ n:ℕ, ∀ n', n < n' → (abs (x^n' * (f n')) < ((2:ℝ)⁻¹)^(n'))), { apply bounded_maclaurin_exponential, apply A1, }, cases A2 with n A3, apply exists.intro n, intros n' A4, have A5 := A3 n' A4, rw mul_comm, apply A5, end lemma abs_nonneg_func2 {f:ℕ → ℝ}:0 ≤ f → (f = (abs ∘ f)) := begin intro A1, ext n, simp, rw abs_of_nonneg, apply A1, end lemma absolutely_summable_bound2 {f:ℕ → ℝ} {g:ℕ → ℝ}: absolutely_summable g → (abs ∘ f) ≤ g → absolutely_summable f := begin unfold absolutely_summable, intros A1 A2, have A3:=@abs_nonneg_func f, have A4:=le_trans A3 A2, have A5:=abs_nonneg_func2 A4, have A6:abs ∘ f ≤ abs ∘ g, { rw ← A5, apply A2, }, apply conditionally_summable_bound (abs ∘ f) (abs ∘ g) A3 A6, apply A1, end lemma range_sum_eq_after {f g:ℕ → ℝ} {n:ℕ}: (∀ n':ℕ, n < n' → (f n' = g n')) → (∀ n':ℕ, n < n' → finset.sum (finset.range n') f + (finset.sum (finset.range (nat.succ n)) g + -finset.sum (finset.range (nat.succ n)) f) = finset.sum (finset.range n') g) := begin intros A1 n', induction n', { intro A2, exfalso, apply nat.not_lt_zero n A2, }, { intro A2, have A3:(n'_n = n)∨ (n'_n ≠ n) := eq_or_ne, cases A3, { subst n'_n, simp, }, { have A4:n < n'_n, { have A4A:n ≤ n'_n := nat.le_of_lt_succ A2, apply lt_of_le_of_ne A4A, symmetry, apply A3, }, have A5:= n'_ih A4, have A6:(finset.range (nat.succ n'_n)).sum f = f n'_n + (finset.range (n'_n)).sum f, { rw finset.range_succ, rw finset.sum_insert, simp, }, have A7:(finset.range (nat.succ n'_n)).sum g = g n'_n + (finset.range (n'_n)).sum g, { rw finset.range_succ, rw finset.sum_insert, simp, }, rw A6, rw A7, rw ← A5, rw A1 n'_n A4, rw add_assoc, } } end lemma has_conditional_sum_eq_after {f g:ℕ → ℝ} {x:ℝ} {n:ℕ}: has_conditional_sum f x → (∀ n':ℕ, n < n' → (f n' = g n')) → has_conditional_sum g (x + (finset.range (nat.succ n)).sum g - (finset.range (nat.succ n)).sum f) := begin rw has_conditional_sum_def, rw has_conditional_sum_def, intros A1 A2, let z:=(finset.range (nat.succ n)).sum g - (finset.range (nat.succ n)).sum f, let h:=λ n':ℕ, z + (finset.range n').sum f, begin have A3:z=(finset.range (nat.succ n)).sum g - (finset.range (nat.succ n)).sum f := rfl, have A4:h=λ n':ℕ, z + (finset.range n').sum f := rfl, have A5:h=(λ n':ℕ, z) + λ n':ℕ, (finset.range n').sum f := rfl, have A6:has_classical_limit h (z + x), { rw A5, apply has_classical_limit_sum, apply has_classical_limit_const, apply A1, }, have A7:(∀ n':ℕ, n < n' → (h n' = (λ n'':ℕ, (finset.range n'').sum g) n')), { intros n' A7A, rw A4, rw A3, simp, rw sub_eq_add_neg, rw add_comm, apply range_sum_eq_after A2, apply A7A, }, have A8:(x + finset.sum (finset.range (nat.succ n)) g - finset.sum (finset.range (nat.succ n)) f) = z + x, { rw add_sub_assoc, rw ← A3, rw add_comm, }, rw A8, apply has_classical_limit_eq_after A6 A7, end end lemma conditionally_summable_eq_after {f g:ℕ → ℝ} {n:ℕ}: conditionally_summable f → (∀ n':ℕ, n < n' → (f n' = g n')) → conditionally_summable g := begin unfold conditionally_summable, intros A1 A2, cases A1 with x A3, apply exists.intro (x + (finset.range (nat.succ n)).sum g - (finset.range (nat.succ n)).sum f), apply has_conditional_sum_eq_after A3 A2, end lemma absolutely_summable_eq_after {f g:ℕ → ℝ} {n:ℕ}: absolutely_summable f → (∀ n':ℕ, n < n' → (f n' = g n')) → absolutely_summable g := begin unfold absolutely_summable, intros A1 A2, have A3:(∀ n':ℕ, n < n' → ((abs ∘ f) n' = (abs ∘ g) n')), { intros n' A3A, simp, rw A2 n' A3A, }, apply conditionally_summable_eq_after A1 A3, end lemma absolutely_summable_bound {f:ℕ → ℝ} {g:ℕ → ℝ}: absolutely_summable g → (∃ (n:ℕ), ∀ (n':ℕ), n < n' → abs (f n') ≤ g n') → absolutely_summable f := begin intros A1 A2, cases A2 with n A3, let h:ℕ → ℝ := λ n':ℕ, if n < n' then g n' else abs (f n'), begin have A4:h = λ n':ℕ, if n < n' then g n' else abs (f n') := rfl, have A5:(∀ n':ℕ, n < n' → (g n' = h n')), { intros n' A5A, rw A4, simp, rw if_pos A5A, }, have A6:abs ∘ f ≤ h, { rw le_func_def, intro n', rw A4, have A6A:(n < n') ∨ ¬ (n < n') := lt_or_not_lt, cases A6A, { simp, rw if_pos A6A, --apply le_of_lt, -- Note: this means that the bound in the lemma is loose. apply A3 n' A6A, }, { simp, rw if_neg A6A, } }, have A7:absolutely_summable h := absolutely_summable_eq_after A1 A5, apply absolutely_summable_bound2 A7 A6, end end lemma has_absolute_sum_exp {x:ℝ}: (0 ≤ x) → (x < 1) → has_absolute_sum (λ n, x^n) (1-x)⁻¹ := begin intros A1 A2, rw has_absolute_sum_iff_has_sum, apply has_sum_geometric_of_lt_1 A1 A2, end lemma has_conditional_sum_exp {x:ℝ}: (0 ≤ x) → (x < 1) → has_conditional_sum (λ n, x^n) (1-x)⁻¹ := begin intros A1 A2, apply has_conditional_sum_of_has_absolute_sum, apply has_absolute_sum_exp, apply A1, apply A2, end lemma absolutely_summable_intro {f:ℕ→ ℝ} {x:ℝ}: has_absolute_sum f x → absolutely_summable f := begin intro A1, apply A1.left end lemma absolutely_summable_exp {x:ℝ}: (0 ≤ x) → (x < 1) → absolutely_summable (λ n, x^n) := begin intros A1 A2, have A3:=has_absolute_sum_exp A1 A2, apply absolutely_summable_intro A3, end noncomputable def exp_maclaurin (n:ℕ):ℝ := ((nat.fact n):ℝ)⁻¹ lemma exp_maclaurin_def:exp_maclaurin = (λ n, ((nat.fact n):ℝ)⁻¹) := rfl lemma bounded_maclaurin_of_is_maclaurin_fn (f:ℕ → ℝ) {g:ℝ → ℝ}: is_maclaurin_fn f g → bounded_maclaurin f := begin intro A1, unfold bounded_maclaurin, intro x, unfold is_maclaurin_fn at A1, have A2 := A1 x, have A3:(λ n:ℕ, x^n * f n) = (λ n:ℕ, f n * x^n), { ext n, apply mul_comm, }, rw A3, apply has_classical_limit_of_conditional_summable, apply conditionally_summable_of_has_conditional_sum A2, end lemma is_maclaurin_fn_of_bounded_maclaurin {f:ℕ → ℝ}: bounded_maclaurin f → is_maclaurin_fn f (maclaurin f) := begin intro A1, unfold is_maclaurin_fn, intro x, unfold maclaurin, apply has_conditional_sum_conditional_sum_of_conditionally_summable, apply conditionally_summable_of_absolutely_summable, have A3:(∃ n:ℕ, ∀ n', n < n' → (abs ((f n') * x^n') < ((2:ℝ)⁻¹)^(n'))), { apply bounded_maclaurin_exponential2, apply A1, }, apply absolutely_summable_bound, { apply @absolutely_summable_exp ((2:ℝ)⁻¹), { simp, apply le_of_lt, apply zero_lt_one, }, { apply inv_lt_one_of_one_lt, apply one_lt_two, }, }, cases A3 with n A4, apply exists.intro n, intros n' A5, apply le_of_lt, apply A4 n' A5, end lemma forall_iff_forall_of_forall_iff (α:Type*) (P Q:α → Prop): (∀ a:α, P a ↔ Q a)→ ((∀ a:α, P a)↔ (∀ a:α, Q a)) := begin intros A1, split;intros A2 a;have A3 := A1 a;have A4 := A2 a, { apply A3.mp A4, }, { apply A3.mpr A4, } end lemma bounded_maclaurin_iff_bounded_maclaurin_abs {f:ℕ → ℝ}: bounded_maclaurin f ↔ bounded_maclaurin (abs ∘ f) := begin unfold bounded_maclaurin, apply forall_iff_forall_of_forall_iff, intros x, have A3:(λ n:ℕ, x^n) * f = (λ n:ℕ, x^n * f n), { ext n, simp, }, rw ← A3, have A4:(λ n:ℕ, x^n) * abs ∘ f = (λ n:ℕ, x^n * (abs ∘ f) n), { ext n, simp, }, rw ← A4, apply has_classical_limit_half_abs_zero, end -- is_maclaurin_fn f (maclaurin f) lemma absolutely_summable_of_bounded_maclaurin {f:ℕ → ℝ} {x:ℝ}: bounded_maclaurin f → (absolutely_summable (λ n:ℕ, f n * x^n )) := begin intro A1, unfold absolutely_summable, have A2:bounded_maclaurin (abs ∘ f), { rw bounded_maclaurin_iff_bounded_maclaurin_abs at A1, apply A1, }, have A3:is_maclaurin_fn (abs ∘ f) (maclaurin (abs ∘ f)) := is_maclaurin_fn_of_bounded_maclaurin A2, let x':=abs x, begin unfold is_maclaurin_fn at A3, have A4:=A3 x', have A5:x' = abs x := rfl, have A6:(λ (n : ℕ), (abs ∘ f) n * x' ^ n)= (λ (n : ℕ), abs(f n * x ^ n)), { ext n, rw A5, rw pow_abs, simp, rw abs_mul, }, rw ← A6, unfold conditionally_summable, apply exists.intro (maclaurin (abs ∘ f) x'), apply A4, end end lemma is_cau_seq_coe (f:ℕ → ℂ):is_cau_seq complex.abs f → is_cau_seq abs (λ n:ℕ, (f n).re) := begin unfold is_cau_seq, intros A1 ε A2, have A3 := A1 ε A2, cases A3 with i A4, apply exists.intro i, intros j A5, have A6 := A4 j A5, have A7:(f j).re - (f i).re = (f j - f i).re, { simp, }, rw A7, apply lt_of_le_of_lt, apply complex.abs_re_le_abs, apply A6, end /- lemma real.exp_def (x:ℝ):has_conditional_sum (λ n:ℕ, x^n * ((nat.fact n):ℝ)⁻¹) (real.exp x) := begin unfold has_conditional_sum, unfold real.exp complex.exp complex.exp', rw ← complex.lim_re, unfold cau_seq_re, rw is_cau_seq_coe, end -/ lemma cau_seq_eq2 {f:cau_seq ℝ abs} {g:ℕ → ℝ}: ⇑ f = g → has_classical_limit g f.lim := begin intro A0, have A1:=cau_seq.complete f, cases A1 with b A1, have A3:f.lim = b := cau_seq.lim_eq_of_equiv_const A1, rw A3, unfold has_classical_limit, intros ε A2A, have A2B:=cau_seq.equiv_def₃ A1 A2A, cases A2B with n A2C, apply exists.intro n, intros n' A2D, have A2E:n ≤ n' := le_of_lt A2D, have A2F := A2C n' A2E, have A2G:n' ≤ n' := le_refl n', have A2H := A2F n' A2G, rw A0 at A2H, simp at A2H, apply A2H, end lemma cau_seq_eq {f:cau_seq ℝ abs} {g:ℕ → ℝ}: ⇑ f = g → f.lim = classical_limit g := begin intro A1, apply eq_classical_limit, apply cau_seq_eq2, apply A1, end lemma cau_seq_re_val {f:cau_seq ℂ complex.abs} {n:ℕ}:((complex.cau_seq_re f) n) = (f n).re := rfl lemma complex.exp_val {z:ℂ} {n:ℕ}:(complex.exp' z) n = (finset.range n).sum (λ m, z ^ m / ((nat.fact m):ℂ)) := rfl noncomputable def complex.add_comm_monoid:add_comm_monoid ℂ := infer_instance lemma is_add_monoid_hom_complex_re_map_add : ∀ x y:ℂ, complex.re (x + y) = complex.re x + complex.re y := begin intros x y, refl end /- This probably exists somewhere. -/ def is_add_monoid_hom_complex_re:is_add_monoid_hom complex.re := { map_zero:= rfl, map_add := is_add_monoid_hom_complex_re_map_add, } lemma finset_sum_re {α:Type*} {S:finset α} [decidable_eq α] {f:α → ℂ}: (finset.sum S (λ a:α, complex.re (f a))) = complex.re (finset.sum S f) := begin apply @finset.sum_hom α ℂ ℝ complex.add_comm_monoid real.add_comm_monoid S f complex.re is_add_monoid_hom_complex_re, end lemma exp_rewrite (x:ℝ):(λ n:ℕ, x^n/((nat.fact n):ℝ)) = (λ n:ℕ, ((nat.fact n):ℝ)⁻¹ * x^n) := begin ext n, rw mul_comm, refl, end lemma bounded_maclaurin_exp_maclaurinh {x:ℝ}: has_classical_limit (λ (n : ℕ), x ^ n * ((nat.fact n):ℝ)⁻¹) 0 := begin have A1:x = 0 ∨ (x ≠ 0) := eq_or_ne, cases A1, { rw A1, have A2:∀ n', 0 < n' → (λ (n:ℕ),(0:ℝ)) n' = (λ (n : ℕ), 0 ^ n * ((nat.fact n):ℝ)⁻¹) n', { intros n' A2A, simp, left, apply A2A, }, apply has_classical_limit_eq_after (@has_classical_limit_const 0) A2, }, { rw ← has_classical_limit_abs_zero, let z := abs x, begin have A2:z = abs x := rfl, have A3:0 < z, { rw A2, apply abs_pos_of_nonzero A1, }, have A4:(abs ∘ λ (n : ℕ), x ^ n * (↑(nat.fact n))⁻¹)= (λ (n : ℕ), z ^ n * (↑(nat.fact n))⁻¹), { ext n, simp, rw abs_mul, rw abs_pow, rw abs_inv, rw real_nat_abs_coe, }, rw A4, apply has_classical_limit_ratio_lt_one2, { apply has_classical_limit_ratio_power_fact, }, { apply zero_lt_one, }, { apply le_refl, }, { intros n, apply mul_pos, apply pow_pos, apply A3, apply inv_pos_of_pos, simp, apply nat_zero_lt_of_nonzero, apply nat_fact_nonzero, }, end } end lemma bounded_maclaurin_exp_maclaurin:bounded_maclaurin exp_maclaurin := begin unfold bounded_maclaurin, unfold exp_maclaurin, apply bounded_maclaurin_exp_maclaurinh, end lemma conditionally_summable_of_bounded_maclaurin (f:ℕ → ℝ) (x:ℝ): bounded_maclaurin f → conditionally_summable (λ (n : ℕ), f n * x ^ n) := begin intro A1, unfold conditionally_summable, apply exists.intro (maclaurin f x), apply is_maclaurin_fn_of_bounded_maclaurin, apply A1, end lemma real.exp_def (x:ℝ):has_conditional_sum (λ n:ℕ, ((nat.fact n):ℝ)⁻¹ * x^n) (real.exp x) := begin unfold has_conditional_sum, unfold real.exp, unfold complex.exp, rw ← complex.lim_re, have A1:⇑(complex.cau_seq_re (complex.exp' ↑x)) = (λ n:ℕ, (finset.range n).sum (λ n:ℕ,((nat.fact n):ℝ)⁻¹ * x^n)), { ext n, rw cau_seq_re_val, rw complex.exp_val, rw ← finset_sum_re, have A1A: (λ (a : ℕ), ((x:ℂ) ^ a / ((nat.fact a):ℂ)).re) = (λ (n : ℕ), ((nat.fact n):ℝ)⁻¹ * x^n), { rw ← exp_rewrite, ext n2, rw complex_pow_div_complex_re, }, rw A1A, }, rw cau_seq_eq A1, clear A1, rw ← has_conditional_sum_def, rw ← conditional_sum_def, apply has_conditional_sum_conditional_sum_of_conditionally_summable, apply conditionally_summable_of_bounded_maclaurin, apply bounded_maclaurin_exp_maclaurin, end lemma is_maclaurin_fn_exp:is_maclaurin_fn (λ n:ℕ, ((nat.fact n):ℝ)⁻¹) real.exp := begin unfold is_maclaurin_fn, intro x, apply real.exp_def, end ----Goals ----------------------------------------------------------------------------------------- lemma zero_le_one_coe_real {n:ℕ}:(0:ℝ) ≤ (1:ℝ) + (n:ℝ) := begin have A1:(0:ℝ) ≤ (1:ℝ), { apply le_of_lt, apply zero_lt_one, }, apply le_trans A1, simp, end lemma inv_pow_succ_2 {x:ℝ} {n:ℕ}:x⁻¹ * x ^ (nat.succ (nat.succ n))=x^ (nat.succ n) := begin have A1:x = 0 ∨ (x ≠ 0) := eq_or_ne, cases A1, { subst x, simp, rw pow_zero_eq_zero_if_pos, simp, }, { rw @nat_succ_pow (nat.succ n), rw ← mul_assoc, rw inv_mul_cancel A1, rw one_mul, } end lemma is_maclaurin_derivative_bounded (f:ℕ → ℝ): bounded_maclaurin f → is_maclaurin_fn (deriv_maclaurin_series f) (maclaurin (deriv_maclaurin_series f)) := begin intro A1, apply is_maclaurin_fn_of_bounded_maclaurin, unfold deriv_maclaurin_series bounded_maclaurin, intro x, have A6B:(x = 0) ∨ (x ≠ 0) := eq_or_ne, have A2:(∃ n:ℕ, ∀ n', n < n' → (abs (x^n' * (f n')) < (2:real)⁻¹ ^(n'))) := @bounded_maclaurin_exponential _ x A1, cases A2 with n A4, have A7:has_classical_limit (λ n:ℕ, abs (x⁻¹) * (2:real)⁻¹ ^(n + 1) * (1 + (n:ℝ))) 0, { cases A6B, { subst x, simp, apply has_classical_limit_const, }, { have A7A:abs (x⁻¹) * 0 = 0 := mul_zero (abs (x⁻¹)), rw ← A7A, have A7B:(λ n:ℕ, abs (x⁻¹) * (2:real)⁻¹ ^(n + 1) * (1 + (n:ℝ)))= (λ n:ℕ, abs (x⁻¹) * ((2:real)⁻¹ ^(n + 1) * (1 + (n:ℝ)))), { ext n', rw mul_assoc, }, rw A7B, apply has_classical_limit_mul_const, have A7C:(λ n:ℕ, (2:real)⁻¹ ^(n + 1) * (1 + (n:ℝ)))= (λ m:ℕ, (m:ℝ) * (2:real)⁻¹ ^(m)) ∘ (λ n:ℕ, 1 + n), { ext n', simp, rw add_comm n' 1, apply mul_comm, }, rw A7C, rw ← @has_classical_limit_offset _ 0 1, have A7D:∀ (n : ℕ), 0 < n → 0 < (n:ℝ), { intros n A7D1, simp, apply A7D1 }, apply has_classical_limit_ratio_one_exp A7D, { apply has_classical_limit_ratio_one, }, { apply half_lt_one, }, { apply half_pos_lit, } } }, apply has_classical_limit_bound, apply A7, { apply exists.intro n, intros n' A6A, have A6B:(x = 0) ∨ (x ≠ 0) := eq_or_ne, cases A6B, { subst x, simp, left, apply lt_of_le_of_lt, apply nat.zero_le n, apply A6A, }, { simp, have A6C:(abs x) * (abs (x ^ n' * (f (nat.succ n') * (1 + ↑n')))) ≤ (abs x) * (abs (x⁻¹) * (2:real)⁻¹ ^(n' + 1) * (1 + ↑n')), { rw ← mul_assoc, rw ← mul_assoc, rw ← abs_mul, rw ← mul_assoc, rw ← mul_assoc, rw ← mul_assoc, rw ← abs_mul, rw ← nat_succ_pow, rw mul_inv_cancel A6B_1, simp, rw abs_mul, simp, have A6C1:0 ≤ (1+(n':ℝ)) := zero_le_one_coe_real, have A6C2:abs (1+(n':ℝ))=1 + (n':ℝ) := abs_of_nonneg A6C1, rw A6C2, apply mul_le_mul_of_nonneg_right _ A6C1, apply le_of_lt, have A6C3:n'+1 = nat.succ n', { simp, }, rw A6C3, rw ← inv_pow_comm, apply A4 (nat.succ n'), apply lt_trans, apply A6A, apply nat.lt_succ_self, }, rw add_comm (n':ℝ) 1, rw ← inv_pow_comm, apply le_of_mul_le_mul_left A6C, apply abs_pos_of_nonzero A6B_1, } }, end /- Sum from m to infinity -/ noncomputable def conditional_sum_lb (m:ℕ) (f:ℕ→ℝ):ℝ := conditional_sum (λ n, if (m ≤ n) then f n else 0) lemma finset_range_disjoint {n n2:ℕ}: n ≤ n2 → disjoint (finset.range n) (finset.Ico n n2) := begin intro A1, rw ← finset.Ico.zero_bot, apply finset.Ico.disjoint_consecutive, end lemma finset_range_union {n n2:ℕ}: n ≤ n2 → (finset.range n) ∪ (finset.Ico n n2) = (finset.range n2) := begin intro A1, rw ← finset.Ico.zero_bot, rw ← finset.Ico.zero_bot, apply finset.Ico.union_consecutive, { simp, }, { apply A1, } end lemma has_conditional_sum_lb_iff_has_conditional_sum {m:ℕ} {f:ℕ→ℝ} {x:ℝ}: has_conditional_sum (λ n, if (m ≤ n) then f n else 0) x ↔ has_conditional_sum (f ∘ (λ k, m + k)) x := begin rw has_conditional_sum_def, rw has_conditional_sum_def, have A1:(λ (k : ℕ), finset.sum (finset.range k) (λ (n : ℕ), ite (m ≤ n) (f n) 0)) ∘ (λ z, m + z)= (λ (n : ℕ), finset.sum (finset.range n) (f∘ (λ k, m + k))), { ext k2, have A1C:m ≤ m + k2, { simp, }, simp, rw ← @finset_range_union m (m + k2), rw finset.sum_union, have A1A:finset.sum (finset.range m) (λ (n : ℕ), ite (m ≤ n) (f n) 0) = finset.sum (finset.range m) (λ (n : ℕ), 0), { apply @finset_sum_subst2, intros s A1A1, rw if_neg, rw finset.mem_range at A1A1, apply not_le_of_lt A1A1, }, have A1B: finset.sum (finset.Ico m (m + k2)) (λ (n : ℕ), ite (m ≤ n) (f n) 0) = finset.sum (finset.Ico (0 + m) (k2 + m)) f, { rw zero_add, rw add_comm k2 m, apply @finset_sum_subst2, intros s A1B1A, rw if_pos, rw finset.Ico.mem at A1B1A, apply A1B1A.left, }, rw A1A, rw A1B, rw ← @finset.sum_Ico_add ℝ _ f 0 k2 m, rw finset.Ico.zero_bot, simp, apply finset_range_disjoint, apply A1C, apply A1C, }, rw @has_classical_limit_offset (λ (k : ℕ), finset.sum (finset.range k) (λ (n : ℕ), ite (m ≤ n) (f n) 0)) x m, rw A1, end lemma conditional_sum_zero_of_not_conditionally_summable {f:ℕ → ℝ}: ¬ (conditionally_summable f) → (conditional_sum f = 0) := begin intro A1, unfold conditional_sum, rw dif_neg, apply A1, end lemma conditional_sum_lb_inc {k:ℕ} {f:ℕ→ℝ}: conditional_sum_lb k f = conditional_sum (f ∘ (λ m, k + m)) := begin have A1:conditionally_summable (f ∘ (λ m, k + m)) ∨ ¬(conditionally_summable (f ∘ (λ m, k + m))) := classical.em (conditionally_summable (f ∘ (λ m, k + m))), cases A1, { -- conditionally_summable (f ∘ (λ m, k + m)) ⊢ ... unfold conditional_sum_lb, have B1:= has_conditional_sum_conditional_sum_of_conditionally_summable A1, have B2:= has_conditional_sum_lb_iff_has_conditional_sum.mpr B1, rw conditional_sum_replace B2, }, { -- ¬ (conditionally_summable (f ∘ (λ m, k + m))) ⊢ ... unfold conditional_sum_lb, have C1:¬conditionally_summable (λ (n : ℕ), ite (k ≤ n) (f n) 0), { intro C1A, apply A1, have C1B:= has_conditional_sum_conditional_sum_of_conditionally_summable C1A, have B2:= has_conditional_sum_lb_iff_has_conditional_sum.mp C1B, apply conditionally_summable_of_has_conditional_sum B2, }, rw conditional_sum_zero_of_not_conditionally_summable A1, rw conditional_sum_zero_of_not_conditionally_summable C1, }, end lemma injective_prod_mk_left {m:ℕ}:function.injective (λ n:ℕ, prod.mk m n) := begin have A1:function.left_inverse (@prod.snd ℕ ℕ) (λ n:ℕ, prod.mk m n), { unfold function.left_inverse, simp, }, apply function.left_inverse.injective A1, end lemma injective_prod_mk_right {n:ℕ}:function.injective (λ m:ℕ, prod.mk m n) := begin have A1:function.left_inverse (@prod.fst ℕ ℕ) (λ m:ℕ, prod.mk m n), { unfold function.left_inverse, simp, }, apply function.left_inverse.injective A1, end lemma injective_prod_mk_right2 {β γ:Type*} {n:γ}:function.injective (λ m:β, prod.mk m n) := begin have A1:function.left_inverse (@prod.fst β γ) (λ m:β, prod.mk m n), { unfold function.left_inverse, simp, }, apply function.left_inverse.injective A1, end lemma injective_prod_mk_left2 {β γ:Type*} {m:β}:function.injective (λ n:γ, prod.mk m n) := begin have A1:function.left_inverse (@prod.snd β γ) (λ n:γ, prod.mk m n), { unfold function.left_inverse, simp, }, apply function.left_inverse.injective A1, end lemma summable_right_of_summable {g:ℕ → ℕ → ℝ} {n:ℕ}: summable (λ p:ℕ × ℕ,g p.fst p.snd) → summable (g n) := begin intro A1, have A2:(g n)=(λ p:ℕ × ℕ,g p.fst p.snd)∘ (λ m:ℕ, prod.mk n m), { ext m, simp, }, rw A2, apply summable.comp_injective A1 (@injective_prod_mk_left n), end lemma summable_right_of_summable2 {g:ℕ × ℕ → ℝ} {n:ℕ}: summable g → summable (λ m:ℕ, g ⟨n, m⟩ ) := begin intro A1, have A2:(λ m:ℕ, g ⟨n, m⟩ )=g ∘ (λ m:ℕ, prod.mk n m), { ext m, simp, }, rw A2, apply summable.comp_injective A1 (@injective_prod_mk_left n), end lemma summable_left_of_summable {g:ℕ → ℕ → ℝ} {n:ℕ}: summable (λ p:ℕ × ℕ,g p.fst p.snd) → summable (λ m:ℕ, g m n) := begin intro A1, have A2:(λ m:ℕ, g m n)=(λ p:ℕ × ℕ,g p.fst p.snd)∘ (λ m:ℕ, prod.mk m n), { ext m, simp, }, rw A2, apply summable.comp_injective A1 (@injective_prod_mk_right n), end lemma summable_left_of_summable2 {g:ℕ × ℕ → ℝ} {n:ℕ}: summable g → summable (λ m:ℕ, g ⟨m, n⟩ ) := begin intro A1, have A2:(λ m:ℕ, g ⟨ m, n ⟩ )=g ∘ (λ m:ℕ, prod.mk m n), { ext m, simp, }, rw A2, apply summable.comp_injective A1 (@injective_prod_mk_right n), end lemma summable_left_of_summable3 {α β γ:Type*} [add_comm_group α] [uniform_space α] [uniform_add_group α] [complete_space α] {g:β × γ → α} {n:γ}: summable g → summable (λ m:β, g ⟨m, n⟩ ) := begin intro A1, have A2:(λ m:β, g ⟨ m, n ⟩ )=g ∘ (λ m:β, prod.mk m n), { ext m, simp, }, rw A2, apply summable.comp_injective A1 (@injective_prod_mk_right2 _ _ n), end lemma summable_right_of_summable3 {α β γ:Type*} [add_comm_group α] [uniform_space α] [uniform_add_group α] [complete_space α] {g:β × γ → α} {m:β}: summable g → summable (λ n:γ, g ⟨m, n⟩ ) := begin intro A1, have A2:(λ n:γ, g ⟨ m, n ⟩ )=g ∘ (λ n:γ, prod.mk m n), { ext m, simp, }, rw A2, apply summable.comp_injective A1 (@injective_prod_mk_left2 _ _ m), end /- summable : Π {α : Type u_1} {β : Type u_2} [_inst_1 : add_comm_monoid α] [_inst_2 : topological_space α], (β → α) → Prop -/ lemma summable_elim {α β:Type*} [add_comm_monoid α] [topological_space α] {g:β → α}: summable g → (∃ x, has_sum g x) := begin intro A1, apply A1, end lemma equiv_to_fun_injective {β γ:Type*} (E:equiv β γ): function.injective E.to_fun := function.left_inverse.injective E.left_inv lemma equiv_to_fun_bijective {β γ:Type*} {E:equiv β γ}: function.bijective E.to_fun := begin unfold function.bijective function.surjective, split, { apply equiv_to_fun_injective, }, { have A1:function.has_right_inverse E.to_fun := exists.intro E.inv_fun E.right_inv, apply function.has_right_inverse.surjective A1, } end lemma sigma_equiv_prod_to_fun_bijective {β γ:Type*}: function.bijective (equiv.sigma_equiv_prod β γ).to_fun := begin apply equiv_to_fun_bijective, end def make_set_element {α:Type*} {s:set α} {x:α} {H:x∈ s}:{a // a∈ s}:= begin apply @subtype.mk α s x H, end /- has_sum_iff_has_sum_of_ne_zero_bij shows that if there is a bijection between the nonzero values of two types, then two sums are equal. This just shows that if there is a traditional bijection, the two sums are equal. -/ lemma has_sum_bijective {α β γ:Type*} [ACM:add_comm_monoid α] [T:topological_space α] [TAM:has_continuous_add α] (f:β → α) {h:γ → β} (a:α): function.bijective h → (has_sum f a ↔ has_sum (f∘ h) a) := begin intros A1, unfold function.bijective at A1, cases A1 with A6 A7, unfold function.injective at A6, unfold function.surjective at A7, let i:= (λ (c:function.support (f ∘ h)), h c), begin have A5:i = (λ (c:function.support (f ∘ h)), h c) := rfl, apply has_sum_iff_has_sum_of_ne_zero_bij i, { intros c₁ c₂ A2, rw A5 at A2, simp at A2, apply A6, apply A2, }, { rw set.subset_def, intros x B1, rw set.mem_range, have B2 := A7 x, cases B2 with c B3, rw function.mem_support at B1, rw ← B3 at B1, have B4:f (h c) = (f ∘ h) c := rfl, rw B4 at B1, rw ← @function.mem_support γ α _ (f ∘ h) at B1, apply exists.intro (subtype.mk c B1), rw A5, simp, rw B3, }, { intro x, rw A5, }, end end -- See also tsum_equiv lemma equiv_sigma_prod_sum {α β γ:Type*} [ACM:add_comm_monoid α] [T:topological_space α] [TAM:has_continuous_add α] {f:β × γ → α} {a:α}: has_sum f a → (has_sum (λ p:(Σ (b : β), (λ b:β, γ) b), f ((equiv.sigma_equiv_prod β γ).to_fun p)) a) := begin intro A1, have A2:(λ p:(Σ (b : β), (λ b:β, γ) b), f ((equiv.sigma_equiv_prod β γ).to_fun p)) = f ∘ (@equiv.sigma_equiv_prod β γ).to_fun := rfl, rw A2, have A3:function.bijective (@equiv.sigma_equiv_prod β γ).to_fun := sigma_equiv_prod_to_fun_bijective, apply (has_sum_bijective f a A3).mp A1, end /- At its essence, this is the core of Fubini's theorem, but at such a level of abstraction that it is obvious. mathlib's has_sum is a generalization of an absolute sum. There is no concept of ordering over the indices. Thus, flipping pairs is a noop. -/ lemma equiv_prod_comm_sum {α β γ:Type*} [ACM:add_comm_monoid α] [T:topological_space α] [TAM:has_continuous_add α] {f:β × γ → α} {a:α}: has_sum f a → (has_sum (λ p:γ × β, f ((equiv.prod_comm γ β).to_fun p)) a) := begin intro A1, have A2:(λ p:γ × β, f ((equiv.prod_comm γ β).to_fun p)) = f ∘ (@equiv.prod_comm γ β).to_fun := rfl, rw A2, have A3:function.bijective (equiv.prod_comm γ β).to_fun := equiv_to_fun_bijective, apply (has_sum_bijective f a A3).mp A1, end lemma has_sum_prod {α β:Type*} [ACM:add_comm_monoid α] [T:topological_space α] [TAM:has_continuous_add α] [R:regular_space α] {γ:Type*} {f:β × γ → α} {g:β → α} {a:α}: (∀ b:β, has_sum (λ c:γ, f ⟨b, c⟩) (g b)) → has_sum f a → has_sum g a := begin /- The proof of this basically leverages the equivalence of (Σ (b : β), γ) and β × γ. has_sum_sigma can be utilized. -/ intros A1 A2, let γ2:=λ b:β, γ, let f2:=λ p:(Σ (b : β), γ2 b), f ((@equiv.sigma_equiv_prod β γ).to_fun p), begin --have AX:(λ (b:β), γ) ≃ β × γ := equiv.sigma_equiv_prod β γ, --rw @equiv.sigma_equiv_prod, have A3:has_sum f2 a, { have A3B:f2 = λ p:(Σ (b : β), γ2 b), f ((@equiv.sigma_equiv_prod β γ).to_fun p) := rfl, rw A3B, apply equiv_sigma_prod_sum A2, }, apply @has_sum.sigma α β ACM T TAM R γ2 f2 g a A3 A1, end end lemma summable_prod {α β:Type*} [ACM:add_comm_group α] [R:uniform_space α] [R2:regular_space α] [TAM:has_continuous_add α] [UAG:uniform_add_group α] [C:complete_space α] {γ:Type*} {f:β × γ → α}: summable f → summable (λ b:β, ∑' c:γ, f ⟨b, c⟩) := begin intro A0, have A1:(∀ b:β, summable (λ c:γ, f ⟨b, c⟩)), { intro b, apply @summable_right_of_summable3 α β γ ACM R UAG C f b A0, }, let g := (λ b:β, ∑' c:γ, f ⟨b, c⟩), let a := (∑' p:β × γ, f p), begin have A2:g = (λ b:β, ∑' c:γ, f ⟨b, c⟩) := rfl, have A2A:a = (∑' p:β × γ, f p) := rfl, have A3:(∀ b:β, has_sum (λ c:γ, f ⟨b, c⟩) (g b)), { intro b, rw A2, simp, apply has_sum_tsum, apply A1 b, }, unfold summable, apply exists.intro a, rw ← A2, apply has_sum_prod A3, rw A2A, apply has_sum_tsum, apply A0, end end lemma has_sum_prod2 {α β:Type*} [ACM:add_comm_monoid α] [T:topological_space α] [TAM:has_continuous_add α] [R:regular_space α] {γ:Type*} {f:β × γ → α} {g:γ → α} {a:α}: (∀ c:γ, has_sum (λ b:β, f ⟨b, c⟩) (g c)) → has_sum f a → has_sum g a := begin intros A1 A2, let f2 := λ p:(γ × β), f ((@equiv.prod_comm γ β).to_fun p), begin have A3:has_sum f2 a, { have A3B:f2 = λ p:(γ × β), f ((@equiv.prod_comm γ β).to_fun p) := rfl, rw A3B, apply equiv_prod_comm_sum A2, }, apply @has_sum_prod α γ ACM T TAM R β f2 g a A1 A3, end end lemma summable_prod2 {α β:Type*} [ACM:add_comm_group α] [R:uniform_space α] [R2:regular_space α] [TAM:has_continuous_add α] [UAG:uniform_add_group α] [C:complete_space α] {γ:Type*} {f:β × γ → α}: summable f → summable (λ c:γ, ∑' b:β, f ⟨b, c⟩) := begin intro A1, let f2 := λ p:(γ × β), f ((@equiv.prod_comm γ β).to_fun p), begin have A4:f2=λ p:(γ × β), f ((@equiv.prod_comm γ β).to_fun p) := rfl, have A5:f2=f ∘ (@equiv.prod_comm γ β) := rfl, have A2:summable f2, { rw A5, have A6:= equiv_to_fun_injective (@equiv.prod_comm γ β), apply summable.comp_injective A1 A6, }, have A3:summable (λ b:γ, ∑' c:β, f2 ⟨b, c⟩) := summable_prod A2, rw A4 at A3, simp at A3, apply A3, end end lemma summable_prod2_real {β:Type*} {γ:Type*} {f:β × γ → ℝ}: summable f → summable (λ c:γ, ∑' b:β, f ⟨b, c⟩) := begin apply summable_prod2, end lemma summable_prod3_real {β:Type*} {γ:Type*} {f:β → γ → ℝ}: summable (function.uncurry f) → summable (λ c:γ, ∑' b:β, f b c) := begin intro A1, let g := (function.uncurry f), begin have A2:summable (λ c:γ, ∑' b:β, g ⟨b, c⟩), { apply summable_prod2_real, apply A1, }, have A3:(λ c:γ, ∑' b:β, g ⟨b, c⟩) = (λ c:γ, ∑' b:β, f b c), { ext c, have A3A:(λ b, g ⟨b, c⟩)=(λ b, f b c), { ext b, refl, }, rw A3A, }, rw ← A3, apply A2, end end lemma fubinis_theorem_discrete_left2 {f:ℕ → ℕ → ℝ} {X:ℝ}: has_sum (λ p:ℕ × ℕ,f p.fst p.snd) X → has_sum (λ i, ∑' j, f i j) X := begin intro A2, have A1:=has_sum.summable A2, let f2:ℕ × ℕ → ℝ := (λ p:ℕ × ℕ,f p.fst p.snd), let g:ℕ → ℝ := λ i:ℕ, ∑' j:ℕ, f2 ⟨i, j⟩, begin have A4:g = λ i:ℕ, ∑' j:ℕ, f2 ⟨i, j⟩ := rfl, have A8:f2 = (λ (p:ℕ × ℕ), f p.fst p.snd) := rfl, have A3:(∀ i:ℕ, has_sum (λ j:ℕ, f2 ⟨i, j⟩) (g i)), { intro i, have A3A:summable (λ j:ℕ, f2 ⟨i, j⟩) := @summable_right_of_summable2 f2 i A1, have A3D:=has_sum_tsum A3A, rw A4, simp, apply A3D, }, apply has_sum_prod A3 A2, end end lemma fubinis_theorem_discrete_left {f:ℕ → ℕ → ℝ}: summable (λ p:ℕ × ℕ,f p.fst p.snd) → (∑' (p:ℕ × ℕ), f p.fst p.snd) = (∑' i, ∑' j, f i j) := begin intro A1, cases (summable_elim A1) with X A2, have A3:=fubinis_theorem_discrete_left2 A2, rw tsum_replace A2, rw tsum_replace A3, end lemma fubinis_theorem_discrete_right {f:ℕ → ℕ → ℝ}: summable (λ p:ℕ × ℕ,f p.fst p.snd) → (∑' (p:ℕ × ℕ), f p.fst p.snd) = (∑' j, ∑' i, f i j) := begin intro A1, cases (summable_elim A1) with X A2, let f2:ℕ × ℕ → ℝ := (λ p:ℕ × ℕ,f p.fst p.snd), let g:ℕ → ℝ := λ i:ℕ, ∑' j:ℕ, f2 ⟨j, i⟩, begin have A4:g = λ i:ℕ, ∑' j:ℕ, f2 ⟨j, i⟩ := rfl, have A8:f2 = (λ (p:ℕ × ℕ), f p.fst p.snd) := rfl, have A5:summable f2, { rw A8, apply A1, }, have A3:(∀ i:ℕ, has_sum (λ j:ℕ, f2 ⟨j, i⟩) (g i)), { intro i, have A3A:summable (λ j:ℕ, f2 ⟨j, i⟩) := @summable_left_of_summable2 f2 i A5, have A3D:=has_sum_tsum A3A, rw A4, simp, apply A3D, }, cases A5 with a A6, have A7:has_sum g a := has_sum_prod2 A3 A6, rw ← A8, rw tsum_replace A6, rw tsum_replace A7, end end lemma fubinis_theorem_discrete {f:ℕ → ℕ → ℝ}: summable (λ p:ℕ × ℕ,f p.fst p.snd) → (∑' i, ∑' j, f i j) = (∑' i, ∑' j, f j i) := begin intro A1, have A2:(∑' (p:ℕ × ℕ), f p.fst p.snd) = (∑' j, ∑' i, f i j) := fubinis_theorem_discrete_right A1, have A3:(∑' (p:ℕ × ℕ), f p.fst p.snd) = (∑' i, ∑' j, f i j) := fubinis_theorem_discrete_left A1, apply trans, symmetry, apply A3, apply A2, end lemma curry_def {α β γ:Type*}:@function.curry α β γ = λ (f:α × β → γ) (a:α) (b:β), f (a, b) := rfl lemma unique_right_inverse_def {α β:Type*} {f:α → β} {g h:β → α}: f ∘ g = f ∘ h → function.left_inverse g f → g = h := begin intros A1 A2, ext b, have A3:f (g b) = f (h b), { have A3A:(f ∘ g) b = (f ∘ h) b, { rw A1, }, simp at A3A, apply A3A, }, have A4:g (f (g b)) = g (f (h b)), { rw A3, }, unfold function.left_inverse at A2, rw A2 at A4, rw A2 at A4, apply A4, end lemma left_inverse_uncurry_curry {α β γ:Type*}: function.left_inverse (@function.uncurry α β γ) (@function.curry α β γ) := begin unfold function.left_inverse, intro f, simp, end /- While being the true definition, this gets us into trouble, simplifying to prod.rec. uncurry_def2 is more useful. -/ lemma uncurry_def {α β γ:Type*}:@function.uncurry α β γ = λ f ⟨a, b⟩, f a b := begin have A1:(@function.curry α β γ)∘ ((@function.uncurry α β γ)) = function.curry ∘ (λ f ⟨a, b⟩, f a b), { ext f, simp, rw curry_def, }, apply unique_right_inverse_def A1 left_inverse_uncurry_curry, end lemma id_rhs_def {α:Type*} {a:α}:id_rhs α a = a := rfl lemma uncurry_def2 {α β γ:Type*}:@function.uncurry α β γ = λ f p, f p.fst p.snd := begin have A1:(@function.curry α β γ)∘ ((@function.uncurry α β γ)) = function.curry ∘ (λ f p, f p.fst p.snd), { ext f, simp, rw curry_def, }, apply unique_right_inverse_def A1 left_inverse_uncurry_curry, end lemma uncurry_def3 {α β γ:Type*} {g:α → β → γ} {a:α} {b:β}: function.uncurry g (prod.mk a b) = g a b := begin rw uncurry_def2, end lemma uncurry_abs {α β:Type*} {f:α → β → ℝ}:abs ∘ (function.uncurry f) = function.uncurry (λ a:α, abs ∘ f a) := begin ext ab, rw uncurry_def2, end lemma tsum_tsum_conditional_sum_conditional_sum_of_summable {f:ℕ → ℕ → ℝ}: summable (function.uncurry f) → (∑' j, ∑' i, f i j) = (∑ₚ n, ∑ₚ m, f m n) := begin intro A0, have B1:summable (λ p:ℕ × ℕ, f p.fst p.snd), { have A2A:(function.uncurry f) = (λ p:ℕ × ℕ, f p.fst p.snd), { rw uncurry_def2, }, rw ← A2A, apply A0, }, have B2:∀ i:ℕ, summable (λ j:ℕ, f j i), { intro i, apply summable_left_of_summable B1, }, let g:ℕ → ℝ := λ i:ℕ, ∑' j:ℕ, f j i, begin -- Delete this have A2:∀ i, has_sum (λ j, f j i) (∑' j:ℕ, f j i), { intro i, apply has_sum_tsum, apply B2 i, }, --have A3:∀ i, has_conditional_sum (λ j, f j i) (∑' j:ℕ, f j i), have A4:∀ i, conditional_sum (λ j, f j i) = (∑' j:ℕ, f j i), { intro i, symmetry, apply tsum_eq_conditional_sum_of_summable, apply B2 i, }, have A5:(λ i, conditional_sum (λ j, f j i)) = (λ i, (∑' j:ℕ, f j i)), { ext i, apply A4, }, rw A5, have A7:summable (λ i, (∑' j:ℕ, f j i)), { apply summable_prod3_real A0, }, have A8:tsum (λ i, (∑' j:ℕ, f j i)) = conditional_sum (λ i, (∑' j:ℕ, f j i)) := tsum_eq_conditional_sum_of_summable A7, rw A8, end end lemma tsum_tsum_conditional_sum_conditional_sum_of_summable2 {f:ℕ → ℕ → ℝ}: summable (function.uncurry f) → (∑' j, ∑' i, f j i) = (∑ₚ n, ∑ₚ m, f n m) := begin intro A0, let g:ℕ → ℕ → ℝ := λ m:ℕ, λ n:ℕ, f n m, begin have A3:g = (λ m:ℕ, λ n:ℕ, f n m) := rfl, have A1:summable (function.uncurry g), { rw A3, have A1A:(function.uncurry f) ∘ ((@equiv.prod_comm ℕ ℕ).to_fun) = (function.uncurry g), { rw uncurry_def2, rw A3, --simp, ext p, simp, }, rw ← A1A, have A1B:= equiv_to_fun_injective (@equiv.prod_comm ℕ ℕ), apply summable.comp_injective, apply A0, apply A1B, }, have A2:=tsum_tsum_conditional_sum_conditional_sum_of_summable A1, rw A3 at A2, simp at A2, simp, apply A2, end end lemma summable_iff_has_sum_tsum {α β:Type*} [add_comm_monoid α] [add_comm_monoid α] [topological_space α] {f : β → α}: summable f ↔ has_sum f (∑' (b : β), f b) := begin split;intro A1, { apply has_sum_tsum A1, }, { apply summable_intro A1, } end lemma tsum_nonneg2 {β:Type*} {f:β → ℝ}: 0 ≤ f → 0 ≤ tsum f := begin intro A1, apply tsum_nonneg, intro b, apply A1, end lemma op_has_sum_le_has_sum_op {β:Type*} [decidable_eq β] {f:β → ℝ} {g:ℝ → ℝ} {x y:ℝ}: (∀ S:finset β, g (S.sum f) ≤ S.sum (g ∘ f)) → continuous g → has_sum f x → has_sum (g ∘ f) y → g x ≤ y := begin intros A1 A2 A3 A4, apply le_of_not_lt, intro A5, /- Proof by contradiction. Assume y < g x. If we set ε = (g x - y)/2, because g is continuous, then there should exist a δ > 0 such that if |x' - x| < δ, then |g x' - g x| < ε. Now, there must exist a set (S:finset β) such that for all supersets T of S, |T.sum f - x| < δ. Thus, g (T.sum f) ≤ T.sum (g ∘ f), and therefore g x - (ε/2) < T.sum (g ∘ f). On the flip side, for ε/2 there exists S' such that for all supersets T' of S', |T'.sum (g ∘ f) - y | < ε/2. Yet, there exists S∪ S', which must be in both sets, a contradiction. -/ let ε := (g x - y)/2, begin have A6 :0 < ε, { apply half_pos, apply sub_pos_of_lt A5, }, have A7 := (continuous_elim x A2) ε A6, cases A7 with δ A8, cases A8 with A9 A10, rw ← has_sum_real at A3, have A11 := A3 δ A9, cases A11 with S A12, rw ← has_sum_real at A4, have A13 := A4 ε A6, cases A13 with S' A14, have A15 := finset.subset_union_left S S', have A16 := finset.subset_union_right S S', have A17 := A14 (S ∪ S') A16, have A18 := A12 (S ∪ S') A15, have A19 := A10 (finset.sum (S ∪ S') f) A18, rw abs_lt2 at A17, cases A17 with A20 A21, -- A21 : finset.sum (S ∪ S') (g ∘ f) < y + ε rw abs_lt2 at A19, cases A19 with A22 A23, -- A22 : g x - ε < g (finset.sum (S ∪ S') f), have A24 : g x - ε < finset.sum (S ∪ S') (g ∘ f), { apply lt_of_lt_of_le, apply A22, apply A1, }, have A25 : g x - ε = y + ε, { have A25A:ε= (g x - y)/2 := rfl, apply half_equal A25A, }, rw A25 at A24, apply lt_irrefl (finset.sum (S ∪ S') (g ∘ f)), apply lt_trans A21 A24, end end lemma abs_abs_sub_abs_le_abs {a b:ℝ}: abs (abs a - abs b) ≤ abs (a - b) := begin have A1:0 ≤ (abs a - abs b) ∨ ((abs a - abs b) < 0) := le_or_lt 0 (abs a - abs b), cases A1, { rw abs_of_nonneg A1, apply abs_sub_abs_le_abs_sub, }, { rw abs_of_neg A1, rw abs_antisymm, have A2:-(abs a - abs b)=abs b - abs a, { simp, }, rw A2, apply abs_sub_abs_le_abs_sub, } end lemma continuous_abs:continuous (@abs ℝ _) := begin rw ← continuous_iff_has_classical_rlimit, intros x ε A1, apply exists.intro ε, split, { apply A1, }, { intros x' A2 A3, have A4:abs (abs x' - abs x) ≤ abs (x - x'), { rw abs_antisymm, apply abs_abs_sub_abs_le_abs, }, apply lt_of_le_of_lt, apply A4, apply A3, } end /- Several functions satisfy the constraints for g. In particular √x, |x|, and other norms. -/ lemma op_has_sum_le_has_sum_op2 {β:Type*} [D:decidable_eq β] {f:β → ℝ} {g:ℝ → ℝ} {x y:ℝ}: (∀ a b, g (a + b) ≤ (g a) + (g b)) → (g 0 = 0) → continuous g → has_sum f x → has_sum (g ∘ f) y → g x ≤ y := begin intros A1 A2 A3 A4 A5, have A6:(∀ S:finset β, g (S.sum f) ≤ S.sum (g ∘ f)), { intros S, apply finset.le_sum_of_subadditive _ A2 A1, }, apply op_has_sum_le_has_sum_op A6 A3 A4 A5, end lemma abs_has_sum_le_has_sum_abs {β:Type*} [D:decidable_eq β] {f : β → ℝ} {x y:ℝ}: has_sum f x → has_sum (abs ∘ f) y → abs x ≤ y := begin intros A1 A2, have A3:abs (0:ℝ) = 0, { simp, }, have A5:continuous (@abs ℝ _) := continuous_abs, have A7:=@abs_add_le_abs_add_abs ℝ _, apply op_has_sum_le_has_sum_op2 A7 A3 A5 A1 A2, end lemma abs_tsum_le_tsum_abs {β:Type*} [D:decidable_eq β] {f : β → ℝ}: abs (tsum f) ≤ tsum (abs ∘ f) := begin have A6:abs (0:ℝ) = 0, { simp, }, have A1:summable f ∨ ¬(summable f) := classical.em (summable f), cases A1, { have A2:has_sum f (tsum f) := has_sum_tsum A1, have A3:summable (abs ∘ f) := (summable_iff_abs_summable.mp A1), have A4:has_sum (abs ∘ f) (tsum (abs ∘ f)) := has_sum_tsum A3, have A5:continuous (@abs ℝ _) := continuous_abs, apply abs_has_sum_le_has_sum_abs A2 A4, }, { have A2:¬summable (abs ∘ f), { intro A2A, apply A1, apply summable_iff_abs_summable.mpr A2A, }, unfold tsum, rw dif_neg, rw dif_neg, rw A6, apply A2, apply A1, } end lemma empty_finset_false {α:Type*} [decidable_eq α]: {x//x∈ (∅:finset α)} → false := begin intro A1, have A2 := A1.property, simp at A2, apply A2, end def empty_finset_fn {α β:Type*} [decidable_eq α]: {x//x∈ (∅:finset α)} → β := begin intro A1, exfalso, apply empty_finset_false A1, end def finset_insert_fn {α β:Type*} [D:decidable_eq α] (a:α) (b:β) (s:finset α) (f:{x//x∈s} → β) (x:{x//x∈ insert a s}):β := @dite (x.val ∈ s) _ β (λ h, (f (subtype.mk (x.val) h))) (λ _, b) lemma coe_finset_subtype {α:Type*} [D:decidable_eq α] {S:finset α} {a:{x//x∈ S}} {a':α}: (a.val = a') → ((a:α)=a') := begin intro A1, rw ← A1, refl, end lemma finset_axiom_of_choice {α β:Type*} [D:decidable_eq α] {P:α → β → Prop} {S:finset α}: (∀ s∈ S, ∃ b:β, P s b) → (∃ (f:{x // x∈S} → β), ∀ x:{x // x∈S}, P x (f x)) := begin apply finset.induction_on S, { intro A1, apply exists.intro empty_finset_fn, intro x, exfalso, apply empty_finset_false x, apply D, }, { intros a s A1 A2 A3, have A4:(∀ (a2 : α), a2 ∈ s → (∃ (b : β), P a2 b)), { intros a2 A4A, have A4B:= A3 a2, have A4C:a2∈ insert a s, { simp, right, exact A4A, }, apply A4B A4C, }, have A5 := A2 A4, cases A5 with f' A6, have A7:∃ b':β, P a b', { have A7A: a ∈ insert a s, { simp, }, apply A3 a A7A, }, cases A7 with b' A8, let f:{x // x ∈ insert a s} → β := finset_insert_fn a b' s f', begin have A9:f=finset_insert_fn a b' s f' := rfl, apply exists.intro f, intro x, have A10:=x.property, --simp at A10, rw finset.mem_insert at A10, cases A10, { have A11:f x = b', { rw A9, unfold finset_insert_fn, rw dif_neg, rw A10, apply A1, }, rw A11, have A12:(x:α) = a, { apply coe_finset_subtype, apply A10, }, rw A12, apply A8, }, { rw A9, unfold finset_insert_fn, rw dif_pos, have A11:∀ a'':α,∀ h:a''∈ s, P a'' (f' (subtype.mk a'' h)), { intros a'' A11A, apply A6 (subtype.mk a'' A11A), }, apply A11 x.val A10, }, end } end def finset.Union {α β:Type*} [Dβ:decidable_eq β] {S:finset α} (f:{x//x∈ S}→ finset β):finset β := finset.fold (@has_union.union (finset β) (@finset.has_union β Dβ)) ∅ f (finset.attach S) lemma finset_Union_empty {α β:Type*} [decidable_eq β] (f:{x//x∈ (∅:finset α)}→ finset β):finset.Union f = (∅:finset β) := begin unfold finset.Union, simp, end def finset_insert_constrain {α β:Type*} [decidable_eq α] {a:α} {S:finset α} (f:{x//x∈ (insert a S)}→ β) (s:{x//x∈ S}):β := f (subtype.mk s.val (finset.mem_insert_of_mem s.property) ) lemma finset_insert_constrain_def {α β:Type*} [decidable_eq α] {a:α} {S:finset α} (f:{x//x∈ (insert a S)}→ β) (s:{x//x∈ S}): finset_insert_constrain f s = f (subtype.mk s.val (finset.mem_insert_of_mem s.property) ) := rfl lemma finset_insert_constrain_def2 {α β:Type*} [decidable_eq α] {a:α} {S:finset α} (f:{x//x∈ (insert a S)}→ β): finset_insert_constrain f = λ (s:{x//x∈ S}), f (subtype.mk s.val (finset.mem_insert_of_mem s.property) ) := rfl -- This generalizes to when a∈S too... lemma finset_Union_insert {α β:Type*} [decidable_eq α] [decidable_eq β] {a:α} {S:finset α} (f:{x//x∈ (insert a S)}→ finset β):a∉ S → finset.Union f = (f (subtype.mk a (finset.mem_insert_self a S))) ∪ (finset.Union (finset_insert_constrain f)) := begin intro A1, unfold finset.Union finset_insert_constrain, rw finset.attach_insert, rw finset.fold_insert, rw finset_insert_constrain_def2, simp, simp, apply A1, end lemma finset_mem_Union_intro {α β:Type*} [decidable_eq α] [decidable_eq β] {b:β} {S:finset α}: ∀ a:{x//x∈ S}, ∀ f:{x//x∈ S}→ finset β, b∈ f a → b∈ finset.Union f := begin apply finset.induction_on S, { intros a f A1, exfalso, have A2 := a.property, apply A2, }, { intros a s A1 A2 a' f A3, rw finset_Union_insert f A1, rw finset.mem_union, have A4 := a'.property, rw finset.mem_insert at A4, cases A4, { left, have A5:subtype.mk a _ = a', { apply subtype.eq, symmetry, apply A4, }, rw A5, apply A3, }, { right, apply A2 (subtype.mk a'.val A4), unfold finset_insert_constrain, simp, apply A3, } } end lemma finset_mem_Union_elim {α β:Type*} [decidable_eq α] [decidable_eq β] {S:finset α}: ∀ f:{x//x∈ S}→ finset β, ∀ b∈ finset.Union f, ∃ a:{x//x∈ S}, b∈ f a := begin apply finset.induction_on S, { intros f b A1, rw finset_Union_empty at A1, exfalso, apply A1, }, { intros a s A1 A2 f b A3, rw finset_Union_insert at A3, rw finset.mem_union at A3, cases A3, { let a':{x//x∈ insert a s} := (subtype.mk a (finset.mem_insert_self a s)), begin apply exists.intro a', apply A3, end }, { have A4 := A2 (finset_insert_constrain f) b A3, cases A4 with a' A5, let a'':{x//x∈ insert a s} := subtype.mk a'.val (finset.mem_insert_of_mem a'.property), begin apply exists.intro a'', unfold finset_insert_constrain at A5, apply A5, end }, { apply A1, } } end def finset.relation {α β:Type*} [decidable_eq α] [decidable_eq β] {S:finset α} (f:{x//x∈ S}→ finset β):finset (α × β) := finset.Union (λ x, finset.product ({x.val}) (f x)) lemma finset_relation_empty {α β:Type*} [decidable_eq α] [decidable_eq β] (f:{x//x∈ (∅:finset α)}→ finset β):finset.relation f = (∅:finset (α × β)) := begin unfold finset.relation, rw finset_Union_empty, end lemma finset_relation_insert {α β:Type*} [decidable_eq α] [decidable_eq β] {a:α} {S:finset α} (f:{x//x∈ (insert a S)}→ finset β):a∉ S → finset.relation f = finset.product ({a}) (f (subtype.mk a (finset.mem_insert_self a S))) ∪ (finset.relation (finset_insert_constrain f)) := begin intro A1, unfold finset.relation, rw finset_Union_insert, refl, apply A1, end lemma mem_fst_finset_relation {α β:Type*} [decidable_eq α] [decidable_eq β] {S:finset α} {ab:α × β} :(∀ f:{x//x∈ S}→ finset β, ab∈ (finset.relation f) → ab.fst ∈ S) := begin apply finset.induction_on S, { intros f A1, rw finset_relation_empty at A1, apply A1, }, { intros a s A1 A2 f A3, rw finset_relation_insert at A3, rw finset.mem_insert, rw finset.mem_union at A3, cases A3, { rw finset.mem_product at A3, cases A3 with A4 A5, simp at A4, left, apply A4, }, { right, apply A2 (finset_insert_constrain f) A3, }, { apply A1, } } end lemma finset_product_empty_right {α β:Type*} {S:finset α}:finset.product S (∅:finset β) = ∅ := begin ext ab, split;intros A1;exfalso;simp at A1;apply A1, end --This is probably in core.lean or something. lemma and_or_distrib {P Q R:Prop}:P ∧ (Q∨ R) ↔ (P ∧ Q) ∨ (P ∧ R) := begin split;intros A1, { cases A1 with A2 A3, cases A3, { left, apply and.intro A2 A3, }, { right, apply and.intro A2 A3, } }, { cases A1;cases A1 with A2 A3;split, { apply A2, }, { left,apply A3, }, { apply A2, }, { right,apply A3, } } end lemma finset_product_insert_right {α β:Type*} [decidable_eq α] [decidable_eq β] {A:finset α} {B:finset β} {b:β}:finset.product A (insert b B) = (finset.product A {b}) ∪ (finset.product A B) := begin ext ab, rw finset.mem_product, rw finset.mem_union, rw finset.mem_product, rw finset.mem_insert, simp, apply and_or_distrib, end lemma finset_product_singleton {α β:Type*} {a:α} {b:β}: (@finset.product α β {a} {b})={prod.mk a b} := begin refl, end lemma finset_product_disjoint_right {α β:Type*} [decidable_eq α] [decidable_eq β] {A1 A2:finset α} {B1 B2:finset β}: disjoint B1 B2 → disjoint (finset.product A1 B1) (finset.product A2 B2) := begin intro C1, apply finset_disjoint_intro, intros ab C2, rw finset.mem_product at C2, cases C2 with C3 C4, have C5:ab.snd ∉ B2 := finset_disjoint_elim ab.snd C1 C4, intro C6, rw finset.mem_product at C6, apply C5, cases C6 with C7 C8, apply C8, end lemma finset_sum_uncurry {α β:Type*} [decidable_eq α] [decidable_eq β] {g:α → β → ℝ} {a:α} {S:finset β}: finset.sum (finset.product ({a}) S) (function.uncurry g) = finset.sum S (g a) := begin apply finset.induction_on S, { rw finset_product_empty_right, simp, }, { intros a2 S2 A1 A2, rw finset_product_insert_right, rw finset.sum_union, rw A2, rw finset_product_singleton, rw finset.sum_singleton, rw finset.sum_insert, rw uncurry_def3, apply A1, apply finset_product_disjoint_right, simp, apply A1, } end lemma finset_relation_sum {α β:Type*} [decidable_eq α] [decidable_eq β] {g:α → β → ℝ} {S:finset α}:∀ f:{x//x∈ S}→ finset β, (finset.relation f).sum (function.uncurry g) = (finset.attach S).sum (λ x, (f x).sum (g x.val)) := begin apply finset.induction_on S, { intro f, rw finset_relation_empty, simp, }, { intros a s A1 A2 f, rw finset.attach_insert, rw finset.sum_insert, rw finset_relation_insert, rw finset.sum_union, rw A2 (finset_insert_constrain f), rw finset_insert_constrain_def2, simp, rw finset_sum_uncurry, { apply finset_disjoint_intro, intros ab A3, intro A4, apply A1, rw finset.mem_product at A3, cases A3 with A5 A6, rw finset.mem_singleton at A5, rw ← A5, apply mem_fst_finset_relation (finset_insert_constrain f) A4, }, { apply A1, }, { simp, apply A1, } } end lemma finset_mem_relation {α β:Type*} [decidable_eq α] [decidable_eq β] {S:finset α} {ab:α × β} {f:{x//x∈ S}→ finset β} :(ab ∈ (finset.relation f)) ↔ (∃ H:ab.fst∈ S, ab.snd∈ f (subtype.mk ab.fst H)) := begin -- If there was a finset_mem_Union, this proof would be easier. split;intros A1, { unfold finset.relation at A1, have A2:=finset_mem_Union_elim _ ab A1, cases A2 with a A3, simp at A3, cases A3 with A4 A5, have A4':ab.fst = a.val, { rw A4, refl, }, have A6:=a.property, rw ← A4' at A6, apply exists.intro A6, have A7:a = ⟨ ab.fst, A6 ⟩, { apply subtype.eq, simp, symmetry, apply A4, }, rw ← A7, apply A5, }, { unfold finset.relation, cases A1 with A2 A3, apply finset_mem_Union_intro ⟨ab.fst, A2⟩, rw finset.mem_product, split, { rw finset.mem_singleton, }, { apply A3, } } end lemma bdd_above_def3 {α:Type*} [preorder α] {S:set α}: (bdd_above S) ↔ (∃ (x : α), x ∈ upper_bounds S) := begin rw bdd_above_def, unfold set.nonempty, end lemma bdd_above_def2 {α:Type*} [preorder α] {S:set α}: (bdd_above S) ↔ (∃ (x : α), ∀ s∈ S, s ≤ x) := begin rw bdd_above_def3, unfold upper_bounds, simp, end def proj_left {α β:Type*} [decidable_eq α] (S:finset (α × β)):finset α := multiset.to_finset (multiset.map (@prod.fst α β) S.val) lemma proj_left_def {α β:Type*} [decidable_eq α] (S:finset (α × β)): proj_left S = multiset.to_finset (multiset.map (@prod.fst α β) S.val) := rfl lemma mem_proj_left {α β:Type*} [decidable_eq α] {S:finset (α × β)} {a:α}: a∈ proj_left S ↔ (∃ b:β, prod.mk a b ∈ S) := begin split;intros A1, { rw proj_left_def at A1, simp at A1, apply A1, }, { rw proj_left_def, simp, apply A1, } end def proj_right {α β:Type*} [decidable_eq β] (S:finset (α × β)):finset β := multiset.to_finset (multiset.map (@prod.snd α β) S.val) lemma proj_right_def {α β:Type*} [decidable_eq β] (S:finset (α × β)): proj_right S = multiset.to_finset (multiset.map (@prod.snd α β) S.val) := rfl lemma mem_proj_right {α β:Type*} [decidable_eq β] {S:finset (α × β)} {b:β}: b ∈ proj_right S ↔ (∃ a:α, prod.mk a b ∈ S) := begin split;intros A1, { rw proj_right_def at A1, simp at A1, apply A1, }, { rw proj_right_def, simp, apply A1, } end def decidable_pred_eq_fst {α β:Type*} [D:decidable_eq α] {a:α}: decidable_pred (λ ab:α× β, a=ab.fst) := (λ ab:α× β, D a ab.fst) def image_right {α β:Type*} [decidable_eq α] [decidable_eq β] (S:finset (α × β)) (a:α):finset β := proj_right (@finset.filter (α × β) (λ ab:α× β, a=ab.fst) (@decidable_pred_eq_fst α β _ a) S) def finset_mem_image_right {α β:Type*} [decidable_eq α] [decidable_eq β] {S:finset (α × β)} {a:α} {b:β}:b∈ image_right S a ↔ prod.mk a b ∈ S := begin unfold image_right, rw mem_proj_right, split,intros A1, { cases A1 with a' A2, rw finset.mem_filter at A2, cases A2 with A3 A4, simp at A4, subst A4, apply A3, }, { intro A2, apply exists.intro a, rw finset.mem_filter, split, apply A2, refl, } end lemma finset_relation_image_right {α β:Type*} [decidable_eq α] [decidable_eq β] {S:finset (α × β)}: (finset.relation (λ a:{x//x ∈ (proj_left S)}, image_right S (a.val))) = S := begin ext ab, rw finset_mem_relation, split;intros A1, { cases A1 with A2 A3, rw finset_mem_image_right at A3, simp at A3, apply A3, }, { have A2:∃ b:β, prod.mk ab.fst b ∈ S, { apply exists.intro ab.snd, simp, apply A1, }, rw ← mem_proj_left at A2, apply exists.intro A2, simp, rw finset_mem_image_right, simp, apply A1, } end --Should replace with finset.sum_attach, but the call site is complex. lemma finset_sum_attach_simp {α:Type*} {S:finset α} [decidable_eq α] {f:α → ℝ}: (finset.attach S).sum (λ x, f (x.val)) = S.sum f := begin apply finset.sum_attach, end /- Okay I am revisiting this proof YET AGAIN. First, we switch to absolute values. In fact, it is probably a better idea to first prove this for 0 ≤ f, and then prove it in general. Okay, assuming 0 ≤ f, then we need to prove that tsum (λ n:ℕ, tsum ((f n))) is an upper bound for any S:finset (N× N), S.sum (function.uncurry f). All we have to do is divide the function by What I need to prove is that there exists an upper bound on any finite sum. In particular, one can break apart any finite sum into pieces, based upon projections into the first index. -/ lemma mem_upper_bounds_range_elim {α:Type*} {x:ℝ} {f:α → ℝ} {a:α}: x ∈ upper_bounds (set.range f) → f a ≤ x := begin intro A1, unfold upper_bounds at A1, simp at A1, have A2:f a = f a := rfl, apply A1 a A2, end lemma summable_function_uncurry4 {f:ℕ → ℕ → ℝ}: 0 ≤ f → (∀ i:ℕ, summable (f i)) → summable (λ n:ℕ, tsum (f n)) → summable (function.uncurry f) := begin intros A1 A2 A3, apply summable_of_bdd_above, { rw le_func_def, intro ab, rw uncurry_def2, simp, apply A1, }, rw bdd_above_def2, apply exists.intro (tsum (λ n:ℕ, tsum (f n))), intros x A4, simp at A4, cases A4 with S A6, subst x, have A7:=@finset_relation_image_right _ _ _ _ S, rw ← A7, rw finset_relation_sum, have A8:finset.sum (finset.attach (proj_left S)) (λ (x : {x // x ∈ proj_left S}), finset.sum (image_right S (x.val)) (f (x.val))) ≤ finset.sum (finset.attach (proj_left S)) (λ (x : {x // x ∈ proj_left S}), tsum (f (x.val))), { apply finset_sum_le2, intros x A8X, have A8A:=A2 (x.val), have A8B:0 ≤ (f (x.val)), { apply A1, }, have A8C:= tsum_in_upper_bounds_of_summable A8B A8A, apply mem_upper_bounds_range_elim A8C, }, apply le_trans, apply A8, rw @finset_sum_attach_simp ℕ (proj_left S) _ (λ n,(tsum (f n))), have A9:(0:ℕ → ℝ) ≤ (λ n:ℕ, tsum (f n)), { rw le_func_def, intro n, apply tsum_nonneg2, apply A1, }, have A10:= tsum_in_upper_bounds_of_summable A9 A3, apply mem_upper_bounds_range_elim A10, end --def finset_p (a:ℕ) (S:finset ℕ):=finset.product (finset.singleton a) S --lemma finset_union --Rearrange the indices to be in order. lemma summable_function_uncurry3 {f:ℕ → ℕ → ℝ}: (∀ i:ℕ, summable (f i)) → summable (λ n:ℕ, tsum (abs ∘ (f n))) → summable (function.uncurry f) := begin /- Consider g = (λ a:ℕ, abs ∘ (f n)), the absolute variant of f. We prove summable (function.uncurry g) using summable_function_uncurry4, and then prove the result here. -/ let g := (λ a:ℕ, abs ∘ (f a)), begin intros A1 A2, have A3:g = (λ a:ℕ, abs ∘ (f a)) := rfl, have A4:(0:ℕ → ℕ → ℝ)≤ g, { rw A3, have A4A:∀ a:ℕ, (0:ℕ → ℕ → ℝ) a ≤ g a, { intro a, rw le_func_def, rw A3, intro b, simp, apply abs_nonneg, }, apply A4A, }, have A5:(∀ i:ℕ, summable (g i)), { intro i, rw ← summable_iff_abs_summable, apply A1, }, have A6:summable (λ n:ℕ, tsum ((g n))), { rw A3, simp, apply A2, }, rw summable_iff_abs_summable, have A7:abs ∘ function.uncurry f = function.uncurry g, { rw A3, rw uncurry_def2, }, rw A7, apply summable_function_uncurry4 A4 A5 A6, end end lemma summable_function_uncurry {f:ℕ → ℕ → ℝ}: (∀ i:ℕ, absolutely_summable (f i)) → conditionally_summable (λ n:ℕ,conditional_sum (abs ∘ (f n))) → summable (function.uncurry f) := begin intros A1 A2, have A3:∀ i:ℕ, summable (f i), { intro i, rw ← absolutely_summable_def, apply A1, }, have A3B:∀ i:ℕ, summable (abs ∘ (f i)), { intro i, rw ← summable_iff_abs_summable, apply A3, }, let g := (λ n:ℕ,conditional_sum (abs ∘ (f n))), begin have A4:∀ n:ℕ, conditional_sum (abs ∘ (f n))=tsum (abs ∘ (f n)), { intro n, symmetry, apply tsum_eq_conditional_sum_of_summable, apply A3B n, }, have A5:conditionally_summable (λ n:ℕ,tsum (abs ∘ (f n))), { have A5A:(λ n:ℕ,conditional_sum (abs ∘ (f n)))= (λ n:ℕ,tsum (abs ∘ (f n))), { ext n, apply A4, }, rw ← A5A, apply A2, }, have A6:summable (λ n:ℕ,tsum (abs ∘ (f n))), { have A6A:(0:ℕ → ℝ) ≤ (λ n:ℕ,tsum (abs ∘ (f n))), { rw le_func_def, intro n, apply tsum_nonneg2, rw le_func_def, intro n2, apply abs_nonneg, }, apply summable_of_nonneg_of_conditionally_summable A6A A5, }, apply summable_function_uncurry3 A3 A6, end end lemma fubinis_theorem_discrete2 {f:ℕ → ℕ → ℝ}: (∀ i:ℕ, absolutely_summable (f i)) → conditionally_summable (λ n:ℕ,conditional_sum (abs ∘ (f n))) → (∑ₚ m, ∑ₚ n, f m n) = (∑ₚ n, ∑ₚ m, f m n) := begin intros A1 A2, have A3:summable (function.uncurry f) := summable_function_uncurry A1 A2, have A4: (∑' j, ∑' i, f j i) = (∑ₚ n, ∑ₚ m, f n m) := tsum_tsum_conditional_sum_conditional_sum_of_summable2 A3, have A5:(∑' j, ∑' i, f i j) = (∑ₚ n, ∑ₚ m, f m n) := tsum_tsum_conditional_sum_conditional_sum_of_summable A3, rw ← A4, rw ← A5, apply fubinis_theorem_discrete, rw function.uncurry_def at A3, apply A3, end lemma pair_conditional_sum_subst {g h:ℕ → ℕ → ℝ}: g = h → (∑ₚ (m n : ℕ), (g m n)) = (∑ₚ (m n : ℕ), (h m n)) := begin intro A1, rw A1, end lemma fubinis_theorem_discrete3 {g:ℕ → ℕ → ℝ}: conditionally_summable (λ n:ℕ,(finset.range n).sum (abs ∘ (g n))) → conditional_sum (λ n:ℕ,(finset.range n).sum ((g n))) = conditional_sum (λ m:ℕ, conditional_sum_lb (m + 1) (λ n:ℕ, g n m)) := begin intro A1, --∑' n, ∑_m=0^n-1 (g n m) let h:ℕ→ ℕ → ℝ:=λ m n, if (n < m) then (g m n) else (0:ℝ), begin have A1B:h = λ m n, if (n < m) then (g m n) else (0:ℝ) := rfl, -- A1C required. have A1E:∀ n, (∀ (b : ℕ), b ∉ (finset.range n) → (h n) b = 0), { intros n m A1EA, have A1EB:¬ (m < n), { intro A1EB1, apply A1EA, rw finset.mem_range, apply A1EB1, }, rw A1B, simp, rw if_neg, apply A1EB, }, have A1C:∀ n, summable (h n), { intro n, apply summable_of_ne_finset_zero, apply A1E, }, have A1D:∀ n, ∀ m∈ (finset.range n), (h n) m=(g n) m, { intros n m A1D1, rw A1B, rw finset.mem_range at A1D1, simp, rw if_pos, apply A1D1, }, have A2:∀ n, tsum (h n) = (finset.range n).sum (g n), { intro n, have A2A:tsum (h n) = (finset.range n).sum (h n), { rw tsum_eq_sum, apply A1E, }, rw A2A, rw finset_sum_subst2 (A1D n), }, have A3:∀ n, absolutely_summable (h n), { intro n, rw absolutely_summable_def, apply A1C, }, have A4:∀ n, summable (abs ∘ (h n)), { intro n, have A4A:=A1C n, rw summable_iff_abs_summable at A4A, apply A4A, }, -- Similar to proof of A2 above. have A5:∀ n, tsum (abs ∘ (h n)) = (finset.range n).sum (abs ∘ (g n)), { intro n, have A5A:(∀ (b : ℕ), b ∉ (finset.range n) → (abs ∘ (h n)) b = 0), { intros m A5A1, simp, apply A1E n m A5A1, }, have A5B:tsum (abs ∘ (h n)) = (finset.range n).sum (abs ∘ (h n)), { rw tsum_eq_sum, apply A5A, }, rw A5B, have A5C:∀ m∈ (finset.range n), (abs ∘ (h n)) m=(abs ∘ (g n)) m, { intros m A5C1, have A5C2 := A1D n m A5C1, simp, rw A5C2, }, rw finset_sum_subst2 (A5C), }, have A6:∀ n, conditional_sum (abs ∘ (h n)) = (finset.range n).sum (abs ∘ (g n)), { intro n, rw ← tsum_eq_conditional_sum_of_summable (A4 n), apply A5, }, -- A7 Required have A7:conditionally_summable (λ n:ℕ,conditional_sum (abs ∘ (h n))), { have A7A:(λ (n:ℕ), conditional_sum (abs ∘ (h n))) = (λ (n : ℕ), finset.sum (finset.range n) (abs ∘ g n)), { ext n, apply A6, }, rw A7A, apply A1, }, -- A8 Required have A8:(λ n:ℕ,(finset.range n).sum (g n)) = (λ n, conditional_sum (h n)), { ext n, rw ← tsum_eq_conditional_sum_of_summable (A1C n), rw (A2 n), }, rw A8, unfold conditional_sum_lb, have A9: (∑ₚ (m n : ℕ), ite (m + 1 ≤ n) (g n m) 0)= (∑ₚ (m n : ℕ), h n m), { apply pair_conditional_sum_subst, ext m n, rw A1B, simp, have A9A:m < n ∨ n ≤ m := lt_or_le m n, cases A9A, { rw if_pos, rw if_pos, apply A9A, apply A9A, }, { rw if_neg, rw if_neg, apply not_lt_of_le A9A, simp, rw nat.lt_succ_iff, apply A9A, } }, rw A9, apply fubinis_theorem_discrete2, apply A3, apply A7, end end lemma bounded_maclaurin_def {f:ℕ → ℝ}: bounded_maclaurin f ↔ (∀ x, has_classical_limit (λ (n : ℕ), x ^ n * f n) 0) := begin refl end lemma bounded_maclaurin_intro {f:ℕ → ℝ}: (∀ x, x≠ 0 → has_classical_limit (λ (n : ℕ), x ^ n * f n) 0) → bounded_maclaurin f := begin intros A1 x, have A2:x = 0 ∨ x ≠ 0 := eq_or_ne, cases A2, { rw A2, rw (@has_classical_limit_offset _ _ 1), have B1:(λ n:ℕ, 1 + n) = nat.succ, { ext n, rw nat_succ_one_add, }, rw B1, have A3:((λ (n : ℕ), 0 ^ n * f n) ∘ (nat.succ)) = (λ n:ℕ, 0), { ext n, simp, }, rw A3, apply has_classical_limit_const, }, { apply A1 x A2, } end lemma bounded_maclaurin_succ {f:ℕ → ℝ}: bounded_maclaurin f → bounded_maclaurin (f ∘ nat.succ) := begin unfold bounded_maclaurin, intros A1 x, apply bounded_maclaurin_intro, intros x A2, have A3:(λ (n : ℕ), x ^ n * (f ∘ nat.succ) n) = (λ (n : ℕ), x⁻¹ * (x ^ (nat.succ n) * (f ∘ nat.succ) n)), { ext n, simp, rw nat_succ_pow, rw ← mul_assoc, rw ← mul_assoc, rw inv_mul_cancel A2, rw one_mul, }, rw A3, apply has_classical_limit_mul_const_zero, have A5:(λ (n : ℕ), x ^ (nat.succ n) * (f ∘ nat.succ) n) = ((λ (n : ℕ), x ^ n * f n) ∘ (λ n:ℕ, 1 + n)), { ext n, simp, rw nat_succ_one_add, }, rw A5, rw ← @has_classical_limit_offset (λ (n : ℕ), x ^ n * f n) 0 1, apply A1, end lemma has_conditional_sum_succ {f:ℕ → ℝ} {x:ℝ}: has_conditional_sum f x → has_conditional_sum (f ∘ nat.succ) (x - (f 0)) := begin intro A1, rw has_conditional_sum_def, have A2:(λ (n : ℕ), finset.sum (finset.range n) (f ∘ nat.succ))= (λ (n : ℕ), finset.sum (finset.Ico 1 (nat.succ n)) (f)), { ext n, simp, have A2A:nat.succ n = n + 1, { simp, }, rw A2A, have A2B: (λ (n' : ℕ), f (nat.succ n')) = (λ n':ℕ, f (1 + n')), { ext n', have A2B1:nat.succ n' = 1 + n', { apply nat_succ_one_add, }, rw A2B1, }, rw A2B, have A2C:finset.Ico 0 n = finset.range n := finset.Ico.zero_bot n, rw ← A2C, rw finset.sum_Ico_add, }, have A3:(λ (n : ℕ), finset.sum (finset.range n) (f ∘ nat.succ))= (λ (n : ℕ), (-(f 0)) + (finset.sum (finset.Ico 0 (nat.succ n)) (f)) ), { rw A2, ext n, have A3A:0 < nat.succ n := nat.zero_lt_succ n, rw finset.sum_eq_sum_Ico_succ_bot A3A, rw ← add_assoc, simp, }, rw A3, have A4:x - (f 0) = (-(f 0)) + x, { rw sub_eq_add_neg, rw add_comm, }, rw A4, apply has_classical_limit_sum, apply has_classical_limit_const, have A5:(λ (n : ℕ), finset.sum (finset.Ico 0 (nat.succ n)) (f))= (λ (n : ℕ), finset.sum (finset.range n) f) ∘ (λ m:ℕ, 1 + m), { ext n, rw finset.Ico.zero_bot, simp, rw nat_succ_one_add, }, rw A5, rw ← has_classical_limit_offset, apply A1, end lemma is_maclaurin_fn_elim {f:ℕ → ℝ} {x:ℝ} {g:ℝ → ℝ}: is_maclaurin_fn f g → has_conditional_sum (λ (n : ℕ), f n * x ^ n) (g x) := begin intro A1, unfold is_maclaurin_fn at A1, apply A1 x, end lemma is_maclaurin_fn_unique {f:ℕ → ℝ} {g h:ℝ → ℝ}: is_maclaurin_fn f g → is_maclaurin_fn f h → g = h := begin intros A1 A2, unfold is_maclaurin_fn at A1, unfold is_maclaurin_fn at A2, ext x, have A3 := A1 x, have A4 := A2 x, apply has_conditional_sum_unique A3 A4, end lemma maclaurin_of_is_maclaurin_fn {f:ℕ → ℝ} {g:ℝ → ℝ}: is_maclaurin_fn f g → g = maclaurin f := begin intro A1, apply is_maclaurin_fn_unique A1, apply is_maclaurin_fn_of_bounded_maclaurin, apply bounded_maclaurin_of_is_maclaurin_fn f A1, end lemma has_sum_of_is_maclaurin_fn {f:ℕ → ℝ} {x:ℝ} {g:ℝ → ℝ}: is_maclaurin_fn f g → has_sum (λ (n : ℕ), f n * x ^ n) (g x) := begin intro A1, have A2 := bounded_maclaurin_of_is_maclaurin_fn _ A1, have A3 := @absolutely_summable_of_bounded_maclaurin _ x A2, have A4 := @is_maclaurin_fn_elim _ x _ A1, have A5 := has_absolute_sum_has_conditional_sum_has_sum_of_absolutely_summable A3, cases A5 with z A6, cases A6 with A7 A8, cases A8 with A9 A10, have A11 := has_conditional_sum_unique A9 A4, rw A11 at A10, apply A10, end lemma bounded_maclaurin_elim {f:ℕ → ℝ} {x:ℝ}: bounded_maclaurin f → has_conditional_sum (λ (n : ℕ), f n * x ^ n) (maclaurin f x) := begin intro A1, have A2:=is_maclaurin_fn_of_bounded_maclaurin A1, apply is_maclaurin_fn_elim A2, end lemma has_sum_of_bounded_maclaurin {f:ℕ → ℝ} {x:ℝ}: bounded_maclaurin f → has_sum (λ (n : ℕ), f n * x ^ n) (maclaurin f x) := begin intro A1, have A2:=is_maclaurin_fn_of_bounded_maclaurin A1, apply has_sum_of_is_maclaurin_fn A2, end lemma add_maclaurin {f:ℕ → ℝ} {x:ℝ}: bounded_maclaurin f → f 0 + x * maclaurin (f ∘ nat.succ) x = maclaurin f x := begin intro A1, have A2:bounded_maclaurin (f ∘ nat.succ) := bounded_maclaurin_succ A1, have A4:=@bounded_maclaurin_elim _ x A1, have A6:=@bounded_maclaurin_elim _ x A2, have A7: has_conditional_sum (λ (n : ℕ), x * ((f ∘ nat.succ) n * x ^ n)) (x * maclaurin (f ∘ nat.succ) x), { apply has_conditional_sum_mul_const_of_has_conditional_sum A6, }, have A8:(λ (n : ℕ), x * ((f ∘ nat.succ) n * x ^ n))=(λ (m : ℕ), (f m * x ^ m)) ∘ nat.succ, { ext n, simp, rw ← mul_assoc, rw mul_comm x, rw mul_assoc, refl, }, rw A8 at A7, have A9:has_conditional_sum ((λ (m : ℕ), (f m * x ^ m)) ∘ nat.succ) (maclaurin f x - ((λ (m : ℕ), (f m * x ^ m)) 0)), { apply has_conditional_sum_succ A4, }, have A10:(x * maclaurin (f ∘ nat.succ) x) = (maclaurin f x - ((λ (m : ℕ), (f m * x ^ m)) 0)), { apply has_conditional_sum_unique A7 A9, }, have A11:((λ (m : ℕ), (f m * x ^ m)) 0) = f 0, { simp, }, rw A11 at A10, have A12:f 0 + (x * maclaurin (f ∘ nat.succ) x) = f 0 + (maclaurin f x + (-f 0)), { rw A10, rw sub_eq_add_neg, }, rw A12, simp, end lemma monotone_nonneg_maclaurin {f:ℕ → ℝ} {x y:ℝ}: bounded_maclaurin f → 0 ≤ f → 0 ≤ x → x ≤ y → maclaurin f x ≤ maclaurin f y := begin intros A1 A2 A3 A4, have A7:=@bounded_maclaurin_elim _ x A1, have A8:=@bounded_maclaurin_elim _ y A1, apply has_conditional_sum_le A7 A8, rw le_func_def, intro n, apply mul_le_mul_of_nonneg_left, apply real_pow_mono A3 A4, apply A2, end lemma monotone_nonneg_maclaurin2 {f g:ℕ → ℝ} {x:ℝ}: bounded_maclaurin f → bounded_maclaurin g → 0 ≤ x → f ≤ g → maclaurin f x ≤ maclaurin g x := begin intros A1 A2 A3 A4, have A5:=@bounded_maclaurin_elim _ x A1, have A6:=@bounded_maclaurin_elim _ x A2, apply has_conditional_sum_le A5 A6, rw le_func_def, intro n, apply mul_le_mul_of_nonneg_right, apply A4, apply pow_nonneg, apply A3, end lemma bounded_maclaurin_zero:bounded_maclaurin 0 := begin unfold bounded_maclaurin, intro x, simp, apply has_classical_limit_const, end lemma maclaurin_zero_eq_zero {x:ℝ}:maclaurin 0 x = 0 := begin have A1:=@bounded_maclaurin_elim (0:ℕ → ℝ) x (bounded_maclaurin_zero), simp at A1, have A2:=has_conditional_sum_zero, apply has_conditional_sum_unique A1 A2, end lemma nonneg_maclaurin {f:ℕ → ℝ} {x:ℝ}: bounded_maclaurin f → 0 ≤ f → 0 ≤ x → 0 ≤ maclaurin f x := begin intros A1 A2 A3, rw ← @maclaurin_zero_eq_zero x, apply monotone_nonneg_maclaurin2, apply bounded_maclaurin_zero, apply A1, apply A3, apply A2, end lemma maclaurin_le_maclaurin_abs {f:ℕ → ℝ} {x:ℝ}: bounded_maclaurin f → abs (maclaurin f x) ≤ maclaurin (abs ∘ f) (abs x) := begin intro A1, have A2:= bounded_maclaurin_iff_bounded_maclaurin_abs.mp A1, have A3:=@has_sum_of_bounded_maclaurin _ x A1, have A4:=@has_sum_of_bounded_maclaurin _ (abs x) A2, have A5:(λ (n : ℕ), (abs ∘ f) n * abs x ^ n) = abs ∘ (λ (n : ℕ), f n * x ^ n), { ext n, simp, rw abs_mul, rw abs_pow, }, rw A5 at A4, apply abs_has_sum_le_has_sum_abs A3 A4, end --Note that this has to be a canonically ordered semiring, because 0 < 1. lemma mul_lt_of_lt_one {α:Type*} [ordered_semiring α] {a b:α}: 0 ≤ a → a < 1 → 0 < b → a * b < b := begin intros A0 A1 A2, have A2:a * b < 1 * b, { apply mul_lt_mul_of_pos_right, apply A1, apply A2, }, rw one_mul at A2, exact A2, end lemma mul_lt_mul_alt {α:Type*} [ordered_semiring α] {a b c d:α}: (a < c) → (b ≤ d) → (0 < d) → (0 ≤ a) → (a * b < c * d) := begin intros A1 A2 A3 A4, have A6:a * b ≤ a * d, { apply mul_le_mul_of_nonneg_left A2 A4, }, apply lt_of_le_of_lt, apply A6, { apply mul_lt_mul_of_pos_right A1 A3, } end /- This is significantly harder than I thought it was. f 0 + x * maclaurin (f ∘ nat.succ) x = maclaurin f x |x * maclaurin (f ∘ nat.succ) x| < |x| * maclaurin (abs ∘ f ∘ nat.succ) x monotone (maclaurin (abs ∘ f ∘ nat.succ)) x ≤ 1 → |x * maclaurin (f ∘ nat.succ) x| < |x| * maclaurin (abs ∘ f ∘ nat.succ) (1:ℝ) if maclaurin (abs ∘ f ∘ nat.succ) (1:ℝ)=0, then done. Else, δ = ε * (maclaurin (abs ∘ f ∘ nat.succ) (1:ℝ))⁻¹ -/ lemma continuous_at_zero_maclaurin {f:ℕ → ℝ}: bounded_maclaurin f → (has_classical_rlimit (maclaurin f) 0 (f 0)) := begin intro A1, intros ε A1A, have C1:bounded_maclaurin (abs ∘ f ∘ nat.succ), { -- ⊢ bounded_maclaurin (abs ∘ f ∘ nat.succ) apply bounded_maclaurin_iff_bounded_maclaurin_abs.mp, apply bounded_maclaurin_succ, exact A1, }, have C2:0 ≤ (abs ∘ f ∘ nat.succ), { -- ⊢ 0 ≤ (abs ∘ f ∘ nat.succ) rw le_func_def, intro n, simp, apply abs_nonneg, }, let r := (maclaurin (abs ∘ f ∘ nat.succ) (1:ℝ)), begin have A1B:r = (maclaurin (abs ∘ f ∘ nat.succ) (1:ℝ)) := rfl, have A2:r = 0 ∨ r ≠ 0 := eq_or_ne, cases A2, { apply exists.intro (1:ℝ), split, { apply zero_lt_one, }, { intros x' A3 A4, have B1:abs (maclaurin (f ∘ nat.succ) x') = 0, { apply le_antisymm, { have B1A:abs (maclaurin (f ∘ nat.succ) x') ≤ (maclaurin (abs ∘ f ∘ nat.succ) (abs x')), { apply maclaurin_le_maclaurin_abs, apply bounded_maclaurin_succ, exact A1, }, apply le_trans B1A, simp at A4, rw ← A2, rw A1B, apply monotone_nonneg_maclaurin, { -- ⊢ bounded_maclaurin (abs ∘ f ∘ nat.succ) apply C1, }, { -- ⊢ 0 ≤ (abs ∘ f ∘ nat.succ) apply C2, }, { apply abs_nonneg, }, { apply le_of_lt A4, } }, { apply abs_nonneg, } }, rw ← add_maclaurin A1, simp, rw abs_mul, rw B1, rw mul_zero, apply A1A, } }, { have A3:0 ≤ r, { apply nonneg_maclaurin, apply C1, rw le_func_def, intro n, simp, apply abs_nonneg, apply le_of_lt zero_lt_one, }, have A4:0 < r, { rw lt_iff_le_and_ne, split, apply A3, intro A4, apply A2, rw A4, }, apply exists.intro (min (ε * r⁻¹) (1:ℝ)), split, { apply lt_min, { -- ⊢ 0 < (ε * r⁻¹) apply mul_pos, apply A1A, apply inv_pos_of_pos A4, }, { -- 0 < 1 apply zero_lt_one, } }, { intros x' A5 A6, rw ← add_maclaurin A1, have A7:f 0 + x' * maclaurin (f ∘ nat.succ) x' - f 0 = x' * maclaurin (f ∘ nat.succ) x', { simp, }, rw A7, rw abs_mul, have A8:ε = ε * r⁻¹ * r, { rw mul_assoc, rw inv_mul_cancel, rw mul_one, apply A2, }, rw A8, simp at A6, cases A6 with A9 A10, apply mul_lt_mul_alt, { -- ⊢ abs x' < ε * r⁻¹ apply A9, }, { -- ⊢ abs (maclaurin (f ∘ nat.succ) x') ≤ r rw A1B, apply le_trans, { apply maclaurin_le_maclaurin_abs, apply bounded_maclaurin_succ A1, }, { apply monotone_nonneg_maclaurin C1, apply C2, apply abs_nonneg, apply le_of_lt, apply A10, } }, { apply A4, }, { apply abs_nonneg, }, } } end end /- has_sum_of_bounded_maclaurin : ∀ {f : ℕ → ℝ} {x : ℝ}, bounded_maclaurin f → has_sum (λ (n : ℕ), f n * x ^ n) (maclaurin f x) summable_function_uncurry3 : ∀ {f : ℕ → ℕ → ℝ}, (∀ (i : ℕ), summable (f i)) → summable (λ (n : ℕ), tsum (abs ∘ f n)) → summable (function.uncurry f) has_sum_sum_of_ne_finset_zero : ∀ {α : Type u_1} {β : Type u_2} [_inst_1 : add_comm_monoid α] [_inst_2 : topological_space α] {f : β → α} {s : finset β}, (∀ (b : β), b ∉ s → f b = 0) → has_sum f (finset.sum s f) -/ lemma has_sum_finset_sum {f:ℕ → ℝ} {S:finset ℕ}: has_sum (λ n, if (n ∈ S) then f n else 0) (finset.sum S f) := begin have A1:finset.sum S f = finset.sum S (λ n, if (n ∈ S) then f n else 0), { apply finset_sum_subst2, intros s A1A, rw if_pos, exact A1A, }, rw A1, apply has_sum_sum_of_ne_finset_zero, intros b A2, rw if_neg, exact A2, end lemma if_subst {α:Type*} {P Q:Prop} [D:decidable P] [decidable Q] {m n:α}: (P ↔ Q) → (if P then m else n) = (if Q then m else n) := begin intro A1, have A2:decidable P, { apply D, }, cases A2, { rw if_neg, rw if_neg, { -- ⊢ ¬Q intro A2A1, apply A2, rw A1, apply A2A1, }, apply A2, }, { rw if_pos, rw if_pos, { rw ← A1, apply A2, }, { apply A2, } } end lemma has_sum_finset_range_succ {f:ℕ → ℝ} {m:ℕ}: has_sum (λ n, if (n ≤ m) then f n else 0) (finset.sum (finset.range (nat.succ m)) f) := begin have A1:(λ n, if (n ≤ m) then f n else 0) = (λ n, if (n ∈ finset.range (nat.succ m)) then f n else 0), { ext n, rw @if_subst ℝ (n ≤ m) (n ∈ finset.range (nat.succ m)), rw finset.mem_range, symmetry, apply nat.lt_succ_iff, }, rw A1, apply has_sum_finset_sum, end /- finset.mul_sum : ∀ {α : Type u_1} {β : Type u_2} {s : finset α} {f : α → β} {b : β} [_inst_1 : semiring β], b * finset.sum s f = finset.sum s (λ (x : α), b * f x) -/ lemma binomial_maclaurinh {f:ℕ → ℝ} {x y:ℝ} {n:ℕ}: has_sum (λ (m : ℕ), if (m ≤ n) then (f n * x ^ m * y ^ (n - m) * ↑(nat.choose n m)) else 0) (f n * (x + y) ^ n) := begin have A3A:f n * (x + y) ^ n = f n * finset.sum (finset.range (nat.succ n)) (λ (m : ℕ), x ^ m * y ^ (n - m) * ↑(nat.choose n m)), { rw add_pow, }, /- have A4A:(λ (m2:ℕ), (f n) * (λ (m : ℕ), x ^ m * y ^ (n - m) * ↑(nat.choose n m)) m2) =(λ (m : ℕ), (f n) * x ^ m * y ^ (n - m) * ↑(nat.choose n m)), { ext m2, simp, rw ← mul_assoc, rw ← mul_assoc, },-/ rw finset.mul_sum at A3A, rw A3A, have A3B := @has_sum_finset_range_succ (λ (x_1 : ℕ), f n * (x ^ x_1 * y ^ (n - x_1) * ↑(nat.choose n x_1))) n, have A3C:(λ (n_1 : ℕ), ite (n_1 ≤ n) ((λ (x_1 : ℕ), f n * (x ^ x_1 * y ^ (n - x_1) * ↑(nat.choose n x_1))) n_1) 0) =(λ (m : ℕ), if (m ≤ n) then (f n * x ^ m * y ^ (n - m) * ↑(nat.choose n m)) else 0), { ext m, simp, have A3CA:(f n * (x ^ m * y ^ (n - m) * ↑(nat.choose n m))) = (f n * x ^ m * y ^ (n - m) * ↑(nat.choose n m)), { rw ← mul_assoc, rw ← mul_assoc, }, rw A3CA, }, rw A3C at A3B, apply A3B, end lemma fubinis_theorem_discrete6 {q:ℕ → ℕ → ℝ} {z:ℝ}: (∀ n:ℕ, summable (q n)) → summable (λ n:ℕ, tsum (abs ∘ (q n))) → has_sum (λ n:ℕ, tsum (q n)) z → has_sum (function.uncurry q) z := begin intros A1 A2 A3, have A4:summable (function.uncurry q) := summable_function_uncurry3 A1 A2, let g := λ n, tsum (q n), let f := function.uncurry q, begin have B1:g = λ n, tsum (q n) := rfl, have B2:f = function.uncurry q := rfl, have B3:summable f := A4, have B4:has_sum f (tsum f) := has_sum_tsum B3, have B5:(∀ (b : ℕ), has_sum (λ (c : ℕ), f (b, c)) (g b)), { intro b, rw B2, rw B1, simp, rw function.uncurry_def, simp, apply has_sum_tsum, apply A1, }, rw ← B2, have B6:has_sum g (tsum f), { apply has_sum_prod B5 B4, }, rw B1 at B6, have B7:tsum f = z, { apply has_sum.unique B6 A3, }, rw ← B7, apply has_sum_tsum B3, end end lemma if_map {α β:Type*} {f:α → β} {u v:α} {P:Prop} [D:decidable P]: f (if P then u else v) = (if P then (f u) else (f v)) := begin have A1:=D, cases A1, { rw if_neg, rw if_neg, apply A1, apply A1, }, { rw if_pos, rw if_pos, apply A1, apply A1, } end -- This theorem is inferior in all ways to fubinis_theorem_discrete4 lemma fubinis_theorem_discrete5 {q:ℕ → ℕ → ℝ} {z:ℝ}: summable (λ n:ℕ, (finset.sum (finset.range (nat.succ n)) (abs ∘ (q n)))) → has_sum (λ n:ℕ, (finset.sum (finset.range (nat.succ n)) (q n))) z → has_sum (λ (p : ℕ × ℕ), if (p.snd ≤ p.fst) then (q (p.fst) (p.snd)) else 0) z := begin intros A1 A2, /- Alright, we can lift this theorem up to fubinis_theorem_discrete6. First, we create a variant of q so that it is zero everywhere we don't want to sum. This proof is longer than I anticipated. -/ let q2:ℕ → ℕ → ℝ := (λ m n:ℕ, if (n ≤ m) then (q m n) else 0), begin have A5:q2 = (λ m n:ℕ, if (n ≤ m) then (q m n) else 0) := rfl, have A3:∀ m, (has_sum (λ n, if (n ≤ m) then q m n else 0) (finset.sum (finset.range (nat.succ m)) (q m))), { intro m, apply has_sum_finset_range_succ, }, have A4:function.uncurry q2 = (λ (p : ℕ × ℕ), if (p.snd ≤ p.fst) then (q (p.fst) (p.snd)) else 0), { rw A5, ext p, rw function.uncurry_def, }, rw ← A4, apply fubinis_theorem_discrete6, { -- ⊢ ∀ (n : ℕ), summable (q2 n) intro m, rw A5, have B1:has_sum ((λ (m n : ℕ), ite (n ≤ m) (q m n) 0) m) (finset.sum (finset.range (nat.succ m)) (q m)), { apply @has_sum_finset_range_succ (q m), }, apply has_sum.summable B1, }, { -- ⊢ summable (λ (n : ℕ), tsum (abs ∘ q2 n)) rw A5, have C1:(λ (n : ℕ), tsum (abs ∘ (λ (m n : ℕ), ite (n ≤ m) (q m n) 0) n)) = (λ n:ℕ, (finset.sum (finset.range (nat.succ n)) (abs ∘ (q n)))), { ext n, simp, have C1A:(abs ∘ λ (n_1 : ℕ), ite (n_1 ≤ n) (q n n_1) 0)= (λ n_1:ℕ, ite (n_1 ≤ n) (abs (q n n_1)) 0), { ext n_1, simp, have C1A1:(n_1 ≤ n) ∨ ¬(n_1≤ n) := le_or_not_le, cases C1A1, { rw if_pos C1A1, rw if_pos C1A1, }, { rw if_neg C1A1, rw if_neg C1A1, rw abs_of_nonneg, simp, } }, rw C1A, have C1B:has_sum (λ (n_1 : ℕ), ite (n_1 ≤ n) (abs (q n n_1)) 0) (finset.sum (finset.range (nat.succ n)) (λ (x : ℕ), abs (q n x))), { apply @has_sum_finset_range_succ, }, have C1C:=has_sum.summable C1B, have C1D:=has_sum_tsum C1C, apply has_sum.unique C1D C1B, }, rw C1, apply A1, }, { -- ⊢ has_sum (λ (n : ℕ), tsum (q2 n)) z rw A5, have D1:(λ (n : ℕ), tsum (q2 n))=(λ m, (finset.sum (finset.range (nat.succ m)) (q m))), { ext m, rw A5, have D1A:has_sum ((λ (m n : ℕ), ite (n ≤ m) (q m n) 0) m) (finset.sum (finset.range (nat.succ m)) (q m)), { apply @has_sum_finset_range_succ (q m), }, have D1B:=has_sum.summable D1A, have D1C:=has_sum_tsum D1B, apply has_sum.unique D1C D1A, }, rw D1, apply A2, } end end lemma fubinis_theorem_discrete4 {q:ℕ → ℕ → ℝ} {z:ℝ}: summable (λ n:ℕ, (finset.sum (finset.range (nat.succ n)) (abs ∘ (q n)))) → has_sum (λ n:ℕ, (finset.sum (finset.range (nat.succ n)) (q n))) z → has_sum (λ p:ℕ × ℕ, q (p.fst + p.snd) (p.fst)) z := begin intros A1 A2, /- This consists of two steps. In the first step, we show: has_sum (λ p:ℕ × ℕ, if (j ≤ i) then (f i j) else 0) z Then, we use has_sum_iff_has_sum_of_ne_zero_bij We have the map <m,n> → <m + n, n> or we can use We have the map <m,n> → <m + n, m> -/ -- The indices are all backwards!!! let f:ℕ × ℕ → ℝ := (λ p:ℕ × ℕ, q (p.fst + p.snd) (p.fst)), let g:ℕ × ℕ → ℝ := (λ p:ℕ × ℕ, if (p.snd ≤ p.fst) then (q p.fst p.snd) else 0), let i:function.support g → (ℕ × ℕ):=λ a:function.support g, (a.val.snd, a.val.fst - a.val.snd), begin let A3:f = (λ p:ℕ × ℕ, q (p.fst + p.snd) (p.fst)) := rfl, let A4:g = (λ p:ℕ × ℕ, if (p.snd ≤ p.fst) then (q p.fst p.snd) else 0) := rfl, let A5:i = λ a:function.support g, (a.val.snd, a.val.fst - a.val.snd) := rfl, have A6:∀ p:ℕ × ℕ, g p ≠ 0 → (p.snd ≤ p.fst), { -- Proof by contradiction. intro p, have A6A:(p.snd ≤ p.fst) ∨ ¬ (p.snd ≤ p.fst) := le_or_not_le, cases A6A, { intro A6A1, apply A6A, }, { intro A6A1, exfalso, apply A6A1, rw A4, simp, rw if_neg, apply A6A, } }, rw ← A3, rw (@has_sum_iff_has_sum_of_ne_zero_bij ℝ (ℕ × ℕ) (ℕ × ℕ) _ _ f z g i), { -- ⊢ has_sum g z rw A4, -- ⊢ has_sum (λ (p : ℕ × ℕ), ite (p.snd ≤ p.fst) (q (p.fst) (p.snd)) 0) z apply fubinis_theorem_discrete5 A1 A2, }, { -- ⊢ ∀ ⦃c₁ c₂ : ℕ × ℕ⦄ (h₁ : g c₁ ≠ 0) (h₂ : g c₂ ≠ 0), i h₁ = i h₂ → c₁ = c₂ intros p1 p2 C1, have G1:p1.val = (@coe (function.support g) (ℕ × ℕ) _ p1) := rfl, have G2:p2.val = (@coe (function.support g) (ℕ × ℕ) _ p2) := rfl, rw A5 at C1, simp at C1, cases C1 with C4 C5, rw C4 at C5, have C6 := A6 p1.val p1.property, have C7 := A6 p2.val p2.property, rw ← G1 at C4, rw ← G2 at C4, rw C4 at C6, rw ← G1, rw ← G2, have C5B := nat.sub_cancel C6 C7 C5, ext, apply C5B, apply C4, }, { -- ⊢ ∀ ⦃b : ℕ × ℕ⦄, f b ≠ 0 → (∃ (c : ℕ × ℕ) (h : g c ≠ 0), i h = b) intros p D1, let p_img:ℕ × ℕ := ⟨ p.fst + p.snd, p.fst ⟩, begin have D2:p_img = ⟨ p.fst + p.snd, p.fst ⟩ := rfl, have D3:g p_img = f p, { rw D2, rw A4, simp, }, rw set.mem_range, have G3:p_img ∈ function.support g, { rw function.mem_support, rw function.mem_support at D1, rw D3, apply D1, }, apply exists.intro (@subtype.mk (ℕ × ℕ) (function.support g) p_img G3), rw A5, simp, end }, { -- ⊢ ∀ ⦃c : ℕ × ℕ⦄ (h : g c ≠ 0), f (i h) = g c intro x, rw A5, have H1:x.val = (@coe (function.support g) (ℕ × ℕ) _ x) := rfl, rw A3, rw ← H1, have A4B:g x.val = (λ (p : ℕ × ℕ), ite (p.snd ≤ p.fst) (q p.fst p.snd) 0) x.val := rfl, rw A4B, simp, have H2:(@coe (function.support g) (ℕ × ℕ) _ x).snd ≤ (@coe (function.support g) (ℕ × ℕ) _ x).fst, { apply A6, apply x.property, }, rw if_pos, rw nat.add_sub_cancel' H2, apply H2, } end end /- Rearranging the binomial into a function -/ lemma binomial_maclaurin2A {f:ℕ → ℝ} {x y:ℝ} {n:ℕ}: f n * (x + y) ^ n = finset.sum (finset.range (nat.succ n)) (λ (m : ℕ), f n * (x ^ m * y ^ (n - m) * ↑(nat.choose n m))) := begin have A3A:f n * (x + y) ^ n = f n * finset.sum (finset.range (nat.succ n)) (λ (m : ℕ), x ^ m * y ^ (n - m) * ↑(nat.choose n m)), { rw add_pow, }, rw finset.mul_sum at A3A, apply A3A, end lemma binomial_maclaurin2B {f:ℕ → ℝ} {x y:ℝ}: bounded_maclaurin f → has_sum (λ n:ℕ, finset.sum (finset.range (nat.succ n)) (λ (m : ℕ), f n * (x ^ m * y ^ (n - m) * ↑(nat.choose n m)))) ((maclaurin f) (x + y)) := begin intro A1, have A2:=@has_sum_of_bounded_maclaurin f (x + y) A1, { have A3A: (λ (n : ℕ), f n * (x + y) ^ n) = (λ n:ℕ, finset.sum (finset.range (nat.succ n)) (λ (m : ℕ), f n * (x ^ m * y ^ (n - m) * ↑(nat.choose n m)))), { ext n, apply @binomial_maclaurin2A f x y, }, rw A3A at A2, apply A2, }, end lemma binomial_maclaurin {f:ℕ → ℝ} {x y:ℝ}: bounded_maclaurin f → has_sum (λ p:ℕ × ℕ, x^(p.fst) * y^(p.snd) * (nat.choose (p.fst + p.snd) p.fst) * (f (p.fst + p.snd))) ((maclaurin f) (x + y)) := begin intro A1, have A2:=@has_sum_of_bounded_maclaurin f (x + y) A1, have A3:=@binomial_maclaurin2B f x y A1, let g := (λ n:ℕ, (λ (m : ℕ), f n * (x ^ m * y ^ (n - m) * ↑(nat.choose n m)))), begin have A4:g = (λ n:ℕ, (λ (m : ℕ), f n * (x ^ m * y ^ (n - m) * ↑(nat.choose n m)))) := rfl, have A5:(λ n:ℕ, finset.sum (finset.range (nat.succ n)) (λ (m : ℕ), f n * (x ^ m * y ^ (n - m) * ↑(nat.choose n m)))) =(λ n:ℕ, finset.sum (finset.range (nat.succ n)) (g n)), { ext n, rw A4, }, rw A5 at A3, have A6:summable (λ n:ℕ, (finset.sum (finset.range (nat.succ n)) (abs ∘ (g n)))), { rw A4, have A6A:bounded_maclaurin (abs ∘ f) := bounded_maclaurin_iff_bounded_maclaurin_abs.mp A1, have A6B:=@binomial_maclaurin2B (abs ∘ f) (abs x) (abs y) A6A, have A6C:summable (λ (n : ℕ), finset.sum (finset.range (nat.succ n)) (λ (m : ℕ), (abs ∘ f) n * (abs x ^ m * abs y ^ (n - m) * ↑(nat.choose n m)))), { apply exists.intro (maclaurin (abs ∘ f) (abs x + abs y)) A6B, }, have A6D:(λ (n : ℕ), finset.sum (finset.range (nat.succ n)) (λ (m : ℕ), (abs ∘ f) n * (abs x ^ m * abs y ^ (n - m) * ↑(nat.choose n m)))) = (λ n:ℕ, (finset.sum (finset.range (nat.succ n)) (abs ∘ (g n)))), { ext n, apply finset_sum_subst2, intros m A6D1, rw A4, simp, rw abs_mul, rw abs_mul, rw abs_mul, rw abs_pow, rw abs_pow, simp, }, rw A6D at A6C, apply A6C, }, have A8 := fubinis_theorem_discrete4 A6 A3, have A9:(λ (p : ℕ × ℕ), (λ (n : ℕ), g n) (p.fst + p.snd) (p.fst)) = (λ (p : ℕ × ℕ), x ^ p.fst * y ^ p.snd * ↑(nat.choose (p.fst + p.snd) (p.fst)) * f (p.fst + p.snd)), { ext p, rw A4, simp, rw mul_comm _ (f (p.fst + p.snd)), }, rw A9 at A8, apply A8, end end lemma binomial_maclaurin2 {f:ℕ → ℝ} {x y:ℝ} {m:ℕ}: bounded_maclaurin f → summable (λ n:ℕ, x^m * y^n * (nat.choose (m + n) m) * (f (m + n))) := begin intro A1, have A2 := @binomial_maclaurin f x y A1, have A3 := has_sum.summable A2, apply summable_right_of_summable3 A3, end lemma bounded_maclaurin_of_conditionally_summable {f:ℕ → ℝ}: (∀ y:ℝ, conditionally_summable (λ n:ℕ, f n * y^n)) → bounded_maclaurin f := begin intro A1, apply @bounded_maclaurin_of_is_maclaurin_fn f (λ y, conditional_sum (λ n:ℕ, f n * y^n)), unfold is_maclaurin_fn, intro x, apply has_conditional_sum_conditional_sum_of_conditionally_summable (A1 x), end lemma bounded_maclaurin_of_summable {f:ℕ → ℝ}: (∀ y:ℝ, summable (λ n:ℕ, f n * y^n)) → bounded_maclaurin f := begin intro A1, apply bounded_maclaurin_of_conditionally_summable, intro y, apply conditionally_summable_of_summable, apply A1, end lemma binomial_maclaurin3 {f:ℕ → ℝ} {y:ℝ} {m:ℕ}: bounded_maclaurin f → summable (λ n:ℕ, y^n * (nat.choose (m + n) m) * (f (m + n))) := begin intro A1, have A2:=@binomial_maclaurin2 f (1:ℝ) y m A1, simp at A2, apply A2, end lemma bounded_maclaurin_binomial_maclaurin {f:ℕ → ℝ} {m:ℕ}: bounded_maclaurin f → bounded_maclaurin (λ n:ℕ, (nat.choose (m + n) m) * (f (m + n))) := begin intro A1, apply bounded_maclaurin_of_summable, intro y, have A2:(λ n:ℕ, y^n * (nat.choose (m + n) m) * (f (m + n))) =(λ n:ℕ, (nat.choose (m + n) m) * (f (m + n)) * y^n), { ext n, rw mul_assoc, rw mul_comm, }, rw ← A2, apply binomial_maclaurin3 A1, end /- binomial_maclaurin : ∀ {f : ℕ → ℝ} {x y : ℝ}, bounded_maclaurin f → has_sum (λ (p : ℕ × ℕ), x ^ p.fst * y ^ p.snd * ↑(nat.choose (p.fst + p.snd) (p.fst)) * f (p.fst + p.snd)) (maclaurin f (x + y)) fubinis_theorem_discrete_left : ∀ {f : ℕ → ℕ → ℝ}, summable (λ (p : ℕ × ℕ), f (p.fst) (p.snd)) → ((∑' (p : ℕ × ℕ), f (p.fst) (p.snd)) = ∑' (i j : ℕ), f i j) -/ /- Looking at this more closely, this is a perfect candidate for applying has_sum_prod DIRECTLY. has_sum_prod : ∀ {α : Type u_1} {β : Type u_2} [ACM : add_comm_monoid α] [T : topological_space α] [TAM : has_continuous_add α] [R : regular_space α] {γ : Type u_3} {f : β × γ → α} {g : β → α} {a : α}, (∀ (b : β), has_sum (λ (c : γ), f (b, c)) (g b)) → has_sum f a → has_sum g a -/ lemma binomial_maclaurin4 {f:ℕ → ℝ} {x y:ℝ}: bounded_maclaurin f → has_sum (λ m:ℕ, x^m * tsum (λ n:ℕ, y^n * (nat.choose (m + n) m) * (f (m + n)))) ((maclaurin f) (x + y)) := begin intro A1, have A2:=@binomial_maclaurin f x y A1, apply has_sum_prod _ A2, simp, intro m, have A7:=@binomial_maclaurin3 f y m A1, have A8:=has_sum_tsum A7, have A9:=has_sum.mul_left (x^m) A8, simp at A9, have A10:(λ (b : ℕ), x ^ m * (y ^ b * ↑(nat.choose (m + b) m) * f (m + b)))= (λ (c : ℕ), x ^ m * y ^ c * ↑(nat.choose (m + c) m) * f (m + c)), { ext b, rw mul_assoc (x^m), rw mul_assoc (x^m), }, rw ← A10, apply A9, end lemma has_conditional_sum_of_has_sum {f:ℕ → ℝ} {x:ℝ}: has_sum f x → has_conditional_sum f x := begin intro A1, have A2:=has_sum.summable A1, have A3:=has_absolute_sum_has_conditional_sum_has_sum_of_summable A2, cases A3 with x2 A4, cases A4 with A5 A6, cases A6 with A7 A8, have A9 := has_sum.unique A8 A1, subst x2, apply A7, end lemma binomial_maclaurin5 {f:ℕ → ℝ} {y:ℝ}: bounded_maclaurin f → is_maclaurin_fn (λ m:ℕ, tsum (λ n:ℕ, y^n * (nat.choose (m + n) m) * (f (m + n)))) (λ x, ((maclaurin f) (x + y))) := begin intro A1, unfold is_maclaurin_fn, intro x, apply has_conditional_sum_of_has_sum, have A2:(λ (n : ℕ), (∑' (n_1 : ℕ), y ^ n_1 * ↑(nat.choose (n + n_1) n) * f (n + n_1)) * x ^ n) = (λ m:ℕ, x^m * tsum (λ n:ℕ, y^n * (nat.choose (m + n) m) * (f (m + n)))), { ext n, rw mul_comm, }, rw A2, apply binomial_maclaurin4, apply A1, end lemma tsum_eq_maclaurin_of_bounded_maclaurin {f:ℕ → ℝ} {y:ℝ}: bounded_maclaurin f → (∑' n:ℕ, f n * y^n) = maclaurin f y := begin intro A1, unfold maclaurin, have A2:=@absolutely_summable_of_bounded_maclaurin f y A1, rw absolutely_summable_def at A2, apply tsum_eq_conditional_sum_of_summable A2, end lemma binomial_maclaurin6 {f:ℕ → ℝ} {x y:ℝ}: bounded_maclaurin f → ((maclaurin f) (x + y)) = (maclaurin f y) + x * (maclaurin ((λ m:ℕ, tsum (λ n:ℕ, y^n * (nat.choose (m + n) m) * (f (m + n))))∘ nat.succ) x) := begin intro A1, have A2:=@binomial_maclaurin5 f y A1, have A3:=bounded_maclaurin_of_is_maclaurin_fn _ A2, have A4:=(@maclaurin_of_is_maclaurin_fn _ _ A2), have A5: (λ (x : ℝ), maclaurin f (x + y)) x = maclaurin (λ (m : ℕ), ∑' (n : ℕ), y ^ n * ↑(nat.choose (m + n) m) * f (m + n)) x, { rw A4, }, simp at A5, rw A5, rw ← add_maclaurin A3, simp, have A6:(λ n, f n * y^n) = (λ n, y^n * f n), { ext n, rw mul_comm, }, rw ← A6, apply @tsum_eq_maclaurin_of_bounded_maclaurin f y A1 end lemma bounded_maclaurin_approx_deriv {f:ℕ → ℝ} {x:ℝ}: bounded_maclaurin f → bounded_maclaurin ((λ m:ℕ, tsum (λ n:ℕ, x^n * (nat.choose (m + n) m) * (f (m + n))))∘ nat.succ) := begin intro A1, apply bounded_maclaurin_succ, apply bounded_maclaurin_of_is_maclaurin_fn, apply binomial_maclaurin5 A1, end lemma approx_deriv_is_almost_maclaurin_fn {f:ℕ → ℝ} {x h:ℝ}: bounded_maclaurin f → h≠ 0 → maclaurin ((λ m:ℕ, tsum (λ n:ℕ, x^n * (nat.choose (m + n) m) * (f (m + n))))∘ nat.succ) h = ((maclaurin f) (x + h) - (maclaurin f) x)/h := begin intros A1 A2, rw add_comm x h, rw binomial_maclaurin6 A1, simp, rw mul_div_cancel_left, apply A2, end lemma deriv_maclaurin_def2 {f:ℕ → ℝ} {x:ℝ}: bounded_maclaurin f → deriv_maclaurin f x = ∑' (n : ℕ), x ^ n * (1 + ↑n) * f (n + 1) := begin intro A1, unfold deriv_maclaurin, have A2:=is_maclaurin_derivative_bounded f A1, have A3:=bounded_maclaurin_of_is_maclaurin_fn _ A2, have A4:=@tsum_eq_maclaurin_of_bounded_maclaurin _ x A3, rw ← A4, have A5: (λ (n : ℕ), deriv_maclaurin_series f n * x ^ n) = λ (n : ℕ), x ^ n * (1 + ↑n) * f (n + 1), { ext n, unfold deriv_maclaurin_series, simp, have A5A:nat.succ n = n + 1, { simp, }, rw A5A, rw mul_comm _ (x^n), rw mul_comm (f (n + 1)), rw mul_assoc, rw add_comm 1 (n:ℝ), }, rw A5, end lemma deriv_maclaurin_def {f:ℕ → ℝ} {x:ℝ} {g:ℝ → ℝ}: is_maclaurin_fn f g → has_classical_rderiv_at g (deriv_maclaurin f x) x := begin intro A1, rw has_classical_rderiv_at_def3, rw maclaurin_of_is_maclaurin_fn A1, have A2:=bounded_maclaurin_of_is_maclaurin_fn f A1, have A4:=@bounded_maclaurin_approx_deriv _ x A2, have A5:=continuous_at_zero_maclaurin A4, have A7:(deriv_maclaurin f x)= (((λ (m : ℕ), ∑' (n : ℕ), x ^ n * ↑(nat.choose (m + n) m) * f (m + n)) ∘ nat.succ) 0), { simp, have A7A:(λ (n : ℕ), x ^ n * (1 + ↑n) * f (1 + n))= (λ (n : ℕ), x ^ n * (1 + ↑n) * f (n + 1)), { ext n, rw add_comm n 1, }, rw A7A, apply @deriv_maclaurin_def2 _ x A2, }, rw ← A7 at A5, rw has_classical_rlimit_def at A5, rw has_classical_rlimit_def, intros ε B1, have B2 := A5 ε B1, cases B2 with δ B3, cases B3 with B4 B5, apply exists.intro δ, split, { apply B4, }, { intros h B6 B7, have B8 := B5 h B6 B7, have B9 := @approx_deriv_is_almost_maclaurin_fn f x h A2 B6, rw B9 at B8, apply B8, }, end lemma deriv_maclaurin_def3 {f:ℕ → ℝ} {x:ℝ} {g:ℝ → ℝ}: is_maclaurin_fn f g → has_deriv_at g (deriv_maclaurin f x) x := begin intro A1, rw ← has_classical_rderiv_at_def, apply deriv_maclaurin_def A1, end lemma inv_mul_mul_cancel {a b:ℝ}:(a ≠ 0) → (a * b)⁻¹ * a = b⁻¹ := begin intro A1, rw mul_inv', rw mul_assoc, rw mul_comm _ a, rw ← mul_assoc, rw inv_mul_cancel, rw one_mul, apply A1, end lemma deriv_maclaurin_series_exp_maclaurin: deriv_maclaurin_series (exp_maclaurin) = exp_maclaurin := begin rw exp_maclaurin_def, rw deriv_maclaurin_series_def, ext n, simp, rw inv_mul_mul_cancel, have A1:0 < 1 + (n:ℝ), { apply lt_of_lt_of_le, apply zero_lt_one, simp, }, intro A2, rw add_comm at A2, rw A2 at A1, apply lt_irrefl (0:ℝ) A1, end lemma has_deriv_at_exp {x:ℝ}: has_deriv_at (real.exp) (real.exp x) x := begin have A1:is_maclaurin_fn exp_maclaurin real.exp := is_maclaurin_fn_exp, have A2:has_deriv_at (real.exp) (deriv_maclaurin (exp_maclaurin) x) x, { apply deriv_maclaurin_def3 A1, }, have A3:deriv_maclaurin (exp_maclaurin) = real.exp, { have A3A:deriv_maclaurin_series (exp_maclaurin) = exp_maclaurin := deriv_maclaurin_series_exp_maclaurin, unfold deriv_maclaurin, rw A3A, symmetry, apply maclaurin_of_is_maclaurin_fn A1, }, rw A3 at A2, exact A2, end
50da1ade12a8e893590930a42b3a7d28dd1afa06
2f8bf12144551bc7d8087a6320990c4621741f3d
/library/init/util.lean
ac38c9113e7acaa7f41bec5d2e89488236528078
[ "Apache-2.0" ]
permissive
jesse-michael-han/lean4
eb63a12960e69823749edceb4f23fd33fa2253ce
fa16920a6a7700cabc567aa629ce4ae2478a2f40
refs/heads/master
1,589,935,810,594
1,557,177,860,000
1,557,177,860,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
680
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.string.basic universes u /- debugging helper functions -/ @[extern cpp inline "lean::dbg_trace(#2, #3)"] def dbgTrace {α : Type u} (s : String) (f : Unit → α) : α := f () /- Display the given message if `a` is shared, that is, RC(a) > 1 -/ @[extern cpp inline "lean::dbg_trace_if_shared(#2, #3)"] def dbgTraceIfShared {α : Type u} (s : String) (a : α) : α := a @[extern cpp inline "lean::dbg_sleep(#2, #3)"] def dbgSleep {α : Type u} (ms : UInt32) (f : Unit → α) : α := f ()
ccfce9b11e8c20849d6d0abe06cf72cef71088f4
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/finset/n_ary.lean
b28c3a645e4be7a84e7f223ed48ba7b870d8600d
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
15,539
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 data.finset.prod /-! # N-ary images of finsets This file defines `finset.image₂`, the binary image of finsets. This is the finset version of `set.image2`. This is mostly useful to define pointwise operations. ## Notes This file is very similar to the n-ary section of `data.set.basic` and to `order.filter.n_ary`. Please keep them in sync. We do not define `finset.image₃` as its only purpose would be to prove properties of `finset.image₂` and `set.image2` already fulfills this task. -/ open function set namespace finset variables {α α' β β' γ γ' δ δ' ε ε' : Type*} [decidable_eq α'] [decidable_eq β'] [decidable_eq γ] [decidable_eq γ'] [decidable_eq δ] [decidable_eq δ'] [decidable_eq ε] [decidable_eq ε'] {f f' : α → β → γ} {g g' : α → β → γ → δ} {s s' : finset α} {t t' : finset β} {u u' : finset γ} {a a' : α} {b b' : β} {c : γ} /-- The image of a binary function `f : α → β → γ` as a function `finset α → finset β → finset γ`. Mathematically this should be thought of as the image of the corresponding function `α × β → γ`. -/ def image₂ (f : α → β → γ) (s : finset α) (t : finset β) : finset γ := (s ×ˢ t).image $ uncurry f @[simp] lemma mem_image₂ : c ∈ image₂ f s t ↔ ∃ a b, a ∈ s ∧ b ∈ t ∧ f a b = c := by simp [image₂, and_assoc] @[simp, norm_cast] lemma coe_image₂ (f : α → β → γ) (s : finset α) (t : finset β) : (image₂ f s t : set γ) = set.image2 f s t := set.ext $ λ _, mem_image₂ lemma card_image₂_le (f : α → β → γ) (s : finset α) (t : finset β) : (image₂ f s t).card ≤ s.card * t.card := card_image_le.trans_eq $ card_product _ _ lemma card_image₂_iff : (image₂ f s t).card = s.card * t.card ↔ (s ×ˢ t : set (α × β)).inj_on (λ x, f x.1 x.2) := by { rw [←card_product, ←coe_product], exact card_image_iff } lemma card_image₂ (hf : injective2 f) (s : finset α) (t : finset β) : (image₂ f s t).card = s.card * t.card := (card_image_of_injective _ hf.uncurry).trans $ card_product _ _ lemma mem_image₂_of_mem (ha : a ∈ s) (hb : b ∈ t) : f a b ∈ image₂ f s t := mem_image₂.2 ⟨a, b, ha, hb, rfl⟩ lemma mem_image₂_iff (hf : injective2 f) : f a b ∈ image₂ f s t ↔ a ∈ s ∧ b ∈ t := by rw [←mem_coe, coe_image₂, mem_image2_iff hf, mem_coe, mem_coe] lemma image₂_subset (hs : s ⊆ s') (ht : t ⊆ t') : image₂ f s t ⊆ image₂ f s' t' := by { rw [←coe_subset, coe_image₂, coe_image₂], exact image2_subset hs ht } lemma image₂_subset_left (ht : t ⊆ t') : image₂ f s t ⊆ image₂ f s t' := image₂_subset subset.rfl ht lemma image₂_subset_right (hs : s ⊆ s') : image₂ f s t ⊆ image₂ f s' t := image₂_subset hs subset.rfl lemma image_subset_image₂_left (hb : b ∈ t) : (λ a, f a b) '' s ⊆ image₂ f s t := ball_image_of_ball $ λ a ha, mem_image₂_of_mem ha hb lemma image_subset_image₂_right (ha : a ∈ s) : f a '' t ⊆ image₂ f s t := ball_image_of_ball $ λ b, mem_image₂_of_mem ha lemma forall_image₂_iff {p : γ → Prop} : (∀ z ∈ image₂ f s t, p z) ↔ ∀ (x ∈ s) (y ∈ t), p (f x y) := by simp_rw [←mem_coe, coe_image₂, forall_image2_iff] @[simp] lemma image₂_subset_iff : image₂ f s t ⊆ u ↔ ∀ (x ∈ s) (y ∈ t), f x y ∈ u := forall_image₂_iff @[simp] lemma image₂_nonempty_iff : (image₂ f s t).nonempty ↔ s.nonempty ∧ t.nonempty := by { rw [←coe_nonempty, coe_image₂], exact image2_nonempty_iff } lemma nonempty.image₂ (hs : s.nonempty) (ht : t.nonempty) : (image₂ f s t).nonempty := image₂_nonempty_iff.2 ⟨hs, ht⟩ lemma nonempty.of_image₂_left (h : (image₂ f s t).nonempty) : s.nonempty := (image₂_nonempty_iff.1 h).1 lemma nonempty.of_image₂_right (h : (image₂ f s t).nonempty) : t.nonempty := (image₂_nonempty_iff.1 h).2 @[simp] lemma image₂_empty_left : image₂ f ∅ t = ∅ := coe_injective $ by simp @[simp] lemma image₂_empty_right : image₂ f s ∅ = ∅ := coe_injective $ by simp @[simp] lemma image₂_eq_empty_iff : image₂ f s t = ∅ ↔ s = ∅ ∨ t = ∅ := by simp_rw [←not_nonempty_iff_eq_empty, image₂_nonempty_iff, not_and_distrib] @[simp] lemma image₂_singleton_left : image₂ f {a} t = t.image (λ b, f a b) := ext $ λ x, by simp @[simp] lemma image₂_singleton_right : image₂ f s {b} = s.image (λ a, f a b) := ext $ λ x, by simp lemma image₂_singleton_left' : image₂ f {a} t = t.image (f a) := image₂_singleton_left lemma image₂_singleton : image₂ f {a} {b} = {f a b} := by simp lemma image₂_union_left [decidable_eq α] : image₂ f (s ∪ s') t = image₂ f s t ∪ image₂ f s' t := coe_injective $ by { push_cast, exact image2_union_left } lemma image₂_union_right [decidable_eq β] : image₂ f s (t ∪ t') = image₂ f s t ∪ image₂ f s t' := coe_injective $ by { push_cast, exact image2_union_right } lemma image₂_inter_subset_left [decidable_eq α] : image₂ f (s ∩ s') t ⊆ image₂ f s t ∩ image₂ f s' t := coe_subset.1 $ by { push_cast, exact image2_inter_subset_left } lemma image₂_inter_subset_right [decidable_eq β] : image₂ f s (t ∩ t') ⊆ image₂ f s t ∩ image₂ f s t' := coe_subset.1 $ by { push_cast, exact image2_inter_subset_right } lemma image₂_congr (h : ∀ (a ∈ s) (b ∈ t), f a b = f' a b) : image₂ f s t = image₂ f' s t := coe_injective $ by { push_cast, exact image2_congr h } /-- A common special case of `image₂_congr` -/ lemma image₂_congr' (h : ∀ a b, f a b = f' a b) : image₂ f s t = image₂ f' s t := image₂_congr $ λ a _ b _, h a b lemma subset_image₂ {s : set α} {t : set β} (hu : ↑u ⊆ image2 f s t) : ∃ (s' : finset α) (t' : finset β), ↑s' ⊆ s ∧ ↑t' ⊆ t ∧ u ⊆ image₂ f s' t' := begin apply finset.induction_on' u, { exact ⟨∅, ∅, set.empty_subset _, set.empty_subset _, empty_subset _⟩ }, rintro a u ha _ _ ⟨s', t', hs, hs', h⟩, obtain ⟨x, y, hx, hy, ha⟩ := hu ha, haveI := classical.dec_eq α, haveI := classical.dec_eq β, refine ⟨insert x s', insert y t', _⟩, simp_rw [coe_insert, set.insert_subset], exact ⟨⟨hx, hs⟩, ⟨hy, hs'⟩, insert_subset.2 ⟨mem_image₂.2 ⟨x, y, mem_insert_self _ _, mem_insert_self _ _, ha⟩, h.trans $ image₂_subset (subset_insert _ _) $ subset_insert _ _⟩⟩, end variables (s t) lemma card_image₂_singleton_left (hf : injective (f a)) : (image₂ f {a} t).card = t.card := by rw [image₂_singleton_left, card_image_of_injective _ hf] lemma card_image₂_singleton_right (hf : injective (λ a, f a b)) : (image₂ f s {b}).card = s.card := by rw [image₂_singleton_right, card_image_of_injective _ hf] lemma image₂_singleton_inter [decidable_eq β] (t₁ t₂ : finset β) (hf : injective (f a)) : image₂ f {a} (t₁ ∩ t₂) = image₂ f {a} t₁ ∩ image₂ f {a} t₂ := by simp_rw [image₂_singleton_left, image_inter _ _ hf] lemma image₂_inter_singleton [decidable_eq α] (s₁ s₂ : finset α) (hf : injective (λ a, f a b)) : image₂ f (s₁ ∩ s₂) {b} = image₂ f s₁ {b} ∩ image₂ f s₂ {b} := by simp_rw [image₂_singleton_right, image_inter _ _ hf] lemma card_le_card_image₂_left {s : finset α} (hs : s.nonempty) (hf : ∀ a, injective (f a)) : t.card ≤ (image₂ f s t).card := begin obtain ⟨a, ha⟩ := hs, rw ←card_image₂_singleton_left _ (hf a), exact card_le_of_subset (image₂_subset_right $ singleton_subset_iff.2 ha), end lemma card_le_card_image₂_right {t : finset β} (ht : t.nonempty) (hf : ∀ b, injective (λ a, f a b)) : s.card ≤ (image₂ f s t).card := begin obtain ⟨b, hb⟩ := ht, rw ←card_image₂_singleton_right _ (hf b), exact card_le_of_subset (image₂_subset_left $ singleton_subset_iff.2 hb), end variables {s t} lemma bUnion_image_left : s.bUnion (λ a, t.image $ f a) = image₂ f s t := coe_injective $ by { push_cast, exact set.Union_image_left _ } lemma bUnion_image_right : t.bUnion (λ b, s.image $ λ a, f a b) = image₂ f s t := coe_injective $ by { push_cast, exact set.Union_image_right _ } /-! ### Algebraic replacement rules A collection of lemmas to transfer associativity, commutativity, distributivity, ... of operations to the associativity, commutativity, distributivity, ... of `finset.image₂` of those operations. The proof pattern is `image₂_lemma operation_lemma`. For example, `image₂_comm mul_comm` proves that `image₂ (*) f g = image₂ (*) g f` in a `comm_semigroup`. -/ lemma image_image₂ (f : α → β → γ) (g : γ → δ) : (image₂ f s t).image g = image₂ (λ a b, g (f a b)) s t := coe_injective $ by { push_cast, exact image_image2 _ _ } lemma image₂_image_left (f : γ → β → δ) (g : α → γ) : image₂ f (s.image g) t = image₂ (λ a b, f (g a) b) s t := coe_injective $ by { push_cast, exact image2_image_left _ _ } lemma image₂_image_right (f : α → γ → δ) (g : β → γ) : image₂ f s (t.image g) = image₂ (λ a b, f a (g b)) s t := coe_injective $ by { push_cast, exact image2_image_right _ _ } lemma image₂_swap (f : α → β → γ) (s : finset α) (t : finset β) : image₂ f s t = image₂ (λ a b, f b a) t s := coe_injective $ by { push_cast, exact image2_swap _ _ _ } @[simp] lemma image₂_left [decidable_eq α] (h : t.nonempty) : image₂ (λ x y, x) s t = s := coe_injective $ by { push_cast, exact image2_left h } @[simp] lemma image₂_right [decidable_eq β] (h : s.nonempty) : image₂ (λ x y, y) s t = t := coe_injective $ by { push_cast, exact image2_right h } lemma image₂_assoc {γ : Type*} {u : finset γ} {f : δ → γ → ε} {g : α → β → δ} {f' : α → ε' → ε} {g' : β → γ → ε'} (h_assoc : ∀ a b c, f (g a b) c = f' a (g' b c)) : image₂ f (image₂ g s t) u = image₂ f' s (image₂ g' t u) := coe_injective $ by { push_cast, exact image2_assoc h_assoc } lemma image₂_comm {g : β → α → γ} (h_comm : ∀ a b, f a b = g b a) : image₂ f s t = image₂ g t s := (image₂_swap _ _ _).trans $ by simp_rw h_comm lemma image₂_left_comm {γ : Type*} {u : finset γ} {f : α → δ → ε} {g : β → γ → δ} {f' : α → γ → δ'} {g' : β → δ' → ε} (h_left_comm : ∀ a b c, f a (g b c) = g' b (f' a c)) : image₂ f s (image₂ g t u) = image₂ g' t (image₂ f' s u) := coe_injective $ by { push_cast, exact image2_left_comm h_left_comm } lemma image₂_right_comm {γ : Type*} {u : finset γ} {f : δ → γ → ε} {g : α → β → δ} {f' : α → γ → δ'} {g' : δ' → β → ε} (h_right_comm : ∀ a b c, f (g a b) c = g' (f' a c) b) : image₂ f (image₂ g s t) u = image₂ g' (image₂ f' s u) t := coe_injective $ by { push_cast, exact image2_right_comm h_right_comm } lemma image_image₂_distrib {g : γ → δ} {f' : α' → β' → δ} {g₁ : α → α'} {g₂ : β → β'} (h_distrib : ∀ a b, g (f a b) = f' (g₁ a) (g₂ b)) : (image₂ f s t).image g = image₂ f' (s.image g₁) (t.image g₂) := coe_injective $ by { push_cast, exact image_image2_distrib h_distrib } /-- Symmetric of `finset.image₂_image_left_comm`. -/ lemma image_image₂_distrib_left {g : γ → δ} {f' : α' → β → δ} {g' : α → α'} (h_distrib : ∀ a b, g (f a b) = f' (g' a) b) : (image₂ f s t).image g = image₂ f' (s.image g') t := coe_injective $ by { push_cast, exact image_image2_distrib_left h_distrib } /-- Symmetric of `finset.image_image₂_right_comm`. -/ lemma image_image₂_distrib_right {g : γ → δ} {f' : α → β' → δ} {g' : β → β'} (h_distrib : ∀ a b, g (f a b) = f' a (g' b)) : (image₂ f s t).image g = image₂ f' s (t.image g') := coe_injective $ by { push_cast, exact image_image2_distrib_right h_distrib } /-- Symmetric of `finset.image_image₂_distrib_left`. -/ lemma image₂_image_left_comm {f : α' → β → γ} {g : α → α'} {f' : α → β → δ} {g' : δ → γ} (h_left_comm : ∀ a b, f (g a) b = g' (f' a b)) : image₂ f (s.image g) t = (image₂ f' s t).image g' := (image_image₂_distrib_left $ λ a b, (h_left_comm a b).symm).symm /-- Symmetric of `finset.image_image₂_distrib_right`. -/ lemma image_image₂_right_comm {f : α → β' → γ} {g : β → β'} {f' : α → β → δ} {g' : δ → γ} (h_right_comm : ∀ a b, f a (g b) = g' (f' a b)) : image₂ f s (t.image g) = (image₂ f' s t).image g' := (image_image₂_distrib_right $ λ a b, (h_right_comm a b).symm).symm /-- The other direction does not hold because of the `s`-`s` cross terms on the RHS. -/ lemma image₂_distrib_subset_left {γ : Type*} {u : finset γ} {f : α → δ → ε} {g : β → γ → δ} {f₁ : α → β → β'} {f₂ : α → γ → γ'} {g' : β' → γ' → ε} (h_distrib : ∀ a b c, f a (g b c) = g' (f₁ a b) (f₂ a c)) : image₂ f s (image₂ g t u) ⊆ image₂ g' (image₂ f₁ s t) (image₂ f₂ s u) := coe_subset.1 $ by { push_cast, exact set.image2_distrib_subset_left h_distrib } /-- The other direction does not hold because of the `u`-`u` cross terms on the RHS. -/ lemma image₂_distrib_subset_right {γ : Type*} {u : finset γ} {f : δ → γ → ε} {g : α → β → δ} {f₁ : α → γ → α'} {f₂ : β → γ → β'} {g' : α' → β' → ε} (h_distrib : ∀ a b c, f (g a b) c = g' (f₁ a c) (f₂ b c)) : image₂ f (image₂ g s t) u ⊆ image₂ g' (image₂ f₁ s u) (image₂ f₂ t u) := coe_subset.1 $ by { push_cast, exact set.image2_distrib_subset_right h_distrib } lemma image_image₂_antidistrib {g : γ → δ} {f' : β' → α' → δ} {g₁ : β → β'} {g₂ : α → α'} (h_antidistrib : ∀ a b, g (f a b) = f' (g₁ b) (g₂ a)) : (image₂ f s t).image g = image₂ f' (t.image g₁) (s.image g₂) := by { rw image₂_swap f, exact image_image₂_distrib (λ _ _, h_antidistrib _ _) } /-- Symmetric of `finset.image₂_image_left_anticomm`. -/ lemma image_image₂_antidistrib_left {g : γ → δ} {f' : β' → α → δ} {g' : β → β'} (h_antidistrib : ∀ a b, g (f a b) = f' (g' b) a) : (image₂ f s t).image g = image₂ f' (t.image g') s := coe_injective $ by { push_cast, exact image_image2_antidistrib_left h_antidistrib } /-- Symmetric of `finset.image_image₂_right_anticomm`. -/ lemma image_image₂_antidistrib_right {g : γ → δ} {f' : β → α' → δ} {g' : α → α'} (h_antidistrib : ∀ a b, g (f a b) = f' b (g' a)) : (image₂ f s t).image g = image₂ f' t (s.image g') := coe_injective $ by { push_cast, exact image_image2_antidistrib_right h_antidistrib } /-- Symmetric of `finset.image_image₂_antidistrib_left`. -/ lemma image₂_image_left_anticomm {f : α' → β → γ} {g : α → α'} {f' : β → α → δ} {g' : δ → γ} (h_left_anticomm : ∀ a b, f (g a) b = g' (f' b a)) : image₂ f (s.image g) t = (image₂ f' t s).image g' := (image_image₂_antidistrib_left $ λ a b, (h_left_anticomm b a).symm).symm /-- Symmetric of `finset.image_image₂_antidistrib_right`. -/ lemma image_image₂_right_anticomm {f : α → β' → γ} {g : β → β'} {f' : β → α → δ} {g' : δ → γ} (h_right_anticomm : ∀ a b, f a (g b) = g' (f' b a)) : image₂ f s (t.image g) = (image₂ f' t s).image g' := (image_image₂_antidistrib_right $ λ a b, (h_right_anticomm b a).symm).symm end finset
7d6b2bbf2a8b031e503e7177fff2c79bade40aeb
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/algebra/category/Semigroup/basic.lean
2c2fa19ea2784ed4f183fe19a8695e1c9d629249
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
6,347
lean
/- Copyright (c) 2021 Julian Kuelshammer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Julian Kuelshammer -/ import category_theory.concrete_category.bundled_hom import category_theory.concrete_category.reflects_isomorphisms import algebra.pempty_instances /-! # Category instances for has_mul, has_add, semigroup and add_semigroup We introduce the bundled categories: * `Magma` * `AddMagma` * `Semigroup` * `AddSemigroup` along with the relevant forgetful functors between them. This closely follows `algebra.category.Mon.basic`. ## TODO * Limits in these categories * free/forgetful adjunctions -/ universes u v open category_theory /-- The category of magmas and magma morphisms. -/ @[to_additive AddMagma] def Magma : Type (u+1) := bundled has_mul /-- The category of additive magmas and additive magma morphisms. -/ add_decl_doc AddMagma namespace Magma @[to_additive] instance bundled_hom : bundled_hom @mul_hom := ⟨@mul_hom.to_fun, @mul_hom.id, @mul_hom.comp, @mul_hom.coe_inj⟩ attribute [derive [has_coe_to_sort, large_category, concrete_category]] Magma AddMagma /-- Construct a bundled `Magma` from the underlying type and typeclass. -/ @[to_additive] def of (M : Type u) [has_mul M] : Magma := bundled.of M /-- Construct a bundled `Magma` from the underlying type and typeclass. -/ add_decl_doc AddMagma.of @[to_additive] instance : inhabited Magma := ⟨Magma.of pempty⟩ @[to_additive] instance (M : Magma) : has_mul M := M.str @[simp, to_additive] lemma coe_of (R : Type u) [has_mul R] : (Magma.of R : Type u) = R := rfl end Magma /-- The category of semigroups and semigroup morphisms. -/ @[to_additive AddSemigroup] def Semigroup : Type (u+1) := bundled semigroup /-- The category of additive semigroups and semigroup morphisms. -/ add_decl_doc AddSemigroup namespace Semigroup @[to_additive] instance : bundled_hom.parent_projection semigroup.to_has_mul := ⟨⟩ attribute [derive [has_coe_to_sort, large_category, concrete_category]] Semigroup AddSemigroup /-- Construct a bundled `Semigroup` from the underlying type and typeclass. -/ @[to_additive] def of (M : Type u) [semigroup M] : Semigroup := bundled.of M /-- Construct a bundled `AddSemigroup` from the underlying type and typeclass. -/ add_decl_doc AddSemigroup.of @[to_additive] instance : inhabited Semigroup := ⟨Semigroup.of pempty⟩ @[to_additive] instance (M : Semigroup) : semigroup M := M.str @[simp, to_additive] lemma coe_of (R : Type u) [semigroup R] : (Semigroup.of R : Type u) = R := rfl @[to_additive has_forget_to_AddMagma] instance has_forget_to_Magma : has_forget₂ Semigroup Magma := bundled_hom.forget₂ _ _ end Semigroup variables {X Y : Type u} section variables [has_mul X] [has_mul Y] /-- Build an isomorphism in the category `Magma` from a `mul_equiv` between `has_mul`s. -/ @[to_additive add_equiv.to_AddMagma_iso "Build an isomorphism in the category `AddMagma` from an `add_equiv` between `has_add`s.", simps] def mul_equiv.to_Magma_iso (e : X ≃* Y) : Magma.of X ≅ Magma.of Y := { hom := e.to_mul_hom, inv := e.symm.to_mul_hom } end section variables [semigroup X] [semigroup Y] /-- Build an isomorphism in the category `Semigroup` from a `mul_equiv` between `semigroup`s. -/ @[to_additive add_equiv.to_AddSemigroup_iso "Build an isomorphism in the category `AddSemigroup` from an `add_equiv` between `add_semigroup`s.", simps] def mul_equiv.to_Semigroup_iso (e : X ≃* Y) : Semigroup.of X ≅ Semigroup.of Y := { hom := e.to_mul_hom, inv := e.symm.to_mul_hom } end namespace category_theory.iso /-- Build a `mul_equiv` from an isomorphism in the category `Magma`. -/ @[to_additive AddMagma_iso_to_add_equiv "Build an `add_equiv` from an isomorphism in the category `AddMagma`."] def Magma_iso_to_mul_equiv {X Y : Magma} (i : X ≅ Y) : X ≃* Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := begin rw function.left_inverse, simp end, right_inv := begin rw function.right_inverse, rw function.left_inverse, simp end, map_mul' := by simp } /-- Build a `mul_equiv` from an isomorphism in the category `Semigroup`. -/ @[to_additive "Build an `add_equiv` from an isomorphism in the category `AddSemigroup`."] def Semigroup_iso_to_mul_equiv {X Y : Semigroup} (i : X ≅ Y) : X ≃* Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := begin rw function.left_inverse, simp end, right_inv := begin rw function.right_inverse, rw function.left_inverse, simp end, map_mul' := by simp } end category_theory.iso /-- multiplicative equivalences between `has_mul`s are the same as (isomorphic to) isomorphisms in `Magma` -/ @[to_additive add_equiv_iso_AddMagma_iso "additive equivalences between `has_add`s are the same as (isomorphic to) isomorphisms in `AddMagma`"] def mul_equiv_iso_Magma_iso {X Y : Type u} [has_mul X] [has_mul Y] : (X ≃* Y) ≅ (Magma.of X ≅ Magma.of Y) := { hom := λ e, e.to_Magma_iso, inv := λ i, i.Magma_iso_to_mul_equiv } /-- multiplicative equivalences between `semigroup`s are the same as (isomorphic to) isomorphisms in `Semigroup` -/ @[to_additive add_equiv_iso_AddSemigroup_iso "additive equivalences between `add_semigroup`s are the same as (isomorphic to) isomorphisms in `AddSemigroup`"] def mul_equiv_iso_Semigroup_iso {X Y : Type u} [semigroup X] [semigroup Y] : (X ≃* Y) ≅ (Semigroup.of X ≅ Semigroup.of Y) := { hom := λ e, e.to_Semigroup_iso, inv := λ i, i.Semigroup_iso_to_mul_equiv } @[to_additive] instance Magma.forget_reflects_isos : reflects_isomorphisms (forget Magma.{u}) := { reflects := λ X Y f _, begin resetI, let i := as_iso ((forget Magma).map f), let e : X ≃* Y := { ..f, ..i.to_equiv }, exact ⟨(is_iso.of_iso e.to_Magma_iso).1⟩, end } @[to_additive] instance Semigroup.forget_reflects_isos : reflects_isomorphisms (forget Semigroup.{u}) := { reflects := λ X Y f _, begin resetI, let i := as_iso ((forget Semigroup).map f), let e : X ≃* Y := { ..f, ..i.to_equiv }, exact ⟨(is_iso.of_iso e.to_Semigroup_iso).1⟩, end } /-! Once we've shown that the forgetful functors to type reflect isomorphisms, we automatically obtain that the `forget₂` functors between our concrete categories reflect isomorphisms. -/ example : reflects_isomorphisms (forget₂ Semigroup Magma) := by apply_instance
97f2de8aab97b060386564751511259aa4a97215
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/category_theory/abelian/exact.lean
894721d963c0ad78eebf33d747d730c84a500dfc
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
6,492
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.abelian.basic import algebra.homology.exact import tactic.tfae /-! # Exact sequences in abelian categories In an abelian category, we get several interesting results related to exactness which are not true in more general settings. ## Main results * `(f, g)` is exact if and only if `f ≫ g = 0` and `kernel.ι g ≫ cokernel.π f = 0`. This characterisation tends to be less cumbersome to work with than the original definition involving the comparison map `image f ⟶ kernel g`. * If `(f, g)` is exact, then `image.ι f` has the universal property of the kernel of `g`. * `f` is a monomorphism iff `kernel.ι f = 0` iff `exact 0 f`, and `f` is an epimorphism iff `cokernel.π = 0` iff `exact f 0`. -/ universes v u noncomputable theory open category_theory open category_theory.limits open category_theory.preadditive variables {C : Type u} [category.{v} C] [abelian C] namespace category_theory.abelian variables {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) local attribute [instance] has_equalizers_of_has_kernels /-- In an abelian category, a pair of morphisms `f : X ⟶ Y`, `g : Y ⟶ Z` is exact iff `image_subobject f = kernel_subobject g`. -/ theorem exact_iff_image_eq_kernel : exact f g ↔ image_subobject f = kernel_subobject g := begin split, { introI h, fapply subobject.eq_of_comm, { suffices : is_iso (image_to_kernel _ _ h.w), { exactI as_iso (image_to_kernel _ _ h.w), }, exact is_iso_of_mono_of_epi _, }, { simp, }, }, { apply exact_of_image_eq_kernel, }, end theorem exact_iff : exact f g ↔ f ≫ g = 0 ∧ kernel.ι g ≫ cokernel.π f = 0 := begin split, { introI h, exact ⟨h.1, kernel_comp_cokernel f g⟩ }, { refine λ h, ⟨h.1, _⟩, suffices hl : is_limit (kernel_fork.of_ι (image_subobject f).arrow (image_subobject_arrow_comp_eq_zero h.1)), { have : image_to_kernel f g h.1 = (is_limit.cone_point_unique_up_to_iso hl (limit.is_limit _)).hom ≫ (kernel_subobject_iso _).inv, { ext, simp }, rw this, apply_instance, }, refine is_limit.of_ι _ _ _ _ _, { refine λ W u hu, kernel.lift (cokernel.π f) u _ ≫ (image_iso_image f).hom ≫ (image_subobject_iso _).inv, rw [←kernel.lift_ι g u hu, category.assoc, h.2, has_zero_morphisms.comp_zero] }, { tidy }, { intros, rw [←cancel_mono (image_subobject f).arrow, w], simp, } } end theorem exact_iff' {cg : kernel_fork g} (hg : is_limit cg) {cf : cokernel_cofork f} (hf : is_colimit cf) : exact f g ↔ f ≫ g = 0 ∧ cg.ι ≫ cf.π = 0 := begin split, { introI h, exact ⟨h.1, fork_ι_comp_cofork_π f g cg cf⟩ }, { rw exact_iff, refine λ h, ⟨h.1, _⟩, apply zero_of_epi_comp (is_limit.cone_point_unique_up_to_iso hg (limit.is_limit _)).hom, apply zero_of_comp_mono (is_colimit.cocone_point_unique_up_to_iso (colimit.is_colimit _) hf).hom, simp [h.2] } end theorem exact_tfae : tfae [exact f g, f ≫ g = 0 ∧ kernel.ι g ≫ cokernel.π f = 0, image_subobject f = kernel_subobject g] := begin tfae_have : 1 ↔ 2, { apply exact_iff }, tfae_have : 1 ↔ 3, { apply exact_iff_image_eq_kernel }, tfae_finish end /-- If `(f, g)` is exact, then `images.image.ι f` is a kernel of `g`. -/ def is_limit_image [h : exact f g] : is_limit (kernel_fork.of_ι (images.image.ι f) (images.image_ι_comp_eq_zero h.1) : kernel_fork g) := begin rw exact_iff at h, refine is_limit.of_ι _ _ _ _ _, { refine λ W u hu, kernel.lift (cokernel.π f) u _, rw [←kernel.lift_ι g u hu, category.assoc, h.2, has_zero_morphisms.comp_zero] }, tidy end /-- If `(f, g)` is exact, then `image.ι f` is a kernel of `g`. -/ def is_limit_image' [h : exact f g] : is_limit (kernel_fork.of_ι (image.ι f) (image_ι_comp_eq_zero h.1)) := is_kernel.iso_kernel _ _ (is_limit_image f g) (image_iso_image f).symm $ is_image.lift_fac _ _ /-- If `(f, g)` is exact, then `coimages.coimage.π g` is a cokernel of `f`. -/ def is_colimit_coimage [h : exact f g] : is_colimit (cokernel_cofork.of_π (coimages.coimage.π g) (coimages.comp_coimage_π_eq_zero h.1) : cokernel_cofork f) := begin rw exact_iff at h, refine is_colimit.of_π _ _ _ _ _, { refine λ W u hu, cokernel.desc (kernel.ι g) u _, rw [←cokernel.π_desc f u hu, ←category.assoc, h.2, has_zero_morphisms.zero_comp] }, tidy end /-- If `(f, g)` is exact, then `factor_thru_image g` is a cokernel of `f`. -/ def is_colimit_image [h : exact f g] : is_colimit (cokernel_cofork.of_π (factor_thru_image g) (comp_factor_thru_image_eq_zero h.1)) := is_cokernel.cokernel_iso _ _ (is_colimit_coimage f g) (coimage_iso_image' g) $ (cancel_mono (image.ι g)).1 $ by simp lemma exact_cokernel : exact f (cokernel.π f) := by { rw exact_iff, tidy } instance [exact f g] : mono (cokernel.desc f g (by simp)) := suffices h : cokernel.desc f g (by simp) = (is_colimit.cocone_point_unique_up_to_iso (colimit.is_colimit _) (is_colimit_image f g)).hom ≫ image.ι g, by { rw h, apply mono_comp }, (cancel_epi (cokernel.π f)).1 $ by simp section variables (Z) lemma tfae_mono : tfae [mono f, kernel.ι f = 0, exact (0 : Z ⟶ X) f] := begin tfae_have : 3 → 2, { introsI, exact kernel_ι_eq_zero_of_exact_zero_left Z }, tfae_have : 1 → 3, { introsI, exact exact_zero_left_of_mono Z }, tfae_have : 2 → 1, { exact mono_of_kernel_ι_eq_zero _ }, tfae_finish end -- Note we've already proved `mono_iff_exact_zero_left : mono f ↔ exact (0 : Z ⟶ X) f` -- in any preadditive category with kernels and images. lemma mono_iff_kernel_ι_eq_zero : mono f ↔ kernel.ι f = 0 := (tfae_mono X f).out 0 1 lemma tfae_epi : tfae [epi f, cokernel.π f = 0, exact f (0 : Y ⟶ Z)] := begin tfae_have : 3 → 2, { rw exact_iff, rintro ⟨-, h⟩, exact zero_of_epi_comp _ h }, tfae_have : 1 → 3, { rw exact_iff, introI, exact ⟨by simp, by simp [cokernel.π_of_epi]⟩ }, tfae_have : 2 → 1, { exact epi_of_cokernel_π_eq_zero _ }, tfae_finish end -- Note we've already proved `epi_iff_exact_zero_right : epi f ↔ exact f (0 : Y ⟶ Z)` -- in any preadditive category with equalizers and images. lemma epi_iff_cokernel_π_eq_zero : epi f ↔ cokernel.π f = 0 := (tfae_epi X f).out 0 1 end end category_theory.abelian
7f0eae2eaf831dd0a93d378c95f61780908409e8
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/priority_test2.lean
d19d508a4573a6e1eb159137a81c0129e34f4d6c
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
714
lean
open nat structure foo [class] := (a : nat) (b : nat) definition i1 [instance] [priority std.priority.default-2] : foo := foo.mk 1 1 example : foo.a = 1 := rfl definition i2 [instance] [priority std.priority.default-1] : foo := foo.mk 2 2 example : foo.a = 2 := rfl definition i3 [instance] : foo := foo.mk 3 3 example : foo.a = 3 := rfl definition i4 [instance] [priority std.priority.default-1] : foo := foo.mk 4 4 example : foo.a = 3 := rfl attribute i4 [instance] [priority std.priority.default+2] example : foo.a = 4 := rfl attribute i1 [instance] [priority std.priority.default+3] example : foo.a = 1 := rfl attribute i2 [instance] [priority std.priority.default+4] example : foo.a = 2 := rfl
059eb1259df5acba99321f7ad00a87a539ae30c6
4727251e0cd73359b15b664c3170e5d754078599
/src/linear_algebra/matrix/reindex.lean
cb94fdbc88ff342ec64c4e9f6536ba443ff25f24
[ "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,455
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, Patrick Massot, Casper Putz, Anne Baanen -/ import linear_algebra.matrix.determinant /-! # Changing the index type of a matrix This file concerns the map `matrix.reindex`, mapping a `m` by `n` matrix to an `m'` by `n'` matrix, as long as `m ≃ m'` and `n ≃ n'`. ## Main definitions * `matrix.reindex_linear_equiv R A`: `matrix.reindex` is an `R`-linear equivalence between `A`-matrices. * `matrix.reindex_alg_equiv R`: `matrix.reindex` is an `R`-algebra equivalence between `R`-matrices. ## Tags matrix, reindex -/ namespace matrix open equiv open_locale matrix variables {l m n o : Type*} {l' m' n' o' : Type*} {m'' n'' : Type*} variables (R A : Type*) section add_comm_monoid variables [semiring R] [add_comm_monoid A] [module R A] /-- The natural map that reindexes a matrix's rows and columns with equivalent types, `matrix.reindex`, is a linear equivalence. -/ def reindex_linear_equiv (eₘ : m ≃ m') (eₙ : n ≃ n') : matrix m n A ≃ₗ[R] matrix m' n' A := { map_add' := λ _ _, rfl, map_smul' := λ _ _, rfl, ..(reindex eₘ eₙ)} @[simp] lemma reindex_linear_equiv_apply (eₘ : m ≃ m') (eₙ : n ≃ n') (M : matrix m n A) : reindex_linear_equiv R A eₘ eₙ M = reindex eₘ eₙ M := rfl @[simp] lemma reindex_linear_equiv_symm (eₘ : m ≃ m') (eₙ : n ≃ n') : (reindex_linear_equiv R A eₘ eₙ).symm = reindex_linear_equiv R A eₘ.symm eₙ.symm := rfl @[simp] lemma reindex_linear_equiv_refl_refl : reindex_linear_equiv R A (equiv.refl m) (equiv.refl n) = linear_equiv.refl R _ := linear_equiv.ext $ λ _, rfl lemma reindex_linear_equiv_trans (e₁ : m ≃ m') (e₂ : n ≃ n') (e₁' : m' ≃ m'') (e₂' : n' ≃ n'') : (reindex_linear_equiv R A e₁ e₂).trans (reindex_linear_equiv R A e₁' e₂') = (reindex_linear_equiv R A (e₁.trans e₁') (e₂.trans e₂') : _ ≃ₗ[R] _) := by { ext, refl } lemma reindex_linear_equiv_comp (e₁ : m ≃ m') (e₂ : n ≃ n') (e₁' : m' ≃ m'') (e₂' : n' ≃ n'') : (reindex_linear_equiv R A e₁' e₂') ∘ (reindex_linear_equiv R A e₁ e₂) = reindex_linear_equiv R A (e₁.trans e₁') (e₂.trans e₂') := by { rw [← reindex_linear_equiv_trans], refl } lemma reindex_linear_equiv_comp_apply (e₁ : m ≃ m') (e₂ : n ≃ n') (e₁' : m' ≃ m'') (e₂' : n' ≃ n'') (M : matrix m n A) : (reindex_linear_equiv R A e₁' e₂') (reindex_linear_equiv R A e₁ e₂ M) = reindex_linear_equiv R A (e₁.trans e₁') (e₂.trans e₂') M := minor_minor _ _ _ _ _ lemma reindex_linear_equiv_one [decidable_eq m] [decidable_eq m'] [has_one A] (e : m ≃ m') : (reindex_linear_equiv R A e e (1 : matrix m m A)) = 1 := minor_one_equiv e.symm end add_comm_monoid section semiring variables [semiring R] [semiring A] [module R A] lemma reindex_linear_equiv_mul [fintype n] [fintype n'] (eₘ : m ≃ m') (eₙ : n ≃ n') (eₒ : o ≃ o') (M : matrix m n A) (N : matrix n o A) : reindex_linear_equiv R A eₘ eₙ M ⬝ reindex_linear_equiv R A eₙ eₒ N = reindex_linear_equiv R A eₘ eₒ (M ⬝ N) := minor_mul_equiv M N _ _ _ lemma mul_reindex_linear_equiv_one [fintype n] [fintype o] [decidable_eq o] (e₁ : o ≃ n) (e₂ : o ≃ n') (M : matrix m n A) : M.mul (reindex_linear_equiv R A e₁ e₂ 1) = reindex_linear_equiv R A (equiv.refl m) (e₁.symm.trans e₂) M := mul_minor_one _ _ _ end semiring section algebra variables [comm_semiring R] [fintype n] [fintype m] [decidable_eq m] [decidable_eq n] /-- For square matrices with coefficients in commutative semirings, the natural map that reindexes a matrix's rows and columns with equivalent types, `matrix.reindex`, is an equivalence of algebras. -/ def reindex_alg_equiv (e : m ≃ n) : matrix m m R ≃ₐ[R] matrix n n R := { to_fun := reindex e e, map_mul' := λ a b, (reindex_linear_equiv_mul R R e e e a b).symm, commutes' := λ r, by simp [algebra_map, algebra.to_ring_hom, minor_smul], ..(reindex_linear_equiv R R e e) } @[simp] lemma reindex_alg_equiv_apply (e : m ≃ n) (M : matrix m m R) : reindex_alg_equiv R e M = reindex e e M := rfl @[simp] lemma reindex_alg_equiv_symm (e : m ≃ n) : (reindex_alg_equiv R e).symm = reindex_alg_equiv R e.symm := rfl @[simp] lemma reindex_alg_equiv_refl : reindex_alg_equiv R (equiv.refl m) = alg_equiv.refl := alg_equiv.ext $ λ _, rfl lemma reindex_alg_equiv_mul (e : m ≃ n) (M : matrix m m R) (N : matrix m m R) : reindex_alg_equiv R e (M ⬝ N) = reindex_alg_equiv R e M ⬝ reindex_alg_equiv R e N := (reindex_alg_equiv R e).map_mul M N end algebra /-- Reindexing both indices along the same equivalence preserves the determinant. For the `simp` version of this lemma, see `det_minor_equiv_self`. -/ lemma det_reindex_linear_equiv_self [comm_ring R] [fintype m] [decidable_eq m] [fintype n] [decidable_eq n] (e : m ≃ n) (M : matrix m m R) : det (reindex_linear_equiv R R e e M) = det M := det_reindex_self e M /-- Reindexing both indices along the same equivalence preserves the determinant. For the `simp` version of this lemma, see `det_minor_equiv_self`. -/ lemma det_reindex_alg_equiv [comm_ring R] [fintype m] [decidable_eq m] [fintype n] [decidable_eq n] (e : m ≃ n) (A : matrix m m R) : det (reindex_alg_equiv R e A) = det A := det_reindex_self e A end matrix
e3d43b53df8529b6f1903fec5d5409c78cb2d957
ba4794a0deca1d2aaa68914cd285d77880907b5c
/src/game/world10/level15.lean
f04caf0a923507a02aaaaaf1f1b07a996a541e9b
[ "Apache-2.0" ]
permissive
ChrisHughes24/natural_number_game
c7c00aa1f6a95004286fd456ed13cf6e113159ce
9d09925424da9f6275e6cfe427c8bcf12bb0944f
refs/heads/master
1,600,715,773,528
1,573,910,462,000
1,573,910,462,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
925
lean
--import mynat.lt -- definition of < import game.world10.level14 -- hide namespace mynat -- hide /- # Inequality world. ## Level 15: introducing `<` To get the remaining collectibles in this world, we need to give a definition of `<`. By default, the definition of `a < b` in Lean, once `≤` is defined, is this: `a < b := a ≤ b ∧ ¬ (b ≤ a)` . But a much more usable definition would be this: `a < b := succ(a) ≤ b` . Let's prove that these two definitions are the same -/ /- Lemma : For all naturals $a$ and $b$, $$a\le b\land\lnot(b\le a)\implies\operatorname{succ}(a)\le b.$$ -/ lemma lt_aux_one (a b : mynat) : a ≤ b ∧ ¬ (b ≤ a) → succ a ≤ b := begin [less_leaky] intro h, cases h with h1 h2, cases h1 with c hc, cases c with d, exfalso, rw add_zero at hc, apply h2, rw hc, refl, use d, rw hc, rw add_succ, rw succ_add, refl, end end mynat -- hide
a17f05923f87d80f2a57cdc7007bc32cce88f207
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/number_theory/primorial.lean
005aeef3847305e323073c17dc06f826e8163472
[ "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
7,369
lean
/- Copyright (c) 2020 Patrick Stevens. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Stevens -/ import tactic.ring_exp import data.nat.parity import data.nat.choose.sum /-! # Primorial This file defines the primorial function (the product of primes less than or equal to some bound), and proves that `primorial n ≤ 4 ^ n`. ## Notations We use the local notation `n#` for the primorial of `n`: that is, the product of the primes less than or equal to `n`. -/ open finset open nat open_locale big_operators nat /-- The primorial `n#` of `n` is the product of the primes less than or equal to `n`. -/ def primorial (n : ℕ) : ℕ := ∏ p in (filter prime (range (n + 1))), p local notation x`#` := primorial x lemma primorial_succ {n : ℕ} (n_big : 1 < n) (r : n % 2 = 1) : (n + 1)# = n# := begin have not_prime : ¬prime (n + 1), { intros is_prime, cases (prime.eq_two_or_odd is_prime) with _ n_even, { linarith, }, { exfalso, rw ←not_even_iff at n_even r, have e : even (n + 1 - n) := (even_sub (lt_add_one n).le).2 (iff_of_false n_even r), simp only [add_tsub_cancel_left, not_even_one] at e, exact e, }, }, apply finset.prod_congr, { rw [@range_succ (n + 1), filter_insert, if_neg not_prime], }, { exact λ _ _, rfl, }, end lemma dvd_choose_of_middling_prime (p : ℕ) (is_prime : prime p) (m : ℕ) (p_big : m + 1 < p) (p_small : p ≤ 2 * m + 1) : p ∣ choose (2 * m + 1) (m + 1) := begin have m_size : m + 1 ≤ 2 * m + 1 := le_of_lt (lt_of_lt_of_le p_big p_small), have expanded : choose (2 * m + 1) (m + 1) * (m + 1)! * (2 * m + 1 - (m + 1))! = (2 * m + 1)! := @choose_mul_factorial_mul_factorial (2 * m + 1) (m + 1) m_size, have p_div_big_fact : p ∣ (2 * m + 1)! := (prime.dvd_factorial is_prime).mpr p_small, rw [←expanded, mul_assoc] at p_div_big_fact, have s : ¬(p ∣ (m + 1)!), { intros p_div_fact, have p_le_succ_m : p ≤ m + 1 := (prime.dvd_factorial is_prime).mp p_div_fact, linarith, }, have t : ¬(p ∣ (2 * m + 1 - (m + 1))!), { intros p_div_fact, have p_small : p ≤ 2 * m + 1 - (m + 1) := (prime.dvd_factorial is_prime).mp p_div_fact, have t : 2 * m + 1 - (m + 1) = m, { norm_num, rw two_mul m, exact add_tsub_cancel_right m m, }, rw t at p_small, obtain p_lt_m | rfl | m_lt_p : _ := lt_trichotomy p m, { have r : m < m + 1 := lt_add_one m, linarith, }, { linarith, }, { linarith, }, }, obtain p_div_choose | p_div_facts : p ∣ choose (2 * m + 1) (m + 1) ∨ p ∣ _! * _! := (prime.dvd_mul is_prime).1 p_div_big_fact, { exact p_div_choose, }, cases (prime.dvd_mul is_prime).1 p_div_facts, cc, cc, end lemma prod_primes_dvd {s : finset ℕ} : ∀ (n : ℕ) (h : ∀ a ∈ s, prime a) (div : ∀ a ∈ s, a ∣ n), (∏ p in s, p) ∣ n := begin apply finset.induction_on s, { simp, }, { intros a s a_not_in_s induct n primes divs, rw finset.prod_insert a_not_in_s, obtain ⟨k, rfl⟩ : a ∣ n, by exact divs a (finset.mem_insert_self a s), have step : ∏ p in s, p ∣ k, { apply induct k, { intros b b_in_s, exact primes b (finset.mem_insert_of_mem b_in_s), }, { intros b b_in_s, have b_div_n, by exact divs b (finset.mem_insert_of_mem b_in_s), have a_prime : prime a, { exact primes a (finset.mem_insert_self a s), }, have b_prime : prime b, { exact primes b (finset.mem_insert_of_mem b_in_s), }, obtain b_div_a | b_div_k : b ∣ a ∨ b ∣ k, exact (prime.dvd_mul b_prime).mp b_div_n, { exfalso, have b_eq_a : b = a, { cases (nat.dvd_prime a_prime).1 b_div_a with b_eq_1 b_eq_a, { subst b_eq_1, exfalso, exact prime.ne_one b_prime rfl, }, { exact b_eq_a } }, subst b_eq_a, exact a_not_in_s b_in_s, }, { exact b_div_k } } }, exact mul_dvd_mul_left a step, } end lemma primorial_le_4_pow : ∀ (n : ℕ), n# ≤ 4 ^ n | 0 := le_refl _ | 1 := le_of_inf_eq rfl | (n + 2) := match nat.mod_two_eq_zero_or_one (n + 1) with | or.inl n_odd := match nat.even_iff.2 n_odd with | ⟨m, twice_m⟩ := let recurse : m + 1 < n + 2 := by linarith in begin calc (n + 2)# = ∏ i in filter prime (range (2 * m + 2)), i : by simpa [←twice_m] ... = ∏ i in filter prime (finset.Ico (m + 2) (2 * m + 2) ∪ range (m + 2)), i : begin rw [range_eq_Ico, finset.union_comm, finset.Ico_union_Ico_eq_Ico], exact bot_le, simp only [add_le_add_iff_right], linarith, end ... = ∏ i in (filter prime (finset.Ico (m + 2) (2 * m + 2)) ∪ (filter prime (range (m + 2)))), i : by rw filter_union ... = (∏ i in filter prime (finset.Ico (m + 2) (2 * m + 2)), i) * (∏ i in filter prime (range (m + 2)), i) : begin apply finset.prod_union, have disj : disjoint (finset.Ico (m + 2) (2 * m + 2)) (range (m + 2)), { simp only [finset.disjoint_left, and_imp, finset.mem_Ico, not_lt, finset.mem_range], intros _ pr _, exact pr, }, exact finset.disjoint_filter_filter disj, end ... ≤ (∏ i in filter prime (finset.Ico (m + 2) (2 * m + 2)), i) * 4 ^ (m + 1) : by exact nat.mul_le_mul_left _ (primorial_le_4_pow (m + 1)) ... ≤ (choose (2 * m + 1) (m + 1)) * 4 ^ (m + 1) : begin have s : ∏ i in filter prime (finset.Ico (m + 2) (2 * m + 2)), i ∣ choose (2 * m + 1) (m + 1), { refine prod_primes_dvd (choose (2 * m + 1) (m + 1)) _ _, { intros a, rw finset.mem_filter, cc, }, { intros a, rw finset.mem_filter, intros pr, rcases pr with ⟨ size, is_prime ⟩, simp only [finset.mem_Ico] at size, rcases size with ⟨ a_big , a_small ⟩, exact dvd_choose_of_middling_prime a is_prime m a_big (nat.lt_succ_iff.mp a_small), }, }, have r : ∏ i in filter prime (finset.Ico (m + 2) (2 * m + 2)), i ≤ choose (2 * m + 1) (m + 1), { refine @nat.le_of_dvd _ _ _ s, exact @choose_pos (2 * m + 1) (m + 1) (by linarith), }, exact nat.mul_le_mul_right _ r, end ... = (choose (2 * m + 1) m) * 4 ^ (m + 1) : by rw choose_symm_half m ... ≤ 4 ^ m * 4 ^ (m + 1) : nat.mul_le_mul_right _ (choose_middle_le_pow m) ... = 4 ^ (2 * m + 1) : by ring_exp ... = 4 ^ (n + 2) : by rw ←twice_m, end end | or.inr n_even := begin obtain one_lt_n | n_le_one : 1 < n + 1 ∨ n + 1 ≤ 1 := lt_or_le 1 (n + 1), { rw primorial_succ (by linarith) n_even, calc (n + 1)# ≤ 4 ^ n.succ : primorial_le_4_pow (n + 1) ... ≤ 4 ^ (n + 2) : pow_le_pow (by norm_num) (nat.le_succ _), }, { have n_zero : n = 0 := eq_bot_iff.2 (succ_le_succ_iff.1 n_le_one), norm_num [n_zero, primorial, range_succ, prod_filter, not_prime_zero, prime_two] }, end end
0f8e1b00ff1396283af72d0ed1627d953f2c4ccd
1dd482be3f611941db7801003235dc84147ec60a
/src/data/complex/exponential.lean
9b27b03bf571fbec578df5e1932134b95aeb4ff9
[ "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
46,918
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import algebra.archimedean import data.nat.choose data.complex.basic import tactic.linarith local attribute [instance, priority 0] classical.prop_decidable local notation `abs'` := _root_.abs open is_absolute_value section open real is_absolute_value finset lemma geo_sum_eq {α : Type*} [field α] {x : α} : ∀ (n : ℕ) (hx1 : x ≠ 1), (range n).sum (λ m, x ^ m) = (1 - x ^ n) / (1 - x) | 0 hx1 := by simp | (n+1) hx1 := have 1 - x ≠ 0 := mt sub_eq_zero_iff_eq.1 hx1.symm, by rw [sum_range_succ, ← mul_div_cancel (x ^ n) this, geo_sum_eq n hx1, ← add_div, _root_.pow_succ]; simp [mul_add, add_mul, mul_comm] lemma geo_sum_inv_eq {α : Type*} [discrete_field α] {x : α} (n : ℕ) (hx1 : x ≠ 1) (hx0 : x ≠ 0) : (range n).sum (λ m, x⁻¹ ^ m) = (x - x * x⁻¹ ^ n) / (x - 1) := have hx1' : x⁻¹ ≠ 1, from λ h, by rw [← @inv_inv' _ _ x, h] at hx1; simpa using hx1, have h1x' : 1 - x⁻¹ ≠ 0, from sub_ne_zero.2 hx1'.symm, have h1x : x - 1 ≠ 0, from sub_ne_zero.2 hx1, by rw [geo_sum_eq _ hx1', div_eq_div_iff h1x' h1x]; simp [mul_add, add_mul, mul_inv_cancel hx0, mul_comm x, mul_left_comm x, mul_assoc, inv_mul_cancel hx0] lemma forall_ge_le_of_forall_le_succ {α : Type*} [preorder α] (f : ℕ → α) {m : ℕ} (h : ∀ n ≥ m, f n.succ ≤ f n) : ∀ {l}, ∀ k ≥ m, k ≤ l → f l ≤ f k := begin assume l k hkm hkl, generalize hp : l - k = p, have : l = k + p := add_comm p k ▸ (nat.sub_eq_iff_eq_add hkl).1 hp, subst this, clear hkl hp, induction p with p ih, { simp }, { exact le_trans (h _ (le_trans hkm (nat.le_add_right _ _))) ih } end variables {α : Type*} {β : Type*} [ring β] [discrete_linear_ordered_field α] [archimedean α] {abv : β → α} [is_absolute_value abv] lemma is_cau_of_decreasing_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a) (hnm : ∀ n ≥ m, f n.succ ≤ f n) : is_cau_seq abs f := λ ε ε0, let ⟨k, hk⟩ := archimedean.arch a ε0 in have h : ∃ l, ∀ n ≥ m, a - add_monoid.smul l ε < f n := ⟨k + k + 1, λ n hnm, lt_of_lt_of_le (show a - add_monoid.smul (k + (k + 1)) ε < -abs (f n), from lt_neg.1 $ lt_of_le_of_lt (ham n hnm) (begin rw [neg_sub, lt_sub_iff_add_lt, add_monoid.add_smul], exact add_lt_add_of_le_of_lt hk (lt_of_le_of_lt hk (lt_add_of_pos_left _ ε0)), end)) (neg_le.2 $ (abs_neg (f n)) ▸ le_abs_self _)⟩, let l := nat.find h in have hl : ∀ (n : ℕ), n ≥ m → f n > a - add_monoid.smul l ε := nat.find_spec h, have hl0 : l ≠ 0 := λ hl0, not_lt_of_ge (ham m (le_refl _)) (lt_of_lt_of_le (by have := hl m (le_refl m); simpa [hl0] using this) (le_abs_self (f m))), begin cases classical.not_forall.1 (nat.find_min h (nat.pred_lt hl0)) with i hi, rw [not_imp, not_lt] at hi, existsi i, assume j hj, have hfij : f j ≤ f i := forall_ge_le_of_forall_le_succ f hnm _ hi.1 hj, rw [abs_of_nonpos (sub_nonpos.2 hfij), neg_sub, sub_lt_iff_lt_add'], exact calc f i ≤ a - add_monoid.smul (nat.pred l) ε : hi.2 ... = a - add_monoid.smul l ε + ε : by conv {to_rhs, rw [← nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hl0), succ_smul', sub_add, add_sub_cancel] } ... < f j + ε : add_lt_add_right (hl j (le_trans hi.1 hj)) _ end lemma is_cau_of_mono_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a) (hnm : ∀ n ≥ m, f n ≤ f n.succ) : is_cau_seq abs f := begin refine @eq.rec_on (ℕ → α) _ (is_cau_seq abs) _ _ (-⟨_, @is_cau_of_decreasing_bounded _ _ _ (λ n, -f n) a m (by simpa) (by simpa)⟩ : cau_seq α abs).2, ext, exact neg_neg _ end lemma is_cau_series_of_abv_le_cau {f : ℕ → β} {g : ℕ → α} (n : ℕ) : (∀ m, n ≤ m → abv (f m) ≤ g m) → is_cau_seq abs (λ n, (range n).sum g) → is_cau_seq abv (λ n, (range n).sum f) := begin assume hm hg ε ε0, cases hg (ε / 2) (div_pos ε0 (by norm_num)) with i hi, existsi max n i, assume j ji, have hi₁ := hi j (le_trans (le_max_right n i) ji), have hi₂ := hi (max n i) (le_max_right n i), have sub_le := abs_sub_le ((range j).sum g) ((range i).sum g) ((range (max n i)).sum g), have := add_lt_add hi₁ hi₂, rw [abs_sub ((range (max n i)).sum g), add_halves ε] at this, refine lt_of_le_of_lt (le_trans (le_trans _ (le_abs_self _)) sub_le) this, generalize hk : j - max n i = k, clear this hi₂ hi₁ hi ε0 ε hg sub_le, rw nat.sub_eq_iff_eq_add ji at hk, rw hk, clear hk ji j, induction k with k' hi, { simp [abv_zero abv] }, { dsimp at *, rw [nat.succ_add, sum_range_succ, sum_range_succ, add_assoc, add_assoc], refine le_trans (abv_add _ _ _) _, exact add_le_add (hm _ (le_add_of_nonneg_of_le (nat.zero_le _) (le_max_left _ _))) hi }, end lemma is_cau_series_of_abv_cau {f : ℕ → β} : is_cau_seq abs (λ m, (range m).sum (λ n, abv (f n))) → is_cau_seq abv (λ m, (range m).sum f) := is_cau_series_of_abv_le_cau 0 (λ n h, le_refl _) lemma is_cau_geo_series {β : Type*} [field β] {abv : β → α} [is_absolute_value abv] (x : β) (hx1 : abv x < 1) : is_cau_seq abv (λ n, (range n).sum (λ m, x ^ m)) := have hx1' : abv x ≠ 1 := λ h, by simpa [h, lt_irrefl] using hx1, is_cau_series_of_abv_cau begin simp only [abv_pow abv, geo_sum_eq _ hx1'] {eta := ff}, refine @is_cau_of_mono_bounded _ _ _ _ ((1 : α) / (1 - abv x)) 0 _ _, { assume n hn, rw abs_of_nonneg , refine div_le_div_of_le_of_pos (sub_le_self _ (abv_pow abv x n ▸ abv_nonneg _ _)) (sub_pos.2 hx1), refine div_nonneg (sub_nonneg.2 _) (sub_pos.2 hx1), clear hn, induction n with n ih, { simp }, { rw [_root_.pow_succ, ← one_mul (1 : α)], refine mul_le_mul (le_of_lt hx1) ih (abv_pow abv x n ▸ abv_nonneg _ _) (by norm_num) } }, { assume n hn, refine div_le_div_of_le_of_pos (sub_le_sub_left _ _) (sub_pos.2 hx1), rw [← one_mul (_ ^ n), _root_.pow_succ], exact mul_le_mul_of_nonneg_right (le_of_lt hx1) (pow_nonneg (abv_nonneg _ _) _) } end lemma is_cau_geo_series_const (a : α) {x : α} (hx1 : abs x < 1) : is_cau_seq abs (λ m, (range m).sum (λ n, a * x ^ n)) := have is_cau_seq abs (λ m, a * (range m).sum (λ n, x ^ n)) := (cau_seq.const abs a * ⟨_, is_cau_geo_series x hx1⟩).2, by simpa only [mul_sum] lemma series_ratio_test {f : ℕ → β} (n : ℕ) (r : α) (hr0 : 0 ≤ r) (hr1 : r < 1) (h : ∀ m, n ≤ m → abv (f m.succ) ≤ r * abv (f m)) : is_cau_seq abv (λ m, (range m).sum f) := have har1 : abs r < 1, by rwa abs_of_nonneg hr0, begin refine is_cau_series_of_abv_le_cau n.succ _ (is_cau_geo_series_const (abv (f n.succ) * r⁻¹ ^ n.succ) har1), assume m hmn, cases classical.em (r = 0) with r_zero r_ne_zero, { have m_pos := lt_of_lt_of_le (nat.succ_pos n) hmn, have := h m.pred (nat.le_of_succ_le_succ (by rwa [nat.succ_pred_eq_of_pos m_pos])), simpa [r_zero, nat.succ_pred_eq_of_pos m_pos, pow_succ] }, generalize hk : m - n.succ = k, have r_pos : 0 < r := lt_of_le_of_ne hr0 (ne.symm r_ne_zero), replace hk : m = k + n.succ := (nat.sub_eq_iff_eq_add hmn).1 hk, induction k with k ih generalizing m n, { rw [hk, zero_add, mul_right_comm, ← pow_inv _ _ r_ne_zero, ← div_eq_mul_inv, mul_div_cancel], exact (ne_of_lt (pow_pos r_pos _)).symm }, { have kn : k + n.succ ≥ n.succ, by rw ← zero_add n.succ; exact add_le_add (zero_le _) (by simp), rw [hk, nat.succ_add, pow_succ' r, ← mul_assoc], exact le_trans (by rw mul_comm; exact h _ (nat.le_of_succ_le kn)) (mul_le_mul_of_nonneg_right (ih (k + n.succ) n h kn rfl) hr0) } end lemma sum_range_diag_flip {α : Type*} [add_comm_monoid α] (n : ℕ) (f : ℕ → ℕ → α) : (range n).sum (λ m, (range (m + 1)).sum (λ k, f k (m - k))) = (range n).sum (λ m, (range (n - m)).sum (f m)) := have h₁ : ((range n).sigma (range ∘ nat.succ)).sum (λ (a : Σ m, ℕ), f (a.2) (a.1 - a.2)) = (range n).sum (λ m, (range (m + 1)).sum (λ k, f k (m - k))) := sum_sigma, have h₂ : ((range n).sigma (λ m, range (n - m))).sum (λ a : Σ (m : ℕ), ℕ, f (a.1) (a.2)) = (range n).sum (λ m, sum (range (n - m)) (f m)) := sum_sigma, h₁ ▸ h₂ ▸ sum_bij (λ a _, ⟨a.2, a.1 - a.2⟩) (λ a ha, have h₁ : a.1 < n := mem_range.1 (mem_sigma.1 ha).1, have h₂ : a.2 < nat.succ a.1 := mem_range.1 (mem_sigma.1 ha).2, mem_sigma.2 ⟨mem_range.2 (lt_of_lt_of_le h₂ h₁), mem_range.2 ((nat.sub_lt_sub_right_iff (nat.le_of_lt_succ h₂)).2 h₁)⟩) (λ _ _, rfl) (λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h, have ha : a₁ < n ∧ a₂ ≤ a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 ha).2)⟩, have hb : b₁ < n ∧ b₂ ≤ b₁ := ⟨mem_range.1 (mem_sigma.1 hb).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 hb).2)⟩, have h : a₂ = b₂ ∧ _ := sigma.mk.inj h, have h' : a₁ = b₁ - b₂ + a₂ := (nat.sub_eq_iff_eq_add ha.2).1 (eq_of_heq h.2), sigma.mk.inj_iff.2 ⟨nat.sub_add_cancel hb.2 ▸ h'.symm ▸ h.1 ▸ rfl, (heq_of_eq h.1)⟩) (λ ⟨a₁, a₂⟩ ha, have ha : a₁ < n ∧ a₂ < n - a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, (mem_range.1 (mem_sigma.1 ha).2)⟩, ⟨⟨a₂ + a₁, a₁⟩, ⟨mem_sigma.2 ⟨mem_range.2 (nat.lt_sub_right_iff_add_lt.1 ha.2), mem_range.2 (nat.lt_succ_of_le (nat.le_add_left _ _))⟩, sigma.mk.inj_iff.2 ⟨rfl, heq_of_eq (nat.add_sub_cancel _ _).symm⟩⟩⟩) lemma abv_sum_le_sum_abv {γ : Type*} (f : γ → β) (s : finset γ) : abv (s.sum f) ≤ s.sum (abv ∘ f) := by haveI := classical.dec_eq γ; exact finset.induction_on s (by simp [abv_zero abv]) (λ a s has ih, by rw [sum_insert has, sum_insert has]; exact le_trans (abv_add abv _ _) (add_le_add_left ih _)) lemma sum_range_sub_sum_range {α : Type*} [add_comm_group α] {f : ℕ → α} {n m : ℕ} (hnm : n ≤ m) : (range m).sum f - (range n).sum f = ((range m).filter (λ k, n ≤ k)).sum f := begin rw [← sum_sdiff (@filter_subset _ (λ k, n ≤ k) _ (range m)), sub_eq_iff_eq_add, ← eq_sub_iff_add_eq, add_sub_cancel'], refine finset.sum_congr (finset.ext.2 $ λ a, ⟨λ h, by simp at *; finish, λ h, have ham : a < m := lt_of_lt_of_le (mem_range.1 h) hnm, by simp * at *⟩) (λ _ _, rfl), end lemma cauchy_product {a b : ℕ → β} (ha : is_cau_seq abs (λ m, (range m).sum (λ n, abv (a n)))) (hb : is_cau_seq abv (λ m, (range m).sum b)) (ε : α) (ε0 : 0 < ε) : ∃ i : ℕ, ∀ j ≥ i, abv ((range j).sum a * (range j).sum b - (range j).sum (λ n, (range (n + 1)).sum (λ m, a m * b (n - m)))) < ε := let ⟨Q, hQ⟩ := cau_seq.bounded ⟨_, hb⟩ in let ⟨P, hP⟩ := cau_seq.bounded ⟨_, ha⟩ in have hP0 : 0 < P, from lt_of_le_of_lt (abs_nonneg _) (hP 0), have hPε0 : 0 < ε / (2 * P), from div_pos ε0 (mul_pos (show (2 : α) > 0, from by norm_num) hP0), let ⟨N, hN⟩ := cau_seq.cauchy₂ ⟨_, hb⟩ hPε0 in have hQε0 : 0 < ε / (4 * Q), from div_pos ε0 (mul_pos (show (0 : α) < 4, by norm_num) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))), let ⟨M, hM⟩ := cau_seq.cauchy₂ ⟨_, ha⟩ hQε0 in ⟨2 * (max N M + 1), λ K hK, have h₁ : sum (range K) (λ m, (range (m + 1)).sum (λ k, a k * b (m - k))) = sum (range K) (λ m, sum (range (K - m)) (λ n, a m * b n)), by simpa using sum_range_diag_flip K (λ m n, a m * b n), have h₂ : (λ i, (range (K - i)).sum (λ k, a i * b k)) = (λ i, a i * (range (K - i)).sum b), by simp [finset.mul_sum], have h₃ : (range K).sum (λ i, a i * (range (K - i)).sum b) = (range K).sum (λ i, a i * ((range (K - i)).sum b - (range K).sum b)) + (range K).sum (λ i, a i * (range K).sum b), by rw ← sum_add_distrib; simp [(mul_add _ _ _).symm], have two_mul_two : (4 : α) = 2 * 2, by norm_num, have hQ0 : Q ≠ 0, from λ h, by simpa [h, lt_irrefl] using hQε0, have h2Q0 : 2 * Q ≠ 0, from mul_ne_zero two_ne_zero hQ0, have hε : ε / (2 * P) * P + ε / (4 * Q) * (2 * Q) = ε, by rw [← div_div_eq_div_mul, div_mul_cancel _ (ne.symm (ne_of_lt hP0)), two_mul_two, mul_assoc, ← div_div_eq_div_mul, div_mul_cancel _ h2Q0, add_halves], have hNMK : max N M + 1 < K, from lt_of_lt_of_le (by rw two_mul; exact lt_add_of_pos_left _ (nat.succ_pos _)) hK, have hKN : N < K, from calc N ≤ max N M : le_max_left _ _ ... < max N M + 1 : nat.lt_succ_self _ ... < K : hNMK, have hsumlesum : (range (max N M + 1)).sum (λ i, abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) ≤ (range (max N M + 1)).sum (λ i, abv (a i) * (ε / (2 * P))), from sum_le_sum (λ m hmJ, mul_le_mul_of_nonneg_left (le_of_lt (hN (K - m) K (nat.le_sub_left_of_add_le (le_trans (by rw two_mul; exact add_le_add (le_of_lt (mem_range.1 hmJ)) (le_trans (le_max_left _ _) (le_of_lt (lt_add_one _)))) hK)) (le_of_lt hKN))) (abv_nonneg abv _)), have hsumltP : sum (range (max N M + 1)) (λ n, abv (a n)) < P := calc sum (range (max N M + 1)) (λ n, abv (a n)) = abs (sum (range (max N M + 1)) (λ n, abv (a n))) : eq.symm (abs_of_nonneg (zero_le_sum (λ x h, abv_nonneg abv (a x)))) ... < P : hP (max N M + 1), begin rw [h₁, h₂, h₃, sum_mul, ← sub_sub, sub_right_comm, sub_self, zero_sub, abv_neg abv], refine lt_of_le_of_lt (abv_sum_le_sum_abv _ _) _, suffices : (range (max N M + 1)).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) + ((range K).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) -(range (max N M + 1)).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b))) < ε / (2 * P) * P + ε / (4 * Q) * (2 * Q), { rw hε at this, simpa [abv_mul abv] }, refine add_lt_add (lt_of_le_of_lt hsumlesum (by rw [← sum_mul, mul_comm]; exact (mul_lt_mul_left hPε0).mpr hsumltP)) _, rw sum_range_sub_sum_range (le_of_lt hNMK), exact calc sum ((range K).filter (λ k, max N M + 1 ≤ k)) (λ i, abv (a i) * abv (sum (range (K - i)) b - sum (range K) b)) ≤ sum ((range K).filter (λ k, max N M + 1 ≤ k)) (λ i, abv (a i) * (2 * Q)) : sum_le_sum (λ n hn, begin refine mul_le_mul_of_nonneg_left _ (abv_nonneg _ _), rw sub_eq_add_neg, refine le_trans (abv_add _ _ _) _, rw [two_mul, abv_neg abv], exact add_le_add (le_of_lt (hQ _)) (le_of_lt (hQ _)), end) ... < ε / (4 * Q) * (2 * Q) : by rw [← sum_mul, ← sum_range_sub_sum_range (le_of_lt hNMK)]; refine (mul_lt_mul_right $ by rw two_mul; exact add_pos (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0)) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))).2 (lt_of_le_of_lt (le_abs_self _) (hM _ _ (le_trans (nat.le_succ_of_le (le_max_right _ _)) (le_of_lt hNMK)) (nat.le_succ_of_le (le_max_right _ _)))) end⟩ end open finset open cau_seq namespace complex lemma is_cau_abs_exp (z : ℂ) : is_cau_seq _root_.abs (λ n, (range n).sum (λ m, abs (z ^ m / nat.fact m))) := let ⟨n, hn⟩ := exists_nat_gt (abs z) in have hn0 : (0 : ℝ) < n, from lt_of_le_of_lt (abs_nonneg _) hn, series_ratio_test n (complex.abs z / n) (div_nonneg_of_nonneg_of_pos (complex.abs_nonneg _) hn0) (by rwa [div_lt_iff hn0, one_mul]) (λ m hm, by rw [abs_abs, abs_abs, nat.fact_succ, pow_succ, mul_comm m.succ, nat.cast_mul, ← div_div_eq_div_mul, mul_div_assoc, mul_div_right_comm, abs_mul, abs_div, abs_cast_nat]; exact mul_le_mul_of_nonneg_right (div_le_div_of_le_left (abs_nonneg _) (nat.cast_pos.2 (nat.succ_pos _)) hn0 (nat.cast_le.2 (le_trans hm (nat.le_succ _)))) (abs_nonneg _)) noncomputable theory lemma is_cau_exp (z : ℂ) : is_cau_seq abs (λ n, (range n).sum (λ m, z ^ m / nat.fact m)) := is_cau_series_of_abv_cau (is_cau_abs_exp z) def exp' (z : ℂ) : cau_seq ℂ complex.abs := ⟨λ n, (range n).sum (λ m, z ^ m / nat.fact m), is_cau_exp z⟩ def exp (z : ℂ) : ℂ := lim (exp' z) def sin (z : ℂ) : ℂ := ((exp (-z * I) - exp (z * I)) * I) / 2 def cos (z : ℂ) : ℂ := (exp (z * I) + exp (-z * I)) / 2 def tan (z : ℂ) : ℂ := sin z / cos z def sinh (z : ℂ) : ℂ := (exp z - exp (-z)) / 2 def cosh (z : ℂ) : ℂ := (exp z + exp (-z)) / 2 def tanh (z : ℂ) : ℂ := sinh z / cosh z end complex namespace real open complex def exp (x : ℝ) : ℝ := (exp x).re def sin (x : ℝ) : ℝ := (sin x).re def cos (x : ℝ) : ℝ := (cos x).re def tan (x : ℝ) : ℝ := (tan x).re def sinh (x : ℝ) : ℝ := (sinh x).re def cosh (x : ℝ) : ℝ := (cosh x).re def tanh (x : ℝ) : ℝ := (tanh x).re end real namespace complex variables (x y : ℂ) @[simp] lemma exp_zero : exp 0 = 1 := lim_eq_of_equiv_const $ λ ε ε0, ⟨1, λ j hj, begin convert ε0, cases j, { exact absurd hj (not_le_of_gt zero_lt_one) }, { dsimp [exp'], induction j with j ih, { dsimp [exp']; simp }, { rw ← ih dec_trivial, simp only [sum_range_succ, pow_succ], simp } } end⟩ lemma exp_add : exp (x + y) = exp x * exp y := show lim (⟨_, is_cau_exp (x + y)⟩ : cau_seq ℂ abs) = lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp x⟩) * lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp y⟩), from have hj : ∀ j : ℕ, (range j).sum (λ m, (x + y) ^ m / m.fact) = (range j).sum (λ i, (range (i + 1)).sum (λ k, x ^ k / k.fact * (y ^ (i - k) / (i - k).fact))), from assume j, finset.sum_congr rfl (λ m hm, begin rw [add_pow, div_eq_mul_inv, sum_mul], refine finset.sum_congr rfl (λ i hi, _), have h₁ : (choose m i : ℂ) ≠ 0 := nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 (choose_pos (nat.le_of_lt_succ (mem_range.1 hi)))), have h₂ := choose_mul_fact_mul_fact (nat.le_of_lt_succ $ finset.mem_range.1 hi), rw [← h₂, nat.cast_mul, nat.cast_mul, mul_inv', mul_inv'], simp only [mul_left_comm (choose m i : ℂ), mul_assoc, mul_left_comm (choose m i : ℂ)⁻¹, mul_comm (choose m i : ℂ)], rw inv_mul_cancel h₁, simp [div_eq_mul_inv, mul_comm, mul_assoc, mul_left_comm] end), by rw lim_mul_lim; exact eq.symm (lim_eq_lim_of_equiv (by dsimp; simp only [hj]; exact cauchy_product (is_cau_abs_exp x) (is_cau_exp y))) lemma exp_ne_zero : exp x ≠ 0 := λ h, @zero_ne_one ℂ _ $ by rw [← exp_zero, ← add_neg_self x, exp_add, h]; simp lemma exp_neg : exp (-x) = (exp x)⁻¹ := by rw [← domain.mul_left_inj (exp_ne_zero x), ← exp_add]; simp [mul_inv_cancel (exp_ne_zero x)] lemma exp_sub : exp (x - y) = exp x / exp y := by simp [exp_add, exp_neg, div_eq_mul_inv] @[simp] lemma exp_conj : exp (conj x) = conj (exp x) := begin dsimp [exp], rw [← lim_conj], refine congr_arg lim (cau_seq.ext (λ _, _)), dsimp [exp', function.comp, cau_seq_conj], rw ← sum_hom conj, refine sum_congr rfl (λ n hn, _), rw [conj_div, conj_pow, ← of_real_nat_cast, conj_of_real] end @[simp] lemma exp_of_real_im (x : ℝ) : (exp x).im = 0 := let ⟨r, hr⟩ := (eq_conj_iff_real (exp x)).1 (by rw [← exp_conj, conj_of_real]) in by rw [hr, of_real_im] lemma exp_of_real_re (x : ℝ) : (exp x).re = real.exp x := rfl @[simp] lemma of_real_exp_of_real_re (x : ℝ) : ((exp x).re : ℂ) = exp x := complex.ext (by simp) (by simp) @[simp] lemma of_real_exp (x : ℝ) : (real.exp x : ℂ) = exp x := complex.ext (by simp [real.exp]) (by simp [real.exp]) @[simp] lemma sin_zero : sin 0 = 0 := by simp [sin] @[simp] lemma sin_neg : sin (-x) = -sin x := by simp [sin, exp_neg, (neg_div _ _).symm, add_mul] lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y := begin rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), ← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _)], simp only [mul_add, add_mul, exp_add, div_mul_div, div_add_div_same, mul_assoc, (div_div_eq_div_mul _ _ _).symm, mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _), sin, cos], simp [mul_add, add_mul, exp_add], ring end @[simp] lemma cos_zero : cos 0 = 1 := by simp [cos] @[simp] lemma cos_neg : cos (-x) = cos x := by simp [cos, exp_neg] lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y := begin rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), ← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _)], simp only [mul_add, add_mul, mul_sub, sub_mul, exp_add, div_mul_div, div_add_div_same, mul_assoc, (div_div_eq_div_mul _ _ _).symm, mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _), sin, cos], apply complex.ext; simp [mul_add, add_mul, exp_add]; ring end lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y := by simp [sin_add, sin_neg, cos_neg] lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y := by simp [cos_add, sin_neg, cos_neg] lemma sin_conj : sin (conj x) = conj (sin x) := begin rw [sin, ← conj_neg_I, ← conj_mul, ← conj_neg, ← conj_mul, exp_conj, exp_conj, ← conj_sub, sin, conj_div, conj_two, ← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _), mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _)], apply complex.ext; simp [sin, neg_add], end @[simp] lemma sin_of_real_im (x : ℝ) : (sin x).im = 0 := let ⟨r, hr⟩ := (eq_conj_iff_real (sin x)).1 (by rw [← sin_conj, conj_of_real]) in by rw [hr, of_real_im] lemma sin_of_real_re (x : ℝ) : (sin x).re = real.sin x := rfl @[simp] lemma of_real_sin_of_real_re (x : ℝ) : ((sin x).re : ℂ) = sin x := by apply complex.ext; simp @[simp] lemma of_real_sin (x : ℝ) : (real.sin x : ℂ) = sin x := by simp [real.sin] lemma cos_conj : cos (conj x) = conj (cos x) := begin rw [cos, ← conj_neg_I, ← conj_mul, ← conj_neg, ← conj_mul, exp_conj, exp_conj, cos, conj_div, conj_two, ← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _), mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _)], apply complex.ext; simp end @[simp] lemma cos_of_real_im (x : ℝ) : (cos x).im = 0 := let ⟨r, hr⟩ := (eq_conj_iff_real (cos x)).1 (by rw [← cos_conj, conj_of_real]) in by rw [hr, of_real_im] lemma cos_of_real_re (x : ℝ) : (cos x).re = real.cos x := rfl @[simp] lemma of_real_cos_of_real_re (x : ℝ) : ((cos x).re : ℂ) = cos x := by apply complex.ext; simp @[simp] lemma of_real_cos (x : ℝ) : (real.cos x : ℂ) = cos x := by simp [real.cos, -cos_of_real_re] @[simp] lemma tan_zero : tan 0 = 0 := by simp [tan] lemma tan_eq_sin_div_cos : tan x = sin x / cos x := rfl @[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div] lemma tan_conj : tan (conj x) = conj (tan x) := by rw [tan, sin_conj, cos_conj, ← conj_div, tan] @[simp] lemma tan_of_real_im (x : ℝ) : (tan x).im = 0 := let ⟨r, hr⟩ := (eq_conj_iff_real (tan x)).1 (by rw [← tan_conj, conj_of_real]) in by rw [hr, of_real_im] lemma tan_of_real_re (x : ℝ) : (tan x).re = real.tan x := rfl @[simp] lemma of_real_tan_of_real_re (x : ℝ) : ((tan x).re : ℂ) = tan x := by apply complex.ext; simp @[simp] lemma of_real_tan (x : ℝ) : (real.tan x : ℂ) = tan x := by simp [real.tan, -tan_of_real_re] lemma sin_pow_two_add_cos_pow_two : sin x ^ 2 + cos x ^ 2 = 1 := begin simp only [pow_two, mul_sub, sub_mul, mul_add, add_mul, div_eq_mul_inv, neg_mul_eq_neg_mul_symm, exp_neg, mul_comm (exp _), mul_left_comm (exp _), mul_assoc, mul_left_comm (exp _)⁻¹, inv_mul_cancel (exp_ne_zero _), mul_inv', mul_one, one_mul, sin, cos], apply complex.ext; simp [norm_sq]; ring end lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 := by rw [two_mul, cos_add, ← pow_two, ← pow_two, eq_sub_iff_add_eq.2 (sin_pow_two_add_cos_pow_two x)]; simp [two_mul] lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x := by rw [two_mul, sin_add, two_mul, add_mul, mul_comm] lemma exp_mul_I : exp (x * I) = cos x + sin x * I := by rw [cos, sin, mul_comm (_ / 2) I, ← mul_div_assoc, mul_left_comm I, I_mul_I, ← add_div]; simp lemma exp_add_mul_I : exp (x + y * I) = exp x * (cos y + sin y * I) := by rw [exp_add, exp_mul_I] lemma exp_eq_exp_re_mul_sin_add_cos : exp x = exp x.re * (cos x.im + sin x.im * I) := by rw [← exp_add_mul_I, re_add_im] @[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh] @[simp] lemma sinh_neg : sinh (-x) = -sinh x := by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul] lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y := begin rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), ← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _)], simp only [mul_add, add_mul, exp_add, div_mul_div, div_add_div_same, mul_assoc, (div_div_eq_div_mul _ _ _).symm, mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _), sinh, cosh], simp [mul_add, add_mul, exp_add], ring end @[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh] @[simp] lemma cosh_neg : cosh (-x) = cosh x := by simp [cosh, exp_neg] lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y := begin rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), ← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _)], simp only [mul_add, add_mul, mul_sub, sub_mul, exp_add, div_mul_div, div_add_div_same, mul_assoc, (div_div_eq_div_mul _ _ _).symm, mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _), sinh, cosh], apply complex.ext; simp [mul_add, add_mul, exp_add]; ring end lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y := by simp [sinh_add, sinh_neg, cosh_neg] lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y := by simp [cosh_add, sinh_neg, cosh_neg] lemma sinh_conj : sinh (conj x) = conj (sinh x) := by rw [sinh, ← conj_neg, exp_conj, exp_conj, ← conj_sub, sinh, conj_div, conj_two] @[simp] lemma sinh_of_real_im (x : ℝ) : (sinh x).im = 0 := let ⟨r, hr⟩ := (eq_conj_iff_real (sinh x)).1 (by rw [← sinh_conj, conj_of_real]) in by rw [hr, of_real_im] lemma sinh_of_real_re (x : ℝ) : (sinh x).re = real.sinh x := rfl @[simp] lemma of_real_sinh_of_real_re (x : ℝ) : ((sinh x).re : ℂ) = sinh x := by apply complex.ext; simp @[simp] lemma of_real_sinh (x : ℝ) : (real.sinh x : ℂ) = sinh x := by simp [real.sinh] lemma cosh_conj : cosh (conj x) = conj (cosh x) := by rw [cosh, ← conj_neg, exp_conj, exp_conj, ← conj_add, cosh, conj_div, conj_two] @[simp] lemma cosh_of_real_im (x : ℝ) : (cosh x).im = 0 := let ⟨r, hr⟩ := (eq_conj_iff_real (cosh x)).1 (by rw [← cosh_conj, conj_of_real]) in by rw [hr, of_real_im] lemma cosh_of_real_re (x : ℝ) : (cosh x).re = real.cosh x := rfl @[simp] lemma of_real_cosh_of_real_re (x : ℝ) : ((cosh x).re : ℂ) = cosh x := by apply complex.ext; simp @[simp] lemma of_real_cosh (x : ℝ) : (real.cosh x : ℂ) = cosh x := by simp [real.cosh] lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := rfl @[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh] @[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div] lemma tanh_conj : tanh (conj x) = conj (tanh x) := by rw [tanh, sinh_conj, cosh_conj, ← conj_div, tanh] @[simp] lemma tanh_of_real_im (x : ℝ) : (tanh x).im = 0 := let ⟨r, hr⟩ := (eq_conj_iff_real (tanh x)).1 (by rw [← tanh_conj, conj_of_real]) in by rw [hr, of_real_im] lemma tanh_of_real_re (x : ℝ) : (tanh x).re = real.tanh x := rfl @[simp] lemma of_real_tanh_of_real_re (x : ℝ) : ((tanh x).re : ℂ) = tanh x := by apply complex.ext; simp @[simp] lemma of_real_tanh (x : ℝ) : (real.tanh x : ℂ) = tanh x := by simp [real.tanh] end complex namespace real open complex variables (x y : ℝ) @[simp] lemma exp_zero : exp 0 = 1 := by simp [real.exp] lemma exp_add : exp (x + y) = exp x * exp y := by simp [exp_add, exp] lemma exp_ne_zero : exp x ≠ 0 := λ h, exp_ne_zero x $ by rw [exp, ← of_real_inj] at h; simp * at * lemma exp_neg : exp (-x) = (exp x)⁻¹ := by rw [← of_real_inj, exp, of_real_exp_of_real_re, of_real_neg, exp_neg, of_real_inv, of_real_exp] lemma exp_sub : exp (x - y) = exp x / exp y := by simp [exp_add, exp_neg, div_eq_mul_inv] @[simp] lemma sin_zero : sin 0 = 0 := by simp [sin] @[simp] lemma sin_neg : sin (-x) = -sin x := by simp [sin, exp_neg, (neg_div _ _).symm, add_mul] lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y := by rw [← of_real_inj]; simp [sin, sin_add] @[simp] lemma cos_zero : cos 0 = 1 := by simp [cos] @[simp] lemma cos_neg : cos (-x) = cos x := by simp [cos, exp_neg] lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y := by rw ← of_real_inj; simp [cos, cos_add] lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y := by simp [sin_add, sin_neg, cos_neg] lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y := by simp [cos_add, sin_neg, cos_neg] lemma tan_eq_sin_div_cos : tan x = sin x / cos x := if h : complex.cos x = 0 then by simp [sin, cos, tan, *, complex.tan, div_eq_mul_inv] at * else by rw [sin, cos, tan, complex.tan, ← of_real_inj, div_eq_mul_inv, mul_re]; simp [norm_sq, (div_div_eq_div_mul _ _ _).symm, div_self h]; refl @[simp] lemma tan_zero : tan 0 = 0 := by simp [tan] @[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div] lemma sin_pow_two_add_cos_pow_two : sin x ^ 2 + cos x ^ 2 = 1 := by rw ← of_real_inj; simpa [sin, of_real_pow] using sin_pow_two_add_cos_pow_two x lemma abs_sin_le_one : abs' (sin x) ≤ 1 := not_lt.1 $ λ h, lt_irrefl (1 * 1 : ℝ) (calc 1 * 1 < abs' (sin x) * abs' (sin x) : mul_lt_mul h (le_of_lt h) zero_lt_one (le_trans zero_le_one (le_of_lt h)) ... = sin x ^ 2 : by rw [← _root_.abs_mul, abs_mul_self, pow_two] ... ≤ sin x ^ 2 + cos x ^ 2 : le_add_of_nonneg_right (pow_two_nonneg _) ... = 1 * 1 : by rw [sin_pow_two_add_cos_pow_two, one_mul]) lemma abs_cos_le_one : abs' (cos x) ≤ 1 := not_lt.1 $ λ h, lt_irrefl (1 * 1 : ℝ) (calc 1 * 1 < abs' (cos x) * abs' (cos x) : mul_lt_mul h (le_of_lt h) zero_lt_one (le_trans zero_le_one (le_of_lt h)) ... = cos x ^ 2 : by rw [← _root_.abs_mul, abs_mul_self, pow_two] ... ≤ sin x ^ 2 + cos x ^ 2 : le_add_of_nonneg_left (pow_two_nonneg _) ... = 1 * 1 : by rw [sin_pow_two_add_cos_pow_two, one_mul]) lemma sin_le_one : sin x ≤ 1 := (abs_le.1 (abs_sin_le_one _)).2 lemma cos_le_one : cos x ≤ 1 := (abs_le.1 (abs_cos_le_one _)).2 lemma neg_one_le_sin : -1 ≤ sin x := (abs_le.1 (abs_sin_le_one _)).1 lemma neg_one_le_cos : -1 ≤ cos x := (abs_le.1 (abs_cos_le_one _)).1 lemma sin_pow_two_le_one : sin x ^ 2 ≤ 1 := by rw [pow_two, ← abs_mul_self, _root_.abs_mul]; exact mul_le_one (abs_sin_le_one _) (abs_nonneg _) (abs_sin_le_one _) lemma cos_pow_two_le_one : cos x ^ 2 ≤ 1 := by rw [pow_two, ← abs_mul_self, _root_.abs_mul]; exact mul_le_one (abs_cos_le_one _) (abs_nonneg _) (abs_cos_le_one _) lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 := by rw ← of_real_inj; simp [cos_two_mul, cos, pow_two] lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x := by rw ← of_real_inj; simp [sin_two_mul, sin, pow_two] @[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh] @[simp] lemma sinh_neg : sinh (-x) = -sinh x := by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul] lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y := by rw ← of_real_inj; simp [sinh, sinh_add] @[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh] @[simp] lemma cosh_neg : cosh (-x) = cosh x := by simp [cosh, exp_neg] lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y := by rw ← of_real_inj; simp [cosh, cosh_add] lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y := by simp [sinh_add, sinh_neg, cosh_neg] lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y := by simp [cosh_add, sinh_neg, cosh_neg] lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := if h : complex.cosh x = 0 then by simp [sinh, cosh, tanh, *, complex.tanh, div_eq_mul_inv] at * else by rw [sinh, cosh, tanh, complex.tanh, ← of_real_inj, div_eq_mul_inv, mul_re]; simp [norm_sq, (div_div_eq_div_mul _ _ _).symm, div_self h]; refl @[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh] @[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div] open is_absolute_value /- TODO make this private and prove ∀ x -/ lemma add_one_le_exp_of_nonneg {x : ℝ} (hx : 0 ≤ x) : x + 1 ≤ exp x := calc x + 1 ≤ lim (⟨(λ n : ℕ, ((exp' x) n).re), is_cau_seq_re (exp' x)⟩ : cau_seq ℝ abs') : le_lim (cau_seq.le_of_exists ⟨2, λ j hj, show x + (1 : ℝ) ≤ ((range j).sum (λ m, (x ^ m / m.fact : ℂ))).re, from have h₁ : (((λ m : ℕ, (x ^ m / m.fact : ℂ)) ∘ nat.succ) 0).re = x, by simp, have h₂ : ((x : ℂ) ^ 0 / nat.fact 0).re = 1, by simp, begin rw [← nat.sub_add_cancel hj, sum_range_succ', sum_range_succ', add_re, add_re, h₁, h₂, add_assoc, ← @sum_hom _ _ _ _ _ _ _ complex.re (is_add_group_hom.to_is_add_monoid_hom _)], refine le_add_of_nonneg_of_le (zero_le_sum (λ m hm, _)) (le_refl _), dsimp [-nat.fact_succ], rw [← of_real_pow, ← of_real_nat_cast, ← of_real_div, of_real_re], exact div_nonneg (pow_nonneg hx _) (nat.cast_pos.2 (nat.fact_pos _)), end⟩) ... = exp x : by rw [exp, complex.exp, ← cau_seq_re, lim_re] lemma one_le_exp {x : ℝ} (hx : 0 ≤ x) : 1 ≤ exp x := by linarith using [add_one_le_exp_of_nonneg hx] lemma exp_pos (x : ℝ) : 0 < exp x := (le_total 0 x).elim (lt_of_lt_of_le zero_lt_one ∘ one_le_exp) (λ h, by rw [← neg_neg x, real.exp_neg]; exact inv_pos (lt_of_lt_of_le zero_lt_one (one_le_exp (neg_nonneg.2 h)))) @[simp] lemma abs_exp (x : ℝ) : abs' (exp x) = exp x := abs_of_nonneg (le_of_lt (exp_pos _)) lemma exp_le_exp {x y : ℝ} (h : x ≤ y) : exp x ≤ exp y := by rw [← sub_add_cancel y x, real.exp_add]; exact (le_mul_iff_one_le_left (exp_pos _)).2 (one_le_exp (sub_nonneg.2 h)) lemma exp_lt_exp {x y : ℝ} (h : x < y) : exp x < exp y := by rw [← sub_add_cancel y x, real.exp_add]; exact (lt_mul_iff_one_lt_left (exp_pos _)).2 (lt_of_lt_of_le (by linarith) (add_one_le_exp_of_nonneg (by linarith))) lemma exp_injective : function.injective exp := λ x y h, begin rcases lt_trichotomy x y with h₁ | h₁ | h₁, { exact absurd h (ne_of_lt (exp_lt_exp h₁)) }, { exact h₁ }, { exact absurd h (ne.symm (ne_of_lt (exp_lt_exp h₁))) } end @[simp] lemma exp_eq_one_iff : exp x = 1 ↔ x = 0 := ⟨by rw ← exp_zero; exact λ h, exp_injective h, λ h, by rw [h, exp_zero]⟩ end real namespace complex lemma sum_div_fact_le {α : Type*} [discrete_linear_ordered_field α] (n j : ℕ) (hn : 0 < n) : (sum (filter (λ k, n ≤ k) (range j)) (λ m : ℕ, (1 / m.fact : α))) ≤ n.succ * (n.fact * n)⁻¹ := calc (filter (λ k, n ≤ k) (range j)).sum (λ m : ℕ, (1 / m.fact : α)) = (range (j - n)).sum (λ m, 1 / (m + n).fact) : sum_bij (λ m _, m - n) (λ m hm, mem_range.2 $ (nat.sub_lt_sub_right_iff (by simp at hm; tauto)).2 (by simp at hm; tauto)) (λ m hm, by rw nat.sub_add_cancel; simp at *; tauto) (λ a₁ a₂ ha₁ ha₂ h, by rwa [nat.sub_eq_iff_eq_add, ← nat.sub_add_comm, eq_comm, nat.sub_eq_iff_eq_add, add_right_inj, eq_comm] at h; simp at *; tauto) (λ b hb, ⟨b + n, mem_filter.2 ⟨mem_range.2 $ nat.add_lt_of_lt_sub_right (mem_range.1 hb), nat.le_add_left _ _⟩, by rw nat.add_sub_cancel⟩) ... ≤ (range (j - n)).sum (λ m, (nat.fact n * n.succ ^ m)⁻¹) : begin refine sum_le_sum (assume m n, _), rw [one_div_eq_inv, inv_le_inv], { rw [← nat.cast_pow, ← nat.cast_mul, nat.cast_le, add_comm], exact nat.fact_mul_pow_le_fact }, { exact nat.cast_pos.2 (nat.fact_pos _) }, { exact mul_pos (nat.cast_pos.2 (nat.fact_pos _)) (pow_pos (nat.cast_pos.2 (nat.succ_pos _)) _) }, end ... = (nat.fact n)⁻¹ * (range (j - n)).sum (λ m, n.succ⁻¹ ^ m) : by simp [mul_inv', mul_sum.symm, sum_mul.symm, -nat.fact_succ, mul_comm, inv_pow'] ... = (n.succ - n.succ * n.succ⁻¹ ^ (j - n)) / (n.fact * n) : have h₁ : (n.succ : α) ≠ 1, from @nat.cast_one α _ _ ▸ mt nat.cast_inj.1 (mt nat.succ_inj (nat.pos_iff_ne_zero.1 hn)), have h₂ : (n.succ : α) ≠ 0, from nat.cast_ne_zero.2 (nat.succ_ne_zero _), have h₃ : (n.fact * n : α) ≠ 0, from mul_ne_zero (nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 (nat.fact_pos _))) (nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 hn)), have h₄ : (n.succ - 1 : α) * (n.fact : ℕ) ≠ 0, from mul_ne_zero (sub_ne_zero.2 h₁) (nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 (nat.fact_pos _))), by rw [geo_sum_inv_eq _ h₁ h₂, mul_comm, ← div_eq_mul_inv, div_div_eq_div_mul, div_eq_div_iff h₄ h₃]; simp [mul_add, add_mul, mul_comm, mul_assoc, mul_left_comm] ... ≤ n.succ / (n.fact * n) : begin refine (div_le_div_right (mul_pos _ _)).2 _, exact nat.cast_pos.2 (nat.fact_pos _), exact nat.cast_pos.2 hn, exact sub_le_self _ (mul_nonneg (nat.cast_nonneg _) (pow_nonneg (inv_nonneg.2 (nat.cast_nonneg _)) _)) end lemma exp_bound {x : ℂ} (hx : abs x ≤ 1) {n : ℕ} (hn : 0 < n) : abs (exp x - (range n).sum (λ m, x ^ m / m.fact)) ≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹) := begin rw [← lim_const ((range n).sum _), exp, sub_eq_add_neg, ← lim_neg, lim_add, ← lim_abs], refine lim_le (cau_seq.le_of_exists ⟨n, λ j hj, _⟩), show abs ((range j).sum (λ m, x ^ m / m.fact) - (range n).sum (λ m, x ^ m / m.fact)) ≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹), rw sum_range_sub_sum_range hj, exact calc abs (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (x ^ m / m.fact : ℂ))) = abs (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (x ^ n * (x ^ (m - n) / m.fact) : ℂ))) : congr_arg abs (sum_congr rfl (λ m hm, by rw [← mul_div_assoc, ← pow_add, nat.add_sub_cancel']; simp at hm; tauto)) ... ≤ sum (filter (λ k, n ≤ k) (range j)) (λ m, abs (x ^ n * (_ / m.fact))) : abv_sum_le_sum_abv _ _ ... ≤ sum (filter (λ k, n ≤ k) (range j)) (λ m, abs x ^ n * (1 / m.fact)) : begin refine sum_le_sum (λ m hm, _), rw [abs_mul, abv_pow abs, abs_div, abs_cast_nat], refine mul_le_mul_of_nonneg_left ((div_le_div_right _).2 _) _, exact nat.cast_pos.2 (nat.fact_pos _), rw abv_pow abs, exact (pow_le_one _ (abs_nonneg _) hx), exact pow_nonneg (abs_nonneg _) _ end ... = abs x ^ n * (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (1 / m.fact : ℝ))) : by simp [abs_mul, abv_pow abs, abs_div, mul_sum.symm] ... ≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹) : mul_le_mul_of_nonneg_left (sum_div_fact_le _ _ hn) (pow_nonneg (abs_nonneg _) _) end lemma abs_exp_sub_one_le {x : ℂ} (hx : abs x ≤ 1) : abs (exp x - 1) ≤ 2 * abs x := calc abs (exp x - 1) = abs (exp x - (range 1).sum (λ m, x ^ m / m.fact)) : by simp [sum_range_succ] ... ≤ abs x ^ 1 * ((nat.succ 1) * (nat.fact 1 * (1 : ℕ))⁻¹) : exp_bound hx dec_trivial ... = 2 * abs x : by simp [two_mul, mul_two, mul_add, mul_comm] end complex namespace real open complex finset lemma cos_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (cos x - (1 - x ^ 2 / 2)) ≤ abs' x ^ 4 * (5 / 96) := calc abs' (cos x - (1 - x ^ 2 / 2)) = abs (complex.cos x - (1 - x ^ 2 / 2)) : by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv] ... = abs ((complex.exp (x * I) + complex.exp (-x * I) - (2 - x ^ 2)) / 2) : by simp [complex.cos, sub_div, add_div, neg_div, div_self (@two_ne_zero' ℂ _ _ _)] ... = abs (((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) + ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)))) / 2) : congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin simp only [sum_range_succ], simp [pow_succ], apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring end) ... ≤ abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) / 2) + abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) / 2) : by rw add_div; exact abs_add _ _ ... = (abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) / 2 + abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact))) / 2) : by simp [complex.abs_div] ... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2 + (complex.abs (-x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2) : add_le_add ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc] lemma sin_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (sin x - (x - x ^ 3 / 6)) ≤ abs' x ^ 4 * (5 / 96) := calc abs' (sin x - (x - x ^ 3 / 6)) = abs (complex.sin x - (x - x ^ 3 / 6)) : by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv] ... = abs (((complex.exp (-x * I) - complex.exp (x * I)) * I - (2 * x - x ^ 3 / 3)) / 2) : by simp [complex.sin, sub_div, add_div, neg_div, mul_div_cancel_left _ (@two_ne_zero' ℂ _ _ _), div_div_eq_div_mul, show (3 : ℂ) * 2 = 6, by norm_num] ... = abs ((((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) - (complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) * I) / 2) : congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin simp only [sum_range_succ], simp [pow_succ], apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring end) ... ≤ abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) * I / 2) + abs (-((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) * I) / 2) : by rw [sub_mul, sub_eq_add_neg, add_div]; exact abs_add _ _ ... = (abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) / 2 + abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact))) / 2) : by simp [complex.abs_div, complex.abs_mul] ... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2 + (complex.abs (-x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2) : add_le_add ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc] lemma cos_pos_of_le_one {x : ℝ} (hx : abs' x ≤ 1) : 0 < cos x := calc 0 < (1 - x ^ 2 / 2) - abs' x ^ 4 * (5 / 96) : sub_pos.2 $ lt_sub_iff_add_lt.2 (calc abs' x ^ 4 * (5 / 96) + x ^ 2 / 2 ≤ 1 * (5 / 96) + 1 / 2 : add_le_add (mul_le_mul_of_nonneg_right (pow_le_one _ (abs_nonneg _) hx) (by norm_num)) ((div_le_div_right (by norm_num)).2 (by rw [pow_two, ← abs_mul_self, _root_.abs_mul]; exact mul_le_one hx (abs_nonneg _) hx)) ... < 1 : by norm_num) ... ≤ cos x : sub_le.1 (abs_sub_le_iff.1 (cos_bound hx)).2 lemma sin_pos_of_pos_of_le_one {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 1) : 0 < sin x := calc 0 < x - x ^ 3 / 6 - abs' x ^ 4 * (5 / 96) : sub_pos.2 $ lt_sub_iff_add_lt.2 (calc abs' x ^ 4 * (5 / 96) + x ^ 3 / 6 ≤ x * (5 / 96) + x / 6 : add_le_add (mul_le_mul_of_nonneg_right (calc abs' x ^ 4 ≤ abs' x ^ 1 : pow_le_pow_of_le_one (abs_nonneg _) (by rwa _root_.abs_of_nonneg (le_of_lt hx0)) dec_trivial ... = x : by simp [_root_.abs_of_nonneg (le_of_lt (hx0))]) (by norm_num)) ((div_le_div_right (by norm_num)).2 (calc x ^ 3 ≤ x ^ 1 : pow_le_pow_of_le_one (le_of_lt hx0) hx dec_trivial ... = x : pow_one _)) ... < x : by linarith) ... ≤ sin x : sub_le.1 (abs_sub_le_iff.1 (sin_bound (by rwa [_root_.abs_of_nonneg (le_of_lt hx0)]))).2 lemma sin_pos_of_pos_of_le_two {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 2) : 0 < sin x := have x / 2 ≤ 1, from div_le_of_le_mul (by norm_num) (by simpa), calc 0 < 2 * sin (x / 2) * cos (x / 2) : mul_pos (mul_pos (by norm_num) (sin_pos_of_pos_of_le_one (half_pos hx0) this)) (cos_pos_of_le_one (by rwa [_root_.abs_of_nonneg (le_of_lt (half_pos hx0))])) ... = sin x : by rw [← sin_two_mul, two_mul, add_halves] lemma cos_one_le : cos 1 ≤ 2 / 3 := calc cos 1 ≤ abs' (1 : ℝ) ^ 4 * (5 / 96) + (1 - 1 ^ 2 / 2) : sub_le_iff_le_add.1 (abs_sub_le_iff.1 (cos_bound (by simp))).1 ... ≤ 2 / 3 : by norm_num lemma cos_one_pos : 0 < cos 1 := cos_pos_of_le_one (by simp) lemma cos_two_neg : cos 2 < 0 := calc cos 2 = cos (2 * 1) : congr_arg cos (mul_one _).symm ... = _ : real.cos_two_mul 1 ... ≤ 2 * (2 / 3) ^ 2 - 1 : sub_le_sub_right (mul_le_mul_of_nonneg_left (by rw [pow_two, pow_two]; exact mul_self_le_mul_self (le_of_lt cos_one_pos) cos_one_le) (by norm_num)) _ ... < 0 : by norm_num end real namespace complex lemma abs_cos_add_sin_mul_I (x : ℝ) : abs (cos x + sin x * I) = 1 := have _ := real.sin_pow_two_add_cos_pow_two x, by simp [abs, norm_sq, pow_two, *, sin_of_real_re, cos_of_real_re, mul_re] at * lemma abs_exp_eq_iff_re_eq {x y : ℂ} : abs (exp x) = abs (exp y) ↔ x.re = y.re := by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y, abs_mul, abs_mul, abs_cos_add_sin_mul_I, abs_cos_add_sin_mul_I, ← of_real_exp, ← of_real_exp, abs_of_nonneg (le_of_lt (real.exp_pos _)), abs_of_nonneg (le_of_lt (real.exp_pos _)), mul_one, mul_one]; exact ⟨λ h, real.exp_injective h, congr_arg _⟩ @[simp] lemma abs_exp_of_real (x : ℝ) : abs (exp x) = real.exp x := by rw [← of_real_exp]; exact abs_of_nonneg (le_of_lt (real.exp_pos _)) end complex
c8a9045e2d119ec7c4cf13bbb39f1580bef7320b
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/tests/lean/run/stuckMVarBug.lean
0936f803a5206be2d16c8beaa4747ae5196cc1c8
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
480
lean
class HasMulComm (α : Type u) [Mul α] : Prop where mulComm : {a b : α} → a * b = b * a class A (α : Type u) where [Mul : Mul α] attribute [instance] A.mk A.Mul class B (α : Type u) where [Mul : Mul α] [HasMulComm : HasMulComm α] attribute [instance] B.mk B.Mul B.HasMulComm example [A α] [HasMulComm α] : B α := inferInstance section variables [A α] [HasMulComm α] example : B α := by exact inferInstance example : B α := inferInstance end
a6a829199ae616181cd5409443da70a86a28c21b
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/basics/unnamed_410.lean
995b7f318de93cb1bac55619f7d7c0d0e9d83bee
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
282
lean
import data.real.basic variables a b c d : ℝ -- BEGIN example (a b c d : ℝ) (hyp : c = d * a + b) (hyp' : b = a * d) : c = 2 * a * d := begin rw hyp' at hyp, rw mul_comm d a at hyp, rw ← two_mul (a * d) at hyp, rw ← mul_assoc 2 a d at hyp, exact hyp end -- END
44367b45834c735f0140bf02615f666a1cf0b3e2
efa51dd2edbbbbd6c34bd0ce436415eb405832e7
/20161026_ICTAC_Tutorial/ex35.lean
2d17b5f3b3889bdbedf4f3f790bf7b8ae9f7266f
[ "Apache-2.0" ]
permissive
leanprover/presentations
dd031a05bcb12c8855676c77e52ed84246bd889a
3ce2d132d299409f1de269fa8e95afa1333d644e
refs/heads/master
1,688,703,388,796
1,686,838,383,000
1,687,465,742,000
29,750,158
12
9
Apache-2.0
1,540,211,670,000
1,422,042,683,000
Lean
UTF-8
Lean
false
false
414
lean
/- Falsity Elimination: anything follows from false. -/ variables p q : Prop example (h : false) : p := false.elim h example (h : false) : p := @false.elim p h /- Negation -/ print not /- If we have (h1 : not p) and (h2 : p) we can derive false -/ example (h1 : not p) (h2 : p) : false := h1 h2 example (h1 : not p) (h2 : p) : q := false.elim (h1 h2) example (h1 : not p) (h2 : p) : q := absurd h2 h1
88073df3c79b159ab8237c60851dd38bc216ed45
d406927ab5617694ec9ea7001f101b7c9e3d9702
/archive/100-theorems-list/73_ascending_descending_sequences.lean
ce47545e5e0c296f53d96977f693fc2c16610439
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
8,024
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import data.fintype.powerset /-! # Erdős–Szekeres theorem This file proves Theorem 73 from the [100 Theorems List](https://www.cs.ru.nl/~freek/100/), also known as the Erdős–Szekeres theorem: given a sequence of more than `r * s` distinct values, there is an increasing sequence of length longer than `r` or a decreasing sequence of length longer than `s`. We use the proof outlined at https://en.wikipedia.org/wiki/Erdos-Szekeres_theorem#Pigeonhole_principle. ## Tags sequences, increasing, decreasing, Ramsey, Erdos-Szekeres, Erdős–Szekeres, Erdős-Szekeres -/ variables {α : Type*} [linear_order α] {β : Type*} open function finset open_locale classical /-- **Erdős–Szekeres Theorem**: Given a sequence of more than `r * s` distinct values, there is an increasing sequence of length longer than `r` or a decreasing sequence of length longer than `s`. Proof idea: We label each value in the sequence with two numbers specifying the longest increasing subsequence ending there, and the longest decreasing subsequence ending there. We then show the pair of labels must be unique. Now if there is no increasing sequence longer than `r` and no decreasing sequence longer than `s`, then there are at most `r * s` possible labels, which is a contradiction if there are more than `r * s` elements. -/ theorem erdos_szekeres {r s n : ℕ} {f : fin n → α} (hn : r * s < n) (hf : injective f) : (∃ (t : finset (fin n)), r < t.card ∧ strict_mono_on f ↑t) ∨ (∃ (t : finset (fin n)), s < t.card ∧ strict_anti_on f ↑t) := begin -- Given an index `i`, produce the set of increasing (resp., decreasing) subsequences which ends -- at `i`. let inc_sequences_ending_in : fin n → finset (finset (fin n)) := λ i, univ.powerset.filter (λ t, finset.max t = i ∧ strict_mono_on f ↑t), let dec_sequences_ending_in : fin n → finset (finset (fin n)) := λ i, univ.powerset.filter (λ t, finset.max t = i ∧ strict_anti_on f ↑t), -- The singleton sequence is in both of the above collections. -- (This is useful to show that the maximum length subsequence is at least 1, and that the set -- of subsequences is nonempty.) have inc_i : ∀ i, {i} ∈ inc_sequences_ending_in i := λ i, by simp [strict_mono_on], have dec_i : ∀ i, {i} ∈ dec_sequences_ending_in i := λ i, by simp [strict_anti_on], -- Define the pair of labels: at index `i`, the pair is the maximum length of an increasing -- subsequence ending at `i`, paired with the maximum length of a decreasing subsequence ending -- at `i`. -- We call these labels `(a_i, b_i)`. let ab : fin n → ℕ × ℕ, { intro i, apply (max' ((inc_sequences_ending_in i).image card) (nonempty.image ⟨{i}, inc_i i⟩ _), max' ((dec_sequences_ending_in i).image card) (nonempty.image ⟨{i}, dec_i i⟩ _)) }, -- It now suffices to show that one of the labels is 'big' somewhere. In particular, if the -- first in the pair is more than `r` somewhere, then we have an increasing subsequence in our -- set, and if the second is more than `s` somewhere, then we have a decreasing subsequence. rsuffices ⟨i, hi⟩ : ∃ i, r < (ab i).1 ∨ s < (ab i).2, { apply or.imp _ _ hi, work_on_goal 1 { have : (ab i).1 ∈ _ := max'_mem _ _ }, work_on_goal 2 { have : (ab i).2 ∈ _ := max'_mem _ _ }, all_goals { intro hi, rw mem_image at this, obtain ⟨t, ht₁, ht₂⟩ := this, refine ⟨t, by rwa ht₂, _⟩, rw mem_filter at ht₁, apply ht₁.2.2 } }, -- Show first that the pair of labels is unique. have : injective ab, { apply injective_of_lt_imp_ne, intros i j k q, injection q with q₁ q₂, -- We have two cases: `f i < f j` or `f j < f i`. -- In the former we'll show `a_i < a_j`, and in the latter we'll show `b_i < b_j`. cases lt_or_gt_of_ne (λ _, ne_of_lt ‹i < j› (hf ‹f i = f j›)), work_on_goal 1 { apply ne_of_lt _ q₁, have : (ab i).1 ∈ _ := max'_mem _ _ }, work_on_goal 2 { apply ne_of_lt _ q₂, have : (ab i).2 ∈ _ := max'_mem _ _ }, all_goals { -- Reduce to showing there is a subsequence of length `a_i + 1` which ends at `j`. rw nat.lt_iff_add_one_le, apply le_max', rw mem_image at this ⊢, -- In particular we take the subsequence `t` of length `a_i` which ends at `i`, by definition -- of `a_i` rcases this with ⟨t, ht₁, ht₂⟩, rw mem_filter at ht₁, -- Ensure `t` ends at `i`. have : t.max = i, simp [ht₁.2.1], -- Now our new subsequence is given by adding `j` at the end of `t`. refine ⟨insert j t, _, _⟩, -- First make sure it's valid, i.e., that this subsequence ends at `j` and is increasing { rw mem_filter, refine ⟨_, _, _⟩, { rw mem_powerset, apply subset_univ }, -- It ends at `j` since `i < j`. { convert max_insert, rw [ht₁.2.1, max_eq_left], apply with_bot.coe_le_coe.mpr (le_of_lt ‹i < j›) }, -- To show it's increasing (i.e., `f` is monotone increasing on `t.insert j`), we do cases -- on what the possibilities could be - either in `t` or equals `j`. simp only [strict_mono_on, strict_anti_on, coe_insert, set.mem_insert_iff, mem_coe], -- Most of the cases are just bashes. rintros x ⟨rfl | _⟩ y ⟨rfl | _⟩ _, { apply (irrefl _ ‹j < j›).elim }, { exfalso, apply not_le_of_lt (trans ‹i < j› ‹j < y›) (le_max_of_eq ‹y ∈ t› ‹t.max = i›) }, { apply lt_of_le_of_lt _ ‹f i < f j› <|> apply lt_of_lt_of_le ‹f j < f i› _, rcases lt_or_eq_of_le (le_max_of_eq ‹x ∈ t› ‹t.max = i›) with _ | rfl, { apply le_of_lt (ht₁.2.2 ‹x ∈ t› (mem_of_max ‹t.max = i›) ‹x < i›) }, { refl } }, { apply ht₁.2.2 ‹x ∈ t› ‹y ∈ t› ‹x < y› } }, -- Finally show that this new subsequence is one longer than the old one. { rw [card_insert_of_not_mem, ht₂], intro _, apply not_le_of_lt ‹i < j› (le_max_of_eq ‹j ∈ t› ‹t.max = i›) } } }, -- Finished both goals! -- Now that we have uniqueness of each label, it remains to do some counting to finish off. -- Suppose all the labels are small. by_contra q, push_neg at q, -- Then the labels `(a_i, b_i)` all fit in the following set: `{ (x,y) | 1 ≤ x ≤ r, 1 ≤ y ≤ s }` let ran : finset (ℕ × ℕ) := (range r).image nat.succ ×ˢ (range s).image nat.succ, -- which we prove here. have : image ab univ ⊆ ran, -- First some logical shuffling { rintro ⟨x₁, x₂⟩, simp only [mem_image, exists_prop, mem_range, mem_univ, mem_product, true_and, prod.mk.inj_iff], rintros ⟨i, rfl, rfl⟩, specialize q i, -- Show `1 ≤ a_i` and `1 ≤ b_i`, which is easy from the fact that `{i}` is a increasing and -- decreasing subsequence which we did right near the top. have z : 1 ≤ (ab i).1 ∧ 1 ≤ (ab i).2, { split; { apply le_max', rw mem_image, refine ⟨{i}, by solve_by_elim, card_singleton i⟩ } }, refine ⟨_, _⟩, -- Need to get `a_i ≤ r`, here phrased as: there is some `a < r` with `a+1 = a_i`. { refine ⟨(ab i).1 - 1, _, nat.succ_pred_eq_of_pos z.1⟩, rw tsub_lt_iff_right z.1, apply nat.lt_succ_of_le q.1 }, { refine ⟨(ab i).2 - 1, _, nat.succ_pred_eq_of_pos z.2⟩, rw tsub_lt_iff_right z.2, apply nat.lt_succ_of_le q.2 } }, -- To get our contradiction, it suffices to prove `n ≤ r * s` apply not_le_of_lt hn, -- Which follows from considering the cardinalities of the subset above, since `ab` is injective. simpa [nat.succ_injective, card_image_of_injective, ‹injective ab›] using card_le_of_subset this, end
2dd70e4c8470fa0ab3365301a365b03f4b5ad30f
d257837ee5f268fcb6e9b719129ff77f25de30a0
/student_contributions/ellen_matrices.lean
b7b19934e14a0280f716f16dbb678e57db4b2980
[]
no_license
pirocks/xena
a05a8a7da5d15b1cc28830eb84d370f3d60edbbf
458a43f36964e5bb7d2d8ea74060d105519e331a
refs/heads/master
1,620,756,281,811
1,516,097,784,000
1,516,097,784,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,413
lean
import algebra.big_operators definition matrix (R: Type) (n m : nat)[ring R] := fin n →( fin m → R ) namespace matrix definition add ( R : Type) [ring R] {n m: nat }(A:matrix R m n) ( B : matrix R m n): (matrix R m n):= begin unfold matrix, intros I J, exact A I J + B I J end definition neg ( R : Type) [ring R] {n m: nat } (A:matrix R m n) : (matrix R m n):= begin unfold matrix, intros I J, exact - A I J, end definition zero ( R : Type) [ring R] {m n: nat }: (matrix R m n):= begin intros I J, exact 0 end theorem add_assoc1 ( R : Type) [ring R] {m n: nat }(A:matrix R m n) ( B : matrix R m n) ( C : matrix R m n) : add R A (add R B C) = add R (add R A B) C := begin apply funext, intro, apply funext, intro y, show A x y + (B x y + C x y) = ( A x y + B x y) + C x y, rw [add_assoc], end theorem add_assoc2 ( R : Type) [ring R] {m n: nat }(A:matrix R m n) ( B : matrix R m n) ( C : matrix R m n) : add R (add R A B) C = add R A (add R B C) := begin apply funext, intro, apply funext, intro y, show ( A x y + B x y) + C x y = A x y + (B x y + C x y) , rw[add_assoc], end theorem zero_add ( R : Type) [ring R] {m n: nat }(A:matrix R m n): add R (zero R) A = A:= begin apply funext, intro, apply funext, intro y, show 0 + A x y = A x y, rw[zero_add] end theorem add_zero (R : Type) [ring R] {m n: nat }(A:matrix R m n): add R A (zero R) = A:= begin apply funext, intro, apply funext, intro y, show A x y + 0 = A x y, rw[add_zero] end theorem add_left_neg (R : Type) [ring R] {m n: nat }(A:matrix R m n) : add R ( neg R A) A = zero R := begin apply funext, intro, apply funext, intro y, show - A x y + A x y = 0 , rw[add_left_neg] end theorem add_comm (R : Type) [ring R] {m n: nat }(A:matrix R m n)( B : matrix R m n): add R A B = add R B A := begin apply funext, intro, apply funext, intro y, show A x y + B x y = B x y + A x y , rw [add_comm] end instance add_comm_group ( R : Type) [ring R] {m n: nat }: add_comm_group (matrix R m n):={ add:= matrix.add R , add_assoc := @matrix.add_assoc2 R _ m n, zero := matrix.zero R , neg := matrix.neg R, zero_add := @matrix.zero_add R _ m n, add_zero := @matrix.add_zero R _ m n, add_left_neg := @matrix.add_left_neg R _ m n, add_comm := @matrix.add_comm R _ m n } end matrix definition mul ( R : Type) [ring R] {n m l: nat }(A:matrix R m n) ( B : matrix R n l): (matrix R m l ):= begin unfold matrix, intros I J, admit end
3c7dd3071b91821d46e0a48a089d512e65fbac20
94637389e03c919023691dcd05bd4411b1034aa5
/src/inClassNotes/02_polymorphicTypes.lean
6b8772bd8a1b146461c795321bc1fe83c564ddd4
[]
no_license
kevinsullivan/complogic-s21
7c4eef2105abad899e46502270d9829d913e8afc
99039501b770248c8ceb39890be5dfe129dc1082
refs/heads/master
1,682,985,669,944
1,621,126,241,000
1,621,126,241,000
335,706,272
0
38
null
1,618,325,669,000
1,612,374,118,000
Lean
UTF-8
Lean
false
false
5,649
lean
namespace hidden /- Inductive, aka algebraic, data types - sum types - product types and case analysis - parametrically polymorphic - sum of product types and destructuring -/ /- SUM TYPES A value of such a type is "one of these OR one of those OR one of something else." SUM means OR in this sense. -/ inductive romanNumeral : Type | I : romanNumeral | II : romanNumeral | III : romanNumeral | IV : romanNumeral | V : romanNumeral -- The empty data type inductive empty : Type -- Look Ma, no terms ("uninhabited") def x : empty := _ def funny : empty → empty := λ (e : empty), e -- The unit data type inductive unit : Type -- A type with one value (void) | star : unit -- The bool data type inductive bool : Type -- Two values | tt : bool | ff -- leave type to be inferred -- An inductive type with seven values inductive day : Type | sun | mon | tue | wed | thu | fri | sat open day /- CASE ANALYSIS We generally define functions that consume values of sum types by case analysis. To know what to return, we need to know what form of value we got as an argument. -/ def next_day : day → day | sun := mon | mon := tue | tue := wed | wed := thu | thu := fri | fri := sat | sat := sun /- The left side of a case is usually called a pattern. Argument values are matched to patterns in the order in which patterns are using a "unification" algorithm (more on that later). Function evaluation finds the first pattern that matches and return the result obtained by evaluating the expression on the right hand side of that pattern-matching rule. -/ #reduce next_day sun #reduce next_day sat /- The _ character can be used to match any value. All functions in Lean must be total. We often use _ to cover all other cases no covered explicitly so far. -/ def isWeekday : day → bool | sat := bool.ff | sun := bool.ff | _ := bool.tt /- PRODUCT TYPES (records, structures) -/ /- A product type has one constructor that takes and bundles up values of zero or more other types into records, or structures. In a value of a product type there a value for the first field of an object AND a value for the secon AND a value for the third, etc. PRODUCT in this sense means AND. -/ /- Note: The empty type can be viewed as also a product type with zero fields. -/ /- We now define a product type with one field. To understand such type definitions you need to understand that a constructor, C, is like a function, with zero or more arguments, the only thing it does is to package up its arguments into a term, (C a₀ ... aₙ). A value of our box type can thus be visualized as a box (term) with a single value, the argument to box.mk, inside. As usual there are several syntactic variants for defining inductive types. -/ inductive box_nat' : Type | mk (val : ℕ) -- : box_nat' inductive box_nat'' : Type | mk : ℕ → box_nat'' structure box_nat''' : Type := mk :: (val : ℕ) structure box_nat := -- readable (val : ℕ) -- Let's create such a value def aBox := box_nat.mk 3 -- What does the resulting term look like? #reduce aBox /- Given a box, we "destructure" it using "pattern matching" to get at the values that were used in its construction: in this case to access the ℕ inside a box. Here we see a more interesting form of unification. The key ideas are (1) the algorithm determines whether a pattern matches, (2) it binds names to parts of the object being matched accordingly. -/ def unbox_nat : box_nat → ℕ -- box_nat.mk 3 -- pattern matching, unification | (box_nat.mk n) := n #eval unbox_nat aBox /- When you use the "structure" syntax, Lean generates a projection (accessor) function for each field automatically. -/ #eval box_nat.val aBox #eval aBox.val -- Preferred notation /- Polymorphic types -/ /- We immediately see the same problem as with functions: the need for a many variants varying only in the type of value "contained in the box". The solution is analogous: make our type polymorphic by having it take a type-valued parameter and by making the value it contains of the type that is the value of that parameter. -/ structure box (α : Type) : Type := /-mk ::-/ (val : α) #check box def nat_box : box nat := box.mk 3 def bool_box : box bool := box.mk bool.tt #check nat_box #check box def str_box : box string:= box.mk "Hello, Lean!" def bool_box' := box.mk bool.tt #eval nat_box.val #eval str_box.val #eval bool_box.val def fun_box : box (nat → nat) := box.mk (nat.succ) def crazy_box : box (box (nat → nat)) := box.mk (box.mk nat.succ) #check crazy_box def f : ℕ → ℕ := (box.val) fun_box #eval f 5 #check nat → nat #check nat.succ #eval nat.succ 4 /- Polymorphic product types with two fields -- the type of ordered pairs -- and two type parameters accordingly. -/ structure prod (α β : Type) : Type := (fst : α) (snd : β) -- Self-test: What's the type of prod? -- "Introduce" some pairs def pair1 := prod.mk 4 "Hi" #check pair1 #check prod #eval prod.fst pair1 #eval prod.snd pair1 #eval pair1.fst #eval pair1.snd def pair2 := prod.mk "Bye" tt -- "Eliminate" some pairs #eval pair1.fst #eval pair1.snd #eval pair2.fst #eval pair2.snd #check prod structure phythagorean_triple : Type := (a : ℕ) (b : ℕ) (c : ℕ) (cert: a*a + b*b = c*c) def py_tri : phythagorean_triple := phythagorean_triple.mk 3 4 5 rfl #reduce py_tri.cert -- construct proof that 25 = 25 def py_tri_bad : phythagorean_triple := phythagorean_triple.mk 3 4 6 rfl -- can't construct proof that 25 = 36 -- Try that in mere Haskell. No way! end hidden
e8b9c5ff306cb05887b691584959768560b9cd08
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/linear_algebra/multilinear.lean
8d53f14d661434717cded1c83b3e57bc64ed9fd0
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
33,006
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 tactic.omega import data.fintype.card /-! # 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 `univ.prod c • 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 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 ι] [ring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [∀i, module R (M₁ i)] [module R M₂] := (to_fun : (Πi, M₁ i) → M₂) (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)) (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 ring variables [ring R] [∀i, add_comm_group (M i)] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [add_comm_group M₃] [add_comm_group 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₂) := ⟨_, to_fun⟩ @[ext] theorem ext {f f' : multilinear_map R M₁ M₂} (H : ∀ x, f x = f' x) : f = f' := by cases f; cases f'; congr'; exact funext H @[simp] lemma map_add (m : Πi, M₁ i) (i : ι) (x y : M₁ i) : f (update m i (x + y)) = f (update m i x) + f (update m i y) := f.add m i x y @[simp] lemma map_smul (m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i) : f (update m i (c • x)) = c • f (update m i x) := f.smul m i c x @[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 { simp only [map_add, add_left_inj, sub_eq_add_neg, (neg_one_smul R y).symm, map_smul], simp } 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_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], λ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_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_zero (multilinear_map R M₁ M₂) := ⟨⟨λ _, 0, λm i x y, by simp, λm i c x, by simp⟩⟩ instance : inhabited (multilinear_map R M₁ M₂) := ⟨0⟩ @[simp] lemma zero_apply (m : Πi, M₁ i) : (0 : multilinear_map R M₁ M₂) m = 0 := rfl instance : add_comm_group (multilinear_map R M₁ M₂) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext; simp [add_comm, add_left_comm] @[simp] lemma sum_apply {α : Type*} (f : α → multilinear_map R M₁ M₂) (m : Πi, M₁ i) : ∀ {s : finset α}, (s.sum f) m = s.sum (λ a, f a m) := begin classical, apply finset.induction, { rw finset.sum_empty, simp }, { assume a s has H, rw finset.sum_insert has, simp [H, has] } end /-- If `f` is a multilinear map, then `f.to_linear_map m i` is the linear map obtained by fixing all coordinates but `i` equal to those of `m`, and varying the `i`-th coordinate. -/ def to_linear_map (m : Πi, M₁ i) (i : ι) : M₁ i →ₗ[R] M₂ := { to_fun := λx, f (update m i x), add := λx y, by simp, 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), add := λ m i x y, by simp, smul := λ m i c x, by simp } /-- Given a multilinear map `f` on `n` variables (parameterized by `fin n`) and a subset `s` of `k` of these variables, one gets a new multilinear map on `fin k` by varying these variables, and fixing the other ones equal to a given value `z`. It is denoted by `f.restr s hk z`, where `hk` is a proof that the cardinality of `s` is `k`. The implicit identification between `fin k` and `s` that we use is the canonical (increasing) bijection. -/ noncomputable 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.mono_equiv_of_fin hk).symm ⟨j, h⟩) else z), add := λ v i x y, by { erw [dite_comp_equiv_update, dite_comp_equiv_update, dite_comp_equiv_update], simp }, 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] /-- If `g` is multilinear and `f` is linear, then `g (f m₁, ..., f mₙ)` is again a multilinear function, that we call `g.comp_linear_map f`. -/ def comp_linear_map (g : multilinear_map R (λ (i : ι), M₂) M₃) (f : M' →ₗ[R] M₂) : multilinear_map R (λ (i : ι), M') M₃ := { to_fun := λ m, g (f ∘ m), add := λ m i x y, by simp [comp_update], smul := λ m i c x, by simp [comp_update] } /-- 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') = t.powerset.sum (λs, 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') = (finset.univ : finset (finset ι)).sum (λs, f (s.piecewise m m')) := by simpa using f.map_piecewise_add m m' finset.univ section apply_sum variables {α : ι → Type*} [fintype ι] (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 {n : ℕ} (h : finset.univ.sum (λ i, (A i).card) = n) : f (λ i, (A i).sum (g i)) = (pi_finset A).sum (λ r, f (λ i, g i (r i))) := begin unfreezeI, 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 : (A i).sum (λ j, g i j) = 0, by convert sum_empty, rw f.map_coord_zero i this, have : pi_finset A = ∅, { apply finset.eq_empty_of_forall_not_mem (λ r hr, _), have : r i ∈ A i := mem_pi_finset.mp hr i, rwa hi at this }, convert sum_empty.symm }, push_neg at Ai_empty, -- Otherwise, if all sets are at most singletons, then they are exactly singletons and the result -- is again straightforward by_cases Ai_singleton : ∀ i, (A i).card ≤ 1, { have Ai_card : ∀ i, (A i).card = 1, { assume i, have : finset.card (A i) ≠ 0, by simp [finset.card_eq_zero, Ai_empty i], have : finset.card (A i) ≤ 1 := Ai_singleton i, omega }, have : ∀ (r : Π i, α i), r ∈ pi_finset A → f (λ i, g i (r i)) = f (λ i, (A i).sum (λ j, g i j)), { assume r hr, unfold_coes, congr, ext 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, add_monoid.one_smul] }, simp only [sum_congr rfl this, Ai_card, card_pi_finset, prod_const_one, add_monoid.one_smul, 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₀ \ finset.singleton j₂), let C := function.update A i₀ (finset.singleton 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, (A i).sum (g i)) = function.update (λ i, (A i).sum (g i)) i₀ ((B i₀).sum (g i₀) + (C i₀).sum (g i₀)), { 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.insert_empty_eq_singleton, finset.sdiff_union_self_eq_union], symmetry, simp only [hj₂, finset.singleton_subset_iff, union_eq_left_iff_subset] }, rw this, apply finset.sum_union, apply finset.disjoint_right.2 (λ j hj, _), have : j = j₂, by { dsimp [C] at hj, simpa using hj }, rw this, dsimp [B], simp only [mem_sdiff, eq_self_iff_true, not_true, not_false_iff, finset.mem_singleton, update_same, and_false] }, { simp [hi] } }, have Beq : function.update (λ i, (A i).sum (g i)) i₀ ((B i₀).sum (g i₀)) = (λ i, finset.sum (B i) (g i)), { 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, (A i).sum (g i)) i₀ ((C i₀).sum (g i₀)) = (λ i, finset.sum (C i) (g i)), { 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, finset.sum (B i) (g i)) = (pi_finset B).sum (λ r, f (λ i, g i (r i))), { have : finset.univ.sum (λ i, finset.card (B i)) < finset.univ.sum (λ 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 : finset.singleton j₂ ⊆ A i₀, by simp [hj₂], simp only [B, finset.card_sdiff this, function.update_same, finset.insert_empty_eq_singleton, finset.card_singleton], exact nat.pred_lt (ne_of_gt (lt_trans zero_lt_one hi₀)) }, rw h at this, exact IH _ this B rfl }, -- Express the inductive assumption for `C` have Crec : f (λ i, finset.sum (C i) (g i)) = (pi_finset C).sum (λ r, f (λ i, g i (r i))), { have : finset.univ.sum (λ i, finset.card (C i)) < finset.univ.sum (λ 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 : f (λ i, (A i).sum (g i)) = (pi_finset A).sum (λ r, 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 [∀ i, fintype (α i)] : f (λ i, finset.univ.sum (g i)) = finset.univ.sum (λ (r : Π i, α i), f (λ i, g i (r i))) := f.map_sum_finset g (λ i, finset.univ) end apply_sum end ring section comm_ring variables [comm_ring R] [∀i, add_comm_group (M₁ i)] [∀i, add_comm_group (M i)] [add_comm_group 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 `s.prod c`. 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) = s.prod c • 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 `univ.prod c • f m`. -/ lemma map_smul_univ [fintype ι] (c : ι → R) (m : Πi, M₁ i) : f (λi, c i • m i) = finset.univ.prod c • f m := by simpa using map_piecewise_smul f c m finset.univ instance : has_scalar R (multilinear_map R M₁ M₂) := ⟨λ c f, ⟨λ m, c • f m, λm i x y, by simp [smul_add], λl i x d, by simp [smul_smul, mul_comm]⟩⟩ @[simp] lemma smul_apply (c : R) (m : Πi, M₁ i) : (c • f) m = c • f m := rfl /-- The space of multilinear maps is a module over `R`, for the pointwise addition and scalar multiplication. -/ instance : module R (multilinear_map R M₁ M₂) := module.of_core $ by refine { smul := (•), ..}; intros; ext; simp [smul_add, add_smul, smul_smul] 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) -/ protected def mk_pi_ring [fintype ι] (z : M₂) : multilinear_map R (λ(i : ι), R) M₂ := { to_fun := λm, finset.univ.prod m • z, add := λ m i x y, by simp [finset.prod_update_of_mem, add_mul, add_smul], smul := λ m i c x, by { rw [smul_eq_mul], simp [finset.prod_update_of_mem, smul_smul, mul_assoc] } } variables {R ι} @[simp] lemma mk_pi_ring_apply [fintype ι] (z : M₂) (m : ι → R) : (multilinear_map.mk_pi_ring R ι z : (ι → R) → M₂) m = finset.univ.prod m • 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 variables (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), add := λ z z', by { ext m, simp [smul_add] }, 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_ring end multilinear_map namespace linear_map variables [ring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [add_comm_group M₃] [∀i, module R (M₁ i)] [module R M₂] [module R M₃] /-- 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 := λ m, g (f m), add := λ m i x y, by simp, smul := λ m i c x, by simp } end linear_map section currying /-! ### Currying We associate to a multilinear map in `n+1` variables (i.e., based on `fin n.succ`) two curried functions, named `f.curry_left` (which is a linear map on `E 0` taking values in multilinear maps in `n` variables) and `f.curry_right` (wich is a multilinear map in `n` variables taking values in linear maps on `E 0`). In both constructions, the variable that is singled out is `0`, to take advantage of the operations `cons` and `tail` on `fin n`. The inverse operations are called `uncurry_left` and `uncurry_right`. We also register linear equiv versions of these correspondences, in `multilinear_curry_left_equiv` and `multilinear_curry_right_equiv`. -/ open multilinear_map variables {R M M₂} [comm_ring R] [∀i, add_comm_group (M i)] [add_comm_group M'] [add_comm_group M₂] [∀i, module R (M i)] [module R M'] [module R M₂] /-! #### Left currying -/ /-- Given a linear map `f` from `M 0` to multilinear maps on `n` variables, construct the corresponding multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (m 0) (tail m)`-/ def linear_map.uncurry_left (f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) : multilinear_map R M M₂ := { to_fun := λm, f (m 0) (tail m), add := λm i x y, begin by_cases h : i = 0, { revert x y, rw h, assume x y, rw [update_same, update_same, update_same, f.map_add, add_apply, tail_update_zero, tail_update_zero, tail_update_zero] }, { rw [update_noteq (ne.symm h), update_noteq (ne.symm h), update_noteq (ne.symm h)], revert x y, rw ← succ_pred i h, assume x y, rw [tail_update_succ, map_add, tail_update_succ, tail_update_succ] } end, smul := λm i c x, begin by_cases h : i = 0, { revert x, rw h, assume x, rw [update_same, update_same, tail_update_zero, tail_update_zero, ← smul_apply, f.map_smul] }, { rw [update_noteq (ne.symm h), update_noteq (ne.symm h)], revert x, rw ← succ_pred i h, assume x, rw [tail_update_succ, tail_update_succ, map_smul] } end } @[simp] lemma linear_map.uncurry_left_apply (f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) (m : Πi, M i) : f.uncurry_left m = f (m 0) (tail m) := rfl /-- Given a multilinear map `f` in `n+1` variables, split the first variable to obtain a linear map into multilinear maps in `n` variables, given by `x ↦ (m ↦ f (cons x m))`. -/ def multilinear_map.curry_left (f : multilinear_map R M M₂) : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂) := { to_fun := λx, { to_fun := λm, f (cons x m), add := λm i y y', by simp, smul := λm i y c, by simp }, add := λx y, by { ext m, exact cons_add f m x y }, 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, add := λf₁ f₂, by { ext m, refl }, 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)), add := λm i x y, begin by_cases h : i.val < n, { have : last n ≠ i := ne.symm (ne_of_lt h), rw [update_noteq this, update_noteq this, update_noteq this], revert x y, rw [(cast_succ_cast_lt i h).symm], assume x y, rw [init_update_cast_succ, map_add, init_update_cast_succ, init_update_cast_succ, linear_map.add_apply] }, { revert x y, rw eq_last_of_not_lt h, assume x y, rw [init_update_last, init_update_last, init_update_last, update_same, update_same, update_same, linear_map.map_add] } end, smul := λm i c x, begin by_cases h : i.val < n, { have : last n ≠ i := ne.symm (ne_of_lt h), rw [update_noteq this, update_noteq this], revert x, rw [(cast_succ_cast_lt i h).symm], assume x, rw [init_update_cast_succ, init_update_cast_succ, map_smul, linear_map.smul_apply] }, { revert x, rw eq_last_of_not_lt h, assume x, rw [update_same, update_same, init_update_last, init_update_last, linear_map.map_smul] } end } @[simp] lemma multilinear_map.uncurry_right_apply (f : (multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂))) (m : Πi, M i) : f.uncurry_right m = f (init m) (m (last n)) := rfl /-- Given a multilinear map `f` in `n+1` variables, split the last variable to obtain a multilinear map in `n` variables taking values in linear maps from `M (last n)` to `M₂`, given by `m ↦ (x ↦ f (snoc m x))`. -/ def multilinear_map.curry_right (f : multilinear_map R M M₂) : multilinear_map R (λ(i : fin n), M (fin.cast_succ i)) ((M (last n)) →ₗ[R] M₂) := { to_fun := λm, { to_fun := λx, f (snoc m x), add := λx y, by rw f.snoc_add, smul := λc x, by rw f.snoc_smul }, 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, 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, add := λf₁ f₂, by { ext m, refl }, smul := λc f, by { ext m, rw [smul_apply], refl }, inv_fun := multilinear_map.curry_right, left_inv := multilinear_map.curry_uncurry_right, right_inv := multilinear_map.uncurry_curry_right } end currying
5a6464e7cb54f5ded1e7329128965c089b08d3cd
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/order/compare.lean
92fb96f856652557fece6a2f41da62a0636d3cf5
[ "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
8,378
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import order.synonym /-! # Comparison > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file provides basic results about orderings and comparison in linear orders. ## Definitions * `cmp_le`: An `ordering` from `≤`. * `ordering.compares`: Turns an `ordering` into `<` and `=` propositions. * `linear_order_of_compares`: Constructs a `linear_order` instance from the fact that any two elements that are not one strictly less than the other either way are equal. -/ variables {α : Type*} /-- Like `cmp`, but uses a `≤` on the type instead of `<`. Given two elements `x` and `y`, returns a three-way comparison result `ordering`. -/ def cmp_le {α} [has_le α] [@decidable_rel α (≤)] (x y : α) : ordering := if x ≤ y then if y ≤ x then ordering.eq else ordering.lt else ordering.gt lemma cmp_le_swap {α} [has_le α] [is_total α (≤)] [@decidable_rel α (≤)] (x y : α) : (cmp_le x y).swap = cmp_le y x := begin by_cases xy : x ≤ y; by_cases yx : y ≤ x; simp [cmp_le, *, ordering.swap], cases not_or xy yx (total_of _ _ _) end lemma cmp_le_eq_cmp {α} [preorder α] [is_total α (≤)] [@decidable_rel α (≤)] [@decidable_rel α (<)] (x y : α) : cmp_le x y = cmp x y := begin by_cases xy : x ≤ y; by_cases yx : y ≤ x; simp [cmp_le, lt_iff_le_not_le, *, cmp, cmp_using], cases not_or xy yx (total_of _ _ _) end namespace ordering /-- `compares o a b` means that `a` and `b` have the ordering relation `o` between them, assuming that the relation `a < b` is defined. -/ @[simp] def compares [has_lt α] : ordering → α → α → Prop | lt a b := a < b | eq a b := a = b | gt a b := a > b lemma compares_swap [has_lt α] {a b : α} {o : ordering} : o.swap.compares a b ↔ o.compares b a := by { cases o, exacts [iff.rfl, eq_comm, iff.rfl] } alias compares_swap ↔ compares.of_swap compares.swap @[simp] theorem swap_inj (o₁ o₂ : ordering) : o₁.swap = o₂.swap ↔ o₁ = o₂ := by cases o₁; cases o₂; dec_trivial lemma swap_eq_iff_eq_swap {o o' : ordering} : o.swap = o' ↔ o = o'.swap := by rw [←swap_inj, swap_swap] lemma compares.eq_lt [preorder α] : ∀ {o} {a b : α}, compares o a b → (o = lt ↔ a < b) | lt a b h := ⟨λ _, h, λ _, rfl⟩ | eq a b h := ⟨λ h, by injection h, λ h', (ne_of_lt h' h).elim⟩ | gt a b h := ⟨λ h, by injection h, λ h', (lt_asymm h h').elim⟩ lemma compares.ne_lt [preorder α] : ∀ {o} {a b : α}, compares o a b → (o ≠ lt ↔ b ≤ a) | lt a b h := ⟨absurd rfl, λ h', (not_le_of_lt h h').elim⟩ | eq a b h := ⟨λ _, ge_of_eq h, λ _ h, by injection h⟩ | gt a b h := ⟨λ _, le_of_lt h, λ _ h, by injection h⟩ lemma compares.eq_eq [preorder α] : ∀ {o} {a b : α}, compares o a b → (o = eq ↔ a = b) | lt a b h := ⟨λ h, by injection h, λ h', (ne_of_lt h h').elim⟩ | eq a b h := ⟨λ _, h, λ _, rfl⟩ | gt a b h := ⟨λ h, by injection h, λ h', (ne_of_gt h h').elim⟩ lemma compares.eq_gt [preorder α] {o} {a b : α} (h : compares o a b) : (o = gt ↔ b < a) := swap_eq_iff_eq_swap.symm.trans h.swap.eq_lt lemma compares.ne_gt [preorder α] {o} {a b : α} (h : compares o a b) : (o ≠ gt ↔ a ≤ b) := (not_congr swap_eq_iff_eq_swap.symm).trans h.swap.ne_lt lemma compares.le_total [preorder α] {a b : α} : ∀ {o}, compares o a b → a ≤ b ∨ b ≤ a | lt h := or.inl (le_of_lt h) | eq h := or.inl (le_of_eq h) | gt h := or.inr (le_of_lt h) lemma compares.le_antisymm [preorder α] {a b : α} : ∀ {o}, compares o a b → a ≤ b → b ≤ a → a = b | lt h _ hba := (not_le_of_lt h hba).elim | eq h _ _ := h | gt h hab _ := (not_le_of_lt h hab).elim lemma compares.inj [preorder α] {o₁} : ∀ {o₂} {a b : α}, compares o₁ a b → compares o₂ a b → o₁ = o₂ | lt a b h₁ h₂ := h₁.eq_lt.2 h₂ | eq a b h₁ h₂ := h₁.eq_eq.2 h₂ | gt a b h₁ h₂ := h₁.eq_gt.2 h₂ lemma compares_iff_of_compares_impl {β : Type*} [linear_order α] [preorder β] {a b : α} {a' b' : β} (h : ∀ {o}, compares o a b → compares o a' b') (o) : compares o a b ↔ compares o a' b' := begin refine ⟨h, λ ho, _⟩, cases lt_trichotomy a b with hab hab, { change compares ordering.lt a b at hab, rwa [ho.inj (h hab)] }, { cases hab with hab hab, { change compares ordering.eq a b at hab, rwa [ho.inj (h hab)] }, { change compares ordering.gt a b at hab, rwa [ho.inj (h hab)] } } end lemma swap_or_else (o₁ o₂) : (or_else o₁ o₂).swap = or_else o₁.swap o₂.swap := by cases o₁; try {refl}; cases o₂; refl lemma or_else_eq_lt (o₁ o₂) : or_else o₁ o₂ = lt ↔ o₁ = lt ∨ (o₁ = eq ∧ o₂ = lt) := by cases o₁; cases o₂; exact dec_trivial end ordering open ordering order_dual @[simp] lemma to_dual_compares_to_dual [has_lt α] {a b : α} {o : ordering} : compares o (to_dual a) (to_dual b) ↔ compares o b a := by { cases o, exacts [iff.rfl, eq_comm, iff.rfl] } @[simp] lemma of_dual_compares_of_dual [has_lt α] {a b : αᵒᵈ} {o : ordering} : compares o (of_dual a) (of_dual b) ↔ compares o b a := by { cases o, exacts [iff.rfl, eq_comm, iff.rfl] } lemma cmp_compares [linear_order α] (a b : α) : (cmp a b).compares a b := by obtain h | h | h := lt_trichotomy a b; simp [cmp, cmp_using, h, h.not_lt] lemma ordering.compares.cmp_eq [linear_order α] {a b : α} {o : ordering} (h : o.compares a b) : cmp a b = o := (cmp_compares a b).inj h @[simp] lemma cmp_swap [preorder α] [@decidable_rel α (<)] (a b : α) : (cmp a b).swap = cmp b a := begin unfold cmp cmp_using, by_cases a < b; by_cases h₂ : b < a; simp [h, h₂, ordering.swap], exact lt_asymm h h₂ end @[simp] lemma cmp_le_to_dual [has_le α] [@decidable_rel α (≤)] (x y : α) : cmp_le (to_dual x) (to_dual y) = cmp_le y x := rfl @[simp] lemma cmp_le_of_dual [has_le α] [@decidable_rel α (≤)] (x y : αᵒᵈ) : cmp_le (of_dual x) (of_dual y) = cmp_le y x := rfl @[simp] lemma cmp_to_dual [has_lt α] [@decidable_rel α (<)] (x y : α) : cmp (to_dual x) (to_dual y) = cmp y x := rfl @[simp] lemma cmp_of_dual [has_lt α] [@decidable_rel α (<)] (x y : αᵒᵈ) : cmp (of_dual x) (of_dual y) = cmp y x := rfl /-- Generate a linear order structure from a preorder and `cmp` function. -/ def linear_order_of_compares [preorder α] (cmp : α → α → ordering) (h : ∀ a b, (cmp a b).compares a b) : linear_order α := { le_antisymm := λ a b, (h a b).le_antisymm, le_total := λ a b, (h a b).le_total, decidable_le := λ a b, decidable_of_iff _ (h a b).ne_gt, decidable_lt := λ a b, decidable_of_iff _ (h a b).eq_lt, decidable_eq := λ a b, decidable_of_iff _ (h a b).eq_eq, .. ‹preorder α› } variables [linear_order α] (x y : α) @[simp] lemma cmp_eq_lt_iff : cmp x y = ordering.lt ↔ x < y := ordering.compares.eq_lt (cmp_compares x y) @[simp] lemma cmp_eq_eq_iff : cmp x y = ordering.eq ↔ x = y := ordering.compares.eq_eq (cmp_compares x y) @[simp] lemma cmp_eq_gt_iff : cmp x y = ordering.gt ↔ y < x := ordering.compares.eq_gt (cmp_compares x y) @[simp] lemma cmp_self_eq_eq : cmp x x = ordering.eq := by rw cmp_eq_eq_iff variables {x y} {β : Type*} [linear_order β] {x' y' : β} lemma cmp_eq_cmp_symm : cmp x y = cmp x' y' ↔ cmp y x = cmp y' x' := by rw [←cmp_swap x', ←cmp_swap x, swap_inj] lemma lt_iff_lt_of_cmp_eq_cmp (h : cmp x y = cmp x' y') : x < y ↔ x' < y' := by rw [←cmp_eq_lt_iff, ←cmp_eq_lt_iff, h] lemma le_iff_le_of_cmp_eq_cmp (h : cmp x y = cmp x' y') : x ≤ y ↔ x' ≤ y' := by { rw [←not_lt, ←not_lt], apply not_congr, apply lt_iff_lt_of_cmp_eq_cmp, rwa cmp_eq_cmp_symm } lemma eq_iff_eq_of_cmp_eq_cmp (h : cmp x y = cmp x' y') : x = y ↔ x' = y' := by rw [le_antisymm_iff, le_antisymm_iff, le_iff_le_of_cmp_eq_cmp h, le_iff_le_of_cmp_eq_cmp (cmp_eq_cmp_symm.1 h)] lemma has_lt.lt.cmp_eq_lt (h : x < y) : cmp x y = ordering.lt := (cmp_eq_lt_iff _ _).2 h lemma has_lt.lt.cmp_eq_gt (h : x < y) : cmp y x = ordering.gt := (cmp_eq_gt_iff _ _).2 h lemma eq.cmp_eq_eq (h : x = y) : cmp x y = ordering.eq := (cmp_eq_eq_iff _ _).2 h lemma eq.cmp_eq_eq' (h : x = y) : cmp y x = ordering.eq := h.symm.cmp_eq_eq
1ab71dac6794bf397605625c0ebd992367de9206
57fdc8de88f5ea3bfde4325e6ecd13f93a274ab5
/algebra/ordered_group.lean
5a2d54c461d7bcb6e76bc0c09b1f4cfb0742ce9a
[ "Apache-2.0" ]
permissive
louisanu/mathlib
11f56f2d40dc792bc05ee2f78ea37d73e98ecbfe
2bd5e2159d20a8f20d04fc4d382e65eea775ed39
refs/heads/master
1,617,706,993,439
1,523,163,654,000
1,523,163,654,000
124,519,997
0
0
Apache-2.0
1,520,588,283,000
1,520,588,283,000
null
UTF-8
Lean
false
false
15,283
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes Hölzl Ordered monoids and groups. -/ import algebra.group tactic universe u variable {α : Type u} section old_structure_cmd set_option old_structure_cmd true /-- An ordered (additive) commutative monoid is a commutative monoid with a partial order such that addition is an order embedding, i.e. `a + b ≤ a + c ↔ b ≤ c`. These monoids are automatically cancellative. -/ class ordered_comm_monoid (α : Type*) extends add_comm_monoid α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) (lt_of_add_lt_add_left : ∀ a b c : α, a + b < a + c → b < c) /-- A canonically ordered monoid is an ordered commutative monoid in which the ordering coincides with the divisibility relation, which is to say, `a ≤ b` iff there exists `c` with `b = a + c`. This is satisfied by the natural numbers, for example, but not the integers or other ordered groups. -/ class canonically_ordered_monoid (α : Type*) extends ordered_comm_monoid α := (le_iff_exists_add : ∀a b:α, a ≤ b ↔ ∃c, b = a + c) end old_structure_cmd section ordered_comm_monoid variables [ordered_comm_monoid α] {a b c d : α} lemma add_le_add_left' (h : a ≤ b) : c + a ≤ c + b := ordered_comm_monoid.add_le_add_left a b h c lemma add_le_add_right' (h : a ≤ b) : a + c ≤ b + c := add_comm c a ▸ add_comm c b ▸ add_le_add_left' h lemma lt_of_add_lt_add_left' : a + b < a + c → b < c := ordered_comm_monoid.lt_of_add_lt_add_left a b c lemma add_le_add' (h₁ : a ≤ b) (h₂ : c ≤ d) : a + c ≤ b + d := le_trans (add_le_add_right' h₁) (add_le_add_left' h₂) lemma le_add_of_nonneg_right' (h : b ≥ 0) : a ≤ a + b := have a + b ≥ a + 0, from add_le_add_left' h, by rwa add_zero at this lemma le_add_of_nonneg_left' (h : b ≥ 0) : a ≤ b + a := have 0 + a ≤ b + a, from add_le_add_right' h, by rwa zero_add at this lemma lt_of_add_lt_add_right' (h : a + b < c + b) : a < c := lt_of_add_lt_add_left' (show b + a < b + c, begin rw [add_comm b a, add_comm b c], assumption end) -- here we start using properties of zero. lemma le_add_of_nonneg_of_le' (ha : 0 ≤ a) (hbc : b ≤ c) : b ≤ a + c := zero_add b ▸ add_le_add' ha hbc lemma le_add_of_le_of_nonneg' (hbc : b ≤ c) (ha : 0 ≤ a) : b ≤ c + a := add_zero b ▸ add_le_add' hbc ha lemma add_nonneg' (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b := le_add_of_nonneg_of_le' ha hb lemma add_pos_of_pos_of_nonneg' (ha : 0 < a) (hb : 0 ≤ b) : 0 < a + b := lt_of_lt_of_le ha $ le_add_of_nonneg_right' hb lemma add_pos' (ha : 0 < a) (hb : 0 < b) : 0 < a + b := add_pos_of_pos_of_nonneg' ha $ le_of_lt hb lemma add_pos_of_nonneg_of_pos' (ha : 0 ≤ a) (hb : 0 < b) : 0 < a + b := lt_of_lt_of_le hb $ le_add_of_nonneg_left' ha lemma add_nonpos' (ha : a ≤ 0) (hb : b ≤ 0) : a + b ≤ 0 := zero_add (0:α) ▸ (add_le_add' ha hb) lemma add_le_of_nonpos_of_le' (ha : a ≤ 0) (hbc : b ≤ c) : a + b ≤ c := zero_add c ▸ add_le_add' ha hbc lemma add_le_of_le_of_nonpos' (hbc : b ≤ c) (ha : a ≤ 0) : b + a ≤ c := add_zero c ▸ add_le_add' hbc ha lemma add_neg_of_neg_of_nonpos' (ha : a < 0) (hb : b ≤ 0) : a + b < 0 := lt_of_le_of_lt (add_le_of_le_of_nonpos' (le_refl _) hb) ha lemma add_neg_of_nonpos_of_neg' (ha : a ≤ 0) (hb : b < 0) : a + b < 0 := lt_of_le_of_lt (add_le_of_nonpos_of_le' ha (le_refl _)) hb lemma add_neg' (ha : a < 0) (hb : b < 0) : a + b < 0 := add_neg_of_nonpos_of_neg' (le_of_lt ha) hb lemma lt_add_of_nonneg_of_lt' (ha : 0 ≤ a) (hbc : b < c) : b < a + c := lt_of_lt_of_le hbc $ le_add_of_nonneg_left' ha lemma lt_add_of_lt_of_nonneg' (hbc : b < c) (ha : 0 ≤ a) : b < c + a := lt_of_lt_of_le hbc $ le_add_of_nonneg_right' ha lemma lt_add_of_pos_of_lt' (ha : 0 < a) (hbc : b < c) : b < a + c := lt_add_of_nonneg_of_lt' (le_of_lt ha) hbc lemma lt_add_of_lt_of_pos' (hbc : b < c) (ha : 0 < a) : b < c + a := lt_add_of_lt_of_nonneg' hbc (le_of_lt ha) lemma add_lt_of_nonpos_of_lt' (ha : a ≤ 0) (hbc : b < c) : a + b < c := lt_of_le_of_lt (add_le_of_nonpos_of_le' ha (le_refl _)) hbc lemma add_lt_of_lt_of_nonpos' (hbc : b < c) (ha : a ≤ 0) : b + a < c := lt_of_le_of_lt (add_le_of_le_of_nonpos' (le_refl _) ha) hbc lemma add_lt_of_neg_of_lt' (ha : a < 0) (hbc : b < c) : a + b < c := add_lt_of_nonpos_of_lt' (le_of_lt ha) hbc lemma add_lt_of_lt_of_neg' (hbc : b < c) (ha : a < 0) : b + a < c := add_lt_of_lt_of_nonpos' hbc (le_of_lt ha) lemma add_eq_zero_iff_eq_zero_and_eq_zero_of_nonneg_of_nonneg' (ha : 0 ≤ a) (hb : 0 ≤ b) : a + b = 0 ↔ a = 0 ∧ b = 0 := iff.intro (assume hab : a + b = 0, have a ≤ 0, from hab ▸ le_add_of_le_of_nonneg' (le_refl _) hb, have a = 0, from le_antisymm this ha, have b ≤ 0, from hab ▸ le_add_of_nonneg_of_le' ha (le_refl _), have b = 0, from le_antisymm this hb, and.intro ‹a = 0› ‹b = 0›) (assume ⟨ha', hb'⟩, by rw [ha', hb', add_zero]) end ordered_comm_monoid section canonically_ordered_monoid variables [canonically_ordered_monoid α] {a b c d : α} lemma le_iff_exists_add : a ≤ b ↔ ∃c, b = a + c := canonically_ordered_monoid.le_iff_exists_add a b @[simp] lemma zero_le (a : α) : 0 ≤ a := le_iff_exists_add.mpr ⟨a, by simp⟩ @[simp] lemma add_eq_zero_iff : a + b = 0 ↔ a = 0 ∧ b = 0 := add_eq_zero_iff_eq_zero_and_eq_zero_of_nonneg_of_nonneg' (zero_le _) (zero_le _) end canonically_ordered_monoid instance ordered_cancel_comm_monoid.to_ordered_comm_monoid [H : ordered_cancel_comm_monoid α] : ordered_comm_monoid α := { lt_of_add_lt_add_left := @lt_of_add_lt_add_left _ _, ..H } section ordered_cancel_comm_monoid variables [ordered_cancel_comm_monoid α] {a b c : α} @[simp] lemma add_le_add_iff_left (a : α) {b c : α} : a + b ≤ a + c ↔ b ≤ c := ⟨le_of_add_le_add_left, λ h, add_le_add_left h _⟩ @[simp] lemma add_le_add_iff_right (c : α) : a + c ≤ b + c ↔ a ≤ b := add_comm c a ▸ add_comm c b ▸ add_le_add_iff_left c @[simp] lemma add_lt_add_iff_left (a : α) {b c : α} : a + b < a + c ↔ b < c := ⟨lt_of_add_lt_add_left, λ h, add_lt_add_left h _⟩ @[simp] lemma add_lt_add_iff_right (c : α) : a + c < b + c ↔ a < b := add_comm c a ▸ add_comm c b ▸ add_lt_add_iff_left c @[simp] lemma le_add_iff_nonneg_right (a : α) {b : α} : a ≤ a + b ↔ 0 ≤ b := have a + 0 ≤ a + b ↔ 0 ≤ b, from add_le_add_iff_left a, by rwa add_zero at this @[simp] lemma le_add_iff_nonneg_left (a : α) {b : α} : a ≤ b + a ↔ 0 ≤ b := by rw [add_comm, le_add_iff_nonneg_right] @[simp] lemma lt_add_iff_pos_right (a : α) {b : α} : a < a + b ↔ 0 < b := have a + 0 < a + b ↔ 0 < b, from add_lt_add_iff_left a, by rwa add_zero at this @[simp] lemma lt_add_iff_pos_left (a : α) {b : α} : a < b + a ↔ 0 < b := by rw [add_comm, lt_add_iff_pos_right] lemma add_eq_zero_iff_eq_zero_of_nonneg (ha : 0 ≤ a) (hb : 0 ≤ b) : a + b = 0 ↔ a = 0 ∧ b = 0 := ⟨λ hab : a + b = 0, by split; apply le_antisymm; try {assumption}; rw ← hab; simp [ha, hb], λ ⟨ha', hb'⟩, by rw [ha', hb', add_zero]⟩ lemma bit0_pos {a : α} (h : 0 < a) : 0 < bit0 a := add_pos h h end ordered_cancel_comm_monoid section ordered_comm_group variables [ordered_comm_group α] {a b c : α} @[simp] lemma neg_le_neg_iff : -a ≤ -b ↔ b ≤ a := have a + b + -a ≤ a + b + -b ↔ -a ≤ -b, from add_le_add_iff_left _, by simp at this; simp [this] lemma neg_le : -a ≤ b ↔ -b ≤ a := have -a ≤ -(-b) ↔ -b ≤ a, from neg_le_neg_iff, by rwa neg_neg at this lemma le_neg : a ≤ -b ↔ b ≤ -a := have -(-a) ≤ -b ↔ b ≤ -a, from neg_le_neg_iff, by rwa neg_neg at this @[simp] lemma neg_nonpos : -a ≤ 0 ↔ 0 ≤ a := have -a ≤ -0 ↔ 0 ≤ a, from neg_le_neg_iff, by rwa neg_zero at this @[simp] lemma neg_nonneg : 0 ≤ -a ↔ a ≤ 0 := have -0 ≤ -a ↔ a ≤ 0, from neg_le_neg_iff, by rwa neg_zero at this @[simp] lemma neg_lt_neg_iff : -a < -b ↔ b < a := have a + b + -a < a + b + -b ↔ -a < -b, from add_lt_add_iff_left _, by simp at this; simp [this] lemma neg_lt_zero : -a < 0 ↔ 0 < a := have -a < -0 ↔ 0 < a, from neg_lt_neg_iff, by rwa neg_zero at this lemma neg_pos : 0 < -a ↔ a < 0 := have -0 < -a ↔ a < 0, from neg_lt_neg_iff, by rwa neg_zero at this lemma neg_lt : -a < b ↔ -b < a := have -a < -(-b) ↔ -b < a, from neg_lt_neg_iff, by rwa neg_neg at this lemma lt_neg : a < -b ↔ b < -a := have -(-a) < -b ↔ b < -a, from neg_lt_neg_iff, by rwa neg_neg at this lemma sub_le_sub_iff_left (a : α) {b c : α} : a - b ≤ a - c ↔ c ≤ b := (add_le_add_iff_left _).trans neg_le_neg_iff lemma sub_le_sub_iff_right (c : α) : a - c ≤ b - c ↔ a ≤ b := add_le_add_iff_right _ lemma sub_lt_sub_iff_left (a : α) {b c : α} : a - b < a - c ↔ c < b := (add_lt_add_iff_left _).trans neg_lt_neg_iff lemma sub_lt_sub_iff_right (c : α) : a - c < b - c ↔ a < b := add_lt_add_iff_right _ lemma sub_lt_iff {a b c : α} : (a - b < c) ↔ (a < c + b) := iff.intro lt_add_of_sub_right_lt (assume h, have a + - b < (c + b) + - b, from add_lt_add_right h _, by simp * at *) lemma lt_sub_iff {a b c : α} : (a < b - c) ↔ (a + c < b) := iff.intro (assume h, have a + c < (b - c) + c, from add_lt_add_right h _, by simp * at *) lt_sub_right_of_add_lt @[simp] lemma sub_nonneg : 0 ≤ a - b ↔ b ≤ a := have a - a ≤ a - b ↔ b ≤ a, from sub_le_sub_iff_left a, by rwa sub_self at this @[simp] lemma sub_nonpos : a - b ≤ 0 ↔ a ≤ b := have a - b ≤ b - b ↔ a ≤ b, from sub_le_sub_iff_right b, by rwa sub_self at this @[simp] lemma sub_pos : 0 < a - b ↔ b < a := have a - a < a - b ↔ b < a, from sub_lt_sub_iff_left a, by rwa sub_self at this @[simp] lemma sub_lt_zero : a - b < 0 ↔ a < b := have a - b < b - b ↔ a < b, from sub_lt_sub_iff_right b, by rwa sub_self at this lemma le_neg_add_iff_add_le : b ≤ -a + c ↔ a + b ≤ c := have -a + (a + b) ≤ -a + c ↔ a + b ≤ c, from add_le_add_iff_left _, by rwa neg_add_cancel_left at this lemma le_sub_iff_add_le' : b ≤ c - a ↔ a + b ≤ c := by rw [sub_eq_add_neg, add_comm, le_neg_add_iff_add_le] lemma le_sub_iff_add_le : a ≤ c - b ↔ a + b ≤ c := by rw [le_sub_iff_add_le', add_comm] @[simp] lemma neg_add_le_iff_le_add : -b + a ≤ c ↔ a ≤ b + c := have -b + a ≤ -b + (b + c) ↔ a ≤ b + c, from add_le_add_iff_left _, by rwa neg_add_cancel_left at this lemma sub_le_iff_le_add' : a - b ≤ c ↔ a ≤ b + c := by rw [sub_eq_add_neg, add_comm, neg_add_le_iff_le_add] lemma sub_le_iff_le_add : a - c ≤ b ↔ a ≤ b + c := by rw [sub_le_iff_le_add', add_comm] @[simp] lemma add_neg_le_iff_le_add : a + -c ≤ b ↔ a ≤ b + c := sub_le_iff_le_add @[simp] lemma add_neg_le_iff_le_add' : a + -b ≤ c ↔ a ≤ b + c := sub_le_iff_le_add' lemma neg_add_le_iff_le_add' : -c + a ≤ b ↔ a ≤ b + c := by rw [neg_add_le_iff_le_add, add_comm] @[simp] lemma neg_le_sub_iff_le_add : -b ≤ a - c ↔ c ≤ a + b := le_sub_iff_add_le.trans neg_add_le_iff_le_add' lemma neg_le_sub_iff_le_add' : -a ≤ b - c ↔ c ≤ a + b := by rw [neg_le_sub_iff_le_add, add_comm] lemma sub_le : a - b ≤ c ↔ a - c ≤ b := sub_le_iff_le_add'.trans sub_le_iff_le_add.symm theorem le_sub : a ≤ b - c ↔ c ≤ b - a := le_sub_iff_add_le'.trans le_sub_iff_add_le.symm @[simp] lemma lt_neg_add_iff_add_lt : b < -a + c ↔ a + b < c := have -a + (a + b) < -a + c ↔ a + b < c, from add_lt_add_iff_left _, by rwa neg_add_cancel_left at this lemma lt_sub_iff_add_lt' : b < c - a ↔ a + b < c := by rw [sub_eq_add_neg, add_comm, lt_neg_add_iff_add_lt] lemma lt_sub_iff_add_lt : a < c - b ↔ a + b < c := by rw [lt_sub_iff_add_lt', add_comm] @[simp] lemma neg_add_lt_iff_lt_add : -b + a < c ↔ a < b + c := have -b + a < -b + (b + c) ↔ a < b + c, from add_lt_add_iff_left _, by rwa neg_add_cancel_left at this lemma sub_lt_iff_lt_add' : a - b < c ↔ a < b + c := by rw [sub_eq_add_neg, add_comm, neg_add_lt_iff_lt_add] lemma sub_lt_iff_lt_add : a - c < b ↔ a < b + c := by rw [sub_lt_iff_lt_add', add_comm] lemma neg_add_lt_iff_lt_add_right : -c + a < b ↔ a < b + c := by rw [neg_add_lt_iff_lt_add, add_comm] @[simp] lemma neg_lt_sub_iff_lt_add : -b < a - c ↔ c < a + b := lt_sub_iff_add_lt.trans neg_add_lt_iff_lt_add_right lemma neg_lt_sub_iff_lt_add' : -a < b - c ↔ c < a + b := by rw [neg_lt_sub_iff_lt_add, add_comm] lemma sub_lt : a - b < c ↔ a - c < b := sub_lt_iff_lt_add'.trans sub_lt_iff_lt_add.symm lemma sub_le_self_iff (a : α) {b : α} : a - b ≤ a ↔ 0 ≤ b := sub_le_iff_le_add'.trans (le_add_iff_nonneg_left _) lemma sub_lt_self_iff (a : α) {b : α} : a - b < a ↔ 0 < b := sub_lt_iff_lt_add'.trans (lt_add_iff_pos_left _) end ordered_comm_group set_option old_structure_cmd true /-- This is not so much a new structure as a construction mechanism for ordered groups, by specifying only the "positive cone" of the group. -/ class nonneg_comm_group (α : Type*) extends add_comm_group α := (nonneg : α → Prop) (pos : α → Prop := λ a, nonneg a ∧ ¬ nonneg (neg a)) (pos_iff : ∀ a, pos a ↔ nonneg a ∧ ¬ nonneg (-a) . order_laws_tac) (zero_nonneg : nonneg 0) (add_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a + b)) (nonneg_antisymm : ∀ {a}, nonneg a → nonneg (-a) → a = 0) namespace nonneg_comm_group variable [s : nonneg_comm_group α] include s @[reducible] instance to_ordered_comm_group : ordered_comm_group α := { le := λ a b, nonneg (b - a), lt := λ a b, pos (b - a), lt_iff_le_not_le := λ a b, by simp; rw [pos_iff]; simp, le_refl := λ a, by simp [zero_nonneg], le_trans := λ a b c nab nbc, by simp [-sub_eq_add_neg]; rw ← sub_add_sub_cancel; exact add_nonneg nbc nab, le_antisymm := λ a b nab nba, eq_of_sub_eq_zero $ nonneg_antisymm nba (by rw neg_sub; exact nab), add_le_add_left := λ a b nab c, by simpa [(≤), preorder.le] using nab, add_lt_add_left := λ a b nab c, by simpa [(<), preorder.lt] using nab, ..s } theorem nonneg_def {a : α} : nonneg a ↔ 0 ≤ a := show _ ↔ nonneg _, by simp theorem pos_def {a : α} : pos a ↔ 0 < a := show _ ↔ pos _, by simp theorem not_zero_pos : ¬ pos (0 : α) := mt pos_def.1 (lt_irrefl _) theorem zero_lt_iff_nonneg_nonneg {a : α} : 0 < a ↔ nonneg a ∧ ¬ nonneg (-a) := pos_def.symm.trans (pos_iff α _) theorem nonneg_total_iff : (∀ a : α, nonneg a ∨ nonneg (-a)) ↔ (∀ a b : α, a ≤ b ∨ b ≤ a) := ⟨λ h a b, by have := h (b - a); rwa [neg_sub] at this, λ h a, by rw [nonneg_def, nonneg_def, neg_nonneg]; apply h⟩ def to_decidable_linear_ordered_comm_group [decidable_pred (@nonneg α _)] (nonneg_total : ∀ a : α, nonneg a ∨ nonneg (-a)) : decidable_linear_ordered_comm_group α := { le := (≤), lt := (<), lt_iff_le_not_le := @lt_iff_le_not_le _ _, le_refl := @le_refl _ _, le_trans := @le_trans _ _, le_antisymm := @le_antisymm _ _, le_total := nonneg_total_iff.1 nonneg_total, decidable_le := by apply_instance, decidable_eq := by apply_instance, decidable_lt := by apply_instance, ..@nonneg_comm_group.to_ordered_comm_group _ s } end nonneg_comm_group
d4dd578985b42f98b45f5b6b0be5f5b9ffe556ed
a047a4718edfa935d17231e9e6ecec8c7b701e05
/src/data/set/lattice.lean
24e889c0508e6fe15414e5f3b0daf362d05c1eea
[ "Apache-2.0" ]
permissive
utensil-contrib/mathlib
bae0c9fafe5e2bdb516efc89d6f8c1502ecc9767
b91909e77e219098a2f8cc031f89d595fe274bd2
refs/heads/master
1,668,048,976,965
1,592,442,701,000
1,592,442,701,000
273,197,855
0
0
null
1,592,472,812,000
1,592,472,811,000
null
UTF-8
Lean
false
false
35,235
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Johannes Hölzl, Mario Carneiro -- QUESTION: can make the first argument in ∀ x ∈ a, ... implicit? -/ import order.complete_boolean_algebra import data.sigma.basic import order.galois_connection open function tactic set auto universes u v w x y variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {ι' : Sort y} namespace set instance lattice_set : complete_lattice (set α) := { le := (⊆), lt := (⊂), sup := (∪), inf := (∩), top := univ, bot := ∅, Sup := λs, {a | ∃ t ∈ s, a ∈ t }, Inf := λs, {a | ∀ t ∈ s, a ∈ t }, le_Sup := assume s t t_in a a_in, ⟨t, ⟨t_in, a_in⟩⟩, Sup_le := assume s t h a ⟨t', ⟨t'_in, a_in⟩⟩, h t' t'_in a_in, le_Inf := assume s t h a a_in t' t'_in, h t' t'_in a_in, Inf_le := assume s t t_in a h, h _ t_in, .. (infer_instance : complete_lattice (α → Prop)) } instance : distrib_lattice (set α) := { le_sup_inf := λ s t u x, or_and_distrib_left.2, ..set.lattice_set } /-- Image is monotone. See `set.image_image` for the statement in terms of `⊆`. -/ lemma monotone_image {f : α → β} : monotone (image f) := assume s t, assume h : s ⊆ t, image_subset _ h theorem monotone_inter [preorder β] {f g : β → set α} (hf : monotone f) (hg : monotone g) : monotone (λx, f x ∩ g x) := assume b₁ b₂ h, inter_subset_inter (hf h) (hg h) theorem monotone_union [preorder β] {f g : β → set α} (hf : monotone f) (hg : monotone g) : monotone (λx, f x ∪ g x) := assume b₁ b₂ h, union_subset_union (hf h) (hg h) theorem monotone_set_of [preorder α] {p : α → β → Prop} (hp : ∀b, monotone (λa, p a b)) : monotone (λa, {b | p a b}) := assume a a' h b, hp b h section galois_connection variables {f : α → β} protected lemma image_preimage : galois_connection (image f) (preimage f) := assume a b, image_subset_iff /-- `kern_image f s` is the set of `y` such that `f ⁻¹ y ⊆ s` -/ def kern_image (f : α → β) (s : set α) : set β := {y | ∀ ⦃x⦄, f x = y → x ∈ s} protected lemma preimage_kern_image : galois_connection (preimage f) (kern_image f) := assume a b, ⟨ assume h x hx y hy, have f y ∈ a, from hy.symm ▸ hx, h this, assume h x (hx : f x ∈ a), h hx rfl⟩ end galois_connection /- union and intersection over a family of sets indexed by a type -/ /-- Indexed union of a family of sets -/ @[reducible] def Union (s : ι → set β) : set β := supr s /-- Indexed intersection of a family of sets -/ @[reducible] def Inter (s : ι → set β) : set β := infi s notation `⋃` binders `, ` r:(scoped f, Union f) := r notation `⋂` binders `, ` r:(scoped f, Inter f) := r @[simp] theorem mem_Union {x : β} {s : ι → set β} : x ∈ Union s ↔ ∃ i, x ∈ s i := ⟨assume ⟨t, ⟨⟨a, (t_eq : s a = t)⟩, (h : x ∈ t)⟩⟩, ⟨a, t_eq.symm ▸ h⟩, assume ⟨a, h⟩, ⟨s a, ⟨⟨a, rfl⟩, h⟩⟩⟩ /- alternative proof: dsimp [Union, supr, Sup]; simp -/ -- TODO: more rewrite rules wrt forall / existentials and logical connectives -- TODO: also eliminate ∃i, ... ∧ i = t ∧ ... theorem set_of_exists (p : ι → β → Prop) : {x | ∃ i, p i x} = ⋃ i, {x | p i x} := ext $ λ i, mem_Union.symm @[simp] theorem mem_Inter {x : β} {s : ι → set β} : x ∈ Inter s ↔ ∀ i, x ∈ s i := ⟨assume (h : ∀a ∈ {a : set β | ∃i, s i = a}, x ∈ a) a, h (s a) ⟨a, rfl⟩, assume h t ⟨a, (eq : s a = t)⟩, eq ▸ h a⟩ theorem set_of_forall (p : ι → β → Prop) : {x | ∀ i, p i x} = ⋂ i, {x | p i x} := ext $ λ i, mem_Inter.symm theorem Union_subset {s : ι → set β} {t : set β} (h : ∀ i, s i ⊆ t) : (⋃ i, s i) ⊆ t := -- TODO: should be simpler when sets' order is based on lattices @supr_le (set β) _ set.lattice_set _ _ h theorem Union_subset_iff {s : ι → set β} {t : set β} : (⋃ i, s i) ⊆ t ↔ (∀ i, s i ⊆ t) := ⟨assume h i, subset.trans (le_supr s _) h, Union_subset⟩ theorem mem_Inter_of_mem {x : β} {s : ι → set β} : (∀ i, x ∈ s i) → (x ∈ ⋂ i, s i) := mem_Inter.2 theorem subset_Inter {t : set β} {s : ι → set β} (h : ∀ i, t ⊆ s i) : t ⊆ ⋂ i, s i := -- TODO: should be simpler when sets' order is based on lattices @le_infi (set β) _ set.lattice_set _ _ h theorem subset_Union : ∀ (s : ι → set β) (i : ι), s i ⊆ (⋃ i, s i) := le_supr -- This rather trivial consequence is convenient with `apply`, -- and has `i` explicit for this use case. theorem subset_subset_Union {A : set β} {s : ι → set β} (i : ι) (h : A ⊆ s i) : A ⊆ ⋃ (i : ι), s i := subset.trans h (subset_Union s i) theorem Inter_subset : ∀ (s : ι → set β) (i : ι), (⋂ i, s i) ⊆ s i := infi_le lemma Inter_subset_of_subset {s : ι → set α} {t : set α} (i : ι) (h : s i ⊆ t) : (⋂ i, s i) ⊆ t := set.subset.trans (set.Inter_subset s i) h lemma Inter_subset_Inter {s t : ι → set α} (h : ∀ i, s i ⊆ t i) : (⋂ i, s i) ⊆ (⋂ i, t i) := set.subset_Inter $ λ i, set.Inter_subset_of_subset i (h i) lemma Inter_subset_Inter2 {s : ι → set α} {t : ι' → set α} (h : ∀ j, ∃ i, s i ⊆ t j) : (⋂ i, s i) ⊆ (⋂ j, t j) := set.subset_Inter $ λ j, let ⟨i, hi⟩ := h j in Inter_subset_of_subset i hi theorem Union_const [nonempty ι] (s : set β) : (⋃ i:ι, s) = s := ext $ by simp theorem Inter_const [nonempty ι] (s : set β) : (⋂ i:ι, s) = s := ext $ by simp @[simp] -- complete_boolean_algebra theorem compl_Union (s : ι → set β) : - (⋃ i, s i) = (⋂ i, - s i) := ext (by simp) -- classical -- complete_boolean_algebra theorem compl_Inter (s : ι → set β) : -(⋂ i, s i) = (⋃ i, - s i) := ext (λ x, by simp [classical.not_forall]) -- classical -- complete_boolean_algebra theorem Union_eq_comp_Inter_comp (s : ι → set β) : (⋃ i, s i) = - (⋂ i, - s i) := by simp [compl_Inter, compl_compl] -- classical -- complete_boolean_algebra theorem Inter_eq_comp_Union_comp (s : ι → set β) : (⋂ i, s i) = - (⋃ i, -s i) := by simp [compl_compl] theorem inter_Union (s : set β) (t : ι → set β) : s ∩ (⋃ i, t i) = ⋃ i, s ∩ t i := ext $ by simp theorem Union_inter (s : set β) (t : ι → set β) : (⋃ i, t i) ∩ s = ⋃ i, t i ∩ s := ext $ by simp theorem Union_union_distrib (s : ι → set β) (t : ι → set β) : (⋃ i, s i ∪ t i) = (⋃ i, s i) ∪ (⋃ i, t i) := ext $ by simp [exists_or_distrib] theorem Inter_inter_distrib (s : ι → set β) (t : ι → set β) : (⋂ i, s i ∩ t i) = (⋂ i, s i) ∩ (⋂ i, t i) := ext $ by simp [forall_and_distrib] theorem union_Union [nonempty ι] (s : set β) (t : ι → set β) : s ∪ (⋃ i, t i) = ⋃ i, s ∪ t i := by rw [Union_union_distrib, Union_const] theorem Union_union [nonempty ι] (s : set β) (t : ι → set β) : (⋃ i, t i) ∪ s = ⋃ i, t i ∪ s := by rw [Union_union_distrib, Union_const] theorem inter_Inter [nonempty ι] (s : set β) (t : ι → set β) : s ∩ (⋂ i, t i) = ⋂ i, s ∩ t i := by rw [Inter_inter_distrib, Inter_const] theorem Inter_inter [nonempty ι] (s : set β) (t : ι → set β) : (⋂ i, t i) ∩ s = ⋂ i, t i ∩ s := by rw [Inter_inter_distrib, Inter_const] -- classical theorem union_Inter (s : set β) (t : ι → set β) : s ∪ (⋂ i, t i) = ⋂ i, s ∪ t i := ext $ assume x, by simp [classical.forall_or_distrib_left] theorem Union_diff (s : set β) (t : ι → set β) : (⋃ i, t i) \ s = ⋃ i, t i \ s := Union_inter _ _ theorem diff_Union [nonempty ι] (s : set β) (t : ι → set β) : s \ (⋃ i, t i) = ⋂ i, s \ t i := by rw [diff_eq, compl_Union, inter_Inter]; refl theorem diff_Inter (s : set β) (t : ι → set β) : s \ (⋂ i, t i) = ⋃ i, s \ t i := by rw [diff_eq, compl_Inter, inter_Union]; refl lemma directed_on_Union {r} {ι : Sort v} {f : ι → set α} (hd : directed (⊆) f) (h : ∀x, directed_on r (f x)) : directed_on r (⋃x, f x) := by simp only [directed_on, exists_prop, mem_Union, exists_imp_distrib]; exact assume a₁ b₁ fb₁ a₂ b₂ fb₂, let ⟨z, zb₁, zb₂⟩ := hd b₁ b₂, ⟨x, xf, xa₁, xa₂⟩ := h z a₁ (zb₁ fb₁) a₂ (zb₂ fb₂) in ⟨x, ⟨z, xf⟩, xa₁, xa₂⟩ /- bounded unions and intersections -/ theorem mem_bUnion_iff {s : set α} {t : α → set β} {y : β} : y ∈ (⋃ x ∈ s, t x) ↔ ∃ x ∈ s, y ∈ t x := by simp theorem mem_bInter_iff {s : set α} {t : α → set β} {y : β} : y ∈ (⋂ x ∈ s, t x) ↔ ∀ x ∈ s, y ∈ t x := by simp theorem mem_bUnion {s : set α} {t : α → set β} {x : α} {y : β} (xs : x ∈ s) (ytx : y ∈ t x) : y ∈ ⋃ x ∈ s, t x := by simp; exact ⟨x, ⟨xs, ytx⟩⟩ theorem mem_bInter {s : set α} {t : α → set β} {y : β} (h : ∀ x ∈ s, y ∈ t x) : y ∈ ⋂ x ∈ s, t x := by simp; assumption theorem bUnion_subset {s : set α} {t : set β} {u : α → set β} (h : ∀ x ∈ s, u x ⊆ t) : (⋃ x ∈ s, u x) ⊆ t := show (⨆ x ∈ s, u x) ≤ t, -- TODO: should not be necessary when sets' order is based on lattices from supr_le $ assume x, supr_le (h x) theorem subset_bInter {s : set α} {t : set β} {u : α → set β} (h : ∀ x ∈ s, t ⊆ u x) : t ⊆ (⋂ x ∈ s, u x) := subset_Inter $ assume x, subset_Inter $ h x theorem subset_bUnion_of_mem {s : set α} {u : α → set β} {x : α} (xs : x ∈ s) : u x ⊆ (⋃ x ∈ s, u x) := show u x ≤ (⨆ x ∈ s, u x), from le_supr_of_le x $ le_supr _ xs theorem bInter_subset_of_mem {s : set α} {t : α → set β} {x : α} (xs : x ∈ s) : (⋂ x ∈ s, t x) ⊆ t x := show (⨅x ∈ s, t x) ≤ t x, from infi_le_of_le x $ infi_le _ xs theorem bUnion_subset_bUnion_left {s s' : set α} {t : α → set β} (h : s ⊆ s') : (⋃ x ∈ s, t x) ⊆ (⋃ x ∈ s', t x) := bUnion_subset (λ x xs, subset_bUnion_of_mem (h xs)) theorem bInter_subset_bInter_left {s s' : set α} {t : α → set β} (h : s' ⊆ s) : (⋂ x ∈ s, t x) ⊆ (⋂ x ∈ s', t x) := subset_bInter (λ x xs, bInter_subset_of_mem (h xs)) theorem bUnion_subset_bUnion_right {s : set α} {t1 t2 : α → set β} (h : ∀ x ∈ s, t1 x ⊆ t2 x) : (⋃ x ∈ s, t1 x) ⊆ (⋃ x ∈ s, t2 x) := bUnion_subset (λ x xs, subset.trans (h x xs) (subset_bUnion_of_mem xs)) theorem bInter_subset_bInter_right {s : set α} {t1 t2 : α → set β} (h : ∀ x ∈ s, t1 x ⊆ t2 x) : (⋂ x ∈ s, t1 x) ⊆ (⋂ x ∈ s, t2 x) := subset_bInter (λ x xs, subset.trans (bInter_subset_of_mem xs) (h x xs)) theorem bUnion_subset_bUnion {γ : Type*} {s : set α} {t : α → set β} {s' : set γ} {t' : γ → set β} (h : ∀ x ∈ s, ∃ y ∈ s', t x ⊆ t' y) : (⋃ x ∈ s, t x) ⊆ (⋃ y ∈ s', t' y) := begin intros x, simp only [mem_Union], rintros ⟨a, a_in, ha⟩, rcases h a a_in with ⟨c, c_in, hc⟩, exact ⟨c, c_in, hc ha⟩ end theorem bUnion_mono {s : set α} {t t' : α → set β} (h : ∀ x ∈ s, t x ⊆ t' x) : (⋃ x ∈ s, t x) ⊆ (⋃ x ∈ s, t' x) := bUnion_subset_bUnion (λ x x_in, ⟨x, x_in, h x x_in⟩) theorem bUnion_eq_Union (s : set α) (t : α → set β) : (⋃ x ∈ s, t x) = (⋃ x : s, t x.1) := set.ext $ by simp theorem bInter_eq_Inter (s : set α) (t : α → set β) : (⋂ x ∈ s, t x) = (⋂ x : s, t x.1) := set.ext $ by simp theorem bInter_empty (u : α → set β) : (⋂ x ∈ (∅ : set α), u x) = univ := show (⨅x ∈ (∅ : set α), u x) = ⊤, -- simplifier should be able to rewrite x ∈ ∅ to false. from infi_emptyset theorem bInter_univ (u : α → set β) : (⋂ x ∈ @univ α, u x) = ⋂ x, u x := infi_univ -- TODO(Jeremy): here is an artifact of the the encoding of bounded intersection: -- without dsimp, the next theorem fails to type check, because there is a lambda -- in a type that needs to be contracted. Using simp [eq_of_mem_singleton xa] also works. @[simp] theorem bInter_singleton (a : α) (s : α → set β) : (⋂ x ∈ ({a} : set α), s x) = s a := show (⨅ x ∈ ({a} : set α), s x) = s a, by simp theorem bInter_union (s t : set α) (u : α → set β) : (⋂ x ∈ s ∪ t, u x) = (⋂ x ∈ s, u x) ∩ (⋂ x ∈ t, u x) := show (⨅ x ∈ s ∪ t, u x) = (⨅ x ∈ s, u x) ⊓ (⨅ x ∈ t, u x), from infi_union -- TODO(Jeremy): simp [insert_eq, bInter_union] doesn't work @[simp] theorem bInter_insert (a : α) (s : set α) (t : α → set β) : (⋂ x ∈ insert a s, t x) = t a ∩ (⋂ x ∈ s, t x) := begin rw insert_eq, simp [bInter_union] end -- TODO(Jeremy): another example of where an annotation is needed theorem bInter_pair (a b : α) (s : α → set β) : (⋂ x ∈ ({a, b} : set α), s x) = s a ∩ s b := by simp [inter_comm] theorem bUnion_empty (s : α → set β) : (⋃ x ∈ (∅ : set α), s x) = ∅ := supr_emptyset theorem bUnion_univ (s : α → set β) : (⋃ x ∈ @univ α, s x) = ⋃ x, s x := supr_univ @[simp] theorem bUnion_singleton (a : α) (s : α → set β) : (⋃ x ∈ ({a} : set α), s x) = s a := supr_singleton @[simp] theorem bUnion_of_singleton (s : set α) : (⋃ x ∈ s, {x}) = s := ext $ by simp theorem bUnion_union (s t : set α) (u : α → set β) : (⋃ x ∈ s ∪ t, u x) = (⋃ x ∈ s, u x) ∪ (⋃ x ∈ t, u x) := supr_union -- TODO(Jeremy): once again, simp doesn't do it alone. @[simp] theorem bUnion_insert (a : α) (s : set α) (t : α → set β) : (⋃ x ∈ insert a s, t x) = t a ∪ (⋃ x ∈ s, t x) := begin rw [insert_eq], simp [bUnion_union] end theorem bUnion_pair (a b : α) (s : α → set β) : (⋃ x ∈ ({a, b} : set α), s x) = s a ∪ s b := by simp [union_comm] @[simp] -- complete_boolean_algebra theorem compl_bUnion (s : set α) (t : α → set β) : - (⋃ i ∈ s, t i) = (⋂ i ∈ s, - t i) := ext (λ x, by simp) -- classical -- complete_boolean_algebra theorem compl_bInter (s : set α) (t : α → set β) : -(⋂ i ∈ s, t i) = (⋃ i ∈ s, - t i) := ext (λ x, by simp [classical.not_forall]) theorem inter_bUnion (s : set α) (t : α → set β) (u : set β) : u ∩ (⋃ i ∈ s, t i) = ⋃ i ∈ s, u ∩ t i := begin ext x, simp only [exists_prop, mem_Union, mem_inter_eq], exact ⟨λ ⟨hx, ⟨i, is, xi⟩⟩, ⟨i, is, hx, xi⟩, λ ⟨i, is, hx, xi⟩, ⟨hx, ⟨i, is, xi⟩⟩⟩ end theorem bUnion_inter (s : set α) (t : α → set β) (u : set β) : (⋃ i ∈ s, t i) ∩ u = (⋃ i ∈ s, t i ∩ u) := by simp [@inter_comm _ _ u, inter_bUnion] /-- Intersection of a set of sets. -/ @[reducible] def sInter (S : set (set α)) : set α := Inf S prefix `⋂₀`:110 := sInter theorem mem_sUnion_of_mem {x : α} {t : set α} {S : set (set α)} (hx : x ∈ t) (ht : t ∈ S) : x ∈ ⋃₀ S := ⟨t, ⟨ht, hx⟩⟩ theorem mem_sUnion {x : α} {S : set (set α)} : x ∈ ⋃₀ S ↔ ∃t ∈ S, x ∈ t := iff.rfl -- is this theorem really necessary? theorem not_mem_of_not_mem_sUnion {x : α} {t : set α} {S : set (set α)} (hx : x ∉ ⋃₀ S) (ht : t ∈ S) : x ∉ t := λ h, hx ⟨t, ht, h⟩ @[simp] theorem mem_sInter {x : α} {S : set (set α)} : x ∈ ⋂₀ S ↔ ∀ t ∈ S, x ∈ t := iff.rfl theorem sInter_subset_of_mem {S : set (set α)} {t : set α} (tS : t ∈ S) : ⋂₀ S ⊆ t := Inf_le tS theorem subset_sUnion_of_mem {S : set (set α)} {t : set α} (tS : t ∈ S) : t ⊆ ⋃₀ S := le_Sup tS lemma subset_sUnion_of_subset {s : set α} (t : set (set α)) (u : set α) (h₁ : s ⊆ u) (h₂ : u ∈ t) : s ⊆ ⋃₀ t := subset.trans h₁ (subset_sUnion_of_mem h₂) theorem sUnion_subset {S : set (set α)} {t : set α} (h : ∀t' ∈ S, t' ⊆ t) : (⋃₀ S) ⊆ t := Sup_le h theorem sUnion_subset_iff {s : set (set α)} {t : set α} : ⋃₀ s ⊆ t ↔ ∀t' ∈ s, t' ⊆ t := ⟨assume h t' ht', subset.trans (subset_sUnion_of_mem ht') h, sUnion_subset⟩ theorem subset_sInter {S : set (set α)} {t : set α} (h : ∀t' ∈ S, t ⊆ t') : t ⊆ (⋂₀ S) := le_Inf h theorem sUnion_subset_sUnion {S T : set (set α)} (h : S ⊆ T) : ⋃₀ S ⊆ ⋃₀ T := sUnion_subset $ λ s hs, subset_sUnion_of_mem (h hs) theorem sInter_subset_sInter {S T : set (set α)} (h : S ⊆ T) : ⋂₀ T ⊆ ⋂₀ S := subset_sInter $ λ s hs, sInter_subset_of_mem (h hs) @[simp] theorem sUnion_empty : ⋃₀ ∅ = (∅ : set α) := Sup_empty @[simp] theorem sInter_empty : ⋂₀ ∅ = (univ : set α) := Inf_empty @[simp] theorem sUnion_singleton (s : set α) : ⋃₀ {s} = s := Sup_singleton @[simp] theorem sInter_singleton (s : set α) : ⋂₀ {s} = s := Inf_singleton theorem sUnion_union (S T : set (set α)) : ⋃₀ (S ∪ T) = ⋃₀ S ∪ ⋃₀ T := Sup_union theorem sInter_union (S T : set (set α)) : ⋂₀ (S ∪ T) = ⋂₀ S ∩ ⋂₀ T := Inf_union theorem sInter_Union (s : ι → set (set α)) : ⋂₀ (⋃ i, s i) = ⋂ i, ⋂₀ s i := begin ext x, simp only [mem_Union, mem_Inter, mem_sInter, exists_imp_distrib], split ; tauto end @[simp] theorem sUnion_insert (s : set α) (T : set (set α)) : ⋃₀ (insert s T) = s ∪ ⋃₀ T := Sup_insert @[simp] theorem sInter_insert (s : set α) (T : set (set α)) : ⋂₀ (insert s T) = s ∩ ⋂₀ T := Inf_insert theorem sUnion_pair (s t : set α) : ⋃₀ {s, t} = s ∪ t := Sup_pair theorem sInter_pair (s t : set α) : ⋂₀ {s, t} = s ∩ t := Inf_pair @[simp] theorem sUnion_image (f : α → set β) (s : set α) : ⋃₀ (f '' s) = ⋃ x ∈ s, f x := Sup_image @[simp] theorem sInter_image (f : α → set β) (s : set α) : ⋂₀ (f '' s) = ⋂ x ∈ s, f x := Inf_image @[simp] theorem sUnion_range (f : ι → set β) : ⋃₀ (range f) = ⋃ x, f x := Sup_range @[simp] theorem sInter_range (f : ι → set β) : ⋂₀ (range f) = ⋂ x, f x := Inf_range lemma sUnion_eq_univ_iff {c : set (set α)} : ⋃₀ c = @set.univ α ↔ ∀ a, ∃ b ∈ c, a ∈ b := ⟨λ H a, let ⟨b, hm, hb⟩ := mem_sUnion.1 $ by rw H; exact mem_univ a in ⟨b, hm, hb⟩, λ H, set.univ_subset_iff.1 $ λ x hx, let ⟨b, hm, hb⟩ := H x in set.mem_sUnion_of_mem hb hm⟩ theorem compl_sUnion (S : set (set α)) : - ⋃₀ S = ⋂₀ (compl '' S) := set.ext $ assume x, ⟨assume : ¬ (∃s∈S, x ∈ s), assume s h, match s, h with ._, ⟨t, hs, rfl⟩ := assume h, this ⟨t, hs, h⟩ end, assume : ∀s, s ∈ compl '' S → x ∈ s, assume ⟨t, tS, xt⟩, this (compl t) (mem_image_of_mem _ tS) xt⟩ -- classical theorem sUnion_eq_compl_sInter_compl (S : set (set α)) : ⋃₀ S = - ⋂₀ (compl '' S) := by rw [←compl_compl (⋃₀ S), compl_sUnion] -- classical theorem compl_sInter (S : set (set α)) : - ⋂₀ S = ⋃₀ (compl '' S) := by rw [sUnion_eq_compl_sInter_compl, compl_compl_image] -- classical theorem sInter_eq_comp_sUnion_compl (S : set (set α)) : ⋂₀ S = -(⋃₀ (compl '' S)) := by rw [←compl_compl (⋂₀ S), compl_sInter] theorem inter_empty_of_inter_sUnion_empty {s t : set α} {S : set (set α)} (hs : t ∈ S) (h : s ∩ ⋃₀ S = ∅) : s ∩ t = ∅ := eq_empty_of_subset_empty $ by rw ← h; exact inter_subset_inter_right _ (subset_sUnion_of_mem hs) theorem range_sigma_eq_Union_range {γ : α → Type*} (f : sigma γ → β) : range f = ⋃ a, range (λ b, f ⟨a, b⟩) := set.ext $ by simp theorem Union_eq_range_sigma (s : α → set β) : (⋃ i, s i) = range (λ a : Σ i, s i, a.2) := by simp [set.ext_iff] theorem Union_image_preimage_sigma_mk_eq_self {ι : Type*} {σ : ι → Type*} (s : set (sigma σ)) : (⋃ i, sigma.mk i '' (sigma.mk i ⁻¹' s)) = s := begin ext x, simp only [mem_Union, mem_image, mem_preimage], split, { rintros ⟨i, a, h, rfl⟩, exact h }, { intro h, cases x with i a, exact ⟨i, a, h, rfl⟩ } end lemma sUnion_mono {s t : set (set α)} (h : s ⊆ t) : (⋃₀ s) ⊆ (⋃₀ t) := sUnion_subset $ assume t' ht', subset_sUnion_of_mem $ h ht' lemma Union_subset_Union {s t : ι → set α} (h : ∀i, s i ⊆ t i) : (⋃i, s i) ⊆ (⋃i, t i) := @supr_le_supr (set α) ι _ s t h lemma Union_subset_Union2 {ι₂ : Sort*} {s : ι → set α} {t : ι₂ → set α} (h : ∀i, ∃j, s i ⊆ t j) : (⋃i, s i) ⊆ (⋃i, t i) := @supr_le_supr2 (set α) ι ι₂ _ s t h lemma Union_subset_Union_const {ι₂ : Sort x} {s : set α} (h : ι → ι₂) : (⋃ i:ι, s) ⊆ (⋃ j:ι₂, s) := @supr_le_supr_const (set α) ι ι₂ _ s h @[simp] lemma Union_of_singleton (α : Type u) : (⋃(x : α), {x}) = @set.univ α := ext $ λ x, ⟨λ h, ⟨⟩, λ h, ⟨{x}, ⟨⟨x, rfl⟩, mem_singleton x⟩⟩⟩ theorem bUnion_subset_Union (s : set α) (t : α → set β) : (⋃ x ∈ s, t x) ⊆ (⋃ x, t x) := Union_subset_Union $ λ i, Union_subset $ λ h, by refl lemma sUnion_eq_bUnion {s : set (set α)} : (⋃₀ s) = (⋃ (i : set α) (h : i ∈ s), i) := set.ext $ by simp lemma sInter_eq_bInter {s : set (set α)} : (⋂₀ s) = (⋂ (i : set α) (h : i ∈ s), i) := set.ext $ by simp lemma sUnion_eq_Union {s : set (set α)} : (⋃₀ s) = (⋃ (i : s), i.1) := set.ext $ λ x, by simp lemma sInter_eq_Inter {s : set (set α)} : (⋂₀ s) = (⋂ (i : s), i.1) := set.ext $ λ x, by simp lemma union_eq_Union {s₁ s₂ : set α} : s₁ ∪ s₂ = ⋃ b : bool, cond b s₁ s₂ := set.ext $ λ x, by simp [bool.exists_bool, or_comm] lemma inter_eq_Inter {s₁ s₂ : set α} : s₁ ∩ s₂ = ⋂ b : bool, cond b s₁ s₂ := set.ext $ λ x, by simp [bool.forall_bool, and_comm] instance : complete_boolean_algebra (set α) := { neg := compl, sub := (\), le_sup_inf := distrib_lattice.le_sup_inf, inf_compl_eq_bot := assume s, ext $ assume x, ⟨assume ⟨h, nh⟩, nh h, false.elim⟩, sup_compl_eq_top := assume s, ext $ assume x, ⟨assume h, trivial, assume _, classical.em $ x ∈ s⟩, sub_eq := assume x y, rfl, infi_sup_le_sup_Inf := assume s t x, show x ∈ (⋂ b ∈ t, s ∪ b) → x ∈ s ∪ (⋂₀ t), by simp; exact assume h, or.imp_right (assume hn : x ∉ s, assume i hi, or.resolve_left (h i hi) hn) (classical.em $ x ∈ s), inf_Sup_le_supr_inf := assume s t x, show x ∈ s ∩ (⋃₀ t) → x ∈ (⋃ b ∈ t, s ∩ b), by simp [-and_imp, and.left_comm], ..set.lattice_set } lemma sInter_union_sInter {S T : set (set α)} : (⋂₀S) ∪ (⋂₀T) = (⋂p ∈ set.prod S T, (p : (set α) × (set α)).1 ∪ p.2) := Inf_sup_Inf lemma sUnion_inter_sUnion {s t : set (set α)} : (⋃₀s) ∩ (⋃₀t) = (⋃p ∈ set.prod s t, (p : (set α) × (set α )).1 ∩ p.2) := Sup_inf_Sup lemma sInter_bUnion {S : set (set α)} {T : set α → set (set α)} (hT : ∀s∈S, s = ⋂₀ T s) : ⋂₀ (⋃s∈S, T s) = ⋂₀ S := begin ext, simp only [and_imp, exists_prop, set.mem_sInter, set.mem_Union, exists_imp_distrib], split, { assume H s sS, rw [hT s sS, mem_sInter], assume t tTs, apply H t s sS tTs }, { assume H t s sS tTs, have xs : x ∈ s := H s sS, have : s ⊆ t, { have Z := hT s sS, rw sInter_eq_bInter at Z, rw Z, apply bInter_subset_of_mem, exact tTs }, exact this xs } end lemma sUnion_bUnion {S : set (set α)} {T : set α → set (set α)} (hT : ∀s∈S, s = ⋃₀ T s) : ⋃₀ (⋃s∈S, T s) = ⋃₀ S := begin ext, simp only [exists_prop, set.mem_Union, set.mem_set_of_eq], split, { rintros ⟨t, ⟨⟨s, ⟨sS, tTs⟩⟩, xt⟩⟩, refine ⟨s, ⟨sS, _⟩⟩, rw hT s sS, exact subset_sUnion_of_mem tTs xt }, { rintros ⟨s, ⟨sS, xs⟩⟩, rw hT s sS at xs, rcases mem_sUnion.1 xs with ⟨t, tTs, xt⟩, exact ⟨t, ⟨⟨s, ⟨sS, tTs⟩⟩, xt⟩⟩ } end lemma Union_range_eq_sUnion {α β : Type*} (C : set (set α)) {f : ∀(s : C), β → s} (hf : ∀(s : C), surjective (f s)) : (⋃(y : β), range (λ(s : C), (f s y).val)) = ⋃₀ C := begin ext x, split, { rintro ⟨s, ⟨y, rfl⟩, ⟨⟨s, hs⟩, rfl⟩⟩, refine ⟨_, hs, _⟩, exact (f ⟨s, hs⟩ y).2 }, { rintro ⟨s, hs, hx⟩, cases hf ⟨s, hs⟩ ⟨x, hx⟩ with y hy, refine ⟨_, ⟨y, rfl⟩, ⟨⟨s, hs⟩, _⟩⟩, exact congr_arg subtype.val hy } end lemma Union_range_eq_Union {ι α β : Type*} (C : ι → set α) {f : ∀(x : ι), β → C x} (hf : ∀(x : ι), surjective (f x)) : (⋃(y : β), range (λ(x : ι), (f x y).val)) = ⋃x, C x := begin ext x, rw [mem_Union, mem_Union], split, { rintro ⟨y, ⟨i, rfl⟩⟩, exact ⟨i, (f i y).2⟩ }, { rintro ⟨i, hx⟩, cases hf i ⟨x, hx⟩ with y hy, refine ⟨y, ⟨i, congr_arg subtype.val hy⟩⟩ } end @[simp] theorem sub_eq_diff (s t : set α) : s - t = s \ t := rfl section variables {p : Prop} {μ : p → set α} @[simp] lemma Inter_pos (hp : p) : (⋂h:p, μ h) = μ hp := infi_pos hp @[simp] lemma Inter_neg (hp : ¬ p) : (⋂h:p, μ h) = univ := infi_neg hp @[simp] lemma Union_pos (hp : p) : (⋃h:p, μ h) = μ hp := supr_pos hp @[simp] lemma Union_neg (hp : ¬ p) : (⋃h:p, μ h) = ∅ := supr_neg hp @[simp] lemma Union_empty {ι : Sort*} : (⋃i:ι, ∅:set α) = ∅ := supr_bot @[simp] lemma Inter_univ {ι : Sort*} : (⋂i:ι, univ:set α) = univ := infi_top end section image lemma image_Union {f : α → β} {s : ι → set α} : f '' (⋃ i, s i) = (⋃i, f '' s i) := begin apply set.ext, intro x, simp [image, exists_and_distrib_right.symm, -exists_and_distrib_right], exact exists_swap end lemma univ_subtype {p : α → Prop} : (univ : set (subtype p)) = (⋃x (h : p x), {⟨x, h⟩}) := set.ext $ assume ⟨x, h⟩, by simp [h] lemma range_eq_Union {ι} (f : ι → α) : range f = (⋃i, {f i}) := set.ext $ assume a, by simp [@eq_comm α a] lemma image_eq_Union (f : α → β) (s : set α) : f '' s = (⋃i∈s, {f i}) := set.ext $ assume b, by simp [@eq_comm β b] @[simp] lemma bUnion_range {f : ι → α} {g : α → set β} : (⋃x ∈ range f, g x) = (⋃y, g (f y)) := by rw [← sUnion_image, ← range_comp, sUnion_range] @[simp] lemma bInter_range {f : ι → α} {g : α → set β} : (⋂x ∈ range f, g x) = (⋂y, g (f y)) := by rw [← sInter_image, ← range_comp, sInter_range] variables {s : set γ} {f : γ → α} {g : α → set β} @[simp] lemma bUnion_image : (⋃x∈ (f '' s), g x) = (⋃y ∈ s, g (f y)) := by rw [← sUnion_image, ← image_comp, sUnion_image] @[simp] lemma bInter_image : (⋂x∈ (f '' s), g x) = (⋂y ∈ s, g (f y)) := by rw [← sInter_image, ← image_comp, sInter_image] end image section preimage theorem monotone_preimage {f : α → β} : monotone (preimage f) := assume a b h, preimage_mono h @[simp] theorem preimage_Union {ι : Sort w} {f : α → β} {s : ι → set β} : preimage f (⋃i, s i) = (⋃i, preimage f (s i)) := set.ext $ by simp [preimage] theorem preimage_bUnion {ι} {f : α → β} {s : set ι} {t : ι → set β} : preimage f (⋃i ∈ s, t i) = (⋃i ∈ s, preimage f (t i)) := by simp @[simp] theorem preimage_sUnion {f : α → β} {s : set (set β)} : preimage f (⋃₀ s) = (⋃t ∈ s, preimage f t) := set.ext $ by simp [preimage] lemma preimage_Inter {ι : Sort*} {s : ι → set β} {f : α → β} : f ⁻¹' (⋂ i, s i) = (⋂ i, f ⁻¹' s i) := by ext; simp lemma preimage_bInter {s : γ → set β} {t : set γ} {f : α → β} : f ⁻¹' (⋂ i∈t, s i) = (⋂ i∈t, f ⁻¹' s i) := by ext; simp end preimage section seq def seq (s : set (α → β)) (t : set α) : set β := {b | ∃f∈s, ∃a∈t, (f : α → β) a = b} lemma seq_def {s : set (α → β)} {t : set α} : seq s t = ⋃f∈s, f '' t := set.ext $ by simp [seq] @[simp] lemma mem_seq_iff {s : set (α → β)} {t : set α} {b : β} : b ∈ seq s t ↔ ∃ (f ∈ s) (a ∈ t), (f : α → β) a = b := iff.rfl lemma seq_subset {s : set (α → β)} {t : set α} {u : set β} : seq s t ⊆ u ↔ (∀f∈s, ∀a∈t, (f : α → β) a ∈ u) := iff.intro (assume h f hf a ha, h ⟨f, hf, a, ha, rfl⟩) (assume h b ⟨f, hf, a, ha, eq⟩, eq ▸ h f hf a ha) lemma seq_mono {s₀ s₁ : set (α → β)} {t₀ t₁ : set α} (hs : s₀ ⊆ s₁) (ht : t₀ ⊆ t₁) : seq s₀ t₀ ⊆ seq s₁ t₁ := assume b ⟨f, hf, a, ha, eq⟩, ⟨f, hs hf, a, ht ha, eq⟩ lemma singleton_seq {f : α → β} {t : set α} : set.seq {f} t = f '' t := set.ext $ by simp lemma seq_singleton {s : set (α → β)} {a : α} : set.seq s {a} = (λf:α→β, f a) '' s := set.ext $ by simp lemma seq_seq {s : set (β → γ)} {t : set (α → β)} {u : set α} : seq s (seq t u) = seq (seq ((∘) '' s) t) u := begin refine set.ext (assume c, iff.intro _ _), { rintros ⟨f, hfs, b, ⟨g, hg, a, hau, rfl⟩, rfl⟩, exact ⟨f ∘ g, ⟨(∘) f, mem_image_of_mem _ hfs, g, hg, rfl⟩, a, hau, rfl⟩ }, { rintros ⟨fg, ⟨fc, ⟨f, hfs, rfl⟩, g, hgt, rfl⟩, a, ha, rfl⟩, exact ⟨f, hfs, g a, ⟨g, hgt, a, ha, rfl⟩, rfl⟩ } end lemma image_seq {f : β → γ} {s : set (α → β)} {t : set α} : f '' seq s t = seq ((∘) f '' s) t := by rw [← singleton_seq, ← singleton_seq, seq_seq, image_singleton] lemma prod_eq_seq {s : set α} {t : set β} : set.prod s t = (prod.mk '' s).seq t := begin ext ⟨a, b⟩, split, { rintros ⟨ha, hb⟩, exact ⟨prod.mk a, ⟨a, ha, rfl⟩, b, hb, rfl⟩ }, { rintros ⟨f, ⟨x, hx, rfl⟩, y, hy, eq⟩, rw ← eq, exact ⟨hx, hy⟩ } end lemma prod_image_seq_comm (s : set α) (t : set β) : (prod.mk '' s).seq t = seq ((λb a, (a, b)) '' t) s := by rw [← prod_eq_seq, ← image_swap_prod, prod_eq_seq, image_seq, ← image_comp] end seq theorem monotone_prod [preorder α] {f : α → set β} {g : α → set γ} (hf : monotone f) (hg : monotone g) : monotone (λx, set.prod (f x) (g x)) := assume a b h, prod_mono (hf h) (hg h) instance : monad set := { pure := λ(α : Type u) a, {a}, bind := λ(α β : Type u) s f, ⋃i∈s, f i, seq := λ(α β : Type u), set.seq, map := λ(α β : Type u), set.image } section monad variables {α' β' : Type u} {s : set α'} {f : α' → set β'} {g : set (α' → β')} @[simp] lemma bind_def : s >>= f = ⋃i∈s, f i := rfl @[simp] lemma fmap_eq_image (f : α' → β') : f <$> s = f '' s := rfl @[simp] lemma seq_eq_set_seq {α β : Type*} (s : set (α → β)) (t : set α) : s <*> t = s.seq t := rfl @[simp] lemma pure_def (a : α) : (pure a : set α) = {a} := rfl end monad instance : is_lawful_monad set := { pure_bind := assume α β x f, by simp, bind_assoc := assume α β γ s f g, set.ext $ assume a, by simp [exists_and_distrib_right.symm, -exists_and_distrib_right, exists_and_distrib_left.symm, -exists_and_distrib_left, and_assoc]; exact exists_swap, id_map := assume α, id_map, bind_pure_comp_eq_map := assume α β f s, set.ext $ by simp [set.image, eq_comm], bind_map_eq_seq := assume α β s t, by simp [seq_def] } instance : is_comm_applicative (set : Type u → Type u) := ⟨ assume α β s t, prod_image_seq_comm s t ⟩ section pi lemma pi_def {α : Type*} {π : α → Type*} (i : set α) (s : Πa, set (π a)) : pi i s = (⋂ a∈i, ((λf:(Πa, π a), f a) ⁻¹' (s a))) := by ext; simp [pi] end pi end set /- disjoint sets -/ namespace set protected theorem disjoint_iff {s t : set α} : disjoint s t ↔ s ∩ t ⊆ ∅ := iff.rfl lemma not_disjoint_iff {s t : set α} : ¬disjoint s t ↔ ∃x, x ∈ s ∧ x ∈ t := (not_congr (set.disjoint_iff.trans subset_empty_iff)).trans ne_empty_iff_nonempty lemma disjoint_left {s t : set α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := show (∀ x, ¬(x ∈ s ∩ t)) ↔ _, from ⟨λ h a, not_and.1 $ h a, λ h a, not_and.2 $ h a⟩ theorem disjoint_right {s t : set α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s := by rw [disjoint.comm, disjoint_left] theorem disjoint_diff {a b : set α} : disjoint a (b \ a) := disjoint_iff.2 (inter_diff_self _ _) theorem disjoint_compl (s : set α) : disjoint s (-s) := assume a ⟨h₁, h₂⟩, h₂ h₁ theorem disjoint_singleton_left {a : α} {s : set α} : disjoint {a} s ↔ a ∉ s := by simp [set.disjoint_iff, subset_def]; exact iff.rfl theorem disjoint_singleton_right {a : α} {s : set α} : disjoint s {a} ↔ a ∉ s := by rw [disjoint.comm]; exact disjoint_singleton_left theorem disjoint_image_image {f : β → α} {g : γ → α} {s : set β} {t : set γ} (h : ∀b∈s, ∀c∈t, f b ≠ g c) : disjoint (f '' s) (g '' t) := by rintros a ⟨⟨b, hb, eq⟩, ⟨c, hc, rfl⟩⟩; exact h b hb c hc eq /-- A collection of sets is `pairwise_disjoint`, if any two different sets in this collection are disjoint. -/ def pairwise_disjoint (s : set (set α)) : Prop := pairwise_on s disjoint lemma pairwise_disjoint.subset {s t : set (set α)} (h : s ⊆ t) (ht : pairwise_disjoint t) : pairwise_disjoint s := pairwise_on.mono h ht lemma pairwise_disjoint.range {s : set (set α)} (f : s → set α) (hf : ∀(x : s), f x ⊆ x.1) (ht : pairwise_disjoint s) : pairwise_disjoint (range f) := begin rintro _ ⟨x, rfl⟩ _ ⟨y, rfl⟩ hxy, refine (ht _ x.2 _ y.2 _).mono (hf x) (hf y), intro h, apply hxy, apply congr_arg f, exact subtype.eq h end /- warning: classical -/ lemma pairwise_disjoint.elim {s : set (set α)} (h : pairwise_disjoint s) {x y : set α} (hx : x ∈ s) (hy : y ∈ s) (z : α) (hzx : z ∈ x) (hzy : z ∈ y) : x = y := classical.not_not.1 $ λ h', h x hx y hy h' ⟨hzx, hzy⟩ end set namespace set variables (t : α → set β) /-- If `t` is an indexed family of sets, then there is a natural map from `Σ i, t i` to `⋃ i, t i` sending `⟨i, x⟩` to `x`. -/ def sigma_to_Union (x : Σi, t i) : (⋃i, t i) := ⟨x.2, mem_Union.2 ⟨x.1, x.2.2⟩⟩ lemma surjective_sigma_to_Union : surjective (sigma_to_Union t) | ⟨b, hb⟩ := have ∃a, b ∈ t a, by simpa using hb, let ⟨a, hb⟩ := this in ⟨⟨a, ⟨b, hb⟩⟩, rfl⟩ lemma injective_sigma_to_Union (h : ∀i j, i ≠ j → disjoint (t i) (t j)) : injective (sigma_to_Union t) | ⟨a₁, ⟨b₁, h₁⟩⟩ ⟨a₂, ⟨b₂, h₂⟩⟩ eq := have b_eq : b₁ = b₂, from congr_arg subtype.val eq, have a_eq : a₁ = a₂, from classical.by_contradiction $ assume ne, have b₁ ∈ t a₁ ∩ t a₂, from ⟨h₁, b_eq.symm ▸ h₂⟩, h _ _ ne this, sigma.eq a_eq $ subtype.eq $ by subst b_eq; subst a_eq lemma bijective_sigma_to_Union (h : ∀i j, i ≠ j → disjoint (t i) (t j)) : bijective (sigma_to_Union t) := ⟨injective_sigma_to_Union t h, surjective_sigma_to_Union t⟩ noncomputable def Union_eq_sigma_of_disjoint {t : α → set β} (h : ∀i j, i ≠ j → disjoint (t i) (t j)) : (⋃i, t i) ≃ (Σi, t i) := (equiv.of_bijective _ $ bijective_sigma_to_Union t h).symm noncomputable def bUnion_eq_sigma_of_disjoint {s : set α} {t : α → set β} (h : pairwise_on s (disjoint on t)) : (⋃i∈s, t i) ≃ (Σi:s, t i.val) := equiv.trans (equiv.set_congr (bUnion_eq_Union _ _)) $ Union_eq_sigma_of_disjoint $ assume ⟨i, hi⟩ ⟨j, hj⟩ ne, h _ hi _ hj $ assume eq, ne $ subtype.eq eq end set
e1c1946b12306ba656250376d95c785c47a2ea59
9dc8cecdf3c4634764a18254e94d43da07142918
/src/set_theory/cardinal/divisibility.lean
883d7bbabacfd3518e68c82ac73e10368fd9364a
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
5,299
lean
/- Copyright (c) 2022 Eric Rodriguez. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Rodriguez -/ import algebra.is_prime_pow import set_theory.cardinal.ordinal /-! # Cardinal Divisibility We show basic results about divisibility in the cardinal numbers. This relation can be characterised in the following simple way: if `a` and `b` are both less than `ℵ₀`, then `a ∣ b` iff they are divisible as natural numbers. If `b` is greater than `ℵ₀`, then `a ∣ b` iff `a ≤ b`. This furthermore shows that all infinite cardinals are prime; recall that `a * b = max a b` if `ℵ₀ ≤ a * b`; therefore `a ∣ b * c = a ∣ max b c` and therefore clearly either `a ∣ b` or `a ∣ c`. Note furthermore that no infinite cardinal is irreducible (`cardinal.not_irreducible_of_aleph_0_le`), showing that the cardinal numbers do not form a `comm_cancel_monoid_with_zero`. ## Main results * `cardinal.prime_of_aleph_0_le`: a `cardinal` is prime if it is infinite. * `cardinal.is_prime_iff`: a `cardinal` is prime iff it is infinite or a prime natural number. * `cardinal.is_prime_pow_iff`: a `cardinal` is a prime power iff it is infinite or a natural number which is itself a prime power. -/ namespace cardinal open_locale cardinal universe u variables {a b : cardinal.{u}} {n m : ℕ} @[simp] lemma is_unit_iff : is_unit a ↔ a = 1 := begin refine ⟨λ h, _, by { rintro rfl, exact is_unit_one }⟩, rcases eq_or_ne a 0 with rfl | ha, { exact (not_is_unit_zero h).elim }, rw is_unit_iff_forall_dvd at h, cases h 1 with t ht, rw [eq_comm, mul_eq_one_iff'] at ht, { exact ht.1 }, all_goals { rwa one_le_iff_ne_zero }, { rintro rfl, rw mul_zero at ht, exact zero_ne_one ht } end instance : unique cardinal.{u}ˣ := { default := 1, uniq := λ a, units.coe_eq_one.mp $ is_unit_iff.mp a.is_unit } theorem le_of_dvd : ∀ {a b : cardinal}, b ≠ 0 → a ∣ b → a ≤ b | a _ b0 ⟨b, rfl⟩ := by simpa only [mul_one] using mul_le_mul_left' (one_le_iff_ne_zero.2 (λ h : b = 0, by simpa only [h, mul_zero] using b0)) a lemma dvd_of_le_of_aleph_0_le (ha : a ≠ 0) (h : a ≤ b) (hb : ℵ₀ ≤ b) : a ∣ b := ⟨b, (mul_eq_right hb h ha).symm⟩ @[simp] lemma prime_of_aleph_0_le (ha : ℵ₀ ≤ a) : prime a := begin refine ⟨(aleph_0_pos.trans_le ha).ne', _, λ b c hbc, _⟩, { rw is_unit_iff, exact (one_lt_aleph_0.trans_le ha).ne' }, cases eq_or_ne (b * c) 0 with hz hz, { rcases mul_eq_zero.mp hz with rfl | rfl; simp }, wlog h : c ≤ b, left, have habc := le_of_dvd hz hbc, rwa [mul_eq_max' $ ha.trans $ habc, max_def, if_pos h] at hbc end lemma not_irreducible_of_aleph_0_le (ha : ℵ₀ ≤ a) : ¬irreducible a := begin rw [irreducible_iff, not_and_distrib], refine or.inr (λ h, _), simpa [mul_aleph_0_eq ha, is_unit_iff, (one_lt_aleph_0.trans_le ha).ne', one_lt_aleph_0.ne'] using h a ℵ₀ end @[simp, norm_cast] lemma nat_coe_dvd_iff : (n : cardinal) ∣ m ↔ n ∣ m := begin refine ⟨_, λ ⟨h, ht⟩, ⟨h, by exact_mod_cast ht⟩⟩, rintro ⟨k, hk⟩, have : ↑m < ℵ₀ := nat_lt_aleph_0 m, rw [hk, mul_lt_aleph_0_iff] at this, rcases this with h | h | ⟨-, hk'⟩, iterate 2 { simp only [h, mul_zero, zero_mul, nat.cast_eq_zero] at hk, simp [hk] }, lift k to ℕ using hk', exact ⟨k, by exact_mod_cast hk⟩ end @[simp] lemma nat_is_prime_iff : prime (n : cardinal) ↔ n.prime := begin simp only [prime, nat.prime_iff], refine and_congr (by simp) (and_congr _ ⟨λ h b c hbc, _, λ h b c hbc, _⟩), { simp only [is_unit_iff, nat.is_unit_iff], exact_mod_cast iff.rfl }, { exact_mod_cast h b c (by exact_mod_cast hbc) }, cases lt_or_le (b * c) ℵ₀ with h' h', { rcases mul_lt_aleph_0_iff.mp h' with rfl | rfl | ⟨hb, hc⟩, { simp }, { simp }, lift b to ℕ using hb, lift c to ℕ using hc, exact_mod_cast h b c (by exact_mod_cast hbc) }, rcases aleph_0_le_mul_iff.mp h' with ⟨hb, hc, hℵ₀⟩, have hn : (n : cardinal) ≠ 0, { intro h, rw [h, zero_dvd_iff, mul_eq_zero] at hbc, cases hbc; contradiction }, wlog hℵ₀ : ℵ₀ ≤ b := hℵ₀ using [b c], exact or.inl (dvd_of_le_of_aleph_0_le hn ((nat_lt_aleph_0 n).le.trans hℵ₀) hℵ₀), end lemma is_prime_iff {a : cardinal} : prime a ↔ ℵ₀ ≤ a ∨ ∃ p : ℕ, a = p ∧ p.prime := begin cases le_or_lt ℵ₀ a with h h, { simp [h] }, lift a to ℕ using id h, simp [not_le.mpr h] end lemma is_prime_pow_iff {a : cardinal} : is_prime_pow a ↔ ℵ₀ ≤ a ∨ ∃ n : ℕ, a = n ∧ is_prime_pow n := begin by_cases h : ℵ₀ ≤ a, { simp [h, (prime_of_aleph_0_le h).is_prime_pow] }, lift a to ℕ using not_le.mp h, simp only [h, nat.cast_inj, exists_eq_left', false_or, is_prime_pow_nat_iff], rw is_prime_pow_def, refine ⟨_, λ ⟨p, k, hp, hk, h⟩, ⟨p, k, nat_is_prime_iff.2 hp, by exact_mod_cast and.intro hk h⟩⟩, rintro ⟨p, k, hp, hk, hpk⟩, have key : _ ≤ p ^ k := power_le_power_left hp.ne_zero (show (1 : cardinal) ≤ k, by exact_mod_cast hk), rw [power_one, hpk] at key, lift p to ℕ using key.trans_lt (nat_lt_aleph_0 a), exact ⟨p, k, nat_is_prime_iff.mp hp, hk, by exact_mod_cast hpk⟩ end end cardinal
3440f602e7af26bac186991fe86e33c132a109f6
c46a31beec236d29b6c02e7d7683691bcfbb3014
/src/utils/real.lean
2f81d1982c993ebb423d78c2c219905bf3fdfc39
[]
no_license
UVM-M52/quiz-5-maddiestrauss
a88b9bfbdd486a521ee280f9b7b551bcb48f23c6
214529615e08bbcdd3d6600c89432ec985e6ba3a
refs/heads/master
1,617,897,187,103
1,584,922,038,000
1,584,922,038,000
248,787,429
0
0
null
null
null
null
UTF-8
Lean
false
false
431
lean
import .cmp attribute [instance, priority 10] classical.prop_decidable axiom ℝ : Type @[instance] axiom real_field : discrete_linear_ordered_field ℝ axiom sup (s : ℕ → ℝ) : (∀ n, s n ≤ s (n+1)) → (∃ b, ∀ n, s n ≤ b) → ℝ axiom sup_spec (s : ℕ → ℝ) (hm : ∀ n, s n ≤ s (n+1)) (hb : ∃ b, ∀ n, s n ≤ b) : (∀ n, s n ≤ sup s hm hb) ∧ (∀ b, (∀ n, s n ≤ b) → sup s hm hb ≤ b)
6aeaca6e1f9c6a996e0da0d01b46bb01177ffefc
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/compiler/closure_bug2.lean
ca586cfb3086bbc806b255afb39b80f5d380a72f
[ "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
539
lean
def f (x : Nat) : Nat × (Nat → String) := let x1 := x + 1 let x2 := x + 2 let x3 := x + 3 let x4 := x + 4 let x5 := x + 5 let x6 := x + 6 let x7 := x + 7 let x8 := x + 8 let x9 := x + 9 let x10 := x + 10 let x11 := x + 11 let x12 := x + 12 let x13 := x + 13 let x14 := x + 14 let x15 := x + 15 let x16 := x + 16 let x17 := x + 17 (x, fun y => toString [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17]) def main (xs : List String) : IO Unit := IO.println ((f (xs.headD "0").toNat!).2 (xs.headD "0").toNat!)
b862fd719ebe0ec88f21cfb5520429f5736947b5
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/algebra/pointwise.lean
0fa274f434b02fecd8dedd5bb5309383db8a200b
[ "Apache-2.0" ]
permissive
EdAyers/mathlib
9ecfb2f14bd6caad748b64c9c131befbff0fb4e0
ca5d4c1f16f9c451cf7170b10105d0051db79e1b
refs/heads/master
1,626,189,395,845
1,555,284,396,000
1,555,284,396,000
144,004,030
0
0
Apache-2.0
1,533,727,664,000
1,533,727,663,000
null
UTF-8
Lean
false
false
7,545
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin Pointwise addition and multiplication of sets -/ import data.set.finite import data.set.lattice import algebra.group import group_theory.subgroup namespace set open function variables {α : Type*} {β : Type*} (f : α → β) @[to_additive set.pointwise_zero] def pointwise_one [has_one α] : has_one (set α) := ⟨{1}⟩ local attribute [instance] pointwise_one @[simp, to_additive set.mem_pointwise_zero] lemma mem_pointwise_one [has_one α] (a : α) : a ∈ (1 : set α) ↔ a = 1 := mem_singleton_iff @[to_additive set.pointwise_add] def pointwise_mul [has_mul α] : has_mul (set α) := ⟨λ s t, {a | ∃ x ∈ s, ∃ y ∈ t, a = x * y}⟩ local attribute [instance] pointwise_one pointwise_mul @[to_additive set.mem_pointwise_add] lemma mem_pointwise_mul [has_mul α] {s t : set α} {a : α} : a ∈ s * t ↔ ∃ x ∈ s, ∃ y ∈ t, a = x * y := iff.rfl @[to_additive set.add_mem_pointwise_add] lemma mul_mem_pointwise_mul [has_mul α] {s t : set α} {a b : α} (ha : a ∈ s) (hb : b ∈ t) : a * b ∈ s * t := ⟨_, ha, _, hb, rfl⟩ @[to_additive set.pointwise_add_eq_image] lemma pointwise_mul_eq_image [has_mul α] {s t : set α} : s * t = (λ x : α × α, x.fst * x.snd) '' s.prod t := set.ext $ λ a, ⟨ by { rintros ⟨_, _, _, _, rfl⟩, exact ⟨(_, _), mem_prod.mpr ⟨‹_›, ‹_›⟩, rfl⟩ }, by { rintros ⟨_, _, rfl⟩, exact ⟨_, (mem_prod.mp ‹_›).1, _, (mem_prod.mp ‹_›).2, rfl⟩ }⟩ @[to_additive set.pointwise_add_finite] lemma pointwise_mul_finite [has_mul α] {s t : set α} (hs : finite s) (ht : finite t) : finite (s * t) := by { rw pointwise_mul_eq_image, apply set.finite_image, exact set.finite_prod hs ht } def pointwise_mul_semigroup [semigroup α] : semigroup (set α) := { mul_assoc := λ _ _ _, set.ext $ λ _, begin split, { rintros ⟨_, ⟨_, _, _, _, rfl⟩, _, _, rfl⟩, exact ⟨_, ‹_›, _, ⟨_, ‹_›, _, ‹_›, rfl⟩, mul_assoc _ _ _⟩ }, { rintros ⟨_, _, _, ⟨_, _, _, _, rfl⟩, rfl⟩, exact ⟨_, ⟨_, ‹_›, _, ‹_›, rfl⟩, _, ‹_›, (mul_assoc _ _ _).symm⟩ } end, ..pointwise_mul } def pointwise_add_add_semigroup [add_semigroup α] : add_semigroup (set α) := { add_assoc := λ _ _ _, set.ext $ λ _, begin split, { rintros ⟨_, ⟨_, _, _, _, rfl⟩, _, _, rfl⟩, exact ⟨_, ‹_›, _, ⟨_, ‹_›, _, ‹_›, rfl⟩, add_assoc _ _ _⟩ }, { rintros ⟨_, _, _, ⟨_, _, _, _, rfl⟩, rfl⟩, exact ⟨_, ⟨_, ‹_›, _, ‹_›, rfl⟩, _, ‹_›, (add_assoc _ _ _).symm⟩ } end, ..pointwise_add } attribute [to_additive set.pointwise_add_add_semigroup._proof_1] pointwise_mul_semigroup._proof_1 attribute [to_additive set.pointwise_add_add_semigroup] pointwise_mul_semigroup def pointwise_mul_monoid [monoid α] : monoid (set α) := { one_mul := λ s, set.ext $ λ a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, simp * at *}, λ h, ⟨1, mem_singleton 1, a, h, (one_mul a).symm⟩⟩, mul_one := λ s, set.ext $ λ a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, simp * at *}, λ h, ⟨a, h, 1, mem_singleton 1, (mul_one a).symm⟩⟩, ..pointwise_mul_semigroup, ..pointwise_one } def pointwise_add_add_monoid [add_monoid α] : add_monoid (set α) := { zero_add := λ s, set.ext $ λ a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, simp * at *}, λ h, ⟨0, mem_singleton 0, a, h, (zero_add a).symm⟩⟩, add_zero := λ s, set.ext $ λ a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, simp * at *}, λ h, ⟨a, h, 0, mem_singleton 0, (add_zero a).symm⟩⟩, ..pointwise_add_add_semigroup, ..pointwise_zero } attribute [to_additive set.pointwise_add_add_monoid._proof_1] pointwise_mul_monoid._proof_1 attribute [to_additive set.pointwise_add_add_monoid._proof_2] pointwise_mul_monoid._proof_2 attribute [to_additive set.pointwise_add_add_monoid._proof_3] pointwise_mul_monoid._proof_3 attribute [to_additive set.pointwise_add_add_monoid] pointwise_mul_monoid local attribute [instance] pointwise_mul_monoid @[to_additive set.singleton.is_add_hom] def singleton.is_mul_hom [has_mul α] : is_mul_hom (singleton : α → set α) := { map_mul := λ x y, set.ext $ λ a, by simp [mem_singleton_iff, mem_pointwise_mul] } @[to_additive set.singleton.is_add_monoid_hom] def singleton.is_monoid_hom [monoid α] : is_monoid_hom (singleton : α → set α) := { map_one := rfl, ..singleton.is_mul_hom } @[to_additive set.pointwise_neg] def pointwise_inv [has_inv α] : has_inv (set α) := ⟨λ s, {a | a⁻¹ ∈ s}⟩ @[simp, to_additive set.pointwise_add_empty] lemma pointwise_mul_empty [has_mul α] (s : set α) : s * ∅ = ∅ := set.ext $ λ a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, tauto}, false.elim⟩ @[simp, to_additive set.empty_pointwise_add] lemma empty_pointwise_mul [has_mul α] (s : set α) : ∅ * s = ∅ := set.ext $ λ a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, tauto}, false.elim⟩ @[to_additive set.pointwise_add_subset_add] lemma pointwise_mul_subset_mul [has_mul α] {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ * s₂ ⊆ t₁ * t₂ := by {rintros _ ⟨_, _, _, _, rfl⟩, exact ⟨_, h₁ ‹_›, _, h₂ ‹_›, rfl⟩ } @[to_additive set.pointwise_add_union] lemma pointwise_mul_union [has_mul α] (s t u : set α) : s * (t ∪ u) = (s * t) ∪ (s * u) := begin ext a, split, { rintros ⟨_, _, _, H, rfl⟩, cases H; [left, right]; exact ⟨_, ‹_›, _, ‹_›, rfl⟩ }, { intro H, cases H with H H; { rcases H with ⟨_, _, _, _, rfl⟩, refine ⟨_, ‹_›, _, _, rfl⟩, erw mem_union, simp * } } end @[to_additive set.union_pointwise_add] lemma union_pointwise_mul [has_mul α] (s t u : set α) : (s ∪ t) * u = (s * u) ∪ (t * u) := begin ext a, split, { rintros ⟨_, H, _, _, rfl⟩, cases H; [left, right]; exact ⟨_, ‹_›, _, ‹_›, rfl⟩ }, { intro H, cases H with H H; { rcases H with ⟨_, _, _, _, rfl⟩; refine ⟨_, _, _, ‹_›, rfl⟩; erw mem_union; simp * } } end def pointwise_mul_semiring [monoid α] : semiring (set α) := { add := (⊔), zero := ∅, add_assoc := set.union_assoc, zero_add := set.empty_union, add_zero := set.union_empty, add_comm := set.union_comm, zero_mul := empty_pointwise_mul, mul_zero := pointwise_mul_empty, left_distrib := pointwise_mul_union, right_distrib := union_pointwise_mul, ..pointwise_mul_monoid } def pointwise_mul_comm_semiring [comm_monoid α] : comm_semiring (set α) := { mul_comm := λ s t, set.ext $ λ a, by split; { rintros ⟨_, _, _, _, rfl⟩, rw mul_comm, exact ⟨_, ‹_›, _, ‹_›, rfl⟩ }, ..pointwise_mul_semiring } local attribute [instance] pointwise_mul_semiring variables [monoid α] [monoid β] [is_monoid_hom f] instance : is_semiring_hom (image f) := { map_zero := image_empty _, map_one := by erw [image_singleton, is_monoid_hom.map_one f]; refl, map_add := image_union _, map_mul := λ s t, set.ext $ λ a, begin split, { rintros ⟨_, ⟨_, _, _, _, rfl⟩, rfl⟩, refine ⟨_, ⟨_, ‹_›, rfl⟩, _, ⟨_, ‹_›, rfl⟩, _⟩, apply is_monoid_hom.map_mul f }, { rintros ⟨_, ⟨_, _, rfl⟩, _, ⟨_, _, rfl⟩, rfl⟩, refine ⟨_, ⟨_, ‹_›, _, ‹_›, rfl⟩, _⟩, apply is_monoid_hom.map_mul f } end } end set
4a3cde25b4942dac8ba4e94a6ca7ee611a4f2834
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/inductive1.lean
e86facc3340caf0ab12799c390076ea7303021fc
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,641
lean
inductive L1.{u} (α : Type u) | nil | cons : α → L1 α → L1 α #check L1 #check @L1.cons inductive L2.{u} (α : Type u) | nil | cons (head : α) (tail : L2 α) #check @L2.cons universe u v variable (α : Type u) inductive A (β : Type v) | nil | protected cons : α → β → A β → A β #check @A.cons #check A.nil (α := Nat) (β := Bool) mutual inductive isEven : Nat → Prop | z : isEven 0 | s (n : Nat) : isOdd n → isEven (n+1) inductive isOdd : Nat → Prop | s (n : Nat) : isEven n → isOdd (n+1) end #check isEven #check isOdd.s #check @isEven.rec inductive V (α : Type _) : Nat → Type _ | nil : V α 0 | cons {n : Nat} : α → V α n → V α (n+1) #check @V.nil #check @V.cons #check @V.rec #check @V.noConfusion #check @V.brecOn #check @V.binductionOn #check @V.casesOn #check @V.recOn #check @V.below class inductive Dec (p : Prop) : Type | isTrue (h : p) | isFalse (h : Not p) instance tst : Dec True := Dec.isTrue True.intro #check tst variable (β : Type _) inductive T1 | mk : β → β → T1 #check @T1.mk inductive MyEq {α : Type} (a : α) : α → Prop | refl : MyEq a a #check @MyEq.refl inductive ListLast {α : Type u} : List α → Type u | empty : ListLast [] | nonEmpty : (as : List α) → (a : α) → ListLast (as ++ [a]) -- make sure to instantiate mvars in constructors inductive Test : Nat → Type | mk : Test ((fun n => n.succ) Nat.zero) inductive SortedMap {α : Type u} {β : Type v} [LT α] : List (α × β) → Prop | nil : SortedMap [] | cons : ∀ (k : α) (v : β) (l : List (α × β)), SortedMap l → SortedMap ((k,v)::l)
77e04105f6bfeb3f42877cd5689f88d2d6609f08
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/whiskering.lean
3983f85813602f553a10740fa94d387cb6bb313f
[ "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
8,473
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.isomorphism import category_theory.functor.category import category_theory.functor.fully_faithful /-! # Whiskering Given a functor `F : C ⥤ D` and functors `G H : D ⥤ E` and a natural transformation `α : G ⟶ H`, we can construct a new natural transformation `F ⋙ G ⟶ F ⋙ H`, called `whisker_left F α`. This is the same as the horizontal composition of `𝟙 F` with `α`. This operation is functorial in `F`, and we package this as `whiskering_left`. Here `(whiskering_left.obj F).obj G` is `F ⋙ G`, and `(whiskering_left.obj F).map α` is `whisker_left F α`. (That is, we might have alternatively named this as the "left composition functor".) We also provide analogues for composition on the right, and for these operations on isomorphisms. At the end of the file, we provide the left and right unitors, and the associator, for functor composition. (In fact functor composition is definitionally associative, but very often relying on this causes extremely slow elaboration, so it is better to insert it explicitly.) We also show these natural isomorphisms satisfy the triangle and pentagon identities. -/ namespace category_theory universes u₁ v₁ u₂ v₂ u₃ v₃ u₄ v₄ section variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] {E : Type u₃} [category.{v₃} E] /-- If `α : G ⟶ H` then `whisker_left F α : (F ⋙ G) ⟶ (F ⋙ H)` has components `α.app (F.obj X)`. -/ @[simps] def whisker_left (F : C ⥤ D) {G H : D ⥤ E} (α : G ⟶ H) : (F ⋙ G) ⟶ (F ⋙ H) := { app := λ X, α.app (F.obj X), naturality' := λ X Y f, by rw [functor.comp_map, functor.comp_map, α.naturality] } /-- If `α : G ⟶ H` then `whisker_right α F : (G ⋙ F) ⟶ (G ⋙ F)` has components `F.map (α.app X)`. -/ @[simps] def whisker_right {G H : C ⥤ D} (α : G ⟶ H) (F : D ⥤ E) : (G ⋙ F) ⟶ (H ⋙ F) := { app := λ X, F.map (α.app X), naturality' := λ X Y f, by rw [functor.comp_map, functor.comp_map, ←F.map_comp, ←F.map_comp, α.naturality] } variables (C D E) /-- Left-composition gives a functor `(C ⥤ D) ⥤ ((D ⥤ E) ⥤ (C ⥤ E))`. `(whiskering_left.obj F).obj G` is `F ⋙ G`, and `(whiskering_left.obj F).map α` is `whisker_left F α`. -/ @[simps] def whiskering_left : (C ⥤ D) ⥤ ((D ⥤ E) ⥤ (C ⥤ E)) := { obj := λ F, { obj := λ G, F ⋙ G, map := λ G H α, whisker_left F α }, map := λ F G τ, { app := λ H, { app := λ c, H.map (τ.app c), naturality' := λ X Y f, begin dsimp, rw [←H.map_comp, ←H.map_comp, ←τ.naturality] end }, naturality' := λ X Y f, begin ext, dsimp, rw [f.naturality] end } } /-- Right-composition gives a functor `(D ⥤ E) ⥤ ((C ⥤ D) ⥤ (C ⥤ E))`. `(whiskering_right.obj H).obj F` is `F ⋙ H`, and `(whiskering_right.obj H).map α` is `whisker_right α H`. -/ @[simps] def whiskering_right : (D ⥤ E) ⥤ ((C ⥤ D) ⥤ (C ⥤ E)) := { obj := λ H, { obj := λ F, F ⋙ H, map := λ _ _ α, whisker_right α H }, map := λ G H τ, { app := λ F, { app := λ c, τ.app (F.obj c), naturality' := λ X Y f, begin dsimp, rw [τ.naturality] end }, naturality' := λ X Y f, begin ext, dsimp, rw [←nat_trans.naturality] end } } variables {C} {D} {E} instance faithful_whiskering_right_obj {F : D ⥤ E} [faithful F] : faithful ((whiskering_right C D E).obj F) := { map_injective' := λ G H α β hαβ, nat_trans.ext _ _ $ funext $ λ X, functor.map_injective _ $ congr_fun (congr_arg nat_trans.app hαβ) X } @[simp] lemma whisker_left_id (F : C ⥤ D) {G : D ⥤ E} : whisker_left F (nat_trans.id G) = nat_trans.id (F.comp G) := rfl @[simp] lemma whisker_left_id' (F : C ⥤ D) {G : D ⥤ E} : whisker_left F (𝟙 G) = 𝟙 (F.comp G) := rfl @[simp] lemma whisker_right_id {G : C ⥤ D} (F : D ⥤ E) : whisker_right (nat_trans.id G) F = nat_trans.id (G.comp F) := ((whiskering_right C D E).obj F).map_id _ @[simp] lemma whisker_right_id' {G : C ⥤ D} (F : D ⥤ E) : whisker_right (𝟙 G) F = 𝟙 (G.comp F) := ((whiskering_right C D E).obj F).map_id _ @[simp] lemma whisker_left_comp (F : C ⥤ D) {G H K : D ⥤ E} (α : G ⟶ H) (β : H ⟶ K) : whisker_left F (α ≫ β) = (whisker_left F α) ≫ (whisker_left F β) := rfl @[simp] lemma whisker_right_comp {G H K : C ⥤ D} (α : G ⟶ H) (β : H ⟶ K) (F : D ⥤ E) : whisker_right (α ≫ β) F = (whisker_right α F) ≫ (whisker_right β F) := ((whiskering_right C D E).obj F).map_comp α β /-- If `α : G ≅ H` is a natural isomorphism then `iso_whisker_left F α : (F ⋙ G) ≅ (F ⋙ H)` has components `α.app (F.obj X)`. -/ def iso_whisker_left (F : C ⥤ D) {G H : D ⥤ E} (α : G ≅ H) : (F ⋙ G) ≅ (F ⋙ H) := ((whiskering_left C D E).obj F).map_iso α @[simp] lemma iso_whisker_left_hom (F : C ⥤ D) {G H : D ⥤ E} (α : G ≅ H) : (iso_whisker_left F α).hom = whisker_left F α.hom := rfl @[simp] lemma iso_whisker_left_inv (F : C ⥤ D) {G H : D ⥤ E} (α : G ≅ H) : (iso_whisker_left F α).inv = whisker_left F α.inv := rfl /-- If `α : G ≅ H` then `iso_whisker_right α F : (G ⋙ F) ≅ (H ⋙ F)` has components `F.map_iso (α.app X)`. -/ def iso_whisker_right {G H : C ⥤ D} (α : G ≅ H) (F : D ⥤ E) : (G ⋙ F) ≅ (H ⋙ F) := ((whiskering_right C D E).obj F).map_iso α @[simp] lemma iso_whisker_right_hom {G H : C ⥤ D} (α : G ≅ H) (F : D ⥤ E) : (iso_whisker_right α F).hom = whisker_right α.hom F := rfl @[simp] lemma iso_whisker_right_inv {G H : C ⥤ D} (α : G ≅ H) (F : D ⥤ E) : (iso_whisker_right α F).inv = whisker_right α.inv F := rfl instance is_iso_whisker_left (F : C ⥤ D) {G H : D ⥤ E} (α : G ⟶ H) [is_iso α] : is_iso (whisker_left F α) := is_iso.of_iso (iso_whisker_left F (as_iso α)) instance is_iso_whisker_right {G H : C ⥤ D} (α : G ⟶ H) (F : D ⥤ E) [is_iso α] : is_iso (whisker_right α F) := is_iso.of_iso (iso_whisker_right (as_iso α) F) variables {B : Type u₄} [category.{v₄} B] local attribute [elab_simple] whisker_left whisker_right @[simp] lemma whisker_left_twice (F : B ⥤ C) (G : C ⥤ D) {H K : D ⥤ E} (α : H ⟶ K) : whisker_left F (whisker_left G α) = whisker_left (F ⋙ G) α := rfl @[simp] lemma whisker_right_twice {H K : B ⥤ C} (F : C ⥤ D) (G : D ⥤ E) (α : H ⟶ K) : whisker_right (whisker_right α F) G = whisker_right α (F ⋙ G) := rfl lemma whisker_right_left (F : B ⥤ C) {G H : C ⥤ D} (α : G ⟶ H) (K : D ⥤ E) : whisker_right (whisker_left F α) K = whisker_left F (whisker_right α K) := rfl end namespace functor universes u₅ v₅ variables {A : Type u₁} [category.{v₁} A] variables {B : Type u₂} [category.{v₂} B] /-- The left unitor, a natural isomorphism `((𝟭 _) ⋙ F) ≅ F`. -/ @[simps] def left_unitor (F : A ⥤ B) : ((𝟭 A) ⋙ F) ≅ F := { hom := { app := λ X, 𝟙 (F.obj X) }, inv := { app := λ X, 𝟙 (F.obj X) } } /-- The right unitor, a natural isomorphism `(F ⋙ (𝟭 B)) ≅ F`. -/ @[simps] def right_unitor (F : A ⥤ B) : (F ⋙ (𝟭 B)) ≅ F := { hom := { app := λ X, 𝟙 (F.obj X) }, inv := { app := λ X, 𝟙 (F.obj X) } } variables {C : Type u₃} [category.{v₃} C] variables {D : Type u₄} [category.{v₄} D] /-- The associator for functors, a natural isomorphism `((F ⋙ G) ⋙ H) ≅ (F ⋙ (G ⋙ H))`. (In fact, `iso.refl _` will work here, but it tends to make Lean slow later, and it's usually best to insert explicit associators.) -/ @[simps] def associator (F : A ⥤ B) (G : B ⥤ C) (H : C ⥤ D) : ((F ⋙ G) ⋙ H) ≅ (F ⋙ (G ⋙ H)) := { hom := { app := λ _, 𝟙 _ }, inv := { app := λ _, 𝟙 _ } } lemma triangle (F : A ⥤ B) (G : B ⥤ C) : (associator F (𝟭 B) G).hom ≫ (whisker_left F (left_unitor G).hom) = (whisker_right (right_unitor F).hom G) := by { ext, dsimp, simp } -- See note [dsimp, simp]. variables {E : Type u₅} [category.{v₅} E] variables (F : A ⥤ B) (G : B ⥤ C) (H : C ⥤ D) (K : D ⥤ E) lemma pentagon : (whisker_right (associator F G H).hom K) ≫ (associator F (G ⋙ H) K).hom ≫ (whisker_left F (associator G H K).hom) = ((associator (F ⋙ G) H K).hom ≫ (associator F G (H ⋙ K)).hom) := by { ext, dsimp, simp } end functor end category_theory
5e03d3035bfe98867f4d49a346e08bc9504a033f
4727251e0cd73359b15b664c3170e5d754078599
/src/data/fintype/card_embedding.lean
4b8b24fcdc02e5f4f06e8a8dbfd57221b033192a
[ "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
1,905
lean
/- Copyright (c) 2021 Eric Rodriguez. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Rodriguez -/ import data.fintype.card import logic.equiv.fin import logic.equiv.embedding /-! # Number of embeddings This file establishes the cardinality of `α ↪ β` in full generality. -/ local notation `|` x `|` := finset.card x local notation `‖` x `‖` := fintype.card x open function open_locale nat big_operators namespace fintype lemma card_embedding_eq_of_unique {α β : Type*} [unique α] [fintype β] [fintype (α ↪ β)] : ‖α ↪ β‖ = ‖β‖ := card_congr equiv.unique_embedding_equiv_result /- Establishes the cardinality of the type of all injections between two finite types. -/ @[simp] theorem card_embedding_eq {α β} [fintype α] [fintype β] [fintype (α ↪ β)] : ‖α ↪ β‖ = (‖β‖.desc_factorial ‖α‖) := begin classical, unfreezingI { induction ‹fintype α› using fintype.induction_empty_option' with α₁ α₂ h₂ e ih α h ih }, { letI := fintype.of_equiv _ e.symm, rw [← card_congr (equiv.embedding_congr e (equiv.refl β)), ih, card_congr e] }, { rw [card_pempty, nat.desc_factorial_zero, card_eq_one_iff], exact ⟨embedding.of_is_empty, λ x, fun_like.ext _ _ is_empty_elim⟩ }, { rw [card_option, nat.desc_factorial_succ, card_congr (embedding.option_embedding_equiv α β), card_sigma, ← ih], simp only [fintype.card_compl_set, fintype.card_range, finset.sum_const, finset.card_univ, smul_eq_mul, mul_comm] }, end /- The cardinality of embeddings from an infinite type to a finite type is zero. This is a re-statement of the pigeonhole principle. -/ @[simp] lemma card_embedding_eq_of_infinite {α β} [infinite α] [fintype β] [fintype (α ↪ β)] : ‖α ↪ β‖ = 0 := card_eq_zero_iff.mpr function.embedding.is_empty end fintype
9c44ce9acb671426b2b378e6f70429f4627bdda4
2c443ed3c09d98edcad40e5d286e53bc5a2ae838
/src/category_theory_stuff/solutions/chapter_1/section_2.lean
fe4a4f6e9ad090929f670264e9ce83384e9308a9
[]
no_license
agusakov/riehl-solutions
f8be5e0bbd9b4d691cb8a45b6e4f4cc06aaf6c17
3cba620de377649e9269c53e17a65cfccd7db651
refs/heads/master
1,668,377,179,927
1,593,744,143,000
1,593,744,143,000
275,534,049
0
0
null
null
null
null
UTF-8
Lean
false
false
1,172
lean
import category_theory.category category_theory.epi_mono tactic-- this transitively imports open category_theory universes v u variables (C : Type u) [category.{v} C] /-Prove Lemma 1.2.11 by proving either (i) or (i') and either (ii) or (i'), then arguing by duality. Conclude that the monomorphisms in any category define a subcategory of that category and dually that the epimorphisms also define a subcategory. -/ /-Lemma 1.2.11(i) If f : x ⟶ y and g : y ⟶ z are monomorphisms, then so is gf : x ⟶ z.-/ lemma mono_comp_mono (X Y Z : C) (f : X ⟶ Y) (g : Y ⟶ Z) [hg : mono g] [hf : mono f]: mono (f ≫ g) := begin constructor, --what the heck this is so useful intros Z h k hcomp, repeat {rw ← category.assoc at hcomp}, rw cancel_mono g at hcomp, rw cancel_mono f at hcomp, exact hcomp, end /-Lemma 1.2.11(ii) If f : x ⟶ y and g : y ⟶ z are morphisms so that gf is monic, then f is monic. -/ lemma comp_mono_mono (X Y Z : C) (f : X ⟶ Y) (g : Y ⟶ Z) [hfg : mono (f ≫ g)]: mono f := begin constructor, intros Z h k hf, rw ← cancel_mono (f ≫ g), repeat {rw ← category.assoc}, rw hf, end
22227d30d7e4c0fed3900d4aeebb80fb85f24c36
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/lvl1.lean
b4ed09fc87b2fb961c32b9ff4dc952cbd02d0852
[ "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
1,345
lean
import Lean.Level new_frontend namespace Lean namespace Level def mkMax (xs : Array Level) : Level := xs.foldlFrom mkLevelMax (xs.get! 0) 1 #eval toString $ normalize $ mkLevelSucc $ mkLevelSucc $ mkMax #[levelZero, mkLevelParam `w, mkLevelSucc (mkLevelSucc (mkLevelSucc (mkLevelParam `z))), levelOne, mkLevelSucc (mkLevelSucc (mkLevelParam `x)), levelZero, mkLevelParam `x, mkLevelParam `y, mkLevelParam `x, mkLevelParam `z, mkLevelSucc levelOne, mkLevelParam `w, mkLevelSucc (mkLevelParam `x)] #eval toString $ normalize $ mkLevelMax levelZero (mkLevelParam `x) #eval toString $ normalize $ mkLevelMax (mkLevelParam `x) levelZero #eval toString $ normalize $ mkLevelMax levelZero levelOne #eval toString $ normalize $ mkLevelSucc (mkLevelMax (mkLevelParam `x) (mkLevelParam `x)) #eval toString $ normalize $ mkLevelMax (mkLevelIMax (mkLevelParam `x) levelOne) (mkLevelMax (mkLevelSucc (mkLevelParam `x)) (mkLevelParam `x)) #eval toString $ normalize $ mkLevelIMax (mkLevelIMax (mkLevelParam `x) levelOne) (mkLevelMax (mkLevelSucc (mkLevelParam `x)) (mkLevelParam `x)) #eval toString $ #[levelZero, mkLevelSucc (mkLevelSucc (mkLevelParam `z)), levelOne, mkLevelSucc (mkLevelSucc (mkLevelParam `x)), levelZero, mkLevelParam `x, mkLevelParam `y, mkLevelParam `x, mkLevelParam `z, mkLevelSucc (mkLevelParam `x)].qsort normLt end Level end Lean
e00ccb5bbd3b6daba8a7e706df5b80972edbfa6f
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/mv_polynomial/equiv_auto.lean
3482cee86c706db3dc83cd2975510f8b6a1fbc0f
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
10,407
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, Johan Commelin, Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.mv_polynomial.rename import Mathlib.data.equiv.fin import Mathlib.PostPort universes u u_1 v w x namespace Mathlib /-! # Equivalences between polynomial rings This file establishes a number of equivalences between polynomial rings, based on equivalences between the underlying types. ## Notation As in other polynomial files, we typically use the notation: + `σ : Type*` (indexing the variables) + `R : Type*` `[comm_semiring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `mv_polynomial σ R` which mathematicians might call `X^s` + `a : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : mv_polynomial σ R` ## Tags equivalence, isomorphism, morphism, ring hom, hom -/ namespace mv_polynomial /-- The ring isomorphism between multivariable polynomials in no variables and the ground ring. -/ def pempty_ring_equiv (R : Type u) [comm_semiring R] : mv_polynomial pempty R ≃+* R := ring_equiv.mk (eval₂ (ring_hom.id R) pempty.elim) ⇑C sorry sorry sorry sorry /-- The algebra isomorphism between multivariable polynomials in no variables and the ground ring. -/ @[simp] theorem pempty_alg_equiv_symm_apply (R : Type u) [comm_semiring R] : ∀ (ᾰ : R), coe_fn (alg_equiv.symm (pempty_alg_equiv R)) ᾰ = coe_fn C ᾰ := fun (ᾰ : R) => Eq.refl (coe_fn (alg_equiv.symm (pempty_alg_equiv R)) ᾰ) /-- The ring isomorphism between multivariable polynomials in a single variable and polynomials over the ground ring. -/ @[simp] theorem punit_ring_equiv_symm_apply (R : Type u) [comm_semiring R] (p : polynomial R) : coe_fn (ring_equiv.symm (punit_ring_equiv R)) p = polynomial.eval₂ C (X PUnit.unit) p := Eq.refl (coe_fn (ring_equiv.symm (punit_ring_equiv R)) p) /-- The ring isomorphism between multivariable polynomials induced by an equivalence of the variables. -/ @[simp] theorem ring_equiv_of_equiv_symm_apply (R : Type u) {S₁ : Type v} {S₂ : Type w} [comm_semiring R] (e : S₁ ≃ S₂) : ∀ (ᾰ : mv_polynomial S₂ R), coe_fn (ring_equiv.symm (ring_equiv_of_equiv R e)) ᾰ = coe_fn (rename ⇑(equiv.symm e)) ᾰ := fun (ᾰ : mv_polynomial S₂ R) => Eq.refl (coe_fn (ring_equiv.symm (ring_equiv_of_equiv R e)) ᾰ) /-- The algebra isomorphism between multivariable polynomials induced by an equivalence of the variables. -/ @[simp] theorem alg_equiv_of_equiv_symm_apply (R : Type u) {S₁ : Type v} {S₂ : Type w} [comm_semiring R] (e : S₁ ≃ S₂) : ∀ (ᾰ : mv_polynomial S₂ R), coe_fn (alg_equiv.symm (alg_equiv_of_equiv R e)) ᾰ = coe_fn (rename ⇑(equiv.symm e)) ᾰ := fun (ᾰ : mv_polynomial S₂ R) => Eq.refl (coe_fn (alg_equiv.symm (alg_equiv_of_equiv R e)) ᾰ) /-- The ring isomorphism between multivariable polynomials induced by a ring isomorphism of the ground ring. -/ def ring_equiv_congr (R : Type u) {S₁ : Type v} {S₂ : Type w} [comm_semiring R] [comm_semiring S₂] (e : R ≃+* S₂) : mv_polynomial S₁ R ≃+* mv_polynomial S₁ S₂ := ring_equiv.mk ⇑(map ↑e) ⇑(map ↑(ring_equiv.symm e)) sorry sorry sorry sorry /-- The function from multivariable polynomials in a sum of two types, to multivariable polynomials in one of the types, with coefficents in multivariable polynomials in the other type. See `sum_ring_equiv` for the ring isomorphism. -/ def sum_to_iter (R : Type u) (S₁ : Type v) (S₂ : Type w) [comm_semiring R] : mv_polynomial (S₁ ⊕ S₂) R →+* mv_polynomial S₁ (mv_polynomial S₂ R) := eval₂_hom (ring_hom.comp C C) fun (bc : S₁ ⊕ S₂) => sum.rec_on bc X (⇑C ∘ X) protected instance is_semiring_hom_sum_to_iter (R : Type u) (S₁ : Type v) (S₂ : Type w) [comm_semiring R] : is_semiring_hom ⇑(sum_to_iter R S₁ S₂) := eval₂.is_semiring_hom (ring_hom.comp C C) fun (n : S₁ ⊕ S₂) => (fun (bc : S₁ ⊕ S₂) => sum.rec_on bc X (⇑C ∘ X)) n @[simp] theorem sum_to_iter_C (R : Type u) (S₁ : Type v) (S₂ : Type w) [comm_semiring R] (a : R) : coe_fn (sum_to_iter R S₁ S₂) (coe_fn C a) = coe_fn C (coe_fn C a) := eval₂_C (ring_hom.comp C C) (fun (n : S₁ ⊕ S₂) => (fun (bc : S₁ ⊕ S₂) => sum.rec_on bc X (⇑C ∘ X)) n) a @[simp] theorem sum_to_iter_Xl (R : Type u) (S₁ : Type v) (S₂ : Type w) [comm_semiring R] (b : S₁) : coe_fn (sum_to_iter R S₁ S₂) (X (sum.inl b)) = X b := eval₂_X (ring_hom.comp C C) (fun (n : S₁ ⊕ S₂) => (fun (bc : S₁ ⊕ S₂) => sum.rec_on bc X (⇑C ∘ X)) n) (sum.inl b) @[simp] theorem sum_to_iter_Xr (R : Type u) (S₁ : Type v) (S₂ : Type w) [comm_semiring R] (c : S₂) : coe_fn (sum_to_iter R S₁ S₂) (X (sum.inr c)) = coe_fn C (X c) := eval₂_X (ring_hom.comp C C) (fun (n : S₁ ⊕ S₂) => (fun (bc : S₁ ⊕ S₂) => sum.rec_on bc X (⇑C ∘ X)) n) (sum.inr c) /-- The function from multivariable polynomials in one type, with coefficents in multivariable polynomials in another type, to multivariable polynomials in the sum of the two types. See `sum_ring_equiv` for the ring isomorphism. -/ def iter_to_sum (R : Type u) (S₁ : Type v) (S₂ : Type w) [comm_semiring R] : mv_polynomial S₁ (mv_polynomial S₂ R) →+* mv_polynomial (S₁ ⊕ S₂) R := eval₂_hom (ring_hom.of (eval₂ C (X ∘ sum.inr))) (X ∘ sum.inl) theorem iter_to_sum_C_C (R : Type u) (S₁ : Type v) (S₂ : Type w) [comm_semiring R] (a : R) : coe_fn (iter_to_sum R S₁ S₂) (coe_fn C (coe_fn C a)) = coe_fn C a := Eq.trans (eval₂_C (ring_hom.of (eval₂ C (X ∘ sum.inr))) (fun (n : S₁) => function.comp X sum.inl n) (coe_fn C a)) (eval₂_C C (fun (n : S₂) => function.comp X sum.inr n) a) theorem iter_to_sum_X (R : Type u) (S₁ : Type v) (S₂ : Type w) [comm_semiring R] (b : S₁) : coe_fn (iter_to_sum R S₁ S₂) (X b) = X (sum.inl b) := eval₂_X (ring_hom.of (eval₂ C (X ∘ sum.inr))) (fun (n : S₁) => function.comp X sum.inl n) b theorem iter_to_sum_C_X (R : Type u) (S₁ : Type v) (S₂ : Type w) [comm_semiring R] (c : S₂) : coe_fn (iter_to_sum R S₁ S₂) (coe_fn C (X c)) = X (sum.inr c) := Eq.trans (eval₂_C (ring_hom.of (eval₂ C (X ∘ sum.inr))) (fun (n : S₁) => function.comp X sum.inl n) (X c)) (eval₂_X C (fun (n : S₂) => function.comp X sum.inr n) c) /-- A helper function for `sum_ring_equiv`. -/ @[simp] theorem mv_polynomial_equiv_mv_polynomial_apply (R : Type u) (S₁ : Type v) (S₂ : Type w) (S₃ : Type x) [comm_semiring R] [comm_semiring S₃] (f : mv_polynomial S₁ R →+* mv_polynomial S₂ S₃) (g : mv_polynomial S₂ S₃ →+* mv_polynomial S₁ R) (hfgC : ∀ (a : S₃), coe_fn f (coe_fn g (coe_fn C a)) = coe_fn C a) (hfgX : ∀ (n : S₂), coe_fn f (coe_fn g (X n)) = X n) (hgfC : ∀ (a : R), coe_fn g (coe_fn f (coe_fn C a)) = coe_fn C a) (hgfX : ∀ (n : S₁), coe_fn g (coe_fn f (X n)) = X n) : ∀ (ᾰ : mv_polynomial S₁ R), coe_fn (mv_polynomial_equiv_mv_polynomial R S₁ S₂ S₃ f g hfgC hfgX hgfC hgfX) ᾰ = coe_fn f ᾰ := fun (ᾰ : mv_polynomial S₁ R) => Eq.refl (coe_fn (mv_polynomial_equiv_mv_polynomial R S₁ S₂ S₃ f g hfgC hfgX hgfC hgfX) ᾰ) /-- The ring isomorphism between multivariable polynomials in a sum of two types, and multivariable polynomials in one of the types, with coefficents in multivariable polynomials in the other type. -/ def sum_ring_equiv (R : Type u) (S₁ : Type v) (S₂ : Type w) [comm_semiring R] : mv_polynomial (S₁ ⊕ S₂) R ≃+* mv_polynomial S₁ (mv_polynomial S₂ R) := mv_polynomial_equiv_mv_polynomial R (S₁ ⊕ S₂) S₁ (mv_polynomial S₂ R) (sum_to_iter R S₁ S₂) (iter_to_sum R S₁ S₂) sorry sorry sorry sorry /-- The ring isomorphism between multivariable polynomials in `option S₁` and polynomials with coefficients in `mv_polynomial S₁ R`. -/ def option_equiv_left (R : Type u) (S₁ : Type v) [comm_semiring R] : mv_polynomial (Option S₁) R ≃+* polynomial (mv_polynomial S₁ R) := ring_equiv.trans (ring_equiv_of_equiv R (equiv.trans (equiv.option_equiv_sum_punit S₁) (equiv.sum_comm S₁ PUnit))) (ring_equiv.trans (sum_ring_equiv R PUnit S₁) (punit_ring_equiv (mv_polynomial S₁ R))) /-- The ring isomorphism between multivariable polynomials in `option S₁` and multivariable polynomials with coefficients in polynomials. -/ def option_equiv_right (R : Type u) (S₁ : Type v) [comm_semiring R] : mv_polynomial (Option S₁) R ≃+* mv_polynomial S₁ (polynomial R) := ring_equiv.trans (ring_equiv_of_equiv R (equiv.option_equiv_sum_punit S₁)) (ring_equiv.trans (sum_ring_equiv R S₁ Unit) (ring_equiv_congr (mv_polynomial Unit R) (punit_ring_equiv R))) /-- The ring isomorphism between multivariable polynomials in `fin (n + 1)` and polynomials over multivariable polynomials in `fin n`. -/ def fin_succ_equiv (R : Type u) [comm_semiring R] (n : ℕ) : mv_polynomial (fin (n + 1)) R ≃+* polynomial (mv_polynomial (fin n) R) := ring_equiv.trans (ring_equiv_of_equiv R (fin_succ_equiv n)) (option_equiv_left R (fin n)) theorem fin_succ_equiv_eq (R : Type u) [comm_semiring R] (n : ℕ) : ↑(fin_succ_equiv R n) = eval₂_hom (ring_hom.comp polynomial.C C) fun (i : fin (n + 1)) => fin.cases polynomial.X (fun (k : fin n) => coe_fn polynomial.C (X k)) i := sorry @[simp] theorem fin_succ_equiv_apply (R : Type u) [comm_semiring R] (n : ℕ) (p : mv_polynomial (fin (n + 1)) R) : coe_fn (fin_succ_equiv R n) p = coe_fn (eval₂_hom (ring_hom.comp polynomial.C C) fun (i : fin (n + 1)) => fin.cases polynomial.X (fun (k : fin n) => coe_fn polynomial.C (X k)) i) p := sorry theorem fin_succ_equiv_comp_C_eq_C {R : Type u} [comm_semiring R] (n : ℕ) : ring_hom.comp (ring_equiv.to_ring_hom (ring_equiv.symm (fin_succ_equiv R n))) (ring_hom.comp polynomial.C C) = C := sorry end Mathlib
9161b5339fa042dbbd2385c820a08d4da1db58d4
fecda8e6b848337561d6467a1e30cf23176d6ad0
/src/category_theory/limits/limits.lean
8b1da2ee8beef896a644948ad2f2ab9366c0515f
[ "Apache-2.0" ]
permissive
spolu/mathlib
bacf18c3d2a561d00ecdc9413187729dd1f705ed
480c92cdfe1cf3c2d083abded87e82162e8814f4
refs/heads/master
1,671,684,094,325
1,600,736,045,000
1,600,736,045,000
297,564,749
1
0
null
1,600,758,368,000
1,600,758,367,000
null
UTF-8
Lean
false
false
61,554
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Mario Carneiro, Scott Morrison, Floris van Doorn -/ import category_theory.adjunction.basic import category_theory.limits.cones import category_theory.reflects_isomorphisms /-! # Limits and colimits We set up the general theory of limits and colimits in a category. In this introduction we only describe the setup for limits; it is repeated, with slightly different names, for colimits. The three main structures involved are * `is_limit c`, for `c : cone F`, `F : J ⥤ C`, expressing that `c` is a limit cone, * `limit_cone F`, which consists of a choice of cone for `F` and the fact it is a limit cone, and * `has_limit F`, asserting the mere existence of some limit cone for `F`. `has_limit` is a propositional typeclass (it's important that it is a proposition merely asserting the existence of a limit, as otherwise we would have non-defeq problems from incompatible instances). Typically there are two different ways one can use the limits library: 1. working with particular cones, and terms of type `is_limit` 2. working solely with `has_limit`. While `has_limit` only asserts the existence of a limit cone, we happily use the axiom of choice in mathlib, so there are convenience functions all depending on `has_limit F`: * `limit F : C`, producing some limit object (of course all such are isomorphic) * `limit.π F j : limit F ⟶ F.obj j`, the morphisms out of the limit, * `limit.lift F c : c.X ⟶ limit F`, the universal morphism from any other `c : cone F`, etc. Key to using the `has_limit` interface is that there is an `@[ext]` lemma stating that to check `f = g`, for `f g : Z ⟶ limit F`, it suffices to check `f ≫ limit.π F j = g ≫ limit.π F j` for every `j`. This, combined with `@[simp]` lemmas, makes it possible to prove many easy facts about limits using automation (e.g. `tidy`). There are abbreviations `has_limits_of_shape J C` and `has_limits C` asserting the existence of classes of limits. Later more are introduced, for finite limits, special shapes of limits, etc. Ideally, many results about limits should be stated first in terms of `is_limit`, and then a result in terms of `has_limit` derived from this. At this point, however, this is far from uniformly achieved in mathlib --- often statements are only written in terms of `has_limit`. ## Implementation At present we simply say everything twice, in order to handle both limits and colimits. It would be highly desirable to have some automation support, e.g. a `@[dualize]` attribute that behaves similarly to `@[to_additive]`. ## References * [Stacks: Limits and colimits](https://stacks.math.columbia.edu/tag/002D) -/ noncomputable theory open category_theory category_theory.category category_theory.functor opposite namespace category_theory.limits universes v u u' u'' w -- declare the `v`'s first; see `category_theory.category` for an explanation variables {J K : Type v} [small_category J] [small_category K] variables {C : Type u} [category.{v} C] variables {F : J ⥤ C} /-- A cone `t` on `F` is a limit cone if each cone on `F` admits a unique cone morphism to `t`. See https://stacks.math.columbia.edu/tag/002E. -/ @[nolint has_inhabited_instance] structure is_limit (t : cone F) := (lift : Π (s : cone F), s.X ⟶ t.X) (fac' : ∀ (s : cone F) (j : J), lift s ≫ t.π.app j = s.π.app j . obviously) (uniq' : ∀ (s : cone F) (m : s.X ⟶ t.X) (w : ∀ j : J, m ≫ t.π.app j = s.π.app j), m = lift s . obviously) restate_axiom is_limit.fac' attribute [simp, reassoc] is_limit.fac restate_axiom is_limit.uniq' namespace is_limit instance subsingleton {t : cone F} : subsingleton (is_limit t) := ⟨by intros P Q; cases P; cases Q; congr; ext; solve_by_elim⟩ /- Repackaging the definition in terms of cone morphisms. -/ /-- The universal morphism from any other cone to a limit cone. -/ @[simps] def lift_cone_morphism {t : cone F} (h : is_limit t) (s : cone F) : s ⟶ t := { hom := h.lift s } lemma uniq_cone_morphism {s t : cone F} (h : is_limit t) {f f' : s ⟶ t} : f = f' := have ∀ {g : s ⟶ t}, g = h.lift_cone_morphism s, by intro g; ext; exact h.uniq _ _ g.w, this.trans this.symm /-- Alternative constructor for `is_limit`, providing a morphism of cones rather than a morphism between the cone points and separately the factorisation condition. -/ @[simps] def mk_cone_morphism {t : cone F} (lift : Π (s : cone F), s ⟶ t) (uniq' : ∀ (s : cone F) (m : s ⟶ t), m = lift s) : is_limit t := { lift := λ s, (lift s).hom, uniq' := λ s m w, have cone_morphism.mk m w = lift s, by apply uniq', congr_arg cone_morphism.hom this } /-- Limit cones on `F` are unique up to isomorphism. -/ @[simps] def unique_up_to_iso {s t : cone F} (P : is_limit s) (Q : is_limit t) : s ≅ t := { hom := Q.lift_cone_morphism s, inv := P.lift_cone_morphism t, hom_inv_id' := P.uniq_cone_morphism, inv_hom_id' := Q.uniq_cone_morphism } /-- Any cone morphism between limit cones is an isomorphism. -/ def hom_is_iso {s t : cone F} (P : is_limit s) (Q : is_limit t) (f : s ⟶ t) : is_iso f := { inv := P.lift_cone_morphism t, hom_inv_id' := P.uniq_cone_morphism, inv_hom_id' := Q.uniq_cone_morphism, } /-- Limits of `F` are unique up to isomorphism. -/ -- We may later want to prove the coherence of these isomorphisms. def cone_point_unique_up_to_iso {s t : cone F} (P : is_limit s) (Q : is_limit t) : s.X ≅ t.X := (cones.forget F).map_iso (unique_up_to_iso P Q) @[simp, reassoc] lemma cone_point_unique_up_to_iso_hom_comp {s t : cone F} (P : is_limit s) (Q : is_limit t) (j : J) : (cone_point_unique_up_to_iso P Q).hom ≫ t.π.app j = s.π.app j := (unique_up_to_iso P Q).hom.w _ @[simp, reassoc] lemma cone_point_unique_up_to_iso_inv_comp {s t : cone F} (P : is_limit s) (Q : is_limit t) (j : J) : (cone_point_unique_up_to_iso P Q).inv ≫ s.π.app j = t.π.app j := (unique_up_to_iso P Q).inv.w _ /-- Transport evidence that a cone is a limit cone across an isomorphism of cones. -/ def of_iso_limit {r t : cone F} (P : is_limit r) (i : r ≅ t) : is_limit t := is_limit.mk_cone_morphism (λ s, P.lift_cone_morphism s ≫ i.hom) (λ s m, by rw ←i.comp_inv_eq; apply P.uniq_cone_morphism) /-- If the canonical morphism from a cone point to a limiting cone point is an iso, then the first cone was limiting also. -/ def of_point_iso {r t : cone F} (P : is_limit r) [i : is_iso (P.lift t)] : is_limit t := of_iso_limit P begin haveI : is_iso (P.lift_cone_morphism t).hom := i, haveI : is_iso (P.lift_cone_morphism t) := cones.cone_iso_of_hom_iso _, symmetry, apply as_iso (P.lift_cone_morphism t), end variables {t : cone F} lemma hom_lift (h : is_limit t) {W : C} (m : W ⟶ t.X) : m = h.lift { X := W, π := { app := λ b, m ≫ t.π.app b } } := h.uniq { X := W, π := { app := λ b, m ≫ t.π.app b } } m (λ b, rfl) /-- Two morphisms into a limit are equal if their compositions with each cone morphism are equal. -/ lemma hom_ext (h : is_limit t) {W : C} {f f' : W ⟶ t.X} (w : ∀ j, f ≫ t.π.app j = f' ≫ t.π.app j) : f = f' := by rw [h.hom_lift f, h.hom_lift f']; congr; exact funext w /-- Given a right adjoint functor between categories of cones, the image of a limit cone is a limit cone. -/ def of_right_adjoint {D : Type u'} [category.{v} D] {G : K ⥤ D} (h : cone G ⥤ cone F) [is_right_adjoint h] {c : cone G} (t : is_limit c) : is_limit (h.obj c) := mk_cone_morphism (λ s, (adjunction.of_right_adjoint h).hom_equiv s c (t.lift_cone_morphism _)) (λ s m, (adjunction.eq_hom_equiv_apply _ _ _).2 t.uniq_cone_morphism) /-- Given two functors which have equivalent categories of cones, we can transport a limiting cone across the equivalence. -/ def of_cone_equiv {D : Type u'} [category.{v} D] {G : K ⥤ D} (h : cone G ≌ cone F) {c : cone G} : is_limit (h.functor.obj c) ≃ is_limit c := { to_fun := λ P, of_iso_limit (of_right_adjoint h.inverse P) (h.unit_iso.symm.app c), inv_fun := of_right_adjoint h.functor, left_inv := by tidy, right_inv := by tidy, } /-- A cone postcomposed with a natural isomorphism is a limit cone if and only if the original cone is. -/ def postcompose_hom_equiv {F G : J ⥤ C} (α : F ≅ G) (c : cone F) : is_limit ((cones.postcompose α.hom).obj c) ≃ is_limit c := of_cone_equiv (cones.postcompose_equivalence α) /-- A cone postcomposed with the inverse of a natural isomorphism is a limit cone if and only if the original cone is. -/ def postcompose_inv_equiv {F G : J ⥤ C} (α : F ≅ G) (c : cone G) : is_limit ((cones.postcompose α.inv).obj c) ≃ is_limit c := postcompose_hom_equiv α.symm c /-- The cone points of two limit cones for naturally isomorphic functors are themselves isomorphic. -/ @[simps] def cone_points_iso_of_nat_iso {F G : J ⥤ C} {s : cone F} {t : cone G} (P : is_limit s) (Q : is_limit t) (w : F ≅ G) : s.X ≅ t.X := { hom := Q.lift ((limits.cones.postcompose w.hom).obj s), inv := P.lift ((limits.cones.postcompose w.inv).obj t), hom_inv_id' := by { apply hom_ext P, tidy, }, inv_hom_id' := by { apply hom_ext Q, tidy, }, } section equivalence open category_theory.equivalence /-- If `s : cone F` is a limit cone, so is `s` whiskered by an equivalence `e`. -/ def whisker_equivalence {s : cone F} (P : is_limit s) (e : K ≌ J) : is_limit (s.whisker e.functor) := of_right_adjoint (cones.whiskering_equivalence e).functor P /-- We can prove two cone points `(s : cone F).X` and `(t.cone F).X` are isomorphic if * both cones are limit cones * their indexing categories are equivalent via some `e : J ≌ K`, * the triangle of functors commutes up to a natural isomorphism: `e.functor ⋙ G ≅ F`. This is the most general form of uniqueness of cone points, allowing relabelling of both the indexing category (up to equivalence) and the functor (up to natural isomorphism). -/ @[simps] def cone_points_iso_of_equivalence {F : J ⥤ C} {s : cone F} {G : K ⥤ C} {t : cone G} (P : is_limit s) (Q : is_limit t) (e : J ≌ K) (w : e.functor ⋙ G ≅ F) : s.X ≅ t.X := let w' : e.inverse ⋙ F ≅ G := (iso_whisker_left e.inverse w).symm ≪≫ inv_fun_id_assoc e G in { hom := Q.lift ((cones.equivalence_of_reindexing e.symm w').functor.obj s), inv := P.lift ((cones.equivalence_of_reindexing e w).functor.obj t), hom_inv_id' := begin apply hom_ext P, intros j, dsimp, simp only [limits.cone.whisker_π, limits.cones.postcompose_obj_π, fac, whisker_left_app, assoc, id_comp, inv_fun_id_assoc_hom_app, fac_assoc, nat_trans.comp_app], rw [counit_functor, ←functor.comp_map, w.hom.naturality], simp, end, inv_hom_id' := by { apply hom_ext Q, tidy, }, } end equivalence /-- The universal property of a limit cone: a map `W ⟶ X` is the same as a cone on `F` with vertex `W`. -/ def hom_iso (h : is_limit t) (W : C) : (W ⟶ t.X) ≅ ((const J).obj W ⟶ F) := { hom := λ f, (t.extend f).π, inv := λ π, h.lift { X := W, π := π }, hom_inv_id' := by ext f; apply h.hom_ext; intro j; simp; dsimp; refl } @[simp] lemma hom_iso_hom (h : is_limit t) {W : C} (f : W ⟶ t.X) : (is_limit.hom_iso h W).hom f = (t.extend f).π := rfl /-- The limit of `F` represents the functor taking `W` to the set of cones on `F` with vertex `W`. -/ def nat_iso (h : is_limit t) : yoneda.obj t.X ≅ F.cones := nat_iso.of_components (λ W, is_limit.hom_iso h (unop W)) (by tidy). /-- Another, more explicit, formulation of the universal property of a limit cone. See also `hom_iso`. -/ def hom_iso' (h : is_limit t) (W : C) : ((W ⟶ t.X) : Type v) ≅ { p : Π j, W ⟶ F.obj j // ∀ {j j'} (f : j ⟶ j'), p j ≫ F.map f = p j' } := h.hom_iso W ≪≫ { hom := λ π, ⟨λ j, π.app j, λ j j' f, by convert ←(π.naturality f).symm; apply id_comp⟩, inv := λ p, { app := λ j, p.1 j, naturality' := λ j j' f, begin dsimp, rw [id_comp], exact (p.2 f).symm end } } /-- If G : C → D is a faithful functor which sends t to a limit cone, then it suffices to check that the induced maps for the image of t can be lifted to maps of C. -/ def of_faithful {t : cone F} {D : Type u'} [category.{v} D] (G : C ⥤ D) [faithful G] (ht : is_limit (G.map_cone t)) (lift : Π (s : cone F), s.X ⟶ t.X) (h : ∀ s, G.map (lift s) = ht.lift (G.map_cone s)) : is_limit t := { lift := lift, fac' := λ s j, by apply G.map_injective; rw [G.map_comp, h]; apply ht.fac, uniq' := λ s m w, begin apply G.map_injective, rw h, refine ht.uniq (G.map_cone s) _ (λ j, _), convert ←congr_arg (λ f, G.map f) (w j), apply G.map_comp end } /-- If `F` and `G` are naturally isomorphic, then `F.map_cone c` being a limit implies `G.map_cone c` is also a limit. -/ def map_cone_equiv {D : Type u'} [category.{v} D] {K : J ⥤ C} {F G : C ⥤ D} (h : F ≅ G) {c : cone K} (t : is_limit (F.map_cone c)) : is_limit (G.map_cone c) := { lift := λ s, t.lift ((cones.postcompose (iso_whisker_left K h).inv).obj s) ≫ h.hom.app c.X, fac' := λ s j, begin slice_lhs 2 3 {erw ← h.hom.naturality (c.π.app j)}, slice_lhs 1 2 {erw t.fac ((cones.postcompose (iso_whisker_left K h).inv).obj s) j}, dsimp, slice_lhs 2 3 {rw iso.inv_hom_id_app}, rw category.comp_id, end, uniq' := λ s m J, begin rw ← cancel_mono (h.inv.app c.X), apply t.hom_ext, intro j, dsimp, slice_lhs 2 3 {erw ← h.inv.naturality (c.π.app j)}, slice_lhs 1 2 {erw J j}, conv_rhs {congr, rw [category.assoc, iso.hom_inv_id_app, comp_id]}, apply (t.fac ((cones.postcompose (iso_whisker_left K h).inv).obj s) j).symm end } /-- A cone is a limit cone exactly if there is a unique cone morphism from any other cone. -/ def iso_unique_cone_morphism {t : cone F} : is_limit t ≅ Π s, unique (s ⟶ t) := { hom := λ h s, { default := h.lift_cone_morphism s, uniq := λ _, h.uniq_cone_morphism }, inv := λ h, { lift := λ s, (h s).default.hom, uniq' := λ s f w, congr_arg cone_morphism.hom ((h s).uniq ⟨f, w⟩) } } namespace of_nat_iso variables {X : C} (h : yoneda.obj X ≅ F.cones) /-- If `F.cones` is represented by `X`, each morphism `f : Y ⟶ X` gives a cone with cone point `Y`. -/ def cone_of_hom {Y : C} (f : Y ⟶ X) : cone F := { X := Y, π := h.hom.app (op Y) f } /-- If `F.cones` is represented by `X`, each cone `s` gives a morphism `s.X ⟶ X`. -/ def hom_of_cone (s : cone F) : s.X ⟶ X := h.inv.app (op s.X) s.π @[simp] lemma cone_of_hom_of_cone (s : cone F) : cone_of_hom h (hom_of_cone h s) = s := begin dsimp [cone_of_hom, hom_of_cone], cases s, congr, dsimp, exact congr_fun (congr_fun (congr_arg nat_trans.app h.inv_hom_id) (op s_X)) s_π, end @[simp] lemma hom_of_cone_of_hom {Y : C} (f : Y ⟶ X) : hom_of_cone h (cone_of_hom h f) = f := congr_fun (congr_fun (congr_arg nat_trans.app h.hom_inv_id) (op Y)) f /-- If `F.cones` is represented by `X`, the cone corresponding to the identity morphism on `X` will be a limit cone. -/ def limit_cone : cone F := cone_of_hom h (𝟙 X) /-- If `F.cones` is represented by `X`, the cone corresponding to a morphism `f : Y ⟶ X` is the limit cone extended by `f`. -/ lemma cone_of_hom_fac {Y : C} (f : Y ⟶ X) : cone_of_hom h f = (limit_cone h).extend f := begin dsimp [cone_of_hom, limit_cone, cone.extend], congr' with j, have t := congr_fun (h.hom.naturality f.op) (𝟙 X), dsimp at t, simp only [comp_id] at t, rw congr_fun (congr_arg nat_trans.app t) j, refl, end /-- If `F.cones` is represented by `X`, any cone is the extension of the limit cone by the corresponding morphism. -/ lemma cone_fac (s : cone F) : (limit_cone h).extend (hom_of_cone h s) = s := begin rw ←cone_of_hom_of_cone h s, conv_lhs { simp only [hom_of_cone_of_hom] }, apply (cone_of_hom_fac _ _).symm, end end of_nat_iso section open of_nat_iso /-- If `F.cones` is representable, then the cone corresponding to the identity morphism on the representing object is a limit cone. -/ def of_nat_iso {X : C} (h : yoneda.obj X ≅ F.cones) : is_limit (limit_cone h) := { lift := λ s, hom_of_cone h s, fac' := λ s j, begin have h := cone_fac h s, cases s, injection h with h₁ h₂, simp only [heq_iff_eq] at h₂, conv_rhs { rw ← h₂ }, refl, end, uniq' := λ s m w, begin rw ←hom_of_cone_of_hom h m, congr, rw cone_of_hom_fac, dsimp, cases s, congr' with j, exact w j, end } end end is_limit /-- A cocone `t` on `F` is a colimit cocone if each cocone on `F` admits a unique cocone morphism from `t`. See https://stacks.math.columbia.edu/tag/002F. -/ @[nolint has_inhabited_instance] structure is_colimit (t : cocone F) := (desc : Π (s : cocone F), t.X ⟶ s.X) (fac' : ∀ (s : cocone F) (j : J), t.ι.app j ≫ desc s = s.ι.app j . obviously) (uniq' : ∀ (s : cocone F) (m : t.X ⟶ s.X) (w : ∀ j : J, t.ι.app j ≫ m = s.ι.app j), m = desc s . obviously) restate_axiom is_colimit.fac' attribute [simp,reassoc] is_colimit.fac restate_axiom is_colimit.uniq' namespace is_colimit instance subsingleton {t : cocone F} : subsingleton (is_colimit t) := ⟨by intros P Q; cases P; cases Q; congr; ext; solve_by_elim⟩ /- Repackaging the definition in terms of cocone morphisms. -/ /-- The universal morphism from a colimit cocone to any other cocone. -/ @[simps] def desc_cocone_morphism {t : cocone F} (h : is_colimit t) (s : cocone F) : t ⟶ s := { hom := h.desc s } lemma uniq_cocone_morphism {s t : cocone F} (h : is_colimit t) {f f' : t ⟶ s} : f = f' := have ∀ {g : t ⟶ s}, g = h.desc_cocone_morphism s, by intro g; ext; exact h.uniq _ _ g.w, this.trans this.symm /-- Alternative constructor for `is_colimit`, providing a morphism of cocones rather than a morphism between the cocone points and separately the factorisation condition. -/ @[simps] def mk_cocone_morphism {t : cocone F} (desc : Π (s : cocone F), t ⟶ s) (uniq' : ∀ (s : cocone F) (m : t ⟶ s), m = desc s) : is_colimit t := { desc := λ s, (desc s).hom, uniq' := λ s m w, have cocone_morphism.mk m w = desc s, by apply uniq', congr_arg cocone_morphism.hom this } /-- Colimit cocones on `F` are unique up to isomorphism. -/ @[simps] def unique_up_to_iso {s t : cocone F} (P : is_colimit s) (Q : is_colimit t) : s ≅ t := { hom := P.desc_cocone_morphism t, inv := Q.desc_cocone_morphism s, hom_inv_id' := P.uniq_cocone_morphism, inv_hom_id' := Q.uniq_cocone_morphism } /-- Any cocone morphism between colimit cocones is an isomorphism. -/ def hom_is_iso {s t : cocone F} (P : is_colimit s) (Q : is_colimit t) (f : s ⟶ t) : is_iso f := { inv := Q.desc_cocone_morphism s, hom_inv_id' := P.uniq_cocone_morphism, inv_hom_id' := Q.uniq_cocone_morphism, } /-- Colimits of `F` are unique up to isomorphism. -/ -- We may later want to prove the coherence of these isomorphisms. def cocone_point_unique_up_to_iso {s t : cocone F} (P : is_colimit s) (Q : is_colimit t) : s.X ≅ t.X := (cocones.forget F).map_iso (unique_up_to_iso P Q) @[simp, reassoc] lemma comp_cocone_point_unique_up_to_iso_hom {s t : cocone F} (P : is_colimit s) (Q : is_colimit t) (j : J) : s.ι.app j ≫ (cocone_point_unique_up_to_iso P Q).hom = t.ι.app j := (unique_up_to_iso P Q).hom.w _ @[simp, reassoc] lemma comp_cocone_point_unique_up_to_iso_inv {s t : cocone F} (P : is_colimit s) (Q : is_colimit t) (j : J) : t.ι.app j ≫ (cocone_point_unique_up_to_iso P Q).inv = s.ι.app j := (unique_up_to_iso P Q).inv.w _ /-- Transport evidence that a cocone is a colimit cocone across an isomorphism of cocones. -/ def of_iso_colimit {r t : cocone F} (P : is_colimit r) (i : r ≅ t) : is_colimit t := is_colimit.mk_cocone_morphism (λ s, i.inv ≫ P.desc_cocone_morphism s) (λ s m, by rw i.eq_inv_comp; apply P.uniq_cocone_morphism) /-- If the canonical morphism to a cocone point from a colimiting cocone point is an iso, then the first cocone was colimiting also. -/ def of_point_iso {r t : cocone F} (P : is_colimit r) [i : is_iso (P.desc t)] : is_colimit t := of_iso_colimit P begin haveI : is_iso (P.desc_cocone_morphism t).hom := i, haveI : is_iso (P.desc_cocone_morphism t) := cocones.cocone_iso_of_hom_iso _, apply as_iso (P.desc_cocone_morphism t), end variables {t : cocone F} lemma hom_desc (h : is_colimit t) {W : C} (m : t.X ⟶ W) : m = h.desc { X := W, ι := { app := λ b, t.ι.app b ≫ m, naturality' := by intros; erw [←assoc, t.ι.naturality, comp_id, comp_id] } } := h.uniq { X := W, ι := { app := λ b, t.ι.app b ≫ m, naturality' := _ } } m (λ b, rfl) /-- Two morphisms out of a colimit are equal if their compositions with each cocone morphism are equal. -/ lemma hom_ext (h : is_colimit t) {W : C} {f f' : t.X ⟶ W} (w : ∀ j, t.ι.app j ≫ f = t.ι.app j ≫ f') : f = f' := by rw [h.hom_desc f, h.hom_desc f']; congr; exact funext w /-- Given a left adjoint functor between categories of cocones, the image of a colimit cocone is a colimit cocone. -/ def of_left_adjoint {D : Type u'} [category.{v} D] {G : K ⥤ D} (h : cocone G ⥤ cocone F) [is_left_adjoint h] {c : cocone G} (t : is_colimit c) : is_colimit (h.obj c) := mk_cocone_morphism (λ s, ((adjunction.of_left_adjoint h).hom_equiv c s).symm (t.desc_cocone_morphism _)) (λ s m, (adjunction.hom_equiv_apply_eq _ _ _).1 t.uniq_cocone_morphism) /-- Given two functors which have equivalent categories of cocones, we can transport a colimiting cocone across the equivalence. -/ def of_cocone_equiv {D : Type u'} [category.{v} D] {G : K ⥤ D} (h : cocone G ≌ cocone F) {c : cocone G} : is_colimit (h.functor.obj c) ≃ is_colimit c := { to_fun := λ P, of_iso_colimit (of_left_adjoint h.inverse P) (h.unit_iso.symm.app c), inv_fun := of_left_adjoint h.functor, left_inv := by tidy, right_inv := by tidy, } /-- A cocone precomposed with a natural isomorphism is a colimit cocone if and only if the original cocone is. -/ def precompose_hom_equiv {F G : J ⥤ C} (α : F ≅ G) (c : cocone G) : is_colimit ((cocones.precompose α.hom).obj c) ≃ is_colimit c := of_cocone_equiv (cocones.precompose_equivalence α) /-- A cocone precomposed with the inverse of a natural isomorphism is a colimit cocone if and only if the original cocone is. -/ def precompose_inv_equiv {F G : J ⥤ C} (α : F ≅ G) (c : cocone F) : is_colimit ((cocones.precompose α.inv).obj c) ≃ is_colimit c := precompose_hom_equiv α.symm c /-- The cocone points of two colimit cocones for naturally isomorphic functors are themselves isomorphic. -/ @[simps] def cocone_points_iso_of_nat_iso {F G : J ⥤ C} {s : cocone F} {t : cocone G} (P : is_colimit s) (Q : is_colimit t) (w : F ≅ G) : s.X ≅ t.X := { hom := P.desc ((limits.cocones.precompose w.hom).obj t), inv := Q.desc ((limits.cocones.precompose w.inv).obj s), hom_inv_id' := by { apply hom_ext P, tidy, }, inv_hom_id' := by { apply hom_ext Q, tidy, }, } section equivalence open category_theory.equivalence /-- If `s : cone F` is a limit cone, so is `s` whiskered by an equivalence `e`. -/ def whisker_equivalence {s : cocone F} (P : is_colimit s) (e : K ≌ J) : is_colimit (s.whisker e.functor) := of_left_adjoint (cocones.whiskering_equivalence e).functor P /-- We can prove two cocone points `(s : cocone F).X` and `(t.cocone F).X` are isomorphic if * both cocones are colimit ccoones * their indexing categories are equivalent via some `e : J ≌ K`, * the triangle of functors commutes up to a natural isomorphism: `e.functor ⋙ G ≅ F`. This is the most general form of uniqueness of cocone points, allowing relabelling of both the indexing category (up to equivalence) and the functor (up to natural isomorphism). -/ @[simps] def cocone_points_iso_of_equivalence {F : J ⥤ C} {s : cocone F} {G : K ⥤ C} {t : cocone G} (P : is_colimit s) (Q : is_colimit t) (e : J ≌ K) (w : e.functor ⋙ G ≅ F) : s.X ≅ t.X := let w' : e.inverse ⋙ F ≅ G := (iso_whisker_left e.inverse w).symm ≪≫ inv_fun_id_assoc e G in { hom := P.desc ((cocones.equivalence_of_reindexing e w).functor.obj t), inv := Q.desc ((cocones.equivalence_of_reindexing e.symm w').functor.obj s), hom_inv_id' := begin apply hom_ext P, intros j, dsimp, simp only [limits.cocone.whisker_ι, fac, inv_fun_id_assoc_inv_app, whisker_left_app, assoc, comp_id, limits.cocones.precompose_obj_ι, fac_assoc, nat_trans.comp_app], rw [←functor_unit, ←functor.comp_map, ←w.inv.naturality_assoc], dsimp, simp, end, inv_hom_id' := by { apply hom_ext Q, tidy, }, } end equivalence /-- The universal property of a colimit cocone: a map `X ⟶ W` is the same as a cocone on `F` with vertex `W`. -/ def hom_iso (h : is_colimit t) (W : C) : (t.X ⟶ W) ≅ (F ⟶ (const J).obj W) := { hom := λ f, (t.extend f).ι, inv := λ ι, h.desc { X := W, ι := ι }, hom_inv_id' := by ext f; apply h.hom_ext; intro j; simp; dsimp; refl } @[simp] lemma hom_iso_hom (h : is_colimit t) {W : C} (f : t.X ⟶ W) : (is_colimit.hom_iso h W).hom f = (t.extend f).ι := rfl /-- The colimit of `F` represents the functor taking `W` to the set of cocones on `F` with vertex `W`. -/ def nat_iso (h : is_colimit t) : coyoneda.obj (op t.X) ≅ F.cocones := nat_iso.of_components (is_colimit.hom_iso h) (by intros; ext; dsimp; rw ←assoc; refl) /-- Another, more explicit, formulation of the universal property of a colimit cocone. See also `hom_iso`. -/ def hom_iso' (h : is_colimit t) (W : C) : ((t.X ⟶ W) : Type v) ≅ { p : Π j, F.obj j ⟶ W // ∀ {j j' : J} (f : j ⟶ j'), F.map f ≫ p j' = p j } := h.hom_iso W ≪≫ { hom := λ ι, ⟨λ j, ι.app j, λ j j' f, by convert ←(ι.naturality f); apply comp_id⟩, inv := λ p, { app := λ j, p.1 j, naturality' := λ j j' f, begin dsimp, rw [comp_id], exact (p.2 f) end } } /-- If G : C → D is a faithful functor which sends t to a colimit cocone, then it suffices to check that the induced maps for the image of t can be lifted to maps of C. -/ def of_faithful {t : cocone F} {D : Type u'} [category.{v} D] (G : C ⥤ D) [faithful G] (ht : is_colimit (G.map_cocone t)) (desc : Π (s : cocone F), t.X ⟶ s.X) (h : ∀ s, G.map (desc s) = ht.desc (G.map_cocone s)) : is_colimit t := { desc := desc, fac' := λ s j, by apply G.map_injective; rw [G.map_comp, h]; apply ht.fac, uniq' := λ s m w, begin apply G.map_injective, rw h, refine ht.uniq (G.map_cocone s) _ (λ j, _), convert ←congr_arg (λ f, G.map f) (w j), apply G.map_comp end } /-- A cocone is a colimit cocone exactly if there is a unique cocone morphism from any other cocone. -/ def iso_unique_cocone_morphism {t : cocone F} : is_colimit t ≅ Π s, unique (t ⟶ s) := { hom := λ h s, { default := h.desc_cocone_morphism s, uniq := λ _, h.uniq_cocone_morphism }, inv := λ h, { desc := λ s, (h s).default.hom, uniq' := λ s f w, congr_arg cocone_morphism.hom ((h s).uniq ⟨f, w⟩) } } namespace of_nat_iso variables {X : C} (h : coyoneda.obj (op X) ≅ F.cocones) /-- If `F.cocones` is corepresented by `X`, each morphism `f : X ⟶ Y` gives a cocone with cone point `Y`. -/ def cocone_of_hom {Y : C} (f : X ⟶ Y) : cocone F := { X := Y, ι := h.hom.app Y f } /-- If `F.cocones` is corepresented by `X`, each cocone `s` gives a morphism `X ⟶ s.X`. -/ def hom_of_cocone (s : cocone F) : X ⟶ s.X := h.inv.app s.X s.ι @[simp] lemma cocone_of_hom_of_cocone (s : cocone F) : cocone_of_hom h (hom_of_cocone h s) = s := begin dsimp [cocone_of_hom, hom_of_cocone], cases s, congr, dsimp, exact congr_fun (congr_fun (congr_arg nat_trans.app h.inv_hom_id) s_X) s_ι, end @[simp] lemma hom_of_cocone_of_hom {Y : C} (f : X ⟶ Y) : hom_of_cocone h (cocone_of_hom h f) = f := congr_fun (congr_fun (congr_arg nat_trans.app h.hom_inv_id) Y) f /-- If `F.cocones` is corepresented by `X`, the cocone corresponding to the identity morphism on `X` will be a colimit cocone. -/ def colimit_cocone : cocone F := cocone_of_hom h (𝟙 X) /-- If `F.cocones` is corepresented by `X`, the cocone corresponding to a morphism `f : Y ⟶ X` is the colimit cocone extended by `f`. -/ lemma cocone_of_hom_fac {Y : C} (f : X ⟶ Y) : cocone_of_hom h f = (colimit_cocone h).extend f := begin dsimp [cocone_of_hom, colimit_cocone, cocone.extend], congr' with j, have t := congr_fun (h.hom.naturality f) (𝟙 X), dsimp at t, simp only [id_comp] at t, rw congr_fun (congr_arg nat_trans.app t) j, refl, end /-- If `F.cocones` is corepresented by `X`, any cocone is the extension of the colimit cocone by the corresponding morphism. -/ lemma cocone_fac (s : cocone F) : (colimit_cocone h).extend (hom_of_cocone h s) = s := begin rw ←cocone_of_hom_of_cocone h s, conv_lhs { simp only [hom_of_cocone_of_hom] }, apply (cocone_of_hom_fac _ _).symm, end end of_nat_iso section open of_nat_iso /-- If `F.cocones` is corepresentable, then the cocone corresponding to the identity morphism on the representing object is a colimit cocone. -/ def of_nat_iso {X : C} (h : coyoneda.obj (op X) ≅ F.cocones) : is_colimit (colimit_cocone h) := { desc := λ s, hom_of_cocone h s, fac' := λ s j, begin have h := cocone_fac h s, cases s, injection h with h₁ h₂, simp only [heq_iff_eq] at h₂, conv_rhs { rw ← h₂ }, refl, end, uniq' := λ s m w, begin rw ←hom_of_cocone_of_hom h m, congr, rw cocone_of_hom_fac, dsimp, cases s, congr' with j, exact w j, end } end end is_colimit section limit /-- `limit_cone F` contains a cone over `F` together with the information that it is a limit. -/ @[nolint has_inhabited_instance] structure limit_cone (F : J ⥤ C) := (cone : cone F) (is_limit : is_limit cone) /-- `has_limit F` represents the mere existence of a limit for `F`. -/ class has_limit (F : J ⥤ C) : Prop := mk' :: (exists_limit : nonempty (limit_cone F)) lemma has_limit.mk {F : J ⥤ C} (d : limit_cone F) : has_limit F := ⟨nonempty.intro d⟩ /-- Use the axiom of choice to extract explicit `limit_cone F` from `has_limit F`. -/ def get_limit_cone (F : J ⥤ C) [has_limit F] : limit_cone F := classical.choice $ has_limit.exists_limit variables (J C) /-- `C` has limits of shape `J` if there exists a limit for every functor `F : J ⥤ C`. -/ class has_limits_of_shape : Prop := (has_limit : Π F : J ⥤ C, has_limit F) /-- `C` has all (small) limits if it has limits of every shape. -/ class has_limits : Prop := (has_limits_of_shape : Π (J : Type v) [𝒥 : small_category J], has_limits_of_shape J C) variables {J C} @[priority 100] -- see Note [lower instance priority] instance has_limit_of_has_limits_of_shape {J : Type v} [small_category J] [H : has_limits_of_shape J C] (F : J ⥤ C) : has_limit F := has_limits_of_shape.has_limit F @[priority 100] -- see Note [lower instance priority] instance has_limits_of_shape_of_has_limits {J : Type v} [small_category J] [H : has_limits C] : has_limits_of_shape J C := has_limits.has_limits_of_shape J /- Interface to the `has_limit` class. -/ /-- An arbitrary choice of limit cone for a functor. -/ def limit.cone (F : J ⥤ C) [has_limit F] : cone F := (get_limit_cone F).cone /-- An arbitrary choice of limit object of a functor. -/ def limit (F : J ⥤ C) [has_limit F] := (limit.cone F).X /-- The projection from the limit object to a value of the functor. -/ def limit.π (F : J ⥤ C) [has_limit F] (j : J) : limit F ⟶ F.obj j := (limit.cone F).π.app j @[simp] lemma limit.cone_X {F : J ⥤ C} [has_limit F] : (limit.cone F).X = limit F := rfl @[simp] lemma limit.cone_π {F : J ⥤ C} [has_limit F] (j : J) : (limit.cone F).π.app j = limit.π _ j := rfl @[simp, reassoc] lemma limit.w (F : J ⥤ C) [has_limit F] {j j' : J} (f : j ⟶ j') : limit.π F j ≫ F.map f = limit.π F j' := (limit.cone F).w f /-- Evidence that the arbitrary choice of cone provied by `limit.cone F` is a limit cone. -/ def limit.is_limit (F : J ⥤ C) [has_limit F] : is_limit (limit.cone F) := (get_limit_cone F).is_limit /-- The morphism from the cone point of any other cone to the limit object. -/ def limit.lift (F : J ⥤ C) [has_limit F] (c : cone F) : c.X ⟶ limit F := (limit.is_limit F).lift c @[simp] lemma limit.is_limit_lift {F : J ⥤ C} [has_limit F] (c : cone F) : (limit.is_limit F).lift c = limit.lift F c := rfl @[simp, reassoc] lemma limit.lift_π {F : J ⥤ C} [has_limit F] (c : cone F) (j : J) : limit.lift F c ≫ limit.π F j = c.π.app j := is_limit.fac _ c j /-- The cone morphism from any cone to the arbitrary choice of limit cone. -/ def limit.cone_morphism {F : J ⥤ C} [has_limit F] (c : cone F) : c ⟶ (limit.cone F) := (limit.is_limit F).lift_cone_morphism c @[simp] lemma limit.cone_morphism_hom {F : J ⥤ C} [has_limit F] (c : cone F) : (limit.cone_morphism c).hom = limit.lift F c := rfl lemma limit.cone_morphism_π {F : J ⥤ C} [has_limit F] (c : cone F) (j : J) : (limit.cone_morphism c).hom ≫ limit.π F j = c.π.app j := by simp @[simp, reassoc] lemma limit.cone_point_unique_up_to_iso_hom_comp {F : J ⥤ C} [has_limit F] {c : cone F} (hc : is_limit c) (j : J) : (is_limit.cone_point_unique_up_to_iso hc (limit.is_limit _)).hom ≫ limit.π F j = c.π.app j := is_limit.cone_point_unique_up_to_iso_hom_comp _ _ _ @[simp, reassoc] lemma limit.cone_point_unique_up_to_iso_inv_comp {F : J ⥤ C} [has_limit F] {c : cone F} (hc : is_limit c) (j : J) : (is_limit.cone_point_unique_up_to_iso (limit.is_limit _) hc).inv ≫ limit.π F j = c.π.app j := is_limit.cone_point_unique_up_to_iso_inv_comp _ _ _ @[ext] lemma limit.hom_ext {F : J ⥤ C} [has_limit F] {X : C} {f f' : X ⟶ limit F} (w : ∀ j, f ≫ limit.π F j = f' ≫ limit.π F j) : f = f' := (limit.is_limit F).hom_ext w /-- The isomorphism (in `Type`) between morphisms from a specified object `W` to the limit object, and cones with cone point `W`. -/ def limit.hom_iso (F : J ⥤ C) [has_limit F] (W : C) : (W ⟶ limit F) ≅ (F.cones.obj (op W)) := (limit.is_limit F).hom_iso W @[simp] lemma limit.hom_iso_hom (F : J ⥤ C) [has_limit F] {W : C} (f : W ⟶ limit F) : (limit.hom_iso F W).hom f = (const J).map f ≫ (limit.cone F).π := (limit.is_limit F).hom_iso_hom f /-- The isomorphism (in `Type`) between morphisms from a specified object `W` to the limit object, and an explicit componentwise description of cones with cone point `W`. -/ def limit.hom_iso' (F : J ⥤ C) [has_limit F] (W : C) : ((W ⟶ limit F) : Type v) ≅ { p : Π j, W ⟶ F.obj j // ∀ {j j' : J} (f : j ⟶ j'), p j ≫ F.map f = p j' } := (limit.is_limit F).hom_iso' W lemma limit.lift_extend {F : J ⥤ C} [has_limit F] (c : cone F) {X : C} (f : X ⟶ c.X) : limit.lift F (c.extend f) = f ≫ limit.lift F c := by obviously /-- If a functor `F` has a limit, so does any naturally isomorphic functor. -/ lemma has_limit_of_iso {F G : J ⥤ C} [has_limit F] (α : F ≅ G) : has_limit G := has_limit.mk { cone := (cones.postcompose α.hom).obj (limit.cone F), is_limit := { lift := λ s, limit.lift F ((cones.postcompose α.inv).obj s), fac' := λ s j, begin rw [cones.postcompose_obj_π, nat_trans.comp_app, limit.cone_π, ←category.assoc, limit.lift_π], simp end, uniq' := λ s m w, begin apply limit.hom_ext, intro j, rw [limit.lift_π, cones.postcompose_obj_π, nat_trans.comp_app, ←nat_iso.app_inv, iso.eq_comp_inv], simpa using w j end } } /-- If a functor `G` has the same collection of cones as a functor `F` which has a limit, then `G` also has a limit. -/ -- See the construction of limits from products and equalizers -- for an example usage. lemma has_limit.of_cones_iso {J K : Type v} [small_category J] [small_category K] (F : J ⥤ C) (G : K ⥤ C) (h : F.cones ≅ G.cones) [has_limit F] : has_limit G := has_limit.mk ⟨_, is_limit.of_nat_iso ((is_limit.nat_iso (limit.is_limit F)) ≪≫ h)⟩ /-- The limits of `F : J ⥤ C` and `G : J ⥤ C` are isomorphic, if the functors are naturally isomorphic. -/ def has_limit.iso_of_nat_iso {F G : J ⥤ C} [has_limit F] [has_limit G] (w : F ≅ G) : limit F ≅ limit G := is_limit.cone_points_iso_of_nat_iso (limit.is_limit F) (limit.is_limit G) w @[simp, reassoc] lemma has_limit.iso_of_nat_iso_hom_π {F G : J ⥤ C} [has_limit F] [has_limit G] (w : F ≅ G) (j : J) : (has_limit.iso_of_nat_iso w).hom ≫ limit.π G j = limit.π F j ≫ w.hom.app j := by simp [has_limit.iso_of_nat_iso, is_limit.cone_points_iso_of_nat_iso_hom] /-- The limits of `F : J ⥤ C` and `G : K ⥤ C` are isomorphic, if there is an equivalence `e : J ≌ K` making the triangle commute up to natural isomorphism. -/ def has_limit.iso_of_equivalence {F : J ⥤ C} [has_limit F] {G : K ⥤ C} [has_limit G] (e : J ≌ K) (w : e.functor ⋙ G ≅ F) : limit F ≅ limit G := is_limit.cone_points_iso_of_equivalence (limit.is_limit F) (limit.is_limit G) e w @[simp] lemma has_limit.iso_of_equivalence_hom_π {F : J ⥤ C} [has_limit F] {G : K ⥤ C} [has_limit G] (e : J ≌ K) (w : e.functor ⋙ G ≅ F) (k : K) : (has_limit.iso_of_equivalence e w).hom ≫ limit.π G k = limit.π F (e.inverse.obj k) ≫ w.inv.app (e.inverse.obj k) ≫ G.map (e.counit.app k) := begin simp only [has_limit.iso_of_equivalence, is_limit.cone_points_iso_of_equivalence_hom], dsimp, simp, end @[simp] lemma has_limit.iso_of_equivalence_inv_π {F : J ⥤ C} [has_limit F] {G : K ⥤ C} [has_limit G] (e : J ≌ K) (w : e.functor ⋙ G ≅ F) (j : J) : (has_limit.iso_of_equivalence e w).inv ≫ limit.π F j = limit.π G (e.functor.obj j) ≫ w.hom.app j := begin simp only [has_limit.iso_of_equivalence, is_limit.cone_points_iso_of_equivalence_hom], dsimp, simp, end section pre variables (F) [has_limit F] (E : K ⥤ J) [has_limit (E ⋙ F)] /-- The canonical morphism from the limit of `F` to the limit of `E ⋙ F`. -/ def limit.pre : limit F ⟶ limit (E ⋙ F) := limit.lift (E ⋙ F) { X := limit F, π := { app := λ k, limit.π F (E.obj k) } } @[simp] lemma limit.pre_π (k : K) : limit.pre F E ≫ limit.π (E ⋙ F) k = limit.π F (E.obj k) := by erw is_limit.fac @[simp] lemma limit.lift_pre (c : cone F) : limit.lift F c ≫ limit.pre F E = limit.lift (E ⋙ F) (c.whisker E) := by ext; simp variables {L : Type v} [small_category L] variables (D : L ⥤ K) [has_limit (D ⋙ E ⋙ F)] @[simp] lemma limit.pre_pre : limit.pre F E ≫ limit.pre (E ⋙ F) D = limit.pre F (D ⋙ E) := by ext j; erw [assoc, limit.pre_π, limit.pre_π, limit.pre_π]; refl end pre section post variables {D : Type u'} [category.{v} D] variables (F) [has_limit F] (G : C ⥤ D) [has_limit (F ⋙ G)] /-- The canonical morphism from `G` applied to the limit of `F` to the limit of `F ⋙ G`. -/ def limit.post : G.obj (limit F) ⟶ limit (F ⋙ G) := limit.lift (F ⋙ G) (G.map_cone (limit.cone F)) @[simp] lemma limit.post_π (j : J) : limit.post F G ≫ limit.π (F ⋙ G) j = G.map (limit.π F j) := by { erw is_limit.fac, refl } @[simp] lemma limit.lift_post (c : cone F) : G.map (limit.lift F c) ≫ limit.post F G = limit.lift (F ⋙ G) (G.map_cone c) := by { ext, rw [assoc, limit.post_π, ←G.map_comp, limit.lift_π, limit.lift_π], refl } @[simp] lemma limit.post_post {E : Type u''} [category.{v} E] (H : D ⥤ E) [has_limit ((F ⋙ G) ⋙ H)] : /- H G (limit F) ⟶ H (limit (F ⋙ G)) ⟶ limit ((F ⋙ G) ⋙ H) equals -/ /- H G (limit F) ⟶ limit (F ⋙ (G ⋙ H)) -/ H.map (limit.post F G) ≫ limit.post (F ⋙ G) H = limit.post F (G ⋙ H) := by ext; erw [assoc, limit.post_π, ←H.map_comp, limit.post_π, limit.post_π]; refl end post lemma limit.pre_post {D : Type u'} [category.{v} D] (E : K ⥤ J) (F : J ⥤ C) (G : C ⥤ D) [has_limit F] [has_limit (E ⋙ F)] [has_limit (F ⋙ G)] [has_limit ((E ⋙ F) ⋙ G)] : /- G (limit F) ⟶ G (limit (E ⋙ F)) ⟶ limit ((E ⋙ F) ⋙ G) vs -/ /- G (limit F) ⟶ limit F ⋙ G ⟶ limit (E ⋙ (F ⋙ G)) or -/ G.map (limit.pre F E) ≫ limit.post (E ⋙ F) G = limit.post F G ≫ limit.pre (F ⋙ G) E := by ext; erw [assoc, limit.post_π, ←G.map_comp, limit.pre_π, assoc, limit.pre_π, limit.post_π]; refl open category_theory.equivalence instance has_limit_equivalence_comp (e : K ≌ J) [has_limit F] : has_limit (e.functor ⋙ F) := has_limit.mk { cone := cone.whisker e.functor (limit.cone F), is_limit := is_limit.whisker_equivalence (limit.is_limit F) e, } local attribute [elab_simple] inv_fun_id_assoc -- not entirely sure why this is needed /-- If a `E ⋙ F` has a limit, and `E` is an equivalence, we can construct a limit of `F`. -/ lemma has_limit_of_equivalence_comp (e : K ≌ J) [has_limit (e.functor ⋙ F)] : has_limit F := begin haveI : has_limit (e.inverse ⋙ e.functor ⋙ F) := limits.has_limit_equivalence_comp e.symm, apply has_limit_of_iso (e.inv_fun_id_assoc F), end -- `has_limit_comp_equivalence` and `has_limit_of_comp_equivalence` -- are proved in `category_theory/adjunction/limits.lean`. section lim_functor /-- Given a natural transformation `α : F ⟶ G`, we give a morphism from the cone point of any cone over `F` to the cone point of a limit cone over `G`. -/ def is_limit.map {F G : J ⥤ C} (s : cone F) {t : cone G} (P : is_limit t) (α : F ⟶ G) : s.X ⟶ t.X := P.lift ((cones.postcompose α).obj s) @[simp, reassoc] lemma is_limit_map_π {F G : J ⥤ C} (c : cone F) {d : cone G} (hd : is_limit d) (α : F ⟶ G) (j : J) : is_limit.map c hd α ≫ d.π.app j = c.π.app j ≫ α.app j := by apply is_limit.fac /-- Functoriality of limits. Usually this morphism should be accessed through `lim.map`, but may be needed separately when you have specified limits for the source and target functors, but not necessarily for all functors of shape `J`. -/ def lim_map {F G : J ⥤ C} [has_limit F] [has_limit G] (α : F ⟶ G) : limit F ⟶ limit G := is_limit.map _ (limit.is_limit G) α @[simp, reassoc] lemma lim_map_π {F G : J ⥤ C} [has_limit F] [has_limit G] (α : F ⟶ G) (j : J) : lim_map α ≫ limit.π G j = limit.π F j ≫ α.app j := by apply is_limit.fac variables [has_limits_of_shape J C] section local attribute [simp] lim_map /-- `limit F` is functorial in `F`, when `C` has all limits of shape `J`. -/ @[simps obj] def lim : (J ⥤ C) ⥤ C := { obj := λ F, limit F, map := λ F G α, lim_map α, map_id' := λ F, by { ext, erw [lim_map_π, category.id_comp, category.comp_id] }, map_comp' := λ F G H α β, by ext; erw [assoc, is_limit.fac, is_limit.fac, ←assoc, is_limit.fac, assoc]; refl } end variables {F} {G : J ⥤ C} (α : F ⟶ G) @[simp, reassoc] lemma limit.map_π (j : J) : lim.map α ≫ limit.π G j = limit.π F j ≫ α.app j := by apply is_limit.fac @[simp] lemma limit.lift_map (c : cone F) : limit.lift F c ≫ lim.map α = limit.lift G ((cones.postcompose α).obj c) := by ext; rw [assoc, limit.map_π, ←assoc, limit.lift_π, limit.lift_π]; refl lemma limit.map_pre [has_limits_of_shape K C] (E : K ⥤ J) : lim.map α ≫ limit.pre G E = limit.pre F E ≫ lim.map (whisker_left E α) := by ext; rw [assoc, limit.pre_π, limit.map_π, assoc, limit.map_π, ←assoc, limit.pre_π]; refl lemma limit.map_pre' [has_limits_of_shape K C] (F : J ⥤ C) {E₁ E₂ : K ⥤ J} (α : E₁ ⟶ E₂) : limit.pre F E₂ = limit.pre F E₁ ≫ lim.map (whisker_right α F) := by ext1; simp [← category.assoc] lemma limit.id_pre (F : J ⥤ C) : limit.pre F (𝟭 _) = lim.map (functor.left_unitor F).inv := by tidy lemma limit.map_post {D : Type u'} [category.{v} D] [has_limits_of_shape J D] (H : C ⥤ D) : /- H (limit F) ⟶ H (limit G) ⟶ limit (G ⋙ H) vs H (limit F) ⟶ limit (F ⋙ H) ⟶ limit (G ⋙ H) -/ H.map (lim.map α) ≫ limit.post G H = limit.post F H ≫ lim.map (whisker_right α H) := begin ext, rw [assoc, limit.post_π, ←H.map_comp, limit.map_π, H.map_comp], rw [assoc, limit.map_π, ←assoc, limit.post_π], refl end /-- The isomorphism between morphisms from `W` to the cone point of the limit cone for `F` and cones over `F` with cone point `W` is natural in `F`. -/ def lim_yoneda : lim ⋙ yoneda ≅ category_theory.cones J C := nat_iso.of_components (λ F, nat_iso.of_components (λ W, limit.hom_iso F (unop W)) (by tidy)) (by tidy) end lim_functor /-- We can transport limits of shape `J` along an equivalence `J ≌ J'`. -/ lemma has_limits_of_shape_of_equivalence {J' : Type v} [small_category J'] (e : J ≌ J') [has_limits_of_shape J C] : has_limits_of_shape J' C := by { constructor, intro F, apply has_limit_of_equivalence_comp e, apply_instance } end limit section colimit /-- `colimit_cocone F` contains a cocone over `F` together with the information that it is a colimit. -/ @[nolint has_inhabited_instance] structure colimit_cocone (F : J ⥤ C) := (cocone : cocone F) (is_colimit : is_colimit cocone) /-- `has_colimit F` represents the mere existence of a colimit for `F`. -/ class has_colimit (F : J ⥤ C) : Prop := mk' :: (exists_colimit : nonempty (colimit_cocone F)) lemma has_colimit.mk {F : J ⥤ C} (d : colimit_cocone F) : has_colimit F := ⟨nonempty.intro d⟩ /-- Use the axiom of choice to extract explicit `colimit_cocone F` from `has_colimit F`. -/ def get_colimit_cocone (F : J ⥤ C) [has_colimit F] : colimit_cocone F := classical.choice $ has_colimit.exists_colimit variables (J C) /-- `C` has colimits of shape `J` if there exists a colimit for every functor `F : J ⥤ C`. -/ class has_colimits_of_shape : Prop := (has_colimit : Π F : J ⥤ C, has_colimit F) /-- `C` has all (small) colimits if it has colimits of every shape. -/ class has_colimits : Prop := (has_colimits_of_shape : Π (J : Type v) [𝒥 : small_category J], has_colimits_of_shape J C) variables {J C} @[priority 100] -- see Note [lower instance priority] instance has_colimit_of_has_colimits_of_shape {J : Type v} [small_category J] [H : has_colimits_of_shape J C] (F : J ⥤ C) : has_colimit F := has_colimits_of_shape.has_colimit F @[priority 100] -- see Note [lower instance priority] instance has_colimits_of_shape_of_has_colimits {J : Type v} [small_category J] [H : has_colimits C] : has_colimits_of_shape J C := has_colimits.has_colimits_of_shape J /- Interface to the `has_colimit` class. -/ /-- An arbitrary choice of colimit cocone of a functor. -/ def colimit.cocone (F : J ⥤ C) [has_colimit F] : cocone F := (get_colimit_cocone F).cocone /-- An arbitrary choice of colimit object of a functor. -/ def colimit (F : J ⥤ C) [has_colimit F] := (colimit.cocone F).X /-- The coprojection from a value of the functor to the colimit object. -/ def colimit.ι (F : J ⥤ C) [has_colimit F] (j : J) : F.obj j ⟶ colimit F := (colimit.cocone F).ι.app j @[simp] lemma colimit.cocone_ι {F : J ⥤ C} [has_colimit F] (j : J) : (colimit.cocone F).ι.app j = colimit.ι _ j := rfl @[simp] lemma colimit.cocone_X {F : J ⥤ C} [has_colimit F] : (colimit.cocone F).X = colimit F := rfl @[simp, reassoc] lemma colimit.w (F : J ⥤ C) [has_colimit F] {j j' : J} (f : j ⟶ j') : F.map f ≫ colimit.ι F j' = colimit.ι F j := (colimit.cocone F).w f /-- Evidence that the arbitrary choice of cocone is a colimit cocone. -/ def colimit.is_colimit (F : J ⥤ C) [has_colimit F] : is_colimit (colimit.cocone F) := (get_colimit_cocone F).is_colimit /-- The morphism from the colimit object to the cone point of any other cocone. -/ def colimit.desc (F : J ⥤ C) [has_colimit F] (c : cocone F) : colimit F ⟶ c.X := (colimit.is_colimit F).desc c @[simp] lemma colimit.is_colimit_desc {F : J ⥤ C} [has_colimit F] (c : cocone F) : (colimit.is_colimit F).desc c = colimit.desc F c := rfl /-- We have lots of lemmas describing how to simplify `colimit.ι F j ≫ _`, and combined with `colimit.ext` we rely on these lemmas for many calculations. However, since `category.assoc` is a `@[simp]` lemma, often expressions are right associated, and it's hard to apply these lemmas about `colimit.ι`. We thus use `reassoc` to define additional `@[simp]` lemmas, with an arbitrary extra morphism. (see `tactic/reassoc_axiom.lean`) -/ @[simp, reassoc] lemma colimit.ι_desc {F : J ⥤ C} [has_colimit F] (c : cocone F) (j : J) : colimit.ι F j ≫ colimit.desc F c = c.ι.app j := is_colimit.fac _ c j /-- The cocone morphism from the arbitrary choice of colimit cocone to any cocone. -/ def colimit.cocone_morphism {F : J ⥤ C} [has_colimit F] (c : cocone F) : (colimit.cocone F) ⟶ c := (colimit.is_colimit F).desc_cocone_morphism c @[simp] lemma colimit.cocone_morphism_hom {F : J ⥤ C} [has_colimit F] (c : cocone F) : (colimit.cocone_morphism c).hom = colimit.desc F c := rfl lemma colimit.ι_cocone_morphism {F : J ⥤ C} [has_colimit F] (c : cocone F) (j : J) : colimit.ι F j ≫ (colimit.cocone_morphism c).hom = c.ι.app j := by simp @[simp, reassoc] lemma colimit.comp_cocone_point_unique_up_to_iso_hom {F : J ⥤ C} [has_colimit F] {c : cocone F} (hc : is_colimit c) (j : J) : colimit.ι F j ≫ (is_colimit.cocone_point_unique_up_to_iso (colimit.is_colimit _) hc).hom = c.ι.app j := is_colimit.comp_cocone_point_unique_up_to_iso_hom _ _ _ @[simp, reassoc] lemma colimit.comp_cocone_point_unique_up_to_iso_inv {F : J ⥤ C} [has_colimit F] {c : cocone F} (hc : is_colimit c) (j : J) : colimit.ι F j ≫ (is_colimit.cocone_point_unique_up_to_iso hc (colimit.is_colimit _)).inv = c.ι.app j := is_colimit.comp_cocone_point_unique_up_to_iso_inv _ _ _ @[ext] lemma colimit.hom_ext {F : J ⥤ C} [has_colimit F] {X : C} {f f' : colimit F ⟶ X} (w : ∀ j, colimit.ι F j ≫ f = colimit.ι F j ≫ f') : f = f' := (colimit.is_colimit F).hom_ext w /-- The isomorphism (in `Type`) between morphisms from the colimit object to a specified object `W`, and cocones with cone point `W`. -/ def colimit.hom_iso (F : J ⥤ C) [has_colimit F] (W : C) : (colimit F ⟶ W) ≅ (F.cocones.obj W) := (colimit.is_colimit F).hom_iso W @[simp] lemma colimit.hom_iso_hom (F : J ⥤ C) [has_colimit F] {W : C} (f : colimit F ⟶ W) : (colimit.hom_iso F W).hom f = (colimit.cocone F).ι ≫ (const J).map f := (colimit.is_colimit F).hom_iso_hom f /-- The isomorphism (in `Type`) between morphisms from the colimit object to a specified object `W`, and an explicit componentwise description of cocones with cone point `W`. -/ def colimit.hom_iso' (F : J ⥤ C) [has_colimit F] (W : C) : ((colimit F ⟶ W) : Type v) ≅ { p : Π j, F.obj j ⟶ W // ∀ {j j'} (f : j ⟶ j'), F.map f ≫ p j' = p j } := (colimit.is_colimit F).hom_iso' W lemma colimit.desc_extend (F : J ⥤ C) [has_colimit F] (c : cocone F) {X : C} (f : c.X ⟶ X) : colimit.desc F (c.extend f) = colimit.desc F c ≫ f := begin ext1, rw [←category.assoc], simp end /-- If `F` has a colimit, so does any naturally isomorphic functor. -/ -- This has the isomorphism pointing in the opposite direction than in `has_limit_of_iso`. -- This is intentional; it seems to help with elaboration. lemma has_colimit_of_iso {F G : J ⥤ C} [has_colimit F] (α : G ≅ F) : has_colimit G := has_colimit.mk { cocone := (cocones.precompose α.hom).obj (colimit.cocone F), is_colimit := { desc := λ s, colimit.desc F ((cocones.precompose α.inv).obj s), fac' := λ s j, begin rw [cocones.precompose_obj_ι, nat_trans.comp_app, colimit.cocone_ι], rw [category.assoc, colimit.ι_desc, ←nat_iso.app_hom, ←iso.eq_inv_comp], refl end, uniq' := λ s m w, begin apply colimit.hom_ext, intro j, rw [colimit.ι_desc, cocones.precompose_obj_ι, nat_trans.comp_app, ←nat_iso.app_inv, iso.eq_inv_comp], simpa using w j end } } /-- If a functor `G` has the same collection of cocones as a functor `F` which has a colimit, then `G` also has a colimit. -/ lemma has_colimit.of_cocones_iso {J K : Type v} [small_category J] [small_category K] (F : J ⥤ C) (G : K ⥤ C) (h : F.cocones ≅ G.cocones) [has_colimit F] : has_colimit G := has_colimit.mk ⟨_, is_colimit.of_nat_iso ((is_colimit.nat_iso (colimit.is_colimit F)) ≪≫ h)⟩ /-- The colimits of `F : J ⥤ C` and `G : J ⥤ C` are isomorphic, if the functors are naturally isomorphic. -/ def has_colimit.iso_of_nat_iso {F G : J ⥤ C} [has_colimit F] [has_colimit G] (w : F ≅ G) : colimit F ≅ colimit G := is_colimit.cocone_points_iso_of_nat_iso (colimit.is_colimit F) (colimit.is_colimit G) w @[simp, reassoc] lemma has_colimit.iso_of_nat_iso_ι_hom {F G : J ⥤ C} [has_colimit F] [has_colimit G] (w : F ≅ G) (j : J) : colimit.ι F j ≫ (has_colimit.iso_of_nat_iso w).hom = w.hom.app j ≫ colimit.ι G j := by simp [has_colimit.iso_of_nat_iso, is_colimit.cocone_points_iso_of_nat_iso_inv] /-- The colimits of `F : J ⥤ C` and `G : K ⥤ C` are isomorphic, if there is an equivalence `e : J ≌ K` making the triangle commute up to natural isomorphism. -/ def has_colimit.iso_of_equivalence {F : J ⥤ C} [has_colimit F] {G : K ⥤ C} [has_colimit G] (e : J ≌ K) (w : e.functor ⋙ G ≅ F) : colimit F ≅ colimit G := is_colimit.cocone_points_iso_of_equivalence (colimit.is_colimit F) (colimit.is_colimit G) e w @[simp] lemma has_colimit.iso_of_equivalence_hom_π {F : J ⥤ C} [has_colimit F] {G : K ⥤ C} [has_colimit G] (e : J ≌ K) (w : e.functor ⋙ G ≅ F) (j : J) : colimit.ι F j ≫ (has_colimit.iso_of_equivalence e w).hom = F.map (e.unit.app j) ≫ w.inv.app _ ≫ colimit.ι G _ := begin simp [has_colimit.iso_of_equivalence, is_colimit.cocone_points_iso_of_equivalence_inv], dsimp, simp, end @[simp] lemma has_colimit.iso_of_equivalence_inv_π {F : J ⥤ C} [has_colimit F] {G : K ⥤ C} [has_colimit G] (e : J ≌ K) (w : e.functor ⋙ G ≅ F) (k : K) : colimit.ι G k ≫ (has_colimit.iso_of_equivalence e w).inv = G.map (e.counit_inv.app k) ≫ w.hom.app (e.inverse.obj k) ≫ colimit.ι F (e.inverse.obj k) := begin simp [has_colimit.iso_of_equivalence, is_colimit.cocone_points_iso_of_equivalence_inv], dsimp, simp, end section pre variables (F) [has_colimit F] (E : K ⥤ J) [has_colimit (E ⋙ F)] /-- The canonical morphism from the colimit of `E ⋙ F` to the colimit of `F`. -/ def colimit.pre : colimit (E ⋙ F) ⟶ colimit F := colimit.desc (E ⋙ F) { X := colimit F, ι := { app := λ k, colimit.ι F (E.obj k) } } @[simp, reassoc] lemma colimit.ι_pre (k : K) : colimit.ι (E ⋙ F) k ≫ colimit.pre F E = colimit.ι F (E.obj k) := by erw is_colimit.fac @[simp] lemma colimit.pre_desc (c : cocone F) : colimit.pre F E ≫ colimit.desc F c = colimit.desc (E ⋙ F) (c.whisker E) := by ext; rw [←assoc, colimit.ι_pre]; simp variables {L : Type v} [small_category L] variables (D : L ⥤ K) [has_colimit (D ⋙ E ⋙ F)] @[simp] lemma colimit.pre_pre : colimit.pre (E ⋙ F) D ≫ colimit.pre F E = colimit.pre F (D ⋙ E) := begin ext j, rw [←assoc, colimit.ι_pre, colimit.ι_pre], letI : has_colimit ((D ⋙ E) ⋙ F) := show has_colimit (D ⋙ E ⋙ F), by apply_instance, exact (colimit.ι_pre F (D ⋙ E) j).symm end end pre section post variables {D : Type u'} [category.{v} D] variables (F) [has_colimit F] (G : C ⥤ D) [has_colimit (F ⋙ G)] /-- The canonical morphism from `G` applied to the colimit of `F ⋙ G` to `G` applied to the colimit of `F`. -/ def colimit.post : colimit (F ⋙ G) ⟶ G.obj (colimit F) := colimit.desc (F ⋙ G) (G.map_cocone (colimit.cocone F)) @[simp, reassoc] lemma colimit.ι_post (j : J) : colimit.ι (F ⋙ G) j ≫ colimit.post F G = G.map (colimit.ι F j) := by { erw is_colimit.fac, refl, } @[simp] lemma colimit.post_desc (c : cocone F) : colimit.post F G ≫ G.map (colimit.desc F c) = colimit.desc (F ⋙ G) (G.map_cocone c) := by { ext, rw [←assoc, colimit.ι_post, ←G.map_comp, colimit.ι_desc, colimit.ι_desc], refl } @[simp] lemma colimit.post_post {E : Type u''} [category.{v} E] (H : D ⥤ E) [has_colimit ((F ⋙ G) ⋙ H)] : /- H G (colimit F) ⟶ H (colimit (F ⋙ G)) ⟶ colimit ((F ⋙ G) ⋙ H) equals -/ /- H G (colimit F) ⟶ colimit (F ⋙ (G ⋙ H)) -/ colimit.post (F ⋙ G) H ≫ H.map (colimit.post F G) = colimit.post F (G ⋙ H) := begin ext, rw [←assoc, colimit.ι_post, ←H.map_comp, colimit.ι_post], exact (colimit.ι_post F (G ⋙ H) j).symm end end post lemma colimit.pre_post {D : Type u'} [category.{v} D] (E : K ⥤ J) (F : J ⥤ C) (G : C ⥤ D) [has_colimit F] [has_colimit (E ⋙ F)] [has_colimit (F ⋙ G)] [has_colimit ((E ⋙ F) ⋙ G)] : /- G (colimit F) ⟶ G (colimit (E ⋙ F)) ⟶ colimit ((E ⋙ F) ⋙ G) vs -/ /- G (colimit F) ⟶ colimit F ⋙ G ⟶ colimit (E ⋙ (F ⋙ G)) or -/ colimit.post (E ⋙ F) G ≫ G.map (colimit.pre F E) = colimit.pre (F ⋙ G) E ≫ colimit.post F G := begin ext, rw [←assoc, colimit.ι_post, ←G.map_comp, colimit.ι_pre, ←assoc], letI : has_colimit (E ⋙ F ⋙ G) := show has_colimit ((E ⋙ F) ⋙ G), by apply_instance, erw [colimit.ι_pre (F ⋙ G) E j, colimit.ι_post] end open category_theory.equivalence instance has_colimit_equivalence_comp (e : K ≌ J) [has_colimit F] : has_colimit (e.functor ⋙ F) := has_colimit.mk { cocone := cocone.whisker e.functor (colimit.cocone F), is_colimit := is_colimit.whisker_equivalence (colimit.is_colimit F) e, } /-- If a `E ⋙ F` has a colimit, and `E` is an equivalence, we can construct a colimit of `F`. -/ lemma has_colimit_of_equivalence_comp (e : K ≌ J) [has_colimit (e.functor ⋙ F)] : has_colimit F := begin haveI : has_colimit (e.inverse ⋙ e.functor ⋙ F) := limits.has_colimit_equivalence_comp e.symm, apply has_colimit_of_iso (e.inv_fun_id_assoc F).symm, end section colim_functor /-- Given a natural transformation `α : F ⟶ G`, we give a morphism from the cocone point of a colimit cocone over `F` to the cocone point of any cocone over `G`. -/ def is_colimit.map {F G : J ⥤ C} {s : cocone F} (P : is_colimit s) (t : cocone G) (α : F ⟶ G) : s.X ⟶ t.X := P.desc ((cocones.precompose α).obj t) @[simp, reassoc] lemma ι_is_colimit_map {F G : J ⥤ C} {c : cocone F} (hc : is_colimit c) (d : cocone G) (α : F ⟶ G) (j : J) : c.ι.app j ≫ is_colimit.map hc d α = α.app j ≫ d.ι.app j := by apply is_colimit.fac /-- Functoriality of colimits. Usually this morphism should be accessed through `colim.map`, but may be needed separately when you have specified colimits for the source and target functors, but not necessarily for all functors of shape `J`. -/ def colim_map {F G : J ⥤ C} [has_colimit F] [has_colimit G] (α : F ⟶ G) : colimit F ⟶ colimit G := is_colimit.map (colimit.is_colimit F) _ α @[simp, reassoc] lemma ι_colim_map {F G : J ⥤ C} [has_colimit F] [has_colimit G] (α : F ⟶ G) (j : J) : colimit.ι F j ≫ colim_map α = α.app j ≫ colimit.ι G j := by apply is_colimit.fac variables [has_colimits_of_shape J C] section local attribute [simp] colim_map /-- `colimit F` is functorial in `F`, when `C` has all colimits of shape `J`. -/ @[simps obj] def colim : (J ⥤ C) ⥤ C := { obj := λ F, colimit F, map := λ F G α, colim_map α, map_id' := λ F, by { ext, erw [ι_colim_map, id_comp, comp_id] }, map_comp' := λ F G H α β, by { ext, erw [←assoc, is_colimit.fac, is_colimit.fac, assoc, is_colimit.fac, ←assoc], refl } } end variables {F} {G : J ⥤ C} (α : F ⟶ G) @[simp, reassoc] lemma colimit.ι_map (j : J) : colimit.ι F j ≫ colim.map α = α.app j ≫ colimit.ι G j := by apply is_colimit.fac @[simp] lemma colimit.map_desc (c : cocone G) : colim.map α ≫ colimit.desc G c = colimit.desc F ((cocones.precompose α).obj c) := by ext; rw [←assoc, colimit.ι_map, assoc, colimit.ι_desc, colimit.ι_desc]; refl lemma colimit.pre_map [has_colimits_of_shape K C] (E : K ⥤ J) : colimit.pre F E ≫ colim.map α = colim.map (whisker_left E α) ≫ colimit.pre G E := by ext; rw [←assoc, colimit.ι_pre, colimit.ι_map, ←assoc, colimit.ι_map, assoc, colimit.ι_pre]; refl lemma colimit.pre_map' [has_colimits_of_shape K C] (F : J ⥤ C) {E₁ E₂ : K ⥤ J} (α : E₁ ⟶ E₂) : colimit.pre F E₁ = colim.map (whisker_right α F) ≫ colimit.pre F E₂ := by ext1; simp [← category.assoc] lemma colimit.pre_id (F : J ⥤ C) : colimit.pre F (𝟭 _) = colim.map (functor.left_unitor F).hom := by tidy lemma colimit.map_post {D : Type u'} [category.{v} D] [has_colimits_of_shape J D] (H : C ⥤ D) : /- H (colimit F) ⟶ H (colimit G) ⟶ colimit (G ⋙ H) vs H (colimit F) ⟶ colimit (F ⋙ H) ⟶ colimit (G ⋙ H) -/ colimit.post F H ≫ H.map (colim.map α) = colim.map (whisker_right α H) ≫ colimit.post G H:= begin ext, rw [←assoc, colimit.ι_post, ←H.map_comp, colimit.ι_map, H.map_comp], rw [←assoc, colimit.ι_map, assoc, colimit.ι_post], refl end /-- The isomorphism between morphisms from the cone point of the colimit cocone for `F` to `W` and cocones over `F` with cone point `W` is natural in `F`. -/ def colim_coyoneda : colim.op ⋙ coyoneda ≅ category_theory.cocones J C := nat_iso.of_components (λ F, nat_iso.of_components (colimit.hom_iso (unop F)) (by tidy)) (by tidy) end colim_functor /-- We can transport colimits of shape `J` along an equivalence `J ≌ J'`. -/ lemma has_colimits_of_shape_of_equivalence {J' : Type v} [small_category J'] (e : J ≌ J') [has_colimits_of_shape J C] : has_colimits_of_shape J' C := by { constructor, intro F, apply has_colimit_of_equivalence_comp e, apply_instance } end colimit end category_theory.limits
04d64b2aee93605fde3329bb6ad3afe4e764dd9f
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/basics/unnamed_1408.lean
9cf4e941b3388c846ba9532fc87b227a554e4269
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
136
lean
import data.real.basic tactic variables a b : ℝ -- BEGIN example : abs (a*b) ≤ (a^2 + b^2) / 2 := sorry #check abs_le'.mpr -- END
918544d596981941323838d1a7ccdae909470650
0845ae2ca02071debcfd4ac24be871236c01784f
/tests/playground/task_test2.lean
a97e1bca7c751007b5456bebb3198a1d0a904fa0
[ "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
430
lean
def run1 (i : Nat) (n : Nat) (xs : List Nat) : Nat := n.repeat (λ r, dbgTrace (">> [" ++ toString i ++ "] " ++ toString r) $ λ _, xs.foldl (+) r) 0 def main (xs : List String) : IO UInt32 := let ys := (List.replicate xs.head.toNat 1) in let ts : List (Task Nat) := (List.iota 10).map (λ i, Task.mk $ λ _, run1 (i+1) xs.head.toNat ys) in let ns : List Nat := ts.map Task.get in IO.println (">> " ++ toString ns) *> pure 0
6e847a8c18f7b7951dab4c15f2b8e889801cff61
82e44445c70db0f03e30d7be725775f122d72f3e
/src/control/fold.lean
a57e90247f3237baec23d95b83b8fde16c00a8bf
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
14,962
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Sean Leather -/ import algebra.free_monoid import algebra.opposites import control.traversable.instances import control.traversable.lemmas import category_theory.category import category_theory.endomorphism import category_theory.types import category_theory.category.Kleisli import deprecated.group /-! # List folds generalized to `traversable` Informally, we can think of `foldl` as a special case of `traverse` where we do not care about the reconstructed data structure and, in a state monad, we care about the final state. The obvious way to define `foldl` would be to use the state monad but it is nicer to reason about a more abstract interface with `fold_map` as a primitive and `fold_map_hom` as a defining property. ``` def fold_map {α ω} [has_one ω] [has_mul ω] (f : α → ω) : t α → ω := ... lemma fold_map_hom (α β) [monoid α] [monoid β] (f : α → β) [is_monoid_hom f] (g : γ → α) (x : t γ) : f (fold_map g x) = fold_map (f ∘ g) x := ... ``` `fold_map` uses a monoid ω to accumulate a value for every element of a data structure and `fold_map_hom` uses a monoid homomorphism to substitute the monoid used by `fold_map`. The two are sufficient to define `foldl`, `foldr` and `to_list`. `to_list` permits the formulation of specifications in terms of operations on lists. Each fold function can be defined using a specialized monoid. `to_list` uses a free monoid represented as a list with concatenation while `foldl` uses endofunctions together with function composition. The definition through monoids uses `traverse` together with the applicative functor `const m` (where `m` is the monoid). As an implementation, `const` guarantees that no resource is spent on reconstructing the structure during traversal. A special class could be defined for `foldable`, similarly to Haskell, but the author cannot think of instances of `foldable` that are not also `traversable`. -/ universes u v open ulift category_theory opposite namespace monoid variables {m : Type u → Type u} [monad m] variables {α β : Type u} /-- For a list, foldl f x [y₀,y₁] reduces as follows: ``` calc foldl f x [y₀,y₁] = foldl f (f x y₀) [y₁] : rfl ... = foldl f (f (f x y₀) y₁) [] : rfl ... = f (f x y₀) y₁ : rfl ``` with ``` f : α → β → α x : α [y₀,y₁] : list β ``` We can view the above as a composition of functions: ``` ... = f (f x y₀) y₁ : rfl ... = flip f y₁ (flip f y₀ x) : rfl ... = (flip f y₁ ∘ flip f y₀) x : rfl ``` We can use traverse and const to construct this composition: ``` calc const.run (traverse (λ y, const.mk' (flip f y)) [y₀,y₁]) x = const.run ((::) <$> const.mk' (flip f y₀) <*> traverse (λ y, const.mk' (flip f y)) [y₁]) x ... = const.run ((::) <$> const.mk' (flip f y₀) <*> ( (::) <$> const.mk' (flip f y₁) <*> traverse (λ y, const.mk' (flip f y)) [] )) x ... = const.run ((::) <$> const.mk' (flip f y₀) <*> ( (::) <$> const.mk' (flip f y₁) <*> pure [] )) x ... = const.run ( ((::) <$> const.mk' (flip f y₁) <*> pure []) ∘ ((::) <$> const.mk' (flip f y₀)) ) x ... = const.run ( const.mk' (flip f y₁) ∘ const.mk' (flip f y₀) ) x ... = const.run ( flip f y₁ ∘ flip f y₀ ) x ... = f (f x y₀) y₁ ``` And this is how `const` turns a monoid into an applicative functor and how the monoid of endofunctions define `foldl`. -/ @[reducible] def foldl (α : Type u) : Type u := (End α)ᵒᵖ def foldl.mk (f : α → α) : foldl α := op f def foldl.get (x : foldl α) : α → α := unop x def foldl.of_free_monoid (f : β → α → β) (xs : free_monoid α) : monoid.foldl β := op $ flip (list.foldl f) xs @[reducible] def foldr (α : Type u) : Type u := End α def foldr.mk (f : α → α) : foldr α := f def foldr.get (x : foldr α) : α → α := x def foldr.of_free_monoid (f : α → β → β) (xs : free_monoid α) : monoid.foldr β := flip (list.foldr f) xs @[reducible] def mfoldl (m : Type u → Type u) [monad m] (α : Type u) : Type u := opposite $ End $ Kleisli.mk m α def mfoldl.mk (f : α → m α) : mfoldl m α := op f def mfoldl.get (x : mfoldl m α) : α → m α := unop x def mfoldl.of_free_monoid (f : β → α → m β) (xs : free_monoid α) : monoid.mfoldl m β := op $ flip (list.mfoldl f) xs @[reducible] def mfoldr (m : Type u → Type u) [monad m] (α : Type u) : Type u := End $ Kleisli.mk m α def mfoldr.mk (f : α → m α) : mfoldr m α := f def mfoldr.get (x : mfoldr m α) : α → m α := x def mfoldr.of_free_monoid (f : α → β → m β) (xs : free_monoid α) : monoid.mfoldr m β := flip (list.mfoldr f) xs end monoid namespace traversable open monoid functor section defs variables {α β : Type u} {t : Type u → Type u} [traversable t] def fold_map {α ω} [has_one ω] [has_mul ω] (f : α → ω) : t α → ω := traverse (const.mk' ∘ f) def foldl (f : α → β → α) (x : α) (xs : t β) : α := (fold_map (foldl.mk ∘ flip f) xs).get x def foldr (f : α → β → β) (x : β) (xs : t α) : β := (fold_map (foldr.mk ∘ f) xs).get x /-- Conceptually, `to_list` collects all the elements of a collection in a list. This idea is formalized by `lemma to_list_spec (x : t α) : to_list x = fold_map free_monoid.mk x`. The definition of `to_list` is based on `foldl` and `list.cons` for speed. It is faster than using `fold_map free_monoid.mk` because, by using `foldl` and `list.cons`, each insertion is done in constant time. As a consequence, `to_list` performs in linear. On the other hand, `fold_map free_monoid.mk` creates a singleton list around each element and concatenates all the resulting lists. In `xs ++ ys`, concatenation takes a time proportional to `length xs`. Since the order in which concatenation is evaluated is unspecified, nothing prevents each element of the traversable to be appended at the end `xs ++ [x]` which would yield a `O(n²)` run time. -/ def to_list : t α → list α := list.reverse ∘ foldl (flip list.cons) [] def length (xs : t α) : ℕ := down $ foldl (λ l _, up $ l.down + 1) (up 0) xs variables {m : Type u → Type u} [monad m] def mfoldl (f : α → β → m α) (x : α) (xs : t β) : m α := (fold_map (mfoldl.mk ∘ flip f) xs).get x def mfoldr (f : α → β → m β) (x : β) (xs : t α) : m β := (fold_map (mfoldr.mk ∘ f) xs).get x end defs section applicative_transformation variables {α β γ : Type u} open function (hiding const) is_monoid_hom def map_fold [monoid α] [monoid β] {f : α → β} (hf : is_monoid_hom f) : applicative_transformation (const α) (const β) := { app := λ x, f, preserves_seq' := by { intros, simp only [map_mul hf, (<*>)], }, preserves_pure' := by { intros, simp only [map_one hf, pure] } } def free.mk : α → free_monoid α := list.ret def free.map (f : α → β) : free_monoid α → free_monoid β := list.map f lemma free.map_eq_map (f : α → β) (xs : list α) : f <$> xs = free.map f xs := rfl lemma free.map.is_monoid_hom (f : α → β) : is_monoid_hom (free.map f) := { map_mul := λ x y, by simp only [free.map, free_monoid.mul_def, list.map_append, free_add_monoid.add_def], map_one := by simp only [free.map, free_monoid.one_def, list.map, free_add_monoid.zero_def] } lemma fold_foldl (f : β → α → β) : is_monoid_hom (foldl.of_free_monoid f) := { map_one := rfl, map_mul := by intros; simp only [free_monoid.mul_def, foldl.of_free_monoid, flip, unop_op, list.foldl_append, op_inj_iff]; refl } lemma foldl.unop_of_free_monoid (f : β → α → β) (xs : free_monoid α) (a : β) : unop (foldl.of_free_monoid f xs) a = list.foldl f a xs := rfl lemma fold_foldr (f : α → β → β) : is_monoid_hom (foldr.of_free_monoid f) := { map_one := rfl, map_mul := begin intros, simp only [free_monoid.mul_def, foldr.of_free_monoid, list.foldr_append, flip], refl end } variables (m : Type u → Type u) [monad m] [is_lawful_monad m] @[simp] lemma mfoldl.unop_of_free_monoid (f : β → α → m β) (xs : free_monoid α) (a : β) : unop (mfoldl.of_free_monoid f xs) a = list.mfoldl f a xs := rfl lemma fold_mfoldl (f : β → α → m β) : is_monoid_hom (mfoldl.of_free_monoid f) := { map_one := rfl, map_mul := by intros; apply unop_injective; ext; apply list.mfoldl_append } lemma fold_mfoldr (f : α → β → m β) : is_monoid_hom (mfoldr.of_free_monoid f) := { map_one := rfl, map_mul := by intros; ext; apply list.mfoldr_append } variables {t : Type u → Type u} [traversable t] [is_lawful_traversable t] open is_lawful_traversable lemma fold_map_hom [monoid α] [monoid β] {f : α → β} (hf : is_monoid_hom f) (g : γ → α) (x : t γ) : f (fold_map g x) = fold_map (f ∘ g) x := calc f (fold_map g x) = f (traverse (const.mk' ∘ g) x) : rfl ... = (map_fold hf).app _ (traverse (const.mk' ∘ g) x) : rfl ... = traverse ((map_fold hf).app _ ∘ (const.mk' ∘ g)) x : naturality (map_fold hf) _ _ ... = fold_map (f ∘ g) x : rfl lemma fold_map_hom_free [monoid β] {f : free_monoid α → β} (hf : is_monoid_hom f) (x : t α) : f (fold_map free.mk x) = fold_map (f ∘ free.mk) x := fold_map_hom hf _ x variable {m} lemma fold_mfoldl_cons (f : α → β → m α) (x : β) (y : α) : list.mfoldl f y (free.mk x) = f y x := by simp only [free.mk, list.ret, list.mfoldl, bind_pure] lemma fold_mfoldr_cons (f : β → α → m α) (x : β) (y : α) : list.mfoldr f y (free.mk x) = f x y := by simp only [free.mk, list.ret, list.mfoldr, pure_bind] end applicative_transformation section equalities open is_lawful_traversable list (cons) variables {α β γ : Type u} variables {t : Type u → Type u} [traversable t] [is_lawful_traversable t] @[simp] lemma foldl.of_free_monoid_comp_free_mk (f : α → β → α) : foldl.of_free_monoid f ∘ free.mk = foldl.mk ∘ flip f := rfl @[simp] lemma foldr.of_free_monoid_comp_free_mk (f : β → α → α) : foldr.of_free_monoid f ∘ free.mk = foldr.mk ∘ f := rfl @[simp] lemma mfoldl.of_free_monoid_comp_free_mk {m} [monad m] [is_lawful_monad m] (f : α → β → m α) : mfoldl.of_free_monoid f ∘ free.mk = mfoldl.mk ∘ flip f := by ext; simp only [(∘), mfoldl.of_free_monoid, mfoldl.mk, flip, fold_mfoldl_cons]; refl @[simp] lemma mfoldr.of_free_monoid_comp_free_mk {m} [monad m] [is_lawful_monad m] (f : β → α → m α) : mfoldr.of_free_monoid f ∘ free.mk = mfoldr.mk ∘ f := by { ext, simp only [(∘), mfoldr.of_free_monoid, mfoldr.mk, flip, fold_mfoldr_cons] } lemma to_list_spec (xs : t α) : to_list xs = (fold_map free.mk xs : free_monoid _) := eq.symm $ calc fold_map free.mk xs = (fold_map free.mk xs).reverse.reverse : by simp only [list.reverse_reverse] ... = (list.foldr cons [] (fold_map free.mk xs).reverse).reverse : by simp only [list.foldr_eta] ... = (unop (foldl.of_free_monoid (flip cons) (fold_map free.mk xs)) []).reverse : by simp only [flip,list.foldr_reverse,foldl.of_free_monoid, unop_op] ... = to_list xs : begin have : is_monoid_hom (foldl.of_free_monoid (flip $ @cons α)), { apply fold_foldl }, rw fold_map_hom_free this, simp only [to_list, foldl, list.reverse_inj, foldl.get, foldl.of_free_monoid_comp_free_mk], all_goals { apply_instance } end lemma fold_map_map [monoid γ] (f : α → β) (g : β → γ) (xs : t α) : fold_map g (f <$> xs) = fold_map (g ∘ f) xs := by simp only [fold_map,traverse_map] lemma foldl_to_list (f : α → β → α) (xs : t β) (x : α) : foldl f x xs = list.foldl f x (to_list xs) := begin rw ← foldl.unop_of_free_monoid, simp only [foldl, to_list_spec, fold_map_hom_free (fold_foldl f), foldl.of_free_monoid_comp_free_mk, foldl.get] end lemma foldr_to_list (f : α → β → β) (xs : t α) (x : β) : foldr f x xs = list.foldr f x (to_list xs) := begin change _ = foldr.of_free_monoid _ _ _, simp only [foldr, to_list_spec, fold_map_hom_free (fold_foldr f), foldr.of_free_monoid_comp_free_mk, foldr.get] end lemma to_list_map (f : α → β) (xs : t α) : to_list (f <$> xs) = f <$> to_list xs := by { simp only [to_list_spec,free.map_eq_map,fold_map_hom (free.map.is_monoid_hom f), fold_map_map]; refl } @[simp] theorem foldl_map (g : β → γ) (f : α → γ → α) (a : α) (l : t β) : foldl f a (g <$> l) = foldl (λ x y, f x (g y)) a l := by simp only [foldl, fold_map_map, (∘), flip] @[simp] theorem foldr_map (g : β → γ) (f : γ → α → α) (a : α) (l : t β) : foldr f a (g <$> l) = foldr (f ∘ g) a l := by simp only [foldr, fold_map_map, (∘), flip] @[simp] theorem to_list_eq_self {xs : list α} : to_list xs = xs := begin simp only [to_list_spec, fold_map, traverse], induction xs, case list.nil { refl }, case list.cons : _ _ ih { unfold list.traverse list.ret, rw ih, refl } end theorem length_to_list {xs : t α} : length xs = list.length (to_list xs) := begin unfold length, rw foldl_to_list, generalize : to_list xs = ys, let f := λ (n : ℕ) (a : α), n + 1, transitivity list.foldl f 0 ys, { generalize : 0 = n, induction ys with _ _ ih generalizing n, { simp only [list.foldl_nil] }, { simp only [list.foldl, ih (n+1)] } }, { induction ys with _ tl ih, { simp only [list.length, list.foldl_nil] }, { simp only [list.foldl, list.length], rw [← ih], exact tl.foldl_hom (λx, x+1) f f 0 (λ n x, rfl) } } end variables {m : Type u → Type u} [monad m] [is_lawful_monad m] section local attribute [semireducible] opposite lemma mfoldl_to_list {f : α → β → m α} {x : α} {xs : t β} : mfoldl f x xs = list.mfoldl f x (to_list xs) := begin change _ = unop (mfoldl.of_free_monoid f (to_list xs)) x, simp only [mfoldl, to_list_spec, fold_map_hom_free (fold_mfoldl (λ (β : Type u), m β) f), mfoldl.of_free_monoid_comp_free_mk, mfoldl.get] end end lemma mfoldr_to_list (f : α → β → m β) (x : β) (xs : t α) : mfoldr f x xs = list.mfoldr f x (to_list xs) := begin change _ = mfoldr.of_free_monoid f (to_list xs) x, simp only [mfoldr, to_list_spec, fold_map_hom_free (fold_mfoldr (λ (β : Type u), m β) f), mfoldr.of_free_monoid_comp_free_mk, mfoldr.get] end @[simp] theorem mfoldl_map (g : β → γ) (f : α → γ → m α) (a : α) (l : t β) : mfoldl f a (g <$> l) = mfoldl (λ x y, f x (g y)) a l := by simp only [mfoldl, fold_map_map, (∘), flip] @[simp] theorem mfoldr_map (g : β → γ) (f : γ → α → m α) (a : α) (l : t β) : mfoldr f a (g <$> l) = mfoldr (f ∘ g) a l := by simp only [mfoldr, fold_map_map, (∘), flip] end equalities end traversable
7c90fb6924c3250abf3ba271179f90f8656f18f8
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1901.lean
0a6e7dfabb392e2596acfba891f2b7619184a905
[ "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
586
lean
class Funny (F : Type _) (A B : outParam (Type _)) where toFun : F → A → B instance [Funny F A B] : CoeFun F fun _ => A → B where coe := Funny.toFun class MulHomClass (F) (A B : outParam _) [Mul A] [Mul B] extends Funny F A B class Monoid (M) extends Mul M instance [Mul A] : Mul (Id A) := ‹_› #check Funny.toFun #check MulHomClass.toFunny example [Monoid A] [Monoid B] [MulHomClass F A B] : Funny F A B := inferInstance -- set_option trace.Meta.synthInstance true example [Monoid A] [Monoid B] [MulHomClass F A B] (f : F) (a : A) : f a = f a := rfl -- infinite loop
31d5eade6118966aae03e2cd09ef550f3ab2c14a
0845ae2ca02071debcfd4ac24be871236c01784f
/library/init/lean/class.lean
4342066b6c5da461a4bd318adb6be320cf7b34c0
[ "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
5,101
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.lean.attributes namespace Lean inductive ClassEntry | «class» (name : Name) (hasOutParam : Bool) | «instance» (name : Name) (ofClass : Name) namespace ClassEntry @[inline] def getName : ClassEntry → Name | («class» n _) := n | («instance» n _) := n def lt (a b : ClassEntry) : Bool := Name.quickLt a.getName b.getName end ClassEntry structure ClassState := (classToInstances : SMap Name (List Name) Name.quickLt := SMap.empty) (hasOutParam : SMap Name Bool Name.quickLt := SMap.empty) (instances : SMap Name Unit Name.quickLt := SMap.empty) namespace ClassState instance : Inhabited ClassState := ⟨{}⟩ def addEntry (s : ClassState) (entry : ClassEntry) : ClassState := match entry with | ClassEntry.«class» clsName hasOutParam => { hasOutParam := s.hasOutParam.insert clsName hasOutParam, .. s } | ClassEntry.«instance» instName clsName => { instances := s.instances.insert instName (), classToInstances := match s.classToInstances.find clsName with | some insts => s.classToInstances.insert clsName (instName :: insts) | none => s.classToInstances.insert clsName [instName], .. s } def switch : ClassState → ClassState | ⟨m₁, m₂, m₃⟩ := ⟨m₁.switch, m₂.switch, m₃.switch⟩ end ClassState /- TODO: add support for scoped instances -/ def mkClassExtension : IO (SimplePersistentEnvExtension ClassEntry ClassState) := registerSimplePersistentEnvExtension { name := `classExt, addEntryFn := ClassState.addEntry, addImportedFn := fun es => (mkStateFromImportedEntries ClassState.addEntry {} es).switch } @[init mkClassExtension] constant classExtension : SimplePersistentEnvExtension ClassEntry ClassState := default _ @[export lean.is_class_core] def isClass (env : Environment) (n : Name) : Bool := (classExtension.getState env).hasOutParam.contains n @[export lean.is_instance_core] def isInstance (env : Environment) (n : Name) : Bool := (classExtension.getState env).instances.contains n @[export lean.get_class_instances_core] def getClassInstances (env : Environment) (n : Name) : List Name := match (classExtension.getState env).classToInstances.find n with | some insts => insts | none => [] @[export lean.has_out_params_core] def hasOutParams (env : Environment) (n : Name) : Bool := match (classExtension.getState env).hasOutParam.find n with | some b => b | none => false @[export lean.is_out_param_core] private def isOutParam (e : Expr) : Bool := e.isAppOfArity `outParam 1 def Expr.hasOutParam : Expr → Bool | (Expr.pi _ _ d b) := isOutParam d || Expr.hasOutParam b | _ := false def addClass (env : Environment) (clsName : Name) : Except String Environment := if isClass env clsName then Except.error ("class has already been declared '" ++ toString clsName ++ "'") else match env.find clsName with | none => Except.error ("unknown declaration '" ++ toString clsName ++ "'") | some decl => Except.ok (classExtension.addEntry env (ClassEntry.«class» clsName decl.type.hasOutParam)) private def consumeNLambdas : Nat → Expr → Option Expr | 0 e := some e | (i+1) (Expr.lam _ _ _ b) := consumeNLambdas i b | _ _ := none partial def getClassName (env : Environment) : Expr → Option Name | (Expr.pi _ _ _ d) := getClassName d | e := do Expr.const c _ ← pure e.getAppFn | none; info ← env.find c; match info.value with | some val => do body ← consumeNLambdas e.getAppNumArgs val; getClassName body | none => if isClass env c then some c else none @[export lean.add_instance_core] def addInstance (env : Environment) (instName : Name) : Except String Environment := match env.find instName with | none => Except.error ("unknown declaration '" ++ toString instName ++ "'") | some decl => match getClassName env decl.type with | none => Except.error ("invalid instance '" ++ toString instName ++ "', failed to retrieve class") | some clsName => Except.ok (classExtension.addEntry env (ClassEntry.«instance» instName clsName)) @[init] def registerClassAttr : IO Unit := registerAttribute { name := `class, descr := "type class", add := fun env decl args persistent => do unless args.isMissing $ throw (IO.userError ("invalid attribute 'class', unexpected argument")); unless persistent $ throw (IO.userError ("invalid attribute 'class', must be persistent")); IO.ofExcept (addClass env decl) } @[init] def registerInstanceAttr : IO Unit := registerAttribute { name := `instance, descr := "type class instance", add := fun env decl args persistent => do unless args.isMissing $ throw (IO.userError ("invalid attribute 'instance', unexpected argument")); unless persistent $ throw (IO.userError ("invalid attribute 'instance', must be persistent")); IO.ofExcept (addInstance env decl) } end Lean
cac23d0bb68b60b85254401bd0719ee078073898
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/topology/bases.lean
40e7781020cff7d08553eb123fd565d1c2238f36
[ "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
26,170
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 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⟩⟩ 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
13f17c3d4099c41b6af861a98617f732c04a26b4
6b02ce66658141f3e0aa3dfa88cd30bbbb24d17b
/src/Lean/Meta/Basic.lean
dee91fae30fab894201ce223b19ab7dd501b0ebc
[ "Apache-2.0" ]
permissive
pbrinkmeier/lean4
d31991fd64095e64490cb7157bcc6803f9c48af4
32fd82efc2eaf1232299e930ec16624b370eac39
refs/heads/master
1,681,364,001,662
1,618,425,427,000
1,618,425,427,000
358,314,562
0
0
Apache-2.0
1,618,504,558,000
1,618,501,999,000
null
UTF-8
Lean
false
false
45,653
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.Data.LOption import Lean.Environment import Lean.Class import Lean.ReducibilityAttrs import Lean.Util.Trace import Lean.Util.RecDepth import Lean.Util.PPExt import Lean.Util.OccursCheck import Lean.Util.MonadBacktrack import Lean.Compiler.InlineAttrs import Lean.Meta.TransparencyMode import Lean.Meta.DiscrTreeTypes import Lean.Eval import Lean.CoreM /- This module provides four (mutually dependent) goodies that are needed for building the elaborator and tactic frameworks. 1- Weak head normal form computation with support for metavariables and transparency modes. 2- Definitionally equality checking with support for metavariables (aka unification modulo definitional equality). 3- Type inference. 4- Type class resolution. They are packed into the MetaM monad. -/ namespace Lean.Meta builtin_initialize isDefEqStuckExceptionId : InternalExceptionId ← registerInternalExceptionId `isDefEqStuck structure Config where foApprox : Bool := false ctxApprox : Bool := false quasiPatternApprox : Bool := false /- When `constApprox` is set to true, we solve `?m t =?= c` using `?m := fun _ => c` when `?m t` is not a higher-order pattern and `c` is not an application as -/ constApprox : Bool := false /- When the following flag is set, `isDefEq` throws the exeption `Exeption.isDefEqStuck` whenever it encounters a constraint `?m ... =?= t` where `?m` is read only. This feature is useful for type class resolution where we may want to notify the caller that the TC problem may be solveable later after it assigns `?m`. -/ isDefEqStuckEx : Bool := false transparency : TransparencyMode := TransparencyMode.default /- If zetaNonDep == false, then non dependent let-decls are not zeta expanded. -/ zetaNonDep : Bool := true /- When `trackZeta == true`, we store zetaFVarIds all free variables that have been zeta-expanded. -/ trackZeta : Bool := false unificationHints : Bool := true structure ParamInfo where implicit : Bool := false instImplicit : Bool := false hasFwdDeps : Bool := false backDeps : Array Nat := #[] deriving Inhabited def ParamInfo.isExplicit (p : ParamInfo) : Bool := !p.implicit && !p.instImplicit structure FunInfo where paramInfo : Array ParamInfo := #[] resultDeps : Array Nat := #[] structure InfoCacheKey where transparency : TransparencyMode expr : Expr nargs? : Option Nat deriving Inhabited, BEq namespace InfoCacheKey instance : Hashable InfoCacheKey := ⟨fun ⟨transparency, expr, nargs⟩ => mixHash (hash transparency) <| mixHash (hash expr) (hash nargs)⟩ end InfoCacheKey open Std (PersistentArray PersistentHashMap) abbrev SynthInstanceCache := PersistentHashMap Expr (Option Expr) abbrev InferTypeCache := PersistentExprStructMap Expr abbrev FunInfoCache := PersistentHashMap InfoCacheKey FunInfo abbrev WhnfCache := PersistentExprStructMap Expr structure Cache where inferType : InferTypeCache := {} funInfo : FunInfoCache := {} synthInstance : SynthInstanceCache := {} whnfDefault : WhnfCache := {} -- cache for closed terms and `TransparencyMode.default` whnfAll : WhnfCache := {} -- cache for closed terms and `TransparencyMode.all` deriving Inhabited /-- "Context" for a postponed universe constraint. `lhs` and `rhs` are the surrounding `isDefEq` call when the postponed constraint was created. -/ structure DefEqContext where lhs : Expr rhs : Expr lctx : LocalContext localInstances : LocalInstances /-- Auxiliary structure for representing postponed universe constraints. Remark: the fields `ref` and `rootDefEq?` are used for error message generation only. Remark: we may consider improving the error message generation in the future. -/ structure PostponedEntry where ref : Syntax -- We save the `ref` at entry creation time lhs : Level rhs : Level ctx? : Option DefEqContext -- Context for the surrounding `isDefEq` call when entry was created deriving Inhabited structure State where mctx : MetavarContext := {} cache : Cache := {} /- When `trackZeta == true`, then any let-decl free variable that is zeta expansion performed by `MetaM` is stored in `zetaFVarIds`. -/ zetaFVarIds : NameSet := {} postponed : PersistentArray PostponedEntry := {} deriving Inhabited structure SavedState where core : Core.State meta : State deriving Inhabited structure Context where config : Config := {} lctx : LocalContext := {} localInstances : LocalInstances := #[] /-- Not `none` when inside of an `isDefEq` test. See `PostponedEntry`. -/ defEqCtx? : Option DefEqContext := none abbrev MetaM := ReaderT Context $ StateRefT State CoreM instance : Inhabited (MetaM α) where default := fun _ _ => arbitrary instance : MonadLCtx MetaM where getLCtx := return (← read).lctx instance : MonadMCtx MetaM where getMCtx := return (← get).mctx modifyMCtx f := modify fun s => { s with mctx := f s.mctx } instance : AddMessageContext MetaM where addMessageContext := addMessageContextFull protected def saveState : MetaM SavedState := return { core := (← getThe Core.State), meta := (← get) } /-- Restore backtrackable parts of the state. -/ def SavedState.restore (b : SavedState) : MetaM Unit := do Core.restore b.core modify fun s => { s with mctx := b.meta.mctx, zetaFVarIds := b.meta.zetaFVarIds, postponed := b.meta.postponed } instance : MonadBacktrack SavedState MetaM where saveState := Meta.saveState restoreState s := s.restore @[inline] def MetaM.run (x : MetaM α) (ctx : Context := {}) (s : State := {}) : CoreM (α × State) := x ctx |>.run s @[inline] def MetaM.run' (x : MetaM α) (ctx : Context := {}) (s : State := {}) : CoreM α := Prod.fst <$> x.run ctx s @[inline] def MetaM.toIO (x : MetaM α) (ctxCore : Core.Context) (sCore : Core.State) (ctx : Context := {}) (s : State := {}) : IO (α × Core.State × State) := do let ((a, s), sCore) ← (x.run ctx s).toIO ctxCore sCore pure (a, sCore, s) instance [MetaEval α] : MetaEval (MetaM α) := ⟨fun env opts x _ => MetaEval.eval env opts x.run' true⟩ protected def throwIsDefEqStuck : MetaM α := throw <| Exception.internal isDefEqStuckExceptionId builtin_initialize registerTraceClass `Meta registerTraceClass `Meta.debug @[inline] def liftMetaM [MonadLiftT MetaM m] (x : MetaM α) : m α := liftM x @[inline] def mapMetaM [MonadControlT MetaM m] [Monad m] (f : forall {α}, MetaM α → MetaM α) {α} (x : m α) : m α := controlAt MetaM fun runInBase => f <| runInBase x @[inline] def map1MetaM [MonadControlT MetaM m] [Monad m] (f : forall {α}, (β → MetaM α) → MetaM α) {α} (k : β → m α) : m α := controlAt MetaM fun runInBase => f fun b => runInBase <| k b @[inline] def map2MetaM [MonadControlT MetaM m] [Monad m] (f : forall {α}, (β → γ → MetaM α) → MetaM α) {α} (k : β → γ → m α) : m α := controlAt MetaM fun runInBase => f fun b c => runInBase <| k b c section Methods variable [MonadControlT MetaM n] [Monad n] @[inline] def modifyCache (f : Cache → Cache) : MetaM Unit := modify fun ⟨mctx, cache, zetaFVarIds, postponed⟩ => ⟨mctx, f cache, zetaFVarIds, postponed⟩ @[inline] def modifyInferTypeCache (f : InferTypeCache → InferTypeCache) : MetaM Unit := modifyCache fun ⟨ic, c1, c2, c3, c4⟩ => ⟨f ic, c1, c2, c3, c4⟩ def getLocalInstances : MetaM LocalInstances := return (← read).localInstances def getConfig : MetaM Config := return (← read).config def setMCtx (mctx : MetavarContext) : MetaM Unit := modify fun s => { s with mctx := mctx } def resetZetaFVarIds : MetaM Unit := modify fun s => { s with zetaFVarIds := {} } def getZetaFVarIds : MetaM NameSet := return (← get).zetaFVarIds def getPostponed : MetaM (PersistentArray PostponedEntry) := return (← get).postponed def setPostponed (postponed : PersistentArray PostponedEntry) : MetaM Unit := modify fun s => { s with postponed := postponed } @[inline] def modifyPostponed (f : PersistentArray PostponedEntry → PersistentArray PostponedEntry) : MetaM Unit := modify fun s => { s with postponed := f s.postponed } builtin_initialize whnfRef : IO.Ref (Expr → MetaM Expr) ← IO.mkRef fun _ => throwError "whnf implementation was not set" builtin_initialize inferTypeRef : IO.Ref (Expr → MetaM Expr) ← IO.mkRef fun _ => throwError "inferType implementation was not set" builtin_initialize isExprDefEqAuxRef : IO.Ref (Expr → Expr → MetaM Bool) ← IO.mkRef fun _ _ => throwError "isDefEq implementation was not set" builtin_initialize synthPendingRef : IO.Ref (MVarId → MetaM Bool) ← IO.mkRef fun _ => pure false def whnf (e : Expr) : MetaM Expr := withIncRecDepth do (← whnfRef.get) e def whnfForall (e : Expr) : MetaM Expr := do let e' ← whnf e if e'.isForall then pure e' else pure e def inferType (e : Expr) : MetaM Expr := withIncRecDepth do (← inferTypeRef.get) e protected def isExprDefEqAux (t s : Expr) : MetaM Bool := withIncRecDepth do (← isExprDefEqAuxRef.get) t s protected def synthPending (mvarId : MVarId) : MetaM Bool := withIncRecDepth do (← synthPendingRef.get) mvarId -- withIncRecDepth for a monad `n` such that `[MonadControlT MetaM n]` protected def withIncRecDepth (x : n α) : n α := mapMetaM (withIncRecDepth (m := MetaM)) x private def mkFreshExprMVarAtCore (mvarId : MVarId) (lctx : LocalContext) (localInsts : LocalInstances) (type : Expr) (kind : MetavarKind) (userName : Name) (numScopeArgs : Nat) : MetaM Expr := do modifyMCtx fun mctx => mctx.addExprMVarDecl mvarId userName lctx localInsts type kind numScopeArgs; return mkMVar mvarId def mkFreshExprMVarAt (lctx : LocalContext) (localInsts : LocalInstances) (type : Expr) (kind : MetavarKind := MetavarKind.natural) (userName : Name := Name.anonymous) (numScopeArgs : Nat := 0) : MetaM Expr := do let mvarId ← mkFreshId mkFreshExprMVarAtCore mvarId lctx localInsts type kind userName numScopeArgs def mkFreshLevelMVar : MetaM Level := do let mvarId ← mkFreshId modifyMCtx fun mctx => mctx.addLevelMVarDecl mvarId; return mkLevelMVar mvarId private def mkFreshExprMVarCore (type : Expr) (kind : MetavarKind) (userName : Name) : MetaM Expr := do let lctx ← getLCtx let localInsts ← getLocalInstances mkFreshExprMVarAt lctx localInsts type kind userName private def mkFreshExprMVarImpl (type? : Option Expr) (kind : MetavarKind) (userName : Name) : MetaM Expr := match type? with | some type => mkFreshExprMVarCore type kind userName | none => do let u ← mkFreshLevelMVar let type ← mkFreshExprMVarCore (mkSort u) MetavarKind.natural Name.anonymous mkFreshExprMVarCore type kind userName def mkFreshExprMVar (type? : Option Expr) (kind := MetavarKind.natural) (userName := Name.anonymous) : MetaM Expr := mkFreshExprMVarImpl type? kind userName def mkFreshTypeMVar (kind := MetavarKind.natural) (userName := Name.anonymous) : MetaM Expr := do let u ← mkFreshLevelMVar mkFreshExprMVar (mkSort u) kind userName /- Low-level version of `MkFreshExprMVar` which allows users to create/reserve a `mvarId` using `mkFreshId`, and then later create the metavar using this method. -/ private def mkFreshExprMVarWithIdCore (mvarId : MVarId) (type : Expr) (kind : MetavarKind := MetavarKind.natural) (userName : Name := Name.anonymous) (numScopeArgs : Nat := 0) : MetaM Expr := do let lctx ← getLCtx let localInsts ← getLocalInstances mkFreshExprMVarAtCore mvarId lctx localInsts type kind userName numScopeArgs def mkFreshExprMVarWithId (mvarId : MVarId) (type? : Option Expr := none) (kind : MetavarKind := MetavarKind.natural) (userName := Name.anonymous) : MetaM Expr := match type? with | some type => mkFreshExprMVarWithIdCore mvarId type kind userName | none => do let u ← mkFreshLevelMVar let type ← mkFreshExprMVar (mkSort u) mkFreshExprMVarWithIdCore mvarId type kind userName def mkFreshLevelMVars (num : Nat) : MetaM (List Level) := num.foldM (init := []) fun _ us => return (← mkFreshLevelMVar)::us def mkFreshLevelMVarsFor (info : ConstantInfo) : MetaM (List Level) := mkFreshLevelMVars info.numLevelParams def mkConstWithFreshMVarLevels (declName : Name) : MetaM Expr := do let info ← getConstInfo declName return mkConst declName (← mkFreshLevelMVarsFor info) def getTransparency : MetaM TransparencyMode := return (← getConfig).transparency def shouldReduceAll : MetaM Bool := return (← getTransparency) == TransparencyMode.all def shouldReduceReducibleOnly : MetaM Bool := return (← getTransparency) == TransparencyMode.reducible def getMVarDecl (mvarId : MVarId) : MetaM MetavarDecl := do let mctx ← getMCtx match mctx.findDecl? mvarId with | some d => pure d | none => throwError "unknown metavariable '?{mvarId}'" def setMVarKind (mvarId : MVarId) (kind : MetavarKind) : MetaM Unit := modifyMCtx fun mctx => mctx.setMVarKind mvarId kind /- Update the type of the given metavariable. This function assumes the new type is definitionally equal to the current one -/ def setMVarType (mvarId : MVarId) (type : Expr) : MetaM Unit := do modifyMCtx fun mctx => mctx.setMVarType mvarId type def isReadOnlyExprMVar (mvarId : MVarId) : MetaM Bool := do let mvarDecl ← getMVarDecl mvarId let mctx ← getMCtx return mvarDecl.depth != mctx.depth def isReadOnlyOrSyntheticOpaqueExprMVar (mvarId : MVarId) : MetaM Bool := do let mvarDecl ← getMVarDecl mvarId match mvarDecl.kind with | MetavarKind.syntheticOpaque => pure true | _ => let mctx ← getMCtx return mvarDecl.depth != mctx.depth def isReadOnlyLevelMVar (mvarId : MVarId) : MetaM Bool := do let mctx ← getMCtx match mctx.findLevelDepth? mvarId with | some depth => return depth != mctx.depth | _ => throwError "unknown universe metavariable '?{mvarId}'" def renameMVar (mvarId : MVarId) (newUserName : Name) : MetaM Unit := modifyMCtx fun mctx => mctx.renameMVar mvarId newUserName def isExprMVarAssigned (mvarId : MVarId) : MetaM Bool := return (← getMCtx).isExprAssigned mvarId def getExprMVarAssignment? (mvarId : MVarId) : MetaM (Option Expr) := return (← getMCtx).getExprAssignment? mvarId /-- Return true if `e` contains `mvarId` directly or indirectly -/ def occursCheck (mvarId : MVarId) (e : Expr) : MetaM Bool := return (← getMCtx).occursCheck mvarId e def assignExprMVar (mvarId : MVarId) (val : Expr) : MetaM Unit := modifyMCtx fun mctx => mctx.assignExpr mvarId val def isDelayedAssigned (mvarId : MVarId) : MetaM Bool := return (← getMCtx).isDelayedAssigned mvarId def getDelayedAssignment? (mvarId : MVarId) : MetaM (Option DelayedMetavarAssignment) := return (← getMCtx).getDelayedAssignment? mvarId def hasAssignableMVar (e : Expr) : MetaM Bool := return (← getMCtx).hasAssignableMVar e def throwUnknownFVar (fvarId : FVarId) : MetaM α := throwError "unknown free variable '{mkFVar fvarId}'" def findLocalDecl? (fvarId : FVarId) : MetaM (Option LocalDecl) := return (← getLCtx).find? fvarId def getLocalDecl (fvarId : FVarId) : MetaM LocalDecl := do match (← getLCtx).find? fvarId with | some d => pure d | none => throwUnknownFVar fvarId def getFVarLocalDecl (fvar : Expr) : MetaM LocalDecl := getLocalDecl fvar.fvarId! def getLocalDeclFromUserName (userName : Name) : MetaM LocalDecl := do match (← getLCtx).findFromUserName? userName with | some d => pure d | none => throwError "unknown local declaration '{userName}'" def instantiateLevelMVars (u : Level) : MetaM Level := MetavarContext.instantiateLevelMVars u def instantiateMVars (e : Expr) : MetaM Expr := (MetavarContext.instantiateExprMVars e).run def instantiateLocalDeclMVars (localDecl : LocalDecl) : MetaM LocalDecl := do match localDecl with | LocalDecl.cdecl idx id n type bi => let type ← instantiateMVars type return LocalDecl.cdecl idx id n type bi | LocalDecl.ldecl idx id n type val nonDep => let type ← instantiateMVars type let val ← instantiateMVars val return LocalDecl.ldecl idx id n type val nonDep @[inline] def liftMkBindingM (x : MetavarContext.MkBindingM α) : MetaM α := do match x (← getLCtx) { mctx := (← getMCtx), ngen := (← getNGen) } with | EStateM.Result.ok e newS => do setNGen newS.ngen; setMCtx newS.mctx; pure e | EStateM.Result.error (MetavarContext.MkBinding.Exception.revertFailure mctx lctx toRevert decl) newS => do setMCtx newS.mctx; setNGen newS.ngen; throwError "failed to create binder due to failure when reverting variable dependencies" def mkForallFVars (xs : Array Expr) (e : Expr) (usedOnly : Bool := false) (usedLetOnly : Bool := true) : MetaM Expr := if xs.isEmpty then pure e else liftMkBindingM <| MetavarContext.mkForall xs e usedOnly usedLetOnly def mkLambdaFVars (xs : Array Expr) (e : Expr) (usedOnly : Bool := false) (usedLetOnly : Bool := true) : MetaM Expr := if xs.isEmpty then pure e else liftMkBindingM <| MetavarContext.mkLambda xs e usedOnly usedLetOnly def mkLetFVars (xs : Array Expr) (e : Expr) : MetaM Expr := mkLambdaFVars xs e def mkArrow (d b : Expr) : MetaM Expr := do let n ← mkFreshUserName `x return Lean.mkForall n BinderInfo.default d b def elimMVarDeps (xs : Array Expr) (e : Expr) (preserveOrder : Bool := false) : MetaM Expr := if xs.isEmpty then pure e else liftMkBindingM <| MetavarContext.elimMVarDeps xs e preserveOrder @[inline] def withConfig (f : Config → Config) : n α → n α := mapMetaM <| withReader (fun ctx => { ctx with config := f ctx.config }) @[inline] def withTrackingZeta (x : n α) : n α := withConfig (fun cfg => { cfg with trackZeta := true }) x @[inline] def withTransparency (mode : TransparencyMode) : n α → n α := mapMetaM <| withConfig (fun config => { config with transparency := mode }) @[inline] def withDefault (x : n α) : n α := withTransparency TransparencyMode.default x @[inline] def withReducible (x : n α) : n α := withTransparency TransparencyMode.reducible x @[inline] def withReducibleAndInstances (x : n α) : n α := withTransparency TransparencyMode.instances x @[inline] def withAtLeastTransparency (mode : TransparencyMode) (x : n α) : n α := withConfig (fun config => let oldMode := config.transparency let mode := if oldMode.lt mode then mode else oldMode { config with transparency := mode }) x /-- Save cache, execute `x`, restore cache -/ @[inline] private def savingCacheImpl (x : MetaM α) : MetaM α := do let s ← get let savedCache := s.cache try x finally modify fun s => { s with cache := savedCache } @[inline] def savingCache : n α → n α := mapMetaM savingCacheImpl def getTheoremInfo (info : ConstantInfo) : MetaM (Option ConstantInfo) := do if (← shouldReduceAll) then return some info else return none private def getDefInfoTemp (info : ConstantInfo) : MetaM (Option ConstantInfo) := do match (← getTransparency) with | TransparencyMode.all => return some info | TransparencyMode.default => return some info | _ => if (← isReducible info.name) then return some info else return none /- Remark: we later define `getConst?` at `GetConst.lean` after we define `Instances.lean`. This method is only used to implement `isClassQuickConst?`. It is very similar to `getConst?`, but it returns none when `TransparencyMode.instances` and `constName` is an instance. This difference should be irrelevant for `isClassQuickConst?`. -/ private def getConstTemp? (constName : Name) : MetaM (Option ConstantInfo) := do let env ← getEnv match env.find? constName with | some (info@(ConstantInfo.thmInfo _)) => getTheoremInfo info | some (info@(ConstantInfo.defnInfo _)) => getDefInfoTemp info | some info => pure (some info) | none => throwUnknownConstant constName private def isClassQuickConst? (constName : Name) : MetaM (LOption Name) := do let env ← getEnv if isClass env constName then pure (LOption.some constName) else match (← getConstTemp? constName) with | some _ => pure LOption.undef | none => pure LOption.none private partial def isClassQuick? : Expr → MetaM (LOption Name) | Expr.bvar .. => pure LOption.none | Expr.lit .. => pure LOption.none | Expr.fvar .. => pure LOption.none | Expr.sort .. => pure LOption.none | Expr.lam .. => pure LOption.none | Expr.letE .. => pure LOption.undef | Expr.proj .. => pure LOption.undef | Expr.forallE _ _ b _ => isClassQuick? b | Expr.mdata _ e _ => isClassQuick? e | Expr.const n _ _ => isClassQuickConst? n | Expr.mvar mvarId _ => do match (← getExprMVarAssignment? mvarId) with | some val => isClassQuick? val | none => pure LOption.none | Expr.app f _ _ => match f.getAppFn with | Expr.const n .. => isClassQuickConst? n | Expr.lam .. => pure LOption.undef | _ => pure LOption.none def saveAndResetSynthInstanceCache : MetaM SynthInstanceCache := do let s ← get let savedSythInstance := s.cache.synthInstance modifyCache fun c => { c with synthInstance := {} } pure savedSythInstance def restoreSynthInstanceCache (cache : SynthInstanceCache) : MetaM Unit := modifyCache fun c => { c with synthInstance := cache } @[inline] private def resettingSynthInstanceCacheImpl (x : MetaM α) : MetaM α := do let savedSythInstance ← saveAndResetSynthInstanceCache try x finally restoreSynthInstanceCache savedSythInstance /-- Reset `synthInstance` cache, execute `x`, and restore cache -/ @[inline] def resettingSynthInstanceCache : n α → n α := mapMetaM resettingSynthInstanceCacheImpl @[inline] def resettingSynthInstanceCacheWhen (b : Bool) (x : n α) : n α := if b then resettingSynthInstanceCache x else x private def withNewLocalInstanceImp (className : Name) (fvar : Expr) (k : MetaM α) : MetaM α := do let localDecl ← getFVarLocalDecl fvar /- Recall that we use `auxDecl` binderInfo when compiling recursive declarations. -/ match localDecl.binderInfo with | BinderInfo.auxDecl => k | _ => resettingSynthInstanceCache <| withReader (fun ctx => { ctx with localInstances := ctx.localInstances.push { className := className, fvar := fvar } }) k /-- Add entry `{ className := className, fvar := fvar }` to localInstances, and then execute continuation `k`. It resets the type class cache using `resettingSynthInstanceCache`. -/ def withNewLocalInstance (className : Name) (fvar : Expr) : n α → n α := mapMetaM <| withNewLocalInstanceImp className fvar private def fvarsSizeLtMaxFVars (fvars : Array Expr) (maxFVars? : Option Nat) : Bool := match maxFVars? with | some maxFVars => fvars.size < maxFVars | none => true mutual /-- `withNewLocalInstances isClassExpensive fvars j k` updates the vector or local instances using free variables `fvars[j] ... fvars.back`, and execute `k`. - `isClassExpensive` is defined later. - The type class chache is reset whenever a new local instance is found. - `isClassExpensive` uses `whnf` which depends (indirectly) on the set of local instances. Thus, each new local instance requires a new `resettingSynthInstanceCache`. -/ private partial def withNewLocalInstancesImp (fvars : Array Expr) (i : Nat) (k : MetaM α) : MetaM α := do if h : i < fvars.size then let fvar := fvars.get ⟨i, h⟩ let decl ← getFVarLocalDecl fvar match (← isClassQuick? decl.type) with | LOption.none => withNewLocalInstancesImp fvars (i+1) k | LOption.undef => match (← isClassExpensive? decl.type) with | none => withNewLocalInstancesImp fvars (i+1) k | some c => withNewLocalInstance c fvar <| withNewLocalInstancesImp fvars (i+1) k | LOption.some c => withNewLocalInstance c fvar <| withNewLocalInstancesImp fvars (i+1) k else k /-- `forallTelescopeAuxAux lctx fvars j type` Remarks: - `lctx` is the `MetaM` local context extended with declarations for `fvars`. - `type` is the type we are computing the telescope for. It contains only dangling bound variables in the range `[j, fvars.size)` - if `reducing? == true` and `type` is not `forallE`, we use `whnf`. - when `type` is not a `forallE` nor it can't be reduced to one, we excute the continuation `k`. Here is an example that demonstrates the `reducing?`. Suppose we have ``` abbrev StateM s a := s -> Prod a s ``` Now, assume we are trying to build the telescope for ``` forall (x : Nat), StateM Int Bool ``` if `reducing == true`, the function executes `k #[(x : Nat) (s : Int)] Bool`. if `reducing == false`, the function executes `k #[(x : Nat)] (StateM Int Bool)` if `maxFVars?` is `some max`, then we interrupt the telescope construction when `fvars.size == max` -/ private partial def forallTelescopeReducingAuxAux (reducing : Bool) (maxFVars? : Option Nat) (type : Expr) (k : Array Expr → Expr → MetaM α) : MetaM α := do let rec process (lctx : LocalContext) (fvars : Array Expr) (j : Nat) (type : Expr) : MetaM α := do match type with | Expr.forallE n d b c => if fvarsSizeLtMaxFVars fvars maxFVars? then let d := d.instantiateRevRange j fvars.size fvars let fvarId ← mkFreshId let lctx := lctx.mkLocalDecl fvarId n d c.binderInfo let fvar := mkFVar fvarId let fvars := fvars.push fvar process lctx fvars j b else let type := type.instantiateRevRange j fvars.size fvars; withReader (fun ctx => { ctx with lctx := lctx }) do withNewLocalInstancesImp fvars j do k fvars type | _ => let type := type.instantiateRevRange j fvars.size fvars; withReader (fun ctx => { ctx with lctx := lctx }) do withNewLocalInstancesImp fvars j do if reducing && fvarsSizeLtMaxFVars fvars maxFVars? then let newType ← whnf type if newType.isForall then process lctx fvars fvars.size newType else k fvars type else k fvars type process (← getLCtx) #[] 0 type private partial def forallTelescopeReducingAux (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → MetaM α) : MetaM α := do match maxFVars? with | some 0 => k #[] type | _ => do let newType ← whnf type if newType.isForall then forallTelescopeReducingAuxAux true maxFVars? newType k else k #[] type private partial def isClassExpensive? : Expr → MetaM (Option Name) | type => withReducible <| -- when testing whether a type is a type class, we only unfold reducible constants. forallTelescopeReducingAux type none fun xs type => do let env ← getEnv match type.getAppFn with | Expr.const c _ _ => do if isClass env c then return some c else -- make sure abbreviations are unfolded match (← whnf type).getAppFn with | Expr.const c _ _ => return if isClass env c then some c else none | _ => return none | _ => return none private partial def isClassImp? (type : Expr) : MetaM (Option Name) := do match (← isClassQuick? type) with | LOption.none => pure none | LOption.some c => pure (some c) | LOption.undef => isClassExpensive? type end def isClass? (type : Expr) : MetaM (Option Name) := try isClassImp? type catch _ => pure none private def withNewLocalInstancesImpAux (fvars : Array Expr) (j : Nat) : n α → n α := mapMetaM <| withNewLocalInstancesImp fvars j partial def withNewLocalInstances (fvars : Array Expr) (j : Nat) : n α → n α := mapMetaM <| withNewLocalInstancesImpAux fvars j @[inline] private def forallTelescopeImp (type : Expr) (k : Array Expr → Expr → MetaM α) : MetaM α := do forallTelescopeReducingAuxAux (reducing := false) (maxFVars? := none) type k /-- Given `type` of the form `forall xs, A`, execute `k xs A`. This combinator will declare local declarations, create free variables for them, execute `k` with updated local context, and make sure the cache is restored after executing `k`. -/ def forallTelescope (type : Expr) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => forallTelescopeImp type k) k private def forallTelescopeReducingImp (type : Expr) (k : Array Expr → Expr → MetaM α) : MetaM α := forallTelescopeReducingAux type (maxFVars? := none) k /-- Similar to `forallTelescope`, but given `type` of the form `forall xs, A`, it reduces `A` and continues bulding the telescope if it is a `forall`. -/ def forallTelescopeReducing (type : Expr) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => forallTelescopeReducingImp type k) k private def forallBoundedTelescopeImp (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → MetaM α) : MetaM α := forallTelescopeReducingAux type maxFVars? k /-- Similar to `forallTelescopeReducing`, stops constructing the telescope when it reaches size `maxFVars`. -/ def forallBoundedTelescope (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => forallBoundedTelescopeImp type maxFVars? k) k /-- Similar to `forallTelescopeAuxAux` but for lambda and let expressions. -/ private partial def lambdaTelescopeAux (k : Array Expr → Expr → MetaM α) : Bool → LocalContext → Array Expr → Nat → Expr → MetaM α | consumeLet, lctx, fvars, j, Expr.lam n d b c => do let d := d.instantiateRevRange j fvars.size fvars let fvarId ← mkFreshId let lctx := lctx.mkLocalDecl fvarId n d c.binderInfo let fvar := mkFVar fvarId lambdaTelescopeAux k consumeLet lctx (fvars.push fvar) j b | true, lctx, fvars, j, Expr.letE n t v b _ => do let t := t.instantiateRevRange j fvars.size fvars let v := v.instantiateRevRange j fvars.size fvars let fvarId ← mkFreshId let lctx := lctx.mkLetDecl fvarId n t v let fvar := mkFVar fvarId lambdaTelescopeAux k true lctx (fvars.push fvar) j b | _, lctx, fvars, j, e => let e := e.instantiateRevRange j fvars.size fvars; withReader (fun ctx => { ctx with lctx := lctx }) do withNewLocalInstancesImp fvars j do k fvars e private partial def lambdaTelescopeImp (e : Expr) (consumeLet : Bool) (k : Array Expr → Expr → MetaM α) : MetaM α := do let rec process (consumeLet : Bool) (lctx : LocalContext) (fvars : Array Expr) (j : Nat) (e : Expr) : MetaM α := do match consumeLet, e with | _, Expr.lam n d b c => let d := d.instantiateRevRange j fvars.size fvars let fvarId ← mkFreshId let lctx := lctx.mkLocalDecl fvarId n d c.binderInfo let fvar := mkFVar fvarId process consumeLet lctx (fvars.push fvar) j b | true, Expr.letE n t v b _ => do let t := t.instantiateRevRange j fvars.size fvars let v := v.instantiateRevRange j fvars.size fvars let fvarId ← mkFreshId let lctx := lctx.mkLetDecl fvarId n t v let fvar := mkFVar fvarId process true lctx (fvars.push fvar) j b | _, e => let e := e.instantiateRevRange j fvars.size fvars withReader (fun ctx => { ctx with lctx := lctx }) do withNewLocalInstancesImp fvars j do k fvars e process consumeLet (← getLCtx) #[] 0 e /-- Similar to `forallTelescope` but for lambda and let expressions. -/ def lambdaLetTelescope (type : Expr) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => lambdaTelescopeImp type true k) k /-- Similar to `forallTelescope` but for lambda expressions. -/ def lambdaTelescope (type : Expr) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => lambdaTelescopeImp type false k) k /-- Return the parameter names for the givel global declaration. -/ def getParamNames (declName : Name) : MetaM (Array Name) := do let cinfo ← getConstInfo declName forallTelescopeReducing cinfo.type fun xs _ => do xs.mapM fun x => do let localDecl ← getLocalDecl x.fvarId! pure localDecl.userName -- `kind` specifies the metavariable kind for metavariables not corresponding to instance implicit `[ ... ]` arguments. private partial def forallMetaTelescopeReducingAux (e : Expr) (reducing : Bool) (maxMVars? : Option Nat) (kind : MetavarKind) : MetaM (Array Expr × Array BinderInfo × Expr) := let rec process (mvars : Array Expr) (bis : Array BinderInfo) (j : Nat) (type : Expr) : MetaM (Array Expr × Array BinderInfo × Expr) := do match type with | Expr.forallE n d b c => let cont : Unit → MetaM (Array Expr × Array BinderInfo × Expr) := fun _ => do let d := d.instantiateRevRange j mvars.size mvars let k := if c.binderInfo.isInstImplicit then MetavarKind.synthetic else kind let mvar ← mkFreshExprMVar d k n let mvars := mvars.push mvar let bis := bis.push c.binderInfo process mvars bis j b match maxMVars? with | none => cont () | some maxMVars => if mvars.size < maxMVars then cont () else let type := type.instantiateRevRange j mvars.size mvars; pure (mvars, bis, type) | _ => let type := type.instantiateRevRange j mvars.size mvars; if reducing then do let newType ← whnf type; if newType.isForall then process mvars bis mvars.size newType else pure (mvars, bis, type) else pure (mvars, bis, type) process #[] #[] 0 e /-- Similar to `forallTelescope`, but creates metavariables instead of free variables. -/ def forallMetaTelescope (e : Expr) (kind := MetavarKind.natural) : MetaM (Array Expr × Array BinderInfo × Expr) := forallMetaTelescopeReducingAux e (reducing := false) (maxMVars? := none) kind /-- Similar to `forallTelescopeReducing`, but creates metavariables instead of free variables. -/ def forallMetaTelescopeReducing (e : Expr) (maxMVars? : Option Nat := none) (kind := MetavarKind.natural) : MetaM (Array Expr × Array BinderInfo × Expr) := forallMetaTelescopeReducingAux e (reducing := true) maxMVars? kind /-- Similar to `forallMetaTelescopeReducingAux` but for lambda expressions. -/ partial def lambdaMetaTelescope (e : Expr) (maxMVars? : Option Nat := none) : MetaM (Array Expr × Array BinderInfo × Expr) := let rec process (mvars : Array Expr) (bis : Array BinderInfo) (j : Nat) (type : Expr) : MetaM (Array Expr × Array BinderInfo × Expr) := do let finalize : Unit → MetaM (Array Expr × Array BinderInfo × Expr) := fun _ => do let type := type.instantiateRevRange j mvars.size mvars pure (mvars, bis, type) let cont : Unit → MetaM (Array Expr × Array BinderInfo × Expr) := fun _ => do match type with | Expr.lam n d b c => let d := d.instantiateRevRange j mvars.size mvars let mvar ← mkFreshExprMVar d let mvars := mvars.push mvar let bis := bis.push c.binderInfo process mvars bis j b | _ => finalize () match maxMVars? with | none => cont () | some maxMVars => if mvars.size < maxMVars then cont () else finalize () process #[] #[] 0 e private def withNewFVar (fvar fvarType : Expr) (k : Expr → MetaM α) : MetaM α := do match (← isClass? fvarType) with | none => k fvar | some c => withNewLocalInstance c fvar <| k fvar private def withLocalDeclImp (n : Name) (bi : BinderInfo) (type : Expr) (k : Expr → MetaM α) : MetaM α := do let fvarId ← mkFreshId let ctx ← read let lctx := ctx.lctx.mkLocalDecl fvarId n type bi let fvar := mkFVar fvarId withReader (fun ctx => { ctx with lctx := lctx }) do withNewFVar fvar type k def withLocalDecl (name : Name) (bi : BinderInfo) (type : Expr) (k : Expr → n α) : n α := map1MetaM (fun k => withLocalDeclImp name bi type k) k def withLocalDeclD (name : Name) (type : Expr) (k : Expr → n α) : n α := withLocalDecl name BinderInfo.default type k private def withLetDeclImp (n : Name) (type : Expr) (val : Expr) (k : Expr → MetaM α) : MetaM α := do let fvarId ← mkFreshId let ctx ← read let lctx := ctx.lctx.mkLetDecl fvarId n type val let fvar := mkFVar fvarId withReader (fun ctx => { ctx with lctx := lctx }) do withNewFVar fvar type k def withLetDecl (name : Name) (type : Expr) (val : Expr) (k : Expr → n α) : n α := map1MetaM (fun k => withLetDeclImp name type val k) k private def withExistingLocalDeclsImp (decls : List LocalDecl) (k : MetaM α) : MetaM α := do let ctx ← read let numLocalInstances := ctx.localInstances.size let lctx := decls.foldl (fun (lctx : LocalContext) decl => lctx.addDecl decl) ctx.lctx withReader (fun ctx => { ctx with lctx := lctx }) do let newLocalInsts ← decls.foldlM (fun (newlocalInsts : Array LocalInstance) (decl : LocalDecl) => (do { match (← isClass? decl.type) with | none => pure newlocalInsts | some c => pure <| newlocalInsts.push { className := c, fvar := decl.toExpr } } : MetaM _)) ctx.localInstances; if newLocalInsts.size == numLocalInstances then k else resettingSynthInstanceCache <| withReader (fun ctx => { ctx with localInstances := newLocalInsts }) k def withExistingLocalDecls (decls : List LocalDecl) : n α → n α := mapMetaM <| withExistingLocalDeclsImp decls private def withNewMCtxDepthImp (x : MetaM α) : MetaM α := do let s ← get let savedMCtx := s.mctx modifyMCtx fun mctx => mctx.incDepth try x finally setMCtx savedMCtx /-- Save cache and `MetavarContext`, bump the `MetavarContext` depth, execute `x`, and restore saved data. -/ def withNewMCtxDepth : n α → n α := mapMetaM withNewMCtxDepthImp private def withLocalContextImp (lctx : LocalContext) (localInsts : LocalInstances) (x : MetaM α) : MetaM α := do let localInstsCurr ← getLocalInstances withReader (fun ctx => { ctx with lctx := lctx, localInstances := localInsts }) do if localInsts == localInstsCurr then x else resettingSynthInstanceCache x def withLCtx (lctx : LocalContext) (localInsts : LocalInstances) : n α → n α := mapMetaM <| withLocalContextImp lctx localInsts private def withMVarContextImp (mvarId : MVarId) (x : MetaM α) : MetaM α := do let mvarDecl ← getMVarDecl mvarId withLocalContextImp mvarDecl.lctx mvarDecl.localInstances x /-- Execute `x` using the given metavariable `LocalContext` and `LocalInstances`. The type class resolution cache is flushed when executing `x` if its `LocalInstances` are different from the current ones. -/ def withMVarContext (mvarId : MVarId) : n α → n α := mapMetaM <| withMVarContextImp mvarId private def withMCtxImp (mctx : MetavarContext) (x : MetaM α) : MetaM α := do let mctx' ← getMCtx setMCtx mctx try x finally setMCtx mctx' def withMCtx (mctx : MetavarContext) : n α → n α := mapMetaM <| withMCtxImp mctx @[inline] private def approxDefEqImp (x : MetaM α) : MetaM α := withConfig (fun config => { config with foApprox := true, ctxApprox := true, quasiPatternApprox := true}) x /-- Execute `x` using approximate unification: `foApprox`, `ctxApprox` and `quasiPatternApprox`. -/ @[inline] def approxDefEq : n α → n α := mapMetaM approxDefEqImp @[inline] private def fullApproxDefEqImp (x : MetaM α) : MetaM α := withConfig (fun config => { config with foApprox := true, ctxApprox := true, quasiPatternApprox := true, constApprox := true }) x /-- Similar to `approxDefEq`, but uses all available approximations. We don't use `constApprox` by default at `approxDefEq` because it often produces undesirable solution for monadic code. For example, suppose we have `pure (x > 0)` which has type `?m Prop`. We also have the goal `[Pure ?m]`. Now, assume the expected type is `IO Bool`. Then, the unification constraint `?m Prop =?= IO Bool` could be solved as `?m := fun _ => IO Bool` using `constApprox`, but this spurious solution would generate a failure when we try to solve `[Pure (fun _ => IO Bool)]` -/ @[inline] def fullApproxDefEq : n α → n α := mapMetaM fullApproxDefEqImp def normalizeLevel (u : Level) : MetaM Level := do let u ← instantiateLevelMVars u pure u.normalize def assignLevelMVar (mvarId : MVarId) (u : Level) : MetaM Unit := do modifyMCtx fun mctx => mctx.assignLevel mvarId u def whnfR (e : Expr) : MetaM Expr := withTransparency TransparencyMode.reducible <| whnf e def whnfD (e : Expr) : MetaM Expr := withTransparency TransparencyMode.default <| whnf e def whnfI (e : Expr) : MetaM Expr := withTransparency TransparencyMode.instances <| whnf e def setInlineAttribute (declName : Name) (kind := Compiler.InlineAttributeKind.inline): MetaM Unit := do let env ← getEnv match Compiler.setInlineAttribute env declName kind with | Except.ok env => setEnv env | Except.error msg => throwError msg private partial def instantiateForallAux (ps : Array Expr) (i : Nat) (e : Expr) : MetaM Expr := do if h : i < ps.size then let p := ps.get ⟨i, h⟩ let e ← whnf e match e with | Expr.forallE _ _ b _ => instantiateForallAux ps (i+1) (b.instantiate1 p) | _ => throwError "invalid instantiateForall, too many parameters" else pure e /- Given `e` of the form `forall (a_1 : A_1) ... (a_n : A_n), B[a_1, ..., a_n]` and `p_1 : A_1, ... p_n : A_n`, return `B[p_1, ..., p_n]`. -/ def instantiateForall (e : Expr) (ps : Array Expr) : MetaM Expr := instantiateForallAux ps 0 e private partial def instantiateLambdaAux (ps : Array Expr) (i : Nat) (e : Expr) : MetaM Expr := do if h : i < ps.size then let p := ps.get ⟨i, h⟩ let e ← whnf e match e with | Expr.lam _ _ b _ => instantiateLambdaAux ps (i+1) (b.instantiate1 p) | _ => throwError "invalid instantiateLambda, too many parameters" else pure e /- Given `e` of the form `fun (a_1 : A_1) ... (a_n : A_n) => t[a_1, ..., a_n]` and `p_1 : A_1, ... p_n : A_n`, return `t[p_1, ..., p_n]`. It uses `whnf` to reduce `e` if it is not a lambda -/ def instantiateLambda (e : Expr) (ps : Array Expr) : MetaM Expr := instantiateLambdaAux ps 0 e /-- Return true iff `e` depends on the free variable `fvarId` -/ def dependsOn (e : Expr) (fvarId : FVarId) : MetaM Bool := return (← getMCtx).exprDependsOn e fvarId def ppExpr (e : Expr) : MetaM Format := do let env ← getEnv let mctx ← getMCtx let lctx ← getLCtx let opts ← getOptions let ctxCore ← readThe Core.Context Lean.ppExpr { env := env, mctx := mctx, lctx := lctx, opts := opts, currNamespace := ctxCore.currNamespace, openDecls := ctxCore.openDecls } e @[inline] protected def orelse (x y : MetaM α) : MetaM α := do let env ← getEnv let mctx ← getMCtx try x catch _ => setEnv env; setMCtx mctx; y instance : OrElse (MetaM α) := ⟨Meta.orelse⟩ @[inline] private def orelseMergeErrorsImp (x y : MetaM α) (mergeRef : Syntax → Syntax → Syntax := fun r₁ r₂ => r₁) (mergeMsg : MessageData → MessageData → MessageData := fun m₁ m₂ => m₁ ++ Format.line ++ m₂) : MetaM α := do let env ← getEnv let mctx ← getMCtx try x catch ex => setEnv env setMCtx mctx match ex with | Exception.error ref₁ m₁ => try y catch | Exception.error ref₂ m₂ => throw <| Exception.error (mergeRef ref₁ ref₂) (mergeMsg m₁ m₂) | ex => throw ex | ex => throw ex /-- Similar to `orelse`, but merge errors. Note that internal errors are not caught. The default `mergeRef` uses the `ref` (position information) for the first message. The default `mergeMsg` combines error messages using `Format.line ++ Format.line` as a separator. -/ @[inline] def orelseMergeErrors [MonadControlT MetaM m] [Monad m] (x y : m α) (mergeRef : Syntax → Syntax → Syntax := fun r₁ r₂ => r₁) (mergeMsg : MessageData → MessageData → MessageData := fun m₁ m₂ => m₁ ++ Format.line ++ Format.line ++ m₂) : m α := do controlAt MetaM fun runInBase => orelseMergeErrorsImp (runInBase x) (runInBase y) mergeRef mergeMsg /-- Execute `x`, and apply `f` to the produced error message -/ def mapErrorImp (x : MetaM α) (f : MessageData → MessageData) : MetaM α := do try x catch | Exception.error ref msg => throw <| Exception.error ref <| f msg | ex => throw ex @[inline] def mapError [MonadControlT MetaM m] [Monad m] (x : m α) (f : MessageData → MessageData) : m α := controlAt MetaM fun runInBase => mapErrorImp (runInBase x) f end Methods end Meta export Meta (MetaM) end Lean
d3b8f0a8744590f0f1dfca8a788117b62262ad35
c777c32c8e484e195053731103c5e52af26a25d1
/src/measure_theory/measure/finite_measure.lean
2f56da55396ca5bff7f65dc043fb1649a2b8df3a
[ "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
33,195
lean
/- Copyright (c) 2021 Kalle Kytölä. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kalle Kytölä -/ import topology.continuous_function.bounded import topology.algebra.module.weak_dual import measure_theory.integral.bochner /-! # Finite measures This file defines the type of finite measures on a given measurable space. When the underlying space has a topology and the measurable space structure (sigma algebra) is finer than the Borel sigma algebra, then the type of finite measures is equipped with the topology of weak convergence of measures. The topology of weak convergence is the coarsest topology w.r.t. which for every bounded continuous `ℝ≥0`-valued function `f`, the integration of `f` against the measure is continuous. ## Main definitions The main definitions are * the type `measure_theory.finite_measure Ω` with the topology of weak convergence; * `measure_theory.finite_measure.to_weak_dual_bcnn : finite_measure Ω → (weak_dual ℝ≥0 (Ω →ᵇ ℝ≥0))` allowing to interpret a finite measure as a continuous linear functional on the space of bounded continuous nonnegative functions on `Ω`. This is used for the definition of the topology of weak convergence. ## Main results * Finite measures `μ` on `Ω` give rise to continuous linear functionals on the space of bounded continuous nonnegative functions on `Ω` via integration: `measure_theory.finite_measure.to_weak_dual_bcnn : finite_measure Ω → (weak_dual ℝ≥0 (Ω →ᵇ ℝ≥0))` * `measure_theory.finite_measure.tendsto_iff_forall_integral_tendsto`: Convergence of finite measures is characterized by the convergence of integrals of all bounded continuous functions. This shows that the chosen definition of topology coincides with the common textbook definition of weak convergence of measures. A similar characterization by the convergence of integrals (in the `measure_theory.lintegral` sense) of all bounded continuous nonnegative functions is `measure_theory.finite_measure.tendsto_iff_forall_lintegral_tendsto`. ## Implementation notes The topology of weak convergence of finite Borel measures is defined using a mapping from `measure_theory.finite_measure Ω` to `weak_dual ℝ≥0 (Ω →ᵇ ℝ≥0)`, inheriting the topology from the latter. The implementation of `measure_theory.finite_measure Ω` and is directly as a subtype of `measure_theory.measure Ω`, and the coercion to a function is the composition `ennreal.to_nnreal` and the coercion to function of `measure_theory.measure Ω`. Another alternative would have been to use a bijection with `measure_theory.vector_measure Ω ℝ≥0` as an intermediate step. Some considerations: * Potential advantages of using the `nnreal`-valued vector measure alternative: * The coercion to function would avoid need to compose with `ennreal.to_nnreal`, the `nnreal`-valued API could be more directly available. * Potential drawbacks of the vector measure alternative: * The coercion to function would lose monotonicity, as non-measurable sets would be defined to have measure 0. * No integration theory directly. E.g., the topology definition requires `measure_theory.lintegral` w.r.t. a coercion to `measure_theory.measure Ω` in any case. ## References * [Billingsley, *Convergence of probability measures*][billingsley1999] ## Tags weak convergence of measures, finite measure -/ noncomputable theory open measure_theory open set open filter open bounded_continuous_function open_locale topology ennreal nnreal bounded_continuous_function namespace measure_theory namespace finite_measure section finite_measure /-! ### Finite measures In this section we define the `Type` of `finite_measure Ω`, when `Ω` is a measurable space. Finite measures on `Ω` are a module over `ℝ≥0`. If `Ω` is moreover a topological space and the sigma algebra on `Ω` is finer than the Borel sigma algebra (i.e. `[opens_measurable_space Ω]`), then `finite_measure Ω` is equipped with the topology of weak convergence of measures. This is implemented by defining a pairing of finite measures `μ` on `Ω` with continuous bounded nonnegative functions `f : Ω →ᵇ ℝ≥0` via integration, and using the associated weak topology (essentially the weak-star topology on the dual of `Ω →ᵇ ℝ≥0`). -/ variables {Ω : Type*} [measurable_space Ω] /-- Finite measures are defined as the subtype of measures that have the property of being finite measures (i.e., their total mass is finite). -/ def _root_.measure_theory.finite_measure (Ω : Type*) [measurable_space Ω] : Type* := {μ : measure Ω // is_finite_measure μ} /-- A finite measure can be interpreted as a measure. -/ instance : has_coe (finite_measure Ω) (measure_theory.measure Ω) := coe_subtype instance is_finite_measure (μ : finite_measure Ω) : is_finite_measure (μ : measure Ω) := μ.prop instance : has_coe_to_fun (finite_measure Ω) (λ _, set Ω → ℝ≥0) := ⟨λ μ s, (μ s).to_nnreal⟩ lemma coe_fn_eq_to_nnreal_coe_fn_to_measure (ν : finite_measure Ω) : (ν : set Ω → ℝ≥0) = λ s, ((ν : measure Ω) s).to_nnreal := rfl @[simp] lemma ennreal_coe_fn_eq_coe_fn_to_measure (ν : finite_measure Ω) (s : set Ω) : (ν s : ℝ≥0∞) = (ν : measure Ω) s := ennreal.coe_to_nnreal (measure_lt_top ↑ν s).ne @[simp] lemma val_eq_to_measure (ν : finite_measure Ω) : ν.val = (ν : measure Ω) := rfl lemma coe_injective : function.injective (coe : finite_measure Ω → measure Ω) := subtype.coe_injective lemma apply_mono (μ : finite_measure Ω) {s₁ s₂ : set Ω} (h : s₁ ⊆ s₂) : μ s₁ ≤ μ s₂ := begin change ((μ : measure Ω) s₁).to_nnreal ≤ ((μ : measure Ω) s₂).to_nnreal, have key : (μ : measure Ω) s₁ ≤ (μ : measure Ω) s₂ := (μ : measure Ω).mono h, apply (ennreal.to_nnreal_le_to_nnreal (measure_ne_top _ s₁) (measure_ne_top _ s₂)).mpr key, end /-- The (total) mass of a finite measure `μ` is `μ univ`, i.e., the cast to `nnreal` of `(μ : measure Ω) univ`. -/ def mass (μ : finite_measure Ω) : ℝ≥0 := μ univ @[simp] lemma ennreal_mass {μ : finite_measure Ω} : (μ.mass : ℝ≥0∞) = (μ : measure Ω) univ := ennreal_coe_fn_eq_coe_fn_to_measure μ set.univ instance has_zero : has_zero (finite_measure Ω) := { zero := ⟨0, measure_theory.is_finite_measure_zero⟩ } @[simp] lemma zero.mass : (0 : finite_measure Ω).mass = 0 := rfl @[simp] lemma mass_zero_iff (μ : finite_measure Ω) : μ.mass = 0 ↔ μ = 0 := begin refine ⟨λ μ_mass, _, (λ hμ, by simp only [hμ, zero.mass])⟩, ext1, apply measure.measure_univ_eq_zero.mp, rwa [← ennreal_mass, ennreal.coe_eq_zero], end lemma mass_nonzero_iff (μ : finite_measure Ω) : μ.mass ≠ 0 ↔ μ ≠ 0 := begin rw not_iff_not, exact finite_measure.mass_zero_iff μ, end @[ext] lemma eq_of_forall_measure_apply_eq (μ ν : finite_measure Ω) (h : ∀ (s : set Ω), measurable_set s → (μ : measure Ω) s = (ν : measure Ω) s) : μ = ν := by { ext1, ext1 s s_mble, exact h s s_mble, } lemma eq_of_forall_apply_eq (μ ν : finite_measure Ω) (h : ∀ (s : set Ω), measurable_set s → μ s = ν s) : μ = ν := begin ext1 s s_mble, simpa [ennreal_coe_fn_eq_coe_fn_to_measure] using congr_arg (coe : ℝ≥0 → ℝ≥0∞) (h s s_mble), end instance : inhabited (finite_measure Ω) := ⟨0⟩ instance : has_add (finite_measure Ω) := { add := λ μ ν, ⟨μ + ν, measure_theory.is_finite_measure_add⟩ } variables {R : Type*} [has_smul R ℝ≥0] [has_smul R ℝ≥0∞] [is_scalar_tower R ℝ≥0 ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] instance : has_smul R (finite_measure Ω) := { smul := λ (c : R) μ, ⟨c • μ, measure_theory.is_finite_measure_smul_of_nnreal_tower⟩, } @[simp, norm_cast] lemma coe_zero : (coe : finite_measure Ω → measure Ω) 0 = 0 := rfl @[simp, norm_cast] lemma coe_add (μ ν : finite_measure Ω) : ↑(μ + ν) = (↑μ + ↑ν : measure Ω) := rfl @[simp, norm_cast] lemma coe_smul (c : R) (μ : finite_measure Ω) : ↑(c • μ) = (c • ↑μ : measure Ω) := rfl @[simp, norm_cast] lemma coe_fn_zero : (⇑(0 : finite_measure Ω) : set Ω → ℝ≥0) = (0 : set Ω → ℝ≥0) := by { funext, refl, } @[simp, norm_cast] lemma coe_fn_add (μ ν : finite_measure Ω) : (⇑(μ + ν) : set Ω → ℝ≥0) = (⇑μ + ⇑ν : set Ω → ℝ≥0) := by { funext, simp [← ennreal.coe_eq_coe], } @[simp, norm_cast] lemma coe_fn_smul [is_scalar_tower R ℝ≥0 ℝ≥0] (c : R) (μ : finite_measure Ω) : (⇑(c • μ) : set Ω → ℝ≥0) = c • (⇑μ : set Ω → ℝ≥0) := by { funext, simp [← ennreal.coe_eq_coe, ennreal.coe_smul], } instance : add_comm_monoid (finite_measure Ω) := coe_injective.add_comm_monoid coe coe_zero coe_add (λ _ _, coe_smul _ _) /-- Coercion is an `add_monoid_hom`. -/ @[simps] def coe_add_monoid_hom : finite_measure Ω →+ measure Ω := { to_fun := coe, map_zero' := coe_zero, map_add' := coe_add } instance {Ω : Type*} [measurable_space Ω] : module ℝ≥0 (finite_measure Ω) := function.injective.module _ coe_add_monoid_hom coe_injective coe_smul @[simp] lemma coe_fn_smul_apply [is_scalar_tower R ℝ≥0 ℝ≥0] (c : R) (μ : finite_measure Ω) (s : set Ω) : (c • μ) s = c • (μ s) := by { simp only [coe_fn_smul, pi.smul_apply], } /-- Restrict a finite measure μ to a set A. -/ def restrict (μ : finite_measure Ω) (A : set Ω) : finite_measure Ω := { val := (μ : measure Ω).restrict A, property := measure_theory.is_finite_measure_restrict μ A, } lemma restrict_measure_eq (μ : finite_measure Ω) (A : set Ω) : (μ.restrict A : measure Ω) = (μ : measure Ω).restrict A := rfl lemma restrict_apply_measure (μ : finite_measure Ω) (A : set Ω) {s : set Ω} (s_mble : measurable_set s) : (μ.restrict A : measure Ω) s = (μ : measure Ω) (s ∩ A) := measure.restrict_apply s_mble lemma restrict_apply (μ : finite_measure Ω) (A : set Ω) {s : set Ω} (s_mble : measurable_set s) : (μ.restrict A) s = μ (s ∩ A) := begin apply congr_arg ennreal.to_nnreal, exact measure.restrict_apply s_mble, end lemma restrict_mass (μ : finite_measure Ω) (A : set Ω) : (μ.restrict A).mass = μ A := by simp only [mass, restrict_apply μ A measurable_set.univ, univ_inter] lemma restrict_eq_zero_iff (μ : finite_measure Ω) (A : set Ω) : μ.restrict A = 0 ↔ μ A = 0 := by rw [← mass_zero_iff, restrict_mass] lemma restrict_nonzero_iff (μ : finite_measure Ω) (A : set Ω) : μ.restrict A ≠ 0 ↔ μ A ≠ 0 := by rw [← mass_nonzero_iff, restrict_mass] variables [topological_space Ω] /-- The pairing of a finite (Borel) measure `μ` with a nonnegative bounded continuous function is obtained by (Lebesgue) integrating the (test) function against the measure. This is `finite_measure.test_against_nn`. -/ def test_against_nn (μ : finite_measure Ω) (f : Ω →ᵇ ℝ≥0) : ℝ≥0 := (∫⁻ ω, f ω ∂(μ : measure Ω)).to_nnreal lemma _root_.bounded_continuous_function.nnreal.to_ennreal_comp_measurable {Ω : Type*} [topological_space Ω] [measurable_space Ω] [opens_measurable_space Ω] (f : Ω →ᵇ ℝ≥0) : measurable (λ ω, (f ω : ℝ≥0∞)) := measurable_coe_nnreal_ennreal.comp f.continuous.measurable lemma _root_.measure_theory.lintegral_lt_top_of_bounded_continuous_to_nnreal (μ : measure Ω) [is_finite_measure μ] (f : Ω →ᵇ ℝ≥0) : ∫⁻ ω, f ω ∂μ < ∞ := begin apply is_finite_measure.lintegral_lt_top_of_bounded_to_ennreal, use nndist f 0, intros x, have key := bounded_continuous_function.nnreal.upper_bound f x, rw ennreal.coe_le_coe, have eq : nndist f 0 = ⟨dist f 0, dist_nonneg⟩, { ext, simp only [real.coe_to_nnreal', max_eq_left_iff, subtype.coe_mk, coe_nndist], }, rwa eq at key, end @[simp] lemma test_against_nn_coe_eq {μ : finite_measure Ω} {f : Ω →ᵇ ℝ≥0} : (μ.test_against_nn f : ℝ≥0∞) = ∫⁻ ω, f ω ∂(μ : measure Ω) := ennreal.coe_to_nnreal (lintegral_lt_top_of_bounded_continuous_to_nnreal _ f).ne lemma test_against_nn_const (μ : finite_measure Ω) (c : ℝ≥0) : μ.test_against_nn (bounded_continuous_function.const Ω c) = c * μ.mass := by simp [← ennreal.coe_eq_coe] lemma test_against_nn_mono (μ : finite_measure Ω) {f g : Ω →ᵇ ℝ≥0} (f_le_g : (f : Ω → ℝ≥0) ≤ g) : μ.test_against_nn f ≤ μ.test_against_nn g := begin simp only [←ennreal.coe_le_coe, test_against_nn_coe_eq], exact lintegral_mono (λ ω, ennreal.coe_mono (f_le_g ω)), end @[simp] lemma test_against_nn_zero (μ : finite_measure Ω) : μ.test_against_nn 0 = 0 := by simpa only [zero_mul] using μ.test_against_nn_const 0 @[simp] lemma test_against_nn_one (μ : finite_measure Ω) : μ.test_against_nn 1 = μ.mass := begin simp only [test_against_nn, coe_one, pi.one_apply, ennreal.coe_one, lintegral_one], refl, end @[simp] lemma zero.test_against_nn_apply (f : Ω →ᵇ ℝ≥0) : (0 : finite_measure Ω).test_against_nn f = 0 := by simp only [test_against_nn, coe_zero, lintegral_zero_measure, ennreal.zero_to_nnreal] lemma zero.test_against_nn : (0 : finite_measure Ω).test_against_nn = 0 := by { funext, simp only [zero.test_against_nn_apply, pi.zero_apply], } @[simp] lemma smul_test_against_nn_apply (c : ℝ≥0) (μ : finite_measure Ω) (f : Ω →ᵇ ℝ≥0) : (c • μ).test_against_nn f = c • (μ.test_against_nn f) := by simp only [test_against_nn, coe_smul, smul_eq_mul, ← ennreal.smul_to_nnreal, ennreal.smul_def, lintegral_smul_measure] variables [opens_measurable_space Ω] lemma test_against_nn_add (μ : finite_measure Ω) (f₁ f₂ : Ω →ᵇ ℝ≥0) : μ.test_against_nn (f₁ + f₂) = μ.test_against_nn f₁ + μ.test_against_nn f₂ := begin simp only [←ennreal.coe_eq_coe, bounded_continuous_function.coe_add, ennreal.coe_add, pi.add_apply, test_against_nn_coe_eq], exact lintegral_add_left (bounded_continuous_function.nnreal.to_ennreal_comp_measurable _) _ end lemma test_against_nn_smul [is_scalar_tower R ℝ≥0 ℝ≥0] [pseudo_metric_space R] [has_zero R] [has_bounded_smul R ℝ≥0] (μ : finite_measure Ω) (c : R) (f : Ω →ᵇ ℝ≥0) : μ.test_against_nn (c • f) = c • μ.test_against_nn f := begin simp only [←ennreal.coe_eq_coe, bounded_continuous_function.coe_smul, test_against_nn_coe_eq, ennreal.coe_smul], simp_rw [←smul_one_smul ℝ≥0∞ c (f _ : ℝ≥0∞), ←smul_one_smul ℝ≥0∞ c (lintegral _ _ : ℝ≥0∞), smul_eq_mul], exact @lintegral_const_mul _ _ (μ : measure Ω) (c • 1) _ (bounded_continuous_function.nnreal.to_ennreal_comp_measurable f), end lemma test_against_nn_lipschitz_estimate (μ : finite_measure Ω) (f g : Ω →ᵇ ℝ≥0) : μ.test_against_nn f ≤ μ.test_against_nn g + (nndist f g) * μ.mass := begin simp only [←μ.test_against_nn_const (nndist f g), ←test_against_nn_add, ←ennreal.coe_le_coe, bounded_continuous_function.coe_add, const_apply, ennreal.coe_add, pi.add_apply, coe_nnreal_ennreal_nndist, test_against_nn_coe_eq], apply lintegral_mono, have le_dist : ∀ ω, dist (f ω) (g ω) ≤ nndist f g := bounded_continuous_function.dist_coe_le_dist, intros ω, have le' : f ω ≤ g ω + nndist f g, { apply (nnreal.le_add_nndist (f ω) (g ω)).trans, rw add_le_add_iff_left, exact dist_le_coe.mp (le_dist ω), }, have le : (f ω : ℝ≥0∞) ≤ (g ω : ℝ≥0∞) + (nndist f g), by { rw ←ennreal.coe_add, exact ennreal.coe_mono le', }, rwa [coe_nnreal_ennreal_nndist] at le, end lemma test_against_nn_lipschitz (μ : finite_measure Ω) : lipschitz_with μ.mass (λ (f : Ω →ᵇ ℝ≥0), μ.test_against_nn f) := begin rw lipschitz_with_iff_dist_le_mul, intros f₁ f₂, suffices : abs (μ.test_against_nn f₁ - μ.test_against_nn f₂ : ℝ) ≤ μ.mass * (dist f₁ f₂), { rwa nnreal.dist_eq, }, apply abs_le.mpr, split, { have key' := μ.test_against_nn_lipschitz_estimate f₂ f₁, rw mul_comm at key', suffices : ↑(μ.test_against_nn f₂) ≤ ↑(μ.test_against_nn f₁) + ↑(μ.mass) * dist f₁ f₂, { linarith, }, have key := nnreal.coe_mono key', rwa [nnreal.coe_add, nnreal.coe_mul, nndist_comm] at key, }, { have key' := μ.test_against_nn_lipschitz_estimate f₁ f₂, rw mul_comm at key', suffices : ↑(μ.test_against_nn f₁) ≤ ↑(μ.test_against_nn f₂) + ↑(μ.mass) * dist f₁ f₂, { linarith, }, have key := nnreal.coe_mono key', rwa [nnreal.coe_add, nnreal.coe_mul] at key, }, end /-- Finite measures yield elements of the `weak_dual` of bounded continuous nonnegative functions via `measure_theory.finite_measure.test_against_nn`, i.e., integration. -/ def to_weak_dual_bcnn (μ : finite_measure Ω) : weak_dual ℝ≥0 (Ω →ᵇ ℝ≥0) := { to_fun := λ f, μ.test_against_nn f, map_add' := test_against_nn_add μ, map_smul' := test_against_nn_smul μ, cont := μ.test_against_nn_lipschitz.continuous, } @[simp] lemma coe_to_weak_dual_bcnn (μ : finite_measure Ω) : ⇑μ.to_weak_dual_bcnn = μ.test_against_nn := rfl @[simp] lemma to_weak_dual_bcnn_apply (μ : finite_measure Ω) (f : Ω →ᵇ ℝ≥0) : μ.to_weak_dual_bcnn f = (∫⁻ x, f x ∂(μ : measure Ω)).to_nnreal := rfl /-- The topology of weak convergence on `measure_theory.finite_measure Ω` is inherited (induced) from the weak-* topology on `weak_dual ℝ≥0 (Ω →ᵇ ℝ≥0)` via the function `measure_theory.finite_measure.to_weak_dual_bcnn`. -/ instance : topological_space (finite_measure Ω) := topological_space.induced to_weak_dual_bcnn infer_instance lemma to_weak_dual_bcnn_continuous : continuous (@to_weak_dual_bcnn Ω _ _ _) := continuous_induced_dom /- Integration of (nonnegative bounded continuous) test functions against finite Borel measures depends continuously on the measure. -/ lemma continuous_test_against_nn_eval (f : Ω →ᵇ ℝ≥0) : continuous (λ (μ : finite_measure Ω), μ.test_against_nn f) := (by apply (weak_bilin.eval_continuous _ _).comp to_weak_dual_bcnn_continuous : continuous ((λ φ : weak_dual ℝ≥0 (Ω →ᵇ ℝ≥0), φ f) ∘ to_weak_dual_bcnn)) /-- The total mass of a finite measure depends continuously on the measure. -/ lemma continuous_mass : continuous (λ (μ : finite_measure Ω), μ.mass) := by { simp_rw ←test_against_nn_one, exact continuous_test_against_nn_eval 1, } /-- Convergence of finite measures implies the convergence of their total masses. -/ lemma _root_.filter.tendsto.mass {γ : Type*} {F : filter γ} {μs : γ → finite_measure Ω} {μ : finite_measure Ω} (h : tendsto μs F (𝓝 μ)) : tendsto (λ i, (μs i).mass) F (𝓝 μ.mass) := (continuous_mass.tendsto μ).comp h lemma tendsto_iff_weak_star_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure Ω} {μ : finite_measure Ω} : tendsto μs F (𝓝 μ) ↔ tendsto (λ i, (μs(i)).to_weak_dual_bcnn) F (𝓝 μ.to_weak_dual_bcnn) := inducing.tendsto_nhds_iff ⟨rfl⟩ theorem tendsto_iff_forall_to_weak_dual_bcnn_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure Ω} {μ : finite_measure Ω} : tendsto μs F (𝓝 μ) ↔ ∀ (f : Ω →ᵇ ℝ≥0), tendsto (λ i, (μs i).to_weak_dual_bcnn f) F (𝓝 (μ.to_weak_dual_bcnn f)) := by { rw [tendsto_iff_weak_star_tendsto, tendsto_iff_forall_eval_tendsto_top_dual_pairing], refl, } theorem tendsto_iff_forall_test_against_nn_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure Ω} {μ : finite_measure Ω} : tendsto μs F (𝓝 μ) ↔ ∀ (f : Ω →ᵇ ℝ≥0), tendsto (λ i, (μs i).test_against_nn f) F (𝓝 (μ.test_against_nn f)) := by { rw finite_measure.tendsto_iff_forall_to_weak_dual_bcnn_tendsto, refl, } /-- If the total masses of finite measures tend to zero, then the measures tend to zero. This formulation concerns the associated functionals on bounded continuous nonnegative test functions. See `finite_measure.tendsto_zero_of_tendsto_zero_mass` for a formulation stating the weak convergence of measures. -/ lemma tendsto_zero_test_against_nn_of_tendsto_zero_mass {γ : Type*} {F : filter γ} {μs : γ → finite_measure Ω} (mass_lim : tendsto (λ i, (μs i).mass) F (𝓝 0)) (f : Ω →ᵇ ℝ≥0) : tendsto (λ i, (μs i).test_against_nn f) F (𝓝 0) := begin apply tendsto_iff_dist_tendsto_zero.mpr, have obs := λ i, (μs i).test_against_nn_lipschitz_estimate f 0, simp_rw [test_against_nn_zero, zero_add] at obs, simp_rw (show ∀ i, dist ((μs i).test_against_nn f) 0 = (μs i).test_against_nn f, by simp only [dist_nndist, nnreal.nndist_zero_eq_val', eq_self_iff_true, implies_true_iff]), refine squeeze_zero (λ i, nnreal.coe_nonneg _) obs _, simp_rw nnreal.coe_mul, have lim_pair : tendsto (λ i, (⟨nndist f 0, (μs i).mass⟩ : ℝ × ℝ)) F (𝓝 (⟨nndist f 0, 0⟩)), { refine (prod.tendsto_iff _ _).mpr ⟨tendsto_const_nhds, _⟩, exact (nnreal.continuous_coe.tendsto 0).comp mass_lim, }, have key := tendsto_mul.comp lim_pair, rwa mul_zero at key, end /-- If the total masses of finite measures tend to zero, then the measures tend to zero. -/ lemma tendsto_zero_of_tendsto_zero_mass {γ : Type*} {F : filter γ} {μs : γ → finite_measure Ω} (mass_lim : tendsto (λ i, (μs i).mass) F (𝓝 0)) : tendsto μs F (𝓝 0) := begin rw tendsto_iff_forall_test_against_nn_tendsto, intro f, convert tendsto_zero_test_against_nn_of_tendsto_zero_mass mass_lim f, rw [zero.test_against_nn_apply], end /-- A characterization of weak convergence in terms of integrals of bounded continuous nonnegative functions. -/ theorem tendsto_iff_forall_lintegral_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure Ω} {μ : finite_measure Ω} : tendsto μs F (𝓝 μ) ↔ ∀ (f : Ω →ᵇ ℝ≥0), tendsto (λ i, (∫⁻ x, (f x) ∂(μs(i) : measure Ω))) F (𝓝 ((∫⁻ x, (f x) ∂(μ : measure Ω)))) := begin rw tendsto_iff_forall_to_weak_dual_bcnn_tendsto, simp_rw [to_weak_dual_bcnn_apply _ _, ←test_against_nn_coe_eq, ennreal.tendsto_coe, ennreal.to_nnreal_coe], end end finite_measure -- section section finite_measure_bounded_convergence /-! ### Bounded convergence results for finite measures This section is about bounded convergence theorems for finite measures. -/ variables {Ω : Type*} [measurable_space Ω] [topological_space Ω] [opens_measurable_space Ω] /-- A bounded convergence theorem for a finite measure: If bounded continuous non-negative functions are uniformly bounded by a constant and tend to a limit, then their integrals against the finite measure tend to the integral of the limit. This formulation assumes: * the functions tend to a limit along a countably generated filter; * the limit is in the almost everywhere sense; * boundedness holds almost everywhere; * integration is `measure_theory.lintegral`, i.e., the functions and their integrals are `ℝ≥0∞`-valued. -/ lemma tendsto_lintegral_nn_filter_of_le_const {ι : Type*} {L : filter ι} [L.is_countably_generated] (μ : measure Ω) [is_finite_measure μ] {fs : ι → (Ω →ᵇ ℝ≥0)} {c : ℝ≥0} (fs_le_const : ∀ᶠ i in L, ∀ᵐ (ω : Ω) ∂μ, fs i ω ≤ c) {f : Ω → ℝ≥0} (fs_lim : ∀ᵐ (ω : Ω) ∂μ, tendsto (λ i, fs i ω) L (𝓝 (f ω))) : tendsto (λ i, (∫⁻ ω, fs i ω ∂μ)) L (𝓝 (∫⁻ ω, (f ω) ∂μ)) := begin simpa only using tendsto_lintegral_filter_of_dominated_convergence (λ _, c) (eventually_of_forall ((λ i, (ennreal.continuous_coe.comp (fs i).continuous).measurable))) _ ((@lintegral_const_lt_top _ _ μ _ _ (@ennreal.coe_ne_top c)).ne) _, { simpa only [ennreal.coe_le_coe] using fs_le_const, }, { simpa only [ennreal.tendsto_coe] using fs_lim, }, end /-- A bounded convergence theorem for a finite measure: If a sequence of bounded continuous non-negative functions are uniformly bounded by a constant and tend pointwise to a limit, then their integrals (`measure_theory.lintegral`) against the finite measure tend to the integral of the limit. A related result with more general assumptions is `measure_theory.finite_measure.tendsto_lintegral_nn_filter_of_le_const`. -/ lemma tendsto_lintegral_nn_of_le_const (μ : finite_measure Ω) {fs : ℕ → (Ω →ᵇ ℝ≥0)} {c : ℝ≥0} (fs_le_const : ∀ n ω, fs n ω ≤ c) {f : Ω → ℝ≥0} (fs_lim : ∀ ω, tendsto (λ n, fs n ω) at_top (𝓝 (f ω))) : tendsto (λ n, (∫⁻ ω, fs n ω ∂(μ : measure Ω))) at_top (𝓝 (∫⁻ ω, (f ω) ∂(μ : measure Ω))) := tendsto_lintegral_nn_filter_of_le_const μ (eventually_of_forall (λ n, eventually_of_forall (fs_le_const n))) (eventually_of_forall fs_lim) /-- A bounded convergence theorem for a finite measure: If bounded continuous non-negative functions are uniformly bounded by a constant and tend to a limit, then their integrals against the finite measure tend to the integral of the limit. This formulation assumes: * the functions tend to a limit along a countably generated filter; * the limit is in the almost everywhere sense; * boundedness holds almost everywhere; * integration is the pairing against non-negative continuous test functions (`measure_theory.finite_measure.test_against_nn`). A related result using `measure_theory.lintegral` for integration is `measure_theory.finite_measure.tendsto_lintegral_nn_filter_of_le_const`. -/ lemma tendsto_test_against_nn_filter_of_le_const {ι : Type*} {L : filter ι} [L.is_countably_generated] {μ : finite_measure Ω} {fs : ι → (Ω →ᵇ ℝ≥0)} {c : ℝ≥0} (fs_le_const : ∀ᶠ i in L, ∀ᵐ (ω : Ω) ∂(μ : measure Ω), fs i ω ≤ c) {f : Ω →ᵇ ℝ≥0} (fs_lim : ∀ᵐ (ω : Ω) ∂(μ : measure Ω), tendsto (λ i, fs i ω) L (𝓝 (f ω))) : tendsto (λ i, μ.test_against_nn (fs i)) L (𝓝 (μ.test_against_nn f)) := begin apply (ennreal.tendsto_to_nnreal (lintegral_lt_top_of_bounded_continuous_to_nnreal (μ : measure Ω) f).ne).comp, exact tendsto_lintegral_nn_filter_of_le_const μ fs_le_const fs_lim, end /-- A bounded convergence theorem for a finite measure: If a sequence of bounded continuous non-negative functions are uniformly bounded by a constant and tend pointwise to a limit, then their integrals (`measure_theory.finite_measure.test_against_nn`) against the finite measure tend to the integral of the limit. Related results: * `measure_theory.finite_measure.tendsto_test_against_nn_filter_of_le_const`: more general assumptions * `measure_theory.finite_measure.tendsto_lintegral_nn_of_le_const`: using `measure_theory.lintegral` for integration. -/ lemma tendsto_test_against_nn_of_le_const {μ : finite_measure Ω} {fs : ℕ → (Ω →ᵇ ℝ≥0)} {c : ℝ≥0} (fs_le_const : ∀ n ω, fs n ω ≤ c) {f : Ω →ᵇ ℝ≥0} (fs_lim : ∀ ω, tendsto (λ n, fs n ω) at_top (𝓝 (f ω))) : tendsto (λ n, μ.test_against_nn (fs n)) at_top (𝓝 (μ.test_against_nn f)) := tendsto_test_against_nn_filter_of_le_const (eventually_of_forall (λ n, eventually_of_forall (fs_le_const n))) (eventually_of_forall fs_lim) end finite_measure_bounded_convergence -- section section finite_measure_convergence_by_bounded_continuous_functions /-! ### Weak convergence of finite measures with bounded continuous real-valued functions In this section we characterize the weak convergence of finite measures by the usual (defining) condition that the integrals of all bounded continuous real-valued functions converge. -/ variables {Ω : Type*} [measurable_space Ω] [topological_space Ω] [opens_measurable_space Ω] lemma integrable_of_bounded_continuous_to_nnreal (μ : measure Ω) [is_finite_measure μ] (f : Ω →ᵇ ℝ≥0) : integrable ((coe : ℝ≥0 → ℝ) ∘ ⇑f) μ := begin refine ⟨(nnreal.continuous_coe.comp f.continuous).measurable.ae_strongly_measurable, _⟩, simp only [has_finite_integral, nnreal.nnnorm_eq], exact lintegral_lt_top_of_bounded_continuous_to_nnreal _ f, end lemma integrable_of_bounded_continuous_to_real (μ : measure Ω) [is_finite_measure μ] (f : Ω →ᵇ ℝ) : integrable ⇑f μ := begin refine ⟨f.continuous.measurable.ae_strongly_measurable, _⟩, have aux : (coe : ℝ≥0 → ℝ) ∘ ⇑f.nnnorm = (λ x, ‖f x‖), { ext ω, simp only [function.comp_app, bounded_continuous_function.nnnorm_coe_fun_eq, coe_nnnorm], }, apply (has_finite_integral_iff_norm ⇑f).mpr, rw ← of_real_integral_eq_lintegral_of_real, { exact ennreal.of_real_lt_top, }, { exact aux ▸ integrable_of_bounded_continuous_to_nnreal μ f.nnnorm, }, { exact eventually_of_forall (λ ω, norm_nonneg (f ω)), }, end lemma _root_.bounded_continuous_function.integral_eq_integral_nnreal_part_sub (μ : measure Ω) [is_finite_measure μ] (f : Ω →ᵇ ℝ) : ∫ ω, f ω ∂μ = ∫ ω, f.nnreal_part ω ∂μ - ∫ ω, (-f).nnreal_part ω ∂μ := by simp only [f.self_eq_nnreal_part_sub_nnreal_part_neg, pi.sub_apply, integral_sub, integrable_of_bounded_continuous_to_nnreal] lemma lintegral_lt_top_of_bounded_continuous_to_real {Ω : Type*} [measurable_space Ω] [topological_space Ω] (μ : measure Ω) [is_finite_measure μ] (f : Ω →ᵇ ℝ) : ∫⁻ ω, ennreal.of_real (f ω) ∂μ < ∞ := lintegral_lt_top_of_bounded_continuous_to_nnreal _ f.nnreal_part theorem tendsto_of_forall_integral_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure Ω} {μ : finite_measure Ω} (h : (∀ (f : Ω →ᵇ ℝ), tendsto (λ i, (∫ x, (f x) ∂(μs i : measure Ω))) F (𝓝 ((∫ x, (f x) ∂(μ : measure Ω)))))) : tendsto μs F (𝓝 μ) := begin apply (@tendsto_iff_forall_lintegral_tendsto Ω _ _ _ γ F μs μ).mpr, intro f, have key := @ennreal.tendsto_to_real_iff _ F _ (λ i, (lintegral_lt_top_of_bounded_continuous_to_nnreal (μs i : measure Ω) f).ne) _ (lintegral_lt_top_of_bounded_continuous_to_nnreal (μ : measure Ω) f).ne, simp only [ennreal.of_real_coe_nnreal] at key, apply key.mp, have lip : lipschitz_with 1 (coe : ℝ≥0 → ℝ), from isometry_subtype_coe.lipschitz, set f₀ := bounded_continuous_function.comp _ lip f with def_f₀, have f₀_eq : ⇑f₀ = (coe : ℝ≥0 → ℝ) ∘ ⇑f, by refl, have f₀_nn : 0 ≤ ⇑f₀, from λ _, by simp only [f₀_eq, pi.zero_apply, nnreal.zero_le_coe], have f₀_ae_nn : 0 ≤ᵐ[(μ : measure Ω)] ⇑f₀, from eventually_of_forall f₀_nn, have f₀_ae_nns : ∀ i, 0 ≤ᵐ[(μs i : measure Ω)] ⇑f₀, from λ i, eventually_of_forall f₀_nn, have aux := integral_eq_lintegral_of_nonneg_ae f₀_ae_nn f₀.continuous.measurable.ae_strongly_measurable, have auxs := λ i, integral_eq_lintegral_of_nonneg_ae (f₀_ae_nns i) f₀.continuous.measurable.ae_strongly_measurable, simp only [f₀_eq, ennreal.of_real_coe_nnreal] at aux auxs, simpa only [←aux, ←auxs] using h f₀, end lemma _root_.bounded_continuous_function.nnreal.to_real_lintegral_eq_integral (f : Ω →ᵇ ℝ≥0) (μ : measure Ω) : (∫⁻ x, (f x : ℝ≥0∞) ∂μ).to_real = (∫ x, f x ∂μ) := begin rw integral_eq_lintegral_of_nonneg_ae _ (nnreal.continuous_coe.comp f.continuous).measurable.ae_strongly_measurable, { simp only [ennreal.of_real_coe_nnreal], }, { apply eventually_of_forall, simp only [pi.zero_apply, nnreal.zero_le_coe, implies_true_iff], }, end /-- A characterization of weak convergence in terms of integrals of bounded continuous real-valued functions. -/ theorem tendsto_iff_forall_integral_tendsto {γ : Type*} {F : filter γ} {μs : γ → finite_measure Ω} {μ : finite_measure Ω} : tendsto μs F (𝓝 μ) ↔ ∀ (f : Ω →ᵇ ℝ), tendsto (λ i, (∫ x, (f x) ∂(μs i : measure Ω))) F (𝓝 ((∫ x, (f x) ∂(μ : measure Ω)))) := begin refine ⟨_, tendsto_of_forall_integral_tendsto⟩, rw tendsto_iff_forall_lintegral_tendsto, intros h f, simp_rw bounded_continuous_function.integral_eq_integral_nnreal_part_sub, set f_pos := f.nnreal_part with def_f_pos, set f_neg := (-f).nnreal_part with def_f_neg, have tends_pos := (ennreal.tendsto_to_real ((lintegral_lt_top_of_bounded_continuous_to_nnreal (μ : measure Ω) f_pos).ne)).comp (h f_pos), have tends_neg := (ennreal.tendsto_to_real ((lintegral_lt_top_of_bounded_continuous_to_nnreal (μ : measure Ω) f_neg).ne)).comp (h f_neg), have aux : ∀ (g : Ω →ᵇ ℝ≥0), ennreal.to_real ∘ (λ (i : γ), ∫⁻ (x : Ω), ↑(g x) ∂(μs i : measure Ω)) = λ (i : γ), (∫⁻ (x : Ω), ↑(g x) ∂(μs i : measure Ω)).to_real, from λ _, rfl, simp_rw [aux, bounded_continuous_function.nnreal.to_real_lintegral_eq_integral] at tends_pos tends_neg, exact tendsto.sub tends_pos tends_neg, end end finite_measure_convergence_by_bounded_continuous_functions -- section end finite_measure -- namespace end measure_theory -- namespace
4ba2bb6b6d85511c8c6e5c0f2cfee21b2677aff2
43390109ab88557e6090f3245c47479c123ee500
/src/M1F/problem_bank/0501/Q0501.lean
fc0bdf671781754a693e4d6d812dd7b2bde666fa
[ "Apache-2.0" ]
permissive
Ja1941/xena-UROP-2018
41f0956519f94d56b8bf6834a8d39473f4923200
b111fb87f343cf79eca3b886f99ee15c1dd9884b
refs/heads/master
1,662,355,955,139
1,590,577,325,000
1,590,577,325,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,192
lean
import algebra.group_power tactic.norm_num algebra.big_operators def Fib : ℕ → ℕ | 0 := 0 | 1 := 1 | (n+2) := Fib n + Fib (n+1) def is_even (n : ℕ) : Prop := ∃ k, n=2*k def is_odd (n : ℕ) : Prop := ∃ k, n=2*k+1 lemma even_of_even_add_even (a b : ℕ) : is_even a → is_even b → is_even (a+b) := begin intros Ha Hb, cases Ha with k Hk, cases Hb with l Hl, existsi k+l, simp [Hk,Hl,add_mul], end lemma odd_of_odd_add_even {a b : ℕ} : is_odd a → is_even b → is_odd (a+b) := begin intros Ha Hb, cases Ha with k Hk, cases Hb with l Hl, existsi k+l, simp [Hk,Hl,add_mul], end lemma odd_of_even_add_odd {a b : ℕ} : is_even a → is_odd b → is_odd (a+b) := λ h1 h2, (add_comm b a) ▸ (odd_of_odd_add_even h2 h1) lemma even_of_odd_add_odd {a b : ℕ} : is_odd a → is_odd b → is_even (a+b) := begin intros Ha Hb, cases Ha with k Hk, cases Hb with l Hl, existsi k+l+1, -- simp [mul_add,Hk,Hl,one_add_one_eq_two] -- fails! rw [Hk,Hl,mul_add,mul_add], change 2 with 1+1, simp [mul_add,add_mul], end theorem Q1a : ∀ n : ℕ, n ≥ 1 → is_odd (Fib (3*n-2)) ∧ is_odd (Fib (3*n-1)) ∧ is_even (Fib (3*n)) := sorry theorem Q1b : is_odd (Fib (2017)) := sorry