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
a5cbcc66d422c8579d5fe00421419eb6ffaf1f89
e791a827712b9cda7e14eb9861a152725a60f884
/reals.lean
b1f8b983940ae25a197c5200b3a035f05971f64a
[]
no_license
Shamrock-Frost/FormalComplexAnalysis
29bf1d45f59b22d6553b24fe5b630464f9c2d9fa
3cac79d9b603b1edf7df1bc7e948c74eb86a2cc0
refs/heads/master
1,588,634,495,874
1,554,397,551,000
1,554,397,551,000
179,541,347
0
0
null
null
null
null
UTF-8
Lean
false
false
12,472
lean
import .macros .algebra open classical constant real : Type notation `ℝ` := real constant real_field : discrete_linear_ordered_field ℝ noncomputable instance : discrete_linear_ordered_field real := real_field noncomputable instance : decidable_linear_ordered_comm_group real := { decidable_le := λ x y, prop_decidable _, ..real_field } def upper_bound {F} [linear_order F] (S : set F) : F → Prop := λ B, ∀ x ∈ S, x ≤ B def least_upper_bound {F} [decidable_linear_ordered_comm_group F] (S : set F) : F → Prop := λ B, upper_bound S B ∧ (∀ C, upper_bound S C → B ≤ C) axiom real.complete : ∀ S : set ℝ, (∃ B, upper_bound S B) → (∃ B, least_upper_bound S B) lemma eight_pos : (8:ℝ) > 0 := by { rw (_ : (8 : ℝ) = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1), tactic.swap, dsimp [bit0], ac_refl, tactic.one_sum_gt } lemma lt_of_sq_lt : ∀ x y : ℝ, 0 < y → x * x < y * y → x < y := begin intros x y hy h, by_cases hx : x > 0, tactic.swap, { exact lt_of_le_of_lt (le_of_not_gt hx) hy }, have : y*y - x*x > 0, apply sub_pos_of_lt h, apply lt_of_sub_pos, rw difference_of_squares at this, apply pos_of_mul_pos_right this, apply le_of_lt, apply add_pos hy hx end lemma real.exists_squareroot : ∀ x : ℝ, x ≥ 0 → ∃ y, y ≥ 0 ∧ y * y = x := begin intros x hx, by_cases x = 0, existsi (0 : ℝ), subst h, exact and.intro hx (mul_zero 0), have : x > 0 := lt_of_le_of_ne hx (ne.symm h), clear h hx, rename this hx, cases real.complete { y | y*y ≤ x } ⟨x+1, _⟩ with y hy, have hy' : y > 0, { by_cases 1 ≤ x, { apply lt_of_lt_of_le zero_lt_one, apply hy.left, suffices : 1 * 1 ≤ x, exact this, rw one_mul, exact h }, { have : x ≤ 1, apply le_of_not_ge h, apply lt_of_lt_of_le hx, apply hy.left, suffices : x*x ≤ x*1, rw mul_one at this, exact this, apply mul_le_mul_of_nonneg_left this (le_of_lt hx) } }, existsi y, { have h2 : 0 < 2*y, { rw [(_ : (2 : ℝ) = 1 + 1), right_distrib, one_mul], apply add_pos hy' hy', refl }, have h3 : 0 < 4*y, { rw [(_ : (4:ℝ) = 2 + 2), right_distrib], apply add_pos h2 h2, refl }, cases lt_trichotomy (y*y) x, exfalso, let ε := min ((x-y*y)/(8*y)) (2*y), have h1 : 2 * y + ε ≤ 4*y, { rw (_ : 4*y = 2*y + 2*y), apply add_le_add (le_refl (2*y)) (min_le_right _ _), transitivity (2 + 2) * y, refl, apply right_distrib }, have h4 : 0 < ε, { apply lt_min, apply lt_div_of_mul_lt, apply mul_pos eight_pos hy', rw zero_mul, apply sub_pos_of_lt h, exact h2 }, have : ε * (2 * y + ε) < x - y*y, { apply @lt_of_le_of_lt _ _ _ (ε * (4*y)), apply mul_le_mul_of_nonneg_left h1, exact le_of_lt h4, apply @lt_of_le_of_lt _ _ _ (((x - y * y) / (8 * y)) * (4*y)), apply mul_le_mul_of_nonneg_right, apply min_le_left, exact le_of_lt h3, rw [div_eq_mul_one_div, mul_assoc, mul_comm _ (4*y), ← div_eq_mul_one_div], rw (_ : 8 * y = (4 * y) * 2), rw ← field.div_div_eq_div_mul, rw div_self, rw ← mul_one (x - y * y), rw mul_assoc, apply mul_lt_mul_of_pos_left, { rw one_mul, apply div_lt_of_mul_lt_of_pos, apply add_pos zero_lt_one zero_lt_one, rw one_mul, apply lt_add_of_pos_right _ zero_lt_one, }, apply sub_pos_of_lt h, apply ne.symm (ne_of_lt h3), apply ne.symm (ne_of_lt h3), refine ne_of_gt _, apply add_pos zero_lt_one zero_lt_one, rw [mul_comm _ (2 : ℝ), ← mul_assoc], congr, simp [bit0], rw [right_distrib, one_mul], ac_refl }, { rw left_distrib at this, have := add_lt_of_lt_sub_left this, rw (_ : (2 : ℝ) = 1 + 1) at this, rw [right_distrib, one_mul, left_distrib] at this, rw (_ : y * y + (ε * y + ε * y + ε * ε) = (y + ε)*(y + ε)) at this, apply not_lt_of_ge (hy.left (y + ε) (le_of_lt this)), apply lt_add_of_pos_right, assumption, rw FOIL, ac_refl, refl }, cases h, { constructor, exact le_of_lt hy', assumption }, exfalso, let ε := (y*y-x)/(2*y), have h4 : ε > 0, { apply lt_div_of_mul_lt, rw (_ : 2 = 1 + (1:ℝ)), rw [right_distrib, one_mul], apply add_pos hy' hy', refl, rw zero_mul, apply sub_pos_of_lt h }, have : ε * (2 * y - ε) < y*y - x, { apply @lt_of_lt_of_le _ _ _ (ε * (2*y)) _, apply mul_lt_mul_of_pos_left, apply sub_lt_self, exact h4, exact h4, dsimp [ε], rw [div_eq_mul_one_div, mul_assoc, mul_comm _ (2*y), ← div_eq_mul_one_div], rw div_self, rw mul_one, apply ne.symm (ne_of_lt h2) }, { rw mul_sub at this, have := add_lt_of_lt_sub_left this, rw add_sub at this, have := lt_add_of_sub_left_lt this, have := lt_sub_right_of_add_lt this, rw (_ : ε * ε + y*y - ε * (2 * y) = (y - ε)*(y-ε)) at this, have : upper_bound {y : ℝ | y * y ≤ x} (y - ε), { intros y' h', apply le_of_lt, apply lt_of_sq_lt, apply sub_pos_of_lt, dsimp [ε], apply div_lt_of_mul_lt_of_pos h2, rw (_ : y * (2*y) = y*y+y*y), apply lt_add_of_pos_of_lt, apply mul_pos hy' hy', rw ← sub_eq_add_neg, apply sub_lt_self _ hx, rw (_ : (2 : ℝ) = 1 + 1), rw [right_distrib, left_distrib], rw one_mul, refl, apply lt_of_le_of_lt h' this }, apply not_lt_of_ge (hy.right _ this), apply sub_lt_self _ h4, rw FOIL_neg_square, rw ← neg_add, rw add_right_comm, rw add_comm, congr, rw mul_comm, rw mul_assoc, rw (_ : (2 : ℝ) = 1 + 1), rw right_distrib, rw one_mul, refl } }, intros y hy, by_cases y ≤ 1, { rw ← zero_add y, apply add_le_add (le_of_lt hx) h }, { transitivity (y*y), have : 1 ≤ y := le_of_not_le h, apply @le_mul_of_ge_one_right _ _ y y (le_trans zero_le_one this) this, transitivity x, assumption, apply le_add_of_nonneg_right zero_le_one } end noncomputable def sqrt (x : ℝ) : ℝ := if h : 0 ≤ x then some $ real.exists_squareroot x h else 0 notation `√` := sqrt lemma sqrt_sq {x : ℝ} (h : 0 ≤ x) : √x * √x = x := by { dsimp [sqrt], rw dif_pos h, exact (and.right $ some_spec $ real.exists_squareroot x h), } lemma sqrt_nonneg (x : ℝ) : √x ≥ 0 := by { dsimp [sqrt], by_cases h : 0 ≤ x, rw dif_pos h, have := some_spec (real.exists_squareroot x h), cases this, assumption, rw dif_neg h, apply le_refl } lemma eq_of_nonneg_and_sq_eq : ∀ {x y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y), x*x = y*y → x = y := begin intros x y hx hy h, generalize h' : x*x = u, rw h' at h, have h1 : (min x y) * (min x y) = u, { min_tac x y }, have h2 : (max x y) * (max x y) = u, { min_tac x y }, have h3 : (min x y) * (min x y) ≤ (min x y) * (max x y), { apply mul_le_mul_of_nonneg_left; min_tac x y }, have h4 : (min x y) * (max x y) ≤ (max x y) * (max x y), { apply mul_le_mul_of_nonneg_right; min_tac x y }, rw h1 at h3, rw h2 at h4, have : (min x y) * (max x y) = u := le_antisymm h4 h3, rw (_ : (min x y) * (max x y) = x * y) at this, rw h at this, by_cases h : y = 0, { subst h, rw [h, mul_zero] at h', apply eq_zero_of_sqr_eq_zero h' }, rw [← eq_div_iff_mul_eq _ _ h, mul_div_cancel _ h] at this, exact this, by_cases x ≤ y; unfold max min; simp [*, if_pos, if_neg], rw mul_comm, end lemma sqrt_unique : ∀ {x y} (hx : 0 ≤ x) (hy : 0 ≤ y), y*y = x → y = √x := by { intros, apply eq_of_nonneg_and_sq_eq, assumption, apply sqrt_nonneg, transitivity x, assumption, symmetry, apply sqrt_sq hx, } lemma abs_eq_sq_sqrt : ∀ x, abs x = √ (x*x) := begin intro x, apply sqrt_unique (mul_self_nonneg x) (abs_nonneg x), dsimp [abs], min_tac x (-x) end lemma eq_of_sqrt_eq {x y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) (h : √x = √y) : x = y := by rw [← sqrt_sq hx, ← sqrt_sq hy, h] lemma le_zero_of_sqrt_eq_zero : ∀ {x}, √x = 0 → x ≤ 0 := begin intros, cases le_or_gt 0 x, { rw (_ : x = 0), transitivity √x * √x, symmetry, apply sqrt_sq h, rw a, apply zero_mul }, apply le_of_lt h end lemma eq_one_of_sqrt_eq_one : ∀ {x}, √x = 1 → x = 1 := begin intros x h, have hx : 0 ≤ x, { by_contradiction, dsimp [sqrt] at h, rw dif_neg at h, exact zero_ne_one h, assumption }, rw (_ : 1 = √1) at h, apply eq_of_sqrt_eq hx zero_le_one h, apply sqrt_unique zero_le_one zero_le_one (one_mul 1) end lemma sqrt_pos_of_pos {x : ℝ} : x > 0 → √ x > 0 := begin intro hx, apply lt_of_le_of_ne, apply sqrt_nonneg, intro h, refine not_lt_of_ge _ hx, apply le_zero_of_sqrt_eq_zero, symmetry, assumption end lemma inv_sqrt : ∀ {x}, x > 0 → (√x)⁻¹ = √(x⁻¹) := begin intros x hx, have : 0 < √ x := sqrt_pos_of_pos hx, apply sqrt_unique, repeat { rw inv_eq_one_div, apply div_nonneg_of_nonneg_of_pos, exact zero_le_one, assumption }, transitivity (√x * √x)⁻¹, rw inv_eq_one_div, rw inv_eq_one_div, rw eq_div_iff_mul_eq _ _ (ne.symm $ ne_of_lt $ mul_pos this this), rw [mul_assoc, mul_left_comm _ (√ x) _, ← mul_assoc], rw mul_comm (1 / √ x) (√ x), rw ← div_eq_mul_one_div, rw div_self (ne.symm $ ne_of_lt this), apply mul_one, rw sqrt_sq (le_of_lt hx), end lemma sqrt_monotone : ∀ {x y}, x ≤ y → √ x ≤ √ y := begin intros x y hxy, by_cases 0 < x, { by_cases x = y, rw h, apply le_of_lt, have : 0 < y, refine lt_of_lt_of_le _ hxy, assumption, apply lt_of_sq_lt (√ x) (√ y) (sqrt_pos_of_pos this) _, rw [sqrt_sq, sqrt_sq], apply lt_of_le_of_ne; assumption, all_goals { apply le_of_lt, assumption } }, { rw (_ : √ x = 0), apply sqrt_nonneg, by_cases x = 0, { rw h, symmetry, apply sqrt_unique, refl, refl, apply zero_mul }, simp [sqrt], rw dif_neg, intro h, cases lt_or_eq_of_le h, trivial, have := eq.symm h_1, trivial } end lemma eq_of_really_close : ∀ {x y : ℝ}, (∀ ε, ε > 0 → abs (x - y) < ε) → x = y := begin suffices : ∀ x y : ℝ, x < y → ∃ ε, ε > 0 ∧ abs (x - y) ≥ ε, { intros x y h, cases (lt_trichotomy x y), { exfalso, cases this x y h_1 with ε, cases h_2, exact not_lt_of_ge h_2_right (h ε h_2_left) }, cases h_1, assumption, { exfalso, cases this y x h_1 with ε, cases h_2, rw abs_sub at h_2_right, exact not_lt_of_ge h_2_right (h ε h_2_left) } }, intros x y hxy, rw abs_sub, have : y - x > 0 := sub_pos_of_lt hxy, existsi (y - x), constructor, assumption, rw abs_of_nonneg (le_of_lt this), apply le_refl end lemma real.triangle_inequality : ∀ (x y : ℝ), abs (x + y) ≤ abs x + abs y := begin intros, have h1 : x + y ≤ abs x + abs y, { apply add_le_add; apply le_abs_self }, have h2 : -x + -y ≤ abs (-x) + abs (-y), { apply add_le_add; apply le_abs_self }, rw [← neg_add, abs_neg, abs_neg] at h2, apply max_le; assumption, end lemma real.triangle_inequality' : ∀ (x y z : ℝ), abs (x - y) ≤ abs (x - z) + abs (z - y) := begin intros, rw (_ : x - y = (x - z) + (z - y)), apply real.triangle_inequality, rw [← add_sub_assoc, sub_add_cancel] end lemma real.lt_of_lt_triangle_lt (x y z ε : ℝ) : abs (x - z) < ε/2 → abs (y - z) < ε/2 → abs (x - y) < ε := by { intros h h', rw abs_sub at h', apply lt_of_le_of_lt (real.triangle_inequality' x y z), rw [← add_self_div_two ε, ← div_add_div_same], exact add_lt_add h h' } noncomputable def sgn (x : ℝ) : ℝ := if x = 0 then 0 else if x > 0 then 1 else -1 lemma abs_div_self_eq_sgn (x : ℝ) : abs x / x = sgn x := begin dsimp [sgn], by_cases x = 0, { subst h, rw div_zero, rw if_pos rfl, }, by_cases x > 0, { rw abs_of_nonneg, rw div_self, rw if_neg, rw if_pos, assumption', apply le_of_lt, assumption }, { rw abs_of_neg, rw neg_div, rw div_self, rw [if_neg, if_neg], assumption', apply lt_of_not_ge, intro h, cases (lt_or_eq_of_le h), trivial, have := eq.symm h_1, trivial, } end lemma sgn_mul_abs_eq_self (x : ℝ) : sgn x * abs x = x := begin dsimp [sgn], by_cases x = 0, { subst h, rw abs_zero, rw mul_zero }, by_cases x > 0, { rw abs_of_nonneg, rw if_neg, rw if_pos, apply one_mul, assumption', apply le_of_lt, assumption }, { rw abs_of_neg, rw [if_neg, if_neg], assumption', rw mul_neg_eq_neg_mul_symm, rw neg_mul_eq_neg_mul_symm, rw neg_neg, rw one_mul, apply lt_of_not_ge, intro h, cases (lt_or_eq_of_le h), trivial, have := eq.symm h_1, trivial, } end lemma abs_mul_abs_eq_self_mul_self (x : ℝ) : abs x * abs x = x * x := by dsimp [abs]; min_tac x (-x)
03b90e2ea91c2c6818ecb5421653cb950416a951
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/category/Mon/limits.lean
2d57d7df2e7bd94154a4646cbfb46543a238ca8c
[ "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,657
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.category.Mon.basic import algebra.group.pi import category_theory.limits.creates import category_theory.limits.types import group_theory.submonoid.operations /-! # The category of (commutative) (additive) monoids has all limits Further, these limits are preserved by the forgetful functor --- that is, the underlying types are just the limits in the category of types. -/ noncomputable theory open category_theory open category_theory.limits universe u namespace Mon variables {J : Type u} [small_category J] @[to_additive] instance monoid_obj (F : J ⥤ Mon) (j) : monoid ((F ⋙ forget Mon).obj j) := by { change monoid (F.obj j), apply_instance } /-- The flat sections of a functor into `Mon` form a submonoid of all sections. -/ @[to_additive "The flat sections of a functor into `AddMon` form an additive submonoid of all sections."] def sections_submonoid (F : J ⥤ Mon) : submonoid (Π j, F.obj j) := { carrier := (F ⋙ forget Mon).sections, one_mem' := λ j j' f, by simp, mul_mem' := λ a b ah bh j j' f, begin simp only [forget_map_eq_coe, functor.comp_map, monoid_hom.map_mul, pi.mul_apply], dsimp [functor.sections] at ah bh, rw [ah f, bh f], end } @[to_additive] instance limit_monoid (F : J ⥤ Mon) : monoid (types.limit_cone (F ⋙ forget Mon.{u})).X := (sections_submonoid F).to_monoid /-- `limit.π (F ⋙ forget Mon) j` as a `monoid_hom`. -/ @[to_additive "`limit.π (F ⋙ forget AddMon) j` as an `add_monoid_hom`."] def limit_π_monoid_hom (F : J ⥤ Mon.{u}) (j) : (types.limit_cone (F ⋙ forget Mon)).X →* (F ⋙ forget Mon).obj j := { to_fun := (types.limit_cone (F ⋙ forget Mon)).π.app j, map_one' := rfl, map_mul' := λ x y, rfl } namespace has_limits -- The next two definitions are used in the construction of `has_limits Mon`. -- After that, the limits should be constructed using the generic limits API, -- e.g. `limit F`, `limit.cone F`, and `limit.is_limit F`. /-- Construction of a limit cone in `Mon`. (Internal use only; use the limits API.) -/ @[to_additive "(Internal use only; use the limits API.)"] def limit_cone (F : J ⥤ Mon.{u}) : cone F := { X := Mon.of (types.limit_cone (F ⋙ forget _)).X, π := { app := limit_π_monoid_hom F, naturality' := λ j j' f, monoid_hom.coe_inj ((types.limit_cone (F ⋙ forget _)).π.naturality f) } } /-- Witness that the limit cone in `Mon` is a limit cone. (Internal use only; use the limits API.) -/ @[to_additive "(Internal use only; use the limits API.)"] def limit_cone_is_limit (F : J ⥤ Mon) : is_limit (limit_cone F) := begin refine is_limit.of_faithful (forget Mon) (types.limit_cone_is_limit _) (λ s, ⟨_, _, _⟩) (λ s, rfl); tidy, end end has_limits open has_limits /-- The category of monoids has all limits. -/ @[to_additive] instance has_limits : has_limits Mon := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit.mk { cone := limit_cone F, is_limit := limit_cone_is_limit F } } } /-- The forgetful functor from monoids to types preserves all limits. (That is, the underlying types could have been computed instead as limits in the category of types.) -/ @[to_additive] instance forget_preserves_limits : preserves_limits (forget Mon) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, preserves_limit_of_preserves_limit_cone (limit_cone_is_limit F) (types.limit_cone_is_limit (F ⋙ forget _)) } } end Mon namespace CommMon variables {J : Type u} [small_category J] @[to_additive] instance comm_monoid_obj (F : J ⥤ CommMon) (j) : comm_monoid ((F ⋙ forget CommMon).obj j) := by { change comm_monoid (F.obj j), apply_instance } @[to_additive] instance limit_comm_monoid (F : J ⥤ CommMon) : comm_monoid (types.limit_cone (F ⋙ forget CommMon.{u})).X := @submonoid.to_comm_monoid (Π j, F.obj j) _ (Mon.sections_submonoid (F ⋙ forget₂ CommMon Mon.{u})) /-- We show that the forgetful functor `CommMon ⥤ Mon` creates limits. All we need to do is notice that the limit point has a `comm_monoid` instance available, and then reuse the existing limit. -/ @[to_additive] instance (F : J ⥤ CommMon) : creates_limit F (forget₂ CommMon Mon.{u}) := creates_limit_of_reflects_iso (λ c' t, { lifted_cone := { X := CommMon.of (types.limit_cone (F ⋙ forget CommMon)).X, π := { app := Mon.limit_π_monoid_hom (F ⋙ forget₂ CommMon Mon), naturality' := (Mon.has_limits.limit_cone (F ⋙ forget₂ _ _)).π.naturality, } }, valid_lift := by apply is_limit.unique_up_to_iso (Mon.has_limits.limit_cone_is_limit _) t, makes_limit := is_limit.of_faithful (forget₂ CommMon Mon.{u}) (Mon.has_limits.limit_cone_is_limit _) (λ s, _) (λ s, rfl) }) /-- A choice of limit cone for a functor into `CommMon`. (Generally, you'll just want to use `limit F`.) -/ @[to_additive "A choice of limit cone for a functor into `CommMon`. (Generally, you'll just want to use `limit F`.)"] def limit_cone (F : J ⥤ CommMon) : cone F := lift_limit (limit.is_limit (F ⋙ (forget₂ CommMon Mon.{u}))) /-- The chosen cone is a limit cone. (Generally, you'll just want to use `limit.cone F`.) -/ @[to_additive "The chosen cone is a limit cone. (Generally, you'll just want to use `limit.cone F`.)"] def limit_cone_is_limit (F : J ⥤ CommMon) : is_limit (limit_cone F) := lifted_limit_is_limit _ /-- The category of commutative monoids has all limits. -/ @[to_additive] instance has_limits : has_limits CommMon := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit_of_created F (forget₂ CommMon Mon) } } /-- The forgetful functor from commutative monoids to monoids preserves all limits. (That is, the underlying monoid could have been computed instead as limits in the category of monoids.) -/ @[to_additive AddCommMon.forget₂_AddMon_preserves_limits] instance forget₂_Mon_preserves_limits : preserves_limits (forget₂ CommMon Mon) := { preserves_limits_of_shape := λ J 𝒥, { preserves_limit := λ F, by apply_instance } } /-- The forgetful functor from commutative monoids to types preserves all limits. (That is, the underlying types could have been computed instead as limits in the category of types.) -/ @[to_additive] instance forget_preserves_limits : preserves_limits (forget CommMon) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, limits.comp_preserves_limit (forget₂ CommMon Mon) (forget Mon) } } end CommMon
a6b4ea0aa16ce79a2b5ff20d68cf0d36db520a9e
e2fc96178628c7451e998a0db2b73877d0648be5
/src/classes/unrestricted/basics/lifting.lean
cb7a0ddc68f9a1d0a077e826467815a4b71c2c28
[ "BSD-2-Clause" ]
permissive
madvorak/grammars
cd324ae19b28f7b8be9c3ad010ef7bf0fabe5df2
1447343a45fcb7821070f1e20b57288d437323a6
refs/heads/main
1,692,383,644,884
1,692,032,429,000
1,692,032,429,000
453,948,141
7
0
null
null
null
null
UTF-8
Lean
false
false
8,396
lean
import classes.unrestricted.basics.toolbox import utilities.list_utils section functions_lift_sink variables {T N₀ N : Type} def lift_symbol_ (lift_N : N₀ → N) : symbol T N₀ → symbol T N | (symbol.terminal t) := symbol.terminal t | (symbol.nonterminal n) := symbol.nonterminal (lift_N n) def sink_symbol_ (sink_N : N → option N₀) : symbol T N → option (symbol T N₀) | (symbol.terminal t) := some (symbol.terminal t) | (symbol.nonterminal n) := option.map symbol.nonterminal (sink_N n) def lift_string_ (lift_N : N₀ → N) : list (symbol T N₀) → list (symbol T N) := list.map (lift_symbol_ lift_N) def sink_string_ (sink_N : N → option N₀) : list (symbol T N) → list (symbol T N₀) := list.filter_map (sink_symbol_ sink_N) def lift_rule_ (lift_N : N₀ → N) : grule T N₀ → grule T N := λ r : grule T N₀, grule.mk (lift_string_ lift_N r.input_L) (lift_N r.input_N) (lift_string_ lift_N r.input_R) (lift_string_ lift_N r.output_string) end functions_lift_sink section lifting_conditions structure lifted_grammar_ (T : Type) := (g₀ g : grammar T) (lift_nt : g₀.nt → g.nt) (sink_nt : g.nt → option g₀.nt) (lift_inj : function.injective lift_nt) (sink_inj : ∀ x y, sink_nt x = sink_nt y → x = y ∨ sink_nt x = none ) (lift_nt_sink : ∀ n₀ : g₀.nt, sink_nt (lift_nt n₀) = some n₀) (corresponding_rules : ∀ r : grule T g₀.nt, r ∈ g₀.rules → lift_rule_ lift_nt r ∈ g.rules ) (preimage_of_rules : ∀ r : grule T g.nt, (r ∈ g.rules ∧ ∃ n₀ : g₀.nt, lift_nt n₀ = r.input_N) → (∃ r₀ ∈ g₀.rules, lift_rule_ lift_nt r₀ = r) ) private lemma lifted_grammar_inverse {T : Type} (lg : lifted_grammar_ T) : ∀ x : lg.g.nt, (∃ n₀, lg.sink_nt x = some n₀) → option.map lg.lift_nt (lg.sink_nt x) = x := begin intros x hyp, cases hyp with valu ass, rw ass, rw option.map_some', apply congr_arg, symmetry, by_contradiction, have inje := lg.sink_inj x (lg.lift_nt valu), rw lg.lift_nt_sink at inje, cases inje ass with case_valu case_none, { exact h case_valu, }, rw ass at case_none, exact option.no_confusion case_none, end end lifting_conditions section translating_derivations variables {T : Type} private lemma lift_tran_ {lg : lifted_grammar_ T} {w₁ w₂ : list (symbol T lg.g₀.nt)} (hyp : grammar_transforms lg.g₀ w₁ w₂) : grammar_transforms lg.g (lift_string_ lg.lift_nt w₁) (lift_string_ lg.lift_nt w₂) := begin rcases hyp with ⟨r, rin, u, v, bef, aft⟩, use lift_rule_ lg.lift_nt r, split, { exact lg.corresponding_rules r rin, }, use lift_string_ lg.lift_nt u, use lift_string_ lg.lift_nt v, split, { have lift_bef := congr_arg (lift_string_ lg.lift_nt) bef, unfold lift_string_ at *, rw list.map_append_append at lift_bef, rw list.map_append_append at lift_bef, exact lift_bef, }, { have lift_aft := congr_arg (lift_string_ lg.lift_nt) aft, unfold lift_string_ at *, rw list.map_append_append at lift_aft, exact lift_aft, }, end lemma lift_deri_ (lg : lifted_grammar_ T) {w₁ w₂ : list (symbol T lg.g₀.nt)} (hyp : grammar_derives lg.g₀ w₁ w₂) : grammar_derives lg.g (lift_string_ lg.lift_nt w₁) (lift_string_ lg.lift_nt w₂) := begin induction hyp with u v trash orig ih, { apply grammar_deri_self, }, apply grammar_deri_of_deri_tran, { exact ih, }, exact lift_tran_ orig, end def good_letter_ {lg : lifted_grammar_ T} : symbol T lg.g.nt → Prop | (symbol.terminal t) := true | (symbol.nonterminal n) := (∃ n₀ : lg.g₀.nt, lg.sink_nt n = n₀) def good_string_ {lg : lifted_grammar_ T} (s : list (symbol T lg.g.nt)) := ∀ a ∈ s, good_letter_ a private lemma sink_tran_ {lg : lifted_grammar_ T} {w₁ w₂ : list (symbol T lg.g.nt)} (hyp : grammar_transforms lg.g w₁ w₂) (ok_input : good_string_ w₁) : grammar_transforms lg.g₀ (sink_string_ lg.sink_nt w₁) (sink_string_ lg.sink_nt w₂) ∧ good_string_ w₂ := begin rcases hyp with ⟨r, rin, u, v, bef, aft⟩, rcases lg.preimage_of_rules r (by { split, { exact rin, }, rw bef at ok_input, have good_matched_nonterminal : good_letter_ (symbol.nonterminal r.input_N), { apply ok_input (symbol.nonterminal r.input_N), apply list.mem_append_left, apply list.mem_append_left, apply list.mem_append_right, rw list.mem_singleton, }, change ∃ n₀ : lg.g₀.nt, lg.sink_nt r.input_N = some n₀ at good_matched_nonterminal, cases good_matched_nonterminal with n₀ hn₀, use n₀, have almost := congr_arg (option.map lg.lift_nt) hn₀, rw lifted_grammar_inverse lg r.input_N ⟨n₀, hn₀⟩ at almost, rw option.map_some' at almost, apply option.some_injective, exact almost.symm, }) with ⟨r₀, pre_in, preimage⟩, split, swap, { rw bef at ok_input, rw aft, unfold good_string_ at ok_input ⊢, rw ←preimage, clear_except ok_input, rw list.forall_mem_append_append at ok_input ⊢, rw list.forall_mem_append_append at ok_input, split, { exact ok_input.1.1, }, split, swap, { exact ok_input.2.2, }, intros a a_in_ros, cases a, { clear_except, unfold good_letter_, }, unfold lift_rule_ at a_in_ros, dsimp only at a_in_ros, unfold lift_string_ at a_in_ros, rw list.mem_map at a_in_ros, rcases a_in_ros with ⟨s, trash, a_from_s⟩, rw ←a_from_s, cases s, { exfalso, clear_except a_from_s, unfold lift_symbol_ at a_from_s, exact symbol.no_confusion a_from_s, }, unfold lift_symbol_, unfold good_letter_, use s, exact lg.lift_nt_sink s, }, use r₀, split, { exact pre_in, }, use sink_string_ lg.sink_nt u, use sink_string_ lg.sink_nt v, have correct_inverse : sink_symbol_ lg.sink_nt ∘ lift_symbol_ lg.lift_nt = option.some, { ext1, cases x, { refl, }, rw function.comp_app, unfold lift_symbol_, unfold sink_symbol_, rw lg.lift_nt_sink, apply option.map_some', }, split, { have sink_bef := congr_arg (sink_string_ lg.sink_nt) bef, unfold sink_string_ at *, rw list.filter_map_append_append at sink_bef, rw list.filter_map_append_append at sink_bef, convert sink_bef; rw ←preimage; unfold lift_rule_; dsimp only; clear_except correct_inverse, { unfold lift_string_, rw list.filter_map_map, rw correct_inverse, rw list.filter_map_some, }, { change [symbol.nonterminal r₀.input_N] = list.filter_map (sink_symbol_ lg.sink_nt) (list.map (lift_symbol_ lg.lift_nt) [symbol.nonterminal r₀.input_N]), rw list.filter_map_map, rw correct_inverse, rw list.filter_map_some, }, { unfold lift_string_, rw list.filter_map_map, rw correct_inverse, rw list.filter_map_some, }, }, { have sink_aft := congr_arg (sink_string_ lg.sink_nt) aft, unfold sink_string_ at *, rw list.filter_map_append_append at sink_aft, convert sink_aft, rw ←preimage, clear_except correct_inverse, unfold lift_rule_, dsimp only, unfold lift_string_, rw list.filter_map_map, rw correct_inverse, rw list.filter_map_some, }, end private lemma sink_deri_aux {lg : lifted_grammar_ T} {w₁ w₂ : list (symbol T lg.g.nt)} (hyp : grammar_derives lg.g w₁ w₂) (ok_input : good_string_ w₁) : grammar_derives lg.g₀ (sink_string_ lg.sink_nt w₁) (sink_string_ lg.sink_nt w₂) ∧ good_string_ w₂ := begin induction hyp with u v trash orig ih, { split, { apply grammar_deri_self, }, { exact ok_input, }, }, have both := sink_tran_ orig ih.2, split, swap, { exact both.2, }, apply grammar_deri_of_deri_tran, { exact ih.1, }, { exact both.1, }, end lemma sink_deri_ (lg : lifted_grammar_ T) {w₁ w₂ : list (symbol T lg.g.nt)} (hyp : grammar_derives lg.g w₁ w₂) (ok_input : good_string_ w₁) : grammar_derives lg.g₀ (sink_string_ lg.sink_nt w₁) (sink_string_ lg.sink_nt w₂) := begin exact (sink_deri_aux hyp ok_input).1, end end translating_derivations
3e3ecc5366da2c8c46f393d7ac26bd7dae6835a3
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/ring_theory/int/basic.lean
32ab9427daa707e0a24b1cbb294e513505de333d
[ "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
15,254
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jens Wagemaker, Aaron Anderson -/ import ring_theory.coprime.basic import ring_theory.unique_factorization_domain /-! # Divisibility over ℕ and ℤ This file collects results for the integers and natural numbers that use abstract algebra in their proofs or cases of ℕ and ℤ being examples of structures in abstract algebra. ## Main statements * `nat.prime_iff`: `nat.prime` coincides with the general definition of `prime` * `nat.irreducible_iff_prime`: a non-unit natural number is only divisible by `1` iff it is prime * `nat.factors_eq`: the multiset of elements of `nat.factors` is equal to the factors given by the `unique_factorization_monoid` instance * ℤ is a `normalization_monoid` * ℤ is a `gcd_monoid` ## Tags prime, irreducible, natural numbers, integers, normalization monoid, gcd monoid, greatest common divisor, prime factorization, prime factors, unique factorization, unique factors -/ theorem nat.prime_iff {p : ℕ} : p.prime ↔ prime p := begin split; intro h, { refine ⟨h.ne_zero, ⟨_, λ a b, _⟩⟩, { rw nat.is_unit_iff, apply h.ne_one }, { apply h.dvd_mul.1 } }, { refine ⟨_, λ m hm, _⟩, { cases p, { exfalso, apply h.ne_zero rfl }, cases p, { exfalso, apply h.ne_one rfl }, exact (add_le_add_right (zero_le p) 2 : _ ) }, { cases hm with n hn, cases h.2.2 m n (hn ▸ dvd_rfl) with hpm hpn, { right, apply nat.dvd_antisymm (dvd.intro _ hn.symm) hpm }, { left, cases n, { exfalso, rw [hn, mul_zero] at h, apply h.ne_zero rfl }, apply nat.eq_of_mul_eq_mul_right (nat.succ_pos _), rw [← hn, one_mul], apply nat.dvd_antisymm hpn (dvd.intro m _), rw [mul_comm, hn], }, } } end theorem nat.irreducible_iff_prime {p : ℕ} : irreducible p ↔ prime p := begin refine ⟨λ h, _, prime.irreducible⟩, rw ← nat.prime_iff, refine ⟨_, λ m hm, _⟩, { cases p, { exfalso, apply h.ne_zero rfl }, cases p, { exfalso, apply h.not_unit is_unit_one, }, exact (add_le_add_right (zero_le p) 2 : _ ) }, { cases hm with n hn, cases h.is_unit_or_is_unit hn with um un, { left, rw nat.is_unit_iff.1 um, }, { right, rw [hn, nat.is_unit_iff.1 un, mul_one], } } end namespace nat instance : wf_dvd_monoid ℕ := ⟨begin apply rel_hom.well_founded _ (with_top.well_founded_lt nat.lt_wf), refine ⟨λ x, if x = 0 then ⊤ else x, _⟩, intros a b h, cases a, { exfalso, revert h, simp [dvd_not_unit] }, cases b, {simp [succ_ne_zero, with_top.coe_lt_top]}, cases dvd_and_not_dvd_iff.2 h with h1 h2, simp only [succ_ne_zero, with_top.coe_lt_coe, if_false], apply lt_of_le_of_ne (nat.le_of_dvd (nat.succ_pos _) h1) (λ con, h2 _), rw con, end⟩ instance : unique_factorization_monoid ℕ := ⟨λ _, nat.irreducible_iff_prime⟩ end nat /-- `ℕ` is a gcd_monoid. -/ instance : gcd_monoid ℕ := { gcd := nat.gcd, lcm := nat.lcm, gcd_dvd_left := nat.gcd_dvd_left , gcd_dvd_right := nat.gcd_dvd_right, dvd_gcd := λ a b c, nat.dvd_gcd, gcd_mul_lcm := λ a b, by rw [nat.gcd_mul_lcm], lcm_zero_left := nat.lcm_zero_left, lcm_zero_right := nat.lcm_zero_right } instance : normalized_gcd_monoid ℕ := { normalize_gcd := λ a b, normalize_eq _, normalize_lcm := λ a b, normalize_eq _, .. (infer_instance : gcd_monoid ℕ), .. (infer_instance : normalization_monoid ℕ) } lemma gcd_eq_nat_gcd (m n : ℕ) : gcd m n = nat.gcd m n := rfl lemma lcm_eq_nat_lcm (m n : ℕ) : lcm m n = nat.lcm m n := rfl namespace int section normalization_monoid instance : normalization_monoid ℤ := { norm_unit := λa:ℤ, if 0 ≤ a then 1 else -1, norm_unit_zero := if_pos (le_refl _), norm_unit_mul := assume a b hna hnb, begin cases hna.lt_or_lt with ha ha; cases hnb.lt_or_lt with hb hb; simp [mul_nonneg_iff, ha.le, ha.not_le, hb.le, hb.not_le] end, norm_unit_coe_units := assume u, (units_eq_one_or u).elim (assume eq, eq.symm ▸ if_pos zero_le_one) (assume eq, eq.symm ▸ if_neg (not_le_of_gt $ show (-1:ℤ) < 0, by dec_trivial)), } lemma normalize_of_nonneg {z : ℤ} (h : 0 ≤ z) : normalize z = z := show z * ↑(ite _ _ _) = z, by rw [if_pos h, units.coe_one, mul_one] lemma normalize_of_neg {z : ℤ} (h : z < 0) : normalize z = -z := show z * ↑(ite _ _ _) = -z, by rw [if_neg (not_le_of_gt h), units.coe_neg, units.coe_one, mul_neg_one] lemma normalize_coe_nat (n : ℕ) : normalize (n : ℤ) = n := normalize_of_nonneg (coe_nat_le_coe_nat_of_le $ nat.zero_le n) theorem coe_nat_abs_eq_normalize (z : ℤ) : (z.nat_abs : ℤ) = normalize z := begin by_cases 0 ≤ z, { simp [nat_abs_of_nonneg h, normalize_of_nonneg h] }, { simp [of_nat_nat_abs_of_nonpos (le_of_not_ge h), normalize_of_neg (lt_of_not_ge h)] } end lemma nonneg_of_normalize_eq_self {z : ℤ} (hz : normalize z = z) : 0 ≤ z := calc 0 ≤ (z.nat_abs : ℤ) : coe_zero_le _ ... = normalize z : coe_nat_abs_eq_normalize _ ... = z : hz lemma nonneg_iff_normalize_eq_self (z : ℤ) : normalize z = z ↔ 0 ≤ z := ⟨nonneg_of_normalize_eq_self, normalize_of_nonneg⟩ lemma eq_of_associated_of_nonneg {a b : ℤ} (h : associated a b) (ha : 0 ≤ a) (hb : 0 ≤ b) : a = b := dvd_antisymm_of_normalize_eq (normalize_of_nonneg ha) (normalize_of_nonneg hb) h.dvd h.symm.dvd end normalization_monoid section gcd_monoid instance : gcd_monoid ℤ := { gcd := λa b, int.gcd a b, lcm := λa b, int.lcm a b, gcd_dvd_left := assume a b, int.gcd_dvd_left _ _, gcd_dvd_right := assume a b, int.gcd_dvd_right _ _, dvd_gcd := assume a b c, dvd_gcd, gcd_mul_lcm := λ a b, by { rw [← int.coe_nat_mul, gcd_mul_lcm, coe_nat_abs_eq_normalize], exact normalize_associated (a * b) }, lcm_zero_left := assume a, coe_nat_eq_zero.2 $ nat.lcm_zero_left _, lcm_zero_right := assume a, coe_nat_eq_zero.2 $ nat.lcm_zero_right _} instance : normalized_gcd_monoid ℤ := { normalize_gcd := λ a b, normalize_coe_nat _, normalize_lcm := λ a b, normalize_coe_nat _, .. int.normalization_monoid, .. (infer_instance : gcd_monoid ℤ) } lemma coe_gcd (i j : ℤ) : ↑(int.gcd i j) = gcd_monoid.gcd i j := rfl lemma coe_lcm (i j : ℤ) : ↑(int.lcm i j) = gcd_monoid.lcm i j := rfl lemma nat_abs_gcd (i j : ℤ) : nat_abs (gcd_monoid.gcd i j) = int.gcd i j := rfl lemma nat_abs_lcm (i j : ℤ) : nat_abs (gcd_monoid.lcm i j) = int.lcm i j := rfl end gcd_monoid lemma exists_unit_of_abs (a : ℤ) : ∃ (u : ℤ) (h : is_unit u), (int.nat_abs a : ℤ) = u * a := begin cases (nat_abs_eq a) with h, { use [1, is_unit_one], rw [← h, one_mul], }, { use [-1, is_unit_one.neg], rw [ ← neg_eq_iff_neg_eq.mp (eq.symm h)], simp only [neg_mul_eq_neg_mul_symm, one_mul] } end lemma gcd_eq_nat_abs {a b : ℤ} : int.gcd a b = nat.gcd a.nat_abs b.nat_abs := rfl lemma gcd_eq_one_iff_coprime {a b : ℤ} : int.gcd a b = 1 ↔ is_coprime a b := begin split, { intro hg, obtain ⟨ua, hua, ha⟩ := exists_unit_of_abs a, obtain ⟨ub, hub, hb⟩ := exists_unit_of_abs b, use [(nat.gcd_a (int.nat_abs a) (int.nat_abs b)) * ua, (nat.gcd_b (int.nat_abs a) (int.nat_abs b)) * ub], rw [mul_assoc, ← ha, mul_assoc, ← hb, mul_comm, mul_comm _ (int.nat_abs b : ℤ), ← nat.gcd_eq_gcd_ab, ←gcd_eq_nat_abs, hg, int.coe_nat_one] }, { rintro ⟨r, s, h⟩, by_contradiction hg, obtain ⟨p, ⟨hp, ha, hb⟩⟩ := nat.prime.not_coprime_iff_dvd.mp hg, apply nat.prime.not_dvd_one hp, rw [←coe_nat_dvd, int.coe_nat_one, ← h], exact dvd_add ((coe_nat_dvd_left.mpr ha).mul_left _) ((coe_nat_dvd_left.mpr hb).mul_left _) } end lemma coprime_iff_nat_coprime {a b : ℤ} : is_coprime a b ↔ nat.coprime a.nat_abs b.nat_abs := by rw [←gcd_eq_one_iff_coprime, nat.coprime_iff_gcd_eq_one, gcd_eq_nat_abs] lemma sq_of_gcd_eq_one {a b c : ℤ} (h : int.gcd a b = 1) (heq : a * b = c ^ 2) : ∃ (a0 : ℤ), a = a0 ^ 2 ∨ a = - (a0 ^ 2) := begin have h' : is_unit (gcd_monoid.gcd a b), { rw [← coe_gcd, h, int.coe_nat_one], exact is_unit_one }, obtain ⟨d, ⟨u, hu⟩⟩ := exists_associated_pow_of_mul_eq_pow h' heq, use d, rw ← hu, cases int.units_eq_one_or u with hu' hu'; { rw hu', simp } end lemma sq_of_coprime {a b c : ℤ} (h : is_coprime a b) (heq : a * b = c ^ 2) : ∃ (a0 : ℤ), a = a0 ^ 2 ∨ a = - (a0 ^ 2) := sq_of_gcd_eq_one (gcd_eq_one_iff_coprime.mpr h) heq lemma nat_abs_euclidean_domain_gcd (a b : ℤ) : int.nat_abs (euclidean_domain.gcd a b) = int.gcd a b := begin apply nat.dvd_antisymm; rw ← int.coe_nat_dvd, { rw int.nat_abs_dvd, exact int.dvd_gcd (euclidean_domain.gcd_dvd_left _ _) (euclidean_domain.gcd_dvd_right _ _) }, { rw int.dvd_nat_abs, exact euclidean_domain.dvd_gcd (int.gcd_dvd_left _ _) (int.gcd_dvd_right _ _) } end end int theorem irreducible_iff_nat_prime : ∀(a : ℕ), irreducible a ↔ nat.prime a | 0 := by simp [nat.not_prime_zero] | 1 := by simp [nat.prime, one_lt_two] | (n + 2) := have h₁ : ¬n + 2 = 1, from dec_trivial, begin simp [h₁, nat.prime, irreducible_iff, (≥), nat.le_add_left 2 n, (∣)], refine forall_congr (assume a, forall_congr $ assume b, forall_congr $ assume hab, _), by_cases a = 1; simp [h], split, { assume hb, simpa [hb] using hab.symm }, { assume ha, subst ha, have : n + 2 > 0, from dec_trivial, refine nat.eq_of_mul_eq_mul_left this _, rw [← hab, mul_one] } end lemma nat.prime_iff_prime_int {p : ℕ} : p.prime ↔ _root_.prime (p : ℤ) := ⟨λ hp, ⟨int.coe_nat_ne_zero_iff_pos.2 hp.pos, mt int.is_unit_iff_nat_abs_eq.1 hp.ne_one, λ a b h, by rw [← int.dvd_nat_abs, int.coe_nat_dvd, int.nat_abs_mul, hp.dvd_mul] at h; rwa [← int.dvd_nat_abs, int.coe_nat_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd]⟩, λ hp, nat.prime_iff.2 ⟨int.coe_nat_ne_zero.1 hp.1, mt nat.is_unit_iff.1 $ λ h, by simpa [h, not_prime_one] using hp, λ a b, by simpa only [int.coe_nat_dvd, (int.coe_nat_mul _ _).symm] using hp.2.2 a b⟩⟩ /-- Maps an associate class of integers consisting of `-n, n` to `n : ℕ` -/ def associates_int_equiv_nat : associates ℤ ≃ ℕ := begin refine ⟨λz, z.out.nat_abs, λn, associates.mk n, _, _⟩, { refine (assume a, quotient.induction_on' a $ assume a, associates.mk_eq_mk_iff_associated.2 $ associated.symm $ ⟨norm_unit a, _⟩), show normalize a = int.nat_abs (normalize a), rw [int.coe_nat_abs_eq_normalize, normalize_idem] }, { intro n, dsimp, rw [associates.out_mk ↑n, ← int.coe_nat_abs_eq_normalize, int.nat_abs_of_nat, int.nat_abs_of_nat] } end lemma int.prime.dvd_mul {m n : ℤ} {p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ m * n) : p ∣ m.nat_abs ∨ p ∣ n.nat_abs := begin apply (nat.prime.dvd_mul hp).mp, rw ← int.nat_abs_mul, exact int.coe_nat_dvd_left.mp h end lemma int.prime.dvd_mul' {m n : ℤ} {p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ m * n) : (p : ℤ) ∣ m ∨ (p : ℤ) ∣ n := begin rw [int.coe_nat_dvd_left, int.coe_nat_dvd_left], exact int.prime.dvd_mul hp h end lemma int.prime.dvd_pow {n : ℤ} {k p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ n ^ k) : p ∣ n.nat_abs := begin apply @nat.prime.dvd_of_dvd_pow _ _ k hp, rw ← int.nat_abs_pow, exact int.coe_nat_dvd_left.mp h end lemma int.prime.dvd_pow' {n : ℤ} {k p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ n ^ k) : (p : ℤ) ∣ n := begin rw int.coe_nat_dvd_left, exact int.prime.dvd_pow hp h end lemma prime_two_or_dvd_of_dvd_two_mul_pow_self_two {m : ℤ} {p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ 2 * m ^ 2) : p = 2 ∨ p ∣ int.nat_abs m := begin cases int.prime.dvd_mul hp h with hp2 hpp, { apply or.intro_left, exact le_antisymm (nat.le_of_dvd zero_lt_two hp2) (nat.prime.two_le hp) }, { apply or.intro_right, rw [sq, int.nat_abs_mul] at hpp, exact (or_self _).mp ((nat.prime.dvd_mul hp).mp hpp)} end open unique_factorization_monoid theorem nat.factors_eq {n : ℕ} : normalized_factors n = n.factors := begin cases n, { simp }, rw [← multiset.rel_eq, ← associated_eq_eq], apply factors_unique (irreducible_of_normalized_factor) _, { rw [multiset.coe_prod, nat.prod_factors (nat.succ_pos _)], apply normalized_factors_prod (nat.succ_ne_zero _) }, { apply_instance }, { intros x hx, rw [nat.irreducible_iff_prime, ← nat.prime_iff], exact nat.prime_of_mem_factors hx } end lemma nat.factors_multiset_prod_of_irreducible {s : multiset ℕ} (h : ∀ (x : ℕ), x ∈ s → irreducible x) : normalized_factors (s.prod) = s := begin rw [← multiset.rel_eq, ← associated_eq_eq], apply unique_factorization_monoid.factors_unique irreducible_of_normalized_factor h (normalized_factors_prod _), rw [ne.def, multiset.prod_eq_zero_iff], intro con, exact not_irreducible_zero (h 0 con), end namespace multiplicity lemma finite_int_iff_nat_abs_finite {a b : ℤ} : finite a b ↔ finite a.nat_abs b.nat_abs := by simp only [finite_def, ← int.nat_abs_dvd_abs_iff, int.nat_abs_pow] lemma finite_int_iff {a b : ℤ} : finite a b ↔ (a.nat_abs ≠ 1 ∧ b ≠ 0) := by rw [finite_int_iff_nat_abs_finite, finite_nat_iff, pos_iff_ne_zero, int.nat_abs_ne_zero] instance decidable_nat : decidable_rel (λ a b : ℕ, (multiplicity a b).dom) := λ a b, decidable_of_iff _ finite_nat_iff.symm instance decidable_int : decidable_rel (λ a b : ℤ, (multiplicity a b).dom) := λ a b, decidable_of_iff _ finite_int_iff.symm end multiplicity lemma induction_on_primes {P : ℕ → Prop} (h₀ : P 0) (h₁ : P 1) (h : ∀ p a : ℕ, p.prime → P a → P (p * a)) (n : ℕ) : P n := begin apply unique_factorization_monoid.induction_on_prime, exact h₀, { intros n h, rw nat.is_unit_iff.1 h, exact h₁, }, { intros a p _ hp ha, exact h p a (nat.prime_iff.2 hp) ha, }, end lemma int.associated_nat_abs (k : ℤ) : associated k k.nat_abs := associated_of_dvd_dvd (int.coe_nat_dvd_right.mpr dvd_rfl) (int.nat_abs_dvd.mpr dvd_rfl) lemma int.prime_iff_nat_abs_prime {k : ℤ} : prime k ↔ nat.prime k.nat_abs := (int.associated_nat_abs k).prime_iff.trans nat.prime_iff_prime_int.symm theorem int.associated_iff_nat_abs {a b : ℤ} : associated a b ↔ a.nat_abs = b.nat_abs := begin rw [←dvd_dvd_iff_associated, ←int.nat_abs_dvd_abs_iff, ←int.nat_abs_dvd_abs_iff, dvd_dvd_iff_associated], exact associated_iff_eq, end lemma int.associated_iff {a b : ℤ} : associated a b ↔ (a = b ∨ a = -b) := begin rw int.associated_iff_nat_abs, exact int.nat_abs_eq_nat_abs_iff, end namespace int lemma zmultiples_nat_abs (a : ℤ) : add_subgroup.zmultiples (a.nat_abs : ℤ) = add_subgroup.zmultiples a := le_antisymm (add_subgroup.zmultiples_subset (mem_zmultiples_iff.mpr (dvd_nat_abs.mpr (dvd_refl a)))) (add_subgroup.zmultiples_subset (mem_zmultiples_iff.mpr (nat_abs_dvd.mpr (dvd_refl a)))) lemma span_nat_abs (a : ℤ) : ideal.span ({a.nat_abs} : set ℤ) = ideal.span {a} := by { rw ideal.span_singleton_eq_span_singleton, exact (associated_nat_abs _).symm } end int
c40c8ac825704b105e9a6d2e19c2c9271a258594
453dcd7c0d1ef170b0843a81d7d8caedc9741dce
/tactic/ext.lean
b43dd6c3ed9b87cbda66e92f77a0f6b4beef9921
[ "Apache-2.0" ]
permissive
amswerdlow/mathlib
9af77a1f08486d8fa059448ae2d97795bd12ec0c
27f96e30b9c9bf518341705c99d641c38638dfd0
refs/heads/master
1,585,200,953,598
1,534,275,532,000
1,534,275,532,000
144,564,700
0
0
null
1,534,156,197,000
1,534,156,197,000
null
UTF-8
Lean
false
false
1,966
lean
import tactic.basic /-- Tag lemmas of the form: ``` lemma my_collection.ext (a b : my_collection) (h : ∀ x, a.lookup x = b.lookup y) : a = b := ... ``` -/ @[user_attribute] meta def extensional_attribute : user_attribute := { name := `extensionality, descr := "lemmas usable by `ext` tactic" } attribute [extensionality] _root_.funext array.ext namespace tactic open interactive interactive.types open lean.parser nat local postfix `?`:9001 := optional local postfix *:9001 := many /-- `ext1 id` selects and apply one extensionality lemma (with attribute `extensionality`), using `id`, if provided, to name a local constant introduced by the lemma. If `id` is omitted, the local constant is named automatically, as per `intro`. -/ meta def interactive.ext1 (x : parse ident_ ?) : tactic unit := do ls ← attribute.get_instances `extensionality, ls.any_of (λ l, applyc l) <|> fail "no applicable extensionality rule found", try ( interactive.intro x ) meta def ext_arg := prod.mk <$> (some <$> small_nat) <*> (tk "with" *> ident_* <|> pure []) <|> prod.mk none <$> (ident_*) /-- - `ext` applies as many extensionality lemmas as possible; - `ext ids`, with `ids` a list of identifiers, finds extentionality and applies them until it runs out of identifiers in `ids` to name the local constants. When trying to prove: ``` α β : Type, f g : α → set β ⊢ f = g ``` applying `ext x y` yields: ``` α β : Type, f g : α → set β, x : α, y : β ⊢ y ∈ f x ↔ y ∈ f x ``` by applying functional extensionality and set extensionality. A maximum depth can be provided with `ext 3 with x y z`. -/ meta def interactive.ext : parse ext_arg → tactic unit | (some n, []) := interactive.ext1 none >> iterate_at_most (pred n) (interactive.ext1 none) | (none, []) := interactive.ext1 none >> repeat (interactive.ext1 none) | (n, xs) := tactic.ext xs n end tactic
4ba5ed1cf38b3a38ee0db4b5c211a31996fab211
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/multiset/default.lean
e341db806bcd223be87e5969eb247606ab91830b
[ "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
419
lean
import data.multiset.antidiagonal import data.multiset.basic import data.multiset.erase_dup import data.multiset.finset_ops import data.multiset.fold import data.multiset.functor import data.multiset.lattice import data.multiset.locally_finite import data.multiset.nat_antidiagonal import data.multiset.nodup import data.multiset.pi import data.multiset.powerset import data.multiset.sections import data.multiset.sort
665a7f595673ae82e0a4f958c94119d1336756ae
8b9f17008684d796c8022dab552e42f0cb6fb347
/tests/lean/run/interp.lean
a5ddb61ed09652baf98ec3ec81551099d2ab9ca6
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
771
lean
import algebra.function open bool nat open function inductive univ := | ubool : univ | unat : univ | uarrow : univ → univ → univ open univ definition interp : univ → Type₁ | ubool := bool | unat := nat | (uarrow fr to) := interp fr → interp to definition foo : Π (u : univ) (el : interp u), interp u | ubool tt := ff | ubool ff := tt | unat n := succ n | (uarrow fr to) f := λ x : interp fr, f (foo fr x) definition is_even : nat → bool | zero := tt | (succ n) := bnot (is_even n) example : foo unat 10 = 11 := rfl example : foo ubool tt = ff := rfl example : foo (uarrow unat ubool) (λ x : nat, is_even x) 3 = tt := rfl example : foo (uarrow unat ubool) (λ x : nat, is_even x) 4 = ff := rfl
b213d4cb3acf47de14146aef1a372260d9914b48
e030b0259b777fedcdf73dd966f3f1556d392178
/library/init/data/set.lean
93f6d7acf9c7a0be9234b4b5721c11c69a5720ce
[ "Apache-2.0" ]
permissive
fgdorais/lean
17b46a095b70b21fa0790ce74876658dc5faca06
c3b7c54d7cca7aaa25328f0a5660b6b75fe26055
refs/heads/master
1,611,523,590,686
1,484,412,902,000
1,484,412,902,000
38,489,734
0
0
null
1,435,923,380,000
1,435,923,379,000
null
UTF-8
Lean
false
false
1,633
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.logic init.category.functor universe variables u v def set (α : Type u) := α → Prop def set_of {α : Type u} (p : α → Prop) : set α := p namespace set variables {α : Type u} {β : Type v} protected def mem (a : α) (s : set α) := s a instance : has_mem α set := ⟨set.mem⟩ protected def subset (s₁ s₂ : set α) := ∀ ⦃a⦄, a ∈ s₁ → a ∈ s₂ instance : has_subset (set α) := ⟨set.subset⟩ protected def sep (p : α → Prop) (s : set α) : set α := {a | a ∈ s ∧ p a} instance : has_sep α set := ⟨set.sep⟩ instance : has_emptyc (set α) := ⟨λ a, false⟩ protected def insert (a : α) (s : set α) : set α := {b | b = a ∨ b ∈ s} instance : has_insert α set := ⟨set.insert⟩ protected def union (s₁ s₂ : set α) : set α := {a | a ∈ s₁ ∨ a ∈ s₂} instance : has_union (set α) := ⟨set.union⟩ protected def inter (s₁ s₂ : set α) : set α := {a | a ∈ s₁ ∧ a ∈ s₂} instance : has_inter (set α) := ⟨set.inter⟩ def compl (s : set α) : set α := {a | a ∉ s} instance : has_neg (set α) := ⟨compl⟩ protected def diff (s t : set α) : set α := {a ∈ s | a ∉ t} instance : has_sdiff (set α) := ⟨set.diff⟩ def powerset (s : set α) : set (set α) := {t | t ⊆ s} prefix `𝒫`:100 := powerset def image (f : α → β) (s : set α) : set β := {b | ∃ a, a ∈ s ∧ f a = b} instance : functor set := ⟨@set.image⟩ end set
2da7a84aced575893d163ba4839bd31bfc916121
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Meta/Tactic/Induction.lean
bbb76b18bab6b7b7ed1ded09533bc31071955950
[ "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
11,461
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.RecursorInfo import Lean.Meta.SynthInstance import Lean.Meta.Tactic.Util import Lean.Meta.Tactic.Revert import Lean.Meta.Tactic.Intro import Lean.Meta.Tactic.Clear import Lean.Meta.Tactic.FVarSubst namespace Lean namespace Meta private partial def getTargetArity : Expr → Nat | Expr.mdata _ b _ => getTargetArity b | Expr.forallE _ _ b _ => getTargetArity b + 1 | e => if e.isHeadBetaTarget then getTargetArity e.headBeta else 0 private def addRecParams (mvarId : MVarId) (majorTypeArgs : Array Expr) : List (Option Nat) → Expr → MetaM Expr | [], rec => pure rec | some pos :: rest, rec => if h : pos < majorTypeArgs.size then addRecParams rest (mkApp rec (majorTypeArgs.get ⟨pos, h⟩)) else throwTacticEx `induction mvarId ("ill-formed recursor") | none :: rest, rec => do recType ← inferType rec; recType ← whnfForall recType; match recType with | Expr.forallE _ d _ _ => do param ← catch (synthInstance d) (fun _ => throwTacticEx `induction mvarId "failed to generate type class instance parameter"); addRecParams rest (mkApp rec param) | _ => throwTacticEx `induction mvarId ("ill-formed recursor") structure InductionSubgoal := (mvarId : MVarId) (fields : Array Expr := #[]) (subst : FVarSubst := {}) instance InductionSubgoal.inhabited : Inhabited InductionSubgoal := ⟨{ mvarId := arbitrary _ }⟩ private def getTypeBody (mvarId : MVarId) (type : Expr) (x : Expr) : MetaM Expr := do type ← whnfForall type; match type with | Expr.forallE _ _ b _ => pure $ b.instantiate1 x | _ => throwTacticEx `induction mvarId "ill-formed recursor" private partial def finalizeAux (mvarId : MVarId) (givenNames : Array (List Name)) (recInfo : RecursorInfo) (reverted : Array FVarId) (major : Expr) (initialArity : Nat) (indices : Array Expr) (numMinors : Nat) (baseSubst : FVarSubst) : Nat → Nat → Expr → Expr → Bool → Array InductionSubgoal → MetaM (Array InductionSubgoal) | pos, minorIdx, rec, recType, consumedMajor, subgoals => do recType ← whnfForall recType; if recType.isForall && pos < recInfo.numArgs then if pos == recInfo.firstIndexPos then do (rec, recType) ← indices.foldlM (fun (acc : Expr × Expr) (index : Expr) => do let (rec, recType) := acc; let rec := mkApp rec index; recType ← getTypeBody mvarId recType index; pure (rec, recType)) (rec, recType); let rec := mkApp rec major; recType ← getTypeBody mvarId recType major; finalizeAux (pos+1+indices.size) minorIdx rec recType true subgoals else do -- consume motive tag ← getMVarTag mvarId; when (minorIdx ≥ numMinors) $ throwTacticEx `induction mvarId "ill-formed recursor"; match recType with | Expr.forallE n d b c => let d := d.headBeta; -- Remark is givenNames is not empty, then user provided explicit alternatives for each minor premise if c.binderInfo.isInstImplicit && givenNames.isEmpty then do inst? ← synthInstance? d; match inst? with | some inst => do let rec := mkApp rec inst; recType ← getTypeBody mvarId recType inst; finalizeAux (pos+1) (minorIdx+1) rec recType consumedMajor subgoals | none => do -- Add newSubgoal if type class resolution failed mvar ← mkFreshExprSyntheticOpaqueMVar d (tag ++ n); let rec := mkApp rec mvar; recType ← getTypeBody mvarId recType mvar; finalizeAux (pos+1) (minorIdx+1) rec recType consumedMajor (subgoals.push { mvarId := mvar.mvarId! }) else do let arity := getTargetArity d; when (arity < initialArity) $ throwTacticEx `induction mvarId "ill-formed recursor"; let nparams := arity - initialArity; -- number of fields due to minor premise let nextra := reverted.size - indices.size - 1; -- extra dependencies that have been reverted let minorGivenNames := if h : minorIdx < givenNames.size then givenNames.get ⟨minorIdx, h⟩ else []; mvar ← mkFreshExprSyntheticOpaqueMVar d (tag ++ n); let rec := mkApp rec mvar; recType ← getTypeBody mvarId recType mvar; -- Try to clear major premise from new goal mvarId' ← tryClear mvar.mvarId! major.fvarId!; (fields, mvarId') ← introN mvarId' nparams minorGivenNames; (extra, mvarId') ← introNP mvarId' nextra; let subst := reverted.size.fold (fun i (subst : FVarSubst) => if i < indices.size + 1 then subst else let revertedFVarId := reverted.get! i; let newFVarId := extra.get! (i - indices.size - 1); subst.insert revertedFVarId (mkFVar newFVarId)) baseSubst; let fields := fields.map mkFVar; finalizeAux (pos+1) (minorIdx+1) rec recType consumedMajor (subgoals.push { mvarId := mvarId', fields := fields, subst := subst }) | _ => unreachable! else do unless consumedMajor $ throwTacticEx `induction mvarId "ill-formed recursor"; assignExprMVar mvarId rec; pure subgoals private def finalize (mvarId : MVarId) (givenNames : Array (List Name)) (recInfo : RecursorInfo) (reverted : Array FVarId) (major : Expr) (indices : Array Expr) (baseSubst : FVarSubst) (rec : Expr) : MetaM (Array InductionSubgoal) := do target ← getMVarType mvarId; let initialArity := getTargetArity target; recType ← inferType rec; let numMinors := recInfo.produceMotive.length; finalizeAux mvarId givenNames recInfo reverted major initialArity indices numMinors baseSubst (recInfo.paramsPos.length + 1) 0 rec recType false #[] private def throwUnexpectedMajorType {α} (mvarId : MVarId) (majorType : Expr) : MetaM α := throwTacticEx `induction mvarId ("unexpected major premise type " ++ indentExpr majorType) def induction (mvarId : MVarId) (majorFVarId : FVarId) (recName : Name) (givenNames : Array (List Name) := #[]) (useUnusedNames := false) : MetaM (Array InductionSubgoal) := withMVarContext mvarId $ do checkNotAssigned mvarId `induction; majorLocalDecl ← getLocalDecl majorFVarId; recInfo ← mkRecursorInfo recName; some majorType ← whnfUntil majorLocalDecl.type recInfo.typeName | throwUnexpectedMajorType mvarId majorLocalDecl.type; majorType.withApp $ fun _ majorTypeArgs => do recInfo.paramsPos.forM $ fun paramPos? => do { match paramPos? with | none => pure () | some paramPos => when (paramPos ≥ majorTypeArgs.size) $ throwTacticEx `induction mvarId ("major premise type is ill-formed" ++ indentExpr majorType) }; mctx ← getMCtx; indices ← recInfo.indicesPos.toArray.mapM $ fun idxPos => do { when (idxPos ≥ majorTypeArgs.size) $ throwTacticEx `induction mvarId ("major premise type is ill-formed" ++ indentExpr majorType); let idx := majorTypeArgs.get! idxPos; unless idx.isFVar $ throwTacticEx `induction mvarId ("major premise type index " ++ idx ++ " is not variable " ++ indentExpr majorType); majorTypeArgs.size.forM $ fun i => do { let arg := majorTypeArgs.get! i; when (i != idxPos && arg == idx) $ throwTacticEx `induction mvarId ("'" ++ idx ++ "' is an index in major premise, but it occurs more than once" ++ indentExpr majorType); when (i < idxPos && mctx.exprDependsOn arg idx.fvarId!) $ throwTacticEx `induction mvarId ("'" ++ idx ++ "' is an index in major premise, but it occurs in previous arguments" ++ indentExpr majorType); -- If arg is also and index and a variable occurring after `idx`, we need to make sure it doesn't depend on `idx`. -- Note that if `arg` is not a variable, we will fail anyway when we visit it. when (i > idxPos && recInfo.indicesPos.contains i && arg.isFVar) $ do { idxDecl ← getLocalDecl idx.fvarId!; when (mctx.localDeclDependsOn idxDecl arg.fvarId!) $ throwTacticEx `induction mvarId ("'" ++ idx ++ "' is an index in major premise, but it depends on index occurring at position #" ++ toString (i+1)) } }; pure idx }; target ← getMVarType mvarId; when (!recInfo.depElim && mctx.exprDependsOn target majorFVarId) $ throwTacticEx `induction mvarId ("recursor '" ++ recName ++ "' does not support dependent elimination, but conclusion depends on major premise"); -- Revert indices and major premise preserving variable order (reverted, mvarId) ← revert mvarId ((indices.map Expr.fvarId!).push majorFVarId) true; -- Re-introduce indices and major (indices', mvarId) ← introNP mvarId indices.size; (majorFVarId', mvarId) ← intro1P mvarId; -- Create FVarSubst with indices let baseSubst : FVarSubst := indices.iterate {} (fun i index subst => subst.insert index.fvarId! (mkFVar (indices'.get! i.val))); trace! `Meta.Tactic.induction ("after revert&intro" ++ Format.line ++ MessageData.ofGoal mvarId); -- Update indices and major let indices := indices'.map mkFVar; let majorFVarId := majorFVarId'; let major := mkFVar majorFVarId; withMVarContext mvarId $ do target ← getMVarType mvarId; targetLevel ← getLevel target; targetLevel ← normalizeLevel targetLevel; majorLocalDecl ← getLocalDecl majorFVarId; some majorType ← whnfUntil majorLocalDecl.type recInfo.typeName | throwUnexpectedMajorType mvarId majorLocalDecl.type; majorType.withApp fun majorTypeFn majorTypeArgs => do match majorTypeFn with | Expr.const majorTypeFnName majorTypeFnLevels _ => do let majorTypeFnLevels := majorTypeFnLevels.toArray; (recLevels, foundTargetLevel) ← recInfo.univLevelPos.foldlM (fun (result : Array Level × Bool) (univPos : RecursorUnivLevelPos) => let (recLevels, foundTargetLevel) := result; match univPos with | RecursorUnivLevelPos.motive => pure (recLevels.push targetLevel, true) | RecursorUnivLevelPos.majorType idx => do when (idx ≥ majorTypeFnLevels.size) $ throwTacticEx `induction mvarId ("ill-formed recursor"); pure (recLevels.push (majorTypeFnLevels.get! idx), foundTargetLevel)) (#[], false); when (!foundTargetLevel && !targetLevel.isZero) $ throwTacticEx `induction mvarId ("recursor '" ++ recName ++ "' can only eliminate into Prop"); let rec := mkConst recName recLevels.toList; rec ← addRecParams mvarId majorTypeArgs recInfo.paramsPos rec; -- Compute motive let motive := target; motive ← if recInfo.depElim then mkLambdaFVars #[major] motive else pure motive; motive ← mkLambdaFVars indices motive; let rec := mkApp rec motive; finalize mvarId givenNames recInfo reverted major indices baseSubst rec | _ => throwTacticEx `induction mvarId "major premise is not of the form (C ...)" @[init] private def regTraceClasses : IO Unit := do registerTraceClass `Meta.Tactic.induction end Meta end Lean
e2f9c66e6fe19555f57a22bf229f67d14fd307da
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/interactive/t6.lean
1211d45a55c7c03cdf5f98f15f1181152dd524f9
[ "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
115
lean
(* import("tactic.lua") *) theorem T1 (a b : Bool) : a → b → a /\ b. apply and_intro. exact. done.
2a709b3965175c666a38797e3d4c8116938aa63e
076f5040b63237c6dd928c6401329ed5adcb0e44
/instructor-notes/2019.10.01.Polymorphism/boxed.lean
a5c72a6c0a1d493e5975bd23af48b3283b7499db
[]
no_license
kevinsullivan/uva-cs-dm-f19
0f123689cf6cb078f263950b18382a7086bf30be
09a950752884bd7ade4be33e9e89a2c4b1927167
refs/heads/master
1,594,771,841,541
1,575,853,850,000
1,575,853,850,000
205,433,890
4
9
null
1,571,592,121,000
1,567,188,539,000
Lean
UTF-8
Lean
false
false
148
lean
namespace boxed_adt inductive boxed (T : Type) : Type | box : T → boxed def unbox {T : Type} : boxed T → T | (boxed.box t) := t end boxed_adt
d21aa4bcbf5c143d232355baf853cdf72ac0589b
cf798a5faaa43a993adcc42d1a99d5eab647e00b
/Equiv.lean
c0a9f3c53f5b3f0d040f580743f94bf7de8b57b4
[]
no_license
myuon/lean-software-foundations
dbbcd37e3552b58c6e139370b16b25c69a42799b
a1a08810f2664493c920742c2d66a3131fb3ae75
refs/heads/master
1,610,261,785,986
1,459,922,839,000
1,459,922,839,000
50,269,716
4
1
null
null
null
null
UTF-8
Lean
false
false
26,295
lean
import Imp open eq.ops nat bool namespace Equiv open Idn Idn.aexp Idn.bexp Idn.com Idn.ceval definition aequiv (a1 a2 : aexp) := ∀st, aeval st a1 = aeval st a2 definition bequiv (b1 b2 : bexp) := ∀st, beval st b1 = beval st b2 definition cequiv (c1 c2 : com) := ∀st st', (c1 / st ⇓ st') ↔ (c2 / st ⇓ st') -- Exercise: 2 stars (equiv_classes) -- omit example : aequiv (AMinus (AId X) (AId X)) (ANum 0) := begin unfold aequiv, intro, unfold aeval, apply !nat.sub_self end example : bequiv (BEq (AMinus (AId X) (AId X)) (ANum 0)) BTrue := begin unfold bequiv, intro, unfold beval, unfold aeval, rewrite nat.sub_self end theorem skip_left: ∀c, cequiv (SKIP ;; c) c := begin unfold cequiv, intros, split, intro, cases a, cases a_1, assumption, intro, cases a, apply !E_Seq, apply !E_Skip, apply !E_Skip, apply !E_Seq, apply !E_Skip, apply !E_Ass, assumption, apply !E_Seq, apply !E_Skip, apply !E_Seq, assumption, assumption, apply !E_Seq, apply !E_Skip, apply (!E_IfTrue a a_1), apply !E_Seq, apply !E_Skip, apply (!E_IfFalse a a_1), apply !E_Seq, apply !E_Skip, apply (!E_WhileEnd a), apply !E_Seq, apply !E_Skip, apply (!E_WhileLoop a a_1 a_2) end -- Exercise: 2 stars (skip_right) theorem skip_right: ∀c, cequiv (c;; SKIP) c := begin unfold cequiv, intros, split, intro, cases a, cases a_2, assumption, intro, cases a, apply (!E_Seq !E_Skip !E_Skip), apply (!E_Seq (!E_Ass a) !E_Skip), apply (!E_Seq (!E_Seq a a_1) !E_Skip), apply (!E_Seq (!E_IfTrue a a_1) !E_Skip), apply (!E_Seq (!E_IfFalse a a_1) !E_Skip), apply (!E_Seq (!E_WhileEnd a) !E_Skip), apply (!E_Seq (!E_WhileLoop a a_1 a_2) !E_Skip) end theorem IFB_true_simple: ∀c1 c2, cequiv (IFB BTrue THEN c1 ELSE c2 FI) c1 := begin unfold cequiv, intros, split, intro, cases a, assumption, cases a_1, intro, cases a, apply !E_IfTrue, unfold beval, apply !E_Skip, apply !E_IfTrue, unfold beval, apply (!E_Ass a), apply !E_IfTrue, unfold beval, apply (!E_Seq a a_1), apply !E_IfTrue, unfold beval, apply (!E_IfTrue a a_1), apply !E_IfTrue, unfold beval, apply (!E_IfFalse a a_1), apply !E_IfTrue, unfold beval, apply (!E_WhileEnd a), apply !E_IfTrue, unfold beval, apply (!E_WhileLoop a a_1), assumption end -- Exercise: 2 stars (IFB_false) theorem IFB_false : ∀b c1 c2, bequiv b BFalse → cequiv (IFB b THEN c1 ELSE c2 FI) c2 := λb c1 c2, assert h: ∀ st, (∀ st, beval st b = beval st BFalse) → beval st b = tt → false, proof take st e bt, have p1: tt = beval st BFalse, from bt ⁻¹ ⬝ e st, assert p1: tt = ff, by rewrite [bt ⁻¹, e st], show false, by contradiction qed, begin unfold bequiv, unfold cequiv, intros, split, intro, cases a_1, exfalso, apply (h st a a_2), assumption, intro, apply (E_IfFalse), rewrite (a st), assumption end -- Exercise: 3 stars (swap_if_branches) theorem swap_if_branches: ∀b e1 e2, cequiv (IFB b THEN e1 ELSE e2 FI) (IFB BNot b THEN e2 ELSE e1 FI) := assert h: ∀ (b1 b2 : bool), bnot b1 = b2 → b1 = bnot b2, by simp, begin unfold cequiv, intros, split, intro, cases a, apply E_IfFalse, unfold beval, rewrite a_1, assumption, apply E_IfTrue, unfold beval, rewrite a_1, assumption, intro, cases a, unfold beval at a_1, apply E_IfFalse, apply (!h a_1), assumption, apply E_IfTrue, apply (!h a_1), assumption end theorem WHILE_false : ∀b c, bequiv b BFalse → cequiv (WHILE b DO c END) SKIP := assert hyp: ∀b st, (∀ st, beval st b = beval st BFalse) → beval st b = tt → false, proof take b st e bt, have p1: tt = beval st BFalse, from bt ⁻¹ ⬝ e st, assert p1: tt = ff, by rewrite [bt ⁻¹, e st], show false, by contradiction qed, begin intros, split, intro, cases a_1, apply E_Skip, cases a_4, unfold bequiv at a, apply (false.elim (!hyp a a_2)), apply (false.elim (!hyp a a_2)), intro, cases a_1, apply E_WhileEnd, unfold bequiv at a, apply a end -- Exercise: 2 stars, advanced, optional (WHILE_false_informal) lemma WHILE_true_nonterm : ∀b c st st', bequiv b BTrue → ¬ (WHILE b DO c END) / st ⇓ st' := λb c st st' be r, while_induction b c _ _ _ r (begin intros, unfold bequiv at be, rewrite (be t) at a, unfold beval at a, contradiction end) (λt t' t'' be2 ce we f, f) -- Exercise: 2 stars, optional (WHILE_true_nonterm_informal) -- Exercise: 2 stars (WHILE_true) theorem WHILE_true: ∀b c, bequiv b BTrue → cequiv (WHILE b DO c END) (WHILE BTrue DO SKIP END) := begin unfold cequiv, intros, split, intro, apply (false.elim (!WHILE_true_nonterm a a_1)), intro, apply (false.elim (!loop_never_stops a_1)) end -- Exercise: 2 stars, optional (seq_assoc) theorem seq_assoc : ∀c1 c2 c3, cequiv ((c1;;c2);;c3) (c1;;(c2;;c3)) := begin unfold cequiv, intros, split, intro, cases a, cases a_1, apply (!E_Seq a (!E_Seq a_3 a_2)), intro, cases a, cases a_2, apply (!E_Seq (!E_Seq a_1 a) a_3) end theorem identity_assignment : ∀ X, cequiv (X ::= AId X) SKIP := λX st st', iff.intro (λr, assert st = st', begin cases r, apply funext, intro, cases decidable.em (X = x), rewrite (if_pos a_1), rewrite (a⁻¹), unfold aeval, rewrite a_1, rewrite (if_neg a_1) end, show (SKIP / st ⇓ st'), by rewrite this; exact !E_Skip) (λr, assert hyp: update st' X (st' X) = st', from funext (λx, !update_same rfl), begin cases r, rewrite (eq.symm hyp) at {2}, apply E_Ass, unfold aeval end) -- Exercise: 2 stars (assign_aequiv) theorem assign_aequiv : ∀X e, aequiv (AId X) e → cequiv SKIP (X ::= e) := assert hyp: ∀X st, update st X (st X) = st, from λX st, funext (λx, !update_same rfl), λX e ae st st', iff.intro (λr, assert h1: st = st', by cases r; simp, suffices X ::= e / st' ⇓ update st' X (st' X), by rewrite [h1, eq.symm (hyp X st') at {2}]; assumption, !E_Ass (by unfold aequiv at ae; rewrite (eq.symm !ae))) (λr, assert st = st', begin cases r, apply funext, intro, cases decidable.em (X = x), rewrite [a_1, if_pos rfl, ↑aequiv at ae, -a, -!ae, ↑aeval, a_1], rewrite [if_neg a_1] end, by rewrite this; apply E_Skip) lemma refl_aequiv : ∀a, aequiv a a := by intros; simp lemma sym_aequiv : ∀a1 a2, aequiv a1 a2 → aequiv a2 a1 := by intros; unfold aequiv at a; simp lemma trans_aequiv : ∀a1 a2 a3, aequiv a1 a2 → aequiv a2 a3 → aequiv a1 a3 := by intros; unfold aequiv at a; unfold aequiv at a_1; simp lemma refl_bequiv : ∀b, bequiv b b := by intros; simp lemma sym_bequiv : ∀b1 b2, bequiv b1 b2 → bequiv b2 b1 := by intros; unfold bequiv at a; simp lemma trans_bequiv : ∀b1 b2 b3, bequiv b1 b2 → bequiv b2 b3 → bequiv b1 b3 := by intros; unfold bequiv at a; unfold bequiv at a_1; simp lemma refl_cequiv : ∀c, cequiv c c := by intros; simp lemma sym_cequiv : ∀c1 c2, cequiv c1 c2 → cequiv c2 c1 := by intros; unfold cequiv at a; simp lemma iff_trans : ∀P1 P2 P3, (P1 ↔ P2) → (P2 ↔ P3) → (P1 ↔ P3) := by simp lemma trans_cequiv : ∀c1 c2 c3, cequiv c1 c2 → cequiv c2 c3 → cequiv c1 c3 := by intros; unfold cequiv at a; unfold cequiv at a_1; simp theorem CAss_congruence : ∀i a1 a1', aequiv a1 a1' → cequiv (CAss i a1) (CAss i a1') := begin intros, split, intro, cases a_1, rewrite [-a_2, ↑aequiv at a, !a], apply !E_Ass, simp, intro, cases a_1, rewrite [-a_2, ↑aequiv at a, -!a], apply !E_Ass, simp end theorem CWhile_congruence : ∀b1 b1' c1 c1', bequiv b1 b1' → cequiv c1 c1' → cequiv (WHILE b1 DO c1 END) (WHILE b1' DO c1' END) := λb1 b1' c1 c1' be ce st st', assert ∀b1 b1' c1 c1' st st', bequiv b1 b1' → cequiv c1 c1' → (WHILE b1 DO c1 END / st ⇓ st') → (WHILE b1' DO c1' END / st ⇓ st'), from λb1 b1' c1 c1' st st' be ce r, !while_induction r (begin intros, rewrite [↑bequiv at be, be at a], apply (!E_WhileEnd a) end) (begin intros, rewrite [↑bequiv at be, be at a, ↑cequiv at ce, ce at a_1], apply (!E_WhileLoop a a_1 a_3) end), iff.intro (λr, !this be ce r) (λr, !this (!sym_bequiv be) (!sym_cequiv ce) r) -- Exercise: 3 stars, optional (CSeq_congruence) theorem CSeq_congruence : ∀c1 c1' c2 c2', cequiv c1 c1' → cequiv c2 c2' → cequiv (c1 ;; c2) (c1' ;; c2') := begin intros, split, intro, cases a_2, rewrite [↑cequiv at a, a at a_3, ↑cequiv at a_1, a_1 at a_4], apply (!E_Seq a_3 a_4), intro, cases a_2, rewrite [↑cequiv at a, -a at a_3, ↑cequiv at a_1, -a_1 at a_4], apply (!E_Seq a_3 a_4) end -- Exercise: 3 stars (CIf_congruence) theorem CIf_congruence : ∀b b' c1 c1' c2 c2', bequiv b b' → cequiv c1 c1' → cequiv c2 c2' → cequiv (IFB b THEN c1 ELSE c2 FI) (IFB b' THEN c1' ELSE c2' FI) := begin intros, split, intro, cases a_3, rewrite [↑bequiv at a, a at a_4, ↑cequiv at a_1, a_1 at a_5], apply (!E_IfTrue a_4 a_5), rewrite [↑bequiv at a, a at a_4, ↑cequiv at a_2, a_2 at a_5], apply (!E_IfFalse a_4 a_5), intro, cases a_3, rewrite [↑bequiv at a, -a at a_4, ↑cequiv at a_1, -a_1 at a_5], apply (!E_IfTrue a_4 a_5), rewrite [↑bequiv at a, -a at a_4, ↑cequiv at a_2, -a_2 at a_5], apply (!E_IfFalse a_4 a_5), end example: cequiv (X ::= ANum 0;; IFB (BEq (AId X) (ANum 0)) THEN Y ::= ANum 0 ELSE Y ::= ANum 42 FI) (X ::= ANum 0;; IFB (BEq (AId X) (ANum 0)) THEN Y ::= AMinus (AId X) (AId X) -- <--- changed here ELSE Y ::= ANum 42 FI) := begin apply CSeq_congruence, apply refl_cequiv, apply CIf_congruence, apply refl_bequiv, apply CAss_congruence, unfold aequiv, intro, unfold aeval, unfold aeval, apply (eq.symm !nat.sub_self), apply refl_cequiv end definition atrans_sound (atrans : aexp → aexp) := ∀a, aequiv a (atrans a) definition btrans_sound (btrans : bexp → bexp) := ∀b, bequiv b (btrans b) definition ctrans_sound (ctrans : com → com) := ∀c, cequiv c (ctrans c) definition fold_constants_aexp : aexp → aexp | (ANum n) := ANum n | (AId i) := AId i | (APlus a1 a2) := match (fold_constants_aexp a1, fold_constants_aexp a2) with | (ANum n1, ANum n2) := ANum (n1 + n2) | (a1',a2') := APlus a1' a2' end | (AMinus a1 a2) := match (fold_constants_aexp a1, fold_constants_aexp a2) with | (ANum n1, ANum n2) := ANum (n1 - n2) | (a1',a2') := AMinus a1' a2' end | (AMult a1 a2) := match (fold_constants_aexp a1, fold_constants_aexp a2) with | (ANum n1, ANum n2) := ANum (n1 * n2) | (a1',a2') := AMult a1' a2' end example : fold_constants_aexp (AMult (APlus (ANum 1) (ANum 2)) (AId X)) = AMult (ANum 3) (AId X) := rfl example : fold_constants_aexp (AMinus (AId X) (APlus (AMult (ANum 0) (ANum 6)) (AId Y))) = AMinus (AId X) (APlus (ANum 0) (AId Y)) := rfl definition fold_constants_bexp : bexp → bexp | BTrue := BTrue | BFalse := BFalse | (BEq a1 a2) := match (fold_constants_aexp a1, fold_constants_aexp a2) with | (ANum n1, ANum n2) := if beq_nat n1 n2 = tt then BTrue else BFalse | (a1', a2') := BEq a1' a2' end | (BLe a1 a2) := match (fold_constants_aexp a1, fold_constants_aexp a2) with | (ANum n1, ANum n2) := if ble_nat n1 n2 = tt then BTrue else BFalse | (a1', a2') := BLe a1' a2' end | (BNot b1) := match fold_constants_bexp b1 with | BTrue := BFalse | BFalse := BTrue | b1' := BNot b1' end | (BAnd b1 b2) := match (fold_constants_bexp b1, fold_constants_bexp b2) with | (BTrue, BTrue) := BTrue | (BTrue, BFalse) := BFalse | (BFalse, BTrue) := BFalse | (BFalse, BFalse) := BFalse | (b1', b2') := BAnd b1' b2' end example : fold_constants_bexp (BAnd BTrue (BNot (BAnd BFalse BTrue))) = BTrue := rfl example : fold_constants_bexp (BAnd (BEq (AId X) (AId Y)) (BEq (ANum 0) (AMinus (ANum 2) (APlus (ANum 1) (ANum 1))))) = BAnd (BEq (AId X) (AId Y)) BTrue := rfl definition fold_constants_com : com → com | SKIP := SKIP | (i ::= a) := i ::= (fold_constants_aexp a) | (c1 ;; c2) := (fold_constants_com c1) ;; (fold_constants_com c2) | (IFB b THEN c1 ELSE c2 FI) := match fold_constants_bexp b with | BTrue := fold_constants_com c1 | BFalse := fold_constants_com c2 | b' := IFB b' THEN fold_constants_com c1 ELSE fold_constants_com c2 FI end | (WHILE b DO c END) := match fold_constants_bexp b with | BTrue := WHILE BTrue DO SKIP END | BFalse := SKIP | b' := WHILE b' DO (fold_constants_com c) END end example : fold_constants_com (X ::= APlus (ANum 4) (ANum 5);; Y ::= AMinus (AId X) (ANum 3);; IFB BEq (AMinus (AId X) (AId Y)) (APlus (ANum 2) (ANum 4)) THEN SKIP ELSE Y ::= ANum 0 FI;; IFB BLe (ANum 0) (AMinus (ANum 4) (APlus (ANum 2) (ANum 1))) THEN Y ::= ANum 0 ELSE SKIP FI;; WHILE BEq (AId Y) (ANum 0) DO X ::= APlus (AId X) (ANum 1) END) = (X ::= ANum 9;; Y ::= AMinus (AId X) (ANum 3);; IFB BEq (AMinus (AId X) (AId Y)) (ANum 6) THEN SKIP ELSE (Y ::= ANum 0) FI;; Y ::= ANum 0;; WHILE BEq (AId Y) (ANum 0) DO X ::= APlus (AId X) (ANum 1) END) := rfl theorem fold_constants_aexp_sound : atrans_sound fold_constants_aexp := proof take a st, show aeval st a = aeval st (fold_constants_aexp a), from aexp.induction_on a (λa, rfl) (λa, rfl) (λa1 a2 ae1 ae2, calc aeval st (APlus a1 a2) = aeval st a1 + aeval st a2 : rfl ... = aeval st (fold_constants_aexp a1) + aeval st (fold_constants_aexp a2) : ae1 ▸ ae2 ▸ rfl ... = aeval st (APlus (fold_constants_aexp a1) (fold_constants_aexp a2)) : rfl ... = aeval st (fold_constants_aexp (APlus a1 a2)) : sorry) sorry sorry qed -- Exercise: 3 stars, optional (fold_bexp_Eq_informal) theorem fold_constants_bexp_sound: btrans_sound fold_constants_bexp := sorry -- Exercise: 3 stars (fold_constants_com_sound) theorem fold_constants_com_sound : ctrans_sound fold_constants_com := sorry -- Exercise: 4 stars, advanced, optional (optimize_0plus) definition optimize_0plus : aexp → aexp | (ANum n) := ANum n | (AId i) := AId i | (APlus (ANum 0) e2) := optimize_0plus e2 | (APlus e1 e2) := APlus (optimize_0plus e1) (optimize_0plus e2) | (AMinus e1 e2) := AMinus (optimize_0plus e1) (optimize_0plus e2) | (AMult e1 e2) := AMult (optimize_0plus e1) (optimize_0plus e2) definition subst_aexp (i : ident) (u : aexp) : aexp → aexp | (ANum n) := ANum n | (AId i') := if i = i' then u else AId i' | (APlus a1 a2) := APlus (subst_aexp a1) (subst_aexp a2) | (AMinus a1 a2) := AMinus (subst_aexp a1) (subst_aexp a2) | (AMult a1 a2) := AMult (subst_aexp a1) (subst_aexp a2) example : subst_aexp X (APlus (ANum 42) (ANum 53)) (APlus (AId Y) (AId X)) = (APlus (AId Y) (APlus (ANum 42) (ANum 53))) := rfl definition subst_equiv_property := ∀i1 i2 a1 a2, cequiv (i1 ::= a1;; i2 ::= a2) (i1 ::= a1;; i2 ::= subst_aexp i1 a1 a2) theorem subst_inequiv : ¬ subst_equiv_property := proof take contra, let c1 := X ::= APlus (AId X) (ANum 1);; Y ::= AId X in let c2 := X ::= APlus (AId X) (ANum 1);; Y ::= APlus (AId X) (ANum 1) in assert c12: cequiv c1 c2, by apply contra, let st1 := update (update empty_state X 1) Y 1 in let st2 := update (update empty_state X 1) Y 2 in assert H1: c1 / empty_state ⇓ st1, from !E_Seq (!E_Ass rfl) (!E_Ass rfl), assert H2: c2 / empty_state ⇓ st2, from !E_Seq (!E_Ass rfl) (!E_Ass rfl), have Hcontra: st1 = st2, by apply (!ceval_deterministic H1); rewrite -c12 at H2; apply H2, have neq: 1 = 2, from calc 1 = st1 Y : rfl ... = st2 Y : congr_fun Hcontra Y ... = 2 : rfl, show false, from succ_ne_self (neq ⁻¹) qed -- Exercise: 4 stars, optional (better_subst_equiv) inductive var_not_used_in_aexp (X : ident) : aexp → Prop := | VNUNum: ∀n, var_not_used_in_aexp X (ANum n) | VNUId: ∀Y, X ≠ Y → var_not_used_in_aexp X (AId Y) | VNUPlus: ∀a1 a2, var_not_used_in_aexp X a1 → var_not_used_in_aexp X a2 → var_not_used_in_aexp X (APlus a1 a2) | VNUMinus: ∀a1 a2, var_not_used_in_aexp X a1 → var_not_used_in_aexp X a2 → var_not_used_in_aexp X (AMinus a1 a2) | VNUMult: ∀a1 a2, var_not_used_in_aexp X a1 → var_not_used_in_aexp X a2 → var_not_used_in_aexp X (AMult a1 a2) lemma aeval_weakening : ∀i st a ni, var_not_used_in_aexp i a → aeval (update st i ni) a = aeval st a := begin intros, induction a_1, unfold aeval, unfold aeval, unfold update, rewrite (if_neg a), unfold aeval, rewrite [v_0, v_1], unfold aeval, rewrite [v_0, v_1], unfold aeval, rewrite [v_0, v_1] end -- Exercise: 3 stars, optional (inequiv_exercise) theorem inequiv_exercise: ¬ cequiv (WHILE BTrue DO SKIP END) SKIP := begin intro, unfold cequiv at a, apply (WHILE_true_nonterm BTrue SKIP empty_state empty_state), apply refl_bequiv, rewrite (a empty_state empty_state), exact !E_Skip end end Equiv namespace Himp open - [notation] Idn open Idn.aexp Idn.bexp inductive com : Type := | CSkip : com | CAss : ident → aexp → com | CSeq : com → com → com | CIf : bexp → com → com → com | CWhile : bexp → com → com | CHavoc : ident → com open com notation `SKIP` := CSkip infix `::=`:800 := CAss infixr `;;`:500 := CSeq notation `WHILE` b `DO` c `END` := CWhile b c notation `IFB` c1 `THEN` c2 `ELSE` c3 `FI` := CIf c1 c2 c3 prefix `HAVOC`:900 := CHavoc -- Exercise: 2 stars (himp_ceval) inductive ceval : com → states → states → Prop := | E_Skip : ∀ st, ceval SKIP st st | E_Ass : ∀ st a1 n x, aeval st a1 = n → ceval (x ::= a1) st (update st x n) | E_Seq : ∀ c1 c2 st st' st'', ceval c1 st st' → ceval c2 st' st'' → ceval (c1 ;; c2) st st'' | E_IfTrue : ∀ st st' b c1 c2, beval st b = tt → ceval c1 st st' → ceval (IFB b THEN c1 ELSE c2 FI) st st' | E_IfFalse : ∀ st st' b c1 c2, beval st b = ff → ceval c2 st st' → ceval (IFB b THEN c1 ELSE c2 FI) st st' | E_WhileEnd : ∀ b st c, beval st b = ff → ceval (WHILE b DO c END) st st | E_WhileLoop : ∀ st st' st'' b c, beval st b = tt → ceval c st st' → ceval (WHILE b DO c END) st' st'' → ceval (WHILE b DO c END) st st'' | E_Havoc : ∀ st i n, ceval (HAVOC i) st (update st i n) open ceval notation c1 `/` st `⇓` st' := ceval c1 st st' example : (HAVOC X) / empty_state ⇓ update empty_state X 0 := !E_Havoc example : (SKIP;; HAVOC Z) / empty_state ⇓ update empty_state Z 42 := !E_Seq !E_Skip !E_Havoc definition cequiv (c1 c2 : com) := ∀st st' : states, (c1 / st ⇓ st') ↔ (c2 / st ⇓ st') -- Exercise: 3 stars (havoc_swap) definition pXY := HAVOC X ;; HAVOC Y definition pYX := HAVOC Y ;; HAVOC X theorem pXY_cequiv_pYX : cequiv pXY pYX := assert hyp1: ∀st n n_1, (λ y, ite (Y = y) n (ite (X = y) n_1 (st y))) = update (update st Y n) X n_1, begin intros, unfold update, apply funext, intro, cases decidable.em (Y = x), rewrite [a, if_pos rfl, if_pos rfl, -a], rewrite [if_neg a, if_neg a] end, assert hyp2: ∀st n n_1, (λ y, ite (X = y) n_1 (ite (Y = y) n (st y))) = update (update st X n_1) Y n, begin intros, unfold update, apply funext, intro, cases decidable.em (Y = x), rewrite [a, if_pos rfl, if_pos rfl, -a], rewrite [if_neg a, if_neg a] end, begin intros, split, intro, cases a, cases a_2, cases a_1, fold X, fold Y, apply !E_Seq (E_Havoc _ _ n), rewrite (hyp1 st n n_1), apply !E_Havoc, intro, cases a, cases a_1, cases a_2, fold X, fold Y, apply !E_Seq (E_Havoc _ _ n_1), rewrite (hyp2 st n n_1), apply !E_Havoc end -- Exercise: 4 stars, optional (havoc_copy) definition ptwice := HAVOC X;; HAVOC Y. definition pcopy := HAVOC X;; Y ::= AId X. theorem ptwice_cequiv_pcopy : ¬cequiv ptwice pcopy := proof take ceq, assert lem1: ∀st st', (Y ::= AId X / st ⇓ st') → st' = update st Y (st X), begin intros, cases a, unfold update, apply funext, intro, unfold aeval at a_1, rewrite -a_1 end, let st1 := update (update empty_state X 0) Y 1 in assert p: ptwice / empty_state ⇓ st1, from !E_Seq !E_Havoc !E_Havoc, assert q: pcopy / empty_state ⇓ st1, by rewrite ceq at p; assumption, assert r: ∃n, (update (update empty_state X 0) Y 1) = update (update empty_state X n) Y n, begin cases q, cases a, fold X at a_1, existsi n, apply (!lem1 a_1) end, show false, begin apply (exists.elim r), intros, cases decidable.em (a = 0), rewrite [a_2 at a_1], apply (@succ_ne_self 0 (congr_fun a_1 Y)), apply (a_2 (congr_fun a_1 X)⁻¹) end qed -- Exercise: 5 stars, advanced (p1_p2_equiv) definition p1 : com := WHILE (BNot (BEq (AId X) (ANum 0))) DO HAVOC Y;; X ::= APlus (AId X) (ANum 1) END definition p2 : com := WHILE (BNot (BEq (AId X) (ANum 0))) DO SKIP END lemma p1_may_diverge : ∀st st', st X ≠ 0 → ¬ p1 / st ⇓ st' := sorry -- X ≥ 1がループ不変条件であることを示す lemma p2_may_diverge : ∀st st', st X ≠ 0 → ¬ p2 / st ⇓ st' := sorry -- X ≠ 0がループ不変条件であることを示す theorem p1_p2_equiv : cequiv p1 p2 := sorry -- Exercise: 4 stars, advanced (p3_p4_inquiv) definition p3 : com := Z ::= ANum 1;; WHILE (BNot (BEq (AId X) (ANum 0))) DO HAVOC X;; HAVOC Z END definition p4 : com := X ::= (ANum 0);; Z ::= (ANum 1). theorem p3_p4_inequiv : ¬ cequiv p3 p4 := sorry -- p3 / {} ⇓ {X := 0, Z := 2} による -- Exercise: 5 stars, advanced, optional (p5_p6_equiv) definition p5 : com := WHILE (BNot (BEq (AId X) (ANum 1))) DO HAVOC X END definition p6 : com := X ::= ANum 1. theorem p5_p6_equiv : cequiv p5 p6 := sorry -- p5 / st ⇓ st' ↔ st' X = 1 end Himp namespace Equiv2 open Idn Idn.aexp Idn.bexp Idn.com Idn.ceval definition stequiv (st1 st2 : states) := ∀(X : ident), st1 X = st2 X -- Exercise: 1 star, optional (stequiv_refl) lemma stequiv_refl : ∀(st : states), stequiv st st := λst X, rfl -- Exercise: 1 star, optional (stequiv_sym) lemma stequiv_sym : ∀(st1 st2 : states), stequiv st1 st2 → stequiv st2 st1 := λst1 st2 se X, (se X) ⁻¹ -- Exercise: 1 star, optional (stequiv_trans) lemma stequiv_trans : ∀(st1 st2 st3 : states), stequiv st1 st2 → stequiv st2 st3 → stequiv st1 st3 := λst1 st2 st3 se1 se2 X, se1 X ⬝ se2 X -- Exercise: 1 star, optional (stequiv_update) lemma stequiv_update : ∀(st1 st2 : states), stequiv st1 st2 → ∀(X:ident) (n:nat), stequiv (update st1 X n) (update st2 X n) := λst1 st2 se1 X n Y, begin unfold update, cases decidable.em (X = Y), rewrite [a, if_pos rfl, if_pos rfl], rewrite [if_neg a, if_neg a], apply !se1 end -- Exercise: 2 stars, optional (stequiv_aeval) lemma stequiv_aeval : ∀(st1 st2 : states), stequiv st1 st2 → ∀(a:aexp), aeval st1 a = aeval st2 a := λst1 st2 se1 a, begin unfold stequiv at se1, induction a, esimp, unfold aeval, apply (se1 a), unfold aeval, rewrite [v_0, v_1], unfold aeval, rewrite [v_0, v_1], unfold aeval, rewrite [v_0, v_1] end -- Exercise: 2 stars, optional (stequiv_beval) lemma stequiv_beval : ∀(st1 st2 : states), stequiv st1 st2 → ∀(b:bexp), beval st1 b = beval st2 b := λst1 st2 se1 b, begin unfold stequiv at se1, induction b, unfold beval, unfold beval, unfold beval, rewrite [!stequiv_aeval se1, !stequiv_aeval se1], unfold beval, rewrite [!stequiv_aeval se1, !stequiv_aeval se1], unfold beval, rewrite v_0, unfold beval, rewrite [v_0, v_1] end lemma stequiv_ceval_lemma : ∀c st1 st1', (c / st1 ⇓ st1') → ∀st2, stequiv st1 st2 → ∀st2', (c / st2 ⇓ st2') → stequiv st1' st2' := begin intros c st1 st1' ce, induction ce, intros, cases a_1, apply a, intros st2 a_1 st2' a_2 X1, cases a_2, have update st x n X1 = update st2 x n_1 X1, begin rewrite [!stequiv_aeval a_1 at a, a_3 at a, a], apply (!stequiv_update a_1) end, unfold update, unfold update at this, assumption, intros, revert X, cases a_3, assert se2: stequiv st' st'_1, apply (v_0 _ a_2 _ a_4), apply (v_1 _ se2 _ a_5), intros, revert X, cases a_3, apply (v_0 _ a_2 _ a_5), rewrite [!stequiv_beval a_2 at a, a at a_4], contradiction, intros, revert X, cases a_3, rewrite [!stequiv_beval a_2 at a, a at a_4], contradiction, apply (v_0 _ a_2 _ a_5), intros, revert X, cases a_2, apply a_1, rewrite [!stequiv_beval a_1 at a, a at a_3], contradiction, intros, revert X, assert a_4': WHILE b DO c END / st2 ⇓ st2', assumption, cases a_4', rewrite [!stequiv_beval a_3 at a, a at a_5], contradiction, apply (v_1 _ (v_0 _ a_3 _ a_6) _ a_7) end lemma stequiv_ceval: ∀(st1 st2 : states), stequiv st1 st2 → ∀(c: com) (st1': states), (c / st1 ⇓ st1') → ∃st2' : states, ((c / st2 ⇓ st2') ∧ stequiv st1' st2') := sorry /- begin intros, generalize a, generalize st2, induction a_1, intros, existsi st2_1, split, apply !E_Skip, assumption, intros, existsi (update st2_1 x n), split, rewrite -a, apply !E_Ass, rewrite [!stequiv_aeval a_1], rewrite [-(!stequiv_aeval a_1), -(!stequiv_aeval a_2)], apply (!stequiv_update a_2), intros, cases (v_0 a_2 _ a_3), cases (v_0 a_2 _ a_2), end -/ /- inductive ceval' : com → states → states → Prop := | E_equiv : ∀c st st' st'', (ceval' c st st') → stequiv st' st'' → (ceval' c st st'') notation c1 `/` st `⇓'` st' := ceval' c1 st st' definition cequiv' (c1 c2 : com) : Prop := ∀(st st' : states), (c1 / st ⇓' st') ↔ (c2 / st ⇓' st'). -/ -- Exercise: 2 stars, optional (identity_assignment') -- Exercise: 4 stars, optional (for_while_equiv) -- Exercise: 3 stars, optional (swap_noninterfering_assignments) end Equiv2
bde0d4607a5c861055b52547bcf5a5614c0d8540
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/order/boolean_algebra.lean
fbc8040e4d0a2f6ae4dd3265ec4af49ab817b579
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
32,399
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, Bryan Gin-ge Chen -/ import order.bounded_lattice /-! # (Generalized) Boolean algebras A Boolean algebra is a bounded distributive lattice with a complement operator. Boolean algebras generalize the (classical) logic of propositions and the lattice of subsets of a set. Generalized Boolean algebras may be less familiar, but they are essentially Boolean algebras which do not necessarily have a top element (`⊤`) (and hence not all elements may have complements). One example in mathlib is `finset α`, the type of all finite subsets of an arbitrary (not-necessarily-finite) type `α`. `generalized_boolean_algebra α` is defined to be a distributive lattice with bottom (`⊥`) admitting a *relative* complement operator, written using "set difference" notation as `x \ y` (`sdiff x y`). For convenience, the `boolean_algebra` type class is defined to extend `generalized_boolean_algebra` so that it is also bundled with a `\` operator. (A terminological point: `x \ y` is the complement of `y` relative to the interval `[⊥, x]`. We do not yet have relative complements for arbitrary intervals, as we do not even have lattice intervals.) ## Main declarations * `has_compl`: a type class for the complement operator * `generalized_boolean_algebra`: a type class for generalized Boolean algebras * `boolean_algebra.core`: a type class with the minimal assumptions for a Boolean algebras * `boolean_algebra`: the main type class for Boolean algebras; it extends both `generalized_boolean_algebra` and `boolean_algebra.core`. An instance of `boolean_algebra` can be obtained from one of `boolean_algebra.core` using `boolean_algebra.of_core`. * `boolean_algebra_Prop`: the Boolean algebra instance on `Prop` ## Implementation notes The `sup_inf_sdiff` and `inf_inf_sdiff` axioms for the relative complement operator in `generalized_boolean_algebra` are taken from [Wikipedia](https://en.wikipedia.org/wiki/Boolean_algebra_(structure)#Generalizations). [Stone's paper introducing generalized Boolean algebras][Stone1935] does not define a relative complement operator `a \ b` for all `a`, `b`. Instead, the postulates there amount to an assumption that for all `a, b : α` where `a ≤ b`, the equations `x ⊔ a = b` and `x ⊓ a = ⊥` have a solution `x`. `disjoint.sdiff_unique` proves that this `x` is in fact `b \ a`. ## Notations * `xᶜ` is notation for `compl x` * `x \ y` is notation for `sdiff x y`. ## References * <https://en.wikipedia.org/wiki/Boolean_algebra_(structure)#Generalizations> * [*Postulates for Boolean Algebras and Generalized Boolean Algebras*, M.H. Stone][Stone1935] * [*Lattice Theory: Foundation*, George Grätzer][Gratzer2011] ## Tags generalized Boolean algebras, Boolean algebras, lattices, sdiff, compl -/ set_option old_structure_cmd true universes u v variables {α : Type u} {w x y z : α} /-! ### Generalized Boolean algebras Some of the lemmas in this section are from: * [*Lattice Theory: Foundation*, George Grätzer][Gratzer2011] * <https://ncatlab.org/nlab/show/relative+complement> * <https://people.math.gatech.edu/~mccuan/courses/4317/symmetricdifference.pdf> -/ export has_sdiff (sdiff) /-- A generalized Boolean algebra is a distributive lattice with `⊥` and a relative complement operation `\` (called `sdiff`, after "set difference") satisfying `(a ⊓ b) ⊔ (a \ b) = a` and `(a ⊓ b) ⊓ (a \ b) = b`, i.e. `a \ b` is the complement of `b` in `a`. This is a generalization of Boolean algebras which applies to `finset α` for arbitrary (not-necessarily-`fintype`) `α`. -/ class generalized_boolean_algebra (α : Type u) extends semilattice_sup_bot α, semilattice_inf_bot α, distrib_lattice α, has_sdiff α := (sup_inf_sdiff : ∀a b:α, (a ⊓ b) ⊔ (a \ b) = a) (inf_inf_sdiff : ∀a b:α, (a ⊓ b) ⊓ (a \ b) = ⊥) -- We might want a `is_compl_of` predicate (for relative complements) generalizing `is_compl`, -- however we'd need another type class for lattices with bot, and all the API for that. section generalized_boolean_algebra variables [generalized_boolean_algebra α] @[simp] theorem sup_inf_sdiff (x y : α) : (x ⊓ y) ⊔ (x \ y) = x := generalized_boolean_algebra.sup_inf_sdiff _ _ @[simp] theorem inf_inf_sdiff (x y : α) : (x ⊓ y) ⊓ (x \ y) = ⊥ := generalized_boolean_algebra.inf_inf_sdiff _ _ @[simp] theorem sup_sdiff_inf (x y : α) : (x \ y) ⊔ (x ⊓ y) = x := by rw [sup_comm, sup_inf_sdiff] @[simp] theorem inf_sdiff_inf (x y : α) : (x \ y) ⊓ (x ⊓ y) = ⊥ := by rw [inf_comm, inf_inf_sdiff] theorem disjoint_inf_sdiff : disjoint (x ⊓ y) (x \ y) := (inf_inf_sdiff x y).le -- TODO: in distributive lattices, relative complements are unique when they exist theorem sdiff_unique (s : (x ⊓ y) ⊔ z = x) (i : (x ⊓ y) ⊓ z = ⊥) : x \ y = z := begin conv_rhs at s { rw [←sup_inf_sdiff x y, sup_comm] }, rw sup_comm at s, conv_rhs at i { rw [←inf_inf_sdiff x y, inf_comm] }, rw inf_comm at i, exact (eq_of_inf_eq_sup_eq i s).symm, end theorem sdiff_symm (hy : y ≤ x) (hz : z ≤ x) (H : x \ y = z) : x \ z = y := have hyi : x ⊓ y = y := inf_eq_right.2 hy, have hzi : x ⊓ z = z := inf_eq_right.2 hz, eq_of_inf_eq_sup_eq (begin have ixy := inf_inf_sdiff x y, rw [H, hyi] at ixy, have ixz := inf_inf_sdiff x z, rwa [hzi, inf_comm, ←ixy] at ixz, end) (begin have sxz := sup_inf_sdiff x z, rw [hzi, sup_comm] at sxz, rw sxz, symmetry, have sxy := sup_inf_sdiff x y, rwa [H, hyi] at sxy, end) lemma sdiff_le : x \ y ≤ x := calc x \ y ≤ (x ⊓ y) ⊔ (x \ y) : le_sup_right ... = x : sup_inf_sdiff x y @[simp] lemma bot_sdiff : ⊥ \ x = ⊥ := le_bot_iff.1 sdiff_le lemma inf_sdiff_right : x ⊓ (x \ y) = x \ y := by rw [inf_of_le_right (@sdiff_le _ x y _)] lemma inf_sdiff_left : (x \ y) ⊓ x = x \ y := by rw [inf_comm, inf_sdiff_right] -- cf. `is_compl_top_bot` @[simp] lemma sdiff_self : x \ x = ⊥ := by rw [←inf_inf_sdiff, inf_idem, inf_of_le_right (@sdiff_le _ x x _)] @[simp] theorem sup_sdiff_self_right : x ⊔ (y \ x) = x ⊔ y := calc x ⊔ (y \ x) = (x ⊔ (x ⊓ y)) ⊔ (y \ x) : by rw sup_inf_self ... = x ⊔ ((y ⊓ x) ⊔ (y \ x)) : by ac_refl ... = x ⊔ y : by rw sup_inf_sdiff @[simp] theorem sup_sdiff_self_left : (y \ x) ⊔ x = y ⊔ x := by rw [sup_comm, sup_sdiff_self_right, sup_comm] lemma sup_sdiff_symm : x ⊔ (y \ x) = y ⊔ (x \ y) := by rw [sup_sdiff_self_right, sup_sdiff_self_right, sup_comm] lemma sup_sdiff_of_le (h : x ≤ y) : x ⊔ (y \ x) = y := by conv_rhs { rw [←sup_inf_sdiff y x, inf_eq_right.2 h] } @[simp] lemma sup_sdiff_left : x ⊔ (x \ y) = x := by { rw sup_eq_left, exact sdiff_le } lemma sup_sdiff_right : (x \ y) ⊔ x = x := by rw [sup_comm, sup_sdiff_left] @[simp] lemma sdiff_inf_sdiff : x \ y ⊓ (y \ x) = ⊥ := eq.symm $ calc ⊥ = (x ⊓ y) ⊓ (x \ y) : by rw inf_inf_sdiff ... = (x ⊓ (y ⊓ x ⊔ y \ x)) ⊓ (x \ y) : by rw sup_inf_sdiff ... = (x ⊓ (y ⊓ x) ⊔ x ⊓ (y \ x)) ⊓ (x \ y) : by rw inf_sup_left ... = (y ⊓ (x ⊓ x) ⊔ x ⊓ (y \ x)) ⊓ (x \ y) : by ac_refl ... = (y ⊓ x ⊔ x ⊓ (y \ x)) ⊓ (x \ y) : by rw inf_idem ... = (x ⊓ y ⊓ (x \ y)) ⊔ (x ⊓ (y \ x) ⊓ (x \ y)) : by rw [inf_sup_right, @inf_comm _ _ x y] ... = x ⊓ (y \ x) ⊓ (x \ y) : by rw [inf_inf_sdiff, bot_sup_eq] ... = x ⊓ (x \ y) ⊓ (y \ x) : by ac_refl ... = (x \ y) ⊓ (y \ x) : by rw inf_sdiff_right lemma disjoint_sdiff_sdiff : disjoint (x \ y) (y \ x) := sdiff_inf_sdiff.le theorem le_sup_sdiff : y ≤ x ⊔ (y \ x) := by { rw [sup_sdiff_self_right], exact le_sup_right } theorem le_sdiff_sup : y ≤ (y \ x) ⊔ x := by { rw [sup_comm], exact le_sup_sdiff } @[simp] theorem inf_sdiff_self_right : x ⊓ (y \ x) = ⊥ := calc x ⊓ (y \ x) = ((x ⊓ y) ⊔ (x \ y)) ⊓ (y \ x) : by rw sup_inf_sdiff ... = (x ⊓ y) ⊓ (y \ x) ⊔ (x \ y) ⊓ (y \ x) : by rw inf_sup_right ... = ⊥ : by rw [@inf_comm _ _ x y, inf_inf_sdiff, sdiff_inf_sdiff, bot_sup_eq] @[simp] theorem inf_sdiff_self_left : (y \ x) ⊓ x = ⊥ := by rw [inf_comm, inf_sdiff_self_right] theorem disjoint_sdiff_self_left : disjoint (y \ x) x := inf_sdiff_self_left.le theorem disjoint_sdiff_self_right : disjoint x (y \ x) := inf_sdiff_self_right.le lemma disjoint.disjoint_sdiff_left (h : disjoint x y) : disjoint (x \ z) y := h.mono_left sdiff_le lemma disjoint.disjoint_sdiff_right (h : disjoint x y) : disjoint x (y \ z) := h.mono_right sdiff_le /- TODO: if we had a typeclass for distributive lattices with `⊥`, we could make an alternative constructor for `generalized_boolean_algebra` using `disjoint x (y \ x)` and `x ⊔ (y \ x) = y` as axioms. -/ theorem disjoint.sdiff_eq_of_sup_eq (hi : disjoint x z) (hs : x ⊔ z = y) : y \ x = z := have h : y ⊓ x = x := inf_eq_right.2 $ le_sup_left.trans hs.le, sdiff_unique (by rw [h, hs]) (by rw [h, hi.eq_bot]) lemma disjoint.sup_sdiff_cancel_left (h : disjoint x y) : (x ⊔ y) \ x = y := h.sdiff_eq_of_sup_eq rfl lemma disjoint.sup_sdiff_cancel_right (h : disjoint x y) : (x ⊔ y) \ y = x := h.symm.sdiff_eq_of_sup_eq sup_comm protected theorem disjoint.sdiff_unique (hd : disjoint x z) (hz : z ≤ y) (hs : y ≤ x ⊔ z) : y \ x = z := sdiff_unique (begin rw ←inf_eq_right at hs, rwa [sup_inf_right, inf_sup_right, @sup_comm _ _ x, inf_sup_self, inf_comm, @sup_comm _ _ z, hs, sup_eq_left], end) (by rw [inf_assoc, hd.eq_bot, inf_bot_eq]) -- cf. `is_compl.disjoint_left_iff` and `is_compl.disjoint_right_iff` lemma disjoint_sdiff_iff_le (hz : z ≤ y) (hx : x ≤ y) : disjoint z (y \ x) ↔ z ≤ x := ⟨λ H, le_of_inf_le_sup_le (le_trans H bot_le) (begin rw sup_sdiff_of_le hx, refine le_trans (sup_le_sup_left sdiff_le z) _, rw sup_eq_right.2 hz, end), λ H, disjoint_sdiff_self_right.mono_left H⟩ -- cf. `is_compl.le_left_iff` and `is_compl.le_right_iff` lemma le_iff_disjoint_sdiff (hz : z ≤ y) (hx : x ≤ y) : z ≤ x ↔ disjoint z (y \ x) := (disjoint_sdiff_iff_le hz hx).symm -- cf. `is_compl.inf_left_eq_bot_iff` and `is_compl.inf_right_eq_bot_iff` lemma inf_sdiff_eq_bot_iff (hz : z ≤ y) (hx : x ≤ y) : z ⊓ (y \ x) = ⊥ ↔ z ≤ x := by { rw ←disjoint_iff, exact disjoint_sdiff_iff_le hz hx } -- cf. `is_compl.left_le_iff` and `is_compl.right_le_iff` lemma le_iff_eq_sup_sdiff (hz : z ≤ y) (hx : x ≤ y) : x ≤ z ↔ y = z ⊔ (y \ x) := ⟨λ H, begin apply le_antisymm, { conv_lhs { rw ←sup_inf_sdiff y x, }, apply sup_le_sup_right, rwa inf_eq_right.2 hx, }, { apply le_trans, { apply sup_le_sup_right hz, }, { rw sup_sdiff_left, } } end, λ H, begin conv_lhs at H { rw ←sup_sdiff_of_le hx, }, refine le_of_inf_le_sup_le _ H.le, rw inf_sdiff_self_right, exact bot_le, end⟩ -- cf. `set.union_diff_cancel'` lemma sup_sdiff_cancel' (hx : x ≤ z) (hz : z ≤ y) : z ⊔ (y \ x) = y := ((le_iff_eq_sup_sdiff hz (hx.trans hz)).1 hx).symm -- cf. `is_compl.sup_inf` lemma sdiff_sup : y \ (x ⊔ z) = (y \ x) ⊓ (y \ z) := sdiff_unique (calc y ⊓ (x ⊔ z) ⊔ y \ x ⊓ (y \ z) = (y ⊓ (x ⊔ z) ⊔ y \ x) ⊓ (y ⊓ (x ⊔ z) ⊔ (y \ z)) : by rw sup_inf_left ... = (y ⊓ x ⊔ y ⊓ z ⊔ y \ x) ⊓ (y ⊓ x ⊔ y ⊓ z ⊔ (y \ z)) : by rw @inf_sup_left _ _ y ... = (y ⊓ z ⊔ (y ⊓ x ⊔ y \ x)) ⊓ (y ⊓ x ⊔ (y ⊓ z ⊔ (y \ z))) : by ac_refl ... = (y ⊓ z ⊔ y) ⊓ (y ⊓ x ⊔ y) : by rw [sup_inf_sdiff, sup_inf_sdiff] ... = (y ⊔ y ⊓ z) ⊓ (y ⊔ y ⊓ x) : by ac_refl ... = y : by rw [sup_inf_self, sup_inf_self, inf_idem]) (calc y ⊓ (x ⊔ z) ⊓ ((y \ x) ⊓ (y \ z)) = (y ⊓ x ⊔ y ⊓ z) ⊓ ((y \ x) ⊓ (y \ z)) : by rw inf_sup_left ... = ((y ⊓ x) ⊓ ((y \ x) ⊓ (y \ z))) ⊔ ((y ⊓ z) ⊓ ((y \ x) ⊓ (y \ z))) : by rw inf_sup_right ... = ((y ⊓ x) ⊓ (y \ x) ⊓ (y \ z)) ⊔ ((y \ x) ⊓ ((y \ z) ⊓ (y ⊓ z))) : by ac_refl ... = ⊥ : by rw [inf_inf_sdiff, bot_inf_eq, bot_sup_eq, @inf_comm _ _ (y \ z), inf_inf_sdiff, inf_bot_eq]) -- cf. `is_compl.inf_sup` lemma sdiff_inf : y \ (x ⊓ z) = y \ x ⊔ y \ z := sdiff_unique (calc y ⊓ (x ⊓ z) ⊔ (y \ x ⊔ y \ z) = (z ⊓ (y ⊓ x)) ⊔ (y \ x ⊔ y \ z) : by ac_refl ... = (z ⊔ (y \ x ⊔ y \ z)) ⊓ ((y ⊓ x) ⊔ (y \ x ⊔ y \ z)) : by rw sup_inf_right ... = (y \ x ⊔ (y \ z ⊔ z)) ⊓ (y ⊓ x ⊔ (y \ x ⊔ y \ z)) : by ac_refl ... = (y ⊔ z) ⊓ ((y ⊓ x) ⊔ (y \ x ⊔ y \ z)) : by rw [sup_sdiff_self_left, ←sup_assoc, sup_sdiff_right] ... = (y ⊔ z) ⊓ y : by rw [←sup_assoc, sup_inf_sdiff, sup_sdiff_left] ... = y : by rw [inf_comm, inf_sup_self]) (calc y ⊓ (x ⊓ z) ⊓ ((y \ x) ⊔ (y \ z)) = (y ⊓ (x ⊓ z) ⊓ (y \ x)) ⊔ (y ⊓ (x ⊓ z) ⊓ (y \ z)) : by rw inf_sup_left ... = z ⊓ (y ⊓ x ⊓ (y \ x)) ⊔ z ⊓ (y ⊓ x) ⊓ (y \ z) : by ac_refl ... = z ⊓ (y ⊓ x) ⊓ (y \ z) : by rw [inf_inf_sdiff, inf_bot_eq, bot_sup_eq] ... = x ⊓ ((y ⊓ z) ⊓ (y \ z)) : by ac_refl ... = ⊥ : by rw [inf_inf_sdiff, inf_bot_eq]) @[simp] lemma sdiff_inf_self_right : y \ (x ⊓ y) = y \ x := by rw [sdiff_inf, sdiff_self, sup_bot_eq] @[simp] lemma sdiff_inf_self_left : y \ (y ⊓ x) = y \ x := by rw [inf_comm, sdiff_inf_self_right] lemma sdiff_eq_sdiff_iff_inf_eq_inf : y \ x = y \ z ↔ y ⊓ x = y ⊓ z := ⟨λ h, eq_of_inf_eq_sup_eq (by rw [inf_inf_sdiff, h, inf_inf_sdiff]) (by rw [sup_inf_sdiff, h, sup_inf_sdiff]), λ h, by rw [←sdiff_inf_self_right, ←@sdiff_inf_self_right _ z y, inf_comm, h, inf_comm]⟩ theorem disjoint.sdiff_eq_left (h : disjoint x y) : x \ y = x := by conv_rhs { rw [←sup_inf_sdiff x y, h.eq_bot, bot_sup_eq] } theorem disjoint.sdiff_eq_right (h : disjoint x y) : y \ x = y := h.symm.sdiff_eq_left -- cf. `is_compl_bot_top` @[simp] theorem sdiff_bot : x \ ⊥ = x := disjoint_bot_right.sdiff_eq_left theorem sdiff_eq_self_iff_disjoint : x \ y = x ↔ disjoint y x := calc x \ y = x ↔ x \ y = x \ ⊥ : by rw sdiff_bot ... ↔ x ⊓ y = x ⊓ ⊥ : sdiff_eq_sdiff_iff_inf_eq_inf ... ↔ disjoint y x : by rw [inf_bot_eq, inf_comm, disjoint_iff] theorem sdiff_eq_self_iff_disjoint' : x \ y = x ↔ disjoint x y := by rw [sdiff_eq_self_iff_disjoint, disjoint.comm] -- cf. `is_compl.antimono` lemma sdiff_le_sdiff_self (h : z ≤ x) : w \ x ≤ w \ z := le_of_inf_le_sup_le (calc (w \ x) ⊓ (w ⊓ z) ≤ (w \ x) ⊓ (w ⊓ x) : inf_le_inf le_rfl (inf_le_inf le_rfl h) ... = ⊥ : by rw [inf_comm, inf_inf_sdiff] ... ≤ (w \ z) ⊓ (w ⊓ z) : bot_le) (calc w \ x ⊔ (w ⊓ z) ≤ w \ x ⊔ (w ⊓ x) : sup_le_sup le_rfl (inf_le_inf le_rfl h) ... ≤ w : by rw [sup_comm, sup_inf_sdiff] ... = (w \ z) ⊔ (w ⊓ z) : by rw [sup_comm, sup_inf_sdiff]) lemma sdiff_le_iff : y \ x ≤ z ↔ y ≤ x ⊔ z := ⟨λ h, le_of_inf_le_sup_le (le_of_eq (calc y ⊓ (y \ x) = y \ x : inf_sdiff_right ... = (x ⊓ (y \ x)) ⊔ (z ⊓ (y \ x)) : by rw [inf_eq_right.2 h, inf_sdiff_self_right, bot_sup_eq] ... = (x ⊔ z) ⊓ (y \ x) : inf_sup_right.symm)) (calc y ⊔ y \ x = y : sup_sdiff_left ... ≤ y ⊔ (x ⊔ z) : le_sup_left ... = ((y \ x) ⊔ x) ⊔ z : by rw [←sup_assoc, ←@sup_sdiff_self_left _ x y] ... = x ⊔ z ⊔ y \ x : by ac_refl), λ h, le_of_inf_le_sup_le (calc y \ x ⊓ x = ⊥ : inf_sdiff_self_left ... ≤ z ⊓ x : bot_le) (calc y \ x ⊔ x = y ⊔ x : sup_sdiff_self_left ... ≤ (x ⊔ z) ⊔ x : sup_le_sup_right h x ... ≤ z ⊔ x : by rw [sup_assoc, sup_comm, sup_assoc, sup_idem])⟩ lemma sdiff_eq_bot_iff : y \ x = ⊥ ↔ y ≤ x := by rw [←le_bot_iff, sdiff_le_iff, sup_bot_eq] lemma sdiff_le_comm : x \ y ≤ z ↔ x \ z ≤ y := by rw [sdiff_le_iff, sup_comm, sdiff_le_iff] lemma sdiff_le_self_sdiff (h : w ≤ y) : w \ x ≤ y \ x := le_of_inf_le_sup_le (calc (w \ x) ⊓ (w ⊓ x) = ⊥ : by rw [inf_comm, inf_inf_sdiff] ... ≤ (y \ x) ⊓ (w ⊓ x) : bot_le) (calc w \ x ⊔ (w ⊓ x) = w : by rw [sup_comm, sup_inf_sdiff] ... ≤ (y ⊓ (y \ x)) ⊔ w : le_sup_right ... = (y ⊓ (y \ x)) ⊔ (y ⊓ w) : by rw inf_eq_right.2 h ... = y ⊓ ((y \ x) ⊔ w) : by rw inf_sup_left ... = ((y \ x) ⊔ (y ⊓ x)) ⊓ ((y \ x) ⊔ w) : by rw [@sup_comm _ _ (y \ x) (y ⊓ x), sup_inf_sdiff] ... = (y \ x) ⊔ ((y ⊓ x) ⊓ w) : by rw ←sup_inf_left ... = (y \ x) ⊔ ((w ⊓ y) ⊓ x) : by ac_refl ... = (y \ x) ⊔ (w ⊓ x) : by rw inf_eq_left.2 h) theorem sdiff_le_sdiff (h₁ : w ≤ y) (h₂ : z ≤ x) : w \ x ≤ y \ z := calc w \ x ≤ w \ z : sdiff_le_sdiff_self h₂ ... ≤ y \ z : sdiff_le_self_sdiff h₁ lemma sup_inf_inf_sdiff : (x ⊓ y) ⊓ z ⊔ (y \ z) = (x ⊓ y) ⊔ (y \ z) := calc (x ⊓ y) ⊓ z ⊔ (y \ z) = x ⊓ (y ⊓ z) ⊔ (y \ z) : by rw inf_assoc ... = (x ⊔ (y \ z)) ⊓ y : by rw [sup_inf_right, sup_inf_sdiff] ... = (x ⊓ y) ⊔ (y \ z) : by rw [inf_sup_right, inf_sdiff_left] @[simp] lemma inf_sdiff_sup_left : (x \ z) ⊓ (x ⊔ y) = x \ z := by rw [inf_sup_left, inf_sdiff_left, sup_inf_self] @[simp] lemma inf_sdiff_sup_right : (x \ z) ⊓ (y ⊔ x) = x \ z := by rw [sup_comm, inf_sdiff_sup_left] lemma sdiff_sdiff_right : x \ (y \ z) = (x \ y) ⊔ (x ⊓ y ⊓ z) := begin rw [sup_comm, inf_comm, ←inf_assoc, sup_inf_inf_sdiff], apply sdiff_unique, { calc x ⊓ (y \ z) ⊔ (z ⊓ x ⊔ x \ y) = (x ⊔ (z ⊓ x ⊔ x \ y)) ⊓ (y \ z ⊔ (z ⊓ x ⊔ x \ y)) : by rw sup_inf_right ... = (x ⊔ x ⊓ z ⊔ x \ y) ⊓ (y \ z ⊔ (x ⊓ z ⊔ x \ y)) : by ac_refl ... = x ⊓ (y \ z ⊔ x ⊓ z ⊔ x \ y) : by rw [sup_inf_self, sup_sdiff_left, ←sup_assoc] ... = x ⊓ (y \ z ⊓ (z ⊔ y) ⊔ x ⊓ (z ⊔ y) ⊔ x \ y) : by rw [sup_inf_left, sup_sdiff_self_left, inf_sup_right, @sup_comm _ _ y] ... = x ⊓ (y \ z ⊔ (x ⊓ z ⊔ x ⊓ y) ⊔ x \ y) : by rw [inf_sdiff_sup_right, @inf_sup_left _ _ x z y] ... = x ⊓ (y \ z ⊔ (x ⊓ z ⊔ (x ⊓ y ⊔ x \ y))) : by ac_refl ... = x ⊓ (y \ z ⊔ (x ⊔ x ⊓ z)) : by rw [sup_inf_sdiff, @sup_comm _ _ (x ⊓ z)] ... = x : by rw [sup_inf_self, sup_comm, inf_sup_self] }, { calc x ⊓ (y \ z) ⊓ (z ⊓ x ⊔ x \ y) = x ⊓ (y \ z) ⊓ (z ⊓ x) ⊔ x ⊓ (y \ z) ⊓ (x \ y) : by rw inf_sup_left ... = x ⊓ (y \ z ⊓ z ⊓ x) ⊔ x ⊓ (y \ z) ⊓ (x \ y) : by ac_refl ... = x ⊓ (y \ z) ⊓ (x \ y) : by rw [inf_sdiff_self_left, bot_inf_eq, inf_bot_eq, bot_sup_eq] ... = x ⊓ (y \ z ⊓ y) ⊓ (x \ y) : by conv_lhs { rw ←inf_sdiff_left } ... = x ⊓ (y \ z ⊓ (y ⊓ (x \ y))) : by ac_refl ... = ⊥ : by rw [inf_sdiff_self_right, inf_bot_eq, inf_bot_eq] } end lemma sdiff_sdiff_right' : x \ (y \ z) = (x \ y) ⊔ (x ⊓ z) := calc x \ (y \ z) = (x \ y) ⊔ (x ⊓ y ⊓ z) : sdiff_sdiff_right ... = z ⊓ x ⊓ y ⊔ (x \ y) : by ac_refl ... = (x \ y) ⊔ (x ⊓ z) : by rw [sup_inf_inf_sdiff, sup_comm, inf_comm] @[simp] lemma sdiff_sdiff_right_self : x \ (x \ y) = x ⊓ y := by rw [sdiff_sdiff_right, inf_idem, sdiff_self, bot_sup_eq] lemma sdiff_sdiff_eq_self (h : y ≤ x) : x \ (x \ y) = y := by rw [sdiff_sdiff_right_self, inf_of_le_right h] lemma sdiff_sdiff_left : (x \ y) \ z = x \ (y ⊔ z) := begin rw sdiff_sup, apply sdiff_unique, { rw [←inf_sup_left, sup_sdiff_self_right, inf_sdiff_sup_right] }, { rw [inf_assoc, @inf_comm _ _ z, inf_assoc, inf_sdiff_self_left, inf_bot_eq, inf_bot_eq] } end lemma sdiff_sdiff_left' : (x \ y) \ z = (x \ y) ⊓ (x \ z) := by rw [sdiff_sdiff_left, sdiff_sup] lemma sdiff_sdiff_comm : (x \ y) \ z = (x \ z) \ y := by rw [sdiff_sdiff_left, sup_comm, sdiff_sdiff_left] @[simp] lemma sdiff_idem : x \ y \ y = x \ y := by rw [sdiff_sdiff_left, sup_idem] @[simp] lemma sdiff_sdiff_self : x \ y \ x = ⊥ := by rw [sdiff_sdiff_comm, sdiff_self, bot_sdiff] lemma sdiff_sdiff_sup_sdiff : z \ (x \ y ⊔ y \ x) = z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ x) := calc z \ (x \ y ⊔ y \ x) = (z \ x ⊔ z ⊓ x ⊓ y) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by rw [sdiff_sup, sdiff_sdiff_right, sdiff_sdiff_right] ... = z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by rw [sup_inf_left, sup_comm, sup_inf_sdiff] ... = z ⊓ (z \ x ⊔ y) ⊓ (z ⊓ (z \ y ⊔ x)) : by rw [sup_inf_left, @sup_comm _ _ (z \ y), sup_inf_sdiff] ... = z ⊓ z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ x) : by ac_refl ... = z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ x) : by rw inf_idem lemma sdiff_sdiff_sup_sdiff' : z \ (x \ y ⊔ y \ x) = z ⊓ x ⊓ y ⊔ ((z \ x) ⊓ (z \ y)) := calc z \ (x \ y ⊔ y \ x) = z \ (x \ y) ⊓ (z \ (y \ x)) : sdiff_sup ... = (z \ x ⊔ z ⊓ x ⊓ y) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by rw [sdiff_sdiff_right, sdiff_sdiff_right] ... = (z \ x ⊔ z ⊓ y ⊓ x) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by ac_refl ... = (z \ x) ⊓ (z \ y) ⊔ z ⊓ y ⊓ x : sup_inf_right.symm ... = z ⊓ x ⊓ y ⊔ ((z \ x) ⊓ (z \ y)) : by ac_refl lemma sup_sdiff : (x ⊔ y) \ z = (x \ z) ⊔ (y \ z) := sdiff_unique (calc (x ⊔ y) ⊓ z ⊔ (x \ z ⊔ y \ z) = (x ⊓ z ⊔ y ⊓ z) ⊔ (x \ z ⊔ y \ z) : by rw inf_sup_right ... = x ⊓ z ⊔ x \ z ⊔ y \ z ⊔ y ⊓ z : by ac_refl ... = x ⊔ (y ⊓ z ⊔ y \ z) : by rw [sup_inf_sdiff, sup_assoc, @sup_comm _ _ (y \ z)] ... = x ⊔ y : by rw sup_inf_sdiff) (calc (x ⊔ y) ⊓ z ⊓ (x \ z ⊔ y \ z) = (x ⊓ z ⊔ y ⊓ z) ⊓ (x \ z ⊔ y \ z) : by rw inf_sup_right ... = (x ⊓ z ⊔ y ⊓ z) ⊓ (x \ z) ⊔ ((x ⊓ z ⊔ y ⊓ z) ⊓ (y \ z)) : by rw [@inf_sup_left _ _ (x ⊓ z ⊔ y ⊓ z)] ... = (y ⊓ z ⊓ (x \ z)) ⊔ ((x ⊓ z ⊔ y ⊓ z) ⊓ (y \ z)) : by rw [inf_sup_right, inf_inf_sdiff, bot_sup_eq] ... = (x ⊓ z ⊔ y ⊓ z) ⊓ (y \ z) : by rw [inf_assoc, inf_sdiff_self_right, inf_bot_eq, bot_sup_eq] ... = x ⊓ z ⊓ (y \ z) : by rw [inf_sup_right, inf_inf_sdiff, sup_bot_eq] ... = ⊥ : by rw [inf_assoc, inf_sdiff_self_right, inf_bot_eq]) lemma sup_sdiff_right_self : (x ⊔ y) \ y = x \ y := by rw [sup_sdiff, sdiff_self, sup_bot_eq] lemma sup_sdiff_left_self : (x ⊔ y) \ x = y \ x := by rw [sup_comm, sup_sdiff_right_self] lemma inf_sdiff : (x ⊓ y) \ z = (x \ z) ⊓ (y \ z) := sdiff_unique (calc (x ⊓ y) ⊓ z ⊔ ((x \ z) ⊓ (y \ z)) = ((x ⊓ y) ⊓ z ⊔ (x \ z)) ⊓ ((x ⊓ y) ⊓ z ⊔ (y \ z)) : by rw [sup_inf_left] ... = (x ⊓ y ⊓ (z ⊔ x) ⊔ x \ z) ⊓ (x ⊓ y ⊓ z ⊔ y \ z) : by rw [sup_inf_right, sup_sdiff_self_right, inf_sup_right, inf_sdiff_sup_right] ... = (y ⊓ (x ⊓ (x ⊔ z)) ⊔ x \ z) ⊓ (x ⊓ y ⊓ z ⊔ y \ z) : by ac_refl ... = ((y ⊓ x) ⊔ (x \ z)) ⊓ ((x ⊓ y) ⊔ (y \ z)) : by rw [inf_sup_self, sup_inf_inf_sdiff] ... = (x ⊓ y) ⊔ ((x \ z) ⊓ (y \ z)) : by rw [@inf_comm _ _ y, sup_inf_left] ... = x ⊓ y : sup_eq_left.2 (inf_le_inf sdiff_le sdiff_le)) (calc (x ⊓ y) ⊓ z ⊓ ((x \ z) ⊓ (y \ z)) = x ⊓ y ⊓ (z ⊓ (x \ z)) ⊓ (y \ z) : by ac_refl ... = ⊥ : by rw [inf_sdiff_self_right, inf_bot_eq, bot_inf_eq]) lemma inf_sdiff_assoc : (x ⊓ y) \ z = x ⊓ (y \ z) := sdiff_unique (calc x ⊓ y ⊓ z ⊔ x ⊓ (y \ z) = x ⊓ (y ⊓ z) ⊔ x ⊓ (y \ z) : by rw inf_assoc ... = x ⊓ ((y ⊓ z) ⊔ y \ z) : inf_sup_left.symm ... = x ⊓ y : by rw sup_inf_sdiff) (calc x ⊓ y ⊓ z ⊓ (x ⊓ (y \ z)) = x ⊓ x ⊓ ((y ⊓ z) ⊓ (y \ z)) : by ac_refl ... = ⊥ : by rw [inf_inf_sdiff, inf_bot_eq]) lemma sup_eq_sdiff_sup_sdiff_sup_inf : x ⊔ y = (x \ y) ⊔ (y \ x) ⊔ (x ⊓ y) := eq.symm $ calc (x \ y) ⊔ (y \ x) ⊔ (x ⊓ y) = ((x \ y) ⊔ (y \ x) ⊔ x) ⊓ ((x \ y) ⊔ (y \ x) ⊔ y) : by rw sup_inf_left ... = ((x \ y) ⊔ x ⊔ (y \ x)) ⊓ ((x \ y) ⊔ ((y \ x) ⊔ y)) : by ac_refl ... = (x ⊔ (y \ x)) ⊓ ((x \ y) ⊔ y) : by rw [sup_sdiff_right, sup_sdiff_right] ... = x ⊔ y : by rw [sup_sdiff_self_right, sup_sdiff_self_left, inf_idem] instance pi.generalized_boolean_algebra {α : Type u} {β : Type v} [generalized_boolean_algebra β] : generalized_boolean_algebra (α → β) := by pi_instance end generalized_boolean_algebra /-! ### Boolean algebras -/ /-- Set / lattice complement -/ class has_compl (α : Type*) := (compl : α → α) export has_compl (compl) postfix `ᶜ`:(max+1) := compl /-- This class contains the core axioms of a Boolean algebra. The `boolean_algebra` class extends both this class and `generalized_boolean_algebra`, see Note [forgetful inheritance]. -/ class boolean_algebra.core (α : Type u) extends bounded_distrib_lattice α, has_compl α := (inf_compl_le_bot : ∀x:α, x ⊓ xᶜ ≤ ⊥) (top_le_sup_compl : ∀x:α, ⊤ ≤ x ⊔ xᶜ) section boolean_algebra_core variables [boolean_algebra.core α] @[simp] theorem inf_compl_eq_bot : x ⊓ xᶜ = ⊥ := bot_unique $ boolean_algebra.core.inf_compl_le_bot x @[simp] theorem compl_inf_eq_bot : xᶜ ⊓ x = ⊥ := eq.trans inf_comm inf_compl_eq_bot @[simp] theorem sup_compl_eq_top : x ⊔ xᶜ = ⊤ := top_unique $ boolean_algebra.core.top_le_sup_compl x @[simp] theorem compl_sup_eq_top : xᶜ ⊔ x = ⊤ := eq.trans sup_comm sup_compl_eq_top theorem is_compl_compl : is_compl x xᶜ := is_compl.of_eq inf_compl_eq_bot sup_compl_eq_top theorem is_compl.eq_compl (h : is_compl x y) : x = yᶜ := h.left_unique is_compl_compl.symm theorem is_compl.compl_eq (h : is_compl x y) : xᶜ = y := (h.right_unique is_compl_compl).symm theorem eq_compl_iff_is_compl : x = yᶜ ↔ is_compl x y := ⟨λ h, by { rw h, exact is_compl_compl.symm }, is_compl.eq_compl⟩ theorem compl_eq_iff_is_compl : xᶜ = y ↔ is_compl x y := ⟨λ h, by { rw ←h, exact is_compl_compl }, is_compl.compl_eq⟩ theorem disjoint_compl_right : disjoint x xᶜ := is_compl_compl.disjoint theorem disjoint_compl_left : disjoint xᶜ x := disjoint_compl_right.symm theorem compl_unique (i : x ⊓ y = ⊥) (s : x ⊔ y = ⊤) : xᶜ = y := (is_compl.of_eq i s).compl_eq @[simp] theorem compl_top : ⊤ᶜ = (⊥:α) := is_compl_top_bot.compl_eq @[simp] theorem compl_bot : ⊥ᶜ = (⊤:α) := is_compl_bot_top.compl_eq @[simp] theorem compl_compl (x : α) : xᶜᶜ = x := is_compl_compl.symm.compl_eq theorem compl_injective : function.injective (compl : α → α) := function.involutive.injective compl_compl @[simp] theorem compl_inj_iff : xᶜ = yᶜ ↔ x = y := compl_injective.eq_iff theorem is_compl.compl_eq_iff (h : is_compl x y) : zᶜ = y ↔ z = x := h.compl_eq ▸ compl_inj_iff @[simp] theorem compl_eq_top : xᶜ = ⊤ ↔ x = ⊥ := is_compl_bot_top.compl_eq_iff @[simp] theorem compl_eq_bot : xᶜ = ⊥ ↔ x = ⊤ := is_compl_top_bot.compl_eq_iff @[simp] theorem compl_inf : (x ⊓ y)ᶜ = xᶜ ⊔ yᶜ := (is_compl_compl.inf_sup is_compl_compl).compl_eq @[simp] theorem compl_sup : (x ⊔ y)ᶜ = xᶜ ⊓ yᶜ := (is_compl_compl.sup_inf is_compl_compl).compl_eq theorem compl_le_compl (h : y ≤ x) : xᶜ ≤ yᶜ := is_compl_compl.antimono is_compl_compl h @[simp] theorem compl_le_compl_iff_le : yᶜ ≤ xᶜ ↔ x ≤ y := ⟨assume h, by have h := compl_le_compl h; simp at h; assumption, compl_le_compl⟩ theorem le_compl_of_le_compl (h : y ≤ xᶜ) : x ≤ yᶜ := by simpa only [compl_compl] using compl_le_compl h theorem compl_le_of_compl_le (h : yᶜ ≤ x) : xᶜ ≤ y := by simpa only [compl_compl] using compl_le_compl h theorem le_compl_iff_le_compl : y ≤ xᶜ ↔ x ≤ yᶜ := ⟨le_compl_of_le_compl, le_compl_of_le_compl⟩ theorem compl_le_iff_compl_le : xᶜ ≤ y ↔ yᶜ ≤ x := ⟨compl_le_of_compl_le, compl_le_of_compl_le⟩ namespace boolean_algebra @[priority 100] instance : is_complemented α := ⟨λ x, ⟨xᶜ, is_compl_compl⟩⟩ end boolean_algebra end boolean_algebra_core /-- A Boolean algebra is a bounded distributive lattice with a complement operator `ᶜ` such that `x ⊓ xᶜ = ⊥` and `x ⊔ xᶜ = ⊤`. For convenience, it must also provide a set difference operation `\` satisfying `x \ y = x ⊓ yᶜ`. This is a generalization of (classical) logic of propositions, or the powerset lattice. -/ -- Lean complains about metavariables in the type if the universe is not specified class boolean_algebra (α : Type u) extends generalized_boolean_algebra α, boolean_algebra.core α := (sdiff_eq : ∀x y:α, x \ y = x ⊓ yᶜ) -- TODO: is there a way to automatically fill in the proofs of sup_inf_sdiff and inf_inf_sdiff given -- everything in `boolean_algebra.core` and `sdiff_eq`? The following doesn't work: -- (sup_inf_sdiff := λ a b, by rw [sdiff_eq, ←inf_sup_left, sup_compl_eq_top, inf_top_eq]) /-- Create a `boolean_algebra` instance from a `boolean_algebra.core` instance, defining `x \ y` to be `x ⊓ yᶜ`. For some types, it may be more convenient to create the `boolean_algebra` instance by hand in order to have a simpler `sdiff` operation. -/ def boolean_algebra.of_core (B : boolean_algebra.core α) : boolean_algebra α := { sdiff := λ x y, x ⊓ yᶜ, sdiff_eq := λ _ _, rfl, sup_inf_sdiff := λ a b, by rw [←inf_sup_left, sup_compl_eq_top, inf_top_eq], inf_inf_sdiff := λ a b, by rw [inf_left_right_swap, @inf_assoc _ _ a, compl_inf_eq_bot, inf_bot_eq, bot_inf_eq], ..B } section boolean_algebra variables [boolean_algebra α] theorem sdiff_eq : x \ y = x ⊓ yᶜ := boolean_algebra.sdiff_eq x y theorem sdiff_compl : x \ yᶜ = x ⊓ y := by rw [sdiff_eq, compl_compl] theorem top_sdiff : ⊤ \ x = xᶜ := by rw [sdiff_eq, top_inf_eq] @[simp] theorem sdiff_top : x \ ⊤ = ⊥ := by rw [sdiff_eq, compl_top, inf_bot_eq] end boolean_algebra instance boolean_algebra_Prop : boolean_algebra Prop := boolean_algebra.of_core { compl := not, inf_compl_le_bot := λ p ⟨Hp, Hpc⟩, Hpc Hp, top_le_sup_compl := λ p H, classical.em p, .. bounded_distrib_lattice_Prop } instance pi.boolean_algebra {ι : Type u} {α : ι → Type v} [∀ i, boolean_algebra (α i)] : boolean_algebra (Π i, α i) := by refine_struct { sdiff := λ x y i, x i \ y i, compl := λ x i, (x i)ᶜ, .. pi.bounded_lattice }; tactic.pi_instance_derive_field
27ec567f934666edd7abf38441a723e57f3220f3
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/number_theory/bernoulli.lean
8ed438170640089bb85627515f03c124037cb453
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
3,020
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.rat import data.fintype import data.fintype.card /-! # Bernoulli numbers The Bernoulli numbers are a sequence of numbers that frequently show up in number theory. For example, they show up in the Taylor series of many trigonometric and hyperbolic functions, and also as (integral multiples of products of powers of `π` and) special values of the Riemann zeta function. (Note: these facts are not yet available in mathlib) In this file, we provide the definition, and the basic fact (`sum_bernoulli`) that $$ ∑_{k < n} \binom{n}{k} * B_k = n, $$ where $B_k$ denotes the the $k$-th Bernoulli number. -/ /-- The Bernoulli numbers: the $n$-th Bernoulli number $B_n$ is defined recursively via $$B_n = \sum_{k < n} \binom{n}{k} * \frac{B_k}{n+1-k}$$ -/ def bernoulli : ℕ → ℚ := well_founded.fix nat.lt_wf (λ n bernoulli, 1 - finset.univ.sum (λ k : fin n, (n.choose k) * bernoulli k k.2 / (n + 1 - k))) lemma bernoulli_def' (n : ℕ) : bernoulli n = 1 - finset.univ.sum (λ (k : fin n), (n.choose k) * (bernoulli k) / (n + 1 - k)) := well_founded.fix_eq _ _ _ lemma bernoulli_def (n : ℕ) : bernoulli n = 1 - (finset.range n).sum (λ k, (n.choose k) * (bernoulli k) / (n + 1 - k)) := by { rw [bernoulli_def', finset.range_sum_eq_univ_sum], refl } @[simp] lemma bernoulli_zero : bernoulli 0 = 1 := rfl @[simp] lemma bernoulli_one : bernoulli 1 = 1/2 := begin rw [bernoulli_def], repeat { try { rw [finset.sum_range_succ] }, try { rw [nat.choose_succ_succ] }, simp, norm_num1 } end @[simp] lemma bernoulli_two : bernoulli 2 = 1/6 := begin rw [bernoulli_def], repeat { try { rw [finset.sum_range_succ] }, try { rw [nat.choose_succ_succ] }, simp, norm_num1 } end @[simp] lemma bernoulli_three : bernoulli 3 = 0 := begin rw [bernoulli_def], repeat { try { rw [finset.sum_range_succ] }, try { rw [nat.choose_succ_succ] }, simp, norm_num1 } end @[simp] lemma bernoulli_four : bernoulli 4 = -1/30 := begin rw [bernoulli_def], repeat { try { rw [finset.sum_range_succ] }, try { rw [nat.choose_succ_succ] }, simp, norm_num1 } end @[simp] lemma sum_bernoulli (n : ℕ) : (finset.range n).sum (λ k, (n.choose k : ℚ) * bernoulli k) = n := begin induction n with n ih, { simp }, rw [finset.sum_range_succ], rw [nat.choose_succ_self_right], rw [bernoulli_def, mul_sub, mul_one, sub_add_eq_add_sub, sub_eq_iff_eq_add], rw [add_left_cancel_iff, finset.mul_sum, finset.sum_congr rfl], intros k hk, rw finset.mem_range at hk, rw [mul_div_right_comm, ← mul_assoc], congr' 1, rw [← mul_div_assoc, eq_div_iff], { rw [mul_comm ((n+1 : ℕ) : ℚ)], rw_mod_cast nat.choose_mul_succ_eq n k, rw [int.coe_nat_mul], have hk' : k ≤ n + 1, by linarith, rw [int.coe_nat_sub hk', int.sub_nat_nat_eq_coe] }, { contrapose! hk with H, rw sub_eq_zero at H, norm_cast at H, linarith } end
a669bd9cf3c4bddd21135e737f260c9782f52db3
e38e95b38a38a99ecfa1255822e78e4b26f65bb0
/src/certigrad/env.lean
c3244043c55c0883d83d9b3b7f3ec2c7d754d5b8
[ "Apache-2.0" ]
permissive
ColaDrill/certigrad
fefb1be3670adccd3bed2f3faf57507f156fd501
fe288251f623ac7152e5ce555f1cd9d3a20203c2
refs/heads/master
1,593,297,324,250
1,499,903,753,000
1,499,903,753,000
97,075,797
1
0
null
1,499,916,210,000
1,499,916,210,000
null
UTF-8
Lean
false
false
9,277
lean
/- Copyright (c) 2017 Daniel Selsam. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Daniel Selsam Environments. -/ import data.hash_map library_dev.data.list.sort .tensor .id .util .reference namespace certigrad def pre_env : Type := hash_map reference (λ (ref : reference), T ref.2) attribute [reducible] pre_env namespace pre_env definition eqv (m₁ m₂ : pre_env) : Prop := ∀ (ref : reference), m₁^.find ref = m₂^.find ref local infix ~ := eqv definition eqv.refl (m : pre_env) : m ~ m := assume ref, rfl definition eqv.symm (m₁ m₂ : pre_env) : m₁ ~ m₂ → m₂ ~ m₁ := assume H ref, eq.symm (H ref) definition eqv.trans (m₁ m₂ m₃ : pre_env) : m₁ ~ m₂ → m₂ ~ m₃ → m₁ ~ m₃ := assume H₁ H₂ ref, eq.trans (H₁ ref) (H₂ ref) instance pdmap.eqv_setoid : setoid pre_env := setoid.mk eqv (mk_equivalence eqv eqv.refl eqv.symm eqv.trans) end pre_env def env : Type := quot pre_env.eqv namespace env def mk : env := quotient.mk (mk_hash_map reference.hash) def get (ref : reference) (q : env) : T ref.2 := quotient.lift_on q (λ (m : pre_env), match m^.find ref with | none := default _ | some x := x end) begin intros m₁ m₂ H_eqv, simp [H_eqv ref] end def insert (ref : reference) (x : T ref.2) (q : env) : env := quotient.lift_on q (λ (m : pre_env), quotient.mk $ m^.insert ref x) begin intros m₁ m₂ H_eqv, dsimp, apply quotient.sound, intros ref', cases (decidable.em (ref = ref')) with H_eq H_neq, simp [hash_map.find_insert, dif_ctx_simp_congr, H_eq, dif_pos], simp [hash_map.find_insert, dif_ctx_simp_congr, H_neq, dif_neg, H_eqv ref'], end def has_key (ref : reference) (q : env) : Prop := quotient.lift_on q (λ (m : pre_env), m^.contains ref) begin intros m₁ m₂ H_eqv, simp [hash_map.contains, H_eqv ref] end def get_ks : Π (refs : list reference) (m : env), dvec T refs^.p2 | [] m := ⟦⟧ | (ref::refs) m := dvec.cons (get ref m) (get_ks refs m) def insert_all : Π (refs : list reference) (vs : dvec T refs^.p2), env | [] ⟦⟧ := env.mk | (k::ks) (v:::vs) := env.insert k v (insert_all ks vs) -- Facts @[simp] lemma get.def (ref : reference) (m : pre_env) : get ref (quotient.mk m) = match m^.find ref with | none := default _ | some x := x end := rfl @[simp] lemma insert.def {ref : reference} {x : T ref.2} (m : pre_env) : insert ref x (quotient.mk m) = quotient.mk (m^.insert ref x) := begin apply quotient.sound, apply pre_env.eqv.refl end @[simp] lemma has_key.def (ref : reference) (m : pre_env) : has_key ref (quotient.mk m) = m^.contains ref := rfl @[simp] lemma bool_lift_t (b : bool) : (lift_t b : Prop) = (b = tt) := rfl -- TODO(dhs): PR to standard library lemma not_has_key_empty (ref : reference) : ¬ env.has_key ref env.mk := begin simp [mk], rw hash_map.contains_iff, simp [hash_map.keys, hash_map.entries, mk_hash_map, bucket_array.as_list, mk_array, array.foldl, array.iterate, array.iterate_aux, list.map, array.read], end lemma has_key_insert {ref₁ ref₂ : reference} {x₂ : T ref₂.2} {m : env} : has_key ref₁ m → has_key ref₁ (insert ref₂ x₂ m) := begin apply @quotient.induction_on _ _ (λ m, has_key ref₁ m → has_key ref₁ (insert ref₂ x₂ m)), clear m, intro m, intro H_hk, simp at *, dsimp [hash_map.contains] at *, rw hash_map.find_insert, cases decidable.em (ref₂ = ref₁) with H_eq H_neq, { subst H_eq, simp [dif_ctx_simp_congr, dif_pos], dunfold option.is_some, reflexivity, }, { simp [H_neq, dif_ctx_simp_congr, dif_neg], exact H_hk } end lemma has_key_insert_same (ref : reference) {x : T ref.2} (m : env) : has_key ref (insert ref x m) := begin apply quotient.induction_on m, clear m, intro m, simp, dunfold hash_map.contains, rw hash_map.find_insert_eq, dsimp [option.is_some], reflexivity end lemma has_key_insert_diff {ref₁ ref₂ : reference} {x : T ref₂.2} {m : env} : ref₁ ≠ ref₂ → has_key ref₁ (insert ref₂ x m) → has_key ref₁ m := begin apply @quotient.induction_on _ _ (λ m, ref₁ ≠ ref₂ → has_key ref₁ (insert ref₂ x m) → has_key ref₁ m), clear m, simp [hash_map.contains], intros m H_neq, rw hash_map.find_insert_ne, intro H, exact H, exact ne.symm H_neq end lemma get_insert_same (ref : reference) (x : T ref.2) (m : env) : get ref (insert ref x m) = x := begin apply quotient.induction_on m, clear m, intro m, simp, rw hash_map.find_insert_eq, end lemma get_insert_diff {ref₁ ref₂ : reference} (x₂ : T ref₂.2) (m : env) : ref₁ ≠ ref₂ → get ref₁ (insert ref₂ x₂ m) = get ref₁ m := begin apply @quotient.induction_on _ _ (λ m, ref₁ ≠ ref₂ → get ref₁ (insert ref₂ x₂ m) = get ref₁ m), clear m, intros m H_neq, simp, rw hash_map.find_insert, -- TODO(dhs): annoying that we can't simplify inside the major premise assert H_dif : (dite (ref₂ = ref₁) (λ h, some (eq.rec_on h x₂ : T ref₁.2)) (λ h, hash_map.find m ref₁)) = hash_map.find m ref₁, simp [dif_ctx_simp_congr, dif_neg, ne.symm H_neq], rw H_dif, end -- TODO(dhs): propagate precondition lemma insert_get_same {ref : reference} {m : env} : has_key ref m → insert ref (get ref m) m = m := begin apply @quotient.induction_on _ _ (λ m, has_key ref m → insert ref (get ref m) m = m), clear m, simp [hash_map.contains], intros m H_has_key, apply quotient.sound, intro ref', cases decidable.em (ref' = ref) with H_eq H_neq, { subst H_eq, rw hash_map.find_insert_eq, cases (hash_map.find m ref'), { dsimp [option.is_some] at H_has_key, injection H_has_key }, dsimp, reflexivity }, { rw hash_map.find_insert_ne, exact ne.symm H_neq } end lemma insert_insert_flip {ref₁ ref₂ : reference} (x₁ : T ref₁.2) (x₂ : T ref₂.2) (m : env) : ref₁ ≠ ref₂ → insert ref₁ x₁ (insert ref₂ x₂ m) = insert ref₂ x₂ (insert ref₁ x₁ m) := begin apply @quotient.induction_on _ _ (λ m, ref₁ ≠ ref₂ → insert ref₁ x₁ (insert ref₂ x₂ m) = insert ref₂ x₂ (insert ref₁ x₁ m)), clear m, simp, intros m H_neq, apply quot.sound, intro ref, simp [hash_map.find_insert], cases decidable.em (ref₁ = ref) with H_eq₁ H_neq₁, cases decidable.em (ref₂ = ref) with H_eq₂ H_neq₂, { exfalso, exact H_neq (eq.trans H_eq₁ (eq.symm H_eq₂)) }, { subst H_eq₁, simp [H_neq₂, dif_ctx_simp_congr, dif_neg, dif_pos] }, cases decidable.em (ref₂ = ref) with H_eq₂ H_neq₂, { subst H_eq₂, simp [H_neq₁, dif_ctx_simp_congr, dif_neg, dif_pos] }, { simp [H_neq₁, H_neq₂, dif_ctx_simp_congr, dif_neg], } end lemma insert_insert_same (ref : reference) (x₁ x₂ : T ref.2) (m : env) : insert ref x₁ (insert ref x₂ m) = insert ref x₁ m := begin apply quotient.induction_on m, clear m, simp, intros m, apply quot.sound, intro ref', cases decidable.em (ref' = ref) with H_eq H_neq, { subst H_eq, simp [hash_map.find_insert_eq] }, -- TODO(dhs): simp fails for annoying reasons rw hash_map.find_insert_ne _ _ _ _ (ne.symm H_neq), rw hash_map.find_insert_ne _ _ _ _ (ne.symm H_neq), rw hash_map.find_insert_ne _ _ _ _ (ne.symm H_neq) end lemma get_ks_env_eq (m₁ m₂ : env) : ∀ (refs : list reference), (∀ (ref : reference), ref ∈ refs → get ref m₁ = get ref m₂) → get_ks refs m₁ = get_ks refs m₂ | [] H := rfl | (ref::refs) H := show get ref m₁ ::: get_ks refs m₁ = get ref m₂ ::: get_ks refs m₂, from have H_get : get ref m₁ = get ref m₂, from H ref list.mem_of_cons_same, have H_pre : ∀ (ref : reference), ref ∈ refs → get ref m₁ = get ref m₂, from assume r H_r_mem, H r (list.mem_cons_of_mem _ H_r_mem), by rw [H_get, get_ks_env_eq _ H_pre] lemma get_ks_insert_diff : ∀ {refs : list reference} {ref : reference} {x : T ref.2} {m : env}, ref ∉ refs → get_ks refs (insert ref x m) = get_ks refs m | [] _ _ _ _ := rfl | (ref::refs) ref₀ x m H_ref₀_notin := show get ref (insert ref₀ x m) ::: get_ks refs (insert ref₀ x m) = get ref m ::: get_ks refs m, from have H_ne : ref ≠ ref₀, from ne.symm (list.ne_of_not_mem_cons H_ref₀_notin), begin rw (env.get_insert_diff _ _ H_ne), rw get_ks_insert_diff (list.not_mem_of_not_mem_cons H_ref₀_notin), end lemma dvec_update_at_env {refs : list reference} {idx : ℕ} {ref : reference} (m : env) : list.at_idx refs idx ref → dvec.update_at (get ref m) (get_ks refs m) idx = get_ks refs m := begin intro H_at_idx, assert H_elem_at_idx : list.elem_at_idx refs idx ref, { exact list.elem_at_idx_of_at_idx H_at_idx }, induction H_elem_at_idx with xs x xs idx' x y H_elem_at_idx IH, { dsimp [get_ks], simp [dif_ctx_simp_congr, dif_pos] }, { dsimp [get_ks], erw IH (list.at_idx_of_cons H_at_idx) } end lemma dvec_get_get_ks {refs : list reference} {idx : ℕ} {ref : reference} (m : env) : list.at_idx refs idx ref → dvec.get ref.2 (get_ks refs m) idx = get ref m := begin intro H_at_idx, assert H_elem_at_idx : list.elem_at_idx refs idx ref, { exact list.elem_at_idx_of_at_idx H_at_idx }, induction H_elem_at_idx with xs x xs idx' x y H_elem_at_idx IH, { dunfold get_ks, erw dvec.get.equations._eqn_2, simp [dif_ctx_simp_congr, dif_pos] }, { dunfold get_ks, erw dvec.get.equations._eqn_3, exact IH (list.at_idx_of_cons H_at_idx) } end end env attribute [semireducible] pre_env end certigrad
b3c6e7fd418ac748fe5da0f1b63ada6ef248bda4
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/elab5.lean
243560966155f39e566fa00afa7c3bb05ead7f28
[ "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
498
lean
variable C {A B : Type} (H : A = B) (a : A) : B variable D {A A' : Type} {B : A -> Type} {B' : A' -> Type} (H : (forall x : A, B x) = (forall x : A', B' x)) : A = A' variable R {A A' : Type} {B : A -> Type} {B' : A' -> Type} (H : (forall x : A, B x) = (forall x : A', B' x)) (a : A) : (B a) = (B' (C (D H) a)) theorem R2 : forall (A1 A2 B1 B2 : Type), ((A1 -> B1) = (A2 -> B2)) -> A1 -> (B1 = B2) := fun A1 A2 B1 B2 H a, R H a set_option pp::implicit true print environment 7.
be86fdd6bccaf8058d88bf163123f0de52bb7ed1
019219cee6f24c05395ca6ccf1365289a9ffdebc
/mason_stothers_v2.lean
43069a7cd647da4d4504fd7a0b1e39bb99ca5479
[]
no_license
ChrisHughes24/mason-stother
df79efeb661cefe9ebba05415ca80d005135df4e
a4095993c2f7fc58091f1a1d05459eae17f786b1
refs/heads/master
1,584,409,893,766
1,523,892,476,000
1,523,892,476,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
54,950
lean
-- the to be Mason-Stother formalization -- Authors Johannes & Jens --Defining the gcd import poly --import euclidean_domain --Need to fix an admit in UFD, for the ...multiset lemma import unique_factorization_domain import data.finsupp import algebraically_closed_field import poly_over_UFD import poly_over_field import poly_derivative noncomputable theory local infix ^ := monoid.pow local notation `d[`a`]` := polynomial.derivative a local notation Σ := finset.sume local notation Π := finset.prod local notation `Π₀` := finsupp.prod local notation `~`a:=polynomial a local notation a `~ᵤ` b : 50 := associated a b open polynomial open associated open classical local attribute [instance] prop_decidable -- TODO: there is some problem with the module instances for module.to_add_comm_group ... -- force ring.to_add_comm_group to be stronger attribute [instance] ring.to_add_comm_group universe u attribute [instance] field.to_unique_factorization_domain --correct? variable {β : Type u} variables [field β] open classical multiset section mason_stothers --It might be good to remove the attribute to domain of integral domain? def rad (p : polynomial β) : polynomial β := p.factors.erase_dup.prod lemma rad_ne_zero {p : polynomial β} : rad p ≠ 0 := begin rw [rad], apply multiset.prod_ne_zero_of_forall_mem_ne_zero, intros x h1, have h2 : irreducible x, { rw mem_erase_dup at h1, exact p.factors_irred x h1, }, exact h2.1, end --naming --Where do we use this? lemma degree_rad_eq_sum_support_degree {f : polynomial β} : degree (rad f) = sum (map degree f.factors.erase_dup) := begin rw rad, have h1 : finset.prod (to_finset (polynomial.factors f)) id ≠ 0, { apply polynomial.prod_ne_zero_of_forall_mem_ne_zero, intros x h1, have : irreducible x, { rw mem_to_finset at h1, exact f.factors_irred x h1, }, exact and.elim_left this, }, rw ←to_finset_val f.factors, exact calc degree (prod ((to_finset (polynomial.factors f)).val)) = degree (prod (map id (to_finset (polynomial.factors f)).val)) : by rw map_id_eq ... = sum (map degree ((to_finset (polynomial.factors f)).val)) : degree_prod_eq_sum_degree_of_prod_ne_zero h1, end private lemma mem_factors_of_mem_factors_sub_factors_erase_dup (f : polynomial β) (x : polynomial β) (h : x ∈ (f.factors)-(f.factors.erase_dup)) : x ∈ f.factors := begin have : ((f.factors)-(f.factors.erase_dup)) ≤ f.factors, from multiset.sub_le_self _ _, exact mem_of_le this h, end --naming lemma prod_pow_min_on_ne_zero {f : polynomial β} : ((f.factors)-(f.factors.erase_dup)).prod ≠ 0 := begin apply multiset.prod_ne_zero_of_forall_mem_ne_zero, intros x h, have h1 : x ∈ f.factors, from mem_factors_of_mem_factors_sub_factors_erase_dup f x h, have : irreducible x, from f.factors_irred x h1, exact this.1, end lemma degree_factors_prod_eq_degree_factors_sub_erase_dup_add_degree_rad {f : polynomial β} : degree (f.factors.prod) = degree ((f.factors)-(f.factors.erase_dup)).prod + degree (rad f) := begin rw [← sub_erase_dup_add_erase_dup_eq f.factors] {occs := occurrences.pos [1]}, rw [←prod_mul_prod_eq_add_prod], apply degree_mul_eq_add_of_mul_ne_zero, exact mul_ne_zero prod_pow_min_on_ne_zero rad_ne_zero, end lemma ne_zero_of_dvd_ne_zero {γ : Type u}{a b : γ} [comm_semiring γ] (h1 : a ∣ b) (h2 : b ≠ 0) : a ≠ 0 := begin simp only [has_dvd.dvd] at h1, let c := some h1, have h3: b = a * c, from some_spec h1, by_contradiction h4, rw not_not at h4, rw h4 at h3, simp at h3, contradiction end open polynomial --Why here? private lemma Mason_Stothers_lemma_aux_1 (f : polynomial β): ∀x ∈ f.factors, x^(count x f.factors - 1) ∣ d[f.factors.prod] := begin rw [derivative_prod_multiset], intros x h, apply multiset.dvd_sum, intros y hy, rw multiset.mem_map at hy, rcases hy with ⟨z, hz⟩, have : y = d[z] * prod (erase (factors f) z), from hz.2.symm, subst this, apply dvd_mul_of_dvd_right, rcases (exists_cons_of_mem hz.1) with ⟨t, ht⟩, rw ht, by_cases h1 : x = z, { subst h1, simp, apply forall_pow_count_dvd_prod, }, { simp [count_cons_of_ne h1], refine dvd_trans _ (forall_pow_count_dvd_prod t x), apply pow_count_sub_one_dvd_pow_count, }, end private lemma count_factors_sub_one (f x : polynomial β) : (count x f.factors - 1) = count x (factors f - erase_dup (factors f)) := begin rw count_sub, by_cases h1 : x ∈ f.factors, { have : count x (erase_dup (factors f)) = 1, { have h2: 0 < count x (erase_dup (factors f)), { rw [count_pos, mem_erase_dup], exact h1 }, have h3: count x (erase_dup (factors f)) ≤ 1, { have : nodup (erase_dup (factors f)), from nodup_erase_dup _, rw nodup_iff_count_le_one at this, exact this x, }, have : 1 ≤ count x (erase_dup (factors f)), from h2, exact nat.le_antisymm h3 this, }, rw this, }, { rw ←count_eq_zero at h1, simp *, } end private lemma Mason_Stothers_lemma_aux_2 (f : polynomial β) (h_dvd : ∀x ∈ f.factors, x^(count x f.factors - 1) ∣ gcd f d[f]): (f.factors - f.factors.erase_dup).prod ∣ gcd f d[f] := begin apply facs_to_pow_prod_dvd_multiset, intros x h, have h1 : x ∈ f.factors, from mem_factors_of_mem_factors_sub_factors_erase_dup f x h, split, { exact f.factors_irred x h1, }, split, { rw ←count_factors_sub_one, exact h_dvd x h1, }, { intros y hy h2, have : y ∈ f.factors, from mem_factors_of_mem_factors_sub_factors_erase_dup f y hy, have h3: monic x, from f.factors_monic x h1, have h4: monic y, from f.factors_monic y this, rw associated_iff_eq_of_monic_of_monic h3 h4, exact h2, } end private lemma degree_factors_prod (f : polynomial β) (h : f ≠ 0): degree (f.factors.prod) = degree f := begin rw [f.factors_eq] {occs := occurrences.pos [2]}, rw [degree_mul_eq_add_of_mul_ne_zero, degree_C], simp, rw ←f.factors_eq, exact h, end --make private? lemma Mason_Stothers_lemma (f : polynomial β) : degree f ≤ degree (gcd f (derivative f )) + degree (rad f) := --I made degree radical from this one begin by_cases hf : (f = 0), { simp [hf, nat.zero_le]}, { have h_dvd_der : ∀x ∈ f.factors, x^(count x f.factors - 1) ∣ d[f], { rw [f.factors_eq] {occs := occurrences.pos [3]}, rw [derivative_C_mul], intros x h, exact dvd_mul_of_dvd_right (Mason_Stothers_lemma_aux_1 f x h) _, }, have h_dvd_f : ∀x ∈ f.factors, x^(count x f.factors - 1) ∣ f, { rw [f.factors_eq] {occs := occurrences.pos [3]}, intros x hx, --We have intros x hx a lot here, duplicate? apply dvd_mul_of_dvd_right, exact dvd_trans (pow_count_sub_one_dvd_pow_count _ _) (forall_pow_count_dvd_prod _ x), --Duplicate 2 lines with Mason_Stothers_lemma_aux_1 }, have h_dvd_gcd_f_der : ∀x ∈ f.factors, x^(count x f.factors - 1) ∣ gcd f d[f], { intros x hx, exact gcd_min (h_dvd_f x hx) (h_dvd_der x hx), }, have h_prod_dvd_gcd_f_der : (f.factors - f.factors.erase_dup).prod ∣ gcd f d[f], from Mason_Stothers_lemma_aux_2 _ h_dvd_gcd_f_der, have h_gcd : gcd f d[f] ≠ 0, { rw [ne.def, gcd_eq_zero_iff_eq_zero_and_eq_zero], simp [hf] }, have h1 : degree ((f.factors - f.factors.erase_dup).prod) ≤ degree (gcd f d[f]), from degree_dvd h_prod_dvd_gcd_f_der h_gcd, have h2 : degree f = degree ((f.factors)-(f.factors.erase_dup)).prod + degree (rad f), { rw ←degree_factors_prod, exact degree_factors_prod_eq_degree_factors_sub_erase_dup_add_degree_rad, exact hf, }, rw h2, apply add_le_add_right, exact h1, } end lemma Mason_Stothers_lemma' (f : polynomial β) : degree f - degree (gcd f (derivative f )) ≤ degree (rad f) := begin have h1 : degree f - degree (gcd f (derivative f )) ≤ degree (gcd f (derivative f )) + degree (rad f) - degree (gcd f (derivative f )), { apply nat.sub_le_sub_right, apply Mason_Stothers_lemma, }, have h2 : degree (gcd f d[f]) + degree (rad f) - degree (gcd f d[f]) = degree (rad f), { rw [add_comm _ (degree (rad f)), nat.add_sub_assoc, nat.sub_self, nat.add_zero], exact nat.le_refl _, }, rw h2 at h1, exact h1, end lemma eq_zero_of_le_pred {n : ℕ} (h : n ≤ nat.pred n) : n = 0 := begin cases n, simp, simp at h, have h1 : nat.succ n = n, from le_antisymm h (nat.le_succ n), have h2 : nat.succ n ≠ n, from nat.succ_ne_self n, contradiction, end lemma derivative_eq_zero_of_dvd_derivative_self {a : polynomial β} (h : a ∣ d[a]) : d[a] = 0 := begin by_contradiction hc, have h1 : degree d[a] ≤ degree a - 1, from degree_derivative_le, have h2 : degree a ≤ degree d[a], from degree_dvd h hc, have h3 : degree a = 0, { have h3 : degree a ≤ degree a - 1, from le_trans h2 h1, exact eq_zero_of_le_pred h3, }, rw ←is_constant_iff_degree_eq_zero at h3, have h5 : d[a] = 0, from derivative_eq_zero_of_is_constant h3, contradiction, end --In MS detailed I call this zero wronskian lemma derivative_eq_zero_and_derivative_eq_zero_of_coprime_of_wron_eq_zero {a b : polynomial β} (h1 : coprime a b) (h2 : d[a] * b - a * d[b] = 0) : d[a] = 0 ∧ d[b] = 0 := begin have h3 : d[a] * b = a * d[b], { exact calc d[a] * b = d[a] * b + (-a * d[b] + a * d[b]) : by simp ... = d[a] * b - (a * d[b]) + a * d[b] : by simp [add_assoc] ... = 0 + a * d[b] : by rw [h2] ... = _ : by simp }, have h4 : a ∣ d[a] * b, from dvd.intro _ h3.symm, rw mul_comm at h4, have h5 : a ∣ d[a], exact dvd_of_dvd_mul_of_coprime h4 h1, have h6 : d[a] = 0, from derivative_eq_zero_of_dvd_derivative_self h5, --duplication rw mul_comm at h3, have h7 : b ∣ a * d[b], from dvd.intro _ h3, have h8 : b ∣ d[b], exact dvd_of_dvd_mul_of_coprime h7 (h1.symm), have h9 : d[b] = 0, from derivative_eq_zero_of_dvd_derivative_self h8, exact ⟨h6, h9⟩, end --Lemma coprime_gcd in MS_detailed lemma coprime_gcd_derivative_gcd_derivative_of_coprime {a b : polynomial β} (h : coprime a b) (c d : polynomial β) : coprime (gcd a c) (gcd b d) := begin rw coprime, by_contradiction h1, let e := (gcd (gcd a c) (gcd b d)), have ha : e ∣ a, { have h2a: e ∣ (gcd a c), from gcd_left, have h2b : (gcd a c) ∣ a, from gcd_left, exact dvd_trans h2a h2b, }, have hb : e ∣ b, { have h2a: e ∣ (gcd b d), from gcd_right, have h2b : (gcd b d) ∣ b, from gcd_left, exact dvd_trans h2a h2b, }, have he: e ∣ (gcd a b), from gcd_min ha hb, have h2 : ¬is_unit (gcd a b), from not_is_unit_of_not_is_unit_dvd h1 he, rw coprime at h, exact h2 h, end lemma degree_gcd_derivative_le_degree {a : polynomial β}: degree (gcd a d[a]) ≤ degree a := begin by_cases h : (a = 0), { simp * at *, }, { apply degree_gcd_le_left, exact h, } end lemma degree_gcd_derivative_lt_degree_of_degree_ne_zero {a : polynomial β} (h : degree a ≠ 0) (h_char : characteristic_zero β) : degree (gcd a d[a]) < degree a := begin have h1 : degree (gcd a d[a]) ≤ degree d[a], { apply degree_dvd, apply gcd_right, rw [ne.def, derivative_eq_zero_iff_is_constant, is_constant_iff_degree_eq_zero], exact h, exact h_char, }, have h2 : ∃ n, degree a = nat.succ n, from nat.exists_eq_succ_of_ne_zero h, let n := some h2, have h3 : degree a = nat.succ n, from some_spec h2, exact calc degree (gcd a d[a]) ≤ degree d[a] : h1 ... ≤ degree a - 1 : degree_derivative_le ... ≤ nat.succ n - 1 : by rw h3 ... = n : rfl ... < nat.succ n : nat.lt_succ_self _ ... = degree a : eq.symm h3, end open associated lemma not_zero_mem_factors {a : polynomial β} : (0 : polynomial β) ∉ a.factors := begin by_contradiction h1, have : irreducible (0 : polynomial β), from a.factors_irred 0 h1, have : ¬ irreducible (0 : polynomial β), { simp}, contradiction, end lemma c_fac_eq_zero_iff_eq_zero {a : polynomial β} : a.c_fac = 0 ↔ a = 0 := begin split, { intro h, rw a.factors_eq, simp *, }, { intro h, rw [a.factors_eq, mul_eq_zero_iff_eq_zero_or_eq_zero] at h, cases h, { rw C_eq_zero_iff_eq_zero at h, exact h, }, { rw prod_eq_zero_iff_zero_mem' at h, have : (0 : polynomial β) ∉ a.factors, from not_zero_mem_factors, contradiction, } } end --Not used private lemma mk_C_c_fac_eq_one_of_ne_zero {a : polynomial β} (h : a ≠ 0) : mk (C a.c_fac) = 1 := begin rw [ne.def, ←c_fac_eq_zero_iff_eq_zero] at h, have h1 : is_unit a.c_fac, from for_all_is_unit_of_not_zero h, have h2 : is_unit (C a.c_fac), from is_unit_of_is_unit h1, rw mk_eq_one_iff_is_unit, exact h2, end --a and b can be implicit lemma factors_inter_factors_eq_zero_of_coprime (a b : polynomial β) (h : coprime a b) : a.factors ∩ b.factors = 0 := begin by_cases ha: a = 0, { subst ha, simp, }, by_cases hb: b = 0, { subst hb, simp, }, --I already have a similar lemma for coprime rw [coprime_iff_mk_inf_mk_eq_one, a.factors_eq, b.factors_eq, mul_mk, mul_mk] at h, --we go to the quotient structure with respect to units. rw inf_mul_eq_one_iff_inf_eq_one_and_inf_eq_one at h, --We probably can do without going to the quotient, because we have the lemmas for coprime let h1 := h.2, rw mul_inf_eq_one_iff_inf_eq_one_and_inf_eq_one at h1, let h2 := h1.2, rw [mk_def, mk_def, ←prod_mk, ←prod_mk] at h2, rw prod_inf_prod_eq_one_iff_forall_forall_inf_eq_one at h2, rw [inter_eq_zero_iff_disjoint, disjoint_iff_ne], { intros x hx y hy, have h3 : mk x ⊓ mk y = 1, { apply h2, { rw mem_map, exact ⟨x, hx, rfl⟩, }, { rw mem_map, exact ⟨y, hy, rfl⟩, }, }, rw [←coprime_iff_mk_inf_mk_eq_one, coprime_iff_not_associated_of_irreducible_of_irreducible] at h3, rw [associated_iff_eq_of_monic_of_monic] at h3, exact h3, exact a.factors_monic x hx, exact b.factors_monic y hy, exact a.factors_irred x hx, exact b.factors_irred y hy, } end open associated private lemma monic_prod_factors (a : polynomial β) : monic a.factors.prod := monic_prod_of_forall_mem_monic a.factors_monic lemma c_fac_eq_leading_coeff (a : polynomial β) : a.c_fac = leading_coeff a := begin by_cases ha : a = 0, { simp * at *, }, { have h1 : a = (C a.c_fac) * a.factors.prod, from a.factors_eq, rw [h1] {occs := occurrences.pos[2]}, rw leading_coeff_mul_eq_mul_leading_coef, let h3 := monic_prod_factors a, rw monic at h3, simp [h3], } end private lemma C_inv_c_fac_mul_eq_prod_factors {a : polynomial β} (ha : a ≠ 0): (C a.c_fac⁻¹) * a = a.factors.prod := begin have h1 : a = (C a.c_fac) * a.factors.prod, from a.factors_eq, rw [ne.def, ←c_fac_eq_zero_iff_eq_zero] at ha, exact calc (C a.c_fac⁻¹) * a = (C a.c_fac⁻¹) * (C (c_fac a) * prod (factors a)) : by rw [←h1] ... = _ : by rw [←mul_assoc, ←C_mul_C, inv_mul_cancel ha, C_1, one_mul], end lemma factors_eq_map_monic_out_to_multiset_mk (a : polynomial β) : a.factors = map monic_out (associated.to_multiset (associated.mk a)) := begin by_cases ha : a = 0, { simp * at *, }, { have h1 : a = C (a.c_fac) * a.factors.prod, from a.factors_eq, have h2 : C (a.c_fac⁻¹) * a = a.factors.prod, from C_inv_c_fac_mul_eq_prod_factors ha, have h3: a.factors.prod = (map monic_out (to_multiset (mk a))).prod, { rw prod_map_monic_out_eq_monic_out_prod, rw ←h2, rw ←associated_iff_eq_of_monic_of_monic, rw to_multiset_prod_eq, have h4 : (a ~ᵤ (monic_out (mk a))), from (monic_out_mk_associated a).symm, --It can no longer elaborate it?? After I refactored all the files have h5 : (C (c_fac a)⁻¹ * a ~ᵤ a), { have h5a : (c_fac a)⁻¹ ≠ 0, { rw ←c_fac_eq_zero_iff_eq_zero at ha, apply inv_ne_zero, exact ha, }, have h5b : (C (c_fac a)⁻¹) ≠ 0, --Is this used? { rw [ne.def, C_eq_zero_iff_eq_zero], exact h5a, }, have h5c: is_unit (C (c_fac a)⁻¹), from is_unit_C_of_ne_zero h5a, rcases h5c with ⟨u, hu⟩, exact ⟨u, by simp *⟩, }, exact associated.trans h5 h4, rwa [ne.def, mk_eq_zero_iff_eq_zero], { rw c_fac_eq_leading_coeff, exact leading_coeff_inv_mul_monic_of_ne_zero ha, }, { apply monic_monic_out_of_ne_zero, rw [to_multiset_prod_eq],--Wrong naminG!! rw ←mk_eq_zero_iff_eq_zero at ha, --Duplicate exact ha, rw ←mk_eq_zero_iff_eq_zero at ha, exact ha, } }, have h4a: ∀ (x : ~β), x ∈ map monic_out (to_multiset (mk a)) → irreducible x, { intros x h, rw mem_map at h, rcases h with ⟨y, hy⟩, rw hy.2.symm, apply irreducible_monic_out_of_irred, apply to_multiset_irred _ y hy.1, }, have h4 : rel_multiset associated a.factors (map monic_out (to_multiset (mk a))), from unique_factorization_domain.unique a.factors_irred h4a h3, apply eq_of_rel_multiset_associated_of_forall_mem_monic_of_forall_mem_monic, exact h4, exact a.factors_monic, { intros x h, rw mem_map at h, rcases h with ⟨y, hy⟩, rw hy.2.symm, apply monic_monic_out_of_ne_zero, { have : irred y, from to_multiset_irred _ y hy.1, exact ne_zero_of_irred this, } } } end --aux --I already have to_multiset_mul, how can I reuse that here?? How to make the connection between monic and associated?? lemma factors_mul_eq_factors_add_factors {a b : polynomial β} (ha : a ≠ 0) (hb : b ≠ 0) : factors (a * b) = a.factors + b.factors := begin rw [factors_eq_map_monic_out_to_multiset_mk, factors_eq_map_monic_out_to_multiset_mk, factors_eq_map_monic_out_to_multiset_mk], rw [mul_mk, to_multiset_mul], simp, simp [mk_eq_zero_iff_eq_zero, *], simp [mk_eq_zero_iff_eq_zero, *], end @[simp] lemma rad_zero : rad (0 : polynomial β ) = 1 := begin simp [rad], end lemma rad_mul_eq_rad_mul_rad_of_coprime {a b : polynomial β} (ha : a ≠ 0) (hb : b ≠ 0) (h : coprime a b) : rad (a * b) = (rad a) * (rad b) := begin simp only [rad], rw prod_mul_prod_eq_add_prod, apply congr_arg, rw factors_mul_eq_factors_add_factors ha hb, --Might be good to go to nodup rw multiset.ext, intros x, by_cases h1 : x ∈ (a.factors + b.factors), { have h2 : count x (erase_dup (factors a + factors b)) = 1, { apply count_eq_one_of_mem, exact nodup_erase_dup _, rwa mem_erase_dup, }, rw mem_add at h1, have h3 : x ∈ a.factors ∩ b.factors ↔ x ∈ a.factors ∧ x ∈ b.factors, from mem_inter, have h4: a.factors ∩ b.factors = 0, from factors_inter_factors_eq_zero_of_coprime _ _ h, cases h1, { have : x ∉ b.factors, { simp * at *, }, rw ←mem_erase_dup at h1, have h1a : count x (erase_dup (factors a)) = 1, from count_eq_one_of_mem (nodup_erase_dup _) h1, rw ←mem_erase_dup at this, have h1b : count x (erase_dup (factors b)) = 0, { rwa count_eq_zero, }, rw count_add, simp * at *, }, { --Strong duplication have : x ∉ a.factors, { simp * at *, }, rw ←mem_erase_dup at h1, have h1a : count x (erase_dup (factors b)) = 1, from count_eq_one_of_mem (nodup_erase_dup _) h1, rw ←mem_erase_dup at this, have h1b : count x (erase_dup (factors a)) = 0, { rwa count_eq_zero, }, rw count_add, simp * at *, } }, { have h2 : count x (erase_dup (factors a + factors b)) = 0, { rwa [count_eq_zero, mem_erase_dup], }, rw [mem_add, not_or_distrib] at h1, have h3 : count x (erase_dup (factors a)) = 0, { rw [count_eq_zero, mem_erase_dup], exact h1.1, }, have h4 : count x (erase_dup (factors b)) = 0, { rw [count_eq_zero, mem_erase_dup], exact h1.2, }, simp * at *, }, end --We will need extra conditions here --We only need this one lemma degree_rad_add {a b: polynomial β} (ha : a ≠ 0) (hb : b ≠ 0) (hab : coprime a b): degree (rad a) + degree (rad b) ≤ degree (rad (a * b)) := begin rw rad_mul_eq_rad_mul_rad_of_coprime ha hb hab, rw degree_mul_eq_add_of_mul_ne_zero, apply _root_.mul_ne_zero, { apply multiset.prod_ne_zero_of_forall_mem_ne_zero, intros x h, rw mem_erase_dup at h, exact (a.factors_irred _ h).1 --Anoying setup for factors_irred }, { apply multiset.prod_ne_zero_of_forall_mem_ne_zero, intros x h, rw mem_erase_dup at h, exact (b.factors_irred _ h).1 --Anoying setup for factors_irred }, end private lemma h_rad_add {a b c: polynomial β} (ha : a ≠ 0) (hb : b ≠ 0) (hc : c ≠ 0) (hab : coprime a b) (h_coprime_bc : coprime b c) (h_coprime_ca : coprime c a) : degree(rad(a))+degree(rad(b))+degree(rad(c)) ≤ degree(rad(a*b*c)) := begin have habc : coprime (a*b) c, { have h1: ((gcd (a * b) c) ~ᵤ (gcd b c)), from gcd_mul_cancel h_coprime_ca.symm, exact is_unit_of_associated h_coprime_bc h1.symm, }, have h1 : a * b ≠ 0, from mul_ne_zero ha hb, have h3 : degree (rad a) + degree (rad b) ≤ degree (rad (a * b)), from degree_rad_add ha hb hab, exact calc degree(rad(a))+degree(rad(b))+degree(rad(c)) ≤ degree (rad (a * b)) + degree (rad c) : add_le_add_right h3 _ ... ≤ _ : degree_rad_add h1 hc habc end lemma gt_zero_of_ne_zero {n : ℕ} (h : n ≠ 0) : n > 0 := begin have h1 : ∃ m : ℕ, n = nat.succ m, from nat.exists_eq_succ_of_ne_zero h, let m := some h1, have h2 : n = nat.succ m, from some_spec h1, rw h2, exact nat.zero_lt_succ _, end lemma MS_aux_1a {c : polynomial β} (h3 : ¬degree c = 0)(h_char : characteristic_zero β) : 1 ≤ (degree c - degree (gcd c d[c])) := begin have h4 : degree c - degree (gcd c d[c]) > 0, { rw [nat.pos_iff_ne_zero, ne.def, nat.sub_eq_zero_iff_le], simp, exact degree_gcd_derivative_lt_degree_of_degree_ne_zero h3 h_char, }, exact h4, end --should be in poly lemma MS_aux_1b {a b c : polynomial β} (h_char : characteristic_zero β) (h_add : a + b = c) (h_constant : ¬(is_constant a ∧ is_constant b ∧ is_constant c)) (h1 : is_constant b) (h2 : ¬is_constant a) : ¬ is_constant c := begin rw [is_constant_iff_degree_eq_zero] at *, have h3 : c (degree a) ≠ 0, { rw ← h_add, simp, have h3 : b (degree a) = 0, { apply eq_zero_of_gt_degree, rw h1, exact gt_zero_of_ne_zero h2, }, rw h3, simp, have h4 : leading_coeff a = 0 ↔ a = 0, from leading_coef_eq_zero_iff_eq_zero, rw leading_coeff at h4, rw h4, by_contradiction h5, rw h5 at h2, simp at h2, exact h2, }, have h4 : degree a ≤ degree c, from le_degree h3, by_contradiction h5, rw h5 at h4, have : degree a = 0, from nat.eq_zero_of_le_zero h4, contradiction, end lemma MS_aux_1 {a b c : polynomial β} (h_char : characteristic_zero β) (h_add : a + b = c) (h_constant : ¬(is_constant a ∧ is_constant b ∧ is_constant c)) : 1 ≤ degree b - degree (gcd b d[b]) + (degree c - degree (gcd c d[c])) := begin by_cases h1 : (is_constant b), { by_cases h2 : (is_constant a), { have h3 : is_constant c, from is_constant_add h2 h1 h_add, simp * at *, }, { have h3 : ¬ is_constant c, { exact MS_aux_1b h_char h_add h_constant h1 h2, }, rw [is_constant_iff_degree_eq_zero] at h3, have h4 : 1 ≤ (degree c - degree (gcd c d[c])), from MS_aux_1a h3 h_char, apply nat.le_trans h4, simp, exact nat.zero_le _, } }, { rw [is_constant_iff_degree_eq_zero] at h1, have h2 : 1 ≤ degree b - degree (gcd b d[b]), from MS_aux_1a h1 h_char, apply nat.le_trans h2, simp, exact nat.zero_le _, } end ---Very likely that the lemmas on rad are overcomplicated, they should use erase_dup_more lemma dvd_of_mem_factors (f : polynomial β) (x : polynomial β) (h : x ∈ f.factors) : x ∣ f := begin rw f.factors_eq, have : x ∣ f.factors.prod, from dvd_prod_of_mem f.factors h, exact dvd_mul_of_dvd_right this _, end lemma not_is_unit_prod_factors_of_factors_ne_zero (a : polynomial β) (h : a.factors ≠ 0) : ¬ is_unit (a.factors.prod) := begin exact not_is_unit_prod_of_ne_zero_of_forall_mem_irreducible h a.factors_irred, end @[simp] lemma factors_C (c : β) : (C c).factors = 0 := begin by_cases h1 : C c = 0, { simp *, }, { by_contradiction h2, rcases (exists_mem_of_ne_zero h2) with ⟨x, hx⟩, have h2 : x ∣ C c, --The part below could be a separate lemma { exact dvd_of_mem_factors (C c) x hx, }, have h3: irreducible x, from (C c).factors_irred x hx, have h4: ¬is_unit x, from not_is_unit_of_irreducible h3, have h5: ¬is_unit (C c), from not_is_unit_of_not_is_unit_dvd h4 h2, have h6 : is_constant (C c), {simp}, rw is_constant_iff_eq_zero_or_is_unit at h6, simp * at *, } end lemma is_constant_iff_factors_eq_zero (a : polynomial β) : is_constant a ↔ a.factors = 0 := begin split, { intro h, rcases h with ⟨c, hc⟩, subst hc, simp, }, { intro h, rw a.factors_eq, simp * at *, } end --lemma factors_eq_zero_iff_rad_eq_zero (a : polynomial β) lemma rad_dvd_prod_factors (a : polynomial β) : (rad a) ∣ a.factors.prod := begin rw rad, apply prod_dvd_prod_of_le, exact erase_dup_le _, end lemma rad_dvd (a : polynomial β) : (rad a) ∣ a := begin rw [a.factors_eq] {occs := occurrences.pos [2]}, apply dvd_mul_of_dvd_right, exact rad_dvd_prod_factors _, end lemma degree_rad_le (a : polynomial β) : degree (rad a) ≤ degree a := begin by_cases h1 : a = 0, { subst h1, simp, }, { apply degree_dvd, exact rad_dvd _, exact h1, } end lemma is_unit_rad_iff_factors_eq_zero (a : polynomial β) : is_unit (rad a) ↔ a.factors = 0 := begin split, { intro h, rw ←erase_dup_eq_zero_iff_eq_zero, by_contradiction h1, have : ¬is_unit (rad a), { apply not_is_unit_prod_of_ne_zero_of_forall_mem_irreducible h1, intros x h, rw mem_erase_dup at h, exact a.factors_irred x h, }, simp * at *, }, { intro h, rw ←erase_dup_eq_zero_iff_eq_zero at h, rw rad, simp *, } end lemma degree_rad_eq_zero_iff_is_constant (a : polynomial β) : degree (rad a) = 0 ↔ is_constant a := begin split, { intro h, rw is_constant_iff_factors_eq_zero, rw [←is_constant_iff_degree_eq_zero, is_constant_iff_eq_zero_or_is_unit] at h, cases h, { have : rad a ≠ 0, from rad_ne_zero, contradiction, }, { rw ←is_unit_rad_iff_factors_eq_zero, exact h, }, }, { intro h, rw is_constant_iff_factors_eq_zero at h, apply degree_eq_zero_of_is_unit, rw is_unit_rad_iff_factors_eq_zero, exact h, } end --Strong duplication with MS_aux_1 lemma MS_aux_2 {a b c : polynomial β} (h_char : characteristic_zero β) (h_add : a + b = c) (h_constant : ¬(is_constant a ∧ is_constant b ∧ is_constant c)) : 1 ≤ degree (rad a) + (degree c - degree (gcd c d[c])) := begin by_cases h1 : is_constant b, { by_cases h2 : is_constant a, { have h3 : is_constant c, from is_constant_add h2 h1 h_add, simp * at *, }, { have h3 : ¬ is_constant c, { rw [is_constant_iff_degree_eq_zero] at *, have h3 : c (degree a) ≠ 0, { rw ← h_add, simp, have h3 : b (degree a) = 0, { apply eq_zero_of_gt_degree, rw h1, exact gt_zero_of_ne_zero h2, }, rw h3, simp, have h4 : leading_coeff a = 0 ↔ a = 0, from leading_coef_eq_zero_iff_eq_zero, rw leading_coeff at h4, rw h4, by_contradiction h5, rw h5 at h2, simp at h2, exact h2, }, have h4 : degree a ≤ degree c, from le_degree h3, by_contradiction h5, rw h5 at h4, have : degree a = 0, from nat.eq_zero_of_le_zero h4, contradiction, }, rw [is_constant_iff_degree_eq_zero] at h3, have h4 : 1 ≤ (degree c - degree (gcd c d[c])), from MS_aux_1a h3 h_char, apply nat.le_trans h4, simp, exact nat.zero_le _, } }, { by_cases h2 : (is_constant a), { rw add_comm at h_add, have h_constant' : ¬(is_constant b ∧ is_constant a ∧ is_constant c), {simp [*, and_comm]}, have h3 : ¬is_constant c, from MS_aux_1b h_char h_add h_constant' h2 h1, rw [is_constant_iff_degree_eq_zero] at h3, have h4 : 1 ≤ (degree c - degree (gcd c d[c])), from MS_aux_1a h3 h_char, apply nat.le_trans h4, simp, exact nat.zero_le _, }, { have : degree a ≠ 0, { rw [ne.def, ←is_constant_iff_degree_eq_zero], exact h2, }, rw [ne.def, ←is_constant_iff_degree_eq_zero, ←degree_rad_eq_zero_iff_is_constant] at this, have h3: 1 ≤ degree (rad a), { rcases (nat.exists_eq_succ_of_ne_zero this) with ⟨n, hn⟩, simp * at *, rw nat.succ_le_succ_iff, exact nat.zero_le _, }, apply nat.le_trans h3, exact nat.le_add_right _ _, } } end private lemma rw_aux_1a [field β] (a b c : polynomial β) (h_add : a + b = c) (h_constant : ¬(is_constant a ∧ is_constant b ∧ is_constant c)) : degree a ≠ 0 ∨ degree b ≠ 0 := begin rw [not_and_distrib, not_and_distrib] at h_constant, cases h_constant, { rw is_constant_iff_degree_eq_zero at h_constant, simp *, }, { cases h_constant, { rw is_constant_iff_degree_eq_zero at h_constant, simp *, }, { rw is_constant_iff_degree_eq_zero at h_constant, have : degree (a + b) ≤ max (degree a) (degree b), from degree_add, simp * at *, rw le_max_iff at this, rcases (nat.exists_eq_succ_of_ne_zero h_constant) with ⟨n, hn⟩, simp * at *, cases this with h, { have : ¬degree a = 0, { by_contradiction h1, simp * at *, exact nat.not_succ_le_zero _ h, }, simp *, }, { have : ¬degree b = 0, { by_contradiction h1, simp * at *, exact nat.not_succ_le_zero _ this, }, simp *, } } } end private lemma rw_aux_1b [field β] (a b c : polynomial β) (h_add : a + b = c) (h_constant : ¬(is_constant a ∧ is_constant b ∧ is_constant c)) : 1 ≤ degree a + degree b := begin have : degree a ≠ 0 ∨ degree b ≠ 0, from rw_aux_1a a b c h_add h_constant, cases this with h h ; { rcases (nat.exists_eq_succ_of_ne_zero h) with ⟨n, hn⟩, simp * at *, have : 1 ≤ nat.succ n, { rw nat.succ_le_succ_iff, exact nat.zero_le _, }, apply nat.le_trans this, apply nat.le_add_right, }, end --h_deg_c_le_1 lemma rw_aux_1 [field β] --(h_char : characteristic_zero β) (a b c : polynomial β) --(h_coprime_ab : coprime a b) --(h_coprime_bc : coprime b c) --(h_coprime_ca : coprime c a) (h_add : a + b = c) (h_constant : ¬(is_constant a ∧ is_constant b ∧ is_constant c)) (h_deg_add_le : degree (gcd a d[a]) + degree (gcd b d[b]) + degree (gcd c d[c]) ≤ degree a + degree b - 1) : degree c ≤ (degree a - degree (gcd a d[a])) + (degree b - degree (gcd b d[b])) + (degree c - degree (gcd c d[c])) - 1 := have 1 ≤ degree a + degree b, from rw_aux_1b a b c h_add h_constant, have h : ∀p:polynomial β, degree (gcd p d[p]) ≤ degree p, from (λ p, @degree_gcd_derivative_le_degree _ _ p), have (degree (gcd a d[a]) : ℤ) + (degree (gcd b d[b]) : ℤ) + (degree (gcd c d[c]) : ℤ) ≤ (degree a : ℤ) + (degree b : ℤ) - 1, by rwa [← int.coe_nat_add, ← int.coe_nat_add, ← int.coe_nat_add, ← int.coe_nat_one, ← int.coe_nat_sub this, int.coe_nat_le], have (degree c : ℤ) ≤ ((degree a : ℤ) - (degree (gcd a d[a]) : ℤ)) + ((degree b : ℤ) - (degree (gcd b d[b]) : ℤ)) + ((degree c : ℤ) - (degree (gcd c d[c]) : ℤ)) - 1, from calc (degree c : ℤ) ≤ ((degree c : ℤ) + ((degree a : ℤ) + (degree b : ℤ) - 1)) - ((degree (gcd a d[a]) : ℤ) + (degree (gcd b d[b]) : ℤ) + (degree (gcd c d[c]) : ℤ)) : le_sub_iff_add_le.mpr $ add_le_add_left this _ ... = _ : by simp, have 1 + (degree c : ℤ) ≤ ((degree a : ℤ) - (degree (gcd a d[a]) : ℤ)) + ((degree b : ℤ) - (degree (gcd b d[b]) : ℤ)) + ((degree c : ℤ) - (degree (gcd c d[c]) : ℤ)), from add_le_of_le_sub_left this, nat.le_sub_left_of_add_le $ by rwa [← int.coe_nat_sub (h _), ← int.coe_nat_sub (h _), ← int.coe_nat_sub (h _), ← int.coe_nat_add, ← int.coe_nat_add, ← int.coe_nat_one, ← int.coe_nat_add, int.coe_nat_le] at this /- lemma Mason_Stothers_lemma (f : polynomial β) : degree f ≤ degree (gcd f (derivative f )) + degree (rad f) -/ /- lemma Mason_Stothers_lemma' (f : polynomial β) : degree f - degree (gcd f (derivative f )) ≤ degree (rad f) := -/ /- --We will need extra conditions here lemma degree_rad_add {a b c : polynomial β}: degree (rad a) + degree (rad b) + degree (rad c) ≤ degree (rad (a * b * c)) := begin admit, end-/ lemma nat.add_mono{a b c d : ℕ} (hab : a ≤ b) (hcd : c ≤ d) : a + c ≤ b + d := begin exact calc a + c ≤ a + d : nat.add_le_add_left hcd _ ... ≤ b + d : nat.add_le_add_right hab _ end --h_le_rad lemma rw_aux_2 [field β] --We want to use the Mason Stothers lemmas here {a b c : polynomial β} (ha : a ≠ 0) (hb : b ≠ 0) (hc : c ≠ 0) (h_coprime_ab : coprime a b) (h_coprime_bc : coprime b c) (h_coprime_ca : coprime c a) : degree a - degree (gcd a d[a]) + (degree b - degree (gcd b d[b])) + (degree c - degree (gcd c d[c])) - 1 ≤ degree (rad (a * b * c)) - 1:= begin apply nat.sub_le_sub_right, have h_rad: degree(rad(a))+degree(rad(b))+degree(rad(c)) ≤ degree(rad(a*b*c)), from h_rad_add ha hb hc h_coprime_ab h_coprime_bc h_coprime_ca, refine nat.le_trans _ h_rad, apply nat.add_mono _ (Mason_Stothers_lemma' c), apply nat.add_mono (Mason_Stothers_lemma' a) (Mason_Stothers_lemma' b), end private lemma h_dvd_wron_a (a b c : polynomial β): gcd a d[a] ∣ d[a] * b - a * d[b] := begin have h1 : gcd a d[a] ∣ d[a] * b, { apply dvd_trans gcd_right, apply dvd_mul_of_dvd_left, simp }, have h2 : gcd a d[a] ∣ a * d[b], { apply dvd_trans gcd_left, apply dvd_mul_of_dvd_left, simp }, exact dvd_sub h1 h2, end private lemma h_dvd_wron_b (a b c : polynomial β): gcd b d[b] ∣ d[a] * b - a * d[b] := begin have h1 : gcd b d[b] ∣ d[a] * b, { apply dvd_trans gcd_left, apply dvd_mul_of_dvd_right, simp }, have h2 : gcd b d[b] ∣ a * d[b], { apply dvd_trans gcd_right, apply dvd_mul_of_dvd_right, simp }, exact dvd_sub h1 h2, end private lemma h_dvd_wron_c (a b c : polynomial β) (h_wron : d[a] * b - a * d[b] = d[a] * c - a * d[c]) : gcd c d[c] ∣ d[a] * b - a * d[b] := begin rw h_wron, have h1 : gcd c d[c] ∣ a * d[c], { apply dvd_trans gcd_right, apply dvd_mul_of_dvd_right, simp }, have h2 : gcd c d[c] ∣ d[a] * c, { apply dvd_trans gcd_left, apply dvd_mul_of_dvd_right, simp }, exact dvd_sub h2 h1, end private lemma one_le_of_ne_zero {n : ℕ } (h : n ≠ 0) : 1 ≤ n := begin let m := some (nat.exists_eq_succ_of_ne_zero h), have h1 : n = nat.succ m, from some_spec (nat.exists_eq_succ_of_ne_zero h), rw [h1, nat.succ_le_succ_iff], exact nat.zero_le _, end --Todo can we remove the a = 0, b = 0 cases? lemma degree_wron_le {a b : polynomial β} : degree (d[a] * b - a * d[b]) ≤ degree a + degree b - 1 := begin by_cases h1 : (a = 0), { simp *, exact nat.zero_le _, }, { by_cases h2 : (degree a = 0), { by_cases h3 : (b = 0), { rw h3, simp, exact nat.zero_le _, }, { simp [*], by_cases h4 : (degree b = 0), { simp *, rw [←is_constant_iff_degree_eq_zero] at *, have h5 : derivative a = 0, from derivative_eq_zero_of_is_constant h2, have h6 : derivative b = 0, from derivative_eq_zero_of_is_constant h4, simp *, }, { have h2a : degree a = 0, from h2, rw [←is_constant_iff_degree_eq_zero] at h2, have h5 : derivative a = 0, from derivative_eq_zero_of_is_constant h2, simp *, by_cases h6 : (derivative b = 0), { simp *, exact nat.zero_le _, }, { --rw [degree_neg], apply nat.le_trans degree_mul, simp *, exact degree_derivative_le, } }, } }, { by_cases h3 : (b = 0), { simp *, exact nat.zero_le _, }, { by_cases h4 : (degree b = 0), { simp *, rw [←is_constant_iff_degree_eq_zero] at h4, have h5 : derivative b = 0, from derivative_eq_zero_of_is_constant h4, simp *, apply nat.le_trans degree_mul, rw [is_constant_iff_degree_eq_zero] at h4, simp *, exact degree_derivative_le, }, { apply nat.le_trans degree_sub, have h5 : degree (d[a] * b) ≤ degree a + degree b - 1, { apply nat.le_trans degree_mul, rw [add_comm _ (degree b), add_comm _ (degree b), nat.add_sub_assoc], apply add_le_add_left, exact degree_derivative_le, exact polynomial.one_le_of_ne_zero h2, --Can I remove this from polynomial?? }, have h6 : (degree (a * d[b])) ≤ degree a + degree b - 1, { apply nat.le_trans degree_mul, rw [nat.add_sub_assoc], apply add_le_add_left, exact degree_derivative_le, exact polynomial.one_le_of_ne_zero h4, }, exact max_le h5 h6, } } } } end lemma degree_wron_le2 {a b : polynomial β} : degree (d[a] * b - a * d[b]) ≤ degree a + degree b - 1 := begin by_cases h2 : (degree a = 0), { simp [is_constant_iff_degree_eq_zero, derivative_eq_zero_of_is_constant, *] at *, by_cases h6 : (derivative b = 0), { simp [nat.zero_le, *]}, { apply nat.le_trans degree_mul, simp [degree_derivative_le, *], } }, { by_cases h4 : (degree b = 0), --This case distinction shouldnn't be needed { simp [is_constant_iff_degree_eq_zero, derivative_eq_zero_of_is_constant, *] at *, apply nat.le_trans degree_mul, simp [degree_derivative_le, *], }, { apply nat.le_trans degree_sub, have h5 : degree (d[a] * b) ≤ degree a + degree b - 1, { apply nat.le_trans degree_mul, rw [add_comm _ (degree b), add_comm _ (degree b), nat.add_sub_assoc (polynomial.one_le_of_ne_zero h2)], apply add_le_add_left degree_derivative_le, }, have h6 : (degree (a * d[b])) ≤ degree a + degree b - 1, { apply nat.le_trans degree_mul, rw [nat.add_sub_assoc (polynomial.one_le_of_ne_zero h4)], apply add_le_add_left degree_derivative_le, }, exact max_le h5 h6, } } end private lemma h_wron_ne_zero (a b c : polynomial β) (h_coprime_ab : coprime a b) (h_coprime_ca : coprime c a) (h_der_not_all_zero : ¬(d[a] = 0 ∧ d[b] = 0 ∧ d[c] = 0)) (h_wron : d[a] * b - a * d[b] = d[a] * c - a * d[c]): d[a] * b - a * d[b] ≠ 0 := begin by_contradiction h1, rw not_not at h1, have h_a_b : d[a] = 0 ∧ d[b] = 0, from derivative_eq_zero_and_derivative_eq_zero_of_coprime_of_wron_eq_zero h_coprime_ab h1, have h2 : d[a] * c - a * d[c] = 0, {rw [←h_wron, h1]}, have h_a_c : d[a] = 0 ∧ d[c] = 0, from derivative_eq_zero_and_derivative_eq_zero_of_coprime_of_wron_eq_zero (h_coprime_ca.symm) h2, have h3 : (d[a] = 0 ∧ d[b] = 0 ∧ d[c] = 0), exact ⟨and.elim_left h_a_b, and.elim_right h_a_b, and.elim_right h_a_c⟩, contradiction end private lemma h_deg_add (a b c : polynomial β) (h_wron_ne_zero : d[a] * b - a * d[b] ≠ 0) (h_gcds_dvd : (gcd a d[a]) * (gcd b d[b]) * (gcd c d[c]) ∣ d[a] * b - a * d[b]): degree (gcd a d[a] * gcd b d[b] * gcd c d[c]) = degree (gcd a d[a]) + degree (gcd b d[b]) + degree (gcd c d[c]) := begin have h1 : gcd a d[a] * gcd b d[b] * gcd c d[c] ≠ 0, from ne_zero_of_dvd_ne_zero h_gcds_dvd h_wron_ne_zero, have h2 : degree (gcd a d[a] * gcd b d[b] * gcd c d[c]) = degree (gcd a d[a] * gcd b d[b]) + degree (gcd c d[c]), from degree_mul_eq_add_of_mul_ne_zero h1, have h3 : gcd a d[a] * gcd b d[b] ≠ 0, from ne_zero_of_mul_ne_zero_right h1, have h4 : degree (gcd a d[a] * gcd b d[b]) = degree (gcd a d[a]) + degree (gcd b d[b]), from degree_mul_eq_add_of_mul_ne_zero h3, rw [h2, h4], end private lemma h_wron (a b c : polynomial β) (h_add : a + b = c) (h_der : d[a] + d[b] = d[c]) : d[a] * b - a * d[b] = d[a] * c - a * d[c] := begin have h1 : d[a] * a + d[a] * b = d[a] * c, exact calc d[a] * a + d[a] * b = d[a] * (a + b) : by rw [mul_add] ... = _ : by rw h_add, have h2 : a * d[a] + a * d[b] = a * d[c], exact calc a * d[a] + a * d[b] = a * (d[a] + d[b]) : by rw [mul_add] ... = _ : by rw h_der, have h3 : d[a] * b - a * d[b] = d[a] * c - a * d[c], exact calc d[a] * b - a * d[b] = d[a] * b + (d[a] * a - d[a] * a) - a * d[b] : by simp ... = d[a] * b + d[a] * a - d[a] * a - a * d[b] : by simp ... = d[a] * c - (d[a] * a + a * d[b]) : by simp [h1] ... = d[a] * c - (a * d[a] + a * d[b]) : by rw [mul_comm _ a] ... = _ : by rw h2, exact h3 end private lemma h_gcds_dvd (a b c : polynomial β) (h_coprime_ab : coprime a b) (h_coprime_bc : coprime b c) (h_coprime_ca : coprime c a) (h_dvd_wron_a : gcd a d[a] ∣ d[a] * b - a * d[b]) (h_dvd_wron_b : gcd b d[b] ∣ d[a] * b - a * d[b]) (h_dvd_wron_c : gcd c d[c] ∣ d[a] * b - a * d[b]): (gcd a d[a]) * (gcd b d[b]) * (gcd c d[c]) ∣ d[a] * b - a * d[b] := begin apply mul_dvd_of_dvd_of_dvd_of_coprime, apply coprime_mul_of_coprime_of_coprime_of_coprime, exact coprime_gcd_derivative_gcd_derivative_of_coprime (h_coprime_ca.symm) _ _, exact coprime_gcd_derivative_gcd_derivative_of_coprime h_coprime_bc _ _, apply mul_dvd_of_dvd_of_dvd_of_coprime, exact coprime_gcd_derivative_gcd_derivative_of_coprime h_coprime_ab _ _, exact h_dvd_wron_a, exact h_dvd_wron_b, exact h_dvd_wron_c end private lemma degree_rad_pos (a b c : polynomial β) (ha : a ≠ 0) (hb : b ≠ 0) (hc : c ≠ 0) (h_constant : ¬(is_constant a ∧ is_constant b ∧ is_constant c)) : degree (rad (a*b*c)) > 0 := begin apply gt_zero_of_ne_zero, rw [ne.def, degree_rad_eq_zero_iff_is_constant], simp only [not_and_distrib] at *, have : a * b ≠ 0, from mul_ne_zero ha hb, cases h_constant, { simp [not_is_constant_mul_of_not_is_constant_of_ne_zero, *]}, cases h_constant, { simp [mul_comm a, not_is_constant_mul_of_not_is_constant_of_ne_zero, *]}, { simp [mul_comm _ c, not_is_constant_mul_of_not_is_constant_of_ne_zero, *]} end theorem Mason_Stothers_special [field β] (h_char : characteristic_zero β) (a b c : polynomial β) (ha : a ≠ 0) (hb : b ≠ 0) (hc : c ≠ 0) (h_coprime_ab : coprime a b) (h_coprime_bc : coprime b c) (h_coprime_ca : coprime c a) (h_add : a + b = c) (h_constant : ¬(is_constant a ∧ is_constant b ∧ is_constant c)) : degree c < degree ( rad (a*b*c)) := begin have h_der_not_all_zero : ¬(d[a] = 0 ∧ d[b] = 0 ∧ d[c] = 0), { rw [derivative_eq_zero_iff_is_constant h_char, derivative_eq_zero_iff_is_constant h_char, derivative_eq_zero_iff_is_constant h_char], exact h_constant, }, have h_der : d[a] + d[b] = d[c], { rw [←h_add, derivative_add], }, have h_wron : d[a] * b - a * d[b] = d[a] * c - a * d[c], from h_wron a b c h_add h_der, have h_dvd_wron_a : gcd a d[a] ∣ d[a] * b - a * d[b], from h_dvd_wron_a a b c, have h_dvd_wron_b : gcd b d[b] ∣ d[a] * b - a * d[b], from h_dvd_wron_b a b c, have h_dvd_wron_c : gcd c d[c] ∣ d[a] * b - a * d[b], from h_dvd_wron_c a b c h_wron, have h_gcds_dvd : (gcd a d[a]) * (gcd b d[b]) * (gcd c d[c]) ∣ d[a] * b - a * d[b], from h_gcds_dvd a b c h_coprime_ab h_coprime_bc h_coprime_ca h_dvd_wron_a h_dvd_wron_b h_dvd_wron_c, have h_wron_ne_zero : d[a] * b - a * d[b] ≠ 0, from h_wron_ne_zero a b c h_coprime_ab h_coprime_ca h_der_not_all_zero h_wron, have h_deg_add : degree (gcd a d[a] * gcd b d[b] * gcd c d[c]) = degree (gcd a d[a]) + degree (gcd b d[b]) + degree (gcd c d[c]), from h_deg_add a b c h_wron_ne_zero h_gcds_dvd, have h_deg_add_le : degree (gcd a d[a]) + degree (gcd b d[b]) + degree (gcd c d[c]) ≤ degree a + degree b - 1, { rw [←h_deg_add], have h1 : degree (gcd a d[a] * gcd b d[b] * gcd c d[c]) ≤ degree (d[a] * b - a * d[b]), from degree_dvd h_gcds_dvd h_wron_ne_zero, exact nat.le_trans h1 (degree_wron_le), }, have h_deg_c_le_1 : degree c ≤ (degree a - degree (gcd a d[a])) + (degree b - degree (gcd b d[b])) + (degree c - degree (gcd c d[c])) - 1, from rw_aux_1 a b c h_add h_constant h_deg_add_le, have h_le_rad : degree a - degree (gcd a d[a]) + (degree b - degree (gcd b d[b])) + (degree c - degree (gcd c d[c])) - 1 ≤ degree (rad (a * b * c)) - 1, from rw_aux_2 ha hb hc h_coprime_ab h_coprime_bc h_coprime_ca, have h_ms : degree c ≤ degree ( rad (a*b*c)) - 1, from nat.le_trans h_deg_c_le_1 h_le_rad, have h_eq : degree ( rad (a*b*c)) - 1 + 1 = degree ( rad (a*b*c)), { have h_pos : degree ( rad (a*b*c)) > 0, from degree_rad_pos a b c ha hb hc h_constant, apply nat.succ_pred_eq_of_pos h_pos, }, exact show degree c < degree ( rad (a*b*c)), from calc degree c + 1 ≤ degree ( rad (a*b*c) ) - 1 + 1 : by simp [h_ms] ... = degree ( rad (a*b*c)) : h_eq end --place in to_multiset private lemma cons_ne_zero {γ : Type u}(a : γ)(s : multiset γ): a :: s ≠ 0 := begin intro h, have : a ∈ a :: s, { simp, }, simp * at *, end --place in to_multiset --private? private lemma map_eq_zero_iff_eq_zero {γ ζ : Type u} (s : multiset γ)(f: γ → ζ): map f s = 0 ↔ s = 0 := begin apply multiset.induction_on s, { simp * at *, }, { intros a s h, split, { intro h1, simp * at *, have h2 : false, from cons_ne_zero _ _ h1, contradiction, }, { intro h1, simp * at *, }, } end --structure? lemma factors_eq_factors_of_associated {a b : polynomial β} (h : (a ~ᵤ b)): a.factors = b.factors := begin rw [factors_eq_map_monic_out_to_multiset_mk, factors_eq_map_monic_out_to_multiset_mk], rw [associated_iff_mk_eq_mk] at *, simp * at *, end @[simp] lemma rad_neg_eq_rad [field β] (p : polynomial β) : rad (-p) = rad p := begin have hp : (p ~ᵤ (-p)), from associated_neg _, have h1 : p.factors = (-p).factors, from factors_eq_factors_of_associated hp, rw [rad, rad, h1], end @[simp] lemma coprime_neg_iff_coprime_left {a b : polynomial β} : coprime (-a) b ↔ coprime a b := begin split, { intro h, apply coprime_of_coprime_of_associated_left h (associated_neg _), }, { intro h, apply coprime_of_coprime_of_associated_left h (associated_neg a).symm, --again elaborator does no longer find placeholder } end @[simp] lemma coprime_neg_iff_coprime_right {a b : polynomial β} : coprime a (-b) ↔ coprime a b := begin split, { intro h, apply coprime_of_coprime_of_associated_right h (associated_neg _), }, { intro h, apply coprime_of_coprime_of_associated_right h (associated_neg b).symm, } end private lemma neg_eq_zero_iff_eq_zero {a : polynomial β} : -a = 0 ↔ a = 0 := begin split, { intro h, exact calc a = -(-a) : by simp ... = - (0) : by rw h ... = _ : by simp }, { intro h, simp *, } end private lemma neg_ne_zero_iff_ne_zero {a : polynomial β} : -a ≠ 0 ↔ a ≠ 0 := not_iff_not_of_iff neg_eq_zero_iff_eq_zero local attribute [simp] is_constant_neg_iff_is_constant theorem Mason_Stothers_case_c [field β] (h_char : characteristic_zero β) (a b c : polynomial β) (ha : a ≠ 0) (hb : b ≠ 0) (hc : c ≠ 0) (h_coprime_ab : coprime a b) (h_coprime_bc : coprime b c) (h_coprime_ca : coprime c a) (h_add : a + b + c = 0) (h_constant : ¬(is_constant a ∧ is_constant b ∧ is_constant c)): --You can do this one by cc? simpa [and.comm, and.left_comm, and.assoc] using h_constant degree c < degree ( rad (a*b*c)) := begin have h_c : degree (-c) < degree ( rad (a*b*(-c))), { have h_add : a + b = - c, { exact calc a + b = a + b + (c - c) : by simp ... = a + b + c - c : by simp ... = 0 - c : by rw [h_add] ... = _ : by simp, }, rw ←(is_constant_neg_iff_is_constant c) at h_constant, --The argument should not have been implicit apply Mason_Stothers_special h_char _ _ _ ha hb _ h_coprime_ab _ _ h_add h_constant,--We need relPrime.symm in general { simp [neg_ne_zero_iff_ne_zero, *], }, have : coprime b (-c),--Rather as rw, rather as simp rule, coprime a -c = coprime a c from coprime_neg_iff_coprime_right.2 h_coprime_bc, exact this, have : coprime (-c) a, from coprime_neg_iff_coprime_left.2 h_coprime_ca, exact this, }, have h_neg_abc: a * b * -c = - (a * b * c), { simp, }, rw [degree_neg, h_neg_abc, rad_neg_eq_rad] at h_c, exact h_c, end theorem Mason_Stothers_case_a [field β] (h_char : characteristic_zero β) (a b c : polynomial β) (ha : a ≠ 0) (hb : b ≠ 0) (hc : c ≠ 0) (h_coprime_ab : coprime a b) (h_coprime_bc : coprime b c) (h_coprime_ca : coprime c a) (h_add : a + b + c = 0) (h_constant : ¬(is_constant a ∧ is_constant b ∧ is_constant c)): degree a < degree (rad (a*b*c)) := /- suffices degree a < degree (rad (c*b*a)), from Mason_Stothers_case_c h_char c b a hc hb ha _ _ _ _ _, _ -/ begin have h_a : degree (-a) < degree ( rad (b*c*(-a))), { have h_add_1 : b + c + a = 0, { simpa [add_comm, add_assoc] using h_add, }, have h_add_2 : b + c = - a, { exact calc b + c = b + c + (a - a) : by simp ... = b + c + a - a : by simp ... = 0 - a : by rw [h_add_1] ... = _ : by simp, }, have h_constant_2 : ¬(is_constant b ∧ is_constant c ∧ is_constant (-a)), { simpa [(is_constant_neg_iff_is_constant a), and_comm, and_assoc] using h_constant, intros h1 h2, simp *, }, apply Mason_Stothers_special h_char b c (-a) hb hc (neg_ne_zero_iff_ne_zero.2 ha) h_coprime_bc _ _ h_add_2 h_constant_2, { simp *, }, { simp *, }, }, have h_neg_abc: b * c * -a = - (a * b * c), { simp, exact calc b * c * a = b * (a * c) : by rw [mul_assoc, mul_comm c a] ... = _ : by rw [←mul_assoc, mul_comm b a], }, rw [degree_neg, h_neg_abc, rad_neg_eq_rad] at h_a, exact h_a, end theorem Mason_Stothers_case_b [field β] (h_char : characteristic_zero β) (a b c : polynomial β) (ha : a ≠ 0) (hb : b ≠ 0) (hc : c ≠ 0) (h_coprime_ab : coprime a b) (h_coprime_bc : coprime b c) (h_coprime_ca : coprime c a) (h_add : a + b + c = 0) (h_constant : ¬(is_constant a ∧ is_constant b ∧ is_constant c)): degree b < degree ( rad (a*b*c)) := begin have h_b : degree (-b) < degree ( rad (a*c*(-b))), { have h_add_1 : a + c + b = 0, { simpa [add_comm, add_assoc] using h_add, }, have h_add_2 : a + c = - b, { exact calc a + c = a + c + (b - b) : by simp ... = a + c + b - b : by simp ... = 0 - b : by rw [h_add_1] ... = _ : by simp, }, have h_constant_2 : ¬(is_constant a ∧ is_constant c ∧ is_constant (-b)), { simpa [(is_constant_neg_iff_is_constant b), and_comm, and_assoc] using h_constant, }, apply Mason_Stothers_special h_char a c (-b) ha hc (neg_ne_zero_iff_ne_zero.2 hb) h_coprime_ca.symm _ _ h_add_2 h_constant_2, { simp [h_coprime_bc.symm, *], }, { simp [h_coprime_ab.symm, *], } }, have h_neg_abc: a * c * -b = - (a * b * c), { simp, exact calc a * c * b = a * (b * c) : by rw [mul_assoc, mul_comm c b] ... = _ : by rw [←mul_assoc], }, rw [degree_neg, h_neg_abc, rad_neg_eq_rad] at h_b, exact h_b, end theorem Mason_Stothers [field β] (h_char : characteristic_zero β) (a b c : polynomial β) (ha : a ≠ 0) (hb : b ≠ 0) (hc : c ≠ 0) (h_coprime_ab : coprime a b) (h_coprime_bc : coprime b c) (h_coprime_ca : coprime c a) (h_add : a + b + c = 0) (h_constant : ¬(is_constant a ∧ is_constant b ∧ is_constant c)): max (degree a) (max (degree b) (degree c)) < degree ( rad (a*b*c)) := begin have h_a: degree a < degree ( rad (a*b*c)), from Mason_Stothers_case_a h_char a b c ha hb hc h_coprime_ab h_coprime_bc h_coprime_ca h_add h_constant, have h_b: degree b < degree ( rad (a*b*c)), from Mason_Stothers_case_b h_char a b c ha hb hc h_coprime_ab h_coprime_bc h_coprime_ca h_add h_constant, have h_c: degree c < degree ( rad (a*b*c)), from Mason_Stothers_case_c h_char a b c ha hb hc h_coprime_ab h_coprime_bc h_coprime_ca h_add h_constant, apply max_lt h_a, apply max_lt h_b h_c, end end mason_stothers
2ac8b8375216211e538473823e84dbb63050a77b
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/metric_space/contracting.lean
19b4557e6799fe8cdb405bcb773ba4db2c0f23ac
[ "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
15,819
lean
/- Copyright (c) 2019 Rohan Mitta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rohan Mitta, Kevin Buzzard, Alistair Tucker, Johannes Hölzl, Yury Kudryashov -/ import analysis.specific_limits import data.setoid.basic import dynamics.fixed_points.topology /-! # Contracting maps A Lipschitz continuous self-map with Lipschitz constant `K < 1` is called a *contracting map*. In this file we prove the Banach fixed point theorem, some explicit estimates on the rate of convergence, and some properties of the map sending a contracting map to its fixed point. ## Main definitions * `contracting_with K f` : a Lipschitz continuous self-map with `K < 1`; * `efixed_point` : given a contracting map `f` on a complete emetric space and a point `x` such that `edist x (f x) ≠ ∞`, `efixed_point f hf x hx` is the unique fixed point of `f` in `emetric.ball x ∞`; * `fixed_point` : the unique fixed point of a contracting map on a complete nonempty metric space. ## Tags contracting map, fixed point, Banach fixed point theorem -/ open_locale nnreal topological_space classical ennreal open filter function variables {α : Type*} /-- A map is said to be `contracting_with K`, if `K < 1` and `f` is `lipschitz_with K`. -/ def contracting_with [emetric_space α] (K : ℝ≥0) (f : α → α) := (K < 1) ∧ lipschitz_with K f namespace contracting_with variables [emetric_space α] [cs : complete_space α] {K : ℝ≥0} {f : α → α} open emetric set lemma to_lipschitz_with (hf : contracting_with K f) : lipschitz_with K f := hf.2 lemma one_sub_K_pos' (hf : contracting_with K f) : (0:ℝ≥0∞) < 1 - K := by simp [hf.1] lemma one_sub_K_ne_zero (hf : contracting_with K f) : (1:ℝ≥0∞) - K ≠ 0 := ne_of_gt hf.one_sub_K_pos' lemma one_sub_K_ne_top : (1:ℝ≥0∞) - K ≠ ∞ := by { norm_cast, exact ennreal.coe_ne_top } lemma edist_inequality (hf : contracting_with K f) {x y} (h : edist x y ≠ ∞) : edist x y ≤ (edist x (f x) + edist y (f y)) / (1 - K) := suffices edist x y ≤ edist x (f x) + edist y (f y) + K * edist x y, by rwa [ennreal.le_div_iff_mul_le (or.inl hf.one_sub_K_ne_zero) (or.inl one_sub_K_ne_top), mul_comm, ennreal.sub_mul (λ _ _, h), one_mul, tsub_le_iff_right], calc edist x y ≤ edist x (f x) + edist (f x) (f y) + edist (f y) y : edist_triangle4 _ _ _ _ ... = edist x (f x) + edist y (f y) + edist (f x) (f y) : by rw [edist_comm y, add_right_comm] ... ≤ edist x (f x) + edist y (f y) + K * edist x y : add_le_add le_rfl (hf.2 _ _) lemma edist_le_of_fixed_point (hf : contracting_with K f) {x y} (h : edist x y ≠ ∞) (hy : is_fixed_pt f y) : edist x y ≤ (edist x (f x)) / (1 - K) := by simpa only [hy.eq, edist_self, add_zero] using hf.edist_inequality h lemma eq_or_edist_eq_top_of_fixed_points (hf : contracting_with K f) {x y} (hx : is_fixed_pt f x) (hy : is_fixed_pt f y) : x = y ∨ edist x y = ∞ := begin refine or_iff_not_imp_right.2 (λ h, edist_le_zero.1 _), simpa only [hx.eq, edist_self, add_zero, ennreal.zero_div] using hf.edist_le_of_fixed_point h hy end /-- If a map `f` is `contracting_with K`, and `s` is a forward-invariant set, then restriction of `f` to `s` is `contracting_with K` as well. -/ lemma restrict (hf : contracting_with K f) {s : set α} (hs : maps_to f s s) : contracting_with K (hs.restrict f s s) := ⟨hf.1, λ x y, hf.2 x y⟩ include cs /-- Banach fixed-point theorem, contraction mapping theorem, `emetric_space` version. A contracting map on a complete metric space has a fixed point. We include more conclusions in this theorem to avoid proving them again later. The main API for this theorem are the functions `efixed_point` and `fixed_point`, and lemmas about these functions. -/ theorem exists_fixed_point (hf : contracting_with K f) (x : α) (hx : edist x (f x) ≠ ∞) : ∃ y, is_fixed_pt f y ∧ tendsto (λ n, f^[n] x) at_top (𝓝 y) ∧ ∀ n:ℕ, edist (f^[n] x) y ≤ (edist x (f x)) * K^n / (1 - K) := have cauchy_seq (λ n, f^[n] x), from cauchy_seq_of_edist_le_geometric K (edist x (f x)) (ennreal.coe_lt_one_iff.2 hf.1) hx (hf.to_lipschitz_with.edist_iterate_succ_le_geometric x), let ⟨y, hy⟩ := cauchy_seq_tendsto_of_complete this in ⟨y, is_fixed_pt_of_tendsto_iterate hy hf.2.continuous.continuous_at, hy, edist_le_of_edist_le_geometric_of_tendsto K (edist x (f x)) (hf.to_lipschitz_with.edist_iterate_succ_le_geometric x) hy⟩ variable (f) -- avoid `efixed_point _` in pretty printer /-- Let `x` be a point of a complete emetric space. Suppose that `f` is a contracting map, and `edist x (f x) ≠ ∞`. Then `efixed_point` is the unique fixed point of `f` in `emetric.ball x ∞`. -/ noncomputable def efixed_point (hf : contracting_with K f) (x : α) (hx : edist x (f x) ≠ ∞) : α := classical.some $ hf.exists_fixed_point x hx variables {f} lemma efixed_point_is_fixed_pt (hf : contracting_with K f) {x : α} (hx : edist x (f x) ≠ ∞) : is_fixed_pt f (efixed_point f hf x hx) := (classical.some_spec $ hf.exists_fixed_point x hx).1 lemma tendsto_iterate_efixed_point (hf : contracting_with K f) {x : α} (hx : edist x (f x) ≠ ∞) : tendsto (λn, f^[n] x) at_top (𝓝 $ efixed_point f hf x hx) := (classical.some_spec $ hf.exists_fixed_point x hx).2.1 lemma apriori_edist_iterate_efixed_point_le (hf : contracting_with K f) {x : α} (hx : edist x (f x) ≠ ∞) (n : ℕ) : edist (f^[n] x) (efixed_point f hf x hx) ≤ (edist x (f x)) * K^n / (1 - K) := (classical.some_spec $ hf.exists_fixed_point x hx).2.2 n lemma edist_efixed_point_le (hf : contracting_with K f) {x : α} (hx : edist x (f x) ≠ ∞) : edist x (efixed_point f hf x hx) ≤ (edist x (f x)) / (1 - K) := by { convert hf.apriori_edist_iterate_efixed_point_le hx 0, simp only [pow_zero, mul_one] } lemma edist_efixed_point_lt_top (hf : contracting_with K f) {x : α} (hx : edist x (f x) ≠ ∞) : edist x (efixed_point f hf x hx) < ∞ := (hf.edist_efixed_point_le hx).trans_lt (ennreal.mul_lt_top hx $ ennreal.inv_ne_top.2 hf.one_sub_K_ne_zero) lemma efixed_point_eq_of_edist_lt_top (hf : contracting_with K f) {x : α} (hx : edist x (f x) ≠ ∞) {y : α} (hy : edist y (f y) ≠ ∞) (h : edist x y ≠ ∞) : efixed_point f hf x hx = efixed_point f hf y hy := begin refine (hf.eq_or_edist_eq_top_of_fixed_points _ _).elim id (λ h', false.elim (ne_of_lt _ h')); try { apply efixed_point_is_fixed_pt }, change edist_lt_top_setoid.rel _ _, transitivity x, by { symmetry, exact hf.edist_efixed_point_lt_top hx }, transitivity y, exacts [lt_top_iff_ne_top.2 h, hf.edist_efixed_point_lt_top hy] end omit cs /-- Banach fixed-point theorem for maps contracting on a complete subset. -/ theorem exists_fixed_point' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s) (hf : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) : ∃ y ∈ s, is_fixed_pt f y ∧ tendsto (λ n, f^[n] x) at_top (𝓝 y) ∧ ∀ n:ℕ, edist (f^[n] x) y ≤ (edist x (f x)) * K^n / (1 - K) := begin haveI := hsc.complete_space_coe, rcases hf.exists_fixed_point ⟨x, hxs⟩ hx with ⟨y, hfy, h_tendsto, hle⟩, refine ⟨y, y.2, subtype.ext_iff_val.1 hfy, _, λ n, _⟩, { convert (continuous_subtype_coe.tendsto _).comp h_tendsto, ext n, simp only [(∘), maps_to.iterate_restrict, maps_to.coe_restrict_apply, subtype.coe_mk] }, { convert hle n, rw [maps_to.iterate_restrict, eq_comm, maps_to.coe_restrict_apply, subtype.coe_mk] } end variable (f) -- avoid `efixed_point _` in pretty printer /-- Let `s` be a complete forward-invariant set of a self-map `f`. If `f` contracts on `s` and `x ∈ s` satisfies `edist x (f x) ≠ ∞`, then `efixed_point'` is the unique fixed point of the restriction of `f` to `s ∩ emetric.ball x ∞`. -/ noncomputable def efixed_point' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s) (hf : contracting_with K $ hsf.restrict f s s) (x : α) (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) : α := classical.some $ hf.exists_fixed_point' hsc hsf hxs hx variables {f} lemma efixed_point_mem' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s) (hf : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) : efixed_point' f hsc hsf hf x hxs hx ∈ s := (classical.some_spec $ hf.exists_fixed_point' hsc hsf hxs hx).fst lemma efixed_point_is_fixed_pt' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s) (hf : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) : is_fixed_pt f (efixed_point' f hsc hsf hf x hxs hx) := (classical.some_spec $ hf.exists_fixed_point' hsc hsf hxs hx).snd.1 lemma tendsto_iterate_efixed_point' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s) (hf : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) : tendsto (λn, f^[n] x) at_top (𝓝 $ efixed_point' f hsc hsf hf x hxs hx) := (classical.some_spec $ hf.exists_fixed_point' hsc hsf hxs hx).snd.2.1 lemma apriori_edist_iterate_efixed_point_le' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s) (hf : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) (n : ℕ) : edist (f^[n] x) (efixed_point' f hsc hsf hf x hxs hx) ≤ (edist x (f x)) * K^n / (1 - K) := (classical.some_spec $ hf.exists_fixed_point' hsc hsf hxs hx).snd.2.2 n lemma edist_efixed_point_le' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s) (hf : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) : edist x (efixed_point' f hsc hsf hf x hxs hx) ≤ (edist x (f x)) / (1 - K) := by { convert hf.apriori_edist_iterate_efixed_point_le' hsc hsf hxs hx 0, rw [pow_zero, mul_one] } lemma edist_efixed_point_lt_top' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s) (hf : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) : edist x (efixed_point' f hsc hsf hf x hxs hx) < ∞ := (hf.edist_efixed_point_le' hsc hsf hxs hx).trans_lt (ennreal.mul_lt_top hx $ ennreal.inv_ne_top.2 hf.one_sub_K_ne_zero) /-- If a globally contracting map `f` has two complete forward-invariant sets `s`, `t`, and `x ∈ s` is at a finite distance from `y ∈ t`, then the `efixed_point'` constructed by `x` is the same as the `efixed_point'` constructed by `y`. This lemma takes additional arguments stating that `f` contracts on `s` and `t` because this way it can be used to prove the desired equality with non-trivial proofs of these facts. -/ lemma efixed_point_eq_of_edist_lt_top' (hf : contracting_with K f) {s : set α} (hsc : is_complete s) (hsf : maps_to f s s) (hfs : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) {t : set α} (htc : is_complete t) (htf : maps_to f t t) (hft : contracting_with K $ htf.restrict f t t) {y : α} (hyt : y ∈ t) (hy : edist y (f y) ≠ ∞) (hxy : edist x y ≠ ∞) : efixed_point' f hsc hsf hfs x hxs hx = efixed_point' f htc htf hft y hyt hy := begin refine (hf.eq_or_edist_eq_top_of_fixed_points _ _).elim id (λ h', false.elim (ne_of_lt _ h')); try { apply efixed_point_is_fixed_pt' }, change edist_lt_top_setoid.rel _ _, transitivity x, by { symmetry, apply edist_efixed_point_lt_top' }, transitivity y, exact lt_top_iff_ne_top.2 hxy, apply edist_efixed_point_lt_top' end end contracting_with namespace contracting_with variables [metric_space α] {K : ℝ≥0} {f : α → α} (hf : contracting_with K f) include hf lemma one_sub_K_pos (hf : contracting_with K f) : (0:ℝ) < 1 - K := sub_pos.2 hf.1 lemma dist_le_mul (x y : α) : dist (f x) (f y) ≤ K * dist x y := hf.to_lipschitz_with.dist_le_mul x y lemma dist_inequality (x y) : dist x y ≤ (dist x (f x) + dist y (f y)) / (1 - K) := suffices dist x y ≤ dist x (f x) + dist y (f y) + K * dist x y, by rwa [le_div_iff hf.one_sub_K_pos, mul_comm, sub_mul, one_mul, sub_le_iff_le_add], calc dist x y ≤ dist x (f x) + dist y (f y) + dist (f x) (f y) : dist_triangle4_right _ _ _ _ ... ≤ dist x (f x) + dist y (f y) + K * dist x y : add_le_add_left (hf.dist_le_mul _ _) _ lemma dist_le_of_fixed_point (x) {y} (hy : is_fixed_pt f y) : dist x y ≤ (dist x (f x)) / (1 - K) := by simpa only [hy.eq, dist_self, add_zero] using hf.dist_inequality x y theorem fixed_point_unique' {x y} (hx : is_fixed_pt f x) (hy : is_fixed_pt f y) : x = y := (hf.eq_or_edist_eq_top_of_fixed_points hx hy).resolve_right (edist_ne_top _ _) /-- Let `f` be a contracting map with constant `K`; let `g` be another map uniformly `C`-close to `f`. If `x` and `y` are their fixed points, then `dist x y ≤ C / (1 - K)`. -/ lemma dist_fixed_point_fixed_point_of_dist_le' (g : α → α) {x y} (hx : is_fixed_pt f x) (hy : is_fixed_pt g y) {C} (hfg : ∀ z, dist (f z) (g z) ≤ C) : dist x y ≤ C / (1 - K) := calc dist x y = dist y x : dist_comm x y ... ≤ (dist y (f y)) / (1 - K) : hf.dist_le_of_fixed_point y hx ... = (dist (f y) (g y)) / (1 - K) : by rw [hy.eq, dist_comm] ... ≤ C / (1 - K) : (div_le_div_right hf.one_sub_K_pos).2 (hfg y) noncomputable theory variables [nonempty α] [complete_space α] variable (f) /-- The unique fixed point of a contracting map in a nonempty complete metric space. -/ def fixed_point : α := efixed_point f hf _ (edist_ne_top (classical.choice ‹nonempty α›) _) variable {f} /-- The point provided by `contracting_with.fixed_point` is actually a fixed point. -/ lemma fixed_point_is_fixed_pt : is_fixed_pt f (fixed_point f hf) := hf.efixed_point_is_fixed_pt _ lemma fixed_point_unique {x} (hx : is_fixed_pt f x) : x = fixed_point f hf := hf.fixed_point_unique' hx hf.fixed_point_is_fixed_pt lemma dist_fixed_point_le (x) : dist x (fixed_point f hf) ≤ (dist x (f x)) / (1 - K) := hf.dist_le_of_fixed_point x hf.fixed_point_is_fixed_pt /-- Aposteriori estimates on the convergence of iterates to the fixed point. -/ lemma aposteriori_dist_iterate_fixed_point_le (x n) : dist (f^[n] x) (fixed_point f hf) ≤ (dist (f^[n] x) (f^[n+1] x)) / (1 - K) := by { rw [iterate_succ'], apply hf.dist_fixed_point_le } lemma apriori_dist_iterate_fixed_point_le (x n) : dist (f^[n] x) (fixed_point f hf) ≤ (dist x (f x)) * K^n / (1 - K) := le_trans (hf.aposteriori_dist_iterate_fixed_point_le x n) $ (div_le_div_right hf.one_sub_K_pos).2 $ hf.to_lipschitz_with.dist_iterate_succ_le_geometric x n lemma tendsto_iterate_fixed_point (x) : tendsto (λn, f^[n] x) at_top (𝓝 $ fixed_point f hf) := begin convert tendsto_iterate_efixed_point hf (edist_ne_top x _), refine (fixed_point_unique _ _).symm, apply efixed_point_is_fixed_pt end lemma fixed_point_lipschitz_in_map {g : α → α} (hg : contracting_with K g) {C} (hfg : ∀ z, dist (f z) (g z) ≤ C) : dist (fixed_point f hf) (fixed_point g hg) ≤ C / (1 - K) := hf.dist_fixed_point_fixed_point_of_dist_le' g hf.fixed_point_is_fixed_pt hg.fixed_point_is_fixed_pt hfg omit hf /-- If a map `f` has a contracting iterate `f^[n]`, then the fixed point of `f^[n]` is also a fixed point of `f`. -/ lemma is_fixed_pt_fixed_point_iterate {n : ℕ} (hf : contracting_with K (f^[n])) : is_fixed_pt f (hf.fixed_point (f^[n])) := begin set x := hf.fixed_point (f^[n]), have hx : (f^[n] x) = x := hf.fixed_point_is_fixed_pt, have := hf.to_lipschitz_with.dist_le_mul x (f x), rw [← iterate_succ_apply, iterate_succ_apply', hx] at this, contrapose! this, have := dist_pos.2 (ne.symm this), simpa only [nnreal.coe_one, one_mul, nnreal.val_eq_coe] using (mul_lt_mul_right this).mpr hf.left end end contracting_with
348c3acc8497e8bef35c8171233436ecbee1b1ee
9dc8cecdf3c4634764a18254e94d43da07142918
/src/category_theory/epi_mono.lean
ff2d5f37c00a90ee2069e93c970781139f789d8f
[ "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
9,260
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Scott Morrison -/ import category_theory.adjunction.basic import category_theory.opposites import category_theory.groupoid /-! # Facts about epimorphisms and monomorphisms. The definitions of `epi` and `mono` are in `category_theory.category`, since they are used by some lemmas for `iso`, which is used everywhere. -/ universes v₁ v₂ u₁ u₂ namespace category_theory variables {C : Type u₁} [category.{v₁} C] instance unop_mono_of_epi {A B : Cᵒᵖ} (f : A ⟶ B) [epi f] : mono f.unop := ⟨λ Z g h eq, quiver.hom.op_inj ((cancel_epi f).1 (quiver.hom.unop_inj eq))⟩ instance unop_epi_of_mono {A B : Cᵒᵖ} (f : A ⟶ B) [mono f] : epi f.unop := ⟨λ Z g h eq, quiver.hom.op_inj ((cancel_mono f).1 (quiver.hom.unop_inj eq))⟩ instance op_mono_of_epi {A B : C} (f : A ⟶ B) [epi f] : mono f.op := ⟨λ Z g h eq, quiver.hom.unop_inj ((cancel_epi f).1 (quiver.hom.op_inj eq))⟩ instance op_epi_of_mono {A B : C} (f : A ⟶ B) [mono f] : epi f.op := ⟨λ Z g h eq, quiver.hom.unop_inj ((cancel_mono f).1 (quiver.hom.op_inj eq))⟩ /-- A split monomorphism is a morphism `f : X ⟶ Y` with a given retraction `retraction f : Y ⟶ X` such that `f ≫ retraction f = 𝟙 X`. Every split monomorphism is a monomorphism. -/ @[ext, nolint has_nonempty_instance] structure split_mono {X Y : C} (f : X ⟶ Y) := (retraction : Y ⟶ X) (id' : f ≫ retraction = 𝟙 X . obviously) restate_axiom split_mono.id' attribute [simp, reassoc] split_mono.id /-- `is_split_mono f` is the assertion that `f` admits a retraction -/ class is_split_mono {X Y : C} (f : X ⟶ Y) : Prop := (exists_split_mono : nonempty (split_mono f)) /-- A constructor for `is_split_mono f` taking a `split_mono f` as an argument -/ lemma is_split_mono.mk' {X Y : C} {f : X ⟶ Y} (sm : split_mono f) : is_split_mono f := ⟨nonempty.intro sm⟩ /-- A split epimorphism is a morphism `f : X ⟶ Y` with a given section `section_ f : Y ⟶ X` such that `section_ f ≫ f = 𝟙 Y`. (Note that `section` is a reserved keyword, so we append an underscore.) Every split epimorphism is an epimorphism. -/ @[ext, nolint has_nonempty_instance] structure split_epi {X Y : C} (f : X ⟶ Y) := (section_ : Y ⟶ X) (id' : section_ ≫ f = 𝟙 Y . obviously) restate_axiom split_epi.id' attribute [simp, reassoc] split_epi.id /-- `is_split_epi f` is the assertion that `f` admits a section -/ class is_split_epi {X Y : C} (f : X ⟶ Y) : Prop := (exists_split_epi : nonempty (split_epi f)) /-- A constructor for `is_split_epi f` taking a `split_epi f` as an argument -/ lemma is_split_epi.mk' {X Y : C} {f : X ⟶ Y} (se : split_epi f) : is_split_epi f := ⟨nonempty.intro se⟩ /-- The chosen retraction of a split monomorphism. -/ noncomputable def retraction {X Y : C} (f : X ⟶ Y) [hf : is_split_mono f] : Y ⟶ X := hf.exists_split_mono.some.retraction @[simp, reassoc] lemma is_split_mono.id {X Y : C} (f : X ⟶ Y) [hf : is_split_mono f] : f ≫ retraction f = 𝟙 X := hf.exists_split_mono.some.id /-- The retraction of a split monomorphism has an obvious section. -/ def split_mono.split_epi {X Y : C} {f : X ⟶ Y} (sm : split_mono f) : split_epi (sm.retraction) := { section_ := f, } /-- The retraction of a split monomorphism is itself a split epimorphism. -/ instance retraction_is_split_epi {X Y : C} (f : X ⟶ Y) [hf : is_split_mono f] : is_split_epi (retraction f) := is_split_epi.mk' (split_mono.split_epi _) /-- A split mono which is epi is an iso. -/ lemma is_iso_of_epi_of_is_split_mono {X Y : C} (f : X ⟶ Y) [is_split_mono f] [epi f] : is_iso f := ⟨⟨retraction f, ⟨by simp, by simp [← cancel_epi f]⟩⟩⟩ /-- The chosen section of a split epimorphism. (Note that `section` is a reserved keyword, so we append an underscore.) -/ noncomputable def section_ {X Y : C} (f : X ⟶ Y) [hf : is_split_epi f] : Y ⟶ X := hf.exists_split_epi.some.section_ @[simp, reassoc] lemma is_split_epi.id {X Y : C} (f : X ⟶ Y) [hf : is_split_epi f] : section_ f ≫ f = 𝟙 Y := hf.exists_split_epi.some.id /-- The section of a split epimorphism has an obvious retraction. -/ def split_epi.split_mono {X Y : C} {f : X ⟶ Y} (se : split_epi f) : split_mono (se.section_) := { retraction := f, } /-- The section of a split epimorphism is itself a split monomorphism. -/ instance section_is_split_mono {X Y : C} (f : X ⟶ Y) [hf : is_split_epi f] : is_split_mono (section_ f) := is_split_mono.mk' (split_epi.split_mono _) /-- A split epi which is mono is an iso. -/ lemma is_iso_of_mono_of_is_split_epi {X Y : C} (f : X ⟶ Y) [mono f] [is_split_epi f] : is_iso f := ⟨⟨section_ f, ⟨by simp [← cancel_mono f], by simp⟩⟩⟩ /-- Every iso is a split mono. -/ @[priority 100] instance is_split_mono.of_iso {X Y : C} (f : X ⟶ Y) [is_iso f] : is_split_mono f := is_split_mono.mk' { retraction := inv f } /-- Every iso is a split epi. -/ @[priority 100] instance is_split_epi.of_iso {X Y : C} (f : X ⟶ Y) [is_iso f] : is_split_epi f := is_split_epi.mk' { section_ := inv f } lemma split_mono.mono {X Y : C} {f : X ⟶ Y} (sm : split_mono f) : mono f := { right_cancellation := λ Z g h w, begin replace w := w =≫ sm.retraction, simpa using w, end } /-- Every split mono is a mono. -/ @[priority 100] instance is_split_mono.mono {X Y : C} (f : X ⟶ Y) [hf : is_split_mono f] : mono f := hf.exists_split_mono.some.mono lemma split_epi.epi {X Y : C} {f : X ⟶ Y} (se : split_epi f) : epi f := { left_cancellation := λ Z g h w, begin replace w := se.section_ ≫= w, simpa using w, end } /-- Every split epi is an epi. -/ @[priority 100] instance is_split_epi.epi {X Y : C} (f : X ⟶ Y) [hf : is_split_epi f] : epi f := hf.exists_split_epi.some.epi /-- Every split mono whose retraction is mono is an iso. -/ lemma is_iso.of_mono_retraction' {X Y : C} {f : X ⟶ Y} (hf : split_mono f) [mono $ hf.retraction] : is_iso f := ⟨⟨hf.retraction, ⟨by simp, (cancel_mono_id $ hf.retraction).mp (by simp)⟩⟩⟩ /-- Every split mono whose retraction is mono is an iso. -/ lemma is_iso.of_mono_retraction {X Y : C} (f : X ⟶ Y) [hf : is_split_mono f] [hf' : mono $ retraction f] : is_iso f := @is_iso.of_mono_retraction' _ _ _ _ _ hf.exists_split_mono.some hf' /-- Every split epi whose section is epi is an iso. -/ lemma is_iso.of_epi_section' {X Y : C} {f : X ⟶ Y} (hf : split_epi f) [epi $ hf.section_] : is_iso f := ⟨⟨hf.section_, ⟨(cancel_epi_id $ hf.section_).mp (by simp), by simp⟩⟩⟩ /-- Every split epi whose section is epi is an iso. -/ lemma is_iso.of_epi_section {X Y : C} (f : X ⟶ Y) [hf : is_split_epi f] [hf' : epi $ section_ f] : is_iso f := @is_iso.of_epi_section' _ _ _ _ _ hf.exists_split_epi.some hf' /-- A category where every morphism has a `trunc` retraction is computably a groupoid. -/ -- FIXME this has unnecessarily become noncomputable! noncomputable def groupoid.of_trunc_split_mono (all_split_mono : ∀ {X Y : C} (f : X ⟶ Y), trunc (is_split_mono f)) : groupoid.{v₁} C := begin apply groupoid.of_is_iso, intros X Y f, trunc_cases all_split_mono f, trunc_cases all_split_mono (retraction f), apply is_iso.of_mono_retraction, end section variables (C) /-- A split mono category is a category in which every monomorphism is split. -/ class split_mono_category := (is_split_mono_of_mono : ∀ {X Y : C} (f : X ⟶ Y) [mono f], is_split_mono f) /-- A split epi category is a category in which every epimorphism is split. -/ class split_epi_category := (is_split_epi_of_epi : ∀ {X Y : C} (f : X ⟶ Y) [epi f], is_split_epi f) end /-- In a category in which every monomorphism is split, every monomorphism splits. This is not an instance because it would create an instance loop. -/ lemma is_split_mono_of_mono [split_mono_category C] {X Y : C} (f : X ⟶ Y) [mono f] : is_split_mono f := split_mono_category.is_split_mono_of_mono _ /-- In a category in which every epimorphism is split, every epimorphism splits. This is not an instance because it would create an instance loop. -/ lemma is_split_epi_of_epi [split_epi_category C] {X Y : C} (f : X ⟶ Y) [epi f] : is_split_epi f := split_epi_category.is_split_epi_of_epi _ section variables {D : Type u₂} [category.{v₂} D] /-- Split monomorphisms are also absolute monomorphisms. -/ @[simps] def split_mono.map {X Y : C} {f : X ⟶ Y} (sm : split_mono f) (F : C ⥤ D ) : split_mono (F.map f) := { retraction := F.map (sm.retraction), id' := by { rw [←functor.map_comp, split_mono.id, functor.map_id], } } /-- Split epimorphisms are also absolute epimorphisms. -/ @[simps] def split_epi.map {X Y : C} {f : X ⟶ Y} (se : split_epi f) (F : C ⥤ D ) : split_epi (F.map f) := { section_ := F.map (se.section_), id' := by { rw [←functor.map_comp, split_epi.id, functor.map_id], } } instance {X Y : C} (f : X ⟶ Y) [hf : is_split_mono f] (F : C ⥤ D) : is_split_mono (F.map f) := is_split_mono.mk' (hf.exists_split_mono.some.map F) instance {X Y : C} (f : X ⟶ Y) [hf : is_split_epi f] (F : C ⥤ D) : is_split_epi (F.map f) := is_split_epi.mk' (hf.exists_split_epi.some.map F) end end category_theory
3b44e08ae9f927b99b5b026451b12ef9f4b46646
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/number_theory/legendre_symbol/quadratic_reciprocity.lean
e3c70794f1c4b13c5bb1b93588d291fecf78a463
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
15,305
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Michael Stoll -/ import number_theory.legendre_symbol.gauss_eisenstein_lemmas import number_theory.legendre_symbol.quadratic_char /-! # Legendre symbol and quadratic reciprocity. This file contains results about quadratic residues modulo a prime number. We define the Legendre symbol `(a / p)` as `legendre_sym p a`. Note the order of arguments! The advantage of this form is that then `legendre_sym p` is a multiplicative map. The main results are the law of quadratic reciprocity, `quadratic_reciprocity`, as well as the interpretations in terms of existence of square roots depending on the congruence mod 4, `exists_sq_eq_prime_iff_of_mod_four_eq_one`, and `exists_sq_eq_prime_iff_of_mod_four_eq_three`. Also proven are conditions for `-1` and `2` to be a square modulo a prime, `legende_sym_neg_one` and `exists_sq_eq_neg_one_iff` for `-1`, and `exists_sq_eq_two_iff` for `2` ## Implementation notes The proof of quadratic reciprocity implemented uses Gauss' lemma and Eisenstein's lemma -/ open finset nat char namespace zmod variables (p q : ℕ) [fact p.prime] [fact q.prime] /-- Euler's Criterion: A unit `x` of `zmod p` is a square if and only if `x ^ (p / 2) = 1`. -/ lemma euler_criterion_units (x : (zmod p)ˣ) : (∃ y : (zmod p)ˣ, y ^ 2 = x) ↔ x ^ (p / 2) = 1 := begin by_cases hc : p = 2, { substI hc, simp only [eq_iff_true_of_subsingleton, exists_const], }, { have h₀ := finite_field.unit_is_square_iff (by rwa ring_char_zmod_n) x, have hs : (∃ y : (zmod p)ˣ, y ^ 2 = x) ↔ is_square(x) := by { rw is_square_iff_exists_sq x, simp_rw eq_comm, }, rw hs, rwa card p at h₀, }, end /-- Euler's Criterion: a nonzero `a : zmod p` is a square if and only if `x ^ (p / 2) = 1`. -/ lemma euler_criterion {a : zmod p} (ha : a ≠ 0) : is_square (a : zmod p) ↔ a ^ (p / 2) = 1 := begin apply (iff_congr _ (by simp [units.ext_iff])).mp (euler_criterion_units p (units.mk0 a ha)), simp only [units.ext_iff, sq, units.coe_mk0, units.coe_mul], split, { rintro ⟨y, hy⟩, exact ⟨y, hy.symm⟩ }, { rintro ⟨y, rfl⟩, have hy : y ≠ 0, { rintro rfl, simpa [zero_pow] using ha, }, refine ⟨units.mk0 y hy, _⟩, simp, } end lemma exists_sq_eq_neg_one_iff : is_square (-1 : zmod p) ↔ p % 4 ≠ 3 := begin have h := @finite_field.is_square_neg_one_iff (zmod p) _ _, rw card p at h, exact h, end lemma mod_four_ne_three_of_sq_eq_neg_one {y : zmod p} (hy : y ^ 2 = -1) : p % 4 ≠ 3 := begin rw pow_two at hy, exact (exists_sq_eq_neg_one_iff p).1 ⟨y, hy.symm⟩ end lemma mod_four_ne_three_of_sq_eq_neg_sq' {x y : zmod p} (hy : y ≠ 0) (hxy : x ^ 2 = - y ^ 2) : p % 4 ≠ 3 := @mod_four_ne_three_of_sq_eq_neg_one p _ (x / y) begin apply_fun (λ z, z / y ^ 2) at hxy, rwa [neg_div, ←div_pow, ←div_pow, div_self hy, one_pow] at hxy end lemma mod_four_ne_three_of_sq_eq_neg_sq {x y : zmod p} (hx : x ≠ 0) (hxy : x ^ 2 = - y ^ 2) : p % 4 ≠ 3 := begin apply_fun (λ x, -x) at hxy, rw neg_neg at hxy, exact mod_four_ne_three_of_sq_eq_neg_sq' p hx hxy.symm end lemma pow_div_two_eq_neg_one_or_one {a : zmod p} (ha : a ≠ 0) : a ^ (p / 2) = 1 ∨ a ^ (p / 2) = -1 := begin cases nat.prime.eq_two_or_odd (fact.out p.prime) with hp2 hp_odd, { substI p, revert a ha, dec_trivial }, rw [← mul_self_eq_one_iff, ← pow_add, ← two_mul, two_mul_odd_div_two hp_odd], exact pow_card_sub_one_eq_one ha end /-- The Legendre symbol of `a : ℤ` and a prime `p`, `legendre_sym p a`, is an integer defined as * `0` if `a` is `0` modulo `p`; * `1` if `a` is a square modulo `p` * `-1` otherwise. Note the order of the arguments! The advantage of the order chosen here is that `legendre_sym p` is a multiplicative function `ℤ → ℤ`. -/ def legendre_sym (p : ℕ) [fact p.prime] (a : ℤ) : ℤ := quadratic_char (zmod p) a /-- We have the congruence `legendre_sym p a ≡ a ^ (p / 2) mod p`. -/ lemma legendre_sym_eq_pow (p : ℕ) (a : ℤ) [hp : fact p.prime] : (legendre_sym p a : zmod p) = (a ^ (p / 2)) := begin rw legendre_sym, by_cases ha : (a : zmod p) = 0, { simp only [ha, zero_pow (nat.div_pos (hp.1.two_le) (succ_pos 1)), mul_char.map_zero, int.cast_zero], }, by_cases hp₁ : p = 2, { substI p, generalize : (a : (zmod 2)) = b, revert b, dec_trivial, }, { have h₁ := quadratic_char_eq_pow_of_char_ne_two (by rwa ring_char_zmod_n p) ha, rw card p at h₁, rw h₁, have h₂ := ring.neg_one_ne_one_of_char_ne_two (by rwa ring_char_zmod_n p), cases pow_div_two_eq_neg_one_or_one p ha with h h, { rw [if_pos h, h, int.cast_one], }, { rw [h, if_neg h₂, int.cast_neg, int.cast_one], } } end /-- If `p ∤ a`, then `legendre_sym p a` is `1` or `-1`. -/ lemma legendre_sym_eq_one_or_neg_one (p : ℕ) [fact p.prime] (a : ℤ) (ha : (a : zmod p) ≠ 0) : legendre_sym p a = 1 ∨ legendre_sym p a = -1 := quadratic_char_dichotomy ha lemma legendre_sym_eq_neg_one_iff_not_one {a : ℤ} (ha : (a : zmod p) ≠ 0) : legendre_sym p a = -1 ↔ ¬ legendre_sym p a = 1 := quadratic_char_eq_neg_one_iff_not_one ha /-- The Legendre symbol of `p` and `a` is zero iff `p ∣ a`. -/ lemma legendre_sym_eq_zero_iff (p : ℕ) [fact p.prime] (a : ℤ) : legendre_sym p a = 0 ↔ (a : zmod p) = 0 := quadratic_char_eq_zero_iff @[simp] lemma legendre_sym_zero (p : ℕ) [fact p.prime] : legendre_sym p 0 = 0 := by rw [legendre_sym, int.cast_zero, mul_char.map_zero] @[simp] lemma legendre_sym_one (p : ℕ) [fact p.prime] : legendre_sym p 1 = 1 := by rw [legendre_sym, int.cast_one, mul_char.map_one] /-- The Legendre symbol is multiplicative in `a` for `p` fixed. -/ lemma legendre_sym_mul (p : ℕ) [fact p.prime] (a b : ℤ) : legendre_sym p (a * b) = legendre_sym p a * legendre_sym p b := begin rw [legendre_sym, legendre_sym, legendre_sym], push_cast, exact quadratic_char_mul (a : zmod p) b, end /-- The Legendre symbol is a homomorphism of monoids with zero. -/ @[simps] def legendre_sym_hom (p : ℕ) [fact p.prime] : ℤ →*₀ ℤ := { to_fun := legendre_sym p, map_zero' := legendre_sym_zero p, map_one' := legendre_sym_one p, map_mul' := legendre_sym_mul p } /-- The square of the symbol is 1 if `p ∤ a`. -/ theorem legendre_sym_sq_one (p : ℕ) [fact p.prime] (a : ℤ) (ha : (a : zmod p) ≠ 0) : (legendre_sym p a)^2 = 1 := quadratic_char_sq_one ha /-- The Legendre symbol of `a^2` at `p` is 1 if `p ∤ a`. -/ theorem legendre_sym_sq_one' (p : ℕ) [fact p.prime] (a : ℤ) (ha : (a : zmod p) ≠ 0) : legendre_sym p (a ^ 2) = 1 := begin rw [legendre_sym], push_cast, exact quadratic_char_sq_one' ha, end /-- The Legendre symbol depends only on `a` mod `p`. -/ theorem legendre_sym_mod (p : ℕ) [fact p.prime] (a : ℤ) : legendre_sym p a = legendre_sym p (a % p) := by simp only [legendre_sym, int_cast_mod] /-- Gauss' lemma. The legendre symbol can be computed by considering the number of naturals less than `p/2` such that `(a * x) % p > p / 2` -/ lemma gauss_lemma {a : ℤ} (hp : p ≠ 2) (ha0 : (a : zmod p) ≠ 0) : legendre_sym p a = (-1) ^ ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card := begin haveI hp' : fact (p % 2 = 1) := ⟨nat.prime.mod_two_eq_one_iff_ne_two.mpr hp⟩, have : (legendre_sym p a : zmod p) = (((-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card : ℤ) : zmod p) := by { rw [legendre_sym_eq_pow, legendre_symbol.gauss_lemma_aux p ha0]; simp }, cases legendre_sym_eq_one_or_neg_one p a ha0; cases neg_one_pow_eq_or ℤ ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card; simp [*, ne_neg_self p one_ne_zero, (ne_neg_self p one_ne_zero).symm] at * end /-- When `p ∤ a`, then `legendre_sym p a = 1` iff `a` is a square mod `p`. -/ lemma legendre_sym_eq_one_iff {a : ℤ} (ha0 : (a : zmod p) ≠ 0) : legendre_sym p a = 1 ↔ is_square (a : zmod p) := quadratic_char_one_iff_is_square ha0 /-- `legendre_sym p a = -1` iff`a` is a nonsquare mod `p`. -/ lemma legendre_sym_eq_neg_one_iff {a : ℤ} : legendre_sym p a = -1 ↔ ¬ is_square (a : zmod p) := quadratic_char_neg_one_iff_not_is_square /-- The number of square roots of `a` modulo `p` is determined by the Legendre symbol. -/ lemma legendre_sym_card_sqrts (hp : p ≠ 2) (a : ℤ) : ↑{x : zmod p | x^2 = a}.to_finset.card = legendre_sym p a + 1 := begin have h : ring_char (zmod p) ≠ 2 := by { rw ring_char_zmod_n, exact hp, }, exact quadratic_char_card_sqrts h a, end /-- `legendre_sym p (-1)` is given by `χ₄ p`. -/ lemma legendre_sym_neg_one (hp : p ≠ 2) : legendre_sym p (-1) = χ₄ p := begin have h : ring_char (zmod p) ≠ 2 := by { rw ring_char_zmod_n, exact hp, }, have h₁ := quadratic_char_neg_one h, rw card p at h₁, exact_mod_cast h₁, end open_locale big_operators lemma eisenstein_lemma (hp : p ≠ 2) {a : ℕ} (ha1 : a % 2 = 1) (ha0 : (a : zmod p) ≠ 0) : legendre_sym p a = (-1)^∑ x in Ico 1 (p / 2).succ, (x * a) / p := begin haveI hp' : fact (p % 2 = 1) := ⟨nat.prime.mod_two_eq_one_iff_ne_two.mpr hp⟩, have ha0' : ((a : ℤ) : zmod p) ≠ 0 := by { norm_cast, exact ha0 }, rw [neg_one_pow_eq_pow_mod_two, gauss_lemma p hp ha0', neg_one_pow_eq_pow_mod_two, (by norm_cast : ((a : ℤ) : zmod p) = (a : zmod p)), show _ = _, from legendre_symbol.eisenstein_lemma_aux p ha1 ha0] end /-- **Quadratic reciprocity theorem** -/ theorem quadratic_reciprocity (hp1 : p ≠ 2) (hq1 : q ≠ 2) (hpq : p ≠ q) : legendre_sym q p * legendre_sym p q = (-1) ^ ((p / 2) * (q / 2)) := have hpq0 : (p : zmod q) ≠ 0, from prime_ne_zero q p hpq.symm, have hqp0 : (q : zmod p) ≠ 0, from prime_ne_zero p q hpq, by rw [eisenstein_lemma q hq1 (nat.prime.mod_two_eq_one_iff_ne_two.mpr hp1) hpq0, eisenstein_lemma p hp1 (nat.prime.mod_two_eq_one_iff_ne_two.mpr hq1) hqp0, ← pow_add, legendre_symbol.sum_mul_div_add_sum_mul_div_eq_mul q p hpq0, mul_comm] lemma legendre_sym_two (hp2 : p ≠ 2) : legendre_sym p 2 = (-1) ^ (p / 4 + p / 2) := begin have hp1 := nat.prime.mod_two_eq_one_iff_ne_two.mpr hp2, have hp22 : p / 2 / 2 = _ := legendre_symbol.div_eq_filter_card (show 0 < 2, from dec_trivial) (nat.div_le_self (p / 2) 2), have hcard : (Ico 1 (p / 2).succ).card = p / 2, by simp, have hx2 : ∀ x ∈ Ico 1 (p / 2).succ, (2 * x : zmod p).val = 2 * x, from λ x hx, have h2xp : 2 * x < p, from calc 2 * x ≤ 2 * (p / 2) : mul_le_mul_of_nonneg_left (le_of_lt_succ $ (mem_Ico.mp hx).2) dec_trivial ... < _ : by conv_rhs {rw [← div_add_mod p 2, hp1]}; exact lt_succ_self _, by rw [← nat.cast_two, ← nat.cast_mul, val_cast_of_lt h2xp], have hdisj : disjoint ((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmod p).val)) ((Ico 1 (p / 2).succ).filter (λ x, x * 2 ≤ p / 2)), from disjoint_filter.2 (λ x hx, by { rw [nat.cast_two, hx2 x hx, mul_comm], simp }), have hunion : ((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmod p).val)) ∪ ((Ico 1 (p / 2).succ).filter (λ x, x * 2 ≤ p / 2)) = Ico 1 (p / 2).succ, begin rw [filter_union_right], conv_rhs {rw [← @filter_true _ (Ico 1 (p / 2).succ)]}, exact filter_congr (λ x hx, by rw [nat.cast_two, hx2 x hx, mul_comm, iff_true_intro (lt_or_le _ _)]) end, have hp2' := prime_ne_zero p 2 hp2, rw (by norm_cast : ((2 : ℕ) : zmod p) = (2 : ℤ)) at *, erw [gauss_lemma p hp2 hp2', neg_one_pow_eq_pow_mod_two, @neg_one_pow_eq_pow_mod_two _ _ (p / 4 + p / 2)], refine congr_arg2 _ rfl ((eq_iff_modeq_nat 2).1 _), rw [show 4 = 2 * 2, from rfl, ← nat.div_div_eq_div_mul, hp22, nat.cast_add, ← sub_eq_iff_eq_add', sub_eq_add_neg, neg_eq_self_mod_two, ← nat.cast_add, ← card_disjoint_union hdisj, hunion, hcard] end lemma exists_sq_eq_two_iff (hp1 : p ≠ 2) : is_square (2 : zmod p) ↔ p % 8 = 1 ∨ p % 8 = 7 := begin have hp2 : ((2 : ℤ) : zmod p) ≠ 0, by exact_mod_cast prime_ne_zero p 2 hp1, have hpm4 : p % 4 = p % 8 % 4, from (nat.mod_mul_left_mod p 2 4).symm, have hpm2 : p % 2 = p % 8 % 2, from (nat.mod_mul_left_mod p 4 2).symm, rw [show (2 : zmod p) = (2 : ℤ), by simp, ← legendre_sym_eq_one_iff p hp2], erw [legendre_sym_two p hp1, neg_one_pow_eq_one_iff_even (show (-1 : ℤ) ≠ 1, from dec_trivial), even_add, even_div, even_div], have := nat.mod_lt p (show 0 < 8, from dec_trivial), have hp := nat.prime.mod_two_eq_one_iff_ne_two.mpr hp1, revert this hp, erw [hpm4, hpm2], generalize hm : p % 8 = m, unfreezingI {clear_dependent p}, dec_trivial!, end lemma exists_sq_eq_prime_iff_of_mod_four_eq_one (hp1 : p % 4 = 1) (hq1 : q ≠ 2) : is_square (q : zmod p) ↔ is_square (p : zmod q) := if hpq : p = q then by substI hpq else have h1 : ((p / 2) * (q / 2)) % 2 = 0, from dvd_iff_mod_eq_zero.1 (dvd_mul_of_dvd_left (dvd_iff_mod_eq_zero.2 $ by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp1]; refl) _), begin have hp_odd : p ≠ 2 := by { by_contra, simp [h] at hp1, norm_num at hp1, }, have hpq0 : ((p : ℤ) : zmod q) ≠ 0 := by exact_mod_cast prime_ne_zero q p (ne.symm hpq), have hqp0 : ((q : ℤ) : zmod p) ≠ 0 := by exact_mod_cast prime_ne_zero p q hpq, have := quadratic_reciprocity p q hp_odd hq1 hpq, rw [neg_one_pow_eq_pow_mod_two, h1, pow_zero] at this, rw [(by norm_cast : (p : zmod q) = (p : ℤ)), (by norm_cast : (q : zmod p) = (q : ℤ)), ← legendre_sym_eq_one_iff _ hpq0, ← legendre_sym_eq_one_iff _ hqp0], cases (legendre_sym_eq_one_or_neg_one p q hqp0) with h h, { simp only [h, eq_self_iff_true, true_iff, mul_one] at this ⊢, exact this, }, { simp only [h, mul_neg, mul_one] at this ⊢, rw eq_neg_of_eq_neg this.symm, }, end lemma exists_sq_eq_prime_iff_of_mod_four_eq_three (hp3 : p % 4 = 3) (hq3 : q % 4 = 3) (hpq : p ≠ q) : is_square (q : zmod p) ↔ ¬ is_square (p : zmod q) := have h1 : ((p / 2) * (q / 2)) % 2 = 1, from nat.odd_mul_odd (by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp3]; refl) (by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hq3]; refl), begin have hp_odd : p ≠ 2 := by { by_contra, simp [h] at hp3, norm_num at hp3, }, have hq_odd : q ≠ 2 := by { by_contra, simp [h] at hq3, norm_num at hq3, }, have hpq0 : ((p : ℤ) : zmod q) ≠ 0 := by exact_mod_cast prime_ne_zero q p (ne.symm hpq), have hqp0 : ((q : ℤ) : zmod p) ≠ 0 := by exact_mod_cast prime_ne_zero p q hpq, have := quadratic_reciprocity p q hp_odd hq_odd hpq, rw [neg_one_pow_eq_pow_mod_two, h1, pow_one] at this, rw [(by norm_cast : (p : zmod q) = (p : ℤ)), (by norm_cast : (q : zmod p) = (q : ℤ)), ← legendre_sym_eq_one_iff _ hpq0, ← legendre_sym_eq_one_iff _ hqp0], cases (legendre_sym_eq_one_or_neg_one q p hpq0) with h h, { simp only [h, eq_self_iff_true, not_true, iff_false, one_mul] at this ⊢, simp only [this], norm_num, }, { simp only [h, neg_mul, one_mul, neg_inj] at this ⊢, simp only [this, eq_self_iff_true, true_iff], norm_num, }, end end zmod
a3052941cdfb02e7ed5ca69c99dad2696907ac16
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/eqndrecEtaLCNFIssue.lean
f49b34bec37b97af02be0c5e7e3ecdf4d9a39691
[ "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
658
lean
import Lean namespace Ex1 inductive Multiset (α: Type): Type def Finset (α : Type) := @Subtype (Multiset α) (λ _ => True) def Finset.insert (a : α): Finset α → Finset α := sorry def Finset.empty : Finset α := sorry structure Foo (G: Fin 0) (m: Nat) where n: Nat f: Fin m inductive Bar {G: Fin 0} : {m: Nat} → Finset (Foo G m) → Type where | insert : Bar (Finset.insert (Foo.mk n f) Γ) | empty : Bar Finset.empty def ex {G: Fin 0} : {n: Nat} → {Γ: Finset <| Foo G n} → Bar Γ → Nat | Nat.zero, _, _ => 1 | Nat.succ _, _, Bar.insert => 2 | Nat.succ _, _, _ => 3 #eval Lean.Compiler.compile #[``ex]
c1a70448e158127e5a546834b1444a15e70dbea5
efa51dd2edbbbbd6c34bd0ce436415eb405832e7
/20170116_POPL/backchain/ematch.lean
e9bc46c279ec8cf7e2e8b309d50cf5dca88f87e4
[ "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
1,366
lean
/- Lean also support E-matching: a heuristic lemma instantiation procedure that is available in many SMT solvers. Goals such as mk_mem_list can also be discharged using heuristic instantiation. -/ open list tactic universe variable u lemma in_tail {α : Type u} {a : α} (b : α) {l : list α} : a ∈ l → a ∈ b::l := mem_cons_of_mem _ lemma in_head {α : Type u} (a : α) (l : list α) : a ∈ a::l := mem_cons_self _ _ lemma in_left {α : Type u} {a : α} {l : list α} (r : list α) : a ∈ l → a ∈ l ++ r := mem_append_left _ lemma in_right {α : Type u} {a : α} (l : list α) {r : list α} : a ∈ r → a ∈ l ++ r := mem_append_right _ /- Using ematching -/ example (a b c : nat) (l₁ l₂ : list nat) : a ∈ l₁ → a ∈ b::b::c::l₂ ++ b::c::l₁ ++ [c, c, b] := begin [smt] repeat {ematch_using [in_left, in_right, in_head, in_tail], try {close}} end /- We mark lemmas for ematching. -/ attribute [ematch] in_left in_right in_head in_tail example (a b c : nat) (l₁ l₂ : list nat) : a ∈ l₁ → a ∈ b::b::c::l₂ ++ b::c::l₁ ++ [c, c, b] := begin [smt] repeat {ematch, try {close}} end /- The following example is not provable -/ example (a b c : nat) (l₁ l₂ : list nat) : a ∈ b::b::c::l₂ ++ b::c::l₁ ++ [c, c, b] := begin [smt] repeat {ematch, try {close}} end
94758b55f9cf3b98e9c9dbfd28684eb041c6b81d
bb31430994044506fa42fd667e2d556327e18dfe
/src/number_theory/zsqrtd/gaussian_int.lean
8aa9a9f7829903f1cc1dc5371c4f2d58e78cb594
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
12,564
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import number_theory.zsqrtd.basic import data.complex.basic import ring_theory.principal_ideal_domain import number_theory.legendre_symbol.quadratic_reciprocity /-! # Gaussian integers The Gaussian integers are complex integer, complex numbers whose real and imaginary parts are both integers. ## Main definitions The Euclidean domain structure on `ℤ[i]` is defined in this file. The homomorphism `to_complex` into the complex numbers is also defined in this file. ## Main statements `prime_iff_mod_four_eq_three_of_nat_prime` A prime natural number is prime in `ℤ[i]` if and only if it is `3` mod `4` ## Notations This file uses the local notation `ℤ[i]` for `gaussian_int` ## Implementation notes Gaussian integers are implemented using the more general definition `zsqrtd`, the type of integers adjoined a square root of `d`, in this case `-1`. The definition is reducible, so that properties and definitions about `zsqrtd` can easily be used. -/ open zsqrtd complex /-- The Gaussian integers, defined as `ℤ√(-1)`. -/ @[reducible] def gaussian_int : Type := zsqrtd (-1) local notation `ℤ[i]` := gaussian_int namespace gaussian_int instance : has_repr ℤ[i] := ⟨λ x, "⟨" ++ repr x.re ++ ", " ++ repr x.im ++ "⟩"⟩ instance : comm_ring ℤ[i] := zsqrtd.comm_ring section local attribute [-instance] complex.field -- Avoid making things noncomputable unnecessarily. /-- The embedding of the Gaussian integers into the complex numbers, as a ring homomorphism. -/ def to_complex : ℤ[i] →+* ℂ := zsqrtd.lift ⟨I, by simp⟩ end instance : has_coe (ℤ[i]) ℂ := ⟨to_complex⟩ lemma to_complex_def (x : ℤ[i]) : (x : ℂ) = x.re + x.im * I := rfl lemma to_complex_def' (x y : ℤ) : ((⟨x, y⟩ : ℤ[i]) : ℂ) = x + y * I := by simp [to_complex_def] lemma to_complex_def₂ (x : ℤ[i]) : (x : ℂ) = ⟨x.re, x.im⟩ := by apply complex.ext; simp [to_complex_def] @[simp] lemma to_real_re (x : ℤ[i]) : ((x.re : ℤ) : ℝ) = (x : ℂ).re := by simp [to_complex_def] @[simp] lemma to_real_im (x : ℤ[i]) : ((x.im : ℤ) : ℝ) = (x : ℂ).im := by simp [to_complex_def] @[simp] lemma to_complex_re (x y : ℤ) : ((⟨x, y⟩ : ℤ[i]) : ℂ).re = x := by simp [to_complex_def] @[simp] lemma to_complex_im (x y : ℤ) : ((⟨x, y⟩ : ℤ[i]) : ℂ).im = y := by simp [to_complex_def] @[simp] lemma to_complex_add (x y : ℤ[i]) : ((x + y : ℤ[i]) : ℂ) = x + y := to_complex.map_add _ _ @[simp] lemma to_complex_mul (x y : ℤ[i]) : ((x * y : ℤ[i]) : ℂ) = x * y := to_complex.map_mul _ _ @[simp] lemma to_complex_one : ((1 : ℤ[i]) : ℂ) = 1 := to_complex.map_one @[simp] lemma to_complex_zero : ((0 : ℤ[i]) : ℂ) = 0 := to_complex.map_zero @[simp] lemma to_complex_neg (x : ℤ[i]) : ((-x : ℤ[i]) : ℂ) = -x := to_complex.map_neg _ @[simp] lemma to_complex_sub (x y : ℤ[i]) : ((x - y : ℤ[i]) : ℂ) = x - y := to_complex.map_sub _ _ @[simp] lemma to_complex_inj {x y : ℤ[i]} : (x : ℂ) = y ↔ x = y := by cases x; cases y; simp [to_complex_def₂] @[simp] lemma to_complex_eq_zero {x : ℤ[i]} : (x : ℂ) = 0 ↔ x = 0 := by rw [← to_complex_zero, to_complex_inj] @[simp] lemma nat_cast_real_norm (x : ℤ[i]) : (x.norm : ℝ) = (x : ℂ).norm_sq := by rw [zsqrtd.norm, norm_sq]; simp @[simp] lemma nat_cast_complex_norm (x : ℤ[i]) : (x.norm : ℂ) = (x : ℂ).norm_sq := by cases x; rw [zsqrtd.norm, norm_sq]; simp lemma norm_nonneg (x : ℤ[i]) : 0 ≤ norm x := norm_nonneg (by norm_num) _ @[simp] lemma norm_eq_zero {x : ℤ[i]} : norm x = 0 ↔ x = 0 := by rw [← @int.cast_inj ℝ _ _ _]; simp lemma norm_pos {x : ℤ[i]} : 0 < norm x ↔ x ≠ 0 := by rw [lt_iff_le_and_ne, ne.def, eq_comm, norm_eq_zero]; simp [norm_nonneg] lemma abs_coe_nat_norm (x : ℤ[i]) : (x.norm.nat_abs : ℤ) = x.norm := int.nat_abs_of_nonneg (norm_nonneg _) @[simp] lemma nat_cast_nat_abs_norm {α : Type*} [ring α] (x : ℤ[i]) : (x.norm.nat_abs : α) = x.norm := by rw [← int.cast_coe_nat, abs_coe_nat_norm] lemma nat_abs_norm_eq (x : ℤ[i]) : x.norm.nat_abs = x.re.nat_abs * x.re.nat_abs + x.im.nat_abs * x.im.nat_abs := int.coe_nat_inj $ begin simp, simp [zsqrtd.norm] end instance : has_div ℤ[i] := ⟨λ x y, let n := (norm y : ℚ)⁻¹, c := y.conj in ⟨round ((x * c).re * n : ℚ), round ((x * c).im * n : ℚ)⟩⟩ lemma div_def (x y : ℤ[i]) : x / y = ⟨round ((x * conj y).re / norm y : ℚ), round ((x * conj y).im / norm y : ℚ)⟩ := show zsqrtd.mk _ _ = _, by simp [div_eq_mul_inv] lemma to_complex_div_re (x y : ℤ[i]) : ((x / y : ℤ[i]) : ℂ).re = round ((x / y : ℂ).re) := by rw [div_def, ← @rat.round_cast ℝ _ _]; simp [-rat.round_cast, mul_assoc, div_eq_mul_inv, mul_add, add_mul] lemma to_complex_div_im (x y : ℤ[i]) : ((x / y : ℤ[i]) : ℂ).im = round ((x / y : ℂ).im) := by rw [div_def, ← @rat.round_cast ℝ _ _, ← @rat.round_cast ℝ _ _]; simp [-rat.round_cast, mul_assoc, div_eq_mul_inv, mul_add, add_mul] lemma norm_sq_le_norm_sq_of_re_le_of_im_le {x y : ℂ} (hre : |x.re| ≤ |y.re|) (him : |x.im| ≤ |y.im|) : x.norm_sq ≤ y.norm_sq := by rw [norm_sq_apply, norm_sq_apply, ← _root_.abs_mul_self, _root_.abs_mul, ← _root_.abs_mul_self y.re, _root_.abs_mul y.re, ← _root_.abs_mul_self x.im, _root_.abs_mul x.im, ← _root_.abs_mul_self y.im, _root_.abs_mul y.im]; exact (add_le_add (mul_self_le_mul_self (abs_nonneg _) hre) (mul_self_le_mul_self (abs_nonneg _) him)) lemma norm_sq_div_sub_div_lt_one (x y : ℤ[i]) : ((x / y : ℂ) - ((x / y : ℤ[i]) : ℂ)).norm_sq < 1 := calc ((x / y : ℂ) - ((x / y : ℤ[i]) : ℂ)).norm_sq = ((x / y : ℂ).re - ((x / y : ℤ[i]) : ℂ).re + ((x / y : ℂ).im - ((x / y : ℤ[i]) : ℂ).im) * I : ℂ).norm_sq : congr_arg _ $ by apply complex.ext; simp ... ≤ (1 / 2 + 1 / 2 * I).norm_sq : have |(2⁻¹ : ℝ)| = 2⁻¹, from _root_.abs_of_nonneg (by norm_num), norm_sq_le_norm_sq_of_re_le_of_im_le (by rw [to_complex_div_re]; simp [norm_sq, this]; simpa using abs_sub_round (x / y : ℂ).re) (by rw [to_complex_div_im]; simp [norm_sq, this]; simpa using abs_sub_round (x / y : ℂ).im) ... < 1 : by simp [norm_sq]; norm_num instance : has_mod ℤ[i] := ⟨λ x y, x - y * (x / y)⟩ lemma mod_def (x y : ℤ[i]) : x % y = x - y * (x / y) := rfl lemma norm_mod_lt (x : ℤ[i]) {y : ℤ[i]} (hy : y ≠ 0) : (x % y).norm < y.norm := have (y : ℂ) ≠ 0, by rwa [ne.def, ← to_complex_zero, to_complex_inj], (@int.cast_lt ℝ _ _ _ _).1 $ calc ↑(zsqrtd.norm (x % y)) = (x - y * (x / y : ℤ[i]) : ℂ).norm_sq : by simp [mod_def] ... = (y : ℂ).norm_sq * (((x / y) - (x / y : ℤ[i])) : ℂ).norm_sq : by rw [← norm_sq_mul, mul_sub, mul_div_cancel' _ this] ... < (y : ℂ).norm_sq * 1 : mul_lt_mul_of_pos_left (norm_sq_div_sub_div_lt_one _ _) (norm_sq_pos.2 this) ... = zsqrtd.norm y : by simp lemma nat_abs_norm_mod_lt (x : ℤ[i]) {y : ℤ[i]} (hy : y ≠ 0) : (x % y).norm.nat_abs < y.norm.nat_abs := int.coe_nat_lt.1 (by simp [-int.coe_nat_lt, norm_mod_lt x hy]) lemma norm_le_norm_mul_left (x : ℤ[i]) {y : ℤ[i]} (hy : y ≠ 0) : (norm x).nat_abs ≤ (norm (x * y)).nat_abs := by rw [zsqrtd.norm_mul, int.nat_abs_mul]; exact le_mul_of_one_le_right (nat.zero_le _) (int.coe_nat_le.1 (by rw [abs_coe_nat_norm]; exact int.add_one_le_of_lt (norm_pos.2 hy))) instance : nontrivial ℤ[i] := ⟨⟨0, 1, dec_trivial⟩⟩ instance : euclidean_domain ℤ[i] := { quotient := (/), remainder := (%), quotient_zero := by { simp [div_def], refl }, quotient_mul_add_remainder_eq := λ _ _, by simp [mod_def], r := _, r_well_founded := measure_wf (int.nat_abs ∘ norm), remainder_lt := nat_abs_norm_mod_lt, mul_left_not_lt := λ a b hb0, not_lt_of_ge $ norm_le_norm_mul_left a hb0, .. gaussian_int.comm_ring, .. gaussian_int.nontrivial } open principal_ideal_ring lemma mod_four_eq_three_of_nat_prime_of_prime (p : ℕ) [hp : fact p.prime] (hpi : prime (p : ℤ[i])) : p % 4 = 3 := hp.1.eq_two_or_odd.elim (λ hp2, absurd hpi (mt irreducible_iff_prime.2 $ λ ⟨hu, h⟩, begin have := h ⟨1, 1⟩ ⟨1, -1⟩ (hp2.symm ▸ rfl), rw [← norm_eq_one_iff, ← norm_eq_one_iff] at this, exact absurd this dec_trivial end)) (λ hp1, by_contradiction $ λ hp3 : p % 4 ≠ 3, have hp41 : p % 4 = 1, begin rw [← nat.mod_mul_left_mod p 2 2, show 2 * 2 = 4, from rfl] at hp1, have := nat.mod_lt p (show 0 < 4, from dec_trivial), revert this hp3 hp1, generalize : p % 4 = m, dec_trivial!, end, let ⟨k, hk⟩ := zmod.exists_sq_eq_neg_one_iff.2 $ by rw hp41; exact dec_trivial in begin obtain ⟨k, k_lt_p, rfl⟩ : ∃ (k' : ℕ) (h : k' < p), (k' : zmod p) = k, { refine ⟨k.val, k.val_lt, zmod.nat_cast_zmod_val k⟩ }, have hpk : p ∣ k ^ 2 + 1, by { rw [pow_two, ← char_p.cast_eq_zero_iff (zmod p) p, nat.cast_add, nat.cast_mul, nat.cast_one, ← hk, add_left_neg], }, have hkmul : (k ^ 2 + 1 : ℤ[i]) = ⟨k, 1⟩ * ⟨k, -1⟩ := by simp [sq, zsqrtd.ext], have hpne1 : p ≠ 1 := ne_of_gt hp.1.one_lt, have hkltp : 1 + k * k < p * p, from calc 1 + k * k ≤ k + k * k : add_le_add_right (nat.pos_of_ne_zero (λ hk0, by clear_aux_decl; simp [*, pow_succ'] at *)) _ ... = k * (k + 1) : by simp [add_comm, mul_add] ... < p * p : mul_lt_mul k_lt_p k_lt_p (nat.succ_pos _) (nat.zero_le _), have hpk₁ : ¬ (p : ℤ[i]) ∣ ⟨k, -1⟩ := λ ⟨x, hx⟩, lt_irrefl (p * x : ℤ[i]).norm.nat_abs $ calc (norm (p * x : ℤ[i])).nat_abs = (zsqrtd.norm ⟨k, -1⟩).nat_abs : by rw hx ... < (norm (p : ℤ[i])).nat_abs : by simpa [add_comm, zsqrtd.norm] using hkltp ... ≤ (norm (p * x : ℤ[i])).nat_abs : norm_le_norm_mul_left _ (λ hx0, (show (-1 : ℤ) ≠ 0, from dec_trivial) $ by simpa [hx0] using congr_arg zsqrtd.im hx), have hpk₂ : ¬ (p : ℤ[i]) ∣ ⟨k, 1⟩ := λ ⟨x, hx⟩, lt_irrefl (p * x : ℤ[i]).norm.nat_abs $ calc (norm (p * x : ℤ[i])).nat_abs = (zsqrtd.norm ⟨k, 1⟩).nat_abs : by rw hx ... < (norm (p : ℤ[i])).nat_abs : by simpa [add_comm, zsqrtd.norm] using hkltp ... ≤ (norm (p * x : ℤ[i])).nat_abs : norm_le_norm_mul_left _ (λ hx0, (show (1 : ℤ) ≠ 0, from dec_trivial) $ by simpa [hx0] using congr_arg zsqrtd.im hx), have hpu : ¬ is_unit (p : ℤ[i]), from mt norm_eq_one_iff.2 (by rw [norm_nat_cast, int.nat_abs_mul, nat.mul_eq_one_iff]; exact λ h, (ne_of_lt hp.1.one_lt).symm h.1), obtain ⟨y, hy⟩ := hpk, have := hpi.2.2 ⟨k, 1⟩ ⟨k, -1⟩ ⟨y, by rw [← hkmul, ← nat.cast_mul p, ← hy]; simp⟩, clear_aux_decl, tauto end) lemma sq_add_sq_of_nat_prime_of_not_irreducible (p : ℕ) [hp : fact p.prime] (hpi : ¬irreducible (p : ℤ[i])) : ∃ a b, a^2 + b^2 = p := have hpu : ¬ is_unit (p : ℤ[i]), from mt norm_eq_one_iff.2 $ by rw [norm_nat_cast, int.nat_abs_mul, nat.mul_eq_one_iff]; exact λ h, (ne_of_lt hp.1.one_lt).symm h.1, have hab : ∃ a b, (p : ℤ[i]) = a * b ∧ ¬ is_unit a ∧ ¬ is_unit b, by simpa [irreducible_iff, hpu, not_forall, not_or_distrib] using hpi, let ⟨a, b, hpab, hau, hbu⟩ := hab in have hnap : (norm a).nat_abs = p, from ((hp.1.mul_eq_prime_sq_iff (mt norm_eq_one_iff.1 hau) (mt norm_eq_one_iff.1 hbu)).1 $ by rw [← int.coe_nat_inj', int.coe_nat_pow, sq, ← @norm_nat_cast (-1), hpab]; simp).1, ⟨a.re.nat_abs, a.im.nat_abs, by simpa [nat_abs_norm_eq, sq] using hnap⟩ lemma prime_of_nat_prime_of_mod_four_eq_three (p : ℕ) [hp : fact p.prime] (hp3 : p % 4 = 3) : prime (p : ℤ[i]) := irreducible_iff_prime.1 $ classical.by_contradiction $ λ hpi, let ⟨a, b, hab⟩ := sq_add_sq_of_nat_prime_of_not_irreducible p hpi in have ∀ a b : zmod 4, a^2 + b^2 ≠ p, by erw [← zmod.nat_cast_mod p 4, hp3]; exact dec_trivial, this a b (hab ▸ by simp) /-- A prime natural number is prime in `ℤ[i]` if and only if it is `3` mod `4` -/ lemma prime_iff_mod_four_eq_three_of_nat_prime (p : ℕ) [hp : fact p.prime] : prime (p : ℤ[i]) ↔ p % 4 = 3 := ⟨mod_four_eq_three_of_nat_prime_of_prime p, prime_of_nat_prime_of_mod_four_eq_three p⟩ end gaussian_int
5f44e72c7e2dd15576535227d4e6a49b6a527ff8
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/big_operators/finsupp.lean
a4b71c17979e10e4947157b4a748d070826f9ec2
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
22,147
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import data.finsupp.defs import algebra.big_operators.pi import algebra.big_operators.ring import algebra.big_operators.order import group_theory.submonoid.membership /-! # Big operators for finsupps This file contains theorems relevant to big operators in finitely supported functions. -/ noncomputable theory open finset function open_locale classical big_operators variables {α ι γ A B C : Type*} [add_comm_monoid A] [add_comm_monoid B] [add_comm_monoid C] variables {t : ι → A → C} (h0 : ∀ i, t i 0 = 0) (h1 : ∀ i x y, t i (x + y) = t i x + t i y) variables {s : finset α} {f : α → (ι →₀ A)} (i : ι) variables (g : ι →₀ A) (k : ι → A → γ → B) (x : γ) variables {β M M' N P G H R S : Type*} namespace finsupp /-! ### Declarations about `sum` and `prod` In most of this section, the domain `β` is assumed to be an `add_monoid`. -/ section sum_prod /-- `prod f g` is the product of `g a (f a)` over the support of `f`. -/ @[to_additive "`sum f g` is the sum of `g a (f a)` over the support of `f`. "] def prod [has_zero M] [comm_monoid N] (f : α →₀ M) (g : α → M → N) : N := ∏ a in f.support, g a (f a) variables [has_zero M] [has_zero M'] [comm_monoid N] @[to_additive] lemma prod_of_support_subset (f : α →₀ M) {s : finset α} (hs : f.support ⊆ s) (g : α → M → N) (h : ∀ i ∈ s, g i 0 = 1) : f.prod g = ∏ x in s, g x (f x) := finset.prod_subset hs $ λ x hxs hx, h x hxs ▸ congr_arg (g x) $ not_mem_support_iff.1 hx @[to_additive] lemma prod_fintype [fintype α] (f : α →₀ M) (g : α → M → N) (h : ∀ i, g i 0 = 1) : f.prod g = ∏ i, g i (f i) := f.prod_of_support_subset (subset_univ _) g (λ x _, h x) @[simp, to_additive] lemma prod_single_index {a : α} {b : M} {h : α → M → N} (h_zero : h a 0 = 1) : (single a b).prod h = h a b := calc (single a b).prod h = ∏ x in {a}, h x (single a b x) : prod_of_support_subset _ support_single_subset h $ λ x hx, (mem_singleton.1 hx).symm ▸ h_zero ... = h a b : by simp @[to_additive] lemma prod_map_range_index {f : M → M'} {hf : f 0 = 0} {g : α →₀ M} {h : α → M' → N} (h0 : ∀a, h a 0 = 1) : (map_range f hf g).prod h = g.prod (λa b, h a (f b)) := finset.prod_subset support_map_range $ λ _ _ H, by rw [not_mem_support_iff.1 H, h0] @[simp, to_additive] lemma prod_zero_index {h : α → M → N} : (0 : α →₀ M).prod h = 1 := rfl @[to_additive] lemma prod_comm (f : α →₀ M) (g : β →₀ M') (h : α → M → β → M' → N) : f.prod (λ x v, g.prod (λ x' v', h x v x' v')) = g.prod (λ x' v', f.prod (λ x v, h x v x' v')) := finset.prod_comm @[simp, to_additive] lemma prod_ite_eq [decidable_eq α] (f : α →₀ M) (a : α) (b : α → M → N) : f.prod (λ x v, ite (a = x) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by { dsimp [finsupp.prod], rw f.support.prod_ite_eq, } @[simp] lemma sum_ite_self_eq [decidable_eq α] {N : Type*} [add_comm_monoid N] (f : α →₀ N) (a : α) : f.sum (λ x v, ite (a = x) v 0) = f a := by { convert f.sum_ite_eq a (λ x, id), simp [ite_eq_right_iff.2 eq.symm] } /-- A restatement of `prod_ite_eq` with the equality test reversed. -/ @[simp, to_additive "A restatement of `sum_ite_eq` with the equality test reversed."] lemma prod_ite_eq' [decidable_eq α] (f : α →₀ M) (a : α) (b : α → M → N) : f.prod (λ x v, ite (x = a) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by { dsimp [finsupp.prod], rw f.support.prod_ite_eq', } @[simp] lemma sum_ite_self_eq' [decidable_eq α] {N : Type*} [add_comm_monoid N] (f : α →₀ N) (a : α) : f.sum (λ x v, ite (x = a) v 0) = f a := by { convert f.sum_ite_eq' a (λ x, id), simp [ite_eq_right_iff.2 eq.symm] } @[simp] lemma prod_pow [fintype α] (f : α →₀ ℕ) (g : α → N) : f.prod (λ a b, g a ^ b) = ∏ a, g a ^ (f a) := f.prod_fintype _ $ λ a, pow_zero _ /-- If `g` maps a second argument of 0 to 1, then multiplying it over the result of `on_finset` is the same as multiplying it over the original `finset`. -/ @[to_additive "If `g` maps a second argument of 0 to 0, summing it over the result of `on_finset` is the same as summing it over the original `finset`."] lemma on_finset_prod {s : finset α} {f : α → M} {g : α → M → N} (hf : ∀a, f a ≠ 0 → a ∈ s) (hg : ∀ a, g a 0 = 1) : (on_finset s f hf).prod g = ∏ a in s, g a (f a) := finset.prod_subset support_on_finset_subset $ by simp [*] { contextual := tt } /-- Taking a product over `f : α →₀ M` is the same as multiplying the value on a single element `y ∈ f.support` by the product over `erase y f`. -/ @[to_additive /-" Taking a sum over over `f : α →₀ M` is the same as adding the value on a single element `y ∈ f.support` to the sum over `erase y f`. "-/] lemma mul_prod_erase (f : α →₀ M) (y : α) (g : α → M → N) (hyf : y ∈ f.support) : g y (f y) * (erase y f).prod g = f.prod g := begin rw [finsupp.prod, finsupp.prod, ←finset.mul_prod_erase _ _ hyf, finsupp.support_erase, finset.prod_congr rfl], intros h hx, rw finsupp.erase_ne (ne_of_mem_erase hx), end /-- Generalization of `finsupp.mul_prod_erase`: if `g` maps a second argument of 0 to 1, then its product over `f : α →₀ M` is the same as multiplying the value on any element `y : α` by the product over `erase y f`. -/ @[to_additive /-" Generalization of `finsupp.add_sum_erase`: if `g` maps a second argument of 0 to 0, then its sum over `f : α →₀ M` is the same as adding the value on any element `y : α` to the sum over `erase y f`. "-/] lemma mul_prod_erase' (f : α →₀ M) (y : α) (g : α → M → N) (hg : ∀ (i : α), g i 0 = 1) : g y (f y) * (erase y f).prod g = f.prod g := begin classical, by_cases hyf : y ∈ f.support, { exact finsupp.mul_prod_erase f y g hyf }, { rw [not_mem_support_iff.mp hyf, hg y, erase_of_not_mem_support hyf, one_mul] }, end @[to_additive] lemma _root_.submonoid_class.finsupp_prod_mem {S : Type*} [set_like S N] [submonoid_class S N] (s : S) (f : α →₀ M) (g : α → M → N) (h : ∀ c, f c ≠ 0 → g c (f c) ∈ s) : f.prod g ∈ s := prod_mem $ λ i hi, h _ (finsupp.mem_support_iff.mp hi) @[to_additive] lemma prod_congr {f : α →₀ M} {g1 g2 : α → M → N} (h : ∀ x ∈ f.support, g1 x (f x) = g2 x (f x)) : f.prod g1 = f.prod g2 := finset.prod_congr rfl h end sum_prod end finsupp @[to_additive] lemma map_finsupp_prod [has_zero M] [comm_monoid N] [comm_monoid P] {H : Type*} [monoid_hom_class H N P] (h : H) (f : α →₀ M) (g : α → M → N) : h (f.prod g) = f.prod (λ a b, h (g a b)) := map_prod h _ _ /-- Deprecated, use `_root_.map_finsupp_prod` instead. -/ @[to_additive "Deprecated, use `_root_.map_finsupp_sum` instead."] protected lemma mul_equiv.map_finsupp_prod [has_zero M] [comm_monoid N] [comm_monoid P] (h : N ≃* P) (f : α →₀ M) (g : α → M → N) : h (f.prod g) = f.prod (λ a b, h (g a b)) := map_finsupp_prod h f g /-- Deprecated, use `_root_.map_finsupp_prod` instead. -/ @[to_additive "Deprecated, use `_root_.map_finsupp_sum` instead."] protected lemma monoid_hom.map_finsupp_prod [has_zero M] [comm_monoid N] [comm_monoid P] (h : N →* P) (f : α →₀ M) (g : α → M → N) : h (f.prod g) = f.prod (λ a b, h (g a b)) := map_finsupp_prod h f g /-- Deprecated, use `_root_.map_finsupp_sum` instead. -/ protected lemma ring_hom.map_finsupp_sum [has_zero M] [semiring R] [semiring S] (h : R →+* S) (f : α →₀ M) (g : α → M → R) : h (f.sum g) = f.sum (λ a b, h (g a b)) := map_finsupp_sum h f g /-- Deprecated, use `_root_.map_finsupp_prod` instead. -/ protected lemma ring_hom.map_finsupp_prod [has_zero M] [comm_semiring R] [comm_semiring S] (h : R →+* S) (f : α →₀ M) (g : α → M → R) : h (f.prod g) = f.prod (λ a b, h (g a b)) := map_finsupp_prod h f g @[to_additive] lemma monoid_hom.coe_finsupp_prod [has_zero β] [monoid N] [comm_monoid P] (f : α →₀ β) (g : α → β → N →* P) : ⇑(f.prod g) = f.prod (λ i fi, g i fi) := monoid_hom.coe_finset_prod _ _ @[simp, to_additive] lemma monoid_hom.finsupp_prod_apply [has_zero β] [monoid N] [comm_monoid P] (f : α →₀ β) (g : α → β → N →* P) (x : N) : f.prod g x = f.prod (λ i fi, g i fi x) := monoid_hom.finset_prod_apply _ _ _ namespace finsupp lemma single_multiset_sum [add_comm_monoid M] (s : multiset M) (a : α) : single a s.sum = (s.map (single a)).sum := multiset.induction_on s (single_zero _) $ λ a s ih, by rw [multiset.sum_cons, single_add, ih, multiset.map_cons, multiset.sum_cons] lemma single_finset_sum [add_comm_monoid M] (s : finset ι) (f : ι → M) (a : α) : single a (∑ b in s, f b) = ∑ b in s, single a (f b) := begin transitivity, apply single_multiset_sum, rw [multiset.map_map], refl end lemma single_sum [has_zero M] [add_comm_monoid N] (s : ι →₀ M) (f : ι → M → N) (a : α) : single a (s.sum f) = s.sum (λd c, single a (f d c)) := single_finset_sum _ _ _ @[to_additive] lemma prod_neg_index [add_group G] [comm_monoid M] {g : α →₀ G} {h : α → G → M} (h0 : ∀a, h a 0 = 1) : (-g).prod h = g.prod (λa b, h a (- b)) := prod_map_range_index h0 end finsupp namespace finsupp lemma finset_sum_apply [add_comm_monoid N] (S : finset ι) (f : ι → α →₀ N) (a : α) : (∑ i in S, f i) a = ∑ i in S, f i a := (apply_add_hom a : (α →₀ N) →+ _).map_sum _ _ @[simp] lemma sum_apply [has_zero M] [add_comm_monoid N] {f : α →₀ M} {g : α → M → β →₀ N} {a₂ : β} : (f.sum g) a₂ = f.sum (λa₁ b, g a₁ b a₂) := finset_sum_apply _ _ _ lemma coe_finset_sum [add_comm_monoid N] (S : finset ι) (f : ι → α →₀ N) : ⇑(∑ i in S, f i) = ∑ i in S, f i := (coe_fn_add_hom : (α →₀ N) →+ _).map_sum _ _ lemma coe_sum [has_zero M] [add_comm_monoid N] (f : α →₀ M) (g : α → M → β →₀ N) : ⇑(f.sum g) = f.sum (λ a₁ b, g a₁ b) := coe_finset_sum _ _ lemma support_sum [decidable_eq β] [has_zero M] [add_comm_monoid N] {f : α →₀ M} {g : α → M → (β →₀ N)} : (f.sum g).support ⊆ f.support.bUnion (λa, (g a (f a)).support) := have ∀ c, f.sum (λ a b, g a b c) ≠ 0 → (∃ a, f a ≠ 0 ∧ ¬ (g a (f a)) c = 0), from assume a₁ h, let ⟨a, ha, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in ⟨a, mem_support_iff.mp ha, ne⟩, by simpa only [finset.subset_iff, mem_support_iff, finset.mem_bUnion, sum_apply, exists_prop] lemma support_finset_sum [decidable_eq β] [add_comm_monoid M] {s : finset α} {f : α → (β →₀ M)} : (finset.sum s f).support ⊆ s.bUnion (λ x, (f x).support) := begin rw ←finset.sup_eq_bUnion, induction s using finset.cons_induction_on with a s ha ih, { refl }, { rw [finset.sum_cons, finset.sup_cons], exact support_add.trans (finset.union_subset_union (finset.subset.refl _) ih), }, end @[simp] lemma sum_zero [has_zero M] [add_comm_monoid N] {f : α →₀ M} : f.sum (λa b, (0 : N)) = 0 := finset.sum_const_zero @[simp, to_additive] lemma prod_mul [has_zero M] [comm_monoid N] {f : α →₀ M} {h₁ h₂ : α → M → N} : f.prod (λa b, h₁ a b * h₂ a b) = f.prod h₁ * f.prod h₂ := finset.prod_mul_distrib @[simp, to_additive] lemma prod_inv [has_zero M] [comm_group G] {f : α →₀ M} {h : α → M → G} : f.prod (λa b, (h a b)⁻¹) = (f.prod h)⁻¹ := (map_prod ((monoid_hom.id G)⁻¹) _ _).symm @[simp] lemma sum_sub [has_zero M] [add_comm_group G] {f : α →₀ M} {h₁ h₂ : α → M → G} : f.sum (λa b, h₁ a b - h₂ a b) = f.sum h₁ - f.sum h₂ := finset.sum_sub_distrib /-- Taking the product under `h` is an additive-to-multiplicative homomorphism of finsupps, if `h` is an additive-to-multiplicative homomorphism on the support. This is a more general version of `finsupp.prod_add_index'`; the latter has simpler hypotheses. -/ @[to_additive "Taking the product under `h` is an additive homomorphism of finsupps, if `h` is an additive homomorphism on the support. This is a more general version of `finsupp.sum_add_index'`; the latter has simpler hypotheses."] lemma prod_add_index [add_zero_class M] [comm_monoid N] {f g : α →₀ M} {h : α → M → N} (h_zero : ∀ a ∈ f.support ∪ g.support, h a 0 = 1) (h_add : ∀ (a ∈ f.support ∪ g.support) b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f + g).prod h = f.prod h * g.prod h := begin rw [finsupp.prod_of_support_subset f (subset_union_left _ g.support) h h_zero, finsupp.prod_of_support_subset g (subset_union_right f.support _) h h_zero, ←finset.prod_mul_distrib, finsupp.prod_of_support_subset (f + g) finsupp.support_add h h_zero], exact finset.prod_congr rfl (λ x hx, (by apply h_add x hx)), end /-- Taking the product under `h` is an additive-to-multiplicative homomorphism of finsupps, if `h` is an additive-to-multiplicative homomorphism. This is a more specialized version of `finsupp.prod_add_index` with simpler hypotheses. -/ @[to_additive "Taking the sum under `h` is an additive homomorphism of finsupps, if `h` is an additive homomorphism. This is a more specific version of `finsupp.sum_add_index` with simpler hypotheses."] lemma prod_add_index' [add_zero_class M] [comm_monoid N] {f g : α →₀ M} {h : α → M → N} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f + g).prod h = f.prod h * g.prod h := prod_add_index (λ a ha, h_zero a) (λ a ha, h_add a) @[simp] lemma sum_hom_add_index [add_zero_class M] [add_comm_monoid N] {f g : α →₀ M} (h : α → M →+ N) : (f + g).sum (λ x, h x) = f.sum (λ x, h x) + g.sum (λ x, h x) := sum_add_index' (λ a, (h a).map_zero) (λ a, (h a).map_add) @[simp] lemma prod_hom_add_index [add_zero_class M] [comm_monoid N] {f g : α →₀ M} (h : α → multiplicative M →* N) : (f + g).prod (λ a b, h a (multiplicative.of_add b)) = f.prod (λ a b, h a (multiplicative.of_add b)) * g.prod (λ a b, h a (multiplicative.of_add b)) := prod_add_index' (λ a, (h a).map_one) (λ a, (h a).map_mul) /-- The canonical isomorphism between families of additive monoid homomorphisms `α → (M →+ N)` and monoid homomorphisms `(α →₀ M) →+ N`. -/ def lift_add_hom [add_zero_class M] [add_comm_monoid N] : (α → M →+ N) ≃+ ((α →₀ M) →+ N) := { to_fun := λ F, { to_fun := λ f, f.sum (λ x, F x), map_zero' := finset.sum_empty, map_add' := λ _ _, sum_add_index' (λ x, (F x).map_zero) (λ x, (F x).map_add) }, inv_fun := λ F x, F.comp $ single_add_hom x, left_inv := λ F, by { ext, simp }, right_inv := λ F, by { ext, simp }, map_add' := λ F G, by { ext, simp } } @[simp] lemma lift_add_hom_apply [add_comm_monoid M] [add_comm_monoid N] (F : α → M →+ N) (f : α →₀ M) : lift_add_hom F f = f.sum (λ x, F x) := rfl @[simp] lemma lift_add_hom_symm_apply [add_comm_monoid M] [add_comm_monoid N] (F : (α →₀ M) →+ N) (x : α) : lift_add_hom.symm F x = F.comp (single_add_hom x) := rfl lemma lift_add_hom_symm_apply_apply [add_comm_monoid M] [add_comm_monoid N] (F : (α →₀ M) →+ N) (x : α) (y : M) : lift_add_hom.symm F x y = F (single x y) := rfl @[simp] lemma lift_add_hom_single_add_hom [add_comm_monoid M] : lift_add_hom (single_add_hom : α → M →+ α →₀ M) = add_monoid_hom.id _ := lift_add_hom.to_equiv.apply_eq_iff_eq_symm_apply.2 rfl @[simp] lemma sum_single [add_comm_monoid M] (f : α →₀ M) : f.sum single = f := add_monoid_hom.congr_fun lift_add_hom_single_add_hom f @[simp] lemma sum_univ_single [add_comm_monoid M] [fintype α] (i : α) (m : M) : ∑ (j : α), (single i m) j = m := by simp [single] @[simp] lemma sum_univ_single' [add_comm_monoid M] [fintype α] (i : α) (m : M) : ∑ (j : α), (single j m) i = m := by simp [single] @[simp] lemma lift_add_hom_apply_single [add_comm_monoid M] [add_comm_monoid N] (f : α → M →+ N) (a : α) (b : M) : lift_add_hom f (single a b) = f a b := sum_single_index (f a).map_zero @[simp] lemma lift_add_hom_comp_single [add_comm_monoid M] [add_comm_monoid N] (f : α → M →+ N) (a : α) : (lift_add_hom f).comp (single_add_hom a) = f a := add_monoid_hom.ext $ λ b, lift_add_hom_apply_single f a b lemma comp_lift_add_hom [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] (g : N →+ P) (f : α → M →+ N) : g.comp (lift_add_hom f) = lift_add_hom (λ a, g.comp (f a)) := lift_add_hom.symm_apply_eq.1 $ funext $ λ a, by rw [lift_add_hom_symm_apply, add_monoid_hom.comp_assoc, lift_add_hom_comp_single] lemma sum_sub_index [add_comm_group β] [add_comm_group γ] {f g : α →₀ β} {h : α → β → γ} (h_sub : ∀a b₁ b₂, h a (b₁ - b₂) = h a b₁ - h a b₂) : (f - g).sum h = f.sum h - g.sum h := (lift_add_hom (λ a, add_monoid_hom.of_map_sub (h a) (h_sub a))).map_sub f g @[to_additive] lemma prod_emb_domain [has_zero M] [comm_monoid N] {v : α →₀ M} {f : α ↪ β} {g : β → M → N} : (v.emb_domain f).prod g = v.prod (λ a b, g (f a) b) := begin rw [prod, prod, support_emb_domain, finset.prod_map], simp_rw emb_domain_apply, end @[to_additive] lemma prod_finset_sum_index [add_comm_monoid M] [comm_monoid N] {s : finset ι} {g : ι → α →₀ M} {h : α → M → N} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : ∏ i in s, (g i).prod h = (∑ i in s, g i).prod h := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, sum_insert has, prod_add_index' h_zero h_add] @[to_additive] lemma prod_sum_index [add_comm_monoid M] [add_comm_monoid N] [comm_monoid P] {f : α →₀ M} {g : α → M → β →₀ N} {h : β → N → P} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f.sum g).prod h = f.prod (λa b, (g a b).prod h) := (prod_finset_sum_index h_zero h_add).symm lemma multiset_sum_sum_index [add_comm_monoid M] [add_comm_monoid N] (f : multiset (α →₀ M)) (h : α → M → N) (h₀ : ∀a, h a 0 = 0) (h₁ : ∀ (a : α) (b₁ b₂ : M), h a (b₁ + b₂) = h a b₁ + h a b₂) : (f.sum.sum h) = (f.map $ λg:α →₀ M, g.sum h).sum := multiset.induction_on f rfl $ assume a s ih, by rw [multiset.sum_cons, multiset.map_cons, multiset.sum_cons, sum_add_index' h₀ h₁, ih] lemma support_sum_eq_bUnion {α : Type*} {ι : Type*} {M : Type*} [add_comm_monoid M] {g : ι → α →₀ M} (s : finset ι) (h : ∀ i₁ i₂, i₁ ≠ i₂ → disjoint (g i₁).support (g i₂).support) : (∑ i in s, g i).support = s.bUnion (λ i, (g i).support) := begin apply finset.induction_on s, { simp }, { intros i s hi, simp only [hi, sum_insert, not_false_iff, bUnion_insert], intro hs, rw [finsupp.support_add_eq, hs], rw [hs, finset.disjoint_bUnion_right], intros j hj, refine h _ _ (ne_of_mem_of_not_mem hj hi).symm } end lemma multiset_map_sum [has_zero M] {f : α →₀ M} {m : β → γ} {h : α → M → multiset β} : multiset.map m (f.sum h) = f.sum (λa b, (h a b).map m) := (multiset.map_add_monoid_hom m).map_sum _ f.support lemma multiset_sum_sum [has_zero M] [add_comm_monoid N] {f : α →₀ M} {h : α → M → multiset N} : multiset.sum (f.sum h) = f.sum (λa b, multiset.sum (h a b)) := (multiset.sum_add_monoid_hom : multiset N →+ N).map_sum _ f.support /-- For disjoint `f1` and `f2`, and function `g`, the product of the products of `g` over `f1` and `f2` equals the product of `g` over `f1 + f2` -/ @[to_additive "For disjoint `f1` and `f2`, and function `g`, the sum of the sums of `g` over `f1` and `f2` equals the sum of `g` over `f1 + f2`"] lemma prod_add_index_of_disjoint [add_comm_monoid M] {f1 f2 : α →₀ M} (hd : disjoint f1.support f2.support) {β : Type*} [comm_monoid β] (g : α → M → β) : (f1 + f2).prod g = f1.prod g * f2.prod g := have ∀ {f1 f2 : α →₀ M}, disjoint f1.support f2.support → ∏ x in f1.support, g x (f1 x + f2 x) = f1.prod g := λ f1 f2 hd, finset.prod_congr rfl (λ x hx, by simp only [not_mem_support_iff.mp (disjoint_left.mp hd hx), add_zero]), by simp_rw [← this hd, ← this hd.symm, add_comm (f2 _), finsupp.prod, support_add_eq hd, prod_union hd, add_apply] lemma prod_dvd_prod_of_subset_of_dvd [add_comm_monoid M] [comm_monoid N] {f1 f2 : α →₀ M} {g1 g2 : α → M → N} (h1 : f1.support ⊆ f2.support) (h2 : ∀ (a : α), a ∈ f1.support → g1 a (f1 a) ∣ g2 a (f2 a)) : f1.prod g1 ∣ f2.prod g2 := begin simp only [finsupp.prod, finsupp.prod_mul], rw [←sdiff_union_of_subset h1, prod_union sdiff_disjoint], apply dvd_mul_of_dvd_right, apply prod_dvd_prod_of_dvd, exact h2, end end finsupp theorem finset.sum_apply' : (∑ k in s, f k) i = ∑ k in s, f k i := (finsupp.apply_add_hom i : (ι →₀ A) →+ A).map_sum f s theorem finsupp.sum_apply' : g.sum k x = g.sum (λ i b, k i b x) := finset.sum_apply _ _ _ section include h0 h1 open_locale classical theorem finsupp.sum_sum_index' : (∑ x in s, f x).sum t = ∑ x in s, (f x).sum t := finset.induction_on s rfl $ λ a s has ih, by simp_rw [finset.sum_insert has, finsupp.sum_add_index' h0 h1, ih] end section variables [non_unital_non_assoc_semiring R] [non_unital_non_assoc_semiring S] lemma finsupp.sum_mul (b : S) (s : α →₀ R) {f : α → R → S} : (s.sum f) * b = s.sum (λ a c, (f a c) * b) := by simp only [finsupp.sum, finset.sum_mul] lemma finsupp.mul_sum (b : S) (s : α →₀ R) {f : α → R → S} : b * (s.sum f) = s.sum (λ a c, b * (f a c)) := by simp only [finsupp.sum, finset.mul_sum] end namespace nat /-- If `0 : ℕ` is not in the support of `f : ℕ →₀ ℕ` then `0 < ∏ x in f.support, x ^ (f x)`. -/ lemma prod_pow_pos_of_zero_not_mem_support {f : ℕ →₀ ℕ} (hf : 0 ∉ f.support) : 0 < f.prod pow := finset.prod_pos (λ a ha, pos_iff_ne_zero.mpr (pow_ne_zero _ (λ H, by {subst H, exact hf ha}))) end nat
0e6a6f146e3d469a1996324d8fc585b7ea501823
26ac254ecb57ffcb886ff709cf018390161a9225
/src/logic/basic.lean
8a6ded64b1158b584bef0fb05581c4339bf43ed4
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
39,733
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 /-! # 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. -/ 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 variables {α : Type*} {β : Type*} @[reducible] def hidden {α : Sort*} {a : α} := a 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 [subsingleton α] (x y : α) : x = y ↔ true := by cc /-- 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 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] def fact (p : Prop) := p 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` -/ 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 @[simp] theorem imp_iff_right (ha : a) : (a → b) ↔ b := ⟨λf, f ha, imp_intro⟩ /-! ### Declarations about `not` -/ 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 by_contradiction {p} [decidable p] : (¬p → false) → p := decidable.by_contradiction theorem not_not [decidable a] : ¬¬a ↔ a := iff.intro by_contradiction not_not_intro theorem of_not_not [decidable a] : ¬¬a → a := by_contradiction theorem of_not_imp [decidable a] (h : ¬ (a → b)) : a := by_contradiction (not_not_of_not_imp h) theorem not.imp_symm [decidable a] (h : ¬a → b) (hb : ¬b) : a := by_contradiction $ hb ∘ h theorem not_imp_comm [decidable a] [decidable b] : (¬a → b) ↔ (¬b → a) := ⟨not.imp_symm, not.imp_symm⟩ theorem imp.swap : (a → b → c) ↔ (b → a → c) := ⟨function.swap, function.swap⟩ theorem imp_not_comm : (a → ¬b) ↔ (b → ¬a) := imp.swap /-! ### Declarations about `and` -/ theorem not_and_of_not_left (b : Prop) : ¬a → ¬(a ∧ b) := mt and.left theorem not_and_of_not_right (a : Prop) {b : Prop} : ¬b → ¬(a ∧ b) := mt and.right theorem and.imp_left (h : a → b) : a ∧ c → b ∧ c := and.imp h id theorem and.imp_right (h : a → b) : c ∧ a → c ∧ b := and.imp id h lemma and.right_comm : (a ∧ b) ∧ c ↔ (a ∧ c) ∧ b := by simp [and.left_comm, and.comm] lemma and.rotate : a ∧ b ∧ c ↔ b ∧ c ∧ a := by simp [and.left_comm, and.comm] theorem and_not_self_iff (a : Prop) : a ∧ ¬ a ↔ false := iff.intro (assume h, (h.right) (h.left)) (assume h, h.elim) theorem not_and_self_iff (a : Prop) : ¬ a ∧ a ↔ false := iff.intro (assume ⟨hna, ha⟩, hna ha) false.elim theorem and_iff_left_of_imp {a b : Prop} (h : a → b) : (a ∧ b) ↔ a := iff.intro and.left (λ ha, ⟨ha, h ha⟩) theorem and_iff_right_of_imp {a b : Prop} (h : b → a) : (a ∧ b) ↔ b := iff.intro and.right (λ hb, ⟨h hb, hb⟩) lemma and.congr_right_iff : (a ∧ b ↔ a ∧ c) ↔ (a → (b ↔ c)) := ⟨λ h ha, by simp [ha] at h; exact h, and_congr_right⟩ @[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_of_or_of_imp_of_imp (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → d) : c ∨ d := or.imp h₂ h₃ h₁ theorem or_of_or_of_imp_left (h₁ : a ∨ c) (h : a → b) : b ∨ c := or.imp_left h h₁ theorem or_of_or_of_imp_right (h₁ : c ∨ a) (h : a → b) : c ∨ b := or.imp_right h h₁ theorem or.elim3 (h : a ∨ b ∨ c) (ha : a → d) (hb : b → d) (hc : c → d) : d := or.elim h ha (assume h₂, or.elim h₂ hb hc) theorem or_imp_distrib : (a ∨ b → c) ↔ (a → c) ∧ (b → c) := ⟨assume h, ⟨assume ha, h (or.inl ha), assume hb, h (or.inr hb)⟩, assume ⟨ha, hb⟩, or.rec ha hb⟩ theorem or_iff_not_imp_left [decidable a] : a ∨ b ↔ (¬ a → b) := ⟨or.resolve_left, λ h, dite _ or.inl (or.inr ∘ h)⟩ theorem or_iff_not_imp_right [decidable b] : a ∨ b ↔ (¬ b → a) := or.comm.trans or_iff_not_imp_left theorem not_imp_not [decidable a] : (¬ a → ¬ b) ↔ (b → a) := ⟨assume h hb, by_contradiction $ assume na, h na hb, mt⟩ /-! ### Declarations about distributivity -/ theorem and_or_distrib_left : a ∧ (b ∨ c) ↔ (a ∧ b) ∨ (a ∧ c) := ⟨λ ⟨ha, hbc⟩, hbc.imp (and.intro ha) (and.intro ha), or.rec (and.imp_right or.inl) (and.imp_right or.inr)⟩ theorem or_and_distrib_right : (a ∨ b) ∧ c ↔ (a ∧ c) ∨ (b ∧ c) := (and.comm.trans and_or_distrib_left).trans (or_congr and.comm and.comm) theorem or_and_distrib_left : a ∨ (b ∧ c) ↔ (a ∨ b) ∧ (a ∨ c) := ⟨or.rec (λha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr), and.rec $ or.rec (imp_intro ∘ or.inl) (or.imp_right ∘ and.intro)⟩ theorem and_or_distrib_right : (a ∧ b) ∨ c ↔ (a ∨ c) ∧ (b ∨ c) := (or.comm.trans or_and_distrib_left).trans (and_congr or.comm or.comm) @[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) theorem not_or_of_imp [decidable a] (h : a → b) : ¬ a ∨ b := if ha : a then or.inr (h ha) else or.inl ha theorem imp_iff_not_or [decidable a] : (a → b) ↔ (¬ a ∨ b) := ⟨not_or_of_imp, or.neg_resolve_left⟩ theorem imp_or_distrib [decidable a] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by simp [imp_iff_not_or, or.comm, or.left_comm] theorem imp_or_distrib' [decidable b] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by by_cases b; simp [h, or_iff_right_of_imp ((∘) false.elim)] theorem not_imp_of_and_not : a ∧ ¬ b → ¬ (a → b) | ⟨ha, hb⟩ h := hb $ h ha theorem not_imp [decidable a] : ¬(a → b) ↔ a ∧ ¬b := ⟨λ h, ⟨of_not_imp h, not_of_not_imp h⟩, not_imp_of_and_not⟩ -- for monotonicity lemma imp_imp_imp (h₀ : c → a) (h₁ : b → d) : (a → b) → (c → d) := assume (h₂ : a → b), h₁ ∘ h₂ ∘ h₀ theorem peirce (a b : Prop) [decidable a] : ((a → b) → a) → a := if ha : a then λ h, ha else λ h, h ha.elim theorem peirce' {a : Prop} (H : ∀ b : Prop, (a → b) → a) : a := H _ id theorem not_iff_not [decidable a] [decidable b] : (¬ a ↔ ¬ b) ↔ (a ↔ b) := by rw [@iff_def (¬ a), @iff_def' a]; exact and_congr not_imp_not not_imp_not theorem not_iff_comm [decidable a] [decidable b] : (¬ a ↔ b) ↔ (¬ b ↔ a) := by rw [@iff_def (¬ a), @iff_def (¬ b)]; exact and_congr not_imp_comm imp_not_comm theorem not_iff [decidable b] : ¬ (a ↔ b) ↔ (¬ a ↔ b) := by split; intro h; [split, skip]; intro h'; [by_contradiction,intro,skip]; try { refine h _; simp [*] }; rw [h',not_iff_self] at h; exact h theorem iff_not_comm [decidable a] [decidable b] : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := by rw [@iff_def a, @iff_def b]; exact and_congr imp_not_comm not_imp_comm theorem iff_iff_and_or_not_and_not [decidable b] : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) := by { split; intro h, { rw h; by_cases b; [left,right]; split; assumption }, { cases h with h h; cases h; split; intro; { contradiction <|> assumption } } } theorem not_and_not_right [decidable b] : ¬(a ∧ ¬b) ↔ (a → b) := ⟨λ h ha, h.imp_symm $ and.intro ha, λ h ⟨ha, hb⟩, hb $ h ha⟩ @[inline] def decidable_of_iff (a : Prop) (h : a ↔ b) [D : decidable a] : decidable b := decidable_of_decidable_of_iff D h @[inline] def decidable_of_iff' (b : Prop) (h : a ↔ b) [D : decidable b] : decidable a := decidable_of_decidable_of_iff D h.symm def decidable_of_bool : ∀ (b : bool) (h : b ↔ a), decidable a | tt h := is_true (h.1 rfl) | ff h := is_false (mt h.2 bool.ff_ne_tt) /-! ### De Morgan's laws -/ theorem not_and_of_not_or_not (h : ¬ a ∨ ¬ b) : ¬ (a ∧ b) | ⟨ha, hb⟩ := or.elim h (absurd ha) (absurd hb) theorem not_and_distrib [decidable a] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if ha : a then or.inr (λ hb, h ⟨ha, hb⟩) else or.inl ha, not_and_of_not_or_not⟩ theorem not_and_distrib' [decidable b] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if hb : b then or.inl (λ ha, h ⟨ha, hb⟩) else or.inr hb, not_and_of_not_or_not⟩ @[simp] theorem not_and : ¬ (a ∧ b) ↔ (a → ¬ b) := and_imp theorem not_and' : ¬ (a ∧ b) ↔ b → ¬a := not_and.trans imp_not_comm theorem not_or_distrib : ¬ (a ∨ b) ↔ ¬ a ∧ ¬ b := ⟨λ h, ⟨λ ha, h (or.inl ha), λ hb, h (or.inr hb)⟩, λ ⟨h₁, h₂⟩ h, or.elim h h₁ h₂⟩ theorem or_iff_not_and_not [decidable a] [decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) := by rw [← not_or_distrib, not_not] theorem and_iff_not_or_not [decidable a] [decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := by rw [← not_and_distrib, not_not] end propositional /-! ### 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 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_rfl {α : Sort*} {a : α} : eq.mp (eq.refl α) a = a := rfl @[simp] lemma eq_mpr_rfl {α : Sort*} {a : α} : eq.mpr (eq.refl α) a = a := rfl lemma heq_of_eq_mp : ∀ {α β : Sort*} {a : α} {a' : β} (e : α = β) (h₂ : (eq.mp e a) = a'), a == a' | α ._ a a' rfl h := eq.rec_on h (heq.refl _) lemma rec_heq_of_heq {β} {C : α → Sort*} {x : C a} {y : β} (eq : a = b) (h : x == y) : @eq.rec α a C x b eq == y := by subst eq; exact h @[simp] lemma {u} eq_mpr_heq {α β : Sort u} (h : β = α) (x : α) : eq.mpr h x == x := by subst h; refl 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 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'⟩) 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) theorem not_forall {p : α → Prop} [decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := ⟨not.imp_symm $ λ nx x, nx.imp_symm $ λ h, ⟨x, h⟩, not_forall_of_exists_not⟩ theorem not_forall_not [decidable (∃ x, p x)] : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := (@not_iff_comm _ _ _ (decidable_of_iff (¬ ∃ x, p x) not_exists)).1 not_exists theorem not_exists_not [∀ x, decidable (p x)] : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := by simp [not_not] @[simp] theorem forall_true_iff : (α → true) ↔ true := iff_true_intro (λ _, trivial) -- Unfortunately this causes simp to loop sometimes, so we -- add the 2 and 3 cases as simp lemmas instead theorem forall_true_iff' (h : ∀ a, p a ↔ true) : (∀ a, p a) ↔ true := iff_true_intro (λ _, of_iff_true (h _)) @[simp] theorem forall_2_true_iff {β : α → Sort*} : (∀ a, β a → true) ↔ true := forall_true_iff' $ λ _, forall_true_iff @[simp] theorem forall_3_true_iff {β : α → Sort*} {γ : Π a, β a → Sort*} : (∀ a (b : β a), γ a b → true) ↔ true := forall_true_iff' $ λ _, forall_2_true_iff @[simp] theorem forall_const (α : Sort*) [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 exists_eq {a' : α} : ∃ a, a = a' := ⟨_, rfl⟩ @[simp] theorem exists_eq' {a' : α} : Exists (eq 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_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] theorem forall_eq' {a' : α} : (∀a, a' = a → p a) ↔ p a' := by simp [@eq_comm _ a'] @[simp] theorem exists_eq_left' {a' : α} : (∃ a, a' = a ∧ p a) ↔ p a' := by simp [@eq_comm _ a'] @[simp] theorem exists_eq_right' {a' : α} : (∃ a, p a ∧ a' = a) ↔ p a' := by simp [@eq_comm _ a'] theorem 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 theorem forall_or_distrib_left {q : Prop} {p : α → Prop} [decidable q] : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := ⟨λ h, if hq : q then or.inl hq else or.inr $ λ x, (h x).resolve_left hq, forall_or_of_or_forall⟩ theorem forall_or_distrib_right {q : Prop} {p : α → Prop} [decidable q] : (∀x, p x ∨ q) ↔ (∀x, p x) ∨ q := by simp [or_comm, forall_or_distrib_left] /-- 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 theorem Exists.fst {p : b → Prop} : Exists p → b | ⟨h, _⟩ := h theorem Exists.snd {p : b → Prop} : ∀ h : Exists p, p h.fst | ⟨_, h⟩ := h @[simp] theorem forall_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∀ h' : p, q h') ↔ q h := @forall_const (q h) p ⟨h⟩ @[simp] theorem exists_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∃ h' : p, q h') ↔ q h := @exists_const (q h) p ⟨h⟩ @[simp] theorem forall_prop_of_false {p : Prop} {q : p → Prop} (hn : ¬ p) : (∀ h' : p, q h') ↔ true := iff_true_intro $ λ h, hn.elim h @[simp] theorem exists_prop_of_false {p : Prop} {q : p → Prop} : ¬ p → ¬ (∃ h' : p, q h') := mt Exists.fst 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₂ @[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 versions of earlier lemmas -/ namespace classical variables {α : Sort*} {p : α → Prop} local attribute [instance] prop_decidable @[simp] protected theorem not_forall : (¬ ∀ x, p x) ↔ (∃ x, ¬ p x) := not_forall @[simp] protected theorem not_exists_not : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := not_exists_not protected theorem forall_or_distrib_left {q : Prop} {p : α → Prop} : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := forall_or_distrib_left protected theorem forall_or_distrib_right {q : Prop} {p : α → Prop} : (∀x, p x ∨ q) ↔ (∀x, p x) ∨ q := forall_or_distrib_right protected theorem forall_or_distrib {β} {p : α → Prop} {q : β → Prop} : (∀x y, p x ∨ q y) ↔ (∀ x, p x) ∨ (∀ y, q y) := by rw ← forall_or_distrib_right; simp [forall_or_distrib_left.symm] theorem cases {p : Prop → Prop} (h1 : p true) (h2 : p false) : ∀a, p a := assume a, cases_on a h1 h2 theorem or_not {p : Prop} : p ∨ ¬ p := by_cases or.inl or.inr protected theorem or_iff_not_imp_left {p q : Prop} : p ∨ q ↔ (¬ p → q) := or_iff_not_imp_left protected theorem or_iff_not_imp_right {p q : Prop} : q ∨ p ↔ (¬ p → q) := or_iff_not_imp_right @[simp] protected lemma not_not {p : Prop} : ¬¬p ↔ p := not_not @[simp] protected lemma not_imp {p q : Prop} : ¬(p → q) ↔ p ∧ ¬q := not_imp protected theorem not_imp_not {p q : Prop} : (¬ p → ¬ q) ↔ (q → p) := not_imp_not protected lemma not_and_distrib {p q : Prop}: ¬(p ∧ q) ↔ ¬p ∨ ¬q := not_and_distrib protected lemma imp_iff_not_or {a b : Prop} : a → b ↔ ¬a ∨ b := imp_iff_not_or lemma iff_iff_not_or_and_or_not {a b : Prop} : (a ↔ b) ↔ ((¬a ∨ b) ∧ (a ∨ ¬b)) := begin rw [iff_iff_implies_and_implies a b], simp only [imp_iff_not_or, or.comm] end /- 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/113488general/08268noncomputabletheorem.html> -/ library_note "classical lemma" @[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 @[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 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 theorem not_ball [decidable (∃ x h, ¬ P x h)] [∀ x h, decidable (P x h)] : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := ⟨not.imp_symm $ λ nx x h, nx.imp_symm $ λ h', ⟨x, h, h'⟩, not_ball_of_bex_not⟩ theorem ball_true_iff (p : α → Prop) : (∀ x, p x → true) ↔ true := iff_true_intro (λ h hrx, trivial) theorem ball_and_distrib : (∀ x h, P x h ∧ Q x h) ↔ (∀ x h, P x h) ∧ (∀ x h, Q x h) := iff.trans (forall_congr $ λ x, forall_and_distrib) forall_and_distrib theorem bex_or_distrib : (∃ x h, P x h ∨ Q x h) ↔ (∃ x h, P x h) ∨ (∃ x h, Q x h) := iff.trans (exists_congr $ λ x, exists_or_distrib) exists_or_distrib end bounded_quantifiers namespace classical local attribute [instance] prop_decidable theorem not_ball {α : Sort*} {p : α → Prop} {P : Π (x : α), p x → Prop} : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := _root_.not_ball end classical 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 * /-! ### 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 : ¬ 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 section ite lemma apply_dite {α β : Type*} (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], } lemma apply_ite {α β : Type*} (f : α → β) (P : Prop) [decidable P] (x y : α) : f (ite P x y) = ite P (f x) (f y) := apply_dite f P (λ _, x) (λ _, y) lemma dite_apply {α : Type*} {β : α → Type*} (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], } lemma ite_apply {α : Type*} {β : α → Type*} (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 end ite
ff20d3fa7f6c7a8192ac10953d8bc0d3d4eb94a8
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/sets_functions_and_relations/unnamed_856.lean
c3a0897768375ece147a722a2a18e6cc491359d9
[]
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
214
lean
import data.set.function variables {α β : Type*} variable f : α → β variables s t : set α -- BEGIN example : s ⊆ f ⁻¹' (f '' s) := begin intros x xs, show f x ∈ f '' s, use [x, xs] end -- END
f1039902e18aaa84daff4019096696aa462aa3c8
5b0c53e5aaa0e60538d10f6b619a464aaf463815
/ch5.hlean
b6e118a6d5383df1533c1d4934239e27040cae08
[ "Apache-2.0" ]
permissive
bbentzen/hott-book-in-lean
f845a19ef09d48d2fb813624b4650d5832a47e10
9e262e633e653280b9cde5d287631fcec8501f64
refs/heads/master
1,586,430,679,994
1,519,975,089,000
1,519,975,089,000
50,330,220
1
0
null
null
null
null
UTF-8
Lean
false
false
3,733
hlean
/- Copyright (c) 2016 Bruno Bentzen. All rights reserved. Released under the Apache License 2.0 (see "License"); Theorems and exercises of the HoTT book (Chapter 5) -/ import .ch2 .ch3 open eq prod unit bool sum sigma ua funext nat lift /- ************************************** -/ /- Ch.5 Induction -/ /- ************************************** -/ /- §5.1 (Introduction to inductive types) -/ variables {A B C X Z: Type} -- Theorem 5.1.1 definition uniq_nat_rec {E : ℕ → Type} (f g : Π (x : ℕ), E x) (e₀ : E 0) (eₛ : Π (x : ℕ), E x → E (succ x)) (H₁ : f 0 = e₀) (H₂ : g 0 = e₀) (H₃ : Π n, f (succ n) = eₛ n (f n)) (H₄ : Π n, g (succ n) = eₛ n (g n)) : f = g := begin apply funext, intro n, induction n with n IH, apply (H₁ ⬝ H₂⁻¹), apply (H₃ n ⬝ ap (eₛ n) IH ⬝ (H₄ n)⁻¹) end -- /- §5.2 (Uniqueness of inductive types) -/ inductive list (A : Type) : Type := | nil : list A | cons : A → list A → list A definition nat_to_list : ℕ → list 𝟭 := by intro n; induction n with n IH; apply list.nil; apply (list.cons ⋆ IH) definition list1_eq_nat : list 𝟭 ≃ ℕ := let f := λ a, list.rec_on a 0 (λ u a H, succ H) in ⟨f, (⟨nat_to_list, show (Π (n : ℕ), f (nat_to_list n) = n), from begin intro n,induction n with n IH, apply idp, apply (ap succ IH) end⟩, ⟨nat_to_list, show (Π (a : list 𝟭), nat_to_list (f a) = a), from begin intro a,induction a with u a H, apply idp, induction u, apply (ap (list.cons ⋆) H) end ⟩ ) ⟩ -- /- §5.3 (W-types) -/ universe variables i j inductive wtype {A : Type.{i}} (B : A → Type.{j}) : Type.{max i j} := | sup : Π (a : A), (B a → wtype B) → wtype B notation `W` binders `, ` x:(scoped P, wtype P) := x open wtype -- ℕ with W-types definition nat' : Type₀ := W (b : 𝟮), bool.rec_on b 𝟬 𝟭 notation `ℕ'` := nat' definition zero' : ℕ' := sup ff (λ (x : 𝟬), empty.rec_on _ x) definition succ' : ℕ' → ℕ' := λ n, sup tt (λ (x : 𝟭), n) -- List with W-types definition list' (A : Type₀) : Type₀ := W (a : 𝟭 + A), sum.rec_on a (λ u, 𝟬) (λ a, 𝟭) definition nil' {A : Type₀} : list' A := sup (inl(⋆)) (λ (x : 𝟬), empty.rec_on _ x) definition cons' {A : Type₀} : A → list' A → list' A := λ a u, sup (inr(a)) (λ (x : 𝟭), u) -- Definition of double definition double' : ℕ' → ℕ' := begin intro n, induction n with b u IH, induction b, apply zero', apply (succ' (succ' (IH ⋆))) end -- Theorem 5.3.1. definition uniq_w_rec {B : A → Type} {E : (W (a : A), B a) → Type} (g h : Π (w : W (a : A), B a), E w) (e : Π (a : A) (f : B a → W (a : A), B a), (Π (b : B a), E (f b)) → E (sup a f)) (H₁ : Π (a : A) (f : B a → W (a : A), B a), g (sup a f) = e a f (λ b, g (f b)) ) (H₂ : Π (a : A) (f : B a → W (a : A), B a), h (sup a f) = e a f (λ b, h (f b)) ) : g = h := definition uniq_w_rec {B : A → Type} {E : (W (a : A), B a) → Type} (g h : Π (w : W (a : A), B a), E w) (e : Π (a : A) (f : B a → W (a : A), B a), (Π (b : B a), E (f b)) → E (sup a f) ) (H₁ : Π (a : A) (f : B a → W (a : A), B a), g (sup a f) = e a f (λ b, g (f b)) ) (H₂ : Π (a : A) (f : B a → W (a : A), B a), h (sup a f) = e a f (λ b, h (f b)) ) : g = h := begin apply funext, intro w, induction w with a f IH, apply (H₁ a f ⬝ begin apply (ap (e a f) (show (λ b, g (f b)) = (λ b, h (f b)), from begin apply funext, apply IH end)) end ⬝ (H₂ a f)⁻¹) end --
35da6384717393246d56f99bd62ffb7a7febbb60
c3f2fcd060adfa2ca29f924839d2d925e8f2c685
/hott/init/path.hlean
6b98187dc5c5f58e59c6065f1fe8c5a7806a19e9
[ "Apache-2.0" ]
permissive
respu/lean
6582d19a2f2838a28ecd2b3c6f81c32d07b5341d
8c76419c60b63d0d9f7bc04ebb0b99812d0ec654
refs/heads/master
1,610,882,451,231
1,427,747,084,000
1,427,747,429,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
24,340
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: init.path Authors: Jeremy Avigad, Jakob von Raumer, Floris van Doorn Ported from Coq HoTT -/ prelude import .function .datatypes .relation .tactic open function eq /- Path equality -/ namespace eq variables {A B C : Type} {P : A → Type} {x y z t : A} --notation a = b := eq a b notation x = y `:>`:50 A:49 := @eq A x y definition idp {a : A} := refl a definition idpath (a : A) := refl a -- unbased path induction definition rec' [reducible] {P : Π (a b : A), (a = b) -> Type} (H : Π (a : A), P a a idp) {a b : A} (p : a = b) : P a b p := eq.rec (H a) p definition rec_on' [reducible] {P : Π (a b : A), (a = b) -> Type} {a b : A} (p : a = b) (H : Π (a : A), P a a idp) : P a b p := eq.rec (H a) p /- Concatenation and inverse -/ definition concat (p : x = y) (q : y = z) : x = z := eq.rec (λu, u) q p definition inverse (p : x = y) : y = x := eq.rec (refl x) p notation p₁ ⬝ p₂ := concat p₁ p₂ notation p ⁻¹ := inverse p --a second notation for the inverse, which is not overloaded postfix [parsing-only] `⁻¹ᵖ`:std.prec.max_plus := inverse /- The 1-dimensional groupoid structure -/ -- The identity path is a right unit. definition con_idp (p : x = y) : p ⬝ idp = p := eq.rec_on p idp -- The identity path is a right unit. definition idp_con (p : x = y) : idp ⬝ p = p := eq.rec_on p idp -- Concatenation is associative. definition con.assoc' (p : x = y) (q : y = z) (r : z = t) : p ⬝ (q ⬝ r) = (p ⬝ q) ⬝ r := eq.rec_on r (eq.rec_on q idp) definition con.assoc (p : x = y) (q : y = z) (r : z = t) : (p ⬝ q) ⬝ r = p ⬝ (q ⬝ r) := eq.rec_on r (eq.rec_on q idp) -- The left inverse law. definition con.right_inv (p : x = y) : p ⬝ p⁻¹ = idp := eq.rec_on p idp -- The right inverse law. definition con.left_inv (p : x = y) : p⁻¹ ⬝ p = idp := eq.rec_on p idp /- Several auxiliary theorems about canceling inverses across associativity. These are somewhat redundant, following from earlier theorems. -/ definition inv_con_cancel_left (p : x = y) (q : y = z) : p⁻¹ ⬝ (p ⬝ q) = q := eq.rec_on q (eq.rec_on p idp) definition con_inv_cancel_left (p : x = y) (q : x = z) : p ⬝ (p⁻¹ ⬝ q) = q := eq.rec_on q (eq.rec_on p idp) definition con_inv_cancel_right (p : x = y) (q : y = z) : (p ⬝ q) ⬝ q⁻¹ = p := eq.rec_on q (eq.rec_on p idp) definition inv_con_cancel_right (p : x = z) (q : y = z) : (p ⬝ q⁻¹) ⬝ q = p := eq.rec_on q (take p, eq.rec_on p idp) p -- Inverse distributes over concatenation definition con_inv (p : x = y) (q : y = z) : (p ⬝ q)⁻¹ = q⁻¹ ⬝ p⁻¹ := eq.rec_on q (eq.rec_on p idp) definition inv_con_inv_left (p : y = x) (q : y = z) : (p⁻¹ ⬝ q)⁻¹ = q⁻¹ ⬝ p := eq.rec_on q (eq.rec_on p idp) -- universe metavariables definition inv_con_inv_right (p : x = y) (q : z = y) : (p ⬝ q⁻¹)⁻¹ = q ⬝ p⁻¹ := eq.rec_on p (take q, eq.rec_on q idp) q definition inv_con_inv_inv (p : y = x) (q : z = y) : (p⁻¹ ⬝ q⁻¹)⁻¹ = q ⬝ p := eq.rec_on p (eq.rec_on q idp) -- Inverse is an involution. definition inv_inv (p : x = y) : p⁻¹⁻¹ = p := eq.rec_on p idp -- auxiliary definition used by 'cases' tactic definition elim_inv_inv {A : Type} {a b : A} {C : a = b → Type} (H₁ : a = b) (H₂ : C (H₁⁻¹⁻¹)) : C H₁ := eq.rec_on (inv_inv H₁) H₂ /- Theorems for moving things around in equations -/ definition con_eq_of_eq_inv_con (p : x = z) (q : y = z) (r : y = x) : p = r⁻¹ ⬝ q → r ⬝ p = q := eq.rec_on r (take p h, idp_con _ ⬝ h ⬝ idp_con _) p definition con_eq_of_eq_con_inv (p : x = z) (q : y = z) (r : y = x) : r = q ⬝ p⁻¹ → r ⬝ p = q := eq.rec_on p (take q h, (con_idp _ ⬝ h ⬝ con_idp _)) q definition inv_con_eq_of_eq_con (p : x = z) (q : y = z) (r : x = y) : p = r ⬝ q → r⁻¹ ⬝ p = q := eq.rec_on r (take q h, idp_con _ ⬝ h ⬝ idp_con _) q definition con_inv_eq_of_eq_con (p : z = x) (q : y = z) (r : y = x) : r = q ⬝ p → r ⬝ p⁻¹ = q := eq.rec_on p (take r h, con_idp _ ⬝ h ⬝ con_idp _) r definition eq_con_of_inv_con_eq (p : x = z) (q : y = z) (r : y = x) : r⁻¹ ⬝ q = p → q = r ⬝ p := eq.rec_on r (take p h, (idp_con _)⁻¹ ⬝ h ⬝ (idp_con _)⁻¹) p definition eq_con_of_con_inv_eq (p : x = z) (q : y = z) (r : y = x) : q ⬝ p⁻¹ = r → q = r ⬝ p := eq.rec_on p (take q h, (con_idp _)⁻¹ ⬝ h ⬝ (con_idp _)⁻¹) q definition eq_inv_con_of_con_eq (p : x = z) (q : y = z) (r : x = y) : r ⬝ q = p → q = r⁻¹ ⬝ p := eq.rec_on r (take q h, (idp_con _)⁻¹ ⬝ h ⬝ (idp_con _)⁻¹) q definition eq_con_inv_of_con_eq (p : z = x) (q : y = z) (r : y = x) : q ⬝ p = r → q = r ⬝ p⁻¹ := eq.rec_on p (take r h, (con_idp _)⁻¹ ⬝ h ⬝ (con_idp _)⁻¹) r definition eq_of_con_inv_eq_idp (p q : x = y) : p ⬝ q⁻¹ = idp → p = q := eq.rec_on q (take p h, (con_idp _)⁻¹ ⬝ h) p definition eq_of_inv_con_eq_idp (p q : x = y) : q⁻¹ ⬝ p = idp → p = q := eq.rec_on q (take p h, (idp_con _)⁻¹ ⬝ h) p definition eq_inv_of_con_eq_idp' (p : x = y) (q : y = x) : p ⬝ q = idp → p = q⁻¹ := eq.rec_on q (take p h, (con_idp _)⁻¹ ⬝ h) p definition eq_inv_of_con_eq_idp (p : x = y) (q : y = x) : q ⬝ p = idp → p = q⁻¹ := eq.rec_on q (take p h, (idp_con _)⁻¹ ⬝ h) p definition eq_of_idp_eq_inv_con (p q : x = y) : idp = p⁻¹ ⬝ q → p = q := eq.rec_on p (take q h, h ⬝ (idp_con _)) q definition eq_of_idp_eq_con_inv (p q : x = y) : idp = q ⬝ p⁻¹ → p = q := eq.rec_on p (take q h, h ⬝ (con_idp _)) q definition inv_eq_of_idp_eq_con (p : x = y) (q : y = x) : idp = q ⬝ p → p⁻¹ = q := eq.rec_on p (take q h, h ⬝ (con_idp _)) q definition inv_eq_of_idp_eq_con' (p : x = y) (q : y = x) : idp = p ⬝ q → p⁻¹ = q := eq.rec_on p (take q h, h ⬝ (idp_con _)) q /- Transport -/ definition transport [reducible] (P : A → Type) {x y : A} (p : x = y) (u : P x) : P y := eq.rec_on p u -- This idiom makes the operation right associative. notation p `▹`:65 x:64 := transport _ p x definition tr_inv [reducible] (P : A → Type) {x y : A} (p : x = y) (u : P y) : P x := p⁻¹ ▹ u definition ap ⦃A B : Type⦄ (f : A → B) {x y:A} (p : x = y) : f x = f y := eq.rec_on p idp definition ap01 [reducible] := ap definition homotopy [reducible] (f g : Πx, P x) : Type := Πx : A, f x = g x notation f ∼ g := homotopy f g namespace homotopy protected definition refl (f : Πx, P x) : f ∼ f := λ x, idp protected definition symm {f g : Πx, P x} (H : f ∼ g) : g ∼ f := λ x, inverse (H x) protected definition trans {f g h : Πx, P x} (H1 : f ∼ g) (H2 : g ∼ h) : f ∼ h := λ x, concat (H1 x) (H2 x) calc_refl refl calc_symm symm calc_trans trans end homotopy definition apD10 {f g : Πx, P x} (H : f = g) : f ∼ g := λx, eq.rec_on H idp --the next theorem is useful if you want to write "apply (apD10' a)" definition apD10' {f g : Πx, P x} (a : A) (H : f = g) : f a = g a := eq.rec_on H idp definition ap10 {f g : A → B} (H : f = g) : f ∼ g := apD10 H definition ap11 {f g : A → B} (H : f = g) {x y : A} (p : x = y) : f x = g y := eq.rec_on H (eq.rec_on p idp) definition apD (f : Πa:A, P a) {x y : A} (p : x = y) : p ▹ (f x) = f y := eq.rec_on p idp /- calc enviroment -/ calc_subst transport calc_trans concat calc_refl refl calc_symm inverse /- More theorems for moving things around in equations -/ definition tr_eq_of_eq_inv_tr (P : A → Type) {x y : A} (p : x = y) (u : P x) (v : P y) : u = p⁻¹ ▹ v → p ▹ u = v := eq.rec_on p (take v, id) v definition inv_tr_eq_of_eq_tr (P : A → Type) {x y : A} (p : y = x) (u : P x) (v : P y) : u = p ▹ v → p⁻¹ ▹ u = v := eq.rec_on p (take u, id) u definition eq_inv_tr_of_tr_eq (P : A → Type) {x y : A} (p : x = y) (u : P x) (v : P y) : p ▹ u = v → u = p⁻¹ ▹ v := eq.rec_on p (take v, id) v definition eq_tr_of_inv_tr_eq (P : A → Type) {x y : A} (p : y = x) (u : P x) (v : P y) : p⁻¹ ▹ u = v → u = p ▹ v := eq.rec_on p (take u, id) u /- Functoriality of functions -/ -- Here we prove that functions behave like functors between groupoids, and that [ap] itself is -- functorial. -- Functions take identity paths to identity paths definition ap_idp (x : A) (f : A → B) : (ap f idp) = idp :> (f x = f x) := idp definition apD_idp (x : A) (f : Π x : A, P x) : apD f idp = idp :> (f x = f x) := idp -- Functions commute with concatenation. definition ap_con (f : A → B) {x y z : A} (p : x = y) (q : y = z) : ap f (p ⬝ q) = (ap f p) ⬝ (ap f q) := eq.rec_on q (eq.rec_on p idp) definition con_ap_con_eq_con_ap_con_ap (f : A → B) {w x y z : A} (r : f w = f x) (p : x = y) (q : y = z) : r ⬝ (ap f (p ⬝ q)) = (r ⬝ ap f p) ⬝ (ap f q) := eq.rec_on q (take p, eq.rec_on p (con.assoc' r idp idp)) p definition ap_con_con_eq_ap_con_ap_con (f : A → B) {w x y z : A} (p : x = y) (q : y = z) (r : f z = f w) : (ap f (p ⬝ q)) ⬝ r = (ap f p) ⬝ (ap f q ⬝ r) := eq.rec_on q (eq.rec_on p (take r, con.assoc _ _ _)) r -- Functions commute with path inverses. definition ap_inv' (f : A → B) {x y : A} (p : x = y) : (ap f p)⁻¹ = ap f p⁻¹ := eq.rec_on p idp definition ap_inv {A B : Type} (f : A → B) {x y : A} (p : x = y) : ap f p⁻¹ = (ap f p)⁻¹ := eq.rec_on p idp -- [ap] itself is functorial in the first argument. definition ap_id (p : x = y) : ap id p = p := eq.rec_on p idp definition ap_compose (f : A → B) (g : B → C) {x y : A} (p : x = y) : ap (g ∘ f) p = ap g (ap f p) := eq.rec_on p idp -- Sometimes we don't have the actual function [compose]. definition ap_compose' (f : A → B) (g : B → C) {x y : A} (p : x = y) : ap (λa, g (f a)) p = ap g (ap f p) := eq.rec_on p idp -- The action of constant maps. definition ap_constant (p : x = y) (z : B) : ap (λu, z) p = idp := eq.rec_on p idp -- Naturality of [ap]. definition ap_con_eq_con_ap {f g : A → B} (p : Π x, f x = g x) {x y : A} (q : x = y) : (ap f q) ⬝ (p y) = (p x) ⬝ (ap g q) := eq.rec_on q (idp_con _ ⬝ (con_idp _)⁻¹) -- Naturality of [ap] at identity. definition ap_con_eq_con {f : A → A} (p : Πx, f x = x) {x y : A} (q : x = y) : (ap f q) ⬝ (p y) = (p x) ⬝ q := eq.rec_on q (idp_con _ ⬝ (con_idp _)⁻¹) definition con_ap_eq_con {f : A → A} (p : Πx, x = f x) {x y : A} (q : x = y) : (p x) ⬝ (ap f q) = q ⬝ (p y) := eq.rec_on q (con_idp _ ⬝ (idp_con _)⁻¹) -- Naturality with other paths hanging around. definition con_ap_con_con_eq_con_con_ap_con {f g : A → B} (p : f ∼ g) {x y : A} (q : x = y) {w z : B} (r : w = f x) (s : g y = z) : (r ⬝ ap f q) ⬝ (p y ⬝ s) = (r ⬝ p x) ⬝ (ap g q ⬝ s) := eq.rec_on s (eq.rec_on q idp) definition con_ap_con_eq_con_con_ap {f g : A → B} (p : f ∼ g) {x y : A} (q : x = y) {w : B} (r : w = f x) : (r ⬝ ap f q) ⬝ p y = (r ⬝ p x) ⬝ ap g q := eq.rec_on q idp -- TODO: try this using the simplifier, and compare proofs definition ap_con_con_eq_con_ap_con {f g : A → B} (p : f ∼ g) {x y : A} (q : x = y) {z : B} (s : g y = z) : (ap f q) ⬝ (p y ⬝ s) = (p x) ⬝ (ap g q ⬝ s) := eq.rec_on s (eq.rec_on q (calc (ap f idp) ⬝ (p x ⬝ idp) = idp ⬝ p x : idp ... = p x : idp_con _ ... = (p x) ⬝ (ap g idp ⬝ idp) : idp)) -- This also works: -- eq.rec_on s (eq.rec_on q (idp_con _ ▹ idp)) definition con_ap_con_con_eq_con_con_con {f : A → A} (p : f ∼ id) {x y : A} (q : x = y) {w z : A} (r : w = f x) (s : y = z) : (r ⬝ ap f q) ⬝ (p y ⬝ s) = (r ⬝ p x) ⬝ (q ⬝ s) := eq.rec_on s (eq.rec_on q idp) definition con_con_ap_con_eq_con_con_con {g : A → A} (p : id ∼ g) {x y : A} (q : x = y) {w z : A} (r : w = x) (s : g y = z) : (r ⬝ p x) ⬝ (ap g q ⬝ s) = (r ⬝ q) ⬝ (p y ⬝ s) := eq.rec_on s (eq.rec_on q idp) definition con_ap_con_eq_con_con {f : A → A} (p : f ∼ id) {x y : A} (q : x = y) {w : A} (r : w = f x) : (r ⬝ ap f q) ⬝ p y = (r ⬝ p x) ⬝ q := eq.rec_on q idp definition ap_con_con_eq_con_con {f : A → A} (p : f ∼ id) {x y : A} (q : x = y) {z : A} (s : y = z) : (ap f q) ⬝ (p y ⬝ s) = (p x) ⬝ (q ⬝ s) := eq.rec_on s (eq.rec_on q (idp_con _ ▹ idp)) definition con_con_ap_eq_con_con {g : A → A} (p : id ∼ g) {x y : A} (q : x = y) {w : A} (r : w = x) : (r ⬝ p x) ⬝ ap g q = (r ⬝ q) ⬝ p y := eq.rec_on q idp definition con_ap_con_eq_con_con' {g : A → A} (p : id ∼ g) {x y : A} (q : x = y) {z : A} (s : g y = z) : p x ⬝ (ap g q ⬝ s) = q ⬝ (p y ⬝ s) := begin apply (eq.rec_on s), apply (eq.rec_on q), apply (idp_con (p x) ▹ idp) end /- Action of [apD10] and [ap10] on paths -/ -- Application of paths between functions preserves the groupoid structure definition apD10_idp (f : Πx, P x) (x : A) : apD10 (refl f) x = idp := idp definition apD10_con {f f' f'' : Πx, P x} (h : f = f') (h' : f' = f'') (x : A) : apD10 (h ⬝ h') x = apD10 h x ⬝ apD10 h' x := eq.rec_on h (take h', eq.rec_on h' idp) h' definition apD10_inv {f g : Πx : A, P x} (h : f = g) (x : A) : apD10 h⁻¹ x = (apD10 h x)⁻¹ := eq.rec_on h idp definition ap10_idp {f : A → B} (x : A) : ap10 (refl f) x = idp := idp definition ap10_con {f f' f'' : A → B} (h : f = f') (h' : f' = f'') (x : A) : ap10 (h ⬝ h') x = ap10 h x ⬝ ap10 h' x := apD10_con h h' x definition ap10_inv {f g : A → B} (h : f = g) (x : A) : ap10 h⁻¹ x = (ap10 h x)⁻¹ := apD10_inv h x -- [ap10] also behaves nicely on paths produced by [ap] definition ap_ap10 (f g : A → B) (h : B → C) (p : f = g) (a : A) : ap h (ap10 p a) = ap10 (ap (λ f', h ∘ f') p) a:= eq.rec_on p idp /- Transport and the groupoid structure of paths -/ definition tr_idp (P : A → Type) {x : A} (u : P x) : idp ▹ u = u := idp definition tr_con (P : A → Type) {x y z : A} (p : x = y) (q : y = z) (u : P x) : p ⬝ q ▹ u = q ▹ p ▹ u := eq.rec_on q (eq.rec_on p idp) definition tr_inv_tr (P : A → Type) {x y : A} (p : x = y) (z : P y) : p ▹ p⁻¹ ▹ z = z := (tr_con P p⁻¹ p z)⁻¹ ⬝ ap (λr, transport P r z) (con.left_inv p) definition inv_tr_tr (P : A → Type) {x y : A} (p : x = y) (z : P x) : p⁻¹ ▹ p ▹ z = z := (tr_con P p p⁻¹ z)⁻¹ ⬝ ap (λr, transport P r z) (con.right_inv p) definition tr_con_lemma (P : A → Type) {x y z w : A} (p : x = y) (q : y = z) (r : z = w) (u : P x) : ap (λe, e ▹ u) (con.assoc' p q r) ⬝ (tr_con P (p ⬝ q) r u) ⬝ ap (transport P r) (tr_con P p q u) = (tr_con P p (q ⬝ r) u) ⬝ (tr_con P q r (p ▹ u)) :> ((p ⬝ (q ⬝ r)) ▹ u = r ▹ q ▹ p ▹ u) := eq.rec_on r (eq.rec_on q (eq.rec_on p idp)) -- Here is another coherence lemma for transport. definition tr_inv_tr_lemma (P : A → Type) {x y : A} (p : x = y) (z : P x) : tr_inv_tr P p (transport P p z) = ap (transport P p) (inv_tr_tr P p z) := eq.rec_on p idp -- Dependent transport in a doubly dependent type. -- should P, Q and y all be explicit here? definition transportD (P : A → Type) (Q : Π a : A, P a → Type) {a a' : A} (p : a = a') (b : P a) (z : Q a b) : Q a' (p ▹ b) := eq.rec_on p z -- In Coq the variables B, C and y are explicit, but in Lean we can probably have them implicit using the following notation notation p `▹D`:65 x:64 := transportD _ _ p _ x -- Transporting along higher-dimensional paths definition transport2 (P : A → Type) {x y : A} {p q : x = y} (r : p = q) (z : P x) : p ▹ z = q ▹ z := ap (λp', p' ▹ z) r notation p `▹2`:65 x:64 := transport2 _ p _ x -- An alternative definition. definition tr2_eq_ap10 (Q : A → Type) {x y : A} {p q : x = y} (r : p = q) (z : Q x) : transport2 Q r z = ap10 (ap (transport Q) r) z := eq.rec_on r idp definition tr2_con (P : A → Type) {x y : A} {p1 p2 p3 : x = y} (r1 : p1 = p2) (r2 : p2 = p3) (z : P x) : transport2 P (r1 ⬝ r2) z = transport2 P r1 z ⬝ transport2 P r2 z := eq.rec_on r1 (eq.rec_on r2 idp) definition tr2_inv (Q : A → Type) {x y : A} {p q : x = y} (r : p = q) (z : Q x) : transport2 Q r⁻¹ z = (transport2 Q r z)⁻¹ := eq.rec_on r idp definition transportD2 (B C : A → Type) (D : Π(a:A), B a → C a → Type) {x1 x2 : A} (p : x1 = x2) (y : B x1) (z : C x1) (w : D x1 y z) : D x2 (p ▹ y) (p ▹ z) := eq.rec_on p w notation p `▹D2`:65 x:64 := transportD2 _ _ _ p _ _ x definition ap_tr_con_tr2 (P : A → Type) {x y : A} {p q : x = y} {z w : P x} (r : p = q) (s : z = w) : ap (transport P p) s ⬝ transport2 P r w = transport2 P r z ⬝ ap (transport P q) s := eq.rec_on r (con_idp _ ⬝ (idp_con _)⁻¹) definition fn_tr_eq_tr_fn {P Q : A → Type} {x y : A} (p : x = y) (f : Πx, P x → Q x) (z : P x) : f y (p ▹ z) = (p ▹ (f x z)) := eq.rec_on p idp /- Transporting in particular fibrations -/ /- From the Coq HoTT library: One frequently needs lemmas showing that transport in a certain dependent type is equal to some more explicitly defined operation, defined according to the structure of that dependent type. For most dependent types, we prove these lemmas in the appropriate file in the types/ subdirectory. Here we consider only the most basic cases. -/ -- Transporting in a constant fibration. definition tr_constant (p : x = y) (z : B) : transport (λx, B) p z = z := eq.rec_on p idp definition tr2_constant {p q : x = y} (r : p = q) (z : B) : tr_constant p z = transport2 (λu, B) r z ⬝ tr_constant q z := eq.rec_on r (idp_con _)⁻¹ -- Transporting in a pulled back fibration. -- TODO: P can probably be implicit definition transport_compose (P : B → Type) (f : A → B) (p : x = y) (z : P (f x)) : transport (P ∘ f) p z = transport P (ap f p) z := eq.rec_on p idp definition ap_precompose (f : A → B) (g g' : B → C) (p : g = g') : ap (λh, h ∘ f) p = transport (λh : B → C, g ∘ f = h ∘ f) p idp := eq.rec_on p idp definition apD10_ap_precompose (f : A → B) (g g' : B → C) (p : g = g') (a : A) : apD10 (ap (λh : B → C, h ∘ f) p) a = apD10 p (f a) := eq.rec_on p idp definition apD10_ap_postcompose (f : B → C) (g g' : A → B) (p : g = g') (a : A) : apD10 (ap (λh : A → B, f ∘ h) p) a = ap f (apD10 p a) := eq.rec_on p idp -- A special case of [transport_compose] which seems to come up a lot. definition tr_eq_tr_id_ap (P : A → Type) x y (p : x = y) (u : P x) : transport P p u = transport id (ap P p) u := eq.rec_on p idp /- The behavior of [ap] and [apD] -/ -- In a constant fibration, [apD] reduces to [ap], modulo [transport_const]. definition apD_eq_tr_constant_con_ap (f : A → B) (p: x = y) : apD f p = tr_constant p (f x) ⬝ ap f p := eq.rec_on p idp /- The 2-dimensional groupoid structure -/ -- Horizontal composition of 2-dimensional paths. definition concat2 {p p' : x = y} {q q' : y = z} (h : p = p') (h' : q = q') : p ⬝ q = p' ⬝ q' := eq.rec_on h (eq.rec_on h' idp) infixl `◾`:75 := concat2 -- 2-dimensional path inversion definition inverse2 {p q : x = y} (h : p = q) : p⁻¹ = q⁻¹ := eq.rec_on h idp /- Whiskering -/ definition whisker_left (p : x = y) {q r : y = z} (h : q = r) : p ⬝ q = p ⬝ r := idp ◾ h definition whisker_right {p q : x = y} (h : p = q) (r : y = z) : p ⬝ r = q ⬝ r := h ◾ idp -- Unwhiskering, a.k.a. cancelling definition cancel_left {x y z : A} (p : x = y) (q r : y = z) : (p ⬝ q = p ⬝ r) → (q = r) := λs, !inv_con_cancel_left⁻¹ ⬝ whisker_left p⁻¹ s ⬝ !inv_con_cancel_left definition cancel_right {x y z : A} (p q : x = y) (r : y = z) : (p ⬝ r = q ⬝ r) → (p = q) := λs, !con_inv_cancel_right⁻¹ ⬝ whisker_right s r⁻¹ ⬝ !con_inv_cancel_right -- Whiskering and identity paths. definition whisker_right_idp_right {p q : x = y} (h : p = q) : (con_idp p)⁻¹ ⬝ whisker_right h idp ⬝ con_idp q = h := eq.rec_on h (eq.rec_on p idp) definition whisker_right_idp_left (p : x = y) (q : y = z) : whisker_right idp q = idp :> (p ⬝ q = p ⬝ q) := eq.rec_on q idp definition whisker_left_idp_right (p : x = y) (q : y = z) : whisker_left p idp = idp :> (p ⬝ q = p ⬝ q) := eq.rec_on q idp definition whisker_left_idp_left {p q : x = y} (h : p = q) : (idp_con p) ⁻¹ ⬝ whisker_left idp h ⬝ idp_con q = h := eq.rec_on h (eq.rec_on p idp) definition con2_idp {p q : x = y} (h : p = q) : h ◾ idp = whisker_right h idp :> (p ⬝ idp = q ⬝ idp) := idp definition idp_con2 {p q : x = y} (h : p = q) : idp ◾ h = whisker_left idp h :> (idp ⬝ p = idp ⬝ q) := idp -- The interchange law for concatenation. definition con2_con_con2 {p p' p'' : x = y} {q q' q'' : y = z} (a : p = p') (b : p' = p'') (c : q = q') (d : q' = q'') : (a ◾ c) ⬝ (b ◾ d) = (a ⬝ b) ◾ (c ⬝ d) := eq.rec_on d (eq.rec_on c (eq.rec_on b (eq.rec_on a idp))) definition whisker_right_con_whisker_left {x y z : A} (p p' : x = y) (q q' : y = z) (a : p = p') (b : q = q') : (whisker_right a q) ⬝ (whisker_left p' b) = (whisker_left p b) ⬝ (whisker_right a q') := eq.rec_on b (eq.rec_on a (idp_con _)⁻¹) -- Structure corresponding to the coherence equations of a bicategory. -- The "pentagonator": the 3-cell witnessing the associativity pentagon. definition pentagon {v w x y z : A} (p : v = w) (q : w = x) (r : x = y) (s : y = z) : whisker_left p (con.assoc' q r s) ⬝ con.assoc' p (q ⬝ r) s ⬝ whisker_right (con.assoc' p q r) s = con.assoc' p q (r ⬝ s) ⬝ con.assoc' (p ⬝ q) r s := eq.rec_on s (eq.rec_on r (eq.rec_on q (eq.rec_on p idp))) -- The 3-cell witnessing the left unit triangle. definition triangulator (p : x = y) (q : y = z) : con.assoc' p idp q ⬝ whisker_right (con_idp p) q = whisker_left p (idp_con q) := eq.rec_on q (eq.rec_on p idp) definition eckmann_hilton {x:A} (p q : idp = idp :> (x = x)) : p ⬝ q = q ⬝ p := (!whisker_right_idp_right ◾ !whisker_left_idp_left)⁻¹ ⬝ (!con_idp ◾ !con_idp) ⬝ (!idp_con ◾ !idp_con) ⬝ !whisker_right_con_whisker_left ⬝ (!idp_con ◾ !idp_con)⁻¹ ⬝ (!con_idp ◾ !con_idp)⁻¹ ⬝ (!whisker_left_idp_left ◾ !whisker_right_idp_right) -- The action of functions on 2-dimensional paths definition ap02 (f:A → B) {x y : A} {p q : x = y} (r : p = q) : ap f p = ap f q := eq.rec_on r idp definition ap02_con (f : A → B) {x y : A} {p p' p'' : x = y} (r : p = p') (r' : p' = p'') : ap02 f (r ⬝ r') = ap02 f r ⬝ ap02 f r' := eq.rec_on r (eq.rec_on r' idp) definition ap02_con2 (f : A → B) {x y z : A} {p p' : x = y} {q q' :y = z} (r : p = p') (s : q = q') : ap02 f (r ◾ s) = ap_con f p q ⬝ (ap02 f r ◾ ap02 f s) ⬝ (ap_con f p' q')⁻¹ := eq.rec_on r (eq.rec_on s (eq.rec_on q (eq.rec_on p idp))) definition apD02 {p q : x = y} (f : Π x, P x) (r : p = q) : apD f p = transport2 P r (f x) ⬝ apD f q := eq.rec_on r (idp_con _)⁻¹ -- And now for a lemma whose statement is much longer than its proof. definition apD02_con (P : A → Type) (f : Π x:A, P x) {x y : A} {p1 p2 p3 : x = y} (r1 : p1 = p2) (r2 : p2 = p3) : apD02 f (r1 ⬝ r2) = apD02 f r1 ⬝ whisker_left (transport2 P r1 (f x)) (apD02 f r2) ⬝ con.assoc' _ _ _ ⬝ (whisker_right (tr2_con P r1 r2 (f x))⁻¹ (apD f p3)) := eq.rec_on r2 (eq.rec_on r1 (eq.rec_on p1 idp)) end eq
ef3754ccc36e1b2aec4c1debd97c54619e485b62
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/quaternion.lean
2b77f49fb542728e36f1fc6bf262f6e5dd513667
[ "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
3,425
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import algebra.quaternion import analysis.inner_product_space.basic /-! # Quaternions as a normed algebra In this file we define the following structures on the space `ℍ := ℍ[ℝ]` of quaternions: * inner product space; * normed ring; * normed space over `ℝ`. ## Notation The following notation is available with `open_locale quaternion`: * `ℍ` : quaternions ## Tags quaternion, normed ring, normed space, normed algebra -/ localized "notation `ℍ` := quaternion ℝ" in quaternion open_locale real_inner_product_space noncomputable theory namespace quaternion instance : has_inner ℝ ℍ := ⟨λ a b, (a * b.conj).re⟩ lemma inner_self (a : ℍ) : ⟪a, a⟫ = norm_sq a := rfl lemma inner_def (a b : ℍ) : ⟪a, b⟫ = (a * b.conj).re := rfl instance : inner_product_space ℝ ℍ := inner_product_space.of_core { inner := has_inner.inner, conj_sym := λ x y, by simp [inner_def, mul_comm], nonneg_re := λ x, norm_sq_nonneg, definite := λ x, norm_sq_eq_zero.1, add_left := λ x y z, by simp only [inner_def, add_mul, add_re], smul_left := λ x y r, by simp [inner_def] } lemma norm_sq_eq_norm_sq (a : ℍ) : norm_sq a = ∥a∥ * ∥a∥ := by rw [← inner_self, real_inner_self_eq_norm_mul_norm] instance : norm_one_class ℍ := ⟨by rw [norm_eq_sqrt_real_inner, inner_self, norm_sq.map_one, real.sqrt_one]⟩ @[simp, norm_cast] lemma norm_coe (a : ℝ) : ∥(a : ℍ)∥ = ∥a∥ := by rw [norm_eq_sqrt_real_inner, inner_self, norm_sq_coe, real.sqrt_sq_eq_abs, real.norm_eq_abs] @[simp, norm_cast] lemma nnnorm_coe (a : ℝ) : ∥(a : ℍ)∥₊ = ∥a∥₊ := subtype.ext $ norm_coe a noncomputable instance : normed_division_ring ℍ := { dist_eq := λ _ _, rfl, norm_mul' := λ a b, by { simp only [norm_eq_sqrt_real_inner, inner_self, norm_sq.map_mul], exact real.sqrt_mul norm_sq_nonneg _ } } noncomputable instance : normed_algebra ℝ ℍ := { norm_smul_le := λ a x, (norm_smul a x).le, to_algebra := quaternion.algebra } instance : has_coe ℂ ℍ := ⟨λ z, ⟨z.re, z.im, 0, 0⟩⟩ @[simp, norm_cast] lemma coe_complex_re (z : ℂ) : (z : ℍ).re = z.re := rfl @[simp, norm_cast] lemma coe_complex_im_i (z : ℂ) : (z : ℍ).im_i = z.im := rfl @[simp, norm_cast] lemma coe_complex_im_j (z : ℂ) : (z : ℍ).im_j = 0 := rfl @[simp, norm_cast] lemma coe_complex_im_k (z : ℂ) : (z : ℍ).im_k = 0 := rfl @[simp, norm_cast] lemma coe_complex_add (z w : ℂ) : ↑(z + w) = (z + w : ℍ) := by ext; simp @[simp, norm_cast] lemma coe_complex_mul (z w : ℂ) : ↑(z * w) = (z * w : ℍ) := by ext; simp @[simp, norm_cast] lemma coe_complex_zero : ((0 : ℂ) : ℍ) = 0 := rfl @[simp, norm_cast] lemma coe_complex_one : ((1 : ℂ) : ℍ) = 1 := rfl @[simp, norm_cast] lemma coe_real_complex_mul (r : ℝ) (z : ℂ) : (r • z : ℍ) = ↑r * ↑z := by ext; simp @[simp, norm_cast] lemma coe_complex_coe (r : ℝ) : ((r : ℂ) : ℍ) = r := rfl /-- Coercion `ℂ →ₐ[ℝ] ℍ` as an algebra homomorphism. -/ def of_complex : ℂ →ₐ[ℝ] ℍ := { to_fun := coe, map_one' := rfl, map_zero' := rfl, map_add' := coe_complex_add, map_mul' := coe_complex_mul, commutes' := λ x, rfl } @[simp] lemma coe_of_complex : ⇑of_complex = coe := rfl end quaternion
7540735496875ea7f182a8ac1cc630679801d286
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Elab/Tactic/Match.lean
1444254784ef7af40083560671b2e9c001d58317
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
2,781
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Parser.Term import Lean.Elab.Match import Lean.Elab.Tactic.Basic import Lean.Elab.Tactic.Induction namespace Lean.Elab.Tactic open Meta /- Erase auxiliary `_discr` variables introduced by `match`-expression elaborator -/ @[builtinTactic Lean.Parser.Tactic.eraseAuxDiscrs] def evalEraseAuxDiscrs : Tactic := fun _ => do withMainContext do let lctx ← getLCtx let auxDecls := lctx.foldl (init := []) fun auxDecls localDecl => if Term.isAuxDiscrName localDecl.userName then localDecl.fvarId :: auxDecls else auxDecls let mut mvarId ← getMainGoal for auxDecl in auxDecls do mvarId ← tryClear mvarId auxDecl replaceMainGoal [mvarId] structure AuxMatchTermState where nextIdx : Nat := 1 «cases» : Array Syntax := #[] private def mkAuxiliaryMatchTermAux (parentTag : Name) (matchTac : Syntax) : StateT AuxMatchTermState MacroM Syntax := do let matchAlts := matchTac[5] let alts := matchAlts[0].getArgs let newAlts ← alts.mapM fun alt => do let alt := alt.setKind ``Parser.Term.matchAlt let holeOrTacticSeq := alt[3] if holeOrTacticSeq.isOfKind ``Parser.Term.syntheticHole then pure alt else if holeOrTacticSeq.isOfKind ``Parser.Term.hole then let s ← get let tag := if alts.size > 1 then parentTag ++ (`match).appendIndexAfter s.nextIdx else parentTag let holeName := mkIdentFrom holeOrTacticSeq tag let newHole ← `(?$holeName:ident) modify fun s => { s with nextIdx := s.nextIdx + 1} pure <| alt.setArg 3 newHole else withFreshMacroScope do let newHole ← `(?rhs) let newHoleId := newHole[1] let newCase ← `(tactic| case $newHoleId =>%$(alt[2]) eraseAuxDiscrs!; ($holeOrTacticSeq:tacticSeq) ) modify fun s => { s with cases := s.cases.push newCase } pure <| alt.setArg 3 newHole let result := matchTac.setKind ``Parser.Term.«match» let result := result.setArg 5 (mkNode ``Parser.Term.matchAlts #[mkNullNode newAlts]) pure result private def mkAuxiliaryMatchTerm (parentTag : Name) (matchTac : Syntax) : MacroM (Syntax × Array Syntax) := do let (matchTerm, s) ← mkAuxiliaryMatchTermAux parentTag matchTac |>.run {} pure (matchTerm, s.cases) @[builtinTactic Lean.Parser.Tactic.match] def evalMatch : Tactic := fun stx => do let tag ← getMainTag let (matchTerm, cases) ← liftMacroM <| mkAuxiliaryMatchTerm tag stx let refineMatchTerm ← `(tactic| refine $matchTerm) let stxNew := mkNullNode (#[refineMatchTerm] ++ cases) withMacroExpansion stx stxNew <| evalTactic stxNew end Lean.Elab.Tactic
47571518d466c80915dfaa3974c794072a7f884b
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/renameI.lean
6dde569c2f9ed3a0ec27c627178810adc235fb50
[ "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
154
lean
example : ∀ a b c d : Nat, a = b → a = d → a = c → c = b := by intros rename_i h1 _ h2 apply Eq.trans apply Eq.symm exact h2 exact h1
5ea00b00a3668a085c1cfd2387e719fbb4058538
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebra/order/field.lean
26df8720a2f498cb42fae6ee8a287b7dcdd62cd6
[ "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
34,844
lean
/- Copyright (c) 2014 Robert Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis, Leonardo de Moura, Mario Carneiro, Floris van Doorn -/ import algebra.field.basic import algebra.group_power.lemmas import algebra.group_power.order import algebra.order.ring import order.bounds import tactic.monotonicity.basic /-! # Linear ordered (semi)fields A linear ordered (semi)field is a (semi)field equipped with a linear order such that * addition respects the order: `a ≤ b → c + a ≤ c + b`; * multiplication of positives is positive: `0 < a → 0 < b → 0 < a * b`; * `0 < 1`. ## Main Definitions * `linear_ordered_semifield`: Typeclass for linear order semifields. * `linear_ordered_field`: Typeclass for linear ordered fields. -/ set_option old_structure_cmd true variables {α β : Type*} /-- A linear ordered semifield is a field with a linear order respecting the operations. -/ @[protect_proj] class linear_ordered_semifield (α : Type*) extends linear_ordered_semiring α, semifield α /-- A linear ordered field is a field with a linear order respecting the operations. -/ @[protect_proj] class linear_ordered_field (α : Type*) extends linear_ordered_comm_ring α, field α @[priority 100] -- See note [lower instance priority] instance linear_ordered_field.to_linear_ordered_semifield [linear_ordered_field α] : linear_ordered_semifield α := { ..linear_ordered_ring.to_linear_ordered_semiring, ..‹linear_ordered_field α› } namespace function /-- Pullback a `linear_ordered_semifield` under an injective map. -/ @[reducible] -- See note [reducible non-instances] def injective.linear_ordered_semifield [linear_ordered_semifield α] [has_zero β] [has_one β] [has_add β] [has_mul β] [has_pow β ℕ] [has_smul ℕ β] [has_nat_cast β] [has_inv β] [has_div β] [has_pow β ℤ] [has_sup β] [has_inf β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) (nat_cast : ∀ n : ℕ, f n = n) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : linear_ordered_semifield β := { ..hf.linear_ordered_semiring f zero one add mul nsmul npow nat_cast hsup hinf, ..hf.semifield f zero one add mul inv div nsmul npow zpow nat_cast } /-- Pullback a `linear_ordered_field` under an injective map. -/ @[reducible] -- See note [reducible non-instances] def injective.linear_ordered_field [linear_ordered_field α] [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] [has_pow β ℕ] [has_smul ℕ β] [has_smul ℤ β] [has_nat_cast β] [has_int_cast β] [has_inv β] [has_div β] [has_pow β ℤ] [has_sup β] [has_inf β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) (nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : linear_ordered_field β := { .. hf.linear_ordered_ring f zero one add mul neg sub nsmul zsmul npow nat_cast int_cast hsup hinf, .. hf.field f zero one add mul neg sub inv div nsmul zsmul npow zpow nat_cast int_cast } end function section linear_ordered_semifield variables [linear_ordered_semifield α] {a b c d e : α} /-- `equiv.mul_left₀` as an order_iso. -/ @[simps {simp_rhs := tt}] def order_iso.mul_left₀ (a : α) (ha : 0 < a) : α ≃o α := { map_rel_iff' := λ _ _, mul_le_mul_left ha, ..equiv.mul_left₀ a ha.ne' } /-- `equiv.mul_right₀` as an order_iso. -/ @[simps {simp_rhs := tt}] def order_iso.mul_right₀ (a : α) (ha : 0 < a) : α ≃o α := { map_rel_iff' := λ _ _, mul_le_mul_right ha, ..equiv.mul_right₀ a ha.ne' } /-! ### Lemmas about pos, nonneg, nonpos, neg -/ @[simp] lemma inv_pos : 0 < a⁻¹ ↔ 0 < a := suffices ∀ a : α, 0 < a → 0 < a⁻¹, from ⟨λ h, inv_inv a ▸ this _ h, this a⟩, assume a ha, flip lt_of_mul_lt_mul_left ha.le $ by simp [ne_of_gt ha, zero_lt_one] @[simp] lemma inv_nonneg : 0 ≤ a⁻¹ ↔ 0 ≤ a := by simp only [le_iff_eq_or_lt, inv_pos, zero_eq_inv] @[simp] lemma inv_lt_zero : a⁻¹ < 0 ↔ a < 0 := by simp only [← not_le, inv_nonneg] @[simp] lemma inv_nonpos : a⁻¹ ≤ 0 ↔ a ≤ 0 := by simp only [← not_lt, inv_pos] lemma one_div_pos : 0 < 1 / a ↔ 0 < a := inv_eq_one_div a ▸ inv_pos lemma one_div_neg : 1 / a < 0 ↔ a < 0 := inv_eq_one_div a ▸ inv_lt_zero lemma one_div_nonneg : 0 ≤ 1 / a ↔ 0 ≤ a := inv_eq_one_div a ▸ inv_nonneg lemma one_div_nonpos : 1 / a ≤ 0 ↔ a ≤ 0 := inv_eq_one_div a ▸ inv_nonpos lemma div_pos (ha : 0 < a) (hb : 0 < b) : 0 < a / b := by { rw div_eq_mul_inv, exact mul_pos ha (inv_pos.2 hb) } lemma div_nonneg (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a / b := by { rw div_eq_mul_inv, exact mul_nonneg ha (inv_nonneg.2 hb) } lemma div_nonpos_of_nonpos_of_nonneg (ha : a ≤ 0) (hb : 0 ≤ b) : a / b ≤ 0 := by { rw div_eq_mul_inv, exact mul_nonpos_of_nonpos_of_nonneg ha (inv_nonneg.2 hb) } lemma div_nonpos_of_nonneg_of_nonpos (ha : 0 ≤ a) (hb : b ≤ 0) : a / b ≤ 0 := by { rw div_eq_mul_inv, exact mul_nonpos_of_nonneg_of_nonpos ha (inv_nonpos.2 hb) } /-! ### Relating one division with another term. -/ lemma le_div_iff (hc : 0 < c) : a ≤ b / c ↔ a * c ≤ b := ⟨λ h, div_mul_cancel b (ne_of_lt hc).symm ▸ mul_le_mul_of_nonneg_right h hc.le, λ h, calc a = a * c * (1 / c) : mul_mul_div a (ne_of_lt hc).symm ... ≤ b * (1 / c) : mul_le_mul_of_nonneg_right h (one_div_pos.2 hc).le ... = b / c : (div_eq_mul_one_div b c).symm⟩ lemma le_div_iff' (hc : 0 < c) : a ≤ b / c ↔ c * a ≤ b := by rw [mul_comm, le_div_iff hc] lemma div_le_iff (hb : 0 < b) : a / b ≤ c ↔ a ≤ c * b := ⟨λ h, calc a = a / b * b : by rw (div_mul_cancel _ (ne_of_lt hb).symm) ... ≤ c * b : mul_le_mul_of_nonneg_right h hb.le, λ h, calc a / b = a * (1 / b) : div_eq_mul_one_div a b ... ≤ (c * b) * (1 / b) : mul_le_mul_of_nonneg_right h (one_div_pos.2 hb).le ... = (c * b) / b : (div_eq_mul_one_div (c * b) b).symm ... = c : by refine (div_eq_iff (ne_of_gt hb)).mpr rfl⟩ lemma div_le_iff' (hb : 0 < b) : a / b ≤ c ↔ a ≤ b * c := by rw [mul_comm, div_le_iff hb] lemma lt_div_iff (hc : 0 < c) : a < b / c ↔ a * c < b := lt_iff_lt_of_le_iff_le $ div_le_iff hc lemma lt_div_iff' (hc : 0 < c) : a < b / c ↔ c * a < b := by rw [mul_comm, lt_div_iff hc] lemma div_lt_iff (hc : 0 < c) : b / c < a ↔ b < a * c := lt_iff_lt_of_le_iff_le (le_div_iff hc) lemma div_lt_iff' (hc : 0 < c) : b / c < a ↔ b < c * a := by rw [mul_comm, div_lt_iff hc] lemma inv_mul_le_iff (h : 0 < b) : b⁻¹ * a ≤ c ↔ a ≤ b * c := begin rw [inv_eq_one_div, mul_comm, ← div_eq_mul_one_div], exact div_le_iff' h, end lemma inv_mul_le_iff' (h : 0 < b) : b⁻¹ * a ≤ c ↔ a ≤ c * b := by rw [inv_mul_le_iff h, mul_comm] lemma mul_inv_le_iff (h : 0 < b) : a * b⁻¹ ≤ c ↔ a ≤ b * c := by rw [mul_comm, inv_mul_le_iff h] lemma mul_inv_le_iff' (h : 0 < b) : a * b⁻¹ ≤ c ↔ a ≤ c * b := by rw [mul_comm, inv_mul_le_iff' h] lemma div_self_le_one (a : α) : a / a ≤ 1 := if h : a = 0 then by simp [h] else by simp [h] lemma inv_mul_lt_iff (h : 0 < b) : b⁻¹ * a < c ↔ a < b * c := begin rw [inv_eq_one_div, mul_comm, ← div_eq_mul_one_div], exact div_lt_iff' h, end lemma inv_mul_lt_iff' (h : 0 < b) : b⁻¹ * a < c ↔ a < c * b := by rw [inv_mul_lt_iff h, mul_comm] lemma mul_inv_lt_iff (h : 0 < b) : a * b⁻¹ < c ↔ a < b * c := by rw [mul_comm, inv_mul_lt_iff h] lemma mul_inv_lt_iff' (h : 0 < b) : a * b⁻¹ < c ↔ a < c * b := by rw [mul_comm, inv_mul_lt_iff' h] lemma inv_pos_le_iff_one_le_mul (ha : 0 < a) : a⁻¹ ≤ b ↔ 1 ≤ b * a := by { rw [inv_eq_one_div], exact div_le_iff ha } lemma inv_pos_le_iff_one_le_mul' (ha : 0 < a) : a⁻¹ ≤ b ↔ 1 ≤ a * b := by { rw [inv_eq_one_div], exact div_le_iff' ha } lemma inv_pos_lt_iff_one_lt_mul (ha : 0 < a) : a⁻¹ < b ↔ 1 < b * a := by { rw [inv_eq_one_div], exact div_lt_iff ha } lemma inv_pos_lt_iff_one_lt_mul' (ha : 0 < a) : a⁻¹ < b ↔ 1 < a * b := by { rw [inv_eq_one_div], exact div_lt_iff' ha } /-- One direction of `div_le_iff` where `b` is allowed to be `0` (but `c` must be nonnegative) -/ lemma div_le_of_nonneg_of_le_mul (hb : 0 ≤ b) (hc : 0 ≤ c) (h : a ≤ c * b) : a / b ≤ c := by { rcases eq_or_lt_of_le hb with rfl|hb', simp [hc], rwa [div_le_iff hb'] } lemma div_le_one_of_le (h : a ≤ b) (hb : 0 ≤ b) : a / b ≤ 1 := div_le_of_nonneg_of_le_mul hb zero_le_one $ by rwa one_mul /-! ### Bi-implications of inequalities using inversions -/ lemma inv_le_inv_of_le (ha : 0 < a) (h : a ≤ b) : b⁻¹ ≤ a⁻¹ := by rwa [← one_div a, le_div_iff' ha, ← div_eq_mul_inv, div_le_iff (ha.trans_le h), one_mul] /-- See `inv_le_inv_of_le` for the implication from right-to-left with one fewer assumption. -/ lemma inv_le_inv (ha : 0 < a) (hb : 0 < b) : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := by rw [← one_div, div_le_iff ha, ← div_eq_inv_mul, le_div_iff hb, one_mul] /-- In a linear ordered field, for positive `a` and `b` we have `a⁻¹ ≤ b ↔ b⁻¹ ≤ a`. See also `inv_le_of_inv_le` for a one-sided implication with one fewer assumption. -/ lemma inv_le (ha : 0 < a) (hb : 0 < b) : a⁻¹ ≤ b ↔ b⁻¹ ≤ a := by rw [← inv_le_inv hb (inv_pos.2 ha), inv_inv] lemma inv_le_of_inv_le (ha : 0 < a) (h : a⁻¹ ≤ b) : b⁻¹ ≤ a := (inv_le ha ((inv_pos.2 ha).trans_le h)).1 h lemma le_inv (ha : 0 < a) (hb : 0 < b) : a ≤ b⁻¹ ↔ b ≤ a⁻¹ := by rw [← inv_le_inv (inv_pos.2 hb) ha, inv_inv] /-- See `inv_lt_inv_of_lt` for the implication from right-to-left with one fewer assumption. -/ lemma inv_lt_inv (ha : 0 < a) (hb : 0 < b) : a⁻¹ < b⁻¹ ↔ b < a := lt_iff_lt_of_le_iff_le (inv_le_inv hb ha) lemma inv_lt_inv_of_lt (hb : 0 < b) (h : b < a) : a⁻¹ < b⁻¹ := (inv_lt_inv (hb.trans h) hb).2 h /-- In a linear ordered field, for positive `a` and `b` we have `a⁻¹ < b ↔ b⁻¹ < a`. See also `inv_lt_of_inv_lt` for a one-sided implication with one fewer assumption. -/ lemma inv_lt (ha : 0 < a) (hb : 0 < b) : a⁻¹ < b ↔ b⁻¹ < a := lt_iff_lt_of_le_iff_le (le_inv hb ha) lemma inv_lt_of_inv_lt (ha : 0 < a) (h : a⁻¹ < b) : b⁻¹ < a := (inv_lt ha ((inv_pos.2 ha).trans h)).1 h lemma lt_inv (ha : 0 < a) (hb : 0 < b) : a < b⁻¹ ↔ b < a⁻¹ := lt_iff_lt_of_le_iff_le (inv_le hb ha) lemma inv_lt_one (ha : 1 < a) : a⁻¹ < 1 := by rwa [inv_lt ((@zero_lt_one α _ _).trans ha) zero_lt_one, inv_one] lemma one_lt_inv (h₁ : 0 < a) (h₂ : a < 1) : 1 < a⁻¹ := by rwa [lt_inv (@zero_lt_one α _ _) h₁, inv_one] lemma inv_le_one (ha : 1 ≤ a) : a⁻¹ ≤ 1 := by rwa [inv_le ((@zero_lt_one α _ _).trans_le ha) zero_lt_one, inv_one] lemma one_le_inv (h₁ : 0 < a) (h₂ : a ≤ 1) : 1 ≤ a⁻¹ := by rwa [le_inv (@zero_lt_one α _ _) h₁, inv_one] lemma inv_lt_one_iff_of_pos (h₀ : 0 < a) : a⁻¹ < 1 ↔ 1 < a := ⟨λ h₁, inv_inv a ▸ one_lt_inv (inv_pos.2 h₀) h₁, inv_lt_one⟩ lemma inv_lt_one_iff : a⁻¹ < 1 ↔ a ≤ 0 ∨ 1 < a := begin cases le_or_lt a 0 with ha ha, { simp [ha, (inv_nonpos.2 ha).trans_lt zero_lt_one] }, { simp only [ha.not_le, false_or, inv_lt_one_iff_of_pos ha] } end lemma one_lt_inv_iff : 1 < a⁻¹ ↔ 0 < a ∧ a < 1 := ⟨λ h, ⟨inv_pos.1 (zero_lt_one.trans h), inv_inv a ▸ inv_lt_one h⟩, and_imp.2 one_lt_inv⟩ lemma inv_le_one_iff : a⁻¹ ≤ 1 ↔ a ≤ 0 ∨ 1 ≤ a := begin rcases em (a = 1) with (rfl|ha), { simp [le_rfl] }, { simp only [ne.le_iff_lt (ne.symm ha), ne.le_iff_lt (mt inv_eq_one.1 ha), inv_lt_one_iff] } end lemma one_le_inv_iff : 1 ≤ a⁻¹ ↔ 0 < a ∧ a ≤ 1 := ⟨λ h, ⟨inv_pos.1 (zero_lt_one.trans_le h), inv_inv a ▸ inv_le_one h⟩, and_imp.2 one_le_inv⟩ /-! ### Relating two divisions. -/ @[mono] lemma div_le_div_of_le (hc : 0 ≤ c) (h : a ≤ b) : a / c ≤ b / c := begin rw [div_eq_mul_one_div a c, div_eq_mul_one_div b c], exact mul_le_mul_of_nonneg_right h (one_div_nonneg.2 hc) end -- Not a `mono` lemma b/c `div_le_div` is strictly more general lemma div_le_div_of_le_left (ha : 0 ≤ a) (hc : 0 < c) (h : c ≤ b) : a / b ≤ a / c := begin rw [div_eq_mul_inv, div_eq_mul_inv], exact mul_le_mul_of_nonneg_left ((inv_le_inv (hc.trans_le h) hc).mpr h) ha end lemma div_le_div_of_le_of_nonneg (hab : a ≤ b) (hc : 0 ≤ c) : a / c ≤ b / c := div_le_div_of_le hc hab lemma div_lt_div_of_lt (hc : 0 < c) (h : a < b) : a / c < b / c := begin rw [div_eq_mul_one_div a c, div_eq_mul_one_div b c], exact mul_lt_mul_of_pos_right h (one_div_pos.2 hc) end lemma div_le_div_right (hc : 0 < c) : a / c ≤ b / c ↔ a ≤ b := ⟨le_imp_le_of_lt_imp_lt $ div_lt_div_of_lt hc, div_le_div_of_le $ hc.le⟩ lemma div_lt_div_right (hc : 0 < c) : a / c < b / c ↔ a < b := lt_iff_lt_of_le_iff_le $ div_le_div_right hc lemma div_lt_div_left (ha : 0 < a) (hb : 0 < b) (hc : 0 < c) : a / b < a / c ↔ c < b := by simp only [div_eq_mul_inv, mul_lt_mul_left ha, inv_lt_inv hb hc] lemma div_le_div_left (ha : 0 < a) (hb : 0 < b) (hc : 0 < c) : a / b ≤ a / c ↔ c ≤ b := le_iff_le_iff_lt_iff_lt.2 (div_lt_div_left ha hc hb) lemma div_lt_div_iff (b0 : 0 < b) (d0 : 0 < d) : a / b < c / d ↔ a * d < c * b := by rw [lt_div_iff d0, div_mul_eq_mul_div, div_lt_iff b0] lemma div_le_div_iff (b0 : 0 < b) (d0 : 0 < d) : a / b ≤ c / d ↔ a * d ≤ c * b := by rw [le_div_iff d0, div_mul_eq_mul_div, div_le_iff b0] @[mono] lemma div_le_div (hc : 0 ≤ c) (hac : a ≤ c) (hd : 0 < d) (hbd : d ≤ b) : a / b ≤ c / d := by { rw div_le_div_iff (hd.trans_le hbd) hd, exact mul_le_mul hac hbd hd.le hc } lemma div_lt_div (hac : a < c) (hbd : d ≤ b) (c0 : 0 ≤ c) (d0 : 0 < d) : a / b < c / d := (div_lt_div_iff (d0.trans_le hbd) d0).2 (mul_lt_mul hac hbd d0 c0) lemma div_lt_div' (hac : a ≤ c) (hbd : d < b) (c0 : 0 < c) (d0 : 0 < d) : a / b < c / d := (div_lt_div_iff (d0.trans hbd) d0).2 (mul_lt_mul' hac hbd d0.le c0) lemma div_lt_div_of_lt_left (hc : 0 < c) (hb : 0 < b) (h : b < a) : c / a < c / b := (div_lt_div_left hc (hb.trans h) hb).mpr h /-! ### Relating one division and involving `1` -/ lemma div_le_self (ha : 0 ≤ a) (hb : 1 ≤ b) : a / b ≤ a := by simpa only [div_one] using div_le_div_of_le_left ha zero_lt_one hb lemma div_lt_self (ha : 0 < a) (hb : 1 < b) : a / b < a := by simpa only [div_one] using div_lt_div_of_lt_left ha zero_lt_one hb lemma le_div_self (ha : 0 ≤ a) (hb₀ : 0 < b) (hb₁ : b ≤ 1) : a ≤ a / b := by simpa only [div_one] using div_le_div_of_le_left ha hb₀ hb₁ lemma one_le_div (hb : 0 < b) : 1 ≤ a / b ↔ b ≤ a := by rw [le_div_iff hb, one_mul] lemma div_le_one (hb : 0 < b) : a / b ≤ 1 ↔ a ≤ b := by rw [div_le_iff hb, one_mul] lemma one_lt_div (hb : 0 < b) : 1 < a / b ↔ b < a := by rw [lt_div_iff hb, one_mul] lemma div_lt_one (hb : 0 < b) : a / b < 1 ↔ a < b := by rw [div_lt_iff hb, one_mul] lemma one_div_le (ha : 0 < a) (hb : 0 < b) : 1 / a ≤ b ↔ 1 / b ≤ a := by simpa using inv_le ha hb lemma one_div_lt (ha : 0 < a) (hb : 0 < b) : 1 / a < b ↔ 1 / b < a := by simpa using inv_lt ha hb lemma le_one_div (ha : 0 < a) (hb : 0 < b) : a ≤ 1 / b ↔ b ≤ 1 / a := by simpa using le_inv ha hb lemma lt_one_div (ha : 0 < a) (hb : 0 < b) : a < 1 / b ↔ b < 1 / a := by simpa using lt_inv ha hb /-! ### Relating two divisions, involving `1` -/ lemma one_div_le_one_div_of_le (ha : 0 < a) (h : a ≤ b) : 1 / b ≤ 1 / a := by simpa using inv_le_inv_of_le ha h lemma one_div_lt_one_div_of_lt (ha : 0 < a) (h : a < b) : 1 / b < 1 / a := by rwa [lt_div_iff' ha, ← div_eq_mul_one_div, div_lt_one (ha.trans h)] lemma le_of_one_div_le_one_div (ha : 0 < a) (h : 1 / a ≤ 1 / b) : b ≤ a := le_imp_le_of_lt_imp_lt (one_div_lt_one_div_of_lt ha) h lemma lt_of_one_div_lt_one_div (ha : 0 < a) (h : 1 / a < 1 / b) : b < a := lt_imp_lt_of_le_imp_le (one_div_le_one_div_of_le ha) h /-- For the single implications with fewer assumptions, see `one_div_le_one_div_of_le` and `le_of_one_div_le_one_div` -/ lemma one_div_le_one_div (ha : 0 < a) (hb : 0 < b) : 1 / a ≤ 1 / b ↔ b ≤ a := div_le_div_left zero_lt_one ha hb /-- For the single implications with fewer assumptions, see `one_div_lt_one_div_of_lt` and `lt_of_one_div_lt_one_div` -/ lemma one_div_lt_one_div (ha : 0 < a) (hb : 0 < b) : 1 / a < 1 / b ↔ b < a := div_lt_div_left zero_lt_one ha hb lemma one_lt_one_div (h1 : 0 < a) (h2 : a < 1) : 1 < 1 / a := by rwa [lt_one_div (@zero_lt_one α _ _) h1, one_div_one] lemma one_le_one_div (h1 : 0 < a) (h2 : a ≤ 1) : 1 ≤ 1 / a := by rwa [le_one_div (@zero_lt_one α _ _) h1, one_div_one] /-! ### Results about halving. The equalities also hold in semifields of characteristic `0`. -/ /- TODO: Unify `add_halves` and `add_halves'` into a single lemma about `division_semiring` + `char_zero` -/ lemma add_halves (a : α) : a / 2 + a / 2 = a := by rw [div_add_div_same, ← two_mul, mul_div_cancel_left a two_ne_zero] -- TODO: Generalize to `division_semiring` lemma add_self_div_two (a : α) : (a + a) / 2 = a := by rw [← mul_two, mul_div_cancel a two_ne_zero] lemma half_pos (h : 0 < a) : 0 < a / 2 := div_pos h zero_lt_two lemma one_half_pos : (0:α) < 1 / 2 := half_pos zero_lt_one lemma div_two_lt_of_pos (h : 0 < a) : a / 2 < a := by { rw [div_lt_iff (@zero_lt_two α _ _)], exact lt_mul_of_one_lt_right h one_lt_two } lemma half_lt_self : 0 < a → a / 2 < a := div_two_lt_of_pos lemma half_le_self (ha_nonneg : 0 ≤ a) : a / 2 ≤ a := begin by_cases h0 : a = 0, { simp [h0], }, { rw ← ne.def at h0, exact (half_lt_self (lt_of_le_of_ne ha_nonneg h0.symm)).le, }, end lemma one_half_lt_one : (1 / 2 : α) < 1 := half_lt_self zero_lt_one lemma left_lt_add_div_two : a < (a + b) / 2 ↔ a < b := by simp [lt_div_iff, mul_two] lemma add_div_two_lt_right : (a + b) / 2 < b ↔ a < b := by simp [div_lt_iff, mul_two] /-! ### Miscellaneous lemmas -/ lemma mul_le_mul_of_mul_div_le (h : a * (b / c) ≤ d) (hc : 0 < c) : b * a ≤ d * c := begin rw [← mul_div_assoc] at h, rwa [mul_comm b, ← div_le_iff hc], end lemma div_mul_le_div_mul_of_div_le_div (h : a / b ≤ c / d) (he : 0 ≤ e) : a / (b * e) ≤ c / (d * e) := begin rw [div_mul_eq_div_mul_one_div, div_mul_eq_div_mul_one_div], exact mul_le_mul_of_nonneg_right h (one_div_nonneg.2 he) end lemma exists_pos_mul_lt {a : α} (h : 0 < a) (b : α) : ∃ c : α, 0 < c ∧ b * c < a := begin have : 0 < a / max (b + 1) 1, from div_pos h (lt_max_iff.2 (or.inr zero_lt_one)), refine ⟨a / max (b + 1) 1, this, _⟩, rw [← lt_div_iff this, div_div_cancel' h.ne'], exact lt_max_iff.2 (or.inl $ lt_add_one _) end lemma monotone.div_const {β : Type*} [preorder β] {f : β → α} (hf : monotone f) {c : α} (hc : 0 ≤ c) : monotone (λ x, (f x) / c) := by simpa only [div_eq_mul_inv] using hf.mul_const (inv_nonneg.2 hc) lemma strict_mono.div_const {β : Type*} [preorder β] {f : β → α} (hf : strict_mono f) {c : α} (hc : 0 < c) : strict_mono (λ x, (f x) / c) := by simpa only [div_eq_mul_inv] using hf.mul_const (inv_pos.2 hc) @[priority 100] -- see Note [lower instance priority] instance linear_ordered_field.to_densely_ordered : densely_ordered α := { dense := λ a₁ a₂ h, ⟨(a₁ + a₂) / 2, calc a₁ = (a₁ + a₁) / 2 : (add_self_div_two a₁).symm ... < (a₁ + a₂) / 2 : div_lt_div_of_lt zero_lt_two (add_lt_add_left h _), calc (a₁ + a₂) / 2 < (a₂ + a₂) / 2 : div_lt_div_of_lt zero_lt_two (add_lt_add_right h _) ... = a₂ : add_self_div_two a₂⟩ } lemma min_div_div_right {c : α} (hc : 0 ≤ c) (a b : α) : min (a / c) (b / c) = (min a b) / c := eq.symm $ monotone.map_min (λ x y, div_le_div_of_le hc) lemma max_div_div_right {c : α} (hc : 0 ≤ c) (a b : α) : max (a / c) (b / c) = (max a b) / c := eq.symm $ monotone.map_max (λ x y, div_le_div_of_le hc) lemma one_div_strict_anti_on : strict_anti_on (λ x : α, 1 / x) (set.Ioi 0) := λ x x1 y y1 xy, (one_div_lt_one_div (set.mem_Ioi.mp y1) (set.mem_Ioi.mp x1)).mpr xy lemma one_div_pow_le_one_div_pow_of_le (a1 : 1 ≤ a) {m n : ℕ} (mn : m ≤ n) : 1 / a ^ n ≤ 1 / a ^ m := by refine (one_div_le_one_div _ _).mpr (pow_le_pow a1 mn); exact pow_pos (zero_lt_one.trans_le a1) _ lemma one_div_pow_lt_one_div_pow_of_lt (a1 : 1 < a) {m n : ℕ} (mn : m < n) : 1 / a ^ n < 1 / a ^ m := by refine (one_div_lt_one_div _ _).mpr (pow_lt_pow a1 mn); exact pow_pos (trans zero_lt_one a1) _ lemma one_div_pow_anti (a1 : 1 ≤ a) : antitone (λ n : ℕ, 1 / a ^ n) := λ m n, one_div_pow_le_one_div_pow_of_le a1 lemma one_div_pow_strict_anti (a1 : 1 < a) : strict_anti (λ n : ℕ, 1 / a ^ n) := λ m n, one_div_pow_lt_one_div_pow_of_lt a1 lemma inv_strict_anti_on : strict_anti_on (λ x : α, x⁻¹) (set.Ioi 0) := λ x hx y hy xy, (inv_lt_inv hy hx).2 xy lemma inv_pow_le_inv_pow_of_le (a1 : 1 ≤ a) {m n : ℕ} (mn : m ≤ n) : (a ^ n)⁻¹ ≤ (a ^ m)⁻¹ := by convert one_div_pow_le_one_div_pow_of_le a1 mn; simp lemma inv_pow_lt_inv_pow_of_lt (a1 : 1 < a) {m n : ℕ} (mn : m < n) : (a ^ n)⁻¹ < (a ^ m)⁻¹ := by convert one_div_pow_lt_one_div_pow_of_lt a1 mn; simp lemma inv_pow_anti (a1 : 1 ≤ a) : antitone (λ n : ℕ, (a ^ n)⁻¹) := λ m n, inv_pow_le_inv_pow_of_le a1 lemma inv_pow_strict_anti (a1 : 1 < a) : strict_anti (λ n : ℕ, (a ^ n)⁻¹) := λ m n, inv_pow_lt_inv_pow_of_lt a1 /-! ### Results about `is_lub` and `is_glb` -/ lemma is_glb.mul_left {s : set α} (ha : 0 ≤ a) (hs : is_glb s b) : is_glb ((λ b, a * b) '' s) (a * b) := begin rcases lt_or_eq_of_le ha with ha | rfl, { exact (order_iso.mul_left₀ _ ha).is_glb_image'.2 hs, }, { simp_rw zero_mul, rw hs.nonempty.image_const, exact is_glb_singleton }, end lemma is_glb.mul_right {s : set α} (ha : 0 ≤ a) (hs : is_glb s b) : is_glb ((λ b, b * a) '' s) (b * a) := by simpa [mul_comm] using hs.mul_left ha end linear_ordered_semifield section variables [linear_ordered_field α] {a b c d : α} /-! ### Lemmas about pos, nonneg, nonpos, neg -/ lemma div_pos_iff : 0 < a / b ↔ 0 < a ∧ 0 < b ∨ a < 0 ∧ b < 0 := by simp [division_def, mul_pos_iff] lemma div_neg_iff : a / b < 0 ↔ 0 < a ∧ b < 0 ∨ a < 0 ∧ 0 < b := by simp [division_def, mul_neg_iff] lemma div_nonneg_iff : 0 ≤ a / b ↔ 0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0 := by simp [division_def, mul_nonneg_iff] lemma div_nonpos_iff : a / b ≤ 0 ↔ 0 ≤ a ∧ b ≤ 0 ∨ a ≤ 0 ∧ 0 ≤ b := by simp [division_def, mul_nonpos_iff] lemma div_nonneg_of_nonpos (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a / b := div_nonneg_iff.2 $ or.inr ⟨ha, hb⟩ lemma div_pos_of_neg_of_neg (ha : a < 0) (hb : b < 0) : 0 < a / b := div_pos_iff.2 $ or.inr ⟨ha, hb⟩ lemma div_neg_of_neg_of_pos (ha : a < 0) (hb : 0 < b) : a / b < 0 := div_neg_iff.2 $ or.inr ⟨ha, hb⟩ lemma div_neg_of_pos_of_neg (ha : 0 < a) (hb : b < 0) : a / b < 0 := div_neg_iff.2 $ or.inl ⟨ha, hb⟩ /-! ### Relating one division with another term -/ lemma div_le_iff_of_neg (hc : c < 0) : b / c ≤ a ↔ a * c ≤ b := ⟨λ h, div_mul_cancel b (ne_of_lt hc) ▸ mul_le_mul_of_nonpos_right h hc.le, λ h, calc a = a * c * (1 / c) : mul_mul_div a (ne_of_lt hc) ... ≥ b * (1 / c) : mul_le_mul_of_nonpos_right h (one_div_neg.2 hc).le ... = b / c : (div_eq_mul_one_div b c).symm⟩ lemma div_le_iff_of_neg' (hc : c < 0) : b / c ≤ a ↔ c * a ≤ b := by rw [mul_comm, div_le_iff_of_neg hc] lemma le_div_iff_of_neg (hc : c < 0) : a ≤ b / c ↔ b ≤ a * c := by rw [← neg_neg c, mul_neg, div_neg, le_neg, div_le_iff (neg_pos.2 hc), neg_mul] lemma le_div_iff_of_neg' (hc : c < 0) : a ≤ b / c ↔ b ≤ c * a := by rw [mul_comm, le_div_iff_of_neg hc] lemma div_lt_iff_of_neg (hc : c < 0) : b / c < a ↔ a * c < b := lt_iff_lt_of_le_iff_le $ le_div_iff_of_neg hc lemma div_lt_iff_of_neg' (hc : c < 0) : b / c < a ↔ c * a < b := by rw [mul_comm, div_lt_iff_of_neg hc] lemma lt_div_iff_of_neg (hc : c < 0) : a < b / c ↔ b < a * c := lt_iff_lt_of_le_iff_le $ div_le_iff_of_neg hc lemma lt_div_iff_of_neg' (hc : c < 0) : a < b / c ↔ b < c * a := by rw [mul_comm, lt_div_iff_of_neg hc] /-! ### Bi-implications of inequalities using inversions -/ lemma inv_le_inv_of_neg (ha : a < 0) (hb : b < 0) : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := by rw [← one_div, div_le_iff_of_neg ha, ← div_eq_inv_mul, div_le_iff_of_neg hb, one_mul] lemma inv_le_of_neg (ha : a < 0) (hb : b < 0) : a⁻¹ ≤ b ↔ b⁻¹ ≤ a := by rw [← inv_le_inv_of_neg hb (inv_lt_zero.2 ha), inv_inv] lemma le_inv_of_neg (ha : a < 0) (hb : b < 0) : a ≤ b⁻¹ ↔ b ≤ a⁻¹ := by rw [← inv_le_inv_of_neg (inv_lt_zero.2 hb) ha, inv_inv] lemma inv_lt_inv_of_neg (ha : a < 0) (hb : b < 0) : a⁻¹ < b⁻¹ ↔ b < a := lt_iff_lt_of_le_iff_le (inv_le_inv_of_neg hb ha) lemma inv_lt_of_neg (ha : a < 0) (hb : b < 0) : a⁻¹ < b ↔ b⁻¹ < a := lt_iff_lt_of_le_iff_le (le_inv_of_neg hb ha) lemma lt_inv_of_neg (ha : a < 0) (hb : b < 0) : a < b⁻¹ ↔ b < a⁻¹ := lt_iff_lt_of_le_iff_le (inv_le_of_neg hb ha) /-! ### Relating two divisions -/ lemma div_le_div_of_nonpos_of_le (hc : c ≤ 0) (h : b ≤ a) : a / c ≤ b / c := begin rw [div_eq_mul_one_div a c, div_eq_mul_one_div b c], exact mul_le_mul_of_nonpos_right h (one_div_nonpos.2 hc) end lemma div_lt_div_of_neg_of_lt (hc : c < 0) (h : b < a) : a / c < b / c := begin rw [div_eq_mul_one_div a c, div_eq_mul_one_div b c], exact mul_lt_mul_of_neg_right h (one_div_neg.2 hc) end lemma div_le_div_right_of_neg (hc : c < 0) : a / c ≤ b / c ↔ b ≤ a := ⟨le_imp_le_of_lt_imp_lt $ div_lt_div_of_neg_of_lt hc, div_le_div_of_nonpos_of_le $ hc.le⟩ lemma div_lt_div_right_of_neg (hc : c < 0) : a / c < b / c ↔ b < a := lt_iff_lt_of_le_iff_le $ div_le_div_right_of_neg hc /-! ### Relating one division and involving `1` -/ lemma one_le_div_of_neg (hb : b < 0) : 1 ≤ a / b ↔ a ≤ b := by rw [le_div_iff_of_neg hb, one_mul] lemma div_le_one_of_neg (hb : b < 0) : a / b ≤ 1 ↔ b ≤ a := by rw [div_le_iff_of_neg hb, one_mul] lemma one_lt_div_of_neg (hb : b < 0) : 1 < a / b ↔ a < b := by rw [lt_div_iff_of_neg hb, one_mul] lemma div_lt_one_of_neg (hb : b < 0) : a / b < 1 ↔ b < a := by rw [div_lt_iff_of_neg hb, one_mul] lemma one_div_le_of_neg (ha : a < 0) (hb : b < 0) : 1 / a ≤ b ↔ 1 / b ≤ a := by simpa using inv_le_of_neg ha hb lemma one_div_lt_of_neg (ha : a < 0) (hb : b < 0) : 1 / a < b ↔ 1 / b < a := by simpa using inv_lt_of_neg ha hb lemma le_one_div_of_neg (ha : a < 0) (hb : b < 0) : a ≤ 1 / b ↔ b ≤ 1 / a := by simpa using le_inv_of_neg ha hb lemma lt_one_div_of_neg (ha : a < 0) (hb : b < 0) : a < 1 / b ↔ b < 1 / a := by simpa using lt_inv_of_neg ha hb lemma one_lt_div_iff : 1 < a / b ↔ 0 < b ∧ b < a ∨ b < 0 ∧ a < b := begin rcases lt_trichotomy b 0 with (hb|rfl|hb), { simp [hb, hb.not_lt, one_lt_div_of_neg] }, { simp [lt_irrefl, zero_le_one] }, { simp [hb, hb.not_lt, one_lt_div] } end lemma one_le_div_iff : 1 ≤ a / b ↔ 0 < b ∧ b ≤ a ∨ b < 0 ∧ a ≤ b := begin rcases lt_trichotomy b 0 with (hb|rfl|hb), { simp [hb, hb.not_lt, one_le_div_of_neg] }, { simp [lt_irrefl, zero_lt_one.not_le, zero_lt_one] }, { simp [hb, hb.not_lt, one_le_div] } end lemma div_lt_one_iff : a / b < 1 ↔ 0 < b ∧ a < b ∨ b = 0 ∨ b < 0 ∧ b < a := begin rcases lt_trichotomy b 0 with (hb|rfl|hb), { simp [hb, hb.not_lt, hb.ne, div_lt_one_of_neg] }, { simp [zero_lt_one], }, { simp [hb, hb.not_lt, div_lt_one, hb.ne.symm] } end lemma div_le_one_iff : a / b ≤ 1 ↔ 0 < b ∧ a ≤ b ∨ b = 0 ∨ b < 0 ∧ b ≤ a := begin rcases lt_trichotomy b 0 with (hb|rfl|hb), { simp [hb, hb.not_lt, hb.ne, div_le_one_of_neg] }, { simp [zero_le_one], }, { simp [hb, hb.not_lt, div_le_one, hb.ne.symm] } end /-! ### Relating two divisions, involving `1` -/ lemma one_div_le_one_div_of_neg_of_le (hb : b < 0) (h : a ≤ b) : 1 / b ≤ 1 / a := by rwa [div_le_iff_of_neg' hb, ← div_eq_mul_one_div, div_le_one_of_neg (h.trans_lt hb)] lemma one_div_lt_one_div_of_neg_of_lt (hb : b < 0) (h : a < b) : 1 / b < 1 / a := by rwa [div_lt_iff_of_neg' hb, ← div_eq_mul_one_div, div_lt_one_of_neg (h.trans hb)] lemma le_of_neg_of_one_div_le_one_div (hb : b < 0) (h : 1 / a ≤ 1 / b) : b ≤ a := le_imp_le_of_lt_imp_lt (one_div_lt_one_div_of_neg_of_lt hb) h lemma lt_of_neg_of_one_div_lt_one_div (hb : b < 0) (h : 1 / a < 1 / b) : b < a := lt_imp_lt_of_le_imp_le (one_div_le_one_div_of_neg_of_le hb) h /-- For the single implications with fewer assumptions, see `one_div_lt_one_div_of_neg_of_lt` and `lt_of_one_div_lt_one_div` -/ lemma one_div_le_one_div_of_neg (ha : a < 0) (hb : b < 0) : 1 / a ≤ 1 / b ↔ b ≤ a := by simpa [one_div] using inv_le_inv_of_neg ha hb /-- For the single implications with fewer assumptions, see `one_div_lt_one_div_of_lt` and `lt_of_one_div_lt_one_div` -/ lemma one_div_lt_one_div_of_neg (ha : a < 0) (hb : b < 0) : 1 / a < 1 / b ↔ b < a := lt_iff_lt_of_le_iff_le (one_div_le_one_div_of_neg hb ha) lemma one_div_lt_neg_one (h1 : a < 0) (h2 : -1 < a) : 1 / a < -1 := suffices 1 / a < 1 / -1, by rwa one_div_neg_one_eq_neg_one at this, one_div_lt_one_div_of_neg_of_lt h1 h2 lemma one_div_le_neg_one (h1 : a < 0) (h2 : -1 ≤ a) : 1 / a ≤ -1 := suffices 1 / a ≤ 1 / -1, by rwa one_div_neg_one_eq_neg_one at this, one_div_le_one_div_of_neg_of_le h1 h2 /-! ### Results about halving -/ lemma sub_self_div_two (a : α) : a - a / 2 = a / 2 := suffices a / 2 + a / 2 - a / 2 = a / 2, by rwa add_halves at this, by rw [add_sub_cancel] lemma div_two_sub_self (a : α) : a / 2 - a = - (a / 2) := suffices a / 2 - (a / 2 + a / 2) = - (a / 2), by rwa add_halves at this, by rw [sub_add_eq_sub_sub, sub_self, zero_sub] lemma add_sub_div_two_lt (h : a < b) : a + (b - a) / 2 < b := begin rwa [← div_sub_div_same, sub_eq_add_neg, add_comm (b/2), ← add_assoc, ← sub_eq_add_neg, ← lt_sub_iff_add_lt, sub_self_div_two, sub_self_div_two, div_lt_div_right (@zero_lt_two α _ _)] end /-- An inequality involving `2`. -/ lemma sub_one_div_inv_le_two (a2 : 2 ≤ a) : (1 - 1 / a)⁻¹ ≤ 2 := begin -- Take inverses on both sides to obtain `2⁻¹ ≤ 1 - 1 / a` refine (inv_le_inv_of_le (inv_pos.2 zero_lt_two) _).trans_eq (inv_inv (2 : α)), -- move `1 / a` to the left and `1 - 1 / 2 = 1 / 2` to the right to obtain `1 / a ≤ ⅟ 2` refine (le_sub_iff_add_le.2 (_ : _ + 2⁻¹ = _ ).le).trans ((sub_le_sub_iff_left 1).2 _), { -- show 2⁻¹ + 2⁻¹ = 1 exact (two_mul _).symm.trans (mul_inv_cancel two_ne_zero) }, { -- take inverses on both sides and use the assumption `2 ≤ a`. exact (one_div a).le.trans (inv_le_inv_of_le zero_lt_two a2) } end /-! ### Results about `is_lub` and `is_glb` -/ -- TODO: Generalize to `linear_ordered_semifield` lemma is_lub.mul_left {s : set α} (ha : 0 ≤ a) (hs : is_lub s b) : is_lub ((λ b, a * b) '' s) (a * b) := begin rcases lt_or_eq_of_le ha with ha | rfl, { exact (order_iso.mul_left₀ _ ha).is_lub_image'.2 hs, }, { simp_rw zero_mul, rw hs.nonempty.image_const, exact is_lub_singleton }, end -- TODO: Generalize to `linear_ordered_semifield` lemma is_lub.mul_right {s : set α} (ha : 0 ≤ a) (hs : is_lub s b) : is_lub ((λ b, b * a) '' s) (b * a) := by simpa [mul_comm] using hs.mul_left ha /-! ### Miscellaneous lemmmas -/ lemma mul_sub_mul_div_mul_neg_iff (hc : c ≠ 0) (hd : d ≠ 0) : (a * d - b * c) / (c * d) < 0 ↔ a / c < b / d := by rw [mul_comm b c, ← div_sub_div _ _ hc hd, sub_lt_zero] lemma mul_sub_mul_div_mul_nonpos_iff (hc : c ≠ 0) (hd : d ≠ 0) : (a * d - b * c) / (c * d) ≤ 0 ↔ a / c ≤ b / d := by rw [mul_comm b c, ← div_sub_div _ _ hc hd, sub_nonpos] alias mul_sub_mul_div_mul_neg_iff ↔ div_lt_div_of_mul_sub_mul_div_neg mul_sub_mul_div_mul_neg alias mul_sub_mul_div_mul_nonpos_iff ↔ div_le_div_of_mul_sub_mul_div_nonpos mul_sub_mul_div_mul_nonpos lemma exists_add_lt_and_pos_of_lt (h : b < a) : ∃ c, b + c < a ∧ 0 < c := ⟨(a - b) / 2, add_sub_div_two_lt h, div_pos (sub_pos_of_lt h) zero_lt_two⟩ lemma le_of_forall_sub_le (h : ∀ ε > 0, b - ε ≤ a) : b ≤ a := begin contrapose! h, simpa only [and_comm ((0 : α) < _), lt_sub_iff_add_lt, gt_iff_lt] using exists_add_lt_and_pos_of_lt h, end lemma mul_self_inj_of_nonneg (a0 : 0 ≤ a) (b0 : 0 ≤ b) : a * a = b * b ↔ a = b := mul_self_eq_mul_self_iff.trans $ or_iff_left_of_imp $ λ h, by { subst a, have : b = 0 := le_antisymm (neg_nonneg.1 a0) b0, rw [this, neg_zero] } lemma min_div_div_right_of_nonpos (hc : c ≤ 0) (a b : α) : min (a / c) (b / c) = (max a b) / c := eq.symm $ antitone.map_max $ λ x y, div_le_div_of_nonpos_of_le hc lemma max_div_div_right_of_nonpos (hc : c ≤ 0) (a b : α) : max (a / c) (b / c) = (min a b) / c := eq.symm $ antitone.map_min $ λ x y, div_le_div_of_nonpos_of_le hc lemma abs_inv (a : α) : |a⁻¹| = (|a|)⁻¹ := (abs_hom : α →*₀ α).map_inv a lemma abs_div (a b : α) : |a / b| = |a| / |b| := (abs_hom : α →*₀ α).map_div a b lemma abs_one_div (a : α) : |1 / a| = 1 / |a| := by rw [abs_div, abs_one] lemma pow_minus_two_nonneg : 0 ≤ a^(-2 : ℤ) := begin simp only [inv_nonneg, zpow_neg], change 0 ≤ a ^ ((2 : ℕ) : ℤ), rw zpow_coe_nat, apply sq_nonneg, end /-- Bernoulli's inequality reformulated to estimate `(n : α)`. -/ lemma nat.cast_le_pow_sub_div_sub (H : 1 < a) (n : ℕ) : (n : α) ≤ (a ^ n - 1) / (a - 1) := (le_div_iff (sub_pos.2 H)).2 $ le_sub_left_of_add_le $ one_add_mul_sub_le_pow ((neg_le_self zero_le_one).trans H.le) _ /-- For any `a > 1` and a natural `n` we have `n ≤ a ^ n / (a - 1)`. See also `nat.cast_le_pow_sub_div_sub` for a stronger inequality with `a ^ n - 1` in the numerator. -/ theorem nat.cast_le_pow_div_sub (H : 1 < a) (n : ℕ) : (n : α) ≤ a ^ n / (a - 1) := (n.cast_le_pow_sub_div_sub H).trans $ div_le_div_of_le (sub_nonneg.2 H.le) (sub_le_self _ zero_le_one) end
daee1b44c3fa7a25286eb33df82e0fadcc25b1ba
a523fc1740c8cb84cd0fa0f4b52a760da4e32a5c
/src/eigenvector.lean
afc29bb081b7b0a2d9e576c534869ba26e07d03b
[]
no_license
abentkamp/spectral
a1aff51e85d30b296a81d256ced1d382345d3396
751645679ef1cb6266316349de9e492eff85484c
refs/heads/master
1,669,994,798,064
1,597,591,646,000
1,597,591,646,000
287,544,072
0
0
null
null
null
null
UTF-8
Lean
false
false
23,866
lean
/- Copyright (c) 2020 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Alexander Bentkamp. -/ import ring_theory.principal_ideal_domain import missing_mathlib.data.polynomial import missing_mathlib.data.multiset import missing_mathlib.data.finsupp import missing_mathlib.linear_algebra.dimension import missing_mathlib.linear_algebra.finite_dimensional import missing_mathlib.linear_algebra.finsupp import missing_mathlib.algebra.group.units import missing_mathlib.algebra.ring import missing_mathlib.algebra.module import missing_mathlib.algebra.group_power import missing_mathlib.data.list.basic import missing_mathlib.set_theory.cardinal import missing_mathlib.ring_theory.algebra import missing_mathlib.ring_theory.polynomial.basic import analysis.complex.polynomial import missing_mathlib.field_thoery.algebraic_closure /-! # Eigenvectors and eigenvalues This file defines eigenvectors and eigenvalues, as well as generalized eigenvectors and eigenvalues. An eigenvector of a linear map `f` is a nonzero vector `x` such that `f x = μ • x` for some scalar `μ`. The scalar `μ` is called an eigenvalue. We express this by writing `eigenvector f μ x`. A generalized eigenvector of a linear map `f` is a nonzero vector `x` such that `(f x - μ • x) ^ k = 0` for some scalar `μ` and some natural number `k`. The scalar `μ` is called a generalized eigenvalue. We express this by writing `generalized_eigenvector f k μ x`. We follow Axler's approach [axler1996] that allows us to prove a lot of properties of eigenvectors without choosing a basis, without determinants and without matrices. We deviate from his definitions by defining 0 not to be an eigenvector, which seems to be the more common convention. ## Notations The expression `algebra_map α (β →ₗ[α] β)` appears very often, which is why we use `am` as a local notation for it. ## References * [Sheldon Axler, *Down with determinants!*, https://www.maa.org/sites/default/files/pdf/awards/Axler-Ford-1996.pdf][axler1996] * https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors ## Tags eigenvector, eigenvalues, eigen -/ universes u v w open vector_space principal_ideal_ring polynomial finite_dimensional variables {α : Type v} {β : Type w} [decidable_eq β] [add_comm_group β] /-- An eigenvector of a linear map `f` with eigenvalue `μ` is a nonzero vector `x` such that `f x = μ • x`. -/ def eigenvector [field α] [vector_space α β] (f : β →ₗ[α] β) (μ : α) (x : β) : Prop := x ≠ 0 ∧ f x = μ • x local notation `am` := algebra_map α (β →ₗ[α] β) /-- Every linear operator on a vector space over an algebraically closed field has an eigenvalue. (Axler's Theorem 2.1.) -/ lemma exists_eigenvector [field α] [is_alg_closed α] [decidable_eq α] [vector_space α β] [finite_dimensional α β] (f : β →ₗ[α] β) (hex : ∃ v : β, v ≠ 0) : ∃ (x : β) (c : α), eigenvector f c x := begin obtain ⟨v, hv⟩ : ∃ v : β, v ≠ 0 := hex, have h_lin_dep : ¬ linear_independent α (λ n : ℕ, (f ^ n) v), { intro h_lin_indep, have : cardinal.mk ℕ < cardinal.omega, by apply (lt_omega_of_linear_independent h_lin_indep), have := cardinal.lift_lt.2 this, rw [cardinal.omega, cardinal.lift_lift] at this, apply lt_irrefl _ this, }, haveI := classical.dec (∃ (x : polynomial α), ¬(polynomial.eval₂ am f x v = 0 → x = 0)), obtain ⟨p, hp⟩ : ∃ p, ¬(eval₂ am f p v = 0 → p = 0), { exact not_forall.1 (λ h, h_lin_dep ((linear_independent_iff_eval₂ f v).2 h)) }, obtain ⟨h_eval_p, h_p_ne_0⟩ : eval₂ am f p v = 0 ∧ p ≠ 0 := not_imp.1 hp, obtain ⟨q, hq_mem, hq_noninj⟩ : ∃ q ∈ factors p, ¬function.injective ⇑(eval₂ am f q), { exact polynomial.exists_noninjective_factor_of_eval₂_0 f v hv p h_p_ne_0 h_eval_p }, have h_q_ne_0 : q ≠ 0 := ne_0_of_mem_factors h_p_ne_0 hq_mem, have h_deg_q : q.degree = 1 := is_alg_closed.degree_eq_one_of_irreducible _ h_q_ne_0 ((factors_spec p h_p_ne_0).1 q hq_mem), have h_q_eval₂ : polynomial.eval₂ am f q = q.leading_coeff • f + am (q.coeff 0), { rw [polynomial.eq_X_add_C_of_degree_eq_one h_deg_q], simp [eval₂_mul_noncomm am f _ _ (λ x y, ( algebra.commutes' x y).symm)], simp [leading_coeff_X_add_C _ _ (λ h, h_q_ne_0 (leading_coeff_eq_zero.1 h))], refl }, obtain ⟨x, hx₁, hx₂⟩ : ∃ (x : β), eval₂ am f q x = 0 ∧ ¬x = 0, { rw [←linear_map.ker_eq_bot, linear_map.ker_eq_bot', classical.not_forall] at hq_noninj, simpa only [not_imp] using hq_noninj }, have h_fx_x_lin_dep: leading_coeff q • f x + coeff q 0 • x = 0, { rw h_q_eval₂ at hx₁, exact hx₁ }, show ∃ (x : β) (c : α), x ≠ 0 ∧ f x = c • x, { use x, use -(coeff q 0 / q.leading_coeff), refine ⟨hx₂, _⟩, rw neg_smul, have : (leading_coeff q)⁻¹ • leading_coeff q • f x = (leading_coeff q)⁻¹ • -(coeff q 0 • x) := congr_arg (λ x, (leading_coeff q)⁻¹ • x) (add_eq_zero_iff_eq_neg.1 h_fx_x_lin_dep), simpa [smul_smul, inv_mul_cancel (λ h, h_q_ne_0 (leading_coeff_eq_zero.1 h)), mul_comm _ (coeff q 0), div_eq_mul_inv.symm] } end /-- Eigenvectors corresponding to distinct eigenvalues of a linear operator are linearly independent (Axler's Proposition 2.2) -/ lemma eigenvectors_linear_independent [field α] [decidable_eq α] [vector_space α β] (f : β →ₗ[α] β) (μs : set α) (xs : μs → β) (h_eigenvec : ∀ μ : μs, eigenvector f μ (xs μ)): linear_independent α xs := begin rw linear_independent_iff, intros l hl, induction h_l_support : l.support using finset.induction with μ₀ l_support' hμ₀ ih generalizing l, { exact finsupp.support_eq_empty.1 h_l_support }, { let l'_f := (λ μ : μs, (↑μ - ↑μ₀) * l μ), have h_l_support' : ∀ (μ : μs), l'_f μ ≠ 0 ↔ μ ∈ l_support', { intros μ, dsimp only [l'_f], rw [mul_ne_zero_iff, sub_ne_zero, ←not_iff_not, not_and_distrib, not_not, not_not, ←subtype.ext_iff], split, { intro h, cases h, { rwa h }, { intro h_mem_l_support', apply finsupp.mem_support_iff.1 _ h, rw h_l_support, apply finset.subset_insert _ _ h_mem_l_support' } }, { intro h, apply (@or_iff_not_imp_right _ _ (classical.dec _)).2, intro hlμ, have := finsupp.mem_support_iff.2 hlμ, rw [h_l_support, finset.mem_insert] at this, cc } }, let l' : μs →₀ α := finsupp.on_finset l_support' l'_f (λ μ, (h_l_support' μ).1), have total_l' : (@linear_map.to_fun α (finsupp μs α) β _ _ _ _ _ (finsupp.total μs β α xs)) l' = 0, { let g := f - am μ₀, have h_gμ₀: g (l μ₀ • xs μ₀) = 0, by rw [linear_map.map_smul, linear_map.sub_apply, (h_eigenvec _).2, module.endomorphism_algebra_map_apply2, sub_self, smul_zero], have h_useless_filter : finset.filter (λ (a : μs), l'_f a ≠ 0) l_support' = l_support', { convert @finset.filter_congr _ _ _ (classical.dec_pred _) (classical.dec_pred _) _ _, { apply finset.filter_true.symm }, exact λ μ hμ, iff_of_true ((h_l_support' μ).2 hμ) true.intro }, have bodies_eq : ∀ (μ : μs), l'_f μ • xs μ = g (l μ • xs μ), { intro μ, dsimp only [g, l'_f], rw [linear_map.map_smul, linear_map.sub_apply, (h_eigenvec _).2, module.endomorphism_algebra_map_apply2, ←sub_smul, smul_smul, mul_comm] }, have := finsupp.total_on_finset l_support' l'_f xs _, unfold_coes at this, rw [this, ←linear_map.map_zero g, ←congr_arg g hl, finsupp.total_apply, finsupp.sum, linear_map.map_sum, h_l_support, finset.sum_insert hμ₀, h_gμ₀, zero_add, h_useless_filter], simp only [bodies_eq] }, have h_l'_support_eq : l'.support = l_support', { dsimp only [l'], ext μ, rw finsupp.on_finset_mem_support l_support' l'_f _ μ, by_cases h_cases: μ ∈ l_support', { refine iff_of_true _ h_cases, exact (h_l_support' μ).2 h_cases }, { refine iff_of_false _ h_cases, rwa not_iff_not.2 (h_l_support' μ) } }, have l'_eq_0 : l' = 0 := ih l' total_l' h_l'_support_eq, have h_mul_eq_0 : ∀ μ : μs, (↑μ - ↑μ₀) * l μ = 0, { intro μ, calc (↑μ - ↑μ₀) * l μ = l' μ : rfl ... = 0 : by { rw [l'_eq_0], refl } }, have h_lμ_eq_0 : ∀ μ : μs, μ ≠ μ₀ → l μ = 0, { intros μ hμ, apply classical.or_iff_not_imp_left.1 (mul_eq_zero.1 (h_mul_eq_0 μ)), rwa [sub_eq_zero, ←subtype.ext_iff] }, have h_sum_l_support'_eq_0 : finset.sum l_support' (λ (μ : ↥μs), l μ • xs μ) = 0, { rw ←finset.sum_const_zero, apply finset.sum_congr rfl, intros μ hμ, rw h_lμ_eq_0, apply zero_smul, intro h, rw h at hμ, contradiction }, have : l μ₀ = 0, { rw [finsupp.total_apply, finsupp.sum, h_l_support, finset.sum_insert hμ₀, h_sum_l_support'_eq_0, add_zero] at hl, by_contra h, exact (h_eigenvec μ₀).1 ((vector_space.smul_neq_zero (xs μ₀) h).1 hl) }, show l = 0, { ext μ, by_cases h_cases : μ = μ₀, { rw h_cases, assumption }, exact h_lμ_eq_0 μ h_cases } } end /-- A generalized eigenvector (also called eventual eigenvector) of a linear map $f$ is a nonzero vector $x$ such that $(f - \mu I) ^ k) x = 0$ for some scalar $\mu$ and some natural number $k$ (where $I$ is the identity map). We deviate from Axler's definition by requiring that $x$ is nonzero, which implies that $k$ is nonzero, too. -/ def generalized_eigenvector [field α] [vector_space α β] (f : β →ₗ[α] β) (k : ℕ) (μ : α) (x : β) : Prop := x ≠ 0 ∧ ((f - am μ) ^ k) x = 0 /-- The natural number of a generalized eigenvector is never 0. -/ lemma exp_ne_zero_of_generalized_eigenvector_ne_zero [field α] [vector_space α β] {f : β →ₗ[α] β} {k : ℕ} {μ : α} {x : β} (h : generalized_eigenvector f k μ x) : k ≠ 0 := begin rcases h with ⟨h_nz, h⟩, contrapose h_nz, rw not_not at h_nz ⊢, rwa [h_nz, pow_zero] at h end /-- A generalized eigenvector for some number `k` is also a generalized eigenvector for number larger than `k`. -/ lemma generalized_eigenvector_zero_beyond [field α] [vector_space α β] {f : β →ₗ[α] β} {k : ℕ} {μ : α} {x : β} (h : generalized_eigenvector f k μ x) : ∀ m : ℕ, k ≤ m → generalized_eigenvector f m μ x := begin intros m hm, refine ⟨h.1, _⟩, rw ←pow_eq_pow_sub_mul _ hm, change ((f - am μ) ^ (m - k)) (((f - am μ) ^ k) x) = 0, unfold generalized_eigenvector at h, rw [h.2, linear_map.map_zero] end /-- All eigenvectors are generalized eigenvectors. -/ lemma generalized_eigenvector_of_eigenvector [field α] [vector_space α β] {f : β →ₗ[α] β} {k : ℕ} {μ : α} {x : β} (hx : eigenvector f μ x) (hk : k > 0) : generalized_eigenvector f k μ x := begin rw [generalized_eigenvector, ←nat.succ_pred_eq_of_pos hk, pow_succ'], change x ≠ 0 ∧ ((f - am μ) ^ nat.pred k) ((f - am μ) x) = 0, have : (f - am μ) x = 0 := by simp [hx.2, module.endomorphism_algebra_map_apply2], simp [this, hx.1] end /-- The set of generalized eigenvectors of f corresponding to an eigenvalue μ equals the kernel of (f - am μ) ^ n, where n is the dimension of the vector space (Axler's Lemma 3.1). -/ lemma generalized_eigenvector_dim [field α] [decidable_eq α] [vector_space α β] [finite_dimensional α β] (f : β →ₗ[α] β) (μ : α) (x : β) : (∃ k : ℕ, generalized_eigenvector f k μ x) ↔ generalized_eigenvector f (findim α β) μ x := begin split, { show (∃ (k : ℕ), generalized_eigenvector f k μ x) → x ≠ 0 ∧ ((f - am μ) ^ findim α β) x = 0, intro h_exists_eigenvec, let k := @nat.find (λ k : ℕ, generalized_eigenvector f k μ x) (classical.dec_pred _) h_exists_eigenvec, let z := (λ i : fin k, ((f - am μ) ^ (i : ℕ)) x), have h_x_nz : x ≠ 0, { rcases h_exists_eigenvec with ⟨k, h⟩, exact h.1 }, have h_lin_indep : linear_independent α z, { rw linear_independent_iff, intros l hl, ext i, induction h_i_val : i.val using nat.strong_induction_on with i_val ih generalizing i, simp only [h_i_val.symm] at *, clear h_i_val i_val, have h_zero_of_lt : ∀ j, j < i → ((f - am μ) ^ (k - i.val - 1)) (l j • z j) = 0, { intros j hj, simp [ih j hj j rfl] }, have h_zero_beyond_k : ∀ m, k ≤ m → ((f - am μ) ^ m) x = 0, { intros m hm, apply (generalized_eigenvector_zero_beyond (@nat.find_spec (λ k : ℕ, generalized_eigenvector f k μ x) (classical.dec_pred _) h_exists_eigenvec) _ hm).2 }, have h_zero_of_gt : ∀ j, j > i → ((f - am μ) ^ (k - i.val - 1)) (l j • z j) = 0, { intros j hj, dsimp only [z], rw [linear_map.map_smul], change l j • ((f - am μ) ^ (k - i.val - 1) * ((f - am μ) ^ ↑j)) x = 0, rw [←pow_add, h_zero_beyond_k, smul_zero], rw [nat.sub_sub, ←nat.sub_add_comm (nat.succ_le_of_lt i.2)], apply nat.le_sub_right_of_add_le, apply nat.add_le_add_left, rw ←nat.lt_iff_add_one_le, unfold_coes, change i.val < (j : ℕ), exact hj }, have h_zero_of_ne : ∀ j, j ≠ i → ((f - am μ) ^ (k - i.val - 1)) (l j • z j) = 0, { intros j hj, cases lt_or_gt_of_ne hj with h_lt h_gt, apply h_zero_of_lt j h_lt, apply h_zero_of_gt j h_gt }, have h_zero_of_not_support : i ∉ l.support → ((f - am μ) ^ (k - i.val - 1)) (l i • z i) = 0, { intros hi, rw [finsupp.mem_support_iff, not_not] at hi, rw [hi, zero_smul, linear_map.map_zero] }, have h_l_smul_pow_k_sub_1 : l i • (((f - am μ) ^ (k - 1)) x) = 0, { have h_k_sub_1 : k - i.val - 1 + i.val = k - 1, { rw ←nat.sub_add_comm, { rw nat.sub_add_cancel, apply le_of_lt i.2 }, { apply nat.le_sub_left_of_add_le, apply nat.succ_le_of_lt i.2 } }, rw [←h_k_sub_1, pow_add], let g := (f - am μ) ^ (k - i.val - 1), rw [finsupp.total_apply, finsupp.sum] at hl, have := congr_arg g hl, rw [linear_map.map_sum, linear_map.map_zero g] at this, dsimp only [g] at this, rw finset.sum_eq_single i (λ j _, h_zero_of_ne j) h_zero_of_not_support at this, simp only [linear_map.map_smul, z] at this, apply this }, have h_pow_k_sub_1 : ((f - am μ) ^ (k - 1)) x ≠ 0 := not_and.1 (@nat.find_min (λ k : ℕ, generalized_eigenvector f k μ x) (classical.dec_pred _) h_exists_eigenvec _ (nat.sub_lt (nat.lt_of_le_of_lt (nat.zero_le _) i.2) nat.zero_lt_one)) h_x_nz, show l i = 0, { contrapose h_pow_k_sub_1 with h_li_ne_0, rw not_not, apply (vector_space.smul_neq_zero _ h_li_ne_0).1, apply h_l_smul_pow_k_sub_1 } }, show x ≠ 0 ∧ ((f - am μ) ^ findim α β) x = 0, { split, { exact h_x_nz }, apply (generalized_eigenvector_zero_beyond (@nat.find_spec (λ k : ℕ, generalized_eigenvector f k μ x) (classical.dec_pred _) h_exists_eigenvec) _ _).2, rw [←cardinal.nat_cast_le, ←cardinal.lift_mk_fin _, ←cardinal.lift_le, cardinal.lift_lift], rw findim_eq_dim, apply h_lin_indep.le_lift_dim} }, { show generalized_eigenvector f (findim α β) μ x → (∃ (k : ℕ), generalized_eigenvector f k μ x), exact λh, ⟨_, h⟩, } end lemma generalized_eigenvector_restrict_aux [field α] [vector_space α β] (f : β →ₗ[α] β) (p : submodule α β) (k : ℕ) (μ : α) (x : p) (hfp : ∀ (x : β), x ∈ p → f x ∈ p) : (((f.restrict p p hfp - algebra_map _ _ μ) ^ k) x : β) = ((f - algebra_map _ _ μ) ^ k) x := begin induction k with k ih, { rw [pow_zero, pow_zero, linear_map.one_app, linear_map.one_app] }, { rw [pow_succ, pow_succ], change ((f.restrict p p hfp - algebra_map _ _ μ) (((f.restrict p p hfp - algebra_map _ _ μ) ^ k) x) : β) = (f - algebra_map _ _ μ) (((f - algebra_map _ _ μ) ^ k) x), rw [linear_map.sub_apply, linear_map.sub_apply, linear_map.restrict_apply, ←ih], refl } end /-- If `f` maps a subspace `p` into itself, then the generalized eigenvectors of `f` restricted to `p` are the generalized eigenvectors of `f` that lie in `p`. -/ lemma generalized_eigenvector_restrict [field α] [vector_space α β] (f : β →ₗ[α] β) (p : submodule α β) (k : ℕ) (μ : α) (x : p) (hfp : ∀ (x : β), x ∈ p → f x ∈ p) : generalized_eigenvector (linear_map.restrict f p p hfp) k μ x ↔ generalized_eigenvector f k μ x := begin rw [generalized_eigenvector, subtype.ext_iff, generalized_eigenvector_restrict_aux], simp [generalized_eigenvector] end /-- If a vector is a generalized eigenvector for some number `k`, then it is also a generalized eigenvector for the dimension of the vector space. -/ lemma generalized_eigenvector_dim_of_any [field α] [decidable_eq α] [vector_space α β] [finite_dimensional α β] {f : β →ₗ[α] β} {μ : α} {k : ℕ} {x : β} (h : generalized_eigenvector f k μ x) : generalized_eigenvector f (findim α β) μ x := begin rw ←generalized_eigenvector_dim, { exact ⟨k, h⟩ } end /-- Kernel and range of $(f - \mu I) ^ n$ are disjoint, where $f$ is a linear map, $\mu$ is a scalar, $I$ is the identity matrix, and $n$ is the dimension of the vector space. -/ lemma generalized_eigenvec_disjoint_range_ker [field α] [decidable_eq α] [vector_space α β] [finite_dimensional α β] (f : β →ₗ[α] β) (μ : α) : disjoint ((f - am μ) ^ findim α β).range ((f - am μ) ^ findim α β).ker := begin rintros v ⟨⟨u, _, hu⟩, hv⟩, have h2n : ((f - am μ) ^ (findim α β + findim α β)) u = 0, { rw [pow_add, ←linear_map.mem_ker.1 hv, ←hu], refl }, have hn : ((f - am μ) ^ findim α β) u = 0, { by_cases h_cases: u = 0, { simp [h_cases] }, { apply (generalized_eigenvector_dim_of_any ⟨h_cases, h2n⟩).2 } }, have hv0 : v = 0, by rw [←hn, hu], show v ∈ ↑⊥, by simp [hv0] end /-- The kernel of $(f - \mu I) ^ k$ for $k > 0$ has positive dimension if $\mu$ is an eigenvalue. -/ lemma pos_dim_eigenker_of_eigenvec [field α] [is_alg_closed α] [vector_space α β] {f : β →ₗ[α] β} {n : ℕ} {μ : α} {x : β} (hx : eigenvector f μ x) : 0 < dim α ((f - am μ) ^ n.succ).ker := begin have x_mem : x ∈ ((f - am μ) ^ n.succ).ker, { simp [pow_succ', hx.2, module.endomorphism_algebra_map_apply2] }, apply dim_pos_of_mem_ne_zero (⟨x, x_mem⟩ : ((f - am μ) ^ n.succ).ker), intros h, apply hx.1, exact congr_arg subtype.val h, end /-- Variant of `pos_dim_eigenker_of_eigenvec` for finite dimensional vector spaces. -/ lemma pos_findim_eigenker_of_eigenvec [field α] [is_alg_closed α] [vector_space α β] [finite_dimensional α β] {f : β →ₗ[α] β} {n : ℕ} {μ : α} {x : β} (hx : eigenvector f μ x) : 0 < findim α ((f - am μ) ^ n.succ).ker := begin apply cardinal.nat_cast_lt.1, rw findim_eq_dim, apply pos_dim_eigenker_of_eigenvec hx, end /-- The kernel of $(f - \mu I) ^ k$ is contained in the span of all generalized eigenvectors. -/ lemma eigenker_le_span_gen_eigenvec [field α] [vector_space α β] (f : β →ₗ[α] β) (μ₀ : α) (n : ℕ) : ((f - am μ₀) ^ n).ker ≤ submodule.span α ({x : β | ∃ (k : ℕ) (μ : α), generalized_eigenvector f k μ x}) := begin intros x hx, by_cases h_cases: x = 0, { simp [h_cases] }, { apply submodule.subset_span, exact ⟨n, μ₀, h_cases, linear_map.mem_ker.1 hx⟩ } end /-- If $x$ is in the range of $(f - \mu I) ^ k$, then so is $f(x)$. -/ lemma image_mem_eigenrange_of_mem_eigenrange [field α] [vector_space α β] {f : β →ₗ[α] β} {μ : α} {x : β} {n : ℕ} (hx : x ∈ ((f - am μ) ^ n).range) : f x ∈ ((f - am μ) ^ n).range := begin rw linear_map.mem_range at *, rcases hx with ⟨w, hw⟩, use f w, have hcommutes : f.comp ((f - am μ) ^ n) = ((f - am μ) ^ n).comp f := algebra.mul_sub_algebra_map_pow_commutes f μ n, rw [←linear_map.comp_apply, ←hcommutes, linear_map.comp_apply, hw], end /-- The generalized eigenvectors of f span the vectorspace β. (Axler's Proposition 3.4). -/ lemma generalized_eigenvector_span [field α] [is_alg_closed α] [decidable_eq α] [vector_space α β] [finite_dimensional α β] (f : β →ₗ[α] β) : submodule.span α {x | ∃ k μ, generalized_eigenvector f k μ x} = ⊤ := begin rw ←top_le_iff, tactic.unfreeze_local_instances, induction h_dim : findim α β using nat.strong_induction_on with n ih generalizing β, cases n, { have h_findim_top: findim α (⊤ : submodule α β) = 0 := eq.trans (@finite_dimensional.findim_top α β _ _ _ _) h_dim, have h_top_eq_bot : (⊤ : submodule α β) = ⊥ := bot_of_findim_zero _ h_findim_top, simp only [h_top_eq_bot, bot_le] }, { have h_dim_pos : 0 < findim α β, { rw [h_dim], apply nat.zero_lt_succ }, obtain ⟨x, μ₀, hx_ne_0, hμ₀⟩ : ∃ (x : β) (μ₀ : α), x ≠ 0 ∧ f x = μ₀ • x, { apply exists_eigenvector f (exists_mem_ne_zero_of_findim_pos h_dim_pos) }, let V₁ := ((f - am μ₀) ^ n.succ).ker, let V₂ := ((f - am μ₀) ^ n.succ).range, have h_disjoint : disjoint V₂ V₁, { simp only [V₁, V₂, h_dim.symm], exact generalized_eigenvec_disjoint_range_ker f μ₀ }, have h_dim_add : findim α V₂ + findim α V₁ = findim α β, { apply linear_map.findim_range_add_findim_ker }, have h_dim_V₁_pos : 0 < findim α V₁, { apply pos_findim_eigenker_of_eigenvec ⟨hx_ne_0, hμ₀⟩ }, have h_findim_V₂ : findim α V₂ < n.succ := by linarith, have h_f_V₂ : ∀ (x : β), x ∈ V₂ → f x ∈ V₂, { intros x hx, apply image_mem_eigenrange_of_mem_eigenrange hx, }, have hV₂ : V₂ ≤ submodule.span α ({x : β | ∃ (k : ℕ) (μ : α), generalized_eigenvector f k μ x}), { have : V₂ ≤ submodule.span α ({x : β | ∃ (k : ℕ) (μ : α), generalized_eigenvector f k μ x} ∩ V₂), { rw ←subtype.image_preimage_val, rw ←submodule.subtype_eq_val V₂, rw submodule.span_image (submodule.subtype V₂), rw set.preimage_set_of_eq, rw submodule.subtype_eq_val, have h₀ : ∀ p, submodule.map (submodule.subtype V₂) ⊤ ≤ submodule.map (submodule.subtype V₂) p ↔ ⊤ ≤ p := λ _, (linear_map.map_le_map_iff' (submodule.ker_subtype V₂)), have := submodule.range_subtype V₂, unfold linear_map.range at this, rw this at h₀, rw h₀, have := ih (findim α V₂) h_findim_V₂ (f.restrict V₂ V₂ h_f_V₂) rfl, simp only [generalized_eigenvector_restrict] at this, apply this }, refine le_trans this _, apply submodule.span_mono, apply set.inter_subset_left }, have hV₁ : V₁ ≤ submodule.span α ({x : β | ∃ (k : ℕ) (μ : α), generalized_eigenvector f k μ x}), { apply eigenker_le_span_gen_eigenvec }, show ⊤ ≤ submodule.span α {x : β | ∃ (k : ℕ) (μ : α), generalized_eigenvector f k μ x}, { rw ←finite_dimensional.eq_top_of_disjoint V₂ V₁ h_dim_add h_disjoint, apply sup_le hV₂ hV₁ } } end
64e0c26a4029cac58ecaa79518fec72a8df68109
a047a4718edfa935d17231e9e6ecec8c7b701e05
/src/linear_algebra/finsupp_vector_space.lean
577fe275b0634858315b778f3abefccf893a2f90
[ "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
6,334
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl Linear structures on function with finite support `ι →₀ β`. -/ import data.mv_polynomial import linear_algebra.dimension noncomputable theory local attribute [instance, priority 100] classical.prop_decidable open set linear_map submodule namespace finsupp section module variables {R : Type*} {M : Type*} {ι : Type*} variables [ring R] [add_comm_group M] [module R M] lemma linear_independent_single {φ : ι → Type*} {f : Π ι, φ ι → M} (hf : ∀i, linear_independent R (f i)) : linear_independent R (λ ix : Σ i, φ i, single ix.1 (f ix.1 ix.2)) := begin apply @linear_independent_Union_finite R _ _ _ _ ι φ (λ i x, single i (f i x)), { assume i, have h_disjoint : disjoint (span R (range (f i))) (ker (lsingle i)), { rw ker_lsingle, exact disjoint_bot_right }, apply linear_independent.image (hf i) h_disjoint }, { intros i t ht hit, refine (disjoint_lsingle_lsingle {i} t (disjoint_singleton_left.2 hit)).mono _ _, { rw span_le, simp only [supr_singleton], rw range_coe, apply range_comp_subset_range }, { refine supr_le_supr (λ i, supr_le_supr _), intros hi, rw span_le, rw range_coe, apply range_comp_subset_range } } end end module section vector_space variables {K : Type*} {V : Type*} {ι : Type*} variables [field K] [add_comm_group V] [vector_space K V] open linear_map submodule lemma is_basis_single {φ : ι → Type*} (f : Π ι, φ ι → V) (hf : ∀i, is_basis K (f i)) : is_basis K (λ ix : Σ i, φ i, single ix.1 (f ix.1 ix.2)) := begin split, { apply linear_independent_single, exact λ i, (hf i).1 }, { rw [range_sigma_eq_Union_range, span_Union], simp only [image_univ.symm, λ i, image_comp (single i) (f i), span_single_image], simp only [image_univ, (hf _).2, map_top, supr_lsingle_range] } end end vector_space section dim universes u v variables {K : Type u} {V : Type v} {ι : Type v} variables [field K] [add_comm_group V] [vector_space K V] lemma dim_eq : vector_space.dim K (ι →₀ V) = cardinal.mk ι * vector_space.dim K V := begin rcases exists_is_basis K V with ⟨bs, hbs⟩, rw [← cardinal.lift_inj, cardinal.lift_mul, ← hbs.mk_eq_dim, ← (is_basis_single _ (λa:ι, hbs)).mk_eq_dim, ← cardinal.sum_mk, ← cardinal.lift_mul, cardinal.lift_inj], { simp only [cardinal.mk_image_eq (injective_single.{u u} _), cardinal.sum_const] } end end dim end finsupp section vector_space /- We use `universe variables` instead of `universes` here because universes introduced by the `universes` keyword do not get replaced by metavariables once a lemma has been proven. So if you prove a lemma using universe `u`, you can only apply it to universe `u` in other lemmas of the same section. -/ universe variables u v w variables {K : Type u} {V V₁ V₂ : Type v} {V' : Type w} variables [field K] variables [add_comm_group V] [vector_space K V] variables [add_comm_group V₁] [vector_space K V₁] variables [add_comm_group V₂] [vector_space K V₂] variables [add_comm_group V'] [vector_space K V'] open vector_space lemma equiv_of_dim_eq_lift_dim (h : cardinal.lift.{v w} (dim K V) = cardinal.lift.{w v} (dim K V')) : nonempty (V ≃ₗ[K] V') := begin haveI := classical.dec_eq V, haveI := classical.dec_eq V', rcases exists_is_basis K V with ⟨m, hm⟩, rcases exists_is_basis K V' with ⟨m', hm'⟩, rw [←cardinal.lift_inj.1 hm.mk_eq_dim, ←cardinal.lift_inj.1 hm'.mk_eq_dim] at h, rcases quotient.exact h with ⟨e⟩, let e := (equiv.ulift.symm.trans e).trans equiv.ulift, exact ⟨((module_equiv_finsupp hm).trans (finsupp.dom_lcongr e)).trans (module_equiv_finsupp hm').symm⟩, end def equiv_of_dim_eq_dim (h : dim K V₁ = dim K V₂) : V₁ ≃ₗ[K] V₂ := begin classical, exact classical.choice (equiv_of_dim_eq_lift_dim (cardinal.lift_inj.2 h)) end def fin_dim_vectorspace_equiv (n : ℕ) (hn : (dim K V) = n) : V ≃ₗ[K] (fin n → K) := begin have : cardinal.lift.{v u} (n : cardinal.{v}) = cardinal.lift.{u v} (n : cardinal.{u}), by simp, have hn := cardinal.lift_inj.{v u}.2 hn, rw this at hn, rw ←@dim_fin_fun K _ n at hn, exact classical.choice (equiv_of_dim_eq_lift_dim hn), end lemma eq_bot_iff_dim_eq_zero (p : submodule K V) (h : dim K p = 0) : p = ⊥ := begin have : dim K p = dim K (⊥ : submodule K V) := by rwa [dim_bot], let e := equiv_of_dim_eq_dim this, exact e.eq_bot_of_equiv _ end lemma injective_of_surjective (f : V₁ →ₗ[K] V₂) (hV₁ : dim K V₁ < cardinal.omega) (heq : dim K V₂ = dim K V₁) (hf : f.range = ⊤) : f.ker = ⊥ := have hk : dim K f.ker < cardinal.omega := lt_of_le_of_lt (dim_submodule_le _) hV₁, begin rcases cardinal.lt_omega.1 hV₁ with ⟨d₁, eq₁⟩, rcases cardinal.lt_omega.1 hk with ⟨d₂, eq₂⟩, have : 0 = d₂, { have := dim_eq_surjective f (linear_map.range_eq_top.1 hf), rw [heq, eq₁, eq₂, ← nat.cast_add, cardinal.nat_cast_inj] at this, exact nat.add_left_cancel this }, refine eq_bot_iff_dim_eq_zero _ _, rw [eq₂, ← this, nat.cast_zero] end end vector_space section vector_space universes u open vector_space variables {K V : Type u} [field K] [add_comm_group V] [vector_space K V] lemma cardinal_mk_eq_cardinal_mk_field_pow_dim (h : dim K V < cardinal.omega) : cardinal.mk V = cardinal.mk K ^ dim K V := begin rcases exists_is_basis K V with ⟨s, hs⟩, have : nonempty (fintype s), { rwa [← cardinal.lt_omega_iff_fintype, cardinal.lift_inj.1 hs.mk_eq_dim] }, cases this with hsf, letI := hsf, calc cardinal.mk V = cardinal.mk (s →₀ K) : quotient.sound ⟨(module_equiv_finsupp hs).to_equiv⟩ ... = cardinal.mk (s → K) : quotient.sound ⟨finsupp.equiv_fun_on_fintype⟩ ... = _ : by rw [← cardinal.lift_inj.1 hs.mk_eq_dim, cardinal.power_def] end lemma cardinal_lt_omega_of_dim_lt_omega [fintype K] (h : dim K V < cardinal.omega) : cardinal.mk V < cardinal.omega := begin rw [cardinal_mk_eq_cardinal_mk_field_pow_dim h], exact cardinal.power_lt_omega (cardinal.lt_omega_iff_fintype.2 ⟨infer_instance⟩) h end end vector_space
9c974f09df83c1f32451234e6720da7291dcf0f7
8930e38ac0fae2e5e55c28d0577a8e44e2639a6d
/data/equiv/denumerable.lean
3649efcc6a8c552e5181b11a2ca2c8a026769123
[ "Apache-2.0" ]
permissive
SG4316/mathlib
3d64035d02a97f8556ad9ff249a81a0a51a3321a
a7846022507b531a8ab53b8af8a91953fceafd3a
refs/heads/master
1,584,869,960,527
1,530,718,645,000
1,530,724,110,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,873
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Denumerable (countably infinite) types, as a typeclass extending encodable. This is used to provide explicit encode/decode functions from nat, where the functions are known inverses of each other. -/ import data.equiv.encodable open nat /-- A denumerable type is one which is (constructively) bijective with ℕ. Although we already have a name for this property, namely `α ≃ ℕ`, we are here interested in using it as a typeclass. -/ class denumerable (α : Type*) extends encodable α := (decode_inv : ∀ n, ∃ a ∈ decode n, encode a = n) namespace denumerable section variables {α : Type*} {β : Type*} [denumerable α] [denumerable β] open encodable @[simp] theorem decode_is_some (α) [denumerable α] (n : ℕ) : (decode α n).is_some := option.is_some_iff_exists.2 $ (decode_inv α n).imp $ λ a, Exists.fst def of_nat (α) [f : denumerable α] (n : ℕ) : α := option.get (decode_is_some α n) @[simp] theorem decode_eq_of_nat (α) [denumerable α] (n : ℕ) : decode α n = some (of_nat α n) := option.eq_some_of_is_some _ @[simp] theorem of_nat_of_decode {n b} (h : decode α n = some b) : of_nat α n = b := option.some.inj $ (decode_eq_of_nat _ _).symm.trans h @[simp] theorem encode_of_nat (n) : encode (of_nat α n) = n := let ⟨a, h, e⟩ := decode_inv α n in by rwa [of_nat_of_decode h] @[simp] theorem of_nat_encode (a) : of_nat α (encode a) = a := of_nat_of_decode (encodek _) def eqv (α) [denumerable α] : α ≃ ℕ := ⟨encode, of_nat α, of_nat_encode, encode_of_nat⟩ def mk' {α} (e : α ≃ ℕ) : denumerable α := { encode := e, decode := some ∘ e.symm, encodek := λ a, congr_arg some (e.inverse_apply_apply _), decode_inv := λ n, ⟨_, rfl, e.apply_inverse_apply _⟩ } def of_equiv (α) {β} [denumerable α] (e : β ≃ α) : denumerable β := { decode_inv := λ n, by simp [option.bind], ..encodable.of_equiv _ e } @[simp] theorem of_equiv_of_nat (α) {β} [denumerable α] (e : β ≃ α) (n) : @of_nat β (of_equiv _ e) n = e.symm (of_nat α n) := by apply of_nat_of_decode; show option.map _ _ = _; simp; refl def equiv₂ (α β) [denumerable α] [denumerable β] : α ≃ β := (eqv α).trans (eqv β).symm instance nat : denumerable nat := ⟨λ n, ⟨_, rfl, rfl⟩⟩ @[simp] theorem of_nat_nat (n) : of_nat ℕ n = n := rfl instance option : denumerable (option α) := ⟨λ n, by cases n; simp⟩ instance sum : denumerable (α ⊕ β) := ⟨λ n, begin suffices : ∃ a ∈ @decode_sum α β _ _ n, encode_sum a = bit (bodd n) (div2 n), {simpa [bit_decomp]}, simp [decode_sum]; cases bodd n; simp [decode_sum, bit], { refine or.inl ⟨_, rfl, _⟩, simp [encode_sum] }, { refine or.inr ⟨_, rfl, _⟩, simp [encode_sum] } end⟩ section sigma variables {γ : α → Type*} [∀ a, denumerable (γ a)] instance sigma : denumerable (sigma γ) := ⟨λ n, by simp [decode_sigma]; exact ⟨_, _, rfl, by simp⟩⟩ @[simp] theorem sigma_of_nat_val (n : ℕ) : of_nat (sigma γ) n = ⟨of_nat α (unpair n).1, of_nat (γ _) (unpair n).2⟩ := option.some.inj $ by rw [← decode_eq_of_nat, decode_sigma_val]; simp; refl end sigma instance prod : denumerable (α × β) := of_equiv _ (equiv.sigma_equiv_prod α β).symm @[simp] theorem prod_of_nat_val (n : ℕ) : of_nat (α × β) n = (of_nat α (unpair n).1, of_nat β (unpair n).2) := by simp; refl @[simp] theorem prod_nat_of_nat : of_nat (ℕ × ℕ) = unpair := by funext; simp instance int : denumerable ℤ := of_equiv _ equiv.int_equiv_nat instance ulift : denumerable (ulift α) := of_equiv _ equiv.ulift instance plift : denumerable (plift α) := of_equiv _ equiv.plift def pair : (α × α) ≃ α := equiv₂ _ _ end end denumerable
5076f36a1a272f534555fff76c73ba8a4481c9cf
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/data/real/nnreal.lean
f78e1cd7058063c000d05c801cf7c6be28166abc
[ "Apache-2.0" ]
permissive
JLimperg/aesop3
306cc6570c556568897ed2e508c8869667252e8a
a4a116f650cc7403428e72bd2e2c4cda300fe03f
refs/heads/master
1,682,884,916,368
1,620,320,033,000
1,620,320,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,951
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.linear_ordered_comm_group_with_zero import algebra.big_operators.ring import data.real.basic import data.indicator_function import algebra.algebra.basic /-! # Nonnegative real numbers In this file we define `nnreal` (notation: `ℝ≥0`) to be the type of non-negative real numbers, a.k.a. the interval `[0, ∞)`. We also define the following operations and structures on `ℝ≥0`: * the order on `ℝ≥0` is the restriction of the order on `ℝ`; these relations define a conditionally complete linear order with a bottom element, `conditionally_complete_linear_order_bot`; * `a + b` and `a * b` are the restrictions of addition and multiplication of real numbers to `ℝ≥0`; these operations together with `0 = ⟨0, _⟩` and `1 = ⟨1, _⟩` turn `ℝ≥0` into a linear ordered archimedean commutative semifield; we have no typeclass for this in `mathlib` yet, so we define the following instances instead: - `linear_ordered_semiring ℝ≥0`; - `comm_semiring ℝ≥0`; - `canonically_ordered_comm_semiring ℝ≥0`; - `linear_ordered_comm_group_with_zero ℝ≥0`; - `archimedean ℝ≥0`. * `nnreal.of_real x` is defined as `⟨max x 0, _⟩`, i.e. `↑(nnreal.of_real x) = x` when `0 ≤ x` and `↑(nnreal.of_real x) = 0` otherwise. We also define an instance `can_lift ℝ ℝ≥0`. This instance can be used by the `lift` tactic to replace `x : ℝ` and `hx : 0 ≤ x` in the proof context with `x : ℝ≥0` while replacing all occurences of `x` with `↑x`. This tactic also works for a function `f : α → ℝ` with a hypothesis `hf : ∀ x, 0 ≤ f x`. ## Notations This file defines `ℝ≥0` as a localized notation for `nnreal`. -/ noncomputable theory open_locale classical big_operators /-- Nonnegative real numbers. -/ def nnreal := {r : ℝ // 0 ≤ r} localized "notation ` ℝ≥0 ` := nnreal" in nnreal namespace nnreal instance : has_coe ℝ≥0 ℝ := ⟨subtype.val⟩ /- Simp lemma to put back `n.val` into the normal form given by the coercion. -/ @[simp] lemma val_eq_coe (n : ℝ≥0) : n.val = n := rfl instance : can_lift ℝ ℝ≥0 := { coe := coe, cond := λ r, 0 ≤ r, prf := λ x hx, ⟨⟨x, hx⟩, rfl⟩ } protected lemma eq {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) → n = m := subtype.eq protected lemma eq_iff {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) ↔ n = m := iff.intro nnreal.eq (congr_arg coe) lemma ne_iff {x y : ℝ≥0} : (x : ℝ) ≠ (y : ℝ) ↔ x ≠ y := not_iff_not_of_iff $ nnreal.eq_iff /-- Reinterpret a real number `r` as a non-negative real number. Returns `0` if `r < 0`. -/ protected def of_real (r : ℝ) : ℝ≥0 := ⟨max r 0, le_max_right _ _⟩ lemma coe_of_real (r : ℝ) (hr : 0 ≤ r) : (nnreal.of_real r : ℝ) = r := max_eq_left hr lemma le_coe_of_real (r : ℝ) : r ≤ nnreal.of_real r := le_max_left r 0 lemma coe_nonneg (r : ℝ≥0) : (0 : ℝ) ≤ r := r.2 @[norm_cast] theorem coe_mk (a : ℝ) (ha) : ((⟨a, ha⟩ : ℝ≥0) : ℝ) = a := rfl instance : has_zero ℝ≥0 := ⟨⟨0, le_refl 0⟩⟩ instance : has_one ℝ≥0 := ⟨⟨1, zero_le_one⟩⟩ instance : has_add ℝ≥0 := ⟨λa b, ⟨a + b, add_nonneg a.2 b.2⟩⟩ instance : has_sub ℝ≥0 := ⟨λa b, nnreal.of_real (a - b)⟩ instance : has_mul ℝ≥0 := ⟨λa b, ⟨a * b, mul_nonneg a.2 b.2⟩⟩ instance : has_inv ℝ≥0 := ⟨λa, ⟨(a.1)⁻¹, inv_nonneg.2 a.2⟩⟩ instance : has_div ℝ≥0 := ⟨λa b, ⟨a / b, div_nonneg a.2 b.2⟩⟩ instance : has_le ℝ≥0 := ⟨λ r s, (r:ℝ) ≤ s⟩ instance : has_bot ℝ≥0 := ⟨0⟩ instance : inhabited ℝ≥0 := ⟨0⟩ protected lemma coe_injective : function.injective (coe : ℝ≥0 → ℝ) := subtype.coe_injective @[simp, norm_cast] protected lemma coe_eq {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) = r₂ ↔ r₁ = r₂ := nnreal.coe_injective.eq_iff @[simp, norm_cast] protected lemma coe_zero : ((0 : ℝ≥0) : ℝ) = 0 := rfl @[simp, norm_cast] protected lemma coe_one : ((1 : ℝ≥0) : ℝ) = 1 := rfl @[simp, norm_cast] protected lemma coe_add (r₁ r₂ : ℝ≥0) : ((r₁ + r₂ : ℝ≥0) : ℝ) = r₁ + r₂ := rfl @[simp, norm_cast] protected lemma coe_mul (r₁ r₂ : ℝ≥0) : ((r₁ * r₂ : ℝ≥0) : ℝ) = r₁ * r₂ := rfl @[simp, norm_cast] protected lemma coe_inv (r : ℝ≥0) : ((r⁻¹ : ℝ≥0) : ℝ) = r⁻¹ := rfl @[simp, norm_cast] protected lemma coe_div (r₁ r₂ : ℝ≥0) : ((r₁ / r₂ : ℝ≥0) : ℝ) = r₁ / r₂ := rfl @[simp, norm_cast] protected lemma coe_bit0 (r : ℝ≥0) : ((bit0 r : ℝ≥0) : ℝ) = bit0 r := rfl @[simp, norm_cast] protected lemma coe_bit1 (r : ℝ≥0) : ((bit1 r : ℝ≥0) : ℝ) = bit1 r := rfl @[simp, norm_cast] protected lemma coe_sub {r₁ r₂ : ℝ≥0} (h : r₂ ≤ r₁) : ((r₁ - r₂ : ℝ≥0) : ℝ) = r₁ - r₂ := max_eq_left $ le_sub.2 $ by simp [show (r₂ : ℝ) ≤ r₁, from h] -- TODO: setup semifield! @[simp] protected lemma coe_eq_zero (r : ℝ≥0) : ↑r = (0 : ℝ) ↔ r = 0 := by norm_cast lemma coe_ne_zero {r : ℝ≥0} : (r : ℝ) ≠ 0 ↔ r ≠ 0 := by norm_cast instance : comm_semiring ℝ≥0 := { zero := 0, add := (+), one := 1, mul := (*), .. nnreal.coe_injective.comm_semiring _ rfl rfl (λ _ _, rfl) (λ _ _, rfl) } /-- Coercion `ℝ≥0 → ℝ` as a `ring_hom`. -/ def to_real_hom : ℝ≥0 →+* ℝ := ⟨coe, nnreal.coe_one, nnreal.coe_mul, nnreal.coe_zero, nnreal.coe_add⟩ /-- The real numbers are an algebra over the non-negative reals. -/ instance : algebra ℝ≥0 ℝ := to_real_hom.to_algebra @[simp] lemma coe_to_real_hom : ⇑to_real_hom = coe := rfl instance : comm_group_with_zero ℝ≥0 := { zero := 0, mul := (*), one := 1, inv := has_inv.inv, div := (/), .. nnreal.coe_injective.comm_group_with_zero _ rfl rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) } @[simp, norm_cast] lemma coe_indicator {α} (s : set α) (f : α → ℝ≥0) (a : α) : ((s.indicator f a : ℝ≥0) : ℝ) = s.indicator (λ x, f x) a := (to_real_hom : ℝ≥0 →+ ℝ).map_indicator _ _ _ @[simp, norm_cast] lemma coe_pow (r : ℝ≥0) (n : ℕ) : ((r^n : ℝ≥0) : ℝ) = r^n := to_real_hom.map_pow r n @[norm_cast] lemma coe_list_sum (l : list ℝ≥0) : ((l.sum : ℝ≥0) : ℝ) = (l.map coe).sum := to_real_hom.map_list_sum l @[norm_cast] lemma coe_list_prod (l : list ℝ≥0) : ((l.prod : ℝ≥0) : ℝ) = (l.map coe).prod := to_real_hom.map_list_prod l @[norm_cast] lemma coe_multiset_sum (s : multiset ℝ≥0) : ((s.sum : ℝ≥0) : ℝ) = (s.map coe).sum := to_real_hom.map_multiset_sum s @[norm_cast] lemma coe_multiset_prod (s : multiset ℝ≥0) : ((s.prod : ℝ≥0) : ℝ) = (s.map coe).prod := to_real_hom.map_multiset_prod s @[norm_cast] lemma coe_sum {α} {s : finset α} {f : α → ℝ≥0} : ↑(∑ a in s, f a) = ∑ a in s, (f a : ℝ) := to_real_hom.map_sum _ _ lemma of_real_sum_of_nonneg {α} {s : finset α} {f : α → ℝ} (hf : ∀ a, a ∈ s → 0 ≤ f a) : nnreal.of_real (∑ a in s, f a) = ∑ a in s, nnreal.of_real (f a) := begin rw [←nnreal.coe_eq, nnreal.coe_sum, nnreal.coe_of_real _ (finset.sum_nonneg hf)], exact finset.sum_congr rfl (λ x hxs, by rw nnreal.coe_of_real _ (hf x hxs)), end @[norm_cast] lemma coe_prod {α} {s : finset α} {f : α → ℝ≥0} : ↑(∏ a in s, f a) = ∏ a in s, (f a : ℝ) := to_real_hom.map_prod _ _ lemma of_real_prod_of_nonneg {α} {s : finset α} {f : α → ℝ} (hf : ∀ a, a ∈ s → 0 ≤ f a) : nnreal.of_real (∏ a in s, f a) = ∏ a in s, nnreal.of_real (f a) := begin rw [←nnreal.coe_eq, nnreal.coe_prod, nnreal.coe_of_real _ (finset.prod_nonneg hf)], exact finset.prod_congr rfl (λ x hxs, by rw nnreal.coe_of_real _ (hf x hxs)), end @[norm_cast] lemma nsmul_coe (r : ℝ≥0) (n : ℕ) : ↑(n • r) = n • (r:ℝ) := to_real_hom.to_add_monoid_hom.map_nsmul _ _ @[simp, norm_cast] protected lemma coe_nat_cast (n : ℕ) : (↑(↑n : ℝ≥0) : ℝ) = n := to_real_hom.map_nat_cast n instance : linear_order ℝ≥0 := linear_order.lift (coe : ℝ≥0 → ℝ) nnreal.coe_injective @[simp, norm_cast] protected lemma coe_le_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) ≤ r₂ ↔ r₁ ≤ r₂ := iff.rfl @[simp, norm_cast] protected lemma coe_lt_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) < r₂ ↔ r₁ < r₂ := iff.rfl @[simp, norm_cast] protected lemma coe_pos {r : ℝ≥0} : (0 : ℝ) < r ↔ 0 < r := iff.rfl protected lemma coe_mono : monotone (coe : ℝ≥0 → ℝ) := λ _ _, nnreal.coe_le_coe.2 protected lemma of_real_mono : monotone nnreal.of_real := λ x y h, max_le_max h (le_refl 0) @[simp] lemma of_real_coe {r : ℝ≥0} : nnreal.of_real r = r := nnreal.eq $ max_eq_left r.2 @[simp] lemma mk_coe_nat (n : ℕ) : @eq ℝ≥0 (⟨(n : ℝ), n.cast_nonneg⟩ : ℝ≥0) n := nnreal.eq (nnreal.coe_nat_cast n).symm @[simp] lemma of_real_coe_nat (n : ℕ) : nnreal.of_real n = n := nnreal.eq $ by simp [coe_of_real] /-- `nnreal.of_real` and `coe : ℝ≥0 → ℝ` form a Galois insertion. -/ protected def gi : galois_insertion nnreal.of_real coe := galois_insertion.monotone_intro nnreal.coe_mono nnreal.of_real_mono le_coe_of_real (λ _, of_real_coe) instance : order_bot ℝ≥0 := { bot := ⊥, bot_le := assume ⟨a, h⟩, h, .. nnreal.linear_order } instance : canonically_linear_ordered_add_monoid ℝ≥0 := { add_le_add_left := assume a b h c, @add_le_add_left ℝ _ a b h c, lt_of_add_lt_add_left := assume a b c, @lt_of_add_lt_add_left ℝ _ a b c, le_iff_exists_add := assume ⟨a, ha⟩ ⟨b, hb⟩, iff.intro (assume h : a ≤ b, ⟨⟨b - a, le_sub_iff_add_le.2 $ by simp [h]⟩, nnreal.eq $ show b = a + (b - a), by rw [add_sub_cancel'_right]⟩) (assume ⟨⟨c, hc⟩, eq⟩, eq.symm ▸ show a ≤ a + c, from (le_add_iff_nonneg_right a).2 hc), ..nnreal.comm_semiring, ..nnreal.order_bot, ..nnreal.linear_order } instance : distrib_lattice ℝ≥0 := by apply_instance instance : semilattice_inf_bot ℝ≥0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : semilattice_sup_bot ℝ≥0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : linear_ordered_semiring ℝ≥0 := { add_left_cancel := assume a b c h, nnreal.eq $ @add_left_cancel ℝ _ a b c (nnreal.eq_iff.2 h), le_of_add_le_add_left := assume a b c, @le_of_add_le_add_left ℝ _ a b c, mul_lt_mul_of_pos_left := assume a b c, @mul_lt_mul_of_pos_left ℝ _ a b c, mul_lt_mul_of_pos_right := assume a b c, @mul_lt_mul_of_pos_right ℝ _ a b c, zero_le_one := @zero_le_one ℝ _, exists_pair_ne := ⟨0, 1, ne_of_lt (@zero_lt_one ℝ _ _)⟩, .. nnreal.canonically_linear_ordered_add_monoid, .. nnreal.comm_semiring, } instance : linear_ordered_comm_group_with_zero ℝ≥0 := { mul_le_mul_left := assume a b h c, mul_le_mul (le_refl c) h (zero_le a) (zero_le c), zero_le_one := zero_le 1, .. nnreal.linear_ordered_semiring, .. nnreal.comm_group_with_zero } instance : canonically_ordered_comm_semiring ℝ≥0 := { .. nnreal.canonically_linear_ordered_add_monoid, .. nnreal.comm_semiring, .. (show no_zero_divisors ℝ≥0, by apply_instance), .. nnreal.comm_group_with_zero } instance : densely_ordered ℝ≥0 := ⟨assume a b (h : (a : ℝ) < b), let ⟨c, hac, hcb⟩ := exists_between h in ⟨⟨c, le_trans a.property $ le_of_lt $ hac⟩, hac, hcb⟩⟩ instance : no_top_order ℝ≥0 := ⟨assume a, let ⟨b, hb⟩ := no_top (a:ℝ) in ⟨⟨b, le_trans a.property $ le_of_lt $ hb⟩, hb⟩⟩ lemma bdd_above_coe {s : set ℝ≥0} : bdd_above ((coe : ℝ≥0 → ℝ) '' s) ↔ bdd_above s := iff.intro (assume ⟨b, hb⟩, ⟨nnreal.of_real b, assume ⟨y, hy⟩ hys, show y ≤ max b 0, from le_max_left_of_le $ hb $ set.mem_image_of_mem _ hys⟩) (assume ⟨b, hb⟩, ⟨b, assume y ⟨x, hx, eq⟩, eq ▸ hb hx⟩) lemma bdd_below_coe (s : set ℝ≥0) : bdd_below ((coe : ℝ≥0 → ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq ▸ q.2⟩ instance : has_Sup ℝ≥0 := ⟨λs, ⟨Sup ((coe : ℝ≥0 → ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Sup_empty] }, rcases h with ⟨⟨b, hb⟩, hbs⟩, by_cases h' : bdd_above s, { exact le_cSup_of_le (bdd_above_coe.2 h') (set.mem_image_of_mem _ hbs) hb }, { rw [real.Sup_of_not_bdd_above], rwa [bdd_above_coe] } end⟩⟩ instance : has_Inf ℝ≥0 := ⟨λs, ⟨Inf ((coe : ℝ≥0 → ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Inf_empty] }, exact le_cInf (h.image _) (assume r ⟨q, _, eq⟩, eq ▸ q.2) end⟩⟩ lemma coe_Sup (s : set ℝ≥0) : (↑(Sup s) : ℝ) = Sup ((coe : ℝ≥0 → ℝ) '' s) := rfl lemma coe_Inf (s : set ℝ≥0) : (↑(Inf s) : ℝ) = Inf ((coe : ℝ≥0 → ℝ) '' s) := rfl instance : conditionally_complete_linear_order_bot ℝ≥0 := { Sup := Sup, Inf := Inf, le_cSup := assume s a hs ha, le_cSup (bdd_above_coe.2 hs) (set.mem_image_of_mem _ ha), cSup_le := assume s a hs h,show Sup ((coe : ℝ≥0 → ℝ) '' s) ≤ a, from cSup_le (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cInf_le := assume s a _ has, cInf_le (bdd_below_coe s) (set.mem_image_of_mem _ has), le_cInf := assume s a hs h, show (↑a : ℝ) ≤ Inf ((coe : ℝ≥0 → ℝ) '' s), from le_cInf (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cSup_empty := nnreal.eq $ by simp [coe_Sup, real.Sup_empty]; refl, decidable_le := begin assume x y, apply classical.dec end, .. nnreal.linear_ordered_semiring, .. lattice_of_linear_order, .. nnreal.order_bot } instance : archimedean ℝ≥0 := ⟨ assume x y pos_y, let ⟨n, hr⟩ := archimedean.arch (x:ℝ) (pos_y : (0 : ℝ) < y) in ⟨n, show (x:ℝ) ≤ (n • y : ℝ≥0), by simp [*, -nsmul_eq_mul, nsmul_coe]⟩ ⟩ lemma le_of_forall_pos_le_add {a b : ℝ≥0} (h : ∀ε, 0 < ε → a ≤ b + ε) : a ≤ b := le_of_forall_le_of_dense $ assume x hxb, begin rcases le_iff_exists_add.1 (le_of_lt hxb) with ⟨ε, rfl⟩, exact h _ ((lt_add_iff_pos_right b).1 hxb) end -- TODO: generalize to some ordered add_monoids, based on #6145 lemma le_of_add_le_left {a b c : ℝ≥0} (h : a + b ≤ c) : a ≤ c := by { refine le_trans _ h, simp } lemma le_of_add_le_right {a b c : ℝ≥0} (h : a + b ≤ c) : b ≤ c := by { refine le_trans _ h, simp } lemma lt_iff_exists_rat_btwn (a b : ℝ≥0) : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < nnreal.of_real q ∧ nnreal.of_real q < b) := iff.intro (assume (h : (↑a:ℝ) < (↑b:ℝ)), let ⟨q, haq, hqb⟩ := exists_rat_btwn h in have 0 ≤ (q : ℝ), from le_trans a.2 $ le_of_lt haq, ⟨q, rat.cast_nonneg.1 this, by simp [coe_of_real _ this, nnreal.coe_lt_coe.symm, haq, hqb]⟩) (assume ⟨q, _, haq, hqb⟩, lt_trans haq hqb) lemma bot_eq_zero : (⊥ : ℝ≥0) = 0 := rfl lemma mul_sup (a b c : ℝ≥0) : a * (b ⊔ c) = (a * b) ⊔ (a * c) := begin cases le_total b c with h h, { simp [sup_eq_max, max_eq_right h, max_eq_right (mul_le_mul_of_nonneg_left h (zero_le a))] }, { simp [sup_eq_max, max_eq_left h, max_eq_left (mul_le_mul_of_nonneg_left h (zero_le a))] }, end lemma mul_finset_sup {α} {f : α → ℝ≥0} {s : finset α} (r : ℝ≥0) : r * s.sup f = s.sup (λa, r * f a) := begin refine s.induction_on _ _, { simp [bot_eq_zero] }, { assume a s has ih, simp [has, ih, mul_sup], } end @[simp, norm_cast] lemma coe_max (x y : ℝ≥0) : ((max x y : ℝ≥0) : ℝ) = max (x : ℝ) (y : ℝ) := by { delta max, split_ifs; refl } @[simp, norm_cast] lemma coe_min (x y : ℝ≥0) : ((min x y : ℝ≥0) : ℝ) = min (x : ℝ) (y : ℝ) := by { delta min, split_ifs; refl } section of_real @[simp] lemma zero_le_coe {q : ℝ≥0} : 0 ≤ (q : ℝ) := q.2 @[simp] lemma of_real_zero : nnreal.of_real 0 = 0 := by simp [nnreal.of_real]; refl @[simp] lemma of_real_one : nnreal.of_real 1 = 1 := by simp [nnreal.of_real, max_eq_left (zero_le_one : (0 :ℝ) ≤ 1)]; refl @[simp] lemma of_real_pos {r : ℝ} : 0 < nnreal.of_real r ↔ 0 < r := by simp [nnreal.of_real, nnreal.coe_lt_coe.symm, lt_irrefl] @[simp] lemma of_real_eq_zero {r : ℝ} : nnreal.of_real r = 0 ↔ r ≤ 0 := by simpa [-of_real_pos] using (not_iff_not.2 (@of_real_pos r)) lemma of_real_of_nonpos {r : ℝ} : r ≤ 0 → nnreal.of_real r = 0 := of_real_eq_zero.2 @[simp] lemma of_real_le_of_real_iff {r p : ℝ} (hp : 0 ≤ p) : nnreal.of_real r ≤ nnreal.of_real p ↔ r ≤ p := by simp [nnreal.coe_le_coe.symm, nnreal.of_real, hp] @[simp] lemma of_real_lt_of_real_iff' {r p : ℝ} : nnreal.of_real r < nnreal.of_real p ↔ r < p ∧ 0 < p := by simp [nnreal.coe_lt_coe.symm, nnreal.of_real, lt_irrefl] lemma of_real_lt_of_real_iff {r p : ℝ} (h : 0 < p) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans (and_iff_left h) lemma of_real_lt_of_real_iff_of_nonneg {r p : ℝ} (hr : 0 ≤ r) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans ⟨and.left, λ h, ⟨h, lt_of_le_of_lt hr h⟩⟩ @[simp] lemma of_real_add {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real (r + p) = nnreal.of_real r + nnreal.of_real p := nnreal.eq $ by simp [nnreal.of_real, hr, hp, add_nonneg] lemma of_real_add_of_real {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real r + nnreal.of_real p = nnreal.of_real (r + p) := (of_real_add hr hp).symm lemma of_real_le_of_real {r p : ℝ} (h : r ≤ p) : nnreal.of_real r ≤ nnreal.of_real p := nnreal.of_real_mono h lemma of_real_add_le {r p : ℝ} : nnreal.of_real (r + p) ≤ nnreal.of_real r + nnreal.of_real p := nnreal.coe_le_coe.1 $ max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) nnreal.zero_le_coe lemma of_real_le_iff_le_coe {r : ℝ} {p : ℝ≥0} : nnreal.of_real r ≤ p ↔ r ≤ ↑p := nnreal.gi.gc r p lemma le_of_real_iff_coe_le {r : ℝ≥0} {p : ℝ} (hp : 0 ≤ p) : r ≤ nnreal.of_real p ↔ ↑r ≤ p := by rw [← nnreal.coe_le_coe, nnreal.coe_of_real p hp] lemma le_of_real_iff_coe_le' {r : ℝ≥0} {p : ℝ} (hr : 0 < r) : r ≤ nnreal.of_real p ↔ ↑r ≤ p := (le_or_lt 0 p).elim le_of_real_iff_coe_le $ λ hp, by simp only [(hp.trans_le r.coe_nonneg).not_le, of_real_eq_zero.2 hp.le, hr.not_le] lemma of_real_lt_iff_lt_coe {r : ℝ} {p : ℝ≥0} (ha : 0 ≤ r) : nnreal.of_real r < p ↔ r < ↑p := by rw [← nnreal.coe_lt_coe, nnreal.coe_of_real r ha] lemma lt_of_real_iff_coe_lt {r : ℝ≥0} {p : ℝ} : r < nnreal.of_real p ↔ ↑r < p := begin cases le_total 0 p, { rw [← nnreal.coe_lt_coe, nnreal.coe_of_real p h] }, { rw [of_real_eq_zero.2 h], split, intro, have := not_lt_of_le (zero_le r), contradiction, intro rp, have : ¬(p ≤ 0) := not_le_of_lt (lt_of_le_of_lt (coe_nonneg _) rp), contradiction } end @[simp] lemma of_real_bit0 {r : ℝ} (hr : 0 ≤ r) : nnreal.of_real (bit0 r) = bit0 (nnreal.of_real r) := of_real_add hr hr @[simp] lemma of_real_bit1 {r : ℝ} (hr : 0 ≤ r) : nnreal.of_real (bit1 r) = bit1 (nnreal.of_real r) := (of_real_add (by simp [hr]) zero_le_one).trans (by simp [of_real_one, bit1, hr]) end of_real section mul lemma mul_eq_mul_left {a b c : ℝ≥0} (h : a ≠ 0) : (a * b = a * c ↔ b = c) := begin rw [← nnreal.eq_iff, ← nnreal.eq_iff, nnreal.coe_mul, nnreal.coe_mul], split, { exact mul_left_cancel' (mt (@nnreal.eq_iff a 0).1 h) }, { assume h, rw [h] } end lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) : nnreal.of_real (p * q) = nnreal.of_real p * nnreal.of_real q := begin cases le_total 0 q with hq hq, { apply nnreal.eq, simp [nnreal.of_real, hp, hq, max_eq_left, mul_nonneg] }, { have hpq := mul_nonpos_of_nonneg_of_nonpos hp hq, rw [of_real_eq_zero.2 hq, of_real_eq_zero.2 hpq, mul_zero] } end end mul section sub lemma sub_def {r p : ℝ≥0} : r - p = nnreal.of_real (r - p) := rfl lemma sub_eq_zero {r p : ℝ≥0} (h : r ≤ p) : r - p = 0 := nnreal.eq $ max_eq_right $ sub_le_iff_le_add.2 $ by simpa [nnreal.coe_le_coe] using h @[simp] lemma sub_self {r : ℝ≥0} : r - r = 0 := sub_eq_zero $ le_refl r @[simp] lemma sub_zero {r : ℝ≥0} : r - 0 = r := by rw [sub_def, nnreal.coe_zero, sub_zero, nnreal.of_real_coe] lemma sub_pos {r p : ℝ≥0} : 0 < r - p ↔ p < r := of_real_pos.trans $ sub_pos.trans $ nnreal.coe_lt_coe protected lemma sub_lt_self {r p : ℝ≥0} : 0 < r → 0 < p → r - p < r := assume hr hp, begin cases le_total r p, { rwa [sub_eq_zero h] }, { rw [← nnreal.coe_lt_coe, nnreal.coe_sub h], exact sub_lt_self _ hp } end @[simp] lemma sub_le_iff_le_add {r p q : ℝ≥0} : r - p ≤ q ↔ r ≤ q + p := match le_total p r with | or.inl h := by rw [← nnreal.coe_le_coe, ← nnreal.coe_le_coe, nnreal.coe_sub h, nnreal.coe_add, sub_le_iff_le_add] | or.inr h := have r ≤ p + q, from le_add_right h, by simpa [nnreal.coe_le_coe, nnreal.coe_le_coe, sub_eq_zero h, add_comm] end @[simp] lemma sub_le_self {r p : ℝ≥0} : r - p ≤ r := sub_le_iff_le_add.2 $ le_add_right $ le_refl r lemma add_sub_cancel {r p : ℝ≥0} : (p + r) - r = p := nnreal.eq $ by rw [nnreal.coe_sub, nnreal.coe_add, add_sub_cancel]; exact le_add_left (le_refl _) lemma add_sub_cancel' {r p : ℝ≥0} : (r + p) - r = p := by rw [add_comm, add_sub_cancel] lemma sub_add_eq_max {r p : ℝ≥0} : (r - p) + p = max r p := nnreal.eq $ by rw [sub_def, nnreal.coe_add, coe_max, nnreal.of_real, coe_mk, ← max_add_add_right, zero_add, sub_add_cancel] lemma add_sub_eq_max {r p : ℝ≥0} : p + (r - p) = max p r := by rw [add_comm, sub_add_eq_max, max_comm] @[simp] lemma sub_add_cancel_of_le {a b : ℝ≥0} (h : b ≤ a) : (a - b) + b = a := by rw [sub_add_eq_max, max_eq_left h] lemma sub_sub_cancel_of_le {r p : ℝ≥0} (h : r ≤ p) : p - (p - r) = r := by rw [nnreal.sub_def, nnreal.sub_def, nnreal.coe_of_real _ $ sub_nonneg.2 h, sub_sub_cancel, nnreal.of_real_coe] lemma lt_sub_iff_add_lt {p q r : ℝ≥0} : p < q - r ↔ p + r < q := begin split, { assume H, have : (((q - r) : ℝ≥0) : ℝ) = (q : ℝ) - (r : ℝ) := nnreal.coe_sub (le_of_lt (sub_pos.1 (lt_of_le_of_lt (zero_le _) H))), rwa [← nnreal.coe_lt_coe, this, lt_sub_iff_add_lt, ← nnreal.coe_add] at H }, { assume H, have : r ≤ q := le_trans (le_add_left (le_refl _)) (le_of_lt H), rwa [← nnreal.coe_lt_coe, nnreal.coe_sub this, lt_sub_iff_add_lt, ← nnreal.coe_add] } end lemma sub_lt_iff_lt_add {a b c : ℝ≥0} (h : b ≤ a) : a - b < c ↔ a < b + c := by simp only [←nnreal.coe_lt_coe, nnreal.coe_sub h, nnreal.coe_add, sub_lt_iff_lt_add'] lemma sub_eq_iff_eq_add {a b c : ℝ≥0} (h : b ≤ a) : a - b = c ↔ a = c + b := by rw [←nnreal.eq_iff, nnreal.coe_sub h, ←nnreal.eq_iff, nnreal.coe_add, sub_eq_iff_eq_add] end sub section inv lemma sum_div {ι} (s : finset ι) (f : ι → ℝ≥0) (b : ℝ≥0) : (∑ i in s, f i) / b = ∑ i in s, (f i / b) := by simp only [div_eq_mul_inv, finset.sum_mul] @[simp] lemma inv_pos {r : ℝ≥0} : 0 < r⁻¹ ↔ 0 < r := by simp [pos_iff_ne_zero] lemma div_pos {r p : ℝ≥0} (hr : 0 < r) (hp : 0 < p) : 0 < r / p := by simpa only [div_eq_mul_inv] using mul_pos hr (inv_pos.2 hp) protected lemma mul_inv {r p : ℝ≥0} : (r * p)⁻¹ = p⁻¹ * r⁻¹ := nnreal.eq $ mul_inv_rev' _ _ lemma div_self_le (r : ℝ≥0) : r / r ≤ 1 := if h : r = 0 then by simp [h] else by rw [div_self h] @[simp] lemma inv_le {r p : ℝ≥0} (h : r ≠ 0) : r⁻¹ ≤ p ↔ 1 ≤ r * p := by rw [← mul_le_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h] lemma inv_le_of_le_mul {r p : ℝ≥0} (h : 1 ≤ r * p) : r⁻¹ ≤ p := by by_cases r = 0; simp [*, inv_le] @[simp] lemma le_inv_iff_mul_le {r p : ℝ≥0} (h : p ≠ 0) : (r ≤ p⁻¹ ↔ r * p ≤ 1) := by rw [← mul_le_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] @[simp] lemma lt_inv_iff_mul_lt {r p : ℝ≥0} (h : p ≠ 0) : (r < p⁻¹ ↔ r * p < 1) := by rw [← mul_lt_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] lemma mul_le_iff_le_inv {a b r : ℝ≥0} (hr : r ≠ 0) : r * a ≤ b ↔ a ≤ r⁻¹ * b := have 0 < r, from lt_of_le_of_ne (zero_le r) hr.symm, by rw [← @mul_le_mul_left _ _ a _ r this, ← mul_assoc, mul_inv_cancel hr, one_mul] lemma le_div_iff_mul_le {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ a * r ≤ b := by rw [div_eq_inv_mul, ← mul_le_iff_le_inv hr, mul_comm] lemma div_le_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a / r ≤ b ↔ a ≤ b * r := @div_le_iff ℝ _ a r b $ pos_iff_ne_zero.2 hr lemma lt_div_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a < b / r ↔ a * r < b := lt_iff_lt_of_le_iff_le (div_le_iff hr) lemma mul_lt_of_lt_div {a b r : ℝ≥0} (h : a < b / r) : a * r < b := begin refine (lt_div_iff $ λ hr, false.elim _).1 h, subst r, simpa using h end lemma le_of_forall_lt_one_mul_le {x y : ℝ≥0} (h : ∀a<1, a * x ≤ y) : x ≤ y := le_of_forall_ge_of_dense $ assume a ha, have hx : x ≠ 0 := pos_iff_ne_zero.1 (lt_of_le_of_lt (zero_le _) ha), have hx' : x⁻¹ ≠ 0, by rwa [(≠), inv_eq_zero], have a * x⁻¹ < 1, by rwa [← lt_inv_iff_mul_lt hx', inv_inv'], have (a * x⁻¹) * x ≤ y, from h _ this, by rwa [mul_assoc, inv_mul_cancel hx, mul_one] at this lemma div_add_div_same (a b c : ℝ≥0) : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma half_pos {a : ℝ≥0} (h : 0 < a) : 0 < a / 2 := div_pos h zero_lt_two lemma add_halves (a : ℝ≥0) : a / 2 + a / 2 = a := nnreal.eq (add_halves a) lemma half_lt_self {a : ℝ≥0} (h : a ≠ 0) : a / 2 < a := by rw [← nnreal.coe_lt_coe, nnreal.coe_div]; exact half_lt_self (bot_lt_iff_ne_bot.2 h) lemma two_inv_lt_one : (2⁻¹:ℝ≥0) < 1 := by simpa using half_lt_self zero_ne_one.symm lemma div_lt_iff {a b c : ℝ≥0} (hc : c ≠ 0) : b / c < a ↔ b < a * c := lt_iff_lt_of_le_iff_le $ nnreal.le_div_iff_mul_le hc lemma div_lt_one_of_lt {a b : ℝ≥0} (h : a < b) : a / b < 1 := begin rwa [div_lt_iff, one_mul], exact ne_of_gt (lt_of_le_of_lt (zero_le _) h) end @[field_simps] lemma div_add_div (a : ℝ≥0) {b : ℝ≥0} (c : ℝ≥0) {d : ℝ≥0} (hb : b ≠ 0) (hd : d ≠ 0) : a / b + c / d = (a * d + b * c) / (b * d) := begin rw ← nnreal.eq_iff, simp only [nnreal.coe_add, nnreal.coe_div, nnreal.coe_mul], exact div_add_div _ _ (coe_ne_zero.2 hb) (coe_ne_zero.2 hd) end @[field_simps] lemma add_div' (a b c : ℝ≥0) (hc : c ≠ 0) : b + a / c = (b * c + a) / c := by simpa using div_add_div b a one_ne_zero hc @[field_simps] lemma div_add' (a b c : ℝ≥0) (hc : c ≠ 0) : a / c + b = (a + b * c) / c := by rwa [add_comm, add_div', add_comm] lemma of_real_inv {x : ℝ} : nnreal.of_real x⁻¹ = (nnreal.of_real x)⁻¹ := begin by_cases hx : 0 ≤ x, { nth_rewrite 0 ← coe_of_real x hx, rw [←nnreal.coe_inv, of_real_coe], }, { have hx' := le_of_not_ge hx, rw [of_real_eq_zero.mpr hx', inv_zero, of_real_eq_zero.mpr (inv_nonpos.mpr hx')], }, end lemma of_real_div {x y : ℝ} (hx : 0 ≤ x) : nnreal.of_real (x / y) = nnreal.of_real x / nnreal.of_real y := by rw [div_eq_mul_inv, div_eq_mul_inv, ←of_real_inv, ←of_real_mul hx] lemma of_real_div' {x y : ℝ} (hy : 0 ≤ y) : nnreal.of_real (x / y) = nnreal.of_real x / nnreal.of_real y := by rw [div_eq_inv_mul, div_eq_inv_mul, of_real_mul (inv_nonneg.2 hy), of_real_inv] end inv @[simp] lemma abs_eq (x : ℝ≥0) : abs (x : ℝ) = x := abs_of_nonneg x.property end nnreal /-- The absolute value on `ℝ` as a map to `ℝ≥0`. -/ @[pp_nodot] def real.nnabs (x : ℝ) : ℝ≥0 := ⟨abs x, abs_nonneg x⟩ @[norm_cast, simp] lemma nnreal.coe_nnabs (x : ℝ) : (real.nnabs x : ℝ) = abs x := by simp [real.nnabs]
c2531df0f55218af64af1cc2833e341084f1ccfa
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/anc1.lean
8cb093a89629ff098a9295de0b556a2e3dd2e58f
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
1,316
lean
#check (⟨1, 2⟩ : nat × nat) #check (⟨trivial, trivial⟩ : true ∧ true) example : true := sorry #check (⟨1, sorry⟩ : Σ' x : nat, x > 0) open tactic #check show true, from ⟨⟩ #check (⟨1, by intro1 >> contradiction⟩ : ∃ x : nat, 1 ≠ 0) universe variables u v #check λ (A B C : Prop), assume (Ha : A) (Hb : B) (Hc : C), show B ∧ A, from ⟨Hb, Ha⟩ #check λ (A B C : Prop), assume (Ha : A) (Hb : B) (Hc : C), show B ∧ A ∧ C ∧ A, from ⟨Hb, ⟨Ha, ⟨Hc, Ha⟩⟩⟩ #check λ (A B C : Prop), assume (Ha : A) (Hb : B) (Hc : C), show B ∧ A ∧ C ∧ A, from ⟨Hb, Ha, Hc, Ha⟩ #check λ (A B C : Prop), assume (Ha : A) (Hb : B) (Hc : C), show ((B ∧ true) ∧ A) ∧ (C ∧ A), from ⟨⟨⟨Hb, ⟨⟩⟩, Ha⟩, ⟨Hc, Ha⟩⟩ #check λ (A : Type u) (P : A → Prop) (Q : A → Prop), assume (a : A), assume (H1 : P a) (H2 : Q a), show ∃ x, P x ∧ Q x, from ⟨a, ⟨H1, H2⟩⟩ #check λ (A : Type u) (P : A → Prop) (Q : A → Prop), assume (a : A) (b : A), assume (H1 : P a) (H2 : Q b), show ∃ x y, P x ∧ Q y, from ⟨a, ⟨b, ⟨H1, H2⟩⟩⟩ #check λ (A : Type u) (P : A → Prop) (Q : A → Prop), assume (a : A) (b : A), assume (H1 : P a) (H2 : Q b), show ∃ x y, P x ∧ Q y, from ⟨a, b, H1, H2⟩
00bae7fcdcfc83b7c07b01868c75b06b8083675b
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/group_theory/free_abelian_group_finsupp.lean
10e05a6d2520bb311fa716da6e0db97e3e48c1c7
[ "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
7,142
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.hom.equiv.type_tags import algebra.module.equiv import data.finsupp.defs import group_theory.free_abelian_group import group_theory.is_free_group import linear_algebra.dimension /-! # Isomorphism between `free_abelian_group X` and `X →₀ ℤ` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we construct the canonical isomorphism between `free_abelian_group X` and `X →₀ ℤ`. We use this to transport the notion of `support` from `finsupp` to `free_abelian_group`. ## Main declarations - `free_abelian_group.equiv_finsupp`: group isomorphism between `free_abelian_group X` and `X →₀ ℤ` - `free_abelian_group.coeff`: the multiplicity of `x : X` in `a : free_abelian_group X` - `free_abelian_group.support`: the finset of `x : X` that occur in `a : free_abelian_group X` -/ noncomputable theory open_locale big_operators variables {X : Type*} /-- The group homomorphism `free_abelian_group X →+ (X →₀ ℤ)`. -/ def free_abelian_group.to_finsupp : free_abelian_group X →+ (X →₀ ℤ) := free_abelian_group.lift $ λ x, finsupp.single x (1 : ℤ) /-- The group homomorphism `(X →₀ ℤ) →+ free_abelian_group X`. -/ def finsupp.to_free_abelian_group : (X →₀ ℤ) →+ free_abelian_group X := finsupp.lift_add_hom $ λ x, (smul_add_hom ℤ (free_abelian_group X)).flip (free_abelian_group.of x) open finsupp free_abelian_group @[simp] lemma finsupp.to_free_abelian_group_comp_single_add_hom (x : X) : finsupp.to_free_abelian_group.comp (finsupp.single_add_hom x) = (smul_add_hom ℤ (free_abelian_group X)).flip (of x) := begin ext, simp only [add_monoid_hom.coe_comp, finsupp.single_add_hom_apply, function.comp_app, one_smul, to_free_abelian_group, finsupp.lift_add_hom_apply_single] end @[simp] lemma free_abelian_group.to_finsupp_comp_to_free_abelian_group : to_finsupp.comp to_free_abelian_group = add_monoid_hom.id (X →₀ ℤ) := begin ext x y, simp only [add_monoid_hom.id_comp], rw [add_monoid_hom.comp_assoc, finsupp.to_free_abelian_group_comp_single_add_hom], simp only [to_finsupp, add_monoid_hom.coe_comp, finsupp.single_add_hom_apply, function.comp_app, one_smul, lift.of, add_monoid_hom.flip_apply, smul_add_hom_apply, add_monoid_hom.id_apply], end @[simp] lemma finsupp.to_free_abelian_group_comp_to_finsupp : to_free_abelian_group.comp to_finsupp = add_monoid_hom.id (free_abelian_group X) := begin ext, rw [to_free_abelian_group, to_finsupp, add_monoid_hom.comp_apply, lift.of, lift_add_hom_apply_single, add_monoid_hom.flip_apply, smul_add_hom_apply, one_smul, add_monoid_hom.id_apply], end @[simp] lemma finsupp.to_free_abelian_group_to_finsupp {X} (x : free_abelian_group X) : x.to_finsupp.to_free_abelian_group = x := by rw [← add_monoid_hom.comp_apply, finsupp.to_free_abelian_group_comp_to_finsupp, add_monoid_hom.id_apply] namespace free_abelian_group open finsupp variable {X} @[simp] lemma to_finsupp_of (x : X) : to_finsupp (of x) = finsupp.single x 1 := by simp only [to_finsupp, lift.of] @[simp] lemma to_finsupp_to_free_abelian_group (f : X →₀ ℤ) : f.to_free_abelian_group.to_finsupp = f := by rw [← add_monoid_hom.comp_apply, to_finsupp_comp_to_free_abelian_group, add_monoid_hom.id_apply] variable (X) /-- The additive equivalence between `free_abelian_group X` and `(X →₀ ℤ)`. -/ @[simps] def equiv_finsupp : free_abelian_group X ≃+ (X →₀ ℤ) := { to_fun := to_finsupp, inv_fun := to_free_abelian_group, left_inv := to_free_abelian_group_to_finsupp, right_inv := to_finsupp_to_free_abelian_group, map_add' := to_finsupp.map_add } /-- `A` is a basis of the ℤ-module `free_abelian_group A`. -/ noncomputable def basis (α : Type*) : basis α ℤ (free_abelian_group α) := ⟨(free_abelian_group.equiv_finsupp α).to_int_linear_equiv ⟩ /-- Isomorphic free ablian groups (as modules) have equivalent bases. -/ def equiv.of_free_abelian_group_linear_equiv {α β : Type*} (e : free_abelian_group α ≃ₗ[ℤ] free_abelian_group β) : α ≃ β := let t : _root_.basis α ℤ (free_abelian_group β) := (free_abelian_group.basis α).map e in t.index_equiv $ free_abelian_group.basis _ /-- Isomorphic free abelian groups (as additive groups) have equivalent bases. -/ def equiv.of_free_abelian_group_equiv {α β : Type*} (e : free_abelian_group α ≃+ free_abelian_group β) : α ≃ β := equiv.of_free_abelian_group_linear_equiv e.to_int_linear_equiv /-- Isomorphic free groups have equivalent bases. -/ def equiv.of_free_group_equiv {α β : Type*} (e : free_group α ≃* free_group β) : α ≃ β := equiv.of_free_abelian_group_equiv e.abelianization_congr.to_additive open is_free_group /-- Isomorphic free groups have equivalent bases (`is_free_group` variant`). -/ def equiv.of_is_free_group_equiv {G H : Type*} [group G] [group H] [is_free_group G] [is_free_group H] (e : G ≃* H) : generators G ≃ generators H := equiv.of_free_group_equiv $ mul_equiv.trans ((to_free_group G).symm) $ mul_equiv.trans e $ to_free_group H variable {X} /-- `coeff x` is the additive group homomorphism `free_abelian_group X →+ ℤ` that sends `a` to the multiplicity of `x : X` in `a`. -/ def coeff (x : X) : free_abelian_group X →+ ℤ := (finsupp.apply_add_hom x).comp to_finsupp /-- `support a` for `a : free_abelian_group X` is the finite set of `x : X` that occur in the formal sum `a`. -/ def support (a : free_abelian_group X) : finset X := a.to_finsupp.support lemma mem_support_iff (x : X) (a : free_abelian_group X) : x ∈ a.support ↔ coeff x a ≠ 0 := by { rw [support, finsupp.mem_support_iff], exact iff.rfl } lemma not_mem_support_iff (x : X) (a : free_abelian_group X) : x ∉ a.support ↔ coeff x a = 0 := by { rw [support, finsupp.not_mem_support_iff], exact iff.rfl } @[simp] lemma support_zero : support (0 : free_abelian_group X) = ∅ := by simp only [support, finsupp.support_zero, add_monoid_hom.map_zero] @[simp] lemma support_of (x : X) : support (of x) = {x} := by simp only [support, to_finsupp_of, finsupp.support_single_ne_zero _ one_ne_zero] @[simp] lemma support_neg (a : free_abelian_group X) : support (-a) = support a := by simp only [support, add_monoid_hom.map_neg, finsupp.support_neg] @[simp] lemma support_zsmul (k : ℤ) (h : k ≠ 0) (a : free_abelian_group X) : support (k • a) = support a := begin ext x, simp only [mem_support_iff, add_monoid_hom.map_zsmul], simp only [h, zsmul_int_int, false_or, ne.def, mul_eq_zero] end @[simp] lemma support_nsmul (k : ℕ) (h : k ≠ 0) (a : free_abelian_group X) : support (k • a) = support a := by { apply support_zsmul k _ a, exact_mod_cast h } open_locale classical lemma support_add (a b : free_abelian_group X) : (support (a + b)) ⊆ a.support ∪ b.support := begin simp only [support, add_monoid_hom.map_add], apply finsupp.support_add end end free_abelian_group
c61761bb5526c01513255019a107cb4864bd5981
74caf7451c921a8d5ab9c6e2b828c9d0a35aae95
/library/init/meta/name.lean
c49054a16ce377cffdb57d2e9ca6bf2094172dbc
[ "Apache-2.0" ]
permissive
sakas--/lean
f37b6fad4fd4206f2891b89f0f8135f57921fc3f
570d9052820be1d6442a5cc58ece37397f8a9e4c
refs/heads/master
1,586,127,145,194
1,480,960,018,000
1,480,960,635,000
40,137,176
0
0
null
1,438,621,351,000
1,438,621,351,000
null
UTF-8
Lean
false
false
2,038
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.data.ordering init.coe /- Reflect a C++ name object. The VM replaces it with the C++ implementation. -/ inductive name | anonymous : name | mk_string : string → name → name | mk_numeral : unsigned → name → name instance : inhabited name := ⟨name.anonymous⟩ def mk_str_name (n : name) (s : string) : name := name.mk_string s n def mk_num_name (n : name) (v : nat) : name := name.mk_numeral (unsigned.of_nat v) n def mk_simple_name (s : string) : name := mk_str_name name.anonymous s instance string_to_name : has_coe string name := ⟨mk_simple_name⟩ infix ` <.> `:65 := mk_str_name open name def name.get_prefix : name → name | anonymous := anonymous | (mk_string s p) := p | (mk_numeral s p) := p def name.to_string : name → string | anonymous := "[anonymous]" | (mk_string s anonymous) := s | (mk_numeral v anonymous) := to_string v | (mk_string s n) := name.to_string n ++ "." ++ s | (mk_numeral v n) := name.to_string n ++ "." ++ to_string v instance : has_to_string name := ⟨name.to_string⟩ /- TODO(Leo): provide a definition in Lean. -/ meta constant name.has_decidable_eq : decidable_eq name /- Both cmp and lex_cmp are total orders, but lex_cmp implements a lexicographical order. -/ meta constant name.cmp : name → name → ordering meta constant name.lex_cmp : name → name → ordering meta constant name.append : name → name → name attribute [instance] name.has_decidable_eq meta instance : has_ordering name := ⟨name.cmp⟩ meta instance : has_append name := ⟨name.append⟩ /- (name.append_after n i) return a name of the form n_i -/ meta constant name.append_after : name → nat → name meta def name.is_prefix_of : name → name → bool | p name.anonymous := ff | p n := if p = n then tt else name.is_prefix_of p n^.get_prefix
e3b4da3c3815b2280395aeb485e6f53a7aa56fa9
8eeb99d0fdf8125f5d39a0ce8631653f588ee817
/src/data/equiv/basic.lean
653fc72500de6e369c61a1a88ff86a469d04f68a
[ "Apache-2.0" ]
permissive
jesse-michael-han/mathlib
a15c58378846011b003669354cbab7062b893cfe
fa6312e4dc971985e6b7708d99a5bc3062485c89
refs/heads/master
1,625,200,760,912
1,602,081,753,000
1,602,081,753,000
181,787,230
0
0
null
1,555,460,682,000
1,555,460,682,000
null
UTF-8
Lean
false
false
76,399
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ import data.set.function import algebra.group.basic /-! # Equivalence between types In this file we define two types: * `equiv α β` a.k.a. `α ≃ β`: a bijective map `α → β` bundled with its inverse map; we use this (and not equality!) to express that various `Type`s or `Sort`s are equivalent. * `equiv.perm α`: the group of permutations `α ≃ α`. Then we define * canonical isomorphisms between various types: e.g., - `equiv.refl α` is the identity map interpreted as `α ≃ α`; - `equiv.sum_equiv_sigma_bool` is the canonical equivalence between the sum of two types `α ⊕ β` and the sigma-type `Σ b : bool, cond b α β`; - `equiv.prod_sum_distrib : α × (β ⊕ γ) ≃ (α × β) ⊕ (α × γ)` shows that type product and type sum satisfy the distributive law up to a canonical equivalence; * operations on equivalences: e.g., - `equiv.symm e : β ≃ α` is the inverse of `e : α ≃ β`; - `equiv.trans e₁ e₂ : α ≃ γ` is the composition of `e₁ : α ≃ β` and `e₂ : β ≃ γ` (note the order of the arguments!); - `equiv.prod_congr ea eb : α₁ × β₁ ≃ α₂ × β₂`: combine two equivalences `ea : α₁ ≃ α₂` and `eb : β₁ ≃ β₂` using `prod.map`. * definitions that transfer some instances along an equivalence. By convention, we transfer instances from right to left. - `equiv.inhabited` takes `e : α ≃ β` and `[inhabited β]` and returns `inhabited α`; - `equiv.unique` takes `e : α ≃ β` and `[unique β]` and returns `unique α`; - `equiv.decidable_eq` takes `e : α ≃ β` and `[decidable_eq β]` and returns `decidable_eq α`. More definitions of this kind can be found in other files. E.g., `data/equiv/transfer_instance` does it for many algebraic type classes like `group`, `module`, etc. * group structure on `equiv.perm α`. More lemmas about `equiv.perm` can be found in `data/equiv/perm`. ## Tags equivalence, congruence, bijective map -/ open function universes u v w z variables {α : Sort u} {β : Sort v} {γ : Sort w} /-- `α ≃ β` is the type of functions from `α → β` with a two-sided inverse. -/ @[nolint inhabited] structure equiv (α : Sort*) (β : Sort*) := (to_fun : α → β) (inv_fun : β → α) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) infix ` ≃ `:25 := equiv /-- Convert an involutive function `f` to an equivalence with `to_fun = inv_fun = f`. -/ def function.involutive.to_equiv (f : α → α) (h : involutive f) : α ≃ α := ⟨f, f, h.left_inverse, h.right_inverse⟩ namespace equiv /-- `perm α` is the type of bijections from `α` to itself. -/ @[reducible] def perm (α : Sort*) := equiv α α instance : has_coe_to_fun (α ≃ β) := ⟨_, to_fun⟩ @[simp] theorem coe_fn_mk (f : α → β) (g l r) : (equiv.mk f g l r : α → β) = f := rfl /-- The map `coe_fn : (r ≃ s) → (r → s)` is injective. We can't use `function.injective` here but mimic its signature by using `⦃e₁ e₂⦄`. -/ theorem coe_fn_injective : ∀ ⦃e₁ e₂ : equiv α β⦄, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨f₁, g₁, l₁, r₁⟩ ⟨f₂, g₂, l₂, r₂⟩ h := have f₁ = f₂, from h, have g₁ = g₂, from l₁.eq_right_inverse (this.symm ▸ r₂), by simp * @[ext] lemma ext {f g : equiv α β} (H : ∀ x, f x = g x) : f = g := coe_fn_injective (funext H) @[ext] lemma perm.ext {σ τ : equiv.perm α} (H : ∀ x, σ x = τ x) : σ = τ := equiv.ext H lemma ext_iff {f g : equiv α β} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, ext⟩ lemma perm.ext_iff {σ τ : equiv.perm α} : σ = τ ↔ ∀ x, σ x = τ x := ext_iff /-- Any type is equivalent to itself. -/ @[refl] protected def refl (α : Sort*) : α ≃ α := ⟨id, id, λ x, rfl, λ x, rfl⟩ instance inhabited' : inhabited (α ≃ α) := ⟨equiv.refl α⟩ /-- Inverse of an equivalence `e : α ≃ β`. -/ @[symm] protected def symm (e : α ≃ β) : β ≃ α := ⟨e.inv_fun, e.to_fun, e.right_inv, e.left_inv⟩ /-- Composition of equivalences `e₁ : α ≃ β` and `e₂ : β ≃ γ`. -/ @[trans] protected def trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ := ⟨e₂.to_fun ∘ e₁.to_fun, e₁.inv_fun ∘ e₂.inv_fun, e₂.left_inv.comp e₁.left_inv, e₂.right_inv.comp e₁.right_inv⟩ @[simp] lemma to_fun_as_coe (e : α ≃ β) : e.to_fun = e := rfl @[simp] lemma inv_fun_as_coe (e : α ≃ β) : e.inv_fun = e.symm := rfl protected theorem injective (e : α ≃ β) : injective e := e.left_inv.injective protected theorem surjective (e : α ≃ β) : surjective e := e.right_inv.surjective protected theorem bijective (f : α ≃ β) : bijective f := ⟨f.injective, f.surjective⟩ @[simp] lemma range_eq_univ {α : Type*} {β : Type*} (e : α ≃ β) : set.range e = set.univ := set.eq_univ_of_forall e.surjective protected theorem subsingleton (e : α ≃ β) [subsingleton β] : subsingleton α := e.injective.comap_subsingleton /-- Transfer `decidable_eq` across an equivalence. -/ protected def decidable_eq (e : α ≃ β) [decidable_eq β] : decidable_eq α := e.injective.decidable_eq lemma nonempty_iff_nonempty (e : α ≃ β) : nonempty α ↔ nonempty β := nonempty.congr e e.symm /-- If `α ≃ β` and `β` is inhabited, then so is `α`. -/ protected def inhabited [inhabited β] (e : α ≃ β) : inhabited α := ⟨e.symm (default _)⟩ /-- If `α ≃ β` and `β` is a singleton type, then so is `α`. -/ protected def unique [unique β] (e : α ≃ β) : unique α := e.symm.surjective.unique /-- Equivalence between equal types. -/ protected def cast {α β : Sort*} (h : α = β) : α ≃ β := ⟨cast h, cast h.symm, λ x, by { cases h, refl }, λ x, by { cases h, refl }⟩ @[simp] theorem coe_fn_symm_mk (f : α → β) (g l r) : ((equiv.mk f g l r).symm : β → α) = g := rfl @[simp] theorem coe_refl : ⇑(equiv.refl α) = id := rfl theorem refl_apply (x : α) : equiv.refl α x = x := rfl @[simp] theorem coe_trans (f : α ≃ β) (g : β ≃ γ) : ⇑(f.trans g) = g ∘ f := rfl theorem trans_apply (f : α ≃ β) (g : β ≃ γ) (a : α) : (f.trans g) a = g (f a) := rfl @[simp] theorem apply_symm_apply (e : α ≃ β) (x : β) : e (e.symm x) = x := e.right_inv x @[simp] theorem symm_apply_apply (e : α ≃ β) (x : α) : e.symm (e x) = x := e.left_inv x @[simp] theorem symm_comp_self (e : α ≃ β) : e.symm ∘ e = id := funext e.symm_apply_apply @[simp] theorem self_comp_symm (e : α ≃ β) : e ∘ e.symm = id := funext e.apply_symm_apply @[simp] lemma symm_trans_apply (f : α ≃ β) (g : β ≃ γ) (a : γ) : (f.trans g).symm a = f.symm (g.symm a) := rfl @[simp] theorem apply_eq_iff_eq (f : α ≃ β) {x y : α} : f x = f y ↔ x = y := f.injective.eq_iff theorem apply_eq_iff_eq_symm_apply {α β : Sort*} (f : α ≃ β) {x : α} {y : β} : f x = y ↔ x = f.symm y := begin conv_lhs { rw ←apply_symm_apply f y, }, rw apply_eq_iff_eq, end @[simp] theorem cast_apply {α β} (h : α = β) (x : α) : equiv.cast h x = cast h x := rfl lemma symm_apply_eq {α β} (e : α ≃ β) {x y} : e.symm x = y ↔ x = e y := ⟨λ H, by simp [H.symm], λ H, by simp [H]⟩ lemma eq_symm_apply {α β} (e : α ≃ β) {x y} : y = e.symm x ↔ e y = x := (eq_comm.trans e.symm_apply_eq).trans eq_comm @[simp] theorem symm_symm (e : α ≃ β) : e.symm.symm = e := by { cases e, refl } @[simp] theorem trans_refl (e : α ≃ β) : e.trans (equiv.refl β) = e := by { cases e, refl } @[simp] theorem refl_symm : (equiv.refl α).symm = equiv.refl α := rfl @[simp] theorem refl_trans (e : α ≃ β) : (equiv.refl α).trans e = e := by { cases e, refl } @[simp] theorem symm_trans (e : α ≃ β) : e.symm.trans e = equiv.refl β := ext (by simp) @[simp] theorem trans_symm (e : α ≃ β) : e.trans e.symm = equiv.refl α := ext (by simp) lemma trans_assoc {δ} (ab : α ≃ β) (bc : β ≃ γ) (cd : γ ≃ δ) : (ab.trans bc).trans cd = ab.trans (bc.trans cd) := equiv.ext $ assume a, rfl theorem left_inverse_symm (f : equiv α β) : left_inverse f.symm f := f.left_inv theorem right_inverse_symm (f : equiv α β) : function.right_inverse f.symm f := f.right_inv /-- If `α` is equivalent to `β` and `γ` is equivalent to `δ`, then the type of equivalences `α ≃ γ` is equivalent to the type of equivalences `β ≃ δ`. -/ def equiv_congr {δ} (ab : α ≃ β) (cd : γ ≃ δ) : (α ≃ γ) ≃ (β ≃ δ) := ⟨ λac, (ab.symm.trans ac).trans cd, λbd, ab.trans $ bd.trans $ cd.symm, assume ac, by { ext x, simp }, assume ac, by { ext x, simp } ⟩ /-- If `α` is equivalent to `β`, then `perm α` is equivalent to `perm β`. -/ def perm_congr {α : Type*} {β : Type*} (e : α ≃ β) : perm α ≃ perm β := equiv_congr e e protected lemma image_eq_preimage {α β} (e : α ≃ β) (s : set α) : e '' s = e.symm ⁻¹' s := set.ext $ assume x, set.mem_image_iff_of_inverse e.left_inv e.right_inv protected lemma subset_image {α β} (e : α ≃ β) (s : set α) (t : set β) : t ⊆ e '' s ↔ e.symm '' t ⊆ s := by rw [set.image_subset_iff, e.image_eq_preimage] lemma symm_image_image {α β} (f : equiv α β) (s : set α) : f.symm '' (f '' s) = s := by { rw [← set.image_comp], simp } protected lemma image_compl {α β} (f : equiv α β) (s : set α) : f '' sᶜ = (f '' s)ᶜ := set.image_compl_eq f.bijective /-! ### The group of permutations (self-equivalences) of a type `α` -/ namespace perm instance perm_group {α : Type u} : group (perm α) := begin refine { mul := λ f g, equiv.trans g f, one := equiv.refl α, inv:= equiv.symm, ..}; intros; apply equiv.ext; try { apply trans_apply }, apply symm_apply_apply end @[simp] theorem mul_apply {α : Type u} (f g : perm α) (x) : (f * g) x = f (g x) := equiv.trans_apply _ _ _ @[simp] theorem one_apply {α : Type u} (x) : (1 : perm α) x = x := rfl @[simp] lemma inv_apply_self {α : Type u} (f : perm α) (x) : f⁻¹ (f x) = x := equiv.symm_apply_apply _ _ @[simp] lemma apply_inv_self {α : Type u} (f : perm α) (x) : f (f⁻¹ x) = x := equiv.apply_symm_apply _ _ lemma one_def {α : Type u} : (1 : perm α) = equiv.refl α := rfl lemma mul_def {α : Type u} (f g : perm α) : f * g = g.trans f := rfl lemma inv_def {α : Type u} (f : perm α) : f⁻¹ = f.symm := rfl end perm /-- If `α` is an empty type, then it is equivalent to the `empty` type. -/ def equiv_empty (h : α → false) : α ≃ empty := ⟨λ x, (h x).elim, λ e, e.rec _, λ x, (h x).elim, λ e, e.rec _⟩ /-- `false` is equivalent to `empty`. -/ def false_equiv_empty : false ≃ empty := equiv_empty _root_.id /-- If `α` is an empty type, then it is equivalent to the `pempty` type in any universe. -/ def {u' v'} equiv_pempty {α : Sort v'} (h : α → false) : α ≃ pempty.{u'} := ⟨λ x, (h x).elim, λ e, e.rec _, λ x, (h x).elim, λ e, e.rec _⟩ /-- `false` is equivalent to `pempty`. -/ def false_equiv_pempty : false ≃ pempty := equiv_pempty _root_.id /-- `empty` is equivalent to `pempty`. -/ def empty_equiv_pempty : empty ≃ pempty := equiv_pempty $ empty.rec _ /-- `pempty` types from any two universes are equivalent. -/ def pempty_equiv_pempty : pempty.{v} ≃ pempty.{w} := equiv_pempty pempty.elim /-- If `α` is not `nonempty`, then it is equivalent to `empty`. -/ def empty_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : α ≃ empty := equiv_empty $ assume a, h ⟨a⟩ /-- If `α` is not `nonempty`, then it is equivalent to `pempty`. -/ def pempty_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : α ≃ pempty := equiv_pempty $ assume a, h ⟨a⟩ /-- The `Sort` of proofs of a true proposition is equivalent to `punit`. -/ def prop_equiv_punit {p : Prop} (h : p) : p ≃ punit := ⟨λ x, (), λ x, h, λ _, rfl, λ ⟨⟩, rfl⟩ /-- `true` is equivalent to `punit`. -/ def true_equiv_punit : true ≃ punit := prop_equiv_punit trivial /-- `ulift α` is equivalent to `α`. -/ protected def ulift {α : Type v} : ulift.{u} α ≃ α := ⟨ulift.down, ulift.up, ulift.up_down, λ a, rfl⟩ @[simp] lemma coe_ulift {α : Type v} : ⇑(@equiv.ulift.{u} α) = ulift.down := rfl @[simp] lemma coe_ulift_symm {α : Type v} : ⇑(@equiv.ulift.{u} α).symm = ulift.up := rfl /-- `plift α` is equivalent to `α`. -/ protected def plift : plift α ≃ α := ⟨plift.down, plift.up, plift.up_down, plift.down_up⟩ @[simp] lemma coe_plift : ⇑(@equiv.plift α) = plift.down := rfl @[simp] lemma coe_plift_symm : ⇑(@equiv.plift α).symm = plift.up := rfl /-- equivalence of propositions is the same as iff -/ def of_iff {P Q : Prop} (h : P ↔ Q) : P ≃ Q := { to_fun := h.mp, inv_fun := h.mpr, left_inv := λ x, rfl, right_inv := λ y, rfl } /-- If `α₁` is equivalent to `α₂` and `β₁` is equivalent to `β₂`, then the type of maps `α₁ → β₁` is equivalent to the type of maps `α₂ → β₂`. -/ @[congr] def arrow_congr {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (α₁ → β₁) ≃ (α₂ → β₂) := { to_fun := λ f, e₂.to_fun ∘ f ∘ e₁.inv_fun, inv_fun := λ f, e₂.inv_fun ∘ f ∘ e₁.to_fun, left_inv := λ f, funext $ λ x, by simp, right_inv := λ f, funext $ λ x, by simp } @[simp] lemma arrow_congr_apply {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (f : α₁ → β₁) (x : α₂) : arrow_congr e₁ e₂ f x = (e₂ $ f $ e₁.symm x) := rfl lemma arrow_congr_comp {α₁ β₁ γ₁ α₂ β₂ γ₂ : Sort*} (ea : α₁ ≃ α₂) (eb : β₁ ≃ β₂) (ec : γ₁ ≃ γ₂) (f : α₁ → β₁) (g : β₁ → γ₁) : arrow_congr ea ec (g ∘ f) = (arrow_congr eb ec g) ∘ (arrow_congr ea eb f) := by { ext, simp only [comp, arrow_congr_apply, eb.symm_apply_apply] } @[simp] lemma arrow_congr_refl {α β : Sort*} : arrow_congr (equiv.refl α) (equiv.refl β) = equiv.refl (α → β) := rfl @[simp] lemma arrow_congr_trans {α₁ β₁ α₂ β₂ α₃ β₃ : Sort*} (e₁ : α₁ ≃ α₂) (e₁' : β₁ ≃ β₂) (e₂ : α₂ ≃ α₃) (e₂' : β₂ ≃ β₃) : arrow_congr (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr e₁ e₁').trans (arrow_congr e₂ e₂') := rfl @[simp] lemma arrow_congr_symm {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (arrow_congr e₁ e₂).symm = arrow_congr e₁.symm e₂.symm := rfl /-- A version of `equiv.arrow_congr` in `Type`, rather than `Sort`. The `equiv_rw` tactic is not able to use the default `Sort` level `equiv.arrow_congr`, because Lean's universe rules will not unify `?l_1` with `imax (1 ?m_1)`. -/ @[congr] def arrow_congr' {α₁ β₁ α₂ β₂ : Type*} (hα : α₁ ≃ α₂) (hβ : β₁ ≃ β₂) : (α₁ → β₁) ≃ (α₂ → β₂) := equiv.arrow_congr hα hβ @[simp] lemma arrow_congr'_apply {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (f : α₁ → β₁) (x : α₂) : arrow_congr' e₁ e₂ f x = (e₂ $ f $ e₁.symm x) := rfl @[simp] lemma arrow_congr'_refl {α β : Type*} : arrow_congr' (equiv.refl α) (equiv.refl β) = equiv.refl (α → β) := rfl @[simp] lemma arrow_congr'_trans {α₁ β₁ α₂ β₂ α₃ β₃ : Type*} (e₁ : α₁ ≃ α₂) (e₁' : β₁ ≃ β₂) (e₂ : α₂ ≃ α₃) (e₂' : β₂ ≃ β₃) : arrow_congr' (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr' e₁ e₁').trans (arrow_congr' e₂ e₂') := rfl @[simp] lemma arrow_congr'_symm {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (arrow_congr' e₁ e₂).symm = arrow_congr' e₁.symm e₂.symm := rfl /-- Conjugate a map `f : α → α` by an equivalence `α ≃ β`. -/ def conj (e : α ≃ β) : (α → α) ≃ (β → β) := arrow_congr e e @[simp] lemma conj_apply (e : α ≃ β) (f : α → α) (x : β) : e.conj f x = (e $ f $ e.symm x) := rfl @[simp] lemma conj_refl : conj (equiv.refl α) = equiv.refl (α → α) := rfl @[simp] lemma conj_symm (e : α ≃ β) : e.conj.symm = e.symm.conj := rfl @[simp] lemma conj_trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : (e₁.trans e₂).conj = e₁.conj.trans e₂.conj := rfl -- This should not be a simp lemma as long as `(∘)` is reducible: -- when `(∘)` is reducible, Lean can unify `f₁ ∘ f₂` with any `g` using -- `f₁ := g` and `f₂ := λ x, x`. This causes nontermination. lemma conj_comp (e : α ≃ β) (f₁ f₂ : α → α) : e.conj (f₁ ∘ f₂) = (e.conj f₁) ∘ (e.conj f₂) := by apply arrow_congr_comp /-- `punit` sorts in any two universes are equivalent. -/ def punit_equiv_punit : punit.{v} ≃ punit.{w} := ⟨λ _, punit.star, λ _, punit.star, λ u, by { cases u, refl }, λ u, by { cases u, reflexivity }⟩ section /-- The sort of maps to `punit.{v}` is equivalent to `punit.{w}`. -/ def arrow_punit_equiv_punit (α : Sort*) : (α → punit.{v}) ≃ punit.{w} := ⟨λ f, punit.star, λ u f, punit.star, λ f, by { funext x, cases f x, refl }, λ u, by { cases u, reflexivity }⟩ /-- The sort of maps from `punit` is equivalent to the codomain. -/ def punit_arrow_equiv (α : Sort*) : (punit.{u} → α) ≃ α := ⟨λ f, f punit.star, λ a u, a, λ f, by { ext ⟨⟩, refl }, λ u, rfl⟩ /-- The sort of maps from `empty` is equivalent to `punit`. -/ def empty_arrow_equiv_punit (α : Sort*) : (empty → α) ≃ punit.{u} := ⟨λ f, punit.star, λ u e, e.rec _, λ f, funext $ λ x, x.rec _, λ u, by { cases u, refl }⟩ /-- The sort of maps from `pempty` is equivalent to `punit`. -/ def pempty_arrow_equiv_punit (α : Sort*) : (pempty → α) ≃ punit.{u} := ⟨λ f, punit.star, λ u e, e.rec _, λ f, funext $ λ x, x.rec _, λ u, by { cases u, refl }⟩ /-- The sort of maps from `false` is equivalent to `punit`. -/ def false_arrow_equiv_punit (α : Sort*) : (false → α) ≃ punit.{u} := calc (false → α) ≃ (empty → α) : arrow_congr false_equiv_empty (equiv.refl _) ... ≃ punit : empty_arrow_equiv_punit _ end /-- Product of two equivalences. If `α₁ ≃ α₂` and `β₁ ≃ β₂`, then `α₁ × β₁ ≃ α₂ × β₂`. -/ @[congr] def prod_congr {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : α₁ × β₁ ≃ α₂ × β₂ := ⟨prod.map e₁ e₂, prod.map e₁.symm e₂.symm, λ ⟨a, b⟩, by simp, λ ⟨a, b⟩, by simp⟩ @[simp] theorem coe_prod_congr {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : ⇑(prod_congr e₁ e₂) = prod.map e₁ e₂ := rfl @[simp] theorem prod_congr_symm {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (prod_congr e₁ e₂).symm = prod_congr e₁.symm e₂.symm := rfl /-- Type product is commutative up to an equivalence: `α × β ≃ β × α`. -/ def prod_comm (α β : Type*) : α × β ≃ β × α := ⟨prod.swap, prod.swap, λ⟨a, b⟩, rfl, λ⟨a, b⟩, rfl⟩ @[simp] lemma coe_prod_comm (α β) : ⇑(prod_comm α β)= prod.swap := rfl @[simp] lemma prod_comm_symm (α β) : (prod_comm α β).symm = prod_comm β α := rfl /-- Type product is associative up to an equivalence. -/ def prod_assoc (α β γ : Sort*) : (α × β) × γ ≃ α × (β × γ) := ⟨λ p, ⟨p.1.1, ⟨p.1.2, p.2⟩⟩, λp, ⟨⟨p.1, p.2.1⟩, p.2.2⟩, λ ⟨⟨a, b⟩, c⟩, rfl, λ ⟨a, ⟨b, c⟩⟩, rfl⟩ @[simp] theorem prod_assoc_apply {α β γ : Sort*} (p : (α × β) × γ) : prod_assoc α β γ p = ⟨p.1.1, ⟨p.1.2, p.2⟩⟩ := rfl @[simp] theorem prod_assoc_sym_apply {α β γ : Sort*} (p : α × (β × γ)) : (prod_assoc α β γ).symm p = ⟨⟨p.1, p.2.1⟩, p.2.2⟩ := rfl section /-- `punit` is a right identity for type product up to an equivalence. -/ def prod_punit (α : Type*) : α × punit.{u+1} ≃ α := ⟨λ p, p.1, λ a, (a, punit.star), λ ⟨_, punit.star⟩, rfl, λ a, rfl⟩ @[simp] theorem prod_punit_apply {α : Sort*} (a : α × punit.{u+1}) : prod_punit α a = a.1 := rfl /-- `punit` is a left identity for type product up to an equivalence. -/ def punit_prod (α : Type*) : punit.{u+1} × α ≃ α := calc punit × α ≃ α × punit : prod_comm _ _ ... ≃ α : prod_punit _ @[simp] theorem punit_prod_apply {α : Type*} (a : punit.{u+1} × α) : punit_prod α a = a.2 := rfl /-- `empty` type is a right absorbing element for type product up to an equivalence. -/ def prod_empty (α : Type*) : α × empty ≃ empty := equiv_empty (λ ⟨_, e⟩, e.rec _) /-- `empty` type is a left absorbing element for type product up to an equivalence. -/ def empty_prod (α : Type*) : empty × α ≃ empty := equiv_empty (λ ⟨e, _⟩, e.rec _) /-- `pempty` type is a right absorbing element for type product up to an equivalence. -/ def prod_pempty (α : Type*) : α × pempty ≃ pempty := equiv_pempty (λ ⟨_, e⟩, e.rec _) /-- `pempty` type is a left absorbing element for type product up to an equivalence. -/ def pempty_prod (α : Type*) : pempty × α ≃ pempty := equiv_pempty (λ ⟨e, _⟩, e.rec _) end section open sum /-- `psum` is equivalent to `sum`. -/ def psum_equiv_sum (α β : Type*) : psum α β ≃ α ⊕ β := ⟨λ s, psum.cases_on s inl inr, λ s, sum.cases_on s psum.inl psum.inr, λ s, by cases s; refl, λ s, by cases s; refl⟩ /-- If `α ≃ α'` and `β ≃ β'`, then `α ⊕ β ≃ α' ⊕ β'`. -/ def sum_congr {α₁ β₁ α₂ β₂ : Type*} (ea : α₁ ≃ α₂) (eb : β₁ ≃ β₂) : α₁ ⊕ β₁ ≃ α₂ ⊕ β₂ := ⟨sum.map ea eb, sum.map ea.symm eb.symm, λ x, by simp, λ x, by simp⟩ @[simp] theorem sum_congr_apply {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (a : α₁ ⊕ β₁) : sum_congr e₁ e₂ a = a.map e₁ e₂ := rfl @[simp] lemma sum_congr_symm {α β γ δ : Type u} (e : α ≃ β) (f : γ ≃ δ) : (equiv.sum_congr e f).symm = (equiv.sum_congr (e.symm) (f.symm)) := rfl /-- `bool` is equivalent the sum of two `punit`s. -/ def bool_equiv_punit_sum_punit : bool ≃ punit.{u+1} ⊕ punit.{v+1} := ⟨λ b, cond b (inr punit.star) (inl punit.star), λ s, sum.rec_on s (λ_, ff) (λ_, tt), λ b, by cases b; refl, λ s, by rcases s with ⟨⟨⟩⟩ | ⟨⟨⟩⟩; refl⟩ /-- `Prop` is noncomputably equivalent to `bool`. -/ noncomputable def Prop_equiv_bool : Prop ≃ bool := ⟨λ p, @to_bool p (classical.prop_decidable _), λ b, b, λ p, by simp, λ b, by simp⟩ /-- Sum of types is commutative up to an equivalence. -/ def sum_comm (α β : Sort*) : α ⊕ β ≃ β ⊕ α := ⟨sum.swap, sum.swap, sum.swap_swap, sum.swap_swap⟩ @[simp] lemma sum_comm_apply (α β) (a) : sum_comm α β a = a.swap := rfl @[simp] lemma sum_comm_symm (α β) : (sum_comm α β).symm = sum_comm β α := rfl /-- Sum of types is associative up to an equivalence. -/ def sum_assoc (α β γ : Sort*) : (α ⊕ β) ⊕ γ ≃ α ⊕ (β ⊕ γ) := ⟨sum.elim (sum.elim sum.inl (sum.inr ∘ sum.inl)) (sum.inr ∘ sum.inr), sum.elim (sum.inl ∘ sum.inl) $ sum.elim (sum.inl ∘ sum.inr) sum.inr, by rintros (⟨_ | _⟩ | _); refl, by rintros (_ | ⟨_ | _⟩); refl⟩ @[simp] theorem sum_assoc_apply_in1 {α β γ} (a) : sum_assoc α β γ (inl (inl a)) = inl a := rfl @[simp] theorem sum_assoc_apply_in2 {α β γ} (b) : sum_assoc α β γ (inl (inr b)) = inr (inl b) := rfl @[simp] theorem sum_assoc_apply_in3 {α β γ} (c) : sum_assoc α β γ (inr c) = inr (inr c) := rfl /-- Sum with `empty` is equivalent to the original type. -/ def sum_empty (α : Type*) : α ⊕ empty ≃ α := ⟨sum.elim id (empty.rec _), inl, λ s, by { rcases s with _ | ⟨⟨⟩⟩, refl }, λ a, rfl⟩ @[simp] lemma sum_empty_apply_inl {α} (a) : sum_empty α (sum.inl a) = a := rfl /-- The sum of `empty` with any `Sort*` is equivalent to the right summand. -/ def empty_sum (α : Sort*) : empty ⊕ α ≃ α := (sum_comm _ _).trans $ sum_empty _ @[simp] lemma empty_sum_apply_inr {α} (a) : empty_sum α (sum.inr a) = a := rfl /-- Sum with `pempty` is equivalent to the original type. -/ def sum_pempty (α : Type*) : α ⊕ pempty ≃ α := ⟨sum.elim id (pempty.rec _), inl, λ s, by { rcases s with _ | ⟨⟨⟩⟩, refl }, λ a, rfl⟩ @[simp] lemma sum_pempty_apply_inl {α} (a) : sum_pempty α (sum.inl a) = a := rfl /-- The sum of `pempty` with any `Sort*` is equivalent to the right summand. -/ def pempty_sum (α : Sort*) : pempty ⊕ α ≃ α := (sum_comm _ _).trans $ sum_pempty _ @[simp] lemma pempty_sum_apply_inr {α} (a) : pempty_sum α (sum.inr a) = a := rfl /-- `option α` is equivalent to `α ⊕ punit` -/ def option_equiv_sum_punit (α : Sort*) : option α ≃ α ⊕ punit.{u+1} := ⟨λ o, match o with none := inr punit.star | some a := inl a end, λ s, match s with inr _ := none | inl a := some a end, λ o, by cases o; refl, λ s, by rcases s with _ | ⟨⟨⟩⟩; refl⟩ @[simp] lemma option_equiv_sum_punit_none {α} : option_equiv_sum_punit α none = sum.inr () := rfl @[simp] lemma option_equiv_sum_punit_some {α} (a) : option_equiv_sum_punit α (some a) = sum.inl a := rfl /-- The set of `x : option α` such that `is_some x` is equivalent to `α`. -/ def option_is_some_equiv (α : Type*) : {x : option α // x.is_some} ≃ α := { to_fun := λ o, option.get o.2, inv_fun := λ x, ⟨some x, dec_trivial⟩, left_inv := λ o, subtype.eq $ option.some_get _, right_inv := λ x, option.get_some _ _ } /-- `α ⊕ β` is equivalent to a `sigma`-type over `bool`. -/ def sum_equiv_sigma_bool (α β : Sort*) : α ⊕ β ≃ (Σ b: bool, cond b α β) := ⟨λ s, match s with inl a := ⟨tt, a⟩ | inr b := ⟨ff, b⟩ end, λ s, match s with ⟨tt, a⟩ := inl a | ⟨ff, b⟩ := inr b end, λ s, by cases s; refl, λ s, by rcases s with ⟨_|_, _⟩; refl⟩ /-- `sigma_preimage_equiv f` for `f : α → β` is the natural equivalence between the type of all fibres of `f` and the total space `α`. -/ def sigma_preimage_equiv {α β : Type*} (f : α → β) : (Σ y : β, {x // f x = y}) ≃ α := ⟨λ x, x.2.1, λ x, ⟨f x, x, rfl⟩, λ ⟨y, x, rfl⟩, rfl, λ x, rfl⟩ @[simp] lemma sigma_preimage_equiv_apply {α β : Type*} (f : α → β) (x : Σ y : β, {x // f x = y}) : (sigma_preimage_equiv f) x = x.2.1 := rfl @[simp] lemma sigma_preimage_equiv_symm_apply_fst {α β : Type*} (f : α → β) (a : α) : ((sigma_preimage_equiv f).symm a).1 = f a := rfl @[simp] lemma sigma_preimage_equiv_symm_apply_snd_fst {α β : Type*} (f : α → β) (a : α) : ((sigma_preimage_equiv f).symm a).2.1 = a := rfl end section sum_compl /-- For any predicate `p` on `α`, the sum of the two subtypes `{a // p a}` and its complement `{a // ¬ p a}` is naturally equivalent to `α`. -/ def sum_compl {α : Type*} (p : α → Prop) [decidable_pred p] : {a // p a} ⊕ {a // ¬ p a} ≃ α := { to_fun := sum.elim coe coe, inv_fun := λ a, if h : p a then sum.inl ⟨a, h⟩ else sum.inr ⟨a, h⟩, left_inv := by { rintros (⟨x,hx⟩|⟨x,hx⟩); dsimp; [rw dif_pos, rw dif_neg], }, right_inv := λ a, by { dsimp, split_ifs; refl } } @[simp] lemma sum_compl_apply_inl {α : Type*} (p : α → Prop) [decidable_pred p] (x : {a // p a}) : sum_compl p (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {α : Type*} (p : α → Prop) [decidable_pred p] (x : {a // ¬ p a}) : sum_compl p (sum.inr x) = x := rfl @[simp] lemma sum_compl_apply_symm_of_pos {α : Type*} (p : α → Prop) [decidable_pred p] (a : α) (h : p a) : (sum_compl p).symm a = sum.inl ⟨a, h⟩ := dif_pos h @[simp] lemma sum_compl_apply_symm_of_neg {α : Type*} (p : α → Prop) [decidable_pred p] (a : α) (h : ¬ p a) : (sum_compl p).symm a = sum.inr ⟨a, h⟩ := dif_neg h end sum_compl section subtype_preimage variables (p : α → Prop) [decidable_pred p] (x₀ : {a // p a} → β) /-- For a fixed function `x₀ : {a // p a} → β` defined on a subtype of `α`, the subtype of functions `x : α → β` that agree with `x₀` on the subtype `{a // p a}` is naturally equivalent to the type of functions `{a // ¬ p a} → β`. -/ def subtype_preimage : {x : α → β // x ∘ coe = x₀} ≃ ({a // ¬ p a} → β) := { to_fun := λ (x : {x : α → β // x ∘ coe = x₀}) a, (x : α → β) a, inv_fun := λ x, ⟨λ a, if h : p a then x₀ ⟨a, h⟩ else x ⟨a, h⟩, funext $ λ ⟨a, h⟩, dif_pos h⟩, left_inv := λ ⟨x, hx⟩, subtype.val_injective $ funext $ λ a, (by { dsimp, split_ifs; [ rw ← hx, skip ]; refl }), right_inv := λ x, funext $ λ ⟨a, h⟩, show dite (p a) _ _ = _, by { dsimp, rw [dif_neg h] } } @[simp] lemma subtype_preimage_apply (x : {x : α → β // x ∘ coe = x₀}) : subtype_preimage p x₀ x = λ a, (x : α → β) a := rfl @[simp] lemma subtype_preimage_symm_apply_coe (x : {a // ¬ p a} → β) : ((subtype_preimage p x₀).symm x : α → β) = λ a, if h : p a then x₀ ⟨a, h⟩ else x ⟨a, h⟩ := rfl lemma subtype_preimage_symm_apply_coe_pos (x : {a // ¬ p a} → β) (a : α) (h : p a) : ((subtype_preimage p x₀).symm x : α → β) a = x₀ ⟨a, h⟩ := dif_pos h lemma subtype_preimage_symm_apply_coe_neg (x : {a // ¬ p a} → β) (a : α) (h : ¬ p a) : ((subtype_preimage p x₀).symm x : α → β) a = x ⟨a, h⟩ := dif_neg h end subtype_preimage section fun_unique variables (α β) [unique α] /-- If `α` has a unique term, then the type of function `α → β` is equivalent to `β`. -/ def fun_unique : (α → β) ≃ β := { to_fun := λ f, f (default α), inv_fun := λ b a, b, left_inv := λ f, funext $ λ a, congr_arg f $ subsingleton.elim _ _, right_inv := λ b, rfl } variables {α β} @[simp] lemma fun_unique_apply (f : α → β) : fun_unique α β f = f (default α) := rfl @[simp] lemma fun_unique_symm_apply (b : β) (a : α) : (fun_unique α β).symm b a = b := rfl end fun_unique section /-- A family of equivalences `Π a, β₁ a ≃ β₂ a` generates an equivalence between `Π a, β₁ a` and `Π a, β₂ a`. -/ def Pi_congr_right {α} {β₁ β₂ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) : (Π a, β₁ a) ≃ (Π a, β₂ a) := ⟨λ H a, F a (H a), λ H a, (F a).symm (H a), λ H, funext $ by simp, λ H, funext $ by simp⟩ /-- Dependent `curry` equivalence: the type of dependent functions on `Σ i, β i` is equivalent to the type of dependent functions of two arguments (i.e., functions to the space of functions). -/ def Pi_curry {α} {β : α → Sort*} (γ : Π a, β a → Sort*) : (Π x : Σ i, β i, γ x.1 x.2) ≃ (Π a b, γ a b) := { to_fun := λ f x y, f ⟨x,y⟩, inv_fun := λ f x, f x.1 x.2, left_inv := λ f, funext $ λ ⟨x,y⟩, rfl, right_inv := λ f, funext $ λ x, funext $ λ y, rfl } end section /-- A `psigma`-type is equivalent to the corresponding `sigma`-type. -/ def psigma_equiv_sigma {α} (β : α → Sort*) : (Σ' i, β i) ≃ Σ i, β i := ⟨λ a, ⟨a.1, a.2⟩, λ a, ⟨a.1, a.2⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩ @[simp] lemma psigma_equiv_sigma_apply {α} (β : α → Sort*) (x) : psigma_equiv_sigma β x = ⟨x.1, x.2⟩ := rfl @[simp] lemma psigma_equiv_sigma_symm_apply {α} (β : α → Sort*) (x) : (psigma_equiv_sigma β).symm x = ⟨x.1, x.2⟩ := rfl /-- A family of equivalences `Π a, β₁ a ≃ β₂ a` generates an equivalence between `Σ a, β₁ a` and `Σ a, β₂ a`. -/ def sigma_congr_right {α} {β₁ β₂ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) : (Σ a, β₁ a) ≃ Σ a, β₂ a := ⟨λ a, ⟨a.1, F a.1 a.2⟩, λ a, ⟨a.1, (F a.1).symm a.2⟩, λ ⟨a, b⟩, congr_arg (sigma.mk a) $ symm_apply_apply (F a) b, λ ⟨a, b⟩, congr_arg (sigma.mk a) $ apply_symm_apply (F a) b⟩ @[simp] lemma sigma_congr_right_apply {α} {β₁ β₂ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) (x) : sigma_congr_right F x = ⟨x.1, F x.1 x.2⟩ := rfl @[simp] lemma sigma_congr_right_symm_apply {α} {β₁ β₂ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) (x) : (sigma_congr_right F).symm x = ⟨x.1, (F x.1).symm x.2⟩ := rfl /-- An equivalence `f : α₁ ≃ α₂` generates an equivalence between `Σ a, β (f a)` and `Σ a, β a`. -/ def sigma_congr_left {α₁ α₂} {β : α₂ → Sort*} (e : α₁ ≃ α₂) : (Σ a:α₁, β (e a)) ≃ (Σ a:α₂, β a) := ⟨λ a, ⟨e a.1, a.2⟩, λ a, ⟨e.symm a.1, @@eq.rec β a.2 (e.right_inv a.1).symm⟩, λ ⟨a, b⟩, match e.symm (e a), e.left_inv a : ∀ a' (h : a' = a), @sigma.mk _ (β ∘ e) _ (@@eq.rec β b (congr_arg e h.symm)) = ⟨a, b⟩ with | _, rfl := rfl end, λ ⟨a, b⟩, match e (e.symm a), _ : ∀ a' (h : a' = a), sigma.mk a' (@@eq.rec β b h.symm) = ⟨a, b⟩ with | _, rfl := rfl end⟩ @[simp] lemma sigma_congr_left_apply {α₁ α₂} {β : α₂ → Sort*} (e : α₁ ≃ α₂) (x : Σ a, β (e a)) : sigma_congr_left e x = ⟨e x.1, x.2⟩ := rfl /-- Transporting a sigma type through an equivalence of the base -/ def sigma_congr_left' {α₁ α₂} {β : α₁ → Sort*} (f : α₁ ≃ α₂) : (Σ a:α₁, β a) ≃ (Σ a:α₂, β (f.symm a)) := (sigma_congr_left f.symm).symm /-- Transporting a sigma type through an equivalence of the base and a family of equivalences of matching fibers -/ def sigma_congr {α₁ α₂} {β₁ : α₁ → Sort*} {β₂ : α₂ → Sort*} (f : α₁ ≃ α₂) (F : ∀ a, β₁ a ≃ β₂ (f a)) : sigma β₁ ≃ sigma β₂ := (sigma_congr_right F).trans (sigma_congr_left f) /-- `sigma` type with a constant fiber is equivalent to the product. -/ def sigma_equiv_prod (α β : Type*) : (Σ_:α, β) ≃ α × β := ⟨λ a, ⟨a.1, a.2⟩, λ a, ⟨a.1, a.2⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩ @[simp] lemma sigma_equiv_prod_apply {α β : Type*} (x : Σ _:α, β) : sigma_equiv_prod α β x = ⟨x.1, x.2⟩ := rfl @[simp] lemma sigma_equiv_prod_symm_apply {α β : Type*} (x : α × β) : (sigma_equiv_prod α β).symm x = ⟨x.1, x.2⟩ := rfl /-- If each fiber of a `sigma` type is equivalent to a fixed type, then the sigma type is equivalent to the product. -/ def sigma_equiv_prod_of_equiv {α β} {β₁ : α → Sort*} (F : Π a, β₁ a ≃ β) : sigma β₁ ≃ α × β := (sigma_congr_right F).trans (sigma_equiv_prod α β) end section prod_congr variables {α₁ β₁ β₂ : Type*} (e : α₁ → β₁ ≃ β₂) /-- A family of equivalences `Π (a : α₁), β₁ ≃ β₂` generates an equivalence between `β₁ × α₁` and `β₂ × α₁`. -/ def prod_congr_left : β₁ × α₁ ≃ β₂ × α₁ := { to_fun := λ ab, ⟨e ab.2 ab.1, ab.2⟩, inv_fun := λ ab, ⟨(e ab.2).symm ab.1, ab.2⟩, left_inv := by { rintros ⟨a, b⟩, simp }, right_inv := by { rintros ⟨a, b⟩, simp } } @[simp] lemma prod_congr_left_apply (b : β₁) (a : α₁) : prod_congr_left e (b, a) = (e a b, a) := rfl lemma prod_congr_refl_right (e : β₁ ≃ β₂) : prod_congr e (equiv.refl α₁) = prod_congr_left (λ _, e) := by { ext ⟨a, b⟩ : 1, simp } /-- A family of equivalences `Π (a : α₁), β₁ ≃ β₂` generates an equivalence between `α₁ × β₁` and `α₁ × β₂`. -/ def prod_congr_right : α₁ × β₁ ≃ α₁ × β₂ := { to_fun := λ ab, ⟨ab.1, e ab.1 ab.2⟩, inv_fun := λ ab, ⟨ab.1, (e ab.1).symm ab.2⟩, left_inv := by { rintros ⟨a, b⟩, simp }, right_inv := by { rintros ⟨a, b⟩, simp } } @[simp] lemma prod_congr_right_apply (a : α₁) (b : β₁) : prod_congr_right e (a, b) = (a, e a b) := rfl lemma prod_congr_refl_left (e : β₁ ≃ β₂) : prod_congr (equiv.refl α₁) e = prod_congr_right (λ _, e) := by { ext ⟨a, b⟩ : 1, simp } @[simp] lemma prod_congr_left_trans_prod_comm : (prod_congr_left e).trans (prod_comm _ _) = (prod_comm _ _).trans (prod_congr_right e) := by { ext ⟨a, b⟩ : 1, simp } @[simp] lemma prod_congr_right_trans_prod_comm : (prod_congr_right e).trans (prod_comm _ _) = (prod_comm _ _).trans (prod_congr_left e) := by { ext ⟨a, b⟩ : 1, simp } lemma sigma_congr_right_sigma_equiv_prod : (sigma_congr_right e).trans (sigma_equiv_prod α₁ β₂) = (sigma_equiv_prod α₁ β₁).trans (prod_congr_right e) := by { ext ⟨a, b⟩ : 1, simp } lemma sigma_equiv_prod_sigma_congr_right : (sigma_equiv_prod α₁ β₁).symm.trans (sigma_congr_right e) = (prod_congr_right e).trans (sigma_equiv_prod α₁ β₂).symm := by { ext ⟨a, b⟩ : 1, simp } end prod_congr namespace perm variables {α₁ β₁ β₂ : Type*} [decidable_eq α₁] (a : α₁) (e : perm β₁) /-- `prod_extend_right a e` extends `e : perm β` to `perm (α × β)` by sending `(a, b)` to `(a, e b)` and keeping the other `(a', b)` fixed. -/ def prod_extend_right : perm (α₁ × β₁) := { to_fun := λ ab, if ab.fst = a then (a, e ab.snd) else ab, inv_fun := λ ab, if ab.fst = a then (a, e⁻¹ ab.snd) else ab, left_inv := by { rintros ⟨k', x⟩, simp only, split_ifs with h; simp [h] }, right_inv := by { rintros ⟨k', x⟩, simp only, split_ifs with h; simp [h] } } @[simp] lemma prod_extend_right_apply_eq (b : β₁) : prod_extend_right a e (a, b) = (a, e b) := if_pos rfl lemma prod_extend_right_apply_ne {a a' : α₁} (h : a' ≠ a) (b : β₁) : prod_extend_right a e (a', b) = (a', b) := if_neg h lemma eq_of_prod_extend_right_ne {e : perm β₁} {a a' : α₁} {b : β₁} (h : prod_extend_right a e (a', b) ≠ (a', b)) : a' = a := by { contrapose! h, exact prod_extend_right_apply_ne _ h _ } @[simp] lemma fst_prod_extend_right (ab : α₁ × β₁) : (prod_extend_right a e ab).fst = ab.fst := begin rw [prod_extend_right, coe_fn_mk], split_ifs with h, { rw h }, { refl } end end perm section /-- The type of functions to a product `α × β` is equivalent to the type of pairs of functions `γ → α` and `γ → β`. -/ def arrow_prod_equiv_prod_arrow (α β γ : Type*) : (γ → α × β) ≃ (γ → α) × (γ → β) := ⟨λ f, (λ c, (f c).1, λ c, (f c).2), λ p c, (p.1 c, p.2 c), λ f, funext $ λ c, prod.mk.eta, λ p, by { cases p, refl }⟩ /-- Functions `α → β → γ` are equivalent to functions on `α × β`. -/ def arrow_arrow_equiv_prod_arrow (α β γ : Sort*) : (α → β → γ) ≃ (α × β → γ) := ⟨uncurry, curry, curry_uncurry, uncurry_curry⟩ open sum /-- The type of functions on a sum type `α ⊕ β` is equivalent to the type of pairs of functions on `α` and on `β`. -/ def sum_arrow_equiv_prod_arrow (α β γ : Type*) : ((α ⊕ β) → γ) ≃ (α → γ) × (β → γ) := ⟨λ f, (f ∘ inl, f ∘ inr), λ p, sum.elim p.1 p.2, λ f, by { ext ⟨⟩; refl }, λ p, by { cases p, refl }⟩ /-- Type product is right distributive with respect to type sum up to an equivalence. -/ def sum_prod_distrib (α β γ : Sort*) : (α ⊕ β) × γ ≃ (α × γ) ⊕ (β × γ) := ⟨λ p, match p with (inl a, c) := inl (a, c) | (inr b, c) := inr (b, c) end, λ s, match s with inl q := (inl q.1, q.2) | inr q := (inr q.1, q.2) end, λ p, by rcases p with ⟨_ | _, _⟩; refl, λ s, by rcases s with ⟨_, _⟩ | ⟨_, _⟩; refl⟩ @[simp] theorem sum_prod_distrib_apply_left {α β γ} (a : α) (c : γ) : sum_prod_distrib α β γ (sum.inl a, c) = sum.inl (a, c) := rfl @[simp] theorem sum_prod_distrib_apply_right {α β γ} (b : β) (c : γ) : sum_prod_distrib α β γ (sum.inr b, c) = sum.inr (b, c) := rfl /-- Type product is left distributive with respect to type sum up to an equivalence. -/ def prod_sum_distrib (α β γ : Sort*) : α × (β ⊕ γ) ≃ (α × β) ⊕ (α × γ) := calc α × (β ⊕ γ) ≃ (β ⊕ γ) × α : prod_comm _ _ ... ≃ (β × α) ⊕ (γ × α) : sum_prod_distrib _ _ _ ... ≃ (α × β) ⊕ (α × γ) : sum_congr (prod_comm _ _) (prod_comm _ _) @[simp] theorem prod_sum_distrib_apply_left {α β γ} (a : α) (b : β) : prod_sum_distrib α β γ (a, sum.inl b) = sum.inl (a, b) := rfl @[simp] theorem prod_sum_distrib_apply_right {α β γ} (a : α) (c : γ) : prod_sum_distrib α β γ (a, sum.inr c) = sum.inr (a, c) := rfl /-- The product of an indexed sum of types (formally, a `sigma`-type `Σ i, α i`) by a type `β` is equivalent to the sum of products `Σ i, (α i × β)`. -/ def sigma_prod_distrib {ι : Type*} (α : ι → Type*) (β : Type*) : ((Σ i, α i) × β) ≃ (Σ i, (α i × β)) := ⟨λ p, ⟨p.1.1, (p.1.2, p.2)⟩, λ p, (⟨p.1, p.2.1⟩, p.2.2), λ p, by { rcases p with ⟨⟨_, _⟩, _⟩, refl }, λ p, by { rcases p with ⟨_, ⟨_, _⟩⟩, refl }⟩ /-- The product `bool × α` is equivalent to `α ⊕ α`. -/ def bool_prod_equiv_sum (α : Type u) : bool × α ≃ α ⊕ α := calc bool × α ≃ (unit ⊕ unit) × α : prod_congr bool_equiv_punit_sum_punit (equiv.refl _) ... ≃ (unit × α) ⊕ (unit × α) : sum_prod_distrib _ _ _ ... ≃ α ⊕ α : sum_congr (punit_prod _) (punit_prod _) end section open sum nat /-- The set of natural numbers is equivalent to `ℕ ⊕ punit`. -/ def nat_equiv_nat_sum_punit : ℕ ≃ ℕ ⊕ punit.{u+1} := ⟨λ n, match n with zero := inr punit.star | succ a := inl a end, λ s, match s with inl n := succ n | inr punit.star := zero end, λ n, begin cases n, repeat { refl } end, λ s, begin cases s with a u, { refl }, {cases u, { refl }} end⟩ /-- `ℕ ⊕ punit` is equivalent to `ℕ`. -/ def nat_sum_punit_equiv_nat : ℕ ⊕ punit.{u+1} ≃ ℕ := nat_equiv_nat_sum_punit.symm /-- The type of integer numbers is equivalent to `ℕ ⊕ ℕ`. -/ def int_equiv_nat_sum_nat : ℤ ≃ ℕ ⊕ ℕ := by refine ⟨_, _, _, _⟩; intro z; {cases z; [left, right]; assumption} <|> {cases z; refl} end /-- An equivalence between `α` and `β` generates an equivalence between `list α` and `list β`. -/ def list_equiv_of_equiv {α β : Type*} (e : α ≃ β) : list α ≃ list β := { to_fun := list.map e, inv_fun := list.map e.symm, left_inv := λ l, by rw [list.map_map, e.symm_comp_self, list.map_id], right_inv := λ l, by rw [list.map_map, e.self_comp_symm, list.map_id] } /-- `fin n` is equivalent to `{m // m < n}`. -/ def fin_equiv_subtype (n : ℕ) : fin n ≃ {m // m < n} := ⟨λ x, ⟨x.1, x.2⟩, λ x, ⟨x.1, x.2⟩, λ ⟨a, b⟩, rfl,λ ⟨a, b⟩, rfl⟩ /-- If `α` is equivalent to `β`, then `unique α` is equivalent to `β`. -/ def unique_congr (e : α ≃ β) : unique α ≃ unique β := { to_fun := λ h, @equiv.unique _ _ h e.symm, inv_fun := λ h, @equiv.unique _ _ h e, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } section open subtype /-- If `α` is equivalent to `β` and the predicates `p : α → Prop` and `q : β → Prop` are equivalent at corresponding points, then `{a // p a}` is equivalent to `{b // q b}`. -/ def subtype_congr {p : α → Prop} {q : β → Prop} (e : α ≃ β) (h : ∀ a, p a ↔ q (e a)) : {a : α // p a} ≃ {b : β // q b} := ⟨λ x, ⟨e x.1, (h _).1 x.2⟩, λ y, ⟨e.symm y.1, (h _).2 (by { simp, exact y.2 })⟩, λ ⟨x, h⟩, subtype.ext_val $ by simp, λ ⟨y, h⟩, subtype.ext_val $ by simp⟩ /-- If two predicates `p` and `q` are pointwise equivalent, then `{x // p x}` is equivalent to `{x // q x}`. -/ def subtype_congr_right {p q : α → Prop} (e : ∀x, p x ↔ q x) : {x // p x} ≃ {x // q x} := subtype_congr (equiv.refl _) e @[simp] lemma subtype_congr_right_mk {p q : α → Prop} (e : ∀x, p x ↔ q x) {x : α} (h : p x) : subtype_congr_right e ⟨x, h⟩ = ⟨x, (e x).1 h⟩ := rfl /-- If `α ≃ β`, then for any predicate `p : β → Prop` the subtype `{a // p (e a)}` is equivalent to the subtype `{b // p b}`. -/ def subtype_equiv_of_subtype {p : β → Prop} (e : α ≃ β) : {a : α // p (e a)} ≃ {b : β // p b} := subtype_congr e $ by simp /-- If `α ≃ β`, then for any predicate `p : α → Prop` the subtype `{a // p a}` is equivalent to the subtype `{b // p (e.symm b)}`. This version is used by `equiv_rw`. -/ def subtype_equiv_of_subtype' {p : α → Prop} (e : α ≃ β) : {a : α // p a} ≃ {b : β // p (e.symm b)} := e.symm.subtype_equiv_of_subtype.symm /-- If two predicates are equal, then the corresponding subtypes are equivalent. -/ def subtype_congr_prop {α : Type*} {p q : α → Prop} (h : p = q) : subtype p ≃ subtype q := subtype_congr (equiv.refl α) (assume a, h ▸ iff.rfl) /-- The subtypes corresponding to equal sets are equivalent. -/ def set_congr {α : Type*} {s t : set α} (h : s = t) : s ≃ t := subtype_congr_prop h /-- A subtype of a subtype is equivalent to the subtype of elements satisfying both predicates. This version allows the “inner” predicate to depend on `h : p a`. -/ def subtype_subtype_equiv_subtype_exists {α : Type u} (p : α → Prop) (q : subtype p → Prop) : subtype q ≃ {a : α // ∃h:p a, q ⟨a, h⟩ } := ⟨λ⟨⟨a, ha⟩, ha'⟩, ⟨a, ha, ha'⟩, λ⟨a, ha⟩, ⟨⟨a, ha.cases_on $ assume h _, h⟩, by { cases ha, exact ha_h }⟩, assume ⟨⟨a, ha⟩, h⟩, rfl, assume ⟨a, h₁, h₂⟩, rfl⟩ /-- A subtype of a subtype is equivalent to the subtype of elements satisfying both predicates. -/ def subtype_subtype_equiv_subtype_inter {α : Type u} (p q : α → Prop) : {x : subtype p // q x.1} ≃ subtype (λ x, p x ∧ q x) := (subtype_subtype_equiv_subtype_exists p _).trans $ subtype_congr_right $ λ x, exists_prop /-- If the outer subtype has more restrictive predicate than the inner one, then we can drop the latter. -/ def subtype_subtype_equiv_subtype {α : Type u} {p q : α → Prop} (h : ∀ {x}, q x → p x) : {x : subtype p // q x.1} ≃ subtype q := (subtype_subtype_equiv_subtype_inter p _).trans $ subtype_congr_right $ assume x, ⟨and.right, λ h₁, ⟨h h₁, h₁⟩⟩ /-- If a proposition holds for all elements, then the subtype is equivalent to the original type. -/ def subtype_univ_equiv {α : Type u} {p : α → Prop} (h : ∀ x, p x) : subtype p ≃ α := ⟨λ x, x, λ x, ⟨x, h x⟩, λ x, subtype.eq rfl, λ x, rfl⟩ /-- A subtype of a sigma-type is a sigma-type over a subtype. -/ def subtype_sigma_equiv {α : Type u} (p : α → Type v) (q : α → Prop) : { y : sigma p // q y.1 } ≃ Σ(x : subtype q), p x.1 := ⟨λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, λ ⟨⟨x, h⟩, y⟩, rfl, λ ⟨⟨x, y⟩, h⟩, rfl⟩ /-- A sigma type over a subtype is equivalent to the sigma set over the original type, if the fiber is empty outside of the subset -/ def sigma_subtype_equiv_of_subset {α : Type u} (p : α → Type v) (q : α → Prop) (h : ∀ x, p x → q x) : (Σ x : subtype q, p x) ≃ Σ x : α, p x := (subtype_sigma_equiv p q).symm.trans $ subtype_univ_equiv $ λ x, h x.1 x.2 /-- If a predicate `p : β → Prop` is true on the range of a map `f : α → β`, then `Σ y : {y // p y}, {x // f x = y}` is equivalent to `α`. -/ def sigma_subtype_preimage_equiv {α : Type u} {β : Type v} (f : α → β) (p : β → Prop) (h : ∀ x, p (f x)) : (Σ y : subtype p, {x : α // f x = y}) ≃ α := calc _ ≃ Σ y : β, {x : α // f x = y} : sigma_subtype_equiv_of_subset _ p (λ y ⟨x, h'⟩, h' ▸ h x) ... ≃ α : sigma_preimage_equiv f /-- If for each `x` we have `p x ↔ q (f x)`, then `Σ y : {y // q y}, f ⁻¹' {y}` is equivalent to `{x // p x}`. -/ def sigma_subtype_preimage_equiv_subtype {α : Type u} {β : Type v} (f : α → β) {p : α → Prop} {q : β → Prop} (h : ∀ x, p x ↔ q (f x)) : (Σ y : subtype q, {x : α // f x = y}) ≃ subtype p := calc (Σ y : subtype q, {x : α // f x = y}) ≃ Σ y : subtype q, {x : subtype p // subtype.mk (f x) ((h x).1 x.2) = y} : begin apply sigma_congr_right, assume y, symmetry, refine (subtype_subtype_equiv_subtype_exists _ _).trans (subtype_congr_right _), assume x, exact ⟨λ ⟨hp, h'⟩, congr_arg subtype.val h', λ h', ⟨(h x).2 (h'.symm ▸ y.2), subtype.eq h'⟩⟩ end ... ≃ subtype p : sigma_preimage_equiv (λ x : subtype p, (⟨f x, (h x).1 x.property⟩ : subtype q)) /-- The `pi`-type `Π i, π i` is equivalent to the type of sections `f : ι → Σ i, π i` of the `sigma` type such that for all `i` we have `(f i).fst = i`. -/ def pi_equiv_subtype_sigma (ι : Type*) (π : ι → Type*) : (Πi, π i) ≃ {f : ι → Σi, π i | ∀i, (f i).1 = i } := ⟨ λf, ⟨λi, ⟨i, f i⟩, assume i, rfl⟩, λf i, begin rw ← f.2 i, exact (f.1 i).2 end, assume f, funext $ assume i, rfl, assume ⟨f, hf⟩, subtype.eq $ funext $ assume i, sigma.eq (hf i).symm $ eq_of_heq $ rec_heq_of_heq _ $ rec_heq_of_heq _ $ heq.refl _⟩ /-- The set of functions `f : Π a, β a` such that for all `a` we have `p a (f a)` is equivalent to the set of functions `Π a, {b : β a // p a b}`. -/ def subtype_pi_equiv_pi {α : Sort u} {β : α → Sort v} {p : Πa, β a → Prop} : {f : Πa, β a // ∀a, p a (f a) } ≃ Πa, { b : β a // p a b } := ⟨λf a, ⟨f.1 a, f.2 a⟩, λf, ⟨λa, (f a).1, λa, (f a).2⟩, by { rintro ⟨f, h⟩, refl }, by { rintro f, funext a, exact subtype.ext_val rfl }⟩ /-- A subtype of a product defined by componentwise conditions is equivalent to a product of subtypes. -/ def subtype_prod_equiv_prod {α : Type u} {β : Type v} {p : α → Prop} {q : β → Prop} : {c : α × β // p c.1 ∧ q c.2} ≃ ({a // p a} × {b // q b}) := ⟨λ x, ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩, λ x, ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩, λ ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl, λ ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl⟩ end section subtype_equiv_codomain variables {X : Type*} {Y : Type*} [decidable_eq X] {x : X} /-- The type of all functions `X → Y` with prescribed values for all `x' ≠ x` is equivalent to the codomain `Y`. -/ def subtype_equiv_codomain (f : {x' // x' ≠ x} → Y) : {g : X → Y // g ∘ coe = f} ≃ Y := (subtype_preimage _ f).trans $ @fun_unique {x' // ¬ x' ≠ x} _ $ show unique {x' // ¬ x' ≠ x}, from @equiv.unique _ _ (show unique {x' // x' = x}, from { default := ⟨x, rfl⟩, uniq := λ ⟨x', h⟩, subtype.val_injective h }) (subtype_congr_right $ λ a, not_not) @[simp] lemma coe_subtype_equiv_codomain (f : {x' // x' ≠ x} → Y) : (subtype_equiv_codomain f : {g : X → Y // g ∘ coe = f} → Y) = λ g, (g : X → Y) x := rfl @[simp] lemma subtype_equiv_codomain_apply (f : {x' // x' ≠ x} → Y) (g : {g : X → Y // g ∘ coe = f}) : subtype_equiv_codomain f g = (g : X → Y) x := rfl lemma coe_subtype_equiv_codomain_symm (f : {x' // x' ≠ x} → Y) : ((subtype_equiv_codomain f).symm : Y → {g : X → Y // g ∘ coe = f}) = λ y, ⟨λ x', if h : x' ≠ x then f ⟨x', h⟩ else y, by { funext x', dsimp, erw [dif_pos x'.2, subtype.coe_eta] }⟩ := rfl @[simp] lemma subtype_equiv_codomain_symm_apply (f : {x' // x' ≠ x} → Y) (y : Y) (x' : X) : ((subtype_equiv_codomain f).symm y : X → Y) x' = if h : x' ≠ x then f ⟨x', h⟩ else y := rfl @[simp] lemma subtype_equiv_codomain_symm_apply_eq (f : {x' // x' ≠ x} → Y) (y : Y) : ((subtype_equiv_codomain f).symm y : X → Y) x = y := dif_neg (not_not.mpr rfl) lemma subtype_equiv_codomain_symm_apply_ne (f : {x' // x' ≠ x} → Y) (y : Y) (x' : X) (h : x' ≠ x) : ((subtype_equiv_codomain f).symm y : X → Y) x' = f ⟨x', h⟩ := dif_pos h end subtype_equiv_codomain namespace set open set /-- `univ α` is equivalent to `α`. -/ protected def univ (α) : @univ α ≃ α := ⟨subtype.val, λ a, ⟨a, trivial⟩, λ ⟨a, _⟩, rfl, λ a, rfl⟩ @[simp] lemma univ_apply {α : Type u} (x : @univ α) : equiv.set.univ α x = x := rfl @[simp] lemma univ_symm_apply {α : Type u} (x : α) : (equiv.set.univ α).symm x = ⟨x, trivial⟩ := rfl /-- An empty set is equivalent to the `empty` type. -/ protected def empty (α) : (∅ : set α) ≃ empty := equiv_empty $ λ ⟨x, h⟩, not_mem_empty x h /-- An empty set is equivalent to a `pempty` type. -/ protected def pempty (α) : (∅ : set α) ≃ pempty := equiv_pempty $ λ ⟨x, h⟩, not_mem_empty x h /-- If sets `s` and `t` are separated by a decidable predicate, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union' {α} {s t : set α} (p : α → Prop) [decidable_pred p] (hs : ∀ x ∈ s, p x) (ht : ∀ x ∈ t, ¬ p x) : (s ∪ t : set α) ≃ s ⊕ t := { to_fun := λ x, if hp : p x then sum.inl ⟨_, x.2.resolve_right (λ xt, ht _ xt hp)⟩ else sum.inr ⟨_, x.2.resolve_left (λ xs, hp (hs _ xs))⟩, inv_fun := λ o, match o with | (sum.inl x) := ⟨x, or.inl x.2⟩ | (sum.inr x) := ⟨x, or.inr x.2⟩ end, left_inv := λ ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, h]; congr, right_inv := λ o, begin rcases o with ⟨x, h⟩ | ⟨x, h⟩; dsimp [union'._match_1]; [simp [hs _ h], simp [ht _ h]] end } /-- If sets `s` and `t` are disjoint, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) : (s ∪ t : set α) ≃ s ⊕ t := set.union' (λ x, x ∈ s) (λ _, id) (λ x xt xs, H ⟨xs, xt⟩) lemma union_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ s) : equiv.set.union H a = sum.inl ⟨a, ha⟩ := dif_pos ha lemma union_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ t) : equiv.set.union H a = sum.inr ⟨a, ha⟩ := dif_neg $ λ h, H ⟨h, ha⟩ -- TODO: Any reason to use the same universe? /-- A singleton set is equivalent to a `punit` type. -/ protected def singleton {α} (a : α) : ({a} : set α) ≃ punit.{u} := ⟨λ _, punit.star, λ _, ⟨a, mem_singleton _⟩, λ ⟨x, h⟩, by { simp at h, subst x }, λ ⟨⟩, rfl⟩ /-- Equal sets are equivalent. -/ protected def of_eq {α : Type u} {s t : set α} (h : s = t) : s ≃ t := { to_fun := λ x, ⟨x.1, h ▸ x.2⟩, inv_fun := λ x, ⟨x.1, h.symm ▸ x.2⟩, left_inv := λ _, subtype.eq rfl, right_inv := λ _, subtype.eq rfl } @[simp] lemma of_eq_apply {α : Type u} {s t : set α} (h : s = t) (a : s) : equiv.set.of_eq h a = ⟨a, h ▸ a.2⟩ := rfl @[simp] lemma of_eq_symm_apply {α : Type u} {s t : set α} (h : s = t) (a : t) : (equiv.set.of_eq h).symm a = ⟨a, h.symm ▸ a.2⟩ := rfl /-- If `a ∉ s`, then `insert a s` is equivalent to `s ⊕ punit`. -/ protected def insert {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) : (insert a s : set α) ≃ s ⊕ punit.{u+1} := calc (insert a s : set α) ≃ ↥(s ∪ {a}) : equiv.set.of_eq (by simp) ... ≃ s ⊕ ({a} : set α) : equiv.set.union (by finish [set.subset_def]) ... ≃ s ⊕ punit.{u+1} : sum_congr (equiv.refl _) (equiv.set.singleton _) /-- If `s : set α` is a set with decidable membership, then `s ⊕ sᶜ` is equivalent to `α`. -/ protected def sum_compl {α} (s : set α) [decidable_pred s] : s ⊕ (sᶜ : set α) ≃ α := calc s ⊕ (sᶜ : set α) ≃ ↥(s ∪ sᶜ) : (equiv.set.union (by simp [set.ext_iff])).symm ... ≃ @univ α : equiv.set.of_eq (by simp) ... ≃ α : equiv.set.univ _ @[simp] lemma sum_compl_apply_inl {α : Type u} (s : set α) [decidable_pred s] (x : s) : equiv.set.sum_compl s (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {α : Type u} (s : set α) [decidable_pred s] (x : sᶜ) : equiv.set.sum_compl s (sum.inr x) = x := rfl lemma sum_compl_symm_apply_of_mem {α : Type u} {s : set α} [decidable_pred s] {x : α} (hx : x ∈ s) : (equiv.set.sum_compl s).symm x = sum.inl ⟨x, hx⟩ := have ↑(⟨x, or.inl hx⟩ : (s ∪ sᶜ : set α)) ∈ s, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_left _ this } lemma sum_compl_symm_apply_of_not_mem {α : Type u} {s : set α} [decidable_pred s] {x : α} (hx : x ∉ s) : (equiv.set.sum_compl s).symm x = sum.inr ⟨x, hx⟩ := have ↑(⟨x, or.inr hx⟩ : (s ∪ sᶜ : set α)) ∈ sᶜ, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_right _ this } /-- `sum_diff_subset s t` is the natural equivalence between `s ⊕ (t \ s)` and `t`, where `s` and `t` are two sets. -/ protected def sum_diff_subset {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] : s ⊕ (t \ s : set α) ≃ t := calc s ⊕ (t \ s : set α) ≃ (s ∪ (t \ s) : set α) : (equiv.set.union (by simp [inter_diff_self])).symm ... ≃ t : equiv.set.of_eq (by { simp [union_diff_self, union_eq_self_of_subset_left h] }) @[simp] lemma sum_diff_subset_apply_inl {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] (x : s) : equiv.set.sum_diff_subset h (sum.inl x) = inclusion h x := rfl @[simp] lemma sum_diff_subset_apply_inr {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] (x : t \ s) : equiv.set.sum_diff_subset h (sum.inr x) = inclusion (diff_subset t s) x := rfl lemma sum_diff_subset_symm_apply_of_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] {x : t} (hx : x.1 ∈ s) : (equiv.set.sum_diff_subset h).symm x = sum.inl ⟨x, hx⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inl], exact subtype.eq rfl, end lemma sum_diff_subset_symm_apply_of_not_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] {x : t} (hx : x.1 ∉ s) : (equiv.set.sum_diff_subset h).symm x = sum.inr ⟨x, ⟨x.2, hx⟩⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inr], exact subtype.eq rfl, end /-- If `s` is a set with decidable membership, then the sum of `s ∪ t` and `s ∩ t` is equivalent to `s ⊕ t`. -/ protected def union_sum_inter {α : Type u} (s t : set α) [decidable_pred s] : (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ s ⊕ t := calc (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ (s ∪ t \ s : set α) ⊕ (s ∩ t : set α) : by rw [union_diff_self] ... ≃ (s ⊕ (t \ s : set α)) ⊕ (s ∩ t : set α) : sum_congr (set.union $ subset_empty_iff.2 (inter_diff_self _ _)) (equiv.refl _) ... ≃ s ⊕ (t \ s : set α) ⊕ (s ∩ t : set α) : sum_assoc _ _ _ ... ≃ s ⊕ (t \ s ∪ s ∩ t : set α) : sum_congr (equiv.refl _) begin refine (set.union' (∉ s) _ _).symm, exacts [λ x hx, hx.2, λ x hx, not_not_intro hx.1] end ... ≃ s ⊕ t : by { rw (_ : t \ s ∪ s ∩ t = t), rw [union_comm, inter_comm, inter_union_diff] } /-- The set product of two sets is equivalent to the type product of their coercions to types. -/ protected def prod {α β} (s : set α) (t : set β) : s.prod t ≃ s × t := @subtype_prod_equiv_prod α β s t /-- If a function `f` is injective on a set `s`, then `s` is equivalent to `f '' s`. -/ protected noncomputable def image_of_inj_on {α β} (f : α → β) (s : set α) (H : inj_on f s) : s ≃ (f '' s) := ⟨λ p, ⟨f p, mem_image_of_mem f p.2⟩, λ p, ⟨classical.some p.2, (classical.some_spec p.2).1⟩, λ ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).1 h (classical.some_spec (mem_image_of_mem f h)).2), λ ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩ /-- If `f` is an injective function, then `s` is equivalent to `f '' s`. -/ protected noncomputable def image {α β} (f : α → β) (s : set α) (H : injective f) : s ≃ (f '' s) := equiv.set.image_of_inj_on f s (λ x y hx hy hxy, H hxy) @[simp] theorem image_apply {α β} (f : α → β) (s : set α) (H : injective f) (a h) : set.image f s H ⟨a, h⟩ = ⟨f a, mem_image_of_mem _ h⟩ := rfl lemma image_symm_preimage {α β} {f : α → β} (hf : injective f) (u s : set α) : (λ x, (set.image f s hf).symm x : f '' s → α) ⁻¹' u = coe ⁻¹' (f '' u) := begin ext ⟨b, a, has, rfl⟩, have : ∀(h : ∃a', a' ∈ s ∧ a' = a), classical.some h = a := λ h, (classical.some_spec h).2, simp [equiv.set.image, equiv.set.image_of_inj_on, hf, this], end /-- If `f : α → β` is an injective function, then `α` is equivalent to the range of `f`. -/ protected noncomputable def range {α β} (f : α → β) (H : injective f) : α ≃ range f := { to_fun := λ x, ⟨f x, mem_range_self _⟩, inv_fun := λ x, classical.some x.2, left_inv := λ x, H (classical.some_spec (show f x ∈ range f, from mem_range_self _)), right_inv := λ x, subtype.eq $ classical.some_spec x.2 } @[simp] theorem range_apply {α β} (f : α → β) (H : injective f) (a) : set.range f H a = ⟨f a, set.mem_range_self _⟩ := rfl theorem apply_range_symm {α β} (f : α → β) (H : injective f) (b : range f) : f ((set.range f H).symm b) = b := begin conv_rhs { rw ←((set.range f H).right_inv b), }, simp, end /-- If `α` is equivalent to `β`, then `set α` is equivalent to `set β`. -/ protected def congr {α β : Type*} (e : α ≃ β) : set α ≃ set β := ⟨λ s, e '' s, λ t, e.symm '' t, symm_image_image e, symm_image_image e.symm⟩ /-- The set `{x ∈ s | t x}` is equivalent to the set of `x : s` such that `t x`. -/ protected def sep {α : Type u} (s : set α) (t : α → Prop) : ({ x ∈ s | t x } : set α) ≃ { x : s | t x } := (equiv.subtype_subtype_equiv_subtype_inter s t).symm end set /-- If `f` is a bijective function, then its domain is equivalent to its codomain. -/ noncomputable def of_bijective {α β} (f : α → β) (hf : bijective f) : α ≃ β := (equiv.set.range f hf.1).trans $ (set_congr hf.2.range_eq).trans $ equiv.set.univ β @[simp] theorem coe_of_bijective {α β} {f : α → β} (hf : bijective f) : (of_bijective f hf : α → β) = f := rfl /-- If `f` is an injective function, then its domain is equivalent to its range. -/ noncomputable def of_injective {α β} (f : α → β) (hf : injective f) : α ≃ _root_.set.range f := of_bijective (λ x, ⟨f x, set.mem_range_self x⟩) ⟨λ x y hxy, hf $ by injections, λ ⟨_, x, rfl⟩, ⟨x, rfl⟩⟩ @[simp] lemma of_injective_apply {α β} (f : α → β) (hf : injective f) (x : α) : of_injective f hf x = ⟨f x, set.mem_range_self x⟩ := rfl /-- Subtype of the quotient is equivalent to the quotient of the subtype. Let `α` be a setoid with equivalence relation `~`. Let `p₂` be a predicate on the quotient type `α/~`, and `p₁` be the lift of this predicate to `α`: `p₁ a ↔ p₂ ⟦a⟧`. Let `~₂` be the restriction of `~` to `{x // p₁ x}`. Then `{x // p₂ x}` is equivalent to the quotient of `{x // p₁ x}` by `~₂`. -/ def subtype_quotient_equiv_quotient_subtype (p₁ : α → Prop) [s₁ : setoid α] [s₂ : setoid (subtype p₁)] (p₂ : quotient s₁ → Prop) (hp₂ : ∀ a, p₁ a ↔ p₂ ⟦a⟧) (h : ∀ x y : subtype p₁, @setoid.r _ s₂ x y ↔ (x : α) ≈ y) : {x // p₂ x} ≃ quotient s₂ := { to_fun := λ a, quotient.hrec_on a.1 (λ a h, ⟦⟨a, (hp₂ _).2 h⟩⟧) (λ a b hab, hfunext (by rw quotient.sound hab) (λ h₁ h₂ _, heq_of_eq (quotient.sound ((h _ _).2 hab)))) a.2, inv_fun := λ a, quotient.lift_on a (λ a, (⟨⟦a.1⟧, (hp₂ _).1 a.2⟩ : {x // p₂ x})) (λ a b hab, subtype.ext_val (quotient.sound ((h _ _).1 hab))), left_inv := λ ⟨a, ha⟩, quotient.induction_on a (λ a ha, rfl) ha, right_inv := λ a, quotient.induction_on a (λ ⟨a, ha⟩, rfl) } section swap variable [decidable_eq α] /-- A helper function for `equiv.swap`. -/ def swap_core (a b r : α) : α := if r = a then b else if r = b then a else r theorem swap_core_self (r a : α) : swap_core a a r = r := by { unfold swap_core, split_ifs; cc } theorem swap_core_swap_core (r a b : α) : swap_core a b (swap_core a b r) = r := by { unfold swap_core, split_ifs; cc } theorem swap_core_comm (r a b : α) : swap_core a b r = swap_core b a r := by { unfold swap_core, split_ifs; cc } /-- `swap a b` is the permutation that swaps `a` and `b` and leaves other values as is. -/ def swap (a b : α) : perm α := ⟨swap_core a b, swap_core a b, λr, swap_core_swap_core r a b, λr, swap_core_swap_core r a b⟩ theorem swap_self (a : α) : swap a a = equiv.refl _ := ext $ λ r, swap_core_self r a theorem swap_comm (a b : α) : swap a b = swap b a := ext $ λ r, swap_core_comm r _ _ theorem swap_apply_def (a b x : α) : swap a b x = if x = a then b else if x = b then a else x := rfl @[simp] theorem swap_apply_left (a b : α) : swap a b a = b := if_pos rfl @[simp] theorem swap_apply_right (a b : α) : swap a b b = a := by { by_cases h : b = a; simp [swap_apply_def, h], } theorem swap_apply_of_ne_of_ne {a b x : α} : x ≠ a → x ≠ b → swap a b x = x := by simp [swap_apply_def] {contextual := tt} @[simp] theorem swap_swap (a b : α) : (swap a b).trans (swap a b) = equiv.refl _ := ext $ λ x, swap_core_swap_core _ _ _ theorem swap_comp_apply {a b x : α} (π : perm α) : π.trans (swap a b) x = if π x = a then b else if π x = b then a else π x := by { cases π, refl } @[simp] lemma swap_inv {α : Type*} [decidable_eq α] (x y : α) : (swap x y)⁻¹ = swap x y := rfl @[simp] lemma symm_trans_swap_trans [decidable_eq β] (a b : α) (e : α ≃ β) : (e.symm.trans (swap a b)).trans e = swap (e a) (e b) := equiv.ext (λ x, begin have : ∀ a, e.symm x = a ↔ x = e a := λ a, by { rw @eq_comm _ (e.symm x), split; intros; simp * at * }, simp [swap_apply_def, this], split_ifs; simp end) @[simp] lemma swap_mul_self {α : Type*} [decidable_eq α] (i j : α) : swap i j * swap i j = 1 := equiv.swap_swap i j @[simp] lemma swap_apply_self {α : Type*} [decidable_eq α] (i j a : α) : swap i j (swap i j a) = a := by rw [← perm.mul_apply, swap_mul_self, perm.one_apply] /-- Augment an equivalence with a prescribed mapping `f a = b` -/ def set_value (f : α ≃ β) (a : α) (b : β) : α ≃ β := (swap a (f.symm b)).trans f @[simp] theorem set_value_eq (f : α ≃ β) (a : α) (b : β) : set_value f a b a = b := by { dsimp [set_value], simp [swap_apply_left] } end swap protected lemma forall_congr {p : α → Prop} {q : β → Prop} (f : α ≃ β) (h : ∀{x}, p x ↔ q (f x)) : (∀x, p x) ↔ (∀y, q y) := begin split; intros h₂ x, { rw [←f.right_inv x], apply h.mp, apply h₂ }, apply h.mpr, apply h₂ end protected lemma forall_congr' {p : α → Prop} {q : β → Prop} (f : α ≃ β) (h : ∀{x}, p (f.symm x) ↔ q x) : (∀x, p x) ↔ (∀y, q y) := (equiv.forall_congr f.symm (λ x, h.symm)).symm -- We next build some higher arity versions of `equiv.forall_congr`. -- Although they appear to just be repeated applications of `equiv.forall_congr`, -- unification of metavariables works better with these versions. -- In particular, they are necessary in `equiv_rw`. -- (Stopping at ternary functions seems reasonable: at least in 1-categorical mathematics, -- it's rare to have axioms involving more than 3 elements at once.) universes ua1 ua2 ub1 ub2 ug1 ug2 variables {α₁ : Sort ua1} {α₂ : Sort ua2} {β₁ : Sort ub1} {β₂ : Sort ub2} {γ₁ : Sort ug1} {γ₂ : Sort ug2} protected lemma forall₂_congr {p : α₁ → β₁ → Prop} {q : α₂ → β₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (h : ∀{x y}, p x y ↔ q (eα x) (eβ y)) : (∀x y, p x y) ↔ (∀x y, q x y) := begin apply equiv.forall_congr, intros, apply equiv.forall_congr, intros, apply h, end protected lemma forall₂_congr' {p : α₁ → β₁ → Prop} {q : α₂ → β₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (h : ∀{x y}, p (eα.symm x) (eβ.symm y) ↔ q x y) : (∀x y, p x y) ↔ (∀x y, q x y) := (equiv.forall₂_congr eα.symm eβ.symm (λ x y, h.symm)).symm protected lemma forall₃_congr {p : α₁ → β₁ → γ₁ → Prop} {q : α₂ → β₂ → γ₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (eγ : γ₁ ≃ γ₂) (h : ∀{x y z}, p x y z ↔ q (eα x) (eβ y) (eγ z)) : (∀x y z, p x y z) ↔ (∀x y z, q x y z) := begin apply equiv.forall₂_congr, intros, apply equiv.forall_congr, intros, apply h, end protected lemma forall₃_congr' {p : α₁ → β₁ → γ₁ → Prop} {q : α₂ → β₂ → γ₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (eγ : γ₁ ≃ γ₂) (h : ∀{x y z}, p (eα.symm x) (eβ.symm y) (eγ.symm z) ↔ q x y z) : (∀x y z, p x y z) ↔ (∀x y z, q x y z) := (equiv.forall₃_congr eα.symm eβ.symm eγ.symm (λ x y z, h.symm)).symm protected lemma forall_congr_left' {p : α → Prop} (f : α ≃ β) : (∀x, p x) ↔ (∀y, p (f.symm y)) := equiv.forall_congr f (λx, by simp) protected lemma forall_congr_left {p : β → Prop} (f : α ≃ β) : (∀x, p (f x)) ↔ (∀y, p y) := (equiv.forall_congr_left' f.symm).symm section variables (P : α → Sort w) (e : α ≃ β) /-- Transport dependent functions through an equivalence of the base space. -/ def Pi_congr_left' : (Π a, P a) ≃ (Π b, P (e.symm b)) := { to_fun := λ f x, f (e.symm x), inv_fun := λ f x, begin rw [← e.symm_apply_apply x], exact f (e x) end, left_inv := λ f, funext $ λ x, eq_of_heq ((eq_rec_heq _ _).trans (by { dsimp, rw e.symm_apply_apply })), right_inv := λ f, funext $ λ x, eq_of_heq ((eq_rec_heq _ _).trans (by { rw e.apply_symm_apply })) } @[simp] lemma Pi_congr_left'_apply (f : Π a, P a) (b : β) : ((Pi_congr_left' P e) f) b = f (e.symm b) := rfl @[simp] lemma Pi_congr_left'_symm_apply (g : Π b, P (e.symm b)) (a : α) : ((Pi_congr_left' P e).symm g) a = (by { convert g (e a), simp }) := rfl end section variables (P : β → Sort w) (e : α ≃ β) /-- Transporting dependent functions through an equivalence of the base, expressed as a "simplification". -/ def Pi_congr_left : (Π a, P (e a)) ≃ (Π b, P b) := (Pi_congr_left' P e.symm).symm end section variables {W : α → Sort w} {Z : β → Sort z} (h₁ : α ≃ β) (h₂ : Π a : α, (W a ≃ Z (h₁ a))) /-- Transport dependent functions through an equivalence of the base spaces and a family of equivalences of the matching fibers. -/ def Pi_congr : (Π a, W a) ≃ (Π b, Z b) := (equiv.Pi_congr_right h₂).trans (equiv.Pi_congr_left _ h₁) end section variables {W : α → Sort w} {Z : β → Sort z} (h₁ : α ≃ β) (h₂ : Π b : β, (W (h₁.symm b) ≃ Z b)) /-- Transport dependent functions through an equivalence of the base spaces and a family of equivalences of the matching fibres. -/ def Pi_congr' : (Π a, W a) ≃ (Π b, Z b) := (Pi_congr h₁.symm (λ b, (h₂ b).symm)).symm end end equiv instance {α} [subsingleton α] : subsingleton (ulift α) := equiv.ulift.subsingleton instance {α} [subsingleton α] : subsingleton (plift α) := equiv.plift.subsingleton instance {α} [decidable_eq α] : decidable_eq (ulift α) := equiv.ulift.decidable_eq instance {α} [decidable_eq α] : decidable_eq (plift α) := equiv.plift.decidable_eq /-- If both `α` and `β` are singletons, then `α ≃ β`. -/ def equiv_of_unique_of_unique [unique α] [unique β] : α ≃ β := { to_fun := λ _, default β, inv_fun := λ _, default α, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } /-- If `α` is a singleton, then it is equivalent to any `punit`. -/ def equiv_punit_of_unique [unique α] : α ≃ punit.{v} := equiv_of_unique_of_unique /-- If `α` is a subsingleton, then it is equivalent to `α × α`. -/ def subsingleton_prod_self_equiv {α : Type*} [subsingleton α] : α × α ≃ α := { to_fun := λ p, p.1, inv_fun := λ a, (a, a), left_inv := λ p, subsingleton.elim _ _, right_inv := λ p, subsingleton.elim _ _, } /-- To give an equivalence between two subsingleton types, it is sufficient to give any two functions between them. -/ def equiv_of_subsingleton_of_subsingleton [subsingleton α] [subsingleton β] (f : α → β) (g : β → α) : α ≃ β := { to_fun := f, inv_fun := g, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } /-- `unique (unique α)` is equivalent to `unique α`. -/ def unique_unique_equiv : unique (unique α) ≃ unique α := equiv_of_subsingleton_of_subsingleton (λ h, h.default) (λ h, { default := h, uniq := λ _, subsingleton.elim _ _ }) namespace quot /-- An equivalence `e : α ≃ β` generates an equivalence between quotient spaces, if `ra a₁ a₂ ↔ rb (e a₁) (e a₂). -/ protected def congr {ra : α → α → Prop} {rb : β → β → Prop} (e : α ≃ β) (eq : ∀a₁ a₂, ra a₁ a₂ ↔ rb (e a₁) (e a₂)) : quot ra ≃ quot rb := { to_fun := quot.map e (assume a₁ a₂, (eq a₁ a₂).1), inv_fun := quot.map e.symm (assume b₁ b₂ h, (eq (e.symm b₁) (e.symm b₂)).2 ((e.apply_symm_apply b₁).symm ▸ (e.apply_symm_apply b₂).symm ▸ h)), left_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.symm_apply_apply] }, right_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.apply_symm_apply] } } /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr_right {r r' : α → α → Prop} (eq : ∀a₁ a₂, r a₁ a₂ ↔ r' a₁ a₂) : quot r ≃ quot r' := quot.congr (equiv.refl α) eq /-- An equivalence `e : α ≃ β` generates an equivalence between the quotient space of `α` by a relation `ra` and the quotient space of `β` by the image of this relation under `e`. -/ protected def congr_left {r : α → α → Prop} (e : α ≃ β) : quot r ≃ quot (λ b b', r (e.symm b) (e.symm b')) := @quot.congr α β r (λ b b', r (e.symm b) (e.symm b')) e (λ a₁ a₂, by simp only [e.symm_apply_apply]) end quot namespace quotient /-- An equivalence `e : α ≃ β` generates an equivalence between quotient spaces, if `ra a₁ a₂ ↔ rb (e a₁) (e a₂). -/ protected def congr {ra : setoid α} {rb : setoid β} (e : α ≃ β) (eq : ∀a₁ a₂, @setoid.r α ra a₁ a₂ ↔ @setoid.r β rb (e a₁) (e a₂)) : quotient ra ≃ quotient rb := quot.congr e eq /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr_right {r r' : setoid α} (eq : ∀a₁ a₂, @setoid.r α r a₁ a₂ ↔ @setoid.r α r' a₁ a₂) : quotient r ≃ quotient r' := quot.congr_right eq end quotient /-- If a function is a bijection between `univ` and a set `s` in the target type, it induces an equivalence between the original type and the type `↑s`. -/ noncomputable def set.bij_on.equiv {α : Type*} {β : Type*} {s : set β} (f : α → β) (h : set.bij_on f set.univ s) : α ≃ s := begin have : function.bijective (λ (x : α), (⟨f x, begin exact h.maps_to (set.mem_univ x) end⟩ : s)), { split, { assume x y hxy, apply h.inj_on (set.mem_univ x) (set.mem_univ y) (subtype.mk.inj hxy) }, { assume x, rcases h.surj_on x.2 with ⟨y, hy⟩, exact ⟨y, subtype.eq hy.2⟩ } }, exact equiv.of_bijective _ this end /-- The composition of an updated function with an equiv on a subset can be expressed as an updated function. -/ lemma dite_comp_equiv_update {α : Type*} {β : Type*} {γ : Type*} {s : set α} (e : β ≃ s) (v : β → γ) (w : α → γ) (j : β) (x : γ) [decidable_eq β] [decidable_eq α] [∀ j, decidable (j ∈ s)] : (λ (i : α), if h : i ∈ s then (function.update v j x) (e.symm ⟨i, h⟩) else w i) = function.update (λ (i : α), if h : i ∈ s then v (e.symm ⟨i, h⟩) else w i) (e j) x := begin ext i, by_cases h : i ∈ s, { simp only [h, dif_pos], have A : e.symm ⟨i, h⟩ = j ↔ i = e j, by { rw equiv.symm_apply_eq, exact subtype.ext_iff_val }, by_cases h' : i = e j, { rw [A.2 h', h'], simp }, { have : ¬ e.symm ⟨i, h⟩ = j, by simpa [← A] using h', simp [h, h', this] } }, { have : i ≠ e j, by { contrapose! h, have : (e j : α) ∈ s := (e j).2, rwa ← h at this }, simp [h, this] } end
0b834a1eee5590be1d1e5a72e48833e834f0db5c
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/category_theory/adjunction/basic.lean
958f4cdba4fb25d842801c7a2d52b61211e2b315
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
15,302
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Johan Commelin, Bhavik Mehta -/ import category_theory.equivalence import data.equiv.basic namespace category_theory open category universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation local attribute [elab_simple] whisker_left whisker_right variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] /-- `F ⊣ G` represents the data of an adjunction between two functors `F : C ⥤ D` and `G : D ⥤ C`. `F` is the left adjoint and `G` is the right adjoint. -/ structure adjunction (F : C ⥤ D) (G : D ⥤ C) := (hom_equiv : Π (X Y), (F.obj X ⟶ Y) ≃ (X ⟶ G.obj Y)) (unit : 𝟭 C ⟶ F.comp G) (counit : G.comp F ⟶ 𝟭 D) (hom_equiv_unit' : Π {X Y f}, (hom_equiv X Y) f = (unit : _ ⟶ _).app X ≫ G.map f . obviously) (hom_equiv_counit' : Π {X Y g}, (hom_equiv X Y).symm g = F.map g ≫ counit.app Y . obviously) infix ` ⊣ `:15 := adjunction class is_left_adjoint (left : C ⥤ D) := (right : D ⥤ C) (adj : left ⊣ right) class is_right_adjoint (right : D ⥤ C) := (left : C ⥤ D) (adj : left ⊣ right) def left_adjoint (R : D ⥤ C) [is_right_adjoint R] : C ⥤ D := is_right_adjoint.left R def right_adjoint (L : C ⥤ D) [is_left_adjoint L] : D ⥤ C := is_left_adjoint.right L /-- The adjunction associated to a functor known to be a left adjoint. -/ def adjunction.of_left_adjoint (left : C ⥤ D) [is_left_adjoint left] : adjunction left (right_adjoint left) := is_left_adjoint.adj /-- The adjunction associated to a functor known to be a right adjoint. -/ def adjunction.of_right_adjoint (right : C ⥤ D) [is_right_adjoint right] : adjunction (left_adjoint right) right := is_right_adjoint.adj namespace adjunction restate_axiom hom_equiv_unit' restate_axiom hom_equiv_counit' attribute [simp, priority 10] hom_equiv_unit hom_equiv_counit section variables {F : C ⥤ D} {G : D ⥤ C} (adj : F ⊣ G) {X' X : C} {Y Y' : D} @[simp, priority 10] lemma hom_equiv_naturality_left_symm (f : X' ⟶ X) (g : X ⟶ G.obj Y) : (adj.hom_equiv X' Y).symm (f ≫ g) = F.map f ≫ (adj.hom_equiv X Y).symm g := by rw [hom_equiv_counit, F.map_comp, assoc, adj.hom_equiv_counit.symm] @[simp] lemma hom_equiv_naturality_left (f : X' ⟶ X) (g : F.obj X ⟶ Y) : (adj.hom_equiv X' Y) (F.map f ≫ g) = f ≫ (adj.hom_equiv X Y) g := by rw [← equiv.eq_symm_apply]; simp [-hom_equiv_unit] @[simp, priority 10] lemma hom_equiv_naturality_right (f : F.obj X ⟶ Y) (g : Y ⟶ Y') : (adj.hom_equiv X Y') (f ≫ g) = (adj.hom_equiv X Y) f ≫ G.map g := by rw [hom_equiv_unit, G.map_comp, ← assoc, ←hom_equiv_unit] @[simp] lemma hom_equiv_naturality_right_symm (f : X ⟶ G.obj Y) (g : Y ⟶ Y') : (adj.hom_equiv X Y').symm (f ≫ G.map g) = (adj.hom_equiv X Y).symm f ≫ g := by rw [equiv.symm_apply_eq]; simp [-hom_equiv_counit] @[simp] lemma left_triangle : (whisker_right adj.unit F) ≫ (whisker_left F adj.counit) = nat_trans.id _ := begin ext, dsimp, erw [← adj.hom_equiv_counit, equiv.symm_apply_eq, adj.hom_equiv_unit], simp end @[simp] lemma right_triangle : (whisker_left G adj.unit) ≫ (whisker_right adj.counit G) = nat_trans.id _ := begin ext, dsimp, erw [← adj.hom_equiv_unit, ← equiv.eq_symm_apply, adj.hom_equiv_counit], simp end @[simp, reassoc] lemma left_triangle_components : F.map (adj.unit.app X) ≫ adj.counit.app (F.obj X) = 𝟙 (F.obj X) := congr_arg (λ (t : nat_trans _ (𝟭 C ⋙ F)), t.app X) adj.left_triangle @[simp, reassoc] lemma right_triangle_components {Y : D} : adj.unit.app (G.obj Y) ≫ G.map (adj.counit.app Y) = 𝟙 (G.obj Y) := congr_arg (λ (t : nat_trans _ (G ⋙ 𝟭 C)), t.app Y) adj.right_triangle @[simp, reassoc] lemma counit_naturality {X Y : D} (f : X ⟶ Y) : F.map (G.map f) ≫ (adj.counit).app Y = (adj.counit).app X ≫ f := adj.counit.naturality f @[simp, reassoc] lemma unit_naturality {X Y : C} (f : X ⟶ Y) : (adj.unit).app X ≫ G.map (F.map f) = f ≫ (adj.unit).app Y := (adj.unit.naturality f).symm lemma hom_equiv_apply_eq {A : C} {B : D} (f : F.obj A ⟶ B) (g : A ⟶ G.obj B) : adj.hom_equiv A B f = g ↔ f = (adj.hom_equiv A B).symm g := ⟨λ h, by {cases h, simp}, λ h, by {cases h, simp}⟩ lemma eq_hom_equiv_apply {A : C} {B : D} (f : F.obj A ⟶ B) (g : A ⟶ G.obj B) : g = adj.hom_equiv A B f ↔ (adj.hom_equiv A B).symm g = f := ⟨λ h, by {cases h, simp}, λ h, by {cases h, simp}⟩ end end adjunction namespace adjunction structure core_hom_equiv (F : C ⥤ D) (G : D ⥤ C) := (hom_equiv : Π (X Y), (F.obj X ⟶ Y) ≃ (X ⟶ G.obj Y)) (hom_equiv_naturality_left_symm' : Π {X' X Y} (f : X' ⟶ X) (g : X ⟶ G.obj Y), (hom_equiv X' Y).symm (f ≫ g) = F.map f ≫ (hom_equiv X Y).symm g . obviously) (hom_equiv_naturality_right' : Π {X Y Y'} (f : F.obj X ⟶ Y) (g : Y ⟶ Y'), (hom_equiv X Y') (f ≫ g) = (hom_equiv X Y) f ≫ G.map g . obviously) namespace core_hom_equiv restate_axiom hom_equiv_naturality_left_symm' restate_axiom hom_equiv_naturality_right' attribute [simp, priority 10] hom_equiv_naturality_left_symm hom_equiv_naturality_right variables {F : C ⥤ D} {G : D ⥤ C} (adj : core_hom_equiv F G) {X' X : C} {Y Y' : D} @[simp] lemma hom_equiv_naturality_left (f : X' ⟶ X) (g : F.obj X ⟶ Y) : (adj.hom_equiv X' Y) (F.map f ≫ g) = f ≫ (adj.hom_equiv X Y) g := by rw [← equiv.eq_symm_apply]; simp @[simp] lemma hom_equiv_naturality_right_symm (f : X ⟶ G.obj Y) (g : Y ⟶ Y') : (adj.hom_equiv X Y').symm (f ≫ G.map g) = (adj.hom_equiv X Y).symm f ≫ g := by rw [equiv.symm_apply_eq]; simp end core_hom_equiv structure core_unit_counit (F : C ⥤ D) (G : D ⥤ C) := (unit : 𝟭 C ⟶ F.comp G) (counit : G.comp F ⟶ 𝟭 D) (left_triangle' : whisker_right unit F ≫ (functor.associator F G F).hom ≫ whisker_left F counit = nat_trans.id (𝟭 C ⋙ F) . obviously) (right_triangle' : whisker_left G unit ≫ (functor.associator G F G).inv ≫ whisker_right counit G = nat_trans.id (G ⋙ 𝟭 C) . obviously) namespace core_unit_counit restate_axiom left_triangle' restate_axiom right_triangle' attribute [simp] left_triangle right_triangle end core_unit_counit variables {F : C ⥤ D} {G : D ⥤ C} def mk_of_hom_equiv (adj : core_hom_equiv F G) : F ⊣ G := { unit := { app := λ X, (adj.hom_equiv X (F.obj X)) (𝟙 (F.obj X)), naturality' := begin intros, erw [← adj.hom_equiv_naturality_left, ← adj.hom_equiv_naturality_right], dsimp, simp end }, counit := { app := λ Y, (adj.hom_equiv _ _).inv_fun (𝟙 (G.obj Y)), naturality' := begin intros, erw [← adj.hom_equiv_naturality_left_symm, ← adj.hom_equiv_naturality_right_symm], dsimp, simp end }, hom_equiv_unit' := λ X Y f, by erw [← adj.hom_equiv_naturality_right]; simp, hom_equiv_counit' := λ X Y f, by erw [← adj.hom_equiv_naturality_left_symm]; simp, .. adj } def mk_of_unit_counit (adj : core_unit_counit F G) : F ⊣ G := { hom_equiv := λ X Y, { to_fun := λ f, adj.unit.app X ≫ G.map f, inv_fun := λ g, F.map g ≫ adj.counit.app Y, left_inv := λ f, begin change F.map (_ ≫ _) ≫ _ = _, rw [F.map_comp, assoc, ←functor.comp_map, adj.counit.naturality, ←assoc], convert id_comp f, have t := congr_arg (λ t : nat_trans _ _, t.app _) adj.left_triangle, dsimp at t, simp only [id_comp] at t, exact t, end, right_inv := λ g, begin change _ ≫ G.map (_ ≫ _) = _, rw [G.map_comp, ←assoc, ←functor.comp_map, ←adj.unit.naturality, assoc], convert comp_id g, have t := congr_arg (λ t : nat_trans _ _, t.app _) adj.right_triangle, dsimp at t, simp only [id_comp] at t, exact t, end }, .. adj } def id : 𝟭 C ⊣ 𝟭 C := { hom_equiv := λ X Y, equiv.refl _, unit := 𝟙 _, counit := 𝟙 _ } /-- If F and G are naturally isomorphic functors, establish an equivalence of hom-sets. -/ def equiv_homset_left_of_nat_iso {F F' : C ⥤ D} (iso : F ≅ F') {X : C} {Y : D} : (F.obj X ⟶ Y) ≃ (F'.obj X ⟶ Y) := { to_fun := λ f, iso.inv.app _ ≫ f, inv_fun := λ g, iso.hom.app _ ≫ g, left_inv := λ f, by simp, right_inv := λ g, by simp } @[simp] lemma equiv_homset_left_of_nat_iso_apply {F F' : C ⥤ D} (iso : F ≅ F') {X : C} {Y : D} (f : F.obj X ⟶ Y) : (equiv_homset_left_of_nat_iso iso) f = iso.inv.app _ ≫ f := rfl @[simp] lemma equiv_homset_left_of_nat_iso_symm_apply {F F' : C ⥤ D} (iso : F ≅ F') {X : C} {Y : D} (g : F'.obj X ⟶ Y) : (equiv_homset_left_of_nat_iso iso).symm g = iso.hom.app _ ≫ g := rfl /-- If G and H are naturally isomorphic functors, establish an equivalence of hom-sets. -/ def equiv_homset_right_of_nat_iso {G G' : D ⥤ C} (iso : G ≅ G') {X : C} {Y : D} : (X ⟶ G.obj Y) ≃ (X ⟶ G'.obj Y) := { to_fun := λ f, f ≫ iso.hom.app _, inv_fun := λ g, g ≫ iso.inv.app _, left_inv := λ f, by simp, right_inv := λ g, by simp } @[simp] lemma equiv_homset_right_of_nat_iso_apply {G G' : D ⥤ C} (iso : G ≅ G') {X : C} {Y : D} (f : X ⟶ G.obj Y) : (equiv_homset_right_of_nat_iso iso) f = f ≫ iso.hom.app _ := rfl @[simp] lemma equiv_homset_right_of_nat_iso_symm_apply {G G' : D ⥤ C} (iso : G ≅ G') {X : C} {Y : D} (g : X ⟶ G'.obj Y) : (equiv_homset_right_of_nat_iso iso).symm g = g ≫ iso.inv.app _ := rfl /-- Transport an adjunction along an natural isomorphism on the left. -/ def of_nat_iso_left {F G : C ⥤ D} {H : D ⥤ C} (adj : F ⊣ H) (iso : F ≅ G) : G ⊣ H := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, (equiv_homset_left_of_nat_iso iso.symm).trans (adj.hom_equiv X Y) } /-- Transport an adjunction along an natural isomorphism on the right. -/ def of_nat_iso_right {F : C ⥤ D} {G H : D ⥤ C} (adj : F ⊣ G) (iso : G ≅ H) : F ⊣ H := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, (adj.hom_equiv X Y).trans (equiv_homset_right_of_nat_iso iso) } /-- Transport being a right adjoint along a natural isomorphism. -/ def right_adjoint_of_nat_iso {F G : C ⥤ D} (h : F ≅ G) [r : is_right_adjoint F] : is_right_adjoint G := { left := r.left, adj := of_nat_iso_right r.adj h } /-- Transport being a left adjoint along a natural isomorphism. -/ def left_adjoint_of_nat_iso {F G : C ⥤ D} (h : F ≅ G) [r : is_left_adjoint F] : is_left_adjoint G := { right := r.right, adj := of_nat_iso_left r.adj h } section variables {E : Type u₃} [ℰ : category.{v₃} E] (H : D ⥤ E) (I : E ⥤ D) /-- Show that adjunctions can be composed. -/ def comp (adj₁ : F ⊣ G) (adj₂ : H ⊣ I) : F ⋙ H ⊣ I ⋙ G := { hom_equiv := λ X Z, equiv.trans (adj₂.hom_equiv _ _) (adj₁.hom_equiv _ _), unit := adj₁.unit ≫ (whisker_left F $ whisker_right adj₂.unit G) ≫ (functor.associator _ _ _).inv, counit := (functor.associator _ _ _).hom ≫ (whisker_left I $ whisker_right adj₁.counit H) ≫ adj₂.counit } /-- If `F` and `G` are left adjoints then `F ⋙ G` is a left adjoint too. -/ instance left_adjoint_of_comp {E : Type u₃} [ℰ : category.{v₃} E] (F : C ⥤ D) (G : D ⥤ E) [Fl : is_left_adjoint F] [Gl : is_left_adjoint G] : is_left_adjoint (F ⋙ G) := { right := Gl.right ⋙ Fl.right, adj := comp _ _ Fl.adj Gl.adj } /-- If `F` and `G` are right adjoints then `F ⋙ G` is a right adjoint too. -/ instance right_adjoint_of_comp {E : Type u₃} [ℰ : category.{v₃} E] {F : C ⥤ D} {G : D ⥤ E} [Fr : is_right_adjoint F] [Gr : is_right_adjoint G] : is_right_adjoint (F ⋙ G) := { left := Gr.left ⋙ Fr.left, adj := comp _ _ Gr.adj Fr.adj } end section construct_left -- Construction of a left adjoint. In order to construct a left -- adjoint to a functor G : D → C, it suffices to give the object part -- of a functor F : C → D together with isomorphisms Hom(FX, Y) ≃ -- Hom(X, GY) natural in Y. The action of F on morphisms can be -- constructed from this data. variables {F_obj : C → D} {G} variables (e : Π X Y, (F_obj X ⟶ Y) ≃ (X ⟶ G.obj Y)) variables (he : Π X Y Y' g h, e X Y' (h ≫ g) = e X Y h ≫ G.map g) include he private lemma he' {X Y Y'} (f g) : (e X Y').symm (f ≫ G.map g) = (e X Y).symm f ≫ g := by intros; rw [equiv.symm_apply_eq, he]; simp def left_adjoint_of_equiv : C ⥤ D := { obj := F_obj, map := λ X X' f, (e X (F_obj X')).symm (f ≫ e X' (F_obj X') (𝟙 _)), map_comp' := λ X X' X'' f f', begin rw [equiv.symm_apply_eq, he, equiv.apply_symm_apply], conv { to_rhs, rw [assoc, ←he, id_comp, equiv.apply_symm_apply] }, simp end } def adjunction_of_equiv_left : left_adjoint_of_equiv e he ⊣ G := mk_of_hom_equiv { hom_equiv := e, hom_equiv_naturality_left_symm' := begin intros, erw [← he' e he, ← equiv.apply_eq_iff_eq], simp [(he _ _ _ _ _).symm] end } end construct_left section construct_right -- Construction of a right adjoint, analogous to the above. variables {F} {G_obj : D → C} variables (e : Π X Y, (F.obj X ⟶ Y) ≃ (X ⟶ G_obj Y)) variables (he : Π X' X Y f g, e X' Y (F.map f ≫ g) = f ≫ e X Y g) include he private lemma he' {X' X Y} (f g) : F.map f ≫ (e X Y).symm g = (e X' Y).symm (f ≫ g) := by intros; rw [equiv.eq_symm_apply, he]; simp def right_adjoint_of_equiv : D ⥤ C := { obj := G_obj, map := λ Y Y' g, (e (G_obj Y) Y') ((e (G_obj Y) Y).symm (𝟙 _) ≫ g), map_comp' := λ Y Y' Y'' g g', begin rw [← equiv.eq_symm_apply, ← he' e he, equiv.symm_apply_apply], conv { to_rhs, rw [← assoc, he' e he, comp_id, equiv.symm_apply_apply] }, simp end } def adjunction_of_equiv_right : F ⊣ right_adjoint_of_equiv e he := mk_of_hom_equiv { hom_equiv := e, hom_equiv_naturality_left_symm' := by intros; rw [equiv.symm_apply_eq, he]; simp, hom_equiv_naturality_right' := begin intros X Y Y' g h, erw [←he, equiv.apply_eq_iff_eq, ←assoc, he' e he, comp_id, equiv.symm_apply_apply] end } end construct_right end adjunction open adjunction namespace equivalence def to_adjunction (e : C ≌ D) : e.functor ⊣ e.inverse := mk_of_unit_counit ⟨e.unit, e.counit, by { ext, dsimp, simp only [id_comp], exact e.functor_unit_comp _, }, by { ext, dsimp, simp only [id_comp], exact e.unit_inverse_comp _, }⟩ end equivalence namespace functor /-- An equivalence `E` is left adjoint to its inverse. -/ def adjunction (E : C ⥤ D) [is_equivalence E] : E ⊣ E.inv := (E.as_equivalence).to_adjunction /-- If `F` is an equivalence, it's a left adjoint. -/ @[priority 10] instance left_adjoint_of_equivalence {F : C ⥤ D} [is_equivalence F] : is_left_adjoint F := { right := _, adj := functor.adjunction F } @[simp] lemma right_adjoint_of_is_equivalence {F : C ⥤ D} [is_equivalence F] : right_adjoint F = inv F := rfl /-- If `F` is an equivalence, it's a right adjoint. -/ @[priority 10] instance right_adjoint_of_equivalence {F : C ⥤ D} [is_equivalence F] : is_right_adjoint F := { left := _, adj := functor.adjunction F.inv } @[simp] lemma left_adjoint_of_is_equivalence {F : C ⥤ D} [is_equivalence F] : left_adjoint F = inv F := rfl end functor end category_theory
94a3c8b2bc3a0a2dadf7e5597b1b2fd118576867
26ac254ecb57ffcb886ff709cf018390161a9225
/src/topology/algebra/ring.lean
f45b5d510dbb9bdb77214365a29c85c6adb979b3
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
4,015
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl Theory of topological rings. -/ import topology.algebra.group import ring_theory.ideals open classical set filter topological_space open_locale classical section topological_ring universes u v w variables (α : Type u) [topological_space α] section prio set_option default_priority 100 -- see Note [default priority] /-- A topological semiring is a semiring where addition and multiplication are continuous. -/ class topological_semiring [semiring α] extends topological_add_monoid α, topological_monoid α : Prop end prio variables [ring α] section prio set_option default_priority 100 -- see Note [default priority] /-- A topological ring is a ring where the ring operations are continuous. -/ class topological_ring extends topological_add_monoid α, topological_monoid α : Prop := (continuous_neg : continuous (λa:α, -a)) end prio variables [t : topological_ring α] @[priority 100] -- see Note [lower instance priority] instance topological_ring.to_topological_semiring : topological_semiring α := {..t} @[priority 100] -- see Note [lower instance priority] instance topological_ring.to_topological_add_group : topological_add_group α := {..t} end topological_ring section topological_comm_ring variables {α : Type*} [topological_space α] [comm_ring α] [topological_ring α] def ideal.closure (S : ideal α) : ideal α := { carrier := closure S, zero_mem' := subset_closure S.zero_mem, add_mem' := assume x y hx hy, mem_closure2 continuous_add hx hy $ assume a b, S.add_mem, smul_mem' := assume c x hx, have continuous (λx:α, c * x) := continuous_const.mul continuous_id, mem_closure this hx $ assume a, S.mul_mem_left } @[simp] lemma ideal.coe_closure (S : ideal α) : (S.closure : set α) = closure S := rfl end topological_comm_ring section topological_ring variables {α : Type*} [topological_space α] [comm_ring α] (N : ideal α) open ideal.quotient instance topological_ring_quotient_topology : topological_space N.quotient := by dunfold ideal.quotient submodule.quotient; apply_instance lemma quotient_ring_saturate {α : Type*} [comm_ring α] (N : ideal α) (s : set α) : mk N ⁻¹' (mk N '' s) = (⋃ x : N, (λ y, x.1 + y) '' s) := begin ext x, simp only [mem_preimage, mem_image, mem_Union, ideal.quotient.eq], split, { exact assume ⟨a, a_in, h⟩, ⟨⟨_, N.neg_mem h⟩, a, a_in, by simp⟩ }, { exact assume ⟨⟨i, hi⟩, a, ha, eq⟩, ⟨a, ha, by rw [← eq, sub_add_eq_sub_sub_swap, sub_self, zero_sub]; exact N.neg_mem hi⟩ } end variable [topological_ring α] lemma quotient_ring.is_open_map_coe : is_open_map (mk N) := begin assume s s_op, show is_open (mk N ⁻¹' (mk N '' s)), rw quotient_ring_saturate N s, exact is_open_Union (assume ⟨n, _⟩, is_open_map_add_left n s s_op) end lemma quotient_ring.quotient_map_coe_coe : quotient_map (λ p : α × α, (mk N p.1, mk N p.2)) := begin apply is_open_map.to_quotient_map, { exact (quotient_ring.is_open_map_coe N).prod (quotient_ring.is_open_map_coe N) }, { exact (continuous_quot_mk.comp continuous_fst).prod_mk (continuous_quot_mk.comp continuous_snd) }, { rintro ⟨⟨x⟩, ⟨y⟩⟩, exact ⟨(x, y), rfl⟩ } end instance topological_ring_quotient : topological_ring N.quotient := { continuous_add := have cont : continuous (mk N ∘ (λ (p : α × α), p.fst + p.snd)) := continuous_quot_mk.comp continuous_add, (quotient_map.continuous_iff (quotient_ring.quotient_map_coe_coe N)).2 cont, continuous_neg := continuous_quotient_lift _ (continuous_quot_mk.comp continuous_neg), continuous_mul := have cont : continuous (mk N ∘ (λ (p : α × α), p.fst * p.snd)) := continuous_quot_mk.comp continuous_mul, (quotient_map.continuous_iff (quotient_ring.quotient_map_coe_coe N)).2 cont } end topological_ring
0b948e5979f164ab377cbe7c98470a9f40953be7
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/ind_bug.lean
65c42fc7f8d6a7d223de4e5362a0a40156b88e80
[ "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
205
lean
import logic constant N : Type.{1} constant I : Type.{1} namespace foo inductive p (a : N) : Prop := intro : p a end foo open foo namespace bla inductive p (a : I) : Prop := intro : p a end bla
754160fa6bf517b8acc169668a353f43d68664d9
8c02fed42525b65813b55c064afe2484758d6d09
/src/irtype.lean
ae4ee9840232dcb47ff1032accfd1ab587beca71
[ "LicenseRef-scancode-generic-cla", "MIT" ]
permissive
microsoft/AliveInLean
3eac351a34154efedd3ffc4fe2fa4ec01b219e0d
4b739dd6e4266b26a045613849df221374119871
refs/heads/master
1,691,419,737,939
1,689,365,567,000
1,689,365,568,000
131,156,103
23
18
NOASSERTION
1,660,342,040,000
1,524,747,538,000
Lean
UTF-8
Lean
false
false
1,274
lean
-- Copyright (c) Microsoft Corporation. All rights reserved. -- Licensed under the MIT license. import .lang -- Returns true if program p contains uninstantiated type. def has_arbitrary_type (p:program): bool := list.foldl (λ (b:bool) inst, bor b (match inst with | instruction.binop t lhs bop nswnuw op1 op2 := t = ty.arbitrary_int | instruction.unaryop r uop t1 op t2 := t1 = ty.arbitrary_int ∨ t2 = ty.arbitrary_int | instruction.icmpop t r cond r1 r2 := t = ty.arbitrary_int | instruction.selectop r t1 cond t2 op1 op2 := t1 = ty.arbitrary_int ∨ t2 = ty.arbitrary_int end)) ff p.insts -- Returns a list of programs with concretized type def concretize_type (p:program) : list program := if ¬ (has_arbitrary_type p) then [p] else let create_one := fun (tysz:nat), program.mk ((list.map (λ inst, match inst with | instruction.binop t lhs bop nswnuw op1 op2 := (instruction.binop (match t with | ty.arbitrary_int := ty.int tysz | _ := t end) lhs bop nswnuw op1 op2) -- has type instantiation. | _ := inst -- no type instantiation. end) (p.insts)):list instruction) in [create_one 8, create_one 16]
1a9d7354cab5b284db40294aff45188d2379e82b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/sheaves/presheaf_auto.lean
0e82d6430ba7c8a88e6bf4948cd58a3983e88ffc
[]
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,829
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Mario Carneiro, Reid Barton -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.category.Top.opens import Mathlib.PostPort universes v u namespace Mathlib /-! # Presheaves on a topological space We define `presheaf C X` simply as `(opens X)ᵒᵖ ⥤ C`, and inherit the category structure with natural transformations as morphisms. We define * `pushforward_obj {X Y : Top.{v}} (f : X ⟶ Y) (ℱ : X.presheaf C) : Y.presheaf C` with notation `f _* ℱ` and for `ℱ : X.presheaf C` provide the natural isomorphisms * `pushforward.id : (𝟙 X) _* ℱ ≅ ℱ`` * `pushforward.comp : (f ≫ g) _* ℱ ≅ g _* (f _* ℱ)` along with their `@[simp]` lemmas. -/ namespace Top def presheaf (C : Type u) [category_theory.category C] (X : Top) := topological_space.opens ↥Xᵒᵖ ⥤ C namespace presheaf /-- Pushforward a presheaf on `X` along a continuous map `f : X ⟶ Y`, obtaining a presheaf on `Y`. -/ def pushforward_obj {C : Type u} [category_theory.category C] {X : Top} {Y : Top} (f : X ⟶ Y) (ℱ : presheaf C X) : presheaf C Y := category_theory.functor.op (topological_space.opens.map f) ⋙ ℱ infixl:80 " _* " => Mathlib.Top.presheaf.pushforward_obj @[simp] theorem pushforward_obj_obj {C : Type u} [category_theory.category C] {X : Top} {Y : Top} (f : X ⟶ Y) (ℱ : presheaf C X) (U : topological_space.opens ↥Yᵒᵖ) : category_theory.functor.obj (f _* ℱ) U = category_theory.functor.obj ℱ (category_theory.functor.obj (category_theory.functor.op (topological_space.opens.map f)) U) := rfl @[simp] theorem pushforward_obj_map {C : Type u} [category_theory.category C] {X : Top} {Y : Top} (f : X ⟶ Y) (ℱ : presheaf C X) {U : topological_space.opens ↥Yᵒᵖ} {V : topological_space.opens ↥Yᵒᵖ} (i : U ⟶ V) : category_theory.functor.map (f _* ℱ) i = category_theory.functor.map ℱ (category_theory.functor.map (category_theory.functor.op (topological_space.opens.map f)) i) := rfl def pushforward_eq {C : Type u} [category_theory.category C] {X : Top} {Y : Top} {f : X ⟶ Y} {g : X ⟶ Y} (h : f = g) (ℱ : presheaf C X) : f _* ℱ ≅ g _* ℱ := category_theory.iso_whisker_right (category_theory.nat_iso.op (category_theory.iso.symm (topological_space.opens.map_iso f g h))) ℱ @[simp] theorem pushforward_eq_hom_app {C : Type u} [category_theory.category C] {X : Top} {Y : Top} {f : X ⟶ Y} {g : X ⟶ Y} (h : f = g) (ℱ : presheaf C X) (U : topological_space.opens ↥Yᵒᵖ) : category_theory.nat_trans.app (category_theory.iso.hom (pushforward_eq h ℱ)) U = category_theory.functor.map ℱ (id (category_theory.has_hom.hom.op (category_theory.eq_to_hom (eq.mpr (id (Eq._oldrec (Eq.refl (category_theory.functor.obj (topological_space.opens.map g) (opposite.unop U) = category_theory.functor.obj (topological_space.opens.map f) (opposite.unop U))) h)) (Eq.refl (category_theory.functor.obj (topological_space.opens.map g) (opposite.unop U))))))) := rfl @[simp] theorem pushforward_eq_rfl {C : Type u} [category_theory.category C] {X : Top} {Y : Top} (f : X ⟶ Y) (ℱ : presheaf C X) (U : topological_space.opens ↥Y) : category_theory.nat_trans.app (category_theory.iso.hom (pushforward_eq rfl ℱ)) (opposite.op U) = 𝟙 := sorry theorem pushforward_eq_eq {C : Type u} [category_theory.category C] {X : Top} {Y : Top} {f : X ⟶ Y} {g : X ⟶ Y} (h₁ : f = g) (h₂ : f = g) (ℱ : presheaf C X) : pushforward_eq h₁ ℱ = pushforward_eq h₂ ℱ := rfl namespace pushforward def id {C : Type u} [category_theory.category C] {X : Top} (ℱ : presheaf C X) : 𝟙 _* ℱ ≅ ℱ := category_theory.iso_whisker_right (category_theory.nat_iso.op (category_theory.iso.symm (topological_space.opens.map_id X))) ℱ ≪≫ category_theory.functor.left_unitor ℱ @[simp] theorem id_hom_app' {C : Type u} [category_theory.category C] {X : Top} (ℱ : presheaf C X) (U : set ↥X) (p : is_open U) : category_theory.nat_trans.app (category_theory.iso.hom (id ℱ)) (opposite.op { val := U, property := p }) = category_theory.functor.map ℱ 𝟙 := sorry @[simp] theorem id_hom_app {C : Type u} [category_theory.category C] {X : Top} (ℱ : presheaf C X) (U : topological_space.opens ↥Xᵒᵖ) : category_theory.nat_trans.app (category_theory.iso.hom (id ℱ)) U = category_theory.functor.map ℱ (category_theory.eq_to_hom (topological_space.opens.op_map_id_obj U)) := sorry @[simp] theorem id_inv_app' {C : Type u} [category_theory.category C] {X : Top} (ℱ : presheaf C X) (U : set ↥X) (p : is_open U) : category_theory.nat_trans.app (category_theory.iso.inv (id ℱ)) (opposite.op { val := U, property := p }) = category_theory.functor.map ℱ 𝟙 := sorry def comp {C : Type u} [category_theory.category C] {X : Top} (ℱ : presheaf C X) {Y : Top} {Z : Top} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g) _* ℱ ≅ g _* (f _* ℱ) := category_theory.iso_whisker_right (category_theory.nat_iso.op (category_theory.iso.symm (topological_space.opens.map_comp f g))) ℱ @[simp] theorem comp_hom_app {C : Type u} [category_theory.category C] {X : Top} (ℱ : presheaf C X) {Y : Top} {Z : Top} (f : X ⟶ Y) (g : Y ⟶ Z) (U : topological_space.opens ↥Zᵒᵖ) : category_theory.nat_trans.app (category_theory.iso.hom (comp ℱ f g)) U = 𝟙 := sorry @[simp] theorem comp_inv_app {C : Type u} [category_theory.category C] {X : Top} (ℱ : presheaf C X) {Y : Top} {Z : Top} (f : X ⟶ Y) (g : Y ⟶ Z) (U : topological_space.opens ↥Zᵒᵖ) : category_theory.nat_trans.app (category_theory.iso.inv (comp ℱ f g)) U = 𝟙 := sorry end pushforward /-- A morphism of presheaves gives rise to a morphisms of the pushforwards of those presheaves. -/ def pushforward_map {C : Type u} [category_theory.category C] {X : Top} {Y : Top} (f : X ⟶ Y) {ℱ : presheaf C X} {𝒢 : presheaf C X} (α : ℱ ⟶ 𝒢) : f _* ℱ ⟶ f _* 𝒢 := category_theory.nat_trans.mk fun (U : topological_space.opens ↥Yᵒᵖ) => category_theory.nat_trans.app α (category_theory.functor.obj (category_theory.functor.op (topological_space.opens.map f)) U) end Mathlib
c8f751821744d66c84857abbd9b8be61fddb7ae9
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/sheaves/presheaf.lean
1a39ca3fc7a89c96f65adf30b336fca1264d83b4
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
15,200
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Mario Carneiro, Reid Barton, Andrew Yang -/ import category_theory.limits.kan_extension import topology.category.Top.opens import category_theory.adjunction.opposites /-! # Presheaves on a topological space We define `presheaf C X` simply as `(opens X)ᵒᵖ ⥤ C`, and inherit the category structure with natural transformations as morphisms. We define * `pushforward_obj {X Y : Top.{w}} (f : X ⟶ Y) (ℱ : X.presheaf C) : Y.presheaf C` with notation `f _* ℱ` and for `ℱ : X.presheaf C` provide the natural isomorphisms * `pushforward.id : (𝟙 X) _* ℱ ≅ ℱ` * `pushforward.comp : (f ≫ g) _* ℱ ≅ g _* (f _* ℱ)` along with their `@[simp]` lemmas. We also define the functors `pushforward` and `pullback` between the categories `X.presheaf C` and `Y.presheaf C`, and provide their adjunction at `pushforward_pullback_adjunction`. -/ universes w v u open category_theory open topological_space open opposite variables (C : Type u) [category.{v} C] namespace Top /-- The category of `C`-valued presheaves on a (bundled) topological space `X`. -/ @[derive category, nolint has_nonempty_instance] def presheaf (X : Top.{w}) : Type (max u v w) := (opens X)ᵒᵖ ⥤ C variables {C} namespace presheaf local attribute [instance] concrete_category.has_coe_to_sort concrete_category.has_coe_to_fun /-- Tag lemmas to use in `Top.presheaf.restrict_tac`. -/ @[user_attribute] meta def restrict_attr : user_attribute (tactic unit → tactic unit) unit := { name := `sheaf_restrict, descr := "tag lemmas to use in `Top.presheaf.restrict_tac`", cache_cfg := { mk_cache := λ ns, pure $ λ t, do { ctx <- tactic.local_context, ctx.any_of (tactic.focus1 ∘ (tactic.apply' >=> (λ _, tactic.done)) >=> (λ _, t)) <|> ns.any_of (tactic.focus1 ∘ (tactic.resolve_name >=> tactic.to_expr >=> tactic.apply' >=> (λ _, tactic.done)) >=> (λ _, t)) }, dependencies := [] } } /-- A tactic to discharge goals of type `U ≤ V` for `Top.presheaf.restrict_open` -/ meta def restrict_tac : Π (n : ℕ), tactic unit | 0 := tactic.fail "`restrict_tac` failed" | (n + 1) := monad.join (restrict_attr.get_cache <*> pure tactic.done) <|> `[apply' le_trans, mjoin (restrict_attr.get_cache <*> pure (restrict_tac n))] /-- A tactic to discharge goals of type `U ≤ V` for `Top.presheaf.restrict_open`. Defaults to three iterations. -/ meta def restrict_tac' := restrict_tac 3 attribute [sheaf_restrict] bot_le le_top le_refl inf_le_left inf_le_right le_sup_left le_sup_right example {X : Top} {v w x y z : opens X} (h₀ : v ≤ x) (h₁ : x ≤ z ⊓ w) (h₂ : x ≤ y ⊓ z) : v ≤ y := by restrict_tac' /-- The restriction of a section along an inclusion of open sets. For `x : F.obj (op V)`, we provide the notation `x |_ₕ i` (`h` stands for `hom`) for `i : U ⟶ V`, and the notation `x |_ₗ U ⟪i⟫` (`l` stands for `le`) for `i : U ≤ V`. -/ def restrict {X : Top} {C : Type*} [category C] [concrete_category C] {F : X.presheaf C} {V : opens X} (x : F.obj (op V)) {U : opens X} (h : U ⟶ V) : F.obj (op U) := F.map h.op x localized "infixl ` |_ₕ `: 80 := Top.presheaf.restrict" in algebraic_geometry localized "notation x ` |_ₗ `: 80 U ` ⟪` e `⟫ ` := @Top.presheaf.restrict _ _ _ _ _ _ x U (@hom_of_le (opens _) _ U _ e)" in algebraic_geometry /-- The restriction of a section along an inclusion of open sets. For `x : F.obj (op V)`, we provide the notation `x |_ U`, where the proof `U ≤ V` is inferred by the tactic `Top.presheaf.restrict_tac'` -/ abbreviation restrict_open {X : Top} {C : Type*} [category C] [concrete_category C] {F : X.presheaf C} {V : opens X} (x : F.obj (op V)) (U : opens X) (e : U ≤ V . Top.presheaf.restrict_tac') : F.obj (op U) := x |_ₗ U ⟪e⟫ localized "infixl ` |_ `: 80 := Top.presheaf.restrict_open" in algebraic_geometry @[simp] lemma restrict_restrict {X : Top} {C : Type*} [category C] [concrete_category C] {F : X.presheaf C} {U V W : opens X} (e₁ : U ≤ V) (e₂ : V ≤ W) (x : F.obj (op W)) : x |_ V |_ U = x |_ U := by { delta restrict_open restrict, rw [← comp_apply, ← functor.map_comp], refl } @[simp] lemma map_restrict {X : Top} {C : Type*} [category C] [concrete_category C] {F G : X.presheaf C} (e : F ⟶ G) {U V : opens X} (h : U ≤ V) (x : F.obj (op V)) : e.app _ (x |_ U) = (e.app _ x) |_ U := by { delta restrict_open restrict, rw [← comp_apply, nat_trans.naturality, comp_apply] } /-- Pushforward a presheaf on `X` along a continuous map `f : X ⟶ Y`, obtaining a presheaf on `Y`. -/ def pushforward_obj {X Y : Top.{w}} (f : X ⟶ Y) (ℱ : X.presheaf C) : Y.presheaf C := (opens.map f).op ⋙ ℱ infix ` _* `: 80 := pushforward_obj @[simp] lemma pushforward_obj_obj {X Y : Top.{w}} (f : X ⟶ Y) (ℱ : X.presheaf C) (U : (opens Y)ᵒᵖ) : (f _* ℱ).obj U = ℱ.obj ((opens.map f).op.obj U) := rfl @[simp] lemma pushforward_obj_map {X Y : Top.{w}} (f : X ⟶ Y) (ℱ : X.presheaf C) {U V : (opens Y)ᵒᵖ} (i : U ⟶ V) : (f _* ℱ).map i = ℱ.map ((opens.map f).op.map i) := rfl /-- An equality of continuous maps induces a natural isomorphism between the pushforwards of a presheaf along those maps. -/ def pushforward_eq {X Y : Top.{w}} {f g : X ⟶ Y} (h : f = g) (ℱ : X.presheaf C) : f _* ℱ ≅ g _* ℱ := iso_whisker_right (nat_iso.op (opens.map_iso f g h).symm) ℱ lemma pushforward_eq' {X Y : Top.{w}} {f g : X ⟶ Y} (h : f = g) (ℱ : X.presheaf C) : f _* ℱ = g _* ℱ := by rw h @[simp] lemma pushforward_eq_hom_app {X Y : Top.{w}} {f g : X ⟶ Y} (h : f = g) (ℱ : X.presheaf C) (U) : (pushforward_eq h ℱ).hom.app U = ℱ.map (begin dsimp [functor.op], apply quiver.hom.op, apply eq_to_hom, rw h, end) := by simp [pushforward_eq] lemma pushforward_eq'_hom_app {X Y : Top.{w}} {f g : X ⟶ Y} (h : f = g) (ℱ : X.presheaf C) (U) : nat_trans.app (eq_to_hom (pushforward_eq' h ℱ)) U = ℱ.map (eq_to_hom (by rw h)) := by simpa [eq_to_hom_map] @[simp] lemma pushforward_eq_rfl {X Y : Top.{w}} (f : X ⟶ Y) (ℱ : X.presheaf C) (U) : (pushforward_eq (rfl : f = f) ℱ).hom.app (op U) = 𝟙 _ := begin dsimp [pushforward_eq], simp, end lemma pushforward_eq_eq {X Y : Top.{w}} {f g : X ⟶ Y} (h₁ h₂ : f = g) (ℱ : X.presheaf C) : ℱ.pushforward_eq h₁ = ℱ.pushforward_eq h₂ := rfl namespace pushforward variables {X : Top.{w}} (ℱ : X.presheaf C) /-- The natural isomorphism between the pushforward of a presheaf along the identity continuous map and the original presheaf. -/ def id : (𝟙 X) _* ℱ ≅ ℱ := (iso_whisker_right (nat_iso.op (opens.map_id X).symm) ℱ) ≪≫ functor.left_unitor _ lemma id_eq : (𝟙 X) _* ℱ = ℱ := by { unfold pushforward_obj, rw opens.map_id_eq, erw functor.id_comp } @[simp] lemma id_hom_app' (U) (p) : (id ℱ).hom.app (op ⟨U, p⟩) = ℱ.map (𝟙 (op ⟨U, p⟩)) := by { dsimp [id], simp, } local attribute [tidy] tactic.op_induction' @[simp, priority 990] lemma id_hom_app (U) : (id ℱ).hom.app U = ℱ.map (eq_to_hom (opens.op_map_id_obj U)) := by tidy @[simp] lemma id_inv_app' (U) (p) : (id ℱ).inv.app (op ⟨U, p⟩) = ℱ.map (𝟙 (op ⟨U, p⟩)) := by { dsimp [id], simp, } /-- The natural isomorphism between the pushforward of a presheaf along the composition of two continuous maps and the corresponding pushforward of a pushforward. -/ def comp {Y Z : Top.{w}} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g) _* ℱ ≅ g _* (f _* ℱ) := iso_whisker_right (nat_iso.op (opens.map_comp f g).symm) ℱ lemma comp_eq {Y Z : Top.{w}} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g) _* ℱ = g _* (f _* ℱ) := rfl @[simp] lemma comp_hom_app {Y Z : Top.{w}} (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (comp ℱ f g).hom.app U = 𝟙 _ := by { dsimp [comp], tidy, } @[simp] lemma comp_inv_app {Y Z : Top.{w}} (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (comp ℱ f g).inv.app U = 𝟙 _ := by { dsimp [comp], tidy, } end pushforward /-- A morphism of presheaves gives rise to a morphisms of the pushforwards of those presheaves. -/ @[simps] def pushforward_map {X Y : Top.{w}} (f : X ⟶ Y) {ℱ 𝒢 : X.presheaf C} (α : ℱ ⟶ 𝒢) : f _* ℱ ⟶ f _* 𝒢 := { app := λ U, α.app _, naturality' := λ U V i, by { erw α.naturality, refl, } } open category_theory.limits section pullback variable [has_colimits C] noncomputable theory /-- Pullback a presheaf on `Y` along a continuous map `f : X ⟶ Y`, obtaining a presheaf on `X`. This is defined in terms of left Kan extensions, which is just a fancy way of saying "take the colimits over the open sets whose preimage contains U". -/ @[simps] def pullback_obj {X Y : Top.{v}} (f : X ⟶ Y) (ℱ : Y.presheaf C) : X.presheaf C := (Lan (opens.map f).op).obj ℱ /-- Pulling back along continuous maps is functorial. -/ def pullback_map {X Y : Top.{v}} (f : X ⟶ Y) {ℱ 𝒢 : Y.presheaf C} (α : ℱ ⟶ 𝒢) : pullback_obj f ℱ ⟶ pullback_obj f 𝒢 := (Lan (opens.map f).op).map α /-- If `f '' U` is open, then `f⁻¹ℱ U ≅ ℱ (f '' U)`. -/ @[simps] def pullback_obj_obj_of_image_open {X Y : Top.{v}} (f : X ⟶ Y) (ℱ : Y.presheaf C) (U : opens X) (H : is_open (f '' U)) : (pullback_obj f ℱ).obj (op U) ≅ ℱ.obj (op ⟨_, H⟩) := begin let x : costructured_arrow (opens.map f).op (op U) := { left := op ⟨f '' U, H⟩, hom := ((@hom_of_le _ _ _ ((opens.map f).obj ⟨_, H⟩) (set.image_preimage.le_u_l _)).op : op ((opens.map f).obj (⟨⇑f '' ↑U, H⟩)) ⟶ op U) }, have hx : is_terminal x := { lift := λ s, begin fapply costructured_arrow.hom_mk, change op (unop _) ⟶ op (⟨_, H⟩ : opens _), refine (hom_of_le _).op, exact (set.image_subset f s.X.hom.unop.le).trans (set.image_preimage.l_u_le ↑(unop s.X.left)), simp end }, exact is_colimit.cocone_point_unique_up_to_iso (colimit.is_colimit _) (colimit_of_diagram_terminal hx _), end namespace pullback variables {X Y : Top.{v}} (ℱ : Y.presheaf C) /-- The pullback along the identity is isomorphic to the original presheaf. -/ def id : pullback_obj (𝟙 _) ℱ ≅ ℱ := nat_iso.of_components (λ U, pullback_obj_obj_of_image_open (𝟙 _) ℱ (unop U) (by simpa using U.unop.2) ≪≫ ℱ.map_iso (eq_to_iso (by simp))) (λ U V i, begin ext, simp, erw colimit.pre_desc_assoc, erw colimit.ι_desc_assoc, erw colimit.ι_desc_assoc, dsimp, simp only [←ℱ.map_comp], congr end) lemma id_inv_app (U : opens Y) : (id ℱ).inv.app (op U) = colimit.ι (Lan.diagram (opens.map (𝟙 Y)).op ℱ (op U)) (@costructured_arrow.mk _ _ _ _ _ (op U) _ (eq_to_hom (by simp))) := begin rw [← category.id_comp ((id ℱ).inv.app (op U)), ← nat_iso.app_inv, iso.comp_inv_eq], dsimp [id], rw colimit.ι_desc_assoc, dsimp, rw [← ℱ.map_comp, ← ℱ.map_id], refl, end end pullback end pullback variable (C) /-- The pushforward functor. -/ def pushforward {X Y : Top.{w}} (f : X ⟶ Y) : X.presheaf C ⥤ Y.presheaf C := { obj := pushforward_obj f, map := @pushforward_map _ _ X Y f } @[simp] lemma pushforward_map_app' {X Y : Top.{w}} (f : X ⟶ Y) {ℱ 𝒢 : X.presheaf C} (α : ℱ ⟶ 𝒢) {U : (opens Y)ᵒᵖ} : ((pushforward C f).map α).app U = α.app (op $ (opens.map f).obj U.unop) := rfl lemma id_pushforward {X : Top.{w}} : pushforward C (𝟙 X) = 𝟭 (X.presheaf C) := begin apply category_theory.functor.ext, { intros, ext U, have h := f.congr, erw h (opens.op_map_id_obj U), simpa [eq_to_hom_map], }, { intros, apply pushforward.id_eq }, end section iso /-- A homeomorphism of spaces gives an equivalence of categories of presheaves. -/ @[simps] def presheaf_equiv_of_iso {X Y : Top} (H : X ≅ Y) : X.presheaf C ≌ Y.presheaf C := equivalence.congr_left (opens.map_map_iso H).symm.op variable {C} /-- If `H : X ≅ Y` is a homeomorphism, then given an `H _* ℱ ⟶ 𝒢`, we may obtain an `ℱ ⟶ H ⁻¹ _* 𝒢`. -/ def to_pushforward_of_iso {X Y : Top} (H : X ≅ Y) {ℱ : X.presheaf C} {𝒢 : Y.presheaf C} (α : H.hom _* ℱ ⟶ 𝒢) : ℱ ⟶ H.inv _* 𝒢 := (presheaf_equiv_of_iso _ H).to_adjunction.hom_equiv ℱ 𝒢 α @[simp] lemma to_pushforward_of_iso_app {X Y : Top} (H₁ : X ≅ Y) {ℱ : X.presheaf C} {𝒢 : Y.presheaf C} (H₂ : H₁.hom _* ℱ ⟶ 𝒢) (U : (opens X)ᵒᵖ) : (to_pushforward_of_iso H₁ H₂).app U = ℱ.map (eq_to_hom (by simp [opens.map, set.preimage_preimage])) ≫ H₂.app (op ((opens.map H₁.inv).obj (unop U))) := begin delta to_pushforward_of_iso, simp only [equiv.to_fun_as_coe, nat_trans.comp_app, equivalence.equivalence_mk'_unit, eq_to_hom_map, eq_to_hom_op, eq_to_hom_trans, presheaf_equiv_of_iso_unit_iso_hom_app_app, equivalence.to_adjunction, equivalence.equivalence_mk'_counit, presheaf_equiv_of_iso_inverse_map_app, adjunction.mk_of_unit_counit_hom_equiv_apply], congr, end /-- If `H : X ≅ Y` is a homeomorphism, then given an `H _* ℱ ⟶ 𝒢`, we may obtain an `ℱ ⟶ H ⁻¹ _* 𝒢`. -/ def pushforward_to_of_iso {X Y : Top} (H₁ : X ≅ Y) {ℱ : Y.presheaf C} {𝒢 : X.presheaf C} (H₂ : ℱ ⟶ H₁.hom _* 𝒢) : H₁.inv _* ℱ ⟶ 𝒢 := ((presheaf_equiv_of_iso _ H₁.symm).to_adjunction.hom_equiv ℱ 𝒢).symm H₂ @[simp] lemma pushforward_to_of_iso_app {X Y : Top} (H₁ : X ≅ Y) {ℱ : Y.presheaf C} {𝒢 : X.presheaf C} (H₂ : ℱ ⟶ H₁.hom _* 𝒢) (U : (opens X)ᵒᵖ) : (pushforward_to_of_iso H₁ H₂).app U = H₂.app (op ((opens.map H₁.inv).obj (unop U))) ≫ 𝒢.map (eq_to_hom (by simp [opens.map, set.preimage_preimage])) := by simpa [pushforward_to_of_iso, equivalence.to_adjunction] end iso variables (C) [has_colimits C] /-- Pullback a presheaf on `Y` along a continuous map `f : X ⟶ Y`, obtaining a presheaf on `X`. -/ @[simps map_app] def pullback {X Y : Top.{v}} (f : X ⟶ Y) : Y.presheaf C ⥤ X.presheaf C := Lan (opens.map f).op @[simp] lemma pullback_obj_eq_pullback_obj {C} [category C] [has_colimits C] {X Y : Top.{w}} (f : X ⟶ Y) (ℱ : Y.presheaf C) : (pullback C f).obj ℱ = pullback_obj f ℱ := rfl /-- The pullback and pushforward along a continuous map are adjoint to each other. -/ @[simps unit_app_app counit_app_app] def pushforward_pullback_adjunction {X Y : Top.{v}} (f : X ⟶ Y) : pullback C f ⊣ pushforward C f := Lan.adjunction _ _ /-- Pulling back along a homeomorphism is the same as pushing forward along its inverse. -/ def pullback_hom_iso_pushforward_inv {X Y : Top.{v}} (H : X ≅ Y) : pullback C H.hom ≅ pushforward C H.inv := adjunction.left_adjoint_uniq (pushforward_pullback_adjunction C H.hom) (presheaf_equiv_of_iso C H.symm).to_adjunction /-- Pulling back along the inverse of a homeomorphism is the same as pushing forward along it. -/ def pullback_inv_iso_pushforward_hom {X Y : Top.{v}} (H : X ≅ Y) : pullback C H.inv ≅ pushforward C H.hom := adjunction.left_adjoint_uniq (pushforward_pullback_adjunction C H.inv) (presheaf_equiv_of_iso C H).to_adjunction end presheaf end Top
60b2ba0abdbf2d767f79f57d1ac30adaee04a64b
3dd1b66af77106badae6edb1c4dea91a146ead30
/library/standard/set.lean
0cc819a17f4b006228a0fb8e0ab038b837ad562f
[ "Apache-2.0" ]
permissive
silky/lean
79c20c15c93feef47bb659a2cc139b26f3614642
df8b88dca2f8da1a422cb618cd476ef5be730546
refs/heads/master
1,610,737,587,697
1,406,574,534,000
1,406,574,534,000
22,362,176
1
0
null
null
null
null
UTF-8
Lean
false
false
1,934
lean
--- Copyright (c) 2014 Jeremy Avigad. All rights reserved. --- Released under Apache 2.0 license as described in the file LICENSE. --- Author: Jeremy Avigad, Leonardo de Moura import logic funext bool using eq_proofs bool namespace set definition set (T : Type) := T → bool definition mem {T : Type} (x : T) (s : set T) := (s x) = '1 infix `∈`:50 := mem section parameter {T : Type} definition empty : set T := λx, '0 notation `∅`:max := empty theorem mem_empty (x : T) : ¬ (x ∈ ∅) := assume H : x ∈ ∅, absurd H b0_ne_b1 definition univ : set T := λx, '1 theorem mem_univ (x : T) : x ∈ univ := refl _ definition inter (A B : set T) : set T := λx, A x && B x infixl `∩`:70 := inter theorem mem_inter (x : T) (A B : set T) : x ∈ A ∩ B ↔ (x ∈ A ∧ x ∈ B) := iff_intro (assume H, and_intro (band_eq_b1_elim_left H) (band_eq_b1_elim_right H)) (assume H, have e1 : A x = '1, from and_elim_left H, have e2 : B x = '1, from and_elim_right H, show A x && B x = '1, from e1⁻¹ ▸ e2⁻¹ ▸ band_b1_left '1) theorem inter_comm (A B : set T) : A ∩ B = B ∩ A := funext (λx, band_comm (A x) (B x)) theorem inter_assoc (A B C : set T) : (A ∩ B) ∩ C = A ∩ (B ∩ C) := funext (λx, band_assoc (A x) (B x) (C x)) definition union (A B : set T) : set T := λx, A x || B x infixl `∪`:65 := union theorem mem_union (x : T) (A B : set T) : x ∈ A ∪ B ↔ (x ∈ A ∨ x ∈ B) := iff_intro (assume H, bor_to_or H) (assume H, or_elim H (assume Ha : A x = '1, show A x || B x = '1, from Ha⁻¹ ▸ bor_b1_left (B x)) (assume Hb : B x = '1, show A x || B x = '1, from Hb⁻¹ ▸ bor_b1_right (A x))) theorem union_comm (A B : set T) : A ∪ B = B ∪ A := funext (λx, bor_comm (A x) (B x)) theorem union_assoc (A B C : set T) : (A ∪ B) ∪ C = A ∪ (B ∪ C) := funext (λx, bor_assoc (A x) (B x) (C x)) end end
6dfd3970ef9c1a579bc3ee8d74b807354ff22dac
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/eval_attr_cache.lean
0ecd8a5fd2e6a809726dd3a2ceee26d4e1e6cde5
[ "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
716
lean
open tactic @[user_attribute] meta def my_attr : user_attribute (name → bool) := { name := "my_attr", descr := "my attr", cache_cfg := { mk_cache := λ ls, do { let c := `(λ n : name, (name.cases_on n ff (λ _ _, to_bool (n ∈ ls)) (λ _ _, ff) : bool)), eval_expr (name → bool) c }, dependencies := []} } meta def my_tac : tactic unit := do f ← my_attr.get_cache, trace (f `foo), return () @[my_attr] def bla := 10 run_cmd my_tac @[my_attr] def foo := 10 -- Cache was invalided run_cmd my_tac -- Add closure to the cache containing auxiliary function created by eval_expr run_cmd my_tac -- Cache should be flushed since the auxiliary function is gone
ef8bbe102d17394c4f72caefd42ecb924971b8f9
b32d3853770e6eaf06817a1b8c52064baaed0ef1
/src/super/distinct.lean
6131ed279520ab809ecfa03b2a541942e5c69535
[]
no_license
gebner/super2
4d58b7477b6f7d945d5d866502982466db33ab0b
9bc5256c31750021ab97d6b59b7387773e54b384
refs/heads/master
1,635,021,682,021
1,634,886,326,000
1,634,886,326,000
225,600,688
4
2
null
1,598,209,306,000
1,575,371,550,000
Lean
UTF-8
Lean
false
false
2,884
lean
import super.resolve data.list.alist namespace super namespace clause_type meta def has_dups (t : clause_type) : bool := t.literals.has_dups meta def is_or_ff_clean : clause_type → bool | ff := tt | (disj _ ff _) := bool.ff | (disj _ _ ff) := bool.ff | (disj _ a b) := a.is_or_ff_clean ∧ b.is_or_ff_clean | (imp _ a) := a.is_or_ff_clean | (atom _) := tt end clause_type namespace clause meta def is_or_ff_clean (c : clause) : bool := c.ty.is_or_ff_clean meta def has_dups (c : clause) : bool := c.ty.has_dups open tactic /- meta def mk_elim_cases (e : expr) (c_true c_false : expr) : tactic expr := do (expr.pi _ _ _ motive) ← infer_type c_true | fail "mk_elim_cases", when motive.has_var (fail "mk_elim_cases: dependent elim"), ip ← band <$> is_prop e <*> is_prop motive, if ip then do mk_mapp ``classical.by_cases [e, motive, c_true, c_false] else do s ← mk_mapp ``classical.type_decidable [e], st ← infer_type s, mk_mapp ``psum.rec [none, none, some (expr.lam `_ binder_info.default st motive), c_true, c_false, s] -/ private meta def distinct_core : clause → alist (λ _ : literal, expr) → tactic clause | c@⟨clause_type.ff, _⟩ _ := pure c | ⟨clause_type.imp a b, prf⟩ ctx := match ctx.lookup (literal.neg a) with | some h := distinct_core ⟨b, prf.app' h⟩ ctx | none := do h ← mk_local_def a.hyp_name_hint a, let ctx := ctx.insert (literal.neg a) h, ⟨b', prfb'⟩ ← distinct_core ⟨b, prf.app' h⟩ ctx, pure ⟨clause_type.imp a b', expr.mk_lambda h prfb'⟩ end | c@⟨clause_type.atom a, prf⟩ ctx := match ctx.lookup (literal.pos a) with | some h := pure ⟨clause_type.ff, h.app' prf⟩ | none := pure c end | c@⟨clause_type.disj _ a b, prf⟩ ctx := do let dups := (a.literals ∩ b.literals) \ ctx.keys, match dups with | (literal.pos l :: _) := do l_ip ← is_prop l, clnl : clause ← (if l_ip then do prf ← mk_mapp ``or_not [l], pure (⟨clause_type.disj tt (clause_type.atom l) (clause_type.imp l clause_type.ff), prf⟩ : clause) else do prf ← mk_mapp ``classical.type_decidable [l], pure (⟨clause_type.disj ff (clause_type.atom l) (clause_type.imp l clause_type.ff), prf⟩ : clause)), clnl.check_if_debug, or_congr clnl (λ cl, pure cl) (λ cnl, distinct_core c (ctx.insert (literal.pos l) cnl.prf)) | (literal.neg l :: _) := do hl ← mk_local_def l.hyp_name_hint l, ⟨ab', prf'⟩ ← distinct_core c (ctx.insert (literal.neg l) hl), pure ⟨clause_type.imp l ab', expr.mk_lambda hl prf'⟩ | [] := or_congr c (λ ca, distinct_core ca ctx) (λ cb, distinct_core cb ctx) end meta def distinct (c : clause) : tactic clause := if c.is_or_ff_clean ∧ ¬ c.has_dups then pure c else clause.check_result_if_debug $ distinct_core c ∅ end clause end super
35d0793c36c3a0b3c96dece98aa665833eefaf09
acc85b4be2c618b11fc7cb3005521ae6858a8d07
/data/subtype.lean
1f5461d47f8385fba2703d0616fbc7e5af31f0a0
[ "Apache-2.0" ]
permissive
linpingchuan/mathlib
d49990b236574df2a45d9919ba43c923f693d341
5ad8020f67eb13896a41cc7691d072c9331b1f76
refs/heads/master
1,626,019,377,808
1,508,048,784,000
1,508,048,784,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
594
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 Quotients -- extends the core library -/ variables {α : Type*} {p : α → Prop} {q : {a // p a} → Prop} lemma forall_subtype : (∀x:{a // p a}, q x) ↔ (∀a (h : p a), q ⟨a, h⟩) := ⟨assume h a ha, h ⟨_, _⟩, assume h ⟨a, ha⟩, h _ _⟩ theorem exists_subtype : (∃x : subtype p, q x) ↔ (∃x (h : p x), q ⟨x, h⟩) := ⟨assume ⟨⟨x, h⟩, h'⟩, ⟨x, h, h'⟩, assume ⟨x, h, h'⟩, ⟨⟨x, h⟩, h'⟩⟩
9f07d383b161713544333badbf583d70939935d9
9dc8cecdf3c4634764a18254e94d43da07142918
/src/order/monotone.lean
a57bbebf9bc9d5b5e9190c0f9034f94b8a038f56
[ "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
35,050
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Mario Carneiro, Yaël Dillies -/ import order.compare import order.max import order.rel_classes /-! # Monotonicity This file defines (strictly) monotone/antitone functions. Contrary to standard mathematical usage, "monotone"/"mono" here means "increasing", not "increasing or decreasing". We use "antitone"/"anti" to mean "decreasing". ## Definitions * `monotone f`: A function `f` between two preorders is monotone if `a ≤ b` implies `f a ≤ f b`. * `antitone f`: A function `f` between two preorders is antitone if `a ≤ b` implies `f b ≤ f a`. * `monotone_on f s`: Same as `monotone f`, but for all `a, b ∈ s`. * `antitone_on f s`: Same as `antitone f`, but for all `a, b ∈ s`. * `strict_mono f` : A function `f` between two preorders is strictly monotone if `a < b` implies `f a < f b`. * `strict_anti f` : A function `f` between two preorders is strictly antitone if `a < b` implies `f b < f a`. * `strict_mono_on f s`: Same as `strict_mono f`, but for all `a, b ∈ s`. * `strict_anti_on f s`: Same as `strict_anti f`, but for all `a, b ∈ s`. ## Main theorems * `monotone_nat_of_le_succ`, `monotone_int_of_le_succ`: If `f : ℕ → α` or `f : ℤ → α` and `f n ≤ f (n + 1)` for all `n`, then `f` is monotone. * `antitone_nat_of_succ_le`, `antitone_int_of_succ_le`: If `f : ℕ → α` or `f : ℤ → α` and `f (n + 1) ≤ f n` for all `n`, then `f` is antitone. * `strict_mono_nat_of_lt_succ`, `strict_mono_int_of_lt_succ`: If `f : ℕ → α` or `f : ℤ → α` and `f n < f (n + 1)` for all `n`, then `f` is strictly monotone. * `strict_anti_nat_of_succ_lt`, `strict_anti_int_of_succ_lt`: If `f : ℕ → α` or `f : ℤ → α` and `f (n + 1) < f n` for all `n`, then `f` is strictly antitone. ## Implementation notes Some of these definitions used to only require `has_le α` or `has_lt α`. The advantage of this is unclear and it led to slight elaboration issues. Now, everything requires `preorder α` and seems to work fine. Related Zulip discussion: https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/Order.20diamond/near/254353352. ## TODO The above theorems are also true in `ℕ+`, `fin n`... To make that work, we need `succ_order α` and `succ_archimedean α`. ## Tags monotone, strictly monotone, antitone, strictly antitone, increasing, strictly increasing, decreasing, strictly decreasing -/ open function order_dual universes u v w variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type*} {r : α → α → Prop} section monotone_def variables [preorder α] [preorder β] /-- A function `f` is monotone if `a ≤ b` implies `f a ≤ f b`. -/ def monotone (f : α → β) : Prop := ∀ ⦃a b⦄, a ≤ b → f a ≤ f b /-- A function `f` is antitone if `a ≤ b` implies `f b ≤ f a`. -/ def antitone (f : α → β) : Prop := ∀ ⦃a b⦄, a ≤ b → f b ≤ f a /-- A function `f` is monotone on `s` if, for all `a, b ∈ s`, `a ≤ b` implies `f a ≤ f b`. -/ def monotone_on (f : α → β) (s : set α) : Prop := ∀ ⦃a⦄ (ha : a ∈ s) ⦃b⦄ (hb : b ∈ s), a ≤ b → f a ≤ f b /-- A function `f` is antitone on `s` if, for all `a, b ∈ s`, `a ≤ b` implies `f b ≤ f a`. -/ def antitone_on (f : α → β) (s : set α) : Prop := ∀ ⦃a⦄ (ha : a ∈ s) ⦃b⦄ (hb : b ∈ s), a ≤ b → f b ≤ f a /-- A function `f` is strictly monotone if `a < b` implies `f a < f b`. -/ def strict_mono (f : α → β) : Prop := ∀ ⦃a b⦄, a < b → f a < f b /-- A function `f` is strictly antitone if `a < b` implies `f b < f a`. -/ def strict_anti (f : α → β) : Prop := ∀ ⦃a b⦄, a < b → f b < f a /-- A function `f` is strictly monotone on `s` if, for all `a, b ∈ s`, `a < b` implies `f a < f b`. -/ def strict_mono_on (f : α → β) (s : set α) : Prop := ∀ ⦃a⦄ (ha : a ∈ s) ⦃b⦄ (hb : b ∈ s), a < b → f a < f b /-- A function `f` is strictly antitone on `s` if, for all `a, b ∈ s`, `a < b` implies `f b < f a`. -/ def strict_anti_on (f : α → β) (s : set α) : Prop := ∀ ⦃a⦄ (ha : a ∈ s) ⦃b⦄ (hb : b ∈ s), a < b → f b < f a end monotone_def /-! ### Monotonicity on the dual order Strictly, many of the `*_on.dual` lemmas in this section should use `of_dual ⁻¹' s` instead of `s`, but right now this is not possible as `set.preimage` is not defined yet, and importing it creates an import cycle. Often, you should not need the rewriting lemmas. Instead, you probably want to add `.dual`, `.dual_left` or `.dual_right` to your `monotone`/`antitone` hypothesis. -/ section order_dual variables [preorder α] [preorder β] {f : α → β} {s : set α} @[simp] lemma monotone_comp_of_dual_iff : monotone (f ∘ of_dual) ↔ antitone f := forall_swap @[simp] lemma antitone_comp_of_dual_iff : antitone (f ∘ of_dual) ↔ monotone f := forall_swap @[simp] lemma monotone_to_dual_comp_iff : monotone (to_dual ∘ f) ↔ antitone f := iff.rfl @[simp] lemma antitone_to_dual_comp_iff : antitone (to_dual ∘ f) ↔ monotone f := iff.rfl @[simp] lemma monotone_on_comp_of_dual_iff : monotone_on (f ∘ of_dual) s ↔ antitone_on f s := forall₂_swap @[simp] lemma antitone_on_comp_of_dual_iff : antitone_on (f ∘ of_dual) s ↔ monotone_on f s := forall₂_swap @[simp] lemma monotone_on_to_dual_comp_iff : monotone_on (to_dual ∘ f) s ↔ antitone_on f s := iff.rfl @[simp] lemma antitone_on_to_dual_comp_iff : antitone_on (to_dual ∘ f) s ↔ monotone_on f s := iff.rfl @[simp] lemma strict_mono_comp_of_dual_iff : strict_mono (f ∘ of_dual) ↔ strict_anti f := forall_swap @[simp] lemma strict_anti_comp_of_dual_iff : strict_anti (f ∘ of_dual) ↔ strict_mono f := forall_swap @[simp] lemma strict_mono_to_dual_comp_iff : strict_mono (to_dual ∘ f) ↔ strict_anti f := iff.rfl @[simp] lemma strict_anti_to_dual_comp_iff : strict_anti (to_dual ∘ f) ↔ strict_mono f := iff.rfl @[simp] lemma strict_mono_on_comp_of_dual_iff : strict_mono_on (f ∘ of_dual) s ↔ strict_anti_on f s := forall₂_swap @[simp] lemma strict_anti_on_comp_of_dual_iff : strict_anti_on (f ∘ of_dual) s ↔ strict_mono_on f s := forall₂_swap @[simp] lemma strict_mono_on_to_dual_comp_iff : strict_mono_on (to_dual ∘ f) s ↔ strict_anti_on f s := iff.rfl @[simp] lemma strict_anti_on_to_dual_comp_iff : strict_anti_on (to_dual ∘ f) s ↔ strict_mono_on f s := iff.rfl protected lemma monotone.dual (hf : monotone f) : monotone (to_dual ∘ f ∘ of_dual) := swap hf protected lemma antitone.dual (hf : antitone f) : antitone (to_dual ∘ f ∘ of_dual) := swap hf protected lemma monotone_on.dual (hf : monotone_on f s) : monotone_on (to_dual ∘ f ∘ of_dual) s := swap₂ hf protected lemma antitone_on.dual (hf : antitone_on f s) : antitone_on (to_dual ∘ f ∘ of_dual) s := swap₂ hf protected lemma strict_mono.dual (hf : strict_mono f) : strict_mono (to_dual ∘ f ∘ of_dual) := swap hf protected lemma strict_anti.dual (hf : strict_anti f) : strict_anti (to_dual ∘ f ∘ of_dual) := swap hf protected lemma strict_mono_on.dual (hf : strict_mono_on f s) : strict_mono_on (to_dual ∘ f ∘ of_dual) s := swap₂ hf protected lemma strict_anti_on.dual (hf : strict_anti_on f s) : strict_anti_on (to_dual ∘ f ∘ of_dual) s := swap₂ hf alias antitone_comp_of_dual_iff ↔ _ monotone.dual_left alias monotone_comp_of_dual_iff ↔ _ antitone.dual_left alias antitone_to_dual_comp_iff ↔ _ monotone.dual_right alias monotone_to_dual_comp_iff ↔ _ antitone.dual_right alias antitone_on_comp_of_dual_iff ↔ _ monotone_on.dual_left alias monotone_on_comp_of_dual_iff ↔ _ antitone_on.dual_left alias antitone_on_to_dual_comp_iff ↔ _ monotone_on.dual_right alias monotone_on_to_dual_comp_iff ↔ _ antitone_on.dual_right alias strict_anti_comp_of_dual_iff ↔ _ strict_mono.dual_left alias strict_mono_comp_of_dual_iff ↔ _ strict_anti.dual_left alias strict_anti_to_dual_comp_iff ↔ _ strict_mono.dual_right alias strict_mono_to_dual_comp_iff ↔ _ strict_anti.dual_right alias strict_anti_on_comp_of_dual_iff ↔ _ strict_mono_on.dual_left alias strict_mono_on_comp_of_dual_iff ↔ _ strict_anti_on.dual_left alias strict_anti_on_to_dual_comp_iff ↔ _ strict_mono_on.dual_right alias strict_mono_on_to_dual_comp_iff ↔ _ strict_anti_on.dual_right end order_dual /-! ### Monotonicity in function spaces -/ section preorder variables [preorder α] theorem monotone.comp_le_comp_left [preorder β] {f : β → α} {g h : γ → β} (hf : monotone f) (le_gh : g ≤ h) : has_le.le.{max w u} (f ∘ g) (f ∘ h) := λ x, hf (le_gh x) variables [preorder γ] theorem monotone_lam {f : α → β → γ} (hf : ∀ b, monotone (λ a, f a b)) : monotone f := λ a a' h b, hf b h theorem monotone_app (f : β → α → γ) (b : β) (hf : monotone (λ a b, f b a)) : monotone (f b) := λ a a' h, hf h b theorem antitone_lam {f : α → β → γ} (hf : ∀ b, antitone (λ a, f a b)) : antitone f := λ a a' h b, hf b h theorem antitone_app (f : β → α → γ) (b : β) (hf : antitone (λ a b, f b a)) : antitone (f b) := λ a a' h, hf h b end preorder lemma function.monotone_eval {ι : Type u} {α : ι → Type v} [∀ i, preorder (α i)] (i : ι) : monotone (function.eval i : (Π i, α i) → α i) := λ f g H, H i /-! ### Monotonicity hierarchy -/ section preorder variables [preorder α] section preorder variables [preorder β] {f : α → β} {a b : α} /-! These four lemmas are there to strip off the semi-implicit arguments `⦃a b : α⦄`. This is useful when you do not want to apply a `monotone` assumption (i.e. your goal is `a ≤ b → f a ≤ f b`). However if you find yourself writing `hf.imp h`, then you should have written `hf h` instead. -/ lemma monotone.imp (hf : monotone f) (h : a ≤ b) : f a ≤ f b := hf h lemma antitone.imp (hf : antitone f) (h : a ≤ b) : f b ≤ f a := hf h lemma strict_mono.imp (hf : strict_mono f) (h : a < b) : f a < f b := hf h lemma strict_anti.imp (hf : strict_anti f) (h : a < b) : f b < f a := hf h protected lemma monotone.monotone_on (hf : monotone f) (s : set α) : monotone_on f s := λ a _ b _, hf.imp protected lemma antitone.antitone_on (hf : antitone f) (s : set α) : antitone_on f s := λ a _ b _, hf.imp lemma monotone_on_univ : monotone_on f set.univ ↔ monotone f := ⟨λ h a b, h trivial trivial, λ h, h.monotone_on _⟩ lemma antitone_on_univ : antitone_on f set.univ ↔ antitone f := ⟨λ h a b, h trivial trivial, λ h, h.antitone_on _⟩ protected lemma strict_mono.strict_mono_on (hf : strict_mono f) (s : set α) : strict_mono_on f s := λ a _ b _, hf.imp protected lemma strict_anti.strict_anti_on (hf : strict_anti f) (s : set α) : strict_anti_on f s := λ a _ b _, hf.imp lemma strict_mono_on_univ : strict_mono_on f set.univ ↔ strict_mono f := ⟨λ h a b, h trivial trivial, λ h, h.strict_mono_on _⟩ lemma strict_anti_on_univ : strict_anti_on f set.univ ↔ strict_anti f := ⟨λ h a b, h trivial trivial, λ h, h.strict_anti_on _⟩ end preorder section partial_order variables [partial_order β] {f : α → β} lemma monotone.strict_mono_of_injective (h₁ : monotone f) (h₂ : injective f) : strict_mono f := λ a b h, (h₁ h.le).lt_of_ne (λ H, h.ne $ h₂ H) lemma antitone.strict_anti_of_injective (h₁ : antitone f) (h₂ : injective f) : strict_anti f := λ a b h, (h₁ h.le).lt_of_ne (λ H, h.ne $ h₂ H.symm) end partial_order end preorder section partial_order variables [partial_order α] [preorder β] {f : α → β} {s : set α} lemma monotone_iff_forall_lt : monotone f ↔ ∀ ⦃a b⦄, a < b → f a ≤ f b := forall₂_congr $ λ a b, ⟨λ hf h, hf h.le, λ hf h, h.eq_or_lt.elim (λ H, (congr_arg _ H).le) hf⟩ lemma antitone_iff_forall_lt : antitone f ↔ ∀ ⦃a b⦄, a < b → f b ≤ f a := forall₂_congr $ λ a b, ⟨λ hf h, hf h.le, λ hf h, h.eq_or_lt.elim (λ H, (congr_arg _ H).ge) hf⟩ lemma monotone_on_iff_forall_lt : monotone_on f s ↔ ∀ ⦃a⦄ (ha : a ∈ s) ⦃b⦄ (hb : b ∈ s), a < b → f a ≤ f b := ⟨λ hf a ha b hb h, hf ha hb h.le, λ hf a ha b hb h, h.eq_or_lt.elim (λ H, (congr_arg _ H).le) (hf ha hb)⟩ lemma antitone_on_iff_forall_lt : antitone_on f s ↔ ∀ ⦃a⦄ (ha : a ∈ s) ⦃b⦄ (hb : b ∈ s), a < b → f b ≤ f a := ⟨λ hf a ha b hb h, hf ha hb h.le, λ hf a ha b hb h, h.eq_or_lt.elim (λ H, (congr_arg _ H).ge) (hf ha hb)⟩ -- `preorder α` isn't strong enough: if the preorder on `α` is an equivalence relation, -- then `strict_mono f` is vacuously true. protected lemma strict_mono_on.monotone_on (hf : strict_mono_on f s) : monotone_on f s := monotone_on_iff_forall_lt.2 $ λ a ha b hb h, (hf ha hb h).le protected lemma strict_anti_on.antitone_on (hf : strict_anti_on f s) : antitone_on f s := antitone_on_iff_forall_lt.2 $ λ a ha b hb h, (hf ha hb h).le protected lemma strict_mono.monotone (hf : strict_mono f) : monotone f := monotone_iff_forall_lt.2 $ λ a b h, (hf h).le protected lemma strict_anti.antitone (hf : strict_anti f) : antitone f := antitone_iff_forall_lt.2 $ λ a b h, (hf h).le end partial_order /-! ### Monotonicity from and to subsingletons -/ namespace subsingleton variables [preorder α] [preorder β] protected lemma monotone [subsingleton α] (f : α → β) : monotone f := λ a b _, (congr_arg _ $ subsingleton.elim _ _).le protected lemma antitone [subsingleton α] (f : α → β) : antitone f := λ a b _, (congr_arg _ $ subsingleton.elim _ _).le lemma monotone' [subsingleton β] (f : α → β) : monotone f := λ a b _, (subsingleton.elim _ _).le lemma antitone' [subsingleton β] (f : α → β) : antitone f := λ a b _, (subsingleton.elim _ _).le protected lemma strict_mono [subsingleton α] (f : α → β) : strict_mono f := λ a b h, (h.ne $ subsingleton.elim _ _).elim protected lemma strict_anti [subsingleton α] (f : α → β) : strict_anti f := λ a b h, (h.ne $ subsingleton.elim _ _).elim end subsingleton /-! ### Miscellaneous monotonicity results -/ lemma monotone_id [preorder α] : monotone (id : α → α) := λ a b, id lemma monotone_on_id [preorder α] {s : set α} : monotone_on id s := λ a ha b hb, id lemma strict_mono_id [preorder α] : strict_mono (id : α → α) := λ a b, id lemma strict_mono_on_id [preorder α] {s : set α} : strict_mono_on id s := λ a ha b hb, id theorem monotone_const [preorder α] [preorder β] {c : β} : monotone (λ (a : α), c) := λ a b _, le_rfl theorem monotone_on_const [preorder α] [preorder β] {c : β} {s : set α} : monotone_on (λ (a : α), c) s := λ a _ b _ _, le_rfl theorem antitone_const [preorder α] [preorder β] {c : β} : antitone (λ (a : α), c) := λ a b _, le_refl c theorem antitone_on_const [preorder α] [preorder β] {c : β} {s : set α} : antitone_on (λ (a : α), c) s := λ a _ b _ _, le_rfl lemma strict_mono_of_le_iff_le [preorder α] [preorder β] {f : α → β} (h : ∀ x y, x ≤ y ↔ f x ≤ f y) : strict_mono f := λ a b, (lt_iff_lt_of_le_iff_le' (h _ _) (h _ _)).1 lemma strict_anti_of_le_iff_le [preorder α] [preorder β] {f : α → β} (h : ∀ x y, x ≤ y ↔ f y ≤ f x) : strict_anti f := λ a b, (lt_iff_lt_of_le_iff_le' (h _ _) (h _ _)).1 lemma injective_of_lt_imp_ne [linear_order α] {f : α → β} (h : ∀ x y, x < y → f x ≠ f y) : injective f := begin intros x y hxy, contrapose hxy, cases ne.lt_or_lt hxy with hxy hxy, exacts [h _ _ hxy, (h _ _ hxy).symm] end lemma injective_of_le_imp_le [partial_order α] [preorder β] (f : α → β) (h : ∀ {x y}, f x ≤ f y → x ≤ y) : injective f := λ x y hxy, (h hxy.le).antisymm (h hxy.ge) section preorder variables [preorder α] [preorder β] {f g : α → β} {a : α} lemma strict_mono.is_max_of_apply (hf : strict_mono f) (ha : is_max (f a)) : is_max a := of_not_not $ λ h, let ⟨b, hb⟩ := not_is_max_iff.1 h in (hf hb).not_is_max ha lemma strict_mono.is_min_of_apply (hf : strict_mono f) (ha : is_min (f a)) : is_min a := of_not_not $ λ h, let ⟨b, hb⟩ := not_is_min_iff.1 h in (hf hb).not_is_min ha lemma strict_anti.is_max_of_apply (hf : strict_anti f) (ha : is_min (f a)) : is_max a := of_not_not $ λ h, let ⟨b, hb⟩ := not_is_max_iff.1 h in (hf hb).not_is_min ha lemma strict_anti.is_min_of_apply (hf : strict_anti f) (ha : is_max (f a)) : is_min a := of_not_not $ λ h, let ⟨b, hb⟩ := not_is_min_iff.1 h in (hf hb).not_is_max ha protected lemma strict_mono.ite' (hf : strict_mono f) (hg : strict_mono g) {p : α → Prop} [decidable_pred p] (hp : ∀ ⦃x y⦄, x < y → p y → p x) (hfg : ∀ ⦃x y⦄, p x → ¬p y → x < y → f x < g y) : strict_mono (λ x, if p x then f x else g x) := begin intros x y h, by_cases hy : p y, { have hx : p x := hp h hy, simpa [hx, hy] using hf h }, by_cases hx : p x, { simpa [hx, hy] using hfg hx hy h }, { simpa [hx, hy] using hg h} end protected lemma strict_mono.ite (hf : strict_mono f) (hg : strict_mono g) {p : α → Prop} [decidable_pred p] (hp : ∀ ⦃x y⦄, x < y → p y → p x) (hfg : ∀ x, f x ≤ g x) : strict_mono (λ x, if p x then f x else g x) := hf.ite' hg hp $ λ x y hx hy h, (hf h).trans_le (hfg y) protected lemma strict_anti.ite' (hf : strict_anti f) (hg : strict_anti g) {p : α → Prop} [decidable_pred p] (hp : ∀ ⦃x y⦄, x < y → p y → p x) (hfg : ∀ ⦃x y⦄, p x → ¬p y → x < y → g y < f x) : strict_anti (λ x, if p x then f x else g x) := (strict_mono.ite' hf.dual_right hg.dual_right hp hfg).dual_right protected lemma strict_anti.ite (hf : strict_anti f) (hg : strict_anti g) {p : α → Prop} [decidable_pred p] (hp : ∀ ⦃x y⦄, x < y → p y → p x) (hfg : ∀ x, g x ≤ f x) : strict_anti (λ x, if p x then f x else g x) := hf.ite' hg hp $ λ x y hx hy h, (hfg y).trans_lt (hf h) end preorder /-! ### Monotonicity under composition -/ section composition variables [preorder α] [preorder β] [preorder γ] {g : β → γ} {f : α → β} {s : set α} protected lemma monotone.comp (hg : monotone g) (hf : monotone f) : monotone (g ∘ f) := λ a b h, hg (hf h) lemma monotone.comp_antitone (hg : monotone g) (hf : antitone f) : antitone (g ∘ f) := λ a b h, hg (hf h) protected lemma antitone.comp (hg : antitone g) (hf : antitone f) : monotone (g ∘ f) := λ a b h, hg (hf h) lemma antitone.comp_monotone (hg : antitone g) (hf : monotone f) : antitone (g ∘ f) := λ a b h, hg (hf h) protected lemma monotone.iterate {f : α → α} (hf : monotone f) (n : ℕ) : monotone (f^[n]) := nat.rec_on n monotone_id (λ n h, h.comp hf) protected lemma monotone.comp_monotone_on (hg : monotone g) (hf : monotone_on f s) : monotone_on (g ∘ f) s := λ a ha b hb h, hg (hf ha hb h) lemma monotone.comp_antitone_on (hg : monotone g) (hf : antitone_on f s) : antitone_on (g ∘ f) s := λ a ha b hb h, hg (hf ha hb h) protected lemma antitone.comp_antitone_on (hg : antitone g) (hf : antitone_on f s) : monotone_on (g ∘ f) s := λ a ha b hb h, hg (hf ha hb h) lemma antitone.comp_monotone_on (hg : antitone g) (hf : monotone_on f s) : antitone_on (g ∘ f) s := λ a ha b hb h, hg (hf ha hb h) protected lemma strict_mono.comp (hg : strict_mono g) (hf : strict_mono f) : strict_mono (g ∘ f) := λ a b h, hg (hf h) lemma strict_mono.comp_strict_anti (hg : strict_mono g) (hf : strict_anti f) : strict_anti (g ∘ f) := λ a b h, hg (hf h) protected lemma strict_anti.comp (hg : strict_anti g) (hf : strict_anti f) : strict_mono (g ∘ f) := λ a b h, hg (hf h) lemma strict_anti.comp_strict_mono (hg : strict_anti g) (hf : strict_mono f) : strict_anti (g ∘ f) := λ a b h, hg (hf h) protected lemma strict_mono.iterate {f : α → α} (hf : strict_mono f) (n : ℕ) : strict_mono (f^[n]) := nat.rec_on n strict_mono_id (λ n h, h.comp hf) protected lemma strict_mono.comp_strict_mono_on (hg : strict_mono g) (hf : strict_mono_on f s) : strict_mono_on (g ∘ f) s := λ a ha b hb h, hg (hf ha hb h) lemma strict_mono.comp_strict_anti_on (hg : strict_mono g) (hf : strict_anti_on f s) : strict_anti_on (g ∘ f) s := λ a ha b hb h, hg (hf ha hb h) protected lemma strict_anti.comp_strict_anti_on (hg : strict_anti g) (hf : strict_anti_on f s) : strict_mono_on (g ∘ f) s := λ a ha b hb h, hg (hf ha hb h) lemma strict_anti.comp_strict_mono_on (hg : strict_anti g) (hf : strict_mono_on f s) : strict_anti_on (g ∘ f) s := λ a ha b hb h, hg (hf ha hb h) end composition namespace list section fold theorem foldl_monotone [preorder α] {f : α → β → α} (H : ∀ b, monotone (λ a, f a b)) (l : list β) : monotone (λ a, l.foldl f a) := list.rec_on l (λ _ _, id) (λ i l hl _ _ h, hl (H _ h)) theorem foldr_monotone [preorder β] {f : α → β → β} (H : ∀ a, monotone (f a)) (l : list α) : monotone (λ b, l.foldr f b) := λ _ _ h, list.rec_on l h (λ i l hl, H i hl) theorem foldl_strict_mono [preorder α] {f : α → β → α} (H : ∀ b, strict_mono (λ a, f a b)) (l : list β) : strict_mono (λ a, l.foldl f a) := list.rec_on l (λ _ _, id) (λ i l hl _ _ h, hl (H _ h)) theorem foldr_strict_mono [preorder β] {f : α → β → β} (H : ∀ a, strict_mono (f a)) (l : list α) : strict_mono (λ b, l.foldr f b) := λ _ _ h, list.rec_on l h (λ i l hl, H i hl) end fold end list /-! ### Monotonicity in linear orders -/ section linear_order variables [linear_order α] section preorder variables [preorder β] {f : α → β} {s : set α} open ordering lemma monotone.reflect_lt (hf : monotone f) {a b : α} (h : f a < f b) : a < b := lt_of_not_ge (λ h', h.not_le (hf h')) lemma antitone.reflect_lt (hf : antitone f) {a b : α} (h : f a < f b) : b < a := lt_of_not_ge (λ h', h.not_le (hf h')) lemma monotone_on.reflect_lt (hf : monotone_on f s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) (h : f a < f b) : a < b := lt_of_not_ge $ λ h', h.not_le $ hf hb ha h' lemma antitone_on.reflect_lt (hf : antitone_on f s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) (h : f a < f b) : b < a := lt_of_not_ge $ λ h', h.not_le $ hf ha hb h' lemma strict_mono_on.le_iff_le (hf : strict_mono_on f s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) : f a ≤ f b ↔ a ≤ b := ⟨λ h, le_of_not_gt $ λ h', (hf hb ha h').not_le h, λ h, h.lt_or_eq_dec.elim (λ h', (hf ha hb h').le) (λ h', h' ▸ le_rfl)⟩ lemma strict_anti_on.le_iff_le (hf : strict_anti_on f s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) : f a ≤ f b ↔ b ≤ a := hf.dual_right.le_iff_le hb ha lemma strict_mono_on.lt_iff_lt (hf : strict_mono_on f s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) : f a < f b ↔ a < b := by rw [lt_iff_le_not_le, lt_iff_le_not_le, hf.le_iff_le ha hb, hf.le_iff_le hb ha] lemma strict_anti_on.lt_iff_lt (hf : strict_anti_on f s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) : f a < f b ↔ b < a := hf.dual_right.lt_iff_lt hb ha lemma strict_mono.le_iff_le (hf : strict_mono f) {a b : α} : f a ≤ f b ↔ a ≤ b := (hf.strict_mono_on set.univ).le_iff_le trivial trivial lemma strict_anti.le_iff_le (hf : strict_anti f) {a b : α} : f a ≤ f b ↔ b ≤ a := (hf.strict_anti_on set.univ).le_iff_le trivial trivial lemma strict_mono.lt_iff_lt (hf : strict_mono f) {a b : α} : f a < f b ↔ a < b := (hf.strict_mono_on set.univ).lt_iff_lt trivial trivial lemma strict_anti.lt_iff_lt (hf : strict_anti f) {a b : α} : f a < f b ↔ b < a := (hf.strict_anti_on set.univ).lt_iff_lt trivial trivial protected theorem strict_mono_on.compares (hf : strict_mono_on f s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) : ∀ {o : ordering}, o.compares (f a) (f b) ↔ o.compares a b | ordering.lt := hf.lt_iff_lt ha hb | ordering.eq := ⟨λ h, ((hf.le_iff_le ha hb).1 h.le).antisymm ((hf.le_iff_le hb ha).1 h.symm.le), congr_arg _⟩ | ordering.gt := hf.lt_iff_lt hb ha protected theorem strict_anti_on.compares (hf : strict_anti_on f s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) {o : ordering} : o.compares (f a) (f b) ↔ o.compares b a := to_dual_compares_to_dual.trans $ hf.dual_right.compares hb ha protected theorem strict_mono.compares (hf : strict_mono f) {a b : α} {o : ordering} : o.compares (f a) (f b) ↔ o.compares a b := (hf.strict_mono_on set.univ).compares trivial trivial protected theorem strict_anti.compares (hf : strict_anti f) {a b : α} {o : ordering} : o.compares (f a) (f b) ↔ o.compares b a := (hf.strict_anti_on set.univ).compares trivial trivial lemma strict_mono.injective (hf : strict_mono f) : injective f := λ x y h, show compares eq x y, from hf.compares.1 h lemma strict_anti.injective (hf : strict_anti f) : injective f := λ x y h, show compares eq x y, from hf.compares.1 h.symm lemma strict_mono.maximal_of_maximal_image (hf : strict_mono f) {a} (hmax : ∀ p, p ≤ f a) (x : α) : x ≤ a := hf.le_iff_le.mp (hmax (f x)) lemma strict_mono.minimal_of_minimal_image (hf : strict_mono f) {a} (hmin : ∀ p, f a ≤ p) (x : α) : a ≤ x := hf.le_iff_le.mp (hmin (f x)) lemma strict_anti.minimal_of_maximal_image (hf : strict_anti f) {a} (hmax : ∀ p, p ≤ f a) (x : α) : a ≤ x := hf.le_iff_le.mp (hmax (f x)) lemma strict_anti.maximal_of_minimal_image (hf : strict_anti f) {a} (hmin : ∀ p, f a ≤ p) (x : α) : x ≤ a := hf.le_iff_le.mp (hmin (f x)) end preorder section partial_order variables [partial_order β] {f : α → β} lemma monotone.strict_mono_iff_injective (hf : monotone f) : strict_mono f ↔ injective f := ⟨λ h, h.injective, hf.strict_mono_of_injective⟩ lemma antitone.strict_anti_iff_injective (hf : antitone f) : strict_anti f ↔ injective f := ⟨λ h, h.injective, hf.strict_anti_of_injective⟩ end partial_order /-! ### Strictly monotone functions and `cmp` -/ variables [linear_order β] {f : α → β} {s : set α} {x y : α} lemma strict_mono_on.cmp_map_eq (hf : strict_mono_on f s) (hx : x ∈ s) (hy : y ∈ s) : cmp (f x) (f y) = cmp x y := ((hf.compares hx hy).2 (cmp_compares x y)).cmp_eq lemma strict_mono.cmp_map_eq (hf : strict_mono f) (x y : α) : cmp (f x) (f y) = cmp x y := (hf.strict_mono_on set.univ).cmp_map_eq trivial trivial lemma strict_anti_on.cmp_map_eq (hf : strict_anti_on f s) (hx : x ∈ s) (hy : y ∈ s) : cmp (f x) (f y) = cmp y x := hf.dual_right.cmp_map_eq hy hx lemma strict_anti.cmp_map_eq (hf : strict_anti f) (x y : α) : cmp (f x) (f y) = cmp y x := (hf.strict_anti_on set.univ).cmp_map_eq trivial trivial end linear_order /-! ### Monotonicity in `ℕ` and `ℤ` -/ section preorder variables [preorder α] lemma nat.rel_of_forall_rel_succ_of_le_of_lt (r : β → β → Prop) [is_trans β r] {f : ℕ → β} {a : ℕ} (h : ∀ n, a ≤ n → r (f n) (f (n + 1))) ⦃b c : ℕ⦄ (hab : a ≤ b) (hbc : b < c) : r (f b) (f c) := begin induction hbc with k b_lt_k r_b_k, exacts [h _ hab, trans r_b_k (h _ (hab.trans_lt b_lt_k).le)] end lemma nat.rel_of_forall_rel_succ_of_le_of_le (r : β → β → Prop) [is_refl β r] [is_trans β r] {f : ℕ → β} {a : ℕ} (h : ∀ n, a ≤ n → r (f n) (f (n + 1))) ⦃b c : ℕ⦄ (hab : a ≤ b) (hbc : b ≤ c) : r (f b) (f c) := hbc.eq_or_lt.elim (λ h, h ▸ refl _) (nat.rel_of_forall_rel_succ_of_le_of_lt r h hab) lemma nat.rel_of_forall_rel_succ_of_lt (r : β → β → Prop) [is_trans β r] {f : ℕ → β} (h : ∀ n, r (f n) (f (n + 1))) ⦃a b : ℕ⦄ (hab : a < b) : r (f a) (f b) := nat.rel_of_forall_rel_succ_of_le_of_lt r (λ n _, h n) le_rfl hab lemma nat.rel_of_forall_rel_succ_of_le (r : β → β → Prop) [is_refl β r] [is_trans β r] {f : ℕ → β} (h : ∀ n, r (f n) (f (n + 1))) ⦃a b : ℕ⦄ (hab : a ≤ b) : r (f a) (f b) := nat.rel_of_forall_rel_succ_of_le_of_le r (λ n _, h n) le_rfl hab lemma monotone_nat_of_le_succ {f : ℕ → α} (hf : ∀ n, f n ≤ f (n + 1)) : monotone f := nat.rel_of_forall_rel_succ_of_le (≤) hf lemma antitone_nat_of_succ_le {f : ℕ → α} (hf : ∀ n, f (n + 1) ≤ f n) : antitone f := @monotone_nat_of_le_succ αᵒᵈ _ _ hf lemma strict_mono_nat_of_lt_succ {f : ℕ → α} (hf : ∀ n, f n < f (n + 1)) : strict_mono f := nat.rel_of_forall_rel_succ_of_lt (<) hf lemma strict_anti_nat_of_succ_lt {f : ℕ → α} (hf : ∀ n, f (n + 1) < f n) : strict_anti f := @strict_mono_nat_of_lt_succ αᵒᵈ _ f hf namespace nat /-- If `α` is a preorder with no maximal elements, then there exists a strictly monotone function `ℕ → α` with any prescribed value of `f 0`. -/ lemma exists_strict_mono' [no_max_order α] (a : α) : ∃ f : ℕ → α, strict_mono f ∧ f 0 = a := begin have := (λ x : α, exists_gt x), choose g hg, exact ⟨λ n, nat.rec_on n a (λ _, g), strict_mono_nat_of_lt_succ $ λ n, hg _, rfl⟩ end /-- If `α` is a preorder with no maximal elements, then there exists a strictly antitone function `ℕ → α` with any prescribed value of `f 0`. -/ lemma exists_strict_anti' [no_min_order α] (a : α) : ∃ f : ℕ → α, strict_anti f ∧ f 0 = a := exists_strict_mono' (order_dual.to_dual a) variable (α) /-- If `α` is a nonempty preorder with no maximal elements, then there exists a strictly monotone function `ℕ → α`. -/ lemma exists_strict_mono [nonempty α] [no_max_order α] : ∃ f : ℕ → α, strict_mono f := let ⟨a⟩ := ‹nonempty α›, ⟨f, hf, hfa⟩ := exists_strict_mono' a in ⟨f, hf⟩ /-- If `α` is a nonempty preorder with no minimal elements, then there exists a strictly antitone function `ℕ → α`. -/ lemma exists_strict_anti [nonempty α] [no_min_order α] : ∃ f : ℕ → α, strict_anti f := exists_strict_mono αᵒᵈ end nat lemma int.rel_of_forall_rel_succ_of_lt (r : β → β → Prop) [is_trans β r] {f : ℤ → β} (h : ∀ n, r (f n) (f (n + 1))) ⦃a b : ℤ⦄ (hab : a < b) : r (f a) (f b) := begin rcases hab.dest with ⟨n, rfl⟩, clear hab, induction n with n ihn, { rw int.coe_nat_one, apply h }, { rw [int.coe_nat_succ, ← int.add_assoc], exact trans ihn (h _) } end lemma int.rel_of_forall_rel_succ_of_le (r : β → β → Prop) [is_refl β r] [is_trans β r] {f : ℤ → β} (h : ∀ n, r (f n) (f (n + 1))) ⦃a b : ℤ⦄ (hab : a ≤ b) : r (f a) (f b) := hab.eq_or_lt.elim (λ h, h ▸ refl _) (λ h', int.rel_of_forall_rel_succ_of_lt r h h') lemma monotone_int_of_le_succ {f : ℤ → α} (hf : ∀ n, f n ≤ f (n + 1)) : monotone f := int.rel_of_forall_rel_succ_of_le (≤) hf lemma antitone_int_of_succ_le {f : ℤ → α} (hf : ∀ n, f (n + 1) ≤ f n) : antitone f := int.rel_of_forall_rel_succ_of_le (≥) hf lemma strict_mono_int_of_lt_succ {f : ℤ → α} (hf : ∀ n, f n < f (n + 1)) : strict_mono f := int.rel_of_forall_rel_succ_of_lt (<) hf lemma strict_anti_int_of_succ_lt {f : ℤ → α} (hf : ∀ n, f (n + 1) < f n) : strict_anti f := int.rel_of_forall_rel_succ_of_lt (>) hf namespace int variables (α) [nonempty α] [no_min_order α] [no_max_order α] /-- If `α` is a nonempty preorder with no minimal or maximal elements, then there exists a strictly monotone function `f : ℤ → α`. -/ lemma exists_strict_mono : ∃ f : ℤ → α, strict_mono f := begin inhabit α, rcases nat.exists_strict_mono' (default : α) with ⟨f, hf, hf₀⟩, rcases nat.exists_strict_anti' (default : α) with ⟨g, hg, hg₀⟩, refine ⟨λ n, int.cases_on n f (λ n, g (n + 1)), strict_mono_int_of_lt_succ _⟩, rintro (n|_|n), { exact hf n.lt_succ_self }, { show g 1 < f 0, rw [hf₀, ← hg₀], exact hg nat.zero_lt_one }, { exact hg (nat.lt_succ_self _) } end /-- If `α` is a nonempty preorder with no minimal or maximal elements, then there exists a strictly antitone function `f : ℤ → α`. -/ lemma exists_strict_anti : ∃ f : ℤ → α, strict_anti f := exists_strict_mono αᵒᵈ end int -- TODO@Yael: Generalize the following four to succ orders /-- If `f` is a monotone function from `ℕ` to a preorder such that `x` lies between `f n` and `f (n + 1)`, then `x` doesn't lie in the range of `f`. -/ lemma monotone.ne_of_lt_of_lt_nat {f : ℕ → α} (hf : monotone f) (n : ℕ) {x : α} (h1 : f n < x) (h2 : x < f (n + 1)) (a : ℕ) : f a ≠ x := by { rintro rfl, exact (hf.reflect_lt h1).not_le (nat.le_of_lt_succ $ hf.reflect_lt h2) } /-- If `f` is an antitone function from `ℕ` to a preorder such that `x` lies between `f (n + 1)` and `f n`, then `x` doesn't lie in the range of `f`. -/ lemma antitone.ne_of_lt_of_lt_nat {f : ℕ → α} (hf : antitone f) (n : ℕ) {x : α} (h1 : f (n + 1) < x) (h2 : x < f n) (a : ℕ) : f a ≠ x := by { rintro rfl, exact (hf.reflect_lt h2).not_le (nat.le_of_lt_succ $ hf.reflect_lt h1) } /-- If `f` is a monotone function from `ℤ` to a preorder and `x` lies between `f n` and `f (n + 1)`, then `x` doesn't lie in the range of `f`. -/ lemma monotone.ne_of_lt_of_lt_int {f : ℤ → α} (hf : monotone f) (n : ℤ) {x : α} (h1 : f n < x) (h2 : x < f (n + 1)) (a : ℤ) : f a ≠ x := by { rintro rfl, exact (hf.reflect_lt h1).not_le (int.le_of_lt_add_one $ hf.reflect_lt h2) } /-- If `f` is an antitone function from `ℤ` to a preorder and `x` lies between `f (n + 1)` and `f n`, then `x` doesn't lie in the range of `f`. -/ lemma antitone.ne_of_lt_of_lt_int {f : ℤ → α} (hf : antitone f) (n : ℤ) {x : α} (h1 : f (n + 1) < x) (h2 : x < f n) (a : ℤ) : f a ≠ x := by { rintro rfl, exact (hf.reflect_lt h2).not_le (int.le_of_lt_add_one $ hf.reflect_lt h1) } lemma strict_mono.id_le {φ : ℕ → ℕ} (h : strict_mono φ) : ∀ n, n ≤ φ n := λ n, nat.rec_on n (nat.zero_le _) (λ n hn, nat.succ_le_of_lt (hn.trans_lt $ h $ nat.lt_succ_self n)) end preorder lemma subtype.mono_coe [preorder α] (t : set α) : monotone (coe : (subtype t) → α) := λ x y, id lemma subtype.strict_mono_coe [preorder α] (t : set α) : strict_mono (coe : (subtype t) → α) := λ x y, id section preorder variables [preorder α] [preorder β] [preorder γ] [preorder δ] {f : α → γ} {g : β → δ} {a b : α} lemma monotone_fst : monotone (@prod.fst α β) := λ a b, and.left lemma monotone_snd : monotone (@prod.snd α β) := λ a b, and.right lemma monotone.prod_map (hf : monotone f) (hg : monotone g) : monotone (prod.map f g) := λ a b h, ⟨hf h.1, hg h.2⟩ lemma antitone.prod_map (hf : antitone f) (hg : antitone g) : antitone (prod.map f g) := λ a b h, ⟨hf h.1, hg h.2⟩ end preorder section partial_order variables [partial_order α] [partial_order β] [preorder γ] [preorder δ] {f : α → γ} {g : β → δ} lemma strict_mono.prod_map (hf : strict_mono f) (hg : strict_mono g) : strict_mono (prod.map f g) := λ a b, by { simp_rw prod.lt_iff, exact or.imp (and.imp hf.imp hg.monotone.imp) (and.imp hf.monotone.imp hg.imp) } lemma strict_anti.prod_map (hf : strict_anti f) (hg : strict_anti g) : strict_anti (prod.map f g) := λ a b, by { simp_rw prod.lt_iff, exact or.imp (and.imp hf.imp hg.antitone.imp) (and.imp hf.antitone.imp hg.imp) } end partial_order
b85696e6c160411d945ff7651916490f2a5eb048
618003631150032a5676f229d13a079ac875ff77
/src/algebra/continued_fractions/terminated_stable.lean
08c9cccfcc79574dcbfd9000e5500c7c2d1a4d47
[ "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
4,645
lean
/- Copyright (c) 2020 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import algebra.continued_fractions.translations /-! # Stabilisation of gcf Computations Under Termination ## Summary We show that the continuants and convergents of a gcf stabilise once the gcf terminates. -/ namespace generalized_continued_fraction open generalized_continued_fraction as gcf variables {K : Type*} {g : gcf K} {n m : ℕ} /-- If a gcf terminated at position `n`, it also terminated at `m ≥ n`.-/ lemma terminated_stable (n_le_m : n ≤ m) (terminated_at_n : g.terminated_at n) : g.terminated_at m := g.s.terminated_stable n_le_m terminated_at_n variable [division_ring K] lemma continuants_aux_stable_step_of_terminated (terminated_at_n : g.terminated_at n) : g.continuants_aux (n + 2) = g.continuants_aux (n + 1) := by { rw [terminated_at_iff_s_none] at terminated_at_n, simp only [terminated_at_n, continuants_aux] } lemma continuants_aux_stable_of_terminated (succ_n_le_m : (n + 1) ≤ m) (terminated_at_n : g.terminated_at n) : g.continuants_aux m = g.continuants_aux (n + 1) := begin induction succ_n_le_m with m succ_n_le_m IH, { refl }, { have : g.continuants_aux (m + 1) = g.continuants_aux m, by { have : n ≤ m - 1, from nat.le_pred_of_lt succ_n_le_m, have : g.terminated_at (m - 1), from terminated_stable this terminated_at_n, have stable_step : g.continuants_aux (m - 1 + 2) = g.continuants_aux (m - 1 + 1), from continuants_aux_stable_step_of_terminated this, have one_le_m : 1 ≤ m, from nat.one_le_of_lt succ_n_le_m, have : m - 1 + 2 = m + 2 - 1, from (nat.sub_add_comm one_le_m).symm, have : m - 1 + 1 = m + 1 - 1, from (nat.sub_add_comm one_le_m).symm, simpa [*] using stable_step }, exact (eq.trans this IH) } end lemma convergents'_aux_stable_step_of_terminated {s : seq $ gcf.pair K} (terminated_at_n : s.terminated_at n) : convergents'_aux s (n + 1) = convergents'_aux s n := begin change s.nth n = none at terminated_at_n, induction n with n IH generalizing s, case nat.zero { simp only [convergents'_aux, terminated_at_n, seq.head] }, case nat.succ { cases s_head_eq : s.head with gp_head, case option.none { simp only [convergents'_aux, s_head_eq] }, case option.some { have : s.tail.terminated_at n, by simp only [seq.terminated_at, s.nth_tail, terminated_at_n], simp only [convergents'_aux, s_head_eq, (IH this)] }} end lemma convergents'_aux_stable_of_terminated {s : seq $ gcf.pair K} (n_le_m : n ≤ m) (terminated_at_n : s.terminated_at n) : convergents'_aux s m = convergents'_aux s n := begin induction n_le_m with m n_le_m IH generalizing s, { refl }, { cases s_head_eq : s.head with gp_head, case option.none { cases n; simp only [convergents'_aux, s_head_eq] }, case option.some { have : convergents'_aux s (n + 1) = convergents'_aux s n, from convergents'_aux_stable_step_of_terminated terminated_at_n, rw [←this], have : s.tail.terminated_at n, by simpa only [seq.terminated_at, seq.nth_tail] using (s.le_stable n.le_succ terminated_at_n), have : convergents'_aux s.tail m = convergents'_aux s.tail n, from IH this, simp only [convergents'_aux, s_head_eq, this] }} end lemma continuants_stable_of_terminated (n_le_m : n ≤ m) (terminated_at_n : g.terminated_at n) : g.continuants m = g.continuants n := by simp only [nth_cont_eq_succ_nth_cont_aux, (continuants_aux_stable_of_terminated (nat.pred_le_iff.elim_left n_le_m) terminated_at_n)] lemma numerators_stable_of_terminated (n_le_m : n ≤ m) (terminated_at_n : g.terminated_at n) : g.numerators m = g.numerators n := by simp only [num_eq_conts_a, (continuants_stable_of_terminated n_le_m terminated_at_n)] lemma denominators_stable_of_terminated (n_le_m : n ≤ m) (terminated_at_n : g.terminated_at n) : g.denominators m = g.denominators n := by simp only [denom_eq_conts_b, (continuants_stable_of_terminated n_le_m terminated_at_n)] lemma convergents_stable_of_terminated (n_le_m : n ≤ m) (terminated_at_n : g.terminated_at n) : g.convergents m = g.convergents n := by simp only [convergents, (denominators_stable_of_terminated n_le_m terminated_at_n), (numerators_stable_of_terminated n_le_m terminated_at_n)] lemma convergents'_stable_of_terminated (n_le_m : n ≤ m) (terminated_at_n : g.terminated_at n) : g.convergents' m = g.convergents' n := by simp only [convergents', (convergents'_aux_stable_of_terminated n_le_m terminated_at_n)] end generalized_continued_fraction
f4b644f108f3a0dd173ef3d81508d449f647dcc8
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/nat/factorial/basic.lean
8bfe1ea691c530b94214194d007d0f80bec8b258
[ "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
14,940
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Chris Hughes, Floris van Doorn, Yaël Dillies -/ import data.nat.basic import data.nat.pow /-! # Factorial and variants This file defines the factorial, along with the ascending and descending variants. ## Main declarations * `nat.factorial`: The factorial. * `nat.asc_factorial`: The ascending factorial. Note that it runs from `n + 1` to `n + k` and *not* from `n` to `n + k - 1`. We might want to change that in the future. * `nat.desc_factorial`: The descending factorial. It runs from `n - k` to `n`. -/ namespace nat /-- `nat.factorial n` is the factorial of `n`. -/ @[simp] def factorial : ℕ → ℕ | 0 := 1 | (succ n) := succ n * factorial n localized "notation n `!`:10000 := nat.factorial n" in nat section factorial variables {m n : ℕ} @[simp] theorem factorial_zero : 0! = 1 := rfl @[simp] theorem factorial_succ (n : ℕ) : n.succ! = (n + 1) * n! := rfl @[simp] theorem factorial_one : 1! = 1 := rfl @[simp] theorem factorial_two : 2! = 2 := rfl theorem mul_factorial_pred (hn : 0 < n) : n * (n - 1)! = n! := tsub_add_cancel_of_le (nat.succ_le_of_lt hn) ▸ rfl theorem factorial_pos : ∀ n, 0 < n! | 0 := zero_lt_one | (succ n) := mul_pos (succ_pos _) (factorial_pos n) theorem factorial_ne_zero (n : ℕ) : n! ≠ 0 := ne_of_gt (factorial_pos _) theorem factorial_dvd_factorial {m n} (h : m ≤ n) : m! ∣ n! := begin induction n with n IH; simp, { have := nat.eq_zero_of_le_zero h, subst m, simp }, obtain he | hl := h.eq_or_lt, { subst m, simp }, exact (IH (le_of_lt_succ hl)).mul_left _, end theorem dvd_factorial : ∀ {m n}, 0 < m → m ≤ n → m ∣ n! | (succ m) n _ h := dvd_of_mul_right_dvd (factorial_dvd_factorial h) @[mono] theorem factorial_le {m n} (h : m ≤ n) : m! ≤ n! := le_of_dvd (factorial_pos _) (factorial_dvd_factorial h) lemma factorial_mul_pow_le_factorial : ∀ {m n : ℕ}, m! * m.succ ^ n ≤ (m + n)! | m 0 := by simp | m (n+1) := by rw [← add_assoc, nat.factorial_succ, mul_comm (nat.succ _), pow_succ', ← mul_assoc]; exact mul_le_mul factorial_mul_pow_le_factorial (nat.succ_le_succ (nat.le_add_right _ _)) (nat.zero_le _) (nat.zero_le _) lemma monotone_factorial : monotone factorial := λ n m, factorial_le lemma factorial_lt (hn : 0 < n) : n! < m! ↔ n < m := begin split; intro h, { rw [← not_le], intro hmn, apply not_le_of_lt h (factorial_le hmn) }, have : ∀ n, 0 < n → n! < n.succ!, { intros k hk, rw [factorial_succ, succ_mul, lt_add_iff_pos_left], apply mul_pos hk (factorial_pos k) }, induction h with k hnk generalizing hn, { exact this _ hn, }, refine lt_trans (h_ih hn) (this _ _), exact lt_trans hn (lt_of_succ_le hnk), end lemma one_lt_factorial : 1 < n! ↔ 1 < n := by { convert factorial_lt _, refl, exact one_pos } lemma factorial_eq_one : n! = 1 ↔ n ≤ 1 := begin split; intro h, { rw [← not_lt, ← one_lt_factorial, h], apply lt_irrefl }, cases h with h h, refl, cases h, refl, end lemma factorial_inj (hn : 1 < n!) : n! = m! ↔ n = m := begin split; intro h, { obtain hnm | hnm | hnm := lt_trichotomy n m, { exfalso, rw [← factorial_lt, h] at hnm, exact lt_irrefl _ hnm, rw [one_lt_factorial] at hn, exact lt_trans one_pos hn }, { exact hnm }, exfalso, rw [h, one_lt_factorial] at hn, rw [←factorial_lt (lt_trans one_pos hn), h] at hnm, exact lt_irrefl _ hnm, }, { rw h }, end lemma self_le_factorial : ∀ n : ℕ, n ≤ n! | 0 := zero_le_one | (k + 1) := le_mul_of_one_le_right k.zero_lt_succ.le (nat.one_le_of_lt $ nat.factorial_pos _) lemma lt_factorial_self {n : ℕ} (hi : 3 ≤ n) : n < n! := begin rw [← succ_pred_eq_of_pos ((zero_lt_two.trans (lt.base 2)).trans_le hi), factorial_succ], exact lt_mul_of_one_lt_right ((pred n).succ_pos) ((one_lt_two.trans_le (le_pred_of_lt (succ_le_iff.mp hi))).trans_le (self_le_factorial _)), end lemma add_factorial_succ_lt_factorial_add_succ {i : ℕ} (n : ℕ) (hi : 2 ≤ i) : i + (n + 1)! < (i + n + 1)! := begin rw [factorial_succ (i + _), add_mul, one_mul], have : i ≤ i + n := le.intro rfl, exact add_lt_add_of_lt_of_le (this.trans_lt ((lt_mul_iff_one_lt_right (zero_lt_two.trans_le (hi.trans this))).mpr (lt_iff_le_and_ne.mpr ⟨(i + n).factorial_pos, λ g, nat.not_succ_le_self 1 ((hi.trans this).trans (factorial_eq_one.mp g.symm))⟩))) (factorial_le ((le_of_eq (add_comm n 1)).trans ((add_le_add_iff_right n).mpr (one_le_two.trans hi)))), end lemma add_factorial_lt_factorial_add {i n : ℕ} (hi : 2 ≤ i) (hn : 1 ≤ n) : i + n! < (i + n)! := begin cases hn, { rw factorial_one, exact lt_factorial_self (succ_le_succ hi) }, exact add_factorial_succ_lt_factorial_add_succ _ hi, end lemma add_factorial_succ_le_factorial_add_succ (i : ℕ) (n : ℕ) : i + (n + 1)! ≤ (i + (n + 1))! := begin obtain i2 | (_ | ⟨_, i0⟩) := le_or_lt 2 i, { exact (n.add_factorial_succ_lt_factorial_add_succ i2).le }, { change 1 + (n + 1)! ≤ (1 + n + 1) * (1 + n)!, rw [add_mul, one_mul, add_comm 1 n], exact (add_le_add_iff_right _).mpr (one_le_mul (nat.le_add_left 1 n) (n + 1).factorial_pos) }, rw [nat.le_zero_iff.mp (nat.succ_le_succ_iff.mp i0), zero_add, zero_add] end lemma add_factorial_le_factorial_add (i : ℕ) {n : ℕ} (n1 : 1 ≤ n) : i + n! ≤ (i + n)! := begin cases n1 with h, { exact self_le_factorial _ }, exact add_factorial_succ_le_factorial_add_succ i h, end lemma factorial_mul_pow_sub_le_factorial {n m : ℕ} (hnm : n ≤ m) : n! * n ^ (m - n) ≤ m! := begin suffices : n! * (n + 1) ^ (m - n) ≤ m!, { apply trans _ this, rw mul_le_mul_left, apply pow_le_pow_of_le_left (zero_le n) (le_succ n), exact factorial_pos n,}, convert nat.factorial_mul_pow_le_factorial, exact (add_tsub_cancel_of_le hnm).symm, end end factorial /-! ### Ascending and descending factorials -/ section asc_factorial /-- `n.asc_factorial k = (n + k)! / n!` (as seen in `nat.asc_factorial_eq_div`), but implemented recursively to allow for "quick" computation when using `norm_num`. This is closely related to `pochhammer`, but much less general. -/ def asc_factorial (n : ℕ) : ℕ → ℕ | 0 := 1 | (k + 1) := (n + k + 1) * asc_factorial k @[simp] lemma asc_factorial_zero (n : ℕ) : n.asc_factorial 0 = 1 := rfl @[simp] lemma zero_asc_factorial (k : ℕ) : (0 : ℕ).asc_factorial k = k! := begin induction k with t ht, refl, unfold asc_factorial, rw [ht, zero_add, nat.factorial_succ], end lemma asc_factorial_succ {n k : ℕ} : n.asc_factorial k.succ = (n + k + 1) * n.asc_factorial k := rfl lemma succ_asc_factorial (n : ℕ) : ∀ k, (n + 1) * n.succ.asc_factorial k = (n + k + 1) * n.asc_factorial k | 0 := by rw [add_zero, asc_factorial_zero, asc_factorial_zero] | (k + 1) := by rw [asc_factorial, mul_left_comm, succ_asc_factorial, asc_factorial, succ_add, ←add_assoc] /-- `n.asc_factorial k = (n + k)! / n!` but without ℕ-division. See `nat.asc_factorial_eq_div` for the version with ℕ-division. -/ theorem factorial_mul_asc_factorial (n : ℕ) : ∀ k, n! * n.asc_factorial k = (n + k)! | 0 := by rw [asc_factorial, add_zero, mul_one] | (k + 1) := by rw [asc_factorial_succ, mul_left_comm, factorial_mul_asc_factorial, ← add_assoc, factorial] /-- Avoid in favor of `nat.factorial_mul_asc_factorial` if you can. ℕ-division isn't worth it. -/ lemma asc_factorial_eq_div (n k : ℕ) : n.asc_factorial k = (n + k)! / n! := begin apply mul_left_cancel₀ (factorial_ne_zero n), rw factorial_mul_asc_factorial, exact (nat.mul_div_cancel' $ factorial_dvd_factorial $ le.intro rfl).symm end lemma asc_factorial_of_sub {n k : ℕ} (h : k < n) : (n - k) * (n - k).asc_factorial k = (n - (k + 1)).asc_factorial (k + 1) := begin set t := n - k.succ with ht, suffices h' : n - k = t.succ, by rw [←ht, h', succ_asc_factorial, asc_factorial_succ], rw [ht, succ_eq_add_one, ←tsub_tsub_assoc (succ_le_of_lt h) (succ_pos _), succ_sub_one], end lemma pow_succ_le_asc_factorial (n : ℕ) : ∀ (k : ℕ), (n + 1)^k ≤ n.asc_factorial k | 0 := by rw [asc_factorial_zero, pow_zero] | (k + 1) := begin rw pow_succ, exact nat.mul_le_mul (nat.add_le_add_right le_self_add _) (pow_succ_le_asc_factorial k), end lemma pow_lt_asc_factorial' (n k : ℕ) : (n + 1)^(k + 2) < n.asc_factorial (k + 2) := begin rw pow_succ, exact nat.mul_lt_mul (nat.add_lt_add_right (nat.lt_add_of_pos_right succ_pos') 1) (pow_succ_le_asc_factorial n _) (pow_pos succ_pos' _), end lemma pow_lt_asc_factorial (n : ℕ) : ∀ {k : ℕ}, 2 ≤ k → (n + 1)^k < n.asc_factorial k | 0 := by rintro ⟨⟩ | 1 := by rintro (_ | ⟨_, ⟨⟩⟩) | (k + 2) := λ _, pow_lt_asc_factorial' n k lemma asc_factorial_le_pow_add (n : ℕ) : ∀ (k : ℕ), n.asc_factorial k ≤ (n + k)^k | 0 := by rw [asc_factorial_zero, pow_zero] | (k + 1) := begin rw [asc_factorial_succ, pow_succ], exact nat.mul_le_mul_of_nonneg_left ((asc_factorial_le_pow_add k).trans (nat.pow_le_pow_of_le_left (le_succ _) _)), end lemma asc_factorial_lt_pow_add (n : ℕ) : ∀ {k : ℕ}, 2 ≤ k → n.asc_factorial k < (n + k)^k | 0 := by rintro ⟨⟩ | 1 := by rintro (_ | ⟨_, ⟨⟩⟩) | (k + 2) := λ _, begin rw [asc_factorial_succ, pow_succ], refine nat.mul_lt_mul' le_rfl ((asc_factorial_le_pow_add n _).trans_lt (pow_lt_pow_of_lt_left (lt_add_one _) (succ_pos _))) (succ_pos _), end lemma asc_factorial_pos (n k : ℕ) : 0 < n.asc_factorial k := (pow_pos (succ_pos n) k).trans_le (pow_succ_le_asc_factorial n k) end asc_factorial section desc_factorial /-- `n.desc_factorial k = n! / (n - k)!` (as seen in `nat.desc_factorial_eq_div`), but implemented recursively to allow for "quick" computation when using `norm_num`. This is closely related to `pochhammer`, but much less general. -/ def desc_factorial (n : ℕ) : ℕ → ℕ | 0 := 1 | (k + 1) := (n - k) * desc_factorial k @[simp] lemma desc_factorial_zero (n : ℕ) : n.desc_factorial 0 = 1 := rfl @[simp] lemma desc_factorial_succ (n k : ℕ) : n.desc_factorial k.succ = (n - k) * n.desc_factorial k := rfl lemma zero_desc_factorial_succ (k : ℕ) : (0 : ℕ).desc_factorial k.succ = 0 := by rw [desc_factorial_succ, zero_tsub, zero_mul] @[simp] lemma desc_factorial_one (n : ℕ) : n.desc_factorial 1 = n := by rw [desc_factorial_succ, desc_factorial_zero, mul_one, tsub_zero] @[simp] lemma succ_desc_factorial_succ (n : ℕ) : ∀ k : ℕ, (n + 1).desc_factorial (k + 1) = (n + 1) * n.desc_factorial k | 0 := by rw [desc_factorial_zero, desc_factorial_one, mul_one] | (succ k) := by rw [desc_factorial_succ, succ_desc_factorial_succ, desc_factorial_succ, succ_sub_succ, mul_left_comm] lemma succ_desc_factorial (n : ℕ) : ∀ k, (n + 1 - k) * (n + 1).desc_factorial k = (n + 1) * n.desc_factorial k | 0 := by rw [tsub_zero, desc_factorial_zero, desc_factorial_zero] | (k + 1) := by rw [desc_factorial, succ_desc_factorial, desc_factorial_succ, succ_sub_succ, mul_left_comm] lemma desc_factorial_self : ∀ n : ℕ, n.desc_factorial n = n! | 0 := by rw [desc_factorial_zero, factorial_zero] | (succ n) := by rw [succ_desc_factorial_succ, desc_factorial_self, factorial_succ] @[simp] lemma desc_factorial_eq_zero_iff_lt {n : ℕ} : ∀ {k : ℕ}, n.desc_factorial k = 0 ↔ n < k | 0 := by simp only [desc_factorial_zero, nat.one_ne_zero, nat.not_lt_zero] | (succ k) := begin rw [desc_factorial_succ, mul_eq_zero, desc_factorial_eq_zero_iff_lt, lt_succ_iff, tsub_eq_zero_iff_le, lt_iff_le_and_ne, or_iff_left_iff_imp, and_imp], exact λ h _, h, end alias nat.desc_factorial_eq_zero_iff_lt ↔ _ nat.desc_factorial_of_lt lemma add_desc_factorial_eq_asc_factorial (n : ℕ) : ∀ k : ℕ, (n + k).desc_factorial k = n.asc_factorial k | 0 := by rw [asc_factorial_zero, desc_factorial_zero] | (succ k) := by rw [nat.add_succ, succ_desc_factorial_succ, asc_factorial_succ, add_desc_factorial_eq_asc_factorial] /-- `n.desc_factorial k = n! / (n - k)!` but without ℕ-division. See `nat.desc_factorial_eq_div` for the version using ℕ-division. -/ theorem factorial_mul_desc_factorial : ∀ {n k : ℕ}, k ≤ n → (n - k)! * n.desc_factorial k = n! | n 0 := λ _, by rw [desc_factorial_zero, mul_one, tsub_zero] | 0 (succ k) := λ h, by { exfalso, exact not_succ_le_zero k h } | (succ n) (succ k) := λ h, by rw [succ_desc_factorial_succ, succ_sub_succ, ←mul_assoc, mul_comm (n - k)!, mul_assoc, factorial_mul_desc_factorial (nat.succ_le_succ_iff.1 h), factorial_succ] /-- Avoid in favor of `nat.factorial_mul_desc_factorial` if you can. ℕ-division isn't worth it. -/ lemma desc_factorial_eq_div {n k : ℕ} (h : k ≤ n) : n.desc_factorial k = n! / (n - k)! := begin apply mul_left_cancel₀ (factorial_ne_zero (n - k)), rw factorial_mul_desc_factorial h, exact (nat.mul_div_cancel' $ factorial_dvd_factorial $ nat.sub_le n k).symm, end lemma pow_sub_le_desc_factorial (n : ℕ) : ∀ (k : ℕ), (n + 1 - k)^k ≤ n.desc_factorial k | 0 := by rw [desc_factorial_zero, pow_zero] | (k + 1) := begin rw [desc_factorial_succ, pow_succ, succ_sub_succ], exact nat.mul_le_mul_of_nonneg_left (le_trans (nat.pow_le_pow_of_le_left (tsub_le_tsub_right (le_succ _) _) k) (pow_sub_le_desc_factorial k)), end lemma pow_sub_lt_desc_factorial' {n : ℕ} : ∀ {k : ℕ}, k + 2 ≤ n → (n - (k + 1))^(k + 2) < n.desc_factorial (k + 2) | 0 := λ h, begin rw [desc_factorial_succ, pow_succ, pow_one, desc_factorial_one], exact nat.mul_lt_mul_of_pos_left (tsub_lt_self (lt_of_lt_of_le zero_lt_two h) zero_lt_one) (tsub_pos_of_lt h), end | (k + 1) := λ h, begin rw [desc_factorial_succ, pow_succ], refine nat.mul_lt_mul_of_pos_left ((nat.pow_le_pow_of_le_left (tsub_le_tsub_right (le_succ n) _) _).trans_lt _) (tsub_pos_of_lt h), rw succ_sub_succ, exact (pow_sub_lt_desc_factorial' ((le_succ _).trans h)), end lemma pow_sub_lt_desc_factorial {n : ℕ} : ∀ {k : ℕ}, 2 ≤ k → k ≤ n → (n + 1 - k)^k < n.desc_factorial k | 0 := by rintro ⟨⟩ | 1 := by rintro (_ | ⟨_, ⟨⟩⟩) | (k + 2) := λ _ h, by { rw succ_sub_succ, exact pow_sub_lt_desc_factorial' h } lemma desc_factorial_le_pow (n : ℕ) : ∀ (k : ℕ), n.desc_factorial k ≤ n^k | 0 := by rw [desc_factorial_zero, pow_zero] | (k + 1) := begin rw [desc_factorial_succ, pow_succ], exact nat.mul_le_mul (nat.sub_le _ _) (desc_factorial_le_pow k), end lemma desc_factorial_lt_pow {n : ℕ} (hn : 1 ≤ n) : ∀ {k : ℕ}, 2 ≤ k → n.desc_factorial k < n^k | 0 := by rintro ⟨⟩ | 1 := by rintro (_ | ⟨_, ⟨⟩⟩) | (k + 2) := λ _, begin rw [desc_factorial_succ, pow_succ', mul_comm], exact nat.mul_lt_mul' (desc_factorial_le_pow _ _) (tsub_lt_self hn k.zero_lt_succ) (pow_pos hn _), end end desc_factorial end nat
f9f9be84f03f75ee6190191dd370ca9bb3a54d91
3ed5a65c1ab3ce5d1a094edce8fa3287980f197b
/src/herstein/ex1_1/Q_01.lean
32a1f9b8670831d736cc148de6cef2c7b7e746e7
[]
no_license
group-study-group/herstein
35d32e77158efa2cc303c84e1ee5e3bc80831137
f5a1a72eb56fa19c19ece0cb3ab6cf7ffd161f66
refs/heads/master
1,586,202,191,519
1,548,969,759,000
1,548,969,759,000
157,746,953
0
0
null
1,542,412,901,000
1,542,302,366,000
Lean
UTF-8
Lean
false
false
504
lean
import data.set.basic variable α: Type* variables A B C: set α open set theorem Q_01a: A ⊆ B ∧ B ⊆ C → A ⊆ C := λ ⟨hl, hr⟩ a ha, hr (hl ha) theorem Q_01b₁: B ⊆ A → A ∪ B = A := λ h, ext $ λ x, ⟨λ hab, or.elim hab (λ ha, ha) (λ hb, h hb), or.inl⟩ theorem Q_01c₁: B ⊆ A → B ∪ C ⊆ A ∪ C := λ h x hbc, or.elim hbc (λ hb, or.inl (h hb)) (λ hc, or.inr hc) theorem Q_01c₂: B ⊆ A → B ∩ C ⊆ A ∩ C := λ h x ⟨hb, hc⟩, ⟨h hb , hc⟩
3ac70a059c4531c14bcc1eebcca4bd64ffa9f315
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/stage0/src/Lean/Parser/Tactic.lean
23e865ded12d1b03aa935a64492238743b3c619d
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,040
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Parser.Term namespace Lean namespace Parser namespace Tactic def underscoreFn : ParserFn := fun c s => let s := symbolFn "_" c s; let stx := s.stxStack.back; let s := s.popSyntax; s.pushSyntax $ mkIdentFrom stx `_ @[inline] def underscore : Parser := { fn := underscoreFn, info := mkAtomicInfo "ident" } @[combinatorParenthesizer underscore] def underscore.parenthesizer := PrettyPrinter.Parenthesizer.rawIdent.parenthesizer @[combinatorFormatter underscore] def underscore.formatter := PrettyPrinter.Formatter.rawIdent.formatter def ident' : Parser := ident <|> underscore @[builtinTacticParser] def «intro» := parser! nonReservedSymbol "intro " >> notFollowedBy "|" >> many (termParser maxPrec) @[builtinTacticParser] def «intros» := parser! nonReservedSymbol "intros " >> many ident' @[builtinTacticParser] def «revert» := parser! nonReservedSymbol "revert " >> many1 ident @[builtinTacticParser] def «clear» := parser! nonReservedSymbol "clear " >> many1 ident @[builtinTacticParser] def «subst» := parser! nonReservedSymbol "subst " >> many1 ident @[builtinTacticParser] def «assumption» := parser! nonReservedSymbol "assumption" @[builtinTacticParser] def «apply» := parser! nonReservedSymbol "apply " >> termParser @[builtinTacticParser] def «exact» := parser! nonReservedSymbol "exact " >> termParser @[builtinTacticParser] def «refine» := parser! nonReservedSymbol "refine " >> termParser @[builtinTacticParser] def «refine!» := parser! nonReservedSymbol "refine! " >> termParser @[builtinTacticParser] def «case» := parser! nonReservedSymbol "case " >> ident >> tacticParser @[builtinTacticParser] def «allGoals» := parser! nonReservedSymbol "allGoals " >> tacticParser @[builtinTacticParser] def «skip» := parser! nonReservedSymbol "skip" @[builtinTacticParser] def «traceState» := parser! nonReservedSymbol "traceState" @[builtinTacticParser] def «failIfSuccess» := parser! nonReservedSymbol "failIfSuccess " >> tacticParser @[builtinTacticParser] def «generalize» := parser! nonReservedSymbol "generalize " >> optional (try (ident >> " : ")) >> termParser 51 >> " = " >> ident def location : Parser := "at " >> ("*" <|> (many ident >> optional (unicodeSymbol "⊢" "|-"))) @[builtinTacticParser] def change := parser! nonReservedSymbol "change " >> termParser >> optional location @[builtinTacticParser] def changeWith := parser! nonReservedSymbol "change " >> termParser >> " with " >> termParser >> optional location def majorPremise := parser! optional (try (ident >> " : ")) >> termParser def holeOrTactic := Term.hole <|> Term.syntheticHole <|> tacticParser def inductionAlt : Parser := nodeWithAntiquot "inductionAlt" `Lean.Parser.Tactic.inductionAlt $ ident' >> many ident' >> darrow >> holeOrTactic def inductionAlts : Parser := withPosition $ fun pos => "|" >> sepBy1 inductionAlt (checkColGe pos.column "alternatives must be indented" >> "|") def withAlts : Parser := optional (" with " >> inductionAlts) def usingRec : Parser := optional (" using " >> ident) def generalizingVars := optional (" generalizing " >> many1 ident) @[builtinTacticParser] def «induction» := parser! nonReservedSymbol "induction " >> majorPremise >> usingRec >> generalizingVars >> withAlts @[builtinTacticParser] def «cases» := parser! nonReservedSymbol "cases " >> majorPremise >> withAlts def withIds : Parser := optional (" with " >> many1 ident') def matchAlt : Parser := parser! sepBy1 termParser ", " >> darrow >> holeOrTactic def matchAlts : Parser := group $ withPosition $ fun pos => (optional "| ") >> sepBy1 matchAlt (checkColGe pos.column "alternatives must be indented" >> "|") @[builtinTacticParser] def «match» := parser! nonReservedSymbol "match " >> sepBy1 Term.matchDiscr ", " >> Term.optType >> " with " >> matchAlts @[builtinTacticParser] def «introMatch» := parser! nonReservedSymbol "intro " >> matchAlts @[builtinTacticParser] def «injection» := parser! nonReservedSymbol "injection " >> termParser >> withIds @[builtinTacticParser] def paren := parser! "(" >> nonEmptySeq >> ")" @[builtinTacticParser] def nestedTacticBlockCurly := parser! "{" >> seq >> "}" @[builtinTacticParser] def orelse := tparser!:2 " <|> " >> tacticParser 1 /- Term binders as tactics. They are all implemented as macros using the triad: named holes, hygiene, and `refine` tactic. -/ @[builtinTacticParser] def «have» := parser! "have " >> Term.haveDecl @[builtinTacticParser] def «suffices» := parser! "suffices " >> Term.sufficesDecl @[builtinTacticParser] def «show» := parser! "show " >> termParser @[builtinTacticParser] def «let» := parser! "let " >> Term.letDecl @[builtinTacticParser] def «let!» := parser! "let! " >> Term.letDecl end Tactic end Parser end Lean
9d9c32d6d00cd3abad0a1e2b08c7856a49e32f67
271e26e338b0c14544a889c31c30b39c989f2e0f
/src/Init/Data/RBMap/Basic.lean
e2a87a9614c287947475e9cdfd0735872d626d04
[ "Apache-2.0" ]
permissive
dgorokho/lean4
805f99b0b60c545b64ac34ab8237a8504f89d7d4
e949a052bad59b1c7b54a82d24d516a656487d8a
refs/heads/master
1,607,061,363,851
1,578,006,086,000
1,578,006,086,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,748
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Data.Repr import Init.Data.Option.Basic universes u v w w' inductive Rbcolor | red | black inductive RBNode (α : Type u) (β : α → Type v) | leaf {} : RBNode | node (color : Rbcolor) (lchild : RBNode) (key : α) (val : β key) (rchild : RBNode) : RBNode namespace RBNode variables {α : Type u} {β : α → Type v} {σ : Type w} open Rbcolor Nat def depth (f : Nat → Nat → Nat) : RBNode α β → Nat | leaf => 0 | node _ l _ _ r => succ (f (depth l) (depth r)) protected def min : RBNode α β → Option (Sigma (fun k => β k)) | leaf => none | node _ leaf k v _ => some ⟨k, v⟩ | node _ l k v _ => min l protected def max : RBNode α β → Option (Sigma (fun k => β k)) | leaf => none | node _ _ k v leaf => some ⟨k, v⟩ | node _ _ k v r => max r @[specialize] def fold (f : σ → ∀ (k : α), β k → σ) : σ → RBNode α β → σ | b, leaf => b | b, node _ l k v r => fold (f (fold b l) k v) r @[specialize] def mfold {m : Type w → Type w'} [Monad m] (f : σ → ∀ (k : α), β k → m σ) : σ → RBNode α β → m σ | b, leaf => pure b | b, node _ l k v r => do b ← mfold b l; b ← f b k v; mfold b r @[specialize] def revFold (f : σ → ∀ (k : α), β k → σ) : σ → RBNode α β → σ | b, leaf => b | b, node _ l k v r => revFold (f (revFold b r) k v) l @[specialize] def all (p : ∀ k, β k → Bool) : RBNode α β → Bool | leaf => true | node _ l k v r => p k v && all l && all r @[specialize] def any (p : ∀ k, β k → Bool) : RBNode α β → Bool | leaf => false | node _ l k v r => p k v || any l || any r def singleton (k : α) (v : β k) : RBNode α β := node red leaf k v leaf @[inline] def balance1 : ∀ a, β a → RBNode α β → RBNode α β → RBNode α β | kv, vv, t, node _ (node red l kx vx r₁) ky vy r₂ => node red (node black l kx vx r₁) ky vy (node black r₂ kv vv t) | kv, vv, t, node _ l₁ ky vy (node red l₂ kx vx r) => node red (node black l₁ ky vy l₂) kx vx (node black r kv vv t) | kv, vv, t, node _ l ky vy r => node black (node red l ky vy r) kv vv t | _, _, _, _ => leaf -- unreachable @[inline] def balance2 : RBNode α β → ∀ a, β a → RBNode α β → RBNode α β | t, kv, vv, node _ (node red l kx₁ vx₁ r₁) ky vy r₂ => node red (node black t kv vv l) kx₁ vx₁ (node black r₁ ky vy r₂) | t, kv, vv, node _ l₁ ky vy (node red l₂ kx₂ vx₂ r₂) => node red (node black t kv vv l₁) ky vy (node black l₂ kx₂ vx₂ r₂) | t, kv, vv, node _ l ky vy r => node black t kv vv (node red l ky vy r) | _, _, _, _ => leaf -- unreachable def isRed : RBNode α β → Bool | node red _ _ _ _ => true | _ => false def isBlack : RBNode α β → Bool | node black _ _ _ _ => true | _ => false section Insert variables (lt : α → α → Bool) @[specialize] def ins : RBNode α β → ∀ k, β k → RBNode α β | leaf, kx, vx => node red leaf kx vx leaf | node red a ky vy b, kx, vx => if lt kx ky then node red (ins a kx vx) ky vy b else if lt ky kx then node red a ky vy (ins b kx vx) else node red a kx vx b | node black a ky vy b, kx, vx => if lt kx ky then if isRed a then balance1 ky vy b (ins a kx vx) else node black (ins a kx vx) ky vy b else if lt ky kx then if isRed b then balance2 a ky vy (ins b kx vx) else node black a ky vy (ins b kx vx) else node black a kx vx b def setBlack : RBNode α β → RBNode α β | node _ l k v r => node black l k v r | e => e @[specialize] def insert (t : RBNode α β) (k : α) (v : β k) : RBNode α β := if isRed t then setBlack (ins lt t k v) else ins lt t k v end Insert def balance₃ : RBNode α β → ∀ k, β k → RBNode α β → RBNode α β | node red (node red a kx vx b) ky vy c, k, v, d => node red (node black a kx vx b) ky vy (node black c k v d) | node red a kx vx (node red b ky vy c), k, v, d => node red (node black a kx vx b) ky vy (node black c k v d) | a, k, v, node red b ky vy (node red c kz vz d) => node red (node black a k v b) ky vy (node black c kz vz d) | a, k, v, node red (node red b ky vy c) kz vz d => node red (node black a k v b) ky vy (node black c kz vz d) | l, k, v, r => node black l k v r def setRed : RBNode α β → RBNode α β | node _ a k v b => node red a k v b | e => e def balLeft : RBNode α β → ∀ k, β k → RBNode α β → RBNode α β | node red a kx vx b, k, v, r => node red (node black a kx vx b) k v r | l, k, v, node black a ky vy b => balance₃ l k v (node red a ky vy b) | l, k, v, node red (node black a ky vy b) kz vz c => node red (node black l k v a) ky vy (balance₃ b kz vz (setRed c)) | l, k, v, r => node red l k v r -- unreachable def balRight (l : RBNode α β) (k : α) (v : β k) (r : RBNode α β) : RBNode α β := match r with | (node red b ky vy c) => node red l k v (node black b ky vy c) | _ => match l with | node black a kx vx b => balance₃ (node red a kx vx b) k v r | node red a kx vx (node black b ky vy c) => node red (balance₃ (setRed a) kx vx b) ky vy (node black c k v r) | _ => node red l k v r -- unreachable -- TODO: use wellfounded recursion partial def appendTrees : RBNode α β → RBNode α β → RBNode α β | leaf, x => x | x, leaf => x | node red a kx vx b, node red c ky vy d => match appendTrees b c with | node red b' kz vz c' => node red (node red a kx vx b') kz vz (node red c' ky vy d) | bc => node red a kx vx (node red bc ky vy d) | node black a kx vx b, node black c ky vy d => match appendTrees b c with | node red b' kz vz c' => node red (node black a kx vx b') kz vz (node black c' ky vy d) | bc => balLeft a kx vx (node black bc ky vy d) | a, node red b kx vx c => node red (appendTrees a b) kx vx c | node red a kx vx b, c => node red a kx vx (appendTrees b c) section Erase variables (lt : α → α → Bool) @[specialize] def del (x : α) : RBNode α β → RBNode α β | leaf => leaf | node _ a y v b => if lt x y then if a.isBlack then balLeft (del a) y v b else node red (del a) y v b else if lt y x then if b.isBlack then balRight a y v (del b) else node red a y v (del b) else appendTrees a b @[specialize] def erase (x : α) (t : RBNode α β) : RBNode α β := let t := del lt x t; t.setBlack end Erase section Membership variable (lt : α → α → Bool) @[specialize] def findCore : RBNode α β → ∀ (k : α), Option (Sigma (fun k => β k)) | leaf, x => none | node _ a ky vy b, x => if lt x ky then findCore a x else if lt ky x then findCore b x else some ⟨ky, vy⟩ @[specialize] def find {β : Type v} : RBNode α (fun _ => β) → α → Option β | leaf, x => none | node _ a ky vy b, x => if lt x ky then find a x else if lt ky x then find b x else some vy @[specialize] def lowerBound : RBNode α β → α → Option (Sigma β) → Option (Sigma β) | leaf, x, lb => lb | node _ a ky vy b, x, lb => if lt x ky then lowerBound a x lb else if lt ky x then lowerBound b x (some ⟨ky, vy⟩) else some ⟨ky, vy⟩ end Membership inductive WellFormed (lt : α → α → Bool) : RBNode α β → Prop | leafWff : WellFormed leaf | insertWff {n n' : RBNode α β} {k : α} {v : β k} : WellFormed n → n' = insert lt n k v → WellFormed n' | eraseWff {n n' : RBNode α β} {k : α} : WellFormed n → n' = erase lt k n → WellFormed n' end RBNode open RBNode /- TODO(Leo): define dRBMap -/ def RBMap (α : Type u) (β : Type v) (lt : α → α → Bool) : Type (max u v) := {t : RBNode α (fun _ => β) // t.WellFormed lt } @[inline] def mkRBMap (α : Type u) (β : Type v) (lt : α → α → Bool) : RBMap α β lt := ⟨leaf, WellFormed.leafWff lt⟩ @[inline] def RBMap.empty {α : Type u} {β : Type v} {lt : α → α → Bool} : RBMap α β lt := mkRBMap _ _ _ instance (α : Type u) (β : Type v) (lt : α → α → Bool) : HasEmptyc (RBMap α β lt) := ⟨RBMap.empty⟩ namespace RBMap variables {α : Type u} {β : Type v} {σ : Type w} {lt : α → α → Bool} def depth (f : Nat → Nat → Nat) (t : RBMap α β lt) : Nat := t.val.depth f @[inline] def fold (f : σ → α → β → σ) : σ → RBMap α β lt → σ | b, ⟨t, _⟩ => t.fold f b @[inline] def revFold (f : σ → α → β → σ) : σ → RBMap α β lt → σ | b, ⟨t, _⟩ => t.revFold f b @[inline] def mfold {m : Type w → Type w'} [Monad m] (f : σ → α → β → m σ) : σ → RBMap α β lt → m σ | b, ⟨t, _⟩ => t.mfold f b @[inline] def mfor {m : Type w → Type w'} [Monad m] (f : α → β → m σ) (t : RBMap α β lt) : m PUnit := t.mfold (fun _ k v => f k v *> pure ⟨⟩) ⟨⟩ @[inline] def isEmpty : RBMap α β lt → Bool | ⟨leaf, _⟩ => true | _ => false @[specialize] def toList : RBMap α β lt → List (α × β) | ⟨t, _⟩ => t.revFold (fun ps k v => (k, v)::ps) [] @[inline] protected def min : RBMap α β lt → Option (α × β) | ⟨t, _⟩ => match t.min with | some ⟨k, v⟩ => some (k, v) | none => none @[inline] protected def max : RBMap α β lt → Option (α × β) | ⟨t, _⟩ => match t.max with | some ⟨k, v⟩ => some (k, v) | none => none instance [HasRepr α] [HasRepr β] : HasRepr (RBMap α β lt) := ⟨fun t => "rbmapOf " ++ repr t.toList⟩ @[inline] def insert : RBMap α β lt → α → β → RBMap α β lt | ⟨t, w⟩, k, v => ⟨t.insert lt k v, WellFormed.insertWff w rfl⟩ @[inline] def erase : RBMap α β lt → α → RBMap α β lt | ⟨t, w⟩, k => ⟨t.erase lt k, WellFormed.eraseWff w rfl⟩ @[specialize] def ofList : List (α × β) → RBMap α β lt | [] => mkRBMap _ _ _ | ⟨k,v⟩::xs => (ofList xs).insert k v @[inline] def findCore : RBMap α β lt → α → Option (Sigma (fun (k : α) => β)) | ⟨t, _⟩, x => t.findCore lt x @[inline] def find : RBMap α β lt → α → Option β | ⟨t, _⟩, x => t.find lt x @[inline] def findD (t : RBMap α β lt) (k : α) (v₀ : β) : β := (t.find k).getD v₀ /-- (lowerBound k) retrieves the kv pair of the largest key smaller than or equal to `k`, if it exists. -/ @[inline] def lowerBound : RBMap α β lt → α → Option (Sigma (fun (k : α) => β)) | ⟨t, _⟩, x => t.lowerBound lt x none @[inline] def contains (t : RBMap α β lt) (a : α) : Bool := (t.find a).isSome @[inline] def fromList (l : List (α × β)) (lt : α → α → Bool) : RBMap α β lt := l.foldl (fun r p => r.insert p.1 p.2) (mkRBMap α β lt) @[inline] def all : RBMap α β lt → (α → β → Bool) → Bool | ⟨t, _⟩, p => t.all p @[inline] def any : RBMap α β lt → (α → β → Bool) → Bool | ⟨t, _⟩, p => t.any p def size (m : RBMap α β lt) : Nat := m.fold (fun sz _ _ => sz+1) 0 def maxDepth (t : RBMap α β lt) : Nat := t.val.depth Nat.max end RBMap def rbmapOf {α : Type u} {β : Type v} (l : List (α × β)) (lt : α → α → Bool) : RBMap α β lt := RBMap.fromList l lt
aad8051fff419e871d64a0efa7bebf217ab00143
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Server/FileWorker.lean
207e2489fffd41b4792d952bb1d60b4c3b07cf3c
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
21,762
lean
/- Copyright (c) 2020 Marc Huisinga. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Marc Huisinga, Wojciech Nawrocki -/ import Init.System.IO import Std.Data.RBMap import Lean.Environment import Lean.Data.Lsp import Lean.Data.Json.FromToJson import Lean.Util.Paths import Lean.Server.Utils import Lean.Server.Snapshots import Lean.Server.AsyncList import Lean.Server.FileWorker.Utils import Lean.Server.FileWorker.RequestHandling import Lean.Server.FileWorker.WidgetRequests import Lean.Server.Rpc.Basic import Lean.Widget.InteractiveDiagnostic /-! For general server architecture, see `README.md`. For details of IPC communication, see `Watchdog.lean`. This module implements per-file worker processes. File processing and requests+notifications against a file should be concurrent for two reasons: - By the LSP standard, requests should be cancellable. - Since Lean allows arbitrary user code to be executed during elaboration via the tactic framework, elaboration can be extremely slow and even not halt in some cases. Users should be able to work with the file while this is happening, e.g. make new changes to the file or send requests. To achieve these goals, elaboration is executed in a chain of tasks, where each task corresponds to the elaboration of one command. When the elaboration of one command is done, the next task is spawned. On didChange notifications, we search for the task in which the change occured. If we stumble across a task that has not yet finished before finding the task we're looking for, we terminate it and start the elaboration there, otherwise we start the elaboration at the task where the change occured. Requests iterate over tasks until they find the command that they need to answer the request. In order to not block the main thread, this is done in a request task. If a task that the request task waits for is terminated, a change occured somewhere before the command that the request is looking for and the request sends a "content changed" error. -/ namespace Lean.Server.FileWorker open Lsp open IO open Snapshots open Std (RBMap RBMap.empty) open JsonRpc structure WorkerContext where hIn : FS.Stream hOut : FS.Stream hLog : FS.Stream srcSearchPath : SearchPath /- Asynchronous snapshot elaboration. -/ section Elab abbrev AsyncElabM := ExceptT ElabTaskError (ReaderT WorkerContext IO) /-- Elaborates the next command after `parentSnap` and emits diagnostics into `hOut`. -/ private def nextCmdSnap (m : DocumentMeta) (parentSnap : Snapshot) (cancelTk : CancelToken) : AsyncElabM Snapshot := do cancelTk.check let hOut := (←read).hOut if parentSnap.isAtEnd then publishDiagnostics m parentSnap.diagnostics.toArray hOut publishProgressDone m hOut throw ElabTaskError.eof publishProgressAtPos m parentSnap.endPos hOut let snap ← compileNextCmd m.text parentSnap -- TODO(MH): check for interrupt with increased precision cancelTk.check /- NOTE(MH): This relies on the client discarding old diagnostics upon receiving new ones while prefering newer versions over old ones. The former is necessary because we do not explicitly clear older diagnostics, while the latter is necessary because we do not guarantee that diagnostics are emitted in order. Specifically, it may happen that we interrupted this elaboration task right at this point and a newer elaboration task emits diagnostics, after which we emit old diagnostics because we did not yet detect the interrupt. Explicitly clearing diagnostics is difficult for a similar reason, because we cannot guarantee that no further diagnostics are emitted after clearing them. -/ -- NOTE(WN): this is *not* redundent even if there are no new diagnostics in this snapshot -- because empty diagnostics clear existing error/information squiggles. Therefore we always -- want to publish in case there was previously a message at this position. publishDiagnostics m snap.diagnostics.toArray hOut return snap /-- Elaborates all commands after `initSnap`, emitting the diagnostics into `hOut`. -/ def unfoldCmdSnaps (m : DocumentMeta) (initSnap : Snapshot) (cancelTk : CancelToken) (initial : Bool) : ReaderT WorkerContext IO (AsyncList ElabTaskError Snapshot) := do if initial && initSnap.msgLog.hasErrors then -- treat header processing errors as fatal so users aren't swamped with followup errors let hOut := (←read).hOut publishProgressAtPos m initSnap.beginPos hOut (kind := LeanFileProgressKind.fatalError) AsyncList.nil else AsyncList.unfoldAsync (nextCmdSnap m . cancelTk (← read)) initSnap end Elab -- Pending requests are tracked so they can be cancelled abbrev PendingRequestMap := RBMap RequestID (Task (Except IO.Error Unit)) compare structure WorkerState where doc : EditableDocument pendingRequests : PendingRequestMap /-- A map of RPC session IDs. We allow asynchronous elab tasks and request handlers to modify sessions. A single `Ref` ensures atomic transactions. -/ rpcSessions : Std.RBMap UInt64 (IO.Ref RpcSession) compare abbrev WorkerM := ReaderT WorkerContext <| StateRefT WorkerState IO /- Worker initialization sequence. -/ section Initialization /-- Use `lake print-paths` to compile dependencies on the fly and add them to `LEAN_PATH`. Compilation progress is reported to `hOut` via LSP notifications. Return the search path for source files. -/ partial def lakeSetupSearchPath (lakePath : System.FilePath) (m : DocumentMeta) (imports : Array Import) (hOut : FS.Stream) : IO SearchPath := do let args := #["print-paths"] ++ imports.map (toString ·.module) let cmdStr := " ".intercalate (toString lakePath :: args.toList) let lakeProc ← Process.spawn { stdin := Process.Stdio.null stdout := Process.Stdio.piped stderr := Process.Stdio.piped cmd := lakePath.toString args } -- progress notification: report latest stderr line let rec processStderr (acc : String) : IO String := do let line ← lakeProc.stderr.getLine if line == "" then return acc else publishDiagnostics m #[{ range := ⟨⟨0, 0⟩, ⟨0, 0⟩⟩, severity? := DiagnosticSeverity.information, message := line }] hOut processStderr (acc ++ line) let stderr ← IO.asTask (processStderr "") Task.Priority.dedicated let stdout := String.trim (← lakeProc.stdout.readToEnd) let stderr ← IO.ofExcept stderr.get match (← lakeProc.wait) with | 0 => -- ignore any output up to the last line -- TODO: leanpkg should instead redirect nested stdout output to stderr let stdout := stdout.split (· == '\n') |>.getLast! let Except.ok (paths : LeanPaths) ← pure (Json.parse stdout >>= fromJson?) | throwServerError s!"invalid output from `{cmdStr}`:\n{stdout}\nstderr:\n{stderr}" initSearchPath (← getBuildDir) paths.oleanPath paths.srcPath.mapM realPathNormalized | 2 => pure [] -- no lakefile.lean | _ => throwServerError s!"`{cmdStr}` failed:\n{stdout}\nstderr:\n{stderr}" def compileHeader (m : DocumentMeta) (hOut : FS.Stream) (opts : Options) : IO (Snapshot × SearchPath) := do let inputCtx := Parser.mkInputContext m.text.source "<input>" let (headerStx, headerParserState, msgLog) ← Parser.parseHeader inputCtx let lakePath ← match (← IO.getEnv "LAKE") with | some path => System.FilePath.mk path | none => let lakePath := -- backward compatibility, kill when `leanpkg` is removed if (← System.FilePath.pathExists "leanpkg.toml") && !(← System.FilePath.pathExists "lakefile.lean") then "leanpkg" else "lake" let lakePath ← match (← IO.getEnv "LEAN_SYSROOT") with | some path => pure <| System.FilePath.mk path / "bin" / lakePath | _ => pure <| (← appDir) / lakePath lakePath.withExtension System.FilePath.exeExtension let srcPath := (← appDir) / ".." / "lib" / "lean" / "src" -- `lake/` should come first since on case-insensitive file systems, Lean thinks that `src/` also contains `Lake/` let mut srcSearchPath := [srcPath / "lake", srcPath] let (headerEnv, msgLog) ← try if let some path := m.uri.toPath? then -- NOTE: we assume for now that `lakefile.lean` does not have any non-stdlib deps -- NOTE: lake does not exist in stage 0 (yet?) if path.fileName != "lakefile.lean" && (← System.FilePath.pathExists lakePath) then let pkgSearchPath ← lakeSetupSearchPath lakePath m (Lean.Elab.headerToImports headerStx).toArray hOut srcSearchPath := pkgSearchPath ++ srcSearchPath Elab.processHeader headerStx opts msgLog inputCtx catch e => -- should be from `lake print-paths` let msgs := MessageLog.empty.add { fileName := "<ignored>", pos := ⟨0, 0⟩, data := e.toString } pure (← mkEmptyEnvironment, msgs) let mut headerEnv := headerEnv try if let some path := m.uri.toPath? then headerEnv := headerEnv.setMainModule (← moduleNameOfFileName path none) catch _ => () if let some p := (← IO.getEnv "LEAN_SRC_PATH") then srcSearchPath := System.SearchPath.parse p ++ srcSearchPath let cmdState := Elab.Command.mkState headerEnv msgLog opts let cmdState := { cmdState with infoState := { enabled := true -- add dummy tree for invariant in `Snapshot.infoTree` -- TODO: maybe even fill with useful stuff trees := #[Elab.InfoTree.node (Elab.Info.ofCommandInfo { elaborator := `import, stx := headerStx }) #[].toPersistentArray].toPersistentArray }} let headerSnap := { beginPos := 0 stx := headerStx mpState := headerParserState cmdState := cmdState interactiveDiags := ← cmdState.messages.msgs.mapM (Widget.msgToInteractiveDiagnostic m.text) } publishDiagnostics m headerSnap.diagnostics.toArray hOut return (headerSnap, srcSearchPath) def initializeWorker (meta : DocumentMeta) (i o e : FS.Stream) (opts : Options) : IO (WorkerContext × WorkerState) := do let (headerSnap, srcSearchPath) ← compileHeader meta o opts let cancelTk ← CancelToken.new let ctx := { hIn := i hOut := o hLog := e srcSearchPath := srcSearchPath } let cmdSnaps ← unfoldCmdSnaps meta headerSnap cancelTk (initial := true) ctx let doc : EditableDocument := ⟨meta, headerSnap, cmdSnaps, cancelTk⟩ return (ctx, { doc := doc pendingRequests := RBMap.empty rpcSessions := Std.RBMap.empty }) end Initialization section Updates def updatePendingRequests (map : PendingRequestMap → PendingRequestMap) : WorkerM Unit := do modify fun st => { st with pendingRequests := map st.pendingRequests } /-- Given the new document, updates editable doc state. -/ def updateDocument (newMeta : DocumentMeta) : WorkerM Unit := do -- The watchdog only restarts the file worker when the syntax tree of the header changes. -- If e.g. a newline is deleted, it will not restart this file worker, but we still -- need to reparse the header so that the offsets are correct. let ctx ← read let oldDoc := (←get).doc let newHeaderSnap ← reparseHeader newMeta.text.source oldDoc.headerSnap if newHeaderSnap.stx != oldDoc.headerSnap.stx then throwServerError "Internal server error: header changed but worker wasn't restarted." let ⟨cmdSnaps, e?⟩ ← oldDoc.cmdSnaps.updateFinishedPrefix match e? with -- This case should not be possible. only the main task aborts tasks and ensures that aborted tasks -- do not show up in `snapshots` of an EditableDocument. | some ElabTaskError.aborted => throwServerError "Internal server error: elab task was aborted while still in use." | some (ElabTaskError.ioError ioError) => throw ioError | _ => -- No error or EOF oldDoc.cancelTk.set let changePos := oldDoc.meta.text.source.firstDiffPos newMeta.text.source -- NOTE(WN): we invalidate eagerly as `endPos` consumes input greedily. To re-elaborate only -- when really necessary, we could do a whitespace-aware `Syntax` comparison instead. let mut validSnaps := cmdSnaps.finishedPrefix.takeWhile (fun s => s.endPos < changePos) if validSnaps.length = 0 then let cancelTk ← CancelToken.new let newCmdSnaps ← unfoldCmdSnaps newMeta newHeaderSnap cancelTk (initial := true) ctx modify fun st => { st with doc := ⟨newMeta, newHeaderSnap, newCmdSnaps, cancelTk⟩ } else /- When at least one valid non-header snap exists, it may happen that a change does not fall within the syntactic range of that last snap but still modifies it by appending tokens. We check for this here. We do not currently handle crazy grammars in which an appended token can merge two or more previous commands into one. To do so would require reparsing the entire file. -/ let mut lastSnap := validSnaps.getLast! let preLastSnap := if validSnaps.length ≥ 2 then validSnaps.get! (validSnaps.length - 2) else newHeaderSnap let newLastStx ← parseNextCmd newMeta.text.source preLastSnap if newLastStx != lastSnap.stx then validSnaps ← validSnaps.dropLast lastSnap ← preLastSnap let cancelTk ← CancelToken.new let newSnaps ← unfoldCmdSnaps newMeta lastSnap cancelTk (initial := false) ctx let newCmdSnaps := AsyncList.ofList validSnaps ++ newSnaps modify fun st => { st with doc := ⟨newMeta, newHeaderSnap, newCmdSnaps, cancelTk⟩ } end Updates /- Notifications are handled in the main thread. They may change global worker state such as the current file contents. -/ section NotificationHandling def handleDidChange (p : DidChangeTextDocumentParams) : WorkerM Unit := do let docId := p.textDocument let changes := p.contentChanges let oldDoc := (←get).doc let some newVersion ← pure docId.version? | throwServerError "Expected version number" if newVersion ≤ oldDoc.meta.version then -- TODO(WN): This happens on restart sometimes. IO.eprintln s!"Got outdated version number: {newVersion} ≤ {oldDoc.meta.version}" else if ¬ changes.isEmpty then let newDocText := foldDocumentChanges changes oldDoc.meta.text updateDocument ⟨docId.uri, newVersion, newDocText⟩ def handleCancelRequest (p : CancelParams) : WorkerM Unit := do updatePendingRequests (fun pendingRequests => pendingRequests.erase p.id) def handleRpcRelease (p : Lsp.RpcReleaseParams) : WorkerM Unit := do let st ← get -- NOTE(WN): when the worker restarts e.g. due to changed imports, we may receive `rpc/release` -- for the previous RPC session. This is fine, just ignore. if let some seshRef := st.rpcSessions.find? p.sessionId then let mut sesh ← seshRef.get for ref in p.refs do sesh := sesh.release ref |>.snd sesh ← sesh.keptAlive seshRef.set sesh def handleRpcKeepAlive (p : Lsp.RpcKeepAliveParams) : WorkerM Unit := do let st ← get match st.rpcSessions.find? p.sessionId with | none => return | some seshRef => let sesh ← seshRef.get let sesh ← sesh.keptAlive seshRef.set sesh end NotificationHandling /-! Requests here are handled synchronously rather than in the asynchronous `RequestM`. -/ section RequestHandling def handleRpcConnect (p : RpcConnectParams) : WorkerM RpcConnected := do let (newId, newSesh) ← RpcSession.new let newSeshRef ← IO.mkRef newSesh modify fun st => { st with rpcSessions := st.rpcSessions.insert newId newSeshRef } return { sessionId := newId } end RequestHandling section MessageHandling def parseParams (paramType : Type) [FromJson paramType] (params : Json) : WorkerM paramType := match fromJson? params with | Except.ok parsed => pure parsed | Except.error inner => throwServerError s!"Got param with wrong structure: {params.compress}\n{inner}" def handleNotification (method : String) (params : Json) : WorkerM Unit := do let handle := fun paramType [FromJson paramType] (handler : paramType → WorkerM Unit) => parseParams paramType params >>= handler match method with | "textDocument/didChange" => handle DidChangeTextDocumentParams handleDidChange | "$/cancelRequest" => handle CancelParams handleCancelRequest | "$/lean/rpc/release" => handle RpcReleaseParams handleRpcRelease | "$/lean/rpc/keepAlive" => handle RpcKeepAliveParams handleRpcKeepAlive | _ => throwServerError s!"Got unsupported notification method: {method}" def queueRequest (id : RequestID) (requestTask : Task (Except IO.Error Unit)) : WorkerM Unit := do updatePendingRequests (fun pendingRequests => pendingRequests.insert id requestTask) def handleRequest (id : RequestID) (method : String) (params : Json) : WorkerM Unit := do let ctx ← read let st ← get if method == "$/lean/rpc/connect" then try let ps ← parseParams RpcConnectParams params let resp ← handleRpcConnect ps ctx.hOut.writeLspResponse ⟨id, resp⟩ catch e => ctx.hOut.writeLspResponseError { id code := ErrorCode.internalError message := toString e } return let rc : RequestContext := { rpcSessions := st.rpcSessions srcSearchPath := ctx.srcSearchPath doc := st.doc hLog := ctx.hLog } let t? ← EIO.toIO' <| handleLspRequest method params rc let t₁ ← match t? with | Except.error e => IO.asTask do ctx.hOut.writeLspResponseError <| e.toLspResponseError id | Except.ok t => (IO.mapTask · t) fun | Except.ok resp => ctx.hOut.writeLspResponse ⟨id, resp⟩ | Except.error e => ctx.hOut.writeLspResponseError <| e.toLspResponseError id queueRequest id t₁ end MessageHandling section MainLoop partial def mainLoop : WorkerM Unit := do let ctx ← read let mut st ← get let msg ← ctx.hIn.readLspMessage let filterFinishedTasks (acc : PendingRequestMap) (id : RequestID) (task : Task (Except IO.Error Unit)) : IO PendingRequestMap := do if (←hasFinished task) then /- Handler tasks are constructed so that the only possible errors here are failures of writing a response into the stream. -/ if let Except.error e := task.get then throwServerError s!"Failed responding to request {id}: {e}" acc.erase id else acc let pendingRequests ← st.pendingRequests.foldM (fun acc id task => filterFinishedTasks acc id task) st.pendingRequests st := { st with pendingRequests } -- Opportunistically (i.e. when we wake up on messages) check if any RPC session has expired. for (id, seshRef) in st.rpcSessions do let sesh ← seshRef.get if (← sesh.hasExpired) then st := { st with rpcSessions := st.rpcSessions.erase id } set st match msg with | Message.request id method (some params) => handleRequest id method (toJson params) mainLoop | Message.notification "exit" none => let doc ← (←get).doc doc.cancelTk.set return () | Message.notification method (some params) => handleNotification method (toJson params) mainLoop | _ => throwServerError "Got invalid JSON-RPC message" end MainLoop def initAndRunWorker (i o e : FS.Stream) (opts : Options) : IO UInt32 := do let i ← maybeTee "fwIn.txt" false i let o ← maybeTee "fwOut.txt" true o let _ ← i.readLspRequestAs "initialize" InitializeParams let ⟨_, param⟩ ← i.readLspNotificationAs "textDocument/didOpen" DidOpenTextDocumentParams let doc := param.textDocument /- NOTE(WN): `toFileMap` marks line beginnings as immediately following "\n", which should be enough to handle both LF and CRLF correctly. This is because LSP always refers to characters by (line, column), so if we get the line number correct it shouldn't matter that there is a CR there. -/ let meta : DocumentMeta := ⟨doc.uri, doc.version, doc.text.toFileMap⟩ let e ← e.withPrefix s!"[{param.textDocument.uri}] " let _ ← IO.setStderr e try let (ctx, st) ← initializeWorker meta i o e opts let _ ← StateRefT'.run (s := st) <| ReaderT.run (r := ctx) mainLoop return (0 : UInt32) catch e => IO.eprintln e publishDiagnostics meta #[{ range := ⟨⟨0, 0⟩, ⟨0, 0⟩⟩, severity? := DiagnosticSeverity.error, message := e.toString }] o return (1 : UInt32) @[export lean_server_worker_main] def workerMain (opts : Options) : IO UInt32 := do let i ← IO.getStdin let o ← IO.getStdout let e ← IO.getStderr try let seed ← (UInt64.toNat ∘ ByteArray.toUInt64LE!) <$> IO.getRandomBytes 8 IO.setRandSeed seed let exitCode ← initAndRunWorker i o e opts -- HACK: all `Task`s are currently "foreground", i.e. we join on them on main thread exit, but we definitely don't -- want to do that in the case of the worker processes, which can produce non-terminating tasks evaluating user code o.flush e.flush IO.Process.exit exitCode.toUInt8 catch err => e.putStrLn s!"worker initialization error: {err}" return (1 : UInt32) end Lean.Server.FileWorker
5683b65222d06dacd7270833bc2f7202b0fef45c
46125763b4dbf50619e8846a1371029346f4c3db
/src/tactic/tauto.lean
4a02bf0823d8a0d1a4eba24dd966805d95d50c92
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
8,875
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import logic.basic tactic.solve_by_elim tactic.hint namespace tactic open expr open tactic.interactive ( casesm constructor_matching ) /-- find all assumptions of the shape `¬ (p ∧ q)` or `¬ (p ∨ q)` and replace them using de Morgan's law. -/ meta def distrib_not : tactic unit := do hs ← local_context, hs.for_each $ λ h, all_goals $ iterate_at_most 3 $ do h ← get_local h.local_pp_name, e ← infer_type h, match e with | `(¬ _ = _) := replace h.local_pp_name ``(mt iff.to_eq %%h) | `(_ ≠ _) := replace h.local_pp_name ``(mt iff.to_eq %%h) | `(_ = _) := replace h.local_pp_name ``(eq.to_iff %%h) | `(¬ (_ ∧ _)) := replace h.local_pp_name ``(not_and_distrib'.mp %%h) <|> replace h.local_pp_name ``(not_and_distrib.mp %%h) | `(¬ (_ ∨ _)) := replace h.local_pp_name ``(not_or_distrib.mp %%h) | `(¬ ¬ _) := replace h.local_pp_name ``(of_not_not %%h) | `(¬ (_ → (_ : Prop))) := replace h.local_pp_name ``(not_imp.mp %%h) | `(¬ (_ ↔ _)) := replace h.local_pp_name ``(not_iff.mp %%h) | `(_ ↔ _) := replace h.local_pp_name ``(iff_iff_and_or_not_and_not.mp %%h) <|> replace h.local_pp_name ``(iff_iff_and_or_not_and_not.mp (%%h).symm) <|> () <$ tactic.cases h | `(_ → _) := replace h.local_pp_name ``(not_or_of_imp %%h) | _ := failed end meta def tauto_state := ref $ expr_map (option (expr × expr)) meta def modify_ref {α : Type} (r : ref α) (f : α → α) := read_ref r >>= write_ref r ∘ f meta def add_refl (r : tauto_state) (e : expr) : tactic (expr × expr) := do m ← read_ref r, p ← mk_mapp `rfl [none,e], write_ref r $ m.insert e none, return (e,p) meta def add_symm_proof (r : tauto_state) (e : expr) : tactic (expr × expr) := do env ← get_env, let rel := e.get_app_fn.const_name, some symm ← pure $ environment.symm_for env rel | add_refl r e, (do e' ← mk_meta_var `(Prop), iff_t ← to_expr ``(%%e = %%e'), (_,p) ← solve_aux iff_t (applyc `iff.to_eq ; () <$ split ; applyc symm), e' ← instantiate_mvars e', m ← read_ref r, write_ref r $ (m.insert e (e',p)).insert e' none, return (e',p) ) <|> add_refl r e meta def add_edge (r : tauto_state) (x y p : expr) : tactic unit := modify_ref r $ λ m, m.insert x (y,p) meta def root (r : tauto_state) : expr → tactic (expr × expr) | e := do m ← read_ref r, let record_e : tactic (expr × expr) := match e with | v@(expr.mvar _ _ _) := (do (e,p) ← get_assignment v >>= root, add_edge r v e p, return (e,p)) <|> add_refl r e | _ := add_refl r e end, some e' ← pure $ m.find e | record_e, match e' with | (some (e',p')) := do (e'',p'') ← root e', p'' ← mk_app `eq.trans [p',p''], add_edge r e e'' p'', pure (e'',p'') | none := prod.mk e <$> mk_mapp `rfl [none,some e] end meta def symm_eq (r : tauto_state) : expr → expr → tactic expr | a b := do m ← read_ref r, (a',pa) ← root r a, (b',pb) ← root r b, (unify a' b' >> add_refl r a' *> mk_mapp `rfl [none,a]) <|> do p ← match (a', b') with | (`(¬ %%a₀), `(¬ %%b₀)) := do p ← symm_eq a₀ b₀, p' ← mk_app `congr_arg [`(not),p], add_edge r a' b' p', return p' | (`(%%a₀ ∧ %%a₁), `(%%b₀ ∧ %%b₁)) := do p₀ ← symm_eq a₀ b₀, p₁ ← symm_eq a₁ b₁, p' ← to_expr ``(congr (congr_arg and %%p₀) %%p₁), add_edge r a' b' p', return p' | (`(%%a₀ ∨ %%a₁), `(%%b₀ ∨ %%b₁)) := do p₀ ← symm_eq a₀ b₀, p₁ ← symm_eq a₁ b₁, p' ← to_expr ``(congr (congr_arg or %%p₀) %%p₁), add_edge r a' b' p', return p' | (`(%%a₀ ↔ %%a₁), `(%%b₀ ↔ %%b₁)) := (do p₀ ← symm_eq a₀ b₀, p₁ ← symm_eq a₁ b₁, p' ← to_expr ``(congr (congr_arg iff %%p₀) %%p₁), add_edge r a' b' p', return p') <|> do p₀ ← symm_eq a₀ b₁, p₁ ← symm_eq a₁ b₀, p' ← to_expr ``(eq.trans (congr (congr_arg iff %%p₀) %%p₁) (iff.to_eq iff.comm ) ), add_edge r a' b' p', return p' | (`(%%a₀ → %%a₁), `(%%b₀ → %%b₁)) := if ¬ a₁.has_var ∧ ¬ b₁.has_var then do p₀ ← symm_eq a₀ b₀, p₁ ← symm_eq a₁ b₁, p' ← mk_app `congr_arg [`(implies),p₀,p₁], add_edge r a' b' p', return p' else unify a' b' >> add_refl r a' *> mk_mapp `rfl [none,a] | (_, _) := (do guard $ a'.get_app_fn.is_constant ∧ a'.get_app_fn.const_name = b'.get_app_fn.const_name, (a'',pa') ← add_symm_proof r a', guard $ a'' =ₐ b', pure pa' ) end, p' ← mk_eq_trans pa p, add_edge r a' b' p', mk_eq_symm pb >>= mk_eq_trans p' meta def find_eq_type (r : tauto_state) : expr → list expr → tactic (expr × expr) | e [] := failed | e (H :: Hs) := do t ← infer_type H, t' ← infer_type e, (prod.mk H <$> symm_eq r e t) <|> find_eq_type e Hs private meta def contra_p_not_p (r : tauto_state) : list expr → list expr → tactic unit | [] Hs := failed | (H1 :: Rs) Hs := do t ← (extract_opt_auto_param <$> infer_type H1) >>= whnf, (do a ← match_not t, (H2,p) ← find_eq_type r a Hs, H2 ← to_expr ``( (%%p).mpr %%H2 ), tgt ← target, pr ← mk_app `absurd [tgt, H2, H1], tactic.exact pr) <|> contra_p_not_p Rs Hs meta def contradiction_with (r : tauto_state) : tactic unit := contradiction <|> do tactic.try intro1, ctx ← local_context, contra_p_not_p r ctx ctx meta def contradiction_symm := using_new_ref (native.rb_map.mk _ _) contradiction_with meta def assumption_with (r : tauto_state) : tactic unit := do { ctx ← local_context, t ← target, (H,p) ← find_eq_type r t ctx, mk_eq_mpr p H >>= tactic.exact } <|> fail "assumption tactic failed" meta def assumption_symm := using_new_ref (native.rb_map.mk _ _) assumption_with meta def tautology (c : bool := ff) : tactic unit := do when c classical, using_new_ref (expr_map.mk _) $ λ r, do try (contradiction_with r); try (assumption_with r); repeat (do gs ← get_goals, repeat (() <$ tactic.intro1); distrib_not; casesm (some ()) [``(_ ∧ _),``(_ ∨ _),``(Exists _),``(false)]; try (contradiction_with r); try (target >>= match_or >> refine ``( or_iff_not_imp_left.mpr _)); try (target >>= match_or >> refine ``( or_iff_not_imp_right.mpr _)); repeat (() <$ tactic.intro1); constructor_matching (some ()) [``(_ ∧ _),``(_ ↔ _),``(true)]; try (assumption_with r), gs' ← get_goals, guard (gs ≠ gs') ) ; repeat (reflexivity <|> solve_by_elim <|> constructor_matching none [``(_ ∧ _),``(_ ↔ _),``(Exists _),``(true)] ) ; done open interactive lean.parser namespace interactive local postfix `?`:9001 := optional /-- `tautology` breaks down assumptions of the form `_ ∧ _`, `_ ∨ _`, `_ ↔ _` and `∃ _, _` and splits a goal of the form `_ ∧ _`, `_ ↔ _` or `∃ _, _` until it can be discharged using `reflexivity` or `solve_by_elim`. This is a finishing tactic: it either closes the goal or raises an error. The variant `tautology!` uses the law of excluded middle. -/ meta def tautology (c : parse $ (tk "!")?) := tactic.tautology c.is_some -- Now define a shorter name for the tactic `tautology`. /-- `tauto` breaks down assumptions of the form `_ ∧ _`, `_ ∨ _`, `_ ↔ _` and `∃ _, _` and splits a goal of the form `_ ∧ _`, `_ ↔ _` or `∃ _, _` until it can be discharged using `reflexivity` or `solve_by_elim`. This is a finishing tactic: it either closes the goal or raises an error. The variant `tauto!` uses the law of excluded middle. -/ meta def tauto (c : parse $ (tk "!")?) := tautology c add_hint_tactic "tauto" end interactive end tactic
ec29f214812455e10d9661739dc5c59993284ac3
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/new_inductive.lean
e9a31f347480229dacfef90ca9a95728e0b4bc33
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,051
lean
new_frontend universes u v inductive myList (α : Type u) | nil : myList α | cons : α → myList α → myList α inductive myPair (α : Type u) (β : Type v) | mk : α → β → myPair α β mutual variables (α : Type u) (m : α → Type v) inductive bla : Nat → Type (max u v) | mk₁ (n : Nat) : α → boo n → bla (n+1) | mk₂ (a : α) : m a → String → bla 0 inductive boo : Nat → Type (max u v) | mk₃ (n : Nat) : bla n → bla (n+1) → boo (n+2) end #print bla inductive Term (α : Type) (β : Type) | var : α → bla (Term α β) (fun _ => Term α β) 10 → Term α β | foo (p : Nat → myPair (Term α β) (myList $ Term α β)) (n : β) : myList (myList $ Term α β) → Term α β #print Term #print Term.below #check @Term.casesOn #print Term.noConfusionType #print Term.noConfusion inductive arrow (α β : Type) | mk (s : Nat → myPair α β) : arrow α β mutual inductive tst1 | mk : (arrow (myPair tst2 Bool) tst2) → tst1 inductive tst2 | mk : tst1 → tst2 end #check @tst1.casesOn #check @tst2.casesOn #check @tst1.recOn namespace test inductive Rbnode (α : Type u) | leaf : Rbnode α | redNode (lchild : Rbnode α) (val : α) (rchild : Rbnode α) : Rbnode α | blackNode (lchild : Rbnode α) (val : α) (rchild : Rbnode α) : Rbnode α #check @Rbnode.brecOn namespace Rbnode variables {α : Type u} constant insert (lt : α → α → Prop) [DecidableRel lt] (t : Rbnode α) (x : α) : Rbnode α := Rbnode.leaf inductive WellFormed (lt : α → α → Prop) : Rbnode α → Prop | leafWff : WellFormed lt leaf | insertWff {n n' : Rbnode α} {x : α} (s : DecidableRel lt) : WellFormed lt n → n' = insert lt n x → WellFormed lt n' end Rbnode def Rbtree (α : Type u) (lt : α → α → Prop) : Type u := {t : Rbnode α // t.WellFormed lt } inductive Trie | Empty : Trie | mk : Char → Rbnode (myPair Char Trie) → Trie #print Trie.rec #print Trie.noConfusion end test inductive Foo | mk : List Foo → Foo #check Foo
d7f1b0224cf140924679fb13880fb38aadf0fca2
9028d228ac200bbefe3a711342514dd4e4458bff
/src/linear_algebra/direct_sum_module.lean
10f0d11b9fd9bd285d803d164afad76bb4788530
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,949
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Direct sum of modules over commutative rings, indexed by a discrete type. -/ import algebra.direct_sum import linear_algebra.basic /-! # Direct sum of modules over commutative rings, indexed by a discrete type. This file provides constructors for finite direct sums of modules. It provides a construction of the direct sum using the universal property and proves its uniqueness. ## Implementation notes All of this file assumes that * `R` is a commutative ring, * `ι` is a discrete type, * `S` is a finite set in `ι`, * `M` is a family of `R` semimodules indexed over `ι`. -/ universes u v w u₁ variables (R : Type u) [semiring R] variables (ι : Type v) [dec_ι : decidable_eq ι] (M : ι → Type w) variables [Π i, add_comm_group (M i)] [Π i, semimodule R (M i)] include R namespace direct_sum open_locale direct_sum variables {R ι M} instance : semimodule R (⨁ i, M i) := dfinsupp.to_semimodule include dec_ι variables R ι M /-- Create the direct sum given a family `M` of `R` semimodules indexed over `ι`. -/ def lmk : Π s : finset ι, (Π i : (↑s : set ι), M i.val) →ₗ[R] (⨁ i, M i) := dfinsupp.lmk M R /-- Inclusion of each component into the direct sum. -/ def lof : Π i : ι, M i →ₗ[R] (⨁ i, M i) := dfinsupp.lsingle M R variables {ι M} lemma single_eq_lof (i : ι) (b : M i) : dfinsupp.single i b = lof R ι M i b := rfl /-- Scalar multiplication commutes with direct sums. -/ theorem mk_smul (s : finset ι) (c : R) (x) : mk M s (c • x) = c • mk M s x := (lmk R ι M s).map_smul c x /-- Scalar multiplication commutes with the inclusion of each component into the direct sum. -/ theorem of_smul (i : ι) (c : R) (x) : of M i (c • x) = c • of M i x := (lof R ι M i).map_smul c x variables {N : Type u₁} [add_comm_group N] [semimodule R N] variables (φ : Π i, M i →ₗ[R] N) variables (ι N φ) /-- The linear map constructed using the universal property of the coproduct. -/ def to_module : (⨁ i, M i) →ₗ[R] N := { to_fun := to_group (λ i, φ i), map_add' := to_group_add _, map_smul' := λ c x, direct_sum.induction_on x (by rw [smul_zero, to_group_zero, smul_zero]) (λ i x, by rw [← of_smul, to_group_of, to_group_of, (φ i).map_smul c x]) (λ x y ihx ihy, by rw [smul_add, to_group_add, ihx, ihy, to_group_add, smul_add]) } variables {ι N φ} /-- The map constructed using the universal property gives back the original maps when restricted to each component. -/ @[simp] lemma to_module_lof (i) (x : M i) : to_module R ι N φ (lof R ι M i x) = φ i x := to_group_of (λ i, φ i) i x variables (ψ : (⨁ i, M i) →ₗ[R] N) /-- Every linear map from a direct sum agrees with the one obtained by applying the universal property to each of its components. -/ theorem to_module.unique (f : ⨁ i, M i) : ψ f = to_module R ι N (λ i, ψ.comp $ lof R ι M i) f := to_group.unique ψ f variables {ψ} {ψ' : (⨁ i, M i) →ₗ[R] N} theorem to_module.ext (H : ∀ i, ψ.comp (lof R ι M i) = ψ'.comp (lof R ι M i)) (f : ⨁ i, M i) : ψ f = ψ' f := by rw [to_module.unique R ψ, to_module.unique R ψ', funext H] /-- The inclusion of a subset of the direct summands into a larger subset of the direct summands, as a linear map. -/ def lset_to_set (S T : set ι) (H : S ⊆ T) : (⨁ (i : S), M i) →ₗ (⨁ (i : T), M i) := to_module R _ _ $ λ i, lof R T (M ∘ @subtype.val _ T) ⟨i.1, H i.2⟩ omit dec_ι /-- The natural linear equivalence between `⨁ _ : punit, M` and `M`. -/ -- TODO: generalize this to arbitrary index type `ι` with `unique ι` protected def lid (M : Type v) [add_comm_group M] [semimodule R M] : (⨁ (_ : punit), M) ≃ₗ M := { .. direct_sum.id M, .. to_module R punit M (λ i, linear_map.id) } variables (ι M) /-- The projection map onto one component, as a linear map. -/ def component (i : ι) : (⨁ i, M i) →ₗ[R] M i := { to_fun := λ f, f i, map_add' := λ _ _, dfinsupp.add_apply, map_smul' := λ _ _, dfinsupp.smul_apply } variables {ι M} lemma apply_eq_component (f : ⨁ i, M i) (i : ι) : f i = component R ι M i f := rfl @[ext] lemma ext {f g : ⨁ i, M i} (h : ∀ i, component R ι M i f = component R ι M i g) : f = g := dfinsupp.ext h lemma ext_iff {f g : ⨁ i, M i} : f = g ↔ ∀ i, component R ι M i f = component R ι M i g := ⟨λ h _, by rw h, ext R⟩ include dec_ι @[simp] lemma lof_apply (i : ι) (b : M i) : ((lof R ι M i) b) i = b := by rw [lof, dfinsupp.lsingle_apply, dfinsupp.single_apply, dif_pos rfl] @[simp] lemma component.lof_self (i : ι) (b : M i) : component R ι M i ((lof R ι M i) b) = b := lof_apply R i b lemma component.of (i j : ι) (b : M j) : component R ι M i ((lof R ι M j) b) = if h : j = i then eq.rec_on h b else 0 := dfinsupp.single_apply end direct_sum
61010c727eba7dbd11403612278c54c9b28db2f2
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/data/nat/bitwise.lean
33346dfca02976b22626ccdfe98884e310b0efd4
[ "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
10,081
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ prelude import init.data.nat.lemmas init.meta.well_founded_tactics universe u namespace nat def bodd_div2 : ℕ → bool × ℕ | 0 := (ff, 0) | (succ n) := match bodd_div2 n with | (ff, m) := (tt, m) | (tt, m) := (ff, succ m) end def div2 (n : ℕ) : ℕ := (bodd_div2 n).2 def bodd (n : ℕ) : bool := (bodd_div2 n).1 @[simp] lemma bodd_zero : bodd 0 = ff := rfl lemma bodd_one : bodd 1 = tt := rfl lemma bodd_two : bodd 2 = ff := rfl @[simp] lemma bodd_succ (n : ℕ) : bodd (succ n) = bnot (bodd n) := by unfold bodd bodd_div2; cases bodd_div2 n; cases fst; refl @[simp] lemma bodd_add (m n : ℕ) : bodd (m + n) = bxor (bodd m) (bodd n) := begin induction n with n IH, { simp, cases bodd m; refl }, { simp [add_succ, IH], cases bodd m; cases bodd n; refl } end @[simp] lemma bodd_mul (m n : ℕ) : bodd (m * n) = bodd m && bodd n := begin induction n with n IH, { simp, cases bodd m; refl }, { simp [mul_succ, IH], cases bodd m; cases bodd n; refl } end lemma mod_two_of_bodd (n : ℕ) : n % 2 = cond (bodd n) 1 0 := begin have := congr_arg bodd (mod_add_div n 2), simp [bnot] at this, rw [show ∀ b, ff && b = ff, by intros; cases b; refl, show ∀ b, bxor b ff = b, by intros; cases b; refl] at this, rw [← this], cases mod_two_eq_zero_or_one n with h h; rw h; refl end @[simp] lemma div2_zero : div2 0 = 0 := rfl lemma div2_one : div2 1 = 0 := rfl lemma div2_two : div2 2 = 1 := rfl @[simp] lemma div2_succ (n : ℕ) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by unfold bodd div2 bodd_div2; cases bodd_div2 n; cases fst; refl local attribute [simp] nat.add_comm nat.add_assoc nat.add_left_comm nat.mul_comm nat.mul_assoc theorem bodd_add_div2 : ∀ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 := rfl | (succ n) := begin simp, refine eq.trans _ (congr_arg succ (bodd_add_div2 n)), cases bodd n; simp [cond, bnot], { rw [nat.add_comm, nat.zero_add], }, { rw [succ_mul, nat.add_comm 1, nat.zero_add] } end theorem div2_val (n) : div2 n = n / 2 := begin refine nat.eq_of_mul_eq_mul_left dec_trivial (nat.add_left_cancel (eq.trans _ (nat.mod_add_div n 2).symm)), rw [mod_two_of_bodd, bodd_add_div2] end def bit (b : bool) : ℕ → ℕ := cond b bit1 bit0 lemma bit0_val (n : nat) : bit0 n = 2 * n := calc n + n = 0 + n + n : by rw nat.zero_add ... = n * 2 : rfl ... = 2 * n : nat.mul_comm _ _ lemma bit1_val (n : nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) lemma bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by { cases b, apply bit0_val, apply bit1_val } lemma bit_decomp (n : nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans $ (nat.add_comm _ _).trans $ bodd_add_div2 _ def bit_cases_on {C : nat → Sort u} (n) (h : ∀ b n, C (bit b n)) : C n := by rw [← bit_decomp n]; apply h lemma bit_zero : bit ff 0 = 0 := rfl def shiftl' (b : bool) (m : ℕ) : ℕ → ℕ | 0 := m | (n+1) := bit b (shiftl' n) def shiftl : ℕ → ℕ → ℕ := shiftl' ff @[simp] theorem shiftl_zero (m) : shiftl m 0 = m := rfl @[simp] theorem shiftl_succ (m n) : shiftl m (n + 1) = bit0 (shiftl m n) := rfl def shiftr : ℕ → ℕ → ℕ | m 0 := m | m (n+1) := div2 (shiftr m n) def test_bit (m n : ℕ) : bool := bodd (shiftr m n) def binary_rec {C : nat → Sort u} (z : C 0) (f : ∀ b n, C n → C (bit b n)) : Π n, C n | n := if n0 : n = 0 then by rw n0; exact z else let n' := div2 n in have n' < n, begin change div2 n < n, rw div2_val, apply (div_lt_iff_lt_mul $ succ_pos 1).2, have := nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le (ne.symm n0)), rwa nat.mul_one at this end, by rw [← show bit (bodd n) n' = n, from bit_decomp n]; exact f (bodd n) n' (binary_rec n') def size : ℕ → ℕ := binary_rec 0 (λ_ _, succ) def bits : ℕ → list bool := binary_rec [] (λb _ IH, b :: IH) def bitwise (f : bool → bool → bool) : ℕ → ℕ → ℕ := binary_rec (λn, cond (f ff tt) n 0) (λa m Ia, binary_rec (cond (f tt ff) (bit a m) 0) (λb n _, bit (f a b) (Ia n))) def lor : ℕ → ℕ → ℕ := bitwise bor def land : ℕ → ℕ → ℕ := bitwise band def ldiff : ℕ → ℕ → ℕ := bitwise (λ a b, a && bnot b) def lxor : ℕ → ℕ → ℕ := bitwise bxor @[simp] lemma binary_rec_zero {C : nat → Sort u} (z : C 0) (f : ∀ b n, C n → C (bit b n)) : binary_rec z f 0 = z := by {rw [binary_rec], refl} /-! bitwise ops -/ lemma bodd_bit (b n) : bodd (bit b n) = b := by rw bit_val; simp; cases b; cases bodd n; refl lemma div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, nat.add_comm, add_mul_div_left, div_eq_of_lt, nat.zero_add]; cases b; exact dec_trivial lemma shiftl'_add (b m n) : ∀ k, shiftl' b m (n + k) = shiftl' b (shiftl' b m n) k | 0 := rfl | (k+1) := congr_arg (bit b) (shiftl'_add k) lemma shiftl_add : ∀ m n k, shiftl m (n + k) = shiftl (shiftl m n) k := shiftl'_add _ lemma shiftr_add (m n) : ∀ k, shiftr m (n + k) = shiftr (shiftr m n) k | 0 := rfl | (k+1) := congr_arg div2 (shiftr_add k) lemma shiftl'_sub (b m) : ∀ {n k}, k ≤ n → shiftl' b m (n - k) = shiftr (shiftl' b m n) k | n 0 h := rfl | (n+1) (k+1) h := begin simp [shiftl'], rw [nat.add_comm, shiftr_add], simp [shiftr, div2_bit], apply shiftl'_sub (nat.le_of_succ_le_succ h) end lemma shiftl_sub : ∀ m {n k}, k ≤ n → shiftl m (n - k) = shiftr (shiftl m n) k := shiftl'_sub _ @[simp] lemma test_bit_zero (b n) : test_bit (bit b n) 0 = b := bodd_bit _ _ lemma test_bit_succ (m b n) : test_bit (bit b n) (succ m) = test_bit n m := have bodd (shiftr (shiftr (bit b n) 1) m) = bodd (shiftr n m), by dsimp [shiftr]; rw div2_bit, by rw [← shiftr_add, nat.add_comm] at this; exact this lemma binary_rec_eq {C : nat → Sort u} {z : C 0} {f : ∀ b n, C n → C (bit b n)} (h : f ff 0 z = z) (b n) : binary_rec z f (bit b n) = f b n (binary_rec z f n) := begin rw [binary_rec], with_cases { by_cases bit b n = 0 }, case pos : h' { simp [dif_pos h'], generalize : binary_rec._main._pack._proof_1 (bit b n) h' = e, revert e, have bf := bodd_bit b n, have n0 := div2_bit b n, rw h' at bf n0, simp at bf n0, rw [← bf, ← n0, binary_rec_zero], intros, exact h.symm }, case neg : h' { simp [dif_neg h'], generalize : binary_rec._main._pack._proof_2 (bit b n) = e, revert e, rw [bodd_bit, div2_bit], intros, refl} end lemma bitwise_bit_aux {f : bool → bool → bool} (h : f ff ff = ff) : @binary_rec (λ_, ℕ) (cond (f tt ff) (bit ff 0) 0) (λ b n _, bit (f ff b) (cond (f ff tt) n 0)) = λ (n : ℕ), cond (f ff tt) n 0 := begin funext n, apply bit_cases_on n, intros b n, rw [binary_rec_eq], { cases b; try {rw h}; induction fft : f ff tt; simp [cond]; refl }, { rw [h, show cond (f ff tt) 0 0 = 0, by cases f ff tt; refl, show cond (f tt ff) (bit ff 0) 0 = 0, by cases f tt ff; refl]; refl } end @[simp] lemma bitwise_zero_left (f : bool → bool → bool) (n) : bitwise f 0 n = cond (f ff tt) n 0 := by unfold bitwise; rw [binary_rec_zero] @[simp] lemma bitwise_zero_right (f : bool → bool → bool) (h : f ff ff = ff) (m) : bitwise f m 0 = cond (f tt ff) m 0 := by unfold bitwise; apply bit_cases_on m; intros; rw [binary_rec_eq, binary_rec_zero]; exact bitwise_bit_aux h @[simp] lemma bitwise_zero (f : bool → bool → bool) : bitwise f 0 0 = 0 := by rw bitwise_zero_left; cases f ff tt; refl @[simp] lemma bitwise_bit {f : bool → bool → bool} (h : f ff ff = ff) (a m b n) : bitwise f (bit a m) (bit b n) = bit (f a b) (bitwise f m n) := begin unfold bitwise, rw [binary_rec_eq, binary_rec_eq], { induction ftf : f tt ff; dsimp [cond], rw [show f a ff = ff, by cases a; assumption], apply @congr_arg _ _ _ 0 (bit ff), tactic.swap, rw [show f a ff = a, by cases a; assumption], apply congr_arg (bit a), all_goals { apply bit_cases_on m, intros a m, rw [binary_rec_eq, binary_rec_zero], rw [← bitwise_bit_aux h, ftf], refl } }, { exact bitwise_bit_aux h } end theorem bitwise_swap {f : bool → bool → bool} (h : f ff ff = ff) : bitwise (function.swap f) = function.swap (bitwise f) := begin funext m n, revert n, dsimp [function.swap], apply binary_rec _ (λ a m' IH, _) m; intro n, { rw [bitwise_zero_left, bitwise_zero_right], exact h }, apply bit_cases_on n; intros b n', rw [bitwise_bit, bitwise_bit, IH]; exact h end @[simp] lemma lor_bit : ∀ (a m b n), lor (bit a m) (bit b n) = bit (a || b) (lor m n) := bitwise_bit rfl @[simp] lemma land_bit : ∀ (a m b n), land (bit a m) (bit b n) = bit (a && b) (land m n) := bitwise_bit rfl @[simp] lemma ldiff_bit : ∀ (a m b n), ldiff (bit a m) (bit b n) = bit (a && bnot b) (ldiff m n) := bitwise_bit rfl @[simp] lemma lxor_bit : ∀ (a m b n), lxor (bit a m) (bit b n) = bit (bxor a b) (lxor m n) := bitwise_bit rfl @[simp] lemma test_bit_bitwise {f : bool → bool → bool} (h : f ff ff = ff) (m n k) : test_bit (bitwise f m n) k = f (test_bit m k) (test_bit n k) := begin revert m n; induction k with k IH; intros m n; apply bit_cases_on m; intros a m'; apply bit_cases_on n; intros b n'; rw bitwise_bit h, { simp [test_bit_zero] }, { simp [test_bit_succ, IH] } end @[simp] lemma test_bit_lor : ∀ (m n k), test_bit (lor m n) k = test_bit m k || test_bit n k := test_bit_bitwise rfl @[simp] lemma test_bit_land : ∀ (m n k), test_bit (land m n) k = test_bit m k && test_bit n k := test_bit_bitwise rfl @[simp] lemma test_bit_ldiff : ∀ (m n k), test_bit (ldiff m n) k = test_bit m k && bnot (test_bit n k) := test_bit_bitwise rfl @[simp] lemma test_bit_lxor : ∀ (m n k), test_bit (lxor m n) k = bxor (test_bit m k) (test_bit n k) := test_bit_bitwise rfl end nat
90bef92f22d6acb2e7ffd2318f74a1c481d3d571
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/analysis/special_functions/trigonometric.lean
8e3a2493ada87fa350964432e694dd37abf2de8a
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
65,890
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 -/ import analysis.special_functions.exp_log /-! # Trigonometric functions ## Main definitions This file contains the following definitions: * π, arcsin, arccos, arctan * argument of a complex number * logarithm on complex numbers ## Main statements Many basic inequalities on trigonometric functions are established. The continuity and differentiability of the usual trigonometric functions are proved, and their derivatives are computed. ## Tags log, sin, cos, tan, arcsin, arccos, arctan, angle, argument -/ noncomputable theory open_locale classical namespace complex /-- The complex sine function is everywhere differentiable, with the derivative `cos x`. -/ lemma has_deriv_at_sin (x : ℂ) : has_deriv_at sin (cos x) x := begin simp only [cos, div_eq_mul_inv], convert ((((has_deriv_at_id x).neg.mul_const I).cexp.sub ((has_deriv_at_id x).mul_const I).cexp).mul_const I).mul_const (2:ℂ)⁻¹, simp only [function.comp, id], rw [sub_mul, mul_assoc, mul_assoc, I_mul_I, neg_one_mul, neg_neg, mul_one, one_mul, mul_assoc, I_mul_I, mul_neg_one, sub_neg_eq_add, add_comm] end lemma differentiable_sin : differentiable ℂ sin := λx, (has_deriv_at_sin x).differentiable_at lemma differentiable_at_sin {x : ℂ} : differentiable_at ℂ sin x := differentiable_sin x @[simp] lemma deriv_sin : deriv sin = cos := funext $ λ x, (has_deriv_at_sin x).deriv lemma continuous_sin : continuous sin := differentiable_sin.continuous /-- The complex cosine function is everywhere differentiable, with the derivative `-sin x`. -/ lemma has_deriv_at_cos (x : ℂ) : has_deriv_at cos (-sin x) x := begin simp only [sin, div_eq_mul_inv, neg_mul_eq_neg_mul], convert (((has_deriv_at_id x).mul_const I).cexp.add ((has_deriv_at_id x).neg.mul_const I).cexp).mul_const (2:ℂ)⁻¹, simp only [function.comp, id], ring end lemma differentiable_cos : differentiable ℂ cos := λx, (has_deriv_at_cos x).differentiable_at lemma differentiable_at_cos {x : ℂ} : differentiable_at ℂ cos x := differentiable_cos x lemma deriv_cos {x : ℂ} : deriv cos x = -sin x := (has_deriv_at_cos x).deriv @[simp] lemma deriv_cos' : deriv cos = (λ x, -sin x) := funext $ λ x, deriv_cos lemma continuous_cos : continuous cos := differentiable_cos.continuous lemma continuous_tan : continuous (λ x : {x // cos x ≠ 0}, tan x) := (continuous_sin.comp continuous_subtype_val).mul (continuous.inv subtype.property (continuous_cos.comp continuous_subtype_val)) /-- The complex hyperbolic sine function is everywhere differentiable, with the derivative `sinh x`. -/ lemma has_deriv_at_sinh (x : ℂ) : has_deriv_at sinh (cosh x) x := begin simp only [cosh, div_eq_mul_inv], convert ((has_deriv_at_exp x).sub (has_deriv_at_id x).neg.cexp).mul_const (2:ℂ)⁻¹, rw [id, mul_neg_one, neg_neg] end lemma differentiable_sinh : differentiable ℂ sinh := λx, (has_deriv_at_sinh x).differentiable_at lemma differentiable_at_sinh {x : ℂ} : differentiable_at ℂ sinh x := differentiable_sinh x @[simp] lemma deriv_sinh : deriv sinh = cosh := funext $ λ x, (has_deriv_at_sinh x).deriv lemma continuous_sinh : continuous sinh := differentiable_sinh.continuous /-- The complex hyperbolic cosine function is everywhere differentiable, with the derivative `cosh x`. -/ lemma has_deriv_at_cosh (x : ℂ) : has_deriv_at cosh (sinh x) x := begin simp only [sinh, div_eq_mul_inv], convert ((has_deriv_at_exp x).add (has_deriv_at_id x).neg.cexp).mul_const (2:ℂ)⁻¹, rw [id, mul_neg_one, sub_eq_add_neg] end lemma differentiable_cosh : differentiable ℂ cosh := λx, (has_deriv_at_cosh x).differentiable_at lemma differentiable_at_cosh {x : ℂ} : differentiable_at ℂ cos x := differentiable_cos x @[simp] lemma deriv_cosh : deriv cosh = sinh := funext $ λ x, (has_deriv_at_cosh x).deriv lemma continuous_cosh : continuous cosh := differentiable_cosh.continuous end complex section /-! Register lemmas for the derivatives of the composition of `complex.cos`, `complex.sin`, `complex.cosh` and `complex.sinh` with a differentiable function, for standalone use and use with `simp`. -/ variables {f : ℂ → ℂ} {f' x : ℂ} {s : set ℂ} /-! `complex.cos`-/ lemma has_deriv_at.ccos (hf : has_deriv_at f f' x) : has_deriv_at (λ x, complex.cos (f x)) (- complex.sin (f x) * f') x := (complex.has_deriv_at_cos (f x)).comp x hf lemma has_deriv_within_at.ccos (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, complex.cos (f x)) (- complex.sin (f x) * f') s x := (complex.has_deriv_at_cos (f x)).comp_has_deriv_within_at x hf lemma differentiable_within_at.ccos (hf : differentiable_within_at ℂ f s x) : differentiable_within_at ℂ (λ x, complex.cos (f x)) s x := hf.has_deriv_within_at.ccos.differentiable_within_at @[simp] lemma differentiable_at.ccos (hc : differentiable_at ℂ f x) : differentiable_at ℂ (λx, complex.cos (f x)) x := hc.has_deriv_at.ccos.differentiable_at lemma differentiable_on.ccos (hc : differentiable_on ℂ f s) : differentiable_on ℂ (λx, complex.cos (f x)) s := λx h, (hc x h).ccos @[simp] lemma differentiable.ccos (hc : differentiable ℂ f) : differentiable ℂ (λx, complex.cos (f x)) := λx, (hc x).ccos lemma deriv_within_ccos (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : deriv_within (λx, complex.cos (f x)) s x = - complex.sin (f x) * (deriv_within f s x) := hf.has_deriv_within_at.ccos.deriv_within hxs @[simp] lemma deriv_ccos (hc : differentiable_at ℂ f x) : deriv (λx, complex.cos (f x)) x = - complex.sin (f x) * (deriv f x) := hc.has_deriv_at.ccos.deriv /-! `complex.sin`-/ lemma has_deriv_at.csin (hf : has_deriv_at f f' x) : has_deriv_at (λ x, complex.sin (f x)) (complex.cos (f x) * f') x := (complex.has_deriv_at_sin (f x)).comp x hf lemma has_deriv_within_at.csin (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, complex.sin (f x)) (complex.cos (f x) * f') s x := (complex.has_deriv_at_sin (f x)).comp_has_deriv_within_at x hf lemma differentiable_within_at.csin (hf : differentiable_within_at ℂ f s x) : differentiable_within_at ℂ (λ x, complex.sin (f x)) s x := hf.has_deriv_within_at.csin.differentiable_within_at @[simp] lemma differentiable_at.csin (hc : differentiable_at ℂ f x) : differentiable_at ℂ (λx, complex.sin (f x)) x := hc.has_deriv_at.csin.differentiable_at lemma differentiable_on.csin (hc : differentiable_on ℂ f s) : differentiable_on ℂ (λx, complex.sin (f x)) s := λx h, (hc x h).csin @[simp] lemma differentiable.csin (hc : differentiable ℂ f) : differentiable ℂ (λx, complex.sin (f x)) := λx, (hc x).csin lemma deriv_within_csin (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : deriv_within (λx, complex.sin (f x)) s x = complex.cos (f x) * (deriv_within f s x) := hf.has_deriv_within_at.csin.deriv_within hxs @[simp] lemma deriv_csin (hc : differentiable_at ℂ f x) : deriv (λx, complex.sin (f x)) x = complex.cos (f x) * (deriv f x) := hc.has_deriv_at.csin.deriv /-! `complex.cosh`-/ lemma has_deriv_at.ccosh (hf : has_deriv_at f f' x) : has_deriv_at (λ x, complex.cosh (f x)) (complex.sinh (f x) * f') x := (complex.has_deriv_at_cosh (f x)).comp x hf lemma has_deriv_within_at.ccosh (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, complex.cosh (f x)) (complex.sinh (f x) * f') s x := (complex.has_deriv_at_cosh (f x)).comp_has_deriv_within_at x hf lemma differentiable_within_at.ccosh (hf : differentiable_within_at ℂ f s x) : differentiable_within_at ℂ (λ x, complex.cosh (f x)) s x := hf.has_deriv_within_at.ccosh.differentiable_within_at @[simp] lemma differentiable_at.ccosh (hc : differentiable_at ℂ f x) : differentiable_at ℂ (λx, complex.cosh (f x)) x := hc.has_deriv_at.ccosh.differentiable_at lemma differentiable_on.ccosh (hc : differentiable_on ℂ f s) : differentiable_on ℂ (λx, complex.cosh (f x)) s := λx h, (hc x h).ccosh @[simp] lemma differentiable.ccosh (hc : differentiable ℂ f) : differentiable ℂ (λx, complex.cosh (f x)) := λx, (hc x).ccosh lemma deriv_within_ccosh (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : deriv_within (λx, complex.cosh (f x)) s x = complex.sinh (f x) * (deriv_within f s x) := hf.has_deriv_within_at.ccosh.deriv_within hxs @[simp] lemma deriv_ccosh (hc : differentiable_at ℂ f x) : deriv (λx, complex.cosh (f x)) x = complex.sinh (f x) * (deriv f x) := hc.has_deriv_at.ccosh.deriv /-! `complex.sinh`-/ lemma has_deriv_at.csinh (hf : has_deriv_at f f' x) : has_deriv_at (λ x, complex.sinh (f x)) (complex.cosh (f x) * f') x := (complex.has_deriv_at_sinh (f x)).comp x hf lemma has_deriv_within_at.csinh (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, complex.sinh (f x)) (complex.cosh (f x) * f') s x := (complex.has_deriv_at_sinh (f x)).comp_has_deriv_within_at x hf lemma differentiable_within_at.csinh (hf : differentiable_within_at ℂ f s x) : differentiable_within_at ℂ (λ x, complex.sinh (f x)) s x := hf.has_deriv_within_at.csinh.differentiable_within_at @[simp] lemma differentiable_at.csinh (hc : differentiable_at ℂ f x) : differentiable_at ℂ (λx, complex.sinh (f x)) x := hc.has_deriv_at.csinh.differentiable_at lemma differentiable_on.csinh (hc : differentiable_on ℂ f s) : differentiable_on ℂ (λx, complex.sinh (f x)) s := λx h, (hc x h).csinh @[simp] lemma differentiable.csinh (hc : differentiable ℂ f) : differentiable ℂ (λx, complex.sinh (f x)) := λx, (hc x).csinh lemma deriv_within_csinh (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : deriv_within (λx, complex.sinh (f x)) s x = complex.cosh (f x) * (deriv_within f s x) := hf.has_deriv_within_at.csinh.deriv_within hxs @[simp] lemma deriv_csinh (hc : differentiable_at ℂ f x) : deriv (λx, complex.sinh (f x)) x = complex.cosh (f x) * (deriv f x) := hc.has_deriv_at.csinh.deriv end namespace real variables {x y z : ℝ} lemma has_deriv_at_sin (x : ℝ) : has_deriv_at sin (cos x) x := has_deriv_at_real_of_complex (complex.has_deriv_at_sin x) lemma differentiable_sin : differentiable ℝ sin := λx, (has_deriv_at_sin x).differentiable_at lemma differentiable_at_sin : differentiable_at ℝ sin x := differentiable_sin x @[simp] lemma deriv_sin : deriv sin = cos := funext $ λ x, (has_deriv_at_sin x).deriv lemma continuous_sin : continuous sin := differentiable_sin.continuous lemma has_deriv_at_cos (x : ℝ) : has_deriv_at cos (-sin x) x := (has_deriv_at_real_of_complex (complex.has_deriv_at_cos x) : _) lemma differentiable_cos : differentiable ℝ cos := λx, (has_deriv_at_cos x).differentiable_at lemma differentiable_at_cos : differentiable_at ℝ cos x := differentiable_cos x lemma deriv_cos : deriv cos x = - sin x := (has_deriv_at_cos x).deriv @[simp] lemma deriv_cos' : deriv cos = (λ x, - sin x) := funext $ λ _, deriv_cos lemma continuous_cos : continuous cos := differentiable_cos.continuous lemma continuous_tan : continuous (λ x : {x // cos x ≠ 0}, tan x) := by simp only [tan_eq_sin_div_cos]; exact (continuous_sin.comp continuous_subtype_val).mul (continuous.inv subtype.property (continuous_cos.comp continuous_subtype_val)) lemma has_deriv_at_sinh (x : ℝ) : has_deriv_at sinh (cosh x) x := has_deriv_at_real_of_complex (complex.has_deriv_at_sinh x) lemma differentiable_sinh : differentiable ℝ sinh := λx, (has_deriv_at_sinh x).differentiable_at lemma differentiable_at_sinh : differentiable_at ℝ sinh x := differentiable_sinh x @[simp] lemma deriv_sinh : deriv sinh = cosh := funext $ λ x, (has_deriv_at_sinh x).deriv lemma continuous_sinh : continuous sinh := differentiable_sinh.continuous lemma has_deriv_at_cosh (x : ℝ) : has_deriv_at cosh (sinh x) x := has_deriv_at_real_of_complex (complex.has_deriv_at_cosh x) lemma differentiable_cosh : differentiable ℝ cosh := λx, (has_deriv_at_cosh x).differentiable_at lemma differentiable_at_cosh : differentiable_at ℝ cosh x := differentiable_cosh x @[simp] lemma deriv_cosh : deriv cosh = sinh := funext $ λ x, (has_deriv_at_cosh x).deriv lemma continuous_cosh : continuous cosh := differentiable_cosh.continuous end real section /-! Register lemmas for the derivatives of the composition of `real.exp`, `real.cos`, `real.sin`, `real.cosh` and `real.sinh` with a differentiable function, for standalone use and use with `simp`. -/ variables {f : ℝ → ℝ} {f' x : ℝ} {s : set ℝ} /-! `real.cos`-/ lemma has_deriv_at.cos (hf : has_deriv_at f f' x) : has_deriv_at (λ x, real.cos (f x)) (- real.sin (f x) * f') x := (real.has_deriv_at_cos (f x)).comp x hf lemma has_deriv_within_at.cos (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, real.cos (f x)) (- real.sin (f x) * f') s x := (real.has_deriv_at_cos (f x)).comp_has_deriv_within_at x hf lemma differentiable_within_at.cos (hf : differentiable_within_at ℝ f s x) : differentiable_within_at ℝ (λ x, real.cos (f x)) s x := hf.has_deriv_within_at.cos.differentiable_within_at @[simp] lemma differentiable_at.cos (hc : differentiable_at ℝ f x) : differentiable_at ℝ (λx, real.cos (f x)) x := hc.has_deriv_at.cos.differentiable_at lemma differentiable_on.cos (hc : differentiable_on ℝ f s) : differentiable_on ℝ (λx, real.cos (f x)) s := λx h, (hc x h).cos @[simp] lemma differentiable.cos (hc : differentiable ℝ f) : differentiable ℝ (λx, real.cos (f x)) := λx, (hc x).cos lemma deriv_within_cos (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, real.cos (f x)) s x = - real.sin (f x) * (deriv_within f s x) := hf.has_deriv_within_at.cos.deriv_within hxs @[simp] lemma deriv_cos (hc : differentiable_at ℝ f x) : deriv (λx, real.cos (f x)) x = - real.sin (f x) * (deriv f x) := hc.has_deriv_at.cos.deriv /-! `real.sin`-/ lemma has_deriv_at.sin (hf : has_deriv_at f f' x) : has_deriv_at (λ x, real.sin (f x)) (real.cos (f x) * f') x := (real.has_deriv_at_sin (f x)).comp x hf lemma has_deriv_within_at.sin (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, real.sin (f x)) (real.cos (f x) * f') s x := (real.has_deriv_at_sin (f x)).comp_has_deriv_within_at x hf lemma differentiable_within_at.sin (hf : differentiable_within_at ℝ f s x) : differentiable_within_at ℝ (λ x, real.sin (f x)) s x := hf.has_deriv_within_at.sin.differentiable_within_at @[simp] lemma differentiable_at.sin (hc : differentiable_at ℝ f x) : differentiable_at ℝ (λx, real.sin (f x)) x := hc.has_deriv_at.sin.differentiable_at lemma differentiable_on.sin (hc : differentiable_on ℝ f s) : differentiable_on ℝ (λx, real.sin (f x)) s := λx h, (hc x h).sin @[simp] lemma differentiable.sin (hc : differentiable ℝ f) : differentiable ℝ (λx, real.sin (f x)) := λx, (hc x).sin lemma deriv_within_sin (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, real.sin (f x)) s x = real.cos (f x) * (deriv_within f s x) := hf.has_deriv_within_at.sin.deriv_within hxs @[simp] lemma deriv_sin (hc : differentiable_at ℝ f x) : deriv (λx, real.sin (f x)) x = real.cos (f x) * (deriv f x) := hc.has_deriv_at.sin.deriv /-! `real.cosh`-/ lemma has_deriv_at.cosh (hf : has_deriv_at f f' x) : has_deriv_at (λ x, real.cosh (f x)) (real.sinh (f x) * f') x := (real.has_deriv_at_cosh (f x)).comp x hf lemma has_deriv_within_at.cosh (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, real.cosh (f x)) (real.sinh (f x) * f') s x := (real.has_deriv_at_cosh (f x)).comp_has_deriv_within_at x hf lemma differentiable_within_at.cosh (hf : differentiable_within_at ℝ f s x) : differentiable_within_at ℝ (λ x, real.cosh (f x)) s x := hf.has_deriv_within_at.cosh.differentiable_within_at @[simp] lemma differentiable_at.cosh (hc : differentiable_at ℝ f x) : differentiable_at ℝ (λx, real.cosh (f x)) x := hc.has_deriv_at.cosh.differentiable_at lemma differentiable_on.cosh (hc : differentiable_on ℝ f s) : differentiable_on ℝ (λx, real.cosh (f x)) s := λx h, (hc x h).cosh @[simp] lemma differentiable.cosh (hc : differentiable ℝ f) : differentiable ℝ (λx, real.cosh (f x)) := λx, (hc x).cosh lemma deriv_within_cosh (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, real.cosh (f x)) s x = real.sinh (f x) * (deriv_within f s x) := hf.has_deriv_within_at.cosh.deriv_within hxs @[simp] lemma deriv_cosh (hc : differentiable_at ℝ f x) : deriv (λx, real.cosh (f x)) x = real.sinh (f x) * (deriv f x) := hc.has_deriv_at.cosh.deriv /-! `real.sinh`-/ lemma has_deriv_at.sinh (hf : has_deriv_at f f' x) : has_deriv_at (λ x, real.sinh (f x)) (real.cosh (f x) * f') x := (real.has_deriv_at_sinh (f x)).comp x hf lemma has_deriv_within_at.sinh (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, real.sinh (f x)) (real.cosh (f x) * f') s x := (real.has_deriv_at_sinh (f x)).comp_has_deriv_within_at x hf lemma differentiable_within_at.sinh (hf : differentiable_within_at ℝ f s x) : differentiable_within_at ℝ (λ x, real.sinh (f x)) s x := hf.has_deriv_within_at.sinh.differentiable_within_at @[simp] lemma differentiable_at.sinh (hc : differentiable_at ℝ f x) : differentiable_at ℝ (λx, real.sinh (f x)) x := hc.has_deriv_at.sinh.differentiable_at lemma differentiable_on.sinh (hc : differentiable_on ℝ f s) : differentiable_on ℝ (λx, real.sinh (f x)) s := λx h, (hc x h).sinh @[simp] lemma differentiable.sinh (hc : differentiable ℝ f) : differentiable ℝ (λx, real.sinh (f x)) := λx, (hc x).sinh lemma deriv_within_sinh (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, real.sinh (f x)) s x = real.cosh (f x) * (deriv_within f s x) := hf.has_deriv_within_at.sinh.deriv_within hxs @[simp] lemma deriv_sinh (hc : differentiable_at ℝ f x) : deriv (λx, real.sinh (f x)) x = real.cosh (f x) * (deriv f x) := hc.has_deriv_at.sinh.deriv end namespace real lemma exists_cos_eq_zero : 0 ∈ cos '' set.Icc (1:ℝ) 2 := intermediate_value_Icc' (by norm_num) continuous_cos.continuous_on ⟨le_of_lt cos_two_neg, le_of_lt cos_one_pos⟩ /-- The number π = 3.14159265... Defined here using choice as twice a zero of cos in [1,2], from which one can derive all its properties. For explicit bounds on π, see `data.real.pi`. -/ noncomputable def pi : ℝ := 2 * classical.some exists_cos_eq_zero localized "notation `π` := real.pi" in real @[simp] lemma cos_pi_div_two : cos (π / 2) = 0 := by rw [pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)]; exact (classical.some_spec exists_cos_eq_zero).2 lemma one_le_pi_div_two : (1 : ℝ) ≤ π / 2 := by rw [pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)]; exact (classical.some_spec exists_cos_eq_zero).1.1 lemma pi_div_two_le_two : π / 2 ≤ 2 := by rw [pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)]; exact (classical.some_spec exists_cos_eq_zero).1.2 lemma two_le_pi : (2 : ℝ) ≤ π := (div_le_div_right (show (0 : ℝ) < 2, by norm_num)).1 (by rw div_self (@two_ne_zero' ℝ _ _ _); exact one_le_pi_div_two) lemma pi_le_four : π ≤ 4 := (div_le_div_right (show (0 : ℝ) < 2, by norm_num)).1 (calc π / 2 ≤ 2 : pi_div_two_le_two ... = 4 / 2 : by norm_num) lemma pi_pos : 0 < π := lt_of_lt_of_le (by norm_num) two_le_pi lemma pi_div_two_pos : 0 < π / 2 := half_pos pi_pos lemma two_pi_pos : 0 < 2 * π := by linarith [pi_pos] @[simp] lemma sin_pi : sin π = 0 := by rw [← mul_div_cancel_left pi (@two_ne_zero ℝ _), two_mul, add_div, sin_add, cos_pi_div_two]; simp @[simp] lemma cos_pi : cos π = -1 := by rw [← mul_div_cancel_left pi (@two_ne_zero ℝ _), mul_div_assoc, cos_two_mul, cos_pi_div_two]; simp [bit0, pow_add] @[simp] lemma sin_two_pi : sin (2 * π) = 0 := by simp [two_mul, sin_add] @[simp] lemma cos_two_pi : cos (2 * π) = 1 := by simp [two_mul, cos_add] lemma sin_add_pi (x : ℝ) : sin (x + π) = -sin x := by simp [sin_add] lemma sin_add_two_pi (x : ℝ) : sin (x + 2 * π) = sin x := by simp [sin_add_pi, sin_add, sin_two_pi, cos_two_pi] lemma cos_add_two_pi (x : ℝ) : cos (x + 2 * π) = cos x := by simp [cos_add, cos_two_pi, sin_two_pi] lemma sin_pi_sub (x : ℝ) : sin (π - x) = sin x := by simp [sub_eq_add_neg, sin_add] lemma cos_add_pi (x : ℝ) : cos (x + π) = -cos x := by simp [cos_add] lemma cos_pi_sub (x : ℝ) : cos (π - x) = -cos x := by simp [sub_eq_add_neg, cos_add] lemma sin_pos_of_pos_of_lt_pi {x : ℝ} (h0x : 0 < x) (hxp : x < π) : 0 < sin x := if hx2 : x ≤ 2 then sin_pos_of_pos_of_le_two h0x hx2 else have (2 : ℝ) + 2 = 4, from rfl, have π - x ≤ 2, from sub_le_iff_le_add.2 (le_trans pi_le_four (this ▸ add_le_add_left (le_of_not_ge hx2) _)), sin_pi_sub x ▸ sin_pos_of_pos_of_le_two (sub_pos.2 hxp) this lemma sin_nonneg_of_nonneg_of_le_pi {x : ℝ} (h0x : 0 ≤ x) (hxp : x ≤ π) : 0 ≤ sin x := match lt_or_eq_of_le h0x with | or.inl h0x := (lt_or_eq_of_le hxp).elim (le_of_lt ∘ sin_pos_of_pos_of_lt_pi h0x) (λ hpx, by simp [hpx]) | or.inr h0x := by simp [h0x.symm] end lemma sin_neg_of_neg_of_neg_pi_lt {x : ℝ} (hx0 : x < 0) (hpx : -π < x) : sin x < 0 := neg_pos.1 $ sin_neg x ▸ sin_pos_of_pos_of_lt_pi (neg_pos.2 hx0) (neg_lt.1 hpx) lemma sin_nonpos_of_nonnpos_of_neg_pi_le {x : ℝ} (hx0 : x ≤ 0) (hpx : -π ≤ x) : sin x ≤ 0 := neg_nonneg.1 $ sin_neg x ▸ sin_nonneg_of_nonneg_of_le_pi (neg_nonneg.2 hx0) (neg_le.1 hpx) @[simp] lemma sin_pi_div_two : sin (π / 2) = 1 := have sin (π / 2) = 1 ∨ sin (π / 2) = -1 := by simpa [pow_two, mul_self_eq_one_iff] using sin_sq_add_cos_sq (π / 2), this.resolve_right (λ h, (show ¬(0 : ℝ) < -1, by norm_num) $ h ▸ sin_pos_of_pos_of_lt_pi pi_div_two_pos (half_lt_self pi_pos)) lemma sin_add_pi_div_two (x : ℝ) : sin (x + π / 2) = cos x := by simp [sin_add] lemma sin_sub_pi_div_two (x : ℝ) : sin (x - π / 2) = -cos x := by simp [sub_eq_add_neg, sin_add] lemma sin_pi_div_two_sub (x : ℝ) : sin (π / 2 - x) = cos x := by simp [sub_eq_add_neg, sin_add] lemma cos_add_pi_div_two (x : ℝ) : cos (x + π / 2) = -sin x := by simp [cos_add] lemma cos_sub_pi_div_two (x : ℝ) : cos (x - π / 2) = sin x := by simp [sub_eq_add_neg, cos_add] lemma cos_pi_div_two_sub (x : ℝ) : cos (π / 2 - x) = sin x := by rw [← cos_neg, neg_sub, cos_sub_pi_div_two] lemma cos_pos_of_neg_pi_div_two_lt_of_lt_pi_div_two {x : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2) : 0 < cos x := sin_add_pi_div_two x ▸ sin_pos_of_pos_of_lt_pi (by linarith) (by linarith) lemma cos_nonneg_of_neg_pi_div_two_le_of_le_pi_div_two {x : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) : 0 ≤ cos x := match lt_or_eq_of_le hx₁, lt_or_eq_of_le hx₂ with | or.inl hx₁, or.inl hx₂ := le_of_lt (cos_pos_of_neg_pi_div_two_lt_of_lt_pi_div_two hx₁ hx₂) | or.inl hx₁, or.inr hx₂ := by simp [hx₂] | or.inr hx₁, _ := by simp [hx₁.symm] end lemma cos_neg_of_pi_div_two_lt_of_lt {x : ℝ} (hx₁ : π / 2 < x) (hx₂ : x < π + π / 2) : cos x < 0 := neg_pos.1 $ cos_pi_sub x ▸ cos_pos_of_neg_pi_div_two_lt_of_lt_pi_div_two (by linarith) (by linarith) lemma cos_nonpos_of_pi_div_two_le_of_le {x : ℝ} (hx₁ : π / 2 ≤ x) (hx₂ : x ≤ π + π / 2) : cos x ≤ 0 := neg_nonneg.1 $ cos_pi_sub x ▸ cos_nonneg_of_neg_pi_div_two_le_of_le_pi_div_two (by linarith) (by linarith) lemma sin_nat_mul_pi (n : ℕ) : sin (n * π) = 0 := by induction n; simp [add_mul, sin_add, *] lemma sin_int_mul_pi (n : ℤ) : sin (n * π) = 0 := by cases n; simp [add_mul, sin_add, *, sin_nat_mul_pi] lemma cos_nat_mul_two_pi (n : ℕ) : cos (n * (2 * π)) = 1 := by induction n; simp [*, mul_add, cos_add, add_mul, cos_two_pi, sin_two_pi] lemma cos_int_mul_two_pi (n : ℤ) : cos (n * (2 * π)) = 1 := by cases n; simp only [cos_nat_mul_two_pi, int.of_nat_eq_coe, int.neg_succ_of_nat_coe, int.cast_coe_nat, int.cast_neg, (neg_mul_eq_neg_mul _ _).symm, cos_neg] lemma cos_int_mul_two_pi_add_pi (n : ℤ) : cos (n * (2 * π) + π) = -1 := by simp [cos_add, sin_add, cos_int_mul_two_pi] lemma sin_eq_zero_iff_of_lt_of_lt {x : ℝ} (hx₁ : -π < x) (hx₂ : x < π) : sin x = 0 ↔ x = 0 := ⟨λ h, le_antisymm (le_of_not_gt (λ h0, lt_irrefl (0 : ℝ) $ calc 0 < sin x : sin_pos_of_pos_of_lt_pi h0 hx₂ ... = 0 : h)) (le_of_not_gt (λ h0, lt_irrefl (0 : ℝ) $ calc 0 = sin x : h.symm ... < 0 : sin_neg_of_neg_of_neg_pi_lt h0 hx₁)), λ h, by simp [h]⟩ lemma sin_eq_zero_iff {x : ℝ} : sin x = 0 ↔ ∃ n : ℤ, (n : ℝ) * π = x := ⟨λ h, ⟨⌊x / π⌋, le_antisymm (sub_nonneg.1 (sub_floor_div_mul_nonneg _ pi_pos)) (sub_nonpos.1 $ le_of_not_gt $ λ h₃, ne_of_lt (sin_pos_of_pos_of_lt_pi h₃ (sub_floor_div_mul_lt _ pi_pos)) (by simp [sub_eq_add_neg, sin_add, h, sin_int_mul_pi]))⟩, λ ⟨n, hn⟩, hn ▸ sin_int_mul_pi _⟩ lemma sin_eq_zero_iff_cos_eq {x : ℝ} : sin x = 0 ↔ cos x = 1 ∨ cos x = -1 := by rw [← mul_self_eq_one_iff (cos x), ← sin_sq_add_cos_sq x, pow_two, pow_two, ← sub_eq_iff_eq_add, sub_self]; exact ⟨λ h, by rw [h, mul_zero], eq_zero_of_mul_self_eq_zero ∘ eq.symm⟩ theorem sin_sub_sin (θ ψ : ℝ) : sin θ - sin ψ = 2 * sin((θ - ψ)/2) * cos((θ + ψ)/2) := begin have s1 := sin_add ((θ + ψ) / 2) ((θ - ψ) / 2), have s2 := sin_sub ((θ + ψ) / 2) ((θ - ψ) / 2), rw [div_add_div_same, add_sub, add_right_comm, add_sub_cancel, add_self_div_two] at s1, rw [div_sub_div_same, ←sub_add, add_sub_cancel', add_self_div_two] at s2, rw [s1, s2, ←sub_add, add_sub_cancel', ← two_mul, ← mul_assoc, mul_right_comm] end lemma cos_eq_one_iff (x : ℝ) : cos x = 1 ↔ ∃ n : ℤ, (n : ℝ) * (2 * π) = x := ⟨λ h, let ⟨n, hn⟩ := sin_eq_zero_iff.1 (sin_eq_zero_iff_cos_eq.2 (or.inl h)) in ⟨n / 2, (int.mod_two_eq_zero_or_one n).elim (λ hn0, by rwa [← mul_assoc, ← @int.cast_two ℝ, ← int.cast_mul, int.div_mul_cancel ((int.dvd_iff_mod_eq_zero _ _).2 hn0)]) (λ hn1, by rw [← int.mod_add_div n 2, hn1, int.cast_add, int.cast_one, add_mul, one_mul, add_comm, mul_comm (2 : ℤ), int.cast_mul, mul_assoc, int.cast_two] at hn; rw [← hn, cos_int_mul_two_pi_add_pi] at h; exact absurd h (by norm_num))⟩, λ ⟨n, hn⟩, hn ▸ cos_int_mul_two_pi _⟩ theorem cos_eq_zero_iff {θ : ℝ} : cos θ = 0 ↔ ∃ k : ℤ, θ = (2 * k + 1) * pi / 2 := begin rw [←real.sin_pi_div_two_sub, sin_eq_zero_iff], split, { rintro ⟨n, hn⟩, existsi -n, rw [int.cast_neg, add_mul, add_div, mul_assoc, mul_div_cancel_left _ (@two_ne_zero ℝ _), one_mul, ←neg_mul_eq_neg_mul, hn, neg_sub, sub_add_cancel] }, { rintro ⟨n, hn⟩, existsi -n, rw [hn, add_mul, one_mul, add_div, mul_assoc, mul_div_cancel_left _ (@two_ne_zero ℝ _), sub_add_eq_sub_sub_swap, sub_self, zero_sub, neg_mul_eq_neg_mul, int.cast_neg] } end lemma cos_eq_one_iff_of_lt_of_lt {x : ℝ} (hx₁ : -(2 * π) < x) (hx₂ : x < 2 * π) : cos x = 1 ↔ x = 0 := ⟨λ h, let ⟨n, hn⟩ := (cos_eq_one_iff x).1 h in begin clear _let_match, subst hn, rw [mul_lt_iff_lt_one_left two_pi_pos, ← int.cast_one, int.cast_lt, ← int.le_sub_one_iff, sub_self] at hx₂, rw [neg_lt, neg_mul_eq_neg_mul, mul_lt_iff_lt_one_left two_pi_pos, neg_lt, ← int.cast_one, ← int.cast_neg, int.cast_lt, ← int.add_one_le_iff, neg_add_self] at hx₁, exact mul_eq_zero.2 (or.inl (int.cast_eq_zero.2 (le_antisymm hx₂ hx₁))), end, λ h, by simp [h]⟩ theorem cos_sub_cos (θ ψ : ℝ) : cos θ - cos ψ = -2 * sin((θ + ψ)/2) * sin((θ - ψ)/2) := by rw [← sin_pi_div_two_sub, ← sin_pi_div_two_sub, sin_sub_sin, sub_sub_sub_cancel_left, add_sub, sub_add_eq_add_sub, add_halves, sub_sub, sub_div π, cos_pi_div_two_sub, ← neg_sub, neg_div, sin_neg, ← neg_mul_eq_mul_neg, neg_mul_eq_neg_mul, mul_right_comm] lemma cos_lt_cos_of_nonneg_of_le_pi_div_two {x y : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π / 2) (hy₁ : 0 ≤ y) (hy₂ : y ≤ π / 2) (hxy : x < y) : cos y < cos x := calc cos y = cos x * cos (y - x) - sin x * sin (y - x) : by rw [← cos_add, add_sub_cancel'_right] ... < (cos x * 1) - sin x * sin (y - x) : sub_lt_sub_right ((mul_lt_mul_left (cos_pos_of_neg_pi_div_two_lt_of_lt_pi_div_two (lt_of_lt_of_le (neg_neg_of_pos pi_div_two_pos) hx₁) (lt_of_lt_of_le hxy hy₂))).2 (lt_of_le_of_ne (cos_le_one _) (mt (cos_eq_one_iff_of_lt_of_lt (show -(2 * π) < y - x, by linarith) (show y - x < 2 * π, by linarith)).1 (sub_ne_zero.2 (ne_of_lt hxy).symm)))) _ ... ≤ _ : by rw mul_one; exact sub_le_self _ (mul_nonneg (sin_nonneg_of_nonneg_of_le_pi hx₁ (by linarith)) (sin_nonneg_of_nonneg_of_le_pi (by linarith) (by linarith))) lemma cos_lt_cos_of_nonneg_of_le_pi {x y : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π) (hy₁ : 0 ≤ y) (hy₂ : y ≤ π) (hxy : x < y) : cos y < cos x := match (le_total x (π / 2) : x ≤ π / 2 ∨ π / 2 ≤ x), le_total y (π / 2) with | or.inl hx, or.inl hy := cos_lt_cos_of_nonneg_of_le_pi_div_two hx₁ hx hy₁ hy hxy | or.inl hx, or.inr hy := (lt_or_eq_of_le hx).elim (λ hx, calc cos y ≤ 0 : cos_nonpos_of_pi_div_two_le_of_le hy (by linarith [pi_pos]) ... < cos x : cos_pos_of_neg_pi_div_two_lt_of_lt_pi_div_two (by linarith) hx) (λ hx, calc cos y < 0 : cos_neg_of_pi_div_two_lt_of_lt (by linarith) (by linarith [pi_pos]) ... = cos x : by rw [hx, cos_pi_div_two]) | or.inr hx, or.inl hy := by linarith | or.inr hx, or.inr hy := neg_lt_neg_iff.1 (by rw [← cos_pi_sub, ← cos_pi_sub]; apply cos_lt_cos_of_nonneg_of_le_pi_div_two; linarith) end lemma cos_le_cos_of_nonneg_of_le_pi {x y : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π) (hy₁ : 0 ≤ y) (hy₂ : y ≤ π) (hxy : x ≤ y) : cos y ≤ cos x := (lt_or_eq_of_le hxy).elim (le_of_lt ∘ cos_lt_cos_of_nonneg_of_le_pi hx₁ hx₂ hy₁ hy₂) (λ h, h ▸ le_refl _) lemma sin_lt_sin_of_le_of_le_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) (hy₁ : -(π / 2) ≤ y) (hy₂ : y ≤ π / 2) (hxy : x < y) : sin x < sin y := by rw [← cos_sub_pi_div_two, ← cos_sub_pi_div_two, ← cos_neg (x - _), ← cos_neg (y - _)]; apply cos_lt_cos_of_nonneg_of_le_pi; linarith lemma sin_le_sin_of_le_of_le_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) (hy₁ : -(π / 2) ≤ y) (hy₂ : y ≤ π / 2) (hxy : x ≤ y) : sin x ≤ sin y := (lt_or_eq_of_le hxy).elim (le_of_lt ∘ sin_lt_sin_of_le_of_le_pi_div_two hx₁ hx₂ hy₁ hy₂) (λ h, h ▸ le_refl _) lemma sin_inj_of_le_of_le_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) (hy₁ : -(π / 2) ≤ y) (hy₂ : y ≤ π / 2) (hxy : sin x = sin y) : x = y := match lt_trichotomy x y with | or.inl h := absurd (sin_lt_sin_of_le_of_le_pi_div_two hx₁ hx₂ hy₁ hy₂ h) (by rw hxy; exact lt_irrefl _) | or.inr (or.inl h) := h | or.inr (or.inr h) := absurd (sin_lt_sin_of_le_of_le_pi_div_two hy₁ hy₂ hx₁ hx₂ h) (by rw hxy; exact lt_irrefl _) end lemma cos_inj_of_nonneg_of_le_pi {x y : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π) (hy₁ : 0 ≤ y) (hy₂ : y ≤ π) (hxy : cos x = cos y) : x = y := begin rw [← sin_pi_div_two_sub, ← sin_pi_div_two_sub] at hxy, refine (sub_right_inj).1 (sin_inj_of_le_of_le_pi_div_two _ _ _ _ hxy); linarith end lemma exists_sin_eq : set.Icc (-1:ℝ) 1 ⊆ sin '' set.Icc (-(π / 2)) (π / 2) := by convert intermediate_value_Icc (le_trans (neg_nonpos.2 (le_of_lt pi_div_two_pos)) (le_of_lt pi_div_two_pos)) continuous_sin.continuous_on; simp only [sin_neg, sin_pi_div_two] lemma sin_lt {x : ℝ} (h : 0 < x) : sin x < x := begin cases le_or_gt x 1 with h' h', { have hx : abs x = x := abs_of_nonneg (le_of_lt h), have : abs x ≤ 1, rwa [hx], have := sin_bound this, rw [abs_le] at this, have := this.2, rw [sub_le_iff_le_add', hx] at this, apply lt_of_le_of_lt this, rw [sub_add], apply lt_of_lt_of_le _ (le_of_eq (sub_zero x)), apply sub_lt_sub_left, rw sub_pos, apply mul_lt_mul', { rw [pow_succ x 3], refine le_trans _ (le_of_eq (one_mul _)), rw mul_le_mul_right, exact h', apply pow_pos h }, norm_num, norm_num, apply pow_pos h }, exact lt_of_le_of_lt (sin_le_one x) h' end /- note 1: this inequality is not tight, the tighter inequality is sin x > x - x ^ 3 / 6. note 2: this is also true for x > 1, but it's nontrivial for x just above 1. -/ lemma sin_gt_sub_cube {x : ℝ} (h : 0 < x) (h' : x ≤ 1) : x - x ^ 3 / 4 < sin x := begin have hx : abs x = x := abs_of_nonneg (le_of_lt h), have : abs x ≤ 1, rwa [hx], have := sin_bound this, rw [abs_le] at this, have := this.1, rw [le_sub_iff_add_le, hx] at this, refine lt_of_lt_of_le _ this, rw [add_comm, sub_add, sub_neg_eq_add], apply sub_lt_sub_left, apply add_lt_of_lt_sub_left, rw (show x ^ 3 / 4 - x ^ 3 / 6 = x ^ 3 / 12, by simp [div_eq_mul_inv, (mul_sub _ _ _).symm, -sub_eq_add_neg]; congr; norm_num), apply mul_lt_mul', { rw [pow_succ x 3], refine le_trans _ (le_of_eq (one_mul _)), rw mul_le_mul_right, exact h', apply pow_pos h }, norm_num, norm_num, apply pow_pos h end /-- The type of angles -/ def angle : Type := quotient_add_group.quotient (gmultiples (2 * π)) namespace angle instance angle.add_comm_group : add_comm_group angle := quotient_add_group.add_comm_group _ instance : inhabited angle := ⟨0⟩ instance angle.has_coe : has_coe ℝ angle := ⟨quotient.mk'⟩ instance angle.is_add_group_hom : @is_add_group_hom ℝ angle _ _ (coe : ℝ → angle) := @quotient_add_group.is_add_group_hom _ _ _ (normal_add_subgroup_of_add_comm_group _) @[simp] lemma coe_zero : ↑(0 : ℝ) = (0 : angle) := rfl @[simp] lemma coe_add (x y : ℝ) : ↑(x + y : ℝ) = (↑x + ↑y : angle) := rfl @[simp] lemma coe_neg (x : ℝ) : ↑(-x : ℝ) = -(↑x : angle) := rfl @[simp] lemma coe_sub (x y : ℝ) : ↑(x - y : ℝ) = (↑x - ↑y : angle) := rfl @[simp] lemma coe_smul (x : ℝ) (n : ℕ) : ↑(add_monoid.smul n x : ℝ) = add_monoid.smul n (↑x : angle) := add_monoid_hom.map_smul ⟨coe, coe_zero, coe_add⟩ _ _ @[simp] lemma coe_gsmul (x : ℝ) (n : ℤ) : ↑(gsmul n x : ℝ) = gsmul n (↑x : angle) := add_monoid_hom.map_gsmul ⟨coe, coe_zero, coe_add⟩ _ _ @[simp] lemma coe_two_pi : ↑(2 * π : ℝ) = (0 : angle) := quotient.sound' ⟨-1, by dsimp only; rw [neg_one_gsmul, add_zero]⟩ lemma angle_eq_iff_two_pi_dvd_sub {ψ θ : ℝ} : (θ : angle) = ψ ↔ ∃ k : ℤ, θ - ψ = 2 * π * k := by simp only [quotient_add_group.eq, gmultiples, set.mem_range, gsmul_eq_mul', (sub_eq_neg_add _ _).symm, eq_comm] theorem cos_eq_iff_eq_or_eq_neg {θ ψ : ℝ} : cos θ = cos ψ ↔ (θ : angle) = ψ ∨ (θ : angle) = -ψ := begin split, { intro Hcos, rw [←sub_eq_zero, cos_sub_cos, mul_eq_zero, mul_eq_zero, neg_eq_zero, eq_false_intro two_ne_zero, false_or, sin_eq_zero_iff, sin_eq_zero_iff] at Hcos, rcases Hcos with ⟨n, hn⟩ | ⟨n, hn⟩, { right, rw [eq_div_iff_mul_eq _ _ (@two_ne_zero ℝ _), ← sub_eq_iff_eq_add] at hn, rw [← hn, coe_sub, eq_neg_iff_add_eq_zero, sub_add_cancel, mul_assoc, ← gsmul_eq_mul, coe_gsmul, mul_comm, coe_two_pi, gsmul_zero] }, { left, rw [eq_div_iff_mul_eq _ _ (@two_ne_zero ℝ _), eq_sub_iff_add_eq] at hn, rw [← hn, coe_add, mul_assoc, ← gsmul_eq_mul, coe_gsmul, mul_comm, coe_two_pi, gsmul_zero, zero_add] } }, { rw [angle_eq_iff_two_pi_dvd_sub, ← coe_neg, angle_eq_iff_two_pi_dvd_sub], rintro (⟨k, H⟩ | ⟨k, H⟩), rw [← sub_eq_zero_iff_eq, cos_sub_cos, H, mul_assoc 2 π k, mul_div_cancel_left _ (@two_ne_zero ℝ _), mul_comm π _, sin_int_mul_pi, mul_zero], rw [←sub_eq_zero_iff_eq, cos_sub_cos, ← sub_neg_eq_add, H, mul_assoc 2 π k, mul_div_cancel_left _ (@two_ne_zero ℝ _), mul_comm π _, sin_int_mul_pi, mul_zero, zero_mul] } end theorem sin_eq_iff_eq_or_add_eq_pi {θ ψ : ℝ} : sin θ = sin ψ ↔ (θ : angle) = ψ ∨ (θ : angle) + ψ = π := begin split, { intro Hsin, rw [← cos_pi_div_two_sub, ← cos_pi_div_two_sub] at Hsin, cases cos_eq_iff_eq_or_eq_neg.mp Hsin with h h, { left, rw coe_sub at h, exact sub_right_inj.1 h }, right, rw [coe_sub, coe_sub, eq_neg_iff_add_eq_zero, add_sub, sub_add_eq_add_sub, ← coe_add, add_halves, sub_sub, sub_eq_zero] at h, exact h.symm }, { rw [angle_eq_iff_two_pi_dvd_sub, ←eq_sub_iff_add_eq, ←coe_sub, angle_eq_iff_two_pi_dvd_sub], rintro (⟨k, H⟩ | ⟨k, H⟩), rw [← sub_eq_zero_iff_eq, sin_sub_sin, H, mul_assoc 2 π k, mul_div_cancel_left _ (@two_ne_zero ℝ _), mul_comm π _, sin_int_mul_pi, mul_zero, zero_mul], have H' : θ + ψ = (2 * k) * π + π := by rwa [←sub_add, sub_add_eq_add_sub, sub_eq_iff_eq_add, mul_assoc, mul_comm π _, ←mul_assoc] at H, rw [← sub_eq_zero_iff_eq, sin_sub_sin, H', add_div, mul_assoc 2 _ π, mul_div_cancel_left _ (@two_ne_zero ℝ _), cos_add_pi_div_two, sin_int_mul_pi, neg_zero, mul_zero] } end theorem cos_sin_inj {θ ψ : ℝ} (Hcos : cos θ = cos ψ) (Hsin : sin θ = sin ψ) : (θ : angle) = ψ := begin cases cos_eq_iff_eq_or_eq_neg.mp Hcos with hc hc, { exact hc }, cases sin_eq_iff_eq_or_add_eq_pi.mp Hsin with hs hs, { exact hs }, rw [eq_neg_iff_add_eq_zero, hs] at hc, cases quotient.exact' hc with n hn, dsimp only at hn, rw [← neg_one_mul, add_zero, ← sub_eq_zero_iff_eq, gsmul_eq_mul, ← mul_assoc, ← sub_mul, mul_eq_zero, eq_false_intro (ne_of_gt pi_pos), or_false, sub_neg_eq_add, ← int.cast_zero, ← int.cast_one, ← int.cast_bit0, ← int.cast_mul, ← int.cast_add, int.cast_inj] at hn, have : (n * 2 + 1) % (2:ℤ) = 0 % (2:ℤ) := congr_arg (%(2:ℤ)) hn, rw [add_comm, int.add_mul_mod_self] at this, exact absurd this one_ne_zero end end angle /-- Inverse of the `sin` function, returns values in the range `-π / 2 ≤ arcsin x` and `arcsin x ≤ π / 2`. If the argument is not between `-1` and `1` it defaults to `0` -/ noncomputable def arcsin (x : ℝ) : ℝ := if hx : -1 ≤ x ∧ x ≤ 1 then classical.some (exists_sin_eq hx) else 0 lemma arcsin_le_pi_div_two (x : ℝ) : arcsin x ≤ π / 2 := if hx : -1 ≤ x ∧ x ≤ 1 then by rw [arcsin, dif_pos hx]; exact (classical.some_spec (exists_sin_eq hx)).1.2 else by rw [arcsin, dif_neg hx]; exact le_of_lt pi_div_two_pos lemma neg_pi_div_two_le_arcsin (x : ℝ) : -(π / 2) ≤ arcsin x := if hx : -1 ≤ x ∧ x ≤ 1 then by rw [arcsin, dif_pos hx]; exact (classical.some_spec (exists_sin_eq hx)).1.1 else by rw [arcsin, dif_neg hx]; exact neg_nonpos.2 (le_of_lt pi_div_two_pos) lemma sin_arcsin {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : sin (arcsin x) = x := by rw [arcsin, dif_pos (and.intro hx₁ hx₂)]; exact (classical.some_spec (exists_sin_eq ⟨hx₁, hx₂⟩)).2 lemma arcsin_sin {x : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) : arcsin (sin x) = x := sin_inj_of_le_of_le_pi_div_two (neg_pi_div_two_le_arcsin _) (arcsin_le_pi_div_two _) hx₁ hx₂ (by rw sin_arcsin (neg_one_le_sin _) (sin_le_one _)) lemma arcsin_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1) (hxy : arcsin x = arcsin y) : x = y := by rw [← sin_arcsin hx₁ hx₂, ← sin_arcsin hy₁ hy₂, hxy] @[simp] lemma arcsin_zero : arcsin 0 = 0 := sin_inj_of_le_of_le_pi_div_two (neg_pi_div_two_le_arcsin _) (arcsin_le_pi_div_two _) (neg_nonpos.2 (le_of_lt pi_div_two_pos)) (le_of_lt pi_div_two_pos) (by rw [sin_arcsin, sin_zero]; norm_num) @[simp] lemma arcsin_one : arcsin 1 = π / 2 := sin_inj_of_le_of_le_pi_div_two (neg_pi_div_two_le_arcsin _) (arcsin_le_pi_div_two _) (by linarith [pi_pos]) (le_refl _) (by rw [sin_arcsin, sin_pi_div_two]; norm_num) @[simp] lemma arcsin_neg (x : ℝ) : arcsin (-x) = -arcsin x := if h : -1 ≤ x ∧ x ≤ 1 then have -1 ≤ -x ∧ -x ≤ 1, by rwa [neg_le_neg_iff, neg_le, and.comm], sin_inj_of_le_of_le_pi_div_two (neg_pi_div_two_le_arcsin _) (arcsin_le_pi_div_two _) (neg_le_neg (arcsin_le_pi_div_two _)) (neg_le.1 (neg_pi_div_two_le_arcsin _)) (by rw [sin_arcsin this.1 this.2, sin_neg, sin_arcsin h.1 h.2]) else have ¬(-1 ≤ -x ∧ -x ≤ 1) := by rwa [neg_le_neg_iff, neg_le, and.comm], by rw [arcsin, arcsin, dif_neg h, dif_neg this, neg_zero] @[simp] lemma arcsin_neg_one : arcsin (-1) = -(π / 2) := by simp lemma arcsin_nonneg {x : ℝ} (hx : 0 ≤ x) : 0 ≤ arcsin x := if hx₁ : x ≤ 1 then not_lt.1 (λ h, not_lt.2 hx begin have := sin_lt_sin_of_le_of_le_pi_div_two (neg_pi_div_two_le_arcsin _) (arcsin_le_pi_div_two _) (neg_nonpos.2 (le_of_lt pi_div_two_pos)) (le_of_lt pi_div_two_pos) h, rw [real.sin_arcsin, sin_zero] at this; linarith end) else by rw [arcsin, dif_neg]; simp [hx₁] lemma arcsin_eq_zero_iff {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : arcsin x = 0 ↔ x = 0 := ⟨λ h, have sin (arcsin x) = 0, by simp [h], by rwa [sin_arcsin hx₁ hx₂] at this, λ h, by simp [h]⟩ lemma arcsin_pos {x : ℝ} (hx₁ : 0 < x) (hx₂ : x ≤ 1) : 0 < arcsin x := lt_of_le_of_ne (arcsin_nonneg (le_of_lt hx₁)) (ne.symm (mt (arcsin_eq_zero_iff (by linarith) hx₂).1 (ne_of_lt hx₁).symm)) lemma arcsin_nonpos {x : ℝ} (hx : x ≤ 0) : arcsin x ≤ 0 := neg_nonneg.1 (arcsin_neg x ▸ arcsin_nonneg (neg_nonneg.2 hx)) /-- Inverse of the `cos` function, returns values in the range `0 ≤ arccos x` and `arccos x ≤ π`. If the argument is not between `-1` and `1` it defaults to `π / 2` -/ noncomputable def arccos (x : ℝ) : ℝ := π / 2 - arcsin x lemma arccos_eq_pi_div_two_sub_arcsin (x : ℝ) : arccos x = π / 2 - arcsin x := rfl lemma arcsin_eq_pi_div_two_sub_arccos (x : ℝ) : arcsin x = π / 2 - arccos x := by simp [sub_eq_add_neg, arccos] lemma arccos_le_pi (x : ℝ) : arccos x ≤ π := by unfold arccos; linarith [neg_pi_div_two_le_arcsin x] lemma arccos_nonneg (x : ℝ) : 0 ≤ arccos x := by unfold arccos; linarith [arcsin_le_pi_div_two x] lemma cos_arccos {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : cos (arccos x) = x := by rw [arccos, cos_pi_div_two_sub, sin_arcsin hx₁ hx₂] lemma arccos_cos {x : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π) : arccos (cos x) = x := by rw [arccos, ← sin_pi_div_two_sub, arcsin_sin]; simp [sub_eq_add_neg]; linarith lemma arccos_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1) (hxy : arccos x = arccos y) : x = y := arcsin_inj hx₁ hx₂ hy₁ hy₂ $ by simp [arccos, *] at * @[simp] lemma arccos_zero : arccos 0 = π / 2 := by simp [arccos] @[simp] lemma arccos_one : arccos 1 = 0 := by simp [arccos] @[simp] lemma arccos_neg_one : arccos (-1) = π := by simp [arccos, add_halves] lemma arccos_neg (x : ℝ) : arccos (-x) = π - arccos x := by rw [← add_halves π, arccos, arcsin_neg, arccos, add_sub_assoc, sub_sub_self]; simp lemma cos_arcsin_nonneg (x : ℝ) : 0 ≤ cos (arcsin x) := cos_nonneg_of_neg_pi_div_two_le_of_le_pi_div_two (neg_pi_div_two_le_arcsin _) (arcsin_le_pi_div_two _) lemma cos_arcsin {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : cos (arcsin x) = sqrt (1 - x ^ 2) := have sin (arcsin x) ^ 2 + cos (arcsin x) ^ 2 = 1 := sin_sq_add_cos_sq (arcsin x), begin rw [← eq_sub_iff_add_eq', ← sqrt_inj (pow_two_nonneg _) (sub_nonneg.2 (sin_sq_le_one (arcsin x))), pow_two, sqrt_mul_self (cos_arcsin_nonneg _)] at this, rw [this, sin_arcsin hx₁ hx₂], end lemma sin_arccos {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : sin (arccos x) = sqrt (1 - x ^ 2) := by rw [arccos_eq_pi_div_two_sub_arcsin, sin_pi_div_two_sub, cos_arcsin hx₁ hx₂] lemma abs_div_sqrt_one_add_lt (x : ℝ) : abs (x / sqrt (1 + x ^ 2)) < 1 := have h₁ : 0 < 1 + x ^ 2, from add_pos_of_pos_of_nonneg zero_lt_one (pow_two_nonneg _), have h₂ : 0 < sqrt (1 + x ^ 2), from sqrt_pos.2 h₁, by rw [abs_div, div_lt_iff (abs_pos_of_pos h₂), one_mul, mul_self_lt_mul_self_iff (abs_nonneg x) (abs_nonneg _), ← abs_mul, ← abs_mul, mul_self_sqrt (add_nonneg zero_le_one (pow_two_nonneg _)), abs_of_nonneg (mul_self_nonneg x), abs_of_nonneg (le_of_lt h₁), pow_two, add_comm]; exact lt_add_one _ lemma div_sqrt_one_add_lt_one (x : ℝ) : x / sqrt (1 + x ^ 2) < 1 := (abs_lt.1 (abs_div_sqrt_one_add_lt _)).2 lemma neg_one_lt_div_sqrt_one_add (x : ℝ) : -1 < x / sqrt (1 + x ^ 2) := (abs_lt.1 (abs_div_sqrt_one_add_lt _)).1 lemma tan_pos_of_pos_of_lt_pi_div_two {x : ℝ} (h0x : 0 < x) (hxp : x < π / 2) : 0 < tan x := by rw tan_eq_sin_div_cos; exact div_pos (sin_pos_of_pos_of_lt_pi h0x (by linarith)) (cos_pos_of_neg_pi_div_two_lt_of_lt_pi_div_two (by linarith) hxp) lemma tan_nonneg_of_nonneg_of_le_pi_div_two {x : ℝ} (h0x : 0 ≤ x) (hxp : x ≤ π / 2) : 0 ≤ tan x := match lt_or_eq_of_le h0x, lt_or_eq_of_le hxp with | or.inl hx0, or.inl hxp := le_of_lt (tan_pos_of_pos_of_lt_pi_div_two hx0 hxp) | or.inl hx0, or.inr hxp := by simp [hxp, tan_eq_sin_div_cos] | or.inr hx0, _ := by simp [hx0.symm] end lemma tan_neg_of_neg_of_pi_div_two_lt {x : ℝ} (hx0 : x < 0) (hpx : -(π / 2) < x) : tan x < 0 := neg_pos.1 (tan_neg x ▸ tan_pos_of_pos_of_lt_pi_div_two (by linarith) (by linarith [pi_pos])) lemma tan_nonpos_of_nonpos_of_neg_pi_div_two_le {x : ℝ} (hx0 : x ≤ 0) (hpx : -(π / 2) ≤ x) : tan x ≤ 0 := neg_nonneg.1 (tan_neg x ▸ tan_nonneg_of_nonneg_of_le_pi_div_two (by linarith) (by linarith [pi_pos])) lemma tan_lt_tan_of_nonneg_of_lt_pi_div_two {x y : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x < π / 2) (hy₁ : 0 ≤ y) (hy₂ : y < π / 2) (hxy : x < y) : tan x < tan y := begin rw [tan_eq_sin_div_cos, tan_eq_sin_div_cos], exact div_lt_div (sin_lt_sin_of_le_of_le_pi_div_two (by linarith) (le_of_lt hx₂) (by linarith) (le_of_lt hy₂) hxy) (cos_le_cos_of_nonneg_of_le_pi hx₁ (by linarith) hy₁ (by linarith) (le_of_lt hxy)) (sin_nonneg_of_nonneg_of_le_pi hy₁ (by linarith)) (cos_pos_of_neg_pi_div_two_lt_of_lt_pi_div_two (by linarith) hy₂) end lemma tan_lt_tan_of_lt_of_lt_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2) (hy₁ : -(π / 2) < y) (hy₂ : y < π / 2) (hxy : x < y) : tan x < tan y := match le_total x 0, le_total y 0 with | or.inl hx0, or.inl hy0 := neg_lt_neg_iff.1 $ by rw [← tan_neg, ← tan_neg]; exact tan_lt_tan_of_nonneg_of_lt_pi_div_two (neg_nonneg.2 hy0) (neg_lt.2 hy₁) (neg_nonneg.2 hx0) (neg_lt.2 hx₁) (neg_lt_neg hxy) | or.inl hx0, or.inr hy0 := (lt_or_eq_of_le hy0).elim (λ hy0, calc tan x ≤ 0 : tan_nonpos_of_nonpos_of_neg_pi_div_two_le hx0 (le_of_lt hx₁) ... < tan y : tan_pos_of_pos_of_lt_pi_div_two hy0 hy₂) (λ hy0, by rw [← hy0, tan_zero]; exact tan_neg_of_neg_of_pi_div_two_lt (hy0.symm ▸ hxy) hx₁) | or.inr hx0, or.inl hy0 := by linarith | or.inr hx0, or.inr hy0 := tan_lt_tan_of_nonneg_of_lt_pi_div_two hx0 hx₂ hy0 hy₂ hxy end lemma tan_inj_of_lt_of_lt_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2) (hy₁ : -(π / 2) < y) (hy₂ : y < π / 2) (hxy : tan x = tan y) : x = y := match lt_trichotomy x y with | or.inl h := absurd (tan_lt_tan_of_lt_of_lt_pi_div_two hx₁ hx₂ hy₁ hy₂ h) (by rw hxy; exact lt_irrefl _) | or.inr (or.inl h) := h | or.inr (or.inr h) := absurd (tan_lt_tan_of_lt_of_lt_pi_div_two hy₁ hy₂ hx₁ hx₂ h) (by rw hxy; exact lt_irrefl _) end /-- Inverse of the `tan` function, returns values in the range `-π / 2 < arctan x` and `arctan x < π / 2` -/ noncomputable def arctan (x : ℝ) : ℝ := arcsin (x / sqrt (1 + x ^ 2)) lemma sin_arctan (x : ℝ) : sin (arctan x) = x / sqrt (1 + x ^ 2) := sin_arcsin (le_of_lt (neg_one_lt_div_sqrt_one_add _)) (le_of_lt (div_sqrt_one_add_lt_one _)) lemma cos_arctan (x : ℝ) : cos (arctan x) = 1 / sqrt (1 + x ^ 2) := have h₁ : (0 : ℝ) < 1 + x ^ 2, from add_pos_of_pos_of_nonneg zero_lt_one (pow_two_nonneg _), have h₂ : (x / sqrt (1 + x ^ 2)) ^ 2 < 1, by rw [pow_two, ← abs_mul_self, _root_.abs_mul]; exact mul_lt_one_of_nonneg_of_lt_one_left (abs_nonneg _) (abs_div_sqrt_one_add_lt _) (le_of_lt (abs_div_sqrt_one_add_lt _)), by rw [arctan, cos_arcsin (le_of_lt (neg_one_lt_div_sqrt_one_add _)) (le_of_lt (div_sqrt_one_add_lt_one _)), one_div_eq_inv, ← sqrt_inv, sqrt_inj (sub_nonneg.2 (le_of_lt h₂)) (inv_nonneg.2 (le_of_lt h₁)), div_pow, pow_two (sqrt _), mul_self_sqrt (le_of_lt h₁), ← domain.mul_right_inj (ne.symm (ne_of_lt h₁)), mul_sub, mul_div_cancel' _ (ne.symm (ne_of_lt h₁)), mul_inv_cancel (ne.symm (ne_of_lt h₁))]; simp lemma tan_arctan (x : ℝ) : tan (arctan x) = x := by rw [tan_eq_sin_div_cos, sin_arctan, cos_arctan, div_div_div_div_eq, mul_one, mul_div_assoc, div_self (mt sqrt_eq_zero'.1 (not_le_of_gt (add_pos_of_pos_of_nonneg zero_lt_one (pow_two_nonneg x)))), mul_one] lemma arctan_lt_pi_div_two (x : ℝ) : arctan x < π / 2 := lt_of_le_of_ne (arcsin_le_pi_div_two _) (λ h, ne_of_lt (div_sqrt_one_add_lt_one x) $ by rw [← sin_arcsin (le_of_lt (neg_one_lt_div_sqrt_one_add _)) (le_of_lt (div_sqrt_one_add_lt_one _)), ← arctan, h, sin_pi_div_two]) lemma neg_pi_div_two_lt_arctan (x : ℝ) : -(π / 2) < arctan x := lt_of_le_of_ne (neg_pi_div_two_le_arcsin _) (λ h, ne_of_lt (neg_one_lt_div_sqrt_one_add x) $ by rw [← sin_arcsin (le_of_lt (neg_one_lt_div_sqrt_one_add _)) (le_of_lt (div_sqrt_one_add_lt_one _)), ← arctan, ← h, sin_neg, sin_pi_div_two]) lemma tan_surjective : function.surjective tan := function.right_inverse.surjective tan_arctan lemma arctan_tan {x : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2) : arctan (tan x) = x := tan_inj_of_lt_of_lt_pi_div_two (neg_pi_div_two_lt_arctan _) (arctan_lt_pi_div_two _) hx₁ hx₂ (by rw tan_arctan) @[simp] lemma arctan_zero : arctan 0 = 0 := by simp [arctan] @[simp] lemma arctan_neg (x : ℝ) : arctan (-x) = - arctan x := by simp [arctan, neg_div] end real namespace complex open_locale real /-- `arg` returns values in the range (-π, π], such that for `x ≠ 0`, `sin (arg x) = x.im / x.abs` and `cos (arg x) = x.re / x.abs`, `arg 0` defaults to `0` -/ noncomputable def arg (x : ℂ) : ℝ := if 0 ≤ x.re then real.arcsin (x.im / x.abs) else if 0 ≤ x.im then real.arcsin ((-x).im / x.abs) + π else real.arcsin ((-x).im / x.abs) - π lemma arg_le_pi (x : ℂ) : arg x ≤ π := if hx₁ : 0 ≤ x.re then by rw [arg, if_pos hx₁]; exact le_trans (real.arcsin_le_pi_div_two _) (le_of_lt (half_lt_self real.pi_pos)) else have hx : x ≠ 0, from λ h, by simpa [h, lt_irrefl] using hx₁, if hx₂ : 0 ≤ x.im then by rw [arg, if_neg hx₁, if_pos hx₂]; exact le_sub_iff_add_le.1 (by rw sub_self; exact real.arcsin_nonpos (by rw [neg_im, neg_div, neg_nonpos]; exact div_nonneg hx₂ (abs_pos.2 hx))) else by rw [arg, if_neg hx₁, if_neg hx₂]; exact sub_le_iff_le_add.2 (le_trans (real.arcsin_le_pi_div_two _) (by linarith [real.pi_pos])) lemma neg_pi_lt_arg (x : ℂ) : -π < arg x := if hx₁ : 0 ≤ x.re then by rw [arg, if_pos hx₁]; exact lt_of_lt_of_le (neg_lt_neg (half_lt_self real.pi_pos)) (real.neg_pi_div_two_le_arcsin _) else have hx : x ≠ 0, from λ h, by simpa [h, lt_irrefl] using hx₁, if hx₂ : 0 ≤ x.im then by rw [arg, if_neg hx₁, if_pos hx₂]; exact sub_lt_iff_lt_add.1 (lt_of_lt_of_le (by linarith [real.pi_pos]) (real.neg_pi_div_two_le_arcsin _)) else by rw [arg, if_neg hx₁, if_neg hx₂]; exact lt_sub_iff_add_lt.2 (by rw neg_add_self; exact real.arcsin_pos (by rw [neg_im]; exact div_pos (neg_pos.2 (lt_of_not_ge hx₂)) (abs_pos.2 hx)) (by rw [← abs_neg x]; exact (abs_le.1 (abs_im_div_abs_le_one _)).2)) lemma arg_eq_arg_neg_add_pi_of_im_nonneg_of_re_neg {x : ℂ} (hxr : x.re < 0) (hxi : 0 ≤ x.im) : arg x = arg (-x) + π := have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos], by rw [arg, arg, if_neg (not_le.2 hxr), if_pos this, if_pos hxi, abs_neg] lemma arg_eq_arg_neg_sub_pi_of_im_neg_of_re_neg {x : ℂ} (hxr : x.re < 0) (hxi : x.im < 0) : arg x = arg (-x) - π := have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos], by rw [arg, arg, if_neg (not_le.2 hxr), if_neg (not_le.2 hxi), if_pos this, abs_neg] @[simp] lemma arg_zero : arg 0 = 0 := by simp [arg, le_refl] @[simp] lemma arg_one : arg 1 = 0 := by simp [arg, zero_le_one] @[simp] lemma arg_neg_one : arg (-1) = π := by simp [arg, le_refl, not_le.2 (@zero_lt_one ℝ _)] @[simp] lemma arg_I : arg I = π / 2 := by simp [arg, le_refl] @[simp] lemma arg_neg_I : arg (-I) = -(π / 2) := by simp [arg, le_refl] lemma sin_arg (x : ℂ) : real.sin (arg x) = x.im / x.abs := by unfold arg; split_ifs; simp [sub_eq_add_neg, arg, real.sin_arcsin (abs_le.1 (abs_im_div_abs_le_one x)).1 (abs_le.1 (abs_im_div_abs_le_one x)).2, real.sin_add, neg_div, real.arcsin_neg, real.sin_neg] private lemma cos_arg_of_re_nonneg {x : ℂ} (hx : x ≠ 0) (hxr : 0 ≤ x.re) : real.cos (arg x) = x.re / x.abs := have 0 ≤ 1 - (x.im / abs x) ^ 2, from sub_nonneg.2 $ by rw [pow_two, ← _root_.abs_mul_self, _root_.abs_mul, ← pow_two]; exact pow_le_one _ (_root_.abs_nonneg _) (abs_im_div_abs_le_one _), by rw [eq_div_iff_mul_eq _ _ (mt abs_eq_zero.1 hx), ← real.mul_self_sqrt (abs_nonneg x), arg, if_pos hxr, real.cos_arcsin (abs_le.1 (abs_im_div_abs_le_one x)).1 (abs_le.1 (abs_im_div_abs_le_one x)).2, ← real.sqrt_mul (abs_nonneg _), ← real.sqrt_mul this, sub_mul, div_pow, ← pow_two, div_mul_cancel _ (pow_ne_zero 2 (mt abs_eq_zero.1 hx)), one_mul, pow_two, mul_self_abs, norm_sq, pow_two, add_sub_cancel, real.sqrt_mul_self hxr] lemma cos_arg {x : ℂ} (hx : x ≠ 0) : real.cos (arg x) = x.re / x.abs := if hxr : 0 ≤ x.re then cos_arg_of_re_nonneg hx hxr else have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos] using hxr, if hxi : 0 ≤ x.im then have 0 ≤ (-x).re, from le_of_lt $ by simpa [neg_pos] using hxr, by rw [arg_eq_arg_neg_add_pi_of_im_nonneg_of_re_neg (not_le.1 hxr) hxi, real.cos_add_pi, cos_arg_of_re_nonneg (neg_ne_zero.2 hx) this]; simp [neg_div] else by rw [arg_eq_arg_neg_sub_pi_of_im_neg_of_re_neg (not_le.1 hxr) (not_le.1 hxi)]; simp [sub_eq_add_neg, real.cos_add, neg_div, cos_arg_of_re_nonneg (neg_ne_zero.2 hx) this] lemma tan_arg {x : ℂ} : real.tan (arg x) = x.im / x.re := begin by_cases h : x = 0, { simp only [h, euclidean_domain.zero_div, complex.zero_im, complex.arg_zero, real.tan_zero, complex.zero_re] }, rw [real.tan_eq_sin_div_cos, sin_arg, cos_arg h, div_div_div_cancel_right' _ (mt abs_eq_zero.1 h)] end lemma arg_cos_add_sin_mul_I {x : ℝ} (hx₁ : -π < x) (hx₂ : x ≤ π) : arg (cos x + sin x * I) = x := if hx₃ : -(π / 2) ≤ x ∧ x ≤ π / 2 then have hx₄ : 0 ≤ (cos x + sin x * I).re, by simp; exact real.cos_nonneg_of_neg_pi_div_two_le_of_le_pi_div_two hx₃.1 hx₃.2, by rw [arg, if_pos hx₄]; simp [abs_cos_add_sin_mul_I, sin_of_real_re, real.arcsin_sin hx₃.1 hx₃.2] else if hx₄ : x < -(π / 2) then have hx₅ : ¬0 ≤ (cos x + sin x * I).re := suffices ¬ 0 ≤ real.cos x, by simpa, not_le.2 $ by rw ← real.cos_neg; apply real.cos_neg_of_pi_div_two_lt_of_lt; linarith, have hx₆ : ¬0 ≤ (cos ↑x + sin ↑x * I).im := suffices real.sin x < 0, by simpa, by apply real.sin_neg_of_neg_of_neg_pi_lt; linarith, suffices -π + -real.arcsin (real.sin x) = x, by rw [arg, if_neg hx₅, if_neg hx₆]; simpa [sub_eq_add_neg, add_comm, abs_cos_add_sin_mul_I, sin_of_real_re], by rw [← real.arcsin_neg, ← real.sin_add_pi, real.arcsin_sin]; try {simp [add_left_comm]}; linarith else have hx₅ : π / 2 < x, by cases not_and_distrib.1 hx₃; linarith, have hx₆ : ¬0 ≤ (cos x + sin x * I).re := suffices ¬0 ≤ real.cos x, by simpa, not_le.2 $ by apply real.cos_neg_of_pi_div_two_lt_of_lt; linarith, have hx₇ : 0 ≤ (cos x + sin x * I).im := suffices 0 ≤ real.sin x, by simpa, by apply real.sin_nonneg_of_nonneg_of_le_pi; linarith, suffices π - real.arcsin (real.sin x) = x, by rw [arg, if_neg hx₆, if_pos hx₇]; simpa [sub_eq_add_neg, add_comm, abs_cos_add_sin_mul_I, sin_of_real_re], by rw [← real.sin_pi_sub, real.arcsin_sin]; simp [sub_eq_add_neg]; linarith lemma arg_eq_arg_iff {x y : ℂ} (hx : x ≠ 0) (hy : y ≠ 0) : arg x = arg y ↔ (abs y / abs x : ℂ) * x = y := have hax : abs x ≠ 0, from (mt abs_eq_zero.1 hx), have hay : abs y ≠ 0, from (mt abs_eq_zero.1 hy), ⟨λ h, begin have hcos := congr_arg real.cos h, rw [cos_arg hx, cos_arg hy, div_eq_div_iff hax hay] at hcos, have hsin := congr_arg real.sin h, rw [sin_arg, sin_arg, div_eq_div_iff hax hay] at hsin, apply complex.ext, { rw [mul_re, ← of_real_div, of_real_re, of_real_im, zero_mul, sub_zero, mul_comm, ← mul_div_assoc, hcos, mul_div_cancel _ hax] }, { rw [mul_im, ← of_real_div, of_real_re, of_real_im, zero_mul, add_zero, mul_comm, ← mul_div_assoc, hsin, mul_div_cancel _ hax] } end, λ h, have hre : abs (y / x) * x.re = y.re, by rw ← of_real_div at h; simpa [-of_real_div] using congr_arg re h, have hre' : abs (x / y) * y.re = x.re, by rw [← hre, abs_div, abs_div, ← mul_assoc, div_mul_div, mul_comm (abs _), div_self (mul_ne_zero hay hax), one_mul], have him : abs (y / x) * x.im = y.im, by rw ← of_real_div at h; simpa [-of_real_div] using congr_arg im h, have him' : abs (x / y) * y.im = x.im, by rw [← him, abs_div, abs_div, ← mul_assoc, div_mul_div, mul_comm (abs _), div_self (mul_ne_zero hay hax), one_mul], have hxya : x.im / abs x = y.im / abs y, by rw [← him, abs_div, mul_comm, ← mul_div_comm, mul_div_cancel_left _ hay], have hnxya : (-x).im / abs x = (-y).im / abs y, by rw [neg_im, neg_im, neg_div, neg_div, hxya], if hxr : 0 ≤ x.re then have hyr : 0 ≤ y.re, from hre ▸ mul_nonneg (abs_nonneg _) hxr, by simp [arg, *] at * else have hyr : ¬ 0 ≤ y.re, from λ hyr, hxr $ hre' ▸ mul_nonneg (abs_nonneg _) hyr, if hxi : 0 ≤ x.im then have hyi : 0 ≤ y.im, from him ▸ mul_nonneg (abs_nonneg _) hxi, by simp [arg, *] at * else have hyi : ¬ 0 ≤ y.im, from λ hyi, hxi $ him' ▸ mul_nonneg (abs_nonneg _) hyi, by simp [arg, *] at *⟩ lemma arg_real_mul (x : ℂ) {r : ℝ} (hr : 0 < r) : arg (r * x) = arg x := if hx : x = 0 then by simp [hx] else (arg_eq_arg_iff (mul_ne_zero (of_real_ne_zero.2 (ne_of_lt hr).symm) hx) hx).2 $ by rw [abs_mul, abs_of_nonneg (le_of_lt hr), ← mul_assoc, of_real_mul, mul_comm (r : ℂ), ← div_div_eq_div_mul, div_mul_cancel _ (of_real_ne_zero.2 (ne_of_lt hr).symm), div_self (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), one_mul] lemma ext_abs_arg {x y : ℂ} (h₁ : x.abs = y.abs) (h₂ : x.arg = y.arg) : x = y := if hy : y = 0 then by simp * at * else have hx : x ≠ 0, from λ hx, by simp [*, eq_comm] at *, by rwa [arg_eq_arg_iff hx hy, h₁, div_self (of_real_ne_zero.2 (mt abs_eq_zero.1 hy)), one_mul] at h₂ lemma arg_of_real_of_nonneg {x : ℝ} (hx : 0 ≤ x) : arg x = 0 := by simp [arg, hx] lemma arg_of_real_of_neg {x : ℝ} (hx : x < 0) : arg x = π := by rw [arg_eq_arg_neg_add_pi_of_im_nonneg_of_re_neg, ← of_real_neg, arg_of_real_of_nonneg]; simp [*, le_iff_eq_or_lt, lt_neg] /-- Inverse of the `exp` function. Returns values such that `(log x).im > - π` and `(log x).im ≤ π`. `log 0 = 0`-/ 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 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] 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 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 cos_pi_div_two : cos (π / 2) = 0 := calc cos (π / 2) = real.cos (π / 2) : by rw [of_real_cos]; simp ... = 0 : by simp @[simp] lemma sin_pi_div_two : sin (π / 2) = 1 := calc sin (π / 2) = real.sin (π / 2) : by rw [of_real_sin]; simp ... = 1 : by simp @[simp] lemma sin_pi : sin π = 0 := by rw [← of_real_sin, real.sin_pi]; simp @[simp] lemma cos_pi : cos π = -1 := by rw [← of_real_cos, real.cos_pi]; simp @[simp] lemma sin_two_pi : sin (2 * π) = 0 := by simp [two_mul, sin_add] @[simp] lemma cos_two_pi : cos (2 * π) = 1 := by simp [two_mul, cos_add] lemma sin_add_pi (x : ℝ) : sin (x + π) = -sin x := by simp [sin_add] lemma sin_add_two_pi (x : ℝ) : sin (x + 2 * π) = sin x := by simp [sin_add_pi, sin_add, sin_two_pi, cos_two_pi] lemma cos_add_two_pi (x : ℝ) : cos (x + 2 * π) = cos x := by simp [cos_add, cos_two_pi, sin_two_pi] lemma sin_pi_sub (x : ℝ) : sin (π - x) = sin x := by simp [sub_eq_add_neg, sin_add] lemma cos_add_pi (x : ℝ) : cos (x + π) = -cos x := by simp [cos_add] lemma cos_pi_sub (x : ℝ) : cos (π - x) = -cos x := by simp [sub_eq_add_neg, cos_add] lemma sin_add_pi_div_two (x : ℝ) : sin (x + π / 2) = cos x := by simp [sin_add] lemma sin_sub_pi_div_two (x : ℝ) : sin (x - π / 2) = -cos x := by simp [sub_eq_add_neg, sin_add] lemma sin_pi_div_two_sub (x : ℝ) : sin (π / 2 - x) = cos x := by simp [sub_eq_add_neg, sin_add] lemma cos_add_pi_div_two (x : ℝ) : cos (x + π / 2) = -sin x := by simp [cos_add] lemma cos_sub_pi_div_two (x : ℝ) : cos (x - π / 2) = sin x := by simp [sub_eq_add_neg, cos_add] lemma cos_pi_div_two_sub (x : ℝ) : cos (π / 2 - x) = sin x := by rw [← cos_neg, neg_sub, cos_sub_pi_div_two] lemma sin_nat_mul_pi (n : ℕ) : sin (n * π) = 0 := by induction n; simp [add_mul, sin_add, *] lemma sin_int_mul_pi (n : ℤ) : sin (n * π) = 0 := by cases n; simp [add_mul, sin_add, *, sin_nat_mul_pi] lemma cos_nat_mul_two_pi (n : ℕ) : cos (n * (2 * π)) = 1 := by induction n; simp [*, mul_add, cos_add, add_mul, cos_two_pi, sin_two_pi] lemma cos_int_mul_two_pi (n : ℤ) : cos (n * (2 * π)) = 1 := by cases n; simp only [cos_nat_mul_two_pi, int.of_nat_eq_coe, int.neg_succ_of_nat_coe, int.cast_coe_nat, int.cast_neg, (neg_mul_eq_neg_mul _ _).symm, cos_neg] lemma cos_int_mul_two_pi_add_pi (n : ℤ) : cos (n * (2 * π) + π) = -1 := by simp [cos_add, sin_add, cos_int_mul_two_pi] end complex
76e3d259055370cb17eb1e0efed953e67ca3048e
4727251e0cd73359b15b664c3170e5d754078599
/src/data/real/pointwise.lean
ba7b6512dacc0738f8fd13db005a3a14f31cb4d3
[ "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,103
lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Eric Wieser -/ import algebra.order.module import data.real.basic /-! # Pointwise operations on sets of reals This file relates `Inf (a • s)`/`Sup (a • s)` with `a • Inf s`/`a • Sup s` for `s : set ℝ`. From these, it relates `⨅ i, a • f i` / `⨆ i, a • f i` with `a • (⨅ i, f i)` / `a • (⨆ i, f i)`, and provides lemmas about distributing `*` over `⨅` and `⨆`. # TODO This is true more generally for conditionally complete linear order whose default value is `0`. We don't have those yet. -/ open set open_locale pointwise variables {ι : Sort*} {α : Type*} [linear_ordered_field α] section mul_action_with_zero variables [mul_action_with_zero α ℝ] [ordered_smul α ℝ] {a : α} lemma real.Inf_smul_of_nonneg (ha : 0 ≤ a) (s : set ℝ) : Inf (a • s) = a • Inf s := begin obtain rfl | hs := s.eq_empty_or_nonempty, { rw [smul_set_empty, real.Inf_empty, smul_zero'] }, obtain rfl | ha' := ha.eq_or_lt, { rw [zero_smul_set hs, zero_smul], exact cInf_singleton 0 }, by_cases bdd_below s, { exact ((order_iso.smul_left ℝ ha').map_cInf' hs h).symm }, { rw [real.Inf_of_not_bdd_below (mt (bdd_below_smul_iff_of_pos ha').1 h), real.Inf_of_not_bdd_below h, smul_zero'] } end lemma real.smul_infi_of_nonneg (ha : 0 ≤ a) (f : ι → ℝ) : a • (⨅ i, f i) = ⨅ i, a • f i := (real.Inf_smul_of_nonneg ha _).symm.trans $ congr_arg Inf $ (range_comp _ _).symm lemma real.Sup_smul_of_nonneg (ha : 0 ≤ a) (s : set ℝ) : Sup (a • s) = a • Sup s := begin obtain rfl | hs := s.eq_empty_or_nonempty, { rw [smul_set_empty, real.Sup_empty, smul_zero'] }, obtain rfl | ha' := ha.eq_or_lt, { rw [zero_smul_set hs, zero_smul], exact cSup_singleton 0 }, by_cases bdd_above s, { exact ((order_iso.smul_left ℝ ha').map_cSup' hs h).symm }, { rw [real.Sup_of_not_bdd_above (mt (bdd_above_smul_iff_of_pos ha').1 h), real.Sup_of_not_bdd_above h, smul_zero'] } end lemma real.smul_supr_of_nonneg (ha : 0 ≤ a) (f : ι → ℝ) : a • (⨆ i, f i) = ⨆ i, a • f i := (real.Sup_smul_of_nonneg ha _).symm.trans $ congr_arg Sup $ (range_comp _ _).symm end mul_action_with_zero section module variables [module α ℝ] [ordered_smul α ℝ] {a : α} lemma real.Inf_smul_of_nonpos (ha : a ≤ 0) (s : set ℝ) : Inf (a • s) = a • Sup s := begin obtain rfl | hs := s.eq_empty_or_nonempty, { rw [smul_set_empty, real.Inf_empty, real.Sup_empty, smul_zero'] }, obtain rfl | ha' := ha.eq_or_lt, { rw [zero_smul_set hs, zero_smul], exact cInf_singleton 0 }, by_cases bdd_above s, { exact ((order_iso.smul_left_dual ℝ ha').map_cSup' hs h).symm }, { rw [real.Inf_of_not_bdd_below (mt (bdd_below_smul_iff_of_neg ha').1 h), real.Sup_of_not_bdd_above h, smul_zero'] } end lemma real.smul_supr_of_nonpos (ha : a ≤ 0) (f : ι → ℝ) : a • (⨆ i, f i) = ⨅ i, a • f i := (real.Inf_smul_of_nonpos ha _).symm.trans $ congr_arg Inf $ (range_comp _ _).symm lemma real.Sup_smul_of_nonpos (ha : a ≤ 0) (s : set ℝ) : Sup (a • s) = a • Inf s := begin obtain rfl | hs := s.eq_empty_or_nonempty, { rw [smul_set_empty, real.Sup_empty, real.Inf_empty, smul_zero] }, obtain rfl | ha' := ha.eq_or_lt, { rw [zero_smul_set hs, zero_smul], exact cSup_singleton 0 }, by_cases bdd_below s, { exact ((order_iso.smul_left_dual ℝ ha').map_cInf' hs h).symm }, { rw [real.Sup_of_not_bdd_above (mt (bdd_above_smul_iff_of_neg ha').1 h), real.Inf_of_not_bdd_below h, smul_zero] } end lemma real.smul_infi_of_nonpos (ha : a ≤ 0) (f : ι → ℝ) : a • (⨅ i, f i) = ⨆ i, a • f i := (real.Sup_smul_of_nonpos ha _).symm.trans $ congr_arg Sup $ (range_comp _ _).symm end module /-! ## Special cases for real multiplication -/ section mul variables {r : ℝ} lemma real.mul_infi_of_nonneg (ha : 0 ≤ r) (f : ι → ℝ) : r * (⨅ i, f i) = ⨅ i, r * f i := real.smul_infi_of_nonneg ha f lemma real.mul_supr_of_nonneg (ha : 0 ≤ r) (f : ι → ℝ) : r * (⨆ i, f i) = ⨆ i, r * f i := real.smul_supr_of_nonneg ha f lemma real.mul_infi_of_nonpos (ha : r ≤ 0) (f : ι → ℝ) : r * (⨅ i, f i) = ⨆ i, r * f i := real.smul_infi_of_nonpos ha f lemma real.mul_supr_of_nonpos (ha : r ≤ 0) (f : ι → ℝ) : r * (⨆ i, f i) = ⨅ i, r * f i := real.smul_supr_of_nonpos ha f lemma real.infi_mul_of_nonneg (ha : 0 ≤ r) (f : ι → ℝ) : (⨅ i, f i) * r = ⨅ i, f i * r := by simp only [real.mul_infi_of_nonneg ha, mul_comm] lemma real.supr_mul_of_nonneg (ha : 0 ≤ r) (f : ι → ℝ) : (⨆ i, f i) * r = ⨆ i, f i * r := by simp only [real.mul_supr_of_nonneg ha, mul_comm] lemma real.infi_mul_of_nonpos (ha : r ≤ 0) (f : ι → ℝ) : (⨅ i, f i) * r = ⨆ i, f i * r := by simp only [real.mul_infi_of_nonpos ha, mul_comm] lemma real.supr_mul_of_nonpos (ha : r ≤ 0) (f : ι → ℝ) : (⨆ i, f i) * r = ⨅ i, f i * r := by simp only [real.mul_supr_of_nonpos ha, mul_comm] end mul
3900a0a80f89b4e0320e6279fb3e2e1cbc9eac11
9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e
/src/exercises/mathcomp_book/chapter_3.lean
c7118f46ef80334fc4bc97504fe3f7ae59478a4e
[]
no_license
agusakov/lean_lib
c0e9cc29fc7d2518004e224376adeb5e69b5cc1a
f88d162da2f990b87c4d34f5f46bbca2bbc5948e
refs/heads/master
1,642,141,461,087
1,557,395,798,000
1,557,395,798,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,735
lean
import data.nat.choose data.nat.prime data.fintype algebra.big_operators import tactic.ring /- --------------------------------------------------------------- -/ -- Section 3.1.2 #check Prop #check Type #check Type 5 def my_matrix (R : Type) (n m : ℕ) : Type := (fin n) → (fin m) → R def my_matrix.mul {R : Type} [ring R] {n m p : ℕ} (M : my_matrix R n m) (N : my_matrix R m p) : my_matrix R n p := λ i k,(@finset.univ (fin m) _).sum (λ j, (M i j) * (N j k)) lemma bool_irrelevance (P Q : bool) : ∀ e1 e2 : P = Q, e1 = e2 | _ _ := rfl lemma proof_irrelevance (P : Prop) (h1 h2 : P) : h1 = h2 := by {refl} /- --------------------------------------------------------------- -/ -- Section 3.1.3 inductive my_nat : Type | O : my_nat | S : my_nat → my_nat def my_pred : my_nat → my_nat | my_nat.O := my_nat.O | (my_nat.S n) := n def my_nat.add : ∀ (n m : my_nat), my_nat | my_nat.O m := m | (my_nat.S n) m := my_nat.S (my_nat.add n m) /- --------------------------------------------------------------- -/ -- Section 3.1.4 inductive my_and (A B : Prop) : Prop | conj : A → B → my_and inductive my_prod (A B : Type) : Type | pair : A → B → my_prod def my_fst {A B : Type} : my_prod A B → A | (my_prod.pair a _) := a def my_snd {A B : Type} : my_prod A B → B | (my_prod.pair _ b) := b inductive my_exists {A : Type} (P : A → Prop) | intro : ∀ (a : A) (h : P a), my_exists example : my_exists (λ n : ℕ , n * n + 1 = 2 * n) := my_exists.intro 1 rfl inductive my_or (P Q : Prop) | intro_left : P → my_or | intro_right : Q → my_or def my_or.ind (P Q R : Prop) (h : my_or P Q) (p : P → R) (q : Q → R) : R := by {cases h, exact p h, exact q h} def my_or.ind' (P Q R : Prop) (h : my_or P Q) (p : P → R) (q : Q → R) : R := @my_or.cases_on P Q (λ _, R) h p q inductive my_true : Prop | trivial inductive my_false : Prop . def my_not (P : Prop) := P → my_false def my_false.elim : ∀ (P : Prop) (h : my_false), P . inductive my_eq {A : Type} : ∀ a b : A, Prop | rfl : ∀ a : A, my_eq a a def my_eq.subst {A : Type} (P : A → Prop) : ∀ {x : A} (px : P x) {y : A} (e : my_eq x y), P y | _ px _ (my_eq.rfl x) := px inductive my_odd : ∀ (n : ℕ), Prop | base : my_odd 1 | step : ∀ n, my_odd n → my_odd (n + 2) /- --------------------------------------------------------------- -/ -- Section 3.2.2 example : ∀ (xy : ℕ × ℕ), nat.prime xy.1 → (my_odd xy.2) → 2 < xy.2 + xy.1 := begin rintros ⟨x,_⟩ pr_x (a|⟨y0,b⟩); dsimp[prod.fst,prod.snd,nat.add] at *; rw[nat.succ_add]; apply nat.succ_lt_succ; apply nat.lt_of_lt_of_le pr_x.gt_one; apply nat.le_add_left _, end /- --------------------------------------------------------------- -/ -- Section 3.2.4 lemma nat.wf_ind (P : ℕ → Prop) (h : ∀ n,(∀ m, m < n → P m) → P n) (n : ℕ) : P n := begin let Q : ℕ → Prop := λ n,(∀ m, m < n → P m), have Q_zero : Q 0 := λ m h, false.elim ((nat.not_lt_zero m) h), have Q_step : ∀ n, Q n → Q (n + 1) := begin intros n hn m hm, rcases le_iff_eq_or_lt.mp (nat.le_of_succ_le_succ hm) with h_eq | h_lt, {rw[h_eq],exact h n hn,}, {exact hn m h_lt} end, exact @nat.rec Q Q_zero Q_step (n + 1) n (nat.lt_succ_self n), end -- def oops (n : ℕ) : false := oops n #print nat.below #check nat.brec_on lemma stamps_aux (n : ℕ) : ∀ (m : ℕ), m < n → ∃ s4 s5 : ℕ, s4 * 4 + s5 * 5 = m + 12 := begin induction n with n0 ih; intros m h, {exfalso,exact nat.not_lt_zero m h,}, rcases lt_trichotomy m n0 with h0|h1|h2, {exact ih m h0,}, {rw[h1], cases n0,{use 3, use 0, refl}, cases n0,{use 2, use 1, refl}, cases n0,{use 1, use 2, refl}, cases n0,{use 0, use 3, refl}, have h3 : n0 < n0 + 4 := nat.add_lt_add_left (dec_trivial : 0 < 4) n0, rcases (ih n0 h3) with ⟨s4,s5,e⟩, use s4 + 1,use s5, have e1 := calc (s4 + 1) * 4 + s5 * 5 = (s4 * 4 + s5 * 5) + 4 : by { ring } ... = (n0 + 12) + 4 : by rw[e] ... = (n0 + 4) + 12 : by { ring }, exact e1, }, {exfalso, exact not_le_of_lt h2 (nat.le_of_succ_le_succ h),} end lemma stamps (n : ℕ) (h : 12 ≤ n) : ∃ s4 s5 : ℕ, s4 * 4 + s5 * 5 = n := begin rw[← nat.sub_add_cancel h], exact stamps_aux ((n - 12) + 1) (n - 12) (nat.lt_succ_self _), end /- --------------------------------------------------------------- -/ -- Section 3.3 def classically (P : Prop) := ¬ (¬ P) lemma my_em (P : Prop) : classically (P ∨ ¬ P) := λ h, h (or.inr (λ p, h(or.inl p))) lemma classically.pure (P : Prop) : P → classically P := λ p np, (np p) lemma classically.bind (P Q : Prop) : (P → classically Q) → (classically P) → (classically Q) := λ hi pc nq, pc (λ p, hi p nq)
18f850ed2adb3648575aaf728b6fa2b2e68e3a8c
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/tactic/omega/eq_elim.lean
b0509d419eb40b5632f0cc8e4b43954650dbc648
[ "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
14,660
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek Correctness lemmas for equality elimination. See 5.5 of <http://www.decision-procedures.org/> for details. -/ import tactic.omega.clause open list.func namespace omega def symdiv (i j : int) : int := if (2 * (i % j)) < j then i / j else (i / j) + 1 def symmod (i j : int) : int := if (2 * (i % j)) < j then i % j else (i % j) - j lemma symmod_add_one_self {i : int} : 0 < i → symmod i (i+1) = -1 := begin intro h1, unfold symmod, rw [int.mod_eq_of_lt (le_of_lt h1) (lt_add_one _), if_neg], simp only [add_comm, add_neg_cancel_left, neg_add_rev, sub_eq_add_neg], have h2 : 2 * i = (1 + 1) * i := rfl, simpa only [h2, add_mul, one_mul, add_lt_add_iff_left, not_lt] using h1 end lemma mul_symdiv_eq {i j : int} : j * (symdiv i j) = i - (symmod i j) := begin unfold symdiv, unfold symmod, by_cases h1 : (2 * (i % j)) < j, { repeat {rw if_pos h1}, rw [int.mod_def, sub_sub_cancel] }, { repeat {rw if_neg h1}, rw [int.mod_def, sub_sub, sub_sub_cancel, mul_add, mul_one] } end lemma symmod_eq {i j : int} : symmod i j = i - j * (symdiv i j) := by rw [mul_symdiv_eq, sub_sub_cancel] /-- (sgm v b as n) is the new value assigned to the nth variable after a single step of equality elimination using valuation v, term ⟨b, as⟩, and variable index n. If v satisfies the initial constraint set, then (v ⟨n ↦ sgm v b as n⟩) satisfies the new constraint set after equality elimination. -/ def sgm (v : nat → int) (b : int) (as : list int) (n : nat) := let a_n : int := get n as in let m : int := a_n + 1 in ((symmod b m) + (coeffs.val v (as.map (λ x, symmod x m)))) / m open_locale list.func def rhs : nat → int → list int → term | n b as := let m := get n as + 1 in ⟨(symmod b m), (as.map (λ x, symmod x m)) {n ↦ -m}⟩ lemma rhs_correct_aux {v : nat → int} {m : int} {as : list int} : ∀ {k}, ∃ d, (m * d + coeffs.val_between v (as.map (λ (x : ℤ), symmod x m)) 0 k = coeffs.val_between v as 0 k) | 0 := begin existsi (0 : int), simp only [add_zero, mul_zero, coeffs.val_between] end | (k+1) := begin simp only [zero_add, coeffs.val_between, list.map], cases @rhs_correct_aux k with d h1, rw ← h1, by_cases hk : k < as.length, { rw [get_map hk, symmod_eq, sub_mul], existsi (d + (symdiv (get k as) m * v k)), ring }, { rw not_lt at hk, repeat {rw get_eq_default_of_le}, existsi d, rw add_assoc, exact hk, simp only [hk, list.length_map] } end open_locale omega lemma rhs_correct {v : nat → int} {b : int} {as : list int} (n : nat) : 0 < get n as → 0 = term.val v (b,as) → v n = term.val (v ⟨n ↦ sgm v b as n⟩) (rhs n b as) := begin intros h0 h1, let a_n := get n as, let m := a_n + 1, have h3 : m ≠ 0, { apply ne_of_gt, apply lt_trans h0, simp [a_n, m] }, have h2 : m * (sgm v b as n) = (symmod b m) + coeffs.val v (as.map (λ x, symmod x m)), { simp only [sgm, mul_comm m], rw [int.div_mul_cancel], have h4 : ∃ c, m * c + (symmod b (get n as + 1) + coeffs.val v (as.map (λ (x : ℤ), symmod x m))) = term.val v (b,as), { have h5: ∃ d, m * d + (coeffs.val v (as.map (λ x, symmod x m))) = coeffs.val v as, { simp only [coeffs.val, list.length_map], apply rhs_correct_aux }, cases h5 with d h5, rw symmod_eq, existsi (symdiv b m + d), unfold term.val, rw ← h5, simp only [term.val, mul_add, add_mul, m, a_n], ring }, cases h4 with c h4, rw [dvd_add_iff_right (dvd_mul_right m c), h4, ← h1], apply dvd_zero }, apply calc v n = -(m * sgm v b as n) + (symmod b m) + (coeffs.val_except n v (as.map (λ x, symmod x m))) : begin rw [h2, ← coeffs.val_except_add_eq n], have hn : n < as.length, { by_contra hc, rw not_lt at hc, rw (get_eq_default_of_le n hc) at h0, cases h0 }, rw get_map hn, simp only [a_n, m], rw [add_comm, symmod_add_one_self h0], ring end ... = term.val (v⟨n↦sgm v b as n⟩) (rhs n b as) : begin unfold rhs, unfold term.val, rw [← coeffs.val_except_add_eq n, get_set, update_eq], have h2 : ∀ a b c : int, a + b + c = b + (c + a) := by {intros, ring}, rw (h2 (- _)), apply fun_mono_2 rfl, apply fun_mono_2, { rw coeffs.val_except_update_set }, { simp only [m, a_n], ring } end end def sym_sym (m b : int) : int := symdiv b m + symmod b m def coeffs_reduce : nat → int → list int → term | n b as := let a := get n as in let m := a + 1 in (sym_sym m b, (as.map (sym_sym m)) {n ↦ -a}) lemma coeffs_reduce_correct {v : nat → int} {b : int} {as : list int} {n : nat} : 0 < get n as → 0 = term.val v (b,as) → 0 = term.val (v ⟨n ↦ sgm v b as n⟩) (coeffs_reduce n b as) := begin intros h1 h2, let a_n := get n as, let m := a_n + 1, have h3 : m ≠ 0, { apply ne_of_gt, apply lt_trans h1, simp only [m, lt_add_iff_pos_right] }, have h4 : 0 = (term.val (v⟨n↦sgm v b as n⟩) (coeffs_reduce n b as)) * m := calc 0 = term.val v (b,as) : h2 ... = b + coeffs.val_except n v as + a_n * ((rhs n b as).val (v⟨n ↦ sgm v b as n⟩)) : begin unfold term.val, rw [← coeffs.val_except_add_eq n, rhs_correct n h1 h2], simp only [a_n, add_assoc], end ... = -(m * a_n * sgm v b as n) + (b + a_n * (symmod b m)) + (coeffs.val_except n v as + a_n * coeffs.val_except n v (as.map (λ x, symmod x m))) : begin simp only [term.val, rhs, mul_add, m, a_n, add_assoc, add_left_inj, add_comm, add_left_comm], rw [← coeffs.val_except_add_eq n, get_set, update_eq, mul_add], apply fun_mono_2, { rw coeffs.val_except_eq_val_except update_eq_of_ne (get_set_eq_of_ne _) }, simp only [m], ring, end ... = -(m * a_n * sgm v b as n) + (b + a_n * (symmod b m)) + coeffs.val_except n v (as.map (λ a_i, a_i + a_n * (symmod a_i m))) : begin apply fun_mono_2 rfl, simp only [coeffs.val_except, mul_add], repeat {rw ← coeffs.val_between_map_mul}, have h4 : ∀ {a b c d : int}, a + b + (c + d) = (a + c) + (b + d), { intros, ring }, rw h4, have h5 : add as (list.map (has_mul.mul a_n) (list.map (λ (x : ℤ), symmod x (get n as + 1)) as)) = list.map (λ (a_i : ℤ), a_i + a_n * symmod a_i m) as, { rw [list.map_map, ←map_add_map], apply fun_mono_2, { have h5 : (λ x : int, x) = id, { rw function.funext_iff, intro x, refl }, rw [h5, list.map_id] }, { apply fun_mono_2 _ rfl, rw function.funext_iff, intro x, simp only [m] } }, simp only [list.length_map], repeat { rw [← coeffs.val_between_add, h5] }, end ... = -(m * a_n * sgm v b as n) + (m * sym_sym m b) + coeffs.val_except n v (as.map (λ a_i, m * sym_sym m a_i)) : begin repeat {rw add_assoc}, apply fun_mono_2, refl, rw ← add_assoc, have h4 : ∀ (x : ℤ), x + a_n * symmod x m = m * sym_sym m x, { intro x, have h5 : a_n = m - 1, { simp only [m], rw add_sub_cancel }, rw [h5, sub_mul, one_mul, add_sub, add_comm, add_sub_assoc, ← mul_symdiv_eq], simp only [sym_sym, mul_add, add_comm] }, apply fun_mono_2 (h4 _), apply coeffs.val_except_eq_val_except; intros x h5, refl, apply congr_arg, apply fun_mono_2 _ rfl, rw function.funext_iff, apply h4 end ... = (-(a_n * sgm v b as n) + (sym_sym m b) + coeffs.val_except n v (as.map (sym_sym m))) * m : begin simp only [add_mul _ _ m], apply fun_mono_2, ring, simp only [coeffs.val_except, add_mul _ _ m], apply fun_mono_2, { rw [mul_comm _ m, ← coeffs.val_between_map_mul, list.map_map] }, simp only [list.length_map, mul_comm _ m], rw [← coeffs.val_between_map_mul, list.map_map] end ... = (sym_sym m b + (coeffs.val_except n v (as.map (sym_sym m)) + (-a_n * sgm v b as n))) * m : by ring ... = (term.val (v ⟨n ↦ sgm v b as n⟩) (coeffs_reduce n b as)) * m : begin simp only [coeffs_reduce, term.val, m, a_n], rw [← coeffs.val_except_add_eq n, coeffs.val_except_update_set, get_set, update_eq] end, rw [← int.mul_div_cancel (term.val _ _) h3, ← h4, int.zero_div] end -- Requires : t1.coeffs[m] = 1 def cancel (m : nat) (t1 t2 : term) : term := term.add (t1.mul (-(get m (t2.snd)))) t2 def subst (n : nat) (t1 t2 : term) : term := term.add (t1.mul (get n t2.snd)) (t2.fst, t2.snd {n ↦ 0}) lemma subst_correct {v : nat → int} {b : int} {as : list int} {t : term} {n : nat} : 0 < get n as → 0 = term.val v (b,as) → term.val v t = term.val (v ⟨n ↦ sgm v b as n⟩) (subst n (rhs n b as) t) := begin intros h1 h2, simp only [subst, term.val, term.val_add, term.val_mul], rw ← rhs_correct _ h1 h2, cases t with b' as', simp only [term.val], have h3 : coeffs.val (v ⟨n ↦ sgm v b as n⟩) (as' {n ↦ 0}) = coeffs.val_except n v as', { rw [← coeffs.val_except_add_eq n, get_set, zero_mul, add_zero, coeffs.val_except_update_set] }, rw [h3, ← coeffs.val_except_add_eq n], ring end /-- The type of equality elimination rules. -/ @[derive has_reflect, derive inhabited] inductive ee : Type | drop : ee | nondiv : int → ee | factor : int → ee | neg : ee | reduce : nat → ee | cancel : nat → ee namespace ee def repr : ee → string | drop := "↓" | (nondiv i) := i.repr ++ "∤" | (factor i) := "/" ++ i.repr | neg := "-" | (reduce n) := "≻" ++ n.repr | (cancel n) := "+" ++ n.repr instance has_repr : has_repr ee := ⟨repr⟩ meta instance has_to_format : has_to_format ee := ⟨λ x, x.repr⟩ end ee /-- Apply a given sequence of equality elimination steps to a clause. -/ def eq_elim : list ee → clause → clause | [] ([], les) := ([],les) | [] ((_::_), les) := ([],[]) | (_::_) ([], les) := ([],[]) | (ee.drop::es) ((eq::eqs), les) := eq_elim es (eqs, les) | (ee.neg::es) ((eq::eqs), les) := eq_elim es ((eq.neg::eqs), les) | (ee.nondiv i::es) ((b,as)::eqs, les) := if ¬(i ∣ b) ∧ (∀ x ∈ as, i ∣ x) then ([],[⟨-1,[]⟩]) else ([],[]) | (ee.factor i::es) ((b,as)::eqs, les) := if (i ∣ b) ∧ (∀ x ∈ as, i ∣ x) then eq_elim es ((term.div i (b,as)::eqs), les) else ([],[]) | (ee.reduce n::es) ((b,as)::eqs, les) := if 0 < get n as then let eq' := coeffs_reduce n b as in let r := rhs n b as in let eqs' := eqs.map (subst n r) in let les' := les.map (subst n r) in eq_elim es ((eq'::eqs'), les') else ([],[]) | (ee.cancel m::es) ((eq::eqs), les) := eq_elim es ((eqs.map (cancel m eq)), (les.map (cancel m eq))) open tactic lemma sat_empty : clause.sat ([],[]) := ⟨λ _,0, ⟨dec_trivial, dec_trivial⟩⟩ lemma sat_eq_elim : ∀ {es : list ee} {c : clause}, c.sat → (eq_elim es c).sat | [] ([], les) h := h | (e::_) ([], les) h := by {cases e; simp only [eq_elim]; apply sat_empty} | [] ((_::_), les) h := sat_empty | (ee.drop::es) ((eq::eqs), les) h1 := begin apply (@sat_eq_elim es _ _), rcases h1 with ⟨v,h1,h2⟩, refine ⟨v, list.forall_mem_of_forall_mem_cons h1, h2⟩ end | (ee.neg::es) ((eq::eqs), les) h1 := begin simp only [eq_elim], apply sat_eq_elim, cases h1 with v h1, existsi v, cases h1 with hl hr, apply and.intro _ hr, rw list.forall_mem_cons at *, apply and.intro _ hl.right, rw term.val_neg, rw ← hl.left, refl end | (ee.nondiv i::es) ((b,as)::eqs, les) h1 := begin unfold eq_elim, by_cases h2 : (¬i ∣ b ∧ ∀ (x : ℤ), x ∈ as → i ∣ x), { exfalso, cases h1 with v h1, have h3 : 0 = b + coeffs.val v as := h1.left _ (or.inl rfl), have h4 : i ∣ coeffs.val v as := coeffs.dvd_val h2.right, have h5 : i ∣ b + coeffs.val v as := by { rw ← h3, apply dvd_zero }, rw ← dvd_add_iff_left h4 at h5, apply h2.left h5 }, rw if_neg h2, apply sat_empty end | (ee.factor i::es) ((b,as)::eqs, les) h1 := begin simp only [eq_elim], by_cases h2 : (i ∣ b) ∧ (∀ x ∈ as, i ∣ x), { rw if_pos h2, apply sat_eq_elim, cases h1 with v h1, existsi v, cases h1 with h3 h4, apply and.intro _ h4, rw list.forall_mem_cons at *, cases h3 with h5 h6, apply and.intro _ h6, rw [term.val_div h2.left h2.right, ← h5, int.zero_div] }, { rw if_neg h2, apply sat_empty } end | (ee.reduce n::es) ((b,as)::eqs, les) h1 := begin simp only [eq_elim], by_cases h2 : 0 < get n as, tactic.rotate 1, { rw if_neg h2, apply sat_empty }, rw if_pos h2, apply sat_eq_elim, cases h1 with v h1, existsi v ⟨n ↦ sgm v b as n⟩, cases h1 with h1 h3, rw list.forall_mem_cons at h1, cases h1 with h4 h5, constructor, { rw list.forall_mem_cons, constructor, { apply coeffs_reduce_correct h2 h4 }, { intros x h6, rw list.mem_map at h6, cases h6 with t h6, cases h6 with h6 h7, rw [← h7, ← subst_correct h2 h4], apply h5 _ h6 } }, { intros x h6, rw list.mem_map at h6, cases h6 with t h6, cases h6 with h6 h7, rw [← h7, ← subst_correct h2 h4], apply h3 _ h6 } end | (ee.cancel m::es) ((eq::eqs), les) h1 := begin unfold eq_elim, apply sat_eq_elim, cases h1 with v h1, existsi v, cases h1 with h1 h2, rw list.forall_mem_cons at h1, cases h1 with h1 h3, constructor; intros t h4; rw list.mem_map at h4; rcases h4 with ⟨s,h4,h5⟩; rw ← h5; simp only [term.val_add, term.val_mul, cancel]; rw [← h1, mul_zero, zero_add], { apply h3 _ h4 }, { apply h2 _ h4 } end /-- If the result of equality elimination is unsatisfiable, the original clause is unsatisfiable. -/ lemma unsat_of_unsat_eq_elim (ee : list ee) (c : clause) : (eq_elim ee c).unsat → c.unsat := by {intros h1 h2, apply h1, apply sat_eq_elim h2} end omega
64a591886901ae17fcbc483d3ce21e9f77b29238
cb3da1b91380e7462b579b426a278072c688954a
/src/leu_pin.lean
0bc0ca20d3037b9528929ab61de9dbf0d7192f86
[]
no_license
hcheval/tikhonov-mann
35c93e46a8287f6479a848a9d4f02013e0bc2035
6ab7fcefe9e1156c20bd5d1998a7deabd1eeb018
refs/heads/master
1,689,232,126,344
1,630,182,255,000
1,630,182,255,000
399,859,996
0
0
null
null
null
null
UTF-8
Lean
false
false
13,584
lean
import data.real.basic tactic utils import rates namespace leu_pin section open_locale big_operators open finset (range Ico) @[reducible] def δ (ψ χ : ℕ → ℕ) (k : ℕ) : ℕ := max (ψ $ 3 * k + 2) ((χ $ 3 * k + 2) + 1) variables (a b c s : ℕ → ℝ) (M : ℕ) (ψ χ θ δ₀ : ℕ → ℕ) variables (a_nonneg : ∀ (n : ℕ), 0 ≤ a n) (a_lt_one : ∀ n : ℕ, a n < 1) (a_le_one : ∀ (n : ℕ), a n ≤ 1) variables (c_nonneg : ∀ (n : ℕ), 0 ≤ c n) (s_nonneg : ∀ (n : ℕ), 0 ≤ s n) variables (main_ineq : ∀ (n : ℕ), s (n + 1) ≤ (1 - a n) * s n + a n * b n + c n) include a_nonneg a_le_one c_nonneg s_nonneg main_ineq lemma leu_pin_3_2 (p N : ℕ) (h : ∀ n : ℕ, N ≤ n → b n ≤ 1 / (p + 1)) : ∀ n m : ℕ, N ≤ n → s (n + m + 1) ≤ (∏ i in range (m + 1), (1 - a (n + i))) * s n + (1 - ∏ i in range (m + 1), (1 - a (n + i))) * (1 / (p + 1)) + ∑ i in range (m + 1), c (n + i) := begin intros n m h_N_le_n, induction m, case zero { simp only [add_zero, finset.prod_singleton, sub_sub_cancel, finset.sum_singleton, finset.range_one], specialize_all [n] at *, specialize h.n h_N_le_n, nlinarith, }, case succ: m ih { have one_sub_a_nonneg : 0 ≤ 1 - a (n + m + 1) := sub_nonneg.mpr (a_le_one $ n + m + 1), have one_sub_a_le_one : 1 - a (n + m + 1) ≤ 1 := sub_le_self _ (a_nonneg $ n + m + 1), set A := ∏ i in range (m + 1), (1 - a (n + i)), have h₁ : (1 - a (n + m + 1)) * A = (∏ i in range (m + 1 + 1), (1 - (a $ n + i))) := by { dsimp [A], have : (1 - (a $ n + m + 1)) = (λ i, (1 - a (n + i))) (m + 1) := by { dsimp only, refl, }, rw [this, ←finset.prod_range_succ_comm], }, calc s (n + m.succ + 1) ≤ (1 - (a $ n + m + 1)) * (s $ n + m + 1) + (a $ n + m + 1) * (b $ n + m + 1) + (c $ n + m + 1) : by apply main_ineq ... ≤ (1 - (a $ n + m + 1)) * (A * s n + (1 - A) * (1 / (↑p + 1)) + ∑ (i : ℕ) in range (m + 1), c (n + i)) + (a $ n + m + 1) * (b $ n + m + 1) + (c $ n + m + 1) : by nlinarith ... ≤ (1 - (a $ n + m + 1)) * (A * s n + (1 - A) * (1 / (↑p + 1)) + ∑ (i : ℕ) in range (m + 1), c (n + i)) + (a $ n + m + 1) * (1 / (↑p + 1)) + (c $ n + m + 1) : by nlinarith [a_nonneg (n + m + 1), h (n + m + 1) (by linarith)] ... = (1 - (a $ n + m + 1)) * (A * s n) + (1 - (a $ n + m + 1)) * (1 - A) * (1 / (↑p + 1)) + (1 - (a $ n + m + 1)) * ( ∑ (i : ℕ) in range (m + 1), c (n + i)) + (a $ n + m + 1) * (1 / (↑p + 1)) + (c $ n + m + 1) : by ring ... = (1 - (a $ n + m + 1)) * (A * s n) + ((1 - (a $ n + m + 1)) * (1 - A) + (a $ n + m + 1)) * (1 / (↑p + 1)) + (1 - (a $ n + m + 1)) * ( ∑ (i : ℕ) in range (m + 1), c (n + i)) + (c $ n + m + 1) : by ring ... = (((1 - (a $ n + m + 1)) * A) * s n) + ((1 - (a $ n + m + 1)) * (1 - A) + (a $ n + m + 1)) * (1 / (↑p + 1)) + (1 - (a $ n + m + 1)) * ( ∑ (i : ℕ) in range (m + 1), c (n + i)) + (c $ n + m + 1) : by rw mul_assoc ... = ((∏ i in range (m.succ + 1), (1 - (a $ n + i))) * s n) + ((1 - (a $ n + m + 1)) * (1 - A) + (a $ n + m + 1)) * (1 / (↑p + 1)) + (1 - (a $ n + m + 1)) * ( ∑ (i : ℕ) in range (m + 1), c (n + i)) + (c $ n + m + 1) : by rw h₁ ... = ((∏ i in range (m.succ + 1), (1 - (a $ n + i))) * s n) + ((1 - (a $ n + m + 1)) - (1 - (a $ n + m + 1)) * A + (a $ n + m + 1)) * (1 / (↑p + 1)) + (1 - (a $ n + m + 1)) * ( ∑ (i : ℕ) in range (m + 1), c (n + i)) + (c $ n + m + 1) : by ring ... = ((∏ i in range (m.succ + 1), (1 - (a $ n + i))) * s n) + (1 - (1 - (a $ n + m + 1)) * A) * (1 / (↑p + 1)) + (1 - (a $ n + m + 1)) * ( ∑ (i : ℕ) in range (m + 1), c (n + i)) + (c $ n + m + 1) : by ring ... = ((∏ i in range (m.succ + 1), (1 - (a $ n + i))) * s n) + (1 - (∏ i in range (m.succ + 1), (1 - (a $ n + i)))) * (1 / (↑p + 1)) + (1 - (a $ n + m + 1)) * ( ∑ (i : ℕ) in range (m + 1), c (n + i)) + (c $ n + m + 1) : by rw h₁ ... ≤ ((∏ i in range (m.succ + 1), (1 - (a $ n + i))) * s n) + (1 - (∏ i in range (m.succ + 1), (1 - (a $ n + i)))) * (1 / (↑p + 1)) + ∑ i in range (m.succ + 1), c (n + i) : by { nth_rewrite 1 finset.sum_range_succ, have h₂ : (1 - a (n + m + 1)) * ∑ (i : ℕ) in range (m + 1), c (n + i) ≤ ∑ (x : ℕ) in range (m.succ), c (n + x) := mul_le_of_le_one_left (finset.sum_nonneg (λ i _, c_nonneg $ n + i)) one_sub_a_le_one, rw [(show n + m + 1 = n + m.succ, by rw add_assoc), ←add_assoc], repeat { rw [add_comm _ (c $ n + m.succ)] }, repeat { apply add_le_add_left }, exact h₂, } } end lemma leu_pin_3_3 (M_pos : 0 < M) (s_le_M : ∀ n, s n ≤ M) (b_le_inv_k_succ : ∀ k n : ℕ, ψ k ≤ n → b n ≤ 1 / (k + 1)) (χ_cauchy_modulus_sum_c : is_cauchy_modulus χ (λ n, ∑ i in range (n + 1), c i)) : ∀ k m n : ℕ, δ ψ χ k ≤ n → s (n + m + 1) ≤ M * (∏ i in range (m + 1), (1 - a (n + i))) + 2 / (↑(3 * k + 2) + 1):= begin -- intros k m n δ_le_n, have h₁ : ∀ k, ψ (3 * k + 2) ≤ δ ψ χ k := λ _, le_max_left _ _, have h₂ : ∀ k n, δ ψ χ k ≤ n → ψ (3 * k + 2) ≤ n := λ _ _ δ_le_n, le_trans (h₁ _) δ_le_n, have h₃ : ∀ k n, ψ (3 * k + 2) ≤ n → b n ≤ 1 / (↑(3 * k + 2) + 1):= λ k n ψ_le_n, b_le_inv_k_succ _ _ ψ_le_n, have h₄ : ∀ k n, δ ψ χ k ≤ n → b n ≤ 1 / (↑(3 * k + 2) + 1) := λ k n δ_le_n, h₃ _ _ (h₂ _ _ δ_le_n), intros k m n δ_le_n, have := leu_pin_3_2 a b c s a_nonneg a_le_one c_nonneg s_nonneg main_ineq (3 * k + 2) (δ ψ χ k) (h₄ k) n m δ_le_n, have : 0 ≤ (∏ i in range (m + 1), (1 - (a $ n + i))) := finset.prod_nonneg (λ i _, sub_nonneg.mpr $ a_le_one $ n + i), have h₅ : s (n + m + 1) ≤ M * (∏ i in range (m + 1), (1 - (a $ n + i))) + (1 / (↑(3 * k + 2) + 1)) + ∑ i in range (m + 1), c (n + i) := calc s (n + m + 1) ≤ (∏ (i : ℕ) in range (m + 1), (1 - a (n + i))) * M + (1 - ∏ (i : ℕ) in range (m + 1), (1 - a (n + i))) * (1 / (↑(3 * k + 2) + 1)) + ∑ (i : ℕ) in range (m + 1), c (n + i) : by nlinarith [s_le_M n] ... ≤ (∏ (i : ℕ) in range (m + 1), (1 - a (n + i))) * M + (1 / (↑(3 * k + 2) + 1)) + ∑ (i : ℕ) in range (m + 1), c (n + i) : by { -- have h₀' : (∏ (i : ℕ) in range (m + 1), (1 - a (n + i))) ≤ 1 := finset.prod_le_one (λ i _, sub_nonneg.mpr $ a_le_one $ n + i) (λ i _, sub_le_self 1 $ a_nonneg $ n + i), have h₁' : (1 - ∏ (i : ℕ) in range (m + 1), (1 - a (n + i))) ≤ 1 := sub_le_self 1 this, have h₂' : (0 : ℝ) ≤ (1 / (↑(3 * k + 2) + 1)) := one_div_nonneg.mpr (by { have : (0 : ℝ) ≤ (↑(3 * k + 2)) := nat.cast_nonneg (3 * k + 2), nlinarith }), have : (1 - ∏ (i : ℕ) in range (m + 1), (1 - a (n + i))) * (1 / (↑(3 * k + 2) + 1)) ≤ 1 / (↑(3 * k + 2) + 1) := mul_le_of_le_one_left h₂' h₁', nlinarith, } ... ≤ M * (∏ i in range (m + 1), (1 - (a $ n + i))) + (1 / (↑(3 * k + 2) + 1)) + ∑ i in range (m + 1), c (n + i) : by rw [mul_comm _ ↑M], have h₆ : (χ $ 3 * k + 2) + 1 ≤ n := le_trans (le_max_right _ _) δ_le_n, set r : ℕ := n + m - χ (3 * k + 2), have h₇ : ∑ i in range (m + 1), c (n + i) ≤ 1 / (↑(3 * k + 2) + 1) := calc ∑ i in range (m + 1), c (n + i) = ∑ i in Ico n (n + m + 1), c i : by { have : n + m + 1 - n = m + 1 := by { rw add_assoc, simp only [nat.add_sub_cancel_left], }, rw ←this, rw finset.sum_Ico_eq_sum_range, } ... ≤ ∑ i in Ico ((χ $ 3 * k + 2) + 1) (n + m + 1), c i : by { apply @finset.sum_mono_set_of_nonneg ℕ ℝ _ _ c_nonneg, apply le_Ico_of_le_left _ h₆, } ... = ∑ i in Ico 0 (n + m + 1), c i - ∑ i in Ico 0 (χ (3 * k + 2) + 1), c i : by { have h₁' : ∑ i in Ico ((χ $ 3 * k + 2) + 1) (n + m + 1), c i + ∑ i in Ico 0 (χ (3 * k + 2) + 1), c i = ∑ i in Ico 0 (n + m + 1), c i := by { have : Ico 0 (n + m + 1) \ Ico 0 (χ (3 * k + 2) + 1) = Ico (χ (3 * k + 2) + 1) (n + m + 1) := by apply finset.Ico.diff_left, rw ←this, apply finset.sum_sdiff, have h₂' : (χ (3 * k + 2)).succ ≤ n + m + 1 := by { have h₁'' : χ (3 * k + 2) + 1 ≤ n + 1 := add_le_add_right (le_trans (nat.le_succ _) h₆) 1, have h₂'' : n + 1 ≤ n + m + 1 := by { rw [add_comm n m, add_assoc], exact le_add_self, }, exact le_trans h₁'' h₂'', }, refine (finset.Ico.subset_iff (nat.succ_pos _)).mpr ⟨le_rfl, h₂'⟩, }, exact eq_sub_of_add_eq h₁', } ... = ∑ i in range (n + m + 1), c i - ∑ i in range (χ (3 * k + 2) + 1), c i : by { repeat { rw finset.range_eq_Ico }, } ... = ∑ i in range (χ (3 * k + 2) + r + 1), c i - ∑ i in range (χ (3 * k + 2) + 1), c i : by { have : n + m = χ (3 * k + 2) + r := by { -- simp [r], have : χ (3 * k + 2) ≤ n + m := by nlinarith, rw [←nat.add_sub_assoc this _], simp only [nat.add_sub_cancel_left], }, rw this, } ... ≤ 1 / (↑(3 * k + 2) + 1) : by { specialize χ_cauchy_modulus_sum_c (3 * k + 2) (χ $ 3 * k + 2) r (le_refl _), rw real.dist_eq at χ_cauchy_modulus_sum_c, rw [abs_eq_self.mpr _] at χ_cauchy_modulus_sum_c, exact χ_cauchy_modulus_sum_c, dsimp only, rw [show χ (3 * k + 2) + r + 1 = χ (3 * k + 2) + 1 + r, by ring], apply nonneg_sub_of_nonneg_sum c_nonneg, }, calc s (n + m + 1) ≤ M * (∏ i in range (m + 1), (1 - (a $ n + i))) + (1 / (↑(3 * k + 2) + 1)) + ∑ i in range (m + 1), c (n + i) : h₅ ... ≤ M * (∏ i in range (m + 1), (1 - (a $ n + i))) + (1 / (↑(3 * k + 2) + 1)) + 1 / (↑(3 * k + 2) + 1) : by nlinarith ... = ↑M * ∏ (i : ℕ) in range (m + 1), (1 - a (n + i)) + 2 / (↑(3 * k + 2) + 1) : by ring, end omit a_le_one include a_lt_one lemma leu_pin_3_5 (M_pos : 0 < M) (s_le_M : ∀ n, s n ≤ M) (b_le_inv_k_succ : ∀ k n : ℕ, ψ k ≤ n → b n ≤ 1 / ↑(k + 1)) (χ_cauchy_modulus_sum_c : is_cauchy_modulus χ (λ n, ∑ i in range (n + 1), c i)) -- (a_lt_one : ∀ n : ℕ, a n < 1) (θ_conv_rate_one_sub_a : is_rate_of_convergence_towards θ (λ n, ∏ i in range (n + 1), (1 - a i)) 0) (one_div_delta_le_one_sub_a : ∀ k : ℕ, (1 : ℝ) / (δ₀ k) ≤ ∏ i in range (δ ψ χ k), (1 - a i)) : is_rate_of_convergence_towards (λ k, (max (θ $ 3 * M * (k + 1) * (δ₀ k) - 1) (δ ψ χ k) + 1)) s 0 := begin intros k n h, set σ := (λ (k : ℕ), max (θ (3 * M * (k + 1) * δ₀ k - 1)) (δ ψ χ k) + 1) with eq_σ, set m := n - δ ψ χ k - 1, have l_3_3 := leu_pin_3_3 a b c s M ψ χ a_nonneg (λ j, le_of_lt (a_lt_one j)) c_nonneg s_nonneg main_ineq M_pos s_le_M b_le_inv_k_succ χ_cauchy_modulus_sum_c k m (δ ψ χ k) (le_refl _), rw [real.dist_0_eq_abs, abs_of_nonneg (s_nonneg _)], have h₁ := finset.prod_range_add (1 - a) (δ ψ χ k) (m + 1), have h₂ : (∏ (x : ℕ) in range (δ ψ χ k + (m + 1)), (1 - a) x) / (∏ (x : ℕ) in range (δ ψ χ k), (1 - a) x) = (∏ (x : ℕ) in range (m + 1), (1 - a) (δ ψ χ k + x)) := by { refine (cancel_factors.cancel_factors_eq_div (eq.symm h₁) (ne.symm _)).symm, apply ne.symm, apply finset.prod_ne_zero_iff.mpr (λ i _, ne_of_gt $ _), work_on_goal 3 { simp only [pi.one_apply, pi.sub_apply], specialize a_lt_one i, exact sub_pos.mpr a_lt_one, }, apply_instance, apply_instance, }, have h₃ : n = δ ψ χ k + m + 1 := sorry, have h₄ : 1 / (∏ (x : ℕ) in range (δ ψ χ k), (1 - a) x) ≤ δ₀ k := sorry, have : ∏ i in range (δ ψ χ k + (m + 1)), (1 - a i) ≤ 1 / (3 * M * δ₀ k * (k + 1)) := sorry, have h₃ : ↑M * ∏ (i : ℕ) in range (m + 1), (1 - a (δ ψ χ k + i)) + 2 / (3 * ↑k + 2) ≤ 1 / (k + 1) := calc ↑M * ∏ (i : ℕ) in range (m + 1), (1 - a (δ ψ χ k + i)) + 2 / (3 * ↑k + 2) = ↑M * ((∏ (x : ℕ) in range (δ ψ χ k + (m + 1)), (1 - a) x) / ∏ (x : ℕ) in range (δ ψ χ k), (1 - a) x) + 2 / (3 * ↑k + 2) : by rw h₂; simp only [pi.one_apply, pi.sub_apply] ... = ↑M * ((∏ (x : ℕ) in range (δ ψ χ k + (m + 1)), (1 - a) x)) * (1 / ∏ (x : ℕ) in range (δ ψ χ k), (1 - a) x) + 2 / (3 * ↑k + 2) : by ring ... ≤ ↑M * ((∏ (x : ℕ) in range (δ ψ χ k + (m + 1)), (1 - a) x)) * δ₀ k + 2 / (3 * ↑k + 2) : by { have : 0 ≤ ↑M * ((∏ (x : ℕ) in range (δ ψ χ k + (m + 1)), (1 - a) x)) := sorry, nlinarith, } ... ≤ ↑M * (1 / (3 * M * δ₀ k * (k + 1))) * δ₀ k + 2 / (3 * ↑k + 2) : by { have : 0 ≤ (M : ℝ) := sorry, } ... ≤ 1 / (k + 1) : sorry end -- nu ne trebuie -- lemma leu_pin_3_6 -- (s_le_M : ∀ n : ℕ, s n ≤ M) -- (b_le_inv_k_succ : ∀ p n : ℕ, ψ p ≤ n → b n ≤ 1 / (p + 1)) -- (θ_conv_rate_one_sub_a: is_rate_of_convergence_towards θ (λ n, ∏ i in range (n + 1), (1 - a i)) 0) -- (one_div_delta_le_one_sub_a : ∀ k : ℕ, (1 : ℝ) / (δ k) ≤ ∏ i in range (δ k), (1 - a i)) : -- is_rate_of_convergence_towards (λ k, max (θ $ 2 * M * (δ k) * (k + 1) - 1) (δ k) + 1) s 0 := -- sorry end end leu_pin
e67c2ab337a59b5a6c94a0927a68358962071efc
7cef822f3b952965621309e88eadf618da0c8ae9
/src/data/zsqrtd/basic.lean
ef9e7f2fbe945c6d9190614f0506ed1a97b68cbc
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
26,831
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.int.basic algebra.associated data.nat.gcd /-- The ring of integers adjoined with a square root of `d`. These have the form `a + b √d` where `a b : ℤ`. The components are called `re` and `im` by analogy to the negative `d` case, but of course both parts are real here since `d` is nonnegative. -/ structure zsqrtd (d : ℤ) := mk {} :: (re : ℤ) (im : ℤ) prefix `ℤ√`:100 := zsqrtd namespace zsqrtd section parameters {d : ℤ} instance : decidable_eq ℤ√d := by tactic.mk_dec_eq_instance theorem ext : ∀ {z w : ℤ√d}, z = w ↔ z.re = w.re ∧ z.im = w.im | ⟨x, y⟩ ⟨x', y'⟩ := ⟨λ h, by injection h; split; assumption, λ ⟨h₁, h₂⟩, by congr; assumption⟩ /-- Convert an integer to a `ℤ√d` -/ def of_int (n : ℤ) : ℤ√d := ⟨n, 0⟩ @[simp] theorem of_int_re (n : ℤ) : (of_int n).re = n := rfl @[simp] theorem of_int_im (n : ℤ) : (of_int n).im = 0 := rfl /-- The zero of the ring -/ def zero : ℤ√d := of_int 0 instance : has_zero ℤ√d := ⟨zsqrtd.zero⟩ @[simp] theorem zero_re : (0 : ℤ√d).re = 0 := rfl @[simp] theorem zero_im : (0 : ℤ√d).im = 0 := rfl /-- The one of the ring -/ def one : ℤ√d := of_int 1 instance : has_one ℤ√d := ⟨zsqrtd.one⟩ @[simp] theorem one_re : (1 : ℤ√d).re = 1 := rfl @[simp] theorem one_im : (1 : ℤ√d).im = 0 := rfl /-- The representative of `√d` in the ring -/ def sqrtd : ℤ√d := ⟨0, 1⟩ @[simp] theorem sqrtd_re : (sqrtd : ℤ√d).re = 0 := rfl @[simp] theorem sqrtd_im : (sqrtd : ℤ√d).im = 1 := rfl /-- Addition of elements of `ℤ√d` -/ def add : ℤ√d → ℤ√d → ℤ√d | ⟨x, y⟩ ⟨x', y'⟩ := ⟨x + x', y + y'⟩ instance : has_add ℤ√d := ⟨zsqrtd.add⟩ @[simp] theorem add_def (x y x' y' : ℤ) : (⟨x, y⟩ + ⟨x', y'⟩ : ℤ√d) = ⟨x + x', y + y'⟩ := rfl @[simp] theorem add_re : ∀ z w : ℤ√d, (z + w).re = z.re + w.re | ⟨x, y⟩ ⟨x', y'⟩ := rfl @[simp] theorem add_im : ∀ z w : ℤ√d, (z + w).im = z.im + w.im | ⟨x, y⟩ ⟨x', y'⟩ := rfl @[simp] theorem bit0_re (z) : (bit0 z : ℤ√d).re = bit0 z.re := add_re _ _ @[simp] theorem bit0_im (z) : (bit0 z : ℤ√d).im = bit0 z.im := add_im _ _ @[simp] theorem bit1_re (z) : (bit1 z : ℤ√d).re = bit1 z.re := by simp [bit1] @[simp] theorem bit1_im (z) : (bit1 z : ℤ√d).im = bit0 z.im := by simp [bit1] /-- Negation in `ℤ√d` -/ def neg : ℤ√d → ℤ√d | ⟨x, y⟩ := ⟨-x, -y⟩ instance : has_neg ℤ√d := ⟨zsqrtd.neg⟩ @[simp] theorem neg_re : ∀ z : ℤ√d, (-z).re = -z.re | ⟨x, y⟩ := rfl @[simp] theorem neg_im : ∀ z : ℤ√d, (-z).im = -z.im | ⟨x, y⟩ := rfl /-- Conjugation in `ℤ√d`. The conjugate of `a + b √d` is `a - b √d`. -/ def conj : ℤ√d → ℤ√d | ⟨x, y⟩ := ⟨x, -y⟩ @[simp] theorem conj_re : ∀ z : ℤ√d, (conj z).re = z.re | ⟨x, y⟩ := rfl @[simp] theorem conj_im : ∀ z : ℤ√d, (conj z).im = -z.im | ⟨x, y⟩ := rfl /-- Multiplication in `ℤ√d` -/ def mul : ℤ√d → ℤ√d → ℤ√d | ⟨x, y⟩ ⟨x', y'⟩ := ⟨x * x' + d * y * y', x * y' + y * x'⟩ instance : has_mul ℤ√d := ⟨zsqrtd.mul⟩ @[simp] theorem mul_re : ∀ z w : ℤ√d, (z * w).re = z.re * w.re + d * z.im * w.im | ⟨x, y⟩ ⟨x', y'⟩ := rfl @[simp] theorem mul_im : ∀ z w : ℤ√d, (z * w).im = z.re * w.im + z.im * w.re | ⟨x, y⟩ ⟨x', y'⟩ := rfl instance : comm_ring ℤ√d := by refine { add := (+), zero := 0, neg := has_neg.neg, mul := (*), one := 1, ..}; { intros, simp [ext, add_mul, mul_add, mul_comm, mul_left_comm] } instance : add_comm_monoid ℤ√d := by apply_instance instance : add_monoid ℤ√d := by apply_instance instance : monoid ℤ√d := by apply_instance instance : comm_monoid ℤ√d := by apply_instance instance : comm_semigroup ℤ√d := by apply_instance instance : semigroup ℤ√d := by apply_instance instance : add_comm_semigroup ℤ√d := by apply_instance instance : add_semigroup ℤ√d := by apply_instance instance : comm_semiring ℤ√d := by apply_instance instance : semiring ℤ√d := by apply_instance instance : ring ℤ√d := by apply_instance instance : distrib ℤ√d := by apply_instance instance : zero_ne_one_class ℤ√d := { zero := 0, one := 1, zero_ne_one := dec_trivial } instance : nonzero_comm_ring ℤ√d := { ..zsqrtd.comm_ring, ..zsqrtd.zero_ne_one_class } @[simp] theorem coe_nat_re (n : ℕ) : (n : ℤ√d).re = n := by induction n; simp * @[simp] theorem coe_nat_im (n : ℕ) : (n : ℤ√d).im = 0 := by induction n; simp * theorem coe_nat_val (n : ℕ) : (n : ℤ√d) = ⟨n, 0⟩ := by simp [ext] @[simp] theorem coe_int_re (n : ℤ) : (n : ℤ√d).re = n := by cases n; simp [*, int.of_nat_eq_coe, int.neg_succ_of_nat_eq] @[simp] theorem coe_int_im (n : ℤ) : (n : ℤ√d).im = 0 := by cases n; simp * theorem coe_int_val (n : ℤ) : (n : ℤ√d) = ⟨n, 0⟩ := by simp [ext] instance : char_zero ℤ√d := { cast_inj := λ m n, ⟨by simp [zsqrtd.ext], congr_arg _⟩ } @[simp] theorem of_int_eq_coe (n : ℤ) : (of_int n : ℤ√d) = n := by simp [ext] @[simp] theorem smul_val (n x y : ℤ) : (n : ℤ√d) * ⟨x, y⟩ = ⟨n * x, n * y⟩ := by simp [ext] @[simp] theorem muld_val (x y : ℤ) : sqrtd * ⟨x, y⟩ = ⟨d * y, x⟩ := by simp [ext] @[simp] theorem smuld_val (n x y : ℤ) : sqrtd * (n : ℤ√d) * ⟨x, y⟩ = ⟨d * n * y, n * x⟩ := by simp [ext] theorem decompose {x y : ℤ} : (⟨x, y⟩ : ℤ√d) = x + sqrtd * y := by simp [ext] theorem mul_conj {x y : ℤ} : (⟨x, y⟩ * conj ⟨x, y⟩ : ℤ√d) = x * x - d * y * y := by simp [ext, mul_comm] theorem conj_mul : Π {a b : ℤ√d}, conj (a * b) = conj a * conj b := by simp [ext] protected lemma coe_int_add (m n : ℤ) : (↑(m + n) : ℤ√d) = ↑m + ↑n := by simp [ext] protected lemma coe_int_sub (m n : ℤ) : (↑(m - n) : ℤ√d) = ↑m - ↑n := by simp [ext] protected lemma coe_int_mul (m n : ℤ) : (↑(m * n) : ℤ√d) = ↑m * ↑n := by simp [ext] protected lemma coe_int_inj {m n : ℤ} (h : (↑m : ℤ√d) = ↑n) : m = n := by simpa using congr_arg re h /-- Read `sq_le a c b d` as `a √c ≤ b √d` -/ def sq_le (a c b d : ℕ) : Prop := c*a*a ≤ d*b*b theorem sq_le_of_le {c d x y z w : ℕ} (xz : z ≤ x) (yw : y ≤ w) (xy : sq_le x c y d) : sq_le z c w d := le_trans (mul_le_mul (nat.mul_le_mul_left _ xz) xz (nat.zero_le _) (nat.zero_le _)) $ le_trans xy (mul_le_mul (nat.mul_le_mul_left _ yw) yw (nat.zero_le _) (nat.zero_le _)) theorem sq_le_add_mixed {c d x y z w : ℕ} (xy : sq_le x c y d) (zw : sq_le z c w d) : c * (x * z) ≤ d * (y * w) := nat.mul_self_le_mul_self_iff.2 $ by simpa [mul_comm, mul_left_comm] using mul_le_mul xy zw (nat.zero_le _) (nat.zero_le _) theorem sq_le_add {c d x y z w : ℕ} (xy : sq_le x c y d) (zw : sq_le z c w d) : sq_le (x + z) c (y + w) d := begin have xz := sq_le_add_mixed xy zw, simp [sq_le, mul_assoc] at xy zw, simp [sq_le, mul_add, mul_comm, mul_left_comm, add_le_add, *] end theorem sq_le_cancel {c d x y z w : ℕ} (zw : sq_le y d x c) (h : sq_le (x + z) c (y + w) d) : sq_le z c w d := begin apply le_of_not_gt, intro l, refine not_le_of_gt _ h, simp [sq_le, mul_add, mul_comm, mul_left_comm], have hm := sq_le_add_mixed zw (le_of_lt l), simp [sq_le, mul_assoc] at l zw, exact lt_of_le_of_lt (add_le_add_right zw _) (add_lt_add_left (add_lt_add_of_le_of_lt hm (add_lt_add_of_le_of_lt hm l)) _) end theorem sq_le_smul {c d x y : ℕ} (n : ℕ) (xy : sq_le x c y d) : sq_le (n * x) c (n * y) d := by simpa [sq_le, mul_left_comm, mul_assoc] using nat.mul_le_mul_left (n * n) xy theorem sq_le_mul {d x y z w : ℕ} : (sq_le x 1 y d → sq_le z 1 w d → sq_le (x * w + y * z) d (x * z + d * y * w) 1) ∧ (sq_le x 1 y d → sq_le w d z 1 → sq_le (x * z + d * y * w) 1 (x * w + y * z) d) ∧ (sq_le y d x 1 → sq_le z 1 w d → sq_le (x * z + d * y * w) 1 (x * w + y * z) d) ∧ (sq_le y d x 1 → sq_le w d z 1 → sq_le (x * w + y * z) d (x * z + d * y * w) 1) := by refine ⟨_, _, _, _⟩; { intros xy zw, have := int.mul_nonneg (sub_nonneg_of_le (int.coe_nat_le_coe_nat_of_le xy)) (sub_nonneg_of_le (int.coe_nat_le_coe_nat_of_le zw)), refine int.le_of_coe_nat_le_coe_nat (le_of_sub_nonneg _), simpa [mul_add, mul_left_comm, mul_comm] } /-- "Generalized" `nonneg`. `nonnegg c d x y` means `a √c + b √d ≥ 0`; we are interested in the case `c = 1` but this is more symmetric -/ def nonnegg (c d : ℕ) : ℤ → ℤ → Prop | (a : ℕ) (b : ℕ) := true | (a : ℕ) -[1+ b] := sq_le (b+1) c a d | -[1+ a] (b : ℕ) := sq_le (a+1) d b c | -[1+ a] -[1+ b] := false theorem nonnegg_comm {c d : ℕ} {x y : ℤ} : nonnegg c d x y = nonnegg d c y x := by induction x; induction y; refl theorem nonnegg_neg_pos {c d} : Π {a b : ℕ}, nonnegg c d (-a) b ↔ sq_le a d b c | 0 b := ⟨by simp [sq_le, nat.zero_le], λa, trivial⟩ | (a+1) b := by rw ← int.neg_succ_of_nat_coe; refl theorem nonnegg_pos_neg {c d} {a b : ℕ} : nonnegg c d a (-b) ↔ sq_le b c a d := by rw nonnegg_comm; exact nonnegg_neg_pos theorem nonnegg_cases_right {c d} {a : ℕ} : Π {b : ℤ}, (Π x : ℕ, b = -x → sq_le x c a d) → nonnegg c d a b | (b:nat) h := trivial | -[1+ b] h := h (b+1) rfl theorem nonnegg_cases_left {c d} {b : ℕ} {a : ℤ} (h : Π x : ℕ, a = -x → sq_le x d b c) : nonnegg c d a b := cast nonnegg_comm (nonnegg_cases_right h) section norm def norm (n : ℤ√d) : ℤ := n.re * n.re - d * n.im * n.im @[simp] lemma norm_zero : norm 0 = 0 := by simp [norm] @[simp] lemma norm_one : norm 1 = 1 := by simp [norm] @[simp] lemma norm_int_cast (n : ℤ) : norm n = n * n := by simp [norm] @[simp] lemma norm_nat_cast (n : ℕ) : norm n = n * n := norm_int_cast n @[simp] lemma norm_mul (n m : ℤ√d) : norm (n * m) = norm n * norm m := by simp [norm, mul_add, add_mul, mul_comm, mul_assoc, mul_left_comm] lemma norm_eq_mul_conj (n : ℤ√d) : (norm n : ℤ√d) = n * n.conj := by cases n; simp [norm, conj, zsqrtd.ext, mul_comm] instance : is_monoid_hom norm := { map_one := norm_one, map_mul := norm_mul } lemma norm_nonneg (hd : d ≤ 0) (n : ℤ√d) : 0 ≤ n.norm := add_nonneg (mul_self_nonneg _) (by rw [mul_assoc, neg_mul_eq_neg_mul]; exact (mul_nonneg (neg_nonneg.2 hd) (mul_self_nonneg _))) lemma norm_eq_one_iff {x : ℤ√d} : x.norm.nat_abs = 1 ↔ is_unit x := ⟨λ h, is_unit_iff_dvd_one.2 $ (le_total 0 (norm x)).cases_on (λ hx, show x ∣ 1, from ⟨x.conj, by rwa [← int.coe_nat_inj', int.nat_abs_of_nonneg hx, ← @int.cast_inj (ℤ√d) _ _, norm_eq_mul_conj, eq_comm] at h⟩) (λ hx, show x ∣ 1, from ⟨- x.conj, by rwa [← int.coe_nat_inj', int.of_nat_nat_abs_of_nonpos hx, ← @int.cast_inj (ℤ√d) _ _, int.cast_neg, norm_eq_mul_conj, neg_mul_eq_mul_neg, eq_comm] at h⟩), λ h, let ⟨y, hy⟩ := is_unit_iff_dvd_one.1 h in begin have := congr_arg (int.nat_abs ∘ norm) hy, rw [function.comp_app, function.comp_app, norm_mul, int.nat_abs_mul, norm_one, int.nat_abs_one, eq_comm, nat.mul_eq_one_iff] at this, exact this.1 end⟩ end norm end section parameter {d : ℕ} /-- Nonnegativity of an element of `ℤ√d`. -/ def nonneg : ℤ√d → Prop | ⟨a, b⟩ := nonnegg d 1 a b protected def le (a b : ℤ√d) : Prop := nonneg (b - a) instance : has_le ℤ√d := ⟨zsqrtd.le⟩ protected def lt (a b : ℤ√d) : Prop := ¬(b ≤ a) instance : has_lt ℤ√d := ⟨zsqrtd.lt⟩ instance decidable_nonnegg (c d a b) : decidable (nonnegg c d a b) := by cases a; cases b; repeat {rw int.of_nat_eq_coe}; unfold nonnegg sq_le; apply_instance instance decidable_nonneg : Π (a : ℤ√d), decidable (nonneg a) | ⟨a, b⟩ := zsqrtd.decidable_nonnegg _ _ _ _ instance decidable_le (a b : ℤ√d) : decidable (a ≤ b) := decidable_nonneg _ theorem nonneg_cases : Π {a : ℤ√d}, nonneg a → ∃ x y : ℕ, a = ⟨x, y⟩ ∨ a = ⟨x, -y⟩ ∨ a = ⟨-x, y⟩ | ⟨(x : ℕ), (y : ℕ)⟩ h := ⟨x, y, or.inl rfl⟩ | ⟨(x : ℕ), -[1+ y]⟩ h := ⟨x, y+1, or.inr $ or.inl rfl⟩ | ⟨-[1+ x], (y : ℕ)⟩ h := ⟨x+1, y, or.inr $ or.inr rfl⟩ | ⟨-[1+ x], -[1+ y]⟩ h := false.elim h lemma nonneg_add_lem {x y z w : ℕ} (xy : nonneg ⟨x, -y⟩) (zw : nonneg ⟨-z, w⟩) : nonneg (⟨x, -y⟩ + ⟨-z, w⟩) := have nonneg ⟨int.sub_nat_nat x z, int.sub_nat_nat w y⟩, from int.sub_nat_nat_elim x z (λm n i, sq_le y d m 1 → sq_le n 1 w d → nonneg ⟨i, int.sub_nat_nat w y⟩) (λj k, int.sub_nat_nat_elim w y (λm n i, sq_le n d (k + j) 1 → sq_le k 1 m d → nonneg ⟨int.of_nat j, i⟩) (λm n xy zw, trivial) (λm n xy zw, sq_le_cancel zw xy)) (λj k, int.sub_nat_nat_elim w y (λm n i, sq_le n d k 1 → sq_le (k + j + 1) 1 m d → nonneg ⟨-[1+ j], i⟩) (λm n xy zw, sq_le_cancel xy zw) (λm n xy zw, let t := nat.le_trans zw (sq_le_of_le (nat.le_add_right n (m+1)) (le_refl _) xy) in have k + j + 1 ≤ k, from nat.mul_self_le_mul_self_iff.2 (by repeat{rw one_mul at t}; exact t), absurd this (not_le_of_gt $ nat.succ_le_succ $ nat.le_add_right _ _))) (nonnegg_pos_neg.1 xy) (nonnegg_neg_pos.1 zw), show nonneg ⟨_, _⟩, by rw [neg_add_eq_sub]; rwa [int.sub_nat_nat_eq_coe,int.sub_nat_nat_eq_coe] at this theorem nonneg_add {a b : ℤ√d} (ha : nonneg a) (hb : nonneg b) : nonneg (a + b) := begin rcases nonneg_cases ha with ⟨x, y, rfl|rfl|rfl⟩; rcases nonneg_cases hb with ⟨z, w, rfl|rfl|rfl⟩; dsimp [add, nonneg] at ha hb ⊢, { trivial }, { refine nonnegg_cases_right (λi h, sq_le_of_le _ _ (nonnegg_pos_neg.1 hb)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ y (by simp *))) }, { apply nat.le_add_left } }, { refine nonnegg_cases_left (λi h, sq_le_of_le _ _ (nonnegg_neg_pos.1 hb)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ x (by simp *))) }, { apply nat.le_add_left } }, { refine nonnegg_cases_right (λi h, sq_le_of_le _ _ (nonnegg_pos_neg.1 ha)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ w (by simp *))) }, { apply nat.le_add_right } }, { simpa using nonnegg_pos_neg.2 (sq_le_add (nonnegg_pos_neg.1 ha) (nonnegg_pos_neg.1 hb)) }, { exact nonneg_add_lem ha hb }, { refine nonnegg_cases_left (λi h, sq_le_of_le _ _ (nonnegg_neg_pos.1 ha)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ z (by simp *))) }, { apply nat.le_add_right } }, { rw [add_comm, add_comm ↑y], exact nonneg_add_lem hb ha }, { simpa using nonnegg_neg_pos.2 (sq_le_add (nonnegg_neg_pos.1 ha) (nonnegg_neg_pos.1 hb)) }, end theorem le_refl (a : ℤ√d) : a ≤ a := show nonneg (a - a), by simp protected theorem le_trans {a b c : ℤ√d} (ab : a ≤ b) (bc : b ≤ c) : a ≤ c := have nonneg (b - a + (c - b)), from nonneg_add ab bc, by simpa theorem nonneg_iff_zero_le {a : ℤ√d} : nonneg a ↔ 0 ≤ a := show _ ↔ nonneg _, by simp theorem le_of_le_le {x y z w : ℤ} (xz : x ≤ z) (yw : y ≤ w) : (⟨x, y⟩ : ℤ√d) ≤ ⟨z, w⟩ := show nonneg ⟨z - x, w - y⟩, from match z - x, w - y, int.le.dest_sub xz, int.le.dest_sub yw with ._, ._, ⟨a, rfl⟩, ⟨b, rfl⟩ := trivial end theorem le_arch (a : ℤ√d) : ∃n : ℕ, a ≤ n := let ⟨x, y, (h : a ≤ ⟨x, y⟩)⟩ := show ∃x y : ℕ, nonneg (⟨x, y⟩ + -a), from match -a with | ⟨int.of_nat x, int.of_nat y⟩ := ⟨0, 0, trivial⟩ | ⟨int.of_nat x, -[1+ y]⟩ := ⟨0, y+1, by simp [int.neg_succ_of_nat_coe]⟩ | ⟨-[1+ x], int.of_nat y⟩ := ⟨x+1, 0, by simp [int.neg_succ_of_nat_coe]⟩ | ⟨-[1+ x], -[1+ y]⟩ := ⟨x+1, y+1, by simp [int.neg_succ_of_nat_coe]⟩ end in begin refine ⟨x + d*y, zsqrtd.le_trans h _⟩, rw [← int.cast_coe_nat, ← of_int_eq_coe], change nonneg ⟨(↑x + d*y) - ↑x, 0-↑y⟩, cases y with y, { simp }, have h : ∀y, sq_le y d (d * y) 1 := λ y, by simpa [sq_le, mul_comm, mul_left_comm] using nat.mul_le_mul_right (y * y) (nat.le_mul_self d), rw [show (x:ℤ) + d * nat.succ y - x = d * nat.succ y, by simp], exact h (y+1) end protected theorem nonneg_total : Π (a : ℤ√d), nonneg a ∨ nonneg (-a) | ⟨(x : ℕ), (y : ℕ)⟩ := or.inl trivial | ⟨-[1+ x], -[1+ y]⟩ := or.inr trivial | ⟨0, -[1+ y]⟩ := or.inr trivial | ⟨-[1+ x], 0⟩ := or.inr trivial | ⟨(x+1:ℕ), -[1+ y]⟩ := nat.le_total | ⟨-[1+ x], (y+1:ℕ)⟩ := nat.le_total protected theorem le_total (a b : ℤ√d) : a ≤ b ∨ b ≤ a := let t := nonneg_total (b - a) in by rw [show -(b-a) = a-b, from neg_sub b a] at t; exact t instance : preorder ℤ√d := { le := zsqrtd.le, le_refl := zsqrtd.le_refl, le_trans := @zsqrtd.le_trans, lt := zsqrtd.lt, lt_iff_le_not_le := λ a b, (and_iff_right_of_imp (zsqrtd.le_total _ _).resolve_left).symm } protected theorem add_le_add_left (a b : ℤ√d) (ab : a ≤ b) (c : ℤ√d) : c + a ≤ c + b := show nonneg _, by rw add_sub_add_left_eq_sub; exact ab protected theorem le_of_add_le_add_left (a b c : ℤ√d) (h : c + a ≤ c + b) : a ≤ b := by simpa using zsqrtd.add_le_add_left _ _ h (-c) protected theorem add_lt_add_left (a b : ℤ√d) (h : a < b) (c) : c + a < c + b := λ h', h (zsqrtd.le_of_add_le_add_left _ _ _ h') theorem nonneg_smul {a : ℤ√d} {n : ℕ} (ha : nonneg a) : nonneg (n * a) := by rw ← int.cast_coe_nat; exact match a, nonneg_cases ha, ha with | ._, ⟨x, y, or.inl rfl⟩, ha := by rw smul_val; trivial | ._, ⟨x, y, or.inr $ or.inl rfl⟩, ha := by rw smul_val; simpa using nonnegg_pos_neg.2 (sq_le_smul n $ nonnegg_pos_neg.1 ha) | ._, ⟨x, y, or.inr $ or.inr rfl⟩, ha := by rw smul_val; simpa using nonnegg_neg_pos.2 (sq_le_smul n $ nonnegg_neg_pos.1 ha) end theorem nonneg_muld {a : ℤ√d} (ha : nonneg a) : nonneg (sqrtd * a) := by refine match a, nonneg_cases ha, ha with | ._, ⟨x, y, or.inl rfl⟩, ha := trivial | ._, ⟨x, y, or.inr $ or.inl rfl⟩, ha := by simp; apply nonnegg_neg_pos.2; simpa [sq_le, mul_comm, mul_left_comm] using nat.mul_le_mul_left d (nonnegg_pos_neg.1 ha) | ._, ⟨x, y, or.inr $ or.inr rfl⟩, ha := by simp; apply nonnegg_pos_neg.2; simpa [sq_le, mul_comm, mul_left_comm] using nat.mul_le_mul_left d (nonnegg_neg_pos.1 ha) end theorem nonneg_mul_lem {x y : ℕ} {a : ℤ√d} (ha : nonneg a) : nonneg (⟨x, y⟩ * a) := have (⟨x, y⟩ * a : ℤ√d) = x * a + sqrtd * (y * a), by rw [decompose, right_distrib, mul_assoc]; refl, by rw this; exact nonneg_add (nonneg_smul ha) (nonneg_muld $ nonneg_smul ha) theorem nonneg_mul {a b : ℤ√d} (ha : nonneg a) (hb : nonneg b) : nonneg (a * b) := match a, b, nonneg_cases ha, nonneg_cases hb, ha, hb with | ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb := trivial | ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb := nonneg_mul_lem hb | ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb := nonneg_mul_lem hb | ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb := by rw mul_comm; exact nonneg_mul_lem ha | ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb := by rw mul_comm; exact nonneg_mul_lem ha | ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb := by rw [calc (⟨-x, y⟩ * ⟨-z, w⟩ : ℤ√d) = ⟨_, _⟩ : rfl ... = ⟨x * z + d * y * w, -(x * w + y * z)⟩ : by simp]; exact nonnegg_pos_neg.2 (sq_le_mul.left (nonnegg_neg_pos.1 ha) (nonnegg_neg_pos.1 hb)) | ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb := by rw [calc (⟨-x, y⟩ * ⟨z, -w⟩ : ℤ√d) = ⟨_, _⟩ : rfl ... = ⟨-(x * z + d * y * w), x * w + y * z⟩ : by simp]; exact nonnegg_neg_pos.2 (sq_le_mul.right.left (nonnegg_neg_pos.1 ha) (nonnegg_pos_neg.1 hb)) | ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb := by rw [calc (⟨x, -y⟩ * ⟨-z, w⟩ : ℤ√d) = ⟨_, _⟩ : rfl ... = ⟨-(x * z + d * y * w), x * w + y * z⟩ : by simp]; exact nonnegg_neg_pos.2 (sq_le_mul.right.right.left (nonnegg_pos_neg.1 ha) (nonnegg_neg_pos.1 hb)) | ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb := by rw [calc (⟨x, -y⟩ * ⟨z, -w⟩ : ℤ√d) = ⟨_, _⟩ : rfl ... = ⟨x * z + d * y * w, -(x * w + y * z)⟩ : by simp]; exact nonnegg_pos_neg.2 (sq_le_mul.right.right.right (nonnegg_pos_neg.1 ha) (nonnegg_pos_neg.1 hb)) end protected theorem mul_nonneg (a b : ℤ√d) : 0 ≤ a → 0 ≤ b → 0 ≤ a * b := by repeat {rw ← nonneg_iff_zero_le}; exact nonneg_mul theorem not_sq_le_succ (c d y) (h : 0 < c) : ¬sq_le (y + 1) c 0 d := not_le_of_gt $ mul_pos (mul_pos h $ nat.succ_pos _) $ nat.succ_pos _ /-- A nonsquare is a natural number that is not equal to the square of an integer. This is implemented as a typeclass because it's a necessary condition for much of the Pell equation theory. -/ class nonsquare (x : ℕ) : Prop := (ns : ∀n : ℕ, x ≠ n*n) parameter [dnsq : nonsquare d] include dnsq theorem d_pos : 0 < d := lt_of_le_of_ne (nat.zero_le _) $ ne.symm $ (nonsquare.ns d 0) theorem divides_sq_eq_zero {x y} (h : x * x = d * y * y) : x = 0 ∧ y = 0 := let g := x.gcd y in or.elim g.eq_zero_or_pos (λH, ⟨nat.eq_zero_of_gcd_eq_zero_left H, nat.eq_zero_of_gcd_eq_zero_right H⟩) (λgpos, false.elim $ let ⟨m, n, co, (hx : x = m * g), (hy : y = n * g)⟩ := nat.exists_coprime gpos in begin rw [hx, hy] at h, have : m * m = d * (n * n) := nat.eq_of_mul_eq_mul_left (mul_pos gpos gpos) (by simpa [mul_comm, mul_left_comm] using h), have co2 := let co1 := co.mul_right co in co1.mul co1, exact nonsquare.ns d m (nat.dvd_antisymm (by rw this; apply dvd_mul_right) $ co2.dvd_of_dvd_mul_right $ by simp [this]) end) theorem divides_sq_eq_zero_z {x y : ℤ} (h : x * x = d * y * y) : x = 0 ∧ y = 0 := by rw [mul_assoc, ← int.nat_abs_mul_self, ← int.nat_abs_mul_self, ← int.coe_nat_mul, ← mul_assoc] at h; exact let ⟨h1, h2⟩ := divides_sq_eq_zero (int.coe_nat_inj h) in ⟨int.eq_zero_of_nat_abs_eq_zero h1, int.eq_zero_of_nat_abs_eq_zero h2⟩ theorem not_divides_square (x y) : (x + 1) * (x + 1) ≠ d * (y + 1) * (y + 1) := λe, by have t := (divides_sq_eq_zero e).left; contradiction theorem nonneg_antisymm : Π {a : ℤ√d}, nonneg a → nonneg (-a) → a = 0 | ⟨0, 0⟩ xy yx := rfl | ⟨-[1+ x], -[1+ y]⟩ xy yx := false.elim xy | ⟨(x+1:nat), (y+1:nat)⟩ xy yx := false.elim yx | ⟨-[1+ x], 0⟩ xy yx := absurd xy (not_sq_le_succ _ _ _ dec_trivial) | ⟨(x+1:nat), 0⟩ xy yx := absurd yx (not_sq_le_succ _ _ _ dec_trivial) | ⟨0, -[1+ y]⟩ xy yx := absurd xy (not_sq_le_succ _ _ _ d_pos) | ⟨0, (y+1:nat)⟩ _ yx := absurd yx (not_sq_le_succ _ _ _ d_pos) | ⟨(x+1:nat), -[1+ y]⟩ (xy : sq_le _ _ _ _) (yx : sq_le _ _ _ _) := let t := le_antisymm yx xy in by rw[one_mul] at t; exact absurd t (not_divides_square _ _) | ⟨-[1+ x], (y+1:nat)⟩ (xy : sq_le _ _ _ _) (yx : sq_le _ _ _ _) := let t := le_antisymm xy yx in by rw[one_mul] at t; exact absurd t (not_divides_square _ _) theorem le_antisymm {a b : ℤ√d} (ab : a ≤ b) (ba : b ≤ a) : a = b := eq_of_sub_eq_zero $ nonneg_antisymm ba (by rw neg_sub; exact ab) instance : decidable_linear_order ℤ√d := { le_antisymm := @zsqrtd.le_antisymm, le_total := zsqrtd.le_total, decidable_le := zsqrtd.decidable_le, ..zsqrtd.preorder } protected theorem eq_zero_or_eq_zero_of_mul_eq_zero : Π {a b : ℤ√d}, a * b = 0 → a = 0 ∨ b = 0 | ⟨x, y⟩ ⟨z, w⟩ h := by injection h with h1 h2; exact have h1 : x*z = -(d*y*w), from eq_neg_of_add_eq_zero h1, have h2 : x*w = -(y*z), from eq_neg_of_add_eq_zero h2, have fin : x*x = d*y*y → (⟨x, y⟩:ℤ√d) = 0, from λe, match x, y, divides_sq_eq_zero_z e with ._, ._, ⟨rfl, rfl⟩ := rfl end, if z0 : z = 0 then if w0 : w = 0 then or.inr (match z, w, z0, w0 with ._, ._, rfl, rfl := rfl end) else or.inl $ fin $ eq_of_mul_eq_mul_right w0 $ calc x * x * w = -y * (x * z) : by simp [h2, mul_assoc, mul_left_comm] ... = d * y * y * w : by simp [h1, mul_assoc, mul_left_comm] else or.inl $ fin $ eq_of_mul_eq_mul_right z0 $ calc x * x * z = d * -y * (x * w) : by simp [h1, mul_assoc, mul_left_comm] ... = d * y * y * z : by simp [h2, mul_assoc, mul_left_comm] instance : integral_domain ℤ√d := { zero_ne_one := zero_ne_one, eq_zero_or_eq_zero_of_mul_eq_zero := @zsqrtd.eq_zero_or_eq_zero_of_mul_eq_zero, ..zsqrtd.comm_ring } protected theorem mul_pos (a b : ℤ√d) (a0 : 0 < a) (b0 : 0 < b) : 0 < a * b := λab, or.elim (eq_zero_or_eq_zero_of_mul_eq_zero (le_antisymm ab (mul_nonneg _ _ (le_of_lt a0) (le_of_lt b0)))) (λe, ne_of_gt a0 e) (λe, ne_of_gt b0 e) instance : decidable_linear_ordered_comm_ring ℤ√d := { add_le_add_left := @zsqrtd.add_le_add_left, add_lt_add_left := @zsqrtd.add_lt_add_left, zero_ne_one := zero_ne_one, mul_nonneg := @zsqrtd.mul_nonneg, mul_pos := @zsqrtd.mul_pos, zero_lt_one := dec_trivial, ..zsqrtd.comm_ring, ..zsqrtd.decidable_linear_order } instance : decidable_linear_ordered_semiring ℤ√d := by apply_instance instance : linear_ordered_semiring ℤ√d := by apply_instance instance : ordered_semiring ℤ√d := by apply_instance end end zsqrtd
b4b452b2b7c209f0c4a03a8cc0c2818312c06164
94637389e03c919023691dcd05bd4411b1034aa5
/src/zzz_junk/has_mul/has_mul.lean
28fd9726a5b315467773572b6d0a43d731530d51
[]
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
1,837
lean
namespace hidden universe u /- Version 1 -/ structure has_mul'' (α : Type u): Type (u+1) := (mul : α → α → α) -- def has_mul''_bool_type := has_mul'' bool def has_mul''_bool : has_mul'' bool := has_mul''.mk band -- in bool lib def has_mul''_nat : has_mul'' nat := has_mul''.mk nat.mul -- in nat lib /- Later on -/ axioms (b1 b2 : bool) #check has_mul''_bool.mul def n1 := 3 def n2 := 4 #eval has_mul''_nat.mul n1 n2 /- Version 2 -/ structure has_mul' {α : Type u}: Type (u+1) := (mul : α → α → α) -- def has_mul''_bool_type := has_mul'' bool def has_mul'_bool : has_mul' := has_mul'.mk band -- in bool lib def has_mul'_nat : has_mul' := has_mul'.mk nat.mul -- in nat lib /- Later on -/ #check has_mul'_bool.mul #eval has_mul'_nat.mul n1 n2 def my_fancy_mul' : nat → nat → nat | n1 n2 := has_mul'_nat.mul n1 n2 #eval my_fancy_mul' 3 4 /- Final version. Typeclasses in Lean. -/ @[class] structure has_mul (α : Type u): Type u := (mul : α → α → α) instance has_mul_bool : hidden.has_mul bool := has_mul.mk band -- in bool lib instance has_mul_nat : hidden.has_mul nat := has_mul.mk nat.mul -- in nat lib /- When I create typeclass instances, they get stored in a database and the typeclass inference mechanism can find these instances by type. -/ /- Implicit instance arguments -/ --def my_fancy_mul (α : Type) [tc : has_mul α] : α → α → α := tc.mul def my_fancy_mul {α : Type} [tc : has_mul α] : α → α → α := tc.mul -- #eval (my_fancy_mul nat) 3 4 -- #eval (my_fancy_mul bool) tt ff #eval my_fancy_mul 3 4 #eval my_fancy_mul tt ff #eval my_fancy_mul "Hello" "Lean" -- there's no instance for the string type! /- Ad hoc polymorphism. Operator overloading. -/ /- Syntactic alternative: class has_mul (α : Type u) := (mul : α → α → α) -/ end hidden
9012c8b51ed98f6ac4b5b8eb068ccf27ad23ce46
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/algebra/affine.lean
dddef55e2611d8d769f2c807174903c747e18512
[ "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,330
lean
/- Copyright (c) 2020 Frédéric Dupuis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Frédéric Dupuis -/ import linear_algebra.affine_space.affine_map import topology.algebra.group import topology.algebra.mul_action /-! # Topological properties of affine spaces and maps For now, this contains only a few facts regarding the continuity of affine maps in the special case when the point space and vector space are the same. TODO: Deal with the case where the point spaces are different from the vector spaces. Note that we do have some results in this direction under the assumption that the topologies are induced by (semi)norms. -/ namespace affine_map variables {R E F : Type*} variables [add_comm_group E] [topological_space E] variables [add_comm_group F] [topological_space F] [topological_add_group F] section ring variables [ring R] [module R E] [module R F] /-- An affine map is continuous iff its underlying linear map is continuous. See also `affine_map.continuous_linear_iff`. -/ lemma continuous_iff {f : E →ᵃ[R] F} : continuous f ↔ continuous f.linear := begin split, { intro hc, rw decomp' f, have := hc.sub continuous_const, exact this, }, { intro hc, rw decomp f, have := hc.add continuous_const, exact this } end /-- The line map is continuous. -/ @[continuity] lemma line_map_continuous [topological_space R] [has_continuous_smul R F] {p v : F} : continuous ⇑(line_map p v : R →ᵃ[R] F) := continuous_iff.mpr $ (continuous_id.smul continuous_const).add $ @continuous_const _ _ _ _ (0 : F) end ring section comm_ring variables [comm_ring R] [module R F] [topological_space R] [has_continuous_smul R F] @[continuity] lemma homothety_continuous (x : F) (t : R) : continuous $ homothety x t := begin suffices : ⇑(homothety x t) = λ y, t • (y - x) + x, { rw this, continuity, }, ext y, simp [homothety_apply], end end comm_ring section field variables [field R] [module R F] [topological_space R] [has_continuous_smul R F] lemma homothety_is_open_map (x : F) (t : R) (ht : t ≠ 0) : is_open_map $ homothety x t := begin apply is_open_map.of_inverse (homothety_continuous x t⁻¹); intros e; simp [← affine_map.comp_apply, ← homothety_mul, ht], end end field end affine_map
0b19708e2941bec78ee64792ea67815ba6b5f8d1
137c667471a40116a7afd7261f030b30180468c2
/src/group_theory/perm/cycles.lean
00f2352e2241eca648c63faaebe8db6db09faf39
[ "Apache-2.0" ]
permissive
bragadeesh153/mathlib
46bf814cfb1eecb34b5d1549b9117dc60f657792
b577bb2cd1f96eb47031878256856020b76f73cd
refs/heads/master
1,687,435,188,334
1,626,384,207,000
1,626,384,207,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
40,513
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.nat.parity import data.equiv.fintype import group_theory.perm.sign import data.finset.noncomm_prod /-! # Cyclic permutations ## Main definitions In the following, `f : equiv.perm β`. * `equiv.perm.is_cycle`: `f.is_cycle` when two nonfixed points of `β` are related by repeated application of `f`. * `equiv.perm.same_cycle`: `f.same_cycle x y` when `x` and `y` are in the same cycle of `f`. The following two definitions require that `β` is a `fintype`: * `equiv.perm.cycle_of`: `f.cycle_of x` is the cycle of `f` that `x` belongs to. * `equiv.perm.cycle_factors`: `f.cycle_factors` is a list of disjoint cyclic permutations that multiply to `f`. ## Main results * This file contains several closure results: - `closure_is_cycle` : The symmetric group is generated by cycles - `closure_cycle_adjacent_swap` : The symmetric group is generated by a cycle and an adjacent transposition - `closure_cycle_coprime_swap` : The symmetric group is generated by a cycle and a coprime transposition - `closure_prime_cycle_swap` : The symmetric group is generated by a prime cycle and a transposition -/ namespace equiv.perm open equiv function finset variables {α : Type*} {β : Type*} [decidable_eq α] section sign_cycle /-! ### `is_cycle` -/ variables [fintype α] /-- A permutation is a cycle when any two nonfixed points of the permutation are related by repeated application of the permutation. -/ def is_cycle (f : perm β) : Prop := ∃ x, f x ≠ x ∧ ∀ y, f y ≠ y → ∃ i : ℤ, (f ^ i) x = y lemma is_cycle.ne_one {f : perm β} (h : is_cycle f) : f ≠ 1 := λ hf, by simpa [hf, is_cycle] using h lemma is_cycle.two_le_card_support {f : perm α} (h : is_cycle f) : 2 ≤ f.support.card := two_le_card_support_of_ne_one h.ne_one lemma is_cycle_swap {α : Type*} [decidable_eq α] {x y : α} (hxy : x ≠ y) : is_cycle (swap x y) := ⟨y, by rwa swap_apply_right, λ a (ha : ite (a = x) y (ite (a = y) x a) ≠ a), if hya : y = a then ⟨0, hya⟩ else ⟨1, by { rw [gpow_one, swap_apply_def], split_ifs at *; cc }⟩⟩ lemma is_swap.is_cycle {α : Type*} [decidable_eq α] {f : perm α} (hf : is_swap f) : is_cycle f := begin obtain ⟨x, y, hxy, rfl⟩ := hf, exact is_cycle_swap hxy, end lemma is_cycle.inv {f : perm β} (hf : is_cycle f) : is_cycle (f⁻¹) := let ⟨x, hx⟩ := hf in ⟨x, by { simp only [inv_eq_iff_eq, *, forall_prop_of_true, ne.def] at *, cc }, λ y hy, let ⟨i, hi⟩ := hx.2 y (by { simp only [inv_eq_iff_eq, *, forall_prop_of_true, ne.def] at *, cc }) in ⟨-i, by rwa [gpow_neg, inv_gpow, inv_inv]⟩⟩ lemma is_cycle.is_cycle_conj {f g : perm β} (hf : is_cycle f) : is_cycle (g * f * g⁻¹) := begin obtain ⟨a, ha1, ha2⟩ := hf, refine ⟨g a, by simp [ha1], λ b hb, _⟩, obtain ⟨i, hi⟩ := ha2 (g⁻¹ b) _, { refine ⟨i, _⟩, rw conj_gpow, simp [hi] }, { contrapose! hb, rw [perm.mul_apply, perm.mul_apply, hb, apply_inv_self] } end lemma is_cycle.exists_gpow_eq {f : perm β} (hf : is_cycle f) {x y : β} (hx : f x ≠ x) (hy : f y ≠ y) : ∃ i : ℤ, (f ^ i) x = y := let ⟨g, hg⟩ := hf in let ⟨a, ha⟩ := hg.2 x hx in let ⟨b, hb⟩ := hg.2 y hy in ⟨b - a, by rw [← ha, ← mul_apply, ← gpow_add, sub_add_cancel, hb]⟩ lemma is_cycle.exists_pow_eq [fintype β] {f : perm β} (hf : is_cycle f) {x y : β} (hx : f x ≠ x) (hy : f y ≠ y) : ∃ i : ℕ, (f ^ i) x = y := let ⟨n, hn⟩ := hf.exists_gpow_eq hx hy in by classical; exact ⟨(n % order_of f).to_nat, by { have := n.mod_nonneg (int.coe_nat_ne_zero.mpr (ne_of_gt (order_of_pos f))), rwa [← gpow_coe_nat, int.to_nat_of_nonneg this, ← gpow_eq_mod_order_of] }⟩ /-- The subgroup generated by a cycle is in bijection with its support -/ noncomputable def is_cycle.gpowers_equiv_support {σ : perm α} (hσ : is_cycle σ) : (↑(subgroup.gpowers σ) : set (perm α)) ≃ (↑(σ.support) : set α) := equiv.of_bijective (λ τ, ⟨τ (classical.some hσ), begin obtain ⟨τ, n, rfl⟩ := τ, rw [finset.mem_coe, coe_fn_coe_base, subtype.coe_mk, gpow_apply_mem_support, mem_support], exact (classical.some_spec hσ).1, end⟩) begin split, { rintros ⟨a, m, rfl⟩ ⟨b, n, rfl⟩ h, ext y, by_cases hy : σ y = y, { simp_rw [subtype.coe_mk, gpow_apply_eq_self_of_apply_eq_self hy] }, { obtain ⟨i, rfl⟩ := (classical.some_spec hσ).2 y hy, rw [subtype.coe_mk, subtype.coe_mk, gpow_apply_comm σ m i, gpow_apply_comm σ n i], exact congr_arg _ (subtype.ext_iff.mp h) } }, by { rintros ⟨y, hy⟩, rw [finset.mem_coe, mem_support] at hy, obtain ⟨n, rfl⟩ := (classical.some_spec hσ).2 y hy, exact ⟨⟨σ ^ n, n, rfl⟩, rfl⟩ }, end @[simp] lemma is_cycle.gpowers_equiv_support_apply {σ : perm α} (hσ : is_cycle σ) {n : ℕ} : hσ.gpowers_equiv_support ⟨σ ^ n, n, rfl⟩ = ⟨(σ ^ n) (classical.some hσ), pow_apply_mem_support.2 (mem_support.2 (classical.some_spec hσ).1)⟩ := rfl @[simp] lemma is_cycle.gpowers_equiv_support_symm_apply {σ : perm α} (hσ : is_cycle σ) (n : ℕ) : hσ.gpowers_equiv_support.symm ⟨(σ ^ n) (classical.some hσ), pow_apply_mem_support.2 (mem_support.2 (classical.some_spec hσ).1)⟩ = ⟨σ ^ n, n, rfl⟩ := (equiv.symm_apply_eq _).2 hσ.gpowers_equiv_support_apply lemma order_of_is_cycle {σ : perm α} (hσ : is_cycle σ) : order_of σ = σ.support.card := begin rw [order_eq_card_gpowers, ←fintype.card_coe], convert fintype.card_congr (is_cycle.gpowers_equiv_support hσ), end lemma is_cycle_swap_mul_aux₁ {α : Type*} [decidable_eq α] : ∀ (n : ℕ) {b x : α} {f : perm α} (hb : (swap x (f x) * f) b ≠ b) (h : (f ^ n) (f x) = b), ∃ i : ℤ, ((swap x (f x) * f) ^ i) (f x) = b | 0 := λ b x f hb h, ⟨0, h⟩ | (n+1 : ℕ) := λ b x f hb h, if hfbx : f x = b then ⟨0, hfbx⟩ else have f b ≠ b ∧ b ≠ x, from ne_and_ne_of_swap_mul_apply_ne_self hb, have hb' : (swap x (f x) * f) (f⁻¹ b) ≠ f⁻¹ b, by { rw [mul_apply, apply_inv_self, swap_apply_of_ne_of_ne this.2 (ne.symm hfbx), ne.def, ← f.injective.eq_iff, apply_inv_self], exact this.1 }, let ⟨i, hi⟩ := is_cycle_swap_mul_aux₁ n hb' (f.injective $ by { rw [apply_inv_self], rwa [pow_succ, mul_apply] at h }) in ⟨i + 1, by rw [add_comm, gpow_add, mul_apply, hi, gpow_one, mul_apply, apply_inv_self, swap_apply_of_ne_of_ne (ne_and_ne_of_swap_mul_apply_ne_self hb).2 (ne.symm hfbx)]⟩ lemma is_cycle_swap_mul_aux₂ {α : Type*} [decidable_eq α] : ∀ (n : ℤ) {b x : α} {f : perm α} (hb : (swap x (f x) * f) b ≠ b) (h : (f ^ n) (f x) = b), ∃ i : ℤ, ((swap x (f x) * f) ^ i) (f x) = b | (n : ℕ) := λ b x f, is_cycle_swap_mul_aux₁ n | -[1+ n] := λ b x f hb h, if hfbx : f⁻¹ x = b then ⟨-1, by rwa [gpow_neg, gpow_one, mul_inv_rev, mul_apply, swap_inv, swap_apply_right]⟩ else if hfbx' : f x = b then ⟨0, hfbx'⟩ else have f b ≠ b ∧ b ≠ x := ne_and_ne_of_swap_mul_apply_ne_self hb, have hb : (swap x (f⁻¹ x) * f⁻¹) (f⁻¹ b) ≠ f⁻¹ b, by { rw [mul_apply, swap_apply_def], split_ifs; simp only [inv_eq_iff_eq, perm.mul_apply, gpow_neg_succ_of_nat, ne.def, perm.apply_inv_self] at *; cc }, let ⟨i, hi⟩ := is_cycle_swap_mul_aux₁ n hb (show (f⁻¹ ^ n) (f⁻¹ x) = f⁻¹ b, by rw [← gpow_coe_nat, ← h, ← mul_apply, ← mul_apply, ← mul_apply, gpow_neg_succ_of_nat, ← inv_pow, pow_succ', mul_assoc, mul_assoc, inv_mul_self, mul_one, gpow_coe_nat, ← pow_succ', ← pow_succ]) in have h : (swap x (f⁻¹ x) * f⁻¹) (f x) = f⁻¹ x, by rw [mul_apply, inv_apply_self, swap_apply_left], ⟨-i, by rw [← add_sub_cancel i 1, neg_sub, sub_eq_add_neg, gpow_add, gpow_one, gpow_neg, ← inv_gpow, mul_inv_rev, swap_inv, mul_swap_eq_swap_mul, inv_apply_self, swap_comm _ x, gpow_add, gpow_one, mul_apply, mul_apply (_ ^ i), h, hi, mul_apply, apply_inv_self, swap_apply_of_ne_of_ne this.2 (ne.symm hfbx')]⟩ lemma is_cycle.eq_swap_of_apply_apply_eq_self {α : Type*} [decidable_eq α] {f : perm α} (hf : is_cycle f) {x : α} (hfx : f x ≠ x) (hffx : f (f x) = x) : f = swap x (f x) := equiv.ext $ λ y, let ⟨z, hz⟩ := hf in let ⟨i, hi⟩ := hz.2 x hfx in if hyx : y = x then by simp [hyx] else if hfyx : y = f x then by simp [hfyx, hffx] else begin rw [swap_apply_of_ne_of_ne hyx hfyx], refine by_contradiction (λ hy, _), cases hz.2 y hy with j hj, rw [← sub_add_cancel j i, gpow_add, mul_apply, hi] at hj, cases gpow_apply_eq_of_apply_apply_eq_self hffx (j - i) with hji hji, { rw [← hj, hji] at hyx, cc }, { rw [← hj, hji] at hfyx, cc } end lemma is_cycle.swap_mul {α : Type*} [decidable_eq α] {f : perm α} (hf : is_cycle f) {x : α} (hx : f x ≠ x) (hffx : f (f x) ≠ x) : is_cycle (swap x (f x) * f) := ⟨f x, by { simp only [swap_apply_def, mul_apply], split_ifs; simp [f.injective.eq_iff] at *; cc }, λ y hy, let ⟨i, hi⟩ := hf.exists_gpow_eq hx (ne_and_ne_of_swap_mul_apply_ne_self hy).1 in have hi : (f ^ (i - 1)) (f x) = y, from calc (f ^ (i - 1)) (f x) = (f ^ (i - 1) * f ^ (1 : ℤ)) x : by rw [gpow_one, mul_apply] ... = y : by rwa [← gpow_add, sub_add_cancel], is_cycle_swap_mul_aux₂ (i - 1) hy hi⟩ lemma is_cycle.sign : ∀ {f : perm α} (hf : is_cycle f), sign f = -(-1) ^ f.support.card | f := λ hf, let ⟨x, hx⟩ := hf in calc sign f = sign (swap x (f x) * (swap x (f x) * f)) : by rw [← mul_assoc, mul_def, mul_def, swap_swap, trans_refl] ... = -(-1) ^ f.support.card : if h1 : f (f x) = x then have h : swap x (f x) * f = 1, begin rw hf.eq_swap_of_apply_apply_eq_self hx.1 h1, simp only [perm.mul_def, perm.one_def, swap_apply_left, swap_swap] end, by { rw [sign_mul, sign_swap hx.1.symm, h, sign_one, hf.eq_swap_of_apply_apply_eq_self hx.1 h1, card_support_swap hx.1.symm], refl } else have h : card (support (swap x (f x) * f)) + 1 = card (support f), by rw [← insert_erase (mem_support.2 hx.1), support_swap_mul_eq _ _ h1, card_insert_of_not_mem (not_mem_erase _ _), sdiff_singleton_eq_erase], have wf : card (support (swap x (f x) * f)) < card (support f), from card_support_swap_mul hx.1, by { rw [sign_mul, sign_swap hx.1.symm, (hf.swap_mul hx.1 h1).sign, ← h], simp only [pow_add, mul_one, units.neg_neg, one_mul, units.mul_neg, eq_self_iff_true, pow_one, units.neg_mul_neg] } using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ f, f.support.card)⟩]} -- The lemma `support_gpow_le` is relevant. It means that `h2` is equivalent to -- `σ.support = (σ ^ n).support`, as well as to `σ.support.card ≤ (σ ^ n).support.card`. lemma is_cycle_of_is_cycle_gpow {σ : perm α} {n : ℤ} (h1 : is_cycle (σ ^ n)) (h2 : σ.support ≤ (σ ^ n).support) : is_cycle σ := begin have key : ∀ x : α, (σ ^ n) x ≠ x ↔ σ x ≠ x, { simp_rw [←mem_support], exact finset.ext_iff.mp (le_antisymm (support_gpow_le σ n) h2) }, obtain ⟨x, hx1, hx2⟩ := h1, refine ⟨x, (key x).mp hx1, λ y hy, _⟩, cases (hx2 y ((key y).mpr hy)) with i _, exact ⟨n * i, by rwa gpow_mul⟩, end lemma is_cycle.extend_domain {α : Type*} {p : β → Prop} [decidable_pred p] (f : α ≃ subtype p) {g : perm α} (h : is_cycle g) : is_cycle (g.extend_domain f) := begin obtain ⟨a, ha, ha'⟩ := h, refine ⟨f a, _, λ b hb, _⟩, { rw extend_domain_apply_image, exact λ con, ha (f.injective (subtype.coe_injective con)) }, by_cases pb : p b, { obtain ⟨i, hi⟩ := ha' (f.symm ⟨b, pb⟩) (λ con, hb _), { refine ⟨i, _⟩, have hnat : ∀ (k : ℕ) (a : α), (g.extend_domain f ^ k) ↑(f a) = f ((g ^ k) a), { intros k a, induction k with k ih, { refl }, rw [pow_succ, perm.mul_apply, ih, extend_domain_apply_image, pow_succ, perm.mul_apply] }, have hint : ∀ (k : ℤ) (a : α), (g.extend_domain f ^ k) ↑(f a) = f ((g ^ k) a), { intros k a, induction k with k k, { rw [gpow_of_nat, gpow_of_nat, hnat] }, rw [gpow_neg_succ_of_nat, gpow_neg_succ_of_nat, inv_eq_iff_eq, hnat, apply_inv_self] }, rw [hint, hi, apply_symm_apply, subtype.coe_mk] }, { rw [extend_domain_apply_subtype _ _ pb, con, apply_symm_apply, subtype.coe_mk] } }, { exact (hb (extend_domain_apply_not_subtype _ _ pb)).elim } end end sign_cycle /-! ### `same_cycle` -/ /-- The equivalence relation indicating that two points are in the same cycle of a permutation. -/ def same_cycle (f : perm β) (x y : β) : Prop := ∃ i : ℤ, (f ^ i) x = y @[refl] lemma same_cycle.refl (f : perm β) (x : β) : same_cycle f x x := ⟨0, rfl⟩ @[symm] lemma same_cycle.symm (f : perm β) {x y : β} : same_cycle f x y → same_cycle f y x := λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← hi, inv_apply_self]⟩ @[trans] lemma same_cycle.trans (f : perm β) {x y z : β} : same_cycle f x y → same_cycle f y z → same_cycle f x z := λ ⟨i, hi⟩ ⟨j, hj⟩, ⟨j + i, by rw [gpow_add, mul_apply, hi, hj]⟩ lemma same_cycle.apply_eq_self_iff {f : perm β} {x y : β} : same_cycle f x y → (f x = x ↔ f y = y) := λ ⟨i, hi⟩, by rw [← hi, ← mul_apply, ← gpow_one_add, add_comm, gpow_add_one, mul_apply, (f ^ i).injective.eq_iff] lemma is_cycle.same_cycle {f : perm β} (hf : is_cycle f) {x y : β} (hx : f x ≠ x) (hy : f y ≠ y) : same_cycle f x y := hf.exists_gpow_eq hx hy instance [fintype α] (f : perm α) : decidable_rel (same_cycle f) := λ x y, decidable_of_iff (∃ n ∈ list.range (fintype.card (perm α)), (f ^ n) x = y) ⟨λ ⟨n, _, hn⟩, ⟨n, hn⟩, λ ⟨i, hi⟩, ⟨(i % order_of f).nat_abs, list.mem_range.2 (int.coe_nat_lt.1 $ by { rw int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), { apply lt_of_lt_of_le (int.mod_lt _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), { simp [order_of_le_card_univ] }, exact fintype_perm }, exact fintype_perm, }), by { rw [← gpow_coe_nat, int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), ← gpow_eq_mod_order_of, hi], exact fintype_perm }⟩⟩ lemma same_cycle_apply {f : perm β} {x y : β} : same_cycle f x (f y) ↔ same_cycle f x y := ⟨λ ⟨i, hi⟩, ⟨-1 + i, by rw [gpow_add, mul_apply, hi, gpow_neg_one, inv_apply_self]⟩, λ ⟨i, hi⟩, ⟨1 + i, by rw [gpow_add, mul_apply, hi, gpow_one]⟩⟩ lemma same_cycle_cycle {f : perm β} {x : β} (hx : f x ≠ x) : is_cycle f ↔ (∀ {y}, same_cycle f x y ↔ f y ≠ y) := ⟨λ hf y, ⟨λ ⟨i, hi⟩ hy, hx $ by { rw [← gpow_apply_eq_self_of_apply_eq_self hy i, (f ^ i).injective.eq_iff] at hi, rw [hi, hy] }, hf.exists_gpow_eq hx⟩, λ h, ⟨x, hx, λ y hy, h.2 hy⟩⟩ lemma same_cycle_inv (f : perm β) {x y : β} : same_cycle f⁻¹ x y ↔ same_cycle f x y := ⟨λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← inv_gpow, hi]⟩, λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← inv_gpow, inv_inv, hi]⟩ ⟩ lemma same_cycle_inv_apply {f : perm β} {x y : β} : same_cycle f x (f⁻¹ y) ↔ same_cycle f x y := by rw [← same_cycle_inv, same_cycle_apply, same_cycle_inv] /-- Unlike `support_congr`, which assumes that `∀ (x ∈ g.support), f x = g x)`, here we have the weaker assumption that `∀ (x ∈ f.support), f x = g x`. -/ lemma is_cycle.support_congr [fintype α] {f g : perm α} (hf : is_cycle f) (hg : is_cycle g) (h : f.support ⊆ g.support) (h' : ∀ (x ∈ f.support), f x = g x) : f = g := begin have : f.support = g.support, { refine le_antisymm h _, intros z hz, obtain ⟨x, hx, hf'⟩ := id hf, have hx' : g x ≠ x, { rwa [←h' x (mem_support.mpr hx)] }, obtain ⟨m, hm⟩ := hg.exists_pow_eq hx' (mem_support.mp hz), have h'' : ∀ (x ∈ f.support ∩ g.support), f x = g x, { intros x hx, exact h' x (mem_of_mem_inter_left hx) }, rwa [←hm, ←pow_eq_on_of_mem_support h'' _ x (mem_inter_of_mem (mem_support.mpr hx) (mem_support.mpr hx')), pow_apply_mem_support, mem_support] }, refine support_congr h _, simpa [←this] using h' end /-- If two cyclic permutations agree on all terms in their intersection, and that intersection is not empty, then the two cyclic permutations must be equal. -/ lemma is_cycle.eq_on_support_inter_nonempty_congr [fintype α] {f g : perm α} (hf : is_cycle f) (hg : is_cycle g) (h : ∀ (x ∈ f.support ∩ g.support), f x = g x) {x : α} (hx : f x = g x) (hx' : x ∈ f.support) : f = g := begin have hx'' : x ∈ g.support, { rwa [mem_support, ←hx, ←mem_support] }, have : f.support ⊆ g.support, { intros y hy, obtain ⟨k, rfl⟩ := hf.exists_pow_eq (mem_support.mp hx') (mem_support.mp hy), rwa [pow_eq_on_of_mem_support h _ _ (mem_inter_of_mem hx' hx''), pow_apply_mem_support] }, rw (inter_eq_left_iff_subset _ _).mpr this at h, exact hf.support_congr hg this h end /-! ### `cycle_of` -/ /-- `f.cycle_of x` is the cycle of the permutation `f` to which `x` belongs. -/ def cycle_of [fintype α] (f : perm α) (x : α) : perm α := of_subtype (@subtype_perm _ f (same_cycle f x) (λ _, same_cycle_apply.symm)) lemma cycle_of_apply [fintype α] (f : perm α) (x y : α) : cycle_of f x y = if same_cycle f x y then f y else y := rfl lemma cycle_of_inv [fintype α] (f : perm α) (x : α) : (cycle_of f x)⁻¹ = cycle_of f⁻¹ x := equiv.ext $ λ y, begin rw [inv_eq_iff_eq, cycle_of_apply, cycle_of_apply], split_ifs; simp [*, same_cycle_inv, same_cycle_inv_apply] at * end @[simp] lemma cycle_of_pow_apply_self [fintype α] (f : perm α) (x : α) : ∀ n : ℕ, (cycle_of f x ^ n) x = (f ^ n) x | 0 := rfl | (n+1) := by { rw [pow_succ, mul_apply, cycle_of_apply, cycle_of_pow_apply_self, if_pos, pow_succ, mul_apply], exact ⟨n, rfl⟩ } @[simp] lemma cycle_of_gpow_apply_self [fintype α] (f : perm α) (x : α) : ∀ n : ℤ, (cycle_of f x ^ n) x = (f ^ n) x | (n : ℕ) := cycle_of_pow_apply_self f x n | -[1+ n] := by rw [gpow_neg_succ_of_nat, ← inv_pow, cycle_of_inv, gpow_neg_succ_of_nat, ← inv_pow, cycle_of_pow_apply_self] lemma same_cycle.cycle_of_apply [fintype α] {f : perm α} {x y : α} (h : same_cycle f x y) : cycle_of f x y = f y := dif_pos h lemma cycle_of_apply_of_not_same_cycle [fintype α] {f : perm α} {x y : α} (h : ¬same_cycle f x y) : cycle_of f x y = y := dif_neg h @[simp] lemma cycle_of_apply_self [fintype α] (f : perm α) (x : α) : cycle_of f x x = f x := (same_cycle.refl _ _).cycle_of_apply lemma is_cycle.cycle_of_eq [fintype α] {f : perm α} (hf : is_cycle f) {x : α} (hx : f x ≠ x) : cycle_of f x = f := equiv.ext $ λ y, if h : same_cycle f x y then by rw [h.cycle_of_apply] else by rw [cycle_of_apply_of_not_same_cycle h, not_not.1 (mt ((same_cycle_cycle hx).1 hf).2 h)] @[simp] lemma cycle_of_eq_one_iff [fintype α] (f : perm α) {x : α} : cycle_of f x = 1 ↔ f x = x := begin simp_rw [ext_iff, cycle_of_apply, one_apply], refine ⟨λ h, (if_pos (same_cycle.refl f x)).symm.trans (h x), λ h y, _⟩, by_cases hy : f y = y, { rw [hy, if_t_t] }, { exact if_neg (mt same_cycle.apply_eq_self_iff (by tauto)) }, end lemma is_cycle.cycle_of [fintype α] {f : perm α} (hf : is_cycle f) {x : α} : cycle_of f x = if f x = x then 1 else f := begin by_cases hx : f x = x, { rwa [if_pos hx, cycle_of_eq_one_iff] }, { rwa [if_neg hx, hf.cycle_of_eq] }, end lemma cycle_of_one [fintype α] (x : α) : cycle_of 1 x = 1 := (cycle_of_eq_one_iff 1).mpr rfl lemma is_cycle_cycle_of [fintype α] (f : perm α) {x : α} (hx : f x ≠ x) : is_cycle (cycle_of f x) := have cycle_of f x x ≠ x, by rwa [(same_cycle.refl _ _).cycle_of_apply], (same_cycle_cycle this).2 $ λ y, ⟨λ h, mt h.apply_eq_self_iff.2 this, λ h, if hxy : same_cycle f x y then let ⟨i, hi⟩ := hxy in ⟨i, by rw [cycle_of_gpow_apply_self, hi]⟩ else by { rw [cycle_of_apply_of_not_same_cycle hxy] at h, exact (h rfl).elim }⟩ /-! ### `cycle_factors` -/ /-- Given a list `l : list α` and a permutation `f : perm α` whose nonfixed points are all in `l`, recursively factors `f` into cycles. -/ def cycle_factors_aux [fintype α] : Π (l : list α) (f : perm α), (∀ {x}, f x ≠ x → x ∈ l) → {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} | [] f h := ⟨[], by { simp only [imp_false, list.pairwise.nil, list.not_mem_nil, forall_const, and_true, forall_prop_of_false, not_not, not_false_iff, list.prod_nil] at *, ext, simp * }⟩ | (x::l) f h := if hx : f x = x then cycle_factors_aux l f (λ y hy, list.mem_of_ne_of_mem (λ h, hy (by rwa h)) (h hy)) else let ⟨m, hm₁, hm₂, hm₃⟩ := cycle_factors_aux l ((cycle_of f x)⁻¹ * f) (λ y hy, list.mem_of_ne_of_mem (λ h : y = x, by { rw [h, mul_apply, ne.def, inv_eq_iff_eq, cycle_of_apply_self] at hy, exact hy rfl }) (h (λ h : f y = y, by { rw [mul_apply, h, ne.def, inv_eq_iff_eq, cycle_of_apply] at hy, split_ifs at hy; cc }))) in ⟨(cycle_of f x) :: m, by { rw [list.prod_cons, hm₁], simp }, λ g hg, ((list.mem_cons_iff _ _ _).1 hg).elim (λ hg, hg.symm ▸ is_cycle_cycle_of _ hx) (hm₂ g), list.pairwise_cons.2 ⟨λ g hg y, or_iff_not_imp_left.2 (λ hfy, have hxy : same_cycle f x y := not_not.1 (mt cycle_of_apply_of_not_same_cycle hfy), have hgm : g :: m.erase g ~ m := list.cons_perm_iff_perm_erase.2 ⟨hg, list.perm.refl _⟩, have ∀ h ∈ m.erase g, disjoint g h, from (list.pairwise_cons.1 ((hgm.pairwise_iff (λ a b (h : disjoint a b), h.symm)).2 hm₃)).1, classical.by_cases id $ λ hgy : g y ≠ y, (disjoint_prod_right _ this y).resolve_right $ have hsc : same_cycle f⁻¹ x (f y), by rwa [same_cycle_inv, same_cycle_apply], by { rw [disjoint_prod_perm hm₃ hgm.symm, list.prod_cons, ← eq_inv_mul_iff_mul_eq] at hm₁, rwa [hm₁, mul_apply, mul_apply, cycle_of_inv, hsc.cycle_of_apply, inv_apply_self, inv_eq_iff_eq, eq_comm] }), hm₃⟩⟩ lemma mem_list_cycles_iff {α : Type*} [fintype α] {l : list (perm α)} (h1 : ∀ σ : perm α, σ ∈ l → σ.is_cycle) (h2 : l.pairwise disjoint) {σ : perm α} : σ ∈ l ↔ σ.is_cycle ∧ ∀ (a : α) (h4 : σ a ≠ a), σ a = l.prod a := begin suffices : σ.is_cycle → (σ ∈ l ↔ ∀ (a : α) (h4 : σ a ≠ a), σ a = l.prod a), { exact ⟨λ hσ, ⟨h1 σ hσ, (this (h1 σ hσ)).mp hσ⟩, λ hσ, (this hσ.1).mpr hσ.2⟩ }, intro h3, classical, split, { intros h a ha, exact eq_on_support_mem_disjoint h h2 _ (mem_support.mpr ha) }, { intros h, have hσl : σ.support ⊆ l.prod.support, { intros x hx, rw mem_support at hx, rwa [mem_support, ←h _ hx] }, obtain ⟨a, ha, -⟩ := id h3, rw ←mem_support at ha, obtain ⟨τ, hτ, hτa⟩ := exists_mem_support_of_mem_support_prod (hσl ha), have hτl : ∀ (x ∈ τ.support), τ x = l.prod x := eq_on_support_mem_disjoint hτ h2, have key : ∀ (x ∈ σ.support ∩ τ.support), σ x = τ x, { intros x hx, rw [h x (mem_support.mp (mem_of_mem_inter_left hx)), hτl x (mem_of_mem_inter_right hx)] }, convert hτ, refine h3.eq_on_support_inter_nonempty_congr (h1 _ hτ) key _ ha, exact key a (mem_inter_of_mem ha hτa) } end lemma list_cycles_perm_list_cycles {α : Type*} [fintype α] {l₁ l₂ : list (perm α)} (h₀ : l₁.prod = l₂.prod) (h₁l₁ : ∀ σ : perm α, σ ∈ l₁ → σ.is_cycle) (h₁l₂ : ∀ σ : perm α, σ ∈ l₂ → σ.is_cycle) (h₂l₁ : l₁.pairwise disjoint) (h₂l₂ : l₂.pairwise disjoint) : l₁ ~ l₂ := begin classical, have h₃l₁ : (1 : perm α) ∉ l₁ := λ h, (h₁l₁ 1 h).ne_one rfl, have h₃l₂ : (1 : perm α) ∉ l₂ := λ h, (h₁l₂ 1 h).ne_one rfl, refine (list.perm_ext (nodup_of_pairwise_disjoint h₃l₁ h₂l₁) (nodup_of_pairwise_disjoint h₃l₂ h₂l₂)).mpr (λ σ, _), by_cases hσ : σ.is_cycle, { obtain ⟨a, ha⟩ := not_forall.mp (mt ext hσ.ne_one), rw [mem_list_cycles_iff h₁l₁ h₂l₁, mem_list_cycles_iff h₁l₂ h₂l₂, h₀] }, { exact iff_of_false (mt (h₁l₁ σ) hσ) (mt (h₁l₂ σ) hσ) } end /-- Factors a permutation `f` into a list of disjoint cyclic permutations that multiply to `f`. -/ def cycle_factors [fintype α] [linear_order α] (f : perm α) : {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} := cycle_factors_aux (univ.sort (≤)) f (λ _ _, (mem_sort _).2 (mem_univ _)) /-- Factors a permutation `f` into a list of disjoint cyclic permutations that multiply to `f`, without a linear order. -/ def trunc_cycle_factors [fintype α] (f : perm α) : trunc {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} := quotient.rec_on_subsingleton (@univ α _).1 (λ l h, trunc.mk (cycle_factors_aux l f h)) (show ∀ x, f x ≠ x → x ∈ (@univ α _).1, from λ _ _, mem_univ _) section cycle_factors_finset variables [fintype α] (f : perm α) /-- Factors a permutation `f` into a `finset` of disjoint cyclic permutations that multiply to `f`. -/ def cycle_factors_finset : finset (perm α) := (trunc_cycle_factors f).lift (λ (l : {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint}), l.val.to_finset) (λ ⟨l, hl⟩ ⟨l', hl'⟩, list.to_finset_eq_of_perm _ _ (list_cycles_perm_list_cycles (hl'.left.symm ▸ hl.left) hl.right.left (hl'.right.left) hl.right.right hl'.right.right)) lemma cycle_factors_finset_eq_list_to_finset {σ : perm α} {l : list (perm α)} (hn : l.nodup) : σ.cycle_factors_finset = l.to_finset ↔ (∀ f : perm α, f ∈ l → f.is_cycle) ∧ l.pairwise disjoint ∧ l.prod = σ := begin obtain ⟨⟨l', hp', hc', hd'⟩, hl⟩ := trunc.exists_rep σ.trunc_cycle_factors, have ht : cycle_factors_finset σ = l'.to_finset, { rw [cycle_factors_finset, ←hl, trunc.lift_mk] }, rw ht, split, { intro h, have hn' : l'.nodup, { refine nodup_of_pairwise_disjoint _ hd', intro H, exact is_cycle.ne_one (hc' _ H) rfl }, have hperm : l ~ l' := list.perm_of_nodup_nodup_to_finset_eq hn hn' h.symm, refine ⟨_, _, _⟩, { exact λ _ h, hc' _ (hperm.subset h)}, { rwa list.perm.pairwise_iff disjoint.symmetric hperm }, { rw [←hp', hperm.symm.prod_eq'], refine hd'.imp _, exact λ _ _, disjoint.commute } }, { rintro ⟨hc, hd, hp⟩, refine list.to_finset_eq_of_perm _ _ _, refine list_cycles_perm_list_cycles _ hc' hc hd' hd, rw [hp, hp'] } end lemma cycle_factors_finset_eq_finset {σ : perm α} {s : finset (perm α)} : σ.cycle_factors_finset = s ↔ (∀ f : perm α, f ∈ s → f.is_cycle) ∧ (∃ h : (∀ (a ∈ s) (b ∈ s), a ≠ b → disjoint a b), s.noncomm_prod id (λ a ha b hb, (em (a = b)).by_cases (λ h, h ▸ commute.refl a) (set.pairwise_on.mono' (λ _ _, disjoint.commute) h a ha b hb)) = σ) := begin obtain ⟨l, hl, rfl⟩ := s.exists_list_nodup_eq, rw cycle_factors_finset_eq_list_to_finset hl, simp only [noncomm_prod_to_finset, hl, exists_prop, list.mem_to_finset, and.congr_left_iff, and.congr_right_iff, list.map_id, ne.def], intros, exact ⟨list.forall_of_pairwise disjoint.symmetric, hl.pairwise_of_forall_ne⟩ end lemma cycle_factors_finset_pairwise_disjoint (p : perm α) (hp : p ∈ cycle_factors_finset f) (q : perm α) (hq : q ∈ cycle_factors_finset f) (h : p ≠ q) : disjoint p q := begin have : f.cycle_factors_finset = f.cycle_factors_finset := rfl, obtain ⟨-, hd, -⟩ := cycle_factors_finset_eq_finset.mp this, exact hd p hp q hq h end -- TODO: use #7578 lemma cycle_factors_finset_mem_commute (p : perm α) (hp : p ∈ cycle_factors_finset f) (q : perm α) (hq : q ∈ cycle_factors_finset f) : _root_.commute p q := begin by_cases h : p = q, { exact h ▸ commute.refl _ }, { exact (cycle_factors_finset_pairwise_disjoint _ _ hp _ hq h).commute } end /-- The product of cycle factors is equal to the original `f : perm α`. -/ lemma cycle_factors_finset_noncomm_prod : f.cycle_factors_finset.noncomm_prod id (cycle_factors_finset_mem_commute f) = f := begin have : f.cycle_factors_finset = f.cycle_factors_finset := rfl, obtain ⟨-, hd, hp⟩ := cycle_factors_finset_eq_finset.mp this, exact hp end lemma mem_cycle_factors_finset_iff {f p : perm α} : p ∈ cycle_factors_finset f ↔ p.is_cycle ∧ ∀ (a ∈ p.support), p a = f a := begin obtain ⟨l, hl, hl'⟩ := f.cycle_factors_finset.exists_list_nodup_eq, rw ←hl', rw [eq_comm, cycle_factors_finset_eq_list_to_finset hl] at hl', simpa [list.mem_to_finset, ne.def, ←hl'.right.right] using mem_list_cycles_iff hl'.left hl'.right.left end lemma cycle_factors_finset_eq_empty_iff : cycle_factors_finset f = ∅ ↔ f = 1 := by simpa [cycle_factors_finset_eq_finset] using eq_comm @[simp] lemma cycle_factors_finset_eq_singleton_self_iff : f.cycle_factors_finset = {f} ↔ f.is_cycle := by simp [cycle_factors_finset_eq_finset] lemma cycle_factors_finset_eq_singleton_iff {g : perm α} : f.cycle_factors_finset = {g} ↔ f.is_cycle ∧ f = g := begin suffices : f = g → (g.is_cycle ↔ f.is_cycle), { simpa [cycle_factors_finset_eq_finset, eq_comm] }, rintro rfl, exact iff.rfl end /-- Two permutations `f g : perm α` have the same cycle factors iff they are the same. -/ lemma cycle_factors_finset_injective : function.injective (@cycle_factors_finset α _ _) := begin intros f g h, rw ←cycle_factors_finset_noncomm_prod f, simpa [h] using cycle_factors_finset_noncomm_prod g end end cycle_factors_finset @[elab_as_eliminator] lemma cycle_induction_on [fintype β] (P : perm β → Prop) (σ : perm β) (base_one : P 1) (base_cycles : ∀ σ : perm β, σ.is_cycle → P σ) (induction_disjoint : ∀ σ τ : perm β, disjoint σ τ → is_cycle σ → P σ → P τ → P (σ * τ)) : P σ := begin suffices : ∀ l : list (perm β), (∀ τ : perm β, τ ∈ l → τ.is_cycle) → l.pairwise disjoint → P l.prod, { classical, let x := σ.trunc_cycle_factors.out, exact (congr_arg P x.2.1).mp (this x.1 x.2.2.1 x.2.2.2) }, intro l, induction l with σ l ih, { exact λ _ _, base_one }, { intros h1 h2, rw list.prod_cons, exact induction_disjoint σ l.prod (disjoint_prod_right _ (list.pairwise_cons.mp h2).1) (h1 _ (list.mem_cons_self _ _)) (base_cycles σ (h1 σ (l.mem_cons_self σ))) (ih (λ τ hτ, h1 τ (list.mem_cons_of_mem σ hτ)) (list.pairwise_of_pairwise_cons h2)) }, end section generation variables [fintype α] [fintype β] open subgroup lemma closure_is_cycle : closure {σ : perm β | is_cycle σ} = ⊤ := begin classical, exact top_le_iff.mp (le_trans (ge_of_eq closure_is_swap) (closure_mono (λ _, is_swap.is_cycle))), end lemma closure_cycle_adjacent_swap {σ : perm α} (h1 : is_cycle σ) (h2 : σ.support = ⊤) (x : α) : closure ({σ, swap x (σ x)} : set (perm α)) = ⊤ := begin let H := closure ({σ, swap x (σ x)} : set (perm α)), have h3 : σ ∈ H := subset_closure (set.mem_insert σ _), have h4 : swap x (σ x) ∈ H := subset_closure (set.mem_insert_of_mem _ (set.mem_singleton _)), have step1 : ∀ (n : ℕ), swap ((σ ^ n) x) ((σ^(n+1)) x) ∈ H, { intro n, induction n with n ih, { exact subset_closure (set.mem_insert_of_mem _ (set.mem_singleton _)) }, { convert H.mul_mem (H.mul_mem h3 ih) (H.inv_mem h3), rw [mul_swap_eq_swap_mul, mul_inv_cancel_right], refl } }, have step2 : ∀ (n : ℕ), swap x ((σ ^ n) x) ∈ H, { intro n, induction n with n ih, { convert H.one_mem, exact swap_self x }, { by_cases h5 : x = (σ ^ n) x, { rw [pow_succ, mul_apply, ←h5], exact h4 }, by_cases h6 : x = (σ^(n+1)) x, { rw [←h6, swap_self], exact H.one_mem }, rw [swap_comm, ←swap_mul_swap_mul_swap h5 h6], exact H.mul_mem (H.mul_mem (step1 n) ih) (step1 n) } }, have step3 : ∀ (y : α), swap x y ∈ H, { intro y, have hx : x ∈ (⊤ : finset α) := finset.mem_univ x, rw [←h2, mem_support] at hx, have hy : y ∈ (⊤ : finset α) := finset.mem_univ y, rw [←h2, mem_support] at hy, cases is_cycle.exists_pow_eq h1 hx hy with n hn, rw ← hn, exact step2 n }, have step4 : ∀ (y z : α), swap y z ∈ H, { intros y z, by_cases h5 : z = x, { rw [h5, swap_comm], exact step3 y }, by_cases h6 : z = y, { rw [h6, swap_self], exact H.one_mem }, rw [←swap_mul_swap_mul_swap h5 h6, swap_comm z x], exact H.mul_mem (H.mul_mem (step3 y) (step3 z)) (step3 y) }, rw [eq_top_iff, ←closure_is_swap, closure_le], rintros τ ⟨y, z, h5, h6⟩, rw h6, exact step4 y z, end lemma closure_cycle_coprime_swap {n : ℕ} {σ : perm α} (h0 : nat.coprime n (fintype.card α)) (h1 : is_cycle σ) (h2 : σ.support = finset.univ) (x : α) : closure ({σ, swap x ((σ ^ n) x)} : set (perm α)) = ⊤ := begin rw [←finset.card_univ, ←h2, ←order_of_is_cycle h1] at h0, cases exists_pow_eq_self_of_coprime h0 with m hm, have h2' : (σ ^ n).support = ⊤ := eq.trans (support_pow_coprime h0) h2, have h1' : is_cycle ((σ ^ n) ^ (m : ℤ)) := by rwa ← hm at h1, replace h1' : is_cycle (σ ^ n) := is_cycle_of_is_cycle_gpow h1' (le_trans (support_gpow_le σ n) (ge_of_eq (congr_arg support hm))), rw [eq_top_iff, ←closure_cycle_adjacent_swap h1' h2' x, closure_le, set.insert_subset], exact ⟨subgroup.pow_mem (closure _) (subset_closure (set.mem_insert σ _)) n, set.singleton_subset_iff.mpr (subset_closure (set.mem_insert_of_mem _ (set.mem_singleton _)))⟩, end lemma closure_prime_cycle_swap {σ τ : perm α} (h0 : (fintype.card α).prime) (h1 : is_cycle σ) (h2 : σ.support = finset.univ) (h3 : is_swap τ) : closure ({σ, τ} : set (perm α)) = ⊤ := begin obtain ⟨x, y, h4, h5⟩ := h3, obtain ⟨i, hi⟩ := h1.exists_pow_eq (mem_support.mp ((finset.ext_iff.mp h2 x).mpr (finset.mem_univ x))) (mem_support.mp ((finset.ext_iff.mp h2 y).mpr (finset.mem_univ y))), rw [h5, ←hi], refine closure_cycle_coprime_swap (nat.coprime.symm (h0.coprime_iff_not_dvd.mpr (λ h, h4 _))) h1 h2 x, cases h with m hm, rwa [hm, pow_mul, ←finset.card_univ, ←h2, ←order_of_is_cycle h1, pow_order_of_eq_one, one_pow, one_apply] at hi, end end generation section variables [fintype α] {σ τ : perm α} noncomputable theory lemma is_conj_of_support_equiv (f : {x // x ∈ (σ.support : set α)} ≃ {x // x ∈ (τ.support : set α)}) (hf : ∀ (x : α) (hx : x ∈ (σ.support : set α)), (f ⟨σ x, apply_mem_support.2 hx⟩ : α) = τ ↑(f ⟨x,hx⟩)) : is_conj σ τ := begin refine is_conj_iff.2 ⟨equiv.extend_subtype f, _⟩, rw mul_inv_eq_iff_eq_mul, ext, simp only [perm.mul_apply], by_cases hx : x ∈ σ.support, { rw [equiv.extend_subtype_apply_of_mem, equiv.extend_subtype_apply_of_mem], { exact hf x (finset.mem_coe.2 hx) } }, { rwa [not_not.1 ((not_congr mem_support).1 (equiv.extend_subtype_not_mem f _ _)), not_not.1 ((not_congr mem_support).mp hx)] } end theorem is_cycle.is_conj (hσ : is_cycle σ) (hτ : is_cycle τ) (h : σ.support.card = τ.support.card) : is_conj σ τ := begin refine is_conj_of_support_equiv (hσ.gpowers_equiv_support.symm.trans ((gpowers_equiv_gpowers begin rw [order_of_is_cycle hσ, h, order_of_is_cycle hτ], end).trans hτ.gpowers_equiv_support)) _, intros x hx, simp only [perm.mul_apply, equiv.trans_apply, equiv.sum_congr_apply], obtain ⟨n, rfl⟩ := hσ.exists_pow_eq (classical.some_spec hσ).1 (mem_support.1 hx), apply eq.trans _ (congr rfl (congr rfl (congr rfl (congr rfl (hσ.gpowers_equiv_support_symm_apply n).symm)))), apply (congr rfl (congr rfl (congr rfl (hσ.gpowers_equiv_support_symm_apply (n + 1))))).trans _, simp only [ne.def, is_cycle.gpowers_equiv_support_apply, subtype.coe_mk, gpowers_equiv_gpowers_apply], rw [pow_succ, perm.mul_apply], end theorem is_cycle.is_conj_iff (hσ : is_cycle σ) (hτ : is_cycle τ) : is_conj σ τ ↔ σ.support.card = τ.support.card := ⟨begin intro h, obtain ⟨π, rfl⟩ := is_conj_iff.1 h, apply finset.card_congr (λ a ha, π a) (λ _ ha, _) (λ _ _ _ _ ab, π.injective ab) (λ b hb, _), { simp [mem_support.1 ha] }, { refine ⟨π⁻¹ b, ⟨_, π.apply_inv_self b⟩⟩, contrapose! hb, rw [mem_support, not_not] at hb, rw [mem_support, not_not, perm.mul_apply, perm.mul_apply, hb, perm.apply_inv_self] } end, hσ.is_conj hτ⟩ @[simp] lemma support_conj : (σ * τ * σ⁻¹).support = τ.support.map σ.to_embedding := begin ext, simp only [mem_map_equiv, perm.coe_mul, comp_app, ne.def, perm.mem_support, equiv.eq_symm_apply], refl, end lemma card_support_conj : (σ * τ * σ⁻¹).support.card = τ.support.card := by simp end theorem disjoint.is_conj_mul {α : Type*} [fintype α] {σ τ π ρ : perm α} (hc1 : is_conj σ π) (hc2 : is_conj τ ρ) (hd1 : disjoint σ τ) (hd2 : disjoint π ρ) : is_conj (σ * τ) (π * ρ) := begin classical, obtain ⟨f, rfl⟩ := is_conj_iff.1 hc1, obtain ⟨g, rfl⟩ := is_conj_iff.1 hc2, have hd1' := coe_inj.2 hd1.support_mul, have hd2' := coe_inj.2 hd2.support_mul, rw [coe_union] at *, have hd1'' := disjoint_iff_disjoint_coe.1 (disjoint_iff_disjoint_support.1 hd1), have hd2'' := disjoint_iff_disjoint_coe.1 (disjoint_iff_disjoint_support.1 hd2), refine is_conj_of_support_equiv _ _, { refine ((equiv.set.of_eq hd1').trans (equiv.set.union hd1'')).trans ((equiv.sum_congr (subtype_equiv f (λ a, _)) (subtype_equiv g (λ a, _))).trans ((equiv.set.of_eq hd2').trans (equiv.set.union hd2'')).symm); { simp only [set.mem_image, to_embedding_apply, exists_eq_right, support_conj, coe_map, apply_eq_iff_eq] } }, { intros x hx, simp only [trans_apply, symm_trans_apply, set.of_eq_apply, set.of_eq_symm_apply, equiv.sum_congr_apply], rw [hd1', set.mem_union] at hx, cases hx with hxσ hxτ, { rw [mem_coe, mem_support] at hxσ, rw [set.union_apply_left hd1'' _, set.union_apply_left hd1'' _], simp only [subtype_equiv_apply, perm.coe_mul, sum.map_inl, comp_app, set.union_symm_apply_left, subtype.coe_mk, apply_eq_iff_eq], { have h := (hd2 (f x)).resolve_left _, { rw [mul_apply, mul_apply] at h, rw [h, inv_apply_self, (hd1 x).resolve_left hxσ] }, { rwa [mul_apply, mul_apply, inv_apply_self, apply_eq_iff_eq] } }, { rwa [subtype.coe_mk, subtype.coe_mk, mem_coe, mem_support] }, { rwa [subtype.coe_mk, subtype.coe_mk, perm.mul_apply, (hd1 x).resolve_left hxσ, mem_coe, apply_mem_support, mem_support] } }, { rw [mem_coe, ← apply_mem_support, mem_support] at hxτ, rw [set.union_apply_right hd1'' _, set.union_apply_right hd1'' _], simp only [subtype_equiv_apply, perm.coe_mul, sum.map_inr, comp_app, set.union_symm_apply_right, subtype.coe_mk, apply_eq_iff_eq], { have h := (hd2 (g (τ x))).resolve_right _, { rw [mul_apply, mul_apply] at h, rw [inv_apply_self, h, (hd1 (τ x)).resolve_right hxτ] }, { rwa [mul_apply, mul_apply, inv_apply_self, apply_eq_iff_eq] } }, { rwa [subtype.coe_mk, subtype.coe_mk, mem_coe, ← apply_mem_support, mem_support] }, { rwa [subtype.coe_mk, subtype.coe_mk, perm.mul_apply, (hd1 (τ x)).resolve_right hxτ, mem_coe, mem_support] } } } end section fixed_points /-! ### Fixed points -/ lemma fixed_point_card_lt_of_ne_one [fintype α] {σ : perm α} (h : σ ≠ 1) : (filter (λ x, σ x = x) univ).card < fintype.card α - 1 := begin rw [nat.lt_sub_left_iff_add_lt, ← nat.lt_sub_right_iff_add_lt, ← finset.card_compl, finset.compl_filter], exact one_lt_card_support_of_ne_one h end end fixed_points end equiv.perm
f3a2d96ea07fb1cbd1475116ca64e9b3e500156f
1a8c2c9f2b591362e78c4cafa31aa400c878b7e8
/library/init/meta/default.lean
53eec7ab66e48144b3582b424e68f044d2b46d5e
[ "Apache-2.0" ]
permissive
jlpaca/lean-freebsd
ea88cd82635fe6eb0391ebb30e830a58be97cb1a
851cdaacf5d6caf50a42425b9246f427f0551866
refs/heads/master
1,613,685,045,763
1,590,309,513,000
1,590,309,513,000
245,241,297
0
0
null
null
null
null
UTF-8
Lean
false
false
1,051
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.name init.meta.options init.meta.format init.meta.rb_map import init.meta.level init.meta.expr init.meta.environment init.meta.attribute import init.meta.tactic init.meta.contradiction_tactic init.meta.constructor_tactic import init.meta.injection_tactic init.meta.relation_tactics init.meta.fun_info import init.meta.congr_lemma init.meta.match_tactic init.meta.ac_tactics import init.meta.backward init.meta.rewrite_tactic import init.meta.derive init.meta.mk_dec_eq_instance import init.meta.simp_tactic init.meta.set_get_option_tactics import init.meta.interactive init.meta.converter init.meta.vm import init.meta.comp_value_tactics init.meta.smt import init.meta.async_tactic init.meta.ref import init.meta.hole_command init.meta.congr_tactic import init.meta.local_context init.meta.type_context import init.meta.module_info import init.meta.expr_address
29d9d72af4fa19fcec578741053569ae8f25658f
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/constructions_auto.lean
371ba8786ab8520741e4b9651e59f2c0c397a669
[]
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
35,201
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, Patrick Massot -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.maps import Mathlib.PostPort universes u v w x u_1 u_2 u_3 u_4 namespace Mathlib /-! # Constructions of new topological spaces from old ones This file constructs products, sums, subtypes and quotients of topological spaces and sets up their basic theory, such as criteria for maps into or out of these constructions to be continuous; descriptions of the open sets, neighborhood filters, and generators of these constructions; and their behavior with respect to embeddings and other specific classes of maps. ## Implementation note The constructed topologies are defined using induced and coinduced topologies along with the complete lattice structure on topologies. Their universal properties (for example, a map `X → Y × Z` is continuous if and only if both projections `X → Y`, `X → Z` are) follow easily using order-theoretic descriptions of continuity. With more work we can also extract descriptions of the open sets, neighborhood filters and so on. ## Tags product, sum, disjoint union, subspace, quotient space -/ protected instance subtype.topological_space {α : Type u} {p : α → Prop} [t : topological_space α] : topological_space (Subtype p) := topological_space.induced coe t protected instance quot.topological_space {α : Type u} {r : α → α → Prop} [t : topological_space α] : topological_space (Quot r) := topological_space.coinduced (Quot.mk r) t protected instance quotient.topological_space {α : Type u} {s : setoid α} [t : topological_space α] : topological_space (quotient s) := topological_space.coinduced quotient.mk t protected instance prod.topological_space {α : Type u} {β : Type v} [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α × β) := topological_space.induced prod.fst t₁ ⊓ topological_space.induced prod.snd t₂ protected instance sum.topological_space {α : Type u} {β : Type v} [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α ⊕ β) := topological_space.coinduced sum.inl t₁ ⊔ topological_space.coinduced sum.inr t₂ protected instance sigma.topological_space {α : Type u} {β : α → Type v} [t₂ : (a : α) → topological_space (β a)] : topological_space (sigma β) := supr fun (a : α) => topological_space.coinduced (sigma.mk a) (t₂ a) protected instance Pi.topological_space {α : Type u} {β : α → Type v} [t₂ : (a : α) → topological_space (β a)] : topological_space ((a : α) → β a) := infi fun (a : α) => topological_space.induced (fun (f : (a : α) → β a) => f a) (t₂ a) protected instance ulift.topological_space {α : Type u} [t : topological_space α] : topological_space (ulift α) := topological_space.induced ulift.down t /-- The image of a dense set under `quotient.mk` is a dense set. -/ theorem dense.quotient {α : Type u} [setoid α] [topological_space α] {s : set α} (H : dense s) : dense (quotient.mk '' s) := dense_range.dense_image (function.surjective.dense_range (surjective_quotient_mk α)) continuous_coinduced_rng H /-- The composition of `quotient.mk` and a function with dense range has dense range. -/ theorem dense_range.quotient {α : Type u} {β : Type v} [setoid α] [topological_space α] {f : β → α} (hf : dense_range f) : dense_range (quotient.mk ∘ f) := dense_range.comp (function.surjective.dense_range (surjective_quotient_mk α)) hf continuous_coinduced_rng protected instance subtype.discrete_topology {α : Type u} {p : α → Prop} [topological_space α] [discrete_topology α] : discrete_topology (Subtype p) := discrete_topology.mk (bot_unique fun (s : set (Subtype p)) (hs : topological_space.is_open ⊥ s) => Exists.intro (coe '' s) { left := is_open_discrete (coe '' s), right := set.preimage_image_eq s subtype.coe_injective }) protected instance sum.discrete_topology {α : Type u} {β : Type v} [topological_space α] [topological_space β] [hα : discrete_topology α] [hβ : discrete_topology β] : discrete_topology (α ⊕ β) := sorry protected instance sigma.discrete_topology {α : Type u} {β : α → Type v} [(a : α) → topological_space (β a)] [h : ∀ (a : α), discrete_topology (β a)] : discrete_topology (sigma β) := sorry /- The 𝓝 filter and the subspace topology. -/ theorem mem_nhds_subtype {α : Type u} [topological_space α] (s : set α) (a : Subtype fun (x : α) => x ∈ s) (t : set (Subtype fun (x : α) => x ∈ s)) : t ∈ nhds a ↔ ∃ (u : set α), ∃ (H : u ∈ nhds ↑a), coe ⁻¹' u ⊆ t := mem_nhds_induced coe a t theorem nhds_subtype {α : Type u} [topological_space α] (s : set α) (a : Subtype fun (x : α) => x ∈ s) : nhds a = filter.comap coe (nhds ↑a) := nhds_induced coe a theorem continuous_fst {α : Type u} {β : Type v} [topological_space α] [topological_space β] : continuous prod.fst := continuous_inf_dom_left continuous_induced_dom theorem continuous_at_fst {α : Type u} {β : Type v} [topological_space α] [topological_space β] {p : α × β} : continuous_at prod.fst p := continuous.continuous_at continuous_fst theorem continuous_snd {α : Type u} {β : Type v} [topological_space α] [topological_space β] : continuous prod.snd := continuous_inf_dom_right continuous_induced_dom theorem continuous_at_snd {α : Type u} {β : Type v} [topological_space α] [topological_space β] {p : α × β} : continuous_at prod.snd p := continuous.continuous_at continuous_snd theorem continuous.prod_mk {α : Type u} {β : Type v} {γ : Type w} [topological_space α] [topological_space β] [topological_space γ] {f : γ → α} {g : γ → β} (hf : continuous f) (hg : continuous g) : continuous fun (x : γ) => (f x, g x) := continuous_inf_rng (continuous_induced_rng hf) (continuous_induced_rng hg) theorem continuous.prod_map {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] {f : γ → α} {g : δ → β} (hf : continuous f) (hg : continuous g) : continuous fun (x : γ × δ) => (f (prod.fst x), g (prod.snd x)) := continuous.prod_mk (continuous.comp hf continuous_fst) (continuous.comp hg continuous_snd) theorem filter.eventually.prod_inl_nhds {α : Type u} {β : Type v} [topological_space α] [topological_space β] {p : α → Prop} {a : α} (h : filter.eventually (fun (x : α) => p x) (nhds a)) (b : β) : filter.eventually (fun (x : α × β) => p (prod.fst x)) (nhds (a, b)) := continuous_at_fst h theorem filter.eventually.prod_inr_nhds {α : Type u} {β : Type v} [topological_space α] [topological_space β] {p : β → Prop} {b : β} (h : filter.eventually (fun (x : β) => p x) (nhds b)) (a : α) : filter.eventually (fun (x : α × β) => p (prod.snd x)) (nhds (a, b)) := continuous_at_snd h theorem filter.eventually.prod_mk_nhds {α : Type u} {β : Type v} [topological_space α] [topological_space β] {pa : α → Prop} {a : α} (ha : filter.eventually (fun (x : α) => pa x) (nhds a)) {pb : β → Prop} {b : β} (hb : filter.eventually (fun (y : β) => pb y) (nhds b)) : filter.eventually (fun (p : α × β) => pa (prod.fst p) ∧ pb (prod.snd p)) (nhds (a, b)) := filter.eventually.and (filter.eventually.prod_inl_nhds ha b) (filter.eventually.prod_inr_nhds hb a) theorem continuous_swap {α : Type u} {β : Type v} [topological_space α] [topological_space β] : continuous prod.swap := continuous.prod_mk continuous_snd continuous_fst theorem continuous_uncurry_left {α : Type u} {β : Type v} {γ : Type w} [topological_space α] [topological_space β] [topological_space γ] {f : α → β → γ} (a : α) (h : continuous (function.uncurry f)) : continuous (f a) := (fun (this : continuous (function.uncurry f ∘ fun (b : β) => (a, b))) => this) (continuous.comp h (continuous.prod_mk continuous_const continuous_id')) theorem continuous_uncurry_right {α : Type u} {β : Type v} {γ : Type w} [topological_space α] [topological_space β] [topological_space γ] {f : α → β → γ} (b : β) (h : continuous (function.uncurry f)) : continuous fun (a : α) => f a b := (fun (this : continuous (function.uncurry f ∘ fun (a : α) => (a, b))) => this) (continuous.comp h (continuous.prod_mk continuous_id' continuous_const)) theorem continuous_curry {α : Type u} {β : Type v} {γ : Type w} [topological_space α] [topological_space β] [topological_space γ] {g : α × β → γ} (a : α) (h : continuous g) : continuous (function.curry g a) := (fun (this : continuous (g ∘ fun (b : β) => (a, b))) => this) (continuous.comp h (continuous.prod_mk continuous_const continuous_id')) theorem is_open.prod {α : Type u} {β : Type v} [topological_space α] [topological_space β] {s : set α} {t : set β} (hs : is_open s) (ht : is_open t) : is_open (set.prod s t) := is_open_inter (is_open.preimage continuous_fst hs) (is_open.preimage continuous_snd ht) theorem nhds_prod_eq {α : Type u} {β : Type v} [topological_space α] [topological_space β] {a : α} {b : β} : nhds (a, b) = filter.prod (nhds a) (nhds b) := sorry theorem mem_nhds_prod_iff {α : Type u} {β : Type v} [topological_space α] [topological_space β] {a : α} {b : β} {s : set (α × β)} : s ∈ nhds (a, b) ↔ ∃ (u : set α), ∃ (H : u ∈ nhds a), ∃ (v : set β), ∃ (H : v ∈ nhds b), set.prod u v ⊆ s := sorry theorem filter.has_basis.prod_nhds {α : Type u} {β : Type v} [topological_space α] [topological_space β] {ιa : Type u_1} {ιb : Type u_2} {pa : ιa → Prop} {pb : ιb → Prop} {sa : ιa → set α} {sb : ιb → set β} {a : α} {b : β} (ha : filter.has_basis (nhds a) pa sa) (hb : filter.has_basis (nhds b) pb sb) : filter.has_basis (nhds (a, b)) (fun (i : ιa × ιb) => pa (prod.fst i) ∧ pb (prod.snd i)) fun (i : ιa × ιb) => set.prod (sa (prod.fst i)) (sb (prod.snd i)) := sorry protected instance prod.discrete_topology {α : Type u} {β : Type v} [topological_space α] [topological_space β] [discrete_topology α] [discrete_topology β] : discrete_topology (α × β) := discrete_topology.mk (eq_of_nhds_eq_nhds fun (_x : α × β) => sorry) theorem prod_mem_nhds_sets {α : Type u} {β : Type v} [topological_space α] [topological_space β] {s : set α} {t : set β} {a : α} {b : β} (ha : s ∈ nhds a) (hb : t ∈ nhds b) : set.prod s t ∈ nhds (a, b) := eq.mpr (id (Eq._oldrec (Eq.refl (set.prod s t ∈ nhds (a, b))) nhds_prod_eq)) (filter.prod_mem_prod ha hb) theorem nhds_swap {α : Type u} {β : Type v} [topological_space α] [topological_space β] (a : α) (b : β) : nhds (a, b) = filter.map prod.swap (nhds (b, a)) := sorry theorem filter.tendsto.prod_mk_nhds {α : Type u} {β : Type v} [topological_space α] [topological_space β] {γ : Type u_1} {a : α} {b : β} {f : filter γ} {ma : γ → α} {mb : γ → β} (ha : filter.tendsto ma f (nhds a)) (hb : filter.tendsto mb f (nhds b)) : filter.tendsto (fun (c : γ) => (ma c, mb c)) f (nhds (a, b)) := eq.mpr (id (Eq._oldrec (Eq.refl (filter.tendsto (fun (c : γ) => (ma c, mb c)) f (nhds (a, b)))) nhds_prod_eq)) (filter.tendsto.prod_mk ha hb) theorem filter.eventually.curry_nhds {α : Type u} {β : Type v} [topological_space α] [topological_space β] {p : α × β → Prop} {x : α} {y : β} (h : filter.eventually (fun (x : α × β) => p x) (nhds (x, y))) : filter.eventually (fun (x' : α) => filter.eventually (fun (y' : β) => p (x', y')) (nhds y)) (nhds x) := filter.eventually.curry (eq.mp (Eq._oldrec (Eq.refl (filter.eventually (fun (x : α × β) => p x) (nhds (x, y)))) nhds_prod_eq) h) theorem continuous_at.prod {α : Type u} {β : Type v} {γ : Type w} [topological_space α] [topological_space β] [topological_space γ] {f : α → β} {g : α → γ} {x : α} (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (fun (x : α) => (f x, g x)) x := filter.tendsto.prod_mk_nhds hf hg theorem continuous_at.prod_map {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] {f : α → γ} {g : β → δ} {p : α × β} (hf : continuous_at f (prod.fst p)) (hg : continuous_at g (prod.snd p)) : continuous_at (fun (p : α × β) => (f (prod.fst p), g (prod.snd p))) p := continuous_at.prod (continuous_at.comp hf (continuous.continuous_at continuous_fst)) (continuous_at.comp hg (continuous.continuous_at continuous_snd)) theorem continuous_at.prod_map' {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] {f : α → γ} {g : β → δ} {x : α} {y : β} (hf : continuous_at f x) (hg : continuous_at g y) : continuous_at (fun (p : α × β) => (f (prod.fst p), g (prod.snd p))) (x, y) := (fun (hf : continuous_at f (prod.fst (x, y))) => (fun (hg : continuous_at g (prod.snd (x, y))) => continuous_at.prod_map hf hg) hg) hf theorem prod_generate_from_generate_from_eq {α : Type u_1} {β : Type u_2} {s : set (set α)} {t : set (set β)} (hs : ⋃₀s = set.univ) (ht : ⋃₀t = set.univ) : prod.topological_space = topological_space.generate_from (set_of fun (g : set (α × β)) => ∃ (u : set α), ∃ (H : u ∈ s), ∃ (v : set β), ∃ (H : v ∈ t), g = set.prod u v) := sorry theorem prod_eq_generate_from {α : Type u} {β : Type v} [topological_space α] [topological_space β] : prod.topological_space = topological_space.generate_from (set_of fun (g : set (α × β)) => ∃ (s : set α), ∃ (t : set β), is_open s ∧ is_open t ∧ g = set.prod s t) := sorry theorem is_open_prod_iff {α : Type u} {β : Type v} [topological_space α] [topological_space β] {s : set (α × β)} : is_open s ↔ ∀ (a : α) (b : β), (a, b) ∈ s → ∃ (u : set α), ∃ (v : set β), is_open u ∧ is_open v ∧ a ∈ u ∧ b ∈ v ∧ set.prod u v ⊆ s := sorry theorem continuous_uncurry_of_discrete_topology_left {α : Type u} {β : Type v} {γ : Type w} [topological_space α] [topological_space β] [topological_space γ] [discrete_topology α] {f : α → β → γ} (h : ∀ (a : α), continuous (f a)) : continuous (function.uncurry f) := sorry /-- Given a neighborhood `s` of `(x, x)`, then `(x, x)` has a square open neighborhood that is a subset of `s`. -/ theorem exists_nhds_square {α : Type u} [topological_space α] {s : set (α × α)} {x : α} (hx : s ∈ nhds (x, x)) : ∃ (U : set α), is_open U ∧ x ∈ U ∧ set.prod U U ⊆ s := sorry /-- The first projection in a product of topological spaces sends open sets to open sets. -/ theorem is_open_map_fst {α : Type u} {β : Type v} [topological_space α] [topological_space β] : is_open_map prod.fst := sorry /-- The second projection in a product of topological spaces sends open sets to open sets. -/ theorem is_open_map_snd {α : Type u} {β : Type v} [topological_space α] [topological_space β] : is_open_map prod.snd := sorry /-- A product set is open in a product space if and only if each factor is open, or one of them is empty -/ theorem is_open_prod_iff' {α : Type u} {β : Type v} [topological_space α] [topological_space β] {s : set α} {t : set β} : is_open (set.prod s t) ↔ is_open s ∧ is_open t ∨ s = ∅ ∨ t = ∅ := sorry theorem closure_prod_eq {α : Type u} {β : Type v} [topological_space α] [topological_space β] {s : set α} {t : set β} : closure (set.prod s t) = set.prod (closure s) (closure t) := sorry theorem map_mem_closure2 {α : Type u} {β : Type v} {γ : Type w} [topological_space α] [topological_space β] [topological_space γ] {s : set α} {t : set β} {u : set γ} {f : α → β → γ} {a : α} {b : β} (hf : continuous fun (p : α × β) => f (prod.fst p) (prod.snd p)) (ha : a ∈ closure s) (hb : b ∈ closure t) (hu : ∀ (a : α) (b : β), a ∈ s → b ∈ t → f a b ∈ u) : f a b ∈ closure u := sorry theorem is_closed.prod {α : Type u} {β : Type v} [topological_space α] [topological_space β] {s₁ : set α} {s₂ : set β} (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (set.prod s₁ s₂) := sorry /-- The product of two dense sets is a dense set. -/ theorem dense.prod {α : Type u} {β : Type v} [topological_space α] [topological_space β] {s : set α} {t : set β} (hs : dense s) (ht : dense t) : dense (set.prod s t) := fun (x : α × β) => eq.mpr (id (Eq._oldrec (Eq.refl (x ∈ closure (set.prod s t))) closure_prod_eq)) { left := hs (prod.fst x), right := ht (prod.snd x) } /-- If `f` and `g` are maps with dense range, then `prod.map f g` has dense range. -/ theorem dense_range.prod_map {β : Type v} {γ : Type w} [topological_space β] [topological_space γ] {ι : Type u_1} {κ : Type u_2} {f : ι → β} {g : κ → γ} (hf : dense_range f) (hg : dense_range g) : dense_range (prod.map f g) := sorry theorem inducing.prod_mk {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] {f : α → β} {g : γ → δ} (hf : inducing f) (hg : inducing g) : inducing fun (x : α × γ) => (f (prod.fst x), g (prod.snd x)) := sorry theorem embedding.prod_mk {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] {f : α → β} {g : γ → δ} (hf : embedding f) (hg : embedding g) : embedding fun (x : α × γ) => (f (prod.fst x), g (prod.snd x)) := sorry protected theorem is_open_map.prod {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] {f : α → β} {g : γ → δ} (hf : is_open_map f) (hg : is_open_map g) : is_open_map fun (p : α × γ) => (f (prod.fst p), g (prod.snd p)) := sorry protected theorem open_embedding.prod {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] {f : α → β} {g : γ → δ} (hf : open_embedding f) (hg : open_embedding g) : open_embedding fun (x : α × γ) => (f (prod.fst x), g (prod.snd x)) := open_embedding_of_embedding_open (embedding.prod_mk (open_embedding.to_embedding hf) (open_embedding.to_embedding hg)) (is_open_map.prod (open_embedding.is_open_map hf) (open_embedding.is_open_map hg)) theorem embedding_graph {α : Type u} {β : Type v} [topological_space α] [topological_space β] {f : α → β} (hf : continuous f) : embedding fun (x : α) => (x, f x) := embedding_of_embedding_compose (continuous.prod_mk continuous_id hf) continuous_fst embedding_id theorem continuous_inl {α : Type u} {β : Type v} [topological_space α] [topological_space β] : continuous sum.inl := continuous_sup_rng_left continuous_coinduced_rng theorem continuous_inr {α : Type u} {β : Type v} [topological_space α] [topological_space β] : continuous sum.inr := continuous_sup_rng_right continuous_coinduced_rng theorem continuous_sum_rec {α : Type u} {β : Type v} {γ : Type w} [topological_space α] [topological_space β] [topological_space γ] {f : α → γ} {g : β → γ} (hf : continuous f) (hg : continuous g) : continuous (sum.rec f g) := sorry theorem is_open_sum_iff {α : Type u} {β : Type v} [topological_space α] [topological_space β] {s : set (α ⊕ β)} : is_open s ↔ is_open (sum.inl ⁻¹' s) ∧ is_open (sum.inr ⁻¹' s) := iff.rfl theorem is_open_map_sum {α : Type u} {β : Type v} {γ : Type w} [topological_space α] [topological_space β] [topological_space γ] {f : α ⊕ β → γ} (h₁ : is_open_map fun (a : α) => f (sum.inl a)) (h₂ : is_open_map fun (b : β) => f (sum.inr b)) : is_open_map f := sorry theorem embedding_inl {α : Type u} {β : Type v} [topological_space α] [topological_space β] : embedding sum.inl := sorry theorem embedding_inr {α : Type u} {β : Type v} [topological_space α] [topological_space β] : embedding sum.inr := sorry theorem is_open_range_inl {α : Type u} {β : Type v} [topological_space α] [topological_space β] : is_open (set.range sum.inl) := sorry theorem is_open_range_inr {α : Type u} {β : Type v} [topological_space α] [topological_space β] : is_open (set.range sum.inr) := sorry theorem open_embedding_inl {α : Type u} {β : Type v} [topological_space α] [topological_space β] : open_embedding sum.inl := open_embedding.mk (embedding.mk (embedding.to_inducing embedding_inl) (embedding.inj embedding_inl)) is_open_range_inl theorem open_embedding_inr {α : Type u} {β : Type v} [topological_space α] [topological_space β] : open_embedding sum.inr := open_embedding.mk (embedding.mk (embedding.to_inducing embedding_inr) (embedding.inj embedding_inr)) is_open_range_inr theorem embedding_subtype_coe {α : Type u} [topological_space α] {p : α → Prop} : embedding coe := embedding.mk (inducing.mk rfl) subtype.coe_injective theorem continuous_subtype_val {α : Type u} [topological_space α] {p : α → Prop} : continuous subtype.val := continuous_induced_dom theorem continuous_subtype_coe {α : Type u} [topological_space α] {p : α → Prop} : continuous coe := continuous_subtype_val theorem is_open.open_embedding_subtype_coe {α : Type u} [topological_space α] {s : set α} (hs : is_open s) : open_embedding coe := open_embedding.mk (embedding.mk (inducing.mk rfl) subtype.coe_injective) (Eq.symm subtype.range_coe ▸ hs) theorem is_open.is_open_map_subtype_coe {α : Type u} [topological_space α] {s : set α} (hs : is_open s) : is_open_map coe := open_embedding.is_open_map (is_open.open_embedding_subtype_coe hs) theorem is_open_map.restrict {α : Type u} {β : Type v} [topological_space α] [topological_space β] {f : α → β} (hf : is_open_map f) {s : set α} (hs : is_open s) : is_open_map (set.restrict f s) := is_open_map.comp hf (is_open.is_open_map_subtype_coe hs) theorem is_closed.closed_embedding_subtype_coe {α : Type u} [topological_space α] {s : set α} (hs : is_closed s) : closed_embedding coe := closed_embedding.mk (embedding.mk (inducing.mk rfl) subtype.coe_injective) (Eq.symm subtype.range_coe ▸ hs) theorem continuous_subtype_mk {α : Type u} {β : Type v} [topological_space α] [topological_space β] {p : α → Prop} {f : β → α} (hp : ∀ (x : β), p (f x)) (h : continuous f) : continuous fun (x : β) => { val := f x, property := hp x } := continuous_induced_rng h theorem continuous_inclusion {α : Type u} [topological_space α] {s : set α} {t : set α} (h : s ⊆ t) : continuous (set.inclusion h) := continuous_subtype_mk (fun (x : ↥s) => set.inclusion._proof_1 h x) continuous_subtype_coe theorem continuous_at_subtype_coe {α : Type u} [topological_space α] {p : α → Prop} {a : Subtype p} : continuous_at coe a := iff.mp continuous_iff_continuous_at continuous_subtype_coe a theorem map_nhds_subtype_coe_eq {α : Type u} [topological_space α] {p : α → Prop} {a : α} (ha : p a) (h : (set_of fun (a : α) => p a) ∈ nhds a) : filter.map coe (nhds { val := a, property := ha }) = nhds a := sorry theorem nhds_subtype_eq_comap {α : Type u} [topological_space α] {p : α → Prop} {a : α} {h : p a} : nhds { val := a, property := h } = filter.comap coe (nhds a) := nhds_induced coe { val := a, property := h } theorem tendsto_subtype_rng {α : Type u} [topological_space α] {β : Type u_1} {p : α → Prop} {b : filter β} {f : β → Subtype p} {a : Subtype p} : filter.tendsto f b (nhds a) ↔ filter.tendsto (fun (x : β) => ↑(f x)) b (nhds ↑a) := sorry theorem continuous_subtype_nhds_cover {α : Type u} {β : Type v} [topological_space α] [topological_space β] {ι : Sort u_1} {f : α → β} {c : ι → α → Prop} (c_cover : ∀ (x : α), ∃ (i : ι), (set_of fun (x : α) => c i x) ∈ nhds x) (f_cont : ∀ (i : ι), continuous fun (x : Subtype (c i)) => f ↑x) : continuous f := sorry theorem continuous_subtype_is_closed_cover {α : Type u} {β : Type v} [topological_space α] [topological_space β] {ι : Type u_1} {f : α → β} (c : ι → α → Prop) (h_lf : locally_finite fun (i : ι) => set_of fun (x : α) => c i x) (h_is_closed : ∀ (i : ι), is_closed (set_of fun (x : α) => c i x)) (h_cover : ∀ (x : α), ∃ (i : ι), c i x) (f_cont : ∀ (i : ι), continuous fun (x : Subtype (c i)) => f ↑x) : continuous f := sorry theorem closure_subtype {α : Type u} [topological_space α] {p : α → Prop} {x : Subtype fun (a : α) => p a} {s : set (Subtype fun (a : α) => p a)} : x ∈ closure s ↔ ↑x ∈ closure (coe '' s) := closure_induced fun (x y : Subtype fun (a : α) => p a) => subtype.eq theorem quotient_map_quot_mk {α : Type u} [topological_space α] {r : α → α → Prop} : quotient_map (Quot.mk r) := { left := quot.exists_rep, right := rfl } theorem continuous_quot_mk {α : Type u} [topological_space α] {r : α → α → Prop} : continuous (Quot.mk r) := continuous_coinduced_rng theorem continuous_quot_lift {α : Type u} {β : Type v} [topological_space α] [topological_space β] {r : α → α → Prop} {f : α → β} (hr : ∀ (a b : α), r a b → f a = f b) (h : continuous f) : continuous (Quot.lift f hr) := continuous_coinduced_dom h theorem quotient_map_quotient_mk {α : Type u} [topological_space α] {s : setoid α} : quotient_map quotient.mk := quotient_map_quot_mk theorem continuous_quotient_mk {α : Type u} [topological_space α] {s : setoid α} : continuous quotient.mk := continuous_coinduced_rng theorem continuous_quotient_lift {α : Type u} {β : Type v} [topological_space α] [topological_space β] {s : setoid α} {f : α → β} (hs : ∀ (a b : α), a ≈ b → f a = f b) (h : continuous f) : continuous (quotient.lift f hs) := continuous_coinduced_dom h theorem continuous_pi {α : Type u} {ι : Type u_1} {π : ι → Type u_2} [topological_space α] [(i : ι) → topological_space (π i)] {f : α → (i : ι) → π i} (h : ∀ (i : ι), continuous fun (a : α) => f a i) : continuous f := continuous_infi_rng fun (i : ι) => continuous_induced_rng (h i) theorem continuous_apply {ι : Type u_1} {π : ι → Type u_2} [(i : ι) → topological_space (π i)] (i : ι) : continuous fun (p : (i : ι) → π i) => p i := continuous_infi_dom continuous_induced_dom /-- Embedding a factor into a product space (by fixing arbitrarily all the other coordinates) is continuous. -/ theorem continuous_update {ι : Type u_1} {π : ι → Type u_2} [DecidableEq ι] [(i : ι) → topological_space (π i)] {i : ι} {f : (i : ι) → π i} : continuous fun (x : π i) => function.update f i x := sorry theorem nhds_pi {ι : Type u_1} {π : ι → Type u_2} [t : (i : ι) → topological_space (π i)] {a : (i : ι) → π i} : nhds a = infi fun (i : ι) => filter.comap (fun (x : (i : ι) → π i) => x i) (nhds (a i)) := sorry theorem tendsto_pi {α : Type u} {ι : Type u_1} {π : ι → Type u_2} [t : (i : ι) → topological_space (π i)] {f : α → (i : ι) → π i} {g : (i : ι) → π i} {u : filter α} : filter.tendsto f u (nhds g) ↔ ∀ (x : ι), filter.tendsto (fun (i : α) => f i x) u (nhds (g x)) := sorry theorem is_open_set_pi {ι : Type u_1} {π : ι → Type u_2} [(a : ι) → topological_space (π a)] {i : set ι} {s : (a : ι) → set (π a)} (hi : set.finite i) (hs : ∀ (a : ι), a ∈ i → is_open (s a)) : is_open (set.pi i s) := eq.mpr (id (Eq._oldrec (Eq.refl (is_open (set.pi i s))) (set.pi_def i s))) (is_open_bInter hi fun (a : ι) (ha : a ∈ i) => is_open.preimage (continuous_apply a) (hs a ha)) theorem set_pi_mem_nhds {ι : Type u_1} {π : ι → Type u_2} [(a : ι) → topological_space (π a)] {i : set ι} {s : (a : ι) → set (π a)} {x : (a : ι) → π a} (hi : set.finite i) (hs : ∀ (a : ι), a ∈ i → s a ∈ nhds (x a)) : set.pi i s ∈ nhds x := sorry theorem pi_eq_generate_from {ι : Type u_1} {π : ι → Type u_2} [(a : ι) → topological_space (π a)] : Pi.topological_space = topological_space.generate_from (set_of fun (g : set ((a : ι) → π a)) => ∃ (s : (a : ι) → set (π a)), ∃ (i : finset ι), (∀ (a : ι), a ∈ i → is_open (s a)) ∧ g = set.pi (↑i) s) := sorry theorem pi_generate_from_eq {ι : Type u_1} {π : ι → Type u_2} {g : (a : ι) → set (set (π a))} : Pi.topological_space = topological_space.generate_from (set_of fun (t : set ((a : ι) → π a)) => ∃ (s : (a : ι) → set (π a)), ∃ (i : finset ι), (∀ (a : ι), a ∈ i → s a ∈ g a) ∧ t = set.pi (↑i) s) := sorry theorem pi_generate_from_eq_fintype {ι : Type u_1} {π : ι → Type u_2} {g : (a : ι) → set (set (π a))} [fintype ι] (hg : ∀ (a : ι), ⋃₀g a = set.univ) : Pi.topological_space = topological_space.generate_from (set_of fun (t : set ((a : ι) → π a)) => ∃ (s : (a : ι) → set (π a)), (∀ (a : ι), s a ∈ g a) ∧ t = set.pi set.univ s) := sorry theorem continuous_sigma_mk {ι : Type u_1} {σ : ι → Type u_2} [(i : ι) → topological_space (σ i)] {i : ι} : continuous (sigma.mk i) := continuous_supr_rng continuous_coinduced_rng theorem is_open_sigma_iff {ι : Type u_1} {σ : ι → Type u_2} [(i : ι) → topological_space (σ i)] {s : set (sigma σ)} : is_open s ↔ ∀ (i : ι), is_open (sigma.mk i ⁻¹' s) := sorry theorem is_closed_sigma_iff {ι : Type u_1} {σ : ι → Type u_2} [(i : ι) → topological_space (σ i)] {s : set (sigma σ)} : is_closed s ↔ ∀ (i : ι), is_closed (sigma.mk i ⁻¹' s) := is_open_sigma_iff theorem is_open_map_sigma_mk {ι : Type u_1} {σ : ι → Type u_2} [(i : ι) → topological_space (σ i)] {i : ι} : is_open_map (sigma.mk i) := sorry theorem is_open_range_sigma_mk {ι : Type u_1} {σ : ι → Type u_2} [(i : ι) → topological_space (σ i)] {i : ι} : is_open (set.range (sigma.mk i)) := eq.mpr (id (Eq._oldrec (Eq.refl (is_open (set.range (sigma.mk i)))) (Eq.symm set.image_univ))) (is_open_map_sigma_mk set.univ is_open_univ) theorem is_closed_map_sigma_mk {ι : Type u_1} {σ : ι → Type u_2} [(i : ι) → topological_space (σ i)] {i : ι} : is_closed_map (sigma.mk i) := sorry theorem is_closed_sigma_mk {ι : Type u_1} {σ : ι → Type u_2} [(i : ι) → topological_space (σ i)] {i : ι} : is_closed (set.range (sigma.mk i)) := eq.mpr (id (Eq._oldrec (Eq.refl (is_closed (set.range (sigma.mk i)))) (Eq.symm set.image_univ))) (is_closed_map_sigma_mk set.univ is_closed_univ) theorem open_embedding_sigma_mk {ι : Type u_1} {σ : ι → Type u_2} [(i : ι) → topological_space (σ i)] {i : ι} : open_embedding (sigma.mk i) := open_embedding_of_continuous_injective_open continuous_sigma_mk sigma_mk_injective is_open_map_sigma_mk theorem closed_embedding_sigma_mk {ι : Type u_1} {σ : ι → Type u_2} [(i : ι) → topological_space (σ i)] {i : ι} : closed_embedding (sigma.mk i) := closed_embedding_of_continuous_injective_closed continuous_sigma_mk sigma_mk_injective is_closed_map_sigma_mk theorem embedding_sigma_mk {ι : Type u_1} {σ : ι → Type u_2} [(i : ι) → topological_space (σ i)] {i : ι} : embedding (sigma.mk i) := closed_embedding.to_embedding closed_embedding_sigma_mk /-- A map out of a sum type is continuous if its restriction to each summand is. -/ theorem continuous_sigma {β : Type v} {ι : Type u_1} {σ : ι → Type u_2} [(i : ι) → topological_space (σ i)] [topological_space β] {f : sigma σ → β} (h : ∀ (i : ι), continuous fun (a : σ i) => f (sigma.mk i a)) : continuous f := continuous_supr_dom fun (i : ι) => continuous_coinduced_dom (h i) theorem continuous_sigma_map {ι : Type u_1} {σ : ι → Type u_2} [(i : ι) → topological_space (σ i)] {κ : Type u_3} {τ : κ → Type u_4} [(k : κ) → topological_space (τ k)] {f₁ : ι → κ} {f₂ : (i : ι) → σ i → τ (f₁ i)} (hf : ∀ (i : ι), continuous (f₂ i)) : continuous (sigma.map f₁ f₂) := sorry theorem is_open_map_sigma {β : Type v} {ι : Type u_1} {σ : ι → Type u_2} [(i : ι) → topological_space (σ i)] [topological_space β] {f : sigma σ → β} (h : ∀ (i : ι), is_open_map fun (a : σ i) => f (sigma.mk i a)) : is_open_map f := sorry /-- The sum of embeddings is an embedding. -/ theorem embedding_sigma_map {ι : Type u_1} {σ : ι → Type u_2} [(i : ι) → topological_space (σ i)] {τ : ι → Type u_3} [(i : ι) → topological_space (τ i)] {f : (i : ι) → σ i → τ i} (hf : ∀ (i : ι), embedding (f i)) : embedding (sigma.map id f) := sorry theorem continuous_ulift_down {α : Type u} [topological_space α] : continuous ulift.down := continuous_induced_dom theorem continuous_ulift_up {α : Type u} [topological_space α] : continuous ulift.up := continuous_induced_rng continuous_id theorem mem_closure_of_continuous {α : Type u} {β : Type v} [topological_space α] [topological_space β] {f : α → β} {a : α} {s : set α} {t : set β} (hf : continuous f) (ha : a ∈ closure s) (h : set.maps_to f s (closure t)) : f a ∈ closure t := set.mem_of_mem_of_subset (set.mem_of_mem_of_subset (set.mem_image_of_mem f ha) (image_closure_subset_closure_image hf)) (closure_minimal (set.maps_to.image_subset h) is_closed_closure) theorem mem_closure_of_continuous2 {α : Type u} {β : Type v} {γ : Type w} [topological_space α] [topological_space β] [topological_space γ] {f : α → β → γ} {a : α} {b : β} {s : set α} {t : set β} {u : set γ} (hf : continuous fun (p : α × β) => f (prod.fst p) (prod.snd p)) (ha : a ∈ closure s) (hb : b ∈ closure t) (h : ∀ (a : α), a ∈ s → ∀ (b : β), b ∈ t → f a b ∈ closure u) : f a b ∈ closure u := sorry end Mathlib
bb31efebc31f2a2f83b7e3a82408e0fa719bddd8
1437b3495ef9020d5413178aa33c0a625f15f15f
/category_theory/limits/limits.lean
bb9bdee843872e1a4104fd037f3b37817d8f9187
[ "Apache-2.0" ]
permissive
jean002/mathlib
c66bbb2d9fdc9c03ae07f869acac7ddbfce67a30
dc6c38a765799c99c4d9c8d5207d9e6c9e0e2cfd
refs/heads/master
1,587,027,806,375
1,547,306,358,000
1,547,306,358,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
25,664
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 import category_theory.whiskering import category_theory.yoneda import category_theory.limits.cones open category_theory category_theory.category category_theory.functor namespace category_theory.limits universes v u u' u'' w -- declare the `v`'s first; see `category_theory.category` for an explanation variables {J : Type v} [small_category J] variables {C : Type u} [𝒞 : category.{v} C] include 𝒞 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`. -/ 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] 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. -/ 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 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. -/ def unique {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 } 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) 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 /-- 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 (is_limit.hom_iso h) (by tidy) def hom_iso' (h : is_limit t) (W : C) : (W ⟶ t.X) ≅ { 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.injectivity; rw [G.map_comp, h]; apply ht.fac, uniq' := λ s m w, begin apply G.injectivity, rw h, refine ht.uniq (G.map_cone s) _ (λ j, _), convert ←congr_arg (λ f, G.map f) (w j), apply G.map_comp 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`. -/ 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] 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 cone morphisms. -/ 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 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 } /-- Limit cones on `F` are unique up to isomorphism. -/ def unique {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 } 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) 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 /-- 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 t.X ≅ F.cocones := nat_iso.of_components (is_colimit.hom_iso h) (by intros; ext; dsimp; rw ←assoc; refl) def hom_iso' (h : is_colimit t) (W : C) : (t.X ⟶ W) ≅ { 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.injectivity; rw [G.map_comp, h]; apply ht.fac, uniq' := λ s m w, begin apply G.injectivity, rw h, refine ht.uniq (G.map_cocone s) _ (λ j, _), convert ←congr_arg (λ f, G.map f) (w j), apply G.map_comp end } end is_colimit section limit /-- `has_limit F` represents a particular chosen limit of the diagram `F`. -/ class has_limit (F : J ⥤ C) := (cone : cone F) (is_limit : is_limit cone) variables (J C) /-- `C` has limits of shape `J` if we have chosen a particular limit of every functor `F : J ⥤ C`. -/ @[class] def has_limits_of_shape := Π F : J ⥤ C, has_limit F /-- `C` has all (small) limits if it has limits of every shape. -/ @[class] def has_limits := Π {J : Type v} {𝒥 : small_category J}, by exactI has_limits_of_shape J C variables {J C} 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 := H F instance has_limits_of_shape_of_has_limits {J : Type v} [small_category J] [H : has_limits.{v} C] : has_limits_of_shape J C := H /- Interface to the `has_limit` class. -/ def limit.cone (F : J ⥤ C) [has_limit F] : cone F := has_limit.cone F def limit (F : J ⥤ C) [has_limit F] := (limit.cone F).X def limit.π (F : J ⥤ C) [has_limit F] (j : J) : limit F ⟶ F.obj j := (limit.cone F).π.app j @[simp] lemma limit.cone_π {F : J ⥤ C} [has_limit F] (j : J) : (limit.cone F).π.app j = limit.π _ j := rfl @[simp] 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 def limit.is_limit (F : J ⥤ C) [has_limit F] : is_limit (limit.cone F) := has_limit.is_limit.{v} F 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] 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 def limit.cone_morphism {F : J ⥤ C} [has_limit F] (c : cone F) : cone_morphism 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 @[simp] 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 erw is_limit.fac @[extensionality] 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 def limit.hom_iso (F : J ⥤ C) [has_limit F] (W : C) : (W ⟶ limit F) ≅ (F.cones.obj 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 def limit.hom_iso' (F : J ⥤ C) [has_limit F] (W : C) : (W ⟶ limit F) ≅ { 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 section pre variables {K : Type v} [small_category K] variables (F) [has_limit F] (E : K ⥤ J) [has_limit (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] include 𝒟 variables (F) [has_limit F] (G : C ⥤ D) [has_limit (F ⋙ G)] def limit.post : G.obj (limit F) ⟶ limit (F ⋙ G) := limit.lift (F ⋙ G) { X := G.obj (limit F), π := { app := λ j, G.map (limit.π F j), naturality' := by intros j j' f; erw [←G.map_comp, limits.cone.w, id_comp]; refl } } @[simp] lemma limit.post_π (j : J) : limit.post F G ≫ limit.π (F ⋙ G) j = G.map (limit.π F j) := by erw is_limit.fac @[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 {K : Type v} [small_category K] {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 section lim_functor variables [has_limits_of_shape J C] /-- `limit F` is functorial in `F`, when `C` has all limits of shape `J`. -/ def lim : (J ⥤ C) ⥤ C := { obj := λ F, limit F, map := λ F G α, limit.lift G { X := limit F, π := { app := λ j, limit.π F j ≫ α.app j, naturality' := λ j j' f, by erw [id_comp, assoc, ←α.naturality, ←assoc, limit.w] } }, map_comp' := λ F G H α β, by ext; erw [assoc, is_limit.fac, is_limit.fac, ←assoc, is_limit.fac, assoc]; refl } variables {F} {G : J ⥤ C} (α : F ⟹ G) @[simp] lemma lim.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 (c.postcompose α) := by ext; rw [assoc, lim.map_π, ←assoc, limit.lift_π, limit.lift_π]; refl lemma limit.map_pre {K : Type v} [small_category K] [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_π, lim.map_π, assoc, lim.map_π, ←assoc, limit.pre_π]; refl lemma limit.map_pre' {K : Type v} [small_category K] [has_limits_of_shape.{v} 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 _ _ _ _).symm] lemma limit.id_pre (F : J ⥤ C) : limit.pre F (functor.id _) = 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, lim.map_π, H.map_comp], rw [assoc, lim.map_π, ←assoc, limit.post_π], refl end def lim_yoneda : lim ⋙ yoneda ≅ category_theory.cones J C := nat_iso.of_components (λ F, nat_iso.of_components (limit.hom_iso F) (by tidy)) (by tidy) end lim_functor end limit section colimit /-- `has_colimit F` represents a particular chosen colimit of the diagram `F`. -/ class has_colimit (F : J ⥤ C) := (cocone : cocone F) (is_colimit : is_colimit cocone) variables (J C) /-- `C` has colimits of shape `J` if we have chosen a particular colimit of every functor `F : J ⥤ C`. -/ @[class] def has_colimits_of_shape := Π F : J ⥤ C, has_colimit F /-- `C` has all (small) colimits if it has limits of every shape. -/ @[class] def has_colimits := Π {J : Type v} {𝒥 : small_category J}, by exactI has_colimits_of_shape J C variables {J C} 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 := H F instance has_colimits_of_shape_of_has_colimits {J : Type v} [small_category J] [H : has_colimits.{v} C] : has_colimits_of_shape J C := H /- Interface to the `has_colimit` class. -/ def colimit.cocone (F : J ⥤ C) [has_colimit F] : cocone F := has_colimit.cocone F def colimit (F : J ⥤ C) [has_colimit F] := (colimit.cocone F).X 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.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 def colimit.is_colimit (F : J ⥤ C) [has_colimit F] : is_colimit (colimit.cocone F) := has_colimit.is_colimit.{v} F 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 @[simp] 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 def colimit.cocone_morphism {F : J ⥤ C} [has_colimit F] (c : cocone F) : cocone_morphism (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 @[simp] 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 erw is_colimit.fac @[extensionality] 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 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 def colimit.hom_iso' (F : J ⥤ C) [has_colimit F] (W : C) : (colimit F ⟶ W) ≅ { 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, simp [category.assoc_symm], refl end section pre variables {K : Type v} [small_category K] variables (F) [has_colimit F] (E : K ⥤ J) [has_colimit (E ⋙ F)] def colimit.pre : colimit (E ⋙ F) ⟶ colimit F := colimit.desc (E ⋙ F) { X := colimit F, ι := { app := λ k, colimit.ι F (E.obj k) } } @[simp] 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] include 𝒟 variables (F) [has_colimit F] (G : C ⥤ D) [has_colimit (F ⋙ G)] def colimit.post : colimit (F ⋙ G) ⟶ G.obj (colimit F) := colimit.desc (F ⋙ G) { X := G.obj (colimit F), ι := { app := λ j, G.map (colimit.ι F j), naturality' := by intros j j' f; erw [←G.map_comp, limits.cocone.w, comp_id]; refl } } @[simp] lemma colimit.ι_post (j : J) : colimit.ι (F ⋙ G) j ≫ colimit.post F G = G.map (colimit.ι F j) := by erw is_colimit.fac @[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 {K : Type v} [small_category K] {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 section colim_functor variables [has_colimits_of_shape J C] /-- `colimit F` is functorial in `F`, when `C` has all colimits of shape `J`. -/ def colim : (J ⥤ C) ⥤ C := { obj := λ F, colimit F, map := λ F G α, colimit.desc F { X := colimit G, ι := { app := λ j, α.app j ≫ colimit.ι G j, naturality' := λ j j' f, by erw [comp_id, ←assoc, α.naturality, assoc, colimit.w] } }, map_comp' := λ F G H α β, by ext; erw [←assoc, is_colimit.fac, is_colimit.fac, assoc, is_colimit.fac, ←assoc]; refl } variables {F} {G : J ⥤ C} (α : F ⟹ G) @[simp] lemma colim.ι_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 (c.precompose α) := by ext; rw [←assoc, colim.ι_map, assoc, colimit.ι_desc, colimit.ι_desc]; refl lemma colimit.pre_map {K : Type v} [small_category K] [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, colim.ι_map, ←assoc, colim.ι_map, assoc, colimit.ι_pre]; refl lemma colimit.pre_map' {K : Type v} [small_category K] [has_colimits_of_shape.{v} 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 _ _ _ _).symm] lemma colimit.pre_id (F : J ⥤ C) : colimit.pre F (functor.id _) = 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, colim.ι_map, H.map_comp], rw [←assoc, colim.ι_map, assoc, colimit.ι_post], refl end def colim_coyoneda : colim.op ⋙ coyoneda ≅ category_theory.cocones J C := nat_iso.of_components (λ F, nat_iso.of_components (colimit.hom_iso F) (by {tidy, dsimp [functor.cocones], rw category.assoc })) (by {tidy, rw [← category.assoc,← category.assoc], tidy }) end colim_functor end colimit end category_theory.limits
a539cd760aae8ff99025891bba1959087077d627
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/basics/unnamed_713.lean
271382ce4764eb84c8a54ae79d85e1c9d2a64382
[]
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
254
lean
import algebra.ring namespace my_ring variables {R : Type*} [ring R] -- BEGIN theorem mul_zero (a : R) : a * 0 = 0 := begin have h : a * 0 + a * 0 = a * 0 + 0, { rw [←mul_add, add_zero, add_zero] }, rw add_left_cancel h end -- END end my_ring
c4ffa5b954b620953e5cc6b06dec7b1a15da0991
1a61aba1b67cddccce19532a9596efe44be4285f
/library/init/function.lean
2a8d286842347d41d6a94ece5a8d2b3b7e7d04f7
[ "Apache-2.0" ]
permissive
eigengrau/lean
07986a0f2548688c13ba36231f6cdbee82abf4c6
f8a773be1112015e2d232661ce616d23f12874d0
refs/heads/master
1,610,939,198,566
1,441,352,386,000
1,441,352,494,000
41,903,576
0
0
null
1,441,352,210,000
1,441,352,210,000
null
UTF-8
Lean
false
false
5,764
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad, Haitao Zhang General operations on functions. -/ prelude import init.prod init.funext init.logic namespace function variables {A : Type} {B : Type} {C : Type} {D : Type} {E : Type} definition compose [reducible] [unfold-full] (f : B → C) (g : A → B) : A → C := λx, f (g x) definition compose_right [reducible] [unfold-full] (f : B → B → B) (g : A → B) : B → A → B := λ b a, f b (g a) definition compose_left [reducible] [unfold-full] (f : B → B → B) (g : A → B) : A → B → B := λ a b, f (g a) b definition id [reducible] [unfold-full] (a : A) : A := a definition on_fun [reducible] [unfold-full] (f : B → B → C) (g : A → B) : A → A → C := λx y, f (g x) (g y) definition combine [reducible] [unfold-full] (f : A → B → C) (op : C → D → E) (g : A → B → D) : A → B → E := λx y, op (f x y) (g x y) definition const [reducible] [unfold-full] (B : Type) (a : A) : B → A := λx, a definition dcompose [reducible] [unfold-full] {B : A → Type} {C : Π {x : A}, B x → Type} (f : Π {x : A} (y : B x), C y) (g : Πx, B x) : Πx, C (g x) := λx, f (g x) definition swap [reducible] [unfold-full] {C : A → B → Type} (f : Πx y, C x y) : Πy x, C x y := λy x, f x y definition app [reducible] {B : A → Type} (f : Πx, B x) (x : A) : B x := f x definition curry [reducible] [unfold-full] : (A × B → C) → A → B → C := λ f a b, f (a, b) definition uncurry [reducible] [unfold 5] : (A → B → C) → (A × B → C) := λ f p, match p with (a, b) := f a b end theorem curry_uncurry (f : A → B → C) : curry (uncurry f) = f := rfl theorem uncurry_curry (f : A × B → C) : uncurry (curry f) = f := funext (λ p, match p with (a, b) := rfl end) precedence `∘'`:60 precedence `on`:1 precedence `$`:1 infixr ∘ := compose infixr ∘' := dcompose infixl on := on_fun infixr $ := app notation f `-[` op `]-` g := combine f op g lemma left_id (f : A → B) : id ∘ f = f := rfl lemma right_id (f : A → B) : f ∘ id = f := rfl theorem compose.assoc (f : C → D) (g : B → C) (h : A → B) : (f ∘ g) ∘ h = f ∘ (g ∘ h) := rfl theorem compose.left_id (f : A → B) : id ∘ f = f := rfl theorem compose.right_id (f : A → B) : f ∘ id = f := rfl theorem compose_const_right (f : B → C) (b : B) : f ∘ (const A b) = const A (f b) := rfl definition injective [reducible] (f : A → B) : Prop := ∀ ⦃a₁ a₂⦄, f a₁ = f a₂ → a₁ = a₂ theorem injective_compose {g : B → C} {f : A → B} (Hg : injective g) (Hf : injective f) : injective (g ∘ f) := take a₁ a₂, assume Heq, Hf (Hg Heq) definition surjective [reducible] (f : A → B) : Prop := ∀ b, ∃ a, f a = b theorem surjective_compose {g : B → C} {f : A → B} (Hg : surjective g) (Hf : surjective f) : surjective (g ∘ f) := take c, obtain b (Hb : g b = c), from Hg c, obtain a (Ha : f a = b), from Hf b, exists.intro a (eq.trans (congr_arg g Ha) Hb) definition bijective (f : A → B) := injective f ∧ surjective f theorem bijective_compose {g : B → C} {f : A → B} (Hg : bijective g) (Hf : bijective f) : bijective (g ∘ f) := obtain Hginj Hgsurj, from Hg, obtain Hfinj Hfsurj, from Hf, and.intro (injective_compose Hginj Hfinj) (surjective_compose Hgsurj Hfsurj) -- g is a left inverse to f definition left_inverse (g : B → A) (f : A → B) : Prop := ∀x, g (f x) = x definition id_of_left_inverse {g : B → A} {f : A → B} : left_inverse g f → g ∘ f = id := assume h, funext h definition has_left_inverse (f : A → B) : Prop := ∃ finv : B → A, left_inverse finv f -- g is a right inverse to f definition right_inverse (g : B → A) (f : A → B) : Prop := left_inverse f g definition id_of_righ_inverse {g : B → A} {f : A → B} : right_inverse g f → f ∘ g = id := assume h, funext h definition has_right_inverse (f : A → B) : Prop := ∃ finv : B → A, right_inverse finv f theorem injective_of_left_inverse {g : B → A} {f : A → B} : left_inverse g f → injective f := assume h, take a b, assume faeqfb, calc a = g (f a) : by rewrite h ... = g (f b) : faeqfb ... = b : by rewrite h theorem injective_of_has_left_inverse {f : A → B} : has_left_inverse f → injective f := assume h, obtain (finv : B → A) (inv : left_inverse finv f), from h, injective_of_left_inverse inv theorem right_inverse_of_injective_of_left_inverse {f : A → B} {g : B → A} (injf : injective f) (lfg : left_inverse f g) : right_inverse f g := take x, have H : f (g (f x)) = f x, from lfg (f x), injf H theorem surjective_of_has_right_inverse {f : A → B} : has_right_inverse f → surjective f := assume h, take b, obtain (finv : B → A) (inv : right_inverse finv f), from h, let a : A := finv b in have h : f a = b, from calc f a = (f ∘ finv) b : rfl ... = id b : by rewrite inv ... = b : rfl, exists.intro a h theorem left_inverse_of_surjective_of_right_inverse {f : A → B} {g : B → A} (surjf : surjective f) (rfg : right_inverse f g) : left_inverse f g := take y, obtain x (Hx : f x = y), from surjf y, calc f (g y) = f (g (f x)) : Hx ... = f x : rfg ... = y : Hx theorem injective_id : injective (@id A) := take a₁ a₂ H, H theorem surjective_id : surjective (@id A) := take a, exists.intro a rfl theorem bijective_id : bijective (@id A) := and.intro injective_id surjective_id end function -- copy reducible annotations to top-level export [reduce-hints] [unfold-hints] function
26956223aa4e25f636762c879d5a1ea65110555d
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/normed/group/hom_completion.lean
108b3d12f2a9b89668ba13d011223c3446536617
[ "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
10,678
lean
/- Copyright (c) 2021 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import analysis.normed.group.hom import analysis.normed.group.completion /-! # Completion of normed group homs Given two (semi) normed groups `G` and `H` and a normed group hom `f : normed_group_hom G H`, we build and study a normed group hom `f.completion : normed_group_hom (completion G) (completion H)` such that the diagram ``` f G -----------> H | | | | | | V V completion G -----------> completion H f.completion ``` commutes. The map itself comes from the general theory of completion of uniform spaces, but here we want a normed group hom, study its operator norm and kernel. The vertical maps in the above diagrams are also normed group homs constructed in this file. ## Main definitions and results: * `normed_group_hom.completion`: see the discussion above. * `normed_group.to_compl : normed_group_hom G (completion G)`: the canonical map from `G` to its completion, as a normed group hom * `normed_group_hom.completion_to_compl`: the above diagram indeed commutes. * `normed_group_hom.norm_completion`: `∥f.completion∥ = ∥f∥` * `normed_group_hom.ker_le_ker_completion`: the kernel of `f.completion` contains the image of the kernel of `f`. * `normed_group_hom.ker_completion`: the kernel of `f.completion` is the closure of the image of the kernel of `f` under an assumption that `f` is quantitatively surjective onto its image. * `normed_group_hom.extension` : if `H` is complete, the extension of `f : normed_group_hom G H` to a `normed_group_hom (completion G) H`. -/ noncomputable theory open set normed_group_hom uniform_space section completion variables {G : Type*} [semi_normed_group G] variables {H : Type*} [semi_normed_group H] variables {K : Type*} [semi_normed_group K] /-- The normed group hom induced between completions. -/ def normed_group_hom.completion (f : normed_group_hom G H) : normed_group_hom (completion G) (completion H) := { bound' := begin use ∥f∥, intro y, apply completion.induction_on y, { exact is_closed_le (continuous_norm.comp $ f.to_add_monoid_hom.continuous_completion f.continuous) (continuous_const.mul continuous_norm) }, { intro x, change ∥f.to_add_monoid_hom.completion _ ↑x∥ ≤ ∥f∥ * ∥↑x∥, rw f.to_add_monoid_hom.completion_coe f.continuous, simp only [completion.norm_coe], exact f.le_op_norm x } end, ..f.to_add_monoid_hom.completion f.continuous } lemma normed_group_hom.completion_def (f : normed_group_hom G H) (x : completion G) : f.completion x = completion.map f x := rfl @[simp] lemma normed_group_hom.completion_coe_to_fun (f : normed_group_hom G H) : (f.completion : completion G → completion H) = completion.map f := by { ext x, exact normed_group_hom.completion_def f x } @[simp] lemma normed_group_hom.completion_coe (f : normed_group_hom G H) (g : G) : f.completion g = f g := completion.map_coe f.uniform_continuous _ /-- Completion of normed group homs as a normed group hom. -/ @[simps] def normed_group_hom_completion_hom : normed_group_hom G H →+ normed_group_hom (completion G) (completion H) := { to_fun := normed_group_hom.completion, map_zero' := begin apply to_add_monoid_hom_injective, exact add_monoid_hom.completion_zero end, map_add' := λ f g, begin apply to_add_monoid_hom_injective, exact f.to_add_monoid_hom.completion_add g.to_add_monoid_hom f.continuous g.continuous, end } @[simp] lemma normed_group_hom.completion_id : (normed_group_hom.id G).completion = normed_group_hom.id (completion G) := begin ext x, rw [normed_group_hom.completion_def, normed_group_hom.coe_id, completion.map_id], refl end lemma normed_group_hom.completion_comp (f : normed_group_hom G H) (g : normed_group_hom H K) : g.completion.comp f.completion = (g.comp f).completion := begin ext x, rw [normed_group_hom.coe_comp, normed_group_hom.completion_def, normed_group_hom.completion_coe_to_fun, normed_group_hom.completion_coe_to_fun, completion.map_comp (normed_group_hom.uniform_continuous _) (normed_group_hom.uniform_continuous _)], refl end lemma normed_group_hom.completion_neg (f : normed_group_hom G H) : (-f).completion = -f.completion := normed_group_hom_completion_hom.map_neg f lemma normed_group_hom.completion_add (f g : normed_group_hom G H) : (f + g).completion = f.completion + g.completion := normed_group_hom_completion_hom.map_add f g lemma normed_group_hom.completion_sub (f g : normed_group_hom G H) : (f - g).completion = f.completion - g.completion := normed_group_hom_completion_hom.map_sub f g @[simp] lemma normed_group_hom.zero_completion : (0 : normed_group_hom G H).completion = 0 := normed_group_hom_completion_hom.map_zero /-- The map from a normed group to its completion, as a normed group hom. -/ def normed_group.to_compl : normed_group_hom G (completion G) := { to_fun := coe, map_add' := completion.to_compl.map_add, bound' := ⟨1, by simp [le_refl]⟩ } open normed_group lemma normed_group.norm_to_compl (x : G) : ∥to_compl x∥ = ∥x∥ := completion.norm_coe x lemma normed_group.dense_range_to_compl : dense_range (to_compl : G → completion G) := completion.dense_inducing_coe.dense @[simp] lemma normed_group_hom.completion_to_compl (f : normed_group_hom G H) : f.completion.comp to_compl = to_compl.comp f := begin ext x, change f.completion x = _, simpa end @[simp] lemma normed_group_hom.norm_completion (f : normed_group_hom G H) : ∥f.completion∥ = ∥f∥ := begin apply f.completion.op_norm_eq_of_bounds (norm_nonneg _), { intro x, apply completion.induction_on x, { apply is_closed_le, continuity }, { intro g, simp [f.le_op_norm g] } }, { intros N N_nonneg hN, apply f.op_norm_le_bound N_nonneg, intro x, simpa using hN x }, end lemma normed_group_hom.ker_le_ker_completion (f : normed_group_hom G H) : (to_compl.comp $ incl f.ker).range ≤ f.completion.ker := begin intros a h, replace h : ∃ y : f.ker, to_compl (y : G) = a, by simpa using h, rcases h with ⟨⟨g, g_in : g ∈ f.ker⟩, rfl⟩, rw f.mem_ker at g_in, change f.completion (g : completion G) = 0, simp [normed_group_hom.mem_ker, f.completion_coe g, g_in, completion.coe_zero], end lemma normed_group_hom.ker_completion {f : normed_group_hom G H} {C : ℝ} (h : f.surjective_on_with f.range C) : (f.completion.ker : set $ completion G) = closure (to_compl.comp $ incl f.ker).range := begin rcases h.exists_pos with ⟨C', C'_pos, hC'⟩, apply le_antisymm, { intros hatg hatg_in, rw semi_normed_group.mem_closure_iff, intros ε ε_pos, have hCf : 0 ≤ C'*∥f∥ := (zero_le_mul_left C'_pos).mpr (norm_nonneg f), have ineq : 0 < 1 + C'*∥f∥, by linarith, set δ := ε/(1 + C'*∥f∥), have δ_pos : δ > 0, from div_pos ε_pos ineq, obtain ⟨_, ⟨g : G, rfl⟩, hg : ∥hatg - g∥ < δ⟩ := semi_normed_group.mem_closure_iff.mp (completion.dense_inducing_coe.dense hatg) δ δ_pos, obtain ⟨g' : G, hgg' : f g' = f g, hfg : ∥g'∥ ≤ C' * ∥f g∥⟩ := hC' (f g) (mem_range_self g), have mem_ker : g - g' ∈ f.ker, by rw [f.mem_ker, f.map_sub, sub_eq_zero.mpr hgg'.symm], have : ∥f g∥ ≤ ∥f∥*∥hatg - g∥, calc ∥f g∥ = ∥f.completion g∥ : by rw [f.completion_coe, completion.norm_coe] ... = ∥f.completion g - 0∥ : by rw [sub_zero _] ... = ∥f.completion g - (f.completion hatg)∥ : by rw [(f.completion.mem_ker _).mp hatg_in] ... = ∥f.completion (g - hatg)∥ : by rw [f.completion.map_sub] ... ≤ ∥f.completion∥ * ∥(g :completion G) - hatg∥ : f.completion.le_op_norm _ ... = ∥f∥ * ∥hatg - g∥ : by rw [norm_sub_rev, f.norm_completion], have : ∥(g' : completion G)∥ ≤ C'*∥f∥*∥hatg - g∥, calc ∥(g' : completion G)∥ = ∥g'∥ : completion.norm_coe _ ... ≤ C' * ∥f g∥ : hfg ... ≤ C' * ∥f∥ * ∥hatg - g∥ : by { rw mul_assoc, exact (mul_le_mul_left C'_pos).mpr this }, refine ⟨g - g', _, _⟩, { norm_cast, rw normed_group_hom.comp_range, apply add_subgroup.mem_map_of_mem, simp only [incl_range, mem_ker] }, { calc ∥hatg - (g - g')∥ = ∥hatg - g + g'∥ : by abel ... ≤ ∥hatg - g∥ + ∥(g' : completion G)∥ : norm_add_le _ _ ... < δ + C'*∥f∥*∥hatg - g∥ : by linarith ... ≤ δ + C'*∥f∥*δ : add_le_add_left (mul_le_mul_of_nonneg_left hg.le hCf) δ ... = (1 + C'*∥f∥)*δ : by ring ... = ε : mul_div_cancel' _ ineq.ne.symm } }, { rw ← f.completion.is_closed_ker.closure_eq, exact closure_mono f.ker_le_ker_completion } end end completion section extension variables {G : Type*} [semi_normed_group G] variables {H : Type*} [semi_normed_group H] [separated_space H] [complete_space H] /-- If `H` is complete, the extension of `f : normed_group_hom G H` to a `normed_group_hom (completion G) H`. -/ def normed_group_hom.extension (f : normed_group_hom G H) : normed_group_hom (completion G) H := { bound' := begin refine ⟨∥f∥, λ v, completion.induction_on v (is_closed_le _ _) (λ a, _)⟩, { exact continuous.comp continuous_norm completion.continuous_extension }, { exact continuous.mul continuous_const continuous_norm }, { rw [completion.norm_coe, add_monoid_hom.to_fun_eq_coe, add_monoid_hom.extension_coe], exact le_op_norm f a } end, ..f.to_add_monoid_hom.extension f.continuous } lemma normed_group_hom.extension_def (f : normed_group_hom G H) (v : G) : f.extension v = completion.extension f v := rfl @[simp] lemma normed_group_hom.extension_coe (f : normed_group_hom G H) (v : G) : f.extension v = f v := add_monoid_hom.extension_coe _ f.continuous _ lemma normed_group_hom.extension_coe_to_fun (f : normed_group_hom G H) : (f.extension : (completion G) → H) = completion.extension f := rfl lemma normed_group_hom.extension_unique (f : normed_group_hom G H) {g : normed_group_hom (completion G) H} (hg : ∀ v, f v = g v) : f.extension = g := begin ext v, rw [normed_group_hom.extension_coe_to_fun, completion.extension_unique f.uniform_continuous g.uniform_continuous (λ a, hg a)] end end extension
859f3d455cf2d3950e634602a9a1b1d898425758
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/algebra/filter_basis.lean
8f9664a67dbdad297fbb16049e04c7ecac944e15
[ "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
17,298
lean
/- Copyright (c) 2021 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import order.filter.bases import topology.algebra.module.basic /-! # Group and ring filter bases > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. A `group_filter_basis` is a `filter_basis` on a group with some properties relating the basis to the group structure. The main theorem is that a `group_filter_basis` on a group gives a topology on the group which makes it into a topological group with neighborhoods of the neutral element generated by the given basis. ## Main definitions and results Given a group `G` and a ring `R`: * `group_filter_basis G`: the type of filter bases that will become neighborhood of `1` for a topology on `G` compatible with the group structure * `group_filter_basis.topology`: the associated topology * `group_filter_basis.is_topological_group`: the compatibility between the above topology and the group structure * `ring_filter_basis R`: the type of filter bases that will become neighborhood of `0` for a topology on `R` compatible with the ring structure * `ring_filter_basis.topology`: the associated topology * `ring_filter_basis.is_topological_ring`: the compatibility between the above topology and the ring structure ## References * [N. Bourbaki, *General Topology*][bourbaki1966] -/ open filter set topological_space function open_locale topology filter pointwise universe u /-- A `group_filter_basis` on a group is a `filter_basis` satisfying some additional axioms. Example : if `G` is a topological group then the neighbourhoods of the identity are a `group_filter_basis`. Conversely given a `group_filter_basis` one can define a topology compatible with the group structure on `G`. -/ class group_filter_basis (G : Type u) [group G] extends filter_basis G := (one' : ∀ {U}, U ∈ sets → (1 : G) ∈ U) (mul' : ∀ {U}, U ∈ sets → ∃ V ∈ sets, V * V ⊆ U) (inv' : ∀ {U}, U ∈ sets → ∃ V ∈ sets, V ⊆ (λ x, x⁻¹) ⁻¹' U) (conj' : ∀ x₀, ∀ {U}, U ∈ sets → ∃ V ∈ sets, V ⊆ (λ x, x₀*x*x₀⁻¹) ⁻¹' U) /-- A `add_group_filter_basis` on an additive group is a `filter_basis` satisfying some additional axioms. Example : if `G` is a topological group then the neighbourhoods of the identity are a `add_group_filter_basis`. Conversely given a `add_group_filter_basis` one can define a topology compatible with the group structure on `G`. -/ class add_group_filter_basis (A : Type u) [add_group A] extends filter_basis A := (zero' : ∀ {U}, U ∈ sets → (0 : A) ∈ U) (add' : ∀ {U}, U ∈ sets → ∃ V ∈ sets, V + V ⊆ U) (neg' : ∀ {U}, U ∈ sets → ∃ V ∈ sets, V ⊆ (λ x, -x) ⁻¹' U) (conj' : ∀ x₀, ∀ {U}, U ∈ sets → ∃ V ∈ sets, V ⊆ (λ x, x₀+x+-x₀) ⁻¹' U) attribute [to_additive] group_filter_basis attribute [to_additive] group_filter_basis.one' attribute [to_additive] group_filter_basis.mul' attribute [to_additive] group_filter_basis.inv' attribute [to_additive] group_filter_basis.conj' attribute [to_additive] group_filter_basis.to_filter_basis /-- `group_filter_basis` constructor in the commutative group case. -/ @[to_additive "`add_group_filter_basis` constructor in the additive commutative group case."] def group_filter_basis_of_comm {G : Type*} [comm_group G] (sets : set (set G)) (nonempty : sets.nonempty) (inter_sets : ∀ (x y), x ∈ sets → y ∈ sets → ∃ z ∈ sets, z ⊆ x ∩ y) (one : ∀ U ∈ sets, (1 : G) ∈ U) (mul : ∀ U ∈ sets, ∃ V ∈ sets, V * V ⊆ U) (inv : ∀ U ∈ sets, ∃ V ∈ sets, V ⊆ (λ x, x⁻¹) ⁻¹' U) : group_filter_basis G := { sets := sets, nonempty := nonempty, inter_sets := inter_sets, one' := one, mul' := mul, inv' := inv, conj' := λ x U U_in, ⟨U, U_in, by simp⟩ } namespace group_filter_basis variables {G : Type u} [group G] {B : group_filter_basis G} @[to_additive] instance : has_mem (set G) (group_filter_basis G) := ⟨λ s f, s ∈ f.sets⟩ @[to_additive] lemma one {U : set G} : U ∈ B → (1 : G) ∈ U := group_filter_basis.one' @[to_additive] lemma mul {U : set G} : U ∈ B → ∃ V ∈ B, V*V ⊆ U := group_filter_basis.mul' @[to_additive] lemma inv {U : set G} : U ∈ B → ∃ V ∈ B, V ⊆ (λ x, x⁻¹) ⁻¹' U := group_filter_basis.inv' @[to_additive] lemma conj : ∀ x₀, ∀ {U}, U ∈ B → ∃ V ∈ B, V ⊆ (λ x, x₀*x*x₀⁻¹) ⁻¹' U := group_filter_basis.conj' /-- The trivial group filter basis consists of `{1}` only. The associated topology is discrete. -/ @[to_additive "The trivial additive group filter basis consists of `{0}` only. The associated topology is discrete."] instance : inhabited (group_filter_basis G) := ⟨begin refine { sets := {{1}}, nonempty := singleton_nonempty _, .. }, all_goals { simp only [exists_prop, mem_singleton_iff] }, { rintros - - rfl rfl, use {1}, simp }, { simp }, { rintro - rfl, use {1}, simp }, { rintro - rfl, use {1}, simp }, { rintro x₀ - rfl, use {1}, simp } end⟩ @[to_additive] lemma prod_subset_self (B : group_filter_basis G) {U : set G} (h : U ∈ B) : U ⊆ U * U := λ x x_in, ⟨1, x, one h, x_in, one_mul x⟩ /-- The neighborhood function of a `group_filter_basis` -/ @[to_additive "The neighborhood function of a `add_group_filter_basis`"] def N (B : group_filter_basis G) : G → filter G := λ x, map (λ y, x*y) B.to_filter_basis.filter @[simp, to_additive] lemma N_one (B : group_filter_basis G) : B.N 1 = B.to_filter_basis.filter := by simp only [N, one_mul, map_id'] @[to_additive] protected lemma has_basis (B : group_filter_basis G) (x : G) : has_basis (B.N x) (λ V : set G, V ∈ B) (λ V, (λ y, x*y) '' V) := has_basis.map (λ y, x * y) to_filter_basis.has_basis /-- The topological space structure coming from a group filter basis. -/ @[to_additive "The topological space structure coming from an additive group filter basis."] def topology (B : group_filter_basis G) : topological_space G := topological_space.mk_of_nhds B.N @[to_additive] lemma nhds_eq (B : group_filter_basis G) {x₀ : G} : @nhds G (B.topology) x₀ = B.N x₀ := begin rw [topological_space.nhds_mk_of_nhds], { intros x U U_in, rw (B.has_basis x).mem_iff at U_in, rcases U_in with ⟨V, V_in, H⟩, simpa [mem_pure] using H (mem_image_of_mem _ (group_filter_basis.one V_in)), }, { intros x U U_in, rw (B.has_basis x).mem_iff at U_in, rcases U_in with ⟨V, V_in, H⟩, rcases group_filter_basis.mul V_in with ⟨W, W_in, hW⟩, use [(λ y, x*y) '' W, image_mem_map (filter_basis.mem_filter_of_mem _ W_in)], split, { rw image_subset_iff at H ⊢, exact ((B.prod_subset_self W_in).trans hW).trans H }, { rintros y ⟨t, tW, rfl⟩, rw (B.has_basis _).mem_iff, use [W, W_in], apply subset.trans _ H, clear H, rintros z ⟨w, wW, rfl⟩, exact ⟨t*w, hW (mul_mem_mul tW wW), by simp [mul_assoc]⟩ } } end @[to_additive] lemma nhds_one_eq (B : group_filter_basis G) : @nhds G (B.topology) (1 : G) = B.to_filter_basis.filter := by { rw B.nhds_eq, simp only [N, one_mul], exact map_id } @[to_additive] lemma nhds_has_basis (B : group_filter_basis G) (x₀ : G) : has_basis (@nhds G B.topology x₀) (λ V : set G, V ∈ B) (λ V, (λ y, x₀ * y) '' V) := by { rw B.nhds_eq, apply B.has_basis } @[to_additive] lemma nhds_one_has_basis (B : group_filter_basis G) : has_basis (@nhds G B.topology 1) (λ V : set G, V ∈ B) id := by { rw B.nhds_one_eq, exact B.to_filter_basis.has_basis } @[to_additive] lemma mem_nhds_one (B : group_filter_basis G) {U : set G} (hU : U ∈ B) : U ∈ @nhds G B.topology 1 := begin rw B.nhds_one_has_basis.mem_iff, exact ⟨U, hU, rfl.subset⟩ end /-- If a group is endowed with a topological structure coming from a group filter basis then it's a topological group. -/ @[to_additive "If a group is endowed with a topological structure coming from a group filter basis then it's a topological group.", priority 100] -- See note [lower instance priority] instance is_topological_group (B : group_filter_basis G) : @topological_group G B.topology _ := begin letI := B.topology, have basis := B.nhds_one_has_basis, have basis' := basis.prod basis, refine topological_group.of_nhds_one _ _ _ _, { rw basis'.tendsto_iff basis, suffices : ∀ U ∈ B, ∃ V W, (V ∈ B ∧ W ∈ B) ∧ ∀ a b, a ∈ V → b ∈ W → a * b ∈ U, by simpa, intros U U_in, rcases mul U_in with ⟨V, V_in, hV⟩, use [V, V, V_in, V_in], intros a b a_in b_in, exact hV ⟨a, b, a_in, b_in, rfl⟩ }, { rw basis.tendsto_iff basis, intros U U_in, simpa using inv U_in }, { intro x₀, rw [nhds_eq, nhds_one_eq], refl }, { intro x₀, rw basis.tendsto_iff basis, intros U U_in, exact conj x₀ U_in } end end group_filter_basis /-- A `ring_filter_basis` on a ring is a `filter_basis` satisfying some additional axioms. Example : if `R` is a topological ring then the neighbourhoods of the identity are a `ring_filter_basis`. Conversely given a `ring_filter_basis` on a ring `R`, one can define a topology on `R` which is compatible with the ring structure. -/ class ring_filter_basis (R : Type u) [ring R] extends add_group_filter_basis R := (mul' : ∀ {U}, U ∈ sets → ∃ V ∈ sets, V * V ⊆ U) (mul_left' : ∀ (x₀ : R) {U}, U ∈ sets → ∃ V ∈ sets, V ⊆ (λ x, x₀*x) ⁻¹' U) (mul_right' : ∀ (x₀ : R) {U}, U ∈ sets → ∃ V ∈ sets, V ⊆ (λ x, x*x₀) ⁻¹' U) namespace ring_filter_basis variables {R : Type u} [ring R] (B : ring_filter_basis R) instance : has_mem (set R) (ring_filter_basis R) := ⟨λ s B, s ∈ B.sets⟩ lemma mul {U : set R} (hU : U ∈ B) : ∃ V ∈ B, V * V ⊆ U := mul' hU lemma mul_left (x₀ : R) {U : set R} (hU : U ∈ B) : ∃ V ∈ B, V ⊆ (λ x, x₀*x) ⁻¹' U := mul_left' x₀ hU lemma mul_right (x₀ : R) {U : set R} (hU : U ∈ B) : ∃ V ∈ B, V ⊆ (λ x, x*x₀) ⁻¹' U := mul_right' x₀ hU /-- The topology associated to a ring filter basis. It has the given basis as a basis of neighborhoods of zero. -/ def topology : topological_space R := B.to_add_group_filter_basis.topology /-- If a ring is endowed with a topological structure coming from a ring filter basis then it's a topological ring. -/ @[priority 100] instance is_topological_ring {R : Type u} [ring R] (B : ring_filter_basis R) : @topological_ring R B.topology _ := begin let B' := B.to_add_group_filter_basis, letI := B'.topology, have basis := B'.nhds_zero_has_basis, have basis' := basis.prod basis, haveI := B'.is_topological_add_group, apply topological_ring.of_add_group_of_nhds_zero, { rw basis'.tendsto_iff basis, suffices : ∀ U ∈ B', ∃ V W, (V ∈ B' ∧ W ∈ B') ∧ ∀ a b, a ∈ V → b ∈ W → a * b ∈ U, by simpa, intros U U_in, rcases B.mul U_in with ⟨V, V_in, hV⟩, use [V, V, V_in, V_in], intros a b a_in b_in, exact hV ⟨a, b, a_in, b_in, rfl⟩ }, { intro x₀, rw basis.tendsto_iff basis, intros U, simpa using B.mul_left x₀ }, { intro x₀, rw basis.tendsto_iff basis, intros U, simpa using B.mul_right x₀ }, end end ring_filter_basis /-- A `module_filter_basis` on a module is a `filter_basis` satisfying some additional axioms. Example : if `M` is a topological module then the neighbourhoods of zero are a `module_filter_basis`. Conversely given a `module_filter_basis` one can define a topology compatible with the module structure on `M`. -/ structure module_filter_basis (R M : Type*) [comm_ring R] [topological_space R] [add_comm_group M] [module R M] extends add_group_filter_basis M := (smul' : ∀ {U}, U ∈ sets → ∃ (V ∈ 𝓝 (0 : R)) (W ∈ sets), V • W ⊆ U) (smul_left' : ∀ (x₀ : R) {U}, U ∈ sets → ∃ V ∈ sets, V ⊆ (λ x, x₀ • x) ⁻¹' U) (smul_right' : ∀ (m₀ : M) {U}, U ∈ sets → ∀ᶠ x in 𝓝 (0 : R), x • m₀ ∈ U) namespace module_filter_basis variables {R M : Type*} [comm_ring R] [topological_space R] [add_comm_group M] [module R M] (B : module_filter_basis R M) instance group_filter_basis.has_mem : has_mem (set M) (module_filter_basis R M) := ⟨λ s B, s ∈ B.sets⟩ lemma smul {U : set M} (hU : U ∈ B) : ∃ (V ∈ 𝓝 (0 : R)) (W ∈ B), V • W ⊆ U := B.smul' hU lemma smul_left (x₀ : R) {U : set M} (hU : U ∈ B) : ∃ V ∈ B, V ⊆ (λ x, x₀ • x) ⁻¹' U := B.smul_left' x₀ hU lemma smul_right (m₀ : M) {U : set M} (hU : U ∈ B) : ∀ᶠ x in 𝓝 (0 : R), x • m₀ ∈ U := B.smul_right' m₀ hU /-- If `R` is discrete then the trivial additive group filter basis on any `R`-module is a module filter basis. -/ instance [discrete_topology R] : inhabited (module_filter_basis R M) := ⟨{ smul' := begin rintro U (h : U ∈ {{(0 : M)}}), rw mem_singleton_iff at h, use [univ, univ_mem, {0}, rfl], rintros a ⟨x, m, -, hm, rfl⟩, simp [mem_singleton_iff.1 hm, h] end, smul_left' := begin rintro x₀ U (h : U ∈ {{(0 : M)}}), rw mem_singleton_iff at h, use [{0}, rfl], simp [h] end, smul_right' := begin rintro m₀ U (h : U ∈ (0 : set (set M))), rw set.mem_zero at h, simp [h, nhds_discrete] end, ..show add_group_filter_basis M, from default }⟩ /-- The topology associated to a module filter basis on a module over a topological ring. It has the given basis as a basis of neighborhoods of zero. -/ def topology : topological_space M := B.to_add_group_filter_basis.topology /-- The topology associated to a module filter basis on a module over a topological ring. It has the given basis as a basis of neighborhoods of zero. This version gets the ring topology by unification instead of type class inference. -/ def topology' {R M : Type*} [comm_ring R] {tR : topological_space R} [add_comm_group M] [module R M] (B : module_filter_basis R M) : topological_space M := B.to_add_group_filter_basis.topology /-- A topological add group whith a basis of `𝓝 0` satisfying the axioms of `module_filter_basis` is a topological module. This lemma is mathematically useless because one could obtain such a result by applying `module_filter_basis.has_continuous_smul` and use the fact that group topologies are characterized by their neighborhoods of 0 to obtain the `has_continuous_smul` on the pre-existing topology. But it turns out it's just easier to get it as a biproduct of the proof, so this is just a free quality-of-life improvement. -/ lemma _root_.has_continuous_smul.of_basis_zero {ι : Type*} [topological_ring R] [topological_space M] [topological_add_group M] {p : ι → Prop} {b : ι → set M} (h : has_basis (𝓝 0) p b) (hsmul : ∀ {i}, p i → ∃ (V ∈ 𝓝 (0 : R)) j (hj : p j), V • (b j) ⊆ b i) (hsmul_left : ∀ (x₀ : R) {i}, p i → ∃ j (hj : p j), (b j) ⊆ (λ x, x₀ • x) ⁻¹' (b i)) (hsmul_right : ∀ (m₀ : M) {i}, p i → ∀ᶠ x in 𝓝 (0 : R), x • m₀ ∈ (b i)) : has_continuous_smul R M := begin apply has_continuous_smul.of_nhds_zero, { rw h.tendsto_right_iff, intros i hi, rcases hsmul hi with ⟨V, V_in, j, hj, hVj⟩, apply mem_of_superset (prod_mem_prod V_in $ h.mem_of_mem hj), rintros ⟨v, w⟩ ⟨v_in : v ∈ V, w_in : w ∈ (b j)⟩, exact hVj (set.smul_mem_smul v_in w_in) }, { intro m₀, rw h.tendsto_right_iff, intros i hi, exact hsmul_right m₀ hi }, { intro x₀, rw h.tendsto_right_iff, intros i hi, rcases hsmul_left x₀ hi with ⟨j, hj, hji⟩, exact mem_of_superset (h.mem_of_mem hj) hji }, end /-- If a module is endowed with a topological structure coming from a module filter basis then it's a topological module. -/ @[priority 100] instance has_continuous_smul [topological_ring R] : @has_continuous_smul R M _ _ B.topology := begin let B' := B.to_add_group_filter_basis, letI := B'.topology, haveI := B'.is_topological_add_group, exact has_continuous_smul.of_basis_zero B'.nhds_zero_has_basis (λ _, B.smul) B.smul_left B.smul_right, end /-- Build a module filter basis from compatible ring and additive group filter bases. -/ def of_bases {R M : Type*} [comm_ring R] [add_comm_group M] [module R M] (BR : ring_filter_basis R) (BM : add_group_filter_basis M) (smul : ∀ {U}, U ∈ BM → ∃ (V ∈ BR) (W ∈ BM), V • W ⊆ U) (smul_left : ∀ (x₀ : R) {U}, U ∈ BM → ∃ V ∈ BM, V ⊆ (λ x, x₀ • x) ⁻¹' U) (smul_right : ∀ (m₀ : M) {U}, U ∈ BM → ∃ V ∈ BR, V ⊆ (λ x, x • m₀) ⁻¹' U) : @module_filter_basis R M _ BR.topology _ _ := { smul' := begin intros U U_in, rcases smul U_in with ⟨V, V_in, W, W_in, H⟩, exact ⟨V, BR.to_add_group_filter_basis.mem_nhds_zero V_in, W, W_in, H⟩ end, smul_left' := smul_left, smul_right' := begin intros m₀ U U_in, rcases smul_right m₀ U_in with ⟨V, V_in, H⟩, exact mem_of_superset (BR.to_add_group_filter_basis.mem_nhds_zero V_in) H end, ..BM } end module_filter_basis
e7e66de9db47bbcae4139140888618f7d287880e
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/data/polynomial/mirror.lean
1b99ce529b7ffed6452cd133d15cd251eec38a70
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,357
lean
/- Copyright (c) 2020 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import data.polynomial.ring_division /-! # "Mirror" of a univariate polynomial In this file we define `polynomial.mirror`, a variant of `polynomial.reverse`. The difference between `reverse` and `mirror` is that `reverse` will decrease the degree if the polynomial is divisible by `X`. We also define `polynomial.norm2`, which is the sum of the squares of the coefficients of a polynomial. It is also a coefficient of `p * p.mirror`. ## Main definitions - `polynomial.mirror` - `polynomial.norm2` ## Main results - `polynomial.mirror_mul_of_domain`: `mirror` preserves multiplication. - `polynomial.irreducible_of_mirror`: an irreducibility criterion involving `mirror` - `polynomial.norm2_eq_mul_reverse_coeff`: `norm2` is a coefficient of `p * p.mirror` -/ namespace polynomial variables {R : Type*} [semiring R] (p : polynomial R) section mirror /-- mirror of a polynomial: reverses the coefficients while preserving `polynomial.nat_degree` -/ noncomputable def mirror := p.reverse * X ^ p.nat_trailing_degree @[simp] lemma mirror_zero : (0 : polynomial R).mirror = 0 := by simp [mirror] lemma mirror_monomial (n : ℕ) (a : R) : (monomial n a).mirror = (monomial n a) := begin by_cases ha : a = 0, { rw [ha, monomial_zero_right, mirror_zero] }, { rw [mirror, reverse, nat_degree_monomial n a ha, nat_trailing_degree_monomial ha, ←C_mul_X_pow_eq_monomial, reflect_C_mul_X_pow, rev_at_le (le_refl n), nat.sub_self, pow_zero, mul_one] }, end lemma mirror_C (a : R) : (C a).mirror = C a := mirror_monomial 0 a lemma mirror_X : X.mirror = (X : polynomial R) := mirror_monomial 1 (1 : R) lemma mirror_nat_degree : p.mirror.nat_degree = p.nat_degree := begin by_cases hp : p = 0, { rw [hp, mirror_zero] }, by_cases hR : nontrivial R, { haveI := hR, rw [mirror, nat_degree_mul', reverse_nat_degree, nat_degree_X_pow, nat.sub_add_cancel p.nat_trailing_degree_le_nat_degree], rwa [leading_coeff_X_pow, mul_one, reverse_leading_coeff, ne, trailing_coeff_eq_zero] }, { haveI := not_nontrivial_iff_subsingleton.mp hR, exact congr_arg nat_degree (subsingleton.elim p.mirror p) }, end lemma mirror_nat_trailing_degree : p.mirror.nat_trailing_degree = p.nat_trailing_degree := begin by_cases hp : p = 0, { rw [hp, mirror_zero] }, { rw [mirror, nat_trailing_degree_mul_X_pow ((mt reverse_eq_zero.mp) hp), reverse_nat_trailing_degree, zero_add] }, end lemma coeff_mirror (n : ℕ) : p.mirror.coeff n = p.coeff (rev_at (p.nat_degree + p.nat_trailing_degree) n) := begin by_cases h2 : p.nat_degree < n, { rw [coeff_eq_zero_of_nat_degree_lt (by rwa mirror_nat_degree)], by_cases h1 : n ≤ p.nat_degree + p.nat_trailing_degree, { rw [rev_at_le h1, coeff_eq_zero_of_lt_nat_trailing_degree], exact (sub_lt_iff_left h1).mpr (nat.add_lt_add_right h2 _) }, { rw [←rev_at_fun_eq, rev_at_fun, if_neg h1, coeff_eq_zero_of_nat_degree_lt h2] } }, rw not_lt at h2, rw [rev_at_le (h2.trans (nat.le_add_right _ _))], by_cases h3 : p.nat_trailing_degree ≤ n, { rw [←sub_add_eq_add_sub' h2, ←sub_sub_assoc h2 h3, mirror, coeff_mul_X_pow', if_pos h3, coeff_reverse, rev_at_le (sub_le_self'.trans h2)] }, rw not_le at h3, rw coeff_eq_zero_of_nat_degree_lt (lt_sub_iff_right.mpr (nat.add_lt_add_left h3 _)), exact coeff_eq_zero_of_lt_nat_trailing_degree (by rwa mirror_nat_trailing_degree), end --TODO: Extract `finset.sum_range_rev_at` lemma. lemma mirror_eval_one : p.mirror.eval 1 = p.eval 1 := begin simp_rw [eval_eq_finset_sum, one_pow, mul_one, mirror_nat_degree], refine finset.sum_bij_ne_zero _ _ _ _ _, { exact λ n hn hp, rev_at (p.nat_degree + p.nat_trailing_degree) n }, { intros n hn hp, rw finset.mem_range_succ_iff at *, rw rev_at_le (hn.trans (nat.le_add_right _ _)), rw [sub_le_iff_sub_le, add_comm, nat.add_sub_cancel, ←mirror_nat_trailing_degree], exact nat_trailing_degree_le_of_ne_zero hp }, { exact λ n₁ n₂ hn₁ hp₁ hn₂ hp₂ h, by rw [←@rev_at_invol _ n₁, h, rev_at_invol] }, { intros n hn hp, use rev_at (p.nat_degree + p.nat_trailing_degree) n, refine ⟨_, _, rev_at_invol.symm⟩, { rw finset.mem_range_succ_iff at *, rw rev_at_le (hn.trans (nat.le_add_right _ _)), rw [sub_le_iff_sub_le, add_comm, nat.add_sub_cancel], exact nat_trailing_degree_le_of_ne_zero hp }, { change p.mirror.coeff _ ≠ 0, rwa [coeff_mirror, rev_at_invol] } }, { exact λ n hn hp, p.coeff_mirror n }, end lemma mirror_mirror : p.mirror.mirror = p := polynomial.ext (λ n, by rw [coeff_mirror, coeff_mirror, mirror_nat_degree, mirror_nat_trailing_degree, rev_at_invol]) lemma mirror_eq_zero : p.mirror = 0 ↔ p = 0 := ⟨λ h, by rw [←p.mirror_mirror, h, mirror_zero], λ h, by rw [h, mirror_zero]⟩ lemma mirror_trailing_coeff : p.mirror.trailing_coeff = p.leading_coeff := by rw [leading_coeff, trailing_coeff, mirror_nat_trailing_degree, coeff_mirror, rev_at_le (nat.le_add_left _ _), nat.add_sub_cancel] lemma mirror_leading_coeff : p.mirror.leading_coeff = p.trailing_coeff := by rw [←p.mirror_mirror, mirror_trailing_coeff, p.mirror_mirror] lemma mirror_mul_of_domain {R : Type*} [comm_ring R] [integral_domain R] (p q : polynomial R) : (p * q).mirror = p.mirror * q.mirror := begin by_cases hp : p = 0, { rw [hp, zero_mul, mirror_zero, zero_mul] }, by_cases hq : q = 0, { rw [hq, mul_zero, mirror_zero, mul_zero] }, rw [mirror, mirror, mirror, reverse_mul_of_domain, nat_trailing_degree_mul hp hq, pow_add], ring, end lemma mirror_smul {R : Type*} [comm_ring R] [integral_domain R] (p : polynomial R) (a : R) : (a • p).mirror = a • p.mirror := by rw [←C_mul', ←C_mul', mirror_mul_of_domain, mirror_C] lemma mirror_neg {R : Type*} [ring R] (p : polynomial R) : (-p).mirror = -(p.mirror) := by rw [mirror, mirror, reverse_neg, nat_trailing_degree_neg, neg_mul_eq_neg_mul] lemma irreducible_of_mirror {R : Type*} [comm_ring R] [integral_domain R] {f : polynomial R} (h1 : ¬ is_unit f) (h2 : ∀ k, f * f.mirror = k * k.mirror → k = f ∨ k = -f ∨ k = f.mirror ∨ k = -f.mirror) (h3 : ∀ g, g ∣ f → g ∣ f.mirror → is_unit g) : irreducible f := begin split, { exact h1 }, { intros g h fgh, let k := g * h.mirror, have key : f * f.mirror = k * k.mirror, { rw [fgh, mirror_mul_of_domain, mirror_mul_of_domain, mirror_mirror, mul_assoc, mul_comm h, mul_comm g.mirror, mul_assoc, ←mul_assoc] }, have g_dvd_f : g ∣ f, { rw fgh, exact dvd_mul_right g h }, have h_dvd_f : h ∣ f, { rw fgh, exact dvd_mul_left h g }, have g_dvd_k : g ∣ k, { exact dvd_mul_right g h.mirror }, have h_dvd_k_rev : h ∣ k.mirror, { rw [mirror_mul_of_domain, mirror_mirror], exact dvd_mul_left h g.mirror }, have hk := h2 k key, rcases hk with hk | hk | hk | hk, { exact or.inr (h3 h h_dvd_f (by rwa ← hk)) }, { exact or.inr (h3 h h_dvd_f (by rwa [eq_neg_iff_eq_neg.mp hk, mirror_neg, dvd_neg])) }, { exact or.inl (h3 g g_dvd_f (by rwa ← hk)) }, { exact or.inl (h3 g g_dvd_f (by rwa [eq_neg_iff_eq_neg.mp hk, dvd_neg])) } }, end end mirror end polynomial
b9d636d8378925b62a8b0e3a909b723c2f0e73b8
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/archive/100-theorems-list/82_cubing_a_cube.lean
c7aa854daadf338089c349e457e961fe8b53d652
[ "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
23,648
lean
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import data.real.basic import data.set.intervals import data.set.pairwise import set_theory.cardinal /-! Proof that a cube (in dimension n ≥ 3) cannot be cubed: There does not exist a partition of a cube into finitely many smaller cubes (at least two) of different sizes. We follow the proof described here: http://www.alaricstephen.com/main-featured/2017/9/28/cubing-a-cube-proof -/ open real set function fin open_locale cardinal noncomputable theory variable {n : ℕ} /-- Given three intervals `I, J, K` such that `J ⊂ I`, neither endpoint of `J` coincides with an endpoint of `I`, `¬ (K ⊆ J)` and `K` does not lie completely to the left nor completely to the right of `J`. Then `I ∩ K \ J` is nonempty. -/ lemma Ico_lemma {α} [linear_order α] {x₁ x₂ y₁ y₂ z₁ z₂ w : α} (h₁ : x₁ < y₁) (hy : y₁ < y₂) (h₂ : y₂ < x₂) (hz₁ : z₁ ≤ y₂) (hz₂ : y₁ ≤ z₂) (hw : w ∉ Ico y₁ y₂ ∧ w ∈ Ico z₁ z₂) : ∃w, w ∈ Ico x₁ x₂ ∧ w ∉ Ico y₁ y₂ ∧ w ∈ Ico z₁ z₂ := begin simp only [not_and, not_lt, mem_Ico] at hw, refine ⟨max x₁ (min w y₂), _, _, _⟩, { simp [le_refl, lt_trans h₁ (lt_trans hy h₂), h₂] }, { simp [hw, lt_irrefl, not_le_of_lt h₁] {contextual := tt} }, { simp [hw.2.1, hw.2.2, hz₁, lt_of_lt_of_le h₁ hz₂] at ⊢ } end /-- A (hyper)-cube (in standard orientation) is a vector `b` consisting of the bottom-left point of the cube, a width `w` and a proof that `w > 0`. We use functions from `fin n` to denote vectors. -/ structure cube (n : ℕ) : Type := (b : fin n → ℝ) -- bottom-left coordinate (w : ℝ) -- width (hw : 0 < w) namespace cube lemma hw' (c : cube n) : 0 ≤ c.w := le_of_lt c.hw /-- The j-th side of a cube is the half-open interval `[b j, b j + w)` -/ def side (c : cube n) (j : fin n) : set ℝ := Ico (c.b j) (c.b j + c.w) @[simp] lemma b_mem_side (c : cube n) (j : fin n) : c.b j ∈ c.side j := by simp [side, cube.hw, le_refl] def to_set (c : cube n) : set (fin n → ℝ) := { x | ∀j, x j ∈ side c j } def to_set_subset {c c' : cube n} : c.to_set ⊆ c'.to_set ↔ ∀j, c.side j ⊆ c'.side j := begin split, intros h j x hx, let f : fin n → ℝ := λ j', if j' = j then x else c.b j', have : f ∈ c.to_set, { intro j', by_cases hj' : j' = j; simp [f, hj', if_pos, if_neg, hx] }, convert h this j, { simp [f, if_pos] }, intros h f hf j, exact h j (hf j) end def to_set_disjoint {c c' : cube n} : disjoint c.to_set c'.to_set ↔ ∃j, disjoint (c.side j) (c'.side j) := begin split, intros h, classical, by_contra h', simp only [not_disjoint_iff, classical.skolem, not_exists] at h', cases h' with f hf, apply not_disjoint_iff.mpr ⟨f, _, _⟩ h; intro j, exact (hf j).1, exact (hf j).2, rintro ⟨j, hj⟩, rw [set.disjoint_iff], rintros f ⟨h1f, h2f⟩, apply not_disjoint_iff.mpr ⟨f j, h1f j, h2f j⟩ hj end lemma b_mem_to_set (c : cube n) : c.b ∈ c.to_set := by simp [to_set] protected def tail (c : cube (n+1)) : cube n := ⟨tail c.b, c.w, c.hw⟩ lemma side_tail (c : cube (n+1)) (j : fin n) : c.tail.side j = c.side j.succ := rfl def bottom (c : cube (n+1)) : set (fin (n+1) → ℝ) := { x | x 0 = c.b 0 ∧ tail x ∈ c.tail.to_set } lemma b_mem_bottom (c : cube (n+1)) : c.b ∈ c.bottom := by simp [bottom, to_set, side, cube.hw, le_refl, cube.tail] def xm (c : cube (n+1)) : ℝ := c.b 0 + c.w lemma b_lt_xm (c : cube (n+1)) : c.b 0 < c.xm := by simp [xm, hw] lemma b_ne_xm (c : cube (n+1)) : c.b 0 ≠ c.xm := ne_of_lt c.b_lt_xm def shift_up (c : cube (n+1)) : cube (n+1) := ⟨cons c.xm $ tail c.b, c.w, c.hw⟩ @[simp] lemma tail_shift_up (c : cube (n+1)) : c.shift_up.tail = c.tail := by simp [shift_up, cube.tail] @[simp] lemma head_shift_up (c : cube (n+1)) : c.shift_up.b 0 = c.xm := rfl def unit_cube : cube n := ⟨λ _, 0, 1, by norm_num⟩ @[simp] lemma side_unit_cube {j : fin n} : unit_cube.side j = Ico 0 1 := by norm_num [unit_cube, side] end cube open cube variables {ι : Type} [fintype ι] {cs : ι → cube (n+1)} {i i' : ι} /-- A finite family of (at least 2) cubes partitioning the unit cube with different sizes -/ def correct (cs : ι → cube n) : Prop := pairwise (disjoint on (cube.to_set ∘ cs)) ∧ (⋃(i : ι), (cs i).to_set) = unit_cube.to_set ∧ injective (cube.w ∘ cs) ∧ 2 ≤ #ι ∧ 3 ≤ n variable (h : correct cs) include h lemma to_set_subset_unit_cube {i} : (cs i).to_set ⊆ unit_cube.to_set := by { rw [←h.2.1], exact subset_Union _ i } lemma side_subset {i j} : (cs i).side j ⊆ Ico 0 1 := by { have := to_set_subset_unit_cube h, rw [to_set_subset] at this, convert this j, norm_num [unit_cube] } lemma zero_le_of_mem_side {i j x} (hx : x ∈ (cs i).side j) : 0 ≤ x := (side_subset h hx).1 lemma zero_le_of_mem {i p} (hp : p ∈ (cs i).to_set) (j) : 0 ≤ p j := zero_le_of_mem_side h (hp j) lemma zero_le_b {i j} : 0 ≤ (cs i).b j := zero_le_of_mem h (cs i).b_mem_to_set j lemma b_add_w_le_one {j} : (cs i).b j + (cs i).w ≤ 1 := by { have := side_subset h, rw [side, Ico_subset_Ico_iff] at this, convert this.2, simp [hw] } /-- The width of any cube in the partition cannot be 1. -/ lemma w_ne_one (i : ι) : (cs i).w ≠ 1 := begin intro hi, have := h.2.2.2.1, rw [cardinal.two_le_iff' i] at this, cases this with i' hi', let p := (cs i').b, have hp : p ∈ (cs i').to_set := (cs i').b_mem_to_set, have h2p : p ∈ (cs i).to_set, { intro j, split, transitivity (0 : ℝ), { rw [←add_le_add_iff_right (1 : ℝ)], convert b_add_w_le_one h, rw hi, rw zero_add }, apply zero_le_b h, apply lt_of_lt_of_le (side_subset h $ (cs i').b_mem_side j).2, simp [hi, zero_le_b h] }, apply not_disjoint_iff.mpr ⟨p, hp, h2p⟩, apply h.1, exact hi'.symm end /-- The top of a cube (which is the bottom of the cube shifted up by its width) must be covered by bottoms of (other) cubes in the family. -/ lemma shift_up_bottom_subset_bottoms (hc : (cs i).xm ≠ 1) : (cs i).shift_up.bottom ⊆ ⋃(i : ι), (cs i).bottom := begin intros p hp, cases hp with hp0 hps, rw [tail_shift_up] at hps, have : p ∈ (unit_cube : cube (n+1)).to_set, { simp only [to_set, forall_fin_succ, hp0, side_unit_cube, mem_set_of_eq, mem_Ico, head_shift_up], refine ⟨⟨_, _⟩, _⟩, { rw [←zero_add (0 : ℝ)], apply add_le_add, apply zero_le_b h, apply (cs i).hw' }, { exact lt_of_le_of_ne (b_add_w_le_one h) hc }, intro j, exact side_subset h (hps j) }, rw [←h.2.1] at this, rcases this with ⟨_, ⟨i', rfl⟩, hi'⟩, rw [mem_Union], use i', refine ⟨_, λ j, hi' j.succ⟩, have : i ≠ i', { rintro rfl, apply not_le_of_lt (hi' 0).2, rw [hp0], refl }, have := h.1 i i' this, rw [on_fun, to_set_disjoint, exists_fin_succ] at this, rcases this with h0|⟨j, hj⟩, rw [hp0], symmetry, apply eq_of_Ico_disjoint h0 (by simp [hw]) _, convert hi' 0, rw [hp0], refl, exfalso, apply not_disjoint_iff.mpr ⟨tail p j, hps j, hi' j.succ⟩ hj end omit h /-- A valley is a square on which cubes in the family of cubes are placed, so that the cubes completely cover the valley and none of those cubes is partially outside the square. We also require that no cube on it has the same size as the valley (so that there are at least two cubes on the valley). This is the main concept in the formalization. We prove that the smallest cube on a valley has another valley on the top of it, which gives an infinite sequence of cubes in the partition, which contradicts the finiteness. A valley is characterized by a cube `c` (which is not a cube in the family cs) by considering the bottom face of `c`. -/ def valley (cs : ι → cube (n+1)) (c : cube (n+1)) : Prop := c.bottom ⊆ (⋃(i : ι), (cs i).bottom) ∧ (∀i, (cs i).b 0 = c.b 0 → (∃x, x ∈ (cs i).tail.to_set ∩ c.tail.to_set) → (cs i).tail.to_set ⊆ c.tail.to_set) ∧ ∀(i : ι), (cs i).b 0 = c.b 0 → (cs i).w ≠ c.w variables {c : cube (n+1)} (v : valley cs c) /-- The bottom of the unit cube is a valley -/ lemma valley_unit_cube (h : correct cs) : valley cs unit_cube := begin refine ⟨_, _, _⟩, { intro v, simp only [bottom, and_imp, mem_Union, mem_set_of_eq], intros h0 hv, have : v ∈ (unit_cube : cube (n+1)).to_set, { dsimp only [to_set, unit_cube, mem_set_of_eq], rw [forall_fin_succ, h0], split, norm_num [side, unit_cube], exact hv }, rw [←h.2.1] at this, rcases this with ⟨_, ⟨i, rfl⟩, hi⟩, use i, split, { apply le_antisymm, rw h0, exact zero_le_b h, exact (hi 0).1 }, intro j, exact hi _ }, { intros i hi h', rw to_set_subset, intro j, convert side_subset h using 1, simp [side_tail] }, { intros i hi, exact w_ne_one h i } end /-- the cubes which lie in the valley `c` -/ def bcubes (cs : ι → cube (n+1)) (c : cube (n+1)) : set ι := { i : ι | (cs i).b 0 = c.b 0 ∧ (cs i).tail.to_set ⊆ c.tail.to_set } /-- A cube which lies on the boundary of a valley in dimension `j` -/ def on_boundary (hi : i ∈ bcubes cs c) (j : fin n) : Prop := c.b j.succ = (cs i).b j.succ ∨ (cs i).b j.succ + (cs i).w = c.b j.succ + c.w lemma tail_sub (hi : i ∈ bcubes cs c) : ∀j, (cs i).tail.side j ⊆ c.tail.side j := by { rw [←to_set_subset], exact hi.2 } lemma bottom_mem_side (hi : i ∈ bcubes cs c) : c.b 0 ∈ (cs i).side 0 := by { convert b_mem_side (cs i) _ using 1, rw hi.1 } lemma b_le_b (hi : i ∈ bcubes cs c) (j : fin n) : c.b j.succ ≤ (cs i).b j.succ := (tail_sub hi j $ b_mem_side _ _).1 lemma t_le_t (hi : i ∈ bcubes cs c) (j : fin n) : (cs i).b j.succ + (cs i).w ≤ c.b j.succ + c.w := begin have h' := tail_sub hi j, dsimp only [side] at h', rw [Ico_subset_Ico_iff] at h', exact h'.2, simp [hw] end include h v /-- Every cube in the valley must be smaller than it -/ lemma w_lt_w (hi : i ∈ bcubes cs c) : (cs i).w < c.w := begin apply lt_of_le_of_ne _ (v.2.2 i hi.1), have j : fin n := ⟨1, nat.le_of_succ_le_succ h.2.2.2.2⟩, rw [←add_le_add_iff_left ((cs i).b j.succ)], apply le_trans (t_le_t hi j), rw [add_le_add_iff_right], apply b_le_b hi, end open cardinal /-- There are at least two cubes in a valley -/ lemma two_le_mk_bcubes : 2 ≤ #(bcubes cs c) := begin rw [two_le_iff], rcases v.1 c.b_mem_bottom with ⟨_, ⟨i, rfl⟩, hi⟩, have h2i : i ∈ bcubes cs c := ⟨hi.1.symm, v.2.1 i hi.1.symm ⟨tail c.b, hi.2, λ j, c.b_mem_side j.succ⟩⟩, let j : fin (n+1) := ⟨2, h.2.2.2.2⟩, have hj : 0 ≠ j := by { simp only [fin.ext_iff, ne.def], contradiction }, let p : fin (n+1) → ℝ := λ j', if j' = j then c.b j + (cs i).w else c.b j', have hp : p ∈ c.bottom, { split, { simp only [bottom, p, if_neg hj] }, intro j', simp only [tail, side_tail], by_cases hj' : j'.succ = j, { simp [p, -add_comm, if_pos, side, hj', hw', w_lt_w h v h2i] }, { simp [p, -add_comm, if_neg hj'] }}, rcases v.1 hp with ⟨_, ⟨i', rfl⟩, hi'⟩, have h2i' : i' ∈ bcubes cs c := ⟨hi'.1.symm, v.2.1 i' hi'.1.symm ⟨tail p, hi'.2, hp.2⟩⟩, refine ⟨⟨i, h2i⟩, ⟨i', h2i'⟩, _⟩, intro hii', cases congr_arg subtype.val hii', apply not_le_of_lt (hi'.2 ⟨1, nat.le_of_succ_le_succ h.2.2.2.2⟩).2, simp only [-add_comm, tail, cube.tail, p], rw [if_pos, add_le_add_iff_right], { exact (hi.2 _).1 }, refl end /-- There is a cube in the valley -/ lemma nonempty_bcubes : (bcubes cs c).nonempty := begin rw [←set.ne_empty_iff_nonempty], intro h', have := two_le_mk_bcubes h v, rw h' at this, apply not_lt_of_le this, rw mk_emptyc, norm_cast, norm_num end /-- There is a smallest cube in the valley -/ lemma exists_mi : ∃(i : ι), i ∈ bcubes cs c ∧ ∀(i' ∈ bcubes cs c), (cs i).w ≤ (cs i').w := by simpa using (bcubes cs c).exists_min_image (λ i, (cs i).w) (finite.of_fintype _) (nonempty_bcubes h v) /-- We let `mi` be the (index for the) smallest cube in the valley `c` -/ def mi : ι := classical.some $ exists_mi h v variables {h v} lemma mi_mem_bcubes : mi h v ∈ bcubes cs c := (classical.some_spec $ exists_mi h v).1 lemma mi_minimal (hi : i ∈ bcubes cs c) : (cs $ mi h v).w ≤ (cs i).w := (classical.some_spec $ exists_mi h v).2 i hi lemma mi_strict_minimal (hii' : mi h v ≠ i) (hi : i ∈ bcubes cs c) : (cs $ mi h v).w < (cs i).w := by { apply lt_of_le_of_ne (mi_minimal hi), apply h.2.2.1.ne, apply hii' } /-- The top of `mi` cannot be 1, since there is a larger cube in the valley -/ lemma mi_xm_ne_one : (cs $ mi h v).xm ≠ 1 := begin apply ne_of_lt, rcases (two_le_iff' _).mp (two_le_mk_bcubes h v) with ⟨⟨i, hi⟩, h2i⟩, swap, exact ⟨mi h v, mi_mem_bcubes⟩, apply lt_of_lt_of_le _ (b_add_w_le_one h), exact i, exact 0, rw [xm, mi_mem_bcubes.1, hi.1, _root_.add_lt_add_iff_left], apply mi_strict_minimal _ hi, intro h', apply h2i, rw subtype.ext_iff_val, exact h' end /-- If `mi` lies on the boundary of the valley in dimension j, then this lemma expresses that all other cubes on the same boundary extend further from the boundary. More precisely, there is a j-th coordinate `x : ℝ` in the valley, but not in `mi`, such that every cube that shares a (particular) j-th coordinate with `mi` also contains j-th coordinate `x` -/ lemma smallest_on_boundary {j} (bi : on_boundary (mi_mem_bcubes : mi h v ∈ _) j) : ∃(x : ℝ), x ∈ c.side j.succ \ (cs $ mi h v).side j.succ ∧ ∀{{i'}} (hi' : i' ∈ bcubes cs c), i' ≠ mi h v → (cs $ mi h v).b j.succ ∈ (cs i').side j.succ → x ∈ (cs i').side j.succ := begin let i := mi h v, have hi : i ∈ bcubes cs c := mi_mem_bcubes, cases bi, { refine ⟨(cs i).b j.succ + (cs i).w, ⟨_, _⟩, _⟩, { simp [side, bi, hw', w_lt_w h v hi] }, { intro h', simpa [i, lt_irrefl] using h'.2 }, intros i' hi' i'_i h2i', split, apply le_trans h2i'.1, { simp [hw'] }, apply lt_of_lt_of_le (add_lt_add_left (mi_strict_minimal i'_i.symm hi') _), simp [bi.symm, b_le_b hi'] }, let s := bcubes cs c \ { i }, have hs : s.nonempty, { rcases (two_le_iff' (⟨i, hi⟩ : bcubes cs c)).mp (two_le_mk_bcubes h v) with ⟨⟨i', hi'⟩, h2i'⟩, refine ⟨i', hi', _⟩, simp only [mem_singleton_iff], intro h, apply h2i', simp [h] }, rcases set.exists_min_image s (w ∘ cs) (finite.of_fintype _) hs with ⟨i', ⟨hi', h2i'⟩, h3i'⟩, rw [mem_singleton_iff] at h2i', let x := c.b j.succ + c.w - (cs i').w, have hx : x < (cs i).b j.succ, { dsimp only [x], rw [←bi, add_sub_assoc, add_lt_iff_neg_left, sub_lt_zero], apply mi_strict_minimal (ne.symm h2i') hi' }, refine ⟨x, ⟨_, _⟩, _⟩, { simp only [side, x, -add_comm, -add_assoc, neg_lt_zero, hw, add_lt_iff_neg_left, and_true, mem_Ico, sub_eq_add_neg], rw [add_assoc, le_add_iff_nonneg_right, ←sub_eq_add_neg, sub_nonneg], apply le_of_lt (w_lt_w h v hi') }, { simp only [side, not_and_distrib, not_lt, add_comm, not_le, mem_Ico], left, exact hx }, intros i'' hi'' h2i'' h3i'', split, swap, apply lt_trans hx h3i''.2, simp only [x], rw [le_sub_iff_add_le], refine le_trans _ (t_le_t hi'' j), rw [add_le_add_iff_left], apply h3i' i'' ⟨hi'', _⟩, simp [mem_singleton, h2i''] end variables (h v) /-- `mi` cannot lie on the boundary of the valley. Otherwise, the cube adjacent to it in the `j`-th direction will intersect one of the neighbouring cubes on the same boundary as `mi`. -/ lemma mi_not_on_boundary (j : fin n) : ¬on_boundary (mi_mem_bcubes : mi h v ∈ _) j := begin let i := mi h v, have hi : i ∈ bcubes cs c := mi_mem_bcubes, rcases (two_le_iff' j).mp _ with ⟨j', hj'⟩, swap, { rw [mk_fin, ←nat.cast_two, nat_cast_le], apply nat.le_of_succ_le_succ h.2.2.2.2 }, intro hj, rcases smallest_on_boundary hj with ⟨x, ⟨hx, h2x⟩, h3x⟩, let p : fin (n+1) → ℝ := cons (c.b 0) (λ j₂, if j₂ = j then x else (cs i).b j₂.succ), have hp : p ∈ c.bottom, { suffices : ∀ (j' : fin n), ite (j' = j) x ((cs i).b j'.succ) ∈ c.side j'.succ, { simpa [bottom, p, to_set, tail, side_tail] }, intro j₂, by_cases hj₂ : j₂ = j, { simp [hj₂, hx] }, simp only [hj₂, if_false], apply tail_sub hi, apply b_mem_side }, rcases v.1 hp with ⟨_, ⟨i', rfl⟩, hi'⟩, have h2i' : i' ∈ bcubes cs c := ⟨hi'.1.symm, v.2.1 i' hi'.1.symm ⟨tail p, hi'.2, hp.2⟩⟩, have i_i' : i ≠ i', { rintro rfl, simpa [p, side_tail, i, h2x] using hi'.2 j }, have : nonempty ↥((cs i').tail.side j' \ (cs i).tail.side j'), { apply nonempty_Ico_sdiff, apply mi_strict_minimal i_i' h2i', apply hw }, rcases this with ⟨⟨x', hx'⟩⟩, let p' : fin (n+1) → ℝ := cons (c.b 0) (λ j₂, if j₂ = j' then x' else (cs i).b j₂.succ), have hp' : p' ∈ c.bottom, { suffices : ∀ (j : fin n), ite (j = j') x' ((cs i).b j.succ) ∈ c.side j.succ, { simpa [bottom, p', to_set, tail, side_tail] }, intro j₂, by_cases hj₂ : j₂ = j', simp [hj₂], apply tail_sub h2i', apply hx'.1, simp only [if_congr, if_false, hj₂], apply tail_sub hi, apply b_mem_side }, rcases v.1 hp' with ⟨_, ⟨i'', rfl⟩, hi''⟩, have h2i'' : i'' ∈ bcubes cs c := ⟨hi''.1.symm, v.2.1 i'' hi''.1.symm ⟨tail p', hi''.2, hp'.2⟩⟩, have i'_i'' : i' ≠ i'', { rintro ⟨⟩, have : (cs i).b ∈ (cs i').to_set, { simp only [to_set, forall_fin_succ, hi.1, bottom_mem_side h2i', true_and, mem_set_of_eq], intro j₂, by_cases hj₂ : j₂ = j, { simpa [side_tail, p', hj', hj₂] using hi''.2 j }, { simpa [hj₂] using hi'.2 j₂ } }, apply not_disjoint_iff.mpr ⟨(cs i).b, (cs i).b_mem_to_set, this⟩ (h.1 i i' i_i') }, have i_i'' : i ≠ i'', { intro h, induction h, simpa [hx'.2] using hi''.2 j' }, apply not.elim _ (h.1 i' i'' i'_i''), simp only [on_fun, to_set_disjoint, not_disjoint_iff, forall_fin_succ, not_exists, comp_app], refine ⟨⟨c.b 0, bottom_mem_side h2i', bottom_mem_side h2i''⟩, _⟩, intro j₂, by_cases hj₂ : j₂ = j, { cases hj₂, refine ⟨x, _, _⟩, { convert hi'.2 j, simp [p] }, apply h3x h2i'' i_i''.symm, convert hi''.2 j, simp [p', hj'] }, by_cases h2j₂ : j₂ = j', { cases h2j₂, refine ⟨x', hx'.1, _⟩, convert hi''.2 j', simp }, refine ⟨(cs i).b j₂.succ, _, _⟩, { convert hi'.2 j₂, simp [hj₂] }, { convert hi''.2 j₂, simp [h2j₂] } end variables {h v} /-- The same result that `mi` cannot lie on the boundary of the valley written as inequalities. -/ lemma mi_not_on_boundary' (j : fin n) : c.tail.b j < (cs (mi h v)).tail.b j ∧ (cs (mi h v)).tail.b j + (cs (mi h v)).w < c.tail.b j + c.w := begin have := mi_not_on_boundary h v j, simp only [on_boundary, not_or_distrib] at this, cases this with h1 h2, split, apply lt_of_le_of_ne (b_le_b mi_mem_bcubes _) h1, apply lt_of_le_of_ne _ h2, apply ((Ico_subset_Ico_iff _).mp (tail_sub mi_mem_bcubes j)).2, simp [hw] end /-- The top of `mi` gives rise to a new valley, since the neighbouring cubes extend further upward than `mi`. -/ def valley_mi : valley cs ((cs (mi h v)).shift_up) := begin let i := mi h v, have hi : i ∈ bcubes cs c := mi_mem_bcubes, refine ⟨_, _, _⟩, { intro p, apply shift_up_bottom_subset_bottoms h mi_xm_ne_one }, { rintros i' hi' ⟨p2, hp2, h2p2⟩, simp only [head_shift_up] at hi', classical, by_contra h2i', rw [tail_shift_up] at h2p2, simp only [not_subset, tail_shift_up] at h2i', rcases h2i' with ⟨p1, hp1, h2p1⟩, have : ∃p3, p3 ∈ (cs i').tail.to_set ∧ p3 ∉ (cs i).tail.to_set ∧ p3 ∈ c.tail.to_set, { simp only [to_set, not_forall, mem_set_of_eq] at h2p1, cases h2p1 with j hj, rcases Ico_lemma (mi_not_on_boundary' j).1 (by simp [hw]) (mi_not_on_boundary' j).2 (le_trans (hp2 j).1 $ le_of_lt (h2p2 j).2) (le_trans (h2p2 j).1 $ le_of_lt (hp2 j).2) ⟨hj, hp1 j⟩ with ⟨w, hw, h2w, h3w⟩, refine ⟨λ j', if j' = j then w else p2 j', _, _, _⟩, { intro j', by_cases h : j' = j, { simp only [if_pos h], convert h3w }, { simp only [if_neg h], exact hp2 j' } }, { simp only [to_set, not_forall, mem_set_of_eq], use j, rw [if_pos rfl], convert h2w }, { intro j', by_cases h : j' = j, { simp only [if_pos h, side_tail], convert hw }, { simp only [if_neg h], apply hi.2, apply h2p2 } } }, rcases this with ⟨p3, h1p3, h2p3, h3p3⟩, let p := @cons n (λ_, ℝ) (c.b 0) p3, have hp : p ∈ c.bottom, { refine ⟨rfl, _⟩, rwa [tail_cons] }, rcases v.1 hp with ⟨_, ⟨i'', rfl⟩, hi''⟩, have h2i'' : i'' ∈ bcubes cs c, { use hi''.1.symm, apply v.2.1 i'' hi''.1.symm, use tail p, split, exact hi''.2, rw [tail_cons], exact h3p3 }, have h3i'' : (cs i).w < (cs i'').w, { apply mi_strict_minimal _ h2i'', rintro rfl, apply h2p3, convert hi''.2, rw [tail_cons] }, let p' := @cons n (λ_, ℝ) (cs i).xm p3, have hp' : p' ∈ (cs i').to_set, { simpa [to_set, forall_fin_succ, p', hi'.symm] using h1p3 }, have h2p' : p' ∈ (cs i'').to_set, { simp only [to_set, forall_fin_succ, p', cons_succ, cons_zero, mem_set_of_eq], refine ⟨_, by simpa [to_set, p] using hi''.2⟩, have : (cs i).b 0 = (cs i'').b 0, { by rw [hi.1, h2i''.1] }, simp [side, hw', xm, this, h3i''] }, apply not_disjoint_iff.mpr ⟨p', hp', h2p'⟩, apply h.1, rintro rfl, apply (cs i).b_ne_xm, rw [←hi', ←hi''.1, hi.1], refl }, { intros i' hi' h2i', dsimp only [shift_up] at h2i', replace h2i' := h.2.2.1 h2i'.symm, induction h2i', exact b_ne_xm (cs i) hi' } end variables (h) omit v /-- We get a sequence of cubes whose size is decreasing -/ noncomputable def sequence_of_cubes : ℕ → { i : ι // valley cs ((cs i).shift_up) } | 0 := let v := valley_unit_cube h in ⟨mi h v, valley_mi⟩ | (k+1) := let v := (sequence_of_cubes k).2 in ⟨mi h v, valley_mi⟩ def decreasing_sequence (k : ℕ) : order_dual ℝ := (cs (sequence_of_cubes h k).1).w lemma strict_mono_sequence_of_cubes : strict_mono $ decreasing_sequence h := strict_mono_nat_of_lt_succ $ begin intro k, let v := (sequence_of_cubes h k).2, dsimp only [decreasing_sequence, sequence_of_cubes], apply w_lt_w h v (mi_mem_bcubes : mi h v ∈ _), end omit h /-- The infinite sequence of cubes contradicts the finiteness of the family. -/ theorem not_correct : ¬correct cs := begin intro h, apply not_le_of_lt (lt_omega_iff_fintype.mpr ⟨_inst_1⟩), rw [omega, lift_id], fapply mk_le_of_injective, exact λ n, (sequence_of_cubes h n).1, intros n m hnm, apply strict_mono.injective (strict_mono_sequence_of_cubes h), dsimp only [decreasing_sequence], rw hnm end /-- **Dissection of Cubes**: A cube cannot be cubed. -/ theorem cannot_cube_a_cube : ∀{n : ℕ}, n ≥ 3 → -- In ℝ^n for n ≥ 3 ∀{ι : Type} [fintype ι] {cs : ι → cube n}, -- given a finite collection of (hyper)cubes 2 ≤ #ι → -- containing at least two elements pairwise (disjoint on (cube.to_set ∘ cs)) → -- which is pairwise disjoint (⋃(i : ι), (cs i).to_set) = unit_cube.to_set → -- whose union is the unit cube injective (cube.w ∘ cs) → -- such that the widths of all cubes are different false := -- then we can derive a contradiction begin intros n hn ι hι cs h1 h2 h3 h4, resetI, rcases n, cases hn, exact not_correct ⟨h2, h3, h4, h1, hn⟩ end
dace73c733fd9dcc51f1c119fbc8ec2cb30a413b
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/qpf/multivariate/basic.lean
8583206ecf9cd50ac553e2e0a5ffaaf94f1a5dfe
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
8,755
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Simon Hudon -/ import data.pfunctor.multivariate.basic /-! # Multivariate quotients of polynomial functors. Basic definition of multivariate QPF. QPFs form a compositional framework for defining inductive and coinductive types, their quotients and nesting. The idea is based on building ever larger functors. For instance, we can define a list using a shape functor: ```lean inductive list_shape (a b : Type) | nil : list_shape | cons : a -> b -> list_shape ``` This shape can itself be decomposed as a sum of product which are themselves QPFs. It follows that the shape is a QPF and we can take its fixed point and create the list itself: ```lean def list (a : Type) := fix list_shape a -- not the actual notation ``` We can continue and define the quotient on permutation of lists and create the multiset type: ```lean def multiset (a : Type) := qpf.quot list.perm list a -- not the actual notion ``` And `multiset` is also a QPF. We can then create a novel data type (for Lean): ```lean inductive tree (a : Type) | node : a -> multiset tree -> tree ``` An unordered tree. This is currently not supported by Lean because it nests an inductive type inside of a quotient. We can go further and define unordered, possibly infinite trees: ```lean coinductive tree' (a : Type) | node : a -> multiset tree' -> tree' ``` by using the `cofix` construct. Those options can all be mixed and matched because they preserve the properties of QPF. The latter example, `tree'`, combines fixed point, co-fixed point and quotients. ## Related modules * constructions * fix * cofix * quot * comp * sigma / pi * prj * const each proves that some operations on functors preserves the QPF structure ##reference * [Jeremy Avigad, Mario M. Carneiro and Simon Hudon, *Data Types as Quotients of Polynomial Functors*][avigad-carneiro-hudon2019] -/ universe u open_locale mvfunctor /-- Multivariate quotients of polynomial functors. -/ class mvqpf {n : ℕ} (F : typevec.{u} n → Type*) [mvfunctor F] := (P : mvpfunctor.{u} n) (abs : Π {α}, P.obj α → F α) (repr : Π {α}, F α → P.obj α) (abs_repr : ∀ {α} (x : F α), abs (repr x) = x) (abs_map : ∀ {α β} (f : α ⟹ β) (p : P.obj α), abs (f <$$> p) = f <$$> abs p) namespace mvqpf variables {n : ℕ} {F : typevec.{u} n → Type*} [mvfunctor F] [q : mvqpf F] include q open mvfunctor (liftp liftr) /-! ### Show that every mvqpf is a lawful mvfunctor. -/ protected theorem id_map {α : typevec n} (x : F α) : typevec.id <$$> x = x := by { rw ←abs_repr x, cases repr x with a f, rw [←abs_map], reflexivity } @[simp] theorem comp_map {α β γ : typevec n} (f : α ⟹ β) (g : β ⟹ γ) (x : F α) : (g ⊚ f) <$$> x = g <$$> f <$$> x := by { rw ←abs_repr x, cases repr x with a f, rw [←abs_map, ←abs_map, ←abs_map], reflexivity } @[priority 100] instance is_lawful_mvfunctor : is_lawful_mvfunctor F := { id_map := @mvqpf.id_map n F _ _, comp_map := @comp_map n F _ _ } /- Lifting predicates and relations -/ theorem liftp_iff {α : typevec n} (p : Π ⦃i⦄, α i → Prop) (x : F α) : liftp p x ↔ ∃ a f, x = abs ⟨a, f⟩ ∧ ∀ i j, p (f i j) := begin split, { rintros ⟨y, hy⟩, cases h : repr y with a f, use [a, λ i j, (f i j).val], split, { rw [←hy, ←abs_repr y, h, ←abs_map], reflexivity }, intros i j, apply (f i j).property }, rintros ⟨a, f, h₀, h₁⟩, dsimp at *, use abs (⟨a, λ i j, ⟨f i j, h₁ i j⟩⟩), rw [←abs_map, h₀], reflexivity end theorem liftr_iff {α : typevec n} (r : Π ⦃i⦄, α i → α i → Prop) (x y : F α) : liftr r x y ↔ ∃ a f₀ f₁, x = abs ⟨a, f₀⟩ ∧ y = abs ⟨a, f₁⟩ ∧ ∀ i j, r (f₀ i j) (f₁ i j) := begin split, { rintros ⟨u, xeq, yeq⟩, cases h : repr u with a f, use [a, λ i j, (f i j).val.fst, λ i j, (f i j).val.snd], split, { rw [←xeq, ←abs_repr u, h, ←abs_map], refl }, split, { rw [←yeq, ←abs_repr u, h, ←abs_map], refl }, intros i j, exact (f i j).property }, rintros ⟨a, f₀, f₁, xeq, yeq, h⟩, use abs ⟨a, λ i j, ⟨(f₀ i j, f₁ i j), h i j⟩⟩, dsimp, split, { rw [xeq, ←abs_map], refl }, rw [yeq, ←abs_map], refl end open set open mvfunctor theorem mem_supp {α : typevec n} (x : F α) (i) (u : α i) : u ∈ supp x i ↔ ∀ a f, abs ⟨a, f⟩ = x → u ∈ f i '' univ := begin rw [supp], dsimp, split, { intros h a f haf, have : liftp (λ i u, u ∈ f i '' univ) x, { rw liftp_iff, refine ⟨a, f, haf.symm, _⟩, intros i u, exact mem_image_of_mem _ (mem_univ _) }, exact h this }, intros h p, rw liftp_iff, rintros ⟨a, f, xeq, h'⟩, rcases h a f xeq.symm with ⟨i, _, hi⟩, rw ←hi, apply h' end theorem supp_eq {α : typevec n} {i} (x : F α) : supp x i = { u | ∀ a f, abs ⟨a, f⟩ = x → u ∈ f i '' univ } := by ext; apply mem_supp theorem has_good_supp_iff {α : typevec n} (x : F α) : (∀ p, liftp p x ↔ ∀ i (u ∈ supp x i), p i u) ↔ ∃ a f, abs ⟨a, f⟩ = x ∧ ∀ i a' f', abs ⟨a', f'⟩ = x → f i '' univ ⊆ f' i '' univ := begin split, { intros h, have : liftp (supp x) x, by { rw h, introv, exact id, }, rw liftp_iff at this, rcases this with ⟨a, f, xeq, h'⟩, refine ⟨a, f, xeq.symm, _⟩, intros a' f' h'', rintros hu u ⟨j, h₂, hfi⟩, have hh : u ∈ supp x a', by rw ←hfi; apply h', refine (mem_supp x _ u).mp hh _ _ hu, }, rintros ⟨a, f, xeq, h⟩ p, rw liftp_iff, split, { rintros ⟨a', f', xeq', h'⟩ i u usuppx, rcases (mem_supp x _ u).mp @usuppx a' f' xeq'.symm with ⟨i, _, f'ieq⟩, rw ←f'ieq, apply h' }, intro h', refine ⟨a, f, xeq.symm, _⟩, intros j y, apply h', rw mem_supp, intros a' f' xeq', apply h _ a' f' xeq', apply mem_image_of_mem _ (mem_univ _) end variable (q) /-- A qpf is said to be uniform if every polynomial functor representing a single value all have the same range. -/ def is_uniform : Prop := ∀ ⦃α : typevec n⦄ (a a' : q.P.A) (f : q.P.B a ⟹ α) (f' : q.P.B a' ⟹ α), abs ⟨a, f⟩ = abs ⟨a', f'⟩ → ∀ i, f i '' univ = f' i '' univ /-- does `abs` preserve `liftp`? -/ def liftp_preservation : Prop := ∀ ⦃α : typevec n⦄ (p : Π ⦃i⦄, α i → Prop) (x : q.P.obj α), liftp p (abs x) ↔ liftp p x /-- does `abs` preserve `supp`? -/ def supp_preservation : Prop := ∀ ⦃α⦄ (x : q.P.obj α), supp (abs x) = supp x variable [q] theorem supp_eq_of_is_uniform (h : q.is_uniform) {α : typevec n} (a : q.P.A) (f : q.P.B a ⟹ α) : ∀ i, supp (abs ⟨a, f⟩) i = f i '' univ := begin intro, ext u, rw [mem_supp], split, { intro h', apply h' _ _ rfl }, intros h' a' f' e, rw [←h _ _ _ _ e.symm], apply h' end theorem liftp_iff_of_is_uniform (h : q.is_uniform) {α : typevec n} (x : F α) (p : Π i, α i → Prop) : liftp p x ↔ ∀ i (u ∈ supp x i), p i u := begin rw [liftp_iff, ←abs_repr x], cases repr x with a f, split, { rintros ⟨a', f', abseq, hf⟩ u, rw [supp_eq_of_is_uniform h, h _ _ _ _ abseq], rintros b ⟨i, _, hi⟩, rw ←hi, apply hf }, intro h', refine ⟨a, f, rfl, λ _ i, h' _ _ _⟩, rw supp_eq_of_is_uniform h, exact ⟨i, mem_univ i, rfl⟩ end theorem supp_map (h : q.is_uniform) {α β : typevec n} (g : α ⟹ β) (x : F α) (i) : supp (g <$$> x) i = g i '' supp x i := begin rw ←abs_repr x, cases repr x with a f, rw [←abs_map, mvpfunctor.map_eq], rw [supp_eq_of_is_uniform h, supp_eq_of_is_uniform h, ← image_comp], refl, end theorem supp_preservation_iff_uniform : q.supp_preservation ↔ q.is_uniform := begin split, { intros h α a a' f f' h' i, rw [← mvpfunctor.supp_eq,← mvpfunctor.supp_eq,← h,h',h] }, { rintros h α ⟨a,f⟩, ext, rwa [supp_eq_of_is_uniform,mvpfunctor.supp_eq], } end theorem supp_preservation_iff_liftp_preservation : q.supp_preservation ↔ q.liftp_preservation := begin split; intro h, { rintros α p ⟨a,f⟩, have h' := h, rw supp_preservation_iff_uniform at h', dsimp only [supp_preservation,supp] at h, simp only [liftp_iff_of_is_uniform, supp_eq_of_is_uniform, mvpfunctor.liftp_iff', h', image_univ, mem_range, exists_imp_distrib], split; intros; subst_vars; solve_by_elim }, { rintros α ⟨a,f⟩, simp only [liftp_preservation] at h, ext, simp [supp,h] } end theorem liftp_preservation_iff_uniform : q.liftp_preservation ↔ q.is_uniform := by rw [← supp_preservation_iff_liftp_preservation, supp_preservation_iff_uniform] end mvqpf
ef86009a4b22569a085c7f048be0a41eb7915214
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/src/Lean/Elab/PreDefinition/Basic.lean
a48966db6e5893b619bea07611749caa6588af60
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,349
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.SCC import Lean.Meta.AbstractNestedProofs import Lean.Elab.MkInhabitant import Lean.Elab.Term import Lean.Elab.DefView namespace Lean namespace Elab open Meta open Term /- A (potentially recursive) definition. The elaborator converts it into Kernel definitions using many different strategies. -/ structure PreDefinition := (kind : DefKind) (lparams : List Name) (modifiers : Modifiers) (declName : Name) (type : Expr) (value : Expr) instance PreDefinition.inhabited : Inhabited PreDefinition := ⟨⟨DefKind.«def», [], {}, arbitrary _, arbitrary _, arbitrary _⟩⟩ def instantiateMVarsAtPreDecls (preDefs : Array PreDefinition) : TermElabM (Array PreDefinition) := preDefs.mapM fun preDef => do type ← instantiateMVars preDef.type; value ← instantiateMVars preDef.value; pure { preDef with type := type, value := value } private def levelMVarToParamExpr (e : Expr) : StateRefT Nat TermElabM Expr := do nextIdx ← get; (e, nextIdx) ← liftM $ levelMVarToParam e nextIdx; set nextIdx; pure e private def levelMVarToParamPreDeclsAux (preDefs : Array PreDefinition) : StateRefT Nat TermElabM (Array PreDefinition) := preDefs.mapM fun preDef => do type ← levelMVarToParamExpr preDef.type; value ← levelMVarToParamExpr preDef.value; pure { preDef with type := type, value := value } def levelMVarToParamPreDecls (preDefs : Array PreDefinition) : TermElabM (Array PreDefinition) := (levelMVarToParamPreDeclsAux preDefs).run' 1 private def collectLevelParamsExpr (e : Expr) : StateM CollectLevelParams.State Unit := do modify fun s => collectLevelParams s e private def getLevelParamsPreDecls (preDefs : Array PreDefinition) (scopeLevelNames allUserLevelNames : List Name) : TermElabM (List Name) := let (_, s) := StateT.run (preDefs.forM fun preDef => do { collectLevelParamsExpr preDef.type; collectLevelParamsExpr preDef.value }) {}; match sortDeclLevelParams scopeLevelNames allUserLevelNames s.params with | Except.error msg => throwError msg | Except.ok levelParams => pure levelParams private def shareCommon (preDefs : Array PreDefinition) : Array PreDefinition := let result : Std.ShareCommonM (Array PreDefinition) := preDefs.mapM fun preDef => do { type ← Std.withShareCommon preDef.type; value ← Std.withShareCommon preDef.value; pure { preDef with type := type, value := value } }; result.run def fixLevelParams (preDefs : Array PreDefinition) (scopeLevelNames allUserLevelNames : List Name) : TermElabM (Array PreDefinition) := do let preDefs := shareCommon preDefs; lparams ← getLevelParamsPreDecls preDefs scopeLevelNames allUserLevelNames; let us := lparams.map mkLevelParam; let fixExpr (e : Expr) : Expr := e.replace fun c => match c with | Expr.const declName _ _ => if preDefs.any fun preDef => preDef.declName == declName then some $ Lean.mkConst declName us else none | _ => none; pure $ preDefs.map fun preDef => { preDef with type := fixExpr preDef.type, value := fixExpr preDef.value, lparams := lparams } def applyAttributesOf (preDefs : Array PreDefinition) (applicationTime : AttributeApplicationTime) : TermElabM Unit := do preDefs.forM fun preDef => applyAttributes preDef.declName preDef.modifiers.attrs applicationTime def abstractNestedProofs (preDef : PreDefinition) : MetaM PreDefinition := if preDef.kind.isTheorem || preDef.kind.isExample then pure preDef else do value ← Meta.abstractNestedProofs preDef.declName preDef.value; pure { preDef with value := value } private def addNonRecAux (preDef : PreDefinition) (compile : Bool) : TermElabM Unit := do preDef ← liftM $ abstractNestedProofs preDef; env ← getEnv; let decl := match preDef.kind with | DefKind.«example» => unreachable! | DefKind.«theorem» => Declaration.thmDecl { name := preDef.declName, lparams := preDef.lparams, type := preDef.type, value := preDef.value } | DefKind.«opaque» => Declaration.opaqueDecl { name := preDef.declName, lparams := preDef.lparams, type := preDef.type, value := preDef.value, isUnsafe := preDef.modifiers.isUnsafe } | DefKind.«abbrev» => Declaration.defnDecl { name := preDef.declName, lparams := preDef.lparams, type := preDef.type, value := preDef.value, hints := ReducibilityHints.«abbrev», isUnsafe := preDef.modifiers.isUnsafe } | DefKind.«def» => Declaration.defnDecl { name := preDef.declName, lparams := preDef.lparams, type := preDef.type, value := preDef.value, hints := ReducibilityHints.regular (getMaxHeight env preDef.value + 1), isUnsafe := preDef.modifiers.isUnsafe }; addDecl decl; applyAttributesOf #[preDef] AttributeApplicationTime.afterTypeChecking; when (compile && !preDef.kind.isTheorem) $ compileDecl decl; applyAttributesOf #[preDef] AttributeApplicationTime.afterCompilation; pure () def addAndCompileNonRec (preDef : PreDefinition) : TermElabM Unit := do addNonRecAux preDef true def addNonRec (preDef : PreDefinition) : TermElabM Unit := do addNonRecAux preDef false def addAndCompileUnsafe (preDefs : Array PreDefinition) : TermElabM Unit := do let decl := Declaration.mutualDefnDecl $ preDefs.toList.map fun preDef => { name := preDef.declName, lparams := preDef.lparams, type := preDef.type, value := preDef.value, isUnsafe := true, hints := ReducibilityHints.opaque }; addDecl decl; applyAttributesOf preDefs AttributeApplicationTime.afterTypeChecking; compileDecl decl; applyAttributesOf preDefs AttributeApplicationTime.afterCompilation; pure () def addAndCompileUnsafeRec (preDefs : Array PreDefinition) : TermElabM Unit := do addAndCompileUnsafe $ preDefs.map fun preDef => { preDef with declName := Compiler.mkUnsafeRecName preDef.declName, value := preDef.value.replace fun e => match e with | Expr.const declName us _ => if preDefs.any fun preDef => preDef.declName == declName then some $ mkConst (Compiler.mkUnsafeRecName declName) us else none | _ => none, modifiers := {} } end Elab end Lean
9d4256b1c51fdc2ea4e64a4baac71091485e7d37
1b8f093752ba748c5ca0083afef2959aaa7dace5
/src/category_theory/universal/constructions/limits_from.lean
c3b50eece56f952411bdcd7fe4cecc7867e0578d
[]
no_license
khoek/lean-category-theory
7ec4cda9cc64a5a4ffeb84712ac7d020dbbba386
63dcb598e9270a3e8b56d1769eb4f825a177cd95
refs/heads/master
1,585,251,725,759
1,539,344,445,000
1,539,344,445,000
145,281,070
0
0
null
1,534,662,376,000
1,534,662,376,000
null
UTF-8
Lean
false
false
1,664
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison import category_theory.limits open category_theory namespace category_theory.limits universes u₁ v₁ variables {C : Type u₁} [category.{u₁ v₁} C] instance [has_products.{u₁ v₁} C] [has_equalizers.{u₁ v₁} C] : has_limits.{u₁ v₁} C := { limit := λ J 𝒥 F, begin resetI, let β_obj := (λ j : J, F j), let β_hom := (λ f : (Σ p : J × J, p.1 ⟶ p.2), F f.1.2), let pi_obj := pi β_obj, let pi_hom := pi β_hom, let s : pi_obj ⟶ pi_hom := pi.lift (λ f : (Σ p : J × J, p.1 ⟶ p.2), pi.π β_obj f.1.1 ≫ F.map f.2), let t : pi_obj ⟶ pi_hom := pi.lift (λ f : (Σ p : J × J, p.1 ⟶ p.2), pi.π β_obj f.1.2), exact { X := equalizer s t, π := λ j, equalizer.ι s t ≫ pi.π β_obj j, w := λ j j' f, begin -- FIXME Ugh, this should be much smoother. rw category.assoc, have p := congr_arg (λ φ , φ ≫ pi.π β_hom ⟨ ⟨ j, j' ⟩, f ⟩) (equalizer.w s t), dsimp at p, simp at p, exact p, end } end, is_limit := λ J 𝒥 F, begin resetI, exact { lift := λ c, begin -- TODO cleanup? fapply equalizer.lift, apply pi.lift, exact (λ j : J, c.π j), obviously, end } end } end category_theory.limits
b8570d72ef2c326cd6f9e604886b68e824480400
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/collision_bug.lean
14c62b62e24a69f921c4a32287470b840a153b88
[ "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
67
lean
import theories.measure_theory.sigma_algebra import data.int.order
bd76d77362de2d435c5a1cc0852de5f745acacf1
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/fintype/sum.lean
a34e9a08da86eacc7dce9d1c7262cfbb96cd757c
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
5,256
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.fintype.card import data.finset.sum import logic.embedding.set /-! ## Instances We provide the `fintype` instance for the sum of two fintypes. -/ universes u v variables {α β : Type*} open finset instance (α : Type u) (β : Type v) [fintype α] [fintype β] : fintype (α ⊕ β) := { elems := univ.disj_sum univ, complete := by rintro (_ | _); simp } @[simp] lemma finset.univ_disj_sum_univ {α β : Type*} [fintype α] [fintype β] : univ.disj_sum univ = (univ : finset (α ⊕ β)) := rfl @[simp] theorem fintype.card_sum [fintype α] [fintype β] : fintype.card (α ⊕ β) = fintype.card α + fintype.card β := card_disj_sum _ _ /-- If the subtype of all-but-one elements is a `fintype` then the type itself is a `fintype`. -/ def fintype_of_fintype_ne (a : α) (h : fintype {b // b ≠ a}) : fintype α := fintype.of_bijective (sum.elim (coe : {b // b = a} → α) (coe : {b // b ≠ a} → α)) $ by { classical, exact (equiv.sum_compl (= a)).bijective } lemma image_subtype_ne_univ_eq_image_erase [fintype α] (k : β) (b : α → β) : image (λ i : {a // b a ≠ k}, b ↑i) univ = (image b univ).erase k := begin apply subset_antisymm, { rw image_subset_iff, intros i _, apply mem_erase_of_ne_of_mem i.2 (mem_image_of_mem _ (mem_univ _)) }, { intros i hi, rw mem_image, rcases mem_image.1 (erase_subset _ _ hi) with ⟨a, _, ha⟩, subst ha, exact ⟨⟨a, ne_of_mem_erase hi⟩, mem_univ _, rfl⟩ } end lemma image_subtype_univ_ssubset_image_univ [fintype α] (k : β) (b : α → β) (hk : k ∈ image b univ) (p : β → Prop) [decidable_pred p] (hp : ¬ p k) : image (λ i : {a // p (b a)}, b ↑i) univ ⊂ image b univ := begin split, { intros x hx, rcases mem_image.1 hx with ⟨y, _, hy⟩, exact hy ▸ mem_image_of_mem b (mem_univ y) }, { intros h, rw mem_image at hk, rcases hk with ⟨k', _, hk'⟩, subst hk', have := h (mem_image_of_mem b (mem_univ k')), rw mem_image at this, rcases this with ⟨j, hj, hj'⟩, exact hp (hj' ▸ j.2) } end /-- Any injection from a finset `s` in a fintype `α` to a finset `t` of the same cardinality as `α` can be extended to a bijection between `α` and `t`. -/ lemma finset.exists_equiv_extend_of_card_eq [fintype α] {t : finset β} (hαt : fintype.card α = t.card) {s : finset α} {f : α → β} (hfst : s.image f ⊆ t) (hfs : set.inj_on f s) : ∃ g : α ≃ t, ∀ i ∈ s, (g i : β) = f i := begin classical, induction s using finset.induction with a s has H generalizing f, { obtain ⟨e⟩ : nonempty (α ≃ ↥t) := by rwa [← fintype.card_eq, fintype.card_coe], use e, simp }, have hfst' : finset.image f s ⊆ t := (finset.image_mono _ (s.subset_insert a)).trans hfst, have hfs' : set.inj_on f s := hfs.mono (s.subset_insert a), obtain ⟨g', hg'⟩ := H hfst' hfs', have hfat : f a ∈ t := hfst (mem_image_of_mem _ (s.mem_insert_self a)), use g'.trans (equiv.swap (⟨f a, hfat⟩ : t) (g' a)), simp_rw mem_insert, rintro i (rfl | hi), { simp }, rw [equiv.trans_apply, equiv.swap_apply_of_ne_of_ne, hg' _ hi], { exact ne_of_apply_ne subtype.val (ne_of_eq_of_ne (hg' _ hi) $ hfs.ne (subset_insert _ _ hi) (mem_insert_self _ _) $ ne_of_mem_of_not_mem hi has) }, { exact g'.injective.ne (ne_of_mem_of_not_mem hi has) }, end /-- Any injection from a set `s` in a fintype `α` to a finset `t` of the same cardinality as `α` can be extended to a bijection between `α` and `t`. -/ lemma set.maps_to.exists_equiv_extend_of_card_eq [fintype α] {t : finset β} (hαt : fintype.card α = t.card) {s : set α} {f : α → β} (hfst : s.maps_to f t) (hfs : set.inj_on f s) : ∃ g : α ≃ t, ∀ i ∈ s, (g i : β) = f i := begin classical, let s' : finset α := s.to_finset, have hfst' : s'.image f ⊆ t := by simpa [← finset.coe_subset] using hfst, have hfs' : set.inj_on f s' := by simpa using hfs, obtain ⟨g, hg⟩ := finset.exists_equiv_extend_of_card_eq hαt hfst' hfs', refine ⟨g, λ i hi, _⟩, apply hg, simpa using hi, end lemma fintype.card_subtype_or (p q : α → Prop) [fintype {x // p x}] [fintype {x // q x}] [fintype {x // p x ∨ q x}] : fintype.card {x // p x ∨ q x} ≤ fintype.card {x // p x} + fintype.card {x // q x} := begin classical, convert fintype.card_le_of_embedding (subtype_or_left_embedding p q), rw fintype.card_sum end lemma fintype.card_subtype_or_disjoint (p q : α → Prop) (h : disjoint p q) [fintype {x // p x}] [fintype {x // q x}] [fintype {x // p x ∨ q x}] : fintype.card {x // p x ∨ q x} = fintype.card {x // p x} + fintype.card {x // q x} := begin classical, convert fintype.card_congr (subtype_or_equiv p q h), simp end section open_locale classical @[simp] lemma infinite_sum : infinite (α ⊕ β) ↔ infinite α ∨ infinite β := begin refine ⟨λ H, _, λ H, H.elim (@sum.infinite_of_left α β) (@sum.infinite_of_right α β)⟩, contrapose! H, haveI := fintype_of_not_infinite H.1, haveI := fintype_of_not_infinite H.2, exact infinite.false end end
a539cc1c18d9c7e1a4b971d1a753bc29b90a7363
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/run/nativeReflBackdoor.lean
fecd31ee5aa0291c0b4e63e1821563e625dc88d3
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
1,002
lean
-- /- This example demonstratea that when we are using `nativeDecide`, we are also trusting the correctness of `implementedBy` annotations, foreign functions (i.e., `[extern]` annotations), etc. -/ def g (b : Bool) := false /- The following `implementedBy` is telling the compiler "trust me, `g` does implement `f`" which is clearly false in this example. -/ @[implementedBy g] def f (b : Bool) := b theorem fConst (b : Bool) : f b = false := match b with | true => /- The following `nativeDecide` is going to use `g` to evaluate `f` because of the `implementedBy` directive. -/ have (f true) = false by nativeDecide this | false => rfl theorem trueEqFalse : true = false := have h₁ : f true = true from rfl; have h₂ : f true = false from fConst true; Eq.trans h₁.symm h₂ /- We managed to prove `False` using the unsound annotation `implementedBy` above. -/ theorem unsound : False := Bool.noConfusion trueEqFalse #print axioms unsound -- axiom 'Lean.ofReduceBool' is listed
ee670ba75138d116a436d7a302efc0d440e24e24
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/termParserAttr.lean
1053b01c29a2c9fea888197ca94af6e6ce5312fa
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
2,088
lean
import Lean open Lean open Lean.Elab def runCore (input : String) (failIff : Bool := true) : CoreM Unit := do let env ← getEnv; let opts ← getOptions; let (env, messages) ← process input env opts; messages.toList.forM fun msg => do IO.println (← msg.toString) if failIff && messages.hasErrors then throwError "errors have been found"; if !failIff && !messages.hasErrors then throwError "there are no errors"; pure () open Lean.Parser @[termParser] def tst := leading_parser "(|" >> termParser >> Parser.optional (symbol ", " >> termParser) >> "|)" def tst2 : Parser := symbol "(||" >> termParser >> symbol "||)" @[termParser] def boo : ParserDescr := ParserDescr.node `boo 10 (ParserDescr.binary `andthen (ParserDescr.symbol "[|") (ParserDescr.binary `andthen (ParserDescr.cat `term 0) (ParserDescr.symbol "|]"))) @[termParser] def boo2 : ParserDescr := ParserDescr.node `boo2 10 (ParserDescr.parser `tst2) open Lean.Elab.Term @[termElab tst] def elabTst : TermElab := adaptExpander $ fun stx => match stx with | `((| $e |)) => pure e | _ => throwUnsupportedSyntax @[termElab boo] def elabBoo : TermElab := fun stx expected? => elabTerm (stx.getArg 1) expected? @[termElab boo2] def elabBool2 : TermElab := adaptExpander $ fun stx => match stx with | `((|| $e ||)) => `($e + 1) | _ => throwUnsupportedSyntax #eval runCore "#check [| @id.{1} Nat |]" #eval runCore "#check (| id 1 |)" #eval runCore "#check (|| id 1 ||)" -- #eval run "#check (| id 1, id 1 |)" -- it will fail @[termElab tst] def elabTst2 : TermElab := adaptExpander $ fun stx => match stx with | `((| $e1, $e2 |)) => `(($e1, $e2)) | _ => throwUnsupportedSyntax -- Now both work #eval runCore "#check (| id 1 |)" #eval runCore "#check (| id 1, id 2 |)" declare_syntax_cat foo syntax "⟨|" term "|⟩" : foo syntax term : foo syntax term ">>>" term : foo syntax (name := tst3) "FOO " foo : term macro_rules | `(FOO ⟨| $t |⟩) => `($t+1) | `(FOO $t:term) => `($t) #check FOO ⟨| id 1 |⟩ #check FOO 1 #check FOO 1 >>> 2
35b32d10f09ec04cf21de0ce53647183d8507c48
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/examples/ffi/lib/lakefile.lean
cf6ac018ec02539282c4dd1e434882a84c983088
[ "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
576
lean
import Lake open System Lake DSL package ffi { srcDir := "lean" precompileModules := true } lean_lib FFI @[default_target] lean_exe test { root := `Main } target ffi.o pkg : FilePath := do let oFile := pkg.buildDir / "c" / "ffi.o" let srcJob ← inputFile <| pkg.dir / "c" / "ffi.cpp" let flags := #["-I", (← getLeanIncludeDir).toString, "-fPIC"] buildO "ffi.cpp" oFile srcJob flags "c++" extern_lib libleanffi pkg := do let name := nameToStaticLib "leanffi" let ffiO ← fetch <| pkg.target ``ffi.o buildStaticLib (pkg.nativeLibDir / name) #[ffiO]
df78de75fb5fd006a99cafe6ba309cf9e4a7dbd3
c777c32c8e484e195053731103c5e52af26a25d1
/src/probability/kernel/measurable_integral.lean
94b0146398a2460a3acf6a8ff163686d63560b8e
[ "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
14,779
lean
/- Copyright (c) 2023 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import probability.kernel.basic /-! # Measurability of the integral against a kernel The Lebesgue integral of a measurable function against a kernel is measurable. The Bochner integral is strongly measurable. ## Main statements * `measurable.lintegral_kernel_prod_right`: the function `a ↦ ∫⁻ b, f a b ∂(κ a)` is measurable, for an s-finite kernel `κ : kernel α β` and a function `f : α → β → ℝ≥0∞` such that `uncurry f` is measurable. * `measure_theory.strongly_measurable.integral_kernel_prod_right`: the function `a ↦ ∫ b, f a b ∂(κ a)` is measurable, for an s-finite kernel `κ : kernel α β` and a function `f : α → β → E` such that `uncurry f` is measurable. -/ open measure_theory probability_theory function set filter open_locale measure_theory ennreal topology variables {α β γ : Type*} {mα : measurable_space α} {mβ : measurable_space β} {mγ : measurable_space γ} {κ : kernel α β} {η : kernel (α × β) γ} {a : α} namespace probability_theory namespace kernel /-- This is an auxiliary lemma for `measurable_kernel_prod_mk_left`. -/ lemma measurable_kernel_prod_mk_left_of_finite {t : set (α × β)} (ht : measurable_set t) (hκs : ∀ a, is_finite_measure (κ a)) : measurable (λ a, κ a (prod.mk a ⁻¹' t)) := begin -- `t` is a measurable set in the product `α × β`: we use that the product σ-algebra is generated -- by boxes to prove the result by induction. refine measurable_space.induction_on_inter generate_from_prod.symm is_pi_system_prod _ _ _ _ ht, { -- case `t = ∅` simp only [preimage_empty, measure_empty, measurable_const], }, { -- case of a box: `t = t₁ ×ˢ t₂` for measurable sets `t₁` and `t₂` intros t' ht', simp only [set.mem_image2, set.mem_set_of_eq, exists_and_distrib_left] at ht', obtain ⟨t₁, ht₁, t₂, ht₂, rfl⟩ := ht', classical, simp_rw mk_preimage_prod_right_eq_if, have h_eq_ite : (λ a, κ a (ite (a ∈ t₁) t₂ ∅)) = λ a, ite (a ∈ t₁) (κ a t₂) 0, { ext1 a, split_ifs, exacts [rfl, measure_empty], }, rw h_eq_ite, exact measurable.ite ht₁ (kernel.measurable_coe κ ht₂) measurable_const }, { -- we assume that the result is true for `t` and we prove it for `tᶜ` intros t' ht' h_meas, have h_eq_sdiff : ∀ a, (prod.mk a ⁻¹' t'ᶜ) = set.univ \ (prod.mk a ⁻¹' t'), { intro a, ext1 b, simp only [mem_compl_iff, mem_preimage, mem_diff, mem_univ, true_and], }, simp_rw h_eq_sdiff, have : (λ a, κ a (set.univ \ (prod.mk a ⁻¹' t'))) = (λ a, (κ a set.univ - κ a (prod.mk a ⁻¹' t'))), { ext1 a, rw [← set.diff_inter_self_eq_diff, set.inter_univ, measure_diff (set.subset_univ _)], { exact (@measurable_prod_mk_left α β _ _ a) t' ht', }, { exact measure_ne_top _ _, }, }, rw this, exact measurable.sub (kernel.measurable_coe κ measurable_set.univ) h_meas, }, { -- we assume that the result is true for a family of disjoint sets and prove it for their union intros f h_disj hf_meas hf, have h_Union : (λ a, κ a (prod.mk a ⁻¹' ⋃ i, f i)) = λ a, κ a (⋃ i, prod.mk a ⁻¹' f i), { ext1 a, congr' with b, simp only [mem_Union, mem_preimage], }, rw h_Union, have h_tsum : (λ a, κ a (⋃ i, prod.mk a ⁻¹' f i)) = λ a, ∑' i, κ a (prod.mk a ⁻¹' f i), { ext1 a, rw measure_Union, { intros i j hij s hsi hsj b hbs, have habi : {(a, b)} ⊆ f i, by { rw set.singleton_subset_iff, exact hsi hbs, }, have habj : {(a, b)} ⊆ f j, by { rw set.singleton_subset_iff, exact hsj hbs, }, simpa only [set.bot_eq_empty, set.le_eq_subset, set.singleton_subset_iff, set.mem_empty_iff_false] using h_disj hij habi habj, }, { exact λ i, (@measurable_prod_mk_left α β _ _ a) _ (hf_meas i), }, }, rw h_tsum, exact measurable.ennreal_tsum hf, }, end lemma measurable_kernel_prod_mk_left [is_s_finite_kernel κ] {t : set (α × β)} (ht : measurable_set t) : measurable (λ a, κ a (prod.mk a ⁻¹' t)) := begin rw ← kernel_sum_seq κ, have : ∀ a, kernel.sum (seq κ) a (prod.mk a ⁻¹' t) = ∑' n, seq κ n a (prod.mk a ⁻¹' t), from λ a, kernel.sum_apply' _ _ (measurable_prod_mk_left ht), simp_rw this, refine measurable.ennreal_tsum (λ n, _), exact measurable_kernel_prod_mk_left_of_finite ht infer_instance, end lemma measurable_kernel_prod_mk_left' [is_s_finite_kernel η] {s : set (β × γ)} (hs : measurable_set s) (a : α) : measurable (λ b, η (a, b) (prod.mk b ⁻¹' s)) := begin have : ∀ b, prod.mk b ⁻¹' s = {c | ((a, b), c) ∈ {p : (α × β) × γ | (p.1.2, p.2) ∈ s}}, { intro b, refl, }, simp_rw this, refine (measurable_kernel_prod_mk_left _).comp measurable_prod_mk_left, exact (measurable_fst.snd.prod_mk measurable_snd) hs, end lemma measurable_kernel_prod_mk_right [is_s_finite_kernel κ] {s : set (β × α)} (hs : measurable_set s) : measurable (λ y, κ y ((λ x, (x, y)) ⁻¹' s)) := measurable_kernel_prod_mk_left (measurable_set_swap_iff.mpr hs) end kernel open probability_theory.kernel section lintegral variables [is_s_finite_kernel κ] [is_s_finite_kernel η] /-- Auxiliary lemma for `measurable.lintegral_kernel_prod_right`. -/ lemma kernel.measurable_lintegral_indicator_const {t : set (α × β)} (ht : measurable_set t) (c : ℝ≥0∞) : measurable (λ a, ∫⁻ b, t.indicator (function.const (α × β) c) (a, b) ∂(κ a)) := begin simp_rw lintegral_indicator_const_comp measurable_prod_mk_left ht _, exact measurable.const_mul (measurable_kernel_prod_mk_left ht) c, end /-- For an s-finite kernel `κ` and a function `f : α → β → ℝ≥0∞` which is measurable when seen as a map from `α × β` (hypothesis `measurable (uncurry f)`), the integral `a ↦ ∫⁻ b, f a b ∂(κ a)` is measurable. -/ lemma _root_.measurable.lintegral_kernel_prod_right {f : α → β → ℝ≥0∞} (hf : measurable (uncurry f)) : measurable (λ a, ∫⁻ b, f a b ∂(κ a)) := begin let F : ℕ → simple_func (α × β) ℝ≥0∞ := simple_func.eapprox (uncurry f), have h : ∀ a, (⨆ n, F n a) = uncurry f a, from simple_func.supr_eapprox_apply (uncurry f) hf, simp only [prod.forall, uncurry_apply_pair] at h, simp_rw ← h, have : ∀ a, ∫⁻ b, (⨆ n, F n (a, b)) ∂(κ a) = ⨆ n, ∫⁻ b, F n (a, b) ∂(κ a), { intro a, rw lintegral_supr, { exact λ n, (F n).measurable.comp measurable_prod_mk_left, }, { exact λ i j hij b, simple_func.monotone_eapprox (uncurry f) hij _, }, }, simp_rw this, refine measurable_supr (λ n, simple_func.induction _ _ (F n)), { intros c t ht, simp only [simple_func.const_zero, simple_func.coe_piecewise, simple_func.coe_const, simple_func.coe_zero, set.piecewise_eq_indicator], exact kernel.measurable_lintegral_indicator_const ht c, }, { intros g₁ g₂ h_disj hm₁ hm₂, simp only [simple_func.coe_add, pi.add_apply], have h_add : (λ a, ∫⁻ b, g₁ (a, b) + g₂ (a, b) ∂(κ a)) = (λ a, ∫⁻ b, g₁ (a, b) ∂(κ a)) + (λ a, ∫⁻ b, g₂ (a, b) ∂(κ a)), { ext1 a, rw [pi.add_apply, lintegral_add_left (g₁.measurable.comp measurable_prod_mk_left)], }, rw h_add, exact measurable.add hm₁ hm₂, }, end lemma _root_.measurable.lintegral_kernel_prod_right' {f : (α × β) → ℝ≥0∞} (hf : measurable f) : measurable (λ a, ∫⁻ b, f (a, b) ∂(κ a)) := begin refine measurable.lintegral_kernel_prod_right _, have : uncurry (λ (a : α) (b : β), f (a, b)) = f, { ext x, rw [← @prod.mk.eta _ _ x, uncurry_apply_pair], }, rwa this, end lemma _root_.measurable.lintegral_kernel_prod_right'' {f : β × γ → ℝ≥0∞} (hf : measurable f) : measurable (λ x, ∫⁻ y, f (x, y) ∂(η (a, x))) := begin change measurable ((λ x, ∫⁻ y, (λ u : (α × β) × γ, f (u.1.2, u.2)) (x, y) ∂(η x)) ∘ (λ x, (a, x))), refine (measurable.lintegral_kernel_prod_right' _).comp measurable_prod_mk_left, exact hf.comp (measurable_fst.snd.prod_mk measurable_snd), end lemma _root_.measurable.set_lintegral_kernel_prod_right {f : α → β → ℝ≥0∞} (hf : measurable (uncurry f)) {s : set β} (hs : measurable_set s) : measurable (λ a, ∫⁻ b in s, f a b ∂(κ a)) := by { simp_rw ← lintegral_restrict κ hs, exact hf.lintegral_kernel_prod_right } lemma _root_.measurable.lintegral_kernel_prod_left' {f : β × α → ℝ≥0∞} (hf : measurable f) : measurable (λ y, ∫⁻ x, f (x, y) ∂(κ y)) := (measurable_swap_iff.mpr hf).lintegral_kernel_prod_right' lemma _root_.measurable.lintegral_kernel_prod_left {f : β → α → ℝ≥0∞} (hf : measurable (uncurry f)) : measurable (λ y, ∫⁻ x, f x y ∂(κ y)) := hf.lintegral_kernel_prod_left' lemma _root_.measurable.set_lintegral_kernel_prod_left {f : β → α → ℝ≥0∞} (hf : measurable (uncurry f)) {s : set β} (hs : measurable_set s) : measurable (λ b, ∫⁻ a in s, f a b ∂(κ b)) := by { simp_rw ← lintegral_restrict κ hs, exact hf.lintegral_kernel_prod_left } lemma _root_.measurable.lintegral_kernel {f : β → ℝ≥0∞} (hf : measurable f) : measurable (λ a, ∫⁻ b, f b ∂(κ a)) := measurable.lintegral_kernel_prod_right (hf.comp measurable_snd) lemma _root_.measurable.set_lintegral_kernel {f : β → ℝ≥0∞} (hf : measurable f) {s : set β} (hs : measurable_set s) : measurable (λ a, ∫⁻ b in s, f b ∂(κ a)) := measurable.set_lintegral_kernel_prod_right (hf.comp measurable_snd) hs end lintegral variables {E : Type*} [normed_add_comm_group E] [is_s_finite_kernel κ] [is_s_finite_kernel η] lemma measurable_set_kernel_integrable ⦃f : α → β → E⦄ (hf : strongly_measurable (uncurry f)) : measurable_set {x | integrable (f x) (κ x)} := begin simp_rw [integrable, hf.of_uncurry_left.ae_strongly_measurable, true_and], exact measurable_set_lt (measurable.lintegral_kernel_prod_right hf.ennnorm) measurable_const end end probability_theory open probability_theory probability_theory.kernel namespace measure_theory variables {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] [is_s_finite_kernel κ] [is_s_finite_kernel η] lemma strongly_measurable.integral_kernel_prod_right ⦃f : α → β → E⦄ (hf : strongly_measurable (uncurry f)) : strongly_measurable (λ x, ∫ y, f x y ∂(κ x)) := begin classical, borelize E, haveI : topological_space.separable_space (range (uncurry f) ∪ {0} : set E) := hf.separable_space_range_union_singleton, let s : ℕ → simple_func (α × β) E := simple_func.approx_on _ hf.measurable (range (uncurry f) ∪ {0}) 0 (by simp), let s' : ℕ → α → simple_func β E := λ n x, (s n).comp (prod.mk x) measurable_prod_mk_left, let f' : ℕ → α → E := λ n, {x | integrable (f x) (κ x)}.indicator (λ x, (s' n x).integral (κ x)), have hf' : ∀ n, strongly_measurable (f' n), { intro n, refine strongly_measurable.indicator _ (measurable_set_kernel_integrable hf), have : ∀ x, (s' n x).range.filter (λ x, x ≠ 0) ⊆ (s n).range, { intros x, refine finset.subset.trans (finset.filter_subset _ _) _, intro y, simp_rw [simple_func.mem_range], rintro ⟨z, rfl⟩, exact ⟨(x, z), rfl⟩ }, simp only [simple_func.integral_eq_sum_of_subset (this _)], refine finset.strongly_measurable_sum _ (λ x _, _), refine (measurable.ennreal_to_real _).strongly_measurable.smul_const _, simp only [simple_func.coe_comp, preimage_comp] {single_pass := tt}, apply measurable_kernel_prod_mk_left, exact (s n).measurable_set_fiber x }, have h2f' : tendsto f' at_top (𝓝 (λ (x : α), ∫ (y : β), f x y ∂(κ x))), { rw [tendsto_pi_nhds], intro x, by_cases hfx : integrable (f x) (κ x), { have : ∀ n, integrable (s' n x) (κ x), { intro n, apply (hfx.norm.add hfx.norm).mono' (s' n x).ae_strongly_measurable, apply eventually_of_forall, intro y, simp_rw [s', simple_func.coe_comp], exact simple_func.norm_approx_on_zero_le _ _ (x, y) n }, simp only [f', hfx, simple_func.integral_eq_integral _ (this _), indicator_of_mem, mem_set_of_eq], refine tendsto_integral_of_dominated_convergence (λ y, ‖f x y‖ + ‖f x y‖) (λ n, (s' n x).ae_strongly_measurable) (hfx.norm.add hfx.norm) _ _, { exact λ n, eventually_of_forall (λ y, simple_func.norm_approx_on_zero_le _ _ (x, y) n) }, { refine eventually_of_forall (λ y, simple_func.tendsto_approx_on _ _ _), apply subset_closure, simp [-uncurry_apply_pair], } }, { simp [f', hfx, integral_undef], } }, exact strongly_measurable_of_tendsto _ hf' h2f', end lemma strongly_measurable.integral_kernel_prod_right' ⦃f : α × β → E⦄ (hf : strongly_measurable f) : strongly_measurable (λ x, ∫ y, f (x, y) ∂(κ x)) := by { rw [← uncurry_curry f] at hf, exact hf.integral_kernel_prod_right } lemma strongly_measurable.integral_kernel_prod_right'' {f : β × γ → E} (hf : strongly_measurable f) : strongly_measurable (λ x, ∫ y, f (x, y) ∂(η (a, x))) := begin change strongly_measurable ((λ x, ∫ y, (λ u : (α × β) × γ, f (u.1.2, u.2)) (x, y) ∂(η x)) ∘ (λ x, (a, x))), refine strongly_measurable.comp_measurable _ measurable_prod_mk_left, refine measure_theory.strongly_measurable.integral_kernel_prod_right' _, exact hf.comp_measurable (measurable_fst.snd.prod_mk measurable_snd), end lemma strongly_measurable.integral_kernel_prod_left ⦃f : β → α → E⦄ (hf : strongly_measurable (uncurry f)) : strongly_measurable (λ y, ∫ x, f x y ∂(κ y)) := (hf.comp_measurable measurable_swap).integral_kernel_prod_right' lemma strongly_measurable.integral_kernel_prod_left' ⦃f : β × α → E⦄ (hf : strongly_measurable f) : strongly_measurable (λ y, ∫ x, f (x, y) ∂(κ y)) := (hf.comp_measurable measurable_swap).integral_kernel_prod_right' lemma strongly_measurable.integral_kernel_prod_left'' {f : γ × β → E} (hf : strongly_measurable f) : strongly_measurable (λ y, ∫ x, f (x, y) ∂(η (a, y))) := begin change strongly_measurable ((λ y, ∫ x, (λ u : γ × (α × β), f (u.1, u.2.2)) (x, y) ∂(η y)) ∘ (λ x, (a, x))), refine strongly_measurable.comp_measurable _ measurable_prod_mk_left, refine measure_theory.strongly_measurable.integral_kernel_prod_left' _, exact hf.comp_measurable (measurable_fst.prod_mk measurable_snd.snd), end end measure_theory
333fdf71b078016a9e52007a99034ae956b522c4
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/fintype/prod.lean
a307a74dddf4bda0fd73fdd1db905511ee8b2da0
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
3,181
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.fintype.card import data.finset.prod /-! # fintype instance for the product of two fintypes. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ open function open_locale nat universes u v variables {α β γ : Type*} open finset function namespace set variables {s t : set α} lemma to_finset_prod (s : set α) (t : set β) [fintype s] [fintype t] [fintype (s ×ˢ t)] : (s ×ˢ t).to_finset = s.to_finset ×ˢ t.to_finset := by { ext, simp } lemma to_finset_off_diag {s : set α} [decidable_eq α] [fintype s] [fintype s.off_diag] : s.off_diag.to_finset = s.to_finset.off_diag := finset.ext $ by simp end set instance (α β : Type*) [fintype α] [fintype β] : fintype (α × β) := ⟨univ ×ˢ univ, λ ⟨a, b⟩, by simp⟩ @[simp] lemma finset.univ_product_univ {α β : Type*} [fintype α] [fintype β] : (univ : finset α) ×ˢ (univ : finset β) = univ := rfl @[simp] theorem fintype.card_prod (α β : Type*) [fintype α] [fintype β] : fintype.card (α × β) = fintype.card α * fintype.card β := card_product _ _ section open_locale classical @[simp] lemma infinite_prod : infinite (α × β) ↔ infinite α ∧ nonempty β ∨ nonempty α ∧ infinite β := begin refine ⟨λ H, _, λ H, H.elim (and_imp.2 $ @prod.infinite_of_left α β) (and_imp.2 $ @prod.infinite_of_right α β)⟩, rw and.comm, contrapose! H, introI H', rcases infinite.nonempty (α × β) with ⟨a, b⟩, haveI := fintype_of_not_infinite (H.1 ⟨b⟩), haveI := fintype_of_not_infinite (H.2 ⟨a⟩), exact H'.false end instance pi.infinite_of_left {ι : Sort*} {π : ι → Sort*} [∀ i, nontrivial $ π i] [infinite ι] : infinite (Π i : ι, π i) := begin choose m n hm using λ i, exists_pair_ne (π i), refine infinite.of_injective (λ i, m.update i (n i)) (λ x y h, not_not.1 $ λ hne, _), simp_rw [update_eq_iff, update_noteq hne] at h, exact (hm x h.1.symm).elim, end /-- If at least one `π i` is infinite and the rest nonempty, the pi type of all `π` is infinite. -/ lemma pi.infinite_of_exists_right {ι : Type*} {π : ι → Type*} (i : ι) [infinite $ π i] [∀ i, nonempty $ π i] : infinite (Π i : ι, π i) := let ⟨m⟩ := @pi.nonempty ι π _ in infinite.of_injective _ (update_injective m i) /-- See `pi.infinite_of_exists_right` for the case that only one `π i` is infinite. -/ instance pi.infinite_of_right {ι : Sort*} {π : ι → Sort*} [∀ i, infinite $ π i] [nonempty ι] : infinite (Π i : ι, π i) := pi.infinite_of_exists_right (classical.arbitrary ι) /-- Non-dependent version of `pi.infinite_of_left`. -/ instance function.infinite_of_left {ι π : Sort*} [nontrivial π] [infinite ι] : infinite (ι → π) := pi.infinite_of_left /-- Non-dependent version of `pi.infinite_of_exists_right` and `pi.infinite_of_right`. -/ instance function.infinite_of_right {ι π : Sort*} [infinite π] [nonempty ι] : infinite (ι → π) := pi.infinite_of_right end
972ed2fdb5285ab336fd75b181a223ed5a32f8c7
fe5d16cef63488c5ef3f73be54a0981243288d1d
/tests/examplefiles/lean/test.lean
4b719e13acbb371d87b498f1563202795f490245
[ "BSD-2-Clause" ]
permissive
sailfishos-mirror/pygments
648bdafcd0cb8128dd684b2eb59b3bbee23f752a
71cbc18df9f5e3852b37bb7f00f836e480a96a73
refs/heads/master
1,693,450,886,577
1,692,365,125,000
1,692,365,125,000
266,720,343
0
1
BSD-2-Clause
1,669,229,703,000
1,590,393,909,000
Python
UTF-8
Lean
false
false
8,706
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 Zorn's lemmas. Ported from Isabelle/HOL (written by Jacques D. Fleuriot, Tobias Nipkow, and Christian Sternagel). -/ import data.set.lattice noncomputable theory universes u open set classical local attribute [instance] decidable_inhabited local attribute [instance] prop_decidable namespace zorn section chain parameters {α : Type u} {r : α → α → Prop} local infix ` ≺ `:50 := r def chain (c : set α) := pairwise_on c (λx y, x ≺ y ∨ y ≺ x) theorem chain_insert {c : set α} {a : α} (hc : chain c) (ha : ∀b∈c, b ≠ a → a ≺ b ∨ b ≺ a) : chain (insert a c) := forall_insert_of_forall (assume x hx, forall_insert_of_forall (hc x hx) (assume hneq, (ha x hx hneq).symm)) (forall_insert_of_forall (assume x hx hneq, ha x hx $ assume h', hneq h'.symm) (assume h, (h rfl).rec _)) def super_chain (c₁ c₂ : set α) := chain c₂ ∧ c₁ ⊂ c₂ def is_max_chain (c : set α) := chain c ∧ ¬ (∃c', super_chain c c') def succ_chain (c : set α) := if h : ∃c', chain c ∧ super_chain c c' then some h else c theorem succ_spec {c : set α} (h : ∃c', chain c ∧ super_chain c c') : super_chain c (succ_chain c) := let ⟨c', hc'⟩ := h in have chain c ∧ super_chain c (some h), from @some_spec _ (λc', chain c ∧ super_chain c c') _, by simp [succ_chain, dif_pos, h, this.right] theorem chain_succ {c : set α} (hc : chain c) : chain (succ_chain c) := if h : ∃c', chain c ∧ super_chain c c' then (succ_spec h).left else by simp [succ_chain, dif_neg, h]; exact hc theorem super_of_not_max {c : set α} (hc₁ : chain c) (hc₂ : ¬ is_max_chain c) : super_chain c (succ_chain c) := begin simp [is_max_chain, not_and_iff, not_not_iff] at hc₂, exact have ∃c', super_chain c c', from hc₂.neg_resolve_left hc₁, let ⟨c', hc'⟩ := this in show super_chain c (succ_chain c), from succ_spec ⟨c', hc₁, hc'⟩ end theorem succ_increasing {c : set α} : c ⊆ succ_chain c := if h : ∃c', chain c ∧ super_chain c c' then have super_chain c (succ_chain c), from succ_spec h, this.right.left else by simp [succ_chain, dif_neg, h, subset.refl] inductive chain_closure : set α → Prop | succ : ∀{s}, chain_closure s → chain_closure (succ_chain s) | union : ∀{s}, (∀a∈s, chain_closure a) → chain_closure (⋃₀ s) theorem chain_closure_empty : chain_closure ∅ := have chain_closure (⋃₀ ∅), from chain_closure.union $ assume a h, h.rec _, by simp at this; assumption theorem chain_closure_closure : chain_closure (⋃₀ chain_closure) := chain_closure.union $ assume s hs, hs variables {c c₁ c₂ c₃ : set α} private lemma chain_closure_succ_total_aux (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h : ∀{c₃}, chain_closure c₃ → c₃ ⊆ c₂ → c₂ = c₃ ∨ succ_chain c₃ ⊆ c₂) : c₁ ⊆ c₂ ∨ succ_chain c₂ ⊆ c₁ := begin induction hc₁, case _root_.zorn.chain_closure.succ c₃ hc₃ ih { cases ih with ih ih, { have h := h hc₃ ih, cases h with h h, { exact or.inr (h ▸ subset.refl _) }, { exact or.inl h } }, { exact or.inr (subset.trans ih succ_increasing) } }, case _root_.zorn.chain_closure.union s hs ih { refine (or_of_not_implies' $ λ hn, sUnion_subset $ λ a ha, _), apply (ih a ha).resolve_right, apply mt (λ h, _) hn, exact subset.trans h (subset_sUnion_of_mem ha) } end private lemma chain_closure_succ_total (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h : c₁ ⊆ c₂) : c₂ = c₁ ∨ succ_chain c₁ ⊆ c₂ := begin induction hc₂ generalizing c₁ hc₁ h, case _root_.zorn.chain_closure.succ c₂ hc₂ ih { have h₁ : c₁ ⊆ c₂ ∨ @succ_chain α r c₂ ⊆ c₁ := (chain_closure_succ_total_aux hc₁ hc₂ $ assume c₁, ih), cases h₁ with h₁ h₁, { have h₂ := ih hc₁ h₁, cases h₂ with h₂ h₂, { exact (or.inr $ h₂ ▸ subset.refl _) }, { exact (or.inr $ subset.trans h₂ succ_increasing) } }, { exact (or.inl $ subset.antisymm h₁ h) } }, case _root_.zorn.chain_closure.union s hs ih { apply or.imp (assume h', subset.antisymm h' h) id, apply classical.by_contradiction, simp [not_or_iff, sUnion_subset_iff, classical.not_forall_iff, not_implies_iff], intro h, cases h with h₁ h₂, cases h₂ with c₃ h₂, cases h₂ with h₂ hc₃, have h := chain_closure_succ_total_aux hc₁ (hs c₃ hc₃) (assume c₄, ih _ hc₃), cases h with h h, { have h' := ih c₃ hc₃ hc₁ h, cases h' with h' h', { exact (h₂ $ h' ▸ subset.refl _) }, { exact (h₁ $ subset.trans h' $ subset_sUnion_of_mem hc₃) } }, { exact (h₂ $ subset.trans succ_increasing h) } } end theorem chain_closure_total (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) : c₁ ⊆ c₂ ∨ c₂ ⊆ c₁ := have c₁ ⊆ c₂ ∨ succ_chain c₂ ⊆ c₁, from chain_closure_succ_total_aux hc₁ hc₂ $ assume c₃ hc₃, chain_closure_succ_total hc₃ hc₂, or.imp_right (assume : succ_chain c₂ ⊆ c₁, subset.trans succ_increasing this) this theorem chain_closure_succ_fixpoint (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h_eq : succ_chain c₂ = c₂) : c₁ ⊆ c₂ := begin induction hc₁, case _root_.zorn.chain_closure.succ c₁ hc₁ h { exact or.elim (chain_closure_succ_total hc₁ hc₂ h) (assume h, h ▸ h_eq.symm ▸ subset.refl c₂) id }, case _root_.zorn.chain_closure.union s hs ih { exact (sUnion_subset $ assume c₁ hc₁, ih c₁ hc₁) } end theorem chain_closure_succ_fixpoint_iff (hc : chain_closure c) : succ_chain c = c ↔ c = ⋃₀ chain_closure := ⟨assume h, subset.antisymm (subset_sUnion_of_mem hc) (chain_closure_succ_fixpoint chain_closure_closure hc h), assume : c = ⋃₀{c : set α | chain_closure c}, subset.antisymm (calc succ_chain c ⊆ ⋃₀{c : set α | chain_closure c} : subset_sUnion_of_mem $ chain_closure.succ hc ... = c : this.symm) succ_increasing⟩ theorem chain_chain_closure (hc : chain_closure c) : chain c := begin induction hc, case _root_.zorn.chain_closure.succ c hc h { exact chain_succ h }, case _root_.zorn.chain_closure.union s hs h { have h : ∀c∈s, zorn.chain c := h, exact assume c₁ ⟨t₁, ht₁, (hc₁ : c₁ ∈ t₁)⟩ c₂ ⟨t₂, ht₂, (hc₂ : c₂ ∈ t₂)⟩ hneq, have t₁ ⊆ t₂ ∨ t₂ ⊆ t₁, from chain_closure_total (hs _ ht₁) (hs _ ht₂), or.elim this (assume : t₁ ⊆ t₂, h t₂ ht₂ c₁ (this hc₁) c₂ hc₂ hneq) (assume : t₂ ⊆ t₁, h t₁ ht₁ c₁ hc₁ c₂ (this hc₂) hneq) } end def max_chain := ⋃₀ chain_closure /-- Hausdorff's maximality principle There exists a maximal totally ordered subset of `α`. Note that we do not require `α` to be partially ordered by `r`. -/ theorem max_chain_spec : is_max_chain max_chain := classical.by_contradiction $ assume : ¬ is_max_chain (⋃₀ chain_closure), have super_chain (⋃₀ chain_closure) (succ_chain (⋃₀ chain_closure)), from super_of_not_max (chain_chain_closure chain_closure_closure) this, let ⟨h₁, h₂, (h₃ : (⋃₀ chain_closure) ≠ succ_chain (⋃₀ chain_closure))⟩ := this in have succ_chain (⋃₀ chain_closure) = (⋃₀ chain_closure), from (chain_closure_succ_fixpoint_iff chain_closure_closure).mpr rfl, h₃ this.symm /-- Zorn's lemma If every chain has an upper bound, then there is a maximal element -/ theorem zorn (h : ∀c, chain c → ∃ub, ∀a∈c, a ≺ ub) (trans : ∀{a b c}, a ≺ b → b ≺ c → a ≺ c) : ∃m, ∀a, m ≺ a → a ≺ m := have ∃ub, ∀a∈max_chain, a ≺ ub, from h _ $ max_chain_spec.left, let ⟨ub, (hub : ∀a∈max_chain, a ≺ ub)⟩ := this in ⟨ub, assume a ha, have chain (insert a max_chain), from chain_insert max_chain_spec.left $ assume b hb _, or.inr $ trans (hub b hb) ha, have a ∈ max_chain, from classical.by_contradiction $ assume h : a ∉ max_chain, max_chain_spec.right $ ⟨insert a max_chain, this, ssubset_insert h⟩, hub a this⟩ end chain theorem zorn_weak_order {α : Type u} [weak_order α] (h : ∀c:set α, @chain α (≤) c → ∃ub, ∀a∈c, a ≤ ub) : ∃m:α, ∀a, m ≤ a → a = m := let ⟨m, hm⟩ := @zorn α (≤) h (assume a b c, le_trans) in ⟨m, assume a ha, le_antisymm (hm a ha) ha⟩ end zorn -- other bits of tricky syntax @[to_additive "See note [foo]"] lemma mul_one : sorry := sorry
31f2f40a41e4fd87e23cd91810f59ed81bd7148a
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/data/real/ennreal.lean
83cab09840b0b86cb4ecdb16cac10447d7bc912d
[ "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
60,092
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov -/ import data.real.nnreal import data.set.intervals /-! # Extended non-negative reals We define `ennreal = ℝ≥0∞ := with_no ℝ≥0` to be the type of extended nonnegative real numbers, i.e., the interval `[0, +∞]`. This type is used as the codomain of a `measure_theory.measure`, and of the extended distance `edist` in a `emetric_space`. In this file we define some algebraic operations and a linear order on `ℝ≥0∞` and prove basic properties of these operations, order, and conversions to/from `ℝ`, `ℝ≥0`, and `ℕ`. ## Main definitions * `ℝ≥0∞`: the extended nonnegative real numbers `[0, ∞]`; defined as `with_top ℝ≥0`; it is equipped with the following structures: - coercion from `ℝ≥0` defined in the natural way; - the natural structure of a complete dense linear order: `↑p ≤ ↑q ↔ p ≤ q` and `∀ a, a ≤ ∞`; - `a + b` is defined so that `↑p + ↑q = ↑(p + q)` for `(p q : ℝ≥0)` and `a + ∞ = ∞ + a = ∞`; - `a * b` is defined so that `↑p * ↑q = ↑(p * q)` for `(p q : ℝ≥0)`, `0 * ∞ = ∞ * 0 = 0`, and `a * ∞ = ∞ * a = ∞` for `a ≠ 0`; - `a - b` is defined as the minimal `d` such that `a ≤ d + b`; this way we have `↑p - ↑q = ↑(p - q)`, `∞ - ↑p = ∞`, `↑p - ∞ = ∞ - ∞ = 0`; note that there is no negation, only subtraction; - `a⁻¹` is defined as `Inf {b | 1 ≤ a * b}`. This way we have `(↑p)⁻¹ = ↑(p⁻¹)` for `p : ℝ≥0`, `p ≠ 0`, `0⁻¹ = ∞`, and `∞⁻¹ = 0`. - `a / b` is defined as `a * b⁻¹`. The addition and multiplication defined this way together with `0 = ↑0` and `1 = ↑1` turn `ℝ≥0∞` into a canonically ordered commutative semiring of characteristic zero. * Coercions to/from other types: - coercion `ℝ≥0 → ℝ≥0∞` is defined as `has_coe`, so one can use `(p : ℝ≥0)` in a context that expects `a : ℝ≥0∞`, and Lean will apply `coe` automatically; - `ennreal.to_nnreal` sends `↑p` to `p` and `∞` to `0`; - `ennreal.to_real := coe ∘ ennreal.to_nnreal` sends `↑p`, `p : ℝ≥0` to `(↑p : ℝ)` and `∞` to `0`; - `ennreal.of_real := coe ∘ nnreal.of_real` sends `x : ℝ` to `↑⟨max x 0, _⟩` - `ennreal.ne_top_equiv_nnreal` is an equivalence between `{a : ℝ≥0∞ // a ≠ 0}` and `ℝ≥0`. ## Implementation notes We define a `can_lift ℝ≥0∞ ℝ≥0` instance, so one of the ways to prove theorems about an `ℝ≥0∞` number `a` is to consider the cases `a = ∞` and `a ≠ ∞`, and use the tactic `lift a to ℝ≥0 using ha` in the second case. This instance is even more useful if one already has `ha : a ≠ ∞` in the context, or if we have `(f : α → ℝ≥0∞) (hf : ∀ x, f x ≠ ∞)`. ## Notations * `ℝ≥0∞`: the type of the extended nonnegative real numbers; * `ℝ≥0`: the type of nonnegative real numbers `[0, ∞)`; defined in `data.real.nnreal`; * `∞`: a localized notation in `ℝ≥0∞` for `⊤ : ℝ≥0∞`. -/ noncomputable theory open classical set open_locale classical big_operators nnreal variables {α : Type*} {β : Type*} /-- The extended nonnegative real numbers. This is usually denoted [0, ∞], and is relevant as the codomain of a measure. -/ @[derive canonically_ordered_comm_semiring, derive complete_linear_order, derive densely_ordered, derive nontrivial] def ennreal := with_top ℝ≥0 localized "notation `ℝ≥0∞` := ennreal" in ennreal localized "notation `∞` := (⊤ : ennreal)" in ennreal instance : linear_ordered_add_comm_monoid ℝ≥0∞ := { .. ennreal.canonically_ordered_comm_semiring, .. ennreal.complete_linear_order } namespace ennreal variables {a b c d : ℝ≥0∞} {r p q : ℝ≥0} instance : inhabited ℝ≥0∞ := ⟨0⟩ instance : has_coe ℝ≥0 ℝ≥0∞ := ⟨ option.some ⟩ instance : can_lift ℝ≥0∞ ℝ≥0 := { coe := coe, cond := λ r, r ≠ ∞, prf := λ x hx, ⟨option.get $ option.ne_none_iff_is_some.1 hx, option.some_get _⟩ } @[simp] lemma none_eq_top : (none : ℝ≥0∞) = ∞ := rfl @[simp] lemma some_eq_coe (a : ℝ≥0) : (some a : ℝ≥0∞) = (↑a : ℝ≥0∞) := rfl /-- `to_nnreal x` returns `x` if it is real, otherwise 0. -/ protected def to_nnreal : ℝ≥0∞ → ℝ≥0 | (some r) := r | none := 0 /-- `to_real x` returns `x` if it is real, `0` otherwise. -/ protected def to_real (a : ℝ≥0∞) : real := coe (a.to_nnreal) /-- `of_real x` returns `x` if it is nonnegative, `0` otherwise. -/ protected def of_real (r : real) : ℝ≥0∞ := coe (nnreal.of_real r) @[simp, norm_cast] lemma to_nnreal_coe : (r : ℝ≥0∞).to_nnreal = r := rfl @[simp] lemma coe_to_nnreal : ∀{a:ℝ≥0∞}, a ≠ ∞ → ↑(a.to_nnreal) = a | (some r) h := rfl | none h := (h rfl).elim @[simp] lemma of_real_to_real {a : ℝ≥0∞} (h : a ≠ ∞) : ennreal.of_real (a.to_real) = a := by simp [ennreal.to_real, ennreal.of_real, h] @[simp] lemma to_real_of_real {r : ℝ} (h : 0 ≤ r) : ennreal.to_real (ennreal.of_real r) = r := by simp [ennreal.to_real, ennreal.of_real, nnreal.coe_of_real _ h] lemma to_real_of_real' {r : ℝ} : ennreal.to_real (ennreal.of_real r) = max r 0 := rfl lemma coe_to_nnreal_le_self : ∀{a:ℝ≥0∞}, ↑(a.to_nnreal) ≤ a | (some r) := by rw [some_eq_coe, to_nnreal_coe]; exact le_refl _ | none := le_top lemma coe_nnreal_eq (r : ℝ≥0) : (r : ℝ≥0∞) = ennreal.of_real r := by { rw [ennreal.of_real, nnreal.of_real], cases r with r h, congr, dsimp, rw max_eq_left h } lemma of_real_eq_coe_nnreal {x : ℝ} (h : 0 ≤ x) : ennreal.of_real x = @coe ℝ≥0 ℝ≥0∞ _ (⟨x, h⟩ : ℝ≥0) := by { rw [coe_nnreal_eq], refl } @[simp] lemma of_real_coe_nnreal : ennreal.of_real p = p := (coe_nnreal_eq p).symm @[simp, norm_cast] lemma coe_zero : ↑(0 : ℝ≥0) = (0 : ℝ≥0∞) := rfl @[simp, norm_cast] lemma coe_one : ↑(1 : ℝ≥0) = (1 : ℝ≥0∞) := rfl @[simp] lemma to_real_nonneg {a : ℝ≥0∞} : 0 ≤ a.to_real := by simp [ennreal.to_real] @[simp] lemma top_to_nnreal : ∞.to_nnreal = 0 := rfl @[simp] lemma top_to_real : ∞.to_real = 0 := rfl @[simp] lemma one_to_real : (1 : ℝ≥0∞).to_real = 1 := rfl @[simp] lemma one_to_nnreal : (1 : ℝ≥0∞).to_nnreal = 1 := rfl @[simp] lemma coe_to_real (r : ℝ≥0) : (r : ℝ≥0∞).to_real = r := rfl @[simp] lemma zero_to_nnreal : (0 : ℝ≥0∞).to_nnreal = 0 := rfl @[simp] lemma zero_to_real : (0 : ℝ≥0∞).to_real = 0 := rfl @[simp] lemma of_real_zero : ennreal.of_real (0 : ℝ) = 0 := by simp [ennreal.of_real]; refl @[simp] lemma of_real_one : ennreal.of_real (1 : ℝ) = (1 : ℝ≥0∞) := by simp [ennreal.of_real] lemma of_real_to_real_le {a : ℝ≥0∞} : ennreal.of_real (a.to_real) ≤ a := if ha : a = ∞ then ha.symm ▸ le_top else le_of_eq (of_real_to_real ha) lemma forall_ennreal {p : ℝ≥0∞ → Prop} : (∀a, p a) ↔ (∀r:ℝ≥0, p r) ∧ p ∞ := ⟨assume h, ⟨assume r, h _, h _⟩, assume ⟨h₁, h₂⟩ a, match a with some r := h₁ _ | none := h₂ end⟩ lemma forall_ne_top {p : ℝ≥0∞ → Prop} : (∀ a ≠ ∞, p a) ↔ ∀ r : ℝ≥0, p r := option.ball_ne_none lemma exists_ne_top {p : ℝ≥0∞ → Prop} : (∃ a ≠ ∞, p a) ↔ ∃ r : ℝ≥0, p r := option.bex_ne_none lemma to_nnreal_eq_zero_iff (x : ℝ≥0∞) : x.to_nnreal = 0 ↔ x = 0 ∨ x = ∞ := ⟨begin cases x, { simp [none_eq_top] }, { have A : some (0:ℝ≥0) = (0:ℝ≥0∞) := rfl, simp [ennreal.to_nnreal, A] {contextual := tt} } end, by intro h; cases h; simp [h]⟩ lemma to_real_eq_zero_iff (x : ℝ≥0∞) : x.to_real = 0 ↔ x = 0 ∨ x = ∞ := by simp [ennreal.to_real, to_nnreal_eq_zero_iff] @[simp] lemma coe_ne_top : (r : ℝ≥0∞) ≠ ∞ := with_top.coe_ne_top @[simp] lemma top_ne_coe : ∞ ≠ (r : ℝ≥0∞) := with_top.top_ne_coe @[simp] lemma of_real_ne_top {r : ℝ} : ennreal.of_real r ≠ ∞ := by simp [ennreal.of_real] @[simp] lemma of_real_lt_top {r : ℝ} : ennreal.of_real r < ∞ := lt_top_iff_ne_top.2 of_real_ne_top @[simp] lemma top_ne_of_real {r : ℝ} : ∞ ≠ ennreal.of_real r := by simp [ennreal.of_real] @[simp] lemma zero_ne_top : 0 ≠ ∞ := coe_ne_top @[simp] lemma top_ne_zero : ∞ ≠ 0 := top_ne_coe @[simp] lemma one_ne_top : 1 ≠ ∞ := coe_ne_top @[simp] lemma top_ne_one : ∞ ≠ 1 := top_ne_coe @[simp, norm_cast] lemma coe_eq_coe : (↑r : ℝ≥0∞) = ↑q ↔ r = q := with_top.coe_eq_coe @[simp, norm_cast] lemma coe_le_coe : (↑r : ℝ≥0∞) ≤ ↑q ↔ r ≤ q := with_top.coe_le_coe @[simp, norm_cast] lemma coe_lt_coe : (↑r : ℝ≥0∞) < ↑q ↔ r < q := with_top.coe_lt_coe lemma coe_mono : monotone (coe : ℝ≥0 → ℝ≥0∞) := λ _ _, coe_le_coe.2 @[simp, norm_cast] lemma coe_eq_zero : (↑r : ℝ≥0∞) = 0 ↔ r = 0 := coe_eq_coe @[simp, norm_cast] lemma zero_eq_coe : 0 = (↑r : ℝ≥0∞) ↔ 0 = r := coe_eq_coe @[simp, norm_cast] lemma coe_eq_one : (↑r : ℝ≥0∞) = 1 ↔ r = 1 := coe_eq_coe @[simp, norm_cast] lemma one_eq_coe : 1 = (↑r : ℝ≥0∞) ↔ 1 = r := coe_eq_coe @[simp, norm_cast] lemma coe_nonneg : 0 ≤ (↑r : ℝ≥0∞) ↔ 0 ≤ r := coe_le_coe @[simp, norm_cast] lemma coe_pos : 0 < (↑r : ℝ≥0∞) ↔ 0 < r := coe_lt_coe @[simp, norm_cast] lemma coe_add : ↑(r + p) = (r + p : ℝ≥0∞) := with_top.coe_add @[simp, norm_cast] lemma coe_mul : ↑(r * p) = (r * p : ℝ≥0∞) := with_top.coe_mul @[simp, norm_cast] lemma coe_bit0 : (↑(bit0 r) : ℝ≥0∞) = bit0 r := coe_add @[simp, norm_cast] lemma coe_bit1 : (↑(bit1 r) : ℝ≥0∞) = bit1 r := by simp [bit1] lemma coe_two : ((2:ℝ≥0) : ℝ≥0∞) = 2 := by norm_cast protected lemma zero_lt_one : 0 < (1 : ℝ≥0∞) := canonically_ordered_semiring.zero_lt_one @[simp] lemma one_lt_two : (1 : ℝ≥0∞) < 2 := coe_one ▸ coe_two ▸ by exact_mod_cast (@one_lt_two ℕ _ _) @[simp] lemma zero_lt_two : (0:ℝ≥0∞) < 2 := lt_trans ennreal.zero_lt_one one_lt_two lemma two_ne_zero : (2:ℝ≥0∞) ≠ 0 := (ne_of_lt zero_lt_two).symm lemma two_ne_top : (2:ℝ≥0∞) ≠ ∞ := coe_two ▸ coe_ne_top /-- The set of numbers in `ℝ≥0∞` that are not equal to `∞` is equivalent to `ℝ≥0`. -/ def ne_top_equiv_nnreal : {a | a ≠ ∞} ≃ ℝ≥0 := { to_fun := λ x, ennreal.to_nnreal x, inv_fun := λ x, ⟨x, coe_ne_top⟩, left_inv := λ ⟨x, hx⟩, subtype.eq $ coe_to_nnreal hx, right_inv := λ x, to_nnreal_coe } lemma cinfi_ne_top [has_Inf α] (f : ℝ≥0∞ → α) : (⨅ x : {x // x ≠ ∞}, f x) = ⨅ x : ℝ≥0, f x := eq.symm $ infi_congr _ ne_top_equiv_nnreal.symm.surjective $ λ x, rfl lemma infi_ne_top [complete_lattice α] (f : ℝ≥0∞ → α) : (⨅ x ≠ ∞, f x) = ⨅ x : ℝ≥0, f x := by rw [infi_subtype', cinfi_ne_top] lemma csupr_ne_top [has_Sup α] (f : ℝ≥0∞ → α) : (⨆ x : {x // x ≠ ∞}, f x) = ⨆ x : ℝ≥0, f x := @cinfi_ne_top (order_dual α) _ _ lemma supr_ne_top [complete_lattice α] (f : ℝ≥0∞ → α) : (⨆ x ≠ ∞, f x) = ⨆ x : ℝ≥0, f x := @infi_ne_top (order_dual α) _ _ lemma infi_ennreal {α : Type*} [complete_lattice α] {f : ℝ≥0∞ → α} : (⨅ n, f n) = (⨅ n : ℝ≥0, f n) ⊓ f ∞ := le_antisymm (le_inf (le_infi $ assume i, infi_le _ _) (infi_le _ _)) (le_infi $ forall_ennreal.2 ⟨assume r, inf_le_left_of_le $ infi_le _ _, inf_le_right⟩) lemma supr_ennreal {α : Type*} [complete_lattice α] {f : ℝ≥0∞ → α} : (⨆ n, f n) = (⨆ n : ℝ≥0, f n) ⊔ f ∞ := @infi_ennreal (order_dual α) _ _ @[simp] lemma add_top : a + ∞ = ∞ := with_top.add_top @[simp] lemma top_add : ∞ + a = ∞ := with_top.top_add /-- Coercion `ℝ≥0 → ℝ≥0∞` as a `ring_hom`. -/ def of_nnreal_hom : ℝ≥0 →+* ℝ≥0∞ := ⟨coe, coe_one, λ _ _, coe_mul, coe_zero, λ _ _, coe_add⟩ @[simp] lemma coe_of_nnreal_hom : ⇑of_nnreal_hom = coe := rfl @[simp, norm_cast] lemma coe_indicator {α} (s : set α) (f : α → ℝ≥0) (a : α) : ((s.indicator f a : ℝ≥0) : ℝ≥0∞) = s.indicator (λ x, f x) a := (of_nnreal_hom : ℝ≥0 →+ ℝ≥0∞).map_indicator _ _ _ @[simp, norm_cast] lemma coe_pow (n : ℕ) : (↑(r^n) : ℝ≥0∞) = r^n := of_nnreal_hom.map_pow r n @[simp] lemma add_eq_top : a + b = ∞ ↔ a = ∞ ∨ b = ∞ := with_top.add_eq_top @[simp] lemma add_lt_top : a + b < ∞ ↔ a < ∞ ∧ b < ∞ := with_top.add_lt_top lemma to_nnreal_add {r₁ r₂ : ℝ≥0∞} (h₁ : r₁ < ∞) (h₂ : r₂ < ∞) : (r₁ + r₂).to_nnreal = r₁.to_nnreal + r₂.to_nnreal := begin rw [← coe_eq_coe, coe_add, coe_to_nnreal, coe_to_nnreal, coe_to_nnreal]; apply @ne_top_of_lt ℝ≥0∞ _ _ ∞, exact h₂, exact h₁, exact add_lt_top.2 ⟨h₁, h₂⟩ end /- rw has trouble with the generic lt_top_iff_ne_top and bot_lt_iff_ne_bot (contrary to erw). This is solved with the next lemmas -/ protected lemma lt_top_iff_ne_top : a < ∞ ↔ a ≠ ∞ := lt_top_iff_ne_top protected lemma bot_lt_iff_ne_bot : 0 < a ↔ a ≠ 0 := bot_lt_iff_ne_bot lemma not_lt_top {x : ℝ≥0∞} : ¬ x < ∞ ↔ x = ∞ := by rw [lt_top_iff_ne_top, not_not] lemma add_ne_top : a + b ≠ ∞ ↔ a ≠ ∞ ∧ b ≠ ∞ := by simpa only [lt_top_iff_ne_top] using add_lt_top lemma mul_top : a * ∞ = (if a = 0 then 0 else ∞) := begin split_ifs, { simp [h] }, { exact with_top.mul_top h } end lemma top_mul : ∞ * a = (if a = 0 then 0 else ∞) := begin split_ifs, { simp [h] }, { exact with_top.top_mul h } end @[simp] lemma top_mul_top : ∞ * ∞ = ∞ := with_top.top_mul_top lemma top_pow {n:ℕ} (h : 0 < n) : ∞^n = ∞ := nat.le_induction (pow_one _) (λ m hm hm', by rw [pow_succ, hm', top_mul_top]) _ (nat.succ_le_of_lt h) lemma mul_eq_top : a * b = ∞ ↔ (a ≠ 0 ∧ b = ∞) ∨ (a = ∞ ∧ b ≠ 0) := with_top.mul_eq_top_iff lemma mul_lt_top : a < ∞ → b < ∞ → a * b < ∞ := with_top.mul_lt_top lemma mul_ne_top : a ≠ ∞ → b ≠ ∞ → a * b ≠ ∞ := by simpa only [lt_top_iff_ne_top] using mul_lt_top lemma ne_top_of_mul_ne_top_left (h : a * b ≠ ∞) (hb : b ≠ 0) : a ≠ ∞ := by { simp [mul_eq_top, hb, not_or_distrib] at h ⊢, exact h.2 } lemma ne_top_of_mul_ne_top_right (h : a * b ≠ ∞) (ha : a ≠ 0) : b ≠ ∞ := ne_top_of_mul_ne_top_left (by rwa [mul_comm]) ha lemma lt_top_of_mul_lt_top_left (h : a * b < ∞) (hb : b ≠ 0) : a < ∞ := by { rw [ennreal.lt_top_iff_ne_top] at h ⊢, exact ne_top_of_mul_ne_top_left h hb } lemma lt_top_of_mul_lt_top_right (h : a * b < ∞) (ha : a ≠ 0) : b < ∞ := lt_top_of_mul_lt_top_left (by rwa [mul_comm]) ha lemma mul_lt_top_iff {a b : ℝ≥0∞} : a * b < ∞ ↔ (a < ∞ ∧ b < ∞) ∨ a = 0 ∨ b = 0 := begin split, { intro h, rw [← or_assoc, or_iff_not_imp_right, or_iff_not_imp_right], intros hb ha, exact ⟨lt_top_of_mul_lt_top_left h hb, lt_top_of_mul_lt_top_right h ha⟩ }, { rintro (⟨ha, hb⟩|rfl|rfl); [exact mul_lt_top ha hb, simp, simp] } end lemma mul_self_lt_top_iff {a : ℝ≥0∞} : a * a < ⊤ ↔ a < ⊤ := by { rw [ennreal.mul_lt_top_iff, and_self, or_self, or_iff_left_iff_imp], rintro rfl, norm_num } @[simp] lemma mul_pos : 0 < a * b ↔ 0 < a ∧ 0 < b := by simp only [pos_iff_ne_zero, ne.def, mul_eq_zero, not_or_distrib] lemma pow_eq_top : ∀ n:ℕ, a^n=∞ → a=∞ | 0 := by simp | (n+1) := λ o, by { rw pow_succ at o, exact (mul_eq_top.1 o).elim (λ h, pow_eq_top n h.2) and.left } lemma pow_ne_top (h : a ≠ ∞) {n:ℕ} : a^n ≠ ∞ := mt (pow_eq_top n) h lemma pow_lt_top : a < ∞ → ∀ n:ℕ, a^n < ∞ := by simpa only [lt_top_iff_ne_top] using pow_ne_top @[simp, norm_cast] lemma coe_finset_sum {s : finset α} {f : α → ℝ≥0} : ↑(∑ a in s, f a) = (∑ a in s, f a : ℝ≥0∞) := of_nnreal_hom.map_sum f s @[simp, norm_cast] lemma coe_finset_prod {s : finset α} {f : α → ℝ≥0} : ↑(∏ a in s, f a) = ((∏ a in s, f a) : ℝ≥0∞) := of_nnreal_hom.map_prod f s section order @[simp] lemma bot_eq_zero : (⊥ : ℝ≥0∞) = 0 := rfl @[simp] lemma coe_lt_top : coe r < ∞ := with_top.coe_lt_top r @[simp] lemma not_top_le_coe : ¬ ∞ ≤ ↑r := with_top.not_top_le_coe r lemma zero_lt_coe_iff : 0 < (↑p : ℝ≥0∞) ↔ 0 < p := coe_lt_coe @[simp, norm_cast] lemma one_le_coe_iff : (1:ℝ≥0∞) ≤ ↑r ↔ 1 ≤ r := coe_le_coe @[simp, norm_cast] lemma coe_le_one_iff : ↑r ≤ (1:ℝ≥0∞) ↔ r ≤ 1 := coe_le_coe @[simp, norm_cast] lemma coe_lt_one_iff : (↑p : ℝ≥0∞) < 1 ↔ p < 1 := coe_lt_coe @[simp, norm_cast] lemma one_lt_coe_iff : 1 < (↑p : ℝ≥0∞) ↔ 1 < p := coe_lt_coe @[simp, norm_cast] lemma coe_nat (n : ℕ) : ((n : ℝ≥0) : ℝ≥0∞) = n := with_top.coe_nat n @[simp] lemma of_real_coe_nat (n : ℕ) : ennreal.of_real n = n := by simp [ennreal.of_real] @[simp] lemma nat_ne_top (n : ℕ) : (n : ℝ≥0∞) ≠ ∞ := with_top.nat_ne_top n @[simp] lemma top_ne_nat (n : ℕ) : ∞ ≠ n := with_top.top_ne_nat n @[simp] lemma one_lt_top : 1 < ∞ := coe_lt_top lemma le_coe_iff : a ≤ ↑r ↔ (∃p:ℝ≥0, a = p ∧ p ≤ r) := with_top.le_coe_iff lemma coe_le_iff : ↑r ≤ a ↔ (∀p:ℝ≥0, a = p → r ≤ p) := with_top.coe_le_iff lemma lt_iff_exists_coe : a < b ↔ (∃p:ℝ≥0, a = p ∧ ↑p < b) := with_top.lt_iff_exists_coe lemma to_real_le_coe_of_le_coe {a : ℝ≥0∞} {b : ℝ≥0} (h : a ≤ b) : a.to_real ≤ b := show ↑a.to_nnreal ≤ ↑b, begin have : ↑a.to_nnreal = a := ennreal.coe_to_nnreal (lt_of_le_of_lt h coe_lt_top).ne, rw ← this at h, exact_mod_cast h end @[simp, norm_cast] lemma coe_finset_sup {s : finset α} {f : α → ℝ≥0} : ↑(s.sup f) = s.sup (λ x, (f x : ℝ≥0∞)) := finset.comp_sup_eq_sup_comp_of_is_total _ coe_mono rfl lemma pow_le_pow {n m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := begin cases a, { cases m, { rw eq_bot_iff.mpr h, exact le_refl _ }, { rw [none_eq_top, top_pow (nat.succ_pos m)], exact le_top } }, { rw [some_eq_coe, ← coe_pow, ← coe_pow, coe_le_coe], exact pow_le_pow (by simpa using ha) h } end @[simp] lemma max_eq_zero_iff : max a b = 0 ↔ a = 0 ∧ b = 0 := by simp only [nonpos_iff_eq_zero.symm, max_le_iff] @[simp] lemma max_zero_left : max 0 a = a := max_eq_right (zero_le a) @[simp] lemma max_zero_right : max a 0 = a := max_eq_left (zero_le a) -- TODO: why this is not a `rfl`? There is some hidden diamond here. @[simp] lemma sup_eq_max : a ⊔ b = max a b := eq_of_forall_ge_iff $ λ c, sup_le_iff.trans max_le_iff.symm protected lemma pow_pos : 0 < a → ∀ n : ℕ, 0 < a^n := canonically_ordered_semiring.pow_pos protected lemma pow_ne_zero : a ≠ 0 → ∀ n : ℕ, a^n ≠ 0 := by simpa only [pos_iff_ne_zero] using ennreal.pow_pos @[simp] lemma not_lt_zero : ¬ a < 0 := by simp lemma add_lt_add_iff_left : a < ∞ → (a + c < a + b ↔ c < b) := with_top.add_lt_add_iff_left lemma add_lt_add_iff_right : a < ∞ → (c + a < b + a ↔ c < b) := with_top.add_lt_add_iff_right lemma lt_add_right (ha : a < ∞) (hb : 0 < b) : a < a + b := by rwa [← add_lt_add_iff_left ha, add_zero] at hb lemma le_of_forall_pos_le_add : ∀{a b : ℝ≥0∞}, (∀ε:ℝ≥0, 0 < ε → b < ∞ → a ≤ b + ε) → a ≤ b | a none h := le_top | none (some a) h := have ∞ ≤ ↑a + ↑(1:ℝ≥0), from h 1 zero_lt_one coe_lt_top, by rw [← coe_add] at this; exact (not_top_le_coe this).elim | (some a) (some b) h := by simp only [none_eq_top, some_eq_coe, coe_add.symm, coe_le_coe, coe_lt_top, true_implies_iff] at *; exact nnreal.le_of_forall_pos_le_add h lemma lt_iff_exists_rat_btwn : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < nnreal.of_real q ∧ (nnreal.of_real q:ℝ≥0∞) < b) := ⟨λ h, begin rcases lt_iff_exists_coe.1 h with ⟨p, rfl, _⟩, rcases exists_between h with ⟨c, pc, cb⟩, rcases lt_iff_exists_coe.1 cb with ⟨r, rfl, _⟩, rcases (nnreal.lt_iff_exists_rat_btwn _ _).1 (coe_lt_coe.1 pc) with ⟨q, hq0, pq, qr⟩, exact ⟨q, hq0, coe_lt_coe.2 pq, lt_trans (coe_lt_coe.2 qr) cb⟩ end, λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩ lemma lt_iff_exists_real_btwn : a < b ↔ (∃r:ℝ, 0 ≤ r ∧ a < ennreal.of_real r ∧ (ennreal.of_real r:ℝ≥0∞) < b) := ⟨λ h, let ⟨q, q0, aq, qb⟩ := ennreal.lt_iff_exists_rat_btwn.1 h in ⟨q, rat.cast_nonneg.2 q0, aq, qb⟩, λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩ lemma lt_iff_exists_nnreal_btwn : a < b ↔ (∃r:ℝ≥0, a < r ∧ (r : ℝ≥0∞) < b) := with_top.lt_iff_exists_coe_btwn lemma lt_iff_exists_add_pos_lt : a < b ↔ (∃ r : ℝ≥0, 0 < r ∧ a + r < b) := begin refine ⟨λ hab, _, λ ⟨r, rpos, hr⟩, lt_of_le_of_lt (le_self_add) hr⟩, cases a, { simpa using hab }, rcases lt_iff_exists_real_btwn.1 hab with ⟨c, c_nonneg, ac, cb⟩, let d : ℝ≥0 := ⟨c, c_nonneg⟩, have ad : a < d, { rw of_real_eq_coe_nnreal c_nonneg at ac, exact coe_lt_coe.1 ac }, refine ⟨d-a, nnreal.sub_pos.2 ad, _⟩, rw [some_eq_coe, ← coe_add], convert cb, have : nnreal.of_real c = d, by { rw [← nnreal.coe_eq, nnreal.coe_of_real _ c_nonneg], refl }, rw [add_comm, this], exact nnreal.sub_add_cancel_of_le (le_of_lt ad) end lemma coe_nat_lt_coe {n : ℕ} : (n : ℝ≥0∞) < r ↔ ↑n < r := ennreal.coe_nat n ▸ coe_lt_coe lemma coe_lt_coe_nat {n : ℕ} : (r : ℝ≥0∞) < n ↔ r < n := ennreal.coe_nat n ▸ coe_lt_coe @[simp, norm_cast] lemma coe_nat_lt_coe_nat {m n : ℕ} : (m : ℝ≥0∞) < n ↔ m < n := ennreal.coe_nat n ▸ coe_nat_lt_coe.trans nat.cast_lt lemma coe_nat_ne_top {n : ℕ} : (n : ℝ≥0∞) ≠ ∞ := ennreal.coe_nat n ▸ coe_ne_top lemma coe_nat_mono : strict_mono (coe : ℕ → ℝ≥0∞) := λ _ _, coe_nat_lt_coe_nat.2 @[simp, norm_cast] lemma coe_nat_le_coe_nat {m n : ℕ} : (m : ℝ≥0∞) ≤ n ↔ m ≤ n := coe_nat_mono.le_iff_le instance : char_zero ℝ≥0∞ := ⟨coe_nat_mono.injective⟩ protected lemma exists_nat_gt {r : ℝ≥0∞} (h : r ≠ ∞) : ∃n:ℕ, r < n := begin lift r to ℝ≥0 using h, rcases exists_nat_gt r with ⟨n, hn⟩, exact ⟨n, coe_lt_coe_nat.2 hn⟩, end lemma add_lt_add (ac : a < c) (bd : b < d) : a + b < c + d := begin lift a to ℝ≥0 using ne_top_of_lt ac, lift b to ℝ≥0 using ne_top_of_lt bd, cases c, { simp }, cases d, { simp }, simp only [← coe_add, some_eq_coe, coe_lt_coe] at *, exact add_lt_add ac bd end @[norm_cast] lemma coe_min : ((min r p:ℝ≥0):ℝ≥0∞) = min r p := coe_mono.map_min @[norm_cast] lemma coe_max : ((max r p:ℝ≥0):ℝ≥0∞) = max r p := coe_mono.map_max lemma le_of_top_imp_top_of_to_nnreal_le {a b : ℝ≥0∞} (h : a = ⊤ → b = ⊤) (h_nnreal : a ≠ ⊤ → b ≠ ⊤ → a.to_nnreal ≤ b.to_nnreal) : a ≤ b := begin by_cases ha : a = ⊤, { rw h ha, exact le_top, }, by_cases hb : b = ⊤, { rw hb, exact le_top, }, rw [←coe_to_nnreal hb, ←coe_to_nnreal ha, coe_le_coe], exact h_nnreal ha hb, end end order section complete_lattice lemma coe_Sup {s : set ℝ≥0} : bdd_above s → (↑(Sup s) : ℝ≥0∞) = (⨆a∈s, ↑a) := with_top.coe_Sup lemma coe_Inf {s : set ℝ≥0} : s.nonempty → (↑(Inf s) : ℝ≥0∞) = (⨅a∈s, ↑a) := with_top.coe_Inf @[simp] lemma top_mem_upper_bounds {s : set ℝ≥0∞} : ∞ ∈ upper_bounds s := assume x hx, le_top lemma coe_mem_upper_bounds {s : set ℝ≥0} : ↑r ∈ upper_bounds ((coe : ℝ≥0 → ℝ≥0∞) '' s) ↔ r ∈ upper_bounds s := by simp [upper_bounds, ball_image_iff, -mem_image, *] {contextual := tt} end complete_lattice section mul @[mono] lemma mul_le_mul : a ≤ b → c ≤ d → a * c ≤ b * d := canonically_ordered_semiring.mul_le_mul @[mono] lemma mul_lt_mul (ac : a < c) (bd : b < d) : a * b < c * d := begin rcases lt_iff_exists_nnreal_btwn.1 ac with ⟨a', aa', a'c⟩, lift a to ℝ≥0 using ne_top_of_lt aa', rcases lt_iff_exists_nnreal_btwn.1 bd with ⟨b', bb', b'd⟩, lift b to ℝ≥0 using ne_top_of_lt bb', norm_cast at *, calc ↑(a * b) < ↑(a' * b') : coe_lt_coe.2 (mul_lt_mul' aa'.le bb' (zero_le _) ((zero_le a).trans_lt aa')) ... = ↑a' * ↑b' : coe_mul ... ≤ c * d : mul_le_mul a'c.le b'd.le end lemma mul_left_mono : monotone ((*) a) := λ b c, mul_le_mul (le_refl a) lemma mul_right_mono : monotone (λ x, x * a) := λ b c h, mul_le_mul h (le_refl a) lemma max_mul : max a b * c = max (a * c) (b * c) := mul_right_mono.map_max lemma mul_max : a * max b c = max (a * b) (a * c) := mul_left_mono.map_max lemma mul_eq_mul_left : a ≠ 0 → a ≠ ∞ → (a * b = a * c ↔ b = c) := begin cases a; cases b; cases c; simp [none_eq_top, some_eq_coe, mul_top, top_mul, -coe_mul, coe_mul.symm, nnreal.mul_eq_mul_left] {contextual := tt}, end lemma mul_eq_mul_right : c ≠ 0 → c ≠ ∞ → (a * c = b * c ↔ a = b) := mul_comm c a ▸ mul_comm c b ▸ mul_eq_mul_left lemma mul_le_mul_left : a ≠ 0 → a ≠ ∞ → (a * b ≤ a * c ↔ b ≤ c) := begin cases a; cases b; cases c; simp [none_eq_top, some_eq_coe, mul_top, top_mul, -coe_mul, coe_mul.symm] {contextual := tt}, assume h, exact mul_le_mul_left (pos_iff_ne_zero.2 h) end lemma mul_le_mul_right : c ≠ 0 → c ≠ ∞ → (a * c ≤ b * c ↔ a ≤ b) := mul_comm c a ▸ mul_comm c b ▸ mul_le_mul_left lemma mul_lt_mul_left : a ≠ 0 → a ≠ ∞ → (a * b < a * c ↔ b < c) := λ h0 ht, by simp only [mul_le_mul_left h0 ht, lt_iff_le_not_le] lemma mul_lt_mul_right : c ≠ 0 → c ≠ ∞ → (a * c < b * c ↔ a < b) := mul_comm c a ▸ mul_comm c b ▸ mul_lt_mul_left end mul section sub instance : has_sub ℝ≥0∞ := ⟨λa b, Inf {d | a ≤ d + b}⟩ @[norm_cast] lemma coe_sub : ↑(p - r) = (↑p:ℝ≥0∞) - r := le_antisymm (le_Inf $ assume b (hb : ↑p ≤ b + r), coe_le_iff.2 $ by rintros d rfl; rwa [← coe_add, coe_le_coe, ← nnreal.sub_le_iff_le_add] at hb) (Inf_le $ show (↑p : ℝ≥0∞) ≤ ↑(p - r) + ↑r, by rw [← coe_add, coe_le_coe, ← nnreal.sub_le_iff_le_add]) @[simp] lemma top_sub_coe : ∞ - ↑r = ∞ := top_unique $ le_Inf $ by simp [add_eq_top] @[simp] lemma sub_eq_zero_of_le (h : a ≤ b) : a - b = 0 := le_antisymm (Inf_le $ le_add_left h) (zero_le _) @[simp] lemma sub_self : a - a = 0 := sub_eq_zero_of_le $ le_refl _ @[simp] lemma zero_sub : 0 - a = 0 := le_antisymm (Inf_le $ zero_le $ 0 + a) (zero_le _) @[simp] lemma sub_infty : a - ∞ = 0 := le_antisymm (Inf_le $ by simp) (zero_le _) lemma sub_le_sub (h₁ : a ≤ b) (h₂ : d ≤ c) : a - c ≤ b - d := Inf_le_Inf $ assume e (h : b ≤ e + d), calc a ≤ b : h₁ ... ≤ e + d : h ... ≤ e + c : add_le_add (le_refl _) h₂ @[simp] lemma add_sub_self : ∀{a b : ℝ≥0∞}, b < ∞ → (a + b) - b = a | a none := by simp [none_eq_top] | none (some b) := by simp [none_eq_top, some_eq_coe] | (some a) (some b) := by simp [some_eq_coe]; rw [← coe_add, ← coe_sub, coe_eq_coe, nnreal.add_sub_cancel] @[simp] lemma add_sub_self' (h : a < ∞) : (a + b) - a = b := by rw [add_comm, add_sub_self h] lemma add_right_inj (h : a < ∞) : a + b = a + c ↔ b = c := ⟨λ e, by simpa [h] using congr_arg (λ x, x - a) e, congr_arg _⟩ lemma add_left_inj (h : a < ∞) : b + a = c + a ↔ b = c := by rw [add_comm, add_comm c, add_right_inj h] @[simp] lemma sub_add_cancel_of_le : ∀{a b : ℝ≥0∞}, b ≤ a → (a - b) + b = a := begin simp [forall_ennreal, le_coe_iff, -add_comm] {contextual := tt}, rintros r p x rfl h, rw [← coe_sub, ← coe_add, nnreal.sub_add_cancel_of_le h] end @[simp] lemma add_sub_cancel_of_le (h : b ≤ a) : b + (a - b) = a := by rwa [add_comm, sub_add_cancel_of_le] lemma sub_add_self_eq_max : (a - b) + b = max a b := match le_total a b with | or.inl h := by simp [h, max_eq_right] | or.inr h := by simp [h, max_eq_left] end lemma le_sub_add_self : a ≤ (a - b) + b := by { rw sub_add_self_eq_max, exact le_max_left a b } @[simp] protected lemma sub_le_iff_le_add : a - b ≤ c ↔ a ≤ c + b := iff.intro (assume h : a - b ≤ c, calc a ≤ (a - b) + b : le_sub_add_self ... ≤ c + b : add_le_add_right h _) (assume h : a ≤ c + b, Inf_le h) protected lemma sub_le_iff_le_add' : a - b ≤ c ↔ a ≤ b + c := add_comm c b ▸ ennreal.sub_le_iff_le_add lemma sub_eq_of_add_eq : b ≠ ∞ → a + b = c → c - b = a := λ hb hc, hc ▸ add_sub_self (lt_top_iff_ne_top.2 hb) protected lemma sub_le_of_sub_le (h : a - b ≤ c) : a - c ≤ b := ennreal.sub_le_iff_le_add.2 $ by { rw add_comm, exact ennreal.sub_le_iff_le_add.1 h } protected lemma sub_lt_self : a ≠ ∞ → a ≠ 0 → 0 < b → a - b < a := match a, b with | none, _ := by { have := none_eq_top, assume h, contradiction } | (some a), none := by {intros, simp only [none_eq_top, sub_infty, pos_iff_ne_zero], assumption} | (some a), (some b) := begin simp only [some_eq_coe, coe_sub.symm, coe_pos, coe_eq_zero, coe_lt_coe, ne.def], assume h₁ h₂, apply nnreal.sub_lt_self, exact pos_iff_ne_zero.2 h₂ end end @[simp] lemma sub_eq_zero_iff_le : a - b = 0 ↔ a ≤ b := by simpa [-ennreal.sub_le_iff_le_add] using @ennreal.sub_le_iff_le_add a b 0 @[simp] lemma zero_lt_sub_iff_lt : 0 < a - b ↔ b < a := by simpa [ennreal.bot_lt_iff_ne_bot, -sub_eq_zero_iff_le] using not_iff_not.2 (@sub_eq_zero_iff_le a b) lemma lt_sub_iff_add_lt : a < b - c ↔ a + c < b := begin cases a, { simp }, cases c, { simp }, cases b, { simp only [true_iff, coe_lt_top, some_eq_coe, top_sub_coe, none_eq_top, ← coe_add] }, simp only [some_eq_coe], rw [← coe_add, ← coe_sub, coe_lt_coe, coe_lt_coe, nnreal.lt_sub_iff_add_lt], end lemma sub_le_self (a b : ℝ≥0∞) : a - b ≤ a := ennreal.sub_le_iff_le_add.2 $ le_self_add @[simp] lemma sub_zero : a - 0 = a := eq.trans (add_zero (a - 0)).symm $ by simp /-- A version of triangle inequality for difference as a "distance". -/ lemma sub_le_sub_add_sub : a - c ≤ a - b + (b - c) := ennreal.sub_le_iff_le_add.2 $ calc a ≤ a - b + b : le_sub_add_self ... ≤ a - b + ((b - c) + c) : add_le_add_left le_sub_add_self _ ... = a - b + (b - c) + c : (add_assoc _ _ _).symm lemma sub_sub_cancel (h : a < ∞) (h2 : b ≤ a) : a - (a - b) = b := by rw [← add_left_inj (lt_of_le_of_lt (sub_le_self _ _) h), sub_add_cancel_of_le (sub_le_self _ _), add_sub_cancel_of_le h2] lemma sub_right_inj {a b c : ℝ≥0∞} (ha : a < ∞) (hb : b ≤ a) (hc : c ≤ a) : a - b = a - c ↔ b = c := iff.intro begin assume h, have : a - (a - b) = a - (a - c), rw h, rw [sub_sub_cancel ha hb, sub_sub_cancel ha hc] at this, exact this end (λ h, by rw h) lemma sub_mul (h : 0 < b → b < a → c ≠ ∞) : (a - b) * c = a * c - b * c := begin cases le_or_lt a b with hab hab, { simp [hab, mul_right_mono hab] }, symmetry, cases eq_or_lt_of_le (zero_le b) with hb hb, { subst b, simp }, apply sub_eq_of_add_eq, { exact mul_ne_top (ne_top_of_lt hab) (h hb hab) }, rw [← add_mul, sub_add_cancel_of_le (le_of_lt hab)] end lemma mul_sub (h : 0 < c → c < b → a ≠ ∞) : a * (b - c) = a * b - a * c := by { simp only [mul_comm a], exact sub_mul h } lemma sub_mul_ge : a * c - b * c ≤ (a - b) * c := begin -- with `0 < b → b < a → c ≠ ∞` Lean names the first variable `a` by_cases h : ∀ (hb : 0 < b), b < a → c ≠ ∞, { rw [sub_mul h], exact le_refl _ }, { push_neg at h, rcases h with ⟨hb, hba, hc⟩, subst c, simp only [mul_top, if_neg (ne_of_gt hb), if_neg (ne_of_gt $ lt_trans hb hba), sub_self, zero_le] } end end sub section sum open finset /-- A product of finite numbers is still finite -/ lemma prod_lt_top {s : finset α} {f : α → ℝ≥0∞} (h : ∀a∈s, f a < ∞) : (∏ a in s, f a) < ∞ := with_top.prod_lt_top h /-- A sum of finite numbers is still finite -/ lemma sum_lt_top {s : finset α} {f : α → ℝ≥0∞} : (∀a∈s, f a < ∞) → ∑ a in s, f a < ∞ := with_top.sum_lt_top /-- A sum of finite numbers is still finite -/ lemma sum_lt_top_iff {s : finset α} {f : α → ℝ≥0∞} : ∑ a in s, f a < ∞ ↔ (∀a∈s, f a < ∞) := with_top.sum_lt_top_iff /-- A sum of numbers is infinite iff one of them is infinite -/ lemma sum_eq_top_iff {s : finset α} {f : α → ℝ≥0∞} : (∑ x in s, f x) = ∞ ↔ (∃a∈s, f a = ∞) := with_top.sum_eq_top_iff /-- seeing `ℝ≥0∞` as `ℝ≥0` does not change their sum, unless one of the `ℝ≥0∞` is infinity -/ lemma to_nnreal_sum {s : finset α} {f : α → ℝ≥0∞} (hf : ∀a∈s, f a < ∞) : ennreal.to_nnreal (∑ a in s, f a) = ∑ a in s, ennreal.to_nnreal (f a) := begin rw [← coe_eq_coe, coe_to_nnreal, coe_finset_sum, sum_congr], { refl }, { intros x hx, exact (coe_to_nnreal (hf x hx).ne).symm }, { exact (sum_lt_top hf).ne } end /-- seeing `ℝ≥0∞` as `real` does not change their sum, unless one of the `ℝ≥0∞` is infinity -/ lemma to_real_sum {s : finset α} {f : α → ℝ≥0∞} (hf : ∀a∈s, f a < ∞) : ennreal.to_real (∑ a in s, f a) = ∑ a in s, ennreal.to_real (f a) := by { rw [ennreal.to_real, to_nnreal_sum hf, nnreal.coe_sum], refl } lemma of_real_sum_of_nonneg {s : finset α} {f : α → ℝ} (hf : ∀ i, i ∈ s → 0 ≤ f i) : ennreal.of_real (∑ i in s, f i) = ∑ i in s, ennreal.of_real (f i) := begin simp_rw [ennreal.of_real, ←coe_finset_sum, coe_eq_coe], exact nnreal.of_real_sum_of_nonneg hf, end end sum section interval variables {x y z : ℝ≥0∞} {ε ε₁ ε₂ : ℝ≥0∞} {s : set ℝ≥0∞} protected lemma Ico_eq_Iio : (Ico 0 y) = (Iio y) := ext $ assume a, iff.intro (assume ⟨_, hx⟩, hx) (assume hx, ⟨zero_le _, hx⟩) lemma mem_Iio_self_add : x ≠ ∞ → 0 < ε → x ∈ Iio (x + ε) := assume xt ε0, lt_add_right (by rwa lt_top_iff_ne_top) ε0 lemma not_mem_Ioo_self_sub : x = 0 → x ∉ Ioo (x - ε) y := assume x0, by simp [x0] lemma mem_Ioo_self_sub_add : x ≠ ∞ → x ≠ 0 → 0 < ε₁ → 0 < ε₂ → x ∈ Ioo (x - ε₁) (x + ε₂) := assume xt x0 ε0 ε0', ⟨ennreal.sub_lt_self xt x0 ε0, lt_add_right (by rwa [lt_top_iff_ne_top]) ε0'⟩ end interval section bit @[simp] lemma bit0_inj : bit0 a = bit0 b ↔ a = b := ⟨λh, begin rcases (lt_trichotomy a b) with h₁| h₂| h₃, { exact (absurd h (ne_of_lt (add_lt_add h₁ h₁))) }, { exact h₂ }, { exact (absurd h.symm (ne_of_lt (add_lt_add h₃ h₃))) } end, λh, congr_arg _ h⟩ @[simp] lemma bit0_eq_zero_iff : bit0 a = 0 ↔ a = 0 := by simpa only [bit0_zero] using @bit0_inj a 0 @[simp] lemma bit0_eq_top_iff : bit0 a = ∞ ↔ a = ∞ := by rw [bit0, add_eq_top, or_self] @[simp] lemma bit1_inj : bit1 a = bit1 b ↔ a = b := ⟨λh, begin unfold bit1 at h, rwa [add_left_inj, bit0_inj] at h, simp [lt_top_iff_ne_top] end, λh, congr_arg _ h⟩ @[simp] lemma bit1_ne_zero : bit1 a ≠ 0 := by unfold bit1; simp @[simp] lemma bit1_eq_one_iff : bit1 a = 1 ↔ a = 0 := by simpa only [bit1_zero] using @bit1_inj a 0 @[simp] lemma bit1_eq_top_iff : bit1 a = ∞ ↔ a = ∞ := by unfold bit1; rw add_eq_top; simp end bit section inv instance : has_inv ℝ≥0∞ := ⟨λa, Inf {b | 1 ≤ a * b}⟩ instance : div_inv_monoid ℝ≥0∞ := { inv := has_inv.inv, .. (infer_instance : monoid ℝ≥0∞) } @[simp] lemma inv_zero : (0 : ℝ≥0∞)⁻¹ = ∞ := show Inf {b : ℝ≥0∞ | 1 ≤ 0 * b} = ∞, by simp; refl @[simp] lemma inv_top : ∞⁻¹ = 0 := bot_unique $ le_of_forall_le_of_dense $ λ a (h : a > 0), Inf_le $ by simp [*, ne_of_gt h, top_mul] @[simp, norm_cast] lemma coe_inv (hr : r ≠ 0) : (↑r⁻¹ : ℝ≥0∞) = (↑r)⁻¹ := le_antisymm (le_Inf $ assume b (hb : 1 ≤ ↑r * b), coe_le_iff.2 $ by rintros b rfl; rwa [← coe_mul, ← coe_one, coe_le_coe, ← nnreal.inv_le hr] at hb) (Inf_le $ by simp; rw [← coe_mul, mul_inv_cancel hr]; exact le_refl 1) lemma coe_inv_le : (↑r⁻¹ : ℝ≥0∞) ≤ (↑r)⁻¹ := if hr : r = 0 then by simp only [hr, inv_zero, coe_zero, le_top] else by simp only [coe_inv hr, le_refl] @[norm_cast] lemma coe_inv_two : ((2⁻¹:ℝ≥0):ℝ≥0∞) = 2⁻¹ := by rw [coe_inv (ne_of_gt _root_.zero_lt_two), coe_two] @[simp, norm_cast] lemma coe_div (hr : r ≠ 0) : (↑(p / r) : ℝ≥0∞) = p / r := by rw [div_eq_mul_inv, div_eq_mul_inv, coe_mul, coe_inv hr] @[simp] lemma inv_one : (1:ℝ≥0∞)⁻¹ = 1 := by simpa only [coe_inv one_ne_zero, coe_one] using coe_eq_coe.2 inv_one @[simp] lemma div_one {a : ℝ≥0∞} : a / 1 = a := by rw [div_eq_mul_inv, inv_one, mul_one] protected lemma inv_pow {n : ℕ} : (a^n)⁻¹ = (a⁻¹)^n := begin by_cases a = 0; cases a; cases n; simp [*, none_eq_top, some_eq_coe, zero_pow, top_pow, nat.zero_lt_succ] at *, rw [← coe_inv h, ← coe_pow, ← coe_inv (pow_ne_zero _ h), ← inv_pow', coe_pow] end @[simp] lemma inv_inv : (a⁻¹)⁻¹ = a := by by_cases a = 0; cases a; simp [*, none_eq_top, some_eq_coe, -coe_inv, (coe_inv _).symm] at * lemma inv_involutive : function.involutive (λ a:ℝ≥0∞, a⁻¹) := λ a, ennreal.inv_inv lemma inv_bijective : function.bijective (λ a:ℝ≥0∞, a⁻¹) := ennreal.inv_involutive.bijective @[simp] lemma inv_eq_inv : a⁻¹ = b⁻¹ ↔ a = b := inv_bijective.1.eq_iff @[simp] lemma inv_eq_top : a⁻¹ = ∞ ↔ a = 0 := inv_zero ▸ inv_eq_inv lemma inv_ne_top : a⁻¹ ≠ ∞ ↔ a ≠ 0 := by simp @[simp] lemma inv_lt_top {x : ℝ≥0∞} : x⁻¹ < ∞ ↔ 0 < x := by { simp only [lt_top_iff_ne_top, inv_ne_top, pos_iff_ne_zero] } lemma div_lt_top {x y : ℝ≥0∞} (h1 : x < ∞) (h2 : 0 < y) : x / y < ∞ := mul_lt_top h1 (inv_lt_top.mpr h2) @[simp] lemma inv_eq_zero : a⁻¹ = 0 ↔ a = ∞ := inv_top ▸ inv_eq_inv lemma inv_ne_zero : a⁻¹ ≠ 0 ↔ a ≠ ∞ := by simp @[simp] lemma inv_pos : 0 < a⁻¹ ↔ a ≠ ∞ := pos_iff_ne_zero.trans inv_ne_zero @[simp] lemma inv_lt_inv : a⁻¹ < b⁻¹ ↔ b < a := begin cases a; cases b; simp only [some_eq_coe, none_eq_top, inv_top], { simp only [lt_irrefl] }, { exact inv_pos.trans lt_top_iff_ne_top.symm }, { simp only [not_lt_zero, not_top_lt] }, { cases eq_or_lt_of_le (zero_le a) with ha ha; cases eq_or_lt_of_le (zero_le b) with hb hb, { subst a, subst b, simp }, { subst a, simp }, { subst b, simp [pos_iff_ne_zero, lt_top_iff_ne_top, inv_ne_top] }, { rw [← coe_inv (ne_of_gt ha), ← coe_inv (ne_of_gt hb), coe_lt_coe, coe_lt_coe], simp only [nnreal.coe_lt_coe.symm] at *, exact inv_lt_inv ha hb } } end lemma inv_lt_iff_inv_lt : a⁻¹ < b ↔ b⁻¹ < a := by simpa only [inv_inv] using @inv_lt_inv a b⁻¹ lemma lt_inv_iff_lt_inv : a < b⁻¹ ↔ b < a⁻¹ := by simpa only [inv_inv] using @inv_lt_inv a⁻¹ b @[simp, priority 1100] -- higher than le_inv_iff_mul_le lemma inv_le_inv : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := by simp only [le_iff_lt_or_eq, inv_lt_inv, inv_eq_inv, eq_comm] lemma inv_le_iff_inv_le : a⁻¹ ≤ b ↔ b⁻¹ ≤ a := by simpa only [inv_inv] using @inv_le_inv a b⁻¹ lemma le_inv_iff_le_inv : a ≤ b⁻¹ ↔ b ≤ a⁻¹ := by simpa only [inv_inv] using @inv_le_inv a⁻¹ b @[simp] lemma inv_le_one : a⁻¹ ≤ 1 ↔ 1 ≤ a := inv_le_iff_inv_le.trans $ by rw inv_one lemma one_le_inv : 1 ≤ a⁻¹ ↔ a ≤ 1 := le_inv_iff_le_inv.trans $ by rw inv_one @[simp] lemma inv_lt_one : a⁻¹ < 1 ↔ 1 < a := inv_lt_iff_inv_lt.trans $ by rw [inv_one] lemma pow_le_pow_of_le_one {n m : ℕ} (ha : a ≤ 1) (h : n ≤ m) : a ^ m ≤ a ^ n := begin rw [← @inv_inv a, ← ennreal.inv_pow, ← @ennreal.inv_pow a⁻¹, inv_le_inv], exact pow_le_pow (one_le_inv.2 ha) h end @[simp] lemma div_top : a / ∞ = 0 := by rw [div_eq_mul_inv, inv_top, mul_zero] @[simp] lemma top_div_coe : ∞ / p = ∞ := by simp [div_eq_mul_inv, top_mul] lemma top_div_of_ne_top (h : a ≠ ∞) : ∞ / a = ∞ := by { lift a to ℝ≥0 using h, exact top_div_coe } lemma top_div_of_lt_top (h : a < ∞) : ∞ / a = ∞ := top_div_of_ne_top h.ne lemma top_div : ∞ / a = if a = ∞ then 0 else ∞ := by by_cases a = ∞; simp [top_div_of_ne_top, *] @[simp] lemma zero_div : 0 / a = 0 := zero_mul a⁻¹ lemma div_eq_top : a / b = ∞ ↔ (a ≠ 0 ∧ b = 0) ∨ (a = ∞ ∧ b ≠ ∞) := by simp [div_eq_mul_inv, ennreal.mul_eq_top] lemma le_div_iff_mul_le (h0 : b ≠ 0 ∨ c ≠ 0) (ht : b ≠ ∞ ∨ c ≠ ∞) : a ≤ c / b ↔ a * b ≤ c := begin cases b, { simp at ht, split, { assume ha, simp at ha, simp [ha] }, { contrapose, assume ha, simp at ha, have : a * ∞ = ∞, by simp [ennreal.mul_eq_top, ha], simp [this, ht] } }, by_cases hb : b ≠ 0, { have : (b : ℝ≥0∞) ≠ 0, by simp [hb], rw [← ennreal.mul_le_mul_left this coe_ne_top], suffices : ↑b * a ≤ (↑b * ↑b⁻¹) * c ↔ a * ↑b ≤ c, { simpa [some_eq_coe, div_eq_mul_inv, hb, mul_left_comm, mul_comm, mul_assoc] }, rw [← coe_mul, mul_inv_cancel hb, coe_one, one_mul, mul_comm] }, { simp at hb, simp [hb] at h0, have : c / 0 = ∞, by simp [div_eq_top, h0], simp [hb, this] } end lemma div_le_iff_le_mul (hb0 : b ≠ 0 ∨ c ≠ ∞) (hbt : b ≠ ∞ ∨ c ≠ 0) : a / b ≤ c ↔ a ≤ c * b := begin suffices : a * b⁻¹ ≤ c ↔ a ≤ c / b⁻¹, by simpa [div_eq_mul_inv], refine (le_div_iff_mul_le _ _).symm; simpa end lemma div_le_of_le_mul (h : a ≤ b * c) : a / c ≤ b := begin by_cases h0 : c = 0, { have : a = 0, by simpa [h0] using h, simp [*] }, by_cases hinf : c = ∞, by simp [hinf], exact (div_le_iff_le_mul (or.inl h0) (or.inl hinf)).2 h end protected lemma div_lt_iff (h0 : b ≠ 0 ∨ c ≠ 0) (ht : b ≠ ∞ ∨ c ≠ ∞) : c / b < a ↔ c < a * b := lt_iff_lt_of_le_iff_le $ le_div_iff_mul_le h0 ht lemma mul_lt_of_lt_div (h : a < b / c) : a * c < b := by { contrapose! h, exact ennreal.div_le_of_le_mul h } lemma inv_le_iff_le_mul : (b = ∞ → a ≠ 0) → (a = ∞ → b ≠ 0) → (a⁻¹ ≤ b ↔ 1 ≤ a * b) := begin cases a; cases b; simp [none_eq_top, some_eq_coe, mul_top, top_mul] {contextual := tt}, by_cases a = 0; simp [*, -coe_mul, coe_mul.symm, -coe_inv, (coe_inv _).symm, nnreal.inv_le] end @[simp] lemma le_inv_iff_mul_le : a ≤ b⁻¹ ↔ a * b ≤ 1 := begin cases b, { by_cases a = 0; simp [*, none_eq_top, mul_top] }, by_cases b = 0; simp [*, some_eq_coe, le_div_iff_mul_le], suffices : a ≤ 1 / b ↔ a * b ≤ 1, { simpa [div_eq_mul_inv, h] }, exact le_div_iff_mul_le (or.inl (mt coe_eq_coe.1 h)) (or.inl coe_ne_top) end lemma mul_inv_cancel (h0 : a ≠ 0) (ht : a ≠ ∞) : a * a⁻¹ = 1 := begin lift a to ℝ≥0 using ht, norm_cast at *, exact mul_inv_cancel h0 end lemma inv_mul_cancel (h0 : a ≠ 0) (ht : a ≠ ∞) : a⁻¹ * a = 1 := mul_comm a a⁻¹ ▸ mul_inv_cancel h0 ht lemma mul_le_iff_le_inv {a b r : ℝ≥0∞} (hr₀ : r ≠ 0) (hr₁ : r ≠ ∞) : (r * a ≤ b ↔ a ≤ r⁻¹ * b) := by rw [← @ennreal.mul_le_mul_left _ a _ hr₀ hr₁, ← mul_assoc, mul_inv_cancel hr₀ hr₁, one_mul] lemma le_of_forall_nnreal_lt {x y : ℝ≥0∞} (h : ∀ r : ℝ≥0, ↑r < x → ↑r ≤ y) : x ≤ y := begin refine le_of_forall_ge_of_dense (λ r hr, _), lift r to ℝ≥0 using ne_top_of_lt hr, exact h r hr end lemma eq_top_of_forall_nnreal_le {x : ℝ≥0∞} (h : ∀ r : ℝ≥0, ↑r ≤ x) : x = ∞ := top_unique $ le_of_forall_nnreal_lt $ λ r hr, h r lemma div_add_div_same {a b c : ℝ≥0∞} : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma div_self (h0 : a ≠ 0) (hI : a ≠ ∞) : a / a = 1 := mul_inv_cancel h0 hI lemma mul_div_cancel (h0 : a ≠ 0) (hI : a ≠ ∞) : (b / a) * a = b := by rw [div_eq_mul_inv, mul_assoc, inv_mul_cancel h0 hI, mul_one] lemma mul_div_cancel' (h0 : a ≠ 0) (hI : a ≠ ∞) : a * (b / a) = b := by rw [mul_comm, mul_div_cancel h0 hI] lemma mul_div_le : a * (b / a) ≤ b := begin by_cases h0 : a = 0, { simp [h0] }, by_cases hI : a = ∞, { simp [hI] }, rw mul_div_cancel' h0 hI, exact le_refl b end lemma inv_two_add_inv_two : (2:ℝ≥0∞)⁻¹ + 2⁻¹ = 1 := by rw [← two_mul, ← div_eq_mul_inv, div_self two_ne_zero two_ne_top] lemma add_halves (a : ℝ≥0∞) : a / 2 + a / 2 = a := by rw [div_eq_mul_inv, ← mul_add, inv_two_add_inv_two, mul_one] @[simp] lemma div_zero_iff : a / b = 0 ↔ a = 0 ∨ b = ∞ := by simp [div_eq_mul_inv] @[simp] lemma div_pos_iff : 0 < a / b ↔ a ≠ 0 ∧ b ≠ ∞ := by simp [pos_iff_ne_zero, not_or_distrib] lemma half_pos {a : ℝ≥0∞} (h : 0 < a) : 0 < a / 2 := by simp [ne_of_gt h] lemma one_half_lt_one : (2⁻¹:ℝ≥0∞) < 1 := inv_lt_one.2 $ one_lt_two lemma half_lt_self {a : ℝ≥0∞} (hz : a ≠ 0) (ht : a ≠ ∞) : a / 2 < a := begin lift a to ℝ≥0 using ht, have h : (2 : ℝ≥0∞) = ((2 : ℝ≥0) : ℝ≥0∞), from rfl, have h' : (2 : ℝ≥0) ≠ 0, from _root_.two_ne_zero', rw [h, ← coe_div h', coe_lt_coe], -- `norm_cast` fails to apply `coe_div` norm_cast at hz, exact nnreal.half_lt_self hz end lemma sub_half (h : a ≠ ∞) : a - a / 2 = a / 2 := begin lift a to ℝ≥0 using h, exact sub_eq_of_add_eq (mul_ne_top coe_ne_top $ by simp) (add_halves a) end @[simp] lemma one_sub_inv_two : (1:ℝ≥0∞) - 2⁻¹ = 2⁻¹ := by simpa only [div_eq_mul_inv, one_mul] using sub_half one_ne_top lemma exists_inv_nat_lt {a : ℝ≥0∞} (h : a ≠ 0) : ∃n:ℕ, (n:ℝ≥0∞)⁻¹ < a := @inv_inv a ▸ by simp only [inv_lt_inv, ennreal.exists_nat_gt (inv_ne_top.2 h)] lemma exists_nat_pos_mul_gt (ha : a ≠ 0) (hb : b ≠ ∞) : ∃ n > 0, b < (n : ℕ) * a := begin have : b / a ≠ ∞, from mul_ne_top hb (inv_ne_top.2 ha), refine (ennreal.exists_nat_gt this).imp (λ n hn, _), have : 0 < (n : ℝ≥0∞), from (zero_le _).trans_lt hn, refine ⟨coe_nat_lt_coe_nat.1 this, _⟩, rwa [← ennreal.div_lt_iff (or.inl ha) (or.inr hb)] end lemma exists_nat_mul_gt (ha : a ≠ 0) (hb : b ≠ ∞) : ∃ n : ℕ, b < n * a := (exists_nat_pos_mul_gt ha hb).imp $ λ n, Exists.snd lemma exists_nat_pos_inv_mul_lt (ha : a ≠ ∞) (hb : b ≠ 0) : ∃ n > 0, ((n : ℕ) : ℝ≥0∞)⁻¹ * a < b := begin rcases exists_nat_pos_mul_gt hb ha with ⟨n, npos, hn⟩, have : (n : ℝ≥0∞) ≠ 0 := nat.cast_ne_zero.2 npos.lt.ne', use [n, npos], rwa [← one_mul b, ← inv_mul_cancel this coe_nat_ne_top, mul_assoc, mul_lt_mul_left (inv_ne_zero.2 coe_nat_ne_top) (inv_ne_top.2 this)] end lemma exists_nnreal_pos_mul_lt (ha : a ≠ ∞) (hb : b ≠ 0) : ∃ n > 0, ↑(n : ℝ≥0) * a < b := begin rcases exists_nat_pos_inv_mul_lt ha hb with ⟨n, npos : 0 < n, hn⟩, use (n : ℝ≥0)⁻¹, simp [*, npos.ne', zero_lt_one] end lemma exists_inv_two_pow_lt (ha : a ≠ 0) : ∃ n : ℕ, 2⁻¹ ^ n < a := begin rcases exists_inv_nat_lt ha with ⟨n, hn⟩, simp only [← ennreal.inv_pow], refine ⟨n, lt_trans (inv_lt_inv.2 _) hn⟩, norm_cast, exact n.lt_two_pow end end inv section real lemma to_real_add (ha : a ≠ ∞) (hb : b ≠ ∞) : (a+b).to_real = a.to_real + b.to_real := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, refl end lemma to_real_add_le : (a+b).to_real ≤ a.to_real + b.to_real := if ha : a = ∞ then by simp only [ha, top_add, top_to_real, zero_add, to_real_nonneg] else if hb : b = ∞ then by simp only [hb, add_top, top_to_real, add_zero, to_real_nonneg] else le_of_eq (to_real_add ha hb) lemma of_real_add {p q : ℝ} (hp : 0 ≤ p) (hq : 0 ≤ q) : ennreal.of_real (p + q) = ennreal.of_real p + ennreal.of_real q := by rw [ennreal.of_real, ennreal.of_real, ennreal.of_real, ← coe_add, coe_eq_coe, nnreal.of_real_add hp hq] lemma of_real_add_le {p q : ℝ} : ennreal.of_real (p + q) ≤ ennreal.of_real p + ennreal.of_real q := coe_le_coe.2 nnreal.of_real_add_le @[simp] lemma to_real_le_to_real (ha : a ≠ ∞) (hb : b ≠ ∞) : a.to_real ≤ b.to_real ↔ a ≤ b := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, norm_cast end @[simp] lemma to_real_lt_to_real (ha : a ≠ ∞) (hb : b ≠ ∞) : a.to_real < b.to_real ↔ a < b := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, norm_cast end lemma to_real_max (hr : a ≠ ∞) (hp : b ≠ ∞) : ennreal.to_real (max a b) = max (ennreal.to_real a) (ennreal.to_real b) := (le_total a b).elim (λ h, by simp only [h, (ennreal.to_real_le_to_real hr hp).2 h, max_eq_right]) (λ h, by simp only [h, (ennreal.to_real_le_to_real hp hr).2 h, max_eq_left]) lemma to_nnreal_pos_iff : 0 < a.to_nnreal ↔ (0 < a ∧ a ≠ ∞) := begin cases a, { simp [none_eq_top] }, { simp [some_eq_coe] } end lemma to_real_pos_iff : 0 < a.to_real ↔ (0 < a ∧ a ≠ ∞):= (nnreal.coe_pos).trans to_nnreal_pos_iff lemma of_real_le_of_real {p q : ℝ} (h : p ≤ q) : ennreal.of_real p ≤ ennreal.of_real q := by simp [ennreal.of_real, nnreal.of_real_le_of_real h] lemma of_real_le_of_le_to_real {a : ℝ} {b : ℝ≥0∞} (h : a ≤ ennreal.to_real b) : ennreal.of_real a ≤ b := (of_real_le_of_real h).trans of_real_to_real_le @[simp] lemma of_real_le_of_real_iff {p q : ℝ} (h : 0 ≤ q) : ennreal.of_real p ≤ ennreal.of_real q ↔ p ≤ q := by rw [ennreal.of_real, ennreal.of_real, coe_le_coe, nnreal.of_real_le_of_real_iff h] @[simp] lemma of_real_lt_of_real_iff {p q : ℝ} (h : 0 < q) : ennreal.of_real p < ennreal.of_real q ↔ p < q := by rw [ennreal.of_real, ennreal.of_real, coe_lt_coe, nnreal.of_real_lt_of_real_iff h] lemma of_real_lt_of_real_iff_of_nonneg {p q : ℝ} (hp : 0 ≤ p) : ennreal.of_real p < ennreal.of_real q ↔ p < q := by rw [ennreal.of_real, ennreal.of_real, coe_lt_coe, nnreal.of_real_lt_of_real_iff_of_nonneg hp] @[simp] lemma of_real_pos {p : ℝ} : 0 < ennreal.of_real p ↔ 0 < p := by simp [ennreal.of_real] @[simp] lemma of_real_eq_zero {p : ℝ} : ennreal.of_real p = 0 ↔ p ≤ 0 := by simp [ennreal.of_real] lemma of_real_le_iff_le_to_real {a : ℝ} {b : ℝ≥0∞} (hb : b ≠ ∞) : ennreal.of_real a ≤ b ↔ a ≤ ennreal.to_real b := begin lift b to ℝ≥0 using hb, simpa [ennreal.of_real, ennreal.to_real] using nnreal.of_real_le_iff_le_coe end lemma of_real_lt_iff_lt_to_real {a : ℝ} {b : ℝ≥0∞} (ha : 0 ≤ a) (hb : b ≠ ∞) : ennreal.of_real a < b ↔ a < ennreal.to_real b := begin lift b to ℝ≥0 using hb, simpa [ennreal.of_real, ennreal.to_real] using nnreal.of_real_lt_iff_lt_coe ha end lemma le_of_real_iff_to_real_le {a : ℝ≥0∞} {b : ℝ} (ha : a ≠ ∞) (hb : 0 ≤ b) : a ≤ ennreal.of_real b ↔ ennreal.to_real a ≤ b := begin lift a to ℝ≥0 using ha, simpa [ennreal.of_real, ennreal.to_real] using nnreal.le_of_real_iff_coe_le hb end lemma to_real_le_of_le_of_real {a : ℝ≥0∞} {b : ℝ} (hb : 0 ≤ b) (h : a ≤ ennreal.of_real b) : ennreal.to_real a ≤ b := have ha : a ≠ ∞, from ne_top_of_le_ne_top of_real_ne_top h, (le_of_real_iff_to_real_le ha hb).1 h lemma lt_of_real_iff_to_real_lt {a : ℝ≥0∞} {b : ℝ} (ha : a ≠ ∞) : a < ennreal.of_real b ↔ ennreal.to_real a < b := begin lift a to ℝ≥0 using ha, simpa [ennreal.of_real, ennreal.to_real] using nnreal.lt_of_real_iff_coe_lt end lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) : ennreal.of_real (p * q) = (ennreal.of_real p) * (ennreal.of_real q) := by { simp only [ennreal.of_real, coe_mul.symm, coe_eq_coe], exact nnreal.of_real_mul hp } lemma of_real_inv_of_pos {x : ℝ} (hx : 0 < x) : (ennreal.of_real x)⁻¹ = ennreal.of_real x⁻¹ := by rw [ennreal.of_real, ennreal.of_real, ←@coe_inv (nnreal.of_real x) (by simp [hx]), coe_eq_coe, nnreal.of_real_inv.symm] lemma of_real_div_of_pos {x y : ℝ} (hy : 0 < y) : ennreal.of_real (x / y) = ennreal.of_real x / ennreal.of_real y := by rw [div_eq_inv_mul, div_eq_mul_inv, of_real_mul (inv_nonneg.2 hy.le), of_real_inv_of_pos hy, mul_comm] lemma to_real_of_real_mul (c : ℝ) (a : ℝ≥0∞) (h : 0 ≤ c) : ennreal.to_real ((ennreal.of_real c) * a) = c * ennreal.to_real a := begin cases a, { simp only [none_eq_top, ennreal.to_real, top_to_nnreal, nnreal.coe_zero, mul_zero, mul_top], by_cases h' : c ≤ 0, { rw [if_pos], { simp }, { convert of_real_zero, exact le_antisymm h' h } }, { rw [if_neg], refl, rw [of_real_eq_zero], assumption } }, { simp only [ennreal.to_real, ennreal.to_nnreal], simp only [some_eq_coe, ennreal.of_real, coe_mul.symm, to_nnreal_coe, nnreal.coe_mul], congr, apply nnreal.coe_of_real, exact h } end @[simp] lemma to_nnreal_mul_top (a : ℝ≥0∞) : ennreal.to_nnreal (a * ∞) = 0 := begin by_cases h : a = 0, { rw [h, zero_mul, zero_to_nnreal] }, { rw [mul_top, if_neg h, top_to_nnreal] } end @[simp] lemma to_nnreal_top_mul (a : ℝ≥0∞) : ennreal.to_nnreal (∞ * a) = 0 := by rw [mul_comm, to_nnreal_mul_top] @[simp] lemma to_real_mul_top (a : ℝ≥0∞) : ennreal.to_real (a * ∞) = 0 := by rw [ennreal.to_real, to_nnreal_mul_top, nnreal.coe_zero] @[simp] lemma to_real_top_mul (a : ℝ≥0∞) : ennreal.to_real (∞ * a) = 0 := by { rw mul_comm, exact to_real_mul_top _ } lemma to_real_eq_to_real (ha : a < ∞) (hb : b < ∞) : ennreal.to_real a = ennreal.to_real b ↔ a = b := begin lift a to ℝ≥0 using ha.ne, lift b to ℝ≥0 using hb.ne, simp only [coe_eq_coe, nnreal.coe_eq, coe_to_real], end /-- `ennreal.to_nnreal` as a `monoid_hom`. -/ def to_nnreal_hom : ℝ≥0∞ →* ℝ≥0 := { to_fun := ennreal.to_nnreal, map_one' := to_nnreal_coe, map_mul' := by rintro (_|x) (_|y); simp only [← coe_mul, none_eq_top, some_eq_coe, to_nnreal_top_mul, to_nnreal_mul_top, top_to_nnreal, mul_zero, zero_mul, to_nnreal_coe] } lemma to_nnreal_mul {a b : ℝ≥0∞}: (a * b).to_nnreal = a.to_nnreal * b.to_nnreal := to_nnreal_hom.map_mul a b lemma to_nnreal_pow (a : ℝ≥0∞) (n : ℕ) : (a ^ n).to_nnreal = a.to_nnreal ^ n := to_nnreal_hom.map_pow a n lemma to_nnreal_prod {ι : Type*} {s : finset ι} {f : ι → ℝ≥0∞} : (∏ i in s, f i).to_nnreal = ∏ i in s, (f i).to_nnreal := to_nnreal_hom.map_prod _ _ /-- `ennreal.to_real` as a `monoid_hom`. -/ def to_real_hom : ℝ≥0∞ →* ℝ := (nnreal.to_real_hom : ℝ≥0 →* ℝ).comp to_nnreal_hom lemma to_real_mul : (a * b).to_real = a.to_real * b.to_real := to_real_hom.map_mul a b lemma to_real_pow (a : ℝ≥0∞) (n : ℕ) : (a ^ n).to_real = a.to_real ^ n := to_real_hom.map_pow a n lemma to_real_prod {ι : Type*} {s : finset ι} {f : ι → ℝ≥0∞} : (∏ i in s, f i).to_real = ∏ i in s, (f i).to_real := to_real_hom.map_prod _ _ lemma of_real_prod_of_nonneg {s : finset α} {f : α → ℝ} (hf : ∀ i, i ∈ s → 0 ≤ f i) : ennreal.of_real (∏ i in s, f i) = ∏ i in s, ennreal.of_real (f i) := begin simp_rw [ennreal.of_real, ←coe_finset_prod, coe_eq_coe], exact nnreal.of_real_prod_of_nonneg hf, end @[simp] lemma to_nnreal_bit0 {x : ℝ≥0∞} : (bit0 x).to_nnreal = bit0 (x.to_nnreal) := begin by_cases hx_top : x = ∞, { simp [hx_top, bit0_eq_top_iff.mpr rfl], }, exact to_nnreal_add (lt_top_iff_ne_top.mpr hx_top) (lt_top_iff_ne_top.mpr hx_top), end @[simp] lemma to_nnreal_bit1 {x : ℝ≥0∞} (hx_top : x ≠ ∞) : (bit1 x).to_nnreal = bit1 (x.to_nnreal) := by simp [bit1, bit1, to_nnreal_add (lt_top_iff_ne_top.mpr (by rwa [ne.def, bit0_eq_top_iff])) ennreal.one_lt_top] @[simp] lemma to_real_bit0 {x : ℝ≥0∞} : (bit0 x).to_real = bit0 (x.to_real) := by simp [ennreal.to_real] @[simp] lemma to_real_bit1 {x : ℝ≥0∞} (hx_top : x ≠ ∞) : (bit1 x).to_real = bit1 (x.to_real) := by simp [ennreal.to_real, hx_top] @[simp] lemma of_real_bit0 {r : ℝ} (hr : 0 ≤ r) : ennreal.of_real (bit0 r) = bit0 (ennreal.of_real r) := of_real_add hr hr @[simp] lemma of_real_bit1 {r : ℝ} (hr : 0 ≤ r) : ennreal.of_real (bit1 r) = bit1 (ennreal.of_real r) := (of_real_add (by simp [hr]) zero_le_one).trans (by simp [nnreal.of_real_one, bit1, hr]) end real section infi variables {ι : Sort*} {f g : ι → ℝ≥0∞} lemma infi_add : infi f + a = ⨅i, f i + a := le_antisymm (le_infi $ assume i, add_le_add (infi_le _ _) $ le_refl _) (ennreal.sub_le_iff_le_add.1 $ le_infi $ assume i, ennreal.sub_le_iff_le_add.2 $ infi_le _ _) lemma supr_sub : (⨆i, f i) - a = (⨆i, f i - a) := le_antisymm (ennreal.sub_le_iff_le_add.2 $ supr_le $ assume i, ennreal.sub_le_iff_le_add.1 $ le_supr _ i) (supr_le $ assume i, ennreal.sub_le_sub (le_supr _ _) (le_refl a)) lemma sub_infi : a - (⨅i, f i) = (⨆i, a - f i) := begin refine (eq_of_forall_ge_iff $ λ c, _), rw [ennreal.sub_le_iff_le_add, add_comm, infi_add], simp [ennreal.sub_le_iff_le_add, sub_eq_add_neg, add_comm], end lemma Inf_add {s : set ℝ≥0∞} : Inf s + a = ⨅b∈s, b + a := by simp [Inf_eq_infi, infi_add] lemma add_infi {a : ℝ≥0∞} : a + infi f = ⨅b, a + f b := by rw [add_comm, infi_add]; simp [add_comm] lemma infi_add_infi (h : ∀i j, ∃k, f k + g k ≤ f i + g j) : infi f + infi g = (⨅a, f a + g a) := suffices (⨅a, f a + g a) ≤ infi f + infi g, from le_antisymm (le_infi $ assume a, add_le_add (infi_le _ _) (infi_le _ _)) this, calc (⨅a, f a + g a) ≤ (⨅ a a', f a + g a') : le_infi $ assume a, le_infi $ assume a', let ⟨k, h⟩ := h a a' in infi_le_of_le k h ... ≤ infi f + infi g : by simp [add_infi, infi_add, -add_comm, -le_infi_iff]; exact le_refl _ lemma infi_sum {f : ι → α → ℝ≥0∞} {s : finset α} [nonempty ι] (h : ∀(t : finset α) (i j : ι), ∃k, ∀a∈t, f k a ≤ f i a ∧ f k a ≤ f j a) : (⨅i, ∑ a in s, f i a) = ∑ a in s, ⨅i, f i a := finset.induction_on s (by simp) $ assume a s ha ih, have ∀ (i j : ι), ∃ (k : ι), f k a + ∑ b in s, f k b ≤ f i a + ∑ b in s, f j b, from assume i j, let ⟨k, hk⟩ := h (insert a s) i j in ⟨k, add_le_add (hk a (finset.mem_insert_self _ _)).left $ finset.sum_le_sum $ assume a ha, (hk _ $ finset.mem_insert_of_mem ha).right⟩, by simp [ha, ih.symm, infi_add_infi this] lemma infi_mul {ι} [nonempty ι] {f : ι → ℝ≥0∞} {x : ℝ≥0∞} (h : x ≠ ∞) : infi f * x = ⨅i, f i * x := begin by_cases h2 : x = 0, simp only [h2, mul_zero, infi_const], refine le_antisymm (le_infi $ λ i, mul_right_mono $ infi_le _ _) ((div_le_iff_le_mul (or.inl h2) $ or.inl h).mp $ le_infi $ λ i, (div_le_iff_le_mul (or.inl h2) $ or.inl h).mpr $ infi_le _ _) end lemma mul_infi {ι} [nonempty ι] {f : ι → ℝ≥0∞} {x : ℝ≥0∞} (h : x ≠ ∞) : x * infi f = ⨅i, x * f i := by { rw [mul_comm, infi_mul h], simp only [mul_comm], assumption } /-! `supr_mul`, `mul_supr` and variants are in `topology.instances.ennreal`. -/ end infi section supr @[simp] lemma supr_eq_zero {ι : Sort*} {f : ι → ℝ≥0∞} : (⨆ i, f i) = 0 ↔ ∀ i, f i = 0 := supr_eq_bot @[simp] lemma supr_zero_eq_zero {ι : Sort*} : (⨆ i : ι, (0 : ℝ≥0∞)) = 0 := by simp lemma sup_eq_zero {a b : ℝ≥0∞} : a ⊔ b = 0 ↔ a = 0 ∧ b = 0 := sup_eq_bot_iff lemma supr_coe_nat : (⨆n:ℕ, (n : ℝ≥0∞)) = ∞ := (supr_eq_top _).2 $ assume b hb, ennreal.exists_nat_gt (lt_top_iff_ne_top.1 hb) end supr /-- `le_of_add_le_add_left` is normally applicable to `ordered_cancel_add_comm_monoid`, but it holds in `ℝ≥0∞` with the additional assumption that `a < ∞`. -/ lemma le_of_add_le_add_left {a b c : ℝ≥0∞} : a < ∞ → a + b ≤ a + c → b ≤ c := by cases a; cases b; cases c; simp [← ennreal.coe_add, ennreal.coe_le_coe] /-- `le_of_add_le_add_right` is normally applicable to `ordered_cancel_add_comm_monoid`, but it holds in `ℝ≥0∞` with the additional assumption that `a < ∞`. -/ lemma le_of_add_le_add_right {a b c : ℝ≥0∞} : a < ∞ → b + a ≤ c + a → b ≤ c := by simpa only [add_comm _ a] using le_of_add_le_add_left end ennreal
c0f4612e60daf2e8cba04851a4de6fb585b211f9
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/project_1_a_decrire/lean-scheme-submission/src/spectrum_of_a_ring/structure_presheaf_res.lean
b9bc1e1682d5cc9898dc9fd56c8417001f64efdc
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
4,866
lean
/- Relate restriction maps of the structure presheaf to the universal property of localization. -/ import spectrum_of_a_ring.structure_presheaf import spectrum_of_a_ring.structure_presheaf_localization universe u local attribute [instance] classical.prop_decidable noncomputable theory open topological_space open classical open localization open localization_alt section res -- We are given U and V in {D(f)} and V ⊆ U and we generalize the results that -- we already proved in properties. We deduce that there's a map from -- R[1/S(V)] to R[1/S(U)]. variables {R : Type u} [comm_ring R] variables {U V : opens (Spec R)} (BU : U ∈ D_fs R) (BV : V ∈ D_fs R) (H : V ⊆ U) include H -- V ⊆ U → D(g) ⊆ D(f). lemma Dfs_subset.of_basis_subset : Spec.DO R (some BV) ⊆ Spec.DO R (some BU) := begin rw [←some_spec BU, ←some_spec BV], exact H, end -- V ⊆ U → f ∈ R[1/g]*. def inverts_data.of_basis_subset.aux : inverts_data (powers (some BU)) (of : R → localization R (powers (some BV))) := begin intros s, have Hs := inverts.of_Dfs_subset (Dfs_subset.of_basis_subset BU BV H) s, rcases (indefinite_description _ Hs) with ⟨si, Hsi⟩, exact ⟨si, Hsi⟩, end -- V ⊆ U → ∃ a e, g^e = a * f. def pow_eq.of_basis_subset : ∃ (a : R) (e : ℕ), (some BV)^e = a * (some BU) := pow_eq.of_Dfs_subset (Dfs_subset.of_basis_subset BU BV H) include BU BV -- V ⊆ U → S(U) ⊆ R[1/S(V)]*. def inverts_data.of_basis_subset : inverts_data (S U) (of : R → localization R (S V)) := begin intros s, rcases s with ⟨s, Hs⟩, change U ⊆ Spec.DO R (s) at Hs, have HsV : s ∈ S V := set.subset.trans H Hs, use ⟦⟨1, ⟨s, HsV⟩⟩⟧, apply quotient.sound, use [1, is_submonoid.one_mem _], simp, end -- The induced map is the restriction map in the structure presheaf. def structure_presheaf_on_basis.res : localization R (S U) → localization R (S V) := is_localization_initial (S U) (of : R → localization R (S U)) (of.is_localization_data (S U)) (of : R → localization R (S V)) (inverts_data.of_basis_subset BU BV H) -- Useful. instance structure_presheaf_on_basis.res.is_ring_hom : is_ring_hom (structure_presheaf_on_basis.res BU BV H) := by simp [structure_presheaf_on_basis.res]; by apply_instance end res section res_eq variables {R : Type u} [comm_ring R] lemma structure_presheaf_on_basis.res_eq : (structure_presheaf_on_basis R).res = @structure_presheaf_on_basis.res R _ := begin apply funext, intro U, apply funext, intro V, apply funext, intro BU, apply funext, intro BV, apply funext, intro HVU, apply funext, intro x, -- x ∈ R[1/S(U)]. apply quotient.induction_on x, rintros ⟨a, b⟩, -- Destruct. simp [structure_presheaf_on_basis.res, is_localization_initial], rcases ((of.is_localization_data (S U)).has_denom ⟦(a, b)⟧) with ⟨⟨q, p⟩, Hpq⟩, rcases inverts_data.of_basis_subset BU BV HVU q with ⟨w, Hw⟩, dsimp only [subtype.coe_mk] at *, revert Hw, -- Induction on w. apply quotient.induction_on w, rintros ⟨c, d⟩ Hw, apply quotient.sound, erw quotient.eq at Hpq, erw quotient.eq at Hw, rcases Hpq with ⟨t, ⟨Ht, Hpq⟩⟩; simp [-sub_eq_add_neg, sub_mul, sub_eq_zero] at Hpq, rcases Hw with ⟨s, ⟨Hs, Hw⟩⟩; simp [-sub_eq_add_neg, sub_mul, sub_eq_zero] at Hw, -- Kill it. replace Ht := (S.rev_mono HVU) Ht, use [s * t, is_submonoid.mul_mem Hs Ht], simp [-sub_eq_add_neg], rw sub_mul, rw sub_eq_zero, repeat { rw ←mul_assoc, }, iterate 2 { rw [mul_assoc _ _ t, mul_comm _ t, ←mul_assoc] }, erw Hpq, symmetry, iterate 1 { rw [mul_assoc _ _ s, mul_comm _ s, ←mul_assoc] }, rw Hw, ring, end lemma structure_presheaf_on_basis.res_comp {U V W : opens (Spec R)} (BU : U ∈ D_fs R) (BV : V ∈ D_fs R) (BW : W ∈ D_fs R) (HVU : V ⊆ U) (HWV : W ⊆ V) : structure_presheaf_on_basis.res BU BW (set.subset.trans HWV HVU) = structure_presheaf_on_basis.res BV BW HWV ∘ structure_presheaf_on_basis.res BU BV HVU := begin rw ←structure_presheaf_on_basis.res_eq, rw (structure_presheaf_on_basis R).Hcomp, end lemma structure_presheaf_on_basis.res_comp_of {U V : opens (Spec R)} (BU : U ∈ D_fs R) (BV : V ∈ D_fs R) (H : V ⊆ U) : (of : R → localization R (S (V))) = structure_presheaf_on_basis.res BU BV H ∘ (of : R → localization R (S (U))) := begin apply funext, intro x, simp [structure_presheaf_on_basis.res], rw is_localization_initial_comp, end lemma structure_presheaf_on_basis.res_comp_of' {U V : opens (Spec R)} (BU : U ∈ D_fs R) (BV : V ∈ D_fs R) (H : V ⊆ U) : ∀ x, (of : R → localization R (S (V))) x = structure_presheaf_on_basis.res BU BV H ((of : R → localization R (S (U))) x) := begin intros x, rw structure_presheaf_on_basis.res_comp_of BU BV H, end end res_eq
4985240519fb8942061a712300d19e1eb75ad835
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/order/compactly_generated.lean
0121795418c846186c58fe601a41663dbfb8707e
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
20,368
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import data.finset.order import order.atoms import order.order_iso_nat import order.zorn import tactic.tfae /-! # Compactness properties for complete lattices For complete lattices, there are numerous equivalent ways to express the fact that the relation `>` is well-founded. In this file we define three especially-useful characterisations and provide proofs that they are indeed equivalent to well-foundedness. ## Main definitions * `complete_lattice.is_sup_closed_compact` * `complete_lattice.is_Sup_finite_compact` * `complete_lattice.is_compact_element` * `complete_lattice.is_compactly_generated` ## Main results The main result is that the following four conditions are equivalent for a complete lattice: * `well_founded (>)` * `complete_lattice.is_sup_closed_compact` * `complete_lattice.is_Sup_finite_compact` * `∀ k, complete_lattice.is_compact_element k` This is demonstrated by means of the following four lemmas: * `complete_lattice.well_founded.is_Sup_finite_compact` * `complete_lattice.is_Sup_finite_compact.is_sup_closed_compact` * `complete_lattice.is_sup_closed_compact.well_founded` * `complete_lattice.is_Sup_finite_compact_iff_all_elements_compact` We also show well-founded lattices are compactly generated (`complete_lattice.compactly_generated_of_well_founded`). ## References - [G. Călugăreanu, *Lattice Concepts of Module Theory*][calugareanu] ## Tags complete lattice, well-founded, compact -/ variables {α : Type*} [complete_lattice α] namespace complete_lattice variables (α) /-- A compactness property for a complete lattice is that any `sup`-closed non-empty subset contains its `Sup`. -/ def is_sup_closed_compact : Prop := ∀ (s : set α) (h : s.nonempty), (∀ a b, a ∈ s → b ∈ s → a ⊔ b ∈ s) → (Sup s) ∈ s /-- A compactness property for a complete lattice is that any subset has a finite subset with the same `Sup`. -/ def is_Sup_finite_compact : Prop := ∀ (s : set α), ∃ (t : finset α), ↑t ⊆ s ∧ Sup s = t.sup id /-- An element `k` of a complete lattice is said to be compact if any set with `Sup` above `k` has a finite subset with `Sup` above `k`. Such an element is also called "finite" or "S-compact". -/ def is_compact_element {α : Type*} [complete_lattice α] (k : α) := ∀ s : set α, k ≤ Sup s → ∃ t : finset α, ↑t ⊆ s ∧ k ≤ t.sup id /-- An element `k` is compact if and only if any directed set with `Sup` above `k` already got above `k` at some point in the set. -/ theorem is_compact_element_iff_le_of_directed_Sup_le (k : α) : is_compact_element k ↔ ∀ s : set α, s.nonempty → directed_on (≤) s → k ≤ Sup s → ∃ x : α, x ∈ s ∧ k ≤ x := begin classical, split, { by_cases hbot : k = ⊥, -- Any nonempty directed set certainly has sup above ⊥ { rintros _ _ ⟨x, hx⟩ _ _, use x, by simp only [hx, hbot, bot_le, and_self], }, { intros hk s hne hdir hsup, obtain ⟨t, ht⟩ := hk s hsup, -- If t were empty, its sup would be ⊥, which is not above k ≠ ⊥. have tne : t.nonempty, { by_contradiction n, rw [finset.nonempty_iff_ne_empty, not_not] at n, simp only [n, true_and, set.empty_subset, finset.coe_empty, finset.sup_empty, le_bot_iff] at ht, exact absurd ht hbot, }, -- certainly every element of t is below something in s, since ↑t ⊆ s. have t_below_s : ∀ x ∈ t, ∃ y ∈ s, x ≤ y, from λ x hxt, ⟨x, ht.left hxt, by refl⟩, obtain ⟨x, ⟨hxs, hsupx⟩⟩ := finset.sup_le_of_le_directed s hne hdir t t_below_s, exact ⟨x, ⟨hxs, le_trans ht.right hsupx⟩⟩, }, }, { intros hk s hsup, -- Consider the set of finite joins of elements of the (plain) set s. let S : set α := { x | ∃ t : finset α, ↑t ⊆ s ∧ x = t.sup id }, -- S is directed, nonempty, and still has sup above k. have dir_US : directed_on (≤) S, { rintros x ⟨c, hc⟩ y ⟨d, hd⟩, use x ⊔ y, split, { use c ∪ d, split, { simp only [hc.left, hd.left, set.union_subset_iff, finset.coe_union, and_self], }, { simp only [hc.right, hd.right, finset.sup_union], }, }, simp only [and_self, le_sup_left, le_sup_right], }, have sup_S : Sup s ≤ Sup S, { apply Sup_le_Sup, intros x hx, use {x}, simpa only [and_true, id.def, finset.coe_singleton, eq_self_iff_true, finset.sup_singleton, set.singleton_subset_iff], }, have Sne : S.nonempty, { suffices : ⊥ ∈ S, from set.nonempty_of_mem this, use ∅, simp only [set.empty_subset, finset.coe_empty, finset.sup_empty, eq_self_iff_true, and_self], }, -- Now apply the defn of compact and finish. obtain ⟨j, ⟨hjS, hjk⟩⟩ := hk S Sne dir_US (le_trans hsup sup_S), obtain ⟨t, ⟨htS, htsup⟩⟩ := hjS, use t, exact ⟨htS, by rwa ←htsup⟩, }, end /-- A compact element `k` has the property that any directed set lying strictly below `k` has its Sup strictly below `k`. -/ lemma is_compact_element.directed_Sup_lt_of_lt {α : Type*} [complete_lattice α] {k : α} (hk : is_compact_element k) {s : set α} (hemp : s.nonempty) (hdir : directed_on (≤) s) (hbelow : ∀ x ∈ s, x < k) : Sup s < k := begin rw is_compact_element_iff_le_of_directed_Sup_le at hk, by_contradiction, have sSup : Sup s ≤ k, from Sup_le (λ s hs, (hbelow s hs).le), replace sSup : Sup s = k := eq_iff_le_not_lt.mpr ⟨sSup, h⟩, obtain ⟨x, hxs, hkx⟩ := hk s hemp hdir sSup.symm.le, obtain hxk := hbelow x hxs, exact hxk.ne (hxk.le.antisymm hkx), end lemma finset_sup_compact_of_compact {α β : Type*} [complete_lattice α] {f : β → α} (s : finset β) (h : ∀ x ∈ s, is_compact_element (f x)) : is_compact_element (s.sup f) := begin classical, rw is_compact_element_iff_le_of_directed_Sup_le, intros d hemp hdir hsup, change f with id ∘ f, rw ←finset.sup_finset_image, apply finset.sup_le_of_le_directed d hemp hdir, rintros x hx, obtain ⟨p, ⟨hps, rfl⟩⟩ := finset.mem_image.mp hx, specialize h p hps, rw is_compact_element_iff_le_of_directed_Sup_le at h, specialize h d hemp hdir (le_trans (finset.le_sup hps) hsup), simpa only [exists_prop], end lemma well_founded.is_Sup_finite_compact (h : well_founded ((>) : α → α → Prop)) : is_Sup_finite_compact α := begin intros s, let p : set α := { x | ∃ (t : finset α), ↑t ⊆ s ∧ t.sup id = x }, have hp : p.nonempty, { use [⊥, ∅], simp, }, obtain ⟨m, ⟨t, ⟨ht₁, ht₂⟩⟩, hm⟩ := well_founded.well_founded_iff_has_max'.mp h p hp, use t, simp only [ht₁, ht₂, true_and], apply le_antisymm, { apply Sup_le, intros y hy, classical, have hy' : (insert y t).sup id ∈ p, { use insert y t, simp, rw set.insert_subset, exact ⟨hy, ht₁⟩, }, have hm' : m ≤ (insert y t).sup id, { rw ← ht₂, exact finset.sup_mono (t.subset_insert y), }, rw ← hm _ hy' hm', simp, }, { rw [← ht₂, finset.sup_id_eq_Sup], exact Sup_le_Sup ht₁, }, end lemma is_Sup_finite_compact.is_sup_closed_compact (h : is_Sup_finite_compact α) : is_sup_closed_compact α := begin intros s hne hsc, obtain ⟨t, ht₁, ht₂⟩ := h s, clear h, cases t.eq_empty_or_nonempty with h h, { subst h, rw finset.sup_empty at ht₂, rw ht₂, simp [eq_singleton_bot_of_Sup_eq_bot_of_nonempty ht₂ hne], }, { rw ht₂, exact t.sup_closed_of_sup_closed h ht₁ hsc, }, end lemma is_sup_closed_compact.well_founded (h : is_sup_closed_compact α) : well_founded ((>) : α → α → Prop) := begin refine rel_embedding.well_founded_iff_no_descending_seq.mpr ⟨λ a, _⟩, suffices : Sup (set.range a) ∈ set.range a, { obtain ⟨n, hn⟩ := set.mem_range.mp this, have h' : Sup (set.range a) < a (n+1), { change _ > _, simp [← hn, a.map_rel_iff], }, apply lt_irrefl (a (n+1)), apply lt_of_le_of_lt _ h', apply le_Sup, apply set.mem_range_self, }, apply h (set.range a), { use a 37, apply set.mem_range_self, }, { rintros x y ⟨m, hm⟩ ⟨n, hn⟩, use m ⊔ n, rw [← hm, ← hn], apply a.to_rel_hom.map_sup, }, end lemma is_Sup_finite_compact_iff_all_elements_compact : is_Sup_finite_compact α ↔ (∀ k : α, is_compact_element k) := begin split, { intros h k s hs, obtain ⟨t, ⟨hts, htsup⟩⟩ := h s, use [t, hts], rwa ←htsup, }, { intros h s, obtain ⟨t, ⟨hts, htsup⟩⟩ := h (Sup s) s (by refl), have : Sup s = t.sup id, { suffices : t.sup id ≤ Sup s, by { apply le_antisymm; assumption }, simp only [id.def, finset.sup_le_iff], intros x hx, apply le_Sup, exact hts hx, }, use [t, hts], assumption, }, end lemma well_founded_characterisations : tfae [well_founded ((>) : α → α → Prop), is_Sup_finite_compact α, is_sup_closed_compact α, ∀ k : α, is_compact_element k] := begin tfae_have : 1 → 2, by { exact well_founded.is_Sup_finite_compact α, }, tfae_have : 2 → 3, by { exact is_Sup_finite_compact.is_sup_closed_compact α, }, tfae_have : 3 → 1, by { exact is_sup_closed_compact.well_founded α, }, tfae_have : 2 ↔ 4, by { exact is_Sup_finite_compact_iff_all_elements_compact α }, tfae_finish, end lemma well_founded_iff_is_Sup_finite_compact : well_founded ((>) : α → α → Prop) ↔ is_Sup_finite_compact α := (well_founded_characterisations α).out 0 1 lemma is_Sup_finite_compact_iff_is_sup_closed_compact : is_Sup_finite_compact α ↔ is_sup_closed_compact α := (well_founded_characterisations α).out 1 2 lemma is_sup_closed_compact_iff_well_founded : is_sup_closed_compact α ↔ well_founded ((>) : α → α → Prop) := (well_founded_characterisations α).out 2 0 alias well_founded_iff_is_Sup_finite_compact ↔ _ is_Sup_finite_compact.well_founded alias is_Sup_finite_compact_iff_is_sup_closed_compact ↔ _ is_sup_closed_compact.is_Sup_finite_compact alias is_sup_closed_compact_iff_well_founded ↔ _ well_founded.is_sup_closed_compact end complete_lattice /-- A complete lattice is said to be compactly generated if any element is the `Sup` of compact elements. -/ class is_compactly_generated (α : Type*) [complete_lattice α] : Prop := (exists_Sup_eq : ∀ (x : α), ∃ (s : set α), (∀ x ∈ s, complete_lattice.is_compact_element x) ∧ Sup s = x) section variables {α} [is_compactly_generated α] {a b : α} {s : set α} @[simp] lemma Sup_compact_le_eq (b) : Sup {c : α | complete_lattice.is_compact_element c ∧ c ≤ b} = b := begin rcases is_compactly_generated.exists_Sup_eq b with ⟨s, hs, rfl⟩, exact le_antisymm (Sup_le (λ c hc, hc.2)) (Sup_le_Sup (λ c cs, ⟨hs c cs, le_Sup cs⟩)), end @[simp] theorem Sup_compact_eq_top : Sup {a : α | complete_lattice.is_compact_element a} = ⊤ := begin refine eq.trans (congr rfl (set.ext (λ x, _))) (Sup_compact_le_eq ⊤), exact (and_iff_left le_top).symm, end theorem le_iff_compact_le_imp {a b : α} : a ≤ b ↔ ∀ c : α, complete_lattice.is_compact_element c → c ≤ a → c ≤ b := ⟨λ ab c hc ca, le_trans ca ab, λ h, begin rw [← Sup_compact_le_eq a, ← Sup_compact_le_eq b], exact Sup_le_Sup (λ c hc, ⟨hc.1, h c hc.1 hc.2⟩), end⟩ /-- This property is sometimes referred to as `α` being upper continuous. -/ theorem inf_Sup_eq_of_directed_on (h : directed_on (≤) s): a ⊓ Sup s = ⨆ b ∈ s, a ⊓ b := le_antisymm (begin rw le_iff_compact_le_imp, by_cases hs : s.nonempty, { intros c hc hcinf, rw le_inf_iff at hcinf, rw complete_lattice.is_compact_element_iff_le_of_directed_Sup_le at hc, rcases hc s hs h hcinf.2 with ⟨d, ds, cd⟩, exact (le_inf hcinf.1 cd).trans (le_bsupr d ds) }, { rw set.not_nonempty_iff_eq_empty at hs, simp [hs] } end) supr_inf_le_inf_Sup /-- This property is equivalent to `α` being upper continuous. -/ theorem inf_Sup_eq_supr_inf_sup_finset : a ⊓ Sup s = ⨆ (t : finset α) (H : ↑t ⊆ s), a ⊓ (t.sup id) := le_antisymm (begin rw le_iff_compact_le_imp, intros c hc hcinf, rw le_inf_iff at hcinf, rcases hc s hcinf.2 with ⟨t, ht1, ht2⟩, exact (le_inf hcinf.1 ht2).trans (le_bsupr t ht1), end) (supr_le $ λ t, supr_le $ λ h, inf_le_inf_left _ ((finset.sup_id_eq_Sup t).symm ▸ (Sup_le_Sup h))) theorem complete_lattice.set_independent_iff_finite {s : set α} : complete_lattice.set_independent s ↔ ∀ t : finset α, ↑t ⊆ s → complete_lattice.set_independent (↑t : set α) := ⟨λ hs t ht, hs.mono ht, λ h a ha, begin rw [disjoint_iff, inf_Sup_eq_supr_inf_sup_finset, supr_eq_bot], intro t, rw [supr_eq_bot, finset.sup_id_eq_Sup], intro ht, classical, have h' := (h (insert a t) _ (t.mem_insert_self a)).eq_bot, { rwa [finset.coe_insert, set.insert_diff_self_of_not_mem] at h', exact λ con, ((set.mem_diff a).1 (ht con)).2 (set.mem_singleton a) }, { rw [finset.coe_insert, set.insert_subset], exact ⟨ha, set.subset.trans ht (set.diff_subset _ _)⟩ } end⟩ lemma complete_lattice.set_independent_Union_of_directed {η : Type*} {s : η → set α} (hs : directed (⊆) s) (h : ∀ i, complete_lattice.set_independent (s i)) : complete_lattice.set_independent (⋃ i, s i) := begin by_cases hη : nonempty η, { resetI, rw complete_lattice.set_independent_iff_finite, intros t ht, obtain ⟨I, fi, hI⟩ := set.finite_subset_Union t.finite_to_set ht, obtain ⟨i, hi⟩ := hs.finset_le fi.to_finset, exact (h i).mono (set.subset.trans hI $ set.bUnion_subset $ λ j hj, hi j (fi.mem_to_finset.2 hj)) }, { rintros a ⟨_, ⟨i, _⟩, _⟩, exfalso, exact hη ⟨i⟩, }, end lemma complete_lattice.independent_sUnion_of_directed {s : set (set α)} (hs : directed_on (⊆) s) (h : ∀ a ∈ s, complete_lattice.set_independent a) : complete_lattice.set_independent (⋃₀ s) := by rw set.sUnion_eq_Union; exact complete_lattice.set_independent_Union_of_directed hs.directed_coe (by simpa using h) end namespace complete_lattice lemma compactly_generated_of_well_founded (h : well_founded ((>) : α → α → Prop)) : is_compactly_generated α := begin rw [well_founded_iff_is_Sup_finite_compact, is_Sup_finite_compact_iff_all_elements_compact] at h, -- x is the join of the set of compact elements {x} exact ⟨λ x, ⟨{x}, ⟨λ x _, h x, Sup_singleton⟩⟩⟩, end /-- A compact element `k` has the property that any `b < `k lies below a "maximal element below `k`", which is to say `[⊥, k]` is coatomic. -/ theorem Iic_coatomic_of_compact_element {k : α} (h : is_compact_element k) : is_coatomic (set.Iic k) := ⟨λ ⟨b, hbk⟩, begin by_cases htriv : b = k, { left, ext, simp only [htriv, set.Iic.coe_top, subtype.coe_mk], }, right, rcases zorn.zorn_nonempty_partial_order₀ (set.Iio k) _ b (lt_of_le_of_ne hbk htriv) with ⟨a, a₀, ba, h⟩, { refine ⟨⟨a, le_of_lt a₀⟩, ⟨ne_of_lt a₀, λ c hck, by_contradiction $ λ c₀, _⟩, ba⟩, cases h c.1 (lt_of_le_of_ne c.2 (λ con, c₀ (subtype.ext con))) hck.le, exact lt_irrefl _ hck, }, { intros S SC cC I IS, by_cases hS : S.nonempty, { exact ⟨Sup S, h.directed_Sup_lt_of_lt hS cC.directed_on SC, λ _, le_Sup⟩, }, exact ⟨b, lt_of_le_of_ne hbk htriv, by simp only [set.not_nonempty_iff_eq_empty.mp hS, set.mem_empty_eq, forall_const, forall_prop_of_false, not_false_iff]⟩, }, end⟩ lemma coatomic_of_top_compact (h : is_compact_element (⊤ : α)) : is_coatomic α := (@order_iso.Iic_top α _).is_coatomic_iff.mp (Iic_coatomic_of_compact_element h) end complete_lattice section variables [is_modular_lattice α] [is_compactly_generated α] @[priority 100] instance is_atomic_of_is_complemented [is_complemented α] : is_atomic α := ⟨λ b, begin by_cases h : {c : α | complete_lattice.is_compact_element c ∧ c ≤ b} ⊆ {⊥}, { left, rw [← Sup_compact_le_eq b, Sup_eq_bot], exact h }, { rcases set.not_subset.1 h with ⟨c, ⟨hc, hcb⟩, hcbot⟩, right, have hc' := complete_lattice.Iic_coatomic_of_compact_element hc, rw ← is_atomic_iff_is_coatomic at hc', haveI := hc', obtain con | ⟨a, ha, hac⟩ := eq_bot_or_exists_atom_le (⟨c, le_refl c⟩ : set.Iic c), { exfalso, apply hcbot, simp only [subtype.ext_iff, set.Iic.coe_bot, subtype.coe_mk] at con, exact con }, rw [← subtype.coe_le_coe, subtype.coe_mk] at hac, exact ⟨a, ha.of_is_atom_coe_Iic, hac.trans hcb⟩ }, end⟩ /-- See Lemma 5.1, Călugăreanu -/ @[priority 100] instance is_atomistic_of_is_complemented [is_complemented α] : is_atomistic α := ⟨λ b, ⟨{a | is_atom a ∧ a ≤ b}, begin symmetry, have hle : Sup {a : α | is_atom a ∧ a ≤ b} ≤ b := (Sup_le $ λ _, and.right), apply (lt_or_eq_of_le hle).resolve_left (λ con, _), obtain ⟨c, hc⟩ := exists_is_compl (⟨Sup {a : α | is_atom a ∧ a ≤ b}, hle⟩ : set.Iic b), obtain rfl | ⟨a, ha, hac⟩ := eq_bot_or_exists_atom_le c, { exact ne_of_lt con (subtype.ext_iff.1 (eq_top_of_is_compl_bot hc)) }, { apply ha.1, rw eq_bot_iff, apply le_trans (le_inf _ hac) hc.1, rw [← subtype.coe_le_coe, subtype.coe_mk], exact le_Sup ⟨ha.of_is_atom_coe_Iic, a.2⟩ } end, λ _, and.left⟩⟩ /-- See Theorem 6.6, Călugăreanu -/ theorem is_complemented_of_Sup_atoms_eq_top (h : Sup {a : α | is_atom a} = ⊤) : is_complemented α := ⟨λ b, begin obtain ⟨s, ⟨s_ind, b_inf_Sup_s, s_atoms⟩, s_max⟩ := zorn.zorn_subset {s : set α | complete_lattice.set_independent s ∧ b ⊓ Sup s = ⊥ ∧ ∀ a ∈ s, is_atom a} _, { refine ⟨Sup s, le_of_eq b_inf_Sup_s, _⟩, rw [← h, Sup_le_iff], intros a ha, rw ← inf_eq_left, refine (eq_bot_or_eq_of_le_atom ha inf_le_left).resolve_left (λ con, ha.1 _), rw [eq_bot_iff, ← con], refine le_inf (le_refl a) ((le_Sup _).trans le_sup_right), rw ← disjoint_iff at *, have a_dis_Sup_s : disjoint a (Sup s) := con.mono_right le_sup_right, rw ← s_max (s ∪ {a}) ⟨λ x hx, _, ⟨_, λ x hx, _⟩⟩ (set.subset_union_left _ _), { exact set.mem_union_right _ (set.mem_singleton _) }, { rw [set.mem_union, set.mem_singleton_iff] at hx, by_cases xa : x = a, { simp only [xa, set.mem_singleton, set.insert_diff_of_mem, set.union_singleton], exact con.mono_right (le_trans (Sup_le_Sup (set.diff_subset s {a})) le_sup_right) }, { have h : (s ∪ {a}) \ {x} = (s \ {x}) ∪ {a}, { simp only [set.union_singleton], rw set.insert_diff_of_not_mem, rw set.mem_singleton_iff, exact ne.symm xa }, rw [h, Sup_union, Sup_singleton], apply (s_ind (hx.resolve_right xa)).disjoint_sup_right_of_disjoint_sup_left (a_dis_Sup_s.mono_right _).symm, rw [← Sup_insert, set.insert_diff_singleton, set.insert_eq_of_mem (hx.resolve_right xa)] } }, { rw [Sup_union, Sup_singleton, ← disjoint_iff], exact b_inf_Sup_s.disjoint_sup_right_of_disjoint_sup_left con.symm }, { rw [set.mem_union, set.mem_singleton_iff] at hx, cases hx, { exact s_atoms x hx }, { rw hx, exact ha } } }, { intros c hc1 hc2, refine ⟨⋃₀ c, ⟨complete_lattice.independent_sUnion_of_directed hc2.directed_on (λ s hs, (hc1 hs).1), _, λ a ha, _⟩, λ _, set.subset_sUnion_of_mem⟩, { rw [Sup_sUnion, ← Sup_image, inf_Sup_eq_of_directed_on, supr_eq_bot], { intro i, rw supr_eq_bot, intro hi, obtain ⟨x, xc, rfl⟩ := (set.mem_image _ _ _).1 hi, exact (hc1 xc).2.1 }, { rw directed_on_image, refine hc2.directed_on.mono (λ s t, Sup_le_Sup) } }, { rcases set.mem_sUnion.1 ha with ⟨s, sc, as⟩, exact (hc1 sc).2.2 a as } } end⟩ /-- See Theorem 6.6, Călugăreanu -/ theorem is_complemented_of_is_atomistic [is_atomistic α] : is_complemented α := is_complemented_of_Sup_atoms_eq_top Sup_atoms_eq_top theorem is_complemented_iff_is_atomistic : is_complemented α ↔ is_atomistic α := begin split; introsI, { exact is_atomistic_of_is_complemented }, { exact is_complemented_of_is_atomistic } end end
4827d5cb9c5f6fbbb5779a7941610e9d955f669b
b561a44b48979a98df50ade0789a21c79ee31288
/stage0/src/Init/Meta.lean
29b98bcbbd8e32aa64f55f2912f77dddb651b2d9
[ "Apache-2.0" ]
permissive
3401ijk/lean4
97659c475ebd33a034fed515cb83a85f75ccfb06
a5b1b8de4f4b038ff752b9e607b721f15a9a4351
refs/heads/master
1,693,933,007,651
1,636,424,845,000
1,636,424,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
34,011
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 and Sebastian Ullrich Additional goodies for writing macros -/ prelude import Init.Data.Array.Basic namespace Lean @[extern c inline "lean_box(LEAN_VERSION_MAJOR)"] private constant version.getMajor (u : Unit) : Nat def version.major : Nat := version.getMajor () @[extern c inline "lean_box(LEAN_VERSION_MINOR)"] private constant version.getMinor (u : Unit) : Nat def version.minor : Nat := version.getMinor () @[extern c inline "lean_box(LEAN_VERSION_PATCH)"] private constant version.getPatch (u : Unit) : Nat def version.patch : Nat := version.getPatch () -- @[extern c inline "lean_mk_string(LEAN_GITHASH)"] -- constant getGithash (u : Unit) : String -- def githash : String := getGithash () @[extern c inline "LEAN_VERSION_IS_RELEASE"] constant version.getIsRelease (u : Unit) : Bool def version.isRelease : Bool := version.getIsRelease () /-- Additional version description like "nightly-2018-03-11" -/ @[extern c inline "lean_mk_string(LEAN_SPECIAL_VERSION_DESC)"] constant version.getSpecialDesc (u : Unit) : String def version.specialDesc : String := version.getSpecialDesc () /- Valid identifier names -/ def isGreek (c : Char) : Bool := 0x391 ≤ c.val && c.val ≤ 0x3dd def isLetterLike (c : Char) : Bool := (0x3b1 ≤ c.val && c.val ≤ 0x3c9 && c.val ≠ 0x3bb) || -- Lower greek, but lambda (0x391 ≤ c.val && c.val ≤ 0x3A9 && c.val ≠ 0x3A0 && c.val ≠ 0x3A3) || -- Upper greek, but Pi and Sigma (0x3ca ≤ c.val && c.val ≤ 0x3fb) || -- Coptic letters (0x1f00 ≤ c.val && c.val ≤ 0x1ffe) || -- Polytonic Greek Extended Character Set (0x2100 ≤ c.val && c.val ≤ 0x214f) || -- Letter like block (0x1d49c ≤ c.val && c.val ≤ 0x1d59f) -- Latin letters, Script, Double-struck, Fractur def isNumericSubscript (c : Char) : Bool := 0x2080 ≤ c.val && c.val ≤ 0x2089 def isSubScriptAlnum (c : Char) : Bool := isNumericSubscript c || (0x2090 ≤ c.val && c.val ≤ 0x209c) || (0x1d62 ≤ c.val && c.val ≤ 0x1d6a) def isIdFirst (c : Char) : Bool := c.isAlpha || c = '_' || isLetterLike c def isIdRest (c : Char) : Bool := c.isAlphanum || c = '_' || c = '\'' || c == '!' || c == '?' || isLetterLike c || isSubScriptAlnum c def idBeginEscape := '«' def idEndEscape := '»' def isIdBeginEscape (c : Char) : Bool := c = idBeginEscape def isIdEndEscape (c : Char) : Bool := c = idEndEscape namespace Name def getRoot : Name → Name | anonymous => anonymous | n@(str anonymous _ _) => n | n@(num anonymous _ _) => n | str n _ _ => getRoot n | num n _ _ => getRoot n @[export lean_is_inaccessible_user_name] def isInaccessibleUserName : Name → Bool | Name.str _ s _ => s.contains '✝' || s == "_inaccessible" | Name.num p idx _ => isInaccessibleUserName p | _ => false def escapePart (s : String) : Option String := if s.length > 0 && isIdFirst s[0] && (s.toSubstring.drop 1).all isIdRest then s else if s.any isIdEndEscape then none else some <| idBeginEscape.toString ++ s ++ idEndEscape.toString -- NOTE: does not roundtrip even with `escape = true` if name is anonymous or contains numeric part or `idEndEscape` variable (sep : String) (escape : Bool) def toStringWithSep : Name → String | anonymous => "[anonymous]" | str anonymous s _ => maybeEscape s | num anonymous v _ => toString v | str n s _ => toStringWithSep n ++ sep ++ maybeEscape s | num n v _ => toStringWithSep n ++ sep ++ Nat.repr v where maybeEscape s := if escape then escapePart s |>.getD s else s protected def toString (n : Name) (escape := true) : String := -- never escape "prettified" inaccessible names or macro scopes or pseudo-syntax introduced by the delaborator toStringWithSep "." (escape && !n.isInaccessibleUserName && !n.hasMacroScopes && !maybePseudoSyntax) n where maybePseudoSyntax := if let Name.str _ s _ := n.getRoot then -- could be pseudo-syntax for loose bvar or universe mvar, output as is "#".isPrefixOf s || "?".isPrefixOf s else false instance : ToString Name where toString n := n.toString private def hasNum : Name → Bool | anonymous => false | num .. => true | str p .. => hasNum p protected def reprPrec (n : Name) (prec : Nat) : Std.Format := match n with | anonymous => Std.Format.text "Lean.Name.anonymous" | num p i _ => Repr.addAppParen ("Lean.Name.mkNum " ++ Name.reprPrec p max_prec ++ " " ++ repr i) prec | str p s _ => if p.hasNum then Repr.addAppParen ("Lean.Name.mkStr " ++ Name.reprPrec p max_prec ++ " " ++ repr s) prec else Std.Format.text "`" ++ n.toString instance : Repr Name where reprPrec := Name.reprPrec deriving instance Repr for Syntax def capitalize : Name → Name | Name.str p s _ => Name.mkStr p s.capitalize | n => n def replacePrefix : Name → Name → Name → Name | anonymous, anonymous, newP => newP | anonymous, _, _ => anonymous | n@(str p s _), queryP, newP => if n == queryP then newP else Name.mkStr (p.replacePrefix queryP newP) s | n@(num p s _), queryP, newP => if n == queryP then newP else Name.mkNum (p.replacePrefix queryP newP) s /-- Remove macros scopes, apply `f`, and put them back -/ @[inline] def modifyBase (n : Name) (f : Name → Name) : Name := if n.hasMacroScopes then let view := extractMacroScopes n { view with name := f view.name }.review else f n @[export lean_name_append_after] def appendAfter (n : Name) (suffix : String) : Name := n.modifyBase fun | str p s _ => Name.mkStr p (s ++ suffix) | n => Name.mkStr n suffix @[export lean_name_append_index_after] def appendIndexAfter (n : Name) (idx : Nat) : Name := n.modifyBase fun | str p s _ => Name.mkStr p (s ++ "_" ++ toString idx) | n => Name.mkStr n ("_" ++ toString idx) @[export lean_name_append_before] def appendBefore (n : Name) (pre : String) : Name := n.modifyBase fun | anonymous => Name.mkStr anonymous pre | str p s _ => Name.mkStr p (pre ++ s) | num p n _ => Name.mkNum (Name.mkStr p pre) n end Name structure NameGenerator where namePrefix : Name := `_uniq idx : Nat := 1 deriving Inhabited namespace NameGenerator @[inline] def curr (g : NameGenerator) : Name := Name.mkNum g.namePrefix g.idx @[inline] def next (g : NameGenerator) : NameGenerator := { g with idx := g.idx + 1 } @[inline] def mkChild (g : NameGenerator) : NameGenerator × NameGenerator := ({ namePrefix := Name.mkNum g.namePrefix g.idx, idx := 1 }, { g with idx := g.idx + 1 }) end NameGenerator class MonadNameGenerator (m : Type → Type) where getNGen : m NameGenerator setNGen : NameGenerator → m Unit export MonadNameGenerator (getNGen setNGen) def mkFreshId {m : Type → Type} [Monad m] [MonadNameGenerator m] : m Name := do let ngen ← getNGen let r := ngen.curr setNGen ngen.next pure r instance monadNameGeneratorLift (m n : Type → Type) [MonadLift m n] [MonadNameGenerator m] : MonadNameGenerator n := { getNGen := liftM (getNGen : m _), setNGen := fun ngen => liftM (setNGen ngen : m _) } namespace Syntax partial def structEq : Syntax → Syntax → Bool | Syntax.missing, Syntax.missing => true | Syntax.node _ k args, Syntax.node _ k' args' => k == k' && args.isEqv args' structEq | Syntax.atom _ val, Syntax.atom _ val' => val == val' | Syntax.ident _ rawVal val preresolved, Syntax.ident _ rawVal' val' preresolved' => rawVal == rawVal' && val == val' && preresolved == preresolved' | _, _ => false instance : BEq Lean.Syntax := ⟨structEq⟩ partial def getTailInfo? : Syntax → Option SourceInfo | atom info _ => info | ident info .. => info | node SourceInfo.none _ args => args.findSomeRev? getTailInfo? | node info _ args => info | _ => none def getTailInfo (stx : Syntax) : SourceInfo := stx.getTailInfo?.getD SourceInfo.none def getTrailingSize (stx : Syntax) : Nat := match stx.getTailInfo? with | some (SourceInfo.original (trailing := trailing) ..) => trailing.bsize | _ => 0 @[specialize] private partial def updateLast {α} [Inhabited α] (a : Array α) (f : α → Option α) (i : Nat) : Option (Array α) := if i == 0 then none else let i := i - 1 let v := a[i] match f v with | some v => some <| a.set! i v | none => updateLast a f i partial def setTailInfoAux (info : SourceInfo) : Syntax → Option Syntax | atom _ val => some <| atom info val | ident _ rawVal val pre => some <| ident info rawVal val pre | node info k args => match updateLast args (setTailInfoAux info) args.size with | some args => some <| node info k args | none => none | stx => none def setTailInfo (stx : Syntax) (info : SourceInfo) : Syntax := match setTailInfoAux info stx with | some stx => stx | none => stx def unsetTrailing (stx : Syntax) : Syntax := match stx.getTailInfo with | SourceInfo.original lead pos trail endPos => stx.setTailInfo (SourceInfo.original lead pos "".toSubstring endPos) | _ => stx @[specialize] private partial def updateFirst {α} [Inhabited α] (a : Array α) (f : α → Option α) (i : Nat) : Option (Array α) := if h : i < a.size then let v := a.get ⟨i, h⟩; match f v with | some v => some <| a.set ⟨i, h⟩ v | none => updateFirst a f (i+1) else none partial def setHeadInfoAux (info : SourceInfo) : Syntax → Option Syntax | atom _ val => some <| atom info val | ident _ rawVal val pre => some <| ident info rawVal val pre | node i k args => match updateFirst args (setHeadInfoAux info) 0 with | some args => some <| node i k args | noxne => none | stx => none def setHeadInfo (stx : Syntax) (info : SourceInfo) : Syntax := match setHeadInfoAux info stx with | some stx => stx | none => stx def setInfo (info : SourceInfo) : Syntax → Syntax | atom _ val => atom info val | ident _ rawVal val pre => ident info rawVal val pre | node _ kind args => node info kind args | missing => missing /-- Return the first atom/identifier that has position information -/ partial def getHead? : Syntax → Option Syntax | stx@(atom info ..) => info.getPos?.map fun _ => stx | stx@(ident info ..) => info.getPos?.map fun _ => stx | node SourceInfo.none _ args => args.findSome? getHead? | stx@(node info _ _) => stx | _ => none def copyHeadTailInfoFrom (target source : Syntax) : Syntax := target.setHeadInfo source.getHeadInfo |>.setTailInfo source.getTailInfo end Syntax /-- Use the head atom/identifier of the current `ref` as the `ref` -/ @[inline] def withHeadRefOnly {m : Type → Type} [Monad m] [MonadRef m] {α} (x : m α) : m α := do match (← getRef).getHead? with | none => x | some ref => withRef ref x @[inline] def mkNode (k : SyntaxNodeKind) (args : Array Syntax) : Syntax := Syntax.node SourceInfo.none k args /- Syntax objects for a Lean module. -/ structure Module where header : Syntax commands : Array Syntax /-- Expand all macros in the given syntax -/ partial def expandMacros : Syntax → MacroM Syntax | stx@(Syntax.node info k args) => do match (← expandMacro? stx) with | some stxNew => expandMacros stxNew | none => do let args ← Macro.withIncRecDepth stx <| args.mapM expandMacros pure <| Syntax.node info k args | stx => pure stx /- Helper functions for processing Syntax programmatically -/ /-- Create an identifier copying the position from `src`. To refer to a specific constant, use `mkCIdentFrom` instead. -/ def mkIdentFrom (src : Syntax) (val : Name) : Syntax := Syntax.ident (SourceInfo.fromRef src) (toString val).toSubstring val [] def mkIdentFromRef [Monad m] [MonadRef m] (val : Name) : m Syntax := do return mkIdentFrom (← getRef) val /-- Create an identifier referring to a constant `c` copying the position from `src`. This variant of `mkIdentFrom` makes sure that the identifier cannot accidentally be captured. -/ def mkCIdentFrom (src : Syntax) (c : Name) : Syntax := -- Remark: We use the reserved macro scope to make sure there are no accidental collision with our frontend let id := addMacroScope `_internal c reservedMacroScope Syntax.ident (SourceInfo.fromRef src) (toString id).toSubstring id [(c, [])] def mkCIdentFromRef [Monad m] [MonadRef m] (c : Name) : m Syntax := do return mkCIdentFrom (← getRef) c def mkCIdent (c : Name) : Syntax := mkCIdentFrom Syntax.missing c @[export lean_mk_syntax_ident] def mkIdent (val : Name) : Syntax := Syntax.ident SourceInfo.none (toString val).toSubstring val [] @[inline] def mkNullNode (args : Array Syntax := #[]) : Syntax := mkNode nullKind args @[inline] def mkGroupNode (args : Array Syntax := #[]) : Syntax := mkNode groupKind args def mkSepArray (as : Array Syntax) (sep : Syntax) : Array Syntax := do let mut i := 0 let mut r := #[] for a in as do if i > 0 then r := r.push sep |>.push a else r := r.push a i := i + 1 return r def mkOptionalNode (arg : Option Syntax) : Syntax := match arg with | some arg => mkNullNode #[arg] | none => mkNullNode #[] def mkHole (ref : Syntax) : Syntax := mkNode `Lean.Parser.Term.hole #[mkAtomFrom ref "_"] namespace Syntax def mkSep (a : Array Syntax) (sep : Syntax) : Syntax := mkNullNode <| mkSepArray a sep def SepArray.ofElems {sep} (elems : Array Syntax) : SepArray sep := ⟨mkSepArray elems (mkAtom sep)⟩ def SepArray.ofElemsUsingRef [Monad m] [MonadRef m] {sep} (elems : Array Syntax) : m (SepArray sep) := do let ref ← getRef; return ⟨mkSepArray elems (mkAtomFrom ref sep)⟩ instance (sep) : Coe (Array Syntax) (SepArray sep) where coe := SepArray.ofElems /-- Create syntax representing a Lean term application, but avoid degenerate empty applications. -/ def mkApp (fn : Syntax) : (args : Array Syntax) → Syntax | #[] => fn | args => mkNode `Lean.Parser.Term.app #[fn, mkNullNode args] def mkCApp (fn : Name) (args : Array Syntax) : Syntax := mkApp (mkCIdent fn) args def mkLit (kind : SyntaxNodeKind) (val : String) (info := SourceInfo.none) : Syntax := let atom : Syntax := Syntax.atom info val mkNode kind #[atom] def mkStrLit (val : String) (info := SourceInfo.none) : Syntax := mkLit strLitKind (String.quote val) info def mkNumLit (val : String) (info := SourceInfo.none) : Syntax := mkLit numLitKind val info def mkScientificLit (val : String) (info := SourceInfo.none) : Syntax := mkLit scientificLitKind val info def mkNameLit (val : String) (info := SourceInfo.none) : Syntax := mkLit nameLitKind val info /- Recall that we don't have special Syntax constructors for storing numeric and string atoms. The idea is to have an extensible approach where embedded DSLs may have new kind of atoms and/or different ways of representing them. So, our atoms contain just the parsed string. The main Lean parser uses the kind `numLitKind` for storing natural numbers that can be encoded in binary, octal, decimal and hexadecimal format. `isNatLit` implements a "decoder" for Syntax objects representing these numerals. -/ private partial def decodeBinLitAux (s : String) (i : String.Pos) (val : Nat) : Option Nat := if s.atEnd i then some val else let c := s.get i if c == '0' then decodeBinLitAux s (s.next i) (2*val) else if c == '1' then decodeBinLitAux s (s.next i) (2*val + 1) else none private partial def decodeOctalLitAux (s : String) (i : String.Pos) (val : Nat) : Option Nat := if s.atEnd i then some val else let c := s.get i if '0' ≤ c && c ≤ '7' then decodeOctalLitAux s (s.next i) (8*val + c.toNat - '0'.toNat) else none private def decodeHexDigit (s : String) (i : String.Pos) : Option (Nat × String.Pos) := let c := s.get i let i := s.next i if '0' ≤ c && c ≤ '9' then some (c.toNat - '0'.toNat, i) else if 'a' ≤ c && c ≤ 'f' then some (10 + c.toNat - 'a'.toNat, i) else if 'A' ≤ c && c ≤ 'F' then some (10 + c.toNat - 'A'.toNat, i) else none private partial def decodeHexLitAux (s : String) (i : String.Pos) (val : Nat) : Option Nat := if s.atEnd i then some val else match decodeHexDigit s i with | some (d, i) => decodeHexLitAux s i (16*val + d) | none => none private partial def decodeDecimalLitAux (s : String) (i : String.Pos) (val : Nat) : Option Nat := if s.atEnd i then some val else let c := s.get i if '0' ≤ c && c ≤ '9' then decodeDecimalLitAux s (s.next i) (10*val + c.toNat - '0'.toNat) else none def decodeNatLitVal? (s : String) : Option Nat := let len := s.length if len == 0 then none else let c := s.get 0 if c == '0' then if len == 1 then some 0 else let c := s.get 1 if c == 'x' || c == 'X' then decodeHexLitAux s 2 0 else if c == 'b' || c == 'B' then decodeBinLitAux s 2 0 else if c == 'o' || c == 'O' then decodeOctalLitAux s 2 0 else if c.isDigit then decodeDecimalLitAux s 0 0 else none else if c.isDigit then decodeDecimalLitAux s 0 0 else none def isLit? (litKind : SyntaxNodeKind) (stx : Syntax) : Option String := match stx with | Syntax.node _ k args => if k == litKind && args.size == 1 then match args.get! 0 with | (Syntax.atom _ val) => some val | _ => none else none | _ => none private def isNatLitAux (litKind : SyntaxNodeKind) (stx : Syntax) : Option Nat := match isLit? litKind stx with | some val => decodeNatLitVal? val | _ => none def isNatLit? (s : Syntax) : Option Nat := isNatLitAux numLitKind s def isFieldIdx? (s : Syntax) : Option Nat := isNatLitAux fieldIdxKind s partial def decodeScientificLitVal? (s : String) : Option (Nat × Bool × Nat) := let len := s.length if len == 0 then none else let c := s.get 0 if c.isDigit then decode 0 0 else none where decodeAfterExp (i : String.Pos) (val : Nat) (e : Nat) (sign : Bool) (exp : Nat) : Option (Nat × Bool × Nat) := if s.atEnd i then if sign then some (val, sign, exp + e) else if exp >= e then some (val, sign, exp - e) else some (val, true, e - exp) else let c := s.get i if '0' ≤ c && c ≤ '9' then decodeAfterExp (s.next i) val e sign (10*exp + c.toNat - '0'.toNat) else none decodeExp (i : String.Pos) (val : Nat) (e : Nat) : Option (Nat × Bool × Nat) := let c := s.get i if c == '-' then decodeAfterExp (s.next i) val e true 0 else decodeAfterExp i val e false 0 decodeAfterDot (i : String.Pos) (val : Nat) (e : Nat) : Option (Nat × Bool × Nat) := if s.atEnd i then some (val, true, e) else let c := s.get i if '0' ≤ c && c ≤ '9' then decodeAfterDot (s.next i) (10*val + c.toNat - '0'.toNat) (e+1) else if c == 'e' || c == 'E' then decodeExp (s.next i) val e else none decode (i : String.Pos) (val : Nat) : Option (Nat × Bool × Nat) := if s.atEnd i then none else let c := s.get i if '0' ≤ c && c ≤ '9' then decode (s.next i) (10*val + c.toNat - '0'.toNat) else if c == '.' then decodeAfterDot (s.next i) val 0 else if c == 'e' || c == 'E' then decodeExp (s.next i) val 0 else none def isScientificLit? (stx : Syntax) : Option (Nat × Bool × Nat) := match isLit? scientificLitKind stx with | some val => decodeScientificLitVal? val | _ => none def isIdOrAtom? : Syntax → Option String | Syntax.atom _ val => some val | Syntax.ident _ rawVal _ _ => some rawVal.toString | _ => none def toNat (stx : Syntax) : Nat := match stx.isNatLit? with | some val => val | none => 0 def decodeQuotedChar (s : String) (i : String.Pos) : Option (Char × String.Pos) := OptionM.run do let c := s.get i let i := s.next i if c == '\\' then pure ('\\', i) else if c = '\"' then pure ('\"', i) else if c = '\'' then pure ('\'', i) else if c = 'r' then pure ('\r', i) else if c = 'n' then pure ('\n', i) else if c = 't' then pure ('\t', i) else if c = 'x' then let (d₁, i) ← decodeHexDigit s i let (d₂, i) ← decodeHexDigit s i pure (Char.ofNat (16*d₁ + d₂), i) else if c = 'u' then do let (d₁, i) ← decodeHexDigit s i let (d₂, i) ← decodeHexDigit s i let (d₃, i) ← decodeHexDigit s i let (d₄, i) ← decodeHexDigit s i pure (Char.ofNat (16*(16*(16*d₁ + d₂) + d₃) + d₄), i) else none partial def decodeStrLitAux (s : String) (i : String.Pos) (acc : String) : Option String := OptionM.run do let c := s.get i let i := s.next i if c == '\"' then pure acc else if s.atEnd i then none else if c == '\\' then do let (c, i) ← decodeQuotedChar s i decodeStrLitAux s i (acc.push c) else decodeStrLitAux s i (acc.push c) def decodeStrLit (s : String) : Option String := decodeStrLitAux s 1 "" def isStrLit? (stx : Syntax) : Option String := match isLit? strLitKind stx with | some val => decodeStrLit val | _ => none def decodeCharLit (s : String) : Option Char := OptionM.run do let c := s.get 1 if c == '\\' then do let (c, _) ← decodeQuotedChar s 2 pure c else pure c def isCharLit? (stx : Syntax) : Option Char := match isLit? charLitKind stx with | some val => decodeCharLit val | _ => none private partial def splitNameLitAux (ss : Substring) (acc : List Substring) : List Substring := let splitRest (ss : Substring) (acc : List Substring) : List Substring := if ss.front == '.' then splitNameLitAux (ss.drop 1) acc else if ss.isEmpty then acc else [] if ss.isEmpty then [] else let curr := ss.front if isIdBeginEscape curr then let escapedPart := ss.takeWhile (!isIdEndEscape ·) let escapedPart := { escapedPart with stopPos := ss.stopPos.min (escapedPart.str.next escapedPart.stopPos) } if !isIdEndEscape (escapedPart.get <| escapedPart.prev escapedPart.bsize) then [] else splitRest (ss.extract escapedPart.bsize ss.bsize) (escapedPart :: acc) else if isIdFirst curr then let idPart := ss.takeWhile isIdRest splitRest (ss.extract idPart.bsize ss.bsize) (idPart :: acc) else if curr.isDigit then let idPart := ss.takeWhile Char.isDigit splitRest (ss.extract idPart.bsize ss.bsize) (idPart :: acc) else [] /-- Split a name literal (without the backtick) into its dot-separated components. For example, `foo.bla.«bo.o»` ↦ `["foo", "bla", "«bo.o»"]`. If the literal cannot be parsed, return `[]`. -/ def splitNameLit (ss : Substring) : List Substring := splitNameLitAux ss [] |>.reverse def decodeNameLit (s : String) : Option Name := if s.get 0 == '`' then match splitNameLitAux (s.toSubstring.drop 1) [] with | [] => none | comps => some <| comps.foldr (init := Name.anonymous) fun comp n => let comp := comp.toString if isIdBeginEscape comp.front then Name.mkStr n (comp.drop 1 |>.dropRight 1) else if comp.front.isDigit then if let some k := decodeNatLitVal? comp then Name.mkNum n k else unreachable! else Name.mkStr n comp else none def isNameLit? (stx : Syntax) : Option Name := match isLit? nameLitKind stx with | some val => decodeNameLit val | _ => none def hasArgs : Syntax → Bool | Syntax.node _ _ args => args.size > 0 | _ => false def isAtom : Syntax → Bool | atom _ _ => true | _ => false def isToken (token : String) : Syntax → Bool | atom _ val => val.trim == token.trim | _ => false def isNone (stx : Syntax) : Bool := match stx with | Syntax.node _ k args => k == nullKind && args.size == 0 -- when elaborating partial syntax trees, it's reasonable to interpret missing parts as `none` | Syntax.missing => true | _ => false def getOptional? (stx : Syntax) : Option Syntax := match stx with | Syntax.node _ k args => if k == nullKind && args.size == 1 then some (args.get! 0) else none | _ => none def getOptionalIdent? (stx : Syntax) : Option Name := match stx.getOptional? with | some stx => some stx.getId | none => none partial def findAux (p : Syntax → Bool) : Syntax → Option Syntax | stx@(Syntax.node _ _ args) => if p stx then some stx else args.findSome? (findAux p) | stx => if p stx then some stx else none def find? (stx : Syntax) (p : Syntax → Bool) : Option Syntax := findAux p stx end Syntax /-- Reflect a runtime datum back to surface syntax (best-effort). -/ class Quote (α : Type) where quote : α → Syntax export Quote (quote) instance : Quote Syntax := ⟨id⟩ instance : Quote Bool := ⟨fun | true => mkCIdent `Bool.true | false => mkCIdent `Bool.false⟩ instance : Quote String := ⟨Syntax.mkStrLit⟩ instance : Quote Nat := ⟨fun n => Syntax.mkNumLit <| toString n⟩ instance : Quote Substring := ⟨fun s => Syntax.mkCApp `String.toSubstring #[quote s.toString]⟩ -- in contrast to `Name.toString`, we can, and want to be, precise here private def getEscapedNameParts? (acc : List String) : Name → OptionM (List String) | Name.anonymous => acc | Name.str n s _ => do let s ← Name.escapePart s getEscapedNameParts? (s::acc) n | Name.num n i _ => none private def quoteNameMk : Name → Syntax | Name.anonymous => mkCIdent ``Name.anonymous | Name.str n s _ => Syntax.mkCApp ``Name.mkStr #[quoteNameMk n, quote s] | Name.num n i _ => Syntax.mkCApp ``Name.mkNum #[quoteNameMk n, quote i] instance : Quote Name where quote n := match getEscapedNameParts? [] n with | some ss => mkNode `Lean.Parser.Term.quotedName #[Syntax.mkNameLit ("`" ++ ".".intercalate ss)] | none => quoteNameMk n instance {α β : Type} [Quote α] [Quote β] : Quote (α × β) where quote | ⟨a, b⟩ => Syntax.mkCApp ``Prod.mk #[quote a, quote b] private def quoteList {α : Type} [Quote α] : List α → Syntax | [] => mkCIdent ``List.nil | (x::xs) => Syntax.mkCApp ``List.cons #[quote x, quoteList xs] instance {α : Type} [Quote α] : Quote (List α) where quote := quoteList instance {α : Type} [Quote α] : Quote (Array α) where quote xs := Syntax.mkCApp ``List.toArray #[quote xs.toList] private def quoteOption {α : Type} [Quote α] : Option α → Syntax | none => mkIdent ``none | (some x) => Syntax.mkCApp ``some #[quote x] instance Option.hasQuote {α : Type} [Quote α] : Quote (Option α) where quote := quoteOption /- Evaluator for `prec` DSL -/ def evalPrec (stx : Syntax) : MacroM Nat := Macro.withIncRecDepth stx do let stx ← expandMacros stx match stx with | `(prec| $num:numLit) => return num.isNatLit?.getD 0 | _ => Macro.throwErrorAt stx "unexpected precedence" macro_rules | `(prec| $a + $b) => do `(prec| $(quote <| (← evalPrec a) + (← evalPrec b)):numLit) macro_rules | `(prec| $a - $b) => do `(prec| $(quote <| (← evalPrec a) - (← evalPrec b)):numLit) macro "eval_prec " p:prec:max : term => return quote (← evalPrec p) /- Evaluator for `prio` DSL -/ def evalPrio (stx : Syntax) : MacroM Nat := Macro.withIncRecDepth stx do let stx ← expandMacros stx match stx with | `(prio| $num:numLit) => return num.isNatLit?.getD 0 | _ => Macro.throwErrorAt stx "unexpected priority" macro_rules | `(prio| $a + $b) => do `(prio| $(quote <| (← evalPrio a) + (← evalPrio b)):numLit) macro_rules | `(prio| $a - $b) => do `(prio| $(quote <| (← evalPrio a) - (← evalPrio b)):numLit) macro "eval_prio " p:prio:max : term => return quote (← evalPrio p) def evalOptPrio : Option Syntax → MacroM Nat | some prio => evalPrio prio | none => return eval_prio default end Lean namespace Array abbrev getSepElems := @getEvenElems open Lean private partial def filterSepElemsMAux {m : Type → Type} [Monad m] (a : Array Syntax) (p : Syntax → m Bool) (i : Nat) (acc : Array Syntax) : m (Array Syntax) := do if h : i < a.size then let stx := a.get ⟨i, h⟩ if (← p stx) then if acc.isEmpty then filterSepElemsMAux a p (i+2) (acc.push stx) else if hz : i ≠ 0 then have : i.pred < i := Nat.pred_lt hz let sepStx := a.get ⟨i.pred, Nat.lt_trans this h⟩ filterSepElemsMAux a p (i+2) ((acc.push sepStx).push stx) else filterSepElemsMAux a p (i+2) (acc.push stx) else filterSepElemsMAux a p (i+2) acc else pure acc def filterSepElemsM {m : Type → Type} [Monad m] (a : Array Syntax) (p : Syntax → m Bool) : m (Array Syntax) := filterSepElemsMAux a p 0 #[] def filterSepElems (a : Array Syntax) (p : Syntax → Bool) : Array Syntax := Id.run <| a.filterSepElemsM p private partial def mapSepElemsMAux {m : Type → Type} [Monad m] (a : Array Syntax) (f : Syntax → m Syntax) (i : Nat) (acc : Array Syntax) : m (Array Syntax) := do if h : i < a.size then let stx := a.get ⟨i, h⟩ if i % 2 == 0 then do let stx ← f stx mapSepElemsMAux a f (i+1) (acc.push stx) else mapSepElemsMAux a f (i+1) (acc.push stx) else pure acc def mapSepElemsM {m : Type → Type} [Monad m] (a : Array Syntax) (f : Syntax → m Syntax) : m (Array Syntax) := mapSepElemsMAux a f 0 #[] def mapSepElems (a : Array Syntax) (f : Syntax → Syntax) : Array Syntax := Id.run <| a.mapSepElemsM f end Array namespace Lean.Syntax.SepArray def getElems {sep} (sa : SepArray sep) : Array Syntax := sa.elemsAndSeps.getSepElems /- We use `CoeTail` here instead of `Coe` to avoid a "loop" when computing `CoeTC`. The "loop" is interrupted using the maximum instance size threshold, but it is a performance bottleneck. The loop occurs because the predicate `isNewAnswer` is too imprecise. -/ instance (sep) : CoeTail (SepArray sep) (Array Syntax) where coe := getElems end Lean.Syntax.SepArray /-- Gadget for automatic parameter support. This is similar to the `optParam` gadget, but it uses the given tactic. Like `optParam`, this gadget only affects elaboration. For example, the tactic will *not* be invoked during type class resolution. -/ abbrev autoParam.{u} (α : Sort u) (tactic : Lean.Syntax) : Sort u := α /- Helper functions for manipulating interpolated strings -/ namespace Lean.Syntax private def decodeInterpStrQuotedChar (s : String) (i : String.Pos) : Option (Char × String.Pos) := OptionM.run do match decodeQuotedChar s i with | some r => some r | none => let c := s.get i let i := s.next i if c == '{' then pure ('{', i) else none private partial def decodeInterpStrLit (s : String) : Option String := let rec loop (i : String.Pos) (acc : String) : OptionM String := let c := s.get i let i := s.next i if c == '\"' || c == '{' then pure acc else if s.atEnd i then none else if c == '\\' then do let (c, i) ← decodeInterpStrQuotedChar s i loop i (acc.push c) else loop i (acc.push c) loop 1 "" partial def isInterpolatedStrLit? (stx : Syntax) : Option String := match isLit? interpolatedStrLitKind stx with | none => none | some val => decodeInterpStrLit val def expandInterpolatedStrChunks (chunks : Array Syntax) (mkAppend : Syntax → Syntax → MacroM Syntax) (mkElem : Syntax → MacroM Syntax) : MacroM Syntax := do let mut i := 0 let mut result := Syntax.missing for elem in chunks do let elem ← match elem.isInterpolatedStrLit? with | none => mkElem elem | some str => mkElem (Syntax.mkStrLit str) if i == 0 then result := elem else result ← mkAppend result elem i := i+1 return result def expandInterpolatedStr (interpStr : Syntax) (type : Syntax) (toTypeFn : Syntax) : MacroM Syntax := do let ref := interpStr let r ← expandInterpolatedStrChunks interpStr.getArgs (fun a b => `($a ++ $b)) (fun a => `($toTypeFn $a)) `(($r : $type)) def getSepArgs (stx : Syntax) : Array Syntax := stx.getArgs.getSepElems end Syntax namespace Meta inductive TransparencyMode where | all | default | reducible | instances deriving Inhabited, BEq, Repr namespace Simp def defaultMaxSteps := 100000 structure Config where maxSteps : Nat := defaultMaxSteps maxDischargeDepth : Nat := 2 contextual : Bool := false memoize : Bool := true singlePass : Bool := false zeta : Bool := true beta : Bool := true eta : Bool := true iota : Bool := true proj : Bool := true decide : Bool := true deriving Inhabited, BEq, Repr -- Configuration object for `simp_all` structure ConfigCtx extends Config where contextual := true end Simp namespace Rewrite structure Config where transparency : TransparencyMode := TransparencyMode.reducible offsetCnstrs : Bool := true end Rewrite end Meta namespace Parser.Tactic macro "erw " s:rwRuleSeq loc:(location)? : tactic => `(rw (config := { transparency := Lean.Meta.TransparencyMode.default }) $s:rwRuleSeq $[$(loc.getOptional?):location]?) end Parser.Tactic end Lean
a64cea497a9e107037325dd918f56daa9b10d866
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/algebra/tower.lean
20fd4850d1baafbe2844d33361be549bfd71d48c
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
11,159
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Anne Baanen -/ import algebra.algebra.equiv /-! # Towers of algebras In this file we prove basic facts about towers of algebra. An algebra tower A/S/R is expressed by having instances of `algebra A S`, `algebra R S`, `algebra R A` and `is_scalar_tower R S A`, the later asserting the compatibility condition `(r • s) • a = r • (s • a)`. An important definition is `to_alg_hom R S A`, the canonical `R`-algebra homomorphism `S →ₐ[R] A`. -/ open_locale pointwise universes u v w u₁ v₁ variables (R : Type u) (S : Type v) (A : Type w) (B : Type u₁) (M : Type v₁) namespace algebra variables [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_monoid M] [module R M] [module A M] [is_scalar_tower R A M] variables {A} /-- The `R`-algebra morphism `A → End (M)` corresponding to the representation of the algebra `A` on the `R`-module `M`. This is a stronger version of `distrib_mul_action.to_linear_map`, and could also have been called `algebra.to_module_End`. -/ def lsmul : A →ₐ[R] module.End R M := { to_fun := distrib_mul_action.to_linear_map R M, map_one' := linear_map.ext $ λ _, one_smul A _, map_mul' := λ a b, linear_map.ext $ smul_assoc a b, map_zero' := linear_map.ext $ λ _, zero_smul A _, map_add' := λ a b, linear_map.ext $ λ _, add_smul _ _ _, commutes' := λ r, linear_map.ext $ algebra_map_smul A r, } @[simp] lemma lsmul_coe (a : A) : (lsmul R M a : M → M) = (•) a := rfl end algebra namespace is_scalar_tower section module variables [comm_semiring R] [semiring A] [algebra R A] variables [has_smul R M] [mul_action A M] [is_scalar_tower R A M] variables {R} (A) {M} theorem algebra_map_smul (r : R) (x : M) : algebra_map R A r • x = r • x := by rw [algebra.algebra_map_eq_smul_one, smul_assoc, one_smul] end module section semiring variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra S B] variables {R S A} theorem of_algebra_map_eq [algebra R A] (h : ∀ x, algebra_map R A x = algebra_map S A (algebra_map R S x)) : is_scalar_tower R S A := ⟨λ x y z, by simp_rw [algebra.smul_def, ring_hom.map_mul, mul_assoc, h]⟩ /-- See note [partially-applied ext lemmas]. -/ theorem of_algebra_map_eq' [algebra R A] (h : algebra_map R A = (algebra_map S A).comp (algebra_map R S)) : is_scalar_tower R S A := of_algebra_map_eq $ ring_hom.ext_iff.1 h variables (R S A) variables [algebra R A] [algebra R B] variables [is_scalar_tower R S A] [is_scalar_tower R S B] theorem algebra_map_eq : algebra_map R A = (algebra_map S A).comp (algebra_map R S) := ring_hom.ext $ λ x, by simp_rw [ring_hom.comp_apply, algebra.algebra_map_eq_smul_one, smul_assoc, one_smul] theorem algebra_map_apply (x : R) : algebra_map R A x = algebra_map S A (algebra_map R S x) := by rw [algebra_map_eq R S A, ring_hom.comp_apply] @[ext] lemma algebra.ext {S : Type u} {A : Type v} [comm_semiring S] [semiring A] (h1 h2 : algebra S A) (h : ∀ (r : S) (x : A), (by haveI := h1; exact r • x) = r • x) : h1 = h2 := algebra.algebra_ext _ _ $ λ r, by simpa only [@algebra.smul_def _ _ _ _ h1, @algebra.smul_def _ _ _ _ h2, mul_one] using h r 1 /-- In a tower, the canonical map from the middle element to the top element is an algebra homomorphism over the bottom element. -/ def to_alg_hom : S →ₐ[R] A := { commutes' := λ _, (algebra_map_apply _ _ _ _).symm, .. algebra_map S A } lemma to_alg_hom_apply (y : S) : to_alg_hom R S A y = algebra_map S A y := rfl @[simp] lemma coe_to_alg_hom : ↑(to_alg_hom R S A) = algebra_map S A := ring_hom.ext $ λ _, rfl @[simp] lemma coe_to_alg_hom' : (to_alg_hom R S A : S → A) = algebra_map S A := rfl variables {R S A B} @[simp, priority 900] lemma _root_.alg_hom.map_algebra_map (f : A →ₐ[S] B) (r : R) : f (algebra_map R A r) = algebra_map R B r := by rw [algebra_map_apply R S A r, f.commutes, ← algebra_map_apply R S B] variables (R) @[simp, priority 900] lemma _root_.alg_hom.comp_algebra_map_of_tower (f : A →ₐ[S] B) : (f : A →+* B).comp (algebra_map R A) = algebra_map R B := ring_hom.ext f.map_algebra_map variables (R) {S A B} -- conflicts with is_scalar_tower.subalgebra @[priority 999] instance subsemiring (U : subsemiring S) : is_scalar_tower U S A := of_algebra_map_eq $ λ x, rfl @[nolint instance_priority] instance of_ring_hom {R A B : Type*} [comm_semiring R] [comm_semiring A] [comm_semiring B] [algebra R A] [algebra R B] (f : A →ₐ[R] B) : @is_scalar_tower R A B _ (f.to_ring_hom.to_algebra.to_has_smul) _ := by { letI := (f : A →+* B).to_algebra, exact of_algebra_map_eq (λ x, (f.commutes x).symm) } end semiring end is_scalar_tower section homs variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra S B] variables [algebra R A] [algebra R B] variables [is_scalar_tower R S A] [is_scalar_tower R S B] variables (R) {A S B} open is_scalar_tower namespace alg_hom /-- R ⟶ S induces S-Alg ⥤ R-Alg -/ def restrict_scalars (f : A →ₐ[S] B) : A →ₐ[R] B := { commutes' := λ r, by { rw [algebra_map_apply R S A, algebra_map_apply R S B], exact f.commutes (algebra_map R S r) }, .. (f : A →+* B) } lemma restrict_scalars_apply (f : A →ₐ[S] B) (x : A) : f.restrict_scalars R x = f x := rfl @[simp] lemma coe_restrict_scalars (f : A →ₐ[S] B) : (f.restrict_scalars R : A →+* B) = f := rfl @[simp] lemma coe_restrict_scalars' (f : A →ₐ[S] B) : (restrict_scalars R f : A → B) = f := rfl lemma restrict_scalars_injective : function.injective (restrict_scalars R : (A →ₐ[S] B) → (A →ₐ[R] B)) := λ f g h, alg_hom.ext (alg_hom.congr_fun h : _) end alg_hom namespace alg_equiv /-- R ⟶ S induces S-Alg ⥤ R-Alg -/ def restrict_scalars (f : A ≃ₐ[S] B) : A ≃ₐ[R] B := { commutes' := λ r, by { rw [algebra_map_apply R S A, algebra_map_apply R S B], exact f.commutes (algebra_map R S r) }, .. (f : A ≃+* B) } lemma restrict_scalars_apply (f : A ≃ₐ[S] B) (x : A) : f.restrict_scalars R x = f x := rfl @[simp] lemma coe_restrict_scalars (f : A ≃ₐ[S] B) : (f.restrict_scalars R : A ≃+* B) = f := rfl @[simp] lemma coe_restrict_scalars' (f : A ≃ₐ[S] B) : (restrict_scalars R f : A → B) = f := rfl lemma restrict_scalars_injective : function.injective (restrict_scalars R : (A ≃ₐ[S] B) → (A ≃ₐ[R] B)) := λ f g h, alg_equiv.ext (alg_equiv.congr_fun h : _) end alg_equiv end homs namespace algebra variables {R A} [comm_semiring R] [semiring A] [algebra R A] variables {M} [add_comm_monoid M] [module A M] [module R M] [is_scalar_tower R A M] lemma span_restrict_scalars_eq_span_of_surjective (h : function.surjective (algebra_map R A)) (s : set M) : (submodule.span A s).restrict_scalars R = submodule.span R s := begin refine le_antisymm (λ x hx, _) (submodule.span_subset_span _ _ _), refine submodule.span_induction hx _ _ _ _, { exact λ x hx, submodule.subset_span hx }, { exact submodule.zero_mem _ }, { exact λ x y, submodule.add_mem _ }, { intros c x hx, obtain ⟨c', rfl⟩ := h c, rw is_scalar_tower.algebra_map_smul, exact submodule.smul_mem _ _ hx }, end lemma coe_span_eq_span_of_surjective (h : function.surjective (algebra_map R A)) (s : set M) : (submodule.span A s : set M) = submodule.span R s := congr_arg coe (algebra.span_restrict_scalars_eq_span_of_surjective h s) end algebra section semiring variables {R S A} namespace submodule section module variables [semiring R] [semiring S] [add_comm_monoid A] variables [module R S] [module S A] [module R A] [is_scalar_tower R S A] open is_scalar_tower theorem smul_mem_span_smul_of_mem {s : set S} {t : set A} {k : S} (hks : k ∈ span R s) {x : A} (hx : x ∈ t) : k • x ∈ span R (s • t) := span_induction hks (λ c hc, subset_span $ set.mem_smul.2 ⟨c, x, hc, hx, rfl⟩) (by { rw zero_smul, exact zero_mem _ }) (λ c₁ c₂ ih₁ ih₂, by { rw add_smul, exact add_mem ih₁ ih₂ }) (λ b c hc, by { rw is_scalar_tower.smul_assoc, exact smul_mem _ _ hc }) variables [smul_comm_class R S A] theorem smul_mem_span_smul {s : set S} (hs : span R s = ⊤) {t : set A} {k : S} {x : A} (hx : x ∈ span R t) : k • x ∈ span R (s • t) := span_induction hx (λ x hx, smul_mem_span_smul_of_mem (hs.symm ▸ mem_top) hx) (by { rw smul_zero, exact zero_mem _ }) (λ x y ihx ihy, by { rw smul_add, exact add_mem ihx ihy }) (λ c x hx, smul_comm c k x ▸ smul_mem _ _ hx) theorem smul_mem_span_smul' {s : set S} (hs : span R s = ⊤) {t : set A} {k : S} {x : A} (hx : x ∈ span R (s • t)) : k • x ∈ span R (s • t) := span_induction hx (λ x hx, let ⟨p, q, hp, hq, hpq⟩ := set.mem_smul.1 hx in by { rw [← hpq, smul_smul], exact smul_mem_span_smul_of_mem (hs.symm ▸ mem_top) hq }) (by { rw smul_zero, exact zero_mem _ }) (λ x y ihx ihy, by { rw smul_add, exact add_mem ihx ihy }) (λ c x hx, smul_comm c k x ▸ smul_mem _ _ hx) theorem span_smul_of_span_eq_top {s : set S} (hs : span R s = ⊤) (t : set A) : span R (s • t) = (span S t).restrict_scalars R := le_antisymm (span_le.2 $ λ x hx, let ⟨p, q, hps, hqt, hpqx⟩ := set.mem_smul.1 hx in hpqx ▸ (span S t).smul_mem p (subset_span hqt)) $ λ p hp, span_induction hp (λ x hx, one_smul S x ▸ smul_mem_span_smul hs (subset_span hx)) (zero_mem _) (λ _ _, add_mem) (λ k x hx, smul_mem_span_smul' hs hx) end module section algebra variables [comm_semiring R] [semiring S] [add_comm_monoid A] variables [algebra R S] [module S A] [module R A] [is_scalar_tower R S A] /-- A variant of `submodule.span_image` for `algebra_map`. -/ lemma span_algebra_map_image (a : set R) : submodule.span R (algebra_map R S '' a) = (submodule.span R a).map (algebra.linear_map R S) := (submodule.span_image $ algebra.linear_map R S).trans rfl lemma span_algebra_map_image_of_tower {S T : Type*} [comm_semiring S] [semiring T] [module R S] [is_scalar_tower R S S] [algebra R T] [algebra S T] [is_scalar_tower R S T] (a : set S) : submodule.span R (algebra_map S T '' a) = (submodule.span R a).map ((algebra.linear_map S T).restrict_scalars R) := (submodule.span_image $ (algebra.linear_map S T).restrict_scalars R).trans rfl lemma map_mem_span_algebra_map_image {S T : Type*} [comm_semiring S] [semiring T] [algebra R S] [algebra R T] [algebra S T] [is_scalar_tower R S T] (x : S) (a : set S) (hx : x ∈ submodule.span R a) : algebra_map S T x ∈ submodule.span R (algebra_map S T '' a) := by { rw [span_algebra_map_image_of_tower, mem_map], exact ⟨x, hx, rfl⟩ } end algebra end submodule end semiring section ring namespace algebra variables [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_group M] [module A M] [module R M] [is_scalar_tower R A M] lemma lsmul_injective [no_zero_smul_divisors A M] {x : A} (hx : x ≠ 0) : function.injective (lsmul R M x) := smul_right_injective _ hx end algebra end ring
a7e0d5172584e5df6bb553c3f1e6e4ff7ea46960
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/stage0/src/Init/WF.lean
c3ccde21e3c89139ccc400a6d7bcda7f1985d095
[ "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
11,267
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import Init.SizeOf import Init.Data.Nat.Basic universes u v set_option codegen false inductive Acc {α : Sort u} (r : α → α → Prop) : α → Prop where | intro (x : α) (h : (y : α) → r y x → Acc r y) : Acc r x abbrev Acc.ndrec.{u1, u2} {α : Sort u2} {r : α → α → Prop} {C : α → Sort u1} (m : (x : α) → ((y : α) → r y x → Acc r y) → ((y : α) → (a : r y x) → C y) → C x) {a : α} (n : Acc r a) : C a := Acc.rec (motive := fun α _ => C α) m n abbrev Acc.ndrecOn.{u1, u2} {α : Sort u2} {r : α → α → Prop} {C : α → Sort u1} {a : α} (n : Acc r a) (m : (x : α) → ((y : α) → r y x → Acc r y) → ((y : α) → (a : r y x) → C y) → C x) : C a := Acc.rec (motive := fun α _ => C α) m n namespace Acc variables {α : Sort u} {r : α → α → Prop} def inv {x y : α} (h₁ : Acc r x) (h₂ : r y x) : Acc r y := Acc.recOn (motive := fun (x : α) _ => r y x → Acc r y) h₁ (fun x₁ ac₁ ih h₂ => ac₁ y h₂) h₂ end Acc inductive WellFounded {α : Sort u} (r : α → α → Prop) : Prop where | intro (h : ∀ a, Acc r a) : WellFounded r class WellFoundedRelation (α : Sort u) : Type u where r : α → α → Prop wf : WellFounded r namespace WellFounded def apply {α : Sort u} {r : α → α → Prop} (wf : WellFounded r) (a : α) : Acc r a := WellFounded.recOn (motive := fun x => (y : α) → Acc r y) wf (fun p => p) a section variables {α : Sort u} {r : α → α → Prop} (hwf : WellFounded r) theorem recursion {C : α → Sort v} (a : α) (h : ∀ x, (∀ y, r y x → C y) → C x) : C a := by induction (apply hwf a) with | intro x₁ ac₁ ih => exact h x₁ ih theorem induction {C : α → Prop} (a : α) (h : ∀ x, (∀ y, r y x → C y) → C x) : C a := recursion hwf a h variable {C : α → Sort v} variable (F : ∀ x, (∀ y, r y x → C y) → C x) def fixF (x : α) (a : Acc r x) : C x := by induction a with | intro x₁ ac₁ ih => exact F x₁ ih def fixFEq (x : α) (acx : Acc r x) : fixF F x acx = F x (fun (y : α) (p : r y x) => fixF F y (Acc.inv acx p)) := by induction acx with | intro x r ih => exact rfl end variables {α : Sort u} {C : α → Sort v} {r : α → α → Prop} -- Well-founded fixpoint def fix (hwf : WellFounded r) (F : ∀ x, (∀ y, r y x → C y) → C x) (x : α) : C x := fixF F x (apply hwf x) -- Well-founded fixpoint satisfies fixpoint equation theorem fixEq (hwf : WellFounded r) (F : ∀ x, (∀ y, r y x → C y) → C x) (x : α) : fix hwf F x = F x (fun y h => fix hwf F y) := fixFEq F x (apply hwf x) end WellFounded open WellFounded -- Empty relation is well-founded def emptyWf {α : Sort u} : WellFounded (@emptyRelation α) := by apply WellFounded.intro intro a apply Acc.intro a intro b h cases h -- Subrelation of a well-founded relation is well-founded namespace Subrelation variables {α : Sort u} {r q : α → α → Prop} def accessible {a : α} (h₁ : Subrelation q r) (ac : Acc r a) : Acc q a := by induction ac with | intro x ax ih => apply Acc.intro intro y h exact ih y (h₁ h) def wf (h₁ : Subrelation q r) (h₂ : WellFounded r) : WellFounded q := ⟨fun a => accessible @h₁ (apply h₂ a)⟩ end Subrelation -- The inverse image of a well-founded relation is well-founded namespace InvImage variables {α : Sort u} {β : Sort v} {r : β → β → Prop} private def accAux (f : α → β) {b : β} (ac : Acc r b) : (x : α) → f x = b → Acc (InvImage r f) x := by induction ac with | intro x acx ih => intro z e apply Acc.intro intro y lt subst x apply ih (f y) lt y rfl def accessible {a : α} (f : α → β) (ac : Acc r (f a)) : Acc (InvImage r f) a := accAux f ac a rfl def wf (f : α → β) (h : WellFounded r) : WellFounded (InvImage r f) := ⟨fun a => accessible f (apply h (f a))⟩ end InvImage -- The transitive closure of a well-founded relation is well-founded namespace TC variables {α : Sort u} {r : α → α → Prop} def accessible {z : α} (ac : Acc r z) : Acc (TC r) z := by induction ac with | intro x acx ih => apply Acc.intro x intro y rel induction rel generalizing acx ih with | base a b rab => exact ih a rab | trans a b c rab rbc ih₁ ih₂ => apply Acc.inv (ih₂ acx ih) rab def wf (h : WellFounded r) : WellFounded (TC r) := ⟨fun a => accessible (apply h a)⟩ end TC -- less-than is well-founded def Nat.ltWf : WellFounded Nat.lt := by apply WellFounded.intro intro n induction n with | zero => apply Acc.intro 0 intro _ h apply absurd h (Nat.notLtZero _) | succ n ih => apply Acc.intro (Nat.succ n) intro m h have m = n ∨ m < n from Nat.eqOrLtOfLe (Nat.leOfSuccLeSucc h) match this with | Or.inl e => subst e; assumption | Or.inr e => exact Acc.inv ih e def measure {α : Sort u} : (α → Nat) → α → α → Prop := InvImage (fun a b => a < b) def measureWf {α : Sort u} (f : α → Nat) : WellFounded (measure f) := InvImage.wf f Nat.ltWf def sizeofMeasure (α : Sort u) [SizeOf α] : α → α → Prop := measure sizeOf def sizeofMeasureWf (α : Sort u) [SizeOf α] : WellFounded (sizeofMeasure α) := measureWf sizeOf instance hasWellFoundedOfSizeOf (α : Sort u) [SizeOf α] : WellFoundedRelation α where r := sizeofMeasure α wf := sizeofMeasureWf α namespace Prod open WellFounded section variables {α : Type u} {β : Type v} variable (ra : α → α → Prop) variable (rb : β → β → Prop) -- Lexicographical order based on ra and rb inductive Lex : α × β → α × β → Prop where | left {a₁} (b₁) {a₂} (b₂) (h : ra a₁ a₂) : Lex (a₁, b₁) (a₂, b₂) | right (a) {b₁ b₂} (h : rb b₁ b₂) : Lex (a, b₁) (a, b₂) -- relational product based on ra and rb inductive Rprod : α × β → α × β → Prop where | intro {a₁ b₁ a₂ b₂} (h₁ : ra a₁ a₂) (h₂ : rb b₁ b₂) : Rprod (a₁, b₁) (a₂, b₂) end section variables {α : Type u} {β : Type v} variables {ra : α → α → Prop} {rb : β → β → Prop} def lexAccessible (aca : (a : α) → Acc ra a) (acb : (b : β) → Acc rb b) (a : α) (b : β) : Acc (Lex ra rb) (a, b) := by induction (aca a) generalizing b with | intro xa aca iha => induction (acb b) with | intro xb acb ihb => apply Acc.intro (xa, xb) intro p lt cases lt with | left a₁ b₁ a₂ b₂ h => apply iha a₁ h | right a b₁ b₂ h => apply ihb b₁ h -- The lexicographical order of well founded relations is well-founded def lexWf (ha : WellFounded ra) (hb : WellFounded rb) : WellFounded (Lex ra rb) := ⟨fun (a, b) => lexAccessible (WellFounded.apply ha) (WellFounded.apply hb) a b⟩ -- relational product is a Subrelation of the Lex def rprodSubLex (a : α × β) (b : α × β) (h : Rprod ra rb a b) : Lex ra rb a b := by cases h with | intro a₁ b₁ a₂ b₂ h₁ h₂ => exact Lex.left b₁ b₂ h₁ -- The relational product of well founded relations is well-founded def rprodWf (ha : WellFounded ra) (hb : WellFounded rb) : WellFounded (Rprod ra rb) := by apply Subrelation.wf (r := Lex ra rb) (h₂ := lexWf ha hb) intro a b h exact rprodSubLex a b h end instance {α : Type u} {β : Type v} [s₁ : WellFoundedRelation α] [s₂ : WellFoundedRelation β] : WellFoundedRelation (α × β) where r := Lex s₁.r s₂.r wf := lexWf s₁.wf s₂.wf end Prod namespace PSigma section variables {α : Sort u} {β : α → Sort v} variable (r : α → α → Prop) variable (s : ∀ a, β a → β a → Prop) -- Lexicographical order based on r and s inductive Lex : PSigma β → PSigma β → Prop where | left : ∀ {a₁ : α} (b₁ : β a₁) {a₂ : α} (b₂ : β a₂), r a₁ a₂ → Lex ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ | right : ∀ (a : α) {b₁ b₂ : β a}, s a b₁ b₂ → Lex ⟨a, b₁⟩ ⟨a, b₂⟩ end section variables {α : Sort u} {β : α → Sort v} variables {r : α → α → Prop} {s : ∀ (a : α), β a → β a → Prop} def lexAccessible {a} (aca : Acc r a) (acb : (a : α) → WellFounded (s a)) (b : β a) : Acc (Lex r s) ⟨a, b⟩ := by induction aca generalizing b with | intro xa aca iha => induction (WellFounded.apply (acb xa) b) with | intro xb acb ihb => apply Acc.intro intro p lt cases lt with | left => apply iha; assumption | right => apply ihb; assumption -- The lexicographical order of well founded relations is well-founded def lexWf (ha : WellFounded r) (hb : (x : α) → WellFounded (s x)) : WellFounded (Lex r s) := WellFounded.intro fun ⟨a, b⟩ => lexAccessible (WellFounded.apply ha a) hb b end section variables {α : Sort u} {β : Sort v} def lexNdep (r : α → α → Prop) (s : β → β → Prop) := Lex r (fun a => s) def lexNdepWf {r : α → α → Prop} {s : β → β → Prop} (ha : WellFounded r) (hb : WellFounded s) : WellFounded (lexNdep r s) := WellFounded.intro fun ⟨a, b⟩ => lexAccessible (WellFounded.apply ha a) (fun x => hb) b end section variables {α : Sort u} {β : Sort v} -- Reverse lexicographical order based on r and s inductive RevLex (r : α → α → Prop) (s : β → β → Prop) : @PSigma α (fun a => β) → @PSigma α (fun a => β) → Prop where | left : {a₁ a₂ : α} → (b : β) → r a₁ a₂ → RevLex r s ⟨a₁, b⟩ ⟨a₂, b⟩ | right : (a₁ : α) → {b₁ : β} → (a₂ : α) → {b₂ : β} → s b₁ b₂ → RevLex r s ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ end section open WellFounded variables {α : Sort u} {β : Sort v} variables {r : α → α → Prop} {s : β → β → Prop} def revLexAccessible {b} (acb : Acc s b) (aca : (a : α) → Acc r a): (a : α) → Acc (RevLex r s) ⟨a, b⟩ := by induction acb with | intro xb acb ihb => intro a induction (aca a) with | intro xa aca iha => apply Acc.intro intro p lt cases lt with | left => apply iha; assumption | right => apply ihb; assumption def revLexWf (ha : WellFounded r) (hb : WellFounded s) : WellFounded (RevLex r s) := WellFounded.intro fun ⟨a, b⟩ => revLexAccessible (apply hb b) (WellFounded.apply ha) a end section def skipLeft (α : Type u) {β : Type v} (s : β → β → Prop) : @PSigma α (fun a => β) → @PSigma α (fun a => β) → Prop := RevLex emptyRelation s def skipLeftWf (α : Type u) {β : Type v} {s : β → β → Prop} (hb : WellFounded s) : WellFounded (skipLeft α s) := revLexWf emptyWf hb def mkSkipLeft {α : Type u} {β : Type v} {b₁ b₂ : β} {s : β → β → Prop} (a₁ a₂ : α) (h : s b₁ b₂) : skipLeft α s ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ := RevLex.right _ _ h end instance WellFoundedRelation {α : Type u} {β : α → Type v} [s₁ : WellFoundedRelation α] [s₂ : ∀ a, WellFoundedRelation (β a)] : WellFoundedRelation (PSigma β) where r := Lex s₁.r (fun a => (s₂ a).r) wf := lexWf s₁.wf (fun a => (s₂ a).wf) end PSigma
e8f36f637611931cac8ca54354a26fd993d6bc12
491068d2ad28831e7dade8d6dff871c3e49d9431
/hott/algebra/ring.hlean
ee693069e504a2bdec6b9ee604846a66d9fd4154
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,300
hlean
/- 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 Structures with multiplicative and additive components, including semirings, rings, and fields. The development is modeled after Isabelle's library. Ported from the standard library -/ import algebra.group open core namespace algebra variable {A : Type} /- auxiliary classes -/ structure distrib [class] (A : Type) extends has_mul A, has_add A := (left_distrib : Πa b c, mul a (add b c) = add (mul a b) (mul a c)) (right_distrib : Πa b c, mul (add a b) c = add (mul a c) (mul b c)) definition left_distrib [s : distrib A] (a b c : A) : a * (b + c) = a * b + a * c := !distrib.left_distrib definition right_distrib [s: distrib A] (a b c : A) : (a + b) * c = a * c + b * c := !distrib.right_distrib structure mul_zero_class [class] (A : Type) extends has_mul A, has_zero A := (zero_mul : Πa, mul zero a = zero) (mul_zero : Πa, mul a zero = zero) definition zero_mul [s : mul_zero_class A] (a : A) : 0 * a = 0 := !mul_zero_class.zero_mul definition mul_zero [s : mul_zero_class A] (a : A) : a * 0 = 0 := !mul_zero_class.mul_zero structure zero_ne_one_class [class] (A : Type) extends has_zero A, has_one A := (zero_ne_one : zero ≠ one) definition zero_ne_one [s: zero_ne_one_class A] : 0 ≠ 1 := @zero_ne_one_class.zero_ne_one A s /- semiring -/ structure semiring [class] (A : Type) extends add_comm_monoid A, monoid A, distrib A, mul_zero_class A section semiring variables [s : semiring A] (a b c : A) include s definition ne_zero_of_mul_ne_zero_right {a b : A} (H : a * b ≠ 0) : a ≠ 0 := assume H1 : a = 0, have H2 : a * b = 0, from H1⁻¹ ▸ zero_mul b, H H2 definition ne_zero_of_mul_ne_zero_left {a b : A} (H : a * b ≠ 0) : b ≠ 0 := assume H1 : b = 0, have H2 : a * b = 0, from H1⁻¹ ▸ mul_zero a, H H2 end semiring /- comm semiring -/ structure comm_semiring [class] (A : Type) extends semiring A, comm_semigroup A -- TODO: we could also define a cancelative comm_semiring, i.e. satisfying -- c ≠ 0 → c * a = c * b → a = b. section comm_semiring variables [s : comm_semiring A] (a b c : A) include s definition dvd (a b : A) : Type := Σc, b = a * c notation a ∣ b := dvd a b definition dvd.intro {a b c : A} (H : a * c = b) : a ∣ b := sigma.mk _ H⁻¹ definition dvd.intro_left {a b c : A} (H : c * a = b) : a ∣ b := dvd.intro (!mul.comm ▸ H) definition exists_eq_mul_right_of_dvd {a b : A} (H : a ∣ b) : Σc, b = a * c := H definition dvd.elim {P : Type} {a b : A} (H₁ : a ∣ b) (H₂ : Πc, b = a * c → P) : P := sigma.rec_on H₁ H₂ definition exists_eq_mul_left_of_dvd {a b : A} (H : a ∣ b) : Σc, b = c * a := dvd.elim H (take c, assume H1 : b = a * c, sigma.mk c (H1 ⬝ !mul.comm)) definition dvd.elim_left {P : Type} {a b : A} (H₁ : a ∣ b) (H₂ : Πc, b = c * a → P) : P := sigma.rec_on (exists_eq_mul_left_of_dvd H₁) (take c, assume H₃ : b = c * a, H₂ c H₃) definition dvd.refl : a ∣ a := dvd.intro !mul_one definition dvd.trans {a b c : A} (H₁ : a ∣ b) (H₂ : b ∣ c) : a ∣ c := dvd.elim H₁ (take d, assume H₃ : b = a * d, dvd.elim H₂ (take e, assume H₄ : c = b * e, dvd.intro (show a * (d * e) = c, by rewrite [-mul.assoc, -H₃, H₄]))) definition eq_zero_of_zero_dvd {a : A} (H : 0 ∣ a) : a = 0 := dvd.elim H (take c, assume H' : a = 0 * c, H' ⬝ !zero_mul) definition dvd_zero : a ∣ 0 := dvd.intro !mul_zero definition one_dvd : 1 ∣ a := dvd.intro !one_mul definition dvd_mul_right : a ∣ a * b := dvd.intro rfl definition dvd_mul_left : a ∣ b * a := mul.comm a b ▸ dvd_mul_right a b definition dvd_mul_of_dvd_left {a b : A} (H : a ∣ b) (c : A) : a ∣ b * c := dvd.elim H (take d, assume H₁ : b = a * d, dvd.intro (show a * (d * c) = b * c, from by rewrite [-mul.assoc, H₁])) definition dvd_mul_of_dvd_right {a b : A} (H : a ∣ b) (c : A) : a ∣ c * b := !mul.comm ▸ (dvd_mul_of_dvd_left H _) definition mul_dvd_mul {a b c d : A} (dvd_ab : (a ∣ b)) (dvd_cd : c ∣ d) : a * c ∣ b * d := dvd.elim dvd_ab (take e, assume Haeb : b = a * e, dvd.elim dvd_cd (take f, assume Hcfd : d = c * f, dvd.intro (show a * c * (e * f) = b * d, by rewrite [mul.assoc, {c*_}mul.left_comm, -mul.assoc, Haeb, Hcfd]))) definition dvd_of_mul_right_dvd {a b c : A} (H : a * b ∣ c) : a ∣ c := dvd.elim H (take d, assume Habdc : c = a * b * d, dvd.intro (!mul.assoc⁻¹ ⬝ Habdc⁻¹)) definition dvd_of_mul_left_dvd {a b c : A} (H : a * b ∣ c) : b ∣ c := dvd_of_mul_right_dvd (mul.comm a b ▸ H) definition dvd_add {a b c : A} (Hab : a ∣ b) (Hac : a ∣ c) : a ∣ b + c := dvd.elim Hab (take d, assume Hadb : b = a * d, dvd.elim Hac (take e, assume Haec : c = a * e, dvd.intro (show a * (d + e) = b + c, by rewrite [left_distrib, -Hadb, -Haec]))) end comm_semiring /- ring -/ structure ring [class] (A : Type) extends add_comm_group A, monoid A, distrib A definition ring.mul_zero [s : ring A] (a : A) : a * 0 = 0 := have H : a * 0 + 0 = a * 0 + a * 0, from calc a * 0 + 0 = a * 0 : by rewrite add_zero ... = a * (0 + 0) : by rewrite add_zero ... = a * 0 + a * 0 : by rewrite {a*_}ring.left_distrib, show a * 0 = 0, from (add.left_cancel H)⁻¹ definition ring.zero_mul [s : ring A] (a : A) : 0 * a = 0 := have H : 0 * a + 0 = 0 * a + 0 * a, from calc 0 * a + 0 = 0 * a : by rewrite add_zero ... = (0 + 0) * a : by rewrite add_zero ... = 0 * a + 0 * a : by rewrite {_*a}ring.right_distrib, show 0 * a = 0, from (add.left_cancel H)⁻¹ definition ring.to_semiring [instance] [coercion] [reducible] [s : ring A] : semiring A := ⦃ semiring, s, mul_zero := ring.mul_zero, zero_mul := ring.zero_mul ⦄ section variables [s : ring A] (a b c d e : A) include s definition neg_mul_eq_neg_mul : -(a * b) = -a * b := neg_eq_of_add_eq_zero begin rewrite [-right_distrib, add.right_inv, zero_mul] end definition neg_mul_eq_mul_neg : -(a * b) = a * -b := neg_eq_of_add_eq_zero begin rewrite [-left_distrib, add.right_inv, mul_zero] end definition neg_mul_neg : -a * -b = a * b := calc -a * -b = -(a * -b) : by rewrite -neg_mul_eq_neg_mul ... = - -(a * b) : by rewrite -neg_mul_eq_mul_neg ... = a * b : by rewrite neg_neg definition neg_mul_comm : -a * b = a * -b := !neg_mul_eq_neg_mul⁻¹ ⬝ !neg_mul_eq_mul_neg definition neg_eq_neg_one_mul : -a = -1 * a := calc -a = -(1 * a) : by rewrite one_mul ... = -1 * a : by rewrite neg_mul_eq_neg_mul definition mul_sub_left_distrib : a * (b - c) = a * b - a * c := calc a * (b - c) = a * b + a * -c : left_distrib ... = a * b + - (a * c) : by rewrite -neg_mul_eq_mul_neg ... = a * b - a * c : rfl definition mul_sub_right_distrib : (a - b) * c = a * c - b * c := calc (a - b) * c = a * c + -b * c : right_distrib ... = a * c + - (b * c) : by rewrite neg_mul_eq_neg_mul ... = a * c - b * c : rfl -- TODO: can calc mode be improved to make this easier? -- TODO: there is also the other direction. It will be easier when we -- have the simplifier. definition mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d := calc a * e + c = b * e + d ↔ a * e + c = d + b * e : by rewrite {b*e+_}add.comm ... ↔ a * e + c - b * e = d : iff.symm !sub_eq_iff_eq_add ... ↔ a * e - b * e + c = d : by rewrite sub_add_eq_add_sub ... ↔ (a - b) * e + c = d : by rewrite mul_sub_right_distrib definition mul_neg_one_eq_neg : a * (-1) = -a := have H : a + a * -1 = 0, from calc a + a * -1 = a * 1 + a * -1 : mul_one ... = a * (1 + -1) : left_distrib ... = a * 0 : add.right_inv ... = 0 : mul_zero, inverse (neg_eq_of_add_eq_zero H) definition ne_zero_and_ne_zero_of_mul_ne_zero {a b : A} (H : a * b ≠ 0) : a ≠ 0 × b ≠ 0 := have Ha : a ≠ 0, from (assume Ha1 : a = 0, have H1 : a * b = 0, by rewrite [Ha1, zero_mul], absurd H1 H), have Hb : b ≠ 0, from (assume Hb1 : b = 0, have H1 : a * b = 0, by rewrite [Hb1, mul_zero], absurd H1 H), pair Ha Hb end structure comm_ring [class] (A : Type) extends ring A, comm_semigroup A definition comm_ring.to_comm_semiring [instance] [coercion] [reducible] [s : comm_ring A] : comm_semiring A := ⦃ comm_semiring, s, mul_zero := mul_zero, zero_mul := zero_mul ⦄ section variables [s : comm_ring A] (a b c d e : A) include s definition mul_self_sub_mul_self_eq : a * a - b * b = (a + b) * (a - b) := by rewrite [left_distrib, *right_distrib, add.assoc, -{b*a + _}add.assoc, -*neg_mul_eq_mul_neg, {a*b}mul.comm, add.right_inv, zero_add] definition mul_self_sub_one_eq : a * a - 1 = (a + 1) * (a - 1) := mul_one 1 ▸ mul_self_sub_mul_self_eq a 1 definition dvd_neg_iff_dvd : (a ∣ -b) ↔ (a ∣ b) := iff.intro (assume H : (a ∣ -b), dvd.elim H (take c, assume H' : -b = a * c, dvd.intro (show a * -c = b, by rewrite [-neg_mul_eq_mul_neg, -H', neg_neg]))) (assume H : (a ∣ b), dvd.elim H (take c, assume H' : b = a * c, dvd.intro (show a * -c = -b, by rewrite [-neg_mul_eq_mul_neg, -H']))) definition neg_dvd_iff_dvd : (-a ∣ b) ↔ (a ∣ b) := iff.intro (assume H : (-a ∣ b), dvd.elim H (take c, assume H' : b = -a * c, dvd.intro (show a * -c = b, by rewrite [-neg_mul_comm, H']))) (assume H : (a ∣ b), dvd.elim H (take c, assume H' : b = a * c, dvd.intro (show -a * -c = b, by rewrite [neg_mul_neg, H']))) definition dvd_sub (H₁ : (a ∣ b)) (H₂ : (a ∣ c)) : (a ∣ b - c) := dvd_add H₁ (iff.elim_right !dvd_neg_iff_dvd H₂) end /- integral domains -/ structure no_zero_divisors [class] (A : Type) extends has_mul A, has_zero A := (eq_zero_or_eq_zero_of_mul_eq_zero : Πa b, mul a b = zero → a = zero ⊎ b = zero) definition eq_zero_or_eq_zero_of_mul_eq_zero {A : Type} [s : no_zero_divisors A] {a b : A} (H : a * b = 0) : a = 0 ⊎ b = 0 := !no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero H structure integral_domain [class] (A : Type) extends comm_ring A, no_zero_divisors A section variables [s : integral_domain A] (a b c d e : A) include s definition mul_ne_zero {a b : A} (H1 : a ≠ 0) (H2 : b ≠ 0) : a * b ≠ 0 := assume H : a * b = 0, sum.rec_on (eq_zero_or_eq_zero_of_mul_eq_zero H) (assume H3, H1 H3) (assume H4, H2 H4) definition eq_of_mul_eq_mul_right {a b c : A} (Ha : a ≠ 0) (H : b * a = c * a) : b = c := have H1 : b * a - c * a = 0, from iff.mp !eq_iff_sub_eq_zero H, have H2 : (b - c) * a = 0, using H1, by rewrite [mul_sub_right_distrib, H1], have H3 : b - c = 0, from sum_resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero H2) Ha, iff.elim_right !eq_iff_sub_eq_zero H3 definition eq_of_mul_eq_mul_left {a b c : A} (Ha : a ≠ 0) (H : a * b = a * c) : b = c := have H1 : a * b - a * c = 0, from iff.mp !eq_iff_sub_eq_zero H, have H2 : a * (b - c) = 0, using H1, by rewrite [mul_sub_left_distrib, H1], have H3 : b - c = 0, from sum_resolve_right (eq_zero_or_eq_zero_of_mul_eq_zero H2) Ha, iff.elim_right !eq_iff_sub_eq_zero H3 -- TODO: do we want the iff versions? definition mul_self_eq_mul_self_iff (a b : A) : a * a = b * b ↔ a = b ⊎ a = -b := iff.intro (λ H : a * a = b * b, have aux₁ : (a - b) * (a + b) = 0, by rewrite [mul.comm, -mul_self_sub_mul_self_eq, H, sub_self], assert aux₂ : a - b = 0 ⊎ a + b = 0, from !eq_zero_or_eq_zero_of_mul_eq_zero aux₁, sum.rec_on aux₂ (λ H : a - b = 0, sum.inl (eq_of_sub_eq_zero H)) (λ H : a + b = 0, sum.inr (eq_neg_of_add_eq_zero H))) (λ H : a = b ⊎ a = -b, sum.rec_on H (λ a_eq_b, by rewrite a_eq_b) (λ a_eq_mb, by rewrite [a_eq_mb, neg_mul_neg])) definition mul_self_eq_one_iff (a : A) : a * a = 1 ↔ a = 1 ⊎ a = -1 := assert aux : a * a = 1 * 1 ↔ a = 1 ⊎ a = -1, from mul_self_eq_mul_self_iff a 1, by rewrite mul_one at aux; exact aux -- TODO: c - b * c → c = 0 ⊎ b = 1 and variants definition dvd_of_mul_dvd_mul_left {a b c : A} (Ha : a ≠ 0) (Hdvd : (a * b ∣ a * c)) : (b ∣ c) := dvd.elim Hdvd (take d, assume H : a * c = a * b * d, have H1 : b * d = c, from eq_of_mul_eq_mul_left Ha (mul.assoc a b d ▸ H⁻¹), dvd.intro H1) definition dvd_of_mul_dvd_mul_right {a b c : A} (Ha : a ≠ 0) (Hdvd : (b * a ∣ c * a)) : (b ∣ c) := dvd.elim Hdvd (take d, assume H : c * a = b * a * d, have H1 : b * d * a = c * a, from by rewrite [mul.right_comm, -H], have H2 : b * d = c, from eq_of_mul_eq_mul_right Ha H1, dvd.intro H2) end end algebra
26cbe67510536f6a4b725bc0eb49566a1ae7b66c
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/src/Init/Control/State.lean
51a7060de0d007a539952dd4da33a69eeffb381e
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
3,475
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, Sebastian Ullrich The State monad transformer. -/ prelude import Init.Control.Basic import Init.Control.Id import Init.Control.Except universes u v w def StateT (σ : Type u) (m : Type u → Type v) (α : Type u) : Type (max u v) := σ → m (α × σ) @[inline] def StateT.run {σ : Type u} {m : Type u → Type v} {α : Type u} (x : StateT σ m α) (s : σ) : m (α × σ) := x s @[inline] def StateT.run' {σ : Type u} {m : Type u → Type v} [Functor m] {α : Type u} (x : StateT σ m α) (s : σ) : m α := (·.1) <$> x s @[reducible] def StateM (σ α : Type u) : Type u := StateT σ Id α instance {σ α} [Subsingleton σ] [Subsingleton α] : Subsingleton (StateM σ α) where allEq x y := by apply funext intro s match x s, y s with | (a₁, s₁), (a₂, s₂) => rw [Subsingleton.elim a₁ a₂, Subsingleton.elim s₁ s₂] namespace StateT section variable {σ : Type u} {m : Type u → Type v} variable [Monad m] {α β : Type u} @[inline] protected def pure (a : α) : StateT σ m α := fun s => pure (a, s) @[inline] protected def bind (x : StateT σ m α) (f : α → StateT σ m β) : StateT σ m β := fun s => do let (a, s) ← x s; f a s @[inline] protected def map (f : α → β) (x : StateT σ m α) : StateT σ m β := fun s => do let (a, s) ← x s; pure (f a, s) instance : Monad (StateT σ m) where pure := StateT.pure bind := StateT.bind map := StateT.map @[inline] protected def orElse [Alternative m] {α : Type u} (x₁ x₂ : StateT σ m α) : StateT σ m α := fun s => x₁ s <|> x₂ s @[inline] protected def failure [Alternative m] {α : Type u} : StateT σ m α := fun s => failure instance [Alternative m] : Alternative (StateT σ m) where failure := StateT.failure orElse := StateT.orElse @[inline] protected def get : StateT σ m σ := fun s => pure (s, s) @[inline] protected def set : σ → StateT σ m PUnit := fun s' s => pure (⟨⟩, s') @[inline] protected def modifyGet (f : σ → α × σ) : StateT σ m α := fun s => pure (f s) @[inline] protected def lift {α : Type u} (t : m α) : StateT σ m α := fun s => do let a ← t; pure (a, s) instance : MonadLift m (StateT σ m) := ⟨StateT.lift⟩ instance (σ m) [Monad m] : MonadFunctor m (StateT σ m) := ⟨fun f x s => f (x s)⟩ instance (ε) [MonadExceptOf ε m] : MonadExceptOf ε (StateT σ m) := { throw := StateT.lift ∘ throwThe ε tryCatch := fun x c s => tryCatchThe ε (x s) (fun e => c e s) } end end StateT section variable {σ : Type u} {m : Type u → Type v} instance [Monad m] : MonadStateOf σ (StateT σ m) where get := StateT.get set := StateT.set modifyGet := StateT.modifyGet end instance StateT.monadControl (σ : Type u) (m : Type u → Type v) [Monad m] : MonadControl m (StateT σ m) where stM := fun α => α × σ liftWith := fun f => do let s ← get; liftM (f (fun x => x.run s)) restoreM := fun x => do let (a, s) ← liftM x; set s; pure a instance StateT.tryFinally {m : Type u → Type v} {σ : Type u} [MonadFinally m] [Monad m] : MonadFinally (StateT σ m) where tryFinally' := fun x h s => do let ((a, _), (b, s'')) ← tryFinally' (x s) fun | some (a, s') => h (some a) s' | none => h none s pure ((a, b), s'')
bc1e3da349c7e5e369cb38be422ce39d6d8923c1
037dba89703a79cd4a4aec5e959818147f97635d
/src/2020/relations/partition_challenge_solution2.lean
a208b6c80e61ac54afb1cce56a50fdc578497602
[]
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
5,553
lean
import tactic /-! # Definition and basic API for partitions -/ /-- The structure of a partition on a Type α. -/ -- Let α be a type. A *partition* on α is defined to -- be the following data. @[ext] structure partition (α : Type) := -- A set C of subsets of α, called "blocks". (C : set (set α)) -- A hypothesis (a.k.a. a proof) that all the blocks are non-empty. (Hnonempty : ∀ X ∈ C, (X : set α).nonempty) -- A hypothesis that every term of type α is in one of the blocks. (Hcover : ∀ a, ∃ X ∈ C, a ∈ X) -- A hypothesis that two blocks with non-empty intersection are equal. (Hdisjoint : ∀ X Y ∈ C, (X ∩ Y : set α).nonempty → X = Y) namespace partition -- let α be a type, and fix a partition P on α. Let X and Y be subsets of α. variables {α : Type} {P : partition α} {X Y : set α} -- a more convenient way of putting it. theorem Hdisjoint' (hX : X ∈ P.C) (hY : Y ∈ P.C) : (X ∩ Y).nonempty → X = Y := P.Hdisjoint X hX Y hY -- another way theorem Hdisjoint'' (hX : X ∈ P.C) (hY : Y ∈ P.C) {a : α} (haX : a ∈ X) (haY : a ∈ Y) : X = Y := P.Hdisjoint _ hX _ hY ⟨a, haX, haY⟩ end partition section equivalence_classes /-! # Definition and basic API for equivalence classes We define equivalence classes and prove a few basic results about them. -/ -- Notation and variables for this section: -- let α be a type, and let R be an equivalence relation on R. variables {α : Type} {R : α → α → Prop} (hR : equivalence R) -- Always assume R is an equivalence relation, even when we don't need it. include hR /-- The equivalence class of `x` is all the `y` such that `y` is related to `x`. -/ def cl (x : α) := {y : α | R y x} /-- Useful for rewriting -- `y` is in the equivalence class of `x` iff `y` is related to `x`. True by definition. -/ theorem mem_cl_iff {x y : α} : x ∈ cl hR y ↔ R x y := iff.rfl /-- x is in cl(x) -/ lemma mem_class_self (x : α) : x ∈ cl hR x := begin rcases hR with ⟨hrefl, hsymm, htrans⟩, exact hrefl x, end lemma class_sub {x y : α} : x ∈ cl hR y → cl hR x ⊆ cl hR y := begin rcases hR with ⟨hrefl, hsymm, htrans⟩, intro hxy, intro z, intro hzx, exact htrans hzx hxy, end lemma class_eq {x y : α} : x ∈ cl hR y → cl hR x = cl hR y := begin intro hxy, apply set.subset.antisymm, apply class_sub hR hxy, apply class_sub hR, rcases hR with ⟨hrefl, hsymm, htrans⟩, exact hsymm hxy, end end equivalence_classes -- section /-! # Statement of the theorem -/ open partition -- There is a bijection between equivalence relations and partitions example (α : Type) : {R : α → α → Prop // equivalence R} ≃ partition α := { -- Let R be an equivalence relation. to_fun := λ R, { -- Let C be the set of equivalence classes for R. C := { B : set α | ∃ x : α, B = cl R.2 x}, -- I claim that C is a partition. We need to check three things. Hnonempty := begin -- If c is a block then c is nonempty. rintros c ⟨x, rfl⟩, use x, apply mem_class_self R.2, end, Hcover := begin intro x, use (cl R.2 x), split, { use x, }, { apply mem_class_self, } end, Hdisjoint := begin rintros c ⟨x, rfl⟩ d ⟨y, rfl⟩ ⟨z, hzx, hzy⟩, cases R with R hR, erw ← class_eq hR hzx, erw ← class_eq hR hzy, end }, -- Conversely, say P is an partition. inv_fun := λ P, -- Let's define a binary relation by x ~ y iff there's a block they're both in ⟨λ a b, ∀ X ∈ P.C, a ∈ X → b ∈ X, begin -- I claim this is an equivalence relation. split, { -- It's reflexive intros a C hC haC, exact haC, }, split, { -- it's symmetric intros x y h C hC hyC, rcases P.Hcover x with ⟨D, hD, hxD⟩, convert hxD, apply P.Hdisjoint _ hC _ hD, use [y, hyC], exact h D hD hxD, }, { -- it's transitive intros x y z hxy hyx C hC hxC, apply hyx C hC, apply hxy C hC, exact hxC, } end⟩, -- If you start with the equivalence relation, and then make the partition -- and a new equivalence relation, you get back to where you started. left_inv := begin rintro ⟨R, hR⟩, simp, ext a b, rcases hR with ⟨hRr,hRs,hRt⟩, split, { intro f, specialize f a (hRr a), exact hRs f, }, { intros hab t hat, refine hRt _ hat, exact hRs hab, }, end, -- Similarly, if you start with the partition, and then make the -- equivalence relation, and then construct the corresponding partition -- into equivalence classes, you have the same partition you started with. right_inv := begin intro P, ext W, simp, split, { rintro ⟨a, rfl⟩, rcases P.Hcover a with ⟨X, hX, haX⟩, convert hX, ext b, rw mem_cl_iff, split, { intro h, rcases P.Hcover b with ⟨Y, hY, hbY⟩, specialize h Y hY hbY, rwa Hdisjoint'' hX hY haX h, }, { intros hbX Y hY hbY, rwa Hdisjoint'' hY hX hbY hbX, } }, { intro hW, cases P.Hnonempty W hW with a haW, use a, ext b, rw mem_cl_iff, split, { intro hbW, intros X hX hbX, rwa Hdisjoint'' hX hW hbX hbW }, { intro haX, rcases P.Hcover b with ⟨X, hX, hbX⟩, specialize haX X hX hbX, rwa Hdisjoint'' hW hX haW haX } } end }
ffaa3d17816fa378f2dbaeded00c96bdf880d0d3
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/implicit2.lean
b6fc8c1e77eec0c7a29f46d30d00b68a1c22cf32
[ "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
317
lean
import Real. variable f {A : Type} (x y : A) : A check f 10 20 check f 10 check @f variable g {A : Type} (x1 x2 : A) {B : Type} (y1 y2 : B) : B check g 10 20 true check let r : Real -> Real -> Real := g 10 20 in r check g 10 set_option pp::implicit true check let r : Real -> Real -> Real := g 10 20 in r